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 | /* |
Ian Rogers | b5d09b2 | 2012-03-06 22:14:17 -0800 | [diff] [blame] | 54 | * Mark load/store instructions that access Dalvik registers through the stack. |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 55 | */ |
Ian Rogers | b5d09b2 | 2012-03-06 22:14:17 -0800 | [diff] [blame] | 56 | void annotateDalvikRegAccess(LIR* lir, int regId, bool isLoad, bool is64bit) |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 57 | { |
| 58 | setMemRefType(lir, isLoad, kDalvikReg); |
| 59 | |
| 60 | /* |
Ian Rogers | b5d09b2 | 2012-03-06 22:14:17 -0800 | [diff] [blame] | 61 | * Store the Dalvik register id in aliasInfo. Mark the MSB if it is a 64-bit |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 62 | * access. |
| 63 | */ |
| 64 | lir->aliasInfo = regId; |
Ian Rogers | b5d09b2 | 2012-03-06 22:14:17 -0800 | [diff] [blame] | 65 | if (is64bit) { |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 66 | lir->aliasInfo |= 0x80000000; |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | /* |
| 71 | * Decode the register id. |
| 72 | */ |
| 73 | inline 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 | */ |
| 93 | inline 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 | */ |
| 101 | void 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 | |
buzbee | e88dfbf | 2012-03-05 11:19:57 -0800 | [diff] [blame] | 117 | /* Get the starting size of the instruction's template */ |
| 118 | lir->flags.size = oatGetInsnSize(lir); |
| 119 | |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 120 | /* 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 | |
buzbee | a7678db | 2012-03-05 15:35:46 -0800 | [diff] [blame] | 147 | #if !defined(TARGET_X86) |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 148 | if (flags & REG_DEF_LR) { |
| 149 | lir->defMask |= ENCODE_REG_LR; |
| 150 | } |
buzbee | a7678db | 2012-03-05 15:35:46 -0800 | [diff] [blame] | 151 | #endif |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 152 | |
| 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 | |
buzbee | 5de3494 | 2012-03-01 14:51:57 -0800 | [diff] [blame] | 161 | #if defined(TARGET_ARM) |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 162 | 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 | } |
buzbee | 5de3494 | 2012-03-01 14:51:57 -0800 | [diff] [blame] | 171 | #endif |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 172 | |
| 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 | |
buzbee | a7678db | 2012-03-05 15:35:46 -0800 | [diff] [blame] | 194 | #if defined(TARGET_ARM) |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 195 | if (flags & REG_USE_PC) { |
| 196 | lir->useMask |= ENCODE_REG_PC; |
| 197 | } |
buzbee | a7678db | 2012-03-05 15:35:46 -0800 | [diff] [blame] | 198 | #endif |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 199 | |
| 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 | |
buzbee | 5de3494 | 2012-03-01 14:51:57 -0800 | [diff] [blame] | 212 | #if defined(TARGET_ARM) |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 213 | 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 | } |
buzbee | 5de3494 | 2012-03-01 14:51:57 -0800 | [diff] [blame] | 222 | #endif |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 223 | |
| 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 | /* |
buzbee | 5de3494 | 2012-03-01 14:51:57 -0800 | [diff] [blame] | 244 | * Debugging macros |
| 245 | */ |
| 246 | #define DUMP_RESOURCE_MASK(X) |
| 247 | #define DUMP_SSA_REP(X) |
| 248 | |
| 249 | /* Pretty-print a LIR instruction */ |
| 250 | void 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]; |
buzbee | 86a4bce | 2012-03-06 18:15:00 -0800 | [diff] [blame] | 255 | const bool dumpNop = (cUnit->enableDebug & (1 << kDebugShowNops)); |
buzbee | 5de3494 | 2012-03-01 14:51:57 -0800 | [diff] [blame] | 256 | |
| 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)); |
buzbee | be00364 | 2012-03-02 15:28:37 -0800 | [diff] [blame] | 313 | LOG(INFO) << StringPrintf("%05x: %-9s%s%s", (unsigned int)(baseAddr + offset), |
buzbee | 5de3494 | 2012-03-01 14:51:57 -0800 | [diff] [blame] | 314 | 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 | |
| 329 | void 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 | |
| 348 | void 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 */ |
| 360 | void 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; |
buzbee | 239c4e7 | 2012-03-16 08:42:29 -0700 | [diff] [blame] | 374 | LOG(INFO) << "CompilerTemps : " << cUnit->numCompilerTemps; |
buzbee | 5de3494 | 2012-03-01 14:51:57 -0800 | [diff] [blame] | 375 | LOG(INFO) << "Frame size : " << cUnit->frameSize; |
buzbee | 5de3494 | 2012-03-01 14:51:57 -0800 | [diff] [blame] | 376 | 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) { |
buzbee | 82488f5 | 2012-03-02 08:20:26 -0800 | [diff] [blame] | 409 | line = StringPrintf(" {0x%05x, 0x%04x},", |
buzbee | 5de3494 | 2012-03-01 14:51:57 -0800 | [diff] [blame] | 410 | cUnit->mappingTable[i], cUnit->mappingTable[i+1]); |
| 411 | LOG(INFO) << line; |
| 412 | } |
| 413 | LOG(INFO) <<" };\n\n"; |
| 414 | } |
| 415 | } |
| 416 | |
buzbee | a2ebdd7 | 2012-03-04 14:57:06 -0800 | [diff] [blame] | 417 | |
| 418 | LIR* rawLIR(CompilationUnit* cUnit, int dalvikOffset, int opcode, int op0, |
Ian Rogers | b5d09b2 | 2012-03-06 22:14:17 -0800 | [diff] [blame] | 419 | int op1, int op2, int op3, int op4, LIR* target) |
buzbee | a2ebdd7 | 2012-03-04 14:57:06 -0800 | [diff] [blame] | 420 | { |
| 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 Rogers | b5d09b2 | 2012-03-06 22:14:17 -0800 | [diff] [blame] | 428 | insn->operands[4] = op4; |
buzbee | a2ebdd7 | 2012-03-04 14:57:06 -0800 | [diff] [blame] | 429 | 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 | |
buzbee | 5de3494 | 2012-03-01 14:51:57 -0800 | [diff] [blame] | 438 | /* |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 439 | * The following are building blocks to construct low-level IRs with 0 - 4 |
| 440 | * operands. |
| 441 | */ |
buzbee | 5de3494 | 2012-03-01 14:51:57 -0800 | [diff] [blame] | 442 | LIR* newLIR0(CompilationUnit* cUnit, int opcode) |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 443 | { |
Ian Rogers | b5d09b2 | 2012-03-06 22:14:17 -0800 | [diff] [blame] | 444 | 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; |
buzbee | a2ebdd7 | 2012-03-04 14:57:06 -0800 | [diff] [blame] | 448 | LIR* insn = rawLIR(cUnit, cUnit->currentDalvikOffset, opcode); |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 449 | oatAppendLIR(cUnit, (LIR*) insn); |
| 450 | return insn; |
| 451 | } |
| 452 | |
buzbee | 5de3494 | 2012-03-01 14:51:57 -0800 | [diff] [blame] | 453 | LIR* newLIR1(CompilationUnit* cUnit, int opcode, |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 454 | int dest) |
| 455 | { |
Ian Rogers | b5d09b2 | 2012-03-06 22:14:17 -0800 | [diff] [blame] | 456 | 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; |
buzbee | a2ebdd7 | 2012-03-04 14:57:06 -0800 | [diff] [blame] | 460 | LIR* insn = rawLIR(cUnit, cUnit->currentDalvikOffset, opcode, dest); |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 461 | oatAppendLIR(cUnit, (LIR*) insn); |
| 462 | return insn; |
| 463 | } |
| 464 | |
buzbee | 5de3494 | 2012-03-01 14:51:57 -0800 | [diff] [blame] | 465 | LIR* newLIR2(CompilationUnit* cUnit, int opcode, |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 466 | int dest, int src1) |
| 467 | { |
Ian Rogers | b5d09b2 | 2012-03-06 22:14:17 -0800 | [diff] [blame] | 468 | 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; |
buzbee | a2ebdd7 | 2012-03-04 14:57:06 -0800 | [diff] [blame] | 472 | LIR* insn = rawLIR(cUnit, cUnit->currentDalvikOffset, opcode, dest, src1); |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 473 | oatAppendLIR(cUnit, (LIR*) insn); |
| 474 | return insn; |
| 475 | } |
| 476 | |
buzbee | 5de3494 | 2012-03-01 14:51:57 -0800 | [diff] [blame] | 477 | LIR* newLIR3(CompilationUnit* cUnit, int opcode, |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 478 | int dest, int src1, int src2) |
| 479 | { |
Ian Rogers | b5d09b2 | 2012-03-06 22:14:17 -0800 | [diff] [blame] | 480 | 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; |
buzbee | a2ebdd7 | 2012-03-04 14:57:06 -0800 | [diff] [blame] | 484 | LIR* insn = rawLIR(cUnit, cUnit->currentDalvikOffset, opcode, dest, src1, |
| 485 | src2); |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 486 | oatAppendLIR(cUnit, (LIR*) insn); |
| 487 | return insn; |
| 488 | } |
| 489 | |
buzbee | 5de3494 | 2012-03-01 14:51:57 -0800 | [diff] [blame] | 490 | LIR* newLIR4(CompilationUnit* cUnit, int opcode, |
Ian Rogers | b5d09b2 | 2012-03-06 22:14:17 -0800 | [diff] [blame] | 491 | int dest, int src1, int src2, int info) |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 492 | { |
Ian Rogers | b5d09b2 | 2012-03-06 22:14:17 -0800 | [diff] [blame] | 493 | 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; |
buzbee | a2ebdd7 | 2012-03-04 14:57:06 -0800 | [diff] [blame] | 497 | LIR* insn = rawLIR(cUnit, cUnit->currentDalvikOffset, opcode, dest, src1, |
| 498 | src2, info); |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 499 | oatAppendLIR(cUnit, (LIR*) insn); |
| 500 | return insn; |
| 501 | } |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 502 | |
Ian Rogers | b5d09b2 | 2012-03-06 22:14:17 -0800 | [diff] [blame] | 503 | LIR* 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 | |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 516 | /* |
| 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 | */ |
| 520 | LIR* 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 */ |
| 532 | LIR* 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 | |
buzbee | 5de3494 | 2012-03-01 14:51:57 -0800 | [diff] [blame] | 555 | /* Add a 32-bit constant either in the constant pool */ |
Ian Rogers | 3fa1379 | 2012-03-18 15:53:45 -0700 | [diff] [blame^] | 556 | LIR* addWordData(CompilationUnit* cUnit, LIR* *constantListP, int value) |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 557 | { |
| 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; |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 566 | } |
| 567 | return NULL; |
| 568 | } |
| 569 | |
| 570 | /* Add a 64-bit constant to the constant pool or mixed with code */ |
| 571 | LIR* addWideData(CompilationUnit* cUnit, LIR* *constantListP, |
| 572 | int valLo, int valHi) |
| 573 | { |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 574 | //FIXME: hard-coded little endian, need BE variant |
buzbee | 5de3494 | 2012-03-01 14:51:57 -0800 | [diff] [blame] | 575 | // Insert high word into list first |
| 576 | addWordData(cUnit, constantListP, valHi); |
| 577 | return addWordData(cUnit, constantListP, valLo); |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 578 | } |
| 579 | |
Ian Rogers | ab058bb | 2012-03-11 22:19:38 -0700 | [diff] [blame] | 580 | void 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); |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 585 | } |
| 586 | |
Ian Rogers | ab058bb | 2012-03-11 22:19:38 -0700 | [diff] [blame] | 587 | void alignBuffer(std::vector<uint8_t>&buf, size_t offset) { |
| 588 | while (buf.size() < offset) { |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 589 | buf.push_back(0); |
Ian Rogers | ab058bb | 2012-03-11 22:19:38 -0700 | [diff] [blame] | 590 | } |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 591 | } |
| 592 | |
| 593 | /* Write the literal pool to the output stream */ |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 594 | void installLiteralPools(CompilationUnit* cUnit) |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 595 | { |
| 596 | alignBuffer(cUnit->codeBuffer, cUnit->dataOffset); |
Ian Rogers | 3fa1379 | 2012-03-18 15:53:45 -0700 | [diff] [blame^] | 597 | LIR* dataLIR = cUnit->literalList; |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 598 | while (dataLIR != NULL) { |
| 599 | pushWord(cUnit->codeBuffer, dataLIR->operands[0]); |
| 600 | dataLIR = NEXT_LIR(dataLIR); |
| 601 | } |
Ian Rogers | 3fa1379 | 2012-03-18 15:53:45 -0700 | [diff] [blame^] | 602 | // 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 | |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 624 | } |
| 625 | |
| 626 | /* Write the switch tables to the output stream */ |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 627 | void installSwitchTables(CompilationUnit* cUnit) |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 628 | { |
| 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); |
buzbee | c5159d5 | 2012-03-03 11:48:39 -0800 | [diff] [blame] | 636 | /* |
| 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 |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 647 | if (cUnit->printMe) { |
| 648 | LOG(INFO) << "Switch table for offset 0x" << std::hex << bxOffset; |
| 649 | } |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 650 | if (tabRec->table[0] == Instruction::kSparseSwitchSignature) { |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 651 | int* keys = (int*)&(tabRec->table[2]); |
| 652 | for (int elems = 0; elems < tabRec->table[1]; elems++) { |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 653 | int disp = tabRec->targets[elems]->offset - bxOffset; |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 654 | 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, |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 661 | tabRec->targets[elems]->offset - bxOffset); |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 662 | } |
| 663 | } else { |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 664 | DCHECK_EQ(static_cast<int>(tabRec->table[0]), static_cast<int>(Instruction::kPackedSwitchSignature)); |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 665 | for (int elems = 0; elems < tabRec->table[1]; elems++) { |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 666 | int disp = tabRec->targets[elems]->offset - bxOffset; |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 667 | if (cUnit->printMe) { |
| 668 | LOG(INFO) << " Case[" << elems << "] disp: 0x" << |
| 669 | std::hex << disp; |
| 670 | } |
| 671 | pushWord(cUnit->codeBuffer, |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 672 | tabRec->targets[elems]->offset - bxOffset); |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 673 | } |
| 674 | } |
| 675 | } |
| 676 | } |
| 677 | |
| 678 | /* Write the fill array dta to the output stream */ |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 679 | void installFillArrayData(CompilationUnit* cUnit) |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 680 | { |
| 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 Rogers | ab058bb | 2012-03-11 22:19:38 -0700 | [diff] [blame] | 688 | 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); |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 691 | } |
| 692 | } |
| 693 | } |
| 694 | |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 695 | int assignLiteralOffsetCommon(LIR* lir, int offset) |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 696 | { |
| 697 | for (;lir != NULL; lir = lir->next) { |
| 698 | lir->offset = offset; |
| 699 | offset += 4; |
| 700 | } |
| 701 | return offset; |
| 702 | } |
| 703 | |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 704 | void createMappingTable(CompilationUnit* cUnit) |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 705 | { |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 706 | LIR* tgtLIR; |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 707 | int currentDalvikOffset = -1; |
| 708 | |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 709 | for (tgtLIR = (LIR *) cUnit->firstLIRInsn; |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 710 | tgtLIR; |
| 711 | tgtLIR = NEXT_LIR(tgtLIR)) { |
| 712 | if ((tgtLIR->opcode >= 0) && !tgtLIR->flags.isNop && |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 713 | (currentDalvikOffset != tgtLIR->dalvikOffset)) { |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 714 | // Changed - need to emit a record |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 715 | cUnit->mappingTable.push_back(tgtLIR->offset); |
| 716 | cUnit->mappingTable.push_back(tgtLIR->dalvikOffset); |
| 717 | currentDalvikOffset = tgtLIR->dalvikOffset; |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 718 | } |
| 719 | } |
| 720 | } |
| 721 | |
| 722 | /* Determine the offset of each literal field */ |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 723 | int assignLiteralOffset(CompilationUnit* cUnit, int offset) |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 724 | { |
| 725 | offset = assignLiteralOffsetCommon(cUnit->literalList, offset); |
Ian Rogers | 3fa1379 | 2012-03-18 15:53:45 -0700 | [diff] [blame^] | 726 | offset = assignLiteralOffsetCommon(cUnit->codeLiteralList, offset); |
| 727 | offset = assignLiteralOffsetCommon(cUnit->methodLiteralList, offset); |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 728 | return offset; |
| 729 | } |
| 730 | |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 731 | int assignSwitchTablesOffset(CompilationUnit* cUnit, int offset) |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 732 | { |
| 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 Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 740 | if (tabRec->table[0] == Instruction::kSparseSwitchSignature) { |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 741 | offset += tabRec->table[1] * (sizeof(int) * 2); |
| 742 | } else { |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 743 | DCHECK_EQ(static_cast<int>(tabRec->table[0]), static_cast<int>(Instruction::kPackedSwitchSignature)); |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 744 | offset += tabRec->table[1] * sizeof(int); |
| 745 | } |
| 746 | } |
| 747 | return offset; |
| 748 | } |
| 749 | |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 750 | int assignFillArrayDataOffset(CompilationUnit* cUnit, int offset) |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 751 | { |
| 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 | */ |
| 770 | void 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 | */ |
| 794 | void 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 | |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 832 | /* |
| 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 | */ |
| 839 | LIR* 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 | |
| 854 | void 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 | |
| 867 | void 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 | |
| 880 | void 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 Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 888 | if (tabRec->table[0] == Instruction::kPackedSwitchSignature) { |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 889 | markPackedCaseLabels(cUnit, tabRec); |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 890 | } else if (tabRec->table[0] == Instruction::kSparseSwitchSignature) { |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 891 | markSparseCaseLabels(cUnit, tabRec); |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 892 | } else { |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 893 | LOG(FATAL) << "Invalid switch table"; |
| 894 | } |
| 895 | } |
| 896 | } |
| 897 | |
| 898 | //FIXME: Do we have endian issues here? |
| 899 | |
| 900 | void 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 | |
| 923 | void 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 | } |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 945 | |
| 946 | |
| 947 | } // namespace art |