blob: 66950d2dc0dcfe3012331a34b6186102775ba7b6 [file] [log] [blame]
buzbee31a4a6f2012-02-28 15:36:15 -08001/*
2 * Copyright (C) 2012 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17namespace art {
18
19/*
20 * This source files contains "gen" codegen routines that should
21 * be applicable to most targets. Only mid-level support utilities
22 * and "op" calls may be used here.
23 */
24
25#if defined(TARGET_ARM)
buzbee82488f52012-03-02 08:20:26 -080026LIR* opIT(CompilationUnit* cUnit, ArmConditionCode cond, const char* guide);
buzbee31a4a6f2012-02-28 15:36:15 -080027#endif
28
29LIR* callRuntimeHelper(CompilationUnit* cUnit, int reg)
30{
31 oatClobberCalleeSave(cUnit);
32 return opReg(cUnit, kOpBlx, reg);
33}
34
35/*
36 * Generate an kPseudoBarrier marker to indicate the boundary of special
37 * blocks.
38 */
39void genBarrier(CompilationUnit* cUnit)
40{
41 LIR* barrier = newLIR0(cUnit, kPseudoBarrier);
42 /* Mark all resources as being clobbered */
43 barrier->defMask = -1;
44}
45
buzbee31a4a6f2012-02-28 15:36:15 -080046
47/* Generate unconditional branch instructions */
buzbee82488f52012-03-02 08:20:26 -080048LIR* opUnconditionalBranch(CompilationUnit* cUnit, LIR* target)
buzbee31a4a6f2012-02-28 15:36:15 -080049{
50 LIR* branch = opNone(cUnit, kOpUncondBr);
51 branch->target = (LIR*) target;
52 return branch;
53}
54
buzbee5de34942012-03-01 14:51:57 -080055// FIXME: need to do some work to split out targets with
56// condition codes and those without
57#if defined(TARGET_ARM) || defined(TARGET_X86)
buzbee31a4a6f2012-02-28 15:36:15 -080058LIR* genCheck(CompilationUnit* cUnit, ConditionCode cCode, MIR* mir,
59 ThrowKind kind)
60{
61 LIR* tgt = (LIR*)oatNew(cUnit, sizeof(LIR), true, kAllocLIR);
62 tgt->opcode = kPseudoThrowTarget;
63 tgt->operands[0] = kind;
64 tgt->operands[1] = mir ? mir->offset : 0;
buzbee82488f52012-03-02 08:20:26 -080065 LIR* branch = opCondBranch(cUnit, cCode, tgt);
buzbee31a4a6f2012-02-28 15:36:15 -080066 // Remember branch target - will process later
67 oatInsertGrowableList(cUnit, &cUnit->throwLaunchpads, (intptr_t)tgt);
68 return branch;
69}
buzbee5de34942012-03-01 14:51:57 -080070#endif
buzbee31a4a6f2012-02-28 15:36:15 -080071
72LIR* genImmedCheck(CompilationUnit* cUnit, ConditionCode cCode,
73 int reg, int immVal, MIR* mir, ThrowKind kind)
74{
75 LIR* tgt = (LIR*)oatNew(cUnit, sizeof(LIR), true, kAllocLIR);
76 tgt->opcode = kPseudoThrowTarget;
77 tgt->operands[0] = kind;
78 tgt->operands[1] = mir->offset;
79 LIR* branch;
80 if (cCode == kCondAl) {
buzbee82488f52012-03-02 08:20:26 -080081 branch = opUnconditionalBranch(cUnit, tgt);
buzbee31a4a6f2012-02-28 15:36:15 -080082 } else {
buzbee82488f52012-03-02 08:20:26 -080083 branch = opCmpImmBranch(cUnit, cCode, reg, immVal, tgt);
buzbee31a4a6f2012-02-28 15:36:15 -080084 }
85 // Remember branch target - will process later
86 oatInsertGrowableList(cUnit, &cUnit->throwLaunchpads, (intptr_t)tgt);
87 return branch;
88}
89
90/* Perform null-check on a register. */
91LIR* genNullCheck(CompilationUnit* cUnit, int sReg, int mReg, MIR* mir)
92{
93 if (!(cUnit->disableOpt & (1 << kNullCheckElimination)) &&
94 mir->optimizationFlags & MIR_IGNORE_NULL_CHECK) {
95 return NULL;
96 }
97 return genImmedCheck(cUnit, kCondEq, mReg, 0, mir, kThrowNullPointer);
98}
99
100/* Perform check on two registers */
101LIR* genRegRegCheck(CompilationUnit* cUnit, ConditionCode cCode,
102 int reg1, int reg2, MIR* mir, ThrowKind kind)
103{
104 LIR* tgt = (LIR*)oatNew(cUnit, sizeof(LIR), true, kAllocLIR);
105 tgt->opcode = kPseudoThrowTarget;
106 tgt->operands[0] = kind;
107 tgt->operands[1] = mir ? mir->offset : 0;
108 tgt->operands[2] = reg1;
109 tgt->operands[3] = reg2;
buzbee5de34942012-03-01 14:51:57 -0800110#if defined(TARGET_MIPS)
buzbee82488f52012-03-02 08:20:26 -0800111 LIR* branch = opCmpBranch(cUnit, cCode, reg1, reg2, tgt);
buzbee5de34942012-03-01 14:51:57 -0800112#else
buzbee31a4a6f2012-02-28 15:36:15 -0800113 opRegReg(cUnit, kOpCmp, reg1, reg2);
buzbee82488f52012-03-02 08:20:26 -0800114 LIR* branch = opCondBranch(cUnit, cCode, tgt);
buzbee5de34942012-03-01 14:51:57 -0800115#endif
buzbee31a4a6f2012-02-28 15:36:15 -0800116 // Remember branch target - will process later
117 oatInsertGrowableList(cUnit, &cUnit->throwLaunchpads, (intptr_t)tgt);
118 return branch;
119}
120
121void genCompareAndBranch(CompilationUnit* cUnit, BasicBlock* bb, MIR* mir,
122 RegLocation rlSrc1, RegLocation rlSrc2, LIR* labelList)
123{
124 ConditionCode cond;
125 rlSrc1 = loadValue(cUnit, rlSrc1, kCoreReg);
126 rlSrc2 = loadValue(cUnit, rlSrc2, kCoreReg);
buzbee31a4a6f2012-02-28 15:36:15 -0800127 Opcode opcode = mir->dalvikInsn.opcode;
128 switch(opcode) {
129 case OP_IF_EQ:
130 cond = kCondEq;
131 break;
132 case OP_IF_NE:
133 cond = kCondNe;
134 break;
135 case OP_IF_LT:
136 cond = kCondLt;
137 break;
138 case OP_IF_GE:
139 cond = kCondGe;
140 break;
141 case OP_IF_GT:
142 cond = kCondGt;
143 break;
144 case OP_IF_LE:
145 cond = kCondLe;
146 break;
147 default:
148 cond = (ConditionCode)0;
149 LOG(FATAL) << "Unexpected opcode " << (int)opcode;
150 }
buzbee5de34942012-03-01 14:51:57 -0800151#if defined(TARGET_MIPS)
buzbee82488f52012-03-02 08:20:26 -0800152 opCmpBranch(cUnit, cond, rlSrc1.lowReg, rlSrc2.lowReg,
153 &labelList[bb->taken->id]);
buzbee5de34942012-03-01 14:51:57 -0800154#else
155 opRegReg(cUnit, kOpCmp, rlSrc1.lowReg, rlSrc2.lowReg);
buzbee82488f52012-03-02 08:20:26 -0800156 opCondBranch(cUnit, cond, &labelList[bb->taken->id]);
buzbee5de34942012-03-01 14:51:57 -0800157#endif
buzbee82488f52012-03-02 08:20:26 -0800158 opUnconditionalBranch(cUnit, &labelList[bb->fallThrough->id]);
buzbee31a4a6f2012-02-28 15:36:15 -0800159}
160
161void genCompareZeroAndBranch(CompilationUnit* cUnit, BasicBlock* bb, MIR* mir,
162 RegLocation rlSrc, LIR* labelList)
163{
164 ConditionCode cond;
165 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
buzbee31a4a6f2012-02-28 15:36:15 -0800166 Opcode opcode = mir->dalvikInsn.opcode;
167 switch(opcode) {
168 case OP_IF_EQZ:
169 cond = kCondEq;
170 break;
171 case OP_IF_NEZ:
172 cond = kCondNe;
173 break;
174 case OP_IF_LTZ:
175 cond = kCondLt;
176 break;
177 case OP_IF_GEZ:
178 cond = kCondGe;
179 break;
180 case OP_IF_GTZ:
181 cond = kCondGt;
182 break;
183 case OP_IF_LEZ:
184 cond = kCondLe;
185 break;
186 default:
187 cond = (ConditionCode)0;
188 LOG(FATAL) << "Unexpected opcode " << (int)opcode;
189 }
buzbee5de34942012-03-01 14:51:57 -0800190#if defined(TARGET_MIPS)
buzbee82488f52012-03-02 08:20:26 -0800191 opCmpImmBranch(cUnit, cond, rlSrc.lowReg, 0, &labelList[bb->taken->id]);
buzbee5de34942012-03-01 14:51:57 -0800192#else
193 opRegImm(cUnit, kOpCmp, rlSrc.lowReg, 0);
buzbee82488f52012-03-02 08:20:26 -0800194 opCondBranch(cUnit, cond, &labelList[bb->taken->id]);
buzbee5de34942012-03-01 14:51:57 -0800195#endif
buzbee82488f52012-03-02 08:20:26 -0800196 opUnconditionalBranch(cUnit, &labelList[bb->fallThrough->id]);
buzbee31a4a6f2012-02-28 15:36:15 -0800197}
198
199void genIntToLong(CompilationUnit* cUnit, MIR* mir, RegLocation rlDest,
200 RegLocation rlSrc)
201{
202 RegLocation rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
203 if (rlSrc.location == kLocPhysReg) {
buzbee82488f52012-03-02 08:20:26 -0800204 opRegCopy(cUnit, rlResult.lowReg, rlSrc.lowReg);
buzbee31a4a6f2012-02-28 15:36:15 -0800205 } else {
206 loadValueDirect(cUnit, rlSrc, rlResult.lowReg);
207 }
208 opRegRegImm(cUnit, kOpAsr, rlResult.highReg,
209 rlResult.lowReg, 31);
210 storeValueWide(cUnit, rlDest, rlResult);
211}
212
213void genIntNarrowing(CompilationUnit* cUnit, MIR* mir, RegLocation rlDest,
214 RegLocation rlSrc)
215{
216 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
217 RegLocation rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
218 OpKind op = kOpInvalid;
219 switch(mir->dalvikInsn.opcode) {
220 case OP_INT_TO_BYTE:
221 op = kOp2Byte;
222 break;
223 case OP_INT_TO_SHORT:
224 op = kOp2Short;
225 break;
226 case OP_INT_TO_CHAR:
227 op = kOp2Char;
228 break;
229 default:
230 LOG(ERROR) << "Bad int conversion type";
231 }
232 opRegReg(cUnit, op, rlResult.lowReg, rlSrc.lowReg);
233 storeValue(cUnit, rlDest, rlResult);
234}
235
236/*
237 * Let helper function take care of everything. Will call
238 * Array::AllocFromCode(type_idx, method, count);
239 * Note: AllocFromCode will handle checks for errNegativeArraySize.
240 */
241void genNewArray(CompilationUnit* cUnit, MIR* mir, RegLocation rlDest,
242 RegLocation rlSrc)
243{
244 oatFlushAllRegs(cUnit); /* Everything to home location */
245 uint32_t type_idx = mir->dalvikInsn.vC;
246 int rTgt;
247 if (cUnit->compiler->CanAccessTypeWithoutChecks(cUnit->method_idx,
248 cUnit->dex_cache,
249 *cUnit->dex_file,
250 type_idx)) {
251 rTgt = loadHelper(cUnit, OFFSETOF_MEMBER(Thread, pAllocArrayFromCode));
252 } else {
253 rTgt = loadHelper(cUnit, OFFSETOF_MEMBER(Thread,
254 pAllocArrayFromCodeWithAccessCheck));
255 }
256 loadCurrMethodDirect(cUnit, rARG1); // arg1 <- Method*
257 loadConstant(cUnit, rARG0, type_idx); // arg0 <- type_id
258 loadValueDirectFixed(cUnit, rlSrc, rARG2); // arg2 <- count
259 callRuntimeHelper(cUnit, rTgt);
260 RegLocation rlResult = oatGetReturn(cUnit);
261 storeValue(cUnit, rlDest, rlResult);
262}
263
264/*
265 * Similar to genNewArray, but with post-allocation initialization.
266 * Verifier guarantees we're dealing with an array class. Current
267 * code throws runtime exception "bad Filled array req" for 'D' and 'J'.
268 * Current code also throws internal unimp if not 'L', '[' or 'I'.
269 */
270void genFilledNewArray(CompilationUnit* cUnit, MIR* mir, bool isRange)
271{
272 DecodedInstruction* dInsn = &mir->dalvikInsn;
273 int elems = dInsn->vA;
274 int typeId = dInsn->vB;
275 oatFlushAllRegs(cUnit); /* Everything to home location */
276 int rTgt;
277 if (cUnit->compiler->CanAccessTypeWithoutChecks(cUnit->method_idx,
278 cUnit->dex_cache,
279 *cUnit->dex_file,
280 typeId)) {
281 rTgt = loadHelper(cUnit, OFFSETOF_MEMBER(Thread,
282 pCheckAndAllocArrayFromCode));
283 } else {
284 rTgt = loadHelper(cUnit, OFFSETOF_MEMBER(Thread,
285 pCheckAndAllocArrayFromCodeWithAccessCheck));
286 }
287 loadCurrMethodDirect(cUnit, rARG1); // arg1 <- Method*
288 loadConstant(cUnit, rARG0, typeId); // arg0 <- type_id
289 loadConstant(cUnit, rARG2, elems); // arg2 <- count
290 callRuntimeHelper(cUnit, rTgt);
291 /*
292 * NOTE: the implicit target for OP_FILLED_NEW_ARRAY is the
293 * return region. Because AllocFromCode placed the new array
294 * in rRET0, we'll just lock it into place. When debugger support is
295 * added, it may be necessary to additionally copy all return
296 * values to a home location in thread-local storage
297 */
298 oatLockTemp(cUnit, rRET0);
299
300 // TODO: use the correct component size, currently all supported types
301 // share array alignment with ints (see comment at head of function)
302 size_t component_size = sizeof(int32_t);
303
304 // Having a range of 0 is legal
305 if (isRange && (dInsn->vA > 0)) {
306 /*
307 * Bit of ugliness here. We're going generate a mem copy loop
308 * on the register range, but it is possible that some regs
309 * in the range have been promoted. This is unlikely, but
310 * before generating the copy, we'll just force a flush
311 * of any regs in the source range that have been promoted to
312 * home location.
313 */
314 for (unsigned int i = 0; i < dInsn->vA; i++) {
315 RegLocation loc = oatUpdateLoc(cUnit,
316 oatGetSrc(cUnit, mir, i));
317 if (loc.location == kLocPhysReg) {
318 storeBaseDisp(cUnit, rSP, oatSRegOffset(cUnit, loc.sRegLow),
319 loc.lowReg, kWord);
320 }
321 }
322 /*
323 * TUNING note: generated code here could be much improved, but
324 * this is an uncommon operation and isn't especially performance
325 * critical.
326 */
327 int rSrc = oatAllocTemp(cUnit);
328 int rDst = oatAllocTemp(cUnit);
329 int rIdx = oatAllocTemp(cUnit);
buzbee5de34942012-03-01 14:51:57 -0800330#if defined(TARGET_ARM)
buzbee31a4a6f2012-02-28 15:36:15 -0800331 int rVal = rLR; // Using a lot of temps, rLR is known free here
buzbee5de34942012-03-01 14:51:57 -0800332#else
333 int rVal = oatAllocTemp(cUnit);
334#endif
buzbee31a4a6f2012-02-28 15:36:15 -0800335 // Set up source pointer
336 RegLocation rlFirst = oatGetSrc(cUnit, mir, 0);
337 opRegRegImm(cUnit, kOpAdd, rSrc, rSP,
338 oatSRegOffset(cUnit, rlFirst.sRegLow));
339 // Set up the target pointer
340 opRegRegImm(cUnit, kOpAdd, rDst, rRET0,
341 Array::DataOffset(component_size).Int32Value());
342 // Set up the loop counter (known to be > 0)
343 loadConstant(cUnit, rIdx, dInsn->vA - 1);
344 // Generate the copy loop. Going backwards for convenience
345 LIR* target = newLIR0(cUnit, kPseudoTargetLabel);
346 target->defMask = ENCODE_ALL;
347 // Copy next element
348 loadBaseIndexed(cUnit, rSrc, rIdx, rVal, 2, kWord);
349 storeBaseIndexed(cUnit, rDst, rIdx, rVal, 2, kWord);
350#if defined(TARGET_ARM)
351 // Combine sub & test using sub setflags encoding here
352 newLIR3(cUnit, kThumb2SubsRRI12, rIdx, rIdx, 1);
buzbee82488f52012-03-02 08:20:26 -0800353 opCondBranch(cUnit, kCondGe, target);
buzbee31a4a6f2012-02-28 15:36:15 -0800354#else
buzbee5de34942012-03-01 14:51:57 -0800355 oatFreeTemp(cUnit, rVal);
buzbee31a4a6f2012-02-28 15:36:15 -0800356 opRegImm(cUnit, kOpSub, rIdx, 1);
buzbee82488f52012-03-02 08:20:26 -0800357 opCmpImmBranch(cUnit, kCondGe, rIdx, 0, target);
buzbee31a4a6f2012-02-28 15:36:15 -0800358#endif
buzbee31a4a6f2012-02-28 15:36:15 -0800359 } else if (!isRange) {
360 // TUNING: interleave
361 for (unsigned int i = 0; i < dInsn->vA; i++) {
362 RegLocation rlArg = loadValue(cUnit,
363 oatGetSrc(cUnit, mir, i), kCoreReg);
364 storeBaseDisp(cUnit, rRET0,
365 Array::DataOffset(component_size).Int32Value() +
366 i * 4, rlArg.lowReg, kWord);
367 // If the loadValue caused a temp to be allocated, free it
368 if (oatIsTemp(cUnit, rlArg.lowReg)) {
369 oatFreeTemp(cUnit, rlArg.lowReg);
370 }
371 }
372 }
373}
374
375void genSput(CompilationUnit* cUnit, MIR* mir, RegLocation rlSrc,
376 bool isLongOrDouble, bool isObject)
377{
378 int fieldOffset;
379 int ssbIndex;
380 bool isVolatile;
381 bool isReferrersClass;
382 uint32_t fieldIdx = mir->dalvikInsn.vB;
383
384 OatCompilationUnit mUnit(cUnit->class_loader, cUnit->class_linker,
385 *cUnit->dex_file, *cUnit->dex_cache,
386 cUnit->code_item, cUnit->method_idx,
387 cUnit->access_flags);
388
389 bool fastPath =
390 cUnit->compiler->ComputeStaticFieldInfo(fieldIdx, &mUnit,
391 fieldOffset, ssbIndex,
392 isReferrersClass, isVolatile, true);
393 if (fastPath && !SLOW_FIELD_PATH) {
394 DCHECK_GE(fieldOffset, 0);
395 int rBase;
396 int rMethod;
397 if (isReferrersClass) {
398 // Fast path, static storage base is this method's class
399 rMethod = loadCurrMethod(cUnit);
400 rBase = oatAllocTemp(cUnit);
401 loadWordDisp(cUnit, rMethod,
402 Method::DeclaringClassOffset().Int32Value(), rBase);
403 } else {
404 // Medium path, static storage base in a different class which
405 // requires checks that the other class is initialized.
406 DCHECK_GE(ssbIndex, 0);
407 // May do runtime call so everything to home locations.
408 oatFlushAllRegs(cUnit);
409 // Using fixed register to sync with possible call to runtime
410 // support.
411 rMethod = rARG1;
412 oatLockTemp(cUnit, rMethod);
413 loadCurrMethodDirect(cUnit, rMethod);
414 rBase = rARG0;
415 oatLockTemp(cUnit, rBase);
416 loadWordDisp(cUnit, rMethod,
417 Method::DexCacheInitializedStaticStorageOffset().Int32Value(),
418 rBase);
419 loadWordDisp(cUnit, rBase,
420 Array::DataOffset(sizeof(Object*)).Int32Value() + sizeof(int32_t*) *
421 ssbIndex, rBase);
422 // rBase now points at appropriate static storage base (Class*)
423 // or NULL if not initialized. Check for NULL and call helper if NULL.
424 // TUNING: fast path should fall through
buzbee82488f52012-03-02 08:20:26 -0800425 LIR* branchOver = opCmpImmBranch(cUnit, kCondNe, rBase, 0, NULL);
buzbee31a4a6f2012-02-28 15:36:15 -0800426 int rTgt = loadHelper(cUnit, OFFSETOF_MEMBER(Thread,
427 pInitializeStaticStorage));
428 loadConstant(cUnit, rARG0, ssbIndex);
429 callRuntimeHelper(cUnit, rTgt);
430#if defined(TARGET_MIPS)
431 // For Arm, rRET0 = rARG0 = rBASE, for Mips, we need to copy
buzbee82488f52012-03-02 08:20:26 -0800432 opRegCopy(cUnit, rBase, rRET0);
buzbee31a4a6f2012-02-28 15:36:15 -0800433#endif
434 LIR* skipTarget = newLIR0(cUnit, kPseudoTargetLabel);
435 skipTarget->defMask = ENCODE_ALL;
436 branchOver->target = (LIR*)skipTarget;
437 }
438 // rBase now holds static storage base
439 oatFreeTemp(cUnit, rMethod);
440 if (isLongOrDouble) {
441 rlSrc = oatGetSrcWide(cUnit, mir, 0, 1);
442 rlSrc = loadValueWide(cUnit, rlSrc, kAnyReg);
443 } else {
444 rlSrc = oatGetSrc(cUnit, mir, 0);
445 rlSrc = loadValue(cUnit, rlSrc, kAnyReg);
446 }
447//FIXME: need to generalize the barrier call
448 if (isVolatile) {
449 oatGenMemBarrier(cUnit, kST);
450 }
451 if (isLongOrDouble) {
452 storeBaseDispWide(cUnit, rBase, fieldOffset, rlSrc.lowReg,
453 rlSrc.highReg);
454 } else {
455 storeWordDisp(cUnit, rBase, fieldOffset, rlSrc.lowReg);
456 }
457 if (isVolatile) {
458 oatGenMemBarrier(cUnit, kSY);
459 }
460 if (isObject) {
461 markGCCard(cUnit, rlSrc.lowReg, rBase);
462 }
463 oatFreeTemp(cUnit, rBase);
464 } else {
465 oatFlushAllRegs(cUnit); // Everything to home locations
466 int setterOffset = isLongOrDouble ? OFFSETOF_MEMBER(Thread, pSet64Static) :
467 (isObject ? OFFSETOF_MEMBER(Thread, pSetObjStatic)
468 : OFFSETOF_MEMBER(Thread, pSet32Static));
469 int rTgt = loadHelper(cUnit, setterOffset);
470 loadConstant(cUnit, rARG0, fieldIdx);
471 if (isLongOrDouble) {
472 loadValueDirectWideFixed(cUnit, rlSrc, rARG2, rARG3);
473 } else {
474 loadValueDirect(cUnit, rlSrc, rARG1);
475 }
476 callRuntimeHelper(cUnit, rTgt);
477 }
478}
479
480void genSget(CompilationUnit* cUnit, MIR* mir, RegLocation rlDest,
481 bool isLongOrDouble, bool isObject)
482{
483 int fieldOffset;
484 int ssbIndex;
485 bool isVolatile;
486 bool isReferrersClass;
487 uint32_t fieldIdx = mir->dalvikInsn.vB;
488
489 OatCompilationUnit mUnit(cUnit->class_loader, cUnit->class_linker,
490 *cUnit->dex_file, *cUnit->dex_cache,
491 cUnit->code_item, cUnit->method_idx,
492 cUnit->access_flags);
493
494 bool fastPath =
495 cUnit->compiler->ComputeStaticFieldInfo(fieldIdx, &mUnit,
496 fieldOffset, ssbIndex,
497 isReferrersClass, isVolatile,
498 false);
499 if (fastPath && !SLOW_FIELD_PATH) {
500 DCHECK_GE(fieldOffset, 0);
501 int rBase;
502 int rMethod;
503 if (isReferrersClass) {
504 // Fast path, static storage base is this method's class
505 rMethod = loadCurrMethod(cUnit);
506 rBase = oatAllocTemp(cUnit);
507 loadWordDisp(cUnit, rMethod,
508 Method::DeclaringClassOffset().Int32Value(), rBase);
509 } else {
510 // Medium path, static storage base in a different class which
511 // requires checks that the other class is initialized
512 DCHECK_GE(ssbIndex, 0);
513 // May do runtime call so everything to home locations.
514 oatFlushAllRegs(cUnit);
515 // Using fixed register to sync with possible call to runtime
516 // support
517 rMethod = rARG1;
518 oatLockTemp(cUnit, rMethod);
519 loadCurrMethodDirect(cUnit, rMethod);
520 rBase = rARG0;
521 oatLockTemp(cUnit, rBase);
522 loadWordDisp(cUnit, rMethod,
523 Method::DexCacheInitializedStaticStorageOffset().Int32Value(),
524 rBase);
525 loadWordDisp(cUnit, rBase,
526 Array::DataOffset(sizeof(Object*)).Int32Value() +
527 sizeof(int32_t*) * ssbIndex,
528 rBase);
529 // rBase now points at appropriate static storage base (Class*)
530 // or NULL if not initialized. Check for NULL and call helper if NULL.
531 // TUNING: fast path should fall through
buzbee82488f52012-03-02 08:20:26 -0800532 LIR* branchOver = opCmpImmBranch(cUnit, kCondNe, rBase, 0, NULL);
buzbee31a4a6f2012-02-28 15:36:15 -0800533 int rTgt = loadHelper(cUnit, OFFSETOF_MEMBER(Thread,
534 pInitializeStaticStorage));
535 loadConstant(cUnit, rARG0, ssbIndex);
536 callRuntimeHelper(cUnit, rTgt);
537#if defined(TARGET_MIPS)
538 // For Arm, rRET0 = rARG0 = rBASE, for Mips, we need to copy
buzbee82488f52012-03-02 08:20:26 -0800539 opRegCopy(cUnit, rBase, rRET0);
buzbee31a4a6f2012-02-28 15:36:15 -0800540#endif
541 LIR* skipTarget = newLIR0(cUnit, kPseudoTargetLabel);
542 skipTarget->defMask = ENCODE_ALL;
543 branchOver->target = (LIR*)skipTarget;
544 }
545 // rBase now holds static storage base
546 oatFreeTemp(cUnit, rMethod);
547 rlDest = isLongOrDouble ? oatGetDestWide(cUnit, mir, 0, 1)
548 : oatGetDest(cUnit, mir, 0);
549 RegLocation rlResult = oatEvalLoc(cUnit, rlDest, kAnyReg, true);
550 if (isVolatile) {
551 oatGenMemBarrier(cUnit, kSY);
552 }
553 if (isLongOrDouble) {
554 loadBaseDispWide(cUnit, NULL, rBase, fieldOffset, rlResult.lowReg,
555 rlResult.highReg, INVALID_SREG);
556 } else {
557 loadWordDisp(cUnit, rBase, fieldOffset, rlResult.lowReg);
558 }
559 oatFreeTemp(cUnit, rBase);
560 if (isLongOrDouble) {
561 storeValueWide(cUnit, rlDest, rlResult);
562 } else {
563 storeValue(cUnit, rlDest, rlResult);
564 }
565 } else {
566 oatFlushAllRegs(cUnit); // Everything to home locations
567 int getterOffset = isLongOrDouble ? OFFSETOF_MEMBER(Thread, pGet64Static) :
568 (isObject ? OFFSETOF_MEMBER(Thread, pGetObjStatic)
569 : OFFSETOF_MEMBER(Thread, pGet32Static));
570 int rTgt = loadHelper(cUnit, getterOffset);
571 loadConstant(cUnit, rARG0, fieldIdx);
572 callRuntimeHelper(cUnit, rTgt);
573 if (isLongOrDouble) {
574 RegLocation rlResult = oatGetReturnWide(cUnit);
575 storeValueWide(cUnit, rlDest, rlResult);
576 } else {
577 RegLocation rlResult = oatGetReturn(cUnit);
578 storeValue(cUnit, rlDest, rlResult);
579 }
580 }
581}
582
583
584// Debugging routine - if null target, branch to DebugMe
585void genShowTarget(CompilationUnit* cUnit)
586{
buzbee82488f52012-03-02 08:20:26 -0800587 LIR* branchOver = opCmpImmBranch(cUnit, kCondNe, rLINK, 0, NULL);
buzbee31a4a6f2012-02-28 15:36:15 -0800588 loadWordDisp(cUnit, rSELF,
589 OFFSETOF_MEMBER(Thread, pDebugMe), rLINK);
590 LIR* target = newLIR0(cUnit, kPseudoTargetLabel);
591 target->defMask = -1;
592 branchOver->target = (LIR*)target;
593}
594
595void genThrowVerificationError(CompilationUnit* cUnit, MIR* mir)
596{
597 int rTgt = loadHelper(cUnit, OFFSETOF_MEMBER(Thread,
598 pThrowVerificationErrorFromCode));
599 loadConstant(cUnit, rARG0, mir->dalvikInsn.vA);
600 loadConstant(cUnit, rARG1, mir->dalvikInsn.vB);
601 callRuntimeHelper(cUnit, rTgt);
602}
603
604void handleSuspendLaunchpads(CompilationUnit *cUnit)
605{
606 LIR** suspendLabel =
607 (LIR **) cUnit->suspendLaunchpads.elemList;
608 int numElems = cUnit->suspendLaunchpads.numUsed;
609
610 for (int i = 0; i < numElems; i++) {
611 /* TUNING: move suspend count load into helper */
612 LIR* lab = suspendLabel[i];
613 LIR* resumeLab = (LIR*)lab->operands[0];
614 cUnit->currentDalvikOffset = lab->operands[1];
615 oatAppendLIR(cUnit, (LIR *)lab);
616 int rTgt = loadHelper(cUnit, OFFSETOF_MEMBER(Thread,
617 pTestSuspendFromCode));
618 if (!cUnit->genDebugger) {
619 // use rSUSPEND for suspend count
620 loadWordDisp(cUnit, rSELF,
621 Thread::SuspendCountOffset().Int32Value(), rSUSPEND);
622 }
623 opReg(cUnit, kOpBlx, rTgt);
624 if ( cUnit->genDebugger) {
625 // use rSUSPEND for update debugger
626 loadWordDisp(cUnit, rSELF,
627 OFFSETOF_MEMBER(Thread, pUpdateDebuggerFromCode),
628 rSUSPEND);
629 }
buzbee82488f52012-03-02 08:20:26 -0800630 opUnconditionalBranch(cUnit, resumeLab);
buzbee31a4a6f2012-02-28 15:36:15 -0800631 }
632}
633
634void handleThrowLaunchpads(CompilationUnit *cUnit)
635{
636 LIR** throwLabel = (LIR **) cUnit->throwLaunchpads.elemList;
637 int numElems = cUnit->throwLaunchpads.numUsed;
638 int i;
639
640 for (i = 0; i < numElems; i++) {
641 LIR* lab = throwLabel[i];
642 cUnit->currentDalvikOffset = lab->operands[1];
643 oatAppendLIR(cUnit, (LIR *)lab);
644 int funcOffset = 0;
645 int v1 = lab->operands[2];
646 int v2 = lab->operands[3];
647 switch(lab->operands[0]) {
648 case kThrowNullPointer:
649 funcOffset = OFFSETOF_MEMBER(Thread, pThrowNullPointerFromCode);
650 break;
651 case kThrowArrayBounds:
buzbee5de34942012-03-01 14:51:57 -0800652 if (v2 != rARG0) {
buzbee82488f52012-03-02 08:20:26 -0800653 opRegCopy(cUnit, rARG0, v1);
654 opRegCopy(cUnit, rARG1, v2);
buzbee31a4a6f2012-02-28 15:36:15 -0800655 } else {
buzbee5de34942012-03-01 14:51:57 -0800656 if (v1 == rARG1) {
buzbee31a4a6f2012-02-28 15:36:15 -0800657#if defined(TARGET_ARM)
658 int rTmp = r12;
659#else
660 int rTmp = oatAllocTemp(cUnit);
661#endif
buzbee82488f52012-03-02 08:20:26 -0800662 opRegCopy(cUnit, rTmp, v1);
663 opRegCopy(cUnit, rARG1, v2);
664 opRegCopy(cUnit, rARG0, rTmp);
buzbee31a4a6f2012-02-28 15:36:15 -0800665#if !(defined(TARGET_ARM))
666 oatFreeTemp(cUnit, rTmp);
667#endif
668 } else {
buzbee82488f52012-03-02 08:20:26 -0800669 opRegCopy(cUnit, rARG1, v2);
670 opRegCopy(cUnit, rARG0, v1);
buzbee31a4a6f2012-02-28 15:36:15 -0800671 }
672 }
673 funcOffset = OFFSETOF_MEMBER(Thread, pThrowArrayBoundsFromCode);
674 break;
675 case kThrowDivZero:
676 funcOffset = OFFSETOF_MEMBER(Thread, pThrowDivZeroFromCode);
677 break;
678 case kThrowVerificationError:
679 loadConstant(cUnit, rARG0, v1);
680 loadConstant(cUnit, rARG1, v2);
681 funcOffset =
682 OFFSETOF_MEMBER(Thread, pThrowVerificationErrorFromCode);
683 break;
684 case kThrowNegArraySize:
buzbee82488f52012-03-02 08:20:26 -0800685 opRegCopy(cUnit, rARG0, v1);
buzbee31a4a6f2012-02-28 15:36:15 -0800686 funcOffset =
687 OFFSETOF_MEMBER(Thread, pThrowNegArraySizeFromCode);
688 break;
689 case kThrowNoSuchMethod:
buzbee82488f52012-03-02 08:20:26 -0800690 opRegCopy(cUnit, rARG0, v1);
buzbee31a4a6f2012-02-28 15:36:15 -0800691 funcOffset =
692 OFFSETOF_MEMBER(Thread, pThrowNoSuchMethodFromCode);
693 break;
694 case kThrowStackOverflow:
695 funcOffset =
696 OFFSETOF_MEMBER(Thread, pThrowStackOverflowFromCode);
697 // Restore stack alignment
698 opRegImm(cUnit, kOpAdd, rSP,
699 (cUnit->numCoreSpills + cUnit->numFPSpills) * 4);
700 break;
701 default:
702 LOG(FATAL) << "Unexpected throw kind: " << lab->operands[0];
703 }
704 int rTgt = loadHelper(cUnit, funcOffset);
705 callRuntimeHelper(cUnit, rTgt);
706 }
707}
708
709/* Needed by the Assembler */
710void oatSetupResourceMasks(LIR* lir)
711{
712 setupResourceMasks(lir);
713}
714
715void genIGet(CompilationUnit* cUnit, MIR* mir, OpSize size,
716 RegLocation rlDest, RegLocation rlObj,
717 bool isLongOrDouble, bool isObject)
718{
719 int fieldOffset;
720 bool isVolatile;
721 uint32_t fieldIdx = mir->dalvikInsn.vC;
722
723 OatCompilationUnit mUnit(cUnit->class_loader, cUnit->class_linker,
724 *cUnit->dex_file, *cUnit->dex_cache,
725 cUnit->code_item, cUnit->method_idx,
726 cUnit->access_flags);
727
728 bool fastPath = cUnit->compiler->ComputeInstanceFieldInfo(fieldIdx, &mUnit,
729 fieldOffset, isVolatile, false);
730
731 if (fastPath && !SLOW_FIELD_PATH) {
732 RegLocation rlResult;
733 RegisterClass regClass = oatRegClassBySize(size);
734 DCHECK_GE(fieldOffset, 0);
735 rlObj = loadValue(cUnit, rlObj, kCoreReg);
736 if (isLongOrDouble) {
737 DCHECK(rlDest.wide);
738 genNullCheck(cUnit, rlObj.sRegLow, rlObj.lowReg, mir);/* null? */
739 int regPtr = oatAllocTemp(cUnit);
740 opRegRegImm(cUnit, kOpAdd, regPtr, rlObj.lowReg, fieldOffset);
741 rlResult = oatEvalLoc(cUnit, rlDest, regClass, true);
742 loadPair(cUnit, regPtr, rlResult.lowReg, rlResult.highReg);
743 if (isVolatile) {
744 oatGenMemBarrier(cUnit, kSY);
745 }
746 oatFreeTemp(cUnit, regPtr);
747 storeValueWide(cUnit, rlDest, rlResult);
748 } else {
749 rlResult = oatEvalLoc(cUnit, rlDest, regClass, true);
750 genNullCheck(cUnit, rlObj.sRegLow, rlObj.lowReg, mir);/* null? */
751 loadBaseDisp(cUnit, mir, rlObj.lowReg, fieldOffset, rlResult.lowReg,
752 kWord, rlObj.sRegLow);
753 if (isVolatile) {
754 oatGenMemBarrier(cUnit, kSY);
755 }
756 storeValue(cUnit, rlDest, rlResult);
757 }
758 } else {
759 int getterOffset = isLongOrDouble ? OFFSETOF_MEMBER(Thread, pGet64Instance) :
760 (isObject ? OFFSETOF_MEMBER(Thread, pGetObjInstance)
761 : OFFSETOF_MEMBER(Thread, pGet32Instance));
762 int rTgt = loadHelper(cUnit, getterOffset);
763 loadValueDirect(cUnit, rlObj, rARG1);
764 loadConstant(cUnit, rARG0, fieldIdx);
765 callRuntimeHelper(cUnit, rTgt);
766 if (isLongOrDouble) {
767 RegLocation rlResult = oatGetReturnWide(cUnit);
768 storeValueWide(cUnit, rlDest, rlResult);
769 } else {
770 RegLocation rlResult = oatGetReturn(cUnit);
771 storeValue(cUnit, rlDest, rlResult);
772 }
773 }
774}
775
776void genIPut(CompilationUnit* cUnit, MIR* mir, OpSize size, RegLocation rlSrc,
777 RegLocation rlObj, bool isLongOrDouble, bool isObject)
778{
779 int fieldOffset;
780 bool isVolatile;
781 uint32_t fieldIdx = mir->dalvikInsn.vC;
782
783 OatCompilationUnit mUnit(cUnit->class_loader, cUnit->class_linker,
784 *cUnit->dex_file, *cUnit->dex_cache,
785 cUnit->code_item, cUnit->method_idx,
786 cUnit->access_flags);
787
788 bool fastPath = cUnit->compiler->ComputeInstanceFieldInfo(fieldIdx, &mUnit,
789 fieldOffset, isVolatile, true);
790 if (fastPath && !SLOW_FIELD_PATH) {
791 RegisterClass regClass = oatRegClassBySize(size);
792 DCHECK_GE(fieldOffset, 0);
793 rlObj = loadValue(cUnit, rlObj, kCoreReg);
794 if (isLongOrDouble) {
795 int regPtr;
796 rlSrc = loadValueWide(cUnit, rlSrc, kAnyReg);
797 genNullCheck(cUnit, rlObj.sRegLow, rlObj.lowReg, mir);/* null? */
798 regPtr = oatAllocTemp(cUnit);
799 opRegRegImm(cUnit, kOpAdd, regPtr, rlObj.lowReg, fieldOffset);
800 if (isVolatile) {
801 oatGenMemBarrier(cUnit, kST);
802 }
803 storePair(cUnit, regPtr, rlSrc.lowReg, rlSrc.highReg);
804 if (isVolatile) {
805 oatGenMemBarrier(cUnit, kSY);
806 }
807 oatFreeTemp(cUnit, regPtr);
808 } else {
809 rlSrc = loadValue(cUnit, rlSrc, regClass);
810 genNullCheck(cUnit, rlObj.sRegLow, rlObj.lowReg, mir);/* null? */
811 if (isVolatile) {
812 oatGenMemBarrier(cUnit, kST);
813 }
814 storeBaseDisp(cUnit, rlObj.lowReg, fieldOffset, rlSrc.lowReg, kWord);
815 if (isVolatile) {
816 oatGenMemBarrier(cUnit, kSY);
817 }
818 }
819 } else {
820 int setterOffset = isLongOrDouble ? OFFSETOF_MEMBER(Thread, pSet64Instance) :
821 (isObject ? OFFSETOF_MEMBER(Thread, pSetObjInstance)
822 : OFFSETOF_MEMBER(Thread, pSet32Instance));
823 int rTgt = loadHelper(cUnit, setterOffset);
824 loadValueDirect(cUnit, rlObj, rARG1);
825 if (isLongOrDouble) {
826 loadValueDirectWide(cUnit, rlSrc, rARG2, rARG3);
827 } else {
828 loadValueDirect(cUnit, rlSrc, rARG2);
829 }
830 loadConstant(cUnit, rARG0, fieldIdx);
831 callRuntimeHelper(cUnit, rTgt);
832 }
833}
834
835void genConstClass(CompilationUnit* cUnit, MIR* mir, RegLocation rlDest,
836 RegLocation rlSrc)
837{
838 uint32_t type_idx = mir->dalvikInsn.vB;
839 int mReg = loadCurrMethod(cUnit);
840 int resReg = oatAllocTemp(cUnit);
841 RegLocation rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
842 if (!cUnit->compiler->CanAccessTypeWithoutChecks(cUnit->method_idx,
843 cUnit->dex_cache,
844 *cUnit->dex_file,
845 type_idx)) {
846 // Call out to helper which resolves type and verifies access.
847 // Resolved type returned in rRET0.
848 int rTgt = loadHelper(cUnit, OFFSETOF_MEMBER(Thread,
849 pInitializeTypeAndVerifyAccessFromCode));
buzbee82488f52012-03-02 08:20:26 -0800850 opRegCopy(cUnit, rARG1, mReg);
buzbee31a4a6f2012-02-28 15:36:15 -0800851 loadConstant(cUnit, rARG0, type_idx);
852 callRuntimeHelper(cUnit, rTgt);
853 RegLocation rlResult = oatGetReturn(cUnit);
854 storeValue(cUnit, rlDest, rlResult);
855 } else {
856 // We're don't need access checks, load type from dex cache
857 int32_t dex_cache_offset =
858 Method::DexCacheResolvedTypesOffset().Int32Value();
859 loadWordDisp(cUnit, mReg, dex_cache_offset, resReg);
860 int32_t offset_of_type =
861 Array::DataOffset(sizeof(Class*)).Int32Value() + (sizeof(Class*)
862 * type_idx);
863 loadWordDisp(cUnit, resReg, offset_of_type, rlResult.lowReg);
864 if (!cUnit->compiler->CanAssumeTypeIsPresentInDexCache(cUnit->dex_cache,
865 type_idx) || SLOW_TYPE_PATH) {
866 // Slow path, at runtime test if type is null and if so initialize
867 oatFlushAllRegs(cUnit);
buzbee82488f52012-03-02 08:20:26 -0800868 LIR* branch1 = opCmpImmBranch(cUnit, kCondEq, rlResult.lowReg, 0,
869 NULL);
buzbee31a4a6f2012-02-28 15:36:15 -0800870 // Resolved, store and hop over following code
871 storeValue(cUnit, rlDest, rlResult);
buzbee82488f52012-03-02 08:20:26 -0800872 LIR* branch2 = opUnconditionalBranch(cUnit,0);
buzbee31a4a6f2012-02-28 15:36:15 -0800873 // TUNING: move slow path to end & remove unconditional branch
874 LIR* target1 = newLIR0(cUnit, kPseudoTargetLabel);
875 target1->defMask = ENCODE_ALL;
buzbee5de34942012-03-01 14:51:57 -0800876 // Call out to helper, which will return resolved type in rARG0
buzbee31a4a6f2012-02-28 15:36:15 -0800877 int rTgt = loadHelper(cUnit, OFFSETOF_MEMBER(Thread,
878 pInitializeTypeFromCode));
buzbee82488f52012-03-02 08:20:26 -0800879 opRegCopy(cUnit, rARG1, mReg);
buzbee31a4a6f2012-02-28 15:36:15 -0800880 loadConstant(cUnit, rARG0, type_idx);
881 callRuntimeHelper(cUnit, rTgt);
882 RegLocation rlResult = oatGetReturn(cUnit);
883 storeValue(cUnit, rlDest, rlResult);
884 // Rejoin code paths
885 LIR* target2 = newLIR0(cUnit, kPseudoTargetLabel);
886 target2->defMask = ENCODE_ALL;
887 branch1->target = (LIR*)target1;
888 branch2->target = (LIR*)target2;
889 } else {
890 // Fast path, we're done - just store result
891 storeValue(cUnit, rlDest, rlResult);
892 }
893 }
894}
895void genConstString(CompilationUnit* cUnit, MIR* mir, RegLocation rlDest,
896 RegLocation rlSrc)
897{
898 /* NOTE: Most strings should be available at compile time */
899 uint32_t string_idx = mir->dalvikInsn.vB;
900 int32_t offset_of_string = Array::DataOffset(sizeof(String*)).Int32Value() +
901 (sizeof(String*) * string_idx);
902 if (!cUnit->compiler->CanAssumeStringIsPresentInDexCache(
903 cUnit->dex_cache, string_idx) || SLOW_STRING_PATH) {
904 // slow path, resolve string if not in dex cache
905 oatFlushAllRegs(cUnit);
906 oatLockCallTemps(cUnit); // Using explicit registers
907 loadCurrMethodDirect(cUnit, rARG2);
908 loadWordDisp(cUnit, rARG2,
909 Method::DexCacheStringsOffset().Int32Value(), rARG0);
910 // Might call out to helper, which will return resolved string in rRET0
911 int rTgt = loadHelper(cUnit, OFFSETOF_MEMBER(Thread,
912 pResolveStringFromCode));
913 loadWordDisp(cUnit, rRET0, offset_of_string, rARG0);
914 loadConstant(cUnit, rARG1, string_idx);
915#if defined(TARGET_ARM)
916 opRegImm(cUnit, kOpCmp, rRET0, 0); // Is resolved?
917 genBarrier(cUnit);
918 // For testing, always force through helper
919 if (!EXERCISE_SLOWEST_STRING_PATH) {
buzbee82488f52012-03-02 08:20:26 -0800920 opIT(cUnit, kArmCondEq, "T");
buzbee31a4a6f2012-02-28 15:36:15 -0800921 }
buzbee82488f52012-03-02 08:20:26 -0800922 opRegCopy(cUnit, rARG0, rARG2); // .eq
buzbee31a4a6f2012-02-28 15:36:15 -0800923 opReg(cUnit, kOpBlx, rTgt); // .eq, helper(Method*, string_idx)
924#else
buzbee82488f52012-03-02 08:20:26 -0800925 LIR* branch = opCmpImmBranch(cUnit, kCondNe, rRET0, 0, NULL);
926 opRegCopy(cUnit, rARG0, rARG2); // .eq
buzbee31a4a6f2012-02-28 15:36:15 -0800927 opReg(cUnit, kOpBlx, rTgt);
928 LIR* target = newLIR0(cUnit, kPseudoTargetLabel);
929 target->defMask = ENCODE_ALL;
930 branch->target = target;
931#endif
932 genBarrier(cUnit);
933 storeValue(cUnit, rlDest, getRetLoc(cUnit));
934 } else {
935 int mReg = loadCurrMethod(cUnit);
936 int resReg = oatAllocTemp(cUnit);
937 RegLocation rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
938 loadWordDisp(cUnit, mReg,
939 Method::DexCacheStringsOffset().Int32Value(), resReg);
940 loadWordDisp(cUnit, resReg, offset_of_string, rlResult.lowReg);
941 storeValue(cUnit, rlDest, rlResult);
942 }
943}
944
945/*
946 * Let helper function take care of everything. Will
947 * call Class::NewInstanceFromCode(type_idx, method);
948 */
949void genNewInstance(CompilationUnit* cUnit, MIR* mir, RegLocation rlDest)
950{
951 oatFlushAllRegs(cUnit); /* Everything to home location */
952 uint32_t type_idx = mir->dalvikInsn.vB;
953 // alloc will always check for resolution, do we also need to verify
954 // access because the verifier was unable to?
955 int rTgt;
956 if (cUnit->compiler->CanAccessInstantiableTypeWithoutChecks(
957 cUnit->method_idx, cUnit->dex_cache, *cUnit->dex_file, type_idx)) {
958 rTgt = loadHelper(cUnit, OFFSETOF_MEMBER(Thread, pAllocObjectFromCode));
959 } else {
960 rTgt = loadHelper(cUnit, OFFSETOF_MEMBER(Thread,
961 pAllocObjectFromCodeWithAccessCheck));
962 }
963 loadCurrMethodDirect(cUnit, rARG1); // arg1 <= Method*
964 loadConstant(cUnit, rARG0, type_idx); // arg0 <- type_idx
965 callRuntimeHelper(cUnit, rTgt);
966 RegLocation rlResult = oatGetReturn(cUnit);
967 storeValue(cUnit, rlDest, rlResult);
968}
969
970void genInstanceof(CompilationUnit* cUnit, MIR* mir, RegLocation rlDest,
971 RegLocation rlSrc)
972{
973 oatFlushAllRegs(cUnit);
974 // May generate a call - use explicit registers
975 oatLockCallTemps(cUnit);
976 uint32_t type_idx = mir->dalvikInsn.vC;
buzbee5de34942012-03-01 14:51:57 -0800977 loadCurrMethodDirect(cUnit, rARG1); // rARG1 <= current Method*
buzbee31a4a6f2012-02-28 15:36:15 -0800978 int classReg = rARG2; // rARG2 will hold the Class*
979 if (!cUnit->compiler->CanAccessTypeWithoutChecks(cUnit->method_idx,
980 cUnit->dex_cache,
981 *cUnit->dex_file,
982 type_idx)) {
983 // Check we have access to type_idx and if not throw IllegalAccessError,
buzbee5de34942012-03-01 14:51:57 -0800984 // returns Class* in rARG0
buzbee31a4a6f2012-02-28 15:36:15 -0800985 int rTgt = loadHelper(cUnit, OFFSETOF_MEMBER(Thread,
986 pInitializeTypeAndVerifyAccessFromCode));
987 loadConstant(cUnit, rARG0, type_idx);
988 callRuntimeHelper(cUnit, rTgt); // InitializeTypeAndVerifyAccess(idx, method)
buzbee82488f52012-03-02 08:20:26 -0800989 opRegCopy(cUnit, classReg, rRET0); // Align usage with fast path
buzbee5de34942012-03-01 14:51:57 -0800990 loadValueDirectFixed(cUnit, rlSrc, rARG0); // rARG0 <= ref
buzbee31a4a6f2012-02-28 15:36:15 -0800991 } else {
buzbee5de34942012-03-01 14:51:57 -0800992 // Load dex cache entry into classReg (rARG2)
buzbee31a4a6f2012-02-28 15:36:15 -0800993 loadValueDirectFixed(cUnit, rlSrc, rARG0); // rARG0 <= ref
994 loadWordDisp(cUnit, rARG1,
995 Method::DexCacheResolvedTypesOffset().Int32Value(),
996 classReg);
997 int32_t offset_of_type =
998 Array::DataOffset(sizeof(Class*)).Int32Value() + (sizeof(Class*)
999 * type_idx);
1000 loadWordDisp(cUnit, classReg, offset_of_type, classReg);
1001 if (!cUnit->compiler->CanAssumeTypeIsPresentInDexCache(
1002 cUnit->dex_cache, type_idx)) {
1003 // Need to test presence of type in dex cache at runtime
buzbee82488f52012-03-02 08:20:26 -08001004 LIR* hopBranch = opCmpImmBranch(cUnit, kCondNe, classReg, 0, NULL);
buzbee31a4a6f2012-02-28 15:36:15 -08001005 // Not resolved
1006 // Call out to helper, which will return resolved type in rRET0
1007 int rTgt = loadHelper(cUnit, OFFSETOF_MEMBER(Thread,
1008 pInitializeTypeFromCode));
1009 loadConstant(cUnit, rARG0, type_idx);
1010 callRuntimeHelper(cUnit, rTgt); // InitializeTypeFromCode(idx, method)
buzbee82488f52012-03-02 08:20:26 -08001011 opRegCopy(cUnit, rARG2, rRET0); // Align usage with fast path
buzbee31a4a6f2012-02-28 15:36:15 -08001012 loadValueDirectFixed(cUnit, rlSrc, rARG0); /* reload Ref */
1013 // Rejoin code paths
1014 LIR* hopTarget = newLIR0(cUnit, kPseudoTargetLabel);
1015 hopTarget->defMask = ENCODE_ALL;
1016 hopBranch->target = (LIR*)hopTarget;
1017 }
1018 }
1019 /* rARG0 is ref, rARG2 is class. If ref==null, use directly as bool result */
buzbee82488f52012-03-02 08:20:26 -08001020 LIR* branch1 = opCmpImmBranch(cUnit, kCondEq, rARG0, 0, NULL);
buzbee31a4a6f2012-02-28 15:36:15 -08001021 /* load object->clazz */
1022 DCHECK_EQ(Object::ClassOffset().Int32Value(), 0);
1023 loadWordDisp(cUnit, rARG0, Object::ClassOffset().Int32Value(), rARG1);
1024 /* rARG0 is ref, rARG1 is ref->clazz, rARG2 is class */
1025 int rTgt = loadHelper(cUnit, OFFSETOF_MEMBER(Thread,
1026 pInstanceofNonTrivialFromCode));
1027#if defined(TARGET_ARM)
1028 opRegReg(cUnit, kOpCmp, rARG1, rARG2); // Same?
1029 genBarrier(cUnit);
buzbee82488f52012-03-02 08:20:26 -08001030 opIT(cUnit, kArmCondEq, "EE"); // if-convert the test
buzbee31a4a6f2012-02-28 15:36:15 -08001031 loadConstant(cUnit, rARG0, 1); // .eq case - load true
buzbee82488f52012-03-02 08:20:26 -08001032 opRegCopy(cUnit, rARG0, rARG2); // .ne case - arg0 <= class
buzbee31a4a6f2012-02-28 15:36:15 -08001033 opReg(cUnit, kOpBlx, rTgt); // .ne case: helper(class, ref->class)
1034 genBarrier(cUnit);
1035 oatClobberCalleeSave(cUnit);
1036#else
buzbee5de34942012-03-01 14:51:57 -08001037 (void)rTgt;
buzbee31a4a6f2012-02-28 15:36:15 -08001038 // Perhaps a general-purpose kOpSelect operator?
1039 UNIMPLEMENTED(FATAL) << "Need non IT implementation";
1040#endif
1041 /* branch target here */
1042 LIR* target = newLIR0(cUnit, kPseudoTargetLabel);
1043 target->defMask = ENCODE_ALL;
1044 RegLocation rlResult = oatGetReturn(cUnit);
1045 storeValue(cUnit, rlDest, rlResult);
1046 branch1->target = (LIR*)target;
1047}
1048
1049void genCheckCast(CompilationUnit* cUnit, MIR* mir, RegLocation rlSrc)
1050{
1051 oatFlushAllRegs(cUnit);
1052 // May generate a call - use explicit registers
1053 oatLockCallTemps(cUnit);
1054 uint32_t type_idx = mir->dalvikInsn.vB;
1055 loadCurrMethodDirect(cUnit, rARG1); // rARG1 <= current Method*
1056 int classReg = rARG2; // rARG2 will hold the Class*
1057 if (!cUnit->compiler->CanAccessTypeWithoutChecks(cUnit->method_idx,
1058 cUnit->dex_cache,
1059 *cUnit->dex_file,
1060 type_idx)) {
1061 // Check we have access to type_idx and if not throw IllegalAccessError,
1062 // returns Class* in rRET0
1063 int rTgt = loadHelper(cUnit, OFFSETOF_MEMBER(Thread,
1064 pInitializeTypeAndVerifyAccessFromCode));
1065 loadConstant(cUnit, rARG0, type_idx);
1066 callRuntimeHelper(cUnit, rTgt); // InitializeTypeAndVerifyAccess(idx, method)
buzbee82488f52012-03-02 08:20:26 -08001067 opRegCopy(cUnit, classReg, rRET0); // Align usage with fast path
buzbee31a4a6f2012-02-28 15:36:15 -08001068 } else {
1069 // Load dex cache entry into classReg (rARG2)
1070 loadWordDisp(cUnit, rARG1,
1071 Method::DexCacheResolvedTypesOffset().Int32Value(),
1072 classReg);
1073 int32_t offset_of_type =
1074 Array::DataOffset(sizeof(Class*)).Int32Value() +
1075 (sizeof(Class*) * type_idx);
1076 loadWordDisp(cUnit, classReg, offset_of_type, classReg);
1077 if (!cUnit->compiler->CanAssumeTypeIsPresentInDexCache(
1078 cUnit->dex_cache, type_idx)) {
1079 // Need to test presence of type in dex cache at runtime
buzbee82488f52012-03-02 08:20:26 -08001080 LIR* hopBranch = opCmpImmBranch(cUnit, kCondNe, classReg, 0, NULL);
buzbee31a4a6f2012-02-28 15:36:15 -08001081 // Not resolved
buzbee5de34942012-03-01 14:51:57 -08001082 // Call out to helper, which will return resolved type in rARG0
1083 int rTgt = loadHelper(cUnit, OFFSETOF_MEMBER(Thread, pInitializeTypeFromCode));
1084 loadConstant(cUnit, rARG0, type_idx);
1085 callRuntimeHelper(cUnit, rTgt); // InitializeTypeFromCode(idx, method)
buzbee82488f52012-03-02 08:20:26 -08001086 opRegCopy(cUnit, classReg, rARG0); // Align usage with fast path
buzbee31a4a6f2012-02-28 15:36:15 -08001087 // Rejoin code paths
1088 LIR* hopTarget = newLIR0(cUnit, kPseudoTargetLabel);
1089 hopTarget->defMask = ENCODE_ALL;
1090 hopBranch->target = (LIR*)hopTarget;
1091 }
1092 }
buzbee5de34942012-03-01 14:51:57 -08001093 // At this point, classReg (rARG2) has class
buzbee31a4a6f2012-02-28 15:36:15 -08001094 loadValueDirectFixed(cUnit, rlSrc, rARG0); // rARG0 <= ref
1095 /* Null is OK - continue */
buzbee82488f52012-03-02 08:20:26 -08001096 LIR* branch1 = opCmpImmBranch(cUnit, kCondEq, rARG0, 0, NULL);
buzbee31a4a6f2012-02-28 15:36:15 -08001097 /* load object->clazz */
1098 DCHECK_EQ(Object::ClassOffset().Int32Value(), 0);
1099 loadWordDisp(cUnit, rARG0, Object::ClassOffset().Int32Value(), rARG1);
1100 /* rARG1 now contains object->clazz */
1101 int rTgt = loadHelper(cUnit, OFFSETOF_MEMBER(Thread,
1102 pCheckCastFromCode));
buzbee5de34942012-03-01 14:51:57 -08001103#if defined(TARGET_MIPS)
buzbee82488f52012-03-02 08:20:26 -08001104 LIR* branch2 = opCmpBranch(cUnit, kCondEq, rARG1, classReg, NULL);
buzbee5de34942012-03-01 14:51:57 -08001105#else
buzbee31a4a6f2012-02-28 15:36:15 -08001106 opRegReg(cUnit, kOpCmp, rARG1, classReg);
buzbee82488f52012-03-02 08:20:26 -08001107 LIR* branch2 = opCondBranch(cUnit, kCondEq, NULL); /* If eq, trivial yes */
buzbee5de34942012-03-01 14:51:57 -08001108#endif
buzbee82488f52012-03-02 08:20:26 -08001109 opRegCopy(cUnit, rARG0, rARG1);
1110 opRegCopy(cUnit, rARG1, rARG2);
buzbee31a4a6f2012-02-28 15:36:15 -08001111 callRuntimeHelper(cUnit, rTgt);
1112 /* branch target here */
1113 LIR* target = newLIR0(cUnit, kPseudoTargetLabel);
1114 target->defMask = ENCODE_ALL;
1115 branch1->target = (LIR*)target;
1116 branch2->target = (LIR*)target;
1117}
1118
1119
1120void genThrow(CompilationUnit* cUnit, MIR* mir, RegLocation rlSrc)
1121{
1122 oatFlushAllRegs(cUnit);
1123 int rTgt = loadHelper(cUnit, OFFSETOF_MEMBER(Thread, pDeliverException));
1124 loadValueDirectFixed(cUnit, rlSrc, rARG0); // Get exception object
1125 callRuntimeHelper(cUnit, rTgt); // art_deliver_exception(exception);
1126}
1127
1128/*
1129 * Generate array store
1130 *
1131 */
1132void genArrayObjPut(CompilationUnit* cUnit, MIR* mir, RegLocation rlArray,
1133 RegLocation rlIndex, RegLocation rlSrc, int scale)
1134{
1135 RegisterClass regClass = oatRegClassBySize(kWord);
1136 int lenOffset = Array::LengthOffset().Int32Value();
1137 int dataOffset = Array::DataOffset(sizeof(Object*)).Int32Value();
1138
1139 oatFlushAllRegs(cUnit);
1140 /* Make sure it's a legal object Put. Use direct regs at first */
1141 loadValueDirectFixed(cUnit, rlArray, rARG1);
1142 loadValueDirectFixed(cUnit, rlSrc, rARG0);
1143
1144 /* null array object? */
1145 genNullCheck(cUnit, rlArray.sRegLow, rARG1, mir);
1146 int rTgt = loadHelper(cUnit, OFFSETOF_MEMBER(Thread,
1147 pCanPutArrayElementFromCode));
1148 /* Get the array's clazz */
1149 loadWordDisp(cUnit, rARG1, Object::ClassOffset().Int32Value(), rARG1);
1150 callRuntimeHelper(cUnit, rTgt);
1151 oatFreeTemp(cUnit, rARG0);
1152 oatFreeTemp(cUnit, rARG1);
1153
1154 // Now, redo loadValues in case they didn't survive the call
1155
1156 int regPtr;
1157 rlArray = loadValue(cUnit, rlArray, kCoreReg);
1158 rlIndex = loadValue(cUnit, rlIndex, kCoreReg);
1159
1160 if (oatIsTemp(cUnit, rlArray.lowReg)) {
1161 oatClobber(cUnit, rlArray.lowReg);
1162 regPtr = rlArray.lowReg;
1163 } else {
1164 regPtr = oatAllocTemp(cUnit);
buzbee82488f52012-03-02 08:20:26 -08001165 opRegCopy(cUnit, regPtr, rlArray.lowReg);
buzbee31a4a6f2012-02-28 15:36:15 -08001166 }
1167
1168 if (!(mir->optimizationFlags & MIR_IGNORE_RANGE_CHECK)) {
1169 int regLen = oatAllocTemp(cUnit);
1170 //NOTE: max live temps(4) here.
1171 /* Get len */
1172 loadWordDisp(cUnit, rlArray.lowReg, lenOffset, regLen);
1173 /* regPtr -> array data */
1174 opRegImm(cUnit, kOpAdd, regPtr, dataOffset);
1175 genRegRegCheck(cUnit, kCondCs, rlIndex.lowReg, regLen, mir,
1176 kThrowArrayBounds);
1177 oatFreeTemp(cUnit, regLen);
1178 } else {
1179 /* regPtr -> array data */
1180 opRegImm(cUnit, kOpAdd, regPtr, dataOffset);
1181 }
1182 /* at this point, regPtr points to array, 2 live temps */
1183 rlSrc = loadValue(cUnit, rlSrc, regClass);
1184 storeBaseIndexed(cUnit, regPtr, rlIndex.lowReg, rlSrc.lowReg,
1185 scale, kWord);
1186}
1187
1188/*
1189 * Generate array load
1190 */
1191void genArrayGet(CompilationUnit* cUnit, MIR* mir, OpSize size,
1192 RegLocation rlArray, RegLocation rlIndex,
1193 RegLocation rlDest, int scale)
1194{
1195 RegisterClass regClass = oatRegClassBySize(size);
1196 int lenOffset = Array::LengthOffset().Int32Value();
1197 int dataOffset;
1198 RegLocation rlResult;
1199 rlArray = loadValue(cUnit, rlArray, kCoreReg);
1200 rlIndex = loadValue(cUnit, rlIndex, kCoreReg);
1201 int regPtr;
1202
1203 if (size == kLong || size == kDouble) {
1204 dataOffset = Array::DataOffset(sizeof(int64_t)).Int32Value();
1205 } else {
1206 dataOffset = Array::DataOffset(sizeof(int32_t)).Int32Value();
1207 }
1208
1209 /* null object? */
1210 genNullCheck(cUnit, rlArray.sRegLow, rlArray.lowReg, mir);
1211
1212 regPtr = oatAllocTemp(cUnit);
1213
1214 if (!(mir->optimizationFlags & MIR_IGNORE_RANGE_CHECK)) {
1215 int regLen = oatAllocTemp(cUnit);
1216 /* Get len */
1217 loadWordDisp(cUnit, rlArray.lowReg, lenOffset, regLen);
1218 /* regPtr -> array data */
1219 opRegRegImm(cUnit, kOpAdd, regPtr, rlArray.lowReg, dataOffset);
1220 genRegRegCheck(cUnit, kCondCs, rlIndex.lowReg, regLen, mir,
1221 kThrowArrayBounds);
1222 oatFreeTemp(cUnit, regLen);
1223 } else {
1224 /* regPtr -> array data */
1225 opRegRegImm(cUnit, kOpAdd, regPtr, rlArray.lowReg, dataOffset);
1226 }
1227 oatFreeTemp(cUnit, rlArray.lowReg);
1228 if ((size == kLong) || (size == kDouble)) {
1229 if (scale) {
1230 int rNewIndex = oatAllocTemp(cUnit);
1231 opRegRegImm(cUnit, kOpLsl, rNewIndex, rlIndex.lowReg, scale);
1232 opRegReg(cUnit, kOpAdd, regPtr, rNewIndex);
1233 oatFreeTemp(cUnit, rNewIndex);
1234 } else {
1235 opRegReg(cUnit, kOpAdd, regPtr, rlIndex.lowReg);
1236 }
1237 oatFreeTemp(cUnit, rlIndex.lowReg);
1238 rlResult = oatEvalLoc(cUnit, rlDest, regClass, true);
1239
1240 loadPair(cUnit, regPtr, rlResult.lowReg, rlResult.highReg);
1241
1242 oatFreeTemp(cUnit, regPtr);
1243 storeValueWide(cUnit, rlDest, rlResult);
1244 } else {
1245 rlResult = oatEvalLoc(cUnit, rlDest, regClass, true);
1246
1247 loadBaseIndexed(cUnit, regPtr, rlIndex.lowReg, rlResult.lowReg,
1248 scale, size);
1249
1250 oatFreeTemp(cUnit, regPtr);
1251 storeValue(cUnit, rlDest, rlResult);
1252 }
1253}
1254
1255/*
1256 * Generate array store
1257 *
1258 */
1259void genArrayPut(CompilationUnit* cUnit, MIR* mir, OpSize size,
1260 RegLocation rlArray, RegLocation rlIndex,
1261 RegLocation rlSrc, int scale)
1262{
1263 RegisterClass regClass = oatRegClassBySize(size);
1264 int lenOffset = Array::LengthOffset().Int32Value();
1265 int dataOffset;
1266
1267 if (size == kLong || size == kDouble) {
1268 dataOffset = Array::DataOffset(sizeof(int64_t)).Int32Value();
1269 } else {
1270 dataOffset = Array::DataOffset(sizeof(int32_t)).Int32Value();
1271 }
1272
1273 int regPtr;
1274 rlArray = loadValue(cUnit, rlArray, kCoreReg);
1275 rlIndex = loadValue(cUnit, rlIndex, kCoreReg);
1276
1277 if (oatIsTemp(cUnit, rlArray.lowReg)) {
1278 oatClobber(cUnit, rlArray.lowReg);
1279 regPtr = rlArray.lowReg;
1280 } else {
1281 regPtr = oatAllocTemp(cUnit);
buzbee82488f52012-03-02 08:20:26 -08001282 opRegCopy(cUnit, regPtr, rlArray.lowReg);
buzbee31a4a6f2012-02-28 15:36:15 -08001283 }
1284
1285 /* null object? */
1286 genNullCheck(cUnit, rlArray.sRegLow, rlArray.lowReg, mir);
1287
1288 if (!(mir->optimizationFlags & MIR_IGNORE_RANGE_CHECK)) {
1289 int regLen = oatAllocTemp(cUnit);
1290 //NOTE: max live temps(4) here.
1291 /* Get len */
1292 loadWordDisp(cUnit, rlArray.lowReg, lenOffset, regLen);
1293 /* regPtr -> array data */
1294 opRegImm(cUnit, kOpAdd, regPtr, dataOffset);
1295 genRegRegCheck(cUnit, kCondCs, rlIndex.lowReg, regLen, mir,
1296 kThrowArrayBounds);
1297 oatFreeTemp(cUnit, regLen);
1298 } else {
1299 /* regPtr -> array data */
1300 opRegImm(cUnit, kOpAdd, regPtr, dataOffset);
1301 }
1302 /* at this point, regPtr points to array, 2 live temps */
1303 if ((size == kLong) || (size == kDouble)) {
1304 //TUNING: specific wide routine that can handle fp regs
1305 if (scale) {
1306 int rNewIndex = oatAllocTemp(cUnit);
1307 opRegRegImm(cUnit, kOpLsl, rNewIndex, rlIndex.lowReg, scale);
1308 opRegReg(cUnit, kOpAdd, regPtr, rNewIndex);
1309 oatFreeTemp(cUnit, rNewIndex);
1310 } else {
1311 opRegReg(cUnit, kOpAdd, regPtr, rlIndex.lowReg);
1312 }
1313 rlSrc = loadValueWide(cUnit, rlSrc, regClass);
1314
1315 storePair(cUnit, regPtr, rlSrc.lowReg, rlSrc.highReg);
1316
1317 oatFreeTemp(cUnit, regPtr);
1318 } else {
1319 rlSrc = loadValue(cUnit, rlSrc, regClass);
1320
1321 storeBaseIndexed(cUnit, regPtr, rlIndex.lowReg, rlSrc.lowReg,
1322 scale, size);
1323 }
1324}
1325
1326void genLong3Addr(CompilationUnit* cUnit, MIR* mir, OpKind firstOp,
1327 OpKind secondOp, RegLocation rlDest,
1328 RegLocation rlSrc1, RegLocation rlSrc2)
1329{
1330 RegLocation rlResult;
1331#if defined(TARGET_ARM)
1332 /*
1333 * NOTE: This is the one place in the code in which we might have
1334 * as many as six live temporary registers. There are 5 in the normal
1335 * set for Arm. Until we have spill capabilities, temporarily add
1336 * lr to the temp set. It is safe to do this locally, but note that
1337 * lr is used explicitly elsewhere in the code generator and cannot
1338 * normally be used as a general temp register.
1339 */
1340 oatMarkTemp(cUnit, rLR); // Add lr to the temp pool
1341 oatFreeTemp(cUnit, rLR); // and make it available
1342#endif
1343 rlSrc1 = loadValueWide(cUnit, rlSrc1, kCoreReg);
1344 rlSrc2 = loadValueWide(cUnit, rlSrc2, kCoreReg);
1345 rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
1346 // The longs may overlap - use intermediate temp if so
1347 if (rlResult.lowReg == rlSrc1.highReg) {
1348 int tReg = oatAllocTemp(cUnit);
buzbee82488f52012-03-02 08:20:26 -08001349 opRegCopy(cUnit, tReg, rlSrc1.highReg);
buzbee31a4a6f2012-02-28 15:36:15 -08001350 opRegRegReg(cUnit, firstOp, rlResult.lowReg, rlSrc1.lowReg,
1351 rlSrc2.lowReg);
1352 opRegRegReg(cUnit, secondOp, rlResult.highReg, tReg,
1353 rlSrc2.highReg);
1354 oatFreeTemp(cUnit, tReg);
1355 } else {
1356 opRegRegReg(cUnit, firstOp, rlResult.lowReg, rlSrc1.lowReg,
1357 rlSrc2.lowReg);
1358 opRegRegReg(cUnit, secondOp, rlResult.highReg, rlSrc1.highReg,
1359 rlSrc2.highReg);
1360 }
1361 /*
1362 * NOTE: If rlDest refers to a frame variable in a large frame, the
1363 * following storeValueWide might need to allocate a temp register.
1364 * To further work around the lack of a spill capability, explicitly
1365 * free any temps from rlSrc1 & rlSrc2 that aren't still live in rlResult.
1366 * Remove when spill is functional.
1367 */
1368 freeRegLocTemps(cUnit, rlResult, rlSrc1);
1369 freeRegLocTemps(cUnit, rlResult, rlSrc2);
1370 storeValueWide(cUnit, rlDest, rlResult);
1371#if defined(TARGET_ARM)
1372 oatClobber(cUnit, rLR);
1373 oatUnmarkTemp(cUnit, rLR); // Remove lr from the temp pool
1374#endif
1375}
1376
1377
1378bool genShiftOpLong(CompilationUnit* cUnit, MIR* mir, RegLocation rlDest,
1379 RegLocation rlSrc1, RegLocation rlShift)
1380{
1381 int funcOffset;
1382
1383 switch( mir->dalvikInsn.opcode) {
1384 case OP_SHL_LONG:
1385 case OP_SHL_LONG_2ADDR:
1386 funcOffset = OFFSETOF_MEMBER(Thread, pShlLong);
1387 break;
1388 case OP_SHR_LONG:
1389 case OP_SHR_LONG_2ADDR:
1390 funcOffset = OFFSETOF_MEMBER(Thread, pShrLong);
1391 break;
1392 case OP_USHR_LONG:
1393 case OP_USHR_LONG_2ADDR:
1394 funcOffset = OFFSETOF_MEMBER(Thread, pUshrLong);
1395 break;
1396 default:
1397 LOG(FATAL) << "Unexpected case";
1398 return true;
1399 }
1400 oatFlushAllRegs(cUnit); /* Send everything to home location */
1401 int rTgt = loadHelper(cUnit, funcOffset);
1402 loadValueDirectWideFixed(cUnit, rlSrc1, rARG0, rARG1);
1403 loadValueDirect(cUnit, rlShift, rARG2);
1404 callRuntimeHelper(cUnit, rTgt);
1405 RegLocation rlResult = oatGetReturnWide(cUnit);
1406 storeValueWide(cUnit, rlDest, rlResult);
1407 return false;
1408}
1409
1410
1411bool genArithOpInt(CompilationUnit* cUnit, MIR* mir, RegLocation rlDest,
1412 RegLocation rlSrc1, RegLocation rlSrc2)
1413{
1414 OpKind op = kOpBkpt;
1415 bool callOut = false;
1416 bool checkZero = false;
1417 bool unary = false;
1418 int retReg = rRET0;
1419 int funcOffset;
1420 RegLocation rlResult;
1421 bool shiftOp = false;
1422
1423 switch (mir->dalvikInsn.opcode) {
1424 case OP_NEG_INT:
1425 op = kOpNeg;
1426 unary = true;
1427 break;
1428 case OP_NOT_INT:
1429 op = kOpMvn;
1430 unary = true;
1431 break;
1432 case OP_ADD_INT:
1433 case OP_ADD_INT_2ADDR:
1434 op = kOpAdd;
1435 break;
1436 case OP_SUB_INT:
1437 case OP_SUB_INT_2ADDR:
1438 op = kOpSub;
1439 break;
1440 case OP_MUL_INT:
1441 case OP_MUL_INT_2ADDR:
1442 op = kOpMul;
1443 break;
1444 case OP_DIV_INT:
1445 case OP_DIV_INT_2ADDR:
1446 callOut = true;
1447 checkZero = true;
1448 funcOffset = OFFSETOF_MEMBER(Thread, pIdiv);
1449 retReg = rRET0;
1450 break;
buzbee5de34942012-03-01 14:51:57 -08001451 /* NOTE: returns in rARG1 */
buzbee31a4a6f2012-02-28 15:36:15 -08001452 case OP_REM_INT:
1453 case OP_REM_INT_2ADDR:
1454 callOut = true;
1455 checkZero = true;
1456 funcOffset = OFFSETOF_MEMBER(Thread, pIdivmod);
1457 retReg = rRET1;
1458 break;
1459 case OP_AND_INT:
1460 case OP_AND_INT_2ADDR:
1461 op = kOpAnd;
1462 break;
1463 case OP_OR_INT:
1464 case OP_OR_INT_2ADDR:
1465 op = kOpOr;
1466 break;
1467 case OP_XOR_INT:
1468 case OP_XOR_INT_2ADDR:
1469 op = kOpXor;
1470 break;
1471 case OP_SHL_INT:
1472 case OP_SHL_INT_2ADDR:
1473 shiftOp = true;
1474 op = kOpLsl;
1475 break;
1476 case OP_SHR_INT:
1477 case OP_SHR_INT_2ADDR:
1478 shiftOp = true;
1479 op = kOpAsr;
1480 break;
1481 case OP_USHR_INT:
1482 case OP_USHR_INT_2ADDR:
1483 shiftOp = true;
1484 op = kOpLsr;
1485 break;
1486 default:
1487 LOG(FATAL) << "Invalid word arith op: " <<
1488 (int)mir->dalvikInsn.opcode;
1489 }
1490 if (!callOut) {
1491 rlSrc1 = loadValue(cUnit, rlSrc1, kCoreReg);
1492 if (unary) {
1493 rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
1494 opRegReg(cUnit, op, rlResult.lowReg,
1495 rlSrc1.lowReg);
1496 } else {
1497 rlSrc2 = loadValue(cUnit, rlSrc2, kCoreReg);
1498 if (shiftOp) {
1499 int tReg = oatAllocTemp(cUnit);
1500 opRegRegImm(cUnit, kOpAnd, tReg, rlSrc2.lowReg, 31);
1501 rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
1502 opRegRegReg(cUnit, op, rlResult.lowReg,
1503 rlSrc1.lowReg, tReg);
1504 oatFreeTemp(cUnit, tReg);
1505 } else {
1506 rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
1507 opRegRegReg(cUnit, op, rlResult.lowReg,
1508 rlSrc1.lowReg, rlSrc2.lowReg);
1509 }
1510 }
1511 storeValue(cUnit, rlDest, rlResult);
1512 } else {
1513 RegLocation rlResult;
1514 oatFlushAllRegs(cUnit); /* Send everything to home location */
1515 loadValueDirectFixed(cUnit, rlSrc2, rRET1);
1516 int rTgt = loadHelper(cUnit, funcOffset);
1517 loadValueDirectFixed(cUnit, rlSrc1, rARG0);
1518 if (checkZero) {
1519 genImmedCheck(cUnit, kCondEq, rARG1, 0, mir, kThrowDivZero);
1520 }
1521 callRuntimeHelper(cUnit, rTgt);
1522 if (retReg == rRET0)
1523 rlResult = oatGetReturn(cUnit);
1524 else
1525 rlResult = oatGetReturnAlt(cUnit);
1526 storeValue(cUnit, rlDest, rlResult);
1527 }
1528 return false;
1529}
1530
1531/*
1532 * The following are the first-level codegen routines that analyze the format
1533 * of each bytecode then either dispatch special purpose codegen routines
1534 * or produce corresponding Thumb instructions directly.
1535 */
1536
1537bool isPowerOfTwo(int x)
1538{
1539 return (x & (x - 1)) == 0;
1540}
1541
1542// Returns true if no more than two bits are set in 'x'.
1543bool isPopCountLE2(unsigned int x)
1544{
1545 x &= x - 1;
1546 return (x & (x - 1)) == 0;
1547}
1548
1549// Returns the index of the lowest set bit in 'x'.
1550int lowestSetBit(unsigned int x) {
1551 int bit_posn = 0;
1552 while ((x & 0xf) == 0) {
1553 bit_posn += 4;
1554 x >>= 4;
1555 }
1556 while ((x & 1) == 0) {
1557 bit_posn++;
1558 x >>= 1;
1559 }
1560 return bit_posn;
1561}
1562
1563// Returns true if it added instructions to 'cUnit' to divide 'rlSrc' by 'lit'
1564// and store the result in 'rlDest'.
1565bool handleEasyDivide(CompilationUnit* cUnit, Opcode dalvikOpcode,
1566 RegLocation rlSrc, RegLocation rlDest, int lit)
1567{
1568 if (lit < 2 || !isPowerOfTwo(lit)) {
1569 return false;
1570 }
1571 int k = lowestSetBit(lit);
1572 if (k >= 30) {
1573 // Avoid special cases.
1574 return false;
1575 }
1576 bool div = (dalvikOpcode == OP_DIV_INT_LIT8 ||
1577 dalvikOpcode == OP_DIV_INT_LIT16);
1578 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
1579 RegLocation rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
1580 if (div) {
1581 int tReg = oatAllocTemp(cUnit);
1582 if (lit == 2) {
1583 // Division by 2 is by far the most common division by constant.
1584 opRegRegImm(cUnit, kOpLsr, tReg, rlSrc.lowReg, 32 - k);
1585 opRegRegReg(cUnit, kOpAdd, tReg, tReg, rlSrc.lowReg);
1586 opRegRegImm(cUnit, kOpAsr, rlResult.lowReg, tReg, k);
1587 } else {
1588 opRegRegImm(cUnit, kOpAsr, tReg, rlSrc.lowReg, 31);
1589 opRegRegImm(cUnit, kOpLsr, tReg, tReg, 32 - k);
1590 opRegRegReg(cUnit, kOpAdd, tReg, tReg, rlSrc.lowReg);
1591 opRegRegImm(cUnit, kOpAsr, rlResult.lowReg, tReg, k);
1592 }
1593 } else {
1594 int cReg = oatAllocTemp(cUnit);
1595 loadConstant(cUnit, cReg, lit - 1);
1596 int tReg1 = oatAllocTemp(cUnit);
1597 int tReg2 = oatAllocTemp(cUnit);
1598 if (lit == 2) {
1599 opRegRegImm(cUnit, kOpLsr, tReg1, rlSrc.lowReg, 32 - k);
1600 opRegRegReg(cUnit, kOpAdd, tReg2, tReg1, rlSrc.lowReg);
1601 opRegRegReg(cUnit, kOpAnd, tReg2, tReg2, cReg);
1602 opRegRegReg(cUnit, kOpSub, rlResult.lowReg, tReg2, tReg1);
1603 } else {
1604 opRegRegImm(cUnit, kOpAsr, tReg1, rlSrc.lowReg, 31);
1605 opRegRegImm(cUnit, kOpLsr, tReg1, tReg1, 32 - k);
1606 opRegRegReg(cUnit, kOpAdd, tReg2, tReg1, rlSrc.lowReg);
1607 opRegRegReg(cUnit, kOpAnd, tReg2, tReg2, cReg);
1608 opRegRegReg(cUnit, kOpSub, rlResult.lowReg, tReg2, tReg1);
1609 }
1610 }
1611 storeValue(cUnit, rlDest, rlResult);
1612 return true;
1613}
1614
1615void genMultiplyByTwoBitMultiplier(CompilationUnit* cUnit, RegLocation rlSrc,
1616 RegLocation rlResult, int lit,
1617 int firstBit, int secondBit)
1618{
buzbee5de34942012-03-01 14:51:57 -08001619#if defined(TARGET_MIPS)
1620 UNIMPLEMENTED(FATAL) << "Need shift & add primative";
1621#else
buzbee31a4a6f2012-02-28 15:36:15 -08001622 opRegRegRegShift(cUnit, kOpAdd, rlResult.lowReg, rlSrc.lowReg, rlSrc.lowReg,
1623 encodeShift(kArmLsl, secondBit - firstBit));
buzbee5de34942012-03-01 14:51:57 -08001624#endif
buzbee31a4a6f2012-02-28 15:36:15 -08001625 if (firstBit != 0) {
1626 opRegRegImm(cUnit, kOpLsl, rlResult.lowReg, rlResult.lowReg, firstBit);
1627 }
1628}
1629
1630// Returns true if it added instructions to 'cUnit' to multiply 'rlSrc' by 'lit'
1631// and store the result in 'rlDest'.
1632bool handleEasyMultiply(CompilationUnit* cUnit, RegLocation rlSrc,
1633 RegLocation rlDest, int lit)
1634{
1635 // Can we simplify this multiplication?
1636 bool powerOfTwo = false;
1637 bool popCountLE2 = false;
1638 bool powerOfTwoMinusOne = false;
1639 if (lit < 2) {
1640 // Avoid special cases.
1641 return false;
1642 } else if (isPowerOfTwo(lit)) {
1643 powerOfTwo = true;
1644 } else if (isPopCountLE2(lit)) {
1645 popCountLE2 = true;
1646 } else if (isPowerOfTwo(lit + 1)) {
1647 powerOfTwoMinusOne = true;
1648 } else {
1649 return false;
1650 }
1651 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
1652 RegLocation rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
1653 if (powerOfTwo) {
1654 // Shift.
1655 opRegRegImm(cUnit, kOpLsl, rlResult.lowReg, rlSrc.lowReg,
1656 lowestSetBit(lit));
1657 } else if (popCountLE2) {
1658 // Shift and add and shift.
1659 int firstBit = lowestSetBit(lit);
1660 int secondBit = lowestSetBit(lit ^ (1 << firstBit));
1661 genMultiplyByTwoBitMultiplier(cUnit, rlSrc, rlResult, lit,
1662 firstBit, secondBit);
1663 } else {
1664 // Reverse subtract: (src << (shift + 1)) - src.
1665 DCHECK(powerOfTwoMinusOne);
1666 // TUNING: rsb dst, src, src lsl#lowestSetBit(lit + 1)
1667 int tReg = oatAllocTemp(cUnit);
1668 opRegRegImm(cUnit, kOpLsl, tReg, rlSrc.lowReg, lowestSetBit(lit + 1));
1669 opRegRegReg(cUnit, kOpSub, rlResult.lowReg, tReg, rlSrc.lowReg);
1670 }
1671 storeValue(cUnit, rlDest, rlResult);
1672 return true;
1673}
1674
1675bool genArithOpIntLit(CompilationUnit* cUnit, MIR* mir, RegLocation rlDest,
1676 RegLocation rlSrc, int lit)
1677{
1678 Opcode dalvikOpcode = mir->dalvikInsn.opcode;
1679 RegLocation rlResult;
1680 OpKind op = (OpKind)0; /* Make gcc happy */
1681 int shiftOp = false;
1682 bool isDiv = false;
1683 int funcOffset;
1684 int rTgt;
1685
1686 switch (dalvikOpcode) {
1687 case OP_RSUB_INT_LIT8:
1688 case OP_RSUB_INT: {
1689 int tReg;
1690 //TUNING: add support for use of Arm rsub op
1691 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
1692 tReg = oatAllocTemp(cUnit);
1693 loadConstant(cUnit, tReg, lit);
1694 rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
1695 opRegRegReg(cUnit, kOpSub, rlResult.lowReg,
1696 tReg, rlSrc.lowReg);
1697 storeValue(cUnit, rlDest, rlResult);
1698 return false;
1699 break;
1700 }
1701
1702 case OP_ADD_INT_LIT8:
1703 case OP_ADD_INT_LIT16:
1704 op = kOpAdd;
1705 break;
1706 case OP_MUL_INT_LIT8:
1707 case OP_MUL_INT_LIT16: {
1708 if (handleEasyMultiply(cUnit, rlSrc, rlDest, lit)) {
1709 return false;
1710 }
1711 op = kOpMul;
1712 break;
1713 }
1714 case OP_AND_INT_LIT8:
1715 case OP_AND_INT_LIT16:
1716 op = kOpAnd;
1717 break;
1718 case OP_OR_INT_LIT8:
1719 case OP_OR_INT_LIT16:
1720 op = kOpOr;
1721 break;
1722 case OP_XOR_INT_LIT8:
1723 case OP_XOR_INT_LIT16:
1724 op = kOpXor;
1725 break;
1726 case OP_SHL_INT_LIT8:
1727 lit &= 31;
1728 shiftOp = true;
1729 op = kOpLsl;
1730 break;
1731 case OP_SHR_INT_LIT8:
1732 lit &= 31;
1733 shiftOp = true;
1734 op = kOpAsr;
1735 break;
1736 case OP_USHR_INT_LIT8:
1737 lit &= 31;
1738 shiftOp = true;
1739 op = kOpLsr;
1740 break;
1741
1742 case OP_DIV_INT_LIT8:
1743 case OP_DIV_INT_LIT16:
1744 case OP_REM_INT_LIT8:
1745 case OP_REM_INT_LIT16:
1746 if (lit == 0) {
1747 genImmedCheck(cUnit, kCondAl, 0, 0, mir, kThrowDivZero);
1748 return false;
1749 }
1750 if (handleEasyDivide(cUnit, dalvikOpcode, rlSrc, rlDest, lit)) {
1751 return false;
1752 }
1753 oatFlushAllRegs(cUnit); /* Everything to home location */
1754 loadValueDirectFixed(cUnit, rlSrc, rARG0);
1755 oatClobber(cUnit, rARG0);
1756 if ((dalvikOpcode == OP_DIV_INT_LIT8) ||
1757 (dalvikOpcode == OP_DIV_INT_LIT16)) {
1758 funcOffset = OFFSETOF_MEMBER(Thread, pIdiv);
1759 isDiv = true;
1760 } else {
1761 funcOffset = OFFSETOF_MEMBER(Thread, pIdivmod);
1762 isDiv = false;
1763 }
1764 rTgt = loadHelper(cUnit, funcOffset);
1765 loadConstant(cUnit, rARG1, lit);
1766 callRuntimeHelper(cUnit, rTgt);
1767 if (isDiv)
1768 rlResult = oatGetReturn(cUnit);
1769 else
1770 rlResult = oatGetReturnAlt(cUnit);
1771 storeValue(cUnit, rlDest, rlResult);
1772 return false;
1773 break;
1774 default:
1775 return true;
1776 }
1777 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
1778 rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
1779 // Avoid shifts by literal 0 - no support in Thumb. Change to copy
1780 if (shiftOp && (lit == 0)) {
buzbee82488f52012-03-02 08:20:26 -08001781 opRegCopy(cUnit, rlResult.lowReg, rlSrc.lowReg);
buzbee31a4a6f2012-02-28 15:36:15 -08001782 } else {
1783 opRegRegImm(cUnit, op, rlResult.lowReg, rlSrc.lowReg, lit);
1784 }
1785 storeValue(cUnit, rlDest, rlResult);
1786 return false;
1787}
1788
1789bool genArithOpLong(CompilationUnit* cUnit, MIR* mir, RegLocation rlDest,
1790 RegLocation rlSrc1, RegLocation rlSrc2)
1791{
1792 RegLocation rlResult;
1793 OpKind firstOp = kOpBkpt;
1794 OpKind secondOp = kOpBkpt;
1795 bool callOut = false;
1796 bool checkZero = false;
1797 int funcOffset;
1798 int retReg = rRET0;
1799
1800 switch (mir->dalvikInsn.opcode) {
1801 case OP_NOT_LONG:
1802 rlSrc2 = loadValueWide(cUnit, rlSrc2, kCoreReg);
1803 rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
1804 // Check for destructive overlap
1805 if (rlResult.lowReg == rlSrc2.highReg) {
1806 int tReg = oatAllocTemp(cUnit);
buzbee82488f52012-03-02 08:20:26 -08001807 opRegCopy(cUnit, tReg, rlSrc2.highReg);
buzbee31a4a6f2012-02-28 15:36:15 -08001808 opRegReg(cUnit, kOpMvn, rlResult.lowReg, rlSrc2.lowReg);
1809 opRegReg(cUnit, kOpMvn, rlResult.highReg, tReg);
1810 oatFreeTemp(cUnit, tReg);
1811 } else {
1812 opRegReg(cUnit, kOpMvn, rlResult.lowReg, rlSrc2.lowReg);
1813 opRegReg(cUnit, kOpMvn, rlResult.highReg, rlSrc2.highReg);
1814 }
1815 storeValueWide(cUnit, rlDest, rlResult);
1816 return false;
1817 break;
1818 case OP_ADD_LONG:
1819 case OP_ADD_LONG_2ADDR:
1820 firstOp = kOpAdd;
1821 secondOp = kOpAdc;
1822 break;
1823 case OP_SUB_LONG:
1824 case OP_SUB_LONG_2ADDR:
1825 firstOp = kOpSub;
1826 secondOp = kOpSbc;
1827 break;
1828 case OP_MUL_LONG:
1829 case OP_MUL_LONG_2ADDR:
1830 callOut = true;
1831 retReg = rRET0;
1832 funcOffset = OFFSETOF_MEMBER(Thread, pLmul);
1833 break;
1834 case OP_DIV_LONG:
1835 case OP_DIV_LONG_2ADDR:
1836 callOut = true;
1837 checkZero = true;
1838 retReg = rRET0;
1839 funcOffset = OFFSETOF_MEMBER(Thread, pLdivmod);
1840 break;
1841 /* NOTE - result is in rARG2/rARG3 instead of rRET0/rRET1 */
1842 // FIXME: is true, or could be made true, or other targets?
1843 case OP_REM_LONG:
1844 case OP_REM_LONG_2ADDR:
1845 callOut = true;
1846 checkZero = true;
1847 funcOffset = OFFSETOF_MEMBER(Thread, pLdivmod);
1848 retReg = rARG2;
1849 break;
1850 case OP_AND_LONG_2ADDR:
1851 case OP_AND_LONG:
1852 firstOp = kOpAnd;
1853 secondOp = kOpAnd;
1854 break;
1855 case OP_OR_LONG:
1856 case OP_OR_LONG_2ADDR:
1857 firstOp = kOpOr;
1858 secondOp = kOpOr;
1859 break;
1860 case OP_XOR_LONG:
1861 case OP_XOR_LONG_2ADDR:
1862 firstOp = kOpXor;
1863 secondOp = kOpXor;
1864 break;
1865 case OP_NEG_LONG: {
1866 rlSrc2 = loadValueWide(cUnit, rlSrc2, kCoreReg);
1867 rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
1868 int zReg = oatAllocTemp(cUnit);
1869 loadConstantNoClobber(cUnit, zReg, 0);
1870 // Check for destructive overlap
1871 if (rlResult.lowReg == rlSrc2.highReg) {
1872 int tReg = oatAllocTemp(cUnit);
1873 opRegRegReg(cUnit, kOpSub, rlResult.lowReg,
1874 zReg, rlSrc2.lowReg);
1875 opRegRegReg(cUnit, kOpSbc, rlResult.highReg,
1876 zReg, tReg);
1877 oatFreeTemp(cUnit, tReg);
1878 } else {
1879 opRegRegReg(cUnit, kOpSub, rlResult.lowReg,
1880 zReg, rlSrc2.lowReg);
1881 opRegRegReg(cUnit, kOpSbc, rlResult.highReg,
1882 zReg, rlSrc2.highReg);
1883 }
1884 oatFreeTemp(cUnit, zReg);
1885 storeValueWide(cUnit, rlDest, rlResult);
1886 return false;
1887 }
1888 default:
1889 LOG(FATAL) << "Invalid long arith op";
1890 }
1891 if (!callOut) {
1892 genLong3Addr(cUnit, mir, firstOp, secondOp, rlDest, rlSrc1, rlSrc2);
1893 } else {
1894 int rTgt;
1895 oatFlushAllRegs(cUnit); /* Send everything to home location */
1896 if (checkZero) {
1897 loadValueDirectWideFixed(cUnit, rlSrc2, rARG2, rARG3);
1898 rTgt = loadHelper(cUnit, funcOffset);
1899 loadValueDirectWideFixed(cUnit, rlSrc1, rARG0, rARG1);
1900 int tReg = oatAllocTemp(cUnit);
1901#if defined(TARGET_ARM)
1902 newLIR4(cUnit, kThumb2OrrRRRs, tReg, rARG2, rARG3, 0);
1903 oatFreeTemp(cUnit, tReg);
1904 genCheck(cUnit, kCondEq, mir, kThrowDivZero);
1905#else
1906 opRegRegReg(cUnit, kOpOr, tReg, rARG2, rARG3);
buzbee5de34942012-03-01 14:51:57 -08001907 genImmedCheck(cUnit, kCondEq, tReg, 0, mir, kThrowDivZero);
buzbee31a4a6f2012-02-28 15:36:15 -08001908 oatFreeTemp(cUnit, tReg);
1909#endif
1910 } else {
1911 rTgt = loadHelper(cUnit, funcOffset);
1912 loadValueDirectWideFixed(cUnit, rlSrc1, rARG0, rARG1);
1913 loadValueDirectWideFixed(cUnit, rlSrc2, rARG2, rARG3);
1914 }
1915 callRuntimeHelper(cUnit, rTgt);
1916 // Adjust return regs in to handle case of rem returning rARG2/rARG3
1917 if (retReg == rRET0)
1918 rlResult = oatGetReturnWide(cUnit);
1919 else
1920 rlResult = oatGetReturnWideAlt(cUnit);
1921 storeValueWide(cUnit, rlDest, rlResult);
1922 }
1923 return false;
1924}
1925
1926bool genConversionCall(CompilationUnit* cUnit, MIR* mir, int funcOffset,
1927 int srcSize, int tgtSize)
1928{
1929 /*
1930 * Don't optimize the register usage since it calls out to support
1931 * functions
1932 */
1933 RegLocation rlSrc;
1934 RegLocation rlDest;
1935 oatFlushAllRegs(cUnit); /* Send everything to home location */
1936 int rTgt = loadHelper(cUnit, funcOffset);
1937 if (srcSize == 1) {
1938 rlSrc = oatGetSrc(cUnit, mir, 0);
1939 loadValueDirectFixed(cUnit, rlSrc, rARG0);
1940 } else {
1941 rlSrc = oatGetSrcWide(cUnit, mir, 0, 1);
1942 loadValueDirectWideFixed(cUnit, rlSrc, rARG0, rARG1);
1943 }
1944 callRuntimeHelper(cUnit, rTgt);
1945 if (tgtSize == 1) {
1946 RegLocation rlResult;
1947 rlDest = oatGetDest(cUnit, mir, 0);
1948 rlResult = oatGetReturn(cUnit);
1949 storeValue(cUnit, rlDest, rlResult);
1950 } else {
1951 RegLocation rlResult;
1952 rlDest = oatGetDestWide(cUnit, mir, 0, 1);
1953 rlResult = oatGetReturnWide(cUnit);
1954 storeValueWide(cUnit, rlDest, rlResult);
1955 }
1956 return false;
1957}
1958
1959void genNegFloat(CompilationUnit* cUnit, RegLocation rlDest, RegLocation rlSrc);
1960bool genArithOpFloatPortable(CompilationUnit* cUnit, MIR* mir,
1961 RegLocation rlDest, RegLocation rlSrc1,
1962 RegLocation rlSrc2)
1963{
1964 RegLocation rlResult;
1965 int funcOffset;
1966
1967 switch (mir->dalvikInsn.opcode) {
1968 case OP_ADD_FLOAT_2ADDR:
1969 case OP_ADD_FLOAT:
1970 funcOffset = OFFSETOF_MEMBER(Thread, pFadd);
1971 break;
1972 case OP_SUB_FLOAT_2ADDR:
1973 case OP_SUB_FLOAT:
1974 funcOffset = OFFSETOF_MEMBER(Thread, pFsub);
1975 break;
1976 case OP_DIV_FLOAT_2ADDR:
1977 case OP_DIV_FLOAT:
1978 funcOffset = OFFSETOF_MEMBER(Thread, pFdiv);
1979 break;
1980 case OP_MUL_FLOAT_2ADDR:
1981 case OP_MUL_FLOAT:
1982 funcOffset = OFFSETOF_MEMBER(Thread, pFmul);
1983 break;
1984 case OP_REM_FLOAT_2ADDR:
1985 case OP_REM_FLOAT:
1986 funcOffset = OFFSETOF_MEMBER(Thread, pFmodf);
1987 break;
1988 case OP_NEG_FLOAT: {
1989 genNegFloat(cUnit, rlDest, rlSrc1);
1990 return false;
1991 }
1992 default:
1993 return true;
1994 }
1995 oatFlushAllRegs(cUnit); /* Send everything to home location */
1996 int rTgt = loadHelper(cUnit, funcOffset);
1997 loadValueDirectFixed(cUnit, rlSrc1, rARG0);
1998 loadValueDirectFixed(cUnit, rlSrc2, rARG1);
1999 callRuntimeHelper(cUnit, rTgt);
2000 rlResult = oatGetReturn(cUnit);
2001 storeValue(cUnit, rlDest, rlResult);
2002 return false;
2003}
2004
2005void genNegDouble(CompilationUnit* cUnit, RegLocation rlDst, RegLocation rlSrc);
2006bool genArithOpDoublePortable(CompilationUnit* cUnit, MIR* mir,
2007 RegLocation rlDest, RegLocation rlSrc1,
2008 RegLocation rlSrc2)
2009{
2010 RegLocation rlResult;
2011 int funcOffset;
2012
2013 switch (mir->dalvikInsn.opcode) {
2014 case OP_ADD_DOUBLE_2ADDR:
2015 case OP_ADD_DOUBLE:
2016 funcOffset = OFFSETOF_MEMBER(Thread, pDadd);
2017 break;
2018 case OP_SUB_DOUBLE_2ADDR:
2019 case OP_SUB_DOUBLE:
2020 funcOffset = OFFSETOF_MEMBER(Thread, pDsub);
2021 break;
2022 case OP_DIV_DOUBLE_2ADDR:
2023 case OP_DIV_DOUBLE:
2024 funcOffset = OFFSETOF_MEMBER(Thread, pDdiv);
2025 break;
2026 case OP_MUL_DOUBLE_2ADDR:
2027 case OP_MUL_DOUBLE:
2028 funcOffset = OFFSETOF_MEMBER(Thread, pDmul);
2029 break;
2030 case OP_REM_DOUBLE_2ADDR:
2031 case OP_REM_DOUBLE:
2032 funcOffset = OFFSETOF_MEMBER(Thread, pFmod);
2033 break;
2034 case OP_NEG_DOUBLE: {
2035 genNegDouble(cUnit, rlDest, rlSrc1);
2036 return false;
2037 }
2038 default:
2039 return true;
2040 }
2041 oatFlushAllRegs(cUnit); /* Send everything to home location */
2042 int rTgt = loadHelper(cUnit, funcOffset);
2043 loadValueDirectWideFixed(cUnit, rlSrc1, rARG0, rARG1);
2044 loadValueDirectWideFixed(cUnit, rlSrc2, rARG2, rARG3);
2045 callRuntimeHelper(cUnit, rTgt);
2046 rlResult = oatGetReturnWide(cUnit);
2047 storeValueWide(cUnit, rlDest, rlResult);
2048 return false;
2049}
2050
2051bool genConversionPortable(CompilationUnit* cUnit, MIR* mir)
2052{
2053 Opcode opcode = mir->dalvikInsn.opcode;
2054
2055 switch (opcode) {
2056 case OP_INT_TO_FLOAT:
2057 return genConversionCall(cUnit, mir, OFFSETOF_MEMBER(Thread, pI2f),
2058 1, 1);
2059 case OP_FLOAT_TO_INT:
2060 return genConversionCall(cUnit, mir, OFFSETOF_MEMBER(Thread, pF2iz),
2061 1, 1);
2062 case OP_DOUBLE_TO_FLOAT:
2063 return genConversionCall(cUnit, mir, OFFSETOF_MEMBER(Thread, pD2f),
2064 2, 1);
2065 case OP_FLOAT_TO_DOUBLE:
2066 return genConversionCall(cUnit, mir, OFFSETOF_MEMBER(Thread, pF2d),
2067 1, 2);
2068 case OP_INT_TO_DOUBLE:
2069 return genConversionCall(cUnit, mir, OFFSETOF_MEMBER(Thread, pI2d),
2070 1, 2);
2071 case OP_DOUBLE_TO_INT:
2072 return genConversionCall(cUnit, mir, OFFSETOF_MEMBER(Thread, pD2iz),
2073 2, 1);
2074 case OP_FLOAT_TO_LONG:
2075 return genConversionCall(cUnit, mir, OFFSETOF_MEMBER(Thread,
2076 pF2l), 1, 2);
2077 case OP_LONG_TO_FLOAT:
2078 return genConversionCall(cUnit, mir, OFFSETOF_MEMBER(Thread, pL2f),
2079 2, 1);
2080 case OP_DOUBLE_TO_LONG:
2081 return genConversionCall(cUnit, mir, OFFSETOF_MEMBER(Thread,
2082 pD2l), 2, 2);
2083 case OP_LONG_TO_DOUBLE:
2084 return genConversionCall(cUnit, mir, OFFSETOF_MEMBER(Thread, pL2d),
2085 2, 2);
2086 default:
2087 return true;
2088 }
2089 return false;
2090}
2091
2092/*
2093 * Generate callout to updateDebugger. Note that we're overloading
2094 * the use of rSUSPEND here. When the debugger is active, this
2095 * register holds the address of the update function. So, if it's
2096 * non-null, we call out to it.
2097 *
2098 * Note also that rRET0 and rRET1 must be preserved across this
2099 * code. This must be handled by the stub.
2100 */
2101void genDebuggerUpdate(CompilationUnit* cUnit, int32_t offset)
2102{
2103 // Following DCHECK verifies that dPC is in range of single load immediate
2104 DCHECK((offset == DEBUGGER_METHOD_ENTRY) ||
2105 (offset == DEBUGGER_METHOD_EXIT) || ((offset & 0xffff) == offset));
2106 oatClobberCalleeSave(cUnit);
2107#if defined(TARGET_ARM)
2108 opRegImm(cUnit, kOpCmp, rSUSPEND, 0);
buzbee82488f52012-03-02 08:20:26 -08002109 opIT(cUnit, kArmCondNe, "T");
buzbee31a4a6f2012-02-28 15:36:15 -08002110 loadConstant(cUnit, rARG2, offset); // arg2 <- Entry code
2111 opReg(cUnit, kOpBlx, rSUSPEND);
2112#else
buzbee82488f52012-03-02 08:20:26 -08002113 LIR* branch = opCmpImmBranch(cUnit, kCondEq, rSUSPEND, 0, NULL);
buzbee31a4a6f2012-02-28 15:36:15 -08002114 loadConstant(cUnit, rARG2, offset);
2115 opReg(cUnit, kOpBlx, rSUSPEND);
2116 LIR* target = newLIR0(cUnit, kPseudoTargetLabel);
2117 target->defMask = ENCODE_ALL;
2118 branch->target = (LIR*)target;
2119#endif
2120 oatFreeTemp(cUnit, rARG2);
2121}
2122
2123/* Check if we need to check for pending suspend request */
2124void genSuspendTest(CompilationUnit* cUnit, MIR* mir)
2125{
2126 if (NO_SUSPEND || (mir->optimizationFlags & MIR_IGNORE_SUSPEND_CHECK)) {
2127 return;
2128 }
2129 oatFlushAllRegs(cUnit);
2130 LIR* branch;
2131 if (cUnit->genDebugger) {
2132 // If generating code for the debugger, always check for suspension
buzbee82488f52012-03-02 08:20:26 -08002133 branch = opUnconditionalBranch(cUnit, NULL);
buzbee31a4a6f2012-02-28 15:36:15 -08002134 } else {
2135#if defined(TARGET_ARM)
2136 // In non-debug case, only check periodically
2137 newLIR2(cUnit, kThumbSubRI8, rSUSPEND, 1);
buzbee82488f52012-03-02 08:20:26 -08002138 branch = opCondBranch(cUnit, kCondEq, NULL);
buzbee31a4a6f2012-02-28 15:36:15 -08002139#else
2140 opRegImm(cUnit, kOpSub, rSUSPEND, 1);
buzbee82488f52012-03-02 08:20:26 -08002141 branch = opCmpImmBranch(cUnit, kCondEq, rSUSPEND, 0, NULL);
buzbee31a4a6f2012-02-28 15:36:15 -08002142#endif
2143 }
2144 LIR* retLab = newLIR0(cUnit, kPseudoTargetLabel);
2145 retLab->defMask = ENCODE_ALL;
2146 LIR* target = (LIR*)oatNew(cUnit, sizeof(LIR), true, kAllocLIR);
2147 target->dalvikOffset = cUnit->currentDalvikOffset;
2148 target->opcode = kPseudoSuspendTarget;
2149 target->operands[0] = (intptr_t)retLab;
2150 target->operands[1] = mir->offset;
2151 branch->target = (LIR*)target;
2152 oatInsertGrowableList(cUnit, &cUnit->suspendLaunchpads, (intptr_t)target);
2153}
2154
2155} // namespace art