blob: c189eb27956c7f6de72b2e8f035e197765c3f317 [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{
Bill Buzbeea114add2012-05-03 15:00:40 -070021 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 }
buzbee31a4a6f2012-02-28 15:36:15 -080051}
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{
Bill Buzbeea114add2012-05-03 15:00:40 -070058 setMemRefType(lir, isLoad, kDalvikReg);
buzbee31a4a6f2012-02-28 15:36:15 -080059
Bill Buzbeea114add2012-05-03 15:00:40 -070060 /*
61 * Store the Dalvik register id in aliasInfo. Mark the MSB if it is a 64-bit
62 * access.
63 */
64 lir->aliasInfo = regId;
65 if (is64bit) {
66 lir->aliasInfo |= 0x80000000;
67 }
buzbee31a4a6f2012-02-28 15:36:15 -080068}
69
70/*
71 * Decode the register id.
72 */
73inline u8 getRegMaskCommon(int reg)
74{
Bill Buzbeea114add2012-05-03 15:00:40 -070075 u8 seed;
76 int shift;
77 int regId = reg & 0x1f;
buzbee31a4a6f2012-02-28 15:36:15 -080078
Bill Buzbeea114add2012-05-03 15:00:40 -070079 /*
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);
buzbee31a4a6f2012-02-28 15:36:15 -080088}
89
90/*
91 * Mark the corresponding bit(s).
92 */
93inline void setupRegMask(u8* mask, int reg)
94{
Bill Buzbeea114add2012-05-03 15:00:40 -070095 *mask |= getRegMaskCommon(reg);
buzbee31a4a6f2012-02-28 15:36:15 -080096}
97
98/*
99 * Set up the proper fields in the resource mask
100 */
101void setupResourceMasks(LIR* lir)
102{
Bill Buzbeea114add2012-05-03 15:00:40 -0700103 int opcode = lir->opcode;
104 int flags;
buzbee31a4a6f2012-02-28 15:36:15 -0800105
Bill Buzbeea114add2012-05-03 15:00:40 -0700106 if (opcode <= 0) {
107 lir->useMask = lir->defMask = 0;
108 return;
109 }
buzbee31a4a6f2012-02-28 15:36:15 -0800110
Bill Buzbeea114add2012-05-03 15:00:40 -0700111 flags = EncodingMap[lir->opcode].flags;
buzbee31a4a6f2012-02-28 15:36:15 -0800112
Bill Buzbeea114add2012-05-03 15:00:40 -0700113 if (flags & NEEDS_FIXUP) {
114 lir->flags.pcRelFixup = true;
115 }
buzbee31a4a6f2012-02-28 15:36:15 -0800116
Bill Buzbeea114add2012-05-03 15:00:40 -0700117 /* Get the starting size of the instruction's template */
118 lir->flags.size = oatGetInsnSize(lir);
buzbeee88dfbf2012-03-05 11:19:57 -0800119
Bill Buzbeea114add2012-05-03 15:00:40 -0700120 /* 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 }
buzbee31a4a6f2012-02-28 15:36:15 -0800125
Bill Buzbeea114add2012-05-03 15:00:40 -0700126 /*
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 }
buzbee31a4a6f2012-02-28 15:36:15 -0800134
Bill Buzbeea114add2012-05-03 15:00:40 -0700135 if (flags & REG_DEF0) {
136 setupRegMask(&lir->defMask, lir->operands[0]);
137 }
buzbee31a4a6f2012-02-28 15:36:15 -0800138
Bill Buzbeea114add2012-05-03 15:00:40 -0700139 if (flags & REG_DEF1) {
140 setupRegMask(&lir->defMask, lir->operands[1]);
141 }
buzbee31a4a6f2012-02-28 15:36:15 -0800142
Bill Buzbeea114add2012-05-03 15:00:40 -0700143 if (flags & REG_DEF_SP) {
144 lir->defMask |= ENCODE_REG_SP;
145 }
buzbee31a4a6f2012-02-28 15:36:15 -0800146
buzbeea7678db2012-03-05 15:35:46 -0800147#if !defined(TARGET_X86)
Bill Buzbeea114add2012-05-03 15:00:40 -0700148 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
Bill Buzbeea114add2012-05-03 15:00:40 -0700153 if (flags & REG_DEF_LIST0) {
154 lir->defMask |= ENCODE_REG_LIST(lir->operands[0]);
155 }
buzbee31a4a6f2012-02-28 15:36:15 -0800156
Bill Buzbeea114add2012-05-03 15:00:40 -0700157 if (flags & REG_DEF_LIST1) {
158 lir->defMask |= ENCODE_REG_LIST(lir->operands[1]);
159 }
buzbee31a4a6f2012-02-28 15:36:15 -0800160
buzbee5de34942012-03-01 14:51:57 -0800161#if defined(TARGET_ARM)
Bill Buzbeea114add2012-05-03 15:00:40 -0700162 if (flags & REG_DEF_FPCS_LIST0) {
163 lir->defMask |= ENCODE_REG_FPCS_LIST(lir->operands[0]);
164 }
buzbee31a4a6f2012-02-28 15:36:15 -0800165
Bill Buzbeea114add2012-05-03 15:00:40 -0700166 if (flags & REG_DEF_FPCS_LIST2) {
167 for (int i = 0; i < lir->operands[2]; i++) {
168 setupRegMask(&lir->defMask, lir->operands[1] + i);
buzbee31a4a6f2012-02-28 15:36:15 -0800169 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700170 }
buzbee5de34942012-03-01 14:51:57 -0800171#endif
buzbee31a4a6f2012-02-28 15:36:15 -0800172
Bill Buzbeea114add2012-05-03 15:00:40 -0700173 if (flags & SETS_CCODES) {
174 lir->defMask |= ENCODE_CCODE;
175 }
buzbee31a4a6f2012-02-28 15:36:15 -0800176
177#if defined(TARGET_ARM)
Bill Buzbeea114add2012-05-03 15:00:40 -0700178 /* Conservatively treat the IT block */
179 if (flags & IS_IT) {
180 lir->defMask = ENCODE_ALL;
181 }
buzbee31a4a6f2012-02-28 15:36:15 -0800182#endif
183
Bill Buzbeea114add2012-05-03 15:00:40 -0700184 if (flags & (REG_USE0 | REG_USE1 | REG_USE2 | REG_USE3)) {
185 int i;
buzbee31a4a6f2012-02-28 15:36:15 -0800186
Bill Buzbeea114add2012-05-03 15:00:40 -0700187 for (i = 0; i < 4; i++) {
188 if (flags & (1 << (kRegUse0 + i))) {
189 setupRegMask(&lir->useMask, lir->operands[i]);
190 }
buzbee31a4a6f2012-02-28 15:36:15 -0800191 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700192 }
buzbee31a4a6f2012-02-28 15:36:15 -0800193
buzbeea7678db2012-03-05 15:35:46 -0800194#if defined(TARGET_ARM)
Bill Buzbeea114add2012-05-03 15:00:40 -0700195 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
Bill Buzbeea114add2012-05-03 15:00:40 -0700200 if (flags & REG_USE_SP) {
201 lir->useMask |= ENCODE_REG_SP;
202 }
buzbee31a4a6f2012-02-28 15:36:15 -0800203
Bill Buzbeea114add2012-05-03 15:00:40 -0700204 if (flags & REG_USE_LIST0) {
205 lir->useMask |= ENCODE_REG_LIST(lir->operands[0]);
206 }
buzbee31a4a6f2012-02-28 15:36:15 -0800207
Bill Buzbeea114add2012-05-03 15:00:40 -0700208 if (flags & REG_USE_LIST1) {
209 lir->useMask |= ENCODE_REG_LIST(lir->operands[1]);
210 }
buzbee31a4a6f2012-02-28 15:36:15 -0800211
buzbee5de34942012-03-01 14:51:57 -0800212#if defined(TARGET_ARM)
Bill Buzbeea114add2012-05-03 15:00:40 -0700213 if (flags & REG_USE_FPCS_LIST0) {
214 lir->useMask |= ENCODE_REG_FPCS_LIST(lir->operands[0]);
215 }
buzbee31a4a6f2012-02-28 15:36:15 -0800216
Bill Buzbeea114add2012-05-03 15:00:40 -0700217 if (flags & REG_USE_FPCS_LIST2) {
218 for (int i = 0; i < lir->operands[2]; i++) {
219 setupRegMask(&lir->useMask, lir->operands[1] + i);
buzbee31a4a6f2012-02-28 15:36:15 -0800220 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700221 }
buzbee5de34942012-03-01 14:51:57 -0800222#endif
buzbee31a4a6f2012-02-28 15:36:15 -0800223
Bill Buzbeea114add2012-05-03 15:00:40 -0700224 if (flags & USES_CCODES) {
225 lir->useMask |= ENCODE_CCODE;
226 }
buzbee31a4a6f2012-02-28 15:36:15 -0800227
228#if defined(TARGET_ARM)
Bill Buzbeea114add2012-05-03 15:00:40 -0700229 /* 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;
buzbee31a4a6f2012-02-28 15:36:15 -0800238 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700239 }
buzbee31a4a6f2012-02-28 15:36:15 -0800240#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{
Bill Buzbeea114add2012-05-03 15:00:40 -0700252 LIR* lir = (LIR*) arg;
253 int offset = lir->offset;
254 int dest = lir->operands[0];
255 const bool dumpNop = (cUnit->enableDebug & (1 << kDebugShowNops));
buzbee5de34942012-03-01 14:51:57 -0800256
Bill Buzbeea114add2012-05-03 15:00:40 -0700257 /* 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" << (void*)lir << ":";
295 break;
296 case kPseudoThrowTarget:
297 LOG(INFO) << "LT" << (void*)lir << ":";
298 break;
299 case kPseudoIntrinsicRetry:
300 LOG(INFO) << "IR" << (void*)lir << ":";
301 break;
302 case kPseudoSuspendTarget:
303 LOG(INFO) << "LS" << (void*)lir << ":";
304 break;
305 case kPseudoCaseLabel:
306 LOG(INFO) << "LC" << (void*)lir << ": Case target 0x"
307 << std::hex << lir->operands[0] << "|" << std::dec <<
308 lir->operands[0];
309 break;
310 default:
311 if (lir->flags.isNop && !dumpNop) {
312 break;
313 } else {
314 std::string op_name(buildInsnString(EncodingMap[lir->opcode].name,
315 lir, baseAddr));
316 std::string op_operands(buildInsnString(EncodingMap[lir->opcode].fmt
317 , lir, baseAddr));
318 LOG(INFO) << StringPrintf("%05x: %-9s%s%s",
319 (unsigned int)(baseAddr + offset),
320 op_name.c_str(), op_operands.c_str(),
321 lir->flags.isNop ? "(nop)" : "");
322 }
323 break;
324 }
buzbee5de34942012-03-01 14:51:57 -0800325
Bill Buzbeea114add2012-05-03 15:00:40 -0700326 if (lir->useMask && (!lir->flags.isNop || dumpNop)) {
327 DUMP_RESOURCE_MASK(oatDumpResourceMask((LIR* ) lir, lir->useMask, "use"));
328 }
329 if (lir->defMask && (!lir->flags.isNop || dumpNop)) {
330 DUMP_RESOURCE_MASK(oatDumpResourceMask((LIR* ) lir, lir->defMask, "def"));
331 }
buzbee5de34942012-03-01 14:51:57 -0800332}
333
334void oatDumpPromotionMap(CompilationUnit *cUnit)
335{
Bill Buzbeea114add2012-05-03 15:00:40 -0700336 int numRegs = cUnit->numDalvikRegisters + cUnit->numCompilerTemps + 1;
337 for (int i = 0; i < numRegs; i++) {
338 PromotionMap vRegMap = cUnit->promotionMap[i];
339 std::string buf;
340 if (vRegMap.fpLocation == kLocPhysReg) {
341 StringAppendF(&buf, " : s%d", vRegMap.fpReg & FP_REG_MASK);
buzbee5de34942012-03-01 14:51:57 -0800342 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700343
344 std::string buf3;
345 if (i < cUnit->numDalvikRegisters) {
346 StringAppendF(&buf3, "%02d", i);
347 } else if (i == cUnit->methodSReg) {
348 buf3 = "Method*";
349 } else {
350 StringAppendF(&buf3, "ct%d", i - cUnit->numDalvikRegisters);
351 }
352
353 LOG(INFO) << StringPrintf("V[%s] -> %s%d%s", buf3.c_str(),
354 vRegMap.coreLocation == kLocPhysReg ?
355 "r" : "SP+", vRegMap.coreLocation == kLocPhysReg ?
356 vRegMap.coreReg : oatSRegOffset(cUnit, i),
357 buf.c_str());
358 }
buzbee5de34942012-03-01 14:51:57 -0800359}
360
buzbee5de34942012-03-01 14:51:57 -0800361/* Dump instructions and constant pool contents */
362void oatCodegenDump(CompilationUnit* cUnit)
363{
Bill Buzbeea114add2012-05-03 15:00:40 -0700364 LOG(INFO) << "Dumping LIR insns for "
365 << PrettyMethod(cUnit->method_idx, *cUnit->dex_file);
366 LIR* lirInsn;
367 LIR* thisLIR;
368 int insnsSize = cUnit->insnsSize;
buzbee5de34942012-03-01 14:51:57 -0800369
Bill Buzbeea114add2012-05-03 15:00:40 -0700370 LOG(INFO) << "Regs (excluding ins) : " << cUnit->numRegs;
371 LOG(INFO) << "Ins : " << cUnit->numIns;
372 LOG(INFO) << "Outs : " << cUnit->numOuts;
373 LOG(INFO) << "CoreSpills : " << cUnit->numCoreSpills;
374 LOG(INFO) << "FPSpills : " << cUnit->numFPSpills;
375 LOG(INFO) << "CompilerTemps : " << cUnit->numCompilerTemps;
376 LOG(INFO) << "Frame size : " << cUnit->frameSize;
377 LOG(INFO) << "code size is " << cUnit->totalSize <<
378 " bytes, Dalvik size is " << insnsSize * 2;
379 LOG(INFO) << "expansion factor: "
380 << (float)cUnit->totalSize / (float)(insnsSize * 2);
381 oatDumpPromotionMap(cUnit);
382 for (lirInsn = cUnit->firstLIRInsn; lirInsn; lirInsn = lirInsn->next) {
383 oatDumpLIRInsn(cUnit, lirInsn, 0);
384 }
385 for (lirInsn = cUnit->classPointerList; lirInsn; lirInsn = lirInsn->next) {
386 thisLIR = (LIR*) lirInsn;
387 LOG(INFO) << StringPrintf("%x (%04x): .class (%s)",
388 thisLIR->offset, thisLIR->offset,
389 ((CallsiteInfo *)
390 thisLIR->operands[0])->classDescriptor);
391 }
392 for (lirInsn = cUnit->literalList; lirInsn; lirInsn = lirInsn->next) {
393 thisLIR = (LIR*) lirInsn;
394 LOG(INFO) << StringPrintf("%x (%04x): .word (%#x)",
395 thisLIR->offset, thisLIR->offset,
396 thisLIR->operands[0]);
397 }
buzbee5de34942012-03-01 14:51:57 -0800398
Bill Buzbeea114add2012-05-03 15:00:40 -0700399 const DexFile::MethodId& method_id =
400 cUnit->dex_file->GetMethodId(cUnit->method_idx);
401 std::string signature(cUnit->dex_file->GetMethodSignature(method_id));
402 std::string name(cUnit->dex_file->GetMethodName(method_id));
403 std::string descriptor(cUnit->dex_file->GetMethodDeclaringClassDescriptor(method_id));
buzbee5de34942012-03-01 14:51:57 -0800404
Bill Buzbeea114add2012-05-03 15:00:40 -0700405 // Dump mapping table
406 if (cUnit->mappingTable.size() > 0) {
407 std::string
408 line(StringPrintf("\n MappingTable %s%s_%s_mappingTable[%zu] = {",
409 descriptor.c_str(), name.c_str(), signature.c_str(),
410 cUnit->mappingTable.size()));
411 std::replace(line.begin(), line.end(), ';', '_');
412 LOG(INFO) << line;
413 for (uint32_t i = 0; i < cUnit->mappingTable.size(); i+=2) {
414 line = StringPrintf(" {0x%05x, 0x%04x},",
415 cUnit->mappingTable[i], cUnit->mappingTable[i+1]);
416 LOG(INFO) << line;
buzbee5de34942012-03-01 14:51:57 -0800417 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700418 LOG(INFO) <<" };\n\n";
419 }
buzbee5de34942012-03-01 14:51:57 -0800420}
421
buzbeea2ebdd72012-03-04 14:57:06 -0800422
423LIR* rawLIR(CompilationUnit* cUnit, int dalvikOffset, int opcode, int op0,
Bill Buzbeea114add2012-05-03 15:00:40 -0700424 int op1, int op2, int op3, int op4, LIR* target)
buzbeea2ebdd72012-03-04 14:57:06 -0800425{
Bill Buzbeea114add2012-05-03 15:00:40 -0700426 LIR* insn = (LIR* ) oatNew(cUnit, sizeof(LIR), true, kAllocLIR);
427 insn->dalvikOffset = dalvikOffset;
428 insn->opcode = opcode;
429 insn->operands[0] = op0;
430 insn->operands[1] = op1;
431 insn->operands[2] = op2;
432 insn->operands[3] = op3;
433 insn->operands[4] = op4;
434 insn->target = target;
435 oatSetupResourceMasks(insn);
436 if (opcode == kPseudoTargetLabel) {
437 // Always make labels scheduling barriers
438 insn->defMask = ENCODE_ALL;
439 }
440 return insn;
buzbeea2ebdd72012-03-04 14:57:06 -0800441}
442
buzbee5de34942012-03-01 14:51:57 -0800443/*
buzbee31a4a6f2012-02-28 15:36:15 -0800444 * The following are building blocks to construct low-level IRs with 0 - 4
445 * operands.
446 */
buzbee5de34942012-03-01 14:51:57 -0800447LIR* newLIR0(CompilationUnit* cUnit, int opcode)
buzbee31a4a6f2012-02-28 15:36:15 -0800448{
Bill Buzbeea114add2012-05-03 15:00:40 -0700449 DCHECK(isPseudoOpcode(opcode) || (EncodingMap[opcode].flags & NO_OPERAND))
450 << EncodingMap[opcode].name << " " << (int)opcode << " "
451 << PrettyMethod(cUnit->method_idx, *cUnit->dex_file) << " "
452 << cUnit->currentDalvikOffset;
453 LIR* insn = rawLIR(cUnit, cUnit->currentDalvikOffset, opcode);
454 oatAppendLIR(cUnit, (LIR*) insn);
455 return insn;
buzbee31a4a6f2012-02-28 15:36:15 -0800456}
457
buzbee5de34942012-03-01 14:51:57 -0800458LIR* newLIR1(CompilationUnit* cUnit, int opcode,
Bill Buzbeea114add2012-05-03 15:00:40 -0700459 int dest)
buzbee31a4a6f2012-02-28 15:36:15 -0800460{
Bill Buzbeea114add2012-05-03 15:00:40 -0700461 DCHECK(isPseudoOpcode(opcode) || (EncodingMap[opcode].flags & IS_UNARY_OP))
462 << EncodingMap[opcode].name << " " << (int)opcode << " "
463 << PrettyMethod(cUnit->method_idx, *cUnit->dex_file) << " "
464 << cUnit->currentDalvikOffset;
465 LIR* insn = rawLIR(cUnit, cUnit->currentDalvikOffset, opcode, dest);
466 oatAppendLIR(cUnit, (LIR*) insn);
467 return insn;
buzbee31a4a6f2012-02-28 15:36:15 -0800468}
469
buzbee5de34942012-03-01 14:51:57 -0800470LIR* newLIR2(CompilationUnit* cUnit, int opcode,
Bill Buzbeea114add2012-05-03 15:00:40 -0700471 int dest, int src1)
buzbee31a4a6f2012-02-28 15:36:15 -0800472{
Bill Buzbeea114add2012-05-03 15:00:40 -0700473 DCHECK(isPseudoOpcode(opcode) || (EncodingMap[opcode].flags & IS_BINARY_OP))
474 << EncodingMap[opcode].name << " " << (int)opcode << " "
475 << PrettyMethod(cUnit->method_idx, *cUnit->dex_file) << " "
476 << cUnit->currentDalvikOffset;
477 LIR* insn = rawLIR(cUnit, cUnit->currentDalvikOffset, opcode, dest, src1);
478 oatAppendLIR(cUnit, (LIR*) insn);
479 return insn;
buzbee31a4a6f2012-02-28 15:36:15 -0800480}
481
buzbee5de34942012-03-01 14:51:57 -0800482LIR* newLIR3(CompilationUnit* cUnit, int opcode,
Bill Buzbeea114add2012-05-03 15:00:40 -0700483 int dest, int src1, int src2)
buzbee31a4a6f2012-02-28 15:36:15 -0800484{
Bill Buzbeea114add2012-05-03 15:00:40 -0700485 DCHECK(isPseudoOpcode(opcode) || (EncodingMap[opcode].flags & IS_TERTIARY_OP))
486 << EncodingMap[opcode].name << " " << (int)opcode << " "
487 << PrettyMethod(cUnit->method_idx, *cUnit->dex_file) << " "
488 << cUnit->currentDalvikOffset;
489 LIR* insn = rawLIR(cUnit, cUnit->currentDalvikOffset, opcode, dest, src1,
490 src2);
491 oatAppendLIR(cUnit, (LIR*) insn);
492 return insn;
buzbee31a4a6f2012-02-28 15:36:15 -0800493}
494
buzbee5de34942012-03-01 14:51:57 -0800495LIR* newLIR4(CompilationUnit* cUnit, int opcode,
Bill Buzbeea114add2012-05-03 15:00:40 -0700496 int dest, int src1, int src2, int info)
buzbee31a4a6f2012-02-28 15:36:15 -0800497{
Bill Buzbeea114add2012-05-03 15:00:40 -0700498 DCHECK(isPseudoOpcode(opcode) || (EncodingMap[opcode].flags & IS_QUAD_OP))
499 << EncodingMap[opcode].name << " " << (int)opcode << " "
500 << PrettyMethod(cUnit->method_idx, *cUnit->dex_file) << " "
501 << cUnit->currentDalvikOffset;
502 LIR* insn = rawLIR(cUnit, cUnit->currentDalvikOffset, opcode, dest, src1,
503 src2, info);
504 oatAppendLIR(cUnit, (LIR*) insn);
505 return insn;
buzbee31a4a6f2012-02-28 15:36:15 -0800506}
buzbee31a4a6f2012-02-28 15:36:15 -0800507
Ian Rogersb5d09b22012-03-06 22:14:17 -0800508LIR* newLIR5(CompilationUnit* cUnit, int opcode,
Bill Buzbeea114add2012-05-03 15:00:40 -0700509 int dest, int src1, int src2, int info1, int info2)
Ian Rogersb5d09b22012-03-06 22:14:17 -0800510{
Bill Buzbeea114add2012-05-03 15:00:40 -0700511 DCHECK(isPseudoOpcode(opcode) || (EncodingMap[opcode].flags & IS_QUIN_OP))
512 << EncodingMap[opcode].name << " " << (int)opcode << " "
513 << PrettyMethod(cUnit->method_idx, *cUnit->dex_file) << " "
514 << cUnit->currentDalvikOffset;
515 LIR* insn = rawLIR(cUnit, cUnit->currentDalvikOffset, opcode, dest, src1,
516 src2, info1, info2);
517 oatAppendLIR(cUnit, (LIR*) insn);
518 return insn;
Ian Rogersb5d09b22012-03-06 22:14:17 -0800519}
520
buzbee31a4a6f2012-02-28 15:36:15 -0800521/*
522 * Search the existing constants in the literal pool for an exact or close match
523 * within specified delta (greater or equal to 0).
524 */
525LIR* scanLiteralPool(LIR* dataTarget, int value, unsigned int delta)
526{
Bill Buzbeea114add2012-05-03 15:00:40 -0700527 while (dataTarget) {
528 if (((unsigned) (value - ((LIR* ) dataTarget)->operands[0])) <= delta)
529 return (LIR* ) dataTarget;
530 dataTarget = dataTarget->next;
531 }
532 return NULL;
buzbee31a4a6f2012-02-28 15:36:15 -0800533}
534
535/* Search the existing constants in the literal pool for an exact wide match */
536LIR* scanLiteralPoolWide(LIR* dataTarget, int valLo, int valHi)
537{
Bill Buzbeea114add2012-05-03 15:00:40 -0700538 bool loMatch = false;
539 LIR* loTarget = NULL;
540 while (dataTarget) {
541 if (loMatch && (((LIR*)dataTarget)->operands[0] == valHi)) {
542 return (LIR*)loTarget;
buzbee31a4a6f2012-02-28 15:36:15 -0800543 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700544 loMatch = false;
545 if (((LIR*)dataTarget)->operands[0] == valLo) {
546 loMatch = true;
547 loTarget = dataTarget;
548 }
549 dataTarget = dataTarget->next;
550 }
551 return NULL;
buzbee31a4a6f2012-02-28 15:36:15 -0800552}
553
554/*
555 * The following are building blocks to insert constants into the pool or
556 * instruction streams.
557 */
558
buzbee5de34942012-03-01 14:51:57 -0800559/* Add a 32-bit constant either in the constant pool */
Ian Rogers3fa13792012-03-18 15:53:45 -0700560LIR* addWordData(CompilationUnit* cUnit, LIR* *constantListP, int value)
buzbee31a4a6f2012-02-28 15:36:15 -0800561{
Bill Buzbeea114add2012-05-03 15:00:40 -0700562 /* Add the constant to the literal pool */
563 if (constantListP) {
564 LIR* newValue = (LIR* ) oatNew(cUnit, sizeof(LIR), true, kAllocData);
565 newValue->operands[0] = value;
566 newValue->next = *constantListP;
567 *constantListP = (LIR*) newValue;
568 return newValue;
569 }
570 return NULL;
buzbee31a4a6f2012-02-28 15:36:15 -0800571}
572
573/* Add a 64-bit constant to the constant pool or mixed with code */
574LIR* addWideData(CompilationUnit* cUnit, LIR* *constantListP,
Bill Buzbeea114add2012-05-03 15:00:40 -0700575 int valLo, int valHi)
buzbee31a4a6f2012-02-28 15:36:15 -0800576{
Bill Buzbeea114add2012-05-03 15:00:40 -0700577 //FIXME: hard-coded little endian, need BE variant
578 // Insert high word into list first
579 addWordData(cUnit, constantListP, valHi);
580 return addWordData(cUnit, constantListP, valLo);
buzbee31a4a6f2012-02-28 15:36:15 -0800581}
582
Ian Rogersab058bb2012-03-11 22:19:38 -0700583void pushWord(std::vector<uint8_t>&buf, int data) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700584 buf.push_back( data & 0xff);
585 buf.push_back( (data >> 8) & 0xff);
586 buf.push_back( (data >> 16) & 0xff);
587 buf.push_back( (data >> 24) & 0xff);
buzbeee3acd072012-02-25 17:03:10 -0800588}
589
Ian Rogersab058bb2012-03-11 22:19:38 -0700590void alignBuffer(std::vector<uint8_t>&buf, size_t offset) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700591 while (buf.size() < offset) {
592 buf.push_back(0);
593 }
buzbeee3acd072012-02-25 17:03:10 -0800594}
595
Brian Carlstromf5822582012-03-19 22:34:31 -0700596bool IsDirect(int invokeType) {
597 InvokeType type = static_cast<InvokeType>(invokeType);
598 return type == kStatic || type == kDirect;
599}
600
buzbeee3acd072012-02-25 17:03:10 -0800601/* Write the literal pool to the output stream */
buzbee31a4a6f2012-02-28 15:36:15 -0800602void installLiteralPools(CompilationUnit* cUnit)
buzbeee3acd072012-02-25 17:03:10 -0800603{
Bill Buzbeea114add2012-05-03 15:00:40 -0700604 alignBuffer(cUnit->codeBuffer, cUnit->dataOffset);
605 LIR* dataLIR = cUnit->literalList;
606 while (dataLIR != NULL) {
607 pushWord(cUnit->codeBuffer, dataLIR->operands[0]);
608 dataLIR = NEXT_LIR(dataLIR);
609 }
610 // Push code and method literals, record offsets for the compiler to patch.
611 dataLIR = cUnit->codeLiteralList;
612 if (dataLIR != NULL) {
buzbeee3acd072012-02-25 17:03:10 -0800613 while (dataLIR != NULL) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700614 uint32_t target = dataLIR->operands[0];
615 cUnit->compiler->AddCodePatch(cUnit->dex_cache, cUnit->dex_file,
616 cUnit->method_idx,
617 cUnit->access_flags,
618 target,
619 IsDirect(dataLIR->operands[1]),
620 cUnit->codeBuffer.size());
621 const DexFile::MethodId& id = cUnit->dex_file->GetMethodId(target);
622 // unique based on target to ensure code deduplication works
623 uint32_t unique_patch_value = reinterpret_cast<uint32_t>(&id);
624 pushWord(cUnit->codeBuffer, unique_patch_value);
625 dataLIR = NEXT_LIR(dataLIR);
buzbeee3acd072012-02-25 17:03:10 -0800626 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700627 dataLIR = cUnit->methodLiteralList;
628 while (dataLIR != NULL) {
629 uint32_t target = dataLIR->operands[0];
630 cUnit->compiler->AddMethodPatch(cUnit->dex_cache, cUnit->dex_file,
631 cUnit->method_idx,
632 cUnit->access_flags,
633 target,
634 IsDirect(dataLIR->operands[1]),
635 cUnit->codeBuffer.size());
636 const DexFile::MethodId& id = cUnit->dex_file->GetMethodId(target);
637 // unique based on target to ensure code deduplication works
638 uint32_t unique_patch_value = reinterpret_cast<uint32_t>(&id);
639 pushWord(cUnit->codeBuffer, unique_patch_value);
640 dataLIR = NEXT_LIR(dataLIR);
Ian Rogers3fa13792012-03-18 15:53:45 -0700641 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700642 }
Ian Rogers3fa13792012-03-18 15:53:45 -0700643
buzbeee3acd072012-02-25 17:03:10 -0800644}
645
646/* Write the switch tables to the output stream */
buzbee31a4a6f2012-02-28 15:36:15 -0800647void installSwitchTables(CompilationUnit* cUnit)
buzbeee3acd072012-02-25 17:03:10 -0800648{
Bill Buzbeea114add2012-05-03 15:00:40 -0700649 GrowableListIterator iterator;
650 oatGrowableListIteratorInit(&cUnit->switchTables, &iterator);
651 while (true) {
652 SwitchTable* tabRec = (SwitchTable *) oatGrowableListIteratorNext(
653 &iterator);
654 if (tabRec == NULL) break;
655 alignBuffer(cUnit->codeBuffer, tabRec->offset);
656 /*
657 * For Arm, our reference point is the address of the bx
658 * instruction that does the launch, so we have to subtract
659 * the auto pc-advance. For other targets the reference point
660 * is a label, so we can use the offset as-is.
661 */
buzbeec5159d52012-03-03 11:48:39 -0800662#if defined(TARGET_ARM)
Bill Buzbeea114add2012-05-03 15:00:40 -0700663 int bxOffset = tabRec->anchor->offset + 4;
Ian Rogers7caad772012-03-30 01:07:54 -0700664#elif defined(TARGET_X86)
Bill Buzbeea114add2012-05-03 15:00:40 -0700665 int bxOffset = 0;
buzbeec5159d52012-03-03 11:48:39 -0800666#else
Bill Buzbeea114add2012-05-03 15:00:40 -0700667 int bxOffset = tabRec->anchor->offset;
buzbeec5159d52012-03-03 11:48:39 -0800668#endif
Bill Buzbeea114add2012-05-03 15:00:40 -0700669 if (cUnit->printMe) {
670 LOG(INFO) << "Switch table for offset 0x" << std::hex << bxOffset;
buzbeee3acd072012-02-25 17:03:10 -0800671 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700672 if (tabRec->table[0] == Instruction::kSparseSwitchSignature) {
673 int* keys = (int*)&(tabRec->table[2]);
674 for (int elems = 0; elems < tabRec->table[1]; elems++) {
675 int disp = tabRec->targets[elems]->offset - bxOffset;
676 if (cUnit->printMe) {
677 LOG(INFO) << " Case[" << elems << "] key: 0x"
678 << std::hex << keys[elems] << ", disp: 0x"
679 << std::hex << disp;
680 }
681 pushWord(cUnit->codeBuffer, keys[elems]);
682 pushWord(cUnit->codeBuffer,
683 tabRec->targets[elems]->offset - bxOffset);
684 }
685 } else {
686 DCHECK_EQ(static_cast<int>(tabRec->table[0]),
687 static_cast<int>(Instruction::kPackedSwitchSignature));
688 for (int elems = 0; elems < tabRec->table[1]; elems++) {
689 int disp = tabRec->targets[elems]->offset - bxOffset;
690 if (cUnit->printMe) {
691 LOG(INFO) << " Case[" << elems << "] disp: 0x"
692 << std::hex << disp;
693 }
694 pushWord(cUnit->codeBuffer, tabRec->targets[elems]->offset - bxOffset);
695 }
696 }
697 }
buzbeee3acd072012-02-25 17:03:10 -0800698}
699
700/* Write the fill array dta to the output stream */
buzbee31a4a6f2012-02-28 15:36:15 -0800701void installFillArrayData(CompilationUnit* cUnit)
buzbeee3acd072012-02-25 17:03:10 -0800702{
Bill Buzbeea114add2012-05-03 15:00:40 -0700703 GrowableListIterator iterator;
704 oatGrowableListIteratorInit(&cUnit->fillArrayData, &iterator);
705 while (true) {
706 FillArrayData *tabRec = (FillArrayData *) oatGrowableListIteratorNext(
707 &iterator);
708 if (tabRec == NULL) break;
709 alignBuffer(cUnit->codeBuffer, tabRec->offset);
710 for (int i = 0; i < (tabRec->size + 1) / 2; i++) {
711 cUnit->codeBuffer.push_back( tabRec->table[i] & 0xFF);
712 cUnit->codeBuffer.push_back( (tabRec->table[i] >> 8) & 0xFF);
buzbeee3acd072012-02-25 17:03:10 -0800713 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700714 }
buzbeee3acd072012-02-25 17:03:10 -0800715}
716
buzbee31a4a6f2012-02-28 15:36:15 -0800717int assignLiteralOffsetCommon(LIR* lir, int offset)
buzbeee3acd072012-02-25 17:03:10 -0800718{
Bill Buzbeea114add2012-05-03 15:00:40 -0700719 for (;lir != NULL; lir = lir->next) {
720 lir->offset = offset;
721 offset += 4;
722 }
723 return offset;
buzbeee3acd072012-02-25 17:03:10 -0800724}
725
buzbee31a4a6f2012-02-28 15:36:15 -0800726void createMappingTable(CompilationUnit* cUnit)
buzbeee3acd072012-02-25 17:03:10 -0800727{
Bill Buzbeea114add2012-05-03 15:00:40 -0700728 LIR* tgtLIR;
729 int currentDalvikOffset = -1;
buzbeee3acd072012-02-25 17:03:10 -0800730
Bill Buzbeea114add2012-05-03 15:00:40 -0700731 for (tgtLIR = (LIR *) cUnit->firstLIRInsn;
732 tgtLIR;
733 tgtLIR = NEXT_LIR(tgtLIR)) {
734 if ((tgtLIR->opcode >= 0) && !tgtLIR->flags.isNop &&
735 (currentDalvikOffset != tgtLIR->dalvikOffset)) {
736 // Changed - need to emit a record
737 cUnit->mappingTable.push_back(tgtLIR->offset);
738 cUnit->mappingTable.push_back(tgtLIR->dalvikOffset);
739 currentDalvikOffset = tgtLIR->dalvikOffset;
buzbeee3acd072012-02-25 17:03:10 -0800740 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700741 }
buzbeee3acd072012-02-25 17:03:10 -0800742}
743
744/* Determine the offset of each literal field */
buzbee31a4a6f2012-02-28 15:36:15 -0800745int assignLiteralOffset(CompilationUnit* cUnit, int offset)
buzbeee3acd072012-02-25 17:03:10 -0800746{
Bill Buzbeea114add2012-05-03 15:00:40 -0700747 offset = assignLiteralOffsetCommon(cUnit->literalList, offset);
748 offset = assignLiteralOffsetCommon(cUnit->codeLiteralList, offset);
749 offset = assignLiteralOffsetCommon(cUnit->methodLiteralList, offset);
750 return offset;
buzbeee3acd072012-02-25 17:03:10 -0800751}
752
buzbee31a4a6f2012-02-28 15:36:15 -0800753int assignSwitchTablesOffset(CompilationUnit* cUnit, int offset)
buzbeee3acd072012-02-25 17:03:10 -0800754{
Bill Buzbeea114add2012-05-03 15:00:40 -0700755 GrowableListIterator iterator;
756 oatGrowableListIteratorInit(&cUnit->switchTables, &iterator);
757 while (true) {
758 SwitchTable *tabRec = (SwitchTable *) oatGrowableListIteratorNext(
759 &iterator);
760 if (tabRec == NULL) break;
761 tabRec->offset = offset;
762 if (tabRec->table[0] == Instruction::kSparseSwitchSignature) {
763 offset += tabRec->table[1] * (sizeof(int) * 2);
764 } else {
765 DCHECK_EQ(static_cast<int>(tabRec->table[0]),
766 static_cast<int>(Instruction::kPackedSwitchSignature));
767 offset += tabRec->table[1] * sizeof(int);
buzbeee3acd072012-02-25 17:03:10 -0800768 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700769 }
770 return offset;
buzbeee3acd072012-02-25 17:03:10 -0800771}
772
buzbee31a4a6f2012-02-28 15:36:15 -0800773int assignFillArrayDataOffset(CompilationUnit* cUnit, int offset)
buzbeee3acd072012-02-25 17:03:10 -0800774{
Bill Buzbeea114add2012-05-03 15:00:40 -0700775 GrowableListIterator iterator;
776 oatGrowableListIteratorInit(&cUnit->fillArrayData, &iterator);
777 while (true) {
778 FillArrayData *tabRec = (FillArrayData *) oatGrowableListIteratorNext(
779 &iterator);
780 if (tabRec == NULL) break;
781 tabRec->offset = offset;
782 offset += tabRec->size;
783 // word align
784 offset = (offset + 3) & ~3;
785 }
786 return offset;
buzbeee3acd072012-02-25 17:03:10 -0800787}
788
789/*
790 * Walk the compilation unit and assign offsets to instructions
791 * and literals and compute the total size of the compiled unit.
792 */
793void oatAssignOffsets(CompilationUnit* cUnit)
794{
Bill Buzbeea114add2012-05-03 15:00:40 -0700795 int offset = oatAssignInsnOffsets(cUnit);
buzbeee3acd072012-02-25 17:03:10 -0800796
Bill Buzbeea114add2012-05-03 15:00:40 -0700797 /* Const values have to be word aligned */
798 offset = (offset + 3) & ~3;
buzbeee3acd072012-02-25 17:03:10 -0800799
Bill Buzbeea114add2012-05-03 15:00:40 -0700800 /* Set up offsets for literals */
801 cUnit->dataOffset = offset;
buzbeee3acd072012-02-25 17:03:10 -0800802
Bill Buzbeea114add2012-05-03 15:00:40 -0700803 offset = assignLiteralOffset(cUnit, offset);
buzbeee3acd072012-02-25 17:03:10 -0800804
Bill Buzbeea114add2012-05-03 15:00:40 -0700805 offset = assignSwitchTablesOffset(cUnit, offset);
buzbeee3acd072012-02-25 17:03:10 -0800806
Bill Buzbeea114add2012-05-03 15:00:40 -0700807 offset = assignFillArrayDataOffset(cUnit, offset);
buzbeee3acd072012-02-25 17:03:10 -0800808
Bill Buzbeea114add2012-05-03 15:00:40 -0700809 cUnit->totalSize = offset;
buzbeee3acd072012-02-25 17:03:10 -0800810}
811
812/*
813 * Go over each instruction in the list and calculate the offset from the top
814 * before sending them off to the assembler. If out-of-range branch distance is
815 * seen rearrange the instructions a bit to correct it.
816 */
817void oatAssembleLIR(CompilationUnit* cUnit)
818{
Bill Buzbeea114add2012-05-03 15:00:40 -0700819 oatAssignOffsets(cUnit);
820 /*
821 * Assemble here. Note that we generate code with optimistic assumptions
822 * and if found now to work, we'll have to redo the sequence and retry.
823 */
buzbeee3acd072012-02-25 17:03:10 -0800824
Bill Buzbeea114add2012-05-03 15:00:40 -0700825 while (true) {
826 AssemblerStatus res = oatAssembleInstructions(cUnit, 0);
827 if (res == kSuccess) {
828 break;
829 } else {
830 cUnit->assemblerRetries++;
831 if (cUnit->assemblerRetries > MAX_ASSEMBLER_RETRIES) {
832 oatCodegenDump(cUnit);
833 LOG(FATAL) << "Assembler error - too many retries";
834 }
835 // Redo offsets and try again
836 oatAssignOffsets(cUnit);
837 cUnit->codeBuffer.clear();
buzbeee3acd072012-02-25 17:03:10 -0800838 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700839 }
buzbeee3acd072012-02-25 17:03:10 -0800840
Bill Buzbeea114add2012-05-03 15:00:40 -0700841 // Install literals
842 installLiteralPools(cUnit);
buzbeee3acd072012-02-25 17:03:10 -0800843
Bill Buzbeea114add2012-05-03 15:00:40 -0700844 // Install switch tables
845 installSwitchTables(cUnit);
buzbeee3acd072012-02-25 17:03:10 -0800846
Bill Buzbeea114add2012-05-03 15:00:40 -0700847 // Install fill array data
848 installFillArrayData(cUnit);
buzbeee3acd072012-02-25 17:03:10 -0800849
Bill Buzbeea114add2012-05-03 15:00:40 -0700850 /*
851 * Create the mapping table
852 */
853 createMappingTable(cUnit);
buzbeee3acd072012-02-25 17:03:10 -0800854}
855
buzbee31a4a6f2012-02-28 15:36:15 -0800856/*
857 * Insert a kPseudoCaseLabel at the beginning of the Dalvik
858 * offset vaddr. This label will be used to fix up the case
859 * branch table during the assembly phase. Be sure to set
860 * all resource flags on this to prevent code motion across
861 * target boundaries. KeyVal is just there for debugging.
862 */
863LIR* insertCaseLabel(CompilationUnit* cUnit, int vaddr, int keyVal)
864{
Bill Buzbeea114add2012-05-03 15:00:40 -0700865 SafeMap<unsigned int, LIR*>::iterator it;
866 it = cUnit->boundaryMap.find(vaddr);
867 if (it == cUnit->boundaryMap.end()) {
868 LOG(FATAL) << "Error: didn't find vaddr 0x" << std::hex << vaddr;
869 }
870 LIR* newLabel = (LIR*)oatNew(cUnit, sizeof(LIR), true, kAllocLIR);
871 newLabel->dalvikOffset = vaddr;
872 newLabel->opcode = kPseudoCaseLabel;
873 newLabel->operands[0] = keyVal;
874 oatInsertLIRAfter(it->second, (LIR*)newLabel);
875 return newLabel;
buzbee31a4a6f2012-02-28 15:36:15 -0800876}
877
878void markPackedCaseLabels(CompilationUnit* cUnit, SwitchTable *tabRec)
879{
Bill Buzbeea114add2012-05-03 15:00:40 -0700880 const u2* table = tabRec->table;
881 int baseVaddr = tabRec->vaddr;
882 int *targets = (int*)&table[4];
883 int entries = table[1];
884 int lowKey = s4FromSwitchData(&table[2]);
885 for (int i = 0; i < entries; i++) {
886 tabRec->targets[i] = insertCaseLabel(cUnit, baseVaddr + targets[i],
887 i + lowKey);
888 }
buzbee31a4a6f2012-02-28 15:36:15 -0800889}
890
891void markSparseCaseLabels(CompilationUnit* cUnit, SwitchTable *tabRec)
892{
Bill Buzbeea114add2012-05-03 15:00:40 -0700893 const u2* table = tabRec->table;
894 int baseVaddr = tabRec->vaddr;
895 int entries = table[1];
896 int* keys = (int*)&table[2];
897 int* targets = &keys[entries];
898 for (int i = 0; i < entries; i++) {
899 tabRec->targets[i] = insertCaseLabel(cUnit, baseVaddr + targets[i],
900 keys[i]);
901 }
buzbee31a4a6f2012-02-28 15:36:15 -0800902}
903
904void oatProcessSwitchTables(CompilationUnit* cUnit)
905{
Bill Buzbeea114add2012-05-03 15:00:40 -0700906 GrowableListIterator iterator;
907 oatGrowableListIteratorInit(&cUnit->switchTables, &iterator);
908 while (true) {
909 SwitchTable *tabRec =
910 (SwitchTable *) oatGrowableListIteratorNext(&iterator);
911 if (tabRec == NULL) break;
912 if (tabRec->table[0] == Instruction::kPackedSwitchSignature) {
913 markPackedCaseLabels(cUnit, tabRec);
914 } else if (tabRec->table[0] == Instruction::kSparseSwitchSignature) {
915 markSparseCaseLabels(cUnit, tabRec);
916 } else {
917 LOG(FATAL) << "Invalid switch table";
buzbee31a4a6f2012-02-28 15:36:15 -0800918 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700919 }
buzbee31a4a6f2012-02-28 15:36:15 -0800920}
921
922//FIXME: Do we have endian issues here?
923
924void dumpSparseSwitchTable(const u2* table)
Bill Buzbeea114add2012-05-03 15:00:40 -0700925 /*
926 * Sparse switch data format:
927 * ushort ident = 0x0200 magic value
928 * ushort size number of entries in the table; > 0
929 * int keys[size] keys, sorted low-to-high; 32-bit aligned
930 * int targets[size] branch targets, relative to switch opcode
931 *
932 * Total size is (2+size*4) 16-bit code units.
933 */
buzbee31a4a6f2012-02-28 15:36:15 -0800934{
Bill Buzbeea114add2012-05-03 15:00:40 -0700935 u2 ident = table[0];
936 int entries = table[1];
937 int* keys = (int*)&table[2];
938 int* targets = &keys[entries];
939 LOG(INFO) << "Sparse switch table - ident:0x" << std::hex << ident
940 << ", entries: " << std::dec << entries;
941 for (int i = 0; i < entries; i++) {
942 LOG(INFO) << " Key[" << keys[i] << "] -> 0x" << std::hex << targets[i];
943 }
buzbee31a4a6f2012-02-28 15:36:15 -0800944}
945
946void dumpPackedSwitchTable(const u2* table)
Bill Buzbeea114add2012-05-03 15:00:40 -0700947 /*
948 * Packed switch data format:
949 * ushort ident = 0x0100 magic value
950 * ushort size number of entries in the table
951 * int first_key first (and lowest) switch case value
952 * int targets[size] branch targets, relative to switch opcode
953 *
954 * Total size is (4+size*2) 16-bit code units.
955 */
buzbee31a4a6f2012-02-28 15:36:15 -0800956{
Bill Buzbeea114add2012-05-03 15:00:40 -0700957 u2 ident = table[0];
958 int* targets = (int*)&table[4];
959 int entries = table[1];
960 int lowKey = s4FromSwitchData(&table[2]);
961 LOG(INFO) << "Packed switch table - ident:0x" << std::hex << ident
962 << ", entries: " << std::dec << entries << ", lowKey: " << lowKey;
963 for (int i = 0; i < entries; i++) {
964 LOG(INFO) << " Key[" << (i + lowKey) << "] -> 0x" << std::hex
965 << targets[i];
966 }
buzbee31a4a6f2012-02-28 15:36:15 -0800967}
buzbeee3acd072012-02-25 17:03:10 -0800968
969
970} // namespace art