blob: 0ef16418582a5b60fbe3e2a92a58f500b0350037 [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 */
buzbeefc9e6fa2012-03-23 15:14:29 -070024void genInvoke(CompilationUnit* cUnit, BasicBlock* bb, MIR* mir,
25 InvokeType type, bool isRange);
buzbee31a4a6f2012-02-28 15:36:15 -080026#if defined(TARGET_ARM)
buzbee82488f52012-03-02 08:20:26 -080027LIR* opIT(CompilationUnit* cUnit, ArmConditionCode cond, const char* guide);
buzbee31a4a6f2012-02-28 15:36:15 -080028#endif
29
Ian Rogersab2b55d2012-03-18 00:06:11 -070030void callRuntimeHelperImm(CompilationUnit* cUnit, int helperOffset, int arg0) {
31#if !defined(TARGET_X86)
32 int rTgt = loadHelper(cUnit, helperOffset);
33#endif
34 loadConstant(cUnit, rARG0, arg0);
buzbee31a4a6f2012-02-28 15:36:15 -080035 oatClobberCalleeSave(cUnit);
Ian Rogers6cbb2bd2012-03-16 13:45:30 -070036#if !defined(TARGET_X86)
Ian Rogersab2b55d2012-03-18 00:06:11 -070037 opReg(cUnit, kOpBlx, rTgt);
38 oatFreeTemp(cUnit, rTgt);
Ian Rogers6cbb2bd2012-03-16 13:45:30 -070039#else
Ian Rogersab2b55d2012-03-18 00:06:11 -070040 opThreadMem(cUnit, kOpBlx, helperOffset);
41#endif
42}
43
44void callRuntimeHelperRegLocation(CompilationUnit* cUnit, int helperOffset,
45 RegLocation arg0) {
46#if !defined(TARGET_X86)
47 int rTgt = loadHelper(cUnit, helperOffset);
48#endif
49 if (arg0.wide == 0) {
50 loadValueDirectFixed(cUnit, arg0, rARG0);
51 } else {
52 loadValueDirectWideFixed(cUnit, arg0, rARG0, rARG1);
53 }
54 oatClobberCalleeSave(cUnit);
55#if !defined(TARGET_X86)
56 opReg(cUnit, kOpBlx, rTgt);
57 oatFreeTemp(cUnit, rTgt);
58#else
59 opThreadMem(cUnit, kOpBlx, helperOffset);
60#endif
61}
62
63void callRuntimeHelperImmImm(CompilationUnit* cUnit, int helperOffset,
64 int arg0, int arg1) {
65#if !defined(TARGET_X86)
66 int rTgt = loadHelper(cUnit, helperOffset);
67#endif
68 loadConstant(cUnit, rARG0, arg0);
69 loadConstant(cUnit, rARG1, arg1);
70 oatClobberCalleeSave(cUnit);
71#if !defined(TARGET_X86)
72 opReg(cUnit, kOpBlx, rTgt);
73 oatFreeTemp(cUnit, rTgt);
74#else
75 opThreadMem(cUnit, kOpBlx, helperOffset);
76#endif
77}
78
79void callRuntimeHelperImmRegLocation(CompilationUnit* cUnit, int helperOffset,
80 int arg0, RegLocation arg1) {
81#if !defined(TARGET_X86)
82 int rTgt = loadHelper(cUnit, helperOffset);
83#endif
84 if (arg1.wide == 0) {
85 loadValueDirectFixed(cUnit, arg1, rARG1);
86 } else {
87 loadValueDirectWideFixed(cUnit, arg1, rARG1, rARG2);
88 }
89 loadConstant(cUnit, rARG0, arg0);
90 oatClobberCalleeSave(cUnit);
91#if !defined(TARGET_X86)
92 opReg(cUnit, kOpBlx, rTgt);
93 oatFreeTemp(cUnit, rTgt);
94#else
95 opThreadMem(cUnit, kOpBlx, helperOffset);
96#endif
97}
98
99void callRuntimeHelperRegLocationImm(CompilationUnit* cUnit, int helperOffset,
100 RegLocation arg0, int arg1) {
101#if !defined(TARGET_X86)
102 int rTgt = loadHelper(cUnit, helperOffset);
103#endif
104 loadValueDirectFixed(cUnit, arg0, rARG0);
105 loadConstant(cUnit, rARG1, arg1);
106 oatClobberCalleeSave(cUnit);
107#if !defined(TARGET_X86)
108 opReg(cUnit, kOpBlx, rTgt);
109 oatFreeTemp(cUnit, rTgt);
110#else
111 opThreadMem(cUnit, kOpBlx, helperOffset);
112#endif
113}
114
115void callRuntimeHelperImmReg(CompilationUnit* cUnit, int helperOffset,
116 int arg0, int arg1) {
117#if !defined(TARGET_X86)
118 int rTgt = loadHelper(cUnit, helperOffset);
119#endif
120 opRegCopy(cUnit, rARG1, arg1);
121 loadConstant(cUnit, rARG0, arg0);
122 oatClobberCalleeSave(cUnit);
123#if !defined(TARGET_X86)
124 opReg(cUnit, kOpBlx, rTgt);
125 oatFreeTemp(cUnit, rTgt);
126#else
127 opThreadMem(cUnit, kOpBlx, helperOffset);
128#endif
129}
130
131void callRuntimeHelperRegImm(CompilationUnit* cUnit, int helperOffset,
132 int arg0, int arg1) {
133#if !defined(TARGET_X86)
134 int rTgt = loadHelper(cUnit, helperOffset);
135#endif
136 opRegCopy(cUnit, rARG0, arg0);
137 loadConstant(cUnit, rARG1, arg1);
138 oatClobberCalleeSave(cUnit);
139#if !defined(TARGET_X86)
140 opReg(cUnit, kOpBlx, rTgt);
141 oatFreeTemp(cUnit, rTgt);
142#else
143 opThreadMem(cUnit, kOpBlx, helperOffset);
144#endif
145}
146
147void callRuntimeHelperImmMethod(CompilationUnit* cUnit, int helperOffset,
148 int arg0) {
149#if !defined(TARGET_X86)
150 int rTgt = loadHelper(cUnit, helperOffset);
151#endif
152 loadCurrMethodDirect(cUnit, rARG1);
153 loadConstant(cUnit, rARG0, arg0);
154 oatClobberCalleeSave(cUnit);
155#if !defined(TARGET_X86)
156 opReg(cUnit, kOpBlx, rTgt);
157 oatFreeTemp(cUnit, rTgt);
158#else
159 opThreadMem(cUnit, kOpBlx, helperOffset);
160#endif
161}
162
163void callRuntimeHelperRegLocationRegLocation(CompilationUnit* cUnit,
164 int helperOffset,
165 RegLocation arg0,
166 RegLocation arg1) {
167#if !defined(TARGET_X86)
168 int rTgt = loadHelper(cUnit, helperOffset);
169#endif
170 if (arg0.wide == 0) {
171 loadValueDirectFixed(cUnit, arg0, rARG0);
172 if (arg1.wide == 0) {
173 loadValueDirectFixed(cUnit, arg1, rARG1);
174 } else {
175 loadValueDirectWideFixed(cUnit, arg1, rARG1, rARG2);
176 }
177 } else {
178 loadValueDirectWideFixed(cUnit, arg0, rARG0, rARG1);
179 if (arg1.wide == 0) {
180 loadValueDirectFixed(cUnit, arg1, rARG2);
181 } else {
Ian Rogersab2b55d2012-03-18 00:06:11 -0700182 loadValueDirectWideFixed(cUnit, arg1, rARG2, rARG3);
Ian Rogersab2b55d2012-03-18 00:06:11 -0700183 }
184 }
185 oatClobberCalleeSave(cUnit);
186#if !defined(TARGET_X86)
187 opReg(cUnit, kOpBlx, rTgt);
188 oatFreeTemp(cUnit, rTgt);
189#else
190 opThreadMem(cUnit, kOpBlx, helperOffset);
191#endif
192}
193
194void callRuntimeHelperRegReg(CompilationUnit* cUnit, int helperOffset,
195 int arg0, int arg1) {
196#if !defined(TARGET_X86)
197 int rTgt = loadHelper(cUnit, helperOffset);
198#endif
199 DCHECK_NE((int)rARG0, arg1); // check copy into arg0 won't clobber arg1
200 opRegCopy(cUnit, rARG0, arg0);
201 opRegCopy(cUnit, rARG1, arg1);
202 oatClobberCalleeSave(cUnit);
203#if !defined(TARGET_X86)
204 opReg(cUnit, kOpBlx, rTgt);
205 oatFreeTemp(cUnit, rTgt);
206#else
207 opThreadMem(cUnit, kOpBlx, helperOffset);
208#endif
209}
210
211void callRuntimeHelperRegRegImm(CompilationUnit* cUnit, int helperOffset,
212 int arg0, int arg1, int arg2) {
213#if !defined(TARGET_X86)
214 int rTgt = loadHelper(cUnit, helperOffset);
215#endif
216 DCHECK_NE((int)rARG0, arg1); // check copy into arg0 won't clobber arg1
217 opRegCopy(cUnit, rARG0, arg0);
218 opRegCopy(cUnit, rARG1, arg1);
219 loadConstant(cUnit, rARG2, arg2);
220 oatClobberCalleeSave(cUnit);
221#if !defined(TARGET_X86)
222 opReg(cUnit, kOpBlx, rTgt);
223 oatFreeTemp(cUnit, rTgt);
224#else
225 opThreadMem(cUnit, kOpBlx, helperOffset);
226#endif
227}
228
229void callRuntimeHelperImmMethodRegLocation(CompilationUnit* cUnit, int helperOffset,
230 int arg0, RegLocation arg2) {
231#if !defined(TARGET_X86)
232 int rTgt = loadHelper(cUnit, helperOffset);
233#endif
234 loadValueDirectFixed(cUnit, arg2, rARG2);
235 loadCurrMethodDirect(cUnit, rARG1);
236 loadConstant(cUnit, rARG0, arg0);
237 oatClobberCalleeSave(cUnit);
238#if !defined(TARGET_X86)
239 opReg(cUnit, kOpBlx, rTgt);
240 oatFreeTemp(cUnit, rTgt);
241#else
242 opThreadMem(cUnit, kOpBlx, helperOffset);
243#endif
244}
245
246void callRuntimeHelperImmMethodImm(CompilationUnit* cUnit, int helperOffset,
247 int arg0, int arg2) {
248#if !defined(TARGET_X86)
249 int rTgt = loadHelper(cUnit, helperOffset);
250#endif
251 loadCurrMethodDirect(cUnit, rARG1);
252 loadConstant(cUnit, rARG2, arg2);
253 loadConstant(cUnit, rARG0, arg0);
254 oatClobberCalleeSave(cUnit);
255#if !defined(TARGET_X86)
256 opReg(cUnit, kOpBlx, rTgt);
257 oatFreeTemp(cUnit, rTgt);
258#else
259 opThreadMem(cUnit, kOpBlx, helperOffset);
260#endif
261}
262
263void callRuntimeHelperImmRegLocationRegLocation(CompilationUnit* cUnit,
264 int helperOffset,
265 int arg0, RegLocation arg1,
266 RegLocation arg2) {
267#if !defined(TARGET_X86)
268 int rTgt = loadHelper(cUnit, helperOffset);
269#endif
270 loadValueDirectFixed(cUnit, arg1, rARG1);
271 if (arg2.wide == 0) {
272 loadValueDirectFixed(cUnit, arg2, rARG2);
273 } else {
Ian Rogersab2b55d2012-03-18 00:06:11 -0700274 loadValueDirectWideFixed(cUnit, arg2, rARG2, rARG3);
Ian Rogersab2b55d2012-03-18 00:06:11 -0700275 }
276 loadConstant(cUnit, rARG0, arg0);
277 oatClobberCalleeSave(cUnit);
278#if !defined(TARGET_X86)
279 opReg(cUnit, kOpBlx, rTgt);
280 oatFreeTemp(cUnit, rTgt);
281#else
282 opThreadMem(cUnit, kOpBlx, helperOffset);
Ian Rogers6cbb2bd2012-03-16 13:45:30 -0700283#endif
buzbee31a4a6f2012-02-28 15:36:15 -0800284}
285
286/*
287 * Generate an kPseudoBarrier marker to indicate the boundary of special
288 * blocks.
289 */
290void genBarrier(CompilationUnit* cUnit)
291{
292 LIR* barrier = newLIR0(cUnit, kPseudoBarrier);
293 /* Mark all resources as being clobbered */
294 barrier->defMask = -1;
295}
296
buzbee31a4a6f2012-02-28 15:36:15 -0800297
298/* Generate unconditional branch instructions */
buzbee82488f52012-03-02 08:20:26 -0800299LIR* opUnconditionalBranch(CompilationUnit* cUnit, LIR* target)
buzbee31a4a6f2012-02-28 15:36:15 -0800300{
Ian Rogers680b1bd2012-03-07 20:18:49 -0800301 LIR* branch = opBranchUnconditional(cUnit, kOpUncondBr);
buzbee31a4a6f2012-02-28 15:36:15 -0800302 branch->target = (LIR*) target;
303 return branch;
304}
305
buzbee5de34942012-03-01 14:51:57 -0800306// FIXME: need to do some work to split out targets with
307// condition codes and those without
308#if defined(TARGET_ARM) || defined(TARGET_X86)
buzbee31a4a6f2012-02-28 15:36:15 -0800309LIR* genCheck(CompilationUnit* cUnit, ConditionCode cCode, MIR* mir,
310 ThrowKind kind)
311{
buzbeea2ebdd72012-03-04 14:57:06 -0800312 LIR* tgt = rawLIR(cUnit, 0, kPseudoThrowTarget, kind,
313 mir ? mir->offset : 0);
buzbee82488f52012-03-02 08:20:26 -0800314 LIR* branch = opCondBranch(cUnit, cCode, tgt);
buzbee31a4a6f2012-02-28 15:36:15 -0800315 // Remember branch target - will process later
316 oatInsertGrowableList(cUnit, &cUnit->throwLaunchpads, (intptr_t)tgt);
317 return branch;
318}
buzbee5de34942012-03-01 14:51:57 -0800319#endif
buzbee31a4a6f2012-02-28 15:36:15 -0800320
321LIR* genImmedCheck(CompilationUnit* cUnit, ConditionCode cCode,
322 int reg, int immVal, MIR* mir, ThrowKind kind)
323{
buzbeea2ebdd72012-03-04 14:57:06 -0800324 LIR* tgt = rawLIR(cUnit, 0, kPseudoThrowTarget, kind, mir->offset);
buzbee31a4a6f2012-02-28 15:36:15 -0800325 LIR* branch;
326 if (cCode == kCondAl) {
buzbee82488f52012-03-02 08:20:26 -0800327 branch = opUnconditionalBranch(cUnit, tgt);
buzbee31a4a6f2012-02-28 15:36:15 -0800328 } else {
buzbee82488f52012-03-02 08:20:26 -0800329 branch = opCmpImmBranch(cUnit, cCode, reg, immVal, tgt);
buzbee31a4a6f2012-02-28 15:36:15 -0800330 }
331 // Remember branch target - will process later
332 oatInsertGrowableList(cUnit, &cUnit->throwLaunchpads, (intptr_t)tgt);
333 return branch;
334}
335
336/* Perform null-check on a register. */
337LIR* genNullCheck(CompilationUnit* cUnit, int sReg, int mReg, MIR* mir)
338{
339 if (!(cUnit->disableOpt & (1 << kNullCheckElimination)) &&
340 mir->optimizationFlags & MIR_IGNORE_NULL_CHECK) {
341 return NULL;
342 }
343 return genImmedCheck(cUnit, kCondEq, mReg, 0, mir, kThrowNullPointer);
344}
345
346/* Perform check on two registers */
347LIR* genRegRegCheck(CompilationUnit* cUnit, ConditionCode cCode,
Ian Rogersb5d09b22012-03-06 22:14:17 -0800348 int reg1, int reg2, MIR* mir, ThrowKind kind)
buzbee31a4a6f2012-02-28 15:36:15 -0800349{
buzbeea2ebdd72012-03-04 14:57:06 -0800350 LIR* tgt = rawLIR(cUnit, 0, kPseudoThrowTarget, kind,
351 mir ? mir->offset : 0, reg1, reg2);
buzbee5de34942012-03-01 14:51:57 -0800352#if defined(TARGET_MIPS)
buzbee82488f52012-03-02 08:20:26 -0800353 LIR* branch = opCmpBranch(cUnit, cCode, reg1, reg2, tgt);
buzbee5de34942012-03-01 14:51:57 -0800354#else
buzbee31a4a6f2012-02-28 15:36:15 -0800355 opRegReg(cUnit, kOpCmp, reg1, reg2);
buzbee82488f52012-03-02 08:20:26 -0800356 LIR* branch = opCondBranch(cUnit, cCode, tgt);
buzbee5de34942012-03-01 14:51:57 -0800357#endif
buzbee31a4a6f2012-02-28 15:36:15 -0800358 // Remember branch target - will process later
359 oatInsertGrowableList(cUnit, &cUnit->throwLaunchpads, (intptr_t)tgt);
360 return branch;
361}
362
363void genCompareAndBranch(CompilationUnit* cUnit, BasicBlock* bb, MIR* mir,
364 RegLocation rlSrc1, RegLocation rlSrc2, LIR* labelList)
365{
366 ConditionCode cond;
367 rlSrc1 = loadValue(cUnit, rlSrc1, kCoreReg);
368 rlSrc2 = loadValue(cUnit, rlSrc2, kCoreReg);
Elliott Hughesadb8c672012-03-06 16:49:32 -0800369 Instruction::Code opcode = mir->dalvikInsn.opcode;
buzbee31a4a6f2012-02-28 15:36:15 -0800370 switch(opcode) {
Elliott Hughesadb8c672012-03-06 16:49:32 -0800371 case Instruction::IF_EQ:
buzbee31a4a6f2012-02-28 15:36:15 -0800372 cond = kCondEq;
373 break;
Elliott Hughesadb8c672012-03-06 16:49:32 -0800374 case Instruction::IF_NE:
buzbee31a4a6f2012-02-28 15:36:15 -0800375 cond = kCondNe;
376 break;
Elliott Hughesadb8c672012-03-06 16:49:32 -0800377 case Instruction::IF_LT:
buzbee31a4a6f2012-02-28 15:36:15 -0800378 cond = kCondLt;
379 break;
Elliott Hughesadb8c672012-03-06 16:49:32 -0800380 case Instruction::IF_GE:
buzbee31a4a6f2012-02-28 15:36:15 -0800381 cond = kCondGe;
382 break;
Elliott Hughesadb8c672012-03-06 16:49:32 -0800383 case Instruction::IF_GT:
buzbee31a4a6f2012-02-28 15:36:15 -0800384 cond = kCondGt;
385 break;
Elliott Hughesadb8c672012-03-06 16:49:32 -0800386 case Instruction::IF_LE:
buzbee31a4a6f2012-02-28 15:36:15 -0800387 cond = kCondLe;
388 break;
389 default:
390 cond = (ConditionCode)0;
391 LOG(FATAL) << "Unexpected opcode " << (int)opcode;
392 }
buzbee5de34942012-03-01 14:51:57 -0800393#if defined(TARGET_MIPS)
buzbee82488f52012-03-02 08:20:26 -0800394 opCmpBranch(cUnit, cond, rlSrc1.lowReg, rlSrc2.lowReg,
395 &labelList[bb->taken->id]);
buzbee5de34942012-03-01 14:51:57 -0800396#else
397 opRegReg(cUnit, kOpCmp, rlSrc1.lowReg, rlSrc2.lowReg);
buzbee82488f52012-03-02 08:20:26 -0800398 opCondBranch(cUnit, cond, &labelList[bb->taken->id]);
buzbee5de34942012-03-01 14:51:57 -0800399#endif
buzbee82488f52012-03-02 08:20:26 -0800400 opUnconditionalBranch(cUnit, &labelList[bb->fallThrough->id]);
buzbee31a4a6f2012-02-28 15:36:15 -0800401}
402
403void genCompareZeroAndBranch(CompilationUnit* cUnit, BasicBlock* bb, MIR* mir,
404 RegLocation rlSrc, LIR* labelList)
405{
406 ConditionCode cond;
407 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
Elliott Hughesadb8c672012-03-06 16:49:32 -0800408 Instruction::Code opcode = mir->dalvikInsn.opcode;
buzbee31a4a6f2012-02-28 15:36:15 -0800409 switch(opcode) {
Elliott Hughesadb8c672012-03-06 16:49:32 -0800410 case Instruction::IF_EQZ:
buzbee31a4a6f2012-02-28 15:36:15 -0800411 cond = kCondEq;
412 break;
Elliott Hughesadb8c672012-03-06 16:49:32 -0800413 case Instruction::IF_NEZ:
buzbee31a4a6f2012-02-28 15:36:15 -0800414 cond = kCondNe;
415 break;
Elliott Hughesadb8c672012-03-06 16:49:32 -0800416 case Instruction::IF_LTZ:
buzbee31a4a6f2012-02-28 15:36:15 -0800417 cond = kCondLt;
418 break;
Elliott Hughesadb8c672012-03-06 16:49:32 -0800419 case Instruction::IF_GEZ:
buzbee31a4a6f2012-02-28 15:36:15 -0800420 cond = kCondGe;
421 break;
Elliott Hughesadb8c672012-03-06 16:49:32 -0800422 case Instruction::IF_GTZ:
buzbee31a4a6f2012-02-28 15:36:15 -0800423 cond = kCondGt;
424 break;
Elliott Hughesadb8c672012-03-06 16:49:32 -0800425 case Instruction::IF_LEZ:
buzbee31a4a6f2012-02-28 15:36:15 -0800426 cond = kCondLe;
427 break;
428 default:
429 cond = (ConditionCode)0;
430 LOG(FATAL) << "Unexpected opcode " << (int)opcode;
431 }
buzbee5de34942012-03-01 14:51:57 -0800432#if defined(TARGET_MIPS)
buzbee82488f52012-03-02 08:20:26 -0800433 opCmpImmBranch(cUnit, cond, rlSrc.lowReg, 0, &labelList[bb->taken->id]);
buzbee5de34942012-03-01 14:51:57 -0800434#else
435 opRegImm(cUnit, kOpCmp, rlSrc.lowReg, 0);
buzbee82488f52012-03-02 08:20:26 -0800436 opCondBranch(cUnit, cond, &labelList[bb->taken->id]);
buzbee5de34942012-03-01 14:51:57 -0800437#endif
buzbee82488f52012-03-02 08:20:26 -0800438 opUnconditionalBranch(cUnit, &labelList[bb->fallThrough->id]);
buzbee31a4a6f2012-02-28 15:36:15 -0800439}
440
441void genIntToLong(CompilationUnit* cUnit, MIR* mir, RegLocation rlDest,
442 RegLocation rlSrc)
443{
444 RegLocation rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
445 if (rlSrc.location == kLocPhysReg) {
buzbee82488f52012-03-02 08:20:26 -0800446 opRegCopy(cUnit, rlResult.lowReg, rlSrc.lowReg);
buzbee31a4a6f2012-02-28 15:36:15 -0800447 } else {
448 loadValueDirect(cUnit, rlSrc, rlResult.lowReg);
449 }
450 opRegRegImm(cUnit, kOpAsr, rlResult.highReg,
451 rlResult.lowReg, 31);
452 storeValueWide(cUnit, rlDest, rlResult);
453}
454
455void genIntNarrowing(CompilationUnit* cUnit, MIR* mir, RegLocation rlDest,
456 RegLocation rlSrc)
457{
458 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
459 RegLocation rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
460 OpKind op = kOpInvalid;
461 switch(mir->dalvikInsn.opcode) {
Elliott Hughesadb8c672012-03-06 16:49:32 -0800462 case Instruction::INT_TO_BYTE:
buzbee31a4a6f2012-02-28 15:36:15 -0800463 op = kOp2Byte;
464 break;
Elliott Hughesadb8c672012-03-06 16:49:32 -0800465 case Instruction::INT_TO_SHORT:
buzbee31a4a6f2012-02-28 15:36:15 -0800466 op = kOp2Short;
467 break;
Elliott Hughesadb8c672012-03-06 16:49:32 -0800468 case Instruction::INT_TO_CHAR:
buzbee31a4a6f2012-02-28 15:36:15 -0800469 op = kOp2Char;
470 break;
471 default:
472 LOG(ERROR) << "Bad int conversion type";
473 }
474 opRegReg(cUnit, op, rlResult.lowReg, rlSrc.lowReg);
475 storeValue(cUnit, rlDest, rlResult);
476}
477
478/*
479 * Let helper function take care of everything. Will call
480 * Array::AllocFromCode(type_idx, method, count);
481 * Note: AllocFromCode will handle checks for errNegativeArraySize.
482 */
483void genNewArray(CompilationUnit* cUnit, MIR* mir, RegLocation rlDest,
484 RegLocation rlSrc)
485{
486 oatFlushAllRegs(cUnit); /* Everything to home location */
487 uint32_t type_idx = mir->dalvikInsn.vC;
Ian Rogers6cbb2bd2012-03-16 13:45:30 -0700488 int funcOffset;
buzbee31a4a6f2012-02-28 15:36:15 -0800489 if (cUnit->compiler->CanAccessTypeWithoutChecks(cUnit->method_idx,
490 cUnit->dex_cache,
491 *cUnit->dex_file,
492 type_idx)) {
Ian Rogers6cbb2bd2012-03-16 13:45:30 -0700493 funcOffset = OFFSETOF_MEMBER(Thread, pAllocArrayFromCode);
buzbee31a4a6f2012-02-28 15:36:15 -0800494 } else {
Ian Rogers6cbb2bd2012-03-16 13:45:30 -0700495 funcOffset= OFFSETOF_MEMBER(Thread, pAllocArrayFromCodeWithAccessCheck);
buzbee31a4a6f2012-02-28 15:36:15 -0800496 }
Ian Rogersab2b55d2012-03-18 00:06:11 -0700497 callRuntimeHelperImmMethodRegLocation(cUnit, funcOffset, type_idx, rlSrc);
Ian Rogersf7d9ad32012-03-13 18:45:39 -0700498 RegLocation rlResult = oatGetReturn(cUnit, false);
buzbee31a4a6f2012-02-28 15:36:15 -0800499 storeValue(cUnit, rlDest, rlResult);
500}
501
502/*
503 * Similar to genNewArray, but with post-allocation initialization.
504 * Verifier guarantees we're dealing with an array class. Current
505 * code throws runtime exception "bad Filled array req" for 'D' and 'J'.
506 * Current code also throws internal unimp if not 'L', '[' or 'I'.
507 */
508void genFilledNewArray(CompilationUnit* cUnit, MIR* mir, bool isRange)
509{
510 DecodedInstruction* dInsn = &mir->dalvikInsn;
511 int elems = dInsn->vA;
Ian Rogersab2b55d2012-03-18 00:06:11 -0700512 int typeIdx = dInsn->vB;
buzbee31a4a6f2012-02-28 15:36:15 -0800513 oatFlushAllRegs(cUnit); /* Everything to home location */
Ian Rogers6cbb2bd2012-03-16 13:45:30 -0700514 int funcOffset;
buzbee31a4a6f2012-02-28 15:36:15 -0800515 if (cUnit->compiler->CanAccessTypeWithoutChecks(cUnit->method_idx,
516 cUnit->dex_cache,
517 *cUnit->dex_file,
Ian Rogersab2b55d2012-03-18 00:06:11 -0700518 typeIdx)) {
Ian Rogers6cbb2bd2012-03-16 13:45:30 -0700519 funcOffset = OFFSETOF_MEMBER(Thread, pCheckAndAllocArrayFromCode);
buzbee31a4a6f2012-02-28 15:36:15 -0800520 } else {
Ian Rogers6cbb2bd2012-03-16 13:45:30 -0700521 funcOffset = OFFSETOF_MEMBER(Thread,
522 pCheckAndAllocArrayFromCodeWithAccessCheck);
buzbee31a4a6f2012-02-28 15:36:15 -0800523 }
Ian Rogersab2b55d2012-03-18 00:06:11 -0700524 callRuntimeHelperImmMethodImm(cUnit, funcOffset, typeIdx, elems);
buzbeee1965672012-03-11 18:39:19 -0700525 oatFreeTemp(cUnit, rARG2);
526 oatFreeTemp(cUnit, rARG1);
buzbee31a4a6f2012-02-28 15:36:15 -0800527 /*
Elliott Hughesadb8c672012-03-06 16:49:32 -0800528 * NOTE: the implicit target for Instruction::FILLED_NEW_ARRAY is the
buzbee31a4a6f2012-02-28 15:36:15 -0800529 * return region. Because AllocFromCode placed the new array
530 * in rRET0, we'll just lock it into place. When debugger support is
531 * added, it may be necessary to additionally copy all return
532 * values to a home location in thread-local storage
533 */
534 oatLockTemp(cUnit, rRET0);
535
536 // TODO: use the correct component size, currently all supported types
537 // share array alignment with ints (see comment at head of function)
538 size_t component_size = sizeof(int32_t);
539
540 // Having a range of 0 is legal
541 if (isRange && (dInsn->vA > 0)) {
542 /*
543 * Bit of ugliness here. We're going generate a mem copy loop
544 * on the register range, but it is possible that some regs
545 * in the range have been promoted. This is unlikely, but
546 * before generating the copy, we'll just force a flush
547 * of any regs in the source range that have been promoted to
548 * home location.
549 */
550 for (unsigned int i = 0; i < dInsn->vA; i++) {
551 RegLocation loc = oatUpdateLoc(cUnit,
552 oatGetSrc(cUnit, mir, i));
553 if (loc.location == kLocPhysReg) {
554 storeBaseDisp(cUnit, rSP, oatSRegOffset(cUnit, loc.sRegLow),
555 loc.lowReg, kWord);
556 }
557 }
558 /*
559 * TUNING note: generated code here could be much improved, but
560 * this is an uncommon operation and isn't especially performance
561 * critical.
562 */
563 int rSrc = oatAllocTemp(cUnit);
564 int rDst = oatAllocTemp(cUnit);
565 int rIdx = oatAllocTemp(cUnit);
buzbee5de34942012-03-01 14:51:57 -0800566#if defined(TARGET_ARM)
buzbee31a4a6f2012-02-28 15:36:15 -0800567 int rVal = rLR; // Using a lot of temps, rLR is known free here
buzbee5de34942012-03-01 14:51:57 -0800568#else
569 int rVal = oatAllocTemp(cUnit);
570#endif
buzbee31a4a6f2012-02-28 15:36:15 -0800571 // Set up source pointer
572 RegLocation rlFirst = oatGetSrc(cUnit, mir, 0);
Ian Rogersb5d09b22012-03-06 22:14:17 -0800573#if defined(TARGET_X86)
574 UNIMPLEMENTED(FATAL);
575#else
buzbee31a4a6f2012-02-28 15:36:15 -0800576 opRegRegImm(cUnit, kOpAdd, rSrc, rSP,
577 oatSRegOffset(cUnit, rlFirst.sRegLow));
578 // Set up the target pointer
579 opRegRegImm(cUnit, kOpAdd, rDst, rRET0,
580 Array::DataOffset(component_size).Int32Value());
Ian Rogersb5d09b22012-03-06 22:14:17 -0800581#endif
buzbee31a4a6f2012-02-28 15:36:15 -0800582 // Set up the loop counter (known to be > 0)
583 loadConstant(cUnit, rIdx, dInsn->vA - 1);
584 // Generate the copy loop. Going backwards for convenience
585 LIR* target = newLIR0(cUnit, kPseudoTargetLabel);
buzbee31a4a6f2012-02-28 15:36:15 -0800586 // Copy next element
587 loadBaseIndexed(cUnit, rSrc, rIdx, rVal, 2, kWord);
588 storeBaseIndexed(cUnit, rDst, rIdx, rVal, 2, kWord);
589#if defined(TARGET_ARM)
590 // Combine sub & test using sub setflags encoding here
591 newLIR3(cUnit, kThumb2SubsRRI12, rIdx, rIdx, 1);
buzbee82488f52012-03-02 08:20:26 -0800592 opCondBranch(cUnit, kCondGe, target);
buzbee31a4a6f2012-02-28 15:36:15 -0800593#else
buzbee5de34942012-03-01 14:51:57 -0800594 oatFreeTemp(cUnit, rVal);
buzbee31a4a6f2012-02-28 15:36:15 -0800595 opRegImm(cUnit, kOpSub, rIdx, 1);
buzbee82488f52012-03-02 08:20:26 -0800596 opCmpImmBranch(cUnit, kCondGe, rIdx, 0, target);
buzbee31a4a6f2012-02-28 15:36:15 -0800597#endif
buzbee31a4a6f2012-02-28 15:36:15 -0800598 } else if (!isRange) {
599 // TUNING: interleave
600 for (unsigned int i = 0; i < dInsn->vA; i++) {
601 RegLocation rlArg = loadValue(cUnit,
602 oatGetSrc(cUnit, mir, i), kCoreReg);
603 storeBaseDisp(cUnit, rRET0,
604 Array::DataOffset(component_size).Int32Value() +
605 i * 4, rlArg.lowReg, kWord);
606 // If the loadValue caused a temp to be allocated, free it
607 if (oatIsTemp(cUnit, rlArg.lowReg)) {
608 oatFreeTemp(cUnit, rlArg.lowReg);
609 }
610 }
611 }
612}
613
614void genSput(CompilationUnit* cUnit, MIR* mir, RegLocation rlSrc,
615 bool isLongOrDouble, bool isObject)
616{
617 int fieldOffset;
618 int ssbIndex;
619 bool isVolatile;
620 bool isReferrersClass;
621 uint32_t fieldIdx = mir->dalvikInsn.vB;
622
623 OatCompilationUnit mUnit(cUnit->class_loader, cUnit->class_linker,
624 *cUnit->dex_file, *cUnit->dex_cache,
625 cUnit->code_item, cUnit->method_idx,
626 cUnit->access_flags);
627
628 bool fastPath =
629 cUnit->compiler->ComputeStaticFieldInfo(fieldIdx, &mUnit,
630 fieldOffset, ssbIndex,
631 isReferrersClass, isVolatile, true);
632 if (fastPath && !SLOW_FIELD_PATH) {
633 DCHECK_GE(fieldOffset, 0);
634 int rBase;
buzbee31a4a6f2012-02-28 15:36:15 -0800635 if (isReferrersClass) {
636 // Fast path, static storage base is this method's class
buzbeee1965672012-03-11 18:39:19 -0700637 RegLocation rlMethod = loadCurrMethod(cUnit);
buzbee31a4a6f2012-02-28 15:36:15 -0800638 rBase = oatAllocTemp(cUnit);
buzbeee1965672012-03-11 18:39:19 -0700639 loadWordDisp(cUnit, rlMethod.lowReg,
buzbee31a4a6f2012-02-28 15:36:15 -0800640 Method::DeclaringClassOffset().Int32Value(), rBase);
Ian Rogersc6f3bb82012-03-21 20:40:33 -0700641 if (oatIsTemp(cUnit, rlMethod.lowReg)) {
642 oatFreeTemp(cUnit, rlMethod.lowReg);
643 }
buzbee31a4a6f2012-02-28 15:36:15 -0800644 } else {
645 // Medium path, static storage base in a different class which
646 // requires checks that the other class is initialized.
647 DCHECK_GE(ssbIndex, 0);
648 // May do runtime call so everything to home locations.
649 oatFlushAllRegs(cUnit);
650 // Using fixed register to sync with possible call to runtime
651 // support.
buzbeee1965672012-03-11 18:39:19 -0700652 int rMethod = rARG1;
buzbee31a4a6f2012-02-28 15:36:15 -0800653 oatLockTemp(cUnit, rMethod);
654 loadCurrMethodDirect(cUnit, rMethod);
655 rBase = rARG0;
656 oatLockTemp(cUnit, rBase);
657 loadWordDisp(cUnit, rMethod,
658 Method::DexCacheInitializedStaticStorageOffset().Int32Value(),
659 rBase);
660 loadWordDisp(cUnit, rBase,
661 Array::DataOffset(sizeof(Object*)).Int32Value() + sizeof(int32_t*) *
662 ssbIndex, rBase);
663 // rBase now points at appropriate static storage base (Class*)
664 // or NULL if not initialized. Check for NULL and call helper if NULL.
665 // TUNING: fast path should fall through
buzbee82488f52012-03-02 08:20:26 -0800666 LIR* branchOver = opCmpImmBranch(cUnit, kCondNe, rBase, 0, NULL);
buzbee31a4a6f2012-02-28 15:36:15 -0800667 loadConstant(cUnit, rARG0, ssbIndex);
Ian Rogersab2b55d2012-03-18 00:06:11 -0700668 callRuntimeHelperImm(cUnit,
669 OFFSETOF_MEMBER(Thread, pInitializeStaticStorage),
670 ssbIndex);
buzbee31a4a6f2012-02-28 15:36:15 -0800671#if defined(TARGET_MIPS)
672 // For Arm, rRET0 = rARG0 = rBASE, for Mips, we need to copy
buzbee82488f52012-03-02 08:20:26 -0800673 opRegCopy(cUnit, rBase, rRET0);
buzbee31a4a6f2012-02-28 15:36:15 -0800674#endif
675 LIR* skipTarget = newLIR0(cUnit, kPseudoTargetLabel);
buzbee31a4a6f2012-02-28 15:36:15 -0800676 branchOver->target = (LIR*)skipTarget;
buzbeee1965672012-03-11 18:39:19 -0700677 oatFreeTemp(cUnit, rMethod);
buzbee31a4a6f2012-02-28 15:36:15 -0800678 }
679 // rBase now holds static storage base
buzbee31a4a6f2012-02-28 15:36:15 -0800680 if (isLongOrDouble) {
681 rlSrc = oatGetSrcWide(cUnit, mir, 0, 1);
682 rlSrc = loadValueWide(cUnit, rlSrc, kAnyReg);
683 } else {
684 rlSrc = oatGetSrc(cUnit, mir, 0);
685 rlSrc = loadValue(cUnit, rlSrc, kAnyReg);
686 }
687//FIXME: need to generalize the barrier call
688 if (isVolatile) {
689 oatGenMemBarrier(cUnit, kST);
690 }
691 if (isLongOrDouble) {
692 storeBaseDispWide(cUnit, rBase, fieldOffset, rlSrc.lowReg,
693 rlSrc.highReg);
694 } else {
695 storeWordDisp(cUnit, rBase, fieldOffset, rlSrc.lowReg);
696 }
697 if (isVolatile) {
698 oatGenMemBarrier(cUnit, kSY);
699 }
700 if (isObject) {
701 markGCCard(cUnit, rlSrc.lowReg, rBase);
702 }
703 oatFreeTemp(cUnit, rBase);
704 } else {
705 oatFlushAllRegs(cUnit); // Everything to home locations
706 int setterOffset = isLongOrDouble ? OFFSETOF_MEMBER(Thread, pSet64Static) :
707 (isObject ? OFFSETOF_MEMBER(Thread, pSetObjStatic)
708 : OFFSETOF_MEMBER(Thread, pSet32Static));
Ian Rogersab2b55d2012-03-18 00:06:11 -0700709 callRuntimeHelperImmRegLocation(cUnit, setterOffset, fieldIdx, rlSrc);
buzbee31a4a6f2012-02-28 15:36:15 -0800710 }
711}
712
713void genSget(CompilationUnit* cUnit, MIR* mir, RegLocation rlDest,
714 bool isLongOrDouble, bool isObject)
715{
716 int fieldOffset;
717 int ssbIndex;
718 bool isVolatile;
719 bool isReferrersClass;
720 uint32_t fieldIdx = mir->dalvikInsn.vB;
721
722 OatCompilationUnit mUnit(cUnit->class_loader, cUnit->class_linker,
723 *cUnit->dex_file, *cUnit->dex_cache,
724 cUnit->code_item, cUnit->method_idx,
725 cUnit->access_flags);
726
727 bool fastPath =
728 cUnit->compiler->ComputeStaticFieldInfo(fieldIdx, &mUnit,
729 fieldOffset, ssbIndex,
730 isReferrersClass, isVolatile,
731 false);
732 if (fastPath && !SLOW_FIELD_PATH) {
733 DCHECK_GE(fieldOffset, 0);
734 int rBase;
buzbee31a4a6f2012-02-28 15:36:15 -0800735 if (isReferrersClass) {
736 // Fast path, static storage base is this method's class
buzbeee1965672012-03-11 18:39:19 -0700737 RegLocation rlMethod = loadCurrMethod(cUnit);
buzbee31a4a6f2012-02-28 15:36:15 -0800738 rBase = oatAllocTemp(cUnit);
buzbeee1965672012-03-11 18:39:19 -0700739 loadWordDisp(cUnit, rlMethod.lowReg,
buzbee31a4a6f2012-02-28 15:36:15 -0800740 Method::DeclaringClassOffset().Int32Value(), rBase);
741 } else {
742 // Medium path, static storage base in a different class which
743 // requires checks that the other class is initialized
744 DCHECK_GE(ssbIndex, 0);
745 // May do runtime call so everything to home locations.
746 oatFlushAllRegs(cUnit);
747 // Using fixed register to sync with possible call to runtime
748 // support
buzbeee1965672012-03-11 18:39:19 -0700749 int rMethod = rARG1;
buzbee31a4a6f2012-02-28 15:36:15 -0800750 oatLockTemp(cUnit, rMethod);
751 loadCurrMethodDirect(cUnit, rMethod);
752 rBase = rARG0;
753 oatLockTemp(cUnit, rBase);
754 loadWordDisp(cUnit, rMethod,
755 Method::DexCacheInitializedStaticStorageOffset().Int32Value(),
756 rBase);
757 loadWordDisp(cUnit, rBase,
758 Array::DataOffset(sizeof(Object*)).Int32Value() +
759 sizeof(int32_t*) * ssbIndex,
760 rBase);
761 // rBase now points at appropriate static storage base (Class*)
762 // or NULL if not initialized. Check for NULL and call helper if NULL.
763 // TUNING: fast path should fall through
buzbee82488f52012-03-02 08:20:26 -0800764 LIR* branchOver = opCmpImmBranch(cUnit, kCondNe, rBase, 0, NULL);
Ian Rogersab2b55d2012-03-18 00:06:11 -0700765 callRuntimeHelperImm(cUnit,
766 OFFSETOF_MEMBER(Thread, pInitializeStaticStorage),
767 ssbIndex);
buzbee31a4a6f2012-02-28 15:36:15 -0800768#if defined(TARGET_MIPS)
769 // For Arm, rRET0 = rARG0 = rBASE, for Mips, we need to copy
buzbee82488f52012-03-02 08:20:26 -0800770 opRegCopy(cUnit, rBase, rRET0);
buzbee31a4a6f2012-02-28 15:36:15 -0800771#endif
772 LIR* skipTarget = newLIR0(cUnit, kPseudoTargetLabel);
buzbee31a4a6f2012-02-28 15:36:15 -0800773 branchOver->target = (LIR*)skipTarget;
buzbeee1965672012-03-11 18:39:19 -0700774 oatFreeTemp(cUnit, rMethod);
buzbee31a4a6f2012-02-28 15:36:15 -0800775 }
776 // rBase now holds static storage base
buzbee31a4a6f2012-02-28 15:36:15 -0800777 rlDest = isLongOrDouble ? oatGetDestWide(cUnit, mir, 0, 1)
778 : oatGetDest(cUnit, mir, 0);
779 RegLocation rlResult = oatEvalLoc(cUnit, rlDest, kAnyReg, true);
780 if (isVolatile) {
781 oatGenMemBarrier(cUnit, kSY);
782 }
783 if (isLongOrDouble) {
784 loadBaseDispWide(cUnit, NULL, rBase, fieldOffset, rlResult.lowReg,
785 rlResult.highReg, INVALID_SREG);
786 } else {
787 loadWordDisp(cUnit, rBase, fieldOffset, rlResult.lowReg);
788 }
789 oatFreeTemp(cUnit, rBase);
790 if (isLongOrDouble) {
791 storeValueWide(cUnit, rlDest, rlResult);
792 } else {
793 storeValue(cUnit, rlDest, rlResult);
794 }
795 } else {
796 oatFlushAllRegs(cUnit); // Everything to home locations
797 int getterOffset = isLongOrDouble ? OFFSETOF_MEMBER(Thread, pGet64Static) :
798 (isObject ? OFFSETOF_MEMBER(Thread, pGetObjStatic)
799 : OFFSETOF_MEMBER(Thread, pGet32Static));
Ian Rogersab2b55d2012-03-18 00:06:11 -0700800 callRuntimeHelperImm(cUnit, getterOffset, fieldIdx);
buzbee31a4a6f2012-02-28 15:36:15 -0800801 if (isLongOrDouble) {
Ian Rogersf7d9ad32012-03-13 18:45:39 -0700802 RegLocation rlResult = oatGetReturnWide(cUnit, rlDest.fp);
buzbee31a4a6f2012-02-28 15:36:15 -0800803 storeValueWide(cUnit, rlDest, rlResult);
804 } else {
Ian Rogersf7d9ad32012-03-13 18:45:39 -0700805 RegLocation rlResult = oatGetReturn(cUnit, rlDest.fp);
buzbee31a4a6f2012-02-28 15:36:15 -0800806 storeValue(cUnit, rlDest, rlResult);
807 }
808 }
809}
810
811
812// Debugging routine - if null target, branch to DebugMe
813void genShowTarget(CompilationUnit* cUnit)
814{
buzbeea7678db2012-03-05 15:35:46 -0800815#if defined(TARGET_X86)
816 UNIMPLEMENTED(WARNING) << "genShowTarget";
817#else
buzbee0398c422012-03-02 15:22:47 -0800818 LIR* branchOver = opCmpImmBranch(cUnit, kCondNe, rINVOKE_TGT, 0, NULL);
buzbee31a4a6f2012-02-28 15:36:15 -0800819 loadWordDisp(cUnit, rSELF,
buzbee0398c422012-03-02 15:22:47 -0800820 OFFSETOF_MEMBER(Thread, pDebugMe), rINVOKE_TGT);
buzbee31a4a6f2012-02-28 15:36:15 -0800821 LIR* target = newLIR0(cUnit, kPseudoTargetLabel);
buzbee31a4a6f2012-02-28 15:36:15 -0800822 branchOver->target = (LIR*)target;
buzbeea7678db2012-03-05 15:35:46 -0800823#endif
buzbee31a4a6f2012-02-28 15:36:15 -0800824}
825
826void genThrowVerificationError(CompilationUnit* cUnit, MIR* mir)
827{
Ian Rogersab2b55d2012-03-18 00:06:11 -0700828 callRuntimeHelperImmImm(cUnit, OFFSETOF_MEMBER(Thread,
829 pThrowVerificationErrorFromCode),
830 mir->dalvikInsn.vA, mir->dalvikInsn.vB);
buzbee31a4a6f2012-02-28 15:36:15 -0800831}
832
833void handleSuspendLaunchpads(CompilationUnit *cUnit)
834{
Ian Rogersab2b55d2012-03-18 00:06:11 -0700835 LIR** suspendLabel = (LIR **)cUnit->suspendLaunchpads.elemList;
buzbee31a4a6f2012-02-28 15:36:15 -0800836 int numElems = cUnit->suspendLaunchpads.numUsed;
buzbee31a4a6f2012-02-28 15:36:15 -0800837 for (int i = 0; i < numElems; i++) {
buzbeec5159d52012-03-03 11:48:39 -0800838 oatResetRegPool(cUnit);
buzbeefc9e6fa2012-03-23 15:14:29 -0700839 oatResetDefTracking(cUnit);
buzbee31a4a6f2012-02-28 15:36:15 -0800840 LIR* lab = suspendLabel[i];
841 LIR* resumeLab = (LIR*)lab->operands[0];
842 cUnit->currentDalvikOffset = lab->operands[1];
Ian Rogersab2b55d2012-03-18 00:06:11 -0700843 oatAppendLIR(cUnit, lab);
Ian Rogers6cbb2bd2012-03-16 13:45:30 -0700844#if defined(TARGET_X86)
845 opThreadMem(cUnit, kOpBlx,
846 OFFSETOF_MEMBER(Thread, pTestSuspendFromCode));
847#else
buzbee31a4a6f2012-02-28 15:36:15 -0800848 int rTgt = loadHelper(cUnit, OFFSETOF_MEMBER(Thread,
849 pTestSuspendFromCode));
buzbee31a4a6f2012-02-28 15:36:15 -0800850 opReg(cUnit, kOpBlx, rTgt);
Ian Rogers6cbb2bd2012-03-16 13:45:30 -0700851#endif
buzbee82488f52012-03-02 08:20:26 -0800852 opUnconditionalBranch(cUnit, resumeLab);
buzbee31a4a6f2012-02-28 15:36:15 -0800853 }
854}
855
buzbeefc9e6fa2012-03-23 15:14:29 -0700856void handleIntrinsicLaunchpads(CompilationUnit *cUnit)
857{
858 LIR** intrinsicLabel = (LIR **)cUnit->intrinsicLaunchpads.elemList;
859 int numElems = cUnit->intrinsicLaunchpads.numUsed;
860 for (int i = 0; i < numElems; i++) {
861 oatResetRegPool(cUnit);
862 oatResetDefTracking(cUnit);
863 LIR* lab = intrinsicLabel[i];
864 MIR* mir = (MIR*)lab->operands[0];
865 InvokeType type = (InvokeType)lab->operands[1];
866 BasicBlock* bb = (BasicBlock*)lab->operands[3];
867 cUnit->currentDalvikOffset = mir->offset;
868 oatAppendLIR(cUnit, lab);
869 genInvoke(cUnit, bb, mir, type, false /* isRange */);
870 LIR* resumeLab = (LIR*)lab->operands[2];
871 if (resumeLab != NULL) {
872 opUnconditionalBranch(cUnit, resumeLab);
873 }
874 }
875}
876
buzbee31a4a6f2012-02-28 15:36:15 -0800877void handleThrowLaunchpads(CompilationUnit *cUnit)
878{
Ian Rogersab2b55d2012-03-18 00:06:11 -0700879 LIR** throwLabel = (LIR **)cUnit->throwLaunchpads.elemList;
buzbee31a4a6f2012-02-28 15:36:15 -0800880 int numElems = cUnit->throwLaunchpads.numUsed;
Ian Rogersab2b55d2012-03-18 00:06:11 -0700881 for (int i = 0; i < numElems; i++) {
buzbeec5159d52012-03-03 11:48:39 -0800882 oatResetRegPool(cUnit);
buzbeefc9e6fa2012-03-23 15:14:29 -0700883 oatResetDefTracking(cUnit);
buzbee31a4a6f2012-02-28 15:36:15 -0800884 LIR* lab = throwLabel[i];
885 cUnit->currentDalvikOffset = lab->operands[1];
Ian Rogersab2b55d2012-03-18 00:06:11 -0700886 oatAppendLIR(cUnit, lab);
buzbee31a4a6f2012-02-28 15:36:15 -0800887 int funcOffset = 0;
888 int v1 = lab->operands[2];
889 int v2 = lab->operands[3];
890 switch(lab->operands[0]) {
891 case kThrowNullPointer:
892 funcOffset = OFFSETOF_MEMBER(Thread, pThrowNullPointerFromCode);
893 break;
894 case kThrowArrayBounds:
buzbee5de34942012-03-01 14:51:57 -0800895 if (v2 != rARG0) {
buzbee82488f52012-03-02 08:20:26 -0800896 opRegCopy(cUnit, rARG0, v1);
897 opRegCopy(cUnit, rARG1, v2);
buzbee31a4a6f2012-02-28 15:36:15 -0800898 } else {
buzbee5de34942012-03-01 14:51:57 -0800899 if (v1 == rARG1) {
buzbee31a4a6f2012-02-28 15:36:15 -0800900#if defined(TARGET_ARM)
901 int rTmp = r12;
902#else
903 int rTmp = oatAllocTemp(cUnit);
904#endif
buzbee82488f52012-03-02 08:20:26 -0800905 opRegCopy(cUnit, rTmp, v1);
906 opRegCopy(cUnit, rARG1, v2);
907 opRegCopy(cUnit, rARG0, rTmp);
buzbee31a4a6f2012-02-28 15:36:15 -0800908 } else {
buzbee82488f52012-03-02 08:20:26 -0800909 opRegCopy(cUnit, rARG1, v2);
910 opRegCopy(cUnit, rARG0, v1);
buzbee31a4a6f2012-02-28 15:36:15 -0800911 }
912 }
913 funcOffset = OFFSETOF_MEMBER(Thread, pThrowArrayBoundsFromCode);
914 break;
915 case kThrowDivZero:
916 funcOffset = OFFSETOF_MEMBER(Thread, pThrowDivZeroFromCode);
917 break;
918 case kThrowVerificationError:
919 loadConstant(cUnit, rARG0, v1);
920 loadConstant(cUnit, rARG1, v2);
921 funcOffset =
922 OFFSETOF_MEMBER(Thread, pThrowVerificationErrorFromCode);
923 break;
924 case kThrowNegArraySize:
buzbee82488f52012-03-02 08:20:26 -0800925 opRegCopy(cUnit, rARG0, v1);
buzbee31a4a6f2012-02-28 15:36:15 -0800926 funcOffset =
927 OFFSETOF_MEMBER(Thread, pThrowNegArraySizeFromCode);
928 break;
929 case kThrowNoSuchMethod:
buzbee82488f52012-03-02 08:20:26 -0800930 opRegCopy(cUnit, rARG0, v1);
buzbee31a4a6f2012-02-28 15:36:15 -0800931 funcOffset =
932 OFFSETOF_MEMBER(Thread, pThrowNoSuchMethodFromCode);
933 break;
934 case kThrowStackOverflow:
935 funcOffset =
936 OFFSETOF_MEMBER(Thread, pThrowStackOverflowFromCode);
937 // Restore stack alignment
938 opRegImm(cUnit, kOpAdd, rSP,
939 (cUnit->numCoreSpills + cUnit->numFPSpills) * 4);
940 break;
941 default:
942 LOG(FATAL) << "Unexpected throw kind: " << lab->operands[0];
943 }
Ian Rogersab2b55d2012-03-18 00:06:11 -0700944 oatClobberCalleeSave(cUnit);
945#if !defined(TARGET_X86)
buzbee31a4a6f2012-02-28 15:36:15 -0800946 int rTgt = loadHelper(cUnit, funcOffset);
Ian Rogersab2b55d2012-03-18 00:06:11 -0700947 opReg(cUnit, kOpBlx, rTgt);
buzbee0398c422012-03-02 15:22:47 -0800948 oatFreeTemp(cUnit, rTgt);
Ian Rogersab2b55d2012-03-18 00:06:11 -0700949#else
950 opThreadMem(cUnit, kOpBlx, funcOffset);
Ian Rogers6cbb2bd2012-03-16 13:45:30 -0700951#endif
buzbee31a4a6f2012-02-28 15:36:15 -0800952 }
953}
954
955/* Needed by the Assembler */
956void oatSetupResourceMasks(LIR* lir)
957{
958 setupResourceMasks(lir);
959}
960
buzbee16da88c2012-03-20 10:38:17 -0700961bool fastInstance(CompilationUnit* cUnit, uint32_t fieldIdx,
962 int& fieldOffset, bool& isVolatile, bool isPut)
963{
964 OatCompilationUnit mUnit(cUnit->class_loader, cUnit->class_linker,
965 *cUnit->dex_file, *cUnit->dex_cache,
966 cUnit->code_item, cUnit->method_idx,
967 cUnit->access_flags);
968 return cUnit->compiler->ComputeInstanceFieldInfo(fieldIdx, &mUnit,
969 fieldOffset, isVolatile, isPut);
970}
971
buzbee31a4a6f2012-02-28 15:36:15 -0800972void genIGet(CompilationUnit* cUnit, MIR* mir, OpSize size,
973 RegLocation rlDest, RegLocation rlObj,
974 bool isLongOrDouble, bool isObject)
975{
976 int fieldOffset;
977 bool isVolatile;
978 uint32_t fieldIdx = mir->dalvikInsn.vC;
979
buzbee16da88c2012-03-20 10:38:17 -0700980 bool fastPath = fastInstance(cUnit, fieldIdx, fieldOffset, isVolatile,
981 false);
buzbee31a4a6f2012-02-28 15:36:15 -0800982
983 if (fastPath && !SLOW_FIELD_PATH) {
984 RegLocation rlResult;
985 RegisterClass regClass = oatRegClassBySize(size);
986 DCHECK_GE(fieldOffset, 0);
987 rlObj = loadValue(cUnit, rlObj, kCoreReg);
988 if (isLongOrDouble) {
989 DCHECK(rlDest.wide);
990 genNullCheck(cUnit, rlObj.sRegLow, rlObj.lowReg, mir);/* null? */
Ian Rogersb5d09b22012-03-06 22:14:17 -0800991#if defined(TARGET_X86)
992 rlResult = oatEvalLoc(cUnit, rlDest, regClass, true);
993 genNullCheck(cUnit, rlObj.sRegLow, rlObj.lowReg, mir);/* null? */
994 loadBaseDispWide(cUnit, mir, rlObj.lowReg, fieldOffset, rlResult.lowReg,
995 rlResult.highReg, rlObj.sRegLow);
996 if (isVolatile) {
997 oatGenMemBarrier(cUnit, kSY);
998 }
999#else
buzbee31a4a6f2012-02-28 15:36:15 -08001000 int regPtr = oatAllocTemp(cUnit);
1001 opRegRegImm(cUnit, kOpAdd, regPtr, rlObj.lowReg, fieldOffset);
1002 rlResult = oatEvalLoc(cUnit, rlDest, regClass, true);
1003 loadPair(cUnit, regPtr, rlResult.lowReg, rlResult.highReg);
1004 if (isVolatile) {
1005 oatGenMemBarrier(cUnit, kSY);
1006 }
1007 oatFreeTemp(cUnit, regPtr);
Ian Rogersb5d09b22012-03-06 22:14:17 -08001008#endif
buzbee31a4a6f2012-02-28 15:36:15 -08001009 storeValueWide(cUnit, rlDest, rlResult);
1010 } else {
1011 rlResult = oatEvalLoc(cUnit, rlDest, regClass, true);
1012 genNullCheck(cUnit, rlObj.sRegLow, rlObj.lowReg, mir);/* null? */
1013 loadBaseDisp(cUnit, mir, rlObj.lowReg, fieldOffset, rlResult.lowReg,
1014 kWord, rlObj.sRegLow);
1015 if (isVolatile) {
1016 oatGenMemBarrier(cUnit, kSY);
1017 }
1018 storeValue(cUnit, rlDest, rlResult);
1019 }
1020 } else {
1021 int getterOffset = isLongOrDouble ? OFFSETOF_MEMBER(Thread, pGet64Instance) :
1022 (isObject ? OFFSETOF_MEMBER(Thread, pGetObjInstance)
1023 : OFFSETOF_MEMBER(Thread, pGet32Instance));
Ian Rogersab2b55d2012-03-18 00:06:11 -07001024 callRuntimeHelperImmRegLocation(cUnit, getterOffset, fieldIdx, rlObj);
buzbee31a4a6f2012-02-28 15:36:15 -08001025 if (isLongOrDouble) {
Ian Rogersf7d9ad32012-03-13 18:45:39 -07001026 RegLocation rlResult = oatGetReturnWide(cUnit, rlDest.fp);
buzbee31a4a6f2012-02-28 15:36:15 -08001027 storeValueWide(cUnit, rlDest, rlResult);
1028 } else {
Ian Rogersf7d9ad32012-03-13 18:45:39 -07001029 RegLocation rlResult = oatGetReturn(cUnit, rlDest.fp);
buzbee31a4a6f2012-02-28 15:36:15 -08001030 storeValue(cUnit, rlDest, rlResult);
1031 }
1032 }
1033}
1034
1035void genIPut(CompilationUnit* cUnit, MIR* mir, OpSize size, RegLocation rlSrc,
1036 RegLocation rlObj, bool isLongOrDouble, bool isObject)
1037{
1038 int fieldOffset;
1039 bool isVolatile;
1040 uint32_t fieldIdx = mir->dalvikInsn.vC;
1041
buzbee16da88c2012-03-20 10:38:17 -07001042 bool fastPath = fastInstance(cUnit, fieldIdx, fieldOffset, isVolatile,
1043 true);
buzbee31a4a6f2012-02-28 15:36:15 -08001044 if (fastPath && !SLOW_FIELD_PATH) {
1045 RegisterClass regClass = oatRegClassBySize(size);
1046 DCHECK_GE(fieldOffset, 0);
1047 rlObj = loadValue(cUnit, rlObj, kCoreReg);
1048 if (isLongOrDouble) {
1049 int regPtr;
1050 rlSrc = loadValueWide(cUnit, rlSrc, kAnyReg);
1051 genNullCheck(cUnit, rlObj.sRegLow, rlObj.lowReg, mir);/* null? */
1052 regPtr = oatAllocTemp(cUnit);
1053 opRegRegImm(cUnit, kOpAdd, regPtr, rlObj.lowReg, fieldOffset);
1054 if (isVolatile) {
1055 oatGenMemBarrier(cUnit, kST);
1056 }
1057 storePair(cUnit, regPtr, rlSrc.lowReg, rlSrc.highReg);
1058 if (isVolatile) {
1059 oatGenMemBarrier(cUnit, kSY);
1060 }
1061 oatFreeTemp(cUnit, regPtr);
1062 } else {
1063 rlSrc = loadValue(cUnit, rlSrc, regClass);
1064 genNullCheck(cUnit, rlObj.sRegLow, rlObj.lowReg, mir);/* null? */
1065 if (isVolatile) {
1066 oatGenMemBarrier(cUnit, kST);
1067 }
1068 storeBaseDisp(cUnit, rlObj.lowReg, fieldOffset, rlSrc.lowReg, kWord);
1069 if (isVolatile) {
1070 oatGenMemBarrier(cUnit, kSY);
1071 }
buzbeea7c12682012-03-19 13:13:53 -07001072 if (isObject) {
1073 markGCCard(cUnit, rlSrc.lowReg, rlObj.lowReg);
1074 }
buzbee31a4a6f2012-02-28 15:36:15 -08001075 }
1076 } else {
1077 int setterOffset = isLongOrDouble ? OFFSETOF_MEMBER(Thread, pSet64Instance) :
1078 (isObject ? OFFSETOF_MEMBER(Thread, pSetObjInstance)
1079 : OFFSETOF_MEMBER(Thread, pSet32Instance));
Ian Rogersab2b55d2012-03-18 00:06:11 -07001080 callRuntimeHelperImmRegLocationRegLocation(cUnit, setterOffset,
1081 fieldIdx, rlObj, rlSrc);
buzbee31a4a6f2012-02-28 15:36:15 -08001082 }
1083}
1084
1085void genConstClass(CompilationUnit* cUnit, MIR* mir, RegLocation rlDest,
1086 RegLocation rlSrc)
1087{
1088 uint32_t type_idx = mir->dalvikInsn.vB;
buzbeee1965672012-03-11 18:39:19 -07001089 RegLocation rlMethod = loadCurrMethod(cUnit);
buzbee31a4a6f2012-02-28 15:36:15 -08001090 int resReg = oatAllocTemp(cUnit);
1091 RegLocation rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
1092 if (!cUnit->compiler->CanAccessTypeWithoutChecks(cUnit->method_idx,
1093 cUnit->dex_cache,
1094 *cUnit->dex_file,
1095 type_idx)) {
1096 // Call out to helper which resolves type and verifies access.
1097 // Resolved type returned in rRET0.
Ian Rogersab2b55d2012-03-18 00:06:11 -07001098 callRuntimeHelperImmReg(cUnit,
1099 OFFSETOF_MEMBER(Thread,
1100 pInitializeTypeAndVerifyAccessFromCode),
1101 type_idx, rlMethod.lowReg);
Ian Rogersf7d9ad32012-03-13 18:45:39 -07001102 RegLocation rlResult = oatGetReturn(cUnit, false);
buzbee31a4a6f2012-02-28 15:36:15 -08001103 storeValue(cUnit, rlDest, rlResult);
1104 } else {
1105 // We're don't need access checks, load type from dex cache
1106 int32_t dex_cache_offset =
1107 Method::DexCacheResolvedTypesOffset().Int32Value();
buzbeee1965672012-03-11 18:39:19 -07001108 loadWordDisp(cUnit, rlMethod.lowReg, dex_cache_offset, resReg);
buzbee31a4a6f2012-02-28 15:36:15 -08001109 int32_t offset_of_type =
1110 Array::DataOffset(sizeof(Class*)).Int32Value() + (sizeof(Class*)
1111 * type_idx);
1112 loadWordDisp(cUnit, resReg, offset_of_type, rlResult.lowReg);
1113 if (!cUnit->compiler->CanAssumeTypeIsPresentInDexCache(cUnit->dex_cache,
1114 type_idx) || SLOW_TYPE_PATH) {
1115 // Slow path, at runtime test if type is null and if so initialize
1116 oatFlushAllRegs(cUnit);
buzbee82488f52012-03-02 08:20:26 -08001117 LIR* branch1 = opCmpImmBranch(cUnit, kCondEq, rlResult.lowReg, 0,
1118 NULL);
buzbee31a4a6f2012-02-28 15:36:15 -08001119 // Resolved, store and hop over following code
1120 storeValue(cUnit, rlDest, rlResult);
buzbee3d661942012-03-14 17:37:27 -07001121 /*
1122 * Because we have stores of the target value on two paths,
1123 * clobber temp tracking for the destination using the ssa name
1124 */
1125 oatClobberSReg(cUnit, rlDest.sRegLow);
buzbee82488f52012-03-02 08:20:26 -08001126 LIR* branch2 = opUnconditionalBranch(cUnit,0);
buzbee31a4a6f2012-02-28 15:36:15 -08001127 // TUNING: move slow path to end & remove unconditional branch
1128 LIR* target1 = newLIR0(cUnit, kPseudoTargetLabel);
buzbee5de34942012-03-01 14:51:57 -08001129 // Call out to helper, which will return resolved type in rARG0
Ian Rogersab2b55d2012-03-18 00:06:11 -07001130 callRuntimeHelperImmReg(cUnit, OFFSETOF_MEMBER(Thread,
1131 pInitializeTypeFromCode),
1132 type_idx, rlMethod.lowReg);
Ian Rogersf7d9ad32012-03-13 18:45:39 -07001133 RegLocation rlResult = oatGetReturn(cUnit, false);
buzbee31a4a6f2012-02-28 15:36:15 -08001134 storeValue(cUnit, rlDest, rlResult);
buzbee3d661942012-03-14 17:37:27 -07001135 /*
1136 * Because we have stores of the target value on two paths,
1137 * clobber temp tracking for the destination using the ssa name
1138 */
1139 oatClobberSReg(cUnit, rlDest.sRegLow);
buzbee31a4a6f2012-02-28 15:36:15 -08001140 // Rejoin code paths
1141 LIR* target2 = newLIR0(cUnit, kPseudoTargetLabel);
buzbee31a4a6f2012-02-28 15:36:15 -08001142 branch1->target = (LIR*)target1;
1143 branch2->target = (LIR*)target2;
1144 } else {
1145 // Fast path, we're done - just store result
1146 storeValue(cUnit, rlDest, rlResult);
1147 }
1148 }
1149}
Ian Rogersab2b55d2012-03-18 00:06:11 -07001150
buzbee31a4a6f2012-02-28 15:36:15 -08001151void genConstString(CompilationUnit* cUnit, MIR* mir, RegLocation rlDest,
1152 RegLocation rlSrc)
1153{
1154 /* NOTE: Most strings should be available at compile time */
1155 uint32_t string_idx = mir->dalvikInsn.vB;
1156 int32_t offset_of_string = Array::DataOffset(sizeof(String*)).Int32Value() +
1157 (sizeof(String*) * string_idx);
1158 if (!cUnit->compiler->CanAssumeStringIsPresentInDexCache(
1159 cUnit->dex_cache, string_idx) || SLOW_STRING_PATH) {
1160 // slow path, resolve string if not in dex cache
1161 oatFlushAllRegs(cUnit);
1162 oatLockCallTemps(cUnit); // Using explicit registers
1163 loadCurrMethodDirect(cUnit, rARG2);
1164 loadWordDisp(cUnit, rARG2,
1165 Method::DexCacheStringsOffset().Int32Value(), rARG0);
1166 // Might call out to helper, which will return resolved string in rRET0
Ian Rogers6cbb2bd2012-03-16 13:45:30 -07001167#if !defined(TARGET_X86)
buzbee31a4a6f2012-02-28 15:36:15 -08001168 int rTgt = loadHelper(cUnit, OFFSETOF_MEMBER(Thread,
1169 pResolveStringFromCode));
Ian Rogers6cbb2bd2012-03-16 13:45:30 -07001170#endif
buzbee31a4a6f2012-02-28 15:36:15 -08001171 loadWordDisp(cUnit, rRET0, offset_of_string, rARG0);
1172 loadConstant(cUnit, rARG1, string_idx);
1173#if defined(TARGET_ARM)
1174 opRegImm(cUnit, kOpCmp, rRET0, 0); // Is resolved?
1175 genBarrier(cUnit);
1176 // For testing, always force through helper
1177 if (!EXERCISE_SLOWEST_STRING_PATH) {
buzbee82488f52012-03-02 08:20:26 -08001178 opIT(cUnit, kArmCondEq, "T");
buzbee31a4a6f2012-02-28 15:36:15 -08001179 }
buzbee82488f52012-03-02 08:20:26 -08001180 opRegCopy(cUnit, rARG0, rARG2); // .eq
buzbee31a4a6f2012-02-28 15:36:15 -08001181 opReg(cUnit, kOpBlx, rTgt); // .eq, helper(Method*, string_idx)
Ian Rogers6cbb2bd2012-03-16 13:45:30 -07001182 oatFreeTemp(cUnit, rTgt);
1183#elif defined(TARGET_MIPS)
buzbee82488f52012-03-02 08:20:26 -08001184 LIR* branch = opCmpImmBranch(cUnit, kCondNe, rRET0, 0, NULL);
1185 opRegCopy(cUnit, rARG0, rARG2); // .eq
buzbee31a4a6f2012-02-28 15:36:15 -08001186 opReg(cUnit, kOpBlx, rTgt);
Ian Rogers6cbb2bd2012-03-16 13:45:30 -07001187 oatFreeTemp(cUnit, rTgt);
buzbee31a4a6f2012-02-28 15:36:15 -08001188 LIR* target = newLIR0(cUnit, kPseudoTargetLabel);
buzbee31a4a6f2012-02-28 15:36:15 -08001189 branch->target = target;
Ian Rogers6cbb2bd2012-03-16 13:45:30 -07001190#else
Ian Rogersab2b55d2012-03-18 00:06:11 -07001191 callRuntimeHelperRegReg(cUnit, OFFSETOF_MEMBER(Thread,
1192 pResolveStringFromCode),
1193 rARG2, rARG1);
buzbee31a4a6f2012-02-28 15:36:15 -08001194#endif
1195 genBarrier(cUnit);
Ian Rogersf7d9ad32012-03-13 18:45:39 -07001196 storeValue(cUnit, rlDest, oatGetReturn(cUnit, false));
buzbee31a4a6f2012-02-28 15:36:15 -08001197 } else {
buzbeee1965672012-03-11 18:39:19 -07001198 RegLocation rlMethod = loadCurrMethod(cUnit);
buzbee31a4a6f2012-02-28 15:36:15 -08001199 int resReg = oatAllocTemp(cUnit);
1200 RegLocation rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
buzbeee1965672012-03-11 18:39:19 -07001201 loadWordDisp(cUnit, rlMethod.lowReg,
buzbee31a4a6f2012-02-28 15:36:15 -08001202 Method::DexCacheStringsOffset().Int32Value(), resReg);
1203 loadWordDisp(cUnit, resReg, offset_of_string, rlResult.lowReg);
1204 storeValue(cUnit, rlDest, rlResult);
1205 }
1206}
1207
1208/*
1209 * Let helper function take care of everything. Will
1210 * call Class::NewInstanceFromCode(type_idx, method);
1211 */
1212void genNewInstance(CompilationUnit* cUnit, MIR* mir, RegLocation rlDest)
1213{
1214 oatFlushAllRegs(cUnit); /* Everything to home location */
1215 uint32_t type_idx = mir->dalvikInsn.vB;
1216 // alloc will always check for resolution, do we also need to verify
1217 // access because the verifier was unable to?
Ian Rogers6cbb2bd2012-03-16 13:45:30 -07001218 int funcOffset;
buzbee31a4a6f2012-02-28 15:36:15 -08001219 if (cUnit->compiler->CanAccessInstantiableTypeWithoutChecks(
1220 cUnit->method_idx, cUnit->dex_cache, *cUnit->dex_file, type_idx)) {
Ian Rogers6cbb2bd2012-03-16 13:45:30 -07001221 funcOffset = OFFSETOF_MEMBER(Thread, pAllocObjectFromCode);
buzbee31a4a6f2012-02-28 15:36:15 -08001222 } else {
Ian Rogers6cbb2bd2012-03-16 13:45:30 -07001223 funcOffset =
1224 OFFSETOF_MEMBER(Thread, pAllocObjectFromCodeWithAccessCheck);
buzbee31a4a6f2012-02-28 15:36:15 -08001225 }
Ian Rogersab2b55d2012-03-18 00:06:11 -07001226 callRuntimeHelperImmMethod(cUnit, funcOffset, type_idx);
Ian Rogersf7d9ad32012-03-13 18:45:39 -07001227 RegLocation rlResult = oatGetReturn(cUnit, false);
buzbee31a4a6f2012-02-28 15:36:15 -08001228 storeValue(cUnit, rlDest, rlResult);
1229}
1230
Ian Rogersab2b55d2012-03-18 00:06:11 -07001231void genThrow(CompilationUnit* cUnit, MIR* mir, RegLocation rlSrc)
1232{
1233 oatFlushAllRegs(cUnit);
1234 callRuntimeHelperRegLocation(cUnit, OFFSETOF_MEMBER(Thread, pDeliverException), rlSrc);
1235}
1236
buzbee31a4a6f2012-02-28 15:36:15 -08001237void genInstanceof(CompilationUnit* cUnit, MIR* mir, RegLocation rlDest,
1238 RegLocation rlSrc)
1239{
1240 oatFlushAllRegs(cUnit);
1241 // May generate a call - use explicit registers
1242 oatLockCallTemps(cUnit);
1243 uint32_t type_idx = mir->dalvikInsn.vC;
buzbee5de34942012-03-01 14:51:57 -08001244 loadCurrMethodDirect(cUnit, rARG1); // rARG1 <= current Method*
buzbee31a4a6f2012-02-28 15:36:15 -08001245 int classReg = rARG2; // rARG2 will hold the Class*
1246 if (!cUnit->compiler->CanAccessTypeWithoutChecks(cUnit->method_idx,
1247 cUnit->dex_cache,
1248 *cUnit->dex_file,
1249 type_idx)) {
1250 // Check we have access to type_idx and if not throw IllegalAccessError,
buzbee5de34942012-03-01 14:51:57 -08001251 // returns Class* in rARG0
Ian Rogersab2b55d2012-03-18 00:06:11 -07001252 callRuntimeHelperImm(cUnit,
1253 OFFSETOF_MEMBER(Thread, pInitializeTypeAndVerifyAccessFromCode),
1254 type_idx);
buzbee82488f52012-03-02 08:20:26 -08001255 opRegCopy(cUnit, classReg, rRET0); // Align usage with fast path
buzbee5de34942012-03-01 14:51:57 -08001256 loadValueDirectFixed(cUnit, rlSrc, rARG0); // rARG0 <= ref
buzbee31a4a6f2012-02-28 15:36:15 -08001257 } else {
buzbee5de34942012-03-01 14:51:57 -08001258 // Load dex cache entry into classReg (rARG2)
buzbee31a4a6f2012-02-28 15:36:15 -08001259 loadValueDirectFixed(cUnit, rlSrc, rARG0); // rARG0 <= ref
1260 loadWordDisp(cUnit, rARG1,
1261 Method::DexCacheResolvedTypesOffset().Int32Value(),
1262 classReg);
1263 int32_t offset_of_type =
1264 Array::DataOffset(sizeof(Class*)).Int32Value() + (sizeof(Class*)
1265 * type_idx);
1266 loadWordDisp(cUnit, classReg, offset_of_type, classReg);
1267 if (!cUnit->compiler->CanAssumeTypeIsPresentInDexCache(
1268 cUnit->dex_cache, type_idx)) {
1269 // Need to test presence of type in dex cache at runtime
buzbee82488f52012-03-02 08:20:26 -08001270 LIR* hopBranch = opCmpImmBranch(cUnit, kCondNe, classReg, 0, NULL);
buzbee31a4a6f2012-02-28 15:36:15 -08001271 // Not resolved
1272 // Call out to helper, which will return resolved type in rRET0
Ian Rogersab2b55d2012-03-18 00:06:11 -07001273 callRuntimeHelperImm(cUnit, OFFSETOF_MEMBER(Thread,
1274 pInitializeTypeFromCode),
1275 type_idx);
buzbee82488f52012-03-02 08:20:26 -08001276 opRegCopy(cUnit, rARG2, rRET0); // Align usage with fast path
buzbee31a4a6f2012-02-28 15:36:15 -08001277 loadValueDirectFixed(cUnit, rlSrc, rARG0); /* reload Ref */
1278 // Rejoin code paths
1279 LIR* hopTarget = newLIR0(cUnit, kPseudoTargetLabel);
buzbee31a4a6f2012-02-28 15:36:15 -08001280 hopBranch->target = (LIR*)hopTarget;
1281 }
1282 }
1283 /* rARG0 is ref, rARG2 is class. If ref==null, use directly as bool result */
buzbee82488f52012-03-02 08:20:26 -08001284 LIR* branch1 = opCmpImmBranch(cUnit, kCondEq, rARG0, 0, NULL);
Elliott Hughese84278b2012-03-22 10:06:53 -07001285 /* load object->klass_ */
buzbee31a4a6f2012-02-28 15:36:15 -08001286 DCHECK_EQ(Object::ClassOffset().Int32Value(), 0);
1287 loadWordDisp(cUnit, rARG0, Object::ClassOffset().Int32Value(), rARG1);
Elliott Hughese84278b2012-03-22 10:06:53 -07001288 /* rARG0 is ref, rARG1 is ref->klass_, rARG2 is class */
buzbee0398c422012-03-02 15:22:47 -08001289#if defined(TARGET_ARM)
1290 /* Uses conditional nullification */
buzbee31a4a6f2012-02-28 15:36:15 -08001291 int rTgt = loadHelper(cUnit, OFFSETOF_MEMBER(Thread,
1292 pInstanceofNonTrivialFromCode));
buzbee31a4a6f2012-02-28 15:36:15 -08001293 opRegReg(cUnit, kOpCmp, rARG1, rARG2); // Same?
buzbee82488f52012-03-02 08:20:26 -08001294 opIT(cUnit, kArmCondEq, "EE"); // if-convert the test
buzbee31a4a6f2012-02-28 15:36:15 -08001295 loadConstant(cUnit, rARG0, 1); // .eq case - load true
buzbee82488f52012-03-02 08:20:26 -08001296 opRegCopy(cUnit, rARG0, rARG2); // .ne case - arg0 <= class
buzbee31a4a6f2012-02-28 15:36:15 -08001297 opReg(cUnit, kOpBlx, rTgt); // .ne case: helper(class, ref->class)
Ian Rogersab2b55d2012-03-18 00:06:11 -07001298 oatFreeTemp(cUnit, rTgt);
buzbee31a4a6f2012-02-28 15:36:15 -08001299#else
buzbee0398c422012-03-02 15:22:47 -08001300 /* Uses branchovers */
1301 loadConstant(cUnit, rARG0, 1); // assume true
1302 LIR* branchover = opCmpBranch(cUnit, kCondEq, rARG1, rARG2, NULL);
Ian Rogers6cbb2bd2012-03-16 13:45:30 -07001303#if !defined(TARGET_X86)
buzbee0398c422012-03-02 15:22:47 -08001304 int rTgt = loadHelper(cUnit, OFFSETOF_MEMBER(Thread,
1305 pInstanceofNonTrivialFromCode));
1306 opRegCopy(cUnit, rARG0, rARG2); // .ne case - arg0 <= class
1307 opReg(cUnit, kOpBlx, rTgt); // .ne case: helper(class, ref->class)
Ian Rogersab2b55d2012-03-18 00:06:11 -07001308 oatFreeTemp(cUnit, rTgt);
Ian Rogers6cbb2bd2012-03-16 13:45:30 -07001309#else
1310 opRegCopy(cUnit, rARG0, rARG2);
Ian Rogersab2b55d2012-03-18 00:06:11 -07001311 opThreadMem(cUnit, kOpBlx,
1312 OFFSETOF_MEMBER(Thread, pInstanceofNonTrivialFromCode));
Ian Rogers6cbb2bd2012-03-16 13:45:30 -07001313#endif
buzbee31a4a6f2012-02-28 15:36:15 -08001314#endif
buzbee0398c422012-03-02 15:22:47 -08001315 oatClobberCalleeSave(cUnit);
1316 /* branch targets here */
buzbee31a4a6f2012-02-28 15:36:15 -08001317 LIR* target = newLIR0(cUnit, kPseudoTargetLabel);
Ian Rogersf7d9ad32012-03-13 18:45:39 -07001318 RegLocation rlResult = oatGetReturn(cUnit, false);
buzbee31a4a6f2012-02-28 15:36:15 -08001319 storeValue(cUnit, rlDest, rlResult);
buzbee0398c422012-03-02 15:22:47 -08001320 branch1->target = target;
1321#if !defined(TARGET_ARM)
1322 branchover->target = target;
1323#endif
buzbee31a4a6f2012-02-28 15:36:15 -08001324}
1325
1326void genCheckCast(CompilationUnit* cUnit, MIR* mir, RegLocation rlSrc)
1327{
1328 oatFlushAllRegs(cUnit);
1329 // May generate a call - use explicit registers
1330 oatLockCallTemps(cUnit);
1331 uint32_t type_idx = mir->dalvikInsn.vB;
1332 loadCurrMethodDirect(cUnit, rARG1); // rARG1 <= current Method*
1333 int classReg = rARG2; // rARG2 will hold the Class*
1334 if (!cUnit->compiler->CanAccessTypeWithoutChecks(cUnit->method_idx,
1335 cUnit->dex_cache,
1336 *cUnit->dex_file,
1337 type_idx)) {
1338 // Check we have access to type_idx and if not throw IllegalAccessError,
1339 // returns Class* in rRET0
Ian Rogersab2b55d2012-03-18 00:06:11 -07001340 // InitializeTypeAndVerifyAccess(idx, method)
1341 callRuntimeHelperImmReg(cUnit,
1342 OFFSETOF_MEMBER(Thread,
1343 pInitializeTypeAndVerifyAccessFromCode),
1344 type_idx, rARG1);
buzbee82488f52012-03-02 08:20:26 -08001345 opRegCopy(cUnit, classReg, rRET0); // Align usage with fast path
buzbee31a4a6f2012-02-28 15:36:15 -08001346 } else {
1347 // Load dex cache entry into classReg (rARG2)
1348 loadWordDisp(cUnit, rARG1,
1349 Method::DexCacheResolvedTypesOffset().Int32Value(),
1350 classReg);
1351 int32_t offset_of_type =
1352 Array::DataOffset(sizeof(Class*)).Int32Value() +
1353 (sizeof(Class*) * type_idx);
1354 loadWordDisp(cUnit, classReg, offset_of_type, classReg);
1355 if (!cUnit->compiler->CanAssumeTypeIsPresentInDexCache(
1356 cUnit->dex_cache, type_idx)) {
1357 // Need to test presence of type in dex cache at runtime
buzbee82488f52012-03-02 08:20:26 -08001358 LIR* hopBranch = opCmpImmBranch(cUnit, kCondNe, classReg, 0, NULL);
buzbee31a4a6f2012-02-28 15:36:15 -08001359 // Not resolved
buzbee5de34942012-03-01 14:51:57 -08001360 // Call out to helper, which will return resolved type in rARG0
Ian Rogersab2b55d2012-03-18 00:06:11 -07001361 // InitializeTypeFromCode(idx, method)
1362 callRuntimeHelperImmReg(cUnit,
1363 OFFSETOF_MEMBER(Thread, pInitializeTypeFromCode),
1364 type_idx, rARG1);
buzbee82488f52012-03-02 08:20:26 -08001365 opRegCopy(cUnit, classReg, rARG0); // Align usage with fast path
buzbee31a4a6f2012-02-28 15:36:15 -08001366 // Rejoin code paths
1367 LIR* hopTarget = newLIR0(cUnit, kPseudoTargetLabel);
buzbee31a4a6f2012-02-28 15:36:15 -08001368 hopBranch->target = (LIR*)hopTarget;
1369 }
1370 }
buzbee5de34942012-03-01 14:51:57 -08001371 // At this point, classReg (rARG2) has class
buzbee31a4a6f2012-02-28 15:36:15 -08001372 loadValueDirectFixed(cUnit, rlSrc, rARG0); // rARG0 <= ref
1373 /* Null is OK - continue */
buzbee82488f52012-03-02 08:20:26 -08001374 LIR* branch1 = opCmpImmBranch(cUnit, kCondEq, rARG0, 0, NULL);
Elliott Hughese84278b2012-03-22 10:06:53 -07001375 /* load object->klass_ */
buzbee31a4a6f2012-02-28 15:36:15 -08001376 DCHECK_EQ(Object::ClassOffset().Int32Value(), 0);
1377 loadWordDisp(cUnit, rARG0, Object::ClassOffset().Int32Value(), rARG1);
Elliott Hughese84278b2012-03-22 10:06:53 -07001378 /* rARG1 now contains object->klass_ */
Ian Rogersab2b55d2012-03-18 00:06:11 -07001379#if defined(TARGET_MIPS) || defined(TARGET_X86)
buzbee82488f52012-03-02 08:20:26 -08001380 LIR* branch2 = opCmpBranch(cUnit, kCondEq, rARG1, classReg, NULL);
Ian Rogersab2b55d2012-03-18 00:06:11 -07001381 callRuntimeHelperRegReg(cUnit, OFFSETOF_MEMBER(Thread,
1382 pCheckCastFromCode),
1383 rARG1, rARG2);
1384#else // defined(TARGET_ARM)
Ian Rogers6cbb2bd2012-03-16 13:45:30 -07001385 int rTgt = loadHelper(cUnit, OFFSETOF_MEMBER(Thread,
1386 pCheckCastFromCode));
1387 opRegReg(cUnit, kOpCmp, rARG1, classReg);
1388 LIR* branch2 = opCondBranch(cUnit, kCondEq, NULL); /* If eq, trivial yes */
1389 opRegCopy(cUnit, rARG0, rARG1);
1390 opRegCopy(cUnit, rARG1, rARG2);
Ian Rogersab2b55d2012-03-18 00:06:11 -07001391 oatClobberCalleeSave(cUnit);
1392 opReg(cUnit, kOpBlx, rTgt);
Ian Rogers6cbb2bd2012-03-16 13:45:30 -07001393 oatFreeTemp(cUnit, rTgt);
Ian Rogers6cbb2bd2012-03-16 13:45:30 -07001394#endif
buzbee31a4a6f2012-02-28 15:36:15 -08001395 /* branch target here */
1396 LIR* target = newLIR0(cUnit, kPseudoTargetLabel);
buzbee31a4a6f2012-02-28 15:36:15 -08001397 branch1->target = (LIR*)target;
1398 branch2->target = (LIR*)target;
1399}
1400
buzbee31a4a6f2012-02-28 15:36:15 -08001401/*
1402 * Generate array store
1403 *
1404 */
1405void genArrayObjPut(CompilationUnit* cUnit, MIR* mir, RegLocation rlArray,
1406 RegLocation rlIndex, RegLocation rlSrc, int scale)
1407{
1408 RegisterClass regClass = oatRegClassBySize(kWord);
1409 int lenOffset = Array::LengthOffset().Int32Value();
1410 int dataOffset = Array::DataOffset(sizeof(Object*)).Int32Value();
1411
1412 oatFlushAllRegs(cUnit);
1413 /* Make sure it's a legal object Put. Use direct regs at first */
1414 loadValueDirectFixed(cUnit, rlArray, rARG1);
1415 loadValueDirectFixed(cUnit, rlSrc, rARG0);
1416
1417 /* null array object? */
1418 genNullCheck(cUnit, rlArray.sRegLow, rARG1, mir);
Elliott Hughese84278b2012-03-22 10:06:53 -07001419 /* Get the array's class */
buzbee31a4a6f2012-02-28 15:36:15 -08001420 loadWordDisp(cUnit, rARG1, Object::ClassOffset().Int32Value(), rARG1);
Ian Rogersab2b55d2012-03-18 00:06:11 -07001421 callRuntimeHelperRegReg(cUnit, OFFSETOF_MEMBER(Thread,
1422 pCanPutArrayElementFromCode),
1423 rARG0, rARG1);
buzbee31a4a6f2012-02-28 15:36:15 -08001424 oatFreeTemp(cUnit, rARG0);
1425 oatFreeTemp(cUnit, rARG1);
1426
1427 // Now, redo loadValues in case they didn't survive the call
1428
buzbee31a4a6f2012-02-28 15:36:15 -08001429 rlArray = loadValue(cUnit, rlArray, kCoreReg);
1430 rlIndex = loadValue(cUnit, rlIndex, kCoreReg);
1431
Ian Rogersb41b33b2012-03-20 14:22:54 -07001432#if defined(TARGET_X86)
1433 if (!(mir->optimizationFlags & MIR_IGNORE_RANGE_CHECK)) {
1434 /* if (rlIndex >= [rlArray + lenOffset]) goto kThrowArrayBounds */
1435 genRegMemCheck(cUnit, kCondUge, rlIndex.lowReg, rlArray.lowReg,
1436 lenOffset, mir, kThrowArrayBounds);
1437 }
1438 rlSrc = loadValue(cUnit, rlSrc, regClass);
1439 storeBaseIndexedDisp(cUnit, NULL, rlArray.lowReg, rlIndex.lowReg, scale,
1440 dataOffset, rlSrc.lowReg, INVALID_REG, kWord,
1441 INVALID_SREG);
Ian Rogersc6f3bb82012-03-21 20:40:33 -07001442 if (oatIsTemp(cUnit, rlIndex.lowReg)) {
1443 oatFreeTemp(cUnit, rlIndex.lowReg);
1444 }
Ian Rogersb41b33b2012-03-20 14:22:54 -07001445#else
1446 int regPtr;
buzbee31a4a6f2012-02-28 15:36:15 -08001447 if (oatIsTemp(cUnit, rlArray.lowReg)) {
1448 oatClobber(cUnit, rlArray.lowReg);
1449 regPtr = rlArray.lowReg;
1450 } else {
1451 regPtr = oatAllocTemp(cUnit);
buzbee82488f52012-03-02 08:20:26 -08001452 opRegCopy(cUnit, regPtr, rlArray.lowReg);
buzbee31a4a6f2012-02-28 15:36:15 -08001453 }
1454
buzbee239c4e72012-03-16 08:42:29 -07001455 bool needsRangeCheck = (!(mir->optimizationFlags & MIR_IGNORE_RANGE_CHECK));
1456 int regLen = INVALID_REG;
1457 if (needsRangeCheck) {
1458 regLen = oatAllocTemp(cUnit);
buzbee31a4a6f2012-02-28 15:36:15 -08001459 //NOTE: max live temps(4) here.
1460 /* Get len */
1461 loadWordDisp(cUnit, rlArray.lowReg, lenOffset, regLen);
buzbee239c4e72012-03-16 08:42:29 -07001462 }
1463 /* regPtr -> array data */
1464 opRegImm(cUnit, kOpAdd, regPtr, dataOffset);
1465 /* at this point, regPtr points to array, 2 live temps */
1466 rlSrc = loadValue(cUnit, rlSrc, regClass);
1467 if (needsRangeCheck) {
buzbee31a4a6f2012-02-28 15:36:15 -08001468 genRegRegCheck(cUnit, kCondCs, rlIndex.lowReg, regLen, mir,
1469 kThrowArrayBounds);
1470 oatFreeTemp(cUnit, regLen);
buzbee31a4a6f2012-02-28 15:36:15 -08001471 }
buzbee31a4a6f2012-02-28 15:36:15 -08001472 storeBaseIndexed(cUnit, regPtr, rlIndex.lowReg, rlSrc.lowReg,
1473 scale, kWord);
Ian Rogersb41b33b2012-03-20 14:22:54 -07001474#endif
buzbeea7c12682012-03-19 13:13:53 -07001475 markGCCard(cUnit, rlSrc.lowReg, rlArray.lowReg);
buzbee31a4a6f2012-02-28 15:36:15 -08001476}
1477
1478/*
1479 * Generate array load
1480 */
1481void genArrayGet(CompilationUnit* cUnit, MIR* mir, OpSize size,
1482 RegLocation rlArray, RegLocation rlIndex,
1483 RegLocation rlDest, int scale)
1484{
1485 RegisterClass regClass = oatRegClassBySize(size);
1486 int lenOffset = Array::LengthOffset().Int32Value();
1487 int dataOffset;
1488 RegLocation rlResult;
1489 rlArray = loadValue(cUnit, rlArray, kCoreReg);
1490 rlIndex = loadValue(cUnit, rlIndex, kCoreReg);
buzbee31a4a6f2012-02-28 15:36:15 -08001491
1492 if (size == kLong || size == kDouble) {
1493 dataOffset = Array::DataOffset(sizeof(int64_t)).Int32Value();
1494 } else {
1495 dataOffset = Array::DataOffset(sizeof(int32_t)).Int32Value();
1496 }
1497
1498 /* null object? */
1499 genNullCheck(cUnit, rlArray.sRegLow, rlArray.lowReg, mir);
1500
Ian Rogersb5d09b22012-03-06 22:14:17 -08001501#if defined(TARGET_X86)
1502 if (!(mir->optimizationFlags & MIR_IGNORE_RANGE_CHECK)) {
1503 /* if (rlIndex >= [rlArray + lenOffset]) goto kThrowArrayBounds */
1504 genRegMemCheck(cUnit, kCondUge, rlIndex.lowReg, rlArray.lowReg,
1505 lenOffset, mir, kThrowArrayBounds);
1506 }
1507 if ((size == kLong) || (size == kDouble)) {
1508 rlResult = oatEvalLoc(cUnit, rlDest, regClass, true);
1509 loadBaseIndexedDisp(cUnit, NULL, rlArray.lowReg, rlIndex.lowReg, scale, dataOffset,
1510 rlResult.lowReg, rlResult.highReg, size, INVALID_SREG);
buzbee31a4a6f2012-02-28 15:36:15 -08001511
Ian Rogersb5d09b22012-03-06 22:14:17 -08001512 storeValueWide(cUnit, rlDest, rlResult);
1513 } else {
1514 rlResult = oatEvalLoc(cUnit, rlDest, regClass, true);
1515
1516 loadBaseIndexedDisp(cUnit, NULL, rlArray.lowReg, rlIndex.lowReg, scale, dataOffset,
1517 rlResult.lowReg, INVALID_REG, size, INVALID_SREG);
1518
1519 storeValue(cUnit, rlDest, rlResult);
1520 }
1521#else
1522 int regPtr = oatAllocTemp(cUnit);
buzbee239c4e72012-03-16 08:42:29 -07001523 bool needsRangeCheck = (!(mir->optimizationFlags & MIR_IGNORE_RANGE_CHECK));
1524 int regLen = INVALID_REG;
1525 if (needsRangeCheck) {
1526 regLen = oatAllocTemp(cUnit);
buzbee31a4a6f2012-02-28 15:36:15 -08001527 /* Get len */
1528 loadWordDisp(cUnit, rlArray.lowReg, lenOffset, regLen);
buzbee31a4a6f2012-02-28 15:36:15 -08001529 }
buzbee239c4e72012-03-16 08:42:29 -07001530 /* regPtr -> array data */
1531 opRegRegImm(cUnit, kOpAdd, regPtr, rlArray.lowReg, dataOffset);
buzbee31a4a6f2012-02-28 15:36:15 -08001532 oatFreeTemp(cUnit, rlArray.lowReg);
1533 if ((size == kLong) || (size == kDouble)) {
1534 if (scale) {
1535 int rNewIndex = oatAllocTemp(cUnit);
1536 opRegRegImm(cUnit, kOpLsl, rNewIndex, rlIndex.lowReg, scale);
1537 opRegReg(cUnit, kOpAdd, regPtr, rNewIndex);
1538 oatFreeTemp(cUnit, rNewIndex);
1539 } else {
1540 opRegReg(cUnit, kOpAdd, regPtr, rlIndex.lowReg);
1541 }
1542 oatFreeTemp(cUnit, rlIndex.lowReg);
1543 rlResult = oatEvalLoc(cUnit, rlDest, regClass, true);
1544
buzbee239c4e72012-03-16 08:42:29 -07001545 if (needsRangeCheck) {
1546 // TODO: change kCondCS to a more meaningful name, is the sense of
1547 // carry-set/clear flipped?
1548 genRegRegCheck(cUnit, kCondCs, rlIndex.lowReg, regLen, mir,
1549 kThrowArrayBounds);
1550 oatFreeTemp(cUnit, regLen);
1551 }
buzbee31a4a6f2012-02-28 15:36:15 -08001552 loadPair(cUnit, regPtr, rlResult.lowReg, rlResult.highReg);
1553
1554 oatFreeTemp(cUnit, regPtr);
1555 storeValueWide(cUnit, rlDest, rlResult);
1556 } else {
1557 rlResult = oatEvalLoc(cUnit, rlDest, regClass, true);
1558
buzbee239c4e72012-03-16 08:42:29 -07001559 if (needsRangeCheck) {
1560 // TODO: change kCondCS to a more meaningful name, is the sense of
1561 // carry-set/clear flipped?
1562 genRegRegCheck(cUnit, kCondCs, rlIndex.lowReg, regLen, mir,
1563 kThrowArrayBounds);
1564 oatFreeTemp(cUnit, regLen);
1565 }
buzbee31a4a6f2012-02-28 15:36:15 -08001566 loadBaseIndexed(cUnit, regPtr, rlIndex.lowReg, rlResult.lowReg,
1567 scale, size);
1568
1569 oatFreeTemp(cUnit, regPtr);
1570 storeValue(cUnit, rlDest, rlResult);
1571 }
Ian Rogersb5d09b22012-03-06 22:14:17 -08001572#endif
buzbee31a4a6f2012-02-28 15:36:15 -08001573}
1574
1575/*
1576 * Generate array store
1577 *
1578 */
1579void genArrayPut(CompilationUnit* cUnit, MIR* mir, OpSize size,
1580 RegLocation rlArray, RegLocation rlIndex,
1581 RegLocation rlSrc, int scale)
1582{
1583 RegisterClass regClass = oatRegClassBySize(size);
1584 int lenOffset = Array::LengthOffset().Int32Value();
1585 int dataOffset;
1586
1587 if (size == kLong || size == kDouble) {
1588 dataOffset = Array::DataOffset(sizeof(int64_t)).Int32Value();
1589 } else {
1590 dataOffset = Array::DataOffset(sizeof(int32_t)).Int32Value();
1591 }
1592
buzbee31a4a6f2012-02-28 15:36:15 -08001593 rlArray = loadValue(cUnit, rlArray, kCoreReg);
1594 rlIndex = loadValue(cUnit, rlIndex, kCoreReg);
Ian Rogersb41b33b2012-03-20 14:22:54 -07001595#if !defined(TARGET_X86)
1596 int regPtr;
buzbee31a4a6f2012-02-28 15:36:15 -08001597 if (oatIsTemp(cUnit, rlArray.lowReg)) {
1598 oatClobber(cUnit, rlArray.lowReg);
1599 regPtr = rlArray.lowReg;
1600 } else {
1601 regPtr = oatAllocTemp(cUnit);
buzbee82488f52012-03-02 08:20:26 -08001602 opRegCopy(cUnit, regPtr, rlArray.lowReg);
buzbee31a4a6f2012-02-28 15:36:15 -08001603 }
Ian Rogersb41b33b2012-03-20 14:22:54 -07001604#endif
buzbee31a4a6f2012-02-28 15:36:15 -08001605
1606 /* null object? */
1607 genNullCheck(cUnit, rlArray.sRegLow, rlArray.lowReg, mir);
1608
Ian Rogersb41b33b2012-03-20 14:22:54 -07001609#if defined(TARGET_X86)
1610 if (!(mir->optimizationFlags & MIR_IGNORE_RANGE_CHECK)) {
1611 /* if (rlIndex >= [rlArray + lenOffset]) goto kThrowArrayBounds */
1612 genRegMemCheck(cUnit, kCondUge, rlIndex.lowReg, rlArray.lowReg,
1613 lenOffset, mir, kThrowArrayBounds);
1614 }
Ian Rogersc6f3bb82012-03-21 20:40:33 -07001615 if ((size == kLong) || (size == kDouble)) {
1616 rlSrc = loadValueWide(cUnit, rlSrc, regClass);
1617 } else {
1618 rlSrc = loadValue(cUnit, rlSrc, regClass);
1619 }
Ian Rogersb41b33b2012-03-20 14:22:54 -07001620 storeBaseIndexedDisp(cUnit, NULL, rlArray.lowReg, rlIndex.lowReg, scale, dataOffset,
1621 rlSrc.lowReg, rlSrc.highReg, size, INVALID_SREG);
1622#else
buzbee239c4e72012-03-16 08:42:29 -07001623 bool needsRangeCheck = (!(mir->optimizationFlags & MIR_IGNORE_RANGE_CHECK));
1624 int regLen = INVALID_REG;
1625 if (needsRangeCheck) {
1626 regLen = oatAllocTemp(cUnit);
buzbee31a4a6f2012-02-28 15:36:15 -08001627 //NOTE: max live temps(4) here.
1628 /* Get len */
1629 loadWordDisp(cUnit, rlArray.lowReg, lenOffset, regLen);
buzbee31a4a6f2012-02-28 15:36:15 -08001630 }
buzbee239c4e72012-03-16 08:42:29 -07001631 /* regPtr -> array data */
1632 opRegImm(cUnit, kOpAdd, regPtr, dataOffset);
buzbee31a4a6f2012-02-28 15:36:15 -08001633 /* at this point, regPtr points to array, 2 live temps */
1634 if ((size == kLong) || (size == kDouble)) {
1635 //TUNING: specific wide routine that can handle fp regs
1636 if (scale) {
1637 int rNewIndex = oatAllocTemp(cUnit);
1638 opRegRegImm(cUnit, kOpLsl, rNewIndex, rlIndex.lowReg, scale);
1639 opRegReg(cUnit, kOpAdd, regPtr, rNewIndex);
1640 oatFreeTemp(cUnit, rNewIndex);
1641 } else {
1642 opRegReg(cUnit, kOpAdd, regPtr, rlIndex.lowReg);
1643 }
1644 rlSrc = loadValueWide(cUnit, rlSrc, regClass);
1645
buzbee239c4e72012-03-16 08:42:29 -07001646 if (needsRangeCheck) {
1647 genRegRegCheck(cUnit, kCondCs, rlIndex.lowReg, regLen, mir,
1648 kThrowArrayBounds);
1649 oatFreeTemp(cUnit, regLen);
1650 }
1651
buzbee31a4a6f2012-02-28 15:36:15 -08001652 storePair(cUnit, regPtr, rlSrc.lowReg, rlSrc.highReg);
1653
1654 oatFreeTemp(cUnit, regPtr);
1655 } else {
1656 rlSrc = loadValue(cUnit, rlSrc, regClass);
buzbee239c4e72012-03-16 08:42:29 -07001657 if (needsRangeCheck) {
1658 genRegRegCheck(cUnit, kCondCs, rlIndex.lowReg, regLen, mir,
1659 kThrowArrayBounds);
1660 oatFreeTemp(cUnit, regLen);
1661 }
buzbee31a4a6f2012-02-28 15:36:15 -08001662 storeBaseIndexed(cUnit, regPtr, rlIndex.lowReg, rlSrc.lowReg,
1663 scale, size);
1664 }
Ian Rogersb41b33b2012-03-20 14:22:54 -07001665#endif
buzbee31a4a6f2012-02-28 15:36:15 -08001666}
1667
1668void genLong3Addr(CompilationUnit* cUnit, MIR* mir, OpKind firstOp,
1669 OpKind secondOp, RegLocation rlDest,
1670 RegLocation rlSrc1, RegLocation rlSrc2)
1671{
1672 RegLocation rlResult;
1673#if defined(TARGET_ARM)
1674 /*
1675 * NOTE: This is the one place in the code in which we might have
1676 * as many as six live temporary registers. There are 5 in the normal
1677 * set for Arm. Until we have spill capabilities, temporarily add
1678 * lr to the temp set. It is safe to do this locally, but note that
1679 * lr is used explicitly elsewhere in the code generator and cannot
1680 * normally be used as a general temp register.
1681 */
1682 oatMarkTemp(cUnit, rLR); // Add lr to the temp pool
1683 oatFreeTemp(cUnit, rLR); // and make it available
1684#endif
1685 rlSrc1 = loadValueWide(cUnit, rlSrc1, kCoreReg);
1686 rlSrc2 = loadValueWide(cUnit, rlSrc2, kCoreReg);
1687 rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
1688 // The longs may overlap - use intermediate temp if so
1689 if (rlResult.lowReg == rlSrc1.highReg) {
1690 int tReg = oatAllocTemp(cUnit);
buzbee82488f52012-03-02 08:20:26 -08001691 opRegCopy(cUnit, tReg, rlSrc1.highReg);
buzbee31a4a6f2012-02-28 15:36:15 -08001692 opRegRegReg(cUnit, firstOp, rlResult.lowReg, rlSrc1.lowReg,
1693 rlSrc2.lowReg);
1694 opRegRegReg(cUnit, secondOp, rlResult.highReg, tReg,
1695 rlSrc2.highReg);
1696 oatFreeTemp(cUnit, tReg);
1697 } else {
1698 opRegRegReg(cUnit, firstOp, rlResult.lowReg, rlSrc1.lowReg,
1699 rlSrc2.lowReg);
1700 opRegRegReg(cUnit, secondOp, rlResult.highReg, rlSrc1.highReg,
1701 rlSrc2.highReg);
1702 }
1703 /*
1704 * NOTE: If rlDest refers to a frame variable in a large frame, the
1705 * following storeValueWide might need to allocate a temp register.
1706 * To further work around the lack of a spill capability, explicitly
1707 * free any temps from rlSrc1 & rlSrc2 that aren't still live in rlResult.
1708 * Remove when spill is functional.
1709 */
1710 freeRegLocTemps(cUnit, rlResult, rlSrc1);
1711 freeRegLocTemps(cUnit, rlResult, rlSrc2);
1712 storeValueWide(cUnit, rlDest, rlResult);
1713#if defined(TARGET_ARM)
1714 oatClobber(cUnit, rLR);
1715 oatUnmarkTemp(cUnit, rLR); // Remove lr from the temp pool
1716#endif
1717}
1718
1719
1720bool genShiftOpLong(CompilationUnit* cUnit, MIR* mir, RegLocation rlDest,
1721 RegLocation rlSrc1, RegLocation rlShift)
1722{
1723 int funcOffset;
1724
1725 switch( mir->dalvikInsn.opcode) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001726 case Instruction::SHL_LONG:
1727 case Instruction::SHL_LONG_2ADDR:
buzbee31a4a6f2012-02-28 15:36:15 -08001728 funcOffset = OFFSETOF_MEMBER(Thread, pShlLong);
1729 break;
Elliott Hughesadb8c672012-03-06 16:49:32 -08001730 case Instruction::SHR_LONG:
1731 case Instruction::SHR_LONG_2ADDR:
buzbee31a4a6f2012-02-28 15:36:15 -08001732 funcOffset = OFFSETOF_MEMBER(Thread, pShrLong);
1733 break;
Elliott Hughesadb8c672012-03-06 16:49:32 -08001734 case Instruction::USHR_LONG:
1735 case Instruction::USHR_LONG_2ADDR:
buzbee31a4a6f2012-02-28 15:36:15 -08001736 funcOffset = OFFSETOF_MEMBER(Thread, pUshrLong);
1737 break;
1738 default:
1739 LOG(FATAL) << "Unexpected case";
1740 return true;
1741 }
1742 oatFlushAllRegs(cUnit); /* Send everything to home location */
Ian Rogersab2b55d2012-03-18 00:06:11 -07001743 callRuntimeHelperRegLocationRegLocation(cUnit, funcOffset, rlSrc1, rlShift);
Ian Rogersf7d9ad32012-03-13 18:45:39 -07001744 RegLocation rlResult = oatGetReturnWide(cUnit, false);
buzbee31a4a6f2012-02-28 15:36:15 -08001745 storeValueWide(cUnit, rlDest, rlResult);
1746 return false;
1747}
1748
1749
1750bool genArithOpInt(CompilationUnit* cUnit, MIR* mir, RegLocation rlDest,
1751 RegLocation rlSrc1, RegLocation rlSrc2)
1752{
1753 OpKind op = kOpBkpt;
1754 bool callOut = false;
1755 bool checkZero = false;
1756 bool unary = false;
buzbee31a4a6f2012-02-28 15:36:15 -08001757 RegLocation rlResult;
1758 bool shiftOp = false;
Ian Rogersb5d09b22012-03-06 22:14:17 -08001759 int funcOffset;
1760 int retReg = rRET0;
buzbee31a4a6f2012-02-28 15:36:15 -08001761 switch (mir->dalvikInsn.opcode) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001762 case Instruction::NEG_INT:
buzbee31a4a6f2012-02-28 15:36:15 -08001763 op = kOpNeg;
1764 unary = true;
1765 break;
Elliott Hughesadb8c672012-03-06 16:49:32 -08001766 case Instruction::NOT_INT:
buzbee31a4a6f2012-02-28 15:36:15 -08001767 op = kOpMvn;
1768 unary = true;
1769 break;
Elliott Hughesadb8c672012-03-06 16:49:32 -08001770 case Instruction::ADD_INT:
1771 case Instruction::ADD_INT_2ADDR:
buzbee31a4a6f2012-02-28 15:36:15 -08001772 op = kOpAdd;
1773 break;
Elliott Hughesadb8c672012-03-06 16:49:32 -08001774 case Instruction::SUB_INT:
1775 case Instruction::SUB_INT_2ADDR:
buzbee31a4a6f2012-02-28 15:36:15 -08001776 op = kOpSub;
1777 break;
Elliott Hughesadb8c672012-03-06 16:49:32 -08001778 case Instruction::MUL_INT:
1779 case Instruction::MUL_INT_2ADDR:
buzbee31a4a6f2012-02-28 15:36:15 -08001780 op = kOpMul;
1781 break;
Elliott Hughesadb8c672012-03-06 16:49:32 -08001782 case Instruction::DIV_INT:
1783 case Instruction::DIV_INT_2ADDR:
buzbee31a4a6f2012-02-28 15:36:15 -08001784 checkZero = true;
Ian Rogersb5d09b22012-03-06 22:14:17 -08001785 op = kOpDiv;
1786 callOut = true;
buzbee31a4a6f2012-02-28 15:36:15 -08001787 funcOffset = OFFSETOF_MEMBER(Thread, pIdiv);
1788 retReg = rRET0;
1789 break;
buzbee5de34942012-03-01 14:51:57 -08001790 /* NOTE: returns in rARG1 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08001791 case Instruction::REM_INT:
1792 case Instruction::REM_INT_2ADDR:
buzbee31a4a6f2012-02-28 15:36:15 -08001793 checkZero = true;
Ian Rogersb5d09b22012-03-06 22:14:17 -08001794 op = kOpRem;
1795 callOut = true;
buzbee31a4a6f2012-02-28 15:36:15 -08001796 funcOffset = OFFSETOF_MEMBER(Thread, pIdivmod);
1797 retReg = rRET1;
1798 break;
Elliott Hughesadb8c672012-03-06 16:49:32 -08001799 case Instruction::AND_INT:
1800 case Instruction::AND_INT_2ADDR:
buzbee31a4a6f2012-02-28 15:36:15 -08001801 op = kOpAnd;
1802 break;
Elliott Hughesadb8c672012-03-06 16:49:32 -08001803 case Instruction::OR_INT:
1804 case Instruction::OR_INT_2ADDR:
buzbee31a4a6f2012-02-28 15:36:15 -08001805 op = kOpOr;
1806 break;
Elliott Hughesadb8c672012-03-06 16:49:32 -08001807 case Instruction::XOR_INT:
1808 case Instruction::XOR_INT_2ADDR:
buzbee31a4a6f2012-02-28 15:36:15 -08001809 op = kOpXor;
1810 break;
Elliott Hughesadb8c672012-03-06 16:49:32 -08001811 case Instruction::SHL_INT:
1812 case Instruction::SHL_INT_2ADDR:
buzbee31a4a6f2012-02-28 15:36:15 -08001813 shiftOp = true;
1814 op = kOpLsl;
1815 break;
Elliott Hughesadb8c672012-03-06 16:49:32 -08001816 case Instruction::SHR_INT:
1817 case Instruction::SHR_INT_2ADDR:
buzbee31a4a6f2012-02-28 15:36:15 -08001818 shiftOp = true;
1819 op = kOpAsr;
1820 break;
Elliott Hughesadb8c672012-03-06 16:49:32 -08001821 case Instruction::USHR_INT:
1822 case Instruction::USHR_INT_2ADDR:
buzbee31a4a6f2012-02-28 15:36:15 -08001823 shiftOp = true;
1824 op = kOpLsr;
1825 break;
1826 default:
1827 LOG(FATAL) << "Invalid word arith op: " <<
1828 (int)mir->dalvikInsn.opcode;
1829 }
1830 if (!callOut) {
1831 rlSrc1 = loadValue(cUnit, rlSrc1, kCoreReg);
1832 if (unary) {
1833 rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
1834 opRegReg(cUnit, op, rlResult.lowReg,
1835 rlSrc1.lowReg);
1836 } else {
1837 rlSrc2 = loadValue(cUnit, rlSrc2, kCoreReg);
Ian Rogersb5d09b22012-03-06 22:14:17 -08001838#if defined(TARGET_X86)
1839 rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
1840 opRegRegReg(cUnit, op, rlResult.lowReg,
1841 rlSrc1.lowReg, rlSrc2.lowReg);
1842#else
buzbee31a4a6f2012-02-28 15:36:15 -08001843 if (shiftOp) {
1844 int tReg = oatAllocTemp(cUnit);
1845 opRegRegImm(cUnit, kOpAnd, tReg, rlSrc2.lowReg, 31);
1846 rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
1847 opRegRegReg(cUnit, op, rlResult.lowReg,
1848 rlSrc1.lowReg, tReg);
1849 oatFreeTemp(cUnit, tReg);
1850 } else {
1851 rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
1852 opRegRegReg(cUnit, op, rlResult.lowReg,
1853 rlSrc1.lowReg, rlSrc2.lowReg);
1854 }
Ian Rogersb5d09b22012-03-06 22:14:17 -08001855#endif
buzbee31a4a6f2012-02-28 15:36:15 -08001856 }
1857 storeValue(cUnit, rlDest, rlResult);
1858 } else {
1859 RegLocation rlResult;
1860 oatFlushAllRegs(cUnit); /* Send everything to home location */
1861 loadValueDirectFixed(cUnit, rlSrc2, rRET1);
Ian Rogers6cbb2bd2012-03-16 13:45:30 -07001862#if !defined(TARGET_X86)
buzbee31a4a6f2012-02-28 15:36:15 -08001863 int rTgt = loadHelper(cUnit, funcOffset);
Ian Rogersab2b55d2012-03-18 00:06:11 -07001864#endif
buzbee31a4a6f2012-02-28 15:36:15 -08001865 loadValueDirectFixed(cUnit, rlSrc1, rARG0);
1866 if (checkZero) {
1867 genImmedCheck(cUnit, kCondEq, rARG1, 0, mir, kThrowDivZero);
1868 }
Ian Rogersab2b55d2012-03-18 00:06:11 -07001869#if !defined(TARGET_X86)
1870 opReg(cUnit, kOpBlx, rTgt);
Ian Rogers6cbb2bd2012-03-16 13:45:30 -07001871 oatFreeTemp(cUnit, rTgt);
1872#else
Ian Rogersab2b55d2012-03-18 00:06:11 -07001873 opThreadMem(cUnit, kOpBlx, funcOffset);
Ian Rogers6cbb2bd2012-03-16 13:45:30 -07001874#endif
buzbee31a4a6f2012-02-28 15:36:15 -08001875 if (retReg == rRET0)
Ian Rogersf7d9ad32012-03-13 18:45:39 -07001876 rlResult = oatGetReturn(cUnit, false);
buzbee31a4a6f2012-02-28 15:36:15 -08001877 else
1878 rlResult = oatGetReturnAlt(cUnit);
1879 storeValue(cUnit, rlDest, rlResult);
1880 }
1881 return false;
1882}
1883
1884/*
1885 * The following are the first-level codegen routines that analyze the format
1886 * of each bytecode then either dispatch special purpose codegen routines
1887 * or produce corresponding Thumb instructions directly.
1888 */
1889
1890bool isPowerOfTwo(int x)
1891{
1892 return (x & (x - 1)) == 0;
1893}
1894
1895// Returns true if no more than two bits are set in 'x'.
1896bool isPopCountLE2(unsigned int x)
1897{
1898 x &= x - 1;
1899 return (x & (x - 1)) == 0;
1900}
1901
1902// Returns the index of the lowest set bit in 'x'.
1903int lowestSetBit(unsigned int x) {
1904 int bit_posn = 0;
1905 while ((x & 0xf) == 0) {
1906 bit_posn += 4;
1907 x >>= 4;
1908 }
1909 while ((x & 1) == 0) {
1910 bit_posn++;
1911 x >>= 1;
1912 }
1913 return bit_posn;
1914}
1915
1916// Returns true if it added instructions to 'cUnit' to divide 'rlSrc' by 'lit'
1917// and store the result in 'rlDest'.
Elliott Hughesadb8c672012-03-06 16:49:32 -08001918bool handleEasyDivide(CompilationUnit* cUnit, Instruction::Code dalvikOpcode,
buzbee31a4a6f2012-02-28 15:36:15 -08001919 RegLocation rlSrc, RegLocation rlDest, int lit)
1920{
1921 if (lit < 2 || !isPowerOfTwo(lit)) {
1922 return false;
1923 }
1924 int k = lowestSetBit(lit);
1925 if (k >= 30) {
1926 // Avoid special cases.
1927 return false;
1928 }
Elliott Hughesadb8c672012-03-06 16:49:32 -08001929 bool div = (dalvikOpcode == Instruction::DIV_INT_LIT8 ||
1930 dalvikOpcode == Instruction::DIV_INT_LIT16);
buzbee31a4a6f2012-02-28 15:36:15 -08001931 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
1932 RegLocation rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
1933 if (div) {
1934 int tReg = oatAllocTemp(cUnit);
1935 if (lit == 2) {
1936 // Division by 2 is by far the most common division by constant.
1937 opRegRegImm(cUnit, kOpLsr, tReg, rlSrc.lowReg, 32 - k);
1938 opRegRegReg(cUnit, kOpAdd, tReg, tReg, rlSrc.lowReg);
1939 opRegRegImm(cUnit, kOpAsr, rlResult.lowReg, tReg, k);
1940 } else {
1941 opRegRegImm(cUnit, kOpAsr, tReg, rlSrc.lowReg, 31);
1942 opRegRegImm(cUnit, kOpLsr, tReg, tReg, 32 - k);
1943 opRegRegReg(cUnit, kOpAdd, tReg, tReg, rlSrc.lowReg);
1944 opRegRegImm(cUnit, kOpAsr, rlResult.lowReg, tReg, k);
1945 }
1946 } else {
buzbee31a4a6f2012-02-28 15:36:15 -08001947 int tReg1 = oatAllocTemp(cUnit);
1948 int tReg2 = oatAllocTemp(cUnit);
1949 if (lit == 2) {
1950 opRegRegImm(cUnit, kOpLsr, tReg1, rlSrc.lowReg, 32 - k);
1951 opRegRegReg(cUnit, kOpAdd, tReg2, tReg1, rlSrc.lowReg);
Ian Rogersb41b33b2012-03-20 14:22:54 -07001952 opRegRegImm(cUnit, kOpAnd, tReg2, tReg2, lit -1);
buzbee31a4a6f2012-02-28 15:36:15 -08001953 opRegRegReg(cUnit, kOpSub, rlResult.lowReg, tReg2, tReg1);
1954 } else {
1955 opRegRegImm(cUnit, kOpAsr, tReg1, rlSrc.lowReg, 31);
1956 opRegRegImm(cUnit, kOpLsr, tReg1, tReg1, 32 - k);
1957 opRegRegReg(cUnit, kOpAdd, tReg2, tReg1, rlSrc.lowReg);
Ian Rogersb41b33b2012-03-20 14:22:54 -07001958 opRegRegImm(cUnit, kOpAnd, tReg2, tReg2, lit - 1);
buzbee31a4a6f2012-02-28 15:36:15 -08001959 opRegRegReg(cUnit, kOpSub, rlResult.lowReg, tReg2, tReg1);
1960 }
1961 }
1962 storeValue(cUnit, rlDest, rlResult);
1963 return true;
1964}
1965
1966void genMultiplyByTwoBitMultiplier(CompilationUnit* cUnit, RegLocation rlSrc,
1967 RegLocation rlResult, int lit,
1968 int firstBit, int secondBit)
1969{
buzbee0398c422012-03-02 15:22:47 -08001970#if defined(TARGET_ARM)
buzbee31a4a6f2012-02-28 15:36:15 -08001971 opRegRegRegShift(cUnit, kOpAdd, rlResult.lowReg, rlSrc.lowReg, rlSrc.lowReg,
1972 encodeShift(kArmLsl, secondBit - firstBit));
buzbee0398c422012-03-02 15:22:47 -08001973#else
1974 int tReg = oatAllocTemp(cUnit);
1975 opRegRegImm(cUnit, kOpLsl, tReg, rlSrc.lowReg, secondBit - firstBit);
1976 opRegRegReg(cUnit, kOpAdd, rlResult.lowReg, rlSrc.lowReg, tReg);
1977 oatFreeTemp(cUnit, tReg);
buzbee5de34942012-03-01 14:51:57 -08001978#endif
buzbee31a4a6f2012-02-28 15:36:15 -08001979 if (firstBit != 0) {
1980 opRegRegImm(cUnit, kOpLsl, rlResult.lowReg, rlResult.lowReg, firstBit);
1981 }
1982}
1983
1984// Returns true if it added instructions to 'cUnit' to multiply 'rlSrc' by 'lit'
1985// and store the result in 'rlDest'.
1986bool handleEasyMultiply(CompilationUnit* cUnit, RegLocation rlSrc,
1987 RegLocation rlDest, int lit)
1988{
1989 // Can we simplify this multiplication?
1990 bool powerOfTwo = false;
1991 bool popCountLE2 = false;
1992 bool powerOfTwoMinusOne = false;
1993 if (lit < 2) {
1994 // Avoid special cases.
1995 return false;
1996 } else if (isPowerOfTwo(lit)) {
1997 powerOfTwo = true;
1998 } else if (isPopCountLE2(lit)) {
1999 popCountLE2 = true;
2000 } else if (isPowerOfTwo(lit + 1)) {
2001 powerOfTwoMinusOne = true;
2002 } else {
2003 return false;
2004 }
2005 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
2006 RegLocation rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
2007 if (powerOfTwo) {
2008 // Shift.
2009 opRegRegImm(cUnit, kOpLsl, rlResult.lowReg, rlSrc.lowReg,
2010 lowestSetBit(lit));
2011 } else if (popCountLE2) {
2012 // Shift and add and shift.
2013 int firstBit = lowestSetBit(lit);
2014 int secondBit = lowestSetBit(lit ^ (1 << firstBit));
2015 genMultiplyByTwoBitMultiplier(cUnit, rlSrc, rlResult, lit,
2016 firstBit, secondBit);
2017 } else {
2018 // Reverse subtract: (src << (shift + 1)) - src.
2019 DCHECK(powerOfTwoMinusOne);
2020 // TUNING: rsb dst, src, src lsl#lowestSetBit(lit + 1)
2021 int tReg = oatAllocTemp(cUnit);
2022 opRegRegImm(cUnit, kOpLsl, tReg, rlSrc.lowReg, lowestSetBit(lit + 1));
2023 opRegRegReg(cUnit, kOpSub, rlResult.lowReg, tReg, rlSrc.lowReg);
2024 }
2025 storeValue(cUnit, rlDest, rlResult);
2026 return true;
2027}
2028
2029bool genArithOpIntLit(CompilationUnit* cUnit, MIR* mir, RegLocation rlDest,
2030 RegLocation rlSrc, int lit)
2031{
Elliott Hughesadb8c672012-03-06 16:49:32 -08002032 Instruction::Code dalvikOpcode = mir->dalvikInsn.opcode;
buzbee31a4a6f2012-02-28 15:36:15 -08002033 RegLocation rlResult;
2034 OpKind op = (OpKind)0; /* Make gcc happy */
2035 int shiftOp = false;
2036 bool isDiv = false;
2037 int funcOffset;
buzbee31a4a6f2012-02-28 15:36:15 -08002038
2039 switch (dalvikOpcode) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002040 case Instruction::RSUB_INT_LIT8:
2041 case Instruction::RSUB_INT: {
buzbee31a4a6f2012-02-28 15:36:15 -08002042 int tReg;
2043 //TUNING: add support for use of Arm rsub op
2044 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
2045 tReg = oatAllocTemp(cUnit);
2046 loadConstant(cUnit, tReg, lit);
2047 rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
2048 opRegRegReg(cUnit, kOpSub, rlResult.lowReg,
2049 tReg, rlSrc.lowReg);
2050 storeValue(cUnit, rlDest, rlResult);
2051 return false;
2052 break;
2053 }
2054
Elliott Hughesadb8c672012-03-06 16:49:32 -08002055 case Instruction::ADD_INT_LIT8:
2056 case Instruction::ADD_INT_LIT16:
buzbee31a4a6f2012-02-28 15:36:15 -08002057 op = kOpAdd;
2058 break;
Elliott Hughesadb8c672012-03-06 16:49:32 -08002059 case Instruction::MUL_INT_LIT8:
2060 case Instruction::MUL_INT_LIT16: {
buzbee31a4a6f2012-02-28 15:36:15 -08002061 if (handleEasyMultiply(cUnit, rlSrc, rlDest, lit)) {
2062 return false;
2063 }
2064 op = kOpMul;
2065 break;
2066 }
Elliott Hughesadb8c672012-03-06 16:49:32 -08002067 case Instruction::AND_INT_LIT8:
2068 case Instruction::AND_INT_LIT16:
buzbee31a4a6f2012-02-28 15:36:15 -08002069 op = kOpAnd;
2070 break;
Elliott Hughesadb8c672012-03-06 16:49:32 -08002071 case Instruction::OR_INT_LIT8:
2072 case Instruction::OR_INT_LIT16:
buzbee31a4a6f2012-02-28 15:36:15 -08002073 op = kOpOr;
2074 break;
Elliott Hughesadb8c672012-03-06 16:49:32 -08002075 case Instruction::XOR_INT_LIT8:
2076 case Instruction::XOR_INT_LIT16:
buzbee31a4a6f2012-02-28 15:36:15 -08002077 op = kOpXor;
2078 break;
Elliott Hughesadb8c672012-03-06 16:49:32 -08002079 case Instruction::SHL_INT_LIT8:
buzbee31a4a6f2012-02-28 15:36:15 -08002080 lit &= 31;
2081 shiftOp = true;
2082 op = kOpLsl;
2083 break;
Elliott Hughesadb8c672012-03-06 16:49:32 -08002084 case Instruction::SHR_INT_LIT8:
buzbee31a4a6f2012-02-28 15:36:15 -08002085 lit &= 31;
2086 shiftOp = true;
2087 op = kOpAsr;
2088 break;
Elliott Hughesadb8c672012-03-06 16:49:32 -08002089 case Instruction::USHR_INT_LIT8:
buzbee31a4a6f2012-02-28 15:36:15 -08002090 lit &= 31;
2091 shiftOp = true;
2092 op = kOpLsr;
2093 break;
2094
Elliott Hughesadb8c672012-03-06 16:49:32 -08002095 case Instruction::DIV_INT_LIT8:
2096 case Instruction::DIV_INT_LIT16:
2097 case Instruction::REM_INT_LIT8:
2098 case Instruction::REM_INT_LIT16:
buzbee31a4a6f2012-02-28 15:36:15 -08002099 if (lit == 0) {
2100 genImmedCheck(cUnit, kCondAl, 0, 0, mir, kThrowDivZero);
2101 return false;
2102 }
2103 if (handleEasyDivide(cUnit, dalvikOpcode, rlSrc, rlDest, lit)) {
2104 return false;
2105 }
2106 oatFlushAllRegs(cUnit); /* Everything to home location */
2107 loadValueDirectFixed(cUnit, rlSrc, rARG0);
2108 oatClobber(cUnit, rARG0);
Elliott Hughesadb8c672012-03-06 16:49:32 -08002109 if ((dalvikOpcode == Instruction::DIV_INT_LIT8) ||
2110 (dalvikOpcode == Instruction::DIV_INT_LIT16)) {
buzbee31a4a6f2012-02-28 15:36:15 -08002111 funcOffset = OFFSETOF_MEMBER(Thread, pIdiv);
2112 isDiv = true;
2113 } else {
2114 funcOffset = OFFSETOF_MEMBER(Thread, pIdivmod);
2115 isDiv = false;
2116 }
Ian Rogersab2b55d2012-03-18 00:06:11 -07002117 callRuntimeHelperRegImm(cUnit, funcOffset, rARG0, lit);
buzbee31a4a6f2012-02-28 15:36:15 -08002118 if (isDiv)
Ian Rogersf7d9ad32012-03-13 18:45:39 -07002119 rlResult = oatGetReturn(cUnit, false);
buzbee31a4a6f2012-02-28 15:36:15 -08002120 else
2121 rlResult = oatGetReturnAlt(cUnit);
2122 storeValue(cUnit, rlDest, rlResult);
2123 return false;
2124 break;
2125 default:
2126 return true;
2127 }
2128 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
2129 rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
2130 // Avoid shifts by literal 0 - no support in Thumb. Change to copy
2131 if (shiftOp && (lit == 0)) {
buzbee82488f52012-03-02 08:20:26 -08002132 opRegCopy(cUnit, rlResult.lowReg, rlSrc.lowReg);
buzbee31a4a6f2012-02-28 15:36:15 -08002133 } else {
2134 opRegRegImm(cUnit, op, rlResult.lowReg, rlSrc.lowReg, lit);
2135 }
2136 storeValue(cUnit, rlDest, rlResult);
2137 return false;
2138}
2139
2140bool genArithOpLong(CompilationUnit* cUnit, MIR* mir, RegLocation rlDest,
2141 RegLocation rlSrc1, RegLocation rlSrc2)
2142{
2143 RegLocation rlResult;
2144 OpKind firstOp = kOpBkpt;
2145 OpKind secondOp = kOpBkpt;
2146 bool callOut = false;
2147 bool checkZero = false;
2148 int funcOffset;
2149 int retReg = rRET0;
2150
2151 switch (mir->dalvikInsn.opcode) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002152 case Instruction::NOT_LONG:
buzbee31a4a6f2012-02-28 15:36:15 -08002153 rlSrc2 = loadValueWide(cUnit, rlSrc2, kCoreReg);
2154 rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
2155 // Check for destructive overlap
2156 if (rlResult.lowReg == rlSrc2.highReg) {
2157 int tReg = oatAllocTemp(cUnit);
buzbee82488f52012-03-02 08:20:26 -08002158 opRegCopy(cUnit, tReg, rlSrc2.highReg);
buzbee31a4a6f2012-02-28 15:36:15 -08002159 opRegReg(cUnit, kOpMvn, rlResult.lowReg, rlSrc2.lowReg);
2160 opRegReg(cUnit, kOpMvn, rlResult.highReg, tReg);
2161 oatFreeTemp(cUnit, tReg);
2162 } else {
2163 opRegReg(cUnit, kOpMvn, rlResult.lowReg, rlSrc2.lowReg);
2164 opRegReg(cUnit, kOpMvn, rlResult.highReg, rlSrc2.highReg);
2165 }
2166 storeValueWide(cUnit, rlDest, rlResult);
2167 return false;
2168 break;
Elliott Hughesadb8c672012-03-06 16:49:32 -08002169 case Instruction::ADD_LONG:
2170 case Instruction::ADD_LONG_2ADDR:
buzbeec5159d52012-03-03 11:48:39 -08002171#if defined(TARGET_MIPS)
2172 return genAddLong(cUnit, mir, rlDest, rlSrc1, rlSrc2);
Ian Rogersc6f3bb82012-03-21 20:40:33 -07002173#elif defined(TARGET_X86)
2174 callOut = true;
2175 retReg = rRET0;
2176 funcOffset = OFFSETOF_MEMBER(Thread, pLadd);
buzbeec5159d52012-03-03 11:48:39 -08002177#else
buzbee31a4a6f2012-02-28 15:36:15 -08002178 firstOp = kOpAdd;
2179 secondOp = kOpAdc;
2180 break;
buzbeec5159d52012-03-03 11:48:39 -08002181#endif
Elliott Hughesadb8c672012-03-06 16:49:32 -08002182 case Instruction::SUB_LONG:
2183 case Instruction::SUB_LONG_2ADDR:
buzbeec5159d52012-03-03 11:48:39 -08002184#if defined(TARGET_MIPS)
2185 return genSubLong(cUnit, mir, rlDest, rlSrc1, rlSrc2);
Ian Rogersc6f3bb82012-03-21 20:40:33 -07002186#elif defined(TARGET_X86)
2187 callOut = true;
2188 retReg = rRET0;
2189 funcOffset = OFFSETOF_MEMBER(Thread, pLsub);
2190#endif
buzbee31a4a6f2012-02-28 15:36:15 -08002191 firstOp = kOpSub;
2192 secondOp = kOpSbc;
2193 break;
Elliott Hughesadb8c672012-03-06 16:49:32 -08002194 case Instruction::MUL_LONG:
2195 case Instruction::MUL_LONG_2ADDR:
buzbee31a4a6f2012-02-28 15:36:15 -08002196 callOut = true;
2197 retReg = rRET0;
2198 funcOffset = OFFSETOF_MEMBER(Thread, pLmul);
2199 break;
Elliott Hughesadb8c672012-03-06 16:49:32 -08002200 case Instruction::DIV_LONG:
2201 case Instruction::DIV_LONG_2ADDR:
buzbee31a4a6f2012-02-28 15:36:15 -08002202 callOut = true;
2203 checkZero = true;
2204 retReg = rRET0;
2205 funcOffset = OFFSETOF_MEMBER(Thread, pLdivmod);
2206 break;
2207 /* NOTE - result is in rARG2/rARG3 instead of rRET0/rRET1 */
2208 // FIXME: is true, or could be made true, or other targets?
Elliott Hughesadb8c672012-03-06 16:49:32 -08002209 case Instruction::REM_LONG:
2210 case Instruction::REM_LONG_2ADDR:
buzbee31a4a6f2012-02-28 15:36:15 -08002211 callOut = true;
2212 checkZero = true;
2213 funcOffset = OFFSETOF_MEMBER(Thread, pLdivmod);
2214 retReg = rARG2;
2215 break;
Elliott Hughesadb8c672012-03-06 16:49:32 -08002216 case Instruction::AND_LONG_2ADDR:
2217 case Instruction::AND_LONG:
Ian Rogersc6f3bb82012-03-21 20:40:33 -07002218#if defined(TARGET_X86)
2219 callOut = true;
2220 retReg = rRET0;
2221 funcOffset = OFFSETOF_MEMBER(Thread, pLand);
2222#endif
buzbee31a4a6f2012-02-28 15:36:15 -08002223 firstOp = kOpAnd;
2224 secondOp = kOpAnd;
2225 break;
Elliott Hughesadb8c672012-03-06 16:49:32 -08002226 case Instruction::OR_LONG:
2227 case Instruction::OR_LONG_2ADDR:
Ian Rogersc6f3bb82012-03-21 20:40:33 -07002228#if defined(TARGET_X86)
2229 callOut = true;
2230 retReg = rRET0;
2231 funcOffset = OFFSETOF_MEMBER(Thread, pLor);
2232#endif
buzbee31a4a6f2012-02-28 15:36:15 -08002233 firstOp = kOpOr;
2234 secondOp = kOpOr;
2235 break;
Elliott Hughesadb8c672012-03-06 16:49:32 -08002236 case Instruction::XOR_LONG:
2237 case Instruction::XOR_LONG_2ADDR:
Ian Rogersc6f3bb82012-03-21 20:40:33 -07002238#if defined(TARGET_X86)
2239 callOut = true;
2240 retReg = rRET0;
2241 funcOffset = OFFSETOF_MEMBER(Thread, pLxor);
2242#endif
buzbee31a4a6f2012-02-28 15:36:15 -08002243 firstOp = kOpXor;
2244 secondOp = kOpXor;
2245 break;
Elliott Hughesadb8c672012-03-06 16:49:32 -08002246 case Instruction::NEG_LONG: {
buzbeec5159d52012-03-03 11:48:39 -08002247 return genNegLong(cUnit, mir, rlDest, rlSrc2);
buzbee31a4a6f2012-02-28 15:36:15 -08002248 }
2249 default:
2250 LOG(FATAL) << "Invalid long arith op";
2251 }
2252 if (!callOut) {
2253 genLong3Addr(cUnit, mir, firstOp, secondOp, rlDest, rlSrc1, rlSrc2);
2254 } else {
buzbee31a4a6f2012-02-28 15:36:15 -08002255 oatFlushAllRegs(cUnit); /* Send everything to home location */
2256 if (checkZero) {
2257 loadValueDirectWideFixed(cUnit, rlSrc2, rARG2, rARG3);
Ian Rogersc6f3bb82012-03-21 20:40:33 -07002258#if !defined(TARGET_X86)
Ian Rogersab2b55d2012-03-18 00:06:11 -07002259 int rTgt = loadHelper(cUnit, funcOffset);
Ian Rogers6cbb2bd2012-03-16 13:45:30 -07002260#endif
buzbee31a4a6f2012-02-28 15:36:15 -08002261 int tReg = oatAllocTemp(cUnit);
2262#if defined(TARGET_ARM)
2263 newLIR4(cUnit, kThumb2OrrRRRs, tReg, rARG2, rARG3, 0);
2264 oatFreeTemp(cUnit, tReg);
2265 genCheck(cUnit, kCondEq, mir, kThrowDivZero);
2266#else
2267 opRegRegReg(cUnit, kOpOr, tReg, rARG2, rARG3);
Ian Rogersb5d09b22012-03-06 22:14:17 -08002268#endif
buzbee5de34942012-03-01 14:51:57 -08002269 genImmedCheck(cUnit, kCondEq, tReg, 0, mir, kThrowDivZero);
buzbee31a4a6f2012-02-28 15:36:15 -08002270 oatFreeTemp(cUnit, tReg);
Ian Rogersc6f3bb82012-03-21 20:40:33 -07002271 loadValueDirectWideFixed(cUnit, rlSrc1, rARG0, rARG1);
Ian Rogersab2b55d2012-03-18 00:06:11 -07002272#if !defined(TARGET_X86)
2273 opReg(cUnit, kOpBlx, rTgt);
2274 oatFreeTemp(cUnit, rTgt);
Ian Rogersc6f3bb82012-03-21 20:40:33 -07002275#else
2276 opThreadMem(cUnit, kOpBlx, funcOffset);
Ian Rogersb5d09b22012-03-06 22:14:17 -08002277#endif
Ian Rogersab2b55d2012-03-18 00:06:11 -07002278 } else {
2279 callRuntimeHelperRegLocationRegLocation(cUnit, funcOffset,
2280 rlSrc1, rlSrc2);
buzbee31a4a6f2012-02-28 15:36:15 -08002281 }
buzbee31a4a6f2012-02-28 15:36:15 -08002282 // Adjust return regs in to handle case of rem returning rARG2/rARG3
2283 if (retReg == rRET0)
Ian Rogersf7d9ad32012-03-13 18:45:39 -07002284 rlResult = oatGetReturnWide(cUnit, false);
buzbee31a4a6f2012-02-28 15:36:15 -08002285 else
2286 rlResult = oatGetReturnWideAlt(cUnit);
2287 storeValueWide(cUnit, rlDest, rlResult);
2288 }
2289 return false;
2290}
2291
2292bool genConversionCall(CompilationUnit* cUnit, MIR* mir, int funcOffset,
2293 int srcSize, int tgtSize)
2294{
2295 /*
2296 * Don't optimize the register usage since it calls out to support
2297 * functions
2298 */
2299 RegLocation rlSrc;
2300 RegLocation rlDest;
2301 oatFlushAllRegs(cUnit); /* Send everything to home location */
buzbee31a4a6f2012-02-28 15:36:15 -08002302 if (srcSize == 1) {
2303 rlSrc = oatGetSrc(cUnit, mir, 0);
2304 loadValueDirectFixed(cUnit, rlSrc, rARG0);
2305 } else {
2306 rlSrc = oatGetSrcWide(cUnit, mir, 0, 1);
2307 loadValueDirectWideFixed(cUnit, rlSrc, rARG0, rARG1);
2308 }
Ian Rogersab2b55d2012-03-18 00:06:11 -07002309 callRuntimeHelperRegLocation(cUnit, funcOffset, rlSrc);
buzbee31a4a6f2012-02-28 15:36:15 -08002310 if (tgtSize == 1) {
2311 RegLocation rlResult;
2312 rlDest = oatGetDest(cUnit, mir, 0);
Ian Rogersf7d9ad32012-03-13 18:45:39 -07002313 rlResult = oatGetReturn(cUnit, rlDest.fp);
buzbee31a4a6f2012-02-28 15:36:15 -08002314 storeValue(cUnit, rlDest, rlResult);
2315 } else {
2316 RegLocation rlResult;
2317 rlDest = oatGetDestWide(cUnit, mir, 0, 1);
Ian Rogersf7d9ad32012-03-13 18:45:39 -07002318 rlResult = oatGetReturnWide(cUnit, rlDest.fp);
buzbee31a4a6f2012-02-28 15:36:15 -08002319 storeValueWide(cUnit, rlDest, rlResult);
2320 }
2321 return false;
2322}
2323
2324void genNegFloat(CompilationUnit* cUnit, RegLocation rlDest, RegLocation rlSrc);
2325bool genArithOpFloatPortable(CompilationUnit* cUnit, MIR* mir,
2326 RegLocation rlDest, RegLocation rlSrc1,
2327 RegLocation rlSrc2)
2328{
2329 RegLocation rlResult;
2330 int funcOffset;
2331
2332 switch (mir->dalvikInsn.opcode) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002333 case Instruction::ADD_FLOAT_2ADDR:
2334 case Instruction::ADD_FLOAT:
buzbee31a4a6f2012-02-28 15:36:15 -08002335 funcOffset = OFFSETOF_MEMBER(Thread, pFadd);
2336 break;
Elliott Hughesadb8c672012-03-06 16:49:32 -08002337 case Instruction::SUB_FLOAT_2ADDR:
2338 case Instruction::SUB_FLOAT:
buzbee31a4a6f2012-02-28 15:36:15 -08002339 funcOffset = OFFSETOF_MEMBER(Thread, pFsub);
2340 break;
Elliott Hughesadb8c672012-03-06 16:49:32 -08002341 case Instruction::DIV_FLOAT_2ADDR:
2342 case Instruction::DIV_FLOAT:
buzbee31a4a6f2012-02-28 15:36:15 -08002343 funcOffset = OFFSETOF_MEMBER(Thread, pFdiv);
2344 break;
Elliott Hughesadb8c672012-03-06 16:49:32 -08002345 case Instruction::MUL_FLOAT_2ADDR:
2346 case Instruction::MUL_FLOAT:
buzbee31a4a6f2012-02-28 15:36:15 -08002347 funcOffset = OFFSETOF_MEMBER(Thread, pFmul);
2348 break;
Elliott Hughesadb8c672012-03-06 16:49:32 -08002349 case Instruction::REM_FLOAT_2ADDR:
2350 case Instruction::REM_FLOAT:
buzbee31a4a6f2012-02-28 15:36:15 -08002351 funcOffset = OFFSETOF_MEMBER(Thread, pFmodf);
2352 break;
Elliott Hughesadb8c672012-03-06 16:49:32 -08002353 case Instruction::NEG_FLOAT: {
buzbee31a4a6f2012-02-28 15:36:15 -08002354 genNegFloat(cUnit, rlDest, rlSrc1);
2355 return false;
2356 }
2357 default:
2358 return true;
2359 }
2360 oatFlushAllRegs(cUnit); /* Send everything to home location */
Ian Rogersab2b55d2012-03-18 00:06:11 -07002361 callRuntimeHelperRegLocationRegLocation(cUnit, funcOffset, rlSrc1, rlSrc2);
Ian Rogersf7d9ad32012-03-13 18:45:39 -07002362 rlResult = oatGetReturn(cUnit, true);
buzbee31a4a6f2012-02-28 15:36:15 -08002363 storeValue(cUnit, rlDest, rlResult);
2364 return false;
2365}
2366
2367void genNegDouble(CompilationUnit* cUnit, RegLocation rlDst, RegLocation rlSrc);
2368bool genArithOpDoublePortable(CompilationUnit* cUnit, MIR* mir,
2369 RegLocation rlDest, RegLocation rlSrc1,
2370 RegLocation rlSrc2)
2371{
2372 RegLocation rlResult;
2373 int funcOffset;
2374
2375 switch (mir->dalvikInsn.opcode) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002376 case Instruction::ADD_DOUBLE_2ADDR:
2377 case Instruction::ADD_DOUBLE:
buzbee31a4a6f2012-02-28 15:36:15 -08002378 funcOffset = OFFSETOF_MEMBER(Thread, pDadd);
2379 break;
Elliott Hughesadb8c672012-03-06 16:49:32 -08002380 case Instruction::SUB_DOUBLE_2ADDR:
2381 case Instruction::SUB_DOUBLE:
buzbee31a4a6f2012-02-28 15:36:15 -08002382 funcOffset = OFFSETOF_MEMBER(Thread, pDsub);
2383 break;
Elliott Hughesadb8c672012-03-06 16:49:32 -08002384 case Instruction::DIV_DOUBLE_2ADDR:
2385 case Instruction::DIV_DOUBLE:
buzbee31a4a6f2012-02-28 15:36:15 -08002386 funcOffset = OFFSETOF_MEMBER(Thread, pDdiv);
2387 break;
Elliott Hughesadb8c672012-03-06 16:49:32 -08002388 case Instruction::MUL_DOUBLE_2ADDR:
2389 case Instruction::MUL_DOUBLE:
buzbee31a4a6f2012-02-28 15:36:15 -08002390 funcOffset = OFFSETOF_MEMBER(Thread, pDmul);
2391 break;
Elliott Hughesadb8c672012-03-06 16:49:32 -08002392 case Instruction::REM_DOUBLE_2ADDR:
2393 case Instruction::REM_DOUBLE:
buzbee31a4a6f2012-02-28 15:36:15 -08002394 funcOffset = OFFSETOF_MEMBER(Thread, pFmod);
2395 break;
Elliott Hughesadb8c672012-03-06 16:49:32 -08002396 case Instruction::NEG_DOUBLE: {
buzbee31a4a6f2012-02-28 15:36:15 -08002397 genNegDouble(cUnit, rlDest, rlSrc1);
2398 return false;
2399 }
2400 default:
2401 return true;
2402 }
2403 oatFlushAllRegs(cUnit); /* Send everything to home location */
Ian Rogersab2b55d2012-03-18 00:06:11 -07002404 callRuntimeHelperRegLocationRegLocation(cUnit, funcOffset, rlSrc1, rlSrc2);
Ian Rogersf7d9ad32012-03-13 18:45:39 -07002405 rlResult = oatGetReturnWide(cUnit, true);
buzbee31a4a6f2012-02-28 15:36:15 -08002406 storeValueWide(cUnit, rlDest, rlResult);
2407 return false;
2408}
2409
2410bool genConversionPortable(CompilationUnit* cUnit, MIR* mir)
2411{
Elliott Hughesadb8c672012-03-06 16:49:32 -08002412 Instruction::Code opcode = mir->dalvikInsn.opcode;
buzbee31a4a6f2012-02-28 15:36:15 -08002413
2414 switch (opcode) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002415 case Instruction::INT_TO_FLOAT:
buzbee31a4a6f2012-02-28 15:36:15 -08002416 return genConversionCall(cUnit, mir, OFFSETOF_MEMBER(Thread, pI2f),
2417 1, 1);
Elliott Hughesadb8c672012-03-06 16:49:32 -08002418 case Instruction::FLOAT_TO_INT:
buzbee31a4a6f2012-02-28 15:36:15 -08002419 return genConversionCall(cUnit, mir, OFFSETOF_MEMBER(Thread, pF2iz),
2420 1, 1);
Elliott Hughesadb8c672012-03-06 16:49:32 -08002421 case Instruction::DOUBLE_TO_FLOAT:
buzbee31a4a6f2012-02-28 15:36:15 -08002422 return genConversionCall(cUnit, mir, OFFSETOF_MEMBER(Thread, pD2f),
2423 2, 1);
Elliott Hughesadb8c672012-03-06 16:49:32 -08002424 case Instruction::FLOAT_TO_DOUBLE:
buzbee31a4a6f2012-02-28 15:36:15 -08002425 return genConversionCall(cUnit, mir, OFFSETOF_MEMBER(Thread, pF2d),
2426 1, 2);
Elliott Hughesadb8c672012-03-06 16:49:32 -08002427 case Instruction::INT_TO_DOUBLE:
buzbee31a4a6f2012-02-28 15:36:15 -08002428 return genConversionCall(cUnit, mir, OFFSETOF_MEMBER(Thread, pI2d),
2429 1, 2);
Elliott Hughesadb8c672012-03-06 16:49:32 -08002430 case Instruction::DOUBLE_TO_INT:
buzbee31a4a6f2012-02-28 15:36:15 -08002431 return genConversionCall(cUnit, mir, OFFSETOF_MEMBER(Thread, pD2iz),
2432 2, 1);
Elliott Hughesadb8c672012-03-06 16:49:32 -08002433 case Instruction::FLOAT_TO_LONG:
buzbee31a4a6f2012-02-28 15:36:15 -08002434 return genConversionCall(cUnit, mir, OFFSETOF_MEMBER(Thread,
2435 pF2l), 1, 2);
Elliott Hughesadb8c672012-03-06 16:49:32 -08002436 case Instruction::LONG_TO_FLOAT:
buzbee31a4a6f2012-02-28 15:36:15 -08002437 return genConversionCall(cUnit, mir, OFFSETOF_MEMBER(Thread, pL2f),
2438 2, 1);
Elliott Hughesadb8c672012-03-06 16:49:32 -08002439 case Instruction::DOUBLE_TO_LONG:
buzbee31a4a6f2012-02-28 15:36:15 -08002440 return genConversionCall(cUnit, mir, OFFSETOF_MEMBER(Thread,
2441 pD2l), 2, 2);
Elliott Hughesadb8c672012-03-06 16:49:32 -08002442 case Instruction::LONG_TO_DOUBLE:
buzbee31a4a6f2012-02-28 15:36:15 -08002443 return genConversionCall(cUnit, mir, OFFSETOF_MEMBER(Thread, pL2d),
2444 2, 2);
2445 default:
2446 return true;
2447 }
2448 return false;
2449}
2450
2451/*
2452 * Generate callout to updateDebugger. Note that we're overloading
2453 * the use of rSUSPEND here. When the debugger is active, this
2454 * register holds the address of the update function. So, if it's
2455 * non-null, we call out to it.
2456 *
2457 * Note also that rRET0 and rRET1 must be preserved across this
2458 * code. This must be handled by the stub.
2459 */
2460void genDebuggerUpdate(CompilationUnit* cUnit, int32_t offset)
2461{
2462 // Following DCHECK verifies that dPC is in range of single load immediate
2463 DCHECK((offset == DEBUGGER_METHOD_ENTRY) ||
2464 (offset == DEBUGGER_METHOD_EXIT) || ((offset & 0xffff) == offset));
2465 oatClobberCalleeSave(cUnit);
2466#if defined(TARGET_ARM)
2467 opRegImm(cUnit, kOpCmp, rSUSPEND, 0);
buzbee82488f52012-03-02 08:20:26 -08002468 opIT(cUnit, kArmCondNe, "T");
buzbee31a4a6f2012-02-28 15:36:15 -08002469 loadConstant(cUnit, rARG2, offset); // arg2 <- Entry code
2470 opReg(cUnit, kOpBlx, rSUSPEND);
Ian Rogersb5d09b22012-03-06 22:14:17 -08002471#elif defined(TARGET_X86)
2472 UNIMPLEMENTED(FATAL);
buzbee31a4a6f2012-02-28 15:36:15 -08002473#else
buzbee82488f52012-03-02 08:20:26 -08002474 LIR* branch = opCmpImmBranch(cUnit, kCondEq, rSUSPEND, 0, NULL);
buzbee31a4a6f2012-02-28 15:36:15 -08002475 loadConstant(cUnit, rARG2, offset);
2476 opReg(cUnit, kOpBlx, rSUSPEND);
2477 LIR* target = newLIR0(cUnit, kPseudoTargetLabel);
buzbee31a4a6f2012-02-28 15:36:15 -08002478 branch->target = (LIR*)target;
2479#endif
2480 oatFreeTemp(cUnit, rARG2);
2481}
2482
2483/* Check if we need to check for pending suspend request */
2484void genSuspendTest(CompilationUnit* cUnit, MIR* mir)
2485{
2486 if (NO_SUSPEND || (mir->optimizationFlags & MIR_IGNORE_SUSPEND_CHECK)) {
2487 return;
2488 }
2489 oatFlushAllRegs(cUnit);
buzbee31a4a6f2012-02-28 15:36:15 -08002490 if (cUnit->genDebugger) {
2491 // If generating code for the debugger, always check for suspension
Ian Rogers6cbb2bd2012-03-16 13:45:30 -07002492#if defined(TARGET_X86)
2493 UNIMPLEMENTED(FATAL);
2494#else
buzbee86a4bce2012-03-06 18:15:00 -08002495 int rTgt = loadHelper(cUnit, OFFSETOF_MEMBER(Thread,
2496 pTestSuspendFromCode));
2497 opReg(cUnit, kOpBlx, rTgt);
2498 // Refresh rSUSPEND
2499 loadWordDisp(cUnit, rSELF,
2500 OFFSETOF_MEMBER(Thread, pUpdateDebuggerFromCode),
2501 rSUSPEND);
Ian Rogersb5d09b22012-03-06 22:14:17 -08002502#endif
buzbee31a4a6f2012-02-28 15:36:15 -08002503 } else {
Ian Rogersb5d09b22012-03-06 22:14:17 -08002504 LIR* branch = NULL;
buzbee31a4a6f2012-02-28 15:36:15 -08002505#if defined(TARGET_ARM)
2506 // In non-debug case, only check periodically
2507 newLIR2(cUnit, kThumbSubRI8, rSUSPEND, 1);
buzbee82488f52012-03-02 08:20:26 -08002508 branch = opCondBranch(cUnit, kCondEq, NULL);
Ian Rogersb5d09b22012-03-06 22:14:17 -08002509#elif defined(TARGET_X86)
2510 newLIR2(cUnit, kX86Cmp32TI, Thread::SuspendCountOffset().Int32Value(), 0);
2511 branch = opCondBranch(cUnit, kCondNe, NULL);
buzbee31a4a6f2012-02-28 15:36:15 -08002512#else
2513 opRegImm(cUnit, kOpSub, rSUSPEND, 1);
buzbee82488f52012-03-02 08:20:26 -08002514 branch = opCmpImmBranch(cUnit, kCondEq, rSUSPEND, 0, NULL);
buzbee31a4a6f2012-02-28 15:36:15 -08002515#endif
buzbee86a4bce2012-03-06 18:15:00 -08002516 LIR* retLab = newLIR0(cUnit, kPseudoTargetLabel);
2517 LIR* target = rawLIR(cUnit, cUnit->currentDalvikOffset,
2518 kPseudoSuspendTarget, (intptr_t)retLab, mir->offset);
2519 branch->target = (LIR*)target;
2520 oatInsertGrowableList(cUnit, &cUnit->suspendLaunchpads, (intptr_t)target);
buzbee31a4a6f2012-02-28 15:36:15 -08002521 }
buzbee31a4a6f2012-02-28 15:36:15 -08002522}
2523
2524} // namespace art