blob: 62ff3ad7b40a59b2830fd8981e8e12e41290e6c2 [file] [log] [blame]
buzbeee88dfbf2012-03-05 11:19:57 -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/*
buzbeea7678db2012-03-05 15:35:46 -080018 * This file contains codegen for the X86 ISA and is intended to be
buzbeee88dfbf2012-03-05 11:19:57 -080019 * includes by:
20 *
21 * Codegen-$(TARGET_ARCH_VARIANT).c
22 *
23 */
24
25namespace art {
26
buzbee16da88c2012-03-20 10:38:17 -070027void genSpecialCase(CompilationUnit* cUnit, BasicBlock* bb, MIR* mir,
28 SpecialCaseHandler specialCase)
29{
Bill Buzbeea114add2012-05-03 15:00:40 -070030 // TODO
buzbee16da88c2012-03-20 10:38:17 -070031}
32
buzbeee88dfbf2012-03-05 11:19:57 -080033/*
Ian Rogersb5d09b22012-03-06 22:14:17 -080034 * Perform register memory operation.
35 */
36LIR* genRegMemCheck(CompilationUnit* cUnit, ConditionCode cCode,
37 int reg1, int base, int offset, MIR* mir, ThrowKind kind)
38{
Bill Buzbeea114add2012-05-03 15:00:40 -070039 LIR* tgt = rawLIR(cUnit, 0, kPseudoThrowTarget, kind,
40 mir ? mir->offset : 0, reg1, base, offset);
41 opRegMem(cUnit, kOpCmp, reg1, base, offset);
42 LIR* branch = opCondBranch(cUnit, cCode, tgt);
43 // Remember branch target - will process later
44 oatInsertGrowableList(cUnit, &cUnit->throwLaunchpads, (intptr_t)tgt);
45 return branch;
Ian Rogersb5d09b22012-03-06 22:14:17 -080046}
47
48/*
Ian Rogers55bd45f2012-04-04 17:31:20 -070049 * The sparse table in the literal pool is an array of <key,displacement>
50 * pairs.
buzbeee88dfbf2012-03-05 11:19:57 -080051 */
Ian Rogers55bd45f2012-04-04 17:31:20 -070052BasicBlock *findBlock(CompilationUnit* cUnit, unsigned int codeOffset,
53 bool split, bool create, BasicBlock** immedPredBlockP);
Bill Buzbeea114add2012-05-03 15:00:40 -070054void genSparseSwitch(CompilationUnit* cUnit, MIR* mir, RegLocation rlSrc,
55 LIR* labelList)
56{
Ian Rogers55bd45f2012-04-04 17:31:20 -070057 const u2* table = cUnit->insns + mir->offset + mir->dalvikInsn.vB;
58 if (cUnit->printMe) {
59 dumpSparseSwitchTable(table);
60 }
61 int entries = table[1];
62 int* keys = (int*)&table[2];
63 int* targets = &keys[entries];
64 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
65 for (int i = 0; i < entries; i++) {
66 int key = keys[i];
67 BasicBlock* case_block = findBlock(cUnit, mir->offset + targets[i],
68 false, false, NULL);
Bill Buzbeea114add2012-05-03 15:00:40 -070069 opCmpImmBranch(cUnit, kCondEq, rlSrc.lowReg, key,
70 &labelList[case_block->id]);
Ian Rogers55bd45f2012-04-04 17:31:20 -070071 }
buzbeee88dfbf2012-03-05 11:19:57 -080072}
73
74/*
75 * Code pattern will look something like:
76 *
Ian Rogers55bd45f2012-04-04 17:31:20 -070077 * mov rVal, ..
78 * call 0
79 * pop rStartOfMethod
80 * sub rStartOfMethod, ..
81 * mov rKeyReg, rVal
82 * sub rKeyReg, lowKey
83 * cmp rKeyReg, size-1 ; bound check
84 * ja done
85 * mov rDisp, [rStartOfMethod + rKeyReg * 4 + tableOffset]
86 * add rStartOfMethod, rDisp
87 * jmp rStartOfMethod
buzbeee88dfbf2012-03-05 11:19:57 -080088 * done:
89 */
Bill Buzbeea114add2012-05-03 15:00:40 -070090void genPackedSwitch(CompilationUnit* cUnit, MIR* mir, RegLocation rlSrc)
91{
Ian Rogers7caad772012-03-30 01:07:54 -070092 const u2* table = cUnit->insns + mir->offset + mir->dalvikInsn.vB;
93 if (cUnit->printMe) {
94 dumpPackedSwitchTable(table);
95 }
96 // Add the table to the list - we'll process it later
97 SwitchTable *tabRec = (SwitchTable *)oatNew(cUnit, sizeof(SwitchTable),
98 true, kAllocData);
99 tabRec->table = table;
100 tabRec->vaddr = mir->offset;
101 int size = table[1];
102 tabRec->targets = (LIR* *)oatNew(cUnit, size * sizeof(LIR*), true,
103 kAllocLIR);
104 oatInsertGrowableList(cUnit, &cUnit->switchTables, (intptr_t)tabRec);
buzbeee88dfbf2012-03-05 11:19:57 -0800105
Ian Rogers7caad772012-03-30 01:07:54 -0700106 // Get the switch value
107 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
108 int startOfMethodReg = oatAllocTemp(cUnit);
109 // Materialize a pointer to the switch table
110 //newLIR0(cUnit, kX86Bkpt);
111 newLIR1(cUnit, kX86StartOfMethod, startOfMethodReg);
112 int lowKey = s4FromSwitchData(&table[2]);
113 int keyReg;
114 // Remove the bias, if necessary
115 if (lowKey == 0) {
116 keyReg = rlSrc.lowReg;
117 } else {
118 keyReg = oatAllocTemp(cUnit);
119 opRegRegImm(cUnit, kOpSub, keyReg, rlSrc.lowReg, lowKey);
120 }
121 // Bounds check - if < 0 or >= size continue following switch
122 opRegImm(cUnit, kOpCmp, keyReg, size-1);
123 LIR* branchOver = opCondBranch(cUnit, kCondHi, NULL);
buzbeee88dfbf2012-03-05 11:19:57 -0800124
Ian Rogers7caad772012-03-30 01:07:54 -0700125 // Load the displacement from the switch table
126 int dispReg = oatAllocTemp(cUnit);
Bill Buzbeea114add2012-05-03 15:00:40 -0700127 newLIR5(cUnit, kX86PcRelLoadRA, dispReg, startOfMethodReg, keyReg, 2,
128 (intptr_t)tabRec);
Ian Rogers7caad772012-03-30 01:07:54 -0700129 // Add displacement to start of method
130 opRegReg(cUnit, kOpAdd, startOfMethodReg, dispReg);
131 // ..and go!
132 LIR* switchBranch = newLIR1(cUnit, kX86JmpR, startOfMethodReg);
133 tabRec->anchor = switchBranch;
buzbeee88dfbf2012-03-05 11:19:57 -0800134
Ian Rogers7caad772012-03-30 01:07:54 -0700135 /* branchOver target here */
136 LIR* target = newLIR0(cUnit, kPseudoTargetLabel);
137 branchOver->target = (LIR*)target;
buzbeee88dfbf2012-03-05 11:19:57 -0800138}
139
Bill Buzbeea114add2012-05-03 15:00:40 -0700140void callRuntimeHelperRegReg(CompilationUnit* cUnit, int helperOffset,
141 int arg0, int arg1);
buzbeee88dfbf2012-03-05 11:19:57 -0800142/*
143 * Array data table format:
144 * ushort ident = 0x0300 magic value
145 * ushort width width of each element in the table
146 * uint size number of elements in the table
147 * ubyte data[size*width] table of data values (may contain a single-byte
148 * padding at the end)
149 *
150 * Total size is 4+(width * size + 1)/2 16-bit code units.
151 */
152void genFillArrayData(CompilationUnit* cUnit, MIR* mir, RegLocation rlSrc)
153{
Ian Rogers7caad772012-03-30 01:07:54 -0700154 const u2* table = cUnit->insns + mir->offset + mir->dalvikInsn.vB;
155 // Add the table to the list - we'll process it later
Bill Buzbeea114add2012-05-03 15:00:40 -0700156 FillArrayData *tabRec = (FillArrayData *)oatNew(cUnit, sizeof(FillArrayData),
157 true, kAllocData);
Ian Rogers7caad772012-03-30 01:07:54 -0700158 tabRec->table = table;
159 tabRec->vaddr = mir->offset;
160 u2 width = tabRec->table[1];
161 u4 size = tabRec->table[2] | (((u4)tabRec->table[3]) << 16);
162 tabRec->size = (size * width) + 8;
buzbeee88dfbf2012-03-05 11:19:57 -0800163
Ian Rogers7caad772012-03-30 01:07:54 -0700164 oatInsertGrowableList(cUnit, &cUnit->fillArrayData, (intptr_t)tabRec);
buzbeee88dfbf2012-03-05 11:19:57 -0800165
Ian Rogers7caad772012-03-30 01:07:54 -0700166 // Making a call - use explicit registers
167 oatFlushAllRegs(cUnit); /* Everything to home location */
168 loadValueDirectFixed(cUnit, rlSrc, rARG0);
169 // Materialize a pointer to the fill data image
170 newLIR1(cUnit, kX86StartOfMethod, rARG2);
171 newLIR2(cUnit, kX86PcRelAdr, rARG1, (intptr_t)tabRec);
172 newLIR2(cUnit, kX86Add32RR, rARG1, rARG2);
Bill Buzbeea114add2012-05-03 15:00:40 -0700173 callRuntimeHelperRegReg(cUnit,
174 ENTRYPOINT_OFFSET(pHandleFillArrayDataFromCode),
175 rARG0, rARG1);
buzbeee88dfbf2012-03-05 11:19:57 -0800176}
177
178void genNegFloat(CompilationUnit *cUnit, RegLocation rlDest, RegLocation rlSrc)
179{
Bill Buzbeea114add2012-05-03 15:00:40 -0700180 UNIMPLEMENTED(WARNING) << "genNegFloat "
181 << PrettyMethod(cUnit->method_idx, *cUnit->dex_file);
182 newLIR0(cUnit, kX86Bkpt);
buzbeea7678db2012-03-05 15:35:46 -0800183#if 0
Bill Buzbeea114add2012-05-03 15:00:40 -0700184 RegLocation rlResult;
185 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
186 rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
187 opRegRegImm(cUnit, kOpAdd, rlResult.lowReg, rlSrc.lowReg, 0x80000000);
188 storeValue(cUnit, rlDest, rlResult);
buzbeee88dfbf2012-03-05 11:19:57 -0800189#endif
190}
191
192void genNegDouble(CompilationUnit *cUnit, RegLocation rlDest, RegLocation rlSrc)
193{
Bill Buzbeea114add2012-05-03 15:00:40 -0700194 UNIMPLEMENTED(WARNING) << "genNegDouble"
195 << PrettyMethod(cUnit->method_idx, *cUnit->dex_file);
196 newLIR0(cUnit, kX86Bkpt);
buzbeee88dfbf2012-03-05 11:19:57 -0800197#if 0
Bill Buzbeea114add2012-05-03 15:00:40 -0700198 RegLocation rlResult;
199 rlSrc = loadValueWide(cUnit, rlSrc, kCoreReg);
200 rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
201 opRegRegImm(cUnit, kOpAdd, rlResult.highReg, rlSrc.highReg, 0x80000000);
202 opRegCopy(cUnit, rlResult.lowReg, rlSrc.lowReg);
203 storeValueWide(cUnit, rlDest, rlResult);
buzbeee88dfbf2012-03-05 11:19:57 -0800204#endif
205}
206
Ian Rogers7caad772012-03-30 01:07:54 -0700207LIR* genNullCheck(CompilationUnit* cUnit, int sReg, int mReg, MIR* mir);
208void callRuntimeHelperReg(CompilationUnit* cUnit, int helperOffset, int arg0);
209
buzbeee88dfbf2012-03-05 11:19:57 -0800210/*
211 * TODO: implement fast path to short-circuit thin-lock case
212 */
213void genMonitorEnter(CompilationUnit* cUnit, MIR* mir, RegLocation rlSrc)
214{
Bill Buzbeea114add2012-05-03 15:00:40 -0700215 oatFlushAllRegs(cUnit);
216 loadValueDirectFixed(cUnit, rlSrc, rARG0); // Get obj
217 oatLockCallTemps(cUnit); // Prepare for explicit register usage
218 genNullCheck(cUnit, rlSrc.sRegLow, rARG0, mir);
219 // Go expensive route - artLockObjectFromCode(self, obj);
220 callRuntimeHelperReg(cUnit, ENTRYPOINT_OFFSET(pLockObjectFromCode), rARG0);
buzbeee88dfbf2012-03-05 11:19:57 -0800221}
222
223/*
224 * TODO: implement fast path to short-circuit thin-lock case
225 */
226void genMonitorExit(CompilationUnit* cUnit, MIR* mir, RegLocation rlSrc)
227{
Bill Buzbeea114add2012-05-03 15:00:40 -0700228 oatFlushAllRegs(cUnit);
229 loadValueDirectFixed(cUnit, rlSrc, rARG0); // Get obj
230 oatLockCallTemps(cUnit); // Prepare for explicit register usage
231 genNullCheck(cUnit, rlSrc.sRegLow, rARG0, mir);
232 // Go expensive route - UnlockObjectFromCode(obj);
233 callRuntimeHelperReg(cUnit, ENTRYPOINT_OFFSET(pUnlockObjectFromCode), rARG0);
buzbeee88dfbf2012-03-05 11:19:57 -0800234}
235
236/*
237 * Compare two 64-bit values
238 * x = y return 0
239 * x < y return -1
240 * x > y return 1
241 *
242 * slt t0, x.hi, y.hi; # (x.hi < y.hi) ? 1:0
243 * sgt t1, x.hi, y.hi; # (y.hi > x.hi) ? 1:0
244 * subu res, t0, t1 # res = -1:1:0 for [ < > = ]
245 * bnez res, finish
246 * sltu t0, x.lo, y.lo
247 * sgtu r1, x.lo, y.lo
248 * subu res, t0, t1
249 * finish:
250 *
251 */
252void genCmpLong(CompilationUnit* cUnit, MIR* mir, RegLocation rlDest,
253 RegLocation rlSrc1, RegLocation rlSrc2)
254{
Bill Buzbeea114add2012-05-03 15:00:40 -0700255 oatFlushAllRegs(cUnit);
256 oatLockCallTemps(cUnit); // Prepare for explicit register usage
257 loadValueDirectWideFixed(cUnit, rlSrc1, r0, r1);
258 loadValueDirectWideFixed(cUnit, rlSrc1, r2, r3);
259 // Compute (r1:r0) = (r1:r0) - (r2:r3)
260 opRegReg(cUnit, kOpSub, r0, r2); // r0 = r0 - r2
261 opRegReg(cUnit, kOpSbc, r1, r3); // r1 = r1 - r3 - CF
262 opRegReg(cUnit, kOpOr, r0, r1); // r0 = high | low - sets ZF
263 newLIR2(cUnit, kX86Set8R, r0, kX86CondNz); // r0 = (r1:r0) != (r2:r3) ? 1 : 0
264 newLIR2(cUnit, kX86Movzx8RR, r0, r0);
265 opRegImm(cUnit, kOpAsr, r1, 31); // r1 = high >> 31
266 opRegReg(cUnit, kOpOr, r0, r1); // r0 holds result
267 RegLocation rlResult = LOC_C_RETURN;
268 storeValue(cUnit, rlDest, rlResult);
buzbeee88dfbf2012-03-05 11:19:57 -0800269}
270
Ian Rogersb5d09b22012-03-06 22:14:17 -0800271X86ConditionCode oatX86ConditionEncoding(ConditionCode cond) {
Elliott Hughesb25c3f62012-03-26 16:35:06 -0700272 switch (cond) {
Ian Rogersb5d09b22012-03-06 22:14:17 -0800273 case kCondEq: return kX86CondEq;
274 case kCondNe: return kX86CondNe;
275 case kCondCs: return kX86CondC;
276 case kCondCc: return kX86CondNc;
277 case kCondMi: return kX86CondS;
278 case kCondPl: return kX86CondNs;
279 case kCondVs: return kX86CondO;
280 case kCondVc: return kX86CondNo;
281 case kCondHi: return kX86CondA;
282 case kCondLs: return kX86CondBe;
283 case kCondGe: return kX86CondGe;
284 case kCondLt: return kX86CondL;
285 case kCondGt: return kX86CondG;
286 case kCondLe: return kX86CondLe;
287 case kCondAl:
288 case kCondNv: LOG(FATAL) << "Should not reach here";
289 }
290 return kX86CondO;
291}
292
Bill Buzbeea114add2012-05-03 15:00:40 -0700293LIR* opCmpBranch(CompilationUnit* cUnit, ConditionCode cond, int src1,
294 int src2, LIR* target)
buzbeee88dfbf2012-03-05 11:19:57 -0800295{
Ian Rogersb5d09b22012-03-06 22:14:17 -0800296 newLIR2(cUnit, kX86Cmp32RR, src1, src2);
297 X86ConditionCode cc = oatX86ConditionEncoding(cond);
Bill Buzbeea114add2012-05-03 15:00:40 -0700298 LIR* branch = newLIR2(cUnit, kX86Jcc8, 0 /* lir operand for Jcc offset */ ,
299 cc);
Ian Rogersb5d09b22012-03-06 22:14:17 -0800300 branch->target = target;
301 return branch;
buzbeee88dfbf2012-03-05 11:19:57 -0800302}
303
304LIR* opCmpImmBranch(CompilationUnit* cUnit, ConditionCode cond, int reg,
305 int checkValue, LIR* target)
306{
Ian Rogers7caad772012-03-30 01:07:54 -0700307 if (false && (checkValue == 0) && (cond == kCondEq || cond == kCondNe)) {
308 // TODO: when checkValue == 0 and reg is rCX, use the jcxz/nz opcode
309 // newLIR2(cUnit, kX86Test32RR, reg, reg);
310 } else {
311 newLIR2(cUnit, kX86Cmp32RI, reg, checkValue);
312 }
Ian Rogersb5d09b22012-03-06 22:14:17 -0800313 X86ConditionCode cc = oatX86ConditionEncoding(cond);
Ian Rogersb41b33b2012-03-20 14:22:54 -0700314 LIR* branch = newLIR2(cUnit, kX86Jcc8, 0 /* lir operand for Jcc offset */ , cc);
Ian Rogersb5d09b22012-03-06 22:14:17 -0800315 branch->target = target;
316 return branch;
buzbeee88dfbf2012-03-05 11:19:57 -0800317}
318
319LIR* opRegCopyNoInsert(CompilationUnit *cUnit, int rDest, int rSrc)
320{
Bill Buzbeea114add2012-05-03 15:00:40 -0700321 if (FPREG(rDest) || FPREG(rSrc))
322 return fpRegCopy(cUnit, rDest, rSrc);
323 LIR* res = rawLIR(cUnit, cUnit->currentDalvikOffset, kX86Mov32RR,
324 rDest, rSrc);
325 if (rDest == rSrc) {
326 res->flags.isNop = true;
327 }
328 return res;
buzbeee88dfbf2012-03-05 11:19:57 -0800329}
330
331LIR* opRegCopy(CompilationUnit *cUnit, int rDest, int rSrc)
332{
Bill Buzbeea114add2012-05-03 15:00:40 -0700333 LIR *res = opRegCopyNoInsert(cUnit, rDest, rSrc);
334 oatAppendLIR(cUnit, res);
335 return res;
buzbeee88dfbf2012-03-05 11:19:57 -0800336}
337
338void opRegCopyWide(CompilationUnit *cUnit, int destLo, int destHi,
Bill Buzbeea114add2012-05-03 15:00:40 -0700339 int srcLo, int srcHi)
340{
Ian Rogersf7d9ad32012-03-13 18:45:39 -0700341 bool destFP = FPREG(destLo) && FPREG(destHi);
342 bool srcFP = FPREG(srcLo) && FPREG(srcHi);
343 assert(FPREG(srcLo) == FPREG(srcHi));
344 assert(FPREG(destLo) == FPREG(destHi));
Ian Rogersf7d9ad32012-03-13 18:45:39 -0700345 if (destFP) {
346 if (srcFP) {
347 opRegCopy(cUnit, S2D(destLo, destHi), S2D(srcLo, srcHi));
buzbeee88dfbf2012-03-05 11:19:57 -0800348 } else {
Ian Rogersf7d9ad32012-03-13 18:45:39 -0700349 UNIMPLEMENTED(WARNING);
Ian Rogers7caad772012-03-30 01:07:54 -0700350 newLIR0(cUnit, kX86Bkpt);
buzbeee88dfbf2012-03-05 11:19:57 -0800351 }
Ian Rogersf7d9ad32012-03-13 18:45:39 -0700352 } else {
353 if (srcFP) {
354 UNIMPLEMENTED(WARNING);
Ian Rogers7caad772012-03-30 01:07:54 -0700355 newLIR0(cUnit, kX86Bkpt);
Ian Rogersf7d9ad32012-03-13 18:45:39 -0700356 } else {
357 // Handle overlap
358 if (srcHi == destLo) {
buzbeee88dfbf2012-03-05 11:19:57 -0800359 opRegCopy(cUnit, destHi, srcHi);
360 opRegCopy(cUnit, destLo, srcLo);
Ian Rogersf7d9ad32012-03-13 18:45:39 -0700361 } else {
buzbeee88dfbf2012-03-05 11:19:57 -0800362 opRegCopy(cUnit, destLo, srcLo);
363 opRegCopy(cUnit, destHi, srcHi);
Ian Rogersf7d9ad32012-03-13 18:45:39 -0700364 }
buzbeee88dfbf2012-03-05 11:19:57 -0800365 }
Ian Rogersf7d9ad32012-03-13 18:45:39 -0700366 }
buzbeee88dfbf2012-03-05 11:19:57 -0800367}
368
369} // namespace art