buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2012 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | namespace art { |
| 18 | |
| 19 | /* |
| 20 | * This source files contains "gen" codegen routines that should |
| 21 | * be applicable to most targets. Only mid-level support utilities |
| 22 | * and "op" calls may be used here. |
| 23 | */ |
| 24 | |
| 25 | #if defined(TARGET_ARM) |
buzbee | 82488f5 | 2012-03-02 08:20:26 -0800 | [diff] [blame] | 26 | LIR* opIT(CompilationUnit* cUnit, ArmConditionCode cond, const char* guide); |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 27 | #endif |
| 28 | |
| 29 | LIR* callRuntimeHelper(CompilationUnit* cUnit, int reg) |
| 30 | { |
| 31 | oatClobberCalleeSave(cUnit); |
| 32 | return opReg(cUnit, kOpBlx, reg); |
| 33 | } |
| 34 | |
| 35 | /* |
| 36 | * Generate an kPseudoBarrier marker to indicate the boundary of special |
| 37 | * blocks. |
| 38 | */ |
| 39 | void genBarrier(CompilationUnit* cUnit) |
| 40 | { |
| 41 | LIR* barrier = newLIR0(cUnit, kPseudoBarrier); |
| 42 | /* Mark all resources as being clobbered */ |
| 43 | barrier->defMask = -1; |
| 44 | } |
| 45 | |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 46 | |
| 47 | /* Generate unconditional branch instructions */ |
buzbee | 82488f5 | 2012-03-02 08:20:26 -0800 | [diff] [blame] | 48 | LIR* opUnconditionalBranch(CompilationUnit* cUnit, LIR* target) |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 49 | { |
Ian Rogers | 680b1bd | 2012-03-07 20:18:49 -0800 | [diff] [blame] | 50 | LIR* branch = opBranchUnconditional(cUnit, kOpUncondBr); |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 51 | branch->target = (LIR*) target; |
| 52 | return branch; |
| 53 | } |
| 54 | |
buzbee | 5de3494 | 2012-03-01 14:51:57 -0800 | [diff] [blame] | 55 | // FIXME: need to do some work to split out targets with |
| 56 | // condition codes and those without |
| 57 | #if defined(TARGET_ARM) || defined(TARGET_X86) |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 58 | LIR* genCheck(CompilationUnit* cUnit, ConditionCode cCode, MIR* mir, |
| 59 | ThrowKind kind) |
| 60 | { |
buzbee | a2ebdd7 | 2012-03-04 14:57:06 -0800 | [diff] [blame] | 61 | LIR* tgt = rawLIR(cUnit, 0, kPseudoThrowTarget, kind, |
| 62 | mir ? mir->offset : 0); |
buzbee | 82488f5 | 2012-03-02 08:20:26 -0800 | [diff] [blame] | 63 | LIR* branch = opCondBranch(cUnit, cCode, tgt); |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 64 | // Remember branch target - will process later |
| 65 | oatInsertGrowableList(cUnit, &cUnit->throwLaunchpads, (intptr_t)tgt); |
| 66 | return branch; |
| 67 | } |
buzbee | 5de3494 | 2012-03-01 14:51:57 -0800 | [diff] [blame] | 68 | #endif |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 69 | |
| 70 | LIR* genImmedCheck(CompilationUnit* cUnit, ConditionCode cCode, |
| 71 | int reg, int immVal, MIR* mir, ThrowKind kind) |
| 72 | { |
buzbee | a2ebdd7 | 2012-03-04 14:57:06 -0800 | [diff] [blame] | 73 | LIR* tgt = rawLIR(cUnit, 0, kPseudoThrowTarget, kind, mir->offset); |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 74 | LIR* branch; |
| 75 | if (cCode == kCondAl) { |
buzbee | 82488f5 | 2012-03-02 08:20:26 -0800 | [diff] [blame] | 76 | branch = opUnconditionalBranch(cUnit, tgt); |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 77 | } else { |
buzbee | 82488f5 | 2012-03-02 08:20:26 -0800 | [diff] [blame] | 78 | branch = opCmpImmBranch(cUnit, cCode, reg, immVal, tgt); |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 79 | } |
| 80 | // Remember branch target - will process later |
| 81 | oatInsertGrowableList(cUnit, &cUnit->throwLaunchpads, (intptr_t)tgt); |
| 82 | return branch; |
| 83 | } |
| 84 | |
| 85 | /* Perform null-check on a register. */ |
| 86 | LIR* genNullCheck(CompilationUnit* cUnit, int sReg, int mReg, MIR* mir) |
| 87 | { |
| 88 | if (!(cUnit->disableOpt & (1 << kNullCheckElimination)) && |
| 89 | mir->optimizationFlags & MIR_IGNORE_NULL_CHECK) { |
| 90 | return NULL; |
| 91 | } |
| 92 | return genImmedCheck(cUnit, kCondEq, mReg, 0, mir, kThrowNullPointer); |
| 93 | } |
| 94 | |
| 95 | /* Perform check on two registers */ |
| 96 | LIR* genRegRegCheck(CompilationUnit* cUnit, ConditionCode cCode, |
Ian Rogers | b5d09b2 | 2012-03-06 22:14:17 -0800 | [diff] [blame] | 97 | int reg1, int reg2, MIR* mir, ThrowKind kind) |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 98 | { |
buzbee | a2ebdd7 | 2012-03-04 14:57:06 -0800 | [diff] [blame] | 99 | LIR* tgt = rawLIR(cUnit, 0, kPseudoThrowTarget, kind, |
| 100 | mir ? mir->offset : 0, reg1, reg2); |
buzbee | 5de3494 | 2012-03-01 14:51:57 -0800 | [diff] [blame] | 101 | #if defined(TARGET_MIPS) |
buzbee | 82488f5 | 2012-03-02 08:20:26 -0800 | [diff] [blame] | 102 | LIR* branch = opCmpBranch(cUnit, cCode, reg1, reg2, tgt); |
buzbee | 5de3494 | 2012-03-01 14:51:57 -0800 | [diff] [blame] | 103 | #else |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 104 | opRegReg(cUnit, kOpCmp, reg1, reg2); |
buzbee | 82488f5 | 2012-03-02 08:20:26 -0800 | [diff] [blame] | 105 | LIR* branch = opCondBranch(cUnit, cCode, tgt); |
buzbee | 5de3494 | 2012-03-01 14:51:57 -0800 | [diff] [blame] | 106 | #endif |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 107 | // Remember branch target - will process later |
| 108 | oatInsertGrowableList(cUnit, &cUnit->throwLaunchpads, (intptr_t)tgt); |
| 109 | return branch; |
| 110 | } |
| 111 | |
| 112 | void genCompareAndBranch(CompilationUnit* cUnit, BasicBlock* bb, MIR* mir, |
| 113 | RegLocation rlSrc1, RegLocation rlSrc2, LIR* labelList) |
| 114 | { |
| 115 | ConditionCode cond; |
| 116 | rlSrc1 = loadValue(cUnit, rlSrc1, kCoreReg); |
| 117 | rlSrc2 = loadValue(cUnit, rlSrc2, kCoreReg); |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 118 | Instruction::Code opcode = mir->dalvikInsn.opcode; |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 119 | switch(opcode) { |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 120 | case Instruction::IF_EQ: |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 121 | cond = kCondEq; |
| 122 | break; |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 123 | case Instruction::IF_NE: |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 124 | cond = kCondNe; |
| 125 | break; |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 126 | case Instruction::IF_LT: |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 127 | cond = kCondLt; |
| 128 | break; |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 129 | case Instruction::IF_GE: |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 130 | cond = kCondGe; |
| 131 | break; |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 132 | case Instruction::IF_GT: |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 133 | cond = kCondGt; |
| 134 | break; |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 135 | case Instruction::IF_LE: |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 136 | cond = kCondLe; |
| 137 | break; |
| 138 | default: |
| 139 | cond = (ConditionCode)0; |
| 140 | LOG(FATAL) << "Unexpected opcode " << (int)opcode; |
| 141 | } |
buzbee | 5de3494 | 2012-03-01 14:51:57 -0800 | [diff] [blame] | 142 | #if defined(TARGET_MIPS) |
buzbee | 82488f5 | 2012-03-02 08:20:26 -0800 | [diff] [blame] | 143 | opCmpBranch(cUnit, cond, rlSrc1.lowReg, rlSrc2.lowReg, |
| 144 | &labelList[bb->taken->id]); |
buzbee | 5de3494 | 2012-03-01 14:51:57 -0800 | [diff] [blame] | 145 | #else |
| 146 | opRegReg(cUnit, kOpCmp, rlSrc1.lowReg, rlSrc2.lowReg); |
buzbee | 82488f5 | 2012-03-02 08:20:26 -0800 | [diff] [blame] | 147 | opCondBranch(cUnit, cond, &labelList[bb->taken->id]); |
buzbee | 5de3494 | 2012-03-01 14:51:57 -0800 | [diff] [blame] | 148 | #endif |
buzbee | 82488f5 | 2012-03-02 08:20:26 -0800 | [diff] [blame] | 149 | opUnconditionalBranch(cUnit, &labelList[bb->fallThrough->id]); |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | void genCompareZeroAndBranch(CompilationUnit* cUnit, BasicBlock* bb, MIR* mir, |
| 153 | RegLocation rlSrc, LIR* labelList) |
| 154 | { |
| 155 | ConditionCode cond; |
| 156 | rlSrc = loadValue(cUnit, rlSrc, kCoreReg); |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 157 | Instruction::Code opcode = mir->dalvikInsn.opcode; |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 158 | switch(opcode) { |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 159 | case Instruction::IF_EQZ: |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 160 | cond = kCondEq; |
| 161 | break; |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 162 | case Instruction::IF_NEZ: |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 163 | cond = kCondNe; |
| 164 | break; |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 165 | case Instruction::IF_LTZ: |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 166 | cond = kCondLt; |
| 167 | break; |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 168 | case Instruction::IF_GEZ: |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 169 | cond = kCondGe; |
| 170 | break; |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 171 | case Instruction::IF_GTZ: |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 172 | cond = kCondGt; |
| 173 | break; |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 174 | case Instruction::IF_LEZ: |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 175 | cond = kCondLe; |
| 176 | break; |
| 177 | default: |
| 178 | cond = (ConditionCode)0; |
| 179 | LOG(FATAL) << "Unexpected opcode " << (int)opcode; |
| 180 | } |
buzbee | 5de3494 | 2012-03-01 14:51:57 -0800 | [diff] [blame] | 181 | #if defined(TARGET_MIPS) |
buzbee | 82488f5 | 2012-03-02 08:20:26 -0800 | [diff] [blame] | 182 | opCmpImmBranch(cUnit, cond, rlSrc.lowReg, 0, &labelList[bb->taken->id]); |
buzbee | 5de3494 | 2012-03-01 14:51:57 -0800 | [diff] [blame] | 183 | #else |
| 184 | opRegImm(cUnit, kOpCmp, rlSrc.lowReg, 0); |
buzbee | 82488f5 | 2012-03-02 08:20:26 -0800 | [diff] [blame] | 185 | opCondBranch(cUnit, cond, &labelList[bb->taken->id]); |
buzbee | 5de3494 | 2012-03-01 14:51:57 -0800 | [diff] [blame] | 186 | #endif |
buzbee | 82488f5 | 2012-03-02 08:20:26 -0800 | [diff] [blame] | 187 | opUnconditionalBranch(cUnit, &labelList[bb->fallThrough->id]); |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 188 | } |
| 189 | |
| 190 | void genIntToLong(CompilationUnit* cUnit, MIR* mir, RegLocation rlDest, |
| 191 | RegLocation rlSrc) |
| 192 | { |
| 193 | RegLocation rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true); |
| 194 | if (rlSrc.location == kLocPhysReg) { |
buzbee | 82488f5 | 2012-03-02 08:20:26 -0800 | [diff] [blame] | 195 | opRegCopy(cUnit, rlResult.lowReg, rlSrc.lowReg); |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 196 | } else { |
| 197 | loadValueDirect(cUnit, rlSrc, rlResult.lowReg); |
| 198 | } |
| 199 | opRegRegImm(cUnit, kOpAsr, rlResult.highReg, |
| 200 | rlResult.lowReg, 31); |
| 201 | storeValueWide(cUnit, rlDest, rlResult); |
| 202 | } |
| 203 | |
| 204 | void genIntNarrowing(CompilationUnit* cUnit, MIR* mir, RegLocation rlDest, |
| 205 | RegLocation rlSrc) |
| 206 | { |
| 207 | rlSrc = loadValue(cUnit, rlSrc, kCoreReg); |
| 208 | RegLocation rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true); |
| 209 | OpKind op = kOpInvalid; |
| 210 | switch(mir->dalvikInsn.opcode) { |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 211 | case Instruction::INT_TO_BYTE: |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 212 | op = kOp2Byte; |
| 213 | break; |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 214 | case Instruction::INT_TO_SHORT: |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 215 | op = kOp2Short; |
| 216 | break; |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 217 | case Instruction::INT_TO_CHAR: |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 218 | op = kOp2Char; |
| 219 | break; |
| 220 | default: |
| 221 | LOG(ERROR) << "Bad int conversion type"; |
| 222 | } |
| 223 | opRegReg(cUnit, op, rlResult.lowReg, rlSrc.lowReg); |
| 224 | storeValue(cUnit, rlDest, rlResult); |
| 225 | } |
| 226 | |
| 227 | /* |
| 228 | * Let helper function take care of everything. Will call |
| 229 | * Array::AllocFromCode(type_idx, method, count); |
| 230 | * Note: AllocFromCode will handle checks for errNegativeArraySize. |
| 231 | */ |
| 232 | void genNewArray(CompilationUnit* cUnit, MIR* mir, RegLocation rlDest, |
| 233 | RegLocation rlSrc) |
| 234 | { |
| 235 | oatFlushAllRegs(cUnit); /* Everything to home location */ |
| 236 | uint32_t type_idx = mir->dalvikInsn.vC; |
| 237 | int rTgt; |
| 238 | if (cUnit->compiler->CanAccessTypeWithoutChecks(cUnit->method_idx, |
| 239 | cUnit->dex_cache, |
| 240 | *cUnit->dex_file, |
| 241 | type_idx)) { |
| 242 | rTgt = loadHelper(cUnit, OFFSETOF_MEMBER(Thread, pAllocArrayFromCode)); |
| 243 | } else { |
| 244 | rTgt = loadHelper(cUnit, OFFSETOF_MEMBER(Thread, |
| 245 | pAllocArrayFromCodeWithAccessCheck)); |
| 246 | } |
| 247 | loadCurrMethodDirect(cUnit, rARG1); // arg1 <- Method* |
| 248 | loadConstant(cUnit, rARG0, type_idx); // arg0 <- type_id |
| 249 | loadValueDirectFixed(cUnit, rlSrc, rARG2); // arg2 <- count |
| 250 | callRuntimeHelper(cUnit, rTgt); |
| 251 | RegLocation rlResult = oatGetReturn(cUnit); |
| 252 | storeValue(cUnit, rlDest, rlResult); |
| 253 | } |
| 254 | |
| 255 | /* |
| 256 | * Similar to genNewArray, but with post-allocation initialization. |
| 257 | * Verifier guarantees we're dealing with an array class. Current |
| 258 | * code throws runtime exception "bad Filled array req" for 'D' and 'J'. |
| 259 | * Current code also throws internal unimp if not 'L', '[' or 'I'. |
| 260 | */ |
| 261 | void genFilledNewArray(CompilationUnit* cUnit, MIR* mir, bool isRange) |
| 262 | { |
| 263 | DecodedInstruction* dInsn = &mir->dalvikInsn; |
| 264 | int elems = dInsn->vA; |
| 265 | int typeId = dInsn->vB; |
| 266 | oatFlushAllRegs(cUnit); /* Everything to home location */ |
| 267 | int rTgt; |
| 268 | if (cUnit->compiler->CanAccessTypeWithoutChecks(cUnit->method_idx, |
| 269 | cUnit->dex_cache, |
| 270 | *cUnit->dex_file, |
| 271 | typeId)) { |
| 272 | rTgt = loadHelper(cUnit, OFFSETOF_MEMBER(Thread, |
| 273 | pCheckAndAllocArrayFromCode)); |
| 274 | } else { |
| 275 | rTgt = loadHelper(cUnit, OFFSETOF_MEMBER(Thread, |
| 276 | pCheckAndAllocArrayFromCodeWithAccessCheck)); |
| 277 | } |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 278 | loadConstant(cUnit, rARG0, typeId); // arg0 <- type_id |
| 279 | loadConstant(cUnit, rARG2, elems); // arg2 <- count |
buzbee | e196567 | 2012-03-11 18:39:19 -0700 | [diff] [blame] | 280 | loadCurrMethodDirect(cUnit, rARG1); // arg1 <- Method* |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 281 | callRuntimeHelper(cUnit, rTgt); |
buzbee | e196567 | 2012-03-11 18:39:19 -0700 | [diff] [blame] | 282 | oatFreeTemp(cUnit, rARG2); |
| 283 | oatFreeTemp(cUnit, rARG1); |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 284 | /* |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 285 | * NOTE: the implicit target for Instruction::FILLED_NEW_ARRAY is the |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 286 | * return region. Because AllocFromCode placed the new array |
| 287 | * in rRET0, we'll just lock it into place. When debugger support is |
| 288 | * added, it may be necessary to additionally copy all return |
| 289 | * values to a home location in thread-local storage |
| 290 | */ |
| 291 | oatLockTemp(cUnit, rRET0); |
| 292 | |
| 293 | // TODO: use the correct component size, currently all supported types |
| 294 | // share array alignment with ints (see comment at head of function) |
| 295 | size_t component_size = sizeof(int32_t); |
| 296 | |
| 297 | // Having a range of 0 is legal |
| 298 | if (isRange && (dInsn->vA > 0)) { |
| 299 | /* |
| 300 | * Bit of ugliness here. We're going generate a mem copy loop |
| 301 | * on the register range, but it is possible that some regs |
| 302 | * in the range have been promoted. This is unlikely, but |
| 303 | * before generating the copy, we'll just force a flush |
| 304 | * of any regs in the source range that have been promoted to |
| 305 | * home location. |
| 306 | */ |
| 307 | for (unsigned int i = 0; i < dInsn->vA; i++) { |
| 308 | RegLocation loc = oatUpdateLoc(cUnit, |
| 309 | oatGetSrc(cUnit, mir, i)); |
| 310 | if (loc.location == kLocPhysReg) { |
| 311 | storeBaseDisp(cUnit, rSP, oatSRegOffset(cUnit, loc.sRegLow), |
| 312 | loc.lowReg, kWord); |
| 313 | } |
| 314 | } |
| 315 | /* |
| 316 | * TUNING note: generated code here could be much improved, but |
| 317 | * this is an uncommon operation and isn't especially performance |
| 318 | * critical. |
| 319 | */ |
| 320 | int rSrc = oatAllocTemp(cUnit); |
| 321 | int rDst = oatAllocTemp(cUnit); |
| 322 | int rIdx = oatAllocTemp(cUnit); |
buzbee | 5de3494 | 2012-03-01 14:51:57 -0800 | [diff] [blame] | 323 | #if defined(TARGET_ARM) |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 324 | int rVal = rLR; // Using a lot of temps, rLR is known free here |
buzbee | 5de3494 | 2012-03-01 14:51:57 -0800 | [diff] [blame] | 325 | #else |
| 326 | int rVal = oatAllocTemp(cUnit); |
| 327 | #endif |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 328 | // Set up source pointer |
| 329 | RegLocation rlFirst = oatGetSrc(cUnit, mir, 0); |
Ian Rogers | b5d09b2 | 2012-03-06 22:14:17 -0800 | [diff] [blame] | 330 | #if defined(TARGET_X86) |
| 331 | UNIMPLEMENTED(FATAL); |
| 332 | #else |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 333 | opRegRegImm(cUnit, kOpAdd, rSrc, rSP, |
| 334 | oatSRegOffset(cUnit, rlFirst.sRegLow)); |
| 335 | // Set up the target pointer |
| 336 | opRegRegImm(cUnit, kOpAdd, rDst, rRET0, |
| 337 | Array::DataOffset(component_size).Int32Value()); |
Ian Rogers | b5d09b2 | 2012-03-06 22:14:17 -0800 | [diff] [blame] | 338 | #endif |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 339 | // Set up the loop counter (known to be > 0) |
| 340 | loadConstant(cUnit, rIdx, dInsn->vA - 1); |
| 341 | // Generate the copy loop. Going backwards for convenience |
| 342 | LIR* target = newLIR0(cUnit, kPseudoTargetLabel); |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 343 | // Copy next element |
| 344 | loadBaseIndexed(cUnit, rSrc, rIdx, rVal, 2, kWord); |
| 345 | storeBaseIndexed(cUnit, rDst, rIdx, rVal, 2, kWord); |
| 346 | #if defined(TARGET_ARM) |
| 347 | // Combine sub & test using sub setflags encoding here |
| 348 | newLIR3(cUnit, kThumb2SubsRRI12, rIdx, rIdx, 1); |
buzbee | 82488f5 | 2012-03-02 08:20:26 -0800 | [diff] [blame] | 349 | opCondBranch(cUnit, kCondGe, target); |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 350 | #else |
buzbee | 5de3494 | 2012-03-01 14:51:57 -0800 | [diff] [blame] | 351 | oatFreeTemp(cUnit, rVal); |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 352 | opRegImm(cUnit, kOpSub, rIdx, 1); |
buzbee | 82488f5 | 2012-03-02 08:20:26 -0800 | [diff] [blame] | 353 | opCmpImmBranch(cUnit, kCondGe, rIdx, 0, target); |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 354 | #endif |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 355 | } else if (!isRange) { |
| 356 | // TUNING: interleave |
| 357 | for (unsigned int i = 0; i < dInsn->vA; i++) { |
| 358 | RegLocation rlArg = loadValue(cUnit, |
| 359 | oatGetSrc(cUnit, mir, i), kCoreReg); |
| 360 | storeBaseDisp(cUnit, rRET0, |
| 361 | Array::DataOffset(component_size).Int32Value() + |
| 362 | i * 4, rlArg.lowReg, kWord); |
| 363 | // If the loadValue caused a temp to be allocated, free it |
| 364 | if (oatIsTemp(cUnit, rlArg.lowReg)) { |
| 365 | oatFreeTemp(cUnit, rlArg.lowReg); |
| 366 | } |
| 367 | } |
| 368 | } |
| 369 | } |
| 370 | |
| 371 | void genSput(CompilationUnit* cUnit, MIR* mir, RegLocation rlSrc, |
| 372 | bool isLongOrDouble, bool isObject) |
| 373 | { |
| 374 | int fieldOffset; |
| 375 | int ssbIndex; |
| 376 | bool isVolatile; |
| 377 | bool isReferrersClass; |
| 378 | uint32_t fieldIdx = mir->dalvikInsn.vB; |
| 379 | |
| 380 | OatCompilationUnit mUnit(cUnit->class_loader, cUnit->class_linker, |
| 381 | *cUnit->dex_file, *cUnit->dex_cache, |
| 382 | cUnit->code_item, cUnit->method_idx, |
| 383 | cUnit->access_flags); |
| 384 | |
| 385 | bool fastPath = |
| 386 | cUnit->compiler->ComputeStaticFieldInfo(fieldIdx, &mUnit, |
| 387 | fieldOffset, ssbIndex, |
| 388 | isReferrersClass, isVolatile, true); |
| 389 | if (fastPath && !SLOW_FIELD_PATH) { |
| 390 | DCHECK_GE(fieldOffset, 0); |
| 391 | int rBase; |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 392 | if (isReferrersClass) { |
| 393 | // Fast path, static storage base is this method's class |
buzbee | e196567 | 2012-03-11 18:39:19 -0700 | [diff] [blame] | 394 | RegLocation rlMethod = loadCurrMethod(cUnit); |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 395 | rBase = oatAllocTemp(cUnit); |
buzbee | e196567 | 2012-03-11 18:39:19 -0700 | [diff] [blame] | 396 | loadWordDisp(cUnit, rlMethod.lowReg, |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 397 | Method::DeclaringClassOffset().Int32Value(), rBase); |
| 398 | } else { |
| 399 | // Medium path, static storage base in a different class which |
| 400 | // requires checks that the other class is initialized. |
| 401 | DCHECK_GE(ssbIndex, 0); |
| 402 | // May do runtime call so everything to home locations. |
| 403 | oatFlushAllRegs(cUnit); |
| 404 | // Using fixed register to sync with possible call to runtime |
| 405 | // support. |
buzbee | e196567 | 2012-03-11 18:39:19 -0700 | [diff] [blame] | 406 | int rMethod = rARG1; |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 407 | oatLockTemp(cUnit, rMethod); |
| 408 | loadCurrMethodDirect(cUnit, rMethod); |
| 409 | rBase = rARG0; |
| 410 | oatLockTemp(cUnit, rBase); |
| 411 | loadWordDisp(cUnit, rMethod, |
| 412 | Method::DexCacheInitializedStaticStorageOffset().Int32Value(), |
| 413 | rBase); |
| 414 | loadWordDisp(cUnit, rBase, |
| 415 | Array::DataOffset(sizeof(Object*)).Int32Value() + sizeof(int32_t*) * |
| 416 | ssbIndex, rBase); |
| 417 | // rBase now points at appropriate static storage base (Class*) |
| 418 | // or NULL if not initialized. Check for NULL and call helper if NULL. |
| 419 | // TUNING: fast path should fall through |
buzbee | 82488f5 | 2012-03-02 08:20:26 -0800 | [diff] [blame] | 420 | LIR* branchOver = opCmpImmBranch(cUnit, kCondNe, rBase, 0, NULL); |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 421 | int rTgt = loadHelper(cUnit, OFFSETOF_MEMBER(Thread, |
| 422 | pInitializeStaticStorage)); |
| 423 | loadConstant(cUnit, rARG0, ssbIndex); |
| 424 | callRuntimeHelper(cUnit, rTgt); |
| 425 | #if defined(TARGET_MIPS) |
| 426 | // For Arm, rRET0 = rARG0 = rBASE, for Mips, we need to copy |
buzbee | 82488f5 | 2012-03-02 08:20:26 -0800 | [diff] [blame] | 427 | opRegCopy(cUnit, rBase, rRET0); |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 428 | #endif |
| 429 | LIR* skipTarget = newLIR0(cUnit, kPseudoTargetLabel); |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 430 | branchOver->target = (LIR*)skipTarget; |
buzbee | e196567 | 2012-03-11 18:39:19 -0700 | [diff] [blame] | 431 | oatFreeTemp(cUnit, rMethod); |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 432 | } |
| 433 | // rBase now holds static storage base |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 434 | if (isLongOrDouble) { |
| 435 | rlSrc = oatGetSrcWide(cUnit, mir, 0, 1); |
| 436 | rlSrc = loadValueWide(cUnit, rlSrc, kAnyReg); |
| 437 | } else { |
| 438 | rlSrc = oatGetSrc(cUnit, mir, 0); |
| 439 | rlSrc = loadValue(cUnit, rlSrc, kAnyReg); |
| 440 | } |
| 441 | //FIXME: need to generalize the barrier call |
| 442 | if (isVolatile) { |
| 443 | oatGenMemBarrier(cUnit, kST); |
| 444 | } |
| 445 | if (isLongOrDouble) { |
| 446 | storeBaseDispWide(cUnit, rBase, fieldOffset, rlSrc.lowReg, |
| 447 | rlSrc.highReg); |
| 448 | } else { |
| 449 | storeWordDisp(cUnit, rBase, fieldOffset, rlSrc.lowReg); |
| 450 | } |
| 451 | if (isVolatile) { |
| 452 | oatGenMemBarrier(cUnit, kSY); |
| 453 | } |
| 454 | if (isObject) { |
| 455 | markGCCard(cUnit, rlSrc.lowReg, rBase); |
| 456 | } |
| 457 | oatFreeTemp(cUnit, rBase); |
| 458 | } else { |
| 459 | oatFlushAllRegs(cUnit); // Everything to home locations |
| 460 | int setterOffset = isLongOrDouble ? OFFSETOF_MEMBER(Thread, pSet64Static) : |
| 461 | (isObject ? OFFSETOF_MEMBER(Thread, pSetObjStatic) |
| 462 | : OFFSETOF_MEMBER(Thread, pSet32Static)); |
| 463 | int rTgt = loadHelper(cUnit, setterOffset); |
| 464 | loadConstant(cUnit, rARG0, fieldIdx); |
| 465 | if (isLongOrDouble) { |
Ian Rogers | b5d09b2 | 2012-03-06 22:14:17 -0800 | [diff] [blame] | 466 | #if defined(TARGET_X86) |
| 467 | UNIMPLEMENTED(FATAL); |
| 468 | #else |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 469 | loadValueDirectWideFixed(cUnit, rlSrc, rARG2, rARG3); |
Ian Rogers | b5d09b2 | 2012-03-06 22:14:17 -0800 | [diff] [blame] | 470 | #endif |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 471 | } else { |
| 472 | loadValueDirect(cUnit, rlSrc, rARG1); |
| 473 | } |
| 474 | callRuntimeHelper(cUnit, rTgt); |
| 475 | } |
| 476 | } |
| 477 | |
| 478 | void genSget(CompilationUnit* cUnit, MIR* mir, RegLocation rlDest, |
| 479 | bool isLongOrDouble, bool isObject) |
| 480 | { |
| 481 | int fieldOffset; |
| 482 | int ssbIndex; |
| 483 | bool isVolatile; |
| 484 | bool isReferrersClass; |
| 485 | uint32_t fieldIdx = mir->dalvikInsn.vB; |
| 486 | |
| 487 | OatCompilationUnit mUnit(cUnit->class_loader, cUnit->class_linker, |
| 488 | *cUnit->dex_file, *cUnit->dex_cache, |
| 489 | cUnit->code_item, cUnit->method_idx, |
| 490 | cUnit->access_flags); |
| 491 | |
| 492 | bool fastPath = |
| 493 | cUnit->compiler->ComputeStaticFieldInfo(fieldIdx, &mUnit, |
| 494 | fieldOffset, ssbIndex, |
| 495 | isReferrersClass, isVolatile, |
| 496 | false); |
| 497 | if (fastPath && !SLOW_FIELD_PATH) { |
| 498 | DCHECK_GE(fieldOffset, 0); |
| 499 | int rBase; |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 500 | if (isReferrersClass) { |
| 501 | // Fast path, static storage base is this method's class |
buzbee | e196567 | 2012-03-11 18:39:19 -0700 | [diff] [blame] | 502 | RegLocation rlMethod = loadCurrMethod(cUnit); |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 503 | rBase = oatAllocTemp(cUnit); |
buzbee | e196567 | 2012-03-11 18:39:19 -0700 | [diff] [blame] | 504 | loadWordDisp(cUnit, rlMethod.lowReg, |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 505 | Method::DeclaringClassOffset().Int32Value(), rBase); |
| 506 | } else { |
| 507 | // Medium path, static storage base in a different class which |
| 508 | // requires checks that the other class is initialized |
| 509 | DCHECK_GE(ssbIndex, 0); |
| 510 | // May do runtime call so everything to home locations. |
| 511 | oatFlushAllRegs(cUnit); |
| 512 | // Using fixed register to sync with possible call to runtime |
| 513 | // support |
buzbee | e196567 | 2012-03-11 18:39:19 -0700 | [diff] [blame] | 514 | int rMethod = rARG1; |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 515 | oatLockTemp(cUnit, rMethod); |
| 516 | loadCurrMethodDirect(cUnit, rMethod); |
| 517 | rBase = rARG0; |
| 518 | oatLockTemp(cUnit, rBase); |
| 519 | loadWordDisp(cUnit, rMethod, |
| 520 | Method::DexCacheInitializedStaticStorageOffset().Int32Value(), |
| 521 | rBase); |
| 522 | loadWordDisp(cUnit, rBase, |
| 523 | Array::DataOffset(sizeof(Object*)).Int32Value() + |
| 524 | sizeof(int32_t*) * ssbIndex, |
| 525 | rBase); |
| 526 | // rBase now points at appropriate static storage base (Class*) |
| 527 | // or NULL if not initialized. Check for NULL and call helper if NULL. |
| 528 | // TUNING: fast path should fall through |
buzbee | 82488f5 | 2012-03-02 08:20:26 -0800 | [diff] [blame] | 529 | LIR* branchOver = opCmpImmBranch(cUnit, kCondNe, rBase, 0, NULL); |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 530 | int rTgt = loadHelper(cUnit, OFFSETOF_MEMBER(Thread, |
| 531 | pInitializeStaticStorage)); |
| 532 | loadConstant(cUnit, rARG0, ssbIndex); |
| 533 | callRuntimeHelper(cUnit, rTgt); |
| 534 | #if defined(TARGET_MIPS) |
| 535 | // For Arm, rRET0 = rARG0 = rBASE, for Mips, we need to copy |
buzbee | 82488f5 | 2012-03-02 08:20:26 -0800 | [diff] [blame] | 536 | opRegCopy(cUnit, rBase, rRET0); |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 537 | #endif |
| 538 | LIR* skipTarget = newLIR0(cUnit, kPseudoTargetLabel); |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 539 | branchOver->target = (LIR*)skipTarget; |
buzbee | e196567 | 2012-03-11 18:39:19 -0700 | [diff] [blame] | 540 | oatFreeTemp(cUnit, rMethod); |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 541 | } |
| 542 | // rBase now holds static storage base |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 543 | rlDest = isLongOrDouble ? oatGetDestWide(cUnit, mir, 0, 1) |
| 544 | : oatGetDest(cUnit, mir, 0); |
| 545 | RegLocation rlResult = oatEvalLoc(cUnit, rlDest, kAnyReg, true); |
| 546 | if (isVolatile) { |
| 547 | oatGenMemBarrier(cUnit, kSY); |
| 548 | } |
| 549 | if (isLongOrDouble) { |
| 550 | loadBaseDispWide(cUnit, NULL, rBase, fieldOffset, rlResult.lowReg, |
| 551 | rlResult.highReg, INVALID_SREG); |
| 552 | } else { |
| 553 | loadWordDisp(cUnit, rBase, fieldOffset, rlResult.lowReg); |
| 554 | } |
| 555 | oatFreeTemp(cUnit, rBase); |
| 556 | if (isLongOrDouble) { |
| 557 | storeValueWide(cUnit, rlDest, rlResult); |
| 558 | } else { |
| 559 | storeValue(cUnit, rlDest, rlResult); |
| 560 | } |
| 561 | } else { |
| 562 | oatFlushAllRegs(cUnit); // Everything to home locations |
| 563 | int getterOffset = isLongOrDouble ? OFFSETOF_MEMBER(Thread, pGet64Static) : |
| 564 | (isObject ? OFFSETOF_MEMBER(Thread, pGetObjStatic) |
| 565 | : OFFSETOF_MEMBER(Thread, pGet32Static)); |
| 566 | int rTgt = loadHelper(cUnit, getterOffset); |
| 567 | loadConstant(cUnit, rARG0, fieldIdx); |
| 568 | callRuntimeHelper(cUnit, rTgt); |
| 569 | if (isLongOrDouble) { |
| 570 | RegLocation rlResult = oatGetReturnWide(cUnit); |
| 571 | storeValueWide(cUnit, rlDest, rlResult); |
| 572 | } else { |
| 573 | RegLocation rlResult = oatGetReturn(cUnit); |
| 574 | storeValue(cUnit, rlDest, rlResult); |
| 575 | } |
| 576 | } |
| 577 | } |
| 578 | |
| 579 | |
| 580 | // Debugging routine - if null target, branch to DebugMe |
| 581 | void genShowTarget(CompilationUnit* cUnit) |
| 582 | { |
buzbee | a7678db | 2012-03-05 15:35:46 -0800 | [diff] [blame] | 583 | #if defined(TARGET_X86) |
| 584 | UNIMPLEMENTED(WARNING) << "genShowTarget"; |
| 585 | #else |
buzbee | 0398c42 | 2012-03-02 15:22:47 -0800 | [diff] [blame] | 586 | LIR* branchOver = opCmpImmBranch(cUnit, kCondNe, rINVOKE_TGT, 0, NULL); |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 587 | loadWordDisp(cUnit, rSELF, |
buzbee | 0398c42 | 2012-03-02 15:22:47 -0800 | [diff] [blame] | 588 | OFFSETOF_MEMBER(Thread, pDebugMe), rINVOKE_TGT); |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 589 | LIR* target = newLIR0(cUnit, kPseudoTargetLabel); |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 590 | branchOver->target = (LIR*)target; |
buzbee | a7678db | 2012-03-05 15:35:46 -0800 | [diff] [blame] | 591 | #endif |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 592 | } |
| 593 | |
| 594 | void genThrowVerificationError(CompilationUnit* cUnit, MIR* mir) |
| 595 | { |
| 596 | int rTgt = loadHelper(cUnit, OFFSETOF_MEMBER(Thread, |
| 597 | pThrowVerificationErrorFromCode)); |
| 598 | loadConstant(cUnit, rARG0, mir->dalvikInsn.vA); |
| 599 | loadConstant(cUnit, rARG1, mir->dalvikInsn.vB); |
| 600 | callRuntimeHelper(cUnit, rTgt); |
| 601 | } |
| 602 | |
| 603 | void handleSuspendLaunchpads(CompilationUnit *cUnit) |
| 604 | { |
| 605 | LIR** suspendLabel = |
| 606 | (LIR **) cUnit->suspendLaunchpads.elemList; |
| 607 | int numElems = cUnit->suspendLaunchpads.numUsed; |
| 608 | |
| 609 | for (int i = 0; i < numElems; i++) { |
buzbee | c5159d5 | 2012-03-03 11:48:39 -0800 | [diff] [blame] | 610 | oatResetRegPool(cUnit); |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 611 | LIR* lab = suspendLabel[i]; |
| 612 | LIR* resumeLab = (LIR*)lab->operands[0]; |
| 613 | cUnit->currentDalvikOffset = lab->operands[1]; |
| 614 | oatAppendLIR(cUnit, (LIR *)lab); |
| 615 | int rTgt = loadHelper(cUnit, OFFSETOF_MEMBER(Thread, |
| 616 | pTestSuspendFromCode)); |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 617 | opReg(cUnit, kOpBlx, rTgt); |
buzbee | 82488f5 | 2012-03-02 08:20:26 -0800 | [diff] [blame] | 618 | opUnconditionalBranch(cUnit, resumeLab); |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 619 | } |
| 620 | } |
| 621 | |
| 622 | void handleThrowLaunchpads(CompilationUnit *cUnit) |
| 623 | { |
| 624 | LIR** throwLabel = (LIR **) cUnit->throwLaunchpads.elemList; |
| 625 | int numElems = cUnit->throwLaunchpads.numUsed; |
| 626 | int i; |
| 627 | |
| 628 | for (i = 0; i < numElems; i++) { |
buzbee | c5159d5 | 2012-03-03 11:48:39 -0800 | [diff] [blame] | 629 | oatResetRegPool(cUnit); |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 630 | LIR* lab = throwLabel[i]; |
| 631 | cUnit->currentDalvikOffset = lab->operands[1]; |
| 632 | oatAppendLIR(cUnit, (LIR *)lab); |
| 633 | int funcOffset = 0; |
| 634 | int v1 = lab->operands[2]; |
| 635 | int v2 = lab->operands[3]; |
| 636 | switch(lab->operands[0]) { |
| 637 | case kThrowNullPointer: |
| 638 | funcOffset = OFFSETOF_MEMBER(Thread, pThrowNullPointerFromCode); |
| 639 | break; |
| 640 | case kThrowArrayBounds: |
buzbee | 5de3494 | 2012-03-01 14:51:57 -0800 | [diff] [blame] | 641 | if (v2 != rARG0) { |
buzbee | 82488f5 | 2012-03-02 08:20:26 -0800 | [diff] [blame] | 642 | opRegCopy(cUnit, rARG0, v1); |
| 643 | opRegCopy(cUnit, rARG1, v2); |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 644 | } else { |
buzbee | 5de3494 | 2012-03-01 14:51:57 -0800 | [diff] [blame] | 645 | if (v1 == rARG1) { |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 646 | #if defined(TARGET_ARM) |
| 647 | int rTmp = r12; |
| 648 | #else |
| 649 | int rTmp = oatAllocTemp(cUnit); |
| 650 | #endif |
buzbee | 82488f5 | 2012-03-02 08:20:26 -0800 | [diff] [blame] | 651 | opRegCopy(cUnit, rTmp, v1); |
| 652 | opRegCopy(cUnit, rARG1, v2); |
| 653 | opRegCopy(cUnit, rARG0, rTmp); |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 654 | } else { |
buzbee | 82488f5 | 2012-03-02 08:20:26 -0800 | [diff] [blame] | 655 | opRegCopy(cUnit, rARG1, v2); |
| 656 | opRegCopy(cUnit, rARG0, v1); |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 657 | } |
| 658 | } |
| 659 | funcOffset = OFFSETOF_MEMBER(Thread, pThrowArrayBoundsFromCode); |
| 660 | break; |
| 661 | case kThrowDivZero: |
| 662 | funcOffset = OFFSETOF_MEMBER(Thread, pThrowDivZeroFromCode); |
| 663 | break; |
| 664 | case kThrowVerificationError: |
| 665 | loadConstant(cUnit, rARG0, v1); |
| 666 | loadConstant(cUnit, rARG1, v2); |
| 667 | funcOffset = |
| 668 | OFFSETOF_MEMBER(Thread, pThrowVerificationErrorFromCode); |
| 669 | break; |
| 670 | case kThrowNegArraySize: |
buzbee | 82488f5 | 2012-03-02 08:20:26 -0800 | [diff] [blame] | 671 | opRegCopy(cUnit, rARG0, v1); |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 672 | funcOffset = |
| 673 | OFFSETOF_MEMBER(Thread, pThrowNegArraySizeFromCode); |
| 674 | break; |
| 675 | case kThrowNoSuchMethod: |
buzbee | 82488f5 | 2012-03-02 08:20:26 -0800 | [diff] [blame] | 676 | opRegCopy(cUnit, rARG0, v1); |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 677 | funcOffset = |
| 678 | OFFSETOF_MEMBER(Thread, pThrowNoSuchMethodFromCode); |
| 679 | break; |
| 680 | case kThrowStackOverflow: |
| 681 | funcOffset = |
| 682 | OFFSETOF_MEMBER(Thread, pThrowStackOverflowFromCode); |
| 683 | // Restore stack alignment |
| 684 | opRegImm(cUnit, kOpAdd, rSP, |
| 685 | (cUnit->numCoreSpills + cUnit->numFPSpills) * 4); |
| 686 | break; |
| 687 | default: |
| 688 | LOG(FATAL) << "Unexpected throw kind: " << lab->operands[0]; |
| 689 | } |
| 690 | int rTgt = loadHelper(cUnit, funcOffset); |
| 691 | callRuntimeHelper(cUnit, rTgt); |
buzbee | 0398c42 | 2012-03-02 15:22:47 -0800 | [diff] [blame] | 692 | oatFreeTemp(cUnit, rTgt); |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 693 | } |
| 694 | } |
| 695 | |
| 696 | /* Needed by the Assembler */ |
| 697 | void oatSetupResourceMasks(LIR* lir) |
| 698 | { |
| 699 | setupResourceMasks(lir); |
| 700 | } |
| 701 | |
| 702 | void genIGet(CompilationUnit* cUnit, MIR* mir, OpSize size, |
| 703 | RegLocation rlDest, RegLocation rlObj, |
| 704 | bool isLongOrDouble, bool isObject) |
| 705 | { |
| 706 | int fieldOffset; |
| 707 | bool isVolatile; |
| 708 | uint32_t fieldIdx = mir->dalvikInsn.vC; |
| 709 | |
| 710 | OatCompilationUnit mUnit(cUnit->class_loader, cUnit->class_linker, |
| 711 | *cUnit->dex_file, *cUnit->dex_cache, |
| 712 | cUnit->code_item, cUnit->method_idx, |
| 713 | cUnit->access_flags); |
| 714 | |
| 715 | bool fastPath = cUnit->compiler->ComputeInstanceFieldInfo(fieldIdx, &mUnit, |
| 716 | fieldOffset, isVolatile, false); |
| 717 | |
| 718 | if (fastPath && !SLOW_FIELD_PATH) { |
| 719 | RegLocation rlResult; |
| 720 | RegisterClass regClass = oatRegClassBySize(size); |
| 721 | DCHECK_GE(fieldOffset, 0); |
| 722 | rlObj = loadValue(cUnit, rlObj, kCoreReg); |
| 723 | if (isLongOrDouble) { |
| 724 | DCHECK(rlDest.wide); |
| 725 | genNullCheck(cUnit, rlObj.sRegLow, rlObj.lowReg, mir);/* null? */ |
Ian Rogers | b5d09b2 | 2012-03-06 22:14:17 -0800 | [diff] [blame] | 726 | #if defined(TARGET_X86) |
| 727 | rlResult = oatEvalLoc(cUnit, rlDest, regClass, true); |
| 728 | genNullCheck(cUnit, rlObj.sRegLow, rlObj.lowReg, mir);/* null? */ |
| 729 | loadBaseDispWide(cUnit, mir, rlObj.lowReg, fieldOffset, rlResult.lowReg, |
| 730 | rlResult.highReg, rlObj.sRegLow); |
| 731 | if (isVolatile) { |
| 732 | oatGenMemBarrier(cUnit, kSY); |
| 733 | } |
| 734 | #else |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 735 | int regPtr = oatAllocTemp(cUnit); |
| 736 | opRegRegImm(cUnit, kOpAdd, regPtr, rlObj.lowReg, fieldOffset); |
| 737 | rlResult = oatEvalLoc(cUnit, rlDest, regClass, true); |
| 738 | loadPair(cUnit, regPtr, rlResult.lowReg, rlResult.highReg); |
| 739 | if (isVolatile) { |
| 740 | oatGenMemBarrier(cUnit, kSY); |
| 741 | } |
| 742 | oatFreeTemp(cUnit, regPtr); |
Ian Rogers | b5d09b2 | 2012-03-06 22:14:17 -0800 | [diff] [blame] | 743 | #endif |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 744 | storeValueWide(cUnit, rlDest, rlResult); |
| 745 | } else { |
| 746 | rlResult = oatEvalLoc(cUnit, rlDest, regClass, true); |
| 747 | genNullCheck(cUnit, rlObj.sRegLow, rlObj.lowReg, mir);/* null? */ |
| 748 | loadBaseDisp(cUnit, mir, rlObj.lowReg, fieldOffset, rlResult.lowReg, |
| 749 | kWord, rlObj.sRegLow); |
| 750 | if (isVolatile) { |
| 751 | oatGenMemBarrier(cUnit, kSY); |
| 752 | } |
| 753 | storeValue(cUnit, rlDest, rlResult); |
| 754 | } |
| 755 | } else { |
| 756 | int getterOffset = isLongOrDouble ? OFFSETOF_MEMBER(Thread, pGet64Instance) : |
| 757 | (isObject ? OFFSETOF_MEMBER(Thread, pGetObjInstance) |
| 758 | : OFFSETOF_MEMBER(Thread, pGet32Instance)); |
| 759 | int rTgt = loadHelper(cUnit, getterOffset); |
| 760 | loadValueDirect(cUnit, rlObj, rARG1); |
| 761 | loadConstant(cUnit, rARG0, fieldIdx); |
| 762 | callRuntimeHelper(cUnit, rTgt); |
| 763 | if (isLongOrDouble) { |
| 764 | RegLocation rlResult = oatGetReturnWide(cUnit); |
| 765 | storeValueWide(cUnit, rlDest, rlResult); |
| 766 | } else { |
| 767 | RegLocation rlResult = oatGetReturn(cUnit); |
| 768 | storeValue(cUnit, rlDest, rlResult); |
| 769 | } |
| 770 | } |
| 771 | } |
| 772 | |
| 773 | void genIPut(CompilationUnit* cUnit, MIR* mir, OpSize size, RegLocation rlSrc, |
| 774 | RegLocation rlObj, bool isLongOrDouble, bool isObject) |
| 775 | { |
| 776 | int fieldOffset; |
| 777 | bool isVolatile; |
| 778 | uint32_t fieldIdx = mir->dalvikInsn.vC; |
| 779 | |
| 780 | OatCompilationUnit mUnit(cUnit->class_loader, cUnit->class_linker, |
| 781 | *cUnit->dex_file, *cUnit->dex_cache, |
| 782 | cUnit->code_item, cUnit->method_idx, |
| 783 | cUnit->access_flags); |
| 784 | |
| 785 | bool fastPath = cUnit->compiler->ComputeInstanceFieldInfo(fieldIdx, &mUnit, |
| 786 | fieldOffset, isVolatile, true); |
| 787 | if (fastPath && !SLOW_FIELD_PATH) { |
| 788 | RegisterClass regClass = oatRegClassBySize(size); |
| 789 | DCHECK_GE(fieldOffset, 0); |
| 790 | rlObj = loadValue(cUnit, rlObj, kCoreReg); |
| 791 | if (isLongOrDouble) { |
| 792 | int regPtr; |
| 793 | rlSrc = loadValueWide(cUnit, rlSrc, kAnyReg); |
| 794 | genNullCheck(cUnit, rlObj.sRegLow, rlObj.lowReg, mir);/* null? */ |
| 795 | regPtr = oatAllocTemp(cUnit); |
| 796 | opRegRegImm(cUnit, kOpAdd, regPtr, rlObj.lowReg, fieldOffset); |
| 797 | if (isVolatile) { |
| 798 | oatGenMemBarrier(cUnit, kST); |
| 799 | } |
| 800 | storePair(cUnit, regPtr, rlSrc.lowReg, rlSrc.highReg); |
| 801 | if (isVolatile) { |
| 802 | oatGenMemBarrier(cUnit, kSY); |
| 803 | } |
| 804 | oatFreeTemp(cUnit, regPtr); |
| 805 | } else { |
| 806 | rlSrc = loadValue(cUnit, rlSrc, regClass); |
| 807 | genNullCheck(cUnit, rlObj.sRegLow, rlObj.lowReg, mir);/* null? */ |
| 808 | if (isVolatile) { |
| 809 | oatGenMemBarrier(cUnit, kST); |
| 810 | } |
| 811 | storeBaseDisp(cUnit, rlObj.lowReg, fieldOffset, rlSrc.lowReg, kWord); |
| 812 | if (isVolatile) { |
| 813 | oatGenMemBarrier(cUnit, kSY); |
| 814 | } |
| 815 | } |
| 816 | } else { |
| 817 | int setterOffset = isLongOrDouble ? OFFSETOF_MEMBER(Thread, pSet64Instance) : |
| 818 | (isObject ? OFFSETOF_MEMBER(Thread, pSetObjInstance) |
| 819 | : OFFSETOF_MEMBER(Thread, pSet32Instance)); |
| 820 | int rTgt = loadHelper(cUnit, setterOffset); |
| 821 | loadValueDirect(cUnit, rlObj, rARG1); |
| 822 | if (isLongOrDouble) { |
Ian Rogers | b5d09b2 | 2012-03-06 22:14:17 -0800 | [diff] [blame] | 823 | #if defined(TARGET_X86) |
| 824 | UNIMPLEMENTED(FATAL); |
| 825 | #else |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 826 | loadValueDirectWide(cUnit, rlSrc, rARG2, rARG3); |
Ian Rogers | b5d09b2 | 2012-03-06 22:14:17 -0800 | [diff] [blame] | 827 | #endif |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 828 | } else { |
| 829 | loadValueDirect(cUnit, rlSrc, rARG2); |
| 830 | } |
| 831 | loadConstant(cUnit, rARG0, fieldIdx); |
| 832 | callRuntimeHelper(cUnit, rTgt); |
| 833 | } |
| 834 | } |
| 835 | |
| 836 | void genConstClass(CompilationUnit* cUnit, MIR* mir, RegLocation rlDest, |
| 837 | RegLocation rlSrc) |
| 838 | { |
| 839 | uint32_t type_idx = mir->dalvikInsn.vB; |
buzbee | e196567 | 2012-03-11 18:39:19 -0700 | [diff] [blame] | 840 | RegLocation rlMethod = loadCurrMethod(cUnit); |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 841 | int resReg = oatAllocTemp(cUnit); |
| 842 | RegLocation rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true); |
| 843 | if (!cUnit->compiler->CanAccessTypeWithoutChecks(cUnit->method_idx, |
| 844 | cUnit->dex_cache, |
| 845 | *cUnit->dex_file, |
| 846 | type_idx)) { |
| 847 | // Call out to helper which resolves type and verifies access. |
| 848 | // Resolved type returned in rRET0. |
| 849 | int rTgt = loadHelper(cUnit, OFFSETOF_MEMBER(Thread, |
| 850 | pInitializeTypeAndVerifyAccessFromCode)); |
buzbee | e196567 | 2012-03-11 18:39:19 -0700 | [diff] [blame] | 851 | opRegCopy(cUnit, rARG1, rlMethod.lowReg); |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 852 | loadConstant(cUnit, rARG0, type_idx); |
| 853 | callRuntimeHelper(cUnit, rTgt); |
| 854 | RegLocation rlResult = oatGetReturn(cUnit); |
| 855 | storeValue(cUnit, rlDest, rlResult); |
| 856 | } else { |
| 857 | // We're don't need access checks, load type from dex cache |
| 858 | int32_t dex_cache_offset = |
| 859 | Method::DexCacheResolvedTypesOffset().Int32Value(); |
buzbee | e196567 | 2012-03-11 18:39:19 -0700 | [diff] [blame] | 860 | loadWordDisp(cUnit, rlMethod.lowReg, dex_cache_offset, resReg); |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 861 | int32_t offset_of_type = |
| 862 | Array::DataOffset(sizeof(Class*)).Int32Value() + (sizeof(Class*) |
| 863 | * type_idx); |
| 864 | loadWordDisp(cUnit, resReg, offset_of_type, rlResult.lowReg); |
| 865 | if (!cUnit->compiler->CanAssumeTypeIsPresentInDexCache(cUnit->dex_cache, |
| 866 | type_idx) || SLOW_TYPE_PATH) { |
| 867 | // Slow path, at runtime test if type is null and if so initialize |
| 868 | oatFlushAllRegs(cUnit); |
buzbee | 82488f5 | 2012-03-02 08:20:26 -0800 | [diff] [blame] | 869 | LIR* branch1 = opCmpImmBranch(cUnit, kCondEq, rlResult.lowReg, 0, |
| 870 | NULL); |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 871 | // Resolved, store and hop over following code |
| 872 | storeValue(cUnit, rlDest, rlResult); |
buzbee | 3d66194 | 2012-03-14 17:37:27 -0700 | [diff] [blame] | 873 | /* |
| 874 | * Because we have stores of the target value on two paths, |
| 875 | * clobber temp tracking for the destination using the ssa name |
| 876 | */ |
| 877 | oatClobberSReg(cUnit, rlDest.sRegLow); |
buzbee | 82488f5 | 2012-03-02 08:20:26 -0800 | [diff] [blame] | 878 | LIR* branch2 = opUnconditionalBranch(cUnit,0); |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 879 | // TUNING: move slow path to end & remove unconditional branch |
| 880 | LIR* target1 = newLIR0(cUnit, kPseudoTargetLabel); |
buzbee | 5de3494 | 2012-03-01 14:51:57 -0800 | [diff] [blame] | 881 | // Call out to helper, which will return resolved type in rARG0 |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 882 | int rTgt = loadHelper(cUnit, OFFSETOF_MEMBER(Thread, |
| 883 | pInitializeTypeFromCode)); |
buzbee | e196567 | 2012-03-11 18:39:19 -0700 | [diff] [blame] | 884 | opRegCopy(cUnit, rARG1, rlMethod.lowReg); |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 885 | loadConstant(cUnit, rARG0, type_idx); |
| 886 | callRuntimeHelper(cUnit, rTgt); |
| 887 | RegLocation rlResult = oatGetReturn(cUnit); |
| 888 | storeValue(cUnit, rlDest, rlResult); |
buzbee | 3d66194 | 2012-03-14 17:37:27 -0700 | [diff] [blame] | 889 | /* |
| 890 | * Because we have stores of the target value on two paths, |
| 891 | * clobber temp tracking for the destination using the ssa name |
| 892 | */ |
| 893 | oatClobberSReg(cUnit, rlDest.sRegLow); |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 894 | // Rejoin code paths |
| 895 | LIR* target2 = newLIR0(cUnit, kPseudoTargetLabel); |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 896 | branch1->target = (LIR*)target1; |
| 897 | branch2->target = (LIR*)target2; |
| 898 | } else { |
| 899 | // Fast path, we're done - just store result |
| 900 | storeValue(cUnit, rlDest, rlResult); |
| 901 | } |
| 902 | } |
| 903 | } |
| 904 | void genConstString(CompilationUnit* cUnit, MIR* mir, RegLocation rlDest, |
| 905 | RegLocation rlSrc) |
| 906 | { |
| 907 | /* NOTE: Most strings should be available at compile time */ |
| 908 | uint32_t string_idx = mir->dalvikInsn.vB; |
| 909 | int32_t offset_of_string = Array::DataOffset(sizeof(String*)).Int32Value() + |
| 910 | (sizeof(String*) * string_idx); |
| 911 | if (!cUnit->compiler->CanAssumeStringIsPresentInDexCache( |
| 912 | cUnit->dex_cache, string_idx) || SLOW_STRING_PATH) { |
| 913 | // slow path, resolve string if not in dex cache |
| 914 | oatFlushAllRegs(cUnit); |
| 915 | oatLockCallTemps(cUnit); // Using explicit registers |
| 916 | loadCurrMethodDirect(cUnit, rARG2); |
| 917 | loadWordDisp(cUnit, rARG2, |
| 918 | Method::DexCacheStringsOffset().Int32Value(), rARG0); |
| 919 | // Might call out to helper, which will return resolved string in rRET0 |
| 920 | int rTgt = loadHelper(cUnit, OFFSETOF_MEMBER(Thread, |
| 921 | pResolveStringFromCode)); |
| 922 | loadWordDisp(cUnit, rRET0, offset_of_string, rARG0); |
| 923 | loadConstant(cUnit, rARG1, string_idx); |
| 924 | #if defined(TARGET_ARM) |
| 925 | opRegImm(cUnit, kOpCmp, rRET0, 0); // Is resolved? |
| 926 | genBarrier(cUnit); |
| 927 | // For testing, always force through helper |
| 928 | if (!EXERCISE_SLOWEST_STRING_PATH) { |
buzbee | 82488f5 | 2012-03-02 08:20:26 -0800 | [diff] [blame] | 929 | opIT(cUnit, kArmCondEq, "T"); |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 930 | } |
buzbee | 82488f5 | 2012-03-02 08:20:26 -0800 | [diff] [blame] | 931 | opRegCopy(cUnit, rARG0, rARG2); // .eq |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 932 | opReg(cUnit, kOpBlx, rTgt); // .eq, helper(Method*, string_idx) |
| 933 | #else |
buzbee | 82488f5 | 2012-03-02 08:20:26 -0800 | [diff] [blame] | 934 | LIR* branch = opCmpImmBranch(cUnit, kCondNe, rRET0, 0, NULL); |
| 935 | opRegCopy(cUnit, rARG0, rARG2); // .eq |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 936 | opReg(cUnit, kOpBlx, rTgt); |
| 937 | LIR* target = newLIR0(cUnit, kPseudoTargetLabel); |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 938 | branch->target = target; |
| 939 | #endif |
| 940 | genBarrier(cUnit); |
buzbee | 86a4bce | 2012-03-06 18:15:00 -0800 | [diff] [blame] | 941 | storeValue(cUnit, rlDest, oatGetReturn(cUnit)); |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 942 | } else { |
buzbee | e196567 | 2012-03-11 18:39:19 -0700 | [diff] [blame] | 943 | RegLocation rlMethod = loadCurrMethod(cUnit); |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 944 | int resReg = oatAllocTemp(cUnit); |
| 945 | RegLocation rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true); |
buzbee | e196567 | 2012-03-11 18:39:19 -0700 | [diff] [blame] | 946 | loadWordDisp(cUnit, rlMethod.lowReg, |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 947 | Method::DexCacheStringsOffset().Int32Value(), resReg); |
| 948 | loadWordDisp(cUnit, resReg, offset_of_string, rlResult.lowReg); |
| 949 | storeValue(cUnit, rlDest, rlResult); |
| 950 | } |
| 951 | } |
| 952 | |
| 953 | /* |
| 954 | * Let helper function take care of everything. Will |
| 955 | * call Class::NewInstanceFromCode(type_idx, method); |
| 956 | */ |
| 957 | void genNewInstance(CompilationUnit* cUnit, MIR* mir, RegLocation rlDest) |
| 958 | { |
| 959 | oatFlushAllRegs(cUnit); /* Everything to home location */ |
| 960 | uint32_t type_idx = mir->dalvikInsn.vB; |
| 961 | // alloc will always check for resolution, do we also need to verify |
| 962 | // access because the verifier was unable to? |
| 963 | int rTgt; |
| 964 | if (cUnit->compiler->CanAccessInstantiableTypeWithoutChecks( |
| 965 | cUnit->method_idx, cUnit->dex_cache, *cUnit->dex_file, type_idx)) { |
| 966 | rTgt = loadHelper(cUnit, OFFSETOF_MEMBER(Thread, pAllocObjectFromCode)); |
| 967 | } else { |
| 968 | rTgt = loadHelper(cUnit, OFFSETOF_MEMBER(Thread, |
| 969 | pAllocObjectFromCodeWithAccessCheck)); |
| 970 | } |
| 971 | loadCurrMethodDirect(cUnit, rARG1); // arg1 <= Method* |
| 972 | loadConstant(cUnit, rARG0, type_idx); // arg0 <- type_idx |
| 973 | callRuntimeHelper(cUnit, rTgt); |
| 974 | RegLocation rlResult = oatGetReturn(cUnit); |
| 975 | storeValue(cUnit, rlDest, rlResult); |
| 976 | } |
| 977 | |
| 978 | void genInstanceof(CompilationUnit* cUnit, MIR* mir, RegLocation rlDest, |
| 979 | RegLocation rlSrc) |
| 980 | { |
| 981 | oatFlushAllRegs(cUnit); |
| 982 | // May generate a call - use explicit registers |
| 983 | oatLockCallTemps(cUnit); |
| 984 | uint32_t type_idx = mir->dalvikInsn.vC; |
buzbee | 5de3494 | 2012-03-01 14:51:57 -0800 | [diff] [blame] | 985 | loadCurrMethodDirect(cUnit, rARG1); // rARG1 <= current Method* |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 986 | int classReg = rARG2; // rARG2 will hold the Class* |
| 987 | if (!cUnit->compiler->CanAccessTypeWithoutChecks(cUnit->method_idx, |
| 988 | cUnit->dex_cache, |
| 989 | *cUnit->dex_file, |
| 990 | type_idx)) { |
| 991 | // Check we have access to type_idx and if not throw IllegalAccessError, |
buzbee | 5de3494 | 2012-03-01 14:51:57 -0800 | [diff] [blame] | 992 | // returns Class* in rARG0 |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 993 | int rTgt = loadHelper(cUnit, OFFSETOF_MEMBER(Thread, |
| 994 | pInitializeTypeAndVerifyAccessFromCode)); |
| 995 | loadConstant(cUnit, rARG0, type_idx); |
| 996 | callRuntimeHelper(cUnit, rTgt); // InitializeTypeAndVerifyAccess(idx, method) |
buzbee | 82488f5 | 2012-03-02 08:20:26 -0800 | [diff] [blame] | 997 | opRegCopy(cUnit, classReg, rRET0); // Align usage with fast path |
buzbee | 5de3494 | 2012-03-01 14:51:57 -0800 | [diff] [blame] | 998 | loadValueDirectFixed(cUnit, rlSrc, rARG0); // rARG0 <= ref |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 999 | } else { |
buzbee | 5de3494 | 2012-03-01 14:51:57 -0800 | [diff] [blame] | 1000 | // Load dex cache entry into classReg (rARG2) |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 1001 | loadValueDirectFixed(cUnit, rlSrc, rARG0); // rARG0 <= ref |
| 1002 | loadWordDisp(cUnit, rARG1, |
| 1003 | Method::DexCacheResolvedTypesOffset().Int32Value(), |
| 1004 | classReg); |
| 1005 | int32_t offset_of_type = |
| 1006 | Array::DataOffset(sizeof(Class*)).Int32Value() + (sizeof(Class*) |
| 1007 | * type_idx); |
| 1008 | loadWordDisp(cUnit, classReg, offset_of_type, classReg); |
| 1009 | if (!cUnit->compiler->CanAssumeTypeIsPresentInDexCache( |
| 1010 | cUnit->dex_cache, type_idx)) { |
| 1011 | // Need to test presence of type in dex cache at runtime |
buzbee | 82488f5 | 2012-03-02 08:20:26 -0800 | [diff] [blame] | 1012 | LIR* hopBranch = opCmpImmBranch(cUnit, kCondNe, classReg, 0, NULL); |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 1013 | // Not resolved |
| 1014 | // Call out to helper, which will return resolved type in rRET0 |
| 1015 | int rTgt = loadHelper(cUnit, OFFSETOF_MEMBER(Thread, |
| 1016 | pInitializeTypeFromCode)); |
| 1017 | loadConstant(cUnit, rARG0, type_idx); |
| 1018 | callRuntimeHelper(cUnit, rTgt); // InitializeTypeFromCode(idx, method) |
buzbee | 82488f5 | 2012-03-02 08:20:26 -0800 | [diff] [blame] | 1019 | opRegCopy(cUnit, rARG2, rRET0); // Align usage with fast path |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 1020 | loadValueDirectFixed(cUnit, rlSrc, rARG0); /* reload Ref */ |
| 1021 | // Rejoin code paths |
| 1022 | LIR* hopTarget = newLIR0(cUnit, kPseudoTargetLabel); |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 1023 | hopBranch->target = (LIR*)hopTarget; |
| 1024 | } |
| 1025 | } |
| 1026 | /* rARG0 is ref, rARG2 is class. If ref==null, use directly as bool result */ |
buzbee | 82488f5 | 2012-03-02 08:20:26 -0800 | [diff] [blame] | 1027 | LIR* branch1 = opCmpImmBranch(cUnit, kCondEq, rARG0, 0, NULL); |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 1028 | /* load object->clazz */ |
| 1029 | DCHECK_EQ(Object::ClassOffset().Int32Value(), 0); |
| 1030 | loadWordDisp(cUnit, rARG0, Object::ClassOffset().Int32Value(), rARG1); |
| 1031 | /* rARG0 is ref, rARG1 is ref->clazz, rARG2 is class */ |
buzbee | 0398c42 | 2012-03-02 15:22:47 -0800 | [diff] [blame] | 1032 | #if defined(TARGET_ARM) |
| 1033 | /* Uses conditional nullification */ |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 1034 | int rTgt = loadHelper(cUnit, OFFSETOF_MEMBER(Thread, |
| 1035 | pInstanceofNonTrivialFromCode)); |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 1036 | opRegReg(cUnit, kOpCmp, rARG1, rARG2); // Same? |
buzbee | 82488f5 | 2012-03-02 08:20:26 -0800 | [diff] [blame] | 1037 | opIT(cUnit, kArmCondEq, "EE"); // if-convert the test |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 1038 | loadConstant(cUnit, rARG0, 1); // .eq case - load true |
buzbee | 82488f5 | 2012-03-02 08:20:26 -0800 | [diff] [blame] | 1039 | opRegCopy(cUnit, rARG0, rARG2); // .ne case - arg0 <= class |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 1040 | opReg(cUnit, kOpBlx, rTgt); // .ne case: helper(class, ref->class) |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 1041 | #else |
buzbee | 0398c42 | 2012-03-02 15:22:47 -0800 | [diff] [blame] | 1042 | /* Uses branchovers */ |
| 1043 | loadConstant(cUnit, rARG0, 1); // assume true |
| 1044 | LIR* branchover = opCmpBranch(cUnit, kCondEq, rARG1, rARG2, NULL); |
| 1045 | int rTgt = loadHelper(cUnit, OFFSETOF_MEMBER(Thread, |
| 1046 | pInstanceofNonTrivialFromCode)); |
| 1047 | opRegCopy(cUnit, rARG0, rARG2); // .ne case - arg0 <= class |
| 1048 | opReg(cUnit, kOpBlx, rTgt); // .ne case: helper(class, ref->class) |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 1049 | #endif |
buzbee | 0398c42 | 2012-03-02 15:22:47 -0800 | [diff] [blame] | 1050 | oatClobberCalleeSave(cUnit); |
| 1051 | /* branch targets here */ |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 1052 | LIR* target = newLIR0(cUnit, kPseudoTargetLabel); |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 1053 | RegLocation rlResult = oatGetReturn(cUnit); |
| 1054 | storeValue(cUnit, rlDest, rlResult); |
buzbee | 0398c42 | 2012-03-02 15:22:47 -0800 | [diff] [blame] | 1055 | branch1->target = target; |
| 1056 | #if !defined(TARGET_ARM) |
| 1057 | branchover->target = target; |
| 1058 | #endif |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 1059 | } |
| 1060 | |
| 1061 | void genCheckCast(CompilationUnit* cUnit, MIR* mir, RegLocation rlSrc) |
| 1062 | { |
| 1063 | oatFlushAllRegs(cUnit); |
| 1064 | // May generate a call - use explicit registers |
| 1065 | oatLockCallTemps(cUnit); |
| 1066 | uint32_t type_idx = mir->dalvikInsn.vB; |
| 1067 | loadCurrMethodDirect(cUnit, rARG1); // rARG1 <= current Method* |
| 1068 | int classReg = rARG2; // rARG2 will hold the Class* |
| 1069 | if (!cUnit->compiler->CanAccessTypeWithoutChecks(cUnit->method_idx, |
| 1070 | cUnit->dex_cache, |
| 1071 | *cUnit->dex_file, |
| 1072 | type_idx)) { |
| 1073 | // Check we have access to type_idx and if not throw IllegalAccessError, |
| 1074 | // returns Class* in rRET0 |
| 1075 | int rTgt = loadHelper(cUnit, OFFSETOF_MEMBER(Thread, |
| 1076 | pInitializeTypeAndVerifyAccessFromCode)); |
| 1077 | loadConstant(cUnit, rARG0, type_idx); |
| 1078 | callRuntimeHelper(cUnit, rTgt); // InitializeTypeAndVerifyAccess(idx, method) |
buzbee | 82488f5 | 2012-03-02 08:20:26 -0800 | [diff] [blame] | 1079 | opRegCopy(cUnit, classReg, rRET0); // Align usage with fast path |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 1080 | } else { |
| 1081 | // Load dex cache entry into classReg (rARG2) |
| 1082 | loadWordDisp(cUnit, rARG1, |
| 1083 | Method::DexCacheResolvedTypesOffset().Int32Value(), |
| 1084 | classReg); |
| 1085 | int32_t offset_of_type = |
| 1086 | Array::DataOffset(sizeof(Class*)).Int32Value() + |
| 1087 | (sizeof(Class*) * type_idx); |
| 1088 | loadWordDisp(cUnit, classReg, offset_of_type, classReg); |
| 1089 | if (!cUnit->compiler->CanAssumeTypeIsPresentInDexCache( |
| 1090 | cUnit->dex_cache, type_idx)) { |
| 1091 | // Need to test presence of type in dex cache at runtime |
buzbee | 82488f5 | 2012-03-02 08:20:26 -0800 | [diff] [blame] | 1092 | LIR* hopBranch = opCmpImmBranch(cUnit, kCondNe, classReg, 0, NULL); |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 1093 | // Not resolved |
buzbee | 5de3494 | 2012-03-01 14:51:57 -0800 | [diff] [blame] | 1094 | // Call out to helper, which will return resolved type in rARG0 |
| 1095 | int rTgt = loadHelper(cUnit, OFFSETOF_MEMBER(Thread, pInitializeTypeFromCode)); |
| 1096 | loadConstant(cUnit, rARG0, type_idx); |
| 1097 | callRuntimeHelper(cUnit, rTgt); // InitializeTypeFromCode(idx, method) |
buzbee | 82488f5 | 2012-03-02 08:20:26 -0800 | [diff] [blame] | 1098 | opRegCopy(cUnit, classReg, rARG0); // Align usage with fast path |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 1099 | // Rejoin code paths |
| 1100 | LIR* hopTarget = newLIR0(cUnit, kPseudoTargetLabel); |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 1101 | hopBranch->target = (LIR*)hopTarget; |
| 1102 | } |
| 1103 | } |
buzbee | 5de3494 | 2012-03-01 14:51:57 -0800 | [diff] [blame] | 1104 | // At this point, classReg (rARG2) has class |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 1105 | loadValueDirectFixed(cUnit, rlSrc, rARG0); // rARG0 <= ref |
| 1106 | /* Null is OK - continue */ |
buzbee | 82488f5 | 2012-03-02 08:20:26 -0800 | [diff] [blame] | 1107 | LIR* branch1 = opCmpImmBranch(cUnit, kCondEq, rARG0, 0, NULL); |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 1108 | /* load object->clazz */ |
| 1109 | DCHECK_EQ(Object::ClassOffset().Int32Value(), 0); |
| 1110 | loadWordDisp(cUnit, rARG0, Object::ClassOffset().Int32Value(), rARG1); |
| 1111 | /* rARG1 now contains object->clazz */ |
| 1112 | int rTgt = loadHelper(cUnit, OFFSETOF_MEMBER(Thread, |
| 1113 | pCheckCastFromCode)); |
Ian Rogers | b5d09b2 | 2012-03-06 22:14:17 -0800 | [diff] [blame] | 1114 | #if defined(TARGET_MIPS) || defined(TARGET_X86) |
buzbee | 82488f5 | 2012-03-02 08:20:26 -0800 | [diff] [blame] | 1115 | LIR* branch2 = opCmpBranch(cUnit, kCondEq, rARG1, classReg, NULL); |
buzbee | 5de3494 | 2012-03-01 14:51:57 -0800 | [diff] [blame] | 1116 | #else |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 1117 | opRegReg(cUnit, kOpCmp, rARG1, classReg); |
buzbee | 82488f5 | 2012-03-02 08:20:26 -0800 | [diff] [blame] | 1118 | LIR* branch2 = opCondBranch(cUnit, kCondEq, NULL); /* If eq, trivial yes */ |
buzbee | 5de3494 | 2012-03-01 14:51:57 -0800 | [diff] [blame] | 1119 | #endif |
buzbee | 82488f5 | 2012-03-02 08:20:26 -0800 | [diff] [blame] | 1120 | opRegCopy(cUnit, rARG0, rARG1); |
| 1121 | opRegCopy(cUnit, rARG1, rARG2); |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 1122 | callRuntimeHelper(cUnit, rTgt); |
| 1123 | /* branch target here */ |
| 1124 | LIR* target = newLIR0(cUnit, kPseudoTargetLabel); |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 1125 | branch1->target = (LIR*)target; |
| 1126 | branch2->target = (LIR*)target; |
| 1127 | } |
| 1128 | |
| 1129 | |
| 1130 | void genThrow(CompilationUnit* cUnit, MIR* mir, RegLocation rlSrc) |
| 1131 | { |
| 1132 | oatFlushAllRegs(cUnit); |
| 1133 | int rTgt = loadHelper(cUnit, OFFSETOF_MEMBER(Thread, pDeliverException)); |
| 1134 | loadValueDirectFixed(cUnit, rlSrc, rARG0); // Get exception object |
| 1135 | callRuntimeHelper(cUnit, rTgt); // art_deliver_exception(exception); |
| 1136 | } |
| 1137 | |
| 1138 | /* |
| 1139 | * Generate array store |
| 1140 | * |
| 1141 | */ |
| 1142 | void genArrayObjPut(CompilationUnit* cUnit, MIR* mir, RegLocation rlArray, |
| 1143 | RegLocation rlIndex, RegLocation rlSrc, int scale) |
| 1144 | { |
| 1145 | RegisterClass regClass = oatRegClassBySize(kWord); |
| 1146 | int lenOffset = Array::LengthOffset().Int32Value(); |
| 1147 | int dataOffset = Array::DataOffset(sizeof(Object*)).Int32Value(); |
| 1148 | |
| 1149 | oatFlushAllRegs(cUnit); |
| 1150 | /* Make sure it's a legal object Put. Use direct regs at first */ |
| 1151 | loadValueDirectFixed(cUnit, rlArray, rARG1); |
| 1152 | loadValueDirectFixed(cUnit, rlSrc, rARG0); |
| 1153 | |
| 1154 | /* null array object? */ |
| 1155 | genNullCheck(cUnit, rlArray.sRegLow, rARG1, mir); |
| 1156 | int rTgt = loadHelper(cUnit, OFFSETOF_MEMBER(Thread, |
| 1157 | pCanPutArrayElementFromCode)); |
| 1158 | /* Get the array's clazz */ |
| 1159 | loadWordDisp(cUnit, rARG1, Object::ClassOffset().Int32Value(), rARG1); |
| 1160 | callRuntimeHelper(cUnit, rTgt); |
| 1161 | oatFreeTemp(cUnit, rARG0); |
| 1162 | oatFreeTemp(cUnit, rARG1); |
| 1163 | |
| 1164 | // Now, redo loadValues in case they didn't survive the call |
| 1165 | |
| 1166 | int regPtr; |
| 1167 | rlArray = loadValue(cUnit, rlArray, kCoreReg); |
| 1168 | rlIndex = loadValue(cUnit, rlIndex, kCoreReg); |
| 1169 | |
| 1170 | if (oatIsTemp(cUnit, rlArray.lowReg)) { |
| 1171 | oatClobber(cUnit, rlArray.lowReg); |
| 1172 | regPtr = rlArray.lowReg; |
| 1173 | } else { |
| 1174 | regPtr = oatAllocTemp(cUnit); |
buzbee | 82488f5 | 2012-03-02 08:20:26 -0800 | [diff] [blame] | 1175 | opRegCopy(cUnit, regPtr, rlArray.lowReg); |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 1176 | } |
| 1177 | |
| 1178 | if (!(mir->optimizationFlags & MIR_IGNORE_RANGE_CHECK)) { |
| 1179 | int regLen = oatAllocTemp(cUnit); |
| 1180 | //NOTE: max live temps(4) here. |
| 1181 | /* Get len */ |
| 1182 | loadWordDisp(cUnit, rlArray.lowReg, lenOffset, regLen); |
| 1183 | /* regPtr -> array data */ |
| 1184 | opRegImm(cUnit, kOpAdd, regPtr, dataOffset); |
| 1185 | genRegRegCheck(cUnit, kCondCs, rlIndex.lowReg, regLen, mir, |
| 1186 | kThrowArrayBounds); |
| 1187 | oatFreeTemp(cUnit, regLen); |
| 1188 | } else { |
| 1189 | /* regPtr -> array data */ |
| 1190 | opRegImm(cUnit, kOpAdd, regPtr, dataOffset); |
| 1191 | } |
| 1192 | /* at this point, regPtr points to array, 2 live temps */ |
| 1193 | rlSrc = loadValue(cUnit, rlSrc, regClass); |
| 1194 | storeBaseIndexed(cUnit, regPtr, rlIndex.lowReg, rlSrc.lowReg, |
| 1195 | scale, kWord); |
| 1196 | } |
| 1197 | |
| 1198 | /* |
| 1199 | * Generate array load |
| 1200 | */ |
| 1201 | void genArrayGet(CompilationUnit* cUnit, MIR* mir, OpSize size, |
| 1202 | RegLocation rlArray, RegLocation rlIndex, |
| 1203 | RegLocation rlDest, int scale) |
| 1204 | { |
| 1205 | RegisterClass regClass = oatRegClassBySize(size); |
| 1206 | int lenOffset = Array::LengthOffset().Int32Value(); |
| 1207 | int dataOffset; |
| 1208 | RegLocation rlResult; |
| 1209 | rlArray = loadValue(cUnit, rlArray, kCoreReg); |
| 1210 | rlIndex = loadValue(cUnit, rlIndex, kCoreReg); |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 1211 | |
| 1212 | if (size == kLong || size == kDouble) { |
| 1213 | dataOffset = Array::DataOffset(sizeof(int64_t)).Int32Value(); |
| 1214 | } else { |
| 1215 | dataOffset = Array::DataOffset(sizeof(int32_t)).Int32Value(); |
| 1216 | } |
| 1217 | |
| 1218 | /* null object? */ |
| 1219 | genNullCheck(cUnit, rlArray.sRegLow, rlArray.lowReg, mir); |
| 1220 | |
Ian Rogers | b5d09b2 | 2012-03-06 22:14:17 -0800 | [diff] [blame] | 1221 | #if defined(TARGET_X86) |
| 1222 | if (!(mir->optimizationFlags & MIR_IGNORE_RANGE_CHECK)) { |
| 1223 | /* if (rlIndex >= [rlArray + lenOffset]) goto kThrowArrayBounds */ |
| 1224 | genRegMemCheck(cUnit, kCondUge, rlIndex.lowReg, rlArray.lowReg, |
| 1225 | lenOffset, mir, kThrowArrayBounds); |
| 1226 | } |
| 1227 | if ((size == kLong) || (size == kDouble)) { |
| 1228 | rlResult = oatEvalLoc(cUnit, rlDest, regClass, true); |
| 1229 | loadBaseIndexedDisp(cUnit, NULL, rlArray.lowReg, rlIndex.lowReg, scale, dataOffset, |
| 1230 | rlResult.lowReg, rlResult.highReg, size, INVALID_SREG); |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 1231 | |
Ian Rogers | b5d09b2 | 2012-03-06 22:14:17 -0800 | [diff] [blame] | 1232 | storeValueWide(cUnit, rlDest, rlResult); |
| 1233 | } else { |
| 1234 | rlResult = oatEvalLoc(cUnit, rlDest, regClass, true); |
| 1235 | |
| 1236 | loadBaseIndexedDisp(cUnit, NULL, rlArray.lowReg, rlIndex.lowReg, scale, dataOffset, |
| 1237 | rlResult.lowReg, INVALID_REG, size, INVALID_SREG); |
| 1238 | |
| 1239 | storeValue(cUnit, rlDest, rlResult); |
| 1240 | } |
| 1241 | #else |
| 1242 | int regPtr = oatAllocTemp(cUnit); |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 1243 | if (!(mir->optimizationFlags & MIR_IGNORE_RANGE_CHECK)) { |
| 1244 | int regLen = oatAllocTemp(cUnit); |
| 1245 | /* Get len */ |
| 1246 | loadWordDisp(cUnit, rlArray.lowReg, lenOffset, regLen); |
| 1247 | /* regPtr -> array data */ |
| 1248 | opRegRegImm(cUnit, kOpAdd, regPtr, rlArray.lowReg, dataOffset); |
Ian Rogers | b5d09b2 | 2012-03-06 22:14:17 -0800 | [diff] [blame] | 1249 | // TODO: change kCondCS to a more meaningful name, is the sense of |
| 1250 | // carry-set/clear flipped? |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 1251 | genRegRegCheck(cUnit, kCondCs, rlIndex.lowReg, regLen, mir, |
| 1252 | kThrowArrayBounds); |
| 1253 | oatFreeTemp(cUnit, regLen); |
| 1254 | } else { |
| 1255 | /* regPtr -> array data */ |
| 1256 | opRegRegImm(cUnit, kOpAdd, regPtr, rlArray.lowReg, dataOffset); |
| 1257 | } |
| 1258 | oatFreeTemp(cUnit, rlArray.lowReg); |
| 1259 | if ((size == kLong) || (size == kDouble)) { |
| 1260 | if (scale) { |
| 1261 | int rNewIndex = oatAllocTemp(cUnit); |
| 1262 | opRegRegImm(cUnit, kOpLsl, rNewIndex, rlIndex.lowReg, scale); |
| 1263 | opRegReg(cUnit, kOpAdd, regPtr, rNewIndex); |
| 1264 | oatFreeTemp(cUnit, rNewIndex); |
| 1265 | } else { |
| 1266 | opRegReg(cUnit, kOpAdd, regPtr, rlIndex.lowReg); |
| 1267 | } |
| 1268 | oatFreeTemp(cUnit, rlIndex.lowReg); |
| 1269 | rlResult = oatEvalLoc(cUnit, rlDest, regClass, true); |
| 1270 | |
| 1271 | loadPair(cUnit, regPtr, rlResult.lowReg, rlResult.highReg); |
| 1272 | |
| 1273 | oatFreeTemp(cUnit, regPtr); |
| 1274 | storeValueWide(cUnit, rlDest, rlResult); |
| 1275 | } else { |
| 1276 | rlResult = oatEvalLoc(cUnit, rlDest, regClass, true); |
| 1277 | |
| 1278 | loadBaseIndexed(cUnit, regPtr, rlIndex.lowReg, rlResult.lowReg, |
| 1279 | scale, size); |
| 1280 | |
| 1281 | oatFreeTemp(cUnit, regPtr); |
| 1282 | storeValue(cUnit, rlDest, rlResult); |
| 1283 | } |
Ian Rogers | b5d09b2 | 2012-03-06 22:14:17 -0800 | [diff] [blame] | 1284 | #endif |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 1285 | } |
| 1286 | |
| 1287 | /* |
| 1288 | * Generate array store |
| 1289 | * |
| 1290 | */ |
| 1291 | void genArrayPut(CompilationUnit* cUnit, MIR* mir, OpSize size, |
| 1292 | RegLocation rlArray, RegLocation rlIndex, |
| 1293 | RegLocation rlSrc, int scale) |
| 1294 | { |
| 1295 | RegisterClass regClass = oatRegClassBySize(size); |
| 1296 | int lenOffset = Array::LengthOffset().Int32Value(); |
| 1297 | int dataOffset; |
| 1298 | |
| 1299 | if (size == kLong || size == kDouble) { |
| 1300 | dataOffset = Array::DataOffset(sizeof(int64_t)).Int32Value(); |
| 1301 | } else { |
| 1302 | dataOffset = Array::DataOffset(sizeof(int32_t)).Int32Value(); |
| 1303 | } |
| 1304 | |
| 1305 | int regPtr; |
| 1306 | rlArray = loadValue(cUnit, rlArray, kCoreReg); |
| 1307 | rlIndex = loadValue(cUnit, rlIndex, kCoreReg); |
| 1308 | |
| 1309 | if (oatIsTemp(cUnit, rlArray.lowReg)) { |
| 1310 | oatClobber(cUnit, rlArray.lowReg); |
| 1311 | regPtr = rlArray.lowReg; |
| 1312 | } else { |
| 1313 | regPtr = oatAllocTemp(cUnit); |
buzbee | 82488f5 | 2012-03-02 08:20:26 -0800 | [diff] [blame] | 1314 | opRegCopy(cUnit, regPtr, rlArray.lowReg); |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 1315 | } |
| 1316 | |
| 1317 | /* null object? */ |
| 1318 | genNullCheck(cUnit, rlArray.sRegLow, rlArray.lowReg, mir); |
| 1319 | |
| 1320 | if (!(mir->optimizationFlags & MIR_IGNORE_RANGE_CHECK)) { |
| 1321 | int regLen = oatAllocTemp(cUnit); |
| 1322 | //NOTE: max live temps(4) here. |
| 1323 | /* Get len */ |
| 1324 | loadWordDisp(cUnit, rlArray.lowReg, lenOffset, regLen); |
| 1325 | /* regPtr -> array data */ |
| 1326 | opRegImm(cUnit, kOpAdd, regPtr, dataOffset); |
| 1327 | genRegRegCheck(cUnit, kCondCs, rlIndex.lowReg, regLen, mir, |
| 1328 | kThrowArrayBounds); |
| 1329 | oatFreeTemp(cUnit, regLen); |
| 1330 | } else { |
| 1331 | /* regPtr -> array data */ |
| 1332 | opRegImm(cUnit, kOpAdd, regPtr, dataOffset); |
| 1333 | } |
| 1334 | /* at this point, regPtr points to array, 2 live temps */ |
| 1335 | if ((size == kLong) || (size == kDouble)) { |
| 1336 | //TUNING: specific wide routine that can handle fp regs |
| 1337 | if (scale) { |
| 1338 | int rNewIndex = oatAllocTemp(cUnit); |
| 1339 | opRegRegImm(cUnit, kOpLsl, rNewIndex, rlIndex.lowReg, scale); |
| 1340 | opRegReg(cUnit, kOpAdd, regPtr, rNewIndex); |
| 1341 | oatFreeTemp(cUnit, rNewIndex); |
| 1342 | } else { |
| 1343 | opRegReg(cUnit, kOpAdd, regPtr, rlIndex.lowReg); |
| 1344 | } |
| 1345 | rlSrc = loadValueWide(cUnit, rlSrc, regClass); |
| 1346 | |
| 1347 | storePair(cUnit, regPtr, rlSrc.lowReg, rlSrc.highReg); |
| 1348 | |
| 1349 | oatFreeTemp(cUnit, regPtr); |
| 1350 | } else { |
| 1351 | rlSrc = loadValue(cUnit, rlSrc, regClass); |
| 1352 | |
| 1353 | storeBaseIndexed(cUnit, regPtr, rlIndex.lowReg, rlSrc.lowReg, |
| 1354 | scale, size); |
| 1355 | } |
| 1356 | } |
| 1357 | |
| 1358 | void genLong3Addr(CompilationUnit* cUnit, MIR* mir, OpKind firstOp, |
| 1359 | OpKind secondOp, RegLocation rlDest, |
| 1360 | RegLocation rlSrc1, RegLocation rlSrc2) |
| 1361 | { |
| 1362 | RegLocation rlResult; |
| 1363 | #if defined(TARGET_ARM) |
| 1364 | /* |
| 1365 | * NOTE: This is the one place in the code in which we might have |
| 1366 | * as many as six live temporary registers. There are 5 in the normal |
| 1367 | * set for Arm. Until we have spill capabilities, temporarily add |
| 1368 | * lr to the temp set. It is safe to do this locally, but note that |
| 1369 | * lr is used explicitly elsewhere in the code generator and cannot |
| 1370 | * normally be used as a general temp register. |
| 1371 | */ |
| 1372 | oatMarkTemp(cUnit, rLR); // Add lr to the temp pool |
| 1373 | oatFreeTemp(cUnit, rLR); // and make it available |
| 1374 | #endif |
| 1375 | rlSrc1 = loadValueWide(cUnit, rlSrc1, kCoreReg); |
| 1376 | rlSrc2 = loadValueWide(cUnit, rlSrc2, kCoreReg); |
| 1377 | rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true); |
| 1378 | // The longs may overlap - use intermediate temp if so |
| 1379 | if (rlResult.lowReg == rlSrc1.highReg) { |
| 1380 | int tReg = oatAllocTemp(cUnit); |
buzbee | 82488f5 | 2012-03-02 08:20:26 -0800 | [diff] [blame] | 1381 | opRegCopy(cUnit, tReg, rlSrc1.highReg); |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 1382 | opRegRegReg(cUnit, firstOp, rlResult.lowReg, rlSrc1.lowReg, |
| 1383 | rlSrc2.lowReg); |
| 1384 | opRegRegReg(cUnit, secondOp, rlResult.highReg, tReg, |
| 1385 | rlSrc2.highReg); |
| 1386 | oatFreeTemp(cUnit, tReg); |
| 1387 | } else { |
| 1388 | opRegRegReg(cUnit, firstOp, rlResult.lowReg, rlSrc1.lowReg, |
| 1389 | rlSrc2.lowReg); |
| 1390 | opRegRegReg(cUnit, secondOp, rlResult.highReg, rlSrc1.highReg, |
| 1391 | rlSrc2.highReg); |
| 1392 | } |
| 1393 | /* |
| 1394 | * NOTE: If rlDest refers to a frame variable in a large frame, the |
| 1395 | * following storeValueWide might need to allocate a temp register. |
| 1396 | * To further work around the lack of a spill capability, explicitly |
| 1397 | * free any temps from rlSrc1 & rlSrc2 that aren't still live in rlResult. |
| 1398 | * Remove when spill is functional. |
| 1399 | */ |
| 1400 | freeRegLocTemps(cUnit, rlResult, rlSrc1); |
| 1401 | freeRegLocTemps(cUnit, rlResult, rlSrc2); |
| 1402 | storeValueWide(cUnit, rlDest, rlResult); |
| 1403 | #if defined(TARGET_ARM) |
| 1404 | oatClobber(cUnit, rLR); |
| 1405 | oatUnmarkTemp(cUnit, rLR); // Remove lr from the temp pool |
| 1406 | #endif |
| 1407 | } |
| 1408 | |
| 1409 | |
| 1410 | bool genShiftOpLong(CompilationUnit* cUnit, MIR* mir, RegLocation rlDest, |
| 1411 | RegLocation rlSrc1, RegLocation rlShift) |
| 1412 | { |
| 1413 | int funcOffset; |
| 1414 | |
| 1415 | switch( mir->dalvikInsn.opcode) { |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 1416 | case Instruction::SHL_LONG: |
| 1417 | case Instruction::SHL_LONG_2ADDR: |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 1418 | funcOffset = OFFSETOF_MEMBER(Thread, pShlLong); |
| 1419 | break; |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 1420 | case Instruction::SHR_LONG: |
| 1421 | case Instruction::SHR_LONG_2ADDR: |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 1422 | funcOffset = OFFSETOF_MEMBER(Thread, pShrLong); |
| 1423 | break; |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 1424 | case Instruction::USHR_LONG: |
| 1425 | case Instruction::USHR_LONG_2ADDR: |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 1426 | funcOffset = OFFSETOF_MEMBER(Thread, pUshrLong); |
| 1427 | break; |
| 1428 | default: |
| 1429 | LOG(FATAL) << "Unexpected case"; |
| 1430 | return true; |
| 1431 | } |
| 1432 | oatFlushAllRegs(cUnit); /* Send everything to home location */ |
| 1433 | int rTgt = loadHelper(cUnit, funcOffset); |
| 1434 | loadValueDirectWideFixed(cUnit, rlSrc1, rARG0, rARG1); |
| 1435 | loadValueDirect(cUnit, rlShift, rARG2); |
| 1436 | callRuntimeHelper(cUnit, rTgt); |
| 1437 | RegLocation rlResult = oatGetReturnWide(cUnit); |
| 1438 | storeValueWide(cUnit, rlDest, rlResult); |
| 1439 | return false; |
| 1440 | } |
| 1441 | |
| 1442 | |
| 1443 | bool genArithOpInt(CompilationUnit* cUnit, MIR* mir, RegLocation rlDest, |
| 1444 | RegLocation rlSrc1, RegLocation rlSrc2) |
| 1445 | { |
| 1446 | OpKind op = kOpBkpt; |
| 1447 | bool callOut = false; |
| 1448 | bool checkZero = false; |
| 1449 | bool unary = false; |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 1450 | RegLocation rlResult; |
| 1451 | bool shiftOp = false; |
Ian Rogers | b5d09b2 | 2012-03-06 22:14:17 -0800 | [diff] [blame] | 1452 | int funcOffset; |
| 1453 | int retReg = rRET0; |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 1454 | switch (mir->dalvikInsn.opcode) { |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 1455 | case Instruction::NEG_INT: |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 1456 | op = kOpNeg; |
| 1457 | unary = true; |
| 1458 | break; |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 1459 | case Instruction::NOT_INT: |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 1460 | op = kOpMvn; |
| 1461 | unary = true; |
| 1462 | break; |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 1463 | case Instruction::ADD_INT: |
| 1464 | case Instruction::ADD_INT_2ADDR: |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 1465 | op = kOpAdd; |
| 1466 | break; |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 1467 | case Instruction::SUB_INT: |
| 1468 | case Instruction::SUB_INT_2ADDR: |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 1469 | op = kOpSub; |
| 1470 | break; |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 1471 | case Instruction::MUL_INT: |
| 1472 | case Instruction::MUL_INT_2ADDR: |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 1473 | op = kOpMul; |
| 1474 | break; |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 1475 | case Instruction::DIV_INT: |
| 1476 | case Instruction::DIV_INT_2ADDR: |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 1477 | checkZero = true; |
Ian Rogers | b5d09b2 | 2012-03-06 22:14:17 -0800 | [diff] [blame] | 1478 | op = kOpDiv; |
| 1479 | callOut = true; |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 1480 | funcOffset = OFFSETOF_MEMBER(Thread, pIdiv); |
| 1481 | retReg = rRET0; |
| 1482 | break; |
buzbee | 5de3494 | 2012-03-01 14:51:57 -0800 | [diff] [blame] | 1483 | /* NOTE: returns in rARG1 */ |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 1484 | case Instruction::REM_INT: |
| 1485 | case Instruction::REM_INT_2ADDR: |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 1486 | checkZero = true; |
Ian Rogers | b5d09b2 | 2012-03-06 22:14:17 -0800 | [diff] [blame] | 1487 | op = kOpRem; |
| 1488 | callOut = true; |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 1489 | funcOffset = OFFSETOF_MEMBER(Thread, pIdivmod); |
| 1490 | retReg = rRET1; |
| 1491 | break; |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 1492 | case Instruction::AND_INT: |
| 1493 | case Instruction::AND_INT_2ADDR: |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 1494 | op = kOpAnd; |
| 1495 | break; |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 1496 | case Instruction::OR_INT: |
| 1497 | case Instruction::OR_INT_2ADDR: |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 1498 | op = kOpOr; |
| 1499 | break; |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 1500 | case Instruction::XOR_INT: |
| 1501 | case Instruction::XOR_INT_2ADDR: |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 1502 | op = kOpXor; |
| 1503 | break; |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 1504 | case Instruction::SHL_INT: |
| 1505 | case Instruction::SHL_INT_2ADDR: |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 1506 | shiftOp = true; |
| 1507 | op = kOpLsl; |
| 1508 | break; |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 1509 | case Instruction::SHR_INT: |
| 1510 | case Instruction::SHR_INT_2ADDR: |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 1511 | shiftOp = true; |
| 1512 | op = kOpAsr; |
| 1513 | break; |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 1514 | case Instruction::USHR_INT: |
| 1515 | case Instruction::USHR_INT_2ADDR: |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 1516 | shiftOp = true; |
| 1517 | op = kOpLsr; |
| 1518 | break; |
| 1519 | default: |
| 1520 | LOG(FATAL) << "Invalid word arith op: " << |
| 1521 | (int)mir->dalvikInsn.opcode; |
| 1522 | } |
| 1523 | if (!callOut) { |
| 1524 | rlSrc1 = loadValue(cUnit, rlSrc1, kCoreReg); |
| 1525 | if (unary) { |
| 1526 | rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true); |
| 1527 | opRegReg(cUnit, op, rlResult.lowReg, |
| 1528 | rlSrc1.lowReg); |
| 1529 | } else { |
| 1530 | rlSrc2 = loadValue(cUnit, rlSrc2, kCoreReg); |
Ian Rogers | b5d09b2 | 2012-03-06 22:14:17 -0800 | [diff] [blame] | 1531 | #if defined(TARGET_X86) |
| 1532 | rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true); |
| 1533 | opRegRegReg(cUnit, op, rlResult.lowReg, |
| 1534 | rlSrc1.lowReg, rlSrc2.lowReg); |
| 1535 | #else |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 1536 | if (shiftOp) { |
| 1537 | int tReg = oatAllocTemp(cUnit); |
| 1538 | opRegRegImm(cUnit, kOpAnd, tReg, rlSrc2.lowReg, 31); |
| 1539 | rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true); |
| 1540 | opRegRegReg(cUnit, op, rlResult.lowReg, |
| 1541 | rlSrc1.lowReg, tReg); |
| 1542 | oatFreeTemp(cUnit, tReg); |
| 1543 | } else { |
| 1544 | rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true); |
| 1545 | opRegRegReg(cUnit, op, rlResult.lowReg, |
| 1546 | rlSrc1.lowReg, rlSrc2.lowReg); |
| 1547 | } |
Ian Rogers | b5d09b2 | 2012-03-06 22:14:17 -0800 | [diff] [blame] | 1548 | #endif |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 1549 | } |
| 1550 | storeValue(cUnit, rlDest, rlResult); |
| 1551 | } else { |
| 1552 | RegLocation rlResult; |
| 1553 | oatFlushAllRegs(cUnit); /* Send everything to home location */ |
| 1554 | loadValueDirectFixed(cUnit, rlSrc2, rRET1); |
| 1555 | int rTgt = loadHelper(cUnit, funcOffset); |
| 1556 | loadValueDirectFixed(cUnit, rlSrc1, rARG0); |
| 1557 | if (checkZero) { |
| 1558 | genImmedCheck(cUnit, kCondEq, rARG1, 0, mir, kThrowDivZero); |
| 1559 | } |
| 1560 | callRuntimeHelper(cUnit, rTgt); |
| 1561 | if (retReg == rRET0) |
| 1562 | rlResult = oatGetReturn(cUnit); |
| 1563 | else |
| 1564 | rlResult = oatGetReturnAlt(cUnit); |
| 1565 | storeValue(cUnit, rlDest, rlResult); |
| 1566 | } |
| 1567 | return false; |
| 1568 | } |
| 1569 | |
| 1570 | /* |
| 1571 | * The following are the first-level codegen routines that analyze the format |
| 1572 | * of each bytecode then either dispatch special purpose codegen routines |
| 1573 | * or produce corresponding Thumb instructions directly. |
| 1574 | */ |
| 1575 | |
| 1576 | bool isPowerOfTwo(int x) |
| 1577 | { |
| 1578 | return (x & (x - 1)) == 0; |
| 1579 | } |
| 1580 | |
| 1581 | // Returns true if no more than two bits are set in 'x'. |
| 1582 | bool isPopCountLE2(unsigned int x) |
| 1583 | { |
| 1584 | x &= x - 1; |
| 1585 | return (x & (x - 1)) == 0; |
| 1586 | } |
| 1587 | |
| 1588 | // Returns the index of the lowest set bit in 'x'. |
| 1589 | int lowestSetBit(unsigned int x) { |
| 1590 | int bit_posn = 0; |
| 1591 | while ((x & 0xf) == 0) { |
| 1592 | bit_posn += 4; |
| 1593 | x >>= 4; |
| 1594 | } |
| 1595 | while ((x & 1) == 0) { |
| 1596 | bit_posn++; |
| 1597 | x >>= 1; |
| 1598 | } |
| 1599 | return bit_posn; |
| 1600 | } |
| 1601 | |
| 1602 | // Returns true if it added instructions to 'cUnit' to divide 'rlSrc' by 'lit' |
| 1603 | // and store the result in 'rlDest'. |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 1604 | bool handleEasyDivide(CompilationUnit* cUnit, Instruction::Code dalvikOpcode, |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 1605 | RegLocation rlSrc, RegLocation rlDest, int lit) |
| 1606 | { |
| 1607 | if (lit < 2 || !isPowerOfTwo(lit)) { |
| 1608 | return false; |
| 1609 | } |
| 1610 | int k = lowestSetBit(lit); |
| 1611 | if (k >= 30) { |
| 1612 | // Avoid special cases. |
| 1613 | return false; |
| 1614 | } |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 1615 | bool div = (dalvikOpcode == Instruction::DIV_INT_LIT8 || |
| 1616 | dalvikOpcode == Instruction::DIV_INT_LIT16); |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 1617 | rlSrc = loadValue(cUnit, rlSrc, kCoreReg); |
| 1618 | RegLocation rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true); |
| 1619 | if (div) { |
| 1620 | int tReg = oatAllocTemp(cUnit); |
| 1621 | if (lit == 2) { |
| 1622 | // Division by 2 is by far the most common division by constant. |
| 1623 | opRegRegImm(cUnit, kOpLsr, tReg, rlSrc.lowReg, 32 - k); |
| 1624 | opRegRegReg(cUnit, kOpAdd, tReg, tReg, rlSrc.lowReg); |
| 1625 | opRegRegImm(cUnit, kOpAsr, rlResult.lowReg, tReg, k); |
| 1626 | } else { |
| 1627 | opRegRegImm(cUnit, kOpAsr, tReg, rlSrc.lowReg, 31); |
| 1628 | opRegRegImm(cUnit, kOpLsr, tReg, tReg, 32 - k); |
| 1629 | opRegRegReg(cUnit, kOpAdd, tReg, tReg, rlSrc.lowReg); |
| 1630 | opRegRegImm(cUnit, kOpAsr, rlResult.lowReg, tReg, k); |
| 1631 | } |
| 1632 | } else { |
| 1633 | int cReg = oatAllocTemp(cUnit); |
| 1634 | loadConstant(cUnit, cReg, lit - 1); |
| 1635 | int tReg1 = oatAllocTemp(cUnit); |
| 1636 | int tReg2 = oatAllocTemp(cUnit); |
| 1637 | if (lit == 2) { |
| 1638 | opRegRegImm(cUnit, kOpLsr, tReg1, rlSrc.lowReg, 32 - k); |
| 1639 | opRegRegReg(cUnit, kOpAdd, tReg2, tReg1, rlSrc.lowReg); |
| 1640 | opRegRegReg(cUnit, kOpAnd, tReg2, tReg2, cReg); |
| 1641 | opRegRegReg(cUnit, kOpSub, rlResult.lowReg, tReg2, tReg1); |
| 1642 | } else { |
| 1643 | opRegRegImm(cUnit, kOpAsr, tReg1, rlSrc.lowReg, 31); |
| 1644 | opRegRegImm(cUnit, kOpLsr, tReg1, tReg1, 32 - k); |
| 1645 | opRegRegReg(cUnit, kOpAdd, tReg2, tReg1, rlSrc.lowReg); |
| 1646 | opRegRegReg(cUnit, kOpAnd, tReg2, tReg2, cReg); |
| 1647 | opRegRegReg(cUnit, kOpSub, rlResult.lowReg, tReg2, tReg1); |
| 1648 | } |
| 1649 | } |
| 1650 | storeValue(cUnit, rlDest, rlResult); |
| 1651 | return true; |
| 1652 | } |
| 1653 | |
| 1654 | void genMultiplyByTwoBitMultiplier(CompilationUnit* cUnit, RegLocation rlSrc, |
| 1655 | RegLocation rlResult, int lit, |
| 1656 | int firstBit, int secondBit) |
| 1657 | { |
buzbee | 0398c42 | 2012-03-02 15:22:47 -0800 | [diff] [blame] | 1658 | #if defined(TARGET_ARM) |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 1659 | opRegRegRegShift(cUnit, kOpAdd, rlResult.lowReg, rlSrc.lowReg, rlSrc.lowReg, |
| 1660 | encodeShift(kArmLsl, secondBit - firstBit)); |
buzbee | 0398c42 | 2012-03-02 15:22:47 -0800 | [diff] [blame] | 1661 | #else |
| 1662 | int tReg = oatAllocTemp(cUnit); |
| 1663 | opRegRegImm(cUnit, kOpLsl, tReg, rlSrc.lowReg, secondBit - firstBit); |
| 1664 | opRegRegReg(cUnit, kOpAdd, rlResult.lowReg, rlSrc.lowReg, tReg); |
| 1665 | oatFreeTemp(cUnit, tReg); |
buzbee | 5de3494 | 2012-03-01 14:51:57 -0800 | [diff] [blame] | 1666 | #endif |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 1667 | if (firstBit != 0) { |
| 1668 | opRegRegImm(cUnit, kOpLsl, rlResult.lowReg, rlResult.lowReg, firstBit); |
| 1669 | } |
| 1670 | } |
| 1671 | |
| 1672 | // Returns true if it added instructions to 'cUnit' to multiply 'rlSrc' by 'lit' |
| 1673 | // and store the result in 'rlDest'. |
| 1674 | bool handleEasyMultiply(CompilationUnit* cUnit, RegLocation rlSrc, |
| 1675 | RegLocation rlDest, int lit) |
| 1676 | { |
| 1677 | // Can we simplify this multiplication? |
| 1678 | bool powerOfTwo = false; |
| 1679 | bool popCountLE2 = false; |
| 1680 | bool powerOfTwoMinusOne = false; |
| 1681 | if (lit < 2) { |
| 1682 | // Avoid special cases. |
| 1683 | return false; |
| 1684 | } else if (isPowerOfTwo(lit)) { |
| 1685 | powerOfTwo = true; |
| 1686 | } else if (isPopCountLE2(lit)) { |
| 1687 | popCountLE2 = true; |
| 1688 | } else if (isPowerOfTwo(lit + 1)) { |
| 1689 | powerOfTwoMinusOne = true; |
| 1690 | } else { |
| 1691 | return false; |
| 1692 | } |
| 1693 | rlSrc = loadValue(cUnit, rlSrc, kCoreReg); |
| 1694 | RegLocation rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true); |
| 1695 | if (powerOfTwo) { |
| 1696 | // Shift. |
| 1697 | opRegRegImm(cUnit, kOpLsl, rlResult.lowReg, rlSrc.lowReg, |
| 1698 | lowestSetBit(lit)); |
| 1699 | } else if (popCountLE2) { |
| 1700 | // Shift and add and shift. |
| 1701 | int firstBit = lowestSetBit(lit); |
| 1702 | int secondBit = lowestSetBit(lit ^ (1 << firstBit)); |
| 1703 | genMultiplyByTwoBitMultiplier(cUnit, rlSrc, rlResult, lit, |
| 1704 | firstBit, secondBit); |
| 1705 | } else { |
| 1706 | // Reverse subtract: (src << (shift + 1)) - src. |
| 1707 | DCHECK(powerOfTwoMinusOne); |
| 1708 | // TUNING: rsb dst, src, src lsl#lowestSetBit(lit + 1) |
| 1709 | int tReg = oatAllocTemp(cUnit); |
| 1710 | opRegRegImm(cUnit, kOpLsl, tReg, rlSrc.lowReg, lowestSetBit(lit + 1)); |
| 1711 | opRegRegReg(cUnit, kOpSub, rlResult.lowReg, tReg, rlSrc.lowReg); |
| 1712 | } |
| 1713 | storeValue(cUnit, rlDest, rlResult); |
| 1714 | return true; |
| 1715 | } |
| 1716 | |
| 1717 | bool genArithOpIntLit(CompilationUnit* cUnit, MIR* mir, RegLocation rlDest, |
| 1718 | RegLocation rlSrc, int lit) |
| 1719 | { |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 1720 | Instruction::Code dalvikOpcode = mir->dalvikInsn.opcode; |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 1721 | RegLocation rlResult; |
| 1722 | OpKind op = (OpKind)0; /* Make gcc happy */ |
| 1723 | int shiftOp = false; |
| 1724 | bool isDiv = false; |
| 1725 | int funcOffset; |
| 1726 | int rTgt; |
| 1727 | |
| 1728 | switch (dalvikOpcode) { |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 1729 | case Instruction::RSUB_INT_LIT8: |
| 1730 | case Instruction::RSUB_INT: { |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 1731 | int tReg; |
| 1732 | //TUNING: add support for use of Arm rsub op |
| 1733 | rlSrc = loadValue(cUnit, rlSrc, kCoreReg); |
| 1734 | tReg = oatAllocTemp(cUnit); |
| 1735 | loadConstant(cUnit, tReg, lit); |
| 1736 | rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true); |
| 1737 | opRegRegReg(cUnit, kOpSub, rlResult.lowReg, |
| 1738 | tReg, rlSrc.lowReg); |
| 1739 | storeValue(cUnit, rlDest, rlResult); |
| 1740 | return false; |
| 1741 | break; |
| 1742 | } |
| 1743 | |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 1744 | case Instruction::ADD_INT_LIT8: |
| 1745 | case Instruction::ADD_INT_LIT16: |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 1746 | op = kOpAdd; |
| 1747 | break; |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 1748 | case Instruction::MUL_INT_LIT8: |
| 1749 | case Instruction::MUL_INT_LIT16: { |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 1750 | if (handleEasyMultiply(cUnit, rlSrc, rlDest, lit)) { |
| 1751 | return false; |
| 1752 | } |
| 1753 | op = kOpMul; |
| 1754 | break; |
| 1755 | } |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 1756 | case Instruction::AND_INT_LIT8: |
| 1757 | case Instruction::AND_INT_LIT16: |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 1758 | op = kOpAnd; |
| 1759 | break; |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 1760 | case Instruction::OR_INT_LIT8: |
| 1761 | case Instruction::OR_INT_LIT16: |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 1762 | op = kOpOr; |
| 1763 | break; |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 1764 | case Instruction::XOR_INT_LIT8: |
| 1765 | case Instruction::XOR_INT_LIT16: |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 1766 | op = kOpXor; |
| 1767 | break; |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 1768 | case Instruction::SHL_INT_LIT8: |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 1769 | lit &= 31; |
| 1770 | shiftOp = true; |
| 1771 | op = kOpLsl; |
| 1772 | break; |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 1773 | case Instruction::SHR_INT_LIT8: |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 1774 | lit &= 31; |
| 1775 | shiftOp = true; |
| 1776 | op = kOpAsr; |
| 1777 | break; |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 1778 | case Instruction::USHR_INT_LIT8: |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 1779 | lit &= 31; |
| 1780 | shiftOp = true; |
| 1781 | op = kOpLsr; |
| 1782 | break; |
| 1783 | |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 1784 | case Instruction::DIV_INT_LIT8: |
| 1785 | case Instruction::DIV_INT_LIT16: |
| 1786 | case Instruction::REM_INT_LIT8: |
| 1787 | case Instruction::REM_INT_LIT16: |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 1788 | if (lit == 0) { |
| 1789 | genImmedCheck(cUnit, kCondAl, 0, 0, mir, kThrowDivZero); |
| 1790 | return false; |
| 1791 | } |
| 1792 | if (handleEasyDivide(cUnit, dalvikOpcode, rlSrc, rlDest, lit)) { |
| 1793 | return false; |
| 1794 | } |
| 1795 | oatFlushAllRegs(cUnit); /* Everything to home location */ |
| 1796 | loadValueDirectFixed(cUnit, rlSrc, rARG0); |
| 1797 | oatClobber(cUnit, rARG0); |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 1798 | if ((dalvikOpcode == Instruction::DIV_INT_LIT8) || |
| 1799 | (dalvikOpcode == Instruction::DIV_INT_LIT16)) { |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 1800 | funcOffset = OFFSETOF_MEMBER(Thread, pIdiv); |
| 1801 | isDiv = true; |
| 1802 | } else { |
| 1803 | funcOffset = OFFSETOF_MEMBER(Thread, pIdivmod); |
| 1804 | isDiv = false; |
| 1805 | } |
| 1806 | rTgt = loadHelper(cUnit, funcOffset); |
| 1807 | loadConstant(cUnit, rARG1, lit); |
| 1808 | callRuntimeHelper(cUnit, rTgt); |
| 1809 | if (isDiv) |
| 1810 | rlResult = oatGetReturn(cUnit); |
| 1811 | else |
| 1812 | rlResult = oatGetReturnAlt(cUnit); |
| 1813 | storeValue(cUnit, rlDest, rlResult); |
| 1814 | return false; |
| 1815 | break; |
| 1816 | default: |
| 1817 | return true; |
| 1818 | } |
| 1819 | rlSrc = loadValue(cUnit, rlSrc, kCoreReg); |
| 1820 | rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true); |
| 1821 | // Avoid shifts by literal 0 - no support in Thumb. Change to copy |
| 1822 | if (shiftOp && (lit == 0)) { |
buzbee | 82488f5 | 2012-03-02 08:20:26 -0800 | [diff] [blame] | 1823 | opRegCopy(cUnit, rlResult.lowReg, rlSrc.lowReg); |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 1824 | } else { |
| 1825 | opRegRegImm(cUnit, op, rlResult.lowReg, rlSrc.lowReg, lit); |
| 1826 | } |
| 1827 | storeValue(cUnit, rlDest, rlResult); |
| 1828 | return false; |
| 1829 | } |
| 1830 | |
| 1831 | bool genArithOpLong(CompilationUnit* cUnit, MIR* mir, RegLocation rlDest, |
| 1832 | RegLocation rlSrc1, RegLocation rlSrc2) |
| 1833 | { |
| 1834 | RegLocation rlResult; |
| 1835 | OpKind firstOp = kOpBkpt; |
| 1836 | OpKind secondOp = kOpBkpt; |
| 1837 | bool callOut = false; |
| 1838 | bool checkZero = false; |
| 1839 | int funcOffset; |
| 1840 | int retReg = rRET0; |
| 1841 | |
| 1842 | switch (mir->dalvikInsn.opcode) { |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 1843 | case Instruction::NOT_LONG: |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 1844 | rlSrc2 = loadValueWide(cUnit, rlSrc2, kCoreReg); |
| 1845 | rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true); |
| 1846 | // Check for destructive overlap |
| 1847 | if (rlResult.lowReg == rlSrc2.highReg) { |
| 1848 | int tReg = oatAllocTemp(cUnit); |
buzbee | 82488f5 | 2012-03-02 08:20:26 -0800 | [diff] [blame] | 1849 | opRegCopy(cUnit, tReg, rlSrc2.highReg); |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 1850 | opRegReg(cUnit, kOpMvn, rlResult.lowReg, rlSrc2.lowReg); |
| 1851 | opRegReg(cUnit, kOpMvn, rlResult.highReg, tReg); |
| 1852 | oatFreeTemp(cUnit, tReg); |
| 1853 | } else { |
| 1854 | opRegReg(cUnit, kOpMvn, rlResult.lowReg, rlSrc2.lowReg); |
| 1855 | opRegReg(cUnit, kOpMvn, rlResult.highReg, rlSrc2.highReg); |
| 1856 | } |
| 1857 | storeValueWide(cUnit, rlDest, rlResult); |
| 1858 | return false; |
| 1859 | break; |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 1860 | case Instruction::ADD_LONG: |
| 1861 | case Instruction::ADD_LONG_2ADDR: |
buzbee | c5159d5 | 2012-03-03 11:48:39 -0800 | [diff] [blame] | 1862 | #if defined(TARGET_MIPS) |
| 1863 | return genAddLong(cUnit, mir, rlDest, rlSrc1, rlSrc2); |
| 1864 | #else |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 1865 | firstOp = kOpAdd; |
| 1866 | secondOp = kOpAdc; |
| 1867 | break; |
buzbee | c5159d5 | 2012-03-03 11:48:39 -0800 | [diff] [blame] | 1868 | #endif |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 1869 | case Instruction::SUB_LONG: |
| 1870 | case Instruction::SUB_LONG_2ADDR: |
buzbee | c5159d5 | 2012-03-03 11:48:39 -0800 | [diff] [blame] | 1871 | #if defined(TARGET_MIPS) |
| 1872 | return genSubLong(cUnit, mir, rlDest, rlSrc1, rlSrc2); |
| 1873 | #else |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 1874 | firstOp = kOpSub; |
| 1875 | secondOp = kOpSbc; |
| 1876 | break; |
buzbee | c5159d5 | 2012-03-03 11:48:39 -0800 | [diff] [blame] | 1877 | #endif |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 1878 | case Instruction::MUL_LONG: |
| 1879 | case Instruction::MUL_LONG_2ADDR: |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 1880 | callOut = true; |
| 1881 | retReg = rRET0; |
| 1882 | funcOffset = OFFSETOF_MEMBER(Thread, pLmul); |
| 1883 | break; |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 1884 | case Instruction::DIV_LONG: |
| 1885 | case Instruction::DIV_LONG_2ADDR: |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 1886 | callOut = true; |
| 1887 | checkZero = true; |
| 1888 | retReg = rRET0; |
| 1889 | funcOffset = OFFSETOF_MEMBER(Thread, pLdivmod); |
| 1890 | break; |
| 1891 | /* NOTE - result is in rARG2/rARG3 instead of rRET0/rRET1 */ |
| 1892 | // FIXME: is true, or could be made true, or other targets? |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 1893 | case Instruction::REM_LONG: |
| 1894 | case Instruction::REM_LONG_2ADDR: |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 1895 | callOut = true; |
| 1896 | checkZero = true; |
| 1897 | funcOffset = OFFSETOF_MEMBER(Thread, pLdivmod); |
| 1898 | retReg = rARG2; |
| 1899 | break; |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 1900 | case Instruction::AND_LONG_2ADDR: |
| 1901 | case Instruction::AND_LONG: |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 1902 | firstOp = kOpAnd; |
| 1903 | secondOp = kOpAnd; |
| 1904 | break; |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 1905 | case Instruction::OR_LONG: |
| 1906 | case Instruction::OR_LONG_2ADDR: |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 1907 | firstOp = kOpOr; |
| 1908 | secondOp = kOpOr; |
| 1909 | break; |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 1910 | case Instruction::XOR_LONG: |
| 1911 | case Instruction::XOR_LONG_2ADDR: |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 1912 | firstOp = kOpXor; |
| 1913 | secondOp = kOpXor; |
| 1914 | break; |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 1915 | case Instruction::NEG_LONG: { |
buzbee | c5159d5 | 2012-03-03 11:48:39 -0800 | [diff] [blame] | 1916 | return genNegLong(cUnit, mir, rlDest, rlSrc2); |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 1917 | } |
| 1918 | default: |
| 1919 | LOG(FATAL) << "Invalid long arith op"; |
| 1920 | } |
| 1921 | if (!callOut) { |
| 1922 | genLong3Addr(cUnit, mir, firstOp, secondOp, rlDest, rlSrc1, rlSrc2); |
| 1923 | } else { |
| 1924 | int rTgt; |
| 1925 | oatFlushAllRegs(cUnit); /* Send everything to home location */ |
| 1926 | if (checkZero) { |
Ian Rogers | b5d09b2 | 2012-03-06 22:14:17 -0800 | [diff] [blame] | 1927 | #if defined(TARGET_X86) |
| 1928 | UNIMPLEMENTED(FATAL); |
| 1929 | #else |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 1930 | loadValueDirectWideFixed(cUnit, rlSrc2, rARG2, rARG3); |
Ian Rogers | b5d09b2 | 2012-03-06 22:14:17 -0800 | [diff] [blame] | 1931 | #endif |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 1932 | rTgt = loadHelper(cUnit, funcOffset); |
| 1933 | loadValueDirectWideFixed(cUnit, rlSrc1, rARG0, rARG1); |
| 1934 | int tReg = oatAllocTemp(cUnit); |
| 1935 | #if defined(TARGET_ARM) |
| 1936 | newLIR4(cUnit, kThumb2OrrRRRs, tReg, rARG2, rARG3, 0); |
| 1937 | oatFreeTemp(cUnit, tReg); |
| 1938 | genCheck(cUnit, kCondEq, mir, kThrowDivZero); |
| 1939 | #else |
Ian Rogers | b5d09b2 | 2012-03-06 22:14:17 -0800 | [diff] [blame] | 1940 | #if defined(TARGET_X86) |
| 1941 | UNIMPLEMENTED(FATAL); |
| 1942 | #else |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 1943 | opRegRegReg(cUnit, kOpOr, tReg, rARG2, rARG3); |
Ian Rogers | b5d09b2 | 2012-03-06 22:14:17 -0800 | [diff] [blame] | 1944 | #endif |
buzbee | 5de3494 | 2012-03-01 14:51:57 -0800 | [diff] [blame] | 1945 | genImmedCheck(cUnit, kCondEq, tReg, 0, mir, kThrowDivZero); |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 1946 | oatFreeTemp(cUnit, tReg); |
| 1947 | #endif |
| 1948 | } else { |
| 1949 | rTgt = loadHelper(cUnit, funcOffset); |
| 1950 | loadValueDirectWideFixed(cUnit, rlSrc1, rARG0, rARG1); |
Ian Rogers | b5d09b2 | 2012-03-06 22:14:17 -0800 | [diff] [blame] | 1951 | #if defined(TARGET_X86) |
| 1952 | UNIMPLEMENTED(FATAL); |
| 1953 | #else |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 1954 | loadValueDirectWideFixed(cUnit, rlSrc2, rARG2, rARG3); |
Ian Rogers | b5d09b2 | 2012-03-06 22:14:17 -0800 | [diff] [blame] | 1955 | #endif |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 1956 | } |
| 1957 | callRuntimeHelper(cUnit, rTgt); |
| 1958 | // Adjust return regs in to handle case of rem returning rARG2/rARG3 |
| 1959 | if (retReg == rRET0) |
| 1960 | rlResult = oatGetReturnWide(cUnit); |
| 1961 | else |
| 1962 | rlResult = oatGetReturnWideAlt(cUnit); |
| 1963 | storeValueWide(cUnit, rlDest, rlResult); |
| 1964 | } |
| 1965 | return false; |
| 1966 | } |
| 1967 | |
| 1968 | bool genConversionCall(CompilationUnit* cUnit, MIR* mir, int funcOffset, |
| 1969 | int srcSize, int tgtSize) |
| 1970 | { |
| 1971 | /* |
| 1972 | * Don't optimize the register usage since it calls out to support |
| 1973 | * functions |
| 1974 | */ |
| 1975 | RegLocation rlSrc; |
| 1976 | RegLocation rlDest; |
| 1977 | oatFlushAllRegs(cUnit); /* Send everything to home location */ |
| 1978 | int rTgt = loadHelper(cUnit, funcOffset); |
| 1979 | if (srcSize == 1) { |
| 1980 | rlSrc = oatGetSrc(cUnit, mir, 0); |
| 1981 | loadValueDirectFixed(cUnit, rlSrc, rARG0); |
| 1982 | } else { |
| 1983 | rlSrc = oatGetSrcWide(cUnit, mir, 0, 1); |
| 1984 | loadValueDirectWideFixed(cUnit, rlSrc, rARG0, rARG1); |
| 1985 | } |
| 1986 | callRuntimeHelper(cUnit, rTgt); |
| 1987 | if (tgtSize == 1) { |
| 1988 | RegLocation rlResult; |
| 1989 | rlDest = oatGetDest(cUnit, mir, 0); |
| 1990 | rlResult = oatGetReturn(cUnit); |
| 1991 | storeValue(cUnit, rlDest, rlResult); |
| 1992 | } else { |
| 1993 | RegLocation rlResult; |
| 1994 | rlDest = oatGetDestWide(cUnit, mir, 0, 1); |
| 1995 | rlResult = oatGetReturnWide(cUnit); |
| 1996 | storeValueWide(cUnit, rlDest, rlResult); |
| 1997 | } |
| 1998 | return false; |
| 1999 | } |
| 2000 | |
| 2001 | void genNegFloat(CompilationUnit* cUnit, RegLocation rlDest, RegLocation rlSrc); |
| 2002 | bool genArithOpFloatPortable(CompilationUnit* cUnit, MIR* mir, |
| 2003 | RegLocation rlDest, RegLocation rlSrc1, |
| 2004 | RegLocation rlSrc2) |
| 2005 | { |
| 2006 | RegLocation rlResult; |
| 2007 | int funcOffset; |
| 2008 | |
| 2009 | switch (mir->dalvikInsn.opcode) { |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 2010 | case Instruction::ADD_FLOAT_2ADDR: |
| 2011 | case Instruction::ADD_FLOAT: |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 2012 | funcOffset = OFFSETOF_MEMBER(Thread, pFadd); |
| 2013 | break; |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 2014 | case Instruction::SUB_FLOAT_2ADDR: |
| 2015 | case Instruction::SUB_FLOAT: |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 2016 | funcOffset = OFFSETOF_MEMBER(Thread, pFsub); |
| 2017 | break; |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 2018 | case Instruction::DIV_FLOAT_2ADDR: |
| 2019 | case Instruction::DIV_FLOAT: |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 2020 | funcOffset = OFFSETOF_MEMBER(Thread, pFdiv); |
| 2021 | break; |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 2022 | case Instruction::MUL_FLOAT_2ADDR: |
| 2023 | case Instruction::MUL_FLOAT: |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 2024 | funcOffset = OFFSETOF_MEMBER(Thread, pFmul); |
| 2025 | break; |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 2026 | case Instruction::REM_FLOAT_2ADDR: |
| 2027 | case Instruction::REM_FLOAT: |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 2028 | funcOffset = OFFSETOF_MEMBER(Thread, pFmodf); |
| 2029 | break; |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 2030 | case Instruction::NEG_FLOAT: { |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 2031 | genNegFloat(cUnit, rlDest, rlSrc1); |
| 2032 | return false; |
| 2033 | } |
| 2034 | default: |
| 2035 | return true; |
| 2036 | } |
| 2037 | oatFlushAllRegs(cUnit); /* Send everything to home location */ |
| 2038 | int rTgt = loadHelper(cUnit, funcOffset); |
| 2039 | loadValueDirectFixed(cUnit, rlSrc1, rARG0); |
| 2040 | loadValueDirectFixed(cUnit, rlSrc2, rARG1); |
| 2041 | callRuntimeHelper(cUnit, rTgt); |
| 2042 | rlResult = oatGetReturn(cUnit); |
| 2043 | storeValue(cUnit, rlDest, rlResult); |
| 2044 | return false; |
| 2045 | } |
| 2046 | |
| 2047 | void genNegDouble(CompilationUnit* cUnit, RegLocation rlDst, RegLocation rlSrc); |
| 2048 | bool genArithOpDoublePortable(CompilationUnit* cUnit, MIR* mir, |
| 2049 | RegLocation rlDest, RegLocation rlSrc1, |
| 2050 | RegLocation rlSrc2) |
| 2051 | { |
| 2052 | RegLocation rlResult; |
| 2053 | int funcOffset; |
| 2054 | |
| 2055 | switch (mir->dalvikInsn.opcode) { |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 2056 | case Instruction::ADD_DOUBLE_2ADDR: |
| 2057 | case Instruction::ADD_DOUBLE: |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 2058 | funcOffset = OFFSETOF_MEMBER(Thread, pDadd); |
| 2059 | break; |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 2060 | case Instruction::SUB_DOUBLE_2ADDR: |
| 2061 | case Instruction::SUB_DOUBLE: |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 2062 | funcOffset = OFFSETOF_MEMBER(Thread, pDsub); |
| 2063 | break; |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 2064 | case Instruction::DIV_DOUBLE_2ADDR: |
| 2065 | case Instruction::DIV_DOUBLE: |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 2066 | funcOffset = OFFSETOF_MEMBER(Thread, pDdiv); |
| 2067 | break; |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 2068 | case Instruction::MUL_DOUBLE_2ADDR: |
| 2069 | case Instruction::MUL_DOUBLE: |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 2070 | funcOffset = OFFSETOF_MEMBER(Thread, pDmul); |
| 2071 | break; |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 2072 | case Instruction::REM_DOUBLE_2ADDR: |
| 2073 | case Instruction::REM_DOUBLE: |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 2074 | funcOffset = OFFSETOF_MEMBER(Thread, pFmod); |
| 2075 | break; |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 2076 | case Instruction::NEG_DOUBLE: { |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 2077 | genNegDouble(cUnit, rlDest, rlSrc1); |
| 2078 | return false; |
| 2079 | } |
| 2080 | default: |
| 2081 | return true; |
| 2082 | } |
| 2083 | oatFlushAllRegs(cUnit); /* Send everything to home location */ |
| 2084 | int rTgt = loadHelper(cUnit, funcOffset); |
| 2085 | loadValueDirectWideFixed(cUnit, rlSrc1, rARG0, rARG1); |
Ian Rogers | b5d09b2 | 2012-03-06 22:14:17 -0800 | [diff] [blame] | 2086 | #if defined(TARGET_X86) |
| 2087 | UNIMPLEMENTED(FATAL); |
| 2088 | #else |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 2089 | loadValueDirectWideFixed(cUnit, rlSrc2, rARG2, rARG3); |
Ian Rogers | b5d09b2 | 2012-03-06 22:14:17 -0800 | [diff] [blame] | 2090 | #endif |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 2091 | callRuntimeHelper(cUnit, rTgt); |
| 2092 | rlResult = oatGetReturnWide(cUnit); |
| 2093 | storeValueWide(cUnit, rlDest, rlResult); |
| 2094 | return false; |
| 2095 | } |
| 2096 | |
| 2097 | bool genConversionPortable(CompilationUnit* cUnit, MIR* mir) |
| 2098 | { |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 2099 | Instruction::Code opcode = mir->dalvikInsn.opcode; |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 2100 | |
| 2101 | switch (opcode) { |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 2102 | case Instruction::INT_TO_FLOAT: |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 2103 | return genConversionCall(cUnit, mir, OFFSETOF_MEMBER(Thread, pI2f), |
| 2104 | 1, 1); |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 2105 | case Instruction::FLOAT_TO_INT: |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 2106 | return genConversionCall(cUnit, mir, OFFSETOF_MEMBER(Thread, pF2iz), |
| 2107 | 1, 1); |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 2108 | case Instruction::DOUBLE_TO_FLOAT: |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 2109 | return genConversionCall(cUnit, mir, OFFSETOF_MEMBER(Thread, pD2f), |
| 2110 | 2, 1); |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 2111 | case Instruction::FLOAT_TO_DOUBLE: |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 2112 | return genConversionCall(cUnit, mir, OFFSETOF_MEMBER(Thread, pF2d), |
| 2113 | 1, 2); |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 2114 | case Instruction::INT_TO_DOUBLE: |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 2115 | return genConversionCall(cUnit, mir, OFFSETOF_MEMBER(Thread, pI2d), |
| 2116 | 1, 2); |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 2117 | case Instruction::DOUBLE_TO_INT: |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 2118 | return genConversionCall(cUnit, mir, OFFSETOF_MEMBER(Thread, pD2iz), |
| 2119 | 2, 1); |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 2120 | case Instruction::FLOAT_TO_LONG: |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 2121 | return genConversionCall(cUnit, mir, OFFSETOF_MEMBER(Thread, |
| 2122 | pF2l), 1, 2); |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 2123 | case Instruction::LONG_TO_FLOAT: |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 2124 | return genConversionCall(cUnit, mir, OFFSETOF_MEMBER(Thread, pL2f), |
| 2125 | 2, 1); |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 2126 | case Instruction::DOUBLE_TO_LONG: |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 2127 | return genConversionCall(cUnit, mir, OFFSETOF_MEMBER(Thread, |
| 2128 | pD2l), 2, 2); |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 2129 | case Instruction::LONG_TO_DOUBLE: |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 2130 | return genConversionCall(cUnit, mir, OFFSETOF_MEMBER(Thread, pL2d), |
| 2131 | 2, 2); |
| 2132 | default: |
| 2133 | return true; |
| 2134 | } |
| 2135 | return false; |
| 2136 | } |
| 2137 | |
| 2138 | /* |
| 2139 | * Generate callout to updateDebugger. Note that we're overloading |
| 2140 | * the use of rSUSPEND here. When the debugger is active, this |
| 2141 | * register holds the address of the update function. So, if it's |
| 2142 | * non-null, we call out to it. |
| 2143 | * |
| 2144 | * Note also that rRET0 and rRET1 must be preserved across this |
| 2145 | * code. This must be handled by the stub. |
| 2146 | */ |
| 2147 | void genDebuggerUpdate(CompilationUnit* cUnit, int32_t offset) |
| 2148 | { |
| 2149 | // Following DCHECK verifies that dPC is in range of single load immediate |
| 2150 | DCHECK((offset == DEBUGGER_METHOD_ENTRY) || |
| 2151 | (offset == DEBUGGER_METHOD_EXIT) || ((offset & 0xffff) == offset)); |
| 2152 | oatClobberCalleeSave(cUnit); |
| 2153 | #if defined(TARGET_ARM) |
| 2154 | opRegImm(cUnit, kOpCmp, rSUSPEND, 0); |
buzbee | 82488f5 | 2012-03-02 08:20:26 -0800 | [diff] [blame] | 2155 | opIT(cUnit, kArmCondNe, "T"); |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 2156 | loadConstant(cUnit, rARG2, offset); // arg2 <- Entry code |
| 2157 | opReg(cUnit, kOpBlx, rSUSPEND); |
Ian Rogers | b5d09b2 | 2012-03-06 22:14:17 -0800 | [diff] [blame] | 2158 | #elif defined(TARGET_X86) |
| 2159 | UNIMPLEMENTED(FATAL); |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 2160 | #else |
buzbee | 82488f5 | 2012-03-02 08:20:26 -0800 | [diff] [blame] | 2161 | LIR* branch = opCmpImmBranch(cUnit, kCondEq, rSUSPEND, 0, NULL); |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 2162 | loadConstant(cUnit, rARG2, offset); |
| 2163 | opReg(cUnit, kOpBlx, rSUSPEND); |
| 2164 | LIR* target = newLIR0(cUnit, kPseudoTargetLabel); |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 2165 | branch->target = (LIR*)target; |
| 2166 | #endif |
| 2167 | oatFreeTemp(cUnit, rARG2); |
| 2168 | } |
| 2169 | |
| 2170 | /* Check if we need to check for pending suspend request */ |
| 2171 | void genSuspendTest(CompilationUnit* cUnit, MIR* mir) |
| 2172 | { |
| 2173 | if (NO_SUSPEND || (mir->optimizationFlags & MIR_IGNORE_SUSPEND_CHECK)) { |
| 2174 | return; |
| 2175 | } |
| 2176 | oatFlushAllRegs(cUnit); |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 2177 | if (cUnit->genDebugger) { |
| 2178 | // If generating code for the debugger, always check for suspension |
buzbee | 86a4bce | 2012-03-06 18:15:00 -0800 | [diff] [blame] | 2179 | int rTgt = loadHelper(cUnit, OFFSETOF_MEMBER(Thread, |
| 2180 | pTestSuspendFromCode)); |
| 2181 | opReg(cUnit, kOpBlx, rTgt); |
| 2182 | // Refresh rSUSPEND |
Ian Rogers | b5d09b2 | 2012-03-06 22:14:17 -0800 | [diff] [blame] | 2183 | #if defined(TARGET_X86) |
| 2184 | UNIMPLEMENTED(FATAL); |
| 2185 | #else |
| 2186 | |
buzbee | 86a4bce | 2012-03-06 18:15:00 -0800 | [diff] [blame] | 2187 | loadWordDisp(cUnit, rSELF, |
| 2188 | OFFSETOF_MEMBER(Thread, pUpdateDebuggerFromCode), |
| 2189 | rSUSPEND); |
Ian Rogers | b5d09b2 | 2012-03-06 22:14:17 -0800 | [diff] [blame] | 2190 | #endif |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 2191 | } else { |
Ian Rogers | b5d09b2 | 2012-03-06 22:14:17 -0800 | [diff] [blame] | 2192 | LIR* branch = NULL; |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 2193 | #if defined(TARGET_ARM) |
| 2194 | // In non-debug case, only check periodically |
| 2195 | newLIR2(cUnit, kThumbSubRI8, rSUSPEND, 1); |
buzbee | 82488f5 | 2012-03-02 08:20:26 -0800 | [diff] [blame] | 2196 | branch = opCondBranch(cUnit, kCondEq, NULL); |
Ian Rogers | b5d09b2 | 2012-03-06 22:14:17 -0800 | [diff] [blame] | 2197 | #elif defined(TARGET_X86) |
| 2198 | newLIR2(cUnit, kX86Cmp32TI, Thread::SuspendCountOffset().Int32Value(), 0); |
| 2199 | branch = opCondBranch(cUnit, kCondNe, NULL); |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 2200 | #else |
| 2201 | opRegImm(cUnit, kOpSub, rSUSPEND, 1); |
buzbee | 82488f5 | 2012-03-02 08:20:26 -0800 | [diff] [blame] | 2202 | branch = opCmpImmBranch(cUnit, kCondEq, rSUSPEND, 0, NULL); |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 2203 | #endif |
buzbee | 86a4bce | 2012-03-06 18:15:00 -0800 | [diff] [blame] | 2204 | LIR* retLab = newLIR0(cUnit, kPseudoTargetLabel); |
| 2205 | LIR* target = rawLIR(cUnit, cUnit->currentDalvikOffset, |
| 2206 | kPseudoSuspendTarget, (intptr_t)retLab, mir->offset); |
| 2207 | branch->target = (LIR*)target; |
| 2208 | oatInsertGrowableList(cUnit, &cUnit->suspendLaunchpads, (intptr_t)target); |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 2209 | } |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 2210 | } |
| 2211 | |
| 2212 | } // namespace art |