blob: 7b0a08735694b9e709d687cae712651396d0e1ce [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
Nicolas Geoffray4a34a422014-04-03 10:38:37 +010029static constexpr int kNumberOfPushedRegistersAtEntry = 1;
30static constexpr int kCurrentMethodStackOffset = 0;
31
32InstructionCodeGeneratorX86::InstructionCodeGeneratorX86(HGraph* graph, CodeGeneratorX86* codegen)
33 : HGraphVisitor(graph),
34 assembler_(codegen->GetAssembler()),
35 codegen_(codegen) {}
36
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000037void CodeGeneratorX86::GenerateFrameEntry() {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +000038 // Create a fake register to mimic Quick.
39 static const int kFakeReturnRegister = 8;
40 core_spill_mask_ |= (1 << kFakeReturnRegister);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +000041
Nicolas Geoffray4a34a422014-04-03 10:38:37 +010042 // Add the current ART method to the frame size and the return PC.
43 SetFrameSize(RoundUp(GetFrameSize() + 2 * kWordSize, kStackAlignment));
44 // The return PC has already been pushed on the stack.
45 __ subl(ESP, Immediate(GetFrameSize() - kNumberOfPushedRegistersAtEntry * kWordSize));
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +000046 __ movl(Address(ESP, 0), EAX);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000047}
48
49void CodeGeneratorX86::GenerateFrameExit() {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +010050 __ addl(ESP, Immediate(GetFrameSize() - kNumberOfPushedRegistersAtEntry * kWordSize));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000051}
52
53void CodeGeneratorX86::Bind(Label* label) {
54 __ Bind(label);
55}
56
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +000057void InstructionCodeGeneratorX86::LoadCurrentMethod(Register reg) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +010058 __ movl(reg, Address(ESP, kCurrentMethodStackOffset));
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +000059}
60
Nicolas Geoffray4a34a422014-04-03 10:38:37 +010061int32_t CodeGeneratorX86::GetStackSlot(HLocal* local) const {
62 return (GetGraph()->GetMaximumNumberOfOutVRegs() + local->GetRegNumber()) * kWordSize;
63}
64
65void CodeGeneratorX86::Move(HInstruction* instruction, Location location, HInstruction* move_for) {
66 if (instruction->AsIntConstant() != nullptr) {
67 __ movl(location.reg<Register>(), Immediate(instruction->AsIntConstant()->GetValue()));
68 } else if (instruction->AsLoadLocal() != nullptr) {
69 __ movl(location.reg<Register>(),
70 Address(ESP, GetStackSlot(instruction->AsLoadLocal()->GetLocal())));
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +000071 } else {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +010072 // This can currently only happen when the instruction that requests the move
73 // is the next to be compiled.
74 DCHECK_EQ(instruction->GetNext(), move_for);
75 __ movl(location.reg<Register>(),
76 instruction->GetLocations()->Out().reg<Register>());
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +000077 }
78}
79
80void LocationsBuilderX86::VisitGoto(HGoto* got) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +000081 got->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +000082}
83
Nicolas Geoffray787c3072014-03-17 10:20:19 +000084void InstructionCodeGeneratorX86::VisitGoto(HGoto* got) {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000085 HBasicBlock* successor = got->GetSuccessor();
Nicolas Geoffray787c3072014-03-17 10:20:19 +000086 if (GetGraph()->GetExitBlock() == successor) {
87 codegen_->GenerateFrameExit();
88 } else if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
89 __ jmp(codegen_->GetLabelOf(successor));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000090 }
91}
92
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +000093void LocationsBuilderX86::VisitExit(HExit* exit) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +000094 exit->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +000095}
96
Nicolas Geoffray787c3072014-03-17 10:20:19 +000097void InstructionCodeGeneratorX86::VisitExit(HExit* exit) {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000098 if (kIsDebugBuild) {
99 __ Comment("Unreachable");
100 __ int3();
101 }
102}
103
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000104void LocationsBuilderX86::VisitIf(HIf* if_instr) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000105 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000106 locations->SetInAt(0, Location(EAX));
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000107 if_instr->SetLocations(locations);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000108}
109
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000110void InstructionCodeGeneratorX86::VisitIf(HIf* if_instr) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000111 // TODO: Generate the input as a condition, instead of materializing in a register.
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000112 __ cmpl(if_instr->GetLocations()->InAt(0).reg<Register>(), Immediate(0));
113 __ j(kEqual, codegen_->GetLabelOf(if_instr->IfFalseSuccessor()));
114 if (!codegen_->GoesToNextBlock(if_instr->GetBlock(), if_instr->IfTrueSuccessor())) {
115 __ jmp(codegen_->GetLabelOf(if_instr->IfTrueSuccessor()));
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000116 }
117}
118
119void LocationsBuilderX86::VisitLocal(HLocal* local) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000120 local->SetLocations(nullptr);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000121}
122
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000123void InstructionCodeGeneratorX86::VisitLocal(HLocal* local) {
124 DCHECK_EQ(local->GetBlock(), GetGraph()->GetEntryBlock());
125 codegen_->SetFrameSize(codegen_->GetFrameSize() + kWordSize);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000126}
127
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000128void LocationsBuilderX86::VisitLoadLocal(HLoadLocal* local) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100129 local->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000130}
131
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000132void InstructionCodeGeneratorX86::VisitLoadLocal(HLoadLocal* load) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100133 // Nothing to do, this is driven by the code generator.
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000134}
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 Geoffray4a34a422014-04-03 10:38:37 +0100143 __ movl(Address(ESP, codegen_->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 Geoffray4a34a422014-04-03 10:38:37 +0100190static constexpr Register kParameterCoreRegisters[] = { ECX, EDX, EBX };
191static constexpr int kParameterCoreRegistersLength = arraysize(kParameterCoreRegisters);
192
193class InvokeStaticCallingConvention : public CallingConvention<Register> {
194 public:
195 InvokeStaticCallingConvention()
196 : CallingConvention(kParameterCoreRegisters, kParameterCoreRegistersLength) {}
197
198 private:
199 DISALLOW_COPY_AND_ASSIGN(InvokeStaticCallingConvention);
200};
201
202void LocationsBuilderX86::VisitPushArgument(HPushArgument* argument) {
203 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(argument);
204 InvokeStaticCallingConvention calling_convention;
205 if (argument->GetArgumentIndex() < calling_convention.GetNumberOfRegisters()) {
206 Location location = Location(calling_convention.GetRegisterAt(argument->GetArgumentIndex()));
207 locations->SetInAt(0, location);
208 locations->SetOut(location);
209 } else {
210 locations->SetInAt(0, Location(EAX));
211 }
212 argument->SetLocations(locations);
213}
214
215void InstructionCodeGeneratorX86::VisitPushArgument(HPushArgument* argument) {
216 uint8_t argument_index = argument->GetArgumentIndex();
217 InvokeStaticCallingConvention calling_convention;
218 size_t parameter_registers = calling_convention.GetNumberOfRegisters();
219 if (argument_index >= parameter_registers) {
220 uint8_t offset = calling_convention.GetStackOffsetOf(argument_index);
221 __ movl(Address(ESP, offset),
222 argument->GetLocations()->InAt(0).reg<Register>());
223
224 } else {
225 DCHECK_EQ(argument->GetLocations()->Out().reg<Register>(),
226 argument->GetLocations()->InAt(0).reg<Register>());
227 }
228}
229
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000230void LocationsBuilderX86::VisitInvokeStatic(HInvokeStatic* invoke) {
231 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(invoke);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000232 locations->AddTemp(Location(EAX));
233 invoke->SetLocations(locations);
234}
235
236void InstructionCodeGeneratorX86::VisitInvokeStatic(HInvokeStatic* invoke) {
237 Register temp = invoke->GetLocations()->GetTemp(0).reg<Register>();
238 size_t index_in_cache = mirror::Array::DataOffset(sizeof(mirror::Object*)).Int32Value() +
239 invoke->GetIndexInDexCache() * kWordSize;
240
241 // TODO: Implement all kinds of calls:
242 // 1) boot -> boot
243 // 2) app -> boot
244 // 3) app -> app
245 //
246 // Currently we implement the app -> app logic, which looks up in the resolve cache.
247
248 // temp = method;
249 LoadCurrentMethod(temp);
250 // temp = temp->dex_cache_resolved_methods_;
251 __ movl(temp, Address(temp, mirror::ArtMethod::DexCacheResolvedMethodsOffset().Int32Value()));
252 // temp = temp[index_in_cache]
253 __ movl(temp, Address(temp, index_in_cache));
254 // (temp + offset_of_quick_compiled_code)()
255 __ call(Address(temp, mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset().Int32Value()));
256
257 codegen_->RecordPcInfo(invoke->GetDexPc());
258}
259
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +0000260void LocationsBuilderX86::VisitAdd(HAdd* add) {
261 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(add);
262 switch (add->GetResultType()) {
263 case Primitive::kPrimInt: {
264 locations->SetInAt(0, Location(EAX));
265 locations->SetInAt(1, Location(ECX));
266 locations->SetOut(Location(EAX));
267 break;
268 }
269 default:
270 LOG(FATAL) << "Unimplemented";
271 }
272 add->SetLocations(locations);
273}
274
275void InstructionCodeGeneratorX86::VisitAdd(HAdd* add) {
276 LocationSummary* locations = add->GetLocations();
277 switch (add->GetResultType()) {
278 case Primitive::kPrimInt:
279 DCHECK_EQ(locations->InAt(0).reg<Register>(), locations->Out().reg<Register>());
280 __ addl(locations->InAt(0).reg<Register>(), locations->InAt(1).reg<Register>());
281 break;
282 default:
283 LOG(FATAL) << "Unimplemented";
284 }
285}
286
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000287} // namespace x86
288} // namespace art