blob: f63ad4c3ca7806c8237630390b5dd85457a964f4 [file] [log] [blame]
buzbee31a4a6f2012-02-28 15:36:15 -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
17namespace art {
18
19/*
20 * This source files contains "gen" codegen routines that should
21 * be applicable to most targets. Only mid-level support utilities
22 * and "op" calls may be used here.
23 */
24
25#if defined(TARGET_ARM)
buzbee82488f52012-03-02 08:20:26 -080026LIR* opIT(CompilationUnit* cUnit, ArmConditionCode cond, const char* guide);
buzbee31a4a6f2012-02-28 15:36:15 -080027#endif
28
Ian Rogersab2b55d2012-03-18 00:06:11 -070029void callRuntimeHelperImm(CompilationUnit* cUnit, int helperOffset, int arg0) {
30#if !defined(TARGET_X86)
31 int rTgt = loadHelper(cUnit, helperOffset);
32#endif
33 loadConstant(cUnit, rARG0, arg0);
buzbee31a4a6f2012-02-28 15:36:15 -080034 oatClobberCalleeSave(cUnit);
Ian Rogers6cbb2bd2012-03-16 13:45:30 -070035#if !defined(TARGET_X86)
Ian Rogersab2b55d2012-03-18 00:06:11 -070036 opReg(cUnit, kOpBlx, rTgt);
37 oatFreeTemp(cUnit, rTgt);
Ian Rogers6cbb2bd2012-03-16 13:45:30 -070038#else
Ian Rogersab2b55d2012-03-18 00:06:11 -070039 opThreadMem(cUnit, kOpBlx, helperOffset);
40#endif
41}
42
43void callRuntimeHelperRegLocation(CompilationUnit* cUnit, int helperOffset,
44 RegLocation arg0) {
45#if !defined(TARGET_X86)
46 int rTgt = loadHelper(cUnit, helperOffset);
47#endif
48 if (arg0.wide == 0) {
49 loadValueDirectFixed(cUnit, arg0, rARG0);
50 } else {
51 loadValueDirectWideFixed(cUnit, arg0, rARG0, rARG1);
52 }
53 oatClobberCalleeSave(cUnit);
54#if !defined(TARGET_X86)
55 opReg(cUnit, kOpBlx, rTgt);
56 oatFreeTemp(cUnit, rTgt);
57#else
58 opThreadMem(cUnit, kOpBlx, helperOffset);
59#endif
60}
61
62void callRuntimeHelperImmImm(CompilationUnit* cUnit, int helperOffset,
63 int arg0, int arg1) {
64#if !defined(TARGET_X86)
65 int rTgt = loadHelper(cUnit, helperOffset);
66#endif
67 loadConstant(cUnit, rARG0, arg0);
68 loadConstant(cUnit, rARG1, arg1);
69 oatClobberCalleeSave(cUnit);
70#if !defined(TARGET_X86)
71 opReg(cUnit, kOpBlx, rTgt);
72 oatFreeTemp(cUnit, rTgt);
73#else
74 opThreadMem(cUnit, kOpBlx, helperOffset);
75#endif
76}
77
78void callRuntimeHelperImmRegLocation(CompilationUnit* cUnit, int helperOffset,
79 int arg0, RegLocation arg1) {
80#if !defined(TARGET_X86)
81 int rTgt = loadHelper(cUnit, helperOffset);
82#endif
83 if (arg1.wide == 0) {
84 loadValueDirectFixed(cUnit, arg1, rARG1);
85 } else {
86 loadValueDirectWideFixed(cUnit, arg1, rARG1, rARG2);
87 }
88 loadConstant(cUnit, rARG0, arg0);
89 oatClobberCalleeSave(cUnit);
90#if !defined(TARGET_X86)
91 opReg(cUnit, kOpBlx, rTgt);
92 oatFreeTemp(cUnit, rTgt);
93#else
94 opThreadMem(cUnit, kOpBlx, helperOffset);
95#endif
96}
97
98void callRuntimeHelperRegLocationImm(CompilationUnit* cUnit, int helperOffset,
99 RegLocation arg0, int arg1) {
100#if !defined(TARGET_X86)
101 int rTgt = loadHelper(cUnit, helperOffset);
102#endif
103 loadValueDirectFixed(cUnit, arg0, rARG0);
104 loadConstant(cUnit, rARG1, arg1);
105 oatClobberCalleeSave(cUnit);
106#if !defined(TARGET_X86)
107 opReg(cUnit, kOpBlx, rTgt);
108 oatFreeTemp(cUnit, rTgt);
109#else
110 opThreadMem(cUnit, kOpBlx, helperOffset);
111#endif
112}
113
114void callRuntimeHelperImmReg(CompilationUnit* cUnit, int helperOffset,
115 int arg0, int arg1) {
116#if !defined(TARGET_X86)
117 int rTgt = loadHelper(cUnit, helperOffset);
118#endif
119 opRegCopy(cUnit, rARG1, arg1);
120 loadConstant(cUnit, rARG0, arg0);
121 oatClobberCalleeSave(cUnit);
122#if !defined(TARGET_X86)
123 opReg(cUnit, kOpBlx, rTgt);
124 oatFreeTemp(cUnit, rTgt);
125#else
126 opThreadMem(cUnit, kOpBlx, helperOffset);
127#endif
128}
129
130void callRuntimeHelperRegImm(CompilationUnit* cUnit, int helperOffset,
131 int arg0, int arg1) {
132#if !defined(TARGET_X86)
133 int rTgt = loadHelper(cUnit, helperOffset);
134#endif
135 opRegCopy(cUnit, rARG0, arg0);
136 loadConstant(cUnit, rARG1, arg1);
137 oatClobberCalleeSave(cUnit);
138#if !defined(TARGET_X86)
139 opReg(cUnit, kOpBlx, rTgt);
140 oatFreeTemp(cUnit, rTgt);
141#else
142 opThreadMem(cUnit, kOpBlx, helperOffset);
143#endif
144}
145
146void callRuntimeHelperImmMethod(CompilationUnit* cUnit, int helperOffset,
147 int arg0) {
148#if !defined(TARGET_X86)
149 int rTgt = loadHelper(cUnit, helperOffset);
150#endif
151 loadCurrMethodDirect(cUnit, rARG1);
152 loadConstant(cUnit, rARG0, arg0);
153 oatClobberCalleeSave(cUnit);
154#if !defined(TARGET_X86)
155 opReg(cUnit, kOpBlx, rTgt);
156 oatFreeTemp(cUnit, rTgt);
157#else
158 opThreadMem(cUnit, kOpBlx, helperOffset);
159#endif
160}
161
162void callRuntimeHelperRegLocationRegLocation(CompilationUnit* cUnit,
163 int helperOffset,
164 RegLocation arg0,
165 RegLocation arg1) {
166#if !defined(TARGET_X86)
167 int rTgt = loadHelper(cUnit, helperOffset);
168#endif
169 if (arg0.wide == 0) {
170 loadValueDirectFixed(cUnit, arg0, rARG0);
171 if (arg1.wide == 0) {
172 loadValueDirectFixed(cUnit, arg1, rARG1);
173 } else {
174 loadValueDirectWideFixed(cUnit, arg1, rARG1, rARG2);
175 }
176 } else {
177 loadValueDirectWideFixed(cUnit, arg0, rARG0, rARG1);
178 if (arg1.wide == 0) {
179 loadValueDirectFixed(cUnit, arg1, rARG2);
180 } else {
Ian Rogersab2b55d2012-03-18 00:06:11 -0700181 loadValueDirectWideFixed(cUnit, arg1, rARG2, rARG3);
Ian Rogersab2b55d2012-03-18 00:06:11 -0700182 }
183 }
184 oatClobberCalleeSave(cUnit);
185#if !defined(TARGET_X86)
186 opReg(cUnit, kOpBlx, rTgt);
187 oatFreeTemp(cUnit, rTgt);
188#else
189 opThreadMem(cUnit, kOpBlx, helperOffset);
190#endif
191}
192
193void callRuntimeHelperRegReg(CompilationUnit* cUnit, int helperOffset,
194 int arg0, int arg1) {
195#if !defined(TARGET_X86)
196 int rTgt = loadHelper(cUnit, helperOffset);
197#endif
198 DCHECK_NE((int)rARG0, arg1); // check copy into arg0 won't clobber arg1
199 opRegCopy(cUnit, rARG0, arg0);
200 opRegCopy(cUnit, rARG1, arg1);
201 oatClobberCalleeSave(cUnit);
202#if !defined(TARGET_X86)
203 opReg(cUnit, kOpBlx, rTgt);
204 oatFreeTemp(cUnit, rTgt);
205#else
206 opThreadMem(cUnit, kOpBlx, helperOffset);
207#endif
208}
209
210void callRuntimeHelperRegRegImm(CompilationUnit* cUnit, int helperOffset,
211 int arg0, int arg1, int arg2) {
212#if !defined(TARGET_X86)
213 int rTgt = loadHelper(cUnit, helperOffset);
214#endif
215 DCHECK_NE((int)rARG0, arg1); // check copy into arg0 won't clobber arg1
216 opRegCopy(cUnit, rARG0, arg0);
217 opRegCopy(cUnit, rARG1, arg1);
218 loadConstant(cUnit, rARG2, arg2);
219 oatClobberCalleeSave(cUnit);
220#if !defined(TARGET_X86)
221 opReg(cUnit, kOpBlx, rTgt);
222 oatFreeTemp(cUnit, rTgt);
223#else
224 opThreadMem(cUnit, kOpBlx, helperOffset);
225#endif
226}
227
228void callRuntimeHelperImmMethodRegLocation(CompilationUnit* cUnit, int helperOffset,
229 int arg0, RegLocation arg2) {
230#if !defined(TARGET_X86)
231 int rTgt = loadHelper(cUnit, helperOffset);
232#endif
233 loadValueDirectFixed(cUnit, arg2, rARG2);
234 loadCurrMethodDirect(cUnit, rARG1);
235 loadConstant(cUnit, rARG0, arg0);
236 oatClobberCalleeSave(cUnit);
237#if !defined(TARGET_X86)
238 opReg(cUnit, kOpBlx, rTgt);
239 oatFreeTemp(cUnit, rTgt);
240#else
241 opThreadMem(cUnit, kOpBlx, helperOffset);
242#endif
243}
244
245void callRuntimeHelperImmMethodImm(CompilationUnit* cUnit, int helperOffset,
246 int arg0, int arg2) {
247#if !defined(TARGET_X86)
248 int rTgt = loadHelper(cUnit, helperOffset);
249#endif
250 loadCurrMethodDirect(cUnit, rARG1);
251 loadConstant(cUnit, rARG2, arg2);
252 loadConstant(cUnit, rARG0, arg0);
253 oatClobberCalleeSave(cUnit);
254#if !defined(TARGET_X86)
255 opReg(cUnit, kOpBlx, rTgt);
256 oatFreeTemp(cUnit, rTgt);
257#else
258 opThreadMem(cUnit, kOpBlx, helperOffset);
259#endif
260}
261
262void callRuntimeHelperImmRegLocationRegLocation(CompilationUnit* cUnit,
263 int helperOffset,
264 int arg0, RegLocation arg1,
265 RegLocation arg2) {
266#if !defined(TARGET_X86)
267 int rTgt = loadHelper(cUnit, helperOffset);
268#endif
269 loadValueDirectFixed(cUnit, arg1, rARG1);
270 if (arg2.wide == 0) {
271 loadValueDirectFixed(cUnit, arg2, rARG2);
272 } else {
Ian Rogersab2b55d2012-03-18 00:06:11 -0700273 loadValueDirectWideFixed(cUnit, arg2, rARG2, rARG3);
Ian Rogersab2b55d2012-03-18 00:06:11 -0700274 }
275 loadConstant(cUnit, rARG0, arg0);
276 oatClobberCalleeSave(cUnit);
277#if !defined(TARGET_X86)
278 opReg(cUnit, kOpBlx, rTgt);
279 oatFreeTemp(cUnit, rTgt);
280#else
281 opThreadMem(cUnit, kOpBlx, helperOffset);
Ian Rogers6cbb2bd2012-03-16 13:45:30 -0700282#endif
buzbee31a4a6f2012-02-28 15:36:15 -0800283}
284
285/*
286 * Generate an kPseudoBarrier marker to indicate the boundary of special
287 * blocks.
288 */
289void genBarrier(CompilationUnit* cUnit)
290{
291 LIR* barrier = newLIR0(cUnit, kPseudoBarrier);
292 /* Mark all resources as being clobbered */
293 barrier->defMask = -1;
294}
295
buzbee31a4a6f2012-02-28 15:36:15 -0800296
297/* Generate unconditional branch instructions */
buzbee82488f52012-03-02 08:20:26 -0800298LIR* opUnconditionalBranch(CompilationUnit* cUnit, LIR* target)
buzbee31a4a6f2012-02-28 15:36:15 -0800299{
Ian Rogers680b1bd2012-03-07 20:18:49 -0800300 LIR* branch = opBranchUnconditional(cUnit, kOpUncondBr);
buzbee31a4a6f2012-02-28 15:36:15 -0800301 branch->target = (LIR*) target;
302 return branch;
303}
304
buzbee5de34942012-03-01 14:51:57 -0800305// FIXME: need to do some work to split out targets with
306// condition codes and those without
307#if defined(TARGET_ARM) || defined(TARGET_X86)
buzbee31a4a6f2012-02-28 15:36:15 -0800308LIR* genCheck(CompilationUnit* cUnit, ConditionCode cCode, MIR* mir,
309 ThrowKind kind)
310{
buzbeea2ebdd72012-03-04 14:57:06 -0800311 LIR* tgt = rawLIR(cUnit, 0, kPseudoThrowTarget, kind,
312 mir ? mir->offset : 0);
buzbee82488f52012-03-02 08:20:26 -0800313 LIR* branch = opCondBranch(cUnit, cCode, tgt);
buzbee31a4a6f2012-02-28 15:36:15 -0800314 // Remember branch target - will process later
315 oatInsertGrowableList(cUnit, &cUnit->throwLaunchpads, (intptr_t)tgt);
316 return branch;
317}
buzbee5de34942012-03-01 14:51:57 -0800318#endif
buzbee31a4a6f2012-02-28 15:36:15 -0800319
320LIR* genImmedCheck(CompilationUnit* cUnit, ConditionCode cCode,
321 int reg, int immVal, MIR* mir, ThrowKind kind)
322{
buzbeea2ebdd72012-03-04 14:57:06 -0800323 LIR* tgt = rawLIR(cUnit, 0, kPseudoThrowTarget, kind, mir->offset);
buzbee31a4a6f2012-02-28 15:36:15 -0800324 LIR* branch;
325 if (cCode == kCondAl) {
buzbee82488f52012-03-02 08:20:26 -0800326 branch = opUnconditionalBranch(cUnit, tgt);
buzbee31a4a6f2012-02-28 15:36:15 -0800327 } else {
buzbee82488f52012-03-02 08:20:26 -0800328 branch = opCmpImmBranch(cUnit, cCode, reg, immVal, tgt);
buzbee31a4a6f2012-02-28 15:36:15 -0800329 }
330 // Remember branch target - will process later
331 oatInsertGrowableList(cUnit, &cUnit->throwLaunchpads, (intptr_t)tgt);
332 return branch;
333}
334
335/* Perform null-check on a register. */
336LIR* genNullCheck(CompilationUnit* cUnit, int sReg, int mReg, MIR* mir)
337{
338 if (!(cUnit->disableOpt & (1 << kNullCheckElimination)) &&
339 mir->optimizationFlags & MIR_IGNORE_NULL_CHECK) {
340 return NULL;
341 }
342 return genImmedCheck(cUnit, kCondEq, mReg, 0, mir, kThrowNullPointer);
343}
344
345/* Perform check on two registers */
346LIR* genRegRegCheck(CompilationUnit* cUnit, ConditionCode cCode,
Ian Rogersb5d09b22012-03-06 22:14:17 -0800347 int reg1, int reg2, MIR* mir, ThrowKind kind)
buzbee31a4a6f2012-02-28 15:36:15 -0800348{
buzbeea2ebdd72012-03-04 14:57:06 -0800349 LIR* tgt = rawLIR(cUnit, 0, kPseudoThrowTarget, kind,
350 mir ? mir->offset : 0, reg1, reg2);
buzbee5de34942012-03-01 14:51:57 -0800351#if defined(TARGET_MIPS)
buzbee82488f52012-03-02 08:20:26 -0800352 LIR* branch = opCmpBranch(cUnit, cCode, reg1, reg2, tgt);
buzbee5de34942012-03-01 14:51:57 -0800353#else
buzbee31a4a6f2012-02-28 15:36:15 -0800354 opRegReg(cUnit, kOpCmp, reg1, reg2);
buzbee82488f52012-03-02 08:20:26 -0800355 LIR* branch = opCondBranch(cUnit, cCode, tgt);
buzbee5de34942012-03-01 14:51:57 -0800356#endif
buzbee31a4a6f2012-02-28 15:36:15 -0800357 // Remember branch target - will process later
358 oatInsertGrowableList(cUnit, &cUnit->throwLaunchpads, (intptr_t)tgt);
359 return branch;
360}
361
362void genCompareAndBranch(CompilationUnit* cUnit, BasicBlock* bb, MIR* mir,
363 RegLocation rlSrc1, RegLocation rlSrc2, LIR* labelList)
364{
365 ConditionCode cond;
366 rlSrc1 = loadValue(cUnit, rlSrc1, kCoreReg);
367 rlSrc2 = loadValue(cUnit, rlSrc2, kCoreReg);
Elliott Hughesadb8c672012-03-06 16:49:32 -0800368 Instruction::Code opcode = mir->dalvikInsn.opcode;
buzbee31a4a6f2012-02-28 15:36:15 -0800369 switch(opcode) {
Elliott Hughesadb8c672012-03-06 16:49:32 -0800370 case Instruction::IF_EQ:
buzbee31a4a6f2012-02-28 15:36:15 -0800371 cond = kCondEq;
372 break;
Elliott Hughesadb8c672012-03-06 16:49:32 -0800373 case Instruction::IF_NE:
buzbee31a4a6f2012-02-28 15:36:15 -0800374 cond = kCondNe;
375 break;
Elliott Hughesadb8c672012-03-06 16:49:32 -0800376 case Instruction::IF_LT:
buzbee31a4a6f2012-02-28 15:36:15 -0800377 cond = kCondLt;
378 break;
Elliott Hughesadb8c672012-03-06 16:49:32 -0800379 case Instruction::IF_GE:
buzbee31a4a6f2012-02-28 15:36:15 -0800380 cond = kCondGe;
381 break;
Elliott Hughesadb8c672012-03-06 16:49:32 -0800382 case Instruction::IF_GT:
buzbee31a4a6f2012-02-28 15:36:15 -0800383 cond = kCondGt;
384 break;
Elliott Hughesadb8c672012-03-06 16:49:32 -0800385 case Instruction::IF_LE:
buzbee31a4a6f2012-02-28 15:36:15 -0800386 cond = kCondLe;
387 break;
388 default:
389 cond = (ConditionCode)0;
390 LOG(FATAL) << "Unexpected opcode " << (int)opcode;
391 }
buzbee5de34942012-03-01 14:51:57 -0800392#if defined(TARGET_MIPS)
buzbee82488f52012-03-02 08:20:26 -0800393 opCmpBranch(cUnit, cond, rlSrc1.lowReg, rlSrc2.lowReg,
394 &labelList[bb->taken->id]);
buzbee5de34942012-03-01 14:51:57 -0800395#else
396 opRegReg(cUnit, kOpCmp, rlSrc1.lowReg, rlSrc2.lowReg);
buzbee82488f52012-03-02 08:20:26 -0800397 opCondBranch(cUnit, cond, &labelList[bb->taken->id]);
buzbee5de34942012-03-01 14:51:57 -0800398#endif
buzbee82488f52012-03-02 08:20:26 -0800399 opUnconditionalBranch(cUnit, &labelList[bb->fallThrough->id]);
buzbee31a4a6f2012-02-28 15:36:15 -0800400}
401
402void genCompareZeroAndBranch(CompilationUnit* cUnit, BasicBlock* bb, MIR* mir,
403 RegLocation rlSrc, LIR* labelList)
404{
405 ConditionCode cond;
406 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
Elliott Hughesadb8c672012-03-06 16:49:32 -0800407 Instruction::Code opcode = mir->dalvikInsn.opcode;
buzbee31a4a6f2012-02-28 15:36:15 -0800408 switch(opcode) {
Elliott Hughesadb8c672012-03-06 16:49:32 -0800409 case Instruction::IF_EQZ:
buzbee31a4a6f2012-02-28 15:36:15 -0800410 cond = kCondEq;
411 break;
Elliott Hughesadb8c672012-03-06 16:49:32 -0800412 case Instruction::IF_NEZ:
buzbee31a4a6f2012-02-28 15:36:15 -0800413 cond = kCondNe;
414 break;
Elliott Hughesadb8c672012-03-06 16:49:32 -0800415 case Instruction::IF_LTZ:
buzbee31a4a6f2012-02-28 15:36:15 -0800416 cond = kCondLt;
417 break;
Elliott Hughesadb8c672012-03-06 16:49:32 -0800418 case Instruction::IF_GEZ:
buzbee31a4a6f2012-02-28 15:36:15 -0800419 cond = kCondGe;
420 break;
Elliott Hughesadb8c672012-03-06 16:49:32 -0800421 case Instruction::IF_GTZ:
buzbee31a4a6f2012-02-28 15:36:15 -0800422 cond = kCondGt;
423 break;
Elliott Hughesadb8c672012-03-06 16:49:32 -0800424 case Instruction::IF_LEZ:
buzbee31a4a6f2012-02-28 15:36:15 -0800425 cond = kCondLe;
426 break;
427 default:
428 cond = (ConditionCode)0;
429 LOG(FATAL) << "Unexpected opcode " << (int)opcode;
430 }
buzbee5de34942012-03-01 14:51:57 -0800431#if defined(TARGET_MIPS)
buzbee82488f52012-03-02 08:20:26 -0800432 opCmpImmBranch(cUnit, cond, rlSrc.lowReg, 0, &labelList[bb->taken->id]);
buzbee5de34942012-03-01 14:51:57 -0800433#else
434 opRegImm(cUnit, kOpCmp, rlSrc.lowReg, 0);
buzbee82488f52012-03-02 08:20:26 -0800435 opCondBranch(cUnit, cond, &labelList[bb->taken->id]);
buzbee5de34942012-03-01 14:51:57 -0800436#endif
buzbee82488f52012-03-02 08:20:26 -0800437 opUnconditionalBranch(cUnit, &labelList[bb->fallThrough->id]);
buzbee31a4a6f2012-02-28 15:36:15 -0800438}
439
440void genIntToLong(CompilationUnit* cUnit, MIR* mir, RegLocation rlDest,
441 RegLocation rlSrc)
442{
443 RegLocation rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
444 if (rlSrc.location == kLocPhysReg) {
buzbee82488f52012-03-02 08:20:26 -0800445 opRegCopy(cUnit, rlResult.lowReg, rlSrc.lowReg);
buzbee31a4a6f2012-02-28 15:36:15 -0800446 } else {
447 loadValueDirect(cUnit, rlSrc, rlResult.lowReg);
448 }
449 opRegRegImm(cUnit, kOpAsr, rlResult.highReg,
450 rlResult.lowReg, 31);
451 storeValueWide(cUnit, rlDest, rlResult);
452}
453
454void genIntNarrowing(CompilationUnit* cUnit, MIR* mir, RegLocation rlDest,
455 RegLocation rlSrc)
456{
457 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
458 RegLocation rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
459 OpKind op = kOpInvalid;
460 switch(mir->dalvikInsn.opcode) {
Elliott Hughesadb8c672012-03-06 16:49:32 -0800461 case Instruction::INT_TO_BYTE:
buzbee31a4a6f2012-02-28 15:36:15 -0800462 op = kOp2Byte;
463 break;
Elliott Hughesadb8c672012-03-06 16:49:32 -0800464 case Instruction::INT_TO_SHORT:
buzbee31a4a6f2012-02-28 15:36:15 -0800465 op = kOp2Short;
466 break;
Elliott Hughesadb8c672012-03-06 16:49:32 -0800467 case Instruction::INT_TO_CHAR:
buzbee31a4a6f2012-02-28 15:36:15 -0800468 op = kOp2Char;
469 break;
470 default:
471 LOG(ERROR) << "Bad int conversion type";
472 }
473 opRegReg(cUnit, op, rlResult.lowReg, rlSrc.lowReg);
474 storeValue(cUnit, rlDest, rlResult);
475}
476
477/*
478 * Let helper function take care of everything. Will call
479 * Array::AllocFromCode(type_idx, method, count);
480 * Note: AllocFromCode will handle checks for errNegativeArraySize.
481 */
482void genNewArray(CompilationUnit* cUnit, MIR* mir, RegLocation rlDest,
483 RegLocation rlSrc)
484{
485 oatFlushAllRegs(cUnit); /* Everything to home location */
486 uint32_t type_idx = mir->dalvikInsn.vC;
Ian Rogers6cbb2bd2012-03-16 13:45:30 -0700487 int funcOffset;
buzbee31a4a6f2012-02-28 15:36:15 -0800488 if (cUnit->compiler->CanAccessTypeWithoutChecks(cUnit->method_idx,
489 cUnit->dex_cache,
490 *cUnit->dex_file,
491 type_idx)) {
Ian Rogers6cbb2bd2012-03-16 13:45:30 -0700492 funcOffset = OFFSETOF_MEMBER(Thread, pAllocArrayFromCode);
buzbee31a4a6f2012-02-28 15:36:15 -0800493 } else {
Ian Rogers6cbb2bd2012-03-16 13:45:30 -0700494 funcOffset= OFFSETOF_MEMBER(Thread, pAllocArrayFromCodeWithAccessCheck);
buzbee31a4a6f2012-02-28 15:36:15 -0800495 }
Ian Rogersab2b55d2012-03-18 00:06:11 -0700496 callRuntimeHelperImmMethodRegLocation(cUnit, funcOffset, type_idx, rlSrc);
Ian Rogersf7d9ad32012-03-13 18:45:39 -0700497 RegLocation rlResult = oatGetReturn(cUnit, false);
buzbee31a4a6f2012-02-28 15:36:15 -0800498 storeValue(cUnit, rlDest, rlResult);
499}
500
501/*
502 * Similar to genNewArray, but with post-allocation initialization.
503 * Verifier guarantees we're dealing with an array class. Current
504 * code throws runtime exception "bad Filled array req" for 'D' and 'J'.
505 * Current code also throws internal unimp if not 'L', '[' or 'I'.
506 */
507void genFilledNewArray(CompilationUnit* cUnit, MIR* mir, bool isRange)
508{
509 DecodedInstruction* dInsn = &mir->dalvikInsn;
510 int elems = dInsn->vA;
Ian Rogersab2b55d2012-03-18 00:06:11 -0700511 int typeIdx = dInsn->vB;
buzbee31a4a6f2012-02-28 15:36:15 -0800512 oatFlushAllRegs(cUnit); /* Everything to home location */
Ian Rogers6cbb2bd2012-03-16 13:45:30 -0700513 int funcOffset;
buzbee31a4a6f2012-02-28 15:36:15 -0800514 if (cUnit->compiler->CanAccessTypeWithoutChecks(cUnit->method_idx,
515 cUnit->dex_cache,
516 *cUnit->dex_file,
Ian Rogersab2b55d2012-03-18 00:06:11 -0700517 typeIdx)) {
Ian Rogers6cbb2bd2012-03-16 13:45:30 -0700518 funcOffset = OFFSETOF_MEMBER(Thread, pCheckAndAllocArrayFromCode);
buzbee31a4a6f2012-02-28 15:36:15 -0800519 } else {
Ian Rogers6cbb2bd2012-03-16 13:45:30 -0700520 funcOffset = OFFSETOF_MEMBER(Thread,
521 pCheckAndAllocArrayFromCodeWithAccessCheck);
buzbee31a4a6f2012-02-28 15:36:15 -0800522 }
Ian Rogersab2b55d2012-03-18 00:06:11 -0700523 callRuntimeHelperImmMethodImm(cUnit, funcOffset, typeIdx, elems);
buzbeee1965672012-03-11 18:39:19 -0700524 oatFreeTemp(cUnit, rARG2);
525 oatFreeTemp(cUnit, rARG1);
buzbee31a4a6f2012-02-28 15:36:15 -0800526 /*
Elliott Hughesadb8c672012-03-06 16:49:32 -0800527 * NOTE: the implicit target for Instruction::FILLED_NEW_ARRAY is the
buzbee31a4a6f2012-02-28 15:36:15 -0800528 * return region. Because AllocFromCode placed the new array
529 * in rRET0, we'll just lock it into place. When debugger support is
530 * added, it may be necessary to additionally copy all return
531 * values to a home location in thread-local storage
532 */
533 oatLockTemp(cUnit, rRET0);
534
535 // TODO: use the correct component size, currently all supported types
536 // share array alignment with ints (see comment at head of function)
537 size_t component_size = sizeof(int32_t);
538
539 // Having a range of 0 is legal
540 if (isRange && (dInsn->vA > 0)) {
541 /*
542 * Bit of ugliness here. We're going generate a mem copy loop
543 * on the register range, but it is possible that some regs
544 * in the range have been promoted. This is unlikely, but
545 * before generating the copy, we'll just force a flush
546 * of any regs in the source range that have been promoted to
547 * home location.
548 */
549 for (unsigned int i = 0; i < dInsn->vA; i++) {
550 RegLocation loc = oatUpdateLoc(cUnit,
551 oatGetSrc(cUnit, mir, i));
552 if (loc.location == kLocPhysReg) {
553 storeBaseDisp(cUnit, rSP, oatSRegOffset(cUnit, loc.sRegLow),
554 loc.lowReg, kWord);
555 }
556 }
557 /*
558 * TUNING note: generated code here could be much improved, but
559 * this is an uncommon operation and isn't especially performance
560 * critical.
561 */
562 int rSrc = oatAllocTemp(cUnit);
563 int rDst = oatAllocTemp(cUnit);
564 int rIdx = oatAllocTemp(cUnit);
buzbee5de34942012-03-01 14:51:57 -0800565#if defined(TARGET_ARM)
buzbee31a4a6f2012-02-28 15:36:15 -0800566 int rVal = rLR; // Using a lot of temps, rLR is known free here
buzbee5de34942012-03-01 14:51:57 -0800567#else
568 int rVal = oatAllocTemp(cUnit);
569#endif
buzbee31a4a6f2012-02-28 15:36:15 -0800570 // Set up source pointer
571 RegLocation rlFirst = oatGetSrc(cUnit, mir, 0);
Ian Rogersb5d09b22012-03-06 22:14:17 -0800572#if defined(TARGET_X86)
573 UNIMPLEMENTED(FATAL);
574#else
buzbee31a4a6f2012-02-28 15:36:15 -0800575 opRegRegImm(cUnit, kOpAdd, rSrc, rSP,
576 oatSRegOffset(cUnit, rlFirst.sRegLow));
577 // Set up the target pointer
578 opRegRegImm(cUnit, kOpAdd, rDst, rRET0,
579 Array::DataOffset(component_size).Int32Value());
Ian Rogersb5d09b22012-03-06 22:14:17 -0800580#endif
buzbee31a4a6f2012-02-28 15:36:15 -0800581 // Set up the loop counter (known to be > 0)
582 loadConstant(cUnit, rIdx, dInsn->vA - 1);
583 // Generate the copy loop. Going backwards for convenience
584 LIR* target = newLIR0(cUnit, kPseudoTargetLabel);
buzbee31a4a6f2012-02-28 15:36:15 -0800585 // Copy next element
586 loadBaseIndexed(cUnit, rSrc, rIdx, rVal, 2, kWord);
587 storeBaseIndexed(cUnit, rDst, rIdx, rVal, 2, kWord);
588#if defined(TARGET_ARM)
589 // Combine sub & test using sub setflags encoding here
590 newLIR3(cUnit, kThumb2SubsRRI12, rIdx, rIdx, 1);
buzbee82488f52012-03-02 08:20:26 -0800591 opCondBranch(cUnit, kCondGe, target);
buzbee31a4a6f2012-02-28 15:36:15 -0800592#else
buzbee5de34942012-03-01 14:51:57 -0800593 oatFreeTemp(cUnit, rVal);
buzbee31a4a6f2012-02-28 15:36:15 -0800594 opRegImm(cUnit, kOpSub, rIdx, 1);
buzbee82488f52012-03-02 08:20:26 -0800595 opCmpImmBranch(cUnit, kCondGe, rIdx, 0, target);
buzbee31a4a6f2012-02-28 15:36:15 -0800596#endif
buzbee31a4a6f2012-02-28 15:36:15 -0800597 } else if (!isRange) {
598 // TUNING: interleave
599 for (unsigned int i = 0; i < dInsn->vA; i++) {
600 RegLocation rlArg = loadValue(cUnit,
601 oatGetSrc(cUnit, mir, i), kCoreReg);
602 storeBaseDisp(cUnit, rRET0,
603 Array::DataOffset(component_size).Int32Value() +
604 i * 4, rlArg.lowReg, kWord);
605 // If the loadValue caused a temp to be allocated, free it
606 if (oatIsTemp(cUnit, rlArg.lowReg)) {
607 oatFreeTemp(cUnit, rlArg.lowReg);
608 }
609 }
610 }
611}
612
613void genSput(CompilationUnit* cUnit, MIR* mir, RegLocation rlSrc,
614 bool isLongOrDouble, bool isObject)
615{
616 int fieldOffset;
617 int ssbIndex;
618 bool isVolatile;
619 bool isReferrersClass;
620 uint32_t fieldIdx = mir->dalvikInsn.vB;
621
622 OatCompilationUnit mUnit(cUnit->class_loader, cUnit->class_linker,
623 *cUnit->dex_file, *cUnit->dex_cache,
624 cUnit->code_item, cUnit->method_idx,
625 cUnit->access_flags);
626
627 bool fastPath =
628 cUnit->compiler->ComputeStaticFieldInfo(fieldIdx, &mUnit,
629 fieldOffset, ssbIndex,
630 isReferrersClass, isVolatile, true);
631 if (fastPath && !SLOW_FIELD_PATH) {
632 DCHECK_GE(fieldOffset, 0);
633 int rBase;
buzbee31a4a6f2012-02-28 15:36:15 -0800634 if (isReferrersClass) {
635 // Fast path, static storage base is this method's class
buzbeee1965672012-03-11 18:39:19 -0700636 RegLocation rlMethod = loadCurrMethod(cUnit);
buzbee31a4a6f2012-02-28 15:36:15 -0800637 rBase = oatAllocTemp(cUnit);
buzbeee1965672012-03-11 18:39:19 -0700638 loadWordDisp(cUnit, rlMethod.lowReg,
buzbee31a4a6f2012-02-28 15:36:15 -0800639 Method::DeclaringClassOffset().Int32Value(), rBase);
640 } else {
641 // Medium path, static storage base in a different class which
642 // requires checks that the other class is initialized.
643 DCHECK_GE(ssbIndex, 0);
644 // May do runtime call so everything to home locations.
645 oatFlushAllRegs(cUnit);
646 // Using fixed register to sync with possible call to runtime
647 // support.
buzbeee1965672012-03-11 18:39:19 -0700648 int rMethod = rARG1;
buzbee31a4a6f2012-02-28 15:36:15 -0800649 oatLockTemp(cUnit, rMethod);
650 loadCurrMethodDirect(cUnit, rMethod);
651 rBase = rARG0;
652 oatLockTemp(cUnit, rBase);
653 loadWordDisp(cUnit, rMethod,
654 Method::DexCacheInitializedStaticStorageOffset().Int32Value(),
655 rBase);
656 loadWordDisp(cUnit, rBase,
657 Array::DataOffset(sizeof(Object*)).Int32Value() + sizeof(int32_t*) *
658 ssbIndex, rBase);
659 // rBase now points at appropriate static storage base (Class*)
660 // or NULL if not initialized. Check for NULL and call helper if NULL.
661 // TUNING: fast path should fall through
buzbee82488f52012-03-02 08:20:26 -0800662 LIR* branchOver = opCmpImmBranch(cUnit, kCondNe, rBase, 0, NULL);
buzbee31a4a6f2012-02-28 15:36:15 -0800663 loadConstant(cUnit, rARG0, ssbIndex);
Ian Rogersab2b55d2012-03-18 00:06:11 -0700664 callRuntimeHelperImm(cUnit,
665 OFFSETOF_MEMBER(Thread, pInitializeStaticStorage),
666 ssbIndex);
buzbee31a4a6f2012-02-28 15:36:15 -0800667#if defined(TARGET_MIPS)
668 // For Arm, rRET0 = rARG0 = rBASE, for Mips, we need to copy
buzbee82488f52012-03-02 08:20:26 -0800669 opRegCopy(cUnit, rBase, rRET0);
buzbee31a4a6f2012-02-28 15:36:15 -0800670#endif
671 LIR* skipTarget = newLIR0(cUnit, kPseudoTargetLabel);
buzbee31a4a6f2012-02-28 15:36:15 -0800672 branchOver->target = (LIR*)skipTarget;
buzbeee1965672012-03-11 18:39:19 -0700673 oatFreeTemp(cUnit, rMethod);
buzbee31a4a6f2012-02-28 15:36:15 -0800674 }
675 // rBase now holds static storage base
buzbee31a4a6f2012-02-28 15:36:15 -0800676 if (isLongOrDouble) {
677 rlSrc = oatGetSrcWide(cUnit, mir, 0, 1);
678 rlSrc = loadValueWide(cUnit, rlSrc, kAnyReg);
679 } else {
680 rlSrc = oatGetSrc(cUnit, mir, 0);
681 rlSrc = loadValue(cUnit, rlSrc, kAnyReg);
682 }
683//FIXME: need to generalize the barrier call
684 if (isVolatile) {
685 oatGenMemBarrier(cUnit, kST);
686 }
687 if (isLongOrDouble) {
688 storeBaseDispWide(cUnit, rBase, fieldOffset, rlSrc.lowReg,
689 rlSrc.highReg);
690 } else {
691 storeWordDisp(cUnit, rBase, fieldOffset, rlSrc.lowReg);
692 }
693 if (isVolatile) {
694 oatGenMemBarrier(cUnit, kSY);
695 }
696 if (isObject) {
697 markGCCard(cUnit, rlSrc.lowReg, rBase);
698 }
699 oatFreeTemp(cUnit, rBase);
700 } else {
701 oatFlushAllRegs(cUnit); // Everything to home locations
702 int setterOffset = isLongOrDouble ? OFFSETOF_MEMBER(Thread, pSet64Static) :
703 (isObject ? OFFSETOF_MEMBER(Thread, pSetObjStatic)
704 : OFFSETOF_MEMBER(Thread, pSet32Static));
Ian Rogersab2b55d2012-03-18 00:06:11 -0700705 callRuntimeHelperImmRegLocation(cUnit, setterOffset, fieldIdx, rlSrc);
buzbee31a4a6f2012-02-28 15:36:15 -0800706 }
707}
708
709void genSget(CompilationUnit* cUnit, MIR* mir, RegLocation rlDest,
710 bool isLongOrDouble, bool isObject)
711{
712 int fieldOffset;
713 int ssbIndex;
714 bool isVolatile;
715 bool isReferrersClass;
716 uint32_t fieldIdx = mir->dalvikInsn.vB;
717
718 OatCompilationUnit mUnit(cUnit->class_loader, cUnit->class_linker,
719 *cUnit->dex_file, *cUnit->dex_cache,
720 cUnit->code_item, cUnit->method_idx,
721 cUnit->access_flags);
722
723 bool fastPath =
724 cUnit->compiler->ComputeStaticFieldInfo(fieldIdx, &mUnit,
725 fieldOffset, ssbIndex,
726 isReferrersClass, isVolatile,
727 false);
728 if (fastPath && !SLOW_FIELD_PATH) {
729 DCHECK_GE(fieldOffset, 0);
730 int rBase;
buzbee31a4a6f2012-02-28 15:36:15 -0800731 if (isReferrersClass) {
732 // Fast path, static storage base is this method's class
buzbeee1965672012-03-11 18:39:19 -0700733 RegLocation rlMethod = loadCurrMethod(cUnit);
buzbee31a4a6f2012-02-28 15:36:15 -0800734 rBase = oatAllocTemp(cUnit);
buzbeee1965672012-03-11 18:39:19 -0700735 loadWordDisp(cUnit, rlMethod.lowReg,
buzbee31a4a6f2012-02-28 15:36:15 -0800736 Method::DeclaringClassOffset().Int32Value(), rBase);
737 } else {
738 // Medium path, static storage base in a different class which
739 // requires checks that the other class is initialized
740 DCHECK_GE(ssbIndex, 0);
741 // May do runtime call so everything to home locations.
742 oatFlushAllRegs(cUnit);
743 // Using fixed register to sync with possible call to runtime
744 // support
buzbeee1965672012-03-11 18:39:19 -0700745 int rMethod = rARG1;
buzbee31a4a6f2012-02-28 15:36:15 -0800746 oatLockTemp(cUnit, rMethod);
747 loadCurrMethodDirect(cUnit, rMethod);
748 rBase = rARG0;
749 oatLockTemp(cUnit, rBase);
750 loadWordDisp(cUnit, rMethod,
751 Method::DexCacheInitializedStaticStorageOffset().Int32Value(),
752 rBase);
753 loadWordDisp(cUnit, rBase,
754 Array::DataOffset(sizeof(Object*)).Int32Value() +
755 sizeof(int32_t*) * ssbIndex,
756 rBase);
757 // rBase now points at appropriate static storage base (Class*)
758 // or NULL if not initialized. Check for NULL and call helper if NULL.
759 // TUNING: fast path should fall through
buzbee82488f52012-03-02 08:20:26 -0800760 LIR* branchOver = opCmpImmBranch(cUnit, kCondNe, rBase, 0, NULL);
Ian Rogersab2b55d2012-03-18 00:06:11 -0700761 callRuntimeHelperImm(cUnit,
762 OFFSETOF_MEMBER(Thread, pInitializeStaticStorage),
763 ssbIndex);
buzbee31a4a6f2012-02-28 15:36:15 -0800764#if defined(TARGET_MIPS)
765 // For Arm, rRET0 = rARG0 = rBASE, for Mips, we need to copy
buzbee82488f52012-03-02 08:20:26 -0800766 opRegCopy(cUnit, rBase, rRET0);
buzbee31a4a6f2012-02-28 15:36:15 -0800767#endif
768 LIR* skipTarget = newLIR0(cUnit, kPseudoTargetLabel);
buzbee31a4a6f2012-02-28 15:36:15 -0800769 branchOver->target = (LIR*)skipTarget;
buzbeee1965672012-03-11 18:39:19 -0700770 oatFreeTemp(cUnit, rMethod);
buzbee31a4a6f2012-02-28 15:36:15 -0800771 }
772 // rBase now holds static storage base
buzbee31a4a6f2012-02-28 15:36:15 -0800773 rlDest = isLongOrDouble ? oatGetDestWide(cUnit, mir, 0, 1)
774 : oatGetDest(cUnit, mir, 0);
775 RegLocation rlResult = oatEvalLoc(cUnit, rlDest, kAnyReg, true);
776 if (isVolatile) {
777 oatGenMemBarrier(cUnit, kSY);
778 }
779 if (isLongOrDouble) {
780 loadBaseDispWide(cUnit, NULL, rBase, fieldOffset, rlResult.lowReg,
781 rlResult.highReg, INVALID_SREG);
782 } else {
783 loadWordDisp(cUnit, rBase, fieldOffset, rlResult.lowReg);
784 }
785 oatFreeTemp(cUnit, rBase);
786 if (isLongOrDouble) {
787 storeValueWide(cUnit, rlDest, rlResult);
788 } else {
789 storeValue(cUnit, rlDest, rlResult);
790 }
791 } else {
792 oatFlushAllRegs(cUnit); // Everything to home locations
793 int getterOffset = isLongOrDouble ? OFFSETOF_MEMBER(Thread, pGet64Static) :
794 (isObject ? OFFSETOF_MEMBER(Thread, pGetObjStatic)
795 : OFFSETOF_MEMBER(Thread, pGet32Static));
Ian Rogersab2b55d2012-03-18 00:06:11 -0700796 callRuntimeHelperImm(cUnit, getterOffset, fieldIdx);
buzbee31a4a6f2012-02-28 15:36:15 -0800797 if (isLongOrDouble) {
Ian Rogersf7d9ad32012-03-13 18:45:39 -0700798 RegLocation rlResult = oatGetReturnWide(cUnit, rlDest.fp);
buzbee31a4a6f2012-02-28 15:36:15 -0800799 storeValueWide(cUnit, rlDest, rlResult);
800 } else {
Ian Rogersf7d9ad32012-03-13 18:45:39 -0700801 RegLocation rlResult = oatGetReturn(cUnit, rlDest.fp);
buzbee31a4a6f2012-02-28 15:36:15 -0800802 storeValue(cUnit, rlDest, rlResult);
803 }
804 }
805}
806
807
808// Debugging routine - if null target, branch to DebugMe
809void genShowTarget(CompilationUnit* cUnit)
810{
buzbeea7678db2012-03-05 15:35:46 -0800811#if defined(TARGET_X86)
812 UNIMPLEMENTED(WARNING) << "genShowTarget";
813#else
buzbee0398c422012-03-02 15:22:47 -0800814 LIR* branchOver = opCmpImmBranch(cUnit, kCondNe, rINVOKE_TGT, 0, NULL);
buzbee31a4a6f2012-02-28 15:36:15 -0800815 loadWordDisp(cUnit, rSELF,
buzbee0398c422012-03-02 15:22:47 -0800816 OFFSETOF_MEMBER(Thread, pDebugMe), rINVOKE_TGT);
buzbee31a4a6f2012-02-28 15:36:15 -0800817 LIR* target = newLIR0(cUnit, kPseudoTargetLabel);
buzbee31a4a6f2012-02-28 15:36:15 -0800818 branchOver->target = (LIR*)target;
buzbeea7678db2012-03-05 15:35:46 -0800819#endif
buzbee31a4a6f2012-02-28 15:36:15 -0800820}
821
822void genThrowVerificationError(CompilationUnit* cUnit, MIR* mir)
823{
Ian Rogersab2b55d2012-03-18 00:06:11 -0700824 callRuntimeHelperImmImm(cUnit, OFFSETOF_MEMBER(Thread,
825 pThrowVerificationErrorFromCode),
826 mir->dalvikInsn.vA, mir->dalvikInsn.vB);
buzbee31a4a6f2012-02-28 15:36:15 -0800827}
828
829void handleSuspendLaunchpads(CompilationUnit *cUnit)
830{
Ian Rogersab2b55d2012-03-18 00:06:11 -0700831 LIR** suspendLabel = (LIR **)cUnit->suspendLaunchpads.elemList;
buzbee31a4a6f2012-02-28 15:36:15 -0800832 int numElems = cUnit->suspendLaunchpads.numUsed;
buzbee31a4a6f2012-02-28 15:36:15 -0800833 for (int i = 0; i < numElems; i++) {
buzbeec5159d52012-03-03 11:48:39 -0800834 oatResetRegPool(cUnit);
buzbee31a4a6f2012-02-28 15:36:15 -0800835 LIR* lab = suspendLabel[i];
836 LIR* resumeLab = (LIR*)lab->operands[0];
837 cUnit->currentDalvikOffset = lab->operands[1];
Ian Rogersab2b55d2012-03-18 00:06:11 -0700838 oatAppendLIR(cUnit, lab);
Ian Rogers6cbb2bd2012-03-16 13:45:30 -0700839#if defined(TARGET_X86)
840 opThreadMem(cUnit, kOpBlx,
841 OFFSETOF_MEMBER(Thread, pTestSuspendFromCode));
842#else
buzbee31a4a6f2012-02-28 15:36:15 -0800843 int rTgt = loadHelper(cUnit, OFFSETOF_MEMBER(Thread,
844 pTestSuspendFromCode));
buzbee31a4a6f2012-02-28 15:36:15 -0800845 opReg(cUnit, kOpBlx, rTgt);
Ian Rogers6cbb2bd2012-03-16 13:45:30 -0700846#endif
buzbee82488f52012-03-02 08:20:26 -0800847 opUnconditionalBranch(cUnit, resumeLab);
buzbee31a4a6f2012-02-28 15:36:15 -0800848 }
849}
850
851void handleThrowLaunchpads(CompilationUnit *cUnit)
852{
Ian Rogersab2b55d2012-03-18 00:06:11 -0700853 LIR** throwLabel = (LIR **)cUnit->throwLaunchpads.elemList;
buzbee31a4a6f2012-02-28 15:36:15 -0800854 int numElems = cUnit->throwLaunchpads.numUsed;
Ian Rogersab2b55d2012-03-18 00:06:11 -0700855 for (int i = 0; i < numElems; i++) {
buzbeec5159d52012-03-03 11:48:39 -0800856 oatResetRegPool(cUnit);
buzbee31a4a6f2012-02-28 15:36:15 -0800857 LIR* lab = throwLabel[i];
858 cUnit->currentDalvikOffset = lab->operands[1];
Ian Rogersab2b55d2012-03-18 00:06:11 -0700859 oatAppendLIR(cUnit, lab);
buzbee31a4a6f2012-02-28 15:36:15 -0800860 int funcOffset = 0;
861 int v1 = lab->operands[2];
862 int v2 = lab->operands[3];
863 switch(lab->operands[0]) {
864 case kThrowNullPointer:
865 funcOffset = OFFSETOF_MEMBER(Thread, pThrowNullPointerFromCode);
866 break;
867 case kThrowArrayBounds:
buzbee5de34942012-03-01 14:51:57 -0800868 if (v2 != rARG0) {
buzbee82488f52012-03-02 08:20:26 -0800869 opRegCopy(cUnit, rARG0, v1);
870 opRegCopy(cUnit, rARG1, v2);
buzbee31a4a6f2012-02-28 15:36:15 -0800871 } else {
buzbee5de34942012-03-01 14:51:57 -0800872 if (v1 == rARG1) {
buzbee31a4a6f2012-02-28 15:36:15 -0800873#if defined(TARGET_ARM)
874 int rTmp = r12;
875#else
876 int rTmp = oatAllocTemp(cUnit);
877#endif
buzbee82488f52012-03-02 08:20:26 -0800878 opRegCopy(cUnit, rTmp, v1);
879 opRegCopy(cUnit, rARG1, v2);
880 opRegCopy(cUnit, rARG0, rTmp);
buzbee31a4a6f2012-02-28 15:36:15 -0800881 } else {
buzbee82488f52012-03-02 08:20:26 -0800882 opRegCopy(cUnit, rARG1, v2);
883 opRegCopy(cUnit, rARG0, v1);
buzbee31a4a6f2012-02-28 15:36:15 -0800884 }
885 }
886 funcOffset = OFFSETOF_MEMBER(Thread, pThrowArrayBoundsFromCode);
887 break;
888 case kThrowDivZero:
889 funcOffset = OFFSETOF_MEMBER(Thread, pThrowDivZeroFromCode);
890 break;
891 case kThrowVerificationError:
892 loadConstant(cUnit, rARG0, v1);
893 loadConstant(cUnit, rARG1, v2);
894 funcOffset =
895 OFFSETOF_MEMBER(Thread, pThrowVerificationErrorFromCode);
896 break;
897 case kThrowNegArraySize:
buzbee82488f52012-03-02 08:20:26 -0800898 opRegCopy(cUnit, rARG0, v1);
buzbee31a4a6f2012-02-28 15:36:15 -0800899 funcOffset =
900 OFFSETOF_MEMBER(Thread, pThrowNegArraySizeFromCode);
901 break;
902 case kThrowNoSuchMethod:
buzbee82488f52012-03-02 08:20:26 -0800903 opRegCopy(cUnit, rARG0, v1);
buzbee31a4a6f2012-02-28 15:36:15 -0800904 funcOffset =
905 OFFSETOF_MEMBER(Thread, pThrowNoSuchMethodFromCode);
906 break;
907 case kThrowStackOverflow:
908 funcOffset =
909 OFFSETOF_MEMBER(Thread, pThrowStackOverflowFromCode);
910 // Restore stack alignment
911 opRegImm(cUnit, kOpAdd, rSP,
912 (cUnit->numCoreSpills + cUnit->numFPSpills) * 4);
913 break;
914 default:
915 LOG(FATAL) << "Unexpected throw kind: " << lab->operands[0];
916 }
Ian Rogersab2b55d2012-03-18 00:06:11 -0700917 oatClobberCalleeSave(cUnit);
918#if !defined(TARGET_X86)
buzbee31a4a6f2012-02-28 15:36:15 -0800919 int rTgt = loadHelper(cUnit, funcOffset);
Ian Rogersab2b55d2012-03-18 00:06:11 -0700920 opReg(cUnit, kOpBlx, rTgt);
buzbee0398c422012-03-02 15:22:47 -0800921 oatFreeTemp(cUnit, rTgt);
Ian Rogersab2b55d2012-03-18 00:06:11 -0700922#else
923 opThreadMem(cUnit, kOpBlx, funcOffset);
Ian Rogers6cbb2bd2012-03-16 13:45:30 -0700924#endif
buzbee31a4a6f2012-02-28 15:36:15 -0800925 }
926}
927
928/* Needed by the Assembler */
929void oatSetupResourceMasks(LIR* lir)
930{
931 setupResourceMasks(lir);
932}
933
buzbee16da88c2012-03-20 10:38:17 -0700934bool fastInstance(CompilationUnit* cUnit, uint32_t fieldIdx,
935 int& fieldOffset, bool& isVolatile, bool isPut)
936{
937 OatCompilationUnit mUnit(cUnit->class_loader, cUnit->class_linker,
938 *cUnit->dex_file, *cUnit->dex_cache,
939 cUnit->code_item, cUnit->method_idx,
940 cUnit->access_flags);
941 return cUnit->compiler->ComputeInstanceFieldInfo(fieldIdx, &mUnit,
942 fieldOffset, isVolatile, isPut);
943}
944
buzbee31a4a6f2012-02-28 15:36:15 -0800945void genIGet(CompilationUnit* cUnit, MIR* mir, OpSize size,
946 RegLocation rlDest, RegLocation rlObj,
947 bool isLongOrDouble, bool isObject)
948{
949 int fieldOffset;
950 bool isVolatile;
951 uint32_t fieldIdx = mir->dalvikInsn.vC;
952
buzbee16da88c2012-03-20 10:38:17 -0700953 bool fastPath = fastInstance(cUnit, fieldIdx, fieldOffset, isVolatile,
954 false);
buzbee31a4a6f2012-02-28 15:36:15 -0800955
956 if (fastPath && !SLOW_FIELD_PATH) {
957 RegLocation rlResult;
958 RegisterClass regClass = oatRegClassBySize(size);
959 DCHECK_GE(fieldOffset, 0);
960 rlObj = loadValue(cUnit, rlObj, kCoreReg);
961 if (isLongOrDouble) {
962 DCHECK(rlDest.wide);
963 genNullCheck(cUnit, rlObj.sRegLow, rlObj.lowReg, mir);/* null? */
Ian Rogersb5d09b22012-03-06 22:14:17 -0800964#if defined(TARGET_X86)
965 rlResult = oatEvalLoc(cUnit, rlDest, regClass, true);
966 genNullCheck(cUnit, rlObj.sRegLow, rlObj.lowReg, mir);/* null? */
967 loadBaseDispWide(cUnit, mir, rlObj.lowReg, fieldOffset, rlResult.lowReg,
968 rlResult.highReg, rlObj.sRegLow);
969 if (isVolatile) {
970 oatGenMemBarrier(cUnit, kSY);
971 }
972#else
buzbee31a4a6f2012-02-28 15:36:15 -0800973 int regPtr = oatAllocTemp(cUnit);
974 opRegRegImm(cUnit, kOpAdd, regPtr, rlObj.lowReg, fieldOffset);
975 rlResult = oatEvalLoc(cUnit, rlDest, regClass, true);
976 loadPair(cUnit, regPtr, rlResult.lowReg, rlResult.highReg);
977 if (isVolatile) {
978 oatGenMemBarrier(cUnit, kSY);
979 }
980 oatFreeTemp(cUnit, regPtr);
Ian Rogersb5d09b22012-03-06 22:14:17 -0800981#endif
buzbee31a4a6f2012-02-28 15:36:15 -0800982 storeValueWide(cUnit, rlDest, rlResult);
983 } else {
984 rlResult = oatEvalLoc(cUnit, rlDest, regClass, true);
985 genNullCheck(cUnit, rlObj.sRegLow, rlObj.lowReg, mir);/* null? */
986 loadBaseDisp(cUnit, mir, rlObj.lowReg, fieldOffset, rlResult.lowReg,
987 kWord, rlObj.sRegLow);
988 if (isVolatile) {
989 oatGenMemBarrier(cUnit, kSY);
990 }
991 storeValue(cUnit, rlDest, rlResult);
992 }
993 } else {
994 int getterOffset = isLongOrDouble ? OFFSETOF_MEMBER(Thread, pGet64Instance) :
995 (isObject ? OFFSETOF_MEMBER(Thread, pGetObjInstance)
996 : OFFSETOF_MEMBER(Thread, pGet32Instance));
Ian Rogersab2b55d2012-03-18 00:06:11 -0700997 callRuntimeHelperImmRegLocation(cUnit, getterOffset, fieldIdx, rlObj);
buzbee31a4a6f2012-02-28 15:36:15 -0800998 if (isLongOrDouble) {
Ian Rogersf7d9ad32012-03-13 18:45:39 -0700999 RegLocation rlResult = oatGetReturnWide(cUnit, rlDest.fp);
buzbee31a4a6f2012-02-28 15:36:15 -08001000 storeValueWide(cUnit, rlDest, rlResult);
1001 } else {
Ian Rogersf7d9ad32012-03-13 18:45:39 -07001002 RegLocation rlResult = oatGetReturn(cUnit, rlDest.fp);
buzbee31a4a6f2012-02-28 15:36:15 -08001003 storeValue(cUnit, rlDest, rlResult);
1004 }
1005 }
1006}
1007
1008void genIPut(CompilationUnit* cUnit, MIR* mir, OpSize size, RegLocation rlSrc,
1009 RegLocation rlObj, bool isLongOrDouble, bool isObject)
1010{
1011 int fieldOffset;
1012 bool isVolatile;
1013 uint32_t fieldIdx = mir->dalvikInsn.vC;
1014
buzbee16da88c2012-03-20 10:38:17 -07001015 bool fastPath = fastInstance(cUnit, fieldIdx, fieldOffset, isVolatile,
1016 true);
buzbee31a4a6f2012-02-28 15:36:15 -08001017 if (fastPath && !SLOW_FIELD_PATH) {
1018 RegisterClass regClass = oatRegClassBySize(size);
1019 DCHECK_GE(fieldOffset, 0);
1020 rlObj = loadValue(cUnit, rlObj, kCoreReg);
1021 if (isLongOrDouble) {
1022 int regPtr;
1023 rlSrc = loadValueWide(cUnit, rlSrc, kAnyReg);
1024 genNullCheck(cUnit, rlObj.sRegLow, rlObj.lowReg, mir);/* null? */
1025 regPtr = oatAllocTemp(cUnit);
1026 opRegRegImm(cUnit, kOpAdd, regPtr, rlObj.lowReg, fieldOffset);
1027 if (isVolatile) {
1028 oatGenMemBarrier(cUnit, kST);
1029 }
1030 storePair(cUnit, regPtr, rlSrc.lowReg, rlSrc.highReg);
1031 if (isVolatile) {
1032 oatGenMemBarrier(cUnit, kSY);
1033 }
1034 oatFreeTemp(cUnit, regPtr);
1035 } else {
1036 rlSrc = loadValue(cUnit, rlSrc, regClass);
1037 genNullCheck(cUnit, rlObj.sRegLow, rlObj.lowReg, mir);/* null? */
1038 if (isVolatile) {
1039 oatGenMemBarrier(cUnit, kST);
1040 }
1041 storeBaseDisp(cUnit, rlObj.lowReg, fieldOffset, rlSrc.lowReg, kWord);
1042 if (isVolatile) {
1043 oatGenMemBarrier(cUnit, kSY);
1044 }
buzbeea7c12682012-03-19 13:13:53 -07001045 if (isObject) {
1046 markGCCard(cUnit, rlSrc.lowReg, rlObj.lowReg);
1047 }
buzbee31a4a6f2012-02-28 15:36:15 -08001048 }
1049 } else {
1050 int setterOffset = isLongOrDouble ? OFFSETOF_MEMBER(Thread, pSet64Instance) :
1051 (isObject ? OFFSETOF_MEMBER(Thread, pSetObjInstance)
1052 : OFFSETOF_MEMBER(Thread, pSet32Instance));
Ian Rogersab2b55d2012-03-18 00:06:11 -07001053 callRuntimeHelperImmRegLocationRegLocation(cUnit, setterOffset,
1054 fieldIdx, rlObj, rlSrc);
buzbee31a4a6f2012-02-28 15:36:15 -08001055 }
1056}
1057
1058void genConstClass(CompilationUnit* cUnit, MIR* mir, RegLocation rlDest,
1059 RegLocation rlSrc)
1060{
1061 uint32_t type_idx = mir->dalvikInsn.vB;
buzbeee1965672012-03-11 18:39:19 -07001062 RegLocation rlMethod = loadCurrMethod(cUnit);
buzbee31a4a6f2012-02-28 15:36:15 -08001063 int resReg = oatAllocTemp(cUnit);
1064 RegLocation rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
1065 if (!cUnit->compiler->CanAccessTypeWithoutChecks(cUnit->method_idx,
1066 cUnit->dex_cache,
1067 *cUnit->dex_file,
1068 type_idx)) {
1069 // Call out to helper which resolves type and verifies access.
1070 // Resolved type returned in rRET0.
Ian Rogersab2b55d2012-03-18 00:06:11 -07001071 callRuntimeHelperImmReg(cUnit,
1072 OFFSETOF_MEMBER(Thread,
1073 pInitializeTypeAndVerifyAccessFromCode),
1074 type_idx, rlMethod.lowReg);
Ian Rogersf7d9ad32012-03-13 18:45:39 -07001075 RegLocation rlResult = oatGetReturn(cUnit, false);
buzbee31a4a6f2012-02-28 15:36:15 -08001076 storeValue(cUnit, rlDest, rlResult);
1077 } else {
1078 // We're don't need access checks, load type from dex cache
1079 int32_t dex_cache_offset =
1080 Method::DexCacheResolvedTypesOffset().Int32Value();
buzbeee1965672012-03-11 18:39:19 -07001081 loadWordDisp(cUnit, rlMethod.lowReg, dex_cache_offset, resReg);
buzbee31a4a6f2012-02-28 15:36:15 -08001082 int32_t offset_of_type =
1083 Array::DataOffset(sizeof(Class*)).Int32Value() + (sizeof(Class*)
1084 * type_idx);
1085 loadWordDisp(cUnit, resReg, offset_of_type, rlResult.lowReg);
1086 if (!cUnit->compiler->CanAssumeTypeIsPresentInDexCache(cUnit->dex_cache,
1087 type_idx) || SLOW_TYPE_PATH) {
1088 // Slow path, at runtime test if type is null and if so initialize
1089 oatFlushAllRegs(cUnit);
buzbee82488f52012-03-02 08:20:26 -08001090 LIR* branch1 = opCmpImmBranch(cUnit, kCondEq, rlResult.lowReg, 0,
1091 NULL);
buzbee31a4a6f2012-02-28 15:36:15 -08001092 // Resolved, store and hop over following code
1093 storeValue(cUnit, rlDest, rlResult);
buzbee3d661942012-03-14 17:37:27 -07001094 /*
1095 * Because we have stores of the target value on two paths,
1096 * clobber temp tracking for the destination using the ssa name
1097 */
1098 oatClobberSReg(cUnit, rlDest.sRegLow);
buzbee82488f52012-03-02 08:20:26 -08001099 LIR* branch2 = opUnconditionalBranch(cUnit,0);
buzbee31a4a6f2012-02-28 15:36:15 -08001100 // TUNING: move slow path to end & remove unconditional branch
1101 LIR* target1 = newLIR0(cUnit, kPseudoTargetLabel);
buzbee5de34942012-03-01 14:51:57 -08001102 // Call out to helper, which will return resolved type in rARG0
Ian Rogersab2b55d2012-03-18 00:06:11 -07001103 callRuntimeHelperImmReg(cUnit, OFFSETOF_MEMBER(Thread,
1104 pInitializeTypeFromCode),
1105 type_idx, rlMethod.lowReg);
Ian Rogersf7d9ad32012-03-13 18:45:39 -07001106 RegLocation rlResult = oatGetReturn(cUnit, false);
buzbee31a4a6f2012-02-28 15:36:15 -08001107 storeValue(cUnit, rlDest, rlResult);
buzbee3d661942012-03-14 17:37:27 -07001108 /*
1109 * Because we have stores of the target value on two paths,
1110 * clobber temp tracking for the destination using the ssa name
1111 */
1112 oatClobberSReg(cUnit, rlDest.sRegLow);
buzbee31a4a6f2012-02-28 15:36:15 -08001113 // Rejoin code paths
1114 LIR* target2 = newLIR0(cUnit, kPseudoTargetLabel);
buzbee31a4a6f2012-02-28 15:36:15 -08001115 branch1->target = (LIR*)target1;
1116 branch2->target = (LIR*)target2;
1117 } else {
1118 // Fast path, we're done - just store result
1119 storeValue(cUnit, rlDest, rlResult);
1120 }
1121 }
1122}
Ian Rogersab2b55d2012-03-18 00:06:11 -07001123
buzbee31a4a6f2012-02-28 15:36:15 -08001124void genConstString(CompilationUnit* cUnit, MIR* mir, RegLocation rlDest,
1125 RegLocation rlSrc)
1126{
1127 /* NOTE: Most strings should be available at compile time */
1128 uint32_t string_idx = mir->dalvikInsn.vB;
1129 int32_t offset_of_string = Array::DataOffset(sizeof(String*)).Int32Value() +
1130 (sizeof(String*) * string_idx);
1131 if (!cUnit->compiler->CanAssumeStringIsPresentInDexCache(
1132 cUnit->dex_cache, string_idx) || SLOW_STRING_PATH) {
1133 // slow path, resolve string if not in dex cache
1134 oatFlushAllRegs(cUnit);
1135 oatLockCallTemps(cUnit); // Using explicit registers
1136 loadCurrMethodDirect(cUnit, rARG2);
1137 loadWordDisp(cUnit, rARG2,
1138 Method::DexCacheStringsOffset().Int32Value(), rARG0);
1139 // Might call out to helper, which will return resolved string in rRET0
Ian Rogers6cbb2bd2012-03-16 13:45:30 -07001140#if !defined(TARGET_X86)
buzbee31a4a6f2012-02-28 15:36:15 -08001141 int rTgt = loadHelper(cUnit, OFFSETOF_MEMBER(Thread,
1142 pResolveStringFromCode));
Ian Rogers6cbb2bd2012-03-16 13:45:30 -07001143#endif
buzbee31a4a6f2012-02-28 15:36:15 -08001144 loadWordDisp(cUnit, rRET0, offset_of_string, rARG0);
1145 loadConstant(cUnit, rARG1, string_idx);
1146#if defined(TARGET_ARM)
1147 opRegImm(cUnit, kOpCmp, rRET0, 0); // Is resolved?
1148 genBarrier(cUnit);
1149 // For testing, always force through helper
1150 if (!EXERCISE_SLOWEST_STRING_PATH) {
buzbee82488f52012-03-02 08:20:26 -08001151 opIT(cUnit, kArmCondEq, "T");
buzbee31a4a6f2012-02-28 15:36:15 -08001152 }
buzbee82488f52012-03-02 08:20:26 -08001153 opRegCopy(cUnit, rARG0, rARG2); // .eq
buzbee31a4a6f2012-02-28 15:36:15 -08001154 opReg(cUnit, kOpBlx, rTgt); // .eq, helper(Method*, string_idx)
Ian Rogers6cbb2bd2012-03-16 13:45:30 -07001155 oatFreeTemp(cUnit, rTgt);
1156#elif defined(TARGET_MIPS)
buzbee82488f52012-03-02 08:20:26 -08001157 LIR* branch = opCmpImmBranch(cUnit, kCondNe, rRET0, 0, NULL);
1158 opRegCopy(cUnit, rARG0, rARG2); // .eq
buzbee31a4a6f2012-02-28 15:36:15 -08001159 opReg(cUnit, kOpBlx, rTgt);
Ian Rogers6cbb2bd2012-03-16 13:45:30 -07001160 oatFreeTemp(cUnit, rTgt);
buzbee31a4a6f2012-02-28 15:36:15 -08001161 LIR* target = newLIR0(cUnit, kPseudoTargetLabel);
buzbee31a4a6f2012-02-28 15:36:15 -08001162 branch->target = target;
Ian Rogers6cbb2bd2012-03-16 13:45:30 -07001163#else
Ian Rogersab2b55d2012-03-18 00:06:11 -07001164 callRuntimeHelperRegReg(cUnit, OFFSETOF_MEMBER(Thread,
1165 pResolveStringFromCode),
1166 rARG2, rARG1);
buzbee31a4a6f2012-02-28 15:36:15 -08001167#endif
1168 genBarrier(cUnit);
Ian Rogersf7d9ad32012-03-13 18:45:39 -07001169 storeValue(cUnit, rlDest, oatGetReturn(cUnit, false));
buzbee31a4a6f2012-02-28 15:36:15 -08001170 } else {
buzbeee1965672012-03-11 18:39:19 -07001171 RegLocation rlMethod = loadCurrMethod(cUnit);
buzbee31a4a6f2012-02-28 15:36:15 -08001172 int resReg = oatAllocTemp(cUnit);
1173 RegLocation rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
buzbeee1965672012-03-11 18:39:19 -07001174 loadWordDisp(cUnit, rlMethod.lowReg,
buzbee31a4a6f2012-02-28 15:36:15 -08001175 Method::DexCacheStringsOffset().Int32Value(), resReg);
1176 loadWordDisp(cUnit, resReg, offset_of_string, rlResult.lowReg);
1177 storeValue(cUnit, rlDest, rlResult);
1178 }
1179}
1180
1181/*
1182 * Let helper function take care of everything. Will
1183 * call Class::NewInstanceFromCode(type_idx, method);
1184 */
1185void genNewInstance(CompilationUnit* cUnit, MIR* mir, RegLocation rlDest)
1186{
1187 oatFlushAllRegs(cUnit); /* Everything to home location */
1188 uint32_t type_idx = mir->dalvikInsn.vB;
1189 // alloc will always check for resolution, do we also need to verify
1190 // access because the verifier was unable to?
Ian Rogers6cbb2bd2012-03-16 13:45:30 -07001191 int funcOffset;
buzbee31a4a6f2012-02-28 15:36:15 -08001192 if (cUnit->compiler->CanAccessInstantiableTypeWithoutChecks(
1193 cUnit->method_idx, cUnit->dex_cache, *cUnit->dex_file, type_idx)) {
Ian Rogers6cbb2bd2012-03-16 13:45:30 -07001194 funcOffset = OFFSETOF_MEMBER(Thread, pAllocObjectFromCode);
buzbee31a4a6f2012-02-28 15:36:15 -08001195 } else {
Ian Rogers6cbb2bd2012-03-16 13:45:30 -07001196 funcOffset =
1197 OFFSETOF_MEMBER(Thread, pAllocObjectFromCodeWithAccessCheck);
buzbee31a4a6f2012-02-28 15:36:15 -08001198 }
Ian Rogersab2b55d2012-03-18 00:06:11 -07001199 callRuntimeHelperImmMethod(cUnit, funcOffset, type_idx);
Ian Rogersf7d9ad32012-03-13 18:45:39 -07001200 RegLocation rlResult = oatGetReturn(cUnit, false);
buzbee31a4a6f2012-02-28 15:36:15 -08001201 storeValue(cUnit, rlDest, rlResult);
1202}
1203
Ian Rogersab2b55d2012-03-18 00:06:11 -07001204void genThrow(CompilationUnit* cUnit, MIR* mir, RegLocation rlSrc)
1205{
1206 oatFlushAllRegs(cUnit);
1207 callRuntimeHelperRegLocation(cUnit, OFFSETOF_MEMBER(Thread, pDeliverException), rlSrc);
1208}
1209
buzbee31a4a6f2012-02-28 15:36:15 -08001210void genInstanceof(CompilationUnit* cUnit, MIR* mir, RegLocation rlDest,
1211 RegLocation rlSrc)
1212{
1213 oatFlushAllRegs(cUnit);
1214 // May generate a call - use explicit registers
1215 oatLockCallTemps(cUnit);
1216 uint32_t type_idx = mir->dalvikInsn.vC;
buzbee5de34942012-03-01 14:51:57 -08001217 loadCurrMethodDirect(cUnit, rARG1); // rARG1 <= current Method*
buzbee31a4a6f2012-02-28 15:36:15 -08001218 int classReg = rARG2; // rARG2 will hold the Class*
1219 if (!cUnit->compiler->CanAccessTypeWithoutChecks(cUnit->method_idx,
1220 cUnit->dex_cache,
1221 *cUnit->dex_file,
1222 type_idx)) {
1223 // Check we have access to type_idx and if not throw IllegalAccessError,
buzbee5de34942012-03-01 14:51:57 -08001224 // returns Class* in rARG0
Ian Rogersab2b55d2012-03-18 00:06:11 -07001225 callRuntimeHelperImm(cUnit,
1226 OFFSETOF_MEMBER(Thread, pInitializeTypeAndVerifyAccessFromCode),
1227 type_idx);
buzbee82488f52012-03-02 08:20:26 -08001228 opRegCopy(cUnit, classReg, rRET0); // Align usage with fast path
buzbee5de34942012-03-01 14:51:57 -08001229 loadValueDirectFixed(cUnit, rlSrc, rARG0); // rARG0 <= ref
buzbee31a4a6f2012-02-28 15:36:15 -08001230 } else {
buzbee5de34942012-03-01 14:51:57 -08001231 // Load dex cache entry into classReg (rARG2)
buzbee31a4a6f2012-02-28 15:36:15 -08001232 loadValueDirectFixed(cUnit, rlSrc, rARG0); // rARG0 <= ref
1233 loadWordDisp(cUnit, rARG1,
1234 Method::DexCacheResolvedTypesOffset().Int32Value(),
1235 classReg);
1236 int32_t offset_of_type =
1237 Array::DataOffset(sizeof(Class*)).Int32Value() + (sizeof(Class*)
1238 * type_idx);
1239 loadWordDisp(cUnit, classReg, offset_of_type, classReg);
1240 if (!cUnit->compiler->CanAssumeTypeIsPresentInDexCache(
1241 cUnit->dex_cache, type_idx)) {
1242 // Need to test presence of type in dex cache at runtime
buzbee82488f52012-03-02 08:20:26 -08001243 LIR* hopBranch = opCmpImmBranch(cUnit, kCondNe, classReg, 0, NULL);
buzbee31a4a6f2012-02-28 15:36:15 -08001244 // Not resolved
1245 // Call out to helper, which will return resolved type in rRET0
Ian Rogersab2b55d2012-03-18 00:06:11 -07001246 callRuntimeHelperImm(cUnit, OFFSETOF_MEMBER(Thread,
1247 pInitializeTypeFromCode),
1248 type_idx);
buzbee82488f52012-03-02 08:20:26 -08001249 opRegCopy(cUnit, rARG2, rRET0); // Align usage with fast path
buzbee31a4a6f2012-02-28 15:36:15 -08001250 loadValueDirectFixed(cUnit, rlSrc, rARG0); /* reload Ref */
1251 // Rejoin code paths
1252 LIR* hopTarget = newLIR0(cUnit, kPseudoTargetLabel);
buzbee31a4a6f2012-02-28 15:36:15 -08001253 hopBranch->target = (LIR*)hopTarget;
1254 }
1255 }
1256 /* rARG0 is ref, rARG2 is class. If ref==null, use directly as bool result */
buzbee82488f52012-03-02 08:20:26 -08001257 LIR* branch1 = opCmpImmBranch(cUnit, kCondEq, rARG0, 0, NULL);
buzbee31a4a6f2012-02-28 15:36:15 -08001258 /* load object->clazz */
1259 DCHECK_EQ(Object::ClassOffset().Int32Value(), 0);
1260 loadWordDisp(cUnit, rARG0, Object::ClassOffset().Int32Value(), rARG1);
1261 /* rARG0 is ref, rARG1 is ref->clazz, rARG2 is class */
buzbee0398c422012-03-02 15:22:47 -08001262#if defined(TARGET_ARM)
1263 /* Uses conditional nullification */
buzbee31a4a6f2012-02-28 15:36:15 -08001264 int rTgt = loadHelper(cUnit, OFFSETOF_MEMBER(Thread,
1265 pInstanceofNonTrivialFromCode));
buzbee31a4a6f2012-02-28 15:36:15 -08001266 opRegReg(cUnit, kOpCmp, rARG1, rARG2); // Same?
buzbee82488f52012-03-02 08:20:26 -08001267 opIT(cUnit, kArmCondEq, "EE"); // if-convert the test
buzbee31a4a6f2012-02-28 15:36:15 -08001268 loadConstant(cUnit, rARG0, 1); // .eq case - load true
buzbee82488f52012-03-02 08:20:26 -08001269 opRegCopy(cUnit, rARG0, rARG2); // .ne case - arg0 <= class
buzbee31a4a6f2012-02-28 15:36:15 -08001270 opReg(cUnit, kOpBlx, rTgt); // .ne case: helper(class, ref->class)
Ian Rogersab2b55d2012-03-18 00:06:11 -07001271 oatFreeTemp(cUnit, rTgt);
buzbee31a4a6f2012-02-28 15:36:15 -08001272#else
buzbee0398c422012-03-02 15:22:47 -08001273 /* Uses branchovers */
1274 loadConstant(cUnit, rARG0, 1); // assume true
1275 LIR* branchover = opCmpBranch(cUnit, kCondEq, rARG1, rARG2, NULL);
Ian Rogers6cbb2bd2012-03-16 13:45:30 -07001276#if !defined(TARGET_X86)
buzbee0398c422012-03-02 15:22:47 -08001277 int rTgt = loadHelper(cUnit, OFFSETOF_MEMBER(Thread,
1278 pInstanceofNonTrivialFromCode));
1279 opRegCopy(cUnit, rARG0, rARG2); // .ne case - arg0 <= class
1280 opReg(cUnit, kOpBlx, rTgt); // .ne case: helper(class, ref->class)
Ian Rogersab2b55d2012-03-18 00:06:11 -07001281 oatFreeTemp(cUnit, rTgt);
Ian Rogers6cbb2bd2012-03-16 13:45:30 -07001282#else
1283 opRegCopy(cUnit, rARG0, rARG2);
Ian Rogersab2b55d2012-03-18 00:06:11 -07001284 opThreadMem(cUnit, kOpBlx,
1285 OFFSETOF_MEMBER(Thread, pInstanceofNonTrivialFromCode));
Ian Rogers6cbb2bd2012-03-16 13:45:30 -07001286#endif
buzbee31a4a6f2012-02-28 15:36:15 -08001287#endif
buzbee0398c422012-03-02 15:22:47 -08001288 oatClobberCalleeSave(cUnit);
1289 /* branch targets here */
buzbee31a4a6f2012-02-28 15:36:15 -08001290 LIR* target = newLIR0(cUnit, kPseudoTargetLabel);
Ian Rogersf7d9ad32012-03-13 18:45:39 -07001291 RegLocation rlResult = oatGetReturn(cUnit, false);
buzbee31a4a6f2012-02-28 15:36:15 -08001292 storeValue(cUnit, rlDest, rlResult);
buzbee0398c422012-03-02 15:22:47 -08001293 branch1->target = target;
1294#if !defined(TARGET_ARM)
1295 branchover->target = target;
1296#endif
buzbee31a4a6f2012-02-28 15:36:15 -08001297}
1298
1299void genCheckCast(CompilationUnit* cUnit, MIR* mir, RegLocation rlSrc)
1300{
1301 oatFlushAllRegs(cUnit);
1302 // May generate a call - use explicit registers
1303 oatLockCallTemps(cUnit);
1304 uint32_t type_idx = mir->dalvikInsn.vB;
1305 loadCurrMethodDirect(cUnit, rARG1); // rARG1 <= current Method*
1306 int classReg = rARG2; // rARG2 will hold the Class*
1307 if (!cUnit->compiler->CanAccessTypeWithoutChecks(cUnit->method_idx,
1308 cUnit->dex_cache,
1309 *cUnit->dex_file,
1310 type_idx)) {
1311 // Check we have access to type_idx and if not throw IllegalAccessError,
1312 // returns Class* in rRET0
Ian Rogersab2b55d2012-03-18 00:06:11 -07001313 // InitializeTypeAndVerifyAccess(idx, method)
1314 callRuntimeHelperImmReg(cUnit,
1315 OFFSETOF_MEMBER(Thread,
1316 pInitializeTypeAndVerifyAccessFromCode),
1317 type_idx, rARG1);
buzbee82488f52012-03-02 08:20:26 -08001318 opRegCopy(cUnit, classReg, rRET0); // Align usage with fast path
buzbee31a4a6f2012-02-28 15:36:15 -08001319 } else {
1320 // Load dex cache entry into classReg (rARG2)
1321 loadWordDisp(cUnit, rARG1,
1322 Method::DexCacheResolvedTypesOffset().Int32Value(),
1323 classReg);
1324 int32_t offset_of_type =
1325 Array::DataOffset(sizeof(Class*)).Int32Value() +
1326 (sizeof(Class*) * type_idx);
1327 loadWordDisp(cUnit, classReg, offset_of_type, classReg);
1328 if (!cUnit->compiler->CanAssumeTypeIsPresentInDexCache(
1329 cUnit->dex_cache, type_idx)) {
1330 // Need to test presence of type in dex cache at runtime
buzbee82488f52012-03-02 08:20:26 -08001331 LIR* hopBranch = opCmpImmBranch(cUnit, kCondNe, classReg, 0, NULL);
buzbee31a4a6f2012-02-28 15:36:15 -08001332 // Not resolved
buzbee5de34942012-03-01 14:51:57 -08001333 // Call out to helper, which will return resolved type in rARG0
Ian Rogersab2b55d2012-03-18 00:06:11 -07001334 // InitializeTypeFromCode(idx, method)
1335 callRuntimeHelperImmReg(cUnit,
1336 OFFSETOF_MEMBER(Thread, pInitializeTypeFromCode),
1337 type_idx, rARG1);
buzbee82488f52012-03-02 08:20:26 -08001338 opRegCopy(cUnit, classReg, rARG0); // Align usage with fast path
buzbee31a4a6f2012-02-28 15:36:15 -08001339 // Rejoin code paths
1340 LIR* hopTarget = newLIR0(cUnit, kPseudoTargetLabel);
buzbee31a4a6f2012-02-28 15:36:15 -08001341 hopBranch->target = (LIR*)hopTarget;
1342 }
1343 }
buzbee5de34942012-03-01 14:51:57 -08001344 // At this point, classReg (rARG2) has class
buzbee31a4a6f2012-02-28 15:36:15 -08001345 loadValueDirectFixed(cUnit, rlSrc, rARG0); // rARG0 <= ref
1346 /* Null is OK - continue */
buzbee82488f52012-03-02 08:20:26 -08001347 LIR* branch1 = opCmpImmBranch(cUnit, kCondEq, rARG0, 0, NULL);
buzbee31a4a6f2012-02-28 15:36:15 -08001348 /* load object->clazz */
1349 DCHECK_EQ(Object::ClassOffset().Int32Value(), 0);
1350 loadWordDisp(cUnit, rARG0, Object::ClassOffset().Int32Value(), rARG1);
1351 /* rARG1 now contains object->clazz */
Ian Rogersab2b55d2012-03-18 00:06:11 -07001352#if defined(TARGET_MIPS) || defined(TARGET_X86)
buzbee82488f52012-03-02 08:20:26 -08001353 LIR* branch2 = opCmpBranch(cUnit, kCondEq, rARG1, classReg, NULL);
Ian Rogersab2b55d2012-03-18 00:06:11 -07001354 callRuntimeHelperRegReg(cUnit, OFFSETOF_MEMBER(Thread,
1355 pCheckCastFromCode),
1356 rARG1, rARG2);
1357#else // defined(TARGET_ARM)
Ian Rogers6cbb2bd2012-03-16 13:45:30 -07001358 int rTgt = loadHelper(cUnit, OFFSETOF_MEMBER(Thread,
1359 pCheckCastFromCode));
1360 opRegReg(cUnit, kOpCmp, rARG1, classReg);
1361 LIR* branch2 = opCondBranch(cUnit, kCondEq, NULL); /* If eq, trivial yes */
1362 opRegCopy(cUnit, rARG0, rARG1);
1363 opRegCopy(cUnit, rARG1, rARG2);
Ian Rogersab2b55d2012-03-18 00:06:11 -07001364 oatClobberCalleeSave(cUnit);
1365 opReg(cUnit, kOpBlx, rTgt);
Ian Rogers6cbb2bd2012-03-16 13:45:30 -07001366 oatFreeTemp(cUnit, rTgt);
Ian Rogers6cbb2bd2012-03-16 13:45:30 -07001367#endif
buzbee31a4a6f2012-02-28 15:36:15 -08001368 /* branch target here */
1369 LIR* target = newLIR0(cUnit, kPseudoTargetLabel);
buzbee31a4a6f2012-02-28 15:36:15 -08001370 branch1->target = (LIR*)target;
1371 branch2->target = (LIR*)target;
1372}
1373
buzbee31a4a6f2012-02-28 15:36:15 -08001374/*
1375 * Generate array store
1376 *
1377 */
1378void genArrayObjPut(CompilationUnit* cUnit, MIR* mir, RegLocation rlArray,
1379 RegLocation rlIndex, RegLocation rlSrc, int scale)
1380{
1381 RegisterClass regClass = oatRegClassBySize(kWord);
1382 int lenOffset = Array::LengthOffset().Int32Value();
1383 int dataOffset = Array::DataOffset(sizeof(Object*)).Int32Value();
1384
1385 oatFlushAllRegs(cUnit);
1386 /* Make sure it's a legal object Put. Use direct regs at first */
1387 loadValueDirectFixed(cUnit, rlArray, rARG1);
1388 loadValueDirectFixed(cUnit, rlSrc, rARG0);
1389
1390 /* null array object? */
1391 genNullCheck(cUnit, rlArray.sRegLow, rARG1, mir);
buzbee31a4a6f2012-02-28 15:36:15 -08001392 /* Get the array's clazz */
1393 loadWordDisp(cUnit, rARG1, Object::ClassOffset().Int32Value(), rARG1);
Ian Rogersab2b55d2012-03-18 00:06:11 -07001394 callRuntimeHelperRegReg(cUnit, OFFSETOF_MEMBER(Thread,
1395 pCanPutArrayElementFromCode),
1396 rARG0, rARG1);
buzbee31a4a6f2012-02-28 15:36:15 -08001397 oatFreeTemp(cUnit, rARG0);
1398 oatFreeTemp(cUnit, rARG1);
1399
1400 // Now, redo loadValues in case they didn't survive the call
1401
buzbee31a4a6f2012-02-28 15:36:15 -08001402 rlArray = loadValue(cUnit, rlArray, kCoreReg);
1403 rlIndex = loadValue(cUnit, rlIndex, kCoreReg);
1404
Ian Rogersb41b33b2012-03-20 14:22:54 -07001405#if defined(TARGET_X86)
1406 if (!(mir->optimizationFlags & MIR_IGNORE_RANGE_CHECK)) {
1407 /* if (rlIndex >= [rlArray + lenOffset]) goto kThrowArrayBounds */
1408 genRegMemCheck(cUnit, kCondUge, rlIndex.lowReg, rlArray.lowReg,
1409 lenOffset, mir, kThrowArrayBounds);
1410 }
1411 rlSrc = loadValue(cUnit, rlSrc, regClass);
1412 storeBaseIndexedDisp(cUnit, NULL, rlArray.lowReg, rlIndex.lowReg, scale,
1413 dataOffset, rlSrc.lowReg, INVALID_REG, kWord,
1414 INVALID_SREG);
1415#else
1416 int regPtr;
buzbee31a4a6f2012-02-28 15:36:15 -08001417 if (oatIsTemp(cUnit, rlArray.lowReg)) {
1418 oatClobber(cUnit, rlArray.lowReg);
1419 regPtr = rlArray.lowReg;
1420 } else {
1421 regPtr = oatAllocTemp(cUnit);
buzbee82488f52012-03-02 08:20:26 -08001422 opRegCopy(cUnit, regPtr, rlArray.lowReg);
buzbee31a4a6f2012-02-28 15:36:15 -08001423 }
1424
buzbee239c4e72012-03-16 08:42:29 -07001425 bool needsRangeCheck = (!(mir->optimizationFlags & MIR_IGNORE_RANGE_CHECK));
1426 int regLen = INVALID_REG;
1427 if (needsRangeCheck) {
1428 regLen = oatAllocTemp(cUnit);
buzbee31a4a6f2012-02-28 15:36:15 -08001429 //NOTE: max live temps(4) here.
1430 /* Get len */
1431 loadWordDisp(cUnit, rlArray.lowReg, lenOffset, regLen);
buzbee239c4e72012-03-16 08:42:29 -07001432 }
1433 /* regPtr -> array data */
1434 opRegImm(cUnit, kOpAdd, regPtr, dataOffset);
1435 /* at this point, regPtr points to array, 2 live temps */
1436 rlSrc = loadValue(cUnit, rlSrc, regClass);
1437 if (needsRangeCheck) {
buzbee31a4a6f2012-02-28 15:36:15 -08001438 genRegRegCheck(cUnit, kCondCs, rlIndex.lowReg, regLen, mir,
1439 kThrowArrayBounds);
1440 oatFreeTemp(cUnit, regLen);
buzbee31a4a6f2012-02-28 15:36:15 -08001441 }
buzbee31a4a6f2012-02-28 15:36:15 -08001442 storeBaseIndexed(cUnit, regPtr, rlIndex.lowReg, rlSrc.lowReg,
1443 scale, kWord);
Ian Rogersb41b33b2012-03-20 14:22:54 -07001444#endif
buzbeea7c12682012-03-19 13:13:53 -07001445 markGCCard(cUnit, rlSrc.lowReg, rlArray.lowReg);
buzbee31a4a6f2012-02-28 15:36:15 -08001446}
1447
1448/*
1449 * Generate array load
1450 */
1451void genArrayGet(CompilationUnit* cUnit, MIR* mir, OpSize size,
1452 RegLocation rlArray, RegLocation rlIndex,
1453 RegLocation rlDest, int scale)
1454{
1455 RegisterClass regClass = oatRegClassBySize(size);
1456 int lenOffset = Array::LengthOffset().Int32Value();
1457 int dataOffset;
1458 RegLocation rlResult;
1459 rlArray = loadValue(cUnit, rlArray, kCoreReg);
1460 rlIndex = loadValue(cUnit, rlIndex, kCoreReg);
buzbee31a4a6f2012-02-28 15:36:15 -08001461
1462 if (size == kLong || size == kDouble) {
1463 dataOffset = Array::DataOffset(sizeof(int64_t)).Int32Value();
1464 } else {
1465 dataOffset = Array::DataOffset(sizeof(int32_t)).Int32Value();
1466 }
1467
1468 /* null object? */
1469 genNullCheck(cUnit, rlArray.sRegLow, rlArray.lowReg, mir);
1470
Ian Rogersb5d09b22012-03-06 22:14:17 -08001471#if defined(TARGET_X86)
1472 if (!(mir->optimizationFlags & MIR_IGNORE_RANGE_CHECK)) {
1473 /* if (rlIndex >= [rlArray + lenOffset]) goto kThrowArrayBounds */
1474 genRegMemCheck(cUnit, kCondUge, rlIndex.lowReg, rlArray.lowReg,
1475 lenOffset, mir, kThrowArrayBounds);
1476 }
1477 if ((size == kLong) || (size == kDouble)) {
1478 rlResult = oatEvalLoc(cUnit, rlDest, regClass, true);
1479 loadBaseIndexedDisp(cUnit, NULL, rlArray.lowReg, rlIndex.lowReg, scale, dataOffset,
1480 rlResult.lowReg, rlResult.highReg, size, INVALID_SREG);
buzbee31a4a6f2012-02-28 15:36:15 -08001481
Ian Rogersb5d09b22012-03-06 22:14:17 -08001482 storeValueWide(cUnit, rlDest, rlResult);
1483 } else {
1484 rlResult = oatEvalLoc(cUnit, rlDest, regClass, true);
1485
1486 loadBaseIndexedDisp(cUnit, NULL, rlArray.lowReg, rlIndex.lowReg, scale, dataOffset,
1487 rlResult.lowReg, INVALID_REG, size, INVALID_SREG);
1488
1489 storeValue(cUnit, rlDest, rlResult);
1490 }
1491#else
1492 int regPtr = oatAllocTemp(cUnit);
buzbee239c4e72012-03-16 08:42:29 -07001493 bool needsRangeCheck = (!(mir->optimizationFlags & MIR_IGNORE_RANGE_CHECK));
1494 int regLen = INVALID_REG;
1495 if (needsRangeCheck) {
1496 regLen = oatAllocTemp(cUnit);
buzbee31a4a6f2012-02-28 15:36:15 -08001497 /* Get len */
1498 loadWordDisp(cUnit, rlArray.lowReg, lenOffset, regLen);
buzbee31a4a6f2012-02-28 15:36:15 -08001499 }
buzbee239c4e72012-03-16 08:42:29 -07001500 /* regPtr -> array data */
1501 opRegRegImm(cUnit, kOpAdd, regPtr, rlArray.lowReg, dataOffset);
buzbee31a4a6f2012-02-28 15:36:15 -08001502 oatFreeTemp(cUnit, rlArray.lowReg);
1503 if ((size == kLong) || (size == kDouble)) {
1504 if (scale) {
1505 int rNewIndex = oatAllocTemp(cUnit);
1506 opRegRegImm(cUnit, kOpLsl, rNewIndex, rlIndex.lowReg, scale);
1507 opRegReg(cUnit, kOpAdd, regPtr, rNewIndex);
1508 oatFreeTemp(cUnit, rNewIndex);
1509 } else {
1510 opRegReg(cUnit, kOpAdd, regPtr, rlIndex.lowReg);
1511 }
1512 oatFreeTemp(cUnit, rlIndex.lowReg);
1513 rlResult = oatEvalLoc(cUnit, rlDest, regClass, true);
1514
buzbee239c4e72012-03-16 08:42:29 -07001515 if (needsRangeCheck) {
1516 // TODO: change kCondCS to a more meaningful name, is the sense of
1517 // carry-set/clear flipped?
1518 genRegRegCheck(cUnit, kCondCs, rlIndex.lowReg, regLen, mir,
1519 kThrowArrayBounds);
1520 oatFreeTemp(cUnit, regLen);
1521 }
buzbee31a4a6f2012-02-28 15:36:15 -08001522 loadPair(cUnit, regPtr, rlResult.lowReg, rlResult.highReg);
1523
1524 oatFreeTemp(cUnit, regPtr);
1525 storeValueWide(cUnit, rlDest, rlResult);
1526 } else {
1527 rlResult = oatEvalLoc(cUnit, rlDest, regClass, true);
1528
buzbee239c4e72012-03-16 08:42:29 -07001529 if (needsRangeCheck) {
1530 // TODO: change kCondCS to a more meaningful name, is the sense of
1531 // carry-set/clear flipped?
1532 genRegRegCheck(cUnit, kCondCs, rlIndex.lowReg, regLen, mir,
1533 kThrowArrayBounds);
1534 oatFreeTemp(cUnit, regLen);
1535 }
buzbee31a4a6f2012-02-28 15:36:15 -08001536 loadBaseIndexed(cUnit, regPtr, rlIndex.lowReg, rlResult.lowReg,
1537 scale, size);
1538
1539 oatFreeTemp(cUnit, regPtr);
1540 storeValue(cUnit, rlDest, rlResult);
1541 }
Ian Rogersb5d09b22012-03-06 22:14:17 -08001542#endif
buzbee31a4a6f2012-02-28 15:36:15 -08001543}
1544
1545/*
1546 * Generate array store
1547 *
1548 */
1549void genArrayPut(CompilationUnit* cUnit, MIR* mir, OpSize size,
1550 RegLocation rlArray, RegLocation rlIndex,
1551 RegLocation rlSrc, int scale)
1552{
1553 RegisterClass regClass = oatRegClassBySize(size);
1554 int lenOffset = Array::LengthOffset().Int32Value();
1555 int dataOffset;
1556
1557 if (size == kLong || size == kDouble) {
1558 dataOffset = Array::DataOffset(sizeof(int64_t)).Int32Value();
1559 } else {
1560 dataOffset = Array::DataOffset(sizeof(int32_t)).Int32Value();
1561 }
1562
buzbee31a4a6f2012-02-28 15:36:15 -08001563 rlArray = loadValue(cUnit, rlArray, kCoreReg);
1564 rlIndex = loadValue(cUnit, rlIndex, kCoreReg);
Ian Rogersb41b33b2012-03-20 14:22:54 -07001565#if !defined(TARGET_X86)
1566 int regPtr;
buzbee31a4a6f2012-02-28 15:36:15 -08001567 if (oatIsTemp(cUnit, rlArray.lowReg)) {
1568 oatClobber(cUnit, rlArray.lowReg);
1569 regPtr = rlArray.lowReg;
1570 } else {
1571 regPtr = oatAllocTemp(cUnit);
buzbee82488f52012-03-02 08:20:26 -08001572 opRegCopy(cUnit, regPtr, rlArray.lowReg);
buzbee31a4a6f2012-02-28 15:36:15 -08001573 }
Ian Rogersb41b33b2012-03-20 14:22:54 -07001574#endif
buzbee31a4a6f2012-02-28 15:36:15 -08001575
1576 /* null object? */
1577 genNullCheck(cUnit, rlArray.sRegLow, rlArray.lowReg, mir);
1578
Ian Rogersb41b33b2012-03-20 14:22:54 -07001579#if defined(TARGET_X86)
1580 if (!(mir->optimizationFlags & MIR_IGNORE_RANGE_CHECK)) {
1581 /* if (rlIndex >= [rlArray + lenOffset]) goto kThrowArrayBounds */
1582 genRegMemCheck(cUnit, kCondUge, rlIndex.lowReg, rlArray.lowReg,
1583 lenOffset, mir, kThrowArrayBounds);
1584 }
1585 rlSrc = loadValue(cUnit, rlSrc, regClass);
1586 storeBaseIndexedDisp(cUnit, NULL, rlArray.lowReg, rlIndex.lowReg, scale, dataOffset,
1587 rlSrc.lowReg, rlSrc.highReg, size, INVALID_SREG);
1588#else
buzbee239c4e72012-03-16 08:42:29 -07001589 bool needsRangeCheck = (!(mir->optimizationFlags & MIR_IGNORE_RANGE_CHECK));
1590 int regLen = INVALID_REG;
1591 if (needsRangeCheck) {
1592 regLen = oatAllocTemp(cUnit);
buzbee31a4a6f2012-02-28 15:36:15 -08001593 //NOTE: max live temps(4) here.
1594 /* Get len */
1595 loadWordDisp(cUnit, rlArray.lowReg, lenOffset, regLen);
buzbee31a4a6f2012-02-28 15:36:15 -08001596 }
buzbee239c4e72012-03-16 08:42:29 -07001597 /* regPtr -> array data */
1598 opRegImm(cUnit, kOpAdd, regPtr, dataOffset);
buzbee31a4a6f2012-02-28 15:36:15 -08001599 /* at this point, regPtr points to array, 2 live temps */
1600 if ((size == kLong) || (size == kDouble)) {
1601 //TUNING: specific wide routine that can handle fp regs
1602 if (scale) {
1603 int rNewIndex = oatAllocTemp(cUnit);
1604 opRegRegImm(cUnit, kOpLsl, rNewIndex, rlIndex.lowReg, scale);
1605 opRegReg(cUnit, kOpAdd, regPtr, rNewIndex);
1606 oatFreeTemp(cUnit, rNewIndex);
1607 } else {
1608 opRegReg(cUnit, kOpAdd, regPtr, rlIndex.lowReg);
1609 }
1610 rlSrc = loadValueWide(cUnit, rlSrc, regClass);
1611
buzbee239c4e72012-03-16 08:42:29 -07001612 if (needsRangeCheck) {
1613 genRegRegCheck(cUnit, kCondCs, rlIndex.lowReg, regLen, mir,
1614 kThrowArrayBounds);
1615 oatFreeTemp(cUnit, regLen);
1616 }
1617
buzbee31a4a6f2012-02-28 15:36:15 -08001618 storePair(cUnit, regPtr, rlSrc.lowReg, rlSrc.highReg);
1619
1620 oatFreeTemp(cUnit, regPtr);
1621 } else {
1622 rlSrc = loadValue(cUnit, rlSrc, regClass);
buzbee239c4e72012-03-16 08:42:29 -07001623 if (needsRangeCheck) {
1624 genRegRegCheck(cUnit, kCondCs, rlIndex.lowReg, regLen, mir,
1625 kThrowArrayBounds);
1626 oatFreeTemp(cUnit, regLen);
1627 }
buzbee31a4a6f2012-02-28 15:36:15 -08001628 storeBaseIndexed(cUnit, regPtr, rlIndex.lowReg, rlSrc.lowReg,
1629 scale, size);
1630 }
Ian Rogersb41b33b2012-03-20 14:22:54 -07001631#endif
buzbee31a4a6f2012-02-28 15:36:15 -08001632}
1633
1634void genLong3Addr(CompilationUnit* cUnit, MIR* mir, OpKind firstOp,
1635 OpKind secondOp, RegLocation rlDest,
1636 RegLocation rlSrc1, RegLocation rlSrc2)
1637{
1638 RegLocation rlResult;
1639#if defined(TARGET_ARM)
1640 /*
1641 * NOTE: This is the one place in the code in which we might have
1642 * as many as six live temporary registers. There are 5 in the normal
1643 * set for Arm. Until we have spill capabilities, temporarily add
1644 * lr to the temp set. It is safe to do this locally, but note that
1645 * lr is used explicitly elsewhere in the code generator and cannot
1646 * normally be used as a general temp register.
1647 */
1648 oatMarkTemp(cUnit, rLR); // Add lr to the temp pool
1649 oatFreeTemp(cUnit, rLR); // and make it available
1650#endif
1651 rlSrc1 = loadValueWide(cUnit, rlSrc1, kCoreReg);
1652 rlSrc2 = loadValueWide(cUnit, rlSrc2, kCoreReg);
1653 rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
1654 // The longs may overlap - use intermediate temp if so
1655 if (rlResult.lowReg == rlSrc1.highReg) {
1656 int tReg = oatAllocTemp(cUnit);
buzbee82488f52012-03-02 08:20:26 -08001657 opRegCopy(cUnit, tReg, rlSrc1.highReg);
buzbee31a4a6f2012-02-28 15:36:15 -08001658 opRegRegReg(cUnit, firstOp, rlResult.lowReg, rlSrc1.lowReg,
1659 rlSrc2.lowReg);
1660 opRegRegReg(cUnit, secondOp, rlResult.highReg, tReg,
1661 rlSrc2.highReg);
1662 oatFreeTemp(cUnit, tReg);
1663 } else {
1664 opRegRegReg(cUnit, firstOp, rlResult.lowReg, rlSrc1.lowReg,
1665 rlSrc2.lowReg);
1666 opRegRegReg(cUnit, secondOp, rlResult.highReg, rlSrc1.highReg,
1667 rlSrc2.highReg);
1668 }
1669 /*
1670 * NOTE: If rlDest refers to a frame variable in a large frame, the
1671 * following storeValueWide might need to allocate a temp register.
1672 * To further work around the lack of a spill capability, explicitly
1673 * free any temps from rlSrc1 & rlSrc2 that aren't still live in rlResult.
1674 * Remove when spill is functional.
1675 */
1676 freeRegLocTemps(cUnit, rlResult, rlSrc1);
1677 freeRegLocTemps(cUnit, rlResult, rlSrc2);
1678 storeValueWide(cUnit, rlDest, rlResult);
1679#if defined(TARGET_ARM)
1680 oatClobber(cUnit, rLR);
1681 oatUnmarkTemp(cUnit, rLR); // Remove lr from the temp pool
1682#endif
1683}
1684
1685
1686bool genShiftOpLong(CompilationUnit* cUnit, MIR* mir, RegLocation rlDest,
1687 RegLocation rlSrc1, RegLocation rlShift)
1688{
1689 int funcOffset;
1690
1691 switch( mir->dalvikInsn.opcode) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001692 case Instruction::SHL_LONG:
1693 case Instruction::SHL_LONG_2ADDR:
buzbee31a4a6f2012-02-28 15:36:15 -08001694 funcOffset = OFFSETOF_MEMBER(Thread, pShlLong);
1695 break;
Elliott Hughesadb8c672012-03-06 16:49:32 -08001696 case Instruction::SHR_LONG:
1697 case Instruction::SHR_LONG_2ADDR:
buzbee31a4a6f2012-02-28 15:36:15 -08001698 funcOffset = OFFSETOF_MEMBER(Thread, pShrLong);
1699 break;
Elliott Hughesadb8c672012-03-06 16:49:32 -08001700 case Instruction::USHR_LONG:
1701 case Instruction::USHR_LONG_2ADDR:
buzbee31a4a6f2012-02-28 15:36:15 -08001702 funcOffset = OFFSETOF_MEMBER(Thread, pUshrLong);
1703 break;
1704 default:
1705 LOG(FATAL) << "Unexpected case";
1706 return true;
1707 }
1708 oatFlushAllRegs(cUnit); /* Send everything to home location */
Ian Rogersab2b55d2012-03-18 00:06:11 -07001709 callRuntimeHelperRegLocationRegLocation(cUnit, funcOffset, rlSrc1, rlShift);
Ian Rogersf7d9ad32012-03-13 18:45:39 -07001710 RegLocation rlResult = oatGetReturnWide(cUnit, false);
buzbee31a4a6f2012-02-28 15:36:15 -08001711 storeValueWide(cUnit, rlDest, rlResult);
1712 return false;
1713}
1714
1715
1716bool genArithOpInt(CompilationUnit* cUnit, MIR* mir, RegLocation rlDest,
1717 RegLocation rlSrc1, RegLocation rlSrc2)
1718{
1719 OpKind op = kOpBkpt;
1720 bool callOut = false;
1721 bool checkZero = false;
1722 bool unary = false;
buzbee31a4a6f2012-02-28 15:36:15 -08001723 RegLocation rlResult;
1724 bool shiftOp = false;
Ian Rogersb5d09b22012-03-06 22:14:17 -08001725 int funcOffset;
1726 int retReg = rRET0;
buzbee31a4a6f2012-02-28 15:36:15 -08001727 switch (mir->dalvikInsn.opcode) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001728 case Instruction::NEG_INT:
buzbee31a4a6f2012-02-28 15:36:15 -08001729 op = kOpNeg;
1730 unary = true;
1731 break;
Elliott Hughesadb8c672012-03-06 16:49:32 -08001732 case Instruction::NOT_INT:
buzbee31a4a6f2012-02-28 15:36:15 -08001733 op = kOpMvn;
1734 unary = true;
1735 break;
Elliott Hughesadb8c672012-03-06 16:49:32 -08001736 case Instruction::ADD_INT:
1737 case Instruction::ADD_INT_2ADDR:
buzbee31a4a6f2012-02-28 15:36:15 -08001738 op = kOpAdd;
1739 break;
Elliott Hughesadb8c672012-03-06 16:49:32 -08001740 case Instruction::SUB_INT:
1741 case Instruction::SUB_INT_2ADDR:
buzbee31a4a6f2012-02-28 15:36:15 -08001742 op = kOpSub;
1743 break;
Elliott Hughesadb8c672012-03-06 16:49:32 -08001744 case Instruction::MUL_INT:
1745 case Instruction::MUL_INT_2ADDR:
buzbee31a4a6f2012-02-28 15:36:15 -08001746 op = kOpMul;
1747 break;
Elliott Hughesadb8c672012-03-06 16:49:32 -08001748 case Instruction::DIV_INT:
1749 case Instruction::DIV_INT_2ADDR:
buzbee31a4a6f2012-02-28 15:36:15 -08001750 checkZero = true;
Ian Rogersb5d09b22012-03-06 22:14:17 -08001751 op = kOpDiv;
1752 callOut = true;
buzbee31a4a6f2012-02-28 15:36:15 -08001753 funcOffset = OFFSETOF_MEMBER(Thread, pIdiv);
1754 retReg = rRET0;
1755 break;
buzbee5de34942012-03-01 14:51:57 -08001756 /* NOTE: returns in rARG1 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08001757 case Instruction::REM_INT:
1758 case Instruction::REM_INT_2ADDR:
buzbee31a4a6f2012-02-28 15:36:15 -08001759 checkZero = true;
Ian Rogersb5d09b22012-03-06 22:14:17 -08001760 op = kOpRem;
1761 callOut = true;
buzbee31a4a6f2012-02-28 15:36:15 -08001762 funcOffset = OFFSETOF_MEMBER(Thread, pIdivmod);
1763 retReg = rRET1;
1764 break;
Elliott Hughesadb8c672012-03-06 16:49:32 -08001765 case Instruction::AND_INT:
1766 case Instruction::AND_INT_2ADDR:
buzbee31a4a6f2012-02-28 15:36:15 -08001767 op = kOpAnd;
1768 break;
Elliott Hughesadb8c672012-03-06 16:49:32 -08001769 case Instruction::OR_INT:
1770 case Instruction::OR_INT_2ADDR:
buzbee31a4a6f2012-02-28 15:36:15 -08001771 op = kOpOr;
1772 break;
Elliott Hughesadb8c672012-03-06 16:49:32 -08001773 case Instruction::XOR_INT:
1774 case Instruction::XOR_INT_2ADDR:
buzbee31a4a6f2012-02-28 15:36:15 -08001775 op = kOpXor;
1776 break;
Elliott Hughesadb8c672012-03-06 16:49:32 -08001777 case Instruction::SHL_INT:
1778 case Instruction::SHL_INT_2ADDR:
buzbee31a4a6f2012-02-28 15:36:15 -08001779 shiftOp = true;
1780 op = kOpLsl;
1781 break;
Elliott Hughesadb8c672012-03-06 16:49:32 -08001782 case Instruction::SHR_INT:
1783 case Instruction::SHR_INT_2ADDR:
buzbee31a4a6f2012-02-28 15:36:15 -08001784 shiftOp = true;
1785 op = kOpAsr;
1786 break;
Elliott Hughesadb8c672012-03-06 16:49:32 -08001787 case Instruction::USHR_INT:
1788 case Instruction::USHR_INT_2ADDR:
buzbee31a4a6f2012-02-28 15:36:15 -08001789 shiftOp = true;
1790 op = kOpLsr;
1791 break;
1792 default:
1793 LOG(FATAL) << "Invalid word arith op: " <<
1794 (int)mir->dalvikInsn.opcode;
1795 }
1796 if (!callOut) {
1797 rlSrc1 = loadValue(cUnit, rlSrc1, kCoreReg);
1798 if (unary) {
1799 rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
1800 opRegReg(cUnit, op, rlResult.lowReg,
1801 rlSrc1.lowReg);
1802 } else {
1803 rlSrc2 = loadValue(cUnit, rlSrc2, kCoreReg);
Ian Rogersb5d09b22012-03-06 22:14:17 -08001804#if defined(TARGET_X86)
1805 rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
1806 opRegRegReg(cUnit, op, rlResult.lowReg,
1807 rlSrc1.lowReg, rlSrc2.lowReg);
1808#else
buzbee31a4a6f2012-02-28 15:36:15 -08001809 if (shiftOp) {
1810 int tReg = oatAllocTemp(cUnit);
1811 opRegRegImm(cUnit, kOpAnd, tReg, rlSrc2.lowReg, 31);
1812 rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
1813 opRegRegReg(cUnit, op, rlResult.lowReg,
1814 rlSrc1.lowReg, tReg);
1815 oatFreeTemp(cUnit, tReg);
1816 } else {
1817 rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
1818 opRegRegReg(cUnit, op, rlResult.lowReg,
1819 rlSrc1.lowReg, rlSrc2.lowReg);
1820 }
Ian Rogersb5d09b22012-03-06 22:14:17 -08001821#endif
buzbee31a4a6f2012-02-28 15:36:15 -08001822 }
1823 storeValue(cUnit, rlDest, rlResult);
1824 } else {
1825 RegLocation rlResult;
1826 oatFlushAllRegs(cUnit); /* Send everything to home location */
1827 loadValueDirectFixed(cUnit, rlSrc2, rRET1);
Ian Rogers6cbb2bd2012-03-16 13:45:30 -07001828#if !defined(TARGET_X86)
buzbee31a4a6f2012-02-28 15:36:15 -08001829 int rTgt = loadHelper(cUnit, funcOffset);
Ian Rogersab2b55d2012-03-18 00:06:11 -07001830#endif
buzbee31a4a6f2012-02-28 15:36:15 -08001831 loadValueDirectFixed(cUnit, rlSrc1, rARG0);
1832 if (checkZero) {
1833 genImmedCheck(cUnit, kCondEq, rARG1, 0, mir, kThrowDivZero);
1834 }
Ian Rogersab2b55d2012-03-18 00:06:11 -07001835#if !defined(TARGET_X86)
1836 opReg(cUnit, kOpBlx, rTgt);
Ian Rogers6cbb2bd2012-03-16 13:45:30 -07001837 oatFreeTemp(cUnit, rTgt);
1838#else
Ian Rogersab2b55d2012-03-18 00:06:11 -07001839 opThreadMem(cUnit, kOpBlx, funcOffset);
Ian Rogers6cbb2bd2012-03-16 13:45:30 -07001840#endif
buzbee31a4a6f2012-02-28 15:36:15 -08001841 if (retReg == rRET0)
Ian Rogersf7d9ad32012-03-13 18:45:39 -07001842 rlResult = oatGetReturn(cUnit, false);
buzbee31a4a6f2012-02-28 15:36:15 -08001843 else
1844 rlResult = oatGetReturnAlt(cUnit);
1845 storeValue(cUnit, rlDest, rlResult);
1846 }
1847 return false;
1848}
1849
1850/*
1851 * The following are the first-level codegen routines that analyze the format
1852 * of each bytecode then either dispatch special purpose codegen routines
1853 * or produce corresponding Thumb instructions directly.
1854 */
1855
1856bool isPowerOfTwo(int x)
1857{
1858 return (x & (x - 1)) == 0;
1859}
1860
1861// Returns true if no more than two bits are set in 'x'.
1862bool isPopCountLE2(unsigned int x)
1863{
1864 x &= x - 1;
1865 return (x & (x - 1)) == 0;
1866}
1867
1868// Returns the index of the lowest set bit in 'x'.
1869int lowestSetBit(unsigned int x) {
1870 int bit_posn = 0;
1871 while ((x & 0xf) == 0) {
1872 bit_posn += 4;
1873 x >>= 4;
1874 }
1875 while ((x & 1) == 0) {
1876 bit_posn++;
1877 x >>= 1;
1878 }
1879 return bit_posn;
1880}
1881
1882// Returns true if it added instructions to 'cUnit' to divide 'rlSrc' by 'lit'
1883// and store the result in 'rlDest'.
Elliott Hughesadb8c672012-03-06 16:49:32 -08001884bool handleEasyDivide(CompilationUnit* cUnit, Instruction::Code dalvikOpcode,
buzbee31a4a6f2012-02-28 15:36:15 -08001885 RegLocation rlSrc, RegLocation rlDest, int lit)
1886{
1887 if (lit < 2 || !isPowerOfTwo(lit)) {
1888 return false;
1889 }
1890 int k = lowestSetBit(lit);
1891 if (k >= 30) {
1892 // Avoid special cases.
1893 return false;
1894 }
Elliott Hughesadb8c672012-03-06 16:49:32 -08001895 bool div = (dalvikOpcode == Instruction::DIV_INT_LIT8 ||
1896 dalvikOpcode == Instruction::DIV_INT_LIT16);
buzbee31a4a6f2012-02-28 15:36:15 -08001897 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
1898 RegLocation rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
1899 if (div) {
1900 int tReg = oatAllocTemp(cUnit);
1901 if (lit == 2) {
1902 // Division by 2 is by far the most common division by constant.
1903 opRegRegImm(cUnit, kOpLsr, tReg, rlSrc.lowReg, 32 - k);
1904 opRegRegReg(cUnit, kOpAdd, tReg, tReg, rlSrc.lowReg);
1905 opRegRegImm(cUnit, kOpAsr, rlResult.lowReg, tReg, k);
1906 } else {
1907 opRegRegImm(cUnit, kOpAsr, tReg, rlSrc.lowReg, 31);
1908 opRegRegImm(cUnit, kOpLsr, tReg, tReg, 32 - k);
1909 opRegRegReg(cUnit, kOpAdd, tReg, tReg, rlSrc.lowReg);
1910 opRegRegImm(cUnit, kOpAsr, rlResult.lowReg, tReg, k);
1911 }
1912 } else {
buzbee31a4a6f2012-02-28 15:36:15 -08001913 int tReg1 = oatAllocTemp(cUnit);
1914 int tReg2 = oatAllocTemp(cUnit);
1915 if (lit == 2) {
1916 opRegRegImm(cUnit, kOpLsr, tReg1, rlSrc.lowReg, 32 - k);
1917 opRegRegReg(cUnit, kOpAdd, tReg2, tReg1, rlSrc.lowReg);
Ian Rogersb41b33b2012-03-20 14:22:54 -07001918 opRegRegImm(cUnit, kOpAnd, tReg2, tReg2, lit -1);
buzbee31a4a6f2012-02-28 15:36:15 -08001919 opRegRegReg(cUnit, kOpSub, rlResult.lowReg, tReg2, tReg1);
1920 } else {
1921 opRegRegImm(cUnit, kOpAsr, tReg1, rlSrc.lowReg, 31);
1922 opRegRegImm(cUnit, kOpLsr, tReg1, tReg1, 32 - k);
1923 opRegRegReg(cUnit, kOpAdd, tReg2, tReg1, rlSrc.lowReg);
Ian Rogersb41b33b2012-03-20 14:22:54 -07001924 opRegRegImm(cUnit, kOpAnd, tReg2, tReg2, lit - 1);
buzbee31a4a6f2012-02-28 15:36:15 -08001925 opRegRegReg(cUnit, kOpSub, rlResult.lowReg, tReg2, tReg1);
1926 }
1927 }
1928 storeValue(cUnit, rlDest, rlResult);
1929 return true;
1930}
1931
1932void genMultiplyByTwoBitMultiplier(CompilationUnit* cUnit, RegLocation rlSrc,
1933 RegLocation rlResult, int lit,
1934 int firstBit, int secondBit)
1935{
buzbee0398c422012-03-02 15:22:47 -08001936#if defined(TARGET_ARM)
buzbee31a4a6f2012-02-28 15:36:15 -08001937 opRegRegRegShift(cUnit, kOpAdd, rlResult.lowReg, rlSrc.lowReg, rlSrc.lowReg,
1938 encodeShift(kArmLsl, secondBit - firstBit));
buzbee0398c422012-03-02 15:22:47 -08001939#else
1940 int tReg = oatAllocTemp(cUnit);
1941 opRegRegImm(cUnit, kOpLsl, tReg, rlSrc.lowReg, secondBit - firstBit);
1942 opRegRegReg(cUnit, kOpAdd, rlResult.lowReg, rlSrc.lowReg, tReg);
1943 oatFreeTemp(cUnit, tReg);
buzbee5de34942012-03-01 14:51:57 -08001944#endif
buzbee31a4a6f2012-02-28 15:36:15 -08001945 if (firstBit != 0) {
1946 opRegRegImm(cUnit, kOpLsl, rlResult.lowReg, rlResult.lowReg, firstBit);
1947 }
1948}
1949
1950// Returns true if it added instructions to 'cUnit' to multiply 'rlSrc' by 'lit'
1951// and store the result in 'rlDest'.
1952bool handleEasyMultiply(CompilationUnit* cUnit, RegLocation rlSrc,
1953 RegLocation rlDest, int lit)
1954{
1955 // Can we simplify this multiplication?
1956 bool powerOfTwo = false;
1957 bool popCountLE2 = false;
1958 bool powerOfTwoMinusOne = false;
1959 if (lit < 2) {
1960 // Avoid special cases.
1961 return false;
1962 } else if (isPowerOfTwo(lit)) {
1963 powerOfTwo = true;
1964 } else if (isPopCountLE2(lit)) {
1965 popCountLE2 = true;
1966 } else if (isPowerOfTwo(lit + 1)) {
1967 powerOfTwoMinusOne = true;
1968 } else {
1969 return false;
1970 }
1971 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
1972 RegLocation rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
1973 if (powerOfTwo) {
1974 // Shift.
1975 opRegRegImm(cUnit, kOpLsl, rlResult.lowReg, rlSrc.lowReg,
1976 lowestSetBit(lit));
1977 } else if (popCountLE2) {
1978 // Shift and add and shift.
1979 int firstBit = lowestSetBit(lit);
1980 int secondBit = lowestSetBit(lit ^ (1 << firstBit));
1981 genMultiplyByTwoBitMultiplier(cUnit, rlSrc, rlResult, lit,
1982 firstBit, secondBit);
1983 } else {
1984 // Reverse subtract: (src << (shift + 1)) - src.
1985 DCHECK(powerOfTwoMinusOne);
1986 // TUNING: rsb dst, src, src lsl#lowestSetBit(lit + 1)
1987 int tReg = oatAllocTemp(cUnit);
1988 opRegRegImm(cUnit, kOpLsl, tReg, rlSrc.lowReg, lowestSetBit(lit + 1));
1989 opRegRegReg(cUnit, kOpSub, rlResult.lowReg, tReg, rlSrc.lowReg);
1990 }
1991 storeValue(cUnit, rlDest, rlResult);
1992 return true;
1993}
1994
1995bool genArithOpIntLit(CompilationUnit* cUnit, MIR* mir, RegLocation rlDest,
1996 RegLocation rlSrc, int lit)
1997{
Elliott Hughesadb8c672012-03-06 16:49:32 -08001998 Instruction::Code dalvikOpcode = mir->dalvikInsn.opcode;
buzbee31a4a6f2012-02-28 15:36:15 -08001999 RegLocation rlResult;
2000 OpKind op = (OpKind)0; /* Make gcc happy */
2001 int shiftOp = false;
2002 bool isDiv = false;
2003 int funcOffset;
buzbee31a4a6f2012-02-28 15:36:15 -08002004
2005 switch (dalvikOpcode) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002006 case Instruction::RSUB_INT_LIT8:
2007 case Instruction::RSUB_INT: {
buzbee31a4a6f2012-02-28 15:36:15 -08002008 int tReg;
2009 //TUNING: add support for use of Arm rsub op
2010 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
2011 tReg = oatAllocTemp(cUnit);
2012 loadConstant(cUnit, tReg, lit);
2013 rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
2014 opRegRegReg(cUnit, kOpSub, rlResult.lowReg,
2015 tReg, rlSrc.lowReg);
2016 storeValue(cUnit, rlDest, rlResult);
2017 return false;
2018 break;
2019 }
2020
Elliott Hughesadb8c672012-03-06 16:49:32 -08002021 case Instruction::ADD_INT_LIT8:
2022 case Instruction::ADD_INT_LIT16:
buzbee31a4a6f2012-02-28 15:36:15 -08002023 op = kOpAdd;
2024 break;
Elliott Hughesadb8c672012-03-06 16:49:32 -08002025 case Instruction::MUL_INT_LIT8:
2026 case Instruction::MUL_INT_LIT16: {
buzbee31a4a6f2012-02-28 15:36:15 -08002027 if (handleEasyMultiply(cUnit, rlSrc, rlDest, lit)) {
2028 return false;
2029 }
2030 op = kOpMul;
2031 break;
2032 }
Elliott Hughesadb8c672012-03-06 16:49:32 -08002033 case Instruction::AND_INT_LIT8:
2034 case Instruction::AND_INT_LIT16:
buzbee31a4a6f2012-02-28 15:36:15 -08002035 op = kOpAnd;
2036 break;
Elliott Hughesadb8c672012-03-06 16:49:32 -08002037 case Instruction::OR_INT_LIT8:
2038 case Instruction::OR_INT_LIT16:
buzbee31a4a6f2012-02-28 15:36:15 -08002039 op = kOpOr;
2040 break;
Elliott Hughesadb8c672012-03-06 16:49:32 -08002041 case Instruction::XOR_INT_LIT8:
2042 case Instruction::XOR_INT_LIT16:
buzbee31a4a6f2012-02-28 15:36:15 -08002043 op = kOpXor;
2044 break;
Elliott Hughesadb8c672012-03-06 16:49:32 -08002045 case Instruction::SHL_INT_LIT8:
buzbee31a4a6f2012-02-28 15:36:15 -08002046 lit &= 31;
2047 shiftOp = true;
2048 op = kOpLsl;
2049 break;
Elliott Hughesadb8c672012-03-06 16:49:32 -08002050 case Instruction::SHR_INT_LIT8:
buzbee31a4a6f2012-02-28 15:36:15 -08002051 lit &= 31;
2052 shiftOp = true;
2053 op = kOpAsr;
2054 break;
Elliott Hughesadb8c672012-03-06 16:49:32 -08002055 case Instruction::USHR_INT_LIT8:
buzbee31a4a6f2012-02-28 15:36:15 -08002056 lit &= 31;
2057 shiftOp = true;
2058 op = kOpLsr;
2059 break;
2060
Elliott Hughesadb8c672012-03-06 16:49:32 -08002061 case Instruction::DIV_INT_LIT8:
2062 case Instruction::DIV_INT_LIT16:
2063 case Instruction::REM_INT_LIT8:
2064 case Instruction::REM_INT_LIT16:
buzbee31a4a6f2012-02-28 15:36:15 -08002065 if (lit == 0) {
2066 genImmedCheck(cUnit, kCondAl, 0, 0, mir, kThrowDivZero);
2067 return false;
2068 }
2069 if (handleEasyDivide(cUnit, dalvikOpcode, rlSrc, rlDest, lit)) {
2070 return false;
2071 }
2072 oatFlushAllRegs(cUnit); /* Everything to home location */
2073 loadValueDirectFixed(cUnit, rlSrc, rARG0);
2074 oatClobber(cUnit, rARG0);
Elliott Hughesadb8c672012-03-06 16:49:32 -08002075 if ((dalvikOpcode == Instruction::DIV_INT_LIT8) ||
2076 (dalvikOpcode == Instruction::DIV_INT_LIT16)) {
buzbee31a4a6f2012-02-28 15:36:15 -08002077 funcOffset = OFFSETOF_MEMBER(Thread, pIdiv);
2078 isDiv = true;
2079 } else {
2080 funcOffset = OFFSETOF_MEMBER(Thread, pIdivmod);
2081 isDiv = false;
2082 }
Ian Rogersab2b55d2012-03-18 00:06:11 -07002083 callRuntimeHelperRegImm(cUnit, funcOffset, rARG0, lit);
buzbee31a4a6f2012-02-28 15:36:15 -08002084 if (isDiv)
Ian Rogersf7d9ad32012-03-13 18:45:39 -07002085 rlResult = oatGetReturn(cUnit, false);
buzbee31a4a6f2012-02-28 15:36:15 -08002086 else
2087 rlResult = oatGetReturnAlt(cUnit);
2088 storeValue(cUnit, rlDest, rlResult);
2089 return false;
2090 break;
2091 default:
2092 return true;
2093 }
2094 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
2095 rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
2096 // Avoid shifts by literal 0 - no support in Thumb. Change to copy
2097 if (shiftOp && (lit == 0)) {
buzbee82488f52012-03-02 08:20:26 -08002098 opRegCopy(cUnit, rlResult.lowReg, rlSrc.lowReg);
buzbee31a4a6f2012-02-28 15:36:15 -08002099 } else {
2100 opRegRegImm(cUnit, op, rlResult.lowReg, rlSrc.lowReg, lit);
2101 }
2102 storeValue(cUnit, rlDest, rlResult);
2103 return false;
2104}
2105
2106bool genArithOpLong(CompilationUnit* cUnit, MIR* mir, RegLocation rlDest,
2107 RegLocation rlSrc1, RegLocation rlSrc2)
2108{
2109 RegLocation rlResult;
2110 OpKind firstOp = kOpBkpt;
2111 OpKind secondOp = kOpBkpt;
2112 bool callOut = false;
2113 bool checkZero = false;
2114 int funcOffset;
2115 int retReg = rRET0;
2116
2117 switch (mir->dalvikInsn.opcode) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002118 case Instruction::NOT_LONG:
buzbee31a4a6f2012-02-28 15:36:15 -08002119 rlSrc2 = loadValueWide(cUnit, rlSrc2, kCoreReg);
2120 rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
2121 // Check for destructive overlap
2122 if (rlResult.lowReg == rlSrc2.highReg) {
2123 int tReg = oatAllocTemp(cUnit);
buzbee82488f52012-03-02 08:20:26 -08002124 opRegCopy(cUnit, tReg, rlSrc2.highReg);
buzbee31a4a6f2012-02-28 15:36:15 -08002125 opRegReg(cUnit, kOpMvn, rlResult.lowReg, rlSrc2.lowReg);
2126 opRegReg(cUnit, kOpMvn, rlResult.highReg, tReg);
2127 oatFreeTemp(cUnit, tReg);
2128 } else {
2129 opRegReg(cUnit, kOpMvn, rlResult.lowReg, rlSrc2.lowReg);
2130 opRegReg(cUnit, kOpMvn, rlResult.highReg, rlSrc2.highReg);
2131 }
2132 storeValueWide(cUnit, rlDest, rlResult);
2133 return false;
2134 break;
Elliott Hughesadb8c672012-03-06 16:49:32 -08002135 case Instruction::ADD_LONG:
2136 case Instruction::ADD_LONG_2ADDR:
buzbeec5159d52012-03-03 11:48:39 -08002137#if defined(TARGET_MIPS)
2138 return genAddLong(cUnit, mir, rlDest, rlSrc1, rlSrc2);
2139#else
buzbee31a4a6f2012-02-28 15:36:15 -08002140 firstOp = kOpAdd;
2141 secondOp = kOpAdc;
2142 break;
buzbeec5159d52012-03-03 11:48:39 -08002143#endif
Elliott Hughesadb8c672012-03-06 16:49:32 -08002144 case Instruction::SUB_LONG:
2145 case Instruction::SUB_LONG_2ADDR:
buzbeec5159d52012-03-03 11:48:39 -08002146#if defined(TARGET_MIPS)
2147 return genSubLong(cUnit, mir, rlDest, rlSrc1, rlSrc2);
2148#else
buzbee31a4a6f2012-02-28 15:36:15 -08002149 firstOp = kOpSub;
2150 secondOp = kOpSbc;
2151 break;
buzbeec5159d52012-03-03 11:48:39 -08002152#endif
Elliott Hughesadb8c672012-03-06 16:49:32 -08002153 case Instruction::MUL_LONG:
2154 case Instruction::MUL_LONG_2ADDR:
buzbee31a4a6f2012-02-28 15:36:15 -08002155 callOut = true;
2156 retReg = rRET0;
2157 funcOffset = OFFSETOF_MEMBER(Thread, pLmul);
2158 break;
Elliott Hughesadb8c672012-03-06 16:49:32 -08002159 case Instruction::DIV_LONG:
2160 case Instruction::DIV_LONG_2ADDR:
buzbee31a4a6f2012-02-28 15:36:15 -08002161 callOut = true;
2162 checkZero = true;
2163 retReg = rRET0;
2164 funcOffset = OFFSETOF_MEMBER(Thread, pLdivmod);
2165 break;
2166 /* NOTE - result is in rARG2/rARG3 instead of rRET0/rRET1 */
2167 // FIXME: is true, or could be made true, or other targets?
Elliott Hughesadb8c672012-03-06 16:49:32 -08002168 case Instruction::REM_LONG:
2169 case Instruction::REM_LONG_2ADDR:
buzbee31a4a6f2012-02-28 15:36:15 -08002170 callOut = true;
2171 checkZero = true;
2172 funcOffset = OFFSETOF_MEMBER(Thread, pLdivmod);
2173 retReg = rARG2;
2174 break;
Elliott Hughesadb8c672012-03-06 16:49:32 -08002175 case Instruction::AND_LONG_2ADDR:
2176 case Instruction::AND_LONG:
buzbee31a4a6f2012-02-28 15:36:15 -08002177 firstOp = kOpAnd;
2178 secondOp = kOpAnd;
2179 break;
Elliott Hughesadb8c672012-03-06 16:49:32 -08002180 case Instruction::OR_LONG:
2181 case Instruction::OR_LONG_2ADDR:
buzbee31a4a6f2012-02-28 15:36:15 -08002182 firstOp = kOpOr;
2183 secondOp = kOpOr;
2184 break;
Elliott Hughesadb8c672012-03-06 16:49:32 -08002185 case Instruction::XOR_LONG:
2186 case Instruction::XOR_LONG_2ADDR:
buzbee31a4a6f2012-02-28 15:36:15 -08002187 firstOp = kOpXor;
2188 secondOp = kOpXor;
2189 break;
Elliott Hughesadb8c672012-03-06 16:49:32 -08002190 case Instruction::NEG_LONG: {
buzbeec5159d52012-03-03 11:48:39 -08002191 return genNegLong(cUnit, mir, rlDest, rlSrc2);
buzbee31a4a6f2012-02-28 15:36:15 -08002192 }
2193 default:
2194 LOG(FATAL) << "Invalid long arith op";
2195 }
2196 if (!callOut) {
2197 genLong3Addr(cUnit, mir, firstOp, secondOp, rlDest, rlSrc1, rlSrc2);
2198 } else {
buzbee31a4a6f2012-02-28 15:36:15 -08002199 oatFlushAllRegs(cUnit); /* Send everything to home location */
2200 if (checkZero) {
Ian Rogersb5d09b22012-03-06 22:14:17 -08002201#if defined(TARGET_X86)
2202 UNIMPLEMENTED(FATAL);
2203#else
buzbee31a4a6f2012-02-28 15:36:15 -08002204 loadValueDirectWideFixed(cUnit, rlSrc2, rARG2, rARG3);
Ian Rogersab2b55d2012-03-18 00:06:11 -07002205 int rTgt = loadHelper(cUnit, funcOffset);
Ian Rogers6cbb2bd2012-03-16 13:45:30 -07002206#endif
buzbee31a4a6f2012-02-28 15:36:15 -08002207 loadValueDirectWideFixed(cUnit, rlSrc1, rARG0, rARG1);
2208 int tReg = oatAllocTemp(cUnit);
2209#if defined(TARGET_ARM)
2210 newLIR4(cUnit, kThumb2OrrRRRs, tReg, rARG2, rARG3, 0);
2211 oatFreeTemp(cUnit, tReg);
2212 genCheck(cUnit, kCondEq, mir, kThrowDivZero);
2213#else
Ian Rogersb5d09b22012-03-06 22:14:17 -08002214#if defined(TARGET_X86)
2215 UNIMPLEMENTED(FATAL);
2216#else
buzbee31a4a6f2012-02-28 15:36:15 -08002217 opRegRegReg(cUnit, kOpOr, tReg, rARG2, rARG3);
Ian Rogersb5d09b22012-03-06 22:14:17 -08002218#endif
buzbee5de34942012-03-01 14:51:57 -08002219 genImmedCheck(cUnit, kCondEq, tReg, 0, mir, kThrowDivZero);
buzbee31a4a6f2012-02-28 15:36:15 -08002220 oatFreeTemp(cUnit, tReg);
2221#endif
Ian Rogersab2b55d2012-03-18 00:06:11 -07002222#if !defined(TARGET_X86)
2223 opReg(cUnit, kOpBlx, rTgt);
2224 oatFreeTemp(cUnit, rTgt);
Ian Rogersb5d09b22012-03-06 22:14:17 -08002225#endif
Ian Rogersab2b55d2012-03-18 00:06:11 -07002226 } else {
2227 callRuntimeHelperRegLocationRegLocation(cUnit, funcOffset,
2228 rlSrc1, rlSrc2);
buzbee31a4a6f2012-02-28 15:36:15 -08002229 }
buzbee31a4a6f2012-02-28 15:36:15 -08002230 // Adjust return regs in to handle case of rem returning rARG2/rARG3
2231 if (retReg == rRET0)
Ian Rogersf7d9ad32012-03-13 18:45:39 -07002232 rlResult = oatGetReturnWide(cUnit, false);
buzbee31a4a6f2012-02-28 15:36:15 -08002233 else
2234 rlResult = oatGetReturnWideAlt(cUnit);
2235 storeValueWide(cUnit, rlDest, rlResult);
2236 }
2237 return false;
2238}
2239
2240bool genConversionCall(CompilationUnit* cUnit, MIR* mir, int funcOffset,
2241 int srcSize, int tgtSize)
2242{
2243 /*
2244 * Don't optimize the register usage since it calls out to support
2245 * functions
2246 */
2247 RegLocation rlSrc;
2248 RegLocation rlDest;
2249 oatFlushAllRegs(cUnit); /* Send everything to home location */
buzbee31a4a6f2012-02-28 15:36:15 -08002250 if (srcSize == 1) {
2251 rlSrc = oatGetSrc(cUnit, mir, 0);
2252 loadValueDirectFixed(cUnit, rlSrc, rARG0);
2253 } else {
2254 rlSrc = oatGetSrcWide(cUnit, mir, 0, 1);
2255 loadValueDirectWideFixed(cUnit, rlSrc, rARG0, rARG1);
2256 }
Ian Rogersab2b55d2012-03-18 00:06:11 -07002257 callRuntimeHelperRegLocation(cUnit, funcOffset, rlSrc);
buzbee31a4a6f2012-02-28 15:36:15 -08002258 if (tgtSize == 1) {
2259 RegLocation rlResult;
2260 rlDest = oatGetDest(cUnit, mir, 0);
Ian Rogersf7d9ad32012-03-13 18:45:39 -07002261 rlResult = oatGetReturn(cUnit, rlDest.fp);
buzbee31a4a6f2012-02-28 15:36:15 -08002262 storeValue(cUnit, rlDest, rlResult);
2263 } else {
2264 RegLocation rlResult;
2265 rlDest = oatGetDestWide(cUnit, mir, 0, 1);
Ian Rogersf7d9ad32012-03-13 18:45:39 -07002266 rlResult = oatGetReturnWide(cUnit, rlDest.fp);
buzbee31a4a6f2012-02-28 15:36:15 -08002267 storeValueWide(cUnit, rlDest, rlResult);
2268 }
2269 return false;
2270}
2271
2272void genNegFloat(CompilationUnit* cUnit, RegLocation rlDest, RegLocation rlSrc);
2273bool genArithOpFloatPortable(CompilationUnit* cUnit, MIR* mir,
2274 RegLocation rlDest, RegLocation rlSrc1,
2275 RegLocation rlSrc2)
2276{
2277 RegLocation rlResult;
2278 int funcOffset;
2279
2280 switch (mir->dalvikInsn.opcode) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002281 case Instruction::ADD_FLOAT_2ADDR:
2282 case Instruction::ADD_FLOAT:
buzbee31a4a6f2012-02-28 15:36:15 -08002283 funcOffset = OFFSETOF_MEMBER(Thread, pFadd);
2284 break;
Elliott Hughesadb8c672012-03-06 16:49:32 -08002285 case Instruction::SUB_FLOAT_2ADDR:
2286 case Instruction::SUB_FLOAT:
buzbee31a4a6f2012-02-28 15:36:15 -08002287 funcOffset = OFFSETOF_MEMBER(Thread, pFsub);
2288 break;
Elliott Hughesadb8c672012-03-06 16:49:32 -08002289 case Instruction::DIV_FLOAT_2ADDR:
2290 case Instruction::DIV_FLOAT:
buzbee31a4a6f2012-02-28 15:36:15 -08002291 funcOffset = OFFSETOF_MEMBER(Thread, pFdiv);
2292 break;
Elliott Hughesadb8c672012-03-06 16:49:32 -08002293 case Instruction::MUL_FLOAT_2ADDR:
2294 case Instruction::MUL_FLOAT:
buzbee31a4a6f2012-02-28 15:36:15 -08002295 funcOffset = OFFSETOF_MEMBER(Thread, pFmul);
2296 break;
Elliott Hughesadb8c672012-03-06 16:49:32 -08002297 case Instruction::REM_FLOAT_2ADDR:
2298 case Instruction::REM_FLOAT:
buzbee31a4a6f2012-02-28 15:36:15 -08002299 funcOffset = OFFSETOF_MEMBER(Thread, pFmodf);
2300 break;
Elliott Hughesadb8c672012-03-06 16:49:32 -08002301 case Instruction::NEG_FLOAT: {
buzbee31a4a6f2012-02-28 15:36:15 -08002302 genNegFloat(cUnit, rlDest, rlSrc1);
2303 return false;
2304 }
2305 default:
2306 return true;
2307 }
2308 oatFlushAllRegs(cUnit); /* Send everything to home location */
Ian Rogersab2b55d2012-03-18 00:06:11 -07002309 callRuntimeHelperRegLocationRegLocation(cUnit, funcOffset, rlSrc1, rlSrc2);
Ian Rogersf7d9ad32012-03-13 18:45:39 -07002310 rlResult = oatGetReturn(cUnit, true);
buzbee31a4a6f2012-02-28 15:36:15 -08002311 storeValue(cUnit, rlDest, rlResult);
2312 return false;
2313}
2314
2315void genNegDouble(CompilationUnit* cUnit, RegLocation rlDst, RegLocation rlSrc);
2316bool genArithOpDoublePortable(CompilationUnit* cUnit, MIR* mir,
2317 RegLocation rlDest, RegLocation rlSrc1,
2318 RegLocation rlSrc2)
2319{
2320 RegLocation rlResult;
2321 int funcOffset;
2322
2323 switch (mir->dalvikInsn.opcode) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002324 case Instruction::ADD_DOUBLE_2ADDR:
2325 case Instruction::ADD_DOUBLE:
buzbee31a4a6f2012-02-28 15:36:15 -08002326 funcOffset = OFFSETOF_MEMBER(Thread, pDadd);
2327 break;
Elliott Hughesadb8c672012-03-06 16:49:32 -08002328 case Instruction::SUB_DOUBLE_2ADDR:
2329 case Instruction::SUB_DOUBLE:
buzbee31a4a6f2012-02-28 15:36:15 -08002330 funcOffset = OFFSETOF_MEMBER(Thread, pDsub);
2331 break;
Elliott Hughesadb8c672012-03-06 16:49:32 -08002332 case Instruction::DIV_DOUBLE_2ADDR:
2333 case Instruction::DIV_DOUBLE:
buzbee31a4a6f2012-02-28 15:36:15 -08002334 funcOffset = OFFSETOF_MEMBER(Thread, pDdiv);
2335 break;
Elliott Hughesadb8c672012-03-06 16:49:32 -08002336 case Instruction::MUL_DOUBLE_2ADDR:
2337 case Instruction::MUL_DOUBLE:
buzbee31a4a6f2012-02-28 15:36:15 -08002338 funcOffset = OFFSETOF_MEMBER(Thread, pDmul);
2339 break;
Elliott Hughesadb8c672012-03-06 16:49:32 -08002340 case Instruction::REM_DOUBLE_2ADDR:
2341 case Instruction::REM_DOUBLE:
buzbee31a4a6f2012-02-28 15:36:15 -08002342 funcOffset = OFFSETOF_MEMBER(Thread, pFmod);
2343 break;
Elliott Hughesadb8c672012-03-06 16:49:32 -08002344 case Instruction::NEG_DOUBLE: {
buzbee31a4a6f2012-02-28 15:36:15 -08002345 genNegDouble(cUnit, rlDest, rlSrc1);
2346 return false;
2347 }
2348 default:
2349 return true;
2350 }
2351 oatFlushAllRegs(cUnit); /* Send everything to home location */
Ian Rogersab2b55d2012-03-18 00:06:11 -07002352 callRuntimeHelperRegLocationRegLocation(cUnit, funcOffset, rlSrc1, rlSrc2);
Ian Rogersf7d9ad32012-03-13 18:45:39 -07002353 rlResult = oatGetReturnWide(cUnit, true);
buzbee31a4a6f2012-02-28 15:36:15 -08002354 storeValueWide(cUnit, rlDest, rlResult);
2355 return false;
2356}
2357
2358bool genConversionPortable(CompilationUnit* cUnit, MIR* mir)
2359{
Elliott Hughesadb8c672012-03-06 16:49:32 -08002360 Instruction::Code opcode = mir->dalvikInsn.opcode;
buzbee31a4a6f2012-02-28 15:36:15 -08002361
2362 switch (opcode) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002363 case Instruction::INT_TO_FLOAT:
buzbee31a4a6f2012-02-28 15:36:15 -08002364 return genConversionCall(cUnit, mir, OFFSETOF_MEMBER(Thread, pI2f),
2365 1, 1);
Elliott Hughesadb8c672012-03-06 16:49:32 -08002366 case Instruction::FLOAT_TO_INT:
buzbee31a4a6f2012-02-28 15:36:15 -08002367 return genConversionCall(cUnit, mir, OFFSETOF_MEMBER(Thread, pF2iz),
2368 1, 1);
Elliott Hughesadb8c672012-03-06 16:49:32 -08002369 case Instruction::DOUBLE_TO_FLOAT:
buzbee31a4a6f2012-02-28 15:36:15 -08002370 return genConversionCall(cUnit, mir, OFFSETOF_MEMBER(Thread, pD2f),
2371 2, 1);
Elliott Hughesadb8c672012-03-06 16:49:32 -08002372 case Instruction::FLOAT_TO_DOUBLE:
buzbee31a4a6f2012-02-28 15:36:15 -08002373 return genConversionCall(cUnit, mir, OFFSETOF_MEMBER(Thread, pF2d),
2374 1, 2);
Elliott Hughesadb8c672012-03-06 16:49:32 -08002375 case Instruction::INT_TO_DOUBLE:
buzbee31a4a6f2012-02-28 15:36:15 -08002376 return genConversionCall(cUnit, mir, OFFSETOF_MEMBER(Thread, pI2d),
2377 1, 2);
Elliott Hughesadb8c672012-03-06 16:49:32 -08002378 case Instruction::DOUBLE_TO_INT:
buzbee31a4a6f2012-02-28 15:36:15 -08002379 return genConversionCall(cUnit, mir, OFFSETOF_MEMBER(Thread, pD2iz),
2380 2, 1);
Elliott Hughesadb8c672012-03-06 16:49:32 -08002381 case Instruction::FLOAT_TO_LONG:
buzbee31a4a6f2012-02-28 15:36:15 -08002382 return genConversionCall(cUnit, mir, OFFSETOF_MEMBER(Thread,
2383 pF2l), 1, 2);
Elliott Hughesadb8c672012-03-06 16:49:32 -08002384 case Instruction::LONG_TO_FLOAT:
buzbee31a4a6f2012-02-28 15:36:15 -08002385 return genConversionCall(cUnit, mir, OFFSETOF_MEMBER(Thread, pL2f),
2386 2, 1);
Elliott Hughesadb8c672012-03-06 16:49:32 -08002387 case Instruction::DOUBLE_TO_LONG:
buzbee31a4a6f2012-02-28 15:36:15 -08002388 return genConversionCall(cUnit, mir, OFFSETOF_MEMBER(Thread,
2389 pD2l), 2, 2);
Elliott Hughesadb8c672012-03-06 16:49:32 -08002390 case Instruction::LONG_TO_DOUBLE:
buzbee31a4a6f2012-02-28 15:36:15 -08002391 return genConversionCall(cUnit, mir, OFFSETOF_MEMBER(Thread, pL2d),
2392 2, 2);
2393 default:
2394 return true;
2395 }
2396 return false;
2397}
2398
2399/*
2400 * Generate callout to updateDebugger. Note that we're overloading
2401 * the use of rSUSPEND here. When the debugger is active, this
2402 * register holds the address of the update function. So, if it's
2403 * non-null, we call out to it.
2404 *
2405 * Note also that rRET0 and rRET1 must be preserved across this
2406 * code. This must be handled by the stub.
2407 */
2408void genDebuggerUpdate(CompilationUnit* cUnit, int32_t offset)
2409{
2410 // Following DCHECK verifies that dPC is in range of single load immediate
2411 DCHECK((offset == DEBUGGER_METHOD_ENTRY) ||
2412 (offset == DEBUGGER_METHOD_EXIT) || ((offset & 0xffff) == offset));
2413 oatClobberCalleeSave(cUnit);
2414#if defined(TARGET_ARM)
2415 opRegImm(cUnit, kOpCmp, rSUSPEND, 0);
buzbee82488f52012-03-02 08:20:26 -08002416 opIT(cUnit, kArmCondNe, "T");
buzbee31a4a6f2012-02-28 15:36:15 -08002417 loadConstant(cUnit, rARG2, offset); // arg2 <- Entry code
2418 opReg(cUnit, kOpBlx, rSUSPEND);
Ian Rogersb5d09b22012-03-06 22:14:17 -08002419#elif defined(TARGET_X86)
2420 UNIMPLEMENTED(FATAL);
buzbee31a4a6f2012-02-28 15:36:15 -08002421#else
buzbee82488f52012-03-02 08:20:26 -08002422 LIR* branch = opCmpImmBranch(cUnit, kCondEq, rSUSPEND, 0, NULL);
buzbee31a4a6f2012-02-28 15:36:15 -08002423 loadConstant(cUnit, rARG2, offset);
2424 opReg(cUnit, kOpBlx, rSUSPEND);
2425 LIR* target = newLIR0(cUnit, kPseudoTargetLabel);
buzbee31a4a6f2012-02-28 15:36:15 -08002426 branch->target = (LIR*)target;
2427#endif
2428 oatFreeTemp(cUnit, rARG2);
2429}
2430
2431/* Check if we need to check for pending suspend request */
2432void genSuspendTest(CompilationUnit* cUnit, MIR* mir)
2433{
2434 if (NO_SUSPEND || (mir->optimizationFlags & MIR_IGNORE_SUSPEND_CHECK)) {
2435 return;
2436 }
2437 oatFlushAllRegs(cUnit);
buzbee31a4a6f2012-02-28 15:36:15 -08002438 if (cUnit->genDebugger) {
2439 // If generating code for the debugger, always check for suspension
Ian Rogers6cbb2bd2012-03-16 13:45:30 -07002440#if defined(TARGET_X86)
2441 UNIMPLEMENTED(FATAL);
2442#else
buzbee86a4bce2012-03-06 18:15:00 -08002443 int rTgt = loadHelper(cUnit, OFFSETOF_MEMBER(Thread,
2444 pTestSuspendFromCode));
2445 opReg(cUnit, kOpBlx, rTgt);
2446 // Refresh rSUSPEND
2447 loadWordDisp(cUnit, rSELF,
2448 OFFSETOF_MEMBER(Thread, pUpdateDebuggerFromCode),
2449 rSUSPEND);
Ian Rogersb5d09b22012-03-06 22:14:17 -08002450#endif
buzbee31a4a6f2012-02-28 15:36:15 -08002451 } else {
Ian Rogersb5d09b22012-03-06 22:14:17 -08002452 LIR* branch = NULL;
buzbee31a4a6f2012-02-28 15:36:15 -08002453#if defined(TARGET_ARM)
2454 // In non-debug case, only check periodically
2455 newLIR2(cUnit, kThumbSubRI8, rSUSPEND, 1);
buzbee82488f52012-03-02 08:20:26 -08002456 branch = opCondBranch(cUnit, kCondEq, NULL);
Ian Rogersb5d09b22012-03-06 22:14:17 -08002457#elif defined(TARGET_X86)
2458 newLIR2(cUnit, kX86Cmp32TI, Thread::SuspendCountOffset().Int32Value(), 0);
2459 branch = opCondBranch(cUnit, kCondNe, NULL);
buzbee31a4a6f2012-02-28 15:36:15 -08002460#else
2461 opRegImm(cUnit, kOpSub, rSUSPEND, 1);
buzbee82488f52012-03-02 08:20:26 -08002462 branch = opCmpImmBranch(cUnit, kCondEq, rSUSPEND, 0, NULL);
buzbee31a4a6f2012-02-28 15:36:15 -08002463#endif
buzbee86a4bce2012-03-06 18:15:00 -08002464 LIR* retLab = newLIR0(cUnit, kPseudoTargetLabel);
2465 LIR* target = rawLIR(cUnit, cUnit->currentDalvikOffset,
2466 kPseudoSuspendTarget, (intptr_t)retLab, mir->offset);
2467 branch->target = (LIR*)target;
2468 oatInsertGrowableList(cUnit, &cUnit->suspendLaunchpads, (intptr_t)target);
buzbee31a4a6f2012-02-28 15:36:15 -08002469 }
buzbee31a4a6f2012-02-28 15:36:15 -08002470}
2471
2472} // namespace art