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 | |
buzbee | 1bc37c6 | 2012-11-20 13:35:41 -0800 | [diff] [blame] | 17 | #include "../compiler_internals.h" |
Ian Rogers | 0c7abda | 2012-09-19 13:33:42 -0700 | [diff] [blame] | 18 | #include "gc_map.h" |
| 19 | #include "verifier/dex_gc_map.h" |
| 20 | #include "verifier/method_verifier.h" |
buzbee | 1bc37c6 | 2012-11-20 13:35:41 -0800 | [diff] [blame] | 21 | #include "ralloc_util.h" |
buzbee | eaf09bc | 2012-11-15 14:51:41 -0800 | [diff] [blame] | 22 | #include "codegen_util.h" |
Ian Rogers | 0c7abda | 2012-09-19 13:33:42 -0700 | [diff] [blame] | 23 | |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 24 | namespace art { |
| 25 | |
buzbee | cbd6d44 | 2012-11-17 14:11:25 -0800 | [diff] [blame] | 26 | /* Convert an instruction to a NOP */ |
| 27 | void oatNopLIR( LIR* lir) |
| 28 | { |
| 29 | lir->flags.isNop = true; |
| 30 | } |
| 31 | |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 32 | void setMemRefType(LIR* lir, bool isLoad, int memType) |
| 33 | { |
buzbee | eaf09bc | 2012-11-15 14:51:41 -0800 | [diff] [blame] | 34 | uint64_t *maskPtr; |
| 35 | uint64_t mask = ENCODE_MEM;; |
buzbee | 1bc37c6 | 2012-11-20 13:35:41 -0800 | [diff] [blame] | 36 | DCHECK(getTargetInstFlags(lir->opcode) & (IS_LOAD | IS_STORE)); |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 37 | if (isLoad) { |
| 38 | maskPtr = &lir->useMask; |
| 39 | } else { |
| 40 | maskPtr = &lir->defMask; |
| 41 | } |
| 42 | /* Clear out the memref flags */ |
| 43 | *maskPtr &= ~mask; |
| 44 | /* ..and then add back the one we need */ |
| 45 | switch (memType) { |
| 46 | case kLiteral: |
| 47 | DCHECK(isLoad); |
| 48 | *maskPtr |= ENCODE_LITERAL; |
| 49 | break; |
| 50 | case kDalvikReg: |
| 51 | *maskPtr |= ENCODE_DALVIK_REG; |
| 52 | break; |
| 53 | case kHeapRef: |
| 54 | *maskPtr |= ENCODE_HEAP_REF; |
| 55 | break; |
| 56 | case kMustNotAlias: |
| 57 | /* Currently only loads can be marked as kMustNotAlias */ |
buzbee | 1bc37c6 | 2012-11-20 13:35:41 -0800 | [diff] [blame] | 58 | DCHECK(!(getTargetInstFlags(lir->opcode) & IS_STORE)); |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 59 | *maskPtr |= ENCODE_MUST_NOT_ALIAS; |
| 60 | break; |
| 61 | default: |
| 62 | LOG(FATAL) << "Oat: invalid memref kind - " << memType; |
| 63 | } |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | /* |
Ian Rogers | b5d09b2 | 2012-03-06 22:14:17 -0800 | [diff] [blame] | 67 | * Mark load/store instructions that access Dalvik registers through the stack. |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 68 | */ |
Ian Rogers | b5d09b2 | 2012-03-06 22:14:17 -0800 | [diff] [blame] | 69 | void annotateDalvikRegAccess(LIR* lir, int regId, bool isLoad, bool is64bit) |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 70 | { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 71 | setMemRefType(lir, isLoad, kDalvikReg); |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 72 | |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 73 | /* |
| 74 | * Store the Dalvik register id in aliasInfo. Mark the MSB if it is a 64-bit |
| 75 | * access. |
| 76 | */ |
buzbee | ec13743 | 2012-11-13 12:13:16 -0800 | [diff] [blame] | 77 | lir->aliasInfo = ENCODE_ALIAS_INFO(regId, is64bit); |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 78 | } |
| 79 | |
buzbee | eaf09bc | 2012-11-15 14:51:41 -0800 | [diff] [blame] | 80 | uint64_t oatGetRegMaskCommon(CompilationUnit* cUnit, int reg) |
buzbee | b046e16 | 2012-10-30 15:48:42 -0700 | [diff] [blame] | 81 | { |
| 82 | return getRegMaskCommon(cUnit, reg); |
| 83 | } |
| 84 | |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 85 | /* |
| 86 | * Mark the corresponding bit(s). |
| 87 | */ |
buzbee | eaf09bc | 2012-11-15 14:51:41 -0800 | [diff] [blame] | 88 | inline void setupRegMask(CompilationUnit* cUnit, uint64_t* mask, int reg) |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 89 | { |
buzbee | b046e16 | 2012-10-30 15:48:42 -0700 | [diff] [blame] | 90 | *mask |= getRegMaskCommon(cUnit, reg); |
| 91 | } |
| 92 | |
| 93 | /* Exported version of setupRegMask */ |
buzbee | eaf09bc | 2012-11-15 14:51:41 -0800 | [diff] [blame] | 94 | void oatSetupRegMask(CompilationUnit* cUnit, uint64_t* mask, int reg) |
buzbee | b046e16 | 2012-10-30 15:48:42 -0700 | [diff] [blame] | 95 | { |
| 96 | setupRegMask(cUnit, mask, reg); |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | /* |
| 100 | * Set up the proper fields in the resource mask |
| 101 | */ |
buzbee | b046e16 | 2012-10-30 15:48:42 -0700 | [diff] [blame] | 102 | void setupResourceMasks(CompilationUnit* cUnit, LIR* lir) |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 103 | { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 104 | int opcode = lir->opcode; |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 105 | |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 106 | if (opcode <= 0) { |
| 107 | lir->useMask = lir->defMask = 0; |
| 108 | return; |
| 109 | } |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 110 | |
buzbee | 1bc37c6 | 2012-11-20 13:35:41 -0800 | [diff] [blame] | 111 | uint64_t flags = getTargetInstFlags(opcode); |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 112 | |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 113 | if (flags & NEEDS_FIXUP) { |
| 114 | lir->flags.pcRelFixup = true; |
| 115 | } |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 116 | |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 117 | /* Get the starting size of the instruction's template */ |
| 118 | lir->flags.size = oatGetInsnSize(lir); |
buzbee | e88dfbf | 2012-03-05 11:19:57 -0800 | [diff] [blame] | 119 | |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [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 | } |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 125 | |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 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 | } |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 134 | |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 135 | if (flags & REG_DEF0) { |
buzbee | b046e16 | 2012-10-30 15:48:42 -0700 | [diff] [blame] | 136 | setupRegMask(cUnit, &lir->defMask, lir->operands[0]); |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 137 | } |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 138 | |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 139 | if (flags & REG_DEF1) { |
buzbee | b046e16 | 2012-10-30 15:48:42 -0700 | [diff] [blame] | 140 | setupRegMask(cUnit, &lir->defMask, lir->operands[1]); |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 141 | } |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 142 | |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 143 | |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 144 | if (flags & SETS_CCODES) { |
| 145 | lir->defMask |= ENCODE_CCODE; |
| 146 | } |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 147 | |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 148 | if (flags & (REG_USE0 | REG_USE1 | REG_USE2 | REG_USE3)) { |
| 149 | int i; |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 150 | |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 151 | for (i = 0; i < 4; i++) { |
| 152 | if (flags & (1 << (kRegUse0 + i))) { |
buzbee | b046e16 | 2012-10-30 15:48:42 -0700 | [diff] [blame] | 153 | setupRegMask(cUnit, &lir->useMask, lir->operands[i]); |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 154 | } |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 155 | } |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 156 | } |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 157 | |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 158 | if (flags & USES_CCODES) { |
| 159 | lir->useMask |= ENCODE_CCODE; |
| 160 | } |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 161 | |
buzbee | b046e16 | 2012-10-30 15:48:42 -0700 | [diff] [blame] | 162 | // Handle target-specific actions |
| 163 | setupTargetResourceMasks(cUnit, lir); |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 164 | } |
| 165 | |
| 166 | /* |
buzbee | 5de3494 | 2012-03-01 14:51:57 -0800 | [diff] [blame] | 167 | * Debugging macros |
| 168 | */ |
| 169 | #define DUMP_RESOURCE_MASK(X) |
| 170 | #define DUMP_SSA_REP(X) |
| 171 | |
| 172 | /* Pretty-print a LIR instruction */ |
buzbee | cbd6d44 | 2012-11-17 14:11:25 -0800 | [diff] [blame] | 173 | void oatDumpLIRInsn(CompilationUnit* cUnit, LIR* lir, unsigned char* baseAddr) |
buzbee | 5de3494 | 2012-03-01 14:51:57 -0800 | [diff] [blame] | 174 | { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 175 | int offset = lir->offset; |
| 176 | int dest = lir->operands[0]; |
| 177 | const bool dumpNop = (cUnit->enableDebug & (1 << kDebugShowNops)); |
buzbee | 5de3494 | 2012-03-01 14:51:57 -0800 | [diff] [blame] | 178 | |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 179 | /* Handle pseudo-ops individually, and all regular insns as a group */ |
| 180 | switch (lir->opcode) { |
| 181 | case kPseudoMethodEntry: |
| 182 | LOG(INFO) << "-------- method entry " |
| 183 | << PrettyMethod(cUnit->method_idx, *cUnit->dex_file); |
| 184 | break; |
| 185 | case kPseudoMethodExit: |
| 186 | LOG(INFO) << "-------- Method_Exit"; |
| 187 | break; |
| 188 | case kPseudoBarrier: |
| 189 | LOG(INFO) << "-------- BARRIER"; |
| 190 | break; |
| 191 | case kPseudoExtended: |
buzbee | cbd6d44 | 2012-11-17 14:11:25 -0800 | [diff] [blame] | 192 | LOG(INFO) << "-------- " << reinterpret_cast<char*>(dest); |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 193 | break; |
| 194 | case kPseudoSSARep: |
buzbee | cbd6d44 | 2012-11-17 14:11:25 -0800 | [diff] [blame] | 195 | DUMP_SSA_REP(LOG(INFO) << "-------- kMirOpPhi: " << reinterpret_cast<char*>(dest)); |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 196 | break; |
| 197 | case kPseudoEntryBlock: |
| 198 | LOG(INFO) << "-------- entry offset: 0x" << std::hex << dest; |
| 199 | break; |
| 200 | case kPseudoDalvikByteCodeBoundary: |
| 201 | LOG(INFO) << "-------- dalvik offset: 0x" << std::hex |
buzbee | cbd6d44 | 2012-11-17 14:11:25 -0800 | [diff] [blame] | 202 | << lir->dalvikOffset << " @ " << reinterpret_cast<char*>(lir->operands[0]); |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 203 | break; |
| 204 | case kPseudoExitBlock: |
| 205 | LOG(INFO) << "-------- exit offset: 0x" << std::hex << dest; |
| 206 | break; |
| 207 | case kPseudoPseudoAlign4: |
buzbee | cbd6d44 | 2012-11-17 14:11:25 -0800 | [diff] [blame] | 208 | LOG(INFO) << reinterpret_cast<uintptr_t>(baseAddr) + offset << " (0x" << std::hex |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 209 | << offset << "): .align4"; |
| 210 | break; |
| 211 | case kPseudoEHBlockLabel: |
| 212 | LOG(INFO) << "Exception_Handling:"; |
| 213 | break; |
| 214 | case kPseudoTargetLabel: |
| 215 | case kPseudoNormalBlockLabel: |
buzbee | cbd6d44 | 2012-11-17 14:11:25 -0800 | [diff] [blame] | 216 | LOG(INFO) << "L" << reinterpret_cast<void*>(lir) << ":"; |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 217 | break; |
| 218 | case kPseudoThrowTarget: |
buzbee | cbd6d44 | 2012-11-17 14:11:25 -0800 | [diff] [blame] | 219 | LOG(INFO) << "LT" << reinterpret_cast<void*>(lir) << ":"; |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 220 | break; |
| 221 | case kPseudoIntrinsicRetry: |
buzbee | cbd6d44 | 2012-11-17 14:11:25 -0800 | [diff] [blame] | 222 | LOG(INFO) << "IR" << reinterpret_cast<void*>(lir) << ":"; |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 223 | break; |
| 224 | case kPseudoSuspendTarget: |
buzbee | cbd6d44 | 2012-11-17 14:11:25 -0800 | [diff] [blame] | 225 | LOG(INFO) << "LS" << reinterpret_cast<void*>(lir) << ":"; |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 226 | break; |
buzbee | 8320f38 | 2012-09-11 16:29:42 -0700 | [diff] [blame] | 227 | case kPseudoSafepointPC: |
| 228 | LOG(INFO) << "LsafepointPC_0x" << std::hex << lir->offset << "_" << lir->dalvikOffset << ":"; |
| 229 | break; |
Bill Buzbee | a5b3024 | 2012-09-28 07:19:44 -0700 | [diff] [blame] | 230 | case kPseudoExportedPC: |
| 231 | LOG(INFO) << "LexportedPC_0x" << std::hex << lir->offset << "_" << lir->dalvikOffset << ":"; |
| 232 | break; |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 233 | case kPseudoCaseLabel: |
buzbee | cbd6d44 | 2012-11-17 14:11:25 -0800 | [diff] [blame] | 234 | LOG(INFO) << "LC" << reinterpret_cast<void*>(lir) << ": Case target 0x" |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 235 | << std::hex << lir->operands[0] << "|" << std::dec << |
| 236 | lir->operands[0]; |
| 237 | break; |
| 238 | default: |
| 239 | if (lir->flags.isNop && !dumpNop) { |
| 240 | break; |
| 241 | } else { |
buzbee | 1bc37c6 | 2012-11-20 13:35:41 -0800 | [diff] [blame] | 242 | std::string op_name(buildInsnString(getTargetInstName(lir->opcode), |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 243 | lir, baseAddr)); |
buzbee | 1bc37c6 | 2012-11-20 13:35:41 -0800 | [diff] [blame] | 244 | std::string op_operands(buildInsnString(getTargetInstFmt(lir->opcode), |
| 245 | lir, baseAddr)); |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 246 | LOG(INFO) << StringPrintf("%05x: %-9s%s%s", |
buzbee | cbd6d44 | 2012-11-17 14:11:25 -0800 | [diff] [blame] | 247 | reinterpret_cast<unsigned int>(baseAddr + offset), |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 248 | op_name.c_str(), op_operands.c_str(), |
| 249 | lir->flags.isNop ? "(nop)" : ""); |
| 250 | } |
| 251 | break; |
| 252 | } |
buzbee | 5de3494 | 2012-03-01 14:51:57 -0800 | [diff] [blame] | 253 | |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 254 | if (lir->useMask && (!lir->flags.isNop || dumpNop)) { |
| 255 | DUMP_RESOURCE_MASK(oatDumpResourceMask((LIR* ) lir, lir->useMask, "use")); |
| 256 | } |
| 257 | if (lir->defMask && (!lir->flags.isNop || dumpNop)) { |
| 258 | DUMP_RESOURCE_MASK(oatDumpResourceMask((LIR* ) lir, lir->defMask, "def")); |
| 259 | } |
buzbee | 5de3494 | 2012-03-01 14:51:57 -0800 | [diff] [blame] | 260 | } |
| 261 | |
| 262 | void oatDumpPromotionMap(CompilationUnit *cUnit) |
| 263 | { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 264 | int numRegs = cUnit->numDalvikRegisters + cUnit->numCompilerTemps + 1; |
| 265 | for (int i = 0; i < numRegs; i++) { |
| 266 | PromotionMap vRegMap = cUnit->promotionMap[i]; |
| 267 | std::string buf; |
| 268 | if (vRegMap.fpLocation == kLocPhysReg) { |
buzbee | f0504cd | 2012-11-13 16:31:10 -0800 | [diff] [blame] | 269 | StringAppendF(&buf, " : s%d", vRegMap.fpReg & fpRegMask()); |
buzbee | 5de3494 | 2012-03-01 14:51:57 -0800 | [diff] [blame] | 270 | } |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 271 | |
| 272 | std::string buf3; |
| 273 | if (i < cUnit->numDalvikRegisters) { |
| 274 | StringAppendF(&buf3, "%02d", i); |
| 275 | } else if (i == cUnit->methodSReg) { |
| 276 | buf3 = "Method*"; |
| 277 | } else { |
| 278 | StringAppendF(&buf3, "ct%d", i - cUnit->numDalvikRegisters); |
| 279 | } |
| 280 | |
| 281 | LOG(INFO) << StringPrintf("V[%s] -> %s%d%s", buf3.c_str(), |
| 282 | vRegMap.coreLocation == kLocPhysReg ? |
| 283 | "r" : "SP+", vRegMap.coreLocation == kLocPhysReg ? |
| 284 | vRegMap.coreReg : oatSRegOffset(cUnit, i), |
| 285 | buf.c_str()); |
| 286 | } |
buzbee | 5de3494 | 2012-03-01 14:51:57 -0800 | [diff] [blame] | 287 | } |
| 288 | |
Bill Buzbee | a5b3024 | 2012-09-28 07:19:44 -0700 | [diff] [blame] | 289 | /* Dump a mapping table */ |
| 290 | void dumpMappingTable(const char* table_name, const std::string& descriptor, |
| 291 | const std::string& name, const std::string& signature, |
| 292 | const std::vector<uint32_t>& v) { |
| 293 | if (v.size() > 0) { |
| 294 | std::string line(StringPrintf("\n %s %s%s_%s_table[%zu] = {", table_name, |
| 295 | descriptor.c_str(), name.c_str(), signature.c_str(), v.size())); |
| 296 | std::replace(line.begin(), line.end(), ';', '_'); |
| 297 | LOG(INFO) << line; |
| 298 | for (uint32_t i = 0; i < v.size(); i+=2) { |
| 299 | line = StringPrintf(" {0x%05x, 0x%04x},", v[i], v[i+1]); |
| 300 | LOG(INFO) << line; |
| 301 | } |
| 302 | LOG(INFO) <<" };\n\n"; |
| 303 | } |
| 304 | } |
| 305 | |
buzbee | 5de3494 | 2012-03-01 14:51:57 -0800 | [diff] [blame] | 306 | /* Dump instructions and constant pool contents */ |
| 307 | void oatCodegenDump(CompilationUnit* cUnit) |
| 308 | { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 309 | LOG(INFO) << "Dumping LIR insns for " |
| 310 | << PrettyMethod(cUnit->method_idx, *cUnit->dex_file); |
| 311 | LIR* lirInsn; |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 312 | int insnsSize = cUnit->insnsSize; |
buzbee | 5de3494 | 2012-03-01 14:51:57 -0800 | [diff] [blame] | 313 | |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 314 | LOG(INFO) << "Regs (excluding ins) : " << cUnit->numRegs; |
| 315 | LOG(INFO) << "Ins : " << cUnit->numIns; |
| 316 | LOG(INFO) << "Outs : " << cUnit->numOuts; |
| 317 | LOG(INFO) << "CoreSpills : " << cUnit->numCoreSpills; |
| 318 | LOG(INFO) << "FPSpills : " << cUnit->numFPSpills; |
| 319 | LOG(INFO) << "CompilerTemps : " << cUnit->numCompilerTemps; |
| 320 | LOG(INFO) << "Frame size : " << cUnit->frameSize; |
| 321 | LOG(INFO) << "code size is " << cUnit->totalSize << |
| 322 | " bytes, Dalvik size is " << insnsSize * 2; |
| 323 | LOG(INFO) << "expansion factor: " |
buzbee | cbd6d44 | 2012-11-17 14:11:25 -0800 | [diff] [blame] | 324 | << static_cast<float>(cUnit->totalSize) / static_cast<float>(insnsSize * 2); |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 325 | oatDumpPromotionMap(cUnit); |
| 326 | for (lirInsn = cUnit->firstLIRInsn; lirInsn; lirInsn = lirInsn->next) { |
| 327 | oatDumpLIRInsn(cUnit, lirInsn, 0); |
| 328 | } |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 329 | for (lirInsn = cUnit->literalList; lirInsn; lirInsn = lirInsn->next) { |
buzbee | cbd6d44 | 2012-11-17 14:11:25 -0800 | [diff] [blame] | 330 | LOG(INFO) << StringPrintf("%x (%04x): .word (%#x)", lirInsn->offset, lirInsn->offset, |
| 331 | lirInsn->operands[0]); |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 332 | } |
buzbee | 5de3494 | 2012-03-01 14:51:57 -0800 | [diff] [blame] | 333 | |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 334 | const DexFile::MethodId& method_id = |
| 335 | cUnit->dex_file->GetMethodId(cUnit->method_idx); |
| 336 | std::string signature(cUnit->dex_file->GetMethodSignature(method_id)); |
| 337 | std::string name(cUnit->dex_file->GetMethodName(method_id)); |
| 338 | std::string descriptor(cUnit->dex_file->GetMethodDeclaringClassDescriptor(method_id)); |
buzbee | 5de3494 | 2012-03-01 14:51:57 -0800 | [diff] [blame] | 339 | |
Bill Buzbee | a5b3024 | 2012-09-28 07:19:44 -0700 | [diff] [blame] | 340 | // Dump mapping tables |
| 341 | dumpMappingTable("PC2Dex_MappingTable", descriptor, name, signature, cUnit->pc2dexMappingTable); |
| 342 | dumpMappingTable("Dex2PC_MappingTable", descriptor, name, signature, cUnit->dex2pcMappingTable); |
buzbee | 5de3494 | 2012-03-01 14:51:57 -0800 | [diff] [blame] | 343 | } |
| 344 | |
buzbee | a2ebdd7 | 2012-03-04 14:57:06 -0800 | [diff] [blame] | 345 | |
| 346 | LIR* rawLIR(CompilationUnit* cUnit, int dalvikOffset, int opcode, int op0, |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 347 | int op1, int op2, int op3, int op4, LIR* target) |
buzbee | a2ebdd7 | 2012-03-04 14:57:06 -0800 | [diff] [blame] | 348 | { |
buzbee | cbd6d44 | 2012-11-17 14:11:25 -0800 | [diff] [blame] | 349 | LIR* insn = static_cast<LIR*>(oatNew(cUnit, sizeof(LIR), true, kAllocLIR)); |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 350 | insn->dalvikOffset = dalvikOffset; |
| 351 | insn->opcode = opcode; |
| 352 | insn->operands[0] = op0; |
| 353 | insn->operands[1] = op1; |
| 354 | insn->operands[2] = op2; |
| 355 | insn->operands[3] = op3; |
| 356 | insn->operands[4] = op4; |
| 357 | insn->target = target; |
buzbee | b046e16 | 2012-10-30 15:48:42 -0700 | [diff] [blame] | 358 | oatSetupResourceMasks(cUnit, insn); |
Bill Buzbee | a5b3024 | 2012-09-28 07:19:44 -0700 | [diff] [blame] | 359 | if ((opcode == kPseudoTargetLabel) || (opcode == kPseudoSafepointPC) || |
| 360 | (opcode == kPseudoExportedPC)) { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 361 | // Always make labels scheduling barriers |
buzbee | 8320f38 | 2012-09-11 16:29:42 -0700 | [diff] [blame] | 362 | insn->useMask = insn->defMask = ENCODE_ALL; |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 363 | } |
| 364 | return insn; |
buzbee | a2ebdd7 | 2012-03-04 14:57:06 -0800 | [diff] [blame] | 365 | } |
| 366 | |
buzbee | 5de3494 | 2012-03-01 14:51:57 -0800 | [diff] [blame] | 367 | /* |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 368 | * The following are building blocks to construct low-level IRs with 0 - 4 |
| 369 | * operands. |
| 370 | */ |
buzbee | 5de3494 | 2012-03-01 14:51:57 -0800 | [diff] [blame] | 371 | LIR* newLIR0(CompilationUnit* cUnit, int opcode) |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 372 | { |
buzbee | 1bc37c6 | 2012-11-20 13:35:41 -0800 | [diff] [blame] | 373 | DCHECK(isPseudoOpcode(opcode) || (getTargetInstFlags(opcode) & NO_OPERAND)) |
| 374 | << getTargetInstName(opcode) << " " << opcode << " " |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 375 | << PrettyMethod(cUnit->method_idx, *cUnit->dex_file) << " " |
| 376 | << cUnit->currentDalvikOffset; |
| 377 | LIR* insn = rawLIR(cUnit, cUnit->currentDalvikOffset, opcode); |
buzbee | cbd6d44 | 2012-11-17 14:11:25 -0800 | [diff] [blame] | 378 | oatAppendLIR(cUnit, insn); |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 379 | return insn; |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 380 | } |
| 381 | |
buzbee | 5de3494 | 2012-03-01 14:51:57 -0800 | [diff] [blame] | 382 | LIR* newLIR1(CompilationUnit* cUnit, int opcode, |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 383 | int dest) |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 384 | { |
buzbee | 1bc37c6 | 2012-11-20 13:35:41 -0800 | [diff] [blame] | 385 | DCHECK(isPseudoOpcode(opcode) || (getTargetInstFlags(opcode) & IS_UNARY_OP)) |
| 386 | << getTargetInstName(opcode) << " " << opcode << " " |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 387 | << PrettyMethod(cUnit->method_idx, *cUnit->dex_file) << " " |
| 388 | << cUnit->currentDalvikOffset; |
| 389 | LIR* insn = rawLIR(cUnit, cUnit->currentDalvikOffset, opcode, dest); |
buzbee | cbd6d44 | 2012-11-17 14:11:25 -0800 | [diff] [blame] | 390 | oatAppendLIR(cUnit, insn); |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 391 | return insn; |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 392 | } |
| 393 | |
buzbee | 5de3494 | 2012-03-01 14:51:57 -0800 | [diff] [blame] | 394 | LIR* newLIR2(CompilationUnit* cUnit, int opcode, |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 395 | int dest, int src1) |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 396 | { |
buzbee | 1bc37c6 | 2012-11-20 13:35:41 -0800 | [diff] [blame] | 397 | DCHECK(isPseudoOpcode(opcode) || (getTargetInstFlags(opcode) & IS_BINARY_OP)) |
| 398 | << getTargetInstName(opcode) << " " << opcode << " " |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 399 | << PrettyMethod(cUnit->method_idx, *cUnit->dex_file) << " " |
| 400 | << cUnit->currentDalvikOffset; |
| 401 | LIR* insn = rawLIR(cUnit, cUnit->currentDalvikOffset, opcode, dest, src1); |
buzbee | cbd6d44 | 2012-11-17 14:11:25 -0800 | [diff] [blame] | 402 | oatAppendLIR(cUnit, insn); |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 403 | return insn; |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 404 | } |
| 405 | |
buzbee | 5de3494 | 2012-03-01 14:51:57 -0800 | [diff] [blame] | 406 | LIR* newLIR3(CompilationUnit* cUnit, int opcode, |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 407 | int dest, int src1, int src2) |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 408 | { |
buzbee | 1bc37c6 | 2012-11-20 13:35:41 -0800 | [diff] [blame] | 409 | DCHECK(isPseudoOpcode(opcode) || (getTargetInstFlags(opcode) & IS_TERTIARY_OP)) |
| 410 | << getTargetInstName(opcode) << " " << opcode << " " |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 411 | << PrettyMethod(cUnit->method_idx, *cUnit->dex_file) << " " |
| 412 | << cUnit->currentDalvikOffset; |
buzbee | cbd6d44 | 2012-11-17 14:11:25 -0800 | [diff] [blame] | 413 | LIR* insn = rawLIR(cUnit, cUnit->currentDalvikOffset, opcode, dest, src1, src2); |
| 414 | oatAppendLIR(cUnit, insn); |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 415 | return insn; |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 416 | } |
| 417 | |
buzbee | 5de3494 | 2012-03-01 14:51:57 -0800 | [diff] [blame] | 418 | LIR* newLIR4(CompilationUnit* cUnit, int opcode, |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 419 | int dest, int src1, int src2, int info) |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 420 | { |
buzbee | 1bc37c6 | 2012-11-20 13:35:41 -0800 | [diff] [blame] | 421 | DCHECK(isPseudoOpcode(opcode) || (getTargetInstFlags(opcode) & IS_QUAD_OP)) |
| 422 | << getTargetInstName(opcode) << " " << opcode << " " |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 423 | << PrettyMethod(cUnit->method_idx, *cUnit->dex_file) << " " |
| 424 | << cUnit->currentDalvikOffset; |
buzbee | cbd6d44 | 2012-11-17 14:11:25 -0800 | [diff] [blame] | 425 | LIR* insn = rawLIR(cUnit, cUnit->currentDalvikOffset, opcode, dest, src1, src2, info); |
| 426 | oatAppendLIR(cUnit, insn); |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 427 | return insn; |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 428 | } |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 429 | |
Ian Rogers | b5d09b2 | 2012-03-06 22:14:17 -0800 | [diff] [blame] | 430 | LIR* newLIR5(CompilationUnit* cUnit, int opcode, |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 431 | int dest, int src1, int src2, int info1, int info2) |
Ian Rogers | b5d09b2 | 2012-03-06 22:14:17 -0800 | [diff] [blame] | 432 | { |
buzbee | 1bc37c6 | 2012-11-20 13:35:41 -0800 | [diff] [blame] | 433 | DCHECK(isPseudoOpcode(opcode) || (getTargetInstFlags(opcode) & IS_QUIN_OP)) |
| 434 | << getTargetInstName(opcode) << " " << opcode << " " |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 435 | << PrettyMethod(cUnit->method_idx, *cUnit->dex_file) << " " |
| 436 | << cUnit->currentDalvikOffset; |
buzbee | cbd6d44 | 2012-11-17 14:11:25 -0800 | [diff] [blame] | 437 | LIR* insn = rawLIR(cUnit, cUnit->currentDalvikOffset, opcode, dest, src1, src2, info1, info2); |
| 438 | oatAppendLIR(cUnit, insn); |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 439 | return insn; |
Ian Rogers | b5d09b2 | 2012-03-06 22:14:17 -0800 | [diff] [blame] | 440 | } |
| 441 | |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 442 | /* |
| 443 | * Search the existing constants in the literal pool for an exact or close match |
| 444 | * within specified delta (greater or equal to 0). |
| 445 | */ |
| 446 | LIR* scanLiteralPool(LIR* dataTarget, int value, unsigned int delta) |
| 447 | { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 448 | while (dataTarget) { |
buzbee | cbd6d44 | 2012-11-17 14:11:25 -0800 | [diff] [blame] | 449 | if ((static_cast<unsigned>(value - dataTarget->operands[0])) <= delta) |
| 450 | return dataTarget; |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 451 | dataTarget = dataTarget->next; |
| 452 | } |
| 453 | return NULL; |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 454 | } |
| 455 | |
| 456 | /* Search the existing constants in the literal pool for an exact wide match */ |
| 457 | LIR* scanLiteralPoolWide(LIR* dataTarget, int valLo, int valHi) |
| 458 | { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 459 | bool loMatch = false; |
| 460 | LIR* loTarget = NULL; |
| 461 | while (dataTarget) { |
buzbee | cbd6d44 | 2012-11-17 14:11:25 -0800 | [diff] [blame] | 462 | if (loMatch && (dataTarget->operands[0] == valHi)) { |
| 463 | return loTarget; |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 464 | } |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 465 | loMatch = false; |
buzbee | cbd6d44 | 2012-11-17 14:11:25 -0800 | [diff] [blame] | 466 | if (dataTarget->operands[0] == valLo) { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 467 | loMatch = true; |
| 468 | loTarget = dataTarget; |
| 469 | } |
| 470 | dataTarget = dataTarget->next; |
| 471 | } |
| 472 | return NULL; |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 473 | } |
| 474 | |
| 475 | /* |
| 476 | * The following are building blocks to insert constants into the pool or |
| 477 | * instruction streams. |
| 478 | */ |
| 479 | |
buzbee | 5de3494 | 2012-03-01 14:51:57 -0800 | [diff] [blame] | 480 | /* Add a 32-bit constant either in the constant pool */ |
Ian Rogers | 3fa1379 | 2012-03-18 15:53:45 -0700 | [diff] [blame] | 481 | LIR* addWordData(CompilationUnit* cUnit, LIR* *constantListP, int value) |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 482 | { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 483 | /* Add the constant to the literal pool */ |
| 484 | if (constantListP) { |
buzbee | cbd6d44 | 2012-11-17 14:11:25 -0800 | [diff] [blame] | 485 | LIR* newValue = static_cast<LIR*>(oatNew(cUnit, sizeof(LIR), true, kAllocData)); |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 486 | newValue->operands[0] = value; |
| 487 | newValue->next = *constantListP; |
buzbee | cbd6d44 | 2012-11-17 14:11:25 -0800 | [diff] [blame] | 488 | *constantListP = newValue; |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 489 | return newValue; |
| 490 | } |
| 491 | return NULL; |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 492 | } |
| 493 | |
| 494 | /* Add a 64-bit constant to the constant pool or mixed with code */ |
| 495 | LIR* addWideData(CompilationUnit* cUnit, LIR* *constantListP, |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 496 | int valLo, int valHi) |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 497 | { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 498 | addWordData(cUnit, constantListP, valHi); |
| 499 | return addWordData(cUnit, constantListP, valLo); |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 500 | } |
| 501 | |
Ian Rogers | ab058bb | 2012-03-11 22:19:38 -0700 | [diff] [blame] | 502 | void pushWord(std::vector<uint8_t>&buf, int data) { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 503 | buf.push_back( data & 0xff); |
| 504 | buf.push_back( (data >> 8) & 0xff); |
| 505 | buf.push_back( (data >> 16) & 0xff); |
| 506 | buf.push_back( (data >> 24) & 0xff); |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 507 | } |
| 508 | |
Ian Rogers | ab058bb | 2012-03-11 22:19:38 -0700 | [diff] [blame] | 509 | void alignBuffer(std::vector<uint8_t>&buf, size_t offset) { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 510 | while (buf.size() < offset) { |
| 511 | buf.push_back(0); |
| 512 | } |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 513 | } |
| 514 | |
Brian Carlstrom | f582258 | 2012-03-19 22:34:31 -0700 | [diff] [blame] | 515 | bool IsDirect(int invokeType) { |
| 516 | InvokeType type = static_cast<InvokeType>(invokeType); |
| 517 | return type == kStatic || type == kDirect; |
| 518 | } |
| 519 | |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 520 | /* Write the literal pool to the output stream */ |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 521 | void installLiteralPools(CompilationUnit* cUnit) |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 522 | { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 523 | alignBuffer(cUnit->codeBuffer, cUnit->dataOffset); |
| 524 | LIR* dataLIR = cUnit->literalList; |
| 525 | while (dataLIR != NULL) { |
| 526 | pushWord(cUnit->codeBuffer, dataLIR->operands[0]); |
| 527 | dataLIR = NEXT_LIR(dataLIR); |
| 528 | } |
| 529 | // Push code and method literals, record offsets for the compiler to patch. |
| 530 | dataLIR = cUnit->codeLiteralList; |
Ian Rogers | 137e88f | 2012-10-08 17:46:47 -0700 | [diff] [blame] | 531 | while (dataLIR != NULL) { |
| 532 | uint32_t target = dataLIR->operands[0]; |
| 533 | cUnit->compiler->AddCodePatch(cUnit->dex_file, |
| 534 | cUnit->method_idx, |
| 535 | cUnit->invoke_type, |
| 536 | target, |
| 537 | static_cast<InvokeType>(dataLIR->operands[1]), |
| 538 | cUnit->codeBuffer.size()); |
| 539 | const DexFile::MethodId& id = cUnit->dex_file->GetMethodId(target); |
| 540 | // unique based on target to ensure code deduplication works |
| 541 | uint32_t unique_patch_value = reinterpret_cast<uint32_t>(&id); |
| 542 | pushWord(cUnit->codeBuffer, unique_patch_value); |
| 543 | dataLIR = NEXT_LIR(dataLIR); |
| 544 | } |
| 545 | dataLIR = cUnit->methodLiteralList; |
| 546 | while (dataLIR != NULL) { |
| 547 | uint32_t target = dataLIR->operands[0]; |
| 548 | cUnit->compiler->AddMethodPatch(cUnit->dex_file, |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 549 | cUnit->method_idx, |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 550 | cUnit->invoke_type, |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 551 | target, |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 552 | static_cast<InvokeType>(dataLIR->operands[1]), |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 553 | cUnit->codeBuffer.size()); |
Ian Rogers | 137e88f | 2012-10-08 17:46:47 -0700 | [diff] [blame] | 554 | const DexFile::MethodId& id = cUnit->dex_file->GetMethodId(target); |
| 555 | // unique based on target to ensure code deduplication works |
| 556 | uint32_t unique_patch_value = reinterpret_cast<uint32_t>(&id); |
| 557 | pushWord(cUnit->codeBuffer, unique_patch_value); |
| 558 | dataLIR = NEXT_LIR(dataLIR); |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 559 | } |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 560 | } |
| 561 | |
| 562 | /* Write the switch tables to the output stream */ |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 563 | void installSwitchTables(CompilationUnit* cUnit) |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 564 | { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 565 | GrowableListIterator iterator; |
| 566 | oatGrowableListIteratorInit(&cUnit->switchTables, &iterator); |
| 567 | while (true) { |
buzbee | cbd6d44 | 2012-11-17 14:11:25 -0800 | [diff] [blame] | 568 | SwitchTable* tabRec = reinterpret_cast<SwitchTable*>(oatGrowableListIteratorNext( &iterator)); |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 569 | if (tabRec == NULL) break; |
| 570 | alignBuffer(cUnit->codeBuffer, tabRec->offset); |
| 571 | /* |
| 572 | * For Arm, our reference point is the address of the bx |
| 573 | * instruction that does the launch, so we have to subtract |
| 574 | * the auto pc-advance. For other targets the reference point |
| 575 | * is a label, so we can use the offset as-is. |
| 576 | */ |
buzbee | b046e16 | 2012-10-30 15:48:42 -0700 | [diff] [blame] | 577 | int bxOffset = INVALID_OFFSET; |
| 578 | switch (cUnit->instructionSet) { |
| 579 | case kThumb2: |
| 580 | bxOffset = tabRec->anchor->offset + 4; |
| 581 | break; |
| 582 | case kX86: |
| 583 | bxOffset = 0; |
| 584 | break; |
| 585 | case kMips: |
| 586 | bxOffset = tabRec->anchor->offset; |
| 587 | break; |
| 588 | default: LOG(FATAL) << "Unexpected instruction set: " << cUnit->instructionSet; |
| 589 | } |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 590 | if (cUnit->printMe) { |
| 591 | LOG(INFO) << "Switch table for offset 0x" << std::hex << bxOffset; |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 592 | } |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 593 | if (tabRec->table[0] == Instruction::kSparseSwitchSignature) { |
buzbee | cbd6d44 | 2012-11-17 14:11:25 -0800 | [diff] [blame] | 594 | const int* keys = reinterpret_cast<const int*>(&(tabRec->table[2])); |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 595 | for (int elems = 0; elems < tabRec->table[1]; elems++) { |
| 596 | int disp = tabRec->targets[elems]->offset - bxOffset; |
| 597 | if (cUnit->printMe) { |
| 598 | LOG(INFO) << " Case[" << elems << "] key: 0x" |
| 599 | << std::hex << keys[elems] << ", disp: 0x" |
| 600 | << std::hex << disp; |
| 601 | } |
| 602 | pushWord(cUnit->codeBuffer, keys[elems]); |
| 603 | pushWord(cUnit->codeBuffer, |
| 604 | tabRec->targets[elems]->offset - bxOffset); |
| 605 | } |
| 606 | } else { |
| 607 | DCHECK_EQ(static_cast<int>(tabRec->table[0]), |
| 608 | static_cast<int>(Instruction::kPackedSwitchSignature)); |
| 609 | for (int elems = 0; elems < tabRec->table[1]; elems++) { |
| 610 | int disp = tabRec->targets[elems]->offset - bxOffset; |
| 611 | if (cUnit->printMe) { |
| 612 | LOG(INFO) << " Case[" << elems << "] disp: 0x" |
| 613 | << std::hex << disp; |
| 614 | } |
| 615 | pushWord(cUnit->codeBuffer, tabRec->targets[elems]->offset - bxOffset); |
| 616 | } |
| 617 | } |
| 618 | } |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 619 | } |
| 620 | |
| 621 | /* Write the fill array dta to the output stream */ |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 622 | void installFillArrayData(CompilationUnit* cUnit) |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 623 | { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 624 | GrowableListIterator iterator; |
| 625 | oatGrowableListIteratorInit(&cUnit->fillArrayData, &iterator); |
| 626 | while (true) { |
buzbee | cbd6d44 | 2012-11-17 14:11:25 -0800 | [diff] [blame] | 627 | FillArrayData *tabRec = |
| 628 | reinterpret_cast<FillArrayData*>(oatGrowableListIteratorNext( &iterator)); |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 629 | if (tabRec == NULL) break; |
| 630 | alignBuffer(cUnit->codeBuffer, tabRec->offset); |
| 631 | for (int i = 0; i < (tabRec->size + 1) / 2; i++) { |
| 632 | cUnit->codeBuffer.push_back( tabRec->table[i] & 0xFF); |
| 633 | cUnit->codeBuffer.push_back( (tabRec->table[i] >> 8) & 0xFF); |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 634 | } |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 635 | } |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 636 | } |
| 637 | |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 638 | int assignLiteralOffsetCommon(LIR* lir, int offset) |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 639 | { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 640 | for (;lir != NULL; lir = lir->next) { |
| 641 | lir->offset = offset; |
| 642 | offset += 4; |
| 643 | } |
| 644 | return offset; |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 645 | } |
| 646 | |
buzbee | 6459e7c | 2012-10-02 14:42:41 -0700 | [diff] [blame] | 647 | // Make sure we have a code address for every declared catch entry |
| 648 | bool verifyCatchEntries(CompilationUnit* cUnit) |
| 649 | { |
| 650 | bool success = true; |
| 651 | for (std::set<uint32_t>::const_iterator it = cUnit->catches.begin(); it != cUnit->catches.end(); ++it) { |
| 652 | uint32_t dexPc = *it; |
| 653 | bool found = false; |
| 654 | for (size_t i = 0; i < cUnit->dex2pcMappingTable.size(); i += 2) { |
| 655 | if (dexPc == cUnit->dex2pcMappingTable[i+1]) { |
| 656 | found = true; |
| 657 | break; |
| 658 | } |
| 659 | } |
| 660 | if (!found) { |
| 661 | LOG(INFO) << "Missing native PC for catch entry @ 0x" << std::hex << dexPc; |
| 662 | success = false; |
| 663 | } |
| 664 | } |
| 665 | // Now, try in the other direction |
| 666 | for (size_t i = 0; i < cUnit->dex2pcMappingTable.size(); i += 2) { |
| 667 | uint32_t dexPc = cUnit->dex2pcMappingTable[i+1]; |
| 668 | if (cUnit->catches.find(dexPc) == cUnit->catches.end()) { |
| 669 | LOG(INFO) << "Unexpected catch entry @ dex pc 0x" << std::hex << dexPc; |
| 670 | success = false; |
| 671 | } |
| 672 | } |
| 673 | if (!success) { |
| 674 | LOG(INFO) << "Bad dex2pcMapping table in " << PrettyMethod(cUnit->method_idx, *cUnit->dex_file); |
| 675 | LOG(INFO) << "Entries @ decode: " << cUnit->catches.size() << ", Entries in table: " |
| 676 | << cUnit->dex2pcMappingTable.size()/2; |
| 677 | } |
| 678 | return success; |
| 679 | } |
| 680 | |
Bill Buzbee | a5b3024 | 2012-09-28 07:19:44 -0700 | [diff] [blame] | 681 | void createMappingTables(CompilationUnit* cUnit) |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 682 | { |
buzbee | cbd6d44 | 2012-11-17 14:11:25 -0800 | [diff] [blame] | 683 | for (LIR* tgtLIR = cUnit->firstLIRInsn; tgtLIR != NULL; tgtLIR = NEXT_LIR(tgtLIR)) { |
buzbee | 8320f38 | 2012-09-11 16:29:42 -0700 | [diff] [blame] | 684 | if (!tgtLIR->flags.isNop && (tgtLIR->opcode == kPseudoSafepointPC)) { |
Bill Buzbee | a5b3024 | 2012-09-28 07:19:44 -0700 | [diff] [blame] | 685 | cUnit->pc2dexMappingTable.push_back(tgtLIR->offset); |
| 686 | cUnit->pc2dexMappingTable.push_back(tgtLIR->dalvikOffset); |
| 687 | } |
| 688 | if (!tgtLIR->flags.isNop && (tgtLIR->opcode == kPseudoExportedPC)) { |
| 689 | cUnit->dex2pcMappingTable.push_back(tgtLIR->offset); |
| 690 | cUnit->dex2pcMappingTable.push_back(tgtLIR->dalvikOffset); |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 691 | } |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 692 | } |
buzbee | 6459e7c | 2012-10-02 14:42:41 -0700 | [diff] [blame] | 693 | DCHECK(verifyCatchEntries(cUnit)); |
Bill Buzbee | a5b3024 | 2012-09-28 07:19:44 -0700 | [diff] [blame] | 694 | cUnit->combinedMappingTable.push_back(cUnit->pc2dexMappingTable.size() + |
| 695 | cUnit->dex2pcMappingTable.size()); |
| 696 | cUnit->combinedMappingTable.push_back(cUnit->pc2dexMappingTable.size()); |
| 697 | cUnit->combinedMappingTable.insert(cUnit->combinedMappingTable.end(), |
| 698 | cUnit->pc2dexMappingTable.begin(), |
| 699 | cUnit->pc2dexMappingTable.end()); |
| 700 | cUnit->combinedMappingTable.insert(cUnit->combinedMappingTable.end(), |
| 701 | cUnit->dex2pcMappingTable.begin(), |
| 702 | cUnit->dex2pcMappingTable.end()); |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 703 | } |
| 704 | |
Ian Rogers | 0c7abda | 2012-09-19 13:33:42 -0700 | [diff] [blame] | 705 | class NativePcToReferenceMapBuilder { |
| 706 | public: |
| 707 | NativePcToReferenceMapBuilder(std::vector<uint8_t>* table, |
| 708 | size_t entries, uint32_t max_native_offset, |
| 709 | size_t references_width) : entries_(entries), |
| 710 | references_width_(references_width), in_use_(entries), |
| 711 | table_(table) { |
| 712 | // Compute width in bytes needed to hold max_native_offset. |
| 713 | native_offset_width_ = 0; |
| 714 | while (max_native_offset != 0) { |
| 715 | native_offset_width_++; |
| 716 | max_native_offset >>= 8; |
| 717 | } |
| 718 | // Resize table and set up header. |
| 719 | table->resize((EntryWidth() * entries) + sizeof(uint32_t)); |
Ian Rogers | 000d724 | 2012-09-21 16:07:36 -0700 | [diff] [blame] | 720 | CHECK_LT(native_offset_width_, 1U << 3); |
| 721 | (*table)[0] = native_offset_width_ & 7; |
| 722 | CHECK_LT(references_width_, 1U << 13); |
| 723 | (*table)[0] |= (references_width_ << 3) & 0xFF; |
| 724 | (*table)[1] = (references_width_ >> 5) & 0xFF; |
Ian Rogers | 0c7abda | 2012-09-19 13:33:42 -0700 | [diff] [blame] | 725 | CHECK_LT(entries, 1U << 16); |
| 726 | (*table)[2] = entries & 0xFF; |
| 727 | (*table)[3] = (entries >> 8) & 0xFF; |
| 728 | } |
| 729 | |
| 730 | void AddEntry(uint32_t native_offset, const uint8_t* references) { |
| 731 | size_t table_index = TableIndex(native_offset); |
| 732 | while (in_use_[table_index]) { |
| 733 | table_index = (table_index + 1) % entries_; |
| 734 | } |
| 735 | in_use_[table_index] = true; |
| 736 | SetNativeOffset(table_index, native_offset); |
| 737 | DCHECK_EQ(native_offset, GetNativeOffset(table_index)); |
| 738 | SetReferences(table_index, references); |
| 739 | } |
| 740 | |
| 741 | private: |
| 742 | size_t TableIndex(uint32_t native_offset) { |
| 743 | return NativePcOffsetToReferenceMap::Hash(native_offset) % entries_; |
| 744 | } |
| 745 | |
| 746 | uint32_t GetNativeOffset(size_t table_index) { |
| 747 | uint32_t native_offset = 0; |
| 748 | size_t table_offset = (table_index * EntryWidth()) + sizeof(uint32_t); |
| 749 | for (size_t i = 0; i < native_offset_width_; i++) { |
| 750 | native_offset |= (*table_)[table_offset + i] << (i * 8); |
| 751 | } |
| 752 | return native_offset; |
| 753 | } |
| 754 | |
| 755 | void SetNativeOffset(size_t table_index, uint32_t native_offset) { |
| 756 | size_t table_offset = (table_index * EntryWidth()) + sizeof(uint32_t); |
| 757 | for (size_t i = 0; i < native_offset_width_; i++) { |
| 758 | (*table_)[table_offset + i] = (native_offset >> (i * 8)) & 0xFF; |
| 759 | } |
| 760 | } |
| 761 | |
| 762 | void SetReferences(size_t table_index, const uint8_t* references) { |
| 763 | size_t table_offset = (table_index * EntryWidth()) + sizeof(uint32_t); |
| 764 | memcpy(&(*table_)[table_offset + native_offset_width_], references, references_width_); |
| 765 | } |
| 766 | |
| 767 | size_t EntryWidth() const { |
| 768 | return native_offset_width_ + references_width_; |
| 769 | } |
| 770 | |
| 771 | // Number of entries in the table. |
| 772 | const size_t entries_; |
| 773 | // Number of bytes used to encode the reference bitmap. |
| 774 | const size_t references_width_; |
| 775 | // Number of bytes used to encode a native offset. |
| 776 | size_t native_offset_width_; |
| 777 | // Entries that are in use. |
| 778 | std::vector<bool> in_use_; |
| 779 | // The table we're building. |
| 780 | std::vector<uint8_t>* const table_; |
| 781 | }; |
| 782 | |
| 783 | static void createNativeGcMap(CompilationUnit* cUnit) { |
Bill Buzbee | a5b3024 | 2012-09-28 07:19:44 -0700 | [diff] [blame] | 784 | const std::vector<uint32_t>& mapping_table = cUnit->pc2dexMappingTable; |
Ian Rogers | 0c7abda | 2012-09-19 13:33:42 -0700 | [diff] [blame] | 785 | uint32_t max_native_offset = 0; |
| 786 | for (size_t i = 0; i < mapping_table.size(); i += 2) { |
| 787 | uint32_t native_offset = mapping_table[i + 0]; |
| 788 | if (native_offset > max_native_offset) { |
| 789 | max_native_offset = native_offset; |
| 790 | } |
| 791 | } |
| 792 | Compiler::MethodReference method_ref(cUnit->dex_file, cUnit->method_idx); |
| 793 | const std::vector<uint8_t>* gc_map_raw = verifier::MethodVerifier::GetDexGcMap(method_ref); |
| 794 | verifier::DexPcToReferenceMap dex_gc_map(&(*gc_map_raw)[4], gc_map_raw->size() - 4); |
| 795 | // Compute native offset to references size. |
| 796 | NativePcToReferenceMapBuilder native_gc_map_builder(&cUnit->nativeGcMap, |
| 797 | mapping_table.size() / 2, max_native_offset, |
| 798 | dex_gc_map.RegWidth()); |
| 799 | |
| 800 | for (size_t i = 0; i < mapping_table.size(); i += 2) { |
| 801 | uint32_t native_offset = mapping_table[i + 0]; |
| 802 | uint32_t dex_pc = mapping_table[i + 1]; |
| 803 | const uint8_t* references = dex_gc_map.FindBitMap(dex_pc, false); |
Bill Buzbee | a5b3024 | 2012-09-28 07:19:44 -0700 | [diff] [blame] | 804 | CHECK(references != NULL) << "Missing ref for dex pc 0x" << std::hex << dex_pc; |
| 805 | native_gc_map_builder.AddEntry(native_offset, references); |
Ian Rogers | 0c7abda | 2012-09-19 13:33:42 -0700 | [diff] [blame] | 806 | } |
| 807 | } |
| 808 | |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 809 | /* Determine the offset of each literal field */ |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 810 | int assignLiteralOffset(CompilationUnit* cUnit, int offset) |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 811 | { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 812 | offset = assignLiteralOffsetCommon(cUnit->literalList, offset); |
| 813 | offset = assignLiteralOffsetCommon(cUnit->codeLiteralList, offset); |
| 814 | offset = assignLiteralOffsetCommon(cUnit->methodLiteralList, offset); |
| 815 | return offset; |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 816 | } |
| 817 | |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 818 | int assignSwitchTablesOffset(CompilationUnit* cUnit, int offset) |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 819 | { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 820 | GrowableListIterator iterator; |
| 821 | oatGrowableListIteratorInit(&cUnit->switchTables, &iterator); |
| 822 | while (true) { |
buzbee | cbd6d44 | 2012-11-17 14:11:25 -0800 | [diff] [blame] | 823 | SwitchTable *tabRec = reinterpret_cast<SwitchTable*>(oatGrowableListIteratorNext(&iterator)); |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 824 | if (tabRec == NULL) break; |
| 825 | tabRec->offset = offset; |
| 826 | if (tabRec->table[0] == Instruction::kSparseSwitchSignature) { |
| 827 | offset += tabRec->table[1] * (sizeof(int) * 2); |
| 828 | } else { |
| 829 | DCHECK_EQ(static_cast<int>(tabRec->table[0]), |
| 830 | static_cast<int>(Instruction::kPackedSwitchSignature)); |
| 831 | offset += tabRec->table[1] * sizeof(int); |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 832 | } |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 833 | } |
| 834 | return offset; |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 835 | } |
| 836 | |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 837 | int assignFillArrayDataOffset(CompilationUnit* cUnit, int offset) |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 838 | { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 839 | GrowableListIterator iterator; |
| 840 | oatGrowableListIteratorInit(&cUnit->fillArrayData, &iterator); |
| 841 | while (true) { |
buzbee | cbd6d44 | 2012-11-17 14:11:25 -0800 | [diff] [blame] | 842 | FillArrayData *tabRec = |
| 843 | reinterpret_cast<FillArrayData*>(oatGrowableListIteratorNext(&iterator)); |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 844 | if (tabRec == NULL) break; |
| 845 | tabRec->offset = offset; |
| 846 | offset += tabRec->size; |
| 847 | // word align |
| 848 | offset = (offset + 3) & ~3; |
| 849 | } |
| 850 | return offset; |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 851 | } |
| 852 | |
| 853 | /* |
| 854 | * Walk the compilation unit and assign offsets to instructions |
| 855 | * and literals and compute the total size of the compiled unit. |
| 856 | */ |
| 857 | void oatAssignOffsets(CompilationUnit* cUnit) |
| 858 | { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 859 | int offset = oatAssignInsnOffsets(cUnit); |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 860 | |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 861 | /* Const values have to be word aligned */ |
| 862 | offset = (offset + 3) & ~3; |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 863 | |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 864 | /* Set up offsets for literals */ |
| 865 | cUnit->dataOffset = offset; |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 866 | |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 867 | offset = assignLiteralOffset(cUnit, offset); |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 868 | |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 869 | offset = assignSwitchTablesOffset(cUnit, offset); |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 870 | |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 871 | offset = assignFillArrayDataOffset(cUnit, offset); |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 872 | |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 873 | cUnit->totalSize = offset; |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 874 | } |
| 875 | |
| 876 | /* |
| 877 | * Go over each instruction in the list and calculate the offset from the top |
| 878 | * before sending them off to the assembler. If out-of-range branch distance is |
| 879 | * seen rearrange the instructions a bit to correct it. |
| 880 | */ |
| 881 | void oatAssembleLIR(CompilationUnit* cUnit) |
| 882 | { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 883 | oatAssignOffsets(cUnit); |
| 884 | /* |
| 885 | * Assemble here. Note that we generate code with optimistic assumptions |
| 886 | * and if found now to work, we'll have to redo the sequence and retry. |
| 887 | */ |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 888 | |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 889 | while (true) { |
| 890 | AssemblerStatus res = oatAssembleInstructions(cUnit, 0); |
| 891 | if (res == kSuccess) { |
| 892 | break; |
| 893 | } else { |
| 894 | cUnit->assemblerRetries++; |
| 895 | if (cUnit->assemblerRetries > MAX_ASSEMBLER_RETRIES) { |
| 896 | oatCodegenDump(cUnit); |
| 897 | LOG(FATAL) << "Assembler error - too many retries"; |
| 898 | } |
| 899 | // Redo offsets and try again |
| 900 | oatAssignOffsets(cUnit); |
| 901 | cUnit->codeBuffer.clear(); |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 902 | } |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 903 | } |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 904 | |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 905 | // Install literals |
| 906 | installLiteralPools(cUnit); |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 907 | |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 908 | // Install switch tables |
| 909 | installSwitchTables(cUnit); |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 910 | |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 911 | // Install fill array data |
| 912 | installFillArrayData(cUnit); |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 913 | |
Ian Rogers | 0c7abda | 2012-09-19 13:33:42 -0700 | [diff] [blame] | 914 | // Create the mapping table and native offset to reference map. |
Bill Buzbee | a5b3024 | 2012-09-28 07:19:44 -0700 | [diff] [blame] | 915 | createMappingTables(cUnit); |
Ian Rogers | 0c7abda | 2012-09-19 13:33:42 -0700 | [diff] [blame] | 916 | |
| 917 | createNativeGcMap(cUnit); |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 918 | } |
| 919 | |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 920 | /* |
| 921 | * Insert a kPseudoCaseLabel at the beginning of the Dalvik |
| 922 | * offset vaddr. This label will be used to fix up the case |
| 923 | * branch table during the assembly phase. Be sure to set |
| 924 | * all resource flags on this to prevent code motion across |
| 925 | * target boundaries. KeyVal is just there for debugging. |
| 926 | */ |
| 927 | LIR* insertCaseLabel(CompilationUnit* cUnit, int vaddr, int keyVal) |
| 928 | { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 929 | SafeMap<unsigned int, LIR*>::iterator it; |
| 930 | it = cUnit->boundaryMap.find(vaddr); |
| 931 | if (it == cUnit->boundaryMap.end()) { |
| 932 | LOG(FATAL) << "Error: didn't find vaddr 0x" << std::hex << vaddr; |
| 933 | } |
buzbee | cbd6d44 | 2012-11-17 14:11:25 -0800 | [diff] [blame] | 934 | LIR* newLabel = static_cast<LIR*>(oatNew(cUnit, sizeof(LIR), true, kAllocLIR)); |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 935 | newLabel->dalvikOffset = vaddr; |
| 936 | newLabel->opcode = kPseudoCaseLabel; |
| 937 | newLabel->operands[0] = keyVal; |
buzbee | cbd6d44 | 2012-11-17 14:11:25 -0800 | [diff] [blame] | 938 | oatInsertLIRAfter(it->second, newLabel); |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 939 | return newLabel; |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 940 | } |
| 941 | |
| 942 | void markPackedCaseLabels(CompilationUnit* cUnit, SwitchTable *tabRec) |
| 943 | { |
buzbee | eaf09bc | 2012-11-15 14:51:41 -0800 | [diff] [blame] | 944 | const uint16_t* table = tabRec->table; |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 945 | int baseVaddr = tabRec->vaddr; |
buzbee | cbd6d44 | 2012-11-17 14:11:25 -0800 | [diff] [blame] | 946 | const int *targets = reinterpret_cast<const int*>(&table[4]); |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 947 | int entries = table[1]; |
| 948 | int lowKey = s4FromSwitchData(&table[2]); |
| 949 | for (int i = 0; i < entries; i++) { |
buzbee | cbd6d44 | 2012-11-17 14:11:25 -0800 | [diff] [blame] | 950 | tabRec->targets[i] = insertCaseLabel(cUnit, baseVaddr + targets[i], i + lowKey); |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 951 | } |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 952 | } |
| 953 | |
| 954 | void markSparseCaseLabels(CompilationUnit* cUnit, SwitchTable *tabRec) |
| 955 | { |
buzbee | eaf09bc | 2012-11-15 14:51:41 -0800 | [diff] [blame] | 956 | const uint16_t* table = tabRec->table; |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 957 | int baseVaddr = tabRec->vaddr; |
| 958 | int entries = table[1]; |
buzbee | cbd6d44 | 2012-11-17 14:11:25 -0800 | [diff] [blame] | 959 | const int* keys = reinterpret_cast<const int*>(&table[2]); |
| 960 | const int* targets = &keys[entries]; |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 961 | for (int i = 0; i < entries; i++) { |
buzbee | cbd6d44 | 2012-11-17 14:11:25 -0800 | [diff] [blame] | 962 | tabRec->targets[i] = insertCaseLabel(cUnit, baseVaddr + targets[i], keys[i]); |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 963 | } |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 964 | } |
| 965 | |
| 966 | void oatProcessSwitchTables(CompilationUnit* cUnit) |
| 967 | { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 968 | GrowableListIterator iterator; |
| 969 | oatGrowableListIteratorInit(&cUnit->switchTables, &iterator); |
| 970 | while (true) { |
| 971 | SwitchTable *tabRec = |
buzbee | cbd6d44 | 2012-11-17 14:11:25 -0800 | [diff] [blame] | 972 | reinterpret_cast<SwitchTable*>(oatGrowableListIteratorNext(&iterator)); |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 973 | if (tabRec == NULL) break; |
| 974 | if (tabRec->table[0] == Instruction::kPackedSwitchSignature) { |
| 975 | markPackedCaseLabels(cUnit, tabRec); |
| 976 | } else if (tabRec->table[0] == Instruction::kSparseSwitchSignature) { |
| 977 | markSparseCaseLabels(cUnit, tabRec); |
| 978 | } else { |
| 979 | LOG(FATAL) << "Invalid switch table"; |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 980 | } |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 981 | } |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 982 | } |
| 983 | |
buzbee | eaf09bc | 2012-11-15 14:51:41 -0800 | [diff] [blame] | 984 | void dumpSparseSwitchTable(const uint16_t* table) |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 985 | /* |
| 986 | * Sparse switch data format: |
| 987 | * ushort ident = 0x0200 magic value |
| 988 | * ushort size number of entries in the table; > 0 |
| 989 | * int keys[size] keys, sorted low-to-high; 32-bit aligned |
| 990 | * int targets[size] branch targets, relative to switch opcode |
| 991 | * |
| 992 | * Total size is (2+size*4) 16-bit code units. |
| 993 | */ |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 994 | { |
buzbee | eaf09bc | 2012-11-15 14:51:41 -0800 | [diff] [blame] | 995 | uint16_t ident = table[0]; |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 996 | int entries = table[1]; |
buzbee | cbd6d44 | 2012-11-17 14:11:25 -0800 | [diff] [blame] | 997 | const int* keys = reinterpret_cast<const int*>(&table[2]); |
| 998 | const int* targets = &keys[entries]; |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 999 | LOG(INFO) << "Sparse switch table - ident:0x" << std::hex << ident |
| 1000 | << ", entries: " << std::dec << entries; |
| 1001 | for (int i = 0; i < entries; i++) { |
| 1002 | LOG(INFO) << " Key[" << keys[i] << "] -> 0x" << std::hex << targets[i]; |
| 1003 | } |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 1004 | } |
| 1005 | |
buzbee | eaf09bc | 2012-11-15 14:51:41 -0800 | [diff] [blame] | 1006 | void dumpPackedSwitchTable(const uint16_t* table) |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 1007 | /* |
| 1008 | * Packed switch data format: |
| 1009 | * ushort ident = 0x0100 magic value |
| 1010 | * ushort size number of entries in the table |
| 1011 | * int first_key first (and lowest) switch case value |
| 1012 | * int targets[size] branch targets, relative to switch opcode |
| 1013 | * |
| 1014 | * Total size is (4+size*2) 16-bit code units. |
| 1015 | */ |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 1016 | { |
buzbee | eaf09bc | 2012-11-15 14:51:41 -0800 | [diff] [blame] | 1017 | uint16_t ident = table[0]; |
buzbee | cbd6d44 | 2012-11-17 14:11:25 -0800 | [diff] [blame] | 1018 | const int* targets = reinterpret_cast<const int*>(&table[4]); |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 1019 | int entries = table[1]; |
| 1020 | int lowKey = s4FromSwitchData(&table[2]); |
| 1021 | LOG(INFO) << "Packed switch table - ident:0x" << std::hex << ident |
| 1022 | << ", entries: " << std::dec << entries << ", lowKey: " << lowKey; |
| 1023 | for (int i = 0; i < entries; i++) { |
| 1024 | LOG(INFO) << " Key[" << (i + lowKey) << "] -> 0x" << std::hex |
| 1025 | << targets[i]; |
| 1026 | } |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 1027 | } |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 1028 | |
buzbee | d1643e4 | 2012-09-05 14:06:51 -0700 | [diff] [blame] | 1029 | /* |
| 1030 | * Set up special LIR to mark a Dalvik byte-code instruction start and |
| 1031 | * record it in the boundaryMap. NOTE: in cases such as kMirOpCheck in |
| 1032 | * which we split a single Dalvik instruction, only the first MIR op |
| 1033 | * associated with a Dalvik PC should be entered into the map. |
| 1034 | */ |
| 1035 | LIR* markBoundary(CompilationUnit* cUnit, int offset, const char* instStr) |
| 1036 | { |
buzbee | cbd6d44 | 2012-11-17 14:11:25 -0800 | [diff] [blame] | 1037 | LIR* res = newLIR1(cUnit, kPseudoDalvikByteCodeBoundary, reinterpret_cast<uintptr_t>(instStr)); |
buzbee | d1643e4 | 2012-09-05 14:06:51 -0700 | [diff] [blame] | 1038 | if (cUnit->boundaryMap.find(offset) == cUnit->boundaryMap.end()) { |
| 1039 | cUnit->boundaryMap.Put(offset, res); |
| 1040 | } |
| 1041 | return res; |
| 1042 | } |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 1043 | |
buzbee | d1643e4 | 2012-09-05 14:06:51 -0700 | [diff] [blame] | 1044 | } |
| 1045 | // namespace art |