buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [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 | |
buzbee | b046e16 | 2012-10-30 15:48:42 -0700 | [diff] [blame] | 17 | /* This file contains arm-specific codegen factory support. */ |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 18 | |
Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 19 | #include "oat/runtime/oat_support_entrypoints.h" |
| 20 | |
Elliott Hughes | 11d1b0c | 2012-01-23 16:57:47 -0800 | [diff] [blame] | 21 | namespace art { |
| 22 | |
buzbee | 408ad16 | 2012-06-06 16:45:18 -0700 | [diff] [blame] | 23 | bool genNegLong(CompilationUnit* cUnit, RegLocation rlDest, |
buzbee | c5159d5 | 2012-03-03 11:48:39 -0800 | [diff] [blame] | 24 | RegLocation rlSrc) |
| 25 | { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 26 | rlSrc = loadValueWide(cUnit, rlSrc, kCoreReg); |
| 27 | RegLocation rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true); |
| 28 | int zReg = oatAllocTemp(cUnit); |
| 29 | loadConstantNoClobber(cUnit, zReg, 0); |
| 30 | // Check for destructive overlap |
| 31 | if (rlResult.lowReg == rlSrc.highReg) { |
| 32 | int tReg = oatAllocTemp(cUnit); |
| 33 | opRegRegReg(cUnit, kOpSub, rlResult.lowReg, zReg, rlSrc.lowReg); |
| 34 | opRegRegReg(cUnit, kOpSbc, rlResult.highReg, zReg, tReg); |
| 35 | oatFreeTemp(cUnit, tReg); |
| 36 | } else { |
| 37 | opRegRegReg(cUnit, kOpSub, rlResult.lowReg, zReg, rlSrc.lowReg); |
| 38 | opRegRegReg(cUnit, kOpSbc, rlResult.highReg, zReg, rlSrc.highReg); |
| 39 | } |
| 40 | oatFreeTemp(cUnit, zReg); |
| 41 | storeValueWide(cUnit, rlDest, rlResult); |
| 42 | return false; |
buzbee | c5159d5 | 2012-03-03 11:48:39 -0800 | [diff] [blame] | 43 | } |
| 44 | |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 45 | int loadHelper(CompilationUnit* cUnit, int offset) |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 46 | { |
buzbee | f0504cd | 2012-11-13 16:31:10 -0800 | [diff] [blame] | 47 | loadWordDisp(cUnit, rARM_SELF, offset, rARM_LR); |
| 48 | return rARM_LR; |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 49 | } |
| 50 | |
buzbee | ad8f15e | 2012-06-18 14:49:45 -0700 | [diff] [blame] | 51 | void genEntrySequence(CompilationUnit* cUnit, RegLocation* argLocs, |
| 52 | RegLocation rlMethod) |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 53 | { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 54 | int spillCount = cUnit->numCoreSpills + cUnit->numFPSpills; |
| 55 | /* |
| 56 | * On entry, r0, r1, r2 & r3 are live. Let the register allocation |
| 57 | * mechanism know so it doesn't try to use any of them when |
| 58 | * expanding the frame or flushing. This leaves the utility |
| 59 | * code with a single temp: r12. This should be enough. |
| 60 | */ |
| 61 | oatLockTemp(cUnit, r0); |
| 62 | oatLockTemp(cUnit, r1); |
| 63 | oatLockTemp(cUnit, r2); |
| 64 | oatLockTemp(cUnit, r3); |
| 65 | |
| 66 | /* |
| 67 | * We can safely skip the stack overflow check if we're |
| 68 | * a leaf *and* our frame size < fudge factor. |
| 69 | */ |
| 70 | bool skipOverflowCheck = ((cUnit->attrs & METHOD_IS_LEAF) && |
| 71 | ((size_t)cUnit->frameSize < |
| 72 | Thread::kStackOverflowReservedBytes)); |
| 73 | newLIR0(cUnit, kPseudoMethodEntry); |
| 74 | if (!skipOverflowCheck) { |
| 75 | /* Load stack limit */ |
buzbee | f0504cd | 2012-11-13 16:31:10 -0800 | [diff] [blame] | 76 | loadWordDisp(cUnit, rARM_SELF, Thread::StackEndOffset().Int32Value(), r12); |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 77 | } |
| 78 | /* Spill core callee saves */ |
| 79 | newLIR1(cUnit, kThumb2Push, cUnit->coreSpillMask); |
| 80 | /* Need to spill any FP regs? */ |
| 81 | if (cUnit->numFPSpills) { |
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 | * NOTE: fp spills are a little different from core spills in that |
| 84 | * they are pushed as a contiguous block. When promoting from |
| 85 | * the fp set, we must allocate all singles from s16..highest-promoted |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 86 | */ |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 87 | newLIR1(cUnit, kThumb2VPushCS, cUnit->numFPSpills); |
| 88 | } |
| 89 | if (!skipOverflowCheck) { |
buzbee | f0504cd | 2012-11-13 16:31:10 -0800 | [diff] [blame] | 90 | opRegRegImm(cUnit, kOpSub, rARM_LR, rARM_SP, cUnit->frameSize - (spillCount * 4)); |
| 91 | genRegRegCheck(cUnit, kCondCc, rARM_LR, r12, kThrowStackOverflow); |
| 92 | opRegCopy(cUnit, rARM_SP, rARM_LR); // Establish stack |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 93 | } else { |
buzbee | f0504cd | 2012-11-13 16:31:10 -0800 | [diff] [blame] | 94 | opRegImm(cUnit, kOpSub, rARM_SP, cUnit->frameSize - (spillCount * 4)); |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 95 | } |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 96 | |
buzbee | ad8f15e | 2012-06-18 14:49:45 -0700 | [diff] [blame] | 97 | flushIns(cUnit, argLocs, rlMethod); |
buzbee | e196567 | 2012-03-11 18:39:19 -0700 | [diff] [blame] | 98 | |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 99 | oatFreeTemp(cUnit, r0); |
| 100 | oatFreeTemp(cUnit, r1); |
| 101 | oatFreeTemp(cUnit, r2); |
| 102 | oatFreeTemp(cUnit, r3); |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 103 | } |
| 104 | |
buzbee | 2cfc639 | 2012-05-07 14:51:40 -0700 | [diff] [blame] | 105 | void genExitSequence(CompilationUnit* cUnit) |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 106 | { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 107 | int spillCount = cUnit->numCoreSpills + cUnit->numFPSpills; |
| 108 | /* |
| 109 | * In the exit path, r0/r1 are live - make sure they aren't |
| 110 | * allocated by the register utilities as temps. |
| 111 | */ |
| 112 | oatLockTemp(cUnit, r0); |
| 113 | oatLockTemp(cUnit, r1); |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 114 | |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 115 | newLIR0(cUnit, kPseudoMethodExit); |
buzbee | f0504cd | 2012-11-13 16:31:10 -0800 | [diff] [blame] | 116 | opRegImm(cUnit, kOpAdd, rARM_SP, cUnit->frameSize - (spillCount * 4)); |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 117 | /* Need to restore any FP callee saves? */ |
| 118 | if (cUnit->numFPSpills) { |
| 119 | newLIR1(cUnit, kThumb2VPopCS, cUnit->numFPSpills); |
| 120 | } |
buzbee | f0504cd | 2012-11-13 16:31:10 -0800 | [diff] [blame] | 121 | if (cUnit->coreSpillMask & (1 << rARM_LR)) { |
| 122 | /* Unspill rARM_LR to rARM_PC */ |
| 123 | cUnit->coreSpillMask &= ~(1 << rARM_LR); |
| 124 | cUnit->coreSpillMask |= (1 << rARM_PC); |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 125 | } |
| 126 | newLIR1(cUnit, kThumb2Pop, cUnit->coreSpillMask); |
buzbee | f0504cd | 2012-11-13 16:31:10 -0800 | [diff] [blame] | 127 | if (!(cUnit->coreSpillMask & (1 << rARM_PC))) { |
| 128 | /* We didn't pop to rARM_PC, so must do a bv rARM_LR */ |
| 129 | newLIR1(cUnit, kThumbBx, rARM_LR); |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 130 | } |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | /* |
| 134 | * Nop any unconditional branches that go to the next instruction. |
| 135 | * Note: new redundant branches may be inserted later, and we'll |
| 136 | * use a check in final instruction assembly to nop those out. |
| 137 | */ |
| 138 | void removeRedundantBranches(CompilationUnit* cUnit) |
| 139 | { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 140 | LIR* thisLIR; |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 141 | |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 142 | for (thisLIR = (LIR*) cUnit->firstLIRInsn; |
| 143 | thisLIR != (LIR*) cUnit->lastLIRInsn; |
| 144 | thisLIR = NEXT_LIR(thisLIR)) { |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 145 | |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 146 | /* Branch to the next instruction */ |
| 147 | if ((thisLIR->opcode == kThumbBUncond) || |
| 148 | (thisLIR->opcode == kThumb2BUncond)) { |
| 149 | LIR* nextLIR = thisLIR; |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 150 | |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 151 | while (true) { |
| 152 | nextLIR = NEXT_LIR(nextLIR); |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 153 | |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 154 | /* |
| 155 | * Is the branch target the next instruction? |
| 156 | */ |
| 157 | if (nextLIR == (LIR*) thisLIR->target) { |
| 158 | thisLIR->flags.isNop = true; |
| 159 | break; |
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 | /* |
| 163 | * Found real useful stuff between the branch and the target. |
| 164 | * Need to explicitly check the lastLIRInsn here because it |
| 165 | * might be the last real instruction. |
| 166 | */ |
| 167 | if (!isPseudoOpcode(nextLIR->opcode) || |
| 168 | (nextLIR = (LIR*) cUnit->lastLIRInsn)) |
| 169 | break; |
| 170 | } |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 171 | } |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 172 | } |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 173 | } |
| 174 | |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 175 | |
| 176 | /* Common initialization routine for an architecture family */ |
| 177 | bool oatArchInit() |
| 178 | { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 179 | int i; |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 180 | |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 181 | for (i = 0; i < kArmLast; i++) { |
| 182 | if (EncodingMap[i].opcode != i) { |
| 183 | LOG(FATAL) << "Encoding order for " << EncodingMap[i].name |
| 184 | << " is wrong: expecting " << i << ", seeing " |
| 185 | << (int)EncodingMap[i].opcode; |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 186 | } |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 187 | } |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 188 | |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 189 | return oatArchVariantInit(); |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 190 | } |
buzbee | b046e16 | 2012-10-30 15:48:42 -0700 | [diff] [blame] | 191 | |
| 192 | bool genAddLong(CompilationUnit* cUnit, RegLocation rlDest, |
| 193 | RegLocation rlSrc1, RegLocation rlSrc2) |
| 194 | { |
| 195 | LOG(FATAL) << "Unexpected use of genAddLong for Arm"; |
| 196 | return false; |
| 197 | } |
| 198 | |
| 199 | bool genSubLong(CompilationUnit* cUnit, RegLocation rlDest, |
| 200 | RegLocation rlSrc1, RegLocation rlSrc2) |
| 201 | { |
| 202 | LOG(FATAL) << "Unexpected use of genSubLong for Arm"; |
| 203 | return false; |
| 204 | } |
| 205 | |
| 206 | bool genAndLong(CompilationUnit* cUnit, RegLocation rlDest, |
| 207 | RegLocation rlSrc1, RegLocation rlSrc2) |
| 208 | { |
| 209 | LOG(FATAL) << "Unexpected use of genAndLong for Arm"; |
| 210 | return false; |
| 211 | } |
| 212 | |
| 213 | bool genOrLong(CompilationUnit* cUnit, RegLocation rlDest, |
| 214 | RegLocation rlSrc1, RegLocation rlSrc2) |
| 215 | { |
| 216 | LOG(FATAL) << "Unexpected use of genOrLong for Arm"; |
| 217 | return false; |
| 218 | } |
| 219 | |
| 220 | bool genXorLong(CompilationUnit* cUnit, RegLocation rlDest, |
| 221 | RegLocation rlSrc1, RegLocation rlSrc2) |
| 222 | { |
| 223 | LOG(FATAL) << "Unexpected use of genXoLong for Arm"; |
| 224 | return false; |
| 225 | } |
| 226 | |
Elliott Hughes | 11d1b0c | 2012-01-23 16:57:47 -0800 | [diff] [blame] | 227 | } // namespace art |