buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | namespace art { |
| 18 | |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 19 | void setMemRefType(LIR* lir, bool isLoad, int memType) |
| 20 | { |
| 21 | u8 *maskPtr; |
| 22 | u8 mask = ENCODE_MEM;; |
| 23 | DCHECK(EncodingMap[lir->opcode].flags & (IS_LOAD | IS_STORE)); |
| 24 | if (isLoad) { |
| 25 | maskPtr = &lir->useMask; |
| 26 | } else { |
| 27 | maskPtr = &lir->defMask; |
| 28 | } |
| 29 | /* Clear out the memref flags */ |
| 30 | *maskPtr &= ~mask; |
| 31 | /* ..and then add back the one we need */ |
| 32 | switch(memType) { |
| 33 | case kLiteral: |
| 34 | DCHECK(isLoad); |
| 35 | *maskPtr |= ENCODE_LITERAL; |
| 36 | break; |
| 37 | case kDalvikReg: |
| 38 | *maskPtr |= ENCODE_DALVIK_REG; |
| 39 | break; |
| 40 | case kHeapRef: |
| 41 | *maskPtr |= ENCODE_HEAP_REF; |
| 42 | break; |
| 43 | case kMustNotAlias: |
| 44 | /* Currently only loads can be marked as kMustNotAlias */ |
| 45 | DCHECK(!(EncodingMap[lir->opcode].flags & IS_STORE)); |
| 46 | *maskPtr |= ENCODE_MUST_NOT_ALIAS; |
| 47 | break; |
| 48 | default: |
| 49 | LOG(FATAL) << "Oat: invalid memref kind - " << memType; |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | /* |
| 54 | * Mark load/store instructions that access Dalvik registers through r5FP + |
| 55 | * offset. |
| 56 | */ |
| 57 | void annotateDalvikRegAccess(LIR* lir, int regId, bool isLoad) |
| 58 | { |
| 59 | setMemRefType(lir, isLoad, kDalvikReg); |
| 60 | |
| 61 | /* |
| 62 | * Store the Dalvik register id in aliasInfo. Mark he MSB if it is a 64-bit |
| 63 | * access. |
| 64 | */ |
| 65 | lir->aliasInfo = regId; |
| 66 | if (DOUBLEREG(lir->operands[0])) { |
| 67 | lir->aliasInfo |= 0x80000000; |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | /* |
| 72 | * Decode the register id. |
| 73 | */ |
| 74 | inline u8 getRegMaskCommon(int reg) |
| 75 | { |
| 76 | u8 seed; |
| 77 | int shift; |
| 78 | int regId = reg & 0x1f; |
| 79 | |
| 80 | /* |
| 81 | * Each double register is equal to a pair of single-precision FP registers |
| 82 | */ |
| 83 | seed = DOUBLEREG(reg) ? 3 : 1; |
| 84 | /* FP register starts at bit position 16 */ |
| 85 | shift = FPREG(reg) ? kFPReg0 : 0; |
| 86 | /* Expand the double register id into single offset */ |
| 87 | shift += regId; |
| 88 | return (seed << shift); |
| 89 | } |
| 90 | |
| 91 | /* |
| 92 | * Mark the corresponding bit(s). |
| 93 | */ |
| 94 | inline void setupRegMask(u8* mask, int reg) |
| 95 | { |
| 96 | *mask |= getRegMaskCommon(reg); |
| 97 | } |
| 98 | |
| 99 | /* |
| 100 | * Set up the proper fields in the resource mask |
| 101 | */ |
| 102 | void setupResourceMasks(LIR* lir) |
| 103 | { |
| 104 | int opcode = lir->opcode; |
| 105 | int flags; |
| 106 | |
| 107 | if (opcode <= 0) { |
| 108 | lir->useMask = lir->defMask = 0; |
| 109 | return; |
| 110 | } |
| 111 | |
| 112 | flags = EncodingMap[lir->opcode].flags; |
| 113 | |
| 114 | if (flags & NEEDS_FIXUP) { |
| 115 | lir->flags.pcRelFixup = true; |
| 116 | } |
| 117 | |
buzbee | e88dfbf | 2012-03-05 11:19:57 -0800 | [diff] [blame] | 118 | /* Get the starting size of the instruction's template */ |
| 119 | lir->flags.size = oatGetInsnSize(lir); |
| 120 | |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 121 | /* Set up the mask for resources that are updated */ |
| 122 | if (flags & (IS_LOAD | IS_STORE)) { |
| 123 | /* Default to heap - will catch specialized classes later */ |
| 124 | setMemRefType(lir, flags & IS_LOAD, kHeapRef); |
| 125 | } |
| 126 | |
| 127 | /* |
| 128 | * Conservatively assume the branch here will call out a function that in |
| 129 | * turn will trash everything. |
| 130 | */ |
| 131 | if (flags & IS_BRANCH) { |
| 132 | lir->defMask = lir->useMask = ENCODE_ALL; |
| 133 | return; |
| 134 | } |
| 135 | |
| 136 | if (flags & REG_DEF0) { |
| 137 | setupRegMask(&lir->defMask, lir->operands[0]); |
| 138 | } |
| 139 | |
| 140 | if (flags & REG_DEF1) { |
| 141 | setupRegMask(&lir->defMask, lir->operands[1]); |
| 142 | } |
| 143 | |
| 144 | if (flags & REG_DEF_SP) { |
| 145 | lir->defMask |= ENCODE_REG_SP; |
| 146 | } |
| 147 | |
buzbee | a7678db | 2012-03-05 15:35:46 -0800 | [diff] [blame^] | 148 | #if !defined(TARGET_X86) |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 149 | if (flags & REG_DEF_LR) { |
| 150 | lir->defMask |= ENCODE_REG_LR; |
| 151 | } |
buzbee | a7678db | 2012-03-05 15:35:46 -0800 | [diff] [blame^] | 152 | #endif |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 153 | |
| 154 | if (flags & REG_DEF_LIST0) { |
| 155 | lir->defMask |= ENCODE_REG_LIST(lir->operands[0]); |
| 156 | } |
| 157 | |
| 158 | if (flags & REG_DEF_LIST1) { |
| 159 | lir->defMask |= ENCODE_REG_LIST(lir->operands[1]); |
| 160 | } |
| 161 | |
buzbee | 5de3494 | 2012-03-01 14:51:57 -0800 | [diff] [blame] | 162 | #if defined(TARGET_ARM) |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 163 | if (flags & REG_DEF_FPCS_LIST0) { |
| 164 | lir->defMask |= ENCODE_REG_FPCS_LIST(lir->operands[0]); |
| 165 | } |
| 166 | |
| 167 | if (flags & REG_DEF_FPCS_LIST2) { |
| 168 | for (int i = 0; i < lir->operands[2]; i++) { |
| 169 | setupRegMask(&lir->defMask, lir->operands[1] + i); |
| 170 | } |
| 171 | } |
buzbee | 5de3494 | 2012-03-01 14:51:57 -0800 | [diff] [blame] | 172 | #endif |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 173 | |
| 174 | if (flags & SETS_CCODES) { |
| 175 | lir->defMask |= ENCODE_CCODE; |
| 176 | } |
| 177 | |
| 178 | #if defined(TARGET_ARM) |
| 179 | /* Conservatively treat the IT block */ |
| 180 | if (flags & IS_IT) { |
| 181 | lir->defMask = ENCODE_ALL; |
| 182 | } |
| 183 | #endif |
| 184 | |
| 185 | if (flags & (REG_USE0 | REG_USE1 | REG_USE2 | REG_USE3)) { |
| 186 | int i; |
| 187 | |
| 188 | for (i = 0; i < 4; i++) { |
| 189 | if (flags & (1 << (kRegUse0 + i))) { |
| 190 | setupRegMask(&lir->useMask, lir->operands[i]); |
| 191 | } |
| 192 | } |
| 193 | } |
| 194 | |
buzbee | a7678db | 2012-03-05 15:35:46 -0800 | [diff] [blame^] | 195 | #if defined(TARGET_ARM) |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 196 | if (flags & REG_USE_PC) { |
| 197 | lir->useMask |= ENCODE_REG_PC; |
| 198 | } |
buzbee | a7678db | 2012-03-05 15:35:46 -0800 | [diff] [blame^] | 199 | #endif |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 200 | |
| 201 | if (flags & REG_USE_SP) { |
| 202 | lir->useMask |= ENCODE_REG_SP; |
| 203 | } |
| 204 | |
| 205 | if (flags & REG_USE_LIST0) { |
| 206 | lir->useMask |= ENCODE_REG_LIST(lir->operands[0]); |
| 207 | } |
| 208 | |
| 209 | if (flags & REG_USE_LIST1) { |
| 210 | lir->useMask |= ENCODE_REG_LIST(lir->operands[1]); |
| 211 | } |
| 212 | |
buzbee | 5de3494 | 2012-03-01 14:51:57 -0800 | [diff] [blame] | 213 | #if defined(TARGET_ARM) |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 214 | if (flags & REG_USE_FPCS_LIST0) { |
| 215 | lir->useMask |= ENCODE_REG_FPCS_LIST(lir->operands[0]); |
| 216 | } |
| 217 | |
| 218 | if (flags & REG_USE_FPCS_LIST2) { |
| 219 | for (int i = 0; i < lir->operands[2]; i++) { |
| 220 | setupRegMask(&lir->useMask, lir->operands[1] + i); |
| 221 | } |
| 222 | } |
buzbee | 5de3494 | 2012-03-01 14:51:57 -0800 | [diff] [blame] | 223 | #endif |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 224 | |
| 225 | if (flags & USES_CCODES) { |
| 226 | lir->useMask |= ENCODE_CCODE; |
| 227 | } |
| 228 | |
| 229 | #if defined(TARGET_ARM) |
| 230 | /* Fixup for kThumbPush/lr and kThumbPop/pc */ |
| 231 | if (opcode == kThumbPush || opcode == kThumbPop) { |
| 232 | u8 r8Mask = getRegMaskCommon(r8); |
| 233 | if ((opcode == kThumbPush) && (lir->useMask & r8Mask)) { |
| 234 | lir->useMask &= ~r8Mask; |
| 235 | lir->useMask |= ENCODE_REG_LR; |
| 236 | } else if ((opcode == kThumbPop) && (lir->defMask & r8Mask)) { |
| 237 | lir->defMask &= ~r8Mask; |
| 238 | lir->defMask |= ENCODE_REG_PC; |
| 239 | } |
| 240 | } |
| 241 | #endif |
| 242 | } |
| 243 | |
| 244 | /* |
buzbee | 5de3494 | 2012-03-01 14:51:57 -0800 | [diff] [blame] | 245 | * Debugging macros |
| 246 | */ |
| 247 | #define DUMP_RESOURCE_MASK(X) |
| 248 | #define DUMP_SSA_REP(X) |
| 249 | |
| 250 | /* Pretty-print a LIR instruction */ |
| 251 | void oatDumpLIRInsn(CompilationUnit* cUnit, LIR* arg, unsigned char* baseAddr) |
| 252 | { |
| 253 | LIR* lir = (LIR*) arg; |
| 254 | int offset = lir->offset; |
| 255 | int dest = lir->operands[0]; |
| 256 | const bool dumpNop = false; |
| 257 | |
| 258 | /* Handle pseudo-ops individually, and all regular insns as a group */ |
| 259 | switch(lir->opcode) { |
| 260 | case kPseudoMethodEntry: |
| 261 | LOG(INFO) << "-------- method entry " << |
| 262 | PrettyMethod(cUnit->method_idx, *cUnit->dex_file); |
| 263 | break; |
| 264 | case kPseudoMethodExit: |
| 265 | LOG(INFO) << "-------- Method_Exit"; |
| 266 | break; |
| 267 | case kPseudoBarrier: |
| 268 | LOG(INFO) << "-------- BARRIER"; |
| 269 | break; |
| 270 | case kPseudoExtended: |
| 271 | LOG(INFO) << "-------- " << (char* ) dest; |
| 272 | break; |
| 273 | case kPseudoSSARep: |
| 274 | DUMP_SSA_REP(LOG(INFO) << "-------- kMirOpPhi: " << (char* ) dest); |
| 275 | break; |
| 276 | case kPseudoEntryBlock: |
| 277 | LOG(INFO) << "-------- entry offset: 0x" << std::hex << dest; |
| 278 | break; |
| 279 | case kPseudoDalvikByteCodeBoundary: |
| 280 | LOG(INFO) << "-------- dalvik offset: 0x" << std::hex << |
| 281 | lir->dalvikOffset << " @ " << (char* )lir->operands[0]; |
| 282 | break; |
| 283 | case kPseudoExitBlock: |
| 284 | LOG(INFO) << "-------- exit offset: 0x" << std::hex << dest; |
| 285 | break; |
| 286 | case kPseudoPseudoAlign4: |
| 287 | LOG(INFO) << (intptr_t)baseAddr + offset << " (0x" << std::hex << |
| 288 | offset << "): .align4"; |
| 289 | break; |
| 290 | case kPseudoEHBlockLabel: |
| 291 | LOG(INFO) << "Exception_Handling:"; |
| 292 | break; |
| 293 | case kPseudoTargetLabel: |
| 294 | case kPseudoNormalBlockLabel: |
| 295 | LOG(INFO) << "L" << (intptr_t)lir << ":"; |
| 296 | break; |
| 297 | case kPseudoThrowTarget: |
| 298 | LOG(INFO) << "LT" << (intptr_t)lir << ":"; |
| 299 | break; |
| 300 | case kPseudoSuspendTarget: |
| 301 | LOG(INFO) << "LS" << (intptr_t)lir << ":"; |
| 302 | break; |
| 303 | case kPseudoCaseLabel: |
| 304 | LOG(INFO) << "LC" << (intptr_t)lir << ": Case target 0x" << |
| 305 | std::hex << lir->operands[0] << "|" << std::dec << |
| 306 | lir->operands[0]; |
| 307 | break; |
| 308 | default: |
| 309 | if (lir->flags.isNop && !dumpNop) { |
| 310 | break; |
| 311 | } else { |
| 312 | std::string op_name(buildInsnString(EncodingMap[lir->opcode].name, lir, baseAddr)); |
| 313 | std::string op_operands(buildInsnString(EncodingMap[lir->opcode].fmt, lir, baseAddr)); |
buzbee | be00364 | 2012-03-02 15:28:37 -0800 | [diff] [blame] | 314 | LOG(INFO) << StringPrintf("%05x: %-9s%s%s", (unsigned int)(baseAddr + offset), |
buzbee | 5de3494 | 2012-03-01 14:51:57 -0800 | [diff] [blame] | 315 | op_name.c_str(), op_operands.c_str(), lir->flags.isNop ? "(nop)" : ""); |
| 316 | } |
| 317 | break; |
| 318 | } |
| 319 | |
| 320 | if (lir->useMask && (!lir->flags.isNop || dumpNop)) { |
| 321 | DUMP_RESOURCE_MASK(oatDumpResourceMask((LIR* ) lir, |
| 322 | lir->useMask, "use")); |
| 323 | } |
| 324 | if (lir->defMask && (!lir->flags.isNop || dumpNop)) { |
| 325 | DUMP_RESOURCE_MASK(oatDumpResourceMask((LIR* ) lir, |
| 326 | lir->defMask, "def")); |
| 327 | } |
| 328 | } |
| 329 | |
| 330 | void oatDumpPromotionMap(CompilationUnit *cUnit) |
| 331 | { |
| 332 | for (int i = 0; i < cUnit->numDalvikRegisters; i++) { |
| 333 | PromotionMap vRegMap = cUnit->promotionMap[i]; |
| 334 | char buf[100]; |
| 335 | if (vRegMap.fpLocation == kLocPhysReg) { |
| 336 | snprintf(buf, 100, " : s%d", vRegMap.fpReg & FP_REG_MASK); |
| 337 | } else { |
| 338 | buf[0] = 0; |
| 339 | } |
| 340 | char buf2[100]; |
| 341 | snprintf(buf2, 100, "V[%02d] -> %s%d%s", i, |
| 342 | vRegMap.coreLocation == kLocPhysReg ? |
| 343 | "r" : "SP+", vRegMap.coreLocation == kLocPhysReg ? |
| 344 | vRegMap.coreReg : oatSRegOffset(cUnit, i), buf); |
| 345 | LOG(INFO) << buf2; |
| 346 | } |
| 347 | } |
| 348 | |
| 349 | void oatDumpFullPromotionMap(CompilationUnit *cUnit) |
| 350 | { |
| 351 | for (int i = 0; i < cUnit->numDalvikRegisters; i++) { |
| 352 | PromotionMap vRegMap = cUnit->promotionMap[i]; |
| 353 | LOG(INFO) << i << " -> " << "CL:" << (int)vRegMap.coreLocation << |
| 354 | ", CR:" << (int)vRegMap.coreReg << ", FL:" << |
| 355 | (int)vRegMap.fpLocation << ", FR:" << (int)vRegMap.fpReg << |
| 356 | ", - " << (int)vRegMap.firstInPair; |
| 357 | } |
| 358 | } |
| 359 | |
| 360 | /* Dump instructions and constant pool contents */ |
| 361 | void oatCodegenDump(CompilationUnit* cUnit) |
| 362 | { |
| 363 | LOG(INFO) << "/*"; |
| 364 | 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; |
| 369 | |
| 370 | 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) << "Padding : " << cUnit->numPadding; |
| 376 | LOG(INFO) << "Frame size : " << cUnit->frameSize; |
| 377 | LOG(INFO) << "Start of ins : " << cUnit->insOffset; |
| 378 | LOG(INFO) << "Start of regs : " << cUnit->regsOffset; |
| 379 | LOG(INFO) << "code size is " << cUnit->totalSize << |
| 380 | " bytes, Dalvik size is " << insnsSize * 2; |
| 381 | LOG(INFO) << "expansion factor: " << |
| 382 | (float)cUnit->totalSize / (float)(insnsSize * 2); |
| 383 | oatDumpPromotionMap(cUnit); |
| 384 | for (lirInsn = cUnit->firstLIRInsn; lirInsn; lirInsn = lirInsn->next) { |
| 385 | oatDumpLIRInsn(cUnit, lirInsn, 0); |
| 386 | } |
| 387 | for (lirInsn = cUnit->classPointerList; lirInsn; lirInsn = lirInsn->next) { |
| 388 | thisLIR = (LIR*) lirInsn; |
| 389 | LOG(INFO) << StringPrintf("%x (%04x): .class (%s)", |
| 390 | thisLIR->offset, thisLIR->offset, |
| 391 | ((CallsiteInfo *) thisLIR->operands[0])->classDescriptor); |
| 392 | } |
| 393 | for (lirInsn = cUnit->literalList; lirInsn; lirInsn = lirInsn->next) { |
| 394 | thisLIR = (LIR*) lirInsn; |
| 395 | LOG(INFO) << StringPrintf("%x (%04x): .word (%#x)", |
| 396 | thisLIR->offset, thisLIR->offset, thisLIR->operands[0]); |
| 397 | } |
| 398 | |
| 399 | 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)); |
| 404 | |
| 405 | // Dump mapping table |
| 406 | if (cUnit->mappingTable.size() > 0) { |
| 407 | std::string line(StringPrintf("\n MappingTable %s%s_%s_mappingTable[%zu] = {", |
| 408 | descriptor.c_str(), name.c_str(), signature.c_str(), cUnit->mappingTable.size())); |
| 409 | std::replace(line.begin(), line.end(), ';', '_'); |
| 410 | LOG(INFO) << line; |
| 411 | for (uint32_t i = 0; i < cUnit->mappingTable.size(); i+=2) { |
buzbee | 82488f5 | 2012-03-02 08:20:26 -0800 | [diff] [blame] | 412 | line = StringPrintf(" {0x%05x, 0x%04x},", |
buzbee | 5de3494 | 2012-03-01 14:51:57 -0800 | [diff] [blame] | 413 | cUnit->mappingTable[i], cUnit->mappingTable[i+1]); |
| 414 | LOG(INFO) << line; |
| 415 | } |
| 416 | LOG(INFO) <<" };\n\n"; |
| 417 | } |
| 418 | } |
| 419 | |
buzbee | a2ebdd7 | 2012-03-04 14:57:06 -0800 | [diff] [blame] | 420 | |
| 421 | LIR* rawLIR(CompilationUnit* cUnit, int dalvikOffset, int opcode, int op0, |
| 422 | int op1, int op2, int op3, LIR* target) |
| 423 | { |
| 424 | LIR* insn = (LIR* ) oatNew(cUnit, sizeof(LIR), true, kAllocLIR); |
| 425 | insn->dalvikOffset = dalvikOffset; |
| 426 | insn->opcode = opcode; |
| 427 | insn->operands[0] = op0; |
| 428 | insn->operands[1] = op1; |
| 429 | insn->operands[2] = op2; |
| 430 | insn->operands[3] = op3; |
| 431 | insn->target = target; |
| 432 | oatSetupResourceMasks(insn); |
| 433 | if (opcode == kPseudoTargetLabel) { |
| 434 | // Always make labels scheduling barriers |
| 435 | insn->defMask = ENCODE_ALL; |
| 436 | } |
| 437 | return insn; |
| 438 | } |
| 439 | |
buzbee | 5de3494 | 2012-03-01 14:51:57 -0800 | [diff] [blame] | 440 | /* |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 441 | * The following are building blocks to construct low-level IRs with 0 - 4 |
| 442 | * operands. |
| 443 | */ |
buzbee | 5de3494 | 2012-03-01 14:51:57 -0800 | [diff] [blame] | 444 | LIR* newLIR0(CompilationUnit* cUnit, int opcode) |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 445 | { |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 446 | DCHECK(isPseudoOpcode(opcode) || (EncodingMap[opcode].flags & NO_OPERAND)); |
buzbee | a2ebdd7 | 2012-03-04 14:57:06 -0800 | [diff] [blame] | 447 | LIR* insn = rawLIR(cUnit, cUnit->currentDalvikOffset, opcode); |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 448 | oatAppendLIR(cUnit, (LIR*) insn); |
| 449 | return insn; |
| 450 | } |
| 451 | |
buzbee | 5de3494 | 2012-03-01 14:51:57 -0800 | [diff] [blame] | 452 | LIR* newLIR1(CompilationUnit* cUnit, int opcode, |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 453 | int dest) |
| 454 | { |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 455 | DCHECK(isPseudoOpcode(opcode) || (EncodingMap[opcode].flags & IS_UNARY_OP)); |
buzbee | a2ebdd7 | 2012-03-04 14:57:06 -0800 | [diff] [blame] | 456 | LIR* insn = rawLIR(cUnit, cUnit->currentDalvikOffset, opcode, dest); |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 457 | oatAppendLIR(cUnit, (LIR*) insn); |
| 458 | return insn; |
| 459 | } |
| 460 | |
buzbee | 5de3494 | 2012-03-01 14:51:57 -0800 | [diff] [blame] | 461 | LIR* newLIR2(CompilationUnit* cUnit, int opcode, |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 462 | int dest, int src1) |
| 463 | { |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 464 | DCHECK(isPseudoOpcode(opcode) || |
| 465 | (EncodingMap[opcode].flags & IS_BINARY_OP)); |
buzbee | a2ebdd7 | 2012-03-04 14:57:06 -0800 | [diff] [blame] | 466 | LIR* insn = rawLIR(cUnit, cUnit->currentDalvikOffset, opcode, dest, src1); |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 467 | oatAppendLIR(cUnit, (LIR*) insn); |
| 468 | return insn; |
| 469 | } |
| 470 | |
buzbee | 5de3494 | 2012-03-01 14:51:57 -0800 | [diff] [blame] | 471 | LIR* newLIR3(CompilationUnit* cUnit, int opcode, |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 472 | int dest, int src1, int src2) |
| 473 | { |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 474 | DCHECK(isPseudoOpcode(opcode) || |
| 475 | (EncodingMap[opcode].flags & IS_TERTIARY_OP)) |
| 476 | << (int)opcode << " " |
| 477 | << PrettyMethod(cUnit->method_idx, *cUnit->dex_file) << " " |
| 478 | << cUnit->currentDalvikOffset; |
buzbee | a2ebdd7 | 2012-03-04 14:57:06 -0800 | [diff] [blame] | 479 | LIR* insn = rawLIR(cUnit, cUnit->currentDalvikOffset, opcode, dest, src1, |
| 480 | src2); |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 481 | oatAppendLIR(cUnit, (LIR*) insn); |
| 482 | return insn; |
| 483 | } |
| 484 | |
buzbee | 5de3494 | 2012-03-01 14:51:57 -0800 | [diff] [blame] | 485 | LIR* newLIR4(CompilationUnit* cUnit, int opcode, |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 486 | int dest, int src1, int src2, int info) |
| 487 | { |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 488 | DCHECK(isPseudoOpcode(opcode) || |
| 489 | (EncodingMap[opcode].flags & IS_QUAD_OP)); |
buzbee | a2ebdd7 | 2012-03-04 14:57:06 -0800 | [diff] [blame] | 490 | LIR* insn = rawLIR(cUnit, cUnit->currentDalvikOffset, opcode, dest, src1, |
| 491 | src2, info); |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 492 | oatAppendLIR(cUnit, (LIR*) insn); |
| 493 | return insn; |
| 494 | } |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 495 | |
| 496 | /* |
| 497 | * Search the existing constants in the literal pool for an exact or close match |
| 498 | * within specified delta (greater or equal to 0). |
| 499 | */ |
| 500 | LIR* scanLiteralPool(LIR* dataTarget, int value, unsigned int delta) |
| 501 | { |
| 502 | while (dataTarget) { |
| 503 | if (((unsigned) (value - ((LIR* ) dataTarget)->operands[0])) <= |
| 504 | delta) |
| 505 | return (LIR* ) dataTarget; |
| 506 | dataTarget = dataTarget->next; |
| 507 | } |
| 508 | return NULL; |
| 509 | } |
| 510 | |
| 511 | /* Search the existing constants in the literal pool for an exact wide match */ |
| 512 | LIR* scanLiteralPoolWide(LIR* dataTarget, int valLo, int valHi) |
| 513 | { |
| 514 | bool loMatch = false; |
| 515 | LIR* loTarget = NULL; |
| 516 | while (dataTarget) { |
| 517 | if (loMatch && (((LIR*)dataTarget)->operands[0] == valHi)) { |
| 518 | return (LIR*)loTarget; |
| 519 | } |
| 520 | loMatch = false; |
| 521 | if (((LIR*)dataTarget)->operands[0] == valLo) { |
| 522 | loMatch = true; |
| 523 | loTarget = dataTarget; |
| 524 | } |
| 525 | dataTarget = dataTarget->next; |
| 526 | } |
| 527 | return NULL; |
| 528 | } |
| 529 | |
| 530 | /* |
| 531 | * The following are building blocks to insert constants into the pool or |
| 532 | * instruction streams. |
| 533 | */ |
| 534 | |
buzbee | 5de3494 | 2012-03-01 14:51:57 -0800 | [diff] [blame] | 535 | /* Add a 32-bit constant either in the constant pool */ |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 536 | LIR* addWordData(CompilationUnit* cUnit, LIR* *constantListP, |
| 537 | int value) |
| 538 | { |
| 539 | /* Add the constant to the literal pool */ |
| 540 | if (constantListP) { |
| 541 | LIR* newValue = (LIR* ) oatNew(cUnit, sizeof(LIR), true, |
| 542 | kAllocData); |
| 543 | newValue->operands[0] = value; |
| 544 | newValue->next = *constantListP; |
| 545 | *constantListP = (LIR*) newValue; |
| 546 | return newValue; |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 547 | } |
| 548 | return NULL; |
| 549 | } |
| 550 | |
| 551 | /* Add a 64-bit constant to the constant pool or mixed with code */ |
| 552 | LIR* addWideData(CompilationUnit* cUnit, LIR* *constantListP, |
| 553 | int valLo, int valHi) |
| 554 | { |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 555 | //FIXME: hard-coded little endian, need BE variant |
buzbee | 5de3494 | 2012-03-01 14:51:57 -0800 | [diff] [blame] | 556 | // Insert high word into list first |
| 557 | addWordData(cUnit, constantListP, valHi); |
| 558 | return addWordData(cUnit, constantListP, valLo); |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 559 | } |
| 560 | |
| 561 | void pushWord(std::vector<uint16_t>&buf, int data) { |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 562 | buf.push_back( data & 0xffff); |
| 563 | buf.push_back( (data >> 16) & 0xffff); |
| 564 | } |
| 565 | |
| 566 | void alignBuffer(std::vector<uint16_t>&buf, size_t offset) { |
| 567 | while (buf.size() < (offset/2)) |
| 568 | buf.push_back(0); |
| 569 | } |
| 570 | |
| 571 | /* Write the literal pool to the output stream */ |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 572 | void installLiteralPools(CompilationUnit* cUnit) |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 573 | { |
| 574 | alignBuffer(cUnit->codeBuffer, cUnit->dataOffset); |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 575 | LIR* dataLIR = (LIR*) cUnit->literalList; |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 576 | while (dataLIR != NULL) { |
| 577 | pushWord(cUnit->codeBuffer, dataLIR->operands[0]); |
| 578 | dataLIR = NEXT_LIR(dataLIR); |
| 579 | } |
| 580 | } |
| 581 | |
| 582 | /* Write the switch tables to the output stream */ |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 583 | void installSwitchTables(CompilationUnit* cUnit) |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 584 | { |
| 585 | GrowableListIterator iterator; |
| 586 | oatGrowableListIteratorInit(&cUnit->switchTables, &iterator); |
| 587 | while (true) { |
| 588 | SwitchTable* tabRec = (SwitchTable *) oatGrowableListIteratorNext( |
| 589 | &iterator); |
| 590 | if (tabRec == NULL) break; |
| 591 | alignBuffer(cUnit->codeBuffer, tabRec->offset); |
buzbee | c5159d5 | 2012-03-03 11:48:39 -0800 | [diff] [blame] | 592 | /* |
| 593 | * For Arm, our reference point is the address of the bx |
| 594 | * instruction that does the launch, so we have to subtract |
| 595 | * the auto pc-advance. For other targets the reference point |
| 596 | * is a label, so we can use the offset as-is. |
| 597 | */ |
| 598 | #if defined(TARGET_ARM) |
| 599 | int bxOffset = tabRec->anchor->offset + 4; |
| 600 | #else |
| 601 | int bxOffset = tabRec->anchor->offset; |
| 602 | #endif |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 603 | if (cUnit->printMe) { |
| 604 | LOG(INFO) << "Switch table for offset 0x" << std::hex << bxOffset; |
| 605 | } |
| 606 | if (tabRec->table[0] == kSparseSwitchSignature) { |
| 607 | int* keys = (int*)&(tabRec->table[2]); |
| 608 | for (int elems = 0; elems < tabRec->table[1]; elems++) { |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 609 | int disp = tabRec->targets[elems]->offset - bxOffset; |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 610 | if (cUnit->printMe) { |
| 611 | LOG(INFO) << " Case[" << elems << "] key: 0x" << |
| 612 | std::hex << keys[elems] << ", disp: 0x" << |
| 613 | std::hex << disp; |
| 614 | } |
| 615 | pushWord(cUnit->codeBuffer, keys[elems]); |
| 616 | pushWord(cUnit->codeBuffer, |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 617 | tabRec->targets[elems]->offset - bxOffset); |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 618 | } |
| 619 | } else { |
| 620 | DCHECK_EQ(tabRec->table[0], kPackedSwitchSignature); |
| 621 | for (int elems = 0; elems < tabRec->table[1]; elems++) { |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 622 | int disp = tabRec->targets[elems]->offset - bxOffset; |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 623 | if (cUnit->printMe) { |
| 624 | LOG(INFO) << " Case[" << elems << "] disp: 0x" << |
| 625 | std::hex << disp; |
| 626 | } |
| 627 | pushWord(cUnit->codeBuffer, |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 628 | tabRec->targets[elems]->offset - bxOffset); |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 629 | } |
| 630 | } |
| 631 | } |
| 632 | } |
| 633 | |
| 634 | /* Write the fill array dta to the output stream */ |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 635 | void installFillArrayData(CompilationUnit* cUnit) |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 636 | { |
| 637 | GrowableListIterator iterator; |
| 638 | oatGrowableListIteratorInit(&cUnit->fillArrayData, &iterator); |
| 639 | while (true) { |
| 640 | FillArrayData *tabRec = (FillArrayData *) oatGrowableListIteratorNext( |
| 641 | &iterator); |
| 642 | if (tabRec == NULL) break; |
| 643 | alignBuffer(cUnit->codeBuffer, tabRec->offset); |
| 644 | for (int i = 0; i < ((tabRec->size + 1) / 2) ; i++) { |
| 645 | cUnit->codeBuffer.push_back( tabRec->table[i]); |
| 646 | } |
| 647 | } |
| 648 | } |
| 649 | |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 650 | int assignLiteralOffsetCommon(LIR* lir, int offset) |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 651 | { |
| 652 | for (;lir != NULL; lir = lir->next) { |
| 653 | lir->offset = offset; |
| 654 | offset += 4; |
| 655 | } |
| 656 | return offset; |
| 657 | } |
| 658 | |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 659 | void createMappingTable(CompilationUnit* cUnit) |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 660 | { |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 661 | LIR* tgtLIR; |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 662 | int currentDalvikOffset = -1; |
| 663 | |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 664 | for (tgtLIR = (LIR *) cUnit->firstLIRInsn; |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 665 | tgtLIR; |
| 666 | tgtLIR = NEXT_LIR(tgtLIR)) { |
| 667 | if ((tgtLIR->opcode >= 0) && !tgtLIR->flags.isNop && |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 668 | (currentDalvikOffset != tgtLIR->dalvikOffset)) { |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 669 | // Changed - need to emit a record |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 670 | cUnit->mappingTable.push_back(tgtLIR->offset); |
| 671 | cUnit->mappingTable.push_back(tgtLIR->dalvikOffset); |
| 672 | currentDalvikOffset = tgtLIR->dalvikOffset; |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 673 | } |
| 674 | } |
| 675 | } |
| 676 | |
| 677 | /* Determine the offset of each literal field */ |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 678 | int assignLiteralOffset(CompilationUnit* cUnit, int offset) |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 679 | { |
| 680 | offset = assignLiteralOffsetCommon(cUnit->literalList, offset); |
| 681 | return offset; |
| 682 | } |
| 683 | |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 684 | int assignSwitchTablesOffset(CompilationUnit* cUnit, int offset) |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 685 | { |
| 686 | GrowableListIterator iterator; |
| 687 | oatGrowableListIteratorInit(&cUnit->switchTables, &iterator); |
| 688 | while (true) { |
| 689 | SwitchTable *tabRec = (SwitchTable *) oatGrowableListIteratorNext( |
| 690 | &iterator); |
| 691 | if (tabRec == NULL) break; |
| 692 | tabRec->offset = offset; |
| 693 | if (tabRec->table[0] == kSparseSwitchSignature) { |
| 694 | offset += tabRec->table[1] * (sizeof(int) * 2); |
| 695 | } else { |
| 696 | DCHECK_EQ(tabRec->table[0], kPackedSwitchSignature); |
| 697 | offset += tabRec->table[1] * sizeof(int); |
| 698 | } |
| 699 | } |
| 700 | return offset; |
| 701 | } |
| 702 | |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 703 | int assignFillArrayDataOffset(CompilationUnit* cUnit, int offset) |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 704 | { |
| 705 | GrowableListIterator iterator; |
| 706 | oatGrowableListIteratorInit(&cUnit->fillArrayData, &iterator); |
| 707 | while (true) { |
| 708 | FillArrayData *tabRec = (FillArrayData *) oatGrowableListIteratorNext( |
| 709 | &iterator); |
| 710 | if (tabRec == NULL) break; |
| 711 | tabRec->offset = offset; |
| 712 | offset += tabRec->size; |
| 713 | // word align |
| 714 | offset = (offset + 3) & ~3; |
| 715 | } |
| 716 | return offset; |
| 717 | } |
| 718 | |
| 719 | /* |
| 720 | * Walk the compilation unit and assign offsets to instructions |
| 721 | * and literals and compute the total size of the compiled unit. |
| 722 | */ |
| 723 | void oatAssignOffsets(CompilationUnit* cUnit) |
| 724 | { |
| 725 | int offset = oatAssignInsnOffsets(cUnit); |
| 726 | |
| 727 | /* Const values have to be word aligned */ |
| 728 | offset = (offset + 3) & ~3; |
| 729 | |
| 730 | /* Set up offsets for literals */ |
| 731 | cUnit->dataOffset = offset; |
| 732 | |
| 733 | offset = assignLiteralOffset(cUnit, offset); |
| 734 | |
| 735 | offset = assignSwitchTablesOffset(cUnit, offset); |
| 736 | |
| 737 | offset = assignFillArrayDataOffset(cUnit, offset); |
| 738 | |
| 739 | cUnit->totalSize = offset; |
| 740 | } |
| 741 | |
| 742 | /* |
| 743 | * Go over each instruction in the list and calculate the offset from the top |
| 744 | * before sending them off to the assembler. If out-of-range branch distance is |
| 745 | * seen rearrange the instructions a bit to correct it. |
| 746 | */ |
| 747 | void oatAssembleLIR(CompilationUnit* cUnit) |
| 748 | { |
| 749 | oatAssignOffsets(cUnit); |
| 750 | /* |
| 751 | * Assemble here. Note that we generate code with optimistic assumptions |
| 752 | * and if found now to work, we'll have to redo the sequence and retry. |
| 753 | */ |
| 754 | |
| 755 | while (true) { |
| 756 | AssemblerStatus res = oatAssembleInstructions(cUnit, 0); |
| 757 | if (res == kSuccess) { |
| 758 | break; |
| 759 | } else { |
| 760 | cUnit->assemblerRetries++; |
| 761 | if (cUnit->assemblerRetries > MAX_ASSEMBLER_RETRIES) { |
| 762 | LOG(FATAL) << "Assembler error - too many retries"; |
| 763 | } |
| 764 | // Redo offsets and try again |
| 765 | oatAssignOffsets(cUnit); |
| 766 | cUnit->codeBuffer.clear(); |
| 767 | } |
| 768 | } |
| 769 | |
| 770 | // Install literals |
| 771 | installLiteralPools(cUnit); |
| 772 | |
| 773 | // Install switch tables |
| 774 | installSwitchTables(cUnit); |
| 775 | |
| 776 | // Install fill array data |
| 777 | installFillArrayData(cUnit); |
| 778 | |
| 779 | /* |
| 780 | * Create the mapping table |
| 781 | */ |
| 782 | createMappingTable(cUnit); |
| 783 | } |
| 784 | |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 785 | /* |
| 786 | * Insert a kPseudoCaseLabel at the beginning of the Dalvik |
| 787 | * offset vaddr. This label will be used to fix up the case |
| 788 | * branch table during the assembly phase. Be sure to set |
| 789 | * all resource flags on this to prevent code motion across |
| 790 | * target boundaries. KeyVal is just there for debugging. |
| 791 | */ |
| 792 | LIR* insertCaseLabel(CompilationUnit* cUnit, int vaddr, int keyVal) |
| 793 | { |
| 794 | std::map<unsigned int, LIR*>::iterator it; |
| 795 | it = cUnit->boundaryMap.find(vaddr); |
| 796 | if (it == cUnit->boundaryMap.end()) { |
| 797 | LOG(FATAL) << "Error: didn't find vaddr 0x" << std::hex << vaddr; |
| 798 | } |
| 799 | LIR* newLabel = (LIR*)oatNew(cUnit, sizeof(LIR), true, kAllocLIR); |
| 800 | newLabel->dalvikOffset = vaddr; |
| 801 | newLabel->opcode = kPseudoCaseLabel; |
| 802 | newLabel->operands[0] = keyVal; |
| 803 | oatInsertLIRAfter(it->second, (LIR*)newLabel); |
| 804 | return newLabel; |
| 805 | } |
| 806 | |
| 807 | void markPackedCaseLabels(CompilationUnit* cUnit, SwitchTable *tabRec) |
| 808 | { |
| 809 | const u2* table = tabRec->table; |
| 810 | int baseVaddr = tabRec->vaddr; |
| 811 | int *targets = (int*)&table[4]; |
| 812 | int entries = table[1]; |
| 813 | int lowKey = s4FromSwitchData(&table[2]); |
| 814 | for (int i = 0; i < entries; i++) { |
| 815 | tabRec->targets[i] = insertCaseLabel(cUnit, baseVaddr + targets[i], |
| 816 | i + lowKey); |
| 817 | } |
| 818 | } |
| 819 | |
| 820 | void markSparseCaseLabels(CompilationUnit* cUnit, SwitchTable *tabRec) |
| 821 | { |
| 822 | const u2* table = tabRec->table; |
| 823 | int baseVaddr = tabRec->vaddr; |
| 824 | int entries = table[1]; |
| 825 | int* keys = (int*)&table[2]; |
| 826 | int* targets = &keys[entries]; |
| 827 | for (int i = 0; i < entries; i++) { |
| 828 | tabRec->targets[i] = insertCaseLabel(cUnit, baseVaddr + targets[i], |
| 829 | keys[i]); |
| 830 | } |
| 831 | } |
| 832 | |
| 833 | void oatProcessSwitchTables(CompilationUnit* cUnit) |
| 834 | { |
| 835 | GrowableListIterator iterator; |
| 836 | oatGrowableListIteratorInit(&cUnit->switchTables, &iterator); |
| 837 | while (true) { |
| 838 | SwitchTable *tabRec = (SwitchTable *) oatGrowableListIteratorNext( |
| 839 | &iterator); |
| 840 | if (tabRec == NULL) break; |
| 841 | if (tabRec->table[0] == kPackedSwitchSignature) |
| 842 | markPackedCaseLabels(cUnit, tabRec); |
| 843 | else if (tabRec->table[0] == kSparseSwitchSignature) |
| 844 | markSparseCaseLabels(cUnit, tabRec); |
| 845 | else { |
| 846 | LOG(FATAL) << "Invalid switch table"; |
| 847 | } |
| 848 | } |
| 849 | } |
| 850 | |
| 851 | //FIXME: Do we have endian issues here? |
| 852 | |
| 853 | void dumpSparseSwitchTable(const u2* table) |
| 854 | /* |
| 855 | * Sparse switch data format: |
| 856 | * ushort ident = 0x0200 magic value |
| 857 | * ushort size number of entries in the table; > 0 |
| 858 | * int keys[size] keys, sorted low-to-high; 32-bit aligned |
| 859 | * int targets[size] branch targets, relative to switch opcode |
| 860 | * |
| 861 | * Total size is (2+size*4) 16-bit code units. |
| 862 | */ |
| 863 | { |
| 864 | u2 ident = table[0]; |
| 865 | int entries = table[1]; |
| 866 | int* keys = (int*)&table[2]; |
| 867 | int* targets = &keys[entries]; |
| 868 | LOG(INFO) << "Sparse switch table - ident:0x" << std::hex << ident << |
| 869 | ", entries: " << std::dec << entries; |
| 870 | for (int i = 0; i < entries; i++) { |
| 871 | LOG(INFO) << " Key[" << keys[i] << "] -> 0x" << std::hex << |
| 872 | targets[i]; |
| 873 | } |
| 874 | } |
| 875 | |
| 876 | void dumpPackedSwitchTable(const u2* table) |
| 877 | /* |
| 878 | * Packed switch data format: |
| 879 | * ushort ident = 0x0100 magic value |
| 880 | * ushort size number of entries in the table |
| 881 | * int first_key first (and lowest) switch case value |
| 882 | * int targets[size] branch targets, relative to switch opcode |
| 883 | * |
| 884 | * Total size is (4+size*2) 16-bit code units. |
| 885 | */ |
| 886 | { |
| 887 | u2 ident = table[0]; |
| 888 | int* targets = (int*)&table[4]; |
| 889 | int entries = table[1]; |
| 890 | int lowKey = s4FromSwitchData(&table[2]); |
| 891 | LOG(INFO) << "Packed switch table - ident:0x" << std::hex << ident << |
| 892 | ", entries: " << std::dec << entries << ", lowKey: " << lowKey; |
| 893 | for (int i = 0; i < entries; i++) { |
| 894 | LOG(INFO) << " Key[" << (i + lowKey) << "] -> 0x" << std::hex << |
| 895 | targets[i]; |
| 896 | } |
| 897 | } |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 898 | |
| 899 | |
| 900 | } // namespace art |