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_x86.h" |
| 18 | #include "utils/assembler.h" |
| 19 | #include "utils/x86/assembler_x86.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<X86Assembler*>(GetAssembler())-> |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 25 | |
| 26 | namespace art { |
| 27 | namespace x86 { |
| 28 | |
| 29 | void CodeGeneratorX86::GenerateFrameEntry() { |
Nicolas Geoffray | 8ccc3f5 | 2014-03-19 10:34:11 +0000 | [diff] [blame] | 30 | // Create a fake register to mimic Quick. |
| 31 | static const int kFakeReturnRegister = 8; |
| 32 | core_spill_mask_ |= (1 << kFakeReturnRegister); |
| 33 | // We're currently always using EBP, which is callee-saved in Quick. |
| 34 | core_spill_mask_ |= (1 << EBP); |
| 35 | |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 36 | __ pushl(EBP); |
| 37 | __ movl(EBP, ESP); |
Nicolas Geoffray | 8ccc3f5 | 2014-03-19 10:34:11 +0000 | [diff] [blame] | 38 | // Add the current ART method to the frame size, the return pc, and EBP. |
| 39 | SetFrameSize(RoundUp(GetFrameSize() + 3 * kWordSize, kStackAlignment)); |
| 40 | // The PC and EBP have already been pushed on the stack. |
| 41 | __ subl(ESP, Immediate(GetFrameSize() - 2 * kWordSize)); |
| 42 | __ movl(Address(ESP, 0), EAX); |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | void CodeGeneratorX86::GenerateFrameExit() { |
| 46 | __ movl(ESP, EBP); |
| 47 | __ popl(EBP); |
| 48 | } |
| 49 | |
| 50 | void CodeGeneratorX86::Bind(Label* label) { |
| 51 | __ Bind(label); |
| 52 | } |
| 53 | |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 54 | void CodeGeneratorX86::Push(HInstruction* instruction, Location location) { |
| 55 | __ pushl(location.reg<Register>()); |
| 56 | } |
| 57 | |
Nicolas Geoffray | 8ccc3f5 | 2014-03-19 10:34:11 +0000 | [diff] [blame] | 58 | void InstructionCodeGeneratorX86::LoadCurrentMethod(Register reg) { |
| 59 | __ movl(reg, Address(ESP, 0)); |
| 60 | } |
| 61 | |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 62 | void CodeGeneratorX86::Move(HInstruction* instruction, Location location) { |
| 63 | HIntConstant* constant = instruction->AsIntConstant(); |
| 64 | if (constant != nullptr) { |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 65 | __ movl(location.reg<Register>(), Immediate(constant->GetValue())); |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 66 | } else { |
| 67 | __ popl(location.reg<Register>()); |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | void LocationsBuilderX86::VisitGoto(HGoto* got) { |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 72 | got->SetLocations(nullptr); |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 73 | } |
| 74 | |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 75 | void InstructionCodeGeneratorX86::VisitGoto(HGoto* got) { |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 76 | HBasicBlock* successor = got->GetSuccessor(); |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 77 | if (GetGraph()->GetExitBlock() == successor) { |
| 78 | codegen_->GenerateFrameExit(); |
| 79 | } else if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) { |
| 80 | __ jmp(codegen_->GetLabelOf(successor)); |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 81 | } |
| 82 | } |
| 83 | |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 84 | void LocationsBuilderX86::VisitExit(HExit* exit) { |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 85 | exit->SetLocations(nullptr); |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 86 | } |
| 87 | |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 88 | void InstructionCodeGeneratorX86::VisitExit(HExit* exit) { |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 89 | if (kIsDebugBuild) { |
| 90 | __ Comment("Unreachable"); |
| 91 | __ int3(); |
| 92 | } |
| 93 | } |
| 94 | |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 95 | void LocationsBuilderX86::VisitIf(HIf* if_instr) { |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 96 | LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr); |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 97 | locations->SetInAt(0, Location(EAX)); |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 98 | if_instr->SetLocations(locations); |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 99 | } |
| 100 | |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 101 | void InstructionCodeGeneratorX86::VisitIf(HIf* if_instr) { |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 102 | // 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] | 103 | __ cmpl(if_instr->GetLocations()->InAt(0).reg<Register>(), Immediate(0)); |
| 104 | __ j(kEqual, codegen_->GetLabelOf(if_instr->IfFalseSuccessor())); |
| 105 | if (!codegen_->GoesToNextBlock(if_instr->GetBlock(), if_instr->IfTrueSuccessor())) { |
| 106 | __ jmp(codegen_->GetLabelOf(if_instr->IfTrueSuccessor())); |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 107 | } |
| 108 | } |
| 109 | |
| 110 | void LocationsBuilderX86::VisitLocal(HLocal* local) { |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 111 | local->SetLocations(nullptr); |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 112 | } |
| 113 | |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 114 | void InstructionCodeGeneratorX86::VisitLocal(HLocal* local) { |
| 115 | DCHECK_EQ(local->GetBlock(), GetGraph()->GetEntryBlock()); |
| 116 | codegen_->SetFrameSize(codegen_->GetFrameSize() + kWordSize); |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 117 | } |
| 118 | |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 119 | void LocationsBuilderX86::VisitLoadLocal(HLoadLocal* local) { |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 120 | LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(local); |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 121 | locations->SetOut(Location(EAX)); |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 122 | local->SetLocations(locations); |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 123 | } |
| 124 | |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 125 | static int32_t GetStackSlot(HLocal* local) { |
| 126 | // We are currently using EBP to access locals, so the offset must be negative. |
Nicolas Geoffray | 8ccc3f5 | 2014-03-19 10:34:11 +0000 | [diff] [blame] | 127 | // +1 for going backwards, +1 for the method pointer. |
| 128 | return (local->GetRegNumber() + 2) * -kWordSize; |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 129 | } |
| 130 | |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 131 | void InstructionCodeGeneratorX86::VisitLoadLocal(HLoadLocal* load) { |
| 132 | __ movl(load->GetLocations()->Out().reg<Register>(), |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 133 | Address(EBP, GetStackSlot(load->GetLocal()))); |
| 134 | } |
| 135 | |
| 136 | void LocationsBuilderX86::VisitStoreLocal(HStoreLocal* local) { |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 137 | LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(local); |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 138 | locations->SetInAt(1, Location(EAX)); |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 139 | local->SetLocations(locations); |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 140 | } |
| 141 | |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 142 | void InstructionCodeGeneratorX86::VisitStoreLocal(HStoreLocal* store) { |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 143 | __ movl(Address(EBP, GetStackSlot(store->GetLocal())), |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 144 | store->GetLocations()->InAt(1).reg<Register>()); |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | void LocationsBuilderX86::VisitEqual(HEqual* equal) { |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 148 | LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(equal); |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 149 | locations->SetInAt(0, Location(EAX)); |
| 150 | locations->SetInAt(1, Location(ECX)); |
| 151 | locations->SetOut(Location(EAX)); |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 152 | equal->SetLocations(locations); |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 153 | } |
| 154 | |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 155 | void InstructionCodeGeneratorX86::VisitEqual(HEqual* equal) { |
| 156 | __ cmpl(equal->GetLocations()->InAt(0).reg<Register>(), |
| 157 | equal->GetLocations()->InAt(1).reg<Register>()); |
| 158 | __ setb(kEqual, equal->GetLocations()->Out().reg<Register>()); |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | void LocationsBuilderX86::VisitIntConstant(HIntConstant* constant) { |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 162 | constant->SetLocations(nullptr); |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 163 | } |
| 164 | |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 165 | void InstructionCodeGeneratorX86::VisitIntConstant(HIntConstant* constant) { |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 166 | // Will be generated at use site. |
| 167 | } |
| 168 | |
| 169 | void LocationsBuilderX86::VisitReturnVoid(HReturnVoid* ret) { |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 170 | ret->SetLocations(nullptr); |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 171 | } |
| 172 | |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 173 | void InstructionCodeGeneratorX86::VisitReturnVoid(HReturnVoid* ret) { |
| 174 | codegen_->GenerateFrameExit(); |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 175 | __ ret(); |
| 176 | } |
| 177 | |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 178 | void LocationsBuilderX86::VisitReturn(HReturn* ret) { |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 179 | LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(ret); |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 180 | locations->SetInAt(0, Location(EAX)); |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 181 | ret->SetLocations(locations); |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 182 | } |
| 183 | |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 184 | void InstructionCodeGeneratorX86::VisitReturn(HReturn* ret) { |
| 185 | DCHECK_EQ(ret->GetLocations()->InAt(0).reg<Register>(), EAX); |
| 186 | codegen_->GenerateFrameExit(); |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 187 | __ ret(); |
| 188 | } |
| 189 | |
Nicolas Geoffray | 8ccc3f5 | 2014-03-19 10:34:11 +0000 | [diff] [blame] | 190 | void LocationsBuilderX86::VisitInvokeStatic(HInvokeStatic* invoke) { |
| 191 | LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(invoke); |
| 192 | CHECK_EQ(invoke->InputCount(), 0); |
| 193 | locations->AddTemp(Location(EAX)); |
| 194 | invoke->SetLocations(locations); |
| 195 | } |
| 196 | |
| 197 | void InstructionCodeGeneratorX86::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 | __ movl(temp, Address(temp, mirror::ArtMethod::DexCacheResolvedMethodsOffset().Int32Value())); |
| 213 | // temp = temp[index_in_cache] |
| 214 | __ movl(temp, Address(temp, index_in_cache)); |
| 215 | // (temp + offset_of_quick_compiled_code)() |
| 216 | __ call(Address(temp, mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset().Int32Value())); |
| 217 | |
| 218 | codegen_->RecordPcInfo(invoke->GetDexPc()); |
| 219 | } |
| 220 | |
Nicolas Geoffray | d8ee737 | 2014-03-28 15:43:40 +0000 | [diff] [blame] | 221 | void LocationsBuilderX86::VisitAdd(HAdd* add) { |
| 222 | LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(add); |
| 223 | switch (add->GetResultType()) { |
| 224 | case Primitive::kPrimInt: { |
| 225 | locations->SetInAt(0, Location(EAX)); |
| 226 | locations->SetInAt(1, Location(ECX)); |
| 227 | locations->SetOut(Location(EAX)); |
| 228 | break; |
| 229 | } |
| 230 | default: |
| 231 | LOG(FATAL) << "Unimplemented"; |
| 232 | } |
| 233 | add->SetLocations(locations); |
| 234 | } |
| 235 | |
| 236 | void InstructionCodeGeneratorX86::VisitAdd(HAdd* add) { |
| 237 | LocationSummary* locations = add->GetLocations(); |
| 238 | switch (add->GetResultType()) { |
| 239 | case Primitive::kPrimInt: |
| 240 | DCHECK_EQ(locations->InAt(0).reg<Register>(), locations->Out().reg<Register>()); |
| 241 | __ addl(locations->InAt(0).reg<Register>(), locations->InAt(1).reg<Register>()); |
| 242 | break; |
| 243 | default: |
| 244 | LOG(FATAL) << "Unimplemented"; |
| 245 | } |
| 246 | } |
| 247 | |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 248 | } // namespace x86 |
| 249 | } // namespace art |