blob: cc61f78a3954f82fbba067b8e251c7b59660b69e [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;
424 oatAppendLIR(cUnit, (LIR*) insn);
425 return insn;
426}
427
buzbee5de34942012-03-01 14:51:57 -0800428LIR* newLIR1(CompilationUnit* cUnit, int opcode,
buzbee31a4a6f2012-02-28 15:36:15 -0800429 int dest)
430{
431 LIR* insn = (LIR* ) oatNew(cUnit, sizeof(LIR), true, kAllocLIR);
432 DCHECK(isPseudoOpcode(opcode) || (EncodingMap[opcode].flags & IS_UNARY_OP));
433 insn->opcode = opcode;
434 insn->operands[0] = dest;
435 setupResourceMasks(insn);
436 insn->dalvikOffset = cUnit->currentDalvikOffset;
437 oatAppendLIR(cUnit, (LIR*) insn);
438 return insn;
439}
440
buzbee5de34942012-03-01 14:51:57 -0800441LIR* newLIR2(CompilationUnit* cUnit, int opcode,
buzbee31a4a6f2012-02-28 15:36:15 -0800442 int dest, int src1)
443{
444 LIR* insn = (LIR* ) oatNew(cUnit, sizeof(LIR), true, kAllocLIR);
445 DCHECK(isPseudoOpcode(opcode) ||
446 (EncodingMap[opcode].flags & IS_BINARY_OP));
447 insn->opcode = opcode;
448 insn->operands[0] = dest;
449 insn->operands[1] = src1;
450 setupResourceMasks(insn);
451 insn->dalvikOffset = cUnit->currentDalvikOffset;
452 oatAppendLIR(cUnit, (LIR*) insn);
453 return insn;
454}
455
buzbee5de34942012-03-01 14:51:57 -0800456LIR* newLIR3(CompilationUnit* cUnit, int opcode,
buzbee31a4a6f2012-02-28 15:36:15 -0800457 int dest, int src1, int src2)
458{
459 LIR* insn = (LIR* ) oatNew(cUnit, sizeof(LIR), true, kAllocLIR);
460 DCHECK(isPseudoOpcode(opcode) ||
461 (EncodingMap[opcode].flags & IS_TERTIARY_OP))
462 << (int)opcode << " "
463 << PrettyMethod(cUnit->method_idx, *cUnit->dex_file) << " "
464 << cUnit->currentDalvikOffset;
465 insn->opcode = opcode;
466 insn->operands[0] = dest;
467 insn->operands[1] = src1;
468 insn->operands[2] = src2;
469 setupResourceMasks(insn);
470 insn->dalvikOffset = cUnit->currentDalvikOffset;
471 oatAppendLIR(cUnit, (LIR*) insn);
472 return insn;
473}
474
475#if defined(TARGET_ARM)
buzbee5de34942012-03-01 14:51:57 -0800476LIR* newLIR4(CompilationUnit* cUnit, int opcode,
buzbee31a4a6f2012-02-28 15:36:15 -0800477 int dest, int src1, int src2, int info)
478{
479 LIR* insn = (LIR* ) oatNew(cUnit, sizeof(LIR), true, kAllocLIR);
480 DCHECK(isPseudoOpcode(opcode) ||
481 (EncodingMap[opcode].flags & IS_QUAD_OP));
482 insn->opcode = opcode;
483 insn->operands[0] = dest;
484 insn->operands[1] = src1;
485 insn->operands[2] = src2;
486 insn->operands[3] = info;
487 setupResourceMasks(insn);
488 insn->dalvikOffset = cUnit->currentDalvikOffset;
489 oatAppendLIR(cUnit, (LIR*) insn);
490 return insn;
491}
492#endif
493
494/*
495 * Search the existing constants in the literal pool for an exact or close match
496 * within specified delta (greater or equal to 0).
497 */
498LIR* scanLiteralPool(LIR* dataTarget, int value, unsigned int delta)
499{
500 while (dataTarget) {
501 if (((unsigned) (value - ((LIR* ) dataTarget)->operands[0])) <=
502 delta)
503 return (LIR* ) dataTarget;
504 dataTarget = dataTarget->next;
505 }
506 return NULL;
507}
508
509/* Search the existing constants in the literal pool for an exact wide match */
510LIR* scanLiteralPoolWide(LIR* dataTarget, int valLo, int valHi)
511{
512 bool loMatch = false;
513 LIR* loTarget = NULL;
514 while (dataTarget) {
515 if (loMatch && (((LIR*)dataTarget)->operands[0] == valHi)) {
516 return (LIR*)loTarget;
517 }
518 loMatch = false;
519 if (((LIR*)dataTarget)->operands[0] == valLo) {
520 loMatch = true;
521 loTarget = dataTarget;
522 }
523 dataTarget = dataTarget->next;
524 }
525 return NULL;
526}
527
528/*
529 * The following are building blocks to insert constants into the pool or
530 * instruction streams.
531 */
532
buzbee5de34942012-03-01 14:51:57 -0800533/* Add a 32-bit constant either in the constant pool */
buzbee31a4a6f2012-02-28 15:36:15 -0800534LIR* addWordData(CompilationUnit* cUnit, LIR* *constantListP,
535 int value)
536{
537 /* Add the constant to the literal pool */
538 if (constantListP) {
539 LIR* newValue = (LIR* ) oatNew(cUnit, sizeof(LIR), true,
540 kAllocData);
541 newValue->operands[0] = value;
542 newValue->next = *constantListP;
543 *constantListP = (LIR*) newValue;
544 return newValue;
buzbee31a4a6f2012-02-28 15:36:15 -0800545 }
546 return NULL;
547}
548
549/* Add a 64-bit constant to the constant pool or mixed with code */
550LIR* addWideData(CompilationUnit* cUnit, LIR* *constantListP,
551 int valLo, int valHi)
552{
buzbee31a4a6f2012-02-28 15:36:15 -0800553 //FIXME: hard-coded little endian, need BE variant
buzbee5de34942012-03-01 14:51:57 -0800554 // Insert high word into list first
555 addWordData(cUnit, constantListP, valHi);
556 return addWordData(cUnit, constantListP, valLo);
buzbee31a4a6f2012-02-28 15:36:15 -0800557}
558
559void pushWord(std::vector<uint16_t>&buf, int data) {
buzbeee3acd072012-02-25 17:03:10 -0800560 buf.push_back( data & 0xffff);
561 buf.push_back( (data >> 16) & 0xffff);
562}
563
564void alignBuffer(std::vector<uint16_t>&buf, size_t offset) {
565 while (buf.size() < (offset/2))
566 buf.push_back(0);
567}
568
569/* Write the literal pool to the output stream */
buzbee31a4a6f2012-02-28 15:36:15 -0800570void installLiteralPools(CompilationUnit* cUnit)
buzbeee3acd072012-02-25 17:03:10 -0800571{
572 alignBuffer(cUnit->codeBuffer, cUnit->dataOffset);
buzbee31a4a6f2012-02-28 15:36:15 -0800573 LIR* dataLIR = (LIR*) cUnit->literalList;
buzbeee3acd072012-02-25 17:03:10 -0800574 while (dataLIR != NULL) {
575 pushWord(cUnit->codeBuffer, dataLIR->operands[0]);
576 dataLIR = NEXT_LIR(dataLIR);
577 }
578}
579
580/* Write the switch tables to the output stream */
buzbee31a4a6f2012-02-28 15:36:15 -0800581void installSwitchTables(CompilationUnit* cUnit)
buzbeee3acd072012-02-25 17:03:10 -0800582{
583 GrowableListIterator iterator;
584 oatGrowableListIteratorInit(&cUnit->switchTables, &iterator);
585 while (true) {
586 SwitchTable* tabRec = (SwitchTable *) oatGrowableListIteratorNext(
587 &iterator);
588 if (tabRec == NULL) break;
589 alignBuffer(cUnit->codeBuffer, tabRec->offset);
buzbee31a4a6f2012-02-28 15:36:15 -0800590 int bxOffset = tabRec->bxInst->offset + 4;
buzbeee3acd072012-02-25 17:03:10 -0800591 if (cUnit->printMe) {
592 LOG(INFO) << "Switch table for offset 0x" << std::hex << bxOffset;
593 }
594 if (tabRec->table[0] == kSparseSwitchSignature) {
595 int* keys = (int*)&(tabRec->table[2]);
596 for (int elems = 0; elems < tabRec->table[1]; elems++) {
buzbee31a4a6f2012-02-28 15:36:15 -0800597 int disp = tabRec->targets[elems]->offset - bxOffset;
buzbeee3acd072012-02-25 17:03:10 -0800598 if (cUnit->printMe) {
599 LOG(INFO) << " Case[" << elems << "] key: 0x" <<
600 std::hex << keys[elems] << ", disp: 0x" <<
601 std::hex << disp;
602 }
603 pushWord(cUnit->codeBuffer, keys[elems]);
604 pushWord(cUnit->codeBuffer,
buzbee31a4a6f2012-02-28 15:36:15 -0800605 tabRec->targets[elems]->offset - bxOffset);
buzbeee3acd072012-02-25 17:03:10 -0800606 }
607 } else {
608 DCHECK_EQ(tabRec->table[0], kPackedSwitchSignature);
609 for (int elems = 0; elems < tabRec->table[1]; elems++) {
buzbee31a4a6f2012-02-28 15:36:15 -0800610 int disp = tabRec->targets[elems]->offset - bxOffset;
buzbeee3acd072012-02-25 17:03:10 -0800611 if (cUnit->printMe) {
612 LOG(INFO) << " Case[" << elems << "] disp: 0x" <<
613 std::hex << disp;
614 }
615 pushWord(cUnit->codeBuffer,
buzbee31a4a6f2012-02-28 15:36:15 -0800616 tabRec->targets[elems]->offset - bxOffset);
buzbeee3acd072012-02-25 17:03:10 -0800617 }
618 }
619 }
620}
621
622/* Write the fill array dta to the output stream */
buzbee31a4a6f2012-02-28 15:36:15 -0800623void installFillArrayData(CompilationUnit* cUnit)
buzbeee3acd072012-02-25 17:03:10 -0800624{
625 GrowableListIterator iterator;
626 oatGrowableListIteratorInit(&cUnit->fillArrayData, &iterator);
627 while (true) {
628 FillArrayData *tabRec = (FillArrayData *) oatGrowableListIteratorNext(
629 &iterator);
630 if (tabRec == NULL) break;
631 alignBuffer(cUnit->codeBuffer, tabRec->offset);
632 for (int i = 0; i < ((tabRec->size + 1) / 2) ; i++) {
633 cUnit->codeBuffer.push_back( tabRec->table[i]);
634 }
635 }
636}
637
buzbee31a4a6f2012-02-28 15:36:15 -0800638int assignLiteralOffsetCommon(LIR* lir, int offset)
buzbeee3acd072012-02-25 17:03:10 -0800639{
640 for (;lir != NULL; lir = lir->next) {
641 lir->offset = offset;
642 offset += 4;
643 }
644 return offset;
645}
646
buzbee31a4a6f2012-02-28 15:36:15 -0800647void createMappingTable(CompilationUnit* cUnit)
buzbeee3acd072012-02-25 17:03:10 -0800648{
buzbee31a4a6f2012-02-28 15:36:15 -0800649 LIR* tgtLIR;
buzbeee3acd072012-02-25 17:03:10 -0800650 int currentDalvikOffset = -1;
651
buzbee31a4a6f2012-02-28 15:36:15 -0800652 for (tgtLIR = (LIR *) cUnit->firstLIRInsn;
buzbeee3acd072012-02-25 17:03:10 -0800653 tgtLIR;
654 tgtLIR = NEXT_LIR(tgtLIR)) {
655 if ((tgtLIR->opcode >= 0) && !tgtLIR->flags.isNop &&
buzbee31a4a6f2012-02-28 15:36:15 -0800656 (currentDalvikOffset != tgtLIR->dalvikOffset)) {
buzbeee3acd072012-02-25 17:03:10 -0800657 // Changed - need to emit a record
buzbee31a4a6f2012-02-28 15:36:15 -0800658 cUnit->mappingTable.push_back(tgtLIR->offset);
659 cUnit->mappingTable.push_back(tgtLIR->dalvikOffset);
660 currentDalvikOffset = tgtLIR->dalvikOffset;
buzbeee3acd072012-02-25 17:03:10 -0800661 }
662 }
663}
664
665/* Determine the offset of each literal field */
buzbee31a4a6f2012-02-28 15:36:15 -0800666int assignLiteralOffset(CompilationUnit* cUnit, int offset)
buzbeee3acd072012-02-25 17:03:10 -0800667{
668 offset = assignLiteralOffsetCommon(cUnit->literalList, offset);
669 return offset;
670}
671
buzbee31a4a6f2012-02-28 15:36:15 -0800672int assignSwitchTablesOffset(CompilationUnit* cUnit, int offset)
buzbeee3acd072012-02-25 17:03:10 -0800673{
674 GrowableListIterator iterator;
675 oatGrowableListIteratorInit(&cUnit->switchTables, &iterator);
676 while (true) {
677 SwitchTable *tabRec = (SwitchTable *) oatGrowableListIteratorNext(
678 &iterator);
679 if (tabRec == NULL) break;
680 tabRec->offset = offset;
681 if (tabRec->table[0] == kSparseSwitchSignature) {
682 offset += tabRec->table[1] * (sizeof(int) * 2);
683 } else {
684 DCHECK_EQ(tabRec->table[0], kPackedSwitchSignature);
685 offset += tabRec->table[1] * sizeof(int);
686 }
687 }
688 return offset;
689}
690
buzbee31a4a6f2012-02-28 15:36:15 -0800691int assignFillArrayDataOffset(CompilationUnit* cUnit, int offset)
buzbeee3acd072012-02-25 17:03:10 -0800692{
693 GrowableListIterator iterator;
694 oatGrowableListIteratorInit(&cUnit->fillArrayData, &iterator);
695 while (true) {
696 FillArrayData *tabRec = (FillArrayData *) oatGrowableListIteratorNext(
697 &iterator);
698 if (tabRec == NULL) break;
699 tabRec->offset = offset;
700 offset += tabRec->size;
701 // word align
702 offset = (offset + 3) & ~3;
703 }
704 return offset;
705}
706
707/*
708 * Walk the compilation unit and assign offsets to instructions
709 * and literals and compute the total size of the compiled unit.
710 */
711void oatAssignOffsets(CompilationUnit* cUnit)
712{
713 int offset = oatAssignInsnOffsets(cUnit);
714
715 /* Const values have to be word aligned */
716 offset = (offset + 3) & ~3;
717
718 /* Set up offsets for literals */
719 cUnit->dataOffset = offset;
720
721 offset = assignLiteralOffset(cUnit, offset);
722
723 offset = assignSwitchTablesOffset(cUnit, offset);
724
725 offset = assignFillArrayDataOffset(cUnit, offset);
726
727 cUnit->totalSize = offset;
728}
729
730/*
731 * Go over each instruction in the list and calculate the offset from the top
732 * before sending them off to the assembler. If out-of-range branch distance is
733 * seen rearrange the instructions a bit to correct it.
734 */
735void oatAssembleLIR(CompilationUnit* cUnit)
736{
737 oatAssignOffsets(cUnit);
738 /*
739 * Assemble here. Note that we generate code with optimistic assumptions
740 * and if found now to work, we'll have to redo the sequence and retry.
741 */
742
743 while (true) {
744 AssemblerStatus res = oatAssembleInstructions(cUnit, 0);
745 if (res == kSuccess) {
746 break;
747 } else {
748 cUnit->assemblerRetries++;
749 if (cUnit->assemblerRetries > MAX_ASSEMBLER_RETRIES) {
750 LOG(FATAL) << "Assembler error - too many retries";
751 }
752 // Redo offsets and try again
753 oatAssignOffsets(cUnit);
754 cUnit->codeBuffer.clear();
755 }
756 }
757
758 // Install literals
759 installLiteralPools(cUnit);
760
761 // Install switch tables
762 installSwitchTables(cUnit);
763
764 // Install fill array data
765 installFillArrayData(cUnit);
766
767 /*
768 * Create the mapping table
769 */
770 createMappingTable(cUnit);
771}
772
buzbee31a4a6f2012-02-28 15:36:15 -0800773/*
774 * Insert a kPseudoCaseLabel at the beginning of the Dalvik
775 * offset vaddr. This label will be used to fix up the case
776 * branch table during the assembly phase. Be sure to set
777 * all resource flags on this to prevent code motion across
778 * target boundaries. KeyVal is just there for debugging.
779 */
780LIR* insertCaseLabel(CompilationUnit* cUnit, int vaddr, int keyVal)
781{
782 std::map<unsigned int, LIR*>::iterator it;
783 it = cUnit->boundaryMap.find(vaddr);
784 if (it == cUnit->boundaryMap.end()) {
785 LOG(FATAL) << "Error: didn't find vaddr 0x" << std::hex << vaddr;
786 }
787 LIR* newLabel = (LIR*)oatNew(cUnit, sizeof(LIR), true, kAllocLIR);
788 newLabel->dalvikOffset = vaddr;
789 newLabel->opcode = kPseudoCaseLabel;
790 newLabel->operands[0] = keyVal;
791 oatInsertLIRAfter(it->second, (LIR*)newLabel);
792 return newLabel;
793}
794
795void markPackedCaseLabels(CompilationUnit* cUnit, SwitchTable *tabRec)
796{
797 const u2* table = tabRec->table;
798 int baseVaddr = tabRec->vaddr;
799 int *targets = (int*)&table[4];
800 int entries = table[1];
801 int lowKey = s4FromSwitchData(&table[2]);
802 for (int i = 0; i < entries; i++) {
803 tabRec->targets[i] = insertCaseLabel(cUnit, baseVaddr + targets[i],
804 i + lowKey);
805 }
806}
807
808void markSparseCaseLabels(CompilationUnit* cUnit, SwitchTable *tabRec)
809{
810 const u2* table = tabRec->table;
811 int baseVaddr = tabRec->vaddr;
812 int entries = table[1];
813 int* keys = (int*)&table[2];
814 int* targets = &keys[entries];
815 for (int i = 0; i < entries; i++) {
816 tabRec->targets[i] = insertCaseLabel(cUnit, baseVaddr + targets[i],
817 keys[i]);
818 }
819}
820
821void oatProcessSwitchTables(CompilationUnit* cUnit)
822{
823 GrowableListIterator iterator;
824 oatGrowableListIteratorInit(&cUnit->switchTables, &iterator);
825 while (true) {
826 SwitchTable *tabRec = (SwitchTable *) oatGrowableListIteratorNext(
827 &iterator);
828 if (tabRec == NULL) break;
829 if (tabRec->table[0] == kPackedSwitchSignature)
830 markPackedCaseLabels(cUnit, tabRec);
831 else if (tabRec->table[0] == kSparseSwitchSignature)
832 markSparseCaseLabels(cUnit, tabRec);
833 else {
834 LOG(FATAL) << "Invalid switch table";
835 }
836 }
837}
838
839//FIXME: Do we have endian issues here?
840
841void dumpSparseSwitchTable(const u2* table)
842 /*
843 * Sparse switch data format:
844 * ushort ident = 0x0200 magic value
845 * ushort size number of entries in the table; > 0
846 * int keys[size] keys, sorted low-to-high; 32-bit aligned
847 * int targets[size] branch targets, relative to switch opcode
848 *
849 * Total size is (2+size*4) 16-bit code units.
850 */
851{
852 u2 ident = table[0];
853 int entries = table[1];
854 int* keys = (int*)&table[2];
855 int* targets = &keys[entries];
856 LOG(INFO) << "Sparse switch table - ident:0x" << std::hex << ident <<
857 ", entries: " << std::dec << entries;
858 for (int i = 0; i < entries; i++) {
859 LOG(INFO) << " Key[" << keys[i] << "] -> 0x" << std::hex <<
860 targets[i];
861 }
862}
863
864void dumpPackedSwitchTable(const u2* table)
865 /*
866 * Packed switch data format:
867 * ushort ident = 0x0100 magic value
868 * ushort size number of entries in the table
869 * int first_key first (and lowest) switch case value
870 * int targets[size] branch targets, relative to switch opcode
871 *
872 * Total size is (4+size*2) 16-bit code units.
873 */
874{
875 u2 ident = table[0];
876 int* targets = (int*)&table[4];
877 int entries = table[1];
878 int lowKey = s4FromSwitchData(&table[2]);
879 LOG(INFO) << "Packed switch table - ident:0x" << std::hex << ident <<
880 ", entries: " << std::dec << entries << ", lowKey: " << lowKey;
881 for (int i = 0; i < entries; i++) {
882 LOG(INFO) << " Key[" << (i + lowKey) << "] -> 0x" << std::hex <<
883 targets[i];
884 }
885}
buzbeee3acd072012-02-25 17:03:10 -0800886
887
888} // namespace art