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 | */ |
buzbee | 408ad16 | 2012-06-06 16:45:18 -0700 | [diff] [blame] | 66 | void genSparseSwitch(CompilationUnit* cUnit, uint32_t tableOffset, |
| 67 | RegLocation rlSrc, LIR* labelList) |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 68 | { |
buzbee | 408ad16 | 2012-06-06 16:45:18 -0700 | [diff] [blame] | 69 | const u2* table = cUnit->insns + cUnit->currentDalvikOffset + tableOffset; |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 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; |
buzbee | 408ad16 | 2012-06-06 16:45:18 -0700 | [diff] [blame] | 77 | tabRec->vaddr = cUnit->currentDalvikOffset; |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 78 | int elements = table[1]; |
| 79 | tabRec->targets = (LIR* *)oatNew(cUnit, elements * sizeof(LIR*), true, |
| 80 | kAllocLIR); |
| 81 | oatInsertGrowableList(cUnit, &cUnit->switchTables, (intptr_t)tabRec); |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 82 | |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 83 | // The table is composed of 8-byte key/disp pairs |
| 84 | int byteSize = elements * 8; |
buzbee | c5159d5 | 2012-03-03 11:48:39 -0800 | [diff] [blame] | 85 | |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 86 | int sizeHi = byteSize >> 16; |
| 87 | int sizeLo = byteSize & 0xffff; |
buzbee | c5159d5 | 2012-03-03 11:48:39 -0800 | [diff] [blame] | 88 | |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 89 | int rEnd = oatAllocTemp(cUnit); |
| 90 | if (sizeHi) { |
| 91 | newLIR2(cUnit, kMipsLui, rEnd, sizeHi); |
| 92 | } |
| 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 |
buzbee | c5159d5 | 2012-03-03 11:48:39 -0800 | [diff] [blame] | 103 | |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 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); |
buzbee | c5159d5 | 2012-03-03 11:48:39 -0800 | [diff] [blame] | 111 | |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 112 | // Grab switch test value |
| 113 | rlSrc = loadValue(cUnit, rlSrc, kCoreReg); |
buzbee | c5159d5 | 2012-03-03 11:48:39 -0800 | [diff] [blame] | 114 | |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 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); |
buzbee | c5159d5 | 2012-03-03 11:48:39 -0800 | [diff] [blame] | 126 | |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 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 | 408ad16 | 2012-06-06 16:45:18 -0700 | [diff] [blame] | 145 | void genPackedSwitch(CompilationUnit* cUnit, uint32_t tableOffset, |
| 146 | RegLocation rlSrc) |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 147 | { |
buzbee | 408ad16 | 2012-06-06 16:45:18 -0700 | [diff] [blame] | 148 | const u2* table = cUnit->insns + cUnit->currentDalvikOffset + tableOffset; |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 149 | if (cUnit->printMe) { |
| 150 | dumpPackedSwitchTable(table); |
| 151 | } |
| 152 | // Add the table to the list - we'll process it later |
| 153 | SwitchTable *tabRec = (SwitchTable *)oatNew(cUnit, sizeof(SwitchTable), |
| 154 | true, kAllocData); |
| 155 | tabRec->table = table; |
buzbee | 408ad16 | 2012-06-06 16:45:18 -0700 | [diff] [blame] | 156 | tabRec->vaddr = cUnit->currentDalvikOffset; |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 157 | int size = table[1]; |
| 158 | tabRec->targets = (LIR* *)oatNew(cUnit, size * sizeof(LIR*), true, |
| 159 | kAllocLIR); |
| 160 | oatInsertGrowableList(cUnit, &cUnit->switchTables, (intptr_t)tabRec); |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 161 | |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 162 | // Get the switch value |
| 163 | rlSrc = loadValue(cUnit, rlSrc, kCoreReg); |
buzbee | c5159d5 | 2012-03-03 11:48:39 -0800 | [diff] [blame] | 164 | |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 165 | // Prepare the bias. If too big, handle 1st stage here |
| 166 | int lowKey = s4FromSwitchData(&table[2]); |
| 167 | bool largeBias = false; |
| 168 | int rKey; |
| 169 | if (lowKey == 0) { |
| 170 | rKey = rlSrc.lowReg; |
| 171 | } else if ((lowKey & 0xffff) != lowKey) { |
| 172 | rKey = oatAllocTemp(cUnit); |
| 173 | loadConstant(cUnit, rKey, lowKey); |
| 174 | largeBias = true; |
| 175 | } else { |
| 176 | rKey = oatAllocTemp(cUnit); |
| 177 | } |
| 178 | |
| 179 | // Must prevent code motion for the curr pc pair |
| 180 | genBarrier(cUnit); |
| 181 | newLIR0(cUnit, kMipsCurrPC); // Really a jal to .+8 |
| 182 | // Now, fill the branch delay slot with bias strip |
| 183 | if (lowKey == 0) { |
| 184 | newLIR0(cUnit, kMipsNop); |
| 185 | } else { |
| 186 | if (largeBias) { |
| 187 | opRegRegReg(cUnit, kOpSub, rKey, rlSrc.lowReg, rKey); |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 188 | } else { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 189 | opRegRegImm(cUnit, kOpSub, rKey, rlSrc.lowReg, lowKey); |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 190 | } |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 191 | } |
| 192 | genBarrier(cUnit); // Scheduling barrier |
buzbee | c5159d5 | 2012-03-03 11:48:39 -0800 | [diff] [blame] | 193 | |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 194 | // Construct BaseLabel and set up table base register |
| 195 | LIR* baseLabel = newLIR0(cUnit, kPseudoTargetLabel); |
| 196 | // Remember base label so offsets can be computed later |
| 197 | tabRec->anchor = baseLabel; |
buzbee | c5159d5 | 2012-03-03 11:48:39 -0800 | [diff] [blame] | 198 | |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 199 | // Bounds check - if < 0 or >= size continue following switch |
| 200 | LIR* branchOver = opCmpImmBranch(cUnit, kCondHi, rKey, size-1, NULL); |
buzbee | c5159d5 | 2012-03-03 11:48:39 -0800 | [diff] [blame] | 201 | |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 202 | // Materialize the table base pointer |
| 203 | int rBase = oatAllocTemp(cUnit); |
| 204 | newLIR4(cUnit, kMipsDelta, rBase, 0, (intptr_t)baseLabel, (intptr_t)tabRec); |
buzbee | c5159d5 | 2012-03-03 11:48:39 -0800 | [diff] [blame] | 205 | |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 206 | // Load the displacement from the switch table |
| 207 | int rDisp = oatAllocTemp(cUnit); |
| 208 | loadBaseIndexed(cUnit, rBase, rKey, rDisp, 2, kWord); |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 209 | |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 210 | // Add to r_AP and go |
| 211 | opRegRegReg(cUnit, kOpAdd, r_RA, r_RA, rDisp); |
| 212 | opReg(cUnit, kOpBx, r_RA); |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 213 | |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 214 | /* branchOver target here */ |
| 215 | LIR* target = newLIR0(cUnit, kPseudoTargetLabel); |
| 216 | branchOver->target = (LIR*)target; |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 217 | } |
| 218 | |
| 219 | /* |
| 220 | * Array data table format: |
| 221 | * ushort ident = 0x0300 magic value |
| 222 | * ushort width width of each element in the table |
| 223 | * uint size number of elements in the table |
| 224 | * ubyte data[size*width] table of data values (may contain a single-byte |
| 225 | * padding at the end) |
| 226 | * |
| 227 | * Total size is 4+(width * size + 1)/2 16-bit code units. |
| 228 | */ |
buzbee | 408ad16 | 2012-06-06 16:45:18 -0700 | [diff] [blame] | 229 | void genFillArrayData(CompilationUnit* cUnit, uint32_t tableOffset, |
| 230 | RegLocation rlSrc) |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 231 | { |
buzbee | 408ad16 | 2012-06-06 16:45:18 -0700 | [diff] [blame] | 232 | const u2* table = cUnit->insns + cUnit->currentDalvikOffset + tableOffset; |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 233 | // Add the table to the list - we'll process it later |
| 234 | FillArrayData *tabRec = (FillArrayData *) |
| 235 | oatNew(cUnit, sizeof(FillArrayData), true, kAllocData); |
| 236 | tabRec->table = table; |
buzbee | 408ad16 | 2012-06-06 16:45:18 -0700 | [diff] [blame] | 237 | tabRec->vaddr = cUnit->currentDalvikOffset; |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 238 | u2 width = tabRec->table[1]; |
| 239 | u4 size = tabRec->table[2] | (((u4)tabRec->table[3]) << 16); |
| 240 | tabRec->size = (size * width) + 8; |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 241 | |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 242 | oatInsertGrowableList(cUnit, &cUnit->fillArrayData, (intptr_t)tabRec); |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 243 | |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 244 | // Making a call - use explicit registers |
| 245 | oatFlushAllRegs(cUnit); /* Everything to home location */ |
| 246 | oatLockCallTemps(cUnit); |
| 247 | loadValueDirectFixed(cUnit, rlSrc, rARG0); |
buzbee | c5159d5 | 2012-03-03 11:48:39 -0800 | [diff] [blame] | 248 | |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 249 | // Must prevent code motion for the curr pc pair |
| 250 | genBarrier(cUnit); |
| 251 | newLIR0(cUnit, kMipsCurrPC); // Really a jal to .+8 |
| 252 | // Now, fill the branch delay slot with the helper load |
| 253 | int rTgt = loadHelper(cUnit, ENTRYPOINT_OFFSET(pHandleFillArrayDataFromCode)); |
| 254 | genBarrier(cUnit); // Scheduling barrier |
buzbee | c5159d5 | 2012-03-03 11:48:39 -0800 | [diff] [blame] | 255 | |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 256 | // Construct BaseLabel and set up table base register |
| 257 | LIR* baseLabel = newLIR0(cUnit, kPseudoTargetLabel); |
buzbee | c5159d5 | 2012-03-03 11:48:39 -0800 | [diff] [blame] | 258 | |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 259 | // Materialize a pointer to the fill data image |
| 260 | newLIR4(cUnit, kMipsDelta, rARG1, 0, (intptr_t)baseLabel, (intptr_t)tabRec); |
buzbee | c5159d5 | 2012-03-03 11:48:39 -0800 | [diff] [blame] | 261 | |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 262 | // And go... |
| 263 | oatClobberCalleeSave(cUnit); |
| 264 | opReg(cUnit, kOpBlx, rTgt); // ( array*, fill_data* ) |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 265 | } |
| 266 | |
buzbee | 71ac994 | 2012-03-01 17:23:10 -0800 | [diff] [blame] | 267 | void genNegFloat(CompilationUnit *cUnit, RegLocation rlDest, RegLocation rlSrc) |
| 268 | { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 269 | RegLocation rlResult; |
| 270 | rlSrc = loadValue(cUnit, rlSrc, kCoreReg); |
| 271 | rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true); |
| 272 | opRegRegImm(cUnit, kOpAdd, rlResult.lowReg, rlSrc.lowReg, 0x80000000); |
| 273 | storeValue(cUnit, rlDest, rlResult); |
buzbee | 71ac994 | 2012-03-01 17:23:10 -0800 | [diff] [blame] | 274 | } |
| 275 | |
| 276 | void genNegDouble(CompilationUnit *cUnit, RegLocation rlDest, RegLocation rlSrc) |
| 277 | { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 278 | RegLocation rlResult; |
| 279 | rlSrc = loadValueWide(cUnit, rlSrc, kCoreReg); |
| 280 | rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true); |
| 281 | opRegRegImm(cUnit, kOpAdd, rlResult.highReg, rlSrc.highReg, 0x80000000); |
| 282 | opRegCopy(cUnit, rlResult.lowReg, rlSrc.lowReg); |
| 283 | storeValueWide(cUnit, rlDest, rlResult); |
buzbee | 71ac994 | 2012-03-01 17:23:10 -0800 | [diff] [blame] | 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 | */ |
buzbee | 408ad16 | 2012-06-06 16:45:18 -0700 | [diff] [blame] | 289 | void genMonitorEnter(CompilationUnit* cUnit, int optFlags, RegLocation rlSrc) |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 290 | { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 291 | oatFlushAllRegs(cUnit); |
| 292 | loadValueDirectFixed(cUnit, rlSrc, rARG0); // Get obj |
| 293 | oatLockCallTemps(cUnit); // Prepare for explicit register usage |
buzbee | 408ad16 | 2012-06-06 16:45:18 -0700 | [diff] [blame] | 294 | genNullCheck(cUnit, rlSrc.sRegLow, rARG0, optFlags); |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 295 | // Go expensive route - artLockObjectFromCode(self, obj); |
| 296 | int rTgt = loadHelper(cUnit, ENTRYPOINT_OFFSET(pLockObjectFromCode)); |
| 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 | 408ad16 | 2012-06-06 16:45:18 -0700 | [diff] [blame] | 304 | void genMonitorExit(CompilationUnit* cUnit, int optFlags, RegLocation rlSrc) |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 305 | { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 306 | oatFlushAllRegs(cUnit); |
| 307 | loadValueDirectFixed(cUnit, rlSrc, rARG0); // Get obj |
| 308 | oatLockCallTemps(cUnit); // Prepare for explicit register usage |
buzbee | 408ad16 | 2012-06-06 16:45:18 -0700 | [diff] [blame] | 309 | genNullCheck(cUnit, rlSrc.sRegLow, rARG0, optFlags); |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 310 | // Go expensive route - UnlockObjectFromCode(obj); |
| 311 | int rTgt = loadHelper(cUnit, ENTRYPOINT_OFFSET(pUnlockObjectFromCode)); |
| 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 | */ |
buzbee | 408ad16 | 2012-06-06 16:45:18 -0700 | [diff] [blame] | 332 | void genCmpLong(CompilationUnit* cUnit, RegLocation rlDest, |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 333 | RegLocation rlSrc1, RegLocation rlSrc2) |
buzbee | 5de3494 | 2012-03-01 14:51:57 -0800 | [diff] [blame] | 334 | { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 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); |
| 343 | LIR* branch = opCmpImmBranch(cUnit, kCondNe, rlResult.lowReg, 0, NULL); |
| 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); |
| 350 | branch->target = (LIR*)target; |
| 351 | storeValue(cUnit, rlDest, rlResult); |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 352 | } |
| 353 | |
buzbee | 82488f5 | 2012-03-02 08:20:26 -0800 | [diff] [blame] | 354 | LIR* opCmpBranch(CompilationUnit* cUnit, ConditionCode cond, int src1, |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 355 | int src2, LIR* target) |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 356 | { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 357 | LIR* branch; |
| 358 | MipsOpCode sltOp; |
| 359 | MipsOpCode brOp; |
| 360 | bool cmpZero = false; |
| 361 | bool swapped = false; |
| 362 | switch (cond) { |
| 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; |
| 397 | case kCondHi: // Gtu |
| 398 | sltOp = kMipsSltu; |
| 399 | brOp = kMipsBnez; |
| 400 | swapped = true; |
| 401 | break; |
| 402 | default: |
| 403 | LOG(FATAL) << "No support for ConditionCode: " << (int) cond; |
| 404 | return NULL; |
| 405 | } |
| 406 | if (cmpZero) { |
| 407 | branch = newLIR2(cUnit, brOp, src1, src2); |
| 408 | } else { |
| 409 | int tReg = oatAllocTemp(cUnit); |
| 410 | if (swapped) { |
| 411 | newLIR3(cUnit, sltOp, tReg, src2, src1); |
buzbee | 82488f5 | 2012-03-02 08:20:26 -0800 | [diff] [blame] | 412 | } else { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 413 | newLIR3(cUnit, sltOp, tReg, src1, src2); |
buzbee | 5de3494 | 2012-03-01 14:51:57 -0800 | [diff] [blame] | 414 | } |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 415 | branch = newLIR1(cUnit, brOp, tReg); |
| 416 | oatFreeTemp(cUnit, tReg); |
| 417 | } |
| 418 | branch->target = target; |
| 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, |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 423 | int checkValue, LIR* target) |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 424 | { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 425 | LIR* branch; |
| 426 | if (checkValue != 0) { |
| 427 | // TUNING: handle s16 & kCondLt/Mi case using slti |
| 428 | int tReg = oatAllocTemp(cUnit); |
| 429 | loadConstant(cUnit, tReg, checkValue); |
| 430 | branch = opCmpBranch(cUnit, cond, reg, tReg, target); |
| 431 | oatFreeTemp(cUnit, tReg); |
buzbee | 82488f5 | 2012-03-02 08:20:26 -0800 | [diff] [blame] | 432 | return branch; |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 433 | } |
| 434 | MipsOpCode opc; |
| 435 | switch (cond) { |
| 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: |
| 444 | // Tuning: use slti when applicable |
| 445 | int tReg = oatAllocTemp(cUnit); |
| 446 | loadConstant(cUnit, tReg, checkValue); |
| 447 | branch = opCmpBranch(cUnit, cond, reg, tReg, target); |
| 448 | oatFreeTemp(cUnit, tReg); |
| 449 | return branch; |
| 450 | } |
| 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 |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 459 | if (FPREG(rDest) || FPREG(rSrc)) |
| 460 | return fpRegCopy(cUnit, rDest, rSrc); |
buzbee | 5de3494 | 2012-03-01 14:51:57 -0800 | [diff] [blame] | 461 | #endif |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 462 | LIR* res = rawLIR(cUnit, cUnit->currentDalvikOffset, kMipsMove, |
| 463 | rDest, rSrc); |
| 464 | if (!(cUnit->disableOpt & (1 << kSafeOptimizations)) && rDest == rSrc) { |
| 465 | res->flags.isNop = true; |
| 466 | } |
| 467 | return res; |
buzbee | 5de3494 | 2012-03-01 14:51:57 -0800 | [diff] [blame] | 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 | { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 472 | LIR *res = opRegCopyNoInsert(cUnit, rDest, rSrc); |
| 473 | oatAppendLIR(cUnit, (LIR*)res); |
| 474 | return res; |
buzbee | 5de3494 | 2012-03-01 14:51:57 -0800 | [diff] [blame] | 475 | } |
| 476 | |
buzbee | 82488f5 | 2012-03-02 08:20:26 -0800 | [diff] [blame] | 477 | void opRegCopyWide(CompilationUnit *cUnit, int destLo, int destHi, |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 478 | int srcLo, int srcHi) |
buzbee | 5de3494 | 2012-03-01 14:51:57 -0800 | [diff] [blame] | 479 | { |
| 480 | #ifdef __mips_hard_float |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 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) { |
| 487 | opRegCopy(cUnit, S2D(destLo, destHi), S2D(srcLo, srcHi)); |
buzbee | 5de3494 | 2012-03-01 14:51:57 -0800 | [diff] [blame] | 488 | } else { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 489 | /* note the operands are swapped for the mtc1 instr */ |
| 490 | newLIR2(cUnit, kMipsMtc1, srcLo, destLo); |
| 491 | newLIR2(cUnit, kMipsMtc1, srcHi, destHi); |
buzbee | 5de3494 | 2012-03-01 14:51:57 -0800 | [diff] [blame] | 492 | } |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 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) { |
| 500 | opRegCopy(cUnit, destHi, srcHi); |
| 501 | opRegCopy(cUnit, destLo, srcLo); |
| 502 | } else { |
| 503 | opRegCopy(cUnit, destLo, srcLo); |
| 504 | opRegCopy(cUnit, destHi, srcHi); |
| 505 | } |
| 506 | } |
| 507 | } |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 508 | #else |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 509 | // Handle overlap |
| 510 | if (srcHi == destLo) { |
| 511 | opRegCopy(cUnit, destHi, srcHi); |
| 512 | opRegCopy(cUnit, destLo, srcLo); |
| 513 | } else { |
| 514 | opRegCopy(cUnit, destLo, srcLo); |
| 515 | opRegCopy(cUnit, destHi, srcHi); |
| 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 |