Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 1 | /* |
| 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 Geoffray | 8ccc3f5 | 2014-03-19 10:34:11 +0000 | [diff] [blame] | 21 | #include "mirror/array.h" |
| 22 | #include "mirror/art_method.h" |
| 23 | |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 24 | #define __ reinterpret_cast<ArmAssembler*>(GetAssembler())-> |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 25 | |
| 26 | namespace art { |
| 27 | namespace arm { |
| 28 | |
| 29 | void CodeGeneratorARM::GenerateFrameEntry() { |
Nicolas Geoffray | 8ccc3f5 | 2014-03-19 10:34:11 +0000 | [diff] [blame] | 30 | 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 Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 34 | __ PushList((1 << FP) | (1 << LR)); |
| 35 | __ mov(FP, ShifterOperand(SP)); |
Nicolas Geoffray | 8ccc3f5 | 2014-03-19 10:34:11 +0000 | [diff] [blame] | 36 | |
| 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 Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 42 | } |
| 43 | |
| 44 | void CodeGeneratorARM::GenerateFrameExit() { |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 45 | __ mov(SP, ShifterOperand(FP)); |
| 46 | __ PopList((1 << FP) | (1 << PC)); |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | void CodeGeneratorARM::Bind(Label* label) { |
| 50 | __ Bind(label); |
| 51 | } |
| 52 | |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 53 | void CodeGeneratorARM::Push(HInstruction* instruction, Location location) { |
| 54 | __ Push(location.reg<Register>()); |
| 55 | } |
| 56 | |
| 57 | void CodeGeneratorARM::Move(HInstruction* instruction, Location location) { |
| 58 | HIntConstant* constant = instruction->AsIntConstant(); |
| 59 | if (constant != nullptr) { |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 60 | __ LoadImmediate(location.reg<Register>(), constant->GetValue()); |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 61 | } else { |
| 62 | __ Pop(location.reg<Register>()); |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | void LocationsBuilderARM::VisitGoto(HGoto* got) { |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 67 | got->SetLocations(nullptr); |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 68 | } |
| 69 | |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 70 | void InstructionCodeGeneratorARM::VisitGoto(HGoto* got) { |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 71 | HBasicBlock* successor = got->GetSuccessor(); |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 72 | if (GetGraph()->GetExitBlock() == successor) { |
| 73 | codegen_->GenerateFrameExit(); |
| 74 | } else if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) { |
| 75 | __ b(codegen_->GetLabelOf(successor)); |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 76 | } |
| 77 | } |
| 78 | |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 79 | void LocationsBuilderARM::VisitExit(HExit* exit) { |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 80 | exit->SetLocations(nullptr); |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 81 | } |
| 82 | |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 83 | void InstructionCodeGeneratorARM::VisitExit(HExit* exit) { |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 84 | if (kIsDebugBuild) { |
| 85 | __ Comment("Unreachable"); |
| 86 | __ bkpt(0); |
| 87 | } |
| 88 | } |
| 89 | |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 90 | void LocationsBuilderARM::VisitIf(HIf* if_instr) { |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 91 | LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr); |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 92 | locations->SetInAt(0, Location(R0)); |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 93 | if_instr->SetLocations(locations); |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 94 | } |
| 95 | |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 96 | void InstructionCodeGeneratorARM::VisitIf(HIf* if_instr) { |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 97 | // TODO: Generate the input as a condition, instead of materializing in a register. |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 98 | __ 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 Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 102 | } |
| 103 | } |
| 104 | |
| 105 | void LocationsBuilderARM::VisitEqual(HEqual* equal) { |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 106 | LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(equal); |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 107 | locations->SetInAt(0, Location(R0)); |
| 108 | locations->SetInAt(1, Location(R1)); |
| 109 | locations->SetOut(Location(R0)); |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 110 | equal->SetLocations(locations); |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 111 | } |
| 112 | |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 113 | void InstructionCodeGeneratorARM::VisitEqual(HEqual* equal) { |
| 114 | LocationSummary* locations = equal->GetLocations(); |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 115 | __ 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 | |
| 121 | void LocationsBuilderARM::VisitLocal(HLocal* local) { |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 122 | local->SetLocations(nullptr); |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 123 | } |
| 124 | |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 125 | void InstructionCodeGeneratorARM::VisitLocal(HLocal* local) { |
| 126 | DCHECK_EQ(local->GetBlock(), GetGraph()->GetEntryBlock()); |
| 127 | codegen_->SetFrameSize(codegen_->GetFrameSize() + kWordSize); |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 128 | } |
| 129 | |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 130 | void LocationsBuilderARM::VisitLoadLocal(HLoadLocal* load) { |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 131 | LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load); |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 132 | locations->SetOut(Location(R0)); |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 133 | load->SetLocations(locations); |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 134 | } |
| 135 | |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 136 | static int32_t GetStackSlot(HLocal* local) { |
| 137 | // We are currently using FP to access locals, so the offset must be negative. |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 138 | return (local->GetRegNumber() + 1) * -kWordSize; |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 139 | } |
| 140 | |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 141 | void InstructionCodeGeneratorARM::VisitLoadLocal(HLoadLocal* load) { |
| 142 | LocationSummary* locations = load->GetLocations(); |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 143 | __ LoadFromOffset(kLoadWord, locations->Out().reg<Register>(), |
| 144 | FP, GetStackSlot(load->GetLocal())); |
| 145 | } |
| 146 | |
| 147 | void LocationsBuilderARM::VisitStoreLocal(HStoreLocal* store) { |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 148 | LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(store); |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 149 | locations->SetInAt(1, Location(R0)); |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 150 | store->SetLocations(locations); |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 151 | } |
| 152 | |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 153 | void InstructionCodeGeneratorARM::VisitStoreLocal(HStoreLocal* store) { |
| 154 | LocationSummary* locations = store->GetLocations(); |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 155 | __ StoreToOffset(kStoreWord, locations->InAt(1).reg<Register>(), |
| 156 | FP, GetStackSlot(store->GetLocal())); |
| 157 | } |
| 158 | |
| 159 | void LocationsBuilderARM::VisitIntConstant(HIntConstant* constant) { |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 160 | constant->SetLocations(nullptr); |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 161 | } |
| 162 | |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 163 | void InstructionCodeGeneratorARM::VisitIntConstant(HIntConstant* constant) { |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 164 | // Will be generated at use site. |
| 165 | } |
| 166 | |
| 167 | void LocationsBuilderARM::VisitReturnVoid(HReturnVoid* ret) { |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 168 | ret->SetLocations(nullptr); |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 169 | } |
| 170 | |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 171 | void InstructionCodeGeneratorARM::VisitReturnVoid(HReturnVoid* ret) { |
| 172 | codegen_->GenerateFrameExit(); |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 173 | } |
| 174 | |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 175 | void LocationsBuilderARM::VisitReturn(HReturn* ret) { |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 176 | LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(ret); |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 177 | locations->SetInAt(0, Location(R0)); |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 178 | ret->SetLocations(locations); |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 179 | } |
| 180 | |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 181 | void InstructionCodeGeneratorARM::VisitReturn(HReturn* ret) { |
| 182 | DCHECK_EQ(ret->GetLocations()->InAt(0).reg<Register>(), R0); |
| 183 | codegen_->GenerateFrameExit(); |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 184 | } |
| 185 | |
Nicolas Geoffray | 8ccc3f5 | 2014-03-19 10:34:11 +0000 | [diff] [blame] | 186 | void 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 | |
| 193 | void InstructionCodeGeneratorARM::LoadCurrentMethod(Register reg) { |
| 194 | __ ldr(reg, Address(SP, 0)); |
| 195 | } |
| 196 | |
| 197 | void 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 Geoffray | d8ee737 | 2014-03-28 15:43:40 +0000 | [diff] [blame^] | 224 | void 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 | |
| 239 | void 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 Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 252 | } // namespace arm |
| 253 | } // namespace art |