blob: b336c31edd88d06b746686b32a1f0a9def68f6ab [file] [log] [blame]
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "code_generator_arm.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000018
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070019#include "entrypoints/quick/quick_entrypoints.h"
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +010020#include "gc/accounting/card_table.h"
Ian Rogers7e70b002014-10-08 11:47:24 -070021#include "mirror/array-inl.h"
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +000022#include "mirror/art_method.h"
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +010023#include "mirror/class.h"
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070024#include "thread.h"
Nicolas Geoffray9cf35522014-06-09 18:40:10 +010025#include "utils/arm/assembler_arm.h"
26#include "utils/arm/managed_register_arm.h"
Roland Levillain946e1432014-11-11 17:35:19 +000027#include "utils/assembler.h"
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010028#include "utils/stack_checks.h"
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +000029
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000030namespace art {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +010031
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000032namespace arm {
33
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +000034static DRegister FromLowSToD(SRegister reg) {
35 DCHECK_EQ(reg % 2, 0);
36 return static_cast<DRegister>(reg / 2);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +010037}
38
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010039static constexpr bool kExplicitStackOverflowCheck = false;
40
41static constexpr int kNumberOfPushedRegistersAtEntry = 1 + 2; // LR, R6, R7
42static constexpr int kCurrentMethodStackOffset = 0;
43
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +010044static constexpr Register kRuntimeParameterCoreRegisters[] = { R0, R1, R2 };
45static constexpr size_t kRuntimeParameterCoreRegistersLength =
46 arraysize(kRuntimeParameterCoreRegisters);
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +000047static constexpr SRegister kRuntimeParameterFpuRegisters[] = { };
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +010048static constexpr size_t kRuntimeParameterFpuRegistersLength = 0;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +010049
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +000050class InvokeRuntimeCallingConvention : public CallingConvention<Register, SRegister> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +010051 public:
52 InvokeRuntimeCallingConvention()
53 : CallingConvention(kRuntimeParameterCoreRegisters,
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +010054 kRuntimeParameterCoreRegistersLength,
55 kRuntimeParameterFpuRegisters,
56 kRuntimeParameterFpuRegistersLength) {}
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +010057
58 private:
59 DISALLOW_COPY_AND_ASSIGN(InvokeRuntimeCallingConvention);
60};
61
Nicolas Geoffraye5038322014-07-04 09:41:32 +010062#define __ reinterpret_cast<ArmAssembler*>(codegen->GetAssembler())->
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +010063#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kArmWordSize, x).Int32Value()
Nicolas Geoffraye5038322014-07-04 09:41:32 +010064
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +010065class SlowPathCodeARM : public SlowPathCode {
66 public:
67 SlowPathCodeARM() : entry_label_(), exit_label_() {}
68
69 Label* GetEntryLabel() { return &entry_label_; }
70 Label* GetExitLabel() { return &exit_label_; }
71
72 private:
73 Label entry_label_;
74 Label exit_label_;
75
76 DISALLOW_COPY_AND_ASSIGN(SlowPathCodeARM);
77};
78
79class NullCheckSlowPathARM : public SlowPathCodeARM {
Nicolas Geoffraye5038322014-07-04 09:41:32 +010080 public:
Nicolas Geoffray39468442014-09-02 15:17:15 +010081 explicit NullCheckSlowPathARM(HNullCheck* instruction) : instruction_(instruction) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +010082
83 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +010084 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
Nicolas Geoffraye5038322014-07-04 09:41:32 +010085 __ Bind(GetEntryLabel());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +010086 arm_codegen->InvokeRuntime(
87 QUICK_ENTRY_POINT(pThrowNullPointer), instruction_, instruction_->GetDexPc());
Nicolas Geoffraye5038322014-07-04 09:41:32 +010088 }
89
90 private:
Nicolas Geoffray39468442014-09-02 15:17:15 +010091 HNullCheck* const instruction_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +010092 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathARM);
93};
94
Calin Juravled0d48522014-11-04 16:40:20 +000095class DivZeroCheckSlowPathARM : public SlowPathCodeARM {
96 public:
97 explicit DivZeroCheckSlowPathARM(HDivZeroCheck* instruction) : instruction_(instruction) {}
98
99 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
100 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
101 __ Bind(GetEntryLabel());
102 arm_codegen->InvokeRuntime(
103 QUICK_ENTRY_POINT(pThrowDivZero), instruction_, instruction_->GetDexPc());
104 }
105
106 private:
107 HDivZeroCheck* const instruction_;
108 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathARM);
109};
110
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100111class StackOverflowCheckSlowPathARM : public SlowPathCodeARM {
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100112 public:
113 StackOverflowCheckSlowPathARM() {}
114
115 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
116 __ Bind(GetEntryLabel());
117 __ LoadFromOffset(kLoadWord, PC, TR,
118 QUICK_ENTRYPOINT_OFFSET(kArmWordSize, pThrowStackOverflow).Int32Value());
119 }
120
121 private:
122 DISALLOW_COPY_AND_ASSIGN(StackOverflowCheckSlowPathARM);
123};
124
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100125class SuspendCheckSlowPathARM : public SlowPathCodeARM {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000126 public:
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100127 explicit SuspendCheckSlowPathARM(HSuspendCheck* instruction, HBasicBlock* successor)
128 : instruction_(instruction), successor_(successor) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000129
130 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100131 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000132 __ Bind(GetEntryLabel());
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100133 codegen->SaveLiveRegisters(instruction_->GetLocations());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100134 arm_codegen->InvokeRuntime(
135 QUICK_ENTRY_POINT(pTestSuspend), instruction_, instruction_->GetDexPc());
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100136 codegen->RestoreLiveRegisters(instruction_->GetLocations());
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100137 if (successor_ == nullptr) {
138 __ b(GetReturnLabel());
139 } else {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100140 __ b(arm_codegen->GetLabelOf(successor_));
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100141 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000142 }
143
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100144 Label* GetReturnLabel() {
145 DCHECK(successor_ == nullptr);
146 return &return_label_;
147 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000148
149 private:
150 HSuspendCheck* const instruction_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100151 // If not null, the block to branch to after the suspend check.
152 HBasicBlock* const successor_;
153
154 // If `successor_` is null, the label to branch to after the suspend check.
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000155 Label return_label_;
156
157 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathARM);
158};
159
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100160class BoundsCheckSlowPathARM : public SlowPathCodeARM {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100161 public:
Roland Levillain5799fc02014-09-25 12:15:20 +0100162 BoundsCheckSlowPathARM(HBoundsCheck* instruction,
163 Location index_location,
164 Location length_location)
Nicolas Geoffray39468442014-09-02 15:17:15 +0100165 : instruction_(instruction),
166 index_location_(index_location),
167 length_location_(length_location) {}
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100168
169 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100170 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100171 __ Bind(GetEntryLabel());
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000172 // We're moving two locations to locations that could overlap, so we need a parallel
173 // move resolver.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100174 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000175 codegen->EmitParallelMoves(
176 index_location_,
177 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
178 length_location_,
179 Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100180 arm_codegen->InvokeRuntime(
181 QUICK_ENTRY_POINT(pThrowArrayBounds), instruction_, instruction_->GetDexPc());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100182 }
183
184 private:
Nicolas Geoffray39468442014-09-02 15:17:15 +0100185 HBoundsCheck* const instruction_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100186 const Location index_location_;
187 const Location length_location_;
188
189 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathARM);
190};
191
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000192class LoadClassSlowPathARM : public SlowPathCodeARM {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100193 public:
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000194 LoadClassSlowPathARM(HLoadClass* cls,
195 HInstruction* at,
196 uint32_t dex_pc,
197 bool do_clinit)
198 : cls_(cls), at_(at), dex_pc_(dex_pc), do_clinit_(do_clinit) {
199 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
200 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100201
202 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000203 LocationSummary* locations = at_->GetLocations();
204
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100205 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
206 __ Bind(GetEntryLabel());
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000207 codegen->SaveLiveRegisters(locations);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100208
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100209 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000210 __ LoadImmediate(calling_convention.GetRegisterAt(0), cls_->GetTypeIndex());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100211 arm_codegen->LoadCurrentMethod(calling_convention.GetRegisterAt(1));
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000212 int32_t entry_point_offset = do_clinit_
213 ? QUICK_ENTRY_POINT(pInitializeStaticStorage)
214 : QUICK_ENTRY_POINT(pInitializeType);
215 arm_codegen->InvokeRuntime(entry_point_offset, at_, dex_pc_);
216
217 // Move the class to the desired location.
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000218 Location out = locations->Out();
219 if (out.IsValid()) {
220 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000221 arm_codegen->Move32(locations->Out(), Location::RegisterLocation(R0));
222 }
223 codegen->RestoreLiveRegisters(locations);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100224 __ b(GetExitLabel());
225 }
226
227 private:
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000228 // The class this slow path will load.
229 HLoadClass* const cls_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100230
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000231 // The instruction where this slow path is happening.
232 // (Might be the load class or an initialization check).
233 HInstruction* const at_;
234
235 // The dex PC of `at_`.
236 const uint32_t dex_pc_;
237
238 // Whether to initialize the class.
239 const bool do_clinit_;
240
241 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathARM);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100242};
243
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000244class LoadStringSlowPathARM : public SlowPathCodeARM {
245 public:
246 explicit LoadStringSlowPathARM(HLoadString* instruction) : instruction_(instruction) {}
247
248 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
249 LocationSummary* locations = instruction_->GetLocations();
250 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
251
252 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
253 __ Bind(GetEntryLabel());
254 codegen->SaveLiveRegisters(locations);
255
256 InvokeRuntimeCallingConvention calling_convention;
257 arm_codegen->LoadCurrentMethod(calling_convention.GetRegisterAt(0));
258 __ LoadImmediate(calling_convention.GetRegisterAt(1), instruction_->GetStringIndex());
259 arm_codegen->InvokeRuntime(
260 QUICK_ENTRY_POINT(pResolveString), instruction_, instruction_->GetDexPc());
261 arm_codegen->Move32(locations->Out(), Location::RegisterLocation(R0));
262
263 codegen->RestoreLiveRegisters(locations);
264 __ b(GetExitLabel());
265 }
266
267 private:
268 HLoadString* const instruction_;
269
270 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathARM);
271};
272
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000273class TypeCheckSlowPathARM : public SlowPathCodeARM {
274 public:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000275 TypeCheckSlowPathARM(HInstruction* instruction,
276 Location class_to_check,
277 Location object_class,
278 uint32_t dex_pc)
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000279 : instruction_(instruction),
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000280 class_to_check_(class_to_check),
281 object_class_(object_class),
282 dex_pc_(dex_pc) {}
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000283
284 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
285 LocationSummary* locations = instruction_->GetLocations();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000286 DCHECK(instruction_->IsCheckCast()
287 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000288
289 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
290 __ Bind(GetEntryLabel());
291 codegen->SaveLiveRegisters(locations);
292
293 // We're moving two locations to locations that could overlap, so we need a parallel
294 // move resolver.
295 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000296 codegen->EmitParallelMoves(
297 class_to_check_,
298 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
299 object_class_,
300 Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000301
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000302 if (instruction_->IsInstanceOf()) {
303 arm_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pInstanceofNonTrivial), instruction_, dex_pc_);
304 arm_codegen->Move32(locations->Out(), Location::RegisterLocation(R0));
305 } else {
306 DCHECK(instruction_->IsCheckCast());
307 arm_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pCheckCast), instruction_, dex_pc_);
308 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000309
310 codegen->RestoreLiveRegisters(locations);
311 __ b(GetExitLabel());
312 }
313
314 private:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000315 HInstruction* const instruction_;
316 const Location class_to_check_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000317 const Location object_class_;
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000318 uint32_t dex_pc_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000319
320 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathARM);
321};
322
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000323#undef __
324
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100325#undef __
326#define __ reinterpret_cast<ArmAssembler*>(GetAssembler())->
Dave Allison20dfc792014-06-16 20:44:29 -0700327
328inline Condition ARMCondition(IfCondition cond) {
329 switch (cond) {
330 case kCondEQ: return EQ;
331 case kCondNE: return NE;
332 case kCondLT: return LT;
333 case kCondLE: return LE;
334 case kCondGT: return GT;
335 case kCondGE: return GE;
336 default:
337 LOG(FATAL) << "Unknown if condition";
338 }
339 return EQ; // Unreachable.
340}
341
342inline Condition ARMOppositeCondition(IfCondition cond) {
343 switch (cond) {
344 case kCondEQ: return NE;
345 case kCondNE: return EQ;
346 case kCondLT: return GE;
347 case kCondLE: return GT;
348 case kCondGT: return LE;
349 case kCondGE: return LT;
350 default:
351 LOG(FATAL) << "Unknown if condition";
352 }
353 return EQ; // Unreachable.
354}
355
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100356void CodeGeneratorARM::DumpCoreRegister(std::ostream& stream, int reg) const {
357 stream << ArmManagedRegister::FromCoreRegister(Register(reg));
358}
359
360void CodeGeneratorARM::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000361 stream << ArmManagedRegister::FromSRegister(SRegister(reg));
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100362}
363
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100364size_t CodeGeneratorARM::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
365 __ StoreToOffset(kStoreWord, static_cast<Register>(reg_id), SP, stack_index);
366 return kArmWordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100367}
368
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100369size_t CodeGeneratorARM::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
370 __ LoadFromOffset(kLoadWord, static_cast<Register>(reg_id), SP, stack_index);
371 return kArmWordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100372}
373
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100374CodeGeneratorARM::CodeGeneratorARM(HGraph* graph)
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000375 : CodeGenerator(graph, kNumberOfCoreRegisters, kNumberOfSRegisters, kNumberOfRegisterPairs),
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100376 block_labels_(graph->GetArena(), 0),
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100377 location_builder_(graph, this),
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +0100378 instruction_visitor_(graph, this),
Nicolas Geoffray8d486732014-07-16 16:23:40 +0100379 move_resolver_(graph->GetArena(), this),
380 assembler_(true) {}
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100381
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100382size_t CodeGeneratorARM::FrameEntrySpillSize() const {
383 return kNumberOfPushedRegistersAtEntry * kArmWordSize;
384}
385
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100386Location CodeGeneratorARM::AllocateFreeRegister(Primitive::Type type) const {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100387 switch (type) {
388 case Primitive::kPrimLong: {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100389 size_t reg = FindFreeEntry(blocked_register_pairs_, kNumberOfRegisterPairs);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100390 ArmManagedRegister pair =
391 ArmManagedRegister::FromRegisterPair(static_cast<RegisterPair>(reg));
Calin Juravle34bacdf2014-10-07 20:23:36 +0100392 DCHECK(!blocked_core_registers_[pair.AsRegisterPairLow()]);
393 DCHECK(!blocked_core_registers_[pair.AsRegisterPairHigh()]);
394
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100395 blocked_core_registers_[pair.AsRegisterPairLow()] = true;
396 blocked_core_registers_[pair.AsRegisterPairHigh()] = true;
Calin Juravle34bacdf2014-10-07 20:23:36 +0100397 UpdateBlockedPairRegisters();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100398 return Location::RegisterPairLocation(pair.AsRegisterPairLow(), pair.AsRegisterPairHigh());
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100399 }
400
401 case Primitive::kPrimByte:
402 case Primitive::kPrimBoolean:
403 case Primitive::kPrimChar:
404 case Primitive::kPrimShort:
405 case Primitive::kPrimInt:
406 case Primitive::kPrimNot: {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100407 int reg = FindFreeEntry(blocked_core_registers_, kNumberOfCoreRegisters);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100408 // Block all register pairs that contain `reg`.
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100409 for (int i = 0; i < kNumberOfRegisterPairs; i++) {
410 ArmManagedRegister current =
411 ArmManagedRegister::FromRegisterPair(static_cast<RegisterPair>(i));
412 if (current.AsRegisterPairLow() == reg || current.AsRegisterPairHigh() == reg) {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100413 blocked_register_pairs_[i] = true;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100414 }
415 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100416 return Location::RegisterLocation(reg);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100417 }
418
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000419 case Primitive::kPrimFloat: {
420 int reg = FindFreeEntry(blocked_fpu_registers_, kNumberOfSRegisters);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100421 return Location::FpuRegisterLocation(reg);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100422 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100423
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000424 case Primitive::kPrimDouble: {
Nicolas Geoffray3c035032014-10-28 10:46:40 +0000425 int reg = FindTwoFreeConsecutiveAlignedEntries(blocked_fpu_registers_, kNumberOfSRegisters);
426 DCHECK_EQ(reg % 2, 0);
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000427 return Location::FpuRegisterPairLocation(reg, reg + 1);
428 }
429
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100430 case Primitive::kPrimVoid:
431 LOG(FATAL) << "Unreachable type " << type;
432 }
433
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100434 return Location();
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100435}
436
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100437void CodeGeneratorARM::SetupBlockedRegisters() const {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100438 // Don't allocate the dalvik style register pair passing.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100439 blocked_register_pairs_[R1_R2] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100440
441 // Stack register, LR and PC are always reserved.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100442 blocked_core_registers_[SP] = true;
443 blocked_core_registers_[LR] = true;
444 blocked_core_registers_[PC] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100445
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100446 // Reserve thread register.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100447 blocked_core_registers_[TR] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100448
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +0100449 // Reserve temp register.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100450 blocked_core_registers_[IP] = true;
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +0100451
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100452 // TODO: We currently don't use Quick's callee saved registers.
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100453 // We always save and restore R6 and R7 to make sure we can use three
454 // register pairs for long operations.
Nicolas Geoffray44b819e2014-11-06 12:00:54 +0000455 blocked_core_registers_[R4] = true;
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100456 blocked_core_registers_[R5] = true;
457 blocked_core_registers_[R8] = true;
458 blocked_core_registers_[R10] = true;
459 blocked_core_registers_[R11] = true;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100460
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000461 blocked_fpu_registers_[S16] = true;
462 blocked_fpu_registers_[S17] = true;
463 blocked_fpu_registers_[S18] = true;
464 blocked_fpu_registers_[S19] = true;
465 blocked_fpu_registers_[S20] = true;
466 blocked_fpu_registers_[S21] = true;
467 blocked_fpu_registers_[S22] = true;
468 blocked_fpu_registers_[S23] = true;
Nicolas Geoffray3c035032014-10-28 10:46:40 +0000469 blocked_fpu_registers_[S24] = true;
470 blocked_fpu_registers_[S25] = true;
471 blocked_fpu_registers_[S26] = true;
472 blocked_fpu_registers_[S27] = true;
473 blocked_fpu_registers_[S28] = true;
474 blocked_fpu_registers_[S29] = true;
475 blocked_fpu_registers_[S30] = true;
476 blocked_fpu_registers_[S31] = true;
Calin Juravle34bacdf2014-10-07 20:23:36 +0100477
478 UpdateBlockedPairRegisters();
479}
480
481void CodeGeneratorARM::UpdateBlockedPairRegisters() const {
482 for (int i = 0; i < kNumberOfRegisterPairs; i++) {
483 ArmManagedRegister current =
484 ArmManagedRegister::FromRegisterPair(static_cast<RegisterPair>(i));
485 if (blocked_core_registers_[current.AsRegisterPairLow()]
486 || blocked_core_registers_[current.AsRegisterPairHigh()]) {
487 blocked_register_pairs_[i] = true;
488 }
489 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100490}
491
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100492InstructionCodeGeneratorARM::InstructionCodeGeneratorARM(HGraph* graph, CodeGeneratorARM* codegen)
493 : HGraphVisitor(graph),
494 assembler_(codegen->GetAssembler()),
495 codegen_(codegen) {}
496
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000497void CodeGeneratorARM::GenerateFrameEntry() {
Dave Allison648d7112014-07-25 16:15:27 -0700498 bool skip_overflow_check = IsLeafMethod() && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kArm);
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100499 if (!skip_overflow_check) {
500 if (kExplicitStackOverflowCheck) {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100501 SlowPathCodeARM* slow_path = new (GetGraph()->GetArena()) StackOverflowCheckSlowPathARM();
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100502 AddSlowPath(slow_path);
503
504 __ LoadFromOffset(kLoadWord, IP, TR, Thread::StackEndOffset<kArmWordSize>().Int32Value());
505 __ cmp(SP, ShifterOperand(IP));
506 __ b(slow_path->GetEntryLabel(), CC);
507 } else {
508 __ AddConstant(IP, SP, -static_cast<int32_t>(GetStackOverflowReservedBytes(kArm)));
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100509 __ LoadFromOffset(kLoadWord, IP, IP, 0);
Nicolas Geoffray39468442014-09-02 15:17:15 +0100510 RecordPcInfo(nullptr, 0);
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100511 }
512 }
513
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100514 core_spill_mask_ |= (1 << LR | 1 << R6 | 1 << R7);
515 __ PushList(1 << LR | 1 << R6 | 1 << R7);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000516
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100517 // The return PC has already been pushed on the stack.
Nicolas Geoffray707c8092014-04-04 10:50:14 +0100518 __ AddConstant(SP, -(GetFrameSize() - kNumberOfPushedRegistersAtEntry * kArmWordSize));
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100519 __ StoreToOffset(kStoreWord, R0, SP, 0);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000520}
521
522void CodeGeneratorARM::GenerateFrameExit() {
Nicolas Geoffray707c8092014-04-04 10:50:14 +0100523 __ AddConstant(SP, GetFrameSize() - kNumberOfPushedRegistersAtEntry * kArmWordSize);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100524 __ PopList(1 << PC | 1 << R6 | 1 << R7);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000525}
526
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100527void CodeGeneratorARM::Bind(HBasicBlock* block) {
528 __ Bind(GetLabelOf(block));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000529}
530
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100531Location CodeGeneratorARM::GetStackLocation(HLoadLocal* load) const {
532 switch (load->GetType()) {
533 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100534 case Primitive::kPrimDouble:
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100535 return Location::DoubleStackSlot(GetStackSlot(load->GetLocal()));
536 break;
537
538 case Primitive::kPrimInt:
539 case Primitive::kPrimNot:
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100540 case Primitive::kPrimFloat:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100541 return Location::StackSlot(GetStackSlot(load->GetLocal()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100542
543 case Primitive::kPrimBoolean:
544 case Primitive::kPrimByte:
545 case Primitive::kPrimChar:
546 case Primitive::kPrimShort:
547 case Primitive::kPrimVoid:
548 LOG(FATAL) << "Unexpected type " << load->GetType();
549 }
550
551 LOG(FATAL) << "Unreachable";
552 return Location();
553}
554
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100555Location InvokeDexCallingConventionVisitor::GetNextLocation(Primitive::Type type) {
556 switch (type) {
557 case Primitive::kPrimBoolean:
558 case Primitive::kPrimByte:
559 case Primitive::kPrimChar:
560 case Primitive::kPrimShort:
561 case Primitive::kPrimInt:
562 case Primitive::kPrimNot: {
563 uint32_t index = gp_index_++;
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000564 uint32_t stack_index = stack_index_++;
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100565 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100566 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100567 } else {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000568 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index));
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100569 }
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100570 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100571
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000572 case Primitive::kPrimLong: {
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100573 uint32_t index = gp_index_;
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000574 uint32_t stack_index = stack_index_;
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100575 gp_index_ += 2;
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000576 stack_index_ += 2;
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100577 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100578 ArmManagedRegister pair = ArmManagedRegister::FromRegisterPair(
579 calling_convention.GetRegisterPairAt(index));
580 return Location::RegisterPairLocation(pair.AsRegisterPairLow(), pair.AsRegisterPairHigh());
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100581 } else if (index + 1 == calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray0a6c4592014-10-30 16:37:57 +0000582 return Location::QuickParameter(index, stack_index);
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100583 } else {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000584 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index));
585 }
586 }
587
588 case Primitive::kPrimFloat: {
589 uint32_t stack_index = stack_index_++;
590 if (float_index_ % 2 == 0) {
591 float_index_ = std::max(double_index_, float_index_);
592 }
593 if (float_index_ < calling_convention.GetNumberOfFpuRegisters()) {
594 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(float_index_++));
595 } else {
596 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index));
597 }
598 }
599
600 case Primitive::kPrimDouble: {
601 double_index_ = std::max(double_index_, RoundUp(float_index_, 2));
602 uint32_t stack_index = stack_index_;
603 stack_index_ += 2;
604 if (double_index_ + 1 < calling_convention.GetNumberOfFpuRegisters()) {
605 uint32_t index = double_index_;
606 double_index_ += 2;
607 return Location::FpuRegisterPairLocation(
608 calling_convention.GetFpuRegisterAt(index),
609 calling_convention.GetFpuRegisterAt(index + 1));
610 } else {
611 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index));
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100612 }
613 }
614
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100615 case Primitive::kPrimVoid:
616 LOG(FATAL) << "Unexpected parameter type " << type;
617 break;
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100618 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100619 return Location();
620}
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100621
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000622Location InvokeDexCallingConventionVisitor::GetReturnLocation(Primitive::Type type) {
623 switch (type) {
624 case Primitive::kPrimBoolean:
625 case Primitive::kPrimByte:
626 case Primitive::kPrimChar:
627 case Primitive::kPrimShort:
628 case Primitive::kPrimInt:
629 case Primitive::kPrimNot: {
630 return Location::RegisterLocation(R0);
631 }
632
633 case Primitive::kPrimFloat: {
634 return Location::FpuRegisterLocation(S0);
635 }
636
637 case Primitive::kPrimLong: {
638 return Location::RegisterPairLocation(R0, R1);
639 }
640
641 case Primitive::kPrimDouble: {
642 return Location::FpuRegisterPairLocation(S0, S1);
643 }
644
645 case Primitive::kPrimVoid:
646 return Location();
647 }
648 UNREACHABLE();
649 return Location();
650}
651
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100652void CodeGeneratorARM::Move32(Location destination, Location source) {
653 if (source.Equals(destination)) {
654 return;
655 }
656 if (destination.IsRegister()) {
657 if (source.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100658 __ Mov(destination.As<Register>(), source.As<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100659 } else if (source.IsFpuRegister()) {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000660 __ vmovrs(destination.As<Register>(), source.As<SRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100661 } else {
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100662 __ LoadFromOffset(kLoadWord, destination.As<Register>(), SP, source.GetStackIndex());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100663 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100664 } else if (destination.IsFpuRegister()) {
665 if (source.IsRegister()) {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000666 __ vmovsr(destination.As<SRegister>(), source.As<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100667 } else if (source.IsFpuRegister()) {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000668 __ vmovs(destination.As<SRegister>(), source.As<SRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100669 } else {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000670 __ LoadSFromOffset(destination.As<SRegister>(), SP, source.GetStackIndex());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100671 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100672 } else {
673 DCHECK(destination.IsStackSlot());
674 if (source.IsRegister()) {
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100675 __ StoreToOffset(kStoreWord, source.As<Register>(), SP, destination.GetStackIndex());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100676 } else if (source.IsFpuRegister()) {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000677 __ StoreSToOffset(source.As<SRegister>(), SP, destination.GetStackIndex());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100678 } else {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100679 DCHECK(source.IsStackSlot());
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100680 __ LoadFromOffset(kLoadWord, IP, SP, source.GetStackIndex());
681 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100682 }
683 }
684}
685
686void CodeGeneratorARM::Move64(Location destination, Location source) {
687 if (source.Equals(destination)) {
688 return;
689 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100690 if (destination.IsRegisterPair()) {
691 if (source.IsRegisterPair()) {
692 __ Mov(destination.AsRegisterPairLow<Register>(), source.AsRegisterPairLow<Register>());
693 __ Mov(destination.AsRegisterPairHigh<Register>(), source.AsRegisterPairHigh<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100694 } else if (source.IsFpuRegister()) {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000695 UNIMPLEMENTED(FATAL);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100696 } else if (source.IsQuickParameter()) {
Nicolas Geoffray0a6c4592014-10-30 16:37:57 +0000697 uint16_t register_index = source.GetQuickParameterRegisterIndex();
698 uint16_t stack_index = source.GetQuickParameterStackIndex();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100699 InvokeDexCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100700 __ Mov(destination.AsRegisterPairLow<Register>(),
Nicolas Geoffray0a6c4592014-10-30 16:37:57 +0000701 calling_convention.GetRegisterAt(register_index));
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100702 __ LoadFromOffset(kLoadWord, destination.AsRegisterPairHigh<Register>(),
Nicolas Geoffray0a6c4592014-10-30 16:37:57 +0000703 SP, calling_convention.GetStackOffsetOf(stack_index + 1) + GetFrameSize());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100704 } else {
705 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100706 if (destination.AsRegisterPairLow<Register>() == R1) {
707 DCHECK_EQ(destination.AsRegisterPairHigh<Register>(), R2);
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100708 __ LoadFromOffset(kLoadWord, R1, SP, source.GetStackIndex());
709 __ LoadFromOffset(kLoadWord, R2, SP, source.GetHighStackIndex(kArmWordSize));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100710 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100711 __ LoadFromOffset(kLoadWordPair, destination.AsRegisterPairLow<Register>(),
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100712 SP, source.GetStackIndex());
713 }
714 }
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000715 } else if (destination.IsFpuRegisterPair()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100716 if (source.IsDoubleStackSlot()) {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000717 __ LoadDFromOffset(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()),
718 SP,
719 source.GetStackIndex());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100720 } else {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000721 UNIMPLEMENTED(FATAL);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100722 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100723 } else if (destination.IsQuickParameter()) {
724 InvokeDexCallingConvention calling_convention;
Nicolas Geoffray0a6c4592014-10-30 16:37:57 +0000725 uint16_t register_index = destination.GetQuickParameterRegisterIndex();
726 uint16_t stack_index = destination.GetQuickParameterStackIndex();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100727 if (source.IsRegisterPair()) {
Nicolas Geoffray0a6c4592014-10-30 16:37:57 +0000728 __ Mov(calling_convention.GetRegisterAt(register_index),
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100729 source.AsRegisterPairLow<Register>());
730 __ StoreToOffset(kStoreWord, source.AsRegisterPairHigh<Register>(),
Nicolas Geoffray0a6c4592014-10-30 16:37:57 +0000731 SP, calling_convention.GetStackOffsetOf(stack_index + 1));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100732 } else if (source.IsFpuRegister()) {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000733 UNIMPLEMENTED(FATAL);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100734 } else {
735 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000736 __ LoadFromOffset(
Nicolas Geoffray0a6c4592014-10-30 16:37:57 +0000737 kLoadWord, calling_convention.GetRegisterAt(register_index), SP, source.GetStackIndex());
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100738 __ LoadFromOffset(kLoadWord, R0, SP, source.GetHighStackIndex(kArmWordSize));
Nicolas Geoffray0a6c4592014-10-30 16:37:57 +0000739 __ StoreToOffset(kStoreWord, R0, SP, calling_convention.GetStackOffsetOf(stack_index + 1));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100740 }
741 } else {
742 DCHECK(destination.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100743 if (source.IsRegisterPair()) {
744 if (source.AsRegisterPairLow<Register>() == R1) {
745 DCHECK_EQ(source.AsRegisterPairHigh<Register>(), R2);
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100746 __ StoreToOffset(kStoreWord, R1, SP, destination.GetStackIndex());
747 __ StoreToOffset(kStoreWord, R2, SP, destination.GetHighStackIndex(kArmWordSize));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100748 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100749 __ StoreToOffset(kStoreWordPair, source.AsRegisterPairLow<Register>(),
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100750 SP, destination.GetStackIndex());
751 }
752 } else if (source.IsQuickParameter()) {
753 InvokeDexCallingConvention calling_convention;
Nicolas Geoffray0a6c4592014-10-30 16:37:57 +0000754 uint16_t register_index = source.GetQuickParameterRegisterIndex();
755 uint16_t stack_index = source.GetQuickParameterStackIndex();
756 __ StoreToOffset(kStoreWord, calling_convention.GetRegisterAt(register_index),
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100757 SP, destination.GetStackIndex());
758 __ LoadFromOffset(kLoadWord, R0,
Nicolas Geoffray0a6c4592014-10-30 16:37:57 +0000759 SP, calling_convention.GetStackOffsetOf(stack_index + 1) + GetFrameSize());
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100760 __ StoreToOffset(kStoreWord, R0, SP, destination.GetHighStackIndex(kArmWordSize));
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000761 } else if (source.IsFpuRegisterPair()) {
762 __ StoreDToOffset(FromLowSToD(source.AsFpuRegisterPairLow<SRegister>()),
763 SP,
764 destination.GetStackIndex());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100765 } else {
766 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100767 __ LoadFromOffset(kLoadWord, IP, SP, source.GetStackIndex());
768 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
769 __ LoadFromOffset(kLoadWord, IP, SP, source.GetHighStackIndex(kArmWordSize));
770 __ StoreToOffset(kStoreWord, IP, SP, destination.GetHighStackIndex(kArmWordSize));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100771 }
772 }
773}
774
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100775void CodeGeneratorARM::Move(HInstruction* instruction, Location location, HInstruction* move_for) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100776 LocationSummary* locations = instruction->GetLocations();
777 if (locations != nullptr && locations->Out().Equals(location)) {
778 return;
779 }
780
Roland Levillain476df552014-10-09 17:51:36 +0100781 if (instruction->IsIntConstant()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100782 int32_t value = instruction->AsIntConstant()->GetValue();
783 if (location.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100784 __ LoadImmediate(location.As<Register>(), value);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100785 } else {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100786 DCHECK(location.IsStackSlot());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +0100787 __ LoadImmediate(IP, value);
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100788 __ StoreToOffset(kStoreWord, IP, SP, location.GetStackIndex());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100789 }
Roland Levillain476df552014-10-09 17:51:36 +0100790 } else if (instruction->IsLongConstant()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100791 int64_t value = instruction->AsLongConstant()->GetValue();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100792 if (location.IsRegisterPair()) {
793 __ LoadImmediate(location.AsRegisterPairLow<Register>(), Low32Bits(value));
794 __ LoadImmediate(location.AsRegisterPairHigh<Register>(), High32Bits(value));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100795 } else {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100796 DCHECK(location.IsDoubleStackSlot());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +0100797 __ LoadImmediate(IP, Low32Bits(value));
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100798 __ StoreToOffset(kStoreWord, IP, SP, location.GetStackIndex());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +0100799 __ LoadImmediate(IP, High32Bits(value));
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100800 __ StoreToOffset(kStoreWord, IP, SP, location.GetHighStackIndex(kArmWordSize));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100801 }
Roland Levillain476df552014-10-09 17:51:36 +0100802 } else if (instruction->IsLoadLocal()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100803 uint32_t stack_slot = GetStackSlot(instruction->AsLoadLocal()->GetLocal());
804 switch (instruction->GetType()) {
805 case Primitive::kPrimBoolean:
806 case Primitive::kPrimByte:
807 case Primitive::kPrimChar:
808 case Primitive::kPrimShort:
809 case Primitive::kPrimInt:
810 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100811 case Primitive::kPrimFloat:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100812 Move32(location, Location::StackSlot(stack_slot));
813 break;
814
815 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100816 case Primitive::kPrimDouble:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100817 Move64(location, Location::DoubleStackSlot(stack_slot));
818 break;
819
820 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100821 LOG(FATAL) << "Unexpected type " << instruction->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100822 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +0000823 } else if (instruction->IsTemporary()) {
824 Location temp_location = GetTemporaryLocation(instruction->AsTemporary());
825 Move32(location, temp_location);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000826 } else {
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100827 DCHECK((instruction->GetNext() == move_for) || instruction->GetNext()->IsTemporary());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100828 switch (instruction->GetType()) {
829 case Primitive::kPrimBoolean:
830 case Primitive::kPrimByte:
831 case Primitive::kPrimChar:
832 case Primitive::kPrimShort:
833 case Primitive::kPrimNot:
834 case Primitive::kPrimInt:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100835 case Primitive::kPrimFloat:
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100836 Move32(location, locations->Out());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100837 break;
838
839 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100840 case Primitive::kPrimDouble:
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100841 Move64(location, locations->Out());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100842 break;
843
844 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100845 LOG(FATAL) << "Unexpected type " << instruction->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100846 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000847 }
848}
849
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100850void CodeGeneratorARM::InvokeRuntime(int32_t entry_point_offset,
851 HInstruction* instruction,
852 uint32_t dex_pc) {
853 __ LoadFromOffset(kLoadWord, LR, TR, entry_point_offset);
854 __ blx(LR);
855 RecordPcInfo(instruction, dex_pc);
856 DCHECK(instruction->IsSuspendCheck()
857 || instruction->IsBoundsCheck()
858 || instruction->IsNullCheck()
Calin Juravled0d48522014-11-04 16:40:20 +0000859 || instruction->IsDivZeroCheck()
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100860 || !IsLeafMethod());
861}
862
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000863void LocationsBuilderARM::VisitGoto(HGoto* got) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000864 got->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000865}
866
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000867void InstructionCodeGeneratorARM::VisitGoto(HGoto* got) {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000868 HBasicBlock* successor = got->GetSuccessor();
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100869 DCHECK(!successor->IsExitBlock());
870
871 HBasicBlock* block = got->GetBlock();
872 HInstruction* previous = got->GetPrevious();
873
874 HLoopInformation* info = block->GetLoopInformation();
875 if (info != nullptr && info->IsBackEdge(block) && info->HasSuspendCheck()) {
876 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
877 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
878 return;
879 }
880
881 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
882 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
883 }
884 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000885 __ b(codegen_->GetLabelOf(successor));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000886 }
887}
888
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000889void LocationsBuilderARM::VisitExit(HExit* exit) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000890 exit->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000891}
892
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000893void InstructionCodeGeneratorARM::VisitExit(HExit* exit) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700894 UNUSED(exit);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000895 if (kIsDebugBuild) {
896 __ Comment("Unreachable");
897 __ bkpt(0);
898 }
899}
900
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000901void LocationsBuilderARM::VisitIf(HIf* if_instr) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100902 LocationSummary* locations =
903 new (GetGraph()->GetArena()) LocationSummary(if_instr, LocationSummary::kNoCall);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100904 HInstruction* cond = if_instr->InputAt(0);
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100905 if (!cond->IsCondition() || cond->AsCondition()->NeedsMaterialization()) {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +0100906 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100907 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000908}
909
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000910void InstructionCodeGeneratorARM::VisitIf(HIf* if_instr) {
Dave Allison20dfc792014-06-16 20:44:29 -0700911 HInstruction* cond = if_instr->InputAt(0);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100912 if (cond->IsIntConstant()) {
913 // Constant condition, statically compared against 1.
914 int32_t cond_value = cond->AsIntConstant()->GetValue();
915 if (cond_value == 1) {
916 if (!codegen_->GoesToNextBlock(if_instr->GetBlock(),
917 if_instr->IfTrueSuccessor())) {
918 __ b(codegen_->GetLabelOf(if_instr->IfTrueSuccessor()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100919 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100920 return;
921 } else {
922 DCHECK_EQ(cond_value, 0);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100923 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100924 } else {
925 if (!cond->IsCondition() || cond->AsCondition()->NeedsMaterialization()) {
926 // Condition has been materialized, compare the output to 0
927 DCHECK(if_instr->GetLocations()->InAt(0).IsRegister());
928 __ cmp(if_instr->GetLocations()->InAt(0).As<Register>(),
929 ShifterOperand(0));
930 __ b(codegen_->GetLabelOf(if_instr->IfTrueSuccessor()), NE);
931 } else {
932 // Condition has not been materialized, use its inputs as the
933 // comparison and its condition as the branch condition.
934 LocationSummary* locations = cond->GetLocations();
935 if (locations->InAt(1).IsRegister()) {
936 __ cmp(locations->InAt(0).As<Register>(),
937 ShifterOperand(locations->InAt(1).As<Register>()));
938 } else {
939 DCHECK(locations->InAt(1).IsConstant());
940 int32_t value =
941 locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
942 ShifterOperand operand;
943 if (ShifterOperand::CanHoldArm(value, &operand)) {
944 __ cmp(locations->InAt(0).As<Register>(), ShifterOperand(value));
945 } else {
946 Register temp = IP;
947 __ LoadImmediate(temp, value);
948 __ cmp(locations->InAt(0).As<Register>(), ShifterOperand(temp));
949 }
950 }
951 __ b(codegen_->GetLabelOf(if_instr->IfTrueSuccessor()),
952 ARMCondition(cond->AsCondition()->GetCondition()));
953 }
Dave Allison20dfc792014-06-16 20:44:29 -0700954 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100955 if (!codegen_->GoesToNextBlock(if_instr->GetBlock(),
956 if_instr->IfFalseSuccessor())) {
Dave Allison20dfc792014-06-16 20:44:29 -0700957 __ b(codegen_->GetLabelOf(if_instr->IfFalseSuccessor()));
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000958 }
959}
960
Dave Allison20dfc792014-06-16 20:44:29 -0700961
962void LocationsBuilderARM::VisitCondition(HCondition* comp) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100963 LocationSummary* locations =
964 new (GetGraph()->GetArena()) LocationSummary(comp, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +0100965 locations->SetInAt(0, Location::RequiresRegister());
966 locations->SetInAt(1, Location::RegisterOrConstant(comp->InputAt(1)));
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100967 if (comp->NeedsMaterialization()) {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +0100968 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100969 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000970}
971
Dave Allison20dfc792014-06-16 20:44:29 -0700972void InstructionCodeGeneratorARM::VisitCondition(HCondition* comp) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100973 if (!comp->NeedsMaterialization()) return;
974
975 LocationSummary* locations = comp->GetLocations();
976 if (locations->InAt(1).IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100977 __ cmp(locations->InAt(0).As<Register>(),
978 ShifterOperand(locations->InAt(1).As<Register>()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100979 } else {
980 DCHECK(locations->InAt(1).IsConstant());
981 int32_t value = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
982 ShifterOperand operand;
983 if (ShifterOperand::CanHoldArm(value, &operand)) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100984 __ cmp(locations->InAt(0).As<Register>(), ShifterOperand(value));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100985 } else {
986 Register temp = IP;
987 __ LoadImmediate(temp, value);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100988 __ cmp(locations->InAt(0).As<Register>(), ShifterOperand(temp));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100989 }
Dave Allison20dfc792014-06-16 20:44:29 -0700990 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100991 __ it(ARMCondition(comp->GetCondition()), kItElse);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100992 __ mov(locations->Out().As<Register>(), ShifterOperand(1),
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100993 ARMCondition(comp->GetCondition()));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100994 __ mov(locations->Out().As<Register>(), ShifterOperand(0),
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100995 ARMOppositeCondition(comp->GetCondition()));
Dave Allison20dfc792014-06-16 20:44:29 -0700996}
997
998void LocationsBuilderARM::VisitEqual(HEqual* comp) {
999 VisitCondition(comp);
1000}
1001
1002void InstructionCodeGeneratorARM::VisitEqual(HEqual* comp) {
1003 VisitCondition(comp);
1004}
1005
1006void LocationsBuilderARM::VisitNotEqual(HNotEqual* comp) {
1007 VisitCondition(comp);
1008}
1009
1010void InstructionCodeGeneratorARM::VisitNotEqual(HNotEqual* comp) {
1011 VisitCondition(comp);
1012}
1013
1014void LocationsBuilderARM::VisitLessThan(HLessThan* comp) {
1015 VisitCondition(comp);
1016}
1017
1018void InstructionCodeGeneratorARM::VisitLessThan(HLessThan* comp) {
1019 VisitCondition(comp);
1020}
1021
1022void LocationsBuilderARM::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
1023 VisitCondition(comp);
1024}
1025
1026void InstructionCodeGeneratorARM::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
1027 VisitCondition(comp);
1028}
1029
1030void LocationsBuilderARM::VisitGreaterThan(HGreaterThan* comp) {
1031 VisitCondition(comp);
1032}
1033
1034void InstructionCodeGeneratorARM::VisitGreaterThan(HGreaterThan* comp) {
1035 VisitCondition(comp);
1036}
1037
1038void LocationsBuilderARM::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
1039 VisitCondition(comp);
1040}
1041
1042void InstructionCodeGeneratorARM::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
1043 VisitCondition(comp);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001044}
1045
1046void LocationsBuilderARM::VisitLocal(HLocal* local) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001047 local->SetLocations(nullptr);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001048}
1049
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001050void InstructionCodeGeneratorARM::VisitLocal(HLocal* local) {
1051 DCHECK_EQ(local->GetBlock(), GetGraph()->GetEntryBlock());
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001052}
1053
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001054void LocationsBuilderARM::VisitLoadLocal(HLoadLocal* load) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001055 load->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001056}
1057
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001058void InstructionCodeGeneratorARM::VisitLoadLocal(HLoadLocal* load) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001059 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001060 UNUSED(load);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001061}
1062
1063void LocationsBuilderARM::VisitStoreLocal(HStoreLocal* store) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001064 LocationSummary* locations =
1065 new (GetGraph()->GetArena()) LocationSummary(store, LocationSummary::kNoCall);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001066 switch (store->InputAt(1)->GetType()) {
1067 case Primitive::kPrimBoolean:
1068 case Primitive::kPrimByte:
1069 case Primitive::kPrimChar:
1070 case Primitive::kPrimShort:
1071 case Primitive::kPrimInt:
1072 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001073 case Primitive::kPrimFloat:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001074 locations->SetInAt(1, Location::StackSlot(codegen_->GetStackSlot(store->GetLocal())));
1075 break;
1076
1077 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001078 case Primitive::kPrimDouble:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001079 locations->SetInAt(1, Location::DoubleStackSlot(codegen_->GetStackSlot(store->GetLocal())));
1080 break;
1081
1082 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001083 LOG(FATAL) << "Unexpected local type " << store->InputAt(1)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001084 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001085}
1086
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001087void InstructionCodeGeneratorARM::VisitStoreLocal(HStoreLocal* store) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001088 UNUSED(store);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001089}
1090
1091void LocationsBuilderARM::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001092 LocationSummary* locations =
1093 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001094 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001095}
1096
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001097void InstructionCodeGeneratorARM::VisitIntConstant(HIntConstant* constant) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001098 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001099 UNUSED(constant);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001100}
1101
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001102void LocationsBuilderARM::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001103 LocationSummary* locations =
1104 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001105 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001106}
1107
1108void InstructionCodeGeneratorARM::VisitLongConstant(HLongConstant* constant) {
1109 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001110 UNUSED(constant);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001111}
1112
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001113void LocationsBuilderARM::VisitFloatConstant(HFloatConstant* constant) {
1114 LocationSummary* locations =
1115 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1116 locations->SetOut(Location::ConstantLocation(constant));
1117}
1118
1119void InstructionCodeGeneratorARM::VisitFloatConstant(HFloatConstant* constant) {
1120 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001121 UNUSED(constant);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001122}
1123
1124void LocationsBuilderARM::VisitDoubleConstant(HDoubleConstant* constant) {
1125 LocationSummary* locations =
1126 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1127 locations->SetOut(Location::ConstantLocation(constant));
1128}
1129
1130void InstructionCodeGeneratorARM::VisitDoubleConstant(HDoubleConstant* constant) {
1131 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001132 UNUSED(constant);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001133}
1134
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001135void LocationsBuilderARM::VisitReturnVoid(HReturnVoid* ret) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001136 ret->SetLocations(nullptr);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001137}
1138
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001139void InstructionCodeGeneratorARM::VisitReturnVoid(HReturnVoid* ret) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001140 UNUSED(ret);
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001141 codegen_->GenerateFrameExit();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001142}
1143
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001144void LocationsBuilderARM::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001145 LocationSummary* locations =
1146 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001147 locations->SetInAt(0, parameter_visitor_.GetReturnLocation(ret->InputAt(0)->GetType()));
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001148}
1149
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001150void InstructionCodeGeneratorARM::VisitReturn(HReturn* ret) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001151 UNUSED(ret);
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001152 codegen_->GenerateFrameExit();
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001153}
1154
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001155void LocationsBuilderARM::VisitInvokeStatic(HInvokeStatic* invoke) {
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001156 HandleInvoke(invoke);
1157}
1158
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001159void CodeGeneratorARM::LoadCurrentMethod(Register reg) {
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001160 __ LoadFromOffset(kLoadWord, reg, SP, kCurrentMethodStackOffset);
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001161}
1162
1163void InstructionCodeGeneratorARM::VisitInvokeStatic(HInvokeStatic* invoke) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001164 Register temp = invoke->GetLocations()->GetTemp(0).As<Register>();
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001165
1166 // TODO: Implement all kinds of calls:
1167 // 1) boot -> boot
1168 // 2) app -> boot
1169 // 3) app -> app
1170 //
1171 // Currently we implement the app -> app logic, which looks up in the resolve cache.
1172
1173 // temp = method;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001174 codegen_->LoadCurrentMethod(temp);
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001175 // temp = temp->dex_cache_resolved_methods_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001176 __ LoadFromOffset(
1177 kLoadWord, temp, temp, mirror::ArtMethod::DexCacheResolvedMethodsOffset().Int32Value());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001178 // temp = temp[index_in_cache]
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001179 __ LoadFromOffset(
1180 kLoadWord, temp, temp, CodeGenerator::GetCacheOffset(invoke->GetIndexInDexCache()));
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001181 // LR = temp[offset_of_quick_compiled_code]
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001182 __ LoadFromOffset(kLoadWord, LR, temp,
1183 mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset().Int32Value());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001184 // LR()
1185 __ blx(LR);
1186
1187 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
1188 DCHECK(!codegen_->IsLeafMethod());
1189}
1190
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001191void LocationsBuilderARM::HandleInvoke(HInvoke* invoke) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001192 LocationSummary* locations =
1193 new (GetGraph()->GetArena()) LocationSummary(invoke, LocationSummary::kCall);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001194 locations->AddTemp(Location::RegisterLocation(R0));
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001195
1196 InvokeDexCallingConventionVisitor calling_convention_visitor;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001197 for (size_t i = 0; i < invoke->InputCount(); i++) {
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001198 HInstruction* input = invoke->InputAt(i);
1199 locations->SetInAt(i, calling_convention_visitor.GetNextLocation(input->GetType()));
1200 }
1201
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001202 locations->SetOut(calling_convention_visitor.GetReturnLocation(invoke->GetType()));
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001203}
1204
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001205void LocationsBuilderARM::VisitInvokeVirtual(HInvokeVirtual* invoke) {
1206 HandleInvoke(invoke);
1207}
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001208
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001209void InstructionCodeGeneratorARM::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001210 Register temp = invoke->GetLocations()->GetTemp(0).As<Register>();
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001211 uint32_t method_offset = mirror::Class::EmbeddedVTableOffset().Uint32Value() +
1212 invoke->GetVTableIndex() * sizeof(mirror::Class::VTableEntry);
1213 LocationSummary* locations = invoke->GetLocations();
1214 Location receiver = locations->InAt(0);
1215 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
1216 // temp = object->GetClass();
1217 if (receiver.IsStackSlot()) {
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001218 __ LoadFromOffset(kLoadWord, temp, SP, receiver.GetStackIndex());
1219 __ LoadFromOffset(kLoadWord, temp, temp, class_offset);
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001220 } else {
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001221 __ LoadFromOffset(kLoadWord, temp, receiver.As<Register>(), class_offset);
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001222 }
1223 // temp = temp->GetMethodAt(method_offset);
1224 uint32_t entry_point = mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset().Int32Value();
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001225 __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001226 // LR = temp->GetEntryPoint();
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001227 __ LoadFromOffset(kLoadWord, LR, temp, entry_point);
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001228 // LR();
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001229 __ blx(LR);
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001230 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001231 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001232}
1233
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001234void LocationsBuilderARM::VisitInvokeInterface(HInvokeInterface* invoke) {
1235 HandleInvoke(invoke);
1236 // Add the hidden argument.
1237 invoke->GetLocations()->AddTemp(Location::RegisterLocation(R12));
1238}
1239
1240void InstructionCodeGeneratorARM::VisitInvokeInterface(HInvokeInterface* invoke) {
1241 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
1242 Register temp = invoke->GetLocations()->GetTemp(0).As<Register>();
1243 uint32_t method_offset = mirror::Class::EmbeddedImTableOffset().Uint32Value() +
1244 (invoke->GetImtIndex() % mirror::Class::kImtSize) * sizeof(mirror::Class::ImTableEntry);
1245 LocationSummary* locations = invoke->GetLocations();
1246 Location receiver = locations->InAt(0);
1247 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
1248
1249 // Set the hidden argument.
1250 __ LoadImmediate(invoke->GetLocations()->GetTemp(1).As<Register>(), invoke->GetDexMethodIndex());
1251
1252 // temp = object->GetClass();
1253 if (receiver.IsStackSlot()) {
1254 __ LoadFromOffset(kLoadWord, temp, SP, receiver.GetStackIndex());
1255 __ LoadFromOffset(kLoadWord, temp, temp, class_offset);
1256 } else {
1257 __ LoadFromOffset(kLoadWord, temp, receiver.As<Register>(), class_offset);
1258 }
1259 // temp = temp->GetImtEntryAt(method_offset);
1260 uint32_t entry_point = mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset().Int32Value();
1261 __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
1262 // LR = temp->GetEntryPoint();
1263 __ LoadFromOffset(kLoadWord, LR, temp, entry_point);
1264 // LR();
1265 __ blx(LR);
1266 DCHECK(!codegen_->IsLeafMethod());
1267 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
1268}
1269
Roland Levillain88cb1752014-10-20 16:36:47 +01001270void LocationsBuilderARM::VisitNeg(HNeg* neg) {
1271 LocationSummary* locations =
1272 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
1273 switch (neg->GetResultType()) {
1274 case Primitive::kPrimInt:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001275 case Primitive::kPrimLong: {
1276 bool output_overlaps = (neg->GetResultType() == Primitive::kPrimLong);
Roland Levillain88cb1752014-10-20 16:36:47 +01001277 locations->SetInAt(0, Location::RequiresRegister());
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001278 locations->SetOut(Location::RequiresRegister(), output_overlaps);
Roland Levillain88cb1752014-10-20 16:36:47 +01001279 break;
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001280 }
Roland Levillain88cb1752014-10-20 16:36:47 +01001281
Roland Levillain88cb1752014-10-20 16:36:47 +01001282 case Primitive::kPrimFloat:
1283 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00001284 locations->SetInAt(0, Location::RequiresFpuRegister());
1285 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillain88cb1752014-10-20 16:36:47 +01001286 break;
1287
1288 default:
1289 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1290 }
1291}
1292
1293void InstructionCodeGeneratorARM::VisitNeg(HNeg* neg) {
1294 LocationSummary* locations = neg->GetLocations();
1295 Location out = locations->Out();
1296 Location in = locations->InAt(0);
1297 switch (neg->GetResultType()) {
1298 case Primitive::kPrimInt:
1299 DCHECK(in.IsRegister());
Roland Levillainb762d2e2014-10-22 10:11:06 +01001300 __ rsb(out.As<Register>(), in.As<Register>(), ShifterOperand(0));
Roland Levillain88cb1752014-10-20 16:36:47 +01001301 break;
1302
1303 case Primitive::kPrimLong:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001304 DCHECK(in.IsRegisterPair());
1305 // out.lo = 0 - in.lo (and update the carry/borrow (C) flag)
1306 __ rsbs(out.AsRegisterPairLow<Register>(),
1307 in.AsRegisterPairLow<Register>(),
1308 ShifterOperand(0));
1309 // We cannot emit an RSC (Reverse Subtract with Carry)
1310 // instruction here, as it does not exist in the Thumb-2
1311 // instruction set. We use the following approach
1312 // using SBC and SUB instead.
1313 //
1314 // out.hi = -C
1315 __ sbc(out.AsRegisterPairHigh<Register>(),
1316 out.AsRegisterPairHigh<Register>(),
1317 ShifterOperand(out.AsRegisterPairHigh<Register>()));
1318 // out.hi = out.hi - in.hi
1319 __ sub(out.AsRegisterPairHigh<Register>(),
1320 out.AsRegisterPairHigh<Register>(),
1321 ShifterOperand(in.AsRegisterPairHigh<Register>()));
1322 break;
1323
Roland Levillain88cb1752014-10-20 16:36:47 +01001324 case Primitive::kPrimFloat:
Roland Levillain3dbcb382014-10-28 17:30:07 +00001325 DCHECK(in.IsFpuRegister());
1326 __ vnegs(out.As<SRegister>(), in.As<SRegister>());
1327 break;
1328
Roland Levillain88cb1752014-10-20 16:36:47 +01001329 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00001330 DCHECK(in.IsFpuRegisterPair());
1331 __ vnegd(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
1332 FromLowSToD(in.AsFpuRegisterPairLow<SRegister>()));
Roland Levillain88cb1752014-10-20 16:36:47 +01001333 break;
1334
1335 default:
1336 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1337 }
1338}
1339
Roland Levillaindff1f282014-11-05 14:15:05 +00001340void LocationsBuilderARM::VisitTypeConversion(HTypeConversion* conversion) {
1341 LocationSummary* locations =
1342 new (GetGraph()->GetArena()) LocationSummary(conversion, LocationSummary::kNoCall);
1343 Primitive::Type result_type = conversion->GetResultType();
1344 Primitive::Type input_type = conversion->GetInputType();
1345 switch (result_type) {
Roland Levillain946e1432014-11-11 17:35:19 +00001346 case Primitive::kPrimInt:
1347 switch (input_type) {
1348 case Primitive::kPrimLong:
1349 // long-to-int conversion.
1350 locations->SetInAt(0, Location::Any());
1351 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1352 break;
1353
1354 case Primitive::kPrimFloat:
1355 case Primitive::kPrimDouble:
1356 LOG(FATAL) << "Type conversion from " << input_type
1357 << " to " << result_type << " not yet implemented";
1358 break;
1359
1360 default:
1361 LOG(FATAL) << "Unexpected type conversion from " << input_type
1362 << " to " << result_type;
1363 }
1364 break;
1365
Roland Levillaindff1f282014-11-05 14:15:05 +00001366 case Primitive::kPrimLong:
1367 switch (input_type) {
1368 case Primitive::kPrimByte:
1369 case Primitive::kPrimShort:
1370 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00001371 case Primitive::kPrimChar:
Roland Levillaindff1f282014-11-05 14:15:05 +00001372 // int-to-long conversion.
1373 locations->SetInAt(0, Location::RequiresRegister());
1374 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1375 break;
1376
1377 case Primitive::kPrimFloat:
1378 case Primitive::kPrimDouble:
1379 LOG(FATAL) << "Type conversion from " << input_type << " to "
1380 << result_type << " not yet implemented";
1381 break;
1382
1383 default:
1384 LOG(FATAL) << "Unexpected type conversion from " << input_type
1385 << " to " << result_type;
1386 }
1387 break;
1388
Roland Levillaindff1f282014-11-05 14:15:05 +00001389 case Primitive::kPrimFloat:
1390 case Primitive::kPrimDouble:
1391 LOG(FATAL) << "Type conversion from " << input_type
1392 << " to " << result_type << " not yet implemented";
1393 break;
1394
1395 default:
1396 LOG(FATAL) << "Unexpected type conversion from " << input_type
1397 << " to " << result_type;
1398 }
1399}
1400
1401void InstructionCodeGeneratorARM::VisitTypeConversion(HTypeConversion* conversion) {
1402 LocationSummary* locations = conversion->GetLocations();
1403 Location out = locations->Out();
1404 Location in = locations->InAt(0);
1405 Primitive::Type result_type = conversion->GetResultType();
1406 Primitive::Type input_type = conversion->GetInputType();
1407 switch (result_type) {
Roland Levillain946e1432014-11-11 17:35:19 +00001408 case Primitive::kPrimInt:
1409 switch (input_type) {
1410 case Primitive::kPrimLong:
1411 // long-to-int conversion.
1412 DCHECK(out.IsRegister());
1413 if (in.IsRegisterPair()) {
1414 __ Mov(out.As<Register>(), in.AsRegisterPairLow<Register>());
1415 } else if (in.IsDoubleStackSlot()) {
1416 __ LoadFromOffset(kLoadWord, out.As<Register>(), SP, in.GetStackIndex());
1417 } else {
1418 DCHECK(in.IsConstant());
1419 DCHECK(in.GetConstant()->IsLongConstant());
1420 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
1421 __ LoadImmediate(out.As<Register>(), static_cast<int32_t>(value));
1422 }
1423 break;
1424
1425 case Primitive::kPrimFloat:
1426 case Primitive::kPrimDouble:
1427 LOG(FATAL) << "Type conversion from " << input_type
1428 << " to " << result_type << " not yet implemented";
1429 break;
1430
1431 default:
1432 LOG(FATAL) << "Unexpected type conversion from " << input_type
1433 << " to " << result_type;
1434 }
1435 break;
1436
Roland Levillaindff1f282014-11-05 14:15:05 +00001437 case Primitive::kPrimLong:
1438 switch (input_type) {
1439 case Primitive::kPrimByte:
1440 case Primitive::kPrimShort:
1441 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00001442 case Primitive::kPrimChar:
Roland Levillaindff1f282014-11-05 14:15:05 +00001443 // int-to-long conversion.
1444 DCHECK(out.IsRegisterPair());
1445 DCHECK(in.IsRegister());
1446 __ Mov(out.AsRegisterPairLow<Register>(), in.As<Register>());
1447 // Sign extension.
1448 __ Asr(out.AsRegisterPairHigh<Register>(),
1449 out.AsRegisterPairLow<Register>(),
1450 31);
1451 break;
1452
1453 case Primitive::kPrimFloat:
1454 case Primitive::kPrimDouble:
1455 LOG(FATAL) << "Type conversion from " << input_type << " to "
1456 << result_type << " not yet implemented";
1457 break;
1458
1459 default:
1460 LOG(FATAL) << "Unexpected type conversion from " << input_type
1461 << " to " << result_type;
1462 }
1463 break;
1464
Roland Levillaindff1f282014-11-05 14:15:05 +00001465 case Primitive::kPrimFloat:
1466 case Primitive::kPrimDouble:
1467 LOG(FATAL) << "Type conversion from " << input_type
1468 << " to " << result_type << " not yet implemented";
1469 break;
1470
1471 default:
1472 LOG(FATAL) << "Unexpected type conversion from " << input_type
1473 << " to " << result_type;
1474 }
1475}
1476
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001477void LocationsBuilderARM::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001478 LocationSummary* locations =
1479 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001480 switch (add->GetResultType()) {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001481 case Primitive::kPrimInt:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001482 case Primitive::kPrimLong: {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01001483 bool output_overlaps = (add->GetResultType() == Primitive::kPrimLong);
1484 locations->SetInAt(0, Location::RequiresRegister());
1485 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
1486 locations->SetOut(Location::RequiresRegister(), output_overlaps);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001487 break;
1488 }
1489
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001490 case Primitive::kPrimFloat:
1491 case Primitive::kPrimDouble: {
1492 locations->SetInAt(0, Location::RequiresFpuRegister());
1493 locations->SetInAt(1, Location::RequiresFpuRegister());
Calin Juravle7c4954d2014-10-28 16:57:40 +00001494 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001495 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001496 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001497
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001498 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001499 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001500 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001501}
1502
1503void InstructionCodeGeneratorARM::VisitAdd(HAdd* add) {
1504 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001505 Location out = locations->Out();
1506 Location first = locations->InAt(0);
1507 Location second = locations->InAt(1);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001508 switch (add->GetResultType()) {
1509 case Primitive::kPrimInt:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001510 if (second.IsRegister()) {
1511 __ add(out.As<Register>(), first.As<Register>(), ShifterOperand(second.As<Register>()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001512 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001513 __ AddConstant(out.As<Register>(),
1514 first.As<Register>(),
1515 second.GetConstant()->AsIntConstant()->GetValue());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001516 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001517 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001518
1519 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001520 __ adds(out.AsRegisterPairLow<Register>(),
1521 first.AsRegisterPairLow<Register>(),
1522 ShifterOperand(second.AsRegisterPairLow<Register>()));
1523 __ adc(out.AsRegisterPairHigh<Register>(),
1524 first.AsRegisterPairHigh<Register>(),
1525 ShifterOperand(second.AsRegisterPairHigh<Register>()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001526 break;
1527
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001528 case Primitive::kPrimFloat:
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001529 __ vadds(out.As<SRegister>(), first.As<SRegister>(), second.As<SRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001530 break;
1531
1532 case Primitive::kPrimDouble:
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001533 __ vaddd(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
1534 FromLowSToD(first.AsFpuRegisterPairLow<SRegister>()),
1535 FromLowSToD(second.AsFpuRegisterPairLow<SRegister>()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001536 break;
1537
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001538 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001539 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001540 }
1541}
1542
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001543void LocationsBuilderARM::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001544 LocationSummary* locations =
1545 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001546 switch (sub->GetResultType()) {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001547 case Primitive::kPrimInt:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001548 case Primitive::kPrimLong: {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01001549 bool output_overlaps = (sub->GetResultType() == Primitive::kPrimLong);
1550 locations->SetInAt(0, Location::RequiresRegister());
1551 locations->SetInAt(1, Location::RegisterOrConstant(sub->InputAt(1)));
1552 locations->SetOut(Location::RequiresRegister(), output_overlaps);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001553 break;
1554 }
Calin Juravle11351682014-10-23 15:38:15 +01001555 case Primitive::kPrimFloat:
1556 case Primitive::kPrimDouble: {
1557 locations->SetInAt(0, Location::RequiresFpuRegister());
1558 locations->SetInAt(1, Location::RequiresFpuRegister());
Calin Juravle7c4954d2014-10-28 16:57:40 +00001559 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001560 break;
Calin Juravle11351682014-10-23 15:38:15 +01001561 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001562 default:
Calin Juravle11351682014-10-23 15:38:15 +01001563 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001564 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001565}
1566
1567void InstructionCodeGeneratorARM::VisitSub(HSub* sub) {
1568 LocationSummary* locations = sub->GetLocations();
Calin Juravle11351682014-10-23 15:38:15 +01001569 Location out = locations->Out();
1570 Location first = locations->InAt(0);
1571 Location second = locations->InAt(1);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001572 switch (sub->GetResultType()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001573 case Primitive::kPrimInt: {
Calin Juravle11351682014-10-23 15:38:15 +01001574 if (second.IsRegister()) {
1575 __ sub(out.As<Register>(), first.As<Register>(), ShifterOperand(second.As<Register>()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001576 } else {
Calin Juravle11351682014-10-23 15:38:15 +01001577 __ AddConstant(out.As<Register>(),
1578 first.As<Register>(),
1579 -second.GetConstant()->AsIntConstant()->GetValue());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001580 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001581 break;
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001582 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001583
Calin Juravle11351682014-10-23 15:38:15 +01001584 case Primitive::kPrimLong: {
1585 __ subs(out.AsRegisterPairLow<Register>(),
1586 first.AsRegisterPairLow<Register>(),
1587 ShifterOperand(second.AsRegisterPairLow<Register>()));
1588 __ sbc(out.AsRegisterPairHigh<Register>(),
1589 first.AsRegisterPairHigh<Register>(),
1590 ShifterOperand(second.AsRegisterPairHigh<Register>()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001591 break;
Calin Juravle11351682014-10-23 15:38:15 +01001592 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001593
Calin Juravle11351682014-10-23 15:38:15 +01001594 case Primitive::kPrimFloat: {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001595 __ vsubs(out.As<SRegister>(), first.As<SRegister>(), second.As<SRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001596 break;
Calin Juravle11351682014-10-23 15:38:15 +01001597 }
1598
1599 case Primitive::kPrimDouble: {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001600 __ vsubd(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
1601 FromLowSToD(first.AsFpuRegisterPairLow<SRegister>()),
1602 FromLowSToD(second.AsFpuRegisterPairLow<SRegister>()));
Calin Juravle11351682014-10-23 15:38:15 +01001603 break;
1604 }
1605
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001606
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001607 default:
Calin Juravle11351682014-10-23 15:38:15 +01001608 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001609 }
1610}
1611
Calin Juravle34bacdf2014-10-07 20:23:36 +01001612void LocationsBuilderARM::VisitMul(HMul* mul) {
1613 LocationSummary* locations =
1614 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
1615 switch (mul->GetResultType()) {
1616 case Primitive::kPrimInt:
1617 case Primitive::kPrimLong: {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01001618 locations->SetInAt(0, Location::RequiresRegister());
1619 locations->SetInAt(1, Location::RequiresRegister());
1620 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Calin Juravle34bacdf2014-10-07 20:23:36 +01001621 break;
1622 }
1623
Calin Juravleb5bfa962014-10-21 18:02:24 +01001624 case Primitive::kPrimFloat:
1625 case Primitive::kPrimDouble: {
1626 locations->SetInAt(0, Location::RequiresFpuRegister());
1627 locations->SetInAt(1, Location::RequiresFpuRegister());
Calin Juravle7c4954d2014-10-28 16:57:40 +00001628 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Calin Juravle34bacdf2014-10-07 20:23:36 +01001629 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01001630 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01001631
1632 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01001633 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01001634 }
1635}
1636
1637void InstructionCodeGeneratorARM::VisitMul(HMul* mul) {
1638 LocationSummary* locations = mul->GetLocations();
1639 Location out = locations->Out();
1640 Location first = locations->InAt(0);
1641 Location second = locations->InAt(1);
1642 switch (mul->GetResultType()) {
1643 case Primitive::kPrimInt: {
1644 __ mul(out.As<Register>(), first.As<Register>(), second.As<Register>());
1645 break;
1646 }
1647 case Primitive::kPrimLong: {
1648 Register out_hi = out.AsRegisterPairHigh<Register>();
1649 Register out_lo = out.AsRegisterPairLow<Register>();
1650 Register in1_hi = first.AsRegisterPairHigh<Register>();
1651 Register in1_lo = first.AsRegisterPairLow<Register>();
1652 Register in2_hi = second.AsRegisterPairHigh<Register>();
1653 Register in2_lo = second.AsRegisterPairLow<Register>();
1654
1655 // Extra checks to protect caused by the existence of R1_R2.
1656 // The algorithm is wrong if out.hi is either in1.lo or in2.lo:
1657 // (e.g. in1=r0_r1, in2=r2_r3 and out=r1_r2);
1658 DCHECK_NE(out_hi, in1_lo);
1659 DCHECK_NE(out_hi, in2_lo);
1660
1661 // input: in1 - 64 bits, in2 - 64 bits
1662 // output: out
1663 // formula: out.hi : out.lo = (in1.lo * in2.hi + in1.hi * in2.lo)* 2^32 + in1.lo * in2.lo
1664 // parts: out.hi = in1.lo * in2.hi + in1.hi * in2.lo + (in1.lo * in2.lo)[63:32]
1665 // parts: out.lo = (in1.lo * in2.lo)[31:0]
1666
1667 // IP <- in1.lo * in2.hi
1668 __ mul(IP, in1_lo, in2_hi);
1669 // out.hi <- in1.lo * in2.hi + in1.hi * in2.lo
1670 __ mla(out_hi, in1_hi, in2_lo, IP);
1671 // out.lo <- (in1.lo * in2.lo)[31:0];
1672 __ umull(out_lo, IP, in1_lo, in2_lo);
1673 // out.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
1674 __ add(out_hi, out_hi, ShifterOperand(IP));
1675 break;
1676 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01001677
1678 case Primitive::kPrimFloat: {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001679 __ vmuls(out.As<SRegister>(), first.As<SRegister>(), second.As<SRegister>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01001680 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01001681 }
1682
1683 case Primitive::kPrimDouble: {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001684 __ vmuld(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
1685 FromLowSToD(first.AsFpuRegisterPairLow<SRegister>()),
1686 FromLowSToD(second.AsFpuRegisterPairLow<SRegister>()));
Calin Juravleb5bfa962014-10-21 18:02:24 +01001687 break;
1688 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01001689
1690 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01001691 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01001692 }
1693}
1694
Calin Juravle7c4954d2014-10-28 16:57:40 +00001695void LocationsBuilderARM::VisitDiv(HDiv* div) {
1696 LocationSummary* locations =
1697 new (GetGraph()->GetArena()) LocationSummary(div, LocationSummary::kNoCall);
1698 switch (div->GetResultType()) {
Calin Juravled0d48522014-11-04 16:40:20 +00001699 case Primitive::kPrimInt: {
1700 locations->SetInAt(0, Location::RequiresRegister());
1701 locations->SetInAt(1, Location::RequiresRegister());
1702 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1703 break;
1704 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00001705 case Primitive::kPrimLong: {
1706 LOG(FATAL) << "Not implemented div type" << div->GetResultType();
1707 break;
1708 }
1709 case Primitive::kPrimFloat:
1710 case Primitive::kPrimDouble: {
1711 locations->SetInAt(0, Location::RequiresFpuRegister());
1712 locations->SetInAt(1, Location::RequiresFpuRegister());
1713 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
1714 break;
1715 }
1716
1717 default:
1718 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
1719 }
1720}
1721
1722void InstructionCodeGeneratorARM::VisitDiv(HDiv* div) {
1723 LocationSummary* locations = div->GetLocations();
1724 Location out = locations->Out();
1725 Location first = locations->InAt(0);
1726 Location second = locations->InAt(1);
1727
1728 switch (div->GetResultType()) {
Calin Juravled0d48522014-11-04 16:40:20 +00001729 case Primitive::kPrimInt: {
1730 __ sdiv(out.As<Register>(), first.As<Register>(), second.As<Register>());
1731 break;
1732 }
1733
Calin Juravle7c4954d2014-10-28 16:57:40 +00001734 case Primitive::kPrimLong: {
1735 LOG(FATAL) << "Not implemented div type" << div->GetResultType();
1736 break;
1737 }
1738
1739 case Primitive::kPrimFloat: {
1740 __ vdivs(out.As<SRegister>(), first.As<SRegister>(), second.As<SRegister>());
1741 break;
1742 }
1743
1744 case Primitive::kPrimDouble: {
1745 __ vdivd(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
1746 FromLowSToD(first.AsFpuRegisterPairLow<SRegister>()),
1747 FromLowSToD(second.AsFpuRegisterPairLow<SRegister>()));
1748 break;
1749 }
1750
1751 default:
1752 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
1753 }
1754}
1755
Calin Juravled0d48522014-11-04 16:40:20 +00001756void LocationsBuilderARM::VisitDivZeroCheck(HDivZeroCheck* instruction) {
1757 LocationSummary* locations =
1758 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1759 locations->SetInAt(0, Location::RequiresRegister());
1760 if (instruction->HasUses()) {
1761 locations->SetOut(Location::SameAsFirstInput());
1762 }
1763}
1764
1765void InstructionCodeGeneratorARM::VisitDivZeroCheck(HDivZeroCheck* instruction) {
1766 SlowPathCodeARM* slow_path = new (GetGraph()->GetArena()) DivZeroCheckSlowPathARM(instruction);
1767 codegen_->AddSlowPath(slow_path);
1768
1769 LocationSummary* locations = instruction->GetLocations();
1770 Location value = locations->InAt(0);
1771
1772 DCHECK(value.IsRegister()) << value;
1773 __ cmp(value.As<Register>(), ShifterOperand(0));
1774 __ b(slow_path->GetEntryLabel(), EQ);
1775}
1776
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001777void LocationsBuilderARM::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001778 LocationSummary* locations =
1779 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001780 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001781 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1782 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
1783 locations->SetOut(Location::RegisterLocation(R0));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001784}
1785
1786void InstructionCodeGeneratorARM::VisitNewInstance(HNewInstance* instruction) {
1787 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001788 codegen_->LoadCurrentMethod(calling_convention.GetRegisterAt(1));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001789 __ LoadImmediate(calling_convention.GetRegisterAt(0), instruction->GetTypeIndex());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001790 codegen_->InvokeRuntime(
1791 QUICK_ENTRY_POINT(pAllocObjectWithAccessCheck), instruction, instruction->GetDexPc());
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001792}
1793
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001794void LocationsBuilderARM::VisitNewArray(HNewArray* instruction) {
1795 LocationSummary* locations =
1796 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
1797 InvokeRuntimeCallingConvention calling_convention;
1798 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1799 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
1800 locations->SetOut(Location::RegisterLocation(R0));
1801 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
1802}
1803
1804void InstructionCodeGeneratorARM::VisitNewArray(HNewArray* instruction) {
1805 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001806 codegen_->LoadCurrentMethod(calling_convention.GetRegisterAt(1));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001807 __ LoadImmediate(calling_convention.GetRegisterAt(0), instruction->GetTypeIndex());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001808 codegen_->InvokeRuntime(
1809 QUICK_ENTRY_POINT(pAllocArrayWithAccessCheck), instruction, instruction->GetDexPc());
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001810}
1811
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001812void LocationsBuilderARM::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001813 LocationSummary* locations =
1814 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001815 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
1816 if (location.IsStackSlot()) {
1817 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
1818 } else if (location.IsDoubleStackSlot()) {
1819 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001820 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001821 locations->SetOut(location);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001822}
1823
1824void InstructionCodeGeneratorARM::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001825 // Nothing to do, the parameter is already at its location.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001826 UNUSED(instruction);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001827}
1828
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001829void LocationsBuilderARM::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001830 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001831 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01001832 locations->SetInAt(0, Location::RequiresRegister());
1833 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01001834}
1835
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001836void InstructionCodeGeneratorARM::VisitNot(HNot* not_) {
1837 LocationSummary* locations = not_->GetLocations();
1838 Location out = locations->Out();
1839 Location in = locations->InAt(0);
1840 switch (not_->InputAt(0)->GetType()) {
1841 case Primitive::kPrimBoolean:
1842 __ eor(out.As<Register>(), in.As<Register>(), ShifterOperand(1));
1843 break;
1844
1845 case Primitive::kPrimInt:
1846 __ mvn(out.As<Register>(), ShifterOperand(in.As<Register>()));
1847 break;
1848
1849 case Primitive::kPrimLong:
Roland Levillain70566432014-10-24 16:20:17 +01001850 __ mvn(out.AsRegisterPairLow<Register>(),
1851 ShifterOperand(in.AsRegisterPairLow<Register>()));
1852 __ mvn(out.AsRegisterPairHigh<Register>(),
1853 ShifterOperand(in.AsRegisterPairHigh<Register>()));
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001854 break;
1855
1856 default:
1857 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
1858 }
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01001859}
1860
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001861void LocationsBuilderARM::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001862 LocationSummary* locations =
1863 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01001864 locations->SetInAt(0, Location::RequiresRegister());
1865 locations->SetInAt(1, Location::RequiresRegister());
1866 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001867}
1868
1869void InstructionCodeGeneratorARM::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001870 LocationSummary* locations = compare->GetLocations();
1871 switch (compare->InputAt(0)->GetType()) {
1872 case Primitive::kPrimLong: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001873 Register output = locations->Out().As<Register>();
1874 Location left = locations->InAt(0);
1875 Location right = locations->InAt(1);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001876 Label less, greater, done;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001877 __ cmp(left.AsRegisterPairHigh<Register>(),
1878 ShifterOperand(right.AsRegisterPairHigh<Register>())); // Signed compare.
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001879 __ b(&less, LT);
1880 __ b(&greater, GT);
Nicolas Geoffray8d486732014-07-16 16:23:40 +01001881 // Do LoadImmediate before any `cmp`, as LoadImmediate might affect
1882 // the status flags.
1883 __ LoadImmediate(output, 0);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001884 __ cmp(left.AsRegisterPairLow<Register>(),
1885 ShifterOperand(right.AsRegisterPairLow<Register>())); // Unsigned compare.
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001886 __ b(&done, EQ);
1887 __ b(&less, CC);
1888
1889 __ Bind(&greater);
1890 __ LoadImmediate(output, 1);
1891 __ b(&done);
1892
1893 __ Bind(&less);
1894 __ LoadImmediate(output, -1);
1895
1896 __ Bind(&done);
1897 break;
1898 }
1899 default:
1900 LOG(FATAL) << "Unimplemented compare type " << compare->InputAt(0)->GetType();
1901 }
1902}
1903
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001904void LocationsBuilderARM::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001905 LocationSummary* locations =
1906 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01001907 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
1908 locations->SetInAt(i, Location::Any());
1909 }
1910 locations->SetOut(Location::Any());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001911}
1912
1913void InstructionCodeGeneratorARM::VisitPhi(HPhi* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001914 UNUSED(instruction);
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01001915 LOG(FATAL) << "Unreachable";
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001916}
1917
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001918void LocationsBuilderARM::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001919 LocationSummary* locations =
1920 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01001921 bool is_object_type = instruction->GetFieldType() == Primitive::kPrimNot;
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01001922 locations->SetInAt(0, Location::RequiresRegister());
1923 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01001924 // Temporary registers for the write barrier.
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01001925 if (is_object_type) {
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01001926 locations->AddTemp(Location::RequiresRegister());
1927 locations->AddTemp(Location::RequiresRegister());
1928 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001929}
1930
1931void InstructionCodeGeneratorARM::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
1932 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001933 Register obj = locations->InAt(0).As<Register>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001934 uint32_t offset = instruction->GetFieldOffset().Uint32Value();
Nicolas Geoffray39468442014-09-02 15:17:15 +01001935 Primitive::Type field_type = instruction->GetFieldType();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001936
1937 switch (field_type) {
1938 case Primitive::kPrimBoolean:
1939 case Primitive::kPrimByte: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001940 Register value = locations->InAt(1).As<Register>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001941 __ StoreToOffset(kStoreByte, value, obj, offset);
1942 break;
1943 }
1944
1945 case Primitive::kPrimShort:
1946 case Primitive::kPrimChar: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001947 Register value = locations->InAt(1).As<Register>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001948 __ StoreToOffset(kStoreHalfword, value, obj, offset);
1949 break;
1950 }
1951
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001952 case Primitive::kPrimInt:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001953 case Primitive::kPrimNot: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001954 Register value = locations->InAt(1).As<Register>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001955 __ StoreToOffset(kStoreWord, value, obj, offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001956 if (field_type == Primitive::kPrimNot) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001957 Register temp = locations->GetTemp(0).As<Register>();
1958 Register card = locations->GetTemp(1).As<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001959 codegen_->MarkGCCard(temp, card, obj, value);
1960 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001961 break;
1962 }
1963
1964 case Primitive::kPrimLong: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001965 Location value = locations->InAt(1);
1966 __ StoreToOffset(kStoreWordPair, value.AsRegisterPairLow<Register>(), obj, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001967 break;
1968 }
1969
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00001970 case Primitive::kPrimFloat: {
1971 SRegister value = locations->InAt(1).As<SRegister>();
1972 __ StoreSToOffset(value, obj, offset);
1973 break;
1974 }
1975
1976 case Primitive::kPrimDouble: {
1977 DRegister value = FromLowSToD(locations->InAt(1).AsFpuRegisterPairLow<SRegister>());
1978 __ StoreDToOffset(value, obj, offset);
1979 break;
1980 }
1981
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001982 case Primitive::kPrimVoid:
1983 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07001984 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001985 }
1986}
1987
1988void LocationsBuilderARM::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001989 LocationSummary* locations =
1990 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01001991 locations->SetInAt(0, Location::RequiresRegister());
1992 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001993}
1994
1995void InstructionCodeGeneratorARM::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
1996 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001997 Register obj = locations->InAt(0).As<Register>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001998 uint32_t offset = instruction->GetFieldOffset().Uint32Value();
1999
2000 switch (instruction->GetType()) {
2001 case Primitive::kPrimBoolean: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002002 Register out = locations->Out().As<Register>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002003 __ LoadFromOffset(kLoadUnsignedByte, out, obj, offset);
2004 break;
2005 }
2006
2007 case Primitive::kPrimByte: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002008 Register out = locations->Out().As<Register>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002009 __ LoadFromOffset(kLoadSignedByte, out, obj, offset);
2010 break;
2011 }
2012
2013 case Primitive::kPrimShort: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002014 Register out = locations->Out().As<Register>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002015 __ LoadFromOffset(kLoadSignedHalfword, out, obj, offset);
2016 break;
2017 }
2018
2019 case Primitive::kPrimChar: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002020 Register out = locations->Out().As<Register>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002021 __ LoadFromOffset(kLoadUnsignedHalfword, out, obj, offset);
2022 break;
2023 }
2024
2025 case Primitive::kPrimInt:
2026 case Primitive::kPrimNot: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002027 Register out = locations->Out().As<Register>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002028 __ LoadFromOffset(kLoadWord, out, obj, offset);
2029 break;
2030 }
2031
2032 case Primitive::kPrimLong: {
2033 // TODO: support volatile.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002034 Location out = locations->Out();
2035 __ LoadFromOffset(kLoadWordPair, out.AsRegisterPairLow<Register>(), obj, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002036 break;
2037 }
2038
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002039 case Primitive::kPrimFloat: {
2040 SRegister out = locations->Out().As<SRegister>();
2041 __ LoadSFromOffset(out, obj, offset);
2042 break;
2043 }
2044
2045 case Primitive::kPrimDouble: {
2046 DRegister out = FromLowSToD(locations->Out().AsFpuRegisterPairLow<SRegister>());
2047 __ LoadDFromOffset(out, obj, offset);
2048 break;
2049 }
2050
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002051 case Primitive::kPrimVoid:
2052 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07002053 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002054 }
2055}
2056
2057void LocationsBuilderARM::VisitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002058 LocationSummary* locations =
2059 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002060 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002061 if (instruction->HasUses()) {
2062 locations->SetOut(Location::SameAsFirstInput());
2063 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002064}
2065
2066void InstructionCodeGeneratorARM::VisitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01002067 SlowPathCodeARM* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathARM(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002068 codegen_->AddSlowPath(slow_path);
2069
2070 LocationSummary* locations = instruction->GetLocations();
2071 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002072
2073 if (obj.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002074 __ cmp(obj.As<Register>(), ShifterOperand(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002075 __ b(slow_path->GetEntryLabel(), EQ);
2076 } else {
2077 DCHECK(obj.IsConstant()) << obj;
2078 DCHECK_EQ(obj.GetConstant()->AsIntConstant()->GetValue(), 0);
2079 __ b(slow_path->GetEntryLabel());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002080 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002081}
2082
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002083void LocationsBuilderARM::VisitArrayGet(HArrayGet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002084 LocationSummary* locations =
2085 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002086 locations->SetInAt(0, Location::RequiresRegister());
2087 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
2088 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002089}
2090
2091void InstructionCodeGeneratorARM::VisitArrayGet(HArrayGet* instruction) {
2092 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002093 Register obj = locations->InAt(0).As<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002094 Location index = locations->InAt(1);
2095
2096 switch (instruction->GetType()) {
2097 case Primitive::kPrimBoolean: {
2098 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002099 Register out = locations->Out().As<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002100 if (index.IsConstant()) {
2101 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
2102 __ LoadFromOffset(kLoadUnsignedByte, out, obj, offset);
2103 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002104 __ add(IP, obj, ShifterOperand(index.As<Register>()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002105 __ LoadFromOffset(kLoadUnsignedByte, out, IP, data_offset);
2106 }
2107 break;
2108 }
2109
2110 case Primitive::kPrimByte: {
2111 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int8_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002112 Register out = locations->Out().As<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002113 if (index.IsConstant()) {
2114 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
2115 __ LoadFromOffset(kLoadSignedByte, out, obj, offset);
2116 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002117 __ add(IP, obj, ShifterOperand(index.As<Register>()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002118 __ LoadFromOffset(kLoadSignedByte, out, IP, data_offset);
2119 }
2120 break;
2121 }
2122
2123 case Primitive::kPrimShort: {
2124 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int16_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002125 Register out = locations->Out().As<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002126 if (index.IsConstant()) {
2127 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
2128 __ LoadFromOffset(kLoadSignedHalfword, out, obj, offset);
2129 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002130 __ add(IP, obj, ShifterOperand(index.As<Register>(), LSL, TIMES_2));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002131 __ LoadFromOffset(kLoadSignedHalfword, out, IP, data_offset);
2132 }
2133 break;
2134 }
2135
2136 case Primitive::kPrimChar: {
2137 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002138 Register out = locations->Out().As<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002139 if (index.IsConstant()) {
2140 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
2141 __ LoadFromOffset(kLoadUnsignedHalfword, out, obj, offset);
2142 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002143 __ add(IP, obj, ShifterOperand(index.As<Register>(), LSL, TIMES_2));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002144 __ LoadFromOffset(kLoadUnsignedHalfword, out, IP, data_offset);
2145 }
2146 break;
2147 }
2148
2149 case Primitive::kPrimInt:
2150 case Primitive::kPrimNot: {
2151 DCHECK_EQ(sizeof(mirror::HeapReference<mirror::Object>), sizeof(int32_t));
2152 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002153 Register out = locations->Out().As<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002154 if (index.IsConstant()) {
2155 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
2156 __ LoadFromOffset(kLoadWord, out, obj, offset);
2157 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002158 __ add(IP, obj, ShifterOperand(index.As<Register>(), LSL, TIMES_4));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002159 __ LoadFromOffset(kLoadWord, out, IP, data_offset);
2160 }
2161 break;
2162 }
2163
2164 case Primitive::kPrimLong: {
2165 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002166 Location out = locations->Out();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002167 if (index.IsConstant()) {
2168 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002169 __ LoadFromOffset(kLoadWordPair, out.AsRegisterPairLow<Register>(), obj, offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002170 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002171 __ add(IP, obj, ShifterOperand(index.As<Register>(), LSL, TIMES_8));
2172 __ LoadFromOffset(kLoadWordPair, out.AsRegisterPairLow<Register>(), IP, data_offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002173 }
2174 break;
2175 }
2176
2177 case Primitive::kPrimFloat:
2178 case Primitive::kPrimDouble:
2179 LOG(FATAL) << "Unimplemented register type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07002180 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002181 case Primitive::kPrimVoid:
2182 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07002183 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002184 }
2185}
2186
2187void LocationsBuilderARM::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002188 Primitive::Type value_type = instruction->GetComponentType();
2189 bool is_object = value_type == Primitive::kPrimNot;
2190 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
2191 instruction, is_object ? LocationSummary::kCall : LocationSummary::kNoCall);
2192 if (is_object) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002193 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002194 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2195 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
2196 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002197 } else {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002198 locations->SetInAt(0, Location::RequiresRegister());
2199 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
2200 locations->SetInAt(2, Location::RequiresRegister());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002201 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002202}
2203
2204void InstructionCodeGeneratorARM::VisitArraySet(HArraySet* instruction) {
2205 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002206 Register obj = locations->InAt(0).As<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002207 Location index = locations->InAt(1);
Nicolas Geoffray39468442014-09-02 15:17:15 +01002208 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002209
2210 switch (value_type) {
2211 case Primitive::kPrimBoolean:
2212 case Primitive::kPrimByte: {
2213 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002214 Register value = locations->InAt(2).As<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002215 if (index.IsConstant()) {
2216 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
2217 __ StoreToOffset(kStoreByte, value, obj, offset);
2218 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002219 __ add(IP, obj, ShifterOperand(index.As<Register>()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002220 __ StoreToOffset(kStoreByte, value, IP, data_offset);
2221 }
2222 break;
2223 }
2224
2225 case Primitive::kPrimShort:
2226 case Primitive::kPrimChar: {
2227 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002228 Register value = locations->InAt(2).As<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002229 if (index.IsConstant()) {
2230 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
2231 __ StoreToOffset(kStoreHalfword, value, obj, offset);
2232 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002233 __ add(IP, obj, ShifterOperand(index.As<Register>(), LSL, TIMES_2));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002234 __ StoreToOffset(kStoreHalfword, value, IP, data_offset);
2235 }
2236 break;
2237 }
2238
2239 case Primitive::kPrimInt: {
2240 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002241 Register value = locations->InAt(2).As<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002242 if (index.IsConstant()) {
2243 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
2244 __ StoreToOffset(kStoreWord, value, obj, offset);
2245 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002246 __ add(IP, obj, ShifterOperand(index.As<Register>(), LSL, TIMES_4));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002247 __ StoreToOffset(kStoreWord, value, IP, data_offset);
2248 }
2249 break;
2250 }
2251
2252 case Primitive::kPrimNot: {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002253 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pAputObject), instruction, instruction->GetDexPc());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002254 break;
2255 }
2256
2257 case Primitive::kPrimLong: {
2258 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002259 Location value = locations->InAt(2);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002260 if (index.IsConstant()) {
2261 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002262 __ StoreToOffset(kStoreWordPair, value.AsRegisterPairLow<Register>(), obj, offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002263 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002264 __ add(IP, obj, ShifterOperand(index.As<Register>(), LSL, TIMES_8));
2265 __ StoreToOffset(kStoreWordPair, value.AsRegisterPairLow<Register>(), IP, data_offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002266 }
2267 break;
2268 }
2269
2270 case Primitive::kPrimFloat:
2271 case Primitive::kPrimDouble:
2272 LOG(FATAL) << "Unimplemented register type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07002273 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002274 case Primitive::kPrimVoid:
2275 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07002276 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002277 }
2278}
2279
2280void LocationsBuilderARM::VisitArrayLength(HArrayLength* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002281 LocationSummary* locations =
2282 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002283 locations->SetInAt(0, Location::RequiresRegister());
2284 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002285}
2286
2287void InstructionCodeGeneratorARM::VisitArrayLength(HArrayLength* instruction) {
2288 LocationSummary* locations = instruction->GetLocations();
2289 uint32_t offset = mirror::Array::LengthOffset().Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002290 Register obj = locations->InAt(0).As<Register>();
2291 Register out = locations->Out().As<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002292 __ LoadFromOffset(kLoadWord, out, obj, offset);
2293}
2294
2295void LocationsBuilderARM::VisitBoundsCheck(HBoundsCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002296 LocationSummary* locations =
2297 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002298 locations->SetInAt(0, Location::RequiresRegister());
2299 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002300 if (instruction->HasUses()) {
2301 locations->SetOut(Location::SameAsFirstInput());
2302 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002303}
2304
2305void InstructionCodeGeneratorARM::VisitBoundsCheck(HBoundsCheck* instruction) {
2306 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01002307 SlowPathCodeARM* slow_path = new (GetGraph()->GetArena()) BoundsCheckSlowPathARM(
Nicolas Geoffray39468442014-09-02 15:17:15 +01002308 instruction, locations->InAt(0), locations->InAt(1));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002309 codegen_->AddSlowPath(slow_path);
2310
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002311 Register index = locations->InAt(0).As<Register>();
2312 Register length = locations->InAt(1).As<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002313
2314 __ cmp(index, ShifterOperand(length));
2315 __ b(slow_path->GetEntryLabel(), CS);
2316}
2317
2318void CodeGeneratorARM::MarkGCCard(Register temp, Register card, Register object, Register value) {
2319 Label is_null;
2320 __ CompareAndBranchIfZero(value, &is_null);
2321 __ LoadFromOffset(kLoadWord, card, TR, Thread::CardTableOffset<kArmWordSize>().Int32Value());
2322 __ Lsr(temp, object, gc::accounting::CardTable::kCardShift);
2323 __ strb(card, Address(card, temp));
2324 __ Bind(&is_null);
2325}
2326
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002327void LocationsBuilderARM::VisitTemporary(HTemporary* temp) {
2328 temp->SetLocations(nullptr);
2329}
2330
2331void InstructionCodeGeneratorARM::VisitTemporary(HTemporary* temp) {
2332 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002333 UNUSED(temp);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002334}
2335
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01002336void LocationsBuilderARM::VisitParallelMove(HParallelMove* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002337 UNUSED(instruction);
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01002338 LOG(FATAL) << "Unreachable";
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01002339}
2340
2341void InstructionCodeGeneratorARM::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01002342 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
2343}
2344
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00002345void LocationsBuilderARM::VisitSuspendCheck(HSuspendCheck* instruction) {
2346 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
2347}
2348
2349void InstructionCodeGeneratorARM::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01002350 HBasicBlock* block = instruction->GetBlock();
2351 if (block->GetLoopInformation() != nullptr) {
2352 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
2353 // The back edge will generate the suspend check.
2354 return;
2355 }
2356 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
2357 // The goto will generate the suspend check.
2358 return;
2359 }
2360 GenerateSuspendCheck(instruction, nullptr);
2361}
2362
2363void InstructionCodeGeneratorARM::GenerateSuspendCheck(HSuspendCheck* instruction,
2364 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00002365 SuspendCheckSlowPathARM* slow_path =
Nicolas Geoffray3c049742014-09-24 18:10:46 +01002366 new (GetGraph()->GetArena()) SuspendCheckSlowPathARM(instruction, successor);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00002367 codegen_->AddSlowPath(slow_path);
2368
Nicolas Geoffray44b819e2014-11-06 12:00:54 +00002369 __ LoadFromOffset(
2370 kLoadUnsignedHalfword, IP, TR, Thread::ThreadFlagsOffset<kArmWordSize>().Int32Value());
2371 __ cmp(IP, ShifterOperand(0));
2372 // TODO: Figure out the branch offsets and use cbz/cbnz.
Nicolas Geoffray3c049742014-09-24 18:10:46 +01002373 if (successor == nullptr) {
Nicolas Geoffray44b819e2014-11-06 12:00:54 +00002374 __ b(slow_path->GetEntryLabel(), NE);
Nicolas Geoffray3c049742014-09-24 18:10:46 +01002375 __ Bind(slow_path->GetReturnLabel());
2376 } else {
Nicolas Geoffray44b819e2014-11-06 12:00:54 +00002377 __ b(codegen_->GetLabelOf(successor), EQ);
Nicolas Geoffray3c049742014-09-24 18:10:46 +01002378 __ b(slow_path->GetEntryLabel());
2379 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00002380}
2381
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01002382ArmAssembler* ParallelMoveResolverARM::GetAssembler() const {
2383 return codegen_->GetAssembler();
2384}
2385
2386void ParallelMoveResolverARM::EmitMove(size_t index) {
2387 MoveOperands* move = moves_.Get(index);
2388 Location source = move->GetSource();
2389 Location destination = move->GetDestination();
2390
2391 if (source.IsRegister()) {
2392 if (destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002393 __ Mov(destination.As<Register>(), source.As<Register>());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01002394 } else {
2395 DCHECK(destination.IsStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002396 __ StoreToOffset(kStoreWord, source.As<Register>(),
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01002397 SP, destination.GetStackIndex());
2398 }
2399 } else if (source.IsStackSlot()) {
2400 if (destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002401 __ LoadFromOffset(kLoadWord, destination.As<Register>(),
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01002402 SP, source.GetStackIndex());
2403 } else {
2404 DCHECK(destination.IsStackSlot());
2405 __ LoadFromOffset(kLoadWord, IP, SP, source.GetStackIndex());
2406 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
2407 }
2408 } else {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002409 DCHECK(source.IsConstant());
Roland Levillain476df552014-10-09 17:51:36 +01002410 DCHECK(source.GetConstant()->IsIntConstant());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002411 int32_t value = source.GetConstant()->AsIntConstant()->GetValue();
2412 if (destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002413 __ LoadImmediate(destination.As<Register>(), value);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002414 } else {
2415 DCHECK(destination.IsStackSlot());
2416 __ LoadImmediate(IP, value);
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002417 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002418 }
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01002419 }
2420}
2421
2422void ParallelMoveResolverARM::Exchange(Register reg, int mem) {
2423 __ Mov(IP, reg);
2424 __ LoadFromOffset(kLoadWord, reg, SP, mem);
2425 __ StoreToOffset(kStoreWord, IP, SP, mem);
2426}
2427
2428void ParallelMoveResolverARM::Exchange(int mem1, int mem2) {
2429 ScratchRegisterScope ensure_scratch(this, IP, R0, codegen_->GetNumberOfCoreRegisters());
2430 int stack_offset = ensure_scratch.IsSpilled() ? kArmWordSize : 0;
2431 __ LoadFromOffset(kLoadWord, static_cast<Register>(ensure_scratch.GetRegister()),
2432 SP, mem1 + stack_offset);
2433 __ LoadFromOffset(kLoadWord, IP, SP, mem2 + stack_offset);
2434 __ StoreToOffset(kStoreWord, static_cast<Register>(ensure_scratch.GetRegister()),
2435 SP, mem2 + stack_offset);
2436 __ StoreToOffset(kStoreWord, IP, SP, mem1 + stack_offset);
2437}
2438
2439void ParallelMoveResolverARM::EmitSwap(size_t index) {
2440 MoveOperands* move = moves_.Get(index);
2441 Location source = move->GetSource();
2442 Location destination = move->GetDestination();
2443
2444 if (source.IsRegister() && destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002445 DCHECK_NE(source.As<Register>(), IP);
2446 DCHECK_NE(destination.As<Register>(), IP);
2447 __ Mov(IP, source.As<Register>());
2448 __ Mov(source.As<Register>(), destination.As<Register>());
2449 __ Mov(destination.As<Register>(), IP);
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01002450 } else if (source.IsRegister() && destination.IsStackSlot()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002451 Exchange(source.As<Register>(), destination.GetStackIndex());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01002452 } else if (source.IsStackSlot() && destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002453 Exchange(destination.As<Register>(), source.GetStackIndex());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01002454 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
2455 Exchange(source.GetStackIndex(), destination.GetStackIndex());
2456 } else {
2457 LOG(FATAL) << "Unimplemented";
2458 }
2459}
2460
2461void ParallelMoveResolverARM::SpillScratch(int reg) {
2462 __ Push(static_cast<Register>(reg));
2463}
2464
2465void ParallelMoveResolverARM::RestoreScratch(int reg) {
2466 __ Pop(static_cast<Register>(reg));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01002467}
2468
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002469void LocationsBuilderARM::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002470 LocationSummary::CallKind call_kind = cls->CanCallRuntime()
2471 ? LocationSummary::kCallOnSlowPath
2472 : LocationSummary::kNoCall;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002473 LocationSummary* locations =
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002474 new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002475 locations->SetOut(Location::RequiresRegister());
2476}
2477
2478void InstructionCodeGeneratorARM::VisitLoadClass(HLoadClass* cls) {
2479 Register out = cls->GetLocations()->Out().As<Register>();
2480 if (cls->IsReferrersClass()) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002481 DCHECK(!cls->CanCallRuntime());
2482 DCHECK(!cls->MustGenerateClinitCheck());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002483 codegen_->LoadCurrentMethod(out);
2484 __ LoadFromOffset(kLoadWord, out, out, mirror::ArtMethod::DeclaringClassOffset().Int32Value());
2485 } else {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002486 DCHECK(cls->CanCallRuntime());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002487 codegen_->LoadCurrentMethod(out);
2488 __ LoadFromOffset(
2489 kLoadWord, out, out, mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value());
2490 __ LoadFromOffset(kLoadWord, out, out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex()));
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002491
2492 SlowPathCodeARM* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM(
2493 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
2494 codegen_->AddSlowPath(slow_path);
2495 __ cmp(out, ShifterOperand(0));
2496 __ b(slow_path->GetEntryLabel(), EQ);
2497 if (cls->MustGenerateClinitCheck()) {
2498 GenerateClassInitializationCheck(slow_path, out);
2499 } else {
2500 __ Bind(slow_path->GetExitLabel());
2501 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002502 }
2503}
2504
2505void LocationsBuilderARM::VisitClinitCheck(HClinitCheck* check) {
2506 LocationSummary* locations =
2507 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
2508 locations->SetInAt(0, Location::RequiresRegister());
2509 if (check->HasUses()) {
2510 locations->SetOut(Location::SameAsFirstInput());
2511 }
2512}
2513
2514void InstructionCodeGeneratorARM::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002515 // We assume the class is not null.
2516 SlowPathCodeARM* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM(
2517 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002518 codegen_->AddSlowPath(slow_path);
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002519 GenerateClassInitializationCheck(slow_path, check->GetLocations()->InAt(0).As<Register>());
2520}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002521
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002522void InstructionCodeGeneratorARM::GenerateClassInitializationCheck(
2523 SlowPathCodeARM* slow_path, Register class_reg) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002524 __ LoadFromOffset(kLoadWord, IP, class_reg, mirror::Class::StatusOffset().Int32Value());
2525 __ cmp(IP, ShifterOperand(mirror::Class::kStatusInitialized));
2526 __ b(slow_path->GetEntryLabel(), LT);
2527 // Even if the initialized flag is set, we may be in a situation where caches are not synced
2528 // properly. Therefore, we do a memory fence.
2529 __ dmb(ISH);
2530 __ Bind(slow_path->GetExitLabel());
2531}
2532
2533void LocationsBuilderARM::VisitStaticFieldGet(HStaticFieldGet* instruction) {
2534 LocationSummary* locations =
2535 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2536 locations->SetInAt(0, Location::RequiresRegister());
2537 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2538}
2539
2540void InstructionCodeGeneratorARM::VisitStaticFieldGet(HStaticFieldGet* instruction) {
2541 LocationSummary* locations = instruction->GetLocations();
2542 Register cls = locations->InAt(0).As<Register>();
2543 uint32_t offset = instruction->GetFieldOffset().Uint32Value();
2544
2545 switch (instruction->GetType()) {
2546 case Primitive::kPrimBoolean: {
2547 Register out = locations->Out().As<Register>();
2548 __ LoadFromOffset(kLoadUnsignedByte, out, cls, offset);
2549 break;
2550 }
2551
2552 case Primitive::kPrimByte: {
2553 Register out = locations->Out().As<Register>();
2554 __ LoadFromOffset(kLoadSignedByte, out, cls, offset);
2555 break;
2556 }
2557
2558 case Primitive::kPrimShort: {
2559 Register out = locations->Out().As<Register>();
2560 __ LoadFromOffset(kLoadSignedHalfword, out, cls, offset);
2561 break;
2562 }
2563
2564 case Primitive::kPrimChar: {
2565 Register out = locations->Out().As<Register>();
2566 __ LoadFromOffset(kLoadUnsignedHalfword, out, cls, offset);
2567 break;
2568 }
2569
2570 case Primitive::kPrimInt:
2571 case Primitive::kPrimNot: {
2572 Register out = locations->Out().As<Register>();
2573 __ LoadFromOffset(kLoadWord, out, cls, offset);
2574 break;
2575 }
2576
2577 case Primitive::kPrimLong: {
2578 // TODO: support volatile.
2579 Location out = locations->Out();
2580 __ LoadFromOffset(kLoadWordPair, out.AsRegisterPairLow<Register>(), cls, offset);
2581 break;
2582 }
2583
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002584 case Primitive::kPrimFloat: {
2585 SRegister out = locations->Out().As<SRegister>();
2586 __ LoadSFromOffset(out, cls, offset);
2587 break;
2588 }
2589
2590 case Primitive::kPrimDouble: {
2591 DRegister out = FromLowSToD(locations->Out().AsFpuRegisterPairLow<SRegister>());
2592 __ LoadDFromOffset(out, cls, offset);
2593 break;
2594 }
2595
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002596 case Primitive::kPrimVoid:
2597 LOG(FATAL) << "Unreachable type " << instruction->GetType();
2598 UNREACHABLE();
2599 }
2600}
2601
2602void LocationsBuilderARM::VisitStaticFieldSet(HStaticFieldSet* instruction) {
2603 LocationSummary* locations =
2604 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2605 bool is_object_type = instruction->GetFieldType() == Primitive::kPrimNot;
2606 locations->SetInAt(0, Location::RequiresRegister());
2607 locations->SetInAt(1, Location::RequiresRegister());
2608 // Temporary registers for the write barrier.
2609 if (is_object_type) {
2610 locations->AddTemp(Location::RequiresRegister());
2611 locations->AddTemp(Location::RequiresRegister());
2612 }
2613}
2614
2615void InstructionCodeGeneratorARM::VisitStaticFieldSet(HStaticFieldSet* instruction) {
2616 LocationSummary* locations = instruction->GetLocations();
2617 Register cls = locations->InAt(0).As<Register>();
2618 uint32_t offset = instruction->GetFieldOffset().Uint32Value();
2619 Primitive::Type field_type = instruction->GetFieldType();
2620
2621 switch (field_type) {
2622 case Primitive::kPrimBoolean:
2623 case Primitive::kPrimByte: {
2624 Register value = locations->InAt(1).As<Register>();
2625 __ StoreToOffset(kStoreByte, value, cls, offset);
2626 break;
2627 }
2628
2629 case Primitive::kPrimShort:
2630 case Primitive::kPrimChar: {
2631 Register value = locations->InAt(1).As<Register>();
2632 __ StoreToOffset(kStoreHalfword, value, cls, offset);
2633 break;
2634 }
2635
2636 case Primitive::kPrimInt:
2637 case Primitive::kPrimNot: {
2638 Register value = locations->InAt(1).As<Register>();
2639 __ StoreToOffset(kStoreWord, value, cls, offset);
2640 if (field_type == Primitive::kPrimNot) {
2641 Register temp = locations->GetTemp(0).As<Register>();
2642 Register card = locations->GetTemp(1).As<Register>();
2643 codegen_->MarkGCCard(temp, card, cls, value);
2644 }
2645 break;
2646 }
2647
2648 case Primitive::kPrimLong: {
2649 Location value = locations->InAt(1);
2650 __ StoreToOffset(kStoreWordPair, value.AsRegisterPairLow<Register>(), cls, offset);
2651 break;
2652 }
2653
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002654 case Primitive::kPrimFloat: {
2655 SRegister value = locations->InAt(1).As<SRegister>();
2656 __ StoreSToOffset(value, cls, offset);
2657 break;
2658 }
2659
2660 case Primitive::kPrimDouble: {
2661 DRegister value = FromLowSToD(locations->InAt(1).AsFpuRegisterPairLow<SRegister>());
2662 __ StoreDToOffset(value, cls, offset);
2663 break;
2664 }
2665
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002666 case Primitive::kPrimVoid:
2667 LOG(FATAL) << "Unreachable type " << field_type;
2668 UNREACHABLE();
2669 }
2670}
2671
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00002672void LocationsBuilderARM::VisitLoadString(HLoadString* load) {
2673 LocationSummary* locations =
2674 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kCallOnSlowPath);
2675 locations->SetOut(Location::RequiresRegister());
2676}
2677
2678void InstructionCodeGeneratorARM::VisitLoadString(HLoadString* load) {
2679 SlowPathCodeARM* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathARM(load);
2680 codegen_->AddSlowPath(slow_path);
2681
2682 Register out = load->GetLocations()->Out().As<Register>();
2683 codegen_->LoadCurrentMethod(out);
2684 __ LoadFromOffset(
2685 kLoadWord, out, out, mirror::ArtMethod::DexCacheStringsOffset().Int32Value());
2686 __ LoadFromOffset(kLoadWord, out, out, CodeGenerator::GetCacheOffset(load->GetStringIndex()));
2687 __ cmp(out, ShifterOperand(0));
2688 __ b(slow_path->GetEntryLabel(), EQ);
2689 __ Bind(slow_path->GetExitLabel());
2690}
2691
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002692void LocationsBuilderARM::VisitLoadException(HLoadException* load) {
2693 LocationSummary* locations =
2694 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
2695 locations->SetOut(Location::RequiresRegister());
2696}
2697
2698void InstructionCodeGeneratorARM::VisitLoadException(HLoadException* load) {
2699 Register out = load->GetLocations()->Out().As<Register>();
2700 int32_t offset = Thread::ExceptionOffset<kArmWordSize>().Int32Value();
2701 __ LoadFromOffset(kLoadWord, out, TR, offset);
2702 __ LoadImmediate(IP, 0);
2703 __ StoreToOffset(kStoreWord, IP, TR, offset);
2704}
2705
2706void LocationsBuilderARM::VisitThrow(HThrow* instruction) {
2707 LocationSummary* locations =
2708 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
2709 InvokeRuntimeCallingConvention calling_convention;
2710 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2711}
2712
2713void InstructionCodeGeneratorARM::VisitThrow(HThrow* instruction) {
2714 codegen_->InvokeRuntime(
2715 QUICK_ENTRY_POINT(pDeliverException), instruction, instruction->GetDexPc());
2716}
2717
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00002718void LocationsBuilderARM::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00002719 LocationSummary::CallKind call_kind = instruction->IsClassFinal()
2720 ? LocationSummary::kNoCall
2721 : LocationSummary::kCallOnSlowPath;
2722 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
2723 locations->SetInAt(0, Location::RequiresRegister());
2724 locations->SetInAt(1, Location::RequiresRegister());
2725 locations->SetOut(Location::RequiresRegister());
2726}
2727
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00002728void InstructionCodeGeneratorARM::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00002729 LocationSummary* locations = instruction->GetLocations();
2730 Register obj = locations->InAt(0).As<Register>();
2731 Register cls = locations->InAt(1).As<Register>();
2732 Register out = locations->Out().As<Register>();
2733 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2734 Label done, zero;
2735 SlowPathCodeARM* slow_path = nullptr;
2736
2737 // Return 0 if `obj` is null.
2738 // TODO: avoid this check if we know obj is not null.
2739 __ cmp(obj, ShifterOperand(0));
2740 __ b(&zero, EQ);
2741 // Compare the class of `obj` with `cls`.
2742 __ LoadFromOffset(kLoadWord, out, obj, class_offset);
2743 __ cmp(out, ShifterOperand(cls));
2744 if (instruction->IsClassFinal()) {
2745 // Classes must be equal for the instanceof to succeed.
2746 __ b(&zero, NE);
2747 __ LoadImmediate(out, 1);
2748 __ b(&done);
2749 } else {
2750 // If the classes are not equal, we go into a slow path.
2751 DCHECK(locations->OnlyCallsOnSlowPath());
2752 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM(
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00002753 instruction, locations->InAt(1), locations->Out(), instruction->GetDexPc());
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00002754 codegen_->AddSlowPath(slow_path);
2755 __ b(slow_path->GetEntryLabel(), NE);
2756 __ LoadImmediate(out, 1);
2757 __ b(&done);
2758 }
2759 __ Bind(&zero);
2760 __ LoadImmediate(out, 0);
2761 if (slow_path != nullptr) {
2762 __ Bind(slow_path->GetExitLabel());
2763 }
2764 __ Bind(&done);
2765}
2766
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00002767void LocationsBuilderARM::VisitCheckCast(HCheckCast* instruction) {
2768 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
2769 instruction, LocationSummary::kCallOnSlowPath);
2770 locations->SetInAt(0, Location::RequiresRegister());
2771 locations->SetInAt(1, Location::RequiresRegister());
2772 locations->AddTemp(Location::RequiresRegister());
2773}
2774
2775void InstructionCodeGeneratorARM::VisitCheckCast(HCheckCast* instruction) {
2776 LocationSummary* locations = instruction->GetLocations();
2777 Register obj = locations->InAt(0).As<Register>();
2778 Register cls = locations->InAt(1).As<Register>();
2779 Register temp = locations->GetTemp(0).As<Register>();
2780 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2781
2782 SlowPathCodeARM* slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM(
2783 instruction, locations->InAt(1), locations->GetTemp(0), instruction->GetDexPc());
2784 codegen_->AddSlowPath(slow_path);
2785
2786 // TODO: avoid this check if we know obj is not null.
2787 __ cmp(obj, ShifterOperand(0));
2788 __ b(slow_path->GetExitLabel(), EQ);
2789 // Compare the class of `obj` with `cls`.
2790 __ LoadFromOffset(kLoadWord, temp, obj, class_offset);
2791 __ cmp(temp, ShifterOperand(cls));
2792 __ b(slow_path->GetEntryLabel(), NE);
2793 __ Bind(slow_path->GetExitLabel());
2794}
2795
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00002796void LocationsBuilderARM::VisitMonitorOperation(HMonitorOperation* instruction) {
2797 LocationSummary* locations =
2798 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
2799 InvokeRuntimeCallingConvention calling_convention;
2800 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2801}
2802
2803void InstructionCodeGeneratorARM::VisitMonitorOperation(HMonitorOperation* instruction) {
2804 codegen_->InvokeRuntime(instruction->IsEnter()
2805 ? QUICK_ENTRY_POINT(pLockObject) : QUICK_ENTRY_POINT(pUnlockObject),
2806 instruction,
2807 instruction->GetDexPc());
2808}
2809
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002810} // namespace arm
2811} // namespace art