blob: ad02c39b3ed30dd8e176179b3cbbd420a5598ff7 [file] [log] [blame]
buzbeee3acd072012-02-25 17:03:10 -08001/*
2 * Copyright (C) 2011 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
17namespace art {
18
buzbee31a4a6f2012-02-28 15:36:15 -080019void setMemRefType(LIR* lir, bool isLoad, int memType)
20{
21 u8 *maskPtr;
22 u8 mask = ENCODE_MEM;;
23 DCHECK(EncodingMap[lir->opcode].flags & (IS_LOAD | IS_STORE));
24 if (isLoad) {
25 maskPtr = &lir->useMask;
26 } else {
27 maskPtr = &lir->defMask;
28 }
29 /* Clear out the memref flags */
30 *maskPtr &= ~mask;
31 /* ..and then add back the one we need */
32 switch(memType) {
33 case kLiteral:
34 DCHECK(isLoad);
35 *maskPtr |= ENCODE_LITERAL;
36 break;
37 case kDalvikReg:
38 *maskPtr |= ENCODE_DALVIK_REG;
39 break;
40 case kHeapRef:
41 *maskPtr |= ENCODE_HEAP_REF;
42 break;
43 case kMustNotAlias:
44 /* Currently only loads can be marked as kMustNotAlias */
45 DCHECK(!(EncodingMap[lir->opcode].flags & IS_STORE));
46 *maskPtr |= ENCODE_MUST_NOT_ALIAS;
47 break;
48 default:
49 LOG(FATAL) << "Oat: invalid memref kind - " << memType;
50 }
51}
52
53/*
54 * Mark load/store instructions that access Dalvik registers through r5FP +
55 * offset.
56 */
57void annotateDalvikRegAccess(LIR* lir, int regId, bool isLoad)
58{
59 setMemRefType(lir, isLoad, kDalvikReg);
60
61 /*
62 * Store the Dalvik register id in aliasInfo. Mark he MSB if it is a 64-bit
63 * access.
64 */
65 lir->aliasInfo = regId;
66 if (DOUBLEREG(lir->operands[0])) {
67 lir->aliasInfo |= 0x80000000;
68 }
69}
70
71/*
72 * Decode the register id.
73 */
74inline u8 getRegMaskCommon(int reg)
75{
76 u8 seed;
77 int shift;
78 int regId = reg & 0x1f;
79
80 /*
81 * Each double register is equal to a pair of single-precision FP registers
82 */
83 seed = DOUBLEREG(reg) ? 3 : 1;
84 /* FP register starts at bit position 16 */
85 shift = FPREG(reg) ? kFPReg0 : 0;
86 /* Expand the double register id into single offset */
87 shift += regId;
88 return (seed << shift);
89}
90
91/*
92 * Mark the corresponding bit(s).
93 */
94inline void setupRegMask(u8* mask, int reg)
95{
96 *mask |= getRegMaskCommon(reg);
97}
98
99/*
100 * Set up the proper fields in the resource mask
101 */
102void setupResourceMasks(LIR* lir)
103{
104 int opcode = lir->opcode;
105 int flags;
106
107 if (opcode <= 0) {
108 lir->useMask = lir->defMask = 0;
109 return;
110 }
111
112 flags = EncodingMap[lir->opcode].flags;
113
114 if (flags & NEEDS_FIXUP) {
115 lir->flags.pcRelFixup = true;
116 }
117
buzbeee88dfbf2012-03-05 11:19:57 -0800118 /* Get the starting size of the instruction's template */
119 lir->flags.size = oatGetInsnSize(lir);
120
buzbee31a4a6f2012-02-28 15:36:15 -0800121 /* Set up the mask for resources that are updated */
122 if (flags & (IS_LOAD | IS_STORE)) {
123 /* Default to heap - will catch specialized classes later */
124 setMemRefType(lir, flags & IS_LOAD, kHeapRef);
125 }
126
127 /*
128 * Conservatively assume the branch here will call out a function that in
129 * turn will trash everything.
130 */
131 if (flags & IS_BRANCH) {
132 lir->defMask = lir->useMask = ENCODE_ALL;
133 return;
134 }
135
136 if (flags & REG_DEF0) {
137 setupRegMask(&lir->defMask, lir->operands[0]);
138 }
139
140 if (flags & REG_DEF1) {
141 setupRegMask(&lir->defMask, lir->operands[1]);
142 }
143
144 if (flags & REG_DEF_SP) {
145 lir->defMask |= ENCODE_REG_SP;
146 }
147
buzbeea7678db2012-03-05 15:35:46 -0800148#if !defined(TARGET_X86)
buzbee31a4a6f2012-02-28 15:36:15 -0800149 if (flags & REG_DEF_LR) {
150 lir->defMask |= ENCODE_REG_LR;
151 }
buzbeea7678db2012-03-05 15:35:46 -0800152#endif
buzbee31a4a6f2012-02-28 15:36:15 -0800153
154 if (flags & REG_DEF_LIST0) {
155 lir->defMask |= ENCODE_REG_LIST(lir->operands[0]);
156 }
157
158 if (flags & REG_DEF_LIST1) {
159 lir->defMask |= ENCODE_REG_LIST(lir->operands[1]);
160 }
161
buzbee5de34942012-03-01 14:51:57 -0800162#if defined(TARGET_ARM)
buzbee31a4a6f2012-02-28 15:36:15 -0800163 if (flags & REG_DEF_FPCS_LIST0) {
164 lir->defMask |= ENCODE_REG_FPCS_LIST(lir->operands[0]);
165 }
166
167 if (flags & REG_DEF_FPCS_LIST2) {
168 for (int i = 0; i < lir->operands[2]; i++) {
169 setupRegMask(&lir->defMask, lir->operands[1] + i);
170 }
171 }
buzbee5de34942012-03-01 14:51:57 -0800172#endif
buzbee31a4a6f2012-02-28 15:36:15 -0800173
174 if (flags & SETS_CCODES) {
175 lir->defMask |= ENCODE_CCODE;
176 }
177
178#if defined(TARGET_ARM)
179 /* Conservatively treat the IT block */
180 if (flags & IS_IT) {
181 lir->defMask = ENCODE_ALL;
182 }
183#endif
184
185 if (flags & (REG_USE0 | REG_USE1 | REG_USE2 | REG_USE3)) {
186 int i;
187
188 for (i = 0; i < 4; i++) {
189 if (flags & (1 << (kRegUse0 + i))) {
190 setupRegMask(&lir->useMask, lir->operands[i]);
191 }
192 }
193 }
194
buzbeea7678db2012-03-05 15:35:46 -0800195#if defined(TARGET_ARM)
buzbee31a4a6f2012-02-28 15:36:15 -0800196 if (flags & REG_USE_PC) {
197 lir->useMask |= ENCODE_REG_PC;
198 }
buzbeea7678db2012-03-05 15:35:46 -0800199#endif
buzbee31a4a6f2012-02-28 15:36:15 -0800200
201 if (flags & REG_USE_SP) {
202 lir->useMask |= ENCODE_REG_SP;
203 }
204
205 if (flags & REG_USE_LIST0) {
206 lir->useMask |= ENCODE_REG_LIST(lir->operands[0]);
207 }
208
209 if (flags & REG_USE_LIST1) {
210 lir->useMask |= ENCODE_REG_LIST(lir->operands[1]);
211 }
212
buzbee5de34942012-03-01 14:51:57 -0800213#if defined(TARGET_ARM)
buzbee31a4a6f2012-02-28 15:36:15 -0800214 if (flags & REG_USE_FPCS_LIST0) {
215 lir->useMask |= ENCODE_REG_FPCS_LIST(lir->operands[0]);
216 }
217
218 if (flags & REG_USE_FPCS_LIST2) {
219 for (int i = 0; i < lir->operands[2]; i++) {
220 setupRegMask(&lir->useMask, lir->operands[1] + i);
221 }
222 }
buzbee5de34942012-03-01 14:51:57 -0800223#endif
buzbee31a4a6f2012-02-28 15:36:15 -0800224
225 if (flags & USES_CCODES) {
226 lir->useMask |= ENCODE_CCODE;
227 }
228
229#if defined(TARGET_ARM)
230 /* Fixup for kThumbPush/lr and kThumbPop/pc */
231 if (opcode == kThumbPush || opcode == kThumbPop) {
232 u8 r8Mask = getRegMaskCommon(r8);
233 if ((opcode == kThumbPush) && (lir->useMask & r8Mask)) {
234 lir->useMask &= ~r8Mask;
235 lir->useMask |= ENCODE_REG_LR;
236 } else if ((opcode == kThumbPop) && (lir->defMask & r8Mask)) {
237 lir->defMask &= ~r8Mask;
238 lir->defMask |= ENCODE_REG_PC;
239 }
240 }
241#endif
242}
243
244/*
buzbee5de34942012-03-01 14:51:57 -0800245 * Debugging macros
246 */
247#define DUMP_RESOURCE_MASK(X)
248#define DUMP_SSA_REP(X)
249
250/* Pretty-print a LIR instruction */
251void oatDumpLIRInsn(CompilationUnit* cUnit, LIR* arg, unsigned char* baseAddr)
252{
253 LIR* lir = (LIR*) arg;
254 int offset = lir->offset;
255 int dest = lir->operands[0];
buzbee86a4bce2012-03-06 18:15:00 -0800256 const bool dumpNop = (cUnit->enableDebug & (1 << kDebugShowNops));
buzbee5de34942012-03-01 14:51:57 -0800257
258 /* Handle pseudo-ops individually, and all regular insns as a group */
259 switch(lir->opcode) {
260 case kPseudoMethodEntry:
261 LOG(INFO) << "-------- method entry " <<
262 PrettyMethod(cUnit->method_idx, *cUnit->dex_file);
263 break;
264 case kPseudoMethodExit:
265 LOG(INFO) << "-------- Method_Exit";
266 break;
267 case kPseudoBarrier:
268 LOG(INFO) << "-------- BARRIER";
269 break;
270 case kPseudoExtended:
271 LOG(INFO) << "-------- " << (char* ) dest;
272 break;
273 case kPseudoSSARep:
274 DUMP_SSA_REP(LOG(INFO) << "-------- kMirOpPhi: " << (char* ) dest);
275 break;
276 case kPseudoEntryBlock:
277 LOG(INFO) << "-------- entry offset: 0x" << std::hex << dest;
278 break;
279 case kPseudoDalvikByteCodeBoundary:
280 LOG(INFO) << "-------- dalvik offset: 0x" << std::hex <<
281 lir->dalvikOffset << " @ " << (char* )lir->operands[0];
282 break;
283 case kPseudoExitBlock:
284 LOG(INFO) << "-------- exit offset: 0x" << std::hex << dest;
285 break;
286 case kPseudoPseudoAlign4:
287 LOG(INFO) << (intptr_t)baseAddr + offset << " (0x" << std::hex <<
288 offset << "): .align4";
289 break;
290 case kPseudoEHBlockLabel:
291 LOG(INFO) << "Exception_Handling:";
292 break;
293 case kPseudoTargetLabel:
294 case kPseudoNormalBlockLabel:
295 LOG(INFO) << "L" << (intptr_t)lir << ":";
296 break;
297 case kPseudoThrowTarget:
298 LOG(INFO) << "LT" << (intptr_t)lir << ":";
299 break;
300 case kPseudoSuspendTarget:
301 LOG(INFO) << "LS" << (intptr_t)lir << ":";
302 break;
303 case kPseudoCaseLabel:
304 LOG(INFO) << "LC" << (intptr_t)lir << ": Case target 0x" <<
305 std::hex << lir->operands[0] << "|" << std::dec <<
306 lir->operands[0];
307 break;
308 default:
309 if (lir->flags.isNop && !dumpNop) {
310 break;
311 } else {
312 std::string op_name(buildInsnString(EncodingMap[lir->opcode].name, lir, baseAddr));
313 std::string op_operands(buildInsnString(EncodingMap[lir->opcode].fmt, lir, baseAddr));
buzbeebe003642012-03-02 15:28:37 -0800314 LOG(INFO) << StringPrintf("%05x: %-9s%s%s", (unsigned int)(baseAddr + offset),
buzbee5de34942012-03-01 14:51:57 -0800315 op_name.c_str(), op_operands.c_str(), lir->flags.isNop ? "(nop)" : "");
316 }
317 break;
318 }
319
320 if (lir->useMask && (!lir->flags.isNop || dumpNop)) {
321 DUMP_RESOURCE_MASK(oatDumpResourceMask((LIR* ) lir,
322 lir->useMask, "use"));
323 }
324 if (lir->defMask && (!lir->flags.isNop || dumpNop)) {
325 DUMP_RESOURCE_MASK(oatDumpResourceMask((LIR* ) lir,
326 lir->defMask, "def"));
327 }
328}
329
330void oatDumpPromotionMap(CompilationUnit *cUnit)
331{
332 for (int i = 0; i < cUnit->numDalvikRegisters; i++) {
333 PromotionMap vRegMap = cUnit->promotionMap[i];
334 char buf[100];
335 if (vRegMap.fpLocation == kLocPhysReg) {
336 snprintf(buf, 100, " : s%d", vRegMap.fpReg & FP_REG_MASK);
337 } else {
338 buf[0] = 0;
339 }
340 char buf2[100];
341 snprintf(buf2, 100, "V[%02d] -> %s%d%s", i,
342 vRegMap.coreLocation == kLocPhysReg ?
343 "r" : "SP+", vRegMap.coreLocation == kLocPhysReg ?
344 vRegMap.coreReg : oatSRegOffset(cUnit, i), buf);
345 LOG(INFO) << buf2;
346 }
347}
348
349void oatDumpFullPromotionMap(CompilationUnit *cUnit)
350{
351 for (int i = 0; i < cUnit->numDalvikRegisters; i++) {
352 PromotionMap vRegMap = cUnit->promotionMap[i];
353 LOG(INFO) << i << " -> " << "CL:" << (int)vRegMap.coreLocation <<
354 ", CR:" << (int)vRegMap.coreReg << ", FL:" <<
355 (int)vRegMap.fpLocation << ", FR:" << (int)vRegMap.fpReg <<
356 ", - " << (int)vRegMap.firstInPair;
357 }
358}
359
360/* Dump instructions and constant pool contents */
361void oatCodegenDump(CompilationUnit* cUnit)
362{
363 LOG(INFO) << "/*";
364 LOG(INFO) << "Dumping LIR insns for "
365 << PrettyMethod(cUnit->method_idx, *cUnit->dex_file);
366 LIR* lirInsn;
367 LIR* thisLIR;
368 int insnsSize = cUnit->insnsSize;
369
370 LOG(INFO) << "Regs (excluding ins) : " << cUnit->numRegs;
371 LOG(INFO) << "Ins : " << cUnit->numIns;
372 LOG(INFO) << "Outs : " << cUnit->numOuts;
373 LOG(INFO) << "CoreSpills : " << cUnit->numCoreSpills;
374 LOG(INFO) << "FPSpills : " << cUnit->numFPSpills;
375 LOG(INFO) << "Padding : " << cUnit->numPadding;
376 LOG(INFO) << "Frame size : " << cUnit->frameSize;
377 LOG(INFO) << "Start of ins : " << cUnit->insOffset;
378 LOG(INFO) << "Start of regs : " << cUnit->regsOffset;
379 LOG(INFO) << "code size is " << cUnit->totalSize <<
380 " bytes, Dalvik size is " << insnsSize * 2;
381 LOG(INFO) << "expansion factor: " <<
382 (float)cUnit->totalSize / (float)(insnsSize * 2);
383 oatDumpPromotionMap(cUnit);
384 for (lirInsn = cUnit->firstLIRInsn; lirInsn; lirInsn = lirInsn->next) {
385 oatDumpLIRInsn(cUnit, lirInsn, 0);
386 }
387 for (lirInsn = cUnit->classPointerList; lirInsn; lirInsn = lirInsn->next) {
388 thisLIR = (LIR*) lirInsn;
389 LOG(INFO) << StringPrintf("%x (%04x): .class (%s)",
390 thisLIR->offset, thisLIR->offset,
391 ((CallsiteInfo *) thisLIR->operands[0])->classDescriptor);
392 }
393 for (lirInsn = cUnit->literalList; lirInsn; lirInsn = lirInsn->next) {
394 thisLIR = (LIR*) lirInsn;
395 LOG(INFO) << StringPrintf("%x (%04x): .word (%#x)",
396 thisLIR->offset, thisLIR->offset, thisLIR->operands[0]);
397 }
398
399 const DexFile::MethodId& method_id =
400 cUnit->dex_file->GetMethodId(cUnit->method_idx);
401 std::string signature(cUnit->dex_file->GetMethodSignature(method_id));
402 std::string name(cUnit->dex_file->GetMethodName(method_id));
403 std::string descriptor(cUnit->dex_file->GetMethodDeclaringClassDescriptor(method_id));
404
405 // Dump mapping table
406 if (cUnit->mappingTable.size() > 0) {
407 std::string line(StringPrintf("\n MappingTable %s%s_%s_mappingTable[%zu] = {",
408 descriptor.c_str(), name.c_str(), signature.c_str(), cUnit->mappingTable.size()));
409 std::replace(line.begin(), line.end(), ';', '_');
410 LOG(INFO) << line;
411 for (uint32_t i = 0; i < cUnit->mappingTable.size(); i+=2) {
buzbee82488f52012-03-02 08:20:26 -0800412 line = StringPrintf(" {0x%05x, 0x%04x},",
buzbee5de34942012-03-01 14:51:57 -0800413 cUnit->mappingTable[i], cUnit->mappingTable[i+1]);
414 LOG(INFO) << line;
415 }
416 LOG(INFO) <<" };\n\n";
417 }
418}
419
buzbeea2ebdd72012-03-04 14:57:06 -0800420
421LIR* rawLIR(CompilationUnit* cUnit, int dalvikOffset, int opcode, int op0,
422 int op1, int op2, int op3, LIR* target)
423{
424 LIR* insn = (LIR* ) oatNew(cUnit, sizeof(LIR), true, kAllocLIR);
425 insn->dalvikOffset = dalvikOffset;
426 insn->opcode = opcode;
427 insn->operands[0] = op0;
428 insn->operands[1] = op1;
429 insn->operands[2] = op2;
430 insn->operands[3] = op3;
431 insn->target = target;
432 oatSetupResourceMasks(insn);
433 if (opcode == kPseudoTargetLabel) {
434 // Always make labels scheduling barriers
435 insn->defMask = ENCODE_ALL;
436 }
437 return insn;
438}
439
buzbee5de34942012-03-01 14:51:57 -0800440/*
buzbee31a4a6f2012-02-28 15:36:15 -0800441 * The following are building blocks to construct low-level IRs with 0 - 4
442 * operands.
443 */
buzbee5de34942012-03-01 14:51:57 -0800444LIR* newLIR0(CompilationUnit* cUnit, int opcode)
buzbee31a4a6f2012-02-28 15:36:15 -0800445{
buzbee31a4a6f2012-02-28 15:36:15 -0800446 DCHECK(isPseudoOpcode(opcode) || (EncodingMap[opcode].flags & NO_OPERAND));
buzbeea2ebdd72012-03-04 14:57:06 -0800447 LIR* insn = rawLIR(cUnit, cUnit->currentDalvikOffset, opcode);
buzbee31a4a6f2012-02-28 15:36:15 -0800448 oatAppendLIR(cUnit, (LIR*) insn);
449 return insn;
450}
451
buzbee5de34942012-03-01 14:51:57 -0800452LIR* newLIR1(CompilationUnit* cUnit, int opcode,
buzbee31a4a6f2012-02-28 15:36:15 -0800453 int dest)
454{
buzbee31a4a6f2012-02-28 15:36:15 -0800455 DCHECK(isPseudoOpcode(opcode) || (EncodingMap[opcode].flags & IS_UNARY_OP));
buzbeea2ebdd72012-03-04 14:57:06 -0800456 LIR* insn = rawLIR(cUnit, cUnit->currentDalvikOffset, opcode, dest);
buzbee31a4a6f2012-02-28 15:36:15 -0800457 oatAppendLIR(cUnit, (LIR*) insn);
458 return insn;
459}
460
buzbee5de34942012-03-01 14:51:57 -0800461LIR* newLIR2(CompilationUnit* cUnit, int opcode,
buzbee31a4a6f2012-02-28 15:36:15 -0800462 int dest, int src1)
463{
buzbee31a4a6f2012-02-28 15:36:15 -0800464 DCHECK(isPseudoOpcode(opcode) ||
465 (EncodingMap[opcode].flags & IS_BINARY_OP));
buzbeea2ebdd72012-03-04 14:57:06 -0800466 LIR* insn = rawLIR(cUnit, cUnit->currentDalvikOffset, opcode, dest, src1);
buzbee31a4a6f2012-02-28 15:36:15 -0800467 oatAppendLIR(cUnit, (LIR*) insn);
468 return insn;
469}
470
buzbee5de34942012-03-01 14:51:57 -0800471LIR* newLIR3(CompilationUnit* cUnit, int opcode,
buzbee31a4a6f2012-02-28 15:36:15 -0800472 int dest, int src1, int src2)
473{
buzbee31a4a6f2012-02-28 15:36:15 -0800474 DCHECK(isPseudoOpcode(opcode) ||
475 (EncodingMap[opcode].flags & IS_TERTIARY_OP))
476 << (int)opcode << " "
477 << PrettyMethod(cUnit->method_idx, *cUnit->dex_file) << " "
478 << cUnit->currentDalvikOffset;
buzbeea2ebdd72012-03-04 14:57:06 -0800479 LIR* insn = rawLIR(cUnit, cUnit->currentDalvikOffset, opcode, dest, src1,
480 src2);
buzbee31a4a6f2012-02-28 15:36:15 -0800481 oatAppendLIR(cUnit, (LIR*) insn);
482 return insn;
483}
484
buzbee5de34942012-03-01 14:51:57 -0800485LIR* newLIR4(CompilationUnit* cUnit, int opcode,
buzbee31a4a6f2012-02-28 15:36:15 -0800486 int dest, int src1, int src2, int info)
487{
buzbee31a4a6f2012-02-28 15:36:15 -0800488 DCHECK(isPseudoOpcode(opcode) ||
489 (EncodingMap[opcode].flags & IS_QUAD_OP));
buzbeea2ebdd72012-03-04 14:57:06 -0800490 LIR* insn = rawLIR(cUnit, cUnit->currentDalvikOffset, opcode, dest, src1,
491 src2, info);
buzbee31a4a6f2012-02-28 15:36:15 -0800492 oatAppendLIR(cUnit, (LIR*) insn);
493 return insn;
494}
buzbee31a4a6f2012-02-28 15:36:15 -0800495
496/*
497 * Search the existing constants in the literal pool for an exact or close match
498 * within specified delta (greater or equal to 0).
499 */
500LIR* scanLiteralPool(LIR* dataTarget, int value, unsigned int delta)
501{
502 while (dataTarget) {
503 if (((unsigned) (value - ((LIR* ) dataTarget)->operands[0])) <=
504 delta)
505 return (LIR* ) dataTarget;
506 dataTarget = dataTarget->next;
507 }
508 return NULL;
509}
510
511/* Search the existing constants in the literal pool for an exact wide match */
512LIR* scanLiteralPoolWide(LIR* dataTarget, int valLo, int valHi)
513{
514 bool loMatch = false;
515 LIR* loTarget = NULL;
516 while (dataTarget) {
517 if (loMatch && (((LIR*)dataTarget)->operands[0] == valHi)) {
518 return (LIR*)loTarget;
519 }
520 loMatch = false;
521 if (((LIR*)dataTarget)->operands[0] == valLo) {
522 loMatch = true;
523 loTarget = dataTarget;
524 }
525 dataTarget = dataTarget->next;
526 }
527 return NULL;
528}
529
530/*
531 * The following are building blocks to insert constants into the pool or
532 * instruction streams.
533 */
534
buzbee5de34942012-03-01 14:51:57 -0800535/* Add a 32-bit constant either in the constant pool */
buzbee31a4a6f2012-02-28 15:36:15 -0800536LIR* addWordData(CompilationUnit* cUnit, LIR* *constantListP,
537 int value)
538{
539 /* Add the constant to the literal pool */
540 if (constantListP) {
541 LIR* newValue = (LIR* ) oatNew(cUnit, sizeof(LIR), true,
542 kAllocData);
543 newValue->operands[0] = value;
544 newValue->next = *constantListP;
545 *constantListP = (LIR*) newValue;
546 return newValue;
buzbee31a4a6f2012-02-28 15:36:15 -0800547 }
548 return NULL;
549}
550
551/* Add a 64-bit constant to the constant pool or mixed with code */
552LIR* addWideData(CompilationUnit* cUnit, LIR* *constantListP,
553 int valLo, int valHi)
554{
buzbee31a4a6f2012-02-28 15:36:15 -0800555 //FIXME: hard-coded little endian, need BE variant
buzbee5de34942012-03-01 14:51:57 -0800556 // Insert high word into list first
557 addWordData(cUnit, constantListP, valHi);
558 return addWordData(cUnit, constantListP, valLo);
buzbee31a4a6f2012-02-28 15:36:15 -0800559}
560
561void pushWord(std::vector<uint16_t>&buf, int data) {
buzbeee3acd072012-02-25 17:03:10 -0800562 buf.push_back( data & 0xffff);
563 buf.push_back( (data >> 16) & 0xffff);
564}
565
566void alignBuffer(std::vector<uint16_t>&buf, size_t offset) {
567 while (buf.size() < (offset/2))
568 buf.push_back(0);
569}
570
571/* Write the literal pool to the output stream */
buzbee31a4a6f2012-02-28 15:36:15 -0800572void installLiteralPools(CompilationUnit* cUnit)
buzbeee3acd072012-02-25 17:03:10 -0800573{
574 alignBuffer(cUnit->codeBuffer, cUnit->dataOffset);
buzbee31a4a6f2012-02-28 15:36:15 -0800575 LIR* dataLIR = (LIR*) cUnit->literalList;
buzbeee3acd072012-02-25 17:03:10 -0800576 while (dataLIR != NULL) {
577 pushWord(cUnit->codeBuffer, dataLIR->operands[0]);
578 dataLIR = NEXT_LIR(dataLIR);
579 }
580}
581
582/* Write the switch tables to the output stream */
buzbee31a4a6f2012-02-28 15:36:15 -0800583void installSwitchTables(CompilationUnit* cUnit)
buzbeee3acd072012-02-25 17:03:10 -0800584{
585 GrowableListIterator iterator;
586 oatGrowableListIteratorInit(&cUnit->switchTables, &iterator);
587 while (true) {
588 SwitchTable* tabRec = (SwitchTable *) oatGrowableListIteratorNext(
589 &iterator);
590 if (tabRec == NULL) break;
591 alignBuffer(cUnit->codeBuffer, tabRec->offset);
buzbeec5159d52012-03-03 11:48:39 -0800592 /*
593 * For Arm, our reference point is the address of the bx
594 * instruction that does the launch, so we have to subtract
595 * the auto pc-advance. For other targets the reference point
596 * is a label, so we can use the offset as-is.
597 */
598#if defined(TARGET_ARM)
599 int bxOffset = tabRec->anchor->offset + 4;
600#else
601 int bxOffset = tabRec->anchor->offset;
602#endif
buzbeee3acd072012-02-25 17:03:10 -0800603 if (cUnit->printMe) {
604 LOG(INFO) << "Switch table for offset 0x" << std::hex << bxOffset;
605 }
Elliott Hughesadb8c672012-03-06 16:49:32 -0800606 if (tabRec->table[0] == Instruction::kSparseSwitchSignature) {
buzbeee3acd072012-02-25 17:03:10 -0800607 int* keys = (int*)&(tabRec->table[2]);
608 for (int elems = 0; elems < tabRec->table[1]; elems++) {
buzbee31a4a6f2012-02-28 15:36:15 -0800609 int disp = tabRec->targets[elems]->offset - bxOffset;
buzbeee3acd072012-02-25 17:03:10 -0800610 if (cUnit->printMe) {
611 LOG(INFO) << " Case[" << elems << "] key: 0x" <<
612 std::hex << keys[elems] << ", disp: 0x" <<
613 std::hex << disp;
614 }
615 pushWord(cUnit->codeBuffer, keys[elems]);
616 pushWord(cUnit->codeBuffer,
buzbee31a4a6f2012-02-28 15:36:15 -0800617 tabRec->targets[elems]->offset - bxOffset);
buzbeee3acd072012-02-25 17:03:10 -0800618 }
619 } else {
Elliott Hughesadb8c672012-03-06 16:49:32 -0800620 DCHECK_EQ(static_cast<int>(tabRec->table[0]), static_cast<int>(Instruction::kPackedSwitchSignature));
buzbeee3acd072012-02-25 17:03:10 -0800621 for (int elems = 0; elems < tabRec->table[1]; elems++) {
buzbee31a4a6f2012-02-28 15:36:15 -0800622 int disp = tabRec->targets[elems]->offset - bxOffset;
buzbeee3acd072012-02-25 17:03:10 -0800623 if (cUnit->printMe) {
624 LOG(INFO) << " Case[" << elems << "] disp: 0x" <<
625 std::hex << disp;
626 }
627 pushWord(cUnit->codeBuffer,
buzbee31a4a6f2012-02-28 15:36:15 -0800628 tabRec->targets[elems]->offset - bxOffset);
buzbeee3acd072012-02-25 17:03:10 -0800629 }
630 }
631 }
632}
633
634/* Write the fill array dta to the output stream */
buzbee31a4a6f2012-02-28 15:36:15 -0800635void installFillArrayData(CompilationUnit* cUnit)
buzbeee3acd072012-02-25 17:03:10 -0800636{
637 GrowableListIterator iterator;
638 oatGrowableListIteratorInit(&cUnit->fillArrayData, &iterator);
639 while (true) {
640 FillArrayData *tabRec = (FillArrayData *) oatGrowableListIteratorNext(
641 &iterator);
642 if (tabRec == NULL) break;
643 alignBuffer(cUnit->codeBuffer, tabRec->offset);
644 for (int i = 0; i < ((tabRec->size + 1) / 2) ; i++) {
645 cUnit->codeBuffer.push_back( tabRec->table[i]);
646 }
647 }
648}
649
buzbee31a4a6f2012-02-28 15:36:15 -0800650int assignLiteralOffsetCommon(LIR* lir, int offset)
buzbeee3acd072012-02-25 17:03:10 -0800651{
652 for (;lir != NULL; lir = lir->next) {
653 lir->offset = offset;
654 offset += 4;
655 }
656 return offset;
657}
658
buzbee31a4a6f2012-02-28 15:36:15 -0800659void createMappingTable(CompilationUnit* cUnit)
buzbeee3acd072012-02-25 17:03:10 -0800660{
buzbee31a4a6f2012-02-28 15:36:15 -0800661 LIR* tgtLIR;
buzbeee3acd072012-02-25 17:03:10 -0800662 int currentDalvikOffset = -1;
663
buzbee31a4a6f2012-02-28 15:36:15 -0800664 for (tgtLIR = (LIR *) cUnit->firstLIRInsn;
buzbeee3acd072012-02-25 17:03:10 -0800665 tgtLIR;
666 tgtLIR = NEXT_LIR(tgtLIR)) {
667 if ((tgtLIR->opcode >= 0) && !tgtLIR->flags.isNop &&
buzbee31a4a6f2012-02-28 15:36:15 -0800668 (currentDalvikOffset != tgtLIR->dalvikOffset)) {
buzbeee3acd072012-02-25 17:03:10 -0800669 // Changed - need to emit a record
buzbee31a4a6f2012-02-28 15:36:15 -0800670 cUnit->mappingTable.push_back(tgtLIR->offset);
671 cUnit->mappingTable.push_back(tgtLIR->dalvikOffset);
672 currentDalvikOffset = tgtLIR->dalvikOffset;
buzbeee3acd072012-02-25 17:03:10 -0800673 }
674 }
675}
676
677/* Determine the offset of each literal field */
buzbee31a4a6f2012-02-28 15:36:15 -0800678int assignLiteralOffset(CompilationUnit* cUnit, int offset)
buzbeee3acd072012-02-25 17:03:10 -0800679{
680 offset = assignLiteralOffsetCommon(cUnit->literalList, offset);
681 return offset;
682}
683
buzbee31a4a6f2012-02-28 15:36:15 -0800684int assignSwitchTablesOffset(CompilationUnit* cUnit, int offset)
buzbeee3acd072012-02-25 17:03:10 -0800685{
686 GrowableListIterator iterator;
687 oatGrowableListIteratorInit(&cUnit->switchTables, &iterator);
688 while (true) {
689 SwitchTable *tabRec = (SwitchTable *) oatGrowableListIteratorNext(
690 &iterator);
691 if (tabRec == NULL) break;
692 tabRec->offset = offset;
Elliott Hughesadb8c672012-03-06 16:49:32 -0800693 if (tabRec->table[0] == Instruction::kSparseSwitchSignature) {
buzbeee3acd072012-02-25 17:03:10 -0800694 offset += tabRec->table[1] * (sizeof(int) * 2);
695 } else {
Elliott Hughesadb8c672012-03-06 16:49:32 -0800696 DCHECK_EQ(static_cast<int>(tabRec->table[0]), static_cast<int>(Instruction::kPackedSwitchSignature));
buzbeee3acd072012-02-25 17:03:10 -0800697 offset += tabRec->table[1] * sizeof(int);
698 }
699 }
700 return offset;
701}
702
buzbee31a4a6f2012-02-28 15:36:15 -0800703int assignFillArrayDataOffset(CompilationUnit* cUnit, int offset)
buzbeee3acd072012-02-25 17:03:10 -0800704{
705 GrowableListIterator iterator;
706 oatGrowableListIteratorInit(&cUnit->fillArrayData, &iterator);
707 while (true) {
708 FillArrayData *tabRec = (FillArrayData *) oatGrowableListIteratorNext(
709 &iterator);
710 if (tabRec == NULL) break;
711 tabRec->offset = offset;
712 offset += tabRec->size;
713 // word align
714 offset = (offset + 3) & ~3;
715 }
716 return offset;
717}
718
719/*
720 * Walk the compilation unit and assign offsets to instructions
721 * and literals and compute the total size of the compiled unit.
722 */
723void oatAssignOffsets(CompilationUnit* cUnit)
724{
725 int offset = oatAssignInsnOffsets(cUnit);
726
727 /* Const values have to be word aligned */
728 offset = (offset + 3) & ~3;
729
730 /* Set up offsets for literals */
731 cUnit->dataOffset = offset;
732
733 offset = assignLiteralOffset(cUnit, offset);
734
735 offset = assignSwitchTablesOffset(cUnit, offset);
736
737 offset = assignFillArrayDataOffset(cUnit, offset);
738
739 cUnit->totalSize = offset;
740}
741
742/*
743 * Go over each instruction in the list and calculate the offset from the top
744 * before sending them off to the assembler. If out-of-range branch distance is
745 * seen rearrange the instructions a bit to correct it.
746 */
747void oatAssembleLIR(CompilationUnit* cUnit)
748{
749 oatAssignOffsets(cUnit);
750 /*
751 * Assemble here. Note that we generate code with optimistic assumptions
752 * and if found now to work, we'll have to redo the sequence and retry.
753 */
754
755 while (true) {
756 AssemblerStatus res = oatAssembleInstructions(cUnit, 0);
757 if (res == kSuccess) {
758 break;
759 } else {
760 cUnit->assemblerRetries++;
761 if (cUnit->assemblerRetries > MAX_ASSEMBLER_RETRIES) {
762 LOG(FATAL) << "Assembler error - too many retries";
763 }
764 // Redo offsets and try again
765 oatAssignOffsets(cUnit);
766 cUnit->codeBuffer.clear();
767 }
768 }
769
770 // Install literals
771 installLiteralPools(cUnit);
772
773 // Install switch tables
774 installSwitchTables(cUnit);
775
776 // Install fill array data
777 installFillArrayData(cUnit);
778
779 /*
780 * Create the mapping table
781 */
782 createMappingTable(cUnit);
783}
784
buzbee31a4a6f2012-02-28 15:36:15 -0800785/*
786 * Insert a kPseudoCaseLabel at the beginning of the Dalvik
787 * offset vaddr. This label will be used to fix up the case
788 * branch table during the assembly phase. Be sure to set
789 * all resource flags on this to prevent code motion across
790 * target boundaries. KeyVal is just there for debugging.
791 */
792LIR* insertCaseLabel(CompilationUnit* cUnit, int vaddr, int keyVal)
793{
794 std::map<unsigned int, LIR*>::iterator it;
795 it = cUnit->boundaryMap.find(vaddr);
796 if (it == cUnit->boundaryMap.end()) {
797 LOG(FATAL) << "Error: didn't find vaddr 0x" << std::hex << vaddr;
798 }
799 LIR* newLabel = (LIR*)oatNew(cUnit, sizeof(LIR), true, kAllocLIR);
800 newLabel->dalvikOffset = vaddr;
801 newLabel->opcode = kPseudoCaseLabel;
802 newLabel->operands[0] = keyVal;
803 oatInsertLIRAfter(it->second, (LIR*)newLabel);
804 return newLabel;
805}
806
807void markPackedCaseLabels(CompilationUnit* cUnit, SwitchTable *tabRec)
808{
809 const u2* table = tabRec->table;
810 int baseVaddr = tabRec->vaddr;
811 int *targets = (int*)&table[4];
812 int entries = table[1];
813 int lowKey = s4FromSwitchData(&table[2]);
814 for (int i = 0; i < entries; i++) {
815 tabRec->targets[i] = insertCaseLabel(cUnit, baseVaddr + targets[i],
816 i + lowKey);
817 }
818}
819
820void markSparseCaseLabels(CompilationUnit* cUnit, SwitchTable *tabRec)
821{
822 const u2* table = tabRec->table;
823 int baseVaddr = tabRec->vaddr;
824 int entries = table[1];
825 int* keys = (int*)&table[2];
826 int* targets = &keys[entries];
827 for (int i = 0; i < entries; i++) {
828 tabRec->targets[i] = insertCaseLabel(cUnit, baseVaddr + targets[i],
829 keys[i]);
830 }
831}
832
833void oatProcessSwitchTables(CompilationUnit* cUnit)
834{
835 GrowableListIterator iterator;
836 oatGrowableListIteratorInit(&cUnit->switchTables, &iterator);
837 while (true) {
838 SwitchTable *tabRec = (SwitchTable *) oatGrowableListIteratorNext(
839 &iterator);
840 if (tabRec == NULL) break;
Elliott Hughesadb8c672012-03-06 16:49:32 -0800841 if (tabRec->table[0] == Instruction::kPackedSwitchSignature) {
buzbee31a4a6f2012-02-28 15:36:15 -0800842 markPackedCaseLabels(cUnit, tabRec);
Elliott Hughesadb8c672012-03-06 16:49:32 -0800843 } else if (tabRec->table[0] == Instruction::kSparseSwitchSignature) {
buzbee31a4a6f2012-02-28 15:36:15 -0800844 markSparseCaseLabels(cUnit, tabRec);
Elliott Hughesadb8c672012-03-06 16:49:32 -0800845 } else {
buzbee31a4a6f2012-02-28 15:36:15 -0800846 LOG(FATAL) << "Invalid switch table";
847 }
848 }
849}
850
851//FIXME: Do we have endian issues here?
852
853void dumpSparseSwitchTable(const u2* table)
854 /*
855 * Sparse switch data format:
856 * ushort ident = 0x0200 magic value
857 * ushort size number of entries in the table; > 0
858 * int keys[size] keys, sorted low-to-high; 32-bit aligned
859 * int targets[size] branch targets, relative to switch opcode
860 *
861 * Total size is (2+size*4) 16-bit code units.
862 */
863{
864 u2 ident = table[0];
865 int entries = table[1];
866 int* keys = (int*)&table[2];
867 int* targets = &keys[entries];
868 LOG(INFO) << "Sparse switch table - ident:0x" << std::hex << ident <<
869 ", entries: " << std::dec << entries;
870 for (int i = 0; i < entries; i++) {
871 LOG(INFO) << " Key[" << keys[i] << "] -> 0x" << std::hex <<
872 targets[i];
873 }
874}
875
876void dumpPackedSwitchTable(const u2* table)
877 /*
878 * Packed switch data format:
879 * ushort ident = 0x0100 magic value
880 * ushort size number of entries in the table
881 * int first_key first (and lowest) switch case value
882 * int targets[size] branch targets, relative to switch opcode
883 *
884 * Total size is (4+size*2) 16-bit code units.
885 */
886{
887 u2 ident = table[0];
888 int* targets = (int*)&table[4];
889 int entries = table[1];
890 int lowKey = s4FromSwitchData(&table[2]);
891 LOG(INFO) << "Packed switch table - ident:0x" << std::hex << ident <<
892 ", entries: " << std::dec << entries << ", lowKey: " << lowKey;
893 for (int i = 0; i < entries; i++) {
894 LOG(INFO) << " Key[" << (i + lowKey) << "] -> 0x" << std::hex <<
895 targets[i];
896 }
897}
buzbeee3acd072012-02-25 17:03:10 -0800898
899
900} // namespace art