blob: 09d6f7b36a3344bf77e4612e6feafe7b828b0ee3 [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"
20
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +000021#include "mirror/array.h"
22#include "mirror/art_method.h"
23
Nicolas Geoffray787c3072014-03-17 10:20:19 +000024#define __ reinterpret_cast<ArmAssembler*>(GetAssembler())->
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000025
26namespace art {
27namespace arm {
28
Nicolas Geoffray4a34a422014-04-03 10:38:37 +010029static constexpr int kNumberOfPushedRegistersAtEntry = 1;
30static constexpr int kCurrentMethodStackOffset = 0;
31
32InstructionCodeGeneratorARM::InstructionCodeGeneratorARM(HGraph* graph, CodeGeneratorARM* codegen)
33 : HGraphVisitor(graph),
34 assembler_(codegen->GetAssembler()),
35 codegen_(codegen) {}
36
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000037void CodeGeneratorARM::GenerateFrameEntry() {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +000038 core_spill_mask_ |= (1 << LR);
Nicolas Geoffray4a34a422014-04-03 10:38:37 +010039 __ PushList((1 << LR));
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +000040
Nicolas Geoffray4a34a422014-04-03 10:38:37 +010041 // Add the current ART method to the frame size and the return PC.
42 SetFrameSize(RoundUp(GetFrameSize() + 2 * kWordSize, kStackAlignment));
43 // The retrn PC has already been pushed on the stack.
44 __ AddConstant(SP, -(GetFrameSize() - kNumberOfPushedRegistersAtEntry * kWordSize));
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +000045 __ str(R0, Address(SP, 0));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000046}
47
48void CodeGeneratorARM::GenerateFrameExit() {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +010049 __ AddConstant(SP, GetFrameSize() - kNumberOfPushedRegistersAtEntry * kWordSize);
50 __ PopList((1 << PC));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000051}
52
53void CodeGeneratorARM::Bind(Label* label) {
54 __ Bind(label);
55}
56
Nicolas Geoffray4a34a422014-04-03 10:38:37 +010057int32_t CodeGeneratorARM::GetStackSlot(HLocal* local) const {
58 return (GetGraph()->GetMaximumNumberOfOutVRegs() + local->GetRegNumber()) * kWordSize;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +000059}
60
Nicolas Geoffray4a34a422014-04-03 10:38:37 +010061void CodeGeneratorARM::Move(HInstruction* instruction, Location location, HInstruction* move_for) {
62 if (instruction->AsIntConstant() != nullptr) {
63 __ LoadImmediate(location.reg<Register>(), instruction->AsIntConstant()->GetValue());
64 } else if (instruction->AsLoadLocal() != nullptr) {
65 __ LoadFromOffset(kLoadWord, location.reg<Register>(),
66 SP, GetStackSlot(instruction->AsLoadLocal()->GetLocal()));
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +000067 } else {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +010068 // This can currently only happen when the instruction that requests the move
69 // is the next to be compiled.
70 DCHECK_EQ(instruction->GetNext(), move_for);
71 __ mov(location.reg<Register>(),
72 ShifterOperand(instruction->GetLocations()->Out().reg<Register>()));
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +000073 }
74}
75
76void LocationsBuilderARM::VisitGoto(HGoto* got) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +000077 got->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +000078}
79
Nicolas Geoffray787c3072014-03-17 10:20:19 +000080void InstructionCodeGeneratorARM::VisitGoto(HGoto* got) {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000081 HBasicBlock* successor = got->GetSuccessor();
Nicolas Geoffray787c3072014-03-17 10:20:19 +000082 if (GetGraph()->GetExitBlock() == successor) {
83 codegen_->GenerateFrameExit();
84 } else if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
85 __ b(codegen_->GetLabelOf(successor));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000086 }
87}
88
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +000089void LocationsBuilderARM::VisitExit(HExit* exit) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +000090 exit->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +000091}
92
Nicolas Geoffray787c3072014-03-17 10:20:19 +000093void InstructionCodeGeneratorARM::VisitExit(HExit* exit) {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000094 if (kIsDebugBuild) {
95 __ Comment("Unreachable");
96 __ bkpt(0);
97 }
98}
99
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000100void LocationsBuilderARM::VisitIf(HIf* if_instr) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000101 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000102 locations->SetInAt(0, Location(R0));
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000103 if_instr->SetLocations(locations);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000104}
105
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000106void InstructionCodeGeneratorARM::VisitIf(HIf* if_instr) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000107 // TODO: Generate the input as a condition, instead of materializing in a register.
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000108 __ cmp(if_instr->GetLocations()->InAt(0).reg<Register>(), ShifterOperand(0));
109 __ b(codegen_->GetLabelOf(if_instr->IfFalseSuccessor()), EQ);
110 if (!codegen_->GoesToNextBlock(if_instr->GetBlock(), if_instr->IfTrueSuccessor())) {
111 __ b(codegen_->GetLabelOf(if_instr->IfTrueSuccessor()));
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000112 }
113}
114
115void LocationsBuilderARM::VisitEqual(HEqual* equal) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000116 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(equal);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000117 locations->SetInAt(0, Location(R0));
118 locations->SetInAt(1, Location(R1));
119 locations->SetOut(Location(R0));
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000120 equal->SetLocations(locations);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000121}
122
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000123void InstructionCodeGeneratorARM::VisitEqual(HEqual* equal) {
124 LocationSummary* locations = equal->GetLocations();
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000125 __ teq(locations->InAt(0).reg<Register>(),
126 ShifterOperand(locations->InAt(1).reg<Register>()));
127 __ mov(locations->Out().reg<Register>(), ShifterOperand(1), EQ);
128 __ mov(locations->Out().reg<Register>(), ShifterOperand(0), NE);
129}
130
131void LocationsBuilderARM::VisitLocal(HLocal* local) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000132 local->SetLocations(nullptr);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000133}
134
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000135void InstructionCodeGeneratorARM::VisitLocal(HLocal* local) {
136 DCHECK_EQ(local->GetBlock(), GetGraph()->GetEntryBlock());
137 codegen_->SetFrameSize(codegen_->GetFrameSize() + kWordSize);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000138}
139
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000140void LocationsBuilderARM::VisitLoadLocal(HLoadLocal* load) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100141 load->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000142}
143
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000144void InstructionCodeGeneratorARM::VisitLoadLocal(HLoadLocal* load) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100145 // Nothing to do, this is driven by the code generator.
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000146}
147
148void LocationsBuilderARM::VisitStoreLocal(HStoreLocal* store) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000149 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(store);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000150 locations->SetInAt(1, Location(R0));
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000151 store->SetLocations(locations);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000152}
153
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000154void InstructionCodeGeneratorARM::VisitStoreLocal(HStoreLocal* store) {
155 LocationSummary* locations = store->GetLocations();
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000156 __ StoreToOffset(kStoreWord, locations->InAt(1).reg<Register>(),
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100157 SP, codegen_->GetStackSlot(store->GetLocal()));
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000158}
159
160void LocationsBuilderARM::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000161 constant->SetLocations(nullptr);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000162}
163
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000164void InstructionCodeGeneratorARM::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000165 // Will be generated at use site.
166}
167
168void LocationsBuilderARM::VisitReturnVoid(HReturnVoid* ret) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000169 ret->SetLocations(nullptr);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000170}
171
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000172void InstructionCodeGeneratorARM::VisitReturnVoid(HReturnVoid* ret) {
173 codegen_->GenerateFrameExit();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000174}
175
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000176void LocationsBuilderARM::VisitReturn(HReturn* ret) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000177 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(ret);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000178 locations->SetInAt(0, Location(R0));
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000179 ret->SetLocations(locations);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000180}
181
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000182void InstructionCodeGeneratorARM::VisitReturn(HReturn* ret) {
183 DCHECK_EQ(ret->GetLocations()->InAt(0).reg<Register>(), R0);
184 codegen_->GenerateFrameExit();
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000185}
186
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100187static constexpr Register kParameterCoreRegisters[] = { R1, R2, R3 };
188static constexpr int kParameterCoreRegistersLength = arraysize(kParameterCoreRegisters);
189
190class InvokeStaticCallingConvention : public CallingConvention<Register> {
191 public:
192 InvokeStaticCallingConvention()
193 : CallingConvention(kParameterCoreRegisters, kParameterCoreRegistersLength) {}
194
195 private:
196 DISALLOW_COPY_AND_ASSIGN(InvokeStaticCallingConvention);
197};
198
199void LocationsBuilderARM::VisitPushArgument(HPushArgument* argument) {
200 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(argument);
201 InvokeStaticCallingConvention calling_convention;
202 if (argument->GetArgumentIndex() < calling_convention.GetNumberOfRegisters()) {
203 Location location = Location(calling_convention.GetRegisterAt(argument->GetArgumentIndex()));
204 locations->SetInAt(0, location);
205 locations->SetOut(location);
206 } else {
207 locations->SetInAt(0, Location(R0));
208 }
209 argument->SetLocations(locations);
210}
211
212void InstructionCodeGeneratorARM::VisitPushArgument(HPushArgument* argument) {
213 uint8_t argument_index = argument->GetArgumentIndex();
214 InvokeStaticCallingConvention calling_convention;
215 size_t parameter_registers = calling_convention.GetNumberOfRegisters();
216 LocationSummary* locations = argument->GetLocations();
217 if (argument_index >= parameter_registers) {
218 uint8_t offset = calling_convention.GetStackOffsetOf(argument_index);
219 __ StoreToOffset(kStoreWord, locations->InAt(0).reg<Register>(), SP, offset);
220 } else {
221 DCHECK_EQ(locations->Out().reg<Register>(), locations->InAt(0).reg<Register>());
222 }
223}
224
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000225void LocationsBuilderARM::VisitInvokeStatic(HInvokeStatic* invoke) {
226 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(invoke);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000227 locations->AddTemp(Location(R0));
228 invoke->SetLocations(locations);
229}
230
231void InstructionCodeGeneratorARM::LoadCurrentMethod(Register reg) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100232 __ ldr(reg, Address(SP, kCurrentMethodStackOffset));
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000233}
234
235void InstructionCodeGeneratorARM::VisitInvokeStatic(HInvokeStatic* invoke) {
236 Register temp = invoke->GetLocations()->GetTemp(0).reg<Register>();
237 size_t index_in_cache = mirror::Array::DataOffset(sizeof(mirror::Object*)).Int32Value() +
238 invoke->GetIndexInDexCache() * kWordSize;
239
240 // TODO: Implement all kinds of calls:
241 // 1) boot -> boot
242 // 2) app -> boot
243 // 3) app -> app
244 //
245 // Currently we implement the app -> app logic, which looks up in the resolve cache.
246
247 // temp = method;
248 LoadCurrentMethod(temp);
249 // temp = temp->dex_cache_resolved_methods_;
250 __ ldr(temp, Address(temp, mirror::ArtMethod::DexCacheResolvedMethodsOffset().Int32Value()));
251 // temp = temp[index_in_cache]
252 __ ldr(temp, Address(temp, index_in_cache));
253 // LR = temp[offset_of_quick_compiled_code]
254 __ ldr(LR, Address(temp,
255 mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset().Int32Value()));
256 // LR()
257 __ blx(LR);
258
259 codegen_->RecordPcInfo(invoke->GetDexPc());
260}
261
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +0000262void LocationsBuilderARM::VisitAdd(HAdd* add) {
263 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(add);
264 switch (add->GetResultType()) {
265 case Primitive::kPrimInt: {
266 locations->SetInAt(0, Location(R0));
267 locations->SetInAt(1, Location(R1));
268 locations->SetOut(Location(R0));
269 break;
270 }
271 default:
272 LOG(FATAL) << "Unimplemented";
273 }
274 add->SetLocations(locations);
275}
276
277void InstructionCodeGeneratorARM::VisitAdd(HAdd* add) {
278 LocationSummary* locations = add->GetLocations();
279 switch (add->GetResultType()) {
280 case Primitive::kPrimInt:
281 __ add(locations->Out().reg<Register>(),
282 locations->InAt(0).reg<Register>(),
283 ShifterOperand(locations->InAt(1).reg<Register>()));
284 break;
285 default:
286 LOG(FATAL) << "Unimplemented";
287 }
288}
289
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000290} // namespace arm
291} // namespace art