buzbee | e88dfbf | 2012-03-05 11:19:57 -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 x86-specific codegen factory support. |
| 19 | * It is included by |
| 20 | * |
| 21 | * Codegen-$(TARGET_ARCH_VARIANT).c |
| 22 | * |
| 23 | */ |
| 24 | |
| 25 | namespace art { |
| 26 | |
| 27 | bool genAddLong(CompilationUnit* cUnit, MIR* mir, RegLocation rlDest, |
| 28 | RegLocation rlSrc1, RegLocation rlSrc2) |
| 29 | { |
buzbee | e88dfbf | 2012-03-05 11:19:57 -0800 | [diff] [blame] | 30 | rlSrc1 = loadValueWide(cUnit, rlSrc1, kCoreReg); |
| 31 | rlSrc2 = loadValueWide(cUnit, rlSrc2, kCoreReg); |
| 32 | RegLocation rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true); |
| 33 | /* |
| 34 | * [v1 v0] = [a1 a0] + [a3 a2]; |
Ian Rogers | f7d9ad3 | 2012-03-13 18:45:39 -0700 | [diff] [blame] | 35 | * add v0,a2,a0 |
| 36 | * adc v1,a3,a1 |
buzbee | e88dfbf | 2012-03-05 11:19:57 -0800 | [diff] [blame] | 37 | */ |
| 38 | |
| 39 | opRegRegReg(cUnit, kOpAdd, rlResult.lowReg, rlSrc2.lowReg, rlSrc1.lowReg); |
Ian Rogers | f7d9ad3 | 2012-03-13 18:45:39 -0700 | [diff] [blame] | 40 | opRegRegReg(cUnit, kOpAdc, rlResult.highReg, rlSrc2.highReg, rlSrc1.highReg); |
buzbee | e88dfbf | 2012-03-05 11:19:57 -0800 | [diff] [blame] | 41 | storeValueWide(cUnit, rlDest, rlResult); |
buzbee | e88dfbf | 2012-03-05 11:19:57 -0800 | [diff] [blame] | 42 | return false; |
| 43 | } |
| 44 | |
| 45 | bool genSubLong(CompilationUnit* cUnit, MIR* mir, RegLocation rlDest, |
| 46 | RegLocation rlSrc1, RegLocation rlSrc2) |
| 47 | { |
buzbee | e88dfbf | 2012-03-05 11:19:57 -0800 | [diff] [blame] | 48 | rlSrc1 = loadValueWide(cUnit, rlSrc1, kCoreReg); |
| 49 | rlSrc2 = loadValueWide(cUnit, rlSrc2, kCoreReg); |
| 50 | RegLocation rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true); |
| 51 | /* |
| 52 | * [v1 v0] = [a1 a0] - [a3 a2]; |
Ian Rogers | f7d9ad3 | 2012-03-13 18:45:39 -0700 | [diff] [blame] | 53 | * sub v0,a0,a2 |
| 54 | * sbb v1,a1,a3 |
buzbee | e88dfbf | 2012-03-05 11:19:57 -0800 | [diff] [blame] | 55 | */ |
| 56 | |
| 57 | opRegRegReg(cUnit, kOpSub, rlResult.lowReg, rlSrc1.lowReg, rlSrc2.lowReg); |
Ian Rogers | f7d9ad3 | 2012-03-13 18:45:39 -0700 | [diff] [blame] | 58 | opRegRegReg(cUnit, kOpSbc, rlResult.highReg, rlSrc1.highReg, rlSrc2.highReg); |
buzbee | e88dfbf | 2012-03-05 11:19:57 -0800 | [diff] [blame] | 59 | storeValueWide(cUnit, rlDest, rlResult); |
buzbee | e88dfbf | 2012-03-05 11:19:57 -0800 | [diff] [blame] | 60 | return false; |
| 61 | } |
| 62 | |
| 63 | bool genNegLong(CompilationUnit* cUnit, MIR* mir, RegLocation rlDest, |
| 64 | RegLocation rlSrc) |
| 65 | { |
| 66 | UNIMPLEMENTED(WARNING) << "genNegLong"; |
| 67 | #if 0 |
| 68 | rlSrc = loadValueWide(cUnit, rlSrc, kCoreReg); |
| 69 | RegLocation rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true); |
| 70 | /* |
| 71 | * [v1 v0] = -[a1 a0] |
| 72 | * negu v0,a0 |
| 73 | * negu v1,a1 |
| 74 | * sltu t1,r_zero |
| 75 | * subu v1,v1,t1 |
| 76 | */ |
| 77 | |
| 78 | opRegReg(cUnit, kOpNeg, rlResult.lowReg, rlSrc.lowReg); |
| 79 | opRegReg(cUnit, kOpNeg, rlResult.highReg, rlSrc.highReg); |
| 80 | int tReg = oatAllocTemp(cUnit); |
buzbee | a7678db | 2012-03-05 15:35:46 -0800 | [diff] [blame] | 81 | newLIR3(cUnit, kX86Sltu, tReg, r_ZERO, rlResult.lowReg); |
buzbee | e88dfbf | 2012-03-05 11:19:57 -0800 | [diff] [blame] | 82 | opRegRegReg(cUnit, kOpSub, rlResult.highReg, rlResult.highReg, tReg); |
| 83 | oatFreeTemp(cUnit, tReg); |
| 84 | storeValueWide(cUnit, rlDest, rlResult); |
| 85 | #endif |
| 86 | return false; |
| 87 | } |
| 88 | |
| 89 | void genDebuggerUpdate(CompilationUnit* cUnit, int32_t offset); |
| 90 | |
Ian Rogers | f7d9ad3 | 2012-03-13 18:45:39 -0700 | [diff] [blame] | 91 | void spillCoreRegs(CompilationUnit* cUnit) { |
| 92 | if (cUnit->numCoreSpills == 0) { |
| 93 | return; |
| 94 | } |
| 95 | // Spill mask not including fake return address register |
| 96 | uint32_t mask = cUnit->coreSpillMask & ~(1 << rRET); |
| 97 | int offset = cUnit->frameSize - 4; |
| 98 | for (int reg = 0; mask; mask >>= 1, reg++) { |
| 99 | if (mask & 0x1) { |
| 100 | offset -= 4; |
| 101 | storeWordDisp(cUnit, rSP, offset, reg); |
buzbee | e88dfbf | 2012-03-05 11:19:57 -0800 | [diff] [blame] | 102 | } |
Ian Rogers | f7d9ad3 | 2012-03-13 18:45:39 -0700 | [diff] [blame] | 103 | } |
buzbee | e88dfbf | 2012-03-05 11:19:57 -0800 | [diff] [blame] | 104 | } |
| 105 | |
Ian Rogers | f7d9ad3 | 2012-03-13 18:45:39 -0700 | [diff] [blame] | 106 | void unSpillCoreRegs(CompilationUnit* cUnit) { |
| 107 | if (cUnit->numCoreSpills == 0) { |
| 108 | return; |
| 109 | } |
| 110 | // Spill mask not including fake return address register |
| 111 | uint32_t mask = cUnit->coreSpillMask & ~(1 << rRET); |
| 112 | int offset = cUnit->frameSize - 4; |
| 113 | for (int reg = 0; mask; mask >>= 1, reg++) { |
| 114 | if (mask & 0x1) { |
| 115 | offset -= 4; |
| 116 | loadWordDisp(cUnit, rSP, offset, reg); |
buzbee | e88dfbf | 2012-03-05 11:19:57 -0800 | [diff] [blame] | 117 | } |
Ian Rogers | f7d9ad3 | 2012-03-13 18:45:39 -0700 | [diff] [blame] | 118 | } |
Ian Rogers | b5d09b2 | 2012-03-06 22:14:17 -0800 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | void opRegThreadMem(CompilationUnit* cUnit, OpKind op, int rDest, int threadOffset) { |
| 122 | X86OpCode opcode = kX86Bkpt; |
| 123 | switch (op) { |
| 124 | case kOpCmp: opcode = kX86Cmp32RT; break; |
| 125 | default: |
| 126 | LOG(FATAL) << "Bad opcode: " << op; |
| 127 | break; |
| 128 | } |
Ian Rogers | b5d09b2 | 2012-03-06 22:14:17 -0800 | [diff] [blame] | 129 | newLIR2(cUnit, opcode, rDest, threadOffset); |
buzbee | e88dfbf | 2012-03-05 11:19:57 -0800 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | void genEntrySequence(CompilationUnit* cUnit, BasicBlock* bb) |
| 133 | { |
buzbee | e88dfbf | 2012-03-05 11:19:57 -0800 | [diff] [blame] | 134 | /* |
Ian Rogers | b5d09b2 | 2012-03-06 22:14:17 -0800 | [diff] [blame] | 135 | * On entry, rARG0, rARG1, rARG2 are live. Let the register |
buzbee | e88dfbf | 2012-03-05 11:19:57 -0800 | [diff] [blame] | 136 | * allocation mechanism know so it doesn't try to use any of them when |
| 137 | * expanding the frame or flushing. This leaves the utility |
Ian Rogers | b5d09b2 | 2012-03-06 22:14:17 -0800 | [diff] [blame] | 138 | * code with no spare temps. |
buzbee | e88dfbf | 2012-03-05 11:19:57 -0800 | [diff] [blame] | 139 | */ |
| 140 | oatLockTemp(cUnit, rARG0); |
| 141 | oatLockTemp(cUnit, rARG1); |
| 142 | oatLockTemp(cUnit, rARG2); |
Ian Rogers | b5d09b2 | 2012-03-06 22:14:17 -0800 | [diff] [blame] | 143 | |
| 144 | /* Build frame, return address already on stack */ |
| 145 | opRegImm(cUnit, kOpSub, rSP, cUnit->frameSize - 4); |
buzbee | e88dfbf | 2012-03-05 11:19:57 -0800 | [diff] [blame] | 146 | |
| 147 | /* |
| 148 | * We can safely skip the stack overflow check if we're |
| 149 | * a leaf *and* our frame size < fudge factor. |
| 150 | */ |
| 151 | bool skipOverflowCheck = ((cUnit->attrs & METHOD_IS_LEAF) && |
| 152 | ((size_t)cUnit->frameSize < |
| 153 | Thread::kStackOverflowReservedBytes)); |
| 154 | newLIR0(cUnit, kPseudoMethodEntry); |
buzbee | e88dfbf | 2012-03-05 11:19:57 -0800 | [diff] [blame] | 155 | /* Spill core callee saves */ |
| 156 | spillCoreRegs(cUnit); |
| 157 | /* NOTE: promotion of FP regs currently unsupported, thus no FP spill */ |
| 158 | DCHECK_EQ(cUnit->numFPSpills, 0); |
| 159 | if (!skipOverflowCheck) { |
Ian Rogers | b5d09b2 | 2012-03-06 22:14:17 -0800 | [diff] [blame] | 160 | // cmp rSP, fs:[stack_end_]; jcc throw_launchpad |
| 161 | LIR* tgt = rawLIR(cUnit, 0, kPseudoThrowTarget, kThrowStackOverflow, 0, 0, 0, 0); |
| 162 | opRegThreadMem(cUnit, kOpCmp, rSP, Thread::StackEndOffset().Int32Value()); |
| 163 | opCondBranch(cUnit, kCondUlt, tgt); |
| 164 | // Remember branch target - will process later |
| 165 | oatInsertGrowableList(cUnit, &cUnit->throwLaunchpads, (intptr_t)tgt); |
buzbee | e88dfbf | 2012-03-05 11:19:57 -0800 | [diff] [blame] | 166 | } |
buzbee | 9c044ce | 2012-03-18 13:24:07 -0700 | [diff] [blame] | 167 | |
buzbee | e88dfbf | 2012-03-05 11:19:57 -0800 | [diff] [blame] | 168 | flushIns(cUnit); |
| 169 | |
| 170 | if (cUnit->genDebugger) { |
| 171 | // Refresh update debugger callout |
Ian Rogers | b5d09b2 | 2012-03-06 22:14:17 -0800 | [diff] [blame] | 172 | UNIMPLEMENTED(WARNING) << "genDebugger"; |
| 173 | #if 0 |
buzbee | e88dfbf | 2012-03-05 11:19:57 -0800 | [diff] [blame] | 174 | loadWordDisp(cUnit, rSELF, |
| 175 | OFFSETOF_MEMBER(Thread, pUpdateDebuggerFromCode), rSUSPEND); |
| 176 | genDebuggerUpdate(cUnit, DEBUGGER_METHOD_ENTRY); |
Ian Rogers | b5d09b2 | 2012-03-06 22:14:17 -0800 | [diff] [blame] | 177 | #endif |
buzbee | e88dfbf | 2012-03-05 11:19:57 -0800 | [diff] [blame] | 178 | } |
| 179 | |
| 180 | oatFreeTemp(cUnit, rARG0); |
| 181 | oatFreeTemp(cUnit, rARG1); |
| 182 | oatFreeTemp(cUnit, rARG2); |
buzbee | e88dfbf | 2012-03-05 11:19:57 -0800 | [diff] [blame] | 183 | } |
| 184 | |
Ian Rogers | b5d09b2 | 2012-03-06 22:14:17 -0800 | [diff] [blame] | 185 | void genExitSequence(CompilationUnit* cUnit, BasicBlock* bb) { |
| 186 | /* |
| 187 | * In the exit path, rRET0/rRET1 are live - make sure they aren't |
| 188 | * allocated by the register utilities as temps. |
| 189 | */ |
| 190 | oatLockTemp(cUnit, rRET0); |
| 191 | oatLockTemp(cUnit, rRET1); |
buzbee | e88dfbf | 2012-03-05 11:19:57 -0800 | [diff] [blame] | 192 | |
Ian Rogers | b5d09b2 | 2012-03-06 22:14:17 -0800 | [diff] [blame] | 193 | newLIR0(cUnit, kPseudoMethodExit); |
| 194 | /* If we're compiling for the debugger, generate an update callout */ |
| 195 | if (cUnit->genDebugger) { |
| 196 | genDebuggerUpdate(cUnit, DEBUGGER_METHOD_EXIT); |
| 197 | } |
| 198 | unSpillCoreRegs(cUnit); |
| 199 | /* Remove frame except for return address */ |
| 200 | opRegImm(cUnit, kOpAdd, rSP, cUnit->frameSize - 4); |
| 201 | newLIR0(cUnit, kX86Ret); |
buzbee | e88dfbf | 2012-03-05 11:19:57 -0800 | [diff] [blame] | 202 | } |
| 203 | |
| 204 | /* |
| 205 | * Nop any unconditional branches that go to the next instruction. |
| 206 | * Note: new redundant branches may be inserted later, and we'll |
| 207 | * use a check in final instruction assembly to nop those out. |
| 208 | */ |
Ian Rogers | b5d09b2 | 2012-03-06 22:14:17 -0800 | [diff] [blame] | 209 | void removeRedundantBranches(CompilationUnit* cUnit) { |
| 210 | LIR* thisLIR; |
buzbee | e88dfbf | 2012-03-05 11:19:57 -0800 | [diff] [blame] | 211 | |
Ian Rogers | b5d09b2 | 2012-03-06 22:14:17 -0800 | [diff] [blame] | 212 | for (thisLIR = (LIR*) cUnit->firstLIRInsn; |
| 213 | thisLIR != (LIR*) cUnit->lastLIRInsn; |
| 214 | thisLIR = NEXT_LIR(thisLIR)) { |
buzbee | e88dfbf | 2012-03-05 11:19:57 -0800 | [diff] [blame] | 215 | |
Ian Rogers | b5d09b2 | 2012-03-06 22:14:17 -0800 | [diff] [blame] | 216 | /* Branch to the next instruction */ |
Ian Rogers | b41b33b | 2012-03-20 14:22:54 -0700 | [diff] [blame^] | 217 | if (thisLIR->opcode == kX86Jmp8 || thisLIR->opcode == kX86Jmp32) { |
Ian Rogers | b5d09b2 | 2012-03-06 22:14:17 -0800 | [diff] [blame] | 218 | LIR* nextLIR = thisLIR; |
buzbee | e88dfbf | 2012-03-05 11:19:57 -0800 | [diff] [blame] | 219 | |
Ian Rogers | b5d09b2 | 2012-03-06 22:14:17 -0800 | [diff] [blame] | 220 | while (true) { |
| 221 | nextLIR = NEXT_LIR(nextLIR); |
buzbee | e88dfbf | 2012-03-05 11:19:57 -0800 | [diff] [blame] | 222 | |
Ian Rogers | b5d09b2 | 2012-03-06 22:14:17 -0800 | [diff] [blame] | 223 | /* |
| 224 | * Is the branch target the next instruction? |
| 225 | */ |
| 226 | if (nextLIR == (LIR*) thisLIR->target) { |
| 227 | thisLIR->flags.isNop = true; |
| 228 | break; |
buzbee | e88dfbf | 2012-03-05 11:19:57 -0800 | [diff] [blame] | 229 | } |
Ian Rogers | b5d09b2 | 2012-03-06 22:14:17 -0800 | [diff] [blame] | 230 | |
| 231 | /* |
| 232 | * Found real useful stuff between the branch and the target. |
| 233 | * Need to explicitly check the lastLIRInsn here because it |
| 234 | * might be the last real instruction. |
| 235 | */ |
| 236 | if (!isPseudoOpcode(nextLIR->opcode) || |
| 237 | (nextLIR = (LIR*) cUnit->lastLIRInsn)) |
| 238 | break; |
| 239 | } |
buzbee | e88dfbf | 2012-03-05 11:19:57 -0800 | [diff] [blame] | 240 | } |
Ian Rogers | b5d09b2 | 2012-03-06 22:14:17 -0800 | [diff] [blame] | 241 | } |
buzbee | e88dfbf | 2012-03-05 11:19:57 -0800 | [diff] [blame] | 242 | } |
| 243 | |
| 244 | |
| 245 | /* Common initialization routine for an architecture family */ |
Ian Rogers | f7d9ad3 | 2012-03-13 18:45:39 -0700 | [diff] [blame] | 246 | bool oatArchInit() { |
| 247 | int i; |
buzbee | e88dfbf | 2012-03-05 11:19:57 -0800 | [diff] [blame] | 248 | |
Ian Rogers | f7d9ad3 | 2012-03-13 18:45:39 -0700 | [diff] [blame] | 249 | for (i = 0; i < kX86Last; i++) { |
| 250 | if (EncodingMap[i].opcode != i) { |
| 251 | LOG(FATAL) << "Encoding order for " << EncodingMap[i].name |
| 252 | << " is wrong: expecting " << i << ", seeing " << (int)EncodingMap[i].opcode; |
buzbee | e88dfbf | 2012-03-05 11:19:57 -0800 | [diff] [blame] | 253 | } |
Ian Rogers | f7d9ad3 | 2012-03-13 18:45:39 -0700 | [diff] [blame] | 254 | } |
buzbee | e88dfbf | 2012-03-05 11:19:57 -0800 | [diff] [blame] | 255 | |
Ian Rogers | f7d9ad3 | 2012-03-13 18:45:39 -0700 | [diff] [blame] | 256 | return oatArchVariantInit(); |
buzbee | e88dfbf | 2012-03-05 11:19:57 -0800 | [diff] [blame] | 257 | } |
| 258 | |
| 259 | } // namespace art |