blob: c2b456f4d353e5dd4015c91858ed9d2d0d53b356 [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{
buzbeeeaf09bc2012-11-15 14:51:41 -080036 const uint16_t* table = cUnit->insns + cUnit->currentDalvikOffset + tableOffset;
buzbeeefc63692012-11-14 16:31:52 -080037 if (cUnit->printMe) {
38 dumpSparseSwitchTable(table);
39 }
40 int entries = table[1];
buzbeecbd6d442012-11-17 14:11:25 -080041 const int* keys = reinterpret_cast<const int*>(&table[2]);
42 const int* targets = &keys[entries];
buzbeeefc63692012-11-14 16:31:52 -080043 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{
buzbeeeaf09bc2012-11-15 14:51:41 -080074 const uint16_t* table = cUnit->insns + cUnit->currentDalvikOffset + tableOffset;
buzbeeefc63692012-11-14 16:31:52 -080075 if (cUnit->printMe) {
76 dumpPackedSwitchTable(table);
77 }
78 // Add the table to the list - we'll process it later
buzbeecbd6d442012-11-17 14:11:25 -080079 SwitchTable *tabRec =
80 static_cast<SwitchTable *>(oatNew(cUnit, sizeof(SwitchTable), true, kAllocData));
buzbeeefc63692012-11-14 16:31:52 -080081 tabRec->table = table;
82 tabRec->vaddr = cUnit->currentDalvikOffset;
83 int size = table[1];
buzbeecbd6d442012-11-17 14:11:25 -080084 tabRec->targets = static_cast<LIR**>(oatNew(cUnit, size * sizeof(LIR*), true, kAllocLIR));
85 oatInsertGrowableList(cUnit, &cUnit->switchTables, reinterpret_cast<uintptr_t>(tabRec));
buzbeeefc63692012-11-14 16:31:52 -080086
87 // Get the switch value
88 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
89 int startOfMethodReg = oatAllocTemp(cUnit);
90 // Materialize a pointer to the switch table
91 //newLIR0(cUnit, kX86Bkpt);
92 newLIR1(cUnit, kX86StartOfMethod, startOfMethodReg);
93 int lowKey = s4FromSwitchData(&table[2]);
94 int keyReg;
95 // Remove the bias, if necessary
96 if (lowKey == 0) {
97 keyReg = rlSrc.lowReg;
98 } else {
99 keyReg = oatAllocTemp(cUnit);
100 opRegRegImm(cUnit, kOpSub, keyReg, rlSrc.lowReg, lowKey);
101 }
102 // Bounds check - if < 0 or >= size continue following switch
103 opRegImm(cUnit, kOpCmp, keyReg, size-1);
104 LIR* branchOver = opCondBranch(cUnit, kCondHi, NULL);
105
106 // Load the displacement from the switch table
107 int dispReg = oatAllocTemp(cUnit);
108 newLIR5(cUnit, kX86PcRelLoadRA, dispReg, startOfMethodReg, keyReg, 2,
buzbeecbd6d442012-11-17 14:11:25 -0800109 reinterpret_cast<uintptr_t>(tabRec));
buzbeeefc63692012-11-14 16:31:52 -0800110 // Add displacement to start of method
111 opRegReg(cUnit, kOpAdd, startOfMethodReg, dispReg);
112 // ..and go!
113 LIR* switchBranch = newLIR1(cUnit, kX86JmpR, startOfMethodReg);
114 tabRec->anchor = switchBranch;
115
116 /* branchOver target here */
117 LIR* target = newLIR0(cUnit, kPseudoTargetLabel);
buzbeecbd6d442012-11-17 14:11:25 -0800118 branchOver->target = target;
buzbeeefc63692012-11-14 16:31:52 -0800119}
120
121void callRuntimeHelperRegReg(CompilationUnit* cUnit, int helperOffset,
122 int arg0, int arg1, bool safepointPC);
123/*
124 * Array data table format:
125 * ushort ident = 0x0300 magic value
126 * ushort width width of each element in the table
127 * uint size number of elements in the table
128 * ubyte data[size*width] table of data values (may contain a single-byte
129 * padding at the end)
130 *
131 * Total size is 4+(width * size + 1)/2 16-bit code units.
132 */
133void genFillArrayData(CompilationUnit* cUnit, uint32_t tableOffset,
134 RegLocation rlSrc)
135{
buzbeeeaf09bc2012-11-15 14:51:41 -0800136 const uint16_t* table = cUnit->insns + cUnit->currentDalvikOffset + tableOffset;
buzbeeefc63692012-11-14 16:31:52 -0800137 // Add the table to the list - we'll process it later
buzbeecbd6d442012-11-17 14:11:25 -0800138 FillArrayData *tabRec =
139 static_cast<FillArrayData*>(oatNew(cUnit, sizeof(FillArrayData), true, kAllocData));
buzbeeefc63692012-11-14 16:31:52 -0800140 tabRec->table = table;
141 tabRec->vaddr = cUnit->currentDalvikOffset;
buzbeeeaf09bc2012-11-15 14:51:41 -0800142 uint16_t width = tabRec->table[1];
143 uint32_t size = tabRec->table[2] | ((static_cast<uint32_t>(tabRec->table[3])) << 16);
buzbeeefc63692012-11-14 16:31:52 -0800144 tabRec->size = (size * width) + 8;
145
buzbeecbd6d442012-11-17 14:11:25 -0800146 oatInsertGrowableList(cUnit, &cUnit->fillArrayData, reinterpret_cast<uintptr_t>(tabRec));
buzbeeefc63692012-11-14 16:31:52 -0800147
148 // Making a call - use explicit registers
149 oatFlushAllRegs(cUnit); /* Everything to home location */
150 loadValueDirectFixed(cUnit, rlSrc, rX86_ARG0);
151 // Materialize a pointer to the fill data image
152 newLIR1(cUnit, kX86StartOfMethod, rX86_ARG2);
buzbeecbd6d442012-11-17 14:11:25 -0800153 newLIR2(cUnit, kX86PcRelAdr, rX86_ARG1, reinterpret_cast<uintptr_t>(tabRec));
buzbeeefc63692012-11-14 16:31:52 -0800154 newLIR2(cUnit, kX86Add32RR, rX86_ARG1, rX86_ARG2);
buzbeecbd6d442012-11-17 14:11:25 -0800155 callRuntimeHelperRegReg(cUnit, ENTRYPOINT_OFFSET(pHandleFillArrayDataFromCode), rX86_ARG0,
156 rX86_ARG1, true);
buzbeeefc63692012-11-14 16:31:52 -0800157}
158
159void genMonitorEnter(CompilationUnit* cUnit, int optFlags, RegLocation rlSrc)
160{
161 oatFlushAllRegs(cUnit);
162 loadValueDirectFixed(cUnit, rlSrc, rCX); // Get obj
163 oatLockCallTemps(cUnit); // Prepare for explicit register usage
164 genNullCheck(cUnit, rlSrc.sRegLow, rCX, optFlags);
165 // If lock is unheld, try to grab it quickly with compare and exchange
166 // TODO: copy and clear hash state?
167 newLIR2(cUnit, kX86Mov32RT, rDX, Thread::ThinLockIdOffset().Int32Value());
168 newLIR2(cUnit, kX86Sal32RI, rDX, LW_LOCK_OWNER_SHIFT);
169 newLIR2(cUnit, kX86Xor32RR, rAX, rAX);
170 newLIR3(cUnit, kX86LockCmpxchgMR, rCX, Object::MonitorOffset().Int32Value(), rDX);
171 LIR* branch = newLIR2(cUnit, kX86Jcc8, 0, kX86CondEq);
172 // If lock is held, go the expensive route - artLockObjectFromCode(self, obj);
173 callRuntimeHelperReg(cUnit, ENTRYPOINT_OFFSET(pLockObjectFromCode), rCX, true);
174 branch->target = newLIR0(cUnit, kPseudoTargetLabel);
175}
176
177void genMonitorExit(CompilationUnit* cUnit, int optFlags, RegLocation rlSrc)
178{
179 oatFlushAllRegs(cUnit);
180 loadValueDirectFixed(cUnit, rlSrc, rAX); // Get obj
181 oatLockCallTemps(cUnit); // Prepare for explicit register usage
182 genNullCheck(cUnit, rlSrc.sRegLow, rAX, optFlags);
183 // If lock is held by the current thread, clear it to quickly release it
184 // TODO: clear hash state?
185 newLIR2(cUnit, kX86Mov32RT, rDX, Thread::ThinLockIdOffset().Int32Value());
186 newLIR2(cUnit, kX86Sal32RI, rDX, LW_LOCK_OWNER_SHIFT);
187 newLIR3(cUnit, kX86Mov32RM, rCX, rAX, Object::MonitorOffset().Int32Value());
188 opRegReg(cUnit, kOpSub, rCX, rDX);
189 LIR* branch = newLIR2(cUnit, kX86Jcc8, 0, kX86CondNe);
190 newLIR3(cUnit, kX86Mov32MR, rAX, Object::MonitorOffset().Int32Value(), rCX);
191 LIR* branch2 = newLIR1(cUnit, kX86Jmp8, 0);
192 branch->target = newLIR0(cUnit, kPseudoTargetLabel);
193 // Otherwise, go the expensive route - UnlockObjectFromCode(obj);
194 callRuntimeHelperReg(cUnit, ENTRYPOINT_OFFSET(pUnlockObjectFromCode), rAX, true);
195 branch2->target = newLIR0(cUnit, kPseudoTargetLabel);
196}
197
198/*
199 * Mark garbage collection card. Skip if the value we're storing is null.
200 */
201void markGCCard(CompilationUnit* cUnit, int valReg, int tgtAddrReg)
202{
203 int regCardBase = oatAllocTemp(cUnit);
204 int regCardNo = oatAllocTemp(cUnit);
205 LIR* branchOver = opCmpImmBranch(cUnit, kCondEq, valReg, 0, NULL);
206 newLIR2(cUnit, kX86Mov32RT, regCardBase, Thread::CardTableOffset().Int32Value());
207 opRegRegImm(cUnit, kOpLsr, regCardNo, tgtAddrReg, CardTable::kCardShift);
208 storeBaseIndexed(cUnit, regCardBase, regCardNo, regCardBase, 0,
209 kUnsignedByte);
210 LIR* target = newLIR0(cUnit, kPseudoTargetLabel);
buzbeecbd6d442012-11-17 14:11:25 -0800211 branchOver->target = target;
buzbeeefc63692012-11-14 16:31:52 -0800212 oatFreeTemp(cUnit, regCardBase);
213 oatFreeTemp(cUnit, regCardNo);
214}
215
216void genEntrySequence(CompilationUnit* cUnit, RegLocation* argLocs,
217 RegLocation rlMethod)
218{
219 /*
220 * On entry, rX86_ARG0, rX86_ARG1, rX86_ARG2 are live. Let the register
221 * allocation mechanism know so it doesn't try to use any of them when
222 * expanding the frame or flushing. This leaves the utility
223 * code with no spare temps.
224 */
225 oatLockTemp(cUnit, rX86_ARG0);
226 oatLockTemp(cUnit, rX86_ARG1);
227 oatLockTemp(cUnit, rX86_ARG2);
228
229 /* Build frame, return address already on stack */
230 opRegImm(cUnit, kOpSub, rX86_SP, cUnit->frameSize - 4);
231
232 /*
233 * We can safely skip the stack overflow check if we're
234 * a leaf *and* our frame size < fudge factor.
235 */
236 bool skipOverflowCheck = ((cUnit->attrs & METHOD_IS_LEAF) &&
buzbeecbd6d442012-11-17 14:11:25 -0800237 (static_cast<size_t>(cUnit->frameSize) <
buzbeeefc63692012-11-14 16:31:52 -0800238 Thread::kStackOverflowReservedBytes));
239 newLIR0(cUnit, kPseudoMethodEntry);
240 /* Spill core callee saves */
241 spillCoreRegs(cUnit);
242 /* NOTE: promotion of FP regs currently unsupported, thus no FP spill */
243 DCHECK_EQ(cUnit->numFPSpills, 0);
244 if (!skipOverflowCheck) {
245 // cmp rX86_SP, fs:[stack_end_]; jcc throw_launchpad
246 LIR* tgt = rawLIR(cUnit, 0, kPseudoThrowTarget, kThrowStackOverflow, 0, 0, 0, 0);
247 opRegThreadMem(cUnit, kOpCmp, rX86_SP, Thread::StackEndOffset().Int32Value());
248 opCondBranch(cUnit, kCondUlt, tgt);
249 // Remember branch target - will process later
buzbeecbd6d442012-11-17 14:11:25 -0800250 oatInsertGrowableList(cUnit, &cUnit->throwLaunchpads, reinterpret_cast<uintptr_t>(tgt));
buzbeeefc63692012-11-14 16:31:52 -0800251 }
252
253 flushIns(cUnit, argLocs, rlMethod);
254
255 oatFreeTemp(cUnit, rX86_ARG0);
256 oatFreeTemp(cUnit, rX86_ARG1);
257 oatFreeTemp(cUnit, rX86_ARG2);
258}
259
260void genExitSequence(CompilationUnit* cUnit) {
261 /*
262 * In the exit path, rX86_RET0/rX86_RET1 are live - make sure they aren't
263 * allocated by the register utilities as temps.
264 */
265 oatLockTemp(cUnit, rX86_RET0);
266 oatLockTemp(cUnit, rX86_RET1);
267
268 newLIR0(cUnit, kPseudoMethodExit);
269 unSpillCoreRegs(cUnit);
270 /* Remove frame except for return address */
271 opRegImm(cUnit, kOpAdd, rX86_SP, cUnit->frameSize - 4);
272 newLIR0(cUnit, kX86Ret);
273}
274
275} // namespace art