blob: 499f068a276355e475636f786261d4bab49eb151 [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_arm.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000018
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070019#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 Geoffray8ccc3f52014-03-19 10:34:11 +000022#include "mirror/art_method.h"
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +010023#include "mirror/class.h"
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070024#include "thread.h"
Nicolas Geoffray9cf35522014-06-09 18:40:10 +010025#include "utils/arm/assembler_arm.h"
26#include "utils/arm/managed_register_arm.h"
Roland Levillain946e1432014-11-11 17:35:19 +000027#include "utils/assembler.h"
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010028#include "utils/stack_checks.h"
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +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 arm {
33
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +000034static DRegister FromLowSToD(SRegister reg) {
35 DCHECK_EQ(reg % 2, 0);
36 return static_cast<DRegister>(reg / 2);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +010037}
38
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010039static constexpr bool kExplicitStackOverflowCheck = false;
40
41static constexpr int kNumberOfPushedRegistersAtEntry = 1 + 2; // LR, R6, R7
42static constexpr int kCurrentMethodStackOffset = 0;
43
Calin Juravled6fb6cf2014-11-11 19:07:44 +000044static constexpr Register kRuntimeParameterCoreRegisters[] = { R0, R1, R2, R3 };
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +010045static constexpr size_t kRuntimeParameterCoreRegistersLength =
46 arraysize(kRuntimeParameterCoreRegisters);
Calin Juravled2ec87d2014-12-08 14:24:46 +000047static constexpr SRegister kRuntimeParameterFpuRegisters[] = { S0, S1, S2, S3 };
Roland Levillain624279f2014-12-04 11:54:28 +000048static constexpr size_t kRuntimeParameterFpuRegistersLength =
49 arraysize(kRuntimeParameterFpuRegisters);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +010050
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +000051class InvokeRuntimeCallingConvention : public CallingConvention<Register, SRegister> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +010052 public:
53 InvokeRuntimeCallingConvention()
54 : CallingConvention(kRuntimeParameterCoreRegisters,
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +010055 kRuntimeParameterCoreRegistersLength,
56 kRuntimeParameterFpuRegisters,
57 kRuntimeParameterFpuRegistersLength) {}
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +010058
59 private:
60 DISALLOW_COPY_AND_ASSIGN(InvokeRuntimeCallingConvention);
61};
62
Nicolas Geoffraye5038322014-07-04 09:41:32 +010063#define __ reinterpret_cast<ArmAssembler*>(codegen->GetAssembler())->
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +010064#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kArmWordSize, x).Int32Value()
Nicolas Geoffraye5038322014-07-04 09:41:32 +010065
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +010066class SlowPathCodeARM : public SlowPathCode {
67 public:
68 SlowPathCodeARM() : entry_label_(), exit_label_() {}
69
70 Label* GetEntryLabel() { return &entry_label_; }
71 Label* GetExitLabel() { return &exit_label_; }
72
73 private:
74 Label entry_label_;
75 Label exit_label_;
76
77 DISALLOW_COPY_AND_ASSIGN(SlowPathCodeARM);
78};
79
80class NullCheckSlowPathARM : public SlowPathCodeARM {
Nicolas Geoffraye5038322014-07-04 09:41:32 +010081 public:
Nicolas Geoffray39468442014-09-02 15:17:15 +010082 explicit NullCheckSlowPathARM(HNullCheck* instruction) : instruction_(instruction) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +010083
Alexandre Rames67555f72014-11-18 10:55:16 +000084 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +010085 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
Nicolas Geoffraye5038322014-07-04 09:41:32 +010086 __ Bind(GetEntryLabel());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +010087 arm_codegen->InvokeRuntime(
88 QUICK_ENTRY_POINT(pThrowNullPointer), instruction_, instruction_->GetDexPc());
Nicolas Geoffraye5038322014-07-04 09:41:32 +010089 }
90
91 private:
Nicolas Geoffray39468442014-09-02 15:17:15 +010092 HNullCheck* const instruction_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +010093 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathARM);
94};
95
Calin Juravled0d48522014-11-04 16:40:20 +000096class DivZeroCheckSlowPathARM : public SlowPathCodeARM {
97 public:
98 explicit DivZeroCheckSlowPathARM(HDivZeroCheck* instruction) : instruction_(instruction) {}
99
Alexandre Rames67555f72014-11-18 10:55:16 +0000100 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Calin Juravled0d48522014-11-04 16:40:20 +0000101 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
102 __ Bind(GetEntryLabel());
103 arm_codegen->InvokeRuntime(
104 QUICK_ENTRY_POINT(pThrowDivZero), instruction_, instruction_->GetDexPc());
105 }
106
107 private:
108 HDivZeroCheck* const instruction_;
109 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathARM);
110};
111
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100112class StackOverflowCheckSlowPathARM : public SlowPathCodeARM {
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100113 public:
114 StackOverflowCheckSlowPathARM() {}
115
Alexandre Rames67555f72014-11-18 10:55:16 +0000116 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100117 __ Bind(GetEntryLabel());
118 __ LoadFromOffset(kLoadWord, PC, TR,
119 QUICK_ENTRYPOINT_OFFSET(kArmWordSize, pThrowStackOverflow).Int32Value());
120 }
121
122 private:
123 DISALLOW_COPY_AND_ASSIGN(StackOverflowCheckSlowPathARM);
124};
125
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100126class SuspendCheckSlowPathARM : public SlowPathCodeARM {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000127 public:
Alexandre Rames67555f72014-11-18 10:55:16 +0000128 SuspendCheckSlowPathARM(HSuspendCheck* instruction, HBasicBlock* successor)
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100129 : instruction_(instruction), successor_(successor) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000130
Alexandre Rames67555f72014-11-18 10:55:16 +0000131 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100132 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000133 __ Bind(GetEntryLabel());
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100134 codegen->SaveLiveRegisters(instruction_->GetLocations());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100135 arm_codegen->InvokeRuntime(
136 QUICK_ENTRY_POINT(pTestSuspend), instruction_, instruction_->GetDexPc());
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100137 codegen->RestoreLiveRegisters(instruction_->GetLocations());
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100138 if (successor_ == nullptr) {
139 __ b(GetReturnLabel());
140 } else {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100141 __ b(arm_codegen->GetLabelOf(successor_));
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100142 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000143 }
144
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100145 Label* GetReturnLabel() {
146 DCHECK(successor_ == nullptr);
147 return &return_label_;
148 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000149
150 private:
151 HSuspendCheck* const instruction_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100152 // If not null, the block to branch to after the suspend check.
153 HBasicBlock* const successor_;
154
155 // If `successor_` is null, the label to branch to after the suspend check.
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000156 Label return_label_;
157
158 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathARM);
159};
160
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100161class BoundsCheckSlowPathARM : public SlowPathCodeARM {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100162 public:
Roland Levillain5799fc02014-09-25 12:15:20 +0100163 BoundsCheckSlowPathARM(HBoundsCheck* instruction,
164 Location index_location,
165 Location length_location)
Nicolas Geoffray39468442014-09-02 15:17:15 +0100166 : instruction_(instruction),
167 index_location_(index_location),
168 length_location_(length_location) {}
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100169
Alexandre Rames67555f72014-11-18 10:55:16 +0000170 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100171 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100172 __ Bind(GetEntryLabel());
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000173 // We're moving two locations to locations that could overlap, so we need a parallel
174 // move resolver.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100175 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000176 codegen->EmitParallelMoves(
177 index_location_,
178 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
179 length_location_,
180 Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100181 arm_codegen->InvokeRuntime(
182 QUICK_ENTRY_POINT(pThrowArrayBounds), instruction_, instruction_->GetDexPc());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100183 }
184
185 private:
Nicolas Geoffray39468442014-09-02 15:17:15 +0100186 HBoundsCheck* const instruction_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100187 const Location index_location_;
188 const Location length_location_;
189
190 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathARM);
191};
192
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000193class LoadClassSlowPathARM : public SlowPathCodeARM {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100194 public:
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000195 LoadClassSlowPathARM(HLoadClass* cls,
196 HInstruction* at,
197 uint32_t dex_pc,
198 bool do_clinit)
199 : cls_(cls), at_(at), dex_pc_(dex_pc), do_clinit_(do_clinit) {
200 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
201 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100202
Alexandre Rames67555f72014-11-18 10:55:16 +0000203 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000204 LocationSummary* locations = at_->GetLocations();
205
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100206 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
207 __ Bind(GetEntryLabel());
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000208 codegen->SaveLiveRegisters(locations);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100209
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100210 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000211 __ LoadImmediate(calling_convention.GetRegisterAt(0), cls_->GetTypeIndex());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100212 arm_codegen->LoadCurrentMethod(calling_convention.GetRegisterAt(1));
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000213 int32_t entry_point_offset = do_clinit_
214 ? QUICK_ENTRY_POINT(pInitializeStaticStorage)
215 : QUICK_ENTRY_POINT(pInitializeType);
216 arm_codegen->InvokeRuntime(entry_point_offset, at_, dex_pc_);
217
218 // Move the class to the desired location.
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000219 Location out = locations->Out();
220 if (out.IsValid()) {
221 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000222 arm_codegen->Move32(locations->Out(), Location::RegisterLocation(R0));
223 }
224 codegen->RestoreLiveRegisters(locations);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100225 __ b(GetExitLabel());
226 }
227
228 private:
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000229 // The class this slow path will load.
230 HLoadClass* const cls_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100231
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000232 // The instruction where this slow path is happening.
233 // (Might be the load class or an initialization check).
234 HInstruction* const at_;
235
236 // The dex PC of `at_`.
237 const uint32_t dex_pc_;
238
239 // Whether to initialize the class.
240 const bool do_clinit_;
241
242 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathARM);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100243};
244
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000245class LoadStringSlowPathARM : public SlowPathCodeARM {
246 public:
247 explicit LoadStringSlowPathARM(HLoadString* instruction) : instruction_(instruction) {}
248
Alexandre Rames67555f72014-11-18 10:55:16 +0000249 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000250 LocationSummary* locations = instruction_->GetLocations();
251 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
252
253 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
254 __ Bind(GetEntryLabel());
255 codegen->SaveLiveRegisters(locations);
256
257 InvokeRuntimeCallingConvention calling_convention;
258 arm_codegen->LoadCurrentMethod(calling_convention.GetRegisterAt(0));
259 __ LoadImmediate(calling_convention.GetRegisterAt(1), instruction_->GetStringIndex());
260 arm_codegen->InvokeRuntime(
261 QUICK_ENTRY_POINT(pResolveString), instruction_, instruction_->GetDexPc());
262 arm_codegen->Move32(locations->Out(), Location::RegisterLocation(R0));
263
264 codegen->RestoreLiveRegisters(locations);
265 __ b(GetExitLabel());
266 }
267
268 private:
269 HLoadString* const instruction_;
270
271 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathARM);
272};
273
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000274class TypeCheckSlowPathARM : public SlowPathCodeARM {
275 public:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000276 TypeCheckSlowPathARM(HInstruction* instruction,
277 Location class_to_check,
278 Location object_class,
279 uint32_t dex_pc)
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000280 : instruction_(instruction),
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000281 class_to_check_(class_to_check),
282 object_class_(object_class),
283 dex_pc_(dex_pc) {}
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000284
Alexandre Rames67555f72014-11-18 10:55:16 +0000285 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000286 LocationSummary* locations = instruction_->GetLocations();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000287 DCHECK(instruction_->IsCheckCast()
288 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000289
290 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
291 __ Bind(GetEntryLabel());
292 codegen->SaveLiveRegisters(locations);
293
294 // We're moving two locations to locations that could overlap, so we need a parallel
295 // move resolver.
296 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000297 codegen->EmitParallelMoves(
298 class_to_check_,
299 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
300 object_class_,
301 Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000302
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000303 if (instruction_->IsInstanceOf()) {
304 arm_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pInstanceofNonTrivial), instruction_, dex_pc_);
305 arm_codegen->Move32(locations->Out(), Location::RegisterLocation(R0));
306 } else {
307 DCHECK(instruction_->IsCheckCast());
308 arm_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pCheckCast), instruction_, dex_pc_);
309 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000310
311 codegen->RestoreLiveRegisters(locations);
312 __ b(GetExitLabel());
313 }
314
315 private:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000316 HInstruction* const instruction_;
317 const Location class_to_check_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000318 const Location object_class_;
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000319 uint32_t dex_pc_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000320
321 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathARM);
322};
323
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000324#undef __
325
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100326#undef __
327#define __ reinterpret_cast<ArmAssembler*>(GetAssembler())->
Dave Allison20dfc792014-06-16 20:44:29 -0700328
329inline Condition ARMCondition(IfCondition cond) {
330 switch (cond) {
331 case kCondEQ: return EQ;
332 case kCondNE: return NE;
333 case kCondLT: return LT;
334 case kCondLE: return LE;
335 case kCondGT: return GT;
336 case kCondGE: return GE;
337 default:
338 LOG(FATAL) << "Unknown if condition";
339 }
340 return EQ; // Unreachable.
341}
342
343inline Condition ARMOppositeCondition(IfCondition cond) {
344 switch (cond) {
345 case kCondEQ: return NE;
346 case kCondNE: return EQ;
347 case kCondLT: return GE;
348 case kCondLE: return GT;
349 case kCondGT: return LE;
350 case kCondGE: return LT;
351 default:
352 LOG(FATAL) << "Unknown if condition";
353 }
354 return EQ; // Unreachable.
355}
356
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100357void CodeGeneratorARM::DumpCoreRegister(std::ostream& stream, int reg) const {
358 stream << ArmManagedRegister::FromCoreRegister(Register(reg));
359}
360
361void CodeGeneratorARM::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000362 stream << ArmManagedRegister::FromSRegister(SRegister(reg));
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100363}
364
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100365size_t CodeGeneratorARM::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
366 __ StoreToOffset(kStoreWord, static_cast<Register>(reg_id), SP, stack_index);
367 return kArmWordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100368}
369
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100370size_t CodeGeneratorARM::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
371 __ LoadFromOffset(kLoadWord, static_cast<Register>(reg_id), SP, stack_index);
372 return kArmWordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100373}
374
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100375CodeGeneratorARM::CodeGeneratorARM(HGraph* graph)
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000376 : CodeGenerator(graph, kNumberOfCoreRegisters, kNumberOfSRegisters, kNumberOfRegisterPairs),
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100377 block_labels_(graph->GetArena(), 0),
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100378 location_builder_(graph, this),
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +0100379 instruction_visitor_(graph, this),
Nicolas Geoffray8d486732014-07-16 16:23:40 +0100380 move_resolver_(graph->GetArena(), this),
381 assembler_(true) {}
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100382
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100383size_t CodeGeneratorARM::FrameEntrySpillSize() const {
384 return kNumberOfPushedRegistersAtEntry * kArmWordSize;
385}
386
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100387Location CodeGeneratorARM::AllocateFreeRegister(Primitive::Type type) const {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100388 switch (type) {
389 case Primitive::kPrimLong: {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100390 size_t reg = FindFreeEntry(blocked_register_pairs_, kNumberOfRegisterPairs);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100391 ArmManagedRegister pair =
392 ArmManagedRegister::FromRegisterPair(static_cast<RegisterPair>(reg));
Calin Juravle34bacdf2014-10-07 20:23:36 +0100393 DCHECK(!blocked_core_registers_[pair.AsRegisterPairLow()]);
394 DCHECK(!blocked_core_registers_[pair.AsRegisterPairHigh()]);
395
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100396 blocked_core_registers_[pair.AsRegisterPairLow()] = true;
397 blocked_core_registers_[pair.AsRegisterPairHigh()] = true;
Calin Juravle34bacdf2014-10-07 20:23:36 +0100398 UpdateBlockedPairRegisters();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100399 return Location::RegisterPairLocation(pair.AsRegisterPairLow(), pair.AsRegisterPairHigh());
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100400 }
401
402 case Primitive::kPrimByte:
403 case Primitive::kPrimBoolean:
404 case Primitive::kPrimChar:
405 case Primitive::kPrimShort:
406 case Primitive::kPrimInt:
407 case Primitive::kPrimNot: {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100408 int reg = FindFreeEntry(blocked_core_registers_, kNumberOfCoreRegisters);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100409 // Block all register pairs that contain `reg`.
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100410 for (int i = 0; i < kNumberOfRegisterPairs; i++) {
411 ArmManagedRegister current =
412 ArmManagedRegister::FromRegisterPair(static_cast<RegisterPair>(i));
413 if (current.AsRegisterPairLow() == reg || current.AsRegisterPairHigh() == reg) {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100414 blocked_register_pairs_[i] = true;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100415 }
416 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100417 return Location::RegisterLocation(reg);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100418 }
419
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000420 case Primitive::kPrimFloat: {
421 int reg = FindFreeEntry(blocked_fpu_registers_, kNumberOfSRegisters);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100422 return Location::FpuRegisterLocation(reg);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100423 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100424
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000425 case Primitive::kPrimDouble: {
Nicolas Geoffray3c035032014-10-28 10:46:40 +0000426 int reg = FindTwoFreeConsecutiveAlignedEntries(blocked_fpu_registers_, kNumberOfSRegisters);
427 DCHECK_EQ(reg % 2, 0);
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000428 return Location::FpuRegisterPairLocation(reg, reg + 1);
429 }
430
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100431 case Primitive::kPrimVoid:
432 LOG(FATAL) << "Unreachable type " << type;
433 }
434
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100435 return Location();
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100436}
437
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100438void CodeGeneratorARM::SetupBlockedRegisters() const {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100439 // Don't allocate the dalvik style register pair passing.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100440 blocked_register_pairs_[R1_R2] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100441
442 // Stack register, LR and PC are always reserved.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100443 blocked_core_registers_[SP] = true;
444 blocked_core_registers_[LR] = true;
445 blocked_core_registers_[PC] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100446
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100447 // Reserve thread register.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100448 blocked_core_registers_[TR] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100449
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +0100450 // Reserve temp register.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100451 blocked_core_registers_[IP] = true;
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +0100452
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100453 // TODO: We currently don't use Quick's callee saved registers.
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100454 // We always save and restore R6 and R7 to make sure we can use three
455 // register pairs for long operations.
Nicolas Geoffray44b819e2014-11-06 12:00:54 +0000456 blocked_core_registers_[R4] = true;
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100457 blocked_core_registers_[R5] = true;
458 blocked_core_registers_[R8] = true;
459 blocked_core_registers_[R10] = true;
460 blocked_core_registers_[R11] = true;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100461
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000462 blocked_fpu_registers_[S16] = true;
463 blocked_fpu_registers_[S17] = true;
464 blocked_fpu_registers_[S18] = true;
465 blocked_fpu_registers_[S19] = true;
466 blocked_fpu_registers_[S20] = true;
467 blocked_fpu_registers_[S21] = true;
468 blocked_fpu_registers_[S22] = true;
469 blocked_fpu_registers_[S23] = true;
Nicolas Geoffray3c035032014-10-28 10:46:40 +0000470 blocked_fpu_registers_[S24] = true;
471 blocked_fpu_registers_[S25] = true;
472 blocked_fpu_registers_[S26] = true;
473 blocked_fpu_registers_[S27] = true;
474 blocked_fpu_registers_[S28] = true;
475 blocked_fpu_registers_[S29] = true;
476 blocked_fpu_registers_[S30] = true;
477 blocked_fpu_registers_[S31] = true;
Calin Juravle34bacdf2014-10-07 20:23:36 +0100478
479 UpdateBlockedPairRegisters();
480}
481
482void CodeGeneratorARM::UpdateBlockedPairRegisters() const {
483 for (int i = 0; i < kNumberOfRegisterPairs; i++) {
484 ArmManagedRegister current =
485 ArmManagedRegister::FromRegisterPair(static_cast<RegisterPair>(i));
486 if (blocked_core_registers_[current.AsRegisterPairLow()]
487 || blocked_core_registers_[current.AsRegisterPairHigh()]) {
488 blocked_register_pairs_[i] = true;
489 }
490 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100491}
492
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100493InstructionCodeGeneratorARM::InstructionCodeGeneratorARM(HGraph* graph, CodeGeneratorARM* codegen)
494 : HGraphVisitor(graph),
495 assembler_(codegen->GetAssembler()),
496 codegen_(codegen) {}
497
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000498void CodeGeneratorARM::GenerateFrameEntry() {
Roland Levillain199f3362014-11-27 17:15:16 +0000499 bool skip_overflow_check =
500 IsLeafMethod() && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kArm);
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100501 if (!skip_overflow_check) {
502 if (kExplicitStackOverflowCheck) {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100503 SlowPathCodeARM* slow_path = new (GetGraph()->GetArena()) StackOverflowCheckSlowPathARM();
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100504 AddSlowPath(slow_path);
505
506 __ LoadFromOffset(kLoadWord, IP, TR, Thread::StackEndOffset<kArmWordSize>().Int32Value());
507 __ cmp(SP, ShifterOperand(IP));
508 __ b(slow_path->GetEntryLabel(), CC);
509 } else {
510 __ AddConstant(IP, SP, -static_cast<int32_t>(GetStackOverflowReservedBytes(kArm)));
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100511 __ LoadFromOffset(kLoadWord, IP, IP, 0);
Nicolas Geoffray39468442014-09-02 15:17:15 +0100512 RecordPcInfo(nullptr, 0);
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100513 }
514 }
515
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100516 core_spill_mask_ |= (1 << LR | 1 << R6 | 1 << R7);
517 __ PushList(1 << LR | 1 << R6 | 1 << R7);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000518
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100519 // The return PC has already been pushed on the stack.
Nicolas Geoffray707c8092014-04-04 10:50:14 +0100520 __ AddConstant(SP, -(GetFrameSize() - kNumberOfPushedRegistersAtEntry * kArmWordSize));
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100521 __ StoreToOffset(kStoreWord, R0, SP, 0);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000522}
523
524void CodeGeneratorARM::GenerateFrameExit() {
Nicolas Geoffray707c8092014-04-04 10:50:14 +0100525 __ AddConstant(SP, GetFrameSize() - kNumberOfPushedRegistersAtEntry * kArmWordSize);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100526 __ PopList(1 << PC | 1 << R6 | 1 << R7);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000527}
528
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100529void CodeGeneratorARM::Bind(HBasicBlock* block) {
530 __ Bind(GetLabelOf(block));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000531}
532
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100533Location CodeGeneratorARM::GetStackLocation(HLoadLocal* load) const {
534 switch (load->GetType()) {
535 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100536 case Primitive::kPrimDouble:
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100537 return Location::DoubleStackSlot(GetStackSlot(load->GetLocal()));
538 break;
539
540 case Primitive::kPrimInt:
541 case Primitive::kPrimNot:
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100542 case Primitive::kPrimFloat:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100543 return Location::StackSlot(GetStackSlot(load->GetLocal()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100544
545 case Primitive::kPrimBoolean:
546 case Primitive::kPrimByte:
547 case Primitive::kPrimChar:
548 case Primitive::kPrimShort:
549 case Primitive::kPrimVoid:
550 LOG(FATAL) << "Unexpected type " << load->GetType();
551 }
552
553 LOG(FATAL) << "Unreachable";
554 return Location();
555}
556
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100557Location InvokeDexCallingConventionVisitor::GetNextLocation(Primitive::Type type) {
558 switch (type) {
559 case Primitive::kPrimBoolean:
560 case Primitive::kPrimByte:
561 case Primitive::kPrimChar:
562 case Primitive::kPrimShort:
563 case Primitive::kPrimInt:
564 case Primitive::kPrimNot: {
565 uint32_t index = gp_index_++;
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000566 uint32_t stack_index = stack_index_++;
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100567 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100568 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100569 } else {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000570 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index));
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100571 }
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100572 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100573
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000574 case Primitive::kPrimLong: {
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100575 uint32_t index = gp_index_;
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000576 uint32_t stack_index = stack_index_;
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100577 gp_index_ += 2;
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000578 stack_index_ += 2;
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100579 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100580 ArmManagedRegister pair = ArmManagedRegister::FromRegisterPair(
581 calling_convention.GetRegisterPairAt(index));
582 return Location::RegisterPairLocation(pair.AsRegisterPairLow(), pair.AsRegisterPairHigh());
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100583 } else if (index + 1 == calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray0a6c4592014-10-30 16:37:57 +0000584 return Location::QuickParameter(index, stack_index);
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100585 } else {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000586 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index));
587 }
588 }
589
590 case Primitive::kPrimFloat: {
591 uint32_t stack_index = stack_index_++;
592 if (float_index_ % 2 == 0) {
593 float_index_ = std::max(double_index_, float_index_);
594 }
595 if (float_index_ < calling_convention.GetNumberOfFpuRegisters()) {
596 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(float_index_++));
597 } else {
598 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index));
599 }
600 }
601
602 case Primitive::kPrimDouble: {
603 double_index_ = std::max(double_index_, RoundUp(float_index_, 2));
604 uint32_t stack_index = stack_index_;
605 stack_index_ += 2;
606 if (double_index_ + 1 < calling_convention.GetNumberOfFpuRegisters()) {
607 uint32_t index = double_index_;
608 double_index_ += 2;
609 return Location::FpuRegisterPairLocation(
610 calling_convention.GetFpuRegisterAt(index),
611 calling_convention.GetFpuRegisterAt(index + 1));
612 } else {
613 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index));
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100614 }
615 }
616
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100617 case Primitive::kPrimVoid:
618 LOG(FATAL) << "Unexpected parameter type " << type;
619 break;
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100620 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100621 return Location();
622}
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100623
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000624Location InvokeDexCallingConventionVisitor::GetReturnLocation(Primitive::Type type) {
625 switch (type) {
626 case Primitive::kPrimBoolean:
627 case Primitive::kPrimByte:
628 case Primitive::kPrimChar:
629 case Primitive::kPrimShort:
630 case Primitive::kPrimInt:
631 case Primitive::kPrimNot: {
632 return Location::RegisterLocation(R0);
633 }
634
635 case Primitive::kPrimFloat: {
636 return Location::FpuRegisterLocation(S0);
637 }
638
639 case Primitive::kPrimLong: {
640 return Location::RegisterPairLocation(R0, R1);
641 }
642
643 case Primitive::kPrimDouble: {
644 return Location::FpuRegisterPairLocation(S0, S1);
645 }
646
647 case Primitive::kPrimVoid:
648 return Location();
649 }
650 UNREACHABLE();
651 return Location();
652}
653
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100654void CodeGeneratorARM::Move32(Location destination, Location source) {
655 if (source.Equals(destination)) {
656 return;
657 }
658 if (destination.IsRegister()) {
659 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000660 __ Mov(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100661 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000662 __ vmovrs(destination.AsRegister<Register>(), source.AsFpuRegister<SRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100663 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000664 __ LoadFromOffset(kLoadWord, destination.AsRegister<Register>(), SP, source.GetStackIndex());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100665 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100666 } else if (destination.IsFpuRegister()) {
667 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000668 __ vmovsr(destination.AsFpuRegister<SRegister>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100669 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000670 __ vmovs(destination.AsFpuRegister<SRegister>(), source.AsFpuRegister<SRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100671 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000672 __ LoadSFromOffset(destination.AsFpuRegister<SRegister>(), SP, source.GetStackIndex());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100673 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100674 } else {
Calin Juravlea21f5982014-11-13 15:53:04 +0000675 DCHECK(destination.IsStackSlot()) << destination;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100676 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000677 __ StoreToOffset(kStoreWord, source.AsRegister<Register>(), SP, destination.GetStackIndex());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100678 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000679 __ StoreSToOffset(source.AsFpuRegister<SRegister>(), SP, destination.GetStackIndex());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100680 } else {
Calin Juravlea21f5982014-11-13 15:53:04 +0000681 DCHECK(source.IsStackSlot()) << source;
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100682 __ LoadFromOffset(kLoadWord, IP, SP, source.GetStackIndex());
683 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100684 }
685 }
686}
687
688void CodeGeneratorARM::Move64(Location destination, Location source) {
689 if (source.Equals(destination)) {
690 return;
691 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100692 if (destination.IsRegisterPair()) {
693 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000694 EmitParallelMoves(
695 Location::RegisterLocation(source.AsRegisterPairHigh<Register>()),
696 Location::RegisterLocation(destination.AsRegisterPairHigh<Register>()),
697 Location::RegisterLocation(source.AsRegisterPairLow<Register>()),
698 Location::RegisterLocation(destination.AsRegisterPairLow<Register>()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100699 } else if (source.IsFpuRegister()) {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000700 UNIMPLEMENTED(FATAL);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100701 } else if (source.IsQuickParameter()) {
Nicolas Geoffray0a6c4592014-10-30 16:37:57 +0000702 uint16_t register_index = source.GetQuickParameterRegisterIndex();
703 uint16_t stack_index = source.GetQuickParameterStackIndex();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100704 InvokeDexCallingConvention calling_convention;
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000705 EmitParallelMoves(
706 Location::RegisterLocation(calling_convention.GetRegisterAt(register_index)),
707 Location::RegisterLocation(destination.AsRegisterPairLow<Register>()),
708 Location::StackSlot(
709 calling_convention.GetStackOffsetOf(stack_index + 1) + GetFrameSize()),
710 Location::RegisterLocation(destination.AsRegisterPairHigh<Register>()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100711 } else {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000712 // No conflict possible, so just do the moves.
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100713 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100714 if (destination.AsRegisterPairLow<Register>() == R1) {
715 DCHECK_EQ(destination.AsRegisterPairHigh<Register>(), R2);
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100716 __ LoadFromOffset(kLoadWord, R1, SP, source.GetStackIndex());
717 __ LoadFromOffset(kLoadWord, R2, SP, source.GetHighStackIndex(kArmWordSize));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100718 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100719 __ LoadFromOffset(kLoadWordPair, destination.AsRegisterPairLow<Register>(),
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100720 SP, source.GetStackIndex());
721 }
722 }
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000723 } else if (destination.IsFpuRegisterPair()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100724 if (source.IsDoubleStackSlot()) {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000725 __ LoadDFromOffset(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()),
726 SP,
727 source.GetStackIndex());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100728 } else {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000729 UNIMPLEMENTED(FATAL);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100730 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100731 } else if (destination.IsQuickParameter()) {
732 InvokeDexCallingConvention calling_convention;
Nicolas Geoffray0a6c4592014-10-30 16:37:57 +0000733 uint16_t register_index = destination.GetQuickParameterRegisterIndex();
734 uint16_t stack_index = destination.GetQuickParameterStackIndex();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100735 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000736 UNIMPLEMENTED(FATAL);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100737 } else if (source.IsFpuRegister()) {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000738 UNIMPLEMENTED(FATAL);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100739 } else {
740 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000741 EmitParallelMoves(
742 Location::StackSlot(source.GetStackIndex()),
743 Location::RegisterLocation(calling_convention.GetRegisterAt(register_index)),
744 Location::StackSlot(source.GetHighStackIndex(kArmWordSize)),
745 Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index + 1)));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100746 }
747 } else {
748 DCHECK(destination.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100749 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000750 // No conflict possible, so just do the moves.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100751 if (source.AsRegisterPairLow<Register>() == R1) {
752 DCHECK_EQ(source.AsRegisterPairHigh<Register>(), R2);
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100753 __ StoreToOffset(kStoreWord, R1, SP, destination.GetStackIndex());
754 __ StoreToOffset(kStoreWord, R2, SP, destination.GetHighStackIndex(kArmWordSize));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100755 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100756 __ StoreToOffset(kStoreWordPair, source.AsRegisterPairLow<Register>(),
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100757 SP, destination.GetStackIndex());
758 }
759 } else if (source.IsQuickParameter()) {
760 InvokeDexCallingConvention calling_convention;
Nicolas Geoffray0a6c4592014-10-30 16:37:57 +0000761 uint16_t register_index = source.GetQuickParameterRegisterIndex();
762 uint16_t stack_index = source.GetQuickParameterStackIndex();
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000763 // Just move the low part. The only time a source is a quick parameter is
764 // when moving the parameter to its stack locations. And the (Java) caller
765 // of this method has already done that.
Nicolas Geoffray0a6c4592014-10-30 16:37:57 +0000766 __ StoreToOffset(kStoreWord, calling_convention.GetRegisterAt(register_index),
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000767 SP, destination.GetStackIndex());
768 DCHECK_EQ(calling_convention.GetStackOffsetOf(stack_index + 1) + GetFrameSize(),
769 static_cast<size_t>(destination.GetHighStackIndex(kArmWordSize)));
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000770 } else if (source.IsFpuRegisterPair()) {
771 __ StoreDToOffset(FromLowSToD(source.AsFpuRegisterPairLow<SRegister>()),
772 SP,
773 destination.GetStackIndex());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100774 } else {
775 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000776 EmitParallelMoves(
777 Location::StackSlot(source.GetStackIndex()),
778 Location::StackSlot(destination.GetStackIndex()),
779 Location::StackSlot(source.GetHighStackIndex(kArmWordSize)),
780 Location::StackSlot(destination.GetHighStackIndex(kArmWordSize)));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100781 }
782 }
783}
784
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100785void CodeGeneratorARM::Move(HInstruction* instruction, Location location, HInstruction* move_for) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100786 LocationSummary* locations = instruction->GetLocations();
787 if (locations != nullptr && locations->Out().Equals(location)) {
788 return;
789 }
790
Calin Juravlea21f5982014-11-13 15:53:04 +0000791 if (locations != nullptr && locations->Out().IsConstant()) {
792 HConstant* const_to_move = locations->Out().GetConstant();
793 if (const_to_move->IsIntConstant()) {
794 int32_t value = const_to_move->AsIntConstant()->GetValue();
795 if (location.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000796 __ LoadImmediate(location.AsRegister<Register>(), value);
Calin Juravlea21f5982014-11-13 15:53:04 +0000797 } else {
798 DCHECK(location.IsStackSlot());
799 __ LoadImmediate(IP, value);
800 __ StoreToOffset(kStoreWord, IP, SP, location.GetStackIndex());
801 }
802 } else if (const_to_move->IsLongConstant()) {
803 int64_t value = const_to_move->AsLongConstant()->GetValue();
804 if (location.IsRegisterPair()) {
805 __ LoadImmediate(location.AsRegisterPairLow<Register>(), Low32Bits(value));
806 __ LoadImmediate(location.AsRegisterPairHigh<Register>(), High32Bits(value));
807 } else {
808 DCHECK(location.IsDoubleStackSlot());
809 __ LoadImmediate(IP, Low32Bits(value));
810 __ StoreToOffset(kStoreWord, IP, SP, location.GetStackIndex());
811 __ LoadImmediate(IP, High32Bits(value));
812 __ StoreToOffset(kStoreWord, IP, SP, location.GetHighStackIndex(kArmWordSize));
813 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100814 }
Roland Levillain476df552014-10-09 17:51:36 +0100815 } else if (instruction->IsLoadLocal()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100816 uint32_t stack_slot = GetStackSlot(instruction->AsLoadLocal()->GetLocal());
817 switch (instruction->GetType()) {
818 case Primitive::kPrimBoolean:
819 case Primitive::kPrimByte:
820 case Primitive::kPrimChar:
821 case Primitive::kPrimShort:
822 case Primitive::kPrimInt:
823 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100824 case Primitive::kPrimFloat:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100825 Move32(location, Location::StackSlot(stack_slot));
826 break;
827
828 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100829 case Primitive::kPrimDouble:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100830 Move64(location, Location::DoubleStackSlot(stack_slot));
831 break;
832
833 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100834 LOG(FATAL) << "Unexpected type " << instruction->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100835 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +0000836 } else if (instruction->IsTemporary()) {
837 Location temp_location = GetTemporaryLocation(instruction->AsTemporary());
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000838 if (temp_location.IsStackSlot()) {
839 Move32(location, temp_location);
840 } else {
841 DCHECK(temp_location.IsDoubleStackSlot());
842 Move64(location, temp_location);
843 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000844 } else {
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100845 DCHECK((instruction->GetNext() == move_for) || instruction->GetNext()->IsTemporary());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100846 switch (instruction->GetType()) {
847 case Primitive::kPrimBoolean:
848 case Primitive::kPrimByte:
849 case Primitive::kPrimChar:
850 case Primitive::kPrimShort:
851 case Primitive::kPrimNot:
852 case Primitive::kPrimInt:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100853 case Primitive::kPrimFloat:
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100854 Move32(location, locations->Out());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100855 break;
856
857 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100858 case Primitive::kPrimDouble:
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100859 Move64(location, locations->Out());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100860 break;
861
862 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100863 LOG(FATAL) << "Unexpected type " << instruction->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100864 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000865 }
866}
867
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100868void CodeGeneratorARM::InvokeRuntime(int32_t entry_point_offset,
869 HInstruction* instruction,
870 uint32_t dex_pc) {
871 __ LoadFromOffset(kLoadWord, LR, TR, entry_point_offset);
872 __ blx(LR);
873 RecordPcInfo(instruction, dex_pc);
874 DCHECK(instruction->IsSuspendCheck()
875 || instruction->IsBoundsCheck()
876 || instruction->IsNullCheck()
Calin Juravled0d48522014-11-04 16:40:20 +0000877 || instruction->IsDivZeroCheck()
Roland Levillain624279f2014-12-04 11:54:28 +0000878 || instruction->GetLocations()->CanCall()
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100879 || !IsLeafMethod());
880}
881
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000882void LocationsBuilderARM::VisitGoto(HGoto* got) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000883 got->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000884}
885
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000886void InstructionCodeGeneratorARM::VisitGoto(HGoto* got) {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000887 HBasicBlock* successor = got->GetSuccessor();
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100888 DCHECK(!successor->IsExitBlock());
889
890 HBasicBlock* block = got->GetBlock();
891 HInstruction* previous = got->GetPrevious();
892
893 HLoopInformation* info = block->GetLoopInformation();
894 if (info != nullptr && info->IsBackEdge(block) && info->HasSuspendCheck()) {
895 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
896 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
897 return;
898 }
899
900 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
901 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
902 }
903 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000904 __ b(codegen_->GetLabelOf(successor));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000905 }
906}
907
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000908void LocationsBuilderARM::VisitExit(HExit* exit) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000909 exit->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000910}
911
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000912void InstructionCodeGeneratorARM::VisitExit(HExit* exit) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700913 UNUSED(exit);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000914 if (kIsDebugBuild) {
915 __ Comment("Unreachable");
916 __ bkpt(0);
917 }
918}
919
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000920void LocationsBuilderARM::VisitIf(HIf* if_instr) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100921 LocationSummary* locations =
922 new (GetGraph()->GetArena()) LocationSummary(if_instr, LocationSummary::kNoCall);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100923 HInstruction* cond = if_instr->InputAt(0);
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100924 if (!cond->IsCondition() || cond->AsCondition()->NeedsMaterialization()) {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +0100925 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100926 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000927}
928
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000929void InstructionCodeGeneratorARM::VisitIf(HIf* if_instr) {
Dave Allison20dfc792014-06-16 20:44:29 -0700930 HInstruction* cond = if_instr->InputAt(0);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100931 if (cond->IsIntConstant()) {
932 // Constant condition, statically compared against 1.
933 int32_t cond_value = cond->AsIntConstant()->GetValue();
934 if (cond_value == 1) {
935 if (!codegen_->GoesToNextBlock(if_instr->GetBlock(),
936 if_instr->IfTrueSuccessor())) {
937 __ b(codegen_->GetLabelOf(if_instr->IfTrueSuccessor()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100938 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100939 return;
940 } else {
941 DCHECK_EQ(cond_value, 0);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100942 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100943 } else {
944 if (!cond->IsCondition() || cond->AsCondition()->NeedsMaterialization()) {
945 // Condition has been materialized, compare the output to 0
946 DCHECK(if_instr->GetLocations()->InAt(0).IsRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +0000947 __ cmp(if_instr->GetLocations()->InAt(0).AsRegister<Register>(),
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100948 ShifterOperand(0));
949 __ b(codegen_->GetLabelOf(if_instr->IfTrueSuccessor()), NE);
950 } else {
951 // Condition has not been materialized, use its inputs as the
952 // comparison and its condition as the branch condition.
953 LocationSummary* locations = cond->GetLocations();
Nicolas Geoffray3bcc8ea2014-11-28 15:00:02 +0000954 Register left = locations->InAt(0).AsRegister<Register>();
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100955 if (locations->InAt(1).IsRegister()) {
Nicolas Geoffray3bcc8ea2014-11-28 15:00:02 +0000956 __ cmp(left, ShifterOperand(locations->InAt(1).AsRegister<Register>()));
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100957 } else {
958 DCHECK(locations->InAt(1).IsConstant());
959 int32_t value =
960 locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
961 ShifterOperand operand;
Nicolas Geoffray3bcc8ea2014-11-28 15:00:02 +0000962 if (GetAssembler()->ShifterOperandCanHold(R0, left, CMP, value, &operand)) {
963 __ cmp(left, operand);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100964 } else {
965 Register temp = IP;
966 __ LoadImmediate(temp, value);
Nicolas Geoffray3bcc8ea2014-11-28 15:00:02 +0000967 __ cmp(left, ShifterOperand(temp));
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100968 }
969 }
970 __ b(codegen_->GetLabelOf(if_instr->IfTrueSuccessor()),
971 ARMCondition(cond->AsCondition()->GetCondition()));
972 }
Dave Allison20dfc792014-06-16 20:44:29 -0700973 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100974 if (!codegen_->GoesToNextBlock(if_instr->GetBlock(),
975 if_instr->IfFalseSuccessor())) {
Dave Allison20dfc792014-06-16 20:44:29 -0700976 __ b(codegen_->GetLabelOf(if_instr->IfFalseSuccessor()));
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000977 }
978}
979
Dave Allison20dfc792014-06-16 20:44:29 -0700980
981void LocationsBuilderARM::VisitCondition(HCondition* comp) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100982 LocationSummary* locations =
983 new (GetGraph()->GetArena()) LocationSummary(comp, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +0100984 locations->SetInAt(0, Location::RequiresRegister());
985 locations->SetInAt(1, Location::RegisterOrConstant(comp->InputAt(1)));
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100986 if (comp->NeedsMaterialization()) {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +0100987 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100988 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000989}
990
Dave Allison20dfc792014-06-16 20:44:29 -0700991void InstructionCodeGeneratorARM::VisitCondition(HCondition* comp) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100992 if (!comp->NeedsMaterialization()) return;
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100993 LocationSummary* locations = comp->GetLocations();
Nicolas Geoffray3bcc8ea2014-11-28 15:00:02 +0000994 Register left = locations->InAt(0).AsRegister<Register>();
995
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100996 if (locations->InAt(1).IsRegister()) {
Nicolas Geoffray3bcc8ea2014-11-28 15:00:02 +0000997 __ cmp(left, ShifterOperand(locations->InAt(1).AsRegister<Register>()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100998 } else {
999 DCHECK(locations->InAt(1).IsConstant());
1000 int32_t value = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
1001 ShifterOperand operand;
Nicolas Geoffray3bcc8ea2014-11-28 15:00:02 +00001002 if (GetAssembler()->ShifterOperandCanHold(R0, left, CMP, value, &operand)) {
1003 __ cmp(left, operand);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001004 } else {
1005 Register temp = IP;
1006 __ LoadImmediate(temp, value);
Nicolas Geoffray3bcc8ea2014-11-28 15:00:02 +00001007 __ cmp(left, ShifterOperand(temp));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001008 }
Dave Allison20dfc792014-06-16 20:44:29 -07001009 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001010 __ it(ARMCondition(comp->GetCondition()), kItElse);
Roland Levillain271ab9c2014-11-27 15:23:57 +00001011 __ mov(locations->Out().AsRegister<Register>(), ShifterOperand(1),
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001012 ARMCondition(comp->GetCondition()));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001013 __ mov(locations->Out().AsRegister<Register>(), ShifterOperand(0),
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001014 ARMOppositeCondition(comp->GetCondition()));
Dave Allison20dfc792014-06-16 20:44:29 -07001015}
1016
1017void LocationsBuilderARM::VisitEqual(HEqual* comp) {
1018 VisitCondition(comp);
1019}
1020
1021void InstructionCodeGeneratorARM::VisitEqual(HEqual* comp) {
1022 VisitCondition(comp);
1023}
1024
1025void LocationsBuilderARM::VisitNotEqual(HNotEqual* comp) {
1026 VisitCondition(comp);
1027}
1028
1029void InstructionCodeGeneratorARM::VisitNotEqual(HNotEqual* comp) {
1030 VisitCondition(comp);
1031}
1032
1033void LocationsBuilderARM::VisitLessThan(HLessThan* comp) {
1034 VisitCondition(comp);
1035}
1036
1037void InstructionCodeGeneratorARM::VisitLessThan(HLessThan* comp) {
1038 VisitCondition(comp);
1039}
1040
1041void LocationsBuilderARM::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
1042 VisitCondition(comp);
1043}
1044
1045void InstructionCodeGeneratorARM::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
1046 VisitCondition(comp);
1047}
1048
1049void LocationsBuilderARM::VisitGreaterThan(HGreaterThan* comp) {
1050 VisitCondition(comp);
1051}
1052
1053void InstructionCodeGeneratorARM::VisitGreaterThan(HGreaterThan* comp) {
1054 VisitCondition(comp);
1055}
1056
1057void LocationsBuilderARM::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
1058 VisitCondition(comp);
1059}
1060
1061void InstructionCodeGeneratorARM::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
1062 VisitCondition(comp);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001063}
1064
1065void LocationsBuilderARM::VisitLocal(HLocal* local) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001066 local->SetLocations(nullptr);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001067}
1068
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001069void InstructionCodeGeneratorARM::VisitLocal(HLocal* local) {
1070 DCHECK_EQ(local->GetBlock(), GetGraph()->GetEntryBlock());
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001071}
1072
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001073void LocationsBuilderARM::VisitLoadLocal(HLoadLocal* load) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001074 load->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001075}
1076
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001077void InstructionCodeGeneratorARM::VisitLoadLocal(HLoadLocal* load) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001078 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001079 UNUSED(load);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001080}
1081
1082void LocationsBuilderARM::VisitStoreLocal(HStoreLocal* store) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001083 LocationSummary* locations =
1084 new (GetGraph()->GetArena()) LocationSummary(store, LocationSummary::kNoCall);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001085 switch (store->InputAt(1)->GetType()) {
1086 case Primitive::kPrimBoolean:
1087 case Primitive::kPrimByte:
1088 case Primitive::kPrimChar:
1089 case Primitive::kPrimShort:
1090 case Primitive::kPrimInt:
1091 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001092 case Primitive::kPrimFloat:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001093 locations->SetInAt(1, Location::StackSlot(codegen_->GetStackSlot(store->GetLocal())));
1094 break;
1095
1096 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001097 case Primitive::kPrimDouble:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001098 locations->SetInAt(1, Location::DoubleStackSlot(codegen_->GetStackSlot(store->GetLocal())));
1099 break;
1100
1101 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001102 LOG(FATAL) << "Unexpected local type " << store->InputAt(1)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001103 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001104}
1105
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001106void InstructionCodeGeneratorARM::VisitStoreLocal(HStoreLocal* store) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001107 UNUSED(store);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001108}
1109
1110void LocationsBuilderARM::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001111 LocationSummary* locations =
1112 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001113 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001114}
1115
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001116void InstructionCodeGeneratorARM::VisitIntConstant(HIntConstant* constant) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001117 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001118 UNUSED(constant);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001119}
1120
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001121void LocationsBuilderARM::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001122 LocationSummary* locations =
1123 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001124 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001125}
1126
1127void InstructionCodeGeneratorARM::VisitLongConstant(HLongConstant* constant) {
1128 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001129 UNUSED(constant);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001130}
1131
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001132void LocationsBuilderARM::VisitFloatConstant(HFloatConstant* constant) {
1133 LocationSummary* locations =
1134 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1135 locations->SetOut(Location::ConstantLocation(constant));
1136}
1137
1138void InstructionCodeGeneratorARM::VisitFloatConstant(HFloatConstant* constant) {
1139 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001140 UNUSED(constant);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001141}
1142
1143void LocationsBuilderARM::VisitDoubleConstant(HDoubleConstant* constant) {
1144 LocationSummary* locations =
1145 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1146 locations->SetOut(Location::ConstantLocation(constant));
1147}
1148
1149void InstructionCodeGeneratorARM::VisitDoubleConstant(HDoubleConstant* constant) {
1150 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001151 UNUSED(constant);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001152}
1153
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001154void LocationsBuilderARM::VisitReturnVoid(HReturnVoid* ret) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001155 ret->SetLocations(nullptr);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001156}
1157
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001158void InstructionCodeGeneratorARM::VisitReturnVoid(HReturnVoid* ret) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001159 UNUSED(ret);
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001160 codegen_->GenerateFrameExit();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001161}
1162
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001163void LocationsBuilderARM::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001164 LocationSummary* locations =
1165 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001166 locations->SetInAt(0, parameter_visitor_.GetReturnLocation(ret->InputAt(0)->GetType()));
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001167}
1168
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001169void InstructionCodeGeneratorARM::VisitReturn(HReturn* ret) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001170 UNUSED(ret);
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001171 codegen_->GenerateFrameExit();
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001172}
1173
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001174void LocationsBuilderARM::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001175 HandleInvoke(invoke);
1176}
1177
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001178void CodeGeneratorARM::LoadCurrentMethod(Register reg) {
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001179 __ LoadFromOffset(kLoadWord, reg, SP, kCurrentMethodStackOffset);
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001180}
1181
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001182void InstructionCodeGeneratorARM::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001183 Register temp = invoke->GetLocations()->GetTemp(0).AsRegister<Register>();
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001184
1185 // TODO: Implement all kinds of calls:
1186 // 1) boot -> boot
1187 // 2) app -> boot
1188 // 3) app -> app
1189 //
1190 // Currently we implement the app -> app logic, which looks up in the resolve cache.
1191
1192 // temp = method;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001193 codegen_->LoadCurrentMethod(temp);
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001194 // temp = temp->dex_cache_resolved_methods_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001195 __ LoadFromOffset(
1196 kLoadWord, temp, temp, mirror::ArtMethod::DexCacheResolvedMethodsOffset().Int32Value());
Nicolas Geoffray390f59f2014-12-12 19:22:03 +00001197 if (!invoke->GetIsRecursive()) {
1198 // temp = temp[index_in_cache]
1199 __ LoadFromOffset(
1200 kLoadWord, temp, temp, CodeGenerator::GetCacheOffset(invoke->GetIndexInDexCache()));
1201 // LR = temp[offset_of_quick_compiled_code]
1202 __ LoadFromOffset(kLoadWord, LR, temp, mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset(
1203 kArmWordSize).Int32Value());
1204 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001205 // LR()
1206 __ blx(LR);
1207
1208 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
1209 DCHECK(!codegen_->IsLeafMethod());
1210}
1211
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001212void LocationsBuilderARM::HandleInvoke(HInvoke* invoke) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001213 LocationSummary* locations =
1214 new (GetGraph()->GetArena()) LocationSummary(invoke, LocationSummary::kCall);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001215 locations->AddTemp(Location::RegisterLocation(R0));
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001216
1217 InvokeDexCallingConventionVisitor calling_convention_visitor;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001218 for (size_t i = 0; i < invoke->InputCount(); i++) {
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001219 HInstruction* input = invoke->InputAt(i);
1220 locations->SetInAt(i, calling_convention_visitor.GetNextLocation(input->GetType()));
1221 }
1222
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001223 locations->SetOut(calling_convention_visitor.GetReturnLocation(invoke->GetType()));
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001224}
1225
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001226void LocationsBuilderARM::VisitInvokeVirtual(HInvokeVirtual* invoke) {
1227 HandleInvoke(invoke);
1228}
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001229
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001230void InstructionCodeGeneratorARM::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001231 Register temp = invoke->GetLocations()->GetTemp(0).AsRegister<Register>();
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001232 uint32_t method_offset = mirror::Class::EmbeddedVTableOffset().Uint32Value() +
1233 invoke->GetVTableIndex() * sizeof(mirror::Class::VTableEntry);
1234 LocationSummary* locations = invoke->GetLocations();
1235 Location receiver = locations->InAt(0);
1236 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
1237 // temp = object->GetClass();
1238 if (receiver.IsStackSlot()) {
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001239 __ LoadFromOffset(kLoadWord, temp, SP, receiver.GetStackIndex());
1240 __ LoadFromOffset(kLoadWord, temp, temp, class_offset);
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001241 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001242 __ LoadFromOffset(kLoadWord, temp, receiver.AsRegister<Register>(), class_offset);
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001243 }
1244 // temp = temp->GetMethodAt(method_offset);
Mathieu Chartier2d721012014-11-10 11:08:06 -08001245 uint32_t entry_point = mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Nicolas Geoffray86a8d7a2014-11-19 08:47:18 +00001246 kArmWordSize).Int32Value();
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001247 __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001248 // LR = temp->GetEntryPoint();
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001249 __ LoadFromOffset(kLoadWord, LR, temp, entry_point);
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001250 // LR();
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001251 __ blx(LR);
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001252 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001253 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001254}
1255
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001256void LocationsBuilderARM::VisitInvokeInterface(HInvokeInterface* invoke) {
1257 HandleInvoke(invoke);
1258 // Add the hidden argument.
1259 invoke->GetLocations()->AddTemp(Location::RegisterLocation(R12));
1260}
1261
1262void InstructionCodeGeneratorARM::VisitInvokeInterface(HInvokeInterface* invoke) {
1263 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain271ab9c2014-11-27 15:23:57 +00001264 Register temp = invoke->GetLocations()->GetTemp(0).AsRegister<Register>();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001265 uint32_t method_offset = mirror::Class::EmbeddedImTableOffset().Uint32Value() +
1266 (invoke->GetImtIndex() % mirror::Class::kImtSize) * sizeof(mirror::Class::ImTableEntry);
1267 LocationSummary* locations = invoke->GetLocations();
1268 Location receiver = locations->InAt(0);
1269 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
1270
1271 // Set the hidden argument.
Roland Levillain199f3362014-11-27 17:15:16 +00001272 __ LoadImmediate(invoke->GetLocations()->GetTemp(1).AsRegister<Register>(),
1273 invoke->GetDexMethodIndex());
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001274
1275 // temp = object->GetClass();
1276 if (receiver.IsStackSlot()) {
1277 __ LoadFromOffset(kLoadWord, temp, SP, receiver.GetStackIndex());
1278 __ LoadFromOffset(kLoadWord, temp, temp, class_offset);
1279 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001280 __ LoadFromOffset(kLoadWord, temp, receiver.AsRegister<Register>(), class_offset);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001281 }
1282 // temp = temp->GetImtEntryAt(method_offset);
Mathieu Chartier2d721012014-11-10 11:08:06 -08001283 uint32_t entry_point = mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Nicolas Geoffray86a8d7a2014-11-19 08:47:18 +00001284 kArmWordSize).Int32Value();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001285 __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
1286 // LR = temp->GetEntryPoint();
1287 __ LoadFromOffset(kLoadWord, LR, temp, entry_point);
1288 // LR();
1289 __ blx(LR);
1290 DCHECK(!codegen_->IsLeafMethod());
1291 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
1292}
1293
Roland Levillain88cb1752014-10-20 16:36:47 +01001294void LocationsBuilderARM::VisitNeg(HNeg* neg) {
1295 LocationSummary* locations =
1296 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
1297 switch (neg->GetResultType()) {
1298 case Primitive::kPrimInt:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001299 case Primitive::kPrimLong: {
1300 bool output_overlaps = (neg->GetResultType() == Primitive::kPrimLong);
Roland Levillain88cb1752014-10-20 16:36:47 +01001301 locations->SetInAt(0, Location::RequiresRegister());
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001302 locations->SetOut(Location::RequiresRegister(), output_overlaps);
Roland Levillain88cb1752014-10-20 16:36:47 +01001303 break;
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001304 }
Roland Levillain88cb1752014-10-20 16:36:47 +01001305
Roland Levillain88cb1752014-10-20 16:36:47 +01001306 case Primitive::kPrimFloat:
1307 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00001308 locations->SetInAt(0, Location::RequiresFpuRegister());
1309 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillain88cb1752014-10-20 16:36:47 +01001310 break;
1311
1312 default:
1313 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1314 }
1315}
1316
1317void InstructionCodeGeneratorARM::VisitNeg(HNeg* neg) {
1318 LocationSummary* locations = neg->GetLocations();
1319 Location out = locations->Out();
1320 Location in = locations->InAt(0);
1321 switch (neg->GetResultType()) {
1322 case Primitive::kPrimInt:
1323 DCHECK(in.IsRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001324 __ rsb(out.AsRegister<Register>(), in.AsRegister<Register>(), ShifterOperand(0));
Roland Levillain88cb1752014-10-20 16:36:47 +01001325 break;
1326
1327 case Primitive::kPrimLong:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001328 DCHECK(in.IsRegisterPair());
1329 // out.lo = 0 - in.lo (and update the carry/borrow (C) flag)
1330 __ rsbs(out.AsRegisterPairLow<Register>(),
1331 in.AsRegisterPairLow<Register>(),
1332 ShifterOperand(0));
1333 // We cannot emit an RSC (Reverse Subtract with Carry)
1334 // instruction here, as it does not exist in the Thumb-2
1335 // instruction set. We use the following approach
1336 // using SBC and SUB instead.
1337 //
1338 // out.hi = -C
1339 __ sbc(out.AsRegisterPairHigh<Register>(),
1340 out.AsRegisterPairHigh<Register>(),
1341 ShifterOperand(out.AsRegisterPairHigh<Register>()));
1342 // out.hi = out.hi - in.hi
1343 __ sub(out.AsRegisterPairHigh<Register>(),
1344 out.AsRegisterPairHigh<Register>(),
1345 ShifterOperand(in.AsRegisterPairHigh<Register>()));
1346 break;
1347
Roland Levillain88cb1752014-10-20 16:36:47 +01001348 case Primitive::kPrimFloat:
Roland Levillain3dbcb382014-10-28 17:30:07 +00001349 DCHECK(in.IsFpuRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001350 __ vnegs(out.AsFpuRegister<SRegister>(), in.AsFpuRegister<SRegister>());
Roland Levillain3dbcb382014-10-28 17:30:07 +00001351 break;
1352
Roland Levillain88cb1752014-10-20 16:36:47 +01001353 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00001354 DCHECK(in.IsFpuRegisterPair());
1355 __ vnegd(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
1356 FromLowSToD(in.AsFpuRegisterPairLow<SRegister>()));
Roland Levillain88cb1752014-10-20 16:36:47 +01001357 break;
1358
1359 default:
1360 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1361 }
1362}
1363
Roland Levillaindff1f282014-11-05 14:15:05 +00001364void LocationsBuilderARM::VisitTypeConversion(HTypeConversion* conversion) {
Roland Levillaindff1f282014-11-05 14:15:05 +00001365 Primitive::Type result_type = conversion->GetResultType();
1366 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00001367 DCHECK_NE(result_type, input_type);
Roland Levillain624279f2014-12-04 11:54:28 +00001368
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001369 // The float-to-long and double-to-long type conversions rely on a
1370 // call to the runtime.
Roland Levillain624279f2014-12-04 11:54:28 +00001371 LocationSummary::CallKind call_kind =
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001372 ((input_type == Primitive::kPrimFloat || input_type == Primitive::kPrimDouble)
1373 && result_type == Primitive::kPrimLong)
Roland Levillain624279f2014-12-04 11:54:28 +00001374 ? LocationSummary::kCall
1375 : LocationSummary::kNoCall;
1376 LocationSummary* locations =
1377 new (GetGraph()->GetArena()) LocationSummary(conversion, call_kind);
1378
Roland Levillaindff1f282014-11-05 14:15:05 +00001379 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00001380 case Primitive::kPrimByte:
1381 switch (input_type) {
1382 case Primitive::kPrimShort:
1383 case Primitive::kPrimInt:
1384 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001385 // Processing a Dex `int-to-byte' instruction.
Roland Levillain51d3fc42014-11-13 14:11:42 +00001386 locations->SetInAt(0, Location::RequiresRegister());
1387 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1388 break;
1389
1390 default:
1391 LOG(FATAL) << "Unexpected type conversion from " << input_type
1392 << " to " << result_type;
1393 }
1394 break;
1395
Roland Levillain01a8d712014-11-14 16:27:39 +00001396 case Primitive::kPrimShort:
1397 switch (input_type) {
1398 case Primitive::kPrimByte:
1399 case Primitive::kPrimInt:
1400 case Primitive::kPrimChar:
1401 // Processing a Dex `int-to-short' instruction.
1402 locations->SetInAt(0, Location::RequiresRegister());
1403 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1404 break;
1405
1406 default:
1407 LOG(FATAL) << "Unexpected type conversion from " << input_type
1408 << " to " << result_type;
1409 }
1410 break;
1411
Roland Levillain946e1432014-11-11 17:35:19 +00001412 case Primitive::kPrimInt:
1413 switch (input_type) {
1414 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00001415 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00001416 locations->SetInAt(0, Location::Any());
1417 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1418 break;
1419
1420 case Primitive::kPrimFloat:
Roland Levillain3f8f9362014-12-02 17:45:01 +00001421 // Processing a Dex `float-to-int' instruction.
1422 locations->SetInAt(0, Location::RequiresFpuRegister());
1423 locations->SetOut(Location::RequiresRegister());
1424 locations->AddTemp(Location::RequiresFpuRegister());
1425 break;
1426
Roland Levillain946e1432014-11-11 17:35:19 +00001427 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001428 // Processing a Dex `double-to-int' instruction.
1429 locations->SetInAt(0, Location::RequiresFpuRegister());
1430 locations->SetOut(Location::RequiresRegister());
1431 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain946e1432014-11-11 17:35:19 +00001432 break;
1433
1434 default:
1435 LOG(FATAL) << "Unexpected type conversion from " << input_type
1436 << " to " << result_type;
1437 }
1438 break;
1439
Roland Levillaindff1f282014-11-05 14:15:05 +00001440 case Primitive::kPrimLong:
1441 switch (input_type) {
1442 case Primitive::kPrimByte:
1443 case Primitive::kPrimShort:
1444 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00001445 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001446 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00001447 locations->SetInAt(0, Location::RequiresRegister());
1448 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1449 break;
1450
Roland Levillain624279f2014-12-04 11:54:28 +00001451 case Primitive::kPrimFloat: {
1452 // Processing a Dex `float-to-long' instruction.
1453 InvokeRuntimeCallingConvention calling_convention;
1454 locations->SetInAt(0, Location::FpuRegisterLocation(
1455 calling_convention.GetFpuRegisterAt(0)));
1456 locations->SetOut(Location::RegisterPairLocation(R0, R1));
1457 break;
1458 }
1459
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001460 case Primitive::kPrimDouble: {
1461 // Processing a Dex `double-to-long' instruction.
1462 InvokeRuntimeCallingConvention calling_convention;
1463 locations->SetInAt(0, Location::FpuRegisterPairLocation(
1464 calling_convention.GetFpuRegisterAt(0),
1465 calling_convention.GetFpuRegisterAt(1)));
1466 locations->SetOut(Location::RegisterPairLocation(R0, R1));
Roland Levillaindff1f282014-11-05 14:15:05 +00001467 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001468 }
Roland Levillaindff1f282014-11-05 14:15:05 +00001469
1470 default:
1471 LOG(FATAL) << "Unexpected type conversion from " << input_type
1472 << " to " << result_type;
1473 }
1474 break;
1475
Roland Levillain981e4542014-11-14 11:47:14 +00001476 case Primitive::kPrimChar:
1477 switch (input_type) {
1478 case Primitive::kPrimByte:
1479 case Primitive::kPrimShort:
1480 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00001481 // Processing a Dex `int-to-char' instruction.
1482 locations->SetInAt(0, Location::RequiresRegister());
1483 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1484 break;
1485
1486 default:
1487 LOG(FATAL) << "Unexpected type conversion from " << input_type
1488 << " to " << result_type;
1489 }
1490 break;
1491
Roland Levillaindff1f282014-11-05 14:15:05 +00001492 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00001493 switch (input_type) {
1494 case Primitive::kPrimByte:
1495 case Primitive::kPrimShort:
1496 case Primitive::kPrimInt:
1497 case Primitive::kPrimChar:
1498 // Processing a Dex `int-to-float' instruction.
1499 locations->SetInAt(0, Location::RequiresRegister());
1500 locations->SetOut(Location::RequiresFpuRegister());
1501 break;
1502
1503 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00001504 // Processing a Dex `long-to-float' instruction.
1505 locations->SetInAt(0, Location::RequiresRegister());
1506 locations->SetOut(Location::RequiresFpuRegister());
1507 locations->AddTemp(Location::RequiresRegister());
1508 locations->AddTemp(Location::RequiresRegister());
1509 locations->AddTemp(Location::RequiresFpuRegister());
1510 locations->AddTemp(Location::RequiresFpuRegister());
1511 break;
1512
Roland Levillaincff13742014-11-17 14:32:17 +00001513 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001514 // Processing a Dex `double-to-float' instruction.
1515 locations->SetInAt(0, Location::RequiresFpuRegister());
1516 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00001517 break;
1518
1519 default:
1520 LOG(FATAL) << "Unexpected type conversion from " << input_type
1521 << " to " << result_type;
1522 };
1523 break;
1524
Roland Levillaindff1f282014-11-05 14:15:05 +00001525 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00001526 switch (input_type) {
1527 case Primitive::kPrimByte:
1528 case Primitive::kPrimShort:
1529 case Primitive::kPrimInt:
1530 case Primitive::kPrimChar:
1531 // Processing a Dex `int-to-double' instruction.
1532 locations->SetInAt(0, Location::RequiresRegister());
1533 locations->SetOut(Location::RequiresFpuRegister());
1534 break;
1535
1536 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00001537 // Processing a Dex `long-to-double' instruction.
1538 locations->SetInAt(0, Location::RequiresRegister());
1539 locations->SetOut(Location::RequiresFpuRegister());
1540 locations->AddTemp(Location::RequiresRegister());
1541 locations->AddTemp(Location::RequiresRegister());
1542 locations->AddTemp(Location::RequiresFpuRegister());
1543 break;
1544
Roland Levillaincff13742014-11-17 14:32:17 +00001545 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001546 // Processing a Dex `float-to-double' instruction.
1547 locations->SetInAt(0, Location::RequiresFpuRegister());
1548 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00001549 break;
1550
1551 default:
1552 LOG(FATAL) << "Unexpected type conversion from " << input_type
1553 << " to " << result_type;
1554 };
Roland Levillaindff1f282014-11-05 14:15:05 +00001555 break;
1556
1557 default:
1558 LOG(FATAL) << "Unexpected type conversion from " << input_type
1559 << " to " << result_type;
1560 }
1561}
1562
1563void InstructionCodeGeneratorARM::VisitTypeConversion(HTypeConversion* conversion) {
1564 LocationSummary* locations = conversion->GetLocations();
1565 Location out = locations->Out();
1566 Location in = locations->InAt(0);
1567 Primitive::Type result_type = conversion->GetResultType();
1568 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00001569 DCHECK_NE(result_type, input_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00001570 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00001571 case Primitive::kPrimByte:
1572 switch (input_type) {
1573 case Primitive::kPrimShort:
1574 case Primitive::kPrimInt:
1575 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001576 // Processing a Dex `int-to-byte' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00001577 __ sbfx(out.AsRegister<Register>(), in.AsRegister<Register>(), 0, 8);
Roland Levillain51d3fc42014-11-13 14:11:42 +00001578 break;
1579
1580 default:
1581 LOG(FATAL) << "Unexpected type conversion from " << input_type
1582 << " to " << result_type;
1583 }
1584 break;
1585
Roland Levillain01a8d712014-11-14 16:27:39 +00001586 case Primitive::kPrimShort:
1587 switch (input_type) {
1588 case Primitive::kPrimByte:
1589 case Primitive::kPrimInt:
1590 case Primitive::kPrimChar:
1591 // Processing a Dex `int-to-short' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00001592 __ sbfx(out.AsRegister<Register>(), in.AsRegister<Register>(), 0, 16);
Roland Levillain01a8d712014-11-14 16:27:39 +00001593 break;
1594
1595 default:
1596 LOG(FATAL) << "Unexpected type conversion from " << input_type
1597 << " to " << result_type;
1598 }
1599 break;
1600
Roland Levillain946e1432014-11-11 17:35:19 +00001601 case Primitive::kPrimInt:
1602 switch (input_type) {
1603 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00001604 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00001605 DCHECK(out.IsRegister());
1606 if (in.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001607 __ Mov(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
Roland Levillain946e1432014-11-11 17:35:19 +00001608 } else if (in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001609 __ LoadFromOffset(kLoadWord, out.AsRegister<Register>(), SP, in.GetStackIndex());
Roland Levillain946e1432014-11-11 17:35:19 +00001610 } else {
1611 DCHECK(in.IsConstant());
1612 DCHECK(in.GetConstant()->IsLongConstant());
1613 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00001614 __ LoadImmediate(out.AsRegister<Register>(), static_cast<int32_t>(value));
Roland Levillain946e1432014-11-11 17:35:19 +00001615 }
1616 break;
1617
Roland Levillain3f8f9362014-12-02 17:45:01 +00001618 case Primitive::kPrimFloat: {
1619 // Processing a Dex `float-to-int' instruction.
1620 SRegister temp = locations->GetTemp(0).AsFpuRegisterPairLow<SRegister>();
1621 __ vmovs(temp, in.AsFpuRegister<SRegister>());
1622 __ vcvtis(temp, temp);
1623 __ vmovrs(out.AsRegister<Register>(), temp);
1624 break;
1625 }
1626
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001627 case Primitive::kPrimDouble: {
1628 // Processing a Dex `double-to-int' instruction.
1629 SRegister temp_s = locations->GetTemp(0).AsFpuRegisterPairLow<SRegister>();
1630 DRegister temp_d = FromLowSToD(temp_s);
1631 __ vmovd(temp_d, FromLowSToD(in.AsFpuRegisterPairLow<SRegister>()));
1632 __ vcvtid(temp_s, temp_d);
1633 __ vmovrs(out.AsRegister<Register>(), temp_s);
Roland Levillain946e1432014-11-11 17:35:19 +00001634 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001635 }
Roland Levillain946e1432014-11-11 17:35:19 +00001636
1637 default:
1638 LOG(FATAL) << "Unexpected type conversion from " << input_type
1639 << " to " << result_type;
1640 }
1641 break;
1642
Roland Levillaindff1f282014-11-05 14:15:05 +00001643 case Primitive::kPrimLong:
1644 switch (input_type) {
1645 case Primitive::kPrimByte:
1646 case Primitive::kPrimShort:
1647 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00001648 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001649 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00001650 DCHECK(out.IsRegisterPair());
1651 DCHECK(in.IsRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001652 __ Mov(out.AsRegisterPairLow<Register>(), in.AsRegister<Register>());
Roland Levillaindff1f282014-11-05 14:15:05 +00001653 // Sign extension.
1654 __ Asr(out.AsRegisterPairHigh<Register>(),
1655 out.AsRegisterPairLow<Register>(),
1656 31);
1657 break;
1658
1659 case Primitive::kPrimFloat:
Roland Levillain624279f2014-12-04 11:54:28 +00001660 // Processing a Dex `float-to-long' instruction.
Roland Levillain624279f2014-12-04 11:54:28 +00001661 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pF2l),
1662 conversion,
1663 conversion->GetDexPc());
1664 break;
1665
Roland Levillaindff1f282014-11-05 14:15:05 +00001666 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001667 // Processing a Dex `double-to-long' instruction.
1668 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pD2l),
1669 conversion,
1670 conversion->GetDexPc());
Roland Levillaindff1f282014-11-05 14:15:05 +00001671 break;
1672
1673 default:
1674 LOG(FATAL) << "Unexpected type conversion from " << input_type
1675 << " to " << result_type;
1676 }
1677 break;
1678
Roland Levillain981e4542014-11-14 11:47:14 +00001679 case Primitive::kPrimChar:
1680 switch (input_type) {
1681 case Primitive::kPrimByte:
1682 case Primitive::kPrimShort:
1683 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00001684 // Processing a Dex `int-to-char' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00001685 __ ubfx(out.AsRegister<Register>(), in.AsRegister<Register>(), 0, 16);
Roland Levillain981e4542014-11-14 11:47:14 +00001686 break;
1687
1688 default:
1689 LOG(FATAL) << "Unexpected type conversion from " << input_type
1690 << " to " << result_type;
1691 }
1692 break;
1693
Roland Levillaindff1f282014-11-05 14:15:05 +00001694 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00001695 switch (input_type) {
1696 case Primitive::kPrimByte:
1697 case Primitive::kPrimShort:
1698 case Primitive::kPrimInt:
1699 case Primitive::kPrimChar: {
1700 // Processing a Dex `int-to-float' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00001701 __ vmovsr(out.AsFpuRegister<SRegister>(), in.AsRegister<Register>());
1702 __ vcvtsi(out.AsFpuRegister<SRegister>(), out.AsFpuRegister<SRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00001703 break;
1704 }
1705
Roland Levillain6d0e4832014-11-27 18:31:21 +00001706 case Primitive::kPrimLong: {
1707 // Processing a Dex `long-to-float' instruction.
1708 Register low = in.AsRegisterPairLow<Register>();
1709 Register high = in.AsRegisterPairHigh<Register>();
1710 SRegister output = out.AsFpuRegister<SRegister>();
1711 Register constant_low = locations->GetTemp(0).AsRegister<Register>();
1712 Register constant_high = locations->GetTemp(1).AsRegister<Register>();
1713 SRegister temp1_s = locations->GetTemp(2).AsFpuRegisterPairLow<SRegister>();
1714 DRegister temp1_d = FromLowSToD(temp1_s);
1715 SRegister temp2_s = locations->GetTemp(3).AsFpuRegisterPairLow<SRegister>();
1716 DRegister temp2_d = FromLowSToD(temp2_s);
1717
1718 // Operations use doubles for precision reasons (each 32-bit
1719 // half of a long fits in the 53-bit mantissa of a double,
1720 // but not in the 24-bit mantissa of a float). This is
1721 // especially important for the low bits. The result is
1722 // eventually converted to float.
1723
1724 // temp1_d = int-to-double(high)
1725 __ vmovsr(temp1_s, high);
1726 __ vcvtdi(temp1_d, temp1_s);
1727 // Using vmovd to load the `k2Pow32EncodingForDouble` constant
1728 // as an immediate value into `temp2_d` does not work, as
1729 // this instruction only transfers 8 significant bits of its
1730 // immediate operand. Instead, use two 32-bit core
1731 // registers to load `k2Pow32EncodingForDouble` into
1732 // `temp2_d`.
1733 __ LoadImmediate(constant_low, Low32Bits(k2Pow32EncodingForDouble));
1734 __ LoadImmediate(constant_high, High32Bits(k2Pow32EncodingForDouble));
1735 __ vmovdrr(temp2_d, constant_low, constant_high);
1736 // temp1_d = temp1_d * 2^32
1737 __ vmuld(temp1_d, temp1_d, temp2_d);
1738 // temp2_d = unsigned-to-double(low)
1739 __ vmovsr(temp2_s, low);
1740 __ vcvtdu(temp2_d, temp2_s);
1741 // temp1_d = temp1_d + temp2_d
1742 __ vaddd(temp1_d, temp1_d, temp2_d);
1743 // output = double-to-float(temp1_d);
1744 __ vcvtsd(output, temp1_d);
1745 break;
1746 }
1747
Roland Levillaincff13742014-11-17 14:32:17 +00001748 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001749 // Processing a Dex `double-to-float' instruction.
1750 __ vcvtsd(out.AsFpuRegister<SRegister>(),
1751 FromLowSToD(in.AsFpuRegisterPairLow<SRegister>()));
Roland Levillaincff13742014-11-17 14:32:17 +00001752 break;
1753
1754 default:
1755 LOG(FATAL) << "Unexpected type conversion from " << input_type
1756 << " to " << result_type;
1757 };
1758 break;
1759
Roland Levillaindff1f282014-11-05 14:15:05 +00001760 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00001761 switch (input_type) {
1762 case Primitive::kPrimByte:
1763 case Primitive::kPrimShort:
1764 case Primitive::kPrimInt:
1765 case Primitive::kPrimChar: {
1766 // Processing a Dex `int-to-double' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00001767 __ vmovsr(out.AsFpuRegisterPairLow<SRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00001768 __ vcvtdi(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
1769 out.AsFpuRegisterPairLow<SRegister>());
1770 break;
1771 }
1772
Roland Levillain647b9ed2014-11-27 12:06:00 +00001773 case Primitive::kPrimLong: {
1774 // Processing a Dex `long-to-double' instruction.
1775 Register low = in.AsRegisterPairLow<Register>();
1776 Register high = in.AsRegisterPairHigh<Register>();
1777 SRegister out_s = out.AsFpuRegisterPairLow<SRegister>();
1778 DRegister out_d = FromLowSToD(out_s);
Roland Levillain271ab9c2014-11-27 15:23:57 +00001779 Register constant_low = locations->GetTemp(0).AsRegister<Register>();
1780 Register constant_high = locations->GetTemp(1).AsRegister<Register>();
Roland Levillain647b9ed2014-11-27 12:06:00 +00001781 SRegister temp_s = locations->GetTemp(2).AsFpuRegisterPairLow<SRegister>();
1782 DRegister temp_d = FromLowSToD(temp_s);
1783
Roland Levillain647b9ed2014-11-27 12:06:00 +00001784 // out_d = int-to-double(high)
1785 __ vmovsr(out_s, high);
1786 __ vcvtdi(out_d, out_s);
Roland Levillain6d0e4832014-11-27 18:31:21 +00001787 // Using vmovd to load the `k2Pow32EncodingForDouble` constant
1788 // as an immediate value into `temp_d` does not work, as
1789 // this instruction only transfers 8 significant bits of its
1790 // immediate operand. Instead, use two 32-bit core
1791 // registers to load `k2Pow32EncodingForDouble` into `temp_d`.
1792 __ LoadImmediate(constant_low, Low32Bits(k2Pow32EncodingForDouble));
1793 __ LoadImmediate(constant_high, High32Bits(k2Pow32EncodingForDouble));
Roland Levillain647b9ed2014-11-27 12:06:00 +00001794 __ vmovdrr(temp_d, constant_low, constant_high);
1795 // out_d = out_d * 2^32
1796 __ vmuld(out_d, out_d, temp_d);
1797 // temp_d = unsigned-to-double(low)
1798 __ vmovsr(temp_s, low);
1799 __ vcvtdu(temp_d, temp_s);
1800 // out_d = out_d + temp_d
1801 __ vaddd(out_d, out_d, temp_d);
1802 break;
1803 }
1804
Roland Levillaincff13742014-11-17 14:32:17 +00001805 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001806 // Processing a Dex `float-to-double' instruction.
1807 __ vcvtds(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
1808 in.AsFpuRegister<SRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00001809 break;
1810
1811 default:
1812 LOG(FATAL) << "Unexpected type conversion from " << input_type
1813 << " to " << result_type;
1814 };
Roland Levillaindff1f282014-11-05 14:15:05 +00001815 break;
1816
1817 default:
1818 LOG(FATAL) << "Unexpected type conversion from " << input_type
1819 << " to " << result_type;
1820 }
1821}
1822
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001823void LocationsBuilderARM::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001824 LocationSummary* locations =
1825 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001826 switch (add->GetResultType()) {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001827 case Primitive::kPrimInt:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001828 case Primitive::kPrimLong: {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01001829 bool output_overlaps = (add->GetResultType() == Primitive::kPrimLong);
1830 locations->SetInAt(0, Location::RequiresRegister());
1831 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
1832 locations->SetOut(Location::RequiresRegister(), output_overlaps);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001833 break;
1834 }
1835
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001836 case Primitive::kPrimFloat:
1837 case Primitive::kPrimDouble: {
1838 locations->SetInAt(0, Location::RequiresFpuRegister());
1839 locations->SetInAt(1, Location::RequiresFpuRegister());
Calin Juravle7c4954d2014-10-28 16:57:40 +00001840 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001841 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001842 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001843
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001844 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001845 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001846 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001847}
1848
1849void InstructionCodeGeneratorARM::VisitAdd(HAdd* add) {
1850 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001851 Location out = locations->Out();
1852 Location first = locations->InAt(0);
1853 Location second = locations->InAt(1);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001854 switch (add->GetResultType()) {
1855 case Primitive::kPrimInt:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001856 if (second.IsRegister()) {
Roland Levillain199f3362014-11-27 17:15:16 +00001857 __ add(out.AsRegister<Register>(),
1858 first.AsRegister<Register>(),
1859 ShifterOperand(second.AsRegister<Register>()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001860 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001861 __ AddConstant(out.AsRegister<Register>(),
1862 first.AsRegister<Register>(),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001863 second.GetConstant()->AsIntConstant()->GetValue());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001864 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001865 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001866
1867 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001868 __ adds(out.AsRegisterPairLow<Register>(),
1869 first.AsRegisterPairLow<Register>(),
1870 ShifterOperand(second.AsRegisterPairLow<Register>()));
1871 __ adc(out.AsRegisterPairHigh<Register>(),
1872 first.AsRegisterPairHigh<Register>(),
1873 ShifterOperand(second.AsRegisterPairHigh<Register>()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001874 break;
1875
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001876 case Primitive::kPrimFloat:
Roland Levillain199f3362014-11-27 17:15:16 +00001877 __ vadds(out.AsFpuRegister<SRegister>(),
1878 first.AsFpuRegister<SRegister>(),
1879 second.AsFpuRegister<SRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001880 break;
1881
1882 case Primitive::kPrimDouble:
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001883 __ vaddd(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
1884 FromLowSToD(first.AsFpuRegisterPairLow<SRegister>()),
1885 FromLowSToD(second.AsFpuRegisterPairLow<SRegister>()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001886 break;
1887
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001888 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001889 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001890 }
1891}
1892
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001893void LocationsBuilderARM::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001894 LocationSummary* locations =
1895 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001896 switch (sub->GetResultType()) {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001897 case Primitive::kPrimInt:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001898 case Primitive::kPrimLong: {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01001899 bool output_overlaps = (sub->GetResultType() == Primitive::kPrimLong);
1900 locations->SetInAt(0, Location::RequiresRegister());
1901 locations->SetInAt(1, Location::RegisterOrConstant(sub->InputAt(1)));
1902 locations->SetOut(Location::RequiresRegister(), output_overlaps);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001903 break;
1904 }
Calin Juravle11351682014-10-23 15:38:15 +01001905 case Primitive::kPrimFloat:
1906 case Primitive::kPrimDouble: {
1907 locations->SetInAt(0, Location::RequiresFpuRegister());
1908 locations->SetInAt(1, Location::RequiresFpuRegister());
Calin Juravle7c4954d2014-10-28 16:57:40 +00001909 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001910 break;
Calin Juravle11351682014-10-23 15:38:15 +01001911 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001912 default:
Calin Juravle11351682014-10-23 15:38:15 +01001913 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001914 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001915}
1916
1917void InstructionCodeGeneratorARM::VisitSub(HSub* sub) {
1918 LocationSummary* locations = sub->GetLocations();
Calin Juravle11351682014-10-23 15:38:15 +01001919 Location out = locations->Out();
1920 Location first = locations->InAt(0);
1921 Location second = locations->InAt(1);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001922 switch (sub->GetResultType()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001923 case Primitive::kPrimInt: {
Calin Juravle11351682014-10-23 15:38:15 +01001924 if (second.IsRegister()) {
Roland Levillain199f3362014-11-27 17:15:16 +00001925 __ sub(out.AsRegister<Register>(),
1926 first.AsRegister<Register>(),
1927 ShifterOperand(second.AsRegister<Register>()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001928 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001929 __ AddConstant(out.AsRegister<Register>(),
1930 first.AsRegister<Register>(),
Calin Juravle11351682014-10-23 15:38:15 +01001931 -second.GetConstant()->AsIntConstant()->GetValue());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001932 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001933 break;
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001934 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001935
Calin Juravle11351682014-10-23 15:38:15 +01001936 case Primitive::kPrimLong: {
1937 __ subs(out.AsRegisterPairLow<Register>(),
1938 first.AsRegisterPairLow<Register>(),
1939 ShifterOperand(second.AsRegisterPairLow<Register>()));
1940 __ sbc(out.AsRegisterPairHigh<Register>(),
1941 first.AsRegisterPairHigh<Register>(),
1942 ShifterOperand(second.AsRegisterPairHigh<Register>()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001943 break;
Calin Juravle11351682014-10-23 15:38:15 +01001944 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001945
Calin Juravle11351682014-10-23 15:38:15 +01001946 case Primitive::kPrimFloat: {
Roland Levillain199f3362014-11-27 17:15:16 +00001947 __ vsubs(out.AsFpuRegister<SRegister>(),
1948 first.AsFpuRegister<SRegister>(),
1949 second.AsFpuRegister<SRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001950 break;
Calin Juravle11351682014-10-23 15:38:15 +01001951 }
1952
1953 case Primitive::kPrimDouble: {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001954 __ vsubd(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
1955 FromLowSToD(first.AsFpuRegisterPairLow<SRegister>()),
1956 FromLowSToD(second.AsFpuRegisterPairLow<SRegister>()));
Calin Juravle11351682014-10-23 15:38:15 +01001957 break;
1958 }
1959
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001960
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001961 default:
Calin Juravle11351682014-10-23 15:38:15 +01001962 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001963 }
1964}
1965
Calin Juravle34bacdf2014-10-07 20:23:36 +01001966void LocationsBuilderARM::VisitMul(HMul* mul) {
1967 LocationSummary* locations =
1968 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
1969 switch (mul->GetResultType()) {
1970 case Primitive::kPrimInt:
1971 case Primitive::kPrimLong: {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01001972 locations->SetInAt(0, Location::RequiresRegister());
1973 locations->SetInAt(1, Location::RequiresRegister());
1974 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Calin Juravle34bacdf2014-10-07 20:23:36 +01001975 break;
1976 }
1977
Calin Juravleb5bfa962014-10-21 18:02:24 +01001978 case Primitive::kPrimFloat:
1979 case Primitive::kPrimDouble: {
1980 locations->SetInAt(0, Location::RequiresFpuRegister());
1981 locations->SetInAt(1, Location::RequiresFpuRegister());
Calin Juravle7c4954d2014-10-28 16:57:40 +00001982 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Calin Juravle34bacdf2014-10-07 20:23:36 +01001983 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01001984 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01001985
1986 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01001987 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01001988 }
1989}
1990
1991void InstructionCodeGeneratorARM::VisitMul(HMul* mul) {
1992 LocationSummary* locations = mul->GetLocations();
1993 Location out = locations->Out();
1994 Location first = locations->InAt(0);
1995 Location second = locations->InAt(1);
1996 switch (mul->GetResultType()) {
1997 case Primitive::kPrimInt: {
Roland Levillain199f3362014-11-27 17:15:16 +00001998 __ mul(out.AsRegister<Register>(),
1999 first.AsRegister<Register>(),
2000 second.AsRegister<Register>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002001 break;
2002 }
2003 case Primitive::kPrimLong: {
2004 Register out_hi = out.AsRegisterPairHigh<Register>();
2005 Register out_lo = out.AsRegisterPairLow<Register>();
2006 Register in1_hi = first.AsRegisterPairHigh<Register>();
2007 Register in1_lo = first.AsRegisterPairLow<Register>();
2008 Register in2_hi = second.AsRegisterPairHigh<Register>();
2009 Register in2_lo = second.AsRegisterPairLow<Register>();
2010
2011 // Extra checks to protect caused by the existence of R1_R2.
2012 // The algorithm is wrong if out.hi is either in1.lo or in2.lo:
2013 // (e.g. in1=r0_r1, in2=r2_r3 and out=r1_r2);
2014 DCHECK_NE(out_hi, in1_lo);
2015 DCHECK_NE(out_hi, in2_lo);
2016
2017 // input: in1 - 64 bits, in2 - 64 bits
2018 // output: out
2019 // formula: out.hi : out.lo = (in1.lo * in2.hi + in1.hi * in2.lo)* 2^32 + in1.lo * in2.lo
2020 // parts: out.hi = in1.lo * in2.hi + in1.hi * in2.lo + (in1.lo * in2.lo)[63:32]
2021 // parts: out.lo = (in1.lo * in2.lo)[31:0]
2022
2023 // IP <- in1.lo * in2.hi
2024 __ mul(IP, in1_lo, in2_hi);
2025 // out.hi <- in1.lo * in2.hi + in1.hi * in2.lo
2026 __ mla(out_hi, in1_hi, in2_lo, IP);
2027 // out.lo <- (in1.lo * in2.lo)[31:0];
2028 __ umull(out_lo, IP, in1_lo, in2_lo);
2029 // out.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
2030 __ add(out_hi, out_hi, ShifterOperand(IP));
2031 break;
2032 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01002033
2034 case Primitive::kPrimFloat: {
Roland Levillain199f3362014-11-27 17:15:16 +00002035 __ vmuls(out.AsFpuRegister<SRegister>(),
2036 first.AsFpuRegister<SRegister>(),
2037 second.AsFpuRegister<SRegister>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002038 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01002039 }
2040
2041 case Primitive::kPrimDouble: {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00002042 __ vmuld(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
2043 FromLowSToD(first.AsFpuRegisterPairLow<SRegister>()),
2044 FromLowSToD(second.AsFpuRegisterPairLow<SRegister>()));
Calin Juravleb5bfa962014-10-21 18:02:24 +01002045 break;
2046 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002047
2048 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01002049 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002050 }
2051}
2052
Calin Juravle7c4954d2014-10-28 16:57:40 +00002053void LocationsBuilderARM::VisitDiv(HDiv* div) {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002054 LocationSummary::CallKind call_kind = div->GetResultType() == Primitive::kPrimLong
2055 ? LocationSummary::kCall
2056 : LocationSummary::kNoCall;
2057 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(div, call_kind);
2058
Calin Juravle7c4954d2014-10-28 16:57:40 +00002059 switch (div->GetResultType()) {
Calin Juravled0d48522014-11-04 16:40:20 +00002060 case Primitive::kPrimInt: {
2061 locations->SetInAt(0, Location::RequiresRegister());
2062 locations->SetInAt(1, Location::RequiresRegister());
2063 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2064 break;
2065 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00002066 case Primitive::kPrimLong: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002067 InvokeRuntimeCallingConvention calling_convention;
2068 locations->SetInAt(0, Location::RegisterPairLocation(
2069 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
2070 locations->SetInAt(1, Location::RegisterPairLocation(
2071 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
2072 // The runtime helper puts the output in R0,R2.
2073 locations->SetOut(Location::RegisterPairLocation(R0, R2));
Calin Juravle7c4954d2014-10-28 16:57:40 +00002074 break;
2075 }
2076 case Primitive::kPrimFloat:
2077 case Primitive::kPrimDouble: {
2078 locations->SetInAt(0, Location::RequiresFpuRegister());
2079 locations->SetInAt(1, Location::RequiresFpuRegister());
2080 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2081 break;
2082 }
2083
2084 default:
2085 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2086 }
2087}
2088
2089void InstructionCodeGeneratorARM::VisitDiv(HDiv* div) {
2090 LocationSummary* locations = div->GetLocations();
2091 Location out = locations->Out();
2092 Location first = locations->InAt(0);
2093 Location second = locations->InAt(1);
2094
2095 switch (div->GetResultType()) {
Calin Juravled0d48522014-11-04 16:40:20 +00002096 case Primitive::kPrimInt: {
Roland Levillain199f3362014-11-27 17:15:16 +00002097 __ sdiv(out.AsRegister<Register>(),
2098 first.AsRegister<Register>(),
2099 second.AsRegister<Register>());
Calin Juravled0d48522014-11-04 16:40:20 +00002100 break;
2101 }
2102
Calin Juravle7c4954d2014-10-28 16:57:40 +00002103 case Primitive::kPrimLong: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002104 InvokeRuntimeCallingConvention calling_convention;
2105 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegisterPairLow<Register>());
2106 DCHECK_EQ(calling_convention.GetRegisterAt(1), first.AsRegisterPairHigh<Register>());
2107 DCHECK_EQ(calling_convention.GetRegisterAt(2), second.AsRegisterPairLow<Register>());
2108 DCHECK_EQ(calling_convention.GetRegisterAt(3), second.AsRegisterPairHigh<Register>());
2109 DCHECK_EQ(R0, out.AsRegisterPairLow<Register>());
2110 DCHECK_EQ(R2, out.AsRegisterPairHigh<Register>());
2111
2112 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pLdiv), div, div->GetDexPc());
Calin Juravle7c4954d2014-10-28 16:57:40 +00002113 break;
2114 }
2115
2116 case Primitive::kPrimFloat: {
Roland Levillain199f3362014-11-27 17:15:16 +00002117 __ vdivs(out.AsFpuRegister<SRegister>(),
2118 first.AsFpuRegister<SRegister>(),
2119 second.AsFpuRegister<SRegister>());
Calin Juravle7c4954d2014-10-28 16:57:40 +00002120 break;
2121 }
2122
2123 case Primitive::kPrimDouble: {
2124 __ vdivd(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
2125 FromLowSToD(first.AsFpuRegisterPairLow<SRegister>()),
2126 FromLowSToD(second.AsFpuRegisterPairLow<SRegister>()));
2127 break;
2128 }
2129
2130 default:
2131 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2132 }
2133}
2134
Calin Juravlebacfec32014-11-14 15:54:36 +00002135void LocationsBuilderARM::VisitRem(HRem* rem) {
Calin Juravled2ec87d2014-12-08 14:24:46 +00002136 Primitive::Type type = rem->GetResultType();
2137 LocationSummary::CallKind call_kind = type == Primitive::kPrimInt
2138 ? LocationSummary::kNoCall
2139 : LocationSummary::kCall;
Calin Juravlebacfec32014-11-14 15:54:36 +00002140 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
2141
Calin Juravled2ec87d2014-12-08 14:24:46 +00002142 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00002143 case Primitive::kPrimInt: {
2144 locations->SetInAt(0, Location::RequiresRegister());
2145 locations->SetInAt(1, Location::RequiresRegister());
2146 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2147 locations->AddTemp(Location::RequiresRegister());
2148 break;
2149 }
2150 case Primitive::kPrimLong: {
2151 InvokeRuntimeCallingConvention calling_convention;
2152 locations->SetInAt(0, Location::RegisterPairLocation(
2153 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
2154 locations->SetInAt(1, Location::RegisterPairLocation(
2155 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
2156 // The runtime helper puts the output in R2,R3.
2157 locations->SetOut(Location::RegisterPairLocation(R2, R3));
2158 break;
2159 }
Calin Juravled2ec87d2014-12-08 14:24:46 +00002160 case Primitive::kPrimFloat: {
2161 InvokeRuntimeCallingConvention calling_convention;
2162 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
2163 locations->SetInAt(1, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(1)));
2164 locations->SetOut(Location::FpuRegisterLocation(S0));
2165 break;
2166 }
2167
Calin Juravlebacfec32014-11-14 15:54:36 +00002168 case Primitive::kPrimDouble: {
Calin Juravled2ec87d2014-12-08 14:24:46 +00002169 InvokeRuntimeCallingConvention calling_convention;
2170 locations->SetInAt(0, Location::FpuRegisterPairLocation(
2171 calling_convention.GetFpuRegisterAt(0), calling_convention.GetFpuRegisterAt(1)));
2172 locations->SetInAt(1, Location::FpuRegisterPairLocation(
2173 calling_convention.GetFpuRegisterAt(2), calling_convention.GetFpuRegisterAt(3)));
2174 locations->SetOut(Location::Location::FpuRegisterPairLocation(S0, S1));
Calin Juravlebacfec32014-11-14 15:54:36 +00002175 break;
2176 }
2177
2178 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00002179 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00002180 }
2181}
2182
2183void InstructionCodeGeneratorARM::VisitRem(HRem* rem) {
2184 LocationSummary* locations = rem->GetLocations();
2185 Location out = locations->Out();
2186 Location first = locations->InAt(0);
2187 Location second = locations->InAt(1);
2188
Calin Juravled2ec87d2014-12-08 14:24:46 +00002189 Primitive::Type type = rem->GetResultType();
2190 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00002191 case Primitive::kPrimInt: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002192 Register reg1 = first.AsRegister<Register>();
2193 Register reg2 = second.AsRegister<Register>();
2194 Register temp = locations->GetTemp(0).AsRegister<Register>();
Calin Juravlebacfec32014-11-14 15:54:36 +00002195
2196 // temp = reg1 / reg2 (integer division)
2197 // temp = temp * reg2
2198 // dest = reg1 - temp
2199 __ sdiv(temp, reg1, reg2);
2200 __ mul(temp, temp, reg2);
Roland Levillain271ab9c2014-11-27 15:23:57 +00002201 __ sub(out.AsRegister<Register>(), reg1, ShifterOperand(temp));
Calin Juravlebacfec32014-11-14 15:54:36 +00002202 break;
2203 }
2204
2205 case Primitive::kPrimLong: {
Calin Juravlebacfec32014-11-14 15:54:36 +00002206 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pLmod), rem, rem->GetDexPc());
2207 break;
2208 }
2209
Calin Juravled2ec87d2014-12-08 14:24:46 +00002210 case Primitive::kPrimFloat: {
2211 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pFmodf), rem, rem->GetDexPc());
2212 break;
2213 }
2214
Calin Juravlebacfec32014-11-14 15:54:36 +00002215 case Primitive::kPrimDouble: {
Calin Juravled2ec87d2014-12-08 14:24:46 +00002216 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pFmod), rem, rem->GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00002217 break;
2218 }
2219
2220 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00002221 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00002222 }
2223}
2224
Calin Juravled0d48522014-11-04 16:40:20 +00002225void LocationsBuilderARM::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2226 LocationSummary* locations =
2227 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002228 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Calin Juravled0d48522014-11-04 16:40:20 +00002229 if (instruction->HasUses()) {
2230 locations->SetOut(Location::SameAsFirstInput());
2231 }
2232}
2233
2234void InstructionCodeGeneratorARM::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2235 SlowPathCodeARM* slow_path = new (GetGraph()->GetArena()) DivZeroCheckSlowPathARM(instruction);
2236 codegen_->AddSlowPath(slow_path);
2237
2238 LocationSummary* locations = instruction->GetLocations();
2239 Location value = locations->InAt(0);
2240
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002241 switch (instruction->GetType()) {
2242 case Primitive::kPrimInt: {
2243 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002244 __ cmp(value.AsRegister<Register>(), ShifterOperand(0));
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002245 __ b(slow_path->GetEntryLabel(), EQ);
2246 } else {
2247 DCHECK(value.IsConstant()) << value;
2248 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
2249 __ b(slow_path->GetEntryLabel());
2250 }
2251 }
2252 break;
2253 }
2254 case Primitive::kPrimLong: {
2255 if (value.IsRegisterPair()) {
2256 __ orrs(IP,
2257 value.AsRegisterPairLow<Register>(),
2258 ShifterOperand(value.AsRegisterPairHigh<Register>()));
2259 __ b(slow_path->GetEntryLabel(), EQ);
2260 } else {
2261 DCHECK(value.IsConstant()) << value;
2262 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
2263 __ b(slow_path->GetEntryLabel());
2264 }
2265 }
2266 break;
2267 default:
2268 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
2269 }
2270 }
Calin Juravled0d48522014-11-04 16:40:20 +00002271}
2272
Calin Juravle9aec02f2014-11-18 23:06:35 +00002273void LocationsBuilderARM::HandleShift(HBinaryOperation* op) {
2274 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
2275
2276 LocationSummary::CallKind call_kind = op->GetResultType() == Primitive::kPrimLong
2277 ? LocationSummary::kCall
2278 : LocationSummary::kNoCall;
2279 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(op, call_kind);
2280
2281 switch (op->GetResultType()) {
2282 case Primitive::kPrimInt: {
2283 locations->SetInAt(0, Location::RequiresRegister());
2284 locations->SetInAt(1, Location::RegisterOrConstant(op->InputAt(1)));
2285 locations->SetOut(Location::RequiresRegister());
2286 break;
2287 }
2288 case Primitive::kPrimLong: {
2289 InvokeRuntimeCallingConvention calling_convention;
2290 locations->SetInAt(0, Location::RegisterPairLocation(
2291 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
2292 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
2293 // The runtime helper puts the output in R0,R2.
2294 locations->SetOut(Location::RegisterPairLocation(R0, R2));
2295 break;
2296 }
2297 default:
2298 LOG(FATAL) << "Unexpected operation type " << op->GetResultType();
2299 }
2300}
2301
2302void InstructionCodeGeneratorARM::HandleShift(HBinaryOperation* op) {
2303 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
2304
2305 LocationSummary* locations = op->GetLocations();
2306 Location out = locations->Out();
2307 Location first = locations->InAt(0);
2308 Location second = locations->InAt(1);
2309
2310 Primitive::Type type = op->GetResultType();
2311 switch (type) {
2312 case Primitive::kPrimInt: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002313 Register out_reg = out.AsRegister<Register>();
2314 Register first_reg = first.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00002315 // Arm doesn't mask the shift count so we need to do it ourselves.
2316 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002317 Register second_reg = second.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00002318 __ and_(second_reg, second_reg, ShifterOperand(kMaxIntShiftValue));
2319 if (op->IsShl()) {
2320 __ Lsl(out_reg, first_reg, second_reg);
2321 } else if (op->IsShr()) {
2322 __ Asr(out_reg, first_reg, second_reg);
2323 } else {
2324 __ Lsr(out_reg, first_reg, second_reg);
2325 }
2326 } else {
2327 int32_t cst = second.GetConstant()->AsIntConstant()->GetValue();
2328 uint32_t shift_value = static_cast<uint32_t>(cst & kMaxIntShiftValue);
2329 if (shift_value == 0) { // arm does not support shifting with 0 immediate.
2330 __ Mov(out_reg, first_reg);
2331 } else if (op->IsShl()) {
2332 __ Lsl(out_reg, first_reg, shift_value);
2333 } else if (op->IsShr()) {
2334 __ Asr(out_reg, first_reg, shift_value);
2335 } else {
2336 __ Lsr(out_reg, first_reg, shift_value);
2337 }
2338 }
2339 break;
2340 }
2341 case Primitive::kPrimLong: {
2342 // TODO: Inline the assembly instead of calling the runtime.
2343 InvokeRuntimeCallingConvention calling_convention;
2344 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegisterPairLow<Register>());
2345 DCHECK_EQ(calling_convention.GetRegisterAt(1), first.AsRegisterPairHigh<Register>());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002346 DCHECK_EQ(calling_convention.GetRegisterAt(2), second.AsRegister<Register>());
Calin Juravle9aec02f2014-11-18 23:06:35 +00002347 DCHECK_EQ(R0, out.AsRegisterPairLow<Register>());
2348 DCHECK_EQ(R2, out.AsRegisterPairHigh<Register>());
2349
2350 int32_t entry_point_offset;
2351 if (op->IsShl()) {
2352 entry_point_offset = QUICK_ENTRY_POINT(pShlLong);
2353 } else if (op->IsShr()) {
2354 entry_point_offset = QUICK_ENTRY_POINT(pShrLong);
2355 } else {
2356 entry_point_offset = QUICK_ENTRY_POINT(pUshrLong);
2357 }
2358 __ LoadFromOffset(kLoadWord, LR, TR, entry_point_offset);
2359 __ blx(LR);
2360 break;
2361 }
2362 default:
2363 LOG(FATAL) << "Unexpected operation type " << type;
2364 }
2365}
2366
2367void LocationsBuilderARM::VisitShl(HShl* shl) {
2368 HandleShift(shl);
2369}
2370
2371void InstructionCodeGeneratorARM::VisitShl(HShl* shl) {
2372 HandleShift(shl);
2373}
2374
2375void LocationsBuilderARM::VisitShr(HShr* shr) {
2376 HandleShift(shr);
2377}
2378
2379void InstructionCodeGeneratorARM::VisitShr(HShr* shr) {
2380 HandleShift(shr);
2381}
2382
2383void LocationsBuilderARM::VisitUShr(HUShr* ushr) {
2384 HandleShift(ushr);
2385}
2386
2387void InstructionCodeGeneratorARM::VisitUShr(HUShr* ushr) {
2388 HandleShift(ushr);
2389}
2390
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002391void LocationsBuilderARM::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002392 LocationSummary* locations =
2393 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002394 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002395 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2396 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
2397 locations->SetOut(Location::RegisterLocation(R0));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002398}
2399
2400void InstructionCodeGeneratorARM::VisitNewInstance(HNewInstance* instruction) {
2401 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002402 codegen_->LoadCurrentMethod(calling_convention.GetRegisterAt(1));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002403 __ LoadImmediate(calling_convention.GetRegisterAt(0), instruction->GetTypeIndex());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002404 codegen_->InvokeRuntime(
2405 QUICK_ENTRY_POINT(pAllocObjectWithAccessCheck), instruction, instruction->GetDexPc());
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002406}
2407
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002408void LocationsBuilderARM::VisitNewArray(HNewArray* instruction) {
2409 LocationSummary* locations =
2410 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
2411 InvokeRuntimeCallingConvention calling_convention;
2412 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2413 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
2414 locations->SetOut(Location::RegisterLocation(R0));
2415 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
2416}
2417
2418void InstructionCodeGeneratorARM::VisitNewArray(HNewArray* instruction) {
2419 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002420 codegen_->LoadCurrentMethod(calling_convention.GetRegisterAt(1));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002421 __ LoadImmediate(calling_convention.GetRegisterAt(0), instruction->GetTypeIndex());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002422 codegen_->InvokeRuntime(
2423 QUICK_ENTRY_POINT(pAllocArrayWithAccessCheck), instruction, instruction->GetDexPc());
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002424}
2425
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002426void LocationsBuilderARM::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002427 LocationSummary* locations =
2428 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya747a392014-04-17 14:56:23 +01002429 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
2430 if (location.IsStackSlot()) {
2431 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
2432 } else if (location.IsDoubleStackSlot()) {
2433 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002434 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01002435 locations->SetOut(location);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002436}
2437
2438void InstructionCodeGeneratorARM::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002439 // Nothing to do, the parameter is already at its location.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002440 UNUSED(instruction);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002441}
2442
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002443void LocationsBuilderARM::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002444 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002445 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002446 locations->SetInAt(0, Location::RequiresRegister());
2447 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01002448}
2449
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002450void InstructionCodeGeneratorARM::VisitNot(HNot* not_) {
2451 LocationSummary* locations = not_->GetLocations();
2452 Location out = locations->Out();
2453 Location in = locations->InAt(0);
2454 switch (not_->InputAt(0)->GetType()) {
2455 case Primitive::kPrimBoolean:
Roland Levillain271ab9c2014-11-27 15:23:57 +00002456 __ eor(out.AsRegister<Register>(), in.AsRegister<Register>(), ShifterOperand(1));
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002457 break;
2458
2459 case Primitive::kPrimInt:
Roland Levillain271ab9c2014-11-27 15:23:57 +00002460 __ mvn(out.AsRegister<Register>(), ShifterOperand(in.AsRegister<Register>()));
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002461 break;
2462
2463 case Primitive::kPrimLong:
Roland Levillain70566432014-10-24 16:20:17 +01002464 __ mvn(out.AsRegisterPairLow<Register>(),
2465 ShifterOperand(in.AsRegisterPairLow<Register>()));
2466 __ mvn(out.AsRegisterPairHigh<Register>(),
2467 ShifterOperand(in.AsRegisterPairHigh<Register>()));
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002468 break;
2469
2470 default:
2471 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
2472 }
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01002473}
2474
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002475void LocationsBuilderARM::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002476 LocationSummary* locations =
2477 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +00002478 switch (compare->InputAt(0)->GetType()) {
2479 case Primitive::kPrimLong: {
2480 locations->SetInAt(0, Location::RequiresRegister());
2481 locations->SetInAt(1, Location::RequiresRegister());
2482 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2483 break;
2484 }
2485 case Primitive::kPrimFloat:
2486 case Primitive::kPrimDouble: {
2487 locations->SetInAt(0, Location::RequiresFpuRegister());
2488 locations->SetInAt(1, Location::RequiresFpuRegister());
2489 locations->SetOut(Location::RequiresRegister());
2490 break;
2491 }
2492 default:
2493 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
2494 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002495}
2496
2497void InstructionCodeGeneratorARM::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002498 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002499 Register out = locations->Out().AsRegister<Register>();
Calin Juravleddb7df22014-11-25 20:56:51 +00002500 Location left = locations->InAt(0);
2501 Location right = locations->InAt(1);
2502
2503 Label less, greater, done;
2504 Primitive::Type type = compare->InputAt(0)->GetType();
2505 switch (type) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002506 case Primitive::kPrimLong: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002507 __ cmp(left.AsRegisterPairHigh<Register>(),
2508 ShifterOperand(right.AsRegisterPairHigh<Register>())); // Signed compare.
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002509 __ b(&less, LT);
2510 __ b(&greater, GT);
Calin Juravleddb7df22014-11-25 20:56:51 +00002511 // Do LoadImmediate before any `cmp`, as LoadImmediate might affect the status flags.
2512 __ LoadImmediate(out, 0);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002513 __ cmp(left.AsRegisterPairLow<Register>(),
2514 ShifterOperand(right.AsRegisterPairLow<Register>())); // Unsigned compare.
Calin Juravleddb7df22014-11-25 20:56:51 +00002515 break;
2516 }
2517 case Primitive::kPrimFloat:
2518 case Primitive::kPrimDouble: {
2519 __ LoadImmediate(out, 0);
2520 if (type == Primitive::kPrimFloat) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002521 __ vcmps(left.AsFpuRegister<SRegister>(), right.AsFpuRegister<SRegister>());
Calin Juravleddb7df22014-11-25 20:56:51 +00002522 } else {
2523 __ vcmpd(FromLowSToD(left.AsFpuRegisterPairLow<SRegister>()),
2524 FromLowSToD(right.AsFpuRegisterPairLow<SRegister>()));
2525 }
2526 __ vmstat(); // transfer FP status register to ARM APSR.
2527 __ b(compare->IsGtBias() ? &greater : &less, VS); // VS for unordered.
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002528 break;
2529 }
2530 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00002531 LOG(FATAL) << "Unexpected compare type " << type;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002532 }
Calin Juravleddb7df22014-11-25 20:56:51 +00002533 __ b(&done, EQ);
2534 __ b(&less, CC); // CC is for both: unsigned compare for longs and 'less than' for floats.
2535
2536 __ Bind(&greater);
2537 __ LoadImmediate(out, 1);
2538 __ b(&done);
2539
2540 __ Bind(&less);
2541 __ LoadImmediate(out, -1);
2542
2543 __ Bind(&done);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002544}
2545
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002546void LocationsBuilderARM::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002547 LocationSummary* locations =
2548 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01002549 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
2550 locations->SetInAt(i, Location::Any());
2551 }
2552 locations->SetOut(Location::Any());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002553}
2554
2555void InstructionCodeGeneratorARM::VisitPhi(HPhi* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002556 UNUSED(instruction);
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01002557 LOG(FATAL) << "Unreachable";
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002558}
2559
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002560void LocationsBuilderARM::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002561 LocationSummary* locations =
2562 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002563 bool needs_write_barrier =
2564 CodeGenerator::StoreNeedsWriteBarrier(instruction->GetFieldType(), instruction->GetValue());
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002565 locations->SetInAt(0, Location::RequiresRegister());
2566 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01002567 // Temporary registers for the write barrier.
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002568 if (needs_write_barrier) {
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01002569 locations->AddTemp(Location::RequiresRegister());
2570 locations->AddTemp(Location::RequiresRegister());
2571 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002572}
2573
2574void InstructionCodeGeneratorARM::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
2575 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002576 Register obj = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002577 uint32_t offset = instruction->GetFieldOffset().Uint32Value();
Nicolas Geoffray39468442014-09-02 15:17:15 +01002578 Primitive::Type field_type = instruction->GetFieldType();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002579
2580 switch (field_type) {
2581 case Primitive::kPrimBoolean:
2582 case Primitive::kPrimByte: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002583 Register value = locations->InAt(1).AsRegister<Register>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002584 __ StoreToOffset(kStoreByte, value, obj, offset);
2585 break;
2586 }
2587
2588 case Primitive::kPrimShort:
2589 case Primitive::kPrimChar: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002590 Register value = locations->InAt(1).AsRegister<Register>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002591 __ StoreToOffset(kStoreHalfword, value, obj, offset);
2592 break;
2593 }
2594
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002595 case Primitive::kPrimInt:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002596 case Primitive::kPrimNot: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002597 Register value = locations->InAt(1).AsRegister<Register>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002598 __ StoreToOffset(kStoreWord, value, obj, offset);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002599 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->GetValue())) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002600 Register temp = locations->GetTemp(0).AsRegister<Register>();
2601 Register card = locations->GetTemp(1).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002602 codegen_->MarkGCCard(temp, card, obj, value);
2603 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002604 break;
2605 }
2606
2607 case Primitive::kPrimLong: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002608 Location value = locations->InAt(1);
2609 __ StoreToOffset(kStoreWordPair, value.AsRegisterPairLow<Register>(), obj, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002610 break;
2611 }
2612
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002613 case Primitive::kPrimFloat: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002614 SRegister value = locations->InAt(1).AsFpuRegister<SRegister>();
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002615 __ StoreSToOffset(value, obj, offset);
2616 break;
2617 }
2618
2619 case Primitive::kPrimDouble: {
2620 DRegister value = FromLowSToD(locations->InAt(1).AsFpuRegisterPairLow<SRegister>());
2621 __ StoreDToOffset(value, obj, offset);
2622 break;
2623 }
2624
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002625 case Primitive::kPrimVoid:
2626 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07002627 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002628 }
2629}
2630
2631void LocationsBuilderARM::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002632 LocationSummary* locations =
2633 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002634 locations->SetInAt(0, Location::RequiresRegister());
2635 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002636}
2637
2638void InstructionCodeGeneratorARM::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
2639 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002640 Register obj = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002641 uint32_t offset = instruction->GetFieldOffset().Uint32Value();
2642
2643 switch (instruction->GetType()) {
2644 case Primitive::kPrimBoolean: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002645 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002646 __ LoadFromOffset(kLoadUnsignedByte, out, obj, offset);
2647 break;
2648 }
2649
2650 case Primitive::kPrimByte: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002651 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002652 __ LoadFromOffset(kLoadSignedByte, out, obj, offset);
2653 break;
2654 }
2655
2656 case Primitive::kPrimShort: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002657 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002658 __ LoadFromOffset(kLoadSignedHalfword, out, obj, offset);
2659 break;
2660 }
2661
2662 case Primitive::kPrimChar: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002663 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002664 __ LoadFromOffset(kLoadUnsignedHalfword, out, obj, offset);
2665 break;
2666 }
2667
2668 case Primitive::kPrimInt:
2669 case Primitive::kPrimNot: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002670 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002671 __ LoadFromOffset(kLoadWord, out, obj, offset);
2672 break;
2673 }
2674
2675 case Primitive::kPrimLong: {
2676 // TODO: support volatile.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002677 Location out = locations->Out();
2678 __ LoadFromOffset(kLoadWordPair, out.AsRegisterPairLow<Register>(), obj, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002679 break;
2680 }
2681
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002682 case Primitive::kPrimFloat: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002683 SRegister out = locations->Out().AsFpuRegister<SRegister>();
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002684 __ LoadSFromOffset(out, obj, offset);
2685 break;
2686 }
2687
2688 case Primitive::kPrimDouble: {
2689 DRegister out = FromLowSToD(locations->Out().AsFpuRegisterPairLow<SRegister>());
2690 __ LoadDFromOffset(out, obj, offset);
2691 break;
2692 }
2693
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002694 case Primitive::kPrimVoid:
2695 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07002696 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002697 }
2698}
2699
2700void LocationsBuilderARM::VisitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002701 LocationSummary* locations =
2702 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002703 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002704 if (instruction->HasUses()) {
2705 locations->SetOut(Location::SameAsFirstInput());
2706 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002707}
2708
2709void InstructionCodeGeneratorARM::VisitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01002710 SlowPathCodeARM* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathARM(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002711 codegen_->AddSlowPath(slow_path);
2712
2713 LocationSummary* locations = instruction->GetLocations();
2714 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002715
2716 if (obj.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002717 __ cmp(obj.AsRegister<Register>(), ShifterOperand(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002718 __ b(slow_path->GetEntryLabel(), EQ);
2719 } else {
2720 DCHECK(obj.IsConstant()) << obj;
2721 DCHECK_EQ(obj.GetConstant()->AsIntConstant()->GetValue(), 0);
2722 __ b(slow_path->GetEntryLabel());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002723 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002724}
2725
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002726void LocationsBuilderARM::VisitArrayGet(HArrayGet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002727 LocationSummary* locations =
2728 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002729 locations->SetInAt(0, Location::RequiresRegister());
2730 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
2731 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002732}
2733
2734void InstructionCodeGeneratorARM::VisitArrayGet(HArrayGet* instruction) {
2735 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002736 Register obj = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002737 Location index = locations->InAt(1);
2738
2739 switch (instruction->GetType()) {
2740 case Primitive::kPrimBoolean: {
2741 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002742 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002743 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00002744 size_t offset =
2745 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002746 __ LoadFromOffset(kLoadUnsignedByte, out, obj, offset);
2747 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002748 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002749 __ LoadFromOffset(kLoadUnsignedByte, out, IP, data_offset);
2750 }
2751 break;
2752 }
2753
2754 case Primitive::kPrimByte: {
2755 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002756 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002757 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00002758 size_t offset =
2759 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002760 __ LoadFromOffset(kLoadSignedByte, out, obj, offset);
2761 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002762 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002763 __ LoadFromOffset(kLoadSignedByte, out, IP, data_offset);
2764 }
2765 break;
2766 }
2767
2768 case Primitive::kPrimShort: {
2769 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002770 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002771 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00002772 size_t offset =
2773 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002774 __ LoadFromOffset(kLoadSignedHalfword, out, obj, offset);
2775 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002776 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_2));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002777 __ LoadFromOffset(kLoadSignedHalfword, out, IP, data_offset);
2778 }
2779 break;
2780 }
2781
2782 case Primitive::kPrimChar: {
2783 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002784 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002785 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00002786 size_t offset =
2787 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002788 __ LoadFromOffset(kLoadUnsignedHalfword, out, obj, offset);
2789 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002790 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_2));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002791 __ LoadFromOffset(kLoadUnsignedHalfword, out, IP, data_offset);
2792 }
2793 break;
2794 }
2795
2796 case Primitive::kPrimInt:
2797 case Primitive::kPrimNot: {
2798 DCHECK_EQ(sizeof(mirror::HeapReference<mirror::Object>), sizeof(int32_t));
2799 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002800 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002801 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00002802 size_t offset =
2803 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002804 __ LoadFromOffset(kLoadWord, out, obj, offset);
2805 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002806 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002807 __ LoadFromOffset(kLoadWord, out, IP, data_offset);
2808 }
2809 break;
2810 }
2811
2812 case Primitive::kPrimLong: {
2813 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002814 Location out = locations->Out();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002815 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00002816 size_t offset =
2817 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002818 __ LoadFromOffset(kLoadWordPair, out.AsRegisterPairLow<Register>(), obj, offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002819 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002820 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_8));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002821 __ LoadFromOffset(kLoadWordPair, out.AsRegisterPairLow<Register>(), IP, data_offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002822 }
2823 break;
2824 }
2825
2826 case Primitive::kPrimFloat:
2827 case Primitive::kPrimDouble:
2828 LOG(FATAL) << "Unimplemented register type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07002829 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002830 case Primitive::kPrimVoid:
2831 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07002832 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002833 }
2834}
2835
2836void LocationsBuilderARM::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002837 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002838
2839 bool needs_write_barrier =
2840 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
2841 bool needs_runtime_call = instruction->NeedsTypeCheck();
2842
Nicolas Geoffray39468442014-09-02 15:17:15 +01002843 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002844 instruction, needs_runtime_call ? LocationSummary::kCall : LocationSummary::kNoCall);
2845 if (needs_runtime_call) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002846 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002847 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2848 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
2849 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002850 } else {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002851 locations->SetInAt(0, Location::RequiresRegister());
2852 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
2853 locations->SetInAt(2, Location::RequiresRegister());
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002854
2855 if (needs_write_barrier) {
2856 // Temporary registers for the write barrier.
2857 locations->AddTemp(Location::RequiresRegister());
2858 locations->AddTemp(Location::RequiresRegister());
2859 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002860 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002861}
2862
2863void InstructionCodeGeneratorARM::VisitArraySet(HArraySet* instruction) {
2864 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002865 Register obj = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002866 Location index = locations->InAt(1);
Nicolas Geoffray39468442014-09-02 15:17:15 +01002867 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002868 bool needs_runtime_call = locations->WillCall();
2869 bool needs_write_barrier =
2870 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002871
2872 switch (value_type) {
2873 case Primitive::kPrimBoolean:
2874 case Primitive::kPrimByte: {
2875 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002876 Register value = locations->InAt(2).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002877 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00002878 size_t offset =
2879 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002880 __ StoreToOffset(kStoreByte, value, obj, offset);
2881 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002882 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002883 __ StoreToOffset(kStoreByte, value, IP, data_offset);
2884 }
2885 break;
2886 }
2887
2888 case Primitive::kPrimShort:
2889 case Primitive::kPrimChar: {
2890 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002891 Register value = locations->InAt(2).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002892 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00002893 size_t offset =
2894 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002895 __ StoreToOffset(kStoreHalfword, value, obj, offset);
2896 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002897 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_2));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002898 __ StoreToOffset(kStoreHalfword, value, IP, data_offset);
2899 }
2900 break;
2901 }
2902
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002903 case Primitive::kPrimInt:
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002904 case Primitive::kPrimNot: {
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002905 if (!needs_runtime_call) {
2906 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002907 Register value = locations->InAt(2).AsRegister<Register>();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002908 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00002909 size_t offset =
2910 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002911 __ StoreToOffset(kStoreWord, value, obj, offset);
2912 } else {
2913 DCHECK(index.IsRegister()) << index;
Roland Levillain271ab9c2014-11-27 15:23:57 +00002914 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4));
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002915 __ StoreToOffset(kStoreWord, value, IP, data_offset);
2916 }
2917 if (needs_write_barrier) {
2918 DCHECK_EQ(value_type, Primitive::kPrimNot);
Roland Levillain271ab9c2014-11-27 15:23:57 +00002919 Register temp = locations->GetTemp(0).AsRegister<Register>();
2920 Register card = locations->GetTemp(1).AsRegister<Register>();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002921 codegen_->MarkGCCard(temp, card, obj, value);
2922 }
2923 } else {
2924 DCHECK_EQ(value_type, Primitive::kPrimNot);
Roland Levillain199f3362014-11-27 17:15:16 +00002925 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pAputObject),
2926 instruction,
2927 instruction->GetDexPc());
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002928 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002929 break;
2930 }
2931
2932 case Primitive::kPrimLong: {
2933 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002934 Location value = locations->InAt(2);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002935 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00002936 size_t offset =
2937 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002938 __ StoreToOffset(kStoreWordPair, value.AsRegisterPairLow<Register>(), obj, offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002939 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002940 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_8));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002941 __ StoreToOffset(kStoreWordPair, value.AsRegisterPairLow<Register>(), IP, data_offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002942 }
2943 break;
2944 }
2945
2946 case Primitive::kPrimFloat:
2947 case Primitive::kPrimDouble:
2948 LOG(FATAL) << "Unimplemented register type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07002949 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002950 case Primitive::kPrimVoid:
2951 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07002952 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002953 }
2954}
2955
2956void LocationsBuilderARM::VisitArrayLength(HArrayLength* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002957 LocationSummary* locations =
2958 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002959 locations->SetInAt(0, Location::RequiresRegister());
2960 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002961}
2962
2963void InstructionCodeGeneratorARM::VisitArrayLength(HArrayLength* instruction) {
2964 LocationSummary* locations = instruction->GetLocations();
2965 uint32_t offset = mirror::Array::LengthOffset().Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002966 Register obj = locations->InAt(0).AsRegister<Register>();
2967 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002968 __ LoadFromOffset(kLoadWord, out, obj, offset);
2969}
2970
2971void LocationsBuilderARM::VisitBoundsCheck(HBoundsCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002972 LocationSummary* locations =
2973 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002974 locations->SetInAt(0, Location::RequiresRegister());
2975 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002976 if (instruction->HasUses()) {
2977 locations->SetOut(Location::SameAsFirstInput());
2978 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002979}
2980
2981void InstructionCodeGeneratorARM::VisitBoundsCheck(HBoundsCheck* instruction) {
2982 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01002983 SlowPathCodeARM* slow_path = new (GetGraph()->GetArena()) BoundsCheckSlowPathARM(
Nicolas Geoffray39468442014-09-02 15:17:15 +01002984 instruction, locations->InAt(0), locations->InAt(1));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002985 codegen_->AddSlowPath(slow_path);
2986
Roland Levillain271ab9c2014-11-27 15:23:57 +00002987 Register index = locations->InAt(0).AsRegister<Register>();
2988 Register length = locations->InAt(1).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002989
2990 __ cmp(index, ShifterOperand(length));
2991 __ b(slow_path->GetEntryLabel(), CS);
2992}
2993
2994void CodeGeneratorARM::MarkGCCard(Register temp, Register card, Register object, Register value) {
2995 Label is_null;
2996 __ CompareAndBranchIfZero(value, &is_null);
2997 __ LoadFromOffset(kLoadWord, card, TR, Thread::CardTableOffset<kArmWordSize>().Int32Value());
2998 __ Lsr(temp, object, gc::accounting::CardTable::kCardShift);
2999 __ strb(card, Address(card, temp));
3000 __ Bind(&is_null);
3001}
3002
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003003void LocationsBuilderARM::VisitTemporary(HTemporary* temp) {
3004 temp->SetLocations(nullptr);
3005}
3006
3007void InstructionCodeGeneratorARM::VisitTemporary(HTemporary* temp) {
3008 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003009 UNUSED(temp);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003010}
3011
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003012void LocationsBuilderARM::VisitParallelMove(HParallelMove* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003013 UNUSED(instruction);
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01003014 LOG(FATAL) << "Unreachable";
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003015}
3016
3017void InstructionCodeGeneratorARM::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01003018 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
3019}
3020
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003021void LocationsBuilderARM::VisitSuspendCheck(HSuspendCheck* instruction) {
3022 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
3023}
3024
3025void InstructionCodeGeneratorARM::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01003026 HBasicBlock* block = instruction->GetBlock();
3027 if (block->GetLoopInformation() != nullptr) {
3028 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
3029 // The back edge will generate the suspend check.
3030 return;
3031 }
3032 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
3033 // The goto will generate the suspend check.
3034 return;
3035 }
3036 GenerateSuspendCheck(instruction, nullptr);
3037}
3038
3039void InstructionCodeGeneratorARM::GenerateSuspendCheck(HSuspendCheck* instruction,
3040 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003041 SuspendCheckSlowPathARM* slow_path =
Nicolas Geoffray3c049742014-09-24 18:10:46 +01003042 new (GetGraph()->GetArena()) SuspendCheckSlowPathARM(instruction, successor);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003043 codegen_->AddSlowPath(slow_path);
3044
Nicolas Geoffray44b819e2014-11-06 12:00:54 +00003045 __ LoadFromOffset(
3046 kLoadUnsignedHalfword, IP, TR, Thread::ThreadFlagsOffset<kArmWordSize>().Int32Value());
3047 __ cmp(IP, ShifterOperand(0));
3048 // TODO: Figure out the branch offsets and use cbz/cbnz.
Nicolas Geoffray3c049742014-09-24 18:10:46 +01003049 if (successor == nullptr) {
Nicolas Geoffray44b819e2014-11-06 12:00:54 +00003050 __ b(slow_path->GetEntryLabel(), NE);
Nicolas Geoffray3c049742014-09-24 18:10:46 +01003051 __ Bind(slow_path->GetReturnLabel());
3052 } else {
Nicolas Geoffray44b819e2014-11-06 12:00:54 +00003053 __ b(codegen_->GetLabelOf(successor), EQ);
Nicolas Geoffray3c049742014-09-24 18:10:46 +01003054 __ b(slow_path->GetEntryLabel());
3055 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003056}
3057
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01003058ArmAssembler* ParallelMoveResolverARM::GetAssembler() const {
3059 return codegen_->GetAssembler();
3060}
3061
3062void ParallelMoveResolverARM::EmitMove(size_t index) {
3063 MoveOperands* move = moves_.Get(index);
3064 Location source = move->GetSource();
3065 Location destination = move->GetDestination();
3066
3067 if (source.IsRegister()) {
3068 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003069 __ Mov(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01003070 } else {
3071 DCHECK(destination.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003072 __ StoreToOffset(kStoreWord, source.AsRegister<Register>(),
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01003073 SP, destination.GetStackIndex());
3074 }
3075 } else if (source.IsStackSlot()) {
3076 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003077 __ LoadFromOffset(kLoadWord, destination.AsRegister<Register>(),
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01003078 SP, source.GetStackIndex());
3079 } else {
3080 DCHECK(destination.IsStackSlot());
3081 __ LoadFromOffset(kLoadWord, IP, SP, source.GetStackIndex());
3082 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
3083 }
3084 } else {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003085 DCHECK(source.IsConstant());
Roland Levillain476df552014-10-09 17:51:36 +01003086 DCHECK(source.GetConstant()->IsIntConstant());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003087 int32_t value = source.GetConstant()->AsIntConstant()->GetValue();
3088 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003089 __ LoadImmediate(destination.AsRegister<Register>(), value);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003090 } else {
3091 DCHECK(destination.IsStackSlot());
3092 __ LoadImmediate(IP, value);
Nicolas Geoffray360231a2014-10-08 21:07:48 +01003093 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003094 }
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01003095 }
3096}
3097
3098void ParallelMoveResolverARM::Exchange(Register reg, int mem) {
3099 __ Mov(IP, reg);
3100 __ LoadFromOffset(kLoadWord, reg, SP, mem);
3101 __ StoreToOffset(kStoreWord, IP, SP, mem);
3102}
3103
3104void ParallelMoveResolverARM::Exchange(int mem1, int mem2) {
3105 ScratchRegisterScope ensure_scratch(this, IP, R0, codegen_->GetNumberOfCoreRegisters());
3106 int stack_offset = ensure_scratch.IsSpilled() ? kArmWordSize : 0;
3107 __ LoadFromOffset(kLoadWord, static_cast<Register>(ensure_scratch.GetRegister()),
3108 SP, mem1 + stack_offset);
3109 __ LoadFromOffset(kLoadWord, IP, SP, mem2 + stack_offset);
3110 __ StoreToOffset(kStoreWord, static_cast<Register>(ensure_scratch.GetRegister()),
3111 SP, mem2 + stack_offset);
3112 __ StoreToOffset(kStoreWord, IP, SP, mem1 + stack_offset);
3113}
3114
3115void ParallelMoveResolverARM::EmitSwap(size_t index) {
3116 MoveOperands* move = moves_.Get(index);
3117 Location source = move->GetSource();
3118 Location destination = move->GetDestination();
3119
3120 if (source.IsRegister() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003121 DCHECK_NE(source.AsRegister<Register>(), IP);
3122 DCHECK_NE(destination.AsRegister<Register>(), IP);
3123 __ Mov(IP, source.AsRegister<Register>());
3124 __ Mov(source.AsRegister<Register>(), destination.AsRegister<Register>());
3125 __ Mov(destination.AsRegister<Register>(), IP);
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01003126 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003127 Exchange(source.AsRegister<Register>(), destination.GetStackIndex());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01003128 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003129 Exchange(destination.AsRegister<Register>(), source.GetStackIndex());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01003130 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
3131 Exchange(source.GetStackIndex(), destination.GetStackIndex());
3132 } else {
3133 LOG(FATAL) << "Unimplemented";
3134 }
3135}
3136
3137void ParallelMoveResolverARM::SpillScratch(int reg) {
3138 __ Push(static_cast<Register>(reg));
3139}
3140
3141void ParallelMoveResolverARM::RestoreScratch(int reg) {
3142 __ Pop(static_cast<Register>(reg));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003143}
3144
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003145void LocationsBuilderARM::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003146 LocationSummary::CallKind call_kind = cls->CanCallRuntime()
3147 ? LocationSummary::kCallOnSlowPath
3148 : LocationSummary::kNoCall;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003149 LocationSummary* locations =
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003150 new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003151 locations->SetOut(Location::RequiresRegister());
3152}
3153
3154void InstructionCodeGeneratorARM::VisitLoadClass(HLoadClass* cls) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003155 Register out = cls->GetLocations()->Out().AsRegister<Register>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003156 if (cls->IsReferrersClass()) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003157 DCHECK(!cls->CanCallRuntime());
3158 DCHECK(!cls->MustGenerateClinitCheck());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003159 codegen_->LoadCurrentMethod(out);
3160 __ LoadFromOffset(kLoadWord, out, out, mirror::ArtMethod::DeclaringClassOffset().Int32Value());
3161 } else {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003162 DCHECK(cls->CanCallRuntime());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003163 codegen_->LoadCurrentMethod(out);
3164 __ LoadFromOffset(
3165 kLoadWord, out, out, mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value());
3166 __ LoadFromOffset(kLoadWord, out, out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex()));
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003167
3168 SlowPathCodeARM* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM(
3169 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
3170 codegen_->AddSlowPath(slow_path);
3171 __ cmp(out, ShifterOperand(0));
3172 __ b(slow_path->GetEntryLabel(), EQ);
3173 if (cls->MustGenerateClinitCheck()) {
3174 GenerateClassInitializationCheck(slow_path, out);
3175 } else {
3176 __ Bind(slow_path->GetExitLabel());
3177 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003178 }
3179}
3180
3181void LocationsBuilderARM::VisitClinitCheck(HClinitCheck* check) {
3182 LocationSummary* locations =
3183 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
3184 locations->SetInAt(0, Location::RequiresRegister());
3185 if (check->HasUses()) {
3186 locations->SetOut(Location::SameAsFirstInput());
3187 }
3188}
3189
3190void InstructionCodeGeneratorARM::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003191 // We assume the class is not null.
3192 SlowPathCodeARM* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM(
3193 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003194 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00003195 GenerateClassInitializationCheck(slow_path,
3196 check->GetLocations()->InAt(0).AsRegister<Register>());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003197}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003198
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003199void InstructionCodeGeneratorARM::GenerateClassInitializationCheck(
3200 SlowPathCodeARM* slow_path, Register class_reg) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003201 __ LoadFromOffset(kLoadWord, IP, class_reg, mirror::Class::StatusOffset().Int32Value());
3202 __ cmp(IP, ShifterOperand(mirror::Class::kStatusInitialized));
3203 __ b(slow_path->GetEntryLabel(), LT);
3204 // Even if the initialized flag is set, we may be in a situation where caches are not synced
3205 // properly. Therefore, we do a memory fence.
3206 __ dmb(ISH);
3207 __ Bind(slow_path->GetExitLabel());
3208}
3209
3210void LocationsBuilderARM::VisitStaticFieldGet(HStaticFieldGet* instruction) {
3211 LocationSummary* locations =
3212 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3213 locations->SetInAt(0, Location::RequiresRegister());
3214 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3215}
3216
3217void InstructionCodeGeneratorARM::VisitStaticFieldGet(HStaticFieldGet* instruction) {
3218 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003219 Register cls = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003220 uint32_t offset = instruction->GetFieldOffset().Uint32Value();
3221
3222 switch (instruction->GetType()) {
3223 case Primitive::kPrimBoolean: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003224 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003225 __ LoadFromOffset(kLoadUnsignedByte, out, cls, offset);
3226 break;
3227 }
3228
3229 case Primitive::kPrimByte: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003230 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003231 __ LoadFromOffset(kLoadSignedByte, out, cls, offset);
3232 break;
3233 }
3234
3235 case Primitive::kPrimShort: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003236 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003237 __ LoadFromOffset(kLoadSignedHalfword, out, cls, offset);
3238 break;
3239 }
3240
3241 case Primitive::kPrimChar: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003242 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003243 __ LoadFromOffset(kLoadUnsignedHalfword, out, cls, offset);
3244 break;
3245 }
3246
3247 case Primitive::kPrimInt:
3248 case Primitive::kPrimNot: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003249 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003250 __ LoadFromOffset(kLoadWord, out, cls, offset);
3251 break;
3252 }
3253
3254 case Primitive::kPrimLong: {
3255 // TODO: support volatile.
3256 Location out = locations->Out();
3257 __ LoadFromOffset(kLoadWordPair, out.AsRegisterPairLow<Register>(), cls, offset);
3258 break;
3259 }
3260
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003261 case Primitive::kPrimFloat: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003262 SRegister out = locations->Out().AsFpuRegister<SRegister>();
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003263 __ LoadSFromOffset(out, cls, offset);
3264 break;
3265 }
3266
3267 case Primitive::kPrimDouble: {
3268 DRegister out = FromLowSToD(locations->Out().AsFpuRegisterPairLow<SRegister>());
3269 __ LoadDFromOffset(out, cls, offset);
3270 break;
3271 }
3272
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003273 case Primitive::kPrimVoid:
3274 LOG(FATAL) << "Unreachable type " << instruction->GetType();
3275 UNREACHABLE();
3276 }
3277}
3278
3279void LocationsBuilderARM::VisitStaticFieldSet(HStaticFieldSet* instruction) {
3280 LocationSummary* locations =
3281 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003282 bool needs_write_barrier =
3283 CodeGenerator::StoreNeedsWriteBarrier(instruction->GetFieldType(), instruction->GetValue());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003284 locations->SetInAt(0, Location::RequiresRegister());
3285 locations->SetInAt(1, Location::RequiresRegister());
3286 // Temporary registers for the write barrier.
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003287 if (needs_write_barrier) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003288 locations->AddTemp(Location::RequiresRegister());
3289 locations->AddTemp(Location::RequiresRegister());
3290 }
3291}
3292
3293void InstructionCodeGeneratorARM::VisitStaticFieldSet(HStaticFieldSet* instruction) {
3294 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003295 Register cls = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003296 uint32_t offset = instruction->GetFieldOffset().Uint32Value();
3297 Primitive::Type field_type = instruction->GetFieldType();
3298
3299 switch (field_type) {
3300 case Primitive::kPrimBoolean:
3301 case Primitive::kPrimByte: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003302 Register value = locations->InAt(1).AsRegister<Register>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003303 __ StoreToOffset(kStoreByte, value, cls, offset);
3304 break;
3305 }
3306
3307 case Primitive::kPrimShort:
3308 case Primitive::kPrimChar: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003309 Register value = locations->InAt(1).AsRegister<Register>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003310 __ StoreToOffset(kStoreHalfword, value, cls, offset);
3311 break;
3312 }
3313
3314 case Primitive::kPrimInt:
3315 case Primitive::kPrimNot: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003316 Register value = locations->InAt(1).AsRegister<Register>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003317 __ StoreToOffset(kStoreWord, value, cls, offset);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003318 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->GetValue())) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003319 Register temp = locations->GetTemp(0).AsRegister<Register>();
3320 Register card = locations->GetTemp(1).AsRegister<Register>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003321 codegen_->MarkGCCard(temp, card, cls, value);
3322 }
3323 break;
3324 }
3325
3326 case Primitive::kPrimLong: {
3327 Location value = locations->InAt(1);
3328 __ StoreToOffset(kStoreWordPair, value.AsRegisterPairLow<Register>(), cls, offset);
3329 break;
3330 }
3331
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003332 case Primitive::kPrimFloat: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003333 SRegister value = locations->InAt(1).AsFpuRegister<SRegister>();
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003334 __ StoreSToOffset(value, cls, offset);
3335 break;
3336 }
3337
3338 case Primitive::kPrimDouble: {
3339 DRegister value = FromLowSToD(locations->InAt(1).AsFpuRegisterPairLow<SRegister>());
3340 __ StoreDToOffset(value, cls, offset);
3341 break;
3342 }
3343
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003344 case Primitive::kPrimVoid:
3345 LOG(FATAL) << "Unreachable type " << field_type;
3346 UNREACHABLE();
3347 }
3348}
3349
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003350void LocationsBuilderARM::VisitLoadString(HLoadString* load) {
3351 LocationSummary* locations =
3352 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kCallOnSlowPath);
3353 locations->SetOut(Location::RequiresRegister());
3354}
3355
3356void InstructionCodeGeneratorARM::VisitLoadString(HLoadString* load) {
3357 SlowPathCodeARM* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathARM(load);
3358 codegen_->AddSlowPath(slow_path);
3359
Roland Levillain271ab9c2014-11-27 15:23:57 +00003360 Register out = load->GetLocations()->Out().AsRegister<Register>();
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003361 codegen_->LoadCurrentMethod(out);
Mathieu Chartiereace4582014-11-24 18:29:54 -08003362 __ LoadFromOffset(kLoadWord, out, out, mirror::ArtMethod::DeclaringClassOffset().Int32Value());
3363 __ LoadFromOffset(kLoadWord, out, out, mirror::Class::DexCacheStringsOffset().Int32Value());
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003364 __ LoadFromOffset(kLoadWord, out, out, CodeGenerator::GetCacheOffset(load->GetStringIndex()));
3365 __ cmp(out, ShifterOperand(0));
3366 __ b(slow_path->GetEntryLabel(), EQ);
3367 __ Bind(slow_path->GetExitLabel());
3368}
3369
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00003370void LocationsBuilderARM::VisitLoadException(HLoadException* load) {
3371 LocationSummary* locations =
3372 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
3373 locations->SetOut(Location::RequiresRegister());
3374}
3375
3376void InstructionCodeGeneratorARM::VisitLoadException(HLoadException* load) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003377 Register out = load->GetLocations()->Out().AsRegister<Register>();
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00003378 int32_t offset = Thread::ExceptionOffset<kArmWordSize>().Int32Value();
3379 __ LoadFromOffset(kLoadWord, out, TR, offset);
3380 __ LoadImmediate(IP, 0);
3381 __ StoreToOffset(kStoreWord, IP, TR, offset);
3382}
3383
3384void LocationsBuilderARM::VisitThrow(HThrow* instruction) {
3385 LocationSummary* locations =
3386 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3387 InvokeRuntimeCallingConvention calling_convention;
3388 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3389}
3390
3391void InstructionCodeGeneratorARM::VisitThrow(HThrow* instruction) {
3392 codegen_->InvokeRuntime(
3393 QUICK_ENTRY_POINT(pDeliverException), instruction, instruction->GetDexPc());
3394}
3395
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003396void LocationsBuilderARM::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003397 LocationSummary::CallKind call_kind = instruction->IsClassFinal()
3398 ? LocationSummary::kNoCall
3399 : LocationSummary::kCallOnSlowPath;
3400 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
3401 locations->SetInAt(0, Location::RequiresRegister());
3402 locations->SetInAt(1, Location::RequiresRegister());
3403 locations->SetOut(Location::RequiresRegister());
3404}
3405
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003406void InstructionCodeGeneratorARM::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003407 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003408 Register obj = locations->InAt(0).AsRegister<Register>();
3409 Register cls = locations->InAt(1).AsRegister<Register>();
3410 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003411 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3412 Label done, zero;
3413 SlowPathCodeARM* slow_path = nullptr;
3414
3415 // Return 0 if `obj` is null.
3416 // TODO: avoid this check if we know obj is not null.
3417 __ cmp(obj, ShifterOperand(0));
3418 __ b(&zero, EQ);
3419 // Compare the class of `obj` with `cls`.
3420 __ LoadFromOffset(kLoadWord, out, obj, class_offset);
3421 __ cmp(out, ShifterOperand(cls));
3422 if (instruction->IsClassFinal()) {
3423 // Classes must be equal for the instanceof to succeed.
3424 __ b(&zero, NE);
3425 __ LoadImmediate(out, 1);
3426 __ b(&done);
3427 } else {
3428 // If the classes are not equal, we go into a slow path.
3429 DCHECK(locations->OnlyCallsOnSlowPath());
3430 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM(
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003431 instruction, locations->InAt(1), locations->Out(), instruction->GetDexPc());
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003432 codegen_->AddSlowPath(slow_path);
3433 __ b(slow_path->GetEntryLabel(), NE);
3434 __ LoadImmediate(out, 1);
3435 __ b(&done);
3436 }
3437 __ Bind(&zero);
3438 __ LoadImmediate(out, 0);
3439 if (slow_path != nullptr) {
3440 __ Bind(slow_path->GetExitLabel());
3441 }
3442 __ Bind(&done);
3443}
3444
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003445void LocationsBuilderARM::VisitCheckCast(HCheckCast* instruction) {
3446 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
3447 instruction, LocationSummary::kCallOnSlowPath);
3448 locations->SetInAt(0, Location::RequiresRegister());
3449 locations->SetInAt(1, Location::RequiresRegister());
3450 locations->AddTemp(Location::RequiresRegister());
3451}
3452
3453void InstructionCodeGeneratorARM::VisitCheckCast(HCheckCast* instruction) {
3454 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003455 Register obj = locations->InAt(0).AsRegister<Register>();
3456 Register cls = locations->InAt(1).AsRegister<Register>();
3457 Register temp = locations->GetTemp(0).AsRegister<Register>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003458 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3459
3460 SlowPathCodeARM* slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM(
3461 instruction, locations->InAt(1), locations->GetTemp(0), instruction->GetDexPc());
3462 codegen_->AddSlowPath(slow_path);
3463
3464 // TODO: avoid this check if we know obj is not null.
3465 __ cmp(obj, ShifterOperand(0));
3466 __ b(slow_path->GetExitLabel(), EQ);
3467 // Compare the class of `obj` with `cls`.
3468 __ LoadFromOffset(kLoadWord, temp, obj, class_offset);
3469 __ cmp(temp, ShifterOperand(cls));
3470 __ b(slow_path->GetEntryLabel(), NE);
3471 __ Bind(slow_path->GetExitLabel());
3472}
3473
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00003474void LocationsBuilderARM::VisitMonitorOperation(HMonitorOperation* instruction) {
3475 LocationSummary* locations =
3476 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3477 InvokeRuntimeCallingConvention calling_convention;
3478 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3479}
3480
3481void InstructionCodeGeneratorARM::VisitMonitorOperation(HMonitorOperation* instruction) {
3482 codegen_->InvokeRuntime(instruction->IsEnter()
3483 ? QUICK_ENTRY_POINT(pLockObject) : QUICK_ENTRY_POINT(pUnlockObject),
3484 instruction,
3485 instruction->GetDexPc());
3486}
3487
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003488void LocationsBuilderARM::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction); }
3489void LocationsBuilderARM::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction); }
3490void LocationsBuilderARM::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction); }
3491
3492void LocationsBuilderARM::HandleBitwiseOperation(HBinaryOperation* instruction) {
3493 LocationSummary* locations =
3494 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3495 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
3496 || instruction->GetResultType() == Primitive::kPrimLong);
3497 locations->SetInAt(0, Location::RequiresRegister());
3498 locations->SetInAt(1, Location::RequiresRegister());
3499 bool output_overlaps = (instruction->GetResultType() == Primitive::kPrimLong);
3500 locations->SetOut(Location::RequiresRegister(), output_overlaps);
3501}
3502
3503void InstructionCodeGeneratorARM::VisitAnd(HAnd* instruction) {
3504 HandleBitwiseOperation(instruction);
3505}
3506
3507void InstructionCodeGeneratorARM::VisitOr(HOr* instruction) {
3508 HandleBitwiseOperation(instruction);
3509}
3510
3511void InstructionCodeGeneratorARM::VisitXor(HXor* instruction) {
3512 HandleBitwiseOperation(instruction);
3513}
3514
3515void InstructionCodeGeneratorARM::HandleBitwiseOperation(HBinaryOperation* instruction) {
3516 LocationSummary* locations = instruction->GetLocations();
3517
3518 if (instruction->GetResultType() == Primitive::kPrimInt) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003519 Register first = locations->InAt(0).AsRegister<Register>();
3520 Register second = locations->InAt(1).AsRegister<Register>();
3521 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003522 if (instruction->IsAnd()) {
3523 __ and_(out, first, ShifterOperand(second));
3524 } else if (instruction->IsOr()) {
3525 __ orr(out, first, ShifterOperand(second));
3526 } else {
3527 DCHECK(instruction->IsXor());
3528 __ eor(out, first, ShifterOperand(second));
3529 }
3530 } else {
3531 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
3532 Location first = locations->InAt(0);
3533 Location second = locations->InAt(1);
3534 Location out = locations->Out();
3535 if (instruction->IsAnd()) {
3536 __ and_(out.AsRegisterPairLow<Register>(),
3537 first.AsRegisterPairLow<Register>(),
3538 ShifterOperand(second.AsRegisterPairLow<Register>()));
3539 __ and_(out.AsRegisterPairHigh<Register>(),
3540 first.AsRegisterPairHigh<Register>(),
3541 ShifterOperand(second.AsRegisterPairHigh<Register>()));
3542 } else if (instruction->IsOr()) {
3543 __ orr(out.AsRegisterPairLow<Register>(),
3544 first.AsRegisterPairLow<Register>(),
3545 ShifterOperand(second.AsRegisterPairLow<Register>()));
3546 __ orr(out.AsRegisterPairHigh<Register>(),
3547 first.AsRegisterPairHigh<Register>(),
3548 ShifterOperand(second.AsRegisterPairHigh<Register>()));
3549 } else {
3550 DCHECK(instruction->IsXor());
3551 __ eor(out.AsRegisterPairLow<Register>(),
3552 first.AsRegisterPairLow<Register>(),
3553 ShifterOperand(second.AsRegisterPairLow<Register>()));
3554 __ eor(out.AsRegisterPairHigh<Register>(),
3555 first.AsRegisterPairHigh<Register>(),
3556 ShifterOperand(second.AsRegisterPairHigh<Register>()));
3557 }
3558 }
3559}
3560
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00003561} // namespace arm
3562} // namespace art