buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [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 | /* |
| 18 | * This file contains codegen for the Thumb2 ISA and is intended to be |
| 19 | * includes by: |
| 20 | * |
| 21 | * Codegen-$(TARGET_ARCH_VARIANT).c |
| 22 | * |
| 23 | */ |
| 24 | |
buzbee | ce30293 | 2011-10-04 14:32:18 -0700 | [diff] [blame] | 25 | #define SLOW_FIELD_PATH (cUnit->enableDebug & (1 << kDebugSlowFieldPath)) |
| 26 | #define SLOW_INVOKE_PATH (cUnit->enableDebug & (1 << kDebugSlowInvokePath)) |
| 27 | #define SLOW_STRING_PATH (cUnit->enableDebug & (1 << kDebugSlowStringPath)) |
| 28 | #define SLOW_TYPE_PATH (cUnit->enableDebug & (1 << kDebugSlowTypePath)) |
| 29 | #define EXERCISE_SLOWEST_FIELD_PATH (cUnit->enableDebug & \ |
| 30 | (1 << kDebugSlowestFieldPath)) |
| 31 | #define EXERCISE_SLOWEST_STRING_PATH (cUnit->enableDebug & \ |
| 32 | (1 << kDebugSlowestStringPath)) |
buzbee | 34c77ad | 2012-01-11 13:01:32 -0800 | [diff] [blame] | 33 | #define EXERCISE_RESOLVE_METHOD (cUnit->enableDebug & \ |
| 34 | (1 << kDebugExerciseResolveMethod)) |
buzbee | ce30293 | 2011-10-04 14:32:18 -0700 | [diff] [blame] | 35 | |
Elliott Hughes | 11d1b0c | 2012-01-23 16:57:47 -0800 | [diff] [blame] | 36 | namespace art { |
| 37 | |
buzbee | ce30293 | 2011-10-04 14:32:18 -0700 | [diff] [blame] | 38 | STATIC RegLocation getRetLoc(CompilationUnit* cUnit); |
buzbee | 34cd9e5 | 2011-09-08 14:31:52 -0700 | [diff] [blame] | 39 | |
Elliott Hughes | 81bc509 | 2011-09-30 17:25:59 -0700 | [diff] [blame] | 40 | void warnIfUnresolved(CompilationUnit* cUnit, int fieldIdx, Field* field) { |
| 41 | if (field == NULL) { |
Elliott Hughes | 11d1b0c | 2012-01-23 16:57:47 -0800 | [diff] [blame] | 42 | const DexFile::FieldId& field_id = cUnit->dex_file->GetFieldId(fieldIdx); |
Elliott Hughes | 9557241 | 2011-12-13 18:14:20 -0800 | [diff] [blame] | 43 | std::string class_name(cUnit->dex_file->GetFieldDeclaringClassDescriptor(field_id)); |
| 44 | std::string field_name(cUnit->dex_file->GetFieldName(field_id)); |
Elliott Hughes | 11d1b0c | 2012-01-23 16:57:47 -0800 | [diff] [blame] | 45 | LOG(INFO) << "Field " << PrettyDescriptor(class_name) << "." << field_name |
Elliott Hughes | 9557241 | 2011-12-13 18:14:20 -0800 | [diff] [blame] | 46 | << " unresolved at compile time"; |
Elliott Hughes | 81bc509 | 2011-09-30 17:25:59 -0700 | [diff] [blame] | 47 | } else { |
| 48 | // We also use the slow path for wide volatile fields. |
| 49 | } |
| 50 | } |
| 51 | |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 52 | /* |
| 53 | * Construct an s4 from two consecutive half-words of switch data. |
| 54 | * This needs to check endianness because the DEX optimizer only swaps |
| 55 | * half-words in instruction stream. |
| 56 | * |
| 57 | * "switchData" must be 32-bit aligned. |
| 58 | */ |
| 59 | #if __BYTE_ORDER == __LITTLE_ENDIAN |
buzbee | ed3e930 | 2011-09-23 17:34:19 -0700 | [diff] [blame] | 60 | STATIC inline s4 s4FromSwitchData(const void* switchData) { |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 61 | return *(s4*) switchData; |
| 62 | } |
| 63 | #else |
buzbee | ed3e930 | 2011-09-23 17:34:19 -0700 | [diff] [blame] | 64 | STATIC inline s4 s4FromSwitchData(const void* switchData) { |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 65 | u2* data = switchData; |
| 66 | return data[0] | (((s4) data[1]) << 16); |
| 67 | } |
| 68 | #endif |
| 69 | |
buzbee | ed3e930 | 2011-09-23 17:34:19 -0700 | [diff] [blame] | 70 | STATIC ArmLIR* callRuntimeHelper(CompilationUnit* cUnit, int reg) |
buzbee | ec5adf3 | 2011-09-11 15:25:43 -0700 | [diff] [blame] | 71 | { |
buzbee | 6181f79 | 2011-09-29 11:14:04 -0700 | [diff] [blame] | 72 | oatClobberCalleeSave(cUnit); |
buzbee | ec5adf3 | 2011-09-11 15:25:43 -0700 | [diff] [blame] | 73 | return opReg(cUnit, kOpBlx, reg); |
| 74 | } |
| 75 | |
buzbee | 1b4c859 | 2011-08-31 10:43:51 -0700 | [diff] [blame] | 76 | /* Generate unconditional branch instructions */ |
buzbee | ed3e930 | 2011-09-23 17:34:19 -0700 | [diff] [blame] | 77 | STATIC ArmLIR* genUnconditionalBranch(CompilationUnit* cUnit, ArmLIR* target) |
buzbee | 1b4c859 | 2011-08-31 10:43:51 -0700 | [diff] [blame] | 78 | { |
| 79 | ArmLIR* branch = opNone(cUnit, kOpUncondBr); |
| 80 | branch->generic.target = (LIR*) target; |
| 81 | return branch; |
| 82 | } |
| 83 | |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 84 | /* |
| 85 | * Generate a Thumb2 IT instruction, which can nullify up to |
| 86 | * four subsequent instructions based on a condition and its |
| 87 | * inverse. The condition applies to the first instruction, which |
| 88 | * is executed if the condition is met. The string "guide" consists |
| 89 | * of 0 to 3 chars, and applies to the 2nd through 4th instruction. |
| 90 | * A "T" means the instruction is executed if the condition is |
| 91 | * met, and an "E" means the instruction is executed if the condition |
| 92 | * is not met. |
| 93 | */ |
buzbee | ed3e930 | 2011-09-23 17:34:19 -0700 | [diff] [blame] | 94 | STATIC ArmLIR* genIT(CompilationUnit* cUnit, ArmConditionCode code, |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 95 | const char* guide) |
| 96 | { |
| 97 | int mask; |
| 98 | int condBit = code & 1; |
| 99 | int altBit = condBit ^ 1; |
| 100 | int mask3 = 0; |
| 101 | int mask2 = 0; |
| 102 | int mask1 = 0; |
| 103 | |
| 104 | //Note: case fallthroughs intentional |
| 105 | switch(strlen(guide)) { |
| 106 | case 3: |
| 107 | mask1 = (guide[2] == 'T') ? condBit : altBit; |
| 108 | case 2: |
| 109 | mask2 = (guide[1] == 'T') ? condBit : altBit; |
| 110 | case 1: |
| 111 | mask3 = (guide[0] == 'T') ? condBit : altBit; |
| 112 | break; |
| 113 | case 0: |
| 114 | break; |
| 115 | default: |
| 116 | LOG(FATAL) << "OAT: bad case in genIT"; |
| 117 | } |
| 118 | mask = (mask3 << 3) | (mask2 << 2) | (mask1 << 1) | |
| 119 | (1 << (3 - strlen(guide))); |
| 120 | return newLIR2(cUnit, kThumb2It, code, mask); |
| 121 | } |
| 122 | |
| 123 | /* |
| 124 | * Insert a kArmPseudoCaseLabel at the beginning of the Dalvik |
| 125 | * offset vaddr. This label will be used to fix up the case |
| 126 | * branch table during the assembly phase. Be sure to set |
| 127 | * all resource flags on this to prevent code motion across |
| 128 | * target boundaries. KeyVal is just there for debugging. |
| 129 | */ |
buzbee | ed3e930 | 2011-09-23 17:34:19 -0700 | [diff] [blame] | 130 | STATIC ArmLIR* insertCaseLabel(CompilationUnit* cUnit, int vaddr, int keyVal) |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 131 | { |
buzbee | 85d8c1e | 2012-01-27 15:52:35 -0800 | [diff] [blame] | 132 | std::map<unsigned int, LIR*>::iterator it; |
| 133 | it = cUnit->boundaryMap.find(vaddr); |
| 134 | if (it == cUnit->boundaryMap.end()) { |
| 135 | LOG(FATAL) << "Error: didn't find vaddr 0x" << std::hex << vaddr; |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 136 | } |
buzbee | 85d8c1e | 2012-01-27 15:52:35 -0800 | [diff] [blame] | 137 | ArmLIR* newLabel = (ArmLIR*)oatNew(sizeof(ArmLIR), true); |
| 138 | newLabel->generic.dalvikOffset = vaddr; |
| 139 | newLabel->opcode = kArmPseudoCaseLabel; |
| 140 | newLabel->operands[0] = keyVal; |
| 141 | oatInsertLIRAfter(it->second, (LIR*)newLabel); |
| 142 | return newLabel; |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 143 | } |
| 144 | |
buzbee | ed3e930 | 2011-09-23 17:34:19 -0700 | [diff] [blame] | 145 | STATIC void markPackedCaseLabels(CompilationUnit* cUnit, SwitchTable *tabRec) |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 146 | { |
| 147 | const u2* table = tabRec->table; |
| 148 | int baseVaddr = tabRec->vaddr; |
| 149 | int *targets = (int*)&table[4]; |
| 150 | int entries = table[1]; |
| 151 | int lowKey = s4FromSwitchData(&table[2]); |
| 152 | for (int i = 0; i < entries; i++) { |
| 153 | tabRec->targets[i] = insertCaseLabel(cUnit, baseVaddr + targets[i], |
| 154 | i + lowKey); |
| 155 | } |
| 156 | } |
| 157 | |
buzbee | ed3e930 | 2011-09-23 17:34:19 -0700 | [diff] [blame] | 158 | STATIC void markSparseCaseLabels(CompilationUnit* cUnit, SwitchTable *tabRec) |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 159 | { |
| 160 | const u2* table = tabRec->table; |
| 161 | int baseVaddr = tabRec->vaddr; |
| 162 | int entries = table[1]; |
| 163 | int* keys = (int*)&table[2]; |
| 164 | int* targets = &keys[entries]; |
| 165 | for (int i = 0; i < entries; i++) { |
| 166 | tabRec->targets[i] = insertCaseLabel(cUnit, baseVaddr + targets[i], |
| 167 | keys[i]); |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | void oatProcessSwitchTables(CompilationUnit* cUnit) |
| 172 | { |
| 173 | GrowableListIterator iterator; |
| 174 | oatGrowableListIteratorInit(&cUnit->switchTables, &iterator); |
| 175 | while (true) { |
| 176 | SwitchTable *tabRec = (SwitchTable *) oatGrowableListIteratorNext( |
| 177 | &iterator); |
| 178 | if (tabRec == NULL) break; |
| 179 | if (tabRec->table[0] == kPackedSwitchSignature) |
| 180 | markPackedCaseLabels(cUnit, tabRec); |
| 181 | else if (tabRec->table[0] == kSparseSwitchSignature) |
| 182 | markSparseCaseLabels(cUnit, tabRec); |
| 183 | else { |
| 184 | LOG(FATAL) << "Invalid switch table"; |
| 185 | } |
| 186 | } |
| 187 | } |
| 188 | |
buzbee | ed3e930 | 2011-09-23 17:34:19 -0700 | [diff] [blame] | 189 | STATIC void dumpSparseSwitchTable(const u2* table) |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 190 | /* |
| 191 | * Sparse switch data format: |
| 192 | * ushort ident = 0x0200 magic value |
| 193 | * ushort size number of entries in the table; > 0 |
| 194 | * int keys[size] keys, sorted low-to-high; 32-bit aligned |
| 195 | * int targets[size] branch targets, relative to switch opcode |
| 196 | * |
| 197 | * Total size is (2+size*4) 16-bit code units. |
| 198 | */ |
| 199 | { |
| 200 | u2 ident = table[0]; |
| 201 | int entries = table[1]; |
| 202 | int* keys = (int*)&table[2]; |
| 203 | int* targets = &keys[entries]; |
| 204 | LOG(INFO) << "Sparse switch table - ident:0x" << std::hex << ident << |
| 205 | ", entries: " << std::dec << entries; |
| 206 | for (int i = 0; i < entries; i++) { |
| 207 | LOG(INFO) << " Key[" << keys[i] << "] -> 0x" << std::hex << |
| 208 | targets[i]; |
| 209 | } |
| 210 | } |
| 211 | |
buzbee | ed3e930 | 2011-09-23 17:34:19 -0700 | [diff] [blame] | 212 | STATIC void dumpPackedSwitchTable(const u2* table) |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 213 | /* |
| 214 | * Packed switch data format: |
| 215 | * ushort ident = 0x0100 magic value |
| 216 | * ushort size number of entries in the table |
| 217 | * int first_key first (and lowest) switch case value |
| 218 | * int targets[size] branch targets, relative to switch opcode |
| 219 | * |
| 220 | * Total size is (4+size*2) 16-bit code units. |
| 221 | */ |
| 222 | { |
| 223 | u2 ident = table[0]; |
| 224 | int* targets = (int*)&table[4]; |
| 225 | int entries = table[1]; |
| 226 | int lowKey = s4FromSwitchData(&table[2]); |
| 227 | LOG(INFO) << "Packed switch table - ident:0x" << std::hex << ident << |
| 228 | ", entries: " << std::dec << entries << ", lowKey: " << lowKey; |
| 229 | for (int i = 0; i < entries; i++) { |
| 230 | LOG(INFO) << " Key[" << (i + lowKey) << "] -> 0x" << std::hex << |
| 231 | targets[i]; |
| 232 | } |
| 233 | } |
| 234 | |
| 235 | /* |
| 236 | * The sparse table in the literal pool is an array of <key,displacement> |
| 237 | * pairs. For each set, we'll load them as a pair using ldmia. |
| 238 | * This means that the register number of the temp we use for the key |
| 239 | * must be lower than the reg for the displacement. |
| 240 | * |
| 241 | * The test loop will look something like: |
| 242 | * |
| 243 | * adr rBase, <table> |
| 244 | * ldr rVal, [rSP, vRegOff] |
| 245 | * mov rIdx, #tableSize |
| 246 | * lp: |
| 247 | * ldmia rBase!, {rKey, rDisp} |
| 248 | * sub rIdx, #1 |
| 249 | * cmp rVal, rKey |
| 250 | * ifeq |
| 251 | * add rPC, rDisp ; This is the branch from which we compute displacement |
| 252 | * cbnz rIdx, lp |
| 253 | */ |
buzbee | ed3e930 | 2011-09-23 17:34:19 -0700 | [diff] [blame] | 254 | STATIC void genSparseSwitch(CompilationUnit* cUnit, MIR* mir, |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 255 | RegLocation rlSrc) |
| 256 | { |
| 257 | const u2* table = cUnit->insns + mir->offset + mir->dalvikInsn.vB; |
| 258 | if (cUnit->printMe) { |
| 259 | dumpSparseSwitchTable(table); |
| 260 | } |
| 261 | // Add the table to the list - we'll process it later |
| 262 | SwitchTable *tabRec = (SwitchTable *)oatNew(sizeof(SwitchTable), |
| 263 | true); |
| 264 | tabRec->table = table; |
| 265 | tabRec->vaddr = mir->offset; |
| 266 | int size = table[1]; |
| 267 | tabRec->targets = (ArmLIR* *)oatNew(size * sizeof(ArmLIR*), true); |
| 268 | oatInsertGrowableList(&cUnit->switchTables, (intptr_t)tabRec); |
| 269 | |
| 270 | // Get the switch value |
| 271 | rlSrc = loadValue(cUnit, rlSrc, kCoreReg); |
| 272 | int rBase = oatAllocTemp(cUnit); |
| 273 | /* Allocate key and disp temps */ |
| 274 | int rKey = oatAllocTemp(cUnit); |
| 275 | int rDisp = oatAllocTemp(cUnit); |
| 276 | // Make sure rKey's register number is less than rDisp's number for ldmia |
| 277 | if (rKey > rDisp) { |
| 278 | int tmp = rDisp; |
| 279 | rDisp = rKey; |
| 280 | rKey = tmp; |
| 281 | } |
| 282 | // Materialize a pointer to the switch table |
buzbee | 03fa263 | 2011-09-20 17:10:57 -0700 | [diff] [blame] | 283 | newLIR3(cUnit, kThumb2Adr, rBase, 0, (intptr_t)tabRec); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 284 | // Set up rIdx |
| 285 | int rIdx = oatAllocTemp(cUnit); |
| 286 | loadConstant(cUnit, rIdx, size); |
| 287 | // Establish loop branch target |
| 288 | ArmLIR* target = newLIR0(cUnit, kArmPseudoTargetLabel); |
| 289 | target->defMask = ENCODE_ALL; |
| 290 | // Load next key/disp |
| 291 | newLIR2(cUnit, kThumb2LdmiaWB, rBase, (1 << rKey) | (1 << rDisp)); |
| 292 | opRegReg(cUnit, kOpCmp, rKey, rlSrc.lowReg); |
| 293 | // Go if match. NOTE: No instruction set switch here - must stay Thumb2 |
| 294 | genIT(cUnit, kArmCondEq, ""); |
| 295 | ArmLIR* switchBranch = newLIR1(cUnit, kThumb2AddPCR, rDisp); |
| 296 | tabRec->bxInst = switchBranch; |
| 297 | // Needs to use setflags encoding here |
| 298 | newLIR3(cUnit, kThumb2SubsRRI12, rIdx, rIdx, 1); |
| 299 | ArmLIR* branch = opCondBranch(cUnit, kArmCondNe); |
| 300 | branch->generic.target = (LIR*)target; |
| 301 | } |
| 302 | |
| 303 | |
buzbee | ed3e930 | 2011-09-23 17:34:19 -0700 | [diff] [blame] | 304 | STATIC void genPackedSwitch(CompilationUnit* cUnit, MIR* mir, |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 305 | RegLocation rlSrc) |
| 306 | { |
| 307 | const u2* table = cUnit->insns + mir->offset + mir->dalvikInsn.vB; |
| 308 | if (cUnit->printMe) { |
| 309 | dumpPackedSwitchTable(table); |
| 310 | } |
| 311 | // Add the table to the list - we'll process it later |
| 312 | SwitchTable *tabRec = (SwitchTable *)oatNew(sizeof(SwitchTable), |
| 313 | true); |
| 314 | tabRec->table = table; |
| 315 | tabRec->vaddr = mir->offset; |
| 316 | int size = table[1]; |
| 317 | tabRec->targets = (ArmLIR* *)oatNew(size * sizeof(ArmLIR*), true); |
| 318 | oatInsertGrowableList(&cUnit->switchTables, (intptr_t)tabRec); |
| 319 | |
| 320 | // Get the switch value |
| 321 | rlSrc = loadValue(cUnit, rlSrc, kCoreReg); |
| 322 | int tableBase = oatAllocTemp(cUnit); |
| 323 | // Materialize a pointer to the switch table |
buzbee | 03fa263 | 2011-09-20 17:10:57 -0700 | [diff] [blame] | 324 | newLIR3(cUnit, kThumb2Adr, tableBase, 0, (intptr_t)tabRec); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 325 | int lowKey = s4FromSwitchData(&table[2]); |
| 326 | int keyReg; |
| 327 | // Remove the bias, if necessary |
| 328 | if (lowKey == 0) { |
| 329 | keyReg = rlSrc.lowReg; |
| 330 | } else { |
| 331 | keyReg = oatAllocTemp(cUnit); |
| 332 | opRegRegImm(cUnit, kOpSub, keyReg, rlSrc.lowReg, lowKey); |
| 333 | } |
| 334 | // Bounds check - if < 0 or >= size continue following switch |
| 335 | opRegImm(cUnit, kOpCmp, keyReg, size-1); |
| 336 | ArmLIR* branchOver = opCondBranch(cUnit, kArmCondHi); |
| 337 | |
| 338 | // Load the displacement from the switch table |
| 339 | int dispReg = oatAllocTemp(cUnit); |
| 340 | loadBaseIndexed(cUnit, tableBase, keyReg, dispReg, 2, kWord); |
| 341 | |
| 342 | // ..and go! NOTE: No instruction set switch here - must stay Thumb2 |
| 343 | ArmLIR* switchBranch = newLIR1(cUnit, kThumb2AddPCR, dispReg); |
| 344 | tabRec->bxInst = switchBranch; |
| 345 | |
| 346 | /* branchOver target here */ |
| 347 | ArmLIR* target = newLIR0(cUnit, kArmPseudoTargetLabel); |
| 348 | target->defMask = ENCODE_ALL; |
| 349 | branchOver->generic.target = (LIR*)target; |
| 350 | } |
| 351 | |
| 352 | /* |
| 353 | * Array data table format: |
| 354 | * ushort ident = 0x0300 magic value |
| 355 | * ushort width width of each element in the table |
| 356 | * uint size number of elements in the table |
| 357 | * ubyte data[size*width] table of data values (may contain a single-byte |
| 358 | * padding at the end) |
| 359 | * |
| 360 | * Total size is 4+(width * size + 1)/2 16-bit code units. |
| 361 | */ |
buzbee | ed3e930 | 2011-09-23 17:34:19 -0700 | [diff] [blame] | 362 | STATIC void genFillArrayData(CompilationUnit* cUnit, MIR* mir, |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 363 | RegLocation rlSrc) |
| 364 | { |
| 365 | const u2* table = cUnit->insns + mir->offset + mir->dalvikInsn.vB; |
| 366 | // Add the table to the list - we'll process it later |
| 367 | FillArrayData *tabRec = (FillArrayData *) |
| 368 | oatNew(sizeof(FillArrayData), true); |
| 369 | tabRec->table = table; |
| 370 | tabRec->vaddr = mir->offset; |
| 371 | u2 width = tabRec->table[1]; |
| 372 | u4 size = tabRec->table[2] | (((u4)tabRec->table[3]) << 16); |
| 373 | tabRec->size = (size * width) + 8; |
| 374 | |
| 375 | oatInsertGrowableList(&cUnit->fillArrayData, (intptr_t)tabRec); |
| 376 | |
| 377 | // Making a call - use explicit registers |
| 378 | oatFlushAllRegs(cUnit); /* Everything to home location */ |
| 379 | loadValueDirectFixed(cUnit, rlSrc, r0); |
| 380 | loadWordDisp(cUnit, rSELF, |
buzbee | 1b4c859 | 2011-08-31 10:43:51 -0700 | [diff] [blame] | 381 | OFFSETOF_MEMBER(Thread, pHandleFillArrayDataFromCode), rLR); |
buzbee | e6d6196 | 2011-08-27 11:58:19 -0700 | [diff] [blame] | 382 | // Materialize a pointer to the fill data image |
buzbee | 03fa263 | 2011-09-20 17:10:57 -0700 | [diff] [blame] | 383 | newLIR3(cUnit, kThumb2Adr, r1, 0, (intptr_t)tabRec); |
Ian Rogers | ff1ed47 | 2011-09-20 13:46:24 -0700 | [diff] [blame] | 384 | callRuntimeHelper(cUnit, rLR); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 385 | } |
| 386 | |
| 387 | /* |
| 388 | * Mark garbage collection card. Skip if the value we're storing is null. |
| 389 | */ |
buzbee | ed3e930 | 2011-09-23 17:34:19 -0700 | [diff] [blame] | 390 | STATIC void markGCCard(CompilationUnit* cUnit, int valReg, int tgtAddrReg) |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 391 | { |
| 392 | int regCardBase = oatAllocTemp(cUnit); |
| 393 | int regCardNo = oatAllocTemp(cUnit); |
| 394 | ArmLIR* branchOver = genCmpImmBranch(cUnit, kArmCondEq, valReg, 0); |
buzbee | c143c55 | 2011-08-20 17:38:58 -0700 | [diff] [blame] | 395 | loadWordDisp(cUnit, rSELF, Thread::CardTableOffset().Int32Value(), |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 396 | regCardBase); |
| 397 | opRegRegImm(cUnit, kOpLsr, regCardNo, tgtAddrReg, GC_CARD_SHIFT); |
| 398 | storeBaseIndexed(cUnit, regCardBase, regCardNo, regCardBase, 0, |
| 399 | kUnsignedByte); |
| 400 | ArmLIR* target = newLIR0(cUnit, kArmPseudoTargetLabel); |
| 401 | target->defMask = ENCODE_ALL; |
| 402 | branchOver->generic.target = (LIR*)target; |
| 403 | oatFreeTemp(cUnit, regCardBase); |
| 404 | oatFreeTemp(cUnit, regCardNo); |
| 405 | } |
| 406 | |
buzbee | 34cd9e5 | 2011-09-08 14:31:52 -0700 | [diff] [blame] | 407 | /* |
| 408 | * Helper function for Iget/put when field not resolved at compile time. |
| 409 | * Will trash call temps and return with the field offset in r0. |
| 410 | */ |
Elliott Hughes | 81bc509 | 2011-09-30 17:25:59 -0700 | [diff] [blame] | 411 | STATIC void getFieldOffset(CompilationUnit* cUnit, MIR* mir, Field* fieldPtr) |
buzbee | 34cd9e5 | 2011-09-08 14:31:52 -0700 | [diff] [blame] | 412 | { |
| 413 | int fieldIdx = mir->dalvikInsn.vC; |
buzbee | 6181f79 | 2011-09-29 11:14:04 -0700 | [diff] [blame] | 414 | oatFlushAllRegs(cUnit); |
Elliott Hughes | 81bc509 | 2011-09-30 17:25:59 -0700 | [diff] [blame] | 415 | warnIfUnresolved(cUnit, fieldIdx, fieldPtr); |
buzbee | 34cd9e5 | 2011-09-08 14:31:52 -0700 | [diff] [blame] | 416 | oatLockCallTemps(cUnit); // Explicit register usage |
| 417 | loadCurrMethodDirect(cUnit, r1); // arg1 <= Method* |
| 418 | loadWordDisp(cUnit, r1, |
| 419 | Method::DexCacheResolvedFieldsOffset().Int32Value(), r0); |
Elliott Hughes | 11d1b0c | 2012-01-23 16:57:47 -0800 | [diff] [blame] | 420 | loadWordDisp(cUnit, r0, Array::DataOffset().Int32Value() + |
buzbee | 34cd9e5 | 2011-09-08 14:31:52 -0700 | [diff] [blame] | 421 | sizeof(int32_t*)* fieldIdx, r0); |
| 422 | /* |
| 423 | * For testing, omit the test for run-time resolution. This will |
| 424 | * force all accesses to go through the runtime resolution path. |
| 425 | */ |
buzbee | ce30293 | 2011-10-04 14:32:18 -0700 | [diff] [blame] | 426 | ArmLIR* branchOver = NULL; |
| 427 | if (!EXERCISE_SLOWEST_FIELD_PATH) { |
| 428 | branchOver = genCmpImmBranch(cUnit, kArmCondNe, r0, 0); |
| 429 | } |
buzbee | 34cd9e5 | 2011-09-08 14:31:52 -0700 | [diff] [blame] | 430 | // Resolve |
| 431 | loadWordDisp(cUnit, rSELF, |
Brian Carlstrom | 845490b | 2011-09-19 15:56:53 -0700 | [diff] [blame] | 432 | OFFSETOF_MEMBER(Thread, pFindInstanceFieldFromCode), rLR); |
buzbee | 34cd9e5 | 2011-09-08 14:31:52 -0700 | [diff] [blame] | 433 | loadConstant(cUnit, r0, fieldIdx); |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 434 | callRuntimeHelper(cUnit, rLR); // FindInstanceFieldFromCoderesolveTypeFromCode(idx, method) |
buzbee | 34cd9e5 | 2011-09-08 14:31:52 -0700 | [diff] [blame] | 435 | ArmLIR* target = newLIR0(cUnit, kArmPseudoTargetLabel); |
| 436 | target->defMask = ENCODE_ALL; |
buzbee | ce30293 | 2011-10-04 14:32:18 -0700 | [diff] [blame] | 437 | if (!EXERCISE_SLOWEST_FIELD_PATH) { |
| 438 | branchOver->generic.target = (LIR*)target; |
| 439 | } |
buzbee | 34cd9e5 | 2011-09-08 14:31:52 -0700 | [diff] [blame] | 440 | // Free temps (except for r0) |
| 441 | oatFreeTemp(cUnit, r1); |
| 442 | oatFreeTemp(cUnit, r2); |
| 443 | oatFreeTemp(cUnit, r3); |
Elliott Hughes | 11d1b0c | 2012-01-23 16:57:47 -0800 | [diff] [blame] | 444 | loadWordDisp(cUnit, r0, Field::OffsetOffset().Int32Value(), r0); |
buzbee | 34cd9e5 | 2011-09-08 14:31:52 -0700 | [diff] [blame] | 445 | } |
| 446 | |
buzbee | ed3e930 | 2011-09-23 17:34:19 -0700 | [diff] [blame] | 447 | STATIC void genIGet(CompilationUnit* cUnit, MIR* mir, OpSize size, |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 448 | RegLocation rlDest, RegLocation rlObj) |
| 449 | { |
Ian Rogers | a3760aa | 2011-11-14 14:32:37 -0800 | [diff] [blame] | 450 | Field* fieldPtr = cUnit->dex_cache->GetResolvedField(mir->dalvikInsn.vC); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 451 | RegLocation rlResult; |
| 452 | RegisterClass regClass = oatRegClassBySize(size); |
buzbee | 34cd9e5 | 2011-09-08 14:31:52 -0700 | [diff] [blame] | 453 | if (SLOW_FIELD_PATH || fieldPtr == NULL) { |
Elliott Hughes | 81bc509 | 2011-09-30 17:25:59 -0700 | [diff] [blame] | 454 | getFieldOffset(cUnit, mir, fieldPtr); |
buzbee | 34cd9e5 | 2011-09-08 14:31:52 -0700 | [diff] [blame] | 455 | // Field offset in r0 |
| 456 | rlObj = loadValue(cUnit, rlObj, kCoreReg); |
| 457 | rlResult = oatEvalLoc(cUnit, rlDest, regClass, true); |
buzbee | 5ade1d2 | 2011-09-09 14:44:52 -0700 | [diff] [blame] | 458 | genNullCheck(cUnit, rlObj.sRegLow, rlObj.lowReg, mir);/* null object? */ |
buzbee | 58f9274 | 2011-10-01 11:22:17 -0700 | [diff] [blame] | 459 | loadBaseIndexed(cUnit, rlObj.lowReg, r0, rlResult.lowReg, 0, kWord); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 460 | oatGenMemBarrier(cUnit, kSY); |
buzbee | 34cd9e5 | 2011-09-08 14:31:52 -0700 | [diff] [blame] | 461 | storeValue(cUnit, rlDest, rlResult); |
| 462 | } else { |
| 463 | #if ANDROID_SMP != 0 |
Elliott Hughes | 1d3f114 | 2011-09-13 12:00:00 -0700 | [diff] [blame] | 464 | bool isVolatile = fieldPtr->IsVolatile(); |
buzbee | 34cd9e5 | 2011-09-08 14:31:52 -0700 | [diff] [blame] | 465 | #else |
| 466 | bool isVolatile = false; |
| 467 | #endif |
| 468 | int fieldOffset = fieldPtr->GetOffset().Int32Value(); |
| 469 | rlObj = loadValue(cUnit, rlObj, kCoreReg); |
| 470 | rlResult = oatEvalLoc(cUnit, rlDest, regClass, true); |
buzbee | 5ade1d2 | 2011-09-09 14:44:52 -0700 | [diff] [blame] | 471 | genNullCheck(cUnit, rlObj.sRegLow, rlObj.lowReg, mir);/* null object? */ |
buzbee | 34cd9e5 | 2011-09-08 14:31:52 -0700 | [diff] [blame] | 472 | loadBaseDisp(cUnit, mir, rlObj.lowReg, fieldOffset, rlResult.lowReg, |
buzbee | 58f9274 | 2011-10-01 11:22:17 -0700 | [diff] [blame] | 473 | kWord, rlObj.sRegLow); |
buzbee | 34cd9e5 | 2011-09-08 14:31:52 -0700 | [diff] [blame] | 474 | if (isVolatile) { |
| 475 | oatGenMemBarrier(cUnit, kSY); |
| 476 | } |
| 477 | storeValue(cUnit, rlDest, rlResult); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 478 | } |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 479 | } |
| 480 | |
buzbee | ed3e930 | 2011-09-23 17:34:19 -0700 | [diff] [blame] | 481 | STATIC void genIPut(CompilationUnit* cUnit, MIR* mir, OpSize size, |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 482 | RegLocation rlSrc, RegLocation rlObj, bool isObject) |
| 483 | { |
Ian Rogers | a3760aa | 2011-11-14 14:32:37 -0800 | [diff] [blame] | 484 | Field* fieldPtr = cUnit->dex_cache->GetResolvedField(mir->dalvikInsn.vC); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 485 | RegisterClass regClass = oatRegClassBySize(size); |
buzbee | 34cd9e5 | 2011-09-08 14:31:52 -0700 | [diff] [blame] | 486 | if (SLOW_FIELD_PATH || fieldPtr == NULL) { |
Elliott Hughes | 81bc509 | 2011-09-30 17:25:59 -0700 | [diff] [blame] | 487 | getFieldOffset(cUnit, mir, fieldPtr); |
buzbee | 34cd9e5 | 2011-09-08 14:31:52 -0700 | [diff] [blame] | 488 | // Field offset in r0 |
| 489 | rlObj = loadValue(cUnit, rlObj, kCoreReg); |
| 490 | rlSrc = loadValue(cUnit, rlSrc, regClass); |
buzbee | 5ade1d2 | 2011-09-09 14:44:52 -0700 | [diff] [blame] | 491 | genNullCheck(cUnit, rlObj.sRegLow, rlObj.lowReg, mir);/* null object? */ |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 492 | oatGenMemBarrier(cUnit, kSY); |
buzbee | 58f9274 | 2011-10-01 11:22:17 -0700 | [diff] [blame] | 493 | storeBaseIndexed(cUnit, rlObj.lowReg, r0, rlSrc.lowReg, 0, kWord); |
buzbee | 34cd9e5 | 2011-09-08 14:31:52 -0700 | [diff] [blame] | 494 | } else { |
| 495 | #if ANDROID_SMP != 0 |
Elliott Hughes | 1d3f114 | 2011-09-13 12:00:00 -0700 | [diff] [blame] | 496 | bool isVolatile = fieldPtr->IsVolatile(); |
buzbee | 34cd9e5 | 2011-09-08 14:31:52 -0700 | [diff] [blame] | 497 | #else |
| 498 | bool isVolatile = false; |
| 499 | #endif |
| 500 | int fieldOffset = fieldPtr->GetOffset().Int32Value(); |
| 501 | rlObj = loadValue(cUnit, rlObj, kCoreReg); |
| 502 | rlSrc = loadValue(cUnit, rlSrc, regClass); |
buzbee | 5ade1d2 | 2011-09-09 14:44:52 -0700 | [diff] [blame] | 503 | genNullCheck(cUnit, rlObj.sRegLow, rlObj.lowReg, mir);/* null obj? */ |
buzbee | 34cd9e5 | 2011-09-08 14:31:52 -0700 | [diff] [blame] | 504 | |
| 505 | if (isVolatile) { |
buzbee | 12246b8 | 2011-09-29 14:15:05 -0700 | [diff] [blame] | 506 | oatGenMemBarrier(cUnit, kST); |
buzbee | 34cd9e5 | 2011-09-08 14:31:52 -0700 | [diff] [blame] | 507 | } |
buzbee | 58f9274 | 2011-10-01 11:22:17 -0700 | [diff] [blame] | 508 | storeBaseDisp(cUnit, rlObj.lowReg, fieldOffset, rlSrc.lowReg, kWord); |
buzbee | 12246b8 | 2011-09-29 14:15:05 -0700 | [diff] [blame] | 509 | if (isVolatile) { |
| 510 | oatGenMemBarrier(cUnit, kSY); |
| 511 | } |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 512 | } |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 513 | if (isObject) { |
| 514 | /* NOTE: marking card based on object head */ |
| 515 | markGCCard(cUnit, rlSrc.lowReg, rlObj.lowReg); |
| 516 | } |
| 517 | } |
| 518 | |
buzbee | ed3e930 | 2011-09-23 17:34:19 -0700 | [diff] [blame] | 519 | STATIC void genIGetWide(CompilationUnit* cUnit, MIR* mir, RegLocation rlDest, |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 520 | RegLocation rlObj) |
| 521 | { |
buzbee | 12246b8 | 2011-09-29 14:15:05 -0700 | [diff] [blame] | 522 | RegLocation rlResult; |
Ian Rogers | a3760aa | 2011-11-14 14:32:37 -0800 | [diff] [blame] | 523 | Field* fieldPtr = cUnit->dex_cache->GetResolvedField(mir->dalvikInsn.vC); |
buzbee | 12246b8 | 2011-09-29 14:15:05 -0700 | [diff] [blame] | 524 | #if ANDROID_SMP != 0 |
| 525 | bool isVolatile = (fieldPtr == NULL) || fieldPtr->IsVolatile(); |
| 526 | #else |
| 527 | bool isVolatile = false; |
| 528 | #endif |
buzbee | ce30293 | 2011-10-04 14:32:18 -0700 | [diff] [blame] | 529 | if (SLOW_FIELD_PATH || (fieldPtr == NULL) || isVolatile) { |
Elliott Hughes | 81bc509 | 2011-09-30 17:25:59 -0700 | [diff] [blame] | 530 | getFieldOffset(cUnit, mir, fieldPtr); |
buzbee | 34cd9e5 | 2011-09-08 14:31:52 -0700 | [diff] [blame] | 531 | // Field offset in r0 |
| 532 | rlObj = loadValue(cUnit, rlObj, kCoreReg); |
| 533 | rlResult = oatEvalLoc(cUnit, rlDest, kAnyReg, true); |
buzbee | 5ade1d2 | 2011-09-09 14:44:52 -0700 | [diff] [blame] | 534 | genNullCheck(cUnit, rlObj.sRegLow, rlObj.lowReg, mir);/* null obj? */ |
buzbee | 34cd9e5 | 2011-09-08 14:31:52 -0700 | [diff] [blame] | 535 | opRegReg(cUnit, kOpAdd, r0, rlObj.lowReg); |
| 536 | loadPair(cUnit, r0, rlResult.lowReg, rlResult.highReg); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 537 | oatGenMemBarrier(cUnit, kSY); |
buzbee | 12246b8 | 2011-09-29 14:15:05 -0700 | [diff] [blame] | 538 | storeValueWide(cUnit, rlDest, rlResult); |
buzbee | 34cd9e5 | 2011-09-08 14:31:52 -0700 | [diff] [blame] | 539 | } else { |
buzbee | 34cd9e5 | 2011-09-08 14:31:52 -0700 | [diff] [blame] | 540 | int fieldOffset = fieldPtr->GetOffset().Int32Value(); |
| 541 | rlObj = loadValue(cUnit, rlObj, kCoreReg); |
| 542 | int regPtr = oatAllocTemp(cUnit); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 543 | |
buzbee | ed3e930 | 2011-09-23 17:34:19 -0700 | [diff] [blame] | 544 | DCHECK(rlDest.wide); |
buzbee | 34cd9e5 | 2011-09-08 14:31:52 -0700 | [diff] [blame] | 545 | |
buzbee | 5ade1d2 | 2011-09-09 14:44:52 -0700 | [diff] [blame] | 546 | genNullCheck(cUnit, rlObj.sRegLow, rlObj.lowReg, mir);/* null obj? */ |
buzbee | 34cd9e5 | 2011-09-08 14:31:52 -0700 | [diff] [blame] | 547 | opRegRegImm(cUnit, kOpAdd, regPtr, rlObj.lowReg, fieldOffset); |
| 548 | rlResult = oatEvalLoc(cUnit, rlDest, kAnyReg, true); |
| 549 | |
| 550 | loadPair(cUnit, regPtr, rlResult.lowReg, rlResult.highReg); |
| 551 | |
buzbee | 34cd9e5 | 2011-09-08 14:31:52 -0700 | [diff] [blame] | 552 | oatFreeTemp(cUnit, regPtr); |
| 553 | storeValueWide(cUnit, rlDest, rlResult); |
| 554 | } |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 555 | } |
| 556 | |
buzbee | ed3e930 | 2011-09-23 17:34:19 -0700 | [diff] [blame] | 557 | STATIC void genIPutWide(CompilationUnit* cUnit, MIR* mir, RegLocation rlSrc, |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 558 | RegLocation rlObj) |
| 559 | { |
Ian Rogers | a3760aa | 2011-11-14 14:32:37 -0800 | [diff] [blame] | 560 | Field* fieldPtr = cUnit->dex_cache->GetResolvedField(mir->dalvikInsn.vC); |
buzbee | 12246b8 | 2011-09-29 14:15:05 -0700 | [diff] [blame] | 561 | #if ANDROID_SMP != 0 |
| 562 | bool isVolatile = (fieldPtr == NULL) || fieldPtr->IsVolatile(); |
| 563 | #else |
| 564 | bool isVolatile = false; |
| 565 | #endif |
buzbee | ce30293 | 2011-10-04 14:32:18 -0700 | [diff] [blame] | 566 | if (SLOW_FIELD_PATH || (fieldPtr == NULL) || isVolatile) { |
Elliott Hughes | 81bc509 | 2011-09-30 17:25:59 -0700 | [diff] [blame] | 567 | getFieldOffset(cUnit, mir, fieldPtr); |
buzbee | 34cd9e5 | 2011-09-08 14:31:52 -0700 | [diff] [blame] | 568 | // Field offset in r0 |
| 569 | rlObj = loadValue(cUnit, rlObj, kCoreReg); |
| 570 | rlSrc = loadValueWide(cUnit, rlSrc, kAnyReg); |
buzbee | 5ade1d2 | 2011-09-09 14:44:52 -0700 | [diff] [blame] | 571 | genNullCheck(cUnit, rlObj.sRegLow, rlObj.lowReg, mir);/* null obj? */ |
buzbee | 34cd9e5 | 2011-09-08 14:31:52 -0700 | [diff] [blame] | 572 | opRegReg(cUnit, kOpAdd, r0, rlObj.lowReg); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 573 | oatGenMemBarrier(cUnit, kSY); |
buzbee | 34cd9e5 | 2011-09-08 14:31:52 -0700 | [diff] [blame] | 574 | storePair(cUnit, r0, rlSrc.lowReg, rlSrc.highReg); |
| 575 | } else { |
buzbee | 34cd9e5 | 2011-09-08 14:31:52 -0700 | [diff] [blame] | 576 | int fieldOffset = fieldPtr->GetOffset().Int32Value(); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 577 | |
buzbee | 34cd9e5 | 2011-09-08 14:31:52 -0700 | [diff] [blame] | 578 | rlObj = loadValue(cUnit, rlObj, kCoreReg); |
| 579 | int regPtr; |
| 580 | rlSrc = loadValueWide(cUnit, rlSrc, kAnyReg); |
buzbee | 5ade1d2 | 2011-09-09 14:44:52 -0700 | [diff] [blame] | 581 | genNullCheck(cUnit, rlObj.sRegLow, rlObj.lowReg, mir);/* null obj? */ |
buzbee | 34cd9e5 | 2011-09-08 14:31:52 -0700 | [diff] [blame] | 582 | regPtr = oatAllocTemp(cUnit); |
| 583 | opRegRegImm(cUnit, kOpAdd, regPtr, rlObj.lowReg, fieldOffset); |
| 584 | |
buzbee | 34cd9e5 | 2011-09-08 14:31:52 -0700 | [diff] [blame] | 585 | storePair(cUnit, regPtr, rlSrc.lowReg, rlSrc.highReg); |
| 586 | |
| 587 | oatFreeTemp(cUnit, regPtr); |
| 588 | } |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 589 | } |
| 590 | |
buzbee | ed3e930 | 2011-09-23 17:34:19 -0700 | [diff] [blame] | 591 | STATIC void genConstClass(CompilationUnit* cUnit, MIR* mir, |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 592 | RegLocation rlDest, RegLocation rlSrc) |
| 593 | { |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 594 | uint32_t type_idx = mir->dalvikInsn.vB; |
buzbee | 1b4c859 | 2011-08-31 10:43:51 -0700 | [diff] [blame] | 595 | int mReg = loadCurrMethod(cUnit); |
| 596 | int resReg = oatAllocTemp(cUnit); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 597 | RegLocation rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true); |
Ian Rogers | a3760aa | 2011-11-14 14:32:37 -0800 | [diff] [blame] | 598 | if (!cUnit->compiler->CanAccessTypeWithoutChecks(cUnit->method_idx, |
| 599 | cUnit->dex_cache, |
| 600 | *cUnit->dex_file, |
| 601 | type_idx)) { |
| 602 | // Call out to helper which resolves type and verifies access. |
| 603 | // Resolved type returned in r0. |
| 604 | loadWordDisp(cUnit, rSELF, |
| 605 | OFFSETOF_MEMBER(Thread, pInitializeTypeAndVerifyAccessFromCode), |
| 606 | rLR); |
| 607 | genRegCopy(cUnit, r1, mReg); |
| 608 | loadConstant(cUnit, r0, type_idx); |
| 609 | callRuntimeHelper(cUnit, rLR); |
| 610 | RegLocation rlResult = oatGetReturn(cUnit); |
| 611 | storeValue(cUnit, rlDest, rlResult); |
buzbee | 1b4c859 | 2011-08-31 10:43:51 -0700 | [diff] [blame] | 612 | } else { |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 613 | // We're don't need access checks, load type from dex cache |
| 614 | int32_t dex_cache_offset = Method::DexCacheResolvedTypesOffset().Int32Value(); |
| 615 | loadWordDisp(cUnit, mReg, dex_cache_offset, resReg); |
| 616 | int32_t offset_of_type = Array::DataOffset().Int32Value() + (sizeof(Class*) * type_idx); |
| 617 | loadWordDisp(cUnit, resReg, offset_of_type, rlResult.lowReg); |
Ian Rogers | a3760aa | 2011-11-14 14:32:37 -0800 | [diff] [blame] | 618 | if (!cUnit->compiler->CanAssumeTypeIsPresentInDexCache(cUnit->dex_cache, |
| 619 | type_idx) || |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 620 | SLOW_TYPE_PATH) { |
| 621 | // Slow path, at runtime test if the type is null and if so initialize |
| 622 | oatFlushAllRegs(cUnit); |
| 623 | ArmLIR* branch1 = genCmpImmBranch(cUnit, kArmCondEq, rlResult.lowReg, 0); |
| 624 | // Resolved, store and hop over following code |
| 625 | storeValue(cUnit, rlDest, rlResult); |
| 626 | ArmLIR* branch2 = genUnconditionalBranch(cUnit,0); |
| 627 | // TUNING: move slow path to end & remove unconditional branch |
| 628 | ArmLIR* target1 = newLIR0(cUnit, kArmPseudoTargetLabel); |
| 629 | target1->defMask = ENCODE_ALL; |
| 630 | // Call out to helper, which will return resolved type in r0 |
| 631 | loadWordDisp(cUnit, rSELF, OFFSETOF_MEMBER(Thread, pInitializeTypeFromCode), rLR); |
| 632 | genRegCopy(cUnit, r1, mReg); |
| 633 | loadConstant(cUnit, r0, type_idx); |
| 634 | callRuntimeHelper(cUnit, rLR); |
| 635 | RegLocation rlResult = oatGetReturn(cUnit); |
| 636 | storeValue(cUnit, rlDest, rlResult); |
| 637 | // Rejoin code paths |
| 638 | ArmLIR* target2 = newLIR0(cUnit, kArmPseudoTargetLabel); |
| 639 | target2->defMask = ENCODE_ALL; |
| 640 | branch1->generic.target = (LIR*)target1; |
| 641 | branch2->generic.target = (LIR*)target2; |
| 642 | } else { |
| 643 | // Fast path, we're done - just store result |
| 644 | storeValue(cUnit, rlDest, rlResult); |
| 645 | } |
buzbee | 1b4c859 | 2011-08-31 10:43:51 -0700 | [diff] [blame] | 646 | } |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 647 | } |
| 648 | |
buzbee | ed3e930 | 2011-09-23 17:34:19 -0700 | [diff] [blame] | 649 | STATIC void genConstString(CompilationUnit* cUnit, MIR* mir, |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 650 | RegLocation rlDest, RegLocation rlSrc) |
| 651 | { |
buzbee | ce30293 | 2011-10-04 14:32:18 -0700 | [diff] [blame] | 652 | /* NOTE: Most strings should be available at compile time */ |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 653 | uint32_t string_idx = mir->dalvikInsn.vB; |
| 654 | int32_t offset_of_string = Array::DataOffset().Int32Value() + (sizeof(String*) * string_idx); |
Ian Rogers | a3760aa | 2011-11-14 14:32:37 -0800 | [diff] [blame] | 655 | if (!cUnit->compiler->CanAssumeStringIsPresentInDexCache(cUnit->dex_cache, string_idx) || |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 656 | SLOW_STRING_PATH) { |
| 657 | // slow path, resolve string if not in dex cache |
buzbee | ce30293 | 2011-10-04 14:32:18 -0700 | [diff] [blame] | 658 | oatFlushAllRegs(cUnit); |
| 659 | oatLockCallTemps(cUnit); // Using explicit registers |
| 660 | loadCurrMethodDirect(cUnit, r2); |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 661 | loadWordDisp(cUnit, r2, Method::DexCacheStringsOffset().Int32Value(), r0); |
buzbee | ce30293 | 2011-10-04 14:32:18 -0700 | [diff] [blame] | 662 | // Might call out to helper, which will return resolved string in r0 |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 663 | loadWordDisp(cUnit, rSELF, OFFSETOF_MEMBER(Thread, pResolveStringFromCode), rLR); |
| 664 | loadWordDisp(cUnit, r0, offset_of_string, r0); |
| 665 | loadConstant(cUnit, r1, string_idx); |
buzbee | ce30293 | 2011-10-04 14:32:18 -0700 | [diff] [blame] | 666 | opRegImm(cUnit, kOpCmp, r0, 0); // Is resolved? |
| 667 | genBarrier(cUnit); |
| 668 | // For testing, always force through helper |
| 669 | if (!EXERCISE_SLOWEST_STRING_PATH) { |
| 670 | genIT(cUnit, kArmCondEq, "T"); |
| 671 | } |
| 672 | genRegCopy(cUnit, r0, r2); // .eq |
| 673 | opReg(cUnit, kOpBlx, rLR); // .eq, helper(Method*, string_idx) |
| 674 | genBarrier(cUnit); |
| 675 | storeValue(cUnit, rlDest, getRetLoc(cUnit)); |
| 676 | } else { |
| 677 | int mReg = loadCurrMethod(cUnit); |
| 678 | int resReg = oatAllocTemp(cUnit); |
| 679 | RegLocation rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true); |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 680 | loadWordDisp(cUnit, mReg, Method::DexCacheStringsOffset().Int32Value(), resReg); |
| 681 | loadWordDisp(cUnit, resReg, offset_of_string, rlResult.lowReg); |
buzbee | ce30293 | 2011-10-04 14:32:18 -0700 | [diff] [blame] | 682 | storeValue(cUnit, rlDest, rlResult); |
| 683 | } |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 684 | } |
| 685 | |
buzbee | dfd3d70 | 2011-08-28 12:56:51 -0700 | [diff] [blame] | 686 | /* |
| 687 | * Let helper function take care of everything. Will |
| 688 | * call Class::NewInstanceFromCode(type_idx, method); |
| 689 | */ |
buzbee | ed3e930 | 2011-09-23 17:34:19 -0700 | [diff] [blame] | 690 | STATIC void genNewInstance(CompilationUnit* cUnit, MIR* mir, |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 691 | RegLocation rlDest) |
| 692 | { |
buzbee | dfd3d70 | 2011-08-28 12:56:51 -0700 | [diff] [blame] | 693 | oatFlushAllRegs(cUnit); /* Everything to home location */ |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 694 | uint32_t type_idx = mir->dalvikInsn.vB; |
| 695 | // alloc will always check for resolution, do we also need to verify access because the |
| 696 | // verifier was unable to? |
Ian Rogers | a3760aa | 2011-11-14 14:32:37 -0800 | [diff] [blame] | 697 | if (cUnit->compiler->CanAccessTypeWithoutChecks(cUnit->method_idx, |
| 698 | cUnit->dex_cache, |
| 699 | *cUnit->dex_file, |
| 700 | type_idx)) { |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 701 | loadWordDisp(cUnit, rSELF, OFFSETOF_MEMBER(Thread, pAllocObjectFromCode), rLR); |
| 702 | } else { |
| 703 | loadWordDisp(cUnit, rSELF, |
| 704 | OFFSETOF_MEMBER(Thread, pAllocObjectFromCodeWithAccessCheck), rLR); |
| 705 | } |
| 706 | loadCurrMethodDirect(cUnit, r1); // arg1 <= Method* |
| 707 | loadConstant(cUnit, r0, type_idx); // arg0 <- type_idx |
Ian Rogers | ff1ed47 | 2011-09-20 13:46:24 -0700 | [diff] [blame] | 708 | callRuntimeHelper(cUnit, rLR); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 709 | RegLocation rlResult = oatGetReturn(cUnit); |
| 710 | storeValue(cUnit, rlDest, rlResult); |
| 711 | } |
| 712 | |
| 713 | void genThrow(CompilationUnit* cUnit, MIR* mir, RegLocation rlSrc) |
| 714 | { |
buzbee | 6181f79 | 2011-09-29 11:14:04 -0700 | [diff] [blame] | 715 | oatFlushAllRegs(cUnit); |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 716 | loadWordDisp(cUnit, rSELF, OFFSETOF_MEMBER(Thread, pDeliverException), rLR); |
Ian Rogers | bdb0391 | 2011-09-14 00:55:44 -0700 | [diff] [blame] | 717 | loadValueDirectFixed(cUnit, rlSrc, r0); // Get exception object |
Ian Rogers | ff1ed47 | 2011-09-20 13:46:24 -0700 | [diff] [blame] | 718 | callRuntimeHelper(cUnit, rLR); // art_deliver_exception(exception); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 719 | } |
| 720 | |
buzbee | ed3e930 | 2011-09-23 17:34:19 -0700 | [diff] [blame] | 721 | STATIC void genInstanceof(CompilationUnit* cUnit, MIR* mir, RegLocation rlDest, |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 722 | RegLocation rlSrc) |
| 723 | { |
buzbee | 6181f79 | 2011-09-29 11:14:04 -0700 | [diff] [blame] | 724 | oatFlushAllRegs(cUnit); |
buzbee | 2a475e7 | 2011-09-07 17:19:17 -0700 | [diff] [blame] | 725 | // May generate a call - use explicit registers |
| 726 | oatLockCallTemps(cUnit); |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 727 | uint32_t type_idx = mir->dalvikInsn.vC; |
buzbee | 2a475e7 | 2011-09-07 17:19:17 -0700 | [diff] [blame] | 728 | loadCurrMethodDirect(cUnit, r1); // r1 <= current Method* |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 729 | int classReg = r2; // r2 will hold the Class* |
Ian Rogers | a3760aa | 2011-11-14 14:32:37 -0800 | [diff] [blame] | 730 | if (!cUnit->compiler->CanAccessTypeWithoutChecks(cUnit->method_idx, |
| 731 | cUnit->dex_cache, |
| 732 | *cUnit->dex_file, |
| 733 | type_idx)) { |
Ian Rogers | b093c6b | 2011-10-31 16:19:55 -0700 | [diff] [blame] | 734 | // Check we have access to type_idx and if not throw IllegalAccessError, |
| 735 | // returns Class* in r0 |
| 736 | loadWordDisp(cUnit, rSELF, |
| 737 | OFFSETOF_MEMBER(Thread, pInitializeTypeAndVerifyAccessFromCode), |
| 738 | rLR); |
| 739 | loadConstant(cUnit, r0, type_idx); |
| 740 | callRuntimeHelper(cUnit, rLR); // InitializeTypeAndVerifyAccess(idx, method) |
| 741 | genRegCopy(cUnit, classReg, r0); // Align usage with fast path |
Ian Rogers | 6a99678 | 2011-10-31 17:06:39 -0700 | [diff] [blame] | 742 | loadValueDirectFixed(cUnit, rlSrc, r0); // r0 <= ref |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 743 | } else { |
| 744 | // Load dex cache entry into classReg (r2) |
Ian Rogers | 6a99678 | 2011-10-31 17:06:39 -0700 | [diff] [blame] | 745 | loadValueDirectFixed(cUnit, rlSrc, r0); // r0 <= ref |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 746 | loadWordDisp(cUnit, r1, Method::DexCacheResolvedTypesOffset().Int32Value(), classReg); |
| 747 | int32_t offset_of_type = Array::DataOffset().Int32Value() + (sizeof(Class*) * type_idx); |
| 748 | loadWordDisp(cUnit, classReg, offset_of_type, classReg); |
Ian Rogers | a3760aa | 2011-11-14 14:32:37 -0800 | [diff] [blame] | 749 | if (!cUnit->compiler->CanAssumeTypeIsPresentInDexCache(cUnit->dex_cache, type_idx)) { |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 750 | // Need to test presence of type in dex cache at runtime |
| 751 | ArmLIR* hopBranch = genCmpImmBranch(cUnit, kArmCondNe, classReg, 0); |
| 752 | // Not resolved |
| 753 | // Call out to helper, which will return resolved type in r0 |
| 754 | loadWordDisp(cUnit, rSELF, OFFSETOF_MEMBER(Thread, pInitializeTypeFromCode), rLR); |
| 755 | loadConstant(cUnit, r0, type_idx); |
Ian Rogers | b093c6b | 2011-10-31 16:19:55 -0700 | [diff] [blame] | 756 | callRuntimeHelper(cUnit, rLR); // InitializeTypeFromCode(idx, method) |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 757 | genRegCopy(cUnit, r2, r0); // Align usage with fast path |
| 758 | loadValueDirectFixed(cUnit, rlSrc, r0); /* reload Ref */ |
| 759 | // Rejoin code paths |
| 760 | ArmLIR* hopTarget = newLIR0(cUnit, kArmPseudoTargetLabel); |
| 761 | hopTarget->defMask = ENCODE_ALL; |
| 762 | hopBranch->generic.target = (LIR*)hopTarget; |
| 763 | } |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 764 | } |
buzbee | 991e3ac | 2011-09-29 15:44:22 -0700 | [diff] [blame] | 765 | /* r0 is ref, r2 is class. If ref==null, use directly as bool result */ |
| 766 | ArmLIR* branch1 = genCmpImmBranch(cUnit, kArmCondEq, r0, 0); |
buzbee | 2a475e7 | 2011-09-07 17:19:17 -0700 | [diff] [blame] | 767 | /* load object->clazz */ |
buzbee | ed3e930 | 2011-09-23 17:34:19 -0700 | [diff] [blame] | 768 | DCHECK_EQ(Object::ClassOffset().Int32Value(), 0); |
buzbee | 991e3ac | 2011-09-29 15:44:22 -0700 | [diff] [blame] | 769 | loadWordDisp(cUnit, r0, Object::ClassOffset().Int32Value(), r1); |
| 770 | /* r0 is ref, r1 is ref->clazz, r2 is class */ |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 771 | loadWordDisp(cUnit, rSELF, OFFSETOF_MEMBER(Thread, pInstanceofNonTrivialFromCode), rLR); |
buzbee | 991e3ac | 2011-09-29 15:44:22 -0700 | [diff] [blame] | 772 | opRegReg(cUnit, kOpCmp, r1, r2); // Same? |
| 773 | genBarrier(cUnit); |
| 774 | genIT(cUnit, kArmCondEq, "EE"); // if-convert the test |
| 775 | loadConstant(cUnit, r0, 1); // .eq case - load true |
| 776 | genRegCopy(cUnit, r0, r2); // .ne case - arg0 <= class |
| 777 | opReg(cUnit, kOpBlx, rLR); // .ne case: helper(class, ref->class) |
| 778 | genBarrier(cUnit); |
| 779 | oatClobberCalleeSave(cUnit); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 780 | /* branch target here */ |
| 781 | ArmLIR* target = newLIR0(cUnit, kArmPseudoTargetLabel); |
| 782 | target->defMask = ENCODE_ALL; |
buzbee | 2a475e7 | 2011-09-07 17:19:17 -0700 | [diff] [blame] | 783 | RegLocation rlResult = oatGetReturn(cUnit); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 784 | storeValue(cUnit, rlDest, rlResult); |
| 785 | branch1->generic.target = (LIR*)target; |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 786 | } |
| 787 | |
buzbee | ed3e930 | 2011-09-23 17:34:19 -0700 | [diff] [blame] | 788 | STATIC void genCheckCast(CompilationUnit* cUnit, MIR* mir, RegLocation rlSrc) |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 789 | { |
buzbee | 6181f79 | 2011-09-29 11:14:04 -0700 | [diff] [blame] | 790 | oatFlushAllRegs(cUnit); |
buzbee | 2a475e7 | 2011-09-07 17:19:17 -0700 | [diff] [blame] | 791 | // May generate a call - use explicit registers |
| 792 | oatLockCallTemps(cUnit); |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 793 | uint32_t type_idx = mir->dalvikInsn.vB; |
buzbee | 2a475e7 | 2011-09-07 17:19:17 -0700 | [diff] [blame] | 794 | loadCurrMethodDirect(cUnit, r1); // r1 <= current Method* |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 795 | int classReg = r2; // r2 will hold the Class* |
Ian Rogers | a3760aa | 2011-11-14 14:32:37 -0800 | [diff] [blame] | 796 | if (!cUnit->compiler->CanAccessTypeWithoutChecks(cUnit->method_idx, |
| 797 | cUnit->dex_cache, |
| 798 | *cUnit->dex_file, |
| 799 | type_idx)) { |
Ian Rogers | b093c6b | 2011-10-31 16:19:55 -0700 | [diff] [blame] | 800 | // Check we have access to type_idx and if not throw IllegalAccessError, |
| 801 | // returns Class* in r0 |
| 802 | loadWordDisp(cUnit, rSELF, |
| 803 | OFFSETOF_MEMBER(Thread, pInitializeTypeAndVerifyAccessFromCode), |
| 804 | rLR); |
| 805 | loadConstant(cUnit, r0, type_idx); |
| 806 | callRuntimeHelper(cUnit, rLR); // InitializeTypeAndVerifyAccess(idx, method) |
| 807 | genRegCopy(cUnit, classReg, r0); // Align usage with fast path |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 808 | } else { |
| 809 | // Load dex cache entry into classReg (r2) |
| 810 | loadWordDisp(cUnit, r1, Method::DexCacheResolvedTypesOffset().Int32Value(), classReg); |
| 811 | int32_t offset_of_type = Array::DataOffset().Int32Value() + (sizeof(Class*) * type_idx); |
| 812 | loadWordDisp(cUnit, classReg, offset_of_type, classReg); |
Ian Rogers | a3760aa | 2011-11-14 14:32:37 -0800 | [diff] [blame] | 813 | if (!cUnit->compiler->CanAssumeTypeIsPresentInDexCache(cUnit->dex_cache, type_idx)) { |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 814 | // Need to test presence of type in dex cache at runtime |
| 815 | ArmLIR* hopBranch = genCmpImmBranch(cUnit, kArmCondNe, classReg, 0); |
| 816 | // Not resolved |
| 817 | // Call out to helper, which will return resolved type in r0 |
| 818 | loadWordDisp(cUnit, rSELF, OFFSETOF_MEMBER(Thread, pInitializeTypeFromCode), rLR); |
| 819 | loadConstant(cUnit, r0, type_idx); |
Ian Rogers | b093c6b | 2011-10-31 16:19:55 -0700 | [diff] [blame] | 820 | callRuntimeHelper(cUnit, rLR); // InitializeTypeFromCode(idx, method) |
| 821 | genRegCopy(cUnit, classReg, r0); // Align usage with fast path |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 822 | // Rejoin code paths |
| 823 | ArmLIR* hopTarget = newLIR0(cUnit, kArmPseudoTargetLabel); |
| 824 | hopTarget->defMask = ENCODE_ALL; |
| 825 | hopBranch->generic.target = (LIR*)hopTarget; |
| 826 | } |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 827 | } |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 828 | // At this point, classReg (r2) has class |
| 829 | loadValueDirectFixed(cUnit, rlSrc, r0); // r0 <= ref |
buzbee | 2a475e7 | 2011-09-07 17:19:17 -0700 | [diff] [blame] | 830 | /* Null is OK - continue */ |
| 831 | ArmLIR* branch1 = genCmpImmBranch(cUnit, kArmCondEq, r0, 0); |
| 832 | /* load object->clazz */ |
buzbee | ed3e930 | 2011-09-23 17:34:19 -0700 | [diff] [blame] | 833 | DCHECK_EQ(Object::ClassOffset().Int32Value(), 0); |
buzbee | 2a475e7 | 2011-09-07 17:19:17 -0700 | [diff] [blame] | 834 | loadWordDisp(cUnit, r0, Object::ClassOffset().Int32Value(), r1); |
| 835 | /* r1 now contains object->clazz */ |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 836 | loadWordDisp(cUnit, rSELF, OFFSETOF_MEMBER(Thread, pCheckCastFromCode), rLR); |
Ian Rogers | b093c6b | 2011-10-31 16:19:55 -0700 | [diff] [blame] | 837 | opRegReg(cUnit, kOpCmp, r1, classReg); |
buzbee | 2a475e7 | 2011-09-07 17:19:17 -0700 | [diff] [blame] | 838 | ArmLIR* branch2 = opCondBranch(cUnit, kArmCondEq); /* If equal, trivial yes */ |
| 839 | genRegCopy(cUnit, r0, r1); |
| 840 | genRegCopy(cUnit, r1, r2); |
Ian Rogers | ff1ed47 | 2011-09-20 13:46:24 -0700 | [diff] [blame] | 841 | callRuntimeHelper(cUnit, rLR); |
buzbee | 2a475e7 | 2011-09-07 17:19:17 -0700 | [diff] [blame] | 842 | /* branch target here */ |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 843 | ArmLIR* target = newLIR0(cUnit, kArmPseudoTargetLabel); |
| 844 | target->defMask = ENCODE_ALL; |
| 845 | branch1->generic.target = (LIR*)target; |
| 846 | branch2->generic.target = (LIR*)target; |
| 847 | } |
| 848 | |
buzbee | ed3e930 | 2011-09-23 17:34:19 -0700 | [diff] [blame] | 849 | STATIC void genNegFloat(CompilationUnit* cUnit, RegLocation rlDest, |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 850 | RegLocation rlSrc) |
| 851 | { |
| 852 | RegLocation rlResult; |
| 853 | rlSrc = loadValue(cUnit, rlSrc, kFPReg); |
| 854 | rlResult = oatEvalLoc(cUnit, rlDest, kFPReg, true); |
| 855 | newLIR2(cUnit, kThumb2Vnegs, rlResult.lowReg, rlSrc.lowReg); |
| 856 | storeValue(cUnit, rlDest, rlResult); |
| 857 | } |
| 858 | |
buzbee | ed3e930 | 2011-09-23 17:34:19 -0700 | [diff] [blame] | 859 | STATIC void genNegDouble(CompilationUnit* cUnit, RegLocation rlDest, |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 860 | RegLocation rlSrc) |
| 861 | { |
| 862 | RegLocation rlResult; |
| 863 | rlSrc = loadValueWide(cUnit, rlSrc, kFPReg); |
| 864 | rlResult = oatEvalLoc(cUnit, rlDest, kFPReg, true); |
| 865 | newLIR2(cUnit, kThumb2Vnegd, S2D(rlResult.lowReg, rlResult.highReg), |
| 866 | S2D(rlSrc.lowReg, rlSrc.highReg)); |
| 867 | storeValueWide(cUnit, rlDest, rlResult); |
| 868 | } |
| 869 | |
buzbee | ed3e930 | 2011-09-23 17:34:19 -0700 | [diff] [blame] | 870 | STATIC void freeRegLocTemps(CompilationUnit* cUnit, RegLocation rlKeep, |
buzbee | 439c4fa | 2011-08-27 15:59:07 -0700 | [diff] [blame] | 871 | RegLocation rlFree) |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 872 | { |
buzbee | 6181f79 | 2011-09-29 11:14:04 -0700 | [diff] [blame] | 873 | if ((rlFree.lowReg != rlKeep.lowReg) && (rlFree.lowReg != rlKeep.highReg) && |
| 874 | (rlFree.highReg != rlKeep.lowReg) && (rlFree.highReg != rlKeep.highReg)) { |
| 875 | // No overlap, free both |
buzbee | 439c4fa | 2011-08-27 15:59:07 -0700 | [diff] [blame] | 876 | oatFreeTemp(cUnit, rlFree.lowReg); |
buzbee | 6181f79 | 2011-09-29 11:14:04 -0700 | [diff] [blame] | 877 | oatFreeTemp(cUnit, rlFree.highReg); |
| 878 | } |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 879 | } |
| 880 | |
buzbee | ed3e930 | 2011-09-23 17:34:19 -0700 | [diff] [blame] | 881 | STATIC void genLong3Addr(CompilationUnit* cUnit, MIR* mir, OpKind firstOp, |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 882 | OpKind secondOp, RegLocation rlDest, |
| 883 | RegLocation rlSrc1, RegLocation rlSrc2) |
| 884 | { |
buzbee | 9e0f9b0 | 2011-08-24 15:32:46 -0700 | [diff] [blame] | 885 | /* |
| 886 | * NOTE: This is the one place in the code in which we might have |
| 887 | * as many as six live temporary registers. There are 5 in the normal |
| 888 | * set for Arm. Until we have spill capabilities, temporarily add |
| 889 | * lr to the temp set. It is safe to do this locally, but note that |
| 890 | * lr is used explicitly elsewhere in the code generator and cannot |
| 891 | * normally be used as a general temp register. |
| 892 | */ |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 893 | RegLocation rlResult; |
buzbee | 9e0f9b0 | 2011-08-24 15:32:46 -0700 | [diff] [blame] | 894 | oatMarkTemp(cUnit, rLR); // Add lr to the temp pool |
| 895 | oatFreeTemp(cUnit, rLR); // and make it available |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 896 | rlSrc1 = loadValueWide(cUnit, rlSrc1, kCoreReg); |
| 897 | rlSrc2 = loadValueWide(cUnit, rlSrc2, kCoreReg); |
| 898 | rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true); |
buzbee | c0ecd65 | 2011-09-25 18:11:54 -0700 | [diff] [blame] | 899 | // The longs may overlap - use intermediate temp if so |
| 900 | if (rlResult.lowReg == rlSrc1.highReg) { |
buzbee | c0ecd65 | 2011-09-25 18:11:54 -0700 | [diff] [blame] | 901 | int tReg = oatAllocTemp(cUnit); |
| 902 | genRegCopy(cUnit, tReg, rlSrc1.highReg); |
| 903 | opRegRegReg(cUnit, firstOp, rlResult.lowReg, rlSrc1.lowReg, |
| 904 | rlSrc2.lowReg); |
| 905 | opRegRegReg(cUnit, secondOp, rlResult.highReg, tReg, |
| 906 | rlSrc2.highReg); |
| 907 | oatFreeTemp(cUnit, tReg); |
| 908 | } else { |
| 909 | opRegRegReg(cUnit, firstOp, rlResult.lowReg, rlSrc1.lowReg, |
| 910 | rlSrc2.lowReg); |
| 911 | opRegRegReg(cUnit, secondOp, rlResult.highReg, rlSrc1.highReg, |
| 912 | rlSrc2.highReg); |
| 913 | } |
buzbee | 439c4fa | 2011-08-27 15:59:07 -0700 | [diff] [blame] | 914 | /* |
| 915 | * NOTE: If rlDest refers to a frame variable in a large frame, the |
| 916 | * following storeValueWide might need to allocate a temp register. |
| 917 | * To further work around the lack of a spill capability, explicitly |
| 918 | * free any temps from rlSrc1 & rlSrc2 that aren't still live in rlResult. |
| 919 | * Remove when spill is functional. |
| 920 | */ |
| 921 | freeRegLocTemps(cUnit, rlResult, rlSrc1); |
| 922 | freeRegLocTemps(cUnit, rlResult, rlSrc2); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 923 | storeValueWide(cUnit, rlDest, rlResult); |
buzbee | 9e0f9b0 | 2011-08-24 15:32:46 -0700 | [diff] [blame] | 924 | oatClobber(cUnit, rLR); |
| 925 | oatUnmarkTemp(cUnit, rLR); // Remove lr from the temp pool |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 926 | } |
| 927 | |
| 928 | void oatInitializeRegAlloc(CompilationUnit* cUnit) |
| 929 | { |
| 930 | int numRegs = sizeof(coreRegs)/sizeof(*coreRegs); |
| 931 | int numReserved = sizeof(reservedRegs)/sizeof(*reservedRegs); |
| 932 | int numTemps = sizeof(coreTemps)/sizeof(*coreTemps); |
| 933 | int numFPRegs = sizeof(fpRegs)/sizeof(*fpRegs); |
| 934 | int numFPTemps = sizeof(fpTemps)/sizeof(*fpTemps); |
| 935 | RegisterPool *pool = (RegisterPool *)oatNew(sizeof(*pool), true); |
| 936 | cUnit->regPool = pool; |
| 937 | pool->numCoreRegs = numRegs; |
| 938 | pool->coreRegs = (RegisterInfo *) |
| 939 | oatNew(numRegs * sizeof(*cUnit->regPool->coreRegs), true); |
| 940 | pool->numFPRegs = numFPRegs; |
| 941 | pool->FPRegs = (RegisterInfo *) |
| 942 | oatNew(numFPRegs * sizeof(*cUnit->regPool->FPRegs), true); |
| 943 | oatInitPool(pool->coreRegs, coreRegs, pool->numCoreRegs); |
| 944 | oatInitPool(pool->FPRegs, fpRegs, pool->numFPRegs); |
| 945 | // Keep special registers from being allocated |
| 946 | for (int i = 0; i < numReserved; i++) { |
buzbee | c0ecd65 | 2011-09-25 18:11:54 -0700 | [diff] [blame] | 947 | if (NO_SUSPEND && (reservedRegs[i] == rSUSPEND)) { |
| 948 | //To measure cost of suspend check |
| 949 | continue; |
| 950 | } |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 951 | oatMarkInUse(cUnit, reservedRegs[i]); |
| 952 | } |
| 953 | // Mark temp regs - all others not in use can be used for promotion |
| 954 | for (int i = 0; i < numTemps; i++) { |
| 955 | oatMarkTemp(cUnit, coreTemps[i]); |
| 956 | } |
| 957 | for (int i = 0; i < numFPTemps; i++) { |
| 958 | oatMarkTemp(cUnit, fpTemps[i]); |
| 959 | } |
buzbee | c0ecd65 | 2011-09-25 18:11:54 -0700 | [diff] [blame] | 960 | // Construct the alias map. |
| 961 | cUnit->phiAliasMap = (int*)oatNew(cUnit->numSSARegs * |
| 962 | sizeof(cUnit->phiAliasMap[0]), false); |
| 963 | for (int i = 0; i < cUnit->numSSARegs; i++) { |
| 964 | cUnit->phiAliasMap[i] = i; |
| 965 | } |
| 966 | for (MIR* phi = cUnit->phiList; phi; phi = phi->meta.phiNext) { |
| 967 | int defReg = phi->ssaRep->defs[0]; |
| 968 | for (int i = 0; i < phi->ssaRep->numUses; i++) { |
| 969 | for (int j = 0; j < cUnit->numSSARegs; j++) { |
| 970 | if (cUnit->phiAliasMap[j] == phi->ssaRep->uses[i]) { |
| 971 | cUnit->phiAliasMap[j] = defReg; |
| 972 | } |
| 973 | } |
| 974 | } |
| 975 | } |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 976 | } |
| 977 | |
| 978 | /* |
| 979 | * Handle simple case (thin lock) inline. If it's complicated, bail |
| 980 | * out to the heavyweight lock/unlock routines. We'll use dedicated |
| 981 | * registers here in order to be in the right position in case we |
| 982 | * to bail to dvm[Lock/Unlock]Object(self, object) |
| 983 | * |
| 984 | * r0 -> self pointer [arg0 for dvm[Lock/Unlock]Object |
| 985 | * r1 -> object [arg1 for dvm[Lock/Unlock]Object |
| 986 | * r2 -> intial contents of object->lock, later result of strex |
| 987 | * r3 -> self->threadId |
| 988 | * r12 -> allow to be used by utilities as general temp |
| 989 | * |
| 990 | * The result of the strex is 0 if we acquire the lock. |
| 991 | * |
| 992 | * See comments in Sync.c for the layout of the lock word. |
| 993 | * Of particular interest to this code is the test for the |
| 994 | * simple case - which we handle inline. For monitor enter, the |
| 995 | * simple case is thin lock, held by no-one. For monitor exit, |
| 996 | * the simple case is thin lock, held by the unlocking thread with |
| 997 | * a recurse count of 0. |
| 998 | * |
| 999 | * A minor complication is that there is a field in the lock word |
| 1000 | * unrelated to locking: the hash state. This field must be ignored, but |
| 1001 | * preserved. |
| 1002 | * |
| 1003 | */ |
buzbee | ed3e930 | 2011-09-23 17:34:19 -0700 | [diff] [blame] | 1004 | STATIC void genMonitorEnter(CompilationUnit* cUnit, MIR* mir, |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 1005 | RegLocation rlSrc) |
| 1006 | { |
| 1007 | ArmLIR* target; |
| 1008 | ArmLIR* hopTarget; |
| 1009 | ArmLIR* branch; |
| 1010 | ArmLIR* hopBranch; |
| 1011 | |
| 1012 | oatFlushAllRegs(cUnit); |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 1013 | DCHECK_EQ(LW_SHAPE_THIN, 0); |
Ian Rogers | 4f0d07c | 2011-10-06 23:38:47 -0700 | [diff] [blame] | 1014 | loadValueDirectFixed(cUnit, rlSrc, r0); // Get obj |
buzbee | 2e748f3 | 2011-08-29 21:02:19 -0700 | [diff] [blame] | 1015 | oatLockCallTemps(cUnit); // Prepare for explicit register usage |
Ian Rogers | 4f0d07c | 2011-10-06 23:38:47 -0700 | [diff] [blame] | 1016 | genNullCheck(cUnit, rlSrc.sRegLow, r0, mir); |
| 1017 | loadWordDisp(cUnit, rSELF, Thread::ThinLockIdOffset().Int32Value(), r2); |
| 1018 | newLIR3(cUnit, kThumb2Ldrex, r1, r0, |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1019 | Object::MonitorOffset().Int32Value() >> 2); // Get object->lock |
buzbee | c143c55 | 2011-08-20 17:38:58 -0700 | [diff] [blame] | 1020 | // Align owner |
Ian Rogers | 4f0d07c | 2011-10-06 23:38:47 -0700 | [diff] [blame] | 1021 | opRegImm(cUnit, kOpLsl, r2, LW_LOCK_OWNER_SHIFT); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 1022 | // Is lock unheld on lock or held by us (==threadId) on unlock? |
Ian Rogers | 4f0d07c | 2011-10-06 23:38:47 -0700 | [diff] [blame] | 1023 | newLIR4(cUnit, kThumb2Bfi, r2, r1, 0, LW_LOCK_OWNER_SHIFT - 1); |
| 1024 | newLIR3(cUnit, kThumb2Bfc, r1, LW_HASH_STATE_SHIFT, LW_LOCK_OWNER_SHIFT - 1); |
| 1025 | hopBranch = newLIR2(cUnit, kThumb2Cbnz, r1, 0); |
| 1026 | newLIR4(cUnit, kThumb2Strex, r1, r2, r0, |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1027 | Object::MonitorOffset().Int32Value() >> 2); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 1028 | oatGenMemBarrier(cUnit, kSY); |
Ian Rogers | 4f0d07c | 2011-10-06 23:38:47 -0700 | [diff] [blame] | 1029 | branch = newLIR2(cUnit, kThumb2Cbz, r1, 0); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 1030 | |
| 1031 | hopTarget = newLIR0(cUnit, kArmPseudoTargetLabel); |
| 1032 | hopTarget->defMask = ENCODE_ALL; |
| 1033 | hopBranch->generic.target = (LIR*)hopTarget; |
| 1034 | |
buzbee | 1b4c859 | 2011-08-31 10:43:51 -0700 | [diff] [blame] | 1035 | // Go expensive route - artLockObjectFromCode(self, obj); |
| 1036 | loadWordDisp(cUnit, rSELF, OFFSETOF_MEMBER(Thread, pLockObjectFromCode), |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 1037 | rLR); |
Ian Rogers | ff1ed47 | 2011-09-20 13:46:24 -0700 | [diff] [blame] | 1038 | callRuntimeHelper(cUnit, rLR); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 1039 | |
| 1040 | // Resume here |
| 1041 | target = newLIR0(cUnit, kArmPseudoTargetLabel); |
| 1042 | target->defMask = ENCODE_ALL; |
| 1043 | branch->generic.target = (LIR*)target; |
| 1044 | } |
| 1045 | |
| 1046 | /* |
| 1047 | * For monitor unlock, we don't have to use ldrex/strex. Once |
| 1048 | * we've determined that the lock is thin and that we own it with |
| 1049 | * a zero recursion count, it's safe to punch it back to the |
| 1050 | * initial, unlock thin state with a store word. |
| 1051 | */ |
buzbee | ed3e930 | 2011-09-23 17:34:19 -0700 | [diff] [blame] | 1052 | STATIC void genMonitorExit(CompilationUnit* cUnit, MIR* mir, |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 1053 | RegLocation rlSrc) |
| 1054 | { |
| 1055 | ArmLIR* target; |
| 1056 | ArmLIR* branch; |
| 1057 | ArmLIR* hopTarget; |
| 1058 | ArmLIR* hopBranch; |
| 1059 | |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 1060 | DCHECK_EQ(LW_SHAPE_THIN, 0); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 1061 | oatFlushAllRegs(cUnit); |
Ian Rogers | 4f0d07c | 2011-10-06 23:38:47 -0700 | [diff] [blame] | 1062 | loadValueDirectFixed(cUnit, rlSrc, r0); // Get obj |
buzbee | 2e748f3 | 2011-08-29 21:02:19 -0700 | [diff] [blame] | 1063 | oatLockCallTemps(cUnit); // Prepare for explicit register usage |
Ian Rogers | 4f0d07c | 2011-10-06 23:38:47 -0700 | [diff] [blame] | 1064 | genNullCheck(cUnit, rlSrc.sRegLow, r0, mir); |
| 1065 | loadWordDisp(cUnit, r0, Object::MonitorOffset().Int32Value(), r1); // Get lock |
| 1066 | loadWordDisp(cUnit, rSELF, Thread::ThinLockIdOffset().Int32Value(), r2); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 1067 | // Is lock unheld on lock or held by us (==threadId) on unlock? |
Ian Rogers | 4f0d07c | 2011-10-06 23:38:47 -0700 | [diff] [blame] | 1068 | opRegRegImm(cUnit, kOpAnd, r3, r1, (LW_HASH_STATE_MASK << LW_HASH_STATE_SHIFT)); |
buzbee | c143c55 | 2011-08-20 17:38:58 -0700 | [diff] [blame] | 1069 | // Align owner |
Ian Rogers | 4f0d07c | 2011-10-06 23:38:47 -0700 | [diff] [blame] | 1070 | opRegImm(cUnit, kOpLsl, r2, LW_LOCK_OWNER_SHIFT); |
| 1071 | newLIR3(cUnit, kThumb2Bfc, r1, LW_HASH_STATE_SHIFT, LW_LOCK_OWNER_SHIFT - 1); |
| 1072 | opRegReg(cUnit, kOpSub, r1, r2); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 1073 | hopBranch = opCondBranch(cUnit, kArmCondNe); |
| 1074 | oatGenMemBarrier(cUnit, kSY); |
Ian Rogers | 4f0d07c | 2011-10-06 23:38:47 -0700 | [diff] [blame] | 1075 | storeWordDisp(cUnit, r0, Object::MonitorOffset().Int32Value(), r3); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 1076 | branch = opNone(cUnit, kOpUncondBr); |
| 1077 | |
| 1078 | hopTarget = newLIR0(cUnit, kArmPseudoTargetLabel); |
| 1079 | hopTarget->defMask = ENCODE_ALL; |
| 1080 | hopBranch->generic.target = (LIR*)hopTarget; |
| 1081 | |
Ian Rogers | 4f0d07c | 2011-10-06 23:38:47 -0700 | [diff] [blame] | 1082 | // Go expensive route - UnlockObjectFromCode(obj); |
buzbee | 1b4c859 | 2011-08-31 10:43:51 -0700 | [diff] [blame] | 1083 | loadWordDisp(cUnit, rSELF, OFFSETOF_MEMBER(Thread, pUnlockObjectFromCode), |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 1084 | rLR); |
Ian Rogers | ff1ed47 | 2011-09-20 13:46:24 -0700 | [diff] [blame] | 1085 | callRuntimeHelper(cUnit, rLR); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 1086 | |
| 1087 | // Resume here |
| 1088 | target = newLIR0(cUnit, kArmPseudoTargetLabel); |
| 1089 | target->defMask = ENCODE_ALL; |
| 1090 | branch->generic.target = (LIR*)target; |
| 1091 | } |
| 1092 | |
| 1093 | /* |
| 1094 | * 64-bit 3way compare function. |
| 1095 | * mov rX, #-1 |
| 1096 | * cmp op1hi, op2hi |
| 1097 | * blt done |
| 1098 | * bgt flip |
| 1099 | * sub rX, op1lo, op2lo (treat as unsigned) |
| 1100 | * beq done |
| 1101 | * ite hi |
| 1102 | * mov(hi) rX, #-1 |
| 1103 | * mov(!hi) rX, #1 |
| 1104 | * flip: |
| 1105 | * neg rX |
| 1106 | * done: |
| 1107 | */ |
buzbee | ed3e930 | 2011-09-23 17:34:19 -0700 | [diff] [blame] | 1108 | STATIC void genCmpLong(CompilationUnit* cUnit, MIR* mir, |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 1109 | RegLocation rlDest, RegLocation rlSrc1, |
| 1110 | RegLocation rlSrc2) |
| 1111 | { |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 1112 | ArmLIR* target1; |
| 1113 | ArmLIR* target2; |
| 1114 | rlSrc1 = loadValueWide(cUnit, rlSrc1, kCoreReg); |
| 1115 | rlSrc2 = loadValueWide(cUnit, rlSrc2, kCoreReg); |
buzbee | b29e4d1 | 2011-09-26 15:05:48 -0700 | [diff] [blame] | 1116 | int tReg = oatAllocTemp(cUnit); |
| 1117 | loadConstant(cUnit, tReg, -1); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 1118 | opRegReg(cUnit, kOpCmp, rlSrc1.highReg, rlSrc2.highReg); |
| 1119 | ArmLIR* branch1 = opCondBranch(cUnit, kArmCondLt); |
| 1120 | ArmLIR* branch2 = opCondBranch(cUnit, kArmCondGt); |
buzbee | b29e4d1 | 2011-09-26 15:05:48 -0700 | [diff] [blame] | 1121 | opRegRegReg(cUnit, kOpSub, tReg, rlSrc1.lowReg, rlSrc2.lowReg); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 1122 | ArmLIR* branch3 = opCondBranch(cUnit, kArmCondEq); |
| 1123 | |
| 1124 | genIT(cUnit, kArmCondHi, "E"); |
buzbee | b29e4d1 | 2011-09-26 15:05:48 -0700 | [diff] [blame] | 1125 | newLIR2(cUnit, kThumb2MovImmShift, tReg, modifiedImmediate(-1)); |
| 1126 | loadConstant(cUnit, tReg, 1); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 1127 | genBarrier(cUnit); |
| 1128 | |
| 1129 | target2 = newLIR0(cUnit, kArmPseudoTargetLabel); |
| 1130 | target2->defMask = -1; |
buzbee | b29e4d1 | 2011-09-26 15:05:48 -0700 | [diff] [blame] | 1131 | opRegReg(cUnit, kOpNeg, tReg, tReg); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 1132 | |
| 1133 | target1 = newLIR0(cUnit, kArmPseudoTargetLabel); |
| 1134 | target1->defMask = -1; |
| 1135 | |
buzbee | b29e4d1 | 2011-09-26 15:05:48 -0700 | [diff] [blame] | 1136 | RegLocation rlTemp = LOC_C_RETURN; // Just using as template, will change |
| 1137 | rlTemp.lowReg = tReg; |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 1138 | storeValue(cUnit, rlDest, rlTemp); |
buzbee | b29e4d1 | 2011-09-26 15:05:48 -0700 | [diff] [blame] | 1139 | oatFreeTemp(cUnit, tReg); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 1140 | |
| 1141 | branch1->generic.target = (LIR*)target1; |
| 1142 | branch2->generic.target = (LIR*)target2; |
| 1143 | branch3->generic.target = branch1->generic.target; |
| 1144 | } |
| 1145 | |
buzbee | ed3e930 | 2011-09-23 17:34:19 -0700 | [diff] [blame] | 1146 | STATIC void genMultiplyByTwoBitMultiplier(CompilationUnit* cUnit, |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 1147 | RegLocation rlSrc, RegLocation rlResult, int lit, |
| 1148 | int firstBit, int secondBit) |
| 1149 | { |
| 1150 | opRegRegRegShift(cUnit, kOpAdd, rlResult.lowReg, rlSrc.lowReg, rlSrc.lowReg, |
| 1151 | encodeShift(kArmLsl, secondBit - firstBit)); |
| 1152 | if (firstBit != 0) { |
| 1153 | opRegRegImm(cUnit, kOpLsl, rlResult.lowReg, rlResult.lowReg, firstBit); |
| 1154 | } |
| 1155 | } |
| 1156 | |
buzbee | ed3e930 | 2011-09-23 17:34:19 -0700 | [diff] [blame] | 1157 | STATIC bool genConversionCall(CompilationUnit* cUnit, MIR* mir, int funcOffset, |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 1158 | int srcSize, int tgtSize) |
| 1159 | { |
| 1160 | /* |
| 1161 | * Don't optimize the register usage since it calls out to support |
| 1162 | * functions |
| 1163 | */ |
| 1164 | RegLocation rlSrc; |
| 1165 | RegLocation rlDest; |
| 1166 | oatFlushAllRegs(cUnit); /* Send everything to home location */ |
| 1167 | loadWordDisp(cUnit, rSELF, funcOffset, rLR); |
| 1168 | if (srcSize == 1) { |
| 1169 | rlSrc = oatGetSrc(cUnit, mir, 0); |
| 1170 | loadValueDirectFixed(cUnit, rlSrc, r0); |
| 1171 | } else { |
| 1172 | rlSrc = oatGetSrcWide(cUnit, mir, 0, 1); |
| 1173 | loadValueDirectWideFixed(cUnit, rlSrc, r0, r1); |
| 1174 | } |
Ian Rogers | ff1ed47 | 2011-09-20 13:46:24 -0700 | [diff] [blame] | 1175 | callRuntimeHelper(cUnit, rLR); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 1176 | if (tgtSize == 1) { |
| 1177 | RegLocation rlResult; |
| 1178 | rlDest = oatGetDest(cUnit, mir, 0); |
| 1179 | rlResult = oatGetReturn(cUnit); |
| 1180 | storeValue(cUnit, rlDest, rlResult); |
| 1181 | } else { |
| 1182 | RegLocation rlResult; |
| 1183 | rlDest = oatGetDestWide(cUnit, mir, 0, 1); |
| 1184 | rlResult = oatGetReturnWide(cUnit); |
| 1185 | storeValueWide(cUnit, rlDest, rlResult); |
| 1186 | } |
| 1187 | return false; |
| 1188 | } |
| 1189 | |
buzbee | ed3e930 | 2011-09-23 17:34:19 -0700 | [diff] [blame] | 1190 | STATIC bool genArithOpFloatPortable(CompilationUnit* cUnit, MIR* mir, |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 1191 | RegLocation rlDest, RegLocation rlSrc1, |
| 1192 | RegLocation rlSrc2) |
| 1193 | { |
| 1194 | RegLocation rlResult; |
| 1195 | int funcOffset; |
| 1196 | |
| 1197 | switch (mir->dalvikInsn.opcode) { |
| 1198 | case OP_ADD_FLOAT_2ADDR: |
| 1199 | case OP_ADD_FLOAT: |
| 1200 | funcOffset = OFFSETOF_MEMBER(Thread, pFadd); |
| 1201 | break; |
| 1202 | case OP_SUB_FLOAT_2ADDR: |
| 1203 | case OP_SUB_FLOAT: |
| 1204 | funcOffset = OFFSETOF_MEMBER(Thread, pFsub); |
| 1205 | break; |
| 1206 | case OP_DIV_FLOAT_2ADDR: |
| 1207 | case OP_DIV_FLOAT: |
| 1208 | funcOffset = OFFSETOF_MEMBER(Thread, pFdiv); |
| 1209 | break; |
| 1210 | case OP_MUL_FLOAT_2ADDR: |
| 1211 | case OP_MUL_FLOAT: |
| 1212 | funcOffset = OFFSETOF_MEMBER(Thread, pFmul); |
| 1213 | break; |
| 1214 | case OP_REM_FLOAT_2ADDR: |
| 1215 | case OP_REM_FLOAT: |
| 1216 | funcOffset = OFFSETOF_MEMBER(Thread, pFmodf); |
| 1217 | break; |
| 1218 | case OP_NEG_FLOAT: { |
| 1219 | genNegFloat(cUnit, rlDest, rlSrc1); |
| 1220 | return false; |
| 1221 | } |
| 1222 | default: |
| 1223 | return true; |
| 1224 | } |
| 1225 | oatFlushAllRegs(cUnit); /* Send everything to home location */ |
| 1226 | loadWordDisp(cUnit, rSELF, funcOffset, rLR); |
| 1227 | loadValueDirectFixed(cUnit, rlSrc1, r0); |
| 1228 | loadValueDirectFixed(cUnit, rlSrc2, r1); |
Ian Rogers | ff1ed47 | 2011-09-20 13:46:24 -0700 | [diff] [blame] | 1229 | callRuntimeHelper(cUnit, rLR); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 1230 | rlResult = oatGetReturn(cUnit); |
| 1231 | storeValue(cUnit, rlDest, rlResult); |
| 1232 | return false; |
| 1233 | } |
| 1234 | |
buzbee | ed3e930 | 2011-09-23 17:34:19 -0700 | [diff] [blame] | 1235 | STATIC bool genArithOpDoublePortable(CompilationUnit* cUnit, MIR* mir, |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 1236 | RegLocation rlDest, RegLocation rlSrc1, |
| 1237 | RegLocation rlSrc2) |
| 1238 | { |
| 1239 | RegLocation rlResult; |
| 1240 | int funcOffset; |
| 1241 | |
| 1242 | switch (mir->dalvikInsn.opcode) { |
| 1243 | case OP_ADD_DOUBLE_2ADDR: |
| 1244 | case OP_ADD_DOUBLE: |
| 1245 | funcOffset = OFFSETOF_MEMBER(Thread, pDadd); |
| 1246 | break; |
| 1247 | case OP_SUB_DOUBLE_2ADDR: |
| 1248 | case OP_SUB_DOUBLE: |
| 1249 | funcOffset = OFFSETOF_MEMBER(Thread, pDsub); |
| 1250 | break; |
| 1251 | case OP_DIV_DOUBLE_2ADDR: |
| 1252 | case OP_DIV_DOUBLE: |
| 1253 | funcOffset = OFFSETOF_MEMBER(Thread, pDdiv); |
| 1254 | break; |
| 1255 | case OP_MUL_DOUBLE_2ADDR: |
| 1256 | case OP_MUL_DOUBLE: |
| 1257 | funcOffset = OFFSETOF_MEMBER(Thread, pDmul); |
| 1258 | break; |
| 1259 | case OP_REM_DOUBLE_2ADDR: |
| 1260 | case OP_REM_DOUBLE: |
| 1261 | funcOffset = OFFSETOF_MEMBER(Thread, pFmod); |
| 1262 | break; |
| 1263 | case OP_NEG_DOUBLE: { |
| 1264 | genNegDouble(cUnit, rlDest, rlSrc1); |
| 1265 | return false; |
| 1266 | } |
| 1267 | default: |
| 1268 | return true; |
| 1269 | } |
| 1270 | oatFlushAllRegs(cUnit); /* Send everything to home location */ |
| 1271 | loadWordDisp(cUnit, rSELF, funcOffset, rLR); |
| 1272 | loadValueDirectWideFixed(cUnit, rlSrc1, r0, r1); |
| 1273 | loadValueDirectWideFixed(cUnit, rlSrc2, r2, r3); |
Ian Rogers | ff1ed47 | 2011-09-20 13:46:24 -0700 | [diff] [blame] | 1274 | callRuntimeHelper(cUnit, rLR); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 1275 | rlResult = oatGetReturnWide(cUnit); |
| 1276 | storeValueWide(cUnit, rlDest, rlResult); |
| 1277 | return false; |
| 1278 | } |
| 1279 | |
buzbee | ed3e930 | 2011-09-23 17:34:19 -0700 | [diff] [blame] | 1280 | STATIC bool genConversionPortable(CompilationUnit* cUnit, MIR* mir) |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 1281 | { |
| 1282 | Opcode opcode = mir->dalvikInsn.opcode; |
| 1283 | |
| 1284 | switch (opcode) { |
| 1285 | case OP_INT_TO_FLOAT: |
| 1286 | return genConversionCall(cUnit, mir, OFFSETOF_MEMBER(Thread, pI2f), |
| 1287 | 1, 1); |
| 1288 | case OP_FLOAT_TO_INT: |
| 1289 | return genConversionCall(cUnit, mir, OFFSETOF_MEMBER(Thread, pF2iz), |
| 1290 | 1, 1); |
| 1291 | case OP_DOUBLE_TO_FLOAT: |
| 1292 | return genConversionCall(cUnit, mir, OFFSETOF_MEMBER(Thread, pD2f), |
| 1293 | 2, 1); |
| 1294 | case OP_FLOAT_TO_DOUBLE: |
| 1295 | return genConversionCall(cUnit, mir, OFFSETOF_MEMBER(Thread, pF2d), |
| 1296 | 1, 2); |
| 1297 | case OP_INT_TO_DOUBLE: |
| 1298 | return genConversionCall(cUnit, mir, OFFSETOF_MEMBER(Thread, pI2d), |
| 1299 | 1, 2); |
| 1300 | case OP_DOUBLE_TO_INT: |
| 1301 | return genConversionCall(cUnit, mir, OFFSETOF_MEMBER(Thread, pD2iz), |
| 1302 | 2, 1); |
| 1303 | case OP_FLOAT_TO_LONG: |
| 1304 | return genConversionCall(cUnit, mir, OFFSETOF_MEMBER(Thread, |
buzbee | 1b4c859 | 2011-08-31 10:43:51 -0700 | [diff] [blame] | 1305 | pF2l), 1, 2); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 1306 | case OP_LONG_TO_FLOAT: |
| 1307 | return genConversionCall(cUnit, mir, OFFSETOF_MEMBER(Thread, pL2f), |
| 1308 | 2, 1); |
| 1309 | case OP_DOUBLE_TO_LONG: |
| 1310 | return genConversionCall(cUnit, mir, OFFSETOF_MEMBER(Thread, |
buzbee | 1b4c859 | 2011-08-31 10:43:51 -0700 | [diff] [blame] | 1311 | pD2l), 2, 2); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 1312 | case OP_LONG_TO_DOUBLE: |
| 1313 | return genConversionCall(cUnit, mir, OFFSETOF_MEMBER(Thread, pL2d), |
| 1314 | 2, 2); |
| 1315 | default: |
| 1316 | return true; |
| 1317 | } |
| 1318 | return false; |
| 1319 | } |
| 1320 | |
| 1321 | /* Generate conditional branch instructions */ |
buzbee | ed3e930 | 2011-09-23 17:34:19 -0700 | [diff] [blame] | 1322 | STATIC ArmLIR* genConditionalBranch(CompilationUnit* cUnit, |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 1323 | ArmConditionCode cond, |
| 1324 | ArmLIR* target) |
| 1325 | { |
| 1326 | ArmLIR* branch = opCondBranch(cUnit, cond); |
| 1327 | branch->generic.target = (LIR*) target; |
| 1328 | return branch; |
| 1329 | } |
| 1330 | |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 1331 | /* |
| 1332 | * Generate array store |
| 1333 | * |
| 1334 | */ |
buzbee | ed3e930 | 2011-09-23 17:34:19 -0700 | [diff] [blame] | 1335 | STATIC void genArrayObjPut(CompilationUnit* cUnit, MIR* mir, |
buzbee | 1b4c859 | 2011-08-31 10:43:51 -0700 | [diff] [blame] | 1336 | RegLocation rlArray, RegLocation rlIndex, |
| 1337 | RegLocation rlSrc, int scale) |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 1338 | { |
| 1339 | RegisterClass regClass = oatRegClassBySize(kWord); |
buzbee | c143c55 | 2011-08-20 17:38:58 -0700 | [diff] [blame] | 1340 | int lenOffset = Array::LengthOffset().Int32Value(); |
| 1341 | int dataOffset = Array::DataOffset().Int32Value(); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 1342 | |
buzbee | 6181f79 | 2011-09-29 11:14:04 -0700 | [diff] [blame] | 1343 | oatFlushAllRegs(cUnit); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 1344 | /* Make sure it's a legal object Put. Use direct regs at first */ |
| 1345 | loadValueDirectFixed(cUnit, rlArray, r1); |
| 1346 | loadValueDirectFixed(cUnit, rlSrc, r0); |
| 1347 | |
| 1348 | /* null array object? */ |
buzbee | 43a3642 | 2011-09-14 14:00:13 -0700 | [diff] [blame] | 1349 | genNullCheck(cUnit, rlArray.sRegLow, r1, mir); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 1350 | loadWordDisp(cUnit, rSELF, |
buzbee | 1b4c859 | 2011-08-31 10:43:51 -0700 | [diff] [blame] | 1351 | OFFSETOF_MEMBER(Thread, pCanPutArrayElementFromCode), rLR); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 1352 | /* Get the array's clazz */ |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1353 | loadWordDisp(cUnit, r1, Object::ClassOffset().Int32Value(), r1); |
Ian Rogers | ff1ed47 | 2011-09-20 13:46:24 -0700 | [diff] [blame] | 1354 | callRuntimeHelper(cUnit, rLR); |
buzbee | 6181f79 | 2011-09-29 11:14:04 -0700 | [diff] [blame] | 1355 | oatFreeTemp(cUnit, r0); |
| 1356 | oatFreeTemp(cUnit, r1); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 1357 | |
| 1358 | // Now, redo loadValues in case they didn't survive the call |
| 1359 | |
| 1360 | int regPtr; |
| 1361 | rlArray = loadValue(cUnit, rlArray, kCoreReg); |
| 1362 | rlIndex = loadValue(cUnit, rlIndex, kCoreReg); |
| 1363 | |
| 1364 | if (oatIsTemp(cUnit, rlArray.lowReg)) { |
| 1365 | oatClobber(cUnit, rlArray.lowReg); |
| 1366 | regPtr = rlArray.lowReg; |
| 1367 | } else { |
| 1368 | regPtr = oatAllocTemp(cUnit); |
| 1369 | genRegCopy(cUnit, regPtr, rlArray.lowReg); |
| 1370 | } |
| 1371 | |
buzbee | 43a3642 | 2011-09-14 14:00:13 -0700 | [diff] [blame] | 1372 | if (!(mir->optimizationFlags & MIR_IGNORE_RANGE_CHECK)) { |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 1373 | int regLen = oatAllocTemp(cUnit); |
| 1374 | //NOTE: max live temps(4) here. |
| 1375 | /* Get len */ |
| 1376 | loadWordDisp(cUnit, rlArray.lowReg, lenOffset, regLen); |
| 1377 | /* regPtr -> array data */ |
| 1378 | opRegImm(cUnit, kOpAdd, regPtr, dataOffset); |
buzbee | ec5adf3 | 2011-09-11 15:25:43 -0700 | [diff] [blame] | 1379 | genRegRegCheck(cUnit, kArmCondCs, rlIndex.lowReg, regLen, mir, |
buzbee | 5ade1d2 | 2011-09-09 14:44:52 -0700 | [diff] [blame] | 1380 | kArmThrowArrayBounds); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 1381 | oatFreeTemp(cUnit, regLen); |
| 1382 | } else { |
| 1383 | /* regPtr -> array data */ |
| 1384 | opRegImm(cUnit, kOpAdd, regPtr, dataOffset); |
| 1385 | } |
| 1386 | /* at this point, regPtr points to array, 2 live temps */ |
| 1387 | rlSrc = loadValue(cUnit, rlSrc, regClass); |
| 1388 | storeBaseIndexed(cUnit, regPtr, rlIndex.lowReg, rlSrc.lowReg, |
| 1389 | scale, kWord); |
| 1390 | } |
| 1391 | |
| 1392 | /* |
| 1393 | * Generate array load |
| 1394 | */ |
buzbee | ed3e930 | 2011-09-23 17:34:19 -0700 | [diff] [blame] | 1395 | STATIC void genArrayGet(CompilationUnit* cUnit, MIR* mir, OpSize size, |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 1396 | RegLocation rlArray, RegLocation rlIndex, |
| 1397 | RegLocation rlDest, int scale) |
| 1398 | { |
| 1399 | RegisterClass regClass = oatRegClassBySize(size); |
buzbee | c143c55 | 2011-08-20 17:38:58 -0700 | [diff] [blame] | 1400 | int lenOffset = Array::LengthOffset().Int32Value(); |
| 1401 | int dataOffset = Array::DataOffset().Int32Value(); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 1402 | RegLocation rlResult; |
| 1403 | rlArray = loadValue(cUnit, rlArray, kCoreReg); |
| 1404 | rlIndex = loadValue(cUnit, rlIndex, kCoreReg); |
| 1405 | int regPtr; |
| 1406 | |
| 1407 | /* null object? */ |
buzbee | 43a3642 | 2011-09-14 14:00:13 -0700 | [diff] [blame] | 1408 | genNullCheck(cUnit, rlArray.sRegLow, rlArray.lowReg, mir); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 1409 | |
| 1410 | regPtr = oatAllocTemp(cUnit); |
| 1411 | |
buzbee | 43a3642 | 2011-09-14 14:00:13 -0700 | [diff] [blame] | 1412 | if (!(mir->optimizationFlags & MIR_IGNORE_RANGE_CHECK)) { |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 1413 | int regLen = oatAllocTemp(cUnit); |
| 1414 | /* Get len */ |
| 1415 | loadWordDisp(cUnit, rlArray.lowReg, lenOffset, regLen); |
| 1416 | /* regPtr -> array data */ |
| 1417 | opRegRegImm(cUnit, kOpAdd, regPtr, rlArray.lowReg, dataOffset); |
buzbee | ec5adf3 | 2011-09-11 15:25:43 -0700 | [diff] [blame] | 1418 | genRegRegCheck(cUnit, kArmCondCs, rlIndex.lowReg, regLen, mir, |
buzbee | 5ade1d2 | 2011-09-09 14:44:52 -0700 | [diff] [blame] | 1419 | kArmThrowArrayBounds); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 1420 | oatFreeTemp(cUnit, regLen); |
| 1421 | } else { |
| 1422 | /* regPtr -> array data */ |
| 1423 | opRegRegImm(cUnit, kOpAdd, regPtr, rlArray.lowReg, dataOffset); |
| 1424 | } |
buzbee | e9a72f6 | 2011-09-04 17:59:07 -0700 | [diff] [blame] | 1425 | oatFreeTemp(cUnit, rlArray.lowReg); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 1426 | if ((size == kLong) || (size == kDouble)) { |
| 1427 | if (scale) { |
| 1428 | int rNewIndex = oatAllocTemp(cUnit); |
| 1429 | opRegRegImm(cUnit, kOpLsl, rNewIndex, rlIndex.lowReg, scale); |
| 1430 | opRegReg(cUnit, kOpAdd, regPtr, rNewIndex); |
| 1431 | oatFreeTemp(cUnit, rNewIndex); |
| 1432 | } else { |
| 1433 | opRegReg(cUnit, kOpAdd, regPtr, rlIndex.lowReg); |
| 1434 | } |
buzbee | e9a72f6 | 2011-09-04 17:59:07 -0700 | [diff] [blame] | 1435 | oatFreeTemp(cUnit, rlIndex.lowReg); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 1436 | rlResult = oatEvalLoc(cUnit, rlDest, regClass, true); |
| 1437 | |
| 1438 | loadPair(cUnit, regPtr, rlResult.lowReg, rlResult.highReg); |
| 1439 | |
| 1440 | oatFreeTemp(cUnit, regPtr); |
| 1441 | storeValueWide(cUnit, rlDest, rlResult); |
| 1442 | } else { |
| 1443 | rlResult = oatEvalLoc(cUnit, rlDest, regClass, true); |
| 1444 | |
| 1445 | loadBaseIndexed(cUnit, regPtr, rlIndex.lowReg, rlResult.lowReg, |
| 1446 | scale, size); |
| 1447 | |
| 1448 | oatFreeTemp(cUnit, regPtr); |
| 1449 | storeValue(cUnit, rlDest, rlResult); |
| 1450 | } |
| 1451 | } |
| 1452 | |
| 1453 | /* |
| 1454 | * Generate array store |
| 1455 | * |
| 1456 | */ |
buzbee | ed3e930 | 2011-09-23 17:34:19 -0700 | [diff] [blame] | 1457 | STATIC void genArrayPut(CompilationUnit* cUnit, MIR* mir, OpSize size, |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 1458 | RegLocation rlArray, RegLocation rlIndex, |
| 1459 | RegLocation rlSrc, int scale) |
| 1460 | { |
| 1461 | RegisterClass regClass = oatRegClassBySize(size); |
buzbee | c143c55 | 2011-08-20 17:38:58 -0700 | [diff] [blame] | 1462 | int lenOffset = Array::LengthOffset().Int32Value(); |
| 1463 | int dataOffset = Array::DataOffset().Int32Value(); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 1464 | |
| 1465 | int regPtr; |
| 1466 | rlArray = loadValue(cUnit, rlArray, kCoreReg); |
| 1467 | rlIndex = loadValue(cUnit, rlIndex, kCoreReg); |
| 1468 | |
| 1469 | if (oatIsTemp(cUnit, rlArray.lowReg)) { |
| 1470 | oatClobber(cUnit, rlArray.lowReg); |
| 1471 | regPtr = rlArray.lowReg; |
| 1472 | } else { |
| 1473 | regPtr = oatAllocTemp(cUnit); |
| 1474 | genRegCopy(cUnit, regPtr, rlArray.lowReg); |
| 1475 | } |
| 1476 | |
| 1477 | /* null object? */ |
buzbee | 43a3642 | 2011-09-14 14:00:13 -0700 | [diff] [blame] | 1478 | genNullCheck(cUnit, rlArray.sRegLow, rlArray.lowReg, mir); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 1479 | |
buzbee | 43a3642 | 2011-09-14 14:00:13 -0700 | [diff] [blame] | 1480 | if (!(mir->optimizationFlags & MIR_IGNORE_RANGE_CHECK)) { |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 1481 | int regLen = oatAllocTemp(cUnit); |
| 1482 | //NOTE: max live temps(4) here. |
| 1483 | /* Get len */ |
| 1484 | loadWordDisp(cUnit, rlArray.lowReg, lenOffset, regLen); |
| 1485 | /* regPtr -> array data */ |
| 1486 | opRegImm(cUnit, kOpAdd, regPtr, dataOffset); |
buzbee | ec5adf3 | 2011-09-11 15:25:43 -0700 | [diff] [blame] | 1487 | genRegRegCheck(cUnit, kArmCondCs, rlIndex.lowReg, regLen, mir, |
buzbee | 5ade1d2 | 2011-09-09 14:44:52 -0700 | [diff] [blame] | 1488 | kArmThrowArrayBounds); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 1489 | oatFreeTemp(cUnit, regLen); |
| 1490 | } else { |
| 1491 | /* regPtr -> array data */ |
| 1492 | opRegImm(cUnit, kOpAdd, regPtr, dataOffset); |
| 1493 | } |
| 1494 | /* at this point, regPtr points to array, 2 live temps */ |
| 1495 | if ((size == kLong) || (size == kDouble)) { |
buzbee | 5ade1d2 | 2011-09-09 14:44:52 -0700 | [diff] [blame] | 1496 | //TUNING: specific wide routine that can handle fp regs |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 1497 | if (scale) { |
| 1498 | int rNewIndex = oatAllocTemp(cUnit); |
| 1499 | opRegRegImm(cUnit, kOpLsl, rNewIndex, rlIndex.lowReg, scale); |
| 1500 | opRegReg(cUnit, kOpAdd, regPtr, rNewIndex); |
| 1501 | oatFreeTemp(cUnit, rNewIndex); |
| 1502 | } else { |
| 1503 | opRegReg(cUnit, kOpAdd, regPtr, rlIndex.lowReg); |
| 1504 | } |
| 1505 | rlSrc = loadValueWide(cUnit, rlSrc, regClass); |
| 1506 | |
| 1507 | storePair(cUnit, regPtr, rlSrc.lowReg, rlSrc.highReg); |
| 1508 | |
| 1509 | oatFreeTemp(cUnit, regPtr); |
| 1510 | } else { |
| 1511 | rlSrc = loadValue(cUnit, rlSrc, regClass); |
| 1512 | |
| 1513 | storeBaseIndexed(cUnit, regPtr, rlIndex.lowReg, rlSrc.lowReg, |
| 1514 | scale, size); |
| 1515 | } |
| 1516 | } |
| 1517 | |
buzbee | ed3e930 | 2011-09-23 17:34:19 -0700 | [diff] [blame] | 1518 | STATIC bool genShiftOpLong(CompilationUnit* cUnit, MIR* mir, |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 1519 | RegLocation rlDest, RegLocation rlSrc1, |
| 1520 | RegLocation rlShift) |
| 1521 | { |
buzbee | 5433072 | 2011-08-23 16:46:55 -0700 | [diff] [blame] | 1522 | int funcOffset; |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 1523 | |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 1524 | switch( mir->dalvikInsn.opcode) { |
| 1525 | case OP_SHL_LONG: |
| 1526 | case OP_SHL_LONG_2ADDR: |
buzbee | 5433072 | 2011-08-23 16:46:55 -0700 | [diff] [blame] | 1527 | funcOffset = OFFSETOF_MEMBER(Thread, pShlLong); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 1528 | break; |
| 1529 | case OP_SHR_LONG: |
| 1530 | case OP_SHR_LONG_2ADDR: |
buzbee | 5433072 | 2011-08-23 16:46:55 -0700 | [diff] [blame] | 1531 | funcOffset = OFFSETOF_MEMBER(Thread, pShrLong); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 1532 | break; |
| 1533 | case OP_USHR_LONG: |
| 1534 | case OP_USHR_LONG_2ADDR: |
buzbee | 5433072 | 2011-08-23 16:46:55 -0700 | [diff] [blame] | 1535 | funcOffset = OFFSETOF_MEMBER(Thread, pUshrLong); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 1536 | break; |
| 1537 | default: |
buzbee | 5433072 | 2011-08-23 16:46:55 -0700 | [diff] [blame] | 1538 | LOG(FATAL) << "Unexpected case"; |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 1539 | return true; |
| 1540 | } |
buzbee | 5433072 | 2011-08-23 16:46:55 -0700 | [diff] [blame] | 1541 | oatFlushAllRegs(cUnit); /* Send everything to home location */ |
| 1542 | loadWordDisp(cUnit, rSELF, funcOffset, rLR); |
| 1543 | loadValueDirectWideFixed(cUnit, rlSrc1, r0, r1); |
| 1544 | loadValueDirect(cUnit, rlShift, r2); |
Ian Rogers | ff1ed47 | 2011-09-20 13:46:24 -0700 | [diff] [blame] | 1545 | callRuntimeHelper(cUnit, rLR); |
buzbee | 5433072 | 2011-08-23 16:46:55 -0700 | [diff] [blame] | 1546 | RegLocation rlResult = oatGetReturnWide(cUnit); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 1547 | storeValueWide(cUnit, rlDest, rlResult); |
| 1548 | return false; |
| 1549 | } |
| 1550 | |
buzbee | ed3e930 | 2011-09-23 17:34:19 -0700 | [diff] [blame] | 1551 | STATIC bool genArithOpLong(CompilationUnit* cUnit, MIR* mir, |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 1552 | RegLocation rlDest, RegLocation rlSrc1, |
| 1553 | RegLocation rlSrc2) |
| 1554 | { |
| 1555 | RegLocation rlResult; |
| 1556 | OpKind firstOp = kOpBkpt; |
| 1557 | OpKind secondOp = kOpBkpt; |
| 1558 | bool callOut = false; |
buzbee | 58f9274 | 2011-10-01 11:22:17 -0700 | [diff] [blame] | 1559 | bool checkZero = false; |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 1560 | int funcOffset; |
| 1561 | int retReg = r0; |
| 1562 | |
| 1563 | switch (mir->dalvikInsn.opcode) { |
| 1564 | case OP_NOT_LONG: |
| 1565 | rlSrc2 = loadValueWide(cUnit, rlSrc2, kCoreReg); |
| 1566 | rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true); |
buzbee | b29e4d1 | 2011-09-26 15:05:48 -0700 | [diff] [blame] | 1567 | // Check for destructive overlap |
| 1568 | if (rlResult.lowReg == rlSrc2.highReg) { |
| 1569 | int tReg = oatAllocTemp(cUnit); |
| 1570 | genRegCopy(cUnit, tReg, rlSrc2.highReg); |
| 1571 | opRegReg(cUnit, kOpMvn, rlResult.lowReg, rlSrc2.lowReg); |
| 1572 | opRegReg(cUnit, kOpMvn, rlResult.highReg, tReg); |
| 1573 | oatFreeTemp(cUnit, tReg); |
| 1574 | } else { |
| 1575 | opRegReg(cUnit, kOpMvn, rlResult.lowReg, rlSrc2.lowReg); |
| 1576 | opRegReg(cUnit, kOpMvn, rlResult.highReg, rlSrc2.highReg); |
| 1577 | } |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 1578 | storeValueWide(cUnit, rlDest, rlResult); |
| 1579 | return false; |
| 1580 | break; |
| 1581 | case OP_ADD_LONG: |
| 1582 | case OP_ADD_LONG_2ADDR: |
| 1583 | firstOp = kOpAdd; |
| 1584 | secondOp = kOpAdc; |
| 1585 | break; |
| 1586 | case OP_SUB_LONG: |
| 1587 | case OP_SUB_LONG_2ADDR: |
| 1588 | firstOp = kOpSub; |
| 1589 | secondOp = kOpSbc; |
| 1590 | break; |
| 1591 | case OP_MUL_LONG: |
| 1592 | case OP_MUL_LONG_2ADDR: |
buzbee | 439c4fa | 2011-08-27 15:59:07 -0700 | [diff] [blame] | 1593 | callOut = true; |
| 1594 | retReg = r0; |
| 1595 | funcOffset = OFFSETOF_MEMBER(Thread, pLmul); |
| 1596 | break; |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 1597 | case OP_DIV_LONG: |
| 1598 | case OP_DIV_LONG_2ADDR: |
| 1599 | callOut = true; |
buzbee | 58f9274 | 2011-10-01 11:22:17 -0700 | [diff] [blame] | 1600 | checkZero = true; |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 1601 | retReg = r0; |
| 1602 | funcOffset = OFFSETOF_MEMBER(Thread, pLdivmod); |
| 1603 | break; |
| 1604 | /* NOTE - result is in r2/r3 instead of r0/r1 */ |
| 1605 | case OP_REM_LONG: |
| 1606 | case OP_REM_LONG_2ADDR: |
| 1607 | callOut = true; |
buzbee | 58f9274 | 2011-10-01 11:22:17 -0700 | [diff] [blame] | 1608 | checkZero = true; |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 1609 | funcOffset = OFFSETOF_MEMBER(Thread, pLdivmod); |
| 1610 | retReg = r2; |
| 1611 | break; |
| 1612 | case OP_AND_LONG_2ADDR: |
| 1613 | case OP_AND_LONG: |
| 1614 | firstOp = kOpAnd; |
| 1615 | secondOp = kOpAnd; |
| 1616 | break; |
| 1617 | case OP_OR_LONG: |
| 1618 | case OP_OR_LONG_2ADDR: |
| 1619 | firstOp = kOpOr; |
| 1620 | secondOp = kOpOr; |
| 1621 | break; |
| 1622 | case OP_XOR_LONG: |
| 1623 | case OP_XOR_LONG_2ADDR: |
| 1624 | firstOp = kOpXor; |
| 1625 | secondOp = kOpXor; |
| 1626 | break; |
| 1627 | case OP_NEG_LONG: { |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 1628 | rlSrc2 = loadValueWide(cUnit, rlSrc2, kCoreReg); |
| 1629 | rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true); |
buzbee | b29e4d1 | 2011-09-26 15:05:48 -0700 | [diff] [blame] | 1630 | int zReg = oatAllocTemp(cUnit); |
| 1631 | loadConstantNoClobber(cUnit, zReg, 0); |
| 1632 | // Check for destructive overlap |
| 1633 | if (rlResult.lowReg == rlSrc2.highReg) { |
| 1634 | int tReg = oatAllocTemp(cUnit); |
| 1635 | opRegRegReg(cUnit, kOpSub, rlResult.lowReg, |
| 1636 | zReg, rlSrc2.lowReg); |
| 1637 | opRegRegReg(cUnit, kOpSbc, rlResult.highReg, |
| 1638 | zReg, tReg); |
| 1639 | oatFreeTemp(cUnit, tReg); |
| 1640 | } else { |
| 1641 | opRegRegReg(cUnit, kOpSub, rlResult.lowReg, |
| 1642 | zReg, rlSrc2.lowReg); |
| 1643 | opRegRegReg(cUnit, kOpSbc, rlResult.highReg, |
| 1644 | zReg, rlSrc2.highReg); |
| 1645 | } |
| 1646 | oatFreeTemp(cUnit, zReg); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 1647 | storeValueWide(cUnit, rlDest, rlResult); |
| 1648 | return false; |
| 1649 | } |
| 1650 | default: |
| 1651 | LOG(FATAL) << "Invalid long arith op"; |
| 1652 | } |
| 1653 | if (!callOut) { |
| 1654 | genLong3Addr(cUnit, mir, firstOp, secondOp, rlDest, rlSrc1, rlSrc2); |
| 1655 | } else { |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 1656 | oatFlushAllRegs(cUnit); /* Send everything to home location */ |
buzbee | 58f9274 | 2011-10-01 11:22:17 -0700 | [diff] [blame] | 1657 | if (checkZero) { |
| 1658 | loadValueDirectWideFixed(cUnit, rlSrc2, r2, r3); |
| 1659 | loadWordDisp(cUnit, rSELF, funcOffset, rLR); |
| 1660 | loadValueDirectWideFixed(cUnit, rlSrc1, r0, r1); |
| 1661 | int tReg = oatAllocTemp(cUnit); |
| 1662 | newLIR4(cUnit, kThumb2OrrRRRs, tReg, r2, r3, 0); |
| 1663 | oatFreeTemp(cUnit, tReg); |
| 1664 | genCheck(cUnit, kArmCondEq, mir, kArmThrowDivZero); |
| 1665 | } else { |
| 1666 | loadWordDisp(cUnit, rSELF, funcOffset, rLR); |
| 1667 | loadValueDirectWideFixed(cUnit, rlSrc1, r0, r1); |
| 1668 | loadValueDirectWideFixed(cUnit, rlSrc2, r2, r3); |
| 1669 | } |
Ian Rogers | ff1ed47 | 2011-09-20 13:46:24 -0700 | [diff] [blame] | 1670 | callRuntimeHelper(cUnit, rLR); |
buzbee | 58f9274 | 2011-10-01 11:22:17 -0700 | [diff] [blame] | 1671 | // Adjust return regs in to handle case of rem returning r2/r3 |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 1672 | if (retReg == r0) |
| 1673 | rlResult = oatGetReturnWide(cUnit); |
| 1674 | else |
| 1675 | rlResult = oatGetReturnWideAlt(cUnit); |
| 1676 | storeValueWide(cUnit, rlDest, rlResult); |
| 1677 | } |
| 1678 | return false; |
| 1679 | } |
| 1680 | |
buzbee | ed3e930 | 2011-09-23 17:34:19 -0700 | [diff] [blame] | 1681 | STATIC bool genArithOpInt(CompilationUnit* cUnit, MIR* mir, |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 1682 | RegLocation rlDest, RegLocation rlSrc1, |
| 1683 | RegLocation rlSrc2) |
| 1684 | { |
| 1685 | OpKind op = kOpBkpt; |
| 1686 | bool callOut = false; |
| 1687 | bool checkZero = false; |
| 1688 | bool unary = false; |
| 1689 | int retReg = r0; |
| 1690 | int funcOffset; |
| 1691 | RegLocation rlResult; |
| 1692 | bool shiftOp = false; |
| 1693 | |
| 1694 | switch (mir->dalvikInsn.opcode) { |
| 1695 | case OP_NEG_INT: |
| 1696 | op = kOpNeg; |
| 1697 | unary = true; |
| 1698 | break; |
| 1699 | case OP_NOT_INT: |
| 1700 | op = kOpMvn; |
| 1701 | unary = true; |
| 1702 | break; |
| 1703 | case OP_ADD_INT: |
| 1704 | case OP_ADD_INT_2ADDR: |
| 1705 | op = kOpAdd; |
| 1706 | break; |
| 1707 | case OP_SUB_INT: |
| 1708 | case OP_SUB_INT_2ADDR: |
| 1709 | op = kOpSub; |
| 1710 | break; |
| 1711 | case OP_MUL_INT: |
| 1712 | case OP_MUL_INT_2ADDR: |
| 1713 | op = kOpMul; |
| 1714 | break; |
| 1715 | case OP_DIV_INT: |
| 1716 | case OP_DIV_INT_2ADDR: |
| 1717 | callOut = true; |
| 1718 | checkZero = true; |
| 1719 | funcOffset = OFFSETOF_MEMBER(Thread, pIdiv); |
| 1720 | retReg = r0; |
| 1721 | break; |
| 1722 | /* NOTE: returns in r1 */ |
| 1723 | case OP_REM_INT: |
| 1724 | case OP_REM_INT_2ADDR: |
| 1725 | callOut = true; |
| 1726 | checkZero = true; |
| 1727 | funcOffset = OFFSETOF_MEMBER(Thread, pIdivmod); |
| 1728 | retReg = r1; |
| 1729 | break; |
| 1730 | case OP_AND_INT: |
| 1731 | case OP_AND_INT_2ADDR: |
| 1732 | op = kOpAnd; |
| 1733 | break; |
| 1734 | case OP_OR_INT: |
| 1735 | case OP_OR_INT_2ADDR: |
| 1736 | op = kOpOr; |
| 1737 | break; |
| 1738 | case OP_XOR_INT: |
| 1739 | case OP_XOR_INT_2ADDR: |
| 1740 | op = kOpXor; |
| 1741 | break; |
| 1742 | case OP_SHL_INT: |
| 1743 | case OP_SHL_INT_2ADDR: |
| 1744 | shiftOp = true; |
| 1745 | op = kOpLsl; |
| 1746 | break; |
| 1747 | case OP_SHR_INT: |
| 1748 | case OP_SHR_INT_2ADDR: |
| 1749 | shiftOp = true; |
| 1750 | op = kOpAsr; |
| 1751 | break; |
| 1752 | case OP_USHR_INT: |
| 1753 | case OP_USHR_INT_2ADDR: |
| 1754 | shiftOp = true; |
| 1755 | op = kOpLsr; |
| 1756 | break; |
| 1757 | default: |
| 1758 | LOG(FATAL) << "Invalid word arith op: " << |
| 1759 | (int)mir->dalvikInsn.opcode; |
| 1760 | } |
| 1761 | if (!callOut) { |
| 1762 | rlSrc1 = loadValue(cUnit, rlSrc1, kCoreReg); |
| 1763 | if (unary) { |
| 1764 | rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true); |
| 1765 | opRegReg(cUnit, op, rlResult.lowReg, |
| 1766 | rlSrc1.lowReg); |
| 1767 | } else { |
| 1768 | rlSrc2 = loadValue(cUnit, rlSrc2, kCoreReg); |
| 1769 | if (shiftOp) { |
| 1770 | int tReg = oatAllocTemp(cUnit); |
| 1771 | opRegRegImm(cUnit, kOpAnd, tReg, rlSrc2.lowReg, 31); |
| 1772 | rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true); |
| 1773 | opRegRegReg(cUnit, op, rlResult.lowReg, |
| 1774 | rlSrc1.lowReg, tReg); |
| 1775 | oatFreeTemp(cUnit, tReg); |
| 1776 | } else { |
| 1777 | rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true); |
| 1778 | opRegRegReg(cUnit, op, rlResult.lowReg, |
| 1779 | rlSrc1.lowReg, rlSrc2.lowReg); |
| 1780 | } |
| 1781 | } |
| 1782 | storeValue(cUnit, rlDest, rlResult); |
| 1783 | } else { |
| 1784 | RegLocation rlResult; |
| 1785 | oatFlushAllRegs(cUnit); /* Send everything to home location */ |
| 1786 | loadValueDirectFixed(cUnit, rlSrc2, r1); |
| 1787 | loadWordDisp(cUnit, rSELF, funcOffset, rLR); |
| 1788 | loadValueDirectFixed(cUnit, rlSrc1, r0); |
| 1789 | if (checkZero) { |
buzbee | 5ade1d2 | 2011-09-09 14:44:52 -0700 | [diff] [blame] | 1790 | genImmedCheck(cUnit, kArmCondEq, r1, 0, mir, kArmThrowDivZero); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 1791 | } |
Ian Rogers | ff1ed47 | 2011-09-20 13:46:24 -0700 | [diff] [blame] | 1792 | callRuntimeHelper(cUnit, rLR); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 1793 | if (retReg == r0) |
| 1794 | rlResult = oatGetReturn(cUnit); |
| 1795 | else |
| 1796 | rlResult = oatGetReturnAlt(cUnit); |
| 1797 | storeValue(cUnit, rlDest, rlResult); |
| 1798 | } |
| 1799 | return false; |
| 1800 | } |
| 1801 | |
buzbee | c1f4504 | 2011-09-21 16:03:19 -0700 | [diff] [blame] | 1802 | /* Check if we need to check for pending suspend request */ |
buzbee | ed3e930 | 2011-09-23 17:34:19 -0700 | [diff] [blame] | 1803 | STATIC void genSuspendTest(CompilationUnit* cUnit, MIR* mir) |
buzbee | c1f4504 | 2011-09-21 16:03:19 -0700 | [diff] [blame] | 1804 | { |
Ian Rogers | a3760aa | 2011-11-14 14:32:37 -0800 | [diff] [blame] | 1805 | if (NO_SUSPEND || (mir->optimizationFlags & MIR_IGNORE_SUSPEND_CHECK)) { |
buzbee | c1f4504 | 2011-09-21 16:03:19 -0700 | [diff] [blame] | 1806 | return; |
| 1807 | } |
buzbee | 6181f79 | 2011-09-29 11:14:04 -0700 | [diff] [blame] | 1808 | oatFlushAllRegs(cUnit); |
buzbee | c1f4504 | 2011-09-21 16:03:19 -0700 | [diff] [blame] | 1809 | newLIR2(cUnit, kThumbSubRI8, rSUSPEND, 1); |
| 1810 | ArmLIR* branch = opCondBranch(cUnit, kArmCondEq); |
| 1811 | ArmLIR* retLab = newLIR0(cUnit, kArmPseudoTargetLabel); |
| 1812 | retLab->defMask = ENCODE_ALL; |
| 1813 | ArmLIR* target = (ArmLIR*)oatNew(sizeof(ArmLIR), true); |
| 1814 | target->generic.dalvikOffset = cUnit->currentDalvikOffset; |
| 1815 | target->opcode = kArmPseudoSuspendTarget; |
| 1816 | target->operands[0] = (intptr_t)retLab; |
| 1817 | target->operands[1] = mir->offset; |
| 1818 | branch->generic.target = (LIR*)target; |
| 1819 | oatInsertGrowableList(&cUnit->suspendLaunchpads, (intptr_t)target); |
| 1820 | } |
| 1821 | |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 1822 | /* |
| 1823 | * The following are the first-level codegen routines that analyze the format |
| 1824 | * of each bytecode then either dispatch special purpose codegen routines |
| 1825 | * or produce corresponding Thumb instructions directly. |
| 1826 | */ |
| 1827 | |
buzbee | ed3e930 | 2011-09-23 17:34:19 -0700 | [diff] [blame] | 1828 | STATIC bool isPowerOfTwo(int x) |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 1829 | { |
| 1830 | return (x & (x - 1)) == 0; |
| 1831 | } |
| 1832 | |
| 1833 | // Returns true if no more than two bits are set in 'x'. |
buzbee | ed3e930 | 2011-09-23 17:34:19 -0700 | [diff] [blame] | 1834 | STATIC bool isPopCountLE2(unsigned int x) |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 1835 | { |
| 1836 | x &= x - 1; |
| 1837 | return (x & (x - 1)) == 0; |
| 1838 | } |
| 1839 | |
| 1840 | // Returns the index of the lowest set bit in 'x'. |
buzbee | ed3e930 | 2011-09-23 17:34:19 -0700 | [diff] [blame] | 1841 | STATIC int lowestSetBit(unsigned int x) { |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 1842 | int bit_posn = 0; |
| 1843 | while ((x & 0xf) == 0) { |
| 1844 | bit_posn += 4; |
| 1845 | x >>= 4; |
| 1846 | } |
| 1847 | while ((x & 1) == 0) { |
| 1848 | bit_posn++; |
| 1849 | x >>= 1; |
| 1850 | } |
| 1851 | return bit_posn; |
| 1852 | } |
| 1853 | |
| 1854 | // Returns true if it added instructions to 'cUnit' to divide 'rlSrc' by 'lit' |
| 1855 | // and store the result in 'rlDest'. |
buzbee | ed3e930 | 2011-09-23 17:34:19 -0700 | [diff] [blame] | 1856 | STATIC bool handleEasyDivide(CompilationUnit* cUnit, Opcode dalvikOpcode, |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 1857 | RegLocation rlSrc, RegLocation rlDest, int lit) |
| 1858 | { |
| 1859 | if (lit < 2 || !isPowerOfTwo(lit)) { |
| 1860 | return false; |
| 1861 | } |
| 1862 | int k = lowestSetBit(lit); |
| 1863 | if (k >= 30) { |
| 1864 | // Avoid special cases. |
| 1865 | return false; |
| 1866 | } |
| 1867 | bool div = (dalvikOpcode == OP_DIV_INT_LIT8 || |
| 1868 | dalvikOpcode == OP_DIV_INT_LIT16); |
| 1869 | rlSrc = loadValue(cUnit, rlSrc, kCoreReg); |
| 1870 | RegLocation rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true); |
| 1871 | if (div) { |
| 1872 | int tReg = oatAllocTemp(cUnit); |
| 1873 | if (lit == 2) { |
| 1874 | // Division by 2 is by far the most common division by constant. |
| 1875 | opRegRegImm(cUnit, kOpLsr, tReg, rlSrc.lowReg, 32 - k); |
| 1876 | opRegRegReg(cUnit, kOpAdd, tReg, tReg, rlSrc.lowReg); |
| 1877 | opRegRegImm(cUnit, kOpAsr, rlResult.lowReg, tReg, k); |
| 1878 | } else { |
| 1879 | opRegRegImm(cUnit, kOpAsr, tReg, rlSrc.lowReg, 31); |
| 1880 | opRegRegImm(cUnit, kOpLsr, tReg, tReg, 32 - k); |
| 1881 | opRegRegReg(cUnit, kOpAdd, tReg, tReg, rlSrc.lowReg); |
| 1882 | opRegRegImm(cUnit, kOpAsr, rlResult.lowReg, tReg, k); |
| 1883 | } |
| 1884 | } else { |
| 1885 | int cReg = oatAllocTemp(cUnit); |
| 1886 | loadConstant(cUnit, cReg, lit - 1); |
| 1887 | int tReg1 = oatAllocTemp(cUnit); |
| 1888 | int tReg2 = oatAllocTemp(cUnit); |
| 1889 | if (lit == 2) { |
| 1890 | opRegRegImm(cUnit, kOpLsr, tReg1, rlSrc.lowReg, 32 - k); |
| 1891 | opRegRegReg(cUnit, kOpAdd, tReg2, tReg1, rlSrc.lowReg); |
| 1892 | opRegRegReg(cUnit, kOpAnd, tReg2, tReg2, cReg); |
| 1893 | opRegRegReg(cUnit, kOpSub, rlResult.lowReg, tReg2, tReg1); |
| 1894 | } else { |
| 1895 | opRegRegImm(cUnit, kOpAsr, tReg1, rlSrc.lowReg, 31); |
| 1896 | opRegRegImm(cUnit, kOpLsr, tReg1, tReg1, 32 - k); |
| 1897 | opRegRegReg(cUnit, kOpAdd, tReg2, tReg1, rlSrc.lowReg); |
| 1898 | opRegRegReg(cUnit, kOpAnd, tReg2, tReg2, cReg); |
| 1899 | opRegRegReg(cUnit, kOpSub, rlResult.lowReg, tReg2, tReg1); |
| 1900 | } |
| 1901 | } |
| 1902 | storeValue(cUnit, rlDest, rlResult); |
| 1903 | return true; |
| 1904 | } |
| 1905 | |
| 1906 | // Returns true if it added instructions to 'cUnit' to multiply 'rlSrc' by 'lit' |
| 1907 | // and store the result in 'rlDest'. |
buzbee | ed3e930 | 2011-09-23 17:34:19 -0700 | [diff] [blame] | 1908 | STATIC bool handleEasyMultiply(CompilationUnit* cUnit, |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 1909 | RegLocation rlSrc, RegLocation rlDest, int lit) |
| 1910 | { |
| 1911 | // Can we simplify this multiplication? |
| 1912 | bool powerOfTwo = false; |
| 1913 | bool popCountLE2 = false; |
| 1914 | bool powerOfTwoMinusOne = false; |
| 1915 | if (lit < 2) { |
| 1916 | // Avoid special cases. |
| 1917 | return false; |
| 1918 | } else if (isPowerOfTwo(lit)) { |
| 1919 | powerOfTwo = true; |
| 1920 | } else if (isPopCountLE2(lit)) { |
| 1921 | popCountLE2 = true; |
| 1922 | } else if (isPowerOfTwo(lit + 1)) { |
| 1923 | powerOfTwoMinusOne = true; |
| 1924 | } else { |
| 1925 | return false; |
| 1926 | } |
| 1927 | rlSrc = loadValue(cUnit, rlSrc, kCoreReg); |
| 1928 | RegLocation rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true); |
| 1929 | if (powerOfTwo) { |
| 1930 | // Shift. |
| 1931 | opRegRegImm(cUnit, kOpLsl, rlResult.lowReg, rlSrc.lowReg, |
| 1932 | lowestSetBit(lit)); |
| 1933 | } else if (popCountLE2) { |
| 1934 | // Shift and add and shift. |
| 1935 | int firstBit = lowestSetBit(lit); |
| 1936 | int secondBit = lowestSetBit(lit ^ (1 << firstBit)); |
| 1937 | genMultiplyByTwoBitMultiplier(cUnit, rlSrc, rlResult, lit, |
| 1938 | firstBit, secondBit); |
| 1939 | } else { |
| 1940 | // Reverse subtract: (src << (shift + 1)) - src. |
buzbee | ed3e930 | 2011-09-23 17:34:19 -0700 | [diff] [blame] | 1941 | DCHECK(powerOfTwoMinusOne); |
buzbee | 5ade1d2 | 2011-09-09 14:44:52 -0700 | [diff] [blame] | 1942 | // TUNING: rsb dst, src, src lsl#lowestSetBit(lit + 1) |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 1943 | int tReg = oatAllocTemp(cUnit); |
| 1944 | opRegRegImm(cUnit, kOpLsl, tReg, rlSrc.lowReg, lowestSetBit(lit + 1)); |
| 1945 | opRegRegReg(cUnit, kOpSub, rlResult.lowReg, tReg, rlSrc.lowReg); |
| 1946 | } |
| 1947 | storeValue(cUnit, rlDest, rlResult); |
| 1948 | return true; |
| 1949 | } |
| 1950 | |
buzbee | ed3e930 | 2011-09-23 17:34:19 -0700 | [diff] [blame] | 1951 | STATIC bool genArithOpIntLit(CompilationUnit* cUnit, MIR* mir, |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 1952 | RegLocation rlDest, RegLocation rlSrc, |
| 1953 | int lit) |
| 1954 | { |
| 1955 | Opcode dalvikOpcode = mir->dalvikInsn.opcode; |
| 1956 | RegLocation rlResult; |
| 1957 | OpKind op = (OpKind)0; /* Make gcc happy */ |
| 1958 | int shiftOp = false; |
| 1959 | bool isDiv = false; |
| 1960 | int funcOffset; |
| 1961 | |
| 1962 | switch (dalvikOpcode) { |
| 1963 | case OP_RSUB_INT_LIT8: |
| 1964 | case OP_RSUB_INT: { |
| 1965 | int tReg; |
| 1966 | //TUNING: add support for use of Arm rsub op |
| 1967 | rlSrc = loadValue(cUnit, rlSrc, kCoreReg); |
| 1968 | tReg = oatAllocTemp(cUnit); |
| 1969 | loadConstant(cUnit, tReg, lit); |
| 1970 | rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true); |
| 1971 | opRegRegReg(cUnit, kOpSub, rlResult.lowReg, |
| 1972 | tReg, rlSrc.lowReg); |
| 1973 | storeValue(cUnit, rlDest, rlResult); |
| 1974 | return false; |
| 1975 | break; |
| 1976 | } |
| 1977 | |
| 1978 | case OP_ADD_INT_LIT8: |
| 1979 | case OP_ADD_INT_LIT16: |
| 1980 | op = kOpAdd; |
| 1981 | break; |
| 1982 | case OP_MUL_INT_LIT8: |
| 1983 | case OP_MUL_INT_LIT16: { |
| 1984 | if (handleEasyMultiply(cUnit, rlSrc, rlDest, lit)) { |
| 1985 | return false; |
| 1986 | } |
| 1987 | op = kOpMul; |
| 1988 | break; |
| 1989 | } |
| 1990 | case OP_AND_INT_LIT8: |
| 1991 | case OP_AND_INT_LIT16: |
| 1992 | op = kOpAnd; |
| 1993 | break; |
| 1994 | case OP_OR_INT_LIT8: |
| 1995 | case OP_OR_INT_LIT16: |
| 1996 | op = kOpOr; |
| 1997 | break; |
| 1998 | case OP_XOR_INT_LIT8: |
| 1999 | case OP_XOR_INT_LIT16: |
| 2000 | op = kOpXor; |
| 2001 | break; |
| 2002 | case OP_SHL_INT_LIT8: |
| 2003 | lit &= 31; |
| 2004 | shiftOp = true; |
| 2005 | op = kOpLsl; |
| 2006 | break; |
| 2007 | case OP_SHR_INT_LIT8: |
| 2008 | lit &= 31; |
| 2009 | shiftOp = true; |
| 2010 | op = kOpAsr; |
| 2011 | break; |
| 2012 | case OP_USHR_INT_LIT8: |
| 2013 | lit &= 31; |
| 2014 | shiftOp = true; |
| 2015 | op = kOpLsr; |
| 2016 | break; |
| 2017 | |
| 2018 | case OP_DIV_INT_LIT8: |
| 2019 | case OP_DIV_INT_LIT16: |
| 2020 | case OP_REM_INT_LIT8: |
| 2021 | case OP_REM_INT_LIT16: |
| 2022 | if (lit == 0) { |
buzbee | 5ade1d2 | 2011-09-09 14:44:52 -0700 | [diff] [blame] | 2023 | genImmedCheck(cUnit, kArmCondAl, 0, 0, mir, kArmThrowDivZero); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 2024 | return false; |
| 2025 | } |
| 2026 | if (handleEasyDivide(cUnit, dalvikOpcode, rlSrc, rlDest, lit)) { |
| 2027 | return false; |
| 2028 | } |
| 2029 | oatFlushAllRegs(cUnit); /* Everything to home location */ |
| 2030 | loadValueDirectFixed(cUnit, rlSrc, r0); |
| 2031 | oatClobber(cUnit, r0); |
| 2032 | if ((dalvikOpcode == OP_DIV_INT_LIT8) || |
| 2033 | (dalvikOpcode == OP_DIV_INT_LIT16)) { |
| 2034 | funcOffset = OFFSETOF_MEMBER(Thread, pIdiv); |
| 2035 | isDiv = true; |
| 2036 | } else { |
| 2037 | funcOffset = OFFSETOF_MEMBER(Thread, pIdivmod); |
| 2038 | isDiv = false; |
| 2039 | } |
| 2040 | loadWordDisp(cUnit, rSELF, funcOffset, rLR); |
| 2041 | loadConstant(cUnit, r1, lit); |
Ian Rogers | ff1ed47 | 2011-09-20 13:46:24 -0700 | [diff] [blame] | 2042 | callRuntimeHelper(cUnit, rLR); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 2043 | if (isDiv) |
| 2044 | rlResult = oatGetReturn(cUnit); |
| 2045 | else |
| 2046 | rlResult = oatGetReturnAlt(cUnit); |
| 2047 | storeValue(cUnit, rlDest, rlResult); |
| 2048 | return false; |
| 2049 | break; |
| 2050 | default: |
| 2051 | return true; |
| 2052 | } |
| 2053 | rlSrc = loadValue(cUnit, rlSrc, kCoreReg); |
| 2054 | rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true); |
| 2055 | // Avoid shifts by literal 0 - no support in Thumb. Change to copy |
| 2056 | if (shiftOp && (lit == 0)) { |
| 2057 | genRegCopy(cUnit, rlResult.lowReg, rlSrc.lowReg); |
| 2058 | } else { |
| 2059 | opRegRegImm(cUnit, op, rlResult.lowReg, rlSrc.lowReg, lit); |
| 2060 | } |
| 2061 | storeValue(cUnit, rlDest, rlResult); |
| 2062 | return false; |
| 2063 | } |
| 2064 | |
| 2065 | /* Architectural-specific debugging helpers go here */ |
| 2066 | void oatArchDump(void) |
| 2067 | { |
| 2068 | /* Print compiled opcode in this VM instance */ |
| 2069 | int i, start, streak; |
Elliott Hughes | 3b6baaa | 2011-10-14 19:13:56 -0700 | [diff] [blame] | 2070 | std::string buf; |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 2071 | |
| 2072 | streak = i = 0; |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 2073 | while (opcodeCoverage[i] == 0 && i < kNumPackedOpcodes) { |
| 2074 | i++; |
| 2075 | } |
| 2076 | if (i == kNumPackedOpcodes) { |
| 2077 | return; |
| 2078 | } |
| 2079 | for (start = i++, streak = 1; i < kNumPackedOpcodes; i++) { |
| 2080 | if (opcodeCoverage[i]) { |
| 2081 | streak++; |
| 2082 | } else { |
| 2083 | if (streak == 1) { |
Elliott Hughes | 3b6baaa | 2011-10-14 19:13:56 -0700 | [diff] [blame] | 2084 | StringAppendF(&buf, "%x,", start); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 2085 | } else { |
Elliott Hughes | 3b6baaa | 2011-10-14 19:13:56 -0700 | [diff] [blame] | 2086 | StringAppendF(&buf, "%x-%x,", start, start + streak - 1); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 2087 | } |
| 2088 | streak = 0; |
| 2089 | while (opcodeCoverage[i] == 0 && i < kNumPackedOpcodes) { |
| 2090 | i++; |
| 2091 | } |
| 2092 | if (i < kNumPackedOpcodes) { |
| 2093 | streak = 1; |
| 2094 | start = i; |
| 2095 | } |
| 2096 | } |
| 2097 | } |
| 2098 | if (streak) { |
| 2099 | if (streak == 1) { |
Elliott Hughes | 3b6baaa | 2011-10-14 19:13:56 -0700 | [diff] [blame] | 2100 | StringAppendF(&buf, "%x", start); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 2101 | } else { |
Elliott Hughes | 3b6baaa | 2011-10-14 19:13:56 -0700 | [diff] [blame] | 2102 | StringAppendF(&buf, "%x-%x", start, start + streak - 1); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 2103 | } |
| 2104 | } |
Elliott Hughes | 3b6baaa | 2011-10-14 19:13:56 -0700 | [diff] [blame] | 2105 | if (!buf.empty()) { |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 2106 | LOG(INFO) << "dalvik.vm.oat.op = " << buf; |
| 2107 | } |
| 2108 | } |
Elliott Hughes | 11d1b0c | 2012-01-23 16:57:47 -0800 | [diff] [blame] | 2109 | |
| 2110 | } // namespace art |