blob: c5b28b3faf9b5f33811aa800bacb1faada4ec697 [file] [log] [blame]
buzbee31a4a6f2012-02-28 15:36:15 -08001/*
2 * Copyright (C) 2012 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17namespace art {
18
19/*
20 * This source files contains "gen" codegen routines that should
21 * be applicable to most targets. Only mid-level support utilities
22 * and "op" calls may be used here.
23 */
24
25#if defined(TARGET_ARM)
buzbee82488f52012-03-02 08:20:26 -080026LIR* opIT(CompilationUnit* cUnit, ArmConditionCode cond, const char* guide);
buzbee31a4a6f2012-02-28 15:36:15 -080027#endif
28
Ian Rogersab2b55d2012-03-18 00:06:11 -070029void callRuntimeHelperImm(CompilationUnit* cUnit, int helperOffset, int arg0) {
30#if !defined(TARGET_X86)
31 int rTgt = loadHelper(cUnit, helperOffset);
32#endif
33 loadConstant(cUnit, rARG0, arg0);
buzbee31a4a6f2012-02-28 15:36:15 -080034 oatClobberCalleeSave(cUnit);
Ian Rogers6cbb2bd2012-03-16 13:45:30 -070035#if !defined(TARGET_X86)
Ian Rogersab2b55d2012-03-18 00:06:11 -070036 opReg(cUnit, kOpBlx, rTgt);
37 oatFreeTemp(cUnit, rTgt);
Ian Rogers6cbb2bd2012-03-16 13:45:30 -070038#else
Ian Rogersab2b55d2012-03-18 00:06:11 -070039 opThreadMem(cUnit, kOpBlx, helperOffset);
40#endif
41}
42
43void callRuntimeHelperRegLocation(CompilationUnit* cUnit, int helperOffset,
44 RegLocation arg0) {
45#if !defined(TARGET_X86)
46 int rTgt = loadHelper(cUnit, helperOffset);
47#endif
48 if (arg0.wide == 0) {
49 loadValueDirectFixed(cUnit, arg0, rARG0);
50 } else {
51 loadValueDirectWideFixed(cUnit, arg0, rARG0, rARG1);
52 }
53 oatClobberCalleeSave(cUnit);
54#if !defined(TARGET_X86)
55 opReg(cUnit, kOpBlx, rTgt);
56 oatFreeTemp(cUnit, rTgt);
57#else
58 opThreadMem(cUnit, kOpBlx, helperOffset);
59#endif
60}
61
62void callRuntimeHelperImmImm(CompilationUnit* cUnit, int helperOffset,
63 int arg0, int arg1) {
64#if !defined(TARGET_X86)
65 int rTgt = loadHelper(cUnit, helperOffset);
66#endif
67 loadConstant(cUnit, rARG0, arg0);
68 loadConstant(cUnit, rARG1, arg1);
69 oatClobberCalleeSave(cUnit);
70#if !defined(TARGET_X86)
71 opReg(cUnit, kOpBlx, rTgt);
72 oatFreeTemp(cUnit, rTgt);
73#else
74 opThreadMem(cUnit, kOpBlx, helperOffset);
75#endif
76}
77
78void callRuntimeHelperImmRegLocation(CompilationUnit* cUnit, int helperOffset,
79 int arg0, RegLocation arg1) {
80#if !defined(TARGET_X86)
81 int rTgt = loadHelper(cUnit, helperOffset);
82#endif
83 if (arg1.wide == 0) {
84 loadValueDirectFixed(cUnit, arg1, rARG1);
85 } else {
86 loadValueDirectWideFixed(cUnit, arg1, rARG1, rARG2);
87 }
88 loadConstant(cUnit, rARG0, arg0);
89 oatClobberCalleeSave(cUnit);
90#if !defined(TARGET_X86)
91 opReg(cUnit, kOpBlx, rTgt);
92 oatFreeTemp(cUnit, rTgt);
93#else
94 opThreadMem(cUnit, kOpBlx, helperOffset);
95#endif
96}
97
98void callRuntimeHelperRegLocationImm(CompilationUnit* cUnit, int helperOffset,
99 RegLocation arg0, int arg1) {
100#if !defined(TARGET_X86)
101 int rTgt = loadHelper(cUnit, helperOffset);
102#endif
103 loadValueDirectFixed(cUnit, arg0, rARG0);
104 loadConstant(cUnit, rARG1, arg1);
105 oatClobberCalleeSave(cUnit);
106#if !defined(TARGET_X86)
107 opReg(cUnit, kOpBlx, rTgt);
108 oatFreeTemp(cUnit, rTgt);
109#else
110 opThreadMem(cUnit, kOpBlx, helperOffset);
111#endif
112}
113
114void callRuntimeHelperImmReg(CompilationUnit* cUnit, int helperOffset,
115 int arg0, int arg1) {
116#if !defined(TARGET_X86)
117 int rTgt = loadHelper(cUnit, helperOffset);
118#endif
119 opRegCopy(cUnit, rARG1, arg1);
120 loadConstant(cUnit, rARG0, arg0);
121 oatClobberCalleeSave(cUnit);
122#if !defined(TARGET_X86)
123 opReg(cUnit, kOpBlx, rTgt);
124 oatFreeTemp(cUnit, rTgt);
125#else
126 opThreadMem(cUnit, kOpBlx, helperOffset);
127#endif
128}
129
130void callRuntimeHelperRegImm(CompilationUnit* cUnit, int helperOffset,
131 int arg0, int arg1) {
132#if !defined(TARGET_X86)
133 int rTgt = loadHelper(cUnit, helperOffset);
134#endif
135 opRegCopy(cUnit, rARG0, arg0);
136 loadConstant(cUnit, rARG1, arg1);
137 oatClobberCalleeSave(cUnit);
138#if !defined(TARGET_X86)
139 opReg(cUnit, kOpBlx, rTgt);
140 oatFreeTemp(cUnit, rTgt);
141#else
142 opThreadMem(cUnit, kOpBlx, helperOffset);
143#endif
144}
145
146void callRuntimeHelperImmMethod(CompilationUnit* cUnit, int helperOffset,
147 int arg0) {
148#if !defined(TARGET_X86)
149 int rTgt = loadHelper(cUnit, helperOffset);
150#endif
151 loadCurrMethodDirect(cUnit, rARG1);
152 loadConstant(cUnit, rARG0, arg0);
153 oatClobberCalleeSave(cUnit);
154#if !defined(TARGET_X86)
155 opReg(cUnit, kOpBlx, rTgt);
156 oatFreeTemp(cUnit, rTgt);
157#else
158 opThreadMem(cUnit, kOpBlx, helperOffset);
159#endif
160}
161
162void callRuntimeHelperRegLocationRegLocation(CompilationUnit* cUnit,
163 int helperOffset,
164 RegLocation arg0,
165 RegLocation arg1) {
166#if !defined(TARGET_X86)
167 int rTgt = loadHelper(cUnit, helperOffset);
168#endif
169 if (arg0.wide == 0) {
170 loadValueDirectFixed(cUnit, arg0, rARG0);
171 if (arg1.wide == 0) {
172 loadValueDirectFixed(cUnit, arg1, rARG1);
173 } else {
174 loadValueDirectWideFixed(cUnit, arg1, rARG1, rARG2);
175 }
176 } else {
177 loadValueDirectWideFixed(cUnit, arg0, rARG0, rARG1);
178 if (arg1.wide == 0) {
179 loadValueDirectFixed(cUnit, arg1, rARG2);
180 } else {
Ian Rogersab2b55d2012-03-18 00:06:11 -0700181 loadValueDirectWideFixed(cUnit, arg1, rARG2, rARG3);
Ian Rogersab2b55d2012-03-18 00:06:11 -0700182 }
183 }
184 oatClobberCalleeSave(cUnit);
185#if !defined(TARGET_X86)
186 opReg(cUnit, kOpBlx, rTgt);
187 oatFreeTemp(cUnit, rTgt);
188#else
189 opThreadMem(cUnit, kOpBlx, helperOffset);
190#endif
191}
192
193void callRuntimeHelperRegReg(CompilationUnit* cUnit, int helperOffset,
194 int arg0, int arg1) {
195#if !defined(TARGET_X86)
196 int rTgt = loadHelper(cUnit, helperOffset);
197#endif
198 DCHECK_NE((int)rARG0, arg1); // check copy into arg0 won't clobber arg1
199 opRegCopy(cUnit, rARG0, arg0);
200 opRegCopy(cUnit, rARG1, arg1);
201 oatClobberCalleeSave(cUnit);
202#if !defined(TARGET_X86)
203 opReg(cUnit, kOpBlx, rTgt);
204 oatFreeTemp(cUnit, rTgt);
205#else
206 opThreadMem(cUnit, kOpBlx, helperOffset);
207#endif
208}
209
210void callRuntimeHelperRegRegImm(CompilationUnit* cUnit, int helperOffset,
211 int arg0, int arg1, int arg2) {
212#if !defined(TARGET_X86)
213 int rTgt = loadHelper(cUnit, helperOffset);
214#endif
215 DCHECK_NE((int)rARG0, arg1); // check copy into arg0 won't clobber arg1
216 opRegCopy(cUnit, rARG0, arg0);
217 opRegCopy(cUnit, rARG1, arg1);
218 loadConstant(cUnit, rARG2, arg2);
219 oatClobberCalleeSave(cUnit);
220#if !defined(TARGET_X86)
221 opReg(cUnit, kOpBlx, rTgt);
222 oatFreeTemp(cUnit, rTgt);
223#else
224 opThreadMem(cUnit, kOpBlx, helperOffset);
225#endif
226}
227
228void callRuntimeHelperImmMethodRegLocation(CompilationUnit* cUnit, int helperOffset,
229 int arg0, RegLocation arg2) {
230#if !defined(TARGET_X86)
231 int rTgt = loadHelper(cUnit, helperOffset);
232#endif
233 loadValueDirectFixed(cUnit, arg2, rARG2);
234 loadCurrMethodDirect(cUnit, rARG1);
235 loadConstant(cUnit, rARG0, arg0);
236 oatClobberCalleeSave(cUnit);
237#if !defined(TARGET_X86)
238 opReg(cUnit, kOpBlx, rTgt);
239 oatFreeTemp(cUnit, rTgt);
240#else
241 opThreadMem(cUnit, kOpBlx, helperOffset);
242#endif
243}
244
245void callRuntimeHelperImmMethodImm(CompilationUnit* cUnit, int helperOffset,
246 int arg0, int arg2) {
247#if !defined(TARGET_X86)
248 int rTgt = loadHelper(cUnit, helperOffset);
249#endif
250 loadCurrMethodDirect(cUnit, rARG1);
251 loadConstant(cUnit, rARG2, arg2);
252 loadConstant(cUnit, rARG0, arg0);
253 oatClobberCalleeSave(cUnit);
254#if !defined(TARGET_X86)
255 opReg(cUnit, kOpBlx, rTgt);
256 oatFreeTemp(cUnit, rTgt);
257#else
258 opThreadMem(cUnit, kOpBlx, helperOffset);
259#endif
260}
261
262void callRuntimeHelperImmRegLocationRegLocation(CompilationUnit* cUnit,
263 int helperOffset,
264 int arg0, RegLocation arg1,
265 RegLocation arg2) {
266#if !defined(TARGET_X86)
267 int rTgt = loadHelper(cUnit, helperOffset);
268#endif
269 loadValueDirectFixed(cUnit, arg1, rARG1);
270 if (arg2.wide == 0) {
271 loadValueDirectFixed(cUnit, arg2, rARG2);
272 } else {
Ian Rogersab2b55d2012-03-18 00:06:11 -0700273 loadValueDirectWideFixed(cUnit, arg2, rARG2, rARG3);
Ian Rogersab2b55d2012-03-18 00:06:11 -0700274 }
275 loadConstant(cUnit, rARG0, arg0);
276 oatClobberCalleeSave(cUnit);
277#if !defined(TARGET_X86)
278 opReg(cUnit, kOpBlx, rTgt);
279 oatFreeTemp(cUnit, rTgt);
280#else
281 opThreadMem(cUnit, kOpBlx, helperOffset);
Ian Rogers6cbb2bd2012-03-16 13:45:30 -0700282#endif
buzbee31a4a6f2012-02-28 15:36:15 -0800283}
284
285/*
286 * Generate an kPseudoBarrier marker to indicate the boundary of special
287 * blocks.
288 */
289void genBarrier(CompilationUnit* cUnit)
290{
291 LIR* barrier = newLIR0(cUnit, kPseudoBarrier);
292 /* Mark all resources as being clobbered */
293 barrier->defMask = -1;
294}
295
buzbee31a4a6f2012-02-28 15:36:15 -0800296
297/* Generate unconditional branch instructions */
buzbee82488f52012-03-02 08:20:26 -0800298LIR* opUnconditionalBranch(CompilationUnit* cUnit, LIR* target)
buzbee31a4a6f2012-02-28 15:36:15 -0800299{
Ian Rogers680b1bd2012-03-07 20:18:49 -0800300 LIR* branch = opBranchUnconditional(cUnit, kOpUncondBr);
buzbee31a4a6f2012-02-28 15:36:15 -0800301 branch->target = (LIR*) target;
302 return branch;
303}
304
buzbee5de34942012-03-01 14:51:57 -0800305// FIXME: need to do some work to split out targets with
306// condition codes and those without
307#if defined(TARGET_ARM) || defined(TARGET_X86)
buzbee31a4a6f2012-02-28 15:36:15 -0800308LIR* genCheck(CompilationUnit* cUnit, ConditionCode cCode, MIR* mir,
309 ThrowKind kind)
310{
buzbeea2ebdd72012-03-04 14:57:06 -0800311 LIR* tgt = rawLIR(cUnit, 0, kPseudoThrowTarget, kind,
312 mir ? mir->offset : 0);
buzbee82488f52012-03-02 08:20:26 -0800313 LIR* branch = opCondBranch(cUnit, cCode, tgt);
buzbee31a4a6f2012-02-28 15:36:15 -0800314 // Remember branch target - will process later
315 oatInsertGrowableList(cUnit, &cUnit->throwLaunchpads, (intptr_t)tgt);
316 return branch;
317}
buzbee5de34942012-03-01 14:51:57 -0800318#endif
buzbee31a4a6f2012-02-28 15:36:15 -0800319
320LIR* genImmedCheck(CompilationUnit* cUnit, ConditionCode cCode,
321 int reg, int immVal, MIR* mir, ThrowKind kind)
322{
buzbeea2ebdd72012-03-04 14:57:06 -0800323 LIR* tgt = rawLIR(cUnit, 0, kPseudoThrowTarget, kind, mir->offset);
buzbee31a4a6f2012-02-28 15:36:15 -0800324 LIR* branch;
325 if (cCode == kCondAl) {
buzbee82488f52012-03-02 08:20:26 -0800326 branch = opUnconditionalBranch(cUnit, tgt);
buzbee31a4a6f2012-02-28 15:36:15 -0800327 } else {
buzbee82488f52012-03-02 08:20:26 -0800328 branch = opCmpImmBranch(cUnit, cCode, reg, immVal, tgt);
buzbee31a4a6f2012-02-28 15:36:15 -0800329 }
330 // Remember branch target - will process later
331 oatInsertGrowableList(cUnit, &cUnit->throwLaunchpads, (intptr_t)tgt);
332 return branch;
333}
334
335/* Perform null-check on a register. */
336LIR* genNullCheck(CompilationUnit* cUnit, int sReg, int mReg, MIR* mir)
337{
338 if (!(cUnit->disableOpt & (1 << kNullCheckElimination)) &&
339 mir->optimizationFlags & MIR_IGNORE_NULL_CHECK) {
340 return NULL;
341 }
342 return genImmedCheck(cUnit, kCondEq, mReg, 0, mir, kThrowNullPointer);
343}
344
345/* Perform check on two registers */
346LIR* genRegRegCheck(CompilationUnit* cUnit, ConditionCode cCode,
Ian Rogersb5d09b22012-03-06 22:14:17 -0800347 int reg1, int reg2, MIR* mir, ThrowKind kind)
buzbee31a4a6f2012-02-28 15:36:15 -0800348{
buzbeea2ebdd72012-03-04 14:57:06 -0800349 LIR* tgt = rawLIR(cUnit, 0, kPseudoThrowTarget, kind,
350 mir ? mir->offset : 0, reg1, reg2);
buzbee5de34942012-03-01 14:51:57 -0800351#if defined(TARGET_MIPS)
buzbee82488f52012-03-02 08:20:26 -0800352 LIR* branch = opCmpBranch(cUnit, cCode, reg1, reg2, tgt);
buzbee5de34942012-03-01 14:51:57 -0800353#else
buzbee31a4a6f2012-02-28 15:36:15 -0800354 opRegReg(cUnit, kOpCmp, reg1, reg2);
buzbee82488f52012-03-02 08:20:26 -0800355 LIR* branch = opCondBranch(cUnit, cCode, tgt);
buzbee5de34942012-03-01 14:51:57 -0800356#endif
buzbee31a4a6f2012-02-28 15:36:15 -0800357 // Remember branch target - will process later
358 oatInsertGrowableList(cUnit, &cUnit->throwLaunchpads, (intptr_t)tgt);
359 return branch;
360}
361
362void genCompareAndBranch(CompilationUnit* cUnit, BasicBlock* bb, MIR* mir,
363 RegLocation rlSrc1, RegLocation rlSrc2, LIR* labelList)
364{
365 ConditionCode cond;
366 rlSrc1 = loadValue(cUnit, rlSrc1, kCoreReg);
367 rlSrc2 = loadValue(cUnit, rlSrc2, kCoreReg);
Elliott Hughesadb8c672012-03-06 16:49:32 -0800368 Instruction::Code opcode = mir->dalvikInsn.opcode;
buzbee31a4a6f2012-02-28 15:36:15 -0800369 switch(opcode) {
Elliott Hughesadb8c672012-03-06 16:49:32 -0800370 case Instruction::IF_EQ:
buzbee31a4a6f2012-02-28 15:36:15 -0800371 cond = kCondEq;
372 break;
Elliott Hughesadb8c672012-03-06 16:49:32 -0800373 case Instruction::IF_NE:
buzbee31a4a6f2012-02-28 15:36:15 -0800374 cond = kCondNe;
375 break;
Elliott Hughesadb8c672012-03-06 16:49:32 -0800376 case Instruction::IF_LT:
buzbee31a4a6f2012-02-28 15:36:15 -0800377 cond = kCondLt;
378 break;
Elliott Hughesadb8c672012-03-06 16:49:32 -0800379 case Instruction::IF_GE:
buzbee31a4a6f2012-02-28 15:36:15 -0800380 cond = kCondGe;
381 break;
Elliott Hughesadb8c672012-03-06 16:49:32 -0800382 case Instruction::IF_GT:
buzbee31a4a6f2012-02-28 15:36:15 -0800383 cond = kCondGt;
384 break;
Elliott Hughesadb8c672012-03-06 16:49:32 -0800385 case Instruction::IF_LE:
buzbee31a4a6f2012-02-28 15:36:15 -0800386 cond = kCondLe;
387 break;
388 default:
389 cond = (ConditionCode)0;
390 LOG(FATAL) << "Unexpected opcode " << (int)opcode;
391 }
buzbee5de34942012-03-01 14:51:57 -0800392#if defined(TARGET_MIPS)
buzbee82488f52012-03-02 08:20:26 -0800393 opCmpBranch(cUnit, cond, rlSrc1.lowReg, rlSrc2.lowReg,
394 &labelList[bb->taken->id]);
buzbee5de34942012-03-01 14:51:57 -0800395#else
396 opRegReg(cUnit, kOpCmp, rlSrc1.lowReg, rlSrc2.lowReg);
buzbee82488f52012-03-02 08:20:26 -0800397 opCondBranch(cUnit, cond, &labelList[bb->taken->id]);
buzbee5de34942012-03-01 14:51:57 -0800398#endif
buzbee82488f52012-03-02 08:20:26 -0800399 opUnconditionalBranch(cUnit, &labelList[bb->fallThrough->id]);
buzbee31a4a6f2012-02-28 15:36:15 -0800400}
401
402void genCompareZeroAndBranch(CompilationUnit* cUnit, BasicBlock* bb, MIR* mir,
403 RegLocation rlSrc, LIR* labelList)
404{
405 ConditionCode cond;
406 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
Elliott Hughesadb8c672012-03-06 16:49:32 -0800407 Instruction::Code opcode = mir->dalvikInsn.opcode;
buzbee31a4a6f2012-02-28 15:36:15 -0800408 switch(opcode) {
Elliott Hughesadb8c672012-03-06 16:49:32 -0800409 case Instruction::IF_EQZ:
buzbee31a4a6f2012-02-28 15:36:15 -0800410 cond = kCondEq;
411 break;
Elliott Hughesadb8c672012-03-06 16:49:32 -0800412 case Instruction::IF_NEZ:
buzbee31a4a6f2012-02-28 15:36:15 -0800413 cond = kCondNe;
414 break;
Elliott Hughesadb8c672012-03-06 16:49:32 -0800415 case Instruction::IF_LTZ:
buzbee31a4a6f2012-02-28 15:36:15 -0800416 cond = kCondLt;
417 break;
Elliott Hughesadb8c672012-03-06 16:49:32 -0800418 case Instruction::IF_GEZ:
buzbee31a4a6f2012-02-28 15:36:15 -0800419 cond = kCondGe;
420 break;
Elliott Hughesadb8c672012-03-06 16:49:32 -0800421 case Instruction::IF_GTZ:
buzbee31a4a6f2012-02-28 15:36:15 -0800422 cond = kCondGt;
423 break;
Elliott Hughesadb8c672012-03-06 16:49:32 -0800424 case Instruction::IF_LEZ:
buzbee31a4a6f2012-02-28 15:36:15 -0800425 cond = kCondLe;
426 break;
427 default:
428 cond = (ConditionCode)0;
429 LOG(FATAL) << "Unexpected opcode " << (int)opcode;
430 }
buzbee5de34942012-03-01 14:51:57 -0800431#if defined(TARGET_MIPS)
buzbee82488f52012-03-02 08:20:26 -0800432 opCmpImmBranch(cUnit, cond, rlSrc.lowReg, 0, &labelList[bb->taken->id]);
buzbee5de34942012-03-01 14:51:57 -0800433#else
434 opRegImm(cUnit, kOpCmp, rlSrc.lowReg, 0);
buzbee82488f52012-03-02 08:20:26 -0800435 opCondBranch(cUnit, cond, &labelList[bb->taken->id]);
buzbee5de34942012-03-01 14:51:57 -0800436#endif
buzbee82488f52012-03-02 08:20:26 -0800437 opUnconditionalBranch(cUnit, &labelList[bb->fallThrough->id]);
buzbee31a4a6f2012-02-28 15:36:15 -0800438}
439
440void genIntToLong(CompilationUnit* cUnit, MIR* mir, RegLocation rlDest,
441 RegLocation rlSrc)
442{
443 RegLocation rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
444 if (rlSrc.location == kLocPhysReg) {
buzbee82488f52012-03-02 08:20:26 -0800445 opRegCopy(cUnit, rlResult.lowReg, rlSrc.lowReg);
buzbee31a4a6f2012-02-28 15:36:15 -0800446 } else {
447 loadValueDirect(cUnit, rlSrc, rlResult.lowReg);
448 }
449 opRegRegImm(cUnit, kOpAsr, rlResult.highReg,
450 rlResult.lowReg, 31);
451 storeValueWide(cUnit, rlDest, rlResult);
452}
453
454void genIntNarrowing(CompilationUnit* cUnit, MIR* mir, RegLocation rlDest,
455 RegLocation rlSrc)
456{
457 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
458 RegLocation rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
459 OpKind op = kOpInvalid;
460 switch(mir->dalvikInsn.opcode) {
Elliott Hughesadb8c672012-03-06 16:49:32 -0800461 case Instruction::INT_TO_BYTE:
buzbee31a4a6f2012-02-28 15:36:15 -0800462 op = kOp2Byte;
463 break;
Elliott Hughesadb8c672012-03-06 16:49:32 -0800464 case Instruction::INT_TO_SHORT:
buzbee31a4a6f2012-02-28 15:36:15 -0800465 op = kOp2Short;
466 break;
Elliott Hughesadb8c672012-03-06 16:49:32 -0800467 case Instruction::INT_TO_CHAR:
buzbee31a4a6f2012-02-28 15:36:15 -0800468 op = kOp2Char;
469 break;
470 default:
471 LOG(ERROR) << "Bad int conversion type";
472 }
473 opRegReg(cUnit, op, rlResult.lowReg, rlSrc.lowReg);
474 storeValue(cUnit, rlDest, rlResult);
475}
476
477/*
478 * Let helper function take care of everything. Will call
479 * Array::AllocFromCode(type_idx, method, count);
480 * Note: AllocFromCode will handle checks for errNegativeArraySize.
481 */
482void genNewArray(CompilationUnit* cUnit, MIR* mir, RegLocation rlDest,
483 RegLocation rlSrc)
484{
485 oatFlushAllRegs(cUnit); /* Everything to home location */
486 uint32_t type_idx = mir->dalvikInsn.vC;
Ian Rogers6cbb2bd2012-03-16 13:45:30 -0700487 int funcOffset;
buzbee31a4a6f2012-02-28 15:36:15 -0800488 if (cUnit->compiler->CanAccessTypeWithoutChecks(cUnit->method_idx,
489 cUnit->dex_cache,
490 *cUnit->dex_file,
491 type_idx)) {
Ian Rogers6cbb2bd2012-03-16 13:45:30 -0700492 funcOffset = OFFSETOF_MEMBER(Thread, pAllocArrayFromCode);
buzbee31a4a6f2012-02-28 15:36:15 -0800493 } else {
Ian Rogers6cbb2bd2012-03-16 13:45:30 -0700494 funcOffset= OFFSETOF_MEMBER(Thread, pAllocArrayFromCodeWithAccessCheck);
buzbee31a4a6f2012-02-28 15:36:15 -0800495 }
Ian Rogersab2b55d2012-03-18 00:06:11 -0700496 callRuntimeHelperImmMethodRegLocation(cUnit, funcOffset, type_idx, rlSrc);
Ian Rogersf7d9ad32012-03-13 18:45:39 -0700497 RegLocation rlResult = oatGetReturn(cUnit, false);
buzbee31a4a6f2012-02-28 15:36:15 -0800498 storeValue(cUnit, rlDest, rlResult);
499}
500
501/*
502 * Similar to genNewArray, but with post-allocation initialization.
503 * Verifier guarantees we're dealing with an array class. Current
504 * code throws runtime exception "bad Filled array req" for 'D' and 'J'.
505 * Current code also throws internal unimp if not 'L', '[' or 'I'.
506 */
507void genFilledNewArray(CompilationUnit* cUnit, MIR* mir, bool isRange)
508{
509 DecodedInstruction* dInsn = &mir->dalvikInsn;
510 int elems = dInsn->vA;
Ian Rogersab2b55d2012-03-18 00:06:11 -0700511 int typeIdx = dInsn->vB;
buzbee31a4a6f2012-02-28 15:36:15 -0800512 oatFlushAllRegs(cUnit); /* Everything to home location */
Ian Rogers6cbb2bd2012-03-16 13:45:30 -0700513 int funcOffset;
buzbee31a4a6f2012-02-28 15:36:15 -0800514 if (cUnit->compiler->CanAccessTypeWithoutChecks(cUnit->method_idx,
515 cUnit->dex_cache,
516 *cUnit->dex_file,
Ian Rogersab2b55d2012-03-18 00:06:11 -0700517 typeIdx)) {
Ian Rogers6cbb2bd2012-03-16 13:45:30 -0700518 funcOffset = OFFSETOF_MEMBER(Thread, pCheckAndAllocArrayFromCode);
buzbee31a4a6f2012-02-28 15:36:15 -0800519 } else {
Ian Rogers6cbb2bd2012-03-16 13:45:30 -0700520 funcOffset = OFFSETOF_MEMBER(Thread,
521 pCheckAndAllocArrayFromCodeWithAccessCheck);
buzbee31a4a6f2012-02-28 15:36:15 -0800522 }
Ian Rogersab2b55d2012-03-18 00:06:11 -0700523 callRuntimeHelperImmMethodImm(cUnit, funcOffset, typeIdx, elems);
buzbeee1965672012-03-11 18:39:19 -0700524 oatFreeTemp(cUnit, rARG2);
525 oatFreeTemp(cUnit, rARG1);
buzbee31a4a6f2012-02-28 15:36:15 -0800526 /*
Elliott Hughesadb8c672012-03-06 16:49:32 -0800527 * NOTE: the implicit target for Instruction::FILLED_NEW_ARRAY is the
buzbee31a4a6f2012-02-28 15:36:15 -0800528 * return region. Because AllocFromCode placed the new array
529 * in rRET0, we'll just lock it into place. When debugger support is
530 * added, it may be necessary to additionally copy all return
531 * values to a home location in thread-local storage
532 */
533 oatLockTemp(cUnit, rRET0);
534
535 // TODO: use the correct component size, currently all supported types
536 // share array alignment with ints (see comment at head of function)
537 size_t component_size = sizeof(int32_t);
538
539 // Having a range of 0 is legal
540 if (isRange && (dInsn->vA > 0)) {
541 /*
542 * Bit of ugliness here. We're going generate a mem copy loop
543 * on the register range, but it is possible that some regs
544 * in the range have been promoted. This is unlikely, but
545 * before generating the copy, we'll just force a flush
546 * of any regs in the source range that have been promoted to
547 * home location.
548 */
549 for (unsigned int i = 0; i < dInsn->vA; i++) {
550 RegLocation loc = oatUpdateLoc(cUnit,
551 oatGetSrc(cUnit, mir, i));
552 if (loc.location == kLocPhysReg) {
553 storeBaseDisp(cUnit, rSP, oatSRegOffset(cUnit, loc.sRegLow),
554 loc.lowReg, kWord);
555 }
556 }
557 /*
558 * TUNING note: generated code here could be much improved, but
559 * this is an uncommon operation and isn't especially performance
560 * critical.
561 */
562 int rSrc = oatAllocTemp(cUnit);
563 int rDst = oatAllocTemp(cUnit);
564 int rIdx = oatAllocTemp(cUnit);
buzbee5de34942012-03-01 14:51:57 -0800565#if defined(TARGET_ARM)
buzbee31a4a6f2012-02-28 15:36:15 -0800566 int rVal = rLR; // Using a lot of temps, rLR is known free here
buzbee5de34942012-03-01 14:51:57 -0800567#else
568 int rVal = oatAllocTemp(cUnit);
569#endif
buzbee31a4a6f2012-02-28 15:36:15 -0800570 // Set up source pointer
571 RegLocation rlFirst = oatGetSrc(cUnit, mir, 0);
Ian Rogersb5d09b22012-03-06 22:14:17 -0800572#if defined(TARGET_X86)
573 UNIMPLEMENTED(FATAL);
574#else
buzbee31a4a6f2012-02-28 15:36:15 -0800575 opRegRegImm(cUnit, kOpAdd, rSrc, rSP,
576 oatSRegOffset(cUnit, rlFirst.sRegLow));
577 // Set up the target pointer
578 opRegRegImm(cUnit, kOpAdd, rDst, rRET0,
579 Array::DataOffset(component_size).Int32Value());
Ian Rogersb5d09b22012-03-06 22:14:17 -0800580#endif
buzbee31a4a6f2012-02-28 15:36:15 -0800581 // Set up the loop counter (known to be > 0)
582 loadConstant(cUnit, rIdx, dInsn->vA - 1);
583 // Generate the copy loop. Going backwards for convenience
584 LIR* target = newLIR0(cUnit, kPseudoTargetLabel);
buzbee31a4a6f2012-02-28 15:36:15 -0800585 // Copy next element
586 loadBaseIndexed(cUnit, rSrc, rIdx, rVal, 2, kWord);
587 storeBaseIndexed(cUnit, rDst, rIdx, rVal, 2, kWord);
588#if defined(TARGET_ARM)
589 // Combine sub & test using sub setflags encoding here
590 newLIR3(cUnit, kThumb2SubsRRI12, rIdx, rIdx, 1);
buzbee82488f52012-03-02 08:20:26 -0800591 opCondBranch(cUnit, kCondGe, target);
buzbee31a4a6f2012-02-28 15:36:15 -0800592#else
buzbee5de34942012-03-01 14:51:57 -0800593 oatFreeTemp(cUnit, rVal);
buzbee31a4a6f2012-02-28 15:36:15 -0800594 opRegImm(cUnit, kOpSub, rIdx, 1);
buzbee82488f52012-03-02 08:20:26 -0800595 opCmpImmBranch(cUnit, kCondGe, rIdx, 0, target);
buzbee31a4a6f2012-02-28 15:36:15 -0800596#endif
buzbee31a4a6f2012-02-28 15:36:15 -0800597 } else if (!isRange) {
598 // TUNING: interleave
599 for (unsigned int i = 0; i < dInsn->vA; i++) {
600 RegLocation rlArg = loadValue(cUnit,
601 oatGetSrc(cUnit, mir, i), kCoreReg);
602 storeBaseDisp(cUnit, rRET0,
603 Array::DataOffset(component_size).Int32Value() +
604 i * 4, rlArg.lowReg, kWord);
605 // If the loadValue caused a temp to be allocated, free it
606 if (oatIsTemp(cUnit, rlArg.lowReg)) {
607 oatFreeTemp(cUnit, rlArg.lowReg);
608 }
609 }
610 }
611}
612
613void genSput(CompilationUnit* cUnit, MIR* mir, RegLocation rlSrc,
614 bool isLongOrDouble, bool isObject)
615{
616 int fieldOffset;
617 int ssbIndex;
618 bool isVolatile;
619 bool isReferrersClass;
620 uint32_t fieldIdx = mir->dalvikInsn.vB;
621
622 OatCompilationUnit mUnit(cUnit->class_loader, cUnit->class_linker,
623 *cUnit->dex_file, *cUnit->dex_cache,
624 cUnit->code_item, cUnit->method_idx,
625 cUnit->access_flags);
626
627 bool fastPath =
628 cUnit->compiler->ComputeStaticFieldInfo(fieldIdx, &mUnit,
629 fieldOffset, ssbIndex,
630 isReferrersClass, isVolatile, true);
631 if (fastPath && !SLOW_FIELD_PATH) {
632 DCHECK_GE(fieldOffset, 0);
633 int rBase;
buzbee31a4a6f2012-02-28 15:36:15 -0800634 if (isReferrersClass) {
635 // Fast path, static storage base is this method's class
buzbeee1965672012-03-11 18:39:19 -0700636 RegLocation rlMethod = loadCurrMethod(cUnit);
buzbee31a4a6f2012-02-28 15:36:15 -0800637 rBase = oatAllocTemp(cUnit);
buzbeee1965672012-03-11 18:39:19 -0700638 loadWordDisp(cUnit, rlMethod.lowReg,
buzbee31a4a6f2012-02-28 15:36:15 -0800639 Method::DeclaringClassOffset().Int32Value(), rBase);
640 } else {
641 // Medium path, static storage base in a different class which
642 // requires checks that the other class is initialized.
643 DCHECK_GE(ssbIndex, 0);
644 // May do runtime call so everything to home locations.
645 oatFlushAllRegs(cUnit);
646 // Using fixed register to sync with possible call to runtime
647 // support.
buzbeee1965672012-03-11 18:39:19 -0700648 int rMethod = rARG1;
buzbee31a4a6f2012-02-28 15:36:15 -0800649 oatLockTemp(cUnit, rMethod);
650 loadCurrMethodDirect(cUnit, rMethod);
651 rBase = rARG0;
652 oatLockTemp(cUnit, rBase);
653 loadWordDisp(cUnit, rMethod,
654 Method::DexCacheInitializedStaticStorageOffset().Int32Value(),
655 rBase);
656 loadWordDisp(cUnit, rBase,
657 Array::DataOffset(sizeof(Object*)).Int32Value() + sizeof(int32_t*) *
658 ssbIndex, rBase);
659 // rBase now points at appropriate static storage base (Class*)
660 // or NULL if not initialized. Check for NULL and call helper if NULL.
661 // TUNING: fast path should fall through
buzbee82488f52012-03-02 08:20:26 -0800662 LIR* branchOver = opCmpImmBranch(cUnit, kCondNe, rBase, 0, NULL);
buzbee31a4a6f2012-02-28 15:36:15 -0800663 loadConstant(cUnit, rARG0, ssbIndex);
Ian Rogersab2b55d2012-03-18 00:06:11 -0700664 callRuntimeHelperImm(cUnit,
665 OFFSETOF_MEMBER(Thread, pInitializeStaticStorage),
666 ssbIndex);
buzbee31a4a6f2012-02-28 15:36:15 -0800667#if defined(TARGET_MIPS)
668 // For Arm, rRET0 = rARG0 = rBASE, for Mips, we need to copy
buzbee82488f52012-03-02 08:20:26 -0800669 opRegCopy(cUnit, rBase, rRET0);
buzbee31a4a6f2012-02-28 15:36:15 -0800670#endif
671 LIR* skipTarget = newLIR0(cUnit, kPseudoTargetLabel);
buzbee31a4a6f2012-02-28 15:36:15 -0800672 branchOver->target = (LIR*)skipTarget;
buzbeee1965672012-03-11 18:39:19 -0700673 oatFreeTemp(cUnit, rMethod);
buzbee31a4a6f2012-02-28 15:36:15 -0800674 }
675 // rBase now holds static storage base
buzbee31a4a6f2012-02-28 15:36:15 -0800676 if (isLongOrDouble) {
677 rlSrc = oatGetSrcWide(cUnit, mir, 0, 1);
678 rlSrc = loadValueWide(cUnit, rlSrc, kAnyReg);
679 } else {
680 rlSrc = oatGetSrc(cUnit, mir, 0);
681 rlSrc = loadValue(cUnit, rlSrc, kAnyReg);
682 }
683//FIXME: need to generalize the barrier call
684 if (isVolatile) {
685 oatGenMemBarrier(cUnit, kST);
686 }
687 if (isLongOrDouble) {
688 storeBaseDispWide(cUnit, rBase, fieldOffset, rlSrc.lowReg,
689 rlSrc.highReg);
690 } else {
691 storeWordDisp(cUnit, rBase, fieldOffset, rlSrc.lowReg);
692 }
693 if (isVolatile) {
694 oatGenMemBarrier(cUnit, kSY);
695 }
696 if (isObject) {
697 markGCCard(cUnit, rlSrc.lowReg, rBase);
698 }
699 oatFreeTemp(cUnit, rBase);
700 } else {
701 oatFlushAllRegs(cUnit); // Everything to home locations
702 int setterOffset = isLongOrDouble ? OFFSETOF_MEMBER(Thread, pSet64Static) :
703 (isObject ? OFFSETOF_MEMBER(Thread, pSetObjStatic)
704 : OFFSETOF_MEMBER(Thread, pSet32Static));
Ian Rogersab2b55d2012-03-18 00:06:11 -0700705 callRuntimeHelperImmRegLocation(cUnit, setterOffset, fieldIdx, rlSrc);
buzbee31a4a6f2012-02-28 15:36:15 -0800706 }
707}
708
709void genSget(CompilationUnit* cUnit, MIR* mir, RegLocation rlDest,
710 bool isLongOrDouble, bool isObject)
711{
712 int fieldOffset;
713 int ssbIndex;
714 bool isVolatile;
715 bool isReferrersClass;
716 uint32_t fieldIdx = mir->dalvikInsn.vB;
717
718 OatCompilationUnit mUnit(cUnit->class_loader, cUnit->class_linker,
719 *cUnit->dex_file, *cUnit->dex_cache,
720 cUnit->code_item, cUnit->method_idx,
721 cUnit->access_flags);
722
723 bool fastPath =
724 cUnit->compiler->ComputeStaticFieldInfo(fieldIdx, &mUnit,
725 fieldOffset, ssbIndex,
726 isReferrersClass, isVolatile,
727 false);
728 if (fastPath && !SLOW_FIELD_PATH) {
729 DCHECK_GE(fieldOffset, 0);
730 int rBase;
buzbee31a4a6f2012-02-28 15:36:15 -0800731 if (isReferrersClass) {
732 // Fast path, static storage base is this method's class
buzbeee1965672012-03-11 18:39:19 -0700733 RegLocation rlMethod = loadCurrMethod(cUnit);
buzbee31a4a6f2012-02-28 15:36:15 -0800734 rBase = oatAllocTemp(cUnit);
buzbeee1965672012-03-11 18:39:19 -0700735 loadWordDisp(cUnit, rlMethod.lowReg,
buzbee31a4a6f2012-02-28 15:36:15 -0800736 Method::DeclaringClassOffset().Int32Value(), rBase);
737 } else {
738 // Medium path, static storage base in a different class which
739 // requires checks that the other class is initialized
740 DCHECK_GE(ssbIndex, 0);
741 // May do runtime call so everything to home locations.
742 oatFlushAllRegs(cUnit);
743 // Using fixed register to sync with possible call to runtime
744 // support
buzbeee1965672012-03-11 18:39:19 -0700745 int rMethod = rARG1;
buzbee31a4a6f2012-02-28 15:36:15 -0800746 oatLockTemp(cUnit, rMethod);
747 loadCurrMethodDirect(cUnit, rMethod);
748 rBase = rARG0;
749 oatLockTemp(cUnit, rBase);
750 loadWordDisp(cUnit, rMethod,
751 Method::DexCacheInitializedStaticStorageOffset().Int32Value(),
752 rBase);
753 loadWordDisp(cUnit, rBase,
754 Array::DataOffset(sizeof(Object*)).Int32Value() +
755 sizeof(int32_t*) * ssbIndex,
756 rBase);
757 // rBase now points at appropriate static storage base (Class*)
758 // or NULL if not initialized. Check for NULL and call helper if NULL.
759 // TUNING: fast path should fall through
buzbee82488f52012-03-02 08:20:26 -0800760 LIR* branchOver = opCmpImmBranch(cUnit, kCondNe, rBase, 0, NULL);
Ian Rogersab2b55d2012-03-18 00:06:11 -0700761 callRuntimeHelperImm(cUnit,
762 OFFSETOF_MEMBER(Thread, pInitializeStaticStorage),
763 ssbIndex);
buzbee31a4a6f2012-02-28 15:36:15 -0800764#if defined(TARGET_MIPS)
765 // For Arm, rRET0 = rARG0 = rBASE, for Mips, we need to copy
buzbee82488f52012-03-02 08:20:26 -0800766 opRegCopy(cUnit, rBase, rRET0);
buzbee31a4a6f2012-02-28 15:36:15 -0800767#endif
768 LIR* skipTarget = newLIR0(cUnit, kPseudoTargetLabel);
buzbee31a4a6f2012-02-28 15:36:15 -0800769 branchOver->target = (LIR*)skipTarget;
buzbeee1965672012-03-11 18:39:19 -0700770 oatFreeTemp(cUnit, rMethod);
buzbee31a4a6f2012-02-28 15:36:15 -0800771 }
772 // rBase now holds static storage base
buzbee31a4a6f2012-02-28 15:36:15 -0800773 rlDest = isLongOrDouble ? oatGetDestWide(cUnit, mir, 0, 1)
774 : oatGetDest(cUnit, mir, 0);
775 RegLocation rlResult = oatEvalLoc(cUnit, rlDest, kAnyReg, true);
776 if (isVolatile) {
777 oatGenMemBarrier(cUnit, kSY);
778 }
779 if (isLongOrDouble) {
780 loadBaseDispWide(cUnit, NULL, rBase, fieldOffset, rlResult.lowReg,
781 rlResult.highReg, INVALID_SREG);
782 } else {
783 loadWordDisp(cUnit, rBase, fieldOffset, rlResult.lowReg);
784 }
785 oatFreeTemp(cUnit, rBase);
786 if (isLongOrDouble) {
787 storeValueWide(cUnit, rlDest, rlResult);
788 } else {
789 storeValue(cUnit, rlDest, rlResult);
790 }
791 } else {
792 oatFlushAllRegs(cUnit); // Everything to home locations
793 int getterOffset = isLongOrDouble ? OFFSETOF_MEMBER(Thread, pGet64Static) :
794 (isObject ? OFFSETOF_MEMBER(Thread, pGetObjStatic)
795 : OFFSETOF_MEMBER(Thread, pGet32Static));
Ian Rogersab2b55d2012-03-18 00:06:11 -0700796 callRuntimeHelperImm(cUnit, getterOffset, fieldIdx);
buzbee31a4a6f2012-02-28 15:36:15 -0800797 if (isLongOrDouble) {
Ian Rogersf7d9ad32012-03-13 18:45:39 -0700798 RegLocation rlResult = oatGetReturnWide(cUnit, rlDest.fp);
buzbee31a4a6f2012-02-28 15:36:15 -0800799 storeValueWide(cUnit, rlDest, rlResult);
800 } else {
Ian Rogersf7d9ad32012-03-13 18:45:39 -0700801 RegLocation rlResult = oatGetReturn(cUnit, rlDest.fp);
buzbee31a4a6f2012-02-28 15:36:15 -0800802 storeValue(cUnit, rlDest, rlResult);
803 }
804 }
805}
806
807
808// Debugging routine - if null target, branch to DebugMe
809void genShowTarget(CompilationUnit* cUnit)
810{
buzbeea7678db2012-03-05 15:35:46 -0800811#if defined(TARGET_X86)
812 UNIMPLEMENTED(WARNING) << "genShowTarget";
813#else
buzbee0398c422012-03-02 15:22:47 -0800814 LIR* branchOver = opCmpImmBranch(cUnit, kCondNe, rINVOKE_TGT, 0, NULL);
buzbee31a4a6f2012-02-28 15:36:15 -0800815 loadWordDisp(cUnit, rSELF,
buzbee0398c422012-03-02 15:22:47 -0800816 OFFSETOF_MEMBER(Thread, pDebugMe), rINVOKE_TGT);
buzbee31a4a6f2012-02-28 15:36:15 -0800817 LIR* target = newLIR0(cUnit, kPseudoTargetLabel);
buzbee31a4a6f2012-02-28 15:36:15 -0800818 branchOver->target = (LIR*)target;
buzbeea7678db2012-03-05 15:35:46 -0800819#endif
buzbee31a4a6f2012-02-28 15:36:15 -0800820}
821
822void genThrowVerificationError(CompilationUnit* cUnit, MIR* mir)
823{
Ian Rogersab2b55d2012-03-18 00:06:11 -0700824 callRuntimeHelperImmImm(cUnit, OFFSETOF_MEMBER(Thread,
825 pThrowVerificationErrorFromCode),
826 mir->dalvikInsn.vA, mir->dalvikInsn.vB);
buzbee31a4a6f2012-02-28 15:36:15 -0800827}
828
829void handleSuspendLaunchpads(CompilationUnit *cUnit)
830{
Ian Rogersab2b55d2012-03-18 00:06:11 -0700831 LIR** suspendLabel = (LIR **)cUnit->suspendLaunchpads.elemList;
buzbee31a4a6f2012-02-28 15:36:15 -0800832 int numElems = cUnit->suspendLaunchpads.numUsed;
buzbee31a4a6f2012-02-28 15:36:15 -0800833 for (int i = 0; i < numElems; i++) {
buzbeec5159d52012-03-03 11:48:39 -0800834 oatResetRegPool(cUnit);
buzbee31a4a6f2012-02-28 15:36:15 -0800835 LIR* lab = suspendLabel[i];
836 LIR* resumeLab = (LIR*)lab->operands[0];
837 cUnit->currentDalvikOffset = lab->operands[1];
Ian Rogersab2b55d2012-03-18 00:06:11 -0700838 oatAppendLIR(cUnit, lab);
Ian Rogers6cbb2bd2012-03-16 13:45:30 -0700839#if defined(TARGET_X86)
840 opThreadMem(cUnit, kOpBlx,
841 OFFSETOF_MEMBER(Thread, pTestSuspendFromCode));
842#else
buzbee31a4a6f2012-02-28 15:36:15 -0800843 int rTgt = loadHelper(cUnit, OFFSETOF_MEMBER(Thread,
844 pTestSuspendFromCode));
buzbee31a4a6f2012-02-28 15:36:15 -0800845 opReg(cUnit, kOpBlx, rTgt);
Ian Rogers6cbb2bd2012-03-16 13:45:30 -0700846#endif
buzbee82488f52012-03-02 08:20:26 -0800847 opUnconditionalBranch(cUnit, resumeLab);
buzbee31a4a6f2012-02-28 15:36:15 -0800848 }
849}
850
851void handleThrowLaunchpads(CompilationUnit *cUnit)
852{
Ian Rogersab2b55d2012-03-18 00:06:11 -0700853 LIR** throwLabel = (LIR **)cUnit->throwLaunchpads.elemList;
buzbee31a4a6f2012-02-28 15:36:15 -0800854 int numElems = cUnit->throwLaunchpads.numUsed;
Ian Rogersab2b55d2012-03-18 00:06:11 -0700855 for (int i = 0; i < numElems; i++) {
buzbeec5159d52012-03-03 11:48:39 -0800856 oatResetRegPool(cUnit);
buzbee31a4a6f2012-02-28 15:36:15 -0800857 LIR* lab = throwLabel[i];
858 cUnit->currentDalvikOffset = lab->operands[1];
Ian Rogersab2b55d2012-03-18 00:06:11 -0700859 oatAppendLIR(cUnit, lab);
buzbee31a4a6f2012-02-28 15:36:15 -0800860 int funcOffset = 0;
861 int v1 = lab->operands[2];
862 int v2 = lab->operands[3];
863 switch(lab->operands[0]) {
864 case kThrowNullPointer:
865 funcOffset = OFFSETOF_MEMBER(Thread, pThrowNullPointerFromCode);
866 break;
867 case kThrowArrayBounds:
buzbee5de34942012-03-01 14:51:57 -0800868 if (v2 != rARG0) {
buzbee82488f52012-03-02 08:20:26 -0800869 opRegCopy(cUnit, rARG0, v1);
870 opRegCopy(cUnit, rARG1, v2);
buzbee31a4a6f2012-02-28 15:36:15 -0800871 } else {
buzbee5de34942012-03-01 14:51:57 -0800872 if (v1 == rARG1) {
buzbee31a4a6f2012-02-28 15:36:15 -0800873#if defined(TARGET_ARM)
874 int rTmp = r12;
875#else
876 int rTmp = oatAllocTemp(cUnit);
877#endif
buzbee82488f52012-03-02 08:20:26 -0800878 opRegCopy(cUnit, rTmp, v1);
879 opRegCopy(cUnit, rARG1, v2);
880 opRegCopy(cUnit, rARG0, rTmp);
buzbee31a4a6f2012-02-28 15:36:15 -0800881 } else {
buzbee82488f52012-03-02 08:20:26 -0800882 opRegCopy(cUnit, rARG1, v2);
883 opRegCopy(cUnit, rARG0, v1);
buzbee31a4a6f2012-02-28 15:36:15 -0800884 }
885 }
886 funcOffset = OFFSETOF_MEMBER(Thread, pThrowArrayBoundsFromCode);
887 break;
888 case kThrowDivZero:
889 funcOffset = OFFSETOF_MEMBER(Thread, pThrowDivZeroFromCode);
890 break;
891 case kThrowVerificationError:
892 loadConstant(cUnit, rARG0, v1);
893 loadConstant(cUnit, rARG1, v2);
894 funcOffset =
895 OFFSETOF_MEMBER(Thread, pThrowVerificationErrorFromCode);
896 break;
897 case kThrowNegArraySize:
buzbee82488f52012-03-02 08:20:26 -0800898 opRegCopy(cUnit, rARG0, v1);
buzbee31a4a6f2012-02-28 15:36:15 -0800899 funcOffset =
900 OFFSETOF_MEMBER(Thread, pThrowNegArraySizeFromCode);
901 break;
902 case kThrowNoSuchMethod:
buzbee82488f52012-03-02 08:20:26 -0800903 opRegCopy(cUnit, rARG0, v1);
buzbee31a4a6f2012-02-28 15:36:15 -0800904 funcOffset =
905 OFFSETOF_MEMBER(Thread, pThrowNoSuchMethodFromCode);
906 break;
907 case kThrowStackOverflow:
908 funcOffset =
909 OFFSETOF_MEMBER(Thread, pThrowStackOverflowFromCode);
910 // Restore stack alignment
911 opRegImm(cUnit, kOpAdd, rSP,
912 (cUnit->numCoreSpills + cUnit->numFPSpills) * 4);
913 break;
914 default:
915 LOG(FATAL) << "Unexpected throw kind: " << lab->operands[0];
916 }
Ian Rogersab2b55d2012-03-18 00:06:11 -0700917 oatClobberCalleeSave(cUnit);
918#if !defined(TARGET_X86)
buzbee31a4a6f2012-02-28 15:36:15 -0800919 int rTgt = loadHelper(cUnit, funcOffset);
Ian Rogersab2b55d2012-03-18 00:06:11 -0700920 opReg(cUnit, kOpBlx, rTgt);
buzbee0398c422012-03-02 15:22:47 -0800921 oatFreeTemp(cUnit, rTgt);
Ian Rogersab2b55d2012-03-18 00:06:11 -0700922#else
923 opThreadMem(cUnit, kOpBlx, funcOffset);
Ian Rogers6cbb2bd2012-03-16 13:45:30 -0700924#endif
buzbee31a4a6f2012-02-28 15:36:15 -0800925 }
926}
927
928/* Needed by the Assembler */
929void oatSetupResourceMasks(LIR* lir)
930{
931 setupResourceMasks(lir);
932}
933
934void genIGet(CompilationUnit* cUnit, MIR* mir, OpSize size,
935 RegLocation rlDest, RegLocation rlObj,
936 bool isLongOrDouble, bool isObject)
937{
938 int fieldOffset;
939 bool isVolatile;
940 uint32_t fieldIdx = mir->dalvikInsn.vC;
941
942 OatCompilationUnit mUnit(cUnit->class_loader, cUnit->class_linker,
943 *cUnit->dex_file, *cUnit->dex_cache,
944 cUnit->code_item, cUnit->method_idx,
945 cUnit->access_flags);
946
947 bool fastPath = cUnit->compiler->ComputeInstanceFieldInfo(fieldIdx, &mUnit,
948 fieldOffset, isVolatile, false);
949
950 if (fastPath && !SLOW_FIELD_PATH) {
951 RegLocation rlResult;
952 RegisterClass regClass = oatRegClassBySize(size);
953 DCHECK_GE(fieldOffset, 0);
954 rlObj = loadValue(cUnit, rlObj, kCoreReg);
955 if (isLongOrDouble) {
956 DCHECK(rlDest.wide);
957 genNullCheck(cUnit, rlObj.sRegLow, rlObj.lowReg, mir);/* null? */
Ian Rogersb5d09b22012-03-06 22:14:17 -0800958#if defined(TARGET_X86)
959 rlResult = oatEvalLoc(cUnit, rlDest, regClass, true);
960 genNullCheck(cUnit, rlObj.sRegLow, rlObj.lowReg, mir);/* null? */
961 loadBaseDispWide(cUnit, mir, rlObj.lowReg, fieldOffset, rlResult.lowReg,
962 rlResult.highReg, rlObj.sRegLow);
963 if (isVolatile) {
964 oatGenMemBarrier(cUnit, kSY);
965 }
966#else
buzbee31a4a6f2012-02-28 15:36:15 -0800967 int regPtr = oatAllocTemp(cUnit);
968 opRegRegImm(cUnit, kOpAdd, regPtr, rlObj.lowReg, fieldOffset);
969 rlResult = oatEvalLoc(cUnit, rlDest, regClass, true);
970 loadPair(cUnit, regPtr, rlResult.lowReg, rlResult.highReg);
971 if (isVolatile) {
972 oatGenMemBarrier(cUnit, kSY);
973 }
974 oatFreeTemp(cUnit, regPtr);
Ian Rogersb5d09b22012-03-06 22:14:17 -0800975#endif
buzbee31a4a6f2012-02-28 15:36:15 -0800976 storeValueWide(cUnit, rlDest, rlResult);
977 } else {
978 rlResult = oatEvalLoc(cUnit, rlDest, regClass, true);
979 genNullCheck(cUnit, rlObj.sRegLow, rlObj.lowReg, mir);/* null? */
980 loadBaseDisp(cUnit, mir, rlObj.lowReg, fieldOffset, rlResult.lowReg,
981 kWord, rlObj.sRegLow);
982 if (isVolatile) {
983 oatGenMemBarrier(cUnit, kSY);
984 }
985 storeValue(cUnit, rlDest, rlResult);
986 }
987 } else {
988 int getterOffset = isLongOrDouble ? OFFSETOF_MEMBER(Thread, pGet64Instance) :
989 (isObject ? OFFSETOF_MEMBER(Thread, pGetObjInstance)
990 : OFFSETOF_MEMBER(Thread, pGet32Instance));
Ian Rogersab2b55d2012-03-18 00:06:11 -0700991 callRuntimeHelperImmRegLocation(cUnit, getterOffset, fieldIdx, rlObj);
buzbee31a4a6f2012-02-28 15:36:15 -0800992 if (isLongOrDouble) {
Ian Rogersf7d9ad32012-03-13 18:45:39 -0700993 RegLocation rlResult = oatGetReturnWide(cUnit, rlDest.fp);
buzbee31a4a6f2012-02-28 15:36:15 -0800994 storeValueWide(cUnit, rlDest, rlResult);
995 } else {
Ian Rogersf7d9ad32012-03-13 18:45:39 -0700996 RegLocation rlResult = oatGetReturn(cUnit, rlDest.fp);
buzbee31a4a6f2012-02-28 15:36:15 -0800997 storeValue(cUnit, rlDest, rlResult);
998 }
999 }
1000}
1001
1002void genIPut(CompilationUnit* cUnit, MIR* mir, OpSize size, RegLocation rlSrc,
1003 RegLocation rlObj, bool isLongOrDouble, bool isObject)
1004{
1005 int fieldOffset;
1006 bool isVolatile;
1007 uint32_t fieldIdx = mir->dalvikInsn.vC;
1008
1009 OatCompilationUnit mUnit(cUnit->class_loader, cUnit->class_linker,
1010 *cUnit->dex_file, *cUnit->dex_cache,
1011 cUnit->code_item, cUnit->method_idx,
1012 cUnit->access_flags);
1013
1014 bool fastPath = cUnit->compiler->ComputeInstanceFieldInfo(fieldIdx, &mUnit,
1015 fieldOffset, isVolatile, true);
1016 if (fastPath && !SLOW_FIELD_PATH) {
1017 RegisterClass regClass = oatRegClassBySize(size);
1018 DCHECK_GE(fieldOffset, 0);
1019 rlObj = loadValue(cUnit, rlObj, kCoreReg);
1020 if (isLongOrDouble) {
1021 int regPtr;
1022 rlSrc = loadValueWide(cUnit, rlSrc, kAnyReg);
1023 genNullCheck(cUnit, rlObj.sRegLow, rlObj.lowReg, mir);/* null? */
1024 regPtr = oatAllocTemp(cUnit);
1025 opRegRegImm(cUnit, kOpAdd, regPtr, rlObj.lowReg, fieldOffset);
1026 if (isVolatile) {
1027 oatGenMemBarrier(cUnit, kST);
1028 }
1029 storePair(cUnit, regPtr, rlSrc.lowReg, rlSrc.highReg);
1030 if (isVolatile) {
1031 oatGenMemBarrier(cUnit, kSY);
1032 }
1033 oatFreeTemp(cUnit, regPtr);
1034 } else {
1035 rlSrc = loadValue(cUnit, rlSrc, regClass);
1036 genNullCheck(cUnit, rlObj.sRegLow, rlObj.lowReg, mir);/* null? */
1037 if (isVolatile) {
1038 oatGenMemBarrier(cUnit, kST);
1039 }
1040 storeBaseDisp(cUnit, rlObj.lowReg, fieldOffset, rlSrc.lowReg, kWord);
1041 if (isVolatile) {
1042 oatGenMemBarrier(cUnit, kSY);
1043 }
buzbeea7c12682012-03-19 13:13:53 -07001044 if (isObject) {
1045 markGCCard(cUnit, rlSrc.lowReg, rlObj.lowReg);
1046 }
buzbee31a4a6f2012-02-28 15:36:15 -08001047 }
1048 } else {
1049 int setterOffset = isLongOrDouble ? OFFSETOF_MEMBER(Thread, pSet64Instance) :
1050 (isObject ? OFFSETOF_MEMBER(Thread, pSetObjInstance)
1051 : OFFSETOF_MEMBER(Thread, pSet32Instance));
Ian Rogersab2b55d2012-03-18 00:06:11 -07001052 callRuntimeHelperImmRegLocationRegLocation(cUnit, setterOffset,
1053 fieldIdx, rlObj, rlSrc);
buzbee31a4a6f2012-02-28 15:36:15 -08001054 }
1055}
1056
1057void genConstClass(CompilationUnit* cUnit, MIR* mir, RegLocation rlDest,
1058 RegLocation rlSrc)
1059{
1060 uint32_t type_idx = mir->dalvikInsn.vB;
buzbeee1965672012-03-11 18:39:19 -07001061 RegLocation rlMethod = loadCurrMethod(cUnit);
buzbee31a4a6f2012-02-28 15:36:15 -08001062 int resReg = oatAllocTemp(cUnit);
1063 RegLocation rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
1064 if (!cUnit->compiler->CanAccessTypeWithoutChecks(cUnit->method_idx,
1065 cUnit->dex_cache,
1066 *cUnit->dex_file,
1067 type_idx)) {
1068 // Call out to helper which resolves type and verifies access.
1069 // Resolved type returned in rRET0.
Ian Rogersab2b55d2012-03-18 00:06:11 -07001070 callRuntimeHelperImmReg(cUnit,
1071 OFFSETOF_MEMBER(Thread,
1072 pInitializeTypeAndVerifyAccessFromCode),
1073 type_idx, rlMethod.lowReg);
Ian Rogersf7d9ad32012-03-13 18:45:39 -07001074 RegLocation rlResult = oatGetReturn(cUnit, false);
buzbee31a4a6f2012-02-28 15:36:15 -08001075 storeValue(cUnit, rlDest, rlResult);
1076 } else {
1077 // We're don't need access checks, load type from dex cache
1078 int32_t dex_cache_offset =
1079 Method::DexCacheResolvedTypesOffset().Int32Value();
buzbeee1965672012-03-11 18:39:19 -07001080 loadWordDisp(cUnit, rlMethod.lowReg, dex_cache_offset, resReg);
buzbee31a4a6f2012-02-28 15:36:15 -08001081 int32_t offset_of_type =
1082 Array::DataOffset(sizeof(Class*)).Int32Value() + (sizeof(Class*)
1083 * type_idx);
1084 loadWordDisp(cUnit, resReg, offset_of_type, rlResult.lowReg);
1085 if (!cUnit->compiler->CanAssumeTypeIsPresentInDexCache(cUnit->dex_cache,
1086 type_idx) || SLOW_TYPE_PATH) {
1087 // Slow path, at runtime test if type is null and if so initialize
1088 oatFlushAllRegs(cUnit);
buzbee82488f52012-03-02 08:20:26 -08001089 LIR* branch1 = opCmpImmBranch(cUnit, kCondEq, rlResult.lowReg, 0,
1090 NULL);
buzbee31a4a6f2012-02-28 15:36:15 -08001091 // Resolved, store and hop over following code
1092 storeValue(cUnit, rlDest, rlResult);
buzbee3d661942012-03-14 17:37:27 -07001093 /*
1094 * Because we have stores of the target value on two paths,
1095 * clobber temp tracking for the destination using the ssa name
1096 */
1097 oatClobberSReg(cUnit, rlDest.sRegLow);
buzbee82488f52012-03-02 08:20:26 -08001098 LIR* branch2 = opUnconditionalBranch(cUnit,0);
buzbee31a4a6f2012-02-28 15:36:15 -08001099 // TUNING: move slow path to end & remove unconditional branch
1100 LIR* target1 = newLIR0(cUnit, kPseudoTargetLabel);
buzbee5de34942012-03-01 14:51:57 -08001101 // Call out to helper, which will return resolved type in rARG0
Ian Rogersab2b55d2012-03-18 00:06:11 -07001102 callRuntimeHelperImmReg(cUnit, OFFSETOF_MEMBER(Thread,
1103 pInitializeTypeFromCode),
1104 type_idx, rlMethod.lowReg);
Ian Rogersf7d9ad32012-03-13 18:45:39 -07001105 RegLocation rlResult = oatGetReturn(cUnit, false);
buzbee31a4a6f2012-02-28 15:36:15 -08001106 storeValue(cUnit, rlDest, rlResult);
buzbee3d661942012-03-14 17:37:27 -07001107 /*
1108 * Because we have stores of the target value on two paths,
1109 * clobber temp tracking for the destination using the ssa name
1110 */
1111 oatClobberSReg(cUnit, rlDest.sRegLow);
buzbee31a4a6f2012-02-28 15:36:15 -08001112 // Rejoin code paths
1113 LIR* target2 = newLIR0(cUnit, kPseudoTargetLabel);
buzbee31a4a6f2012-02-28 15:36:15 -08001114 branch1->target = (LIR*)target1;
1115 branch2->target = (LIR*)target2;
1116 } else {
1117 // Fast path, we're done - just store result
1118 storeValue(cUnit, rlDest, rlResult);
1119 }
1120 }
1121}
Ian Rogersab2b55d2012-03-18 00:06:11 -07001122
buzbee31a4a6f2012-02-28 15:36:15 -08001123void genConstString(CompilationUnit* cUnit, MIR* mir, RegLocation rlDest,
1124 RegLocation rlSrc)
1125{
1126 /* NOTE: Most strings should be available at compile time */
1127 uint32_t string_idx = mir->dalvikInsn.vB;
1128 int32_t offset_of_string = Array::DataOffset(sizeof(String*)).Int32Value() +
1129 (sizeof(String*) * string_idx);
1130 if (!cUnit->compiler->CanAssumeStringIsPresentInDexCache(
1131 cUnit->dex_cache, string_idx) || SLOW_STRING_PATH) {
1132 // slow path, resolve string if not in dex cache
1133 oatFlushAllRegs(cUnit);
1134 oatLockCallTemps(cUnit); // Using explicit registers
1135 loadCurrMethodDirect(cUnit, rARG2);
1136 loadWordDisp(cUnit, rARG2,
1137 Method::DexCacheStringsOffset().Int32Value(), rARG0);
1138 // Might call out to helper, which will return resolved string in rRET0
Ian Rogers6cbb2bd2012-03-16 13:45:30 -07001139#if !defined(TARGET_X86)
buzbee31a4a6f2012-02-28 15:36:15 -08001140 int rTgt = loadHelper(cUnit, OFFSETOF_MEMBER(Thread,
1141 pResolveStringFromCode));
Ian Rogers6cbb2bd2012-03-16 13:45:30 -07001142#endif
buzbee31a4a6f2012-02-28 15:36:15 -08001143 loadWordDisp(cUnit, rRET0, offset_of_string, rARG0);
1144 loadConstant(cUnit, rARG1, string_idx);
1145#if defined(TARGET_ARM)
1146 opRegImm(cUnit, kOpCmp, rRET0, 0); // Is resolved?
1147 genBarrier(cUnit);
1148 // For testing, always force through helper
1149 if (!EXERCISE_SLOWEST_STRING_PATH) {
buzbee82488f52012-03-02 08:20:26 -08001150 opIT(cUnit, kArmCondEq, "T");
buzbee31a4a6f2012-02-28 15:36:15 -08001151 }
buzbee82488f52012-03-02 08:20:26 -08001152 opRegCopy(cUnit, rARG0, rARG2); // .eq
buzbee31a4a6f2012-02-28 15:36:15 -08001153 opReg(cUnit, kOpBlx, rTgt); // .eq, helper(Method*, string_idx)
Ian Rogers6cbb2bd2012-03-16 13:45:30 -07001154 oatFreeTemp(cUnit, rTgt);
1155#elif defined(TARGET_MIPS)
buzbee82488f52012-03-02 08:20:26 -08001156 LIR* branch = opCmpImmBranch(cUnit, kCondNe, rRET0, 0, NULL);
1157 opRegCopy(cUnit, rARG0, rARG2); // .eq
buzbee31a4a6f2012-02-28 15:36:15 -08001158 opReg(cUnit, kOpBlx, rTgt);
Ian Rogers6cbb2bd2012-03-16 13:45:30 -07001159 oatFreeTemp(cUnit, rTgt);
buzbee31a4a6f2012-02-28 15:36:15 -08001160 LIR* target = newLIR0(cUnit, kPseudoTargetLabel);
buzbee31a4a6f2012-02-28 15:36:15 -08001161 branch->target = target;
Ian Rogers6cbb2bd2012-03-16 13:45:30 -07001162#else
Ian Rogersab2b55d2012-03-18 00:06:11 -07001163 callRuntimeHelperRegReg(cUnit, OFFSETOF_MEMBER(Thread,
1164 pResolveStringFromCode),
1165 rARG2, rARG1);
buzbee31a4a6f2012-02-28 15:36:15 -08001166#endif
1167 genBarrier(cUnit);
Ian Rogersf7d9ad32012-03-13 18:45:39 -07001168 storeValue(cUnit, rlDest, oatGetReturn(cUnit, false));
buzbee31a4a6f2012-02-28 15:36:15 -08001169 } else {
buzbeee1965672012-03-11 18:39:19 -07001170 RegLocation rlMethod = loadCurrMethod(cUnit);
buzbee31a4a6f2012-02-28 15:36:15 -08001171 int resReg = oatAllocTemp(cUnit);
1172 RegLocation rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
buzbeee1965672012-03-11 18:39:19 -07001173 loadWordDisp(cUnit, rlMethod.lowReg,
buzbee31a4a6f2012-02-28 15:36:15 -08001174 Method::DexCacheStringsOffset().Int32Value(), resReg);
1175 loadWordDisp(cUnit, resReg, offset_of_string, rlResult.lowReg);
1176 storeValue(cUnit, rlDest, rlResult);
1177 }
1178}
1179
1180/*
1181 * Let helper function take care of everything. Will
1182 * call Class::NewInstanceFromCode(type_idx, method);
1183 */
1184void genNewInstance(CompilationUnit* cUnit, MIR* mir, RegLocation rlDest)
1185{
1186 oatFlushAllRegs(cUnit); /* Everything to home location */
1187 uint32_t type_idx = mir->dalvikInsn.vB;
1188 // alloc will always check for resolution, do we also need to verify
1189 // access because the verifier was unable to?
Ian Rogers6cbb2bd2012-03-16 13:45:30 -07001190 int funcOffset;
buzbee31a4a6f2012-02-28 15:36:15 -08001191 if (cUnit->compiler->CanAccessInstantiableTypeWithoutChecks(
1192 cUnit->method_idx, cUnit->dex_cache, *cUnit->dex_file, type_idx)) {
Ian Rogers6cbb2bd2012-03-16 13:45:30 -07001193 funcOffset = OFFSETOF_MEMBER(Thread, pAllocObjectFromCode);
buzbee31a4a6f2012-02-28 15:36:15 -08001194 } else {
Ian Rogers6cbb2bd2012-03-16 13:45:30 -07001195 funcOffset =
1196 OFFSETOF_MEMBER(Thread, pAllocObjectFromCodeWithAccessCheck);
buzbee31a4a6f2012-02-28 15:36:15 -08001197 }
Ian Rogersab2b55d2012-03-18 00:06:11 -07001198 callRuntimeHelperImmMethod(cUnit, funcOffset, type_idx);
Ian Rogersf7d9ad32012-03-13 18:45:39 -07001199 RegLocation rlResult = oatGetReturn(cUnit, false);
buzbee31a4a6f2012-02-28 15:36:15 -08001200 storeValue(cUnit, rlDest, rlResult);
1201}
1202
Ian Rogersab2b55d2012-03-18 00:06:11 -07001203void genThrow(CompilationUnit* cUnit, MIR* mir, RegLocation rlSrc)
1204{
1205 oatFlushAllRegs(cUnit);
1206 callRuntimeHelperRegLocation(cUnit, OFFSETOF_MEMBER(Thread, pDeliverException), rlSrc);
1207}
1208
buzbee31a4a6f2012-02-28 15:36:15 -08001209void genInstanceof(CompilationUnit* cUnit, MIR* mir, RegLocation rlDest,
1210 RegLocation rlSrc)
1211{
1212 oatFlushAllRegs(cUnit);
1213 // May generate a call - use explicit registers
1214 oatLockCallTemps(cUnit);
1215 uint32_t type_idx = mir->dalvikInsn.vC;
buzbee5de34942012-03-01 14:51:57 -08001216 loadCurrMethodDirect(cUnit, rARG1); // rARG1 <= current Method*
buzbee31a4a6f2012-02-28 15:36:15 -08001217 int classReg = rARG2; // rARG2 will hold the Class*
1218 if (!cUnit->compiler->CanAccessTypeWithoutChecks(cUnit->method_idx,
1219 cUnit->dex_cache,
1220 *cUnit->dex_file,
1221 type_idx)) {
1222 // Check we have access to type_idx and if not throw IllegalAccessError,
buzbee5de34942012-03-01 14:51:57 -08001223 // returns Class* in rARG0
Ian Rogersab2b55d2012-03-18 00:06:11 -07001224 callRuntimeHelperImm(cUnit,
1225 OFFSETOF_MEMBER(Thread, pInitializeTypeAndVerifyAccessFromCode),
1226 type_idx);
buzbee82488f52012-03-02 08:20:26 -08001227 opRegCopy(cUnit, classReg, rRET0); // Align usage with fast path
buzbee5de34942012-03-01 14:51:57 -08001228 loadValueDirectFixed(cUnit, rlSrc, rARG0); // rARG0 <= ref
buzbee31a4a6f2012-02-28 15:36:15 -08001229 } else {
buzbee5de34942012-03-01 14:51:57 -08001230 // Load dex cache entry into classReg (rARG2)
buzbee31a4a6f2012-02-28 15:36:15 -08001231 loadValueDirectFixed(cUnit, rlSrc, rARG0); // rARG0 <= ref
1232 loadWordDisp(cUnit, rARG1,
1233 Method::DexCacheResolvedTypesOffset().Int32Value(),
1234 classReg);
1235 int32_t offset_of_type =
1236 Array::DataOffset(sizeof(Class*)).Int32Value() + (sizeof(Class*)
1237 * type_idx);
1238 loadWordDisp(cUnit, classReg, offset_of_type, classReg);
1239 if (!cUnit->compiler->CanAssumeTypeIsPresentInDexCache(
1240 cUnit->dex_cache, type_idx)) {
1241 // Need to test presence of type in dex cache at runtime
buzbee82488f52012-03-02 08:20:26 -08001242 LIR* hopBranch = opCmpImmBranch(cUnit, kCondNe, classReg, 0, NULL);
buzbee31a4a6f2012-02-28 15:36:15 -08001243 // Not resolved
1244 // Call out to helper, which will return resolved type in rRET0
Ian Rogersab2b55d2012-03-18 00:06:11 -07001245 callRuntimeHelperImm(cUnit, OFFSETOF_MEMBER(Thread,
1246 pInitializeTypeFromCode),
1247 type_idx);
buzbee82488f52012-03-02 08:20:26 -08001248 opRegCopy(cUnit, rARG2, rRET0); // Align usage with fast path
buzbee31a4a6f2012-02-28 15:36:15 -08001249 loadValueDirectFixed(cUnit, rlSrc, rARG0); /* reload Ref */
1250 // Rejoin code paths
1251 LIR* hopTarget = newLIR0(cUnit, kPseudoTargetLabel);
buzbee31a4a6f2012-02-28 15:36:15 -08001252 hopBranch->target = (LIR*)hopTarget;
1253 }
1254 }
1255 /* rARG0 is ref, rARG2 is class. If ref==null, use directly as bool result */
buzbee82488f52012-03-02 08:20:26 -08001256 LIR* branch1 = opCmpImmBranch(cUnit, kCondEq, rARG0, 0, NULL);
buzbee31a4a6f2012-02-28 15:36:15 -08001257 /* load object->clazz */
1258 DCHECK_EQ(Object::ClassOffset().Int32Value(), 0);
1259 loadWordDisp(cUnit, rARG0, Object::ClassOffset().Int32Value(), rARG1);
1260 /* rARG0 is ref, rARG1 is ref->clazz, rARG2 is class */
buzbee0398c422012-03-02 15:22:47 -08001261#if defined(TARGET_ARM)
1262 /* Uses conditional nullification */
buzbee31a4a6f2012-02-28 15:36:15 -08001263 int rTgt = loadHelper(cUnit, OFFSETOF_MEMBER(Thread,
1264 pInstanceofNonTrivialFromCode));
buzbee31a4a6f2012-02-28 15:36:15 -08001265 opRegReg(cUnit, kOpCmp, rARG1, rARG2); // Same?
buzbee82488f52012-03-02 08:20:26 -08001266 opIT(cUnit, kArmCondEq, "EE"); // if-convert the test
buzbee31a4a6f2012-02-28 15:36:15 -08001267 loadConstant(cUnit, rARG0, 1); // .eq case - load true
buzbee82488f52012-03-02 08:20:26 -08001268 opRegCopy(cUnit, rARG0, rARG2); // .ne case - arg0 <= class
buzbee31a4a6f2012-02-28 15:36:15 -08001269 opReg(cUnit, kOpBlx, rTgt); // .ne case: helper(class, ref->class)
Ian Rogersab2b55d2012-03-18 00:06:11 -07001270 oatFreeTemp(cUnit, rTgt);
buzbee31a4a6f2012-02-28 15:36:15 -08001271#else
buzbee0398c422012-03-02 15:22:47 -08001272 /* Uses branchovers */
1273 loadConstant(cUnit, rARG0, 1); // assume true
1274 LIR* branchover = opCmpBranch(cUnit, kCondEq, rARG1, rARG2, NULL);
Ian Rogers6cbb2bd2012-03-16 13:45:30 -07001275#if !defined(TARGET_X86)
buzbee0398c422012-03-02 15:22:47 -08001276 int rTgt = loadHelper(cUnit, OFFSETOF_MEMBER(Thread,
1277 pInstanceofNonTrivialFromCode));
1278 opRegCopy(cUnit, rARG0, rARG2); // .ne case - arg0 <= class
1279 opReg(cUnit, kOpBlx, rTgt); // .ne case: helper(class, ref->class)
Ian Rogersab2b55d2012-03-18 00:06:11 -07001280 oatFreeTemp(cUnit, rTgt);
Ian Rogers6cbb2bd2012-03-16 13:45:30 -07001281#else
1282 opRegCopy(cUnit, rARG0, rARG2);
Ian Rogersab2b55d2012-03-18 00:06:11 -07001283 opThreadMem(cUnit, kOpBlx,
1284 OFFSETOF_MEMBER(Thread, pInstanceofNonTrivialFromCode));
Ian Rogers6cbb2bd2012-03-16 13:45:30 -07001285#endif
buzbee31a4a6f2012-02-28 15:36:15 -08001286#endif
buzbee0398c422012-03-02 15:22:47 -08001287 oatClobberCalleeSave(cUnit);
1288 /* branch targets here */
buzbee31a4a6f2012-02-28 15:36:15 -08001289 LIR* target = newLIR0(cUnit, kPseudoTargetLabel);
Ian Rogersf7d9ad32012-03-13 18:45:39 -07001290 RegLocation rlResult = oatGetReturn(cUnit, false);
buzbee31a4a6f2012-02-28 15:36:15 -08001291 storeValue(cUnit, rlDest, rlResult);
buzbee0398c422012-03-02 15:22:47 -08001292 branch1->target = target;
1293#if !defined(TARGET_ARM)
1294 branchover->target = target;
1295#endif
buzbee31a4a6f2012-02-28 15:36:15 -08001296}
1297
1298void genCheckCast(CompilationUnit* cUnit, MIR* mir, RegLocation rlSrc)
1299{
1300 oatFlushAllRegs(cUnit);
1301 // May generate a call - use explicit registers
1302 oatLockCallTemps(cUnit);
1303 uint32_t type_idx = mir->dalvikInsn.vB;
1304 loadCurrMethodDirect(cUnit, rARG1); // rARG1 <= current Method*
1305 int classReg = rARG2; // rARG2 will hold the Class*
1306 if (!cUnit->compiler->CanAccessTypeWithoutChecks(cUnit->method_idx,
1307 cUnit->dex_cache,
1308 *cUnit->dex_file,
1309 type_idx)) {
1310 // Check we have access to type_idx and if not throw IllegalAccessError,
1311 // returns Class* in rRET0
Ian Rogersab2b55d2012-03-18 00:06:11 -07001312 // InitializeTypeAndVerifyAccess(idx, method)
1313 callRuntimeHelperImmReg(cUnit,
1314 OFFSETOF_MEMBER(Thread,
1315 pInitializeTypeAndVerifyAccessFromCode),
1316 type_idx, rARG1);
buzbee82488f52012-03-02 08:20:26 -08001317 opRegCopy(cUnit, classReg, rRET0); // Align usage with fast path
buzbee31a4a6f2012-02-28 15:36:15 -08001318 } else {
1319 // Load dex cache entry into classReg (rARG2)
1320 loadWordDisp(cUnit, rARG1,
1321 Method::DexCacheResolvedTypesOffset().Int32Value(),
1322 classReg);
1323 int32_t offset_of_type =
1324 Array::DataOffset(sizeof(Class*)).Int32Value() +
1325 (sizeof(Class*) * type_idx);
1326 loadWordDisp(cUnit, classReg, offset_of_type, classReg);
1327 if (!cUnit->compiler->CanAssumeTypeIsPresentInDexCache(
1328 cUnit->dex_cache, type_idx)) {
1329 // Need to test presence of type in dex cache at runtime
buzbee82488f52012-03-02 08:20:26 -08001330 LIR* hopBranch = opCmpImmBranch(cUnit, kCondNe, classReg, 0, NULL);
buzbee31a4a6f2012-02-28 15:36:15 -08001331 // Not resolved
buzbee5de34942012-03-01 14:51:57 -08001332 // Call out to helper, which will return resolved type in rARG0
Ian Rogersab2b55d2012-03-18 00:06:11 -07001333 // InitializeTypeFromCode(idx, method)
1334 callRuntimeHelperImmReg(cUnit,
1335 OFFSETOF_MEMBER(Thread, pInitializeTypeFromCode),
1336 type_idx, rARG1);
buzbee82488f52012-03-02 08:20:26 -08001337 opRegCopy(cUnit, classReg, rARG0); // Align usage with fast path
buzbee31a4a6f2012-02-28 15:36:15 -08001338 // Rejoin code paths
1339 LIR* hopTarget = newLIR0(cUnit, kPseudoTargetLabel);
buzbee31a4a6f2012-02-28 15:36:15 -08001340 hopBranch->target = (LIR*)hopTarget;
1341 }
1342 }
buzbee5de34942012-03-01 14:51:57 -08001343 // At this point, classReg (rARG2) has class
buzbee31a4a6f2012-02-28 15:36:15 -08001344 loadValueDirectFixed(cUnit, rlSrc, rARG0); // rARG0 <= ref
1345 /* Null is OK - continue */
buzbee82488f52012-03-02 08:20:26 -08001346 LIR* branch1 = opCmpImmBranch(cUnit, kCondEq, rARG0, 0, NULL);
buzbee31a4a6f2012-02-28 15:36:15 -08001347 /* load object->clazz */
1348 DCHECK_EQ(Object::ClassOffset().Int32Value(), 0);
1349 loadWordDisp(cUnit, rARG0, Object::ClassOffset().Int32Value(), rARG1);
1350 /* rARG1 now contains object->clazz */
Ian Rogersab2b55d2012-03-18 00:06:11 -07001351#if defined(TARGET_MIPS) || defined(TARGET_X86)
buzbee82488f52012-03-02 08:20:26 -08001352 LIR* branch2 = opCmpBranch(cUnit, kCondEq, rARG1, classReg, NULL);
Ian Rogersab2b55d2012-03-18 00:06:11 -07001353 callRuntimeHelperRegReg(cUnit, OFFSETOF_MEMBER(Thread,
1354 pCheckCastFromCode),
1355 rARG1, rARG2);
1356#else // defined(TARGET_ARM)
Ian Rogers6cbb2bd2012-03-16 13:45:30 -07001357 int rTgt = loadHelper(cUnit, OFFSETOF_MEMBER(Thread,
1358 pCheckCastFromCode));
1359 opRegReg(cUnit, kOpCmp, rARG1, classReg);
1360 LIR* branch2 = opCondBranch(cUnit, kCondEq, NULL); /* If eq, trivial yes */
1361 opRegCopy(cUnit, rARG0, rARG1);
1362 opRegCopy(cUnit, rARG1, rARG2);
Ian Rogersab2b55d2012-03-18 00:06:11 -07001363 oatClobberCalleeSave(cUnit);
1364 opReg(cUnit, kOpBlx, rTgt);
Ian Rogers6cbb2bd2012-03-16 13:45:30 -07001365 oatFreeTemp(cUnit, rTgt);
Ian Rogers6cbb2bd2012-03-16 13:45:30 -07001366#endif
buzbee31a4a6f2012-02-28 15:36:15 -08001367 /* branch target here */
1368 LIR* target = newLIR0(cUnit, kPseudoTargetLabel);
buzbee31a4a6f2012-02-28 15:36:15 -08001369 branch1->target = (LIR*)target;
1370 branch2->target = (LIR*)target;
1371}
1372
buzbee31a4a6f2012-02-28 15:36:15 -08001373/*
1374 * Generate array store
1375 *
1376 */
1377void genArrayObjPut(CompilationUnit* cUnit, MIR* mir, RegLocation rlArray,
1378 RegLocation rlIndex, RegLocation rlSrc, int scale)
1379{
1380 RegisterClass regClass = oatRegClassBySize(kWord);
1381 int lenOffset = Array::LengthOffset().Int32Value();
1382 int dataOffset = Array::DataOffset(sizeof(Object*)).Int32Value();
1383
1384 oatFlushAllRegs(cUnit);
1385 /* Make sure it's a legal object Put. Use direct regs at first */
1386 loadValueDirectFixed(cUnit, rlArray, rARG1);
1387 loadValueDirectFixed(cUnit, rlSrc, rARG0);
1388
1389 /* null array object? */
1390 genNullCheck(cUnit, rlArray.sRegLow, rARG1, mir);
buzbee31a4a6f2012-02-28 15:36:15 -08001391 /* Get the array's clazz */
1392 loadWordDisp(cUnit, rARG1, Object::ClassOffset().Int32Value(), rARG1);
Ian Rogersab2b55d2012-03-18 00:06:11 -07001393 callRuntimeHelperRegReg(cUnit, OFFSETOF_MEMBER(Thread,
1394 pCanPutArrayElementFromCode),
1395 rARG0, rARG1);
buzbee31a4a6f2012-02-28 15:36:15 -08001396 oatFreeTemp(cUnit, rARG0);
1397 oatFreeTemp(cUnit, rARG1);
1398
1399 // Now, redo loadValues in case they didn't survive the call
1400
buzbee31a4a6f2012-02-28 15:36:15 -08001401 rlArray = loadValue(cUnit, rlArray, kCoreReg);
1402 rlIndex = loadValue(cUnit, rlIndex, kCoreReg);
1403
Ian Rogersb41b33b2012-03-20 14:22:54 -07001404#if defined(TARGET_X86)
1405 if (!(mir->optimizationFlags & MIR_IGNORE_RANGE_CHECK)) {
1406 /* if (rlIndex >= [rlArray + lenOffset]) goto kThrowArrayBounds */
1407 genRegMemCheck(cUnit, kCondUge, rlIndex.lowReg, rlArray.lowReg,
1408 lenOffset, mir, kThrowArrayBounds);
1409 }
1410 rlSrc = loadValue(cUnit, rlSrc, regClass);
1411 storeBaseIndexedDisp(cUnit, NULL, rlArray.lowReg, rlIndex.lowReg, scale,
1412 dataOffset, rlSrc.lowReg, INVALID_REG, kWord,
1413 INVALID_SREG);
1414#else
1415 int regPtr;
buzbee31a4a6f2012-02-28 15:36:15 -08001416 if (oatIsTemp(cUnit, rlArray.lowReg)) {
1417 oatClobber(cUnit, rlArray.lowReg);
1418 regPtr = rlArray.lowReg;
1419 } else {
1420 regPtr = oatAllocTemp(cUnit);
buzbee82488f52012-03-02 08:20:26 -08001421 opRegCopy(cUnit, regPtr, rlArray.lowReg);
buzbee31a4a6f2012-02-28 15:36:15 -08001422 }
1423
buzbee239c4e72012-03-16 08:42:29 -07001424 bool needsRangeCheck = (!(mir->optimizationFlags & MIR_IGNORE_RANGE_CHECK));
1425 int regLen = INVALID_REG;
1426 if (needsRangeCheck) {
1427 regLen = oatAllocTemp(cUnit);
buzbee31a4a6f2012-02-28 15:36:15 -08001428 //NOTE: max live temps(4) here.
1429 /* Get len */
1430 loadWordDisp(cUnit, rlArray.lowReg, lenOffset, regLen);
buzbee239c4e72012-03-16 08:42:29 -07001431 }
1432 /* regPtr -> array data */
1433 opRegImm(cUnit, kOpAdd, regPtr, dataOffset);
1434 /* at this point, regPtr points to array, 2 live temps */
1435 rlSrc = loadValue(cUnit, rlSrc, regClass);
1436 if (needsRangeCheck) {
buzbee31a4a6f2012-02-28 15:36:15 -08001437 genRegRegCheck(cUnit, kCondCs, rlIndex.lowReg, regLen, mir,
1438 kThrowArrayBounds);
1439 oatFreeTemp(cUnit, regLen);
buzbee31a4a6f2012-02-28 15:36:15 -08001440 }
buzbee31a4a6f2012-02-28 15:36:15 -08001441 storeBaseIndexed(cUnit, regPtr, rlIndex.lowReg, rlSrc.lowReg,
1442 scale, kWord);
Ian Rogersb41b33b2012-03-20 14:22:54 -07001443#endif
buzbeea7c12682012-03-19 13:13:53 -07001444 markGCCard(cUnit, rlSrc.lowReg, rlArray.lowReg);
buzbee31a4a6f2012-02-28 15:36:15 -08001445}
1446
1447/*
1448 * Generate array load
1449 */
1450void genArrayGet(CompilationUnit* cUnit, MIR* mir, OpSize size,
1451 RegLocation rlArray, RegLocation rlIndex,
1452 RegLocation rlDest, int scale)
1453{
1454 RegisterClass regClass = oatRegClassBySize(size);
1455 int lenOffset = Array::LengthOffset().Int32Value();
1456 int dataOffset;
1457 RegLocation rlResult;
1458 rlArray = loadValue(cUnit, rlArray, kCoreReg);
1459 rlIndex = loadValue(cUnit, rlIndex, kCoreReg);
buzbee31a4a6f2012-02-28 15:36:15 -08001460
1461 if (size == kLong || size == kDouble) {
1462 dataOffset = Array::DataOffset(sizeof(int64_t)).Int32Value();
1463 } else {
1464 dataOffset = Array::DataOffset(sizeof(int32_t)).Int32Value();
1465 }
1466
1467 /* null object? */
1468 genNullCheck(cUnit, rlArray.sRegLow, rlArray.lowReg, mir);
1469
Ian Rogersb5d09b22012-03-06 22:14:17 -08001470#if defined(TARGET_X86)
1471 if (!(mir->optimizationFlags & MIR_IGNORE_RANGE_CHECK)) {
1472 /* if (rlIndex >= [rlArray + lenOffset]) goto kThrowArrayBounds */
1473 genRegMemCheck(cUnit, kCondUge, rlIndex.lowReg, rlArray.lowReg,
1474 lenOffset, mir, kThrowArrayBounds);
1475 }
1476 if ((size == kLong) || (size == kDouble)) {
1477 rlResult = oatEvalLoc(cUnit, rlDest, regClass, true);
1478 loadBaseIndexedDisp(cUnit, NULL, rlArray.lowReg, rlIndex.lowReg, scale, dataOffset,
1479 rlResult.lowReg, rlResult.highReg, size, INVALID_SREG);
buzbee31a4a6f2012-02-28 15:36:15 -08001480
Ian Rogersb5d09b22012-03-06 22:14:17 -08001481 storeValueWide(cUnit, rlDest, rlResult);
1482 } else {
1483 rlResult = oatEvalLoc(cUnit, rlDest, regClass, true);
1484
1485 loadBaseIndexedDisp(cUnit, NULL, rlArray.lowReg, rlIndex.lowReg, scale, dataOffset,
1486 rlResult.lowReg, INVALID_REG, size, INVALID_SREG);
1487
1488 storeValue(cUnit, rlDest, rlResult);
1489 }
1490#else
1491 int regPtr = oatAllocTemp(cUnit);
buzbee239c4e72012-03-16 08:42:29 -07001492 bool needsRangeCheck = (!(mir->optimizationFlags & MIR_IGNORE_RANGE_CHECK));
1493 int regLen = INVALID_REG;
1494 if (needsRangeCheck) {
1495 regLen = oatAllocTemp(cUnit);
buzbee31a4a6f2012-02-28 15:36:15 -08001496 /* Get len */
1497 loadWordDisp(cUnit, rlArray.lowReg, lenOffset, regLen);
buzbee31a4a6f2012-02-28 15:36:15 -08001498 }
buzbee239c4e72012-03-16 08:42:29 -07001499 /* regPtr -> array data */
1500 opRegRegImm(cUnit, kOpAdd, regPtr, rlArray.lowReg, dataOffset);
buzbee31a4a6f2012-02-28 15:36:15 -08001501 oatFreeTemp(cUnit, rlArray.lowReg);
1502 if ((size == kLong) || (size == kDouble)) {
1503 if (scale) {
1504 int rNewIndex = oatAllocTemp(cUnit);
1505 opRegRegImm(cUnit, kOpLsl, rNewIndex, rlIndex.lowReg, scale);
1506 opRegReg(cUnit, kOpAdd, regPtr, rNewIndex);
1507 oatFreeTemp(cUnit, rNewIndex);
1508 } else {
1509 opRegReg(cUnit, kOpAdd, regPtr, rlIndex.lowReg);
1510 }
1511 oatFreeTemp(cUnit, rlIndex.lowReg);
1512 rlResult = oatEvalLoc(cUnit, rlDest, regClass, true);
1513
buzbee239c4e72012-03-16 08:42:29 -07001514 if (needsRangeCheck) {
1515 // TODO: change kCondCS to a more meaningful name, is the sense of
1516 // carry-set/clear flipped?
1517 genRegRegCheck(cUnit, kCondCs, rlIndex.lowReg, regLen, mir,
1518 kThrowArrayBounds);
1519 oatFreeTemp(cUnit, regLen);
1520 }
buzbee31a4a6f2012-02-28 15:36:15 -08001521 loadPair(cUnit, regPtr, rlResult.lowReg, rlResult.highReg);
1522
1523 oatFreeTemp(cUnit, regPtr);
1524 storeValueWide(cUnit, rlDest, rlResult);
1525 } else {
1526 rlResult = oatEvalLoc(cUnit, rlDest, regClass, true);
1527
buzbee239c4e72012-03-16 08:42:29 -07001528 if (needsRangeCheck) {
1529 // TODO: change kCondCS to a more meaningful name, is the sense of
1530 // carry-set/clear flipped?
1531 genRegRegCheck(cUnit, kCondCs, rlIndex.lowReg, regLen, mir,
1532 kThrowArrayBounds);
1533 oatFreeTemp(cUnit, regLen);
1534 }
buzbee31a4a6f2012-02-28 15:36:15 -08001535 loadBaseIndexed(cUnit, regPtr, rlIndex.lowReg, rlResult.lowReg,
1536 scale, size);
1537
1538 oatFreeTemp(cUnit, regPtr);
1539 storeValue(cUnit, rlDest, rlResult);
1540 }
Ian Rogersb5d09b22012-03-06 22:14:17 -08001541#endif
buzbee31a4a6f2012-02-28 15:36:15 -08001542}
1543
1544/*
1545 * Generate array store
1546 *
1547 */
1548void genArrayPut(CompilationUnit* cUnit, MIR* mir, OpSize size,
1549 RegLocation rlArray, RegLocation rlIndex,
1550 RegLocation rlSrc, int scale)
1551{
1552 RegisterClass regClass = oatRegClassBySize(size);
1553 int lenOffset = Array::LengthOffset().Int32Value();
1554 int dataOffset;
1555
1556 if (size == kLong || size == kDouble) {
1557 dataOffset = Array::DataOffset(sizeof(int64_t)).Int32Value();
1558 } else {
1559 dataOffset = Array::DataOffset(sizeof(int32_t)).Int32Value();
1560 }
1561
buzbee31a4a6f2012-02-28 15:36:15 -08001562 rlArray = loadValue(cUnit, rlArray, kCoreReg);
1563 rlIndex = loadValue(cUnit, rlIndex, kCoreReg);
Ian Rogersb41b33b2012-03-20 14:22:54 -07001564#if !defined(TARGET_X86)
1565 int regPtr;
buzbee31a4a6f2012-02-28 15:36:15 -08001566 if (oatIsTemp(cUnit, rlArray.lowReg)) {
1567 oatClobber(cUnit, rlArray.lowReg);
1568 regPtr = rlArray.lowReg;
1569 } else {
1570 regPtr = oatAllocTemp(cUnit);
buzbee82488f52012-03-02 08:20:26 -08001571 opRegCopy(cUnit, regPtr, rlArray.lowReg);
buzbee31a4a6f2012-02-28 15:36:15 -08001572 }
Ian Rogersb41b33b2012-03-20 14:22:54 -07001573#endif
buzbee31a4a6f2012-02-28 15:36:15 -08001574
1575 /* null object? */
1576 genNullCheck(cUnit, rlArray.sRegLow, rlArray.lowReg, mir);
1577
Ian Rogersb41b33b2012-03-20 14:22:54 -07001578#if defined(TARGET_X86)
1579 if (!(mir->optimizationFlags & MIR_IGNORE_RANGE_CHECK)) {
1580 /* if (rlIndex >= [rlArray + lenOffset]) goto kThrowArrayBounds */
1581 genRegMemCheck(cUnit, kCondUge, rlIndex.lowReg, rlArray.lowReg,
1582 lenOffset, mir, kThrowArrayBounds);
1583 }
1584 rlSrc = loadValue(cUnit, rlSrc, regClass);
1585 storeBaseIndexedDisp(cUnit, NULL, rlArray.lowReg, rlIndex.lowReg, scale, dataOffset,
1586 rlSrc.lowReg, rlSrc.highReg, size, INVALID_SREG);
1587#else
buzbee239c4e72012-03-16 08:42:29 -07001588 bool needsRangeCheck = (!(mir->optimizationFlags & MIR_IGNORE_RANGE_CHECK));
1589 int regLen = INVALID_REG;
1590 if (needsRangeCheck) {
1591 regLen = oatAllocTemp(cUnit);
buzbee31a4a6f2012-02-28 15:36:15 -08001592 //NOTE: max live temps(4) here.
1593 /* Get len */
1594 loadWordDisp(cUnit, rlArray.lowReg, lenOffset, regLen);
buzbee31a4a6f2012-02-28 15:36:15 -08001595 }
buzbee239c4e72012-03-16 08:42:29 -07001596 /* regPtr -> array data */
1597 opRegImm(cUnit, kOpAdd, regPtr, dataOffset);
buzbee31a4a6f2012-02-28 15:36:15 -08001598 /* at this point, regPtr points to array, 2 live temps */
1599 if ((size == kLong) || (size == kDouble)) {
1600 //TUNING: specific wide routine that can handle fp regs
1601 if (scale) {
1602 int rNewIndex = oatAllocTemp(cUnit);
1603 opRegRegImm(cUnit, kOpLsl, rNewIndex, rlIndex.lowReg, scale);
1604 opRegReg(cUnit, kOpAdd, regPtr, rNewIndex);
1605 oatFreeTemp(cUnit, rNewIndex);
1606 } else {
1607 opRegReg(cUnit, kOpAdd, regPtr, rlIndex.lowReg);
1608 }
1609 rlSrc = loadValueWide(cUnit, rlSrc, regClass);
1610
buzbee239c4e72012-03-16 08:42:29 -07001611 if (needsRangeCheck) {
1612 genRegRegCheck(cUnit, kCondCs, rlIndex.lowReg, regLen, mir,
1613 kThrowArrayBounds);
1614 oatFreeTemp(cUnit, regLen);
1615 }
1616
buzbee31a4a6f2012-02-28 15:36:15 -08001617 storePair(cUnit, regPtr, rlSrc.lowReg, rlSrc.highReg);
1618
1619 oatFreeTemp(cUnit, regPtr);
1620 } else {
1621 rlSrc = loadValue(cUnit, rlSrc, regClass);
buzbee239c4e72012-03-16 08:42:29 -07001622 if (needsRangeCheck) {
1623 genRegRegCheck(cUnit, kCondCs, rlIndex.lowReg, regLen, mir,
1624 kThrowArrayBounds);
1625 oatFreeTemp(cUnit, regLen);
1626 }
buzbee31a4a6f2012-02-28 15:36:15 -08001627 storeBaseIndexed(cUnit, regPtr, rlIndex.lowReg, rlSrc.lowReg,
1628 scale, size);
1629 }
Ian Rogersb41b33b2012-03-20 14:22:54 -07001630#endif
buzbee31a4a6f2012-02-28 15:36:15 -08001631}
1632
1633void genLong3Addr(CompilationUnit* cUnit, MIR* mir, OpKind firstOp,
1634 OpKind secondOp, RegLocation rlDest,
1635 RegLocation rlSrc1, RegLocation rlSrc2)
1636{
1637 RegLocation rlResult;
1638#if defined(TARGET_ARM)
1639 /*
1640 * NOTE: This is the one place in the code in which we might have
1641 * as many as six live temporary registers. There are 5 in the normal
1642 * set for Arm. Until we have spill capabilities, temporarily add
1643 * lr to the temp set. It is safe to do this locally, but note that
1644 * lr is used explicitly elsewhere in the code generator and cannot
1645 * normally be used as a general temp register.
1646 */
1647 oatMarkTemp(cUnit, rLR); // Add lr to the temp pool
1648 oatFreeTemp(cUnit, rLR); // and make it available
1649#endif
1650 rlSrc1 = loadValueWide(cUnit, rlSrc1, kCoreReg);
1651 rlSrc2 = loadValueWide(cUnit, rlSrc2, kCoreReg);
1652 rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
1653 // The longs may overlap - use intermediate temp if so
1654 if (rlResult.lowReg == rlSrc1.highReg) {
1655 int tReg = oatAllocTemp(cUnit);
buzbee82488f52012-03-02 08:20:26 -08001656 opRegCopy(cUnit, tReg, rlSrc1.highReg);
buzbee31a4a6f2012-02-28 15:36:15 -08001657 opRegRegReg(cUnit, firstOp, rlResult.lowReg, rlSrc1.lowReg,
1658 rlSrc2.lowReg);
1659 opRegRegReg(cUnit, secondOp, rlResult.highReg, tReg,
1660 rlSrc2.highReg);
1661 oatFreeTemp(cUnit, tReg);
1662 } else {
1663 opRegRegReg(cUnit, firstOp, rlResult.lowReg, rlSrc1.lowReg,
1664 rlSrc2.lowReg);
1665 opRegRegReg(cUnit, secondOp, rlResult.highReg, rlSrc1.highReg,
1666 rlSrc2.highReg);
1667 }
1668 /*
1669 * NOTE: If rlDest refers to a frame variable in a large frame, the
1670 * following storeValueWide might need to allocate a temp register.
1671 * To further work around the lack of a spill capability, explicitly
1672 * free any temps from rlSrc1 & rlSrc2 that aren't still live in rlResult.
1673 * Remove when spill is functional.
1674 */
1675 freeRegLocTemps(cUnit, rlResult, rlSrc1);
1676 freeRegLocTemps(cUnit, rlResult, rlSrc2);
1677 storeValueWide(cUnit, rlDest, rlResult);
1678#if defined(TARGET_ARM)
1679 oatClobber(cUnit, rLR);
1680 oatUnmarkTemp(cUnit, rLR); // Remove lr from the temp pool
1681#endif
1682}
1683
1684
1685bool genShiftOpLong(CompilationUnit* cUnit, MIR* mir, RegLocation rlDest,
1686 RegLocation rlSrc1, RegLocation rlShift)
1687{
1688 int funcOffset;
1689
1690 switch( mir->dalvikInsn.opcode) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001691 case Instruction::SHL_LONG:
1692 case Instruction::SHL_LONG_2ADDR:
buzbee31a4a6f2012-02-28 15:36:15 -08001693 funcOffset = OFFSETOF_MEMBER(Thread, pShlLong);
1694 break;
Elliott Hughesadb8c672012-03-06 16:49:32 -08001695 case Instruction::SHR_LONG:
1696 case Instruction::SHR_LONG_2ADDR:
buzbee31a4a6f2012-02-28 15:36:15 -08001697 funcOffset = OFFSETOF_MEMBER(Thread, pShrLong);
1698 break;
Elliott Hughesadb8c672012-03-06 16:49:32 -08001699 case Instruction::USHR_LONG:
1700 case Instruction::USHR_LONG_2ADDR:
buzbee31a4a6f2012-02-28 15:36:15 -08001701 funcOffset = OFFSETOF_MEMBER(Thread, pUshrLong);
1702 break;
1703 default:
1704 LOG(FATAL) << "Unexpected case";
1705 return true;
1706 }
1707 oatFlushAllRegs(cUnit); /* Send everything to home location */
Ian Rogersab2b55d2012-03-18 00:06:11 -07001708 callRuntimeHelperRegLocationRegLocation(cUnit, funcOffset, rlSrc1, rlShift);
Ian Rogersf7d9ad32012-03-13 18:45:39 -07001709 RegLocation rlResult = oatGetReturnWide(cUnit, false);
buzbee31a4a6f2012-02-28 15:36:15 -08001710 storeValueWide(cUnit, rlDest, rlResult);
1711 return false;
1712}
1713
1714
1715bool genArithOpInt(CompilationUnit* cUnit, MIR* mir, RegLocation rlDest,
1716 RegLocation rlSrc1, RegLocation rlSrc2)
1717{
1718 OpKind op = kOpBkpt;
1719 bool callOut = false;
1720 bool checkZero = false;
1721 bool unary = false;
buzbee31a4a6f2012-02-28 15:36:15 -08001722 RegLocation rlResult;
1723 bool shiftOp = false;
Ian Rogersb5d09b22012-03-06 22:14:17 -08001724 int funcOffset;
1725 int retReg = rRET0;
buzbee31a4a6f2012-02-28 15:36:15 -08001726 switch (mir->dalvikInsn.opcode) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001727 case Instruction::NEG_INT:
buzbee31a4a6f2012-02-28 15:36:15 -08001728 op = kOpNeg;
1729 unary = true;
1730 break;
Elliott Hughesadb8c672012-03-06 16:49:32 -08001731 case Instruction::NOT_INT:
buzbee31a4a6f2012-02-28 15:36:15 -08001732 op = kOpMvn;
1733 unary = true;
1734 break;
Elliott Hughesadb8c672012-03-06 16:49:32 -08001735 case Instruction::ADD_INT:
1736 case Instruction::ADD_INT_2ADDR:
buzbee31a4a6f2012-02-28 15:36:15 -08001737 op = kOpAdd;
1738 break;
Elliott Hughesadb8c672012-03-06 16:49:32 -08001739 case Instruction::SUB_INT:
1740 case Instruction::SUB_INT_2ADDR:
buzbee31a4a6f2012-02-28 15:36:15 -08001741 op = kOpSub;
1742 break;
Elliott Hughesadb8c672012-03-06 16:49:32 -08001743 case Instruction::MUL_INT:
1744 case Instruction::MUL_INT_2ADDR:
buzbee31a4a6f2012-02-28 15:36:15 -08001745 op = kOpMul;
1746 break;
Elliott Hughesadb8c672012-03-06 16:49:32 -08001747 case Instruction::DIV_INT:
1748 case Instruction::DIV_INT_2ADDR:
buzbee31a4a6f2012-02-28 15:36:15 -08001749 checkZero = true;
Ian Rogersb5d09b22012-03-06 22:14:17 -08001750 op = kOpDiv;
1751 callOut = true;
buzbee31a4a6f2012-02-28 15:36:15 -08001752 funcOffset = OFFSETOF_MEMBER(Thread, pIdiv);
1753 retReg = rRET0;
1754 break;
buzbee5de34942012-03-01 14:51:57 -08001755 /* NOTE: returns in rARG1 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08001756 case Instruction::REM_INT:
1757 case Instruction::REM_INT_2ADDR:
buzbee31a4a6f2012-02-28 15:36:15 -08001758 checkZero = true;
Ian Rogersb5d09b22012-03-06 22:14:17 -08001759 op = kOpRem;
1760 callOut = true;
buzbee31a4a6f2012-02-28 15:36:15 -08001761 funcOffset = OFFSETOF_MEMBER(Thread, pIdivmod);
1762 retReg = rRET1;
1763 break;
Elliott Hughesadb8c672012-03-06 16:49:32 -08001764 case Instruction::AND_INT:
1765 case Instruction::AND_INT_2ADDR:
buzbee31a4a6f2012-02-28 15:36:15 -08001766 op = kOpAnd;
1767 break;
Elliott Hughesadb8c672012-03-06 16:49:32 -08001768 case Instruction::OR_INT:
1769 case Instruction::OR_INT_2ADDR:
buzbee31a4a6f2012-02-28 15:36:15 -08001770 op = kOpOr;
1771 break;
Elliott Hughesadb8c672012-03-06 16:49:32 -08001772 case Instruction::XOR_INT:
1773 case Instruction::XOR_INT_2ADDR:
buzbee31a4a6f2012-02-28 15:36:15 -08001774 op = kOpXor;
1775 break;
Elliott Hughesadb8c672012-03-06 16:49:32 -08001776 case Instruction::SHL_INT:
1777 case Instruction::SHL_INT_2ADDR:
buzbee31a4a6f2012-02-28 15:36:15 -08001778 shiftOp = true;
1779 op = kOpLsl;
1780 break;
Elliott Hughesadb8c672012-03-06 16:49:32 -08001781 case Instruction::SHR_INT:
1782 case Instruction::SHR_INT_2ADDR:
buzbee31a4a6f2012-02-28 15:36:15 -08001783 shiftOp = true;
1784 op = kOpAsr;
1785 break;
Elliott Hughesadb8c672012-03-06 16:49:32 -08001786 case Instruction::USHR_INT:
1787 case Instruction::USHR_INT_2ADDR:
buzbee31a4a6f2012-02-28 15:36:15 -08001788 shiftOp = true;
1789 op = kOpLsr;
1790 break;
1791 default:
1792 LOG(FATAL) << "Invalid word arith op: " <<
1793 (int)mir->dalvikInsn.opcode;
1794 }
1795 if (!callOut) {
1796 rlSrc1 = loadValue(cUnit, rlSrc1, kCoreReg);
1797 if (unary) {
1798 rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
1799 opRegReg(cUnit, op, rlResult.lowReg,
1800 rlSrc1.lowReg);
1801 } else {
1802 rlSrc2 = loadValue(cUnit, rlSrc2, kCoreReg);
Ian Rogersb5d09b22012-03-06 22:14:17 -08001803#if defined(TARGET_X86)
1804 rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
1805 opRegRegReg(cUnit, op, rlResult.lowReg,
1806 rlSrc1.lowReg, rlSrc2.lowReg);
1807#else
buzbee31a4a6f2012-02-28 15:36:15 -08001808 if (shiftOp) {
1809 int tReg = oatAllocTemp(cUnit);
1810 opRegRegImm(cUnit, kOpAnd, tReg, rlSrc2.lowReg, 31);
1811 rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
1812 opRegRegReg(cUnit, op, rlResult.lowReg,
1813 rlSrc1.lowReg, tReg);
1814 oatFreeTemp(cUnit, tReg);
1815 } else {
1816 rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
1817 opRegRegReg(cUnit, op, rlResult.lowReg,
1818 rlSrc1.lowReg, rlSrc2.lowReg);
1819 }
Ian Rogersb5d09b22012-03-06 22:14:17 -08001820#endif
buzbee31a4a6f2012-02-28 15:36:15 -08001821 }
1822 storeValue(cUnit, rlDest, rlResult);
1823 } else {
1824 RegLocation rlResult;
1825 oatFlushAllRegs(cUnit); /* Send everything to home location */
1826 loadValueDirectFixed(cUnit, rlSrc2, rRET1);
Ian Rogers6cbb2bd2012-03-16 13:45:30 -07001827#if !defined(TARGET_X86)
buzbee31a4a6f2012-02-28 15:36:15 -08001828 int rTgt = loadHelper(cUnit, funcOffset);
Ian Rogersab2b55d2012-03-18 00:06:11 -07001829#endif
buzbee31a4a6f2012-02-28 15:36:15 -08001830 loadValueDirectFixed(cUnit, rlSrc1, rARG0);
1831 if (checkZero) {
1832 genImmedCheck(cUnit, kCondEq, rARG1, 0, mir, kThrowDivZero);
1833 }
Ian Rogersab2b55d2012-03-18 00:06:11 -07001834#if !defined(TARGET_X86)
1835 opReg(cUnit, kOpBlx, rTgt);
Ian Rogers6cbb2bd2012-03-16 13:45:30 -07001836 oatFreeTemp(cUnit, rTgt);
1837#else
Ian Rogersab2b55d2012-03-18 00:06:11 -07001838 opThreadMem(cUnit, kOpBlx, funcOffset);
Ian Rogers6cbb2bd2012-03-16 13:45:30 -07001839#endif
buzbee31a4a6f2012-02-28 15:36:15 -08001840 if (retReg == rRET0)
Ian Rogersf7d9ad32012-03-13 18:45:39 -07001841 rlResult = oatGetReturn(cUnit, false);
buzbee31a4a6f2012-02-28 15:36:15 -08001842 else
1843 rlResult = oatGetReturnAlt(cUnit);
1844 storeValue(cUnit, rlDest, rlResult);
1845 }
1846 return false;
1847}
1848
1849/*
1850 * The following are the first-level codegen routines that analyze the format
1851 * of each bytecode then either dispatch special purpose codegen routines
1852 * or produce corresponding Thumb instructions directly.
1853 */
1854
1855bool isPowerOfTwo(int x)
1856{
1857 return (x & (x - 1)) == 0;
1858}
1859
1860// Returns true if no more than two bits are set in 'x'.
1861bool isPopCountLE2(unsigned int x)
1862{
1863 x &= x - 1;
1864 return (x & (x - 1)) == 0;
1865}
1866
1867// Returns the index of the lowest set bit in 'x'.
1868int lowestSetBit(unsigned int x) {
1869 int bit_posn = 0;
1870 while ((x & 0xf) == 0) {
1871 bit_posn += 4;
1872 x >>= 4;
1873 }
1874 while ((x & 1) == 0) {
1875 bit_posn++;
1876 x >>= 1;
1877 }
1878 return bit_posn;
1879}
1880
1881// Returns true if it added instructions to 'cUnit' to divide 'rlSrc' by 'lit'
1882// and store the result in 'rlDest'.
Elliott Hughesadb8c672012-03-06 16:49:32 -08001883bool handleEasyDivide(CompilationUnit* cUnit, Instruction::Code dalvikOpcode,
buzbee31a4a6f2012-02-28 15:36:15 -08001884 RegLocation rlSrc, RegLocation rlDest, int lit)
1885{
1886 if (lit < 2 || !isPowerOfTwo(lit)) {
1887 return false;
1888 }
1889 int k = lowestSetBit(lit);
1890 if (k >= 30) {
1891 // Avoid special cases.
1892 return false;
1893 }
Elliott Hughesadb8c672012-03-06 16:49:32 -08001894 bool div = (dalvikOpcode == Instruction::DIV_INT_LIT8 ||
1895 dalvikOpcode == Instruction::DIV_INT_LIT16);
buzbee31a4a6f2012-02-28 15:36:15 -08001896 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
1897 RegLocation rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
1898 if (div) {
1899 int tReg = oatAllocTemp(cUnit);
1900 if (lit == 2) {
1901 // Division by 2 is by far the most common division by constant.
1902 opRegRegImm(cUnit, kOpLsr, tReg, rlSrc.lowReg, 32 - k);
1903 opRegRegReg(cUnit, kOpAdd, tReg, tReg, rlSrc.lowReg);
1904 opRegRegImm(cUnit, kOpAsr, rlResult.lowReg, tReg, k);
1905 } else {
1906 opRegRegImm(cUnit, kOpAsr, tReg, rlSrc.lowReg, 31);
1907 opRegRegImm(cUnit, kOpLsr, tReg, tReg, 32 - k);
1908 opRegRegReg(cUnit, kOpAdd, tReg, tReg, rlSrc.lowReg);
1909 opRegRegImm(cUnit, kOpAsr, rlResult.lowReg, tReg, k);
1910 }
1911 } else {
buzbee31a4a6f2012-02-28 15:36:15 -08001912 int tReg1 = oatAllocTemp(cUnit);
1913 int tReg2 = oatAllocTemp(cUnit);
1914 if (lit == 2) {
1915 opRegRegImm(cUnit, kOpLsr, tReg1, rlSrc.lowReg, 32 - k);
1916 opRegRegReg(cUnit, kOpAdd, tReg2, tReg1, rlSrc.lowReg);
Ian Rogersb41b33b2012-03-20 14:22:54 -07001917 opRegRegImm(cUnit, kOpAnd, tReg2, tReg2, lit -1);
buzbee31a4a6f2012-02-28 15:36:15 -08001918 opRegRegReg(cUnit, kOpSub, rlResult.lowReg, tReg2, tReg1);
1919 } else {
1920 opRegRegImm(cUnit, kOpAsr, tReg1, rlSrc.lowReg, 31);
1921 opRegRegImm(cUnit, kOpLsr, tReg1, tReg1, 32 - k);
1922 opRegRegReg(cUnit, kOpAdd, tReg2, tReg1, rlSrc.lowReg);
Ian Rogersb41b33b2012-03-20 14:22:54 -07001923 opRegRegImm(cUnit, kOpAnd, tReg2, tReg2, lit - 1);
buzbee31a4a6f2012-02-28 15:36:15 -08001924 opRegRegReg(cUnit, kOpSub, rlResult.lowReg, tReg2, tReg1);
1925 }
1926 }
1927 storeValue(cUnit, rlDest, rlResult);
1928 return true;
1929}
1930
1931void genMultiplyByTwoBitMultiplier(CompilationUnit* cUnit, RegLocation rlSrc,
1932 RegLocation rlResult, int lit,
1933 int firstBit, int secondBit)
1934{
buzbee0398c422012-03-02 15:22:47 -08001935#if defined(TARGET_ARM)
buzbee31a4a6f2012-02-28 15:36:15 -08001936 opRegRegRegShift(cUnit, kOpAdd, rlResult.lowReg, rlSrc.lowReg, rlSrc.lowReg,
1937 encodeShift(kArmLsl, secondBit - firstBit));
buzbee0398c422012-03-02 15:22:47 -08001938#else
1939 int tReg = oatAllocTemp(cUnit);
1940 opRegRegImm(cUnit, kOpLsl, tReg, rlSrc.lowReg, secondBit - firstBit);
1941 opRegRegReg(cUnit, kOpAdd, rlResult.lowReg, rlSrc.lowReg, tReg);
1942 oatFreeTemp(cUnit, tReg);
buzbee5de34942012-03-01 14:51:57 -08001943#endif
buzbee31a4a6f2012-02-28 15:36:15 -08001944 if (firstBit != 0) {
1945 opRegRegImm(cUnit, kOpLsl, rlResult.lowReg, rlResult.lowReg, firstBit);
1946 }
1947}
1948
1949// Returns true if it added instructions to 'cUnit' to multiply 'rlSrc' by 'lit'
1950// and store the result in 'rlDest'.
1951bool handleEasyMultiply(CompilationUnit* cUnit, RegLocation rlSrc,
1952 RegLocation rlDest, int lit)
1953{
1954 // Can we simplify this multiplication?
1955 bool powerOfTwo = false;
1956 bool popCountLE2 = false;
1957 bool powerOfTwoMinusOne = false;
1958 if (lit < 2) {
1959 // Avoid special cases.
1960 return false;
1961 } else if (isPowerOfTwo(lit)) {
1962 powerOfTwo = true;
1963 } else if (isPopCountLE2(lit)) {
1964 popCountLE2 = true;
1965 } else if (isPowerOfTwo(lit + 1)) {
1966 powerOfTwoMinusOne = true;
1967 } else {
1968 return false;
1969 }
1970 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
1971 RegLocation rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
1972 if (powerOfTwo) {
1973 // Shift.
1974 opRegRegImm(cUnit, kOpLsl, rlResult.lowReg, rlSrc.lowReg,
1975 lowestSetBit(lit));
1976 } else if (popCountLE2) {
1977 // Shift and add and shift.
1978 int firstBit = lowestSetBit(lit);
1979 int secondBit = lowestSetBit(lit ^ (1 << firstBit));
1980 genMultiplyByTwoBitMultiplier(cUnit, rlSrc, rlResult, lit,
1981 firstBit, secondBit);
1982 } else {
1983 // Reverse subtract: (src << (shift + 1)) - src.
1984 DCHECK(powerOfTwoMinusOne);
1985 // TUNING: rsb dst, src, src lsl#lowestSetBit(lit + 1)
1986 int tReg = oatAllocTemp(cUnit);
1987 opRegRegImm(cUnit, kOpLsl, tReg, rlSrc.lowReg, lowestSetBit(lit + 1));
1988 opRegRegReg(cUnit, kOpSub, rlResult.lowReg, tReg, rlSrc.lowReg);
1989 }
1990 storeValue(cUnit, rlDest, rlResult);
1991 return true;
1992}
1993
1994bool genArithOpIntLit(CompilationUnit* cUnit, MIR* mir, RegLocation rlDest,
1995 RegLocation rlSrc, int lit)
1996{
Elliott Hughesadb8c672012-03-06 16:49:32 -08001997 Instruction::Code dalvikOpcode = mir->dalvikInsn.opcode;
buzbee31a4a6f2012-02-28 15:36:15 -08001998 RegLocation rlResult;
1999 OpKind op = (OpKind)0; /* Make gcc happy */
2000 int shiftOp = false;
2001 bool isDiv = false;
2002 int funcOffset;
buzbee31a4a6f2012-02-28 15:36:15 -08002003
2004 switch (dalvikOpcode) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002005 case Instruction::RSUB_INT_LIT8:
2006 case Instruction::RSUB_INT: {
buzbee31a4a6f2012-02-28 15:36:15 -08002007 int tReg;
2008 //TUNING: add support for use of Arm rsub op
2009 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
2010 tReg = oatAllocTemp(cUnit);
2011 loadConstant(cUnit, tReg, lit);
2012 rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
2013 opRegRegReg(cUnit, kOpSub, rlResult.lowReg,
2014 tReg, rlSrc.lowReg);
2015 storeValue(cUnit, rlDest, rlResult);
2016 return false;
2017 break;
2018 }
2019
Elliott Hughesadb8c672012-03-06 16:49:32 -08002020 case Instruction::ADD_INT_LIT8:
2021 case Instruction::ADD_INT_LIT16:
buzbee31a4a6f2012-02-28 15:36:15 -08002022 op = kOpAdd;
2023 break;
Elliott Hughesadb8c672012-03-06 16:49:32 -08002024 case Instruction::MUL_INT_LIT8:
2025 case Instruction::MUL_INT_LIT16: {
buzbee31a4a6f2012-02-28 15:36:15 -08002026 if (handleEasyMultiply(cUnit, rlSrc, rlDest, lit)) {
2027 return false;
2028 }
2029 op = kOpMul;
2030 break;
2031 }
Elliott Hughesadb8c672012-03-06 16:49:32 -08002032 case Instruction::AND_INT_LIT8:
2033 case Instruction::AND_INT_LIT16:
buzbee31a4a6f2012-02-28 15:36:15 -08002034 op = kOpAnd;
2035 break;
Elliott Hughesadb8c672012-03-06 16:49:32 -08002036 case Instruction::OR_INT_LIT8:
2037 case Instruction::OR_INT_LIT16:
buzbee31a4a6f2012-02-28 15:36:15 -08002038 op = kOpOr;
2039 break;
Elliott Hughesadb8c672012-03-06 16:49:32 -08002040 case Instruction::XOR_INT_LIT8:
2041 case Instruction::XOR_INT_LIT16:
buzbee31a4a6f2012-02-28 15:36:15 -08002042 op = kOpXor;
2043 break;
Elliott Hughesadb8c672012-03-06 16:49:32 -08002044 case Instruction::SHL_INT_LIT8:
buzbee31a4a6f2012-02-28 15:36:15 -08002045 lit &= 31;
2046 shiftOp = true;
2047 op = kOpLsl;
2048 break;
Elliott Hughesadb8c672012-03-06 16:49:32 -08002049 case Instruction::SHR_INT_LIT8:
buzbee31a4a6f2012-02-28 15:36:15 -08002050 lit &= 31;
2051 shiftOp = true;
2052 op = kOpAsr;
2053 break;
Elliott Hughesadb8c672012-03-06 16:49:32 -08002054 case Instruction::USHR_INT_LIT8:
buzbee31a4a6f2012-02-28 15:36:15 -08002055 lit &= 31;
2056 shiftOp = true;
2057 op = kOpLsr;
2058 break;
2059
Elliott Hughesadb8c672012-03-06 16:49:32 -08002060 case Instruction::DIV_INT_LIT8:
2061 case Instruction::DIV_INT_LIT16:
2062 case Instruction::REM_INT_LIT8:
2063 case Instruction::REM_INT_LIT16:
buzbee31a4a6f2012-02-28 15:36:15 -08002064 if (lit == 0) {
2065 genImmedCheck(cUnit, kCondAl, 0, 0, mir, kThrowDivZero);
2066 return false;
2067 }
2068 if (handleEasyDivide(cUnit, dalvikOpcode, rlSrc, rlDest, lit)) {
2069 return false;
2070 }
2071 oatFlushAllRegs(cUnit); /* Everything to home location */
2072 loadValueDirectFixed(cUnit, rlSrc, rARG0);
2073 oatClobber(cUnit, rARG0);
Elliott Hughesadb8c672012-03-06 16:49:32 -08002074 if ((dalvikOpcode == Instruction::DIV_INT_LIT8) ||
2075 (dalvikOpcode == Instruction::DIV_INT_LIT16)) {
buzbee31a4a6f2012-02-28 15:36:15 -08002076 funcOffset = OFFSETOF_MEMBER(Thread, pIdiv);
2077 isDiv = true;
2078 } else {
2079 funcOffset = OFFSETOF_MEMBER(Thread, pIdivmod);
2080 isDiv = false;
2081 }
Ian Rogersab2b55d2012-03-18 00:06:11 -07002082 callRuntimeHelperRegImm(cUnit, funcOffset, rARG0, lit);
buzbee31a4a6f2012-02-28 15:36:15 -08002083 if (isDiv)
Ian Rogersf7d9ad32012-03-13 18:45:39 -07002084 rlResult = oatGetReturn(cUnit, false);
buzbee31a4a6f2012-02-28 15:36:15 -08002085 else
2086 rlResult = oatGetReturnAlt(cUnit);
2087 storeValue(cUnit, rlDest, rlResult);
2088 return false;
2089 break;
2090 default:
2091 return true;
2092 }
2093 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
2094 rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
2095 // Avoid shifts by literal 0 - no support in Thumb. Change to copy
2096 if (shiftOp && (lit == 0)) {
buzbee82488f52012-03-02 08:20:26 -08002097 opRegCopy(cUnit, rlResult.lowReg, rlSrc.lowReg);
buzbee31a4a6f2012-02-28 15:36:15 -08002098 } else {
2099 opRegRegImm(cUnit, op, rlResult.lowReg, rlSrc.lowReg, lit);
2100 }
2101 storeValue(cUnit, rlDest, rlResult);
2102 return false;
2103}
2104
2105bool genArithOpLong(CompilationUnit* cUnit, MIR* mir, RegLocation rlDest,
2106 RegLocation rlSrc1, RegLocation rlSrc2)
2107{
2108 RegLocation rlResult;
2109 OpKind firstOp = kOpBkpt;
2110 OpKind secondOp = kOpBkpt;
2111 bool callOut = false;
2112 bool checkZero = false;
2113 int funcOffset;
2114 int retReg = rRET0;
2115
2116 switch (mir->dalvikInsn.opcode) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002117 case Instruction::NOT_LONG:
buzbee31a4a6f2012-02-28 15:36:15 -08002118 rlSrc2 = loadValueWide(cUnit, rlSrc2, kCoreReg);
2119 rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
2120 // Check for destructive overlap
2121 if (rlResult.lowReg == rlSrc2.highReg) {
2122 int tReg = oatAllocTemp(cUnit);
buzbee82488f52012-03-02 08:20:26 -08002123 opRegCopy(cUnit, tReg, rlSrc2.highReg);
buzbee31a4a6f2012-02-28 15:36:15 -08002124 opRegReg(cUnit, kOpMvn, rlResult.lowReg, rlSrc2.lowReg);
2125 opRegReg(cUnit, kOpMvn, rlResult.highReg, tReg);
2126 oatFreeTemp(cUnit, tReg);
2127 } else {
2128 opRegReg(cUnit, kOpMvn, rlResult.lowReg, rlSrc2.lowReg);
2129 opRegReg(cUnit, kOpMvn, rlResult.highReg, rlSrc2.highReg);
2130 }
2131 storeValueWide(cUnit, rlDest, rlResult);
2132 return false;
2133 break;
Elliott Hughesadb8c672012-03-06 16:49:32 -08002134 case Instruction::ADD_LONG:
2135 case Instruction::ADD_LONG_2ADDR:
buzbeec5159d52012-03-03 11:48:39 -08002136#if defined(TARGET_MIPS)
2137 return genAddLong(cUnit, mir, rlDest, rlSrc1, rlSrc2);
2138#else
buzbee31a4a6f2012-02-28 15:36:15 -08002139 firstOp = kOpAdd;
2140 secondOp = kOpAdc;
2141 break;
buzbeec5159d52012-03-03 11:48:39 -08002142#endif
Elliott Hughesadb8c672012-03-06 16:49:32 -08002143 case Instruction::SUB_LONG:
2144 case Instruction::SUB_LONG_2ADDR:
buzbeec5159d52012-03-03 11:48:39 -08002145#if defined(TARGET_MIPS)
2146 return genSubLong(cUnit, mir, rlDest, rlSrc1, rlSrc2);
2147#else
buzbee31a4a6f2012-02-28 15:36:15 -08002148 firstOp = kOpSub;
2149 secondOp = kOpSbc;
2150 break;
buzbeec5159d52012-03-03 11:48:39 -08002151#endif
Elliott Hughesadb8c672012-03-06 16:49:32 -08002152 case Instruction::MUL_LONG:
2153 case Instruction::MUL_LONG_2ADDR:
buzbee31a4a6f2012-02-28 15:36:15 -08002154 callOut = true;
2155 retReg = rRET0;
2156 funcOffset = OFFSETOF_MEMBER(Thread, pLmul);
2157 break;
Elliott Hughesadb8c672012-03-06 16:49:32 -08002158 case Instruction::DIV_LONG:
2159 case Instruction::DIV_LONG_2ADDR:
buzbee31a4a6f2012-02-28 15:36:15 -08002160 callOut = true;
2161 checkZero = true;
2162 retReg = rRET0;
2163 funcOffset = OFFSETOF_MEMBER(Thread, pLdivmod);
2164 break;
2165 /* NOTE - result is in rARG2/rARG3 instead of rRET0/rRET1 */
2166 // FIXME: is true, or could be made true, or other targets?
Elliott Hughesadb8c672012-03-06 16:49:32 -08002167 case Instruction::REM_LONG:
2168 case Instruction::REM_LONG_2ADDR:
buzbee31a4a6f2012-02-28 15:36:15 -08002169 callOut = true;
2170 checkZero = true;
2171 funcOffset = OFFSETOF_MEMBER(Thread, pLdivmod);
2172 retReg = rARG2;
2173 break;
Elliott Hughesadb8c672012-03-06 16:49:32 -08002174 case Instruction::AND_LONG_2ADDR:
2175 case Instruction::AND_LONG:
buzbee31a4a6f2012-02-28 15:36:15 -08002176 firstOp = kOpAnd;
2177 secondOp = kOpAnd;
2178 break;
Elliott Hughesadb8c672012-03-06 16:49:32 -08002179 case Instruction::OR_LONG:
2180 case Instruction::OR_LONG_2ADDR:
buzbee31a4a6f2012-02-28 15:36:15 -08002181 firstOp = kOpOr;
2182 secondOp = kOpOr;
2183 break;
Elliott Hughesadb8c672012-03-06 16:49:32 -08002184 case Instruction::XOR_LONG:
2185 case Instruction::XOR_LONG_2ADDR:
buzbee31a4a6f2012-02-28 15:36:15 -08002186 firstOp = kOpXor;
2187 secondOp = kOpXor;
2188 break;
Elliott Hughesadb8c672012-03-06 16:49:32 -08002189 case Instruction::NEG_LONG: {
buzbeec5159d52012-03-03 11:48:39 -08002190 return genNegLong(cUnit, mir, rlDest, rlSrc2);
buzbee31a4a6f2012-02-28 15:36:15 -08002191 }
2192 default:
2193 LOG(FATAL) << "Invalid long arith op";
2194 }
2195 if (!callOut) {
2196 genLong3Addr(cUnit, mir, firstOp, secondOp, rlDest, rlSrc1, rlSrc2);
2197 } else {
buzbee31a4a6f2012-02-28 15:36:15 -08002198 oatFlushAllRegs(cUnit); /* Send everything to home location */
2199 if (checkZero) {
Ian Rogersb5d09b22012-03-06 22:14:17 -08002200#if defined(TARGET_X86)
2201 UNIMPLEMENTED(FATAL);
2202#else
buzbee31a4a6f2012-02-28 15:36:15 -08002203 loadValueDirectWideFixed(cUnit, rlSrc2, rARG2, rARG3);
Ian Rogersab2b55d2012-03-18 00:06:11 -07002204 int rTgt = loadHelper(cUnit, funcOffset);
Ian Rogers6cbb2bd2012-03-16 13:45:30 -07002205#endif
buzbee31a4a6f2012-02-28 15:36:15 -08002206 loadValueDirectWideFixed(cUnit, rlSrc1, rARG0, rARG1);
2207 int tReg = oatAllocTemp(cUnit);
2208#if defined(TARGET_ARM)
2209 newLIR4(cUnit, kThumb2OrrRRRs, tReg, rARG2, rARG3, 0);
2210 oatFreeTemp(cUnit, tReg);
2211 genCheck(cUnit, kCondEq, mir, kThrowDivZero);
2212#else
Ian Rogersb5d09b22012-03-06 22:14:17 -08002213#if defined(TARGET_X86)
2214 UNIMPLEMENTED(FATAL);
2215#else
buzbee31a4a6f2012-02-28 15:36:15 -08002216 opRegRegReg(cUnit, kOpOr, tReg, rARG2, rARG3);
Ian Rogersb5d09b22012-03-06 22:14:17 -08002217#endif
buzbee5de34942012-03-01 14:51:57 -08002218 genImmedCheck(cUnit, kCondEq, tReg, 0, mir, kThrowDivZero);
buzbee31a4a6f2012-02-28 15:36:15 -08002219 oatFreeTemp(cUnit, tReg);
2220#endif
Ian Rogersab2b55d2012-03-18 00:06:11 -07002221#if !defined(TARGET_X86)
2222 opReg(cUnit, kOpBlx, rTgt);
2223 oatFreeTemp(cUnit, rTgt);
Ian Rogersb5d09b22012-03-06 22:14:17 -08002224#endif
Ian Rogersab2b55d2012-03-18 00:06:11 -07002225 } else {
2226 callRuntimeHelperRegLocationRegLocation(cUnit, funcOffset,
2227 rlSrc1, rlSrc2);
buzbee31a4a6f2012-02-28 15:36:15 -08002228 }
buzbee31a4a6f2012-02-28 15:36:15 -08002229 // Adjust return regs in to handle case of rem returning rARG2/rARG3
2230 if (retReg == rRET0)
Ian Rogersf7d9ad32012-03-13 18:45:39 -07002231 rlResult = oatGetReturnWide(cUnit, false);
buzbee31a4a6f2012-02-28 15:36:15 -08002232 else
2233 rlResult = oatGetReturnWideAlt(cUnit);
2234 storeValueWide(cUnit, rlDest, rlResult);
2235 }
2236 return false;
2237}
2238
2239bool genConversionCall(CompilationUnit* cUnit, MIR* mir, int funcOffset,
2240 int srcSize, int tgtSize)
2241{
2242 /*
2243 * Don't optimize the register usage since it calls out to support
2244 * functions
2245 */
2246 RegLocation rlSrc;
2247 RegLocation rlDest;
2248 oatFlushAllRegs(cUnit); /* Send everything to home location */
buzbee31a4a6f2012-02-28 15:36:15 -08002249 if (srcSize == 1) {
2250 rlSrc = oatGetSrc(cUnit, mir, 0);
2251 loadValueDirectFixed(cUnit, rlSrc, rARG0);
2252 } else {
2253 rlSrc = oatGetSrcWide(cUnit, mir, 0, 1);
2254 loadValueDirectWideFixed(cUnit, rlSrc, rARG0, rARG1);
2255 }
Ian Rogersab2b55d2012-03-18 00:06:11 -07002256 callRuntimeHelperRegLocation(cUnit, funcOffset, rlSrc);
buzbee31a4a6f2012-02-28 15:36:15 -08002257 if (tgtSize == 1) {
2258 RegLocation rlResult;
2259 rlDest = oatGetDest(cUnit, mir, 0);
Ian Rogersf7d9ad32012-03-13 18:45:39 -07002260 rlResult = oatGetReturn(cUnit, rlDest.fp);
buzbee31a4a6f2012-02-28 15:36:15 -08002261 storeValue(cUnit, rlDest, rlResult);
2262 } else {
2263 RegLocation rlResult;
2264 rlDest = oatGetDestWide(cUnit, mir, 0, 1);
Ian Rogersf7d9ad32012-03-13 18:45:39 -07002265 rlResult = oatGetReturnWide(cUnit, rlDest.fp);
buzbee31a4a6f2012-02-28 15:36:15 -08002266 storeValueWide(cUnit, rlDest, rlResult);
2267 }
2268 return false;
2269}
2270
2271void genNegFloat(CompilationUnit* cUnit, RegLocation rlDest, RegLocation rlSrc);
2272bool genArithOpFloatPortable(CompilationUnit* cUnit, MIR* mir,
2273 RegLocation rlDest, RegLocation rlSrc1,
2274 RegLocation rlSrc2)
2275{
2276 RegLocation rlResult;
2277 int funcOffset;
2278
2279 switch (mir->dalvikInsn.opcode) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002280 case Instruction::ADD_FLOAT_2ADDR:
2281 case Instruction::ADD_FLOAT:
buzbee31a4a6f2012-02-28 15:36:15 -08002282 funcOffset = OFFSETOF_MEMBER(Thread, pFadd);
2283 break;
Elliott Hughesadb8c672012-03-06 16:49:32 -08002284 case Instruction::SUB_FLOAT_2ADDR:
2285 case Instruction::SUB_FLOAT:
buzbee31a4a6f2012-02-28 15:36:15 -08002286 funcOffset = OFFSETOF_MEMBER(Thread, pFsub);
2287 break;
Elliott Hughesadb8c672012-03-06 16:49:32 -08002288 case Instruction::DIV_FLOAT_2ADDR:
2289 case Instruction::DIV_FLOAT:
buzbee31a4a6f2012-02-28 15:36:15 -08002290 funcOffset = OFFSETOF_MEMBER(Thread, pFdiv);
2291 break;
Elliott Hughesadb8c672012-03-06 16:49:32 -08002292 case Instruction::MUL_FLOAT_2ADDR:
2293 case Instruction::MUL_FLOAT:
buzbee31a4a6f2012-02-28 15:36:15 -08002294 funcOffset = OFFSETOF_MEMBER(Thread, pFmul);
2295 break;
Elliott Hughesadb8c672012-03-06 16:49:32 -08002296 case Instruction::REM_FLOAT_2ADDR:
2297 case Instruction::REM_FLOAT:
buzbee31a4a6f2012-02-28 15:36:15 -08002298 funcOffset = OFFSETOF_MEMBER(Thread, pFmodf);
2299 break;
Elliott Hughesadb8c672012-03-06 16:49:32 -08002300 case Instruction::NEG_FLOAT: {
buzbee31a4a6f2012-02-28 15:36:15 -08002301 genNegFloat(cUnit, rlDest, rlSrc1);
2302 return false;
2303 }
2304 default:
2305 return true;
2306 }
2307 oatFlushAllRegs(cUnit); /* Send everything to home location */
Ian Rogersab2b55d2012-03-18 00:06:11 -07002308 callRuntimeHelperRegLocationRegLocation(cUnit, funcOffset, rlSrc1, rlSrc2);
Ian Rogersf7d9ad32012-03-13 18:45:39 -07002309 rlResult = oatGetReturn(cUnit, true);
buzbee31a4a6f2012-02-28 15:36:15 -08002310 storeValue(cUnit, rlDest, rlResult);
2311 return false;
2312}
2313
2314void genNegDouble(CompilationUnit* cUnit, RegLocation rlDst, RegLocation rlSrc);
2315bool genArithOpDoublePortable(CompilationUnit* cUnit, MIR* mir,
2316 RegLocation rlDest, RegLocation rlSrc1,
2317 RegLocation rlSrc2)
2318{
2319 RegLocation rlResult;
2320 int funcOffset;
2321
2322 switch (mir->dalvikInsn.opcode) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002323 case Instruction::ADD_DOUBLE_2ADDR:
2324 case Instruction::ADD_DOUBLE:
buzbee31a4a6f2012-02-28 15:36:15 -08002325 funcOffset = OFFSETOF_MEMBER(Thread, pDadd);
2326 break;
Elliott Hughesadb8c672012-03-06 16:49:32 -08002327 case Instruction::SUB_DOUBLE_2ADDR:
2328 case Instruction::SUB_DOUBLE:
buzbee31a4a6f2012-02-28 15:36:15 -08002329 funcOffset = OFFSETOF_MEMBER(Thread, pDsub);
2330 break;
Elliott Hughesadb8c672012-03-06 16:49:32 -08002331 case Instruction::DIV_DOUBLE_2ADDR:
2332 case Instruction::DIV_DOUBLE:
buzbee31a4a6f2012-02-28 15:36:15 -08002333 funcOffset = OFFSETOF_MEMBER(Thread, pDdiv);
2334 break;
Elliott Hughesadb8c672012-03-06 16:49:32 -08002335 case Instruction::MUL_DOUBLE_2ADDR:
2336 case Instruction::MUL_DOUBLE:
buzbee31a4a6f2012-02-28 15:36:15 -08002337 funcOffset = OFFSETOF_MEMBER(Thread, pDmul);
2338 break;
Elliott Hughesadb8c672012-03-06 16:49:32 -08002339 case Instruction::REM_DOUBLE_2ADDR:
2340 case Instruction::REM_DOUBLE:
buzbee31a4a6f2012-02-28 15:36:15 -08002341 funcOffset = OFFSETOF_MEMBER(Thread, pFmod);
2342 break;
Elliott Hughesadb8c672012-03-06 16:49:32 -08002343 case Instruction::NEG_DOUBLE: {
buzbee31a4a6f2012-02-28 15:36:15 -08002344 genNegDouble(cUnit, rlDest, rlSrc1);
2345 return false;
2346 }
2347 default:
2348 return true;
2349 }
2350 oatFlushAllRegs(cUnit); /* Send everything to home location */
Ian Rogersab2b55d2012-03-18 00:06:11 -07002351 callRuntimeHelperRegLocationRegLocation(cUnit, funcOffset, rlSrc1, rlSrc2);
Ian Rogersf7d9ad32012-03-13 18:45:39 -07002352 rlResult = oatGetReturnWide(cUnit, true);
buzbee31a4a6f2012-02-28 15:36:15 -08002353 storeValueWide(cUnit, rlDest, rlResult);
2354 return false;
2355}
2356
2357bool genConversionPortable(CompilationUnit* cUnit, MIR* mir)
2358{
Elliott Hughesadb8c672012-03-06 16:49:32 -08002359 Instruction::Code opcode = mir->dalvikInsn.opcode;
buzbee31a4a6f2012-02-28 15:36:15 -08002360
2361 switch (opcode) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002362 case Instruction::INT_TO_FLOAT:
buzbee31a4a6f2012-02-28 15:36:15 -08002363 return genConversionCall(cUnit, mir, OFFSETOF_MEMBER(Thread, pI2f),
2364 1, 1);
Elliott Hughesadb8c672012-03-06 16:49:32 -08002365 case Instruction::FLOAT_TO_INT:
buzbee31a4a6f2012-02-28 15:36:15 -08002366 return genConversionCall(cUnit, mir, OFFSETOF_MEMBER(Thread, pF2iz),
2367 1, 1);
Elliott Hughesadb8c672012-03-06 16:49:32 -08002368 case Instruction::DOUBLE_TO_FLOAT:
buzbee31a4a6f2012-02-28 15:36:15 -08002369 return genConversionCall(cUnit, mir, OFFSETOF_MEMBER(Thread, pD2f),
2370 2, 1);
Elliott Hughesadb8c672012-03-06 16:49:32 -08002371 case Instruction::FLOAT_TO_DOUBLE:
buzbee31a4a6f2012-02-28 15:36:15 -08002372 return genConversionCall(cUnit, mir, OFFSETOF_MEMBER(Thread, pF2d),
2373 1, 2);
Elliott Hughesadb8c672012-03-06 16:49:32 -08002374 case Instruction::INT_TO_DOUBLE:
buzbee31a4a6f2012-02-28 15:36:15 -08002375 return genConversionCall(cUnit, mir, OFFSETOF_MEMBER(Thread, pI2d),
2376 1, 2);
Elliott Hughesadb8c672012-03-06 16:49:32 -08002377 case Instruction::DOUBLE_TO_INT:
buzbee31a4a6f2012-02-28 15:36:15 -08002378 return genConversionCall(cUnit, mir, OFFSETOF_MEMBER(Thread, pD2iz),
2379 2, 1);
Elliott Hughesadb8c672012-03-06 16:49:32 -08002380 case Instruction::FLOAT_TO_LONG:
buzbee31a4a6f2012-02-28 15:36:15 -08002381 return genConversionCall(cUnit, mir, OFFSETOF_MEMBER(Thread,
2382 pF2l), 1, 2);
Elliott Hughesadb8c672012-03-06 16:49:32 -08002383 case Instruction::LONG_TO_FLOAT:
buzbee31a4a6f2012-02-28 15:36:15 -08002384 return genConversionCall(cUnit, mir, OFFSETOF_MEMBER(Thread, pL2f),
2385 2, 1);
Elliott Hughesadb8c672012-03-06 16:49:32 -08002386 case Instruction::DOUBLE_TO_LONG:
buzbee31a4a6f2012-02-28 15:36:15 -08002387 return genConversionCall(cUnit, mir, OFFSETOF_MEMBER(Thread,
2388 pD2l), 2, 2);
Elliott Hughesadb8c672012-03-06 16:49:32 -08002389 case Instruction::LONG_TO_DOUBLE:
buzbee31a4a6f2012-02-28 15:36:15 -08002390 return genConversionCall(cUnit, mir, OFFSETOF_MEMBER(Thread, pL2d),
2391 2, 2);
2392 default:
2393 return true;
2394 }
2395 return false;
2396}
2397
2398/*
2399 * Generate callout to updateDebugger. Note that we're overloading
2400 * the use of rSUSPEND here. When the debugger is active, this
2401 * register holds the address of the update function. So, if it's
2402 * non-null, we call out to it.
2403 *
2404 * Note also that rRET0 and rRET1 must be preserved across this
2405 * code. This must be handled by the stub.
2406 */
2407void genDebuggerUpdate(CompilationUnit* cUnit, int32_t offset)
2408{
2409 // Following DCHECK verifies that dPC is in range of single load immediate
2410 DCHECK((offset == DEBUGGER_METHOD_ENTRY) ||
2411 (offset == DEBUGGER_METHOD_EXIT) || ((offset & 0xffff) == offset));
2412 oatClobberCalleeSave(cUnit);
2413#if defined(TARGET_ARM)
2414 opRegImm(cUnit, kOpCmp, rSUSPEND, 0);
buzbee82488f52012-03-02 08:20:26 -08002415 opIT(cUnit, kArmCondNe, "T");
buzbee31a4a6f2012-02-28 15:36:15 -08002416 loadConstant(cUnit, rARG2, offset); // arg2 <- Entry code
2417 opReg(cUnit, kOpBlx, rSUSPEND);
Ian Rogersb5d09b22012-03-06 22:14:17 -08002418#elif defined(TARGET_X86)
2419 UNIMPLEMENTED(FATAL);
buzbee31a4a6f2012-02-28 15:36:15 -08002420#else
buzbee82488f52012-03-02 08:20:26 -08002421 LIR* branch = opCmpImmBranch(cUnit, kCondEq, rSUSPEND, 0, NULL);
buzbee31a4a6f2012-02-28 15:36:15 -08002422 loadConstant(cUnit, rARG2, offset);
2423 opReg(cUnit, kOpBlx, rSUSPEND);
2424 LIR* target = newLIR0(cUnit, kPseudoTargetLabel);
buzbee31a4a6f2012-02-28 15:36:15 -08002425 branch->target = (LIR*)target;
2426#endif
2427 oatFreeTemp(cUnit, rARG2);
2428}
2429
2430/* Check if we need to check for pending suspend request */
2431void genSuspendTest(CompilationUnit* cUnit, MIR* mir)
2432{
2433 if (NO_SUSPEND || (mir->optimizationFlags & MIR_IGNORE_SUSPEND_CHECK)) {
2434 return;
2435 }
2436 oatFlushAllRegs(cUnit);
buzbee31a4a6f2012-02-28 15:36:15 -08002437 if (cUnit->genDebugger) {
2438 // If generating code for the debugger, always check for suspension
Ian Rogers6cbb2bd2012-03-16 13:45:30 -07002439#if defined(TARGET_X86)
2440 UNIMPLEMENTED(FATAL);
2441#else
buzbee86a4bce2012-03-06 18:15:00 -08002442 int rTgt = loadHelper(cUnit, OFFSETOF_MEMBER(Thread,
2443 pTestSuspendFromCode));
2444 opReg(cUnit, kOpBlx, rTgt);
2445 // Refresh rSUSPEND
2446 loadWordDisp(cUnit, rSELF,
2447 OFFSETOF_MEMBER(Thread, pUpdateDebuggerFromCode),
2448 rSUSPEND);
Ian Rogersb5d09b22012-03-06 22:14:17 -08002449#endif
buzbee31a4a6f2012-02-28 15:36:15 -08002450 } else {
Ian Rogersb5d09b22012-03-06 22:14:17 -08002451 LIR* branch = NULL;
buzbee31a4a6f2012-02-28 15:36:15 -08002452#if defined(TARGET_ARM)
2453 // In non-debug case, only check periodically
2454 newLIR2(cUnit, kThumbSubRI8, rSUSPEND, 1);
buzbee82488f52012-03-02 08:20:26 -08002455 branch = opCondBranch(cUnit, kCondEq, NULL);
Ian Rogersb5d09b22012-03-06 22:14:17 -08002456#elif defined(TARGET_X86)
2457 newLIR2(cUnit, kX86Cmp32TI, Thread::SuspendCountOffset().Int32Value(), 0);
2458 branch = opCondBranch(cUnit, kCondNe, NULL);
buzbee31a4a6f2012-02-28 15:36:15 -08002459#else
2460 opRegImm(cUnit, kOpSub, rSUSPEND, 1);
buzbee82488f52012-03-02 08:20:26 -08002461 branch = opCmpImmBranch(cUnit, kCondEq, rSUSPEND, 0, NULL);
buzbee31a4a6f2012-02-28 15:36:15 -08002462#endif
buzbee86a4bce2012-03-06 18:15:00 -08002463 LIR* retLab = newLIR0(cUnit, kPseudoTargetLabel);
2464 LIR* target = rawLIR(cUnit, cUnit->currentDalvikOffset,
2465 kPseudoSuspendTarget, (intptr_t)retLab, mir->offset);
2466 branch->target = (LIR*)target;
2467 oatInsertGrowableList(cUnit, &cUnit->suspendLaunchpads, (intptr_t)target);
buzbee31a4a6f2012-02-28 15:36:15 -08002468 }
buzbee31a4a6f2012-02-28 15:36:15 -08002469}
2470
2471} // namespace art