blob: 2f49107bc98f7a896a5ee5c9e95a72eb140416b7 [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
Calin Juravle34166012014-12-19 17:22:29 +000019#include "arch/arm/instruction_set_features_arm.h"
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070020#include "entrypoints/quick/quick_entrypoints.h"
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +010021#include "gc/accounting/card_table.h"
Ian Rogers7e70b002014-10-08 11:47:24 -070022#include "mirror/array-inl.h"
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +000023#include "mirror/art_method.h"
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +010024#include "mirror/class.h"
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070025#include "thread.h"
Nicolas Geoffray9cf35522014-06-09 18:40:10 +010026#include "utils/arm/assembler_arm.h"
27#include "utils/arm/managed_register_arm.h"
Roland Levillain946e1432014-11-11 17:35:19 +000028#include "utils/assembler.h"
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010029#include "utils/stack_checks.h"
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +000030
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000031namespace art {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +010032
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000033namespace arm {
34
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +000035static DRegister FromLowSToD(SRegister reg) {
36 DCHECK_EQ(reg % 2, 0);
37 return static_cast<DRegister>(reg / 2);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +010038}
39
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010040static constexpr bool kExplicitStackOverflowCheck = false;
41
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +000042static constexpr int kNumberOfPushedRegistersAtEntry = 1 + 2; // LR, R6, R7
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010043static constexpr int kCurrentMethodStackOffset = 0;
44
Calin Juravled6fb6cf2014-11-11 19:07:44 +000045static constexpr Register kRuntimeParameterCoreRegisters[] = { R0, R1, R2, R3 };
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +010046static constexpr size_t kRuntimeParameterCoreRegistersLength =
47 arraysize(kRuntimeParameterCoreRegisters);
Calin Juravled2ec87d2014-12-08 14:24:46 +000048static constexpr SRegister kRuntimeParameterFpuRegisters[] = { S0, S1, S2, S3 };
Roland Levillain624279f2014-12-04 11:54:28 +000049static constexpr size_t kRuntimeParameterFpuRegistersLength =
50 arraysize(kRuntimeParameterFpuRegisters);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +010051
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +000052class InvokeRuntimeCallingConvention : public CallingConvention<Register, SRegister> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +010053 public:
54 InvokeRuntimeCallingConvention()
55 : CallingConvention(kRuntimeParameterCoreRegisters,
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +010056 kRuntimeParameterCoreRegistersLength,
57 kRuntimeParameterFpuRegisters,
58 kRuntimeParameterFpuRegistersLength) {}
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +010059
60 private:
61 DISALLOW_COPY_AND_ASSIGN(InvokeRuntimeCallingConvention);
62};
63
Nicolas Geoffraye5038322014-07-04 09:41:32 +010064#define __ reinterpret_cast<ArmAssembler*>(codegen->GetAssembler())->
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +010065#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kArmWordSize, x).Int32Value()
Nicolas Geoffraye5038322014-07-04 09:41:32 +010066
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +010067class SlowPathCodeARM : public SlowPathCode {
68 public:
69 SlowPathCodeARM() : entry_label_(), exit_label_() {}
70
71 Label* GetEntryLabel() { return &entry_label_; }
72 Label* GetExitLabel() { return &exit_label_; }
73
74 private:
75 Label entry_label_;
76 Label exit_label_;
77
78 DISALLOW_COPY_AND_ASSIGN(SlowPathCodeARM);
79};
80
81class NullCheckSlowPathARM : public SlowPathCodeARM {
Nicolas Geoffraye5038322014-07-04 09:41:32 +010082 public:
Nicolas Geoffray39468442014-09-02 15:17:15 +010083 explicit NullCheckSlowPathARM(HNullCheck* instruction) : instruction_(instruction) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +010084
Alexandre Rames67555f72014-11-18 10:55:16 +000085 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +010086 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
Nicolas Geoffraye5038322014-07-04 09:41:32 +010087 __ Bind(GetEntryLabel());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +010088 arm_codegen->InvokeRuntime(
89 QUICK_ENTRY_POINT(pThrowNullPointer), instruction_, instruction_->GetDexPc());
Nicolas Geoffraye5038322014-07-04 09:41:32 +010090 }
91
92 private:
Nicolas Geoffray39468442014-09-02 15:17:15 +010093 HNullCheck* const instruction_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +010094 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathARM);
95};
96
Calin Juravled0d48522014-11-04 16:40:20 +000097class DivZeroCheckSlowPathARM : public SlowPathCodeARM {
98 public:
99 explicit DivZeroCheckSlowPathARM(HDivZeroCheck* instruction) : instruction_(instruction) {}
100
Alexandre Rames67555f72014-11-18 10:55:16 +0000101 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Calin Juravled0d48522014-11-04 16:40:20 +0000102 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
103 __ Bind(GetEntryLabel());
104 arm_codegen->InvokeRuntime(
105 QUICK_ENTRY_POINT(pThrowDivZero), instruction_, instruction_->GetDexPc());
106 }
107
108 private:
109 HDivZeroCheck* const instruction_;
110 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathARM);
111};
112
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100113class StackOverflowCheckSlowPathARM : public SlowPathCodeARM {
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100114 public:
115 StackOverflowCheckSlowPathARM() {}
116
Alexandre Rames67555f72014-11-18 10:55:16 +0000117 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100118 __ Bind(GetEntryLabel());
119 __ LoadFromOffset(kLoadWord, PC, TR,
120 QUICK_ENTRYPOINT_OFFSET(kArmWordSize, pThrowStackOverflow).Int32Value());
121 }
122
123 private:
124 DISALLOW_COPY_AND_ASSIGN(StackOverflowCheckSlowPathARM);
125};
126
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100127class SuspendCheckSlowPathARM : public SlowPathCodeARM {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000128 public:
Alexandre Rames67555f72014-11-18 10:55:16 +0000129 SuspendCheckSlowPathARM(HSuspendCheck* instruction, HBasicBlock* successor)
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100130 : instruction_(instruction), successor_(successor) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000131
Alexandre Rames67555f72014-11-18 10:55:16 +0000132 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100133 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000134 __ Bind(GetEntryLabel());
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100135 codegen->SaveLiveRegisters(instruction_->GetLocations());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100136 arm_codegen->InvokeRuntime(
137 QUICK_ENTRY_POINT(pTestSuspend), instruction_, instruction_->GetDexPc());
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100138 codegen->RestoreLiveRegisters(instruction_->GetLocations());
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100139 if (successor_ == nullptr) {
140 __ b(GetReturnLabel());
141 } else {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100142 __ b(arm_codegen->GetLabelOf(successor_));
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100143 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000144 }
145
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100146 Label* GetReturnLabel() {
147 DCHECK(successor_ == nullptr);
148 return &return_label_;
149 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000150
151 private:
152 HSuspendCheck* const instruction_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100153 // If not null, the block to branch to after the suspend check.
154 HBasicBlock* const successor_;
155
156 // If `successor_` is null, the label to branch to after the suspend check.
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000157 Label return_label_;
158
159 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathARM);
160};
161
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100162class BoundsCheckSlowPathARM : public SlowPathCodeARM {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100163 public:
Roland Levillain5799fc02014-09-25 12:15:20 +0100164 BoundsCheckSlowPathARM(HBoundsCheck* instruction,
165 Location index_location,
166 Location length_location)
Nicolas Geoffray39468442014-09-02 15:17:15 +0100167 : instruction_(instruction),
168 index_location_(index_location),
169 length_location_(length_location) {}
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100170
Alexandre Rames67555f72014-11-18 10:55:16 +0000171 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100172 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100173 __ Bind(GetEntryLabel());
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000174 // We're moving two locations to locations that could overlap, so we need a parallel
175 // move resolver.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100176 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000177 codegen->EmitParallelMoves(
178 index_location_,
179 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
180 length_location_,
181 Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100182 arm_codegen->InvokeRuntime(
183 QUICK_ENTRY_POINT(pThrowArrayBounds), instruction_, instruction_->GetDexPc());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100184 }
185
186 private:
Nicolas Geoffray39468442014-09-02 15:17:15 +0100187 HBoundsCheck* const instruction_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100188 const Location index_location_;
189 const Location length_location_;
190
191 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathARM);
192};
193
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000194class LoadClassSlowPathARM : public SlowPathCodeARM {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100195 public:
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000196 LoadClassSlowPathARM(HLoadClass* cls,
197 HInstruction* at,
198 uint32_t dex_pc,
199 bool do_clinit)
200 : cls_(cls), at_(at), dex_pc_(dex_pc), do_clinit_(do_clinit) {
201 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
202 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100203
Alexandre Rames67555f72014-11-18 10:55:16 +0000204 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000205 LocationSummary* locations = at_->GetLocations();
206
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100207 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
208 __ Bind(GetEntryLabel());
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000209 codegen->SaveLiveRegisters(locations);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100210
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100211 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000212 __ LoadImmediate(calling_convention.GetRegisterAt(0), cls_->GetTypeIndex());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100213 arm_codegen->LoadCurrentMethod(calling_convention.GetRegisterAt(1));
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000214 int32_t entry_point_offset = do_clinit_
215 ? QUICK_ENTRY_POINT(pInitializeStaticStorage)
216 : QUICK_ENTRY_POINT(pInitializeType);
217 arm_codegen->InvokeRuntime(entry_point_offset, at_, dex_pc_);
218
219 // Move the class to the desired location.
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000220 Location out = locations->Out();
221 if (out.IsValid()) {
222 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000223 arm_codegen->Move32(locations->Out(), Location::RegisterLocation(R0));
224 }
225 codegen->RestoreLiveRegisters(locations);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100226 __ b(GetExitLabel());
227 }
228
229 private:
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000230 // The class this slow path will load.
231 HLoadClass* const cls_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100232
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000233 // The instruction where this slow path is happening.
234 // (Might be the load class or an initialization check).
235 HInstruction* const at_;
236
237 // The dex PC of `at_`.
238 const uint32_t dex_pc_;
239
240 // Whether to initialize the class.
241 const bool do_clinit_;
242
243 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathARM);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100244};
245
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000246class LoadStringSlowPathARM : public SlowPathCodeARM {
247 public:
248 explicit LoadStringSlowPathARM(HLoadString* instruction) : instruction_(instruction) {}
249
Alexandre Rames67555f72014-11-18 10:55:16 +0000250 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000251 LocationSummary* locations = instruction_->GetLocations();
252 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
253
254 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
255 __ Bind(GetEntryLabel());
256 codegen->SaveLiveRegisters(locations);
257
258 InvokeRuntimeCallingConvention calling_convention;
259 arm_codegen->LoadCurrentMethod(calling_convention.GetRegisterAt(0));
260 __ LoadImmediate(calling_convention.GetRegisterAt(1), instruction_->GetStringIndex());
261 arm_codegen->InvokeRuntime(
262 QUICK_ENTRY_POINT(pResolveString), instruction_, instruction_->GetDexPc());
263 arm_codegen->Move32(locations->Out(), Location::RegisterLocation(R0));
264
265 codegen->RestoreLiveRegisters(locations);
266 __ b(GetExitLabel());
267 }
268
269 private:
270 HLoadString* const instruction_;
271
272 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathARM);
273};
274
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000275class TypeCheckSlowPathARM : public SlowPathCodeARM {
276 public:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000277 TypeCheckSlowPathARM(HInstruction* instruction,
278 Location class_to_check,
279 Location object_class,
280 uint32_t dex_pc)
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000281 : instruction_(instruction),
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000282 class_to_check_(class_to_check),
283 object_class_(object_class),
284 dex_pc_(dex_pc) {}
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000285
Alexandre Rames67555f72014-11-18 10:55:16 +0000286 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000287 LocationSummary* locations = instruction_->GetLocations();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000288 DCHECK(instruction_->IsCheckCast()
289 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000290
291 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
292 __ Bind(GetEntryLabel());
293 codegen->SaveLiveRegisters(locations);
294
295 // We're moving two locations to locations that could overlap, so we need a parallel
296 // move resolver.
297 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000298 codegen->EmitParallelMoves(
299 class_to_check_,
300 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
301 object_class_,
302 Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000303
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000304 if (instruction_->IsInstanceOf()) {
305 arm_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pInstanceofNonTrivial), instruction_, dex_pc_);
306 arm_codegen->Move32(locations->Out(), Location::RegisterLocation(R0));
307 } else {
308 DCHECK(instruction_->IsCheckCast());
309 arm_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pCheckCast), instruction_, dex_pc_);
310 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000311
312 codegen->RestoreLiveRegisters(locations);
313 __ b(GetExitLabel());
314 }
315
316 private:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000317 HInstruction* const instruction_;
318 const Location class_to_check_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000319 const Location object_class_;
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000320 uint32_t dex_pc_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000321
322 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathARM);
323};
324
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000325#undef __
326
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100327#undef __
328#define __ reinterpret_cast<ArmAssembler*>(GetAssembler())->
Dave Allison20dfc792014-06-16 20:44:29 -0700329
330inline Condition ARMCondition(IfCondition cond) {
331 switch (cond) {
332 case kCondEQ: return EQ;
333 case kCondNE: return NE;
334 case kCondLT: return LT;
335 case kCondLE: return LE;
336 case kCondGT: return GT;
337 case kCondGE: return GE;
338 default:
339 LOG(FATAL) << "Unknown if condition";
340 }
341 return EQ; // Unreachable.
342}
343
344inline Condition ARMOppositeCondition(IfCondition cond) {
345 switch (cond) {
346 case kCondEQ: return NE;
347 case kCondNE: return EQ;
348 case kCondLT: return GE;
349 case kCondLE: return GT;
350 case kCondGT: return LE;
351 case kCondGE: return LT;
352 default:
353 LOG(FATAL) << "Unknown if condition";
354 }
355 return EQ; // Unreachable.
356}
357
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100358void CodeGeneratorARM::DumpCoreRegister(std::ostream& stream, int reg) const {
359 stream << ArmManagedRegister::FromCoreRegister(Register(reg));
360}
361
362void CodeGeneratorARM::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000363 stream << ArmManagedRegister::FromSRegister(SRegister(reg));
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100364}
365
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100366size_t CodeGeneratorARM::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
367 __ StoreToOffset(kStoreWord, static_cast<Register>(reg_id), SP, stack_index);
368 return kArmWordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100369}
370
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100371size_t CodeGeneratorARM::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
372 __ LoadFromOffset(kLoadWord, static_cast<Register>(reg_id), SP, stack_index);
373 return kArmWordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100374}
375
Calin Juravle34166012014-12-19 17:22:29 +0000376CodeGeneratorARM::CodeGeneratorARM(HGraph* graph,
377 const ArmInstructionSetFeatures* isa_features)
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000378 : CodeGenerator(graph, kNumberOfCoreRegisters, kNumberOfSRegisters, kNumberOfRegisterPairs),
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100379 block_labels_(graph->GetArena(), 0),
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100380 location_builder_(graph, this),
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +0100381 instruction_visitor_(graph, this),
Nicolas Geoffray8d486732014-07-16 16:23:40 +0100382 move_resolver_(graph->GetArena(), this),
Calin Juravle34166012014-12-19 17:22:29 +0000383 assembler_(true),
384 isa_features_(isa_features) {}
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100385
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100386size_t CodeGeneratorARM::FrameEntrySpillSize() const {
387 return kNumberOfPushedRegistersAtEntry * kArmWordSize;
388}
389
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100390Location CodeGeneratorARM::AllocateFreeRegister(Primitive::Type type) const {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100391 switch (type) {
392 case Primitive::kPrimLong: {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100393 size_t reg = FindFreeEntry(blocked_register_pairs_, kNumberOfRegisterPairs);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100394 ArmManagedRegister pair =
395 ArmManagedRegister::FromRegisterPair(static_cast<RegisterPair>(reg));
Calin Juravle34bacdf2014-10-07 20:23:36 +0100396 DCHECK(!blocked_core_registers_[pair.AsRegisterPairLow()]);
397 DCHECK(!blocked_core_registers_[pair.AsRegisterPairHigh()]);
398
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100399 blocked_core_registers_[pair.AsRegisterPairLow()] = true;
400 blocked_core_registers_[pair.AsRegisterPairHigh()] = true;
Calin Juravle34bacdf2014-10-07 20:23:36 +0100401 UpdateBlockedPairRegisters();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100402 return Location::RegisterPairLocation(pair.AsRegisterPairLow(), pair.AsRegisterPairHigh());
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100403 }
404
405 case Primitive::kPrimByte:
406 case Primitive::kPrimBoolean:
407 case Primitive::kPrimChar:
408 case Primitive::kPrimShort:
409 case Primitive::kPrimInt:
410 case Primitive::kPrimNot: {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100411 int reg = FindFreeEntry(blocked_core_registers_, kNumberOfCoreRegisters);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100412 // Block all register pairs that contain `reg`.
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100413 for (int i = 0; i < kNumberOfRegisterPairs; i++) {
414 ArmManagedRegister current =
415 ArmManagedRegister::FromRegisterPair(static_cast<RegisterPair>(i));
416 if (current.AsRegisterPairLow() == reg || current.AsRegisterPairHigh() == reg) {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100417 blocked_register_pairs_[i] = true;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100418 }
419 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100420 return Location::RegisterLocation(reg);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100421 }
422
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000423 case Primitive::kPrimFloat: {
424 int reg = FindFreeEntry(blocked_fpu_registers_, kNumberOfSRegisters);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100425 return Location::FpuRegisterLocation(reg);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100426 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100427
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000428 case Primitive::kPrimDouble: {
Nicolas Geoffray3c035032014-10-28 10:46:40 +0000429 int reg = FindTwoFreeConsecutiveAlignedEntries(blocked_fpu_registers_, kNumberOfSRegisters);
430 DCHECK_EQ(reg % 2, 0);
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000431 return Location::FpuRegisterPairLocation(reg, reg + 1);
432 }
433
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100434 case Primitive::kPrimVoid:
435 LOG(FATAL) << "Unreachable type " << type;
436 }
437
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100438 return Location();
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100439}
440
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100441void CodeGeneratorARM::SetupBlockedRegisters() const {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100442 // Don't allocate the dalvik style register pair passing.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100443 blocked_register_pairs_[R1_R2] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100444
445 // Stack register, LR and PC are always reserved.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100446 blocked_core_registers_[SP] = true;
447 blocked_core_registers_[LR] = true;
448 blocked_core_registers_[PC] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100449
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100450 // Reserve thread register.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100451 blocked_core_registers_[TR] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100452
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +0100453 // Reserve temp register.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100454 blocked_core_registers_[IP] = true;
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +0100455
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +0000456 // TODO: We currently don't use Quick's callee saved registers.
457 // We always save and restore R6 and R7 to make sure we can use three
458 // register pairs for long operations.
459 blocked_core_registers_[R4] = true;
460 blocked_core_registers_[R5] = true;
461 blocked_core_registers_[R8] = true;
462 blocked_core_registers_[R10] = true;
463 blocked_core_registers_[R11] = true;
464
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000465 blocked_fpu_registers_[S16] = true;
466 blocked_fpu_registers_[S17] = true;
467 blocked_fpu_registers_[S18] = true;
468 blocked_fpu_registers_[S19] = true;
469 blocked_fpu_registers_[S20] = true;
470 blocked_fpu_registers_[S21] = true;
471 blocked_fpu_registers_[S22] = true;
472 blocked_fpu_registers_[S23] = true;
Nicolas Geoffray3c035032014-10-28 10:46:40 +0000473 blocked_fpu_registers_[S24] = true;
474 blocked_fpu_registers_[S25] = true;
475 blocked_fpu_registers_[S26] = true;
476 blocked_fpu_registers_[S27] = true;
477 blocked_fpu_registers_[S28] = true;
478 blocked_fpu_registers_[S29] = true;
479 blocked_fpu_registers_[S30] = true;
480 blocked_fpu_registers_[S31] = true;
Calin Juravle34bacdf2014-10-07 20:23:36 +0100481
482 UpdateBlockedPairRegisters();
483}
484
485void CodeGeneratorARM::UpdateBlockedPairRegisters() const {
486 for (int i = 0; i < kNumberOfRegisterPairs; i++) {
487 ArmManagedRegister current =
488 ArmManagedRegister::FromRegisterPair(static_cast<RegisterPair>(i));
489 if (blocked_core_registers_[current.AsRegisterPairLow()]
490 || blocked_core_registers_[current.AsRegisterPairHigh()]) {
491 blocked_register_pairs_[i] = true;
492 }
493 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100494}
495
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100496InstructionCodeGeneratorARM::InstructionCodeGeneratorARM(HGraph* graph, CodeGeneratorARM* codegen)
497 : HGraphVisitor(graph),
498 assembler_(codegen->GetAssembler()),
499 codegen_(codegen) {}
500
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000501void CodeGeneratorARM::GenerateFrameEntry() {
Roland Levillain199f3362014-11-27 17:15:16 +0000502 bool skip_overflow_check =
503 IsLeafMethod() && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kArm);
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100504 if (!skip_overflow_check) {
505 if (kExplicitStackOverflowCheck) {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100506 SlowPathCodeARM* slow_path = new (GetGraph()->GetArena()) StackOverflowCheckSlowPathARM();
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100507 AddSlowPath(slow_path);
508
509 __ LoadFromOffset(kLoadWord, IP, TR, Thread::StackEndOffset<kArmWordSize>().Int32Value());
510 __ cmp(SP, ShifterOperand(IP));
511 __ b(slow_path->GetEntryLabel(), CC);
512 } else {
513 __ AddConstant(IP, SP, -static_cast<int32_t>(GetStackOverflowReservedBytes(kArm)));
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100514 __ LoadFromOffset(kLoadWord, IP, IP, 0);
Nicolas Geoffray39468442014-09-02 15:17:15 +0100515 RecordPcInfo(nullptr, 0);
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100516 }
517 }
518
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +0000519 core_spill_mask_ |= (1 << LR | 1 << R6 | 1 << R7);
520 __ PushList(1 << LR | 1 << R6 | 1 << R7);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000521
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100522 // The return PC has already been pushed on the stack.
Nicolas Geoffray707c8092014-04-04 10:50:14 +0100523 __ AddConstant(SP, -(GetFrameSize() - kNumberOfPushedRegistersAtEntry * kArmWordSize));
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100524 __ StoreToOffset(kStoreWord, R0, SP, 0);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000525}
526
527void CodeGeneratorARM::GenerateFrameExit() {
Nicolas Geoffray707c8092014-04-04 10:50:14 +0100528 __ AddConstant(SP, GetFrameSize() - kNumberOfPushedRegistersAtEntry * kArmWordSize);
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +0000529 __ PopList(1 << PC | 1 << R6 | 1 << R7);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000530}
531
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100532void CodeGeneratorARM::Bind(HBasicBlock* block) {
533 __ Bind(GetLabelOf(block));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000534}
535
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100536Location CodeGeneratorARM::GetStackLocation(HLoadLocal* load) const {
537 switch (load->GetType()) {
538 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100539 case Primitive::kPrimDouble:
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100540 return Location::DoubleStackSlot(GetStackSlot(load->GetLocal()));
541 break;
542
543 case Primitive::kPrimInt:
544 case Primitive::kPrimNot:
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100545 case Primitive::kPrimFloat:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100546 return Location::StackSlot(GetStackSlot(load->GetLocal()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100547
548 case Primitive::kPrimBoolean:
549 case Primitive::kPrimByte:
550 case Primitive::kPrimChar:
551 case Primitive::kPrimShort:
552 case Primitive::kPrimVoid:
553 LOG(FATAL) << "Unexpected type " << load->GetType();
554 }
555
556 LOG(FATAL) << "Unreachable";
557 return Location();
558}
559
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100560Location InvokeDexCallingConventionVisitor::GetNextLocation(Primitive::Type type) {
561 switch (type) {
562 case Primitive::kPrimBoolean:
563 case Primitive::kPrimByte:
564 case Primitive::kPrimChar:
565 case Primitive::kPrimShort:
566 case Primitive::kPrimInt:
567 case Primitive::kPrimNot: {
568 uint32_t index = gp_index_++;
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000569 uint32_t stack_index = stack_index_++;
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100570 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100571 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100572 } else {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000573 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index));
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100574 }
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100575 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100576
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000577 case Primitive::kPrimLong: {
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100578 uint32_t index = gp_index_;
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000579 uint32_t stack_index = stack_index_;
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100580 gp_index_ += 2;
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000581 stack_index_ += 2;
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100582 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100583 ArmManagedRegister pair = ArmManagedRegister::FromRegisterPair(
584 calling_convention.GetRegisterPairAt(index));
585 return Location::RegisterPairLocation(pair.AsRegisterPairLow(), pair.AsRegisterPairHigh());
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100586 } else if (index + 1 == calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray0a6c4592014-10-30 16:37:57 +0000587 return Location::QuickParameter(index, stack_index);
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100588 } else {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000589 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index));
590 }
591 }
592
593 case Primitive::kPrimFloat: {
594 uint32_t stack_index = stack_index_++;
595 if (float_index_ % 2 == 0) {
596 float_index_ = std::max(double_index_, float_index_);
597 }
598 if (float_index_ < calling_convention.GetNumberOfFpuRegisters()) {
599 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(float_index_++));
600 } else {
601 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index));
602 }
603 }
604
605 case Primitive::kPrimDouble: {
606 double_index_ = std::max(double_index_, RoundUp(float_index_, 2));
607 uint32_t stack_index = stack_index_;
608 stack_index_ += 2;
609 if (double_index_ + 1 < calling_convention.GetNumberOfFpuRegisters()) {
610 uint32_t index = double_index_;
611 double_index_ += 2;
612 return Location::FpuRegisterPairLocation(
613 calling_convention.GetFpuRegisterAt(index),
614 calling_convention.GetFpuRegisterAt(index + 1));
615 } else {
616 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index));
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100617 }
618 }
619
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100620 case Primitive::kPrimVoid:
621 LOG(FATAL) << "Unexpected parameter type " << type;
622 break;
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100623 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100624 return Location();
625}
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100626
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000627Location InvokeDexCallingConventionVisitor::GetReturnLocation(Primitive::Type type) {
628 switch (type) {
629 case Primitive::kPrimBoolean:
630 case Primitive::kPrimByte:
631 case Primitive::kPrimChar:
632 case Primitive::kPrimShort:
633 case Primitive::kPrimInt:
634 case Primitive::kPrimNot: {
635 return Location::RegisterLocation(R0);
636 }
637
638 case Primitive::kPrimFloat: {
639 return Location::FpuRegisterLocation(S0);
640 }
641
642 case Primitive::kPrimLong: {
643 return Location::RegisterPairLocation(R0, R1);
644 }
645
646 case Primitive::kPrimDouble: {
647 return Location::FpuRegisterPairLocation(S0, S1);
648 }
649
650 case Primitive::kPrimVoid:
651 return Location();
652 }
653 UNREACHABLE();
654 return Location();
655}
656
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100657void CodeGeneratorARM::Move32(Location destination, Location source) {
658 if (source.Equals(destination)) {
659 return;
660 }
661 if (destination.IsRegister()) {
662 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000663 __ Mov(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100664 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000665 __ vmovrs(destination.AsRegister<Register>(), source.AsFpuRegister<SRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100666 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000667 __ LoadFromOffset(kLoadWord, destination.AsRegister<Register>(), SP, source.GetStackIndex());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100668 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100669 } else if (destination.IsFpuRegister()) {
670 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000671 __ vmovsr(destination.AsFpuRegister<SRegister>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100672 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000673 __ vmovs(destination.AsFpuRegister<SRegister>(), source.AsFpuRegister<SRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100674 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000675 __ LoadSFromOffset(destination.AsFpuRegister<SRegister>(), SP, source.GetStackIndex());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100676 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100677 } else {
Calin Juravlea21f5982014-11-13 15:53:04 +0000678 DCHECK(destination.IsStackSlot()) << destination;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100679 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000680 __ StoreToOffset(kStoreWord, source.AsRegister<Register>(), SP, destination.GetStackIndex());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100681 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000682 __ StoreSToOffset(source.AsFpuRegister<SRegister>(), SP, destination.GetStackIndex());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100683 } else {
Calin Juravlea21f5982014-11-13 15:53:04 +0000684 DCHECK(source.IsStackSlot()) << source;
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100685 __ LoadFromOffset(kLoadWord, IP, SP, source.GetStackIndex());
686 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100687 }
688 }
689}
690
691void CodeGeneratorARM::Move64(Location destination, Location source) {
692 if (source.Equals(destination)) {
693 return;
694 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100695 if (destination.IsRegisterPair()) {
696 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000697 EmitParallelMoves(
698 Location::RegisterLocation(source.AsRegisterPairHigh<Register>()),
699 Location::RegisterLocation(destination.AsRegisterPairHigh<Register>()),
700 Location::RegisterLocation(source.AsRegisterPairLow<Register>()),
701 Location::RegisterLocation(destination.AsRegisterPairLow<Register>()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100702 } else if (source.IsFpuRegister()) {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000703 UNIMPLEMENTED(FATAL);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100704 } else if (source.IsQuickParameter()) {
Nicolas Geoffray0a6c4592014-10-30 16:37:57 +0000705 uint16_t register_index = source.GetQuickParameterRegisterIndex();
706 uint16_t stack_index = source.GetQuickParameterStackIndex();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100707 InvokeDexCallingConvention calling_convention;
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000708 EmitParallelMoves(
709 Location::RegisterLocation(calling_convention.GetRegisterAt(register_index)),
710 Location::RegisterLocation(destination.AsRegisterPairLow<Register>()),
711 Location::StackSlot(
712 calling_convention.GetStackOffsetOf(stack_index + 1) + GetFrameSize()),
713 Location::RegisterLocation(destination.AsRegisterPairHigh<Register>()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100714 } else {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000715 // No conflict possible, so just do the moves.
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100716 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100717 if (destination.AsRegisterPairLow<Register>() == R1) {
718 DCHECK_EQ(destination.AsRegisterPairHigh<Register>(), R2);
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100719 __ LoadFromOffset(kLoadWord, R1, SP, source.GetStackIndex());
720 __ LoadFromOffset(kLoadWord, R2, SP, source.GetHighStackIndex(kArmWordSize));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100721 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100722 __ LoadFromOffset(kLoadWordPair, destination.AsRegisterPairLow<Register>(),
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100723 SP, source.GetStackIndex());
724 }
725 }
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000726 } else if (destination.IsFpuRegisterPair()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100727 if (source.IsDoubleStackSlot()) {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000728 __ LoadDFromOffset(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()),
729 SP,
730 source.GetStackIndex());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100731 } else {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000732 UNIMPLEMENTED(FATAL);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100733 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100734 } else if (destination.IsQuickParameter()) {
735 InvokeDexCallingConvention calling_convention;
Nicolas Geoffray0a6c4592014-10-30 16:37:57 +0000736 uint16_t register_index = destination.GetQuickParameterRegisterIndex();
737 uint16_t stack_index = destination.GetQuickParameterStackIndex();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100738 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000739 UNIMPLEMENTED(FATAL);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100740 } else if (source.IsFpuRegister()) {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000741 UNIMPLEMENTED(FATAL);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100742 } else {
743 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000744 EmitParallelMoves(
745 Location::StackSlot(source.GetStackIndex()),
746 Location::RegisterLocation(calling_convention.GetRegisterAt(register_index)),
747 Location::StackSlot(source.GetHighStackIndex(kArmWordSize)),
748 Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index + 1)));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100749 }
750 } else {
751 DCHECK(destination.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100752 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000753 // No conflict possible, so just do the moves.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100754 if (source.AsRegisterPairLow<Register>() == R1) {
755 DCHECK_EQ(source.AsRegisterPairHigh<Register>(), R2);
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100756 __ StoreToOffset(kStoreWord, R1, SP, destination.GetStackIndex());
757 __ StoreToOffset(kStoreWord, R2, SP, destination.GetHighStackIndex(kArmWordSize));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100758 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100759 __ StoreToOffset(kStoreWordPair, source.AsRegisterPairLow<Register>(),
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100760 SP, destination.GetStackIndex());
761 }
762 } else if (source.IsQuickParameter()) {
763 InvokeDexCallingConvention calling_convention;
Nicolas Geoffray0a6c4592014-10-30 16:37:57 +0000764 uint16_t register_index = source.GetQuickParameterRegisterIndex();
765 uint16_t stack_index = source.GetQuickParameterStackIndex();
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000766 // Just move the low part. The only time a source is a quick parameter is
767 // when moving the parameter to its stack locations. And the (Java) caller
768 // of this method has already done that.
Nicolas Geoffray0a6c4592014-10-30 16:37:57 +0000769 __ StoreToOffset(kStoreWord, calling_convention.GetRegisterAt(register_index),
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000770 SP, destination.GetStackIndex());
771 DCHECK_EQ(calling_convention.GetStackOffsetOf(stack_index + 1) + GetFrameSize(),
772 static_cast<size_t>(destination.GetHighStackIndex(kArmWordSize)));
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000773 } else if (source.IsFpuRegisterPair()) {
774 __ StoreDToOffset(FromLowSToD(source.AsFpuRegisterPairLow<SRegister>()),
775 SP,
776 destination.GetStackIndex());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100777 } else {
778 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000779 EmitParallelMoves(
780 Location::StackSlot(source.GetStackIndex()),
781 Location::StackSlot(destination.GetStackIndex()),
782 Location::StackSlot(source.GetHighStackIndex(kArmWordSize)),
783 Location::StackSlot(destination.GetHighStackIndex(kArmWordSize)));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100784 }
785 }
786}
787
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100788void CodeGeneratorARM::Move(HInstruction* instruction, Location location, HInstruction* move_for) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100789 LocationSummary* locations = instruction->GetLocations();
790 if (locations != nullptr && locations->Out().Equals(location)) {
791 return;
792 }
793
Calin Juravlea21f5982014-11-13 15:53:04 +0000794 if (locations != nullptr && locations->Out().IsConstant()) {
795 HConstant* const_to_move = locations->Out().GetConstant();
796 if (const_to_move->IsIntConstant()) {
797 int32_t value = const_to_move->AsIntConstant()->GetValue();
798 if (location.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000799 __ LoadImmediate(location.AsRegister<Register>(), value);
Calin Juravlea21f5982014-11-13 15:53:04 +0000800 } else {
801 DCHECK(location.IsStackSlot());
802 __ LoadImmediate(IP, value);
803 __ StoreToOffset(kStoreWord, IP, SP, location.GetStackIndex());
804 }
805 } else if (const_to_move->IsLongConstant()) {
806 int64_t value = const_to_move->AsLongConstant()->GetValue();
807 if (location.IsRegisterPair()) {
808 __ LoadImmediate(location.AsRegisterPairLow<Register>(), Low32Bits(value));
809 __ LoadImmediate(location.AsRegisterPairHigh<Register>(), High32Bits(value));
810 } else {
811 DCHECK(location.IsDoubleStackSlot());
812 __ LoadImmediate(IP, Low32Bits(value));
813 __ StoreToOffset(kStoreWord, IP, SP, location.GetStackIndex());
814 __ LoadImmediate(IP, High32Bits(value));
815 __ StoreToOffset(kStoreWord, IP, SP, location.GetHighStackIndex(kArmWordSize));
816 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100817 }
Roland Levillain476df552014-10-09 17:51:36 +0100818 } else if (instruction->IsLoadLocal()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100819 uint32_t stack_slot = GetStackSlot(instruction->AsLoadLocal()->GetLocal());
820 switch (instruction->GetType()) {
821 case Primitive::kPrimBoolean:
822 case Primitive::kPrimByte:
823 case Primitive::kPrimChar:
824 case Primitive::kPrimShort:
825 case Primitive::kPrimInt:
826 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100827 case Primitive::kPrimFloat:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100828 Move32(location, Location::StackSlot(stack_slot));
829 break;
830
831 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100832 case Primitive::kPrimDouble:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100833 Move64(location, Location::DoubleStackSlot(stack_slot));
834 break;
835
836 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100837 LOG(FATAL) << "Unexpected type " << instruction->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100838 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +0000839 } else if (instruction->IsTemporary()) {
840 Location temp_location = GetTemporaryLocation(instruction->AsTemporary());
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000841 if (temp_location.IsStackSlot()) {
842 Move32(location, temp_location);
843 } else {
844 DCHECK(temp_location.IsDoubleStackSlot());
845 Move64(location, temp_location);
846 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000847 } else {
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100848 DCHECK((instruction->GetNext() == move_for) || instruction->GetNext()->IsTemporary());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100849 switch (instruction->GetType()) {
850 case Primitive::kPrimBoolean:
851 case Primitive::kPrimByte:
852 case Primitive::kPrimChar:
853 case Primitive::kPrimShort:
854 case Primitive::kPrimNot:
855 case Primitive::kPrimInt:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100856 case Primitive::kPrimFloat:
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100857 Move32(location, locations->Out());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100858 break;
859
860 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100861 case Primitive::kPrimDouble:
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100862 Move64(location, locations->Out());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100863 break;
864
865 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100866 LOG(FATAL) << "Unexpected type " << instruction->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100867 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000868 }
869}
870
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100871void CodeGeneratorARM::InvokeRuntime(int32_t entry_point_offset,
872 HInstruction* instruction,
873 uint32_t dex_pc) {
874 __ LoadFromOffset(kLoadWord, LR, TR, entry_point_offset);
875 __ blx(LR);
876 RecordPcInfo(instruction, dex_pc);
877 DCHECK(instruction->IsSuspendCheck()
878 || instruction->IsBoundsCheck()
879 || instruction->IsNullCheck()
Calin Juravled0d48522014-11-04 16:40:20 +0000880 || instruction->IsDivZeroCheck()
Roland Levillain624279f2014-12-04 11:54:28 +0000881 || instruction->GetLocations()->CanCall()
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100882 || !IsLeafMethod());
883}
884
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000885void LocationsBuilderARM::VisitGoto(HGoto* got) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000886 got->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000887}
888
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000889void InstructionCodeGeneratorARM::VisitGoto(HGoto* got) {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000890 HBasicBlock* successor = got->GetSuccessor();
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100891 DCHECK(!successor->IsExitBlock());
892
893 HBasicBlock* block = got->GetBlock();
894 HInstruction* previous = got->GetPrevious();
895
896 HLoopInformation* info = block->GetLoopInformation();
897 if (info != nullptr && info->IsBackEdge(block) && info->HasSuspendCheck()) {
898 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
899 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
900 return;
901 }
902
903 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
904 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
905 }
906 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000907 __ b(codegen_->GetLabelOf(successor));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000908 }
909}
910
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000911void LocationsBuilderARM::VisitExit(HExit* exit) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000912 exit->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000913}
914
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000915void InstructionCodeGeneratorARM::VisitExit(HExit* exit) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700916 UNUSED(exit);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000917 if (kIsDebugBuild) {
918 __ Comment("Unreachable");
919 __ bkpt(0);
920 }
921}
922
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000923void LocationsBuilderARM::VisitIf(HIf* if_instr) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100924 LocationSummary* locations =
925 new (GetGraph()->GetArena()) LocationSummary(if_instr, LocationSummary::kNoCall);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100926 HInstruction* cond = if_instr->InputAt(0);
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100927 if (!cond->IsCondition() || cond->AsCondition()->NeedsMaterialization()) {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +0100928 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100929 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000930}
931
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000932void InstructionCodeGeneratorARM::VisitIf(HIf* if_instr) {
Dave Allison20dfc792014-06-16 20:44:29 -0700933 HInstruction* cond = if_instr->InputAt(0);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100934 if (cond->IsIntConstant()) {
935 // Constant condition, statically compared against 1.
936 int32_t cond_value = cond->AsIntConstant()->GetValue();
937 if (cond_value == 1) {
938 if (!codegen_->GoesToNextBlock(if_instr->GetBlock(),
939 if_instr->IfTrueSuccessor())) {
940 __ b(codegen_->GetLabelOf(if_instr->IfTrueSuccessor()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100941 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100942 return;
943 } else {
944 DCHECK_EQ(cond_value, 0);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100945 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100946 } else {
947 if (!cond->IsCondition() || cond->AsCondition()->NeedsMaterialization()) {
948 // Condition has been materialized, compare the output to 0
949 DCHECK(if_instr->GetLocations()->InAt(0).IsRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +0000950 __ cmp(if_instr->GetLocations()->InAt(0).AsRegister<Register>(),
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100951 ShifterOperand(0));
952 __ b(codegen_->GetLabelOf(if_instr->IfTrueSuccessor()), NE);
953 } else {
954 // Condition has not been materialized, use its inputs as the
955 // comparison and its condition as the branch condition.
956 LocationSummary* locations = cond->GetLocations();
Nicolas Geoffray3bcc8ea2014-11-28 15:00:02 +0000957 Register left = locations->InAt(0).AsRegister<Register>();
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100958 if (locations->InAt(1).IsRegister()) {
Nicolas Geoffray3bcc8ea2014-11-28 15:00:02 +0000959 __ cmp(left, ShifterOperand(locations->InAt(1).AsRegister<Register>()));
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100960 } else {
961 DCHECK(locations->InAt(1).IsConstant());
962 int32_t value =
963 locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
964 ShifterOperand operand;
Nicolas Geoffray3bcc8ea2014-11-28 15:00:02 +0000965 if (GetAssembler()->ShifterOperandCanHold(R0, left, CMP, value, &operand)) {
966 __ cmp(left, operand);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100967 } else {
968 Register temp = IP;
969 __ LoadImmediate(temp, value);
Nicolas Geoffray3bcc8ea2014-11-28 15:00:02 +0000970 __ cmp(left, ShifterOperand(temp));
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100971 }
972 }
973 __ b(codegen_->GetLabelOf(if_instr->IfTrueSuccessor()),
974 ARMCondition(cond->AsCondition()->GetCondition()));
975 }
Dave Allison20dfc792014-06-16 20:44:29 -0700976 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100977 if (!codegen_->GoesToNextBlock(if_instr->GetBlock(),
978 if_instr->IfFalseSuccessor())) {
Dave Allison20dfc792014-06-16 20:44:29 -0700979 __ b(codegen_->GetLabelOf(if_instr->IfFalseSuccessor()));
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000980 }
981}
982
Dave Allison20dfc792014-06-16 20:44:29 -0700983
984void LocationsBuilderARM::VisitCondition(HCondition* comp) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100985 LocationSummary* locations =
986 new (GetGraph()->GetArena()) LocationSummary(comp, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +0100987 locations->SetInAt(0, Location::RequiresRegister());
988 locations->SetInAt(1, Location::RegisterOrConstant(comp->InputAt(1)));
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100989 if (comp->NeedsMaterialization()) {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +0100990 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100991 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000992}
993
Dave Allison20dfc792014-06-16 20:44:29 -0700994void InstructionCodeGeneratorARM::VisitCondition(HCondition* comp) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100995 if (!comp->NeedsMaterialization()) return;
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100996 LocationSummary* locations = comp->GetLocations();
Nicolas Geoffray3bcc8ea2014-11-28 15:00:02 +0000997 Register left = locations->InAt(0).AsRegister<Register>();
998
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100999 if (locations->InAt(1).IsRegister()) {
Nicolas Geoffray3bcc8ea2014-11-28 15:00:02 +00001000 __ cmp(left, ShifterOperand(locations->InAt(1).AsRegister<Register>()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001001 } else {
1002 DCHECK(locations->InAt(1).IsConstant());
1003 int32_t value = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
1004 ShifterOperand operand;
Nicolas Geoffray3bcc8ea2014-11-28 15:00:02 +00001005 if (GetAssembler()->ShifterOperandCanHold(R0, left, CMP, value, &operand)) {
1006 __ cmp(left, operand);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001007 } else {
1008 Register temp = IP;
1009 __ LoadImmediate(temp, value);
Nicolas Geoffray3bcc8ea2014-11-28 15:00:02 +00001010 __ cmp(left, ShifterOperand(temp));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001011 }
Dave Allison20dfc792014-06-16 20:44:29 -07001012 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001013 __ it(ARMCondition(comp->GetCondition()), kItElse);
Roland Levillain271ab9c2014-11-27 15:23:57 +00001014 __ mov(locations->Out().AsRegister<Register>(), ShifterOperand(1),
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001015 ARMCondition(comp->GetCondition()));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001016 __ mov(locations->Out().AsRegister<Register>(), ShifterOperand(0),
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001017 ARMOppositeCondition(comp->GetCondition()));
Dave Allison20dfc792014-06-16 20:44:29 -07001018}
1019
1020void LocationsBuilderARM::VisitEqual(HEqual* comp) {
1021 VisitCondition(comp);
1022}
1023
1024void InstructionCodeGeneratorARM::VisitEqual(HEqual* comp) {
1025 VisitCondition(comp);
1026}
1027
1028void LocationsBuilderARM::VisitNotEqual(HNotEqual* comp) {
1029 VisitCondition(comp);
1030}
1031
1032void InstructionCodeGeneratorARM::VisitNotEqual(HNotEqual* comp) {
1033 VisitCondition(comp);
1034}
1035
1036void LocationsBuilderARM::VisitLessThan(HLessThan* comp) {
1037 VisitCondition(comp);
1038}
1039
1040void InstructionCodeGeneratorARM::VisitLessThan(HLessThan* comp) {
1041 VisitCondition(comp);
1042}
1043
1044void LocationsBuilderARM::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
1045 VisitCondition(comp);
1046}
1047
1048void InstructionCodeGeneratorARM::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
1049 VisitCondition(comp);
1050}
1051
1052void LocationsBuilderARM::VisitGreaterThan(HGreaterThan* comp) {
1053 VisitCondition(comp);
1054}
1055
1056void InstructionCodeGeneratorARM::VisitGreaterThan(HGreaterThan* comp) {
1057 VisitCondition(comp);
1058}
1059
1060void LocationsBuilderARM::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
1061 VisitCondition(comp);
1062}
1063
1064void InstructionCodeGeneratorARM::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
1065 VisitCondition(comp);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001066}
1067
1068void LocationsBuilderARM::VisitLocal(HLocal* local) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001069 local->SetLocations(nullptr);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001070}
1071
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001072void InstructionCodeGeneratorARM::VisitLocal(HLocal* local) {
1073 DCHECK_EQ(local->GetBlock(), GetGraph()->GetEntryBlock());
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001074}
1075
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001076void LocationsBuilderARM::VisitLoadLocal(HLoadLocal* load) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001077 load->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001078}
1079
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001080void InstructionCodeGeneratorARM::VisitLoadLocal(HLoadLocal* load) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001081 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001082 UNUSED(load);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001083}
1084
1085void LocationsBuilderARM::VisitStoreLocal(HStoreLocal* store) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001086 LocationSummary* locations =
1087 new (GetGraph()->GetArena()) LocationSummary(store, LocationSummary::kNoCall);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001088 switch (store->InputAt(1)->GetType()) {
1089 case Primitive::kPrimBoolean:
1090 case Primitive::kPrimByte:
1091 case Primitive::kPrimChar:
1092 case Primitive::kPrimShort:
1093 case Primitive::kPrimInt:
1094 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001095 case Primitive::kPrimFloat:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001096 locations->SetInAt(1, Location::StackSlot(codegen_->GetStackSlot(store->GetLocal())));
1097 break;
1098
1099 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001100 case Primitive::kPrimDouble:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001101 locations->SetInAt(1, Location::DoubleStackSlot(codegen_->GetStackSlot(store->GetLocal())));
1102 break;
1103
1104 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001105 LOG(FATAL) << "Unexpected local type " << store->InputAt(1)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001106 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001107}
1108
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001109void InstructionCodeGeneratorARM::VisitStoreLocal(HStoreLocal* store) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001110 UNUSED(store);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001111}
1112
1113void LocationsBuilderARM::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001114 LocationSummary* locations =
1115 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001116 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001117}
1118
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001119void InstructionCodeGeneratorARM::VisitIntConstant(HIntConstant* constant) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001120 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001121 UNUSED(constant);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001122}
1123
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001124void LocationsBuilderARM::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001125 LocationSummary* locations =
1126 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001127 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001128}
1129
1130void InstructionCodeGeneratorARM::VisitLongConstant(HLongConstant* constant) {
1131 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001132 UNUSED(constant);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001133}
1134
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001135void LocationsBuilderARM::VisitFloatConstant(HFloatConstant* constant) {
1136 LocationSummary* locations =
1137 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1138 locations->SetOut(Location::ConstantLocation(constant));
1139}
1140
1141void InstructionCodeGeneratorARM::VisitFloatConstant(HFloatConstant* constant) {
1142 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001143 UNUSED(constant);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001144}
1145
1146void LocationsBuilderARM::VisitDoubleConstant(HDoubleConstant* constant) {
1147 LocationSummary* locations =
1148 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1149 locations->SetOut(Location::ConstantLocation(constant));
1150}
1151
1152void InstructionCodeGeneratorARM::VisitDoubleConstant(HDoubleConstant* constant) {
1153 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001154 UNUSED(constant);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001155}
1156
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001157void LocationsBuilderARM::VisitReturnVoid(HReturnVoid* ret) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001158 ret->SetLocations(nullptr);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001159}
1160
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001161void InstructionCodeGeneratorARM::VisitReturnVoid(HReturnVoid* ret) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001162 UNUSED(ret);
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001163 codegen_->GenerateFrameExit();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001164}
1165
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001166void LocationsBuilderARM::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001167 LocationSummary* locations =
1168 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001169 locations->SetInAt(0, parameter_visitor_.GetReturnLocation(ret->InputAt(0)->GetType()));
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001170}
1171
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001172void InstructionCodeGeneratorARM::VisitReturn(HReturn* ret) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001173 UNUSED(ret);
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001174 codegen_->GenerateFrameExit();
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001175}
1176
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001177void LocationsBuilderARM::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001178 HandleInvoke(invoke);
1179}
1180
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001181void CodeGeneratorARM::LoadCurrentMethod(Register reg) {
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001182 __ LoadFromOffset(kLoadWord, reg, SP, kCurrentMethodStackOffset);
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001183}
1184
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001185void InstructionCodeGeneratorARM::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001186 Register temp = invoke->GetLocations()->GetTemp(0).AsRegister<Register>();
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001187
1188 // TODO: Implement all kinds of calls:
1189 // 1) boot -> boot
1190 // 2) app -> boot
1191 // 3) app -> app
1192 //
1193 // Currently we implement the app -> app logic, which looks up in the resolve cache.
1194
1195 // temp = method;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001196 codegen_->LoadCurrentMethod(temp);
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001197 // temp = temp->dex_cache_resolved_methods_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001198 __ LoadFromOffset(
1199 kLoadWord, temp, temp, mirror::ArtMethod::DexCacheResolvedMethodsOffset().Int32Value());
Nicolas Geoffray4e44c822014-12-17 12:25:12 +00001200 // temp = temp[index_in_cache]
1201 __ LoadFromOffset(
1202 kLoadWord, temp, temp, CodeGenerator::GetCacheOffset(invoke->GetIndexInDexCache()));
1203 // LR = temp[offset_of_quick_compiled_code]
1204 __ LoadFromOffset(kLoadWord, LR, temp,
1205 mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset(
1206 kArmWordSize).Int32Value());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001207 // LR()
1208 __ blx(LR);
1209
1210 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
1211 DCHECK(!codegen_->IsLeafMethod());
1212}
1213
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001214void LocationsBuilderARM::HandleInvoke(HInvoke* invoke) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001215 LocationSummary* locations =
1216 new (GetGraph()->GetArena()) LocationSummary(invoke, LocationSummary::kCall);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001217 locations->AddTemp(Location::RegisterLocation(R0));
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001218
1219 InvokeDexCallingConventionVisitor calling_convention_visitor;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001220 for (size_t i = 0; i < invoke->InputCount(); i++) {
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001221 HInstruction* input = invoke->InputAt(i);
1222 locations->SetInAt(i, calling_convention_visitor.GetNextLocation(input->GetType()));
1223 }
1224
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001225 locations->SetOut(calling_convention_visitor.GetReturnLocation(invoke->GetType()));
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001226}
1227
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001228void LocationsBuilderARM::VisitInvokeVirtual(HInvokeVirtual* invoke) {
1229 HandleInvoke(invoke);
1230}
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001231
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001232void InstructionCodeGeneratorARM::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001233 Register temp = invoke->GetLocations()->GetTemp(0).AsRegister<Register>();
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001234 uint32_t method_offset = mirror::Class::EmbeddedVTableOffset().Uint32Value() +
1235 invoke->GetVTableIndex() * sizeof(mirror::Class::VTableEntry);
1236 LocationSummary* locations = invoke->GetLocations();
1237 Location receiver = locations->InAt(0);
1238 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
1239 // temp = object->GetClass();
1240 if (receiver.IsStackSlot()) {
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001241 __ LoadFromOffset(kLoadWord, temp, SP, receiver.GetStackIndex());
1242 __ LoadFromOffset(kLoadWord, temp, temp, class_offset);
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001243 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001244 __ LoadFromOffset(kLoadWord, temp, receiver.AsRegister<Register>(), class_offset);
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001245 }
1246 // temp = temp->GetMethodAt(method_offset);
Mathieu Chartier2d721012014-11-10 11:08:06 -08001247 uint32_t entry_point = mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Nicolas Geoffray86a8d7a2014-11-19 08:47:18 +00001248 kArmWordSize).Int32Value();
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001249 __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001250 // LR = temp->GetEntryPoint();
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001251 __ LoadFromOffset(kLoadWord, LR, temp, entry_point);
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001252 // LR();
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001253 __ blx(LR);
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001254 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001255 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001256}
1257
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001258void LocationsBuilderARM::VisitInvokeInterface(HInvokeInterface* invoke) {
1259 HandleInvoke(invoke);
1260 // Add the hidden argument.
1261 invoke->GetLocations()->AddTemp(Location::RegisterLocation(R12));
1262}
1263
1264void InstructionCodeGeneratorARM::VisitInvokeInterface(HInvokeInterface* invoke) {
1265 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain271ab9c2014-11-27 15:23:57 +00001266 Register temp = invoke->GetLocations()->GetTemp(0).AsRegister<Register>();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001267 uint32_t method_offset = mirror::Class::EmbeddedImTableOffset().Uint32Value() +
1268 (invoke->GetImtIndex() % mirror::Class::kImtSize) * sizeof(mirror::Class::ImTableEntry);
1269 LocationSummary* locations = invoke->GetLocations();
1270 Location receiver = locations->InAt(0);
1271 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
1272
1273 // Set the hidden argument.
Roland Levillain199f3362014-11-27 17:15:16 +00001274 __ LoadImmediate(invoke->GetLocations()->GetTemp(1).AsRegister<Register>(),
1275 invoke->GetDexMethodIndex());
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001276
1277 // temp = object->GetClass();
1278 if (receiver.IsStackSlot()) {
1279 __ LoadFromOffset(kLoadWord, temp, SP, receiver.GetStackIndex());
1280 __ LoadFromOffset(kLoadWord, temp, temp, class_offset);
1281 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001282 __ LoadFromOffset(kLoadWord, temp, receiver.AsRegister<Register>(), class_offset);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001283 }
1284 // temp = temp->GetImtEntryAt(method_offset);
Mathieu Chartier2d721012014-11-10 11:08:06 -08001285 uint32_t entry_point = mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Nicolas Geoffray86a8d7a2014-11-19 08:47:18 +00001286 kArmWordSize).Int32Value();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001287 __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
1288 // LR = temp->GetEntryPoint();
1289 __ LoadFromOffset(kLoadWord, LR, temp, entry_point);
1290 // LR();
1291 __ blx(LR);
1292 DCHECK(!codegen_->IsLeafMethod());
1293 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
1294}
1295
Roland Levillain88cb1752014-10-20 16:36:47 +01001296void LocationsBuilderARM::VisitNeg(HNeg* neg) {
1297 LocationSummary* locations =
1298 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
1299 switch (neg->GetResultType()) {
1300 case Primitive::kPrimInt:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001301 case Primitive::kPrimLong: {
1302 bool output_overlaps = (neg->GetResultType() == Primitive::kPrimLong);
Roland Levillain88cb1752014-10-20 16:36:47 +01001303 locations->SetInAt(0, Location::RequiresRegister());
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001304 locations->SetOut(Location::RequiresRegister(), output_overlaps);
Roland Levillain88cb1752014-10-20 16:36:47 +01001305 break;
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001306 }
Roland Levillain88cb1752014-10-20 16:36:47 +01001307
Roland Levillain88cb1752014-10-20 16:36:47 +01001308 case Primitive::kPrimFloat:
1309 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00001310 locations->SetInAt(0, Location::RequiresFpuRegister());
1311 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillain88cb1752014-10-20 16:36:47 +01001312 break;
1313
1314 default:
1315 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1316 }
1317}
1318
1319void InstructionCodeGeneratorARM::VisitNeg(HNeg* neg) {
1320 LocationSummary* locations = neg->GetLocations();
1321 Location out = locations->Out();
1322 Location in = locations->InAt(0);
1323 switch (neg->GetResultType()) {
1324 case Primitive::kPrimInt:
1325 DCHECK(in.IsRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001326 __ rsb(out.AsRegister<Register>(), in.AsRegister<Register>(), ShifterOperand(0));
Roland Levillain88cb1752014-10-20 16:36:47 +01001327 break;
1328
1329 case Primitive::kPrimLong:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001330 DCHECK(in.IsRegisterPair());
1331 // out.lo = 0 - in.lo (and update the carry/borrow (C) flag)
1332 __ rsbs(out.AsRegisterPairLow<Register>(),
1333 in.AsRegisterPairLow<Register>(),
1334 ShifterOperand(0));
1335 // We cannot emit an RSC (Reverse Subtract with Carry)
1336 // instruction here, as it does not exist in the Thumb-2
1337 // instruction set. We use the following approach
1338 // using SBC and SUB instead.
1339 //
1340 // out.hi = -C
1341 __ sbc(out.AsRegisterPairHigh<Register>(),
1342 out.AsRegisterPairHigh<Register>(),
1343 ShifterOperand(out.AsRegisterPairHigh<Register>()));
1344 // out.hi = out.hi - in.hi
1345 __ sub(out.AsRegisterPairHigh<Register>(),
1346 out.AsRegisterPairHigh<Register>(),
1347 ShifterOperand(in.AsRegisterPairHigh<Register>()));
1348 break;
1349
Roland Levillain88cb1752014-10-20 16:36:47 +01001350 case Primitive::kPrimFloat:
Roland Levillain3dbcb382014-10-28 17:30:07 +00001351 DCHECK(in.IsFpuRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001352 __ vnegs(out.AsFpuRegister<SRegister>(), in.AsFpuRegister<SRegister>());
Roland Levillain3dbcb382014-10-28 17:30:07 +00001353 break;
1354
Roland Levillain88cb1752014-10-20 16:36:47 +01001355 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00001356 DCHECK(in.IsFpuRegisterPair());
1357 __ vnegd(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
1358 FromLowSToD(in.AsFpuRegisterPairLow<SRegister>()));
Roland Levillain88cb1752014-10-20 16:36:47 +01001359 break;
1360
1361 default:
1362 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1363 }
1364}
1365
Roland Levillaindff1f282014-11-05 14:15:05 +00001366void LocationsBuilderARM::VisitTypeConversion(HTypeConversion* conversion) {
Roland Levillaindff1f282014-11-05 14:15:05 +00001367 Primitive::Type result_type = conversion->GetResultType();
1368 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00001369 DCHECK_NE(result_type, input_type);
Roland Levillain624279f2014-12-04 11:54:28 +00001370
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001371 // The float-to-long and double-to-long type conversions rely on a
1372 // call to the runtime.
Roland Levillain624279f2014-12-04 11:54:28 +00001373 LocationSummary::CallKind call_kind =
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001374 ((input_type == Primitive::kPrimFloat || input_type == Primitive::kPrimDouble)
1375 && result_type == Primitive::kPrimLong)
Roland Levillain624279f2014-12-04 11:54:28 +00001376 ? LocationSummary::kCall
1377 : LocationSummary::kNoCall;
1378 LocationSummary* locations =
1379 new (GetGraph()->GetArena()) LocationSummary(conversion, call_kind);
1380
Roland Levillaindff1f282014-11-05 14:15:05 +00001381 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00001382 case Primitive::kPrimByte:
1383 switch (input_type) {
1384 case Primitive::kPrimShort:
1385 case Primitive::kPrimInt:
1386 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001387 // Processing a Dex `int-to-byte' instruction.
Roland Levillain51d3fc42014-11-13 14:11:42 +00001388 locations->SetInAt(0, Location::RequiresRegister());
1389 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1390 break;
1391
1392 default:
1393 LOG(FATAL) << "Unexpected type conversion from " << input_type
1394 << " to " << result_type;
1395 }
1396 break;
1397
Roland Levillain01a8d712014-11-14 16:27:39 +00001398 case Primitive::kPrimShort:
1399 switch (input_type) {
1400 case Primitive::kPrimByte:
1401 case Primitive::kPrimInt:
1402 case Primitive::kPrimChar:
1403 // Processing a Dex `int-to-short' instruction.
1404 locations->SetInAt(0, Location::RequiresRegister());
1405 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1406 break;
1407
1408 default:
1409 LOG(FATAL) << "Unexpected type conversion from " << input_type
1410 << " to " << result_type;
1411 }
1412 break;
1413
Roland Levillain946e1432014-11-11 17:35:19 +00001414 case Primitive::kPrimInt:
1415 switch (input_type) {
1416 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00001417 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00001418 locations->SetInAt(0, Location::Any());
1419 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1420 break;
1421
1422 case Primitive::kPrimFloat:
Roland Levillain3f8f9362014-12-02 17:45:01 +00001423 // Processing a Dex `float-to-int' instruction.
1424 locations->SetInAt(0, Location::RequiresFpuRegister());
1425 locations->SetOut(Location::RequiresRegister());
1426 locations->AddTemp(Location::RequiresFpuRegister());
1427 break;
1428
Roland Levillain946e1432014-11-11 17:35:19 +00001429 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001430 // Processing a Dex `double-to-int' instruction.
1431 locations->SetInAt(0, Location::RequiresFpuRegister());
1432 locations->SetOut(Location::RequiresRegister());
1433 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain946e1432014-11-11 17:35:19 +00001434 break;
1435
1436 default:
1437 LOG(FATAL) << "Unexpected type conversion from " << input_type
1438 << " to " << result_type;
1439 }
1440 break;
1441
Roland Levillaindff1f282014-11-05 14:15:05 +00001442 case Primitive::kPrimLong:
1443 switch (input_type) {
1444 case Primitive::kPrimByte:
1445 case Primitive::kPrimShort:
1446 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00001447 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001448 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00001449 locations->SetInAt(0, Location::RequiresRegister());
1450 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1451 break;
1452
Roland Levillain624279f2014-12-04 11:54:28 +00001453 case Primitive::kPrimFloat: {
1454 // Processing a Dex `float-to-long' instruction.
1455 InvokeRuntimeCallingConvention calling_convention;
1456 locations->SetInAt(0, Location::FpuRegisterLocation(
1457 calling_convention.GetFpuRegisterAt(0)));
1458 locations->SetOut(Location::RegisterPairLocation(R0, R1));
1459 break;
1460 }
1461
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001462 case Primitive::kPrimDouble: {
1463 // Processing a Dex `double-to-long' instruction.
1464 InvokeRuntimeCallingConvention calling_convention;
1465 locations->SetInAt(0, Location::FpuRegisterPairLocation(
1466 calling_convention.GetFpuRegisterAt(0),
1467 calling_convention.GetFpuRegisterAt(1)));
1468 locations->SetOut(Location::RegisterPairLocation(R0, R1));
Roland Levillaindff1f282014-11-05 14:15:05 +00001469 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001470 }
Roland Levillaindff1f282014-11-05 14:15:05 +00001471
1472 default:
1473 LOG(FATAL) << "Unexpected type conversion from " << input_type
1474 << " to " << result_type;
1475 }
1476 break;
1477
Roland Levillain981e4542014-11-14 11:47:14 +00001478 case Primitive::kPrimChar:
1479 switch (input_type) {
1480 case Primitive::kPrimByte:
1481 case Primitive::kPrimShort:
1482 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00001483 // Processing a Dex `int-to-char' instruction.
1484 locations->SetInAt(0, Location::RequiresRegister());
1485 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1486 break;
1487
1488 default:
1489 LOG(FATAL) << "Unexpected type conversion from " << input_type
1490 << " to " << result_type;
1491 }
1492 break;
1493
Roland Levillaindff1f282014-11-05 14:15:05 +00001494 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00001495 switch (input_type) {
1496 case Primitive::kPrimByte:
1497 case Primitive::kPrimShort:
1498 case Primitive::kPrimInt:
1499 case Primitive::kPrimChar:
1500 // Processing a Dex `int-to-float' instruction.
1501 locations->SetInAt(0, Location::RequiresRegister());
1502 locations->SetOut(Location::RequiresFpuRegister());
1503 break;
1504
1505 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00001506 // Processing a Dex `long-to-float' instruction.
1507 locations->SetInAt(0, Location::RequiresRegister());
1508 locations->SetOut(Location::RequiresFpuRegister());
1509 locations->AddTemp(Location::RequiresRegister());
1510 locations->AddTemp(Location::RequiresRegister());
1511 locations->AddTemp(Location::RequiresFpuRegister());
1512 locations->AddTemp(Location::RequiresFpuRegister());
1513 break;
1514
Roland Levillaincff13742014-11-17 14:32:17 +00001515 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001516 // Processing a Dex `double-to-float' instruction.
1517 locations->SetInAt(0, Location::RequiresFpuRegister());
1518 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00001519 break;
1520
1521 default:
1522 LOG(FATAL) << "Unexpected type conversion from " << input_type
1523 << " to " << result_type;
1524 };
1525 break;
1526
Roland Levillaindff1f282014-11-05 14:15:05 +00001527 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00001528 switch (input_type) {
1529 case Primitive::kPrimByte:
1530 case Primitive::kPrimShort:
1531 case Primitive::kPrimInt:
1532 case Primitive::kPrimChar:
1533 // Processing a Dex `int-to-double' instruction.
1534 locations->SetInAt(0, Location::RequiresRegister());
1535 locations->SetOut(Location::RequiresFpuRegister());
1536 break;
1537
1538 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00001539 // Processing a Dex `long-to-double' instruction.
1540 locations->SetInAt(0, Location::RequiresRegister());
1541 locations->SetOut(Location::RequiresFpuRegister());
1542 locations->AddTemp(Location::RequiresRegister());
1543 locations->AddTemp(Location::RequiresRegister());
1544 locations->AddTemp(Location::RequiresFpuRegister());
1545 break;
1546
Roland Levillaincff13742014-11-17 14:32:17 +00001547 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001548 // Processing a Dex `float-to-double' instruction.
1549 locations->SetInAt(0, Location::RequiresFpuRegister());
1550 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00001551 break;
1552
1553 default:
1554 LOG(FATAL) << "Unexpected type conversion from " << input_type
1555 << " to " << result_type;
1556 };
Roland Levillaindff1f282014-11-05 14:15:05 +00001557 break;
1558
1559 default:
1560 LOG(FATAL) << "Unexpected type conversion from " << input_type
1561 << " to " << result_type;
1562 }
1563}
1564
1565void InstructionCodeGeneratorARM::VisitTypeConversion(HTypeConversion* conversion) {
1566 LocationSummary* locations = conversion->GetLocations();
1567 Location out = locations->Out();
1568 Location in = locations->InAt(0);
1569 Primitive::Type result_type = conversion->GetResultType();
1570 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00001571 DCHECK_NE(result_type, input_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00001572 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00001573 case Primitive::kPrimByte:
1574 switch (input_type) {
1575 case Primitive::kPrimShort:
1576 case Primitive::kPrimInt:
1577 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001578 // Processing a Dex `int-to-byte' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00001579 __ sbfx(out.AsRegister<Register>(), in.AsRegister<Register>(), 0, 8);
Roland Levillain51d3fc42014-11-13 14:11:42 +00001580 break;
1581
1582 default:
1583 LOG(FATAL) << "Unexpected type conversion from " << input_type
1584 << " to " << result_type;
1585 }
1586 break;
1587
Roland Levillain01a8d712014-11-14 16:27:39 +00001588 case Primitive::kPrimShort:
1589 switch (input_type) {
1590 case Primitive::kPrimByte:
1591 case Primitive::kPrimInt:
1592 case Primitive::kPrimChar:
1593 // Processing a Dex `int-to-short' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00001594 __ sbfx(out.AsRegister<Register>(), in.AsRegister<Register>(), 0, 16);
Roland Levillain01a8d712014-11-14 16:27:39 +00001595 break;
1596
1597 default:
1598 LOG(FATAL) << "Unexpected type conversion from " << input_type
1599 << " to " << result_type;
1600 }
1601 break;
1602
Roland Levillain946e1432014-11-11 17:35:19 +00001603 case Primitive::kPrimInt:
1604 switch (input_type) {
1605 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00001606 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00001607 DCHECK(out.IsRegister());
1608 if (in.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001609 __ Mov(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
Roland Levillain946e1432014-11-11 17:35:19 +00001610 } else if (in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001611 __ LoadFromOffset(kLoadWord, out.AsRegister<Register>(), SP, in.GetStackIndex());
Roland Levillain946e1432014-11-11 17:35:19 +00001612 } else {
1613 DCHECK(in.IsConstant());
1614 DCHECK(in.GetConstant()->IsLongConstant());
1615 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00001616 __ LoadImmediate(out.AsRegister<Register>(), static_cast<int32_t>(value));
Roland Levillain946e1432014-11-11 17:35:19 +00001617 }
1618 break;
1619
Roland Levillain3f8f9362014-12-02 17:45:01 +00001620 case Primitive::kPrimFloat: {
1621 // Processing a Dex `float-to-int' instruction.
1622 SRegister temp = locations->GetTemp(0).AsFpuRegisterPairLow<SRegister>();
1623 __ vmovs(temp, in.AsFpuRegister<SRegister>());
1624 __ vcvtis(temp, temp);
1625 __ vmovrs(out.AsRegister<Register>(), temp);
1626 break;
1627 }
1628
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001629 case Primitive::kPrimDouble: {
1630 // Processing a Dex `double-to-int' instruction.
1631 SRegister temp_s = locations->GetTemp(0).AsFpuRegisterPairLow<SRegister>();
1632 DRegister temp_d = FromLowSToD(temp_s);
1633 __ vmovd(temp_d, FromLowSToD(in.AsFpuRegisterPairLow<SRegister>()));
1634 __ vcvtid(temp_s, temp_d);
1635 __ vmovrs(out.AsRegister<Register>(), temp_s);
Roland Levillain946e1432014-11-11 17:35:19 +00001636 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001637 }
Roland Levillain946e1432014-11-11 17:35:19 +00001638
1639 default:
1640 LOG(FATAL) << "Unexpected type conversion from " << input_type
1641 << " to " << result_type;
1642 }
1643 break;
1644
Roland Levillaindff1f282014-11-05 14:15:05 +00001645 case Primitive::kPrimLong:
1646 switch (input_type) {
1647 case Primitive::kPrimByte:
1648 case Primitive::kPrimShort:
1649 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00001650 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001651 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00001652 DCHECK(out.IsRegisterPair());
1653 DCHECK(in.IsRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001654 __ Mov(out.AsRegisterPairLow<Register>(), in.AsRegister<Register>());
Roland Levillaindff1f282014-11-05 14:15:05 +00001655 // Sign extension.
1656 __ Asr(out.AsRegisterPairHigh<Register>(),
1657 out.AsRegisterPairLow<Register>(),
1658 31);
1659 break;
1660
1661 case Primitive::kPrimFloat:
Roland Levillain624279f2014-12-04 11:54:28 +00001662 // Processing a Dex `float-to-long' instruction.
Roland Levillain624279f2014-12-04 11:54:28 +00001663 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pF2l),
1664 conversion,
1665 conversion->GetDexPc());
1666 break;
1667
Roland Levillaindff1f282014-11-05 14:15:05 +00001668 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001669 // Processing a Dex `double-to-long' instruction.
1670 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pD2l),
1671 conversion,
1672 conversion->GetDexPc());
Roland Levillaindff1f282014-11-05 14:15:05 +00001673 break;
1674
1675 default:
1676 LOG(FATAL) << "Unexpected type conversion from " << input_type
1677 << " to " << result_type;
1678 }
1679 break;
1680
Roland Levillain981e4542014-11-14 11:47:14 +00001681 case Primitive::kPrimChar:
1682 switch (input_type) {
1683 case Primitive::kPrimByte:
1684 case Primitive::kPrimShort:
1685 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00001686 // Processing a Dex `int-to-char' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00001687 __ ubfx(out.AsRegister<Register>(), in.AsRegister<Register>(), 0, 16);
Roland Levillain981e4542014-11-14 11:47:14 +00001688 break;
1689
1690 default:
1691 LOG(FATAL) << "Unexpected type conversion from " << input_type
1692 << " to " << result_type;
1693 }
1694 break;
1695
Roland Levillaindff1f282014-11-05 14:15:05 +00001696 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00001697 switch (input_type) {
1698 case Primitive::kPrimByte:
1699 case Primitive::kPrimShort:
1700 case Primitive::kPrimInt:
1701 case Primitive::kPrimChar: {
1702 // Processing a Dex `int-to-float' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00001703 __ vmovsr(out.AsFpuRegister<SRegister>(), in.AsRegister<Register>());
1704 __ vcvtsi(out.AsFpuRegister<SRegister>(), out.AsFpuRegister<SRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00001705 break;
1706 }
1707
Roland Levillain6d0e4832014-11-27 18:31:21 +00001708 case Primitive::kPrimLong: {
1709 // Processing a Dex `long-to-float' instruction.
1710 Register low = in.AsRegisterPairLow<Register>();
1711 Register high = in.AsRegisterPairHigh<Register>();
1712 SRegister output = out.AsFpuRegister<SRegister>();
1713 Register constant_low = locations->GetTemp(0).AsRegister<Register>();
1714 Register constant_high = locations->GetTemp(1).AsRegister<Register>();
1715 SRegister temp1_s = locations->GetTemp(2).AsFpuRegisterPairLow<SRegister>();
1716 DRegister temp1_d = FromLowSToD(temp1_s);
1717 SRegister temp2_s = locations->GetTemp(3).AsFpuRegisterPairLow<SRegister>();
1718 DRegister temp2_d = FromLowSToD(temp2_s);
1719
1720 // Operations use doubles for precision reasons (each 32-bit
1721 // half of a long fits in the 53-bit mantissa of a double,
1722 // but not in the 24-bit mantissa of a float). This is
1723 // especially important for the low bits. The result is
1724 // eventually converted to float.
1725
1726 // temp1_d = int-to-double(high)
1727 __ vmovsr(temp1_s, high);
1728 __ vcvtdi(temp1_d, temp1_s);
1729 // Using vmovd to load the `k2Pow32EncodingForDouble` constant
1730 // as an immediate value into `temp2_d` does not work, as
1731 // this instruction only transfers 8 significant bits of its
1732 // immediate operand. Instead, use two 32-bit core
1733 // registers to load `k2Pow32EncodingForDouble` into
1734 // `temp2_d`.
1735 __ LoadImmediate(constant_low, Low32Bits(k2Pow32EncodingForDouble));
1736 __ LoadImmediate(constant_high, High32Bits(k2Pow32EncodingForDouble));
1737 __ vmovdrr(temp2_d, constant_low, constant_high);
1738 // temp1_d = temp1_d * 2^32
1739 __ vmuld(temp1_d, temp1_d, temp2_d);
1740 // temp2_d = unsigned-to-double(low)
1741 __ vmovsr(temp2_s, low);
1742 __ vcvtdu(temp2_d, temp2_s);
1743 // temp1_d = temp1_d + temp2_d
1744 __ vaddd(temp1_d, temp1_d, temp2_d);
1745 // output = double-to-float(temp1_d);
1746 __ vcvtsd(output, temp1_d);
1747 break;
1748 }
1749
Roland Levillaincff13742014-11-17 14:32:17 +00001750 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001751 // Processing a Dex `double-to-float' instruction.
1752 __ vcvtsd(out.AsFpuRegister<SRegister>(),
1753 FromLowSToD(in.AsFpuRegisterPairLow<SRegister>()));
Roland Levillaincff13742014-11-17 14:32:17 +00001754 break;
1755
1756 default:
1757 LOG(FATAL) << "Unexpected type conversion from " << input_type
1758 << " to " << result_type;
1759 };
1760 break;
1761
Roland Levillaindff1f282014-11-05 14:15:05 +00001762 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00001763 switch (input_type) {
1764 case Primitive::kPrimByte:
1765 case Primitive::kPrimShort:
1766 case Primitive::kPrimInt:
1767 case Primitive::kPrimChar: {
1768 // Processing a Dex `int-to-double' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00001769 __ vmovsr(out.AsFpuRegisterPairLow<SRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00001770 __ vcvtdi(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
1771 out.AsFpuRegisterPairLow<SRegister>());
1772 break;
1773 }
1774
Roland Levillain647b9ed2014-11-27 12:06:00 +00001775 case Primitive::kPrimLong: {
1776 // Processing a Dex `long-to-double' instruction.
1777 Register low = in.AsRegisterPairLow<Register>();
1778 Register high = in.AsRegisterPairHigh<Register>();
1779 SRegister out_s = out.AsFpuRegisterPairLow<SRegister>();
1780 DRegister out_d = FromLowSToD(out_s);
Roland Levillain271ab9c2014-11-27 15:23:57 +00001781 Register constant_low = locations->GetTemp(0).AsRegister<Register>();
1782 Register constant_high = locations->GetTemp(1).AsRegister<Register>();
Roland Levillain647b9ed2014-11-27 12:06:00 +00001783 SRegister temp_s = locations->GetTemp(2).AsFpuRegisterPairLow<SRegister>();
1784 DRegister temp_d = FromLowSToD(temp_s);
1785
Roland Levillain647b9ed2014-11-27 12:06:00 +00001786 // out_d = int-to-double(high)
1787 __ vmovsr(out_s, high);
1788 __ vcvtdi(out_d, out_s);
Roland Levillain6d0e4832014-11-27 18:31:21 +00001789 // Using vmovd to load the `k2Pow32EncodingForDouble` constant
1790 // as an immediate value into `temp_d` does not work, as
1791 // this instruction only transfers 8 significant bits of its
1792 // immediate operand. Instead, use two 32-bit core
1793 // registers to load `k2Pow32EncodingForDouble` into `temp_d`.
1794 __ LoadImmediate(constant_low, Low32Bits(k2Pow32EncodingForDouble));
1795 __ LoadImmediate(constant_high, High32Bits(k2Pow32EncodingForDouble));
Roland Levillain647b9ed2014-11-27 12:06:00 +00001796 __ vmovdrr(temp_d, constant_low, constant_high);
1797 // out_d = out_d * 2^32
1798 __ vmuld(out_d, out_d, temp_d);
1799 // temp_d = unsigned-to-double(low)
1800 __ vmovsr(temp_s, low);
1801 __ vcvtdu(temp_d, temp_s);
1802 // out_d = out_d + temp_d
1803 __ vaddd(out_d, out_d, temp_d);
1804 break;
1805 }
1806
Roland Levillaincff13742014-11-17 14:32:17 +00001807 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001808 // Processing a Dex `float-to-double' instruction.
1809 __ vcvtds(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
1810 in.AsFpuRegister<SRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00001811 break;
1812
1813 default:
1814 LOG(FATAL) << "Unexpected type conversion from " << input_type
1815 << " to " << result_type;
1816 };
Roland Levillaindff1f282014-11-05 14:15:05 +00001817 break;
1818
1819 default:
1820 LOG(FATAL) << "Unexpected type conversion from " << input_type
1821 << " to " << result_type;
1822 }
1823}
1824
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001825void LocationsBuilderARM::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001826 LocationSummary* locations =
1827 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001828 switch (add->GetResultType()) {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001829 case Primitive::kPrimInt:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001830 case Primitive::kPrimLong: {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01001831 bool output_overlaps = (add->GetResultType() == Primitive::kPrimLong);
1832 locations->SetInAt(0, Location::RequiresRegister());
1833 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
1834 locations->SetOut(Location::RequiresRegister(), output_overlaps);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001835 break;
1836 }
1837
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001838 case Primitive::kPrimFloat:
1839 case Primitive::kPrimDouble: {
1840 locations->SetInAt(0, Location::RequiresFpuRegister());
1841 locations->SetInAt(1, Location::RequiresFpuRegister());
Calin Juravle7c4954d2014-10-28 16:57:40 +00001842 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001843 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001844 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001845
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001846 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001847 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001848 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001849}
1850
1851void InstructionCodeGeneratorARM::VisitAdd(HAdd* add) {
1852 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001853 Location out = locations->Out();
1854 Location first = locations->InAt(0);
1855 Location second = locations->InAt(1);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001856 switch (add->GetResultType()) {
1857 case Primitive::kPrimInt:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001858 if (second.IsRegister()) {
Roland Levillain199f3362014-11-27 17:15:16 +00001859 __ add(out.AsRegister<Register>(),
1860 first.AsRegister<Register>(),
1861 ShifterOperand(second.AsRegister<Register>()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001862 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001863 __ AddConstant(out.AsRegister<Register>(),
1864 first.AsRegister<Register>(),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001865 second.GetConstant()->AsIntConstant()->GetValue());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001866 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001867 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001868
1869 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001870 __ adds(out.AsRegisterPairLow<Register>(),
1871 first.AsRegisterPairLow<Register>(),
1872 ShifterOperand(second.AsRegisterPairLow<Register>()));
1873 __ adc(out.AsRegisterPairHigh<Register>(),
1874 first.AsRegisterPairHigh<Register>(),
1875 ShifterOperand(second.AsRegisterPairHigh<Register>()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001876 break;
1877
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001878 case Primitive::kPrimFloat:
Roland Levillain199f3362014-11-27 17:15:16 +00001879 __ vadds(out.AsFpuRegister<SRegister>(),
1880 first.AsFpuRegister<SRegister>(),
1881 second.AsFpuRegister<SRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001882 break;
1883
1884 case Primitive::kPrimDouble:
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001885 __ vaddd(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
1886 FromLowSToD(first.AsFpuRegisterPairLow<SRegister>()),
1887 FromLowSToD(second.AsFpuRegisterPairLow<SRegister>()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001888 break;
1889
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001890 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001891 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001892 }
1893}
1894
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001895void LocationsBuilderARM::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001896 LocationSummary* locations =
1897 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001898 switch (sub->GetResultType()) {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001899 case Primitive::kPrimInt:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001900 case Primitive::kPrimLong: {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01001901 bool output_overlaps = (sub->GetResultType() == Primitive::kPrimLong);
1902 locations->SetInAt(0, Location::RequiresRegister());
1903 locations->SetInAt(1, Location::RegisterOrConstant(sub->InputAt(1)));
1904 locations->SetOut(Location::RequiresRegister(), output_overlaps);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001905 break;
1906 }
Calin Juravle11351682014-10-23 15:38:15 +01001907 case Primitive::kPrimFloat:
1908 case Primitive::kPrimDouble: {
1909 locations->SetInAt(0, Location::RequiresFpuRegister());
1910 locations->SetInAt(1, Location::RequiresFpuRegister());
Calin Juravle7c4954d2014-10-28 16:57:40 +00001911 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001912 break;
Calin Juravle11351682014-10-23 15:38:15 +01001913 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001914 default:
Calin Juravle11351682014-10-23 15:38:15 +01001915 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001916 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001917}
1918
1919void InstructionCodeGeneratorARM::VisitSub(HSub* sub) {
1920 LocationSummary* locations = sub->GetLocations();
Calin Juravle11351682014-10-23 15:38:15 +01001921 Location out = locations->Out();
1922 Location first = locations->InAt(0);
1923 Location second = locations->InAt(1);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001924 switch (sub->GetResultType()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001925 case Primitive::kPrimInt: {
Calin Juravle11351682014-10-23 15:38:15 +01001926 if (second.IsRegister()) {
Roland Levillain199f3362014-11-27 17:15:16 +00001927 __ sub(out.AsRegister<Register>(),
1928 first.AsRegister<Register>(),
1929 ShifterOperand(second.AsRegister<Register>()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001930 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001931 __ AddConstant(out.AsRegister<Register>(),
1932 first.AsRegister<Register>(),
Calin Juravle11351682014-10-23 15:38:15 +01001933 -second.GetConstant()->AsIntConstant()->GetValue());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001934 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001935 break;
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001936 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001937
Calin Juravle11351682014-10-23 15:38:15 +01001938 case Primitive::kPrimLong: {
1939 __ subs(out.AsRegisterPairLow<Register>(),
1940 first.AsRegisterPairLow<Register>(),
1941 ShifterOperand(second.AsRegisterPairLow<Register>()));
1942 __ sbc(out.AsRegisterPairHigh<Register>(),
1943 first.AsRegisterPairHigh<Register>(),
1944 ShifterOperand(second.AsRegisterPairHigh<Register>()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001945 break;
Calin Juravle11351682014-10-23 15:38:15 +01001946 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001947
Calin Juravle11351682014-10-23 15:38:15 +01001948 case Primitive::kPrimFloat: {
Roland Levillain199f3362014-11-27 17:15:16 +00001949 __ vsubs(out.AsFpuRegister<SRegister>(),
1950 first.AsFpuRegister<SRegister>(),
1951 second.AsFpuRegister<SRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001952 break;
Calin Juravle11351682014-10-23 15:38:15 +01001953 }
1954
1955 case Primitive::kPrimDouble: {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001956 __ vsubd(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
1957 FromLowSToD(first.AsFpuRegisterPairLow<SRegister>()),
1958 FromLowSToD(second.AsFpuRegisterPairLow<SRegister>()));
Calin Juravle11351682014-10-23 15:38:15 +01001959 break;
1960 }
1961
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001962
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001963 default:
Calin Juravle11351682014-10-23 15:38:15 +01001964 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001965 }
1966}
1967
Calin Juravle34bacdf2014-10-07 20:23:36 +01001968void LocationsBuilderARM::VisitMul(HMul* mul) {
1969 LocationSummary* locations =
1970 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
1971 switch (mul->GetResultType()) {
1972 case Primitive::kPrimInt:
1973 case Primitive::kPrimLong: {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01001974 locations->SetInAt(0, Location::RequiresRegister());
1975 locations->SetInAt(1, Location::RequiresRegister());
1976 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Calin Juravle34bacdf2014-10-07 20:23:36 +01001977 break;
1978 }
1979
Calin Juravleb5bfa962014-10-21 18:02:24 +01001980 case Primitive::kPrimFloat:
1981 case Primitive::kPrimDouble: {
1982 locations->SetInAt(0, Location::RequiresFpuRegister());
1983 locations->SetInAt(1, Location::RequiresFpuRegister());
Calin Juravle7c4954d2014-10-28 16:57:40 +00001984 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Calin Juravle34bacdf2014-10-07 20:23:36 +01001985 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01001986 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01001987
1988 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01001989 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01001990 }
1991}
1992
1993void InstructionCodeGeneratorARM::VisitMul(HMul* mul) {
1994 LocationSummary* locations = mul->GetLocations();
1995 Location out = locations->Out();
1996 Location first = locations->InAt(0);
1997 Location second = locations->InAt(1);
1998 switch (mul->GetResultType()) {
1999 case Primitive::kPrimInt: {
Roland Levillain199f3362014-11-27 17:15:16 +00002000 __ mul(out.AsRegister<Register>(),
2001 first.AsRegister<Register>(),
2002 second.AsRegister<Register>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002003 break;
2004 }
2005 case Primitive::kPrimLong: {
2006 Register out_hi = out.AsRegisterPairHigh<Register>();
2007 Register out_lo = out.AsRegisterPairLow<Register>();
2008 Register in1_hi = first.AsRegisterPairHigh<Register>();
2009 Register in1_lo = first.AsRegisterPairLow<Register>();
2010 Register in2_hi = second.AsRegisterPairHigh<Register>();
2011 Register in2_lo = second.AsRegisterPairLow<Register>();
2012
2013 // Extra checks to protect caused by the existence of R1_R2.
2014 // The algorithm is wrong if out.hi is either in1.lo or in2.lo:
2015 // (e.g. in1=r0_r1, in2=r2_r3 and out=r1_r2);
2016 DCHECK_NE(out_hi, in1_lo);
2017 DCHECK_NE(out_hi, in2_lo);
2018
2019 // input: in1 - 64 bits, in2 - 64 bits
2020 // output: out
2021 // formula: out.hi : out.lo = (in1.lo * in2.hi + in1.hi * in2.lo)* 2^32 + in1.lo * in2.lo
2022 // parts: out.hi = in1.lo * in2.hi + in1.hi * in2.lo + (in1.lo * in2.lo)[63:32]
2023 // parts: out.lo = (in1.lo * in2.lo)[31:0]
2024
2025 // IP <- in1.lo * in2.hi
2026 __ mul(IP, in1_lo, in2_hi);
2027 // out.hi <- in1.lo * in2.hi + in1.hi * in2.lo
2028 __ mla(out_hi, in1_hi, in2_lo, IP);
2029 // out.lo <- (in1.lo * in2.lo)[31:0];
2030 __ umull(out_lo, IP, in1_lo, in2_lo);
2031 // out.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
2032 __ add(out_hi, out_hi, ShifterOperand(IP));
2033 break;
2034 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01002035
2036 case Primitive::kPrimFloat: {
Roland Levillain199f3362014-11-27 17:15:16 +00002037 __ vmuls(out.AsFpuRegister<SRegister>(),
2038 first.AsFpuRegister<SRegister>(),
2039 second.AsFpuRegister<SRegister>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002040 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01002041 }
2042
2043 case Primitive::kPrimDouble: {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00002044 __ vmuld(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
2045 FromLowSToD(first.AsFpuRegisterPairLow<SRegister>()),
2046 FromLowSToD(second.AsFpuRegisterPairLow<SRegister>()));
Calin Juravleb5bfa962014-10-21 18:02:24 +01002047 break;
2048 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002049
2050 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01002051 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002052 }
2053}
2054
Calin Juravle7c4954d2014-10-28 16:57:40 +00002055void LocationsBuilderARM::VisitDiv(HDiv* div) {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002056 LocationSummary::CallKind call_kind = div->GetResultType() == Primitive::kPrimLong
2057 ? LocationSummary::kCall
2058 : LocationSummary::kNoCall;
2059 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(div, call_kind);
2060
Calin Juravle7c4954d2014-10-28 16:57:40 +00002061 switch (div->GetResultType()) {
Calin Juravled0d48522014-11-04 16:40:20 +00002062 case Primitive::kPrimInt: {
2063 locations->SetInAt(0, Location::RequiresRegister());
2064 locations->SetInAt(1, Location::RequiresRegister());
2065 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2066 break;
2067 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00002068 case Primitive::kPrimLong: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002069 InvokeRuntimeCallingConvention calling_convention;
2070 locations->SetInAt(0, Location::RegisterPairLocation(
2071 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
2072 locations->SetInAt(1, Location::RegisterPairLocation(
2073 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
2074 // The runtime helper puts the output in R0,R2.
2075 locations->SetOut(Location::RegisterPairLocation(R0, R2));
Calin Juravle7c4954d2014-10-28 16:57:40 +00002076 break;
2077 }
2078 case Primitive::kPrimFloat:
2079 case Primitive::kPrimDouble: {
2080 locations->SetInAt(0, Location::RequiresFpuRegister());
2081 locations->SetInAt(1, Location::RequiresFpuRegister());
2082 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2083 break;
2084 }
2085
2086 default:
2087 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2088 }
2089}
2090
2091void InstructionCodeGeneratorARM::VisitDiv(HDiv* div) {
2092 LocationSummary* locations = div->GetLocations();
2093 Location out = locations->Out();
2094 Location first = locations->InAt(0);
2095 Location second = locations->InAt(1);
2096
2097 switch (div->GetResultType()) {
Calin Juravled0d48522014-11-04 16:40:20 +00002098 case Primitive::kPrimInt: {
Roland Levillain199f3362014-11-27 17:15:16 +00002099 __ sdiv(out.AsRegister<Register>(),
2100 first.AsRegister<Register>(),
2101 second.AsRegister<Register>());
Calin Juravled0d48522014-11-04 16:40:20 +00002102 break;
2103 }
2104
Calin Juravle7c4954d2014-10-28 16:57:40 +00002105 case Primitive::kPrimLong: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002106 InvokeRuntimeCallingConvention calling_convention;
2107 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegisterPairLow<Register>());
2108 DCHECK_EQ(calling_convention.GetRegisterAt(1), first.AsRegisterPairHigh<Register>());
2109 DCHECK_EQ(calling_convention.GetRegisterAt(2), second.AsRegisterPairLow<Register>());
2110 DCHECK_EQ(calling_convention.GetRegisterAt(3), second.AsRegisterPairHigh<Register>());
2111 DCHECK_EQ(R0, out.AsRegisterPairLow<Register>());
2112 DCHECK_EQ(R2, out.AsRegisterPairHigh<Register>());
2113
2114 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pLdiv), div, div->GetDexPc());
Calin Juravle7c4954d2014-10-28 16:57:40 +00002115 break;
2116 }
2117
2118 case Primitive::kPrimFloat: {
Roland Levillain199f3362014-11-27 17:15:16 +00002119 __ vdivs(out.AsFpuRegister<SRegister>(),
2120 first.AsFpuRegister<SRegister>(),
2121 second.AsFpuRegister<SRegister>());
Calin Juravle7c4954d2014-10-28 16:57:40 +00002122 break;
2123 }
2124
2125 case Primitive::kPrimDouble: {
2126 __ vdivd(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
2127 FromLowSToD(first.AsFpuRegisterPairLow<SRegister>()),
2128 FromLowSToD(second.AsFpuRegisterPairLow<SRegister>()));
2129 break;
2130 }
2131
2132 default:
2133 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2134 }
2135}
2136
Calin Juravlebacfec32014-11-14 15:54:36 +00002137void LocationsBuilderARM::VisitRem(HRem* rem) {
Calin Juravled2ec87d2014-12-08 14:24:46 +00002138 Primitive::Type type = rem->GetResultType();
2139 LocationSummary::CallKind call_kind = type == Primitive::kPrimInt
2140 ? LocationSummary::kNoCall
2141 : LocationSummary::kCall;
Calin Juravlebacfec32014-11-14 15:54:36 +00002142 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
2143
Calin Juravled2ec87d2014-12-08 14:24:46 +00002144 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00002145 case Primitive::kPrimInt: {
2146 locations->SetInAt(0, Location::RequiresRegister());
2147 locations->SetInAt(1, Location::RequiresRegister());
2148 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2149 locations->AddTemp(Location::RequiresRegister());
2150 break;
2151 }
2152 case Primitive::kPrimLong: {
2153 InvokeRuntimeCallingConvention calling_convention;
2154 locations->SetInAt(0, Location::RegisterPairLocation(
2155 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
2156 locations->SetInAt(1, Location::RegisterPairLocation(
2157 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
2158 // The runtime helper puts the output in R2,R3.
2159 locations->SetOut(Location::RegisterPairLocation(R2, R3));
2160 break;
2161 }
Calin Juravled2ec87d2014-12-08 14:24:46 +00002162 case Primitive::kPrimFloat: {
2163 InvokeRuntimeCallingConvention calling_convention;
2164 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
2165 locations->SetInAt(1, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(1)));
2166 locations->SetOut(Location::FpuRegisterLocation(S0));
2167 break;
2168 }
2169
Calin Juravlebacfec32014-11-14 15:54:36 +00002170 case Primitive::kPrimDouble: {
Calin Juravled2ec87d2014-12-08 14:24:46 +00002171 InvokeRuntimeCallingConvention calling_convention;
2172 locations->SetInAt(0, Location::FpuRegisterPairLocation(
2173 calling_convention.GetFpuRegisterAt(0), calling_convention.GetFpuRegisterAt(1)));
2174 locations->SetInAt(1, Location::FpuRegisterPairLocation(
2175 calling_convention.GetFpuRegisterAt(2), calling_convention.GetFpuRegisterAt(3)));
2176 locations->SetOut(Location::Location::FpuRegisterPairLocation(S0, S1));
Calin Juravlebacfec32014-11-14 15:54:36 +00002177 break;
2178 }
2179
2180 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00002181 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00002182 }
2183}
2184
2185void InstructionCodeGeneratorARM::VisitRem(HRem* rem) {
2186 LocationSummary* locations = rem->GetLocations();
2187 Location out = locations->Out();
2188 Location first = locations->InAt(0);
2189 Location second = locations->InAt(1);
2190
Calin Juravled2ec87d2014-12-08 14:24:46 +00002191 Primitive::Type type = rem->GetResultType();
2192 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00002193 case Primitive::kPrimInt: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002194 Register reg1 = first.AsRegister<Register>();
2195 Register reg2 = second.AsRegister<Register>();
2196 Register temp = locations->GetTemp(0).AsRegister<Register>();
Calin Juravlebacfec32014-11-14 15:54:36 +00002197
2198 // temp = reg1 / reg2 (integer division)
2199 // temp = temp * reg2
2200 // dest = reg1 - temp
2201 __ sdiv(temp, reg1, reg2);
2202 __ mul(temp, temp, reg2);
Roland Levillain271ab9c2014-11-27 15:23:57 +00002203 __ sub(out.AsRegister<Register>(), reg1, ShifterOperand(temp));
Calin Juravlebacfec32014-11-14 15:54:36 +00002204 break;
2205 }
2206
2207 case Primitive::kPrimLong: {
Calin Juravlebacfec32014-11-14 15:54:36 +00002208 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pLmod), rem, rem->GetDexPc());
2209 break;
2210 }
2211
Calin Juravled2ec87d2014-12-08 14:24:46 +00002212 case Primitive::kPrimFloat: {
2213 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pFmodf), rem, rem->GetDexPc());
2214 break;
2215 }
2216
Calin Juravlebacfec32014-11-14 15:54:36 +00002217 case Primitive::kPrimDouble: {
Calin Juravled2ec87d2014-12-08 14:24:46 +00002218 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pFmod), rem, rem->GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00002219 break;
2220 }
2221
2222 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00002223 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00002224 }
2225}
2226
Calin Juravled0d48522014-11-04 16:40:20 +00002227void LocationsBuilderARM::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2228 LocationSummary* locations =
2229 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002230 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Calin Juravled0d48522014-11-04 16:40:20 +00002231 if (instruction->HasUses()) {
2232 locations->SetOut(Location::SameAsFirstInput());
2233 }
2234}
2235
2236void InstructionCodeGeneratorARM::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2237 SlowPathCodeARM* slow_path = new (GetGraph()->GetArena()) DivZeroCheckSlowPathARM(instruction);
2238 codegen_->AddSlowPath(slow_path);
2239
2240 LocationSummary* locations = instruction->GetLocations();
2241 Location value = locations->InAt(0);
2242
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002243 switch (instruction->GetType()) {
2244 case Primitive::kPrimInt: {
2245 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002246 __ cmp(value.AsRegister<Register>(), ShifterOperand(0));
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002247 __ b(slow_path->GetEntryLabel(), EQ);
2248 } else {
2249 DCHECK(value.IsConstant()) << value;
2250 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
2251 __ b(slow_path->GetEntryLabel());
2252 }
2253 }
2254 break;
2255 }
2256 case Primitive::kPrimLong: {
2257 if (value.IsRegisterPair()) {
2258 __ orrs(IP,
2259 value.AsRegisterPairLow<Register>(),
2260 ShifterOperand(value.AsRegisterPairHigh<Register>()));
2261 __ b(slow_path->GetEntryLabel(), EQ);
2262 } else {
2263 DCHECK(value.IsConstant()) << value;
2264 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
2265 __ b(slow_path->GetEntryLabel());
2266 }
2267 }
2268 break;
2269 default:
2270 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
2271 }
2272 }
Calin Juravled0d48522014-11-04 16:40:20 +00002273}
2274
Calin Juravle9aec02f2014-11-18 23:06:35 +00002275void LocationsBuilderARM::HandleShift(HBinaryOperation* op) {
2276 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
2277
2278 LocationSummary::CallKind call_kind = op->GetResultType() == Primitive::kPrimLong
2279 ? LocationSummary::kCall
2280 : LocationSummary::kNoCall;
2281 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(op, call_kind);
2282
2283 switch (op->GetResultType()) {
2284 case Primitive::kPrimInt: {
2285 locations->SetInAt(0, Location::RequiresRegister());
2286 locations->SetInAt(1, Location::RegisterOrConstant(op->InputAt(1)));
2287 locations->SetOut(Location::RequiresRegister());
2288 break;
2289 }
2290 case Primitive::kPrimLong: {
2291 InvokeRuntimeCallingConvention calling_convention;
2292 locations->SetInAt(0, Location::RegisterPairLocation(
2293 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
2294 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
2295 // The runtime helper puts the output in R0,R2.
2296 locations->SetOut(Location::RegisterPairLocation(R0, R2));
2297 break;
2298 }
2299 default:
2300 LOG(FATAL) << "Unexpected operation type " << op->GetResultType();
2301 }
2302}
2303
2304void InstructionCodeGeneratorARM::HandleShift(HBinaryOperation* op) {
2305 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
2306
2307 LocationSummary* locations = op->GetLocations();
2308 Location out = locations->Out();
2309 Location first = locations->InAt(0);
2310 Location second = locations->InAt(1);
2311
2312 Primitive::Type type = op->GetResultType();
2313 switch (type) {
2314 case Primitive::kPrimInt: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002315 Register out_reg = out.AsRegister<Register>();
2316 Register first_reg = first.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00002317 // Arm doesn't mask the shift count so we need to do it ourselves.
2318 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002319 Register second_reg = second.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00002320 __ and_(second_reg, second_reg, ShifterOperand(kMaxIntShiftValue));
2321 if (op->IsShl()) {
2322 __ Lsl(out_reg, first_reg, second_reg);
2323 } else if (op->IsShr()) {
2324 __ Asr(out_reg, first_reg, second_reg);
2325 } else {
2326 __ Lsr(out_reg, first_reg, second_reg);
2327 }
2328 } else {
2329 int32_t cst = second.GetConstant()->AsIntConstant()->GetValue();
2330 uint32_t shift_value = static_cast<uint32_t>(cst & kMaxIntShiftValue);
2331 if (shift_value == 0) { // arm does not support shifting with 0 immediate.
2332 __ Mov(out_reg, first_reg);
2333 } else if (op->IsShl()) {
2334 __ Lsl(out_reg, first_reg, shift_value);
2335 } else if (op->IsShr()) {
2336 __ Asr(out_reg, first_reg, shift_value);
2337 } else {
2338 __ Lsr(out_reg, first_reg, shift_value);
2339 }
2340 }
2341 break;
2342 }
2343 case Primitive::kPrimLong: {
2344 // TODO: Inline the assembly instead of calling the runtime.
2345 InvokeRuntimeCallingConvention calling_convention;
2346 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegisterPairLow<Register>());
2347 DCHECK_EQ(calling_convention.GetRegisterAt(1), first.AsRegisterPairHigh<Register>());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002348 DCHECK_EQ(calling_convention.GetRegisterAt(2), second.AsRegister<Register>());
Calin Juravle9aec02f2014-11-18 23:06:35 +00002349 DCHECK_EQ(R0, out.AsRegisterPairLow<Register>());
2350 DCHECK_EQ(R2, out.AsRegisterPairHigh<Register>());
2351
2352 int32_t entry_point_offset;
2353 if (op->IsShl()) {
2354 entry_point_offset = QUICK_ENTRY_POINT(pShlLong);
2355 } else if (op->IsShr()) {
2356 entry_point_offset = QUICK_ENTRY_POINT(pShrLong);
2357 } else {
2358 entry_point_offset = QUICK_ENTRY_POINT(pUshrLong);
2359 }
2360 __ LoadFromOffset(kLoadWord, LR, TR, entry_point_offset);
2361 __ blx(LR);
2362 break;
2363 }
2364 default:
2365 LOG(FATAL) << "Unexpected operation type " << type;
2366 }
2367}
2368
2369void LocationsBuilderARM::VisitShl(HShl* shl) {
2370 HandleShift(shl);
2371}
2372
2373void InstructionCodeGeneratorARM::VisitShl(HShl* shl) {
2374 HandleShift(shl);
2375}
2376
2377void LocationsBuilderARM::VisitShr(HShr* shr) {
2378 HandleShift(shr);
2379}
2380
2381void InstructionCodeGeneratorARM::VisitShr(HShr* shr) {
2382 HandleShift(shr);
2383}
2384
2385void LocationsBuilderARM::VisitUShr(HUShr* ushr) {
2386 HandleShift(ushr);
2387}
2388
2389void InstructionCodeGeneratorARM::VisitUShr(HUShr* ushr) {
2390 HandleShift(ushr);
2391}
2392
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002393void LocationsBuilderARM::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002394 LocationSummary* locations =
2395 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002396 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002397 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2398 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
2399 locations->SetOut(Location::RegisterLocation(R0));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002400}
2401
2402void InstructionCodeGeneratorARM::VisitNewInstance(HNewInstance* instruction) {
2403 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002404 codegen_->LoadCurrentMethod(calling_convention.GetRegisterAt(1));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002405 __ LoadImmediate(calling_convention.GetRegisterAt(0), instruction->GetTypeIndex());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002406 codegen_->InvokeRuntime(
2407 QUICK_ENTRY_POINT(pAllocObjectWithAccessCheck), instruction, instruction->GetDexPc());
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002408}
2409
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002410void LocationsBuilderARM::VisitNewArray(HNewArray* instruction) {
2411 LocationSummary* locations =
2412 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
2413 InvokeRuntimeCallingConvention calling_convention;
2414 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2415 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
2416 locations->SetOut(Location::RegisterLocation(R0));
2417 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
2418}
2419
2420void InstructionCodeGeneratorARM::VisitNewArray(HNewArray* instruction) {
2421 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002422 codegen_->LoadCurrentMethod(calling_convention.GetRegisterAt(1));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002423 __ LoadImmediate(calling_convention.GetRegisterAt(0), instruction->GetTypeIndex());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002424 codegen_->InvokeRuntime(
2425 QUICK_ENTRY_POINT(pAllocArrayWithAccessCheck), instruction, instruction->GetDexPc());
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002426}
2427
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002428void LocationsBuilderARM::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002429 LocationSummary* locations =
2430 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya747a392014-04-17 14:56:23 +01002431 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
2432 if (location.IsStackSlot()) {
2433 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
2434 } else if (location.IsDoubleStackSlot()) {
2435 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002436 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01002437 locations->SetOut(location);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002438}
2439
2440void InstructionCodeGeneratorARM::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002441 // Nothing to do, the parameter is already at its location.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002442 UNUSED(instruction);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002443}
2444
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002445void LocationsBuilderARM::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002446 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002447 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002448 locations->SetInAt(0, Location::RequiresRegister());
2449 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01002450}
2451
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002452void InstructionCodeGeneratorARM::VisitNot(HNot* not_) {
2453 LocationSummary* locations = not_->GetLocations();
2454 Location out = locations->Out();
2455 Location in = locations->InAt(0);
2456 switch (not_->InputAt(0)->GetType()) {
2457 case Primitive::kPrimBoolean:
Roland Levillain271ab9c2014-11-27 15:23:57 +00002458 __ eor(out.AsRegister<Register>(), in.AsRegister<Register>(), ShifterOperand(1));
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002459 break;
2460
2461 case Primitive::kPrimInt:
Roland Levillain271ab9c2014-11-27 15:23:57 +00002462 __ mvn(out.AsRegister<Register>(), ShifterOperand(in.AsRegister<Register>()));
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002463 break;
2464
2465 case Primitive::kPrimLong:
Roland Levillain70566432014-10-24 16:20:17 +01002466 __ mvn(out.AsRegisterPairLow<Register>(),
2467 ShifterOperand(in.AsRegisterPairLow<Register>()));
2468 __ mvn(out.AsRegisterPairHigh<Register>(),
2469 ShifterOperand(in.AsRegisterPairHigh<Register>()));
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002470 break;
2471
2472 default:
2473 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
2474 }
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01002475}
2476
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002477void LocationsBuilderARM::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002478 LocationSummary* locations =
2479 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +00002480 switch (compare->InputAt(0)->GetType()) {
2481 case Primitive::kPrimLong: {
2482 locations->SetInAt(0, Location::RequiresRegister());
2483 locations->SetInAt(1, Location::RequiresRegister());
2484 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2485 break;
2486 }
2487 case Primitive::kPrimFloat:
2488 case Primitive::kPrimDouble: {
2489 locations->SetInAt(0, Location::RequiresFpuRegister());
2490 locations->SetInAt(1, Location::RequiresFpuRegister());
2491 locations->SetOut(Location::RequiresRegister());
2492 break;
2493 }
2494 default:
2495 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
2496 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002497}
2498
2499void InstructionCodeGeneratorARM::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002500 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002501 Register out = locations->Out().AsRegister<Register>();
Calin Juravleddb7df22014-11-25 20:56:51 +00002502 Location left = locations->InAt(0);
2503 Location right = locations->InAt(1);
2504
2505 Label less, greater, done;
2506 Primitive::Type type = compare->InputAt(0)->GetType();
2507 switch (type) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002508 case Primitive::kPrimLong: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002509 __ cmp(left.AsRegisterPairHigh<Register>(),
2510 ShifterOperand(right.AsRegisterPairHigh<Register>())); // Signed compare.
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002511 __ b(&less, LT);
2512 __ b(&greater, GT);
Calin Juravleddb7df22014-11-25 20:56:51 +00002513 // Do LoadImmediate before any `cmp`, as LoadImmediate might affect the status flags.
2514 __ LoadImmediate(out, 0);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002515 __ cmp(left.AsRegisterPairLow<Register>(),
2516 ShifterOperand(right.AsRegisterPairLow<Register>())); // Unsigned compare.
Calin Juravleddb7df22014-11-25 20:56:51 +00002517 break;
2518 }
2519 case Primitive::kPrimFloat:
2520 case Primitive::kPrimDouble: {
2521 __ LoadImmediate(out, 0);
2522 if (type == Primitive::kPrimFloat) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002523 __ vcmps(left.AsFpuRegister<SRegister>(), right.AsFpuRegister<SRegister>());
Calin Juravleddb7df22014-11-25 20:56:51 +00002524 } else {
2525 __ vcmpd(FromLowSToD(left.AsFpuRegisterPairLow<SRegister>()),
2526 FromLowSToD(right.AsFpuRegisterPairLow<SRegister>()));
2527 }
2528 __ vmstat(); // transfer FP status register to ARM APSR.
2529 __ b(compare->IsGtBias() ? &greater : &less, VS); // VS for unordered.
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002530 break;
2531 }
2532 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00002533 LOG(FATAL) << "Unexpected compare type " << type;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002534 }
Calin Juravleddb7df22014-11-25 20:56:51 +00002535 __ b(&done, EQ);
2536 __ b(&less, CC); // CC is for both: unsigned compare for longs and 'less than' for floats.
2537
2538 __ Bind(&greater);
2539 __ LoadImmediate(out, 1);
2540 __ b(&done);
2541
2542 __ Bind(&less);
2543 __ LoadImmediate(out, -1);
2544
2545 __ Bind(&done);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002546}
2547
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002548void LocationsBuilderARM::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002549 LocationSummary* locations =
2550 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01002551 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
2552 locations->SetInAt(i, Location::Any());
2553 }
2554 locations->SetOut(Location::Any());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002555}
2556
2557void InstructionCodeGeneratorARM::VisitPhi(HPhi* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002558 UNUSED(instruction);
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01002559 LOG(FATAL) << "Unreachable";
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002560}
2561
Calin Juravle52c48962014-12-16 17:02:57 +00002562void InstructionCodeGeneratorARM::GenerateMemoryBarrier(MemBarrierKind kind) {
2563 // TODO (ported from quick): revisit Arm barrier kinds
2564 DmbOptions flavour = DmbOptions::ISH; // quiet c++ warnings
2565 switch (kind) {
2566 case MemBarrierKind::kAnyStore:
2567 case MemBarrierKind::kLoadAny:
2568 case MemBarrierKind::kAnyAny: {
2569 flavour = DmbOptions::ISH;
2570 break;
2571 }
2572 case MemBarrierKind::kStoreStore: {
2573 flavour = DmbOptions::ISHST;
2574 break;
2575 }
2576 default:
2577 LOG(FATAL) << "Unexpected memory barrier " << kind;
2578 }
2579 __ dmb(flavour);
2580}
2581
2582void InstructionCodeGeneratorARM::GenerateWideAtomicLoad(Register addr,
2583 uint32_t offset,
2584 Register out_lo,
2585 Register out_hi) {
2586 if (offset != 0) {
2587 __ LoadImmediate(out_lo, offset);
2588 __ add(addr, addr, ShifterOperand(out_lo));
2589 }
2590 __ ldrexd(out_lo, out_hi, addr);
2591}
2592
2593void InstructionCodeGeneratorARM::GenerateWideAtomicStore(Register addr,
2594 uint32_t offset,
2595 Register value_lo,
2596 Register value_hi,
2597 Register temp1,
2598 Register temp2) {
2599 Label fail;
2600 if (offset != 0) {
2601 __ LoadImmediate(temp1, offset);
2602 __ add(addr, addr, ShifterOperand(temp1));
2603 }
2604 __ Bind(&fail);
2605 // We need a load followed by store. (The address used in a STREX instruction must
2606 // be the same as the address in the most recently executed LDREX instruction.)
2607 __ ldrexd(temp1, temp2, addr);
2608 __ strexd(temp1, value_lo, value_hi, addr);
2609 __ cmp(temp1, ShifterOperand(0));
2610 __ b(&fail, NE);
2611}
2612
2613void LocationsBuilderARM::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
2614 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
2615
Nicolas Geoffray39468442014-09-02 15:17:15 +01002616 LocationSummary* locations =
2617 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002618 locations->SetInAt(0, Location::RequiresRegister());
2619 locations->SetInAt(1, Location::RequiresRegister());
Calin Juravle52c48962014-12-16 17:02:57 +00002620
Calin Juravle34166012014-12-19 17:22:29 +00002621
Calin Juravle52c48962014-12-16 17:02:57 +00002622 Primitive::Type field_type = field_info.GetFieldType();
2623 bool is_wide = field_type == Primitive::kPrimLong || field_type == Primitive::kPrimDouble;
Calin Juravle34166012014-12-19 17:22:29 +00002624 bool generate_volatile = field_info.IsVolatile()
2625 && is_wide
2626 && !codegen_->GetInstructionSetFeatures()->HasAtomicLdrdAndStrd();
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01002627 // Temporary registers for the write barrier.
Calin Juravle52c48962014-12-16 17:02:57 +00002628 // TODO: consider renaming StoreNeedsWriteBarrier to StoreNeedsGCMark.
2629 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01002630 locations->AddTemp(Location::RequiresRegister());
2631 locations->AddTemp(Location::RequiresRegister());
Calin Juravle34166012014-12-19 17:22:29 +00002632 } else if (generate_volatile) {
Calin Juravle52c48962014-12-16 17:02:57 +00002633 // Arm encoding have some additional constraints for ldrexd/strexd:
2634 // - registers need to be consecutive
2635 // - the first register should be even but not R14.
2636 // We don't test for Arm yet, and the assertion makes sure that we revisit this if we ever
2637 // enable Arm encoding.
2638 DCHECK_EQ(InstructionSet::kThumb2, codegen_->GetInstructionSet());
2639
2640 locations->AddTemp(Location::RequiresRegister());
2641 locations->AddTemp(Location::RequiresRegister());
2642 if (field_type == Primitive::kPrimDouble) {
2643 // For doubles we need two more registers to copy the value.
2644 locations->AddTemp(Location::RegisterLocation(R2));
2645 locations->AddTemp(Location::RegisterLocation(R3));
2646 }
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01002647 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002648}
2649
Calin Juravle52c48962014-12-16 17:02:57 +00002650void InstructionCodeGeneratorARM::HandleFieldSet(HInstruction* instruction,
2651 const FieldInfo& field_info) {
2652 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
2653
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002654 LocationSummary* locations = instruction->GetLocations();
Calin Juravle52c48962014-12-16 17:02:57 +00002655 Register base = locations->InAt(0).AsRegister<Register>();
2656 Location value = locations->InAt(1);
2657
2658 bool is_volatile = field_info.IsVolatile();
Calin Juravle34166012014-12-19 17:22:29 +00002659 bool atomic_ldrd_strd = codegen_->GetInstructionSetFeatures()->HasAtomicLdrdAndStrd();
Calin Juravle52c48962014-12-16 17:02:57 +00002660 Primitive::Type field_type = field_info.GetFieldType();
2661 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
2662
2663 if (is_volatile) {
2664 GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
2665 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002666
2667 switch (field_type) {
2668 case Primitive::kPrimBoolean:
2669 case Primitive::kPrimByte: {
Calin Juravle52c48962014-12-16 17:02:57 +00002670 __ StoreToOffset(kStoreByte, value.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002671 break;
2672 }
2673
2674 case Primitive::kPrimShort:
2675 case Primitive::kPrimChar: {
Calin Juravle52c48962014-12-16 17:02:57 +00002676 __ StoreToOffset(kStoreHalfword, value.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002677 break;
2678 }
2679
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002680 case Primitive::kPrimInt:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002681 case Primitive::kPrimNot: {
Calin Juravle52c48962014-12-16 17:02:57 +00002682 Register value_reg = value.AsRegister<Register>();
2683 __ StoreToOffset(kStoreWord, value_reg, base, offset);
2684 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002685 Register temp = locations->GetTemp(0).AsRegister<Register>();
2686 Register card = locations->GetTemp(1).AsRegister<Register>();
Calin Juravle52c48962014-12-16 17:02:57 +00002687 codegen_->MarkGCCard(temp, card, base, value_reg);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002688 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002689 break;
2690 }
2691
2692 case Primitive::kPrimLong: {
Calin Juravle34166012014-12-19 17:22:29 +00002693 if (is_volatile && !atomic_ldrd_strd) {
Calin Juravle52c48962014-12-16 17:02:57 +00002694 GenerateWideAtomicStore(base, offset,
2695 value.AsRegisterPairLow<Register>(),
2696 value.AsRegisterPairHigh<Register>(),
2697 locations->GetTemp(0).AsRegister<Register>(),
2698 locations->GetTemp(1).AsRegister<Register>());
2699 } else {
2700 __ StoreToOffset(kStoreWordPair, value.AsRegisterPairLow<Register>(), base, offset);
2701 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002702 break;
2703 }
2704
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002705 case Primitive::kPrimFloat: {
Calin Juravle52c48962014-12-16 17:02:57 +00002706 __ StoreSToOffset(value.AsFpuRegister<SRegister>(), base, offset);
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002707 break;
2708 }
2709
2710 case Primitive::kPrimDouble: {
Calin Juravle52c48962014-12-16 17:02:57 +00002711 DRegister value_reg = FromLowSToD(value.AsFpuRegisterPairLow<SRegister>());
Calin Juravle34166012014-12-19 17:22:29 +00002712 if (is_volatile && !atomic_ldrd_strd) {
Calin Juravle52c48962014-12-16 17:02:57 +00002713 Register value_reg_lo = locations->GetTemp(0).AsRegister<Register>();
2714 Register value_reg_hi = locations->GetTemp(1).AsRegister<Register>();
2715
2716 __ vmovrrd(value_reg_lo, value_reg_hi, value_reg);
2717
2718 GenerateWideAtomicStore(base, offset,
2719 value_reg_lo,
2720 value_reg_hi,
2721 locations->GetTemp(2).AsRegister<Register>(),
2722 locations->GetTemp(3).AsRegister<Register>());
2723 } else {
2724 __ StoreDToOffset(value_reg, base, offset);
2725 }
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002726 break;
2727 }
2728
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002729 case Primitive::kPrimVoid:
2730 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07002731 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002732 }
Calin Juravle52c48962014-12-16 17:02:57 +00002733
2734 if (is_volatile) {
2735 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
2736 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002737}
2738
Calin Juravle52c48962014-12-16 17:02:57 +00002739void LocationsBuilderARM::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
2740 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Nicolas Geoffray39468442014-09-02 15:17:15 +01002741 LocationSummary* locations =
2742 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002743 locations->SetInAt(0, Location::RequiresRegister());
2744 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Calin Juravle52c48962014-12-16 17:02:57 +00002745
Calin Juravle34166012014-12-19 17:22:29 +00002746 bool generate_volatile = field_info.IsVolatile()
2747 && (field_info.GetFieldType() == Primitive::kPrimDouble)
2748 && !codegen_->GetInstructionSetFeatures()->HasAtomicLdrdAndStrd();
2749 if (generate_volatile) {
Calin Juravle52c48962014-12-16 17:02:57 +00002750 // Arm encoding have some additional constraints for ldrexd/strexd:
2751 // - registers need to be consecutive
2752 // - the first register should be even but not R14.
2753 // We don't test for Arm yet, and the assertion makes sure that we revisit this if we ever
2754 // enable Arm encoding.
2755 DCHECK_EQ(InstructionSet::kThumb2, codegen_->GetInstructionSet());
2756 locations->AddTemp(Location::RequiresRegister());
2757 locations->AddTemp(Location::RequiresRegister());
2758 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002759}
2760
Calin Juravle52c48962014-12-16 17:02:57 +00002761void InstructionCodeGeneratorARM::HandleFieldGet(HInstruction* instruction,
2762 const FieldInfo& field_info) {
2763 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002764
Calin Juravle52c48962014-12-16 17:02:57 +00002765 LocationSummary* locations = instruction->GetLocations();
2766 Register base = locations->InAt(0).AsRegister<Register>();
2767 Location out = locations->Out();
2768 bool is_volatile = field_info.IsVolatile();
Calin Juravle34166012014-12-19 17:22:29 +00002769 bool atomic_ldrd_strd = codegen_->GetInstructionSetFeatures()->HasAtomicLdrdAndStrd();
Calin Juravle52c48962014-12-16 17:02:57 +00002770 Primitive::Type field_type = field_info.GetFieldType();
2771 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
2772
2773 switch (field_type) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002774 case Primitive::kPrimBoolean: {
Calin Juravle52c48962014-12-16 17:02:57 +00002775 __ LoadFromOffset(kLoadUnsignedByte, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002776 break;
2777 }
2778
2779 case Primitive::kPrimByte: {
Calin Juravle52c48962014-12-16 17:02:57 +00002780 __ LoadFromOffset(kLoadSignedByte, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002781 break;
2782 }
2783
2784 case Primitive::kPrimShort: {
Calin Juravle52c48962014-12-16 17:02:57 +00002785 __ LoadFromOffset(kLoadSignedHalfword, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002786 break;
2787 }
2788
2789 case Primitive::kPrimChar: {
Calin Juravle52c48962014-12-16 17:02:57 +00002790 __ LoadFromOffset(kLoadUnsignedHalfword, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002791 break;
2792 }
2793
2794 case Primitive::kPrimInt:
2795 case Primitive::kPrimNot: {
Calin Juravle52c48962014-12-16 17:02:57 +00002796 __ LoadFromOffset(kLoadWord, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002797 break;
2798 }
2799
2800 case Primitive::kPrimLong: {
Calin Juravle34166012014-12-19 17:22:29 +00002801 if (is_volatile && !atomic_ldrd_strd) {
Calin Juravle52c48962014-12-16 17:02:57 +00002802 GenerateWideAtomicLoad(base, offset,
2803 out.AsRegisterPairLow<Register>(),
2804 out.AsRegisterPairHigh<Register>());
2805 } else {
2806 __ LoadFromOffset(kLoadWordPair, out.AsRegisterPairLow<Register>(), base, offset);
2807 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002808 break;
2809 }
2810
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002811 case Primitive::kPrimFloat: {
Calin Juravle52c48962014-12-16 17:02:57 +00002812 __ LoadSFromOffset(out.AsFpuRegister<SRegister>(), base, offset);
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002813 break;
2814 }
2815
2816 case Primitive::kPrimDouble: {
Calin Juravle52c48962014-12-16 17:02:57 +00002817 DRegister out_reg = FromLowSToD(out.AsFpuRegisterPairLow<SRegister>());
Calin Juravle34166012014-12-19 17:22:29 +00002818 if (is_volatile && !atomic_ldrd_strd) {
Calin Juravle52c48962014-12-16 17:02:57 +00002819 Register lo = locations->GetTemp(0).AsRegister<Register>();
2820 Register hi = locations->GetTemp(1).AsRegister<Register>();
2821 GenerateWideAtomicLoad(base, offset, lo, hi);
2822 __ vmovdrr(out_reg, lo, hi);
2823 } else {
2824 __ LoadDFromOffset(out_reg, base, offset);
2825 }
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002826 break;
2827 }
2828
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002829 case Primitive::kPrimVoid:
Calin Juravle52c48962014-12-16 17:02:57 +00002830 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07002831 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002832 }
Calin Juravle52c48962014-12-16 17:02:57 +00002833
2834 if (is_volatile) {
2835 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
2836 }
2837}
2838
2839void LocationsBuilderARM::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
2840 HandleFieldSet(instruction, instruction->GetFieldInfo());
2841}
2842
2843void InstructionCodeGeneratorARM::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
2844 HandleFieldSet(instruction, instruction->GetFieldInfo());
2845}
2846
2847void LocationsBuilderARM::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
2848 HandleFieldGet(instruction, instruction->GetFieldInfo());
2849}
2850
2851void InstructionCodeGeneratorARM::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
2852 HandleFieldGet(instruction, instruction->GetFieldInfo());
2853}
2854
2855void LocationsBuilderARM::VisitStaticFieldGet(HStaticFieldGet* instruction) {
2856 HandleFieldGet(instruction, instruction->GetFieldInfo());
2857}
2858
2859void InstructionCodeGeneratorARM::VisitStaticFieldGet(HStaticFieldGet* instruction) {
2860 HandleFieldGet(instruction, instruction->GetFieldInfo());
2861}
2862
2863void LocationsBuilderARM::VisitStaticFieldSet(HStaticFieldSet* instruction) {
2864 HandleFieldSet(instruction, instruction->GetFieldInfo());
2865}
2866
2867void InstructionCodeGeneratorARM::VisitStaticFieldSet(HStaticFieldSet* instruction) {
2868 HandleFieldSet(instruction, instruction->GetFieldInfo());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002869}
2870
2871void LocationsBuilderARM::VisitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002872 LocationSummary* locations =
2873 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002874 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002875 if (instruction->HasUses()) {
2876 locations->SetOut(Location::SameAsFirstInput());
2877 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002878}
2879
2880void InstructionCodeGeneratorARM::VisitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01002881 SlowPathCodeARM* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathARM(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002882 codegen_->AddSlowPath(slow_path);
2883
2884 LocationSummary* locations = instruction->GetLocations();
2885 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002886
2887 if (obj.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002888 __ cmp(obj.AsRegister<Register>(), ShifterOperand(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002889 __ b(slow_path->GetEntryLabel(), EQ);
2890 } else {
2891 DCHECK(obj.IsConstant()) << obj;
2892 DCHECK_EQ(obj.GetConstant()->AsIntConstant()->GetValue(), 0);
2893 __ b(slow_path->GetEntryLabel());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002894 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002895}
2896
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002897void LocationsBuilderARM::VisitArrayGet(HArrayGet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002898 LocationSummary* locations =
2899 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002900 locations->SetInAt(0, Location::RequiresRegister());
2901 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
2902 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002903}
2904
2905void InstructionCodeGeneratorARM::VisitArrayGet(HArrayGet* instruction) {
2906 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002907 Register obj = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002908 Location index = locations->InAt(1);
2909
2910 switch (instruction->GetType()) {
2911 case Primitive::kPrimBoolean: {
2912 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002913 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002914 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00002915 size_t offset =
2916 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002917 __ LoadFromOffset(kLoadUnsignedByte, out, obj, offset);
2918 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002919 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002920 __ LoadFromOffset(kLoadUnsignedByte, out, IP, data_offset);
2921 }
2922 break;
2923 }
2924
2925 case Primitive::kPrimByte: {
2926 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002927 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002928 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00002929 size_t offset =
2930 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002931 __ LoadFromOffset(kLoadSignedByte, out, obj, offset);
2932 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002933 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002934 __ LoadFromOffset(kLoadSignedByte, out, IP, data_offset);
2935 }
2936 break;
2937 }
2938
2939 case Primitive::kPrimShort: {
2940 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002941 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002942 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00002943 size_t offset =
2944 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002945 __ LoadFromOffset(kLoadSignedHalfword, out, obj, offset);
2946 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002947 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_2));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002948 __ LoadFromOffset(kLoadSignedHalfword, out, IP, data_offset);
2949 }
2950 break;
2951 }
2952
2953 case Primitive::kPrimChar: {
2954 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002955 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002956 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00002957 size_t offset =
2958 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002959 __ LoadFromOffset(kLoadUnsignedHalfword, out, obj, offset);
2960 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002961 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_2));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002962 __ LoadFromOffset(kLoadUnsignedHalfword, out, IP, data_offset);
2963 }
2964 break;
2965 }
2966
2967 case Primitive::kPrimInt:
2968 case Primitive::kPrimNot: {
2969 DCHECK_EQ(sizeof(mirror::HeapReference<mirror::Object>), sizeof(int32_t));
2970 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002971 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002972 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00002973 size_t offset =
2974 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002975 __ LoadFromOffset(kLoadWord, out, obj, offset);
2976 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002977 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002978 __ LoadFromOffset(kLoadWord, out, IP, data_offset);
2979 }
2980 break;
2981 }
2982
2983 case Primitive::kPrimLong: {
2984 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002985 Location out = locations->Out();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002986 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00002987 size_t offset =
2988 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002989 __ LoadFromOffset(kLoadWordPair, out.AsRegisterPairLow<Register>(), obj, offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002990 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002991 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_8));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002992 __ LoadFromOffset(kLoadWordPair, out.AsRegisterPairLow<Register>(), IP, data_offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002993 }
2994 break;
2995 }
2996
2997 case Primitive::kPrimFloat:
2998 case Primitive::kPrimDouble:
2999 LOG(FATAL) << "Unimplemented register type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07003000 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003001 case Primitive::kPrimVoid:
3002 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07003003 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003004 }
3005}
3006
3007void LocationsBuilderARM::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003008 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003009
3010 bool needs_write_barrier =
3011 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
3012 bool needs_runtime_call = instruction->NeedsTypeCheck();
3013
Nicolas Geoffray39468442014-09-02 15:17:15 +01003014 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003015 instruction, needs_runtime_call ? LocationSummary::kCall : LocationSummary::kNoCall);
3016 if (needs_runtime_call) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003017 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003018 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3019 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
3020 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003021 } else {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003022 locations->SetInAt(0, Location::RequiresRegister());
3023 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
3024 locations->SetInAt(2, Location::RequiresRegister());
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003025
3026 if (needs_write_barrier) {
3027 // Temporary registers for the write barrier.
3028 locations->AddTemp(Location::RequiresRegister());
3029 locations->AddTemp(Location::RequiresRegister());
3030 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003031 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003032}
3033
3034void InstructionCodeGeneratorARM::VisitArraySet(HArraySet* instruction) {
3035 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003036 Register obj = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003037 Location index = locations->InAt(1);
Nicolas Geoffray39468442014-09-02 15:17:15 +01003038 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003039 bool needs_runtime_call = locations->WillCall();
3040 bool needs_write_barrier =
3041 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003042
3043 switch (value_type) {
3044 case Primitive::kPrimBoolean:
3045 case Primitive::kPrimByte: {
3046 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003047 Register value = locations->InAt(2).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003048 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003049 size_t offset =
3050 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003051 __ StoreToOffset(kStoreByte, value, obj, offset);
3052 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003053 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003054 __ StoreToOffset(kStoreByte, value, IP, data_offset);
3055 }
3056 break;
3057 }
3058
3059 case Primitive::kPrimShort:
3060 case Primitive::kPrimChar: {
3061 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003062 Register value = locations->InAt(2).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003063 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003064 size_t offset =
3065 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003066 __ StoreToOffset(kStoreHalfword, value, obj, offset);
3067 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003068 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_2));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003069 __ StoreToOffset(kStoreHalfword, value, IP, data_offset);
3070 }
3071 break;
3072 }
3073
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003074 case Primitive::kPrimInt:
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003075 case Primitive::kPrimNot: {
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003076 if (!needs_runtime_call) {
3077 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003078 Register value = locations->InAt(2).AsRegister<Register>();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003079 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003080 size_t offset =
3081 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003082 __ StoreToOffset(kStoreWord, value, obj, offset);
3083 } else {
3084 DCHECK(index.IsRegister()) << index;
Roland Levillain271ab9c2014-11-27 15:23:57 +00003085 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4));
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003086 __ StoreToOffset(kStoreWord, value, IP, data_offset);
3087 }
3088 if (needs_write_barrier) {
3089 DCHECK_EQ(value_type, Primitive::kPrimNot);
Roland Levillain271ab9c2014-11-27 15:23:57 +00003090 Register temp = locations->GetTemp(0).AsRegister<Register>();
3091 Register card = locations->GetTemp(1).AsRegister<Register>();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003092 codegen_->MarkGCCard(temp, card, obj, value);
3093 }
3094 } else {
3095 DCHECK_EQ(value_type, Primitive::kPrimNot);
Roland Levillain199f3362014-11-27 17:15:16 +00003096 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pAputObject),
3097 instruction,
3098 instruction->GetDexPc());
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003099 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003100 break;
3101 }
3102
3103 case Primitive::kPrimLong: {
3104 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003105 Location value = locations->InAt(2);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003106 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003107 size_t offset =
3108 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003109 __ StoreToOffset(kStoreWordPair, value.AsRegisterPairLow<Register>(), obj, offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003110 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003111 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_8));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003112 __ StoreToOffset(kStoreWordPair, value.AsRegisterPairLow<Register>(), IP, data_offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003113 }
3114 break;
3115 }
3116
3117 case Primitive::kPrimFloat:
3118 case Primitive::kPrimDouble:
3119 LOG(FATAL) << "Unimplemented register type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07003120 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003121 case Primitive::kPrimVoid:
3122 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07003123 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003124 }
3125}
3126
3127void LocationsBuilderARM::VisitArrayLength(HArrayLength* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003128 LocationSummary* locations =
3129 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003130 locations->SetInAt(0, Location::RequiresRegister());
3131 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003132}
3133
3134void InstructionCodeGeneratorARM::VisitArrayLength(HArrayLength* instruction) {
3135 LocationSummary* locations = instruction->GetLocations();
3136 uint32_t offset = mirror::Array::LengthOffset().Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003137 Register obj = locations->InAt(0).AsRegister<Register>();
3138 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003139 __ LoadFromOffset(kLoadWord, out, obj, offset);
3140}
3141
3142void LocationsBuilderARM::VisitBoundsCheck(HBoundsCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003143 LocationSummary* locations =
3144 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003145 locations->SetInAt(0, Location::RequiresRegister());
3146 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003147 if (instruction->HasUses()) {
3148 locations->SetOut(Location::SameAsFirstInput());
3149 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003150}
3151
3152void InstructionCodeGeneratorARM::VisitBoundsCheck(HBoundsCheck* instruction) {
3153 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01003154 SlowPathCodeARM* slow_path = new (GetGraph()->GetArena()) BoundsCheckSlowPathARM(
Nicolas Geoffray39468442014-09-02 15:17:15 +01003155 instruction, locations->InAt(0), locations->InAt(1));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003156 codegen_->AddSlowPath(slow_path);
3157
Roland Levillain271ab9c2014-11-27 15:23:57 +00003158 Register index = locations->InAt(0).AsRegister<Register>();
3159 Register length = locations->InAt(1).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003160
3161 __ cmp(index, ShifterOperand(length));
3162 __ b(slow_path->GetEntryLabel(), CS);
3163}
3164
3165void CodeGeneratorARM::MarkGCCard(Register temp, Register card, Register object, Register value) {
3166 Label is_null;
3167 __ CompareAndBranchIfZero(value, &is_null);
3168 __ LoadFromOffset(kLoadWord, card, TR, Thread::CardTableOffset<kArmWordSize>().Int32Value());
3169 __ Lsr(temp, object, gc::accounting::CardTable::kCardShift);
3170 __ strb(card, Address(card, temp));
3171 __ Bind(&is_null);
3172}
3173
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003174void LocationsBuilderARM::VisitTemporary(HTemporary* temp) {
3175 temp->SetLocations(nullptr);
3176}
3177
3178void InstructionCodeGeneratorARM::VisitTemporary(HTemporary* temp) {
3179 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003180 UNUSED(temp);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003181}
3182
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003183void LocationsBuilderARM::VisitParallelMove(HParallelMove* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003184 UNUSED(instruction);
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01003185 LOG(FATAL) << "Unreachable";
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003186}
3187
3188void InstructionCodeGeneratorARM::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01003189 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
3190}
3191
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003192void LocationsBuilderARM::VisitSuspendCheck(HSuspendCheck* instruction) {
3193 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
3194}
3195
3196void InstructionCodeGeneratorARM::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01003197 HBasicBlock* block = instruction->GetBlock();
3198 if (block->GetLoopInformation() != nullptr) {
3199 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
3200 // The back edge will generate the suspend check.
3201 return;
3202 }
3203 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
3204 // The goto will generate the suspend check.
3205 return;
3206 }
3207 GenerateSuspendCheck(instruction, nullptr);
3208}
3209
3210void InstructionCodeGeneratorARM::GenerateSuspendCheck(HSuspendCheck* instruction,
3211 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003212 SuspendCheckSlowPathARM* slow_path =
Nicolas Geoffray3c049742014-09-24 18:10:46 +01003213 new (GetGraph()->GetArena()) SuspendCheckSlowPathARM(instruction, successor);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003214 codegen_->AddSlowPath(slow_path);
3215
Nicolas Geoffray44b819e2014-11-06 12:00:54 +00003216 __ LoadFromOffset(
3217 kLoadUnsignedHalfword, IP, TR, Thread::ThreadFlagsOffset<kArmWordSize>().Int32Value());
3218 __ cmp(IP, ShifterOperand(0));
3219 // TODO: Figure out the branch offsets and use cbz/cbnz.
Nicolas Geoffray3c049742014-09-24 18:10:46 +01003220 if (successor == nullptr) {
Nicolas Geoffray44b819e2014-11-06 12:00:54 +00003221 __ b(slow_path->GetEntryLabel(), NE);
Nicolas Geoffray3c049742014-09-24 18:10:46 +01003222 __ Bind(slow_path->GetReturnLabel());
3223 } else {
Nicolas Geoffray44b819e2014-11-06 12:00:54 +00003224 __ b(codegen_->GetLabelOf(successor), EQ);
Nicolas Geoffray3c049742014-09-24 18:10:46 +01003225 __ b(slow_path->GetEntryLabel());
3226 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003227}
3228
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01003229ArmAssembler* ParallelMoveResolverARM::GetAssembler() const {
3230 return codegen_->GetAssembler();
3231}
3232
3233void ParallelMoveResolverARM::EmitMove(size_t index) {
3234 MoveOperands* move = moves_.Get(index);
3235 Location source = move->GetSource();
3236 Location destination = move->GetDestination();
3237
3238 if (source.IsRegister()) {
3239 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003240 __ Mov(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01003241 } else {
3242 DCHECK(destination.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003243 __ StoreToOffset(kStoreWord, source.AsRegister<Register>(),
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01003244 SP, destination.GetStackIndex());
3245 }
3246 } else if (source.IsStackSlot()) {
3247 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003248 __ LoadFromOffset(kLoadWord, destination.AsRegister<Register>(),
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01003249 SP, source.GetStackIndex());
3250 } else {
3251 DCHECK(destination.IsStackSlot());
3252 __ LoadFromOffset(kLoadWord, IP, SP, source.GetStackIndex());
3253 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
3254 }
3255 } else {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003256 DCHECK(source.IsConstant());
Roland Levillain476df552014-10-09 17:51:36 +01003257 DCHECK(source.GetConstant()->IsIntConstant());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003258 int32_t value = source.GetConstant()->AsIntConstant()->GetValue();
3259 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003260 __ LoadImmediate(destination.AsRegister<Register>(), value);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003261 } else {
3262 DCHECK(destination.IsStackSlot());
3263 __ LoadImmediate(IP, value);
Nicolas Geoffray360231a2014-10-08 21:07:48 +01003264 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003265 }
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01003266 }
3267}
3268
3269void ParallelMoveResolverARM::Exchange(Register reg, int mem) {
3270 __ Mov(IP, reg);
3271 __ LoadFromOffset(kLoadWord, reg, SP, mem);
3272 __ StoreToOffset(kStoreWord, IP, SP, mem);
3273}
3274
3275void ParallelMoveResolverARM::Exchange(int mem1, int mem2) {
3276 ScratchRegisterScope ensure_scratch(this, IP, R0, codegen_->GetNumberOfCoreRegisters());
3277 int stack_offset = ensure_scratch.IsSpilled() ? kArmWordSize : 0;
3278 __ LoadFromOffset(kLoadWord, static_cast<Register>(ensure_scratch.GetRegister()),
3279 SP, mem1 + stack_offset);
3280 __ LoadFromOffset(kLoadWord, IP, SP, mem2 + stack_offset);
3281 __ StoreToOffset(kStoreWord, static_cast<Register>(ensure_scratch.GetRegister()),
3282 SP, mem2 + stack_offset);
3283 __ StoreToOffset(kStoreWord, IP, SP, mem1 + stack_offset);
3284}
3285
3286void ParallelMoveResolverARM::EmitSwap(size_t index) {
3287 MoveOperands* move = moves_.Get(index);
3288 Location source = move->GetSource();
3289 Location destination = move->GetDestination();
3290
3291 if (source.IsRegister() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003292 DCHECK_NE(source.AsRegister<Register>(), IP);
3293 DCHECK_NE(destination.AsRegister<Register>(), IP);
3294 __ Mov(IP, source.AsRegister<Register>());
3295 __ Mov(source.AsRegister<Register>(), destination.AsRegister<Register>());
3296 __ Mov(destination.AsRegister<Register>(), IP);
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01003297 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003298 Exchange(source.AsRegister<Register>(), destination.GetStackIndex());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01003299 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003300 Exchange(destination.AsRegister<Register>(), source.GetStackIndex());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01003301 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
3302 Exchange(source.GetStackIndex(), destination.GetStackIndex());
3303 } else {
3304 LOG(FATAL) << "Unimplemented";
3305 }
3306}
3307
3308void ParallelMoveResolverARM::SpillScratch(int reg) {
3309 __ Push(static_cast<Register>(reg));
3310}
3311
3312void ParallelMoveResolverARM::RestoreScratch(int reg) {
3313 __ Pop(static_cast<Register>(reg));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003314}
3315
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003316void LocationsBuilderARM::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003317 LocationSummary::CallKind call_kind = cls->CanCallRuntime()
3318 ? LocationSummary::kCallOnSlowPath
3319 : LocationSummary::kNoCall;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003320 LocationSummary* locations =
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003321 new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003322 locations->SetOut(Location::RequiresRegister());
3323}
3324
3325void InstructionCodeGeneratorARM::VisitLoadClass(HLoadClass* cls) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003326 Register out = cls->GetLocations()->Out().AsRegister<Register>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003327 if (cls->IsReferrersClass()) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003328 DCHECK(!cls->CanCallRuntime());
3329 DCHECK(!cls->MustGenerateClinitCheck());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003330 codegen_->LoadCurrentMethod(out);
3331 __ LoadFromOffset(kLoadWord, out, out, mirror::ArtMethod::DeclaringClassOffset().Int32Value());
3332 } else {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003333 DCHECK(cls->CanCallRuntime());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003334 codegen_->LoadCurrentMethod(out);
3335 __ LoadFromOffset(
3336 kLoadWord, out, out, mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value());
3337 __ LoadFromOffset(kLoadWord, out, out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex()));
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003338
3339 SlowPathCodeARM* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM(
3340 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
3341 codegen_->AddSlowPath(slow_path);
3342 __ cmp(out, ShifterOperand(0));
3343 __ b(slow_path->GetEntryLabel(), EQ);
3344 if (cls->MustGenerateClinitCheck()) {
3345 GenerateClassInitializationCheck(slow_path, out);
3346 } else {
3347 __ Bind(slow_path->GetExitLabel());
3348 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003349 }
3350}
3351
3352void LocationsBuilderARM::VisitClinitCheck(HClinitCheck* check) {
3353 LocationSummary* locations =
3354 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
3355 locations->SetInAt(0, Location::RequiresRegister());
3356 if (check->HasUses()) {
3357 locations->SetOut(Location::SameAsFirstInput());
3358 }
3359}
3360
3361void InstructionCodeGeneratorARM::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003362 // We assume the class is not null.
3363 SlowPathCodeARM* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM(
3364 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003365 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00003366 GenerateClassInitializationCheck(slow_path,
3367 check->GetLocations()->InAt(0).AsRegister<Register>());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003368}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003369
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003370void InstructionCodeGeneratorARM::GenerateClassInitializationCheck(
3371 SlowPathCodeARM* slow_path, Register class_reg) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003372 __ LoadFromOffset(kLoadWord, IP, class_reg, mirror::Class::StatusOffset().Int32Value());
3373 __ cmp(IP, ShifterOperand(mirror::Class::kStatusInitialized));
3374 __ b(slow_path->GetEntryLabel(), LT);
3375 // Even if the initialized flag is set, we may be in a situation where caches are not synced
3376 // properly. Therefore, we do a memory fence.
3377 __ dmb(ISH);
3378 __ Bind(slow_path->GetExitLabel());
3379}
3380
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003381void LocationsBuilderARM::VisitLoadString(HLoadString* load) {
3382 LocationSummary* locations =
3383 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kCallOnSlowPath);
3384 locations->SetOut(Location::RequiresRegister());
3385}
3386
3387void InstructionCodeGeneratorARM::VisitLoadString(HLoadString* load) {
3388 SlowPathCodeARM* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathARM(load);
3389 codegen_->AddSlowPath(slow_path);
3390
Roland Levillain271ab9c2014-11-27 15:23:57 +00003391 Register out = load->GetLocations()->Out().AsRegister<Register>();
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003392 codegen_->LoadCurrentMethod(out);
Mathieu Chartiereace4582014-11-24 18:29:54 -08003393 __ LoadFromOffset(kLoadWord, out, out, mirror::ArtMethod::DeclaringClassOffset().Int32Value());
3394 __ LoadFromOffset(kLoadWord, out, out, mirror::Class::DexCacheStringsOffset().Int32Value());
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003395 __ LoadFromOffset(kLoadWord, out, out, CodeGenerator::GetCacheOffset(load->GetStringIndex()));
3396 __ cmp(out, ShifterOperand(0));
3397 __ b(slow_path->GetEntryLabel(), EQ);
3398 __ Bind(slow_path->GetExitLabel());
3399}
3400
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00003401void LocationsBuilderARM::VisitLoadException(HLoadException* load) {
3402 LocationSummary* locations =
3403 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
3404 locations->SetOut(Location::RequiresRegister());
3405}
3406
3407void InstructionCodeGeneratorARM::VisitLoadException(HLoadException* load) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003408 Register out = load->GetLocations()->Out().AsRegister<Register>();
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00003409 int32_t offset = Thread::ExceptionOffset<kArmWordSize>().Int32Value();
3410 __ LoadFromOffset(kLoadWord, out, TR, offset);
3411 __ LoadImmediate(IP, 0);
3412 __ StoreToOffset(kStoreWord, IP, TR, offset);
3413}
3414
3415void LocationsBuilderARM::VisitThrow(HThrow* instruction) {
3416 LocationSummary* locations =
3417 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3418 InvokeRuntimeCallingConvention calling_convention;
3419 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3420}
3421
3422void InstructionCodeGeneratorARM::VisitThrow(HThrow* instruction) {
3423 codegen_->InvokeRuntime(
3424 QUICK_ENTRY_POINT(pDeliverException), instruction, instruction->GetDexPc());
3425}
3426
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003427void LocationsBuilderARM::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003428 LocationSummary::CallKind call_kind = instruction->IsClassFinal()
3429 ? LocationSummary::kNoCall
3430 : LocationSummary::kCallOnSlowPath;
3431 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
3432 locations->SetInAt(0, Location::RequiresRegister());
3433 locations->SetInAt(1, Location::RequiresRegister());
3434 locations->SetOut(Location::RequiresRegister());
3435}
3436
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003437void InstructionCodeGeneratorARM::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003438 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003439 Register obj = locations->InAt(0).AsRegister<Register>();
3440 Register cls = locations->InAt(1).AsRegister<Register>();
3441 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003442 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3443 Label done, zero;
3444 SlowPathCodeARM* slow_path = nullptr;
3445
3446 // Return 0 if `obj` is null.
3447 // TODO: avoid this check if we know obj is not null.
3448 __ cmp(obj, ShifterOperand(0));
3449 __ b(&zero, EQ);
3450 // Compare the class of `obj` with `cls`.
3451 __ LoadFromOffset(kLoadWord, out, obj, class_offset);
3452 __ cmp(out, ShifterOperand(cls));
3453 if (instruction->IsClassFinal()) {
3454 // Classes must be equal for the instanceof to succeed.
3455 __ b(&zero, NE);
3456 __ LoadImmediate(out, 1);
3457 __ b(&done);
3458 } else {
3459 // If the classes are not equal, we go into a slow path.
3460 DCHECK(locations->OnlyCallsOnSlowPath());
3461 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM(
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003462 instruction, locations->InAt(1), locations->Out(), instruction->GetDexPc());
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003463 codegen_->AddSlowPath(slow_path);
3464 __ b(slow_path->GetEntryLabel(), NE);
3465 __ LoadImmediate(out, 1);
3466 __ b(&done);
3467 }
3468 __ Bind(&zero);
3469 __ LoadImmediate(out, 0);
3470 if (slow_path != nullptr) {
3471 __ Bind(slow_path->GetExitLabel());
3472 }
3473 __ Bind(&done);
3474}
3475
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003476void LocationsBuilderARM::VisitCheckCast(HCheckCast* instruction) {
3477 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
3478 instruction, LocationSummary::kCallOnSlowPath);
3479 locations->SetInAt(0, Location::RequiresRegister());
3480 locations->SetInAt(1, Location::RequiresRegister());
3481 locations->AddTemp(Location::RequiresRegister());
3482}
3483
3484void InstructionCodeGeneratorARM::VisitCheckCast(HCheckCast* instruction) {
3485 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003486 Register obj = locations->InAt(0).AsRegister<Register>();
3487 Register cls = locations->InAt(1).AsRegister<Register>();
3488 Register temp = locations->GetTemp(0).AsRegister<Register>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003489 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3490
3491 SlowPathCodeARM* slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM(
3492 instruction, locations->InAt(1), locations->GetTemp(0), instruction->GetDexPc());
3493 codegen_->AddSlowPath(slow_path);
3494
3495 // TODO: avoid this check if we know obj is not null.
3496 __ cmp(obj, ShifterOperand(0));
3497 __ b(slow_path->GetExitLabel(), EQ);
3498 // Compare the class of `obj` with `cls`.
3499 __ LoadFromOffset(kLoadWord, temp, obj, class_offset);
3500 __ cmp(temp, ShifterOperand(cls));
3501 __ b(slow_path->GetEntryLabel(), NE);
3502 __ Bind(slow_path->GetExitLabel());
3503}
3504
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00003505void LocationsBuilderARM::VisitMonitorOperation(HMonitorOperation* instruction) {
3506 LocationSummary* locations =
3507 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3508 InvokeRuntimeCallingConvention calling_convention;
3509 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3510}
3511
3512void InstructionCodeGeneratorARM::VisitMonitorOperation(HMonitorOperation* instruction) {
3513 codegen_->InvokeRuntime(instruction->IsEnter()
3514 ? QUICK_ENTRY_POINT(pLockObject) : QUICK_ENTRY_POINT(pUnlockObject),
3515 instruction,
3516 instruction->GetDexPc());
3517}
3518
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003519void LocationsBuilderARM::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction); }
3520void LocationsBuilderARM::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction); }
3521void LocationsBuilderARM::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction); }
3522
3523void LocationsBuilderARM::HandleBitwiseOperation(HBinaryOperation* instruction) {
3524 LocationSummary* locations =
3525 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3526 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
3527 || instruction->GetResultType() == Primitive::kPrimLong);
3528 locations->SetInAt(0, Location::RequiresRegister());
3529 locations->SetInAt(1, Location::RequiresRegister());
3530 bool output_overlaps = (instruction->GetResultType() == Primitive::kPrimLong);
3531 locations->SetOut(Location::RequiresRegister(), output_overlaps);
3532}
3533
3534void InstructionCodeGeneratorARM::VisitAnd(HAnd* instruction) {
3535 HandleBitwiseOperation(instruction);
3536}
3537
3538void InstructionCodeGeneratorARM::VisitOr(HOr* instruction) {
3539 HandleBitwiseOperation(instruction);
3540}
3541
3542void InstructionCodeGeneratorARM::VisitXor(HXor* instruction) {
3543 HandleBitwiseOperation(instruction);
3544}
3545
3546void InstructionCodeGeneratorARM::HandleBitwiseOperation(HBinaryOperation* instruction) {
3547 LocationSummary* locations = instruction->GetLocations();
3548
3549 if (instruction->GetResultType() == Primitive::kPrimInt) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003550 Register first = locations->InAt(0).AsRegister<Register>();
3551 Register second = locations->InAt(1).AsRegister<Register>();
3552 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003553 if (instruction->IsAnd()) {
3554 __ and_(out, first, ShifterOperand(second));
3555 } else if (instruction->IsOr()) {
3556 __ orr(out, first, ShifterOperand(second));
3557 } else {
3558 DCHECK(instruction->IsXor());
3559 __ eor(out, first, ShifterOperand(second));
3560 }
3561 } else {
3562 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
3563 Location first = locations->InAt(0);
3564 Location second = locations->InAt(1);
3565 Location out = locations->Out();
3566 if (instruction->IsAnd()) {
3567 __ and_(out.AsRegisterPairLow<Register>(),
3568 first.AsRegisterPairLow<Register>(),
3569 ShifterOperand(second.AsRegisterPairLow<Register>()));
3570 __ and_(out.AsRegisterPairHigh<Register>(),
3571 first.AsRegisterPairHigh<Register>(),
3572 ShifterOperand(second.AsRegisterPairHigh<Register>()));
3573 } else if (instruction->IsOr()) {
3574 __ orr(out.AsRegisterPairLow<Register>(),
3575 first.AsRegisterPairLow<Register>(),
3576 ShifterOperand(second.AsRegisterPairLow<Register>()));
3577 __ orr(out.AsRegisterPairHigh<Register>(),
3578 first.AsRegisterPairHigh<Register>(),
3579 ShifterOperand(second.AsRegisterPairHigh<Register>()));
3580 } else {
3581 DCHECK(instruction->IsXor());
3582 __ eor(out.AsRegisterPairLow<Register>(),
3583 first.AsRegisterPairLow<Register>(),
3584 ShifterOperand(second.AsRegisterPairLow<Register>()));
3585 __ eor(out.AsRegisterPairHigh<Register>(),
3586 first.AsRegisterPairHigh<Register>(),
3587 ShifterOperand(second.AsRegisterPairHigh<Register>()));
3588 }
3589 }
3590}
3591
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00003592} // namespace arm
3593} // namespace art