blob: 3ca0450e1c8679f6bd3ef0e31b071680c8c54dc0 [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);
Ian Rogersc6f3bb82012-03-21 20:40:33 -0700640 if (oatIsTemp(cUnit, rlMethod.lowReg)) {
641 oatFreeTemp(cUnit, rlMethod.lowReg);
642 }
buzbee31a4a6f2012-02-28 15:36:15 -0800643 } else {
644 // Medium path, static storage base in a different class which
645 // requires checks that the other class is initialized.
646 DCHECK_GE(ssbIndex, 0);
647 // May do runtime call so everything to home locations.
648 oatFlushAllRegs(cUnit);
649 // Using fixed register to sync with possible call to runtime
650 // support.
buzbeee1965672012-03-11 18:39:19 -0700651 int rMethod = rARG1;
buzbee31a4a6f2012-02-28 15:36:15 -0800652 oatLockTemp(cUnit, rMethod);
653 loadCurrMethodDirect(cUnit, rMethod);
654 rBase = rARG0;
655 oatLockTemp(cUnit, rBase);
656 loadWordDisp(cUnit, rMethod,
657 Method::DexCacheInitializedStaticStorageOffset().Int32Value(),
658 rBase);
659 loadWordDisp(cUnit, rBase,
660 Array::DataOffset(sizeof(Object*)).Int32Value() + sizeof(int32_t*) *
661 ssbIndex, rBase);
662 // rBase now points at appropriate static storage base (Class*)
663 // or NULL if not initialized. Check for NULL and call helper if NULL.
664 // TUNING: fast path should fall through
buzbee82488f52012-03-02 08:20:26 -0800665 LIR* branchOver = opCmpImmBranch(cUnit, kCondNe, rBase, 0, NULL);
buzbee31a4a6f2012-02-28 15:36:15 -0800666 loadConstant(cUnit, rARG0, ssbIndex);
Ian Rogersab2b55d2012-03-18 00:06:11 -0700667 callRuntimeHelperImm(cUnit,
668 OFFSETOF_MEMBER(Thread, pInitializeStaticStorage),
669 ssbIndex);
buzbee31a4a6f2012-02-28 15:36:15 -0800670#if defined(TARGET_MIPS)
671 // For Arm, rRET0 = rARG0 = rBASE, for Mips, we need to copy
buzbee82488f52012-03-02 08:20:26 -0800672 opRegCopy(cUnit, rBase, rRET0);
buzbee31a4a6f2012-02-28 15:36:15 -0800673#endif
674 LIR* skipTarget = newLIR0(cUnit, kPseudoTargetLabel);
buzbee31a4a6f2012-02-28 15:36:15 -0800675 branchOver->target = (LIR*)skipTarget;
buzbeee1965672012-03-11 18:39:19 -0700676 oatFreeTemp(cUnit, rMethod);
buzbee31a4a6f2012-02-28 15:36:15 -0800677 }
678 // rBase now holds static storage base
buzbee31a4a6f2012-02-28 15:36:15 -0800679 if (isLongOrDouble) {
680 rlSrc = oatGetSrcWide(cUnit, mir, 0, 1);
681 rlSrc = loadValueWide(cUnit, rlSrc, kAnyReg);
682 } else {
683 rlSrc = oatGetSrc(cUnit, mir, 0);
684 rlSrc = loadValue(cUnit, rlSrc, kAnyReg);
685 }
686//FIXME: need to generalize the barrier call
687 if (isVolatile) {
688 oatGenMemBarrier(cUnit, kST);
689 }
690 if (isLongOrDouble) {
691 storeBaseDispWide(cUnit, rBase, fieldOffset, rlSrc.lowReg,
692 rlSrc.highReg);
693 } else {
694 storeWordDisp(cUnit, rBase, fieldOffset, rlSrc.lowReg);
695 }
696 if (isVolatile) {
697 oatGenMemBarrier(cUnit, kSY);
698 }
699 if (isObject) {
700 markGCCard(cUnit, rlSrc.lowReg, rBase);
701 }
702 oatFreeTemp(cUnit, rBase);
703 } else {
704 oatFlushAllRegs(cUnit); // Everything to home locations
705 int setterOffset = isLongOrDouble ? OFFSETOF_MEMBER(Thread, pSet64Static) :
706 (isObject ? OFFSETOF_MEMBER(Thread, pSetObjStatic)
707 : OFFSETOF_MEMBER(Thread, pSet32Static));
Ian Rogersab2b55d2012-03-18 00:06:11 -0700708 callRuntimeHelperImmRegLocation(cUnit, setterOffset, fieldIdx, rlSrc);
buzbee31a4a6f2012-02-28 15:36:15 -0800709 }
710}
711
712void genSget(CompilationUnit* cUnit, MIR* mir, RegLocation rlDest,
713 bool isLongOrDouble, bool isObject)
714{
715 int fieldOffset;
716 int ssbIndex;
717 bool isVolatile;
718 bool isReferrersClass;
719 uint32_t fieldIdx = mir->dalvikInsn.vB;
720
721 OatCompilationUnit mUnit(cUnit->class_loader, cUnit->class_linker,
722 *cUnit->dex_file, *cUnit->dex_cache,
723 cUnit->code_item, cUnit->method_idx,
724 cUnit->access_flags);
725
726 bool fastPath =
727 cUnit->compiler->ComputeStaticFieldInfo(fieldIdx, &mUnit,
728 fieldOffset, ssbIndex,
729 isReferrersClass, isVolatile,
730 false);
731 if (fastPath && !SLOW_FIELD_PATH) {
732 DCHECK_GE(fieldOffset, 0);
733 int rBase;
buzbee31a4a6f2012-02-28 15:36:15 -0800734 if (isReferrersClass) {
735 // Fast path, static storage base is this method's class
buzbeee1965672012-03-11 18:39:19 -0700736 RegLocation rlMethod = loadCurrMethod(cUnit);
buzbee31a4a6f2012-02-28 15:36:15 -0800737 rBase = oatAllocTemp(cUnit);
buzbeee1965672012-03-11 18:39:19 -0700738 loadWordDisp(cUnit, rlMethod.lowReg,
buzbee31a4a6f2012-02-28 15:36:15 -0800739 Method::DeclaringClassOffset().Int32Value(), rBase);
740 } else {
741 // Medium path, static storage base in a different class which
742 // requires checks that the other class is initialized
743 DCHECK_GE(ssbIndex, 0);
744 // May do runtime call so everything to home locations.
745 oatFlushAllRegs(cUnit);
746 // Using fixed register to sync with possible call to runtime
747 // support
buzbeee1965672012-03-11 18:39:19 -0700748 int rMethod = rARG1;
buzbee31a4a6f2012-02-28 15:36:15 -0800749 oatLockTemp(cUnit, rMethod);
750 loadCurrMethodDirect(cUnit, rMethod);
751 rBase = rARG0;
752 oatLockTemp(cUnit, rBase);
753 loadWordDisp(cUnit, rMethod,
754 Method::DexCacheInitializedStaticStorageOffset().Int32Value(),
755 rBase);
756 loadWordDisp(cUnit, rBase,
757 Array::DataOffset(sizeof(Object*)).Int32Value() +
758 sizeof(int32_t*) * ssbIndex,
759 rBase);
760 // rBase now points at appropriate static storage base (Class*)
761 // or NULL if not initialized. Check for NULL and call helper if NULL.
762 // TUNING: fast path should fall through
buzbee82488f52012-03-02 08:20:26 -0800763 LIR* branchOver = opCmpImmBranch(cUnit, kCondNe, rBase, 0, NULL);
Ian Rogersab2b55d2012-03-18 00:06:11 -0700764 callRuntimeHelperImm(cUnit,
765 OFFSETOF_MEMBER(Thread, pInitializeStaticStorage),
766 ssbIndex);
buzbee31a4a6f2012-02-28 15:36:15 -0800767#if defined(TARGET_MIPS)
768 // For Arm, rRET0 = rARG0 = rBASE, for Mips, we need to copy
buzbee82488f52012-03-02 08:20:26 -0800769 opRegCopy(cUnit, rBase, rRET0);
buzbee31a4a6f2012-02-28 15:36:15 -0800770#endif
771 LIR* skipTarget = newLIR0(cUnit, kPseudoTargetLabel);
buzbee31a4a6f2012-02-28 15:36:15 -0800772 branchOver->target = (LIR*)skipTarget;
buzbeee1965672012-03-11 18:39:19 -0700773 oatFreeTemp(cUnit, rMethod);
buzbee31a4a6f2012-02-28 15:36:15 -0800774 }
775 // rBase now holds static storage base
buzbee31a4a6f2012-02-28 15:36:15 -0800776 rlDest = isLongOrDouble ? oatGetDestWide(cUnit, mir, 0, 1)
777 : oatGetDest(cUnit, mir, 0);
778 RegLocation rlResult = oatEvalLoc(cUnit, rlDest, kAnyReg, true);
779 if (isVolatile) {
780 oatGenMemBarrier(cUnit, kSY);
781 }
782 if (isLongOrDouble) {
783 loadBaseDispWide(cUnit, NULL, rBase, fieldOffset, rlResult.lowReg,
784 rlResult.highReg, INVALID_SREG);
785 } else {
786 loadWordDisp(cUnit, rBase, fieldOffset, rlResult.lowReg);
787 }
788 oatFreeTemp(cUnit, rBase);
789 if (isLongOrDouble) {
790 storeValueWide(cUnit, rlDest, rlResult);
791 } else {
792 storeValue(cUnit, rlDest, rlResult);
793 }
794 } else {
795 oatFlushAllRegs(cUnit); // Everything to home locations
796 int getterOffset = isLongOrDouble ? OFFSETOF_MEMBER(Thread, pGet64Static) :
797 (isObject ? OFFSETOF_MEMBER(Thread, pGetObjStatic)
798 : OFFSETOF_MEMBER(Thread, pGet32Static));
Ian Rogersab2b55d2012-03-18 00:06:11 -0700799 callRuntimeHelperImm(cUnit, getterOffset, fieldIdx);
buzbee31a4a6f2012-02-28 15:36:15 -0800800 if (isLongOrDouble) {
Ian Rogersf7d9ad32012-03-13 18:45:39 -0700801 RegLocation rlResult = oatGetReturnWide(cUnit, rlDest.fp);
buzbee31a4a6f2012-02-28 15:36:15 -0800802 storeValueWide(cUnit, rlDest, rlResult);
803 } else {
Ian Rogersf7d9ad32012-03-13 18:45:39 -0700804 RegLocation rlResult = oatGetReturn(cUnit, rlDest.fp);
buzbee31a4a6f2012-02-28 15:36:15 -0800805 storeValue(cUnit, rlDest, rlResult);
806 }
807 }
808}
809
810
811// Debugging routine - if null target, branch to DebugMe
812void genShowTarget(CompilationUnit* cUnit)
813{
buzbeea7678db2012-03-05 15:35:46 -0800814#if defined(TARGET_X86)
815 UNIMPLEMENTED(WARNING) << "genShowTarget";
816#else
buzbee0398c422012-03-02 15:22:47 -0800817 LIR* branchOver = opCmpImmBranch(cUnit, kCondNe, rINVOKE_TGT, 0, NULL);
buzbee31a4a6f2012-02-28 15:36:15 -0800818 loadWordDisp(cUnit, rSELF,
buzbee0398c422012-03-02 15:22:47 -0800819 OFFSETOF_MEMBER(Thread, pDebugMe), rINVOKE_TGT);
buzbee31a4a6f2012-02-28 15:36:15 -0800820 LIR* target = newLIR0(cUnit, kPseudoTargetLabel);
buzbee31a4a6f2012-02-28 15:36:15 -0800821 branchOver->target = (LIR*)target;
buzbeea7678db2012-03-05 15:35:46 -0800822#endif
buzbee31a4a6f2012-02-28 15:36:15 -0800823}
824
825void genThrowVerificationError(CompilationUnit* cUnit, MIR* mir)
826{
Ian Rogersab2b55d2012-03-18 00:06:11 -0700827 callRuntimeHelperImmImm(cUnit, OFFSETOF_MEMBER(Thread,
828 pThrowVerificationErrorFromCode),
829 mir->dalvikInsn.vA, mir->dalvikInsn.vB);
buzbee31a4a6f2012-02-28 15:36:15 -0800830}
831
832void handleSuspendLaunchpads(CompilationUnit *cUnit)
833{
Ian Rogersab2b55d2012-03-18 00:06:11 -0700834 LIR** suspendLabel = (LIR **)cUnit->suspendLaunchpads.elemList;
buzbee31a4a6f2012-02-28 15:36:15 -0800835 int numElems = cUnit->suspendLaunchpads.numUsed;
buzbee31a4a6f2012-02-28 15:36:15 -0800836 for (int i = 0; i < numElems; i++) {
buzbeec5159d52012-03-03 11:48:39 -0800837 oatResetRegPool(cUnit);
buzbee31a4a6f2012-02-28 15:36:15 -0800838 LIR* lab = suspendLabel[i];
839 LIR* resumeLab = (LIR*)lab->operands[0];
840 cUnit->currentDalvikOffset = lab->operands[1];
Ian Rogersab2b55d2012-03-18 00:06:11 -0700841 oatAppendLIR(cUnit, lab);
Ian Rogers6cbb2bd2012-03-16 13:45:30 -0700842#if defined(TARGET_X86)
843 opThreadMem(cUnit, kOpBlx,
844 OFFSETOF_MEMBER(Thread, pTestSuspendFromCode));
845#else
buzbee31a4a6f2012-02-28 15:36:15 -0800846 int rTgt = loadHelper(cUnit, OFFSETOF_MEMBER(Thread,
847 pTestSuspendFromCode));
buzbee31a4a6f2012-02-28 15:36:15 -0800848 opReg(cUnit, kOpBlx, rTgt);
Ian Rogers6cbb2bd2012-03-16 13:45:30 -0700849#endif
buzbee82488f52012-03-02 08:20:26 -0800850 opUnconditionalBranch(cUnit, resumeLab);
buzbee31a4a6f2012-02-28 15:36:15 -0800851 }
852}
853
854void handleThrowLaunchpads(CompilationUnit *cUnit)
855{
Ian Rogersab2b55d2012-03-18 00:06:11 -0700856 LIR** throwLabel = (LIR **)cUnit->throwLaunchpads.elemList;
buzbee31a4a6f2012-02-28 15:36:15 -0800857 int numElems = cUnit->throwLaunchpads.numUsed;
Ian Rogersab2b55d2012-03-18 00:06:11 -0700858 for (int i = 0; i < numElems; i++) {
buzbeec5159d52012-03-03 11:48:39 -0800859 oatResetRegPool(cUnit);
buzbee31a4a6f2012-02-28 15:36:15 -0800860 LIR* lab = throwLabel[i];
861 cUnit->currentDalvikOffset = lab->operands[1];
Ian Rogersab2b55d2012-03-18 00:06:11 -0700862 oatAppendLIR(cUnit, lab);
buzbee31a4a6f2012-02-28 15:36:15 -0800863 int funcOffset = 0;
864 int v1 = lab->operands[2];
865 int v2 = lab->operands[3];
866 switch(lab->operands[0]) {
867 case kThrowNullPointer:
868 funcOffset = OFFSETOF_MEMBER(Thread, pThrowNullPointerFromCode);
869 break;
870 case kThrowArrayBounds:
buzbee5de34942012-03-01 14:51:57 -0800871 if (v2 != rARG0) {
buzbee82488f52012-03-02 08:20:26 -0800872 opRegCopy(cUnit, rARG0, v1);
873 opRegCopy(cUnit, rARG1, v2);
buzbee31a4a6f2012-02-28 15:36:15 -0800874 } else {
buzbee5de34942012-03-01 14:51:57 -0800875 if (v1 == rARG1) {
buzbee31a4a6f2012-02-28 15:36:15 -0800876#if defined(TARGET_ARM)
877 int rTmp = r12;
878#else
879 int rTmp = oatAllocTemp(cUnit);
880#endif
buzbee82488f52012-03-02 08:20:26 -0800881 opRegCopy(cUnit, rTmp, v1);
882 opRegCopy(cUnit, rARG1, v2);
883 opRegCopy(cUnit, rARG0, rTmp);
buzbee31a4a6f2012-02-28 15:36:15 -0800884 } else {
buzbee82488f52012-03-02 08:20:26 -0800885 opRegCopy(cUnit, rARG1, v2);
886 opRegCopy(cUnit, rARG0, v1);
buzbee31a4a6f2012-02-28 15:36:15 -0800887 }
888 }
889 funcOffset = OFFSETOF_MEMBER(Thread, pThrowArrayBoundsFromCode);
890 break;
891 case kThrowDivZero:
892 funcOffset = OFFSETOF_MEMBER(Thread, pThrowDivZeroFromCode);
893 break;
894 case kThrowVerificationError:
895 loadConstant(cUnit, rARG0, v1);
896 loadConstant(cUnit, rARG1, v2);
897 funcOffset =
898 OFFSETOF_MEMBER(Thread, pThrowVerificationErrorFromCode);
899 break;
900 case kThrowNegArraySize:
buzbee82488f52012-03-02 08:20:26 -0800901 opRegCopy(cUnit, rARG0, v1);
buzbee31a4a6f2012-02-28 15:36:15 -0800902 funcOffset =
903 OFFSETOF_MEMBER(Thread, pThrowNegArraySizeFromCode);
904 break;
905 case kThrowNoSuchMethod:
buzbee82488f52012-03-02 08:20:26 -0800906 opRegCopy(cUnit, rARG0, v1);
buzbee31a4a6f2012-02-28 15:36:15 -0800907 funcOffset =
908 OFFSETOF_MEMBER(Thread, pThrowNoSuchMethodFromCode);
909 break;
910 case kThrowStackOverflow:
911 funcOffset =
912 OFFSETOF_MEMBER(Thread, pThrowStackOverflowFromCode);
913 // Restore stack alignment
914 opRegImm(cUnit, kOpAdd, rSP,
915 (cUnit->numCoreSpills + cUnit->numFPSpills) * 4);
916 break;
917 default:
918 LOG(FATAL) << "Unexpected throw kind: " << lab->operands[0];
919 }
Ian Rogersab2b55d2012-03-18 00:06:11 -0700920 oatClobberCalleeSave(cUnit);
921#if !defined(TARGET_X86)
buzbee31a4a6f2012-02-28 15:36:15 -0800922 int rTgt = loadHelper(cUnit, funcOffset);
Ian Rogersab2b55d2012-03-18 00:06:11 -0700923 opReg(cUnit, kOpBlx, rTgt);
buzbee0398c422012-03-02 15:22:47 -0800924 oatFreeTemp(cUnit, rTgt);
Ian Rogersab2b55d2012-03-18 00:06:11 -0700925#else
926 opThreadMem(cUnit, kOpBlx, funcOffset);
Ian Rogers6cbb2bd2012-03-16 13:45:30 -0700927#endif
buzbee31a4a6f2012-02-28 15:36:15 -0800928 }
929}
930
931/* Needed by the Assembler */
932void oatSetupResourceMasks(LIR* lir)
933{
934 setupResourceMasks(lir);
935}
936
buzbee16da88c2012-03-20 10:38:17 -0700937bool fastInstance(CompilationUnit* cUnit, uint32_t fieldIdx,
938 int& fieldOffset, bool& isVolatile, bool isPut)
939{
940 OatCompilationUnit mUnit(cUnit->class_loader, cUnit->class_linker,
941 *cUnit->dex_file, *cUnit->dex_cache,
942 cUnit->code_item, cUnit->method_idx,
943 cUnit->access_flags);
944 return cUnit->compiler->ComputeInstanceFieldInfo(fieldIdx, &mUnit,
945 fieldOffset, isVolatile, isPut);
946}
947
buzbee31a4a6f2012-02-28 15:36:15 -0800948void genIGet(CompilationUnit* cUnit, MIR* mir, OpSize size,
949 RegLocation rlDest, RegLocation rlObj,
950 bool isLongOrDouble, bool isObject)
951{
952 int fieldOffset;
953 bool isVolatile;
954 uint32_t fieldIdx = mir->dalvikInsn.vC;
955
buzbee16da88c2012-03-20 10:38:17 -0700956 bool fastPath = fastInstance(cUnit, fieldIdx, fieldOffset, isVolatile,
957 false);
buzbee31a4a6f2012-02-28 15:36:15 -0800958
959 if (fastPath && !SLOW_FIELD_PATH) {
960 RegLocation rlResult;
961 RegisterClass regClass = oatRegClassBySize(size);
962 DCHECK_GE(fieldOffset, 0);
963 rlObj = loadValue(cUnit, rlObj, kCoreReg);
964 if (isLongOrDouble) {
965 DCHECK(rlDest.wide);
966 genNullCheck(cUnit, rlObj.sRegLow, rlObj.lowReg, mir);/* null? */
Ian Rogersb5d09b22012-03-06 22:14:17 -0800967#if defined(TARGET_X86)
968 rlResult = oatEvalLoc(cUnit, rlDest, regClass, true);
969 genNullCheck(cUnit, rlObj.sRegLow, rlObj.lowReg, mir);/* null? */
970 loadBaseDispWide(cUnit, mir, rlObj.lowReg, fieldOffset, rlResult.lowReg,
971 rlResult.highReg, rlObj.sRegLow);
972 if (isVolatile) {
973 oatGenMemBarrier(cUnit, kSY);
974 }
975#else
buzbee31a4a6f2012-02-28 15:36:15 -0800976 int regPtr = oatAllocTemp(cUnit);
977 opRegRegImm(cUnit, kOpAdd, regPtr, rlObj.lowReg, fieldOffset);
978 rlResult = oatEvalLoc(cUnit, rlDest, regClass, true);
979 loadPair(cUnit, regPtr, rlResult.lowReg, rlResult.highReg);
980 if (isVolatile) {
981 oatGenMemBarrier(cUnit, kSY);
982 }
983 oatFreeTemp(cUnit, regPtr);
Ian Rogersb5d09b22012-03-06 22:14:17 -0800984#endif
buzbee31a4a6f2012-02-28 15:36:15 -0800985 storeValueWide(cUnit, rlDest, rlResult);
986 } else {
987 rlResult = oatEvalLoc(cUnit, rlDest, regClass, true);
988 genNullCheck(cUnit, rlObj.sRegLow, rlObj.lowReg, mir);/* null? */
989 loadBaseDisp(cUnit, mir, rlObj.lowReg, fieldOffset, rlResult.lowReg,
990 kWord, rlObj.sRegLow);
991 if (isVolatile) {
992 oatGenMemBarrier(cUnit, kSY);
993 }
994 storeValue(cUnit, rlDest, rlResult);
995 }
996 } else {
997 int getterOffset = isLongOrDouble ? OFFSETOF_MEMBER(Thread, pGet64Instance) :
998 (isObject ? OFFSETOF_MEMBER(Thread, pGetObjInstance)
999 : OFFSETOF_MEMBER(Thread, pGet32Instance));
Ian Rogersab2b55d2012-03-18 00:06:11 -07001000 callRuntimeHelperImmRegLocation(cUnit, getterOffset, fieldIdx, rlObj);
buzbee31a4a6f2012-02-28 15:36:15 -08001001 if (isLongOrDouble) {
Ian Rogersf7d9ad32012-03-13 18:45:39 -07001002 RegLocation rlResult = oatGetReturnWide(cUnit, rlDest.fp);
buzbee31a4a6f2012-02-28 15:36:15 -08001003 storeValueWide(cUnit, rlDest, rlResult);
1004 } else {
Ian Rogersf7d9ad32012-03-13 18:45:39 -07001005 RegLocation rlResult = oatGetReturn(cUnit, rlDest.fp);
buzbee31a4a6f2012-02-28 15:36:15 -08001006 storeValue(cUnit, rlDest, rlResult);
1007 }
1008 }
1009}
1010
1011void genIPut(CompilationUnit* cUnit, MIR* mir, OpSize size, RegLocation rlSrc,
1012 RegLocation rlObj, bool isLongOrDouble, bool isObject)
1013{
1014 int fieldOffset;
1015 bool isVolatile;
1016 uint32_t fieldIdx = mir->dalvikInsn.vC;
1017
buzbee16da88c2012-03-20 10:38:17 -07001018 bool fastPath = fastInstance(cUnit, fieldIdx, fieldOffset, isVolatile,
1019 true);
buzbee31a4a6f2012-02-28 15:36:15 -08001020 if (fastPath && !SLOW_FIELD_PATH) {
1021 RegisterClass regClass = oatRegClassBySize(size);
1022 DCHECK_GE(fieldOffset, 0);
1023 rlObj = loadValue(cUnit, rlObj, kCoreReg);
1024 if (isLongOrDouble) {
1025 int regPtr;
1026 rlSrc = loadValueWide(cUnit, rlSrc, kAnyReg);
1027 genNullCheck(cUnit, rlObj.sRegLow, rlObj.lowReg, mir);/* null? */
1028 regPtr = oatAllocTemp(cUnit);
1029 opRegRegImm(cUnit, kOpAdd, regPtr, rlObj.lowReg, fieldOffset);
1030 if (isVolatile) {
1031 oatGenMemBarrier(cUnit, kST);
1032 }
1033 storePair(cUnit, regPtr, rlSrc.lowReg, rlSrc.highReg);
1034 if (isVolatile) {
1035 oatGenMemBarrier(cUnit, kSY);
1036 }
1037 oatFreeTemp(cUnit, regPtr);
1038 } else {
1039 rlSrc = loadValue(cUnit, rlSrc, regClass);
1040 genNullCheck(cUnit, rlObj.sRegLow, rlObj.lowReg, mir);/* null? */
1041 if (isVolatile) {
1042 oatGenMemBarrier(cUnit, kST);
1043 }
1044 storeBaseDisp(cUnit, rlObj.lowReg, fieldOffset, rlSrc.lowReg, kWord);
1045 if (isVolatile) {
1046 oatGenMemBarrier(cUnit, kSY);
1047 }
buzbeea7c12682012-03-19 13:13:53 -07001048 if (isObject) {
1049 markGCCard(cUnit, rlSrc.lowReg, rlObj.lowReg);
1050 }
buzbee31a4a6f2012-02-28 15:36:15 -08001051 }
1052 } else {
1053 int setterOffset = isLongOrDouble ? OFFSETOF_MEMBER(Thread, pSet64Instance) :
1054 (isObject ? OFFSETOF_MEMBER(Thread, pSetObjInstance)
1055 : OFFSETOF_MEMBER(Thread, pSet32Instance));
Ian Rogersab2b55d2012-03-18 00:06:11 -07001056 callRuntimeHelperImmRegLocationRegLocation(cUnit, setterOffset,
1057 fieldIdx, rlObj, rlSrc);
buzbee31a4a6f2012-02-28 15:36:15 -08001058 }
1059}
1060
1061void genConstClass(CompilationUnit* cUnit, MIR* mir, RegLocation rlDest,
1062 RegLocation rlSrc)
1063{
1064 uint32_t type_idx = mir->dalvikInsn.vB;
buzbeee1965672012-03-11 18:39:19 -07001065 RegLocation rlMethod = loadCurrMethod(cUnit);
buzbee31a4a6f2012-02-28 15:36:15 -08001066 int resReg = oatAllocTemp(cUnit);
1067 RegLocation rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
1068 if (!cUnit->compiler->CanAccessTypeWithoutChecks(cUnit->method_idx,
1069 cUnit->dex_cache,
1070 *cUnit->dex_file,
1071 type_idx)) {
1072 // Call out to helper which resolves type and verifies access.
1073 // Resolved type returned in rRET0.
Ian Rogersab2b55d2012-03-18 00:06:11 -07001074 callRuntimeHelperImmReg(cUnit,
1075 OFFSETOF_MEMBER(Thread,
1076 pInitializeTypeAndVerifyAccessFromCode),
1077 type_idx, rlMethod.lowReg);
Ian Rogersf7d9ad32012-03-13 18:45:39 -07001078 RegLocation rlResult = oatGetReturn(cUnit, false);
buzbee31a4a6f2012-02-28 15:36:15 -08001079 storeValue(cUnit, rlDest, rlResult);
1080 } else {
1081 // We're don't need access checks, load type from dex cache
1082 int32_t dex_cache_offset =
1083 Method::DexCacheResolvedTypesOffset().Int32Value();
buzbeee1965672012-03-11 18:39:19 -07001084 loadWordDisp(cUnit, rlMethod.lowReg, dex_cache_offset, resReg);
buzbee31a4a6f2012-02-28 15:36:15 -08001085 int32_t offset_of_type =
1086 Array::DataOffset(sizeof(Class*)).Int32Value() + (sizeof(Class*)
1087 * type_idx);
1088 loadWordDisp(cUnit, resReg, offset_of_type, rlResult.lowReg);
1089 if (!cUnit->compiler->CanAssumeTypeIsPresentInDexCache(cUnit->dex_cache,
1090 type_idx) || SLOW_TYPE_PATH) {
1091 // Slow path, at runtime test if type is null and if so initialize
1092 oatFlushAllRegs(cUnit);
buzbee82488f52012-03-02 08:20:26 -08001093 LIR* branch1 = opCmpImmBranch(cUnit, kCondEq, rlResult.lowReg, 0,
1094 NULL);
buzbee31a4a6f2012-02-28 15:36:15 -08001095 // Resolved, store and hop over following code
1096 storeValue(cUnit, rlDest, rlResult);
buzbee3d661942012-03-14 17:37:27 -07001097 /*
1098 * Because we have stores of the target value on two paths,
1099 * clobber temp tracking for the destination using the ssa name
1100 */
1101 oatClobberSReg(cUnit, rlDest.sRegLow);
buzbee82488f52012-03-02 08:20:26 -08001102 LIR* branch2 = opUnconditionalBranch(cUnit,0);
buzbee31a4a6f2012-02-28 15:36:15 -08001103 // TUNING: move slow path to end & remove unconditional branch
1104 LIR* target1 = newLIR0(cUnit, kPseudoTargetLabel);
buzbee5de34942012-03-01 14:51:57 -08001105 // Call out to helper, which will return resolved type in rARG0
Ian Rogersab2b55d2012-03-18 00:06:11 -07001106 callRuntimeHelperImmReg(cUnit, OFFSETOF_MEMBER(Thread,
1107 pInitializeTypeFromCode),
1108 type_idx, rlMethod.lowReg);
Ian Rogersf7d9ad32012-03-13 18:45:39 -07001109 RegLocation rlResult = oatGetReturn(cUnit, false);
buzbee31a4a6f2012-02-28 15:36:15 -08001110 storeValue(cUnit, rlDest, rlResult);
buzbee3d661942012-03-14 17:37:27 -07001111 /*
1112 * Because we have stores of the target value on two paths,
1113 * clobber temp tracking for the destination using the ssa name
1114 */
1115 oatClobberSReg(cUnit, rlDest.sRegLow);
buzbee31a4a6f2012-02-28 15:36:15 -08001116 // Rejoin code paths
1117 LIR* target2 = newLIR0(cUnit, kPseudoTargetLabel);
buzbee31a4a6f2012-02-28 15:36:15 -08001118 branch1->target = (LIR*)target1;
1119 branch2->target = (LIR*)target2;
1120 } else {
1121 // Fast path, we're done - just store result
1122 storeValue(cUnit, rlDest, rlResult);
1123 }
1124 }
1125}
Ian Rogersab2b55d2012-03-18 00:06:11 -07001126
buzbee31a4a6f2012-02-28 15:36:15 -08001127void genConstString(CompilationUnit* cUnit, MIR* mir, RegLocation rlDest,
1128 RegLocation rlSrc)
1129{
1130 /* NOTE: Most strings should be available at compile time */
1131 uint32_t string_idx = mir->dalvikInsn.vB;
1132 int32_t offset_of_string = Array::DataOffset(sizeof(String*)).Int32Value() +
1133 (sizeof(String*) * string_idx);
1134 if (!cUnit->compiler->CanAssumeStringIsPresentInDexCache(
1135 cUnit->dex_cache, string_idx) || SLOW_STRING_PATH) {
1136 // slow path, resolve string if not in dex cache
1137 oatFlushAllRegs(cUnit);
1138 oatLockCallTemps(cUnit); // Using explicit registers
1139 loadCurrMethodDirect(cUnit, rARG2);
1140 loadWordDisp(cUnit, rARG2,
1141 Method::DexCacheStringsOffset().Int32Value(), rARG0);
1142 // Might call out to helper, which will return resolved string in rRET0
Ian Rogers6cbb2bd2012-03-16 13:45:30 -07001143#if !defined(TARGET_X86)
buzbee31a4a6f2012-02-28 15:36:15 -08001144 int rTgt = loadHelper(cUnit, OFFSETOF_MEMBER(Thread,
1145 pResolveStringFromCode));
Ian Rogers6cbb2bd2012-03-16 13:45:30 -07001146#endif
buzbee31a4a6f2012-02-28 15:36:15 -08001147 loadWordDisp(cUnit, rRET0, offset_of_string, rARG0);
1148 loadConstant(cUnit, rARG1, string_idx);
1149#if defined(TARGET_ARM)
1150 opRegImm(cUnit, kOpCmp, rRET0, 0); // Is resolved?
1151 genBarrier(cUnit);
1152 // For testing, always force through helper
1153 if (!EXERCISE_SLOWEST_STRING_PATH) {
buzbee82488f52012-03-02 08:20:26 -08001154 opIT(cUnit, kArmCondEq, "T");
buzbee31a4a6f2012-02-28 15:36:15 -08001155 }
buzbee82488f52012-03-02 08:20:26 -08001156 opRegCopy(cUnit, rARG0, rARG2); // .eq
buzbee31a4a6f2012-02-28 15:36:15 -08001157 opReg(cUnit, kOpBlx, rTgt); // .eq, helper(Method*, string_idx)
Ian Rogers6cbb2bd2012-03-16 13:45:30 -07001158 oatFreeTemp(cUnit, rTgt);
1159#elif defined(TARGET_MIPS)
buzbee82488f52012-03-02 08:20:26 -08001160 LIR* branch = opCmpImmBranch(cUnit, kCondNe, rRET0, 0, NULL);
1161 opRegCopy(cUnit, rARG0, rARG2); // .eq
buzbee31a4a6f2012-02-28 15:36:15 -08001162 opReg(cUnit, kOpBlx, rTgt);
Ian Rogers6cbb2bd2012-03-16 13:45:30 -07001163 oatFreeTemp(cUnit, rTgt);
buzbee31a4a6f2012-02-28 15:36:15 -08001164 LIR* target = newLIR0(cUnit, kPseudoTargetLabel);
buzbee31a4a6f2012-02-28 15:36:15 -08001165 branch->target = target;
Ian Rogers6cbb2bd2012-03-16 13:45:30 -07001166#else
Ian Rogersab2b55d2012-03-18 00:06:11 -07001167 callRuntimeHelperRegReg(cUnit, OFFSETOF_MEMBER(Thread,
1168 pResolveStringFromCode),
1169 rARG2, rARG1);
buzbee31a4a6f2012-02-28 15:36:15 -08001170#endif
1171 genBarrier(cUnit);
Ian Rogersf7d9ad32012-03-13 18:45:39 -07001172 storeValue(cUnit, rlDest, oatGetReturn(cUnit, false));
buzbee31a4a6f2012-02-28 15:36:15 -08001173 } else {
buzbeee1965672012-03-11 18:39:19 -07001174 RegLocation rlMethod = loadCurrMethod(cUnit);
buzbee31a4a6f2012-02-28 15:36:15 -08001175 int resReg = oatAllocTemp(cUnit);
1176 RegLocation rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
buzbeee1965672012-03-11 18:39:19 -07001177 loadWordDisp(cUnit, rlMethod.lowReg,
buzbee31a4a6f2012-02-28 15:36:15 -08001178 Method::DexCacheStringsOffset().Int32Value(), resReg);
1179 loadWordDisp(cUnit, resReg, offset_of_string, rlResult.lowReg);
1180 storeValue(cUnit, rlDest, rlResult);
1181 }
1182}
1183
1184/*
1185 * Let helper function take care of everything. Will
1186 * call Class::NewInstanceFromCode(type_idx, method);
1187 */
1188void genNewInstance(CompilationUnit* cUnit, MIR* mir, RegLocation rlDest)
1189{
1190 oatFlushAllRegs(cUnit); /* Everything to home location */
1191 uint32_t type_idx = mir->dalvikInsn.vB;
1192 // alloc will always check for resolution, do we also need to verify
1193 // access because the verifier was unable to?
Ian Rogers6cbb2bd2012-03-16 13:45:30 -07001194 int funcOffset;
buzbee31a4a6f2012-02-28 15:36:15 -08001195 if (cUnit->compiler->CanAccessInstantiableTypeWithoutChecks(
1196 cUnit->method_idx, cUnit->dex_cache, *cUnit->dex_file, type_idx)) {
Ian Rogers6cbb2bd2012-03-16 13:45:30 -07001197 funcOffset = OFFSETOF_MEMBER(Thread, pAllocObjectFromCode);
buzbee31a4a6f2012-02-28 15:36:15 -08001198 } else {
Ian Rogers6cbb2bd2012-03-16 13:45:30 -07001199 funcOffset =
1200 OFFSETOF_MEMBER(Thread, pAllocObjectFromCodeWithAccessCheck);
buzbee31a4a6f2012-02-28 15:36:15 -08001201 }
Ian Rogersab2b55d2012-03-18 00:06:11 -07001202 callRuntimeHelperImmMethod(cUnit, funcOffset, type_idx);
Ian Rogersf7d9ad32012-03-13 18:45:39 -07001203 RegLocation rlResult = oatGetReturn(cUnit, false);
buzbee31a4a6f2012-02-28 15:36:15 -08001204 storeValue(cUnit, rlDest, rlResult);
1205}
1206
Ian Rogersab2b55d2012-03-18 00:06:11 -07001207void genThrow(CompilationUnit* cUnit, MIR* mir, RegLocation rlSrc)
1208{
1209 oatFlushAllRegs(cUnit);
1210 callRuntimeHelperRegLocation(cUnit, OFFSETOF_MEMBER(Thread, pDeliverException), rlSrc);
1211}
1212
buzbee31a4a6f2012-02-28 15:36:15 -08001213void genInstanceof(CompilationUnit* cUnit, MIR* mir, RegLocation rlDest,
1214 RegLocation rlSrc)
1215{
1216 oatFlushAllRegs(cUnit);
1217 // May generate a call - use explicit registers
1218 oatLockCallTemps(cUnit);
1219 uint32_t type_idx = mir->dalvikInsn.vC;
buzbee5de34942012-03-01 14:51:57 -08001220 loadCurrMethodDirect(cUnit, rARG1); // rARG1 <= current Method*
buzbee31a4a6f2012-02-28 15:36:15 -08001221 int classReg = rARG2; // rARG2 will hold the Class*
1222 if (!cUnit->compiler->CanAccessTypeWithoutChecks(cUnit->method_idx,
1223 cUnit->dex_cache,
1224 *cUnit->dex_file,
1225 type_idx)) {
1226 // Check we have access to type_idx and if not throw IllegalAccessError,
buzbee5de34942012-03-01 14:51:57 -08001227 // returns Class* in rARG0
Ian Rogersab2b55d2012-03-18 00:06:11 -07001228 callRuntimeHelperImm(cUnit,
1229 OFFSETOF_MEMBER(Thread, pInitializeTypeAndVerifyAccessFromCode),
1230 type_idx);
buzbee82488f52012-03-02 08:20:26 -08001231 opRegCopy(cUnit, classReg, rRET0); // Align usage with fast path
buzbee5de34942012-03-01 14:51:57 -08001232 loadValueDirectFixed(cUnit, rlSrc, rARG0); // rARG0 <= ref
buzbee31a4a6f2012-02-28 15:36:15 -08001233 } else {
buzbee5de34942012-03-01 14:51:57 -08001234 // Load dex cache entry into classReg (rARG2)
buzbee31a4a6f2012-02-28 15:36:15 -08001235 loadValueDirectFixed(cUnit, rlSrc, rARG0); // rARG0 <= ref
1236 loadWordDisp(cUnit, rARG1,
1237 Method::DexCacheResolvedTypesOffset().Int32Value(),
1238 classReg);
1239 int32_t offset_of_type =
1240 Array::DataOffset(sizeof(Class*)).Int32Value() + (sizeof(Class*)
1241 * type_idx);
1242 loadWordDisp(cUnit, classReg, offset_of_type, classReg);
1243 if (!cUnit->compiler->CanAssumeTypeIsPresentInDexCache(
1244 cUnit->dex_cache, type_idx)) {
1245 // Need to test presence of type in dex cache at runtime
buzbee82488f52012-03-02 08:20:26 -08001246 LIR* hopBranch = opCmpImmBranch(cUnit, kCondNe, classReg, 0, NULL);
buzbee31a4a6f2012-02-28 15:36:15 -08001247 // Not resolved
1248 // Call out to helper, which will return resolved type in rRET0
Ian Rogersab2b55d2012-03-18 00:06:11 -07001249 callRuntimeHelperImm(cUnit, OFFSETOF_MEMBER(Thread,
1250 pInitializeTypeFromCode),
1251 type_idx);
buzbee82488f52012-03-02 08:20:26 -08001252 opRegCopy(cUnit, rARG2, rRET0); // Align usage with fast path
buzbee31a4a6f2012-02-28 15:36:15 -08001253 loadValueDirectFixed(cUnit, rlSrc, rARG0); /* reload Ref */
1254 // Rejoin code paths
1255 LIR* hopTarget = newLIR0(cUnit, kPseudoTargetLabel);
buzbee31a4a6f2012-02-28 15:36:15 -08001256 hopBranch->target = (LIR*)hopTarget;
1257 }
1258 }
1259 /* rARG0 is ref, rARG2 is class. If ref==null, use directly as bool result */
buzbee82488f52012-03-02 08:20:26 -08001260 LIR* branch1 = opCmpImmBranch(cUnit, kCondEq, rARG0, 0, NULL);
Elliott Hughese84278b2012-03-22 10:06:53 -07001261 /* load object->klass_ */
buzbee31a4a6f2012-02-28 15:36:15 -08001262 DCHECK_EQ(Object::ClassOffset().Int32Value(), 0);
1263 loadWordDisp(cUnit, rARG0, Object::ClassOffset().Int32Value(), rARG1);
Elliott Hughese84278b2012-03-22 10:06:53 -07001264 /* rARG0 is ref, rARG1 is ref->klass_, rARG2 is class */
buzbee0398c422012-03-02 15:22:47 -08001265#if defined(TARGET_ARM)
1266 /* Uses conditional nullification */
buzbee31a4a6f2012-02-28 15:36:15 -08001267 int rTgt = loadHelper(cUnit, OFFSETOF_MEMBER(Thread,
1268 pInstanceofNonTrivialFromCode));
buzbee31a4a6f2012-02-28 15:36:15 -08001269 opRegReg(cUnit, kOpCmp, rARG1, rARG2); // Same?
buzbee82488f52012-03-02 08:20:26 -08001270 opIT(cUnit, kArmCondEq, "EE"); // if-convert the test
buzbee31a4a6f2012-02-28 15:36:15 -08001271 loadConstant(cUnit, rARG0, 1); // .eq case - load true
buzbee82488f52012-03-02 08:20:26 -08001272 opRegCopy(cUnit, rARG0, rARG2); // .ne case - arg0 <= class
buzbee31a4a6f2012-02-28 15:36:15 -08001273 opReg(cUnit, kOpBlx, rTgt); // .ne case: helper(class, ref->class)
Ian Rogersab2b55d2012-03-18 00:06:11 -07001274 oatFreeTemp(cUnit, rTgt);
buzbee31a4a6f2012-02-28 15:36:15 -08001275#else
buzbee0398c422012-03-02 15:22:47 -08001276 /* Uses branchovers */
1277 loadConstant(cUnit, rARG0, 1); // assume true
1278 LIR* branchover = opCmpBranch(cUnit, kCondEq, rARG1, rARG2, NULL);
Ian Rogers6cbb2bd2012-03-16 13:45:30 -07001279#if !defined(TARGET_X86)
buzbee0398c422012-03-02 15:22:47 -08001280 int rTgt = loadHelper(cUnit, OFFSETOF_MEMBER(Thread,
1281 pInstanceofNonTrivialFromCode));
1282 opRegCopy(cUnit, rARG0, rARG2); // .ne case - arg0 <= class
1283 opReg(cUnit, kOpBlx, rTgt); // .ne case: helper(class, ref->class)
Ian Rogersab2b55d2012-03-18 00:06:11 -07001284 oatFreeTemp(cUnit, rTgt);
Ian Rogers6cbb2bd2012-03-16 13:45:30 -07001285#else
1286 opRegCopy(cUnit, rARG0, rARG2);
Ian Rogersab2b55d2012-03-18 00:06:11 -07001287 opThreadMem(cUnit, kOpBlx,
1288 OFFSETOF_MEMBER(Thread, pInstanceofNonTrivialFromCode));
Ian Rogers6cbb2bd2012-03-16 13:45:30 -07001289#endif
buzbee31a4a6f2012-02-28 15:36:15 -08001290#endif
buzbee0398c422012-03-02 15:22:47 -08001291 oatClobberCalleeSave(cUnit);
1292 /* branch targets here */
buzbee31a4a6f2012-02-28 15:36:15 -08001293 LIR* target = newLIR0(cUnit, kPseudoTargetLabel);
Ian Rogersf7d9ad32012-03-13 18:45:39 -07001294 RegLocation rlResult = oatGetReturn(cUnit, false);
buzbee31a4a6f2012-02-28 15:36:15 -08001295 storeValue(cUnit, rlDest, rlResult);
buzbee0398c422012-03-02 15:22:47 -08001296 branch1->target = target;
1297#if !defined(TARGET_ARM)
1298 branchover->target = target;
1299#endif
buzbee31a4a6f2012-02-28 15:36:15 -08001300}
1301
1302void genCheckCast(CompilationUnit* cUnit, MIR* mir, RegLocation rlSrc)
1303{
1304 oatFlushAllRegs(cUnit);
1305 // May generate a call - use explicit registers
1306 oatLockCallTemps(cUnit);
1307 uint32_t type_idx = mir->dalvikInsn.vB;
1308 loadCurrMethodDirect(cUnit, rARG1); // rARG1 <= current Method*
1309 int classReg = rARG2; // rARG2 will hold the Class*
1310 if (!cUnit->compiler->CanAccessTypeWithoutChecks(cUnit->method_idx,
1311 cUnit->dex_cache,
1312 *cUnit->dex_file,
1313 type_idx)) {
1314 // Check we have access to type_idx and if not throw IllegalAccessError,
1315 // returns Class* in rRET0
Ian Rogersab2b55d2012-03-18 00:06:11 -07001316 // InitializeTypeAndVerifyAccess(idx, method)
1317 callRuntimeHelperImmReg(cUnit,
1318 OFFSETOF_MEMBER(Thread,
1319 pInitializeTypeAndVerifyAccessFromCode),
1320 type_idx, rARG1);
buzbee82488f52012-03-02 08:20:26 -08001321 opRegCopy(cUnit, classReg, rRET0); // Align usage with fast path
buzbee31a4a6f2012-02-28 15:36:15 -08001322 } else {
1323 // Load dex cache entry into classReg (rARG2)
1324 loadWordDisp(cUnit, rARG1,
1325 Method::DexCacheResolvedTypesOffset().Int32Value(),
1326 classReg);
1327 int32_t offset_of_type =
1328 Array::DataOffset(sizeof(Class*)).Int32Value() +
1329 (sizeof(Class*) * type_idx);
1330 loadWordDisp(cUnit, classReg, offset_of_type, classReg);
1331 if (!cUnit->compiler->CanAssumeTypeIsPresentInDexCache(
1332 cUnit->dex_cache, type_idx)) {
1333 // Need to test presence of type in dex cache at runtime
buzbee82488f52012-03-02 08:20:26 -08001334 LIR* hopBranch = opCmpImmBranch(cUnit, kCondNe, classReg, 0, NULL);
buzbee31a4a6f2012-02-28 15:36:15 -08001335 // Not resolved
buzbee5de34942012-03-01 14:51:57 -08001336 // Call out to helper, which will return resolved type in rARG0
Ian Rogersab2b55d2012-03-18 00:06:11 -07001337 // InitializeTypeFromCode(idx, method)
1338 callRuntimeHelperImmReg(cUnit,
1339 OFFSETOF_MEMBER(Thread, pInitializeTypeFromCode),
1340 type_idx, rARG1);
buzbee82488f52012-03-02 08:20:26 -08001341 opRegCopy(cUnit, classReg, rARG0); // Align usage with fast path
buzbee31a4a6f2012-02-28 15:36:15 -08001342 // Rejoin code paths
1343 LIR* hopTarget = newLIR0(cUnit, kPseudoTargetLabel);
buzbee31a4a6f2012-02-28 15:36:15 -08001344 hopBranch->target = (LIR*)hopTarget;
1345 }
1346 }
buzbee5de34942012-03-01 14:51:57 -08001347 // At this point, classReg (rARG2) has class
buzbee31a4a6f2012-02-28 15:36:15 -08001348 loadValueDirectFixed(cUnit, rlSrc, rARG0); // rARG0 <= ref
1349 /* Null is OK - continue */
buzbee82488f52012-03-02 08:20:26 -08001350 LIR* branch1 = opCmpImmBranch(cUnit, kCondEq, rARG0, 0, NULL);
Elliott Hughese84278b2012-03-22 10:06:53 -07001351 /* load object->klass_ */
buzbee31a4a6f2012-02-28 15:36:15 -08001352 DCHECK_EQ(Object::ClassOffset().Int32Value(), 0);
1353 loadWordDisp(cUnit, rARG0, Object::ClassOffset().Int32Value(), rARG1);
Elliott Hughese84278b2012-03-22 10:06:53 -07001354 /* rARG1 now contains object->klass_ */
Ian Rogersab2b55d2012-03-18 00:06:11 -07001355#if defined(TARGET_MIPS) || defined(TARGET_X86)
buzbee82488f52012-03-02 08:20:26 -08001356 LIR* branch2 = opCmpBranch(cUnit, kCondEq, rARG1, classReg, NULL);
Ian Rogersab2b55d2012-03-18 00:06:11 -07001357 callRuntimeHelperRegReg(cUnit, OFFSETOF_MEMBER(Thread,
1358 pCheckCastFromCode),
1359 rARG1, rARG2);
1360#else // defined(TARGET_ARM)
Ian Rogers6cbb2bd2012-03-16 13:45:30 -07001361 int rTgt = loadHelper(cUnit, OFFSETOF_MEMBER(Thread,
1362 pCheckCastFromCode));
1363 opRegReg(cUnit, kOpCmp, rARG1, classReg);
1364 LIR* branch2 = opCondBranch(cUnit, kCondEq, NULL); /* If eq, trivial yes */
1365 opRegCopy(cUnit, rARG0, rARG1);
1366 opRegCopy(cUnit, rARG1, rARG2);
Ian Rogersab2b55d2012-03-18 00:06:11 -07001367 oatClobberCalleeSave(cUnit);
1368 opReg(cUnit, kOpBlx, rTgt);
Ian Rogers6cbb2bd2012-03-16 13:45:30 -07001369 oatFreeTemp(cUnit, rTgt);
Ian Rogers6cbb2bd2012-03-16 13:45:30 -07001370#endif
buzbee31a4a6f2012-02-28 15:36:15 -08001371 /* branch target here */
1372 LIR* target = newLIR0(cUnit, kPseudoTargetLabel);
buzbee31a4a6f2012-02-28 15:36:15 -08001373 branch1->target = (LIR*)target;
1374 branch2->target = (LIR*)target;
1375}
1376
buzbee31a4a6f2012-02-28 15:36:15 -08001377/*
1378 * Generate array store
1379 *
1380 */
1381void genArrayObjPut(CompilationUnit* cUnit, MIR* mir, RegLocation rlArray,
1382 RegLocation rlIndex, RegLocation rlSrc, int scale)
1383{
1384 RegisterClass regClass = oatRegClassBySize(kWord);
1385 int lenOffset = Array::LengthOffset().Int32Value();
1386 int dataOffset = Array::DataOffset(sizeof(Object*)).Int32Value();
1387
1388 oatFlushAllRegs(cUnit);
1389 /* Make sure it's a legal object Put. Use direct regs at first */
1390 loadValueDirectFixed(cUnit, rlArray, rARG1);
1391 loadValueDirectFixed(cUnit, rlSrc, rARG0);
1392
1393 /* null array object? */
1394 genNullCheck(cUnit, rlArray.sRegLow, rARG1, mir);
Elliott Hughese84278b2012-03-22 10:06:53 -07001395 /* Get the array's class */
buzbee31a4a6f2012-02-28 15:36:15 -08001396 loadWordDisp(cUnit, rARG1, Object::ClassOffset().Int32Value(), rARG1);
Ian Rogersab2b55d2012-03-18 00:06:11 -07001397 callRuntimeHelperRegReg(cUnit, OFFSETOF_MEMBER(Thread,
1398 pCanPutArrayElementFromCode),
1399 rARG0, rARG1);
buzbee31a4a6f2012-02-28 15:36:15 -08001400 oatFreeTemp(cUnit, rARG0);
1401 oatFreeTemp(cUnit, rARG1);
1402
1403 // Now, redo loadValues in case they didn't survive the call
1404
buzbee31a4a6f2012-02-28 15:36:15 -08001405 rlArray = loadValue(cUnit, rlArray, kCoreReg);
1406 rlIndex = loadValue(cUnit, rlIndex, kCoreReg);
1407
Ian Rogersb41b33b2012-03-20 14:22:54 -07001408#if defined(TARGET_X86)
1409 if (!(mir->optimizationFlags & MIR_IGNORE_RANGE_CHECK)) {
1410 /* if (rlIndex >= [rlArray + lenOffset]) goto kThrowArrayBounds */
1411 genRegMemCheck(cUnit, kCondUge, rlIndex.lowReg, rlArray.lowReg,
1412 lenOffset, mir, kThrowArrayBounds);
1413 }
1414 rlSrc = loadValue(cUnit, rlSrc, regClass);
1415 storeBaseIndexedDisp(cUnit, NULL, rlArray.lowReg, rlIndex.lowReg, scale,
1416 dataOffset, rlSrc.lowReg, INVALID_REG, kWord,
1417 INVALID_SREG);
Ian Rogersc6f3bb82012-03-21 20:40:33 -07001418 if (oatIsTemp(cUnit, rlIndex.lowReg)) {
1419 oatFreeTemp(cUnit, rlIndex.lowReg);
1420 }
Ian Rogersb41b33b2012-03-20 14:22:54 -07001421#else
1422 int regPtr;
buzbee31a4a6f2012-02-28 15:36:15 -08001423 if (oatIsTemp(cUnit, rlArray.lowReg)) {
1424 oatClobber(cUnit, rlArray.lowReg);
1425 regPtr = rlArray.lowReg;
1426 } else {
1427 regPtr = oatAllocTemp(cUnit);
buzbee82488f52012-03-02 08:20:26 -08001428 opRegCopy(cUnit, regPtr, rlArray.lowReg);
buzbee31a4a6f2012-02-28 15:36:15 -08001429 }
1430
buzbee239c4e72012-03-16 08:42:29 -07001431 bool needsRangeCheck = (!(mir->optimizationFlags & MIR_IGNORE_RANGE_CHECK));
1432 int regLen = INVALID_REG;
1433 if (needsRangeCheck) {
1434 regLen = oatAllocTemp(cUnit);
buzbee31a4a6f2012-02-28 15:36:15 -08001435 //NOTE: max live temps(4) here.
1436 /* Get len */
1437 loadWordDisp(cUnit, rlArray.lowReg, lenOffset, regLen);
buzbee239c4e72012-03-16 08:42:29 -07001438 }
1439 /* regPtr -> array data */
1440 opRegImm(cUnit, kOpAdd, regPtr, dataOffset);
1441 /* at this point, regPtr points to array, 2 live temps */
1442 rlSrc = loadValue(cUnit, rlSrc, regClass);
1443 if (needsRangeCheck) {
buzbee31a4a6f2012-02-28 15:36:15 -08001444 genRegRegCheck(cUnit, kCondCs, rlIndex.lowReg, regLen, mir,
1445 kThrowArrayBounds);
1446 oatFreeTemp(cUnit, regLen);
buzbee31a4a6f2012-02-28 15:36:15 -08001447 }
buzbee31a4a6f2012-02-28 15:36:15 -08001448 storeBaseIndexed(cUnit, regPtr, rlIndex.lowReg, rlSrc.lowReg,
1449 scale, kWord);
Ian Rogersb41b33b2012-03-20 14:22:54 -07001450#endif
buzbeea7c12682012-03-19 13:13:53 -07001451 markGCCard(cUnit, rlSrc.lowReg, rlArray.lowReg);
buzbee31a4a6f2012-02-28 15:36:15 -08001452}
1453
1454/*
1455 * Generate array load
1456 */
1457void genArrayGet(CompilationUnit* cUnit, MIR* mir, OpSize size,
1458 RegLocation rlArray, RegLocation rlIndex,
1459 RegLocation rlDest, int scale)
1460{
1461 RegisterClass regClass = oatRegClassBySize(size);
1462 int lenOffset = Array::LengthOffset().Int32Value();
1463 int dataOffset;
1464 RegLocation rlResult;
1465 rlArray = loadValue(cUnit, rlArray, kCoreReg);
1466 rlIndex = loadValue(cUnit, rlIndex, kCoreReg);
buzbee31a4a6f2012-02-28 15:36:15 -08001467
1468 if (size == kLong || size == kDouble) {
1469 dataOffset = Array::DataOffset(sizeof(int64_t)).Int32Value();
1470 } else {
1471 dataOffset = Array::DataOffset(sizeof(int32_t)).Int32Value();
1472 }
1473
1474 /* null object? */
1475 genNullCheck(cUnit, rlArray.sRegLow, rlArray.lowReg, mir);
1476
Ian Rogersb5d09b22012-03-06 22:14:17 -08001477#if defined(TARGET_X86)
1478 if (!(mir->optimizationFlags & MIR_IGNORE_RANGE_CHECK)) {
1479 /* if (rlIndex >= [rlArray + lenOffset]) goto kThrowArrayBounds */
1480 genRegMemCheck(cUnit, kCondUge, rlIndex.lowReg, rlArray.lowReg,
1481 lenOffset, mir, kThrowArrayBounds);
1482 }
1483 if ((size == kLong) || (size == kDouble)) {
1484 rlResult = oatEvalLoc(cUnit, rlDest, regClass, true);
1485 loadBaseIndexedDisp(cUnit, NULL, rlArray.lowReg, rlIndex.lowReg, scale, dataOffset,
1486 rlResult.lowReg, rlResult.highReg, size, INVALID_SREG);
buzbee31a4a6f2012-02-28 15:36:15 -08001487
Ian Rogersb5d09b22012-03-06 22:14:17 -08001488 storeValueWide(cUnit, rlDest, rlResult);
1489 } else {
1490 rlResult = oatEvalLoc(cUnit, rlDest, regClass, true);
1491
1492 loadBaseIndexedDisp(cUnit, NULL, rlArray.lowReg, rlIndex.lowReg, scale, dataOffset,
1493 rlResult.lowReg, INVALID_REG, size, INVALID_SREG);
1494
1495 storeValue(cUnit, rlDest, rlResult);
1496 }
1497#else
1498 int regPtr = oatAllocTemp(cUnit);
buzbee239c4e72012-03-16 08:42:29 -07001499 bool needsRangeCheck = (!(mir->optimizationFlags & MIR_IGNORE_RANGE_CHECK));
1500 int regLen = INVALID_REG;
1501 if (needsRangeCheck) {
1502 regLen = oatAllocTemp(cUnit);
buzbee31a4a6f2012-02-28 15:36:15 -08001503 /* Get len */
1504 loadWordDisp(cUnit, rlArray.lowReg, lenOffset, regLen);
buzbee31a4a6f2012-02-28 15:36:15 -08001505 }
buzbee239c4e72012-03-16 08:42:29 -07001506 /* regPtr -> array data */
1507 opRegRegImm(cUnit, kOpAdd, regPtr, rlArray.lowReg, dataOffset);
buzbee31a4a6f2012-02-28 15:36:15 -08001508 oatFreeTemp(cUnit, rlArray.lowReg);
1509 if ((size == kLong) || (size == kDouble)) {
1510 if (scale) {
1511 int rNewIndex = oatAllocTemp(cUnit);
1512 opRegRegImm(cUnit, kOpLsl, rNewIndex, rlIndex.lowReg, scale);
1513 opRegReg(cUnit, kOpAdd, regPtr, rNewIndex);
1514 oatFreeTemp(cUnit, rNewIndex);
1515 } else {
1516 opRegReg(cUnit, kOpAdd, regPtr, rlIndex.lowReg);
1517 }
1518 oatFreeTemp(cUnit, rlIndex.lowReg);
1519 rlResult = oatEvalLoc(cUnit, rlDest, regClass, true);
1520
buzbee239c4e72012-03-16 08:42:29 -07001521 if (needsRangeCheck) {
1522 // TODO: change kCondCS to a more meaningful name, is the sense of
1523 // carry-set/clear flipped?
1524 genRegRegCheck(cUnit, kCondCs, rlIndex.lowReg, regLen, mir,
1525 kThrowArrayBounds);
1526 oatFreeTemp(cUnit, regLen);
1527 }
buzbee31a4a6f2012-02-28 15:36:15 -08001528 loadPair(cUnit, regPtr, rlResult.lowReg, rlResult.highReg);
1529
1530 oatFreeTemp(cUnit, regPtr);
1531 storeValueWide(cUnit, rlDest, rlResult);
1532 } else {
1533 rlResult = oatEvalLoc(cUnit, rlDest, regClass, true);
1534
buzbee239c4e72012-03-16 08:42:29 -07001535 if (needsRangeCheck) {
1536 // TODO: change kCondCS to a more meaningful name, is the sense of
1537 // carry-set/clear flipped?
1538 genRegRegCheck(cUnit, kCondCs, rlIndex.lowReg, regLen, mir,
1539 kThrowArrayBounds);
1540 oatFreeTemp(cUnit, regLen);
1541 }
buzbee31a4a6f2012-02-28 15:36:15 -08001542 loadBaseIndexed(cUnit, regPtr, rlIndex.lowReg, rlResult.lowReg,
1543 scale, size);
1544
1545 oatFreeTemp(cUnit, regPtr);
1546 storeValue(cUnit, rlDest, rlResult);
1547 }
Ian Rogersb5d09b22012-03-06 22:14:17 -08001548#endif
buzbee31a4a6f2012-02-28 15:36:15 -08001549}
1550
1551/*
1552 * Generate array store
1553 *
1554 */
1555void genArrayPut(CompilationUnit* cUnit, MIR* mir, OpSize size,
1556 RegLocation rlArray, RegLocation rlIndex,
1557 RegLocation rlSrc, int scale)
1558{
1559 RegisterClass regClass = oatRegClassBySize(size);
1560 int lenOffset = Array::LengthOffset().Int32Value();
1561 int dataOffset;
1562
1563 if (size == kLong || size == kDouble) {
1564 dataOffset = Array::DataOffset(sizeof(int64_t)).Int32Value();
1565 } else {
1566 dataOffset = Array::DataOffset(sizeof(int32_t)).Int32Value();
1567 }
1568
buzbee31a4a6f2012-02-28 15:36:15 -08001569 rlArray = loadValue(cUnit, rlArray, kCoreReg);
1570 rlIndex = loadValue(cUnit, rlIndex, kCoreReg);
Ian Rogersb41b33b2012-03-20 14:22:54 -07001571#if !defined(TARGET_X86)
1572 int regPtr;
buzbee31a4a6f2012-02-28 15:36:15 -08001573 if (oatIsTemp(cUnit, rlArray.lowReg)) {
1574 oatClobber(cUnit, rlArray.lowReg);
1575 regPtr = rlArray.lowReg;
1576 } else {
1577 regPtr = oatAllocTemp(cUnit);
buzbee82488f52012-03-02 08:20:26 -08001578 opRegCopy(cUnit, regPtr, rlArray.lowReg);
buzbee31a4a6f2012-02-28 15:36:15 -08001579 }
Ian Rogersb41b33b2012-03-20 14:22:54 -07001580#endif
buzbee31a4a6f2012-02-28 15:36:15 -08001581
1582 /* null object? */
1583 genNullCheck(cUnit, rlArray.sRegLow, rlArray.lowReg, mir);
1584
Ian Rogersb41b33b2012-03-20 14:22:54 -07001585#if defined(TARGET_X86)
1586 if (!(mir->optimizationFlags & MIR_IGNORE_RANGE_CHECK)) {
1587 /* if (rlIndex >= [rlArray + lenOffset]) goto kThrowArrayBounds */
1588 genRegMemCheck(cUnit, kCondUge, rlIndex.lowReg, rlArray.lowReg,
1589 lenOffset, mir, kThrowArrayBounds);
1590 }
Ian Rogersc6f3bb82012-03-21 20:40:33 -07001591 if ((size == kLong) || (size == kDouble)) {
1592 rlSrc = loadValueWide(cUnit, rlSrc, regClass);
1593 } else {
1594 rlSrc = loadValue(cUnit, rlSrc, regClass);
1595 }
Ian Rogersb41b33b2012-03-20 14:22:54 -07001596 storeBaseIndexedDisp(cUnit, NULL, rlArray.lowReg, rlIndex.lowReg, scale, dataOffset,
1597 rlSrc.lowReg, rlSrc.highReg, size, INVALID_SREG);
1598#else
buzbee239c4e72012-03-16 08:42:29 -07001599 bool needsRangeCheck = (!(mir->optimizationFlags & MIR_IGNORE_RANGE_CHECK));
1600 int regLen = INVALID_REG;
1601 if (needsRangeCheck) {
1602 regLen = oatAllocTemp(cUnit);
buzbee31a4a6f2012-02-28 15:36:15 -08001603 //NOTE: max live temps(4) here.
1604 /* Get len */
1605 loadWordDisp(cUnit, rlArray.lowReg, lenOffset, regLen);
buzbee31a4a6f2012-02-28 15:36:15 -08001606 }
buzbee239c4e72012-03-16 08:42:29 -07001607 /* regPtr -> array data */
1608 opRegImm(cUnit, kOpAdd, regPtr, dataOffset);
buzbee31a4a6f2012-02-28 15:36:15 -08001609 /* at this point, regPtr points to array, 2 live temps */
1610 if ((size == kLong) || (size == kDouble)) {
1611 //TUNING: specific wide routine that can handle fp regs
1612 if (scale) {
1613 int rNewIndex = oatAllocTemp(cUnit);
1614 opRegRegImm(cUnit, kOpLsl, rNewIndex, rlIndex.lowReg, scale);
1615 opRegReg(cUnit, kOpAdd, regPtr, rNewIndex);
1616 oatFreeTemp(cUnit, rNewIndex);
1617 } else {
1618 opRegReg(cUnit, kOpAdd, regPtr, rlIndex.lowReg);
1619 }
1620 rlSrc = loadValueWide(cUnit, rlSrc, regClass);
1621
buzbee239c4e72012-03-16 08:42:29 -07001622 if (needsRangeCheck) {
1623 genRegRegCheck(cUnit, kCondCs, rlIndex.lowReg, regLen, mir,
1624 kThrowArrayBounds);
1625 oatFreeTemp(cUnit, regLen);
1626 }
1627
buzbee31a4a6f2012-02-28 15:36:15 -08001628 storePair(cUnit, regPtr, rlSrc.lowReg, rlSrc.highReg);
1629
1630 oatFreeTemp(cUnit, regPtr);
1631 } else {
1632 rlSrc = loadValue(cUnit, rlSrc, regClass);
buzbee239c4e72012-03-16 08:42:29 -07001633 if (needsRangeCheck) {
1634 genRegRegCheck(cUnit, kCondCs, rlIndex.lowReg, regLen, mir,
1635 kThrowArrayBounds);
1636 oatFreeTemp(cUnit, regLen);
1637 }
buzbee31a4a6f2012-02-28 15:36:15 -08001638 storeBaseIndexed(cUnit, regPtr, rlIndex.lowReg, rlSrc.lowReg,
1639 scale, size);
1640 }
Ian Rogersb41b33b2012-03-20 14:22:54 -07001641#endif
buzbee31a4a6f2012-02-28 15:36:15 -08001642}
1643
1644void genLong3Addr(CompilationUnit* cUnit, MIR* mir, OpKind firstOp,
1645 OpKind secondOp, RegLocation rlDest,
1646 RegLocation rlSrc1, RegLocation rlSrc2)
1647{
1648 RegLocation rlResult;
1649#if defined(TARGET_ARM)
1650 /*
1651 * NOTE: This is the one place in the code in which we might have
1652 * as many as six live temporary registers. There are 5 in the normal
1653 * set for Arm. Until we have spill capabilities, temporarily add
1654 * lr to the temp set. It is safe to do this locally, but note that
1655 * lr is used explicitly elsewhere in the code generator and cannot
1656 * normally be used as a general temp register.
1657 */
1658 oatMarkTemp(cUnit, rLR); // Add lr to the temp pool
1659 oatFreeTemp(cUnit, rLR); // and make it available
1660#endif
1661 rlSrc1 = loadValueWide(cUnit, rlSrc1, kCoreReg);
1662 rlSrc2 = loadValueWide(cUnit, rlSrc2, kCoreReg);
1663 rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
1664 // The longs may overlap - use intermediate temp if so
1665 if (rlResult.lowReg == rlSrc1.highReg) {
1666 int tReg = oatAllocTemp(cUnit);
buzbee82488f52012-03-02 08:20:26 -08001667 opRegCopy(cUnit, tReg, rlSrc1.highReg);
buzbee31a4a6f2012-02-28 15:36:15 -08001668 opRegRegReg(cUnit, firstOp, rlResult.lowReg, rlSrc1.lowReg,
1669 rlSrc2.lowReg);
1670 opRegRegReg(cUnit, secondOp, rlResult.highReg, tReg,
1671 rlSrc2.highReg);
1672 oatFreeTemp(cUnit, tReg);
1673 } else {
1674 opRegRegReg(cUnit, firstOp, rlResult.lowReg, rlSrc1.lowReg,
1675 rlSrc2.lowReg);
1676 opRegRegReg(cUnit, secondOp, rlResult.highReg, rlSrc1.highReg,
1677 rlSrc2.highReg);
1678 }
1679 /*
1680 * NOTE: If rlDest refers to a frame variable in a large frame, the
1681 * following storeValueWide might need to allocate a temp register.
1682 * To further work around the lack of a spill capability, explicitly
1683 * free any temps from rlSrc1 & rlSrc2 that aren't still live in rlResult.
1684 * Remove when spill is functional.
1685 */
1686 freeRegLocTemps(cUnit, rlResult, rlSrc1);
1687 freeRegLocTemps(cUnit, rlResult, rlSrc2);
1688 storeValueWide(cUnit, rlDest, rlResult);
1689#if defined(TARGET_ARM)
1690 oatClobber(cUnit, rLR);
1691 oatUnmarkTemp(cUnit, rLR); // Remove lr from the temp pool
1692#endif
1693}
1694
1695
1696bool genShiftOpLong(CompilationUnit* cUnit, MIR* mir, RegLocation rlDest,
1697 RegLocation rlSrc1, RegLocation rlShift)
1698{
1699 int funcOffset;
1700
1701 switch( mir->dalvikInsn.opcode) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001702 case Instruction::SHL_LONG:
1703 case Instruction::SHL_LONG_2ADDR:
buzbee31a4a6f2012-02-28 15:36:15 -08001704 funcOffset = OFFSETOF_MEMBER(Thread, pShlLong);
1705 break;
Elliott Hughesadb8c672012-03-06 16:49:32 -08001706 case Instruction::SHR_LONG:
1707 case Instruction::SHR_LONG_2ADDR:
buzbee31a4a6f2012-02-28 15:36:15 -08001708 funcOffset = OFFSETOF_MEMBER(Thread, pShrLong);
1709 break;
Elliott Hughesadb8c672012-03-06 16:49:32 -08001710 case Instruction::USHR_LONG:
1711 case Instruction::USHR_LONG_2ADDR:
buzbee31a4a6f2012-02-28 15:36:15 -08001712 funcOffset = OFFSETOF_MEMBER(Thread, pUshrLong);
1713 break;
1714 default:
1715 LOG(FATAL) << "Unexpected case";
1716 return true;
1717 }
1718 oatFlushAllRegs(cUnit); /* Send everything to home location */
Ian Rogersab2b55d2012-03-18 00:06:11 -07001719 callRuntimeHelperRegLocationRegLocation(cUnit, funcOffset, rlSrc1, rlShift);
Ian Rogersf7d9ad32012-03-13 18:45:39 -07001720 RegLocation rlResult = oatGetReturnWide(cUnit, false);
buzbee31a4a6f2012-02-28 15:36:15 -08001721 storeValueWide(cUnit, rlDest, rlResult);
1722 return false;
1723}
1724
1725
1726bool genArithOpInt(CompilationUnit* cUnit, MIR* mir, RegLocation rlDest,
1727 RegLocation rlSrc1, RegLocation rlSrc2)
1728{
1729 OpKind op = kOpBkpt;
1730 bool callOut = false;
1731 bool checkZero = false;
1732 bool unary = false;
buzbee31a4a6f2012-02-28 15:36:15 -08001733 RegLocation rlResult;
1734 bool shiftOp = false;
Ian Rogersb5d09b22012-03-06 22:14:17 -08001735 int funcOffset;
1736 int retReg = rRET0;
buzbee31a4a6f2012-02-28 15:36:15 -08001737 switch (mir->dalvikInsn.opcode) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001738 case Instruction::NEG_INT:
buzbee31a4a6f2012-02-28 15:36:15 -08001739 op = kOpNeg;
1740 unary = true;
1741 break;
Elliott Hughesadb8c672012-03-06 16:49:32 -08001742 case Instruction::NOT_INT:
buzbee31a4a6f2012-02-28 15:36:15 -08001743 op = kOpMvn;
1744 unary = true;
1745 break;
Elliott Hughesadb8c672012-03-06 16:49:32 -08001746 case Instruction::ADD_INT:
1747 case Instruction::ADD_INT_2ADDR:
buzbee31a4a6f2012-02-28 15:36:15 -08001748 op = kOpAdd;
1749 break;
Elliott Hughesadb8c672012-03-06 16:49:32 -08001750 case Instruction::SUB_INT:
1751 case Instruction::SUB_INT_2ADDR:
buzbee31a4a6f2012-02-28 15:36:15 -08001752 op = kOpSub;
1753 break;
Elliott Hughesadb8c672012-03-06 16:49:32 -08001754 case Instruction::MUL_INT:
1755 case Instruction::MUL_INT_2ADDR:
buzbee31a4a6f2012-02-28 15:36:15 -08001756 op = kOpMul;
1757 break;
Elliott Hughesadb8c672012-03-06 16:49:32 -08001758 case Instruction::DIV_INT:
1759 case Instruction::DIV_INT_2ADDR:
buzbee31a4a6f2012-02-28 15:36:15 -08001760 checkZero = true;
Ian Rogersb5d09b22012-03-06 22:14:17 -08001761 op = kOpDiv;
1762 callOut = true;
buzbee31a4a6f2012-02-28 15:36:15 -08001763 funcOffset = OFFSETOF_MEMBER(Thread, pIdiv);
1764 retReg = rRET0;
1765 break;
buzbee5de34942012-03-01 14:51:57 -08001766 /* NOTE: returns in rARG1 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08001767 case Instruction::REM_INT:
1768 case Instruction::REM_INT_2ADDR:
buzbee31a4a6f2012-02-28 15:36:15 -08001769 checkZero = true;
Ian Rogersb5d09b22012-03-06 22:14:17 -08001770 op = kOpRem;
1771 callOut = true;
buzbee31a4a6f2012-02-28 15:36:15 -08001772 funcOffset = OFFSETOF_MEMBER(Thread, pIdivmod);
1773 retReg = rRET1;
1774 break;
Elliott Hughesadb8c672012-03-06 16:49:32 -08001775 case Instruction::AND_INT:
1776 case Instruction::AND_INT_2ADDR:
buzbee31a4a6f2012-02-28 15:36:15 -08001777 op = kOpAnd;
1778 break;
Elliott Hughesadb8c672012-03-06 16:49:32 -08001779 case Instruction::OR_INT:
1780 case Instruction::OR_INT_2ADDR:
buzbee31a4a6f2012-02-28 15:36:15 -08001781 op = kOpOr;
1782 break;
Elliott Hughesadb8c672012-03-06 16:49:32 -08001783 case Instruction::XOR_INT:
1784 case Instruction::XOR_INT_2ADDR:
buzbee31a4a6f2012-02-28 15:36:15 -08001785 op = kOpXor;
1786 break;
Elliott Hughesadb8c672012-03-06 16:49:32 -08001787 case Instruction::SHL_INT:
1788 case Instruction::SHL_INT_2ADDR:
buzbee31a4a6f2012-02-28 15:36:15 -08001789 shiftOp = true;
1790 op = kOpLsl;
1791 break;
Elliott Hughesadb8c672012-03-06 16:49:32 -08001792 case Instruction::SHR_INT:
1793 case Instruction::SHR_INT_2ADDR:
buzbee31a4a6f2012-02-28 15:36:15 -08001794 shiftOp = true;
1795 op = kOpAsr;
1796 break;
Elliott Hughesadb8c672012-03-06 16:49:32 -08001797 case Instruction::USHR_INT:
1798 case Instruction::USHR_INT_2ADDR:
buzbee31a4a6f2012-02-28 15:36:15 -08001799 shiftOp = true;
1800 op = kOpLsr;
1801 break;
1802 default:
1803 LOG(FATAL) << "Invalid word arith op: " <<
1804 (int)mir->dalvikInsn.opcode;
1805 }
1806 if (!callOut) {
1807 rlSrc1 = loadValue(cUnit, rlSrc1, kCoreReg);
1808 if (unary) {
1809 rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
1810 opRegReg(cUnit, op, rlResult.lowReg,
1811 rlSrc1.lowReg);
1812 } else {
1813 rlSrc2 = loadValue(cUnit, rlSrc2, kCoreReg);
Ian Rogersb5d09b22012-03-06 22:14:17 -08001814#if defined(TARGET_X86)
1815 rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
1816 opRegRegReg(cUnit, op, rlResult.lowReg,
1817 rlSrc1.lowReg, rlSrc2.lowReg);
1818#else
buzbee31a4a6f2012-02-28 15:36:15 -08001819 if (shiftOp) {
1820 int tReg = oatAllocTemp(cUnit);
1821 opRegRegImm(cUnit, kOpAnd, tReg, rlSrc2.lowReg, 31);
1822 rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
1823 opRegRegReg(cUnit, op, rlResult.lowReg,
1824 rlSrc1.lowReg, tReg);
1825 oatFreeTemp(cUnit, tReg);
1826 } else {
1827 rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
1828 opRegRegReg(cUnit, op, rlResult.lowReg,
1829 rlSrc1.lowReg, rlSrc2.lowReg);
1830 }
Ian Rogersb5d09b22012-03-06 22:14:17 -08001831#endif
buzbee31a4a6f2012-02-28 15:36:15 -08001832 }
1833 storeValue(cUnit, rlDest, rlResult);
1834 } else {
1835 RegLocation rlResult;
1836 oatFlushAllRegs(cUnit); /* Send everything to home location */
1837 loadValueDirectFixed(cUnit, rlSrc2, rRET1);
Ian Rogers6cbb2bd2012-03-16 13:45:30 -07001838#if !defined(TARGET_X86)
buzbee31a4a6f2012-02-28 15:36:15 -08001839 int rTgt = loadHelper(cUnit, funcOffset);
Ian Rogersab2b55d2012-03-18 00:06:11 -07001840#endif
buzbee31a4a6f2012-02-28 15:36:15 -08001841 loadValueDirectFixed(cUnit, rlSrc1, rARG0);
1842 if (checkZero) {
1843 genImmedCheck(cUnit, kCondEq, rARG1, 0, mir, kThrowDivZero);
1844 }
Ian Rogersab2b55d2012-03-18 00:06:11 -07001845#if !defined(TARGET_X86)
1846 opReg(cUnit, kOpBlx, rTgt);
Ian Rogers6cbb2bd2012-03-16 13:45:30 -07001847 oatFreeTemp(cUnit, rTgt);
1848#else
Ian Rogersab2b55d2012-03-18 00:06:11 -07001849 opThreadMem(cUnit, kOpBlx, funcOffset);
Ian Rogers6cbb2bd2012-03-16 13:45:30 -07001850#endif
buzbee31a4a6f2012-02-28 15:36:15 -08001851 if (retReg == rRET0)
Ian Rogersf7d9ad32012-03-13 18:45:39 -07001852 rlResult = oatGetReturn(cUnit, false);
buzbee31a4a6f2012-02-28 15:36:15 -08001853 else
1854 rlResult = oatGetReturnAlt(cUnit);
1855 storeValue(cUnit, rlDest, rlResult);
1856 }
1857 return false;
1858}
1859
1860/*
1861 * The following are the first-level codegen routines that analyze the format
1862 * of each bytecode then either dispatch special purpose codegen routines
1863 * or produce corresponding Thumb instructions directly.
1864 */
1865
1866bool isPowerOfTwo(int x)
1867{
1868 return (x & (x - 1)) == 0;
1869}
1870
1871// Returns true if no more than two bits are set in 'x'.
1872bool isPopCountLE2(unsigned int x)
1873{
1874 x &= x - 1;
1875 return (x & (x - 1)) == 0;
1876}
1877
1878// Returns the index of the lowest set bit in 'x'.
1879int lowestSetBit(unsigned int x) {
1880 int bit_posn = 0;
1881 while ((x & 0xf) == 0) {
1882 bit_posn += 4;
1883 x >>= 4;
1884 }
1885 while ((x & 1) == 0) {
1886 bit_posn++;
1887 x >>= 1;
1888 }
1889 return bit_posn;
1890}
1891
1892// Returns true if it added instructions to 'cUnit' to divide 'rlSrc' by 'lit'
1893// and store the result in 'rlDest'.
Elliott Hughesadb8c672012-03-06 16:49:32 -08001894bool handleEasyDivide(CompilationUnit* cUnit, Instruction::Code dalvikOpcode,
buzbee31a4a6f2012-02-28 15:36:15 -08001895 RegLocation rlSrc, RegLocation rlDest, int lit)
1896{
1897 if (lit < 2 || !isPowerOfTwo(lit)) {
1898 return false;
1899 }
1900 int k = lowestSetBit(lit);
1901 if (k >= 30) {
1902 // Avoid special cases.
1903 return false;
1904 }
Elliott Hughesadb8c672012-03-06 16:49:32 -08001905 bool div = (dalvikOpcode == Instruction::DIV_INT_LIT8 ||
1906 dalvikOpcode == Instruction::DIV_INT_LIT16);
buzbee31a4a6f2012-02-28 15:36:15 -08001907 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
1908 RegLocation rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
1909 if (div) {
1910 int tReg = oatAllocTemp(cUnit);
1911 if (lit == 2) {
1912 // Division by 2 is by far the most common division by constant.
1913 opRegRegImm(cUnit, kOpLsr, tReg, rlSrc.lowReg, 32 - k);
1914 opRegRegReg(cUnit, kOpAdd, tReg, tReg, rlSrc.lowReg);
1915 opRegRegImm(cUnit, kOpAsr, rlResult.lowReg, tReg, k);
1916 } else {
1917 opRegRegImm(cUnit, kOpAsr, tReg, rlSrc.lowReg, 31);
1918 opRegRegImm(cUnit, kOpLsr, tReg, tReg, 32 - k);
1919 opRegRegReg(cUnit, kOpAdd, tReg, tReg, rlSrc.lowReg);
1920 opRegRegImm(cUnit, kOpAsr, rlResult.lowReg, tReg, k);
1921 }
1922 } else {
buzbee31a4a6f2012-02-28 15:36:15 -08001923 int tReg1 = oatAllocTemp(cUnit);
1924 int tReg2 = oatAllocTemp(cUnit);
1925 if (lit == 2) {
1926 opRegRegImm(cUnit, kOpLsr, tReg1, rlSrc.lowReg, 32 - k);
1927 opRegRegReg(cUnit, kOpAdd, tReg2, tReg1, rlSrc.lowReg);
Ian Rogersb41b33b2012-03-20 14:22:54 -07001928 opRegRegImm(cUnit, kOpAnd, tReg2, tReg2, lit -1);
buzbee31a4a6f2012-02-28 15:36:15 -08001929 opRegRegReg(cUnit, kOpSub, rlResult.lowReg, tReg2, tReg1);
1930 } else {
1931 opRegRegImm(cUnit, kOpAsr, tReg1, rlSrc.lowReg, 31);
1932 opRegRegImm(cUnit, kOpLsr, tReg1, tReg1, 32 - k);
1933 opRegRegReg(cUnit, kOpAdd, tReg2, tReg1, rlSrc.lowReg);
Ian Rogersb41b33b2012-03-20 14:22:54 -07001934 opRegRegImm(cUnit, kOpAnd, tReg2, tReg2, lit - 1);
buzbee31a4a6f2012-02-28 15:36:15 -08001935 opRegRegReg(cUnit, kOpSub, rlResult.lowReg, tReg2, tReg1);
1936 }
1937 }
1938 storeValue(cUnit, rlDest, rlResult);
1939 return true;
1940}
1941
1942void genMultiplyByTwoBitMultiplier(CompilationUnit* cUnit, RegLocation rlSrc,
1943 RegLocation rlResult, int lit,
1944 int firstBit, int secondBit)
1945{
buzbee0398c422012-03-02 15:22:47 -08001946#if defined(TARGET_ARM)
buzbee31a4a6f2012-02-28 15:36:15 -08001947 opRegRegRegShift(cUnit, kOpAdd, rlResult.lowReg, rlSrc.lowReg, rlSrc.lowReg,
1948 encodeShift(kArmLsl, secondBit - firstBit));
buzbee0398c422012-03-02 15:22:47 -08001949#else
1950 int tReg = oatAllocTemp(cUnit);
1951 opRegRegImm(cUnit, kOpLsl, tReg, rlSrc.lowReg, secondBit - firstBit);
1952 opRegRegReg(cUnit, kOpAdd, rlResult.lowReg, rlSrc.lowReg, tReg);
1953 oatFreeTemp(cUnit, tReg);
buzbee5de34942012-03-01 14:51:57 -08001954#endif
buzbee31a4a6f2012-02-28 15:36:15 -08001955 if (firstBit != 0) {
1956 opRegRegImm(cUnit, kOpLsl, rlResult.lowReg, rlResult.lowReg, firstBit);
1957 }
1958}
1959
1960// Returns true if it added instructions to 'cUnit' to multiply 'rlSrc' by 'lit'
1961// and store the result in 'rlDest'.
1962bool handleEasyMultiply(CompilationUnit* cUnit, RegLocation rlSrc,
1963 RegLocation rlDest, int lit)
1964{
1965 // Can we simplify this multiplication?
1966 bool powerOfTwo = false;
1967 bool popCountLE2 = false;
1968 bool powerOfTwoMinusOne = false;
1969 if (lit < 2) {
1970 // Avoid special cases.
1971 return false;
1972 } else if (isPowerOfTwo(lit)) {
1973 powerOfTwo = true;
1974 } else if (isPopCountLE2(lit)) {
1975 popCountLE2 = true;
1976 } else if (isPowerOfTwo(lit + 1)) {
1977 powerOfTwoMinusOne = true;
1978 } else {
1979 return false;
1980 }
1981 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
1982 RegLocation rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
1983 if (powerOfTwo) {
1984 // Shift.
1985 opRegRegImm(cUnit, kOpLsl, rlResult.lowReg, rlSrc.lowReg,
1986 lowestSetBit(lit));
1987 } else if (popCountLE2) {
1988 // Shift and add and shift.
1989 int firstBit = lowestSetBit(lit);
1990 int secondBit = lowestSetBit(lit ^ (1 << firstBit));
1991 genMultiplyByTwoBitMultiplier(cUnit, rlSrc, rlResult, lit,
1992 firstBit, secondBit);
1993 } else {
1994 // Reverse subtract: (src << (shift + 1)) - src.
1995 DCHECK(powerOfTwoMinusOne);
1996 // TUNING: rsb dst, src, src lsl#lowestSetBit(lit + 1)
1997 int tReg = oatAllocTemp(cUnit);
1998 opRegRegImm(cUnit, kOpLsl, tReg, rlSrc.lowReg, lowestSetBit(lit + 1));
1999 opRegRegReg(cUnit, kOpSub, rlResult.lowReg, tReg, rlSrc.lowReg);
2000 }
2001 storeValue(cUnit, rlDest, rlResult);
2002 return true;
2003}
2004
2005bool genArithOpIntLit(CompilationUnit* cUnit, MIR* mir, RegLocation rlDest,
2006 RegLocation rlSrc, int lit)
2007{
Elliott Hughesadb8c672012-03-06 16:49:32 -08002008 Instruction::Code dalvikOpcode = mir->dalvikInsn.opcode;
buzbee31a4a6f2012-02-28 15:36:15 -08002009 RegLocation rlResult;
2010 OpKind op = (OpKind)0; /* Make gcc happy */
2011 int shiftOp = false;
2012 bool isDiv = false;
2013 int funcOffset;
buzbee31a4a6f2012-02-28 15:36:15 -08002014
2015 switch (dalvikOpcode) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002016 case Instruction::RSUB_INT_LIT8:
2017 case Instruction::RSUB_INT: {
buzbee31a4a6f2012-02-28 15:36:15 -08002018 int tReg;
2019 //TUNING: add support for use of Arm rsub op
2020 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
2021 tReg = oatAllocTemp(cUnit);
2022 loadConstant(cUnit, tReg, lit);
2023 rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
2024 opRegRegReg(cUnit, kOpSub, rlResult.lowReg,
2025 tReg, rlSrc.lowReg);
2026 storeValue(cUnit, rlDest, rlResult);
2027 return false;
2028 break;
2029 }
2030
Elliott Hughesadb8c672012-03-06 16:49:32 -08002031 case Instruction::ADD_INT_LIT8:
2032 case Instruction::ADD_INT_LIT16:
buzbee31a4a6f2012-02-28 15:36:15 -08002033 op = kOpAdd;
2034 break;
Elliott Hughesadb8c672012-03-06 16:49:32 -08002035 case Instruction::MUL_INT_LIT8:
2036 case Instruction::MUL_INT_LIT16: {
buzbee31a4a6f2012-02-28 15:36:15 -08002037 if (handleEasyMultiply(cUnit, rlSrc, rlDest, lit)) {
2038 return false;
2039 }
2040 op = kOpMul;
2041 break;
2042 }
Elliott Hughesadb8c672012-03-06 16:49:32 -08002043 case Instruction::AND_INT_LIT8:
2044 case Instruction::AND_INT_LIT16:
buzbee31a4a6f2012-02-28 15:36:15 -08002045 op = kOpAnd;
2046 break;
Elliott Hughesadb8c672012-03-06 16:49:32 -08002047 case Instruction::OR_INT_LIT8:
2048 case Instruction::OR_INT_LIT16:
buzbee31a4a6f2012-02-28 15:36:15 -08002049 op = kOpOr;
2050 break;
Elliott Hughesadb8c672012-03-06 16:49:32 -08002051 case Instruction::XOR_INT_LIT8:
2052 case Instruction::XOR_INT_LIT16:
buzbee31a4a6f2012-02-28 15:36:15 -08002053 op = kOpXor;
2054 break;
Elliott Hughesadb8c672012-03-06 16:49:32 -08002055 case Instruction::SHL_INT_LIT8:
buzbee31a4a6f2012-02-28 15:36:15 -08002056 lit &= 31;
2057 shiftOp = true;
2058 op = kOpLsl;
2059 break;
Elliott Hughesadb8c672012-03-06 16:49:32 -08002060 case Instruction::SHR_INT_LIT8:
buzbee31a4a6f2012-02-28 15:36:15 -08002061 lit &= 31;
2062 shiftOp = true;
2063 op = kOpAsr;
2064 break;
Elliott Hughesadb8c672012-03-06 16:49:32 -08002065 case Instruction::USHR_INT_LIT8:
buzbee31a4a6f2012-02-28 15:36:15 -08002066 lit &= 31;
2067 shiftOp = true;
2068 op = kOpLsr;
2069 break;
2070
Elliott Hughesadb8c672012-03-06 16:49:32 -08002071 case Instruction::DIV_INT_LIT8:
2072 case Instruction::DIV_INT_LIT16:
2073 case Instruction::REM_INT_LIT8:
2074 case Instruction::REM_INT_LIT16:
buzbee31a4a6f2012-02-28 15:36:15 -08002075 if (lit == 0) {
2076 genImmedCheck(cUnit, kCondAl, 0, 0, mir, kThrowDivZero);
2077 return false;
2078 }
2079 if (handleEasyDivide(cUnit, dalvikOpcode, rlSrc, rlDest, lit)) {
2080 return false;
2081 }
2082 oatFlushAllRegs(cUnit); /* Everything to home location */
2083 loadValueDirectFixed(cUnit, rlSrc, rARG0);
2084 oatClobber(cUnit, rARG0);
Elliott Hughesadb8c672012-03-06 16:49:32 -08002085 if ((dalvikOpcode == Instruction::DIV_INT_LIT8) ||
2086 (dalvikOpcode == Instruction::DIV_INT_LIT16)) {
buzbee31a4a6f2012-02-28 15:36:15 -08002087 funcOffset = OFFSETOF_MEMBER(Thread, pIdiv);
2088 isDiv = true;
2089 } else {
2090 funcOffset = OFFSETOF_MEMBER(Thread, pIdivmod);
2091 isDiv = false;
2092 }
Ian Rogersab2b55d2012-03-18 00:06:11 -07002093 callRuntimeHelperRegImm(cUnit, funcOffset, rARG0, lit);
buzbee31a4a6f2012-02-28 15:36:15 -08002094 if (isDiv)
Ian Rogersf7d9ad32012-03-13 18:45:39 -07002095 rlResult = oatGetReturn(cUnit, false);
buzbee31a4a6f2012-02-28 15:36:15 -08002096 else
2097 rlResult = oatGetReturnAlt(cUnit);
2098 storeValue(cUnit, rlDest, rlResult);
2099 return false;
2100 break;
2101 default:
2102 return true;
2103 }
2104 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
2105 rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
2106 // Avoid shifts by literal 0 - no support in Thumb. Change to copy
2107 if (shiftOp && (lit == 0)) {
buzbee82488f52012-03-02 08:20:26 -08002108 opRegCopy(cUnit, rlResult.lowReg, rlSrc.lowReg);
buzbee31a4a6f2012-02-28 15:36:15 -08002109 } else {
2110 opRegRegImm(cUnit, op, rlResult.lowReg, rlSrc.lowReg, lit);
2111 }
2112 storeValue(cUnit, rlDest, rlResult);
2113 return false;
2114}
2115
2116bool genArithOpLong(CompilationUnit* cUnit, MIR* mir, RegLocation rlDest,
2117 RegLocation rlSrc1, RegLocation rlSrc2)
2118{
2119 RegLocation rlResult;
2120 OpKind firstOp = kOpBkpt;
2121 OpKind secondOp = kOpBkpt;
2122 bool callOut = false;
2123 bool checkZero = false;
2124 int funcOffset;
2125 int retReg = rRET0;
2126
2127 switch (mir->dalvikInsn.opcode) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002128 case Instruction::NOT_LONG:
buzbee31a4a6f2012-02-28 15:36:15 -08002129 rlSrc2 = loadValueWide(cUnit, rlSrc2, kCoreReg);
2130 rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
2131 // Check for destructive overlap
2132 if (rlResult.lowReg == rlSrc2.highReg) {
2133 int tReg = oatAllocTemp(cUnit);
buzbee82488f52012-03-02 08:20:26 -08002134 opRegCopy(cUnit, tReg, rlSrc2.highReg);
buzbee31a4a6f2012-02-28 15:36:15 -08002135 opRegReg(cUnit, kOpMvn, rlResult.lowReg, rlSrc2.lowReg);
2136 opRegReg(cUnit, kOpMvn, rlResult.highReg, tReg);
2137 oatFreeTemp(cUnit, tReg);
2138 } else {
2139 opRegReg(cUnit, kOpMvn, rlResult.lowReg, rlSrc2.lowReg);
2140 opRegReg(cUnit, kOpMvn, rlResult.highReg, rlSrc2.highReg);
2141 }
2142 storeValueWide(cUnit, rlDest, rlResult);
2143 return false;
2144 break;
Elliott Hughesadb8c672012-03-06 16:49:32 -08002145 case Instruction::ADD_LONG:
2146 case Instruction::ADD_LONG_2ADDR:
buzbeec5159d52012-03-03 11:48:39 -08002147#if defined(TARGET_MIPS)
2148 return genAddLong(cUnit, mir, rlDest, rlSrc1, rlSrc2);
Ian Rogersc6f3bb82012-03-21 20:40:33 -07002149#elif defined(TARGET_X86)
2150 callOut = true;
2151 retReg = rRET0;
2152 funcOffset = OFFSETOF_MEMBER(Thread, pLadd);
buzbeec5159d52012-03-03 11:48:39 -08002153#else
buzbee31a4a6f2012-02-28 15:36:15 -08002154 firstOp = kOpAdd;
2155 secondOp = kOpAdc;
2156 break;
buzbeec5159d52012-03-03 11:48:39 -08002157#endif
Elliott Hughesadb8c672012-03-06 16:49:32 -08002158 case Instruction::SUB_LONG:
2159 case Instruction::SUB_LONG_2ADDR:
buzbeec5159d52012-03-03 11:48:39 -08002160#if defined(TARGET_MIPS)
2161 return genSubLong(cUnit, mir, rlDest, rlSrc1, rlSrc2);
Ian Rogersc6f3bb82012-03-21 20:40:33 -07002162#elif defined(TARGET_X86)
2163 callOut = true;
2164 retReg = rRET0;
2165 funcOffset = OFFSETOF_MEMBER(Thread, pLsub);
2166#endif
buzbee31a4a6f2012-02-28 15:36:15 -08002167 firstOp = kOpSub;
2168 secondOp = kOpSbc;
2169 break;
Elliott Hughesadb8c672012-03-06 16:49:32 -08002170 case Instruction::MUL_LONG:
2171 case Instruction::MUL_LONG_2ADDR:
buzbee31a4a6f2012-02-28 15:36:15 -08002172 callOut = true;
2173 retReg = rRET0;
2174 funcOffset = OFFSETOF_MEMBER(Thread, pLmul);
2175 break;
Elliott Hughesadb8c672012-03-06 16:49:32 -08002176 case Instruction::DIV_LONG:
2177 case Instruction::DIV_LONG_2ADDR:
buzbee31a4a6f2012-02-28 15:36:15 -08002178 callOut = true;
2179 checkZero = true;
2180 retReg = rRET0;
2181 funcOffset = OFFSETOF_MEMBER(Thread, pLdivmod);
2182 break;
2183 /* NOTE - result is in rARG2/rARG3 instead of rRET0/rRET1 */
2184 // FIXME: is true, or could be made true, or other targets?
Elliott Hughesadb8c672012-03-06 16:49:32 -08002185 case Instruction::REM_LONG:
2186 case Instruction::REM_LONG_2ADDR:
buzbee31a4a6f2012-02-28 15:36:15 -08002187 callOut = true;
2188 checkZero = true;
2189 funcOffset = OFFSETOF_MEMBER(Thread, pLdivmod);
2190 retReg = rARG2;
2191 break;
Elliott Hughesadb8c672012-03-06 16:49:32 -08002192 case Instruction::AND_LONG_2ADDR:
2193 case Instruction::AND_LONG:
Ian Rogersc6f3bb82012-03-21 20:40:33 -07002194#if defined(TARGET_X86)
2195 callOut = true;
2196 retReg = rRET0;
2197 funcOffset = OFFSETOF_MEMBER(Thread, pLand);
2198#endif
buzbee31a4a6f2012-02-28 15:36:15 -08002199 firstOp = kOpAnd;
2200 secondOp = kOpAnd;
2201 break;
Elliott Hughesadb8c672012-03-06 16:49:32 -08002202 case Instruction::OR_LONG:
2203 case Instruction::OR_LONG_2ADDR:
Ian Rogersc6f3bb82012-03-21 20:40:33 -07002204#if defined(TARGET_X86)
2205 callOut = true;
2206 retReg = rRET0;
2207 funcOffset = OFFSETOF_MEMBER(Thread, pLor);
2208#endif
buzbee31a4a6f2012-02-28 15:36:15 -08002209 firstOp = kOpOr;
2210 secondOp = kOpOr;
2211 break;
Elliott Hughesadb8c672012-03-06 16:49:32 -08002212 case Instruction::XOR_LONG:
2213 case Instruction::XOR_LONG_2ADDR:
Ian Rogersc6f3bb82012-03-21 20:40:33 -07002214#if defined(TARGET_X86)
2215 callOut = true;
2216 retReg = rRET0;
2217 funcOffset = OFFSETOF_MEMBER(Thread, pLxor);
2218#endif
buzbee31a4a6f2012-02-28 15:36:15 -08002219 firstOp = kOpXor;
2220 secondOp = kOpXor;
2221 break;
Elliott Hughesadb8c672012-03-06 16:49:32 -08002222 case Instruction::NEG_LONG: {
buzbeec5159d52012-03-03 11:48:39 -08002223 return genNegLong(cUnit, mir, rlDest, rlSrc2);
buzbee31a4a6f2012-02-28 15:36:15 -08002224 }
2225 default:
2226 LOG(FATAL) << "Invalid long arith op";
2227 }
2228 if (!callOut) {
2229 genLong3Addr(cUnit, mir, firstOp, secondOp, rlDest, rlSrc1, rlSrc2);
2230 } else {
buzbee31a4a6f2012-02-28 15:36:15 -08002231 oatFlushAllRegs(cUnit); /* Send everything to home location */
2232 if (checkZero) {
2233 loadValueDirectWideFixed(cUnit, rlSrc2, rARG2, rARG3);
Ian Rogersc6f3bb82012-03-21 20:40:33 -07002234#if !defined(TARGET_X86)
Ian Rogersab2b55d2012-03-18 00:06:11 -07002235 int rTgt = loadHelper(cUnit, funcOffset);
Ian Rogers6cbb2bd2012-03-16 13:45:30 -07002236#endif
buzbee31a4a6f2012-02-28 15:36:15 -08002237 int tReg = oatAllocTemp(cUnit);
2238#if defined(TARGET_ARM)
2239 newLIR4(cUnit, kThumb2OrrRRRs, tReg, rARG2, rARG3, 0);
2240 oatFreeTemp(cUnit, tReg);
2241 genCheck(cUnit, kCondEq, mir, kThrowDivZero);
2242#else
2243 opRegRegReg(cUnit, kOpOr, tReg, rARG2, rARG3);
Ian Rogersb5d09b22012-03-06 22:14:17 -08002244#endif
buzbee5de34942012-03-01 14:51:57 -08002245 genImmedCheck(cUnit, kCondEq, tReg, 0, mir, kThrowDivZero);
buzbee31a4a6f2012-02-28 15:36:15 -08002246 oatFreeTemp(cUnit, tReg);
Ian Rogersc6f3bb82012-03-21 20:40:33 -07002247 loadValueDirectWideFixed(cUnit, rlSrc1, rARG0, rARG1);
Ian Rogersab2b55d2012-03-18 00:06:11 -07002248#if !defined(TARGET_X86)
2249 opReg(cUnit, kOpBlx, rTgt);
2250 oatFreeTemp(cUnit, rTgt);
Ian Rogersc6f3bb82012-03-21 20:40:33 -07002251#else
2252 opThreadMem(cUnit, kOpBlx, funcOffset);
Ian Rogersb5d09b22012-03-06 22:14:17 -08002253#endif
Ian Rogersab2b55d2012-03-18 00:06:11 -07002254 } else {
2255 callRuntimeHelperRegLocationRegLocation(cUnit, funcOffset,
2256 rlSrc1, rlSrc2);
buzbee31a4a6f2012-02-28 15:36:15 -08002257 }
buzbee31a4a6f2012-02-28 15:36:15 -08002258 // Adjust return regs in to handle case of rem returning rARG2/rARG3
2259 if (retReg == rRET0)
Ian Rogersf7d9ad32012-03-13 18:45:39 -07002260 rlResult = oatGetReturnWide(cUnit, false);
buzbee31a4a6f2012-02-28 15:36:15 -08002261 else
2262 rlResult = oatGetReturnWideAlt(cUnit);
2263 storeValueWide(cUnit, rlDest, rlResult);
2264 }
2265 return false;
2266}
2267
2268bool genConversionCall(CompilationUnit* cUnit, MIR* mir, int funcOffset,
2269 int srcSize, int tgtSize)
2270{
2271 /*
2272 * Don't optimize the register usage since it calls out to support
2273 * functions
2274 */
2275 RegLocation rlSrc;
2276 RegLocation rlDest;
2277 oatFlushAllRegs(cUnit); /* Send everything to home location */
buzbee31a4a6f2012-02-28 15:36:15 -08002278 if (srcSize == 1) {
2279 rlSrc = oatGetSrc(cUnit, mir, 0);
2280 loadValueDirectFixed(cUnit, rlSrc, rARG0);
2281 } else {
2282 rlSrc = oatGetSrcWide(cUnit, mir, 0, 1);
2283 loadValueDirectWideFixed(cUnit, rlSrc, rARG0, rARG1);
2284 }
Ian Rogersab2b55d2012-03-18 00:06:11 -07002285 callRuntimeHelperRegLocation(cUnit, funcOffset, rlSrc);
buzbee31a4a6f2012-02-28 15:36:15 -08002286 if (tgtSize == 1) {
2287 RegLocation rlResult;
2288 rlDest = oatGetDest(cUnit, mir, 0);
Ian Rogersf7d9ad32012-03-13 18:45:39 -07002289 rlResult = oatGetReturn(cUnit, rlDest.fp);
buzbee31a4a6f2012-02-28 15:36:15 -08002290 storeValue(cUnit, rlDest, rlResult);
2291 } else {
2292 RegLocation rlResult;
2293 rlDest = oatGetDestWide(cUnit, mir, 0, 1);
Ian Rogersf7d9ad32012-03-13 18:45:39 -07002294 rlResult = oatGetReturnWide(cUnit, rlDest.fp);
buzbee31a4a6f2012-02-28 15:36:15 -08002295 storeValueWide(cUnit, rlDest, rlResult);
2296 }
2297 return false;
2298}
2299
2300void genNegFloat(CompilationUnit* cUnit, RegLocation rlDest, RegLocation rlSrc);
2301bool genArithOpFloatPortable(CompilationUnit* cUnit, MIR* mir,
2302 RegLocation rlDest, RegLocation rlSrc1,
2303 RegLocation rlSrc2)
2304{
2305 RegLocation rlResult;
2306 int funcOffset;
2307
2308 switch (mir->dalvikInsn.opcode) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002309 case Instruction::ADD_FLOAT_2ADDR:
2310 case Instruction::ADD_FLOAT:
buzbee31a4a6f2012-02-28 15:36:15 -08002311 funcOffset = OFFSETOF_MEMBER(Thread, pFadd);
2312 break;
Elliott Hughesadb8c672012-03-06 16:49:32 -08002313 case Instruction::SUB_FLOAT_2ADDR:
2314 case Instruction::SUB_FLOAT:
buzbee31a4a6f2012-02-28 15:36:15 -08002315 funcOffset = OFFSETOF_MEMBER(Thread, pFsub);
2316 break;
Elliott Hughesadb8c672012-03-06 16:49:32 -08002317 case Instruction::DIV_FLOAT_2ADDR:
2318 case Instruction::DIV_FLOAT:
buzbee31a4a6f2012-02-28 15:36:15 -08002319 funcOffset = OFFSETOF_MEMBER(Thread, pFdiv);
2320 break;
Elliott Hughesadb8c672012-03-06 16:49:32 -08002321 case Instruction::MUL_FLOAT_2ADDR:
2322 case Instruction::MUL_FLOAT:
buzbee31a4a6f2012-02-28 15:36:15 -08002323 funcOffset = OFFSETOF_MEMBER(Thread, pFmul);
2324 break;
Elliott Hughesadb8c672012-03-06 16:49:32 -08002325 case Instruction::REM_FLOAT_2ADDR:
2326 case Instruction::REM_FLOAT:
buzbee31a4a6f2012-02-28 15:36:15 -08002327 funcOffset = OFFSETOF_MEMBER(Thread, pFmodf);
2328 break;
Elliott Hughesadb8c672012-03-06 16:49:32 -08002329 case Instruction::NEG_FLOAT: {
buzbee31a4a6f2012-02-28 15:36:15 -08002330 genNegFloat(cUnit, rlDest, rlSrc1);
2331 return false;
2332 }
2333 default:
2334 return true;
2335 }
2336 oatFlushAllRegs(cUnit); /* Send everything to home location */
Ian Rogersab2b55d2012-03-18 00:06:11 -07002337 callRuntimeHelperRegLocationRegLocation(cUnit, funcOffset, rlSrc1, rlSrc2);
Ian Rogersf7d9ad32012-03-13 18:45:39 -07002338 rlResult = oatGetReturn(cUnit, true);
buzbee31a4a6f2012-02-28 15:36:15 -08002339 storeValue(cUnit, rlDest, rlResult);
2340 return false;
2341}
2342
2343void genNegDouble(CompilationUnit* cUnit, RegLocation rlDst, RegLocation rlSrc);
2344bool genArithOpDoublePortable(CompilationUnit* cUnit, MIR* mir,
2345 RegLocation rlDest, RegLocation rlSrc1,
2346 RegLocation rlSrc2)
2347{
2348 RegLocation rlResult;
2349 int funcOffset;
2350
2351 switch (mir->dalvikInsn.opcode) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002352 case Instruction::ADD_DOUBLE_2ADDR:
2353 case Instruction::ADD_DOUBLE:
buzbee31a4a6f2012-02-28 15:36:15 -08002354 funcOffset = OFFSETOF_MEMBER(Thread, pDadd);
2355 break;
Elliott Hughesadb8c672012-03-06 16:49:32 -08002356 case Instruction::SUB_DOUBLE_2ADDR:
2357 case Instruction::SUB_DOUBLE:
buzbee31a4a6f2012-02-28 15:36:15 -08002358 funcOffset = OFFSETOF_MEMBER(Thread, pDsub);
2359 break;
Elliott Hughesadb8c672012-03-06 16:49:32 -08002360 case Instruction::DIV_DOUBLE_2ADDR:
2361 case Instruction::DIV_DOUBLE:
buzbee31a4a6f2012-02-28 15:36:15 -08002362 funcOffset = OFFSETOF_MEMBER(Thread, pDdiv);
2363 break;
Elliott Hughesadb8c672012-03-06 16:49:32 -08002364 case Instruction::MUL_DOUBLE_2ADDR:
2365 case Instruction::MUL_DOUBLE:
buzbee31a4a6f2012-02-28 15:36:15 -08002366 funcOffset = OFFSETOF_MEMBER(Thread, pDmul);
2367 break;
Elliott Hughesadb8c672012-03-06 16:49:32 -08002368 case Instruction::REM_DOUBLE_2ADDR:
2369 case Instruction::REM_DOUBLE:
buzbee31a4a6f2012-02-28 15:36:15 -08002370 funcOffset = OFFSETOF_MEMBER(Thread, pFmod);
2371 break;
Elliott Hughesadb8c672012-03-06 16:49:32 -08002372 case Instruction::NEG_DOUBLE: {
buzbee31a4a6f2012-02-28 15:36:15 -08002373 genNegDouble(cUnit, rlDest, rlSrc1);
2374 return false;
2375 }
2376 default:
2377 return true;
2378 }
2379 oatFlushAllRegs(cUnit); /* Send everything to home location */
Ian Rogersab2b55d2012-03-18 00:06:11 -07002380 callRuntimeHelperRegLocationRegLocation(cUnit, funcOffset, rlSrc1, rlSrc2);
Ian Rogersf7d9ad32012-03-13 18:45:39 -07002381 rlResult = oatGetReturnWide(cUnit, true);
buzbee31a4a6f2012-02-28 15:36:15 -08002382 storeValueWide(cUnit, rlDest, rlResult);
2383 return false;
2384}
2385
2386bool genConversionPortable(CompilationUnit* cUnit, MIR* mir)
2387{
Elliott Hughesadb8c672012-03-06 16:49:32 -08002388 Instruction::Code opcode = mir->dalvikInsn.opcode;
buzbee31a4a6f2012-02-28 15:36:15 -08002389
2390 switch (opcode) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002391 case Instruction::INT_TO_FLOAT:
buzbee31a4a6f2012-02-28 15:36:15 -08002392 return genConversionCall(cUnit, mir, OFFSETOF_MEMBER(Thread, pI2f),
2393 1, 1);
Elliott Hughesadb8c672012-03-06 16:49:32 -08002394 case Instruction::FLOAT_TO_INT:
buzbee31a4a6f2012-02-28 15:36:15 -08002395 return genConversionCall(cUnit, mir, OFFSETOF_MEMBER(Thread, pF2iz),
2396 1, 1);
Elliott Hughesadb8c672012-03-06 16:49:32 -08002397 case Instruction::DOUBLE_TO_FLOAT:
buzbee31a4a6f2012-02-28 15:36:15 -08002398 return genConversionCall(cUnit, mir, OFFSETOF_MEMBER(Thread, pD2f),
2399 2, 1);
Elliott Hughesadb8c672012-03-06 16:49:32 -08002400 case Instruction::FLOAT_TO_DOUBLE:
buzbee31a4a6f2012-02-28 15:36:15 -08002401 return genConversionCall(cUnit, mir, OFFSETOF_MEMBER(Thread, pF2d),
2402 1, 2);
Elliott Hughesadb8c672012-03-06 16:49:32 -08002403 case Instruction::INT_TO_DOUBLE:
buzbee31a4a6f2012-02-28 15:36:15 -08002404 return genConversionCall(cUnit, mir, OFFSETOF_MEMBER(Thread, pI2d),
2405 1, 2);
Elliott Hughesadb8c672012-03-06 16:49:32 -08002406 case Instruction::DOUBLE_TO_INT:
buzbee31a4a6f2012-02-28 15:36:15 -08002407 return genConversionCall(cUnit, mir, OFFSETOF_MEMBER(Thread, pD2iz),
2408 2, 1);
Elliott Hughesadb8c672012-03-06 16:49:32 -08002409 case Instruction::FLOAT_TO_LONG:
buzbee31a4a6f2012-02-28 15:36:15 -08002410 return genConversionCall(cUnit, mir, OFFSETOF_MEMBER(Thread,
2411 pF2l), 1, 2);
Elliott Hughesadb8c672012-03-06 16:49:32 -08002412 case Instruction::LONG_TO_FLOAT:
buzbee31a4a6f2012-02-28 15:36:15 -08002413 return genConversionCall(cUnit, mir, OFFSETOF_MEMBER(Thread, pL2f),
2414 2, 1);
Elliott Hughesadb8c672012-03-06 16:49:32 -08002415 case Instruction::DOUBLE_TO_LONG:
buzbee31a4a6f2012-02-28 15:36:15 -08002416 return genConversionCall(cUnit, mir, OFFSETOF_MEMBER(Thread,
2417 pD2l), 2, 2);
Elliott Hughesadb8c672012-03-06 16:49:32 -08002418 case Instruction::LONG_TO_DOUBLE:
buzbee31a4a6f2012-02-28 15:36:15 -08002419 return genConversionCall(cUnit, mir, OFFSETOF_MEMBER(Thread, pL2d),
2420 2, 2);
2421 default:
2422 return true;
2423 }
2424 return false;
2425}
2426
2427/*
2428 * Generate callout to updateDebugger. Note that we're overloading
2429 * the use of rSUSPEND here. When the debugger is active, this
2430 * register holds the address of the update function. So, if it's
2431 * non-null, we call out to it.
2432 *
2433 * Note also that rRET0 and rRET1 must be preserved across this
2434 * code. This must be handled by the stub.
2435 */
2436void genDebuggerUpdate(CompilationUnit* cUnit, int32_t offset)
2437{
2438 // Following DCHECK verifies that dPC is in range of single load immediate
2439 DCHECK((offset == DEBUGGER_METHOD_ENTRY) ||
2440 (offset == DEBUGGER_METHOD_EXIT) || ((offset & 0xffff) == offset));
2441 oatClobberCalleeSave(cUnit);
2442#if defined(TARGET_ARM)
2443 opRegImm(cUnit, kOpCmp, rSUSPEND, 0);
buzbee82488f52012-03-02 08:20:26 -08002444 opIT(cUnit, kArmCondNe, "T");
buzbee31a4a6f2012-02-28 15:36:15 -08002445 loadConstant(cUnit, rARG2, offset); // arg2 <- Entry code
2446 opReg(cUnit, kOpBlx, rSUSPEND);
Ian Rogersb5d09b22012-03-06 22:14:17 -08002447#elif defined(TARGET_X86)
2448 UNIMPLEMENTED(FATAL);
buzbee31a4a6f2012-02-28 15:36:15 -08002449#else
buzbee82488f52012-03-02 08:20:26 -08002450 LIR* branch = opCmpImmBranch(cUnit, kCondEq, rSUSPEND, 0, NULL);
buzbee31a4a6f2012-02-28 15:36:15 -08002451 loadConstant(cUnit, rARG2, offset);
2452 opReg(cUnit, kOpBlx, rSUSPEND);
2453 LIR* target = newLIR0(cUnit, kPseudoTargetLabel);
buzbee31a4a6f2012-02-28 15:36:15 -08002454 branch->target = (LIR*)target;
2455#endif
2456 oatFreeTemp(cUnit, rARG2);
2457}
2458
2459/* Check if we need to check for pending suspend request */
2460void genSuspendTest(CompilationUnit* cUnit, MIR* mir)
2461{
2462 if (NO_SUSPEND || (mir->optimizationFlags & MIR_IGNORE_SUSPEND_CHECK)) {
2463 return;
2464 }
2465 oatFlushAllRegs(cUnit);
buzbee31a4a6f2012-02-28 15:36:15 -08002466 if (cUnit->genDebugger) {
2467 // If generating code for the debugger, always check for suspension
Ian Rogers6cbb2bd2012-03-16 13:45:30 -07002468#if defined(TARGET_X86)
2469 UNIMPLEMENTED(FATAL);
2470#else
buzbee86a4bce2012-03-06 18:15:00 -08002471 int rTgt = loadHelper(cUnit, OFFSETOF_MEMBER(Thread,
2472 pTestSuspendFromCode));
2473 opReg(cUnit, kOpBlx, rTgt);
2474 // Refresh rSUSPEND
2475 loadWordDisp(cUnit, rSELF,
2476 OFFSETOF_MEMBER(Thread, pUpdateDebuggerFromCode),
2477 rSUSPEND);
Ian Rogersb5d09b22012-03-06 22:14:17 -08002478#endif
buzbee31a4a6f2012-02-28 15:36:15 -08002479 } else {
Ian Rogersb5d09b22012-03-06 22:14:17 -08002480 LIR* branch = NULL;
buzbee31a4a6f2012-02-28 15:36:15 -08002481#if defined(TARGET_ARM)
2482 // In non-debug case, only check periodically
2483 newLIR2(cUnit, kThumbSubRI8, rSUSPEND, 1);
buzbee82488f52012-03-02 08:20:26 -08002484 branch = opCondBranch(cUnit, kCondEq, NULL);
Ian Rogersb5d09b22012-03-06 22:14:17 -08002485#elif defined(TARGET_X86)
2486 newLIR2(cUnit, kX86Cmp32TI, Thread::SuspendCountOffset().Int32Value(), 0);
2487 branch = opCondBranch(cUnit, kCondNe, NULL);
buzbee31a4a6f2012-02-28 15:36:15 -08002488#else
2489 opRegImm(cUnit, kOpSub, rSUSPEND, 1);
buzbee82488f52012-03-02 08:20:26 -08002490 branch = opCmpImmBranch(cUnit, kCondEq, rSUSPEND, 0, NULL);
buzbee31a4a6f2012-02-28 15:36:15 -08002491#endif
buzbee86a4bce2012-03-06 18:15:00 -08002492 LIR* retLab = newLIR0(cUnit, kPseudoTargetLabel);
2493 LIR* target = rawLIR(cUnit, cUnit->currentDalvikOffset,
2494 kPseudoSuspendTarget, (intptr_t)retLab, mir->offset);
2495 branch->target = (LIR*)target;
2496 oatInsertGrowableList(cUnit, &cUnit->suspendLaunchpads, (intptr_t)target);
buzbee31a4a6f2012-02-28 15:36:15 -08002497 }
buzbee31a4a6f2012-02-28 15:36:15 -08002498}
2499
2500} // namespace art