blob: 8a38db480ff30a84feb6e5b0bd5f54f33aa696c7 [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
118 /* Set up the mask for resources that are updated */
119 if (flags & (IS_LOAD | IS_STORE)) {
120 /* Default to heap - will catch specialized classes later */
121 setMemRefType(lir, flags & IS_LOAD, kHeapRef);
122 }
123
124 /*
125 * Conservatively assume the branch here will call out a function that in
126 * turn will trash everything.
127 */
128 if (flags & IS_BRANCH) {
129 lir->defMask = lir->useMask = ENCODE_ALL;
130 return;
131 }
132
133 if (flags & REG_DEF0) {
134 setupRegMask(&lir->defMask, lir->operands[0]);
135 }
136
137 if (flags & REG_DEF1) {
138 setupRegMask(&lir->defMask, lir->operands[1]);
139 }
140
141 if (flags & REG_DEF_SP) {
142 lir->defMask |= ENCODE_REG_SP;
143 }
144
145 if (flags & REG_DEF_LR) {
146 lir->defMask |= ENCODE_REG_LR;
147 }
148
149 if (flags & REG_DEF_LIST0) {
150 lir->defMask |= ENCODE_REG_LIST(lir->operands[0]);
151 }
152
153 if (flags & REG_DEF_LIST1) {
154 lir->defMask |= ENCODE_REG_LIST(lir->operands[1]);
155 }
156
buzbee5de34942012-03-01 14:51:57 -0800157#if defined(TARGET_ARM)
buzbee31a4a6f2012-02-28 15:36:15 -0800158 if (flags & REG_DEF_FPCS_LIST0) {
159 lir->defMask |= ENCODE_REG_FPCS_LIST(lir->operands[0]);
160 }
161
162 if (flags & REG_DEF_FPCS_LIST2) {
163 for (int i = 0; i < lir->operands[2]; i++) {
164 setupRegMask(&lir->defMask, lir->operands[1] + i);
165 }
166 }
buzbee5de34942012-03-01 14:51:57 -0800167#endif
buzbee31a4a6f2012-02-28 15:36:15 -0800168
169 if (flags & SETS_CCODES) {
170 lir->defMask |= ENCODE_CCODE;
171 }
172
173#if defined(TARGET_ARM)
174 /* Conservatively treat the IT block */
175 if (flags & IS_IT) {
176 lir->defMask = ENCODE_ALL;
177 }
178#endif
179
180 if (flags & (REG_USE0 | REG_USE1 | REG_USE2 | REG_USE3)) {
181 int i;
182
183 for (i = 0; i < 4; i++) {
184 if (flags & (1 << (kRegUse0 + i))) {
185 setupRegMask(&lir->useMask, lir->operands[i]);
186 }
187 }
188 }
189
190 if (flags & REG_USE_PC) {
191 lir->useMask |= ENCODE_REG_PC;
192 }
193
194 if (flags & REG_USE_SP) {
195 lir->useMask |= ENCODE_REG_SP;
196 }
197
198 if (flags & REG_USE_LIST0) {
199 lir->useMask |= ENCODE_REG_LIST(lir->operands[0]);
200 }
201
202 if (flags & REG_USE_LIST1) {
203 lir->useMask |= ENCODE_REG_LIST(lir->operands[1]);
204 }
205
buzbee5de34942012-03-01 14:51:57 -0800206#if defined(TARGET_ARM)
buzbee31a4a6f2012-02-28 15:36:15 -0800207 if (flags & REG_USE_FPCS_LIST0) {
208 lir->useMask |= ENCODE_REG_FPCS_LIST(lir->operands[0]);
209 }
210
211 if (flags & REG_USE_FPCS_LIST2) {
212 for (int i = 0; i < lir->operands[2]; i++) {
213 setupRegMask(&lir->useMask, lir->operands[1] + i);
214 }
215 }
buzbee5de34942012-03-01 14:51:57 -0800216#endif
buzbee31a4a6f2012-02-28 15:36:15 -0800217
218 if (flags & USES_CCODES) {
219 lir->useMask |= ENCODE_CCODE;
220 }
221
222#if defined(TARGET_ARM)
223 /* Fixup for kThumbPush/lr and kThumbPop/pc */
224 if (opcode == kThumbPush || opcode == kThumbPop) {
225 u8 r8Mask = getRegMaskCommon(r8);
226 if ((opcode == kThumbPush) && (lir->useMask & r8Mask)) {
227 lir->useMask &= ~r8Mask;
228 lir->useMask |= ENCODE_REG_LR;
229 } else if ((opcode == kThumbPop) && (lir->defMask & r8Mask)) {
230 lir->defMask &= ~r8Mask;
231 lir->defMask |= ENCODE_REG_PC;
232 }
233 }
234#endif
235}
236
237/*
buzbee5de34942012-03-01 14:51:57 -0800238 * Debugging macros
239 */
240#define DUMP_RESOURCE_MASK(X)
241#define DUMP_SSA_REP(X)
242
243/* Pretty-print a LIR instruction */
244void oatDumpLIRInsn(CompilationUnit* cUnit, LIR* arg, unsigned char* baseAddr)
245{
246 LIR* lir = (LIR*) arg;
247 int offset = lir->offset;
248 int dest = lir->operands[0];
249 const bool dumpNop = false;
250
251 /* Handle pseudo-ops individually, and all regular insns as a group */
252 switch(lir->opcode) {
253 case kPseudoMethodEntry:
254 LOG(INFO) << "-------- method entry " <<
255 PrettyMethod(cUnit->method_idx, *cUnit->dex_file);
256 break;
257 case kPseudoMethodExit:
258 LOG(INFO) << "-------- Method_Exit";
259 break;
260 case kPseudoBarrier:
261 LOG(INFO) << "-------- BARRIER";
262 break;
263 case kPseudoExtended:
264 LOG(INFO) << "-------- " << (char* ) dest;
265 break;
266 case kPseudoSSARep:
267 DUMP_SSA_REP(LOG(INFO) << "-------- kMirOpPhi: " << (char* ) dest);
268 break;
269 case kPseudoEntryBlock:
270 LOG(INFO) << "-------- entry offset: 0x" << std::hex << dest;
271 break;
272 case kPseudoDalvikByteCodeBoundary:
273 LOG(INFO) << "-------- dalvik offset: 0x" << std::hex <<
274 lir->dalvikOffset << " @ " << (char* )lir->operands[0];
275 break;
276 case kPseudoExitBlock:
277 LOG(INFO) << "-------- exit offset: 0x" << std::hex << dest;
278 break;
279 case kPseudoPseudoAlign4:
280 LOG(INFO) << (intptr_t)baseAddr + offset << " (0x" << std::hex <<
281 offset << "): .align4";
282 break;
283 case kPseudoEHBlockLabel:
284 LOG(INFO) << "Exception_Handling:";
285 break;
286 case kPseudoTargetLabel:
287 case kPseudoNormalBlockLabel:
288 LOG(INFO) << "L" << (intptr_t)lir << ":";
289 break;
290 case kPseudoThrowTarget:
291 LOG(INFO) << "LT" << (intptr_t)lir << ":";
292 break;
293 case kPseudoSuspendTarget:
294 LOG(INFO) << "LS" << (intptr_t)lir << ":";
295 break;
296 case kPseudoCaseLabel:
297 LOG(INFO) << "LC" << (intptr_t)lir << ": Case target 0x" <<
298 std::hex << lir->operands[0] << "|" << std::dec <<
299 lir->operands[0];
300 break;
301 default:
302 if (lir->flags.isNop && !dumpNop) {
303 break;
304 } else {
305 std::string op_name(buildInsnString(EncodingMap[lir->opcode].name, lir, baseAddr));
306 std::string op_operands(buildInsnString(EncodingMap[lir->opcode].fmt, lir, baseAddr));
buzbeebe003642012-03-02 15:28:37 -0800307 LOG(INFO) << StringPrintf("%05x: %-9s%s%s", (unsigned int)(baseAddr + offset),
buzbee5de34942012-03-01 14:51:57 -0800308 op_name.c_str(), op_operands.c_str(), lir->flags.isNop ? "(nop)" : "");
309 }
310 break;
311 }
312
313 if (lir->useMask && (!lir->flags.isNop || dumpNop)) {
314 DUMP_RESOURCE_MASK(oatDumpResourceMask((LIR* ) lir,
315 lir->useMask, "use"));
316 }
317 if (lir->defMask && (!lir->flags.isNop || dumpNop)) {
318 DUMP_RESOURCE_MASK(oatDumpResourceMask((LIR* ) lir,
319 lir->defMask, "def"));
320 }
321}
322
323void oatDumpPromotionMap(CompilationUnit *cUnit)
324{
325 for (int i = 0; i < cUnit->numDalvikRegisters; i++) {
326 PromotionMap vRegMap = cUnit->promotionMap[i];
327 char buf[100];
328 if (vRegMap.fpLocation == kLocPhysReg) {
329 snprintf(buf, 100, " : s%d", vRegMap.fpReg & FP_REG_MASK);
330 } else {
331 buf[0] = 0;
332 }
333 char buf2[100];
334 snprintf(buf2, 100, "V[%02d] -> %s%d%s", i,
335 vRegMap.coreLocation == kLocPhysReg ?
336 "r" : "SP+", vRegMap.coreLocation == kLocPhysReg ?
337 vRegMap.coreReg : oatSRegOffset(cUnit, i), buf);
338 LOG(INFO) << buf2;
339 }
340}
341
342void oatDumpFullPromotionMap(CompilationUnit *cUnit)
343{
344 for (int i = 0; i < cUnit->numDalvikRegisters; i++) {
345 PromotionMap vRegMap = cUnit->promotionMap[i];
346 LOG(INFO) << i << " -> " << "CL:" << (int)vRegMap.coreLocation <<
347 ", CR:" << (int)vRegMap.coreReg << ", FL:" <<
348 (int)vRegMap.fpLocation << ", FR:" << (int)vRegMap.fpReg <<
349 ", - " << (int)vRegMap.firstInPair;
350 }
351}
352
353/* Dump instructions and constant pool contents */
354void oatCodegenDump(CompilationUnit* cUnit)
355{
356 LOG(INFO) << "/*";
357 LOG(INFO) << "Dumping LIR insns for "
358 << PrettyMethod(cUnit->method_idx, *cUnit->dex_file);
359 LIR* lirInsn;
360 LIR* thisLIR;
361 int insnsSize = cUnit->insnsSize;
362
363 LOG(INFO) << "Regs (excluding ins) : " << cUnit->numRegs;
364 LOG(INFO) << "Ins : " << cUnit->numIns;
365 LOG(INFO) << "Outs : " << cUnit->numOuts;
366 LOG(INFO) << "CoreSpills : " << cUnit->numCoreSpills;
367 LOG(INFO) << "FPSpills : " << cUnit->numFPSpills;
368 LOG(INFO) << "Padding : " << cUnit->numPadding;
369 LOG(INFO) << "Frame size : " << cUnit->frameSize;
370 LOG(INFO) << "Start of ins : " << cUnit->insOffset;
371 LOG(INFO) << "Start of regs : " << cUnit->regsOffset;
372 LOG(INFO) << "code size is " << cUnit->totalSize <<
373 " bytes, Dalvik size is " << insnsSize * 2;
374 LOG(INFO) << "expansion factor: " <<
375 (float)cUnit->totalSize / (float)(insnsSize * 2);
376 oatDumpPromotionMap(cUnit);
377 for (lirInsn = cUnit->firstLIRInsn; lirInsn; lirInsn = lirInsn->next) {
378 oatDumpLIRInsn(cUnit, lirInsn, 0);
379 }
380 for (lirInsn = cUnit->classPointerList; lirInsn; lirInsn = lirInsn->next) {
381 thisLIR = (LIR*) lirInsn;
382 LOG(INFO) << StringPrintf("%x (%04x): .class (%s)",
383 thisLIR->offset, thisLIR->offset,
384 ((CallsiteInfo *) thisLIR->operands[0])->classDescriptor);
385 }
386 for (lirInsn = cUnit->literalList; lirInsn; lirInsn = lirInsn->next) {
387 thisLIR = (LIR*) lirInsn;
388 LOG(INFO) << StringPrintf("%x (%04x): .word (%#x)",
389 thisLIR->offset, thisLIR->offset, thisLIR->operands[0]);
390 }
391
392 const DexFile::MethodId& method_id =
393 cUnit->dex_file->GetMethodId(cUnit->method_idx);
394 std::string signature(cUnit->dex_file->GetMethodSignature(method_id));
395 std::string name(cUnit->dex_file->GetMethodName(method_id));
396 std::string descriptor(cUnit->dex_file->GetMethodDeclaringClassDescriptor(method_id));
397
398 // Dump mapping table
399 if (cUnit->mappingTable.size() > 0) {
400 std::string line(StringPrintf("\n MappingTable %s%s_%s_mappingTable[%zu] = {",
401 descriptor.c_str(), name.c_str(), signature.c_str(), cUnit->mappingTable.size()));
402 std::replace(line.begin(), line.end(), ';', '_');
403 LOG(INFO) << line;
404 for (uint32_t i = 0; i < cUnit->mappingTable.size(); i+=2) {
buzbee82488f52012-03-02 08:20:26 -0800405 line = StringPrintf(" {0x%05x, 0x%04x},",
buzbee5de34942012-03-01 14:51:57 -0800406 cUnit->mappingTable[i], cUnit->mappingTable[i+1]);
407 LOG(INFO) << line;
408 }
409 LOG(INFO) <<" };\n\n";
410 }
411}
412
413/*
buzbee31a4a6f2012-02-28 15:36:15 -0800414 * The following are building blocks to construct low-level IRs with 0 - 4
415 * operands.
416 */
buzbee5de34942012-03-01 14:51:57 -0800417LIR* newLIR0(CompilationUnit* cUnit, int opcode)
buzbee31a4a6f2012-02-28 15:36:15 -0800418{
419 LIR* insn = (LIR* ) oatNew(cUnit, sizeof(LIR), true, kAllocLIR);
420 DCHECK(isPseudoOpcode(opcode) || (EncodingMap[opcode].flags & NO_OPERAND));
421 insn->opcode = opcode;
422 setupResourceMasks(insn);
423 insn->dalvikOffset = cUnit->currentDalvikOffset;
buzbeec5159d52012-03-03 11:48:39 -0800424 if (opcode == kPseudoTargetLabel) {
425 // Always make labels scheduling barriers
426 insn->defMask = ENCODE_ALL;
427 }
buzbee31a4a6f2012-02-28 15:36:15 -0800428 oatAppendLIR(cUnit, (LIR*) insn);
429 return insn;
430}
431
buzbee5de34942012-03-01 14:51:57 -0800432LIR* newLIR1(CompilationUnit* cUnit, int opcode,
buzbee31a4a6f2012-02-28 15:36:15 -0800433 int dest)
434{
435 LIR* insn = (LIR* ) oatNew(cUnit, sizeof(LIR), true, kAllocLIR);
436 DCHECK(isPseudoOpcode(opcode) || (EncodingMap[opcode].flags & IS_UNARY_OP));
437 insn->opcode = opcode;
438 insn->operands[0] = dest;
439 setupResourceMasks(insn);
440 insn->dalvikOffset = cUnit->currentDalvikOffset;
441 oatAppendLIR(cUnit, (LIR*) insn);
442 return insn;
443}
444
buzbee5de34942012-03-01 14:51:57 -0800445LIR* newLIR2(CompilationUnit* cUnit, int opcode,
buzbee31a4a6f2012-02-28 15:36:15 -0800446 int dest, int src1)
447{
448 LIR* insn = (LIR* ) oatNew(cUnit, sizeof(LIR), true, kAllocLIR);
449 DCHECK(isPseudoOpcode(opcode) ||
450 (EncodingMap[opcode].flags & IS_BINARY_OP));
451 insn->opcode = opcode;
452 insn->operands[0] = dest;
453 insn->operands[1] = src1;
454 setupResourceMasks(insn);
455 insn->dalvikOffset = cUnit->currentDalvikOffset;
456 oatAppendLIR(cUnit, (LIR*) insn);
457 return insn;
458}
459
buzbee5de34942012-03-01 14:51:57 -0800460LIR* newLIR3(CompilationUnit* cUnit, int opcode,
buzbee31a4a6f2012-02-28 15:36:15 -0800461 int dest, int src1, int src2)
462{
463 LIR* insn = (LIR* ) oatNew(cUnit, sizeof(LIR), true, kAllocLIR);
464 DCHECK(isPseudoOpcode(opcode) ||
465 (EncodingMap[opcode].flags & IS_TERTIARY_OP))
466 << (int)opcode << " "
467 << PrettyMethod(cUnit->method_idx, *cUnit->dex_file) << " "
468 << cUnit->currentDalvikOffset;
469 insn->opcode = opcode;
470 insn->operands[0] = dest;
471 insn->operands[1] = src1;
472 insn->operands[2] = src2;
473 setupResourceMasks(insn);
474 insn->dalvikOffset = cUnit->currentDalvikOffset;
475 oatAppendLIR(cUnit, (LIR*) insn);
476 return insn;
477}
478
buzbee5de34942012-03-01 14:51:57 -0800479LIR* newLIR4(CompilationUnit* cUnit, int opcode,
buzbee31a4a6f2012-02-28 15:36:15 -0800480 int dest, int src1, int src2, int info)
481{
482 LIR* insn = (LIR* ) oatNew(cUnit, sizeof(LIR), true, kAllocLIR);
483 DCHECK(isPseudoOpcode(opcode) ||
484 (EncodingMap[opcode].flags & IS_QUAD_OP));
485 insn->opcode = opcode;
486 insn->operands[0] = dest;
487 insn->operands[1] = src1;
488 insn->operands[2] = src2;
489 insn->operands[3] = info;
490 setupResourceMasks(insn);
491 insn->dalvikOffset = cUnit->currentDalvikOffset;
492 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 }
606 if (tabRec->table[0] == kSparseSwitchSignature) {
607 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 {
620 DCHECK_EQ(tabRec->table[0], kPackedSwitchSignature);
621 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;
693 if (tabRec->table[0] == kSparseSwitchSignature) {
694 offset += tabRec->table[1] * (sizeof(int) * 2);
695 } else {
696 DCHECK_EQ(tabRec->table[0], kPackedSwitchSignature);
697 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;
841 if (tabRec->table[0] == kPackedSwitchSignature)
842 markPackedCaseLabels(cUnit, tabRec);
843 else if (tabRec->table[0] == kSparseSwitchSignature)
844 markSparseCaseLabels(cUnit, tabRec);
845 else {
846 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