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