blob: 882541b43b99b770113a3249e30b7cf071c4e29e [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 };
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +0100191static constexpr size_t kParameterCoreRegistersLength = arraysize(kParameterCoreRegisters);
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100192
193class InvokeStaticCallingConvention : public CallingConvention<Register> {
194 public:
195 InvokeStaticCallingConvention()
196 : CallingConvention(kParameterCoreRegisters, kParameterCoreRegistersLength) {}
197
198 private:
199 DISALLOW_COPY_AND_ASSIGN(InvokeStaticCallingConvention);
200};
201
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +0100202static constexpr Register kRuntimeParameterCoreRegisters[] = { EAX, ECX, EDX };
203static constexpr size_t kRuntimeParameterCoreRegistersLength =
204 arraysize(kRuntimeParameterCoreRegisters);
205
206class InvokeRuntimeCallingConvention : public CallingConvention<Register> {
207 public:
208 InvokeRuntimeCallingConvention()
209 : CallingConvention(kRuntimeParameterCoreRegisters,
210 kRuntimeParameterCoreRegistersLength) {}
211
212 private:
213 DISALLOW_COPY_AND_ASSIGN(InvokeRuntimeCallingConvention);
214};
215
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100216void LocationsBuilderX86::VisitPushArgument(HPushArgument* argument) {
217 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(argument);
218 InvokeStaticCallingConvention calling_convention;
219 if (argument->GetArgumentIndex() < calling_convention.GetNumberOfRegisters()) {
220 Location location = Location(calling_convention.GetRegisterAt(argument->GetArgumentIndex()));
221 locations->SetInAt(0, location);
222 locations->SetOut(location);
223 } else {
224 locations->SetInAt(0, Location(EAX));
225 }
226 argument->SetLocations(locations);
227}
228
229void InstructionCodeGeneratorX86::VisitPushArgument(HPushArgument* argument) {
230 uint8_t argument_index = argument->GetArgumentIndex();
231 InvokeStaticCallingConvention calling_convention;
232 size_t parameter_registers = calling_convention.GetNumberOfRegisters();
233 if (argument_index >= parameter_registers) {
234 uint8_t offset = calling_convention.GetStackOffsetOf(argument_index);
235 __ movl(Address(ESP, offset),
236 argument->GetLocations()->InAt(0).reg<Register>());
237
238 } else {
239 DCHECK_EQ(argument->GetLocations()->Out().reg<Register>(),
240 argument->GetLocations()->InAt(0).reg<Register>());
241 }
242}
243
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000244void LocationsBuilderX86::VisitInvokeStatic(HInvokeStatic* invoke) {
245 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(invoke);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000246 locations->AddTemp(Location(EAX));
247 invoke->SetLocations(locations);
248}
249
250void InstructionCodeGeneratorX86::VisitInvokeStatic(HInvokeStatic* invoke) {
251 Register temp = invoke->GetLocations()->GetTemp(0).reg<Register>();
252 size_t index_in_cache = mirror::Array::DataOffset(sizeof(mirror::Object*)).Int32Value() +
253 invoke->GetIndexInDexCache() * kWordSize;
254
255 // TODO: Implement all kinds of calls:
256 // 1) boot -> boot
257 // 2) app -> boot
258 // 3) app -> app
259 //
260 // Currently we implement the app -> app logic, which looks up in the resolve cache.
261
262 // temp = method;
263 LoadCurrentMethod(temp);
264 // temp = temp->dex_cache_resolved_methods_;
265 __ movl(temp, Address(temp, mirror::ArtMethod::DexCacheResolvedMethodsOffset().Int32Value()));
266 // temp = temp[index_in_cache]
267 __ movl(temp, Address(temp, index_in_cache));
268 // (temp + offset_of_quick_compiled_code)()
269 __ call(Address(temp, mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset().Int32Value()));
270
271 codegen_->RecordPcInfo(invoke->GetDexPc());
272}
273
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +0000274void LocationsBuilderX86::VisitAdd(HAdd* add) {
275 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(add);
276 switch (add->GetResultType()) {
277 case Primitive::kPrimInt: {
278 locations->SetInAt(0, Location(EAX));
279 locations->SetInAt(1, Location(ECX));
280 locations->SetOut(Location(EAX));
281 break;
282 }
283 default:
284 LOG(FATAL) << "Unimplemented";
285 }
286 add->SetLocations(locations);
287}
288
289void InstructionCodeGeneratorX86::VisitAdd(HAdd* add) {
290 LocationSummary* locations = add->GetLocations();
291 switch (add->GetResultType()) {
292 case Primitive::kPrimInt:
293 DCHECK_EQ(locations->InAt(0).reg<Register>(), locations->Out().reg<Register>());
294 __ addl(locations->InAt(0).reg<Register>(), locations->InAt(1).reg<Register>());
295 break;
296 default:
297 LOG(FATAL) << "Unimplemented";
298 }
299}
300
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +0100301void LocationsBuilderX86::VisitNewInstance(HNewInstance* instruction) {
302 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
303 locations->SetOut(Location(EAX));
304 instruction->SetLocations(locations);
305}
306
307void InstructionCodeGeneratorX86::VisitNewInstance(HNewInstance* instruction) {
308 InvokeRuntimeCallingConvention calling_convention;
309 LoadCurrentMethod(calling_convention.GetRegisterAt(1));
310 __ movl(calling_convention.GetRegisterAt(0),
311 Immediate(instruction->GetTypeIndex()));
312
313 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kWordSize, pAllocObjectWithAccessCheck)));
314
315 codegen_->RecordPcInfo(instruction->GetDexPc());
316}
317
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000318} // namespace x86
319} // namespace art