blob: 9b1654f76410887850d4e612adc2dc38f1a9ebbf [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{
Ian Rogers680b1bd2012-03-07 20:18:49 -080050 LIR* branch = opBranchUnconditional(cUnit, kOpUncondBr);
buzbee31a4a6f2012-02-28 15:36:15 -080051 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{
buzbeea2ebdd72012-03-04 14:57:06 -080061 LIR* tgt = rawLIR(cUnit, 0, kPseudoThrowTarget, kind,
62 mir ? mir->offset : 0);
buzbee82488f52012-03-02 08:20:26 -080063 LIR* branch = opCondBranch(cUnit, cCode, tgt);
buzbee31a4a6f2012-02-28 15:36:15 -080064 // Remember branch target - will process later
65 oatInsertGrowableList(cUnit, &cUnit->throwLaunchpads, (intptr_t)tgt);
66 return branch;
67}
buzbee5de34942012-03-01 14:51:57 -080068#endif
buzbee31a4a6f2012-02-28 15:36:15 -080069
70LIR* genImmedCheck(CompilationUnit* cUnit, ConditionCode cCode,
71 int reg, int immVal, MIR* mir, ThrowKind kind)
72{
buzbeea2ebdd72012-03-04 14:57:06 -080073 LIR* tgt = rawLIR(cUnit, 0, kPseudoThrowTarget, kind, mir->offset);
buzbee31a4a6f2012-02-28 15:36:15 -080074 LIR* branch;
75 if (cCode == kCondAl) {
buzbee82488f52012-03-02 08:20:26 -080076 branch = opUnconditionalBranch(cUnit, tgt);
buzbee31a4a6f2012-02-28 15:36:15 -080077 } else {
buzbee82488f52012-03-02 08:20:26 -080078 branch = opCmpImmBranch(cUnit, cCode, reg, immVal, tgt);
buzbee31a4a6f2012-02-28 15:36:15 -080079 }
80 // Remember branch target - will process later
81 oatInsertGrowableList(cUnit, &cUnit->throwLaunchpads, (intptr_t)tgt);
82 return branch;
83}
84
85/* Perform null-check on a register. */
86LIR* genNullCheck(CompilationUnit* cUnit, int sReg, int mReg, MIR* mir)
87{
88 if (!(cUnit->disableOpt & (1 << kNullCheckElimination)) &&
89 mir->optimizationFlags & MIR_IGNORE_NULL_CHECK) {
90 return NULL;
91 }
92 return genImmedCheck(cUnit, kCondEq, mReg, 0, mir, kThrowNullPointer);
93}
94
95/* Perform check on two registers */
96LIR* genRegRegCheck(CompilationUnit* cUnit, ConditionCode cCode,
Ian Rogersb5d09b22012-03-06 22:14:17 -080097 int reg1, int reg2, MIR* mir, ThrowKind kind)
buzbee31a4a6f2012-02-28 15:36:15 -080098{
buzbeea2ebdd72012-03-04 14:57:06 -080099 LIR* tgt = rawLIR(cUnit, 0, kPseudoThrowTarget, kind,
100 mir ? mir->offset : 0, reg1, reg2);
buzbee5de34942012-03-01 14:51:57 -0800101#if defined(TARGET_MIPS)
buzbee82488f52012-03-02 08:20:26 -0800102 LIR* branch = opCmpBranch(cUnit, cCode, reg1, reg2, tgt);
buzbee5de34942012-03-01 14:51:57 -0800103#else
buzbee31a4a6f2012-02-28 15:36:15 -0800104 opRegReg(cUnit, kOpCmp, reg1, reg2);
buzbee82488f52012-03-02 08:20:26 -0800105 LIR* branch = opCondBranch(cUnit, cCode, tgt);
buzbee5de34942012-03-01 14:51:57 -0800106#endif
buzbee31a4a6f2012-02-28 15:36:15 -0800107 // Remember branch target - will process later
108 oatInsertGrowableList(cUnit, &cUnit->throwLaunchpads, (intptr_t)tgt);
109 return branch;
110}
111
112void genCompareAndBranch(CompilationUnit* cUnit, BasicBlock* bb, MIR* mir,
113 RegLocation rlSrc1, RegLocation rlSrc2, LIR* labelList)
114{
115 ConditionCode cond;
116 rlSrc1 = loadValue(cUnit, rlSrc1, kCoreReg);
117 rlSrc2 = loadValue(cUnit, rlSrc2, kCoreReg);
Elliott Hughesadb8c672012-03-06 16:49:32 -0800118 Instruction::Code opcode = mir->dalvikInsn.opcode;
buzbee31a4a6f2012-02-28 15:36:15 -0800119 switch(opcode) {
Elliott Hughesadb8c672012-03-06 16:49:32 -0800120 case Instruction::IF_EQ:
buzbee31a4a6f2012-02-28 15:36:15 -0800121 cond = kCondEq;
122 break;
Elliott Hughesadb8c672012-03-06 16:49:32 -0800123 case Instruction::IF_NE:
buzbee31a4a6f2012-02-28 15:36:15 -0800124 cond = kCondNe;
125 break;
Elliott Hughesadb8c672012-03-06 16:49:32 -0800126 case Instruction::IF_LT:
buzbee31a4a6f2012-02-28 15:36:15 -0800127 cond = kCondLt;
128 break;
Elliott Hughesadb8c672012-03-06 16:49:32 -0800129 case Instruction::IF_GE:
buzbee31a4a6f2012-02-28 15:36:15 -0800130 cond = kCondGe;
131 break;
Elliott Hughesadb8c672012-03-06 16:49:32 -0800132 case Instruction::IF_GT:
buzbee31a4a6f2012-02-28 15:36:15 -0800133 cond = kCondGt;
134 break;
Elliott Hughesadb8c672012-03-06 16:49:32 -0800135 case Instruction::IF_LE:
buzbee31a4a6f2012-02-28 15:36:15 -0800136 cond = kCondLe;
137 break;
138 default:
139 cond = (ConditionCode)0;
140 LOG(FATAL) << "Unexpected opcode " << (int)opcode;
141 }
buzbee5de34942012-03-01 14:51:57 -0800142#if defined(TARGET_MIPS)
buzbee82488f52012-03-02 08:20:26 -0800143 opCmpBranch(cUnit, cond, rlSrc1.lowReg, rlSrc2.lowReg,
144 &labelList[bb->taken->id]);
buzbee5de34942012-03-01 14:51:57 -0800145#else
146 opRegReg(cUnit, kOpCmp, rlSrc1.lowReg, rlSrc2.lowReg);
buzbee82488f52012-03-02 08:20:26 -0800147 opCondBranch(cUnit, cond, &labelList[bb->taken->id]);
buzbee5de34942012-03-01 14:51:57 -0800148#endif
buzbee82488f52012-03-02 08:20:26 -0800149 opUnconditionalBranch(cUnit, &labelList[bb->fallThrough->id]);
buzbee31a4a6f2012-02-28 15:36:15 -0800150}
151
152void genCompareZeroAndBranch(CompilationUnit* cUnit, BasicBlock* bb, MIR* mir,
153 RegLocation rlSrc, LIR* labelList)
154{
155 ConditionCode cond;
156 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
Elliott Hughesadb8c672012-03-06 16:49:32 -0800157 Instruction::Code opcode = mir->dalvikInsn.opcode;
buzbee31a4a6f2012-02-28 15:36:15 -0800158 switch(opcode) {
Elliott Hughesadb8c672012-03-06 16:49:32 -0800159 case Instruction::IF_EQZ:
buzbee31a4a6f2012-02-28 15:36:15 -0800160 cond = kCondEq;
161 break;
Elliott Hughesadb8c672012-03-06 16:49:32 -0800162 case Instruction::IF_NEZ:
buzbee31a4a6f2012-02-28 15:36:15 -0800163 cond = kCondNe;
164 break;
Elliott Hughesadb8c672012-03-06 16:49:32 -0800165 case Instruction::IF_LTZ:
buzbee31a4a6f2012-02-28 15:36:15 -0800166 cond = kCondLt;
167 break;
Elliott Hughesadb8c672012-03-06 16:49:32 -0800168 case Instruction::IF_GEZ:
buzbee31a4a6f2012-02-28 15:36:15 -0800169 cond = kCondGe;
170 break;
Elliott Hughesadb8c672012-03-06 16:49:32 -0800171 case Instruction::IF_GTZ:
buzbee31a4a6f2012-02-28 15:36:15 -0800172 cond = kCondGt;
173 break;
Elliott Hughesadb8c672012-03-06 16:49:32 -0800174 case Instruction::IF_LEZ:
buzbee31a4a6f2012-02-28 15:36:15 -0800175 cond = kCondLe;
176 break;
177 default:
178 cond = (ConditionCode)0;
179 LOG(FATAL) << "Unexpected opcode " << (int)opcode;
180 }
buzbee5de34942012-03-01 14:51:57 -0800181#if defined(TARGET_MIPS)
buzbee82488f52012-03-02 08:20:26 -0800182 opCmpImmBranch(cUnit, cond, rlSrc.lowReg, 0, &labelList[bb->taken->id]);
buzbee5de34942012-03-01 14:51:57 -0800183#else
184 opRegImm(cUnit, kOpCmp, rlSrc.lowReg, 0);
buzbee82488f52012-03-02 08:20:26 -0800185 opCondBranch(cUnit, cond, &labelList[bb->taken->id]);
buzbee5de34942012-03-01 14:51:57 -0800186#endif
buzbee82488f52012-03-02 08:20:26 -0800187 opUnconditionalBranch(cUnit, &labelList[bb->fallThrough->id]);
buzbee31a4a6f2012-02-28 15:36:15 -0800188}
189
190void genIntToLong(CompilationUnit* cUnit, MIR* mir, RegLocation rlDest,
191 RegLocation rlSrc)
192{
193 RegLocation rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
194 if (rlSrc.location == kLocPhysReg) {
buzbee82488f52012-03-02 08:20:26 -0800195 opRegCopy(cUnit, rlResult.lowReg, rlSrc.lowReg);
buzbee31a4a6f2012-02-28 15:36:15 -0800196 } else {
197 loadValueDirect(cUnit, rlSrc, rlResult.lowReg);
198 }
199 opRegRegImm(cUnit, kOpAsr, rlResult.highReg,
200 rlResult.lowReg, 31);
201 storeValueWide(cUnit, rlDest, rlResult);
202}
203
204void genIntNarrowing(CompilationUnit* cUnit, MIR* mir, RegLocation rlDest,
205 RegLocation rlSrc)
206{
207 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
208 RegLocation rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
209 OpKind op = kOpInvalid;
210 switch(mir->dalvikInsn.opcode) {
Elliott Hughesadb8c672012-03-06 16:49:32 -0800211 case Instruction::INT_TO_BYTE:
buzbee31a4a6f2012-02-28 15:36:15 -0800212 op = kOp2Byte;
213 break;
Elliott Hughesadb8c672012-03-06 16:49:32 -0800214 case Instruction::INT_TO_SHORT:
buzbee31a4a6f2012-02-28 15:36:15 -0800215 op = kOp2Short;
216 break;
Elliott Hughesadb8c672012-03-06 16:49:32 -0800217 case Instruction::INT_TO_CHAR:
buzbee31a4a6f2012-02-28 15:36:15 -0800218 op = kOp2Char;
219 break;
220 default:
221 LOG(ERROR) << "Bad int conversion type";
222 }
223 opRegReg(cUnit, op, rlResult.lowReg, rlSrc.lowReg);
224 storeValue(cUnit, rlDest, rlResult);
225}
226
227/*
228 * Let helper function take care of everything. Will call
229 * Array::AllocFromCode(type_idx, method, count);
230 * Note: AllocFromCode will handle checks for errNegativeArraySize.
231 */
232void genNewArray(CompilationUnit* cUnit, MIR* mir, RegLocation rlDest,
233 RegLocation rlSrc)
234{
235 oatFlushAllRegs(cUnit); /* Everything to home location */
236 uint32_t type_idx = mir->dalvikInsn.vC;
237 int rTgt;
238 if (cUnit->compiler->CanAccessTypeWithoutChecks(cUnit->method_idx,
239 cUnit->dex_cache,
240 *cUnit->dex_file,
241 type_idx)) {
242 rTgt = loadHelper(cUnit, OFFSETOF_MEMBER(Thread, pAllocArrayFromCode));
243 } else {
244 rTgt = loadHelper(cUnit, OFFSETOF_MEMBER(Thread,
245 pAllocArrayFromCodeWithAccessCheck));
246 }
247 loadCurrMethodDirect(cUnit, rARG1); // arg1 <- Method*
248 loadConstant(cUnit, rARG0, type_idx); // arg0 <- type_id
249 loadValueDirectFixed(cUnit, rlSrc, rARG2); // arg2 <- count
250 callRuntimeHelper(cUnit, rTgt);
251 RegLocation rlResult = oatGetReturn(cUnit);
252 storeValue(cUnit, rlDest, rlResult);
253}
254
255/*
256 * Similar to genNewArray, but with post-allocation initialization.
257 * Verifier guarantees we're dealing with an array class. Current
258 * code throws runtime exception "bad Filled array req" for 'D' and 'J'.
259 * Current code also throws internal unimp if not 'L', '[' or 'I'.
260 */
261void genFilledNewArray(CompilationUnit* cUnit, MIR* mir, bool isRange)
262{
263 DecodedInstruction* dInsn = &mir->dalvikInsn;
264 int elems = dInsn->vA;
265 int typeId = dInsn->vB;
266 oatFlushAllRegs(cUnit); /* Everything to home location */
267 int rTgt;
268 if (cUnit->compiler->CanAccessTypeWithoutChecks(cUnit->method_idx,
269 cUnit->dex_cache,
270 *cUnit->dex_file,
271 typeId)) {
272 rTgt = loadHelper(cUnit, OFFSETOF_MEMBER(Thread,
273 pCheckAndAllocArrayFromCode));
274 } else {
275 rTgt = loadHelper(cUnit, OFFSETOF_MEMBER(Thread,
276 pCheckAndAllocArrayFromCodeWithAccessCheck));
277 }
buzbee31a4a6f2012-02-28 15:36:15 -0800278 loadConstant(cUnit, rARG0, typeId); // arg0 <- type_id
279 loadConstant(cUnit, rARG2, elems); // arg2 <- count
buzbeee1965672012-03-11 18:39:19 -0700280 loadCurrMethodDirect(cUnit, rARG1); // arg1 <- Method*
buzbee31a4a6f2012-02-28 15:36:15 -0800281 callRuntimeHelper(cUnit, rTgt);
buzbeee1965672012-03-11 18:39:19 -0700282 oatFreeTemp(cUnit, rARG2);
283 oatFreeTemp(cUnit, rARG1);
buzbee31a4a6f2012-02-28 15:36:15 -0800284 /*
Elliott Hughesadb8c672012-03-06 16:49:32 -0800285 * NOTE: the implicit target for Instruction::FILLED_NEW_ARRAY is the
buzbee31a4a6f2012-02-28 15:36:15 -0800286 * return region. Because AllocFromCode placed the new array
287 * in rRET0, we'll just lock it into place. When debugger support is
288 * added, it may be necessary to additionally copy all return
289 * values to a home location in thread-local storage
290 */
291 oatLockTemp(cUnit, rRET0);
292
293 // TODO: use the correct component size, currently all supported types
294 // share array alignment with ints (see comment at head of function)
295 size_t component_size = sizeof(int32_t);
296
297 // Having a range of 0 is legal
298 if (isRange && (dInsn->vA > 0)) {
299 /*
300 * Bit of ugliness here. We're going generate a mem copy loop
301 * on the register range, but it is possible that some regs
302 * in the range have been promoted. This is unlikely, but
303 * before generating the copy, we'll just force a flush
304 * of any regs in the source range that have been promoted to
305 * home location.
306 */
307 for (unsigned int i = 0; i < dInsn->vA; i++) {
308 RegLocation loc = oatUpdateLoc(cUnit,
309 oatGetSrc(cUnit, mir, i));
310 if (loc.location == kLocPhysReg) {
311 storeBaseDisp(cUnit, rSP, oatSRegOffset(cUnit, loc.sRegLow),
312 loc.lowReg, kWord);
313 }
314 }
315 /*
316 * TUNING note: generated code here could be much improved, but
317 * this is an uncommon operation and isn't especially performance
318 * critical.
319 */
320 int rSrc = oatAllocTemp(cUnit);
321 int rDst = oatAllocTemp(cUnit);
322 int rIdx = oatAllocTemp(cUnit);
buzbee5de34942012-03-01 14:51:57 -0800323#if defined(TARGET_ARM)
buzbee31a4a6f2012-02-28 15:36:15 -0800324 int rVal = rLR; // Using a lot of temps, rLR is known free here
buzbee5de34942012-03-01 14:51:57 -0800325#else
326 int rVal = oatAllocTemp(cUnit);
327#endif
buzbee31a4a6f2012-02-28 15:36:15 -0800328 // Set up source pointer
329 RegLocation rlFirst = oatGetSrc(cUnit, mir, 0);
Ian Rogersb5d09b22012-03-06 22:14:17 -0800330#if defined(TARGET_X86)
331 UNIMPLEMENTED(FATAL);
332#else
buzbee31a4a6f2012-02-28 15:36:15 -0800333 opRegRegImm(cUnit, kOpAdd, rSrc, rSP,
334 oatSRegOffset(cUnit, rlFirst.sRegLow));
335 // Set up the target pointer
336 opRegRegImm(cUnit, kOpAdd, rDst, rRET0,
337 Array::DataOffset(component_size).Int32Value());
Ian Rogersb5d09b22012-03-06 22:14:17 -0800338#endif
buzbee31a4a6f2012-02-28 15:36:15 -0800339 // Set up the loop counter (known to be > 0)
340 loadConstant(cUnit, rIdx, dInsn->vA - 1);
341 // Generate the copy loop. Going backwards for convenience
342 LIR* target = newLIR0(cUnit, kPseudoTargetLabel);
buzbee31a4a6f2012-02-28 15:36:15 -0800343 // Copy next element
344 loadBaseIndexed(cUnit, rSrc, rIdx, rVal, 2, kWord);
345 storeBaseIndexed(cUnit, rDst, rIdx, rVal, 2, kWord);
346#if defined(TARGET_ARM)
347 // Combine sub & test using sub setflags encoding here
348 newLIR3(cUnit, kThumb2SubsRRI12, rIdx, rIdx, 1);
buzbee82488f52012-03-02 08:20:26 -0800349 opCondBranch(cUnit, kCondGe, target);
buzbee31a4a6f2012-02-28 15:36:15 -0800350#else
buzbee5de34942012-03-01 14:51:57 -0800351 oatFreeTemp(cUnit, rVal);
buzbee31a4a6f2012-02-28 15:36:15 -0800352 opRegImm(cUnit, kOpSub, rIdx, 1);
buzbee82488f52012-03-02 08:20:26 -0800353 opCmpImmBranch(cUnit, kCondGe, rIdx, 0, target);
buzbee31a4a6f2012-02-28 15:36:15 -0800354#endif
buzbee31a4a6f2012-02-28 15:36:15 -0800355 } else if (!isRange) {
356 // TUNING: interleave
357 for (unsigned int i = 0; i < dInsn->vA; i++) {
358 RegLocation rlArg = loadValue(cUnit,
359 oatGetSrc(cUnit, mir, i), kCoreReg);
360 storeBaseDisp(cUnit, rRET0,
361 Array::DataOffset(component_size).Int32Value() +
362 i * 4, rlArg.lowReg, kWord);
363 // If the loadValue caused a temp to be allocated, free it
364 if (oatIsTemp(cUnit, rlArg.lowReg)) {
365 oatFreeTemp(cUnit, rlArg.lowReg);
366 }
367 }
368 }
369}
370
371void genSput(CompilationUnit* cUnit, MIR* mir, RegLocation rlSrc,
372 bool isLongOrDouble, bool isObject)
373{
374 int fieldOffset;
375 int ssbIndex;
376 bool isVolatile;
377 bool isReferrersClass;
378 uint32_t fieldIdx = mir->dalvikInsn.vB;
379
380 OatCompilationUnit mUnit(cUnit->class_loader, cUnit->class_linker,
381 *cUnit->dex_file, *cUnit->dex_cache,
382 cUnit->code_item, cUnit->method_idx,
383 cUnit->access_flags);
384
385 bool fastPath =
386 cUnit->compiler->ComputeStaticFieldInfo(fieldIdx, &mUnit,
387 fieldOffset, ssbIndex,
388 isReferrersClass, isVolatile, true);
389 if (fastPath && !SLOW_FIELD_PATH) {
390 DCHECK_GE(fieldOffset, 0);
391 int rBase;
buzbee31a4a6f2012-02-28 15:36:15 -0800392 if (isReferrersClass) {
393 // Fast path, static storage base is this method's class
buzbeee1965672012-03-11 18:39:19 -0700394 RegLocation rlMethod = loadCurrMethod(cUnit);
buzbee31a4a6f2012-02-28 15:36:15 -0800395 rBase = oatAllocTemp(cUnit);
buzbeee1965672012-03-11 18:39:19 -0700396 loadWordDisp(cUnit, rlMethod.lowReg,
buzbee31a4a6f2012-02-28 15:36:15 -0800397 Method::DeclaringClassOffset().Int32Value(), rBase);
398 } else {
399 // Medium path, static storage base in a different class which
400 // requires checks that the other class is initialized.
401 DCHECK_GE(ssbIndex, 0);
402 // May do runtime call so everything to home locations.
403 oatFlushAllRegs(cUnit);
404 // Using fixed register to sync with possible call to runtime
405 // support.
buzbeee1965672012-03-11 18:39:19 -0700406 int rMethod = rARG1;
buzbee31a4a6f2012-02-28 15:36:15 -0800407 oatLockTemp(cUnit, rMethod);
408 loadCurrMethodDirect(cUnit, rMethod);
409 rBase = rARG0;
410 oatLockTemp(cUnit, rBase);
411 loadWordDisp(cUnit, rMethod,
412 Method::DexCacheInitializedStaticStorageOffset().Int32Value(),
413 rBase);
414 loadWordDisp(cUnit, rBase,
415 Array::DataOffset(sizeof(Object*)).Int32Value() + sizeof(int32_t*) *
416 ssbIndex, rBase);
417 // rBase now points at appropriate static storage base (Class*)
418 // or NULL if not initialized. Check for NULL and call helper if NULL.
419 // TUNING: fast path should fall through
buzbee82488f52012-03-02 08:20:26 -0800420 LIR* branchOver = opCmpImmBranch(cUnit, kCondNe, rBase, 0, NULL);
buzbee31a4a6f2012-02-28 15:36:15 -0800421 int rTgt = loadHelper(cUnit, OFFSETOF_MEMBER(Thread,
422 pInitializeStaticStorage));
423 loadConstant(cUnit, rARG0, ssbIndex);
424 callRuntimeHelper(cUnit, rTgt);
425#if defined(TARGET_MIPS)
426 // For Arm, rRET0 = rARG0 = rBASE, for Mips, we need to copy
buzbee82488f52012-03-02 08:20:26 -0800427 opRegCopy(cUnit, rBase, rRET0);
buzbee31a4a6f2012-02-28 15:36:15 -0800428#endif
429 LIR* skipTarget = newLIR0(cUnit, kPseudoTargetLabel);
buzbee31a4a6f2012-02-28 15:36:15 -0800430 branchOver->target = (LIR*)skipTarget;
buzbeee1965672012-03-11 18:39:19 -0700431 oatFreeTemp(cUnit, rMethod);
buzbee31a4a6f2012-02-28 15:36:15 -0800432 }
433 // rBase now holds static storage base
buzbee31a4a6f2012-02-28 15:36:15 -0800434 if (isLongOrDouble) {
435 rlSrc = oatGetSrcWide(cUnit, mir, 0, 1);
436 rlSrc = loadValueWide(cUnit, rlSrc, kAnyReg);
437 } else {
438 rlSrc = oatGetSrc(cUnit, mir, 0);
439 rlSrc = loadValue(cUnit, rlSrc, kAnyReg);
440 }
441//FIXME: need to generalize the barrier call
442 if (isVolatile) {
443 oatGenMemBarrier(cUnit, kST);
444 }
445 if (isLongOrDouble) {
446 storeBaseDispWide(cUnit, rBase, fieldOffset, rlSrc.lowReg,
447 rlSrc.highReg);
448 } else {
449 storeWordDisp(cUnit, rBase, fieldOffset, rlSrc.lowReg);
450 }
451 if (isVolatile) {
452 oatGenMemBarrier(cUnit, kSY);
453 }
454 if (isObject) {
455 markGCCard(cUnit, rlSrc.lowReg, rBase);
456 }
457 oatFreeTemp(cUnit, rBase);
458 } else {
459 oatFlushAllRegs(cUnit); // Everything to home locations
460 int setterOffset = isLongOrDouble ? OFFSETOF_MEMBER(Thread, pSet64Static) :
461 (isObject ? OFFSETOF_MEMBER(Thread, pSetObjStatic)
462 : OFFSETOF_MEMBER(Thread, pSet32Static));
463 int rTgt = loadHelper(cUnit, setterOffset);
464 loadConstant(cUnit, rARG0, fieldIdx);
465 if (isLongOrDouble) {
Ian Rogersb5d09b22012-03-06 22:14:17 -0800466#if defined(TARGET_X86)
467 UNIMPLEMENTED(FATAL);
468#else
buzbee31a4a6f2012-02-28 15:36:15 -0800469 loadValueDirectWideFixed(cUnit, rlSrc, rARG2, rARG3);
Ian Rogersb5d09b22012-03-06 22:14:17 -0800470#endif
buzbee31a4a6f2012-02-28 15:36:15 -0800471 } else {
472 loadValueDirect(cUnit, rlSrc, rARG1);
473 }
474 callRuntimeHelper(cUnit, rTgt);
475 }
476}
477
478void genSget(CompilationUnit* cUnit, MIR* mir, RegLocation rlDest,
479 bool isLongOrDouble, bool isObject)
480{
481 int fieldOffset;
482 int ssbIndex;
483 bool isVolatile;
484 bool isReferrersClass;
485 uint32_t fieldIdx = mir->dalvikInsn.vB;
486
487 OatCompilationUnit mUnit(cUnit->class_loader, cUnit->class_linker,
488 *cUnit->dex_file, *cUnit->dex_cache,
489 cUnit->code_item, cUnit->method_idx,
490 cUnit->access_flags);
491
492 bool fastPath =
493 cUnit->compiler->ComputeStaticFieldInfo(fieldIdx, &mUnit,
494 fieldOffset, ssbIndex,
495 isReferrersClass, isVolatile,
496 false);
497 if (fastPath && !SLOW_FIELD_PATH) {
498 DCHECK_GE(fieldOffset, 0);
499 int rBase;
buzbee31a4a6f2012-02-28 15:36:15 -0800500 if (isReferrersClass) {
501 // Fast path, static storage base is this method's class
buzbeee1965672012-03-11 18:39:19 -0700502 RegLocation rlMethod = loadCurrMethod(cUnit);
buzbee31a4a6f2012-02-28 15:36:15 -0800503 rBase = oatAllocTemp(cUnit);
buzbeee1965672012-03-11 18:39:19 -0700504 loadWordDisp(cUnit, rlMethod.lowReg,
buzbee31a4a6f2012-02-28 15:36:15 -0800505 Method::DeclaringClassOffset().Int32Value(), rBase);
506 } else {
507 // Medium path, static storage base in a different class which
508 // requires checks that the other class is initialized
509 DCHECK_GE(ssbIndex, 0);
510 // May do runtime call so everything to home locations.
511 oatFlushAllRegs(cUnit);
512 // Using fixed register to sync with possible call to runtime
513 // support
buzbeee1965672012-03-11 18:39:19 -0700514 int rMethod = rARG1;
buzbee31a4a6f2012-02-28 15:36:15 -0800515 oatLockTemp(cUnit, rMethod);
516 loadCurrMethodDirect(cUnit, rMethod);
517 rBase = rARG0;
518 oatLockTemp(cUnit, rBase);
519 loadWordDisp(cUnit, rMethod,
520 Method::DexCacheInitializedStaticStorageOffset().Int32Value(),
521 rBase);
522 loadWordDisp(cUnit, rBase,
523 Array::DataOffset(sizeof(Object*)).Int32Value() +
524 sizeof(int32_t*) * ssbIndex,
525 rBase);
526 // rBase now points at appropriate static storage base (Class*)
527 // or NULL if not initialized. Check for NULL and call helper if NULL.
528 // TUNING: fast path should fall through
buzbee82488f52012-03-02 08:20:26 -0800529 LIR* branchOver = opCmpImmBranch(cUnit, kCondNe, rBase, 0, NULL);
buzbee31a4a6f2012-02-28 15:36:15 -0800530 int rTgt = loadHelper(cUnit, OFFSETOF_MEMBER(Thread,
531 pInitializeStaticStorage));
532 loadConstant(cUnit, rARG0, ssbIndex);
533 callRuntimeHelper(cUnit, rTgt);
534#if defined(TARGET_MIPS)
535 // For Arm, rRET0 = rARG0 = rBASE, for Mips, we need to copy
buzbee82488f52012-03-02 08:20:26 -0800536 opRegCopy(cUnit, rBase, rRET0);
buzbee31a4a6f2012-02-28 15:36:15 -0800537#endif
538 LIR* skipTarget = newLIR0(cUnit, kPseudoTargetLabel);
buzbee31a4a6f2012-02-28 15:36:15 -0800539 branchOver->target = (LIR*)skipTarget;
buzbeee1965672012-03-11 18:39:19 -0700540 oatFreeTemp(cUnit, rMethod);
buzbee31a4a6f2012-02-28 15:36:15 -0800541 }
542 // rBase now holds static storage base
buzbee31a4a6f2012-02-28 15:36:15 -0800543 rlDest = isLongOrDouble ? oatGetDestWide(cUnit, mir, 0, 1)
544 : oatGetDest(cUnit, mir, 0);
545 RegLocation rlResult = oatEvalLoc(cUnit, rlDest, kAnyReg, true);
546 if (isVolatile) {
547 oatGenMemBarrier(cUnit, kSY);
548 }
549 if (isLongOrDouble) {
550 loadBaseDispWide(cUnit, NULL, rBase, fieldOffset, rlResult.lowReg,
551 rlResult.highReg, INVALID_SREG);
552 } else {
553 loadWordDisp(cUnit, rBase, fieldOffset, rlResult.lowReg);
554 }
555 oatFreeTemp(cUnit, rBase);
556 if (isLongOrDouble) {
557 storeValueWide(cUnit, rlDest, rlResult);
558 } else {
559 storeValue(cUnit, rlDest, rlResult);
560 }
561 } else {
562 oatFlushAllRegs(cUnit); // Everything to home locations
563 int getterOffset = isLongOrDouble ? OFFSETOF_MEMBER(Thread, pGet64Static) :
564 (isObject ? OFFSETOF_MEMBER(Thread, pGetObjStatic)
565 : OFFSETOF_MEMBER(Thread, pGet32Static));
566 int rTgt = loadHelper(cUnit, getterOffset);
567 loadConstant(cUnit, rARG0, fieldIdx);
568 callRuntimeHelper(cUnit, rTgt);
569 if (isLongOrDouble) {
570 RegLocation rlResult = oatGetReturnWide(cUnit);
571 storeValueWide(cUnit, rlDest, rlResult);
572 } else {
573 RegLocation rlResult = oatGetReturn(cUnit);
574 storeValue(cUnit, rlDest, rlResult);
575 }
576 }
577}
578
579
580// Debugging routine - if null target, branch to DebugMe
581void genShowTarget(CompilationUnit* cUnit)
582{
buzbeea7678db2012-03-05 15:35:46 -0800583#if defined(TARGET_X86)
584 UNIMPLEMENTED(WARNING) << "genShowTarget";
585#else
buzbee0398c422012-03-02 15:22:47 -0800586 LIR* branchOver = opCmpImmBranch(cUnit, kCondNe, rINVOKE_TGT, 0, NULL);
buzbee31a4a6f2012-02-28 15:36:15 -0800587 loadWordDisp(cUnit, rSELF,
buzbee0398c422012-03-02 15:22:47 -0800588 OFFSETOF_MEMBER(Thread, pDebugMe), rINVOKE_TGT);
buzbee31a4a6f2012-02-28 15:36:15 -0800589 LIR* target = newLIR0(cUnit, kPseudoTargetLabel);
buzbee31a4a6f2012-02-28 15:36:15 -0800590 branchOver->target = (LIR*)target;
buzbeea7678db2012-03-05 15:35:46 -0800591#endif
buzbee31a4a6f2012-02-28 15:36:15 -0800592}
593
594void genThrowVerificationError(CompilationUnit* cUnit, MIR* mir)
595{
596 int rTgt = loadHelper(cUnit, OFFSETOF_MEMBER(Thread,
597 pThrowVerificationErrorFromCode));
598 loadConstant(cUnit, rARG0, mir->dalvikInsn.vA);
599 loadConstant(cUnit, rARG1, mir->dalvikInsn.vB);
600 callRuntimeHelper(cUnit, rTgt);
601}
602
603void handleSuspendLaunchpads(CompilationUnit *cUnit)
604{
605 LIR** suspendLabel =
606 (LIR **) cUnit->suspendLaunchpads.elemList;
607 int numElems = cUnit->suspendLaunchpads.numUsed;
608
609 for (int i = 0; i < numElems; i++) {
buzbeec5159d52012-03-03 11:48:39 -0800610 oatResetRegPool(cUnit);
buzbee31a4a6f2012-02-28 15:36:15 -0800611 LIR* lab = suspendLabel[i];
612 LIR* resumeLab = (LIR*)lab->operands[0];
613 cUnit->currentDalvikOffset = lab->operands[1];
614 oatAppendLIR(cUnit, (LIR *)lab);
615 int rTgt = loadHelper(cUnit, OFFSETOF_MEMBER(Thread,
616 pTestSuspendFromCode));
buzbee31a4a6f2012-02-28 15:36:15 -0800617 opReg(cUnit, kOpBlx, rTgt);
buzbee82488f52012-03-02 08:20:26 -0800618 opUnconditionalBranch(cUnit, resumeLab);
buzbee31a4a6f2012-02-28 15:36:15 -0800619 }
620}
621
622void handleThrowLaunchpads(CompilationUnit *cUnit)
623{
624 LIR** throwLabel = (LIR **) cUnit->throwLaunchpads.elemList;
625 int numElems = cUnit->throwLaunchpads.numUsed;
626 int i;
627
628 for (i = 0; i < numElems; i++) {
buzbeec5159d52012-03-03 11:48:39 -0800629 oatResetRegPool(cUnit);
buzbee31a4a6f2012-02-28 15:36:15 -0800630 LIR* lab = throwLabel[i];
631 cUnit->currentDalvikOffset = lab->operands[1];
632 oatAppendLIR(cUnit, (LIR *)lab);
633 int funcOffset = 0;
634 int v1 = lab->operands[2];
635 int v2 = lab->operands[3];
636 switch(lab->operands[0]) {
637 case kThrowNullPointer:
638 funcOffset = OFFSETOF_MEMBER(Thread, pThrowNullPointerFromCode);
639 break;
640 case kThrowArrayBounds:
buzbee5de34942012-03-01 14:51:57 -0800641 if (v2 != rARG0) {
buzbee82488f52012-03-02 08:20:26 -0800642 opRegCopy(cUnit, rARG0, v1);
643 opRegCopy(cUnit, rARG1, v2);
buzbee31a4a6f2012-02-28 15:36:15 -0800644 } else {
buzbee5de34942012-03-01 14:51:57 -0800645 if (v1 == rARG1) {
buzbee31a4a6f2012-02-28 15:36:15 -0800646#if defined(TARGET_ARM)
647 int rTmp = r12;
648#else
649 int rTmp = oatAllocTemp(cUnit);
650#endif
buzbee82488f52012-03-02 08:20:26 -0800651 opRegCopy(cUnit, rTmp, v1);
652 opRegCopy(cUnit, rARG1, v2);
653 opRegCopy(cUnit, rARG0, rTmp);
buzbee31a4a6f2012-02-28 15:36:15 -0800654 } else {
buzbee82488f52012-03-02 08:20:26 -0800655 opRegCopy(cUnit, rARG1, v2);
656 opRegCopy(cUnit, rARG0, v1);
buzbee31a4a6f2012-02-28 15:36:15 -0800657 }
658 }
659 funcOffset = OFFSETOF_MEMBER(Thread, pThrowArrayBoundsFromCode);
660 break;
661 case kThrowDivZero:
662 funcOffset = OFFSETOF_MEMBER(Thread, pThrowDivZeroFromCode);
663 break;
664 case kThrowVerificationError:
665 loadConstant(cUnit, rARG0, v1);
666 loadConstant(cUnit, rARG1, v2);
667 funcOffset =
668 OFFSETOF_MEMBER(Thread, pThrowVerificationErrorFromCode);
669 break;
670 case kThrowNegArraySize:
buzbee82488f52012-03-02 08:20:26 -0800671 opRegCopy(cUnit, rARG0, v1);
buzbee31a4a6f2012-02-28 15:36:15 -0800672 funcOffset =
673 OFFSETOF_MEMBER(Thread, pThrowNegArraySizeFromCode);
674 break;
675 case kThrowNoSuchMethod:
buzbee82488f52012-03-02 08:20:26 -0800676 opRegCopy(cUnit, rARG0, v1);
buzbee31a4a6f2012-02-28 15:36:15 -0800677 funcOffset =
678 OFFSETOF_MEMBER(Thread, pThrowNoSuchMethodFromCode);
679 break;
680 case kThrowStackOverflow:
681 funcOffset =
682 OFFSETOF_MEMBER(Thread, pThrowStackOverflowFromCode);
683 // Restore stack alignment
684 opRegImm(cUnit, kOpAdd, rSP,
685 (cUnit->numCoreSpills + cUnit->numFPSpills) * 4);
686 break;
687 default:
688 LOG(FATAL) << "Unexpected throw kind: " << lab->operands[0];
689 }
690 int rTgt = loadHelper(cUnit, funcOffset);
691 callRuntimeHelper(cUnit, rTgt);
buzbee0398c422012-03-02 15:22:47 -0800692 oatFreeTemp(cUnit, rTgt);
buzbee31a4a6f2012-02-28 15:36:15 -0800693 }
694}
695
696/* Needed by the Assembler */
697void oatSetupResourceMasks(LIR* lir)
698{
699 setupResourceMasks(lir);
700}
701
702void genIGet(CompilationUnit* cUnit, MIR* mir, OpSize size,
703 RegLocation rlDest, RegLocation rlObj,
704 bool isLongOrDouble, bool isObject)
705{
706 int fieldOffset;
707 bool isVolatile;
708 uint32_t fieldIdx = mir->dalvikInsn.vC;
709
710 OatCompilationUnit mUnit(cUnit->class_loader, cUnit->class_linker,
711 *cUnit->dex_file, *cUnit->dex_cache,
712 cUnit->code_item, cUnit->method_idx,
713 cUnit->access_flags);
714
715 bool fastPath = cUnit->compiler->ComputeInstanceFieldInfo(fieldIdx, &mUnit,
716 fieldOffset, isVolatile, false);
717
718 if (fastPath && !SLOW_FIELD_PATH) {
719 RegLocation rlResult;
720 RegisterClass regClass = oatRegClassBySize(size);
721 DCHECK_GE(fieldOffset, 0);
722 rlObj = loadValue(cUnit, rlObj, kCoreReg);
723 if (isLongOrDouble) {
724 DCHECK(rlDest.wide);
725 genNullCheck(cUnit, rlObj.sRegLow, rlObj.lowReg, mir);/* null? */
Ian Rogersb5d09b22012-03-06 22:14:17 -0800726#if defined(TARGET_X86)
727 rlResult = oatEvalLoc(cUnit, rlDest, regClass, true);
728 genNullCheck(cUnit, rlObj.sRegLow, rlObj.lowReg, mir);/* null? */
729 loadBaseDispWide(cUnit, mir, rlObj.lowReg, fieldOffset, rlResult.lowReg,
730 rlResult.highReg, rlObj.sRegLow);
731 if (isVolatile) {
732 oatGenMemBarrier(cUnit, kSY);
733 }
734#else
buzbee31a4a6f2012-02-28 15:36:15 -0800735 int regPtr = oatAllocTemp(cUnit);
736 opRegRegImm(cUnit, kOpAdd, regPtr, rlObj.lowReg, fieldOffset);
737 rlResult = oatEvalLoc(cUnit, rlDest, regClass, true);
738 loadPair(cUnit, regPtr, rlResult.lowReg, rlResult.highReg);
739 if (isVolatile) {
740 oatGenMemBarrier(cUnit, kSY);
741 }
742 oatFreeTemp(cUnit, regPtr);
Ian Rogersb5d09b22012-03-06 22:14:17 -0800743#endif
buzbee31a4a6f2012-02-28 15:36:15 -0800744 storeValueWide(cUnit, rlDest, rlResult);
745 } else {
746 rlResult = oatEvalLoc(cUnit, rlDest, regClass, true);
747 genNullCheck(cUnit, rlObj.sRegLow, rlObj.lowReg, mir);/* null? */
748 loadBaseDisp(cUnit, mir, rlObj.lowReg, fieldOffset, rlResult.lowReg,
749 kWord, rlObj.sRegLow);
750 if (isVolatile) {
751 oatGenMemBarrier(cUnit, kSY);
752 }
753 storeValue(cUnit, rlDest, rlResult);
754 }
755 } else {
756 int getterOffset = isLongOrDouble ? OFFSETOF_MEMBER(Thread, pGet64Instance) :
757 (isObject ? OFFSETOF_MEMBER(Thread, pGetObjInstance)
758 : OFFSETOF_MEMBER(Thread, pGet32Instance));
759 int rTgt = loadHelper(cUnit, getterOffset);
760 loadValueDirect(cUnit, rlObj, rARG1);
761 loadConstant(cUnit, rARG0, fieldIdx);
762 callRuntimeHelper(cUnit, rTgt);
763 if (isLongOrDouble) {
764 RegLocation rlResult = oatGetReturnWide(cUnit);
765 storeValueWide(cUnit, rlDest, rlResult);
766 } else {
767 RegLocation rlResult = oatGetReturn(cUnit);
768 storeValue(cUnit, rlDest, rlResult);
769 }
770 }
771}
772
773void genIPut(CompilationUnit* cUnit, MIR* mir, OpSize size, RegLocation rlSrc,
774 RegLocation rlObj, bool isLongOrDouble, bool isObject)
775{
776 int fieldOffset;
777 bool isVolatile;
778 uint32_t fieldIdx = mir->dalvikInsn.vC;
779
780 OatCompilationUnit mUnit(cUnit->class_loader, cUnit->class_linker,
781 *cUnit->dex_file, *cUnit->dex_cache,
782 cUnit->code_item, cUnit->method_idx,
783 cUnit->access_flags);
784
785 bool fastPath = cUnit->compiler->ComputeInstanceFieldInfo(fieldIdx, &mUnit,
786 fieldOffset, isVolatile, true);
787 if (fastPath && !SLOW_FIELD_PATH) {
788 RegisterClass regClass = oatRegClassBySize(size);
789 DCHECK_GE(fieldOffset, 0);
790 rlObj = loadValue(cUnit, rlObj, kCoreReg);
791 if (isLongOrDouble) {
792 int regPtr;
793 rlSrc = loadValueWide(cUnit, rlSrc, kAnyReg);
794 genNullCheck(cUnit, rlObj.sRegLow, rlObj.lowReg, mir);/* null? */
795 regPtr = oatAllocTemp(cUnit);
796 opRegRegImm(cUnit, kOpAdd, regPtr, rlObj.lowReg, fieldOffset);
797 if (isVolatile) {
798 oatGenMemBarrier(cUnit, kST);
799 }
800 storePair(cUnit, regPtr, rlSrc.lowReg, rlSrc.highReg);
801 if (isVolatile) {
802 oatGenMemBarrier(cUnit, kSY);
803 }
804 oatFreeTemp(cUnit, regPtr);
805 } else {
806 rlSrc = loadValue(cUnit, rlSrc, regClass);
807 genNullCheck(cUnit, rlObj.sRegLow, rlObj.lowReg, mir);/* null? */
808 if (isVolatile) {
809 oatGenMemBarrier(cUnit, kST);
810 }
811 storeBaseDisp(cUnit, rlObj.lowReg, fieldOffset, rlSrc.lowReg, kWord);
812 if (isVolatile) {
813 oatGenMemBarrier(cUnit, kSY);
814 }
815 }
816 } else {
817 int setterOffset = isLongOrDouble ? OFFSETOF_MEMBER(Thread, pSet64Instance) :
818 (isObject ? OFFSETOF_MEMBER(Thread, pSetObjInstance)
819 : OFFSETOF_MEMBER(Thread, pSet32Instance));
820 int rTgt = loadHelper(cUnit, setterOffset);
821 loadValueDirect(cUnit, rlObj, rARG1);
822 if (isLongOrDouble) {
Ian Rogersb5d09b22012-03-06 22:14:17 -0800823#if defined(TARGET_X86)
824 UNIMPLEMENTED(FATAL);
825#else
buzbee31a4a6f2012-02-28 15:36:15 -0800826 loadValueDirectWide(cUnit, rlSrc, rARG2, rARG3);
Ian Rogersb5d09b22012-03-06 22:14:17 -0800827#endif
buzbee31a4a6f2012-02-28 15:36:15 -0800828 } 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;
buzbeee1965672012-03-11 18:39:19 -0700840 RegLocation rlMethod = loadCurrMethod(cUnit);
buzbee31a4a6f2012-02-28 15:36:15 -0800841 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));
buzbeee1965672012-03-11 18:39:19 -0700851 opRegCopy(cUnit, rARG1, rlMethod.lowReg);
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();
buzbeee1965672012-03-11 18:39:19 -0700860 loadWordDisp(cUnit, rlMethod.lowReg, dex_cache_offset, resReg);
buzbee31a4a6f2012-02-28 15:36:15 -0800861 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);
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));
buzbeee1965672012-03-11 18:39:19 -0700879 opRegCopy(cUnit, rARG1, rlMethod.lowReg);
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);
buzbee31a4a6f2012-02-28 15:36:15 -0800886 branch1->target = (LIR*)target1;
887 branch2->target = (LIR*)target2;
888 } else {
889 // Fast path, we're done - just store result
890 storeValue(cUnit, rlDest, rlResult);
891 }
892 }
893}
894void genConstString(CompilationUnit* cUnit, MIR* mir, RegLocation rlDest,
895 RegLocation rlSrc)
896{
897 /* NOTE: Most strings should be available at compile time */
898 uint32_t string_idx = mir->dalvikInsn.vB;
899 int32_t offset_of_string = Array::DataOffset(sizeof(String*)).Int32Value() +
900 (sizeof(String*) * string_idx);
901 if (!cUnit->compiler->CanAssumeStringIsPresentInDexCache(
902 cUnit->dex_cache, string_idx) || SLOW_STRING_PATH) {
903 // slow path, resolve string if not in dex cache
904 oatFlushAllRegs(cUnit);
905 oatLockCallTemps(cUnit); // Using explicit registers
906 loadCurrMethodDirect(cUnit, rARG2);
907 loadWordDisp(cUnit, rARG2,
908 Method::DexCacheStringsOffset().Int32Value(), rARG0);
909 // Might call out to helper, which will return resolved string in rRET0
910 int rTgt = loadHelper(cUnit, OFFSETOF_MEMBER(Thread,
911 pResolveStringFromCode));
912 loadWordDisp(cUnit, rRET0, offset_of_string, rARG0);
913 loadConstant(cUnit, rARG1, string_idx);
914#if defined(TARGET_ARM)
915 opRegImm(cUnit, kOpCmp, rRET0, 0); // Is resolved?
916 genBarrier(cUnit);
917 // For testing, always force through helper
918 if (!EXERCISE_SLOWEST_STRING_PATH) {
buzbee82488f52012-03-02 08:20:26 -0800919 opIT(cUnit, kArmCondEq, "T");
buzbee31a4a6f2012-02-28 15:36:15 -0800920 }
buzbee82488f52012-03-02 08:20:26 -0800921 opRegCopy(cUnit, rARG0, rARG2); // .eq
buzbee31a4a6f2012-02-28 15:36:15 -0800922 opReg(cUnit, kOpBlx, rTgt); // .eq, helper(Method*, string_idx)
923#else
buzbee82488f52012-03-02 08:20:26 -0800924 LIR* branch = opCmpImmBranch(cUnit, kCondNe, rRET0, 0, NULL);
925 opRegCopy(cUnit, rARG0, rARG2); // .eq
buzbee31a4a6f2012-02-28 15:36:15 -0800926 opReg(cUnit, kOpBlx, rTgt);
927 LIR* target = newLIR0(cUnit, kPseudoTargetLabel);
buzbee31a4a6f2012-02-28 15:36:15 -0800928 branch->target = target;
929#endif
930 genBarrier(cUnit);
buzbee86a4bce2012-03-06 18:15:00 -0800931 storeValue(cUnit, rlDest, oatGetReturn(cUnit));
buzbee31a4a6f2012-02-28 15:36:15 -0800932 } else {
buzbeee1965672012-03-11 18:39:19 -0700933 RegLocation rlMethod = loadCurrMethod(cUnit);
buzbee31a4a6f2012-02-28 15:36:15 -0800934 int resReg = oatAllocTemp(cUnit);
935 RegLocation rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
buzbeee1965672012-03-11 18:39:19 -0700936 loadWordDisp(cUnit, rlMethod.lowReg,
buzbee31a4a6f2012-02-28 15:36:15 -0800937 Method::DexCacheStringsOffset().Int32Value(), resReg);
938 loadWordDisp(cUnit, resReg, offset_of_string, rlResult.lowReg);
939 storeValue(cUnit, rlDest, rlResult);
940 }
941}
942
943/*
944 * Let helper function take care of everything. Will
945 * call Class::NewInstanceFromCode(type_idx, method);
946 */
947void genNewInstance(CompilationUnit* cUnit, MIR* mir, RegLocation rlDest)
948{
949 oatFlushAllRegs(cUnit); /* Everything to home location */
950 uint32_t type_idx = mir->dalvikInsn.vB;
951 // alloc will always check for resolution, do we also need to verify
952 // access because the verifier was unable to?
953 int rTgt;
954 if (cUnit->compiler->CanAccessInstantiableTypeWithoutChecks(
955 cUnit->method_idx, cUnit->dex_cache, *cUnit->dex_file, type_idx)) {
956 rTgt = loadHelper(cUnit, OFFSETOF_MEMBER(Thread, pAllocObjectFromCode));
957 } else {
958 rTgt = loadHelper(cUnit, OFFSETOF_MEMBER(Thread,
959 pAllocObjectFromCodeWithAccessCheck));
960 }
961 loadCurrMethodDirect(cUnit, rARG1); // arg1 <= Method*
962 loadConstant(cUnit, rARG0, type_idx); // arg0 <- type_idx
963 callRuntimeHelper(cUnit, rTgt);
964 RegLocation rlResult = oatGetReturn(cUnit);
965 storeValue(cUnit, rlDest, rlResult);
966}
967
968void genInstanceof(CompilationUnit* cUnit, MIR* mir, RegLocation rlDest,
969 RegLocation rlSrc)
970{
971 oatFlushAllRegs(cUnit);
972 // May generate a call - use explicit registers
973 oatLockCallTemps(cUnit);
974 uint32_t type_idx = mir->dalvikInsn.vC;
buzbee5de34942012-03-01 14:51:57 -0800975 loadCurrMethodDirect(cUnit, rARG1); // rARG1 <= current Method*
buzbee31a4a6f2012-02-28 15:36:15 -0800976 int classReg = rARG2; // rARG2 will hold the Class*
977 if (!cUnit->compiler->CanAccessTypeWithoutChecks(cUnit->method_idx,
978 cUnit->dex_cache,
979 *cUnit->dex_file,
980 type_idx)) {
981 // Check we have access to type_idx and if not throw IllegalAccessError,
buzbee5de34942012-03-01 14:51:57 -0800982 // returns Class* in rARG0
buzbee31a4a6f2012-02-28 15:36:15 -0800983 int rTgt = loadHelper(cUnit, OFFSETOF_MEMBER(Thread,
984 pInitializeTypeAndVerifyAccessFromCode));
985 loadConstant(cUnit, rARG0, type_idx);
986 callRuntimeHelper(cUnit, rTgt); // InitializeTypeAndVerifyAccess(idx, method)
buzbee82488f52012-03-02 08:20:26 -0800987 opRegCopy(cUnit, classReg, rRET0); // Align usage with fast path
buzbee5de34942012-03-01 14:51:57 -0800988 loadValueDirectFixed(cUnit, rlSrc, rARG0); // rARG0 <= ref
buzbee31a4a6f2012-02-28 15:36:15 -0800989 } else {
buzbee5de34942012-03-01 14:51:57 -0800990 // Load dex cache entry into classReg (rARG2)
buzbee31a4a6f2012-02-28 15:36:15 -0800991 loadValueDirectFixed(cUnit, rlSrc, rARG0); // rARG0 <= ref
992 loadWordDisp(cUnit, rARG1,
993 Method::DexCacheResolvedTypesOffset().Int32Value(),
994 classReg);
995 int32_t offset_of_type =
996 Array::DataOffset(sizeof(Class*)).Int32Value() + (sizeof(Class*)
997 * type_idx);
998 loadWordDisp(cUnit, classReg, offset_of_type, classReg);
999 if (!cUnit->compiler->CanAssumeTypeIsPresentInDexCache(
1000 cUnit->dex_cache, type_idx)) {
1001 // Need to test presence of type in dex cache at runtime
buzbee82488f52012-03-02 08:20:26 -08001002 LIR* hopBranch = opCmpImmBranch(cUnit, kCondNe, classReg, 0, NULL);
buzbee31a4a6f2012-02-28 15:36:15 -08001003 // Not resolved
1004 // Call out to helper, which will return resolved type in rRET0
1005 int rTgt = loadHelper(cUnit, OFFSETOF_MEMBER(Thread,
1006 pInitializeTypeFromCode));
1007 loadConstant(cUnit, rARG0, type_idx);
1008 callRuntimeHelper(cUnit, rTgt); // InitializeTypeFromCode(idx, method)
buzbee82488f52012-03-02 08:20:26 -08001009 opRegCopy(cUnit, rARG2, rRET0); // Align usage with fast path
buzbee31a4a6f2012-02-28 15:36:15 -08001010 loadValueDirectFixed(cUnit, rlSrc, rARG0); /* reload Ref */
1011 // Rejoin code paths
1012 LIR* hopTarget = newLIR0(cUnit, kPseudoTargetLabel);
buzbee31a4a6f2012-02-28 15:36:15 -08001013 hopBranch->target = (LIR*)hopTarget;
1014 }
1015 }
1016 /* rARG0 is ref, rARG2 is class. If ref==null, use directly as bool result */
buzbee82488f52012-03-02 08:20:26 -08001017 LIR* branch1 = opCmpImmBranch(cUnit, kCondEq, rARG0, 0, NULL);
buzbee31a4a6f2012-02-28 15:36:15 -08001018 /* load object->clazz */
1019 DCHECK_EQ(Object::ClassOffset().Int32Value(), 0);
1020 loadWordDisp(cUnit, rARG0, Object::ClassOffset().Int32Value(), rARG1);
1021 /* rARG0 is ref, rARG1 is ref->clazz, rARG2 is class */
buzbee0398c422012-03-02 15:22:47 -08001022#if defined(TARGET_ARM)
1023 /* Uses conditional nullification */
buzbee31a4a6f2012-02-28 15:36:15 -08001024 int rTgt = loadHelper(cUnit, OFFSETOF_MEMBER(Thread,
1025 pInstanceofNonTrivialFromCode));
buzbee31a4a6f2012-02-28 15:36:15 -08001026 opRegReg(cUnit, kOpCmp, rARG1, rARG2); // Same?
buzbee82488f52012-03-02 08:20:26 -08001027 opIT(cUnit, kArmCondEq, "EE"); // if-convert the test
buzbee31a4a6f2012-02-28 15:36:15 -08001028 loadConstant(cUnit, rARG0, 1); // .eq case - load true
buzbee82488f52012-03-02 08:20:26 -08001029 opRegCopy(cUnit, rARG0, rARG2); // .ne case - arg0 <= class
buzbee31a4a6f2012-02-28 15:36:15 -08001030 opReg(cUnit, kOpBlx, rTgt); // .ne case: helper(class, ref->class)
buzbee31a4a6f2012-02-28 15:36:15 -08001031#else
buzbee0398c422012-03-02 15:22:47 -08001032 /* Uses branchovers */
1033 loadConstant(cUnit, rARG0, 1); // assume true
1034 LIR* branchover = opCmpBranch(cUnit, kCondEq, rARG1, rARG2, NULL);
1035 int rTgt = loadHelper(cUnit, OFFSETOF_MEMBER(Thread,
1036 pInstanceofNonTrivialFromCode));
1037 opRegCopy(cUnit, rARG0, rARG2); // .ne case - arg0 <= class
1038 opReg(cUnit, kOpBlx, rTgt); // .ne case: helper(class, ref->class)
buzbee31a4a6f2012-02-28 15:36:15 -08001039#endif
buzbee0398c422012-03-02 15:22:47 -08001040 oatClobberCalleeSave(cUnit);
1041 /* branch targets here */
buzbee31a4a6f2012-02-28 15:36:15 -08001042 LIR* target = newLIR0(cUnit, kPseudoTargetLabel);
buzbee31a4a6f2012-02-28 15:36:15 -08001043 RegLocation rlResult = oatGetReturn(cUnit);
1044 storeValue(cUnit, rlDest, rlResult);
buzbee0398c422012-03-02 15:22:47 -08001045 branch1->target = target;
1046#if !defined(TARGET_ARM)
1047 branchover->target = target;
1048#endif
buzbee31a4a6f2012-02-28 15:36:15 -08001049}
1050
1051void genCheckCast(CompilationUnit* cUnit, MIR* mir, RegLocation rlSrc)
1052{
1053 oatFlushAllRegs(cUnit);
1054 // May generate a call - use explicit registers
1055 oatLockCallTemps(cUnit);
1056 uint32_t type_idx = mir->dalvikInsn.vB;
1057 loadCurrMethodDirect(cUnit, rARG1); // rARG1 <= current Method*
1058 int classReg = rARG2; // rARG2 will hold the Class*
1059 if (!cUnit->compiler->CanAccessTypeWithoutChecks(cUnit->method_idx,
1060 cUnit->dex_cache,
1061 *cUnit->dex_file,
1062 type_idx)) {
1063 // Check we have access to type_idx and if not throw IllegalAccessError,
1064 // returns Class* in rRET0
1065 int rTgt = loadHelper(cUnit, OFFSETOF_MEMBER(Thread,
1066 pInitializeTypeAndVerifyAccessFromCode));
1067 loadConstant(cUnit, rARG0, type_idx);
1068 callRuntimeHelper(cUnit, rTgt); // InitializeTypeAndVerifyAccess(idx, method)
buzbee82488f52012-03-02 08:20:26 -08001069 opRegCopy(cUnit, classReg, rRET0); // Align usage with fast path
buzbee31a4a6f2012-02-28 15:36:15 -08001070 } else {
1071 // Load dex cache entry into classReg (rARG2)
1072 loadWordDisp(cUnit, rARG1,
1073 Method::DexCacheResolvedTypesOffset().Int32Value(),
1074 classReg);
1075 int32_t offset_of_type =
1076 Array::DataOffset(sizeof(Class*)).Int32Value() +
1077 (sizeof(Class*) * type_idx);
1078 loadWordDisp(cUnit, classReg, offset_of_type, classReg);
1079 if (!cUnit->compiler->CanAssumeTypeIsPresentInDexCache(
1080 cUnit->dex_cache, type_idx)) {
1081 // Need to test presence of type in dex cache at runtime
buzbee82488f52012-03-02 08:20:26 -08001082 LIR* hopBranch = opCmpImmBranch(cUnit, kCondNe, classReg, 0, NULL);
buzbee31a4a6f2012-02-28 15:36:15 -08001083 // Not resolved
buzbee5de34942012-03-01 14:51:57 -08001084 // Call out to helper, which will return resolved type in rARG0
1085 int rTgt = loadHelper(cUnit, OFFSETOF_MEMBER(Thread, pInitializeTypeFromCode));
1086 loadConstant(cUnit, rARG0, type_idx);
1087 callRuntimeHelper(cUnit, rTgt); // InitializeTypeFromCode(idx, method)
buzbee82488f52012-03-02 08:20:26 -08001088 opRegCopy(cUnit, classReg, rARG0); // Align usage with fast path
buzbee31a4a6f2012-02-28 15:36:15 -08001089 // Rejoin code paths
1090 LIR* hopTarget = newLIR0(cUnit, kPseudoTargetLabel);
buzbee31a4a6f2012-02-28 15:36:15 -08001091 hopBranch->target = (LIR*)hopTarget;
1092 }
1093 }
buzbee5de34942012-03-01 14:51:57 -08001094 // At this point, classReg (rARG2) has class
buzbee31a4a6f2012-02-28 15:36:15 -08001095 loadValueDirectFixed(cUnit, rlSrc, rARG0); // rARG0 <= ref
1096 /* Null is OK - continue */
buzbee82488f52012-03-02 08:20:26 -08001097 LIR* branch1 = opCmpImmBranch(cUnit, kCondEq, rARG0, 0, NULL);
buzbee31a4a6f2012-02-28 15:36:15 -08001098 /* load object->clazz */
1099 DCHECK_EQ(Object::ClassOffset().Int32Value(), 0);
1100 loadWordDisp(cUnit, rARG0, Object::ClassOffset().Int32Value(), rARG1);
1101 /* rARG1 now contains object->clazz */
1102 int rTgt = loadHelper(cUnit, OFFSETOF_MEMBER(Thread,
1103 pCheckCastFromCode));
Ian Rogersb5d09b22012-03-06 22:14:17 -08001104#if defined(TARGET_MIPS) || defined(TARGET_X86)
buzbee82488f52012-03-02 08:20:26 -08001105 LIR* branch2 = opCmpBranch(cUnit, kCondEq, rARG1, classReg, NULL);
buzbee5de34942012-03-01 14:51:57 -08001106#else
buzbee31a4a6f2012-02-28 15:36:15 -08001107 opRegReg(cUnit, kOpCmp, rARG1, classReg);
buzbee82488f52012-03-02 08:20:26 -08001108 LIR* branch2 = opCondBranch(cUnit, kCondEq, NULL); /* If eq, trivial yes */
buzbee5de34942012-03-01 14:51:57 -08001109#endif
buzbee82488f52012-03-02 08:20:26 -08001110 opRegCopy(cUnit, rARG0, rARG1);
1111 opRegCopy(cUnit, rARG1, rARG2);
buzbee31a4a6f2012-02-28 15:36:15 -08001112 callRuntimeHelper(cUnit, rTgt);
1113 /* branch target here */
1114 LIR* target = newLIR0(cUnit, kPseudoTargetLabel);
buzbee31a4a6f2012-02-28 15:36:15 -08001115 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);
buzbee31a4a6f2012-02-28 15:36:15 -08001201
1202 if (size == kLong || size == kDouble) {
1203 dataOffset = Array::DataOffset(sizeof(int64_t)).Int32Value();
1204 } else {
1205 dataOffset = Array::DataOffset(sizeof(int32_t)).Int32Value();
1206 }
1207
1208 /* null object? */
1209 genNullCheck(cUnit, rlArray.sRegLow, rlArray.lowReg, mir);
1210
Ian Rogersb5d09b22012-03-06 22:14:17 -08001211#if defined(TARGET_X86)
1212 if (!(mir->optimizationFlags & MIR_IGNORE_RANGE_CHECK)) {
1213 /* if (rlIndex >= [rlArray + lenOffset]) goto kThrowArrayBounds */
1214 genRegMemCheck(cUnit, kCondUge, rlIndex.lowReg, rlArray.lowReg,
1215 lenOffset, mir, kThrowArrayBounds);
1216 }
1217 if ((size == kLong) || (size == kDouble)) {
1218 rlResult = oatEvalLoc(cUnit, rlDest, regClass, true);
1219 loadBaseIndexedDisp(cUnit, NULL, rlArray.lowReg, rlIndex.lowReg, scale, dataOffset,
1220 rlResult.lowReg, rlResult.highReg, size, INVALID_SREG);
buzbee31a4a6f2012-02-28 15:36:15 -08001221
Ian Rogersb5d09b22012-03-06 22:14:17 -08001222 storeValueWide(cUnit, rlDest, rlResult);
1223 } else {
1224 rlResult = oatEvalLoc(cUnit, rlDest, regClass, true);
1225
1226 loadBaseIndexedDisp(cUnit, NULL, rlArray.lowReg, rlIndex.lowReg, scale, dataOffset,
1227 rlResult.lowReg, INVALID_REG, size, INVALID_SREG);
1228
1229 storeValue(cUnit, rlDest, rlResult);
1230 }
1231#else
1232 int regPtr = oatAllocTemp(cUnit);
buzbee31a4a6f2012-02-28 15:36:15 -08001233 if (!(mir->optimizationFlags & MIR_IGNORE_RANGE_CHECK)) {
1234 int regLen = oatAllocTemp(cUnit);
1235 /* Get len */
1236 loadWordDisp(cUnit, rlArray.lowReg, lenOffset, regLen);
1237 /* regPtr -> array data */
1238 opRegRegImm(cUnit, kOpAdd, regPtr, rlArray.lowReg, dataOffset);
Ian Rogersb5d09b22012-03-06 22:14:17 -08001239 // TODO: change kCondCS to a more meaningful name, is the sense of
1240 // carry-set/clear flipped?
buzbee31a4a6f2012-02-28 15:36:15 -08001241 genRegRegCheck(cUnit, kCondCs, rlIndex.lowReg, regLen, mir,
1242 kThrowArrayBounds);
1243 oatFreeTemp(cUnit, regLen);
1244 } else {
1245 /* regPtr -> array data */
1246 opRegRegImm(cUnit, kOpAdd, regPtr, rlArray.lowReg, dataOffset);
1247 }
1248 oatFreeTemp(cUnit, rlArray.lowReg);
1249 if ((size == kLong) || (size == kDouble)) {
1250 if (scale) {
1251 int rNewIndex = oatAllocTemp(cUnit);
1252 opRegRegImm(cUnit, kOpLsl, rNewIndex, rlIndex.lowReg, scale);
1253 opRegReg(cUnit, kOpAdd, regPtr, rNewIndex);
1254 oatFreeTemp(cUnit, rNewIndex);
1255 } else {
1256 opRegReg(cUnit, kOpAdd, regPtr, rlIndex.lowReg);
1257 }
1258 oatFreeTemp(cUnit, rlIndex.lowReg);
1259 rlResult = oatEvalLoc(cUnit, rlDest, regClass, true);
1260
1261 loadPair(cUnit, regPtr, rlResult.lowReg, rlResult.highReg);
1262
1263 oatFreeTemp(cUnit, regPtr);
1264 storeValueWide(cUnit, rlDest, rlResult);
1265 } else {
1266 rlResult = oatEvalLoc(cUnit, rlDest, regClass, true);
1267
1268 loadBaseIndexed(cUnit, regPtr, rlIndex.lowReg, rlResult.lowReg,
1269 scale, size);
1270
1271 oatFreeTemp(cUnit, regPtr);
1272 storeValue(cUnit, rlDest, rlResult);
1273 }
Ian Rogersb5d09b22012-03-06 22:14:17 -08001274#endif
buzbee31a4a6f2012-02-28 15:36:15 -08001275}
1276
1277/*
1278 * Generate array store
1279 *
1280 */
1281void genArrayPut(CompilationUnit* cUnit, MIR* mir, OpSize size,
1282 RegLocation rlArray, RegLocation rlIndex,
1283 RegLocation rlSrc, int scale)
1284{
1285 RegisterClass regClass = oatRegClassBySize(size);
1286 int lenOffset = Array::LengthOffset().Int32Value();
1287 int dataOffset;
1288
1289 if (size == kLong || size == kDouble) {
1290 dataOffset = Array::DataOffset(sizeof(int64_t)).Int32Value();
1291 } else {
1292 dataOffset = Array::DataOffset(sizeof(int32_t)).Int32Value();
1293 }
1294
1295 int regPtr;
1296 rlArray = loadValue(cUnit, rlArray, kCoreReg);
1297 rlIndex = loadValue(cUnit, rlIndex, kCoreReg);
1298
1299 if (oatIsTemp(cUnit, rlArray.lowReg)) {
1300 oatClobber(cUnit, rlArray.lowReg);
1301 regPtr = rlArray.lowReg;
1302 } else {
1303 regPtr = oatAllocTemp(cUnit);
buzbee82488f52012-03-02 08:20:26 -08001304 opRegCopy(cUnit, regPtr, rlArray.lowReg);
buzbee31a4a6f2012-02-28 15:36:15 -08001305 }
1306
1307 /* null object? */
1308 genNullCheck(cUnit, rlArray.sRegLow, rlArray.lowReg, mir);
1309
1310 if (!(mir->optimizationFlags & MIR_IGNORE_RANGE_CHECK)) {
1311 int regLen = oatAllocTemp(cUnit);
1312 //NOTE: max live temps(4) here.
1313 /* Get len */
1314 loadWordDisp(cUnit, rlArray.lowReg, lenOffset, regLen);
1315 /* regPtr -> array data */
1316 opRegImm(cUnit, kOpAdd, regPtr, dataOffset);
1317 genRegRegCheck(cUnit, kCondCs, rlIndex.lowReg, regLen, mir,
1318 kThrowArrayBounds);
1319 oatFreeTemp(cUnit, regLen);
1320 } else {
1321 /* regPtr -> array data */
1322 opRegImm(cUnit, kOpAdd, regPtr, dataOffset);
1323 }
1324 /* at this point, regPtr points to array, 2 live temps */
1325 if ((size == kLong) || (size == kDouble)) {
1326 //TUNING: specific wide routine that can handle fp regs
1327 if (scale) {
1328 int rNewIndex = oatAllocTemp(cUnit);
1329 opRegRegImm(cUnit, kOpLsl, rNewIndex, rlIndex.lowReg, scale);
1330 opRegReg(cUnit, kOpAdd, regPtr, rNewIndex);
1331 oatFreeTemp(cUnit, rNewIndex);
1332 } else {
1333 opRegReg(cUnit, kOpAdd, regPtr, rlIndex.lowReg);
1334 }
1335 rlSrc = loadValueWide(cUnit, rlSrc, regClass);
1336
1337 storePair(cUnit, regPtr, rlSrc.lowReg, rlSrc.highReg);
1338
1339 oatFreeTemp(cUnit, regPtr);
1340 } else {
1341 rlSrc = loadValue(cUnit, rlSrc, regClass);
1342
1343 storeBaseIndexed(cUnit, regPtr, rlIndex.lowReg, rlSrc.lowReg,
1344 scale, size);
1345 }
1346}
1347
1348void genLong3Addr(CompilationUnit* cUnit, MIR* mir, OpKind firstOp,
1349 OpKind secondOp, RegLocation rlDest,
1350 RegLocation rlSrc1, RegLocation rlSrc2)
1351{
1352 RegLocation rlResult;
1353#if defined(TARGET_ARM)
1354 /*
1355 * NOTE: This is the one place in the code in which we might have
1356 * as many as six live temporary registers. There are 5 in the normal
1357 * set for Arm. Until we have spill capabilities, temporarily add
1358 * lr to the temp set. It is safe to do this locally, but note that
1359 * lr is used explicitly elsewhere in the code generator and cannot
1360 * normally be used as a general temp register.
1361 */
1362 oatMarkTemp(cUnit, rLR); // Add lr to the temp pool
1363 oatFreeTemp(cUnit, rLR); // and make it available
1364#endif
1365 rlSrc1 = loadValueWide(cUnit, rlSrc1, kCoreReg);
1366 rlSrc2 = loadValueWide(cUnit, rlSrc2, kCoreReg);
1367 rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
1368 // The longs may overlap - use intermediate temp if so
1369 if (rlResult.lowReg == rlSrc1.highReg) {
1370 int tReg = oatAllocTemp(cUnit);
buzbee82488f52012-03-02 08:20:26 -08001371 opRegCopy(cUnit, tReg, rlSrc1.highReg);
buzbee31a4a6f2012-02-28 15:36:15 -08001372 opRegRegReg(cUnit, firstOp, rlResult.lowReg, rlSrc1.lowReg,
1373 rlSrc2.lowReg);
1374 opRegRegReg(cUnit, secondOp, rlResult.highReg, tReg,
1375 rlSrc2.highReg);
1376 oatFreeTemp(cUnit, tReg);
1377 } else {
1378 opRegRegReg(cUnit, firstOp, rlResult.lowReg, rlSrc1.lowReg,
1379 rlSrc2.lowReg);
1380 opRegRegReg(cUnit, secondOp, rlResult.highReg, rlSrc1.highReg,
1381 rlSrc2.highReg);
1382 }
1383 /*
1384 * NOTE: If rlDest refers to a frame variable in a large frame, the
1385 * following storeValueWide might need to allocate a temp register.
1386 * To further work around the lack of a spill capability, explicitly
1387 * free any temps from rlSrc1 & rlSrc2 that aren't still live in rlResult.
1388 * Remove when spill is functional.
1389 */
1390 freeRegLocTemps(cUnit, rlResult, rlSrc1);
1391 freeRegLocTemps(cUnit, rlResult, rlSrc2);
1392 storeValueWide(cUnit, rlDest, rlResult);
1393#if defined(TARGET_ARM)
1394 oatClobber(cUnit, rLR);
1395 oatUnmarkTemp(cUnit, rLR); // Remove lr from the temp pool
1396#endif
1397}
1398
1399
1400bool genShiftOpLong(CompilationUnit* cUnit, MIR* mir, RegLocation rlDest,
1401 RegLocation rlSrc1, RegLocation rlShift)
1402{
1403 int funcOffset;
1404
1405 switch( mir->dalvikInsn.opcode) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001406 case Instruction::SHL_LONG:
1407 case Instruction::SHL_LONG_2ADDR:
buzbee31a4a6f2012-02-28 15:36:15 -08001408 funcOffset = OFFSETOF_MEMBER(Thread, pShlLong);
1409 break;
Elliott Hughesadb8c672012-03-06 16:49:32 -08001410 case Instruction::SHR_LONG:
1411 case Instruction::SHR_LONG_2ADDR:
buzbee31a4a6f2012-02-28 15:36:15 -08001412 funcOffset = OFFSETOF_MEMBER(Thread, pShrLong);
1413 break;
Elliott Hughesadb8c672012-03-06 16:49:32 -08001414 case Instruction::USHR_LONG:
1415 case Instruction::USHR_LONG_2ADDR:
buzbee31a4a6f2012-02-28 15:36:15 -08001416 funcOffset = OFFSETOF_MEMBER(Thread, pUshrLong);
1417 break;
1418 default:
1419 LOG(FATAL) << "Unexpected case";
1420 return true;
1421 }
1422 oatFlushAllRegs(cUnit); /* Send everything to home location */
1423 int rTgt = loadHelper(cUnit, funcOffset);
1424 loadValueDirectWideFixed(cUnit, rlSrc1, rARG0, rARG1);
1425 loadValueDirect(cUnit, rlShift, rARG2);
1426 callRuntimeHelper(cUnit, rTgt);
1427 RegLocation rlResult = oatGetReturnWide(cUnit);
1428 storeValueWide(cUnit, rlDest, rlResult);
1429 return false;
1430}
1431
1432
1433bool genArithOpInt(CompilationUnit* cUnit, MIR* mir, RegLocation rlDest,
1434 RegLocation rlSrc1, RegLocation rlSrc2)
1435{
1436 OpKind op = kOpBkpt;
1437 bool callOut = false;
1438 bool checkZero = false;
1439 bool unary = false;
buzbee31a4a6f2012-02-28 15:36:15 -08001440 RegLocation rlResult;
1441 bool shiftOp = false;
Ian Rogersb5d09b22012-03-06 22:14:17 -08001442 int funcOffset;
1443 int retReg = rRET0;
buzbee31a4a6f2012-02-28 15:36:15 -08001444 switch (mir->dalvikInsn.opcode) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001445 case Instruction::NEG_INT:
buzbee31a4a6f2012-02-28 15:36:15 -08001446 op = kOpNeg;
1447 unary = true;
1448 break;
Elliott Hughesadb8c672012-03-06 16:49:32 -08001449 case Instruction::NOT_INT:
buzbee31a4a6f2012-02-28 15:36:15 -08001450 op = kOpMvn;
1451 unary = true;
1452 break;
Elliott Hughesadb8c672012-03-06 16:49:32 -08001453 case Instruction::ADD_INT:
1454 case Instruction::ADD_INT_2ADDR:
buzbee31a4a6f2012-02-28 15:36:15 -08001455 op = kOpAdd;
1456 break;
Elliott Hughesadb8c672012-03-06 16:49:32 -08001457 case Instruction::SUB_INT:
1458 case Instruction::SUB_INT_2ADDR:
buzbee31a4a6f2012-02-28 15:36:15 -08001459 op = kOpSub;
1460 break;
Elliott Hughesadb8c672012-03-06 16:49:32 -08001461 case Instruction::MUL_INT:
1462 case Instruction::MUL_INT_2ADDR:
buzbee31a4a6f2012-02-28 15:36:15 -08001463 op = kOpMul;
1464 break;
Elliott Hughesadb8c672012-03-06 16:49:32 -08001465 case Instruction::DIV_INT:
1466 case Instruction::DIV_INT_2ADDR:
buzbee31a4a6f2012-02-28 15:36:15 -08001467 checkZero = true;
Ian Rogersb5d09b22012-03-06 22:14:17 -08001468 op = kOpDiv;
1469 callOut = true;
buzbee31a4a6f2012-02-28 15:36:15 -08001470 funcOffset = OFFSETOF_MEMBER(Thread, pIdiv);
1471 retReg = rRET0;
1472 break;
buzbee5de34942012-03-01 14:51:57 -08001473 /* NOTE: returns in rARG1 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08001474 case Instruction::REM_INT:
1475 case Instruction::REM_INT_2ADDR:
buzbee31a4a6f2012-02-28 15:36:15 -08001476 checkZero = true;
Ian Rogersb5d09b22012-03-06 22:14:17 -08001477 op = kOpRem;
1478 callOut = true;
buzbee31a4a6f2012-02-28 15:36:15 -08001479 funcOffset = OFFSETOF_MEMBER(Thread, pIdivmod);
1480 retReg = rRET1;
1481 break;
Elliott Hughesadb8c672012-03-06 16:49:32 -08001482 case Instruction::AND_INT:
1483 case Instruction::AND_INT_2ADDR:
buzbee31a4a6f2012-02-28 15:36:15 -08001484 op = kOpAnd;
1485 break;
Elliott Hughesadb8c672012-03-06 16:49:32 -08001486 case Instruction::OR_INT:
1487 case Instruction::OR_INT_2ADDR:
buzbee31a4a6f2012-02-28 15:36:15 -08001488 op = kOpOr;
1489 break;
Elliott Hughesadb8c672012-03-06 16:49:32 -08001490 case Instruction::XOR_INT:
1491 case Instruction::XOR_INT_2ADDR:
buzbee31a4a6f2012-02-28 15:36:15 -08001492 op = kOpXor;
1493 break;
Elliott Hughesadb8c672012-03-06 16:49:32 -08001494 case Instruction::SHL_INT:
1495 case Instruction::SHL_INT_2ADDR:
buzbee31a4a6f2012-02-28 15:36:15 -08001496 shiftOp = true;
1497 op = kOpLsl;
1498 break;
Elliott Hughesadb8c672012-03-06 16:49:32 -08001499 case Instruction::SHR_INT:
1500 case Instruction::SHR_INT_2ADDR:
buzbee31a4a6f2012-02-28 15:36:15 -08001501 shiftOp = true;
1502 op = kOpAsr;
1503 break;
Elliott Hughesadb8c672012-03-06 16:49:32 -08001504 case Instruction::USHR_INT:
1505 case Instruction::USHR_INT_2ADDR:
buzbee31a4a6f2012-02-28 15:36:15 -08001506 shiftOp = true;
1507 op = kOpLsr;
1508 break;
1509 default:
1510 LOG(FATAL) << "Invalid word arith op: " <<
1511 (int)mir->dalvikInsn.opcode;
1512 }
1513 if (!callOut) {
1514 rlSrc1 = loadValue(cUnit, rlSrc1, kCoreReg);
1515 if (unary) {
1516 rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
1517 opRegReg(cUnit, op, rlResult.lowReg,
1518 rlSrc1.lowReg);
1519 } else {
1520 rlSrc2 = loadValue(cUnit, rlSrc2, kCoreReg);
Ian Rogersb5d09b22012-03-06 22:14:17 -08001521#if defined(TARGET_X86)
1522 rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
1523 opRegRegReg(cUnit, op, rlResult.lowReg,
1524 rlSrc1.lowReg, rlSrc2.lowReg);
1525#else
buzbee31a4a6f2012-02-28 15:36:15 -08001526 if (shiftOp) {
1527 int tReg = oatAllocTemp(cUnit);
1528 opRegRegImm(cUnit, kOpAnd, tReg, rlSrc2.lowReg, 31);
1529 rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
1530 opRegRegReg(cUnit, op, rlResult.lowReg,
1531 rlSrc1.lowReg, tReg);
1532 oatFreeTemp(cUnit, tReg);
1533 } else {
1534 rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
1535 opRegRegReg(cUnit, op, rlResult.lowReg,
1536 rlSrc1.lowReg, rlSrc2.lowReg);
1537 }
Ian Rogersb5d09b22012-03-06 22:14:17 -08001538#endif
buzbee31a4a6f2012-02-28 15:36:15 -08001539 }
1540 storeValue(cUnit, rlDest, rlResult);
1541 } else {
1542 RegLocation rlResult;
1543 oatFlushAllRegs(cUnit); /* Send everything to home location */
1544 loadValueDirectFixed(cUnit, rlSrc2, rRET1);
1545 int rTgt = loadHelper(cUnit, funcOffset);
1546 loadValueDirectFixed(cUnit, rlSrc1, rARG0);
1547 if (checkZero) {
1548 genImmedCheck(cUnit, kCondEq, rARG1, 0, mir, kThrowDivZero);
1549 }
1550 callRuntimeHelper(cUnit, rTgt);
1551 if (retReg == rRET0)
1552 rlResult = oatGetReturn(cUnit);
1553 else
1554 rlResult = oatGetReturnAlt(cUnit);
1555 storeValue(cUnit, rlDest, rlResult);
1556 }
1557 return false;
1558}
1559
1560/*
1561 * The following are the first-level codegen routines that analyze the format
1562 * of each bytecode then either dispatch special purpose codegen routines
1563 * or produce corresponding Thumb instructions directly.
1564 */
1565
1566bool isPowerOfTwo(int x)
1567{
1568 return (x & (x - 1)) == 0;
1569}
1570
1571// Returns true if no more than two bits are set in 'x'.
1572bool isPopCountLE2(unsigned int x)
1573{
1574 x &= x - 1;
1575 return (x & (x - 1)) == 0;
1576}
1577
1578// Returns the index of the lowest set bit in 'x'.
1579int lowestSetBit(unsigned int x) {
1580 int bit_posn = 0;
1581 while ((x & 0xf) == 0) {
1582 bit_posn += 4;
1583 x >>= 4;
1584 }
1585 while ((x & 1) == 0) {
1586 bit_posn++;
1587 x >>= 1;
1588 }
1589 return bit_posn;
1590}
1591
1592// Returns true if it added instructions to 'cUnit' to divide 'rlSrc' by 'lit'
1593// and store the result in 'rlDest'.
Elliott Hughesadb8c672012-03-06 16:49:32 -08001594bool handleEasyDivide(CompilationUnit* cUnit, Instruction::Code dalvikOpcode,
buzbee31a4a6f2012-02-28 15:36:15 -08001595 RegLocation rlSrc, RegLocation rlDest, int lit)
1596{
1597 if (lit < 2 || !isPowerOfTwo(lit)) {
1598 return false;
1599 }
1600 int k = lowestSetBit(lit);
1601 if (k >= 30) {
1602 // Avoid special cases.
1603 return false;
1604 }
Elliott Hughesadb8c672012-03-06 16:49:32 -08001605 bool div = (dalvikOpcode == Instruction::DIV_INT_LIT8 ||
1606 dalvikOpcode == Instruction::DIV_INT_LIT16);
buzbee31a4a6f2012-02-28 15:36:15 -08001607 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
1608 RegLocation rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
1609 if (div) {
1610 int tReg = oatAllocTemp(cUnit);
1611 if (lit == 2) {
1612 // Division by 2 is by far the most common division by constant.
1613 opRegRegImm(cUnit, kOpLsr, tReg, rlSrc.lowReg, 32 - k);
1614 opRegRegReg(cUnit, kOpAdd, tReg, tReg, rlSrc.lowReg);
1615 opRegRegImm(cUnit, kOpAsr, rlResult.lowReg, tReg, k);
1616 } else {
1617 opRegRegImm(cUnit, kOpAsr, tReg, rlSrc.lowReg, 31);
1618 opRegRegImm(cUnit, kOpLsr, tReg, tReg, 32 - k);
1619 opRegRegReg(cUnit, kOpAdd, tReg, tReg, rlSrc.lowReg);
1620 opRegRegImm(cUnit, kOpAsr, rlResult.lowReg, tReg, k);
1621 }
1622 } else {
1623 int cReg = oatAllocTemp(cUnit);
1624 loadConstant(cUnit, cReg, lit - 1);
1625 int tReg1 = oatAllocTemp(cUnit);
1626 int tReg2 = oatAllocTemp(cUnit);
1627 if (lit == 2) {
1628 opRegRegImm(cUnit, kOpLsr, tReg1, rlSrc.lowReg, 32 - k);
1629 opRegRegReg(cUnit, kOpAdd, tReg2, tReg1, rlSrc.lowReg);
1630 opRegRegReg(cUnit, kOpAnd, tReg2, tReg2, cReg);
1631 opRegRegReg(cUnit, kOpSub, rlResult.lowReg, tReg2, tReg1);
1632 } else {
1633 opRegRegImm(cUnit, kOpAsr, tReg1, rlSrc.lowReg, 31);
1634 opRegRegImm(cUnit, kOpLsr, tReg1, tReg1, 32 - k);
1635 opRegRegReg(cUnit, kOpAdd, tReg2, tReg1, rlSrc.lowReg);
1636 opRegRegReg(cUnit, kOpAnd, tReg2, tReg2, cReg);
1637 opRegRegReg(cUnit, kOpSub, rlResult.lowReg, tReg2, tReg1);
1638 }
1639 }
1640 storeValue(cUnit, rlDest, rlResult);
1641 return true;
1642}
1643
1644void genMultiplyByTwoBitMultiplier(CompilationUnit* cUnit, RegLocation rlSrc,
1645 RegLocation rlResult, int lit,
1646 int firstBit, int secondBit)
1647{
buzbee0398c422012-03-02 15:22:47 -08001648#if defined(TARGET_ARM)
buzbee31a4a6f2012-02-28 15:36:15 -08001649 opRegRegRegShift(cUnit, kOpAdd, rlResult.lowReg, rlSrc.lowReg, rlSrc.lowReg,
1650 encodeShift(kArmLsl, secondBit - firstBit));
buzbee0398c422012-03-02 15:22:47 -08001651#else
1652 int tReg = oatAllocTemp(cUnit);
1653 opRegRegImm(cUnit, kOpLsl, tReg, rlSrc.lowReg, secondBit - firstBit);
1654 opRegRegReg(cUnit, kOpAdd, rlResult.lowReg, rlSrc.lowReg, tReg);
1655 oatFreeTemp(cUnit, tReg);
buzbee5de34942012-03-01 14:51:57 -08001656#endif
buzbee31a4a6f2012-02-28 15:36:15 -08001657 if (firstBit != 0) {
1658 opRegRegImm(cUnit, kOpLsl, rlResult.lowReg, rlResult.lowReg, firstBit);
1659 }
1660}
1661
1662// Returns true if it added instructions to 'cUnit' to multiply 'rlSrc' by 'lit'
1663// and store the result in 'rlDest'.
1664bool handleEasyMultiply(CompilationUnit* cUnit, RegLocation rlSrc,
1665 RegLocation rlDest, int lit)
1666{
1667 // Can we simplify this multiplication?
1668 bool powerOfTwo = false;
1669 bool popCountLE2 = false;
1670 bool powerOfTwoMinusOne = false;
1671 if (lit < 2) {
1672 // Avoid special cases.
1673 return false;
1674 } else if (isPowerOfTwo(lit)) {
1675 powerOfTwo = true;
1676 } else if (isPopCountLE2(lit)) {
1677 popCountLE2 = true;
1678 } else if (isPowerOfTwo(lit + 1)) {
1679 powerOfTwoMinusOne = true;
1680 } else {
1681 return false;
1682 }
1683 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
1684 RegLocation rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
1685 if (powerOfTwo) {
1686 // Shift.
1687 opRegRegImm(cUnit, kOpLsl, rlResult.lowReg, rlSrc.lowReg,
1688 lowestSetBit(lit));
1689 } else if (popCountLE2) {
1690 // Shift and add and shift.
1691 int firstBit = lowestSetBit(lit);
1692 int secondBit = lowestSetBit(lit ^ (1 << firstBit));
1693 genMultiplyByTwoBitMultiplier(cUnit, rlSrc, rlResult, lit,
1694 firstBit, secondBit);
1695 } else {
1696 // Reverse subtract: (src << (shift + 1)) - src.
1697 DCHECK(powerOfTwoMinusOne);
1698 // TUNING: rsb dst, src, src lsl#lowestSetBit(lit + 1)
1699 int tReg = oatAllocTemp(cUnit);
1700 opRegRegImm(cUnit, kOpLsl, tReg, rlSrc.lowReg, lowestSetBit(lit + 1));
1701 opRegRegReg(cUnit, kOpSub, rlResult.lowReg, tReg, rlSrc.lowReg);
1702 }
1703 storeValue(cUnit, rlDest, rlResult);
1704 return true;
1705}
1706
1707bool genArithOpIntLit(CompilationUnit* cUnit, MIR* mir, RegLocation rlDest,
1708 RegLocation rlSrc, int lit)
1709{
Elliott Hughesadb8c672012-03-06 16:49:32 -08001710 Instruction::Code dalvikOpcode = mir->dalvikInsn.opcode;
buzbee31a4a6f2012-02-28 15:36:15 -08001711 RegLocation rlResult;
1712 OpKind op = (OpKind)0; /* Make gcc happy */
1713 int shiftOp = false;
1714 bool isDiv = false;
1715 int funcOffset;
1716 int rTgt;
1717
1718 switch (dalvikOpcode) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001719 case Instruction::RSUB_INT_LIT8:
1720 case Instruction::RSUB_INT: {
buzbee31a4a6f2012-02-28 15:36:15 -08001721 int tReg;
1722 //TUNING: add support for use of Arm rsub op
1723 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
1724 tReg = oatAllocTemp(cUnit);
1725 loadConstant(cUnit, tReg, lit);
1726 rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
1727 opRegRegReg(cUnit, kOpSub, rlResult.lowReg,
1728 tReg, rlSrc.lowReg);
1729 storeValue(cUnit, rlDest, rlResult);
1730 return false;
1731 break;
1732 }
1733
Elliott Hughesadb8c672012-03-06 16:49:32 -08001734 case Instruction::ADD_INT_LIT8:
1735 case Instruction::ADD_INT_LIT16:
buzbee31a4a6f2012-02-28 15:36:15 -08001736 op = kOpAdd;
1737 break;
Elliott Hughesadb8c672012-03-06 16:49:32 -08001738 case Instruction::MUL_INT_LIT8:
1739 case Instruction::MUL_INT_LIT16: {
buzbee31a4a6f2012-02-28 15:36:15 -08001740 if (handleEasyMultiply(cUnit, rlSrc, rlDest, lit)) {
1741 return false;
1742 }
1743 op = kOpMul;
1744 break;
1745 }
Elliott Hughesadb8c672012-03-06 16:49:32 -08001746 case Instruction::AND_INT_LIT8:
1747 case Instruction::AND_INT_LIT16:
buzbee31a4a6f2012-02-28 15:36:15 -08001748 op = kOpAnd;
1749 break;
Elliott Hughesadb8c672012-03-06 16:49:32 -08001750 case Instruction::OR_INT_LIT8:
1751 case Instruction::OR_INT_LIT16:
buzbee31a4a6f2012-02-28 15:36:15 -08001752 op = kOpOr;
1753 break;
Elliott Hughesadb8c672012-03-06 16:49:32 -08001754 case Instruction::XOR_INT_LIT8:
1755 case Instruction::XOR_INT_LIT16:
buzbee31a4a6f2012-02-28 15:36:15 -08001756 op = kOpXor;
1757 break;
Elliott Hughesadb8c672012-03-06 16:49:32 -08001758 case Instruction::SHL_INT_LIT8:
buzbee31a4a6f2012-02-28 15:36:15 -08001759 lit &= 31;
1760 shiftOp = true;
1761 op = kOpLsl;
1762 break;
Elliott Hughesadb8c672012-03-06 16:49:32 -08001763 case Instruction::SHR_INT_LIT8:
buzbee31a4a6f2012-02-28 15:36:15 -08001764 lit &= 31;
1765 shiftOp = true;
1766 op = kOpAsr;
1767 break;
Elliott Hughesadb8c672012-03-06 16:49:32 -08001768 case Instruction::USHR_INT_LIT8:
buzbee31a4a6f2012-02-28 15:36:15 -08001769 lit &= 31;
1770 shiftOp = true;
1771 op = kOpLsr;
1772 break;
1773
Elliott Hughesadb8c672012-03-06 16:49:32 -08001774 case Instruction::DIV_INT_LIT8:
1775 case Instruction::DIV_INT_LIT16:
1776 case Instruction::REM_INT_LIT8:
1777 case Instruction::REM_INT_LIT16:
buzbee31a4a6f2012-02-28 15:36:15 -08001778 if (lit == 0) {
1779 genImmedCheck(cUnit, kCondAl, 0, 0, mir, kThrowDivZero);
1780 return false;
1781 }
1782 if (handleEasyDivide(cUnit, dalvikOpcode, rlSrc, rlDest, lit)) {
1783 return false;
1784 }
1785 oatFlushAllRegs(cUnit); /* Everything to home location */
1786 loadValueDirectFixed(cUnit, rlSrc, rARG0);
1787 oatClobber(cUnit, rARG0);
Elliott Hughesadb8c672012-03-06 16:49:32 -08001788 if ((dalvikOpcode == Instruction::DIV_INT_LIT8) ||
1789 (dalvikOpcode == Instruction::DIV_INT_LIT16)) {
buzbee31a4a6f2012-02-28 15:36:15 -08001790 funcOffset = OFFSETOF_MEMBER(Thread, pIdiv);
1791 isDiv = true;
1792 } else {
1793 funcOffset = OFFSETOF_MEMBER(Thread, pIdivmod);
1794 isDiv = false;
1795 }
1796 rTgt = loadHelper(cUnit, funcOffset);
1797 loadConstant(cUnit, rARG1, lit);
1798 callRuntimeHelper(cUnit, rTgt);
1799 if (isDiv)
1800 rlResult = oatGetReturn(cUnit);
1801 else
1802 rlResult = oatGetReturnAlt(cUnit);
1803 storeValue(cUnit, rlDest, rlResult);
1804 return false;
1805 break;
1806 default:
1807 return true;
1808 }
1809 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
1810 rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
1811 // Avoid shifts by literal 0 - no support in Thumb. Change to copy
1812 if (shiftOp && (lit == 0)) {
buzbee82488f52012-03-02 08:20:26 -08001813 opRegCopy(cUnit, rlResult.lowReg, rlSrc.lowReg);
buzbee31a4a6f2012-02-28 15:36:15 -08001814 } else {
1815 opRegRegImm(cUnit, op, rlResult.lowReg, rlSrc.lowReg, lit);
1816 }
1817 storeValue(cUnit, rlDest, rlResult);
1818 return false;
1819}
1820
1821bool genArithOpLong(CompilationUnit* cUnit, MIR* mir, RegLocation rlDest,
1822 RegLocation rlSrc1, RegLocation rlSrc2)
1823{
1824 RegLocation rlResult;
1825 OpKind firstOp = kOpBkpt;
1826 OpKind secondOp = kOpBkpt;
1827 bool callOut = false;
1828 bool checkZero = false;
1829 int funcOffset;
1830 int retReg = rRET0;
1831
1832 switch (mir->dalvikInsn.opcode) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001833 case Instruction::NOT_LONG:
buzbee31a4a6f2012-02-28 15:36:15 -08001834 rlSrc2 = loadValueWide(cUnit, rlSrc2, kCoreReg);
1835 rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
1836 // Check for destructive overlap
1837 if (rlResult.lowReg == rlSrc2.highReg) {
1838 int tReg = oatAllocTemp(cUnit);
buzbee82488f52012-03-02 08:20:26 -08001839 opRegCopy(cUnit, tReg, rlSrc2.highReg);
buzbee31a4a6f2012-02-28 15:36:15 -08001840 opRegReg(cUnit, kOpMvn, rlResult.lowReg, rlSrc2.lowReg);
1841 opRegReg(cUnit, kOpMvn, rlResult.highReg, tReg);
1842 oatFreeTemp(cUnit, tReg);
1843 } else {
1844 opRegReg(cUnit, kOpMvn, rlResult.lowReg, rlSrc2.lowReg);
1845 opRegReg(cUnit, kOpMvn, rlResult.highReg, rlSrc2.highReg);
1846 }
1847 storeValueWide(cUnit, rlDest, rlResult);
1848 return false;
1849 break;
Elliott Hughesadb8c672012-03-06 16:49:32 -08001850 case Instruction::ADD_LONG:
1851 case Instruction::ADD_LONG_2ADDR:
buzbeec5159d52012-03-03 11:48:39 -08001852#if defined(TARGET_MIPS)
1853 return genAddLong(cUnit, mir, rlDest, rlSrc1, rlSrc2);
1854#else
buzbee31a4a6f2012-02-28 15:36:15 -08001855 firstOp = kOpAdd;
1856 secondOp = kOpAdc;
1857 break;
buzbeec5159d52012-03-03 11:48:39 -08001858#endif
Elliott Hughesadb8c672012-03-06 16:49:32 -08001859 case Instruction::SUB_LONG:
1860 case Instruction::SUB_LONG_2ADDR:
buzbeec5159d52012-03-03 11:48:39 -08001861#if defined(TARGET_MIPS)
1862 return genSubLong(cUnit, mir, rlDest, rlSrc1, rlSrc2);
1863#else
buzbee31a4a6f2012-02-28 15:36:15 -08001864 firstOp = kOpSub;
1865 secondOp = kOpSbc;
1866 break;
buzbeec5159d52012-03-03 11:48:39 -08001867#endif
Elliott Hughesadb8c672012-03-06 16:49:32 -08001868 case Instruction::MUL_LONG:
1869 case Instruction::MUL_LONG_2ADDR:
buzbee31a4a6f2012-02-28 15:36:15 -08001870 callOut = true;
1871 retReg = rRET0;
1872 funcOffset = OFFSETOF_MEMBER(Thread, pLmul);
1873 break;
Elliott Hughesadb8c672012-03-06 16:49:32 -08001874 case Instruction::DIV_LONG:
1875 case Instruction::DIV_LONG_2ADDR:
buzbee31a4a6f2012-02-28 15:36:15 -08001876 callOut = true;
1877 checkZero = true;
1878 retReg = rRET0;
1879 funcOffset = OFFSETOF_MEMBER(Thread, pLdivmod);
1880 break;
1881 /* NOTE - result is in rARG2/rARG3 instead of rRET0/rRET1 */
1882 // FIXME: is true, or could be made true, or other targets?
Elliott Hughesadb8c672012-03-06 16:49:32 -08001883 case Instruction::REM_LONG:
1884 case Instruction::REM_LONG_2ADDR:
buzbee31a4a6f2012-02-28 15:36:15 -08001885 callOut = true;
1886 checkZero = true;
1887 funcOffset = OFFSETOF_MEMBER(Thread, pLdivmod);
1888 retReg = rARG2;
1889 break;
Elliott Hughesadb8c672012-03-06 16:49:32 -08001890 case Instruction::AND_LONG_2ADDR:
1891 case Instruction::AND_LONG:
buzbee31a4a6f2012-02-28 15:36:15 -08001892 firstOp = kOpAnd;
1893 secondOp = kOpAnd;
1894 break;
Elliott Hughesadb8c672012-03-06 16:49:32 -08001895 case Instruction::OR_LONG:
1896 case Instruction::OR_LONG_2ADDR:
buzbee31a4a6f2012-02-28 15:36:15 -08001897 firstOp = kOpOr;
1898 secondOp = kOpOr;
1899 break;
Elliott Hughesadb8c672012-03-06 16:49:32 -08001900 case Instruction::XOR_LONG:
1901 case Instruction::XOR_LONG_2ADDR:
buzbee31a4a6f2012-02-28 15:36:15 -08001902 firstOp = kOpXor;
1903 secondOp = kOpXor;
1904 break;
Elliott Hughesadb8c672012-03-06 16:49:32 -08001905 case Instruction::NEG_LONG: {
buzbeec5159d52012-03-03 11:48:39 -08001906 return genNegLong(cUnit, mir, rlDest, rlSrc2);
buzbee31a4a6f2012-02-28 15:36:15 -08001907 }
1908 default:
1909 LOG(FATAL) << "Invalid long arith op";
1910 }
1911 if (!callOut) {
1912 genLong3Addr(cUnit, mir, firstOp, secondOp, rlDest, rlSrc1, rlSrc2);
1913 } else {
1914 int rTgt;
1915 oatFlushAllRegs(cUnit); /* Send everything to home location */
1916 if (checkZero) {
Ian Rogersb5d09b22012-03-06 22:14:17 -08001917#if defined(TARGET_X86)
1918 UNIMPLEMENTED(FATAL);
1919#else
buzbee31a4a6f2012-02-28 15:36:15 -08001920 loadValueDirectWideFixed(cUnit, rlSrc2, rARG2, rARG3);
Ian Rogersb5d09b22012-03-06 22:14:17 -08001921#endif
buzbee31a4a6f2012-02-28 15:36:15 -08001922 rTgt = loadHelper(cUnit, funcOffset);
1923 loadValueDirectWideFixed(cUnit, rlSrc1, rARG0, rARG1);
1924 int tReg = oatAllocTemp(cUnit);
1925#if defined(TARGET_ARM)
1926 newLIR4(cUnit, kThumb2OrrRRRs, tReg, rARG2, rARG3, 0);
1927 oatFreeTemp(cUnit, tReg);
1928 genCheck(cUnit, kCondEq, mir, kThrowDivZero);
1929#else
Ian Rogersb5d09b22012-03-06 22:14:17 -08001930#if defined(TARGET_X86)
1931 UNIMPLEMENTED(FATAL);
1932#else
buzbee31a4a6f2012-02-28 15:36:15 -08001933 opRegRegReg(cUnit, kOpOr, tReg, rARG2, rARG3);
Ian Rogersb5d09b22012-03-06 22:14:17 -08001934#endif
buzbee5de34942012-03-01 14:51:57 -08001935 genImmedCheck(cUnit, kCondEq, tReg, 0, mir, kThrowDivZero);
buzbee31a4a6f2012-02-28 15:36:15 -08001936 oatFreeTemp(cUnit, tReg);
1937#endif
1938 } else {
1939 rTgt = loadHelper(cUnit, funcOffset);
1940 loadValueDirectWideFixed(cUnit, rlSrc1, rARG0, rARG1);
Ian Rogersb5d09b22012-03-06 22:14:17 -08001941#if defined(TARGET_X86)
1942 UNIMPLEMENTED(FATAL);
1943#else
buzbee31a4a6f2012-02-28 15:36:15 -08001944 loadValueDirectWideFixed(cUnit, rlSrc2, rARG2, rARG3);
Ian Rogersb5d09b22012-03-06 22:14:17 -08001945#endif
buzbee31a4a6f2012-02-28 15:36:15 -08001946 }
1947 callRuntimeHelper(cUnit, rTgt);
1948 // Adjust return regs in to handle case of rem returning rARG2/rARG3
1949 if (retReg == rRET0)
1950 rlResult = oatGetReturnWide(cUnit);
1951 else
1952 rlResult = oatGetReturnWideAlt(cUnit);
1953 storeValueWide(cUnit, rlDest, rlResult);
1954 }
1955 return false;
1956}
1957
1958bool genConversionCall(CompilationUnit* cUnit, MIR* mir, int funcOffset,
1959 int srcSize, int tgtSize)
1960{
1961 /*
1962 * Don't optimize the register usage since it calls out to support
1963 * functions
1964 */
1965 RegLocation rlSrc;
1966 RegLocation rlDest;
1967 oatFlushAllRegs(cUnit); /* Send everything to home location */
1968 int rTgt = loadHelper(cUnit, funcOffset);
1969 if (srcSize == 1) {
1970 rlSrc = oatGetSrc(cUnit, mir, 0);
1971 loadValueDirectFixed(cUnit, rlSrc, rARG0);
1972 } else {
1973 rlSrc = oatGetSrcWide(cUnit, mir, 0, 1);
1974 loadValueDirectWideFixed(cUnit, rlSrc, rARG0, rARG1);
1975 }
1976 callRuntimeHelper(cUnit, rTgt);
1977 if (tgtSize == 1) {
1978 RegLocation rlResult;
1979 rlDest = oatGetDest(cUnit, mir, 0);
1980 rlResult = oatGetReturn(cUnit);
1981 storeValue(cUnit, rlDest, rlResult);
1982 } else {
1983 RegLocation rlResult;
1984 rlDest = oatGetDestWide(cUnit, mir, 0, 1);
1985 rlResult = oatGetReturnWide(cUnit);
1986 storeValueWide(cUnit, rlDest, rlResult);
1987 }
1988 return false;
1989}
1990
1991void genNegFloat(CompilationUnit* cUnit, RegLocation rlDest, RegLocation rlSrc);
1992bool genArithOpFloatPortable(CompilationUnit* cUnit, MIR* mir,
1993 RegLocation rlDest, RegLocation rlSrc1,
1994 RegLocation rlSrc2)
1995{
1996 RegLocation rlResult;
1997 int funcOffset;
1998
1999 switch (mir->dalvikInsn.opcode) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002000 case Instruction::ADD_FLOAT_2ADDR:
2001 case Instruction::ADD_FLOAT:
buzbee31a4a6f2012-02-28 15:36:15 -08002002 funcOffset = OFFSETOF_MEMBER(Thread, pFadd);
2003 break;
Elliott Hughesadb8c672012-03-06 16:49:32 -08002004 case Instruction::SUB_FLOAT_2ADDR:
2005 case Instruction::SUB_FLOAT:
buzbee31a4a6f2012-02-28 15:36:15 -08002006 funcOffset = OFFSETOF_MEMBER(Thread, pFsub);
2007 break;
Elliott Hughesadb8c672012-03-06 16:49:32 -08002008 case Instruction::DIV_FLOAT_2ADDR:
2009 case Instruction::DIV_FLOAT:
buzbee31a4a6f2012-02-28 15:36:15 -08002010 funcOffset = OFFSETOF_MEMBER(Thread, pFdiv);
2011 break;
Elliott Hughesadb8c672012-03-06 16:49:32 -08002012 case Instruction::MUL_FLOAT_2ADDR:
2013 case Instruction::MUL_FLOAT:
buzbee31a4a6f2012-02-28 15:36:15 -08002014 funcOffset = OFFSETOF_MEMBER(Thread, pFmul);
2015 break;
Elliott Hughesadb8c672012-03-06 16:49:32 -08002016 case Instruction::REM_FLOAT_2ADDR:
2017 case Instruction::REM_FLOAT:
buzbee31a4a6f2012-02-28 15:36:15 -08002018 funcOffset = OFFSETOF_MEMBER(Thread, pFmodf);
2019 break;
Elliott Hughesadb8c672012-03-06 16:49:32 -08002020 case Instruction::NEG_FLOAT: {
buzbee31a4a6f2012-02-28 15:36:15 -08002021 genNegFloat(cUnit, rlDest, rlSrc1);
2022 return false;
2023 }
2024 default:
2025 return true;
2026 }
2027 oatFlushAllRegs(cUnit); /* Send everything to home location */
2028 int rTgt = loadHelper(cUnit, funcOffset);
2029 loadValueDirectFixed(cUnit, rlSrc1, rARG0);
2030 loadValueDirectFixed(cUnit, rlSrc2, rARG1);
2031 callRuntimeHelper(cUnit, rTgt);
2032 rlResult = oatGetReturn(cUnit);
2033 storeValue(cUnit, rlDest, rlResult);
2034 return false;
2035}
2036
2037void genNegDouble(CompilationUnit* cUnit, RegLocation rlDst, RegLocation rlSrc);
2038bool genArithOpDoublePortable(CompilationUnit* cUnit, MIR* mir,
2039 RegLocation rlDest, RegLocation rlSrc1,
2040 RegLocation rlSrc2)
2041{
2042 RegLocation rlResult;
2043 int funcOffset;
2044
2045 switch (mir->dalvikInsn.opcode) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002046 case Instruction::ADD_DOUBLE_2ADDR:
2047 case Instruction::ADD_DOUBLE:
buzbee31a4a6f2012-02-28 15:36:15 -08002048 funcOffset = OFFSETOF_MEMBER(Thread, pDadd);
2049 break;
Elliott Hughesadb8c672012-03-06 16:49:32 -08002050 case Instruction::SUB_DOUBLE_2ADDR:
2051 case Instruction::SUB_DOUBLE:
buzbee31a4a6f2012-02-28 15:36:15 -08002052 funcOffset = OFFSETOF_MEMBER(Thread, pDsub);
2053 break;
Elliott Hughesadb8c672012-03-06 16:49:32 -08002054 case Instruction::DIV_DOUBLE_2ADDR:
2055 case Instruction::DIV_DOUBLE:
buzbee31a4a6f2012-02-28 15:36:15 -08002056 funcOffset = OFFSETOF_MEMBER(Thread, pDdiv);
2057 break;
Elliott Hughesadb8c672012-03-06 16:49:32 -08002058 case Instruction::MUL_DOUBLE_2ADDR:
2059 case Instruction::MUL_DOUBLE:
buzbee31a4a6f2012-02-28 15:36:15 -08002060 funcOffset = OFFSETOF_MEMBER(Thread, pDmul);
2061 break;
Elliott Hughesadb8c672012-03-06 16:49:32 -08002062 case Instruction::REM_DOUBLE_2ADDR:
2063 case Instruction::REM_DOUBLE:
buzbee31a4a6f2012-02-28 15:36:15 -08002064 funcOffset = OFFSETOF_MEMBER(Thread, pFmod);
2065 break;
Elliott Hughesadb8c672012-03-06 16:49:32 -08002066 case Instruction::NEG_DOUBLE: {
buzbee31a4a6f2012-02-28 15:36:15 -08002067 genNegDouble(cUnit, rlDest, rlSrc1);
2068 return false;
2069 }
2070 default:
2071 return true;
2072 }
2073 oatFlushAllRegs(cUnit); /* Send everything to home location */
2074 int rTgt = loadHelper(cUnit, funcOffset);
2075 loadValueDirectWideFixed(cUnit, rlSrc1, rARG0, rARG1);
Ian Rogersb5d09b22012-03-06 22:14:17 -08002076#if defined(TARGET_X86)
2077 UNIMPLEMENTED(FATAL);
2078#else
buzbee31a4a6f2012-02-28 15:36:15 -08002079 loadValueDirectWideFixed(cUnit, rlSrc2, rARG2, rARG3);
Ian Rogersb5d09b22012-03-06 22:14:17 -08002080#endif
buzbee31a4a6f2012-02-28 15:36:15 -08002081 callRuntimeHelper(cUnit, rTgt);
2082 rlResult = oatGetReturnWide(cUnit);
2083 storeValueWide(cUnit, rlDest, rlResult);
2084 return false;
2085}
2086
2087bool genConversionPortable(CompilationUnit* cUnit, MIR* mir)
2088{
Elliott Hughesadb8c672012-03-06 16:49:32 -08002089 Instruction::Code opcode = mir->dalvikInsn.opcode;
buzbee31a4a6f2012-02-28 15:36:15 -08002090
2091 switch (opcode) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002092 case Instruction::INT_TO_FLOAT:
buzbee31a4a6f2012-02-28 15:36:15 -08002093 return genConversionCall(cUnit, mir, OFFSETOF_MEMBER(Thread, pI2f),
2094 1, 1);
Elliott Hughesadb8c672012-03-06 16:49:32 -08002095 case Instruction::FLOAT_TO_INT:
buzbee31a4a6f2012-02-28 15:36:15 -08002096 return genConversionCall(cUnit, mir, OFFSETOF_MEMBER(Thread, pF2iz),
2097 1, 1);
Elliott Hughesadb8c672012-03-06 16:49:32 -08002098 case Instruction::DOUBLE_TO_FLOAT:
buzbee31a4a6f2012-02-28 15:36:15 -08002099 return genConversionCall(cUnit, mir, OFFSETOF_MEMBER(Thread, pD2f),
2100 2, 1);
Elliott Hughesadb8c672012-03-06 16:49:32 -08002101 case Instruction::FLOAT_TO_DOUBLE:
buzbee31a4a6f2012-02-28 15:36:15 -08002102 return genConversionCall(cUnit, mir, OFFSETOF_MEMBER(Thread, pF2d),
2103 1, 2);
Elliott Hughesadb8c672012-03-06 16:49:32 -08002104 case Instruction::INT_TO_DOUBLE:
buzbee31a4a6f2012-02-28 15:36:15 -08002105 return genConversionCall(cUnit, mir, OFFSETOF_MEMBER(Thread, pI2d),
2106 1, 2);
Elliott Hughesadb8c672012-03-06 16:49:32 -08002107 case Instruction::DOUBLE_TO_INT:
buzbee31a4a6f2012-02-28 15:36:15 -08002108 return genConversionCall(cUnit, mir, OFFSETOF_MEMBER(Thread, pD2iz),
2109 2, 1);
Elliott Hughesadb8c672012-03-06 16:49:32 -08002110 case Instruction::FLOAT_TO_LONG:
buzbee31a4a6f2012-02-28 15:36:15 -08002111 return genConversionCall(cUnit, mir, OFFSETOF_MEMBER(Thread,
2112 pF2l), 1, 2);
Elliott Hughesadb8c672012-03-06 16:49:32 -08002113 case Instruction::LONG_TO_FLOAT:
buzbee31a4a6f2012-02-28 15:36:15 -08002114 return genConversionCall(cUnit, mir, OFFSETOF_MEMBER(Thread, pL2f),
2115 2, 1);
Elliott Hughesadb8c672012-03-06 16:49:32 -08002116 case Instruction::DOUBLE_TO_LONG:
buzbee31a4a6f2012-02-28 15:36:15 -08002117 return genConversionCall(cUnit, mir, OFFSETOF_MEMBER(Thread,
2118 pD2l), 2, 2);
Elliott Hughesadb8c672012-03-06 16:49:32 -08002119 case Instruction::LONG_TO_DOUBLE:
buzbee31a4a6f2012-02-28 15:36:15 -08002120 return genConversionCall(cUnit, mir, OFFSETOF_MEMBER(Thread, pL2d),
2121 2, 2);
2122 default:
2123 return true;
2124 }
2125 return false;
2126}
2127
2128/*
2129 * Generate callout to updateDebugger. Note that we're overloading
2130 * the use of rSUSPEND here. When the debugger is active, this
2131 * register holds the address of the update function. So, if it's
2132 * non-null, we call out to it.
2133 *
2134 * Note also that rRET0 and rRET1 must be preserved across this
2135 * code. This must be handled by the stub.
2136 */
2137void genDebuggerUpdate(CompilationUnit* cUnit, int32_t offset)
2138{
2139 // Following DCHECK verifies that dPC is in range of single load immediate
2140 DCHECK((offset == DEBUGGER_METHOD_ENTRY) ||
2141 (offset == DEBUGGER_METHOD_EXIT) || ((offset & 0xffff) == offset));
2142 oatClobberCalleeSave(cUnit);
2143#if defined(TARGET_ARM)
2144 opRegImm(cUnit, kOpCmp, rSUSPEND, 0);
buzbee82488f52012-03-02 08:20:26 -08002145 opIT(cUnit, kArmCondNe, "T");
buzbee31a4a6f2012-02-28 15:36:15 -08002146 loadConstant(cUnit, rARG2, offset); // arg2 <- Entry code
2147 opReg(cUnit, kOpBlx, rSUSPEND);
Ian Rogersb5d09b22012-03-06 22:14:17 -08002148#elif defined(TARGET_X86)
2149 UNIMPLEMENTED(FATAL);
buzbee31a4a6f2012-02-28 15:36:15 -08002150#else
buzbee82488f52012-03-02 08:20:26 -08002151 LIR* branch = opCmpImmBranch(cUnit, kCondEq, rSUSPEND, 0, NULL);
buzbee31a4a6f2012-02-28 15:36:15 -08002152 loadConstant(cUnit, rARG2, offset);
2153 opReg(cUnit, kOpBlx, rSUSPEND);
2154 LIR* target = newLIR0(cUnit, kPseudoTargetLabel);
buzbee31a4a6f2012-02-28 15:36:15 -08002155 branch->target = (LIR*)target;
2156#endif
2157 oatFreeTemp(cUnit, rARG2);
2158}
2159
2160/* Check if we need to check for pending suspend request */
2161void genSuspendTest(CompilationUnit* cUnit, MIR* mir)
2162{
2163 if (NO_SUSPEND || (mir->optimizationFlags & MIR_IGNORE_SUSPEND_CHECK)) {
2164 return;
2165 }
2166 oatFlushAllRegs(cUnit);
buzbee31a4a6f2012-02-28 15:36:15 -08002167 if (cUnit->genDebugger) {
2168 // If generating code for the debugger, always check for suspension
buzbee86a4bce2012-03-06 18:15:00 -08002169 int rTgt = loadHelper(cUnit, OFFSETOF_MEMBER(Thread,
2170 pTestSuspendFromCode));
2171 opReg(cUnit, kOpBlx, rTgt);
2172 // Refresh rSUSPEND
Ian Rogersb5d09b22012-03-06 22:14:17 -08002173#if defined(TARGET_X86)
2174 UNIMPLEMENTED(FATAL);
2175#else
2176
buzbee86a4bce2012-03-06 18:15:00 -08002177 loadWordDisp(cUnit, rSELF,
2178 OFFSETOF_MEMBER(Thread, pUpdateDebuggerFromCode),
2179 rSUSPEND);
Ian Rogersb5d09b22012-03-06 22:14:17 -08002180#endif
buzbee31a4a6f2012-02-28 15:36:15 -08002181 } else {
Ian Rogersb5d09b22012-03-06 22:14:17 -08002182 LIR* branch = NULL;
buzbee31a4a6f2012-02-28 15:36:15 -08002183#if defined(TARGET_ARM)
2184 // In non-debug case, only check periodically
2185 newLIR2(cUnit, kThumbSubRI8, rSUSPEND, 1);
buzbee82488f52012-03-02 08:20:26 -08002186 branch = opCondBranch(cUnit, kCondEq, NULL);
Ian Rogersb5d09b22012-03-06 22:14:17 -08002187#elif defined(TARGET_X86)
2188 newLIR2(cUnit, kX86Cmp32TI, Thread::SuspendCountOffset().Int32Value(), 0);
2189 branch = opCondBranch(cUnit, kCondNe, NULL);
buzbee31a4a6f2012-02-28 15:36:15 -08002190#else
2191 opRegImm(cUnit, kOpSub, rSUSPEND, 1);
buzbee82488f52012-03-02 08:20:26 -08002192 branch = opCmpImmBranch(cUnit, kCondEq, rSUSPEND, 0, NULL);
buzbee31a4a6f2012-02-28 15:36:15 -08002193#endif
buzbee86a4bce2012-03-06 18:15:00 -08002194 LIR* retLab = newLIR0(cUnit, kPseudoTargetLabel);
2195 LIR* target = rawLIR(cUnit, cUnit->currentDalvikOffset,
2196 kPseudoSuspendTarget, (intptr_t)retLab, mir->offset);
2197 branch->target = (LIR*)target;
2198 oatInsertGrowableList(cUnit, &cUnit->suspendLaunchpads, (intptr_t)target);
buzbee31a4a6f2012-02-28 15:36:15 -08002199 }
buzbee31a4a6f2012-02-28 15:36:15 -08002200}
2201
2202} // namespace art