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