blob: 8223db01d98826611884ea88a2e0f0b5ab0c285f [file] [log] [blame]
buzbeee88dfbf2012-03-05 11:19:57 -08001/*
2 * Copyright (C) 2012 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 "../../Dalvik.h"
18#include "../../CompilerInternals.h"
19#include "X86LIR.h"
20#include "Codegen.h"
21#include <sys/mman.h> /* for protection change */
22
23namespace art {
24
25#define MAX_ASSEMBLER_RETRIES 50
26
Ian Rogers96ab4202012-03-05 19:51:02 -080027#define BINARY_ENCODING_MAP(opcode, \
28 rm8_r8, rm32_r32, \
29 r8_rm8, r32_rm32, \
30 rax8_i8, rax32_i32, \
Ian Rogersde797832012-03-06 10:18:10 -080031 rm8_i8_opcode, rm8_i8_modrm_opcode, \
32 rm32_i32_opcode, rm32_i32_modrm_opcode, \
33 rm32_i8_opcode, rm32_i8_modrm_opcode) \
Ian Rogers96ab4202012-03-05 19:51:02 -080034{ kOp ## opcode ## RI, \
35 kRegImm, \
36 0, \
37 { RegMem_Immediate: { rax8_i8, rax32_i32, \
Ian Rogersde797832012-03-06 10:18:10 -080038 {rm8_i8_opcode, rm8_i8_modrm_opcode}, \
39 {rm32_i32_opcode, rm32_i32_modrm_opcode}, \
40 {rm32_i8_opcode, rm32_i8_modrm_opcode} } }, \
Ian Rogers96ab4202012-03-05 19:51:02 -080041 #opcode "RI", "" \
42}, \
43{ kOp ## opcode ## MI, \
44 kMemImm, \
45 0, \
46 { RegMem_Immediate: { rax8_i8, rax32_i32, \
Ian Rogersde797832012-03-06 10:18:10 -080047 {rm8_i8_opcode, rm8_i8_modrm_opcode}, \
48 {rm32_i32_opcode, rm32_i32_modrm_opcode}, \
49 {rm32_i8_opcode, rm32_i8_modrm_opcode} } }, \
Ian Rogers96ab4202012-03-05 19:51:02 -080050 #opcode "MI", "" \
51}, \
52{ kOp ## opcode ## AI, \
53 kArrayImm, \
54 0, \
55 { RegMem_Immediate: { rax8_i8, rax32_i32, \
Ian Rogersde797832012-03-06 10:18:10 -080056 {rm8_i8_opcode, rm8_i8_modrm_opcode}, \
57 {rm32_i32_opcode, rm32_i32_modrm_opcode}, \
58 {rm32_i8_opcode, rm32_i8_modrm_opcode} } }, \
Ian Rogers96ab4202012-03-05 19:51:02 -080059 #opcode "AI", "" \
60}, \
61{ kOp ## opcode ## RR, \
62 kRegReg, \
63 0, \
64 { Reg_RegMem: {r8_rm8, r32_rm32} }, \
65 #opcode "RR", "" \
66}, \
67{ kOp ## opcode ## RM, \
68 kRegMem, \
69 0, \
70 { Reg_RegMem: {r8_rm8, r32_rm32} }, \
71 #opcode "RM", "" \
72}, \
73{ kOp ## opcode ## RA, \
74 kRegArray, \
75 0, \
76 { Reg_RegMem: {r8_rm8, r32_rm32} }, \
77 #opcode "RA", "" \
78}, \
79{ kOp ## opcode ## MR, \
80 kMemReg, \
81 0, \
82 { RegMem_Reg: {rm8_r8, rm32_r32} }, \
83 #opcode "MR", "" \
84}, \
85{ kOp ## opcode ## AR, \
86 kArrayReg, \
87 0, \
88 { RegMem_Reg: {rm8_r8, rm32_r32} }, \
89 #opcode "AR", "" \
90}
buzbeee88dfbf2012-03-05 11:19:57 -080091
buzbeea7678db2012-03-05 15:35:46 -080092X86EncodingMap EncodingMap[kX86Last] = {
Ian Rogers96ab4202012-03-05 19:51:02 -080093 { kX8632BitData, kData, 0 /* flags - TODO */, { unused: 0 }, "data", "" },
94BINARY_ENCODING_MAP(Add,
95 0x00 /* RegMem8/Reg8 */, 0x01 /* RegMem32/Reg32 */,
96 0x02 /* Reg8/RegMem8 */, 0x03 /* Reg32/RegMem32 */,
97 0x04 /* Rax8/imm8 opcode */, 0x05 /* Rax32/imm32 */,
98 0x80, 0x0 /* RegMem8/imm8 */,
99 0x81, 0x0 /* RegMem32/imm32 */, 0x83, 0x0 /* RegMem32/imm8 */),
100BINARY_ENCODING_MAP(Or,
101 0x08 /* RegMem8/Reg8 */, 0x09 /* RegMem32/Reg32 */,
102 0x0A /* Reg8/RegMem8 */, 0x0B /* Reg32/RegMem32 */,
103 0x0C /* Rax8/imm8 opcode */, 0x0D /* Rax32/imm32 */,
104 0x80, 0x1 /* RegMem8/imm8 */,
105 0x81, 0x1 /* RegMem32/imm32 */, 0x83, 0x1 /* RegMem32/imm8 */),
106BINARY_ENCODING_MAP(Adc,
107 0x10 /* RegMem8/Reg8 */, 0x11 /* RegMem32/Reg32 */,
108 0x12 /* Reg8/RegMem8 */, 0x13 /* Reg32/RegMem32 */,
109 0x14 /* Rax8/imm8 opcode */, 0x15 /* Rax32/imm32 */,
110 0x80, 0x2 /* RegMem8/imm8 */,
111 0x81, 0x2 /* RegMem32/imm32 */, 0x83, 0x2 /* RegMem32/imm8 */),
112BINARY_ENCODING_MAP(Sbb,
113 0x18 /* RegMem8/Reg8 */, 0x19 /* RegMem32/Reg32 */,
114 0x1A /* Reg8/RegMem8 */, 0x1B /* Reg32/RegMem32 */,
115 0x1C /* Rax8/imm8 opcode */, 0x1D /* Rax32/imm32 */,
116 0x80, 0x3 /* RegMem8/imm8 */,
117 0x81, 0x3 /* RegMem32/imm32 */, 0x83, 0x3 /* RegMem32/imm8 */),
118BINARY_ENCODING_MAP(And,
119 0x20 /* RegMem8/Reg8 */, 0x21 /* RegMem32/Reg32 */,
120 0x22 /* Reg8/RegMem8 */, 0x23 /* Reg32/RegMem32 */,
121 0x24 /* Rax8/imm8 opcode */, 0x25 /* Rax32/imm32 */,
122 0x80, 0x4 /* RegMem8/imm8 */,
123 0x81, 0x4 /* RegMem32/imm32 */, 0x83, 0x4 /* RegMem32/imm8 */),
124BINARY_ENCODING_MAP(Sub,
125 0x28 /* RegMem8/Reg8 */, 0x29 /* RegMem32/Reg32 */,
126 0x2A /* Reg8/RegMem8 */, 0x2B /* Reg32/RegMem32 */,
127 0x2C /* Rax8/imm8 opcode */, 0x2D /* Rax32/imm32 */,
128 0x80, 0x5 /* RegMem8/imm8 */,
129 0x81, 0x5 /* RegMem32/imm32 */, 0x83, 0x5 /* RegMem32/imm8 */),
130BINARY_ENCODING_MAP(Xor,
131 0x30 /* RegMem8/Reg8 */, 0x31 /* RegMem32/Reg32 */,
132 0x32 /* Reg8/RegMem8 */, 0x33 /* Reg32/RegMem32 */,
133 0x34 /* Rax8/imm8 opcode */, 0x35 /* Rax32/imm32 */,
134 0x80, 0x6 /* RegMem8/imm8 */,
135 0x81, 0x6 /* RegMem32/imm32 */, 0x83, 0x6 /* RegMem32/imm8 */),
136BINARY_ENCODING_MAP(Cmp,
137 0x38 /* RegMem8/Reg8 */, 0x39 /* RegMem32/Reg32 */,
138 0x3A /* Reg8/RegMem8 */, 0x3B /* Reg32/RegMem32 */,
139 0x3C /* Rax8/imm8 opcode */, 0x3D /* Rax32/imm32 */,
140 0x80, 0x7 /* RegMem8/imm8 */,
Ian Rogersde797832012-03-06 10:18:10 -0800141 0x81, 0x7 /* RegMem32/imm32 */, 0x83, 0x7 /* RegMem32/imm8 */),
142 { kOpMovRI, kUnimplemented, 0 /* flags - TODO */ , { unused: 0 }, "MovRI", "" },
143 { kOpMovMI, kUnimplemented, 0 /* flags - TODO */ , { unused: 0 }, "MovMI", "" },
144 { kOpMovAI, kUnimplemented, 0 /* flags - TODO */ , { unused: 0 }, "MovAI", "" },
145 { kOpMovRR, kRegReg, 0 /* flags - TODO */, { Reg_RegMem: {0x8A, 0x8B} }, "MovRR", "" },
146 { kOpMovRM, kRegMem, 0 /* flags - TODO */, { Reg_RegMem: {0x8A, 0x8B} }, "MovRM", "" },
147 { kOpMovRA, kRegArray, 0 /* flags - TODO */, { Reg_RegMem: {0x8A, 0x8B} }, "MovRA", "" },
148 { kOpMovMR, kMemReg, 0 /* flags - TODO */, { RegMem_Reg: {0x88, 0x89} }, "MovMR", "" },
149 { kOpMovAR, kArrayReg, 0 /* flags - TODO */, { RegMem_Reg: {0x88, 0x89} }, "MovAR", "" }
buzbeee88dfbf2012-03-05 11:19:57 -0800150};
151
Ian Rogersde797832012-03-06 10:18:10 -0800152int oatGetInsnSize(LIR* lir)
153{
154 switch (EncodingMap[lir->opcode].kind) {
155 case kData:
156 return 4;
157 case kRegImm: {
158 int reg = lir->operands[0];
159 int imm = lir->operands[1];
160 return (reg == rAX ? 1 : 2) + // AX opcodes don't require the modrm byte
161 (IS_SIMM8(imm) ? 1 : 4); // 1 or 4 byte immediate
162 break;
163 }
164 case kMemImm: {
165 // int base = lir->operands[0];
166 int disp = lir->operands[1];
167 int imm = lir->operands[2];
168 return 2 + // opcode and modrm bytes
169 (disp == 0 ? 0 : (IS_SIMM8(disp) ? 1 : 4)) + // 0, 1 or 4 byte displacement
170 (IS_SIMM8(imm) ? 1 : 4); // 1 or 4 byte immediate
171 break;
172 }
173 case kArrayImm:
174 UNIMPLEMENTED(FATAL);
175 return 0;
176 case kRegReg:
177 return 2; // opcode and modrm
178 case kRegMem: {
179 // int reg = lir->operands[0];
180 // int base = lir->operands[1];
181 int disp = lir->operands[2];
182 return 2 + // opcode and modrm bytes
183 (disp == 0 ? 0 : (IS_SIMM8(disp) ? 1 : 4)); // 0, 1 or 4 byte displacement
184 break;
185 }
186 case kRegArray:
187 UNIMPLEMENTED(FATAL);
188 return 0;
189 case kMemReg: {
190 // int base = lir->operands[0];
191 int disp = lir->operands[1];
192 // int reg = lir->operands[2];
193 return 2 + // opcode and modrm bytes
194 (disp == 0 ? 0 : (IS_SIMM8(disp) ? 1 : 4)); // 0, 1 or 4 byte displacement
195 break;
196 }
197 case kArrayReg:
198 UNIMPLEMENTED(FATAL);
199 return 0;
200 case kUnimplemented:
201 UNIMPLEMENTED(FATAL);
202 return 0;
203 }
204 UNIMPLEMENTED(FATAL); // unreachable
205 return 0;
206}
buzbeee88dfbf2012-03-05 11:19:57 -0800207
208/*
209 * Assemble the LIR into binary instruction format. Note that we may
210 * discover that pc-relative displacements may not fit the selected
211 * instruction. In those cases we will try to substitute a new code
212 * sequence or request that the trace be shortened and retried.
213 */
214AssemblerStatus oatAssembleInstructions(CompilationUnit *cUnit,
215 intptr_t startAddr)
216{
217 UNIMPLEMENTED(WARNING) << "oatAssembleInstructions";
218 return kSuccess;
219#if 0
220 LIR *lir;
221 AssemblerStatus res = kSuccess; // Assume success
222
223 for (lir = (LIR *) cUnit->firstLIRInsn; lir; lir = NEXT_LIR(lir)) {
224 if (lir->opcode < 0) {
225 continue;
226 }
227
228
229 if (lir->flags.isNop) {
230 continue;
231 }
232
233 if (lir->flags.pcRelFixup) {
buzbeea7678db2012-03-05 15:35:46 -0800234 if (lir->opcode == kX86Delta) {
buzbeee88dfbf2012-03-05 11:19:57 -0800235 /*
236 * The "Delta" pseudo-ops load the difference between
237 * two pc-relative locations into a the target register
238 * found in operands[0]. The delta is determined by
239 * (label2 - label1), where label1 is a standard
240 * kPseudoTargetLabel and is stored in operands[2].
241 * If operands[3] is null, then label2 is a kPseudoTargetLabel
242 * and is found in lir->target. If operands[3] is non-NULL,
243 * then it is a Switch/Data table.
244 */
245 int offset1 = ((LIR*)lir->operands[2])->offset;
246 SwitchTable *tabRec = (SwitchTable*)lir->operands[3];
247 int offset2 = tabRec ? tabRec->offset : lir->target->offset;
248 int delta = offset2 - offset1;
249 if ((delta & 0xffff) == delta) {
250 // Fits
251 lir->operands[1] = delta;
252 } else {
buzbeea7678db2012-03-05 15:35:46 -0800253 // Doesn't fit - must expand to kX86Delta[Hi|Lo] pair
buzbeee88dfbf2012-03-05 11:19:57 -0800254 LIR *newDeltaHi =
buzbeea7678db2012-03-05 15:35:46 -0800255 rawLIR(cUnit, lir->dalvikOffset, kX86DeltaHi,
buzbeee88dfbf2012-03-05 11:19:57 -0800256 lir->operands[0], 0, lir->operands[2],
257 lir->operands[3], lir->target);
258 oatInsertLIRBefore((LIR*)lir, (LIR*)newDeltaHi);
259 LIR *newDeltaLo =
buzbeea7678db2012-03-05 15:35:46 -0800260 rawLIR(cUnit, lir->dalvikOffset, kX86DeltaLo,
buzbeee88dfbf2012-03-05 11:19:57 -0800261 lir->operands[0], 0, lir->operands[2],
262 lir->operands[3], lir->target);
263 oatInsertLIRBefore((LIR*)lir, (LIR*)newDeltaLo);
264 lir->flags.isNop = true;
265 res = kRetryAll;
266 }
buzbeea7678db2012-03-05 15:35:46 -0800267 } else if (lir->opcode == kX86DeltaLo) {
buzbeee88dfbf2012-03-05 11:19:57 -0800268 int offset1 = ((LIR*)lir->operands[2])->offset;
269 SwitchTable *tabRec = (SwitchTable*)lir->operands[3];
270 int offset2 = tabRec ? tabRec->offset : lir->target->offset;
271 int delta = offset2 - offset1;
272 lir->operands[1] = delta & 0xffff;
buzbeea7678db2012-03-05 15:35:46 -0800273 } else if (lir->opcode == kX86DeltaHi) {
buzbeee88dfbf2012-03-05 11:19:57 -0800274 int offset1 = ((LIR*)lir->operands[2])->offset;
275 SwitchTable *tabRec = (SwitchTable*)lir->operands[3];
276 int offset2 = tabRec ? tabRec->offset : lir->target->offset;
277 int delta = offset2 - offset1;
278 lir->operands[1] = (delta >> 16) & 0xffff;
buzbeea7678db2012-03-05 15:35:46 -0800279 } else if (lir->opcode == kX86B || lir->opcode == kX86Bal) {
buzbeee88dfbf2012-03-05 11:19:57 -0800280 LIR *targetLIR = (LIR *) lir->target;
281 intptr_t pc = lir->offset + 4;
282 intptr_t target = targetLIR->offset;
283 int delta = target - pc;
284 if (delta & 0x3) {
285 LOG(FATAL) << "PC-rel offset not multiple of 4: " << delta;
286 }
287 if (delta > 131068 || delta < -131069) {
288 res = kRetryAll;
289 convertShortToLongBranch(cUnit, lir);
290 } else {
291 lir->operands[0] = delta >> 2;
292 }
buzbeea7678db2012-03-05 15:35:46 -0800293 } else if (lir->opcode >= kX86Beqz && lir->opcode <= kX86Bnez) {
buzbeee88dfbf2012-03-05 11:19:57 -0800294 LIR *targetLIR = (LIR *) lir->target;
295 intptr_t pc = lir->offset + 4;
296 intptr_t target = targetLIR->offset;
297 int delta = target - pc;
298 if (delta & 0x3) {
299 LOG(FATAL) << "PC-rel offset not multiple of 4: " << delta;
300 }
301 if (delta > 131068 || delta < -131069) {
302 res = kRetryAll;
303 convertShortToLongBranch(cUnit, lir);
304 } else {
305 lir->operands[1] = delta >> 2;
306 }
buzbeea7678db2012-03-05 15:35:46 -0800307 } else if (lir->opcode == kX86Beq || lir->opcode == kX86Bne) {
buzbeee88dfbf2012-03-05 11:19:57 -0800308 LIR *targetLIR = (LIR *) lir->target;
309 intptr_t pc = lir->offset + 4;
310 intptr_t target = targetLIR->offset;
311 int delta = target - pc;
312 if (delta & 0x3) {
313 LOG(FATAL) << "PC-rel offset not multiple of 4: " << delta;
314 }
315 if (delta > 131068 || delta < -131069) {
316 res = kRetryAll;
317 convertShortToLongBranch(cUnit, lir);
318 } else {
319 lir->operands[2] = delta >> 2;
320 }
buzbeea7678db2012-03-05 15:35:46 -0800321 } else if (lir->opcode == kX86Jal) {
buzbeee88dfbf2012-03-05 11:19:57 -0800322 intptr_t curPC = (startAddr + lir->offset + 4) & ~3;
323 intptr_t target = lir->operands[0];
324 /* ensure PC-region branch can be used */
325 DCHECK_EQ((curPC & 0xF0000000), (target & 0xF0000000));
326 if (target & 0x3) {
327 LOG(FATAL) << "Jump target not multiple of 4: " << target;
328 }
329 lir->operands[0] = target >> 2;
buzbeea7678db2012-03-05 15:35:46 -0800330 } else if (lir->opcode == kX86Lahi) { /* ld address hi (via lui) */
buzbeee88dfbf2012-03-05 11:19:57 -0800331 LIR *targetLIR = (LIR *) lir->target;
332 intptr_t target = startAddr + targetLIR->offset;
333 lir->operands[1] = target >> 16;
buzbeea7678db2012-03-05 15:35:46 -0800334 } else if (lir->opcode == kX86Lalo) { /* ld address lo (via ori) */
buzbeee88dfbf2012-03-05 11:19:57 -0800335 LIR *targetLIR = (LIR *) lir->target;
336 intptr_t target = startAddr + targetLIR->offset;
337 lir->operands[2] = lir->operands[2] + target;
338 }
339 }
340
341 /*
342 * If one of the pc-relative instructions expanded we'll have
343 * to make another pass. Don't bother to fully assemble the
344 * instruction.
345 */
346 if (res != kSuccess) {
347 continue;
348 }
buzbeea7678db2012-03-05 15:35:46 -0800349 const X86EncodingMap *encoder = &EncodingMap[lir->opcode];
buzbeee88dfbf2012-03-05 11:19:57 -0800350 u4 bits = encoder->skeleton;
351 int i;
352 for (i = 0; i < 4; i++) {
353 u4 operand;
354 u4 value;
355 operand = lir->operands[i];
356 switch(encoder->fieldLoc[i].kind) {
357 case kFmtUnused:
358 break;
359 case kFmtBitBlt:
360 if (encoder->fieldLoc[i].start == 0 && encoder->fieldLoc[i].end == 31) {
361 value = operand;
362 } else {
363 value = (operand << encoder->fieldLoc[i].start) &
364 ((1 << (encoder->fieldLoc[i].end + 1)) - 1);
365 }
366 bits |= value;
367 break;
368 case kFmtBlt5_2:
369 value = (operand & 0x1f);
370 bits |= (value << encoder->fieldLoc[i].start);
371 bits |= (value << encoder->fieldLoc[i].end);
372 break;
373 case kFmtDfp: {
374 DCHECK(DOUBLEREG(operand));
375 DCHECK((operand & 0x1) == 0);
376 value = ((operand & FP_REG_MASK) << encoder->fieldLoc[i].start) &
377 ((1 << (encoder->fieldLoc[i].end + 1)) - 1);
378 bits |= value;
379 break;
380 }
381 case kFmtSfp:
382 DCHECK(SINGLEREG(operand));
383 value = ((operand & FP_REG_MASK) << encoder->fieldLoc[i].start) &
384 ((1 << (encoder->fieldLoc[i].end + 1)) - 1);
385 bits |= value;
386 break;
387 default:
388 LOG(FATAL) << "Bad encoder format: "
389 << (int)encoder->fieldLoc[i].kind;
390 }
391 }
392 // FIXME: need multi-endian handling here
393 cUnit->codeBuffer.push_back((bits >> 16) & 0xffff);
394 cUnit->codeBuffer.push_back(bits & 0xffff);
395 // TUNING: replace with proper delay slot handling
396 if (encoder->size == 8) {
buzbeea7678db2012-03-05 15:35:46 -0800397 const X86EncodingMap *encoder = &EncodingMap[kX86Nop];
buzbeee88dfbf2012-03-05 11:19:57 -0800398 u4 bits = encoder->skeleton;
399 cUnit->codeBuffer.push_back((bits >> 16) & 0xffff);
400 cUnit->codeBuffer.push_back(bits & 0xffff);
401 }
402 }
403 return res;
404#endif
405}
406
buzbeee88dfbf2012-03-05 11:19:57 -0800407/*
408 * Target-dependent offset assignment.
409 * independent.
410 */
411int oatAssignInsnOffsets(CompilationUnit* cUnit)
412{
413 LIR* x86LIR;
414 int offset = 0;
415
416 for (x86LIR = (LIR *) cUnit->firstLIRInsn;
417 x86LIR;
418 x86LIR = NEXT_LIR(x86LIR)) {
419 x86LIR->offset = offset;
420 if (x86LIR->opcode >= 0) {
421 if (!x86LIR->flags.isNop) {
422 offset += x86LIR->flags.size;
423 }
424 } else if (x86LIR->opcode == kPseudoPseudoAlign4) {
425 if (offset & 0x2) {
426 offset += 2;
427 x86LIR->operands[0] = 1;
428 } else {
429 x86LIR->operands[0] = 0;
430 }
431 }
432 /* Pseudo opcodes don't consume space */
433 }
434
435 return offset;
436}
437
438} // namespace art