buzbee | efc6369 | 2012-11-14 16:31:52 -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 | /* This file contains codegen for the Mips ISA */ |
| 18 | |
| 19 | #include "oat/runtime/oat_support_entrypoints.h" |
| 20 | |
| 21 | namespace art { |
| 22 | |
| 23 | void genSpecialCase(CompilationUnit* cUnit, BasicBlock* bb, MIR* mir, |
| 24 | SpecialCaseHandler specialCase) |
| 25 | { |
| 26 | // TODO |
| 27 | } |
| 28 | |
| 29 | /* |
| 30 | * The lack of pc-relative loads on Mips presents somewhat of a challenge |
| 31 | * for our PIC switch table strategy. To materialize the current location |
| 32 | * we'll do a dummy JAL and reference our tables using r_RA as the |
| 33 | * base register. Note that r_RA will be used both as the base to |
| 34 | * locate the switch table data and as the reference base for the switch |
| 35 | * target offsets stored in the table. We'll use a special pseudo-instruction |
| 36 | * to represent the jal and trigger the construction of the |
| 37 | * switch table offsets (which will happen after final assembly and all |
| 38 | * labels are fixed). |
| 39 | * |
| 40 | * The test loop will look something like: |
| 41 | * |
| 42 | * ori rEnd, r_ZERO, #tableSize ; size in bytes |
| 43 | * jal BaseLabel ; stores "return address" (BaseLabel) in r_RA |
| 44 | * nop ; opportunistically fill |
| 45 | * BaseLabel: |
| 46 | * addiu rBase, r_RA, <table> - <BaseLabel> ; table relative to BaseLabel |
| 47 | addu rEnd, rEnd, rBase ; end of table |
| 48 | * lw rVal, [rSP, vRegOff] ; Test Value |
| 49 | * loop: |
| 50 | * beq rBase, rEnd, done |
| 51 | * lw rKey, 0(rBase) |
| 52 | * addu rBase, 8 |
| 53 | * bne rVal, rKey, loop |
| 54 | * lw rDisp, -4(rBase) |
| 55 | * addu r_RA, rDisp |
| 56 | * jr r_RA |
| 57 | * done: |
| 58 | * |
| 59 | */ |
| 60 | void genSparseSwitch(CompilationUnit* cUnit, uint32_t tableOffset, |
| 61 | RegLocation rlSrc) |
| 62 | { |
buzbee | eaf09bc | 2012-11-15 14:51:41 -0800 | [diff] [blame^] | 63 | const uint16_t* table = cUnit->insns + cUnit->currentDalvikOffset + tableOffset; |
buzbee | efc6369 | 2012-11-14 16:31:52 -0800 | [diff] [blame] | 64 | if (cUnit->printMe) { |
| 65 | dumpSparseSwitchTable(table); |
| 66 | } |
| 67 | // Add the table to the list - we'll process it later |
| 68 | SwitchTable *tabRec = (SwitchTable *)oatNew(cUnit, sizeof(SwitchTable), |
| 69 | true, kAllocData); |
| 70 | tabRec->table = table; |
| 71 | tabRec->vaddr = cUnit->currentDalvikOffset; |
| 72 | int elements = table[1]; |
| 73 | tabRec->targets = (LIR* *)oatNew(cUnit, elements * sizeof(LIR*), true, |
| 74 | kAllocLIR); |
| 75 | oatInsertGrowableList(cUnit, &cUnit->switchTables, (intptr_t)tabRec); |
| 76 | |
| 77 | // The table is composed of 8-byte key/disp pairs |
| 78 | int byteSize = elements * 8; |
| 79 | |
| 80 | int sizeHi = byteSize >> 16; |
| 81 | int sizeLo = byteSize & 0xffff; |
| 82 | |
| 83 | int rEnd = oatAllocTemp(cUnit); |
| 84 | if (sizeHi) { |
| 85 | newLIR2(cUnit, kMipsLui, rEnd, sizeHi); |
| 86 | } |
| 87 | // Must prevent code motion for the curr pc pair |
| 88 | genBarrier(cUnit); // Scheduling barrier |
| 89 | newLIR0(cUnit, kMipsCurrPC); // Really a jal to .+8 |
| 90 | // Now, fill the branch delay slot |
| 91 | if (sizeHi) { |
| 92 | newLIR3(cUnit, kMipsOri, rEnd, rEnd, sizeLo); |
| 93 | } else { |
| 94 | newLIR3(cUnit, kMipsOri, rEnd, r_ZERO, sizeLo); |
| 95 | } |
| 96 | genBarrier(cUnit); // Scheduling barrier |
| 97 | |
| 98 | // Construct BaseLabel and set up table base register |
| 99 | LIR* baseLabel = newLIR0(cUnit, kPseudoTargetLabel); |
| 100 | // Remember base label so offsets can be computed later |
| 101 | tabRec->anchor = baseLabel; |
| 102 | int rBase = oatAllocTemp(cUnit); |
| 103 | newLIR4(cUnit, kMipsDelta, rBase, 0, (intptr_t)baseLabel, (intptr_t)tabRec); |
| 104 | opRegRegReg(cUnit, kOpAdd, rEnd, rEnd, rBase); |
| 105 | |
| 106 | // Grab switch test value |
| 107 | rlSrc = loadValue(cUnit, rlSrc, kCoreReg); |
| 108 | |
| 109 | // Test loop |
| 110 | int rKey = oatAllocTemp(cUnit); |
| 111 | LIR* loopLabel = newLIR0(cUnit, kPseudoTargetLabel); |
| 112 | LIR* exitBranch = opCmpBranch(cUnit , kCondEq, rBase, rEnd, NULL); |
| 113 | loadWordDisp(cUnit, rBase, 0, rKey); |
| 114 | opRegImm(cUnit, kOpAdd, rBase, 8); |
| 115 | opCmpBranch(cUnit, kCondNe, rlSrc.lowReg, rKey, loopLabel); |
| 116 | int rDisp = oatAllocTemp(cUnit); |
| 117 | loadWordDisp(cUnit, rBase, -4, rDisp); |
| 118 | opRegRegReg(cUnit, kOpAdd, r_RA, r_RA, rDisp); |
| 119 | opReg(cUnit, kOpBx, r_RA); |
| 120 | |
| 121 | // Loop exit |
| 122 | LIR* exitLabel = newLIR0(cUnit, kPseudoTargetLabel); |
| 123 | exitBranch->target = exitLabel; |
| 124 | } |
| 125 | |
| 126 | /* |
| 127 | * Code pattern will look something like: |
| 128 | * |
| 129 | * lw rVal |
| 130 | * jal BaseLabel ; stores "return address" (BaseLabel) in r_RA |
| 131 | * nop ; opportunistically fill |
| 132 | * [subiu rVal, bias] ; Remove bias if lowVal != 0 |
| 133 | * bound check -> done |
| 134 | * lw rDisp, [r_RA, rVal] |
| 135 | * addu r_RA, rDisp |
| 136 | * jr r_RA |
| 137 | * done: |
| 138 | */ |
| 139 | void genPackedSwitch(CompilationUnit* cUnit, uint32_t tableOffset, |
| 140 | RegLocation rlSrc) |
| 141 | { |
buzbee | eaf09bc | 2012-11-15 14:51:41 -0800 | [diff] [blame^] | 142 | const uint16_t* table = cUnit->insns + cUnit->currentDalvikOffset + tableOffset; |
buzbee | efc6369 | 2012-11-14 16:31:52 -0800 | [diff] [blame] | 143 | if (cUnit->printMe) { |
| 144 | dumpPackedSwitchTable(table); |
| 145 | } |
| 146 | // Add the table to the list - we'll process it later |
| 147 | SwitchTable *tabRec = (SwitchTable *)oatNew(cUnit, sizeof(SwitchTable), |
| 148 | true, kAllocData); |
| 149 | tabRec->table = table; |
| 150 | tabRec->vaddr = cUnit->currentDalvikOffset; |
| 151 | int size = table[1]; |
| 152 | tabRec->targets = (LIR* *)oatNew(cUnit, size * sizeof(LIR*), true, |
| 153 | kAllocLIR); |
| 154 | oatInsertGrowableList(cUnit, &cUnit->switchTables, (intptr_t)tabRec); |
| 155 | |
| 156 | // Get the switch value |
| 157 | rlSrc = loadValue(cUnit, rlSrc, kCoreReg); |
| 158 | |
| 159 | // Prepare the bias. If too big, handle 1st stage here |
| 160 | int lowKey = s4FromSwitchData(&table[2]); |
| 161 | bool largeBias = false; |
| 162 | int rKey; |
| 163 | if (lowKey == 0) { |
| 164 | rKey = rlSrc.lowReg; |
| 165 | } else if ((lowKey & 0xffff) != lowKey) { |
| 166 | rKey = oatAllocTemp(cUnit); |
| 167 | loadConstant(cUnit, rKey, lowKey); |
| 168 | largeBias = true; |
| 169 | } else { |
| 170 | rKey = oatAllocTemp(cUnit); |
| 171 | } |
| 172 | |
| 173 | // Must prevent code motion for the curr pc pair |
| 174 | genBarrier(cUnit); |
| 175 | newLIR0(cUnit, kMipsCurrPC); // Really a jal to .+8 |
| 176 | // Now, fill the branch delay slot with bias strip |
| 177 | if (lowKey == 0) { |
| 178 | newLIR0(cUnit, kMipsNop); |
| 179 | } else { |
| 180 | if (largeBias) { |
| 181 | opRegRegReg(cUnit, kOpSub, rKey, rlSrc.lowReg, rKey); |
| 182 | } else { |
| 183 | opRegRegImm(cUnit, kOpSub, rKey, rlSrc.lowReg, lowKey); |
| 184 | } |
| 185 | } |
| 186 | genBarrier(cUnit); // Scheduling barrier |
| 187 | |
| 188 | // Construct BaseLabel and set up table base register |
| 189 | LIR* baseLabel = newLIR0(cUnit, kPseudoTargetLabel); |
| 190 | // Remember base label so offsets can be computed later |
| 191 | tabRec->anchor = baseLabel; |
| 192 | |
| 193 | // Bounds check - if < 0 or >= size continue following switch |
| 194 | LIR* branchOver = opCmpImmBranch(cUnit, kCondHi, rKey, size-1, NULL); |
| 195 | |
| 196 | // Materialize the table base pointer |
| 197 | int rBase = oatAllocTemp(cUnit); |
| 198 | newLIR4(cUnit, kMipsDelta, rBase, 0, (intptr_t)baseLabel, (intptr_t)tabRec); |
| 199 | |
| 200 | // Load the displacement from the switch table |
| 201 | int rDisp = oatAllocTemp(cUnit); |
| 202 | loadBaseIndexed(cUnit, rBase, rKey, rDisp, 2, kWord); |
| 203 | |
| 204 | // Add to r_AP and go |
| 205 | opRegRegReg(cUnit, kOpAdd, r_RA, r_RA, rDisp); |
| 206 | opReg(cUnit, kOpBx, r_RA); |
| 207 | |
| 208 | /* branchOver target here */ |
| 209 | LIR* target = newLIR0(cUnit, kPseudoTargetLabel); |
| 210 | branchOver->target = (LIR*)target; |
| 211 | } |
| 212 | |
| 213 | /* |
| 214 | * Array data table format: |
| 215 | * ushort ident = 0x0300 magic value |
| 216 | * ushort width width of each element in the table |
| 217 | * uint size number of elements in the table |
| 218 | * ubyte data[size*width] table of data values (may contain a single-byte |
| 219 | * padding at the end) |
| 220 | * |
| 221 | * Total size is 4+(width * size + 1)/2 16-bit code units. |
| 222 | */ |
| 223 | void genFillArrayData(CompilationUnit* cUnit, uint32_t tableOffset, |
| 224 | RegLocation rlSrc) |
| 225 | { |
buzbee | eaf09bc | 2012-11-15 14:51:41 -0800 | [diff] [blame^] | 226 | const uint16_t* table = cUnit->insns + cUnit->currentDalvikOffset + tableOffset; |
buzbee | efc6369 | 2012-11-14 16:31:52 -0800 | [diff] [blame] | 227 | // Add the table to the list - we'll process it later |
| 228 | FillArrayData *tabRec = (FillArrayData *) |
| 229 | oatNew(cUnit, sizeof(FillArrayData), true, kAllocData); |
| 230 | tabRec->table = table; |
| 231 | tabRec->vaddr = cUnit->currentDalvikOffset; |
buzbee | eaf09bc | 2012-11-15 14:51:41 -0800 | [diff] [blame^] | 232 | uint16_t width = tabRec->table[1]; |
| 233 | uint32_t size = tabRec->table[2] | ((static_cast<uint32_t>(tabRec->table[3])) << 16); |
buzbee | efc6369 | 2012-11-14 16:31:52 -0800 | [diff] [blame] | 234 | tabRec->size = (size * width) + 8; |
| 235 | |
| 236 | oatInsertGrowableList(cUnit, &cUnit->fillArrayData, (intptr_t)tabRec); |
| 237 | |
| 238 | // Making a call - use explicit registers |
| 239 | oatFlushAllRegs(cUnit); /* Everything to home location */ |
| 240 | oatLockCallTemps(cUnit); |
| 241 | loadValueDirectFixed(cUnit, rlSrc, rMIPS_ARG0); |
| 242 | |
| 243 | // Must prevent code motion for the curr pc pair |
| 244 | genBarrier(cUnit); |
| 245 | newLIR0(cUnit, kMipsCurrPC); // Really a jal to .+8 |
| 246 | // Now, fill the branch delay slot with the helper load |
| 247 | int rTgt = loadHelper(cUnit, ENTRYPOINT_OFFSET(pHandleFillArrayDataFromCode)); |
| 248 | genBarrier(cUnit); // Scheduling barrier |
| 249 | |
| 250 | // Construct BaseLabel and set up table base register |
| 251 | LIR* baseLabel = newLIR0(cUnit, kPseudoTargetLabel); |
| 252 | |
| 253 | // Materialize a pointer to the fill data image |
| 254 | newLIR4(cUnit, kMipsDelta, rMIPS_ARG1, 0, (intptr_t)baseLabel, (intptr_t)tabRec); |
| 255 | |
| 256 | // And go... |
| 257 | oatClobberCalleeSave(cUnit); |
| 258 | LIR* callInst = opReg(cUnit, kOpBlx, rTgt); // ( array*, fill_data* ) |
| 259 | markSafepointPC(cUnit, callInst); |
| 260 | } |
| 261 | |
| 262 | /* |
| 263 | * TODO: implement fast path to short-circuit thin-lock case |
| 264 | */ |
| 265 | void genMonitorEnter(CompilationUnit* cUnit, int optFlags, RegLocation rlSrc) |
| 266 | { |
| 267 | oatFlushAllRegs(cUnit); |
| 268 | loadValueDirectFixed(cUnit, rlSrc, rMIPS_ARG0); // Get obj |
| 269 | oatLockCallTemps(cUnit); // Prepare for explicit register usage |
| 270 | genNullCheck(cUnit, rlSrc.sRegLow, rMIPS_ARG0, optFlags); |
| 271 | // Go expensive route - artLockObjectFromCode(self, obj); |
| 272 | int rTgt = loadHelper(cUnit, ENTRYPOINT_OFFSET(pLockObjectFromCode)); |
| 273 | oatClobberCalleeSave(cUnit); |
| 274 | LIR* callInst = opReg(cUnit, kOpBlx, rTgt); |
| 275 | markSafepointPC(cUnit, callInst); |
| 276 | } |
| 277 | |
| 278 | /* |
| 279 | * TODO: implement fast path to short-circuit thin-lock case |
| 280 | */ |
| 281 | void genMonitorExit(CompilationUnit* cUnit, int optFlags, RegLocation rlSrc) |
| 282 | { |
| 283 | oatFlushAllRegs(cUnit); |
| 284 | loadValueDirectFixed(cUnit, rlSrc, rMIPS_ARG0); // Get obj |
| 285 | oatLockCallTemps(cUnit); // Prepare for explicit register usage |
| 286 | genNullCheck(cUnit, rlSrc.sRegLow, rMIPS_ARG0, optFlags); |
| 287 | // Go expensive route - UnlockObjectFromCode(obj); |
| 288 | int rTgt = loadHelper(cUnit, ENTRYPOINT_OFFSET(pUnlockObjectFromCode)); |
| 289 | oatClobberCalleeSave(cUnit); |
| 290 | LIR* callInst = opReg(cUnit, kOpBlx, rTgt); |
| 291 | markSafepointPC(cUnit, callInst); |
| 292 | } |
| 293 | |
| 294 | /* |
| 295 | * Mark garbage collection card. Skip if the value we're storing is null. |
| 296 | */ |
| 297 | void markGCCard(CompilationUnit* cUnit, int valReg, int tgtAddrReg) |
| 298 | { |
| 299 | int regCardBase = oatAllocTemp(cUnit); |
| 300 | int regCardNo = oatAllocTemp(cUnit); |
| 301 | LIR* branchOver = opCmpImmBranch(cUnit, kCondEq, valReg, 0, NULL); |
| 302 | loadWordDisp(cUnit, rMIPS_SELF, Thread::CardTableOffset().Int32Value(), regCardBase); |
| 303 | opRegRegImm(cUnit, kOpLsr, regCardNo, tgtAddrReg, CardTable::kCardShift); |
| 304 | storeBaseIndexed(cUnit, regCardBase, regCardNo, regCardBase, 0, |
| 305 | kUnsignedByte); |
| 306 | LIR* target = newLIR0(cUnit, kPseudoTargetLabel); |
| 307 | branchOver->target = (LIR*)target; |
| 308 | oatFreeTemp(cUnit, regCardBase); |
| 309 | oatFreeTemp(cUnit, regCardNo); |
| 310 | } |
| 311 | void genEntrySequence(CompilationUnit* cUnit, RegLocation* argLocs, |
| 312 | RegLocation rlMethod) |
| 313 | { |
| 314 | int spillCount = cUnit->numCoreSpills + cUnit->numFPSpills; |
| 315 | /* |
| 316 | * On entry, rMIPS_ARG0, rMIPS_ARG1, rMIPS_ARG2 & rMIPS_ARG3 are live. Let the register |
| 317 | * allocation mechanism know so it doesn't try to use any of them when |
| 318 | * expanding the frame or flushing. This leaves the utility |
| 319 | * code with a single temp: r12. This should be enough. |
| 320 | */ |
| 321 | oatLockTemp(cUnit, rMIPS_ARG0); |
| 322 | oatLockTemp(cUnit, rMIPS_ARG1); |
| 323 | oatLockTemp(cUnit, rMIPS_ARG2); |
| 324 | oatLockTemp(cUnit, rMIPS_ARG3); |
| 325 | |
| 326 | /* |
| 327 | * We can safely skip the stack overflow check if we're |
| 328 | * a leaf *and* our frame size < fudge factor. |
| 329 | */ |
| 330 | bool skipOverflowCheck = ((cUnit->attrs & METHOD_IS_LEAF) && |
| 331 | ((size_t)cUnit->frameSize < Thread::kStackOverflowReservedBytes)); |
| 332 | newLIR0(cUnit, kPseudoMethodEntry); |
| 333 | int checkReg = oatAllocTemp(cUnit); |
| 334 | int newSP = oatAllocTemp(cUnit); |
| 335 | if (!skipOverflowCheck) { |
| 336 | /* Load stack limit */ |
| 337 | loadWordDisp(cUnit, rMIPS_SELF, Thread::StackEndOffset().Int32Value(), checkReg); |
| 338 | } |
| 339 | /* Spill core callee saves */ |
| 340 | spillCoreRegs(cUnit); |
| 341 | /* NOTE: promotion of FP regs currently unsupported, thus no FP spill */ |
| 342 | DCHECK_EQ(cUnit->numFPSpills, 0); |
| 343 | if (!skipOverflowCheck) { |
| 344 | opRegRegImm(cUnit, kOpSub, newSP, rMIPS_SP, cUnit->frameSize - (spillCount * 4)); |
| 345 | genRegRegCheck(cUnit, kCondCc, newSP, checkReg, kThrowStackOverflow); |
| 346 | opRegCopy(cUnit, rMIPS_SP, newSP); // Establish stack |
| 347 | } else { |
| 348 | opRegImm(cUnit, kOpSub, rMIPS_SP, cUnit->frameSize - (spillCount * 4)); |
| 349 | } |
| 350 | |
| 351 | flushIns(cUnit, argLocs, rlMethod); |
| 352 | |
| 353 | oatFreeTemp(cUnit, rMIPS_ARG0); |
| 354 | oatFreeTemp(cUnit, rMIPS_ARG1); |
| 355 | oatFreeTemp(cUnit, rMIPS_ARG2); |
| 356 | oatFreeTemp(cUnit, rMIPS_ARG3); |
| 357 | } |
| 358 | |
| 359 | void genExitSequence(CompilationUnit* cUnit) |
| 360 | { |
| 361 | /* |
| 362 | * In the exit path, rMIPS_RET0/rMIPS_RET1 are live - make sure they aren't |
| 363 | * allocated by the register utilities as temps. |
| 364 | */ |
| 365 | oatLockTemp(cUnit, rMIPS_RET0); |
| 366 | oatLockTemp(cUnit, rMIPS_RET1); |
| 367 | |
| 368 | newLIR0(cUnit, kPseudoMethodExit); |
| 369 | unSpillCoreRegs(cUnit); |
| 370 | opReg(cUnit, kOpBx, r_RA); |
| 371 | } |
| 372 | |
| 373 | } // namespace art |