blob: 91aa26902dc669f5d1902ca55281f8fb6dd3ecb0 [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"
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +010018
19#include "entrypoints/quick/quick_entrypoints.h"
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +010020#include "gc/accounting/card_table.h"
Ian Rogers7e70b002014-10-08 11:47:24 -070021#include "mirror/array-inl.h"
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +010022#include "mirror/art_method.h"
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +010023#include "mirror/class.h"
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +010024#include "thread.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000025#include "utils/assembler.h"
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010026#include "utils/stack_checks.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000027#include "utils/x86/assembler_x86.h"
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +010028#include "utils/x86/managed_register_x86.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000029
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000030namespace art {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +010031
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000032namespace x86 {
33
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010034static constexpr bool kExplicitStackOverflowCheck = false;
35
36static constexpr int kNumberOfPushedRegistersAtEntry = 1;
37static constexpr int kCurrentMethodStackOffset = 0;
38
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +010039static constexpr Register kRuntimeParameterCoreRegisters[] = { EAX, ECX, EDX };
40static constexpr size_t kRuntimeParameterCoreRegistersLength =
41 arraysize(kRuntimeParameterCoreRegisters);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +010042static constexpr XmmRegister kRuntimeParameterFpuRegisters[] = { };
43static constexpr size_t kRuntimeParameterFpuRegistersLength = 0;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +010044
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +010045class InvokeRuntimeCallingConvention : public CallingConvention<Register, XmmRegister> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +010046 public:
47 InvokeRuntimeCallingConvention()
48 : CallingConvention(kRuntimeParameterCoreRegisters,
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +010049 kRuntimeParameterCoreRegistersLength,
50 kRuntimeParameterFpuRegisters,
51 kRuntimeParameterFpuRegistersLength) {}
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +010052
53 private:
54 DISALLOW_COPY_AND_ASSIGN(InvokeRuntimeCallingConvention);
55};
56
Nicolas Geoffraye5038322014-07-04 09:41:32 +010057#define __ reinterpret_cast<X86Assembler*>(codegen->GetAssembler())->
58
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +010059class SlowPathCodeX86 : public SlowPathCode {
60 public:
61 SlowPathCodeX86() : entry_label_(), exit_label_() {}
62
63 Label* GetEntryLabel() { return &entry_label_; }
64 Label* GetExitLabel() { return &exit_label_; }
65
66 private:
67 Label entry_label_;
68 Label exit_label_;
69
70 DISALLOW_COPY_AND_ASSIGN(SlowPathCodeX86);
71};
72
73class NullCheckSlowPathX86 : public SlowPathCodeX86 {
Nicolas Geoffraye5038322014-07-04 09:41:32 +010074 public:
Nicolas Geoffray39468442014-09-02 15:17:15 +010075 explicit NullCheckSlowPathX86(HNullCheck* instruction) : instruction_(instruction) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +010076
77 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
78 __ Bind(GetEntryLabel());
79 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pThrowNullPointer)));
Nicolas Geoffray39468442014-09-02 15:17:15 +010080 codegen->RecordPcInfo(instruction_, instruction_->GetDexPc());
Nicolas Geoffraye5038322014-07-04 09:41:32 +010081 }
82
83 private:
Nicolas Geoffray39468442014-09-02 15:17:15 +010084 HNullCheck* const instruction_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +010085 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathX86);
86};
87
Calin Juravled0d48522014-11-04 16:40:20 +000088class DivZeroCheckSlowPathX86 : public SlowPathCodeX86 {
89 public:
90 explicit DivZeroCheckSlowPathX86(HDivZeroCheck* instruction) : instruction_(instruction) {}
91
92 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
93 __ Bind(GetEntryLabel());
94 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pThrowDivZero)));
95 codegen->RecordPcInfo(instruction_, instruction_->GetDexPc());
96 }
97
98 private:
99 HDivZeroCheck* const instruction_;
100 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathX86);
101};
102
103class DivMinusOneSlowPathX86 : public SlowPathCodeX86 {
104 public:
105 explicit DivMinusOneSlowPathX86(Register reg) : reg_(reg) {}
106
107 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
108 __ Bind(GetEntryLabel());
109 __ negl(reg_);
110 __ jmp(GetExitLabel());
111 }
112
113 private:
114 Register reg_;
115 DISALLOW_COPY_AND_ASSIGN(DivMinusOneSlowPathX86);
116};
117
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100118class StackOverflowCheckSlowPathX86 : public SlowPathCodeX86 {
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100119 public:
120 StackOverflowCheckSlowPathX86() {}
121
122 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
123 __ Bind(GetEntryLabel());
124 __ addl(ESP,
125 Immediate(codegen->GetFrameSize() - kNumberOfPushedRegistersAtEntry * kX86WordSize));
126 __ fs()->jmp(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pThrowStackOverflow)));
127 }
128
129 private:
130 DISALLOW_COPY_AND_ASSIGN(StackOverflowCheckSlowPathX86);
131};
132
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100133class BoundsCheckSlowPathX86 : public SlowPathCodeX86 {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100134 public:
Roland Levillain5799fc02014-09-25 12:15:20 +0100135 BoundsCheckSlowPathX86(HBoundsCheck* instruction,
136 Location index_location,
137 Location length_location)
Nicolas Geoffray39468442014-09-02 15:17:15 +0100138 : instruction_(instruction), index_location_(index_location), length_location_(length_location) {}
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100139
140 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100141 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100142 __ Bind(GetEntryLabel());
143 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100144 x86_codegen->Move32(Location::RegisterLocation(calling_convention.GetRegisterAt(0)), index_location_);
145 x86_codegen->Move32(Location::RegisterLocation(calling_convention.GetRegisterAt(1)), length_location_);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100146 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pThrowArrayBounds)));
Nicolas Geoffray39468442014-09-02 15:17:15 +0100147 codegen->RecordPcInfo(instruction_, instruction_->GetDexPc());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100148 }
149
150 private:
Nicolas Geoffray39468442014-09-02 15:17:15 +0100151 HBoundsCheck* const instruction_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100152 const Location index_location_;
153 const Location length_location_;
154
155 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathX86);
156};
157
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100158class SuspendCheckSlowPathX86 : public SlowPathCodeX86 {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000159 public:
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100160 explicit SuspendCheckSlowPathX86(HSuspendCheck* instruction, HBasicBlock* successor)
161 : instruction_(instruction), successor_(successor) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000162
163 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100164 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000165 __ Bind(GetEntryLabel());
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100166 codegen->SaveLiveRegisters(instruction_->GetLocations());
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000167 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pTestSuspend)));
168 codegen->RecordPcInfo(instruction_, instruction_->GetDexPc());
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100169 codegen->RestoreLiveRegisters(instruction_->GetLocations());
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100170 if (successor_ == nullptr) {
171 __ jmp(GetReturnLabel());
172 } else {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100173 __ jmp(x86_codegen->GetLabelOf(successor_));
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100174 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000175 }
176
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100177 Label* GetReturnLabel() {
178 DCHECK(successor_ == nullptr);
179 return &return_label_;
180 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000181
182 private:
183 HSuspendCheck* const instruction_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100184 HBasicBlock* const successor_;
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000185 Label return_label_;
186
187 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathX86);
188};
189
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000190class LoadStringSlowPathX86 : public SlowPathCodeX86 {
191 public:
192 explicit LoadStringSlowPathX86(HLoadString* instruction) : instruction_(instruction) {}
193
194 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
195 LocationSummary* locations = instruction_->GetLocations();
196 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
197
198 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
199 __ Bind(GetEntryLabel());
200 codegen->SaveLiveRegisters(locations);
201
202 InvokeRuntimeCallingConvention calling_convention;
203 x86_codegen->LoadCurrentMethod(calling_convention.GetRegisterAt(0));
204 __ movl(calling_convention.GetRegisterAt(1), Immediate(instruction_->GetStringIndex()));
205 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pResolveString)));
206 codegen->RecordPcInfo(instruction_, instruction_->GetDexPc());
207 x86_codegen->Move32(locations->Out(), Location::RegisterLocation(EAX));
208 codegen->RestoreLiveRegisters(locations);
209
210 __ jmp(GetExitLabel());
211 }
212
213 private:
214 HLoadString* const instruction_;
215
216 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathX86);
217};
218
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000219class LoadClassSlowPathX86 : public SlowPathCodeX86 {
220 public:
221 LoadClassSlowPathX86(HLoadClass* cls,
222 HInstruction* at,
223 uint32_t dex_pc,
224 bool do_clinit)
225 : cls_(cls), at_(at), dex_pc_(dex_pc), do_clinit_(do_clinit) {
226 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
227 }
228
229 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
230 LocationSummary* locations = at_->GetLocations();
231 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
232 __ Bind(GetEntryLabel());
233 codegen->SaveLiveRegisters(locations);
234
235 InvokeRuntimeCallingConvention calling_convention;
236 __ movl(calling_convention.GetRegisterAt(0), Immediate(cls_->GetTypeIndex()));
237 x86_codegen->LoadCurrentMethod(calling_convention.GetRegisterAt(1));
238 __ fs()->call(Address::Absolute(do_clinit_
239 ? QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pInitializeStaticStorage)
240 : QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pInitializeType)));
241 codegen->RecordPcInfo(at_, dex_pc_);
242
243 // Move the class to the desired location.
244 if (locations->Out().IsValid()) {
245 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
246 x86_codegen->Move32(locations->Out(), Location::RegisterLocation(EAX));
247 }
248 codegen->RestoreLiveRegisters(locations);
249 __ jmp(GetExitLabel());
250 }
251
252 private:
253 // The class this slow path will load.
254 HLoadClass* const cls_;
255
256 // The instruction where this slow path is happening.
257 // (Might be the load class or an initialization check).
258 HInstruction* const at_;
259
260 // The dex PC of `at_`.
261 const uint32_t dex_pc_;
262
263 // Whether to initialize the class.
264 const bool do_clinit_;
265
266 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathX86);
267};
268
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100269#undef __
270#define __ reinterpret_cast<X86Assembler*>(GetAssembler())->
271
Dave Allison20dfc792014-06-16 20:44:29 -0700272inline Condition X86Condition(IfCondition cond) {
273 switch (cond) {
274 case kCondEQ: return kEqual;
275 case kCondNE: return kNotEqual;
276 case kCondLT: return kLess;
277 case kCondLE: return kLessEqual;
278 case kCondGT: return kGreater;
279 case kCondGE: return kGreaterEqual;
280 default:
281 LOG(FATAL) << "Unknown if condition";
282 }
283 return kEqual;
284}
285
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100286void CodeGeneratorX86::DumpCoreRegister(std::ostream& stream, int reg) const {
287 stream << X86ManagedRegister::FromCpuRegister(Register(reg));
288}
289
290void CodeGeneratorX86::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
291 stream << X86ManagedRegister::FromXmmRegister(XmmRegister(reg));
292}
293
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100294size_t CodeGeneratorX86::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
295 __ movl(Address(ESP, stack_index), static_cast<Register>(reg_id));
296 return kX86WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100297}
298
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100299size_t CodeGeneratorX86::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
300 __ movl(static_cast<Register>(reg_id), Address(ESP, stack_index));
301 return kX86WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100302}
303
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100304CodeGeneratorX86::CodeGeneratorX86(HGraph* graph)
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100305 : CodeGenerator(graph, kNumberOfCpuRegisters, kNumberOfXmmRegisters, kNumberOfRegisterPairs),
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100306 block_labels_(graph->GetArena(), 0),
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100307 location_builder_(graph, this),
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100308 instruction_visitor_(graph, this),
309 move_resolver_(graph->GetArena(), this) {}
310
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100311size_t CodeGeneratorX86::FrameEntrySpillSize() const {
312 return kNumberOfPushedRegistersAtEntry * kX86WordSize;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100313}
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100314
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100315Location CodeGeneratorX86::AllocateFreeRegister(Primitive::Type type) const {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100316 switch (type) {
317 case Primitive::kPrimLong: {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100318 size_t reg = FindFreeEntry(blocked_register_pairs_, kNumberOfRegisterPairs);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100319 X86ManagedRegister pair =
320 X86ManagedRegister::FromRegisterPair(static_cast<RegisterPair>(reg));
Calin Juravle34bacdf2014-10-07 20:23:36 +0100321 DCHECK(!blocked_core_registers_[pair.AsRegisterPairLow()]);
322 DCHECK(!blocked_core_registers_[pair.AsRegisterPairHigh()]);
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100323 blocked_core_registers_[pair.AsRegisterPairLow()] = true;
324 blocked_core_registers_[pair.AsRegisterPairHigh()] = true;
Calin Juravle34bacdf2014-10-07 20:23:36 +0100325 UpdateBlockedPairRegisters();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100326 return Location::RegisterPairLocation(pair.AsRegisterPairLow(), pair.AsRegisterPairHigh());
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100327 }
328
329 case Primitive::kPrimByte:
330 case Primitive::kPrimBoolean:
331 case Primitive::kPrimChar:
332 case Primitive::kPrimShort:
333 case Primitive::kPrimInt:
334 case Primitive::kPrimNot: {
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100335 Register reg = static_cast<Register>(
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100336 FindFreeEntry(blocked_core_registers_, kNumberOfCpuRegisters));
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100337 // Block all register pairs that contain `reg`.
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100338 for (int i = 0; i < kNumberOfRegisterPairs; i++) {
339 X86ManagedRegister current =
340 X86ManagedRegister::FromRegisterPair(static_cast<RegisterPair>(i));
341 if (current.AsRegisterPairLow() == reg || current.AsRegisterPairHigh() == reg) {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100342 blocked_register_pairs_[i] = true;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100343 }
344 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100345 return Location::RegisterLocation(reg);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100346 }
347
348 case Primitive::kPrimFloat:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100349 case Primitive::kPrimDouble: {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100350 return Location::FpuRegisterLocation(
351 FindFreeEntry(blocked_fpu_registers_, kNumberOfXmmRegisters));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100352 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100353
354 case Primitive::kPrimVoid:
355 LOG(FATAL) << "Unreachable type " << type;
356 }
357
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100358 return Location();
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100359}
360
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100361void CodeGeneratorX86::SetupBlockedRegisters() const {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100362 // Don't allocate the dalvik style register pair passing.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100363 blocked_register_pairs_[ECX_EDX] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100364
365 // Stack register is always reserved.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100366 blocked_core_registers_[ESP] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100367
368 // TODO: We currently don't use Quick's callee saved registers.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100369 blocked_core_registers_[EBP] = true;
370 blocked_core_registers_[ESI] = true;
371 blocked_core_registers_[EDI] = true;
Calin Juravle34bacdf2014-10-07 20:23:36 +0100372
373 UpdateBlockedPairRegisters();
374}
375
376void CodeGeneratorX86::UpdateBlockedPairRegisters() const {
377 for (int i = 0; i < kNumberOfRegisterPairs; i++) {
378 X86ManagedRegister current =
379 X86ManagedRegister::FromRegisterPair(static_cast<RegisterPair>(i));
380 if (blocked_core_registers_[current.AsRegisterPairLow()]
381 || blocked_core_registers_[current.AsRegisterPairHigh()]) {
382 blocked_register_pairs_[i] = true;
383 }
384 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100385}
386
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100387InstructionCodeGeneratorX86::InstructionCodeGeneratorX86(HGraph* graph, CodeGeneratorX86* codegen)
388 : HGraphVisitor(graph),
389 assembler_(codegen->GetAssembler()),
390 codegen_(codegen) {}
391
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000392void CodeGeneratorX86::GenerateFrameEntry() {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000393 // Create a fake register to mimic Quick.
394 static const int kFakeReturnRegister = 8;
395 core_spill_mask_ |= (1 << kFakeReturnRegister);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000396
Dave Allison648d7112014-07-25 16:15:27 -0700397 bool skip_overflow_check = IsLeafMethod() && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kX86);
Nicolas Geoffray397f2e42014-07-23 12:57:19 +0100398 if (!skip_overflow_check && !kExplicitStackOverflowCheck) {
399 __ testl(EAX, Address(ESP, -static_cast<int32_t>(GetStackOverflowReservedBytes(kX86))));
Nicolas Geoffray39468442014-09-02 15:17:15 +0100400 RecordPcInfo(nullptr, 0);
Nicolas Geoffray397f2e42014-07-23 12:57:19 +0100401 }
402
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100403 // The return PC has already been pushed on the stack.
Nicolas Geoffray707c8092014-04-04 10:50:14 +0100404 __ subl(ESP, Immediate(GetFrameSize() - kNumberOfPushedRegistersAtEntry * kX86WordSize));
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100405
Nicolas Geoffray397f2e42014-07-23 12:57:19 +0100406 if (!skip_overflow_check && kExplicitStackOverflowCheck) {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100407 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) StackOverflowCheckSlowPathX86();
Nicolas Geoffray397f2e42014-07-23 12:57:19 +0100408 AddSlowPath(slow_path);
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100409
Nicolas Geoffray397f2e42014-07-23 12:57:19 +0100410 __ fs()->cmpl(ESP, Address::Absolute(Thread::StackEndOffset<kX86WordSize>()));
411 __ j(kLess, slow_path->GetEntryLabel());
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100412 }
413
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100414 __ movl(Address(ESP, kCurrentMethodStackOffset), EAX);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000415}
416
417void CodeGeneratorX86::GenerateFrameExit() {
Nicolas Geoffray707c8092014-04-04 10:50:14 +0100418 __ addl(ESP, Immediate(GetFrameSize() - kNumberOfPushedRegistersAtEntry * kX86WordSize));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000419}
420
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100421void CodeGeneratorX86::Bind(HBasicBlock* block) {
422 __ Bind(GetLabelOf(block));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000423}
424
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100425void CodeGeneratorX86::LoadCurrentMethod(Register reg) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100426 __ movl(reg, Address(ESP, kCurrentMethodStackOffset));
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000427}
428
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100429Location CodeGeneratorX86::GetStackLocation(HLoadLocal* load) const {
430 switch (load->GetType()) {
431 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100432 case Primitive::kPrimDouble:
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100433 return Location::DoubleStackSlot(GetStackSlot(load->GetLocal()));
434 break;
435
436 case Primitive::kPrimInt:
437 case Primitive::kPrimNot:
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100438 case Primitive::kPrimFloat:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100439 return Location::StackSlot(GetStackSlot(load->GetLocal()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100440
441 case Primitive::kPrimBoolean:
442 case Primitive::kPrimByte:
443 case Primitive::kPrimChar:
444 case Primitive::kPrimShort:
445 case Primitive::kPrimVoid:
446 LOG(FATAL) << "Unexpected type " << load->GetType();
447 }
448
449 LOG(FATAL) << "Unreachable";
450 return Location();
451}
452
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100453Location InvokeDexCallingConventionVisitor::GetNextLocation(Primitive::Type type) {
454 switch (type) {
455 case Primitive::kPrimBoolean:
456 case Primitive::kPrimByte:
457 case Primitive::kPrimChar:
458 case Primitive::kPrimShort:
459 case Primitive::kPrimInt:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100460 case Primitive::kPrimFloat:
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100461 case Primitive::kPrimNot: {
462 uint32_t index = gp_index_++;
463 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100464 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100465 } else {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100466 return Location::StackSlot(calling_convention.GetStackOffsetOf(index));
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100467 }
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100468 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100469
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100470 case Primitive::kPrimLong:
471 case Primitive::kPrimDouble: {
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100472 uint32_t index = gp_index_;
473 gp_index_ += 2;
474 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100475 X86ManagedRegister pair = X86ManagedRegister::FromRegisterPair(
476 calling_convention.GetRegisterPairAt(index));
477 return Location::RegisterPairLocation(pair.AsRegisterPairLow(), pair.AsRegisterPairHigh());
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100478 } else if (index + 1 == calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray0a6c4592014-10-30 16:37:57 +0000479 // On X86, the register index and stack index of a quick parameter is the same, since
480 // we are passing floating pointer values in core registers.
481 return Location::QuickParameter(index, index);
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100482 } else {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100483 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(index));
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100484 }
485 }
486
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100487 case Primitive::kPrimVoid:
488 LOG(FATAL) << "Unexpected parameter type " << type;
489 break;
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100490 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100491 return Location();
492}
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100493
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100494void CodeGeneratorX86::Move32(Location destination, Location source) {
495 if (source.Equals(destination)) {
496 return;
497 }
498 if (destination.IsRegister()) {
499 if (source.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100500 __ movl(destination.As<Register>(), source.As<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100501 } else if (source.IsFpuRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100502 __ movd(destination.As<Register>(), source.As<XmmRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100503 } else {
504 DCHECK(source.IsStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100505 __ movl(destination.As<Register>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100506 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100507 } else if (destination.IsFpuRegister()) {
508 if (source.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100509 __ movd(destination.As<XmmRegister>(), source.As<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100510 } else if (source.IsFpuRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100511 __ movaps(destination.As<XmmRegister>(), source.As<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100512 } else {
513 DCHECK(source.IsStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100514 __ movss(destination.As<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100515 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100516 } else {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100517 DCHECK(destination.IsStackSlot());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100518 if (source.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100519 __ movl(Address(ESP, destination.GetStackIndex()), source.As<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100520 } else if (source.IsFpuRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100521 __ movss(Address(ESP, destination.GetStackIndex()), source.As<XmmRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100522 } else {
523 DCHECK(source.IsStackSlot());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100524 __ pushl(Address(ESP, source.GetStackIndex()));
525 __ popl(Address(ESP, destination.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100526 }
527 }
528}
529
530void CodeGeneratorX86::Move64(Location destination, Location source) {
531 if (source.Equals(destination)) {
532 return;
533 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100534 if (destination.IsRegisterPair()) {
535 if (source.IsRegisterPair()) {
536 __ movl(destination.AsRegisterPairLow<Register>(), source.AsRegisterPairLow<Register>());
537 __ movl(destination.AsRegisterPairHigh<Register>(), source.AsRegisterPairHigh<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100538 } else if (source.IsFpuRegister()) {
539 LOG(FATAL) << "Unimplemented";
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100540 } else if (source.IsQuickParameter()) {
Nicolas Geoffray0a6c4592014-10-30 16:37:57 +0000541 uint16_t register_index = source.GetQuickParameterRegisterIndex();
542 uint16_t stack_index = source.GetQuickParameterStackIndex();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100543 InvokeDexCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100544 __ movl(destination.AsRegisterPairLow<Register>(),
Nicolas Geoffray0a6c4592014-10-30 16:37:57 +0000545 calling_convention.GetRegisterAt(register_index));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100546 __ movl(destination.AsRegisterPairHigh<Register>(), Address(ESP,
Nicolas Geoffray0a6c4592014-10-30 16:37:57 +0000547 calling_convention.GetStackOffsetOf(stack_index + 1) + GetFrameSize()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100548 } else {
549 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100550 __ movl(destination.AsRegisterPairLow<Register>(), Address(ESP, source.GetStackIndex()));
551 __ movl(destination.AsRegisterPairHigh<Register>(),
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100552 Address(ESP, source.GetHighStackIndex(kX86WordSize)));
553 }
554 } else if (destination.IsQuickParameter()) {
555 InvokeDexCallingConvention calling_convention;
Nicolas Geoffray0a6c4592014-10-30 16:37:57 +0000556 uint16_t register_index = destination.GetQuickParameterRegisterIndex();
557 uint16_t stack_index = destination.GetQuickParameterStackIndex();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100558 if (source.IsRegister()) {
Nicolas Geoffray0a6c4592014-10-30 16:37:57 +0000559 __ movl(calling_convention.GetRegisterAt(register_index), source.AsRegisterPairLow<Register>());
560 __ movl(Address(ESP, calling_convention.GetStackOffsetOf(stack_index + 1)),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100561 source.AsRegisterPairHigh<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100562 } else if (source.IsFpuRegister()) {
563 LOG(FATAL) << "Unimplemented";
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100564 } else {
565 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray0a6c4592014-10-30 16:37:57 +0000566 __ movl(calling_convention.GetRegisterAt(register_index),
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100567 Address(ESP, source.GetStackIndex()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100568 __ pushl(Address(ESP, source.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray0a6c4592014-10-30 16:37:57 +0000569 __ popl(Address(ESP, calling_convention.GetStackOffsetOf(stack_index + 1)));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100570 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100571 } else if (destination.IsFpuRegister()) {
572 if (source.IsDoubleStackSlot()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100573 __ movsd(destination.As<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100574 } else {
575 LOG(FATAL) << "Unimplemented";
576 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100577 } else {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100578 DCHECK(destination.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100579 if (source.IsRegisterPair()) {
580 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegisterPairLow<Register>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100581 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100582 source.AsRegisterPairHigh<Register>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100583 } else if (source.IsQuickParameter()) {
584 InvokeDexCallingConvention calling_convention;
Nicolas Geoffray0a6c4592014-10-30 16:37:57 +0000585 uint16_t register_index = source.GetQuickParameterRegisterIndex();
586 uint16_t stack_index = source.GetQuickParameterStackIndex();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100587 __ movl(Address(ESP, destination.GetStackIndex()),
Nicolas Geoffray0a6c4592014-10-30 16:37:57 +0000588 calling_convention.GetRegisterAt(register_index));
589 DCHECK_EQ(calling_convention.GetStackOffsetOf(stack_index + 1) + GetFrameSize(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100590 static_cast<size_t>(destination.GetHighStackIndex(kX86WordSize)));
591 } else if (source.IsFpuRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100592 __ movsd(Address(ESP, destination.GetStackIndex()), source.As<XmmRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100593 } else {
594 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100595 __ pushl(Address(ESP, source.GetStackIndex()));
596 __ popl(Address(ESP, destination.GetStackIndex()));
597 __ pushl(Address(ESP, source.GetHighStackIndex(kX86WordSize)));
598 __ popl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100599 }
600 }
601}
602
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100603void CodeGeneratorX86::Move(HInstruction* instruction, Location location, HInstruction* move_for) {
Roland Levillain476df552014-10-09 17:51:36 +0100604 if (instruction->IsIntConstant()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100605 Immediate imm(instruction->AsIntConstant()->GetValue());
606 if (location.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100607 __ movl(location.As<Register>(), imm);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +0100608 } else if (location.IsStackSlot()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100609 __ movl(Address(ESP, location.GetStackIndex()), imm);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +0100610 } else {
611 DCHECK(location.IsConstant());
612 DCHECK_EQ(location.GetConstant(), instruction);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100613 }
Roland Levillain476df552014-10-09 17:51:36 +0100614 } else if (instruction->IsLongConstant()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100615 int64_t value = instruction->AsLongConstant()->GetValue();
616 if (location.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100617 __ movl(location.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
618 __ movl(location.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +0100619 } else if (location.IsDoubleStackSlot()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100620 __ movl(Address(ESP, location.GetStackIndex()), Immediate(Low32Bits(value)));
621 __ movl(Address(ESP, location.GetHighStackIndex(kX86WordSize)), Immediate(High32Bits(value)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +0100622 } else {
623 DCHECK(location.IsConstant());
624 DCHECK_EQ(location.GetConstant(), instruction);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100625 }
Roland Levillain476df552014-10-09 17:51:36 +0100626 } else if (instruction->IsLoadLocal()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100627 int slot = GetStackSlot(instruction->AsLoadLocal()->GetLocal());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100628 switch (instruction->GetType()) {
629 case Primitive::kPrimBoolean:
630 case Primitive::kPrimByte:
631 case Primitive::kPrimChar:
632 case Primitive::kPrimShort:
633 case Primitive::kPrimInt:
634 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100635 case Primitive::kPrimFloat:
636 Move32(location, Location::StackSlot(slot));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100637 break;
638
639 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100640 case Primitive::kPrimDouble:
641 Move64(location, Location::DoubleStackSlot(slot));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100642 break;
643
644 default:
645 LOG(FATAL) << "Unimplemented local type " << instruction->GetType();
646 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000647 } else {
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100648 DCHECK((instruction->GetNext() == move_for) || instruction->GetNext()->IsTemporary());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100649 switch (instruction->GetType()) {
650 case Primitive::kPrimBoolean:
651 case Primitive::kPrimByte:
652 case Primitive::kPrimChar:
653 case Primitive::kPrimShort:
654 case Primitive::kPrimInt:
655 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100656 case Primitive::kPrimFloat:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100657 Move32(location, instruction->GetLocations()->Out());
658 break;
659
660 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100661 case Primitive::kPrimDouble:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100662 Move64(location, instruction->GetLocations()->Out());
663 break;
664
665 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100666 LOG(FATAL) << "Unexpected type " << instruction->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100667 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000668 }
669}
670
671void LocationsBuilderX86::VisitGoto(HGoto* got) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000672 got->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000673}
674
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000675void InstructionCodeGeneratorX86::VisitGoto(HGoto* got) {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000676 HBasicBlock* successor = got->GetSuccessor();
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100677 DCHECK(!successor->IsExitBlock());
678
679 HBasicBlock* block = got->GetBlock();
680 HInstruction* previous = got->GetPrevious();
681
682 HLoopInformation* info = block->GetLoopInformation();
683 if (info != nullptr && info->IsBackEdge(block) && info->HasSuspendCheck()) {
684 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
685 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
686 return;
687 }
688
689 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
690 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
691 }
692 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000693 __ jmp(codegen_->GetLabelOf(successor));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000694 }
695}
696
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000697void LocationsBuilderX86::VisitExit(HExit* exit) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000698 exit->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000699}
700
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000701void InstructionCodeGeneratorX86::VisitExit(HExit* exit) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700702 UNUSED(exit);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000703 if (kIsDebugBuild) {
704 __ Comment("Unreachable");
705 __ int3();
706 }
707}
708
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000709void LocationsBuilderX86::VisitIf(HIf* if_instr) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100710 LocationSummary* locations =
711 new (GetGraph()->GetArena()) LocationSummary(if_instr, LocationSummary::kNoCall);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100712 HInstruction* cond = if_instr->InputAt(0);
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100713 if (!cond->IsCondition() || cond->AsCondition()->NeedsMaterialization()) {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +0100714 locations->SetInAt(0, Location::Any());
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100715 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000716}
717
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000718void InstructionCodeGeneratorX86::VisitIf(HIf* if_instr) {
Dave Allison20dfc792014-06-16 20:44:29 -0700719 HInstruction* cond = if_instr->InputAt(0);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100720 if (cond->IsIntConstant()) {
721 // Constant condition, statically compared against 1.
722 int32_t cond_value = cond->AsIntConstant()->GetValue();
723 if (cond_value == 1) {
724 if (!codegen_->GoesToNextBlock(if_instr->GetBlock(),
725 if_instr->IfTrueSuccessor())) {
726 __ jmp(codegen_->GetLabelOf(if_instr->IfTrueSuccessor()));
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100727 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100728 return;
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100729 } else {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100730 DCHECK_EQ(cond_value, 0);
731 }
732 } else {
733 bool materialized =
734 !cond->IsCondition() || cond->AsCondition()->NeedsMaterialization();
735 // Moves do not affect the eflags register, so if the condition is
736 // evaluated just before the if, we don't need to evaluate it
737 // again.
738 bool eflags_set = cond->IsCondition()
739 && cond->AsCondition()->IsBeforeWhenDisregardMoves(if_instr);
740 if (materialized) {
741 if (!eflags_set) {
742 // Materialized condition, compare against 0.
743 Location lhs = if_instr->GetLocations()->InAt(0);
744 if (lhs.IsRegister()) {
745 __ cmpl(lhs.As<Register>(), Immediate(0));
746 } else {
747 __ cmpl(Address(ESP, lhs.GetStackIndex()), Immediate(0));
748 }
749 __ j(kNotEqual, codegen_->GetLabelOf(if_instr->IfTrueSuccessor()));
750 } else {
751 __ j(X86Condition(cond->AsCondition()->GetCondition()),
752 codegen_->GetLabelOf(if_instr->IfTrueSuccessor()));
753 }
754 } else {
755 Location lhs = cond->GetLocations()->InAt(0);
756 Location rhs = cond->GetLocations()->InAt(1);
757 // LHS is guaranteed to be in a register (see
758 // LocationsBuilderX86::VisitCondition).
759 if (rhs.IsRegister()) {
760 __ cmpl(lhs.As<Register>(), rhs.As<Register>());
761 } else if (rhs.IsConstant()) {
762 HIntConstant* instruction = rhs.GetConstant()->AsIntConstant();
763 Immediate imm(instruction->AsIntConstant()->GetValue());
764 __ cmpl(lhs.As<Register>(), imm);
765 } else {
766 __ cmpl(lhs.As<Register>(), Address(ESP, rhs.GetStackIndex()));
767 }
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100768 __ j(X86Condition(cond->AsCondition()->GetCondition()),
769 codegen_->GetLabelOf(if_instr->IfTrueSuccessor()));
Dave Allison20dfc792014-06-16 20:44:29 -0700770 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100771 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100772 if (!codegen_->GoesToNextBlock(if_instr->GetBlock(),
773 if_instr->IfFalseSuccessor())) {
Dave Allison20dfc792014-06-16 20:44:29 -0700774 __ jmp(codegen_->GetLabelOf(if_instr->IfFalseSuccessor()));
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000775 }
776}
777
778void LocationsBuilderX86::VisitLocal(HLocal* local) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000779 local->SetLocations(nullptr);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000780}
781
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000782void InstructionCodeGeneratorX86::VisitLocal(HLocal* local) {
783 DCHECK_EQ(local->GetBlock(), GetGraph()->GetEntryBlock());
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000784}
785
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000786void LocationsBuilderX86::VisitLoadLocal(HLoadLocal* local) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100787 local->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000788}
789
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000790void InstructionCodeGeneratorX86::VisitLoadLocal(HLoadLocal* load) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100791 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700792 UNUSED(load);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000793}
794
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100795void LocationsBuilderX86::VisitStoreLocal(HStoreLocal* store) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100796 LocationSummary* locations =
797 new (GetGraph()->GetArena()) LocationSummary(store, LocationSummary::kNoCall);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100798 switch (store->InputAt(1)->GetType()) {
799 case Primitive::kPrimBoolean:
800 case Primitive::kPrimByte:
801 case Primitive::kPrimChar:
802 case Primitive::kPrimShort:
803 case Primitive::kPrimInt:
804 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100805 case Primitive::kPrimFloat:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100806 locations->SetInAt(1, Location::StackSlot(codegen_->GetStackSlot(store->GetLocal())));
807 break;
808
809 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100810 case Primitive::kPrimDouble:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100811 locations->SetInAt(1, Location::DoubleStackSlot(codegen_->GetStackSlot(store->GetLocal())));
812 break;
813
814 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100815 LOG(FATAL) << "Unknown local type " << store->InputAt(1)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100816 }
817 store->SetLocations(locations);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000818}
819
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000820void InstructionCodeGeneratorX86::VisitStoreLocal(HStoreLocal* store) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700821 UNUSED(store);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000822}
823
Dave Allison20dfc792014-06-16 20:44:29 -0700824void LocationsBuilderX86::VisitCondition(HCondition* comp) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100825 LocationSummary* locations =
826 new (GetGraph()->GetArena()) LocationSummary(comp, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +0100827 locations->SetInAt(0, Location::RequiresRegister());
828 locations->SetInAt(1, Location::Any());
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100829 if (comp->NeedsMaterialization()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100830 locations->SetOut(Location::RequiresRegister());
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100831 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000832}
833
Dave Allison20dfc792014-06-16 20:44:29 -0700834void InstructionCodeGeneratorX86::VisitCondition(HCondition* comp) {
835 if (comp->NeedsMaterialization()) {
836 LocationSummary* locations = comp->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100837 Register reg = locations->Out().As<Register>();
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100838 // Clear register: setcc only sets the low byte.
839 __ xorl(reg, reg);
Dave Allison20dfc792014-06-16 20:44:29 -0700840 if (locations->InAt(1).IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100841 __ cmpl(locations->InAt(0).As<Register>(),
842 locations->InAt(1).As<Register>());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100843 } else if (locations->InAt(1).IsConstant()) {
844 HConstant* instruction = locations->InAt(1).GetConstant();
845 Immediate imm(instruction->AsIntConstant()->GetValue());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100846 __ cmpl(locations->InAt(0).As<Register>(), imm);
Dave Allison20dfc792014-06-16 20:44:29 -0700847 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100848 __ cmpl(locations->InAt(0).As<Register>(),
Dave Allison20dfc792014-06-16 20:44:29 -0700849 Address(ESP, locations->InAt(1).GetStackIndex()));
850 }
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100851 __ setb(X86Condition(comp->GetCondition()), reg);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100852 }
Dave Allison20dfc792014-06-16 20:44:29 -0700853}
854
855void LocationsBuilderX86::VisitEqual(HEqual* comp) {
856 VisitCondition(comp);
857}
858
859void InstructionCodeGeneratorX86::VisitEqual(HEqual* comp) {
860 VisitCondition(comp);
861}
862
863void LocationsBuilderX86::VisitNotEqual(HNotEqual* comp) {
864 VisitCondition(comp);
865}
866
867void InstructionCodeGeneratorX86::VisitNotEqual(HNotEqual* comp) {
868 VisitCondition(comp);
869}
870
871void LocationsBuilderX86::VisitLessThan(HLessThan* comp) {
872 VisitCondition(comp);
873}
874
875void InstructionCodeGeneratorX86::VisitLessThan(HLessThan* comp) {
876 VisitCondition(comp);
877}
878
879void LocationsBuilderX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
880 VisitCondition(comp);
881}
882
883void InstructionCodeGeneratorX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
884 VisitCondition(comp);
885}
886
887void LocationsBuilderX86::VisitGreaterThan(HGreaterThan* comp) {
888 VisitCondition(comp);
889}
890
891void InstructionCodeGeneratorX86::VisitGreaterThan(HGreaterThan* comp) {
892 VisitCondition(comp);
893}
894
895void LocationsBuilderX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
896 VisitCondition(comp);
897}
898
899void InstructionCodeGeneratorX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
900 VisitCondition(comp);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000901}
902
903void LocationsBuilderX86::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100904 LocationSummary* locations =
905 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100906 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000907}
908
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000909void InstructionCodeGeneratorX86::VisitIntConstant(HIntConstant* constant) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100910 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700911 UNUSED(constant);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000912}
913
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100914void LocationsBuilderX86::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100915 LocationSummary* locations =
916 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100917 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100918}
919
920void InstructionCodeGeneratorX86::VisitLongConstant(HLongConstant* constant) {
921 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700922 UNUSED(constant);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100923}
924
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100925void LocationsBuilderX86::VisitFloatConstant(HFloatConstant* constant) {
926 LocationSummary* locations =
927 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
928 locations->SetOut(Location::ConstantLocation(constant));
929}
930
931void InstructionCodeGeneratorX86::VisitFloatConstant(HFloatConstant* constant) {
932 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700933 UNUSED(constant);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100934}
935
936void LocationsBuilderX86::VisitDoubleConstant(HDoubleConstant* constant) {
937 LocationSummary* locations =
938 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
939 locations->SetOut(Location::ConstantLocation(constant));
940}
941
942void InstructionCodeGeneratorX86::VisitDoubleConstant(HDoubleConstant* constant) {
943 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700944 UNUSED(constant);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100945}
946
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000947void LocationsBuilderX86::VisitReturnVoid(HReturnVoid* ret) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000948 ret->SetLocations(nullptr);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000949}
950
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000951void InstructionCodeGeneratorX86::VisitReturnVoid(HReturnVoid* ret) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700952 UNUSED(ret);
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000953 codegen_->GenerateFrameExit();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000954 __ ret();
955}
956
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000957void LocationsBuilderX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100958 LocationSummary* locations =
959 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100960 switch (ret->InputAt(0)->GetType()) {
961 case Primitive::kPrimBoolean:
962 case Primitive::kPrimByte:
963 case Primitive::kPrimChar:
964 case Primitive::kPrimShort:
965 case Primitive::kPrimInt:
966 case Primitive::kPrimNot:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100967 locations->SetInAt(0, Location::RegisterLocation(EAX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100968 break;
969
970 case Primitive::kPrimLong:
971 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100972 0, Location::RegisterPairLocation(EAX, EDX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100973 break;
974
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100975 case Primitive::kPrimFloat:
976 case Primitive::kPrimDouble:
977 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100978 0, Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100979 break;
980
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100981 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100982 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100983 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000984}
985
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000986void InstructionCodeGeneratorX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100987 if (kIsDebugBuild) {
988 switch (ret->InputAt(0)->GetType()) {
989 case Primitive::kPrimBoolean:
990 case Primitive::kPrimByte:
991 case Primitive::kPrimChar:
992 case Primitive::kPrimShort:
993 case Primitive::kPrimInt:
994 case Primitive::kPrimNot:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100995 DCHECK_EQ(ret->GetLocations()->InAt(0).As<Register>(), EAX);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100996 break;
997
998 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100999 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairLow<Register>(), EAX);
1000 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairHigh<Register>(), EDX);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001001 break;
1002
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001003 case Primitive::kPrimFloat:
1004 case Primitive::kPrimDouble:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001005 DCHECK_EQ(ret->GetLocations()->InAt(0).As<XmmRegister>(), XMM0);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001006 break;
1007
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001008 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001009 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001010 }
1011 }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001012 codegen_->GenerateFrameExit();
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001013 __ ret();
1014}
1015
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001016void LocationsBuilderX86::VisitInvokeStatic(HInvokeStatic* invoke) {
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001017 HandleInvoke(invoke);
1018}
1019
1020void InstructionCodeGeneratorX86::VisitInvokeStatic(HInvokeStatic* invoke) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001021 Register temp = invoke->GetLocations()->GetTemp(0).As<Register>();
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001022
1023 // TODO: Implement all kinds of calls:
1024 // 1) boot -> boot
1025 // 2) app -> boot
1026 // 3) app -> app
1027 //
1028 // Currently we implement the app -> app logic, which looks up in the resolve cache.
1029
1030 // temp = method;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001031 codegen_->LoadCurrentMethod(temp);
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001032 // temp = temp->dex_cache_resolved_methods_;
1033 __ movl(temp, Address(temp, mirror::ArtMethod::DexCacheResolvedMethodsOffset().Int32Value()));
1034 // temp = temp[index_in_cache]
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001035 __ movl(temp, Address(temp, CodeGenerator::GetCacheOffset(invoke->GetIndexInDexCache())));
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001036 // (temp + offset_of_quick_compiled_code)()
1037 __ call(Address(temp, mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset().Int32Value()));
1038
1039 DCHECK(!codegen_->IsLeafMethod());
1040 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
1041}
1042
1043void LocationsBuilderX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
1044 HandleInvoke(invoke);
1045}
1046
1047void LocationsBuilderX86::HandleInvoke(HInvoke* invoke) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001048 LocationSummary* locations =
1049 new (GetGraph()->GetArena()) LocationSummary(invoke, LocationSummary::kCall);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001050 locations->AddTemp(Location::RegisterLocation(EAX));
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001051
1052 InvokeDexCallingConventionVisitor calling_convention_visitor;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001053 for (size_t i = 0; i < invoke->InputCount(); i++) {
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001054 HInstruction* input = invoke->InputAt(i);
1055 locations->SetInAt(i, calling_convention_visitor.GetNextLocation(input->GetType()));
1056 }
1057
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001058 switch (invoke->GetType()) {
1059 case Primitive::kPrimBoolean:
1060 case Primitive::kPrimByte:
1061 case Primitive::kPrimChar:
1062 case Primitive::kPrimShort:
1063 case Primitive::kPrimInt:
1064 case Primitive::kPrimNot:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001065 locations->SetOut(Location::RegisterLocation(EAX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001066 break;
1067
1068 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001069 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001070 break;
1071
1072 case Primitive::kPrimVoid:
1073 break;
1074
1075 case Primitive::kPrimDouble:
1076 case Primitive::kPrimFloat:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001077 locations->SetOut(Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001078 break;
1079 }
1080
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001081 invoke->SetLocations(locations);
1082}
1083
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001084void InstructionCodeGeneratorX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001085 Register temp = invoke->GetLocations()->GetTemp(0).As<Register>();
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001086 uint32_t method_offset = mirror::Class::EmbeddedVTableOffset().Uint32Value() +
1087 invoke->GetVTableIndex() * sizeof(mirror::Class::VTableEntry);
1088 LocationSummary* locations = invoke->GetLocations();
1089 Location receiver = locations->InAt(0);
1090 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
1091 // temp = object->GetClass();
1092 if (receiver.IsStackSlot()) {
1093 __ movl(temp, Address(ESP, receiver.GetStackIndex()));
1094 __ movl(temp, Address(temp, class_offset));
1095 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001096 __ movl(temp, Address(receiver.As<Register>(), class_offset));
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001097 }
1098 // temp = temp->GetMethodAt(method_offset);
1099 __ movl(temp, Address(temp, method_offset));
1100 // call temp->GetEntryPoint();
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001101 __ call(Address(temp, mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset().Int32Value()));
1102
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001103 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +01001104 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001105}
1106
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001107void LocationsBuilderX86::VisitInvokeInterface(HInvokeInterface* invoke) {
1108 HandleInvoke(invoke);
1109 // Add the hidden argument.
1110 invoke->GetLocations()->AddTemp(Location::FpuRegisterLocation(XMM0));
1111}
1112
1113void InstructionCodeGeneratorX86::VisitInvokeInterface(HInvokeInterface* invoke) {
1114 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
1115 Register temp = invoke->GetLocations()->GetTemp(0).As<Register>();
1116 uint32_t method_offset = mirror::Class::EmbeddedImTableOffset().Uint32Value() +
1117 (invoke->GetImtIndex() % mirror::Class::kImtSize) * sizeof(mirror::Class::ImTableEntry);
1118 LocationSummary* locations = invoke->GetLocations();
1119 Location receiver = locations->InAt(0);
1120 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
1121
1122 // Set the hidden argument.
1123 __ movl(temp, Immediate(invoke->GetDexMethodIndex()));
1124 __ movd(invoke->GetLocations()->GetTemp(1).As<XmmRegister>(), temp);
1125
1126 // temp = object->GetClass();
1127 if (receiver.IsStackSlot()) {
1128 __ movl(temp, Address(ESP, receiver.GetStackIndex()));
1129 __ movl(temp, Address(temp, class_offset));
1130 } else {
1131 __ movl(temp, Address(receiver.As<Register>(), class_offset));
1132 }
1133 // temp = temp->GetImtEntryAt(method_offset);
1134 __ movl(temp, Address(temp, method_offset));
1135 // call temp->GetEntryPoint();
1136 __ call(Address(temp, mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset().Int32Value()));
1137
1138 DCHECK(!codegen_->IsLeafMethod());
1139 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
1140}
1141
Roland Levillain88cb1752014-10-20 16:36:47 +01001142void LocationsBuilderX86::VisitNeg(HNeg* neg) {
1143 LocationSummary* locations =
1144 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
1145 switch (neg->GetResultType()) {
1146 case Primitive::kPrimInt:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001147 case Primitive::kPrimLong:
Roland Levillain88cb1752014-10-20 16:36:47 +01001148 locations->SetInAt(0, Location::RequiresRegister());
1149 locations->SetOut(Location::SameAsFirstInput());
1150 break;
1151
Roland Levillain88cb1752014-10-20 16:36:47 +01001152 case Primitive::kPrimFloat:
1153 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00001154 locations->SetInAt(0, Location::RequiresFpuRegister());
1155 // Output overlaps as we need a fresh (zero-initialized)
1156 // register to perform subtraction from zero.
1157 locations->SetOut(Location::RequiresFpuRegister());
Roland Levillain88cb1752014-10-20 16:36:47 +01001158 break;
1159
1160 default:
1161 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1162 }
1163}
1164
1165void InstructionCodeGeneratorX86::VisitNeg(HNeg* neg) {
1166 LocationSummary* locations = neg->GetLocations();
1167 Location out = locations->Out();
1168 Location in = locations->InAt(0);
1169 switch (neg->GetResultType()) {
1170 case Primitive::kPrimInt:
1171 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00001172 DCHECK(in.Equals(out));
Roland Levillain88cb1752014-10-20 16:36:47 +01001173 __ negl(out.As<Register>());
1174 break;
1175
1176 case Primitive::kPrimLong:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001177 DCHECK(in.IsRegisterPair());
Roland Levillain3dbcb382014-10-28 17:30:07 +00001178 DCHECK(in.Equals(out));
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001179 __ negl(out.AsRegisterPairLow<Register>());
1180 // Negation is similar to subtraction from zero. The least
1181 // significant byte triggers a borrow when it is different from
1182 // zero; to take it into account, add 1 to the most significant
1183 // byte if the carry flag (CF) is set to 1 after the first NEGL
1184 // operation.
1185 __ adcl(out.AsRegisterPairHigh<Register>(), Immediate(0));
1186 __ negl(out.AsRegisterPairHigh<Register>());
1187 break;
1188
Roland Levillain88cb1752014-10-20 16:36:47 +01001189 case Primitive::kPrimFloat:
Roland Levillain3dbcb382014-10-28 17:30:07 +00001190 DCHECK(!in.Equals(out));
1191 // out = 0
1192 __ xorps(out.As<XmmRegister>(), out.As<XmmRegister>());
1193 // out = out - in
1194 __ subss(out.As<XmmRegister>(), in.As<XmmRegister>());
1195 break;
1196
Roland Levillain88cb1752014-10-20 16:36:47 +01001197 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00001198 DCHECK(!in.Equals(out));
1199 // out = 0
1200 __ xorpd(out.As<XmmRegister>(), out.As<XmmRegister>());
1201 // out = out - in
1202 __ subsd(out.As<XmmRegister>(), in.As<XmmRegister>());
Roland Levillain88cb1752014-10-20 16:36:47 +01001203 break;
1204
1205 default:
1206 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1207 }
1208}
1209
Roland Levillaindff1f282014-11-05 14:15:05 +00001210void LocationsBuilderX86::VisitTypeConversion(HTypeConversion* conversion) {
1211 LocationSummary* locations =
1212 new (GetGraph()->GetArena()) LocationSummary(conversion, LocationSummary::kNoCall);
1213 Primitive::Type result_type = conversion->GetResultType();
1214 Primitive::Type input_type = conversion->GetInputType();
1215 switch (result_type) {
1216 case Primitive::kPrimLong:
1217 switch (input_type) {
1218 case Primitive::kPrimByte:
1219 case Primitive::kPrimShort:
1220 case Primitive::kPrimInt:
1221 // int-to-long conversion.
1222 locations->SetInAt(0, Location::RegisterLocation(EAX));
1223 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
1224 break;
1225
1226 case Primitive::kPrimFloat:
1227 case Primitive::kPrimDouble:
1228 LOG(FATAL) << "Type conversion from " << input_type << " to "
1229 << result_type << " not yet implemented";
1230 break;
1231
1232 default:
1233 LOG(FATAL) << "Unexpected type conversion from " << input_type
1234 << " to " << result_type;
1235 }
1236 break;
1237
1238 case Primitive::kPrimInt:
1239 case Primitive::kPrimFloat:
1240 case Primitive::kPrimDouble:
1241 LOG(FATAL) << "Type conversion from " << input_type
1242 << " to " << result_type << " not yet implemented";
1243 break;
1244
1245 default:
1246 LOG(FATAL) << "Unexpected type conversion from " << input_type
1247 << " to " << result_type;
1248 }
1249}
1250
1251void InstructionCodeGeneratorX86::VisitTypeConversion(HTypeConversion* conversion) {
1252 LocationSummary* locations = conversion->GetLocations();
1253 Location out = locations->Out();
1254 Location in = locations->InAt(0);
1255 Primitive::Type result_type = conversion->GetResultType();
1256 Primitive::Type input_type = conversion->GetInputType();
1257 switch (result_type) {
1258 case Primitive::kPrimLong:
1259 switch (input_type) {
1260 case Primitive::kPrimByte:
1261 case Primitive::kPrimShort:
1262 case Primitive::kPrimInt:
1263 // int-to-long conversion.
1264 DCHECK_EQ(out.AsRegisterPairLow<Register>(), EAX);
1265 DCHECK_EQ(out.AsRegisterPairHigh<Register>(), EDX);
1266 DCHECK_EQ(in.As<Register>(), EAX);
1267 __ cdq();
1268 break;
1269
1270 case Primitive::kPrimFloat:
1271 case Primitive::kPrimDouble:
1272 LOG(FATAL) << "Type conversion from " << input_type << " to "
1273 << result_type << " not yet implemented";
1274 break;
1275
1276 default:
1277 LOG(FATAL) << "Unexpected type conversion from " << input_type
1278 << " to " << result_type;
1279 }
1280 break;
1281
1282 case Primitive::kPrimInt:
1283 case Primitive::kPrimFloat:
1284 case Primitive::kPrimDouble:
1285 LOG(FATAL) << "Type conversion from " << input_type
1286 << " to " << result_type << " not yet implemented";
1287 break;
1288
1289 default:
1290 LOG(FATAL) << "Unexpected type conversion from " << input_type
1291 << " to " << result_type;
1292 }
1293}
1294
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001295void LocationsBuilderX86::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001296 LocationSummary* locations =
1297 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001298 switch (add->GetResultType()) {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001299 case Primitive::kPrimInt:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001300 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001301 locations->SetInAt(0, Location::RequiresRegister());
1302 locations->SetInAt(1, Location::Any());
1303 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001304 break;
1305 }
1306
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001307 case Primitive::kPrimFloat:
1308 case Primitive::kPrimDouble: {
1309 locations->SetInAt(0, Location::RequiresFpuRegister());
1310 locations->SetInAt(1, Location::Any());
1311 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001312 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001313 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001314
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001315 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001316 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
1317 break;
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001318 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001319}
1320
1321void InstructionCodeGeneratorX86::VisitAdd(HAdd* add) {
1322 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001323 Location first = locations->InAt(0);
1324 Location second = locations->InAt(1);
Calin Juravle11351682014-10-23 15:38:15 +01001325 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001326 switch (add->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001327 case Primitive::kPrimInt: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001328 if (second.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001329 __ addl(first.As<Register>(), second.As<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001330 } else if (second.IsConstant()) {
Calin Juravle11351682014-10-23 15:38:15 +01001331 __ addl(first.As<Register>(), Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001332 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001333 __ addl(first.As<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001334 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001335 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001336 }
1337
1338 case Primitive::kPrimLong: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001339 if (second.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001340 __ addl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
1341 __ adcl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001342 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001343 __ addl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
1344 __ adcl(first.AsRegisterPairHigh<Register>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001345 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001346 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001347 break;
1348 }
1349
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001350 case Primitive::kPrimFloat: {
1351 if (second.IsFpuRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001352 __ addss(first.As<XmmRegister>(), second.As<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001353 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001354 __ addss(first.As<XmmRegister>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001355 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001356 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001357 }
1358
1359 case Primitive::kPrimDouble: {
1360 if (second.IsFpuRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001361 __ addsd(first.As<XmmRegister>(), second.As<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001362 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001363 __ addsd(first.As<XmmRegister>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001364 }
1365 break;
1366 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001367
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001368 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001369 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001370 }
1371}
1372
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001373void LocationsBuilderX86::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001374 LocationSummary* locations =
1375 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001376 switch (sub->GetResultType()) {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001377 case Primitive::kPrimInt:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001378 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001379 locations->SetInAt(0, Location::RequiresRegister());
1380 locations->SetInAt(1, Location::Any());
1381 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001382 break;
1383 }
Calin Juravle11351682014-10-23 15:38:15 +01001384 case Primitive::kPrimFloat:
1385 case Primitive::kPrimDouble: {
1386 locations->SetInAt(0, Location::RequiresFpuRegister());
1387 locations->SetInAt(1, Location::RequiresFpuRegister());
1388 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001389 break;
Calin Juravle11351682014-10-23 15:38:15 +01001390 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001391
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001392 default:
Calin Juravle11351682014-10-23 15:38:15 +01001393 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001394 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001395}
1396
1397void InstructionCodeGeneratorX86::VisitSub(HSub* sub) {
1398 LocationSummary* locations = sub->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001399 Location first = locations->InAt(0);
1400 Location second = locations->InAt(1);
Calin Juravle11351682014-10-23 15:38:15 +01001401 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001402 switch (sub->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001403 case Primitive::kPrimInt: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001404 if (second.IsRegister()) {
Calin Juravle11351682014-10-23 15:38:15 +01001405 __ subl(first.As<Register>(), second.As<Register>());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001406 } else if (second.IsConstant()) {
Calin Juravle11351682014-10-23 15:38:15 +01001407 __ subl(first.As<Register>(), Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001408 } else {
Calin Juravle11351682014-10-23 15:38:15 +01001409 __ subl(first.As<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001410 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001411 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001412 }
1413
1414 case Primitive::kPrimLong: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001415 if (second.IsRegister()) {
Calin Juravle11351682014-10-23 15:38:15 +01001416 __ subl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
1417 __ sbbl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001418 } else {
Calin Juravle11351682014-10-23 15:38:15 +01001419 __ subl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001420 __ sbbl(first.AsRegisterPairHigh<Register>(),
1421 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001422 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001423 break;
1424 }
1425
Calin Juravle11351682014-10-23 15:38:15 +01001426 case Primitive::kPrimFloat: {
1427 __ subss(first.As<XmmRegister>(), second.As<XmmRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001428 break;
Calin Juravle11351682014-10-23 15:38:15 +01001429 }
1430
1431 case Primitive::kPrimDouble: {
1432 __ subsd(first.As<XmmRegister>(), second.As<XmmRegister>());
1433 break;
1434 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001435
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001436 default:
Calin Juravle11351682014-10-23 15:38:15 +01001437 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001438 }
1439}
1440
Calin Juravle34bacdf2014-10-07 20:23:36 +01001441void LocationsBuilderX86::VisitMul(HMul* mul) {
1442 LocationSummary* locations =
1443 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
1444 switch (mul->GetResultType()) {
1445 case Primitive::kPrimInt:
1446 locations->SetInAt(0, Location::RequiresRegister());
1447 locations->SetInAt(1, Location::Any());
1448 locations->SetOut(Location::SameAsFirstInput());
1449 break;
1450 case Primitive::kPrimLong: {
1451 locations->SetInAt(0, Location::RequiresRegister());
1452 // TODO: Currently this handles only stack operands:
1453 // - we don't have enough registers because we currently use Quick ABI.
1454 // - by the time we have a working register allocator we will probably change the ABI
1455 // and fix the above.
1456 // - we don't have a way yet to request operands on stack but the base line compiler
1457 // will leave the operands on the stack with Any().
1458 locations->SetInAt(1, Location::Any());
1459 locations->SetOut(Location::SameAsFirstInput());
1460 // Needed for imul on 32bits with 64bits output.
1461 locations->AddTemp(Location::RegisterLocation(EAX));
1462 locations->AddTemp(Location::RegisterLocation(EDX));
1463 break;
1464 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01001465 case Primitive::kPrimFloat:
1466 case Primitive::kPrimDouble: {
1467 locations->SetInAt(0, Location::RequiresFpuRegister());
1468 locations->SetInAt(1, Location::RequiresFpuRegister());
1469 locations->SetOut(Location::SameAsFirstInput());
Calin Juravle34bacdf2014-10-07 20:23:36 +01001470 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01001471 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01001472
1473 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01001474 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01001475 }
1476}
1477
1478void InstructionCodeGeneratorX86::VisitMul(HMul* mul) {
1479 LocationSummary* locations = mul->GetLocations();
1480 Location first = locations->InAt(0);
1481 Location second = locations->InAt(1);
1482 DCHECK(first.Equals(locations->Out()));
1483
1484 switch (mul->GetResultType()) {
1485 case Primitive::kPrimInt: {
1486 if (second.IsRegister()) {
1487 __ imull(first.As<Register>(), second.As<Register>());
1488 } else if (second.IsConstant()) {
1489 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
1490 __ imull(first.As<Register>(), imm);
1491 } else {
1492 DCHECK(second.IsStackSlot());
1493 __ imull(first.As<Register>(), Address(ESP, second.GetStackIndex()));
1494 }
1495 break;
1496 }
1497
1498 case Primitive::kPrimLong: {
1499 DCHECK(second.IsDoubleStackSlot());
1500
1501 Register in1_hi = first.AsRegisterPairHigh<Register>();
1502 Register in1_lo = first.AsRegisterPairLow<Register>();
1503 Address in2_hi(ESP, second.GetHighStackIndex(kX86WordSize));
1504 Address in2_lo(ESP, second.GetStackIndex());
1505 Register eax = locations->GetTemp(0).As<Register>();
1506 Register edx = locations->GetTemp(1).As<Register>();
1507
1508 DCHECK_EQ(EAX, eax);
1509 DCHECK_EQ(EDX, edx);
1510
1511 // input: in1 - 64 bits, in2 - 64 bits
1512 // output: in1
1513 // formula: in1.hi : in1.lo = (in1.lo * in2.hi + in1.hi * in2.lo)* 2^32 + in1.lo * in2.lo
1514 // parts: in1.hi = in1.lo * in2.hi + in1.hi * in2.lo + (in1.lo * in2.lo)[63:32]
1515 // parts: in1.lo = (in1.lo * in2.lo)[31:0]
1516
1517 __ movl(eax, in2_hi);
1518 // eax <- in1.lo * in2.hi
1519 __ imull(eax, in1_lo);
1520 // in1.hi <- in1.hi * in2.lo
1521 __ imull(in1_hi, in2_lo);
1522 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
1523 __ addl(in1_hi, eax);
1524 // move in1_lo to eax to prepare for double precision
1525 __ movl(eax, in1_lo);
1526 // edx:eax <- in1.lo * in2.lo
1527 __ mull(in2_lo);
1528 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
1529 __ addl(in1_hi, edx);
1530 // in1.lo <- (in1.lo * in2.lo)[31:0];
1531 __ movl(in1_lo, eax);
1532
1533 break;
1534 }
1535
Calin Juravleb5bfa962014-10-21 18:02:24 +01001536 case Primitive::kPrimFloat: {
1537 __ mulss(first.As<XmmRegister>(), second.As<XmmRegister>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01001538 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01001539 }
1540
1541 case Primitive::kPrimDouble: {
1542 __ mulsd(first.As<XmmRegister>(), second.As<XmmRegister>());
1543 break;
1544 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01001545
1546 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01001547 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01001548 }
1549}
1550
Calin Juravle7c4954d2014-10-28 16:57:40 +00001551void LocationsBuilderX86::VisitDiv(HDiv* div) {
1552 LocationSummary* locations =
1553 new (GetGraph()->GetArena()) LocationSummary(div, LocationSummary::kNoCall);
1554 switch (div->GetResultType()) {
Calin Juravled0d48522014-11-04 16:40:20 +00001555 case Primitive::kPrimInt: {
1556 locations->SetInAt(0, Location::RegisterLocation(EAX));
1557 locations->SetInAt(1, Location::RequiresRegister());
1558 locations->SetOut(Location::SameAsFirstInput());
1559 // Intel uses edx:eax as the dividend.
1560 locations->AddTemp(Location::RegisterLocation(EDX));
1561 break;
1562 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00001563 case Primitive::kPrimLong: {
1564 LOG(FATAL) << "Not implemented div type" << div->GetResultType();
1565 break;
1566 }
1567 case Primitive::kPrimFloat:
1568 case Primitive::kPrimDouble: {
1569 locations->SetInAt(0, Location::RequiresFpuRegister());
1570 locations->SetInAt(1, Location::RequiresFpuRegister());
1571 locations->SetOut(Location::SameAsFirstInput());
1572 break;
1573 }
1574
1575 default:
1576 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
1577 }
1578}
1579
1580void InstructionCodeGeneratorX86::VisitDiv(HDiv* div) {
1581 LocationSummary* locations = div->GetLocations();
1582 Location first = locations->InAt(0);
1583 Location second = locations->InAt(1);
1584 DCHECK(first.Equals(locations->Out()));
1585
1586 switch (div->GetResultType()) {
Calin Juravled0d48522014-11-04 16:40:20 +00001587 case Primitive::kPrimInt: {
1588 Register first_reg = first.As<Register>();
1589 Register second_reg = second.As<Register>();
1590 DCHECK_EQ(EAX, first_reg);
1591 DCHECK_EQ(EDX, locations->GetTemp(0).As<Register>());
1592
1593 SlowPathCodeX86* slow_path =
1594 new (GetGraph()->GetArena()) DivMinusOneSlowPathX86(first_reg);
1595 codegen_->AddSlowPath(slow_path);
1596
1597 // 0x80000000/-1 triggers an arithmetic exception!
1598 // Dividing by -1 is actually negation and -0x800000000 = 0x80000000 so
1599 // it's safe to just use negl instead of more complex comparisons.
1600
1601 __ cmpl(second_reg, Immediate(-1));
1602 __ j(kEqual, slow_path->GetEntryLabel());
1603
1604 // edx:eax <- sign-extended of eax
1605 __ cdq();
1606 // eax = quotient, edx = remainder
1607 __ idivl(second_reg);
1608
1609 __ Bind(slow_path->GetExitLabel());
1610 break;
1611 }
1612
Calin Juravle7c4954d2014-10-28 16:57:40 +00001613 case Primitive::kPrimLong: {
1614 LOG(FATAL) << "Not implemented div type" << div->GetResultType();
1615 break;
1616 }
1617
1618 case Primitive::kPrimFloat: {
1619 __ divss(first.As<XmmRegister>(), second.As<XmmRegister>());
1620 break;
1621 }
1622
1623 case Primitive::kPrimDouble: {
1624 __ divsd(first.As<XmmRegister>(), second.As<XmmRegister>());
1625 break;
1626 }
1627
1628 default:
1629 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
1630 }
1631}
1632
Calin Juravled0d48522014-11-04 16:40:20 +00001633void LocationsBuilderX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
1634 LocationSummary* locations =
1635 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1636 locations->SetInAt(0, Location::Any());
1637 if (instruction->HasUses()) {
1638 locations->SetOut(Location::SameAsFirstInput());
1639 }
1640}
1641
1642void InstructionCodeGeneratorX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
1643 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) DivZeroCheckSlowPathX86(instruction);
1644 codegen_->AddSlowPath(slow_path);
1645
1646 LocationSummary* locations = instruction->GetLocations();
1647 Location value = locations->InAt(0);
1648
1649 if (value.IsRegister()) {
1650 __ testl(value.As<Register>(), value.As<Register>());
1651 } else if (value.IsStackSlot()) {
1652 __ cmpl(Address(ESP, value.GetStackIndex()), Immediate(0));
1653 } else {
1654 DCHECK(value.IsConstant()) << value;
1655 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
1656 __ jmp(slow_path->GetEntryLabel());
1657 }
1658 return;
1659 }
1660 __ j(kEqual, slow_path->GetEntryLabel());
1661}
1662
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001663void LocationsBuilderX86::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001664 LocationSummary* locations =
1665 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001666 locations->SetOut(Location::RegisterLocation(EAX));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001667 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001668 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1669 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001670}
1671
1672void InstructionCodeGeneratorX86::VisitNewInstance(HNewInstance* instruction) {
1673 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001674 codegen_->LoadCurrentMethod(calling_convention.GetRegisterAt(1));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001675 __ movl(calling_convention.GetRegisterAt(0), Immediate(instruction->GetTypeIndex()));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001676
Nicolas Geoffray707c8092014-04-04 10:50:14 +01001677 __ fs()->call(
1678 Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pAllocObjectWithAccessCheck)));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001679
Nicolas Geoffray39468442014-09-02 15:17:15 +01001680 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001681 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001682}
1683
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001684void LocationsBuilderX86::VisitNewArray(HNewArray* instruction) {
1685 LocationSummary* locations =
1686 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
1687 locations->SetOut(Location::RegisterLocation(EAX));
1688 InvokeRuntimeCallingConvention calling_convention;
1689 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1690 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
1691 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
1692}
1693
1694void InstructionCodeGeneratorX86::VisitNewArray(HNewArray* instruction) {
1695 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001696 codegen_->LoadCurrentMethod(calling_convention.GetRegisterAt(1));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001697 __ movl(calling_convention.GetRegisterAt(0), Immediate(instruction->GetTypeIndex()));
1698
1699 __ fs()->call(
1700 Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pAllocArrayWithAccessCheck)));
1701
1702 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
1703 DCHECK(!codegen_->IsLeafMethod());
1704}
1705
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001706void LocationsBuilderX86::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001707 LocationSummary* locations =
1708 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001709 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
1710 if (location.IsStackSlot()) {
1711 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
1712 } else if (location.IsDoubleStackSlot()) {
1713 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001714 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001715 locations->SetOut(location);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001716}
1717
1718void InstructionCodeGeneratorX86::VisitParameterValue(HParameterValue* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001719 UNUSED(instruction);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001720}
1721
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001722void LocationsBuilderX86::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001723 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001724 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001725 locations->SetInAt(0, Location::RequiresRegister());
1726 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01001727}
1728
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001729void InstructionCodeGeneratorX86::VisitNot(HNot* not_) {
1730 LocationSummary* locations = not_->GetLocations();
Roland Levillain70566432014-10-24 16:20:17 +01001731 Location in = locations->InAt(0);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001732 Location out = locations->Out();
Roland Levillain70566432014-10-24 16:20:17 +01001733 DCHECK(in.Equals(out));
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001734 switch (not_->InputAt(0)->GetType()) {
1735 case Primitive::kPrimBoolean:
1736 __ xorl(out.As<Register>(), Immediate(1));
1737 break;
1738
1739 case Primitive::kPrimInt:
1740 __ notl(out.As<Register>());
1741 break;
1742
1743 case Primitive::kPrimLong:
Roland Levillain70566432014-10-24 16:20:17 +01001744 __ notl(out.AsRegisterPairLow<Register>());
1745 __ notl(out.AsRegisterPairHigh<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001746 break;
1747
1748 default:
1749 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
1750 }
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01001751}
1752
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001753void LocationsBuilderX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001754 LocationSummary* locations =
1755 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01001756 locations->SetInAt(0, Location::RequiresRegister());
1757 locations->SetInAt(1, Location::Any());
1758 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001759}
1760
1761void InstructionCodeGeneratorX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001762 LocationSummary* locations = compare->GetLocations();
1763 switch (compare->InputAt(0)->GetType()) {
1764 case Primitive::kPrimLong: {
1765 Label less, greater, done;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001766 Register output = locations->Out().As<Register>();
1767 Location left = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001768 Location right = locations->InAt(1);
1769 if (right.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001770 __ cmpl(left.AsRegisterPairHigh<Register>(), right.AsRegisterPairHigh<Register>());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001771 } else {
1772 DCHECK(right.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001773 __ cmpl(left.AsRegisterPairHigh<Register>(),
1774 Address(ESP, right.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001775 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001776 __ j(kLess, &less); // Signed compare.
1777 __ j(kGreater, &greater); // Signed compare.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001778 if (right.IsRegisterPair()) {
1779 __ cmpl(left.AsRegisterPairLow<Register>(), right.AsRegisterPairLow<Register>());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001780 } else {
1781 DCHECK(right.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001782 __ cmpl(left.AsRegisterPairLow<Register>(), Address(ESP, right.GetStackIndex()));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001783 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001784 __ movl(output, Immediate(0));
1785 __ j(kEqual, &done);
1786 __ j(kBelow, &less); // Unsigned compare.
1787
1788 __ Bind(&greater);
1789 __ movl(output, Immediate(1));
1790 __ jmp(&done);
1791
1792 __ Bind(&less);
1793 __ movl(output, Immediate(-1));
1794
1795 __ Bind(&done);
1796 break;
1797 }
1798 default:
1799 LOG(FATAL) << "Unimplemented compare type " << compare->InputAt(0)->GetType();
1800 }
1801}
1802
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001803void LocationsBuilderX86::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001804 LocationSummary* locations =
1805 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01001806 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
1807 locations->SetInAt(i, Location::Any());
1808 }
1809 locations->SetOut(Location::Any());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001810}
1811
1812void InstructionCodeGeneratorX86::VisitPhi(HPhi* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001813 UNUSED(instruction);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001814 LOG(FATAL) << "Unreachable";
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001815}
1816
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001817void LocationsBuilderX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001818 LocationSummary* locations =
1819 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001820 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray39468442014-09-02 15:17:15 +01001821 Primitive::Type field_type = instruction->GetFieldType();
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01001822 bool is_object_type = field_type == Primitive::kPrimNot;
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01001823 bool is_byte_type = (field_type == Primitive::kPrimBoolean)
1824 || (field_type == Primitive::kPrimByte);
1825 // The register allocator does not support multiple
1826 // inputs that die at entry with one in a specific register.
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01001827 if (is_byte_type) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001828 // Ensure the value is in a byte register.
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01001829 locations->SetInAt(1, Location::RegisterLocation(EAX));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001830 } else {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01001831 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001832 }
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01001833 // Temporary registers for the write barrier.
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01001834 if (is_object_type) {
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01001835 locations->AddTemp(Location::RequiresRegister());
1836 // Ensure the card is in a byte register.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001837 locations->AddTemp(Location::RegisterLocation(ECX));
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01001838 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001839}
1840
1841void InstructionCodeGeneratorX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
1842 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001843 Register obj = locations->InAt(0).As<Register>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001844 uint32_t offset = instruction->GetFieldOffset().Uint32Value();
Nicolas Geoffray39468442014-09-02 15:17:15 +01001845 Primitive::Type field_type = instruction->GetFieldType();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001846
1847 switch (field_type) {
1848 case Primitive::kPrimBoolean:
1849 case Primitive::kPrimByte: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001850 ByteRegister value = locations->InAt(1).As<ByteRegister>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001851 __ movb(Address(obj, offset), value);
1852 break;
1853 }
1854
1855 case Primitive::kPrimShort:
1856 case Primitive::kPrimChar: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001857 Register value = locations->InAt(1).As<Register>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001858 __ movw(Address(obj, offset), value);
1859 break;
1860 }
1861
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001862 case Primitive::kPrimInt:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001863 case Primitive::kPrimNot: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001864 Register value = locations->InAt(1).As<Register>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001865 __ movl(Address(obj, offset), value);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001866
1867 if (field_type == Primitive::kPrimNot) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001868 Register temp = locations->GetTemp(0).As<Register>();
1869 Register card = locations->GetTemp(1).As<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001870 codegen_->MarkGCCard(temp, card, obj, value);
1871 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001872 break;
1873 }
1874
1875 case Primitive::kPrimLong: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001876 Location value = locations->InAt(1);
1877 __ movl(Address(obj, offset), value.AsRegisterPairLow<Register>());
1878 __ movl(Address(obj, kX86WordSize + offset), value.AsRegisterPairHigh<Register>());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001879 break;
1880 }
1881
1882 case Primitive::kPrimFloat:
1883 case Primitive::kPrimDouble:
1884 LOG(FATAL) << "Unimplemented register type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07001885 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001886 case Primitive::kPrimVoid:
1887 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07001888 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001889 }
1890}
1891
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001892void CodeGeneratorX86::MarkGCCard(Register temp, Register card, Register object, Register value) {
1893 Label is_null;
1894 __ testl(value, value);
1895 __ j(kEqual, &is_null);
1896 __ fs()->movl(card, Address::Absolute(Thread::CardTableOffset<kX86WordSize>().Int32Value()));
1897 __ movl(temp, object);
1898 __ shrl(temp, Immediate(gc::accounting::CardTable::kCardShift));
1899 __ movb(Address(temp, card, TIMES_1, 0),
1900 X86ManagedRegister::FromCpuRegister(card).AsByteRegister());
1901 __ Bind(&is_null);
1902}
1903
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001904void LocationsBuilderX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001905 LocationSummary* locations =
1906 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01001907 locations->SetInAt(0, Location::RequiresRegister());
1908 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001909}
1910
1911void InstructionCodeGeneratorX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
1912 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001913 Register obj = locations->InAt(0).As<Register>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001914 uint32_t offset = instruction->GetFieldOffset().Uint32Value();
1915
1916 switch (instruction->GetType()) {
1917 case Primitive::kPrimBoolean: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001918 Register out = locations->Out().As<Register>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001919 __ movzxb(out, Address(obj, offset));
1920 break;
1921 }
1922
1923 case Primitive::kPrimByte: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001924 Register out = locations->Out().As<Register>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001925 __ movsxb(out, Address(obj, offset));
1926 break;
1927 }
1928
1929 case Primitive::kPrimShort: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001930 Register out = locations->Out().As<Register>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001931 __ movsxw(out, Address(obj, offset));
1932 break;
1933 }
1934
1935 case Primitive::kPrimChar: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001936 Register out = locations->Out().As<Register>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001937 __ movzxw(out, Address(obj, offset));
1938 break;
1939 }
1940
1941 case Primitive::kPrimInt:
1942 case Primitive::kPrimNot: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001943 Register out = locations->Out().As<Register>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001944 __ movl(out, Address(obj, offset));
1945 break;
1946 }
1947
1948 case Primitive::kPrimLong: {
1949 // TODO: support volatile.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001950 __ movl(locations->Out().AsRegisterPairLow<Register>(), Address(obj, offset));
1951 __ movl(locations->Out().AsRegisterPairHigh<Register>(), Address(obj, kX86WordSize + offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001952 break;
1953 }
1954
1955 case Primitive::kPrimFloat:
1956 case Primitive::kPrimDouble:
1957 LOG(FATAL) << "Unimplemented register type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07001958 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001959 case Primitive::kPrimVoid:
1960 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07001961 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001962 }
1963}
1964
1965void LocationsBuilderX86::VisitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001966 LocationSummary* locations =
1967 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001968 locations->SetInAt(0, Location::Any());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001969 if (instruction->HasUses()) {
1970 locations->SetOut(Location::SameAsFirstInput());
1971 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001972}
1973
1974void InstructionCodeGeneratorX86::VisitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01001975 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathX86(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001976 codegen_->AddSlowPath(slow_path);
1977
1978 LocationSummary* locations = instruction->GetLocations();
1979 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001980
1981 if (obj.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001982 __ cmpl(obj.As<Register>(), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001983 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001984 __ cmpl(Address(ESP, obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001985 } else {
1986 DCHECK(obj.IsConstant()) << obj;
1987 DCHECK_EQ(obj.GetConstant()->AsIntConstant()->GetValue(), 0);
1988 __ jmp(slow_path->GetEntryLabel());
1989 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001990 }
1991 __ j(kEqual, slow_path->GetEntryLabel());
1992}
1993
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001994void LocationsBuilderX86::VisitArrayGet(HArrayGet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001995 LocationSummary* locations =
1996 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01001997 locations->SetInAt(0, Location::RequiresRegister());
1998 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
1999 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002000}
2001
2002void InstructionCodeGeneratorX86::VisitArrayGet(HArrayGet* instruction) {
2003 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002004 Register obj = locations->InAt(0).As<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002005 Location index = locations->InAt(1);
2006
2007 switch (instruction->GetType()) {
2008 case Primitive::kPrimBoolean: {
2009 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002010 Register out = locations->Out().As<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002011 if (index.IsConstant()) {
2012 __ movzxb(out, Address(obj,
2013 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
2014 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002015 __ movzxb(out, Address(obj, index.As<Register>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002016 }
2017 break;
2018 }
2019
2020 case Primitive::kPrimByte: {
2021 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int8_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002022 Register out = locations->Out().As<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002023 if (index.IsConstant()) {
2024 __ movsxb(out, Address(obj,
2025 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
2026 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002027 __ movsxb(out, Address(obj, index.As<Register>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002028 }
2029 break;
2030 }
2031
2032 case Primitive::kPrimShort: {
2033 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int16_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002034 Register out = locations->Out().As<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002035 if (index.IsConstant()) {
2036 __ movsxw(out, Address(obj,
2037 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
2038 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002039 __ movsxw(out, Address(obj, index.As<Register>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002040 }
2041 break;
2042 }
2043
2044 case Primitive::kPrimChar: {
2045 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002046 Register out = locations->Out().As<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002047 if (index.IsConstant()) {
2048 __ movzxw(out, Address(obj,
2049 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
2050 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002051 __ movzxw(out, Address(obj, index.As<Register>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002052 }
2053 break;
2054 }
2055
2056 case Primitive::kPrimInt:
2057 case Primitive::kPrimNot: {
2058 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002059 Register out = locations->Out().As<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002060 if (index.IsConstant()) {
2061 __ movl(out, Address(obj,
2062 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
2063 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002064 __ movl(out, Address(obj, index.As<Register>(), TIMES_4, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002065 }
2066 break;
2067 }
2068
2069 case Primitive::kPrimLong: {
2070 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002071 Location out = locations->Out();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002072 if (index.IsConstant()) {
2073 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002074 __ movl(out.AsRegisterPairLow<Register>(), Address(obj, offset));
2075 __ movl(out.AsRegisterPairHigh<Register>(), Address(obj, offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002076 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002077 __ movl(out.AsRegisterPairLow<Register>(),
2078 Address(obj, index.As<Register>(), TIMES_8, data_offset));
2079 __ movl(out.AsRegisterPairHigh<Register>(),
2080 Address(obj, index.As<Register>(), TIMES_8, data_offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002081 }
2082 break;
2083 }
2084
2085 case Primitive::kPrimFloat:
2086 case Primitive::kPrimDouble:
2087 LOG(FATAL) << "Unimplemented register type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07002088 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002089 case Primitive::kPrimVoid:
2090 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07002091 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002092 }
2093}
2094
2095void LocationsBuilderX86::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002096 Primitive::Type value_type = instruction->GetComponentType();
2097 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
2098 instruction,
2099 value_type == Primitive::kPrimNot ? LocationSummary::kCall : LocationSummary::kNoCall);
2100
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002101 if (value_type == Primitive::kPrimNot) {
2102 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002103 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2104 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
2105 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002106 } else {
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01002107 bool is_byte_type = (value_type == Primitive::kPrimBoolean)
2108 || (value_type == Primitive::kPrimByte);
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01002109 // We need the inputs to be different than the output in case of long operation.
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01002110 // In case of a byte operation, the register allocator does not support multiple
2111 // inputs that die at entry with one in a specific register.
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002112 locations->SetInAt(0, Location::RequiresRegister());
2113 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01002114 if (is_byte_type) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002115 // Ensure the value is in a byte register.
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002116 locations->SetInAt(2, Location::ByteRegisterOrConstant(EAX, instruction->InputAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002117 } else {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002118 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002119 }
2120 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002121}
2122
2123void InstructionCodeGeneratorX86::VisitArraySet(HArraySet* instruction) {
2124 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002125 Register obj = locations->InAt(0).As<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002126 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002127 Location value = locations->InAt(2);
Nicolas Geoffray39468442014-09-02 15:17:15 +01002128 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002129
2130 switch (value_type) {
2131 case Primitive::kPrimBoolean:
2132 case Primitive::kPrimByte: {
2133 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002134 if (index.IsConstant()) {
2135 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002136 if (value.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002137 __ movb(Address(obj, offset), value.As<ByteRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002138 } else {
2139 __ movb(Address(obj, offset),
2140 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
2141 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002142 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002143 if (value.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002144 __ movb(Address(obj, index.As<Register>(), TIMES_1, data_offset),
2145 value.As<ByteRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002146 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002147 __ movb(Address(obj, index.As<Register>(), TIMES_1, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002148 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
2149 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002150 }
2151 break;
2152 }
2153
2154 case Primitive::kPrimShort:
2155 case Primitive::kPrimChar: {
2156 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002157 if (index.IsConstant()) {
2158 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002159 if (value.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002160 __ movw(Address(obj, offset), value.As<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002161 } else {
2162 __ movw(Address(obj, offset),
2163 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
2164 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002165 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002166 if (value.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002167 __ movw(Address(obj, index.As<Register>(), TIMES_2, data_offset),
2168 value.As<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002169 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002170 __ movw(Address(obj, index.As<Register>(), TIMES_2, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002171 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
2172 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002173 }
2174 break;
2175 }
2176
2177 case Primitive::kPrimInt: {
2178 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002179 if (index.IsConstant()) {
2180 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002181 if (value.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002182 __ movl(Address(obj, offset), value.As<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002183 } else {
2184 __ movl(Address(obj, offset), Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
2185 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002186 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002187 if (value.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002188 __ movl(Address(obj, index.As<Register>(), TIMES_4, data_offset),
2189 value.As<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002190 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002191 __ movl(Address(obj, index.As<Register>(), TIMES_4, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002192 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
2193 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002194 }
2195 break;
2196 }
2197
2198 case Primitive::kPrimNot: {
2199 DCHECK(!codegen_->IsLeafMethod());
2200 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pAputObject)));
Nicolas Geoffray39468442014-09-02 15:17:15 +01002201 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002202 break;
2203 }
2204
2205 case Primitive::kPrimLong: {
2206 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002207 if (index.IsConstant()) {
2208 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002209 if (value.IsRegisterPair()) {
2210 __ movl(Address(obj, offset), value.AsRegisterPairLow<Register>());
2211 __ movl(Address(obj, offset + kX86WordSize), value.AsRegisterPairHigh<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002212 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002213 DCHECK(value.IsConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002214 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
2215 __ movl(Address(obj, offset), Immediate(Low32Bits(val)));
2216 __ movl(Address(obj, offset + kX86WordSize), Immediate(High32Bits(val)));
2217 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002218 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002219 if (value.IsRegisterPair()) {
2220 __ movl(Address(obj, index.As<Register>(), TIMES_8, data_offset),
2221 value.AsRegisterPairLow<Register>());
2222 __ movl(Address(obj, index.As<Register>(), TIMES_8, data_offset + kX86WordSize),
2223 value.AsRegisterPairHigh<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002224 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002225 DCHECK(value.IsConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002226 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002227 __ movl(Address(obj, index.As<Register>(), TIMES_8, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002228 Immediate(Low32Bits(val)));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002229 __ movl(Address(obj, index.As<Register>(), TIMES_8, data_offset + kX86WordSize),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002230 Immediate(High32Bits(val)));
2231 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002232 }
2233 break;
2234 }
2235
2236 case Primitive::kPrimFloat:
2237 case Primitive::kPrimDouble:
2238 LOG(FATAL) << "Unimplemented register type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07002239 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002240 case Primitive::kPrimVoid:
2241 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07002242 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002243 }
2244}
2245
2246void LocationsBuilderX86::VisitArrayLength(HArrayLength* instruction) {
2247 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002248 locations->SetInAt(0, Location::RequiresRegister());
2249 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002250 instruction->SetLocations(locations);
2251}
2252
2253void InstructionCodeGeneratorX86::VisitArrayLength(HArrayLength* instruction) {
2254 LocationSummary* locations = instruction->GetLocations();
2255 uint32_t offset = mirror::Array::LengthOffset().Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002256 Register obj = locations->InAt(0).As<Register>();
2257 Register out = locations->Out().As<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002258 __ movl(out, Address(obj, offset));
2259}
2260
2261void LocationsBuilderX86::VisitBoundsCheck(HBoundsCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002262 LocationSummary* locations =
2263 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002264 locations->SetInAt(0, Location::RequiresRegister());
2265 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002266 if (instruction->HasUses()) {
2267 locations->SetOut(Location::SameAsFirstInput());
2268 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002269}
2270
2271void InstructionCodeGeneratorX86::VisitBoundsCheck(HBoundsCheck* instruction) {
2272 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01002273 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) BoundsCheckSlowPathX86(
Nicolas Geoffray39468442014-09-02 15:17:15 +01002274 instruction, locations->InAt(0), locations->InAt(1));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002275 codegen_->AddSlowPath(slow_path);
2276
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002277 Register index = locations->InAt(0).As<Register>();
2278 Register length = locations->InAt(1).As<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002279
2280 __ cmpl(index, length);
2281 __ j(kAboveEqual, slow_path->GetEntryLabel());
2282}
2283
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002284void LocationsBuilderX86::VisitTemporary(HTemporary* temp) {
2285 temp->SetLocations(nullptr);
2286}
2287
2288void InstructionCodeGeneratorX86::VisitTemporary(HTemporary* temp) {
2289 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002290 UNUSED(temp);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002291}
2292
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01002293void LocationsBuilderX86::VisitParallelMove(HParallelMove* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002294 UNUSED(instruction);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002295 LOG(FATAL) << "Unreachable";
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01002296}
2297
2298void InstructionCodeGeneratorX86::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002299 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
2300}
2301
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00002302void LocationsBuilderX86::VisitSuspendCheck(HSuspendCheck* instruction) {
2303 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
2304}
2305
2306void InstructionCodeGeneratorX86::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01002307 HBasicBlock* block = instruction->GetBlock();
2308 if (block->GetLoopInformation() != nullptr) {
2309 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
2310 // The back edge will generate the suspend check.
2311 return;
2312 }
2313 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
2314 // The goto will generate the suspend check.
2315 return;
2316 }
2317 GenerateSuspendCheck(instruction, nullptr);
2318}
2319
2320void InstructionCodeGeneratorX86::GenerateSuspendCheck(HSuspendCheck* instruction,
2321 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00002322 SuspendCheckSlowPathX86* slow_path =
Nicolas Geoffray3c049742014-09-24 18:10:46 +01002323 new (GetGraph()->GetArena()) SuspendCheckSlowPathX86(instruction, successor);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00002324 codegen_->AddSlowPath(slow_path);
Nicolas Geoffray3c049742014-09-24 18:10:46 +01002325 __ fs()->cmpw(Address::Absolute(
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00002326 Thread::ThreadFlagsOffset<kX86WordSize>().Int32Value()), Immediate(0));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01002327 if (successor == nullptr) {
2328 __ j(kNotEqual, slow_path->GetEntryLabel());
2329 __ Bind(slow_path->GetReturnLabel());
2330 } else {
2331 __ j(kEqual, codegen_->GetLabelOf(successor));
2332 __ jmp(slow_path->GetEntryLabel());
2333 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00002334}
2335
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002336X86Assembler* ParallelMoveResolverX86::GetAssembler() const {
2337 return codegen_->GetAssembler();
2338}
2339
2340void ParallelMoveResolverX86::MoveMemoryToMemory(int dst, int src) {
2341 ScratchRegisterScope ensure_scratch(
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01002342 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002343 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
2344 __ movl(static_cast<Register>(ensure_scratch.GetRegister()), Address(ESP, src + stack_offset));
2345 __ movl(Address(ESP, dst + stack_offset), static_cast<Register>(ensure_scratch.GetRegister()));
2346}
2347
2348void ParallelMoveResolverX86::EmitMove(size_t index) {
2349 MoveOperands* move = moves_.Get(index);
2350 Location source = move->GetSource();
2351 Location destination = move->GetDestination();
2352
2353 if (source.IsRegister()) {
2354 if (destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002355 __ movl(destination.As<Register>(), source.As<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002356 } else {
2357 DCHECK(destination.IsStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002358 __ movl(Address(ESP, destination.GetStackIndex()), source.As<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002359 }
2360 } else if (source.IsStackSlot()) {
2361 if (destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002362 __ movl(destination.As<Register>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002363 } else {
2364 DCHECK(destination.IsStackSlot());
2365 MoveMemoryToMemory(destination.GetStackIndex(),
2366 source.GetStackIndex());
2367 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002368 } else if (source.IsConstant()) {
2369 HIntConstant* instruction = source.GetConstant()->AsIntConstant();
2370 Immediate imm(instruction->AsIntConstant()->GetValue());
2371 if (destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002372 __ movl(destination.As<Register>(), imm);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002373 } else {
2374 __ movl(Address(ESP, destination.GetStackIndex()), imm);
2375 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002376 } else {
2377 LOG(FATAL) << "Unimplemented";
2378 }
2379}
2380
2381void ParallelMoveResolverX86::Exchange(Register reg, int mem) {
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01002382 Register suggested_scratch = reg == EAX ? EBX : EAX;
2383 ScratchRegisterScope ensure_scratch(
2384 this, reg, suggested_scratch, codegen_->GetNumberOfCoreRegisters());
2385
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002386 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
2387 __ movl(static_cast<Register>(ensure_scratch.GetRegister()), Address(ESP, mem + stack_offset));
2388 __ movl(Address(ESP, mem + stack_offset), reg);
2389 __ movl(reg, static_cast<Register>(ensure_scratch.GetRegister()));
2390}
2391
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002392void ParallelMoveResolverX86::Exchange(int mem1, int mem2) {
2393 ScratchRegisterScope ensure_scratch1(
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01002394 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
2395
2396 Register suggested_scratch = ensure_scratch1.GetRegister() == EAX ? EBX : EAX;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002397 ScratchRegisterScope ensure_scratch2(
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01002398 this, ensure_scratch1.GetRegister(), suggested_scratch, codegen_->GetNumberOfCoreRegisters());
2399
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002400 int stack_offset = ensure_scratch1.IsSpilled() ? kX86WordSize : 0;
2401 stack_offset += ensure_scratch2.IsSpilled() ? kX86WordSize : 0;
2402 __ movl(static_cast<Register>(ensure_scratch1.GetRegister()), Address(ESP, mem1 + stack_offset));
2403 __ movl(static_cast<Register>(ensure_scratch2.GetRegister()), Address(ESP, mem2 + stack_offset));
2404 __ movl(Address(ESP, mem2 + stack_offset), static_cast<Register>(ensure_scratch1.GetRegister()));
2405 __ movl(Address(ESP, mem1 + stack_offset), static_cast<Register>(ensure_scratch2.GetRegister()));
2406}
2407
2408void ParallelMoveResolverX86::EmitSwap(size_t index) {
2409 MoveOperands* move = moves_.Get(index);
2410 Location source = move->GetSource();
2411 Location destination = move->GetDestination();
2412
2413 if (source.IsRegister() && destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002414 __ xchgl(destination.As<Register>(), source.As<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002415 } else if (source.IsRegister() && destination.IsStackSlot()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002416 Exchange(source.As<Register>(), destination.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002417 } else if (source.IsStackSlot() && destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002418 Exchange(destination.As<Register>(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002419 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
2420 Exchange(destination.GetStackIndex(), source.GetStackIndex());
2421 } else {
2422 LOG(FATAL) << "Unimplemented";
2423 }
2424}
2425
2426void ParallelMoveResolverX86::SpillScratch(int reg) {
2427 __ pushl(static_cast<Register>(reg));
2428}
2429
2430void ParallelMoveResolverX86::RestoreScratch(int reg) {
2431 __ popl(static_cast<Register>(reg));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01002432}
2433
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002434void LocationsBuilderX86::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002435 LocationSummary::CallKind call_kind = cls->CanCallRuntime()
2436 ? LocationSummary::kCallOnSlowPath
2437 : LocationSummary::kNoCall;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002438 LocationSummary* locations =
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002439 new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002440 locations->SetOut(Location::RequiresRegister());
2441}
2442
2443void InstructionCodeGeneratorX86::VisitLoadClass(HLoadClass* cls) {
2444 Register out = cls->GetLocations()->Out().As<Register>();
2445 if (cls->IsReferrersClass()) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002446 DCHECK(!cls->CanCallRuntime());
2447 DCHECK(!cls->MustGenerateClinitCheck());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002448 codegen_->LoadCurrentMethod(out);
2449 __ movl(out, Address(out, mirror::ArtMethod::DeclaringClassOffset().Int32Value()));
2450 } else {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002451 DCHECK(cls->CanCallRuntime());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002452 codegen_->LoadCurrentMethod(out);
2453 __ movl(out, Address(out, mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value()));
2454 __ movl(out, Address(out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex())));
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002455
2456 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86(
2457 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
2458 codegen_->AddSlowPath(slow_path);
2459 __ testl(out, out);
2460 __ j(kEqual, slow_path->GetEntryLabel());
2461 if (cls->MustGenerateClinitCheck()) {
2462 GenerateClassInitializationCheck(slow_path, out);
2463 } else {
2464 __ Bind(slow_path->GetExitLabel());
2465 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002466 }
2467}
2468
2469void LocationsBuilderX86::VisitClinitCheck(HClinitCheck* check) {
2470 LocationSummary* locations =
2471 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
2472 locations->SetInAt(0, Location::RequiresRegister());
2473 if (check->HasUses()) {
2474 locations->SetOut(Location::SameAsFirstInput());
2475 }
2476}
2477
2478void InstructionCodeGeneratorX86::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002479 // We assume the class to not be null.
2480 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86(
2481 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002482 codegen_->AddSlowPath(slow_path);
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002483 GenerateClassInitializationCheck(slow_path, check->GetLocations()->InAt(0).As<Register>());
2484}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002485
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002486void InstructionCodeGeneratorX86::GenerateClassInitializationCheck(
2487 SlowPathCodeX86* slow_path, Register class_reg) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002488 __ cmpl(Address(class_reg, mirror::Class::StatusOffset().Int32Value()),
2489 Immediate(mirror::Class::kStatusInitialized));
2490 __ j(kLess, slow_path->GetEntryLabel());
2491 __ Bind(slow_path->GetExitLabel());
2492 // No need for memory fence, thanks to the X86 memory model.
2493}
2494
2495void LocationsBuilderX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
2496 LocationSummary* locations =
2497 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2498 locations->SetInAt(0, Location::RequiresRegister());
2499 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2500}
2501
2502void InstructionCodeGeneratorX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
2503 LocationSummary* locations = instruction->GetLocations();
2504 Register cls = locations->InAt(0).As<Register>();
2505 uint32_t offset = instruction->GetFieldOffset().Uint32Value();
2506
2507 switch (instruction->GetType()) {
2508 case Primitive::kPrimBoolean: {
2509 Register out = locations->Out().As<Register>();
2510 __ movzxb(out, Address(cls, offset));
2511 break;
2512 }
2513
2514 case Primitive::kPrimByte: {
2515 Register out = locations->Out().As<Register>();
2516 __ movsxb(out, Address(cls, offset));
2517 break;
2518 }
2519
2520 case Primitive::kPrimShort: {
2521 Register out = locations->Out().As<Register>();
2522 __ movsxw(out, Address(cls, offset));
2523 break;
2524 }
2525
2526 case Primitive::kPrimChar: {
2527 Register out = locations->Out().As<Register>();
2528 __ movzxw(out, Address(cls, offset));
2529 break;
2530 }
2531
2532 case Primitive::kPrimInt:
2533 case Primitive::kPrimNot: {
2534 Register out = locations->Out().As<Register>();
2535 __ movl(out, Address(cls, offset));
2536 break;
2537 }
2538
2539 case Primitive::kPrimLong: {
2540 // TODO: support volatile.
2541 __ movl(locations->Out().AsRegisterPairLow<Register>(), Address(cls, offset));
2542 __ movl(locations->Out().AsRegisterPairHigh<Register>(), Address(cls, kX86WordSize + offset));
2543 break;
2544 }
2545
2546 case Primitive::kPrimFloat:
2547 case Primitive::kPrimDouble:
2548 LOG(FATAL) << "Unimplemented register type " << instruction->GetType();
2549 UNREACHABLE();
2550 case Primitive::kPrimVoid:
2551 LOG(FATAL) << "Unreachable type " << instruction->GetType();
2552 UNREACHABLE();
2553 }
2554}
2555
2556void LocationsBuilderX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
2557 LocationSummary* locations =
2558 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2559 locations->SetInAt(0, Location::RequiresRegister());
2560 Primitive::Type field_type = instruction->GetFieldType();
2561 bool is_object_type = field_type == Primitive::kPrimNot;
2562 bool is_byte_type = (field_type == Primitive::kPrimBoolean)
2563 || (field_type == Primitive::kPrimByte);
2564 // The register allocator does not support multiple
2565 // inputs that die at entry with one in a specific register.
2566 if (is_byte_type) {
2567 // Ensure the value is in a byte register.
2568 locations->SetInAt(1, Location::RegisterLocation(EAX));
2569 } else {
2570 locations->SetInAt(1, Location::RequiresRegister());
2571 }
2572 // Temporary registers for the write barrier.
2573 if (is_object_type) {
2574 locations->AddTemp(Location::RequiresRegister());
2575 // Ensure the card is in a byte register.
2576 locations->AddTemp(Location::RegisterLocation(ECX));
2577 }
2578}
2579
2580void InstructionCodeGeneratorX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
2581 LocationSummary* locations = instruction->GetLocations();
2582 Register cls = locations->InAt(0).As<Register>();
2583 uint32_t offset = instruction->GetFieldOffset().Uint32Value();
2584 Primitive::Type field_type = instruction->GetFieldType();
2585
2586 switch (field_type) {
2587 case Primitive::kPrimBoolean:
2588 case Primitive::kPrimByte: {
2589 ByteRegister value = locations->InAt(1).As<ByteRegister>();
2590 __ movb(Address(cls, offset), value);
2591 break;
2592 }
2593
2594 case Primitive::kPrimShort:
2595 case Primitive::kPrimChar: {
2596 Register value = locations->InAt(1).As<Register>();
2597 __ movw(Address(cls, offset), value);
2598 break;
2599 }
2600
2601 case Primitive::kPrimInt:
2602 case Primitive::kPrimNot: {
2603 Register value = locations->InAt(1).As<Register>();
2604 __ movl(Address(cls, offset), value);
2605
2606 if (field_type == Primitive::kPrimNot) {
2607 Register temp = locations->GetTemp(0).As<Register>();
2608 Register card = locations->GetTemp(1).As<Register>();
2609 codegen_->MarkGCCard(temp, card, cls, value);
2610 }
2611 break;
2612 }
2613
2614 case Primitive::kPrimLong: {
2615 Location value = locations->InAt(1);
2616 __ movl(Address(cls, offset), value.AsRegisterPairLow<Register>());
2617 __ movl(Address(cls, kX86WordSize + offset), value.AsRegisterPairHigh<Register>());
2618 break;
2619 }
2620
2621 case Primitive::kPrimFloat:
2622 case Primitive::kPrimDouble:
2623 LOG(FATAL) << "Unimplemented register type " << field_type;
2624 UNREACHABLE();
2625 case Primitive::kPrimVoid:
2626 LOG(FATAL) << "Unreachable type " << field_type;
2627 UNREACHABLE();
2628 }
2629}
2630
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00002631void LocationsBuilderX86::VisitLoadString(HLoadString* load) {
2632 LocationSummary* locations =
2633 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kCallOnSlowPath);
2634 locations->SetOut(Location::RequiresRegister());
2635}
2636
2637void InstructionCodeGeneratorX86::VisitLoadString(HLoadString* load) {
2638 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathX86(load);
2639 codegen_->AddSlowPath(slow_path);
2640
2641 Register out = load->GetLocations()->Out().As<Register>();
2642 codegen_->LoadCurrentMethod(out);
2643 __ movl(out, Address(out, mirror::ArtMethod::DexCacheStringsOffset().Int32Value()));
2644 __ movl(out, Address(out, CodeGenerator::GetCacheOffset(load->GetStringIndex())));
2645 __ testl(out, out);
2646 __ j(kEqual, slow_path->GetEntryLabel());
2647 __ Bind(slow_path->GetExitLabel());
2648}
2649
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002650void LocationsBuilderX86::VisitLoadException(HLoadException* load) {
2651 LocationSummary* locations =
2652 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
2653 locations->SetOut(Location::RequiresRegister());
2654}
2655
2656void InstructionCodeGeneratorX86::VisitLoadException(HLoadException* load) {
2657 Address address = Address::Absolute(Thread::ExceptionOffset<kX86WordSize>().Int32Value());
2658 __ fs()->movl(load->GetLocations()->Out().As<Register>(), address);
2659 __ fs()->movl(address, Immediate(0));
2660}
2661
2662void LocationsBuilderX86::VisitThrow(HThrow* instruction) {
2663 LocationSummary* locations =
2664 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
2665 InvokeRuntimeCallingConvention calling_convention;
2666 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2667}
2668
2669void InstructionCodeGeneratorX86::VisitThrow(HThrow* instruction) {
2670 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pDeliverException)));
2671 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
2672}
2673
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002674} // namespace x86
2675} // namespace art