blob: 9d17fb137a3f4077c40aca5e396d0ae147441fb8 [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 Geoffray8ccc3f52014-03-19 10:34:11 +000020#include "mirror/array.h"
21#include "mirror/art_method.h"
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070022#include "thread.h"
Nicolas Geoffray9cf35522014-06-09 18:40:10 +010023#include "utils/assembler.h"
24#include "utils/arm/assembler_arm.h"
25#include "utils/arm/managed_register_arm.h"
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +000026
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000027namespace art {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +010028
29arm::ArmManagedRegister Location::AsArm() const {
30 return reg().AsArm();
31}
32
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000033namespace arm {
34
Nicolas Geoffraye5038322014-07-04 09:41:32 +010035#define __ reinterpret_cast<ArmAssembler*>(codegen->GetAssembler())->
36
37class NullCheckSlowPathARM : public SlowPathCode {
38 public:
39 explicit NullCheckSlowPathARM(uint32_t dex_pc) : dex_pc_(dex_pc) {}
40
41 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
42 __ Bind(GetEntryLabel());
43 int32_t offset = QUICK_ENTRYPOINT_OFFSET(kArmWordSize, pThrowNullPointer).Int32Value();
44 __ ldr(LR, Address(TR, offset));
45 __ blx(LR);
46 codegen->RecordPcInfo(dex_pc_);
47 }
48
49 private:
50 const uint32_t dex_pc_;
51 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathARM);
52};
53
54#undef __
55#define __ reinterpret_cast<ArmAssembler*>(GetAssembler())->
Dave Allison20dfc792014-06-16 20:44:29 -070056
57inline Condition ARMCondition(IfCondition cond) {
58 switch (cond) {
59 case kCondEQ: return EQ;
60 case kCondNE: return NE;
61 case kCondLT: return LT;
62 case kCondLE: return LE;
63 case kCondGT: return GT;
64 case kCondGE: return GE;
65 default:
66 LOG(FATAL) << "Unknown if condition";
67 }
68 return EQ; // Unreachable.
69}
70
71inline Condition ARMOppositeCondition(IfCondition cond) {
72 switch (cond) {
73 case kCondEQ: return NE;
74 case kCondNE: return EQ;
75 case kCondLT: return GE;
76 case kCondLE: return GT;
77 case kCondGT: return LE;
78 case kCondGE: return LT;
79 default:
80 LOG(FATAL) << "Unknown if condition";
81 }
82 return EQ; // Unreachable.
83}
84
Nicolas Geoffraye5038322014-07-04 09:41:32 +010085static constexpr int kNumberOfPushedRegistersAtEntry = 1 + 2; // LR, R6, R7
Nicolas Geoffray4a34a422014-04-03 10:38:37 +010086static constexpr int kCurrentMethodStackOffset = 0;
87
Nicolas Geoffraya7062e02014-05-22 12:50:17 +010088void CodeGeneratorARM::DumpCoreRegister(std::ostream& stream, int reg) const {
89 stream << ArmManagedRegister::FromCoreRegister(Register(reg));
90}
91
92void CodeGeneratorARM::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
93 stream << ArmManagedRegister::FromDRegister(DRegister(reg));
94}
95
Nicolas Geoffraya7aca372014-04-28 17:47:12 +010096CodeGeneratorARM::CodeGeneratorARM(HGraph* graph)
97 : CodeGenerator(graph, kNumberOfRegIds),
98 location_builder_(graph, this),
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +010099 instruction_visitor_(graph, this),
Nicolas Geoffray8d486732014-07-16 16:23:40 +0100100 move_resolver_(graph->GetArena(), this),
101 assembler_(true) {}
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100102
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100103size_t CodeGeneratorARM::FrameEntrySpillSize() const {
104 return kNumberOfPushedRegistersAtEntry * kArmWordSize;
105}
106
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100107static bool* GetBlockedRegisterPairs(bool* blocked_registers) {
108 return blocked_registers + kNumberOfAllocIds;
109}
110
111ManagedRegister CodeGeneratorARM::AllocateFreeRegister(Primitive::Type type,
112 bool* blocked_registers) const {
113 switch (type) {
114 case Primitive::kPrimLong: {
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100115 bool* blocked_register_pairs = GetBlockedRegisterPairs(blocked_registers);
116 size_t reg = AllocateFreeRegisterInternal(blocked_register_pairs, kNumberOfRegisterPairs);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100117 ArmManagedRegister pair =
118 ArmManagedRegister::FromRegisterPair(static_cast<RegisterPair>(reg));
119 blocked_registers[pair.AsRegisterPairLow()] = true;
120 blocked_registers[pair.AsRegisterPairHigh()] = true;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100121 // Block all other register pairs that share a register with `pair`.
122 for (int i = 0; i < kNumberOfRegisterPairs; i++) {
123 ArmManagedRegister current =
124 ArmManagedRegister::FromRegisterPair(static_cast<RegisterPair>(i));
125 if (current.AsRegisterPairLow() == pair.AsRegisterPairLow()
126 || current.AsRegisterPairLow() == pair.AsRegisterPairHigh()
127 || current.AsRegisterPairHigh() == pair.AsRegisterPairLow()
128 || current.AsRegisterPairHigh() == pair.AsRegisterPairHigh()) {
129 blocked_register_pairs[i] = true;
130 }
131 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100132 return pair;
133 }
134
135 case Primitive::kPrimByte:
136 case Primitive::kPrimBoolean:
137 case Primitive::kPrimChar:
138 case Primitive::kPrimShort:
139 case Primitive::kPrimInt:
140 case Primitive::kPrimNot: {
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100141 int reg = AllocateFreeRegisterInternal(blocked_registers, kNumberOfCoreRegisters);
142 // Block all register pairs that contain `reg`.
143 bool* blocked_register_pairs = GetBlockedRegisterPairs(blocked_registers);
144 for (int i = 0; i < kNumberOfRegisterPairs; i++) {
145 ArmManagedRegister current =
146 ArmManagedRegister::FromRegisterPair(static_cast<RegisterPair>(i));
147 if (current.AsRegisterPairLow() == reg || current.AsRegisterPairHigh() == reg) {
148 blocked_register_pairs[i] = true;
149 }
150 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100151 return ArmManagedRegister::FromCoreRegister(static_cast<Register>(reg));
152 }
153
154 case Primitive::kPrimFloat:
155 case Primitive::kPrimDouble:
156 LOG(FATAL) << "Unimplemented register type " << type;
157
158 case Primitive::kPrimVoid:
159 LOG(FATAL) << "Unreachable type " << type;
160 }
161
162 return ManagedRegister::NoRegister();
163}
164
165void CodeGeneratorARM::SetupBlockedRegisters(bool* blocked_registers) const {
166 bool* blocked_register_pairs = GetBlockedRegisterPairs(blocked_registers);
167
168 // Don't allocate the dalvik style register pair passing.
169 blocked_register_pairs[R1_R2] = true;
170
171 // Stack register, LR and PC are always reserved.
172 blocked_registers[SP] = true;
173 blocked_registers[LR] = true;
174 blocked_registers[PC] = true;
175
176 // Reserve R4 for suspend check.
177 blocked_registers[R4] = true;
178 blocked_register_pairs[R4_R5] = true;
179
180 // Reserve thread register.
181 blocked_registers[TR] = true;
182
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +0100183 // Reserve temp register.
184 blocked_registers[IP] = true;
185
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100186 // TODO: We currently don't use Quick's callee saved registers.
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100187 // We always save and restore R6 and R7 to make sure we can use three
188 // register pairs for long operations.
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100189 blocked_registers[R5] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100190 blocked_registers[R8] = true;
191 blocked_registers[R10] = true;
192 blocked_registers[R11] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100193}
194
195size_t CodeGeneratorARM::GetNumberOfRegisters() const {
196 return kNumberOfRegIds;
197}
198
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100199static Location ArmCoreLocation(Register reg) {
200 return Location::RegisterLocation(ArmManagedRegister::FromCoreRegister(reg));
201}
202
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100203InstructionCodeGeneratorARM::InstructionCodeGeneratorARM(HGraph* graph, CodeGeneratorARM* codegen)
204 : HGraphVisitor(graph),
205 assembler_(codegen->GetAssembler()),
206 codegen_(codegen) {}
207
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000208void CodeGeneratorARM::GenerateFrameEntry() {
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100209 core_spill_mask_ |= (1 << LR | 1 << R6 | 1 << R7);
210 __ PushList(1 << LR | 1 << R6 | 1 << R7);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000211
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100212 // The return PC has already been pushed on the stack.
Nicolas Geoffray707c8092014-04-04 10:50:14 +0100213 __ AddConstant(SP, -(GetFrameSize() - kNumberOfPushedRegistersAtEntry * kArmWordSize));
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000214 __ str(R0, Address(SP, 0));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000215}
216
217void CodeGeneratorARM::GenerateFrameExit() {
Nicolas Geoffray707c8092014-04-04 10:50:14 +0100218 __ AddConstant(SP, GetFrameSize() - kNumberOfPushedRegistersAtEntry * kArmWordSize);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100219 __ PopList(1 << PC | 1 << R6 | 1 << R7);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000220}
221
222void CodeGeneratorARM::Bind(Label* label) {
223 __ Bind(label);
224}
225
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100226Location CodeGeneratorARM::GetStackLocation(HLoadLocal* load) const {
227 switch (load->GetType()) {
228 case Primitive::kPrimLong:
229 return Location::DoubleStackSlot(GetStackSlot(load->GetLocal()));
230 break;
231
232 case Primitive::kPrimInt:
233 case Primitive::kPrimNot:
234 return Location::StackSlot(GetStackSlot(load->GetLocal()));
235
236 case Primitive::kPrimFloat:
237 case Primitive::kPrimDouble:
238 LOG(FATAL) << "Unimplemented type " << load->GetType();
239
240 case Primitive::kPrimBoolean:
241 case Primitive::kPrimByte:
242 case Primitive::kPrimChar:
243 case Primitive::kPrimShort:
244 case Primitive::kPrimVoid:
245 LOG(FATAL) << "Unexpected type " << load->GetType();
246 }
247
248 LOG(FATAL) << "Unreachable";
249 return Location();
250}
251
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100252Location InvokeDexCallingConventionVisitor::GetNextLocation(Primitive::Type type) {
253 switch (type) {
254 case Primitive::kPrimBoolean:
255 case Primitive::kPrimByte:
256 case Primitive::kPrimChar:
257 case Primitive::kPrimShort:
258 case Primitive::kPrimInt:
259 case Primitive::kPrimNot: {
260 uint32_t index = gp_index_++;
261 if (index < calling_convention.GetNumberOfRegisters()) {
262 return ArmCoreLocation(calling_convention.GetRegisterAt(index));
263 } else {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100264 return Location::StackSlot(calling_convention.GetStackOffsetOf(index));
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100265 }
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100266 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100267
268 case Primitive::kPrimLong: {
269 uint32_t index = gp_index_;
270 gp_index_ += 2;
271 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
272 return Location::RegisterLocation(ArmManagedRegister::FromRegisterPair(
273 calling_convention.GetRegisterPairAt(index)));
274 } else if (index + 1 == calling_convention.GetNumberOfRegisters()) {
275 return Location::QuickParameter(index);
276 } else {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100277 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(index));
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100278 }
279 }
280
281 case Primitive::kPrimDouble:
282 case Primitive::kPrimFloat:
283 LOG(FATAL) << "Unimplemented parameter type " << type;
284 break;
285
286 case Primitive::kPrimVoid:
287 LOG(FATAL) << "Unexpected parameter type " << type;
288 break;
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100289 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100290 return Location();
291}
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100292
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100293void CodeGeneratorARM::Move32(Location destination, Location source) {
294 if (source.Equals(destination)) {
295 return;
296 }
297 if (destination.IsRegister()) {
298 if (source.IsRegister()) {
299 __ Mov(destination.AsArm().AsCoreRegister(), source.AsArm().AsCoreRegister());
300 } else {
301 __ ldr(destination.AsArm().AsCoreRegister(), Address(SP, source.GetStackIndex()));
302 }
303 } else {
304 DCHECK(destination.IsStackSlot());
305 if (source.IsRegister()) {
306 __ str(source.AsArm().AsCoreRegister(), Address(SP, destination.GetStackIndex()));
307 } else {
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +0100308 __ ldr(IP, Address(SP, source.GetStackIndex()));
309 __ str(IP, Address(SP, destination.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100310 }
311 }
312}
313
314void CodeGeneratorARM::Move64(Location destination, Location source) {
315 if (source.Equals(destination)) {
316 return;
317 }
318 if (destination.IsRegister()) {
319 if (source.IsRegister()) {
320 __ Mov(destination.AsArm().AsRegisterPairLow(), source.AsArm().AsRegisterPairLow());
321 __ Mov(destination.AsArm().AsRegisterPairHigh(), source.AsArm().AsRegisterPairHigh());
322 } else if (source.IsQuickParameter()) {
323 uint32_t argument_index = source.GetQuickParameterIndex();
324 InvokeDexCallingConvention calling_convention;
325 __ Mov(destination.AsArm().AsRegisterPairLow(),
326 calling_convention.GetRegisterAt(argument_index));
327 __ ldr(destination.AsArm().AsRegisterPairHigh(),
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100328 Address(SP, calling_convention.GetStackOffsetOf(argument_index + 1) + GetFrameSize()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100329 } else {
330 DCHECK(source.IsDoubleStackSlot());
331 if (destination.AsArm().AsRegisterPair() == R1_R2) {
332 __ ldr(R1, Address(SP, source.GetStackIndex()));
333 __ ldr(R2, Address(SP, source.GetHighStackIndex(kArmWordSize)));
334 } else {
335 __ LoadFromOffset(kLoadWordPair, destination.AsArm().AsRegisterPairLow(),
336 SP, source.GetStackIndex());
337 }
338 }
339 } else if (destination.IsQuickParameter()) {
340 InvokeDexCallingConvention calling_convention;
341 uint32_t argument_index = destination.GetQuickParameterIndex();
342 if (source.IsRegister()) {
343 __ Mov(calling_convention.GetRegisterAt(argument_index), source.AsArm().AsRegisterPairLow());
344 __ str(source.AsArm().AsRegisterPairHigh(),
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100345 Address(SP, calling_convention.GetStackOffsetOf(argument_index + 1)));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100346 } else {
347 DCHECK(source.IsDoubleStackSlot());
348 __ ldr(calling_convention.GetRegisterAt(argument_index), Address(SP, source.GetStackIndex()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100349 __ ldr(R0, Address(SP, source.GetHighStackIndex(kArmWordSize)));
350 __ str(R0, Address(SP, calling_convention.GetStackOffsetOf(argument_index + 1)));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100351 }
352 } else {
353 DCHECK(destination.IsDoubleStackSlot());
354 if (source.IsRegister()) {
355 if (source.AsArm().AsRegisterPair() == R1_R2) {
356 __ str(R1, Address(SP, destination.GetStackIndex()));
357 __ str(R2, Address(SP, destination.GetHighStackIndex(kArmWordSize)));
358 } else {
359 __ StoreToOffset(kStoreWordPair, source.AsArm().AsRegisterPairLow(),
360 SP, destination.GetStackIndex());
361 }
362 } else if (source.IsQuickParameter()) {
363 InvokeDexCallingConvention calling_convention;
364 uint32_t argument_index = source.GetQuickParameterIndex();
365 __ str(calling_convention.GetRegisterAt(argument_index),
366 Address(SP, destination.GetStackIndex()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100367 __ ldr(R0,
368 Address(SP, calling_convention.GetStackOffsetOf(argument_index + 1) + GetFrameSize()));
369 __ str(R0, Address(SP, destination.GetHighStackIndex(kArmWordSize)));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100370 } else {
371 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +0100372 __ ldr(IP, Address(SP, source.GetStackIndex()));
373 __ str(IP, Address(SP, destination.GetStackIndex()));
374 __ ldr(IP, Address(SP, source.GetHighStackIndex(kArmWordSize)));
375 __ str(IP, Address(SP, destination.GetHighStackIndex(kArmWordSize)));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100376 }
377 }
378}
379
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100380void CodeGeneratorARM::Move(HInstruction* instruction, Location location, HInstruction* move_for) {
381 if (instruction->AsIntConstant() != nullptr) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100382 int32_t value = instruction->AsIntConstant()->GetValue();
383 if (location.IsRegister()) {
384 __ LoadImmediate(location.AsArm().AsCoreRegister(), value);
385 } else {
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +0100386 __ LoadImmediate(IP, value);
387 __ str(IP, Address(SP, location.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100388 }
389 } else if (instruction->AsLongConstant() != nullptr) {
390 int64_t value = instruction->AsLongConstant()->GetValue();
391 if (location.IsRegister()) {
392 __ LoadImmediate(location.AsArm().AsRegisterPairLow(), Low32Bits(value));
393 __ LoadImmediate(location.AsArm().AsRegisterPairHigh(), High32Bits(value));
394 } else {
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +0100395 __ LoadImmediate(IP, Low32Bits(value));
396 __ str(IP, Address(SP, location.GetStackIndex()));
397 __ LoadImmediate(IP, High32Bits(value));
398 __ str(IP, Address(SP, location.GetHighStackIndex(kArmWordSize)));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100399 }
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100400 } else if (instruction->AsLoadLocal() != nullptr) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100401 uint32_t stack_slot = GetStackSlot(instruction->AsLoadLocal()->GetLocal());
402 switch (instruction->GetType()) {
403 case Primitive::kPrimBoolean:
404 case Primitive::kPrimByte:
405 case Primitive::kPrimChar:
406 case Primitive::kPrimShort:
407 case Primitive::kPrimInt:
408 case Primitive::kPrimNot:
409 Move32(location, Location::StackSlot(stack_slot));
410 break;
411
412 case Primitive::kPrimLong:
413 Move64(location, Location::DoubleStackSlot(stack_slot));
414 break;
415
416 default:
417 LOG(FATAL) << "Unimplemented type " << instruction->GetType();
418 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000419 } else {
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100420 DCHECK((instruction->GetNext() == move_for) || instruction->GetNext()->IsTemporary());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100421 switch (instruction->GetType()) {
422 case Primitive::kPrimBoolean:
423 case Primitive::kPrimByte:
424 case Primitive::kPrimChar:
425 case Primitive::kPrimShort:
426 case Primitive::kPrimNot:
427 case Primitive::kPrimInt:
428 Move32(location, instruction->GetLocations()->Out());
429 break;
430
431 case Primitive::kPrimLong:
432 Move64(location, instruction->GetLocations()->Out());
433 break;
434
435 default:
436 LOG(FATAL) << "Unimplemented type " << instruction->GetType();
437 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000438 }
439}
440
441void LocationsBuilderARM::VisitGoto(HGoto* got) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000442 got->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000443}
444
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000445void InstructionCodeGeneratorARM::VisitGoto(HGoto* got) {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000446 HBasicBlock* successor = got->GetSuccessor();
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000447 if (GetGraph()->GetExitBlock() == successor) {
448 codegen_->GenerateFrameExit();
449 } else if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
450 __ b(codegen_->GetLabelOf(successor));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000451 }
452}
453
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000454void LocationsBuilderARM::VisitExit(HExit* exit) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000455 exit->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000456}
457
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000458void InstructionCodeGeneratorARM::VisitExit(HExit* exit) {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000459 if (kIsDebugBuild) {
460 __ Comment("Unreachable");
461 __ bkpt(0);
462 }
463}
464
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000465void LocationsBuilderARM::VisitIf(HIf* if_instr) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000466 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100467 HInstruction* cond = if_instr->InputAt(0);
468 DCHECK(cond->IsCondition());
469 HCondition* condition = cond->AsCondition();
470 if (condition->NeedsMaterialization()) {
471 locations->SetInAt(0, Location::Any());
472 }
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000473 if_instr->SetLocations(locations);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000474}
475
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000476void InstructionCodeGeneratorARM::VisitIf(HIf* if_instr) {
Dave Allison20dfc792014-06-16 20:44:29 -0700477 HInstruction* cond = if_instr->InputAt(0);
478 DCHECK(cond->IsCondition());
479 HCondition* condition = cond->AsCondition();
480 if (condition->NeedsMaterialization()) {
481 // Condition has been materialized, compare the output to 0
482 if (!if_instr->GetLocations()->InAt(0).IsRegister()) {
483 LOG(FATAL) << "Materialized condition is not in an ARM register";
484 }
485 __ cmp(if_instr->GetLocations()->InAt(0).AsArm().AsCoreRegister(),
486 ShifterOperand(0));
487 __ b(codegen_->GetLabelOf(if_instr->IfTrueSuccessor()), EQ);
488 } else {
489 // Condition has not been materialized, use its inputs as the comparison and its
490 // condition as the branch condition.
491 __ cmp(condition->GetLocations()->InAt(0).AsArm().AsCoreRegister(),
492 ShifterOperand(condition->GetLocations()->InAt(1).AsArm().AsCoreRegister()));
493 __ b(codegen_->GetLabelOf(if_instr->IfTrueSuccessor()),
494 ARMCondition(condition->GetCondition()));
495 }
496 if (!codegen_->GoesToNextBlock(if_instr->GetBlock(), if_instr->IfFalseSuccessor())) {
497 __ b(codegen_->GetLabelOf(if_instr->IfFalseSuccessor()));
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000498 }
499}
500
Dave Allison20dfc792014-06-16 20:44:29 -0700501
502void LocationsBuilderARM::VisitCondition(HCondition* comp) {
503 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(comp);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100504 locations->SetInAt(0, Location::RequiresRegister());
505 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100506 if (comp->NeedsMaterialization()) {
507 locations->SetOut(Location::RequiresRegister());
508 }
Dave Allison20dfc792014-06-16 20:44:29 -0700509 comp->SetLocations(locations);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000510}
511
Dave Allison20dfc792014-06-16 20:44:29 -0700512void InstructionCodeGeneratorARM::VisitCondition(HCondition* comp) {
513 if (comp->NeedsMaterialization()) {
514 LocationSummary* locations = comp->GetLocations();
515 __ cmp(locations->InAt(0).AsArm().AsCoreRegister(),
516 ShifterOperand(locations->InAt(1).AsArm().AsCoreRegister()));
517 __ it(ARMCondition(comp->GetCondition()), kItElse);
518 __ mov(locations->Out().AsArm().AsCoreRegister(), ShifterOperand(1),
519 ARMCondition(comp->GetCondition()));
520 __ mov(locations->Out().AsArm().AsCoreRegister(), ShifterOperand(0),
521 ARMOppositeCondition(comp->GetCondition()));
522 }
523}
524
525void LocationsBuilderARM::VisitEqual(HEqual* comp) {
526 VisitCondition(comp);
527}
528
529void InstructionCodeGeneratorARM::VisitEqual(HEqual* comp) {
530 VisitCondition(comp);
531}
532
533void LocationsBuilderARM::VisitNotEqual(HNotEqual* comp) {
534 VisitCondition(comp);
535}
536
537void InstructionCodeGeneratorARM::VisitNotEqual(HNotEqual* comp) {
538 VisitCondition(comp);
539}
540
541void LocationsBuilderARM::VisitLessThan(HLessThan* comp) {
542 VisitCondition(comp);
543}
544
545void InstructionCodeGeneratorARM::VisitLessThan(HLessThan* comp) {
546 VisitCondition(comp);
547}
548
549void LocationsBuilderARM::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
550 VisitCondition(comp);
551}
552
553void InstructionCodeGeneratorARM::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
554 VisitCondition(comp);
555}
556
557void LocationsBuilderARM::VisitGreaterThan(HGreaterThan* comp) {
558 VisitCondition(comp);
559}
560
561void InstructionCodeGeneratorARM::VisitGreaterThan(HGreaterThan* comp) {
562 VisitCondition(comp);
563}
564
565void LocationsBuilderARM::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
566 VisitCondition(comp);
567}
568
569void InstructionCodeGeneratorARM::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
570 VisitCondition(comp);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000571}
572
573void LocationsBuilderARM::VisitLocal(HLocal* local) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000574 local->SetLocations(nullptr);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000575}
576
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000577void InstructionCodeGeneratorARM::VisitLocal(HLocal* local) {
578 DCHECK_EQ(local->GetBlock(), GetGraph()->GetEntryBlock());
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000579}
580
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000581void LocationsBuilderARM::VisitLoadLocal(HLoadLocal* load) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100582 load->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000583}
584
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000585void InstructionCodeGeneratorARM::VisitLoadLocal(HLoadLocal* load) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100586 // Nothing to do, this is driven by the code generator.
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000587}
588
589void LocationsBuilderARM::VisitStoreLocal(HStoreLocal* store) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000590 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(store);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100591 switch (store->InputAt(1)->GetType()) {
592 case Primitive::kPrimBoolean:
593 case Primitive::kPrimByte:
594 case Primitive::kPrimChar:
595 case Primitive::kPrimShort:
596 case Primitive::kPrimInt:
597 case Primitive::kPrimNot:
598 locations->SetInAt(1, Location::StackSlot(codegen_->GetStackSlot(store->GetLocal())));
599 break;
600
601 case Primitive::kPrimLong:
602 locations->SetInAt(1, Location::DoubleStackSlot(codegen_->GetStackSlot(store->GetLocal())));
603 break;
604
605 default:
606 LOG(FATAL) << "Unimplemented local type " << store->InputAt(1)->GetType();
607 }
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000608 store->SetLocations(locations);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000609}
610
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000611void InstructionCodeGeneratorARM::VisitStoreLocal(HStoreLocal* store) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000612}
613
614void LocationsBuilderARM::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100615 // TODO: Support constant locations.
616 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
617 locations->SetOut(Location::RequiresRegister());
618 constant->SetLocations(locations);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000619}
620
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000621void InstructionCodeGeneratorARM::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +0100622 codegen_->Move(constant, constant->GetLocations()->Out(), nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000623}
624
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100625void LocationsBuilderARM::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100626 // TODO: Support constant locations.
627 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
628 locations->SetOut(Location::RequiresRegister());
629 constant->SetLocations(locations);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100630}
631
632void InstructionCodeGeneratorARM::VisitLongConstant(HLongConstant* constant) {
633 // Will be generated at use site.
634}
635
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000636void LocationsBuilderARM::VisitReturnVoid(HReturnVoid* ret) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000637 ret->SetLocations(nullptr);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000638}
639
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000640void InstructionCodeGeneratorARM::VisitReturnVoid(HReturnVoid* ret) {
641 codegen_->GenerateFrameExit();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000642}
643
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000644void LocationsBuilderARM::VisitReturn(HReturn* ret) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000645 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(ret);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100646 switch (ret->InputAt(0)->GetType()) {
647 case Primitive::kPrimBoolean:
648 case Primitive::kPrimByte:
649 case Primitive::kPrimChar:
650 case Primitive::kPrimShort:
651 case Primitive::kPrimInt:
652 case Primitive::kPrimNot:
653 locations->SetInAt(0, ArmCoreLocation(R0));
654 break;
655
656 case Primitive::kPrimLong:
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100657 locations->SetInAt(
658 0, Location::RegisterLocation(ArmManagedRegister::FromRegisterPair(R0_R1)));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100659 break;
660
661 default:
662 LOG(FATAL) << "Unimplemented return type " << ret->InputAt(0)->GetType();
663 }
664
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000665 ret->SetLocations(locations);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000666}
667
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000668void InstructionCodeGeneratorARM::VisitReturn(HReturn* ret) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100669 if (kIsDebugBuild) {
670 switch (ret->InputAt(0)->GetType()) {
671 case Primitive::kPrimBoolean:
672 case Primitive::kPrimByte:
673 case Primitive::kPrimChar:
674 case Primitive::kPrimShort:
675 case Primitive::kPrimInt:
676 case Primitive::kPrimNot:
677 DCHECK_EQ(ret->GetLocations()->InAt(0).AsArm().AsCoreRegister(), R0);
678 break;
679
680 case Primitive::kPrimLong:
681 DCHECK_EQ(ret->GetLocations()->InAt(0).AsArm().AsRegisterPair(), R0_R1);
682 break;
683
684 default:
685 LOG(FATAL) << "Unimplemented return type " << ret->InputAt(0)->GetType();
686 }
687 }
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000688 codegen_->GenerateFrameExit();
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000689}
690
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000691void LocationsBuilderARM::VisitInvokeStatic(HInvokeStatic* invoke) {
692 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(invoke);
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +0100693 locations->AddTemp(ArmCoreLocation(R0));
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100694
695 InvokeDexCallingConventionVisitor calling_convention_visitor;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100696 for (size_t i = 0; i < invoke->InputCount(); i++) {
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100697 HInstruction* input = invoke->InputAt(i);
698 locations->SetInAt(i, calling_convention_visitor.GetNextLocation(input->GetType()));
699 }
700
701 switch (invoke->GetType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100702 case Primitive::kPrimBoolean:
703 case Primitive::kPrimByte:
704 case Primitive::kPrimChar:
705 case Primitive::kPrimShort:
706 case Primitive::kPrimInt:
707 case Primitive::kPrimNot:
708 locations->SetOut(ArmCoreLocation(R0));
709 break;
710
711 case Primitive::kPrimLong:
712 locations->SetOut(Location::RegisterLocation(ArmManagedRegister::FromRegisterPair(R0_R1)));
713 break;
714
715 case Primitive::kPrimVoid:
716 break;
717
718 case Primitive::kPrimDouble:
719 case Primitive::kPrimFloat:
720 LOG(FATAL) << "Unimplemented return type " << invoke->GetType();
721 break;
722 }
723
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000724 invoke->SetLocations(locations);
725}
726
727void InstructionCodeGeneratorARM::LoadCurrentMethod(Register reg) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100728 __ ldr(reg, Address(SP, kCurrentMethodStackOffset));
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000729}
730
731void InstructionCodeGeneratorARM::VisitInvokeStatic(HInvokeStatic* invoke) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100732 Register temp = invoke->GetLocations()->GetTemp(0).AsArm().AsCoreRegister();
Nicolas Geoffrayf61b5372014-06-25 14:35:34 +0100733 uint32_t heap_reference_size = sizeof(mirror::HeapReference<mirror::Object>);
734 size_t index_in_cache = mirror::Array::DataOffset(heap_reference_size).Int32Value() +
Nicolas Geoffray707c8092014-04-04 10:50:14 +0100735 invoke->GetIndexInDexCache() * kArmWordSize;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000736
737 // TODO: Implement all kinds of calls:
738 // 1) boot -> boot
739 // 2) app -> boot
740 // 3) app -> app
741 //
742 // Currently we implement the app -> app logic, which looks up in the resolve cache.
743
744 // temp = method;
745 LoadCurrentMethod(temp);
746 // temp = temp->dex_cache_resolved_methods_;
747 __ ldr(temp, Address(temp, mirror::ArtMethod::DexCacheResolvedMethodsOffset().Int32Value()));
748 // temp = temp[index_in_cache]
749 __ ldr(temp, Address(temp, index_in_cache));
750 // LR = temp[offset_of_quick_compiled_code]
751 __ ldr(LR, Address(temp,
752 mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset().Int32Value()));
753 // LR()
754 __ blx(LR);
755
756 codegen_->RecordPcInfo(invoke->GetDexPc());
757}
758
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +0000759void LocationsBuilderARM::VisitAdd(HAdd* add) {
760 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(add);
761 switch (add->GetResultType()) {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100762 case Primitive::kPrimInt:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100763 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100764 locations->SetInAt(0, Location::RequiresRegister());
765 locations->SetInAt(1, Location::RequiresRegister());
766 locations->SetOut(Location::RequiresRegister());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100767 break;
768 }
769
770 case Primitive::kPrimBoolean:
771 case Primitive::kPrimByte:
772 case Primitive::kPrimChar:
773 case Primitive::kPrimShort:
774 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
775 break;
776
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +0000777 default:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100778 LOG(FATAL) << "Unimplemented add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +0000779 }
780 add->SetLocations(locations);
781}
782
783void InstructionCodeGeneratorARM::VisitAdd(HAdd* add) {
784 LocationSummary* locations = add->GetLocations();
785 switch (add->GetResultType()) {
786 case Primitive::kPrimInt:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100787 __ add(locations->Out().AsArm().AsCoreRegister(),
788 locations->InAt(0).AsArm().AsCoreRegister(),
789 ShifterOperand(locations->InAt(1).AsArm().AsCoreRegister()));
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +0000790 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100791
792 case Primitive::kPrimLong:
793 __ adds(locations->Out().AsArm().AsRegisterPairLow(),
794 locations->InAt(0).AsArm().AsRegisterPairLow(),
795 ShifterOperand(locations->InAt(1).AsArm().AsRegisterPairLow()));
796 __ adc(locations->Out().AsArm().AsRegisterPairHigh(),
797 locations->InAt(0).AsArm().AsRegisterPairHigh(),
798 ShifterOperand(locations->InAt(1).AsArm().AsRegisterPairHigh()));
799 break;
800
801 case Primitive::kPrimBoolean:
802 case Primitive::kPrimByte:
803 case Primitive::kPrimChar:
804 case Primitive::kPrimShort:
805 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
806 break;
807
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +0000808 default:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100809 LOG(FATAL) << "Unimplemented add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +0000810 }
811}
812
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100813void LocationsBuilderARM::VisitSub(HSub* sub) {
814 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(sub);
815 switch (sub->GetResultType()) {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100816 case Primitive::kPrimInt:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100817 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100818 locations->SetInAt(0, Location::RequiresRegister());
819 locations->SetInAt(1, Location::RequiresRegister());
820 locations->SetOut(Location::RequiresRegister());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100821 break;
822 }
823
824 case Primitive::kPrimBoolean:
825 case Primitive::kPrimByte:
826 case Primitive::kPrimChar:
827 case Primitive::kPrimShort:
828 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
829 break;
830
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100831 default:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100832 LOG(FATAL) << "Unimplemented sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100833 }
834 sub->SetLocations(locations);
835}
836
837void InstructionCodeGeneratorARM::VisitSub(HSub* sub) {
838 LocationSummary* locations = sub->GetLocations();
839 switch (sub->GetResultType()) {
840 case Primitive::kPrimInt:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100841 __ sub(locations->Out().AsArm().AsCoreRegister(),
842 locations->InAt(0).AsArm().AsCoreRegister(),
843 ShifterOperand(locations->InAt(1).AsArm().AsCoreRegister()));
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100844 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100845
846 case Primitive::kPrimLong:
847 __ subs(locations->Out().AsArm().AsRegisterPairLow(),
848 locations->InAt(0).AsArm().AsRegisterPairLow(),
849 ShifterOperand(locations->InAt(1).AsArm().AsRegisterPairLow()));
850 __ sbc(locations->Out().AsArm().AsRegisterPairHigh(),
851 locations->InAt(0).AsArm().AsRegisterPairHigh(),
852 ShifterOperand(locations->InAt(1).AsArm().AsRegisterPairHigh()));
853 break;
854
855 case Primitive::kPrimBoolean:
856 case Primitive::kPrimByte:
857 case Primitive::kPrimChar:
858 case Primitive::kPrimShort:
859 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
860 break;
861
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100862 default:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100863 LOG(FATAL) << "Unimplemented sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100864 }
865}
866
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +0100867static constexpr Register kRuntimeParameterCoreRegisters[] = { R0, R1 };
868static constexpr size_t kRuntimeParameterCoreRegistersLength =
869 arraysize(kRuntimeParameterCoreRegisters);
870
871class InvokeRuntimeCallingConvention : public CallingConvention<Register> {
872 public:
873 InvokeRuntimeCallingConvention()
874 : CallingConvention(kRuntimeParameterCoreRegisters,
875 kRuntimeParameterCoreRegistersLength) {}
876
877 private:
878 DISALLOW_COPY_AND_ASSIGN(InvokeRuntimeCallingConvention);
879};
880
881void LocationsBuilderARM::VisitNewInstance(HNewInstance* instruction) {
882 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100883 InvokeRuntimeCallingConvention calling_convention;
884 locations->AddTemp(ArmCoreLocation(calling_convention.GetRegisterAt(0)));
885 locations->AddTemp(ArmCoreLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100886 locations->SetOut(ArmCoreLocation(R0));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +0100887 instruction->SetLocations(locations);
888}
889
890void InstructionCodeGeneratorARM::VisitNewInstance(HNewInstance* instruction) {
891 InvokeRuntimeCallingConvention calling_convention;
892 LoadCurrentMethod(calling_convention.GetRegisterAt(1));
893 __ LoadImmediate(calling_convention.GetRegisterAt(0), instruction->GetTypeIndex());
894
Nicolas Geoffray707c8092014-04-04 10:50:14 +0100895 int32_t offset = QUICK_ENTRYPOINT_OFFSET(kArmWordSize, pAllocObjectWithAccessCheck).Int32Value();
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +0100896 __ ldr(LR, Address(TR, offset));
897 __ blx(LR);
898
899 codegen_->RecordPcInfo(instruction->GetDexPc());
900}
901
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100902void LocationsBuilderARM::VisitParameterValue(HParameterValue* instruction) {
903 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100904 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
905 if (location.IsStackSlot()) {
906 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
907 } else if (location.IsDoubleStackSlot()) {
908 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100909 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100910 locations->SetOut(location);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100911 instruction->SetLocations(locations);
912}
913
914void InstructionCodeGeneratorARM::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100915 // Nothing to do, the parameter is already at its location.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100916}
917
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +0100918void LocationsBuilderARM::VisitNot(HNot* instruction) {
919 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100920 locations->SetInAt(0, Location::RequiresRegister());
921 locations->SetOut(Location::RequiresRegister());
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +0100922 instruction->SetLocations(locations);
923}
924
925void InstructionCodeGeneratorARM::VisitNot(HNot* instruction) {
926 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100927 __ eor(locations->Out().AsArm().AsCoreRegister(),
928 locations->InAt(0).AsArm().AsCoreRegister(), ShifterOperand(1));
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +0100929}
930
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100931void LocationsBuilderARM::VisitCompare(HCompare* compare) {
932 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(compare);
933 locations->SetInAt(0, Location::RequiresRegister());
934 locations->SetInAt(1, Location::RequiresRegister());
935 locations->SetOut(Location::RequiresRegister());
936 compare->SetLocations(locations);
937}
938
939void InstructionCodeGeneratorARM::VisitCompare(HCompare* compare) {
940 Label greater, done;
941 LocationSummary* locations = compare->GetLocations();
942 switch (compare->InputAt(0)->GetType()) {
943 case Primitive::kPrimLong: {
944 Register output = locations->Out().AsArm().AsCoreRegister();
945 ArmManagedRegister left = locations->InAt(0).AsArm();
946 ArmManagedRegister right = locations->InAt(1).AsArm();
947 Label less, greater, done;
948 __ cmp(left.AsRegisterPairHigh(),
949 ShifterOperand(right.AsRegisterPairHigh())); // Signed compare.
950 __ b(&less, LT);
951 __ b(&greater, GT);
Nicolas Geoffray8d486732014-07-16 16:23:40 +0100952 // Do LoadImmediate before any `cmp`, as LoadImmediate might affect
953 // the status flags.
954 __ LoadImmediate(output, 0);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100955 __ cmp(left.AsRegisterPairLow(),
956 ShifterOperand(right.AsRegisterPairLow())); // Unsigned compare.
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100957 __ b(&done, EQ);
958 __ b(&less, CC);
959
960 __ Bind(&greater);
961 __ LoadImmediate(output, 1);
962 __ b(&done);
963
964 __ Bind(&less);
965 __ LoadImmediate(output, -1);
966
967 __ Bind(&done);
968 break;
969 }
970 default:
971 LOG(FATAL) << "Unimplemented compare type " << compare->InputAt(0)->GetType();
972 }
973}
974
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100975void LocationsBuilderARM::VisitPhi(HPhi* instruction) {
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100976 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
977 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
978 locations->SetInAt(i, Location::Any());
979 }
980 locations->SetOut(Location::Any());
981 instruction->SetLocations(locations);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100982}
983
984void InstructionCodeGeneratorARM::VisitPhi(HPhi* instruction) {
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +0100985 LOG(FATAL) << "Unreachable";
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100986}
987
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100988void LocationsBuilderARM::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
989 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
990 locations->SetInAt(0, Location::RequiresRegister());
991 locations->SetInAt(1, Location::RequiresRegister());
992 instruction->SetLocations(locations);
993}
994
995void InstructionCodeGeneratorARM::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
996 LocationSummary* locations = instruction->GetLocations();
997 Register obj = locations->InAt(0).AsArm().AsCoreRegister();
998 uint32_t offset = instruction->GetFieldOffset().Uint32Value();
999 Primitive::Type field_type = instruction->InputAt(1)->GetType();
1000
1001 switch (field_type) {
1002 case Primitive::kPrimBoolean:
1003 case Primitive::kPrimByte: {
1004 Register value = locations->InAt(1).AsArm().AsCoreRegister();
1005 __ StoreToOffset(kStoreByte, value, obj, offset);
1006 break;
1007 }
1008
1009 case Primitive::kPrimShort:
1010 case Primitive::kPrimChar: {
1011 Register value = locations->InAt(1).AsArm().AsCoreRegister();
1012 __ StoreToOffset(kStoreHalfword, value, obj, offset);
1013 break;
1014 }
1015
1016 case Primitive::kPrimInt:
1017 case Primitive::kPrimNot: {
1018 Register value = locations->InAt(1).AsArm().AsCoreRegister();
1019 __ StoreToOffset(kStoreWord, value, obj, offset);
1020 break;
1021 }
1022
1023 case Primitive::kPrimLong: {
1024 ArmManagedRegister value = locations->InAt(1).AsArm();
1025 __ StoreToOffset(kStoreWordPair, value.AsRegisterPairLow(), obj, offset);
1026 break;
1027 }
1028
1029 case Primitive::kPrimFloat:
1030 case Primitive::kPrimDouble:
1031 LOG(FATAL) << "Unimplemented register type " << field_type;
1032
1033 case Primitive::kPrimVoid:
1034 LOG(FATAL) << "Unreachable type " << field_type;
1035 }
1036}
1037
1038void LocationsBuilderARM::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
1039 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
1040 locations->SetInAt(0, Location::RequiresRegister());
1041 locations->SetOut(Location::RequiresRegister());
1042 instruction->SetLocations(locations);
1043}
1044
1045void InstructionCodeGeneratorARM::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
1046 LocationSummary* locations = instruction->GetLocations();
1047 Register obj = locations->InAt(0).AsArm().AsCoreRegister();
1048 uint32_t offset = instruction->GetFieldOffset().Uint32Value();
1049
1050 switch (instruction->GetType()) {
1051 case Primitive::kPrimBoolean: {
1052 Register out = locations->Out().AsArm().AsCoreRegister();
1053 __ LoadFromOffset(kLoadUnsignedByte, out, obj, offset);
1054 break;
1055 }
1056
1057 case Primitive::kPrimByte: {
1058 Register out = locations->Out().AsArm().AsCoreRegister();
1059 __ LoadFromOffset(kLoadSignedByte, out, obj, offset);
1060 break;
1061 }
1062
1063 case Primitive::kPrimShort: {
1064 Register out = locations->Out().AsArm().AsCoreRegister();
1065 __ LoadFromOffset(kLoadSignedHalfword, out, obj, offset);
1066 break;
1067 }
1068
1069 case Primitive::kPrimChar: {
1070 Register out = locations->Out().AsArm().AsCoreRegister();
1071 __ LoadFromOffset(kLoadUnsignedHalfword, out, obj, offset);
1072 break;
1073 }
1074
1075 case Primitive::kPrimInt:
1076 case Primitive::kPrimNot: {
1077 Register out = locations->Out().AsArm().AsCoreRegister();
1078 __ LoadFromOffset(kLoadWord, out, obj, offset);
1079 break;
1080 }
1081
1082 case Primitive::kPrimLong: {
1083 // TODO: support volatile.
1084 ArmManagedRegister out = locations->Out().AsArm();
1085 __ LoadFromOffset(kLoadWordPair, out.AsRegisterPairLow(), obj, offset);
1086 break;
1087 }
1088
1089 case Primitive::kPrimFloat:
1090 case Primitive::kPrimDouble:
1091 LOG(FATAL) << "Unimplemented register type " << instruction->GetType();
1092
1093 case Primitive::kPrimVoid:
1094 LOG(FATAL) << "Unreachable type " << instruction->GetType();
1095 }
1096}
1097
1098void LocationsBuilderARM::VisitNullCheck(HNullCheck* instruction) {
1099 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
1100 locations->SetInAt(0, Location::RequiresRegister());
1101 // TODO: Have a normalization phase that makes this instruction never used.
1102 locations->SetOut(Location::SameAsFirstInput());
1103 instruction->SetLocations(locations);
1104}
1105
1106void InstructionCodeGeneratorARM::VisitNullCheck(HNullCheck* instruction) {
1107 SlowPathCode* slow_path =
1108 new (GetGraph()->GetArena()) NullCheckSlowPathARM(instruction->GetDexPc());
1109 codegen_->AddSlowPath(slow_path);
1110
1111 LocationSummary* locations = instruction->GetLocations();
1112 Location obj = locations->InAt(0);
1113 DCHECK(obj.Equals(locations->Out()));
1114
1115 if (obj.IsRegister()) {
1116 __ cmp(obj.AsArm().AsCoreRegister(), ShifterOperand(0));
1117 }
1118 __ b(slow_path->GetEntryLabel(), EQ);
1119}
1120
1121void LocationsBuilderARM::VisitTemporary(HTemporary* temp) {
1122 temp->SetLocations(nullptr);
1123}
1124
1125void InstructionCodeGeneratorARM::VisitTemporary(HTemporary* temp) {
1126 // Nothing to do, this is driven by the code generator.
1127}
1128
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01001129void LocationsBuilderARM::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01001130 LOG(FATAL) << "Unreachable";
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01001131}
1132
1133void InstructionCodeGeneratorARM::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01001134 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
1135}
1136
1137ArmAssembler* ParallelMoveResolverARM::GetAssembler() const {
1138 return codegen_->GetAssembler();
1139}
1140
1141void ParallelMoveResolverARM::EmitMove(size_t index) {
1142 MoveOperands* move = moves_.Get(index);
1143 Location source = move->GetSource();
1144 Location destination = move->GetDestination();
1145
1146 if (source.IsRegister()) {
1147 if (destination.IsRegister()) {
1148 __ Mov(destination.AsArm().AsCoreRegister(), source.AsArm().AsCoreRegister());
1149 } else {
1150 DCHECK(destination.IsStackSlot());
1151 __ StoreToOffset(kStoreWord, source.AsArm().AsCoreRegister(),
1152 SP, destination.GetStackIndex());
1153 }
1154 } else if (source.IsStackSlot()) {
1155 if (destination.IsRegister()) {
1156 __ LoadFromOffset(kLoadWord, destination.AsArm().AsCoreRegister(),
1157 SP, source.GetStackIndex());
1158 } else {
1159 DCHECK(destination.IsStackSlot());
1160 __ LoadFromOffset(kLoadWord, IP, SP, source.GetStackIndex());
1161 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
1162 }
1163 } else {
1164 LOG(FATAL) << "Unimplemented";
1165 }
1166}
1167
1168void ParallelMoveResolverARM::Exchange(Register reg, int mem) {
1169 __ Mov(IP, reg);
1170 __ LoadFromOffset(kLoadWord, reg, SP, mem);
1171 __ StoreToOffset(kStoreWord, IP, SP, mem);
1172}
1173
1174void ParallelMoveResolverARM::Exchange(int mem1, int mem2) {
1175 ScratchRegisterScope ensure_scratch(this, IP, R0, codegen_->GetNumberOfCoreRegisters());
1176 int stack_offset = ensure_scratch.IsSpilled() ? kArmWordSize : 0;
1177 __ LoadFromOffset(kLoadWord, static_cast<Register>(ensure_scratch.GetRegister()),
1178 SP, mem1 + stack_offset);
1179 __ LoadFromOffset(kLoadWord, IP, SP, mem2 + stack_offset);
1180 __ StoreToOffset(kStoreWord, static_cast<Register>(ensure_scratch.GetRegister()),
1181 SP, mem2 + stack_offset);
1182 __ StoreToOffset(kStoreWord, IP, SP, mem1 + stack_offset);
1183}
1184
1185void ParallelMoveResolverARM::EmitSwap(size_t index) {
1186 MoveOperands* move = moves_.Get(index);
1187 Location source = move->GetSource();
1188 Location destination = move->GetDestination();
1189
1190 if (source.IsRegister() && destination.IsRegister()) {
1191 DCHECK_NE(source.AsArm().AsCoreRegister(), IP);
1192 DCHECK_NE(destination.AsArm().AsCoreRegister(), IP);
1193 __ Mov(IP, source.AsArm().AsCoreRegister());
1194 __ Mov(source.AsArm().AsCoreRegister(), destination.AsArm().AsCoreRegister());
1195 __ Mov(destination.AsArm().AsCoreRegister(), IP);
1196 } else if (source.IsRegister() && destination.IsStackSlot()) {
1197 Exchange(source.AsArm().AsCoreRegister(), destination.GetStackIndex());
1198 } else if (source.IsStackSlot() && destination.IsRegister()) {
1199 Exchange(destination.AsArm().AsCoreRegister(), source.GetStackIndex());
1200 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
1201 Exchange(source.GetStackIndex(), destination.GetStackIndex());
1202 } else {
1203 LOG(FATAL) << "Unimplemented";
1204 }
1205}
1206
1207void ParallelMoveResolverARM::SpillScratch(int reg) {
1208 __ Push(static_cast<Register>(reg));
1209}
1210
1211void ParallelMoveResolverARM::RestoreScratch(int reg) {
1212 __ Pop(static_cast<Register>(reg));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01001213}
1214
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001215} // namespace arm
1216} // namespace art