blob: 6e6d64cbfc1d15ad6c63b9ae46167d8cb0b9b275 [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/assembler.h"
26#include "utils/arm/assembler_arm.h"
27#include "utils/arm/managed_register_arm.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
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +010095class StackOverflowCheckSlowPathARM : public SlowPathCodeARM {
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010096 public:
97 StackOverflowCheckSlowPathARM() {}
98
99 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
100 __ Bind(GetEntryLabel());
101 __ LoadFromOffset(kLoadWord, PC, TR,
102 QUICK_ENTRYPOINT_OFFSET(kArmWordSize, pThrowStackOverflow).Int32Value());
103 }
104
105 private:
106 DISALLOW_COPY_AND_ASSIGN(StackOverflowCheckSlowPathARM);
107};
108
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100109class SuspendCheckSlowPathARM : public SlowPathCodeARM {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000110 public:
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100111 explicit SuspendCheckSlowPathARM(HSuspendCheck* instruction, HBasicBlock* successor)
112 : instruction_(instruction), successor_(successor) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000113
114 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100115 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000116 __ Bind(GetEntryLabel());
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100117 codegen->SaveLiveRegisters(instruction_->GetLocations());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100118 arm_codegen->InvokeRuntime(
119 QUICK_ENTRY_POINT(pTestSuspend), instruction_, instruction_->GetDexPc());
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100120 codegen->RestoreLiveRegisters(instruction_->GetLocations());
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100121 if (successor_ == nullptr) {
122 __ b(GetReturnLabel());
123 } else {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100124 __ b(arm_codegen->GetLabelOf(successor_));
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100125 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000126 }
127
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100128 Label* GetReturnLabel() {
129 DCHECK(successor_ == nullptr);
130 return &return_label_;
131 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000132
133 private:
134 HSuspendCheck* const instruction_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100135 // If not null, the block to branch to after the suspend check.
136 HBasicBlock* const successor_;
137
138 // If `successor_` is null, the label to branch to after the suspend check.
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000139 Label return_label_;
140
141 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathARM);
142};
143
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100144class BoundsCheckSlowPathARM : public SlowPathCodeARM {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100145 public:
Roland Levillain5799fc02014-09-25 12:15:20 +0100146 BoundsCheckSlowPathARM(HBoundsCheck* instruction,
147 Location index_location,
148 Location length_location)
Nicolas Geoffray39468442014-09-02 15:17:15 +0100149 : instruction_(instruction),
150 index_location_(index_location),
151 length_location_(length_location) {}
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100152
153 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100154 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100155 __ Bind(GetEntryLabel());
156 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100157 arm_codegen->Move32(
158 Location::RegisterLocation(calling_convention.GetRegisterAt(0)), index_location_);
159 arm_codegen->Move32(
160 Location::RegisterLocation(calling_convention.GetRegisterAt(1)), length_location_);
161 arm_codegen->InvokeRuntime(
162 QUICK_ENTRY_POINT(pThrowArrayBounds), instruction_, instruction_->GetDexPc());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100163 }
164
165 private:
Nicolas Geoffray39468442014-09-02 15:17:15 +0100166 HBoundsCheck* const instruction_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100167 const Location index_location_;
168 const Location length_location_;
169
170 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathARM);
171};
172
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000173class LoadClassSlowPathARM : public SlowPathCodeARM {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100174 public:
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000175 LoadClassSlowPathARM(HLoadClass* cls,
176 HInstruction* at,
177 uint32_t dex_pc,
178 bool do_clinit)
179 : cls_(cls), at_(at), dex_pc_(dex_pc), do_clinit_(do_clinit) {
180 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
181 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100182
183 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000184 LocationSummary* locations = at_->GetLocations();
185
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100186 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
187 __ Bind(GetEntryLabel());
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000188 codegen->SaveLiveRegisters(locations);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100189
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100190 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000191 __ LoadImmediate(calling_convention.GetRegisterAt(0), cls_->GetTypeIndex());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100192 arm_codegen->LoadCurrentMethod(calling_convention.GetRegisterAt(1));
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000193 int32_t entry_point_offset = do_clinit_
194 ? QUICK_ENTRY_POINT(pInitializeStaticStorage)
195 : QUICK_ENTRY_POINT(pInitializeType);
196 arm_codegen->InvokeRuntime(entry_point_offset, at_, dex_pc_);
197
198 // Move the class to the desired location.
199 if (locations->Out().IsValid()) {
200 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
201 arm_codegen->Move32(locations->Out(), Location::RegisterLocation(R0));
202 }
203 codegen->RestoreLiveRegisters(locations);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100204 __ b(GetExitLabel());
205 }
206
207 private:
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000208 // The class this slow path will load.
209 HLoadClass* const cls_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100210
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000211 // The instruction where this slow path is happening.
212 // (Might be the load class or an initialization check).
213 HInstruction* const at_;
214
215 // The dex PC of `at_`.
216 const uint32_t dex_pc_;
217
218 // Whether to initialize the class.
219 const bool do_clinit_;
220
221 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathARM);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100222};
223
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000224class LoadStringSlowPathARM : public SlowPathCodeARM {
225 public:
226 explicit LoadStringSlowPathARM(HLoadString* instruction) : instruction_(instruction) {}
227
228 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
229 LocationSummary* locations = instruction_->GetLocations();
230 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
231
232 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
233 __ Bind(GetEntryLabel());
234 codegen->SaveLiveRegisters(locations);
235
236 InvokeRuntimeCallingConvention calling_convention;
237 arm_codegen->LoadCurrentMethod(calling_convention.GetRegisterAt(0));
238 __ LoadImmediate(calling_convention.GetRegisterAt(1), instruction_->GetStringIndex());
239 arm_codegen->InvokeRuntime(
240 QUICK_ENTRY_POINT(pResolveString), instruction_, instruction_->GetDexPc());
241 arm_codegen->Move32(locations->Out(), Location::RegisterLocation(R0));
242
243 codegen->RestoreLiveRegisters(locations);
244 __ b(GetExitLabel());
245 }
246
247 private:
248 HLoadString* const instruction_;
249
250 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathARM);
251};
252
253#undef __
254
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100255#undef __
256#define __ reinterpret_cast<ArmAssembler*>(GetAssembler())->
Dave Allison20dfc792014-06-16 20:44:29 -0700257
258inline Condition ARMCondition(IfCondition cond) {
259 switch (cond) {
260 case kCondEQ: return EQ;
261 case kCondNE: return NE;
262 case kCondLT: return LT;
263 case kCondLE: return LE;
264 case kCondGT: return GT;
265 case kCondGE: return GE;
266 default:
267 LOG(FATAL) << "Unknown if condition";
268 }
269 return EQ; // Unreachable.
270}
271
272inline Condition ARMOppositeCondition(IfCondition cond) {
273 switch (cond) {
274 case kCondEQ: return NE;
275 case kCondNE: return EQ;
276 case kCondLT: return GE;
277 case kCondLE: return GT;
278 case kCondGT: return LE;
279 case kCondGE: return LT;
280 default:
281 LOG(FATAL) << "Unknown if condition";
282 }
283 return EQ; // Unreachable.
284}
285
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100286void CodeGeneratorARM::DumpCoreRegister(std::ostream& stream, int reg) const {
287 stream << ArmManagedRegister::FromCoreRegister(Register(reg));
288}
289
290void CodeGeneratorARM::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000291 stream << ArmManagedRegister::FromSRegister(SRegister(reg));
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100292}
293
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100294size_t CodeGeneratorARM::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
295 __ StoreToOffset(kStoreWord, static_cast<Register>(reg_id), SP, stack_index);
296 return kArmWordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100297}
298
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100299size_t CodeGeneratorARM::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
300 __ LoadFromOffset(kLoadWord, static_cast<Register>(reg_id), SP, stack_index);
301 return kArmWordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100302}
303
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100304CodeGeneratorARM::CodeGeneratorARM(HGraph* graph)
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000305 : CodeGenerator(graph, kNumberOfCoreRegisters, kNumberOfSRegisters, kNumberOfRegisterPairs),
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100306 block_labels_(graph->GetArena(), 0),
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100307 location_builder_(graph, this),
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +0100308 instruction_visitor_(graph, this),
Nicolas Geoffray8d486732014-07-16 16:23:40 +0100309 move_resolver_(graph->GetArena(), this),
310 assembler_(true) {}
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100311
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100312size_t CodeGeneratorARM::FrameEntrySpillSize() const {
313 return kNumberOfPushedRegistersAtEntry * kArmWordSize;
314}
315
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100316Location CodeGeneratorARM::AllocateFreeRegister(Primitive::Type type) const {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100317 switch (type) {
318 case Primitive::kPrimLong: {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100319 size_t reg = FindFreeEntry(blocked_register_pairs_, kNumberOfRegisterPairs);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100320 ArmManagedRegister pair =
321 ArmManagedRegister::FromRegisterPair(static_cast<RegisterPair>(reg));
Calin Juravle34bacdf2014-10-07 20:23:36 +0100322 DCHECK(!blocked_core_registers_[pair.AsRegisterPairLow()]);
323 DCHECK(!blocked_core_registers_[pair.AsRegisterPairHigh()]);
324
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100325 blocked_core_registers_[pair.AsRegisterPairLow()] = true;
326 blocked_core_registers_[pair.AsRegisterPairHigh()] = true;
Calin Juravle34bacdf2014-10-07 20:23:36 +0100327 UpdateBlockedPairRegisters();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100328 return Location::RegisterPairLocation(pair.AsRegisterPairLow(), pair.AsRegisterPairHigh());
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100329 }
330
331 case Primitive::kPrimByte:
332 case Primitive::kPrimBoolean:
333 case Primitive::kPrimChar:
334 case Primitive::kPrimShort:
335 case Primitive::kPrimInt:
336 case Primitive::kPrimNot: {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100337 int reg = FindFreeEntry(blocked_core_registers_, kNumberOfCoreRegisters);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100338 // Block all register pairs that contain `reg`.
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100339 for (int i = 0; i < kNumberOfRegisterPairs; i++) {
340 ArmManagedRegister current =
341 ArmManagedRegister::FromRegisterPair(static_cast<RegisterPair>(i));
342 if (current.AsRegisterPairLow() == reg || current.AsRegisterPairHigh() == reg) {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100343 blocked_register_pairs_[i] = true;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100344 }
345 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100346 return Location::RegisterLocation(reg);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100347 }
348
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000349 case Primitive::kPrimFloat: {
350 int reg = FindFreeEntry(blocked_fpu_registers_, kNumberOfSRegisters);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100351 return Location::FpuRegisterLocation(reg);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100352 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100353
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000354 case Primitive::kPrimDouble: {
Nicolas Geoffray3c035032014-10-28 10:46:40 +0000355 int reg = FindTwoFreeConsecutiveAlignedEntries(blocked_fpu_registers_, kNumberOfSRegisters);
356 DCHECK_EQ(reg % 2, 0);
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000357 return Location::FpuRegisterPairLocation(reg, reg + 1);
358 }
359
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100360 case Primitive::kPrimVoid:
361 LOG(FATAL) << "Unreachable type " << type;
362 }
363
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100364 return Location();
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100365}
366
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100367void CodeGeneratorARM::SetupBlockedRegisters() const {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100368 // Don't allocate the dalvik style register pair passing.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100369 blocked_register_pairs_[R1_R2] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100370
371 // Stack register, LR and PC are always reserved.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100372 blocked_core_registers_[SP] = true;
373 blocked_core_registers_[LR] = true;
374 blocked_core_registers_[PC] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100375
376 // Reserve R4 for suspend check.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100377 blocked_core_registers_[R4] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100378
379 // Reserve thread register.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100380 blocked_core_registers_[TR] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100381
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +0100382 // Reserve temp register.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100383 blocked_core_registers_[IP] = true;
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +0100384
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100385 // TODO: We currently don't use Quick's callee saved registers.
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100386 // We always save and restore R6 and R7 to make sure we can use three
387 // register pairs for long operations.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100388 blocked_core_registers_[R5] = true;
389 blocked_core_registers_[R8] = true;
390 blocked_core_registers_[R10] = true;
391 blocked_core_registers_[R11] = true;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100392
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000393 blocked_fpu_registers_[S16] = true;
394 blocked_fpu_registers_[S17] = true;
395 blocked_fpu_registers_[S18] = true;
396 blocked_fpu_registers_[S19] = true;
397 blocked_fpu_registers_[S20] = true;
398 blocked_fpu_registers_[S21] = true;
399 blocked_fpu_registers_[S22] = true;
400 blocked_fpu_registers_[S23] = true;
Nicolas Geoffray3c035032014-10-28 10:46:40 +0000401 blocked_fpu_registers_[S24] = true;
402 blocked_fpu_registers_[S25] = true;
403 blocked_fpu_registers_[S26] = true;
404 blocked_fpu_registers_[S27] = true;
405 blocked_fpu_registers_[S28] = true;
406 blocked_fpu_registers_[S29] = true;
407 blocked_fpu_registers_[S30] = true;
408 blocked_fpu_registers_[S31] = true;
Calin Juravle34bacdf2014-10-07 20:23:36 +0100409
410 UpdateBlockedPairRegisters();
411}
412
413void CodeGeneratorARM::UpdateBlockedPairRegisters() const {
414 for (int i = 0; i < kNumberOfRegisterPairs; i++) {
415 ArmManagedRegister current =
416 ArmManagedRegister::FromRegisterPair(static_cast<RegisterPair>(i));
417 if (blocked_core_registers_[current.AsRegisterPairLow()]
418 || blocked_core_registers_[current.AsRegisterPairHigh()]) {
419 blocked_register_pairs_[i] = true;
420 }
421 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100422}
423
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100424InstructionCodeGeneratorARM::InstructionCodeGeneratorARM(HGraph* graph, CodeGeneratorARM* codegen)
425 : HGraphVisitor(graph),
426 assembler_(codegen->GetAssembler()),
427 codegen_(codegen) {}
428
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000429void CodeGeneratorARM::GenerateFrameEntry() {
Dave Allison648d7112014-07-25 16:15:27 -0700430 bool skip_overflow_check = IsLeafMethod() && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kArm);
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100431 if (!skip_overflow_check) {
432 if (kExplicitStackOverflowCheck) {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100433 SlowPathCodeARM* slow_path = new (GetGraph()->GetArena()) StackOverflowCheckSlowPathARM();
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100434 AddSlowPath(slow_path);
435
436 __ LoadFromOffset(kLoadWord, IP, TR, Thread::StackEndOffset<kArmWordSize>().Int32Value());
437 __ cmp(SP, ShifterOperand(IP));
438 __ b(slow_path->GetEntryLabel(), CC);
439 } else {
440 __ AddConstant(IP, SP, -static_cast<int32_t>(GetStackOverflowReservedBytes(kArm)));
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100441 __ LoadFromOffset(kLoadWord, IP, IP, 0);
Nicolas Geoffray39468442014-09-02 15:17:15 +0100442 RecordPcInfo(nullptr, 0);
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100443 }
444 }
445
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100446 core_spill_mask_ |= (1 << LR | 1 << R6 | 1 << R7);
447 __ PushList(1 << LR | 1 << R6 | 1 << R7);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000448
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100449 // The return PC has already been pushed on the stack.
Nicolas Geoffray707c8092014-04-04 10:50:14 +0100450 __ AddConstant(SP, -(GetFrameSize() - kNumberOfPushedRegistersAtEntry * kArmWordSize));
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100451 __ StoreToOffset(kStoreWord, R0, SP, 0);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000452}
453
454void CodeGeneratorARM::GenerateFrameExit() {
Nicolas Geoffray707c8092014-04-04 10:50:14 +0100455 __ AddConstant(SP, GetFrameSize() - kNumberOfPushedRegistersAtEntry * kArmWordSize);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100456 __ PopList(1 << PC | 1 << R6 | 1 << R7);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000457}
458
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100459void CodeGeneratorARM::Bind(HBasicBlock* block) {
460 __ Bind(GetLabelOf(block));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000461}
462
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100463Location CodeGeneratorARM::GetStackLocation(HLoadLocal* load) const {
464 switch (load->GetType()) {
465 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100466 case Primitive::kPrimDouble:
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100467 return Location::DoubleStackSlot(GetStackSlot(load->GetLocal()));
468 break;
469
470 case Primitive::kPrimInt:
471 case Primitive::kPrimNot:
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100472 case Primitive::kPrimFloat:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100473 return Location::StackSlot(GetStackSlot(load->GetLocal()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100474
475 case Primitive::kPrimBoolean:
476 case Primitive::kPrimByte:
477 case Primitive::kPrimChar:
478 case Primitive::kPrimShort:
479 case Primitive::kPrimVoid:
480 LOG(FATAL) << "Unexpected type " << load->GetType();
481 }
482
483 LOG(FATAL) << "Unreachable";
484 return Location();
485}
486
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100487Location InvokeDexCallingConventionVisitor::GetNextLocation(Primitive::Type type) {
488 switch (type) {
489 case Primitive::kPrimBoolean:
490 case Primitive::kPrimByte:
491 case Primitive::kPrimChar:
492 case Primitive::kPrimShort:
493 case Primitive::kPrimInt:
494 case Primitive::kPrimNot: {
495 uint32_t index = gp_index_++;
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000496 uint32_t stack_index = stack_index_++;
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100497 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100498 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100499 } else {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000500 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index));
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100501 }
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100502 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100503
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000504 case Primitive::kPrimLong: {
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100505 uint32_t index = gp_index_;
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000506 uint32_t stack_index = stack_index_;
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100507 gp_index_ += 2;
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000508 stack_index_ += 2;
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100509 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100510 ArmManagedRegister pair = ArmManagedRegister::FromRegisterPair(
511 calling_convention.GetRegisterPairAt(index));
512 return Location::RegisterPairLocation(pair.AsRegisterPairLow(), pair.AsRegisterPairHigh());
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100513 } else if (index + 1 == calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray0a6c4592014-10-30 16:37:57 +0000514 return Location::QuickParameter(index, stack_index);
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100515 } else {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000516 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index));
517 }
518 }
519
520 case Primitive::kPrimFloat: {
521 uint32_t stack_index = stack_index_++;
522 if (float_index_ % 2 == 0) {
523 float_index_ = std::max(double_index_, float_index_);
524 }
525 if (float_index_ < calling_convention.GetNumberOfFpuRegisters()) {
526 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(float_index_++));
527 } else {
528 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index));
529 }
530 }
531
532 case Primitive::kPrimDouble: {
533 double_index_ = std::max(double_index_, RoundUp(float_index_, 2));
534 uint32_t stack_index = stack_index_;
535 stack_index_ += 2;
536 if (double_index_ + 1 < calling_convention.GetNumberOfFpuRegisters()) {
537 uint32_t index = double_index_;
538 double_index_ += 2;
539 return Location::FpuRegisterPairLocation(
540 calling_convention.GetFpuRegisterAt(index),
541 calling_convention.GetFpuRegisterAt(index + 1));
542 } else {
543 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index));
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100544 }
545 }
546
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100547 case Primitive::kPrimVoid:
548 LOG(FATAL) << "Unexpected parameter type " << type;
549 break;
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100550 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100551 return Location();
552}
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100553
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000554Location InvokeDexCallingConventionVisitor::GetReturnLocation(Primitive::Type type) {
555 switch (type) {
556 case Primitive::kPrimBoolean:
557 case Primitive::kPrimByte:
558 case Primitive::kPrimChar:
559 case Primitive::kPrimShort:
560 case Primitive::kPrimInt:
561 case Primitive::kPrimNot: {
562 return Location::RegisterLocation(R0);
563 }
564
565 case Primitive::kPrimFloat: {
566 return Location::FpuRegisterLocation(S0);
567 }
568
569 case Primitive::kPrimLong: {
570 return Location::RegisterPairLocation(R0, R1);
571 }
572
573 case Primitive::kPrimDouble: {
574 return Location::FpuRegisterPairLocation(S0, S1);
575 }
576
577 case Primitive::kPrimVoid:
578 return Location();
579 }
580 UNREACHABLE();
581 return Location();
582}
583
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100584void CodeGeneratorARM::Move32(Location destination, Location source) {
585 if (source.Equals(destination)) {
586 return;
587 }
588 if (destination.IsRegister()) {
589 if (source.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100590 __ Mov(destination.As<Register>(), source.As<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100591 } else if (source.IsFpuRegister()) {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000592 __ vmovrs(destination.As<Register>(), source.As<SRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100593 } else {
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100594 __ LoadFromOffset(kLoadWord, destination.As<Register>(), SP, source.GetStackIndex());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100595 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100596 } else if (destination.IsFpuRegister()) {
597 if (source.IsRegister()) {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000598 __ vmovsr(destination.As<SRegister>(), source.As<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100599 } else if (source.IsFpuRegister()) {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000600 __ vmovs(destination.As<SRegister>(), source.As<SRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100601 } else {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000602 __ LoadSFromOffset(destination.As<SRegister>(), SP, source.GetStackIndex());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100603 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100604 } else {
605 DCHECK(destination.IsStackSlot());
606 if (source.IsRegister()) {
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100607 __ StoreToOffset(kStoreWord, source.As<Register>(), SP, destination.GetStackIndex());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100608 } else if (source.IsFpuRegister()) {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000609 __ StoreSToOffset(source.As<SRegister>(), SP, destination.GetStackIndex());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100610 } else {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100611 DCHECK(source.IsStackSlot());
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100612 __ LoadFromOffset(kLoadWord, IP, SP, source.GetStackIndex());
613 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100614 }
615 }
616}
617
618void CodeGeneratorARM::Move64(Location destination, Location source) {
619 if (source.Equals(destination)) {
620 return;
621 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100622 if (destination.IsRegisterPair()) {
623 if (source.IsRegisterPair()) {
624 __ Mov(destination.AsRegisterPairLow<Register>(), source.AsRegisterPairLow<Register>());
625 __ Mov(destination.AsRegisterPairHigh<Register>(), source.AsRegisterPairHigh<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100626 } else if (source.IsFpuRegister()) {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000627 UNIMPLEMENTED(FATAL);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100628 } else if (source.IsQuickParameter()) {
Nicolas Geoffray0a6c4592014-10-30 16:37:57 +0000629 uint16_t register_index = source.GetQuickParameterRegisterIndex();
630 uint16_t stack_index = source.GetQuickParameterStackIndex();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100631 InvokeDexCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100632 __ Mov(destination.AsRegisterPairLow<Register>(),
Nicolas Geoffray0a6c4592014-10-30 16:37:57 +0000633 calling_convention.GetRegisterAt(register_index));
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100634 __ LoadFromOffset(kLoadWord, destination.AsRegisterPairHigh<Register>(),
Nicolas Geoffray0a6c4592014-10-30 16:37:57 +0000635 SP, calling_convention.GetStackOffsetOf(stack_index + 1) + GetFrameSize());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100636 } else {
637 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100638 if (destination.AsRegisterPairLow<Register>() == R1) {
639 DCHECK_EQ(destination.AsRegisterPairHigh<Register>(), R2);
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100640 __ LoadFromOffset(kLoadWord, R1, SP, source.GetStackIndex());
641 __ LoadFromOffset(kLoadWord, R2, SP, source.GetHighStackIndex(kArmWordSize));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100642 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100643 __ LoadFromOffset(kLoadWordPair, destination.AsRegisterPairLow<Register>(),
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100644 SP, source.GetStackIndex());
645 }
646 }
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000647 } else if (destination.IsFpuRegisterPair()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100648 if (source.IsDoubleStackSlot()) {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000649 __ LoadDFromOffset(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()),
650 SP,
651 source.GetStackIndex());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100652 } else {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000653 UNIMPLEMENTED(FATAL);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100654 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100655 } else if (destination.IsQuickParameter()) {
656 InvokeDexCallingConvention calling_convention;
Nicolas Geoffray0a6c4592014-10-30 16:37:57 +0000657 uint16_t register_index = destination.GetQuickParameterRegisterIndex();
658 uint16_t stack_index = destination.GetQuickParameterStackIndex();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100659 if (source.IsRegisterPair()) {
Nicolas Geoffray0a6c4592014-10-30 16:37:57 +0000660 __ Mov(calling_convention.GetRegisterAt(register_index),
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100661 source.AsRegisterPairLow<Register>());
662 __ StoreToOffset(kStoreWord, source.AsRegisterPairHigh<Register>(),
Nicolas Geoffray0a6c4592014-10-30 16:37:57 +0000663 SP, calling_convention.GetStackOffsetOf(stack_index + 1));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100664 } else if (source.IsFpuRegister()) {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000665 UNIMPLEMENTED(FATAL);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100666 } else {
667 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000668 __ LoadFromOffset(
Nicolas Geoffray0a6c4592014-10-30 16:37:57 +0000669 kLoadWord, calling_convention.GetRegisterAt(register_index), SP, source.GetStackIndex());
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100670 __ LoadFromOffset(kLoadWord, R0, SP, source.GetHighStackIndex(kArmWordSize));
Nicolas Geoffray0a6c4592014-10-30 16:37:57 +0000671 __ StoreToOffset(kStoreWord, R0, SP, calling_convention.GetStackOffsetOf(stack_index + 1));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100672 }
673 } else {
674 DCHECK(destination.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100675 if (source.IsRegisterPair()) {
676 if (source.AsRegisterPairLow<Register>() == R1) {
677 DCHECK_EQ(source.AsRegisterPairHigh<Register>(), R2);
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100678 __ StoreToOffset(kStoreWord, R1, SP, destination.GetStackIndex());
679 __ StoreToOffset(kStoreWord, R2, SP, destination.GetHighStackIndex(kArmWordSize));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100680 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100681 __ StoreToOffset(kStoreWordPair, source.AsRegisterPairLow<Register>(),
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100682 SP, destination.GetStackIndex());
683 }
684 } else if (source.IsQuickParameter()) {
685 InvokeDexCallingConvention calling_convention;
Nicolas Geoffray0a6c4592014-10-30 16:37:57 +0000686 uint16_t register_index = source.GetQuickParameterRegisterIndex();
687 uint16_t stack_index = source.GetQuickParameterStackIndex();
688 __ StoreToOffset(kStoreWord, calling_convention.GetRegisterAt(register_index),
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100689 SP, destination.GetStackIndex());
690 __ LoadFromOffset(kLoadWord, R0,
Nicolas Geoffray0a6c4592014-10-30 16:37:57 +0000691 SP, calling_convention.GetStackOffsetOf(stack_index + 1) + GetFrameSize());
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100692 __ StoreToOffset(kStoreWord, R0, SP, destination.GetHighStackIndex(kArmWordSize));
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000693 } else if (source.IsFpuRegisterPair()) {
694 __ StoreDToOffset(FromLowSToD(source.AsFpuRegisterPairLow<SRegister>()),
695 SP,
696 destination.GetStackIndex());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100697 } else {
698 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100699 __ LoadFromOffset(kLoadWord, IP, SP, source.GetStackIndex());
700 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
701 __ LoadFromOffset(kLoadWord, IP, SP, source.GetHighStackIndex(kArmWordSize));
702 __ StoreToOffset(kStoreWord, IP, SP, destination.GetHighStackIndex(kArmWordSize));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100703 }
704 }
705}
706
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100707void CodeGeneratorARM::Move(HInstruction* instruction, Location location, HInstruction* move_for) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100708 LocationSummary* locations = instruction->GetLocations();
709 if (locations != nullptr && locations->Out().Equals(location)) {
710 return;
711 }
712
Roland Levillain476df552014-10-09 17:51:36 +0100713 if (instruction->IsIntConstant()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100714 int32_t value = instruction->AsIntConstant()->GetValue();
715 if (location.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100716 __ LoadImmediate(location.As<Register>(), value);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100717 } else {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100718 DCHECK(location.IsStackSlot());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +0100719 __ LoadImmediate(IP, value);
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100720 __ StoreToOffset(kStoreWord, IP, SP, location.GetStackIndex());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100721 }
Roland Levillain476df552014-10-09 17:51:36 +0100722 } else if (instruction->IsLongConstant()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100723 int64_t value = instruction->AsLongConstant()->GetValue();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100724 if (location.IsRegisterPair()) {
725 __ LoadImmediate(location.AsRegisterPairLow<Register>(), Low32Bits(value));
726 __ LoadImmediate(location.AsRegisterPairHigh<Register>(), High32Bits(value));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100727 } else {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100728 DCHECK(location.IsDoubleStackSlot());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +0100729 __ LoadImmediate(IP, Low32Bits(value));
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100730 __ StoreToOffset(kStoreWord, IP, SP, location.GetStackIndex());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +0100731 __ LoadImmediate(IP, High32Bits(value));
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100732 __ StoreToOffset(kStoreWord, IP, SP, location.GetHighStackIndex(kArmWordSize));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100733 }
Roland Levillain476df552014-10-09 17:51:36 +0100734 } else if (instruction->IsLoadLocal()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100735 uint32_t stack_slot = GetStackSlot(instruction->AsLoadLocal()->GetLocal());
736 switch (instruction->GetType()) {
737 case Primitive::kPrimBoolean:
738 case Primitive::kPrimByte:
739 case Primitive::kPrimChar:
740 case Primitive::kPrimShort:
741 case Primitive::kPrimInt:
742 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100743 case Primitive::kPrimFloat:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100744 Move32(location, Location::StackSlot(stack_slot));
745 break;
746
747 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100748 case Primitive::kPrimDouble:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100749 Move64(location, Location::DoubleStackSlot(stack_slot));
750 break;
751
752 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100753 LOG(FATAL) << "Unexpected type " << instruction->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100754 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000755 } else {
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100756 DCHECK((instruction->GetNext() == move_for) || instruction->GetNext()->IsTemporary());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100757 switch (instruction->GetType()) {
758 case Primitive::kPrimBoolean:
759 case Primitive::kPrimByte:
760 case Primitive::kPrimChar:
761 case Primitive::kPrimShort:
762 case Primitive::kPrimNot:
763 case Primitive::kPrimInt:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100764 case Primitive::kPrimFloat:
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100765 Move32(location, locations->Out());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100766 break;
767
768 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100769 case Primitive::kPrimDouble:
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100770 Move64(location, locations->Out());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100771 break;
772
773 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100774 LOG(FATAL) << "Unexpected type " << instruction->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100775 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000776 }
777}
778
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100779void CodeGeneratorARM::InvokeRuntime(int32_t entry_point_offset,
780 HInstruction* instruction,
781 uint32_t dex_pc) {
782 __ LoadFromOffset(kLoadWord, LR, TR, entry_point_offset);
783 __ blx(LR);
784 RecordPcInfo(instruction, dex_pc);
785 DCHECK(instruction->IsSuspendCheck()
786 || instruction->IsBoundsCheck()
787 || instruction->IsNullCheck()
788 || !IsLeafMethod());
789}
790
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000791void LocationsBuilderARM::VisitGoto(HGoto* got) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000792 got->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000793}
794
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000795void InstructionCodeGeneratorARM::VisitGoto(HGoto* got) {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000796 HBasicBlock* successor = got->GetSuccessor();
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100797 DCHECK(!successor->IsExitBlock());
798
799 HBasicBlock* block = got->GetBlock();
800 HInstruction* previous = got->GetPrevious();
801
802 HLoopInformation* info = block->GetLoopInformation();
803 if (info != nullptr && info->IsBackEdge(block) && info->HasSuspendCheck()) {
804 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
805 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
806 return;
807 }
808
809 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
810 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
811 }
812 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000813 __ b(codegen_->GetLabelOf(successor));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000814 }
815}
816
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000817void LocationsBuilderARM::VisitExit(HExit* exit) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000818 exit->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000819}
820
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000821void InstructionCodeGeneratorARM::VisitExit(HExit* exit) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700822 UNUSED(exit);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000823 if (kIsDebugBuild) {
824 __ Comment("Unreachable");
825 __ bkpt(0);
826 }
827}
828
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000829void LocationsBuilderARM::VisitIf(HIf* if_instr) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100830 LocationSummary* locations =
831 new (GetGraph()->GetArena()) LocationSummary(if_instr, LocationSummary::kNoCall);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100832 HInstruction* cond = if_instr->InputAt(0);
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100833 if (!cond->IsCondition() || cond->AsCondition()->NeedsMaterialization()) {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +0100834 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100835 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000836}
837
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000838void InstructionCodeGeneratorARM::VisitIf(HIf* if_instr) {
Dave Allison20dfc792014-06-16 20:44:29 -0700839 HInstruction* cond = if_instr->InputAt(0);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100840 if (cond->IsIntConstant()) {
841 // Constant condition, statically compared against 1.
842 int32_t cond_value = cond->AsIntConstant()->GetValue();
843 if (cond_value == 1) {
844 if (!codegen_->GoesToNextBlock(if_instr->GetBlock(),
845 if_instr->IfTrueSuccessor())) {
846 __ b(codegen_->GetLabelOf(if_instr->IfTrueSuccessor()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100847 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100848 return;
849 } else {
850 DCHECK_EQ(cond_value, 0);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100851 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100852 } else {
853 if (!cond->IsCondition() || cond->AsCondition()->NeedsMaterialization()) {
854 // Condition has been materialized, compare the output to 0
855 DCHECK(if_instr->GetLocations()->InAt(0).IsRegister());
856 __ cmp(if_instr->GetLocations()->InAt(0).As<Register>(),
857 ShifterOperand(0));
858 __ b(codegen_->GetLabelOf(if_instr->IfTrueSuccessor()), NE);
859 } else {
860 // Condition has not been materialized, use its inputs as the
861 // comparison and its condition as the branch condition.
862 LocationSummary* locations = cond->GetLocations();
863 if (locations->InAt(1).IsRegister()) {
864 __ cmp(locations->InAt(0).As<Register>(),
865 ShifterOperand(locations->InAt(1).As<Register>()));
866 } else {
867 DCHECK(locations->InAt(1).IsConstant());
868 int32_t value =
869 locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
870 ShifterOperand operand;
871 if (ShifterOperand::CanHoldArm(value, &operand)) {
872 __ cmp(locations->InAt(0).As<Register>(), ShifterOperand(value));
873 } else {
874 Register temp = IP;
875 __ LoadImmediate(temp, value);
876 __ cmp(locations->InAt(0).As<Register>(), ShifterOperand(temp));
877 }
878 }
879 __ b(codegen_->GetLabelOf(if_instr->IfTrueSuccessor()),
880 ARMCondition(cond->AsCondition()->GetCondition()));
881 }
Dave Allison20dfc792014-06-16 20:44:29 -0700882 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100883 if (!codegen_->GoesToNextBlock(if_instr->GetBlock(),
884 if_instr->IfFalseSuccessor())) {
Dave Allison20dfc792014-06-16 20:44:29 -0700885 __ b(codegen_->GetLabelOf(if_instr->IfFalseSuccessor()));
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000886 }
887}
888
Dave Allison20dfc792014-06-16 20:44:29 -0700889
890void LocationsBuilderARM::VisitCondition(HCondition* comp) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100891 LocationSummary* locations =
892 new (GetGraph()->GetArena()) LocationSummary(comp, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +0100893 locations->SetInAt(0, Location::RequiresRegister());
894 locations->SetInAt(1, Location::RegisterOrConstant(comp->InputAt(1)));
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100895 if (comp->NeedsMaterialization()) {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +0100896 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100897 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000898}
899
Dave Allison20dfc792014-06-16 20:44:29 -0700900void InstructionCodeGeneratorARM::VisitCondition(HCondition* comp) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100901 if (!comp->NeedsMaterialization()) return;
902
903 LocationSummary* locations = comp->GetLocations();
904 if (locations->InAt(1).IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100905 __ cmp(locations->InAt(0).As<Register>(),
906 ShifterOperand(locations->InAt(1).As<Register>()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100907 } else {
908 DCHECK(locations->InAt(1).IsConstant());
909 int32_t value = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
910 ShifterOperand operand;
911 if (ShifterOperand::CanHoldArm(value, &operand)) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100912 __ cmp(locations->InAt(0).As<Register>(), ShifterOperand(value));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100913 } else {
914 Register temp = IP;
915 __ LoadImmediate(temp, value);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100916 __ cmp(locations->InAt(0).As<Register>(), ShifterOperand(temp));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100917 }
Dave Allison20dfc792014-06-16 20:44:29 -0700918 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100919 __ it(ARMCondition(comp->GetCondition()), kItElse);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100920 __ mov(locations->Out().As<Register>(), ShifterOperand(1),
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100921 ARMCondition(comp->GetCondition()));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100922 __ mov(locations->Out().As<Register>(), ShifterOperand(0),
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100923 ARMOppositeCondition(comp->GetCondition()));
Dave Allison20dfc792014-06-16 20:44:29 -0700924}
925
926void LocationsBuilderARM::VisitEqual(HEqual* comp) {
927 VisitCondition(comp);
928}
929
930void InstructionCodeGeneratorARM::VisitEqual(HEqual* comp) {
931 VisitCondition(comp);
932}
933
934void LocationsBuilderARM::VisitNotEqual(HNotEqual* comp) {
935 VisitCondition(comp);
936}
937
938void InstructionCodeGeneratorARM::VisitNotEqual(HNotEqual* comp) {
939 VisitCondition(comp);
940}
941
942void LocationsBuilderARM::VisitLessThan(HLessThan* comp) {
943 VisitCondition(comp);
944}
945
946void InstructionCodeGeneratorARM::VisitLessThan(HLessThan* comp) {
947 VisitCondition(comp);
948}
949
950void LocationsBuilderARM::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
951 VisitCondition(comp);
952}
953
954void InstructionCodeGeneratorARM::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
955 VisitCondition(comp);
956}
957
958void LocationsBuilderARM::VisitGreaterThan(HGreaterThan* comp) {
959 VisitCondition(comp);
960}
961
962void InstructionCodeGeneratorARM::VisitGreaterThan(HGreaterThan* comp) {
963 VisitCondition(comp);
964}
965
966void LocationsBuilderARM::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
967 VisitCondition(comp);
968}
969
970void InstructionCodeGeneratorARM::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
971 VisitCondition(comp);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000972}
973
974void LocationsBuilderARM::VisitLocal(HLocal* local) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000975 local->SetLocations(nullptr);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000976}
977
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000978void InstructionCodeGeneratorARM::VisitLocal(HLocal* local) {
979 DCHECK_EQ(local->GetBlock(), GetGraph()->GetEntryBlock());
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000980}
981
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000982void LocationsBuilderARM::VisitLoadLocal(HLoadLocal* load) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100983 load->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000984}
985
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000986void InstructionCodeGeneratorARM::VisitLoadLocal(HLoadLocal* load) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100987 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700988 UNUSED(load);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000989}
990
991void LocationsBuilderARM::VisitStoreLocal(HStoreLocal* store) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100992 LocationSummary* locations =
993 new (GetGraph()->GetArena()) LocationSummary(store, LocationSummary::kNoCall);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100994 switch (store->InputAt(1)->GetType()) {
995 case Primitive::kPrimBoolean:
996 case Primitive::kPrimByte:
997 case Primitive::kPrimChar:
998 case Primitive::kPrimShort:
999 case Primitive::kPrimInt:
1000 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001001 case Primitive::kPrimFloat:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001002 locations->SetInAt(1, Location::StackSlot(codegen_->GetStackSlot(store->GetLocal())));
1003 break;
1004
1005 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001006 case Primitive::kPrimDouble:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001007 locations->SetInAt(1, Location::DoubleStackSlot(codegen_->GetStackSlot(store->GetLocal())));
1008 break;
1009
1010 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001011 LOG(FATAL) << "Unexpected local type " << store->InputAt(1)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001012 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001013}
1014
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001015void InstructionCodeGeneratorARM::VisitStoreLocal(HStoreLocal* store) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001016 UNUSED(store);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001017}
1018
1019void LocationsBuilderARM::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001020 LocationSummary* locations =
1021 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001022 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001023}
1024
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001025void InstructionCodeGeneratorARM::VisitIntConstant(HIntConstant* constant) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001026 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001027 UNUSED(constant);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001028}
1029
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001030void LocationsBuilderARM::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001031 LocationSummary* locations =
1032 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001033 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001034}
1035
1036void InstructionCodeGeneratorARM::VisitLongConstant(HLongConstant* constant) {
1037 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001038 UNUSED(constant);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001039}
1040
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001041void LocationsBuilderARM::VisitFloatConstant(HFloatConstant* constant) {
1042 LocationSummary* locations =
1043 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1044 locations->SetOut(Location::ConstantLocation(constant));
1045}
1046
1047void InstructionCodeGeneratorARM::VisitFloatConstant(HFloatConstant* constant) {
1048 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001049 UNUSED(constant);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001050}
1051
1052void LocationsBuilderARM::VisitDoubleConstant(HDoubleConstant* constant) {
1053 LocationSummary* locations =
1054 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1055 locations->SetOut(Location::ConstantLocation(constant));
1056}
1057
1058void InstructionCodeGeneratorARM::VisitDoubleConstant(HDoubleConstant* constant) {
1059 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001060 UNUSED(constant);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001061}
1062
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001063void LocationsBuilderARM::VisitReturnVoid(HReturnVoid* ret) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001064 ret->SetLocations(nullptr);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001065}
1066
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001067void InstructionCodeGeneratorARM::VisitReturnVoid(HReturnVoid* ret) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001068 UNUSED(ret);
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001069 codegen_->GenerateFrameExit();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001070}
1071
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001072void LocationsBuilderARM::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001073 LocationSummary* locations =
1074 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001075 locations->SetInAt(0, parameter_visitor_.GetReturnLocation(ret->InputAt(0)->GetType()));
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001076}
1077
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001078void InstructionCodeGeneratorARM::VisitReturn(HReturn* ret) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001079 UNUSED(ret);
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001080 codegen_->GenerateFrameExit();
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001081}
1082
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001083void LocationsBuilderARM::VisitInvokeStatic(HInvokeStatic* invoke) {
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001084 HandleInvoke(invoke);
1085}
1086
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001087void CodeGeneratorARM::LoadCurrentMethod(Register reg) {
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001088 __ LoadFromOffset(kLoadWord, reg, SP, kCurrentMethodStackOffset);
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001089}
1090
1091void InstructionCodeGeneratorARM::VisitInvokeStatic(HInvokeStatic* invoke) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001092 Register temp = invoke->GetLocations()->GetTemp(0).As<Register>();
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001093
1094 // TODO: Implement all kinds of calls:
1095 // 1) boot -> boot
1096 // 2) app -> boot
1097 // 3) app -> app
1098 //
1099 // Currently we implement the app -> app logic, which looks up in the resolve cache.
1100
1101 // temp = method;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001102 codegen_->LoadCurrentMethod(temp);
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001103 // temp = temp->dex_cache_resolved_methods_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001104 __ LoadFromOffset(
1105 kLoadWord, temp, temp, mirror::ArtMethod::DexCacheResolvedMethodsOffset().Int32Value());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001106 // temp = temp[index_in_cache]
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001107 __ LoadFromOffset(
1108 kLoadWord, temp, temp, CodeGenerator::GetCacheOffset(invoke->GetIndexInDexCache()));
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001109 // LR = temp[offset_of_quick_compiled_code]
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001110 __ LoadFromOffset(kLoadWord, LR, temp,
1111 mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset().Int32Value());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001112 // LR()
1113 __ blx(LR);
1114
1115 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
1116 DCHECK(!codegen_->IsLeafMethod());
1117}
1118
1119void LocationsBuilderARM::VisitInvokeVirtual(HInvokeVirtual* invoke) {
1120 HandleInvoke(invoke);
1121}
1122
1123void LocationsBuilderARM::HandleInvoke(HInvoke* invoke) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001124 LocationSummary* locations =
1125 new (GetGraph()->GetArena()) LocationSummary(invoke, LocationSummary::kCall);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001126 locations->AddTemp(Location::RegisterLocation(R0));
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001127
1128 InvokeDexCallingConventionVisitor calling_convention_visitor;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001129 for (size_t i = 0; i < invoke->InputCount(); i++) {
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001130 HInstruction* input = invoke->InputAt(i);
1131 locations->SetInAt(i, calling_convention_visitor.GetNextLocation(input->GetType()));
1132 }
1133
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001134 locations->SetOut(calling_convention_visitor.GetReturnLocation(invoke->GetType()));
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001135}
1136
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001137
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001138void InstructionCodeGeneratorARM::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001139 Register temp = invoke->GetLocations()->GetTemp(0).As<Register>();
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001140 uint32_t method_offset = mirror::Class::EmbeddedVTableOffset().Uint32Value() +
1141 invoke->GetVTableIndex() * sizeof(mirror::Class::VTableEntry);
1142 LocationSummary* locations = invoke->GetLocations();
1143 Location receiver = locations->InAt(0);
1144 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
1145 // temp = object->GetClass();
1146 if (receiver.IsStackSlot()) {
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001147 __ LoadFromOffset(kLoadWord, temp, SP, receiver.GetStackIndex());
1148 __ LoadFromOffset(kLoadWord, temp, temp, class_offset);
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001149 } else {
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001150 __ LoadFromOffset(kLoadWord, temp, receiver.As<Register>(), class_offset);
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001151 }
1152 // temp = temp->GetMethodAt(method_offset);
1153 uint32_t entry_point = mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset().Int32Value();
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001154 __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001155 // LR = temp->GetEntryPoint();
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001156 __ LoadFromOffset(kLoadWord, LR, temp, entry_point);
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001157 // LR();
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001158 __ blx(LR);
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001159 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001160 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001161}
1162
Roland Levillain88cb1752014-10-20 16:36:47 +01001163void LocationsBuilderARM::VisitNeg(HNeg* neg) {
1164 LocationSummary* locations =
1165 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
1166 switch (neg->GetResultType()) {
1167 case Primitive::kPrimInt:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001168 case Primitive::kPrimLong: {
1169 bool output_overlaps = (neg->GetResultType() == Primitive::kPrimLong);
Roland Levillain88cb1752014-10-20 16:36:47 +01001170 locations->SetInAt(0, Location::RequiresRegister());
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001171 locations->SetOut(Location::RequiresRegister(), output_overlaps);
Roland Levillain88cb1752014-10-20 16:36:47 +01001172 break;
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001173 }
Roland Levillain88cb1752014-10-20 16:36:47 +01001174
Roland Levillain88cb1752014-10-20 16:36:47 +01001175 case Primitive::kPrimFloat:
1176 case Primitive::kPrimDouble:
1177 LOG(FATAL) << "Not yet implemented neg type " << neg->GetResultType();
1178 break;
1179
1180 default:
1181 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1182 }
1183}
1184
1185void InstructionCodeGeneratorARM::VisitNeg(HNeg* neg) {
1186 LocationSummary* locations = neg->GetLocations();
1187 Location out = locations->Out();
1188 Location in = locations->InAt(0);
1189 switch (neg->GetResultType()) {
1190 case Primitive::kPrimInt:
1191 DCHECK(in.IsRegister());
Roland Levillainb762d2e2014-10-22 10:11:06 +01001192 __ rsb(out.As<Register>(), in.As<Register>(), ShifterOperand(0));
Roland Levillain88cb1752014-10-20 16:36:47 +01001193 break;
1194
1195 case Primitive::kPrimLong:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001196 DCHECK(in.IsRegisterPair());
1197 // out.lo = 0 - in.lo (and update the carry/borrow (C) flag)
1198 __ rsbs(out.AsRegisterPairLow<Register>(),
1199 in.AsRegisterPairLow<Register>(),
1200 ShifterOperand(0));
1201 // We cannot emit an RSC (Reverse Subtract with Carry)
1202 // instruction here, as it does not exist in the Thumb-2
1203 // instruction set. We use the following approach
1204 // using SBC and SUB instead.
1205 //
1206 // out.hi = -C
1207 __ sbc(out.AsRegisterPairHigh<Register>(),
1208 out.AsRegisterPairHigh<Register>(),
1209 ShifterOperand(out.AsRegisterPairHigh<Register>()));
1210 // out.hi = out.hi - in.hi
1211 __ sub(out.AsRegisterPairHigh<Register>(),
1212 out.AsRegisterPairHigh<Register>(),
1213 ShifterOperand(in.AsRegisterPairHigh<Register>()));
1214 break;
1215
Roland Levillain88cb1752014-10-20 16:36:47 +01001216 case Primitive::kPrimFloat:
1217 case Primitive::kPrimDouble:
1218 LOG(FATAL) << "Not yet implemented neg type " << neg->GetResultType();
1219 break;
1220
1221 default:
1222 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1223 }
1224}
1225
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001226void LocationsBuilderARM::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001227 LocationSummary* locations =
1228 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001229 switch (add->GetResultType()) {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001230 case Primitive::kPrimInt:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001231 case Primitive::kPrimLong: {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01001232 bool output_overlaps = (add->GetResultType() == Primitive::kPrimLong);
1233 locations->SetInAt(0, Location::RequiresRegister());
1234 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
1235 locations->SetOut(Location::RequiresRegister(), output_overlaps);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001236 break;
1237 }
1238
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001239 case Primitive::kPrimFloat:
1240 case Primitive::kPrimDouble: {
1241 locations->SetInAt(0, Location::RequiresFpuRegister());
1242 locations->SetInAt(1, Location::RequiresFpuRegister());
Calin Juravle7c4954d2014-10-28 16:57:40 +00001243 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001244 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001245 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001246
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001247 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001248 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001249 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001250}
1251
1252void InstructionCodeGeneratorARM::VisitAdd(HAdd* add) {
1253 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001254 Location out = locations->Out();
1255 Location first = locations->InAt(0);
1256 Location second = locations->InAt(1);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001257 switch (add->GetResultType()) {
1258 case Primitive::kPrimInt:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001259 if (second.IsRegister()) {
1260 __ add(out.As<Register>(), first.As<Register>(), ShifterOperand(second.As<Register>()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001261 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001262 __ AddConstant(out.As<Register>(),
1263 first.As<Register>(),
1264 second.GetConstant()->AsIntConstant()->GetValue());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001265 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001266 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001267
1268 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001269 __ adds(out.AsRegisterPairLow<Register>(),
1270 first.AsRegisterPairLow<Register>(),
1271 ShifterOperand(second.AsRegisterPairLow<Register>()));
1272 __ adc(out.AsRegisterPairHigh<Register>(),
1273 first.AsRegisterPairHigh<Register>(),
1274 ShifterOperand(second.AsRegisterPairHigh<Register>()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001275 break;
1276
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001277 case Primitive::kPrimFloat:
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001278 __ vadds(out.As<SRegister>(), first.As<SRegister>(), second.As<SRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001279 break;
1280
1281 case Primitive::kPrimDouble:
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001282 __ vaddd(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
1283 FromLowSToD(first.AsFpuRegisterPairLow<SRegister>()),
1284 FromLowSToD(second.AsFpuRegisterPairLow<SRegister>()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001285 break;
1286
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001287 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001288 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001289 }
1290}
1291
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001292void LocationsBuilderARM::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001293 LocationSummary* locations =
1294 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001295 switch (sub->GetResultType()) {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001296 case Primitive::kPrimInt:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001297 case Primitive::kPrimLong: {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01001298 bool output_overlaps = (sub->GetResultType() == Primitive::kPrimLong);
1299 locations->SetInAt(0, Location::RequiresRegister());
1300 locations->SetInAt(1, Location::RegisterOrConstant(sub->InputAt(1)));
1301 locations->SetOut(Location::RequiresRegister(), output_overlaps);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001302 break;
1303 }
Calin Juravle11351682014-10-23 15:38:15 +01001304 case Primitive::kPrimFloat:
1305 case Primitive::kPrimDouble: {
1306 locations->SetInAt(0, Location::RequiresFpuRegister());
1307 locations->SetInAt(1, Location::RequiresFpuRegister());
Calin Juravle7c4954d2014-10-28 16:57:40 +00001308 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001309 break;
Calin Juravle11351682014-10-23 15:38:15 +01001310 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001311 default:
Calin Juravle11351682014-10-23 15:38:15 +01001312 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001313 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001314}
1315
1316void InstructionCodeGeneratorARM::VisitSub(HSub* sub) {
1317 LocationSummary* locations = sub->GetLocations();
Calin Juravle11351682014-10-23 15:38:15 +01001318 Location out = locations->Out();
1319 Location first = locations->InAt(0);
1320 Location second = locations->InAt(1);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001321 switch (sub->GetResultType()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001322 case Primitive::kPrimInt: {
Calin Juravle11351682014-10-23 15:38:15 +01001323 if (second.IsRegister()) {
1324 __ sub(out.As<Register>(), first.As<Register>(), ShifterOperand(second.As<Register>()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001325 } else {
Calin Juravle11351682014-10-23 15:38:15 +01001326 __ AddConstant(out.As<Register>(),
1327 first.As<Register>(),
1328 -second.GetConstant()->AsIntConstant()->GetValue());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001329 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001330 break;
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001331 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001332
Calin Juravle11351682014-10-23 15:38:15 +01001333 case Primitive::kPrimLong: {
1334 __ subs(out.AsRegisterPairLow<Register>(),
1335 first.AsRegisterPairLow<Register>(),
1336 ShifterOperand(second.AsRegisterPairLow<Register>()));
1337 __ sbc(out.AsRegisterPairHigh<Register>(),
1338 first.AsRegisterPairHigh<Register>(),
1339 ShifterOperand(second.AsRegisterPairHigh<Register>()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001340 break;
Calin Juravle11351682014-10-23 15:38:15 +01001341 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001342
Calin Juravle11351682014-10-23 15:38:15 +01001343 case Primitive::kPrimFloat: {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001344 __ vsubs(out.As<SRegister>(), first.As<SRegister>(), second.As<SRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001345 break;
Calin Juravle11351682014-10-23 15:38:15 +01001346 }
1347
1348 case Primitive::kPrimDouble: {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001349 __ vsubd(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
1350 FromLowSToD(first.AsFpuRegisterPairLow<SRegister>()),
1351 FromLowSToD(second.AsFpuRegisterPairLow<SRegister>()));
Calin Juravle11351682014-10-23 15:38:15 +01001352 break;
1353 }
1354
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001355
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001356 default:
Calin Juravle11351682014-10-23 15:38:15 +01001357 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001358 }
1359}
1360
Calin Juravle34bacdf2014-10-07 20:23:36 +01001361void LocationsBuilderARM::VisitMul(HMul* mul) {
1362 LocationSummary* locations =
1363 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
1364 switch (mul->GetResultType()) {
1365 case Primitive::kPrimInt:
1366 case Primitive::kPrimLong: {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01001367 locations->SetInAt(0, Location::RequiresRegister());
1368 locations->SetInAt(1, Location::RequiresRegister());
1369 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Calin Juravle34bacdf2014-10-07 20:23:36 +01001370 break;
1371 }
1372
Calin Juravleb5bfa962014-10-21 18:02:24 +01001373 case Primitive::kPrimFloat:
1374 case Primitive::kPrimDouble: {
1375 locations->SetInAt(0, Location::RequiresFpuRegister());
1376 locations->SetInAt(1, Location::RequiresFpuRegister());
Calin Juravle7c4954d2014-10-28 16:57:40 +00001377 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Calin Juravle34bacdf2014-10-07 20:23:36 +01001378 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01001379 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01001380
1381 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01001382 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01001383 }
1384}
1385
1386void InstructionCodeGeneratorARM::VisitMul(HMul* mul) {
1387 LocationSummary* locations = mul->GetLocations();
1388 Location out = locations->Out();
1389 Location first = locations->InAt(0);
1390 Location second = locations->InAt(1);
1391 switch (mul->GetResultType()) {
1392 case Primitive::kPrimInt: {
1393 __ mul(out.As<Register>(), first.As<Register>(), second.As<Register>());
1394 break;
1395 }
1396 case Primitive::kPrimLong: {
1397 Register out_hi = out.AsRegisterPairHigh<Register>();
1398 Register out_lo = out.AsRegisterPairLow<Register>();
1399 Register in1_hi = first.AsRegisterPairHigh<Register>();
1400 Register in1_lo = first.AsRegisterPairLow<Register>();
1401 Register in2_hi = second.AsRegisterPairHigh<Register>();
1402 Register in2_lo = second.AsRegisterPairLow<Register>();
1403
1404 // Extra checks to protect caused by the existence of R1_R2.
1405 // The algorithm is wrong if out.hi is either in1.lo or in2.lo:
1406 // (e.g. in1=r0_r1, in2=r2_r3 and out=r1_r2);
1407 DCHECK_NE(out_hi, in1_lo);
1408 DCHECK_NE(out_hi, in2_lo);
1409
1410 // input: in1 - 64 bits, in2 - 64 bits
1411 // output: out
1412 // formula: out.hi : out.lo = (in1.lo * in2.hi + in1.hi * in2.lo)* 2^32 + in1.lo * in2.lo
1413 // parts: out.hi = in1.lo * in2.hi + in1.hi * in2.lo + (in1.lo * in2.lo)[63:32]
1414 // parts: out.lo = (in1.lo * in2.lo)[31:0]
1415
1416 // IP <- in1.lo * in2.hi
1417 __ mul(IP, in1_lo, in2_hi);
1418 // out.hi <- in1.lo * in2.hi + in1.hi * in2.lo
1419 __ mla(out_hi, in1_hi, in2_lo, IP);
1420 // out.lo <- (in1.lo * in2.lo)[31:0];
1421 __ umull(out_lo, IP, in1_lo, in2_lo);
1422 // out.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
1423 __ add(out_hi, out_hi, ShifterOperand(IP));
1424 break;
1425 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01001426
1427 case Primitive::kPrimFloat: {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001428 __ vmuls(out.As<SRegister>(), first.As<SRegister>(), second.As<SRegister>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01001429 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01001430 }
1431
1432 case Primitive::kPrimDouble: {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001433 __ vmuld(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
1434 FromLowSToD(first.AsFpuRegisterPairLow<SRegister>()),
1435 FromLowSToD(second.AsFpuRegisterPairLow<SRegister>()));
Calin Juravleb5bfa962014-10-21 18:02:24 +01001436 break;
1437 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01001438
1439 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01001440 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01001441 }
1442}
1443
Calin Juravle7c4954d2014-10-28 16:57:40 +00001444void LocationsBuilderARM::VisitDiv(HDiv* div) {
1445 LocationSummary* locations =
1446 new (GetGraph()->GetArena()) LocationSummary(div, LocationSummary::kNoCall);
1447 switch (div->GetResultType()) {
1448 case Primitive::kPrimInt:
1449 case Primitive::kPrimLong: {
1450 LOG(FATAL) << "Not implemented div type" << div->GetResultType();
1451 break;
1452 }
1453 case Primitive::kPrimFloat:
1454 case Primitive::kPrimDouble: {
1455 locations->SetInAt(0, Location::RequiresFpuRegister());
1456 locations->SetInAt(1, Location::RequiresFpuRegister());
1457 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
1458 break;
1459 }
1460
1461 default:
1462 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
1463 }
1464}
1465
1466void InstructionCodeGeneratorARM::VisitDiv(HDiv* div) {
1467 LocationSummary* locations = div->GetLocations();
1468 Location out = locations->Out();
1469 Location first = locations->InAt(0);
1470 Location second = locations->InAt(1);
1471
1472 switch (div->GetResultType()) {
1473 case Primitive::kPrimInt:
1474 case Primitive::kPrimLong: {
1475 LOG(FATAL) << "Not implemented div type" << div->GetResultType();
1476 break;
1477 }
1478
1479 case Primitive::kPrimFloat: {
1480 __ vdivs(out.As<SRegister>(), first.As<SRegister>(), second.As<SRegister>());
1481 break;
1482 }
1483
1484 case Primitive::kPrimDouble: {
1485 __ vdivd(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
1486 FromLowSToD(first.AsFpuRegisterPairLow<SRegister>()),
1487 FromLowSToD(second.AsFpuRegisterPairLow<SRegister>()));
1488 break;
1489 }
1490
1491 default:
1492 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
1493 }
1494}
1495
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001496void LocationsBuilderARM::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001497 LocationSummary* locations =
1498 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001499 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001500 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1501 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
1502 locations->SetOut(Location::RegisterLocation(R0));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001503}
1504
1505void InstructionCodeGeneratorARM::VisitNewInstance(HNewInstance* instruction) {
1506 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001507 codegen_->LoadCurrentMethod(calling_convention.GetRegisterAt(1));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001508 __ LoadImmediate(calling_convention.GetRegisterAt(0), instruction->GetTypeIndex());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001509 codegen_->InvokeRuntime(
1510 QUICK_ENTRY_POINT(pAllocObjectWithAccessCheck), instruction, instruction->GetDexPc());
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001511}
1512
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001513void LocationsBuilderARM::VisitNewArray(HNewArray* instruction) {
1514 LocationSummary* locations =
1515 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
1516 InvokeRuntimeCallingConvention calling_convention;
1517 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1518 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
1519 locations->SetOut(Location::RegisterLocation(R0));
1520 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
1521}
1522
1523void InstructionCodeGeneratorARM::VisitNewArray(HNewArray* instruction) {
1524 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001525 codegen_->LoadCurrentMethod(calling_convention.GetRegisterAt(1));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001526 __ LoadImmediate(calling_convention.GetRegisterAt(0), instruction->GetTypeIndex());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001527 codegen_->InvokeRuntime(
1528 QUICK_ENTRY_POINT(pAllocArrayWithAccessCheck), instruction, instruction->GetDexPc());
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001529}
1530
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001531void LocationsBuilderARM::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001532 LocationSummary* locations =
1533 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001534 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
1535 if (location.IsStackSlot()) {
1536 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
1537 } else if (location.IsDoubleStackSlot()) {
1538 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001539 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001540 locations->SetOut(location);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001541}
1542
1543void InstructionCodeGeneratorARM::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001544 // Nothing to do, the parameter is already at its location.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001545 UNUSED(instruction);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001546}
1547
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001548void LocationsBuilderARM::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001549 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001550 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01001551 locations->SetInAt(0, Location::RequiresRegister());
1552 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01001553}
1554
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001555void InstructionCodeGeneratorARM::VisitNot(HNot* not_) {
1556 LocationSummary* locations = not_->GetLocations();
1557 Location out = locations->Out();
1558 Location in = locations->InAt(0);
1559 switch (not_->InputAt(0)->GetType()) {
1560 case Primitive::kPrimBoolean:
1561 __ eor(out.As<Register>(), in.As<Register>(), ShifterOperand(1));
1562 break;
1563
1564 case Primitive::kPrimInt:
1565 __ mvn(out.As<Register>(), ShifterOperand(in.As<Register>()));
1566 break;
1567
1568 case Primitive::kPrimLong:
Roland Levillain70566432014-10-24 16:20:17 +01001569 __ mvn(out.AsRegisterPairLow<Register>(),
1570 ShifterOperand(in.AsRegisterPairLow<Register>()));
1571 __ mvn(out.AsRegisterPairHigh<Register>(),
1572 ShifterOperand(in.AsRegisterPairHigh<Register>()));
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001573 break;
1574
1575 default:
1576 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
1577 }
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01001578}
1579
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001580void LocationsBuilderARM::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001581 LocationSummary* locations =
1582 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01001583 locations->SetInAt(0, Location::RequiresRegister());
1584 locations->SetInAt(1, Location::RequiresRegister());
1585 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001586}
1587
1588void InstructionCodeGeneratorARM::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001589 LocationSummary* locations = compare->GetLocations();
1590 switch (compare->InputAt(0)->GetType()) {
1591 case Primitive::kPrimLong: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001592 Register output = locations->Out().As<Register>();
1593 Location left = locations->InAt(0);
1594 Location right = locations->InAt(1);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001595 Label less, greater, done;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001596 __ cmp(left.AsRegisterPairHigh<Register>(),
1597 ShifterOperand(right.AsRegisterPairHigh<Register>())); // Signed compare.
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001598 __ b(&less, LT);
1599 __ b(&greater, GT);
Nicolas Geoffray8d486732014-07-16 16:23:40 +01001600 // Do LoadImmediate before any `cmp`, as LoadImmediate might affect
1601 // the status flags.
1602 __ LoadImmediate(output, 0);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001603 __ cmp(left.AsRegisterPairLow<Register>(),
1604 ShifterOperand(right.AsRegisterPairLow<Register>())); // Unsigned compare.
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001605 __ b(&done, EQ);
1606 __ b(&less, CC);
1607
1608 __ Bind(&greater);
1609 __ LoadImmediate(output, 1);
1610 __ b(&done);
1611
1612 __ Bind(&less);
1613 __ LoadImmediate(output, -1);
1614
1615 __ Bind(&done);
1616 break;
1617 }
1618 default:
1619 LOG(FATAL) << "Unimplemented compare type " << compare->InputAt(0)->GetType();
1620 }
1621}
1622
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001623void LocationsBuilderARM::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001624 LocationSummary* locations =
1625 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01001626 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
1627 locations->SetInAt(i, Location::Any());
1628 }
1629 locations->SetOut(Location::Any());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001630}
1631
1632void InstructionCodeGeneratorARM::VisitPhi(HPhi* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001633 UNUSED(instruction);
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01001634 LOG(FATAL) << "Unreachable";
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001635}
1636
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001637void LocationsBuilderARM::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001638 LocationSummary* locations =
1639 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01001640 bool is_object_type = instruction->GetFieldType() == Primitive::kPrimNot;
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01001641 locations->SetInAt(0, Location::RequiresRegister());
1642 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01001643 // Temporary registers for the write barrier.
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01001644 if (is_object_type) {
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01001645 locations->AddTemp(Location::RequiresRegister());
1646 locations->AddTemp(Location::RequiresRegister());
1647 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001648}
1649
1650void InstructionCodeGeneratorARM::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
1651 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001652 Register obj = locations->InAt(0).As<Register>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001653 uint32_t offset = instruction->GetFieldOffset().Uint32Value();
Nicolas Geoffray39468442014-09-02 15:17:15 +01001654 Primitive::Type field_type = instruction->GetFieldType();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001655
1656 switch (field_type) {
1657 case Primitive::kPrimBoolean:
1658 case Primitive::kPrimByte: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001659 Register value = locations->InAt(1).As<Register>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001660 __ StoreToOffset(kStoreByte, value, obj, offset);
1661 break;
1662 }
1663
1664 case Primitive::kPrimShort:
1665 case Primitive::kPrimChar: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001666 Register value = locations->InAt(1).As<Register>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001667 __ StoreToOffset(kStoreHalfword, value, obj, offset);
1668 break;
1669 }
1670
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001671 case Primitive::kPrimInt:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001672 case Primitive::kPrimNot: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001673 Register value = locations->InAt(1).As<Register>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001674 __ StoreToOffset(kStoreWord, value, obj, offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001675 if (field_type == Primitive::kPrimNot) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001676 Register temp = locations->GetTemp(0).As<Register>();
1677 Register card = locations->GetTemp(1).As<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001678 codegen_->MarkGCCard(temp, card, obj, value);
1679 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001680 break;
1681 }
1682
1683 case Primitive::kPrimLong: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001684 Location value = locations->InAt(1);
1685 __ StoreToOffset(kStoreWordPair, value.AsRegisterPairLow<Register>(), obj, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001686 break;
1687 }
1688
1689 case Primitive::kPrimFloat:
1690 case Primitive::kPrimDouble:
1691 LOG(FATAL) << "Unimplemented register type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07001692 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001693 case Primitive::kPrimVoid:
1694 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07001695 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001696 }
1697}
1698
1699void LocationsBuilderARM::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001700 LocationSummary* locations =
1701 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01001702 locations->SetInAt(0, Location::RequiresRegister());
1703 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001704}
1705
1706void InstructionCodeGeneratorARM::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
1707 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001708 Register obj = locations->InAt(0).As<Register>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001709 uint32_t offset = instruction->GetFieldOffset().Uint32Value();
1710
1711 switch (instruction->GetType()) {
1712 case Primitive::kPrimBoolean: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001713 Register out = locations->Out().As<Register>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001714 __ LoadFromOffset(kLoadUnsignedByte, out, obj, offset);
1715 break;
1716 }
1717
1718 case Primitive::kPrimByte: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001719 Register out = locations->Out().As<Register>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001720 __ LoadFromOffset(kLoadSignedByte, out, obj, offset);
1721 break;
1722 }
1723
1724 case Primitive::kPrimShort: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001725 Register out = locations->Out().As<Register>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001726 __ LoadFromOffset(kLoadSignedHalfword, out, obj, offset);
1727 break;
1728 }
1729
1730 case Primitive::kPrimChar: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001731 Register out = locations->Out().As<Register>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001732 __ LoadFromOffset(kLoadUnsignedHalfword, out, obj, offset);
1733 break;
1734 }
1735
1736 case Primitive::kPrimInt:
1737 case Primitive::kPrimNot: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001738 Register out = locations->Out().As<Register>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001739 __ LoadFromOffset(kLoadWord, out, obj, offset);
1740 break;
1741 }
1742
1743 case Primitive::kPrimLong: {
1744 // TODO: support volatile.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001745 Location out = locations->Out();
1746 __ LoadFromOffset(kLoadWordPair, out.AsRegisterPairLow<Register>(), obj, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001747 break;
1748 }
1749
1750 case Primitive::kPrimFloat:
1751 case Primitive::kPrimDouble:
1752 LOG(FATAL) << "Unimplemented register type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07001753 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001754 case Primitive::kPrimVoid:
1755 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07001756 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001757 }
1758}
1759
1760void LocationsBuilderARM::VisitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001761 LocationSummary* locations =
1762 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001763 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001764 if (instruction->HasUses()) {
1765 locations->SetOut(Location::SameAsFirstInput());
1766 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001767}
1768
1769void InstructionCodeGeneratorARM::VisitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01001770 SlowPathCodeARM* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathARM(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001771 codegen_->AddSlowPath(slow_path);
1772
1773 LocationSummary* locations = instruction->GetLocations();
1774 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001775
1776 if (obj.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001777 __ cmp(obj.As<Register>(), ShifterOperand(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001778 __ b(slow_path->GetEntryLabel(), EQ);
1779 } else {
1780 DCHECK(obj.IsConstant()) << obj;
1781 DCHECK_EQ(obj.GetConstant()->AsIntConstant()->GetValue(), 0);
1782 __ b(slow_path->GetEntryLabel());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001783 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001784}
1785
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001786void LocationsBuilderARM::VisitArrayGet(HArrayGet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001787 LocationSummary* locations =
1788 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01001789 locations->SetInAt(0, Location::RequiresRegister());
1790 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
1791 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001792}
1793
1794void InstructionCodeGeneratorARM::VisitArrayGet(HArrayGet* instruction) {
1795 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001796 Register obj = locations->InAt(0).As<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001797 Location index = locations->InAt(1);
1798
1799 switch (instruction->GetType()) {
1800 case Primitive::kPrimBoolean: {
1801 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001802 Register out = locations->Out().As<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001803 if (index.IsConstant()) {
1804 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
1805 __ LoadFromOffset(kLoadUnsignedByte, out, obj, offset);
1806 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001807 __ add(IP, obj, ShifterOperand(index.As<Register>()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001808 __ LoadFromOffset(kLoadUnsignedByte, out, IP, data_offset);
1809 }
1810 break;
1811 }
1812
1813 case Primitive::kPrimByte: {
1814 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int8_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001815 Register out = locations->Out().As<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001816 if (index.IsConstant()) {
1817 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
1818 __ LoadFromOffset(kLoadSignedByte, out, obj, offset);
1819 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001820 __ add(IP, obj, ShifterOperand(index.As<Register>()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001821 __ LoadFromOffset(kLoadSignedByte, out, IP, data_offset);
1822 }
1823 break;
1824 }
1825
1826 case Primitive::kPrimShort: {
1827 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int16_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001828 Register out = locations->Out().As<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001829 if (index.IsConstant()) {
1830 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
1831 __ LoadFromOffset(kLoadSignedHalfword, out, obj, offset);
1832 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001833 __ add(IP, obj, ShifterOperand(index.As<Register>(), LSL, TIMES_2));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001834 __ LoadFromOffset(kLoadSignedHalfword, out, IP, data_offset);
1835 }
1836 break;
1837 }
1838
1839 case Primitive::kPrimChar: {
1840 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001841 Register out = locations->Out().As<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001842 if (index.IsConstant()) {
1843 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
1844 __ LoadFromOffset(kLoadUnsignedHalfword, out, obj, offset);
1845 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001846 __ add(IP, obj, ShifterOperand(index.As<Register>(), LSL, TIMES_2));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001847 __ LoadFromOffset(kLoadUnsignedHalfword, out, IP, data_offset);
1848 }
1849 break;
1850 }
1851
1852 case Primitive::kPrimInt:
1853 case Primitive::kPrimNot: {
1854 DCHECK_EQ(sizeof(mirror::HeapReference<mirror::Object>), sizeof(int32_t));
1855 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001856 Register out = locations->Out().As<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001857 if (index.IsConstant()) {
1858 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
1859 __ LoadFromOffset(kLoadWord, out, obj, offset);
1860 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001861 __ add(IP, obj, ShifterOperand(index.As<Register>(), LSL, TIMES_4));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001862 __ LoadFromOffset(kLoadWord, out, IP, data_offset);
1863 }
1864 break;
1865 }
1866
1867 case Primitive::kPrimLong: {
1868 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001869 Location out = locations->Out();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001870 if (index.IsConstant()) {
1871 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001872 __ LoadFromOffset(kLoadWordPair, out.AsRegisterPairLow<Register>(), obj, offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001873 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001874 __ add(IP, obj, ShifterOperand(index.As<Register>(), LSL, TIMES_8));
1875 __ LoadFromOffset(kLoadWordPair, out.AsRegisterPairLow<Register>(), IP, data_offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001876 }
1877 break;
1878 }
1879
1880 case Primitive::kPrimFloat:
1881 case Primitive::kPrimDouble:
1882 LOG(FATAL) << "Unimplemented register type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07001883 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001884 case Primitive::kPrimVoid:
1885 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07001886 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001887 }
1888}
1889
1890void LocationsBuilderARM::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001891 Primitive::Type value_type = instruction->GetComponentType();
1892 bool is_object = value_type == Primitive::kPrimNot;
1893 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
1894 instruction, is_object ? LocationSummary::kCall : LocationSummary::kNoCall);
1895 if (is_object) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001896 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001897 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1898 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
1899 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001900 } else {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01001901 locations->SetInAt(0, Location::RequiresRegister());
1902 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
1903 locations->SetInAt(2, Location::RequiresRegister());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001904 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001905}
1906
1907void InstructionCodeGeneratorARM::VisitArraySet(HArraySet* instruction) {
1908 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001909 Register obj = locations->InAt(0).As<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001910 Location index = locations->InAt(1);
Nicolas Geoffray39468442014-09-02 15:17:15 +01001911 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001912
1913 switch (value_type) {
1914 case Primitive::kPrimBoolean:
1915 case Primitive::kPrimByte: {
1916 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001917 Register value = locations->InAt(2).As<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001918 if (index.IsConstant()) {
1919 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
1920 __ StoreToOffset(kStoreByte, value, obj, offset);
1921 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001922 __ add(IP, obj, ShifterOperand(index.As<Register>()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001923 __ StoreToOffset(kStoreByte, value, IP, data_offset);
1924 }
1925 break;
1926 }
1927
1928 case Primitive::kPrimShort:
1929 case Primitive::kPrimChar: {
1930 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001931 Register value = locations->InAt(2).As<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001932 if (index.IsConstant()) {
1933 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
1934 __ StoreToOffset(kStoreHalfword, value, obj, offset);
1935 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001936 __ add(IP, obj, ShifterOperand(index.As<Register>(), LSL, TIMES_2));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001937 __ StoreToOffset(kStoreHalfword, value, IP, data_offset);
1938 }
1939 break;
1940 }
1941
1942 case Primitive::kPrimInt: {
1943 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001944 Register value = locations->InAt(2).As<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001945 if (index.IsConstant()) {
1946 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
1947 __ StoreToOffset(kStoreWord, value, obj, offset);
1948 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001949 __ add(IP, obj, ShifterOperand(index.As<Register>(), LSL, TIMES_4));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001950 __ StoreToOffset(kStoreWord, value, IP, data_offset);
1951 }
1952 break;
1953 }
1954
1955 case Primitive::kPrimNot: {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001956 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pAputObject), instruction, instruction->GetDexPc());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001957 break;
1958 }
1959
1960 case Primitive::kPrimLong: {
1961 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001962 Location value = locations->InAt(2);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001963 if (index.IsConstant()) {
1964 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001965 __ StoreToOffset(kStoreWordPair, value.AsRegisterPairLow<Register>(), obj, offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001966 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001967 __ add(IP, obj, ShifterOperand(index.As<Register>(), LSL, TIMES_8));
1968 __ StoreToOffset(kStoreWordPair, value.AsRegisterPairLow<Register>(), IP, data_offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001969 }
1970 break;
1971 }
1972
1973 case Primitive::kPrimFloat:
1974 case Primitive::kPrimDouble:
1975 LOG(FATAL) << "Unimplemented register type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07001976 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001977 case Primitive::kPrimVoid:
1978 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07001979 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001980 }
1981}
1982
1983void LocationsBuilderARM::VisitArrayLength(HArrayLength* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001984 LocationSummary* locations =
1985 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01001986 locations->SetInAt(0, Location::RequiresRegister());
1987 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001988}
1989
1990void InstructionCodeGeneratorARM::VisitArrayLength(HArrayLength* instruction) {
1991 LocationSummary* locations = instruction->GetLocations();
1992 uint32_t offset = mirror::Array::LengthOffset().Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001993 Register obj = locations->InAt(0).As<Register>();
1994 Register out = locations->Out().As<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001995 __ LoadFromOffset(kLoadWord, out, obj, offset);
1996}
1997
1998void LocationsBuilderARM::VisitBoundsCheck(HBoundsCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001999 LocationSummary* locations =
2000 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002001 locations->SetInAt(0, Location::RequiresRegister());
2002 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002003 if (instruction->HasUses()) {
2004 locations->SetOut(Location::SameAsFirstInput());
2005 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002006}
2007
2008void InstructionCodeGeneratorARM::VisitBoundsCheck(HBoundsCheck* instruction) {
2009 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01002010 SlowPathCodeARM* slow_path = new (GetGraph()->GetArena()) BoundsCheckSlowPathARM(
Nicolas Geoffray39468442014-09-02 15:17:15 +01002011 instruction, locations->InAt(0), locations->InAt(1));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002012 codegen_->AddSlowPath(slow_path);
2013
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002014 Register index = locations->InAt(0).As<Register>();
2015 Register length = locations->InAt(1).As<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002016
2017 __ cmp(index, ShifterOperand(length));
2018 __ b(slow_path->GetEntryLabel(), CS);
2019}
2020
2021void CodeGeneratorARM::MarkGCCard(Register temp, Register card, Register object, Register value) {
2022 Label is_null;
2023 __ CompareAndBranchIfZero(value, &is_null);
2024 __ LoadFromOffset(kLoadWord, card, TR, Thread::CardTableOffset<kArmWordSize>().Int32Value());
2025 __ Lsr(temp, object, gc::accounting::CardTable::kCardShift);
2026 __ strb(card, Address(card, temp));
2027 __ Bind(&is_null);
2028}
2029
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002030void LocationsBuilderARM::VisitTemporary(HTemporary* temp) {
2031 temp->SetLocations(nullptr);
2032}
2033
2034void InstructionCodeGeneratorARM::VisitTemporary(HTemporary* temp) {
2035 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002036 UNUSED(temp);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002037}
2038
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01002039void LocationsBuilderARM::VisitParallelMove(HParallelMove* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002040 UNUSED(instruction);
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01002041 LOG(FATAL) << "Unreachable";
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01002042}
2043
2044void InstructionCodeGeneratorARM::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01002045 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
2046}
2047
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00002048void LocationsBuilderARM::VisitSuspendCheck(HSuspendCheck* instruction) {
2049 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
2050}
2051
2052void InstructionCodeGeneratorARM::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01002053 HBasicBlock* block = instruction->GetBlock();
2054 if (block->GetLoopInformation() != nullptr) {
2055 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
2056 // The back edge will generate the suspend check.
2057 return;
2058 }
2059 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
2060 // The goto will generate the suspend check.
2061 return;
2062 }
2063 GenerateSuspendCheck(instruction, nullptr);
2064}
2065
2066void InstructionCodeGeneratorARM::GenerateSuspendCheck(HSuspendCheck* instruction,
2067 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00002068 SuspendCheckSlowPathARM* slow_path =
Nicolas Geoffray3c049742014-09-24 18:10:46 +01002069 new (GetGraph()->GetArena()) SuspendCheckSlowPathARM(instruction, successor);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00002070 codegen_->AddSlowPath(slow_path);
2071
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01002072 __ subs(R4, R4, ShifterOperand(1));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01002073 if (successor == nullptr) {
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01002074 __ b(slow_path->GetEntryLabel(), EQ);
Nicolas Geoffray3c049742014-09-24 18:10:46 +01002075 __ Bind(slow_path->GetReturnLabel());
2076 } else {
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01002077 __ b(codegen_->GetLabelOf(successor), NE);
Nicolas Geoffray3c049742014-09-24 18:10:46 +01002078 __ b(slow_path->GetEntryLabel());
2079 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00002080}
2081
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01002082ArmAssembler* ParallelMoveResolverARM::GetAssembler() const {
2083 return codegen_->GetAssembler();
2084}
2085
2086void ParallelMoveResolverARM::EmitMove(size_t index) {
2087 MoveOperands* move = moves_.Get(index);
2088 Location source = move->GetSource();
2089 Location destination = move->GetDestination();
2090
2091 if (source.IsRegister()) {
2092 if (destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002093 __ Mov(destination.As<Register>(), source.As<Register>());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01002094 } else {
2095 DCHECK(destination.IsStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002096 __ StoreToOffset(kStoreWord, source.As<Register>(),
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01002097 SP, destination.GetStackIndex());
2098 }
2099 } else if (source.IsStackSlot()) {
2100 if (destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002101 __ LoadFromOffset(kLoadWord, destination.As<Register>(),
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01002102 SP, source.GetStackIndex());
2103 } else {
2104 DCHECK(destination.IsStackSlot());
2105 __ LoadFromOffset(kLoadWord, IP, SP, source.GetStackIndex());
2106 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
2107 }
2108 } else {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002109 DCHECK(source.IsConstant());
Roland Levillain476df552014-10-09 17:51:36 +01002110 DCHECK(source.GetConstant()->IsIntConstant());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002111 int32_t value = source.GetConstant()->AsIntConstant()->GetValue();
2112 if (destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002113 __ LoadImmediate(destination.As<Register>(), value);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002114 } else {
2115 DCHECK(destination.IsStackSlot());
2116 __ LoadImmediate(IP, value);
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002117 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002118 }
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01002119 }
2120}
2121
2122void ParallelMoveResolverARM::Exchange(Register reg, int mem) {
2123 __ Mov(IP, reg);
2124 __ LoadFromOffset(kLoadWord, reg, SP, mem);
2125 __ StoreToOffset(kStoreWord, IP, SP, mem);
2126}
2127
2128void ParallelMoveResolverARM::Exchange(int mem1, int mem2) {
2129 ScratchRegisterScope ensure_scratch(this, IP, R0, codegen_->GetNumberOfCoreRegisters());
2130 int stack_offset = ensure_scratch.IsSpilled() ? kArmWordSize : 0;
2131 __ LoadFromOffset(kLoadWord, static_cast<Register>(ensure_scratch.GetRegister()),
2132 SP, mem1 + stack_offset);
2133 __ LoadFromOffset(kLoadWord, IP, SP, mem2 + stack_offset);
2134 __ StoreToOffset(kStoreWord, static_cast<Register>(ensure_scratch.GetRegister()),
2135 SP, mem2 + stack_offset);
2136 __ StoreToOffset(kStoreWord, IP, SP, mem1 + stack_offset);
2137}
2138
2139void ParallelMoveResolverARM::EmitSwap(size_t index) {
2140 MoveOperands* move = moves_.Get(index);
2141 Location source = move->GetSource();
2142 Location destination = move->GetDestination();
2143
2144 if (source.IsRegister() && destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002145 DCHECK_NE(source.As<Register>(), IP);
2146 DCHECK_NE(destination.As<Register>(), IP);
2147 __ Mov(IP, source.As<Register>());
2148 __ Mov(source.As<Register>(), destination.As<Register>());
2149 __ Mov(destination.As<Register>(), IP);
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01002150 } else if (source.IsRegister() && destination.IsStackSlot()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002151 Exchange(source.As<Register>(), destination.GetStackIndex());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01002152 } else if (source.IsStackSlot() && destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002153 Exchange(destination.As<Register>(), source.GetStackIndex());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01002154 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
2155 Exchange(source.GetStackIndex(), destination.GetStackIndex());
2156 } else {
2157 LOG(FATAL) << "Unimplemented";
2158 }
2159}
2160
2161void ParallelMoveResolverARM::SpillScratch(int reg) {
2162 __ Push(static_cast<Register>(reg));
2163}
2164
2165void ParallelMoveResolverARM::RestoreScratch(int reg) {
2166 __ Pop(static_cast<Register>(reg));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01002167}
2168
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002169void LocationsBuilderARM::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002170 LocationSummary::CallKind call_kind = cls->CanCallRuntime()
2171 ? LocationSummary::kCallOnSlowPath
2172 : LocationSummary::kNoCall;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002173 LocationSummary* locations =
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002174 new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002175 locations->SetOut(Location::RequiresRegister());
2176}
2177
2178void InstructionCodeGeneratorARM::VisitLoadClass(HLoadClass* cls) {
2179 Register out = cls->GetLocations()->Out().As<Register>();
2180 if (cls->IsReferrersClass()) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002181 DCHECK(!cls->CanCallRuntime());
2182 DCHECK(!cls->MustGenerateClinitCheck());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002183 codegen_->LoadCurrentMethod(out);
2184 __ LoadFromOffset(kLoadWord, out, out, mirror::ArtMethod::DeclaringClassOffset().Int32Value());
2185 } else {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002186 DCHECK(cls->CanCallRuntime());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002187 codegen_->LoadCurrentMethod(out);
2188 __ LoadFromOffset(
2189 kLoadWord, out, out, mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value());
2190 __ LoadFromOffset(kLoadWord, out, out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex()));
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002191
2192 SlowPathCodeARM* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM(
2193 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
2194 codegen_->AddSlowPath(slow_path);
2195 __ cmp(out, ShifterOperand(0));
2196 __ b(slow_path->GetEntryLabel(), EQ);
2197 if (cls->MustGenerateClinitCheck()) {
2198 GenerateClassInitializationCheck(slow_path, out);
2199 } else {
2200 __ Bind(slow_path->GetExitLabel());
2201 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002202 }
2203}
2204
2205void LocationsBuilderARM::VisitClinitCheck(HClinitCheck* check) {
2206 LocationSummary* locations =
2207 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
2208 locations->SetInAt(0, Location::RequiresRegister());
2209 if (check->HasUses()) {
2210 locations->SetOut(Location::SameAsFirstInput());
2211 }
2212}
2213
2214void InstructionCodeGeneratorARM::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002215 // We assume the class is not null.
2216 SlowPathCodeARM* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM(
2217 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002218 codegen_->AddSlowPath(slow_path);
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002219 GenerateClassInitializationCheck(slow_path, check->GetLocations()->InAt(0).As<Register>());
2220}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002221
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002222void InstructionCodeGeneratorARM::GenerateClassInitializationCheck(
2223 SlowPathCodeARM* slow_path, Register class_reg) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002224 __ LoadFromOffset(kLoadWord, IP, class_reg, mirror::Class::StatusOffset().Int32Value());
2225 __ cmp(IP, ShifterOperand(mirror::Class::kStatusInitialized));
2226 __ b(slow_path->GetEntryLabel(), LT);
2227 // Even if the initialized flag is set, we may be in a situation where caches are not synced
2228 // properly. Therefore, we do a memory fence.
2229 __ dmb(ISH);
2230 __ Bind(slow_path->GetExitLabel());
2231}
2232
2233void LocationsBuilderARM::VisitStaticFieldGet(HStaticFieldGet* instruction) {
2234 LocationSummary* locations =
2235 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2236 locations->SetInAt(0, Location::RequiresRegister());
2237 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2238}
2239
2240void InstructionCodeGeneratorARM::VisitStaticFieldGet(HStaticFieldGet* instruction) {
2241 LocationSummary* locations = instruction->GetLocations();
2242 Register cls = locations->InAt(0).As<Register>();
2243 uint32_t offset = instruction->GetFieldOffset().Uint32Value();
2244
2245 switch (instruction->GetType()) {
2246 case Primitive::kPrimBoolean: {
2247 Register out = locations->Out().As<Register>();
2248 __ LoadFromOffset(kLoadUnsignedByte, out, cls, offset);
2249 break;
2250 }
2251
2252 case Primitive::kPrimByte: {
2253 Register out = locations->Out().As<Register>();
2254 __ LoadFromOffset(kLoadSignedByte, out, cls, offset);
2255 break;
2256 }
2257
2258 case Primitive::kPrimShort: {
2259 Register out = locations->Out().As<Register>();
2260 __ LoadFromOffset(kLoadSignedHalfword, out, cls, offset);
2261 break;
2262 }
2263
2264 case Primitive::kPrimChar: {
2265 Register out = locations->Out().As<Register>();
2266 __ LoadFromOffset(kLoadUnsignedHalfword, out, cls, offset);
2267 break;
2268 }
2269
2270 case Primitive::kPrimInt:
2271 case Primitive::kPrimNot: {
2272 Register out = locations->Out().As<Register>();
2273 __ LoadFromOffset(kLoadWord, out, cls, offset);
2274 break;
2275 }
2276
2277 case Primitive::kPrimLong: {
2278 // TODO: support volatile.
2279 Location out = locations->Out();
2280 __ LoadFromOffset(kLoadWordPair, out.AsRegisterPairLow<Register>(), cls, offset);
2281 break;
2282 }
2283
2284 case Primitive::kPrimFloat:
2285 case Primitive::kPrimDouble:
2286 LOG(FATAL) << "Unimplemented register type " << instruction->GetType();
2287 UNREACHABLE();
2288 case Primitive::kPrimVoid:
2289 LOG(FATAL) << "Unreachable type " << instruction->GetType();
2290 UNREACHABLE();
2291 }
2292}
2293
2294void LocationsBuilderARM::VisitStaticFieldSet(HStaticFieldSet* instruction) {
2295 LocationSummary* locations =
2296 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2297 bool is_object_type = instruction->GetFieldType() == Primitive::kPrimNot;
2298 locations->SetInAt(0, Location::RequiresRegister());
2299 locations->SetInAt(1, Location::RequiresRegister());
2300 // Temporary registers for the write barrier.
2301 if (is_object_type) {
2302 locations->AddTemp(Location::RequiresRegister());
2303 locations->AddTemp(Location::RequiresRegister());
2304 }
2305}
2306
2307void InstructionCodeGeneratorARM::VisitStaticFieldSet(HStaticFieldSet* instruction) {
2308 LocationSummary* locations = instruction->GetLocations();
2309 Register cls = locations->InAt(0).As<Register>();
2310 uint32_t offset = instruction->GetFieldOffset().Uint32Value();
2311 Primitive::Type field_type = instruction->GetFieldType();
2312
2313 switch (field_type) {
2314 case Primitive::kPrimBoolean:
2315 case Primitive::kPrimByte: {
2316 Register value = locations->InAt(1).As<Register>();
2317 __ StoreToOffset(kStoreByte, value, cls, offset);
2318 break;
2319 }
2320
2321 case Primitive::kPrimShort:
2322 case Primitive::kPrimChar: {
2323 Register value = locations->InAt(1).As<Register>();
2324 __ StoreToOffset(kStoreHalfword, value, cls, offset);
2325 break;
2326 }
2327
2328 case Primitive::kPrimInt:
2329 case Primitive::kPrimNot: {
2330 Register value = locations->InAt(1).As<Register>();
2331 __ StoreToOffset(kStoreWord, value, cls, offset);
2332 if (field_type == Primitive::kPrimNot) {
2333 Register temp = locations->GetTemp(0).As<Register>();
2334 Register card = locations->GetTemp(1).As<Register>();
2335 codegen_->MarkGCCard(temp, card, cls, value);
2336 }
2337 break;
2338 }
2339
2340 case Primitive::kPrimLong: {
2341 Location value = locations->InAt(1);
2342 __ StoreToOffset(kStoreWordPair, value.AsRegisterPairLow<Register>(), cls, offset);
2343 break;
2344 }
2345
2346 case Primitive::kPrimFloat:
2347 case Primitive::kPrimDouble:
2348 LOG(FATAL) << "Unimplemented register type " << field_type;
2349 UNREACHABLE();
2350 case Primitive::kPrimVoid:
2351 LOG(FATAL) << "Unreachable type " << field_type;
2352 UNREACHABLE();
2353 }
2354}
2355
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00002356void LocationsBuilderARM::VisitLoadString(HLoadString* load) {
2357 LocationSummary* locations =
2358 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kCallOnSlowPath);
2359 locations->SetOut(Location::RequiresRegister());
2360}
2361
2362void InstructionCodeGeneratorARM::VisitLoadString(HLoadString* load) {
2363 SlowPathCodeARM* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathARM(load);
2364 codegen_->AddSlowPath(slow_path);
2365
2366 Register out = load->GetLocations()->Out().As<Register>();
2367 codegen_->LoadCurrentMethod(out);
2368 __ LoadFromOffset(
2369 kLoadWord, out, out, mirror::ArtMethod::DexCacheStringsOffset().Int32Value());
2370 __ LoadFromOffset(kLoadWord, out, out, CodeGenerator::GetCacheOffset(load->GetStringIndex()));
2371 __ cmp(out, ShifterOperand(0));
2372 __ b(slow_path->GetEntryLabel(), EQ);
2373 __ Bind(slow_path->GetExitLabel());
2374}
2375
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002376} // namespace arm
2377} // namespace art