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