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