blob: b8518f1db90c137c2b1b281059c6c6d0a5b74ed9 [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 Geoffrayaf07bc12014-11-12 18:08:09 +000045// Marker for places that can be updated once we don't follow the quick ABI.
46static constexpr bool kFollowsQuickABI = true;
47
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +010048class InvokeRuntimeCallingConvention : public CallingConvention<Register, XmmRegister> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +010049 public:
50 InvokeRuntimeCallingConvention()
51 : CallingConvention(kRuntimeParameterCoreRegisters,
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +010052 kRuntimeParameterCoreRegistersLength,
53 kRuntimeParameterFpuRegisters,
54 kRuntimeParameterFpuRegistersLength) {}
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +010055
56 private:
57 DISALLOW_COPY_AND_ASSIGN(InvokeRuntimeCallingConvention);
58};
59
Nicolas Geoffraye5038322014-07-04 09:41:32 +010060#define __ reinterpret_cast<X86Assembler*>(codegen->GetAssembler())->
61
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +010062class SlowPathCodeX86 : public SlowPathCode {
63 public:
64 SlowPathCodeX86() : entry_label_(), exit_label_() {}
65
66 Label* GetEntryLabel() { return &entry_label_; }
67 Label* GetExitLabel() { return &exit_label_; }
68
69 private:
70 Label entry_label_;
71 Label exit_label_;
72
73 DISALLOW_COPY_AND_ASSIGN(SlowPathCodeX86);
74};
75
76class NullCheckSlowPathX86 : public SlowPathCodeX86 {
Nicolas Geoffraye5038322014-07-04 09:41:32 +010077 public:
Nicolas Geoffray39468442014-09-02 15:17:15 +010078 explicit NullCheckSlowPathX86(HNullCheck* instruction) : instruction_(instruction) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +010079
80 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
81 __ Bind(GetEntryLabel());
82 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pThrowNullPointer)));
Nicolas Geoffray39468442014-09-02 15:17:15 +010083 codegen->RecordPcInfo(instruction_, instruction_->GetDexPc());
Nicolas Geoffraye5038322014-07-04 09:41:32 +010084 }
85
86 private:
Nicolas Geoffray39468442014-09-02 15:17:15 +010087 HNullCheck* const instruction_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +010088 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathX86);
89};
90
Calin Juravled0d48522014-11-04 16:40:20 +000091class DivZeroCheckSlowPathX86 : public SlowPathCodeX86 {
92 public:
93 explicit DivZeroCheckSlowPathX86(HDivZeroCheck* instruction) : instruction_(instruction) {}
94
95 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
96 __ Bind(GetEntryLabel());
97 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pThrowDivZero)));
98 codegen->RecordPcInfo(instruction_, instruction_->GetDexPc());
99 }
100
101 private:
102 HDivZeroCheck* const instruction_;
103 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathX86);
104};
105
106class DivMinusOneSlowPathX86 : public SlowPathCodeX86 {
107 public:
108 explicit DivMinusOneSlowPathX86(Register reg) : reg_(reg) {}
109
110 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
111 __ Bind(GetEntryLabel());
112 __ negl(reg_);
113 __ jmp(GetExitLabel());
114 }
115
116 private:
117 Register reg_;
118 DISALLOW_COPY_AND_ASSIGN(DivMinusOneSlowPathX86);
119};
120
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100121class StackOverflowCheckSlowPathX86 : public SlowPathCodeX86 {
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100122 public:
123 StackOverflowCheckSlowPathX86() {}
124
125 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
126 __ Bind(GetEntryLabel());
127 __ addl(ESP,
128 Immediate(codegen->GetFrameSize() - kNumberOfPushedRegistersAtEntry * kX86WordSize));
129 __ fs()->jmp(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pThrowStackOverflow)));
130 }
131
132 private:
133 DISALLOW_COPY_AND_ASSIGN(StackOverflowCheckSlowPathX86);
134};
135
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100136class BoundsCheckSlowPathX86 : public SlowPathCodeX86 {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100137 public:
Roland Levillain5799fc02014-09-25 12:15:20 +0100138 BoundsCheckSlowPathX86(HBoundsCheck* instruction,
139 Location index_location,
140 Location length_location)
Nicolas Geoffray39468442014-09-02 15:17:15 +0100141 : instruction_(instruction), index_location_(index_location), length_location_(length_location) {}
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100142
143 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100144 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100145 __ Bind(GetEntryLabel());
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000146 // We're moving two locations to locations that could overlap, so we need a parallel
147 // move resolver.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100148 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000149 x86_codegen->EmitParallelMoves(
150 index_location_,
151 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
152 length_location_,
153 Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100154 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pThrowArrayBounds)));
Nicolas Geoffray39468442014-09-02 15:17:15 +0100155 codegen->RecordPcInfo(instruction_, instruction_->GetDexPc());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100156 }
157
158 private:
Nicolas Geoffray39468442014-09-02 15:17:15 +0100159 HBoundsCheck* const instruction_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100160 const Location index_location_;
161 const Location length_location_;
162
163 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathX86);
164};
165
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100166class SuspendCheckSlowPathX86 : public SlowPathCodeX86 {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000167 public:
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100168 explicit SuspendCheckSlowPathX86(HSuspendCheck* instruction, HBasicBlock* successor)
169 : instruction_(instruction), successor_(successor) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000170
171 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100172 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000173 __ Bind(GetEntryLabel());
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100174 codegen->SaveLiveRegisters(instruction_->GetLocations());
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000175 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pTestSuspend)));
176 codegen->RecordPcInfo(instruction_, instruction_->GetDexPc());
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100177 codegen->RestoreLiveRegisters(instruction_->GetLocations());
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100178 if (successor_ == nullptr) {
179 __ jmp(GetReturnLabel());
180 } else {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100181 __ jmp(x86_codegen->GetLabelOf(successor_));
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100182 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000183 }
184
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100185 Label* GetReturnLabel() {
186 DCHECK(successor_ == nullptr);
187 return &return_label_;
188 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000189
190 private:
191 HSuspendCheck* const instruction_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100192 HBasicBlock* const successor_;
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000193 Label return_label_;
194
195 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathX86);
196};
197
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000198class LoadStringSlowPathX86 : public SlowPathCodeX86 {
199 public:
200 explicit LoadStringSlowPathX86(HLoadString* instruction) : instruction_(instruction) {}
201
202 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
203 LocationSummary* locations = instruction_->GetLocations();
204 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
205
206 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
207 __ Bind(GetEntryLabel());
208 codegen->SaveLiveRegisters(locations);
209
210 InvokeRuntimeCallingConvention calling_convention;
211 x86_codegen->LoadCurrentMethod(calling_convention.GetRegisterAt(0));
212 __ movl(calling_convention.GetRegisterAt(1), Immediate(instruction_->GetStringIndex()));
213 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pResolveString)));
214 codegen->RecordPcInfo(instruction_, instruction_->GetDexPc());
215 x86_codegen->Move32(locations->Out(), Location::RegisterLocation(EAX));
216 codegen->RestoreLiveRegisters(locations);
217
218 __ jmp(GetExitLabel());
219 }
220
221 private:
222 HLoadString* const instruction_;
223
224 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathX86);
225};
226
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000227class LoadClassSlowPathX86 : public SlowPathCodeX86 {
228 public:
229 LoadClassSlowPathX86(HLoadClass* cls,
230 HInstruction* at,
231 uint32_t dex_pc,
232 bool do_clinit)
233 : cls_(cls), at_(at), dex_pc_(dex_pc), do_clinit_(do_clinit) {
234 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
235 }
236
237 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
238 LocationSummary* locations = at_->GetLocations();
239 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
240 __ Bind(GetEntryLabel());
241 codegen->SaveLiveRegisters(locations);
242
243 InvokeRuntimeCallingConvention calling_convention;
244 __ movl(calling_convention.GetRegisterAt(0), Immediate(cls_->GetTypeIndex()));
245 x86_codegen->LoadCurrentMethod(calling_convention.GetRegisterAt(1));
246 __ fs()->call(Address::Absolute(do_clinit_
247 ? QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pInitializeStaticStorage)
248 : QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pInitializeType)));
249 codegen->RecordPcInfo(at_, dex_pc_);
250
251 // Move the class to the desired location.
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000252 Location out = locations->Out();
253 if (out.IsValid()) {
254 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
255 x86_codegen->Move32(out, Location::RegisterLocation(EAX));
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000256 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000257
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000258 codegen->RestoreLiveRegisters(locations);
259 __ jmp(GetExitLabel());
260 }
261
262 private:
263 // The class this slow path will load.
264 HLoadClass* const cls_;
265
266 // The instruction where this slow path is happening.
267 // (Might be the load class or an initialization check).
268 HInstruction* const at_;
269
270 // The dex PC of `at_`.
271 const uint32_t dex_pc_;
272
273 // Whether to initialize the class.
274 const bool do_clinit_;
275
276 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathX86);
277};
278
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000279class TypeCheckSlowPathX86 : public SlowPathCodeX86 {
280 public:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000281 TypeCheckSlowPathX86(HInstruction* instruction,
282 Location class_to_check,
283 Location object_class,
284 uint32_t dex_pc)
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000285 : instruction_(instruction),
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000286 class_to_check_(class_to_check),
287 object_class_(object_class),
288 dex_pc_(dex_pc) {}
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000289
290 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
291 LocationSummary* locations = instruction_->GetLocations();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000292 DCHECK(instruction_->IsCheckCast()
293 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000294
295 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
296 __ Bind(GetEntryLabel());
297 codegen->SaveLiveRegisters(locations);
298
299 // We're moving two locations to locations that could overlap, so we need a parallel
300 // move resolver.
301 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000302 x86_codegen->EmitParallelMoves(
303 class_to_check_,
304 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
305 object_class_,
306 Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000307
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000308 if (instruction_->IsInstanceOf()) {
309 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pInstanceofNonTrivial)));
310 } else {
311 DCHECK(instruction_->IsCheckCast());
312 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pCheckCast)));
313 }
314
315 codegen->RecordPcInfo(instruction_, dex_pc_);
316 if (instruction_->IsInstanceOf()) {
317 x86_codegen->Move32(locations->Out(), Location::RegisterLocation(EAX));
318 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000319 codegen->RestoreLiveRegisters(locations);
320
321 __ jmp(GetExitLabel());
322 }
323
324 private:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000325 HInstruction* const instruction_;
326 const Location class_to_check_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000327 const Location object_class_;
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000328 const uint32_t dex_pc_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000329
330 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathX86);
331};
332
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100333#undef __
334#define __ reinterpret_cast<X86Assembler*>(GetAssembler())->
335
Dave Allison20dfc792014-06-16 20:44:29 -0700336inline Condition X86Condition(IfCondition cond) {
337 switch (cond) {
338 case kCondEQ: return kEqual;
339 case kCondNE: return kNotEqual;
340 case kCondLT: return kLess;
341 case kCondLE: return kLessEqual;
342 case kCondGT: return kGreater;
343 case kCondGE: return kGreaterEqual;
344 default:
345 LOG(FATAL) << "Unknown if condition";
346 }
347 return kEqual;
348}
349
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100350void CodeGeneratorX86::DumpCoreRegister(std::ostream& stream, int reg) const {
351 stream << X86ManagedRegister::FromCpuRegister(Register(reg));
352}
353
354void CodeGeneratorX86::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
355 stream << X86ManagedRegister::FromXmmRegister(XmmRegister(reg));
356}
357
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100358size_t CodeGeneratorX86::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
359 __ movl(Address(ESP, stack_index), static_cast<Register>(reg_id));
360 return kX86WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100361}
362
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100363size_t CodeGeneratorX86::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
364 __ movl(static_cast<Register>(reg_id), Address(ESP, stack_index));
365 return kX86WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100366}
367
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100368CodeGeneratorX86::CodeGeneratorX86(HGraph* graph)
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100369 : CodeGenerator(graph, kNumberOfCpuRegisters, kNumberOfXmmRegisters, kNumberOfRegisterPairs),
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100370 block_labels_(graph->GetArena(), 0),
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100371 location_builder_(graph, this),
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100372 instruction_visitor_(graph, this),
373 move_resolver_(graph->GetArena(), this) {}
374
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100375size_t CodeGeneratorX86::FrameEntrySpillSize() const {
376 return kNumberOfPushedRegistersAtEntry * kX86WordSize;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100377}
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100378
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100379Location CodeGeneratorX86::AllocateFreeRegister(Primitive::Type type) const {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100380 switch (type) {
381 case Primitive::kPrimLong: {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100382 size_t reg = FindFreeEntry(blocked_register_pairs_, kNumberOfRegisterPairs);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100383 X86ManagedRegister pair =
384 X86ManagedRegister::FromRegisterPair(static_cast<RegisterPair>(reg));
Calin Juravle34bacdf2014-10-07 20:23:36 +0100385 DCHECK(!blocked_core_registers_[pair.AsRegisterPairLow()]);
386 DCHECK(!blocked_core_registers_[pair.AsRegisterPairHigh()]);
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100387 blocked_core_registers_[pair.AsRegisterPairLow()] = true;
388 blocked_core_registers_[pair.AsRegisterPairHigh()] = true;
Calin Juravle34bacdf2014-10-07 20:23:36 +0100389 UpdateBlockedPairRegisters();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100390 return Location::RegisterPairLocation(pair.AsRegisterPairLow(), pair.AsRegisterPairHigh());
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100391 }
392
393 case Primitive::kPrimByte:
394 case Primitive::kPrimBoolean:
395 case Primitive::kPrimChar:
396 case Primitive::kPrimShort:
397 case Primitive::kPrimInt:
398 case Primitive::kPrimNot: {
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100399 Register reg = static_cast<Register>(
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100400 FindFreeEntry(blocked_core_registers_, kNumberOfCpuRegisters));
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100401 // Block all register pairs that contain `reg`.
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100402 for (int i = 0; i < kNumberOfRegisterPairs; i++) {
403 X86ManagedRegister current =
404 X86ManagedRegister::FromRegisterPair(static_cast<RegisterPair>(i));
405 if (current.AsRegisterPairLow() == reg || current.AsRegisterPairHigh() == reg) {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100406 blocked_register_pairs_[i] = true;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100407 }
408 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100409 return Location::RegisterLocation(reg);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100410 }
411
412 case Primitive::kPrimFloat:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100413 case Primitive::kPrimDouble: {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100414 return Location::FpuRegisterLocation(
415 FindFreeEntry(blocked_fpu_registers_, kNumberOfXmmRegisters));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100416 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100417
418 case Primitive::kPrimVoid:
419 LOG(FATAL) << "Unreachable type " << type;
420 }
421
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100422 return Location();
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100423}
424
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100425void CodeGeneratorX86::SetupBlockedRegisters() const {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100426 // Don't allocate the dalvik style register pair passing.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100427 blocked_register_pairs_[ECX_EDX] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100428
429 // Stack register is always reserved.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100430 blocked_core_registers_[ESP] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100431
432 // TODO: We currently don't use Quick's callee saved registers.
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +0000433 DCHECK(kFollowsQuickABI);
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100434 blocked_core_registers_[EBP] = true;
435 blocked_core_registers_[ESI] = true;
436 blocked_core_registers_[EDI] = true;
Calin Juravle34bacdf2014-10-07 20:23:36 +0100437
438 UpdateBlockedPairRegisters();
439}
440
441void CodeGeneratorX86::UpdateBlockedPairRegisters() const {
442 for (int i = 0; i < kNumberOfRegisterPairs; i++) {
443 X86ManagedRegister current =
444 X86ManagedRegister::FromRegisterPair(static_cast<RegisterPair>(i));
445 if (blocked_core_registers_[current.AsRegisterPairLow()]
446 || blocked_core_registers_[current.AsRegisterPairHigh()]) {
447 blocked_register_pairs_[i] = true;
448 }
449 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100450}
451
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100452InstructionCodeGeneratorX86::InstructionCodeGeneratorX86(HGraph* graph, CodeGeneratorX86* codegen)
453 : HGraphVisitor(graph),
454 assembler_(codegen->GetAssembler()),
455 codegen_(codegen) {}
456
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000457void CodeGeneratorX86::GenerateFrameEntry() {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000458 // Create a fake register to mimic Quick.
459 static const int kFakeReturnRegister = 8;
460 core_spill_mask_ |= (1 << kFakeReturnRegister);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000461
Dave Allison648d7112014-07-25 16:15:27 -0700462 bool skip_overflow_check = IsLeafMethod() && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kX86);
Nicolas Geoffray397f2e42014-07-23 12:57:19 +0100463 if (!skip_overflow_check && !kExplicitStackOverflowCheck) {
464 __ testl(EAX, Address(ESP, -static_cast<int32_t>(GetStackOverflowReservedBytes(kX86))));
Nicolas Geoffray39468442014-09-02 15:17:15 +0100465 RecordPcInfo(nullptr, 0);
Nicolas Geoffray397f2e42014-07-23 12:57:19 +0100466 }
467
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100468 // The return PC has already been pushed on the stack.
Nicolas Geoffray707c8092014-04-04 10:50:14 +0100469 __ subl(ESP, Immediate(GetFrameSize() - kNumberOfPushedRegistersAtEntry * kX86WordSize));
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100470
Nicolas Geoffray397f2e42014-07-23 12:57:19 +0100471 if (!skip_overflow_check && kExplicitStackOverflowCheck) {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100472 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) StackOverflowCheckSlowPathX86();
Nicolas Geoffray397f2e42014-07-23 12:57:19 +0100473 AddSlowPath(slow_path);
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100474
Nicolas Geoffray397f2e42014-07-23 12:57:19 +0100475 __ fs()->cmpl(ESP, Address::Absolute(Thread::StackEndOffset<kX86WordSize>()));
476 __ j(kLess, slow_path->GetEntryLabel());
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100477 }
478
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100479 __ movl(Address(ESP, kCurrentMethodStackOffset), EAX);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000480}
481
482void CodeGeneratorX86::GenerateFrameExit() {
Nicolas Geoffray707c8092014-04-04 10:50:14 +0100483 __ addl(ESP, Immediate(GetFrameSize() - kNumberOfPushedRegistersAtEntry * kX86WordSize));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000484}
485
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100486void CodeGeneratorX86::Bind(HBasicBlock* block) {
487 __ Bind(GetLabelOf(block));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000488}
489
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100490void CodeGeneratorX86::LoadCurrentMethod(Register reg) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100491 __ movl(reg, Address(ESP, kCurrentMethodStackOffset));
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000492}
493
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100494Location CodeGeneratorX86::GetStackLocation(HLoadLocal* load) const {
495 switch (load->GetType()) {
496 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100497 case Primitive::kPrimDouble:
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100498 return Location::DoubleStackSlot(GetStackSlot(load->GetLocal()));
499 break;
500
501 case Primitive::kPrimInt:
502 case Primitive::kPrimNot:
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100503 case Primitive::kPrimFloat:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100504 return Location::StackSlot(GetStackSlot(load->GetLocal()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100505
506 case Primitive::kPrimBoolean:
507 case Primitive::kPrimByte:
508 case Primitive::kPrimChar:
509 case Primitive::kPrimShort:
510 case Primitive::kPrimVoid:
511 LOG(FATAL) << "Unexpected type " << load->GetType();
512 }
513
514 LOG(FATAL) << "Unreachable";
515 return Location();
516}
517
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100518Location InvokeDexCallingConventionVisitor::GetNextLocation(Primitive::Type type) {
519 switch (type) {
520 case Primitive::kPrimBoolean:
521 case Primitive::kPrimByte:
522 case Primitive::kPrimChar:
523 case Primitive::kPrimShort:
524 case Primitive::kPrimInt:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100525 case Primitive::kPrimFloat:
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100526 case Primitive::kPrimNot: {
527 uint32_t index = gp_index_++;
528 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100529 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100530 } else {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100531 return Location::StackSlot(calling_convention.GetStackOffsetOf(index));
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100532 }
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100533 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100534
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100535 case Primitive::kPrimLong:
536 case Primitive::kPrimDouble: {
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100537 uint32_t index = gp_index_;
538 gp_index_ += 2;
539 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100540 X86ManagedRegister pair = X86ManagedRegister::FromRegisterPair(
541 calling_convention.GetRegisterPairAt(index));
542 return Location::RegisterPairLocation(pair.AsRegisterPairLow(), pair.AsRegisterPairHigh());
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100543 } else if (index + 1 == calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray0a6c4592014-10-30 16:37:57 +0000544 // On X86, the register index and stack index of a quick parameter is the same, since
545 // we are passing floating pointer values in core registers.
546 return Location::QuickParameter(index, index);
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100547 } else {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100548 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(index));
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100549 }
550 }
551
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100552 case Primitive::kPrimVoid:
553 LOG(FATAL) << "Unexpected parameter type " << type;
554 break;
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100555 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100556 return Location();
557}
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100558
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100559void CodeGeneratorX86::Move32(Location destination, Location source) {
560 if (source.Equals(destination)) {
561 return;
562 }
563 if (destination.IsRegister()) {
564 if (source.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100565 __ movl(destination.As<Register>(), source.As<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100566 } else if (source.IsFpuRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100567 __ movd(destination.As<Register>(), source.As<XmmRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100568 } else {
569 DCHECK(source.IsStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100570 __ movl(destination.As<Register>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100571 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100572 } else if (destination.IsFpuRegister()) {
573 if (source.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100574 __ movd(destination.As<XmmRegister>(), source.As<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100575 } else if (source.IsFpuRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100576 __ movaps(destination.As<XmmRegister>(), source.As<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100577 } else {
578 DCHECK(source.IsStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100579 __ movss(destination.As<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100580 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100581 } else {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100582 DCHECK(destination.IsStackSlot());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100583 if (source.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100584 __ movl(Address(ESP, destination.GetStackIndex()), source.As<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100585 } else if (source.IsFpuRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100586 __ movss(Address(ESP, destination.GetStackIndex()), source.As<XmmRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100587 } else {
588 DCHECK(source.IsStackSlot());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100589 __ pushl(Address(ESP, source.GetStackIndex()));
590 __ popl(Address(ESP, destination.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100591 }
592 }
593}
594
595void CodeGeneratorX86::Move64(Location destination, Location source) {
596 if (source.Equals(destination)) {
597 return;
598 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100599 if (destination.IsRegisterPair()) {
600 if (source.IsRegisterPair()) {
601 __ movl(destination.AsRegisterPairLow<Register>(), source.AsRegisterPairLow<Register>());
602 __ movl(destination.AsRegisterPairHigh<Register>(), source.AsRegisterPairHigh<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100603 } else if (source.IsFpuRegister()) {
604 LOG(FATAL) << "Unimplemented";
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100605 } else if (source.IsQuickParameter()) {
Nicolas Geoffray0a6c4592014-10-30 16:37:57 +0000606 uint16_t register_index = source.GetQuickParameterRegisterIndex();
607 uint16_t stack_index = source.GetQuickParameterStackIndex();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100608 InvokeDexCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100609 __ movl(destination.AsRegisterPairLow<Register>(),
Nicolas Geoffray0a6c4592014-10-30 16:37:57 +0000610 calling_convention.GetRegisterAt(register_index));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100611 __ movl(destination.AsRegisterPairHigh<Register>(), Address(ESP,
Nicolas Geoffray0a6c4592014-10-30 16:37:57 +0000612 calling_convention.GetStackOffsetOf(stack_index + 1) + GetFrameSize()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100613 } else {
614 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100615 __ movl(destination.AsRegisterPairLow<Register>(), Address(ESP, source.GetStackIndex()));
616 __ movl(destination.AsRegisterPairHigh<Register>(),
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100617 Address(ESP, source.GetHighStackIndex(kX86WordSize)));
618 }
619 } else if (destination.IsQuickParameter()) {
620 InvokeDexCallingConvention calling_convention;
Nicolas Geoffray0a6c4592014-10-30 16:37:57 +0000621 uint16_t register_index = destination.GetQuickParameterRegisterIndex();
622 uint16_t stack_index = destination.GetQuickParameterStackIndex();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100623 if (source.IsRegister()) {
Nicolas Geoffray0a6c4592014-10-30 16:37:57 +0000624 __ movl(calling_convention.GetRegisterAt(register_index), source.AsRegisterPairLow<Register>());
625 __ movl(Address(ESP, calling_convention.GetStackOffsetOf(stack_index + 1)),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100626 source.AsRegisterPairHigh<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100627 } else if (source.IsFpuRegister()) {
628 LOG(FATAL) << "Unimplemented";
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100629 } else {
630 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray0a6c4592014-10-30 16:37:57 +0000631 __ movl(calling_convention.GetRegisterAt(register_index),
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100632 Address(ESP, source.GetStackIndex()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100633 __ pushl(Address(ESP, source.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray0a6c4592014-10-30 16:37:57 +0000634 __ popl(Address(ESP, calling_convention.GetStackOffsetOf(stack_index + 1)));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100635 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100636 } else if (destination.IsFpuRegister()) {
637 if (source.IsDoubleStackSlot()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100638 __ movsd(destination.As<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100639 } else {
640 LOG(FATAL) << "Unimplemented";
641 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100642 } else {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100643 DCHECK(destination.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100644 if (source.IsRegisterPair()) {
645 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegisterPairLow<Register>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100646 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100647 source.AsRegisterPairHigh<Register>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100648 } else if (source.IsQuickParameter()) {
649 InvokeDexCallingConvention calling_convention;
Nicolas Geoffray0a6c4592014-10-30 16:37:57 +0000650 uint16_t register_index = source.GetQuickParameterRegisterIndex();
651 uint16_t stack_index = source.GetQuickParameterStackIndex();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100652 __ movl(Address(ESP, destination.GetStackIndex()),
Nicolas Geoffray0a6c4592014-10-30 16:37:57 +0000653 calling_convention.GetRegisterAt(register_index));
654 DCHECK_EQ(calling_convention.GetStackOffsetOf(stack_index + 1) + GetFrameSize(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100655 static_cast<size_t>(destination.GetHighStackIndex(kX86WordSize)));
656 } else if (source.IsFpuRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100657 __ movsd(Address(ESP, destination.GetStackIndex()), source.As<XmmRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100658 } else {
659 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100660 __ pushl(Address(ESP, source.GetStackIndex()));
661 __ popl(Address(ESP, destination.GetStackIndex()));
662 __ pushl(Address(ESP, source.GetHighStackIndex(kX86WordSize)));
663 __ popl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100664 }
665 }
666}
667
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100668void CodeGeneratorX86::Move(HInstruction* instruction, Location location, HInstruction* move_for) {
Roland Levillain476df552014-10-09 17:51:36 +0100669 if (instruction->IsIntConstant()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100670 Immediate imm(instruction->AsIntConstant()->GetValue());
671 if (location.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100672 __ movl(location.As<Register>(), imm);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +0100673 } else if (location.IsStackSlot()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100674 __ movl(Address(ESP, location.GetStackIndex()), imm);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +0100675 } else {
676 DCHECK(location.IsConstant());
677 DCHECK_EQ(location.GetConstant(), instruction);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100678 }
Roland Levillain476df552014-10-09 17:51:36 +0100679 } else if (instruction->IsLongConstant()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100680 int64_t value = instruction->AsLongConstant()->GetValue();
681 if (location.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100682 __ movl(location.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
683 __ movl(location.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +0100684 } else if (location.IsDoubleStackSlot()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100685 __ movl(Address(ESP, location.GetStackIndex()), Immediate(Low32Bits(value)));
686 __ movl(Address(ESP, location.GetHighStackIndex(kX86WordSize)), Immediate(High32Bits(value)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +0100687 } else {
688 DCHECK(location.IsConstant());
689 DCHECK_EQ(location.GetConstant(), instruction);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100690 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +0000691 } else if (instruction->IsTemporary()) {
692 Location temp_location = GetTemporaryLocation(instruction->AsTemporary());
693 Move32(location, temp_location);
Roland Levillain476df552014-10-09 17:51:36 +0100694 } else if (instruction->IsLoadLocal()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100695 int slot = GetStackSlot(instruction->AsLoadLocal()->GetLocal());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100696 switch (instruction->GetType()) {
697 case Primitive::kPrimBoolean:
698 case Primitive::kPrimByte:
699 case Primitive::kPrimChar:
700 case Primitive::kPrimShort:
701 case Primitive::kPrimInt:
702 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100703 case Primitive::kPrimFloat:
704 Move32(location, Location::StackSlot(slot));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100705 break;
706
707 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100708 case Primitive::kPrimDouble:
709 Move64(location, Location::DoubleStackSlot(slot));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100710 break;
711
712 default:
713 LOG(FATAL) << "Unimplemented local type " << instruction->GetType();
714 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000715 } else {
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100716 DCHECK((instruction->GetNext() == move_for) || instruction->GetNext()->IsTemporary());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100717 switch (instruction->GetType()) {
718 case Primitive::kPrimBoolean:
719 case Primitive::kPrimByte:
720 case Primitive::kPrimChar:
721 case Primitive::kPrimShort:
722 case Primitive::kPrimInt:
723 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100724 case Primitive::kPrimFloat:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100725 Move32(location, instruction->GetLocations()->Out());
726 break;
727
728 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100729 case Primitive::kPrimDouble:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100730 Move64(location, instruction->GetLocations()->Out());
731 break;
732
733 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100734 LOG(FATAL) << "Unexpected type " << instruction->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100735 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000736 }
737}
738
739void LocationsBuilderX86::VisitGoto(HGoto* got) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000740 got->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000741}
742
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000743void InstructionCodeGeneratorX86::VisitGoto(HGoto* got) {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000744 HBasicBlock* successor = got->GetSuccessor();
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100745 DCHECK(!successor->IsExitBlock());
746
747 HBasicBlock* block = got->GetBlock();
748 HInstruction* previous = got->GetPrevious();
749
750 HLoopInformation* info = block->GetLoopInformation();
751 if (info != nullptr && info->IsBackEdge(block) && info->HasSuspendCheck()) {
752 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
753 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
754 return;
755 }
756
757 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
758 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
759 }
760 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000761 __ jmp(codegen_->GetLabelOf(successor));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000762 }
763}
764
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000765void LocationsBuilderX86::VisitExit(HExit* exit) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000766 exit->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000767}
768
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000769void InstructionCodeGeneratorX86::VisitExit(HExit* exit) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700770 UNUSED(exit);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000771 if (kIsDebugBuild) {
772 __ Comment("Unreachable");
773 __ int3();
774 }
775}
776
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000777void LocationsBuilderX86::VisitIf(HIf* if_instr) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100778 LocationSummary* locations =
779 new (GetGraph()->GetArena()) LocationSummary(if_instr, LocationSummary::kNoCall);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100780 HInstruction* cond = if_instr->InputAt(0);
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100781 if (!cond->IsCondition() || cond->AsCondition()->NeedsMaterialization()) {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +0100782 locations->SetInAt(0, Location::Any());
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100783 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000784}
785
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000786void InstructionCodeGeneratorX86::VisitIf(HIf* if_instr) {
Dave Allison20dfc792014-06-16 20:44:29 -0700787 HInstruction* cond = if_instr->InputAt(0);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100788 if (cond->IsIntConstant()) {
789 // Constant condition, statically compared against 1.
790 int32_t cond_value = cond->AsIntConstant()->GetValue();
791 if (cond_value == 1) {
792 if (!codegen_->GoesToNextBlock(if_instr->GetBlock(),
793 if_instr->IfTrueSuccessor())) {
794 __ jmp(codegen_->GetLabelOf(if_instr->IfTrueSuccessor()));
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100795 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100796 return;
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100797 } else {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100798 DCHECK_EQ(cond_value, 0);
799 }
800 } else {
801 bool materialized =
802 !cond->IsCondition() || cond->AsCondition()->NeedsMaterialization();
803 // Moves do not affect the eflags register, so if the condition is
804 // evaluated just before the if, we don't need to evaluate it
805 // again.
806 bool eflags_set = cond->IsCondition()
807 && cond->AsCondition()->IsBeforeWhenDisregardMoves(if_instr);
808 if (materialized) {
809 if (!eflags_set) {
810 // Materialized condition, compare against 0.
811 Location lhs = if_instr->GetLocations()->InAt(0);
812 if (lhs.IsRegister()) {
813 __ cmpl(lhs.As<Register>(), Immediate(0));
814 } else {
815 __ cmpl(Address(ESP, lhs.GetStackIndex()), Immediate(0));
816 }
817 __ j(kNotEqual, codegen_->GetLabelOf(if_instr->IfTrueSuccessor()));
818 } else {
819 __ j(X86Condition(cond->AsCondition()->GetCondition()),
820 codegen_->GetLabelOf(if_instr->IfTrueSuccessor()));
821 }
822 } else {
823 Location lhs = cond->GetLocations()->InAt(0);
824 Location rhs = cond->GetLocations()->InAt(1);
825 // LHS is guaranteed to be in a register (see
826 // LocationsBuilderX86::VisitCondition).
827 if (rhs.IsRegister()) {
828 __ cmpl(lhs.As<Register>(), rhs.As<Register>());
829 } else if (rhs.IsConstant()) {
830 HIntConstant* instruction = rhs.GetConstant()->AsIntConstant();
831 Immediate imm(instruction->AsIntConstant()->GetValue());
832 __ cmpl(lhs.As<Register>(), imm);
833 } else {
834 __ cmpl(lhs.As<Register>(), Address(ESP, rhs.GetStackIndex()));
835 }
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100836 __ j(X86Condition(cond->AsCondition()->GetCondition()),
837 codegen_->GetLabelOf(if_instr->IfTrueSuccessor()));
Dave Allison20dfc792014-06-16 20:44:29 -0700838 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100839 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100840 if (!codegen_->GoesToNextBlock(if_instr->GetBlock(),
841 if_instr->IfFalseSuccessor())) {
Dave Allison20dfc792014-06-16 20:44:29 -0700842 __ jmp(codegen_->GetLabelOf(if_instr->IfFalseSuccessor()));
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000843 }
844}
845
846void LocationsBuilderX86::VisitLocal(HLocal* local) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000847 local->SetLocations(nullptr);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000848}
849
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000850void InstructionCodeGeneratorX86::VisitLocal(HLocal* local) {
851 DCHECK_EQ(local->GetBlock(), GetGraph()->GetEntryBlock());
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000852}
853
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000854void LocationsBuilderX86::VisitLoadLocal(HLoadLocal* local) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100855 local->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000856}
857
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000858void InstructionCodeGeneratorX86::VisitLoadLocal(HLoadLocal* load) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100859 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700860 UNUSED(load);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000861}
862
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100863void LocationsBuilderX86::VisitStoreLocal(HStoreLocal* store) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100864 LocationSummary* locations =
865 new (GetGraph()->GetArena()) LocationSummary(store, LocationSummary::kNoCall);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100866 switch (store->InputAt(1)->GetType()) {
867 case Primitive::kPrimBoolean:
868 case Primitive::kPrimByte:
869 case Primitive::kPrimChar:
870 case Primitive::kPrimShort:
871 case Primitive::kPrimInt:
872 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100873 case Primitive::kPrimFloat:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100874 locations->SetInAt(1, Location::StackSlot(codegen_->GetStackSlot(store->GetLocal())));
875 break;
876
877 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100878 case Primitive::kPrimDouble:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100879 locations->SetInAt(1, Location::DoubleStackSlot(codegen_->GetStackSlot(store->GetLocal())));
880 break;
881
882 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100883 LOG(FATAL) << "Unknown local type " << store->InputAt(1)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100884 }
885 store->SetLocations(locations);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000886}
887
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000888void InstructionCodeGeneratorX86::VisitStoreLocal(HStoreLocal* store) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700889 UNUSED(store);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000890}
891
Dave Allison20dfc792014-06-16 20:44:29 -0700892void LocationsBuilderX86::VisitCondition(HCondition* comp) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100893 LocationSummary* locations =
894 new (GetGraph()->GetArena()) LocationSummary(comp, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +0100895 locations->SetInAt(0, Location::RequiresRegister());
896 locations->SetInAt(1, Location::Any());
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100897 if (comp->NeedsMaterialization()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100898 locations->SetOut(Location::RequiresRegister());
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100899 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000900}
901
Dave Allison20dfc792014-06-16 20:44:29 -0700902void InstructionCodeGeneratorX86::VisitCondition(HCondition* comp) {
903 if (comp->NeedsMaterialization()) {
904 LocationSummary* locations = comp->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100905 Register reg = locations->Out().As<Register>();
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100906 // Clear register: setcc only sets the low byte.
907 __ xorl(reg, reg);
Dave Allison20dfc792014-06-16 20:44:29 -0700908 if (locations->InAt(1).IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100909 __ cmpl(locations->InAt(0).As<Register>(),
910 locations->InAt(1).As<Register>());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100911 } else if (locations->InAt(1).IsConstant()) {
912 HConstant* instruction = locations->InAt(1).GetConstant();
913 Immediate imm(instruction->AsIntConstant()->GetValue());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100914 __ cmpl(locations->InAt(0).As<Register>(), imm);
Dave Allison20dfc792014-06-16 20:44:29 -0700915 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100916 __ cmpl(locations->InAt(0).As<Register>(),
Dave Allison20dfc792014-06-16 20:44:29 -0700917 Address(ESP, locations->InAt(1).GetStackIndex()));
918 }
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100919 __ setb(X86Condition(comp->GetCondition()), reg);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100920 }
Dave Allison20dfc792014-06-16 20:44:29 -0700921}
922
923void LocationsBuilderX86::VisitEqual(HEqual* comp) {
924 VisitCondition(comp);
925}
926
927void InstructionCodeGeneratorX86::VisitEqual(HEqual* comp) {
928 VisitCondition(comp);
929}
930
931void LocationsBuilderX86::VisitNotEqual(HNotEqual* comp) {
932 VisitCondition(comp);
933}
934
935void InstructionCodeGeneratorX86::VisitNotEqual(HNotEqual* comp) {
936 VisitCondition(comp);
937}
938
939void LocationsBuilderX86::VisitLessThan(HLessThan* comp) {
940 VisitCondition(comp);
941}
942
943void InstructionCodeGeneratorX86::VisitLessThan(HLessThan* comp) {
944 VisitCondition(comp);
945}
946
947void LocationsBuilderX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
948 VisitCondition(comp);
949}
950
951void InstructionCodeGeneratorX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
952 VisitCondition(comp);
953}
954
955void LocationsBuilderX86::VisitGreaterThan(HGreaterThan* comp) {
956 VisitCondition(comp);
957}
958
959void InstructionCodeGeneratorX86::VisitGreaterThan(HGreaterThan* comp) {
960 VisitCondition(comp);
961}
962
963void LocationsBuilderX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
964 VisitCondition(comp);
965}
966
967void InstructionCodeGeneratorX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
968 VisitCondition(comp);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000969}
970
971void LocationsBuilderX86::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100972 LocationSummary* locations =
973 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100974 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000975}
976
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000977void InstructionCodeGeneratorX86::VisitIntConstant(HIntConstant* constant) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100978 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700979 UNUSED(constant);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000980}
981
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100982void LocationsBuilderX86::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100983 LocationSummary* locations =
984 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100985 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100986}
987
988void InstructionCodeGeneratorX86::VisitLongConstant(HLongConstant* constant) {
989 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700990 UNUSED(constant);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100991}
992
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100993void LocationsBuilderX86::VisitFloatConstant(HFloatConstant* constant) {
994 LocationSummary* locations =
995 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
996 locations->SetOut(Location::ConstantLocation(constant));
997}
998
999void InstructionCodeGeneratorX86::VisitFloatConstant(HFloatConstant* constant) {
1000 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001001 UNUSED(constant);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001002}
1003
1004void LocationsBuilderX86::VisitDoubleConstant(HDoubleConstant* constant) {
1005 LocationSummary* locations =
1006 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1007 locations->SetOut(Location::ConstantLocation(constant));
1008}
1009
1010void InstructionCodeGeneratorX86::VisitDoubleConstant(HDoubleConstant* constant) {
1011 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001012 UNUSED(constant);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001013}
1014
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001015void LocationsBuilderX86::VisitReturnVoid(HReturnVoid* ret) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001016 ret->SetLocations(nullptr);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001017}
1018
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001019void InstructionCodeGeneratorX86::VisitReturnVoid(HReturnVoid* ret) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001020 UNUSED(ret);
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001021 codegen_->GenerateFrameExit();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001022 __ ret();
1023}
1024
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001025void LocationsBuilderX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001026 LocationSummary* locations =
1027 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001028 switch (ret->InputAt(0)->GetType()) {
1029 case Primitive::kPrimBoolean:
1030 case Primitive::kPrimByte:
1031 case Primitive::kPrimChar:
1032 case Primitive::kPrimShort:
1033 case Primitive::kPrimInt:
1034 case Primitive::kPrimNot:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001035 locations->SetInAt(0, Location::RegisterLocation(EAX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001036 break;
1037
1038 case Primitive::kPrimLong:
1039 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001040 0, Location::RegisterPairLocation(EAX, EDX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001041 break;
1042
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001043 case Primitive::kPrimFloat:
1044 case Primitive::kPrimDouble:
1045 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001046 0, Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001047 break;
1048
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001049 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001050 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001051 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001052}
1053
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001054void InstructionCodeGeneratorX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001055 if (kIsDebugBuild) {
1056 switch (ret->InputAt(0)->GetType()) {
1057 case Primitive::kPrimBoolean:
1058 case Primitive::kPrimByte:
1059 case Primitive::kPrimChar:
1060 case Primitive::kPrimShort:
1061 case Primitive::kPrimInt:
1062 case Primitive::kPrimNot:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001063 DCHECK_EQ(ret->GetLocations()->InAt(0).As<Register>(), EAX);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001064 break;
1065
1066 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001067 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairLow<Register>(), EAX);
1068 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairHigh<Register>(), EDX);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001069 break;
1070
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001071 case Primitive::kPrimFloat:
1072 case Primitive::kPrimDouble:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001073 DCHECK_EQ(ret->GetLocations()->InAt(0).As<XmmRegister>(), XMM0);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001074 break;
1075
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001076 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001077 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001078 }
1079 }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001080 codegen_->GenerateFrameExit();
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001081 __ ret();
1082}
1083
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001084void LocationsBuilderX86::VisitInvokeStatic(HInvokeStatic* invoke) {
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001085 HandleInvoke(invoke);
1086}
1087
1088void InstructionCodeGeneratorX86::VisitInvokeStatic(HInvokeStatic* invoke) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001089 Register temp = invoke->GetLocations()->GetTemp(0).As<Register>();
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001090
1091 // TODO: Implement all kinds of calls:
1092 // 1) boot -> boot
1093 // 2) app -> boot
1094 // 3) app -> app
1095 //
1096 // Currently we implement the app -> app logic, which looks up in the resolve cache.
1097
1098 // temp = method;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001099 codegen_->LoadCurrentMethod(temp);
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001100 // temp = temp->dex_cache_resolved_methods_;
1101 __ movl(temp, Address(temp, mirror::ArtMethod::DexCacheResolvedMethodsOffset().Int32Value()));
1102 // temp = temp[index_in_cache]
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001103 __ movl(temp, Address(temp, CodeGenerator::GetCacheOffset(invoke->GetIndexInDexCache())));
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001104 // (temp + offset_of_quick_compiled_code)()
1105 __ call(Address(temp, mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset().Int32Value()));
1106
1107 DCHECK(!codegen_->IsLeafMethod());
1108 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
1109}
1110
1111void LocationsBuilderX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
1112 HandleInvoke(invoke);
1113}
1114
1115void LocationsBuilderX86::HandleInvoke(HInvoke* invoke) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001116 LocationSummary* locations =
1117 new (GetGraph()->GetArena()) LocationSummary(invoke, LocationSummary::kCall);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001118 locations->AddTemp(Location::RegisterLocation(EAX));
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001119
1120 InvokeDexCallingConventionVisitor calling_convention_visitor;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001121 for (size_t i = 0; i < invoke->InputCount(); i++) {
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001122 HInstruction* input = invoke->InputAt(i);
1123 locations->SetInAt(i, calling_convention_visitor.GetNextLocation(input->GetType()));
1124 }
1125
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001126 switch (invoke->GetType()) {
1127 case Primitive::kPrimBoolean:
1128 case Primitive::kPrimByte:
1129 case Primitive::kPrimChar:
1130 case Primitive::kPrimShort:
1131 case Primitive::kPrimInt:
1132 case Primitive::kPrimNot:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001133 locations->SetOut(Location::RegisterLocation(EAX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001134 break;
1135
1136 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001137 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001138 break;
1139
1140 case Primitive::kPrimVoid:
1141 break;
1142
1143 case Primitive::kPrimDouble:
1144 case Primitive::kPrimFloat:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001145 locations->SetOut(Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001146 break;
1147 }
1148
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001149 invoke->SetLocations(locations);
1150}
1151
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001152void InstructionCodeGeneratorX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001153 Register temp = invoke->GetLocations()->GetTemp(0).As<Register>();
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001154 uint32_t method_offset = mirror::Class::EmbeddedVTableOffset().Uint32Value() +
1155 invoke->GetVTableIndex() * sizeof(mirror::Class::VTableEntry);
1156 LocationSummary* locations = invoke->GetLocations();
1157 Location receiver = locations->InAt(0);
1158 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
1159 // temp = object->GetClass();
1160 if (receiver.IsStackSlot()) {
1161 __ movl(temp, Address(ESP, receiver.GetStackIndex()));
1162 __ movl(temp, Address(temp, class_offset));
1163 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001164 __ movl(temp, Address(receiver.As<Register>(), class_offset));
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001165 }
1166 // temp = temp->GetMethodAt(method_offset);
1167 __ movl(temp, Address(temp, method_offset));
1168 // call temp->GetEntryPoint();
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001169 __ call(Address(temp, mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset().Int32Value()));
1170
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001171 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +01001172 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001173}
1174
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001175void LocationsBuilderX86::VisitInvokeInterface(HInvokeInterface* invoke) {
1176 HandleInvoke(invoke);
1177 // Add the hidden argument.
1178 invoke->GetLocations()->AddTemp(Location::FpuRegisterLocation(XMM0));
1179}
1180
1181void InstructionCodeGeneratorX86::VisitInvokeInterface(HInvokeInterface* invoke) {
1182 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
1183 Register temp = invoke->GetLocations()->GetTemp(0).As<Register>();
1184 uint32_t method_offset = mirror::Class::EmbeddedImTableOffset().Uint32Value() +
1185 (invoke->GetImtIndex() % mirror::Class::kImtSize) * sizeof(mirror::Class::ImTableEntry);
1186 LocationSummary* locations = invoke->GetLocations();
1187 Location receiver = locations->InAt(0);
1188 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
1189
1190 // Set the hidden argument.
1191 __ movl(temp, Immediate(invoke->GetDexMethodIndex()));
1192 __ movd(invoke->GetLocations()->GetTemp(1).As<XmmRegister>(), temp);
1193
1194 // temp = object->GetClass();
1195 if (receiver.IsStackSlot()) {
1196 __ movl(temp, Address(ESP, receiver.GetStackIndex()));
1197 __ movl(temp, Address(temp, class_offset));
1198 } else {
1199 __ movl(temp, Address(receiver.As<Register>(), class_offset));
1200 }
1201 // temp = temp->GetImtEntryAt(method_offset);
1202 __ movl(temp, Address(temp, method_offset));
1203 // call temp->GetEntryPoint();
1204 __ call(Address(temp, mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset().Int32Value()));
1205
1206 DCHECK(!codegen_->IsLeafMethod());
1207 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
1208}
1209
Roland Levillain88cb1752014-10-20 16:36:47 +01001210void LocationsBuilderX86::VisitNeg(HNeg* neg) {
1211 LocationSummary* locations =
1212 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
1213 switch (neg->GetResultType()) {
1214 case Primitive::kPrimInt:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001215 case Primitive::kPrimLong:
Roland Levillain88cb1752014-10-20 16:36:47 +01001216 locations->SetInAt(0, Location::RequiresRegister());
1217 locations->SetOut(Location::SameAsFirstInput());
1218 break;
1219
Roland Levillain88cb1752014-10-20 16:36:47 +01001220 case Primitive::kPrimFloat:
1221 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00001222 locations->SetInAt(0, Location::RequiresFpuRegister());
1223 // Output overlaps as we need a fresh (zero-initialized)
1224 // register to perform subtraction from zero.
1225 locations->SetOut(Location::RequiresFpuRegister());
Roland Levillain88cb1752014-10-20 16:36:47 +01001226 break;
1227
1228 default:
1229 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1230 }
1231}
1232
1233void InstructionCodeGeneratorX86::VisitNeg(HNeg* neg) {
1234 LocationSummary* locations = neg->GetLocations();
1235 Location out = locations->Out();
1236 Location in = locations->InAt(0);
1237 switch (neg->GetResultType()) {
1238 case Primitive::kPrimInt:
1239 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00001240 DCHECK(in.Equals(out));
Roland Levillain88cb1752014-10-20 16:36:47 +01001241 __ negl(out.As<Register>());
1242 break;
1243
1244 case Primitive::kPrimLong:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001245 DCHECK(in.IsRegisterPair());
Roland Levillain3dbcb382014-10-28 17:30:07 +00001246 DCHECK(in.Equals(out));
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001247 __ negl(out.AsRegisterPairLow<Register>());
1248 // Negation is similar to subtraction from zero. The least
1249 // significant byte triggers a borrow when it is different from
1250 // zero; to take it into account, add 1 to the most significant
1251 // byte if the carry flag (CF) is set to 1 after the first NEGL
1252 // operation.
1253 __ adcl(out.AsRegisterPairHigh<Register>(), Immediate(0));
1254 __ negl(out.AsRegisterPairHigh<Register>());
1255 break;
1256
Roland Levillain88cb1752014-10-20 16:36:47 +01001257 case Primitive::kPrimFloat:
Roland Levillain3dbcb382014-10-28 17:30:07 +00001258 DCHECK(!in.Equals(out));
1259 // out = 0
1260 __ xorps(out.As<XmmRegister>(), out.As<XmmRegister>());
1261 // out = out - in
1262 __ subss(out.As<XmmRegister>(), in.As<XmmRegister>());
1263 break;
1264
Roland Levillain88cb1752014-10-20 16:36:47 +01001265 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00001266 DCHECK(!in.Equals(out));
1267 // out = 0
1268 __ xorpd(out.As<XmmRegister>(), out.As<XmmRegister>());
1269 // out = out - in
1270 __ subsd(out.As<XmmRegister>(), in.As<XmmRegister>());
Roland Levillain88cb1752014-10-20 16:36:47 +01001271 break;
1272
1273 default:
1274 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1275 }
1276}
1277
Roland Levillaindff1f282014-11-05 14:15:05 +00001278void LocationsBuilderX86::VisitTypeConversion(HTypeConversion* conversion) {
1279 LocationSummary* locations =
1280 new (GetGraph()->GetArena()) LocationSummary(conversion, LocationSummary::kNoCall);
1281 Primitive::Type result_type = conversion->GetResultType();
1282 Primitive::Type input_type = conversion->GetInputType();
1283 switch (result_type) {
Roland Levillain946e1432014-11-11 17:35:19 +00001284 case Primitive::kPrimInt:
1285 switch (input_type) {
1286 case Primitive::kPrimLong:
1287 // long-to-int conversion.
1288 locations->SetInAt(0, Location::Any());
1289 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1290 break;
1291
1292 case Primitive::kPrimFloat:
1293 case Primitive::kPrimDouble:
1294 LOG(FATAL) << "Type conversion from " << input_type
1295 << " to " << result_type << " not yet implemented";
1296 break;
1297
1298 default:
1299 LOG(FATAL) << "Unexpected type conversion from " << input_type
1300 << " to " << result_type;
1301 }
1302 break;
1303
Roland Levillaindff1f282014-11-05 14:15:05 +00001304 case Primitive::kPrimLong:
1305 switch (input_type) {
1306 case Primitive::kPrimByte:
1307 case Primitive::kPrimShort:
1308 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00001309 case Primitive::kPrimChar:
Roland Levillaindff1f282014-11-05 14:15:05 +00001310 // int-to-long conversion.
1311 locations->SetInAt(0, Location::RegisterLocation(EAX));
1312 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
1313 break;
1314
1315 case Primitive::kPrimFloat:
1316 case Primitive::kPrimDouble:
1317 LOG(FATAL) << "Type conversion from " << input_type << " to "
1318 << result_type << " not yet implemented";
1319 break;
1320
1321 default:
1322 LOG(FATAL) << "Unexpected type conversion from " << input_type
1323 << " to " << result_type;
1324 }
1325 break;
1326
Roland Levillaindff1f282014-11-05 14:15:05 +00001327 case Primitive::kPrimFloat:
1328 case Primitive::kPrimDouble:
1329 LOG(FATAL) << "Type conversion from " << input_type
1330 << " to " << result_type << " not yet implemented";
1331 break;
1332
1333 default:
1334 LOG(FATAL) << "Unexpected type conversion from " << input_type
1335 << " to " << result_type;
1336 }
1337}
1338
1339void InstructionCodeGeneratorX86::VisitTypeConversion(HTypeConversion* conversion) {
1340 LocationSummary* locations = conversion->GetLocations();
1341 Location out = locations->Out();
1342 Location in = locations->InAt(0);
1343 Primitive::Type result_type = conversion->GetResultType();
1344 Primitive::Type input_type = conversion->GetInputType();
1345 switch (result_type) {
Roland Levillain946e1432014-11-11 17:35:19 +00001346 case Primitive::kPrimInt:
1347 switch (input_type) {
1348 case Primitive::kPrimLong:
1349 // long-to-int conversion.
1350 if (in.IsRegisterPair()) {
1351 __ movl(out.As<Register>(), in.AsRegisterPairLow<Register>());
1352 } else if (in.IsDoubleStackSlot()) {
1353 __ movl(out.As<Register>(), Address(ESP, in.GetStackIndex()));
1354 } else {
1355 DCHECK(in.IsConstant());
1356 DCHECK(in.GetConstant()->IsLongConstant());
1357 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
1358 __ movl(out.As<Register>(), Immediate(static_cast<int32_t>(value)));
1359 }
1360 break;
1361
1362 case Primitive::kPrimFloat:
1363 case Primitive::kPrimDouble:
1364 LOG(FATAL) << "Type conversion from " << input_type
1365 << " to " << result_type << " not yet implemented";
1366 break;
1367
1368 default:
1369 LOG(FATAL) << "Unexpected type conversion from " << input_type
1370 << " to " << result_type;
1371 }
1372 break;
1373
Roland Levillaindff1f282014-11-05 14:15:05 +00001374 case Primitive::kPrimLong:
1375 switch (input_type) {
1376 case Primitive::kPrimByte:
1377 case Primitive::kPrimShort:
1378 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00001379 case Primitive::kPrimChar:
Roland Levillaindff1f282014-11-05 14:15:05 +00001380 // int-to-long conversion.
1381 DCHECK_EQ(out.AsRegisterPairLow<Register>(), EAX);
1382 DCHECK_EQ(out.AsRegisterPairHigh<Register>(), EDX);
1383 DCHECK_EQ(in.As<Register>(), EAX);
1384 __ cdq();
1385 break;
1386
1387 case Primitive::kPrimFloat:
1388 case Primitive::kPrimDouble:
1389 LOG(FATAL) << "Type conversion from " << input_type << " to "
1390 << result_type << " not yet implemented";
1391 break;
1392
1393 default:
1394 LOG(FATAL) << "Unexpected type conversion from " << input_type
1395 << " to " << result_type;
1396 }
1397 break;
1398
Roland Levillaindff1f282014-11-05 14:15:05 +00001399 case Primitive::kPrimFloat:
1400 case Primitive::kPrimDouble:
1401 LOG(FATAL) << "Type conversion from " << input_type
1402 << " to " << result_type << " not yet implemented";
1403 break;
1404
1405 default:
1406 LOG(FATAL) << "Unexpected type conversion from " << input_type
1407 << " to " << result_type;
1408 }
1409}
1410
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001411void LocationsBuilderX86::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001412 LocationSummary* locations =
1413 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001414 switch (add->GetResultType()) {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001415 case Primitive::kPrimInt:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001416 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001417 locations->SetInAt(0, Location::RequiresRegister());
1418 locations->SetInAt(1, Location::Any());
1419 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001420 break;
1421 }
1422
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001423 case Primitive::kPrimFloat:
1424 case Primitive::kPrimDouble: {
1425 locations->SetInAt(0, Location::RequiresFpuRegister());
1426 locations->SetInAt(1, Location::Any());
1427 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001428 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001429 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001430
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001431 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001432 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
1433 break;
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001434 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001435}
1436
1437void InstructionCodeGeneratorX86::VisitAdd(HAdd* add) {
1438 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001439 Location first = locations->InAt(0);
1440 Location second = locations->InAt(1);
Calin Juravle11351682014-10-23 15:38:15 +01001441 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001442 switch (add->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001443 case Primitive::kPrimInt: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001444 if (second.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001445 __ addl(first.As<Register>(), second.As<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001446 } else if (second.IsConstant()) {
Calin Juravle11351682014-10-23 15:38:15 +01001447 __ addl(first.As<Register>(), Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001448 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001449 __ addl(first.As<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001450 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001451 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001452 }
1453
1454 case Primitive::kPrimLong: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001455 if (second.IsRegisterPair()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001456 __ addl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
1457 __ adcl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001458 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001459 __ addl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
1460 __ adcl(first.AsRegisterPairHigh<Register>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001461 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001462 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001463 break;
1464 }
1465
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001466 case Primitive::kPrimFloat: {
1467 if (second.IsFpuRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001468 __ addss(first.As<XmmRegister>(), second.As<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001469 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001470 __ addss(first.As<XmmRegister>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001471 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001472 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001473 }
1474
1475 case Primitive::kPrimDouble: {
1476 if (second.IsFpuRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001477 __ addsd(first.As<XmmRegister>(), second.As<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001478 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001479 __ addsd(first.As<XmmRegister>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001480 }
1481 break;
1482 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001483
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001484 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001485 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001486 }
1487}
1488
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001489void LocationsBuilderX86::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001490 LocationSummary* locations =
1491 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001492 switch (sub->GetResultType()) {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001493 case Primitive::kPrimInt:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001494 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001495 locations->SetInAt(0, Location::RequiresRegister());
1496 locations->SetInAt(1, Location::Any());
1497 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001498 break;
1499 }
Calin Juravle11351682014-10-23 15:38:15 +01001500 case Primitive::kPrimFloat:
1501 case Primitive::kPrimDouble: {
1502 locations->SetInAt(0, Location::RequiresFpuRegister());
1503 locations->SetInAt(1, Location::RequiresFpuRegister());
1504 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001505 break;
Calin Juravle11351682014-10-23 15:38:15 +01001506 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001507
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001508 default:
Calin Juravle11351682014-10-23 15:38:15 +01001509 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001510 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001511}
1512
1513void InstructionCodeGeneratorX86::VisitSub(HSub* sub) {
1514 LocationSummary* locations = sub->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001515 Location first = locations->InAt(0);
1516 Location second = locations->InAt(1);
Calin Juravle11351682014-10-23 15:38:15 +01001517 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001518 switch (sub->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001519 case Primitive::kPrimInt: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001520 if (second.IsRegister()) {
Calin Juravle11351682014-10-23 15:38:15 +01001521 __ subl(first.As<Register>(), second.As<Register>());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001522 } else if (second.IsConstant()) {
Calin Juravle11351682014-10-23 15:38:15 +01001523 __ subl(first.As<Register>(), Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001524 } else {
Calin Juravle11351682014-10-23 15:38:15 +01001525 __ subl(first.As<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001526 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001527 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001528 }
1529
1530 case Primitive::kPrimLong: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001531 if (second.IsRegisterPair()) {
Calin Juravle11351682014-10-23 15:38:15 +01001532 __ subl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
1533 __ sbbl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001534 } else {
Calin Juravle11351682014-10-23 15:38:15 +01001535 __ subl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001536 __ sbbl(first.AsRegisterPairHigh<Register>(),
1537 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001538 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001539 break;
1540 }
1541
Calin Juravle11351682014-10-23 15:38:15 +01001542 case Primitive::kPrimFloat: {
1543 __ subss(first.As<XmmRegister>(), second.As<XmmRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001544 break;
Calin Juravle11351682014-10-23 15:38:15 +01001545 }
1546
1547 case Primitive::kPrimDouble: {
1548 __ subsd(first.As<XmmRegister>(), second.As<XmmRegister>());
1549 break;
1550 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001551
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001552 default:
Calin Juravle11351682014-10-23 15:38:15 +01001553 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001554 }
1555}
1556
Calin Juravle34bacdf2014-10-07 20:23:36 +01001557void LocationsBuilderX86::VisitMul(HMul* mul) {
1558 LocationSummary* locations =
1559 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
1560 switch (mul->GetResultType()) {
1561 case Primitive::kPrimInt:
1562 locations->SetInAt(0, Location::RequiresRegister());
1563 locations->SetInAt(1, Location::Any());
1564 locations->SetOut(Location::SameAsFirstInput());
1565 break;
1566 case Primitive::kPrimLong: {
1567 locations->SetInAt(0, Location::RequiresRegister());
1568 // TODO: Currently this handles only stack operands:
1569 // - we don't have enough registers because we currently use Quick ABI.
1570 // - by the time we have a working register allocator we will probably change the ABI
1571 // and fix the above.
1572 // - we don't have a way yet to request operands on stack but the base line compiler
1573 // will leave the operands on the stack with Any().
1574 locations->SetInAt(1, Location::Any());
1575 locations->SetOut(Location::SameAsFirstInput());
1576 // Needed for imul on 32bits with 64bits output.
1577 locations->AddTemp(Location::RegisterLocation(EAX));
1578 locations->AddTemp(Location::RegisterLocation(EDX));
1579 break;
1580 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01001581 case Primitive::kPrimFloat:
1582 case Primitive::kPrimDouble: {
1583 locations->SetInAt(0, Location::RequiresFpuRegister());
1584 locations->SetInAt(1, Location::RequiresFpuRegister());
1585 locations->SetOut(Location::SameAsFirstInput());
Calin Juravle34bacdf2014-10-07 20:23:36 +01001586 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01001587 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01001588
1589 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01001590 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01001591 }
1592}
1593
1594void InstructionCodeGeneratorX86::VisitMul(HMul* mul) {
1595 LocationSummary* locations = mul->GetLocations();
1596 Location first = locations->InAt(0);
1597 Location second = locations->InAt(1);
1598 DCHECK(first.Equals(locations->Out()));
1599
1600 switch (mul->GetResultType()) {
1601 case Primitive::kPrimInt: {
1602 if (second.IsRegister()) {
1603 __ imull(first.As<Register>(), second.As<Register>());
1604 } else if (second.IsConstant()) {
1605 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
1606 __ imull(first.As<Register>(), imm);
1607 } else {
1608 DCHECK(second.IsStackSlot());
1609 __ imull(first.As<Register>(), Address(ESP, second.GetStackIndex()));
1610 }
1611 break;
1612 }
1613
1614 case Primitive::kPrimLong: {
1615 DCHECK(second.IsDoubleStackSlot());
1616
1617 Register in1_hi = first.AsRegisterPairHigh<Register>();
1618 Register in1_lo = first.AsRegisterPairLow<Register>();
1619 Address in2_hi(ESP, second.GetHighStackIndex(kX86WordSize));
1620 Address in2_lo(ESP, second.GetStackIndex());
1621 Register eax = locations->GetTemp(0).As<Register>();
1622 Register edx = locations->GetTemp(1).As<Register>();
1623
1624 DCHECK_EQ(EAX, eax);
1625 DCHECK_EQ(EDX, edx);
1626
1627 // input: in1 - 64 bits, in2 - 64 bits
1628 // output: in1
1629 // formula: in1.hi : in1.lo = (in1.lo * in2.hi + in1.hi * in2.lo)* 2^32 + in1.lo * in2.lo
1630 // parts: in1.hi = in1.lo * in2.hi + in1.hi * in2.lo + (in1.lo * in2.lo)[63:32]
1631 // parts: in1.lo = (in1.lo * in2.lo)[31:0]
1632
1633 __ movl(eax, in2_hi);
1634 // eax <- in1.lo * in2.hi
1635 __ imull(eax, in1_lo);
1636 // in1.hi <- in1.hi * in2.lo
1637 __ imull(in1_hi, in2_lo);
1638 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
1639 __ addl(in1_hi, eax);
1640 // move in1_lo to eax to prepare for double precision
1641 __ movl(eax, in1_lo);
1642 // edx:eax <- in1.lo * in2.lo
1643 __ mull(in2_lo);
1644 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
1645 __ addl(in1_hi, edx);
1646 // in1.lo <- (in1.lo * in2.lo)[31:0];
1647 __ movl(in1_lo, eax);
1648
1649 break;
1650 }
1651
Calin Juravleb5bfa962014-10-21 18:02:24 +01001652 case Primitive::kPrimFloat: {
1653 __ mulss(first.As<XmmRegister>(), second.As<XmmRegister>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01001654 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01001655 }
1656
1657 case Primitive::kPrimDouble: {
1658 __ mulsd(first.As<XmmRegister>(), second.As<XmmRegister>());
1659 break;
1660 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01001661
1662 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01001663 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01001664 }
1665}
1666
Calin Juravle7c4954d2014-10-28 16:57:40 +00001667void LocationsBuilderX86::VisitDiv(HDiv* div) {
1668 LocationSummary* locations =
1669 new (GetGraph()->GetArena()) LocationSummary(div, LocationSummary::kNoCall);
1670 switch (div->GetResultType()) {
Calin Juravled0d48522014-11-04 16:40:20 +00001671 case Primitive::kPrimInt: {
1672 locations->SetInAt(0, Location::RegisterLocation(EAX));
1673 locations->SetInAt(1, Location::RequiresRegister());
1674 locations->SetOut(Location::SameAsFirstInput());
1675 // Intel uses edx:eax as the dividend.
1676 locations->AddTemp(Location::RegisterLocation(EDX));
1677 break;
1678 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00001679 case Primitive::kPrimLong: {
1680 LOG(FATAL) << "Not implemented div type" << div->GetResultType();
1681 break;
1682 }
1683 case Primitive::kPrimFloat:
1684 case Primitive::kPrimDouble: {
1685 locations->SetInAt(0, Location::RequiresFpuRegister());
1686 locations->SetInAt(1, Location::RequiresFpuRegister());
1687 locations->SetOut(Location::SameAsFirstInput());
1688 break;
1689 }
1690
1691 default:
1692 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
1693 }
1694}
1695
1696void InstructionCodeGeneratorX86::VisitDiv(HDiv* div) {
1697 LocationSummary* locations = div->GetLocations();
1698 Location first = locations->InAt(0);
1699 Location second = locations->InAt(1);
1700 DCHECK(first.Equals(locations->Out()));
1701
1702 switch (div->GetResultType()) {
Calin Juravled0d48522014-11-04 16:40:20 +00001703 case Primitive::kPrimInt: {
1704 Register first_reg = first.As<Register>();
1705 Register second_reg = second.As<Register>();
1706 DCHECK_EQ(EAX, first_reg);
1707 DCHECK_EQ(EDX, locations->GetTemp(0).As<Register>());
1708
1709 SlowPathCodeX86* slow_path =
1710 new (GetGraph()->GetArena()) DivMinusOneSlowPathX86(first_reg);
1711 codegen_->AddSlowPath(slow_path);
1712
1713 // 0x80000000/-1 triggers an arithmetic exception!
1714 // Dividing by -1 is actually negation and -0x800000000 = 0x80000000 so
1715 // it's safe to just use negl instead of more complex comparisons.
1716
1717 __ cmpl(second_reg, Immediate(-1));
1718 __ j(kEqual, slow_path->GetEntryLabel());
1719
1720 // edx:eax <- sign-extended of eax
1721 __ cdq();
1722 // eax = quotient, edx = remainder
1723 __ idivl(second_reg);
1724
1725 __ Bind(slow_path->GetExitLabel());
1726 break;
1727 }
1728
Calin Juravle7c4954d2014-10-28 16:57:40 +00001729 case Primitive::kPrimLong: {
1730 LOG(FATAL) << "Not implemented div type" << div->GetResultType();
1731 break;
1732 }
1733
1734 case Primitive::kPrimFloat: {
1735 __ divss(first.As<XmmRegister>(), second.As<XmmRegister>());
1736 break;
1737 }
1738
1739 case Primitive::kPrimDouble: {
1740 __ divsd(first.As<XmmRegister>(), second.As<XmmRegister>());
1741 break;
1742 }
1743
1744 default:
1745 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
1746 }
1747}
1748
Calin Juravled0d48522014-11-04 16:40:20 +00001749void LocationsBuilderX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
1750 LocationSummary* locations =
1751 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1752 locations->SetInAt(0, Location::Any());
1753 if (instruction->HasUses()) {
1754 locations->SetOut(Location::SameAsFirstInput());
1755 }
1756}
1757
1758void InstructionCodeGeneratorX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
1759 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) DivZeroCheckSlowPathX86(instruction);
1760 codegen_->AddSlowPath(slow_path);
1761
1762 LocationSummary* locations = instruction->GetLocations();
1763 Location value = locations->InAt(0);
1764
1765 if (value.IsRegister()) {
1766 __ testl(value.As<Register>(), value.As<Register>());
1767 } else if (value.IsStackSlot()) {
1768 __ cmpl(Address(ESP, value.GetStackIndex()), Immediate(0));
1769 } else {
1770 DCHECK(value.IsConstant()) << value;
1771 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
1772 __ jmp(slow_path->GetEntryLabel());
1773 }
1774 return;
1775 }
1776 __ j(kEqual, slow_path->GetEntryLabel());
1777}
1778
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001779void LocationsBuilderX86::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001780 LocationSummary* locations =
1781 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001782 locations->SetOut(Location::RegisterLocation(EAX));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001783 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001784 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1785 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001786}
1787
1788void InstructionCodeGeneratorX86::VisitNewInstance(HNewInstance* instruction) {
1789 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001790 codegen_->LoadCurrentMethod(calling_convention.GetRegisterAt(1));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001791 __ movl(calling_convention.GetRegisterAt(0), Immediate(instruction->GetTypeIndex()));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001792
Nicolas Geoffray707c8092014-04-04 10:50:14 +01001793 __ fs()->call(
1794 Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pAllocObjectWithAccessCheck)));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001795
Nicolas Geoffray39468442014-09-02 15:17:15 +01001796 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001797 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001798}
1799
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001800void LocationsBuilderX86::VisitNewArray(HNewArray* instruction) {
1801 LocationSummary* locations =
1802 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
1803 locations->SetOut(Location::RegisterLocation(EAX));
1804 InvokeRuntimeCallingConvention calling_convention;
1805 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1806 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
1807 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
1808}
1809
1810void InstructionCodeGeneratorX86::VisitNewArray(HNewArray* instruction) {
1811 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001812 codegen_->LoadCurrentMethod(calling_convention.GetRegisterAt(1));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001813 __ movl(calling_convention.GetRegisterAt(0), Immediate(instruction->GetTypeIndex()));
1814
1815 __ fs()->call(
1816 Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pAllocArrayWithAccessCheck)));
1817
1818 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
1819 DCHECK(!codegen_->IsLeafMethod());
1820}
1821
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001822void LocationsBuilderX86::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001823 LocationSummary* locations =
1824 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001825 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
1826 if (location.IsStackSlot()) {
1827 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
1828 } else if (location.IsDoubleStackSlot()) {
1829 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001830 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001831 locations->SetOut(location);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001832}
1833
1834void InstructionCodeGeneratorX86::VisitParameterValue(HParameterValue* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001835 UNUSED(instruction);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001836}
1837
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001838void LocationsBuilderX86::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001839 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001840 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001841 locations->SetInAt(0, Location::RequiresRegister());
1842 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01001843}
1844
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001845void InstructionCodeGeneratorX86::VisitNot(HNot* not_) {
1846 LocationSummary* locations = not_->GetLocations();
Roland Levillain70566432014-10-24 16:20:17 +01001847 Location in = locations->InAt(0);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001848 Location out = locations->Out();
Roland Levillain70566432014-10-24 16:20:17 +01001849 DCHECK(in.Equals(out));
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001850 switch (not_->InputAt(0)->GetType()) {
1851 case Primitive::kPrimBoolean:
1852 __ xorl(out.As<Register>(), Immediate(1));
1853 break;
1854
1855 case Primitive::kPrimInt:
1856 __ notl(out.As<Register>());
1857 break;
1858
1859 case Primitive::kPrimLong:
Roland Levillain70566432014-10-24 16:20:17 +01001860 __ notl(out.AsRegisterPairLow<Register>());
1861 __ notl(out.AsRegisterPairHigh<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001862 break;
1863
1864 default:
1865 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
1866 }
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01001867}
1868
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001869void LocationsBuilderX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001870 LocationSummary* locations =
1871 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01001872 locations->SetInAt(0, Location::RequiresRegister());
1873 locations->SetInAt(1, Location::Any());
1874 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001875}
1876
1877void InstructionCodeGeneratorX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001878 LocationSummary* locations = compare->GetLocations();
1879 switch (compare->InputAt(0)->GetType()) {
1880 case Primitive::kPrimLong: {
1881 Label less, greater, done;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001882 Register output = locations->Out().As<Register>();
1883 Location left = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001884 Location right = locations->InAt(1);
1885 if (right.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001886 __ cmpl(left.AsRegisterPairHigh<Register>(), right.AsRegisterPairHigh<Register>());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001887 } else {
1888 DCHECK(right.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001889 __ cmpl(left.AsRegisterPairHigh<Register>(),
1890 Address(ESP, right.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001891 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001892 __ j(kLess, &less); // Signed compare.
1893 __ j(kGreater, &greater); // Signed compare.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001894 if (right.IsRegisterPair()) {
1895 __ cmpl(left.AsRegisterPairLow<Register>(), right.AsRegisterPairLow<Register>());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001896 } else {
1897 DCHECK(right.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001898 __ cmpl(left.AsRegisterPairLow<Register>(), Address(ESP, right.GetStackIndex()));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001899 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001900 __ movl(output, Immediate(0));
1901 __ j(kEqual, &done);
1902 __ j(kBelow, &less); // Unsigned compare.
1903
1904 __ Bind(&greater);
1905 __ movl(output, Immediate(1));
1906 __ jmp(&done);
1907
1908 __ Bind(&less);
1909 __ movl(output, Immediate(-1));
1910
1911 __ Bind(&done);
1912 break;
1913 }
1914 default:
1915 LOG(FATAL) << "Unimplemented compare type " << compare->InputAt(0)->GetType();
1916 }
1917}
1918
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001919void LocationsBuilderX86::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001920 LocationSummary* locations =
1921 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01001922 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
1923 locations->SetInAt(i, Location::Any());
1924 }
1925 locations->SetOut(Location::Any());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001926}
1927
1928void InstructionCodeGeneratorX86::VisitPhi(HPhi* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001929 UNUSED(instruction);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001930 LOG(FATAL) << "Unreachable";
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001931}
1932
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001933void LocationsBuilderX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001934 LocationSummary* locations =
1935 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001936 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray39468442014-09-02 15:17:15 +01001937 Primitive::Type field_type = instruction->GetFieldType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00001938 bool needs_write_barrier =
1939 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
1940
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01001941 bool is_byte_type = (field_type == Primitive::kPrimBoolean)
1942 || (field_type == Primitive::kPrimByte);
1943 // The register allocator does not support multiple
1944 // inputs that die at entry with one in a specific register.
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01001945 if (is_byte_type) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001946 // Ensure the value is in a byte register.
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01001947 locations->SetInAt(1, Location::RegisterLocation(EAX));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001948 } else {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01001949 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001950 }
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01001951 // Temporary registers for the write barrier.
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00001952 if (needs_write_barrier) {
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01001953 locations->AddTemp(Location::RequiresRegister());
1954 // Ensure the card is in a byte register.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001955 locations->AddTemp(Location::RegisterLocation(ECX));
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01001956 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001957}
1958
1959void InstructionCodeGeneratorX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
1960 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001961 Register obj = locations->InAt(0).As<Register>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001962 uint32_t offset = instruction->GetFieldOffset().Uint32Value();
Nicolas Geoffray39468442014-09-02 15:17:15 +01001963 Primitive::Type field_type = instruction->GetFieldType();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001964
1965 switch (field_type) {
1966 case Primitive::kPrimBoolean:
1967 case Primitive::kPrimByte: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001968 ByteRegister value = locations->InAt(1).As<ByteRegister>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001969 __ movb(Address(obj, offset), value);
1970 break;
1971 }
1972
1973 case Primitive::kPrimShort:
1974 case Primitive::kPrimChar: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001975 Register value = locations->InAt(1).As<Register>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001976 __ movw(Address(obj, offset), value);
1977 break;
1978 }
1979
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001980 case Primitive::kPrimInt:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001981 case Primitive::kPrimNot: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001982 Register value = locations->InAt(1).As<Register>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001983 __ movl(Address(obj, offset), value);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001984
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00001985 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001986 Register temp = locations->GetTemp(0).As<Register>();
1987 Register card = locations->GetTemp(1).As<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001988 codegen_->MarkGCCard(temp, card, obj, value);
1989 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001990 break;
1991 }
1992
1993 case Primitive::kPrimLong: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001994 Location value = locations->InAt(1);
1995 __ movl(Address(obj, offset), value.AsRegisterPairLow<Register>());
1996 __ movl(Address(obj, kX86WordSize + offset), value.AsRegisterPairHigh<Register>());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001997 break;
1998 }
1999
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002000 case Primitive::kPrimFloat: {
2001 XmmRegister value = locations->InAt(1).As<XmmRegister>();
2002 __ movss(Address(obj, offset), value);
2003 break;
2004 }
2005
2006 case Primitive::kPrimDouble: {
2007 XmmRegister value = locations->InAt(1).As<XmmRegister>();
2008 __ movsd(Address(obj, offset), value);
2009 break;
2010 }
2011
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002012 case Primitive::kPrimVoid:
2013 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07002014 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002015 }
2016}
2017
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002018void CodeGeneratorX86::MarkGCCard(Register temp, Register card, Register object, Register value) {
2019 Label is_null;
2020 __ testl(value, value);
2021 __ j(kEqual, &is_null);
2022 __ fs()->movl(card, Address::Absolute(Thread::CardTableOffset<kX86WordSize>().Int32Value()));
2023 __ movl(temp, object);
2024 __ shrl(temp, Immediate(gc::accounting::CardTable::kCardShift));
2025 __ movb(Address(temp, card, TIMES_1, 0),
2026 X86ManagedRegister::FromCpuRegister(card).AsByteRegister());
2027 __ Bind(&is_null);
2028}
2029
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002030void LocationsBuilderX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002031 LocationSummary* locations =
2032 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002033 locations->SetInAt(0, Location::RequiresRegister());
2034 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002035}
2036
2037void InstructionCodeGeneratorX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
2038 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002039 Register obj = locations->InAt(0).As<Register>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002040 uint32_t offset = instruction->GetFieldOffset().Uint32Value();
2041
2042 switch (instruction->GetType()) {
2043 case Primitive::kPrimBoolean: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002044 Register out = locations->Out().As<Register>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002045 __ movzxb(out, Address(obj, offset));
2046 break;
2047 }
2048
2049 case Primitive::kPrimByte: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002050 Register out = locations->Out().As<Register>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002051 __ movsxb(out, Address(obj, offset));
2052 break;
2053 }
2054
2055 case Primitive::kPrimShort: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002056 Register out = locations->Out().As<Register>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002057 __ movsxw(out, Address(obj, offset));
2058 break;
2059 }
2060
2061 case Primitive::kPrimChar: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002062 Register out = locations->Out().As<Register>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002063 __ movzxw(out, Address(obj, offset));
2064 break;
2065 }
2066
2067 case Primitive::kPrimInt:
2068 case Primitive::kPrimNot: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002069 Register out = locations->Out().As<Register>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002070 __ movl(out, Address(obj, offset));
2071 break;
2072 }
2073
2074 case Primitive::kPrimLong: {
2075 // TODO: support volatile.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002076 __ movl(locations->Out().AsRegisterPairLow<Register>(), Address(obj, offset));
2077 __ movl(locations->Out().AsRegisterPairHigh<Register>(), Address(obj, kX86WordSize + offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002078 break;
2079 }
2080
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002081 case Primitive::kPrimFloat: {
2082 XmmRegister out = locations->Out().As<XmmRegister>();
2083 __ movss(out, Address(obj, offset));
2084 break;
2085 }
2086
2087 case Primitive::kPrimDouble: {
2088 XmmRegister out = locations->Out().As<XmmRegister>();
2089 __ movsd(out, Address(obj, offset));
2090 break;
2091 }
2092
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002093 case Primitive::kPrimVoid:
2094 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07002095 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002096 }
2097}
2098
2099void LocationsBuilderX86::VisitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002100 LocationSummary* locations =
2101 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002102 locations->SetInAt(0, Location::Any());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002103 if (instruction->HasUses()) {
2104 locations->SetOut(Location::SameAsFirstInput());
2105 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002106}
2107
2108void InstructionCodeGeneratorX86::VisitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01002109 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathX86(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002110 codegen_->AddSlowPath(slow_path);
2111
2112 LocationSummary* locations = instruction->GetLocations();
2113 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002114
2115 if (obj.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002116 __ cmpl(obj.As<Register>(), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002117 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002118 __ cmpl(Address(ESP, obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002119 } else {
2120 DCHECK(obj.IsConstant()) << obj;
2121 DCHECK_EQ(obj.GetConstant()->AsIntConstant()->GetValue(), 0);
2122 __ jmp(slow_path->GetEntryLabel());
2123 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002124 }
2125 __ j(kEqual, slow_path->GetEntryLabel());
2126}
2127
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002128void LocationsBuilderX86::VisitArrayGet(HArrayGet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002129 LocationSummary* locations =
2130 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002131 locations->SetInAt(0, Location::RequiresRegister());
2132 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
2133 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002134}
2135
2136void InstructionCodeGeneratorX86::VisitArrayGet(HArrayGet* instruction) {
2137 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002138 Register obj = locations->InAt(0).As<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002139 Location index = locations->InAt(1);
2140
2141 switch (instruction->GetType()) {
2142 case Primitive::kPrimBoolean: {
2143 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002144 Register out = locations->Out().As<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002145 if (index.IsConstant()) {
2146 __ movzxb(out, Address(obj,
2147 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
2148 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002149 __ movzxb(out, Address(obj, index.As<Register>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002150 }
2151 break;
2152 }
2153
2154 case Primitive::kPrimByte: {
2155 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int8_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002156 Register out = locations->Out().As<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002157 if (index.IsConstant()) {
2158 __ movsxb(out, Address(obj,
2159 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
2160 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002161 __ movsxb(out, Address(obj, index.As<Register>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002162 }
2163 break;
2164 }
2165
2166 case Primitive::kPrimShort: {
2167 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int16_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002168 Register out = locations->Out().As<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002169 if (index.IsConstant()) {
2170 __ movsxw(out, Address(obj,
2171 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
2172 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002173 __ movsxw(out, Address(obj, index.As<Register>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002174 }
2175 break;
2176 }
2177
2178 case Primitive::kPrimChar: {
2179 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002180 Register out = locations->Out().As<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002181 if (index.IsConstant()) {
2182 __ movzxw(out, Address(obj,
2183 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
2184 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002185 __ movzxw(out, Address(obj, index.As<Register>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002186 }
2187 break;
2188 }
2189
2190 case Primitive::kPrimInt:
2191 case Primitive::kPrimNot: {
2192 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002193 Register out = locations->Out().As<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002194 if (index.IsConstant()) {
2195 __ movl(out, Address(obj,
2196 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
2197 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002198 __ movl(out, Address(obj, index.As<Register>(), TIMES_4, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002199 }
2200 break;
2201 }
2202
2203 case Primitive::kPrimLong: {
2204 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002205 Location out = locations->Out();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002206 if (index.IsConstant()) {
2207 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002208 __ movl(out.AsRegisterPairLow<Register>(), Address(obj, offset));
2209 __ movl(out.AsRegisterPairHigh<Register>(), Address(obj, offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002210 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002211 __ movl(out.AsRegisterPairLow<Register>(),
2212 Address(obj, index.As<Register>(), TIMES_8, data_offset));
2213 __ movl(out.AsRegisterPairHigh<Register>(),
2214 Address(obj, index.As<Register>(), TIMES_8, data_offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002215 }
2216 break;
2217 }
2218
2219 case Primitive::kPrimFloat:
2220 case Primitive::kPrimDouble:
2221 LOG(FATAL) << "Unimplemented register type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07002222 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002223 case Primitive::kPrimVoid:
2224 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07002225 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002226 }
2227}
2228
2229void LocationsBuilderX86::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002230 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002231 bool needs_write_barrier =
2232 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
2233
2234 DCHECK(kFollowsQuickABI);
2235 bool not_enough_registers = needs_write_barrier
2236 && !instruction->GetValue()->IsConstant()
2237 && !instruction->GetIndex()->IsConstant();
2238 bool needs_runtime_call = instruction->NeedsTypeCheck() || not_enough_registers;
2239
Nicolas Geoffray39468442014-09-02 15:17:15 +01002240 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
2241 instruction,
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002242 needs_runtime_call ? LocationSummary::kCall : LocationSummary::kNoCall);
Nicolas Geoffray39468442014-09-02 15:17:15 +01002243
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002244 if (needs_runtime_call) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002245 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002246 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2247 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
2248 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002249 } else {
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01002250 bool is_byte_type = (value_type == Primitive::kPrimBoolean)
2251 || (value_type == Primitive::kPrimByte);
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01002252 // We need the inputs to be different than the output in case of long operation.
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01002253 // In case of a byte operation, the register allocator does not support multiple
2254 // inputs that die at entry with one in a specific register.
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002255 locations->SetInAt(0, Location::RequiresRegister());
2256 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01002257 if (is_byte_type) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002258 // Ensure the value is in a byte register.
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002259 locations->SetInAt(2, Location::ByteRegisterOrConstant(EAX, instruction->InputAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002260 } else {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002261 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002262 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002263 // Temporary registers for the write barrier.
2264 if (needs_write_barrier) {
2265 locations->AddTemp(Location::RequiresRegister());
2266 // Ensure the card is in a byte register.
2267 locations->AddTemp(Location::RegisterLocation(ECX));
2268 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002269 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002270}
2271
2272void InstructionCodeGeneratorX86::VisitArraySet(HArraySet* instruction) {
2273 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002274 Register obj = locations->InAt(0).As<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002275 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002276 Location value = locations->InAt(2);
Nicolas Geoffray39468442014-09-02 15:17:15 +01002277 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002278 bool needs_runtime_call = locations->WillCall();
2279 bool needs_write_barrier =
2280 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002281
2282 switch (value_type) {
2283 case Primitive::kPrimBoolean:
2284 case Primitive::kPrimByte: {
2285 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002286 if (index.IsConstant()) {
2287 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002288 if (value.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002289 __ movb(Address(obj, offset), value.As<ByteRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002290 } else {
2291 __ movb(Address(obj, offset),
2292 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
2293 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002294 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002295 if (value.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002296 __ movb(Address(obj, index.As<Register>(), TIMES_1, data_offset),
2297 value.As<ByteRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002298 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002299 __ movb(Address(obj, index.As<Register>(), TIMES_1, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002300 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
2301 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002302 }
2303 break;
2304 }
2305
2306 case Primitive::kPrimShort:
2307 case Primitive::kPrimChar: {
2308 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002309 if (index.IsConstant()) {
2310 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002311 if (value.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002312 __ movw(Address(obj, offset), value.As<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002313 } else {
2314 __ movw(Address(obj, offset),
2315 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
2316 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002317 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002318 if (value.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002319 __ movw(Address(obj, index.As<Register>(), TIMES_2, data_offset),
2320 value.As<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002321 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002322 __ movw(Address(obj, index.As<Register>(), TIMES_2, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002323 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
2324 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002325 }
2326 break;
2327 }
2328
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002329 case Primitive::kPrimInt:
2330 case Primitive::kPrimNot: {
2331 if (!needs_runtime_call) {
2332 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
2333 if (index.IsConstant()) {
2334 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
2335 if (value.IsRegister()) {
2336 __ movl(Address(obj, offset), value.As<Register>());
2337 } else {
2338 DCHECK(value.IsConstant()) << value;
2339 __ movl(Address(obj, offset),
2340 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
2341 }
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002342 } else {
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002343 DCHECK(index.IsRegister()) << index;
2344 if (value.IsRegister()) {
2345 __ movl(Address(obj, index.As<Register>(), TIMES_4, data_offset),
2346 value.As<Register>());
2347 } else {
2348 DCHECK(value.IsConstant()) << value;
2349 __ movl(Address(obj, index.As<Register>(), TIMES_4, data_offset),
2350 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
2351 }
2352 }
2353
2354 if (needs_write_barrier) {
2355 Register temp = locations->GetTemp(0).As<Register>();
2356 Register card = locations->GetTemp(1).As<Register>();
2357 codegen_->MarkGCCard(temp, card, obj, value.As<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002358 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002359 } else {
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002360 DCHECK_EQ(value_type, Primitive::kPrimNot);
2361 DCHECK(!codegen_->IsLeafMethod());
2362 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pAputObject)));
2363 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002364 }
2365 break;
2366 }
2367
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002368 case Primitive::kPrimLong: {
2369 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002370 if (index.IsConstant()) {
2371 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002372 if (value.IsRegisterPair()) {
2373 __ movl(Address(obj, offset), value.AsRegisterPairLow<Register>());
2374 __ movl(Address(obj, offset + kX86WordSize), value.AsRegisterPairHigh<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002375 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002376 DCHECK(value.IsConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002377 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
2378 __ movl(Address(obj, offset), Immediate(Low32Bits(val)));
2379 __ movl(Address(obj, offset + kX86WordSize), Immediate(High32Bits(val)));
2380 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002381 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002382 if (value.IsRegisterPair()) {
2383 __ movl(Address(obj, index.As<Register>(), TIMES_8, data_offset),
2384 value.AsRegisterPairLow<Register>());
2385 __ movl(Address(obj, index.As<Register>(), TIMES_8, data_offset + kX86WordSize),
2386 value.AsRegisterPairHigh<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002387 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002388 DCHECK(value.IsConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002389 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002390 __ movl(Address(obj, index.As<Register>(), TIMES_8, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002391 Immediate(Low32Bits(val)));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002392 __ movl(Address(obj, index.As<Register>(), TIMES_8, data_offset + kX86WordSize),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002393 Immediate(High32Bits(val)));
2394 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002395 }
2396 break;
2397 }
2398
2399 case Primitive::kPrimFloat:
2400 case Primitive::kPrimDouble:
2401 LOG(FATAL) << "Unimplemented register type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07002402 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002403 case Primitive::kPrimVoid:
2404 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07002405 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002406 }
2407}
2408
2409void LocationsBuilderX86::VisitArrayLength(HArrayLength* instruction) {
2410 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002411 locations->SetInAt(0, Location::RequiresRegister());
2412 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002413 instruction->SetLocations(locations);
2414}
2415
2416void InstructionCodeGeneratorX86::VisitArrayLength(HArrayLength* instruction) {
2417 LocationSummary* locations = instruction->GetLocations();
2418 uint32_t offset = mirror::Array::LengthOffset().Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002419 Register obj = locations->InAt(0).As<Register>();
2420 Register out = locations->Out().As<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002421 __ movl(out, Address(obj, offset));
2422}
2423
2424void LocationsBuilderX86::VisitBoundsCheck(HBoundsCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002425 LocationSummary* locations =
2426 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002427 locations->SetInAt(0, Location::RequiresRegister());
2428 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002429 if (instruction->HasUses()) {
2430 locations->SetOut(Location::SameAsFirstInput());
2431 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002432}
2433
2434void InstructionCodeGeneratorX86::VisitBoundsCheck(HBoundsCheck* instruction) {
2435 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01002436 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) BoundsCheckSlowPathX86(
Nicolas Geoffray39468442014-09-02 15:17:15 +01002437 instruction, locations->InAt(0), locations->InAt(1));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002438 codegen_->AddSlowPath(slow_path);
2439
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002440 Register index = locations->InAt(0).As<Register>();
2441 Register length = locations->InAt(1).As<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002442
2443 __ cmpl(index, length);
2444 __ j(kAboveEqual, slow_path->GetEntryLabel());
2445}
2446
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002447void LocationsBuilderX86::VisitTemporary(HTemporary* temp) {
2448 temp->SetLocations(nullptr);
2449}
2450
2451void InstructionCodeGeneratorX86::VisitTemporary(HTemporary* temp) {
2452 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002453 UNUSED(temp);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002454}
2455
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01002456void LocationsBuilderX86::VisitParallelMove(HParallelMove* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002457 UNUSED(instruction);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002458 LOG(FATAL) << "Unreachable";
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01002459}
2460
2461void InstructionCodeGeneratorX86::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002462 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
2463}
2464
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00002465void LocationsBuilderX86::VisitSuspendCheck(HSuspendCheck* instruction) {
2466 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
2467}
2468
2469void InstructionCodeGeneratorX86::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01002470 HBasicBlock* block = instruction->GetBlock();
2471 if (block->GetLoopInformation() != nullptr) {
2472 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
2473 // The back edge will generate the suspend check.
2474 return;
2475 }
2476 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
2477 // The goto will generate the suspend check.
2478 return;
2479 }
2480 GenerateSuspendCheck(instruction, nullptr);
2481}
2482
2483void InstructionCodeGeneratorX86::GenerateSuspendCheck(HSuspendCheck* instruction,
2484 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00002485 SuspendCheckSlowPathX86* slow_path =
Nicolas Geoffray3c049742014-09-24 18:10:46 +01002486 new (GetGraph()->GetArena()) SuspendCheckSlowPathX86(instruction, successor);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00002487 codegen_->AddSlowPath(slow_path);
Nicolas Geoffray3c049742014-09-24 18:10:46 +01002488 __ fs()->cmpw(Address::Absolute(
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00002489 Thread::ThreadFlagsOffset<kX86WordSize>().Int32Value()), Immediate(0));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01002490 if (successor == nullptr) {
2491 __ j(kNotEqual, slow_path->GetEntryLabel());
2492 __ Bind(slow_path->GetReturnLabel());
2493 } else {
2494 __ j(kEqual, codegen_->GetLabelOf(successor));
2495 __ jmp(slow_path->GetEntryLabel());
2496 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00002497}
2498
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002499X86Assembler* ParallelMoveResolverX86::GetAssembler() const {
2500 return codegen_->GetAssembler();
2501}
2502
2503void ParallelMoveResolverX86::MoveMemoryToMemory(int dst, int src) {
2504 ScratchRegisterScope ensure_scratch(
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01002505 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002506 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
2507 __ movl(static_cast<Register>(ensure_scratch.GetRegister()), Address(ESP, src + stack_offset));
2508 __ movl(Address(ESP, dst + stack_offset), static_cast<Register>(ensure_scratch.GetRegister()));
2509}
2510
2511void ParallelMoveResolverX86::EmitMove(size_t index) {
2512 MoveOperands* move = moves_.Get(index);
2513 Location source = move->GetSource();
2514 Location destination = move->GetDestination();
2515
2516 if (source.IsRegister()) {
2517 if (destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002518 __ movl(destination.As<Register>(), source.As<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002519 } else {
2520 DCHECK(destination.IsStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002521 __ movl(Address(ESP, destination.GetStackIndex()), source.As<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002522 }
2523 } else if (source.IsStackSlot()) {
2524 if (destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002525 __ movl(destination.As<Register>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002526 } else {
2527 DCHECK(destination.IsStackSlot());
2528 MoveMemoryToMemory(destination.GetStackIndex(),
2529 source.GetStackIndex());
2530 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002531 } else if (source.IsConstant()) {
2532 HIntConstant* instruction = source.GetConstant()->AsIntConstant();
2533 Immediate imm(instruction->AsIntConstant()->GetValue());
2534 if (destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002535 __ movl(destination.As<Register>(), imm);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002536 } else {
2537 __ movl(Address(ESP, destination.GetStackIndex()), imm);
2538 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002539 } else {
2540 LOG(FATAL) << "Unimplemented";
2541 }
2542}
2543
2544void ParallelMoveResolverX86::Exchange(Register reg, int mem) {
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01002545 Register suggested_scratch = reg == EAX ? EBX : EAX;
2546 ScratchRegisterScope ensure_scratch(
2547 this, reg, suggested_scratch, codegen_->GetNumberOfCoreRegisters());
2548
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002549 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
2550 __ movl(static_cast<Register>(ensure_scratch.GetRegister()), Address(ESP, mem + stack_offset));
2551 __ movl(Address(ESP, mem + stack_offset), reg);
2552 __ movl(reg, static_cast<Register>(ensure_scratch.GetRegister()));
2553}
2554
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002555void ParallelMoveResolverX86::Exchange(int mem1, int mem2) {
2556 ScratchRegisterScope ensure_scratch1(
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01002557 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
2558
2559 Register suggested_scratch = ensure_scratch1.GetRegister() == EAX ? EBX : EAX;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002560 ScratchRegisterScope ensure_scratch2(
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01002561 this, ensure_scratch1.GetRegister(), suggested_scratch, codegen_->GetNumberOfCoreRegisters());
2562
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002563 int stack_offset = ensure_scratch1.IsSpilled() ? kX86WordSize : 0;
2564 stack_offset += ensure_scratch2.IsSpilled() ? kX86WordSize : 0;
2565 __ movl(static_cast<Register>(ensure_scratch1.GetRegister()), Address(ESP, mem1 + stack_offset));
2566 __ movl(static_cast<Register>(ensure_scratch2.GetRegister()), Address(ESP, mem2 + stack_offset));
2567 __ movl(Address(ESP, mem2 + stack_offset), static_cast<Register>(ensure_scratch1.GetRegister()));
2568 __ movl(Address(ESP, mem1 + stack_offset), static_cast<Register>(ensure_scratch2.GetRegister()));
2569}
2570
2571void ParallelMoveResolverX86::EmitSwap(size_t index) {
2572 MoveOperands* move = moves_.Get(index);
2573 Location source = move->GetSource();
2574 Location destination = move->GetDestination();
2575
2576 if (source.IsRegister() && destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002577 __ xchgl(destination.As<Register>(), source.As<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002578 } else if (source.IsRegister() && destination.IsStackSlot()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002579 Exchange(source.As<Register>(), destination.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002580 } else if (source.IsStackSlot() && destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002581 Exchange(destination.As<Register>(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002582 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
2583 Exchange(destination.GetStackIndex(), source.GetStackIndex());
2584 } else {
2585 LOG(FATAL) << "Unimplemented";
2586 }
2587}
2588
2589void ParallelMoveResolverX86::SpillScratch(int reg) {
2590 __ pushl(static_cast<Register>(reg));
2591}
2592
2593void ParallelMoveResolverX86::RestoreScratch(int reg) {
2594 __ popl(static_cast<Register>(reg));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01002595}
2596
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002597void LocationsBuilderX86::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002598 LocationSummary::CallKind call_kind = cls->CanCallRuntime()
2599 ? LocationSummary::kCallOnSlowPath
2600 : LocationSummary::kNoCall;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002601 LocationSummary* locations =
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002602 new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002603 locations->SetOut(Location::RequiresRegister());
2604}
2605
2606void InstructionCodeGeneratorX86::VisitLoadClass(HLoadClass* cls) {
2607 Register out = cls->GetLocations()->Out().As<Register>();
2608 if (cls->IsReferrersClass()) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002609 DCHECK(!cls->CanCallRuntime());
2610 DCHECK(!cls->MustGenerateClinitCheck());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002611 codegen_->LoadCurrentMethod(out);
2612 __ movl(out, Address(out, mirror::ArtMethod::DeclaringClassOffset().Int32Value()));
2613 } else {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002614 DCHECK(cls->CanCallRuntime());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002615 codegen_->LoadCurrentMethod(out);
2616 __ movl(out, Address(out, mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value()));
2617 __ movl(out, Address(out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex())));
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002618
2619 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86(
2620 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
2621 codegen_->AddSlowPath(slow_path);
2622 __ testl(out, out);
2623 __ j(kEqual, slow_path->GetEntryLabel());
2624 if (cls->MustGenerateClinitCheck()) {
2625 GenerateClassInitializationCheck(slow_path, out);
2626 } else {
2627 __ Bind(slow_path->GetExitLabel());
2628 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002629 }
2630}
2631
2632void LocationsBuilderX86::VisitClinitCheck(HClinitCheck* check) {
2633 LocationSummary* locations =
2634 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
2635 locations->SetInAt(0, Location::RequiresRegister());
2636 if (check->HasUses()) {
2637 locations->SetOut(Location::SameAsFirstInput());
2638 }
2639}
2640
2641void InstructionCodeGeneratorX86::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002642 // We assume the class to not be null.
2643 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86(
2644 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002645 codegen_->AddSlowPath(slow_path);
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002646 GenerateClassInitializationCheck(slow_path, check->GetLocations()->InAt(0).As<Register>());
2647}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002648
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002649void InstructionCodeGeneratorX86::GenerateClassInitializationCheck(
2650 SlowPathCodeX86* slow_path, Register class_reg) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002651 __ cmpl(Address(class_reg, mirror::Class::StatusOffset().Int32Value()),
2652 Immediate(mirror::Class::kStatusInitialized));
2653 __ j(kLess, slow_path->GetEntryLabel());
2654 __ Bind(slow_path->GetExitLabel());
2655 // No need for memory fence, thanks to the X86 memory model.
2656}
2657
2658void LocationsBuilderX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
2659 LocationSummary* locations =
2660 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2661 locations->SetInAt(0, Location::RequiresRegister());
2662 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2663}
2664
2665void InstructionCodeGeneratorX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
2666 LocationSummary* locations = instruction->GetLocations();
2667 Register cls = locations->InAt(0).As<Register>();
2668 uint32_t offset = instruction->GetFieldOffset().Uint32Value();
2669
2670 switch (instruction->GetType()) {
2671 case Primitive::kPrimBoolean: {
2672 Register out = locations->Out().As<Register>();
2673 __ movzxb(out, Address(cls, offset));
2674 break;
2675 }
2676
2677 case Primitive::kPrimByte: {
2678 Register out = locations->Out().As<Register>();
2679 __ movsxb(out, Address(cls, offset));
2680 break;
2681 }
2682
2683 case Primitive::kPrimShort: {
2684 Register out = locations->Out().As<Register>();
2685 __ movsxw(out, Address(cls, offset));
2686 break;
2687 }
2688
2689 case Primitive::kPrimChar: {
2690 Register out = locations->Out().As<Register>();
2691 __ movzxw(out, Address(cls, offset));
2692 break;
2693 }
2694
2695 case Primitive::kPrimInt:
2696 case Primitive::kPrimNot: {
2697 Register out = locations->Out().As<Register>();
2698 __ movl(out, Address(cls, offset));
2699 break;
2700 }
2701
2702 case Primitive::kPrimLong: {
2703 // TODO: support volatile.
2704 __ movl(locations->Out().AsRegisterPairLow<Register>(), Address(cls, offset));
2705 __ movl(locations->Out().AsRegisterPairHigh<Register>(), Address(cls, kX86WordSize + offset));
2706 break;
2707 }
2708
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002709 case Primitive::kPrimFloat: {
2710 XmmRegister out = locations->Out().As<XmmRegister>();
2711 __ movss(out, Address(cls, offset));
2712 break;
2713 }
2714
2715 case Primitive::kPrimDouble: {
2716 XmmRegister out = locations->Out().As<XmmRegister>();
2717 __ movsd(out, Address(cls, offset));
2718 break;
2719 }
2720
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002721 case Primitive::kPrimVoid:
2722 LOG(FATAL) << "Unreachable type " << instruction->GetType();
2723 UNREACHABLE();
2724 }
2725}
2726
2727void LocationsBuilderX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
2728 LocationSummary* locations =
2729 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2730 locations->SetInAt(0, Location::RequiresRegister());
2731 Primitive::Type field_type = instruction->GetFieldType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002732 bool needs_write_barrier =
2733 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002734 bool is_byte_type = (field_type == Primitive::kPrimBoolean)
2735 || (field_type == Primitive::kPrimByte);
2736 // The register allocator does not support multiple
2737 // inputs that die at entry with one in a specific register.
2738 if (is_byte_type) {
2739 // Ensure the value is in a byte register.
2740 locations->SetInAt(1, Location::RegisterLocation(EAX));
2741 } else {
2742 locations->SetInAt(1, Location::RequiresRegister());
2743 }
2744 // Temporary registers for the write barrier.
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002745 if (needs_write_barrier) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002746 locations->AddTemp(Location::RequiresRegister());
2747 // Ensure the card is in a byte register.
2748 locations->AddTemp(Location::RegisterLocation(ECX));
2749 }
2750}
2751
2752void InstructionCodeGeneratorX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
2753 LocationSummary* locations = instruction->GetLocations();
2754 Register cls = locations->InAt(0).As<Register>();
2755 uint32_t offset = instruction->GetFieldOffset().Uint32Value();
2756 Primitive::Type field_type = instruction->GetFieldType();
2757
2758 switch (field_type) {
2759 case Primitive::kPrimBoolean:
2760 case Primitive::kPrimByte: {
2761 ByteRegister value = locations->InAt(1).As<ByteRegister>();
2762 __ movb(Address(cls, offset), value);
2763 break;
2764 }
2765
2766 case Primitive::kPrimShort:
2767 case Primitive::kPrimChar: {
2768 Register value = locations->InAt(1).As<Register>();
2769 __ movw(Address(cls, offset), value);
2770 break;
2771 }
2772
2773 case Primitive::kPrimInt:
2774 case Primitive::kPrimNot: {
2775 Register value = locations->InAt(1).As<Register>();
2776 __ movl(Address(cls, offset), value);
2777
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002778 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002779 Register temp = locations->GetTemp(0).As<Register>();
2780 Register card = locations->GetTemp(1).As<Register>();
2781 codegen_->MarkGCCard(temp, card, cls, value);
2782 }
2783 break;
2784 }
2785
2786 case Primitive::kPrimLong: {
2787 Location value = locations->InAt(1);
2788 __ movl(Address(cls, offset), value.AsRegisterPairLow<Register>());
2789 __ movl(Address(cls, kX86WordSize + offset), value.AsRegisterPairHigh<Register>());
2790 break;
2791 }
2792
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002793 case Primitive::kPrimFloat: {
2794 XmmRegister value = locations->InAt(1).As<XmmRegister>();
2795 __ movss(Address(cls, offset), value);
2796 break;
2797 }
2798
2799 case Primitive::kPrimDouble: {
2800 XmmRegister value = locations->InAt(1).As<XmmRegister>();
2801 __ movsd(Address(cls, offset), value);
2802 break;
2803 }
2804
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002805 case Primitive::kPrimVoid:
2806 LOG(FATAL) << "Unreachable type " << field_type;
2807 UNREACHABLE();
2808 }
2809}
2810
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00002811void LocationsBuilderX86::VisitLoadString(HLoadString* load) {
2812 LocationSummary* locations =
2813 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kCallOnSlowPath);
2814 locations->SetOut(Location::RequiresRegister());
2815}
2816
2817void InstructionCodeGeneratorX86::VisitLoadString(HLoadString* load) {
2818 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathX86(load);
2819 codegen_->AddSlowPath(slow_path);
2820
2821 Register out = load->GetLocations()->Out().As<Register>();
2822 codegen_->LoadCurrentMethod(out);
2823 __ movl(out, Address(out, mirror::ArtMethod::DexCacheStringsOffset().Int32Value()));
2824 __ movl(out, Address(out, CodeGenerator::GetCacheOffset(load->GetStringIndex())));
2825 __ testl(out, out);
2826 __ j(kEqual, slow_path->GetEntryLabel());
2827 __ Bind(slow_path->GetExitLabel());
2828}
2829
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002830void LocationsBuilderX86::VisitLoadException(HLoadException* load) {
2831 LocationSummary* locations =
2832 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
2833 locations->SetOut(Location::RequiresRegister());
2834}
2835
2836void InstructionCodeGeneratorX86::VisitLoadException(HLoadException* load) {
2837 Address address = Address::Absolute(Thread::ExceptionOffset<kX86WordSize>().Int32Value());
2838 __ fs()->movl(load->GetLocations()->Out().As<Register>(), address);
2839 __ fs()->movl(address, Immediate(0));
2840}
2841
2842void LocationsBuilderX86::VisitThrow(HThrow* instruction) {
2843 LocationSummary* locations =
2844 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
2845 InvokeRuntimeCallingConvention calling_convention;
2846 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2847}
2848
2849void InstructionCodeGeneratorX86::VisitThrow(HThrow* instruction) {
2850 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pDeliverException)));
2851 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
2852}
2853
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00002854void LocationsBuilderX86::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00002855 LocationSummary::CallKind call_kind = instruction->IsClassFinal()
2856 ? LocationSummary::kNoCall
2857 : LocationSummary::kCallOnSlowPath;
2858 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
2859 locations->SetInAt(0, Location::RequiresRegister());
2860 locations->SetInAt(1, Location::Any());
2861 locations->SetOut(Location::RequiresRegister());
2862}
2863
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00002864void InstructionCodeGeneratorX86::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00002865 LocationSummary* locations = instruction->GetLocations();
2866 Register obj = locations->InAt(0).As<Register>();
2867 Location cls = locations->InAt(1);
2868 Register out = locations->Out().As<Register>();
2869 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2870 Label done, zero;
2871 SlowPathCodeX86* slow_path = nullptr;
2872
2873 // Return 0 if `obj` is null.
2874 // TODO: avoid this check if we know obj is not null.
2875 __ testl(obj, obj);
2876 __ j(kEqual, &zero);
2877 __ movl(out, Address(obj, class_offset));
2878 // Compare the class of `obj` with `cls`.
2879 if (cls.IsRegister()) {
2880 __ cmpl(out, cls.As<Register>());
2881 } else {
2882 DCHECK(cls.IsStackSlot()) << cls;
2883 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
2884 }
2885
2886 if (instruction->IsClassFinal()) {
2887 // Classes must be equal for the instanceof to succeed.
2888 __ j(kNotEqual, &zero);
2889 __ movl(out, Immediate(1));
2890 __ jmp(&done);
2891 } else {
2892 // If the classes are not equal, we go into a slow path.
2893 DCHECK(locations->OnlyCallsOnSlowPath());
2894 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86(
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00002895 instruction, locations->InAt(1), locations->Out(), instruction->GetDexPc());
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00002896 codegen_->AddSlowPath(slow_path);
2897 __ j(kNotEqual, slow_path->GetEntryLabel());
2898 __ movl(out, Immediate(1));
2899 __ jmp(&done);
2900 }
2901 __ Bind(&zero);
2902 __ movl(out, Immediate(0));
2903 if (slow_path != nullptr) {
2904 __ Bind(slow_path->GetExitLabel());
2905 }
2906 __ Bind(&done);
2907}
2908
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00002909void LocationsBuilderX86::VisitCheckCast(HCheckCast* instruction) {
2910 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
2911 instruction, LocationSummary::kCallOnSlowPath);
2912 locations->SetInAt(0, Location::RequiresRegister());
2913 locations->SetInAt(1, Location::Any());
2914 locations->AddTemp(Location::RequiresRegister());
2915}
2916
2917void InstructionCodeGeneratorX86::VisitCheckCast(HCheckCast* instruction) {
2918 LocationSummary* locations = instruction->GetLocations();
2919 Register obj = locations->InAt(0).As<Register>();
2920 Location cls = locations->InAt(1);
2921 Register temp = locations->GetTemp(0).As<Register>();
2922 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2923 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86(
2924 instruction, locations->InAt(1), locations->GetTemp(0), instruction->GetDexPc());
2925 codegen_->AddSlowPath(slow_path);
2926
2927 // TODO: avoid this check if we know obj is not null.
2928 __ testl(obj, obj);
2929 __ j(kEqual, slow_path->GetExitLabel());
2930 __ movl(temp, Address(obj, class_offset));
2931
2932 // Compare the class of `obj` with `cls`.
2933 if (cls.IsRegister()) {
2934 __ cmpl(temp, cls.As<Register>());
2935 } else {
2936 DCHECK(cls.IsStackSlot()) << cls;
2937 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
2938 }
2939
2940 __ j(kNotEqual, slow_path->GetEntryLabel());
2941 __ Bind(slow_path->GetExitLabel());
2942}
2943
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00002944void LocationsBuilderX86::VisitMonitorOperation(HMonitorOperation* instruction) {
2945 LocationSummary* locations =
2946 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
2947 InvokeRuntimeCallingConvention calling_convention;
2948 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2949}
2950
2951void InstructionCodeGeneratorX86::VisitMonitorOperation(HMonitorOperation* instruction) {
2952 __ fs()->call(Address::Absolute(instruction->IsEnter()
2953 ? QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pLockObject)
2954 : QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pUnlockObject)));
2955 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
2956}
2957
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002958void LocationsBuilderX86::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction); }
2959void LocationsBuilderX86::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction); }
2960void LocationsBuilderX86::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction); }
2961
2962void LocationsBuilderX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
2963 LocationSummary* locations =
2964 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2965 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
2966 || instruction->GetResultType() == Primitive::kPrimLong);
2967 locations->SetInAt(0, Location::RequiresRegister());
2968 locations->SetInAt(1, Location::Any());
2969 locations->SetOut(Location::SameAsFirstInput());
2970}
2971
2972void InstructionCodeGeneratorX86::VisitAnd(HAnd* instruction) {
2973 HandleBitwiseOperation(instruction);
2974}
2975
2976void InstructionCodeGeneratorX86::VisitOr(HOr* instruction) {
2977 HandleBitwiseOperation(instruction);
2978}
2979
2980void InstructionCodeGeneratorX86::VisitXor(HXor* instruction) {
2981 HandleBitwiseOperation(instruction);
2982}
2983
2984void InstructionCodeGeneratorX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
2985 LocationSummary* locations = instruction->GetLocations();
2986 Location first = locations->InAt(0);
2987 Location second = locations->InAt(1);
2988 DCHECK(first.Equals(locations->Out()));
2989
2990 if (instruction->GetResultType() == Primitive::kPrimInt) {
2991 if (second.IsRegister()) {
2992 if (instruction->IsAnd()) {
2993 __ andl(first.As<Register>(), second.As<Register>());
2994 } else if (instruction->IsOr()) {
2995 __ orl(first.As<Register>(), second.As<Register>());
2996 } else {
2997 DCHECK(instruction->IsXor());
2998 __ xorl(first.As<Register>(), second.As<Register>());
2999 }
3000 } else if (second.IsConstant()) {
3001 if (instruction->IsAnd()) {
3002 __ andl(first.As<Register>(), Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
3003 } else if (instruction->IsOr()) {
3004 __ orl(first.As<Register>(), Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
3005 } else {
3006 DCHECK(instruction->IsXor());
3007 __ xorl(first.As<Register>(), Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
3008 }
3009 } else {
3010 if (instruction->IsAnd()) {
3011 __ andl(first.As<Register>(), Address(ESP, second.GetStackIndex()));
3012 } else if (instruction->IsOr()) {
3013 __ orl(first.As<Register>(), Address(ESP, second.GetStackIndex()));
3014 } else {
3015 DCHECK(instruction->IsXor());
3016 __ xorl(first.As<Register>(), Address(ESP, second.GetStackIndex()));
3017 }
3018 }
3019 } else {
3020 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
3021 if (second.IsRegisterPair()) {
3022 if (instruction->IsAnd()) {
3023 __ andl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
3024 __ andl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
3025 } else if (instruction->IsOr()) {
3026 __ orl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
3027 __ orl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
3028 } else {
3029 DCHECK(instruction->IsXor());
3030 __ xorl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
3031 __ xorl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
3032 }
3033 } else {
3034 if (instruction->IsAnd()) {
3035 __ andl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
3036 __ andl(first.AsRegisterPairHigh<Register>(),
3037 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
3038 } else if (instruction->IsOr()) {
3039 __ orl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
3040 __ orl(first.AsRegisterPairHigh<Register>(),
3041 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
3042 } else {
3043 DCHECK(instruction->IsXor());
3044 __ xorl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
3045 __ xorl(first.AsRegisterPairHigh<Register>(),
3046 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
3047 }
3048 }
3049 }
3050}
3051
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00003052} // namespace x86
3053} // namespace art