blob: 722f7bb25886e0dd571dd5c2a6d82e8c84f363fb [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, \
31 rm8_i8_opcode, rm8_i8_modrm, \
32 rm32_i32_opcode, rm32_i32_modrm, \
33 rm32_i8_opcode, rm32_i8_modrm) \
34{ kOp ## opcode ## RI, \
35 kRegImm, \
36 0, \
37 { RegMem_Immediate: { rax8_i8, rax32_i32, \
38 {rm8_i8_opcode, rm8_i8_modrm}, \
39 {rm32_i32_opcode, rm32_i32_modrm}, \
40 {rm32_i8_opcode, rm32_i8_modrm} } }, \
41 #opcode "RI", "" \
42}, \
43{ kOp ## opcode ## MI, \
44 kMemImm, \
45 0, \
46 { RegMem_Immediate: { rax8_i8, rax32_i32, \
47 {rm8_i8_opcode, rm8_i8_modrm}, \
48 {rm32_i32_opcode, rm32_i32_modrm}, \
49 {rm32_i8_opcode, rm32_i8_modrm} } }, \
50 #opcode "MI", "" \
51}, \
52{ kOp ## opcode ## AI, \
53 kArrayImm, \
54 0, \
55 { RegMem_Immediate: { rax8_i8, rax32_i32, \
56 {rm8_i8_opcode, rm8_i8_modrm}, \
57 {rm32_i32_opcode, rm32_i32_modrm}, \
58 {rm32_i8_opcode, rm32_i8_modrm} } }, \
59 #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 */,
141 0x81, 0x7 /* RegMem32/imm32 */, 0x83, 0x7 /* RegMem32/imm8 */)
buzbeee88dfbf2012-03-05 11:19:57 -0800142};
143
144
145/*
146 * Assemble the LIR into binary instruction format. Note that we may
147 * discover that pc-relative displacements may not fit the selected
148 * instruction. In those cases we will try to substitute a new code
149 * sequence or request that the trace be shortened and retried.
150 */
151AssemblerStatus oatAssembleInstructions(CompilationUnit *cUnit,
152 intptr_t startAddr)
153{
154 UNIMPLEMENTED(WARNING) << "oatAssembleInstructions";
155 return kSuccess;
156#if 0
157 LIR *lir;
158 AssemblerStatus res = kSuccess; // Assume success
159
160 for (lir = (LIR *) cUnit->firstLIRInsn; lir; lir = NEXT_LIR(lir)) {
161 if (lir->opcode < 0) {
162 continue;
163 }
164
165
166 if (lir->flags.isNop) {
167 continue;
168 }
169
170 if (lir->flags.pcRelFixup) {
buzbeea7678db2012-03-05 15:35:46 -0800171 if (lir->opcode == kX86Delta) {
buzbeee88dfbf2012-03-05 11:19:57 -0800172 /*
173 * The "Delta" pseudo-ops load the difference between
174 * two pc-relative locations into a the target register
175 * found in operands[0]. The delta is determined by
176 * (label2 - label1), where label1 is a standard
177 * kPseudoTargetLabel and is stored in operands[2].
178 * If operands[3] is null, then label2 is a kPseudoTargetLabel
179 * and is found in lir->target. If operands[3] is non-NULL,
180 * then it is a Switch/Data table.
181 */
182 int offset1 = ((LIR*)lir->operands[2])->offset;
183 SwitchTable *tabRec = (SwitchTable*)lir->operands[3];
184 int offset2 = tabRec ? tabRec->offset : lir->target->offset;
185 int delta = offset2 - offset1;
186 if ((delta & 0xffff) == delta) {
187 // Fits
188 lir->operands[1] = delta;
189 } else {
buzbeea7678db2012-03-05 15:35:46 -0800190 // Doesn't fit - must expand to kX86Delta[Hi|Lo] pair
buzbeee88dfbf2012-03-05 11:19:57 -0800191 LIR *newDeltaHi =
buzbeea7678db2012-03-05 15:35:46 -0800192 rawLIR(cUnit, lir->dalvikOffset, kX86DeltaHi,
buzbeee88dfbf2012-03-05 11:19:57 -0800193 lir->operands[0], 0, lir->operands[2],
194 lir->operands[3], lir->target);
195 oatInsertLIRBefore((LIR*)lir, (LIR*)newDeltaHi);
196 LIR *newDeltaLo =
buzbeea7678db2012-03-05 15:35:46 -0800197 rawLIR(cUnit, lir->dalvikOffset, kX86DeltaLo,
buzbeee88dfbf2012-03-05 11:19:57 -0800198 lir->operands[0], 0, lir->operands[2],
199 lir->operands[3], lir->target);
200 oatInsertLIRBefore((LIR*)lir, (LIR*)newDeltaLo);
201 lir->flags.isNop = true;
202 res = kRetryAll;
203 }
buzbeea7678db2012-03-05 15:35:46 -0800204 } else if (lir->opcode == kX86DeltaLo) {
buzbeee88dfbf2012-03-05 11:19:57 -0800205 int offset1 = ((LIR*)lir->operands[2])->offset;
206 SwitchTable *tabRec = (SwitchTable*)lir->operands[3];
207 int offset2 = tabRec ? tabRec->offset : lir->target->offset;
208 int delta = offset2 - offset1;
209 lir->operands[1] = delta & 0xffff;
buzbeea7678db2012-03-05 15:35:46 -0800210 } else if (lir->opcode == kX86DeltaHi) {
buzbeee88dfbf2012-03-05 11:19:57 -0800211 int offset1 = ((LIR*)lir->operands[2])->offset;
212 SwitchTable *tabRec = (SwitchTable*)lir->operands[3];
213 int offset2 = tabRec ? tabRec->offset : lir->target->offset;
214 int delta = offset2 - offset1;
215 lir->operands[1] = (delta >> 16) & 0xffff;
buzbeea7678db2012-03-05 15:35:46 -0800216 } else if (lir->opcode == kX86B || lir->opcode == kX86Bal) {
buzbeee88dfbf2012-03-05 11:19:57 -0800217 LIR *targetLIR = (LIR *) lir->target;
218 intptr_t pc = lir->offset + 4;
219 intptr_t target = targetLIR->offset;
220 int delta = target - pc;
221 if (delta & 0x3) {
222 LOG(FATAL) << "PC-rel offset not multiple of 4: " << delta;
223 }
224 if (delta > 131068 || delta < -131069) {
225 res = kRetryAll;
226 convertShortToLongBranch(cUnit, lir);
227 } else {
228 lir->operands[0] = delta >> 2;
229 }
buzbeea7678db2012-03-05 15:35:46 -0800230 } else if (lir->opcode >= kX86Beqz && lir->opcode <= kX86Bnez) {
buzbeee88dfbf2012-03-05 11:19:57 -0800231 LIR *targetLIR = (LIR *) lir->target;
232 intptr_t pc = lir->offset + 4;
233 intptr_t target = targetLIR->offset;
234 int delta = target - pc;
235 if (delta & 0x3) {
236 LOG(FATAL) << "PC-rel offset not multiple of 4: " << delta;
237 }
238 if (delta > 131068 || delta < -131069) {
239 res = kRetryAll;
240 convertShortToLongBranch(cUnit, lir);
241 } else {
242 lir->operands[1] = delta >> 2;
243 }
buzbeea7678db2012-03-05 15:35:46 -0800244 } else if (lir->opcode == kX86Beq || lir->opcode == kX86Bne) {
buzbeee88dfbf2012-03-05 11:19:57 -0800245 LIR *targetLIR = (LIR *) lir->target;
246 intptr_t pc = lir->offset + 4;
247 intptr_t target = targetLIR->offset;
248 int delta = target - pc;
249 if (delta & 0x3) {
250 LOG(FATAL) << "PC-rel offset not multiple of 4: " << delta;
251 }
252 if (delta > 131068 || delta < -131069) {
253 res = kRetryAll;
254 convertShortToLongBranch(cUnit, lir);
255 } else {
256 lir->operands[2] = delta >> 2;
257 }
buzbeea7678db2012-03-05 15:35:46 -0800258 } else if (lir->opcode == kX86Jal) {
buzbeee88dfbf2012-03-05 11:19:57 -0800259 intptr_t curPC = (startAddr + lir->offset + 4) & ~3;
260 intptr_t target = lir->operands[0];
261 /* ensure PC-region branch can be used */
262 DCHECK_EQ((curPC & 0xF0000000), (target & 0xF0000000));
263 if (target & 0x3) {
264 LOG(FATAL) << "Jump target not multiple of 4: " << target;
265 }
266 lir->operands[0] = target >> 2;
buzbeea7678db2012-03-05 15:35:46 -0800267 } else if (lir->opcode == kX86Lahi) { /* ld address hi (via lui) */
buzbeee88dfbf2012-03-05 11:19:57 -0800268 LIR *targetLIR = (LIR *) lir->target;
269 intptr_t target = startAddr + targetLIR->offset;
270 lir->operands[1] = target >> 16;
buzbeea7678db2012-03-05 15:35:46 -0800271 } else if (lir->opcode == kX86Lalo) { /* ld address lo (via ori) */
buzbeee88dfbf2012-03-05 11:19:57 -0800272 LIR *targetLIR = (LIR *) lir->target;
273 intptr_t target = startAddr + targetLIR->offset;
274 lir->operands[2] = lir->operands[2] + target;
275 }
276 }
277
278 /*
279 * If one of the pc-relative instructions expanded we'll have
280 * to make another pass. Don't bother to fully assemble the
281 * instruction.
282 */
283 if (res != kSuccess) {
284 continue;
285 }
buzbeea7678db2012-03-05 15:35:46 -0800286 const X86EncodingMap *encoder = &EncodingMap[lir->opcode];
buzbeee88dfbf2012-03-05 11:19:57 -0800287 u4 bits = encoder->skeleton;
288 int i;
289 for (i = 0; i < 4; i++) {
290 u4 operand;
291 u4 value;
292 operand = lir->operands[i];
293 switch(encoder->fieldLoc[i].kind) {
294 case kFmtUnused:
295 break;
296 case kFmtBitBlt:
297 if (encoder->fieldLoc[i].start == 0 && encoder->fieldLoc[i].end == 31) {
298 value = operand;
299 } else {
300 value = (operand << encoder->fieldLoc[i].start) &
301 ((1 << (encoder->fieldLoc[i].end + 1)) - 1);
302 }
303 bits |= value;
304 break;
305 case kFmtBlt5_2:
306 value = (operand & 0x1f);
307 bits |= (value << encoder->fieldLoc[i].start);
308 bits |= (value << encoder->fieldLoc[i].end);
309 break;
310 case kFmtDfp: {
311 DCHECK(DOUBLEREG(operand));
312 DCHECK((operand & 0x1) == 0);
313 value = ((operand & FP_REG_MASK) << encoder->fieldLoc[i].start) &
314 ((1 << (encoder->fieldLoc[i].end + 1)) - 1);
315 bits |= value;
316 break;
317 }
318 case kFmtSfp:
319 DCHECK(SINGLEREG(operand));
320 value = ((operand & FP_REG_MASK) << encoder->fieldLoc[i].start) &
321 ((1 << (encoder->fieldLoc[i].end + 1)) - 1);
322 bits |= value;
323 break;
324 default:
325 LOG(FATAL) << "Bad encoder format: "
326 << (int)encoder->fieldLoc[i].kind;
327 }
328 }
329 // FIXME: need multi-endian handling here
330 cUnit->codeBuffer.push_back((bits >> 16) & 0xffff);
331 cUnit->codeBuffer.push_back(bits & 0xffff);
332 // TUNING: replace with proper delay slot handling
333 if (encoder->size == 8) {
buzbeea7678db2012-03-05 15:35:46 -0800334 const X86EncodingMap *encoder = &EncodingMap[kX86Nop];
buzbeee88dfbf2012-03-05 11:19:57 -0800335 u4 bits = encoder->skeleton;
336 cUnit->codeBuffer.push_back((bits >> 16) & 0xffff);
337 cUnit->codeBuffer.push_back(bits & 0xffff);
338 }
339 }
340 return res;
341#endif
342}
343
344int oatGetInsnSize(LIR* lir)
345{
Ian Rogers96ab4202012-03-05 19:51:02 -0800346 switch (EncodingMap[lir->opcode].kind) {
347 case kData:
348 return 4;
349 case kRegImm: {
350 int reg = lir->operands[0];
351 int imm = lir->operands[1];
352 return (reg == rAX ? 1 : 2) + // AX opcodes don't require the modrm byte
353 (IS_SIMM8(imm) ? 1 : 4); // 1 or 4 byte immediate
354 break;
355 }
356 case kMemImm: {
357 // int base = lir->operands[0];
358 int disp = lir->operands[1];
359 int imm = lir->operands[2];
360 return 2 + // opcode and modrm bytes
361 (disp == 0 ? 0 : (IS_SIMM8(disp) ? 1 : 4)) + // 0, 1 or 4 byte displacement
362 (IS_SIMM8(imm) ? 1 : 4); // 1 or 4 byte immediate
363 break;
364 }
365 case kArrayImm:
366 UNIMPLEMENTED(FATAL);
367 return 0;
368 case kRegReg:
369 return 2; // opcode and modrm
370 case kRegMem: {
371 // int reg = lir->operands[0];
372 // int base = lir->operands[1];
373 int disp = lir->operands[2];
374 return 2 + // opcode and modrm bytes
375 (disp == 0 ? 0 : (IS_SIMM8(disp) ? 1 : 4)); // 0, 1 or 4 byte displacement
376 break;
377 }
378 case kRegArray:
379 UNIMPLEMENTED(FATAL);
380 return 0;
381 case kMemReg: {
382 // int base = lir->operands[0];
383 int disp = lir->operands[1];
384 // int reg = lir->operands[2];
385 return 2 + // opcode and modrm bytes
386 (disp == 0 ? 0 : (IS_SIMM8(disp) ? 1 : 4)); // 0, 1 or 4 byte displacement
387 break;
388 }
389 case kArrayReg:
390 UNIMPLEMENTED(FATAL);
391 return 0;
392 }
393 UNIMPLEMENTED(FATAL); // unreachable
394 return 0;
buzbeee88dfbf2012-03-05 11:19:57 -0800395}
396/*
397 * Target-dependent offset assignment.
398 * independent.
399 */
400int oatAssignInsnOffsets(CompilationUnit* cUnit)
401{
402 LIR* x86LIR;
403 int offset = 0;
404
405 for (x86LIR = (LIR *) cUnit->firstLIRInsn;
406 x86LIR;
407 x86LIR = NEXT_LIR(x86LIR)) {
408 x86LIR->offset = offset;
409 if (x86LIR->opcode >= 0) {
410 if (!x86LIR->flags.isNop) {
411 offset += x86LIR->flags.size;
412 }
413 } else if (x86LIR->opcode == kPseudoPseudoAlign4) {
414 if (offset & 0x2) {
415 offset += 2;
416 x86LIR->operands[0] = 1;
417 } else {
418 x86LIR->operands[0] = 0;
419 }
420 }
421 /* Pseudo opcodes don't consume space */
422 }
423
424 return offset;
425}
426
427} // namespace art