blob: 4e69a0cad830a5b4fe2b048e75707b3419bb2027 [file] [log] [blame]
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "code_generator_x86.h"
18#include "utils/assembler.h"
19#include "utils/x86/assembler_x86.h"
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +010020#include "utils/x86/managed_register_x86.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000021
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070022#include "entrypoints/quick/quick_entrypoints.h"
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +000023#include "mirror/array.h"
24#include "mirror/art_method.h"
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070025#include "thread.h"
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +000026
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000027namespace art {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +010028
29x86::X86ManagedRegister Location::AsX86() const {
30 return reg().AsX86();
31}
32
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000033namespace x86 {
34
Nicolas Geoffraye5038322014-07-04 09:41:32 +010035#define __ reinterpret_cast<X86Assembler*>(codegen->GetAssembler())->
36
37class NullCheckSlowPathX86 : public SlowPathCode {
38 public:
39 explicit NullCheckSlowPathX86(uint32_t dex_pc) : dex_pc_(dex_pc) {}
40
41 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
42 __ Bind(GetEntryLabel());
43 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pThrowNullPointer)));
44 codegen->RecordPcInfo(dex_pc_);
45 }
46
47 private:
48 const uint32_t dex_pc_;
49 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathX86);
50};
51
52#undef __
53#define __ reinterpret_cast<X86Assembler*>(GetAssembler())->
54
Dave Allison20dfc792014-06-16 20:44:29 -070055inline Condition X86Condition(IfCondition cond) {
56 switch (cond) {
57 case kCondEQ: return kEqual;
58 case kCondNE: return kNotEqual;
59 case kCondLT: return kLess;
60 case kCondLE: return kLessEqual;
61 case kCondGT: return kGreater;
62 case kCondGE: return kGreaterEqual;
63 default:
64 LOG(FATAL) << "Unknown if condition";
65 }
66 return kEqual;
67}
68
Nicolas Geoffray4a34a422014-04-03 10:38:37 +010069static constexpr int kNumberOfPushedRegistersAtEntry = 1;
70static constexpr int kCurrentMethodStackOffset = 0;
71
Nicolas Geoffraya7062e02014-05-22 12:50:17 +010072void CodeGeneratorX86::DumpCoreRegister(std::ostream& stream, int reg) const {
73 stream << X86ManagedRegister::FromCpuRegister(Register(reg));
74}
75
76void CodeGeneratorX86::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
77 stream << X86ManagedRegister::FromXmmRegister(XmmRegister(reg));
78}
79
Nicolas Geoffraya7aca372014-04-28 17:47:12 +010080CodeGeneratorX86::CodeGeneratorX86(HGraph* graph)
81 : CodeGenerator(graph, kNumberOfRegIds),
82 location_builder_(graph, this),
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +010083 instruction_visitor_(graph, this),
84 move_resolver_(graph->GetArena(), this) {}
85
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +010086size_t CodeGeneratorX86::FrameEntrySpillSize() const {
87 return kNumberOfPushedRegistersAtEntry * kX86WordSize;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +010088}
Nicolas Geoffraya7aca372014-04-28 17:47:12 +010089
90static bool* GetBlockedRegisterPairs(bool* blocked_registers) {
91 return blocked_registers + kNumberOfAllocIds;
92}
93
94ManagedRegister CodeGeneratorX86::AllocateFreeRegister(Primitive::Type type,
95 bool* blocked_registers) const {
96 switch (type) {
97 case Primitive::kPrimLong: {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +010098 bool* blocked_register_pairs = GetBlockedRegisterPairs(blocked_registers);
99 size_t reg = AllocateFreeRegisterInternal(blocked_register_pairs, kNumberOfRegisterPairs);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100100 X86ManagedRegister pair =
101 X86ManagedRegister::FromRegisterPair(static_cast<RegisterPair>(reg));
102 blocked_registers[pair.AsRegisterPairLow()] = true;
103 blocked_registers[pair.AsRegisterPairHigh()] = true;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100104 // Block all other register pairs that share a register with `pair`.
105 for (int i = 0; i < kNumberOfRegisterPairs; i++) {
106 X86ManagedRegister current =
107 X86ManagedRegister::FromRegisterPair(static_cast<RegisterPair>(i));
108 if (current.AsRegisterPairLow() == pair.AsRegisterPairLow()
109 || current.AsRegisterPairLow() == pair.AsRegisterPairHigh()
110 || current.AsRegisterPairHigh() == pair.AsRegisterPairLow()
111 || current.AsRegisterPairHigh() == pair.AsRegisterPairHigh()) {
112 blocked_register_pairs[i] = true;
113 }
114 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100115 return pair;
116 }
117
118 case Primitive::kPrimByte:
119 case Primitive::kPrimBoolean:
120 case Primitive::kPrimChar:
121 case Primitive::kPrimShort:
122 case Primitive::kPrimInt:
123 case Primitive::kPrimNot: {
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100124 Register reg = static_cast<Register>(
125 AllocateFreeRegisterInternal(blocked_registers, kNumberOfCpuRegisters));
126 // Block all register pairs that contain `reg`.
127 bool* blocked_register_pairs = GetBlockedRegisterPairs(blocked_registers);
128 for (int i = 0; i < kNumberOfRegisterPairs; i++) {
129 X86ManagedRegister current =
130 X86ManagedRegister::FromRegisterPair(static_cast<RegisterPair>(i));
131 if (current.AsRegisterPairLow() == reg || current.AsRegisterPairHigh() == reg) {
132 blocked_register_pairs[i] = true;
133 }
134 }
135 return X86ManagedRegister::FromCpuRegister(reg);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100136 }
137
138 case Primitive::kPrimFloat:
139 case Primitive::kPrimDouble:
140 LOG(FATAL) << "Unimplemented register type " << type;
141
142 case Primitive::kPrimVoid:
143 LOG(FATAL) << "Unreachable type " << type;
144 }
145
146 return ManagedRegister::NoRegister();
147}
148
149void CodeGeneratorX86::SetupBlockedRegisters(bool* blocked_registers) const {
150 bool* blocked_register_pairs = GetBlockedRegisterPairs(blocked_registers);
151
152 // Don't allocate the dalvik style register pair passing.
153 blocked_register_pairs[ECX_EDX] = true;
154
155 // Stack register is always reserved.
156 blocked_registers[ESP] = true;
157
158 // TODO: We currently don't use Quick's callee saved registers.
159 blocked_registers[EBP] = true;
160 blocked_registers[ESI] = true;
161 blocked_registers[EDI] = true;
162 blocked_register_pairs[EAX_EDI] = true;
163 blocked_register_pairs[EDX_EDI] = true;
164 blocked_register_pairs[ECX_EDI] = true;
165 blocked_register_pairs[EBX_EDI] = true;
166}
167
168size_t CodeGeneratorX86::GetNumberOfRegisters() const {
169 return kNumberOfRegIds;
170}
171
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100172static Location X86CpuLocation(Register reg) {
173 return Location::RegisterLocation(X86ManagedRegister::FromCpuRegister(reg));
174}
175
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100176InstructionCodeGeneratorX86::InstructionCodeGeneratorX86(HGraph* graph, CodeGeneratorX86* codegen)
177 : HGraphVisitor(graph),
178 assembler_(codegen->GetAssembler()),
179 codegen_(codegen) {}
180
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000181void CodeGeneratorX86::GenerateFrameEntry() {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000182 // Create a fake register to mimic Quick.
183 static const int kFakeReturnRegister = 8;
184 core_spill_mask_ |= (1 << kFakeReturnRegister);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000185
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100186 // The return PC has already been pushed on the stack.
Nicolas Geoffray707c8092014-04-04 10:50:14 +0100187 __ subl(ESP, Immediate(GetFrameSize() - kNumberOfPushedRegistersAtEntry * kX86WordSize));
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100188 __ movl(Address(ESP, kCurrentMethodStackOffset), EAX);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000189}
190
191void CodeGeneratorX86::GenerateFrameExit() {
Nicolas Geoffray707c8092014-04-04 10:50:14 +0100192 __ addl(ESP, Immediate(GetFrameSize() - kNumberOfPushedRegistersAtEntry * kX86WordSize));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000193}
194
195void CodeGeneratorX86::Bind(Label* label) {
196 __ Bind(label);
197}
198
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000199void InstructionCodeGeneratorX86::LoadCurrentMethod(Register reg) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100200 __ movl(reg, Address(ESP, kCurrentMethodStackOffset));
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000201}
202
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100203Location CodeGeneratorX86::GetStackLocation(HLoadLocal* load) const {
204 switch (load->GetType()) {
205 case Primitive::kPrimLong:
206 return Location::DoubleStackSlot(GetStackSlot(load->GetLocal()));
207 break;
208
209 case Primitive::kPrimInt:
210 case Primitive::kPrimNot:
211 return Location::StackSlot(GetStackSlot(load->GetLocal()));
212
213 case Primitive::kPrimFloat:
214 case Primitive::kPrimDouble:
215 LOG(FATAL) << "Unimplemented type " << load->GetType();
216
217 case Primitive::kPrimBoolean:
218 case Primitive::kPrimByte:
219 case Primitive::kPrimChar:
220 case Primitive::kPrimShort:
221 case Primitive::kPrimVoid:
222 LOG(FATAL) << "Unexpected type " << load->GetType();
223 }
224
225 LOG(FATAL) << "Unreachable";
226 return Location();
227}
228
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100229static constexpr Register kRuntimeParameterCoreRegisters[] = { EAX, ECX, EDX };
230static constexpr size_t kRuntimeParameterCoreRegistersLength =
231 arraysize(kRuntimeParameterCoreRegisters);
232
233class InvokeRuntimeCallingConvention : public CallingConvention<Register> {
234 public:
235 InvokeRuntimeCallingConvention()
236 : CallingConvention(kRuntimeParameterCoreRegisters,
237 kRuntimeParameterCoreRegistersLength) {}
238
239 private:
240 DISALLOW_COPY_AND_ASSIGN(InvokeRuntimeCallingConvention);
241};
242
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100243Location InvokeDexCallingConventionVisitor::GetNextLocation(Primitive::Type type) {
244 switch (type) {
245 case Primitive::kPrimBoolean:
246 case Primitive::kPrimByte:
247 case Primitive::kPrimChar:
248 case Primitive::kPrimShort:
249 case Primitive::kPrimInt:
250 case Primitive::kPrimNot: {
251 uint32_t index = gp_index_++;
252 if (index < calling_convention.GetNumberOfRegisters()) {
253 return X86CpuLocation(calling_convention.GetRegisterAt(index));
254 } else {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100255 return Location::StackSlot(calling_convention.GetStackOffsetOf(index));
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100256 }
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100257 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100258
259 case Primitive::kPrimLong: {
260 uint32_t index = gp_index_;
261 gp_index_ += 2;
262 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
263 return Location::RegisterLocation(X86ManagedRegister::FromRegisterPair(
264 calling_convention.GetRegisterPairAt(index)));
265 } else if (index + 1 == calling_convention.GetNumberOfRegisters()) {
266 return Location::QuickParameter(index);
267 } else {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100268 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(index));
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100269 }
270 }
271
272 case Primitive::kPrimDouble:
273 case Primitive::kPrimFloat:
274 LOG(FATAL) << "Unimplemented parameter type " << type;
275 break;
276
277 case Primitive::kPrimVoid:
278 LOG(FATAL) << "Unexpected parameter type " << type;
279 break;
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100280 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100281 return Location();
282}
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100283
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100284void CodeGeneratorX86::Move32(Location destination, Location source) {
285 if (source.Equals(destination)) {
286 return;
287 }
288 if (destination.IsRegister()) {
289 if (source.IsRegister()) {
290 __ movl(destination.AsX86().AsCpuRegister(), source.AsX86().AsCpuRegister());
291 } else {
292 DCHECK(source.IsStackSlot());
293 __ movl(destination.AsX86().AsCpuRegister(), Address(ESP, source.GetStackIndex()));
294 }
295 } else {
296 if (source.IsRegister()) {
297 __ movl(Address(ESP, destination.GetStackIndex()), source.AsX86().AsCpuRegister());
298 } else {
299 DCHECK(source.IsStackSlot());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100300 __ pushl(Address(ESP, source.GetStackIndex()));
301 __ popl(Address(ESP, destination.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100302 }
303 }
304}
305
306void CodeGeneratorX86::Move64(Location destination, Location source) {
307 if (source.Equals(destination)) {
308 return;
309 }
310 if (destination.IsRegister()) {
311 if (source.IsRegister()) {
312 __ movl(destination.AsX86().AsRegisterPairLow(), source.AsX86().AsRegisterPairLow());
313 __ movl(destination.AsX86().AsRegisterPairHigh(), source.AsX86().AsRegisterPairHigh());
314 } else if (source.IsQuickParameter()) {
315 uint32_t argument_index = source.GetQuickParameterIndex();
316 InvokeDexCallingConvention calling_convention;
317 __ movl(destination.AsX86().AsRegisterPairLow(),
318 calling_convention.GetRegisterAt(argument_index));
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100319 __ movl(destination.AsX86().AsRegisterPairHigh(), Address(ESP,
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100320 calling_convention.GetStackOffsetOf(argument_index + 1) + GetFrameSize()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100321 } else {
322 DCHECK(source.IsDoubleStackSlot());
323 __ movl(destination.AsX86().AsRegisterPairLow(), Address(ESP, source.GetStackIndex()));
324 __ movl(destination.AsX86().AsRegisterPairHigh(),
325 Address(ESP, source.GetHighStackIndex(kX86WordSize)));
326 }
327 } else if (destination.IsQuickParameter()) {
328 InvokeDexCallingConvention calling_convention;
329 uint32_t argument_index = destination.GetQuickParameterIndex();
330 if (source.IsRegister()) {
331 __ movl(calling_convention.GetRegisterAt(argument_index), source.AsX86().AsRegisterPairLow());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100332 __ movl(Address(ESP, calling_convention.GetStackOffsetOf(argument_index + 1)),
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100333 source.AsX86().AsRegisterPairHigh());
334 } else {
335 DCHECK(source.IsDoubleStackSlot());
336 __ movl(calling_convention.GetRegisterAt(argument_index),
337 Address(ESP, source.GetStackIndex()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100338 __ pushl(Address(ESP, source.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100339 __ popl(Address(ESP, calling_convention.GetStackOffsetOf(argument_index + 1)));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100340 }
341 } else {
342 if (source.IsRegister()) {
343 __ movl(Address(ESP, destination.GetStackIndex()), source.AsX86().AsRegisterPairLow());
344 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)),
345 source.AsX86().AsRegisterPairHigh());
346 } else if (source.IsQuickParameter()) {
347 InvokeDexCallingConvention calling_convention;
348 uint32_t argument_index = source.GetQuickParameterIndex();
349 __ movl(Address(ESP, destination.GetStackIndex()),
350 calling_convention.GetRegisterAt(argument_index));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100351 __ pushl(Address(ESP,
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100352 calling_convention.GetStackOffsetOf(argument_index + 1) + GetFrameSize()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100353 __ popl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100354 } else {
355 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100356 __ pushl(Address(ESP, source.GetStackIndex()));
357 __ popl(Address(ESP, destination.GetStackIndex()));
358 __ pushl(Address(ESP, source.GetHighStackIndex(kX86WordSize)));
359 __ popl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100360 }
361 }
362}
363
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100364void CodeGeneratorX86::Move(HInstruction* instruction, Location location, HInstruction* move_for) {
365 if (instruction->AsIntConstant() != nullptr) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100366 Immediate imm(instruction->AsIntConstant()->GetValue());
367 if (location.IsRegister()) {
368 __ movl(location.AsX86().AsCpuRegister(), imm);
369 } else {
370 __ movl(Address(ESP, location.GetStackIndex()), imm);
371 }
372 } else if (instruction->AsLongConstant() != nullptr) {
373 int64_t value = instruction->AsLongConstant()->GetValue();
374 if (location.IsRegister()) {
375 __ movl(location.AsX86().AsRegisterPairLow(), Immediate(Low32Bits(value)));
376 __ movl(location.AsX86().AsRegisterPairHigh(), Immediate(High32Bits(value)));
377 } else {
378 __ movl(Address(ESP, location.GetStackIndex()), Immediate(Low32Bits(value)));
379 __ movl(Address(ESP, location.GetHighStackIndex(kX86WordSize)), Immediate(High32Bits(value)));
380 }
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100381 } else if (instruction->AsLoadLocal() != nullptr) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100382 switch (instruction->GetType()) {
383 case Primitive::kPrimBoolean:
384 case Primitive::kPrimByte:
385 case Primitive::kPrimChar:
386 case Primitive::kPrimShort:
387 case Primitive::kPrimInt:
388 case Primitive::kPrimNot:
389 Move32(location, Location::StackSlot(GetStackSlot(instruction->AsLoadLocal()->GetLocal())));
390 break;
391
392 case Primitive::kPrimLong:
393 Move64(location, Location::DoubleStackSlot(
394 GetStackSlot(instruction->AsLoadLocal()->GetLocal())));
395 break;
396
397 default:
398 LOG(FATAL) << "Unimplemented local type " << instruction->GetType();
399 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000400 } else {
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100401 DCHECK((instruction->GetNext() == move_for) || instruction->GetNext()->IsTemporary());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100402 switch (instruction->GetType()) {
403 case Primitive::kPrimBoolean:
404 case Primitive::kPrimByte:
405 case Primitive::kPrimChar:
406 case Primitive::kPrimShort:
407 case Primitive::kPrimInt:
408 case Primitive::kPrimNot:
409 Move32(location, instruction->GetLocations()->Out());
410 break;
411
412 case Primitive::kPrimLong:
413 Move64(location, instruction->GetLocations()->Out());
414 break;
415
416 default:
417 LOG(FATAL) << "Unimplemented type " << instruction->GetType();
418 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000419 }
420}
421
422void LocationsBuilderX86::VisitGoto(HGoto* got) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000423 got->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000424}
425
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000426void InstructionCodeGeneratorX86::VisitGoto(HGoto* got) {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000427 HBasicBlock* successor = got->GetSuccessor();
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000428 if (GetGraph()->GetExitBlock() == successor) {
429 codegen_->GenerateFrameExit();
430 } else if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
431 __ jmp(codegen_->GetLabelOf(successor));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000432 }
433}
434
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000435void LocationsBuilderX86::VisitExit(HExit* exit) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000436 exit->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000437}
438
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000439void InstructionCodeGeneratorX86::VisitExit(HExit* exit) {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000440 if (kIsDebugBuild) {
441 __ Comment("Unreachable");
442 __ int3();
443 }
444}
445
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000446void LocationsBuilderX86::VisitIf(HIf* if_instr) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000447 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100448 HInstruction* cond = if_instr->InputAt(0);
449 DCHECK(cond->IsCondition());
450 HCondition* condition = cond->AsCondition();
451 if (condition->NeedsMaterialization()) {
452 locations->SetInAt(0, Location::Any());
453 }
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000454 if_instr->SetLocations(locations);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000455}
456
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000457void InstructionCodeGeneratorX86::VisitIf(HIf* if_instr) {
Dave Allison20dfc792014-06-16 20:44:29 -0700458 HInstruction* cond = if_instr->InputAt(0);
459 DCHECK(cond->IsCondition());
460 HCondition* condition = cond->AsCondition();
461 if (condition->NeedsMaterialization()) {
462 // Materialized condition, compare against 0
463 Location lhs = if_instr->GetLocations()->InAt(0);
464 if (lhs.IsRegister()) {
465 __ cmpl(lhs.AsX86().AsCpuRegister(), Immediate(0));
466 } else {
467 __ cmpl(Address(ESP, lhs.GetStackIndex()), Immediate(0));
468 }
469 __ j(kEqual, codegen_->GetLabelOf(if_instr->IfTrueSuccessor()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100470 } else {
Dave Allison20dfc792014-06-16 20:44:29 -0700471 Location lhs = condition->GetLocations()->InAt(0);
472 Location rhs = condition->GetLocations()->InAt(1);
473 // LHS is guaranteed to be in a register (see LocationsBuilderX86::VisitCondition).
474 if (rhs.IsRegister()) {
475 __ cmpl(lhs.AsX86().AsCpuRegister(), rhs.AsX86().AsCpuRegister());
476 } else {
477 __ cmpl(lhs.AsX86().AsCpuRegister(), Address(ESP, rhs.GetStackIndex()));
478 }
479 __ j(X86Condition(condition->GetCondition()),
480 codegen_->GetLabelOf(if_instr->IfTrueSuccessor()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100481 }
Dave Allison20dfc792014-06-16 20:44:29 -0700482 if (!codegen_->GoesToNextBlock(if_instr->GetBlock(), if_instr->IfFalseSuccessor())) {
483 __ jmp(codegen_->GetLabelOf(if_instr->IfFalseSuccessor()));
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000484 }
485}
486
487void LocationsBuilderX86::VisitLocal(HLocal* local) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000488 local->SetLocations(nullptr);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000489}
490
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000491void InstructionCodeGeneratorX86::VisitLocal(HLocal* local) {
492 DCHECK_EQ(local->GetBlock(), GetGraph()->GetEntryBlock());
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000493}
494
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000495void LocationsBuilderX86::VisitLoadLocal(HLoadLocal* local) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100496 local->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000497}
498
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000499void InstructionCodeGeneratorX86::VisitLoadLocal(HLoadLocal* load) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100500 // Nothing to do, this is driven by the code generator.
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000501}
502
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100503void LocationsBuilderX86::VisitStoreLocal(HStoreLocal* store) {
504 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(store);
505 switch (store->InputAt(1)->GetType()) {
506 case Primitive::kPrimBoolean:
507 case Primitive::kPrimByte:
508 case Primitive::kPrimChar:
509 case Primitive::kPrimShort:
510 case Primitive::kPrimInt:
511 case Primitive::kPrimNot:
512 locations->SetInAt(1, Location::StackSlot(codegen_->GetStackSlot(store->GetLocal())));
513 break;
514
515 case Primitive::kPrimLong:
516 locations->SetInAt(1, Location::DoubleStackSlot(codegen_->GetStackSlot(store->GetLocal())));
517 break;
518
519 default:
520 LOG(FATAL) << "Unimplemented local type " << store->InputAt(1)->GetType();
521 }
522 store->SetLocations(locations);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000523}
524
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000525void InstructionCodeGeneratorX86::VisitStoreLocal(HStoreLocal* store) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000526}
527
Dave Allison20dfc792014-06-16 20:44:29 -0700528void LocationsBuilderX86::VisitCondition(HCondition* comp) {
529 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(comp);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100530 locations->SetInAt(0, Location::RequiresRegister());
531 locations->SetInAt(1, Location::Any());
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100532 if (comp->NeedsMaterialization()) {
533 locations->SetOut(Location::SameAsFirstInput());
534 }
Dave Allison20dfc792014-06-16 20:44:29 -0700535 comp->SetLocations(locations);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000536}
537
Dave Allison20dfc792014-06-16 20:44:29 -0700538void InstructionCodeGeneratorX86::VisitCondition(HCondition* comp) {
539 if (comp->NeedsMaterialization()) {
540 LocationSummary* locations = comp->GetLocations();
541 if (locations->InAt(1).IsRegister()) {
542 __ cmpl(locations->InAt(0).AsX86().AsCpuRegister(),
543 locations->InAt(1).AsX86().AsCpuRegister());
544 } else {
545 __ cmpl(locations->InAt(0).AsX86().AsCpuRegister(),
546 Address(ESP, locations->InAt(1).GetStackIndex()));
547 }
548 __ setb(X86Condition(comp->GetCondition()), locations->Out().AsX86().AsCpuRegister());
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100549 }
Dave Allison20dfc792014-06-16 20:44:29 -0700550}
551
552void LocationsBuilderX86::VisitEqual(HEqual* comp) {
553 VisitCondition(comp);
554}
555
556void InstructionCodeGeneratorX86::VisitEqual(HEqual* comp) {
557 VisitCondition(comp);
558}
559
560void LocationsBuilderX86::VisitNotEqual(HNotEqual* comp) {
561 VisitCondition(comp);
562}
563
564void InstructionCodeGeneratorX86::VisitNotEqual(HNotEqual* comp) {
565 VisitCondition(comp);
566}
567
568void LocationsBuilderX86::VisitLessThan(HLessThan* comp) {
569 VisitCondition(comp);
570}
571
572void InstructionCodeGeneratorX86::VisitLessThan(HLessThan* comp) {
573 VisitCondition(comp);
574}
575
576void LocationsBuilderX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
577 VisitCondition(comp);
578}
579
580void InstructionCodeGeneratorX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
581 VisitCondition(comp);
582}
583
584void LocationsBuilderX86::VisitGreaterThan(HGreaterThan* comp) {
585 VisitCondition(comp);
586}
587
588void InstructionCodeGeneratorX86::VisitGreaterThan(HGreaterThan* comp) {
589 VisitCondition(comp);
590}
591
592void LocationsBuilderX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
593 VisitCondition(comp);
594}
595
596void InstructionCodeGeneratorX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
597 VisitCondition(comp);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000598}
599
600void LocationsBuilderX86::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100601 // TODO: Support constant locations.
602 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
603 locations->SetOut(Location::RequiresRegister());
604 constant->SetLocations(locations);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000605}
606
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000607void InstructionCodeGeneratorX86::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100608 codegen_->Move(constant, constant->GetLocations()->Out(), nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000609}
610
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100611void LocationsBuilderX86::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100612 // TODO: Support constant locations.
613 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
614 locations->SetOut(Location::RequiresRegister());
615 constant->SetLocations(locations);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100616}
617
618void InstructionCodeGeneratorX86::VisitLongConstant(HLongConstant* constant) {
619 // Will be generated at use site.
620}
621
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000622void LocationsBuilderX86::VisitReturnVoid(HReturnVoid* ret) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000623 ret->SetLocations(nullptr);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000624}
625
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000626void InstructionCodeGeneratorX86::VisitReturnVoid(HReturnVoid* ret) {
627 codegen_->GenerateFrameExit();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000628 __ ret();
629}
630
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000631void LocationsBuilderX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000632 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(ret);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100633 switch (ret->InputAt(0)->GetType()) {
634 case Primitive::kPrimBoolean:
635 case Primitive::kPrimByte:
636 case Primitive::kPrimChar:
637 case Primitive::kPrimShort:
638 case Primitive::kPrimInt:
639 case Primitive::kPrimNot:
640 locations->SetInAt(0, X86CpuLocation(EAX));
641 break;
642
643 case Primitive::kPrimLong:
644 locations->SetInAt(
645 0, Location::RegisterLocation(X86ManagedRegister::FromRegisterPair(EAX_EDX)));
646 break;
647
648 default:
649 LOG(FATAL) << "Unimplemented return type " << ret->InputAt(0)->GetType();
650 }
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000651 ret->SetLocations(locations);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000652}
653
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000654void InstructionCodeGeneratorX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100655 if (kIsDebugBuild) {
656 switch (ret->InputAt(0)->GetType()) {
657 case Primitive::kPrimBoolean:
658 case Primitive::kPrimByte:
659 case Primitive::kPrimChar:
660 case Primitive::kPrimShort:
661 case Primitive::kPrimInt:
662 case Primitive::kPrimNot:
663 DCHECK_EQ(ret->GetLocations()->InAt(0).AsX86().AsCpuRegister(), EAX);
664 break;
665
666 case Primitive::kPrimLong:
667 DCHECK_EQ(ret->GetLocations()->InAt(0).AsX86().AsRegisterPair(), EAX_EDX);
668 break;
669
670 default:
671 LOG(FATAL) << "Unimplemented return type " << ret->InputAt(0)->GetType();
672 }
673 }
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000674 codegen_->GenerateFrameExit();
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000675 __ ret();
676}
677
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000678void LocationsBuilderX86::VisitInvokeStatic(HInvokeStatic* invoke) {
679 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(invoke);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100680 locations->AddTemp(X86CpuLocation(EAX));
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100681
682 InvokeDexCallingConventionVisitor calling_convention_visitor;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100683 for (size_t i = 0; i < invoke->InputCount(); i++) {
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100684 HInstruction* input = invoke->InputAt(i);
685 locations->SetInAt(i, calling_convention_visitor.GetNextLocation(input->GetType()));
686 }
687
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100688 switch (invoke->GetType()) {
689 case Primitive::kPrimBoolean:
690 case Primitive::kPrimByte:
691 case Primitive::kPrimChar:
692 case Primitive::kPrimShort:
693 case Primitive::kPrimInt:
694 case Primitive::kPrimNot:
695 locations->SetOut(X86CpuLocation(EAX));
696 break;
697
698 case Primitive::kPrimLong:
699 locations->SetOut(Location::RegisterLocation(X86ManagedRegister::FromRegisterPair(EAX_EDX)));
700 break;
701
702 case Primitive::kPrimVoid:
703 break;
704
705 case Primitive::kPrimDouble:
706 case Primitive::kPrimFloat:
707 LOG(FATAL) << "Unimplemented return type " << invoke->GetType();
708 break;
709 }
710
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000711 invoke->SetLocations(locations);
712}
713
714void InstructionCodeGeneratorX86::VisitInvokeStatic(HInvokeStatic* invoke) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100715 Register temp = invoke->GetLocations()->GetTemp(0).AsX86().AsCpuRegister();
Nicolas Geoffrayf61b5372014-06-25 14:35:34 +0100716 uint32_t heap_reference_size = sizeof(mirror::HeapReference<mirror::Object>);
717 size_t index_in_cache = mirror::Array::DataOffset(heap_reference_size).Int32Value() +
Nicolas Geoffray707c8092014-04-04 10:50:14 +0100718 invoke->GetIndexInDexCache() * kX86WordSize;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000719
720 // TODO: Implement all kinds of calls:
721 // 1) boot -> boot
722 // 2) app -> boot
723 // 3) app -> app
724 //
725 // Currently we implement the app -> app logic, which looks up in the resolve cache.
726
727 // temp = method;
728 LoadCurrentMethod(temp);
729 // temp = temp->dex_cache_resolved_methods_;
730 __ movl(temp, Address(temp, mirror::ArtMethod::DexCacheResolvedMethodsOffset().Int32Value()));
731 // temp = temp[index_in_cache]
732 __ movl(temp, Address(temp, index_in_cache));
733 // (temp + offset_of_quick_compiled_code)()
734 __ call(Address(temp, mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset().Int32Value()));
735
736 codegen_->RecordPcInfo(invoke->GetDexPc());
737}
738
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +0000739void LocationsBuilderX86::VisitAdd(HAdd* add) {
740 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(add);
741 switch (add->GetResultType()) {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100742 case Primitive::kPrimInt:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100743 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100744 locations->SetInAt(0, Location::RequiresRegister());
745 locations->SetInAt(1, Location::Any());
746 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100747 break;
748 }
749
750 case Primitive::kPrimBoolean:
751 case Primitive::kPrimByte:
752 case Primitive::kPrimChar:
753 case Primitive::kPrimShort:
754 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
755 break;
756
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +0000757 default:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100758 LOG(FATAL) << "Unimplemented add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +0000759 }
760 add->SetLocations(locations);
761}
762
763void InstructionCodeGeneratorX86::VisitAdd(HAdd* add) {
764 LocationSummary* locations = add->GetLocations();
765 switch (add->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100766 case Primitive::kPrimInt: {
767 DCHECK_EQ(locations->InAt(0).AsX86().AsCpuRegister(),
768 locations->Out().AsX86().AsCpuRegister());
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100769 if (locations->InAt(1).IsRegister()) {
770 __ addl(locations->InAt(0).AsX86().AsCpuRegister(),
771 locations->InAt(1).AsX86().AsCpuRegister());
772 } else {
773 __ addl(locations->InAt(0).AsX86().AsCpuRegister(),
774 Address(ESP, locations->InAt(1).GetStackIndex()));
775 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +0000776 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100777 }
778
779 case Primitive::kPrimLong: {
780 DCHECK_EQ(locations->InAt(0).AsX86().AsRegisterPair(),
781 locations->Out().AsX86().AsRegisterPair());
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100782 if (locations->InAt(1).IsRegister()) {
783 __ addl(locations->InAt(0).AsX86().AsRegisterPairLow(),
784 locations->InAt(1).AsX86().AsRegisterPairLow());
785 __ adcl(locations->InAt(0).AsX86().AsRegisterPairHigh(),
786 locations->InAt(1).AsX86().AsRegisterPairHigh());
787 } else {
788 __ addl(locations->InAt(0).AsX86().AsRegisterPairLow(),
789 Address(ESP, locations->InAt(1).GetStackIndex()));
790 __ adcl(locations->InAt(0).AsX86().AsRegisterPairHigh(),
791 Address(ESP, locations->InAt(1).GetHighStackIndex(kX86WordSize)));
792 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100793 break;
794 }
795
796 case Primitive::kPrimBoolean:
797 case Primitive::kPrimByte:
798 case Primitive::kPrimChar:
799 case Primitive::kPrimShort:
800 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
801 break;
802
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +0000803 default:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100804 LOG(FATAL) << "Unimplemented add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +0000805 }
806}
807
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100808void LocationsBuilderX86::VisitSub(HSub* sub) {
809 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(sub);
810 switch (sub->GetResultType()) {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100811 case Primitive::kPrimInt:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100812 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100813 locations->SetInAt(0, Location::RequiresRegister());
814 locations->SetInAt(1, Location::Any());
815 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100816 break;
817 }
818
819 case Primitive::kPrimBoolean:
820 case Primitive::kPrimByte:
821 case Primitive::kPrimChar:
822 case Primitive::kPrimShort:
823 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
824 break;
825
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100826 default:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100827 LOG(FATAL) << "Unimplemented sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100828 }
829 sub->SetLocations(locations);
830}
831
832void InstructionCodeGeneratorX86::VisitSub(HSub* sub) {
833 LocationSummary* locations = sub->GetLocations();
834 switch (sub->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100835 case Primitive::kPrimInt: {
836 DCHECK_EQ(locations->InAt(0).AsX86().AsCpuRegister(),
837 locations->Out().AsX86().AsCpuRegister());
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100838 if (locations->InAt(1).IsRegister()) {
839 __ subl(locations->InAt(0).AsX86().AsCpuRegister(),
840 locations->InAt(1).AsX86().AsCpuRegister());
841 } else {
842 __ subl(locations->InAt(0).AsX86().AsCpuRegister(),
843 Address(ESP, locations->InAt(1).GetStackIndex()));
844 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100845 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100846 }
847
848 case Primitive::kPrimLong: {
849 DCHECK_EQ(locations->InAt(0).AsX86().AsRegisterPair(),
850 locations->Out().AsX86().AsRegisterPair());
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100851 if (locations->InAt(1).IsRegister()) {
852 __ subl(locations->InAt(0).AsX86().AsRegisterPairLow(),
853 locations->InAt(1).AsX86().AsRegisterPairLow());
854 __ sbbl(locations->InAt(0).AsX86().AsRegisterPairHigh(),
855 locations->InAt(1).AsX86().AsRegisterPairHigh());
856 } else {
857 __ subl(locations->InAt(0).AsX86().AsRegisterPairLow(),
858 Address(ESP, locations->InAt(1).GetStackIndex()));
859 __ sbbl(locations->InAt(0).AsX86().AsRegisterPairHigh(),
860 Address(ESP, locations->InAt(1).GetHighStackIndex(kX86WordSize)));
861 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100862 break;
863 }
864
865 case Primitive::kPrimBoolean:
866 case Primitive::kPrimByte:
867 case Primitive::kPrimChar:
868 case Primitive::kPrimShort:
869 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
870 break;
871
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100872 default:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100873 LOG(FATAL) << "Unimplemented sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100874 }
875}
876
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +0100877void LocationsBuilderX86::VisitNewInstance(HNewInstance* instruction) {
878 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100879 locations->SetOut(X86CpuLocation(EAX));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100880 InvokeRuntimeCallingConvention calling_convention;
881 locations->AddTemp(X86CpuLocation(calling_convention.GetRegisterAt(0)));
882 locations->AddTemp(X86CpuLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +0100883 instruction->SetLocations(locations);
884}
885
886void InstructionCodeGeneratorX86::VisitNewInstance(HNewInstance* instruction) {
887 InvokeRuntimeCallingConvention calling_convention;
888 LoadCurrentMethod(calling_convention.GetRegisterAt(1));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100889 __ movl(calling_convention.GetRegisterAt(0), Immediate(instruction->GetTypeIndex()));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +0100890
Nicolas Geoffray707c8092014-04-04 10:50:14 +0100891 __ fs()->call(
892 Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pAllocObjectWithAccessCheck)));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +0100893
894 codegen_->RecordPcInfo(instruction->GetDexPc());
895}
896
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100897void LocationsBuilderX86::VisitParameterValue(HParameterValue* instruction) {
898 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100899 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
900 if (location.IsStackSlot()) {
901 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
902 } else if (location.IsDoubleStackSlot()) {
903 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100904 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100905 locations->SetOut(location);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100906 instruction->SetLocations(locations);
907}
908
909void InstructionCodeGeneratorX86::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100910}
911
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +0100912void LocationsBuilderX86::VisitNot(HNot* instruction) {
913 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100914 locations->SetInAt(0, Location::RequiresRegister());
915 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +0100916 instruction->SetLocations(locations);
917}
918
919void InstructionCodeGeneratorX86::VisitNot(HNot* instruction) {
920 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100921 Location out = locations->Out();
922 DCHECK_EQ(locations->InAt(0).AsX86().AsCpuRegister(), out.AsX86().AsCpuRegister());
923 __ xorl(out.AsX86().AsCpuRegister(), Immediate(1));
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +0100924}
925
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100926void LocationsBuilderX86::VisitCompare(HCompare* compare) {
927 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(compare);
928 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100929 locations->SetInAt(1, Location::Any());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100930 locations->SetOut(Location::RequiresRegister());
931 compare->SetLocations(locations);
932}
933
934void InstructionCodeGeneratorX86::VisitCompare(HCompare* compare) {
935 Label greater, done;
936 LocationSummary* locations = compare->GetLocations();
937 switch (compare->InputAt(0)->GetType()) {
938 case Primitive::kPrimLong: {
939 Label less, greater, done;
940 Register output = locations->Out().AsX86().AsCpuRegister();
941 X86ManagedRegister left = locations->InAt(0).AsX86();
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100942 Location right = locations->InAt(1);
943 if (right.IsRegister()) {
944 __ cmpl(left.AsRegisterPairHigh(), right.AsX86().AsRegisterPairHigh());
945 } else {
946 DCHECK(right.IsDoubleStackSlot());
947 __ cmpl(left.AsRegisterPairHigh(), Address(ESP, right.GetHighStackIndex(kX86WordSize)));
948 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100949 __ j(kLess, &less); // Signed compare.
950 __ j(kGreater, &greater); // Signed compare.
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100951 if (right.IsRegister()) {
952 __ cmpl(left.AsRegisterPairLow(), right.AsX86().AsRegisterPairLow());
953 } else {
954 DCHECK(right.IsDoubleStackSlot());
955 __ cmpl(left.AsRegisterPairLow(), Address(ESP, right.GetStackIndex()));
956 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100957 __ movl(output, Immediate(0));
958 __ j(kEqual, &done);
959 __ j(kBelow, &less); // Unsigned compare.
960
961 __ Bind(&greater);
962 __ movl(output, Immediate(1));
963 __ jmp(&done);
964
965 __ Bind(&less);
966 __ movl(output, Immediate(-1));
967
968 __ Bind(&done);
969 break;
970 }
971 default:
972 LOG(FATAL) << "Unimplemented compare type " << compare->InputAt(0)->GetType();
973 }
974}
975
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100976void LocationsBuilderX86::VisitPhi(HPhi* instruction) {
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100977 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
978 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
979 locations->SetInAt(i, Location::Any());
980 }
981 locations->SetOut(Location::Any());
982 instruction->SetLocations(locations);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100983}
984
985void InstructionCodeGeneratorX86::VisitPhi(HPhi* instruction) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100986 LOG(FATAL) << "Unreachable";
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100987}
988
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100989void LocationsBuilderX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
990 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
991 locations->SetInAt(0, Location::RequiresRegister());
992 Primitive::Type field_type = instruction->InputAt(1)->GetType();
993 if (field_type == Primitive::kPrimBoolean || field_type == Primitive::kPrimByte) {
994 // Ensure the value is in a byte register.
995 locations->SetInAt(1, X86CpuLocation(EAX));
996 } else {
997 locations->SetInAt(1, Location::RequiresRegister());
998 }
999 instruction->SetLocations(locations);
1000}
1001
1002void InstructionCodeGeneratorX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
1003 LocationSummary* locations = instruction->GetLocations();
1004 Register obj = locations->InAt(0).AsX86().AsCpuRegister();
1005 uint32_t offset = instruction->GetFieldOffset().Uint32Value();
1006 Primitive::Type field_type = instruction->InputAt(1)->GetType();
1007
1008 switch (field_type) {
1009 case Primitive::kPrimBoolean:
1010 case Primitive::kPrimByte: {
1011 ByteRegister value = locations->InAt(1).AsX86().AsByteRegister();
1012 __ movb(Address(obj, offset), value);
1013 break;
1014 }
1015
1016 case Primitive::kPrimShort:
1017 case Primitive::kPrimChar: {
1018 Register value = locations->InAt(1).AsX86().AsCpuRegister();
1019 __ movw(Address(obj, offset), value);
1020 break;
1021 }
1022
1023 case Primitive::kPrimInt:
1024 case Primitive::kPrimNot: {
1025 Register value = locations->InAt(1).AsX86().AsCpuRegister();
1026 __ movl(Address(obj, offset), value);
1027 break;
1028 }
1029
1030 case Primitive::kPrimLong: {
1031 X86ManagedRegister value = locations->InAt(1).AsX86();
1032 __ movl(Address(obj, offset), value.AsRegisterPairLow());
1033 __ movl(Address(obj, kX86WordSize + offset), value.AsRegisterPairHigh());
1034 break;
1035 }
1036
1037 case Primitive::kPrimFloat:
1038 case Primitive::kPrimDouble:
1039 LOG(FATAL) << "Unimplemented register type " << field_type;
1040
1041 case Primitive::kPrimVoid:
1042 LOG(FATAL) << "Unreachable type " << field_type;
1043 }
1044}
1045
1046void LocationsBuilderX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
1047 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
1048 locations->SetInAt(0, Location::RequiresRegister());
1049 locations->SetOut(Location::RequiresRegister());
1050 instruction->SetLocations(locations);
1051}
1052
1053void InstructionCodeGeneratorX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
1054 LocationSummary* locations = instruction->GetLocations();
1055 Register obj = locations->InAt(0).AsX86().AsCpuRegister();
1056 uint32_t offset = instruction->GetFieldOffset().Uint32Value();
1057
1058 switch (instruction->GetType()) {
1059 case Primitive::kPrimBoolean: {
1060 Register out = locations->Out().AsX86().AsCpuRegister();
1061 __ movzxb(out, Address(obj, offset));
1062 break;
1063 }
1064
1065 case Primitive::kPrimByte: {
1066 Register out = locations->Out().AsX86().AsCpuRegister();
1067 __ movsxb(out, Address(obj, offset));
1068 break;
1069 }
1070
1071 case Primitive::kPrimShort: {
1072 Register out = locations->Out().AsX86().AsCpuRegister();
1073 __ movsxw(out, Address(obj, offset));
1074 break;
1075 }
1076
1077 case Primitive::kPrimChar: {
1078 Register out = locations->Out().AsX86().AsCpuRegister();
1079 __ movzxw(out, Address(obj, offset));
1080 break;
1081 }
1082
1083 case Primitive::kPrimInt:
1084 case Primitive::kPrimNot: {
1085 Register out = locations->Out().AsX86().AsCpuRegister();
1086 __ movl(out, Address(obj, offset));
1087 break;
1088 }
1089
1090 case Primitive::kPrimLong: {
1091 // TODO: support volatile.
1092 X86ManagedRegister out = locations->Out().AsX86();
1093 __ movl(out.AsRegisterPairLow(), Address(obj, offset));
1094 __ movl(out.AsRegisterPairHigh(), Address(obj, kX86WordSize + offset));
1095 break;
1096 }
1097
1098 case Primitive::kPrimFloat:
1099 case Primitive::kPrimDouble:
1100 LOG(FATAL) << "Unimplemented register type " << instruction->GetType();
1101
1102 case Primitive::kPrimVoid:
1103 LOG(FATAL) << "Unreachable type " << instruction->GetType();
1104 }
1105}
1106
1107void LocationsBuilderX86::VisitNullCheck(HNullCheck* instruction) {
1108 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
1109 locations->SetInAt(0, Location::Any());
1110 // TODO: Have a normalization phase that makes this instruction never used.
1111 locations->SetOut(Location::SameAsFirstInput());
1112 instruction->SetLocations(locations);
1113}
1114
1115void InstructionCodeGeneratorX86::VisitNullCheck(HNullCheck* instruction) {
1116 SlowPathCode* slow_path =
1117 new (GetGraph()->GetArena()) NullCheckSlowPathX86(instruction->GetDexPc());
1118 codegen_->AddSlowPath(slow_path);
1119
1120 LocationSummary* locations = instruction->GetLocations();
1121 Location obj = locations->InAt(0);
1122 DCHECK(obj.Equals(locations->Out()));
1123
1124 if (obj.IsRegister()) {
1125 __ cmpl(obj.AsX86().AsCpuRegister(), Immediate(0));
1126 } else {
1127 DCHECK(locations->InAt(0).IsStackSlot());
1128 __ cmpl(Address(ESP, obj.GetStackIndex()), Immediate(0));
1129 }
1130 __ j(kEqual, slow_path->GetEntryLabel());
1131}
1132
1133void LocationsBuilderX86::VisitTemporary(HTemporary* temp) {
1134 temp->SetLocations(nullptr);
1135}
1136
1137void InstructionCodeGeneratorX86::VisitTemporary(HTemporary* temp) {
1138 // Nothing to do, this is driven by the code generator.
1139}
1140
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01001141void LocationsBuilderX86::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001142 LOG(FATAL) << "Unreachable";
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01001143}
1144
1145void InstructionCodeGeneratorX86::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001146 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
1147}
1148
1149X86Assembler* ParallelMoveResolverX86::GetAssembler() const {
1150 return codegen_->GetAssembler();
1151}
1152
1153void ParallelMoveResolverX86::MoveMemoryToMemory(int dst, int src) {
1154 ScratchRegisterScope ensure_scratch(
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01001155 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001156 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
1157 __ movl(static_cast<Register>(ensure_scratch.GetRegister()), Address(ESP, src + stack_offset));
1158 __ movl(Address(ESP, dst + stack_offset), static_cast<Register>(ensure_scratch.GetRegister()));
1159}
1160
1161void ParallelMoveResolverX86::EmitMove(size_t index) {
1162 MoveOperands* move = moves_.Get(index);
1163 Location source = move->GetSource();
1164 Location destination = move->GetDestination();
1165
1166 if (source.IsRegister()) {
1167 if (destination.IsRegister()) {
1168 __ movl(destination.AsX86().AsCpuRegister(), source.AsX86().AsCpuRegister());
1169 } else {
1170 DCHECK(destination.IsStackSlot());
1171 __ movl(Address(ESP, destination.GetStackIndex()), source.AsX86().AsCpuRegister());
1172 }
1173 } else if (source.IsStackSlot()) {
1174 if (destination.IsRegister()) {
1175 __ movl(destination.AsX86().AsCpuRegister(), Address(ESP, source.GetStackIndex()));
1176 } else {
1177 DCHECK(destination.IsStackSlot());
1178 MoveMemoryToMemory(destination.GetStackIndex(),
1179 source.GetStackIndex());
1180 }
1181 } else {
1182 LOG(FATAL) << "Unimplemented";
1183 }
1184}
1185
1186void ParallelMoveResolverX86::Exchange(Register reg, int mem) {
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01001187 Register suggested_scratch = reg == EAX ? EBX : EAX;
1188 ScratchRegisterScope ensure_scratch(
1189 this, reg, suggested_scratch, codegen_->GetNumberOfCoreRegisters());
1190
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001191 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
1192 __ movl(static_cast<Register>(ensure_scratch.GetRegister()), Address(ESP, mem + stack_offset));
1193 __ movl(Address(ESP, mem + stack_offset), reg);
1194 __ movl(reg, static_cast<Register>(ensure_scratch.GetRegister()));
1195}
1196
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001197void ParallelMoveResolverX86::Exchange(int mem1, int mem2) {
1198 ScratchRegisterScope ensure_scratch1(
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01001199 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
1200
1201 Register suggested_scratch = ensure_scratch1.GetRegister() == EAX ? EBX : EAX;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001202 ScratchRegisterScope ensure_scratch2(
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01001203 this, ensure_scratch1.GetRegister(), suggested_scratch, codegen_->GetNumberOfCoreRegisters());
1204
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001205 int stack_offset = ensure_scratch1.IsSpilled() ? kX86WordSize : 0;
1206 stack_offset += ensure_scratch2.IsSpilled() ? kX86WordSize : 0;
1207 __ movl(static_cast<Register>(ensure_scratch1.GetRegister()), Address(ESP, mem1 + stack_offset));
1208 __ movl(static_cast<Register>(ensure_scratch2.GetRegister()), Address(ESP, mem2 + stack_offset));
1209 __ movl(Address(ESP, mem2 + stack_offset), static_cast<Register>(ensure_scratch1.GetRegister()));
1210 __ movl(Address(ESP, mem1 + stack_offset), static_cast<Register>(ensure_scratch2.GetRegister()));
1211}
1212
1213void ParallelMoveResolverX86::EmitSwap(size_t index) {
1214 MoveOperands* move = moves_.Get(index);
1215 Location source = move->GetSource();
1216 Location destination = move->GetDestination();
1217
1218 if (source.IsRegister() && destination.IsRegister()) {
1219 __ xchgl(destination.AsX86().AsCpuRegister(), source.AsX86().AsCpuRegister());
1220 } else if (source.IsRegister() && destination.IsStackSlot()) {
1221 Exchange(source.AsX86().AsCpuRegister(), destination.GetStackIndex());
1222 } else if (source.IsStackSlot() && destination.IsRegister()) {
1223 Exchange(destination.AsX86().AsCpuRegister(), source.GetStackIndex());
1224 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
1225 Exchange(destination.GetStackIndex(), source.GetStackIndex());
1226 } else {
1227 LOG(FATAL) << "Unimplemented";
1228 }
1229}
1230
1231void ParallelMoveResolverX86::SpillScratch(int reg) {
1232 __ pushl(static_cast<Register>(reg));
1233}
1234
1235void ParallelMoveResolverX86::RestoreScratch(int reg) {
1236 __ popl(static_cast<Register>(reg));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01001237}
1238
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001239} // namespace x86
1240} // namespace art