| Bill Buzbee | 9bc3df3 | 2009-07-30 10:52:29 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2009 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | /* |
| 18 | * This file contains codegen for the Thumb ISA and is intended to be |
| Bill Buzbee | 270c1d6 | 2009-08-13 16:58:07 -0700 | [diff] [blame] | 19 | * includes by: |
| Bill Buzbee | 9bc3df3 | 2009-07-30 10:52:29 -0700 | [diff] [blame] | 20 | * |
| 21 | * Codegen-$(TARGET_ARCH_VARIANT).c |
| 22 | * |
| 23 | */ |
| 24 | |
| 25 | #include "Codegen.h" |
| Bill Buzbee | 270c1d6 | 2009-08-13 16:58:07 -0700 | [diff] [blame] | 26 | /* Forward decls */ |
| 27 | static ArmLIR *genNullCheck(CompilationUnit *cUnit, int vReg, int mReg, |
| 28 | int dOffset, ArmLIR *pcrLabel); |
| 29 | static ArmLIR *loadValueAddress(CompilationUnit *cUnit, int vSrc, int rDest); |
| 30 | static ArmLIR *loadValue(CompilationUnit *cUnit, int vSrc, int rDest); |
| 31 | static ArmLIR *loadWordDisp(CompilationUnit *cUnit, int rBase, |
| 32 | int displacement, int rDest); |
| 33 | static ArmLIR *storeWordDisp(CompilationUnit *cUnit, int rBase, |
| 34 | int displacement, int rSrc, int rScratch); |
| 35 | static ArmLIR *storeValue(CompilationUnit *cUnit, int rSrc, int vDest, |
| 36 | int rScratch); |
| 37 | static ArmLIR *genConditionalBranch(CompilationUnit *cUnit, |
| 38 | ArmConditionCode cond, |
| 39 | ArmLIR *target); |
| 40 | static ArmLIR *genUnconditionalBranch(CompilationUnit *cUnit, ArmLIR *target); |
| 41 | static ArmLIR *loadValuePair(CompilationUnit *cUnit, int vSrc, int rDestLo, |
| 42 | int rDestHi); |
| 43 | static ArmLIR *storeValuePair(CompilationUnit *cUnit, int rSrcLo, int rSrcHi, |
| 44 | int vDest, int rScratch); |
| 45 | static ArmLIR *genBoundsCheck(CompilationUnit *cUnit, int rIndex, |
| 46 | int rBound, int dOffset, ArmLIR *pcrLabel); |
| 47 | static ArmLIR *genRegCopy(CompilationUnit *cUnit, int rDest, int rSrc); |
| Bill Buzbee | a4a7f07 | 2009-08-27 13:58:09 -0700 | [diff] [blame] | 48 | static int inlinedTarget(MIR *mir); |
| Bill Buzbee | 270c1d6 | 2009-08-13 16:58:07 -0700 | [diff] [blame] | 49 | |
| Bill Buzbee | 9bc3df3 | 2009-07-30 10:52:29 -0700 | [diff] [blame] | 50 | |
| 51 | /* Routines which must be supplied here */ |
| Bill Buzbee | 270c1d6 | 2009-08-13 16:58:07 -0700 | [diff] [blame] | 52 | static ArmLIR *loadConstant(CompilationUnit *cUnit, int rDest, int value); |
| 53 | static ArmLIR *genExportPC(CompilationUnit *cUnit, MIR *mir, int rDPC, |
| 54 | int rAddr); |
| 55 | static ArmLIR *loadBaseDisp(CompilationUnit *cUnit, MIR *mir, int rBase, |
| 56 | int displacement, int rDest, OpSize size, |
| 57 | bool nullCheck, int vReg); |
| 58 | static ArmLIR *storeBaseDisp(CompilationUnit *cUnit, int rBase, |
| 59 | int displacement, int rSrc, OpSize size, |
| 60 | int rScratch); |
| Bill Buzbee | 9bc3df3 | 2009-07-30 10:52:29 -0700 | [diff] [blame] | 61 | static inline ArmLIR *genRegImmCheck(CompilationUnit *cUnit, |
| Bill Buzbee | 270c1d6 | 2009-08-13 16:58:07 -0700 | [diff] [blame] | 62 | ArmConditionCode cond, int reg, |
| 63 | int checkValue, int dOffset, |
| 64 | ArmLIR *pcrLabel); |
| Ben Cheng | 0fd31e4 | 2009-09-03 14:40:16 -0700 | [diff] [blame] | 65 | static inline ArmLIR *genRegRegCheck(CompilationUnit *cUnit, |
| 66 | ArmConditionCode cond, |
| 67 | int reg1, int reg2, int dOffset, |
| 68 | ArmLIR *pcrLabel); |
| Bill Buzbee | 7ea0f64 | 2009-08-10 17:06:51 -0700 | [diff] [blame] | 69 | ArmLIR* dvmCompilerRegCopy(CompilationUnit *cUnit, int rDest, int rSrc); |
| Bill Buzbee | 270c1d6 | 2009-08-13 16:58:07 -0700 | [diff] [blame] | 70 | static ArmLIR *loadMultiple(CompilationUnit *cUnit, int rBase, int rMask); |
| 71 | static ArmLIR *storeMultiple(CompilationUnit *cUnit, int rBase, int rMask); |
| Bill Buzbee | 9bc3df3 | 2009-07-30 10:52:29 -0700 | [diff] [blame] | 72 | |
| Bill Buzbee | 270c1d6 | 2009-08-13 16:58:07 -0700 | [diff] [blame] | 73 | static ArmLIR *opNone(CompilationUnit *cUnit, OpKind op); |
| 74 | static ArmLIR *opImm(CompilationUnit *cUnit, OpKind op, int value); |
| Ben Cheng | 4f48917 | 2009-09-27 17:08:35 -0700 | [diff] [blame^] | 75 | static ArmLIR *opCondBranch(CompilationUnit *cUnit, ArmConditionCode cc); |
| Bill Buzbee | 270c1d6 | 2009-08-13 16:58:07 -0700 | [diff] [blame] | 76 | static ArmLIR *opReg(CompilationUnit *cUnit, OpKind op, int rDestSrc); |
| 77 | static ArmLIR *opRegReg(CompilationUnit *cUnit, OpKind op, int rDestSrc1, |
| 78 | int rSrc2); |
| 79 | static ArmLIR *opRegImm(CompilationUnit *cUnit, OpKind op, int rDestSrc1, |
| 80 | int value, int rScratch); |
| 81 | static ArmLIR *opRegRegImm(CompilationUnit *cUnit, OpKind op, int rDest, |
| 82 | int rSrc1, int value, int rScratch); |
| 83 | static ArmLIR *opRegRegReg(CompilationUnit *cUnit, OpKind op, int rDest, |
| 84 | int rSrc1, int rSrc2); |
| 85 | static ArmLIR *loadBaseIndexed(CompilationUnit *cUnit, int rBase, |
| 86 | int rIndex, int rDest, int scale, OpSize size); |
| Bill Buzbee | a4a7f07 | 2009-08-27 13:58:09 -0700 | [diff] [blame] | 87 | static void genCmpLong(CompilationUnit *cUnit, MIR *mir, int vDest, int vSrc1, |
| 88 | int vSrc2); |
| Bill Buzbee | 270c1d6 | 2009-08-13 16:58:07 -0700 | [diff] [blame] | 89 | |
| 90 | static bool genInlinedStringLength(CompilationUnit *cUnit, MIR *mir); |
| 91 | static bool genInlinedStringCharAt(CompilationUnit *cUnit, MIR *mir); |
| 92 | static bool genInlinedAbsInt(CompilationUnit *cUnit, MIR *mir); |
| 93 | static bool genInlinedAbsFloat(CompilationUnit *cUnit, MIR *mir); |
| 94 | static bool genInlinedAbsDouble(CompilationUnit *cUnit, MIR *mir); |
| 95 | static bool genInlinedMinMaxInt(CompilationUnit *cUnit, MIR *mir, bool isMin); |
| 96 | static bool genInlinedAbsLong(CompilationUnit *cUnit, MIR *mir); |
| Bill Buzbee | 9bc3df3 | 2009-07-30 10:52:29 -0700 | [diff] [blame] | 97 | |
| 98 | /* |
| 99 | * Support for register allocation |
| 100 | */ |
| 101 | |
| Bill Buzbee | 9bc3df3 | 2009-07-30 10:52:29 -0700 | [diff] [blame] | 102 | /* get the next register in r0..r3 in a round-robin fashion */ |
| 103 | #define NEXT_REG(reg) ((reg + 1) & 3) |
| 104 | /* |
| 105 | * The following are utility routines to help maintain the RegisterScoreboard |
| 106 | * state to facilitate register renaming. |
| 107 | */ |
| 108 | |
| 109 | /* Reset the tracker to unknown state */ |
| 110 | static inline void resetRegisterScoreboard(CompilationUnit *cUnit) |
| 111 | { |
| 112 | RegisterScoreboard *registerScoreboard = &cUnit->registerScoreboard; |
| 113 | |
| 114 | dvmClearAllBits(registerScoreboard->nullCheckedRegs); |
| 115 | registerScoreboard->liveDalvikReg = vNone; |
| 116 | registerScoreboard->nativeReg = vNone; |
| 117 | registerScoreboard->nativeRegHi = vNone; |
| 118 | } |
| 119 | |
| 120 | /* Kill the corresponding bit in the null-checked register list */ |
| 121 | static inline void killNullCheckedRegister(CompilationUnit *cUnit, int vReg) |
| 122 | { |
| 123 | dvmClearBit(cUnit->registerScoreboard.nullCheckedRegs, vReg); |
| 124 | } |
| 125 | |
| 126 | /* The Dalvik register pair held in native registers have changed */ |
| 127 | static inline void updateLiveRegisterPair(CompilationUnit *cUnit, |
| 128 | int vReg, int mRegLo, int mRegHi) |
| 129 | { |
| 130 | cUnit->registerScoreboard.liveDalvikReg = vReg; |
| 131 | cUnit->registerScoreboard.nativeReg = mRegLo; |
| 132 | cUnit->registerScoreboard.nativeRegHi = mRegHi; |
| 133 | cUnit->registerScoreboard.isWide = true; |
| 134 | } |
| 135 | |
| 136 | /* The Dalvik register held in a native register has changed */ |
| 137 | static inline void updateLiveRegister(CompilationUnit *cUnit, |
| 138 | int vReg, int mReg) |
| 139 | { |
| 140 | cUnit->registerScoreboard.liveDalvikReg = vReg; |
| 141 | cUnit->registerScoreboard.nativeReg = mReg; |
| 142 | cUnit->registerScoreboard.isWide = false; |
| 143 | } |
| 144 | |
| 145 | /* |
| 146 | * Given a Dalvik register id vSrc, use a very simple algorithm to increase |
| 147 | * the lifetime of cached Dalvik value in a native register. |
| 148 | */ |
| 149 | static inline int selectFirstRegister(CompilationUnit *cUnit, int vSrc, |
| 150 | bool isWide) |
| 151 | { |
| 152 | RegisterScoreboard *registerScoreboard = &cUnit->registerScoreboard; |
| 153 | |
| 154 | /* No live value - suggest to use r0 */ |
| 155 | if (registerScoreboard->liveDalvikReg == vNone) |
| 156 | return r0; |
| 157 | |
| 158 | /* Reuse the previously used native reg */ |
| 159 | if (registerScoreboard->liveDalvikReg == vSrc) { |
| 160 | if (isWide != true) { |
| 161 | return registerScoreboard->nativeReg; |
| 162 | } else { |
| 163 | /* Return either r0 or r2 */ |
| 164 | return (registerScoreboard->nativeReg + 1) & 2; |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | /* No reuse - choose the next one among r0..r3 in the round-robin fashion */ |
| 169 | if (isWide) { |
| 170 | return (registerScoreboard->nativeReg + 2) & 2; |
| 171 | } else { |
| 172 | return (registerScoreboard->nativeReg + 1) & 3; |
| 173 | } |
| 174 | |
| 175 | } |
| 176 | |
| Bill Buzbee | 7ea0f64 | 2009-08-10 17:06:51 -0700 | [diff] [blame] | 177 | ArmLIR* dvmCompilerRegCopy(CompilationUnit *cUnit, int rDest, int rSrc) |
| 178 | { |
| Bill Buzbee | 270c1d6 | 2009-08-13 16:58:07 -0700 | [diff] [blame] | 179 | ArmLIR* res; |
| 180 | ArmOpCode opCode; |
| 181 | res = dvmCompilerNew(sizeof(ArmLIR), true); |
| 182 | if (LOWREG(rDest) && LOWREG(rSrc)) |
| 183 | opCode = THUMB_MOV_RR; |
| 184 | else if (!LOWREG(rDest) && !LOWREG(rSrc)) |
| 185 | opCode = THUMB_MOV_RR_H2H; |
| 186 | else if (LOWREG(rDest)) |
| 187 | opCode = THUMB_MOV_RR_H2L; |
| 188 | else |
| 189 | opCode = THUMB_MOV_RR_L2H; |
| Bill Buzbee | 270c1d6 | 2009-08-13 16:58:07 -0700 | [diff] [blame] | 190 | |
| Ben Cheng | dcf3e5d | 2009-09-11 13:42:05 -0700 | [diff] [blame] | 191 | res->operands[0] = rDest; |
| 192 | res->operands[1] = rSrc; |
| Bill Buzbee | 270c1d6 | 2009-08-13 16:58:07 -0700 | [diff] [blame] | 193 | res->opCode = opCode; |
| Ben Cheng | dcf3e5d | 2009-09-11 13:42:05 -0700 | [diff] [blame] | 194 | setupResourceMasks(res); |
| Bill Buzbee | 7ea0f64 | 2009-08-10 17:06:51 -0700 | [diff] [blame] | 195 | if (rDest == rSrc) { |
| 196 | res->isNop = true; |
| 197 | } |
| 198 | return res; |
| 199 | } |
| 200 | |
| Bill Buzbee | 9bc3df3 | 2009-07-30 10:52:29 -0700 | [diff] [blame] | 201 | /* |
| 202 | * Load a immediate using a shortcut if possible; otherwise |
| 203 | * grab from the per-translation literal pool |
| 204 | */ |
| Bill Buzbee | 270c1d6 | 2009-08-13 16:58:07 -0700 | [diff] [blame] | 205 | static ArmLIR *loadConstant(CompilationUnit *cUnit, int rDest, int value) |
| Bill Buzbee | 9bc3df3 | 2009-07-30 10:52:29 -0700 | [diff] [blame] | 206 | { |
| Bill Buzbee | 270c1d6 | 2009-08-13 16:58:07 -0700 | [diff] [blame] | 207 | ArmLIR *res; |
| Bill Buzbee | 9bc3df3 | 2009-07-30 10:52:29 -0700 | [diff] [blame] | 208 | /* See if the value can be constructed cheaply */ |
| 209 | if ((value >= 0) && (value <= 255)) { |
| Bill Buzbee | 270c1d6 | 2009-08-13 16:58:07 -0700 | [diff] [blame] | 210 | return newLIR2(cUnit, THUMB_MOV_IMM, rDest, value); |
| Bill Buzbee | 9bc3df3 | 2009-07-30 10:52:29 -0700 | [diff] [blame] | 211 | } else if ((value & 0xFFFFFF00) == 0xFFFFFF00) { |
| Bill Buzbee | 270c1d6 | 2009-08-13 16:58:07 -0700 | [diff] [blame] | 212 | res = newLIR2(cUnit, THUMB_MOV_IMM, rDest, ~value); |
| Bill Buzbee | 9bc3df3 | 2009-07-30 10:52:29 -0700 | [diff] [blame] | 213 | newLIR2(cUnit, THUMB_MVN, rDest, rDest); |
| Bill Buzbee | 270c1d6 | 2009-08-13 16:58:07 -0700 | [diff] [blame] | 214 | return res; |
| Bill Buzbee | 9bc3df3 | 2009-07-30 10:52:29 -0700 | [diff] [blame] | 215 | } |
| 216 | /* No shortcut - go ahead and use literal pool */ |
| 217 | ArmLIR *dataTarget = scanLiteralPool(cUnit, value, 255); |
| 218 | if (dataTarget == NULL) { |
| 219 | dataTarget = addWordData(cUnit, value, false); |
| 220 | } |
| 221 | ArmLIR *loadPcRel = dvmCompilerNew(sizeof(ArmLIR), true); |
| 222 | loadPcRel->opCode = THUMB_LDR_PC_REL; |
| 223 | loadPcRel->generic.target = (LIR *) dataTarget; |
| 224 | loadPcRel->operands[0] = rDest; |
| Ben Cheng | dcf3e5d | 2009-09-11 13:42:05 -0700 | [diff] [blame] | 225 | setupResourceMasks(loadPcRel); |
| Bill Buzbee | 270c1d6 | 2009-08-13 16:58:07 -0700 | [diff] [blame] | 226 | res = loadPcRel; |
| Bill Buzbee | 9bc3df3 | 2009-07-30 10:52:29 -0700 | [diff] [blame] | 227 | dvmCompilerAppendLIR(cUnit, (LIR *) loadPcRel); |
| 228 | |
| 229 | /* |
| 230 | * To save space in the constant pool, we use the ADD_RRI8 instruction to |
| 231 | * add up to 255 to an existing constant value. |
| 232 | */ |
| 233 | if (dataTarget->operands[0] != value) { |
| 234 | newLIR2(cUnit, THUMB_ADD_RI8, rDest, value - dataTarget->operands[0]); |
| 235 | } |
| Bill Buzbee | 270c1d6 | 2009-08-13 16:58:07 -0700 | [diff] [blame] | 236 | return res; |
| Bill Buzbee | 9bc3df3 | 2009-07-30 10:52:29 -0700 | [diff] [blame] | 237 | } |
| 238 | |
| 239 | /* Export the Dalvik PC assicated with an instruction to the StackSave area */ |
| Bill Buzbee | 270c1d6 | 2009-08-13 16:58:07 -0700 | [diff] [blame] | 240 | static ArmLIR *genExportPC(CompilationUnit *cUnit, MIR *mir, int rDPC, |
| 241 | int rAddr) |
| Bill Buzbee | 9bc3df3 | 2009-07-30 10:52:29 -0700 | [diff] [blame] | 242 | { |
| Bill Buzbee | 270c1d6 | 2009-08-13 16:58:07 -0700 | [diff] [blame] | 243 | ArmLIR *res; |
| Bill Buzbee | 9bc3df3 | 2009-07-30 10:52:29 -0700 | [diff] [blame] | 244 | int offset = offsetof(StackSaveArea, xtra.currentPc); |
| Bill Buzbee | 270c1d6 | 2009-08-13 16:58:07 -0700 | [diff] [blame] | 245 | res = loadConstant(cUnit, rDPC, (int) (cUnit->method->insns + mir->offset)); |
| Bill Buzbee | 9bc3df3 | 2009-07-30 10:52:29 -0700 | [diff] [blame] | 246 | newLIR2(cUnit, THUMB_MOV_RR, rAddr, rFP); |
| 247 | newLIR2(cUnit, THUMB_SUB_RI8, rAddr, sizeof(StackSaveArea) - offset); |
| Bill Buzbee | 270c1d6 | 2009-08-13 16:58:07 -0700 | [diff] [blame] | 248 | storeWordDisp( cUnit, rAddr, 0, rDPC, -1); |
| 249 | return res; |
| Bill Buzbee | 9bc3df3 | 2009-07-30 10:52:29 -0700 | [diff] [blame] | 250 | } |
| 251 | |
| Bill Buzbee | 270c1d6 | 2009-08-13 16:58:07 -0700 | [diff] [blame] | 252 | /* Load value from base + scaled index. Note: index reg killed */ |
| 253 | static ArmLIR *loadBaseIndexed(CompilationUnit *cUnit, int rBase, |
| 254 | int rIndex, int rDest, int scale, OpSize size) |
| Bill Buzbee | 9bc3df3 | 2009-07-30 10:52:29 -0700 | [diff] [blame] | 255 | { |
| Bill Buzbee | 270c1d6 | 2009-08-13 16:58:07 -0700 | [diff] [blame] | 256 | ArmLIR *first = NULL; |
| 257 | ArmLIR *res; |
| 258 | ArmOpCode opCode = THUMB_BKPT; |
| 259 | if (scale) |
| 260 | first = opRegRegImm(cUnit, OP_LSL, rIndex, rIndex, scale, rNone); |
| 261 | switch (size) { |
| 262 | case WORD: |
| 263 | opCode = THUMB_LDR_RRR; |
| 264 | break; |
| 265 | case UNSIGNED_HALF: |
| 266 | opCode = THUMB_LDRH_RRR; |
| 267 | break; |
| 268 | case SIGNED_HALF: |
| 269 | opCode = THUMB_LDRSH_RRR; |
| 270 | break; |
| 271 | case UNSIGNED_BYTE: |
| 272 | opCode = THUMB_LDRB_RRR; |
| 273 | break; |
| 274 | case SIGNED_BYTE: |
| 275 | opCode = THUMB_LDRSB_RRR; |
| 276 | break; |
| 277 | default: |
| 278 | assert(0); |
| 279 | } |
| 280 | res = newLIR3(cUnit, opCode, rDest, rBase, rIndex); |
| 281 | return (first) ? first : res; |
| Bill Buzbee | 9bc3df3 | 2009-07-30 10:52:29 -0700 | [diff] [blame] | 282 | } |
| 283 | |
| Bill Buzbee | 270c1d6 | 2009-08-13 16:58:07 -0700 | [diff] [blame] | 284 | /* store value base base + scaled index. Note: index reg killed */ |
| 285 | static ArmLIR *storeBaseIndexed(CompilationUnit *cUnit, int rBase, |
| 286 | int rIndex, int rSrc, int scale, OpSize size) |
| Bill Buzbee | 9bc3df3 | 2009-07-30 10:52:29 -0700 | [diff] [blame] | 287 | { |
| Bill Buzbee | 270c1d6 | 2009-08-13 16:58:07 -0700 | [diff] [blame] | 288 | ArmLIR *first = NULL; |
| 289 | ArmLIR *res; |
| 290 | ArmOpCode opCode = THUMB_BKPT; |
| 291 | if (scale) |
| 292 | first = opRegRegImm(cUnit, OP_LSL, rIndex, rIndex, scale, rNone); |
| 293 | switch (size) { |
| 294 | case WORD: |
| 295 | opCode = THUMB_STR_RRR; |
| 296 | break; |
| 297 | case UNSIGNED_HALF: |
| 298 | case SIGNED_HALF: |
| 299 | opCode = THUMB_STRH_RRR; |
| 300 | break; |
| 301 | case UNSIGNED_BYTE: |
| 302 | case SIGNED_BYTE: |
| 303 | opCode = THUMB_STRB_RRR; |
| 304 | break; |
| 305 | default: |
| 306 | assert(0); |
| 307 | } |
| 308 | res = newLIR3(cUnit, opCode, rSrc, rBase, rIndex); |
| 309 | return (first) ? first : res; |
| Bill Buzbee | 9bc3df3 | 2009-07-30 10:52:29 -0700 | [diff] [blame] | 310 | } |
| 311 | |
| 312 | /* |
| Bill Buzbee | 270c1d6 | 2009-08-13 16:58:07 -0700 | [diff] [blame] | 313 | * Load value from base + displacement. Optionally perform null check |
| 314 | * on base (which must have an associated vReg and MIR). If not |
| 315 | * performing null check, incoming MIR can be null. Note: base and |
| 316 | * dest must not be the same if there is any chance that the long |
| 317 | * form must be used. |
| Bill Buzbee | 9bc3df3 | 2009-07-30 10:52:29 -0700 | [diff] [blame] | 318 | */ |
| Bill Buzbee | 270c1d6 | 2009-08-13 16:58:07 -0700 | [diff] [blame] | 319 | static ArmLIR *loadBaseDisp(CompilationUnit *cUnit, MIR *mir, int rBase, |
| 320 | int displacement, int rDest, OpSize size, |
| 321 | bool nullCheck, int vReg) |
| Bill Buzbee | 9bc3df3 | 2009-07-30 10:52:29 -0700 | [diff] [blame] | 322 | { |
| Bill Buzbee | 270c1d6 | 2009-08-13 16:58:07 -0700 | [diff] [blame] | 323 | ArmLIR *first = NULL; |
| Ben Cheng | d7d426a | 2009-09-22 11:23:36 -0700 | [diff] [blame] | 324 | ArmLIR *res, *load; |
| Bill Buzbee | 270c1d6 | 2009-08-13 16:58:07 -0700 | [diff] [blame] | 325 | ArmOpCode opCode = THUMB_BKPT; |
| 326 | bool shortForm = false; |
| 327 | int shortMax = 128; |
| Ben Cheng | d7d426a | 2009-09-22 11:23:36 -0700 | [diff] [blame] | 328 | int encodedDisp = displacement; |
| 329 | |
| Bill Buzbee | 270c1d6 | 2009-08-13 16:58:07 -0700 | [diff] [blame] | 330 | switch (size) { |
| 331 | case WORD: |
| 332 | if (LOWREG(rDest) && (rBase == rpc) && |
| 333 | (displacement <= 1020) && (displacement >= 0)) { |
| 334 | shortForm = true; |
| Ben Cheng | d7d426a | 2009-09-22 11:23:36 -0700 | [diff] [blame] | 335 | encodedDisp >>= 2; |
| Bill Buzbee | 270c1d6 | 2009-08-13 16:58:07 -0700 | [diff] [blame] | 336 | opCode = THUMB_LDR_PC_REL; |
| 337 | } else if (LOWREG(rDest) && (rBase == r13) && |
| 338 | (displacement <= 1020) && (displacement >= 0)) { |
| 339 | shortForm = true; |
| Ben Cheng | d7d426a | 2009-09-22 11:23:36 -0700 | [diff] [blame] | 340 | encodedDisp >>= 2; |
| Bill Buzbee | 270c1d6 | 2009-08-13 16:58:07 -0700 | [diff] [blame] | 341 | opCode = THUMB_LDR_SP_REL; |
| 342 | } else if (displacement < 128 && displacement >= 0) { |
| 343 | assert((displacement & 0x3) == 0); |
| 344 | shortForm = true; |
| Ben Cheng | d7d426a | 2009-09-22 11:23:36 -0700 | [diff] [blame] | 345 | encodedDisp >>= 2; |
| Bill Buzbee | 270c1d6 | 2009-08-13 16:58:07 -0700 | [diff] [blame] | 346 | opCode = THUMB_LDR_RRI5; |
| 347 | } else { |
| 348 | opCode = THUMB_LDR_RRR; |
| 349 | } |
| 350 | break; |
| 351 | case UNSIGNED_HALF: |
| 352 | if (displacement < 64 && displacement >= 0) { |
| 353 | assert((displacement & 0x1) == 0); |
| 354 | shortForm = true; |
| Ben Cheng | d7d426a | 2009-09-22 11:23:36 -0700 | [diff] [blame] | 355 | encodedDisp >>= 1; |
| Bill Buzbee | 270c1d6 | 2009-08-13 16:58:07 -0700 | [diff] [blame] | 356 | opCode = THUMB_LDRH_RRI5; |
| 357 | } else { |
| 358 | opCode = THUMB_LDRH_RRR; |
| 359 | } |
| 360 | break; |
| 361 | case SIGNED_HALF: |
| 362 | opCode = THUMB_LDRSH_RRR; |
| 363 | break; |
| 364 | case UNSIGNED_BYTE: |
| 365 | if (displacement < 32 && displacement >= 0) { |
| 366 | shortForm = true; |
| 367 | opCode = THUMB_LDRB_RRI5; |
| 368 | } else { |
| 369 | opCode = THUMB_LDRB_RRR; |
| 370 | } |
| 371 | break; |
| 372 | case SIGNED_BYTE: |
| 373 | opCode = THUMB_LDRSB_RRR; |
| 374 | break; |
| 375 | default: |
| 376 | assert(0); |
| Bill Buzbee | 9bc3df3 | 2009-07-30 10:52:29 -0700 | [diff] [blame] | 377 | } |
| Bill Buzbee | 270c1d6 | 2009-08-13 16:58:07 -0700 | [diff] [blame] | 378 | if (nullCheck) |
| 379 | first = genNullCheck(cUnit, vReg, rBase, mir->offset, NULL); |
| 380 | if (shortForm) { |
| Ben Cheng | d7d426a | 2009-09-22 11:23:36 -0700 | [diff] [blame] | 381 | load = res = newLIR3(cUnit, opCode, rDest, rBase, encodedDisp); |
| Bill Buzbee | 270c1d6 | 2009-08-13 16:58:07 -0700 | [diff] [blame] | 382 | } else { |
| 383 | assert(rBase != rDest); |
| Ben Cheng | d7d426a | 2009-09-22 11:23:36 -0700 | [diff] [blame] | 384 | res = loadConstant(cUnit, rDest, encodedDisp); |
| 385 | load = newLIR3(cUnit, opCode, rDest, rBase, rDest); |
| Bill Buzbee | 270c1d6 | 2009-08-13 16:58:07 -0700 | [diff] [blame] | 386 | } |
| Ben Cheng | d7d426a | 2009-09-22 11:23:36 -0700 | [diff] [blame] | 387 | |
| 388 | if (rBase == rFP) { |
| 389 | annotateDalvikRegAccess(load, displacement >> 2, true /* isLoad */); |
| 390 | } |
| 391 | |
| Bill Buzbee | 270c1d6 | 2009-08-13 16:58:07 -0700 | [diff] [blame] | 392 | return (first) ? first : res; |
| Bill Buzbee | 9bc3df3 | 2009-07-30 10:52:29 -0700 | [diff] [blame] | 393 | } |
| 394 | |
| Bill Buzbee | 270c1d6 | 2009-08-13 16:58:07 -0700 | [diff] [blame] | 395 | static ArmLIR *storeBaseDisp(CompilationUnit *cUnit, int rBase, |
| 396 | int displacement, int rSrc, OpSize size, |
| 397 | int rScratch) |
| Bill Buzbee | 9bc3df3 | 2009-07-30 10:52:29 -0700 | [diff] [blame] | 398 | { |
| Ben Cheng | d7d426a | 2009-09-22 11:23:36 -0700 | [diff] [blame] | 399 | ArmLIR *res, *store; |
| Bill Buzbee | 270c1d6 | 2009-08-13 16:58:07 -0700 | [diff] [blame] | 400 | ArmOpCode opCode = THUMB_BKPT; |
| 401 | bool shortForm = false; |
| 402 | int shortMax = 128; |
| Ben Cheng | d7d426a | 2009-09-22 11:23:36 -0700 | [diff] [blame] | 403 | int encodedDisp = displacement; |
| 404 | |
| Bill Buzbee | 270c1d6 | 2009-08-13 16:58:07 -0700 | [diff] [blame] | 405 | switch (size) { |
| 406 | case WORD: |
| 407 | if (displacement < 128 && displacement >= 0) { |
| 408 | assert((displacement & 0x3) == 0); |
| 409 | shortForm = true; |
| Ben Cheng | d7d426a | 2009-09-22 11:23:36 -0700 | [diff] [blame] | 410 | encodedDisp >>= 2; |
| Bill Buzbee | 270c1d6 | 2009-08-13 16:58:07 -0700 | [diff] [blame] | 411 | opCode = THUMB_STR_RRI5; |
| 412 | } else { |
| 413 | opCode = THUMB_STR_RRR; |
| 414 | } |
| 415 | break; |
| 416 | case UNSIGNED_HALF: |
| 417 | case SIGNED_HALF: |
| 418 | if (displacement < 64 && displacement >= 0) { |
| 419 | assert((displacement & 0x1) == 0); |
| 420 | shortForm = true; |
| Ben Cheng | d7d426a | 2009-09-22 11:23:36 -0700 | [diff] [blame] | 421 | encodedDisp >>= 1; |
| Bill Buzbee | 270c1d6 | 2009-08-13 16:58:07 -0700 | [diff] [blame] | 422 | opCode = THUMB_STRH_RRI5; |
| 423 | } else { |
| 424 | opCode = THUMB_STRH_RRR; |
| 425 | } |
| 426 | break; |
| 427 | case UNSIGNED_BYTE: |
| 428 | case SIGNED_BYTE: |
| 429 | if (displacement < 32 && displacement >= 0) { |
| 430 | shortForm = true; |
| 431 | opCode = THUMB_STRB_RRI5; |
| 432 | } else { |
| 433 | opCode = THUMB_STRB_RRR; |
| 434 | } |
| 435 | break; |
| 436 | default: |
| 437 | assert(0); |
| Bill Buzbee | 9bc3df3 | 2009-07-30 10:52:29 -0700 | [diff] [blame] | 438 | } |
| Bill Buzbee | 270c1d6 | 2009-08-13 16:58:07 -0700 | [diff] [blame] | 439 | if (shortForm) { |
| Ben Cheng | d7d426a | 2009-09-22 11:23:36 -0700 | [diff] [blame] | 440 | store = res = newLIR3(cUnit, opCode, rSrc, rBase, encodedDisp); |
| Bill Buzbee | 9bc3df3 | 2009-07-30 10:52:29 -0700 | [diff] [blame] | 441 | } else { |
| Bill Buzbee | 270c1d6 | 2009-08-13 16:58:07 -0700 | [diff] [blame] | 442 | assert(rScratch != -1); |
| Ben Cheng | d7d426a | 2009-09-22 11:23:36 -0700 | [diff] [blame] | 443 | res = loadConstant(cUnit, rScratch, encodedDisp); |
| 444 | store = newLIR3(cUnit, opCode, rSrc, rBase, rScratch); |
| 445 | } |
| 446 | |
| 447 | if (rBase == rFP) { |
| 448 | annotateDalvikRegAccess(store, displacement >> 2, false /* isLoad */); |
| Bill Buzbee | 9bc3df3 | 2009-07-30 10:52:29 -0700 | [diff] [blame] | 449 | } |
| Bill Buzbee | 270c1d6 | 2009-08-13 16:58:07 -0700 | [diff] [blame] | 450 | return res; |
| Bill Buzbee | 9bc3df3 | 2009-07-30 10:52:29 -0700 | [diff] [blame] | 451 | } |
| 452 | |
| 453 | /* |
| 454 | * Perform a "reg cmp imm" operation and jump to the PCR region if condition |
| 455 | * satisfies. |
| 456 | */ |
| 457 | static inline ArmLIR *genRegImmCheck(CompilationUnit *cUnit, |
| 458 | ArmConditionCode cond, int reg, |
| 459 | int checkValue, int dOffset, |
| 460 | ArmLIR *pcrLabel) |
| 461 | { |
| Ben Cheng | 0fd31e4 | 2009-09-03 14:40:16 -0700 | [diff] [blame] | 462 | if ((checkValue & 0xff) != checkValue) { |
| 463 | /* NOTE: direct use of hot temp r7 here. Revisit. */ |
| 464 | loadConstant(cUnit, r7, checkValue); |
| 465 | return genRegRegCheck(cUnit, cond, reg, r7, dOffset, pcrLabel); |
| 466 | } |
| Bill Buzbee | 9bc3df3 | 2009-07-30 10:52:29 -0700 | [diff] [blame] | 467 | newLIR2(cUnit, THUMB_CMP_RI8, reg, checkValue); |
| 468 | ArmLIR *branch = newLIR2(cUnit, THUMB_B_COND, 0, cond); |
| 469 | return genCheckCommon(cUnit, dOffset, branch, pcrLabel); |
| 470 | } |
| Bill Buzbee | 270c1d6 | 2009-08-13 16:58:07 -0700 | [diff] [blame] | 471 | |
| 472 | static ArmLIR *loadMultiple(CompilationUnit *cUnit, int rBase, int rMask) |
| 473 | { |
| 474 | return newLIR2(cUnit, THUMB_LDMIA, rBase, rMask); |
| 475 | } |
| 476 | |
| 477 | static ArmLIR *storeMultiple(CompilationUnit *cUnit, int rBase, int rMask) |
| 478 | { |
| 479 | return newLIR2(cUnit, THUMB_STMIA, rBase, rMask); |
| 480 | } |
| 481 | |
| 482 | static ArmLIR *opNone(CompilationUnit *cUnit, OpKind op) |
| 483 | { |
| 484 | ArmOpCode opCode = THUMB_BKPT; |
| 485 | switch (op) { |
| 486 | case OP_UNCOND_BR: |
| 487 | opCode = THUMB_B_UNCOND; |
| 488 | break; |
| 489 | default: |
| 490 | assert(0); |
| 491 | } |
| 492 | return newLIR0(cUnit, opCode); |
| 493 | } |
| 494 | |
| Ben Cheng | 4f48917 | 2009-09-27 17:08:35 -0700 | [diff] [blame^] | 495 | static ArmLIR *opCondBranch(CompilationUnit *cUnit, ArmConditionCode cc) |
| Bill Buzbee | 270c1d6 | 2009-08-13 16:58:07 -0700 | [diff] [blame] | 496 | { |
| Ben Cheng | 4f48917 | 2009-09-27 17:08:35 -0700 | [diff] [blame^] | 497 | return newLIR2(cUnit, THUMB_B_COND, 0 /* offset to be patched */, cc); |
| Bill Buzbee | 270c1d6 | 2009-08-13 16:58:07 -0700 | [diff] [blame] | 498 | } |
| 499 | |
| 500 | static ArmLIR *opImm(CompilationUnit *cUnit, OpKind op, int value) |
| 501 | { |
| 502 | ArmOpCode opCode = THUMB_BKPT; |
| 503 | switch (op) { |
| 504 | case OP_PUSH: |
| 505 | opCode = THUMB_PUSH; |
| 506 | break; |
| 507 | case OP_POP: |
| 508 | opCode = THUMB_POP; |
| 509 | break; |
| 510 | default: |
| 511 | assert(0); |
| 512 | } |
| 513 | return newLIR1(cUnit, opCode, value); |
| 514 | } |
| 515 | |
| 516 | static ArmLIR *opReg(CompilationUnit *cUnit, OpKind op, int rDestSrc) |
| 517 | { |
| 518 | ArmOpCode opCode = THUMB_BKPT; |
| 519 | switch (op) { |
| 520 | case OP_BLX: |
| 521 | opCode = THUMB_BLX_R; |
| 522 | break; |
| 523 | default: |
| 524 | assert(0); |
| 525 | } |
| 526 | return newLIR1(cUnit, opCode, rDestSrc); |
| 527 | } |
| 528 | |
| 529 | static ArmLIR *opRegReg(CompilationUnit *cUnit, OpKind op, int rDestSrc1, |
| 530 | int rSrc2) |
| 531 | { |
| 532 | ArmLIR *res; |
| 533 | ArmOpCode opCode = THUMB_BKPT; |
| 534 | switch (op) { |
| 535 | case OP_ADC: |
| Ben Cheng | dcf3e5d | 2009-09-11 13:42:05 -0700 | [diff] [blame] | 536 | opCode = THUMB_ADC_RR; |
| Bill Buzbee | 270c1d6 | 2009-08-13 16:58:07 -0700 | [diff] [blame] | 537 | break; |
| 538 | case OP_AND: |
| 539 | opCode = THUMB_AND_RR; |
| 540 | break; |
| 541 | case OP_BIC: |
| Ben Cheng | dcf3e5d | 2009-09-11 13:42:05 -0700 | [diff] [blame] | 542 | opCode = THUMB_BIC_RR; |
| Bill Buzbee | 270c1d6 | 2009-08-13 16:58:07 -0700 | [diff] [blame] | 543 | break; |
| 544 | case OP_CMN: |
| Ben Cheng | dcf3e5d | 2009-09-11 13:42:05 -0700 | [diff] [blame] | 545 | opCode = THUMB_CMN_RR; |
| Bill Buzbee | 270c1d6 | 2009-08-13 16:58:07 -0700 | [diff] [blame] | 546 | break; |
| 547 | case OP_CMP: |
| 548 | opCode = THUMB_CMP_RR; |
| 549 | break; |
| 550 | case OP_XOR: |
| Ben Cheng | dcf3e5d | 2009-09-11 13:42:05 -0700 | [diff] [blame] | 551 | opCode = THUMB_EOR_RR; |
| Bill Buzbee | 270c1d6 | 2009-08-13 16:58:07 -0700 | [diff] [blame] | 552 | break; |
| 553 | case OP_MOV: |
| 554 | if (LOWREG(rDestSrc1) && LOWREG(rSrc2)) |
| 555 | opCode = THUMB_MOV_RR; |
| 556 | else if (!LOWREG(rDestSrc1) && !LOWREG(rSrc2)) |
| 557 | opCode = THUMB_MOV_RR_H2H; |
| 558 | else if (LOWREG(rDestSrc1)) |
| 559 | opCode = THUMB_MOV_RR_H2L; |
| 560 | else |
| 561 | opCode = THUMB_MOV_RR_L2H; |
| Bill Buzbee | 270c1d6 | 2009-08-13 16:58:07 -0700 | [diff] [blame] | 562 | break; |
| 563 | case OP_MUL: |
| 564 | opCode = THUMB_MUL; |
| 565 | break; |
| 566 | case OP_MVN: |
| 567 | opCode = THUMB_MVN; |
| 568 | break; |
| 569 | case OP_NEG: |
| 570 | opCode = THUMB_NEG; |
| 571 | break; |
| 572 | case OP_OR: |
| 573 | opCode = THUMB_ORR; |
| 574 | break; |
| 575 | case OP_SBC: |
| 576 | opCode = THUMB_SBC; |
| 577 | break; |
| 578 | case OP_TST: |
| 579 | opCode = THUMB_TST; |
| 580 | break; |
| 581 | case OP_LSL: |
| Ben Cheng | dcf3e5d | 2009-09-11 13:42:05 -0700 | [diff] [blame] | 582 | opCode = THUMB_LSL_RR; |
| Bill Buzbee | 270c1d6 | 2009-08-13 16:58:07 -0700 | [diff] [blame] | 583 | break; |
| 584 | case OP_LSR: |
| Ben Cheng | 2275f9c | 2009-09-11 15:34:19 -0700 | [diff] [blame] | 585 | opCode = THUMB_LSR_RR; |
| Bill Buzbee | 270c1d6 | 2009-08-13 16:58:07 -0700 | [diff] [blame] | 586 | break; |
| 587 | case OP_ASR: |
| Ben Cheng | dcf3e5d | 2009-09-11 13:42:05 -0700 | [diff] [blame] | 588 | opCode = THUMB_ASR_RR; |
| Bill Buzbee | 270c1d6 | 2009-08-13 16:58:07 -0700 | [diff] [blame] | 589 | break; |
| 590 | case OP_ROR: |
| Ben Cheng | dcf3e5d | 2009-09-11 13:42:05 -0700 | [diff] [blame] | 591 | opCode = THUMB_ROR_RR; |
| Bill Buzbee | 270c1d6 | 2009-08-13 16:58:07 -0700 | [diff] [blame] | 592 | case OP_ADD: |
| 593 | case OP_SUB: |
| 594 | return opRegRegReg(cUnit, op, rDestSrc1, rDestSrc1, rSrc2); |
| 595 | case OP_2BYTE: |
| 596 | res = opRegRegImm(cUnit, OP_LSL, rDestSrc1, rSrc2, 24, rNone); |
| 597 | opRegRegImm(cUnit, OP_ASR, rDestSrc1, rDestSrc1, 24, rNone); |
| 598 | return res; |
| 599 | case OP_2SHORT: |
| 600 | res = opRegRegImm(cUnit, OP_LSL, rDestSrc1, rSrc2, 16, rNone); |
| 601 | opRegRegImm(cUnit, OP_ASR, rDestSrc1, rDestSrc1, 16, rNone); |
| 602 | return res; |
| 603 | case OP_2CHAR: |
| 604 | res = opRegRegImm(cUnit, OP_LSL, rDestSrc1, rSrc2, 16, rNone); |
| 605 | opRegRegImm(cUnit, OP_LSR, rDestSrc1, rDestSrc1, 16, rNone); |
| 606 | return res; |
| 607 | default: |
| 608 | assert(0); |
| 609 | break; |
| 610 | } |
| 611 | return newLIR2(cUnit, opCode, rDestSrc1, rSrc2); |
| 612 | } |
| 613 | |
| 614 | static ArmLIR *opRegImm(CompilationUnit *cUnit, OpKind op, int rDestSrc1, |
| 615 | int value, int rScratch) |
| 616 | { |
| 617 | ArmLIR *res; |
| 618 | bool neg = (value < 0); |
| 619 | int absValue = (neg) ? -value : value; |
| 620 | bool shortForm = (absValue & 0xff) == absValue; |
| 621 | ArmOpCode opCode = THUMB_BKPT; |
| 622 | switch (op) { |
| 623 | case OP_ADD: |
| 624 | if ( !neg && (rDestSrc1 == 13) && (value <= 508)) { /* sp */ |
| 625 | assert((value & 0x3) == 0); |
| 626 | return newLIR1(cUnit, THUMB_ADD_SPI7, value >> 2); |
| 627 | } else if (shortForm) { |
| 628 | opCode = (neg) ? THUMB_SUB_RI8 : THUMB_ADD_RI8; |
| 629 | } else |
| 630 | opCode = THUMB_ADD_RRR; |
| 631 | break; |
| 632 | case OP_SUB: |
| 633 | if (!neg && (rDestSrc1 == 13) && (value <= 508)) { /* sp */ |
| 634 | assert((value & 0x3) == 0); |
| 635 | return newLIR1(cUnit, THUMB_SUB_SPI7, value >> 2); |
| 636 | } else if (shortForm) { |
| 637 | opCode = (neg) ? THUMB_ADD_RI8 : THUMB_SUB_RI8; |
| 638 | } else |
| 639 | opCode = THUMB_SUB_RRR; |
| 640 | break; |
| 641 | case OP_CMP: |
| 642 | if (LOWREG(rDestSrc1) && shortForm) |
| 643 | opCode = (shortForm) ? THUMB_CMP_RI8 : THUMB_CMP_RR; |
| 644 | else if (LOWREG(rDestSrc1)) |
| 645 | opCode = THUMB_CMP_RR; |
| 646 | else { |
| 647 | shortForm = false; |
| 648 | opCode = THUMB_CMP_HL; |
| 649 | } |
| 650 | break; |
| 651 | default: |
| 652 | assert(0); |
| 653 | break; |
| 654 | } |
| 655 | if (shortForm) |
| 656 | res = newLIR2(cUnit, opCode, rDestSrc1, absValue); |
| 657 | else { |
| 658 | assert(rScratch != rNone); |
| 659 | res = loadConstant(cUnit, rScratch, value); |
| 660 | newLIR3(cUnit, opCode, rDestSrc1, rDestSrc1, rScratch); |
| 661 | } |
| 662 | return res; |
| 663 | } |
| 664 | |
| 665 | static ArmLIR *opRegRegReg(CompilationUnit *cUnit, OpKind op, int rDest, |
| 666 | int rSrc1, int rSrc2) |
| 667 | { |
| 668 | ArmOpCode opCode = THUMB_BKPT; |
| 669 | switch (op) { |
| 670 | case OP_ADD: |
| 671 | opCode = THUMB_ADD_RRR; |
| 672 | break; |
| 673 | case OP_SUB: |
| 674 | opCode = THUMB_SUB_RRR; |
| 675 | break; |
| 676 | default: |
| 677 | assert(0); |
| 678 | break; |
| 679 | } |
| 680 | return newLIR3(cUnit, opCode, rDest, rSrc1, rSrc2); |
| 681 | } |
| 682 | |
| 683 | static ArmLIR *opRegRegImm(CompilationUnit *cUnit, OpKind op, int rDest, |
| 684 | int rSrc1, int value, int rScratch) |
| 685 | { |
| 686 | ArmLIR *res; |
| 687 | bool neg = (value < 0); |
| 688 | int absValue = (neg) ? -value : value; |
| 689 | ArmOpCode opCode = THUMB_BKPT; |
| 690 | bool shortForm = (absValue & 0x7) == absValue; |
| 691 | switch(op) { |
| 692 | case OP_ADD: |
| 693 | if ((rSrc1 == 13) && (value <= 1020)) { /* sp */ |
| 694 | assert((value & 0x3) == 0); |
| 695 | shortForm = true; |
| 696 | opCode = THUMB_ADD_SP_REL; |
| 697 | value >>= 2; |
| 698 | } else if ((rSrc1 == 15) && (value <= 1020)) { /* pc */ |
| 699 | assert((value & 0x3) == 0); |
| 700 | shortForm = true; |
| 701 | opCode = THUMB_ADD_PC_REL; |
| 702 | value >>= 2; |
| 703 | } else if (shortForm) { |
| 704 | opCode = (neg) ? THUMB_SUB_RRI3 : THUMB_ADD_RRI3; |
| 705 | } else if ((absValue > 0) && (absValue <= (255 + 7))) { |
| 706 | /* Two shots - 1st handle the 7 */ |
| 707 | opCode = (neg) ? THUMB_SUB_RRI3 : THUMB_ADD_RRI3; |
| 708 | res = newLIR3(cUnit, opCode, rDest, rSrc1, 7); |
| 709 | opCode = (neg) ? THUMB_SUB_RI8 : THUMB_ADD_RI8; |
| 710 | newLIR2(cUnit, opCode, rDest, absValue - 7); |
| 711 | return res; |
| 712 | } else |
| 713 | opCode = THUMB_ADD_RRR; |
| 714 | break; |
| 715 | |
| 716 | case OP_SUB: |
| 717 | if (shortForm) { |
| 718 | opCode = (neg) ? THUMB_ADD_RRI3 : THUMB_SUB_RRI3; |
| 719 | } else if ((absValue > 0) && (absValue <= (255 + 7))) { |
| 720 | /* Two shots - 1st handle the 7 */ |
| 721 | opCode = (neg) ? THUMB_ADD_RRI3 : THUMB_SUB_RRI3; |
| 722 | res = newLIR3(cUnit, opCode, rDest, rSrc1, 7); |
| 723 | opCode = (neg) ? THUMB_ADD_RI8 : THUMB_SUB_RI8; |
| 724 | newLIR2(cUnit, opCode, rDest, absValue - 7); |
| 725 | return res; |
| 726 | } else |
| 727 | opCode = THUMB_SUB_RRR; |
| 728 | break; |
| 729 | case OP_LSL: |
| 730 | shortForm = (!neg && value <= 31); |
| Ben Cheng | dcf3e5d | 2009-09-11 13:42:05 -0700 | [diff] [blame] | 731 | opCode = THUMB_LSL_RRI5; |
| Bill Buzbee | 270c1d6 | 2009-08-13 16:58:07 -0700 | [diff] [blame] | 732 | break; |
| 733 | case OP_LSR: |
| 734 | shortForm = (!neg && value <= 31); |
| Ben Cheng | dcf3e5d | 2009-09-11 13:42:05 -0700 | [diff] [blame] | 735 | opCode = THUMB_LSR_RRI5; |
| Bill Buzbee | 270c1d6 | 2009-08-13 16:58:07 -0700 | [diff] [blame] | 736 | break; |
| 737 | case OP_ASR: |
| 738 | shortForm = (!neg && value <= 31); |
| Ben Cheng | dcf3e5d | 2009-09-11 13:42:05 -0700 | [diff] [blame] | 739 | opCode = THUMB_ASR_RRI5; |
| Bill Buzbee | 270c1d6 | 2009-08-13 16:58:07 -0700 | [diff] [blame] | 740 | break; |
| 741 | case OP_MUL: |
| 742 | case OP_AND: |
| 743 | case OP_OR: |
| 744 | case OP_XOR: |
| 745 | if (rDest == rSrc1) { |
| 746 | res = loadConstant(cUnit, rScratch, value); |
| 747 | opRegReg(cUnit, op, rDest, rScratch); |
| 748 | } else { |
| 749 | res = loadConstant(cUnit, rDest, value); |
| 750 | opRegReg(cUnit, op, rDest, rSrc1); |
| 751 | } |
| 752 | return res; |
| 753 | default: |
| 754 | assert(0); |
| 755 | break; |
| 756 | } |
| 757 | if (shortForm) |
| 758 | res = newLIR3(cUnit, opCode, rDest, rSrc1, absValue); |
| 759 | else { |
| 760 | if (rDest != rSrc1) { |
| 761 | res = loadConstant(cUnit, rDest, value); |
| 762 | newLIR3(cUnit, opCode, rDest, rSrc1, rDest); |
| 763 | } else { |
| 764 | assert(rScratch != rNone); |
| 765 | res = loadConstant(cUnit, rScratch, value); |
| 766 | newLIR3(cUnit, opCode, rDest, rSrc1, rScratch); |
| 767 | } |
| 768 | } |
| 769 | return res; |
| 770 | } |
| 771 | |
| Bill Buzbee | a4a7f07 | 2009-08-27 13:58:09 -0700 | [diff] [blame] | 772 | static void genCmpLong(CompilationUnit *cUnit, MIR *mir, |
| 773 | int vDest, int vSrc1, int vSrc2) |
| 774 | { |
| 775 | loadValuePair(cUnit, vSrc1, r0, r1); |
| 776 | loadValuePair(cUnit, vSrc2, r2, r3); |
| 777 | genDispatchToHandler(cUnit, TEMPLATE_CMP_LONG); |
| 778 | storeValue(cUnit, r0, vDest, r1); |
| 779 | } |
| 780 | |
| Bill Buzbee | 270c1d6 | 2009-08-13 16:58:07 -0700 | [diff] [blame] | 781 | static bool genInlinedStringLength(CompilationUnit *cUnit, MIR *mir) |
| 782 | { |
| 783 | DecodedInstruction *dInsn = &mir->dalvikInsn; |
| 784 | int offset = offsetof(InterpState, retval); |
| 785 | int regObj = selectFirstRegister(cUnit, dInsn->arg[0], false); |
| 786 | int reg1 = NEXT_REG(regObj); |
| 787 | loadValue(cUnit, dInsn->arg[0], regObj); |
| 788 | genNullCheck(cUnit, dInsn->arg[0], regObj, mir->offset, NULL); |
| 789 | loadWordDisp(cUnit, regObj, gDvm.offJavaLangString_count, reg1); |
| 790 | storeWordDisp(cUnit, rGLUE, offset, reg1, regObj); |
| 791 | return false; |
| 792 | } |
| 793 | |
| 794 | static bool genInlinedStringCharAt(CompilationUnit *cUnit, MIR *mir) |
| 795 | { |
| 796 | DecodedInstruction *dInsn = &mir->dalvikInsn; |
| 797 | int offset = offsetof(InterpState, retval); |
| 798 | int contents = offsetof(ArrayObject, contents); |
| 799 | int regObj = selectFirstRegister(cUnit, dInsn->arg[0], false); |
| 800 | int regIdx = NEXT_REG(regObj); |
| 801 | int regMax = NEXT_REG(regIdx); |
| 802 | int regOff = NEXT_REG(regMax); |
| 803 | loadValue(cUnit, dInsn->arg[0], regObj); |
| 804 | loadValue(cUnit, dInsn->arg[1], regIdx); |
| 805 | ArmLIR * pcrLabel = genNullCheck(cUnit, dInsn->arg[0], regObj, |
| 806 | mir->offset, NULL); |
| 807 | loadWordDisp(cUnit, regObj, gDvm.offJavaLangString_count, regMax); |
| 808 | loadWordDisp(cUnit, regObj, gDvm.offJavaLangString_offset, regOff); |
| 809 | loadWordDisp(cUnit, regObj, gDvm.offJavaLangString_value, regObj); |
| 810 | genBoundsCheck(cUnit, regIdx, regMax, mir->offset, pcrLabel); |
| 811 | |
| 812 | newLIR2(cUnit, THUMB_ADD_RI8, regObj, contents); |
| 813 | newLIR3(cUnit, THUMB_ADD_RRR, regIdx, regIdx, regOff); |
| 814 | newLIR3(cUnit, THUMB_ADD_RRR, regIdx, regIdx, regIdx); |
| 815 | newLIR3(cUnit, THUMB_LDRH_RRR, regMax, regObj, regIdx); |
| 816 | storeWordDisp(cUnit, rGLUE, offset, regMax, regObj); |
| 817 | return false; |
| 818 | } |
| 819 | |
| 820 | static bool genInlinedAbsInt(CompilationUnit *cUnit, MIR *mir) |
| 821 | { |
| 822 | int offset = offsetof(InterpState, retval); |
| 823 | DecodedInstruction *dInsn = &mir->dalvikInsn; |
| 824 | int reg0 = selectFirstRegister(cUnit, dInsn->arg[0], false); |
| 825 | int sign = NEXT_REG(reg0); |
| 826 | /* abs(x) = y<=x>>31, (x+y)^y. Shorter in ARM/THUMB2, no skip in THUMB */ |
| 827 | loadValue(cUnit, dInsn->arg[0], reg0); |
| Ben Cheng | dcf3e5d | 2009-09-11 13:42:05 -0700 | [diff] [blame] | 828 | newLIR3(cUnit, THUMB_ASR_RRI5, sign, reg0, 31); |
| Bill Buzbee | 270c1d6 | 2009-08-13 16:58:07 -0700 | [diff] [blame] | 829 | newLIR3(cUnit, THUMB_ADD_RRR, reg0, reg0, sign); |
| Ben Cheng | dcf3e5d | 2009-09-11 13:42:05 -0700 | [diff] [blame] | 830 | newLIR2(cUnit, THUMB_EOR_RR, reg0, sign); |
| Bill Buzbee | 270c1d6 | 2009-08-13 16:58:07 -0700 | [diff] [blame] | 831 | storeWordDisp(cUnit, rGLUE, offset, reg0, sign); |
| 832 | return false; |
| 833 | } |
| 834 | |
| 835 | static bool genInlinedAbsFloat(CompilationUnit *cUnit, MIR *mir) |
| 836 | { |
| 837 | int offset = offsetof(InterpState, retval); |
| 838 | DecodedInstruction *dInsn = &mir->dalvikInsn; |
| 839 | int reg0 = selectFirstRegister(cUnit, dInsn->arg[0], false); |
| 840 | int signMask = NEXT_REG(reg0); |
| 841 | loadValue(cUnit, dInsn->arg[0], reg0); |
| 842 | loadConstant(cUnit, signMask, 0x7fffffff); |
| 843 | newLIR2(cUnit, THUMB_AND_RR, reg0, signMask); |
| 844 | storeWordDisp(cUnit, rGLUE, offset, reg0, signMask); |
| 845 | return false; |
| 846 | } |
| 847 | |
| 848 | static bool genInlinedAbsDouble(CompilationUnit *cUnit, MIR *mir) |
| 849 | { |
| 850 | int offset = offsetof(InterpState, retval); |
| 851 | DecodedInstruction *dInsn = &mir->dalvikInsn; |
| 852 | int oplo = selectFirstRegister(cUnit, dInsn->arg[0], true); |
| 853 | int ophi = NEXT_REG(oplo); |
| 854 | int signMask = NEXT_REG(ophi); |
| 855 | loadValuePair(cUnit, dInsn->arg[0], oplo, ophi); |
| 856 | loadConstant(cUnit, signMask, 0x7fffffff); |
| 857 | storeWordDisp(cUnit, rGLUE, offset, oplo, ophi); |
| 858 | newLIR2(cUnit, THUMB_AND_RR, ophi, signMask); |
| 859 | storeWordDisp(cUnit, rGLUE, offset + 4, ophi, oplo); |
| 860 | return false; |
| 861 | } |
| 862 | |
| 863 | /* No select in thumb, so we need to branch. Thumb2 will do better */ |
| 864 | static bool genInlinedMinMaxInt(CompilationUnit *cUnit, MIR *mir, bool isMin) |
| 865 | { |
| 866 | int offset = offsetof(InterpState, retval); |
| 867 | DecodedInstruction *dInsn = &mir->dalvikInsn; |
| 868 | int reg0 = selectFirstRegister(cUnit, dInsn->arg[0], false); |
| 869 | int reg1 = NEXT_REG(reg0); |
| 870 | loadValue(cUnit, dInsn->arg[0], reg0); |
| 871 | loadValue(cUnit, dInsn->arg[1], reg1); |
| 872 | newLIR2(cUnit, THUMB_CMP_RR, reg0, reg1); |
| 873 | ArmLIR *branch1 = newLIR2(cUnit, THUMB_B_COND, 2, |
| 874 | isMin ? ARM_COND_LT : ARM_COND_GT); |
| 875 | newLIR2(cUnit, THUMB_MOV_RR, reg0, reg1); |
| 876 | ArmLIR *target = |
| 877 | newLIR3(cUnit, THUMB_STR_RRI5, reg0, rGLUE, offset >> 2); |
| 878 | branch1->generic.target = (LIR *)target; |
| 879 | return false; |
| 880 | } |
| 881 | |
| 882 | static bool genInlinedAbsLong(CompilationUnit *cUnit, MIR *mir) |
| 883 | { |
| 884 | int offset = offsetof(InterpState, retval); |
| 885 | DecodedInstruction *dInsn = &mir->dalvikInsn; |
| 886 | int oplo = selectFirstRegister(cUnit, dInsn->arg[0], true); |
| 887 | int ophi = NEXT_REG(oplo); |
| 888 | int sign = NEXT_REG(ophi); |
| 889 | /* abs(x) = y<=x>>31, (x+y)^y. Shorter in ARM/THUMB2, no skip in THUMB */ |
| 890 | loadValuePair(cUnit, dInsn->arg[0], oplo, ophi); |
| Ben Cheng | dcf3e5d | 2009-09-11 13:42:05 -0700 | [diff] [blame] | 891 | newLIR3(cUnit, THUMB_ASR_RRI5, sign, ophi, 31); |
| Bill Buzbee | 270c1d6 | 2009-08-13 16:58:07 -0700 | [diff] [blame] | 892 | newLIR3(cUnit, THUMB_ADD_RRR, oplo, oplo, sign); |
| Ben Cheng | dcf3e5d | 2009-09-11 13:42:05 -0700 | [diff] [blame] | 893 | newLIR2(cUnit, THUMB_ADC_RR, ophi, sign); |
| 894 | newLIR2(cUnit, THUMB_EOR_RR, oplo, sign); |
| 895 | newLIR2(cUnit, THUMB_EOR_RR, ophi, sign); |
| Bill Buzbee | 270c1d6 | 2009-08-13 16:58:07 -0700 | [diff] [blame] | 896 | storeWordDisp(cUnit, rGLUE, offset, oplo, sign); |
| 897 | storeWordDisp(cUnit, rGLUE, offset + 4, ophi, sign); |
| 898 | return false; |
| 899 | } |