blob: cc31b292caa7e0fa539070c7a137a3dd0bb5b6f7 [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/*
Ian Rogersb5d09b22012-03-06 22:14:17 -080054 * Mark load/store instructions that access Dalvik registers through the stack.
buzbee31a4a6f2012-02-28 15:36:15 -080055 */
Ian Rogersb5d09b22012-03-06 22:14:17 -080056void annotateDalvikRegAccess(LIR* lir, int regId, bool isLoad, bool is64bit)
buzbee31a4a6f2012-02-28 15:36:15 -080057{
58 setMemRefType(lir, isLoad, kDalvikReg);
59
60 /*
Ian Rogersb5d09b22012-03-06 22:14:17 -080061 * Store the Dalvik register id in aliasInfo. Mark the MSB if it is a 64-bit
buzbee31a4a6f2012-02-28 15:36:15 -080062 * access.
63 */
64 lir->aliasInfo = regId;
Ian Rogersb5d09b22012-03-06 22:14:17 -080065 if (is64bit) {
buzbee31a4a6f2012-02-28 15:36:15 -080066 lir->aliasInfo |= 0x80000000;
67 }
68}
69
70/*
71 * Decode the register id.
72 */
73inline u8 getRegMaskCommon(int reg)
74{
75 u8 seed;
76 int shift;
77 int regId = reg & 0x1f;
78
79 /*
80 * Each double register is equal to a pair of single-precision FP registers
81 */
82 seed = DOUBLEREG(reg) ? 3 : 1;
83 /* FP register starts at bit position 16 */
84 shift = FPREG(reg) ? kFPReg0 : 0;
85 /* Expand the double register id into single offset */
86 shift += regId;
87 return (seed << shift);
88}
89
90/*
91 * Mark the corresponding bit(s).
92 */
93inline void setupRegMask(u8* mask, int reg)
94{
95 *mask |= getRegMaskCommon(reg);
96}
97
98/*
99 * Set up the proper fields in the resource mask
100 */
101void setupResourceMasks(LIR* lir)
102{
103 int opcode = lir->opcode;
104 int flags;
105
106 if (opcode <= 0) {
107 lir->useMask = lir->defMask = 0;
108 return;
109 }
110
111 flags = EncodingMap[lir->opcode].flags;
112
113 if (flags & NEEDS_FIXUP) {
114 lir->flags.pcRelFixup = true;
115 }
116
buzbeee88dfbf2012-03-05 11:19:57 -0800117 /* Get the starting size of the instruction's template */
118 lir->flags.size = oatGetInsnSize(lir);
119
buzbee31a4a6f2012-02-28 15:36:15 -0800120 /* Set up the mask for resources that are updated */
121 if (flags & (IS_LOAD | IS_STORE)) {
122 /* Default to heap - will catch specialized classes later */
123 setMemRefType(lir, flags & IS_LOAD, kHeapRef);
124 }
125
126 /*
127 * Conservatively assume the branch here will call out a function that in
128 * turn will trash everything.
129 */
130 if (flags & IS_BRANCH) {
131 lir->defMask = lir->useMask = ENCODE_ALL;
132 return;
133 }
134
135 if (flags & REG_DEF0) {
136 setupRegMask(&lir->defMask, lir->operands[0]);
137 }
138
139 if (flags & REG_DEF1) {
140 setupRegMask(&lir->defMask, lir->operands[1]);
141 }
142
143 if (flags & REG_DEF_SP) {
144 lir->defMask |= ENCODE_REG_SP;
145 }
146
buzbeea7678db2012-03-05 15:35:46 -0800147#if !defined(TARGET_X86)
buzbee31a4a6f2012-02-28 15:36:15 -0800148 if (flags & REG_DEF_LR) {
149 lir->defMask |= ENCODE_REG_LR;
150 }
buzbeea7678db2012-03-05 15:35:46 -0800151#endif
buzbee31a4a6f2012-02-28 15:36:15 -0800152
153 if (flags & REG_DEF_LIST0) {
154 lir->defMask |= ENCODE_REG_LIST(lir->operands[0]);
155 }
156
157 if (flags & REG_DEF_LIST1) {
158 lir->defMask |= ENCODE_REG_LIST(lir->operands[1]);
159 }
160
buzbee5de34942012-03-01 14:51:57 -0800161#if defined(TARGET_ARM)
buzbee31a4a6f2012-02-28 15:36:15 -0800162 if (flags & REG_DEF_FPCS_LIST0) {
163 lir->defMask |= ENCODE_REG_FPCS_LIST(lir->operands[0]);
164 }
165
166 if (flags & REG_DEF_FPCS_LIST2) {
167 for (int i = 0; i < lir->operands[2]; i++) {
168 setupRegMask(&lir->defMask, lir->operands[1] + i);
169 }
170 }
buzbee5de34942012-03-01 14:51:57 -0800171#endif
buzbee31a4a6f2012-02-28 15:36:15 -0800172
173 if (flags & SETS_CCODES) {
174 lir->defMask |= ENCODE_CCODE;
175 }
176
177#if defined(TARGET_ARM)
178 /* Conservatively treat the IT block */
179 if (flags & IS_IT) {
180 lir->defMask = ENCODE_ALL;
181 }
182#endif
183
184 if (flags & (REG_USE0 | REG_USE1 | REG_USE2 | REG_USE3)) {
185 int i;
186
187 for (i = 0; i < 4; i++) {
188 if (flags & (1 << (kRegUse0 + i))) {
189 setupRegMask(&lir->useMask, lir->operands[i]);
190 }
191 }
192 }
193
buzbeea7678db2012-03-05 15:35:46 -0800194#if defined(TARGET_ARM)
buzbee31a4a6f2012-02-28 15:36:15 -0800195 if (flags & REG_USE_PC) {
196 lir->useMask |= ENCODE_REG_PC;
197 }
buzbeea7678db2012-03-05 15:35:46 -0800198#endif
buzbee31a4a6f2012-02-28 15:36:15 -0800199
200 if (flags & REG_USE_SP) {
201 lir->useMask |= ENCODE_REG_SP;
202 }
203
204 if (flags & REG_USE_LIST0) {
205 lir->useMask |= ENCODE_REG_LIST(lir->operands[0]);
206 }
207
208 if (flags & REG_USE_LIST1) {
209 lir->useMask |= ENCODE_REG_LIST(lir->operands[1]);
210 }
211
buzbee5de34942012-03-01 14:51:57 -0800212#if defined(TARGET_ARM)
buzbee31a4a6f2012-02-28 15:36:15 -0800213 if (flags & REG_USE_FPCS_LIST0) {
214 lir->useMask |= ENCODE_REG_FPCS_LIST(lir->operands[0]);
215 }
216
217 if (flags & REG_USE_FPCS_LIST2) {
218 for (int i = 0; i < lir->operands[2]; i++) {
219 setupRegMask(&lir->useMask, lir->operands[1] + i);
220 }
221 }
buzbee5de34942012-03-01 14:51:57 -0800222#endif
buzbee31a4a6f2012-02-28 15:36:15 -0800223
224 if (flags & USES_CCODES) {
225 lir->useMask |= ENCODE_CCODE;
226 }
227
228#if defined(TARGET_ARM)
229 /* Fixup for kThumbPush/lr and kThumbPop/pc */
230 if (opcode == kThumbPush || opcode == kThumbPop) {
231 u8 r8Mask = getRegMaskCommon(r8);
232 if ((opcode == kThumbPush) && (lir->useMask & r8Mask)) {
233 lir->useMask &= ~r8Mask;
234 lir->useMask |= ENCODE_REG_LR;
235 } else if ((opcode == kThumbPop) && (lir->defMask & r8Mask)) {
236 lir->defMask &= ~r8Mask;
237 lir->defMask |= ENCODE_REG_PC;
238 }
239 }
240#endif
241}
242
243/*
buzbee5de34942012-03-01 14:51:57 -0800244 * Debugging macros
245 */
246#define DUMP_RESOURCE_MASK(X)
247#define DUMP_SSA_REP(X)
248
249/* Pretty-print a LIR instruction */
250void oatDumpLIRInsn(CompilationUnit* cUnit, LIR* arg, unsigned char* baseAddr)
251{
252 LIR* lir = (LIR*) arg;
253 int offset = lir->offset;
254 int dest = lir->operands[0];
buzbee86a4bce2012-03-06 18:15:00 -0800255 const bool dumpNop = (cUnit->enableDebug & (1 << kDebugShowNops));
buzbee5de34942012-03-01 14:51:57 -0800256
257 /* Handle pseudo-ops individually, and all regular insns as a group */
258 switch(lir->opcode) {
259 case kPseudoMethodEntry:
260 LOG(INFO) << "-------- method entry " <<
261 PrettyMethod(cUnit->method_idx, *cUnit->dex_file);
262 break;
263 case kPseudoMethodExit:
264 LOG(INFO) << "-------- Method_Exit";
265 break;
266 case kPseudoBarrier:
267 LOG(INFO) << "-------- BARRIER";
268 break;
269 case kPseudoExtended:
270 LOG(INFO) << "-------- " << (char* ) dest;
271 break;
272 case kPseudoSSARep:
273 DUMP_SSA_REP(LOG(INFO) << "-------- kMirOpPhi: " << (char* ) dest);
274 break;
275 case kPseudoEntryBlock:
276 LOG(INFO) << "-------- entry offset: 0x" << std::hex << dest;
277 break;
278 case kPseudoDalvikByteCodeBoundary:
279 LOG(INFO) << "-------- dalvik offset: 0x" << std::hex <<
280 lir->dalvikOffset << " @ " << (char* )lir->operands[0];
281 break;
282 case kPseudoExitBlock:
283 LOG(INFO) << "-------- exit offset: 0x" << std::hex << dest;
284 break;
285 case kPseudoPseudoAlign4:
286 LOG(INFO) << (intptr_t)baseAddr + offset << " (0x" << std::hex <<
287 offset << "): .align4";
288 break;
289 case kPseudoEHBlockLabel:
290 LOG(INFO) << "Exception_Handling:";
291 break;
292 case kPseudoTargetLabel:
293 case kPseudoNormalBlockLabel:
294 LOG(INFO) << "L" << (intptr_t)lir << ":";
295 break;
296 case kPseudoThrowTarget:
297 LOG(INFO) << "LT" << (intptr_t)lir << ":";
298 break;
299 case kPseudoSuspendTarget:
300 LOG(INFO) << "LS" << (intptr_t)lir << ":";
301 break;
302 case kPseudoCaseLabel:
303 LOG(INFO) << "LC" << (intptr_t)lir << ": Case target 0x" <<
304 std::hex << lir->operands[0] << "|" << std::dec <<
305 lir->operands[0];
306 break;
307 default:
308 if (lir->flags.isNop && !dumpNop) {
309 break;
310 } else {
311 std::string op_name(buildInsnString(EncodingMap[lir->opcode].name, lir, baseAddr));
312 std::string op_operands(buildInsnString(EncodingMap[lir->opcode].fmt, lir, baseAddr));
buzbeebe003642012-03-02 15:28:37 -0800313 LOG(INFO) << StringPrintf("%05x: %-9s%s%s", (unsigned int)(baseAddr + offset),
buzbee5de34942012-03-01 14:51:57 -0800314 op_name.c_str(), op_operands.c_str(), lir->flags.isNop ? "(nop)" : "");
315 }
316 break;
317 }
318
319 if (lir->useMask && (!lir->flags.isNop || dumpNop)) {
320 DUMP_RESOURCE_MASK(oatDumpResourceMask((LIR* ) lir,
321 lir->useMask, "use"));
322 }
323 if (lir->defMask && (!lir->flags.isNop || dumpNop)) {
324 DUMP_RESOURCE_MASK(oatDumpResourceMask((LIR* ) lir,
325 lir->defMask, "def"));
326 }
327}
328
329void oatDumpPromotionMap(CompilationUnit *cUnit)
330{
331 for (int i = 0; i < cUnit->numDalvikRegisters; i++) {
332 PromotionMap vRegMap = cUnit->promotionMap[i];
333 char buf[100];
334 if (vRegMap.fpLocation == kLocPhysReg) {
335 snprintf(buf, 100, " : s%d", vRegMap.fpReg & FP_REG_MASK);
336 } else {
337 buf[0] = 0;
338 }
339 char buf2[100];
340 snprintf(buf2, 100, "V[%02d] -> %s%d%s", i,
341 vRegMap.coreLocation == kLocPhysReg ?
342 "r" : "SP+", vRegMap.coreLocation == kLocPhysReg ?
343 vRegMap.coreReg : oatSRegOffset(cUnit, i), buf);
344 LOG(INFO) << buf2;
345 }
346}
347
348void oatDumpFullPromotionMap(CompilationUnit *cUnit)
349{
350 for (int i = 0; i < cUnit->numDalvikRegisters; i++) {
351 PromotionMap vRegMap = cUnit->promotionMap[i];
352 LOG(INFO) << i << " -> " << "CL:" << (int)vRegMap.coreLocation <<
353 ", CR:" << (int)vRegMap.coreReg << ", FL:" <<
354 (int)vRegMap.fpLocation << ", FR:" << (int)vRegMap.fpReg <<
355 ", - " << (int)vRegMap.firstInPair;
356 }
357}
358
359/* Dump instructions and constant pool contents */
360void oatCodegenDump(CompilationUnit* cUnit)
361{
362 LOG(INFO) << "/*";
363 LOG(INFO) << "Dumping LIR insns for "
364 << PrettyMethod(cUnit->method_idx, *cUnit->dex_file);
365 LIR* lirInsn;
366 LIR* thisLIR;
367 int insnsSize = cUnit->insnsSize;
368
369 LOG(INFO) << "Regs (excluding ins) : " << cUnit->numRegs;
370 LOG(INFO) << "Ins : " << cUnit->numIns;
371 LOG(INFO) << "Outs : " << cUnit->numOuts;
372 LOG(INFO) << "CoreSpills : " << cUnit->numCoreSpills;
373 LOG(INFO) << "FPSpills : " << cUnit->numFPSpills;
buzbee239c4e72012-03-16 08:42:29 -0700374 LOG(INFO) << "CompilerTemps : " << cUnit->numCompilerTemps;
buzbee5de34942012-03-01 14:51:57 -0800375 LOG(INFO) << "Frame size : " << cUnit->frameSize;
buzbee5de34942012-03-01 14:51:57 -0800376 LOG(INFO) << "code size is " << cUnit->totalSize <<
377 " bytes, Dalvik size is " << insnsSize * 2;
378 LOG(INFO) << "expansion factor: " <<
379 (float)cUnit->totalSize / (float)(insnsSize * 2);
380 oatDumpPromotionMap(cUnit);
381 for (lirInsn = cUnit->firstLIRInsn; lirInsn; lirInsn = lirInsn->next) {
382 oatDumpLIRInsn(cUnit, lirInsn, 0);
383 }
384 for (lirInsn = cUnit->classPointerList; lirInsn; lirInsn = lirInsn->next) {
385 thisLIR = (LIR*) lirInsn;
386 LOG(INFO) << StringPrintf("%x (%04x): .class (%s)",
387 thisLIR->offset, thisLIR->offset,
388 ((CallsiteInfo *) thisLIR->operands[0])->classDescriptor);
389 }
390 for (lirInsn = cUnit->literalList; lirInsn; lirInsn = lirInsn->next) {
391 thisLIR = (LIR*) lirInsn;
392 LOG(INFO) << StringPrintf("%x (%04x): .word (%#x)",
393 thisLIR->offset, thisLIR->offset, thisLIR->operands[0]);
394 }
395
396 const DexFile::MethodId& method_id =
397 cUnit->dex_file->GetMethodId(cUnit->method_idx);
398 std::string signature(cUnit->dex_file->GetMethodSignature(method_id));
399 std::string name(cUnit->dex_file->GetMethodName(method_id));
400 std::string descriptor(cUnit->dex_file->GetMethodDeclaringClassDescriptor(method_id));
401
402 // Dump mapping table
403 if (cUnit->mappingTable.size() > 0) {
404 std::string line(StringPrintf("\n MappingTable %s%s_%s_mappingTable[%zu] = {",
405 descriptor.c_str(), name.c_str(), signature.c_str(), cUnit->mappingTable.size()));
406 std::replace(line.begin(), line.end(), ';', '_');
407 LOG(INFO) << line;
408 for (uint32_t i = 0; i < cUnit->mappingTable.size(); i+=2) {
buzbee82488f52012-03-02 08:20:26 -0800409 line = StringPrintf(" {0x%05x, 0x%04x},",
buzbee5de34942012-03-01 14:51:57 -0800410 cUnit->mappingTable[i], cUnit->mappingTable[i+1]);
411 LOG(INFO) << line;
412 }
413 LOG(INFO) <<" };\n\n";
414 }
415}
416
buzbeea2ebdd72012-03-04 14:57:06 -0800417
418LIR* rawLIR(CompilationUnit* cUnit, int dalvikOffset, int opcode, int op0,
Ian Rogersb5d09b22012-03-06 22:14:17 -0800419 int op1, int op2, int op3, int op4, LIR* target)
buzbeea2ebdd72012-03-04 14:57:06 -0800420{
421 LIR* insn = (LIR* ) oatNew(cUnit, sizeof(LIR), true, kAllocLIR);
422 insn->dalvikOffset = dalvikOffset;
423 insn->opcode = opcode;
424 insn->operands[0] = op0;
425 insn->operands[1] = op1;
426 insn->operands[2] = op2;
427 insn->operands[3] = op3;
Ian Rogersb5d09b22012-03-06 22:14:17 -0800428 insn->operands[4] = op4;
buzbeea2ebdd72012-03-04 14:57:06 -0800429 insn->target = target;
430 oatSetupResourceMasks(insn);
431 if (opcode == kPseudoTargetLabel) {
432 // Always make labels scheduling barriers
433 insn->defMask = ENCODE_ALL;
434 }
435 return insn;
436}
437
buzbee5de34942012-03-01 14:51:57 -0800438/*
buzbee31a4a6f2012-02-28 15:36:15 -0800439 * The following are building blocks to construct low-level IRs with 0 - 4
440 * operands.
441 */
buzbee5de34942012-03-01 14:51:57 -0800442LIR* newLIR0(CompilationUnit* cUnit, int opcode)
buzbee31a4a6f2012-02-28 15:36:15 -0800443{
Ian Rogersb5d09b22012-03-06 22:14:17 -0800444 DCHECK(isPseudoOpcode(opcode) || (EncodingMap[opcode].flags & NO_OPERAND))
445 << EncodingMap[opcode].name << " " << (int)opcode << " "
446 << PrettyMethod(cUnit->method_idx, *cUnit->dex_file) << " "
447 << cUnit->currentDalvikOffset;
buzbeea2ebdd72012-03-04 14:57:06 -0800448 LIR* insn = rawLIR(cUnit, cUnit->currentDalvikOffset, opcode);
buzbee31a4a6f2012-02-28 15:36:15 -0800449 oatAppendLIR(cUnit, (LIR*) insn);
450 return insn;
451}
452
buzbee5de34942012-03-01 14:51:57 -0800453LIR* newLIR1(CompilationUnit* cUnit, int opcode,
buzbee31a4a6f2012-02-28 15:36:15 -0800454 int dest)
455{
Ian Rogersb5d09b22012-03-06 22:14:17 -0800456 DCHECK(isPseudoOpcode(opcode) || (EncodingMap[opcode].flags & IS_UNARY_OP))
457 << EncodingMap[opcode].name << " " << (int)opcode << " "
458 << PrettyMethod(cUnit->method_idx, *cUnit->dex_file) << " "
459 << cUnit->currentDalvikOffset;
buzbeea2ebdd72012-03-04 14:57:06 -0800460 LIR* insn = rawLIR(cUnit, cUnit->currentDalvikOffset, opcode, dest);
buzbee31a4a6f2012-02-28 15:36:15 -0800461 oatAppendLIR(cUnit, (LIR*) insn);
462 return insn;
463}
464
buzbee5de34942012-03-01 14:51:57 -0800465LIR* newLIR2(CompilationUnit* cUnit, int opcode,
buzbee31a4a6f2012-02-28 15:36:15 -0800466 int dest, int src1)
467{
Ian Rogersb5d09b22012-03-06 22:14:17 -0800468 DCHECK(isPseudoOpcode(opcode) || (EncodingMap[opcode].flags & IS_BINARY_OP))
469 << EncodingMap[opcode].name << " " << (int)opcode << " "
470 << PrettyMethod(cUnit->method_idx, *cUnit->dex_file) << " "
471 << cUnit->currentDalvikOffset;
buzbeea2ebdd72012-03-04 14:57:06 -0800472 LIR* insn = rawLIR(cUnit, cUnit->currentDalvikOffset, opcode, dest, src1);
buzbee31a4a6f2012-02-28 15:36:15 -0800473 oatAppendLIR(cUnit, (LIR*) insn);
474 return insn;
475}
476
buzbee5de34942012-03-01 14:51:57 -0800477LIR* newLIR3(CompilationUnit* cUnit, int opcode,
buzbee31a4a6f2012-02-28 15:36:15 -0800478 int dest, int src1, int src2)
479{
Ian Rogersb5d09b22012-03-06 22:14:17 -0800480 DCHECK(isPseudoOpcode(opcode) || (EncodingMap[opcode].flags & IS_TERTIARY_OP))
481 << EncodingMap[opcode].name << " " << (int)opcode << " "
482 << PrettyMethod(cUnit->method_idx, *cUnit->dex_file) << " "
483 << cUnit->currentDalvikOffset;
buzbeea2ebdd72012-03-04 14:57:06 -0800484 LIR* insn = rawLIR(cUnit, cUnit->currentDalvikOffset, opcode, dest, src1,
485 src2);
buzbee31a4a6f2012-02-28 15:36:15 -0800486 oatAppendLIR(cUnit, (LIR*) insn);
487 return insn;
488}
489
buzbee5de34942012-03-01 14:51:57 -0800490LIR* newLIR4(CompilationUnit* cUnit, int opcode,
Ian Rogersb5d09b22012-03-06 22:14:17 -0800491 int dest, int src1, int src2, int info)
buzbee31a4a6f2012-02-28 15:36:15 -0800492{
Ian Rogersb5d09b22012-03-06 22:14:17 -0800493 DCHECK(isPseudoOpcode(opcode) || (EncodingMap[opcode].flags & IS_QUAD_OP))
494 << EncodingMap[opcode].name << " " << (int)opcode << " "
495 << PrettyMethod(cUnit->method_idx, *cUnit->dex_file) << " "
496 << cUnit->currentDalvikOffset;
buzbeea2ebdd72012-03-04 14:57:06 -0800497 LIR* insn = rawLIR(cUnit, cUnit->currentDalvikOffset, opcode, dest, src1,
498 src2, info);
buzbee31a4a6f2012-02-28 15:36:15 -0800499 oatAppendLIR(cUnit, (LIR*) insn);
500 return insn;
501}
buzbee31a4a6f2012-02-28 15:36:15 -0800502
Ian Rogersb5d09b22012-03-06 22:14:17 -0800503LIR* newLIR5(CompilationUnit* cUnit, int opcode,
504 int dest, int src1, int src2, int info1, int info2)
505{
506 DCHECK(isPseudoOpcode(opcode) || (EncodingMap[opcode].flags & IS_QUIN_OP))
507 << EncodingMap[opcode].name << " " << (int)opcode << " "
508 << PrettyMethod(cUnit->method_idx, *cUnit->dex_file) << " "
509 << cUnit->currentDalvikOffset;
510 LIR* insn = rawLIR(cUnit, cUnit->currentDalvikOffset, opcode, dest, src1,
511 src2, info1, info2);
512 oatAppendLIR(cUnit, (LIR*) insn);
513 return insn;
514}
515
buzbee31a4a6f2012-02-28 15:36:15 -0800516/*
517 * Search the existing constants in the literal pool for an exact or close match
518 * within specified delta (greater or equal to 0).
519 */
520LIR* scanLiteralPool(LIR* dataTarget, int value, unsigned int delta)
521{
522 while (dataTarget) {
523 if (((unsigned) (value - ((LIR* ) dataTarget)->operands[0])) <=
524 delta)
525 return (LIR* ) dataTarget;
526 dataTarget = dataTarget->next;
527 }
528 return NULL;
529}
530
531/* Search the existing constants in the literal pool for an exact wide match */
532LIR* scanLiteralPoolWide(LIR* dataTarget, int valLo, int valHi)
533{
534 bool loMatch = false;
535 LIR* loTarget = NULL;
536 while (dataTarget) {
537 if (loMatch && (((LIR*)dataTarget)->operands[0] == valHi)) {
538 return (LIR*)loTarget;
539 }
540 loMatch = false;
541 if (((LIR*)dataTarget)->operands[0] == valLo) {
542 loMatch = true;
543 loTarget = dataTarget;
544 }
545 dataTarget = dataTarget->next;
546 }
547 return NULL;
548}
549
550/*
551 * The following are building blocks to insert constants into the pool or
552 * instruction streams.
553 */
554
buzbee5de34942012-03-01 14:51:57 -0800555/* Add a 32-bit constant either in the constant pool */
buzbee31a4a6f2012-02-28 15:36:15 -0800556LIR* addWordData(CompilationUnit* cUnit, LIR* *constantListP,
557 int value)
558{
559 /* Add the constant to the literal pool */
560 if (constantListP) {
561 LIR* newValue = (LIR* ) oatNew(cUnit, sizeof(LIR), true,
562 kAllocData);
563 newValue->operands[0] = value;
564 newValue->next = *constantListP;
565 *constantListP = (LIR*) newValue;
566 return newValue;
buzbee31a4a6f2012-02-28 15:36:15 -0800567 }
568 return NULL;
569}
570
571/* Add a 64-bit constant to the constant pool or mixed with code */
572LIR* addWideData(CompilationUnit* cUnit, LIR* *constantListP,
573 int valLo, int valHi)
574{
buzbee31a4a6f2012-02-28 15:36:15 -0800575 //FIXME: hard-coded little endian, need BE variant
buzbee5de34942012-03-01 14:51:57 -0800576 // Insert high word into list first
577 addWordData(cUnit, constantListP, valHi);
578 return addWordData(cUnit, constantListP, valLo);
buzbee31a4a6f2012-02-28 15:36:15 -0800579}
580
Ian Rogersab058bb2012-03-11 22:19:38 -0700581void pushWord(std::vector<uint8_t>&buf, int data) {
582 buf.push_back( data & 0xff);
583 buf.push_back( (data >> 8) & 0xff);
584 buf.push_back( (data >> 16) & 0xff);
585 buf.push_back( (data >> 24) & 0xff);
buzbeee3acd072012-02-25 17:03:10 -0800586}
587
Ian Rogersab058bb2012-03-11 22:19:38 -0700588void alignBuffer(std::vector<uint8_t>&buf, size_t offset) {
589 while (buf.size() < offset) {
buzbeee3acd072012-02-25 17:03:10 -0800590 buf.push_back(0);
Ian Rogersab058bb2012-03-11 22:19:38 -0700591 }
buzbeee3acd072012-02-25 17:03:10 -0800592}
593
594/* Write the literal pool to the output stream */
buzbee31a4a6f2012-02-28 15:36:15 -0800595void installLiteralPools(CompilationUnit* cUnit)
buzbeee3acd072012-02-25 17:03:10 -0800596{
597 alignBuffer(cUnit->codeBuffer, cUnit->dataOffset);
buzbee31a4a6f2012-02-28 15:36:15 -0800598 LIR* dataLIR = (LIR*) cUnit->literalList;
buzbeee3acd072012-02-25 17:03:10 -0800599 while (dataLIR != NULL) {
600 pushWord(cUnit->codeBuffer, dataLIR->operands[0]);
601 dataLIR = NEXT_LIR(dataLIR);
602 }
603}
604
605/* Write the switch tables to the output stream */
buzbee31a4a6f2012-02-28 15:36:15 -0800606void installSwitchTables(CompilationUnit* cUnit)
buzbeee3acd072012-02-25 17:03:10 -0800607{
608 GrowableListIterator iterator;
609 oatGrowableListIteratorInit(&cUnit->switchTables, &iterator);
610 while (true) {
611 SwitchTable* tabRec = (SwitchTable *) oatGrowableListIteratorNext(
612 &iterator);
613 if (tabRec == NULL) break;
614 alignBuffer(cUnit->codeBuffer, tabRec->offset);
buzbeec5159d52012-03-03 11:48:39 -0800615 /*
616 * For Arm, our reference point is the address of the bx
617 * instruction that does the launch, so we have to subtract
618 * the auto pc-advance. For other targets the reference point
619 * is a label, so we can use the offset as-is.
620 */
621#if defined(TARGET_ARM)
622 int bxOffset = tabRec->anchor->offset + 4;
623#else
624 int bxOffset = tabRec->anchor->offset;
625#endif
buzbeee3acd072012-02-25 17:03:10 -0800626 if (cUnit->printMe) {
627 LOG(INFO) << "Switch table for offset 0x" << std::hex << bxOffset;
628 }
Elliott Hughesadb8c672012-03-06 16:49:32 -0800629 if (tabRec->table[0] == Instruction::kSparseSwitchSignature) {
buzbeee3acd072012-02-25 17:03:10 -0800630 int* keys = (int*)&(tabRec->table[2]);
631 for (int elems = 0; elems < tabRec->table[1]; elems++) {
buzbee31a4a6f2012-02-28 15:36:15 -0800632 int disp = tabRec->targets[elems]->offset - bxOffset;
buzbeee3acd072012-02-25 17:03:10 -0800633 if (cUnit->printMe) {
634 LOG(INFO) << " Case[" << elems << "] key: 0x" <<
635 std::hex << keys[elems] << ", disp: 0x" <<
636 std::hex << disp;
637 }
638 pushWord(cUnit->codeBuffer, keys[elems]);
639 pushWord(cUnit->codeBuffer,
buzbee31a4a6f2012-02-28 15:36:15 -0800640 tabRec->targets[elems]->offset - bxOffset);
buzbeee3acd072012-02-25 17:03:10 -0800641 }
642 } else {
Elliott Hughesadb8c672012-03-06 16:49:32 -0800643 DCHECK_EQ(static_cast<int>(tabRec->table[0]), static_cast<int>(Instruction::kPackedSwitchSignature));
buzbeee3acd072012-02-25 17:03:10 -0800644 for (int elems = 0; elems < tabRec->table[1]; elems++) {
buzbee31a4a6f2012-02-28 15:36:15 -0800645 int disp = tabRec->targets[elems]->offset - bxOffset;
buzbeee3acd072012-02-25 17:03:10 -0800646 if (cUnit->printMe) {
647 LOG(INFO) << " Case[" << elems << "] disp: 0x" <<
648 std::hex << disp;
649 }
650 pushWord(cUnit->codeBuffer,
buzbee31a4a6f2012-02-28 15:36:15 -0800651 tabRec->targets[elems]->offset - bxOffset);
buzbeee3acd072012-02-25 17:03:10 -0800652 }
653 }
654 }
655}
656
657/* Write the fill array dta to the output stream */
buzbee31a4a6f2012-02-28 15:36:15 -0800658void installFillArrayData(CompilationUnit* cUnit)
buzbeee3acd072012-02-25 17:03:10 -0800659{
660 GrowableListIterator iterator;
661 oatGrowableListIteratorInit(&cUnit->fillArrayData, &iterator);
662 while (true) {
663 FillArrayData *tabRec = (FillArrayData *) oatGrowableListIteratorNext(
664 &iterator);
665 if (tabRec == NULL) break;
666 alignBuffer(cUnit->codeBuffer, tabRec->offset);
Ian Rogersab058bb2012-03-11 22:19:38 -0700667 for (int i = 0; i < (tabRec->size + 1) / 2; i++) {
668 cUnit->codeBuffer.push_back( tabRec->table[i] & 0xFF);
669 cUnit->codeBuffer.push_back( (tabRec->table[i] >> 8) & 0xFF);
buzbeee3acd072012-02-25 17:03:10 -0800670 }
671 }
672}
673
buzbee31a4a6f2012-02-28 15:36:15 -0800674int assignLiteralOffsetCommon(LIR* lir, int offset)
buzbeee3acd072012-02-25 17:03:10 -0800675{
676 for (;lir != NULL; lir = lir->next) {
677 lir->offset = offset;
678 offset += 4;
679 }
680 return offset;
681}
682
buzbee31a4a6f2012-02-28 15:36:15 -0800683void createMappingTable(CompilationUnit* cUnit)
buzbeee3acd072012-02-25 17:03:10 -0800684{
buzbee31a4a6f2012-02-28 15:36:15 -0800685 LIR* tgtLIR;
buzbeee3acd072012-02-25 17:03:10 -0800686 int currentDalvikOffset = -1;
687
buzbee31a4a6f2012-02-28 15:36:15 -0800688 for (tgtLIR = (LIR *) cUnit->firstLIRInsn;
buzbeee3acd072012-02-25 17:03:10 -0800689 tgtLIR;
690 tgtLIR = NEXT_LIR(tgtLIR)) {
691 if ((tgtLIR->opcode >= 0) && !tgtLIR->flags.isNop &&
buzbee31a4a6f2012-02-28 15:36:15 -0800692 (currentDalvikOffset != tgtLIR->dalvikOffset)) {
buzbeee3acd072012-02-25 17:03:10 -0800693 // Changed - need to emit a record
buzbee31a4a6f2012-02-28 15:36:15 -0800694 cUnit->mappingTable.push_back(tgtLIR->offset);
695 cUnit->mappingTable.push_back(tgtLIR->dalvikOffset);
696 currentDalvikOffset = tgtLIR->dalvikOffset;
buzbeee3acd072012-02-25 17:03:10 -0800697 }
698 }
699}
700
701/* Determine the offset of each literal field */
buzbee31a4a6f2012-02-28 15:36:15 -0800702int assignLiteralOffset(CompilationUnit* cUnit, int offset)
buzbeee3acd072012-02-25 17:03:10 -0800703{
704 offset = assignLiteralOffsetCommon(cUnit->literalList, offset);
705 return offset;
706}
707
buzbee31a4a6f2012-02-28 15:36:15 -0800708int assignSwitchTablesOffset(CompilationUnit* cUnit, int offset)
buzbeee3acd072012-02-25 17:03:10 -0800709{
710 GrowableListIterator iterator;
711 oatGrowableListIteratorInit(&cUnit->switchTables, &iterator);
712 while (true) {
713 SwitchTable *tabRec = (SwitchTable *) oatGrowableListIteratorNext(
714 &iterator);
715 if (tabRec == NULL) break;
716 tabRec->offset = offset;
Elliott Hughesadb8c672012-03-06 16:49:32 -0800717 if (tabRec->table[0] == Instruction::kSparseSwitchSignature) {
buzbeee3acd072012-02-25 17:03:10 -0800718 offset += tabRec->table[1] * (sizeof(int) * 2);
719 } else {
Elliott Hughesadb8c672012-03-06 16:49:32 -0800720 DCHECK_EQ(static_cast<int>(tabRec->table[0]), static_cast<int>(Instruction::kPackedSwitchSignature));
buzbeee3acd072012-02-25 17:03:10 -0800721 offset += tabRec->table[1] * sizeof(int);
722 }
723 }
724 return offset;
725}
726
buzbee31a4a6f2012-02-28 15:36:15 -0800727int assignFillArrayDataOffset(CompilationUnit* cUnit, int offset)
buzbeee3acd072012-02-25 17:03:10 -0800728{
729 GrowableListIterator iterator;
730 oatGrowableListIteratorInit(&cUnit->fillArrayData, &iterator);
731 while (true) {
732 FillArrayData *tabRec = (FillArrayData *) oatGrowableListIteratorNext(
733 &iterator);
734 if (tabRec == NULL) break;
735 tabRec->offset = offset;
736 offset += tabRec->size;
737 // word align
738 offset = (offset + 3) & ~3;
739 }
740 return offset;
741}
742
743/*
744 * Walk the compilation unit and assign offsets to instructions
745 * and literals and compute the total size of the compiled unit.
746 */
747void oatAssignOffsets(CompilationUnit* cUnit)
748{
749 int offset = oatAssignInsnOffsets(cUnit);
750
751 /* Const values have to be word aligned */
752 offset = (offset + 3) & ~3;
753
754 /* Set up offsets for literals */
755 cUnit->dataOffset = offset;
756
757 offset = assignLiteralOffset(cUnit, offset);
758
759 offset = assignSwitchTablesOffset(cUnit, offset);
760
761 offset = assignFillArrayDataOffset(cUnit, offset);
762
763 cUnit->totalSize = offset;
764}
765
766/*
767 * Go over each instruction in the list and calculate the offset from the top
768 * before sending them off to the assembler. If out-of-range branch distance is
769 * seen rearrange the instructions a bit to correct it.
770 */
771void oatAssembleLIR(CompilationUnit* cUnit)
772{
773 oatAssignOffsets(cUnit);
774 /*
775 * Assemble here. Note that we generate code with optimistic assumptions
776 * and if found now to work, we'll have to redo the sequence and retry.
777 */
778
779 while (true) {
780 AssemblerStatus res = oatAssembleInstructions(cUnit, 0);
781 if (res == kSuccess) {
782 break;
783 } else {
784 cUnit->assemblerRetries++;
785 if (cUnit->assemblerRetries > MAX_ASSEMBLER_RETRIES) {
786 LOG(FATAL) << "Assembler error - too many retries";
787 }
788 // Redo offsets and try again
789 oatAssignOffsets(cUnit);
790 cUnit->codeBuffer.clear();
791 }
792 }
793
794 // Install literals
795 installLiteralPools(cUnit);
796
797 // Install switch tables
798 installSwitchTables(cUnit);
799
800 // Install fill array data
801 installFillArrayData(cUnit);
802
803 /*
804 * Create the mapping table
805 */
806 createMappingTable(cUnit);
807}
808
buzbee31a4a6f2012-02-28 15:36:15 -0800809/*
810 * Insert a kPseudoCaseLabel at the beginning of the Dalvik
811 * offset vaddr. This label will be used to fix up the case
812 * branch table during the assembly phase. Be sure to set
813 * all resource flags on this to prevent code motion across
814 * target boundaries. KeyVal is just there for debugging.
815 */
816LIR* insertCaseLabel(CompilationUnit* cUnit, int vaddr, int keyVal)
817{
818 std::map<unsigned int, LIR*>::iterator it;
819 it = cUnit->boundaryMap.find(vaddr);
820 if (it == cUnit->boundaryMap.end()) {
821 LOG(FATAL) << "Error: didn't find vaddr 0x" << std::hex << vaddr;
822 }
823 LIR* newLabel = (LIR*)oatNew(cUnit, sizeof(LIR), true, kAllocLIR);
824 newLabel->dalvikOffset = vaddr;
825 newLabel->opcode = kPseudoCaseLabel;
826 newLabel->operands[0] = keyVal;
827 oatInsertLIRAfter(it->second, (LIR*)newLabel);
828 return newLabel;
829}
830
831void markPackedCaseLabels(CompilationUnit* cUnit, SwitchTable *tabRec)
832{
833 const u2* table = tabRec->table;
834 int baseVaddr = tabRec->vaddr;
835 int *targets = (int*)&table[4];
836 int entries = table[1];
837 int lowKey = s4FromSwitchData(&table[2]);
838 for (int i = 0; i < entries; i++) {
839 tabRec->targets[i] = insertCaseLabel(cUnit, baseVaddr + targets[i],
840 i + lowKey);
841 }
842}
843
844void markSparseCaseLabels(CompilationUnit* cUnit, SwitchTable *tabRec)
845{
846 const u2* table = tabRec->table;
847 int baseVaddr = tabRec->vaddr;
848 int entries = table[1];
849 int* keys = (int*)&table[2];
850 int* targets = &keys[entries];
851 for (int i = 0; i < entries; i++) {
852 tabRec->targets[i] = insertCaseLabel(cUnit, baseVaddr + targets[i],
853 keys[i]);
854 }
855}
856
857void oatProcessSwitchTables(CompilationUnit* cUnit)
858{
859 GrowableListIterator iterator;
860 oatGrowableListIteratorInit(&cUnit->switchTables, &iterator);
861 while (true) {
862 SwitchTable *tabRec = (SwitchTable *) oatGrowableListIteratorNext(
863 &iterator);
864 if (tabRec == NULL) break;
Elliott Hughesadb8c672012-03-06 16:49:32 -0800865 if (tabRec->table[0] == Instruction::kPackedSwitchSignature) {
buzbee31a4a6f2012-02-28 15:36:15 -0800866 markPackedCaseLabels(cUnit, tabRec);
Elliott Hughesadb8c672012-03-06 16:49:32 -0800867 } else if (tabRec->table[0] == Instruction::kSparseSwitchSignature) {
buzbee31a4a6f2012-02-28 15:36:15 -0800868 markSparseCaseLabels(cUnit, tabRec);
Elliott Hughesadb8c672012-03-06 16:49:32 -0800869 } else {
buzbee31a4a6f2012-02-28 15:36:15 -0800870 LOG(FATAL) << "Invalid switch table";
871 }
872 }
873}
874
875//FIXME: Do we have endian issues here?
876
877void dumpSparseSwitchTable(const u2* table)
878 /*
879 * Sparse switch data format:
880 * ushort ident = 0x0200 magic value
881 * ushort size number of entries in the table; > 0
882 * int keys[size] keys, sorted low-to-high; 32-bit aligned
883 * int targets[size] branch targets, relative to switch opcode
884 *
885 * Total size is (2+size*4) 16-bit code units.
886 */
887{
888 u2 ident = table[0];
889 int entries = table[1];
890 int* keys = (int*)&table[2];
891 int* targets = &keys[entries];
892 LOG(INFO) << "Sparse switch table - ident:0x" << std::hex << ident <<
893 ", entries: " << std::dec << entries;
894 for (int i = 0; i < entries; i++) {
895 LOG(INFO) << " Key[" << keys[i] << "] -> 0x" << std::hex <<
896 targets[i];
897 }
898}
899
900void dumpPackedSwitchTable(const u2* table)
901 /*
902 * Packed switch data format:
903 * ushort ident = 0x0100 magic value
904 * ushort size number of entries in the table
905 * int first_key first (and lowest) switch case value
906 * int targets[size] branch targets, relative to switch opcode
907 *
908 * Total size is (4+size*2) 16-bit code units.
909 */
910{
911 u2 ident = table[0];
912 int* targets = (int*)&table[4];
913 int entries = table[1];
914 int lowKey = s4FromSwitchData(&table[2]);
915 LOG(INFO) << "Packed switch table - ident:0x" << std::hex << ident <<
916 ", entries: " << std::dec << entries << ", lowKey: " << lowKey;
917 for (int i = 0; i < entries; i++) {
918 LOG(INFO) << " Key[" << (i + lowKey) << "] -> 0x" << std::hex <<
919 targets[i];
920 }
921}
buzbeee3acd072012-02-25 17:03:10 -0800922
923
924} // namespace art