blob: 68c997bf5f356cc26ee5b26bb1cc6a4741b8242a [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
29void CodeGeneratorARM::GenerateFrameEntry() {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +000030 core_spill_mask_ |= (1 << LR);
31 // We're currently always using FP, which is callee-saved in Quick.
32 core_spill_mask_ |= (1 << FP);
33
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +000034 __ PushList((1 << FP) | (1 << LR));
35 __ mov(FP, ShifterOperand(SP));
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +000036
37 // Add the current ART method to the frame size, the return pc, and FP.
38 SetFrameSize(RoundUp(GetFrameSize() + 3 * kWordSize, kStackAlignment));
39 // PC and FP have already been pushed on the stack.
40 __ AddConstant(SP, -(GetFrameSize() - 2 * kWordSize));
41 __ str(R0, Address(SP, 0));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000042}
43
44void CodeGeneratorARM::GenerateFrameExit() {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +000045 __ mov(SP, ShifterOperand(FP));
46 __ PopList((1 << FP) | (1 << PC));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000047}
48
49void CodeGeneratorARM::Bind(Label* label) {
50 __ Bind(label);
51}
52
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +000053void CodeGeneratorARM::Push(HInstruction* instruction, Location location) {
54 __ Push(location.reg<Register>());
55}
56
57void CodeGeneratorARM::Move(HInstruction* instruction, Location location) {
58 HIntConstant* constant = instruction->AsIntConstant();
59 if (constant != nullptr) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +000060 __ LoadImmediate(location.reg<Register>(), constant->GetValue());
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +000061 } else {
62 __ Pop(location.reg<Register>());
63 }
64}
65
66void LocationsBuilderARM::VisitGoto(HGoto* got) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +000067 got->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +000068}
69
Nicolas Geoffray787c3072014-03-17 10:20:19 +000070void InstructionCodeGeneratorARM::VisitGoto(HGoto* got) {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000071 HBasicBlock* successor = got->GetSuccessor();
Nicolas Geoffray787c3072014-03-17 10:20:19 +000072 if (GetGraph()->GetExitBlock() == successor) {
73 codegen_->GenerateFrameExit();
74 } else if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
75 __ b(codegen_->GetLabelOf(successor));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000076 }
77}
78
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +000079void LocationsBuilderARM::VisitExit(HExit* exit) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +000080 exit->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +000081}
82
Nicolas Geoffray787c3072014-03-17 10:20:19 +000083void InstructionCodeGeneratorARM::VisitExit(HExit* exit) {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000084 if (kIsDebugBuild) {
85 __ Comment("Unreachable");
86 __ bkpt(0);
87 }
88}
89
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +000090void LocationsBuilderARM::VisitIf(HIf* if_instr) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +000091 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +000092 locations->SetInAt(0, Location(R0));
Nicolas Geoffray787c3072014-03-17 10:20:19 +000093 if_instr->SetLocations(locations);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +000094}
95
Nicolas Geoffray787c3072014-03-17 10:20:19 +000096void InstructionCodeGeneratorARM::VisitIf(HIf* if_instr) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +000097 // TODO: Generate the input as a condition, instead of materializing in a register.
Nicolas Geoffray787c3072014-03-17 10:20:19 +000098 __ cmp(if_instr->GetLocations()->InAt(0).reg<Register>(), ShifterOperand(0));
99 __ b(codegen_->GetLabelOf(if_instr->IfFalseSuccessor()), EQ);
100 if (!codegen_->GoesToNextBlock(if_instr->GetBlock(), if_instr->IfTrueSuccessor())) {
101 __ b(codegen_->GetLabelOf(if_instr->IfTrueSuccessor()));
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000102 }
103}
104
105void LocationsBuilderARM::VisitEqual(HEqual* equal) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000106 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(equal);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000107 locations->SetInAt(0, Location(R0));
108 locations->SetInAt(1, Location(R1));
109 locations->SetOut(Location(R0));
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000110 equal->SetLocations(locations);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000111}
112
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000113void InstructionCodeGeneratorARM::VisitEqual(HEqual* equal) {
114 LocationSummary* locations = equal->GetLocations();
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000115 __ teq(locations->InAt(0).reg<Register>(),
116 ShifterOperand(locations->InAt(1).reg<Register>()));
117 __ mov(locations->Out().reg<Register>(), ShifterOperand(1), EQ);
118 __ mov(locations->Out().reg<Register>(), ShifterOperand(0), NE);
119}
120
121void LocationsBuilderARM::VisitLocal(HLocal* local) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000122 local->SetLocations(nullptr);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000123}
124
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000125void InstructionCodeGeneratorARM::VisitLocal(HLocal* local) {
126 DCHECK_EQ(local->GetBlock(), GetGraph()->GetEntryBlock());
127 codegen_->SetFrameSize(codegen_->GetFrameSize() + kWordSize);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000128}
129
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000130void LocationsBuilderARM::VisitLoadLocal(HLoadLocal* load) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000131 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000132 locations->SetOut(Location(R0));
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000133 load->SetLocations(locations);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000134}
135
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000136static int32_t GetStackSlot(HLocal* local) {
137 // We are currently using FP to access locals, so the offset must be negative.
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000138 return (local->GetRegNumber() + 1) * -kWordSize;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000139}
140
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000141void InstructionCodeGeneratorARM::VisitLoadLocal(HLoadLocal* load) {
142 LocationSummary* locations = load->GetLocations();
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000143 __ LoadFromOffset(kLoadWord, locations->Out().reg<Register>(),
144 FP, GetStackSlot(load->GetLocal()));
145}
146
147void LocationsBuilderARM::VisitStoreLocal(HStoreLocal* store) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000148 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(store);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000149 locations->SetInAt(1, Location(R0));
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000150 store->SetLocations(locations);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000151}
152
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000153void InstructionCodeGeneratorARM::VisitStoreLocal(HStoreLocal* store) {
154 LocationSummary* locations = store->GetLocations();
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000155 __ StoreToOffset(kStoreWord, locations->InAt(1).reg<Register>(),
156 FP, GetStackSlot(store->GetLocal()));
157}
158
159void LocationsBuilderARM::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000160 constant->SetLocations(nullptr);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000161}
162
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000163void InstructionCodeGeneratorARM::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000164 // Will be generated at use site.
165}
166
167void LocationsBuilderARM::VisitReturnVoid(HReturnVoid* ret) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000168 ret->SetLocations(nullptr);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000169}
170
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000171void InstructionCodeGeneratorARM::VisitReturnVoid(HReturnVoid* ret) {
172 codegen_->GenerateFrameExit();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000173}
174
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000175void LocationsBuilderARM::VisitReturn(HReturn* ret) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000176 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(ret);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000177 locations->SetInAt(0, Location(R0));
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000178 ret->SetLocations(locations);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000179}
180
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000181void InstructionCodeGeneratorARM::VisitReturn(HReturn* ret) {
182 DCHECK_EQ(ret->GetLocations()->InAt(0).reg<Register>(), R0);
183 codegen_->GenerateFrameExit();
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000184}
185
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000186void LocationsBuilderARM::VisitInvokeStatic(HInvokeStatic* invoke) {
187 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(invoke);
188 CHECK_EQ(invoke->InputCount(), 0);
189 locations->AddTemp(Location(R0));
190 invoke->SetLocations(locations);
191}
192
193void InstructionCodeGeneratorARM::LoadCurrentMethod(Register reg) {
194 __ ldr(reg, Address(SP, 0));
195}
196
197void InstructionCodeGeneratorARM::VisitInvokeStatic(HInvokeStatic* invoke) {
198 Register temp = invoke->GetLocations()->GetTemp(0).reg<Register>();
199 size_t index_in_cache = mirror::Array::DataOffset(sizeof(mirror::Object*)).Int32Value() +
200 invoke->GetIndexInDexCache() * kWordSize;
201
202 // TODO: Implement all kinds of calls:
203 // 1) boot -> boot
204 // 2) app -> boot
205 // 3) app -> app
206 //
207 // Currently we implement the app -> app logic, which looks up in the resolve cache.
208
209 // temp = method;
210 LoadCurrentMethod(temp);
211 // temp = temp->dex_cache_resolved_methods_;
212 __ ldr(temp, Address(temp, mirror::ArtMethod::DexCacheResolvedMethodsOffset().Int32Value()));
213 // temp = temp[index_in_cache]
214 __ ldr(temp, Address(temp, index_in_cache));
215 // LR = temp[offset_of_quick_compiled_code]
216 __ ldr(LR, Address(temp,
217 mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset().Int32Value()));
218 // LR()
219 __ blx(LR);
220
221 codegen_->RecordPcInfo(invoke->GetDexPc());
222}
223
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +0000224void LocationsBuilderARM::VisitAdd(HAdd* add) {
225 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(add);
226 switch (add->GetResultType()) {
227 case Primitive::kPrimInt: {
228 locations->SetInAt(0, Location(R0));
229 locations->SetInAt(1, Location(R1));
230 locations->SetOut(Location(R0));
231 break;
232 }
233 default:
234 LOG(FATAL) << "Unimplemented";
235 }
236 add->SetLocations(locations);
237}
238
239void InstructionCodeGeneratorARM::VisitAdd(HAdd* add) {
240 LocationSummary* locations = add->GetLocations();
241 switch (add->GetResultType()) {
242 case Primitive::kPrimInt:
243 __ add(locations->Out().reg<Register>(),
244 locations->InAt(0).reg<Register>(),
245 ShifterOperand(locations->InAt(1).reg<Register>()));
246 break;
247 default:
248 LOG(FATAL) << "Unimplemented";
249 }
250}
251
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000252} // namespace arm
253} // namespace art