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: |
Ian Rogers | b3ab25b | 2012-03-19 01:12:01 -0700 | [diff] [blame] | 294 | LOG(INFO) << "L" << (void*)lir << ":"; |
buzbee | 5de3494 | 2012-03-01 14:51:57 -0800 | [diff] [blame] | 295 | break; |
| 296 | case kPseudoThrowTarget: |
Ian Rogers | b3ab25b | 2012-03-19 01:12:01 -0700 | [diff] [blame] | 297 | LOG(INFO) << "LT" << (void*)lir << ":"; |
buzbee | 5de3494 | 2012-03-01 14:51:57 -0800 | [diff] [blame] | 298 | break; |
| 299 | case kPseudoSuspendTarget: |
Ian Rogers | b3ab25b | 2012-03-19 01:12:01 -0700 | [diff] [blame] | 300 | LOG(INFO) << "LS" << (void*)lir << ":"; |
buzbee | 5de3494 | 2012-03-01 14:51:57 -0800 | [diff] [blame] | 301 | break; |
| 302 | case kPseudoCaseLabel: |
Ian Rogers | b3ab25b | 2012-03-19 01:12:01 -0700 | [diff] [blame] | 303 | LOG(INFO) << "LC" << (void*)lir << ": Case target 0x" << |
buzbee | 5de3494 | 2012-03-01 14:51:57 -0800 | [diff] [blame] | 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 | { |
buzbee | 9c044ce | 2012-03-18 13:24:07 -0700 | [diff] [blame] | 331 | int numRegs = cUnit->numDalvikRegisters + cUnit->numCompilerTemps + 1; |
| 332 | for (int i = 0; i < numRegs; i++) { |
buzbee | 5de3494 | 2012-03-01 14:51:57 -0800 | [diff] [blame] | 333 | PromotionMap vRegMap = cUnit->promotionMap[i]; |
Elliott Hughes | bdf6c3d | 2012-03-20 13:43:53 -0700 | [diff] [blame] | 334 | std::string buf; |
buzbee | 5de3494 | 2012-03-01 14:51:57 -0800 | [diff] [blame] | 335 | if (vRegMap.fpLocation == kLocPhysReg) { |
Elliott Hughes | bdf6c3d | 2012-03-20 13:43:53 -0700 | [diff] [blame] | 336 | StringAppendF(&buf, " : s%d", vRegMap.fpReg & FP_REG_MASK); |
buzbee | 9c044ce | 2012-03-18 13:24:07 -0700 | [diff] [blame] | 337 | } |
| 338 | |
Elliott Hughes | bdf6c3d | 2012-03-20 13:43:53 -0700 | [diff] [blame] | 339 | std::string buf3; |
| 340 | if (i < cUnit->numDalvikRegisters) { |
| 341 | StringAppendF(&buf3, "%02d", i); |
| 342 | } else if (i == cUnit->methodSReg) { |
| 343 | buf3 = "Method*"; |
| 344 | } else { |
| 345 | StringAppendF(&buf3, "ct%d", i - cUnit->numDalvikRegisters); |
| 346 | } |
| 347 | |
| 348 | LOG(INFO) << StringPrintf("V[%s] -> %s%d%s", buf3.c_str(), |
buzbee | 5de3494 | 2012-03-01 14:51:57 -0800 | [diff] [blame] | 349 | vRegMap.coreLocation == kLocPhysReg ? |
| 350 | "r" : "SP+", vRegMap.coreLocation == kLocPhysReg ? |
Elliott Hughes | bdf6c3d | 2012-03-20 13:43:53 -0700 | [diff] [blame] | 351 | vRegMap.coreReg : oatSRegOffset(cUnit, i), buf.c_str()); |
buzbee | 5de3494 | 2012-03-01 14:51:57 -0800 | [diff] [blame] | 352 | } |
| 353 | } |
| 354 | |
buzbee | 5de3494 | 2012-03-01 14:51:57 -0800 | [diff] [blame] | 355 | /* Dump instructions and constant pool contents */ |
| 356 | void oatCodegenDump(CompilationUnit* cUnit) |
| 357 | { |
| 358 | LOG(INFO) << "/*"; |
| 359 | LOG(INFO) << "Dumping LIR insns for " |
| 360 | << PrettyMethod(cUnit->method_idx, *cUnit->dex_file); |
| 361 | LIR* lirInsn; |
| 362 | LIR* thisLIR; |
| 363 | int insnsSize = cUnit->insnsSize; |
| 364 | |
| 365 | LOG(INFO) << "Regs (excluding ins) : " << cUnit->numRegs; |
| 366 | LOG(INFO) << "Ins : " << cUnit->numIns; |
| 367 | LOG(INFO) << "Outs : " << cUnit->numOuts; |
| 368 | LOG(INFO) << "CoreSpills : " << cUnit->numCoreSpills; |
| 369 | LOG(INFO) << "FPSpills : " << cUnit->numFPSpills; |
buzbee | 239c4e7 | 2012-03-16 08:42:29 -0700 | [diff] [blame] | 370 | LOG(INFO) << "CompilerTemps : " << cUnit->numCompilerTemps; |
buzbee | 5de3494 | 2012-03-01 14:51:57 -0800 | [diff] [blame] | 371 | LOG(INFO) << "Frame size : " << cUnit->frameSize; |
buzbee | 5de3494 | 2012-03-01 14:51:57 -0800 | [diff] [blame] | 372 | LOG(INFO) << "code size is " << cUnit->totalSize << |
| 373 | " bytes, Dalvik size is " << insnsSize * 2; |
| 374 | LOG(INFO) << "expansion factor: " << |
| 375 | (float)cUnit->totalSize / (float)(insnsSize * 2); |
| 376 | oatDumpPromotionMap(cUnit); |
| 377 | for (lirInsn = cUnit->firstLIRInsn; lirInsn; lirInsn = lirInsn->next) { |
| 378 | oatDumpLIRInsn(cUnit, lirInsn, 0); |
| 379 | } |
| 380 | for (lirInsn = cUnit->classPointerList; lirInsn; lirInsn = lirInsn->next) { |
| 381 | thisLIR = (LIR*) lirInsn; |
| 382 | LOG(INFO) << StringPrintf("%x (%04x): .class (%s)", |
| 383 | thisLIR->offset, thisLIR->offset, |
| 384 | ((CallsiteInfo *) thisLIR->operands[0])->classDescriptor); |
| 385 | } |
| 386 | for (lirInsn = cUnit->literalList; lirInsn; lirInsn = lirInsn->next) { |
| 387 | thisLIR = (LIR*) lirInsn; |
| 388 | LOG(INFO) << StringPrintf("%x (%04x): .word (%#x)", |
| 389 | thisLIR->offset, thisLIR->offset, thisLIR->operands[0]); |
| 390 | } |
| 391 | |
| 392 | const DexFile::MethodId& method_id = |
| 393 | cUnit->dex_file->GetMethodId(cUnit->method_idx); |
| 394 | std::string signature(cUnit->dex_file->GetMethodSignature(method_id)); |
| 395 | std::string name(cUnit->dex_file->GetMethodName(method_id)); |
| 396 | std::string descriptor(cUnit->dex_file->GetMethodDeclaringClassDescriptor(method_id)); |
| 397 | |
| 398 | // Dump mapping table |
| 399 | if (cUnit->mappingTable.size() > 0) { |
| 400 | std::string line(StringPrintf("\n MappingTable %s%s_%s_mappingTable[%zu] = {", |
| 401 | descriptor.c_str(), name.c_str(), signature.c_str(), cUnit->mappingTable.size())); |
| 402 | std::replace(line.begin(), line.end(), ';', '_'); |
| 403 | LOG(INFO) << line; |
| 404 | for (uint32_t i = 0; i < cUnit->mappingTable.size(); i+=2) { |
buzbee | 82488f5 | 2012-03-02 08:20:26 -0800 | [diff] [blame] | 405 | line = StringPrintf(" {0x%05x, 0x%04x},", |
buzbee | 5de3494 | 2012-03-01 14:51:57 -0800 | [diff] [blame] | 406 | cUnit->mappingTable[i], cUnit->mappingTable[i+1]); |
| 407 | LOG(INFO) << line; |
| 408 | } |
| 409 | LOG(INFO) <<" };\n\n"; |
| 410 | } |
| 411 | } |
| 412 | |
buzbee | a2ebdd7 | 2012-03-04 14:57:06 -0800 | [diff] [blame] | 413 | |
| 414 | LIR* rawLIR(CompilationUnit* cUnit, int dalvikOffset, int opcode, int op0, |
Ian Rogers | b5d09b2 | 2012-03-06 22:14:17 -0800 | [diff] [blame] | 415 | int op1, int op2, int op3, int op4, LIR* target) |
buzbee | a2ebdd7 | 2012-03-04 14:57:06 -0800 | [diff] [blame] | 416 | { |
| 417 | LIR* insn = (LIR* ) oatNew(cUnit, sizeof(LIR), true, kAllocLIR); |
| 418 | insn->dalvikOffset = dalvikOffset; |
| 419 | insn->opcode = opcode; |
| 420 | insn->operands[0] = op0; |
| 421 | insn->operands[1] = op1; |
| 422 | insn->operands[2] = op2; |
| 423 | insn->operands[3] = op3; |
Ian Rogers | b5d09b2 | 2012-03-06 22:14:17 -0800 | [diff] [blame] | 424 | insn->operands[4] = op4; |
buzbee | a2ebdd7 | 2012-03-04 14:57:06 -0800 | [diff] [blame] | 425 | insn->target = target; |
| 426 | oatSetupResourceMasks(insn); |
| 427 | if (opcode == kPseudoTargetLabel) { |
| 428 | // Always make labels scheduling barriers |
| 429 | insn->defMask = ENCODE_ALL; |
| 430 | } |
| 431 | return insn; |
| 432 | } |
| 433 | |
buzbee | 5de3494 | 2012-03-01 14:51:57 -0800 | [diff] [blame] | 434 | /* |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 435 | * The following are building blocks to construct low-level IRs with 0 - 4 |
| 436 | * operands. |
| 437 | */ |
buzbee | 5de3494 | 2012-03-01 14:51:57 -0800 | [diff] [blame] | 438 | LIR* newLIR0(CompilationUnit* cUnit, int opcode) |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 439 | { |
Ian Rogers | b5d09b2 | 2012-03-06 22:14:17 -0800 | [diff] [blame] | 440 | DCHECK(isPseudoOpcode(opcode) || (EncodingMap[opcode].flags & NO_OPERAND)) |
| 441 | << EncodingMap[opcode].name << " " << (int)opcode << " " |
| 442 | << PrettyMethod(cUnit->method_idx, *cUnit->dex_file) << " " |
| 443 | << cUnit->currentDalvikOffset; |
buzbee | a2ebdd7 | 2012-03-04 14:57:06 -0800 | [diff] [blame] | 444 | LIR* insn = rawLIR(cUnit, cUnit->currentDalvikOffset, opcode); |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 445 | oatAppendLIR(cUnit, (LIR*) insn); |
| 446 | return insn; |
| 447 | } |
| 448 | |
buzbee | 5de3494 | 2012-03-01 14:51:57 -0800 | [diff] [blame] | 449 | LIR* newLIR1(CompilationUnit* cUnit, int opcode, |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 450 | int dest) |
| 451 | { |
Ian Rogers | b5d09b2 | 2012-03-06 22:14:17 -0800 | [diff] [blame] | 452 | DCHECK(isPseudoOpcode(opcode) || (EncodingMap[opcode].flags & IS_UNARY_OP)) |
| 453 | << EncodingMap[opcode].name << " " << (int)opcode << " " |
| 454 | << PrettyMethod(cUnit->method_idx, *cUnit->dex_file) << " " |
| 455 | << cUnit->currentDalvikOffset; |
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 | { |
Ian Rogers | b5d09b2 | 2012-03-06 22:14:17 -0800 | [diff] [blame] | 464 | DCHECK(isPseudoOpcode(opcode) || (EncodingMap[opcode].flags & IS_BINARY_OP)) |
| 465 | << EncodingMap[opcode].name << " " << (int)opcode << " " |
| 466 | << PrettyMethod(cUnit->method_idx, *cUnit->dex_file) << " " |
| 467 | << cUnit->currentDalvikOffset; |
buzbee | a2ebdd7 | 2012-03-04 14:57:06 -0800 | [diff] [blame] | 468 | LIR* insn = rawLIR(cUnit, cUnit->currentDalvikOffset, opcode, dest, src1); |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 469 | oatAppendLIR(cUnit, (LIR*) insn); |
| 470 | return insn; |
| 471 | } |
| 472 | |
buzbee | 5de3494 | 2012-03-01 14:51:57 -0800 | [diff] [blame] | 473 | LIR* newLIR3(CompilationUnit* cUnit, int opcode, |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 474 | int dest, int src1, int src2) |
| 475 | { |
Ian Rogers | b5d09b2 | 2012-03-06 22:14:17 -0800 | [diff] [blame] | 476 | DCHECK(isPseudoOpcode(opcode) || (EncodingMap[opcode].flags & IS_TERTIARY_OP)) |
| 477 | << EncodingMap[opcode].name << " " << (int)opcode << " " |
| 478 | << PrettyMethod(cUnit->method_idx, *cUnit->dex_file) << " " |
| 479 | << cUnit->currentDalvikOffset; |
buzbee | a2ebdd7 | 2012-03-04 14:57:06 -0800 | [diff] [blame] | 480 | LIR* insn = rawLIR(cUnit, cUnit->currentDalvikOffset, opcode, dest, src1, |
| 481 | src2); |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 482 | oatAppendLIR(cUnit, (LIR*) insn); |
| 483 | return insn; |
| 484 | } |
| 485 | |
buzbee | 5de3494 | 2012-03-01 14:51:57 -0800 | [diff] [blame] | 486 | LIR* newLIR4(CompilationUnit* cUnit, int opcode, |
Ian Rogers | b5d09b2 | 2012-03-06 22:14:17 -0800 | [diff] [blame] | 487 | int dest, int src1, int src2, int info) |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 488 | { |
Ian Rogers | b5d09b2 | 2012-03-06 22:14:17 -0800 | [diff] [blame] | 489 | DCHECK(isPseudoOpcode(opcode) || (EncodingMap[opcode].flags & IS_QUAD_OP)) |
| 490 | << EncodingMap[opcode].name << " " << (int)opcode << " " |
| 491 | << PrettyMethod(cUnit->method_idx, *cUnit->dex_file) << " " |
| 492 | << cUnit->currentDalvikOffset; |
buzbee | a2ebdd7 | 2012-03-04 14:57:06 -0800 | [diff] [blame] | 493 | LIR* insn = rawLIR(cUnit, cUnit->currentDalvikOffset, opcode, dest, src1, |
| 494 | src2, info); |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 495 | oatAppendLIR(cUnit, (LIR*) insn); |
| 496 | return insn; |
| 497 | } |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 498 | |
Ian Rogers | b5d09b2 | 2012-03-06 22:14:17 -0800 | [diff] [blame] | 499 | LIR* newLIR5(CompilationUnit* cUnit, int opcode, |
| 500 | int dest, int src1, int src2, int info1, int info2) |
| 501 | { |
| 502 | DCHECK(isPseudoOpcode(opcode) || (EncodingMap[opcode].flags & IS_QUIN_OP)) |
| 503 | << EncodingMap[opcode].name << " " << (int)opcode << " " |
| 504 | << PrettyMethod(cUnit->method_idx, *cUnit->dex_file) << " " |
| 505 | << cUnit->currentDalvikOffset; |
| 506 | LIR* insn = rawLIR(cUnit, cUnit->currentDalvikOffset, opcode, dest, src1, |
| 507 | src2, info1, info2); |
| 508 | oatAppendLIR(cUnit, (LIR*) insn); |
| 509 | return insn; |
| 510 | } |
| 511 | |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 512 | /* |
| 513 | * Search the existing constants in the literal pool for an exact or close match |
| 514 | * within specified delta (greater or equal to 0). |
| 515 | */ |
| 516 | LIR* scanLiteralPool(LIR* dataTarget, int value, unsigned int delta) |
| 517 | { |
| 518 | while (dataTarget) { |
| 519 | if (((unsigned) (value - ((LIR* ) dataTarget)->operands[0])) <= |
| 520 | delta) |
| 521 | return (LIR* ) dataTarget; |
| 522 | dataTarget = dataTarget->next; |
| 523 | } |
| 524 | return NULL; |
| 525 | } |
| 526 | |
| 527 | /* Search the existing constants in the literal pool for an exact wide match */ |
| 528 | LIR* scanLiteralPoolWide(LIR* dataTarget, int valLo, int valHi) |
| 529 | { |
| 530 | bool loMatch = false; |
| 531 | LIR* loTarget = NULL; |
| 532 | while (dataTarget) { |
| 533 | if (loMatch && (((LIR*)dataTarget)->operands[0] == valHi)) { |
| 534 | return (LIR*)loTarget; |
| 535 | } |
| 536 | loMatch = false; |
| 537 | if (((LIR*)dataTarget)->operands[0] == valLo) { |
| 538 | loMatch = true; |
| 539 | loTarget = dataTarget; |
| 540 | } |
| 541 | dataTarget = dataTarget->next; |
| 542 | } |
| 543 | return NULL; |
| 544 | } |
| 545 | |
| 546 | /* |
| 547 | * The following are building blocks to insert constants into the pool or |
| 548 | * instruction streams. |
| 549 | */ |
| 550 | |
buzbee | 5de3494 | 2012-03-01 14:51:57 -0800 | [diff] [blame] | 551 | /* Add a 32-bit constant either in the constant pool */ |
Ian Rogers | 3fa1379 | 2012-03-18 15:53:45 -0700 | [diff] [blame] | 552 | LIR* addWordData(CompilationUnit* cUnit, LIR* *constantListP, int value) |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 553 | { |
| 554 | /* Add the constant to the literal pool */ |
| 555 | if (constantListP) { |
| 556 | LIR* newValue = (LIR* ) oatNew(cUnit, sizeof(LIR), true, |
| 557 | kAllocData); |
| 558 | newValue->operands[0] = value; |
| 559 | newValue->next = *constantListP; |
| 560 | *constantListP = (LIR*) newValue; |
| 561 | return newValue; |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 562 | } |
| 563 | return NULL; |
| 564 | } |
| 565 | |
| 566 | /* Add a 64-bit constant to the constant pool or mixed with code */ |
| 567 | LIR* addWideData(CompilationUnit* cUnit, LIR* *constantListP, |
| 568 | int valLo, int valHi) |
| 569 | { |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 570 | //FIXME: hard-coded little endian, need BE variant |
buzbee | 5de3494 | 2012-03-01 14:51:57 -0800 | [diff] [blame] | 571 | // Insert high word into list first |
| 572 | addWordData(cUnit, constantListP, valHi); |
| 573 | return addWordData(cUnit, constantListP, valLo); |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 574 | } |
| 575 | |
Ian Rogers | ab058bb | 2012-03-11 22:19:38 -0700 | [diff] [blame] | 576 | void pushWord(std::vector<uint8_t>&buf, int data) { |
| 577 | buf.push_back( data & 0xff); |
| 578 | buf.push_back( (data >> 8) & 0xff); |
| 579 | buf.push_back( (data >> 16) & 0xff); |
| 580 | buf.push_back( (data >> 24) & 0xff); |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 581 | } |
| 582 | |
Ian Rogers | ab058bb | 2012-03-11 22:19:38 -0700 | [diff] [blame] | 583 | void alignBuffer(std::vector<uint8_t>&buf, size_t offset) { |
| 584 | while (buf.size() < offset) { |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 585 | buf.push_back(0); |
Ian Rogers | ab058bb | 2012-03-11 22:19:38 -0700 | [diff] [blame] | 586 | } |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 587 | } |
| 588 | |
Brian Carlstrom | f582258 | 2012-03-19 22:34:31 -0700 | [diff] [blame] | 589 | bool IsDirect(int invokeType) { |
| 590 | InvokeType type = static_cast<InvokeType>(invokeType); |
| 591 | return type == kStatic || type == kDirect; |
| 592 | } |
| 593 | |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 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); |
Ian Rogers | 3fa1379 | 2012-03-18 15:53:45 -0700 | [diff] [blame] | 598 | LIR* dataLIR = 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 | } |
Ian Rogers | 3fa1379 | 2012-03-18 15:53:45 -0700 | [diff] [blame] | 603 | // Push code and method literals, record offsets for the compiler to patch. |
| 604 | dataLIR = cUnit->codeLiteralList; |
| 605 | if (dataLIR != NULL) { |
| 606 | while (dataLIR != NULL) { |
Brian Carlstrom | f582258 | 2012-03-19 22:34:31 -0700 | [diff] [blame] | 607 | uint32_t target = dataLIR->operands[0]; |
Ian Rogers | 3fa1379 | 2012-03-18 15:53:45 -0700 | [diff] [blame] | 608 | cUnit->compiler->AddCodePatch(cUnit->dex_cache, cUnit->dex_file, |
| 609 | cUnit->method_idx, |
Brian Carlstrom | f582258 | 2012-03-19 22:34:31 -0700 | [diff] [blame] | 610 | cUnit->access_flags, |
| 611 | target, |
| 612 | IsDirect(dataLIR->operands[1]), |
Ian Rogers | 3fa1379 | 2012-03-18 15:53:45 -0700 | [diff] [blame] | 613 | cUnit->codeBuffer.size()); |
Brian Carlstrom | f582258 | 2012-03-19 22:34:31 -0700 | [diff] [blame] | 614 | const DexFile::MethodId& id = cUnit->dex_file->GetMethodId(target); |
| 615 | // unique based on target to ensure code deduplication works |
| 616 | uint32_t unique_patch_value = reinterpret_cast<uint32_t>(&id); |
| 617 | pushWord(cUnit->codeBuffer, unique_patch_value); |
Ian Rogers | 3fa1379 | 2012-03-18 15:53:45 -0700 | [diff] [blame] | 618 | dataLIR = NEXT_LIR(dataLIR); |
| 619 | } |
| 620 | dataLIR = cUnit->methodLiteralList; |
| 621 | while (dataLIR != NULL) { |
Brian Carlstrom | f582258 | 2012-03-19 22:34:31 -0700 | [diff] [blame] | 622 | uint32_t target = dataLIR->operands[0]; |
Ian Rogers | 3fa1379 | 2012-03-18 15:53:45 -0700 | [diff] [blame] | 623 | cUnit->compiler->AddMethodPatch(cUnit->dex_cache, cUnit->dex_file, |
| 624 | cUnit->method_idx, |
Brian Carlstrom | f582258 | 2012-03-19 22:34:31 -0700 | [diff] [blame] | 625 | cUnit->access_flags, |
| 626 | target, |
| 627 | IsDirect(dataLIR->operands[1]), |
Ian Rogers | 3fa1379 | 2012-03-18 15:53:45 -0700 | [diff] [blame] | 628 | cUnit->codeBuffer.size()); |
Brian Carlstrom | f582258 | 2012-03-19 22:34:31 -0700 | [diff] [blame] | 629 | const DexFile::MethodId& id = cUnit->dex_file->GetMethodId(target); |
| 630 | // unique based on target to ensure code deduplication works |
| 631 | uint32_t unique_patch_value = reinterpret_cast<uint32_t>(&id); |
| 632 | pushWord(cUnit->codeBuffer, unique_patch_value); |
Ian Rogers | 3fa1379 | 2012-03-18 15:53:45 -0700 | [diff] [blame] | 633 | dataLIR = NEXT_LIR(dataLIR); |
| 634 | } |
| 635 | } |
| 636 | |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 637 | } |
| 638 | |
| 639 | /* Write the switch tables to the output stream */ |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 640 | void installSwitchTables(CompilationUnit* cUnit) |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 641 | { |
| 642 | GrowableListIterator iterator; |
| 643 | oatGrowableListIteratorInit(&cUnit->switchTables, &iterator); |
| 644 | while (true) { |
| 645 | SwitchTable* tabRec = (SwitchTable *) oatGrowableListIteratorNext( |
| 646 | &iterator); |
| 647 | if (tabRec == NULL) break; |
| 648 | alignBuffer(cUnit->codeBuffer, tabRec->offset); |
buzbee | c5159d5 | 2012-03-03 11:48:39 -0800 | [diff] [blame] | 649 | /* |
| 650 | * For Arm, our reference point is the address of the bx |
| 651 | * instruction that does the launch, so we have to subtract |
| 652 | * the auto pc-advance. For other targets the reference point |
| 653 | * is a label, so we can use the offset as-is. |
| 654 | */ |
| 655 | #if defined(TARGET_ARM) |
| 656 | int bxOffset = tabRec->anchor->offset + 4; |
| 657 | #else |
| 658 | int bxOffset = tabRec->anchor->offset; |
| 659 | #endif |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 660 | if (cUnit->printMe) { |
| 661 | LOG(INFO) << "Switch table for offset 0x" << std::hex << bxOffset; |
| 662 | } |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 663 | if (tabRec->table[0] == Instruction::kSparseSwitchSignature) { |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 664 | int* keys = (int*)&(tabRec->table[2]); |
| 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 << "] key: 0x" << |
| 669 | std::hex << keys[elems] << ", disp: 0x" << |
| 670 | std::hex << disp; |
| 671 | } |
| 672 | pushWord(cUnit->codeBuffer, keys[elems]); |
| 673 | pushWord(cUnit->codeBuffer, |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 674 | tabRec->targets[elems]->offset - bxOffset); |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 675 | } |
| 676 | } else { |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 677 | DCHECK_EQ(static_cast<int>(tabRec->table[0]), static_cast<int>(Instruction::kPackedSwitchSignature)); |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 678 | for (int elems = 0; elems < tabRec->table[1]; elems++) { |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 679 | int disp = tabRec->targets[elems]->offset - bxOffset; |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 680 | if (cUnit->printMe) { |
| 681 | LOG(INFO) << " Case[" << elems << "] disp: 0x" << |
| 682 | std::hex << disp; |
| 683 | } |
| 684 | pushWord(cUnit->codeBuffer, |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 685 | tabRec->targets[elems]->offset - bxOffset); |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 686 | } |
| 687 | } |
| 688 | } |
| 689 | } |
| 690 | |
| 691 | /* Write the fill array dta to the output stream */ |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 692 | void installFillArrayData(CompilationUnit* cUnit) |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 693 | { |
| 694 | GrowableListIterator iterator; |
| 695 | oatGrowableListIteratorInit(&cUnit->fillArrayData, &iterator); |
| 696 | while (true) { |
| 697 | FillArrayData *tabRec = (FillArrayData *) oatGrowableListIteratorNext( |
| 698 | &iterator); |
| 699 | if (tabRec == NULL) break; |
| 700 | alignBuffer(cUnit->codeBuffer, tabRec->offset); |
Ian Rogers | ab058bb | 2012-03-11 22:19:38 -0700 | [diff] [blame] | 701 | for (int i = 0; i < (tabRec->size + 1) / 2; i++) { |
| 702 | cUnit->codeBuffer.push_back( tabRec->table[i] & 0xFF); |
| 703 | cUnit->codeBuffer.push_back( (tabRec->table[i] >> 8) & 0xFF); |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 704 | } |
| 705 | } |
| 706 | } |
| 707 | |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 708 | int assignLiteralOffsetCommon(LIR* lir, int offset) |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 709 | { |
| 710 | for (;lir != NULL; lir = lir->next) { |
| 711 | lir->offset = offset; |
| 712 | offset += 4; |
| 713 | } |
| 714 | return offset; |
| 715 | } |
| 716 | |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 717 | void createMappingTable(CompilationUnit* cUnit) |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 718 | { |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 719 | LIR* tgtLIR; |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 720 | int currentDalvikOffset = -1; |
| 721 | |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 722 | for (tgtLIR = (LIR *) cUnit->firstLIRInsn; |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 723 | tgtLIR; |
| 724 | tgtLIR = NEXT_LIR(tgtLIR)) { |
| 725 | if ((tgtLIR->opcode >= 0) && !tgtLIR->flags.isNop && |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 726 | (currentDalvikOffset != tgtLIR->dalvikOffset)) { |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 727 | // Changed - need to emit a record |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 728 | cUnit->mappingTable.push_back(tgtLIR->offset); |
| 729 | cUnit->mappingTable.push_back(tgtLIR->dalvikOffset); |
| 730 | currentDalvikOffset = tgtLIR->dalvikOffset; |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 731 | } |
| 732 | } |
| 733 | } |
| 734 | |
| 735 | /* Determine the offset of each literal field */ |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 736 | int assignLiteralOffset(CompilationUnit* cUnit, int offset) |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 737 | { |
| 738 | offset = assignLiteralOffsetCommon(cUnit->literalList, offset); |
Ian Rogers | 3fa1379 | 2012-03-18 15:53:45 -0700 | [diff] [blame] | 739 | offset = assignLiteralOffsetCommon(cUnit->codeLiteralList, offset); |
| 740 | offset = assignLiteralOffsetCommon(cUnit->methodLiteralList, offset); |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 741 | return offset; |
| 742 | } |
| 743 | |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 744 | int assignSwitchTablesOffset(CompilationUnit* cUnit, int offset) |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 745 | { |
| 746 | GrowableListIterator iterator; |
| 747 | oatGrowableListIteratorInit(&cUnit->switchTables, &iterator); |
| 748 | while (true) { |
| 749 | SwitchTable *tabRec = (SwitchTable *) oatGrowableListIteratorNext( |
| 750 | &iterator); |
| 751 | if (tabRec == NULL) break; |
| 752 | tabRec->offset = offset; |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 753 | if (tabRec->table[0] == Instruction::kSparseSwitchSignature) { |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 754 | offset += tabRec->table[1] * (sizeof(int) * 2); |
| 755 | } else { |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 756 | DCHECK_EQ(static_cast<int>(tabRec->table[0]), static_cast<int>(Instruction::kPackedSwitchSignature)); |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 757 | offset += tabRec->table[1] * sizeof(int); |
| 758 | } |
| 759 | } |
| 760 | return offset; |
| 761 | } |
| 762 | |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 763 | int assignFillArrayDataOffset(CompilationUnit* cUnit, int offset) |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 764 | { |
| 765 | GrowableListIterator iterator; |
| 766 | oatGrowableListIteratorInit(&cUnit->fillArrayData, &iterator); |
| 767 | while (true) { |
| 768 | FillArrayData *tabRec = (FillArrayData *) oatGrowableListIteratorNext( |
| 769 | &iterator); |
| 770 | if (tabRec == NULL) break; |
| 771 | tabRec->offset = offset; |
| 772 | offset += tabRec->size; |
| 773 | // word align |
| 774 | offset = (offset + 3) & ~3; |
| 775 | } |
| 776 | return offset; |
| 777 | } |
| 778 | |
| 779 | /* |
| 780 | * Walk the compilation unit and assign offsets to instructions |
| 781 | * and literals and compute the total size of the compiled unit. |
| 782 | */ |
| 783 | void oatAssignOffsets(CompilationUnit* cUnit) |
| 784 | { |
| 785 | int offset = oatAssignInsnOffsets(cUnit); |
| 786 | |
| 787 | /* Const values have to be word aligned */ |
| 788 | offset = (offset + 3) & ~3; |
| 789 | |
| 790 | /* Set up offsets for literals */ |
| 791 | cUnit->dataOffset = offset; |
| 792 | |
| 793 | offset = assignLiteralOffset(cUnit, offset); |
| 794 | |
| 795 | offset = assignSwitchTablesOffset(cUnit, offset); |
| 796 | |
| 797 | offset = assignFillArrayDataOffset(cUnit, offset); |
| 798 | |
| 799 | cUnit->totalSize = offset; |
| 800 | } |
| 801 | |
| 802 | /* |
| 803 | * Go over each instruction in the list and calculate the offset from the top |
| 804 | * before sending them off to the assembler. If out-of-range branch distance is |
| 805 | * seen rearrange the instructions a bit to correct it. |
| 806 | */ |
| 807 | void oatAssembleLIR(CompilationUnit* cUnit) |
| 808 | { |
| 809 | oatAssignOffsets(cUnit); |
| 810 | /* |
| 811 | * Assemble here. Note that we generate code with optimistic assumptions |
| 812 | * and if found now to work, we'll have to redo the sequence and retry. |
| 813 | */ |
| 814 | |
| 815 | while (true) { |
| 816 | AssemblerStatus res = oatAssembleInstructions(cUnit, 0); |
| 817 | if (res == kSuccess) { |
| 818 | break; |
| 819 | } else { |
| 820 | cUnit->assemblerRetries++; |
| 821 | if (cUnit->assemblerRetries > MAX_ASSEMBLER_RETRIES) { |
Ian Rogers | b41b33b | 2012-03-20 14:22:54 -0700 | [diff] [blame^] | 822 | oatCodegenDump(cUnit); |
| 823 | LOG(FATAL) << "Assembler error - too many retries"; |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 824 | } |
| 825 | // Redo offsets and try again |
| 826 | oatAssignOffsets(cUnit); |
| 827 | cUnit->codeBuffer.clear(); |
| 828 | } |
| 829 | } |
| 830 | |
| 831 | // Install literals |
| 832 | installLiteralPools(cUnit); |
| 833 | |
| 834 | // Install switch tables |
| 835 | installSwitchTables(cUnit); |
| 836 | |
| 837 | // Install fill array data |
| 838 | installFillArrayData(cUnit); |
| 839 | |
| 840 | /* |
| 841 | * Create the mapping table |
| 842 | */ |
| 843 | createMappingTable(cUnit); |
| 844 | } |
| 845 | |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 846 | /* |
| 847 | * Insert a kPseudoCaseLabel at the beginning of the Dalvik |
| 848 | * offset vaddr. This label will be used to fix up the case |
| 849 | * branch table during the assembly phase. Be sure to set |
| 850 | * all resource flags on this to prevent code motion across |
| 851 | * target boundaries. KeyVal is just there for debugging. |
| 852 | */ |
| 853 | LIR* insertCaseLabel(CompilationUnit* cUnit, int vaddr, int keyVal) |
| 854 | { |
| 855 | std::map<unsigned int, LIR*>::iterator it; |
| 856 | it = cUnit->boundaryMap.find(vaddr); |
| 857 | if (it == cUnit->boundaryMap.end()) { |
| 858 | LOG(FATAL) << "Error: didn't find vaddr 0x" << std::hex << vaddr; |
| 859 | } |
| 860 | LIR* newLabel = (LIR*)oatNew(cUnit, sizeof(LIR), true, kAllocLIR); |
| 861 | newLabel->dalvikOffset = vaddr; |
| 862 | newLabel->opcode = kPseudoCaseLabel; |
| 863 | newLabel->operands[0] = keyVal; |
| 864 | oatInsertLIRAfter(it->second, (LIR*)newLabel); |
| 865 | return newLabel; |
| 866 | } |
| 867 | |
| 868 | void markPackedCaseLabels(CompilationUnit* cUnit, SwitchTable *tabRec) |
| 869 | { |
| 870 | const u2* table = tabRec->table; |
| 871 | int baseVaddr = tabRec->vaddr; |
| 872 | int *targets = (int*)&table[4]; |
| 873 | int entries = table[1]; |
| 874 | int lowKey = s4FromSwitchData(&table[2]); |
| 875 | for (int i = 0; i < entries; i++) { |
| 876 | tabRec->targets[i] = insertCaseLabel(cUnit, baseVaddr + targets[i], |
| 877 | i + lowKey); |
| 878 | } |
| 879 | } |
| 880 | |
| 881 | void markSparseCaseLabels(CompilationUnit* cUnit, SwitchTable *tabRec) |
| 882 | { |
| 883 | const u2* table = tabRec->table; |
| 884 | int baseVaddr = tabRec->vaddr; |
| 885 | int entries = table[1]; |
| 886 | int* keys = (int*)&table[2]; |
| 887 | int* targets = &keys[entries]; |
| 888 | for (int i = 0; i < entries; i++) { |
| 889 | tabRec->targets[i] = insertCaseLabel(cUnit, baseVaddr + targets[i], |
| 890 | keys[i]); |
| 891 | } |
| 892 | } |
| 893 | |
| 894 | void oatProcessSwitchTables(CompilationUnit* cUnit) |
| 895 | { |
| 896 | GrowableListIterator iterator; |
| 897 | oatGrowableListIteratorInit(&cUnit->switchTables, &iterator); |
| 898 | while (true) { |
| 899 | SwitchTable *tabRec = (SwitchTable *) oatGrowableListIteratorNext( |
| 900 | &iterator); |
| 901 | if (tabRec == NULL) break; |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 902 | if (tabRec->table[0] == Instruction::kPackedSwitchSignature) { |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 903 | markPackedCaseLabels(cUnit, tabRec); |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 904 | } else if (tabRec->table[0] == Instruction::kSparseSwitchSignature) { |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 905 | markSparseCaseLabels(cUnit, tabRec); |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 906 | } else { |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 907 | LOG(FATAL) << "Invalid switch table"; |
| 908 | } |
| 909 | } |
| 910 | } |
| 911 | |
| 912 | //FIXME: Do we have endian issues here? |
| 913 | |
| 914 | void dumpSparseSwitchTable(const u2* table) |
| 915 | /* |
| 916 | * Sparse switch data format: |
| 917 | * ushort ident = 0x0200 magic value |
| 918 | * ushort size number of entries in the table; > 0 |
| 919 | * int keys[size] keys, sorted low-to-high; 32-bit aligned |
| 920 | * int targets[size] branch targets, relative to switch opcode |
| 921 | * |
| 922 | * Total size is (2+size*4) 16-bit code units. |
| 923 | */ |
| 924 | { |
| 925 | u2 ident = table[0]; |
| 926 | int entries = table[1]; |
| 927 | int* keys = (int*)&table[2]; |
| 928 | int* targets = &keys[entries]; |
| 929 | LOG(INFO) << "Sparse switch table - ident:0x" << std::hex << ident << |
| 930 | ", entries: " << std::dec << entries; |
| 931 | for (int i = 0; i < entries; i++) { |
| 932 | LOG(INFO) << " Key[" << keys[i] << "] -> 0x" << std::hex << |
| 933 | targets[i]; |
| 934 | } |
| 935 | } |
| 936 | |
| 937 | void dumpPackedSwitchTable(const u2* table) |
| 938 | /* |
| 939 | * Packed switch data format: |
| 940 | * ushort ident = 0x0100 magic value |
| 941 | * ushort size number of entries in the table |
| 942 | * int first_key first (and lowest) switch case value |
| 943 | * int targets[size] branch targets, relative to switch opcode |
| 944 | * |
| 945 | * Total size is (4+size*2) 16-bit code units. |
| 946 | */ |
| 947 | { |
| 948 | u2 ident = table[0]; |
| 949 | int* targets = (int*)&table[4]; |
| 950 | int entries = table[1]; |
| 951 | int lowKey = s4FromSwitchData(&table[2]); |
| 952 | LOG(INFO) << "Packed switch table - ident:0x" << std::hex << ident << |
| 953 | ", entries: " << std::dec << entries << ", lowKey: " << lowKey; |
| 954 | for (int i = 0; i < entries; i++) { |
| 955 | LOG(INFO) << " Key[" << (i + lowKey) << "] -> 0x" << std::hex << |
| 956 | targets[i]; |
| 957 | } |
| 958 | } |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 959 | |
| 960 | |
| 961 | } // namespace art |