buzbee | e3acd07 | 2012-02-25 17:03:10 -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 | /* |
| 18 | * This file contains codegen for the Mips ISA and is intended to be |
| 19 | * includes by: |
| 20 | * |
| 21 | * Codegen-$(TARGET_ARCH_VARIANT).c |
| 22 | * |
| 23 | */ |
| 24 | |
| 25 | namespace art { |
| 26 | |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 27 | /* |
buzbee | c5159d5 | 2012-03-03 11:48:39 -0800 | [diff] [blame] | 28 | * The lack of pc-relative loads on Mips presents somewhat of a challenge |
| 29 | * for our PIC switch table strategy. To materialize the current location |
| 30 | * we'll do a dummy JAL and reference our tables using r_RA as the |
| 31 | * base register. Note that r_RA will be used both as the base to |
| 32 | * locate the switch table data and as the reference base for the switch |
| 33 | * target offsets stored in the table. We'll use a special pseudo-instruction |
| 34 | * to represent the jal and trigger the construction of the |
| 35 | * switch table offsets (which will happen after final assembly and all |
| 36 | * labels are fixed). |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 37 | * |
| 38 | * The test loop will look something like: |
| 39 | * |
buzbee | c5159d5 | 2012-03-03 11:48:39 -0800 | [diff] [blame] | 40 | * ori rEnd, r_ZERO, #tableSize ; size in bytes |
| 41 | * jal BaseLabel ; stores "return address" (BaseLabel) in r_RA |
| 42 | * nop ; opportunistically fill |
| 43 | * BaseLabel: |
| 44 | * addiu rBase, r_RA, <table> - <BaseLabel> ; table relative to BaseLabel |
| 45 | addu rEnd, rEnd, rBase ; end of table |
| 46 | * lw rVal, [rSP, vRegOff] ; Test Value |
| 47 | * loop: |
| 48 | * beq rBase, rEnd, done |
| 49 | * lw rKey, 0(rBase) |
| 50 | * addu rBase, 8 |
| 51 | * bne rVal, rKey, loop |
| 52 | * lw rDisp, -4(rBase) |
| 53 | * addu r_RA, rDisp |
| 54 | * jr r_RA |
| 55 | * done: |
| 56 | * |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 57 | */ |
buzbee | 5de3494 | 2012-03-01 14:51:57 -0800 | [diff] [blame] | 58 | void genSparseSwitch(CompilationUnit* cUnit, MIR* mir, RegLocation rlSrc) |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 59 | { |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 60 | const u2* table = cUnit->insns + mir->offset + mir->dalvikInsn.vB; |
| 61 | if (cUnit->printMe) { |
| 62 | dumpSparseSwitchTable(table); |
| 63 | } |
| 64 | // Add the table to the list - we'll process it later |
| 65 | SwitchTable *tabRec = (SwitchTable *)oatNew(cUnit, sizeof(SwitchTable), |
| 66 | true, kAllocData); |
| 67 | tabRec->table = table; |
| 68 | tabRec->vaddr = mir->offset; |
buzbee | c5159d5 | 2012-03-03 11:48:39 -0800 | [diff] [blame] | 69 | int elements = table[1]; |
| 70 | tabRec->targets = (LIR* *)oatNew(cUnit, elements * sizeof(LIR*), true, |
buzbee | 5de3494 | 2012-03-01 14:51:57 -0800 | [diff] [blame] | 71 | kAllocLIR); |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 72 | oatInsertGrowableList(cUnit, &cUnit->switchTables, (intptr_t)tabRec); |
| 73 | |
buzbee | c5159d5 | 2012-03-03 11:48:39 -0800 | [diff] [blame] | 74 | // The table is composed of 8-byte key/disp pairs |
| 75 | int byteSize = elements * 8; |
| 76 | |
| 77 | int sizeHi = byteSize >> 16; |
| 78 | int sizeLo = byteSize & 0xffff; |
| 79 | |
| 80 | int rEnd = oatAllocTemp(cUnit); |
| 81 | if (sizeHi) { |
| 82 | newLIR2(cUnit, kMipsLui, rEnd, sizeHi); |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 83 | } |
buzbee | c5159d5 | 2012-03-03 11:48:39 -0800 | [diff] [blame] | 84 | // Must prevent code motion for the curr pc pair |
| 85 | genBarrier(cUnit); // Scheduling barrier |
| 86 | newLIR0(cUnit, kMipsCurrPC); // Really a jal to .+8 |
| 87 | // Now, fill the branch delay slot |
| 88 | if (sizeHi) { |
| 89 | newLIR3(cUnit, kMipsOri, rEnd, rEnd, sizeLo); |
| 90 | } else { |
| 91 | newLIR3(cUnit, kMipsOri, rEnd, r_ZERO, sizeLo); |
| 92 | } |
| 93 | genBarrier(cUnit); // Scheduling barrier |
| 94 | |
| 95 | // Construct BaseLabel and set up table base register |
| 96 | LIR* baseLabel = newLIR0(cUnit, kPseudoTargetLabel); |
| 97 | // Remember base label so offsets can be computed later |
| 98 | tabRec->anchor = baseLabel; |
| 99 | int rBase = oatAllocTemp(cUnit); |
| 100 | newLIR4(cUnit, kMipsDelta, rBase, 0, (intptr_t)baseLabel, (intptr_t)tabRec); |
| 101 | opRegRegReg(cUnit, kOpAdd, rEnd, rEnd, rBase); |
| 102 | |
| 103 | // Grab switch test value |
| 104 | rlSrc = loadValue(cUnit, rlSrc, kCoreReg); |
| 105 | |
| 106 | // Test loop |
| 107 | int rKey = oatAllocTemp(cUnit); |
| 108 | LIR* loopLabel = newLIR0(cUnit, kPseudoTargetLabel); |
| 109 | LIR* exitBranch = opCmpBranch(cUnit , kCondEq, rBase, rEnd, NULL); |
| 110 | loadWordDisp(cUnit, rBase, 0, rKey); |
| 111 | opRegImm(cUnit, kOpAdd, rBase, 8); |
| 112 | opCmpBranch(cUnit, kCondNe, rlSrc.lowReg, rKey, loopLabel); |
| 113 | int rDisp = oatAllocTemp(cUnit); |
| 114 | loadWordDisp(cUnit, rBase, -4, rDisp); |
| 115 | opRegRegReg(cUnit, kOpAdd, r_RA, r_RA, rDisp); |
| 116 | opReg(cUnit, kOpBx, r_RA); |
| 117 | |
| 118 | // Loop exit |
| 119 | LIR* exitLabel = newLIR0(cUnit, kPseudoTargetLabel); |
| 120 | exitBranch->target = exitLabel; |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 121 | } |
| 122 | |
buzbee | c5159d5 | 2012-03-03 11:48:39 -0800 | [diff] [blame] | 123 | /* |
| 124 | * Code pattern will look something like: |
| 125 | * |
| 126 | * lw rVal |
| 127 | * jal BaseLabel ; stores "return address" (BaseLabel) in r_RA |
| 128 | * nop ; opportunistically fill |
| 129 | * [subiu rVal, bias] ; Remove bias if lowVal != 0 |
| 130 | * bound check -> done |
| 131 | * lw rDisp, [r_RA, rVal] |
| 132 | * addu r_RA, rDisp |
| 133 | * jr r_RA |
| 134 | * done: |
| 135 | */ |
buzbee | 5de3494 | 2012-03-01 14:51:57 -0800 | [diff] [blame] | 136 | void genPackedSwitch(CompilationUnit* cUnit, MIR* mir, RegLocation rlSrc) |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 137 | { |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 138 | const u2* table = cUnit->insns + mir->offset + mir->dalvikInsn.vB; |
| 139 | if (cUnit->printMe) { |
| 140 | dumpPackedSwitchTable(table); |
| 141 | } |
| 142 | // Add the table to the list - we'll process it later |
| 143 | SwitchTable *tabRec = (SwitchTable *)oatNew(cUnit, sizeof(SwitchTable), |
| 144 | true, kAllocData); |
| 145 | tabRec->table = table; |
| 146 | tabRec->vaddr = mir->offset; |
| 147 | int size = table[1]; |
buzbee | 5de3494 | 2012-03-01 14:51:57 -0800 | [diff] [blame] | 148 | tabRec->targets = (LIR* *)oatNew(cUnit, size * sizeof(LIR*), true, |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 149 | kAllocLIR); |
| 150 | oatInsertGrowableList(cUnit, &cUnit->switchTables, (intptr_t)tabRec); |
| 151 | |
| 152 | // Get the switch value |
| 153 | rlSrc = loadValue(cUnit, rlSrc, kCoreReg); |
buzbee | c5159d5 | 2012-03-03 11:48:39 -0800 | [diff] [blame] | 154 | |
| 155 | // Prepare the bias. If too big, handle 1st stage here |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 156 | int lowKey = s4FromSwitchData(&table[2]); |
buzbee | c5159d5 | 2012-03-03 11:48:39 -0800 | [diff] [blame] | 157 | bool largeBias = false; |
| 158 | int rKey; |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 159 | if (lowKey == 0) { |
buzbee | c5159d5 | 2012-03-03 11:48:39 -0800 | [diff] [blame] | 160 | rKey = rlSrc.lowReg; |
| 161 | } else if ((lowKey & 0xffff) != lowKey) { |
| 162 | rKey = oatAllocTemp(cUnit); |
| 163 | loadConstant(cUnit, rKey, lowKey); |
| 164 | largeBias = true; |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 165 | } else { |
buzbee | c5159d5 | 2012-03-03 11:48:39 -0800 | [diff] [blame] | 166 | rKey = oatAllocTemp(cUnit); |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 167 | } |
buzbee | c5159d5 | 2012-03-03 11:48:39 -0800 | [diff] [blame] | 168 | |
| 169 | // Must prevent code motion for the curr pc pair |
| 170 | genBarrier(cUnit); |
| 171 | newLIR0(cUnit, kMipsCurrPC); // Really a jal to .+8 |
| 172 | // Now, fill the branch delay slot with bias strip |
| 173 | if (lowKey == 0) { |
| 174 | newLIR0(cUnit, kMipsNop); |
| 175 | } else { |
| 176 | if (largeBias) { |
| 177 | opRegRegReg(cUnit, kOpSub, rKey, rlSrc.lowReg, rKey); |
| 178 | } else { |
| 179 | opRegRegImm(cUnit, kOpSub, rKey, rlSrc.lowReg, lowKey); |
| 180 | } |
| 181 | } |
| 182 | genBarrier(cUnit); // Scheduling barrier |
| 183 | |
| 184 | // Construct BaseLabel and set up table base register |
| 185 | LIR* baseLabel = newLIR0(cUnit, kPseudoTargetLabel); |
| 186 | // Remember base label so offsets can be computed later |
| 187 | tabRec->anchor = baseLabel; |
| 188 | |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 189 | // Bounds check - if < 0 or >= size continue following switch |
buzbee | c5159d5 | 2012-03-03 11:48:39 -0800 | [diff] [blame] | 190 | LIR* branchOver = opCmpImmBranch(cUnit, kCondHi, rKey, size-1, NULL); |
| 191 | |
| 192 | // Materialize the table base pointer |
| 193 | int rBase = oatAllocTemp(cUnit); |
| 194 | newLIR4(cUnit, kMipsDelta, rBase, 0, (intptr_t)baseLabel, (intptr_t)tabRec); |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 195 | |
| 196 | // Load the displacement from the switch table |
buzbee | c5159d5 | 2012-03-03 11:48:39 -0800 | [diff] [blame] | 197 | int rDisp = oatAllocTemp(cUnit); |
| 198 | loadBaseIndexed(cUnit, rBase, rKey, rDisp, 2, kWord); |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 199 | |
buzbee | c5159d5 | 2012-03-03 11:48:39 -0800 | [diff] [blame] | 200 | // Add to r_AP and go |
| 201 | opRegRegReg(cUnit, kOpAdd, r_RA, r_RA, rDisp); |
| 202 | opReg(cUnit, kOpBx, r_RA); |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 203 | |
| 204 | /* branchOver target here */ |
buzbee | 5de3494 | 2012-03-01 14:51:57 -0800 | [diff] [blame] | 205 | LIR* target = newLIR0(cUnit, kPseudoTargetLabel); |
buzbee | 5de3494 | 2012-03-01 14:51:57 -0800 | [diff] [blame] | 206 | branchOver->target = (LIR*)target; |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 207 | } |
| 208 | |
| 209 | /* |
| 210 | * Array data table format: |
| 211 | * ushort ident = 0x0300 magic value |
| 212 | * ushort width width of each element in the table |
| 213 | * uint size number of elements in the table |
| 214 | * ubyte data[size*width] table of data values (may contain a single-byte |
| 215 | * padding at the end) |
| 216 | * |
| 217 | * Total size is 4+(width * size + 1)/2 16-bit code units. |
| 218 | */ |
buzbee | 5de3494 | 2012-03-01 14:51:57 -0800 | [diff] [blame] | 219 | void genFillArrayData(CompilationUnit* cUnit, MIR* mir, RegLocation rlSrc) |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 220 | { |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 221 | const u2* table = cUnit->insns + mir->offset + mir->dalvikInsn.vB; |
| 222 | // Add the table to the list - we'll process it later |
| 223 | FillArrayData *tabRec = (FillArrayData *) |
| 224 | oatNew(cUnit, sizeof(FillArrayData), true, kAllocData); |
| 225 | tabRec->table = table; |
| 226 | tabRec->vaddr = mir->offset; |
| 227 | u2 width = tabRec->table[1]; |
| 228 | u4 size = tabRec->table[2] | (((u4)tabRec->table[3]) << 16); |
| 229 | tabRec->size = (size * width) + 8; |
| 230 | |
| 231 | oatInsertGrowableList(cUnit, &cUnit->fillArrayData, (intptr_t)tabRec); |
| 232 | |
| 233 | // Making a call - use explicit registers |
| 234 | oatFlushAllRegs(cUnit); /* Everything to home location */ |
buzbee | c5159d5 | 2012-03-03 11:48:39 -0800 | [diff] [blame] | 235 | oatLockCallTemps(cUnit); |
buzbee | 5de3494 | 2012-03-01 14:51:57 -0800 | [diff] [blame] | 236 | loadValueDirectFixed(cUnit, rlSrc, rARG0); |
buzbee | c5159d5 | 2012-03-03 11:48:39 -0800 | [diff] [blame] | 237 | |
| 238 | // Must prevent code motion for the curr pc pair |
| 239 | genBarrier(cUnit); |
| 240 | newLIR0(cUnit, kMipsCurrPC); // Really a jal to .+8 |
| 241 | // Now, fill the branch delay slot with the helper load |
| 242 | int rTgt = loadHelper(cUnit, OFFSETOF_MEMBER(Thread, |
| 243 | pHandleFillArrayDataFromCode)); |
| 244 | genBarrier(cUnit); // Scheduling barrier |
| 245 | |
| 246 | // Construct BaseLabel and set up table base register |
| 247 | LIR* baseLabel = newLIR0(cUnit, kPseudoTargetLabel); |
| 248 | |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 249 | // Materialize a pointer to the fill data image |
buzbee | c5159d5 | 2012-03-03 11:48:39 -0800 | [diff] [blame] | 250 | newLIR4(cUnit, kMipsDelta, rARG1, 0, (intptr_t)baseLabel, (intptr_t)tabRec); |
| 251 | |
| 252 | // And go... |
| 253 | callRuntimeHelper(cUnit, rTgt); // ( array*, fill_data* ) |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 254 | } |
| 255 | |
buzbee | 71ac994 | 2012-03-01 17:23:10 -0800 | [diff] [blame] | 256 | void genNegFloat(CompilationUnit *cUnit, RegLocation rlDest, RegLocation rlSrc) |
| 257 | { |
| 258 | RegLocation rlResult; |
| 259 | rlSrc = loadValue(cUnit, rlSrc, kCoreReg); |
| 260 | rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true); |
| 261 | opRegRegImm(cUnit, kOpAdd, rlResult.lowReg, |
| 262 | rlSrc.lowReg, 0x80000000); |
| 263 | storeValue(cUnit, rlDest, rlResult); |
| 264 | } |
| 265 | |
| 266 | void genNegDouble(CompilationUnit *cUnit, RegLocation rlDest, RegLocation rlSrc) |
| 267 | { |
| 268 | RegLocation rlResult; |
| 269 | rlSrc = loadValueWide(cUnit, rlSrc, kCoreReg); |
| 270 | rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true); |
| 271 | opRegRegImm(cUnit, kOpAdd, rlResult.highReg, rlSrc.highReg, |
| 272 | 0x80000000); |
buzbee | 82488f5 | 2012-03-02 08:20:26 -0800 | [diff] [blame] | 273 | opRegCopy(cUnit, rlResult.lowReg, rlSrc.lowReg); |
buzbee | 71ac994 | 2012-03-01 17:23:10 -0800 | [diff] [blame] | 274 | storeValueWide(cUnit, rlDest, rlResult); |
| 275 | } |
| 276 | |
buzbee | 5de3494 | 2012-03-01 14:51:57 -0800 | [diff] [blame] | 277 | /* |
| 278 | * TODO: implement fast path to short-circuit thin-lock case |
| 279 | */ |
| 280 | void genMonitorEnter(CompilationUnit* cUnit, MIR* mir, RegLocation rlSrc) |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 281 | { |
buzbee | 5de3494 | 2012-03-01 14:51:57 -0800 | [diff] [blame] | 282 | oatFlushAllRegs(cUnit); |
| 283 | loadValueDirectFixed(cUnit, rlSrc, rARG0); // Get obj |
| 284 | oatLockCallTemps(cUnit); // Prepare for explicit register usage |
| 285 | genNullCheck(cUnit, rlSrc.sRegLow, rARG0, mir); |
| 286 | // Go expensive route - artLockObjectFromCode(self, obj); |
| 287 | int rTgt = loadHelper(cUnit, OFFSETOF_MEMBER(Thread, pLockObjectFromCode)); |
| 288 | callRuntimeHelper(cUnit, rTgt); |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 289 | } |
| 290 | |
| 291 | /* |
buzbee | 5de3494 | 2012-03-01 14:51:57 -0800 | [diff] [blame] | 292 | * TODO: implement fast path to short-circuit thin-lock case |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 293 | */ |
buzbee | 5de3494 | 2012-03-01 14:51:57 -0800 | [diff] [blame] | 294 | void genMonitorExit(CompilationUnit* cUnit, MIR* mir, RegLocation rlSrc) |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 295 | { |
buzbee | 5de3494 | 2012-03-01 14:51:57 -0800 | [diff] [blame] | 296 | oatFlushAllRegs(cUnit); |
| 297 | loadValueDirectFixed(cUnit, rlSrc, rARG0); // Get obj |
| 298 | oatLockCallTemps(cUnit); // Prepare for explicit register usage |
| 299 | genNullCheck(cUnit, rlSrc.sRegLow, rARG0, mir); |
| 300 | // Go expensive route - UnlockObjectFromCode(obj); |
| 301 | int rTgt = loadHelper(cUnit, OFFSETOF_MEMBER(Thread, pUnlockObjectFromCode)); |
| 302 | callRuntimeHelper(cUnit, rTgt); |
| 303 | } |
| 304 | |
| 305 | /* |
| 306 | * Compare two 64-bit values |
| 307 | * x = y return 0 |
| 308 | * x < y return -1 |
| 309 | * x > y return 1 |
| 310 | * |
| 311 | * slt t0, x.hi, y.hi; # (x.hi < y.hi) ? 1:0 |
| 312 | * sgt t1, x.hi, y.hi; # (y.hi > x.hi) ? 1:0 |
| 313 | * subu res, t0, t1 # res = -1:1:0 for [ < > = ] |
| 314 | * bnez res, finish |
| 315 | * sltu t0, x.lo, y.lo |
| 316 | * sgtu r1, x.lo, y.lo |
| 317 | * subu res, t0, t1 |
| 318 | * finish: |
| 319 | * |
| 320 | */ |
| 321 | void genCmpLong(CompilationUnit* cUnit, MIR* mir, RegLocation rlDest, |
| 322 | RegLocation rlSrc1, RegLocation rlSrc2) |
| 323 | { |
| 324 | rlSrc1 = loadValueWide(cUnit, rlSrc1, kCoreReg); |
| 325 | rlSrc2 = loadValueWide(cUnit, rlSrc2, kCoreReg); |
| 326 | int t0 = oatAllocTemp(cUnit); |
| 327 | int t1 = oatAllocTemp(cUnit); |
| 328 | RegLocation rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true); |
| 329 | newLIR3(cUnit, kMipsSlt, t0, rlSrc1.highReg, rlSrc2.highReg); |
| 330 | newLIR3(cUnit, kMipsSlt, t1, rlSrc2.highReg, rlSrc1.highReg); |
| 331 | newLIR3(cUnit, kMipsSubu, rlResult.lowReg, t1, t0); |
buzbee | 82488f5 | 2012-03-02 08:20:26 -0800 | [diff] [blame] | 332 | LIR* branch = opCmpImmBranch(cUnit, kCondNe, rlResult.lowReg, 0, NULL); |
buzbee | 5de3494 | 2012-03-01 14:51:57 -0800 | [diff] [blame] | 333 | newLIR3(cUnit, kMipsSltu, t0, rlSrc1.lowReg, rlSrc2.lowReg); |
| 334 | newLIR3(cUnit, kMipsSltu, t1, rlSrc2.lowReg, rlSrc1.lowReg); |
| 335 | newLIR3(cUnit, kMipsSubu, rlResult.lowReg, t1, t0); |
| 336 | oatFreeTemp(cUnit, t0); |
| 337 | oatFreeTemp(cUnit, t1); |
| 338 | LIR* target = newLIR0(cUnit, kPseudoTargetLabel); |
buzbee | 5de3494 | 2012-03-01 14:51:57 -0800 | [diff] [blame] | 339 | branch->target = (LIR*)target; |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 340 | storeValue(cUnit, rlDest, rlResult); |
| 341 | } |
| 342 | |
buzbee | 82488f5 | 2012-03-02 08:20:26 -0800 | [diff] [blame] | 343 | LIR* opCmpBranch(CompilationUnit* cUnit, ConditionCode cond, int src1, |
| 344 | int src2, LIR* target) |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 345 | { |
buzbee | 0398c42 | 2012-03-02 15:22:47 -0800 | [diff] [blame] | 346 | LIR* branch; |
| 347 | MipsOpCode sltOp; |
| 348 | MipsOpCode brOp; |
| 349 | bool cmpZero = false; |
| 350 | bool swapped = false; |
| 351 | switch(cond) { |
| 352 | case kCondEq: |
| 353 | brOp = kMipsBeq; |
| 354 | cmpZero = true; |
| 355 | break; |
| 356 | case kCondNe: |
| 357 | brOp = kMipsBne; |
| 358 | cmpZero = true; |
| 359 | break; |
| 360 | case kCondCc: |
| 361 | sltOp = kMipsSltu; |
| 362 | brOp = kMipsBnez; |
| 363 | break; |
| 364 | case kCondCs: |
| 365 | sltOp = kMipsSltu; |
| 366 | brOp = kMipsBeqz; |
| 367 | break; |
| 368 | case kCondGe: |
| 369 | sltOp = kMipsSlt; |
| 370 | brOp = kMipsBeqz; |
| 371 | break; |
| 372 | case kCondGt: |
| 373 | sltOp = kMipsSlt; |
| 374 | brOp = kMipsBnez; |
| 375 | swapped = true; |
| 376 | break; |
| 377 | case kCondLe: |
| 378 | sltOp = kMipsSlt; |
| 379 | brOp = kMipsBeqz; |
| 380 | swapped = true; |
| 381 | break; |
| 382 | case kCondLt: |
| 383 | sltOp = kMipsSlt; |
| 384 | brOp = kMipsBnez; |
| 385 | break; |
buzbee | c5159d5 | 2012-03-03 11:48:39 -0800 | [diff] [blame] | 386 | case kCondHi: // Gtu |
| 387 | sltOp = kMipsSltu; |
| 388 | brOp = kMipsBnez; |
| 389 | swapped = true; |
| 390 | break; |
buzbee | 0398c42 | 2012-03-02 15:22:47 -0800 | [diff] [blame] | 391 | default: |
buzbee | a2ebdd7 | 2012-03-04 14:57:06 -0800 | [diff] [blame^] | 392 | LOG(FATAL) << "No support for ConditionCode: " << (int) cond; |
buzbee | 0398c42 | 2012-03-02 15:22:47 -0800 | [diff] [blame] | 393 | return NULL; |
| 394 | } |
| 395 | if (cmpZero) { |
| 396 | branch = newLIR2(cUnit, brOp, src1, src2); |
buzbee | 82488f5 | 2012-03-02 08:20:26 -0800 | [diff] [blame] | 397 | } else { |
buzbee | 82488f5 | 2012-03-02 08:20:26 -0800 | [diff] [blame] | 398 | int tReg = oatAllocTemp(cUnit); |
| 399 | if (swapped) { |
| 400 | newLIR3(cUnit, sltOp, tReg, src2, src1); |
| 401 | } else { |
| 402 | newLIR3(cUnit, sltOp, tReg, src1, src2); |
| 403 | } |
| 404 | branch = newLIR1(cUnit, brOp, tReg); |
buzbee | 0398c42 | 2012-03-02 15:22:47 -0800 | [diff] [blame] | 405 | oatFreeTemp(cUnit, tReg); |
buzbee | 5de3494 | 2012-03-01 14:51:57 -0800 | [diff] [blame] | 406 | } |
buzbee | 0398c42 | 2012-03-02 15:22:47 -0800 | [diff] [blame] | 407 | branch->target = target; |
buzbee | 82488f5 | 2012-03-02 08:20:26 -0800 | [diff] [blame] | 408 | return branch; |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 409 | } |
| 410 | |
buzbee | 82488f5 | 2012-03-02 08:20:26 -0800 | [diff] [blame] | 411 | LIR* opCmpImmBranch(CompilationUnit* cUnit, ConditionCode cond, int reg, |
| 412 | int checkValue, LIR* target) |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 413 | { |
buzbee | 82488f5 | 2012-03-02 08:20:26 -0800 | [diff] [blame] | 414 | LIR* branch; |
buzbee | 5de3494 | 2012-03-01 14:51:57 -0800 | [diff] [blame] | 415 | if (checkValue != 0) { |
| 416 | // TUNING: handle s16 & kCondLt/Mi case using slti |
| 417 | int tReg = oatAllocTemp(cUnit); |
| 418 | loadConstant(cUnit, tReg, checkValue); |
buzbee | 82488f5 | 2012-03-02 08:20:26 -0800 | [diff] [blame] | 419 | branch = opCmpBranch(cUnit, cond, reg, tReg, target); |
| 420 | oatFreeTemp(cUnit, tReg); |
| 421 | return branch; |
buzbee | 5de3494 | 2012-03-01 14:51:57 -0800 | [diff] [blame] | 422 | } |
| 423 | MipsOpCode opc; |
| 424 | switch(cond) { |
| 425 | case kCondEq: opc = kMipsBeqz; break; |
| 426 | case kCondGe: opc = kMipsBgez; break; |
| 427 | case kCondGt: opc = kMipsBgtz; break; |
| 428 | case kCondLe: opc = kMipsBlez; break; |
| 429 | //case KCondMi: |
| 430 | case kCondLt: opc = kMipsBltz; break; |
| 431 | case kCondNe: opc = kMipsBnez; break; |
| 432 | default: |
buzbee | 82488f5 | 2012-03-02 08:20:26 -0800 | [diff] [blame] | 433 | // Tuning: use slti when applicable |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 434 | int tReg = oatAllocTemp(cUnit); |
buzbee | 5de3494 | 2012-03-01 14:51:57 -0800 | [diff] [blame] | 435 | loadConstant(cUnit, tReg, checkValue); |
buzbee | 82488f5 | 2012-03-02 08:20:26 -0800 | [diff] [blame] | 436 | branch = opCmpBranch(cUnit, cond, reg, tReg, target); |
| 437 | oatFreeTemp(cUnit, tReg); |
| 438 | return branch; |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 439 | } |
buzbee | 82488f5 | 2012-03-02 08:20:26 -0800 | [diff] [blame] | 440 | branch = newLIR1(cUnit, opc, reg); |
| 441 | branch->target = target; |
| 442 | return branch; |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 443 | } |
| 444 | |
buzbee | 82488f5 | 2012-03-02 08:20:26 -0800 | [diff] [blame] | 445 | LIR* opRegCopyNoInsert(CompilationUnit *cUnit, int rDest, int rSrc) |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 446 | { |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 447 | #ifdef __mips_hard_float |
buzbee | 5de3494 | 2012-03-01 14:51:57 -0800 | [diff] [blame] | 448 | if (FPREG(rDest) || FPREG(rSrc)) |
| 449 | return fpRegCopy(cUnit, rDest, rSrc); |
| 450 | #endif |
buzbee | a2ebdd7 | 2012-03-04 14:57:06 -0800 | [diff] [blame^] | 451 | LIR* res = rawLIR(cUnit, cUnit->currentDalvikOffset, kMipsMove, |
| 452 | rDest, rSrc); |
buzbee | 5de3494 | 2012-03-01 14:51:57 -0800 | [diff] [blame] | 453 | if (rDest == rSrc) { |
| 454 | res->flags.isNop = true; |
| 455 | } |
| 456 | return res; |
| 457 | } |
| 458 | |
buzbee | 82488f5 | 2012-03-02 08:20:26 -0800 | [diff] [blame] | 459 | LIR* opRegCopy(CompilationUnit *cUnit, int rDest, int rSrc) |
buzbee | 5de3494 | 2012-03-01 14:51:57 -0800 | [diff] [blame] | 460 | { |
buzbee | 82488f5 | 2012-03-02 08:20:26 -0800 | [diff] [blame] | 461 | LIR *res = opRegCopyNoInsert(cUnit, rDest, rSrc); |
buzbee | 5de3494 | 2012-03-01 14:51:57 -0800 | [diff] [blame] | 462 | oatAppendLIR(cUnit, (LIR*)res); |
| 463 | return res; |
| 464 | } |
| 465 | |
buzbee | 82488f5 | 2012-03-02 08:20:26 -0800 | [diff] [blame] | 466 | void opRegCopyWide(CompilationUnit *cUnit, int destLo, int destHi, |
buzbee | 5de3494 | 2012-03-01 14:51:57 -0800 | [diff] [blame] | 467 | int srcLo, int srcHi) |
| 468 | { |
| 469 | #ifdef __mips_hard_float |
| 470 | bool destFP = FPREG(destLo) && FPREG(destHi); |
| 471 | bool srcFP = FPREG(srcLo) && FPREG(srcHi); |
| 472 | assert(FPREG(srcLo) == FPREG(srcHi)); |
| 473 | assert(FPREG(destLo) == FPREG(destHi)); |
| 474 | if (destFP) { |
| 475 | if (srcFP) { |
buzbee | 82488f5 | 2012-03-02 08:20:26 -0800 | [diff] [blame] | 476 | opRegCopy(cUnit, S2D(destLo, destHi), S2D(srcLo, srcHi)); |
buzbee | 5de3494 | 2012-03-01 14:51:57 -0800 | [diff] [blame] | 477 | } else { |
| 478 | /* note the operands are swapped for the mtc1 instr */ |
| 479 | newLIR2(cUnit, kMipsMtc1, srcLo, destLo); |
| 480 | newLIR2(cUnit, kMipsMtc1, srcHi, destHi); |
| 481 | } |
| 482 | } else { |
| 483 | if (srcFP) { |
| 484 | newLIR2(cUnit, kMipsMfc1, destLo, srcLo); |
| 485 | newLIR2(cUnit, kMipsMfc1, destHi, srcHi); |
| 486 | } else { |
| 487 | // Handle overlap |
| 488 | if (srcHi == destLo) { |
buzbee | 82488f5 | 2012-03-02 08:20:26 -0800 | [diff] [blame] | 489 | opRegCopy(cUnit, destHi, srcHi); |
| 490 | opRegCopy(cUnit, destLo, srcLo); |
buzbee | 5de3494 | 2012-03-01 14:51:57 -0800 | [diff] [blame] | 491 | } else { |
buzbee | 82488f5 | 2012-03-02 08:20:26 -0800 | [diff] [blame] | 492 | opRegCopy(cUnit, destLo, srcLo); |
| 493 | opRegCopy(cUnit, destHi, srcHi); |
buzbee | 5de3494 | 2012-03-01 14:51:57 -0800 | [diff] [blame] | 494 | } |
| 495 | } |
| 496 | } |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 497 | #else |
buzbee | 5de3494 | 2012-03-01 14:51:57 -0800 | [diff] [blame] | 498 | // Handle overlap |
| 499 | if (srcHi == destLo) { |
buzbee | 82488f5 | 2012-03-02 08:20:26 -0800 | [diff] [blame] | 500 | opRegCopy(cUnit, destHi, srcHi); |
| 501 | opRegCopy(cUnit, destLo, srcLo); |
buzbee | 5de3494 | 2012-03-01 14:51:57 -0800 | [diff] [blame] | 502 | } else { |
buzbee | 82488f5 | 2012-03-02 08:20:26 -0800 | [diff] [blame] | 503 | opRegCopy(cUnit, destLo, srcLo); |
| 504 | opRegCopy(cUnit, destHi, srcHi); |
buzbee | 5de3494 | 2012-03-01 14:51:57 -0800 | [diff] [blame] | 505 | } |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 506 | #endif |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 507 | } |
| 508 | |
| 509 | } // namespace art |