blob: 4d80a690a965ed6d827bc0e75957ff8dffa073cf [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{
buzbee0398c422012-03-02 15:22:47 -0800587 LIR* branchOver = opCmpImmBranch(cUnit, kCondNe, rINVOKE_TGT, 0, NULL);
buzbee31a4a6f2012-02-28 15:36:15 -0800588 loadWordDisp(cUnit, rSELF,
buzbee0398c422012-03-02 15:22:47 -0800589 OFFSETOF_MEMBER(Thread, pDebugMe), rINVOKE_TGT);
buzbee31a4a6f2012-02-28 15:36:15 -0800590 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);
buzbee0398c422012-03-02 15:22:47 -0800706 oatFreeTemp(cUnit, rTgt);
buzbee31a4a6f2012-02-28 15:36:15 -0800707 }
708}
709
710/* Needed by the Assembler */
711void oatSetupResourceMasks(LIR* lir)
712{
713 setupResourceMasks(lir);
714}
715
716void genIGet(CompilationUnit* cUnit, MIR* mir, OpSize size,
717 RegLocation rlDest, RegLocation rlObj,
718 bool isLongOrDouble, bool isObject)
719{
720 int fieldOffset;
721 bool isVolatile;
722 uint32_t fieldIdx = mir->dalvikInsn.vC;
723
724 OatCompilationUnit mUnit(cUnit->class_loader, cUnit->class_linker,
725 *cUnit->dex_file, *cUnit->dex_cache,
726 cUnit->code_item, cUnit->method_idx,
727 cUnit->access_flags);
728
729 bool fastPath = cUnit->compiler->ComputeInstanceFieldInfo(fieldIdx, &mUnit,
730 fieldOffset, isVolatile, false);
731
732 if (fastPath && !SLOW_FIELD_PATH) {
733 RegLocation rlResult;
734 RegisterClass regClass = oatRegClassBySize(size);
735 DCHECK_GE(fieldOffset, 0);
736 rlObj = loadValue(cUnit, rlObj, kCoreReg);
737 if (isLongOrDouble) {
738 DCHECK(rlDest.wide);
739 genNullCheck(cUnit, rlObj.sRegLow, rlObj.lowReg, mir);/* null? */
740 int regPtr = oatAllocTemp(cUnit);
741 opRegRegImm(cUnit, kOpAdd, regPtr, rlObj.lowReg, fieldOffset);
742 rlResult = oatEvalLoc(cUnit, rlDest, regClass, true);
743 loadPair(cUnit, regPtr, rlResult.lowReg, rlResult.highReg);
744 if (isVolatile) {
745 oatGenMemBarrier(cUnit, kSY);
746 }
747 oatFreeTemp(cUnit, regPtr);
748 storeValueWide(cUnit, rlDest, rlResult);
749 } else {
750 rlResult = oatEvalLoc(cUnit, rlDest, regClass, true);
751 genNullCheck(cUnit, rlObj.sRegLow, rlObj.lowReg, mir);/* null? */
752 loadBaseDisp(cUnit, mir, rlObj.lowReg, fieldOffset, rlResult.lowReg,
753 kWord, rlObj.sRegLow);
754 if (isVolatile) {
755 oatGenMemBarrier(cUnit, kSY);
756 }
757 storeValue(cUnit, rlDest, rlResult);
758 }
759 } else {
760 int getterOffset = isLongOrDouble ? OFFSETOF_MEMBER(Thread, pGet64Instance) :
761 (isObject ? OFFSETOF_MEMBER(Thread, pGetObjInstance)
762 : OFFSETOF_MEMBER(Thread, pGet32Instance));
763 int rTgt = loadHelper(cUnit, getterOffset);
764 loadValueDirect(cUnit, rlObj, rARG1);
765 loadConstant(cUnit, rARG0, fieldIdx);
766 callRuntimeHelper(cUnit, rTgt);
767 if (isLongOrDouble) {
768 RegLocation rlResult = oatGetReturnWide(cUnit);
769 storeValueWide(cUnit, rlDest, rlResult);
770 } else {
771 RegLocation rlResult = oatGetReturn(cUnit);
772 storeValue(cUnit, rlDest, rlResult);
773 }
774 }
775}
776
777void genIPut(CompilationUnit* cUnit, MIR* mir, OpSize size, RegLocation rlSrc,
778 RegLocation rlObj, bool isLongOrDouble, bool isObject)
779{
780 int fieldOffset;
781 bool isVolatile;
782 uint32_t fieldIdx = mir->dalvikInsn.vC;
783
784 OatCompilationUnit mUnit(cUnit->class_loader, cUnit->class_linker,
785 *cUnit->dex_file, *cUnit->dex_cache,
786 cUnit->code_item, cUnit->method_idx,
787 cUnit->access_flags);
788
789 bool fastPath = cUnit->compiler->ComputeInstanceFieldInfo(fieldIdx, &mUnit,
790 fieldOffset, isVolatile, true);
791 if (fastPath && !SLOW_FIELD_PATH) {
792 RegisterClass regClass = oatRegClassBySize(size);
793 DCHECK_GE(fieldOffset, 0);
794 rlObj = loadValue(cUnit, rlObj, kCoreReg);
795 if (isLongOrDouble) {
796 int regPtr;
797 rlSrc = loadValueWide(cUnit, rlSrc, kAnyReg);
798 genNullCheck(cUnit, rlObj.sRegLow, rlObj.lowReg, mir);/* null? */
799 regPtr = oatAllocTemp(cUnit);
800 opRegRegImm(cUnit, kOpAdd, regPtr, rlObj.lowReg, fieldOffset);
801 if (isVolatile) {
802 oatGenMemBarrier(cUnit, kST);
803 }
804 storePair(cUnit, regPtr, rlSrc.lowReg, rlSrc.highReg);
805 if (isVolatile) {
806 oatGenMemBarrier(cUnit, kSY);
807 }
808 oatFreeTemp(cUnit, regPtr);
809 } else {
810 rlSrc = loadValue(cUnit, rlSrc, regClass);
811 genNullCheck(cUnit, rlObj.sRegLow, rlObj.lowReg, mir);/* null? */
812 if (isVolatile) {
813 oatGenMemBarrier(cUnit, kST);
814 }
815 storeBaseDisp(cUnit, rlObj.lowReg, fieldOffset, rlSrc.lowReg, kWord);
816 if (isVolatile) {
817 oatGenMemBarrier(cUnit, kSY);
818 }
819 }
820 } else {
821 int setterOffset = isLongOrDouble ? OFFSETOF_MEMBER(Thread, pSet64Instance) :
822 (isObject ? OFFSETOF_MEMBER(Thread, pSetObjInstance)
823 : OFFSETOF_MEMBER(Thread, pSet32Instance));
824 int rTgt = loadHelper(cUnit, setterOffset);
825 loadValueDirect(cUnit, rlObj, rARG1);
826 if (isLongOrDouble) {
827 loadValueDirectWide(cUnit, rlSrc, rARG2, rARG3);
828 } else {
829 loadValueDirect(cUnit, rlSrc, rARG2);
830 }
831 loadConstant(cUnit, rARG0, fieldIdx);
832 callRuntimeHelper(cUnit, rTgt);
833 }
834}
835
836void genConstClass(CompilationUnit* cUnit, MIR* mir, RegLocation rlDest,
837 RegLocation rlSrc)
838{
839 uint32_t type_idx = mir->dalvikInsn.vB;
840 int mReg = loadCurrMethod(cUnit);
841 int resReg = oatAllocTemp(cUnit);
842 RegLocation rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
843 if (!cUnit->compiler->CanAccessTypeWithoutChecks(cUnit->method_idx,
844 cUnit->dex_cache,
845 *cUnit->dex_file,
846 type_idx)) {
847 // Call out to helper which resolves type and verifies access.
848 // Resolved type returned in rRET0.
849 int rTgt = loadHelper(cUnit, OFFSETOF_MEMBER(Thread,
850 pInitializeTypeAndVerifyAccessFromCode));
buzbee82488f52012-03-02 08:20:26 -0800851 opRegCopy(cUnit, rARG1, mReg);
buzbee31a4a6f2012-02-28 15:36:15 -0800852 loadConstant(cUnit, rARG0, type_idx);
853 callRuntimeHelper(cUnit, rTgt);
854 RegLocation rlResult = oatGetReturn(cUnit);
855 storeValue(cUnit, rlDest, rlResult);
856 } else {
857 // We're don't need access checks, load type from dex cache
858 int32_t dex_cache_offset =
859 Method::DexCacheResolvedTypesOffset().Int32Value();
860 loadWordDisp(cUnit, mReg, dex_cache_offset, resReg);
861 int32_t offset_of_type =
862 Array::DataOffset(sizeof(Class*)).Int32Value() + (sizeof(Class*)
863 * type_idx);
864 loadWordDisp(cUnit, resReg, offset_of_type, rlResult.lowReg);
865 if (!cUnit->compiler->CanAssumeTypeIsPresentInDexCache(cUnit->dex_cache,
866 type_idx) || SLOW_TYPE_PATH) {
867 // Slow path, at runtime test if type is null and if so initialize
868 oatFlushAllRegs(cUnit);
buzbee82488f52012-03-02 08:20:26 -0800869 LIR* branch1 = opCmpImmBranch(cUnit, kCondEq, rlResult.lowReg, 0,
870 NULL);
buzbee31a4a6f2012-02-28 15:36:15 -0800871 // Resolved, store and hop over following code
872 storeValue(cUnit, rlDest, rlResult);
buzbee82488f52012-03-02 08:20:26 -0800873 LIR* branch2 = opUnconditionalBranch(cUnit,0);
buzbee31a4a6f2012-02-28 15:36:15 -0800874 // TUNING: move slow path to end & remove unconditional branch
875 LIR* target1 = newLIR0(cUnit, kPseudoTargetLabel);
876 target1->defMask = ENCODE_ALL;
buzbee5de34942012-03-01 14:51:57 -0800877 // Call out to helper, which will return resolved type in rARG0
buzbee31a4a6f2012-02-28 15:36:15 -0800878 int rTgt = loadHelper(cUnit, OFFSETOF_MEMBER(Thread,
879 pInitializeTypeFromCode));
buzbee82488f52012-03-02 08:20:26 -0800880 opRegCopy(cUnit, rARG1, mReg);
buzbee31a4a6f2012-02-28 15:36:15 -0800881 loadConstant(cUnit, rARG0, type_idx);
882 callRuntimeHelper(cUnit, rTgt);
883 RegLocation rlResult = oatGetReturn(cUnit);
884 storeValue(cUnit, rlDest, rlResult);
885 // Rejoin code paths
886 LIR* target2 = newLIR0(cUnit, kPseudoTargetLabel);
887 target2->defMask = ENCODE_ALL;
888 branch1->target = (LIR*)target1;
889 branch2->target = (LIR*)target2;
890 } else {
891 // Fast path, we're done - just store result
892 storeValue(cUnit, rlDest, rlResult);
893 }
894 }
895}
896void genConstString(CompilationUnit* cUnit, MIR* mir, RegLocation rlDest,
897 RegLocation rlSrc)
898{
899 /* NOTE: Most strings should be available at compile time */
900 uint32_t string_idx = mir->dalvikInsn.vB;
901 int32_t offset_of_string = Array::DataOffset(sizeof(String*)).Int32Value() +
902 (sizeof(String*) * string_idx);
903 if (!cUnit->compiler->CanAssumeStringIsPresentInDexCache(
904 cUnit->dex_cache, string_idx) || SLOW_STRING_PATH) {
905 // slow path, resolve string if not in dex cache
906 oatFlushAllRegs(cUnit);
907 oatLockCallTemps(cUnit); // Using explicit registers
908 loadCurrMethodDirect(cUnit, rARG2);
909 loadWordDisp(cUnit, rARG2,
910 Method::DexCacheStringsOffset().Int32Value(), rARG0);
911 // Might call out to helper, which will return resolved string in rRET0
912 int rTgt = loadHelper(cUnit, OFFSETOF_MEMBER(Thread,
913 pResolveStringFromCode));
914 loadWordDisp(cUnit, rRET0, offset_of_string, rARG0);
915 loadConstant(cUnit, rARG1, string_idx);
916#if defined(TARGET_ARM)
917 opRegImm(cUnit, kOpCmp, rRET0, 0); // Is resolved?
918 genBarrier(cUnit);
919 // For testing, always force through helper
920 if (!EXERCISE_SLOWEST_STRING_PATH) {
buzbee82488f52012-03-02 08:20:26 -0800921 opIT(cUnit, kArmCondEq, "T");
buzbee31a4a6f2012-02-28 15:36:15 -0800922 }
buzbee82488f52012-03-02 08:20:26 -0800923 opRegCopy(cUnit, rARG0, rARG2); // .eq
buzbee31a4a6f2012-02-28 15:36:15 -0800924 opReg(cUnit, kOpBlx, rTgt); // .eq, helper(Method*, string_idx)
925#else
buzbee82488f52012-03-02 08:20:26 -0800926 LIR* branch = opCmpImmBranch(cUnit, kCondNe, rRET0, 0, NULL);
927 opRegCopy(cUnit, rARG0, rARG2); // .eq
buzbee31a4a6f2012-02-28 15:36:15 -0800928 opReg(cUnit, kOpBlx, rTgt);
929 LIR* target = newLIR0(cUnit, kPseudoTargetLabel);
930 target->defMask = ENCODE_ALL;
931 branch->target = target;
932#endif
933 genBarrier(cUnit);
934 storeValue(cUnit, rlDest, getRetLoc(cUnit));
935 } else {
936 int mReg = loadCurrMethod(cUnit);
937 int resReg = oatAllocTemp(cUnit);
938 RegLocation rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
939 loadWordDisp(cUnit, mReg,
940 Method::DexCacheStringsOffset().Int32Value(), resReg);
941 loadWordDisp(cUnit, resReg, offset_of_string, rlResult.lowReg);
942 storeValue(cUnit, rlDest, rlResult);
943 }
944}
945
946/*
947 * Let helper function take care of everything. Will
948 * call Class::NewInstanceFromCode(type_idx, method);
949 */
950void genNewInstance(CompilationUnit* cUnit, MIR* mir, RegLocation rlDest)
951{
952 oatFlushAllRegs(cUnit); /* Everything to home location */
953 uint32_t type_idx = mir->dalvikInsn.vB;
954 // alloc will always check for resolution, do we also need to verify
955 // access because the verifier was unable to?
956 int rTgt;
957 if (cUnit->compiler->CanAccessInstantiableTypeWithoutChecks(
958 cUnit->method_idx, cUnit->dex_cache, *cUnit->dex_file, type_idx)) {
959 rTgt = loadHelper(cUnit, OFFSETOF_MEMBER(Thread, pAllocObjectFromCode));
960 } else {
961 rTgt = loadHelper(cUnit, OFFSETOF_MEMBER(Thread,
962 pAllocObjectFromCodeWithAccessCheck));
963 }
964 loadCurrMethodDirect(cUnit, rARG1); // arg1 <= Method*
965 loadConstant(cUnit, rARG0, type_idx); // arg0 <- type_idx
966 callRuntimeHelper(cUnit, rTgt);
967 RegLocation rlResult = oatGetReturn(cUnit);
968 storeValue(cUnit, rlDest, rlResult);
969}
970
971void genInstanceof(CompilationUnit* cUnit, MIR* mir, RegLocation rlDest,
972 RegLocation rlSrc)
973{
974 oatFlushAllRegs(cUnit);
975 // May generate a call - use explicit registers
976 oatLockCallTemps(cUnit);
977 uint32_t type_idx = mir->dalvikInsn.vC;
buzbee5de34942012-03-01 14:51:57 -0800978 loadCurrMethodDirect(cUnit, rARG1); // rARG1 <= current Method*
buzbee31a4a6f2012-02-28 15:36:15 -0800979 int classReg = rARG2; // rARG2 will hold the Class*
980 if (!cUnit->compiler->CanAccessTypeWithoutChecks(cUnit->method_idx,
981 cUnit->dex_cache,
982 *cUnit->dex_file,
983 type_idx)) {
984 // Check we have access to type_idx and if not throw IllegalAccessError,
buzbee5de34942012-03-01 14:51:57 -0800985 // returns Class* in rARG0
buzbee31a4a6f2012-02-28 15:36:15 -0800986 int rTgt = loadHelper(cUnit, OFFSETOF_MEMBER(Thread,
987 pInitializeTypeAndVerifyAccessFromCode));
988 loadConstant(cUnit, rARG0, type_idx);
989 callRuntimeHelper(cUnit, rTgt); // InitializeTypeAndVerifyAccess(idx, method)
buzbee82488f52012-03-02 08:20:26 -0800990 opRegCopy(cUnit, classReg, rRET0); // Align usage with fast path
buzbee5de34942012-03-01 14:51:57 -0800991 loadValueDirectFixed(cUnit, rlSrc, rARG0); // rARG0 <= ref
buzbee31a4a6f2012-02-28 15:36:15 -0800992 } else {
buzbee5de34942012-03-01 14:51:57 -0800993 // Load dex cache entry into classReg (rARG2)
buzbee31a4a6f2012-02-28 15:36:15 -0800994 loadValueDirectFixed(cUnit, rlSrc, rARG0); // rARG0 <= ref
995 loadWordDisp(cUnit, rARG1,
996 Method::DexCacheResolvedTypesOffset().Int32Value(),
997 classReg);
998 int32_t offset_of_type =
999 Array::DataOffset(sizeof(Class*)).Int32Value() + (sizeof(Class*)
1000 * type_idx);
1001 loadWordDisp(cUnit, classReg, offset_of_type, classReg);
1002 if (!cUnit->compiler->CanAssumeTypeIsPresentInDexCache(
1003 cUnit->dex_cache, type_idx)) {
1004 // Need to test presence of type in dex cache at runtime
buzbee82488f52012-03-02 08:20:26 -08001005 LIR* hopBranch = opCmpImmBranch(cUnit, kCondNe, classReg, 0, NULL);
buzbee31a4a6f2012-02-28 15:36:15 -08001006 // Not resolved
1007 // Call out to helper, which will return resolved type in rRET0
1008 int rTgt = loadHelper(cUnit, OFFSETOF_MEMBER(Thread,
1009 pInitializeTypeFromCode));
1010 loadConstant(cUnit, rARG0, type_idx);
1011 callRuntimeHelper(cUnit, rTgt); // InitializeTypeFromCode(idx, method)
buzbee82488f52012-03-02 08:20:26 -08001012 opRegCopy(cUnit, rARG2, rRET0); // Align usage with fast path
buzbee31a4a6f2012-02-28 15:36:15 -08001013 loadValueDirectFixed(cUnit, rlSrc, rARG0); /* reload Ref */
1014 // Rejoin code paths
1015 LIR* hopTarget = newLIR0(cUnit, kPseudoTargetLabel);
1016 hopTarget->defMask = ENCODE_ALL;
1017 hopBranch->target = (LIR*)hopTarget;
1018 }
1019 }
1020 /* rARG0 is ref, rARG2 is class. If ref==null, use directly as bool result */
buzbee82488f52012-03-02 08:20:26 -08001021 LIR* branch1 = opCmpImmBranch(cUnit, kCondEq, rARG0, 0, NULL);
buzbee31a4a6f2012-02-28 15:36:15 -08001022 /* load object->clazz */
1023 DCHECK_EQ(Object::ClassOffset().Int32Value(), 0);
1024 loadWordDisp(cUnit, rARG0, Object::ClassOffset().Int32Value(), rARG1);
1025 /* rARG0 is ref, rARG1 is ref->clazz, rARG2 is class */
buzbee0398c422012-03-02 15:22:47 -08001026#if defined(TARGET_ARM)
1027 /* Uses conditional nullification */
buzbee31a4a6f2012-02-28 15:36:15 -08001028 int rTgt = loadHelper(cUnit, OFFSETOF_MEMBER(Thread,
1029 pInstanceofNonTrivialFromCode));
buzbee31a4a6f2012-02-28 15:36:15 -08001030 opRegReg(cUnit, kOpCmp, rARG1, rARG2); // Same?
buzbee82488f52012-03-02 08:20:26 -08001031 opIT(cUnit, kArmCondEq, "EE"); // if-convert the test
buzbee31a4a6f2012-02-28 15:36:15 -08001032 loadConstant(cUnit, rARG0, 1); // .eq case - load true
buzbee82488f52012-03-02 08:20:26 -08001033 opRegCopy(cUnit, rARG0, rARG2); // .ne case - arg0 <= class
buzbee31a4a6f2012-02-28 15:36:15 -08001034 opReg(cUnit, kOpBlx, rTgt); // .ne case: helper(class, ref->class)
buzbee31a4a6f2012-02-28 15:36:15 -08001035#else
buzbee0398c422012-03-02 15:22:47 -08001036 /* Uses branchovers */
1037 loadConstant(cUnit, rARG0, 1); // assume true
1038 LIR* branchover = opCmpBranch(cUnit, kCondEq, rARG1, rARG2, NULL);
1039 int rTgt = loadHelper(cUnit, OFFSETOF_MEMBER(Thread,
1040 pInstanceofNonTrivialFromCode));
1041 opRegCopy(cUnit, rARG0, rARG2); // .ne case - arg0 <= class
1042 opReg(cUnit, kOpBlx, rTgt); // .ne case: helper(class, ref->class)
buzbee31a4a6f2012-02-28 15:36:15 -08001043#endif
buzbee0398c422012-03-02 15:22:47 -08001044 oatClobberCalleeSave(cUnit);
1045 /* branch targets here */
buzbee31a4a6f2012-02-28 15:36:15 -08001046 LIR* target = newLIR0(cUnit, kPseudoTargetLabel);
1047 target->defMask = ENCODE_ALL;
1048 RegLocation rlResult = oatGetReturn(cUnit);
1049 storeValue(cUnit, rlDest, rlResult);
buzbee0398c422012-03-02 15:22:47 -08001050 branch1->target = target;
1051#if !defined(TARGET_ARM)
1052 branchover->target = target;
1053#endif
buzbee31a4a6f2012-02-28 15:36:15 -08001054}
1055
1056void genCheckCast(CompilationUnit* cUnit, MIR* mir, RegLocation rlSrc)
1057{
1058 oatFlushAllRegs(cUnit);
1059 // May generate a call - use explicit registers
1060 oatLockCallTemps(cUnit);
1061 uint32_t type_idx = mir->dalvikInsn.vB;
1062 loadCurrMethodDirect(cUnit, rARG1); // rARG1 <= current Method*
1063 int classReg = rARG2; // rARG2 will hold the Class*
1064 if (!cUnit->compiler->CanAccessTypeWithoutChecks(cUnit->method_idx,
1065 cUnit->dex_cache,
1066 *cUnit->dex_file,
1067 type_idx)) {
1068 // Check we have access to type_idx and if not throw IllegalAccessError,
1069 // returns Class* in rRET0
1070 int rTgt = loadHelper(cUnit, OFFSETOF_MEMBER(Thread,
1071 pInitializeTypeAndVerifyAccessFromCode));
1072 loadConstant(cUnit, rARG0, type_idx);
1073 callRuntimeHelper(cUnit, rTgt); // InitializeTypeAndVerifyAccess(idx, method)
buzbee82488f52012-03-02 08:20:26 -08001074 opRegCopy(cUnit, classReg, rRET0); // Align usage with fast path
buzbee31a4a6f2012-02-28 15:36:15 -08001075 } else {
1076 // Load dex cache entry into classReg (rARG2)
1077 loadWordDisp(cUnit, rARG1,
1078 Method::DexCacheResolvedTypesOffset().Int32Value(),
1079 classReg);
1080 int32_t offset_of_type =
1081 Array::DataOffset(sizeof(Class*)).Int32Value() +
1082 (sizeof(Class*) * type_idx);
1083 loadWordDisp(cUnit, classReg, offset_of_type, classReg);
1084 if (!cUnit->compiler->CanAssumeTypeIsPresentInDexCache(
1085 cUnit->dex_cache, type_idx)) {
1086 // Need to test presence of type in dex cache at runtime
buzbee82488f52012-03-02 08:20:26 -08001087 LIR* hopBranch = opCmpImmBranch(cUnit, kCondNe, classReg, 0, NULL);
buzbee31a4a6f2012-02-28 15:36:15 -08001088 // Not resolved
buzbee5de34942012-03-01 14:51:57 -08001089 // Call out to helper, which will return resolved type in rARG0
1090 int rTgt = loadHelper(cUnit, OFFSETOF_MEMBER(Thread, pInitializeTypeFromCode));
1091 loadConstant(cUnit, rARG0, type_idx);
1092 callRuntimeHelper(cUnit, rTgt); // InitializeTypeFromCode(idx, method)
buzbee82488f52012-03-02 08:20:26 -08001093 opRegCopy(cUnit, classReg, rARG0); // Align usage with fast path
buzbee31a4a6f2012-02-28 15:36:15 -08001094 // Rejoin code paths
1095 LIR* hopTarget = newLIR0(cUnit, kPseudoTargetLabel);
1096 hopTarget->defMask = ENCODE_ALL;
1097 hopBranch->target = (LIR*)hopTarget;
1098 }
1099 }
buzbee5de34942012-03-01 14:51:57 -08001100 // At this point, classReg (rARG2) has class
buzbee31a4a6f2012-02-28 15:36:15 -08001101 loadValueDirectFixed(cUnit, rlSrc, rARG0); // rARG0 <= ref
1102 /* Null is OK - continue */
buzbee82488f52012-03-02 08:20:26 -08001103 LIR* branch1 = opCmpImmBranch(cUnit, kCondEq, rARG0, 0, NULL);
buzbee31a4a6f2012-02-28 15:36:15 -08001104 /* load object->clazz */
1105 DCHECK_EQ(Object::ClassOffset().Int32Value(), 0);
1106 loadWordDisp(cUnit, rARG0, Object::ClassOffset().Int32Value(), rARG1);
1107 /* rARG1 now contains object->clazz */
1108 int rTgt = loadHelper(cUnit, OFFSETOF_MEMBER(Thread,
1109 pCheckCastFromCode));
buzbee5de34942012-03-01 14:51:57 -08001110#if defined(TARGET_MIPS)
buzbee82488f52012-03-02 08:20:26 -08001111 LIR* branch2 = opCmpBranch(cUnit, kCondEq, rARG1, classReg, NULL);
buzbee5de34942012-03-01 14:51:57 -08001112#else
buzbee31a4a6f2012-02-28 15:36:15 -08001113 opRegReg(cUnit, kOpCmp, rARG1, classReg);
buzbee82488f52012-03-02 08:20:26 -08001114 LIR* branch2 = opCondBranch(cUnit, kCondEq, NULL); /* If eq, trivial yes */
buzbee5de34942012-03-01 14:51:57 -08001115#endif
buzbee82488f52012-03-02 08:20:26 -08001116 opRegCopy(cUnit, rARG0, rARG1);
1117 opRegCopy(cUnit, rARG1, rARG2);
buzbee31a4a6f2012-02-28 15:36:15 -08001118 callRuntimeHelper(cUnit, rTgt);
1119 /* branch target here */
1120 LIR* target = newLIR0(cUnit, kPseudoTargetLabel);
1121 target->defMask = ENCODE_ALL;
1122 branch1->target = (LIR*)target;
1123 branch2->target = (LIR*)target;
1124}
1125
1126
1127void genThrow(CompilationUnit* cUnit, MIR* mir, RegLocation rlSrc)
1128{
1129 oatFlushAllRegs(cUnit);
1130 int rTgt = loadHelper(cUnit, OFFSETOF_MEMBER(Thread, pDeliverException));
1131 loadValueDirectFixed(cUnit, rlSrc, rARG0); // Get exception object
1132 callRuntimeHelper(cUnit, rTgt); // art_deliver_exception(exception);
1133}
1134
1135/*
1136 * Generate array store
1137 *
1138 */
1139void genArrayObjPut(CompilationUnit* cUnit, MIR* mir, RegLocation rlArray,
1140 RegLocation rlIndex, RegLocation rlSrc, int scale)
1141{
1142 RegisterClass regClass = oatRegClassBySize(kWord);
1143 int lenOffset = Array::LengthOffset().Int32Value();
1144 int dataOffset = Array::DataOffset(sizeof(Object*)).Int32Value();
1145
1146 oatFlushAllRegs(cUnit);
1147 /* Make sure it's a legal object Put. Use direct regs at first */
1148 loadValueDirectFixed(cUnit, rlArray, rARG1);
1149 loadValueDirectFixed(cUnit, rlSrc, rARG0);
1150
1151 /* null array object? */
1152 genNullCheck(cUnit, rlArray.sRegLow, rARG1, mir);
1153 int rTgt = loadHelper(cUnit, OFFSETOF_MEMBER(Thread,
1154 pCanPutArrayElementFromCode));
1155 /* Get the array's clazz */
1156 loadWordDisp(cUnit, rARG1, Object::ClassOffset().Int32Value(), rARG1);
1157 callRuntimeHelper(cUnit, rTgt);
1158 oatFreeTemp(cUnit, rARG0);
1159 oatFreeTemp(cUnit, rARG1);
1160
1161 // Now, redo loadValues in case they didn't survive the call
1162
1163 int regPtr;
1164 rlArray = loadValue(cUnit, rlArray, kCoreReg);
1165 rlIndex = loadValue(cUnit, rlIndex, kCoreReg);
1166
1167 if (oatIsTemp(cUnit, rlArray.lowReg)) {
1168 oatClobber(cUnit, rlArray.lowReg);
1169 regPtr = rlArray.lowReg;
1170 } else {
1171 regPtr = oatAllocTemp(cUnit);
buzbee82488f52012-03-02 08:20:26 -08001172 opRegCopy(cUnit, regPtr, rlArray.lowReg);
buzbee31a4a6f2012-02-28 15:36:15 -08001173 }
1174
1175 if (!(mir->optimizationFlags & MIR_IGNORE_RANGE_CHECK)) {
1176 int regLen = oatAllocTemp(cUnit);
1177 //NOTE: max live temps(4) here.
1178 /* Get len */
1179 loadWordDisp(cUnit, rlArray.lowReg, lenOffset, regLen);
1180 /* regPtr -> array data */
1181 opRegImm(cUnit, kOpAdd, regPtr, dataOffset);
1182 genRegRegCheck(cUnit, kCondCs, rlIndex.lowReg, regLen, mir,
1183 kThrowArrayBounds);
1184 oatFreeTemp(cUnit, regLen);
1185 } else {
1186 /* regPtr -> array data */
1187 opRegImm(cUnit, kOpAdd, regPtr, dataOffset);
1188 }
1189 /* at this point, regPtr points to array, 2 live temps */
1190 rlSrc = loadValue(cUnit, rlSrc, regClass);
1191 storeBaseIndexed(cUnit, regPtr, rlIndex.lowReg, rlSrc.lowReg,
1192 scale, kWord);
1193}
1194
1195/*
1196 * Generate array load
1197 */
1198void genArrayGet(CompilationUnit* cUnit, MIR* mir, OpSize size,
1199 RegLocation rlArray, RegLocation rlIndex,
1200 RegLocation rlDest, int scale)
1201{
1202 RegisterClass regClass = oatRegClassBySize(size);
1203 int lenOffset = Array::LengthOffset().Int32Value();
1204 int dataOffset;
1205 RegLocation rlResult;
1206 rlArray = loadValue(cUnit, rlArray, kCoreReg);
1207 rlIndex = loadValue(cUnit, rlIndex, kCoreReg);
1208 int regPtr;
1209
1210 if (size == kLong || size == kDouble) {
1211 dataOffset = Array::DataOffset(sizeof(int64_t)).Int32Value();
1212 } else {
1213 dataOffset = Array::DataOffset(sizeof(int32_t)).Int32Value();
1214 }
1215
1216 /* null object? */
1217 genNullCheck(cUnit, rlArray.sRegLow, rlArray.lowReg, mir);
1218
1219 regPtr = oatAllocTemp(cUnit);
1220
1221 if (!(mir->optimizationFlags & MIR_IGNORE_RANGE_CHECK)) {
1222 int regLen = oatAllocTemp(cUnit);
1223 /* Get len */
1224 loadWordDisp(cUnit, rlArray.lowReg, lenOffset, regLen);
1225 /* regPtr -> array data */
1226 opRegRegImm(cUnit, kOpAdd, regPtr, rlArray.lowReg, dataOffset);
1227 genRegRegCheck(cUnit, kCondCs, rlIndex.lowReg, regLen, mir,
1228 kThrowArrayBounds);
1229 oatFreeTemp(cUnit, regLen);
1230 } else {
1231 /* regPtr -> array data */
1232 opRegRegImm(cUnit, kOpAdd, regPtr, rlArray.lowReg, dataOffset);
1233 }
1234 oatFreeTemp(cUnit, rlArray.lowReg);
1235 if ((size == kLong) || (size == kDouble)) {
1236 if (scale) {
1237 int rNewIndex = oatAllocTemp(cUnit);
1238 opRegRegImm(cUnit, kOpLsl, rNewIndex, rlIndex.lowReg, scale);
1239 opRegReg(cUnit, kOpAdd, regPtr, rNewIndex);
1240 oatFreeTemp(cUnit, rNewIndex);
1241 } else {
1242 opRegReg(cUnit, kOpAdd, regPtr, rlIndex.lowReg);
1243 }
1244 oatFreeTemp(cUnit, rlIndex.lowReg);
1245 rlResult = oatEvalLoc(cUnit, rlDest, regClass, true);
1246
1247 loadPair(cUnit, regPtr, rlResult.lowReg, rlResult.highReg);
1248
1249 oatFreeTemp(cUnit, regPtr);
1250 storeValueWide(cUnit, rlDest, rlResult);
1251 } else {
1252 rlResult = oatEvalLoc(cUnit, rlDest, regClass, true);
1253
1254 loadBaseIndexed(cUnit, regPtr, rlIndex.lowReg, rlResult.lowReg,
1255 scale, size);
1256
1257 oatFreeTemp(cUnit, regPtr);
1258 storeValue(cUnit, rlDest, rlResult);
1259 }
1260}
1261
1262/*
1263 * Generate array store
1264 *
1265 */
1266void genArrayPut(CompilationUnit* cUnit, MIR* mir, OpSize size,
1267 RegLocation rlArray, RegLocation rlIndex,
1268 RegLocation rlSrc, int scale)
1269{
1270 RegisterClass regClass = oatRegClassBySize(size);
1271 int lenOffset = Array::LengthOffset().Int32Value();
1272 int dataOffset;
1273
1274 if (size == kLong || size == kDouble) {
1275 dataOffset = Array::DataOffset(sizeof(int64_t)).Int32Value();
1276 } else {
1277 dataOffset = Array::DataOffset(sizeof(int32_t)).Int32Value();
1278 }
1279
1280 int regPtr;
1281 rlArray = loadValue(cUnit, rlArray, kCoreReg);
1282 rlIndex = loadValue(cUnit, rlIndex, kCoreReg);
1283
1284 if (oatIsTemp(cUnit, rlArray.lowReg)) {
1285 oatClobber(cUnit, rlArray.lowReg);
1286 regPtr = rlArray.lowReg;
1287 } else {
1288 regPtr = oatAllocTemp(cUnit);
buzbee82488f52012-03-02 08:20:26 -08001289 opRegCopy(cUnit, regPtr, rlArray.lowReg);
buzbee31a4a6f2012-02-28 15:36:15 -08001290 }
1291
1292 /* null object? */
1293 genNullCheck(cUnit, rlArray.sRegLow, rlArray.lowReg, mir);
1294
1295 if (!(mir->optimizationFlags & MIR_IGNORE_RANGE_CHECK)) {
1296 int regLen = oatAllocTemp(cUnit);
1297 //NOTE: max live temps(4) here.
1298 /* Get len */
1299 loadWordDisp(cUnit, rlArray.lowReg, lenOffset, regLen);
1300 /* regPtr -> array data */
1301 opRegImm(cUnit, kOpAdd, regPtr, dataOffset);
1302 genRegRegCheck(cUnit, kCondCs, rlIndex.lowReg, regLen, mir,
1303 kThrowArrayBounds);
1304 oatFreeTemp(cUnit, regLen);
1305 } else {
1306 /* regPtr -> array data */
1307 opRegImm(cUnit, kOpAdd, regPtr, dataOffset);
1308 }
1309 /* at this point, regPtr points to array, 2 live temps */
1310 if ((size == kLong) || (size == kDouble)) {
1311 //TUNING: specific wide routine that can handle fp regs
1312 if (scale) {
1313 int rNewIndex = oatAllocTemp(cUnit);
1314 opRegRegImm(cUnit, kOpLsl, rNewIndex, rlIndex.lowReg, scale);
1315 opRegReg(cUnit, kOpAdd, regPtr, rNewIndex);
1316 oatFreeTemp(cUnit, rNewIndex);
1317 } else {
1318 opRegReg(cUnit, kOpAdd, regPtr, rlIndex.lowReg);
1319 }
1320 rlSrc = loadValueWide(cUnit, rlSrc, regClass);
1321
1322 storePair(cUnit, regPtr, rlSrc.lowReg, rlSrc.highReg);
1323
1324 oatFreeTemp(cUnit, regPtr);
1325 } else {
1326 rlSrc = loadValue(cUnit, rlSrc, regClass);
1327
1328 storeBaseIndexed(cUnit, regPtr, rlIndex.lowReg, rlSrc.lowReg,
1329 scale, size);
1330 }
1331}
1332
1333void genLong3Addr(CompilationUnit* cUnit, MIR* mir, OpKind firstOp,
1334 OpKind secondOp, RegLocation rlDest,
1335 RegLocation rlSrc1, RegLocation rlSrc2)
1336{
1337 RegLocation rlResult;
1338#if defined(TARGET_ARM)
1339 /*
1340 * NOTE: This is the one place in the code in which we might have
1341 * as many as six live temporary registers. There are 5 in the normal
1342 * set for Arm. Until we have spill capabilities, temporarily add
1343 * lr to the temp set. It is safe to do this locally, but note that
1344 * lr is used explicitly elsewhere in the code generator and cannot
1345 * normally be used as a general temp register.
1346 */
1347 oatMarkTemp(cUnit, rLR); // Add lr to the temp pool
1348 oatFreeTemp(cUnit, rLR); // and make it available
1349#endif
1350 rlSrc1 = loadValueWide(cUnit, rlSrc1, kCoreReg);
1351 rlSrc2 = loadValueWide(cUnit, rlSrc2, kCoreReg);
1352 rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
1353 // The longs may overlap - use intermediate temp if so
1354 if (rlResult.lowReg == rlSrc1.highReg) {
1355 int tReg = oatAllocTemp(cUnit);
buzbee82488f52012-03-02 08:20:26 -08001356 opRegCopy(cUnit, tReg, rlSrc1.highReg);
buzbee31a4a6f2012-02-28 15:36:15 -08001357 opRegRegReg(cUnit, firstOp, rlResult.lowReg, rlSrc1.lowReg,
1358 rlSrc2.lowReg);
1359 opRegRegReg(cUnit, secondOp, rlResult.highReg, tReg,
1360 rlSrc2.highReg);
1361 oatFreeTemp(cUnit, tReg);
1362 } else {
1363 opRegRegReg(cUnit, firstOp, rlResult.lowReg, rlSrc1.lowReg,
1364 rlSrc2.lowReg);
1365 opRegRegReg(cUnit, secondOp, rlResult.highReg, rlSrc1.highReg,
1366 rlSrc2.highReg);
1367 }
1368 /*
1369 * NOTE: If rlDest refers to a frame variable in a large frame, the
1370 * following storeValueWide might need to allocate a temp register.
1371 * To further work around the lack of a spill capability, explicitly
1372 * free any temps from rlSrc1 & rlSrc2 that aren't still live in rlResult.
1373 * Remove when spill is functional.
1374 */
1375 freeRegLocTemps(cUnit, rlResult, rlSrc1);
1376 freeRegLocTemps(cUnit, rlResult, rlSrc2);
1377 storeValueWide(cUnit, rlDest, rlResult);
1378#if defined(TARGET_ARM)
1379 oatClobber(cUnit, rLR);
1380 oatUnmarkTemp(cUnit, rLR); // Remove lr from the temp pool
1381#endif
1382}
1383
1384
1385bool genShiftOpLong(CompilationUnit* cUnit, MIR* mir, RegLocation rlDest,
1386 RegLocation rlSrc1, RegLocation rlShift)
1387{
1388 int funcOffset;
1389
1390 switch( mir->dalvikInsn.opcode) {
1391 case OP_SHL_LONG:
1392 case OP_SHL_LONG_2ADDR:
1393 funcOffset = OFFSETOF_MEMBER(Thread, pShlLong);
1394 break;
1395 case OP_SHR_LONG:
1396 case OP_SHR_LONG_2ADDR:
1397 funcOffset = OFFSETOF_MEMBER(Thread, pShrLong);
1398 break;
1399 case OP_USHR_LONG:
1400 case OP_USHR_LONG_2ADDR:
1401 funcOffset = OFFSETOF_MEMBER(Thread, pUshrLong);
1402 break;
1403 default:
1404 LOG(FATAL) << "Unexpected case";
1405 return true;
1406 }
1407 oatFlushAllRegs(cUnit); /* Send everything to home location */
1408 int rTgt = loadHelper(cUnit, funcOffset);
1409 loadValueDirectWideFixed(cUnit, rlSrc1, rARG0, rARG1);
1410 loadValueDirect(cUnit, rlShift, rARG2);
1411 callRuntimeHelper(cUnit, rTgt);
1412 RegLocation rlResult = oatGetReturnWide(cUnit);
1413 storeValueWide(cUnit, rlDest, rlResult);
1414 return false;
1415}
1416
1417
1418bool genArithOpInt(CompilationUnit* cUnit, MIR* mir, RegLocation rlDest,
1419 RegLocation rlSrc1, RegLocation rlSrc2)
1420{
1421 OpKind op = kOpBkpt;
1422 bool callOut = false;
1423 bool checkZero = false;
1424 bool unary = false;
1425 int retReg = rRET0;
1426 int funcOffset;
1427 RegLocation rlResult;
1428 bool shiftOp = false;
1429
1430 switch (mir->dalvikInsn.opcode) {
1431 case OP_NEG_INT:
1432 op = kOpNeg;
1433 unary = true;
1434 break;
1435 case OP_NOT_INT:
1436 op = kOpMvn;
1437 unary = true;
1438 break;
1439 case OP_ADD_INT:
1440 case OP_ADD_INT_2ADDR:
1441 op = kOpAdd;
1442 break;
1443 case OP_SUB_INT:
1444 case OP_SUB_INT_2ADDR:
1445 op = kOpSub;
1446 break;
1447 case OP_MUL_INT:
1448 case OP_MUL_INT_2ADDR:
1449 op = kOpMul;
1450 break;
1451 case OP_DIV_INT:
1452 case OP_DIV_INT_2ADDR:
1453 callOut = true;
1454 checkZero = true;
1455 funcOffset = OFFSETOF_MEMBER(Thread, pIdiv);
1456 retReg = rRET0;
1457 break;
buzbee5de34942012-03-01 14:51:57 -08001458 /* NOTE: returns in rARG1 */
buzbee31a4a6f2012-02-28 15:36:15 -08001459 case OP_REM_INT:
1460 case OP_REM_INT_2ADDR:
1461 callOut = true;
1462 checkZero = true;
1463 funcOffset = OFFSETOF_MEMBER(Thread, pIdivmod);
1464 retReg = rRET1;
1465 break;
1466 case OP_AND_INT:
1467 case OP_AND_INT_2ADDR:
1468 op = kOpAnd;
1469 break;
1470 case OP_OR_INT:
1471 case OP_OR_INT_2ADDR:
1472 op = kOpOr;
1473 break;
1474 case OP_XOR_INT:
1475 case OP_XOR_INT_2ADDR:
1476 op = kOpXor;
1477 break;
1478 case OP_SHL_INT:
1479 case OP_SHL_INT_2ADDR:
1480 shiftOp = true;
1481 op = kOpLsl;
1482 break;
1483 case OP_SHR_INT:
1484 case OP_SHR_INT_2ADDR:
1485 shiftOp = true;
1486 op = kOpAsr;
1487 break;
1488 case OP_USHR_INT:
1489 case OP_USHR_INT_2ADDR:
1490 shiftOp = true;
1491 op = kOpLsr;
1492 break;
1493 default:
1494 LOG(FATAL) << "Invalid word arith op: " <<
1495 (int)mir->dalvikInsn.opcode;
1496 }
1497 if (!callOut) {
1498 rlSrc1 = loadValue(cUnit, rlSrc1, kCoreReg);
1499 if (unary) {
1500 rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
1501 opRegReg(cUnit, op, rlResult.lowReg,
1502 rlSrc1.lowReg);
1503 } else {
1504 rlSrc2 = loadValue(cUnit, rlSrc2, kCoreReg);
1505 if (shiftOp) {
1506 int tReg = oatAllocTemp(cUnit);
1507 opRegRegImm(cUnit, kOpAnd, tReg, rlSrc2.lowReg, 31);
1508 rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
1509 opRegRegReg(cUnit, op, rlResult.lowReg,
1510 rlSrc1.lowReg, tReg);
1511 oatFreeTemp(cUnit, tReg);
1512 } else {
1513 rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
1514 opRegRegReg(cUnit, op, rlResult.lowReg,
1515 rlSrc1.lowReg, rlSrc2.lowReg);
1516 }
1517 }
1518 storeValue(cUnit, rlDest, rlResult);
1519 } else {
1520 RegLocation rlResult;
1521 oatFlushAllRegs(cUnit); /* Send everything to home location */
1522 loadValueDirectFixed(cUnit, rlSrc2, rRET1);
1523 int rTgt = loadHelper(cUnit, funcOffset);
1524 loadValueDirectFixed(cUnit, rlSrc1, rARG0);
1525 if (checkZero) {
1526 genImmedCheck(cUnit, kCondEq, rARG1, 0, mir, kThrowDivZero);
1527 }
1528 callRuntimeHelper(cUnit, rTgt);
1529 if (retReg == rRET0)
1530 rlResult = oatGetReturn(cUnit);
1531 else
1532 rlResult = oatGetReturnAlt(cUnit);
1533 storeValue(cUnit, rlDest, rlResult);
1534 }
1535 return false;
1536}
1537
1538/*
1539 * The following are the first-level codegen routines that analyze the format
1540 * of each bytecode then either dispatch special purpose codegen routines
1541 * or produce corresponding Thumb instructions directly.
1542 */
1543
1544bool isPowerOfTwo(int x)
1545{
1546 return (x & (x - 1)) == 0;
1547}
1548
1549// Returns true if no more than two bits are set in 'x'.
1550bool isPopCountLE2(unsigned int x)
1551{
1552 x &= x - 1;
1553 return (x & (x - 1)) == 0;
1554}
1555
1556// Returns the index of the lowest set bit in 'x'.
1557int lowestSetBit(unsigned int x) {
1558 int bit_posn = 0;
1559 while ((x & 0xf) == 0) {
1560 bit_posn += 4;
1561 x >>= 4;
1562 }
1563 while ((x & 1) == 0) {
1564 bit_posn++;
1565 x >>= 1;
1566 }
1567 return bit_posn;
1568}
1569
1570// Returns true if it added instructions to 'cUnit' to divide 'rlSrc' by 'lit'
1571// and store the result in 'rlDest'.
1572bool handleEasyDivide(CompilationUnit* cUnit, Opcode dalvikOpcode,
1573 RegLocation rlSrc, RegLocation rlDest, int lit)
1574{
1575 if (lit < 2 || !isPowerOfTwo(lit)) {
1576 return false;
1577 }
1578 int k = lowestSetBit(lit);
1579 if (k >= 30) {
1580 // Avoid special cases.
1581 return false;
1582 }
1583 bool div = (dalvikOpcode == OP_DIV_INT_LIT8 ||
1584 dalvikOpcode == OP_DIV_INT_LIT16);
1585 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
1586 RegLocation rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
1587 if (div) {
1588 int tReg = oatAllocTemp(cUnit);
1589 if (lit == 2) {
1590 // Division by 2 is by far the most common division by constant.
1591 opRegRegImm(cUnit, kOpLsr, tReg, rlSrc.lowReg, 32 - k);
1592 opRegRegReg(cUnit, kOpAdd, tReg, tReg, rlSrc.lowReg);
1593 opRegRegImm(cUnit, kOpAsr, rlResult.lowReg, tReg, k);
1594 } else {
1595 opRegRegImm(cUnit, kOpAsr, tReg, rlSrc.lowReg, 31);
1596 opRegRegImm(cUnit, kOpLsr, tReg, tReg, 32 - k);
1597 opRegRegReg(cUnit, kOpAdd, tReg, tReg, rlSrc.lowReg);
1598 opRegRegImm(cUnit, kOpAsr, rlResult.lowReg, tReg, k);
1599 }
1600 } else {
1601 int cReg = oatAllocTemp(cUnit);
1602 loadConstant(cUnit, cReg, lit - 1);
1603 int tReg1 = oatAllocTemp(cUnit);
1604 int tReg2 = oatAllocTemp(cUnit);
1605 if (lit == 2) {
1606 opRegRegImm(cUnit, kOpLsr, tReg1, rlSrc.lowReg, 32 - k);
1607 opRegRegReg(cUnit, kOpAdd, tReg2, tReg1, rlSrc.lowReg);
1608 opRegRegReg(cUnit, kOpAnd, tReg2, tReg2, cReg);
1609 opRegRegReg(cUnit, kOpSub, rlResult.lowReg, tReg2, tReg1);
1610 } else {
1611 opRegRegImm(cUnit, kOpAsr, tReg1, rlSrc.lowReg, 31);
1612 opRegRegImm(cUnit, kOpLsr, tReg1, tReg1, 32 - k);
1613 opRegRegReg(cUnit, kOpAdd, tReg2, tReg1, rlSrc.lowReg);
1614 opRegRegReg(cUnit, kOpAnd, tReg2, tReg2, cReg);
1615 opRegRegReg(cUnit, kOpSub, rlResult.lowReg, tReg2, tReg1);
1616 }
1617 }
1618 storeValue(cUnit, rlDest, rlResult);
1619 return true;
1620}
1621
1622void genMultiplyByTwoBitMultiplier(CompilationUnit* cUnit, RegLocation rlSrc,
1623 RegLocation rlResult, int lit,
1624 int firstBit, int secondBit)
1625{
buzbee0398c422012-03-02 15:22:47 -08001626#if defined(TARGET_ARM)
buzbee31a4a6f2012-02-28 15:36:15 -08001627 opRegRegRegShift(cUnit, kOpAdd, rlResult.lowReg, rlSrc.lowReg, rlSrc.lowReg,
1628 encodeShift(kArmLsl, secondBit - firstBit));
buzbee0398c422012-03-02 15:22:47 -08001629#else
1630 int tReg = oatAllocTemp(cUnit);
1631 opRegRegImm(cUnit, kOpLsl, tReg, rlSrc.lowReg, secondBit - firstBit);
1632 opRegRegReg(cUnit, kOpAdd, rlResult.lowReg, rlSrc.lowReg, tReg);
1633 oatFreeTemp(cUnit, tReg);
buzbee5de34942012-03-01 14:51:57 -08001634#endif
buzbee31a4a6f2012-02-28 15:36:15 -08001635 if (firstBit != 0) {
1636 opRegRegImm(cUnit, kOpLsl, rlResult.lowReg, rlResult.lowReg, firstBit);
1637 }
1638}
1639
1640// Returns true if it added instructions to 'cUnit' to multiply 'rlSrc' by 'lit'
1641// and store the result in 'rlDest'.
1642bool handleEasyMultiply(CompilationUnit* cUnit, RegLocation rlSrc,
1643 RegLocation rlDest, int lit)
1644{
1645 // Can we simplify this multiplication?
1646 bool powerOfTwo = false;
1647 bool popCountLE2 = false;
1648 bool powerOfTwoMinusOne = false;
1649 if (lit < 2) {
1650 // Avoid special cases.
1651 return false;
1652 } else if (isPowerOfTwo(lit)) {
1653 powerOfTwo = true;
1654 } else if (isPopCountLE2(lit)) {
1655 popCountLE2 = true;
1656 } else if (isPowerOfTwo(lit + 1)) {
1657 powerOfTwoMinusOne = true;
1658 } else {
1659 return false;
1660 }
1661 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
1662 RegLocation rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
1663 if (powerOfTwo) {
1664 // Shift.
1665 opRegRegImm(cUnit, kOpLsl, rlResult.lowReg, rlSrc.lowReg,
1666 lowestSetBit(lit));
1667 } else if (popCountLE2) {
1668 // Shift and add and shift.
1669 int firstBit = lowestSetBit(lit);
1670 int secondBit = lowestSetBit(lit ^ (1 << firstBit));
1671 genMultiplyByTwoBitMultiplier(cUnit, rlSrc, rlResult, lit,
1672 firstBit, secondBit);
1673 } else {
1674 // Reverse subtract: (src << (shift + 1)) - src.
1675 DCHECK(powerOfTwoMinusOne);
1676 // TUNING: rsb dst, src, src lsl#lowestSetBit(lit + 1)
1677 int tReg = oatAllocTemp(cUnit);
1678 opRegRegImm(cUnit, kOpLsl, tReg, rlSrc.lowReg, lowestSetBit(lit + 1));
1679 opRegRegReg(cUnit, kOpSub, rlResult.lowReg, tReg, rlSrc.lowReg);
1680 }
1681 storeValue(cUnit, rlDest, rlResult);
1682 return true;
1683}
1684
1685bool genArithOpIntLit(CompilationUnit* cUnit, MIR* mir, RegLocation rlDest,
1686 RegLocation rlSrc, int lit)
1687{
1688 Opcode dalvikOpcode = mir->dalvikInsn.opcode;
1689 RegLocation rlResult;
1690 OpKind op = (OpKind)0; /* Make gcc happy */
1691 int shiftOp = false;
1692 bool isDiv = false;
1693 int funcOffset;
1694 int rTgt;
1695
1696 switch (dalvikOpcode) {
1697 case OP_RSUB_INT_LIT8:
1698 case OP_RSUB_INT: {
1699 int tReg;
1700 //TUNING: add support for use of Arm rsub op
1701 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
1702 tReg = oatAllocTemp(cUnit);
1703 loadConstant(cUnit, tReg, lit);
1704 rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
1705 opRegRegReg(cUnit, kOpSub, rlResult.lowReg,
1706 tReg, rlSrc.lowReg);
1707 storeValue(cUnit, rlDest, rlResult);
1708 return false;
1709 break;
1710 }
1711
1712 case OP_ADD_INT_LIT8:
1713 case OP_ADD_INT_LIT16:
1714 op = kOpAdd;
1715 break;
1716 case OP_MUL_INT_LIT8:
1717 case OP_MUL_INT_LIT16: {
1718 if (handleEasyMultiply(cUnit, rlSrc, rlDest, lit)) {
1719 return false;
1720 }
1721 op = kOpMul;
1722 break;
1723 }
1724 case OP_AND_INT_LIT8:
1725 case OP_AND_INT_LIT16:
1726 op = kOpAnd;
1727 break;
1728 case OP_OR_INT_LIT8:
1729 case OP_OR_INT_LIT16:
1730 op = kOpOr;
1731 break;
1732 case OP_XOR_INT_LIT8:
1733 case OP_XOR_INT_LIT16:
1734 op = kOpXor;
1735 break;
1736 case OP_SHL_INT_LIT8:
1737 lit &= 31;
1738 shiftOp = true;
1739 op = kOpLsl;
1740 break;
1741 case OP_SHR_INT_LIT8:
1742 lit &= 31;
1743 shiftOp = true;
1744 op = kOpAsr;
1745 break;
1746 case OP_USHR_INT_LIT8:
1747 lit &= 31;
1748 shiftOp = true;
1749 op = kOpLsr;
1750 break;
1751
1752 case OP_DIV_INT_LIT8:
1753 case OP_DIV_INT_LIT16:
1754 case OP_REM_INT_LIT8:
1755 case OP_REM_INT_LIT16:
1756 if (lit == 0) {
1757 genImmedCheck(cUnit, kCondAl, 0, 0, mir, kThrowDivZero);
1758 return false;
1759 }
1760 if (handleEasyDivide(cUnit, dalvikOpcode, rlSrc, rlDest, lit)) {
1761 return false;
1762 }
1763 oatFlushAllRegs(cUnit); /* Everything to home location */
1764 loadValueDirectFixed(cUnit, rlSrc, rARG0);
1765 oatClobber(cUnit, rARG0);
1766 if ((dalvikOpcode == OP_DIV_INT_LIT8) ||
1767 (dalvikOpcode == OP_DIV_INT_LIT16)) {
1768 funcOffset = OFFSETOF_MEMBER(Thread, pIdiv);
1769 isDiv = true;
1770 } else {
1771 funcOffset = OFFSETOF_MEMBER(Thread, pIdivmod);
1772 isDiv = false;
1773 }
1774 rTgt = loadHelper(cUnit, funcOffset);
1775 loadConstant(cUnit, rARG1, lit);
1776 callRuntimeHelper(cUnit, rTgt);
1777 if (isDiv)
1778 rlResult = oatGetReturn(cUnit);
1779 else
1780 rlResult = oatGetReturnAlt(cUnit);
1781 storeValue(cUnit, rlDest, rlResult);
1782 return false;
1783 break;
1784 default:
1785 return true;
1786 }
1787 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
1788 rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
1789 // Avoid shifts by literal 0 - no support in Thumb. Change to copy
1790 if (shiftOp && (lit == 0)) {
buzbee82488f52012-03-02 08:20:26 -08001791 opRegCopy(cUnit, rlResult.lowReg, rlSrc.lowReg);
buzbee31a4a6f2012-02-28 15:36:15 -08001792 } else {
1793 opRegRegImm(cUnit, op, rlResult.lowReg, rlSrc.lowReg, lit);
1794 }
1795 storeValue(cUnit, rlDest, rlResult);
1796 return false;
1797}
1798
1799bool genArithOpLong(CompilationUnit* cUnit, MIR* mir, RegLocation rlDest,
1800 RegLocation rlSrc1, RegLocation rlSrc2)
1801{
1802 RegLocation rlResult;
1803 OpKind firstOp = kOpBkpt;
1804 OpKind secondOp = kOpBkpt;
1805 bool callOut = false;
1806 bool checkZero = false;
1807 int funcOffset;
1808 int retReg = rRET0;
1809
1810 switch (mir->dalvikInsn.opcode) {
1811 case OP_NOT_LONG:
1812 rlSrc2 = loadValueWide(cUnit, rlSrc2, kCoreReg);
1813 rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
1814 // Check for destructive overlap
1815 if (rlResult.lowReg == rlSrc2.highReg) {
1816 int tReg = oatAllocTemp(cUnit);
buzbee82488f52012-03-02 08:20:26 -08001817 opRegCopy(cUnit, tReg, rlSrc2.highReg);
buzbee31a4a6f2012-02-28 15:36:15 -08001818 opRegReg(cUnit, kOpMvn, rlResult.lowReg, rlSrc2.lowReg);
1819 opRegReg(cUnit, kOpMvn, rlResult.highReg, tReg);
1820 oatFreeTemp(cUnit, tReg);
1821 } else {
1822 opRegReg(cUnit, kOpMvn, rlResult.lowReg, rlSrc2.lowReg);
1823 opRegReg(cUnit, kOpMvn, rlResult.highReg, rlSrc2.highReg);
1824 }
1825 storeValueWide(cUnit, rlDest, rlResult);
1826 return false;
1827 break;
1828 case OP_ADD_LONG:
1829 case OP_ADD_LONG_2ADDR:
1830 firstOp = kOpAdd;
1831 secondOp = kOpAdc;
1832 break;
1833 case OP_SUB_LONG:
1834 case OP_SUB_LONG_2ADDR:
1835 firstOp = kOpSub;
1836 secondOp = kOpSbc;
1837 break;
1838 case OP_MUL_LONG:
1839 case OP_MUL_LONG_2ADDR:
1840 callOut = true;
1841 retReg = rRET0;
1842 funcOffset = OFFSETOF_MEMBER(Thread, pLmul);
1843 break;
1844 case OP_DIV_LONG:
1845 case OP_DIV_LONG_2ADDR:
1846 callOut = true;
1847 checkZero = true;
1848 retReg = rRET0;
1849 funcOffset = OFFSETOF_MEMBER(Thread, pLdivmod);
1850 break;
1851 /* NOTE - result is in rARG2/rARG3 instead of rRET0/rRET1 */
1852 // FIXME: is true, or could be made true, or other targets?
1853 case OP_REM_LONG:
1854 case OP_REM_LONG_2ADDR:
1855 callOut = true;
1856 checkZero = true;
1857 funcOffset = OFFSETOF_MEMBER(Thread, pLdivmod);
1858 retReg = rARG2;
1859 break;
1860 case OP_AND_LONG_2ADDR:
1861 case OP_AND_LONG:
1862 firstOp = kOpAnd;
1863 secondOp = kOpAnd;
1864 break;
1865 case OP_OR_LONG:
1866 case OP_OR_LONG_2ADDR:
1867 firstOp = kOpOr;
1868 secondOp = kOpOr;
1869 break;
1870 case OP_XOR_LONG:
1871 case OP_XOR_LONG_2ADDR:
1872 firstOp = kOpXor;
1873 secondOp = kOpXor;
1874 break;
1875 case OP_NEG_LONG: {
1876 rlSrc2 = loadValueWide(cUnit, rlSrc2, kCoreReg);
1877 rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
1878 int zReg = oatAllocTemp(cUnit);
1879 loadConstantNoClobber(cUnit, zReg, 0);
1880 // Check for destructive overlap
1881 if (rlResult.lowReg == rlSrc2.highReg) {
1882 int tReg = oatAllocTemp(cUnit);
1883 opRegRegReg(cUnit, kOpSub, rlResult.lowReg,
1884 zReg, rlSrc2.lowReg);
1885 opRegRegReg(cUnit, kOpSbc, rlResult.highReg,
1886 zReg, tReg);
1887 oatFreeTemp(cUnit, tReg);
1888 } else {
1889 opRegRegReg(cUnit, kOpSub, rlResult.lowReg,
1890 zReg, rlSrc2.lowReg);
1891 opRegRegReg(cUnit, kOpSbc, rlResult.highReg,
1892 zReg, rlSrc2.highReg);
1893 }
1894 oatFreeTemp(cUnit, zReg);
1895 storeValueWide(cUnit, rlDest, rlResult);
1896 return false;
1897 }
1898 default:
1899 LOG(FATAL) << "Invalid long arith op";
1900 }
1901 if (!callOut) {
1902 genLong3Addr(cUnit, mir, firstOp, secondOp, rlDest, rlSrc1, rlSrc2);
1903 } else {
1904 int rTgt;
1905 oatFlushAllRegs(cUnit); /* Send everything to home location */
1906 if (checkZero) {
1907 loadValueDirectWideFixed(cUnit, rlSrc2, rARG2, rARG3);
1908 rTgt = loadHelper(cUnit, funcOffset);
1909 loadValueDirectWideFixed(cUnit, rlSrc1, rARG0, rARG1);
1910 int tReg = oatAllocTemp(cUnit);
1911#if defined(TARGET_ARM)
1912 newLIR4(cUnit, kThumb2OrrRRRs, tReg, rARG2, rARG3, 0);
1913 oatFreeTemp(cUnit, tReg);
1914 genCheck(cUnit, kCondEq, mir, kThrowDivZero);
1915#else
1916 opRegRegReg(cUnit, kOpOr, tReg, rARG2, rARG3);
buzbee5de34942012-03-01 14:51:57 -08001917 genImmedCheck(cUnit, kCondEq, tReg, 0, mir, kThrowDivZero);
buzbee31a4a6f2012-02-28 15:36:15 -08001918 oatFreeTemp(cUnit, tReg);
1919#endif
1920 } else {
1921 rTgt = loadHelper(cUnit, funcOffset);
1922 loadValueDirectWideFixed(cUnit, rlSrc1, rARG0, rARG1);
1923 loadValueDirectWideFixed(cUnit, rlSrc2, rARG2, rARG3);
1924 }
1925 callRuntimeHelper(cUnit, rTgt);
1926 // Adjust return regs in to handle case of rem returning rARG2/rARG3
1927 if (retReg == rRET0)
1928 rlResult = oatGetReturnWide(cUnit);
1929 else
1930 rlResult = oatGetReturnWideAlt(cUnit);
1931 storeValueWide(cUnit, rlDest, rlResult);
1932 }
1933 return false;
1934}
1935
1936bool genConversionCall(CompilationUnit* cUnit, MIR* mir, int funcOffset,
1937 int srcSize, int tgtSize)
1938{
1939 /*
1940 * Don't optimize the register usage since it calls out to support
1941 * functions
1942 */
1943 RegLocation rlSrc;
1944 RegLocation rlDest;
1945 oatFlushAllRegs(cUnit); /* Send everything to home location */
1946 int rTgt = loadHelper(cUnit, funcOffset);
1947 if (srcSize == 1) {
1948 rlSrc = oatGetSrc(cUnit, mir, 0);
1949 loadValueDirectFixed(cUnit, rlSrc, rARG0);
1950 } else {
1951 rlSrc = oatGetSrcWide(cUnit, mir, 0, 1);
1952 loadValueDirectWideFixed(cUnit, rlSrc, rARG0, rARG1);
1953 }
1954 callRuntimeHelper(cUnit, rTgt);
1955 if (tgtSize == 1) {
1956 RegLocation rlResult;
1957 rlDest = oatGetDest(cUnit, mir, 0);
1958 rlResult = oatGetReturn(cUnit);
1959 storeValue(cUnit, rlDest, rlResult);
1960 } else {
1961 RegLocation rlResult;
1962 rlDest = oatGetDestWide(cUnit, mir, 0, 1);
1963 rlResult = oatGetReturnWide(cUnit);
1964 storeValueWide(cUnit, rlDest, rlResult);
1965 }
1966 return false;
1967}
1968
1969void genNegFloat(CompilationUnit* cUnit, RegLocation rlDest, RegLocation rlSrc);
1970bool genArithOpFloatPortable(CompilationUnit* cUnit, MIR* mir,
1971 RegLocation rlDest, RegLocation rlSrc1,
1972 RegLocation rlSrc2)
1973{
1974 RegLocation rlResult;
1975 int funcOffset;
1976
1977 switch (mir->dalvikInsn.opcode) {
1978 case OP_ADD_FLOAT_2ADDR:
1979 case OP_ADD_FLOAT:
1980 funcOffset = OFFSETOF_MEMBER(Thread, pFadd);
1981 break;
1982 case OP_SUB_FLOAT_2ADDR:
1983 case OP_SUB_FLOAT:
1984 funcOffset = OFFSETOF_MEMBER(Thread, pFsub);
1985 break;
1986 case OP_DIV_FLOAT_2ADDR:
1987 case OP_DIV_FLOAT:
1988 funcOffset = OFFSETOF_MEMBER(Thread, pFdiv);
1989 break;
1990 case OP_MUL_FLOAT_2ADDR:
1991 case OP_MUL_FLOAT:
1992 funcOffset = OFFSETOF_MEMBER(Thread, pFmul);
1993 break;
1994 case OP_REM_FLOAT_2ADDR:
1995 case OP_REM_FLOAT:
1996 funcOffset = OFFSETOF_MEMBER(Thread, pFmodf);
1997 break;
1998 case OP_NEG_FLOAT: {
1999 genNegFloat(cUnit, rlDest, rlSrc1);
2000 return false;
2001 }
2002 default:
2003 return true;
2004 }
2005 oatFlushAllRegs(cUnit); /* Send everything to home location */
2006 int rTgt = loadHelper(cUnit, funcOffset);
2007 loadValueDirectFixed(cUnit, rlSrc1, rARG0);
2008 loadValueDirectFixed(cUnit, rlSrc2, rARG1);
2009 callRuntimeHelper(cUnit, rTgt);
2010 rlResult = oatGetReturn(cUnit);
2011 storeValue(cUnit, rlDest, rlResult);
2012 return false;
2013}
2014
2015void genNegDouble(CompilationUnit* cUnit, RegLocation rlDst, RegLocation rlSrc);
2016bool genArithOpDoublePortable(CompilationUnit* cUnit, MIR* mir,
2017 RegLocation rlDest, RegLocation rlSrc1,
2018 RegLocation rlSrc2)
2019{
2020 RegLocation rlResult;
2021 int funcOffset;
2022
2023 switch (mir->dalvikInsn.opcode) {
2024 case OP_ADD_DOUBLE_2ADDR:
2025 case OP_ADD_DOUBLE:
2026 funcOffset = OFFSETOF_MEMBER(Thread, pDadd);
2027 break;
2028 case OP_SUB_DOUBLE_2ADDR:
2029 case OP_SUB_DOUBLE:
2030 funcOffset = OFFSETOF_MEMBER(Thread, pDsub);
2031 break;
2032 case OP_DIV_DOUBLE_2ADDR:
2033 case OP_DIV_DOUBLE:
2034 funcOffset = OFFSETOF_MEMBER(Thread, pDdiv);
2035 break;
2036 case OP_MUL_DOUBLE_2ADDR:
2037 case OP_MUL_DOUBLE:
2038 funcOffset = OFFSETOF_MEMBER(Thread, pDmul);
2039 break;
2040 case OP_REM_DOUBLE_2ADDR:
2041 case OP_REM_DOUBLE:
2042 funcOffset = OFFSETOF_MEMBER(Thread, pFmod);
2043 break;
2044 case OP_NEG_DOUBLE: {
2045 genNegDouble(cUnit, rlDest, rlSrc1);
2046 return false;
2047 }
2048 default:
2049 return true;
2050 }
2051 oatFlushAllRegs(cUnit); /* Send everything to home location */
2052 int rTgt = loadHelper(cUnit, funcOffset);
2053 loadValueDirectWideFixed(cUnit, rlSrc1, rARG0, rARG1);
2054 loadValueDirectWideFixed(cUnit, rlSrc2, rARG2, rARG3);
2055 callRuntimeHelper(cUnit, rTgt);
2056 rlResult = oatGetReturnWide(cUnit);
2057 storeValueWide(cUnit, rlDest, rlResult);
2058 return false;
2059}
2060
2061bool genConversionPortable(CompilationUnit* cUnit, MIR* mir)
2062{
2063 Opcode opcode = mir->dalvikInsn.opcode;
2064
2065 switch (opcode) {
2066 case OP_INT_TO_FLOAT:
2067 return genConversionCall(cUnit, mir, OFFSETOF_MEMBER(Thread, pI2f),
2068 1, 1);
2069 case OP_FLOAT_TO_INT:
2070 return genConversionCall(cUnit, mir, OFFSETOF_MEMBER(Thread, pF2iz),
2071 1, 1);
2072 case OP_DOUBLE_TO_FLOAT:
2073 return genConversionCall(cUnit, mir, OFFSETOF_MEMBER(Thread, pD2f),
2074 2, 1);
2075 case OP_FLOAT_TO_DOUBLE:
2076 return genConversionCall(cUnit, mir, OFFSETOF_MEMBER(Thread, pF2d),
2077 1, 2);
2078 case OP_INT_TO_DOUBLE:
2079 return genConversionCall(cUnit, mir, OFFSETOF_MEMBER(Thread, pI2d),
2080 1, 2);
2081 case OP_DOUBLE_TO_INT:
2082 return genConversionCall(cUnit, mir, OFFSETOF_MEMBER(Thread, pD2iz),
2083 2, 1);
2084 case OP_FLOAT_TO_LONG:
2085 return genConversionCall(cUnit, mir, OFFSETOF_MEMBER(Thread,
2086 pF2l), 1, 2);
2087 case OP_LONG_TO_FLOAT:
2088 return genConversionCall(cUnit, mir, OFFSETOF_MEMBER(Thread, pL2f),
2089 2, 1);
2090 case OP_DOUBLE_TO_LONG:
2091 return genConversionCall(cUnit, mir, OFFSETOF_MEMBER(Thread,
2092 pD2l), 2, 2);
2093 case OP_LONG_TO_DOUBLE:
2094 return genConversionCall(cUnit, mir, OFFSETOF_MEMBER(Thread, pL2d),
2095 2, 2);
2096 default:
2097 return true;
2098 }
2099 return false;
2100}
2101
2102/*
2103 * Generate callout to updateDebugger. Note that we're overloading
2104 * the use of rSUSPEND here. When the debugger is active, this
2105 * register holds the address of the update function. So, if it's
2106 * non-null, we call out to it.
2107 *
2108 * Note also that rRET0 and rRET1 must be preserved across this
2109 * code. This must be handled by the stub.
2110 */
2111void genDebuggerUpdate(CompilationUnit* cUnit, int32_t offset)
2112{
2113 // Following DCHECK verifies that dPC is in range of single load immediate
2114 DCHECK((offset == DEBUGGER_METHOD_ENTRY) ||
2115 (offset == DEBUGGER_METHOD_EXIT) || ((offset & 0xffff) == offset));
2116 oatClobberCalleeSave(cUnit);
2117#if defined(TARGET_ARM)
2118 opRegImm(cUnit, kOpCmp, rSUSPEND, 0);
buzbee82488f52012-03-02 08:20:26 -08002119 opIT(cUnit, kArmCondNe, "T");
buzbee31a4a6f2012-02-28 15:36:15 -08002120 loadConstant(cUnit, rARG2, offset); // arg2 <- Entry code
2121 opReg(cUnit, kOpBlx, rSUSPEND);
2122#else
buzbee82488f52012-03-02 08:20:26 -08002123 LIR* branch = opCmpImmBranch(cUnit, kCondEq, rSUSPEND, 0, NULL);
buzbee31a4a6f2012-02-28 15:36:15 -08002124 loadConstant(cUnit, rARG2, offset);
2125 opReg(cUnit, kOpBlx, rSUSPEND);
2126 LIR* target = newLIR0(cUnit, kPseudoTargetLabel);
2127 target->defMask = ENCODE_ALL;
2128 branch->target = (LIR*)target;
2129#endif
2130 oatFreeTemp(cUnit, rARG2);
2131}
2132
2133/* Check if we need to check for pending suspend request */
2134void genSuspendTest(CompilationUnit* cUnit, MIR* mir)
2135{
2136 if (NO_SUSPEND || (mir->optimizationFlags & MIR_IGNORE_SUSPEND_CHECK)) {
2137 return;
2138 }
2139 oatFlushAllRegs(cUnit);
2140 LIR* branch;
2141 if (cUnit->genDebugger) {
2142 // If generating code for the debugger, always check for suspension
buzbee82488f52012-03-02 08:20:26 -08002143 branch = opUnconditionalBranch(cUnit, NULL);
buzbee31a4a6f2012-02-28 15:36:15 -08002144 } else {
2145#if defined(TARGET_ARM)
2146 // In non-debug case, only check periodically
2147 newLIR2(cUnit, kThumbSubRI8, rSUSPEND, 1);
buzbee82488f52012-03-02 08:20:26 -08002148 branch = opCondBranch(cUnit, kCondEq, NULL);
buzbee31a4a6f2012-02-28 15:36:15 -08002149#else
2150 opRegImm(cUnit, kOpSub, rSUSPEND, 1);
buzbee82488f52012-03-02 08:20:26 -08002151 branch = opCmpImmBranch(cUnit, kCondEq, rSUSPEND, 0, NULL);
buzbee31a4a6f2012-02-28 15:36:15 -08002152#endif
2153 }
2154 LIR* retLab = newLIR0(cUnit, kPseudoTargetLabel);
2155 retLab->defMask = ENCODE_ALL;
2156 LIR* target = (LIR*)oatNew(cUnit, sizeof(LIR), true, kAllocLIR);
2157 target->dalvikOffset = cUnit->currentDalvikOffset;
2158 target->opcode = kPseudoSuspendTarget;
2159 target->operands[0] = (intptr_t)retLab;
2160 target->operands[1] = mir->offset;
2161 branch->target = (LIR*)target;
2162 oatInsertGrowableList(cUnit, &cUnit->suspendLaunchpads, (intptr_t)target);
2163}
2164
2165} // namespace art