buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2011 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 mips-specific codegen factory support. |
| 19 | * It is included 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 | c5159d5 | 2012-03-03 11:48:39 -0800 | [diff] [blame] | 29 | bool genAddLong(CompilationUnit* cUnit, MIR* mir, RegLocation rlDest, |
| 30 | RegLocation rlSrc1, RegLocation rlSrc2) |
| 31 | { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame^] | 32 | rlSrc1 = loadValueWide(cUnit, rlSrc1, kCoreReg); |
| 33 | rlSrc2 = loadValueWide(cUnit, rlSrc2, kCoreReg); |
| 34 | RegLocation rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true); |
| 35 | /* |
| 36 | * [v1 v0] = [a1 a0] + [a3 a2]; |
| 37 | * addu v0,a2,a0 |
| 38 | * addu t1,a3,a1 |
| 39 | * sltu v1,v0,a2 |
| 40 | * addu v1,v1,t1 |
| 41 | */ |
buzbee | c5159d5 | 2012-03-03 11:48:39 -0800 | [diff] [blame] | 42 | |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame^] | 43 | opRegRegReg(cUnit, kOpAdd, rlResult.lowReg, rlSrc2.lowReg, rlSrc1.lowReg); |
| 44 | int tReg = oatAllocTemp(cUnit); |
| 45 | opRegRegReg(cUnit, kOpAdd, tReg, rlSrc2.highReg, rlSrc1.highReg); |
| 46 | newLIR3(cUnit, kMipsSltu, rlResult.highReg, rlResult.lowReg, rlSrc2.lowReg); |
| 47 | opRegRegReg(cUnit, kOpAdd, rlResult.highReg, rlResult.highReg, tReg); |
| 48 | oatFreeTemp(cUnit, tReg); |
| 49 | storeValueWide(cUnit, rlDest, rlResult); |
| 50 | return false; |
buzbee | c5159d5 | 2012-03-03 11:48:39 -0800 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | bool genSubLong(CompilationUnit* cUnit, MIR* mir, RegLocation rlDest, |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame^] | 54 | RegLocation rlSrc1, RegLocation rlSrc2) |
buzbee | c5159d5 | 2012-03-03 11:48:39 -0800 | [diff] [blame] | 55 | { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame^] | 56 | rlSrc1 = loadValueWide(cUnit, rlSrc1, kCoreReg); |
| 57 | rlSrc2 = loadValueWide(cUnit, rlSrc2, kCoreReg); |
| 58 | RegLocation rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true); |
| 59 | /* |
| 60 | * [v1 v0] = [a1 a0] - [a3 a2]; |
| 61 | * subu v0,a0,a2 |
| 62 | * subu v1,a1,a3 |
| 63 | * sltu t1,a0,v0 |
| 64 | * subu v1,v1,t1 |
| 65 | */ |
buzbee | c5159d5 | 2012-03-03 11:48:39 -0800 | [diff] [blame] | 66 | |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame^] | 67 | opRegRegReg(cUnit, kOpSub, rlResult.lowReg, rlSrc1.lowReg, rlSrc2.lowReg); |
| 68 | opRegRegReg(cUnit, kOpSub, rlResult.highReg, rlSrc1.highReg, rlSrc2.highReg); |
| 69 | int tReg = oatAllocTemp(cUnit); |
| 70 | newLIR3(cUnit, kMipsSltu, tReg, rlSrc1.lowReg, rlResult.lowReg); |
| 71 | opRegRegReg(cUnit, kOpSub, rlResult.highReg, rlResult.highReg, tReg); |
| 72 | oatFreeTemp(cUnit, tReg); |
| 73 | storeValueWide(cUnit, rlDest, rlResult); |
| 74 | return false; |
buzbee | c5159d5 | 2012-03-03 11:48:39 -0800 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | bool genNegLong(CompilationUnit* cUnit, MIR* mir, RegLocation rlDest, |
| 78 | RegLocation rlSrc) |
| 79 | { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame^] | 80 | rlSrc = loadValueWide(cUnit, rlSrc, kCoreReg); |
| 81 | RegLocation rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true); |
| 82 | /* |
| 83 | * [v1 v0] = -[a1 a0] |
| 84 | * negu v0,a0 |
| 85 | * negu v1,a1 |
| 86 | * sltu t1,r_zero |
| 87 | * subu v1,v1,t1 |
| 88 | */ |
buzbee | c5159d5 | 2012-03-03 11:48:39 -0800 | [diff] [blame] | 89 | |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame^] | 90 | opRegReg(cUnit, kOpNeg, rlResult.lowReg, rlSrc.lowReg); |
| 91 | opRegReg(cUnit, kOpNeg, rlResult.highReg, rlSrc.highReg); |
| 92 | int tReg = oatAllocTemp(cUnit); |
| 93 | newLIR3(cUnit, kMipsSltu, tReg, r_ZERO, rlResult.lowReg); |
| 94 | opRegRegReg(cUnit, kOpSub, rlResult.highReg, rlResult.highReg, tReg); |
| 95 | oatFreeTemp(cUnit, tReg); |
| 96 | storeValueWide(cUnit, rlDest, rlResult); |
| 97 | return false; |
buzbee | c5159d5 | 2012-03-03 11:48:39 -0800 | [diff] [blame] | 98 | } |
| 99 | |
buzbee | 5de3494 | 2012-03-01 14:51:57 -0800 | [diff] [blame] | 100 | void genDebuggerUpdate(CompilationUnit* cUnit, int32_t offset); |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 101 | |
| 102 | /* |
buzbee | 5de3494 | 2012-03-01 14:51:57 -0800 | [diff] [blame] | 103 | * In the Arm code a it is typical to use the link register |
| 104 | * to hold the target address. However, for Mips we must |
| 105 | * ensure that all branch instructions can be restarted if |
| 106 | * there is a trap in the shadow. Allocate a temp register. |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 107 | */ |
buzbee | 5de3494 | 2012-03-01 14:51:57 -0800 | [diff] [blame] | 108 | int loadHelper(CompilationUnit* cUnit, int offset) |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 109 | { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame^] | 110 | int tReg = oatAllocTemp(cUnit); |
| 111 | loadWordDisp(cUnit, rSELF, offset, tReg); |
| 112 | return tReg; |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 113 | } |
| 114 | |
buzbee | 5de3494 | 2012-03-01 14:51:57 -0800 | [diff] [blame] | 115 | void spillCoreRegs(CompilationUnit* cUnit) |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 116 | { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame^] | 117 | if (cUnit->numCoreSpills == 0) { |
| 118 | return; |
| 119 | } |
| 120 | uint32_t mask = cUnit->coreSpillMask; |
| 121 | int offset = cUnit->numCoreSpills * 4; |
| 122 | opRegImm(cUnit, kOpSub, rSP, offset); |
| 123 | for (int reg = 0; mask; mask >>= 1, reg++) { |
| 124 | if (mask & 0x1) { |
| 125 | offset -= 4; |
| 126 | storeWordDisp(cUnit, rSP, offset, reg); |
buzbee | 5de3494 | 2012-03-01 14:51:57 -0800 | [diff] [blame] | 127 | } |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame^] | 128 | } |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 129 | } |
| 130 | |
buzbee | 5de3494 | 2012-03-01 14:51:57 -0800 | [diff] [blame] | 131 | void unSpillCoreRegs(CompilationUnit* cUnit) |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 132 | { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame^] | 133 | if (cUnit->numCoreSpills == 0) { |
| 134 | return; |
| 135 | } |
| 136 | uint32_t mask = cUnit->coreSpillMask; |
| 137 | int offset = cUnit->frameSize; |
| 138 | for (int reg = 0; mask; mask >>= 1, reg++) { |
| 139 | if (mask & 0x1) { |
| 140 | offset -= 4; |
| 141 | loadWordDisp(cUnit, rSP, offset, reg); |
buzbee | 5de3494 | 2012-03-01 14:51:57 -0800 | [diff] [blame] | 142 | } |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame^] | 143 | } |
| 144 | opRegImm(cUnit, kOpAdd, rSP, cUnit->frameSize); |
buzbee | 5de3494 | 2012-03-01 14:51:57 -0800 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | void genEntrySequence(CompilationUnit* cUnit, BasicBlock* bb) |
| 148 | { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame^] | 149 | int spillCount = cUnit->numCoreSpills + cUnit->numFPSpills; |
| 150 | /* |
| 151 | * On entry, rARG0, rARG1, rARG2 & rARG3 are live. Let the register |
| 152 | * allocation mechanism know so it doesn't try to use any of them when |
| 153 | * expanding the frame or flushing. This leaves the utility |
| 154 | * code with a single temp: r12. This should be enough. |
| 155 | */ |
| 156 | oatLockTemp(cUnit, rARG0); |
| 157 | oatLockTemp(cUnit, rARG1); |
| 158 | oatLockTemp(cUnit, rARG2); |
| 159 | oatLockTemp(cUnit, rARG3); |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 160 | |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame^] | 161 | /* |
| 162 | * We can safely skip the stack overflow check if we're |
| 163 | * a leaf *and* our frame size < fudge factor. |
| 164 | */ |
| 165 | bool skipOverflowCheck = ((cUnit->attrs & METHOD_IS_LEAF) && |
| 166 | ((size_t)cUnit->frameSize < Thread::kStackOverflowReservedBytes)); |
| 167 | newLIR0(cUnit, kPseudoMethodEntry); |
| 168 | int checkReg = oatAllocTemp(cUnit); |
| 169 | int newSP = oatAllocTemp(cUnit); |
| 170 | if (!skipOverflowCheck) { |
| 171 | /* Load stack limit */ |
| 172 | loadWordDisp(cUnit, rSELF, Thread::StackEndOffset().Int32Value(), checkReg); |
| 173 | } |
| 174 | /* Spill core callee saves */ |
| 175 | spillCoreRegs(cUnit); |
| 176 | /* NOTE: promotion of FP regs currently unsupported, thus no FP spill */ |
| 177 | DCHECK_EQ(cUnit->numFPSpills, 0); |
| 178 | if (!skipOverflowCheck) { |
| 179 | opRegRegImm(cUnit, kOpSub, newSP, rSP, cUnit->frameSize - (spillCount * 4)); |
| 180 | genRegRegCheck(cUnit, kCondCc, newSP, checkReg, NULL, kThrowStackOverflow); |
| 181 | opRegCopy(cUnit, rSP, newSP); // Establish stack |
| 182 | } else { |
| 183 | opRegImm(cUnit, kOpSub, rSP, cUnit->frameSize - (spillCount * 4)); |
| 184 | } |
buzbee | 9c044ce | 2012-03-18 13:24:07 -0700 | [diff] [blame] | 185 | |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame^] | 186 | flushIns(cUnit); |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 187 | |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame^] | 188 | if (cUnit->genDebugger) { |
| 189 | // Refresh update debugger callout |
| 190 | loadWordDisp(cUnit, rSELF, |
| 191 | ENTRYPOINT_OFFSET(pUpdateDebuggerFromCode), rSUSPEND); |
| 192 | genDebuggerUpdate(cUnit, DEBUGGER_METHOD_ENTRY); |
| 193 | } |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 194 | |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame^] | 195 | oatFreeTemp(cUnit, rARG0); |
| 196 | oatFreeTemp(cUnit, rARG1); |
| 197 | oatFreeTemp(cUnit, rARG2); |
| 198 | oatFreeTemp(cUnit, rARG3); |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 199 | } |
| 200 | |
buzbee | 5de3494 | 2012-03-01 14:51:57 -0800 | [diff] [blame] | 201 | void genExitSequence(CompilationUnit* cUnit, BasicBlock* bb) |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 202 | { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame^] | 203 | /* |
| 204 | * In the exit path, rRET0/rRET1 are live - make sure they aren't |
| 205 | * allocated by the register utilities as temps. |
| 206 | */ |
| 207 | oatLockTemp(cUnit, rRET0); |
| 208 | oatLockTemp(cUnit, rRET1); |
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 | newLIR0(cUnit, kPseudoMethodExit); |
| 211 | /* If we're compiling for the debugger, generate an update callout */ |
| 212 | if (cUnit->genDebugger) { |
| 213 | genDebuggerUpdate(cUnit, DEBUGGER_METHOD_EXIT); |
| 214 | } |
| 215 | unSpillCoreRegs(cUnit); |
| 216 | opReg(cUnit, kOpBx, r_RA); |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 217 | } |
| 218 | |
| 219 | /* |
| 220 | * Nop any unconditional branches that go to the next instruction. |
| 221 | * Note: new redundant branches may be inserted later, and we'll |
| 222 | * use a check in final instruction assembly to nop those out. |
| 223 | */ |
| 224 | void removeRedundantBranches(CompilationUnit* cUnit) |
| 225 | { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame^] | 226 | LIR* thisLIR; |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 227 | |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame^] | 228 | for (thisLIR = (LIR*) cUnit->firstLIRInsn; |
| 229 | thisLIR != (LIR*) cUnit->lastLIRInsn; |
| 230 | thisLIR = NEXT_LIR(thisLIR)) { |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 231 | |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame^] | 232 | /* Branch to the next instruction */ |
| 233 | if (thisLIR->opcode == kMipsB) { |
| 234 | LIR* nextLIR = thisLIR; |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 235 | |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame^] | 236 | while (true) { |
| 237 | nextLIR = NEXT_LIR(nextLIR); |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 238 | |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame^] | 239 | /* |
| 240 | * Is the branch target the next instruction? |
| 241 | */ |
| 242 | if (nextLIR == (LIR*) thisLIR->target) { |
| 243 | thisLIR->flags.isNop = true; |
| 244 | break; |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 245 | } |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame^] | 246 | |
| 247 | /* |
| 248 | * Found real useful stuff between the branch and the target. |
| 249 | * Need to explicitly check the lastLIRInsn here because it |
| 250 | * might be the last real instruction. |
| 251 | */ |
| 252 | if (!isPseudoOpcode(nextLIR->opcode) || |
| 253 | (nextLIR = (LIR*) cUnit->lastLIRInsn)) |
| 254 | break; |
| 255 | } |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 256 | } |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame^] | 257 | } |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 258 | } |
| 259 | |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 260 | |
| 261 | /* Common initialization routine for an architecture family */ |
| 262 | bool oatArchInit() |
| 263 | { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame^] | 264 | int i; |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 265 | |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame^] | 266 | for (i = 0; i < kMipsLast; i++) { |
| 267 | if (EncodingMap[i].opcode != i) { |
| 268 | LOG(FATAL) << "Encoding order for " << EncodingMap[i].name << |
| 269 | " is wrong: expecting " << i << ", seeing " << |
| 270 | (int)EncodingMap[i].opcode; |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 271 | } |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame^] | 272 | } |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 273 | |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame^] | 274 | return oatArchVariantInit(); |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 275 | } |
| 276 | |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 277 | } // namespace art |