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 X86 ISA */ |
| 18 | |
buzbee | 1bc37c6 | 2012-11-20 13:35:41 -0800 | [diff] [blame] | 19 | #include "x86_lir.h" |
| 20 | #include "../codegen_util.h" |
| 21 | #include "../ralloc_util.h" |
| 22 | |
buzbee | efc6369 | 2012-11-14 16:31:52 -0800 | [diff] [blame] | 23 | namespace art { |
| 24 | |
| 25 | void genSpecialCase(CompilationUnit* cUnit, BasicBlock* bb, MIR* mir, |
| 26 | SpecialCaseHandler specialCase) |
| 27 | { |
| 28 | // TODO |
| 29 | } |
| 30 | |
| 31 | /* |
| 32 | * The sparse table in the literal pool is an array of <key,displacement> |
| 33 | * pairs. |
| 34 | */ |
| 35 | BasicBlock *findBlock(CompilationUnit* cUnit, unsigned int codeOffset, |
| 36 | bool split, bool create, BasicBlock** immedPredBlockP); |
| 37 | void genSparseSwitch(CompilationUnit* cUnit, uint32_t tableOffset, |
| 38 | RegLocation rlSrc) |
| 39 | { |
buzbee | eaf09bc | 2012-11-15 14:51:41 -0800 | [diff] [blame] | 40 | const uint16_t* table = cUnit->insns + cUnit->currentDalvikOffset + tableOffset; |
buzbee | efc6369 | 2012-11-14 16:31:52 -0800 | [diff] [blame] | 41 | if (cUnit->printMe) { |
| 42 | dumpSparseSwitchTable(table); |
| 43 | } |
| 44 | int entries = table[1]; |
buzbee | cbd6d44 | 2012-11-17 14:11:25 -0800 | [diff] [blame] | 45 | const int* keys = reinterpret_cast<const int*>(&table[2]); |
| 46 | const int* targets = &keys[entries]; |
buzbee | efc6369 | 2012-11-14 16:31:52 -0800 | [diff] [blame] | 47 | rlSrc = loadValue(cUnit, rlSrc, kCoreReg); |
| 48 | for (int i = 0; i < entries; i++) { |
| 49 | int key = keys[i]; |
| 50 | BasicBlock* case_block = findBlock(cUnit, |
| 51 | cUnit->currentDalvikOffset + targets[i], |
| 52 | false, false, NULL); |
| 53 | LIR* labelList = cUnit->blockLabelList; |
| 54 | opCmpImmBranch(cUnit, kCondEq, rlSrc.lowReg, key, |
| 55 | &labelList[case_block->id]); |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | /* |
| 60 | * Code pattern will look something like: |
| 61 | * |
| 62 | * mov rVal, .. |
| 63 | * call 0 |
| 64 | * pop rStartOfMethod |
| 65 | * sub rStartOfMethod, .. |
| 66 | * mov rKeyReg, rVal |
| 67 | * sub rKeyReg, lowKey |
| 68 | * cmp rKeyReg, size-1 ; bound check |
| 69 | * ja done |
| 70 | * mov rDisp, [rStartOfMethod + rKeyReg * 4 + tableOffset] |
| 71 | * add rStartOfMethod, rDisp |
| 72 | * jmp rStartOfMethod |
| 73 | * done: |
| 74 | */ |
| 75 | void genPackedSwitch(CompilationUnit* cUnit, uint32_t tableOffset, |
| 76 | RegLocation rlSrc) |
| 77 | { |
buzbee | eaf09bc | 2012-11-15 14:51:41 -0800 | [diff] [blame] | 78 | const uint16_t* table = cUnit->insns + cUnit->currentDalvikOffset + tableOffset; |
buzbee | efc6369 | 2012-11-14 16:31:52 -0800 | [diff] [blame] | 79 | if (cUnit->printMe) { |
| 80 | dumpPackedSwitchTable(table); |
| 81 | } |
| 82 | // Add the table to the list - we'll process it later |
buzbee | cbd6d44 | 2012-11-17 14:11:25 -0800 | [diff] [blame] | 83 | SwitchTable *tabRec = |
| 84 | static_cast<SwitchTable *>(oatNew(cUnit, sizeof(SwitchTable), true, kAllocData)); |
buzbee | efc6369 | 2012-11-14 16:31:52 -0800 | [diff] [blame] | 85 | tabRec->table = table; |
| 86 | tabRec->vaddr = cUnit->currentDalvikOffset; |
| 87 | int size = table[1]; |
buzbee | cbd6d44 | 2012-11-17 14:11:25 -0800 | [diff] [blame] | 88 | tabRec->targets = static_cast<LIR**>(oatNew(cUnit, size * sizeof(LIR*), true, kAllocLIR)); |
| 89 | oatInsertGrowableList(cUnit, &cUnit->switchTables, reinterpret_cast<uintptr_t>(tabRec)); |
buzbee | efc6369 | 2012-11-14 16:31:52 -0800 | [diff] [blame] | 90 | |
| 91 | // Get the switch value |
| 92 | rlSrc = loadValue(cUnit, rlSrc, kCoreReg); |
| 93 | int startOfMethodReg = oatAllocTemp(cUnit); |
| 94 | // Materialize a pointer to the switch table |
| 95 | //newLIR0(cUnit, kX86Bkpt); |
| 96 | newLIR1(cUnit, kX86StartOfMethod, startOfMethodReg); |
| 97 | int lowKey = s4FromSwitchData(&table[2]); |
| 98 | int keyReg; |
| 99 | // Remove the bias, if necessary |
| 100 | if (lowKey == 0) { |
| 101 | keyReg = rlSrc.lowReg; |
| 102 | } else { |
| 103 | keyReg = oatAllocTemp(cUnit); |
| 104 | opRegRegImm(cUnit, kOpSub, keyReg, rlSrc.lowReg, lowKey); |
| 105 | } |
| 106 | // Bounds check - if < 0 or >= size continue following switch |
| 107 | opRegImm(cUnit, kOpCmp, keyReg, size-1); |
| 108 | LIR* branchOver = opCondBranch(cUnit, kCondHi, NULL); |
| 109 | |
| 110 | // Load the displacement from the switch table |
| 111 | int dispReg = oatAllocTemp(cUnit); |
| 112 | newLIR5(cUnit, kX86PcRelLoadRA, dispReg, startOfMethodReg, keyReg, 2, |
buzbee | cbd6d44 | 2012-11-17 14:11:25 -0800 | [diff] [blame] | 113 | reinterpret_cast<uintptr_t>(tabRec)); |
buzbee | efc6369 | 2012-11-14 16:31:52 -0800 | [diff] [blame] | 114 | // Add displacement to start of method |
| 115 | opRegReg(cUnit, kOpAdd, startOfMethodReg, dispReg); |
| 116 | // ..and go! |
| 117 | LIR* switchBranch = newLIR1(cUnit, kX86JmpR, startOfMethodReg); |
| 118 | tabRec->anchor = switchBranch; |
| 119 | |
| 120 | /* branchOver target here */ |
| 121 | LIR* target = newLIR0(cUnit, kPseudoTargetLabel); |
buzbee | cbd6d44 | 2012-11-17 14:11:25 -0800 | [diff] [blame] | 122 | branchOver->target = target; |
buzbee | efc6369 | 2012-11-14 16:31:52 -0800 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | void callRuntimeHelperRegReg(CompilationUnit* cUnit, int helperOffset, |
| 126 | int arg0, int arg1, bool safepointPC); |
| 127 | /* |
| 128 | * Array data table format: |
| 129 | * ushort ident = 0x0300 magic value |
| 130 | * ushort width width of each element in the table |
| 131 | * uint size number of elements in the table |
| 132 | * ubyte data[size*width] table of data values (may contain a single-byte |
| 133 | * padding at the end) |
| 134 | * |
| 135 | * Total size is 4+(width * size + 1)/2 16-bit code units. |
| 136 | */ |
| 137 | void genFillArrayData(CompilationUnit* cUnit, uint32_t tableOffset, |
| 138 | RegLocation rlSrc) |
| 139 | { |
buzbee | eaf09bc | 2012-11-15 14:51:41 -0800 | [diff] [blame] | 140 | const uint16_t* table = cUnit->insns + cUnit->currentDalvikOffset + tableOffset; |
buzbee | efc6369 | 2012-11-14 16:31:52 -0800 | [diff] [blame] | 141 | // Add the table to the list - we'll process it later |
buzbee | cbd6d44 | 2012-11-17 14:11:25 -0800 | [diff] [blame] | 142 | FillArrayData *tabRec = |
| 143 | static_cast<FillArrayData*>(oatNew(cUnit, sizeof(FillArrayData), true, kAllocData)); |
buzbee | efc6369 | 2012-11-14 16:31:52 -0800 | [diff] [blame] | 144 | tabRec->table = table; |
| 145 | tabRec->vaddr = cUnit->currentDalvikOffset; |
buzbee | eaf09bc | 2012-11-15 14:51:41 -0800 | [diff] [blame] | 146 | uint16_t width = tabRec->table[1]; |
| 147 | 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] | 148 | tabRec->size = (size * width) + 8; |
| 149 | |
buzbee | cbd6d44 | 2012-11-17 14:11:25 -0800 | [diff] [blame] | 150 | oatInsertGrowableList(cUnit, &cUnit->fillArrayData, reinterpret_cast<uintptr_t>(tabRec)); |
buzbee | efc6369 | 2012-11-14 16:31:52 -0800 | [diff] [blame] | 151 | |
| 152 | // Making a call - use explicit registers |
| 153 | oatFlushAllRegs(cUnit); /* Everything to home location */ |
| 154 | loadValueDirectFixed(cUnit, rlSrc, rX86_ARG0); |
| 155 | // Materialize a pointer to the fill data image |
| 156 | newLIR1(cUnit, kX86StartOfMethod, rX86_ARG2); |
buzbee | cbd6d44 | 2012-11-17 14:11:25 -0800 | [diff] [blame] | 157 | newLIR2(cUnit, kX86PcRelAdr, rX86_ARG1, reinterpret_cast<uintptr_t>(tabRec)); |
buzbee | efc6369 | 2012-11-14 16:31:52 -0800 | [diff] [blame] | 158 | newLIR2(cUnit, kX86Add32RR, rX86_ARG1, rX86_ARG2); |
buzbee | cbd6d44 | 2012-11-17 14:11:25 -0800 | [diff] [blame] | 159 | callRuntimeHelperRegReg(cUnit, ENTRYPOINT_OFFSET(pHandleFillArrayDataFromCode), rX86_ARG0, |
| 160 | rX86_ARG1, true); |
buzbee | efc6369 | 2012-11-14 16:31:52 -0800 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | void genMonitorEnter(CompilationUnit* cUnit, int optFlags, RegLocation rlSrc) |
| 164 | { |
| 165 | oatFlushAllRegs(cUnit); |
| 166 | loadValueDirectFixed(cUnit, rlSrc, rCX); // Get obj |
| 167 | oatLockCallTemps(cUnit); // Prepare for explicit register usage |
| 168 | genNullCheck(cUnit, rlSrc.sRegLow, rCX, optFlags); |
| 169 | // If lock is unheld, try to grab it quickly with compare and exchange |
| 170 | // TODO: copy and clear hash state? |
| 171 | newLIR2(cUnit, kX86Mov32RT, rDX, Thread::ThinLockIdOffset().Int32Value()); |
| 172 | newLIR2(cUnit, kX86Sal32RI, rDX, LW_LOCK_OWNER_SHIFT); |
| 173 | newLIR2(cUnit, kX86Xor32RR, rAX, rAX); |
| 174 | newLIR3(cUnit, kX86LockCmpxchgMR, rCX, Object::MonitorOffset().Int32Value(), rDX); |
| 175 | LIR* branch = newLIR2(cUnit, kX86Jcc8, 0, kX86CondEq); |
| 176 | // If lock is held, go the expensive route - artLockObjectFromCode(self, obj); |
| 177 | callRuntimeHelperReg(cUnit, ENTRYPOINT_OFFSET(pLockObjectFromCode), rCX, true); |
| 178 | branch->target = newLIR0(cUnit, kPseudoTargetLabel); |
| 179 | } |
| 180 | |
| 181 | void genMonitorExit(CompilationUnit* cUnit, int optFlags, RegLocation rlSrc) |
| 182 | { |
| 183 | oatFlushAllRegs(cUnit); |
| 184 | loadValueDirectFixed(cUnit, rlSrc, rAX); // Get obj |
| 185 | oatLockCallTemps(cUnit); // Prepare for explicit register usage |
| 186 | genNullCheck(cUnit, rlSrc.sRegLow, rAX, optFlags); |
| 187 | // If lock is held by the current thread, clear it to quickly release it |
| 188 | // TODO: clear hash state? |
| 189 | newLIR2(cUnit, kX86Mov32RT, rDX, Thread::ThinLockIdOffset().Int32Value()); |
| 190 | newLIR2(cUnit, kX86Sal32RI, rDX, LW_LOCK_OWNER_SHIFT); |
| 191 | newLIR3(cUnit, kX86Mov32RM, rCX, rAX, Object::MonitorOffset().Int32Value()); |
| 192 | opRegReg(cUnit, kOpSub, rCX, rDX); |
| 193 | LIR* branch = newLIR2(cUnit, kX86Jcc8, 0, kX86CondNe); |
| 194 | newLIR3(cUnit, kX86Mov32MR, rAX, Object::MonitorOffset().Int32Value(), rCX); |
| 195 | LIR* branch2 = newLIR1(cUnit, kX86Jmp8, 0); |
| 196 | branch->target = newLIR0(cUnit, kPseudoTargetLabel); |
| 197 | // Otherwise, go the expensive route - UnlockObjectFromCode(obj); |
| 198 | callRuntimeHelperReg(cUnit, ENTRYPOINT_OFFSET(pUnlockObjectFromCode), rAX, true); |
| 199 | branch2->target = newLIR0(cUnit, kPseudoTargetLabel); |
| 200 | } |
| 201 | |
| 202 | /* |
| 203 | * Mark garbage collection card. Skip if the value we're storing is null. |
| 204 | */ |
| 205 | void markGCCard(CompilationUnit* cUnit, int valReg, int tgtAddrReg) |
| 206 | { |
| 207 | int regCardBase = oatAllocTemp(cUnit); |
| 208 | int regCardNo = oatAllocTemp(cUnit); |
| 209 | LIR* branchOver = opCmpImmBranch(cUnit, kCondEq, valReg, 0, NULL); |
| 210 | newLIR2(cUnit, kX86Mov32RT, regCardBase, Thread::CardTableOffset().Int32Value()); |
| 211 | opRegRegImm(cUnit, kOpLsr, regCardNo, tgtAddrReg, CardTable::kCardShift); |
| 212 | storeBaseIndexed(cUnit, regCardBase, regCardNo, regCardBase, 0, |
| 213 | kUnsignedByte); |
| 214 | LIR* target = newLIR0(cUnit, kPseudoTargetLabel); |
buzbee | cbd6d44 | 2012-11-17 14:11:25 -0800 | [diff] [blame] | 215 | branchOver->target = target; |
buzbee | efc6369 | 2012-11-14 16:31:52 -0800 | [diff] [blame] | 216 | oatFreeTemp(cUnit, regCardBase); |
| 217 | oatFreeTemp(cUnit, regCardNo); |
| 218 | } |
| 219 | |
| 220 | void genEntrySequence(CompilationUnit* cUnit, RegLocation* argLocs, |
| 221 | RegLocation rlMethod) |
| 222 | { |
| 223 | /* |
| 224 | * On entry, rX86_ARG0, rX86_ARG1, rX86_ARG2 are live. Let the register |
| 225 | * allocation mechanism know so it doesn't try to use any of them when |
| 226 | * expanding the frame or flushing. This leaves the utility |
| 227 | * code with no spare temps. |
| 228 | */ |
| 229 | oatLockTemp(cUnit, rX86_ARG0); |
| 230 | oatLockTemp(cUnit, rX86_ARG1); |
| 231 | oatLockTemp(cUnit, rX86_ARG2); |
| 232 | |
| 233 | /* Build frame, return address already on stack */ |
| 234 | opRegImm(cUnit, kOpSub, rX86_SP, cUnit->frameSize - 4); |
| 235 | |
| 236 | /* |
| 237 | * We can safely skip the stack overflow check if we're |
| 238 | * a leaf *and* our frame size < fudge factor. |
| 239 | */ |
| 240 | bool skipOverflowCheck = ((cUnit->attrs & METHOD_IS_LEAF) && |
buzbee | cbd6d44 | 2012-11-17 14:11:25 -0800 | [diff] [blame] | 241 | (static_cast<size_t>(cUnit->frameSize) < |
buzbee | efc6369 | 2012-11-14 16:31:52 -0800 | [diff] [blame] | 242 | Thread::kStackOverflowReservedBytes)); |
| 243 | newLIR0(cUnit, kPseudoMethodEntry); |
| 244 | /* Spill core callee saves */ |
| 245 | spillCoreRegs(cUnit); |
| 246 | /* NOTE: promotion of FP regs currently unsupported, thus no FP spill */ |
| 247 | DCHECK_EQ(cUnit->numFPSpills, 0); |
| 248 | if (!skipOverflowCheck) { |
| 249 | // cmp rX86_SP, fs:[stack_end_]; jcc throw_launchpad |
| 250 | LIR* tgt = rawLIR(cUnit, 0, kPseudoThrowTarget, kThrowStackOverflow, 0, 0, 0, 0); |
| 251 | opRegThreadMem(cUnit, kOpCmp, rX86_SP, Thread::StackEndOffset().Int32Value()); |
| 252 | opCondBranch(cUnit, kCondUlt, tgt); |
| 253 | // Remember branch target - will process later |
buzbee | cbd6d44 | 2012-11-17 14:11:25 -0800 | [diff] [blame] | 254 | oatInsertGrowableList(cUnit, &cUnit->throwLaunchpads, reinterpret_cast<uintptr_t>(tgt)); |
buzbee | efc6369 | 2012-11-14 16:31:52 -0800 | [diff] [blame] | 255 | } |
| 256 | |
| 257 | flushIns(cUnit, argLocs, rlMethod); |
| 258 | |
| 259 | oatFreeTemp(cUnit, rX86_ARG0); |
| 260 | oatFreeTemp(cUnit, rX86_ARG1); |
| 261 | oatFreeTemp(cUnit, rX86_ARG2); |
| 262 | } |
| 263 | |
| 264 | void genExitSequence(CompilationUnit* cUnit) { |
| 265 | /* |
| 266 | * In the exit path, rX86_RET0/rX86_RET1 are live - make sure they aren't |
| 267 | * allocated by the register utilities as temps. |
| 268 | */ |
| 269 | oatLockTemp(cUnit, rX86_RET0); |
| 270 | oatLockTemp(cUnit, rX86_RET1); |
| 271 | |
| 272 | newLIR0(cUnit, kPseudoMethodExit); |
| 273 | unSpillCoreRegs(cUnit); |
| 274 | /* Remove frame except for return address */ |
| 275 | opRegImm(cUnit, kOpAdd, rX86_SP, cUnit->frameSize - 4); |
| 276 | newLIR0(cUnit, kX86Ret); |
| 277 | } |
| 278 | |
| 279 | } // namespace art |