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