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