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