blob: 1764486e578a5342df6b6e2eba00bdae81bec8cf [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_x86.h"
18#include "utils/assembler.h"
19#include "utils/x86/assembler_x86.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<X86Assembler*>(GetAssembler())->
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000025
26namespace art {
27namespace x86 {
28
29void CodeGeneratorX86::GenerateFrameEntry() {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +000030 // 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 Geoffrayd4dd2552014-02-28 10:23:58 +000036 __ pushl(EBP);
37 __ movl(EBP, ESP);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +000038 // 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 Geoffrayd4dd2552014-02-28 10:23:58 +000043}
44
45void CodeGeneratorX86::GenerateFrameExit() {
46 __ movl(ESP, EBP);
47 __ popl(EBP);
48}
49
50void CodeGeneratorX86::Bind(Label* label) {
51 __ Bind(label);
52}
53
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +000054void CodeGeneratorX86::Push(HInstruction* instruction, Location location) {
55 __ pushl(location.reg<Register>());
56}
57
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +000058void InstructionCodeGeneratorX86::LoadCurrentMethod(Register reg) {
59 __ movl(reg, Address(ESP, 0));
60}
61
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +000062void CodeGeneratorX86::Move(HInstruction* instruction, Location location) {
63 HIntConstant* constant = instruction->AsIntConstant();
64 if (constant != nullptr) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +000065 __ movl(location.reg<Register>(), Immediate(constant->GetValue()));
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +000066 } else {
67 __ popl(location.reg<Register>());
68 }
69}
70
71void LocationsBuilderX86::VisitGoto(HGoto* got) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +000072 got->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +000073}
74
Nicolas Geoffray787c3072014-03-17 10:20:19 +000075void InstructionCodeGeneratorX86::VisitGoto(HGoto* got) {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000076 HBasicBlock* successor = got->GetSuccessor();
Nicolas Geoffray787c3072014-03-17 10:20:19 +000077 if (GetGraph()->GetExitBlock() == successor) {
78 codegen_->GenerateFrameExit();
79 } else if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
80 __ jmp(codegen_->GetLabelOf(successor));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000081 }
82}
83
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +000084void LocationsBuilderX86::VisitExit(HExit* exit) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +000085 exit->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +000086}
87
Nicolas Geoffray787c3072014-03-17 10:20:19 +000088void InstructionCodeGeneratorX86::VisitExit(HExit* exit) {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000089 if (kIsDebugBuild) {
90 __ Comment("Unreachable");
91 __ int3();
92 }
93}
94
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +000095void LocationsBuilderX86::VisitIf(HIf* if_instr) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +000096 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +000097 locations->SetInAt(0, Location(EAX));
Nicolas Geoffray787c3072014-03-17 10:20:19 +000098 if_instr->SetLocations(locations);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +000099}
100
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000101void InstructionCodeGeneratorX86::VisitIf(HIf* if_instr) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000102 // TODO: Generate the input as a condition, instead of materializing in a register.
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000103 __ 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 Geoffraybab4ed72014-03-11 17:53:17 +0000107 }
108}
109
110void LocationsBuilderX86::VisitLocal(HLocal* local) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000111 local->SetLocations(nullptr);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000112}
113
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000114void InstructionCodeGeneratorX86::VisitLocal(HLocal* local) {
115 DCHECK_EQ(local->GetBlock(), GetGraph()->GetEntryBlock());
116 codegen_->SetFrameSize(codegen_->GetFrameSize() + kWordSize);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000117}
118
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000119void LocationsBuilderX86::VisitLoadLocal(HLoadLocal* local) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000120 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(local);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000121 locations->SetOut(Location(EAX));
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000122 local->SetLocations(locations);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000123}
124
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000125static int32_t GetStackSlot(HLocal* local) {
126 // We are currently using EBP to access locals, so the offset must be negative.
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000127 // +1 for going backwards, +1 for the method pointer.
128 return (local->GetRegNumber() + 2) * -kWordSize;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000129}
130
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000131void InstructionCodeGeneratorX86::VisitLoadLocal(HLoadLocal* load) {
132 __ movl(load->GetLocations()->Out().reg<Register>(),
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000133 Address(EBP, GetStackSlot(load->GetLocal())));
134}
135
136void LocationsBuilderX86::VisitStoreLocal(HStoreLocal* local) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000137 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(local);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000138 locations->SetInAt(1, Location(EAX));
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000139 local->SetLocations(locations);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000140}
141
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000142void InstructionCodeGeneratorX86::VisitStoreLocal(HStoreLocal* store) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000143 __ movl(Address(EBP, GetStackSlot(store->GetLocal())),
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000144 store->GetLocations()->InAt(1).reg<Register>());
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000145}
146
147void LocationsBuilderX86::VisitEqual(HEqual* equal) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000148 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(equal);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000149 locations->SetInAt(0, Location(EAX));
150 locations->SetInAt(1, Location(ECX));
151 locations->SetOut(Location(EAX));
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000152 equal->SetLocations(locations);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000153}
154
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000155void 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 Geoffraybab4ed72014-03-11 17:53:17 +0000159}
160
161void LocationsBuilderX86::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000162 constant->SetLocations(nullptr);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000163}
164
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000165void InstructionCodeGeneratorX86::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000166 // Will be generated at use site.
167}
168
169void LocationsBuilderX86::VisitReturnVoid(HReturnVoid* ret) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000170 ret->SetLocations(nullptr);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000171}
172
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000173void InstructionCodeGeneratorX86::VisitReturnVoid(HReturnVoid* ret) {
174 codegen_->GenerateFrameExit();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000175 __ ret();
176}
177
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000178void LocationsBuilderX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000179 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(ret);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000180 locations->SetInAt(0, Location(EAX));
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000181 ret->SetLocations(locations);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000182}
183
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000184void InstructionCodeGeneratorX86::VisitReturn(HReturn* ret) {
185 DCHECK_EQ(ret->GetLocations()->InAt(0).reg<Register>(), EAX);
186 codegen_->GenerateFrameExit();
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000187 __ ret();
188}
189
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000190void 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
197void 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 Geoffrayd8ee7372014-03-28 15:43:40 +0000221void 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
236void 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 Geoffrayd4dd2552014-02-28 10:23:58 +0000248} // namespace x86
249} // namespace art