1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
|
/*
* Copyright (C) 2014 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "code_generator_x86.h"
#include "utils/assembler.h"
#include "utils/x86/assembler_x86.h"
#include "mirror/array.h"
#include "mirror/art_method.h"
#define __ reinterpret_cast<X86Assembler*>(GetAssembler())->
namespace art {
namespace x86 {
static constexpr int kNumberOfPushedRegistersAtEntry = 1;
static constexpr int kCurrentMethodStackOffset = 0;
InstructionCodeGeneratorX86::InstructionCodeGeneratorX86(HGraph* graph, CodeGeneratorX86* codegen)
: HGraphVisitor(graph),
assembler_(codegen->GetAssembler()),
codegen_(codegen) {}
void CodeGeneratorX86::GenerateFrameEntry() {
// Create a fake register to mimic Quick.
static const int kFakeReturnRegister = 8;
core_spill_mask_ |= (1 << kFakeReturnRegister);
// Add the current ART method to the frame size, the return PC, and the filler.
SetFrameSize(RoundUp((
GetGraph()->GetMaximumNumberOfOutVRegs() + GetGraph()->GetNumberOfVRegs() + 3) * kX86WordSize,
kStackAlignment));
// The return PC has already been pushed on the stack.
__ subl(ESP, Immediate(GetFrameSize() - kNumberOfPushedRegistersAtEntry * kX86WordSize));
__ movl(Address(ESP, kCurrentMethodStackOffset), EAX);
}
void CodeGeneratorX86::GenerateFrameExit() {
__ addl(ESP, Immediate(GetFrameSize() - kNumberOfPushedRegistersAtEntry * kX86WordSize));
}
void CodeGeneratorX86::Bind(Label* label) {
__ Bind(label);
}
void InstructionCodeGeneratorX86::LoadCurrentMethod(Register reg) {
__ movl(reg, Address(ESP, kCurrentMethodStackOffset));
}
int32_t CodeGeneratorX86::GetStackSlot(HLocal* local) const {
uint16_t reg_number = local->GetRegNumber();
uint16_t number_of_vregs = GetGraph()->GetNumberOfVRegs();
uint16_t number_of_in_vregs = GetGraph()->GetNumberOfInVRegs();
if (reg_number >= number_of_vregs - number_of_in_vregs) {
// Local is a parameter of the method. It is stored in the caller's frame.
return GetFrameSize() + kX86WordSize // ART method
+ (reg_number - number_of_vregs + number_of_in_vregs) * kX86WordSize;
} else {
// Local is a temporary in this method. It is stored in this method's frame.
return GetFrameSize() - (kNumberOfPushedRegistersAtEntry * kX86WordSize)
- kX86WordSize // filler.
- (number_of_vregs * kX86WordSize)
+ (reg_number * kX86WordSize);
}
}
void CodeGeneratorX86::Move(HInstruction* instruction, Location location, HInstruction* move_for) {
if (instruction->AsIntConstant() != nullptr) {
__ movl(location.reg<Register>(), Immediate(instruction->AsIntConstant()->GetValue()));
} else if (instruction->AsLoadLocal() != nullptr) {
__ movl(location.reg<Register>(),
Address(ESP, GetStackSlot(instruction->AsLoadLocal()->GetLocal())));
} else {
// This can currently only happen when the instruction that requests the move
// is the next to be compiled.
DCHECK_EQ(instruction->GetNext(), move_for);
__ movl(location.reg<Register>(),
instruction->GetLocations()->Out().reg<Register>());
}
}
void LocationsBuilderX86::VisitGoto(HGoto* got) {
got->SetLocations(nullptr);
}
void InstructionCodeGeneratorX86::VisitGoto(HGoto* got) {
HBasicBlock* successor = got->GetSuccessor();
if (GetGraph()->GetExitBlock() == successor) {
codegen_->GenerateFrameExit();
} else if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
__ jmp(codegen_->GetLabelOf(successor));
}
}
void LocationsBuilderX86::VisitExit(HExit* exit) {
exit->SetLocations(nullptr);
}
void InstructionCodeGeneratorX86::VisitExit(HExit* exit) {
if (kIsDebugBuild) {
__ Comment("Unreachable");
__ int3();
}
}
void LocationsBuilderX86::VisitIf(HIf* if_instr) {
LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
locations->SetInAt(0, Location(EAX));
if_instr->SetLocations(locations);
}
void InstructionCodeGeneratorX86::VisitIf(HIf* if_instr) {
// TODO: Generate the input as a condition, instead of materializing in a register.
__ cmpl(if_instr->GetLocations()->InAt(0).reg<Register>(), Immediate(0));
__ j(kEqual, codegen_->GetLabelOf(if_instr->IfFalseSuccessor()));
if (!codegen_->GoesToNextBlock(if_instr->GetBlock(), if_instr->IfTrueSuccessor())) {
__ jmp(codegen_->GetLabelOf(if_instr->IfTrueSuccessor()));
}
}
void LocationsBuilderX86::VisitLocal(HLocal* local) {
local->SetLocations(nullptr);
}
void InstructionCodeGeneratorX86::VisitLocal(HLocal* local) {
DCHECK_EQ(local->GetBlock(), GetGraph()->GetEntryBlock());
}
void LocationsBuilderX86::VisitLoadLocal(HLoadLocal* local) {
local->SetLocations(nullptr);
}
void InstructionCodeGeneratorX86::VisitLoadLocal(HLoadLocal* load) {
// Nothing to do, this is driven by the code generator.
}
void LocationsBuilderX86::VisitStoreLocal(HStoreLocal* local) {
LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(local);
locations->SetInAt(1, Location(EAX));
local->SetLocations(locations);
}
void InstructionCodeGeneratorX86::VisitStoreLocal(HStoreLocal* store) {
__ movl(Address(ESP, codegen_->GetStackSlot(store->GetLocal())),
store->GetLocations()->InAt(1).reg<Register>());
}
void LocationsBuilderX86::VisitEqual(HEqual* equal) {
LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(equal);
locations->SetInAt(0, Location(EAX));
locations->SetInAt(1, Location(ECX));
locations->SetOut(Location(EAX));
equal->SetLocations(locations);
}
void InstructionCodeGeneratorX86::VisitEqual(HEqual* equal) {
__ cmpl(equal->GetLocations()->InAt(0).reg<Register>(),
equal->GetLocations()->InAt(1).reg<Register>());
__ setb(kEqual, equal->GetLocations()->Out().reg<Register>());
}
void LocationsBuilderX86::VisitIntConstant(HIntConstant* constant) {
constant->SetLocations(nullptr);
}
void InstructionCodeGeneratorX86::VisitIntConstant(HIntConstant* constant) {
// Will be generated at use site.
}
void LocationsBuilderX86::VisitReturnVoid(HReturnVoid* ret) {
ret->SetLocations(nullptr);
}
void InstructionCodeGeneratorX86::VisitReturnVoid(HReturnVoid* ret) {
codegen_->GenerateFrameExit();
__ ret();
}
void LocationsBuilderX86::VisitReturn(HReturn* ret) {
LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(ret);
locations->SetInAt(0, Location(EAX));
ret->SetLocations(locations);
}
void InstructionCodeGeneratorX86::VisitReturn(HReturn* ret) {
DCHECK_EQ(ret->GetLocations()->InAt(0).reg<Register>(), EAX);
codegen_->GenerateFrameExit();
__ ret();
}
static constexpr Register kParameterCoreRegisters[] = { ECX, EDX, EBX };
static constexpr size_t kParameterCoreRegistersLength = arraysize(kParameterCoreRegisters);
class InvokeDexCallingConvention : public CallingConvention<Register> {
public:
InvokeDexCallingConvention()
: CallingConvention(kParameterCoreRegisters, kParameterCoreRegistersLength) {}
private:
DISALLOW_COPY_AND_ASSIGN(InvokeDexCallingConvention);
};
static constexpr Register kRuntimeParameterCoreRegisters[] = { EAX, ECX, EDX };
static constexpr size_t kRuntimeParameterCoreRegistersLength =
arraysize(kRuntimeParameterCoreRegisters);
class InvokeRuntimeCallingConvention : public CallingConvention<Register> {
public:
InvokeRuntimeCallingConvention()
: CallingConvention(kRuntimeParameterCoreRegisters,
kRuntimeParameterCoreRegistersLength) {}
private:
DISALLOW_COPY_AND_ASSIGN(InvokeRuntimeCallingConvention);
};
void LocationsBuilderX86::VisitPushArgument(HPushArgument* argument) {
LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(argument);
InvokeDexCallingConvention calling_convention;
if (argument->GetArgumentIndex() < calling_convention.GetNumberOfRegisters()) {
Location location = Location(calling_convention.GetRegisterAt(argument->GetArgumentIndex()));
locations->SetInAt(0, location);
locations->SetOut(location);
} else {
locations->SetInAt(0, Location(EAX));
}
argument->SetLocations(locations);
}
void InstructionCodeGeneratorX86::VisitPushArgument(HPushArgument* argument) {
uint8_t argument_index = argument->GetArgumentIndex();
InvokeDexCallingConvention calling_convention;
size_t parameter_registers = calling_convention.GetNumberOfRegisters();
if (argument_index >= parameter_registers) {
uint8_t offset = calling_convention.GetStackOffsetOf(argument_index);
__ movl(Address(ESP, offset),
argument->GetLocations()->InAt(0).reg<Register>());
} else {
DCHECK_EQ(argument->GetLocations()->Out().reg<Register>(),
argument->GetLocations()->InAt(0).reg<Register>());
}
}
void LocationsBuilderX86::VisitInvokeStatic(HInvokeStatic* invoke) {
LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(invoke);
locations->AddTemp(Location(EAX));
invoke->SetLocations(locations);
}
void InstructionCodeGeneratorX86::VisitInvokeStatic(HInvokeStatic* invoke) {
Register temp = invoke->GetLocations()->GetTemp(0).reg<Register>();
size_t index_in_cache = mirror::Array::DataOffset(sizeof(mirror::Object*)).Int32Value() +
invoke->GetIndexInDexCache() * kX86WordSize;
// TODO: Implement all kinds of calls:
// 1) boot -> boot
// 2) app -> boot
// 3) app -> app
//
// Currently we implement the app -> app logic, which looks up in the resolve cache.
// temp = method;
LoadCurrentMethod(temp);
// temp = temp->dex_cache_resolved_methods_;
__ movl(temp, Address(temp, mirror::ArtMethod::DexCacheResolvedMethodsOffset().Int32Value()));
// temp = temp[index_in_cache]
__ movl(temp, Address(temp, index_in_cache));
// (temp + offset_of_quick_compiled_code)()
__ call(Address(temp, mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset().Int32Value()));
codegen_->RecordPcInfo(invoke->GetDexPc());
}
void LocationsBuilderX86::VisitAdd(HAdd* add) {
LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(add);
switch (add->GetResultType()) {
case Primitive::kPrimInt: {
locations->SetInAt(0, Location(EAX));
locations->SetInAt(1, Location(ECX));
locations->SetOut(Location(EAX));
break;
}
default:
LOG(FATAL) << "Unimplemented";
}
add->SetLocations(locations);
}
void InstructionCodeGeneratorX86::VisitAdd(HAdd* add) {
LocationSummary* locations = add->GetLocations();
switch (add->GetResultType()) {
case Primitive::kPrimInt:
DCHECK_EQ(locations->InAt(0).reg<Register>(), locations->Out().reg<Register>());
__ addl(locations->InAt(0).reg<Register>(), locations->InAt(1).reg<Register>());
break;
default:
LOG(FATAL) << "Unimplemented";
}
}
void LocationsBuilderX86::VisitSub(HSub* sub) {
LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(sub);
switch (sub->GetResultType()) {
case Primitive::kPrimInt: {
locations->SetInAt(0, Location(EAX));
locations->SetInAt(1, Location(ECX));
locations->SetOut(Location(EAX));
break;
}
default:
LOG(FATAL) << "Unimplemented";
}
sub->SetLocations(locations);
}
void InstructionCodeGeneratorX86::VisitSub(HSub* sub) {
LocationSummary* locations = sub->GetLocations();
switch (sub->GetResultType()) {
case Primitive::kPrimInt:
DCHECK_EQ(locations->InAt(0).reg<Register>(), locations->Out().reg<Register>());
__ subl(locations->InAt(0).reg<Register>(), locations->InAt(1).reg<Register>());
break;
default:
LOG(FATAL) << "Unimplemented";
}
}
void LocationsBuilderX86::VisitNewInstance(HNewInstance* instruction) {
LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
locations->SetOut(Location(EAX));
instruction->SetLocations(locations);
}
void InstructionCodeGeneratorX86::VisitNewInstance(HNewInstance* instruction) {
InvokeRuntimeCallingConvention calling_convention;
LoadCurrentMethod(calling_convention.GetRegisterAt(1));
__ movl(calling_convention.GetRegisterAt(0),
Immediate(instruction->GetTypeIndex()));
__ fs()->call(
Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pAllocObjectWithAccessCheck)));
codegen_->RecordPcInfo(instruction->GetDexPc());
}
void LocationsBuilderX86::VisitParameterValue(HParameterValue* instruction) {
LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
InvokeDexCallingConvention calling_convention;
uint32_t argument_index = instruction->GetIndex();
if (argument_index < calling_convention.GetNumberOfRegisters()) {
locations->SetOut(Location(calling_convention.GetRegisterAt(argument_index)));
} else {
locations->SetOut(Location(EAX));
}
instruction->SetLocations(locations);
}
void InstructionCodeGeneratorX86::VisitParameterValue(HParameterValue* instruction) {
LocationSummary* locations = instruction->GetLocations();
InvokeDexCallingConvention calling_convention;
uint32_t argument_index = instruction->GetIndex();
if (argument_index >= calling_convention.GetNumberOfRegisters()) {
uint8_t offset = calling_convention.GetStackOffsetOf(argument_index);
__ movl(locations->Out().reg<Register>(), Address(ESP, offset + codegen_->GetFrameSize()));
}
}
void LocationsBuilderX86::VisitNot(HNot* instruction) {
LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
locations->SetInAt(0, Location(EAX));
locations->SetOut(Location(EAX));
instruction->SetLocations(locations);
}
void InstructionCodeGeneratorX86::VisitNot(HNot* instruction) {
LocationSummary* locations = instruction->GetLocations();
DCHECK_EQ(locations->InAt(0).reg<Register>(), locations->Out().reg<Register>());
__ xorl(locations->Out().reg<Register>(), Immediate(1));
}
} // namespace x86
} // namespace art
|