blob: ed3f43c9a5440da2374f0df95e8a053060ad3b44 [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"
18#include "utils/assembler.h"
19#include "utils/arm/assembler_arm.h"
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +010020#include "utils/arm/managed_register_arm.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000021
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070022#include "entrypoints/quick/quick_entrypoints.h"
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +000023#include "mirror/array.h"
24#include "mirror/art_method.h"
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070025#include "thread.h"
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +000026
Nicolas Geoffray787c3072014-03-17 10:20:19 +000027#define __ reinterpret_cast<ArmAssembler*>(GetAssembler())->
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000028
29namespace art {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +010030
31arm::ArmManagedRegister Location::AsArm() const {
32 return reg().AsArm();
33}
34
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000035namespace arm {
36
Nicolas Geoffray4a34a422014-04-03 10:38:37 +010037static constexpr int kNumberOfPushedRegistersAtEntry = 1;
38static constexpr int kCurrentMethodStackOffset = 0;
39
Nicolas Geoffraya7062e02014-05-22 12:50:17 +010040void CodeGeneratorARM::DumpCoreRegister(std::ostream& stream, int reg) const {
41 stream << ArmManagedRegister::FromCoreRegister(Register(reg));
42}
43
44void CodeGeneratorARM::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
45 stream << ArmManagedRegister::FromDRegister(DRegister(reg));
46}
47
Nicolas Geoffraya7aca372014-04-28 17:47:12 +010048CodeGeneratorARM::CodeGeneratorARM(HGraph* graph)
49 : CodeGenerator(graph, kNumberOfRegIds),
50 location_builder_(graph, this),
51 instruction_visitor_(graph, this) {}
52
53static bool* GetBlockedRegisterPairs(bool* blocked_registers) {
54 return blocked_registers + kNumberOfAllocIds;
55}
56
57ManagedRegister CodeGeneratorARM::AllocateFreeRegister(Primitive::Type type,
58 bool* blocked_registers) const {
59 switch (type) {
60 case Primitive::kPrimLong: {
61 size_t reg = AllocateFreeRegisterInternal(
62 GetBlockedRegisterPairs(blocked_registers), kNumberOfRegisterPairs);
63 ArmManagedRegister pair =
64 ArmManagedRegister::FromRegisterPair(static_cast<RegisterPair>(reg));
65 blocked_registers[pair.AsRegisterPairLow()] = true;
66 blocked_registers[pair.AsRegisterPairHigh()] = true;
67 return pair;
68 }
69
70 case Primitive::kPrimByte:
71 case Primitive::kPrimBoolean:
72 case Primitive::kPrimChar:
73 case Primitive::kPrimShort:
74 case Primitive::kPrimInt:
75 case Primitive::kPrimNot: {
76 size_t reg = AllocateFreeRegisterInternal(blocked_registers, kNumberOfCoreRegisters);
77 return ArmManagedRegister::FromCoreRegister(static_cast<Register>(reg));
78 }
79
80 case Primitive::kPrimFloat:
81 case Primitive::kPrimDouble:
82 LOG(FATAL) << "Unimplemented register type " << type;
83
84 case Primitive::kPrimVoid:
85 LOG(FATAL) << "Unreachable type " << type;
86 }
87
88 return ManagedRegister::NoRegister();
89}
90
91void CodeGeneratorARM::SetupBlockedRegisters(bool* blocked_registers) const {
92 bool* blocked_register_pairs = GetBlockedRegisterPairs(blocked_registers);
93
94 // Don't allocate the dalvik style register pair passing.
95 blocked_register_pairs[R1_R2] = true;
96
97 // Stack register, LR and PC are always reserved.
98 blocked_registers[SP] = true;
99 blocked_registers[LR] = true;
100 blocked_registers[PC] = true;
101
102 // Reserve R4 for suspend check.
103 blocked_registers[R4] = true;
104 blocked_register_pairs[R4_R5] = true;
105
106 // Reserve thread register.
107 blocked_registers[TR] = true;
108
109 // TODO: We currently don't use Quick's callee saved registers.
110 blocked_registers[R5] = true;
111 blocked_registers[R6] = true;
112 blocked_registers[R7] = true;
113 blocked_registers[R8] = true;
114 blocked_registers[R10] = true;
115 blocked_registers[R11] = true;
116 blocked_register_pairs[R6_R7] = true;
117}
118
119size_t CodeGeneratorARM::GetNumberOfRegisters() const {
120 return kNumberOfRegIds;
121}
122
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100123static Location ArmCoreLocation(Register reg) {
124 return Location::RegisterLocation(ArmManagedRegister::FromCoreRegister(reg));
125}
126
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100127InstructionCodeGeneratorARM::InstructionCodeGeneratorARM(HGraph* graph, CodeGeneratorARM* codegen)
128 : HGraphVisitor(graph),
129 assembler_(codegen->GetAssembler()),
130 codegen_(codegen) {}
131
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000132void CodeGeneratorARM::GenerateFrameEntry() {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000133 core_spill_mask_ |= (1 << LR);
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100134 __ PushList((1 << LR));
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000135
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100136 SetFrameSize(RoundUp(
137 (GetGraph()->GetMaximumNumberOfOutVRegs() + GetGraph()->GetNumberOfVRegs()) * kVRegSize
138 + kVRegSize // filler
139 + kArmWordSize // Art method
140 + kNumberOfPushedRegistersAtEntry * kArmWordSize,
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100141 kStackAlignment));
142 // The return PC has already been pushed on the stack.
Nicolas Geoffray707c8092014-04-04 10:50:14 +0100143 __ AddConstant(SP, -(GetFrameSize() - kNumberOfPushedRegistersAtEntry * kArmWordSize));
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000144 __ str(R0, Address(SP, 0));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000145}
146
147void CodeGeneratorARM::GenerateFrameExit() {
Nicolas Geoffray707c8092014-04-04 10:50:14 +0100148 __ AddConstant(SP, GetFrameSize() - kNumberOfPushedRegistersAtEntry * kArmWordSize);
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100149 __ PopList((1 << PC));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000150}
151
152void CodeGeneratorARM::Bind(Label* label) {
153 __ Bind(label);
154}
155
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100156int32_t CodeGeneratorARM::GetStackSlot(HLocal* local) const {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100157 uint16_t reg_number = local->GetRegNumber();
158 uint16_t number_of_vregs = GetGraph()->GetNumberOfVRegs();
159 uint16_t number_of_in_vregs = GetGraph()->GetNumberOfInVRegs();
160 if (reg_number >= number_of_vregs - number_of_in_vregs) {
161 // Local is a parameter of the method. It is stored in the caller's frame.
162 return GetFrameSize() + kArmWordSize // ART method
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100163 + (reg_number - number_of_vregs + number_of_in_vregs) * kVRegSize;
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100164 } else {
165 // Local is a temporary in this method. It is stored in this method's frame.
166 return GetFrameSize() - (kNumberOfPushedRegistersAtEntry * kArmWordSize)
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100167 - kVRegSize // filler.
168 - (number_of_vregs * kVRegSize)
169 + (reg_number * kVRegSize);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100170 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000171}
172
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100173Location CodeGeneratorARM::GetStackLocation(HLoadLocal* load) const {
174 switch (load->GetType()) {
175 case Primitive::kPrimLong:
176 return Location::DoubleStackSlot(GetStackSlot(load->GetLocal()));
177 break;
178
179 case Primitive::kPrimInt:
180 case Primitive::kPrimNot:
181 return Location::StackSlot(GetStackSlot(load->GetLocal()));
182
183 case Primitive::kPrimFloat:
184 case Primitive::kPrimDouble:
185 LOG(FATAL) << "Unimplemented type " << load->GetType();
186
187 case Primitive::kPrimBoolean:
188 case Primitive::kPrimByte:
189 case Primitive::kPrimChar:
190 case Primitive::kPrimShort:
191 case Primitive::kPrimVoid:
192 LOG(FATAL) << "Unexpected type " << load->GetType();
193 }
194
195 LOG(FATAL) << "Unreachable";
196 return Location();
197}
198
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100199Location InvokeDexCallingConventionVisitor::GetNextLocation(Primitive::Type type) {
200 switch (type) {
201 case Primitive::kPrimBoolean:
202 case Primitive::kPrimByte:
203 case Primitive::kPrimChar:
204 case Primitive::kPrimShort:
205 case Primitive::kPrimInt:
206 case Primitive::kPrimNot: {
207 uint32_t index = gp_index_++;
208 if (index < calling_convention.GetNumberOfRegisters()) {
209 return ArmCoreLocation(calling_convention.GetRegisterAt(index));
210 } else {
211 return Location::StackSlot(calling_convention.GetStackOffsetOf(index, kArmWordSize));
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100212 }
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100213 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100214
215 case Primitive::kPrimLong: {
216 uint32_t index = gp_index_;
217 gp_index_ += 2;
218 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
219 return Location::RegisterLocation(ArmManagedRegister::FromRegisterPair(
220 calling_convention.GetRegisterPairAt(index)));
221 } else if (index + 1 == calling_convention.GetNumberOfRegisters()) {
222 return Location::QuickParameter(index);
223 } else {
224 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(index, kArmWordSize));
225 }
226 }
227
228 case Primitive::kPrimDouble:
229 case Primitive::kPrimFloat:
230 LOG(FATAL) << "Unimplemented parameter type " << type;
231 break;
232
233 case Primitive::kPrimVoid:
234 LOG(FATAL) << "Unexpected parameter type " << type;
235 break;
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100236 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100237 return Location();
238}
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100239
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100240void CodeGeneratorARM::Move32(Location destination, Location source) {
241 if (source.Equals(destination)) {
242 return;
243 }
244 if (destination.IsRegister()) {
245 if (source.IsRegister()) {
246 __ Mov(destination.AsArm().AsCoreRegister(), source.AsArm().AsCoreRegister());
247 } else {
248 __ ldr(destination.AsArm().AsCoreRegister(), Address(SP, source.GetStackIndex()));
249 }
250 } else {
251 DCHECK(destination.IsStackSlot());
252 if (source.IsRegister()) {
253 __ str(source.AsArm().AsCoreRegister(), Address(SP, destination.GetStackIndex()));
254 } else {
255 __ ldr(R0, Address(SP, source.GetStackIndex()));
256 __ str(R0, Address(SP, destination.GetStackIndex()));
257 }
258 }
259}
260
261void CodeGeneratorARM::Move64(Location destination, Location source) {
262 if (source.Equals(destination)) {
263 return;
264 }
265 if (destination.IsRegister()) {
266 if (source.IsRegister()) {
267 __ Mov(destination.AsArm().AsRegisterPairLow(), source.AsArm().AsRegisterPairLow());
268 __ Mov(destination.AsArm().AsRegisterPairHigh(), source.AsArm().AsRegisterPairHigh());
269 } else if (source.IsQuickParameter()) {
270 uint32_t argument_index = source.GetQuickParameterIndex();
271 InvokeDexCallingConvention calling_convention;
272 __ Mov(destination.AsArm().AsRegisterPairLow(),
273 calling_convention.GetRegisterAt(argument_index));
274 __ ldr(destination.AsArm().AsRegisterPairHigh(),
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100275 Address(SP, calling_convention.GetStackOffsetOf(argument_index + 1, kArmWordSize) + GetFrameSize()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100276 } else {
277 DCHECK(source.IsDoubleStackSlot());
278 if (destination.AsArm().AsRegisterPair() == R1_R2) {
279 __ ldr(R1, Address(SP, source.GetStackIndex()));
280 __ ldr(R2, Address(SP, source.GetHighStackIndex(kArmWordSize)));
281 } else {
282 __ LoadFromOffset(kLoadWordPair, destination.AsArm().AsRegisterPairLow(),
283 SP, source.GetStackIndex());
284 }
285 }
286 } else if (destination.IsQuickParameter()) {
287 InvokeDexCallingConvention calling_convention;
288 uint32_t argument_index = destination.GetQuickParameterIndex();
289 if (source.IsRegister()) {
290 __ Mov(calling_convention.GetRegisterAt(argument_index), source.AsArm().AsRegisterPairLow());
291 __ str(source.AsArm().AsRegisterPairHigh(),
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100292 Address(SP, calling_convention.GetStackOffsetOf(argument_index + 1, kArmWordSize)));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100293 } else {
294 DCHECK(source.IsDoubleStackSlot());
295 __ ldr(calling_convention.GetRegisterAt(argument_index), Address(SP, source.GetStackIndex()));
296 __ ldr(R0, Address(SP, source.GetHighStackIndex(kArmWordSize)));
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100297 __ str(R0, Address(SP, calling_convention.GetStackOffsetOf(argument_index + 1, kArmWordSize)));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100298 }
299 } else {
300 DCHECK(destination.IsDoubleStackSlot());
301 if (source.IsRegister()) {
302 if (source.AsArm().AsRegisterPair() == R1_R2) {
303 __ str(R1, Address(SP, destination.GetStackIndex()));
304 __ str(R2, Address(SP, destination.GetHighStackIndex(kArmWordSize)));
305 } else {
306 __ StoreToOffset(kStoreWordPair, source.AsArm().AsRegisterPairLow(),
307 SP, destination.GetStackIndex());
308 }
309 } else if (source.IsQuickParameter()) {
310 InvokeDexCallingConvention calling_convention;
311 uint32_t argument_index = source.GetQuickParameterIndex();
312 __ str(calling_convention.GetRegisterAt(argument_index),
313 Address(SP, destination.GetStackIndex()));
314 __ ldr(R0,
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100315 Address(SP, calling_convention.GetStackOffsetOf(argument_index + 1, kArmWordSize) + GetFrameSize()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100316 __ str(R0, Address(SP, destination.GetHighStackIndex(kArmWordSize)));
317 } else {
318 DCHECK(source.IsDoubleStackSlot());
319 __ ldr(R0, Address(SP, source.GetStackIndex()));
320 __ str(R0, Address(SP, destination.GetStackIndex()));
321 __ ldr(R0, Address(SP, source.GetHighStackIndex(kArmWordSize)));
322 __ str(R0, Address(SP, destination.GetHighStackIndex(kArmWordSize)));
323 }
324 }
325}
326
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100327void CodeGeneratorARM::Move(HInstruction* instruction, Location location, HInstruction* move_for) {
328 if (instruction->AsIntConstant() != nullptr) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100329 int32_t value = instruction->AsIntConstant()->GetValue();
330 if (location.IsRegister()) {
331 __ LoadImmediate(location.AsArm().AsCoreRegister(), value);
332 } else {
333 __ LoadImmediate(R0, value);
334 __ str(R0, Address(SP, location.GetStackIndex()));
335 }
336 } else if (instruction->AsLongConstant() != nullptr) {
337 int64_t value = instruction->AsLongConstant()->GetValue();
338 if (location.IsRegister()) {
339 __ LoadImmediate(location.AsArm().AsRegisterPairLow(), Low32Bits(value));
340 __ LoadImmediate(location.AsArm().AsRegisterPairHigh(), High32Bits(value));
341 } else {
342 __ LoadImmediate(R0, Low32Bits(value));
343 __ str(R0, Address(SP, location.GetStackIndex()));
344 __ LoadImmediate(R0, High32Bits(value));
345 __ str(R0, Address(SP, location.GetHighStackIndex(kArmWordSize)));
346 }
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100347 } else if (instruction->AsLoadLocal() != nullptr) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100348 uint32_t stack_slot = GetStackSlot(instruction->AsLoadLocal()->GetLocal());
349 switch (instruction->GetType()) {
350 case Primitive::kPrimBoolean:
351 case Primitive::kPrimByte:
352 case Primitive::kPrimChar:
353 case Primitive::kPrimShort:
354 case Primitive::kPrimInt:
355 case Primitive::kPrimNot:
356 Move32(location, Location::StackSlot(stack_slot));
357 break;
358
359 case Primitive::kPrimLong:
360 Move64(location, Location::DoubleStackSlot(stack_slot));
361 break;
362
363 default:
364 LOG(FATAL) << "Unimplemented type " << instruction->GetType();
365 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000366 } else {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100367 // This can currently only happen when the instruction that requests the move
368 // is the next to be compiled.
369 DCHECK_EQ(instruction->GetNext(), move_for);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100370 switch (instruction->GetType()) {
371 case Primitive::kPrimBoolean:
372 case Primitive::kPrimByte:
373 case Primitive::kPrimChar:
374 case Primitive::kPrimShort:
375 case Primitive::kPrimNot:
376 case Primitive::kPrimInt:
377 Move32(location, instruction->GetLocations()->Out());
378 break;
379
380 case Primitive::kPrimLong:
381 Move64(location, instruction->GetLocations()->Out());
382 break;
383
384 default:
385 LOG(FATAL) << "Unimplemented type " << instruction->GetType();
386 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000387 }
388}
389
390void LocationsBuilderARM::VisitGoto(HGoto* got) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000391 got->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000392}
393
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000394void InstructionCodeGeneratorARM::VisitGoto(HGoto* got) {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000395 HBasicBlock* successor = got->GetSuccessor();
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000396 if (GetGraph()->GetExitBlock() == successor) {
397 codegen_->GenerateFrameExit();
398 } else if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
399 __ b(codegen_->GetLabelOf(successor));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000400 }
401}
402
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000403void LocationsBuilderARM::VisitExit(HExit* exit) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000404 exit->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000405}
406
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000407void InstructionCodeGeneratorARM::VisitExit(HExit* exit) {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000408 if (kIsDebugBuild) {
409 __ Comment("Unreachable");
410 __ bkpt(0);
411 }
412}
413
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000414void LocationsBuilderARM::VisitIf(HIf* if_instr) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000415 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100416 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000417 if_instr->SetLocations(locations);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000418}
419
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000420void InstructionCodeGeneratorARM::VisitIf(HIf* if_instr) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000421 // TODO: Generate the input as a condition, instead of materializing in a register.
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100422 __ cmp(if_instr->GetLocations()->InAt(0).AsArm().AsCoreRegister(), ShifterOperand(0));
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000423 __ b(codegen_->GetLabelOf(if_instr->IfFalseSuccessor()), EQ);
424 if (!codegen_->GoesToNextBlock(if_instr->GetBlock(), if_instr->IfTrueSuccessor())) {
425 __ b(codegen_->GetLabelOf(if_instr->IfTrueSuccessor()));
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000426 }
427}
428
429void LocationsBuilderARM::VisitEqual(HEqual* equal) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000430 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(equal);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100431 locations->SetInAt(0, Location::RequiresRegister());
432 locations->SetInAt(1, Location::RequiresRegister());
433 locations->SetOut(Location::RequiresRegister());
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000434 equal->SetLocations(locations);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000435}
436
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000437void InstructionCodeGeneratorARM::VisitEqual(HEqual* equal) {
438 LocationSummary* locations = equal->GetLocations();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100439 __ teq(locations->InAt(0).AsArm().AsCoreRegister(),
440 ShifterOperand(locations->InAt(1).AsArm().AsCoreRegister()));
441 __ mov(locations->Out().AsArm().AsCoreRegister(), ShifterOperand(1), EQ);
442 __ mov(locations->Out().AsArm().AsCoreRegister(), ShifterOperand(0), NE);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000443}
444
445void LocationsBuilderARM::VisitLocal(HLocal* local) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000446 local->SetLocations(nullptr);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000447}
448
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000449void InstructionCodeGeneratorARM::VisitLocal(HLocal* local) {
450 DCHECK_EQ(local->GetBlock(), GetGraph()->GetEntryBlock());
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000451}
452
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000453void LocationsBuilderARM::VisitLoadLocal(HLoadLocal* load) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100454 load->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000455}
456
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000457void InstructionCodeGeneratorARM::VisitLoadLocal(HLoadLocal* load) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100458 // Nothing to do, this is driven by the code generator.
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000459}
460
461void LocationsBuilderARM::VisitStoreLocal(HStoreLocal* store) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000462 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(store);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100463 switch (store->InputAt(1)->GetType()) {
464 case Primitive::kPrimBoolean:
465 case Primitive::kPrimByte:
466 case Primitive::kPrimChar:
467 case Primitive::kPrimShort:
468 case Primitive::kPrimInt:
469 case Primitive::kPrimNot:
470 locations->SetInAt(1, Location::StackSlot(codegen_->GetStackSlot(store->GetLocal())));
471 break;
472
473 case Primitive::kPrimLong:
474 locations->SetInAt(1, Location::DoubleStackSlot(codegen_->GetStackSlot(store->GetLocal())));
475 break;
476
477 default:
478 LOG(FATAL) << "Unimplemented local type " << store->InputAt(1)->GetType();
479 }
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000480 store->SetLocations(locations);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000481}
482
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000483void InstructionCodeGeneratorARM::VisitStoreLocal(HStoreLocal* store) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000484}
485
486void LocationsBuilderARM::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000487 constant->SetLocations(nullptr);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000488}
489
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000490void InstructionCodeGeneratorARM::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000491 // Will be generated at use site.
492}
493
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100494void LocationsBuilderARM::VisitLongConstant(HLongConstant* constant) {
495 constant->SetLocations(nullptr);
496}
497
498void InstructionCodeGeneratorARM::VisitLongConstant(HLongConstant* constant) {
499 // Will be generated at use site.
500}
501
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000502void LocationsBuilderARM::VisitReturnVoid(HReturnVoid* ret) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000503 ret->SetLocations(nullptr);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000504}
505
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000506void InstructionCodeGeneratorARM::VisitReturnVoid(HReturnVoid* ret) {
507 codegen_->GenerateFrameExit();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000508}
509
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000510void LocationsBuilderARM::VisitReturn(HReturn* ret) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000511 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(ret);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100512 switch (ret->InputAt(0)->GetType()) {
513 case Primitive::kPrimBoolean:
514 case Primitive::kPrimByte:
515 case Primitive::kPrimChar:
516 case Primitive::kPrimShort:
517 case Primitive::kPrimInt:
518 case Primitive::kPrimNot:
519 locations->SetInAt(0, ArmCoreLocation(R0));
520 break;
521
522 case Primitive::kPrimLong:
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100523 locations->SetInAt(
524 0, Location::RegisterLocation(ArmManagedRegister::FromRegisterPair(R0_R1)));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100525 break;
526
527 default:
528 LOG(FATAL) << "Unimplemented return type " << ret->InputAt(0)->GetType();
529 }
530
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000531 ret->SetLocations(locations);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000532}
533
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000534void InstructionCodeGeneratorARM::VisitReturn(HReturn* ret) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100535 if (kIsDebugBuild) {
536 switch (ret->InputAt(0)->GetType()) {
537 case Primitive::kPrimBoolean:
538 case Primitive::kPrimByte:
539 case Primitive::kPrimChar:
540 case Primitive::kPrimShort:
541 case Primitive::kPrimInt:
542 case Primitive::kPrimNot:
543 DCHECK_EQ(ret->GetLocations()->InAt(0).AsArm().AsCoreRegister(), R0);
544 break;
545
546 case Primitive::kPrimLong:
547 DCHECK_EQ(ret->GetLocations()->InAt(0).AsArm().AsRegisterPair(), R0_R1);
548 break;
549
550 default:
551 LOG(FATAL) << "Unimplemented return type " << ret->InputAt(0)->GetType();
552 }
553 }
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000554 codegen_->GenerateFrameExit();
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000555}
556
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000557void LocationsBuilderARM::VisitInvokeStatic(HInvokeStatic* invoke) {
558 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(invoke);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100559 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100560
561 InvokeDexCallingConventionVisitor calling_convention_visitor;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100562 for (size_t i = 0; i < invoke->InputCount(); i++) {
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100563 HInstruction* input = invoke->InputAt(i);
564 locations->SetInAt(i, calling_convention_visitor.GetNextLocation(input->GetType()));
565 }
566
567 switch (invoke->GetType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100568 case Primitive::kPrimBoolean:
569 case Primitive::kPrimByte:
570 case Primitive::kPrimChar:
571 case Primitive::kPrimShort:
572 case Primitive::kPrimInt:
573 case Primitive::kPrimNot:
574 locations->SetOut(ArmCoreLocation(R0));
575 break;
576
577 case Primitive::kPrimLong:
578 locations->SetOut(Location::RegisterLocation(ArmManagedRegister::FromRegisterPair(R0_R1)));
579 break;
580
581 case Primitive::kPrimVoid:
582 break;
583
584 case Primitive::kPrimDouble:
585 case Primitive::kPrimFloat:
586 LOG(FATAL) << "Unimplemented return type " << invoke->GetType();
587 break;
588 }
589
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000590 invoke->SetLocations(locations);
591}
592
593void InstructionCodeGeneratorARM::LoadCurrentMethod(Register reg) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100594 __ ldr(reg, Address(SP, kCurrentMethodStackOffset));
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000595}
596
597void InstructionCodeGeneratorARM::VisitInvokeStatic(HInvokeStatic* invoke) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100598 Register temp = invoke->GetLocations()->GetTemp(0).AsArm().AsCoreRegister();
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000599 size_t index_in_cache = mirror::Array::DataOffset(sizeof(mirror::Object*)).Int32Value() +
Nicolas Geoffray707c8092014-04-04 10:50:14 +0100600 invoke->GetIndexInDexCache() * kArmWordSize;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000601
602 // TODO: Implement all kinds of calls:
603 // 1) boot -> boot
604 // 2) app -> boot
605 // 3) app -> app
606 //
607 // Currently we implement the app -> app logic, which looks up in the resolve cache.
608
609 // temp = method;
610 LoadCurrentMethod(temp);
611 // temp = temp->dex_cache_resolved_methods_;
612 __ ldr(temp, Address(temp, mirror::ArtMethod::DexCacheResolvedMethodsOffset().Int32Value()));
613 // temp = temp[index_in_cache]
614 __ ldr(temp, Address(temp, index_in_cache));
615 // LR = temp[offset_of_quick_compiled_code]
616 __ ldr(LR, Address(temp,
617 mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset().Int32Value()));
618 // LR()
619 __ blx(LR);
620
621 codegen_->RecordPcInfo(invoke->GetDexPc());
622}
623
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +0000624void LocationsBuilderARM::VisitAdd(HAdd* add) {
625 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(add);
626 switch (add->GetResultType()) {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100627 case Primitive::kPrimInt:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100628 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100629 locations->SetInAt(0, Location::RequiresRegister());
630 locations->SetInAt(1, Location::RequiresRegister());
631 locations->SetOut(Location::RequiresRegister());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100632 break;
633 }
634
635 case Primitive::kPrimBoolean:
636 case Primitive::kPrimByte:
637 case Primitive::kPrimChar:
638 case Primitive::kPrimShort:
639 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
640 break;
641
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +0000642 default:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100643 LOG(FATAL) << "Unimplemented add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +0000644 }
645 add->SetLocations(locations);
646}
647
648void InstructionCodeGeneratorARM::VisitAdd(HAdd* add) {
649 LocationSummary* locations = add->GetLocations();
650 switch (add->GetResultType()) {
651 case Primitive::kPrimInt:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100652 __ add(locations->Out().AsArm().AsCoreRegister(),
653 locations->InAt(0).AsArm().AsCoreRegister(),
654 ShifterOperand(locations->InAt(1).AsArm().AsCoreRegister()));
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +0000655 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100656
657 case Primitive::kPrimLong:
658 __ adds(locations->Out().AsArm().AsRegisterPairLow(),
659 locations->InAt(0).AsArm().AsRegisterPairLow(),
660 ShifterOperand(locations->InAt(1).AsArm().AsRegisterPairLow()));
661 __ adc(locations->Out().AsArm().AsRegisterPairHigh(),
662 locations->InAt(0).AsArm().AsRegisterPairHigh(),
663 ShifterOperand(locations->InAt(1).AsArm().AsRegisterPairHigh()));
664 break;
665
666 case Primitive::kPrimBoolean:
667 case Primitive::kPrimByte:
668 case Primitive::kPrimChar:
669 case Primitive::kPrimShort:
670 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
671 break;
672
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +0000673 default:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100674 LOG(FATAL) << "Unimplemented add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +0000675 }
676}
677
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100678void LocationsBuilderARM::VisitSub(HSub* sub) {
679 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(sub);
680 switch (sub->GetResultType()) {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100681 case Primitive::kPrimInt:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100682 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100683 locations->SetInAt(0, Location::RequiresRegister());
684 locations->SetInAt(1, Location::RequiresRegister());
685 locations->SetOut(Location::RequiresRegister());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100686 break;
687 }
688
689 case Primitive::kPrimBoolean:
690 case Primitive::kPrimByte:
691 case Primitive::kPrimChar:
692 case Primitive::kPrimShort:
693 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
694 break;
695
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100696 default:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100697 LOG(FATAL) << "Unimplemented sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100698 }
699 sub->SetLocations(locations);
700}
701
702void InstructionCodeGeneratorARM::VisitSub(HSub* sub) {
703 LocationSummary* locations = sub->GetLocations();
704 switch (sub->GetResultType()) {
705 case Primitive::kPrimInt:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100706 __ sub(locations->Out().AsArm().AsCoreRegister(),
707 locations->InAt(0).AsArm().AsCoreRegister(),
708 ShifterOperand(locations->InAt(1).AsArm().AsCoreRegister()));
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100709 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100710
711 case Primitive::kPrimLong:
712 __ subs(locations->Out().AsArm().AsRegisterPairLow(),
713 locations->InAt(0).AsArm().AsRegisterPairLow(),
714 ShifterOperand(locations->InAt(1).AsArm().AsRegisterPairLow()));
715 __ sbc(locations->Out().AsArm().AsRegisterPairHigh(),
716 locations->InAt(0).AsArm().AsRegisterPairHigh(),
717 ShifterOperand(locations->InAt(1).AsArm().AsRegisterPairHigh()));
718 break;
719
720 case Primitive::kPrimBoolean:
721 case Primitive::kPrimByte:
722 case Primitive::kPrimChar:
723 case Primitive::kPrimShort:
724 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
725 break;
726
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100727 default:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100728 LOG(FATAL) << "Unimplemented sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100729 }
730}
731
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +0100732static constexpr Register kRuntimeParameterCoreRegisters[] = { R0, R1 };
733static constexpr size_t kRuntimeParameterCoreRegistersLength =
734 arraysize(kRuntimeParameterCoreRegisters);
735
736class InvokeRuntimeCallingConvention : public CallingConvention<Register> {
737 public:
738 InvokeRuntimeCallingConvention()
739 : CallingConvention(kRuntimeParameterCoreRegisters,
740 kRuntimeParameterCoreRegistersLength) {}
741
742 private:
743 DISALLOW_COPY_AND_ASSIGN(InvokeRuntimeCallingConvention);
744};
745
746void LocationsBuilderARM::VisitNewInstance(HNewInstance* instruction) {
747 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100748 InvokeRuntimeCallingConvention calling_convention;
749 locations->AddTemp(ArmCoreLocation(calling_convention.GetRegisterAt(0)));
750 locations->AddTemp(ArmCoreLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100751 locations->SetOut(ArmCoreLocation(R0));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +0100752 instruction->SetLocations(locations);
753}
754
755void InstructionCodeGeneratorARM::VisitNewInstance(HNewInstance* instruction) {
756 InvokeRuntimeCallingConvention calling_convention;
757 LoadCurrentMethod(calling_convention.GetRegisterAt(1));
758 __ LoadImmediate(calling_convention.GetRegisterAt(0), instruction->GetTypeIndex());
759
Nicolas Geoffray707c8092014-04-04 10:50:14 +0100760 int32_t offset = QUICK_ENTRYPOINT_OFFSET(kArmWordSize, pAllocObjectWithAccessCheck).Int32Value();
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +0100761 __ ldr(LR, Address(TR, offset));
762 __ blx(LR);
763
764 codegen_->RecordPcInfo(instruction->GetDexPc());
765}
766
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100767void LocationsBuilderARM::VisitParameterValue(HParameterValue* instruction) {
768 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100769 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
770 if (location.IsStackSlot()) {
771 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
772 } else if (location.IsDoubleStackSlot()) {
773 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100774 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100775 locations->SetOut(location);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100776 instruction->SetLocations(locations);
777}
778
779void InstructionCodeGeneratorARM::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100780 // Nothing to do, the parameter is already at its location.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100781}
782
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +0100783void LocationsBuilderARM::VisitNot(HNot* instruction) {
784 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100785 locations->SetInAt(0, Location::RequiresRegister());
786 locations->SetOut(Location::RequiresRegister());
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +0100787 instruction->SetLocations(locations);
788}
789
790void InstructionCodeGeneratorARM::VisitNot(HNot* instruction) {
791 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100792 __ eor(locations->Out().AsArm().AsCoreRegister(),
793 locations->InAt(0).AsArm().AsCoreRegister(), ShifterOperand(1));
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +0100794}
795
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100796void LocationsBuilderARM::VisitPhi(HPhi* instruction) {
797 LOG(FATAL) << "Unimplemented";
798}
799
800void InstructionCodeGeneratorARM::VisitPhi(HPhi* instruction) {
801 LOG(FATAL) << "Unimplemented";
802}
803
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +0100804void LocationsBuilderARM::VisitParallelMove(HParallelMove* instruction) {
805 LOG(FATAL) << "Unimplemented";
806}
807
808void InstructionCodeGeneratorARM::VisitParallelMove(HParallelMove* instruction) {
809 LOG(FATAL) << "Unimplemented";
810}
811
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000812} // namespace arm
813} // namespace art