blob: 8b324eda50a6bec8d51dbcb9f1a6cc1ed73ac15b [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 */
Ian Rogers3fa13792012-03-18 15:53:45 -0700556LIR* addWordData(CompilationUnit* cUnit, LIR* *constantListP, int value)
buzbee31a4a6f2012-02-28 15:36:15 -0800557{
558 /* Add the constant to the literal pool */
559 if (constantListP) {
560 LIR* newValue = (LIR* ) oatNew(cUnit, sizeof(LIR), true,
561 kAllocData);
562 newValue->operands[0] = value;
563 newValue->next = *constantListP;
564 *constantListP = (LIR*) newValue;
565 return newValue;
buzbee31a4a6f2012-02-28 15:36:15 -0800566 }
567 return NULL;
568}
569
570/* Add a 64-bit constant to the constant pool or mixed with code */
571LIR* addWideData(CompilationUnit* cUnit, LIR* *constantListP,
572 int valLo, int valHi)
573{
buzbee31a4a6f2012-02-28 15:36:15 -0800574 //FIXME: hard-coded little endian, need BE variant
buzbee5de34942012-03-01 14:51:57 -0800575 // Insert high word into list first
576 addWordData(cUnit, constantListP, valHi);
577 return addWordData(cUnit, constantListP, valLo);
buzbee31a4a6f2012-02-28 15:36:15 -0800578}
579
Ian Rogersab058bb2012-03-11 22:19:38 -0700580void pushWord(std::vector<uint8_t>&buf, int data) {
581 buf.push_back( data & 0xff);
582 buf.push_back( (data >> 8) & 0xff);
583 buf.push_back( (data >> 16) & 0xff);
584 buf.push_back( (data >> 24) & 0xff);
buzbeee3acd072012-02-25 17:03:10 -0800585}
586
Ian Rogersab058bb2012-03-11 22:19:38 -0700587void alignBuffer(std::vector<uint8_t>&buf, size_t offset) {
588 while (buf.size() < offset) {
buzbeee3acd072012-02-25 17:03:10 -0800589 buf.push_back(0);
Ian Rogersab058bb2012-03-11 22:19:38 -0700590 }
buzbeee3acd072012-02-25 17:03:10 -0800591}
592
593/* Write the literal pool to the output stream */
buzbee31a4a6f2012-02-28 15:36:15 -0800594void installLiteralPools(CompilationUnit* cUnit)
buzbeee3acd072012-02-25 17:03:10 -0800595{
596 alignBuffer(cUnit->codeBuffer, cUnit->dataOffset);
Ian Rogers3fa13792012-03-18 15:53:45 -0700597 LIR* dataLIR = cUnit->literalList;
buzbeee3acd072012-02-25 17:03:10 -0800598 while (dataLIR != NULL) {
599 pushWord(cUnit->codeBuffer, dataLIR->operands[0]);
600 dataLIR = NEXT_LIR(dataLIR);
601 }
Ian Rogers3fa13792012-03-18 15:53:45 -0700602 // Push code and method literals, record offsets for the compiler to patch.
603 dataLIR = cUnit->codeLiteralList;
604 if (dataLIR != NULL) {
605 while (dataLIR != NULL) {
606 cUnit->compiler->AddCodePatch(cUnit->dex_cache, cUnit->dex_file,
607 cUnit->method_idx,
608 dataLIR->operands[0],
609 cUnit->codeBuffer.size());
610 pushWord(cUnit->codeBuffer, 0xEBAD9A7C); // value to be patched
611 dataLIR = NEXT_LIR(dataLIR);
612 }
613 dataLIR = cUnit->methodLiteralList;
614 while (dataLIR != NULL) {
615 cUnit->compiler->AddMethodPatch(cUnit->dex_cache, cUnit->dex_file,
616 cUnit->method_idx,
617 dataLIR->operands[0],
618 cUnit->codeBuffer.size());
619 pushWord(cUnit->codeBuffer, 0xEBAD9A7D); // value to be patched
620 dataLIR = NEXT_LIR(dataLIR);
621 }
622 }
623
buzbeee3acd072012-02-25 17:03:10 -0800624}
625
626/* Write the switch tables to the output stream */
buzbee31a4a6f2012-02-28 15:36:15 -0800627void installSwitchTables(CompilationUnit* cUnit)
buzbeee3acd072012-02-25 17:03:10 -0800628{
629 GrowableListIterator iterator;
630 oatGrowableListIteratorInit(&cUnit->switchTables, &iterator);
631 while (true) {
632 SwitchTable* tabRec = (SwitchTable *) oatGrowableListIteratorNext(
633 &iterator);
634 if (tabRec == NULL) break;
635 alignBuffer(cUnit->codeBuffer, tabRec->offset);
buzbeec5159d52012-03-03 11:48:39 -0800636 /*
637 * For Arm, our reference point is the address of the bx
638 * instruction that does the launch, so we have to subtract
639 * the auto pc-advance. For other targets the reference point
640 * is a label, so we can use the offset as-is.
641 */
642#if defined(TARGET_ARM)
643 int bxOffset = tabRec->anchor->offset + 4;
644#else
645 int bxOffset = tabRec->anchor->offset;
646#endif
buzbeee3acd072012-02-25 17:03:10 -0800647 if (cUnit->printMe) {
648 LOG(INFO) << "Switch table for offset 0x" << std::hex << bxOffset;
649 }
Elliott Hughesadb8c672012-03-06 16:49:32 -0800650 if (tabRec->table[0] == Instruction::kSparseSwitchSignature) {
buzbeee3acd072012-02-25 17:03:10 -0800651 int* keys = (int*)&(tabRec->table[2]);
652 for (int elems = 0; elems < tabRec->table[1]; elems++) {
buzbee31a4a6f2012-02-28 15:36:15 -0800653 int disp = tabRec->targets[elems]->offset - bxOffset;
buzbeee3acd072012-02-25 17:03:10 -0800654 if (cUnit->printMe) {
655 LOG(INFO) << " Case[" << elems << "] key: 0x" <<
656 std::hex << keys[elems] << ", disp: 0x" <<
657 std::hex << disp;
658 }
659 pushWord(cUnit->codeBuffer, keys[elems]);
660 pushWord(cUnit->codeBuffer,
buzbee31a4a6f2012-02-28 15:36:15 -0800661 tabRec->targets[elems]->offset - bxOffset);
buzbeee3acd072012-02-25 17:03:10 -0800662 }
663 } else {
Elliott Hughesadb8c672012-03-06 16:49:32 -0800664 DCHECK_EQ(static_cast<int>(tabRec->table[0]), static_cast<int>(Instruction::kPackedSwitchSignature));
buzbeee3acd072012-02-25 17:03:10 -0800665 for (int elems = 0; elems < tabRec->table[1]; elems++) {
buzbee31a4a6f2012-02-28 15:36:15 -0800666 int disp = tabRec->targets[elems]->offset - bxOffset;
buzbeee3acd072012-02-25 17:03:10 -0800667 if (cUnit->printMe) {
668 LOG(INFO) << " Case[" << elems << "] disp: 0x" <<
669 std::hex << disp;
670 }
671 pushWord(cUnit->codeBuffer,
buzbee31a4a6f2012-02-28 15:36:15 -0800672 tabRec->targets[elems]->offset - bxOffset);
buzbeee3acd072012-02-25 17:03:10 -0800673 }
674 }
675 }
676}
677
678/* Write the fill array dta to the output stream */
buzbee31a4a6f2012-02-28 15:36:15 -0800679void installFillArrayData(CompilationUnit* cUnit)
buzbeee3acd072012-02-25 17:03:10 -0800680{
681 GrowableListIterator iterator;
682 oatGrowableListIteratorInit(&cUnit->fillArrayData, &iterator);
683 while (true) {
684 FillArrayData *tabRec = (FillArrayData *) oatGrowableListIteratorNext(
685 &iterator);
686 if (tabRec == NULL) break;
687 alignBuffer(cUnit->codeBuffer, tabRec->offset);
Ian Rogersab058bb2012-03-11 22:19:38 -0700688 for (int i = 0; i < (tabRec->size + 1) / 2; i++) {
689 cUnit->codeBuffer.push_back( tabRec->table[i] & 0xFF);
690 cUnit->codeBuffer.push_back( (tabRec->table[i] >> 8) & 0xFF);
buzbeee3acd072012-02-25 17:03:10 -0800691 }
692 }
693}
694
buzbee31a4a6f2012-02-28 15:36:15 -0800695int assignLiteralOffsetCommon(LIR* lir, int offset)
buzbeee3acd072012-02-25 17:03:10 -0800696{
697 for (;lir != NULL; lir = lir->next) {
698 lir->offset = offset;
699 offset += 4;
700 }
701 return offset;
702}
703
buzbee31a4a6f2012-02-28 15:36:15 -0800704void createMappingTable(CompilationUnit* cUnit)
buzbeee3acd072012-02-25 17:03:10 -0800705{
buzbee31a4a6f2012-02-28 15:36:15 -0800706 LIR* tgtLIR;
buzbeee3acd072012-02-25 17:03:10 -0800707 int currentDalvikOffset = -1;
708
buzbee31a4a6f2012-02-28 15:36:15 -0800709 for (tgtLIR = (LIR *) cUnit->firstLIRInsn;
buzbeee3acd072012-02-25 17:03:10 -0800710 tgtLIR;
711 tgtLIR = NEXT_LIR(tgtLIR)) {
712 if ((tgtLIR->opcode >= 0) && !tgtLIR->flags.isNop &&
buzbee31a4a6f2012-02-28 15:36:15 -0800713 (currentDalvikOffset != tgtLIR->dalvikOffset)) {
buzbeee3acd072012-02-25 17:03:10 -0800714 // Changed - need to emit a record
buzbee31a4a6f2012-02-28 15:36:15 -0800715 cUnit->mappingTable.push_back(tgtLIR->offset);
716 cUnit->mappingTable.push_back(tgtLIR->dalvikOffset);
717 currentDalvikOffset = tgtLIR->dalvikOffset;
buzbeee3acd072012-02-25 17:03:10 -0800718 }
719 }
720}
721
722/* Determine the offset of each literal field */
buzbee31a4a6f2012-02-28 15:36:15 -0800723int assignLiteralOffset(CompilationUnit* cUnit, int offset)
buzbeee3acd072012-02-25 17:03:10 -0800724{
725 offset = assignLiteralOffsetCommon(cUnit->literalList, offset);
Ian Rogers3fa13792012-03-18 15:53:45 -0700726 offset = assignLiteralOffsetCommon(cUnit->codeLiteralList, offset);
727 offset = assignLiteralOffsetCommon(cUnit->methodLiteralList, offset);
buzbeee3acd072012-02-25 17:03:10 -0800728 return offset;
729}
730
buzbee31a4a6f2012-02-28 15:36:15 -0800731int assignSwitchTablesOffset(CompilationUnit* cUnit, int offset)
buzbeee3acd072012-02-25 17:03:10 -0800732{
733 GrowableListIterator iterator;
734 oatGrowableListIteratorInit(&cUnit->switchTables, &iterator);
735 while (true) {
736 SwitchTable *tabRec = (SwitchTable *) oatGrowableListIteratorNext(
737 &iterator);
738 if (tabRec == NULL) break;
739 tabRec->offset = offset;
Elliott Hughesadb8c672012-03-06 16:49:32 -0800740 if (tabRec->table[0] == Instruction::kSparseSwitchSignature) {
buzbeee3acd072012-02-25 17:03:10 -0800741 offset += tabRec->table[1] * (sizeof(int) * 2);
742 } else {
Elliott Hughesadb8c672012-03-06 16:49:32 -0800743 DCHECK_EQ(static_cast<int>(tabRec->table[0]), static_cast<int>(Instruction::kPackedSwitchSignature));
buzbeee3acd072012-02-25 17:03:10 -0800744 offset += tabRec->table[1] * sizeof(int);
745 }
746 }
747 return offset;
748}
749
buzbee31a4a6f2012-02-28 15:36:15 -0800750int assignFillArrayDataOffset(CompilationUnit* cUnit, int offset)
buzbeee3acd072012-02-25 17:03:10 -0800751{
752 GrowableListIterator iterator;
753 oatGrowableListIteratorInit(&cUnit->fillArrayData, &iterator);
754 while (true) {
755 FillArrayData *tabRec = (FillArrayData *) oatGrowableListIteratorNext(
756 &iterator);
757 if (tabRec == NULL) break;
758 tabRec->offset = offset;
759 offset += tabRec->size;
760 // word align
761 offset = (offset + 3) & ~3;
762 }
763 return offset;
764}
765
766/*
767 * Walk the compilation unit and assign offsets to instructions
768 * and literals and compute the total size of the compiled unit.
769 */
770void oatAssignOffsets(CompilationUnit* cUnit)
771{
772 int offset = oatAssignInsnOffsets(cUnit);
773
774 /* Const values have to be word aligned */
775 offset = (offset + 3) & ~3;
776
777 /* Set up offsets for literals */
778 cUnit->dataOffset = offset;
779
780 offset = assignLiteralOffset(cUnit, offset);
781
782 offset = assignSwitchTablesOffset(cUnit, offset);
783
784 offset = assignFillArrayDataOffset(cUnit, offset);
785
786 cUnit->totalSize = offset;
787}
788
789/*
790 * Go over each instruction in the list and calculate the offset from the top
791 * before sending them off to the assembler. If out-of-range branch distance is
792 * seen rearrange the instructions a bit to correct it.
793 */
794void oatAssembleLIR(CompilationUnit* cUnit)
795{
796 oatAssignOffsets(cUnit);
797 /*
798 * Assemble here. Note that we generate code with optimistic assumptions
799 * and if found now to work, we'll have to redo the sequence and retry.
800 */
801
802 while (true) {
803 AssemblerStatus res = oatAssembleInstructions(cUnit, 0);
804 if (res == kSuccess) {
805 break;
806 } else {
807 cUnit->assemblerRetries++;
808 if (cUnit->assemblerRetries > MAX_ASSEMBLER_RETRIES) {
809 LOG(FATAL) << "Assembler error - too many retries";
810 }
811 // Redo offsets and try again
812 oatAssignOffsets(cUnit);
813 cUnit->codeBuffer.clear();
814 }
815 }
816
817 // Install literals
818 installLiteralPools(cUnit);
819
820 // Install switch tables
821 installSwitchTables(cUnit);
822
823 // Install fill array data
824 installFillArrayData(cUnit);
825
826 /*
827 * Create the mapping table
828 */
829 createMappingTable(cUnit);
830}
831
buzbee31a4a6f2012-02-28 15:36:15 -0800832/*
833 * Insert a kPseudoCaseLabel at the beginning of the Dalvik
834 * offset vaddr. This label will be used to fix up the case
835 * branch table during the assembly phase. Be sure to set
836 * all resource flags on this to prevent code motion across
837 * target boundaries. KeyVal is just there for debugging.
838 */
839LIR* insertCaseLabel(CompilationUnit* cUnit, int vaddr, int keyVal)
840{
841 std::map<unsigned int, LIR*>::iterator it;
842 it = cUnit->boundaryMap.find(vaddr);
843 if (it == cUnit->boundaryMap.end()) {
844 LOG(FATAL) << "Error: didn't find vaddr 0x" << std::hex << vaddr;
845 }
846 LIR* newLabel = (LIR*)oatNew(cUnit, sizeof(LIR), true, kAllocLIR);
847 newLabel->dalvikOffset = vaddr;
848 newLabel->opcode = kPseudoCaseLabel;
849 newLabel->operands[0] = keyVal;
850 oatInsertLIRAfter(it->second, (LIR*)newLabel);
851 return newLabel;
852}
853
854void markPackedCaseLabels(CompilationUnit* cUnit, SwitchTable *tabRec)
855{
856 const u2* table = tabRec->table;
857 int baseVaddr = tabRec->vaddr;
858 int *targets = (int*)&table[4];
859 int entries = table[1];
860 int lowKey = s4FromSwitchData(&table[2]);
861 for (int i = 0; i < entries; i++) {
862 tabRec->targets[i] = insertCaseLabel(cUnit, baseVaddr + targets[i],
863 i + lowKey);
864 }
865}
866
867void markSparseCaseLabels(CompilationUnit* cUnit, SwitchTable *tabRec)
868{
869 const u2* table = tabRec->table;
870 int baseVaddr = tabRec->vaddr;
871 int entries = table[1];
872 int* keys = (int*)&table[2];
873 int* targets = &keys[entries];
874 for (int i = 0; i < entries; i++) {
875 tabRec->targets[i] = insertCaseLabel(cUnit, baseVaddr + targets[i],
876 keys[i]);
877 }
878}
879
880void oatProcessSwitchTables(CompilationUnit* cUnit)
881{
882 GrowableListIterator iterator;
883 oatGrowableListIteratorInit(&cUnit->switchTables, &iterator);
884 while (true) {
885 SwitchTable *tabRec = (SwitchTable *) oatGrowableListIteratorNext(
886 &iterator);
887 if (tabRec == NULL) break;
Elliott Hughesadb8c672012-03-06 16:49:32 -0800888 if (tabRec->table[0] == Instruction::kPackedSwitchSignature) {
buzbee31a4a6f2012-02-28 15:36:15 -0800889 markPackedCaseLabels(cUnit, tabRec);
Elliott Hughesadb8c672012-03-06 16:49:32 -0800890 } else if (tabRec->table[0] == Instruction::kSparseSwitchSignature) {
buzbee31a4a6f2012-02-28 15:36:15 -0800891 markSparseCaseLabels(cUnit, tabRec);
Elliott Hughesadb8c672012-03-06 16:49:32 -0800892 } else {
buzbee31a4a6f2012-02-28 15:36:15 -0800893 LOG(FATAL) << "Invalid switch table";
894 }
895 }
896}
897
898//FIXME: Do we have endian issues here?
899
900void dumpSparseSwitchTable(const u2* table)
901 /*
902 * Sparse switch data format:
903 * ushort ident = 0x0200 magic value
904 * ushort size number of entries in the table; > 0
905 * int keys[size] keys, sorted low-to-high; 32-bit aligned
906 * int targets[size] branch targets, relative to switch opcode
907 *
908 * Total size is (2+size*4) 16-bit code units.
909 */
910{
911 u2 ident = table[0];
912 int entries = table[1];
913 int* keys = (int*)&table[2];
914 int* targets = &keys[entries];
915 LOG(INFO) << "Sparse switch table - ident:0x" << std::hex << ident <<
916 ", entries: " << std::dec << entries;
917 for (int i = 0; i < entries; i++) {
918 LOG(INFO) << " Key[" << keys[i] << "] -> 0x" << std::hex <<
919 targets[i];
920 }
921}
922
923void dumpPackedSwitchTable(const u2* table)
924 /*
925 * Packed switch data format:
926 * ushort ident = 0x0100 magic value
927 * ushort size number of entries in the table
928 * int first_key first (and lowest) switch case value
929 * int targets[size] branch targets, relative to switch opcode
930 *
931 * Total size is (4+size*2) 16-bit code units.
932 */
933{
934 u2 ident = table[0];
935 int* targets = (int*)&table[4];
936 int entries = table[1];
937 int lowKey = s4FromSwitchData(&table[2]);
938 LOG(INFO) << "Packed switch table - ident:0x" << std::hex << ident <<
939 ", entries: " << std::dec << entries << ", lowKey: " << lowKey;
940 for (int i = 0; i < entries; i++) {
941 LOG(INFO) << " Key[" << (i + lowKey) << "] -> 0x" << std::hex <<
942 targets[i];
943 }
944}
buzbeee3acd072012-02-25 17:03:10 -0800945
946
947} // namespace art