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 */ |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 556 | LIR* addWordData(CompilationUnit* cUnit, LIR* *constantListP, |
| 557 | int value) |
| 558 | { |
| 559 | /* Add the constant to the literal pool */ |
| 560 | if (constantListP) { |
| 561 | LIR* newValue = (LIR* ) oatNew(cUnit, sizeof(LIR), true, |
| 562 | kAllocData); |
| 563 | newValue->operands[0] = value; |
| 564 | newValue->next = *constantListP; |
| 565 | *constantListP = (LIR*) newValue; |
| 566 | return newValue; |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 567 | } |
| 568 | return NULL; |
| 569 | } |
| 570 | |
| 571 | /* Add a 64-bit constant to the constant pool or mixed with code */ |
| 572 | LIR* addWideData(CompilationUnit* cUnit, LIR* *constantListP, |
| 573 | int valLo, int valHi) |
| 574 | { |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 575 | //FIXME: hard-coded little endian, need BE variant |
buzbee | 5de3494 | 2012-03-01 14:51:57 -0800 | [diff] [blame] | 576 | // Insert high word into list first |
| 577 | addWordData(cUnit, constantListP, valHi); |
| 578 | return addWordData(cUnit, constantListP, valLo); |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 579 | } |
| 580 | |
Ian Rogers | ab058bb | 2012-03-11 22:19:38 -0700 | [diff] [blame] | 581 | void pushWord(std::vector<uint8_t>&buf, int data) { |
| 582 | buf.push_back( data & 0xff); |
| 583 | buf.push_back( (data >> 8) & 0xff); |
| 584 | buf.push_back( (data >> 16) & 0xff); |
| 585 | buf.push_back( (data >> 24) & 0xff); |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 586 | } |
| 587 | |
Ian Rogers | ab058bb | 2012-03-11 22:19:38 -0700 | [diff] [blame] | 588 | void alignBuffer(std::vector<uint8_t>&buf, size_t offset) { |
| 589 | while (buf.size() < offset) { |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 590 | buf.push_back(0); |
Ian Rogers | ab058bb | 2012-03-11 22:19:38 -0700 | [diff] [blame] | 591 | } |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 592 | } |
| 593 | |
| 594 | /* Write the literal pool to the output stream */ |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 595 | void installLiteralPools(CompilationUnit* cUnit) |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 596 | { |
| 597 | alignBuffer(cUnit->codeBuffer, cUnit->dataOffset); |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 598 | LIR* dataLIR = (LIR*) cUnit->literalList; |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 599 | while (dataLIR != NULL) { |
| 600 | pushWord(cUnit->codeBuffer, dataLIR->operands[0]); |
| 601 | dataLIR = NEXT_LIR(dataLIR); |
| 602 | } |
| 603 | } |
| 604 | |
| 605 | /* Write the switch tables to the output stream */ |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 606 | void installSwitchTables(CompilationUnit* cUnit) |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 607 | { |
| 608 | GrowableListIterator iterator; |
| 609 | oatGrowableListIteratorInit(&cUnit->switchTables, &iterator); |
| 610 | while (true) { |
| 611 | SwitchTable* tabRec = (SwitchTable *) oatGrowableListIteratorNext( |
| 612 | &iterator); |
| 613 | if (tabRec == NULL) break; |
| 614 | alignBuffer(cUnit->codeBuffer, tabRec->offset); |
buzbee | c5159d5 | 2012-03-03 11:48:39 -0800 | [diff] [blame] | 615 | /* |
| 616 | * For Arm, our reference point is the address of the bx |
| 617 | * instruction that does the launch, so we have to subtract |
| 618 | * the auto pc-advance. For other targets the reference point |
| 619 | * is a label, so we can use the offset as-is. |
| 620 | */ |
| 621 | #if defined(TARGET_ARM) |
| 622 | int bxOffset = tabRec->anchor->offset + 4; |
| 623 | #else |
| 624 | int bxOffset = tabRec->anchor->offset; |
| 625 | #endif |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 626 | if (cUnit->printMe) { |
| 627 | LOG(INFO) << "Switch table for offset 0x" << std::hex << bxOffset; |
| 628 | } |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 629 | if (tabRec->table[0] == Instruction::kSparseSwitchSignature) { |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 630 | int* keys = (int*)&(tabRec->table[2]); |
| 631 | for (int elems = 0; elems < tabRec->table[1]; elems++) { |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 632 | int disp = tabRec->targets[elems]->offset - bxOffset; |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 633 | if (cUnit->printMe) { |
| 634 | LOG(INFO) << " Case[" << elems << "] key: 0x" << |
| 635 | std::hex << keys[elems] << ", disp: 0x" << |
| 636 | std::hex << disp; |
| 637 | } |
| 638 | pushWord(cUnit->codeBuffer, keys[elems]); |
| 639 | pushWord(cUnit->codeBuffer, |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 640 | tabRec->targets[elems]->offset - bxOffset); |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 641 | } |
| 642 | } else { |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 643 | DCHECK_EQ(static_cast<int>(tabRec->table[0]), static_cast<int>(Instruction::kPackedSwitchSignature)); |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 644 | for (int elems = 0; elems < tabRec->table[1]; elems++) { |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 645 | int disp = tabRec->targets[elems]->offset - bxOffset; |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 646 | if (cUnit->printMe) { |
| 647 | LOG(INFO) << " Case[" << elems << "] disp: 0x" << |
| 648 | std::hex << disp; |
| 649 | } |
| 650 | pushWord(cUnit->codeBuffer, |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 651 | tabRec->targets[elems]->offset - bxOffset); |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 652 | } |
| 653 | } |
| 654 | } |
| 655 | } |
| 656 | |
| 657 | /* Write the fill array dta to the output stream */ |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 658 | void installFillArrayData(CompilationUnit* cUnit) |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 659 | { |
| 660 | GrowableListIterator iterator; |
| 661 | oatGrowableListIteratorInit(&cUnit->fillArrayData, &iterator); |
| 662 | while (true) { |
| 663 | FillArrayData *tabRec = (FillArrayData *) oatGrowableListIteratorNext( |
| 664 | &iterator); |
| 665 | if (tabRec == NULL) break; |
| 666 | alignBuffer(cUnit->codeBuffer, tabRec->offset); |
Ian Rogers | ab058bb | 2012-03-11 22:19:38 -0700 | [diff] [blame] | 667 | for (int i = 0; i < (tabRec->size + 1) / 2; i++) { |
| 668 | cUnit->codeBuffer.push_back( tabRec->table[i] & 0xFF); |
| 669 | cUnit->codeBuffer.push_back( (tabRec->table[i] >> 8) & 0xFF); |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 670 | } |
| 671 | } |
| 672 | } |
| 673 | |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 674 | int assignLiteralOffsetCommon(LIR* lir, int offset) |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 675 | { |
| 676 | for (;lir != NULL; lir = lir->next) { |
| 677 | lir->offset = offset; |
| 678 | offset += 4; |
| 679 | } |
| 680 | return offset; |
| 681 | } |
| 682 | |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 683 | void createMappingTable(CompilationUnit* cUnit) |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 684 | { |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 685 | LIR* tgtLIR; |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 686 | int currentDalvikOffset = -1; |
| 687 | |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 688 | for (tgtLIR = (LIR *) cUnit->firstLIRInsn; |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 689 | tgtLIR; |
| 690 | tgtLIR = NEXT_LIR(tgtLIR)) { |
| 691 | if ((tgtLIR->opcode >= 0) && !tgtLIR->flags.isNop && |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 692 | (currentDalvikOffset != tgtLIR->dalvikOffset)) { |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 693 | // Changed - need to emit a record |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 694 | cUnit->mappingTable.push_back(tgtLIR->offset); |
| 695 | cUnit->mappingTable.push_back(tgtLIR->dalvikOffset); |
| 696 | currentDalvikOffset = tgtLIR->dalvikOffset; |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 697 | } |
| 698 | } |
| 699 | } |
| 700 | |
| 701 | /* Determine the offset of each literal field */ |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 702 | int assignLiteralOffset(CompilationUnit* cUnit, int offset) |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 703 | { |
| 704 | offset = assignLiteralOffsetCommon(cUnit->literalList, offset); |
| 705 | return offset; |
| 706 | } |
| 707 | |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 708 | int assignSwitchTablesOffset(CompilationUnit* cUnit, int offset) |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 709 | { |
| 710 | GrowableListIterator iterator; |
| 711 | oatGrowableListIteratorInit(&cUnit->switchTables, &iterator); |
| 712 | while (true) { |
| 713 | SwitchTable *tabRec = (SwitchTable *) oatGrowableListIteratorNext( |
| 714 | &iterator); |
| 715 | if (tabRec == NULL) break; |
| 716 | tabRec->offset = offset; |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 717 | if (tabRec->table[0] == Instruction::kSparseSwitchSignature) { |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 718 | offset += tabRec->table[1] * (sizeof(int) * 2); |
| 719 | } else { |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 720 | DCHECK_EQ(static_cast<int>(tabRec->table[0]), static_cast<int>(Instruction::kPackedSwitchSignature)); |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 721 | offset += tabRec->table[1] * sizeof(int); |
| 722 | } |
| 723 | } |
| 724 | return offset; |
| 725 | } |
| 726 | |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 727 | int assignFillArrayDataOffset(CompilationUnit* cUnit, int offset) |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 728 | { |
| 729 | GrowableListIterator iterator; |
| 730 | oatGrowableListIteratorInit(&cUnit->fillArrayData, &iterator); |
| 731 | while (true) { |
| 732 | FillArrayData *tabRec = (FillArrayData *) oatGrowableListIteratorNext( |
| 733 | &iterator); |
| 734 | if (tabRec == NULL) break; |
| 735 | tabRec->offset = offset; |
| 736 | offset += tabRec->size; |
| 737 | // word align |
| 738 | offset = (offset + 3) & ~3; |
| 739 | } |
| 740 | return offset; |
| 741 | } |
| 742 | |
| 743 | /* |
| 744 | * Walk the compilation unit and assign offsets to instructions |
| 745 | * and literals and compute the total size of the compiled unit. |
| 746 | */ |
| 747 | void oatAssignOffsets(CompilationUnit* cUnit) |
| 748 | { |
| 749 | int offset = oatAssignInsnOffsets(cUnit); |
| 750 | |
| 751 | /* Const values have to be word aligned */ |
| 752 | offset = (offset + 3) & ~3; |
| 753 | |
| 754 | /* Set up offsets for literals */ |
| 755 | cUnit->dataOffset = offset; |
| 756 | |
| 757 | offset = assignLiteralOffset(cUnit, offset); |
| 758 | |
| 759 | offset = assignSwitchTablesOffset(cUnit, offset); |
| 760 | |
| 761 | offset = assignFillArrayDataOffset(cUnit, offset); |
| 762 | |
| 763 | cUnit->totalSize = offset; |
| 764 | } |
| 765 | |
| 766 | /* |
| 767 | * Go over each instruction in the list and calculate the offset from the top |
| 768 | * before sending them off to the assembler. If out-of-range branch distance is |
| 769 | * seen rearrange the instructions a bit to correct it. |
| 770 | */ |
| 771 | void oatAssembleLIR(CompilationUnit* cUnit) |
| 772 | { |
| 773 | oatAssignOffsets(cUnit); |
| 774 | /* |
| 775 | * Assemble here. Note that we generate code with optimistic assumptions |
| 776 | * and if found now to work, we'll have to redo the sequence and retry. |
| 777 | */ |
| 778 | |
| 779 | while (true) { |
| 780 | AssemblerStatus res = oatAssembleInstructions(cUnit, 0); |
| 781 | if (res == kSuccess) { |
| 782 | break; |
| 783 | } else { |
| 784 | cUnit->assemblerRetries++; |
| 785 | if (cUnit->assemblerRetries > MAX_ASSEMBLER_RETRIES) { |
| 786 | LOG(FATAL) << "Assembler error - too many retries"; |
| 787 | } |
| 788 | // Redo offsets and try again |
| 789 | oatAssignOffsets(cUnit); |
| 790 | cUnit->codeBuffer.clear(); |
| 791 | } |
| 792 | } |
| 793 | |
| 794 | // Install literals |
| 795 | installLiteralPools(cUnit); |
| 796 | |
| 797 | // Install switch tables |
| 798 | installSwitchTables(cUnit); |
| 799 | |
| 800 | // Install fill array data |
| 801 | installFillArrayData(cUnit); |
| 802 | |
| 803 | /* |
| 804 | * Create the mapping table |
| 805 | */ |
| 806 | createMappingTable(cUnit); |
| 807 | } |
| 808 | |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 809 | /* |
| 810 | * Insert a kPseudoCaseLabel at the beginning of the Dalvik |
| 811 | * offset vaddr. This label will be used to fix up the case |
| 812 | * branch table during the assembly phase. Be sure to set |
| 813 | * all resource flags on this to prevent code motion across |
| 814 | * target boundaries. KeyVal is just there for debugging. |
| 815 | */ |
| 816 | LIR* insertCaseLabel(CompilationUnit* cUnit, int vaddr, int keyVal) |
| 817 | { |
| 818 | std::map<unsigned int, LIR*>::iterator it; |
| 819 | it = cUnit->boundaryMap.find(vaddr); |
| 820 | if (it == cUnit->boundaryMap.end()) { |
| 821 | LOG(FATAL) << "Error: didn't find vaddr 0x" << std::hex << vaddr; |
| 822 | } |
| 823 | LIR* newLabel = (LIR*)oatNew(cUnit, sizeof(LIR), true, kAllocLIR); |
| 824 | newLabel->dalvikOffset = vaddr; |
| 825 | newLabel->opcode = kPseudoCaseLabel; |
| 826 | newLabel->operands[0] = keyVal; |
| 827 | oatInsertLIRAfter(it->second, (LIR*)newLabel); |
| 828 | return newLabel; |
| 829 | } |
| 830 | |
| 831 | void markPackedCaseLabels(CompilationUnit* cUnit, SwitchTable *tabRec) |
| 832 | { |
| 833 | const u2* table = tabRec->table; |
| 834 | int baseVaddr = tabRec->vaddr; |
| 835 | int *targets = (int*)&table[4]; |
| 836 | int entries = table[1]; |
| 837 | int lowKey = s4FromSwitchData(&table[2]); |
| 838 | for (int i = 0; i < entries; i++) { |
| 839 | tabRec->targets[i] = insertCaseLabel(cUnit, baseVaddr + targets[i], |
| 840 | i + lowKey); |
| 841 | } |
| 842 | } |
| 843 | |
| 844 | void markSparseCaseLabels(CompilationUnit* cUnit, SwitchTable *tabRec) |
| 845 | { |
| 846 | const u2* table = tabRec->table; |
| 847 | int baseVaddr = tabRec->vaddr; |
| 848 | int entries = table[1]; |
| 849 | int* keys = (int*)&table[2]; |
| 850 | int* targets = &keys[entries]; |
| 851 | for (int i = 0; i < entries; i++) { |
| 852 | tabRec->targets[i] = insertCaseLabel(cUnit, baseVaddr + targets[i], |
| 853 | keys[i]); |
| 854 | } |
| 855 | } |
| 856 | |
| 857 | void oatProcessSwitchTables(CompilationUnit* cUnit) |
| 858 | { |
| 859 | GrowableListIterator iterator; |
| 860 | oatGrowableListIteratorInit(&cUnit->switchTables, &iterator); |
| 861 | while (true) { |
| 862 | SwitchTable *tabRec = (SwitchTable *) oatGrowableListIteratorNext( |
| 863 | &iterator); |
| 864 | if (tabRec == NULL) break; |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 865 | if (tabRec->table[0] == Instruction::kPackedSwitchSignature) { |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 866 | markPackedCaseLabels(cUnit, tabRec); |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 867 | } else if (tabRec->table[0] == Instruction::kSparseSwitchSignature) { |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 868 | markSparseCaseLabels(cUnit, tabRec); |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 869 | } else { |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 870 | LOG(FATAL) << "Invalid switch table"; |
| 871 | } |
| 872 | } |
| 873 | } |
| 874 | |
| 875 | //FIXME: Do we have endian issues here? |
| 876 | |
| 877 | void dumpSparseSwitchTable(const u2* table) |
| 878 | /* |
| 879 | * Sparse switch data format: |
| 880 | * ushort ident = 0x0200 magic value |
| 881 | * ushort size number of entries in the table; > 0 |
| 882 | * int keys[size] keys, sorted low-to-high; 32-bit aligned |
| 883 | * int targets[size] branch targets, relative to switch opcode |
| 884 | * |
| 885 | * Total size is (2+size*4) 16-bit code units. |
| 886 | */ |
| 887 | { |
| 888 | u2 ident = table[0]; |
| 889 | int entries = table[1]; |
| 890 | int* keys = (int*)&table[2]; |
| 891 | int* targets = &keys[entries]; |
| 892 | LOG(INFO) << "Sparse switch table - ident:0x" << std::hex << ident << |
| 893 | ", entries: " << std::dec << entries; |
| 894 | for (int i = 0; i < entries; i++) { |
| 895 | LOG(INFO) << " Key[" << keys[i] << "] -> 0x" << std::hex << |
| 896 | targets[i]; |
| 897 | } |
| 898 | } |
| 899 | |
| 900 | void dumpPackedSwitchTable(const u2* table) |
| 901 | /* |
| 902 | * Packed switch data format: |
| 903 | * ushort ident = 0x0100 magic value |
| 904 | * ushort size number of entries in the table |
| 905 | * int first_key first (and lowest) switch case value |
| 906 | * int targets[size] branch targets, relative to switch opcode |
| 907 | * |
| 908 | * Total size is (4+size*2) 16-bit code units. |
| 909 | */ |
| 910 | { |
| 911 | u2 ident = table[0]; |
| 912 | int* targets = (int*)&table[4]; |
| 913 | int entries = table[1]; |
| 914 | int lowKey = s4FromSwitchData(&table[2]); |
| 915 | LOG(INFO) << "Packed switch table - ident:0x" << std::hex << ident << |
| 916 | ", entries: " << std::dec << entries << ", lowKey: " << lowKey; |
| 917 | for (int i = 0; i < entries; i++) { |
| 918 | LOG(INFO) << " Key[" << (i + lowKey) << "] -> 0x" << std::hex << |
| 919 | targets[i]; |
| 920 | } |
| 921 | } |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 922 | |
| 923 | |
| 924 | } // namespace art |