blob: 7ada136e201bc1324de7e51a5a203064e5825b99 [file] [log] [blame]
buzbeeefc63692012-11-14 16:31:52 -08001/*
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
buzbee1bc37c62012-11-20 13:35:41 -080019#include "x86_lir.h"
20#include "../codegen_util.h"
21#include "../ralloc_util.h"
22
buzbeeefc63692012-11-14 16:31:52 -080023namespace art {
24
buzbee52a77fc2012-11-20 19:50:46 -080025void GenSpecialCase(CompilationUnit* cUnit, BasicBlock* bb, MIR* mir,
buzbeeefc63692012-11-14 16:31:52 -080026 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 */
buzbee52a77fc2012-11-20 19:50:46 -080035BasicBlock *FindBlock(CompilationUnit* cUnit, unsigned int codeOffset,
buzbeeefc63692012-11-14 16:31:52 -080036 bool split, bool create, BasicBlock** immedPredBlockP);
buzbee52a77fc2012-11-20 19:50:46 -080037void GenSparseSwitch(CompilationUnit* cUnit, uint32_t tableOffset,
buzbeeefc63692012-11-14 16:31:52 -080038 RegLocation rlSrc)
39{
buzbeeeaf09bc2012-11-15 14:51:41 -080040 const uint16_t* table = cUnit->insns + cUnit->currentDalvikOffset + tableOffset;
buzbeeefc63692012-11-14 16:31:52 -080041 if (cUnit->printMe) {
buzbee52a77fc2012-11-20 19:50:46 -080042 DumpSparseSwitchTable(table);
buzbeeefc63692012-11-14 16:31:52 -080043 }
44 int entries = table[1];
buzbeecbd6d442012-11-17 14:11:25 -080045 const int* keys = reinterpret_cast<const int*>(&table[2]);
46 const int* targets = &keys[entries];
buzbee52a77fc2012-11-20 19:50:46 -080047 rlSrc = LoadValue(cUnit, rlSrc, kCoreReg);
buzbeeefc63692012-11-14 16:31:52 -080048 for (int i = 0; i < entries; i++) {
49 int key = keys[i];
buzbee52a77fc2012-11-20 19:50:46 -080050 BasicBlock* case_block = FindBlock(cUnit,
buzbeeefc63692012-11-14 16:31:52 -080051 cUnit->currentDalvikOffset + targets[i],
52 false, false, NULL);
53 LIR* labelList = cUnit->blockLabelList;
buzbee52a77fc2012-11-20 19:50:46 -080054 OpCmpImmBranch(cUnit, kCondEq, rlSrc.lowReg, key,
buzbeeefc63692012-11-14 16:31:52 -080055 &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 */
buzbee52a77fc2012-11-20 19:50:46 -080075void GenPackedSwitch(CompilationUnit* cUnit, uint32_t tableOffset,
buzbeeefc63692012-11-14 16:31:52 -080076 RegLocation rlSrc)
77{
buzbeeeaf09bc2012-11-15 14:51:41 -080078 const uint16_t* table = cUnit->insns + cUnit->currentDalvikOffset + tableOffset;
buzbeeefc63692012-11-14 16:31:52 -080079 if (cUnit->printMe) {
buzbee52a77fc2012-11-20 19:50:46 -080080 DumpPackedSwitchTable(table);
buzbeeefc63692012-11-14 16:31:52 -080081 }
82 // Add the table to the list - we'll process it later
buzbeecbd6d442012-11-17 14:11:25 -080083 SwitchTable *tabRec =
buzbee52a77fc2012-11-20 19:50:46 -080084 static_cast<SwitchTable *>(NewMem(cUnit, sizeof(SwitchTable), true, kAllocData));
buzbeeefc63692012-11-14 16:31:52 -080085 tabRec->table = table;
86 tabRec->vaddr = cUnit->currentDalvikOffset;
87 int size = table[1];
buzbee52a77fc2012-11-20 19:50:46 -080088 tabRec->targets = static_cast<LIR**>(NewMem(cUnit, size * sizeof(LIR*), true, kAllocLIR));
89 InsertGrowableList(cUnit, &cUnit->switchTables, reinterpret_cast<uintptr_t>(tabRec));
buzbeeefc63692012-11-14 16:31:52 -080090
91 // Get the switch value
buzbee52a77fc2012-11-20 19:50:46 -080092 rlSrc = LoadValue(cUnit, rlSrc, kCoreReg);
93 int startOfMethodReg = AllocTemp(cUnit);
buzbeeefc63692012-11-14 16:31:52 -080094 // Materialize a pointer to the switch table
buzbee52a77fc2012-11-20 19:50:46 -080095 //NewLIR0(cUnit, kX86Bkpt);
96 NewLIR1(cUnit, kX86StartOfMethod, startOfMethodReg);
buzbeeefc63692012-11-14 16:31:52 -080097 int lowKey = s4FromSwitchData(&table[2]);
98 int keyReg;
99 // Remove the bias, if necessary
100 if (lowKey == 0) {
101 keyReg = rlSrc.lowReg;
102 } else {
buzbee52a77fc2012-11-20 19:50:46 -0800103 keyReg = AllocTemp(cUnit);
104 OpRegRegImm(cUnit, kOpSub, keyReg, rlSrc.lowReg, lowKey);
buzbeeefc63692012-11-14 16:31:52 -0800105 }
106 // Bounds check - if < 0 or >= size continue following switch
buzbee52a77fc2012-11-20 19:50:46 -0800107 OpRegImm(cUnit, kOpCmp, keyReg, size-1);
108 LIR* branchOver = OpCondBranch(cUnit, kCondHi, NULL);
buzbeeefc63692012-11-14 16:31:52 -0800109
110 // Load the displacement from the switch table
buzbee52a77fc2012-11-20 19:50:46 -0800111 int dispReg = AllocTemp(cUnit);
112 NewLIR5(cUnit, kX86PcRelLoadRA, dispReg, startOfMethodReg, keyReg, 2,
buzbeecbd6d442012-11-17 14:11:25 -0800113 reinterpret_cast<uintptr_t>(tabRec));
buzbeeefc63692012-11-14 16:31:52 -0800114 // Add displacement to start of method
buzbee52a77fc2012-11-20 19:50:46 -0800115 OpRegReg(cUnit, kOpAdd, startOfMethodReg, dispReg);
buzbeeefc63692012-11-14 16:31:52 -0800116 // ..and go!
buzbee52a77fc2012-11-20 19:50:46 -0800117 LIR* switchBranch = NewLIR1(cUnit, kX86JmpR, startOfMethodReg);
buzbeeefc63692012-11-14 16:31:52 -0800118 tabRec->anchor = switchBranch;
119
120 /* branchOver target here */
buzbee52a77fc2012-11-20 19:50:46 -0800121 LIR* target = NewLIR0(cUnit, kPseudoTargetLabel);
buzbeecbd6d442012-11-17 14:11:25 -0800122 branchOver->target = target;
buzbeeefc63692012-11-14 16:31:52 -0800123}
124
buzbee52a77fc2012-11-20 19:50:46 -0800125void CallRuntimeHelperRegReg(CompilationUnit* cUnit, int helperOffset,
buzbeeefc63692012-11-14 16:31:52 -0800126 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 */
buzbee52a77fc2012-11-20 19:50:46 -0800137void GenFillArrayData(CompilationUnit* cUnit, uint32_t tableOffset,
buzbeeefc63692012-11-14 16:31:52 -0800138 RegLocation rlSrc)
139{
buzbeeeaf09bc2012-11-15 14:51:41 -0800140 const uint16_t* table = cUnit->insns + cUnit->currentDalvikOffset + tableOffset;
buzbeeefc63692012-11-14 16:31:52 -0800141 // Add the table to the list - we'll process it later
buzbeecbd6d442012-11-17 14:11:25 -0800142 FillArrayData *tabRec =
buzbee52a77fc2012-11-20 19:50:46 -0800143 static_cast<FillArrayData*>(NewMem(cUnit, sizeof(FillArrayData), true, kAllocData));
buzbeeefc63692012-11-14 16:31:52 -0800144 tabRec->table = table;
145 tabRec->vaddr = cUnit->currentDalvikOffset;
buzbeeeaf09bc2012-11-15 14:51:41 -0800146 uint16_t width = tabRec->table[1];
147 uint32_t size = tabRec->table[2] | ((static_cast<uint32_t>(tabRec->table[3])) << 16);
buzbeeefc63692012-11-14 16:31:52 -0800148 tabRec->size = (size * width) + 8;
149
buzbee52a77fc2012-11-20 19:50:46 -0800150 InsertGrowableList(cUnit, &cUnit->fillArrayData, reinterpret_cast<uintptr_t>(tabRec));
buzbeeefc63692012-11-14 16:31:52 -0800151
152 // Making a call - use explicit registers
buzbee52a77fc2012-11-20 19:50:46 -0800153 FlushAllRegs(cUnit); /* Everything to home location */
154 LoadValueDirectFixed(cUnit, rlSrc, rX86_ARG0);
buzbeeefc63692012-11-14 16:31:52 -0800155 // Materialize a pointer to the fill data image
buzbee52a77fc2012-11-20 19:50:46 -0800156 NewLIR1(cUnit, kX86StartOfMethod, rX86_ARG2);
157 NewLIR2(cUnit, kX86PcRelAdr, rX86_ARG1, reinterpret_cast<uintptr_t>(tabRec));
158 NewLIR2(cUnit, kX86Add32RR, rX86_ARG1, rX86_ARG2);
159 CallRuntimeHelperRegReg(cUnit, ENTRYPOINT_OFFSET(pHandleFillArrayDataFromCode), rX86_ARG0,
buzbeecbd6d442012-11-17 14:11:25 -0800160 rX86_ARG1, true);
buzbeeefc63692012-11-14 16:31:52 -0800161}
162
buzbee52a77fc2012-11-20 19:50:46 -0800163void GenMonitorEnter(CompilationUnit* cUnit, int optFlags, RegLocation rlSrc)
buzbeeefc63692012-11-14 16:31:52 -0800164{
buzbee52a77fc2012-11-20 19:50:46 -0800165 FlushAllRegs(cUnit);
166 LoadValueDirectFixed(cUnit, rlSrc, rCX); // Get obj
167 LockCallTemps(cUnit); // Prepare for explicit register usage
168 GenNullCheck(cUnit, rlSrc.sRegLow, rCX, optFlags);
buzbeeefc63692012-11-14 16:31:52 -0800169 // If lock is unheld, try to grab it quickly with compare and exchange
170 // TODO: copy and clear hash state?
buzbee52a77fc2012-11-20 19:50:46 -0800171 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);
buzbeeefc63692012-11-14 16:31:52 -0800176 // If lock is held, go the expensive route - artLockObjectFromCode(self, obj);
buzbee52a77fc2012-11-20 19:50:46 -0800177 CallRuntimeHelperReg(cUnit, ENTRYPOINT_OFFSET(pLockObjectFromCode), rCX, true);
178 branch->target = NewLIR0(cUnit, kPseudoTargetLabel);
buzbeeefc63692012-11-14 16:31:52 -0800179}
180
buzbee52a77fc2012-11-20 19:50:46 -0800181void GenMonitorExit(CompilationUnit* cUnit, int optFlags, RegLocation rlSrc)
buzbeeefc63692012-11-14 16:31:52 -0800182{
buzbee52a77fc2012-11-20 19:50:46 -0800183 FlushAllRegs(cUnit);
184 LoadValueDirectFixed(cUnit, rlSrc, rAX); // Get obj
185 LockCallTemps(cUnit); // Prepare for explicit register usage
186 GenNullCheck(cUnit, rlSrc.sRegLow, rAX, optFlags);
buzbeeefc63692012-11-14 16:31:52 -0800187 // If lock is held by the current thread, clear it to quickly release it
188 // TODO: clear hash state?
buzbee52a77fc2012-11-20 19:50:46 -0800189 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);
buzbeeefc63692012-11-14 16:31:52 -0800197 // Otherwise, go the expensive route - UnlockObjectFromCode(obj);
buzbee52a77fc2012-11-20 19:50:46 -0800198 CallRuntimeHelperReg(cUnit, ENTRYPOINT_OFFSET(pUnlockObjectFromCode), rAX, true);
199 branch2->target = NewLIR0(cUnit, kPseudoTargetLabel);
buzbeeefc63692012-11-14 16:31:52 -0800200}
201
202/*
203 * Mark garbage collection card. Skip if the value we're storing is null.
204 */
buzbee52a77fc2012-11-20 19:50:46 -0800205void MarkGCCard(CompilationUnit* cUnit, int valReg, int tgtAddrReg)
buzbeeefc63692012-11-14 16:31:52 -0800206{
buzbee52a77fc2012-11-20 19:50:46 -0800207 int regCardBase = AllocTemp(cUnit);
208 int regCardNo = AllocTemp(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,
buzbeeefc63692012-11-14 16:31:52 -0800213 kUnsignedByte);
buzbee52a77fc2012-11-20 19:50:46 -0800214 LIR* target = NewLIR0(cUnit, kPseudoTargetLabel);
buzbeecbd6d442012-11-17 14:11:25 -0800215 branchOver->target = target;
buzbee52a77fc2012-11-20 19:50:46 -0800216 FreeTemp(cUnit, regCardBase);
217 FreeTemp(cUnit, regCardNo);
buzbeeefc63692012-11-14 16:31:52 -0800218}
219
buzbee52a77fc2012-11-20 19:50:46 -0800220void GenEntrySequence(CompilationUnit* cUnit, RegLocation* ArgLocs,
buzbeeefc63692012-11-14 16:31:52 -0800221 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 */
buzbee52a77fc2012-11-20 19:50:46 -0800229 LockTemp(cUnit, rX86_ARG0);
230 LockTemp(cUnit, rX86_ARG1);
231 LockTemp(cUnit, rX86_ARG2);
buzbeeefc63692012-11-14 16:31:52 -0800232
233 /* Build frame, return address already on stack */
buzbee52a77fc2012-11-20 19:50:46 -0800234 OpRegImm(cUnit, kOpSub, rX86_SP, cUnit->frameSize - 4);
buzbeeefc63692012-11-14 16:31:52 -0800235
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) &&
buzbeecbd6d442012-11-17 14:11:25 -0800241 (static_cast<size_t>(cUnit->frameSize) <
buzbeeefc63692012-11-14 16:31:52 -0800242 Thread::kStackOverflowReservedBytes));
buzbee52a77fc2012-11-20 19:50:46 -0800243 NewLIR0(cUnit, kPseudoMethodEntry);
buzbeeefc63692012-11-14 16:31:52 -0800244 /* Spill core callee saves */
buzbee52a77fc2012-11-20 19:50:46 -0800245 SpillCoreRegs(cUnit);
buzbeeefc63692012-11-14 16:31:52 -0800246 /* 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
buzbee52a77fc2012-11-20 19:50:46 -0800250 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);
buzbeeefc63692012-11-14 16:31:52 -0800253 // Remember branch target - will process later
buzbee52a77fc2012-11-20 19:50:46 -0800254 InsertGrowableList(cUnit, &cUnit->throwLaunchpads, reinterpret_cast<uintptr_t>(tgt));
buzbeeefc63692012-11-14 16:31:52 -0800255 }
256
buzbee52a77fc2012-11-20 19:50:46 -0800257 FlushIns(cUnit, ArgLocs, rlMethod);
buzbeeefc63692012-11-14 16:31:52 -0800258
buzbee52a77fc2012-11-20 19:50:46 -0800259 FreeTemp(cUnit, rX86_ARG0);
260 FreeTemp(cUnit, rX86_ARG1);
261 FreeTemp(cUnit, rX86_ARG2);
buzbeeefc63692012-11-14 16:31:52 -0800262}
263
buzbee52a77fc2012-11-20 19:50:46 -0800264void GenExitSequence(CompilationUnit* cUnit) {
buzbeeefc63692012-11-14 16:31:52 -0800265 /*
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 */
buzbee52a77fc2012-11-20 19:50:46 -0800269 LockTemp(cUnit, rX86_RET0);
270 LockTemp(cUnit, rX86_RET1);
buzbeeefc63692012-11-14 16:31:52 -0800271
buzbee52a77fc2012-11-20 19:50:46 -0800272 NewLIR0(cUnit, kPseudoMethodExit);
273 UnSpillCoreRegs(cUnit);
buzbeeefc63692012-11-14 16:31:52 -0800274 /* Remove frame except for return address */
buzbee52a77fc2012-11-20 19:50:46 -0800275 OpRegImm(cUnit, kOpAdd, rX86_SP, cUnit->frameSize - 4);
276 NewLIR0(cUnit, kX86Ret);
buzbeeefc63692012-11-14 16:31:52 -0800277}
278
279} // namespace art