blob: 72a596afed7d9cd2da0838728d065d04b176afea [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);
buzbee3d661942012-03-14 17:37:27 -0700873 /*
874 * Because we have stores of the target value on two paths,
875 * clobber temp tracking for the destination using the ssa name
876 */
877 oatClobberSReg(cUnit, rlDest.sRegLow);
buzbee82488f52012-03-02 08:20:26 -0800878 LIR* branch2 = opUnconditionalBranch(cUnit,0);
buzbee31a4a6f2012-02-28 15:36:15 -0800879 // TUNING: move slow path to end & remove unconditional branch
880 LIR* target1 = newLIR0(cUnit, kPseudoTargetLabel);
buzbee5de34942012-03-01 14:51:57 -0800881 // Call out to helper, which will return resolved type in rARG0
buzbee31a4a6f2012-02-28 15:36:15 -0800882 int rTgt = loadHelper(cUnit, OFFSETOF_MEMBER(Thread,
883 pInitializeTypeFromCode));
buzbeee1965672012-03-11 18:39:19 -0700884 opRegCopy(cUnit, rARG1, rlMethod.lowReg);
buzbee31a4a6f2012-02-28 15:36:15 -0800885 loadConstant(cUnit, rARG0, type_idx);
886 callRuntimeHelper(cUnit, rTgt);
887 RegLocation rlResult = oatGetReturn(cUnit);
888 storeValue(cUnit, rlDest, rlResult);
buzbee3d661942012-03-14 17:37:27 -0700889 /*
890 * Because we have stores of the target value on two paths,
891 * clobber temp tracking for the destination using the ssa name
892 */
893 oatClobberSReg(cUnit, rlDest.sRegLow);
buzbee31a4a6f2012-02-28 15:36:15 -0800894 // Rejoin code paths
895 LIR* target2 = newLIR0(cUnit, kPseudoTargetLabel);
buzbee31a4a6f2012-02-28 15:36:15 -0800896 branch1->target = (LIR*)target1;
897 branch2->target = (LIR*)target2;
898 } else {
899 // Fast path, we're done - just store result
900 storeValue(cUnit, rlDest, rlResult);
901 }
902 }
903}
904void genConstString(CompilationUnit* cUnit, MIR* mir, RegLocation rlDest,
905 RegLocation rlSrc)
906{
907 /* NOTE: Most strings should be available at compile time */
908 uint32_t string_idx = mir->dalvikInsn.vB;
909 int32_t offset_of_string = Array::DataOffset(sizeof(String*)).Int32Value() +
910 (sizeof(String*) * string_idx);
911 if (!cUnit->compiler->CanAssumeStringIsPresentInDexCache(
912 cUnit->dex_cache, string_idx) || SLOW_STRING_PATH) {
913 // slow path, resolve string if not in dex cache
914 oatFlushAllRegs(cUnit);
915 oatLockCallTemps(cUnit); // Using explicit registers
916 loadCurrMethodDirect(cUnit, rARG2);
917 loadWordDisp(cUnit, rARG2,
918 Method::DexCacheStringsOffset().Int32Value(), rARG0);
919 // Might call out to helper, which will return resolved string in rRET0
920 int rTgt = loadHelper(cUnit, OFFSETOF_MEMBER(Thread,
921 pResolveStringFromCode));
922 loadWordDisp(cUnit, rRET0, offset_of_string, rARG0);
923 loadConstant(cUnit, rARG1, string_idx);
924#if defined(TARGET_ARM)
925 opRegImm(cUnit, kOpCmp, rRET0, 0); // Is resolved?
926 genBarrier(cUnit);
927 // For testing, always force through helper
928 if (!EXERCISE_SLOWEST_STRING_PATH) {
buzbee82488f52012-03-02 08:20:26 -0800929 opIT(cUnit, kArmCondEq, "T");
buzbee31a4a6f2012-02-28 15:36:15 -0800930 }
buzbee82488f52012-03-02 08:20:26 -0800931 opRegCopy(cUnit, rARG0, rARG2); // .eq
buzbee31a4a6f2012-02-28 15:36:15 -0800932 opReg(cUnit, kOpBlx, rTgt); // .eq, helper(Method*, string_idx)
933#else
buzbee82488f52012-03-02 08:20:26 -0800934 LIR* branch = opCmpImmBranch(cUnit, kCondNe, rRET0, 0, NULL);
935 opRegCopy(cUnit, rARG0, rARG2); // .eq
buzbee31a4a6f2012-02-28 15:36:15 -0800936 opReg(cUnit, kOpBlx, rTgt);
937 LIR* target = newLIR0(cUnit, kPseudoTargetLabel);
buzbee31a4a6f2012-02-28 15:36:15 -0800938 branch->target = target;
939#endif
940 genBarrier(cUnit);
buzbee86a4bce2012-03-06 18:15:00 -0800941 storeValue(cUnit, rlDest, oatGetReturn(cUnit));
buzbee31a4a6f2012-02-28 15:36:15 -0800942 } else {
buzbeee1965672012-03-11 18:39:19 -0700943 RegLocation rlMethod = loadCurrMethod(cUnit);
buzbee31a4a6f2012-02-28 15:36:15 -0800944 int resReg = oatAllocTemp(cUnit);
945 RegLocation rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
buzbeee1965672012-03-11 18:39:19 -0700946 loadWordDisp(cUnit, rlMethod.lowReg,
buzbee31a4a6f2012-02-28 15:36:15 -0800947 Method::DexCacheStringsOffset().Int32Value(), resReg);
948 loadWordDisp(cUnit, resReg, offset_of_string, rlResult.lowReg);
949 storeValue(cUnit, rlDest, rlResult);
950 }
951}
952
953/*
954 * Let helper function take care of everything. Will
955 * call Class::NewInstanceFromCode(type_idx, method);
956 */
957void genNewInstance(CompilationUnit* cUnit, MIR* mir, RegLocation rlDest)
958{
959 oatFlushAllRegs(cUnit); /* Everything to home location */
960 uint32_t type_idx = mir->dalvikInsn.vB;
961 // alloc will always check for resolution, do we also need to verify
962 // access because the verifier was unable to?
963 int rTgt;
964 if (cUnit->compiler->CanAccessInstantiableTypeWithoutChecks(
965 cUnit->method_idx, cUnit->dex_cache, *cUnit->dex_file, type_idx)) {
966 rTgt = loadHelper(cUnit, OFFSETOF_MEMBER(Thread, pAllocObjectFromCode));
967 } else {
968 rTgt = loadHelper(cUnit, OFFSETOF_MEMBER(Thread,
969 pAllocObjectFromCodeWithAccessCheck));
970 }
971 loadCurrMethodDirect(cUnit, rARG1); // arg1 <= Method*
972 loadConstant(cUnit, rARG0, type_idx); // arg0 <- type_idx
973 callRuntimeHelper(cUnit, rTgt);
974 RegLocation rlResult = oatGetReturn(cUnit);
975 storeValue(cUnit, rlDest, rlResult);
976}
977
978void genInstanceof(CompilationUnit* cUnit, MIR* mir, RegLocation rlDest,
979 RegLocation rlSrc)
980{
981 oatFlushAllRegs(cUnit);
982 // May generate a call - use explicit registers
983 oatLockCallTemps(cUnit);
984 uint32_t type_idx = mir->dalvikInsn.vC;
buzbee5de34942012-03-01 14:51:57 -0800985 loadCurrMethodDirect(cUnit, rARG1); // rARG1 <= current Method*
buzbee31a4a6f2012-02-28 15:36:15 -0800986 int classReg = rARG2; // rARG2 will hold the Class*
987 if (!cUnit->compiler->CanAccessTypeWithoutChecks(cUnit->method_idx,
988 cUnit->dex_cache,
989 *cUnit->dex_file,
990 type_idx)) {
991 // Check we have access to type_idx and if not throw IllegalAccessError,
buzbee5de34942012-03-01 14:51:57 -0800992 // returns Class* in rARG0
buzbee31a4a6f2012-02-28 15:36:15 -0800993 int rTgt = loadHelper(cUnit, OFFSETOF_MEMBER(Thread,
994 pInitializeTypeAndVerifyAccessFromCode));
995 loadConstant(cUnit, rARG0, type_idx);
996 callRuntimeHelper(cUnit, rTgt); // InitializeTypeAndVerifyAccess(idx, method)
buzbee82488f52012-03-02 08:20:26 -0800997 opRegCopy(cUnit, classReg, rRET0); // Align usage with fast path
buzbee5de34942012-03-01 14:51:57 -0800998 loadValueDirectFixed(cUnit, rlSrc, rARG0); // rARG0 <= ref
buzbee31a4a6f2012-02-28 15:36:15 -0800999 } else {
buzbee5de34942012-03-01 14:51:57 -08001000 // Load dex cache entry into classReg (rARG2)
buzbee31a4a6f2012-02-28 15:36:15 -08001001 loadValueDirectFixed(cUnit, rlSrc, rARG0); // rARG0 <= ref
1002 loadWordDisp(cUnit, rARG1,
1003 Method::DexCacheResolvedTypesOffset().Int32Value(),
1004 classReg);
1005 int32_t offset_of_type =
1006 Array::DataOffset(sizeof(Class*)).Int32Value() + (sizeof(Class*)
1007 * type_idx);
1008 loadWordDisp(cUnit, classReg, offset_of_type, classReg);
1009 if (!cUnit->compiler->CanAssumeTypeIsPresentInDexCache(
1010 cUnit->dex_cache, type_idx)) {
1011 // Need to test presence of type in dex cache at runtime
buzbee82488f52012-03-02 08:20:26 -08001012 LIR* hopBranch = opCmpImmBranch(cUnit, kCondNe, classReg, 0, NULL);
buzbee31a4a6f2012-02-28 15:36:15 -08001013 // Not resolved
1014 // Call out to helper, which will return resolved type in rRET0
1015 int rTgt = loadHelper(cUnit, OFFSETOF_MEMBER(Thread,
1016 pInitializeTypeFromCode));
1017 loadConstant(cUnit, rARG0, type_idx);
1018 callRuntimeHelper(cUnit, rTgt); // InitializeTypeFromCode(idx, method)
buzbee82488f52012-03-02 08:20:26 -08001019 opRegCopy(cUnit, rARG2, rRET0); // Align usage with fast path
buzbee31a4a6f2012-02-28 15:36:15 -08001020 loadValueDirectFixed(cUnit, rlSrc, rARG0); /* reload Ref */
1021 // Rejoin code paths
1022 LIR* hopTarget = newLIR0(cUnit, kPseudoTargetLabel);
buzbee31a4a6f2012-02-28 15:36:15 -08001023 hopBranch->target = (LIR*)hopTarget;
1024 }
1025 }
1026 /* rARG0 is ref, rARG2 is class. If ref==null, use directly as bool result */
buzbee82488f52012-03-02 08:20:26 -08001027 LIR* branch1 = opCmpImmBranch(cUnit, kCondEq, rARG0, 0, NULL);
buzbee31a4a6f2012-02-28 15:36:15 -08001028 /* load object->clazz */
1029 DCHECK_EQ(Object::ClassOffset().Int32Value(), 0);
1030 loadWordDisp(cUnit, rARG0, Object::ClassOffset().Int32Value(), rARG1);
1031 /* rARG0 is ref, rARG1 is ref->clazz, rARG2 is class */
buzbee0398c422012-03-02 15:22:47 -08001032#if defined(TARGET_ARM)
1033 /* Uses conditional nullification */
buzbee31a4a6f2012-02-28 15:36:15 -08001034 int rTgt = loadHelper(cUnit, OFFSETOF_MEMBER(Thread,
1035 pInstanceofNonTrivialFromCode));
buzbee31a4a6f2012-02-28 15:36:15 -08001036 opRegReg(cUnit, kOpCmp, rARG1, rARG2); // Same?
buzbee82488f52012-03-02 08:20:26 -08001037 opIT(cUnit, kArmCondEq, "EE"); // if-convert the test
buzbee31a4a6f2012-02-28 15:36:15 -08001038 loadConstant(cUnit, rARG0, 1); // .eq case - load true
buzbee82488f52012-03-02 08:20:26 -08001039 opRegCopy(cUnit, rARG0, rARG2); // .ne case - arg0 <= class
buzbee31a4a6f2012-02-28 15:36:15 -08001040 opReg(cUnit, kOpBlx, rTgt); // .ne case: helper(class, ref->class)
buzbee31a4a6f2012-02-28 15:36:15 -08001041#else
buzbee0398c422012-03-02 15:22:47 -08001042 /* Uses branchovers */
1043 loadConstant(cUnit, rARG0, 1); // assume true
1044 LIR* branchover = opCmpBranch(cUnit, kCondEq, rARG1, rARG2, NULL);
1045 int rTgt = loadHelper(cUnit, OFFSETOF_MEMBER(Thread,
1046 pInstanceofNonTrivialFromCode));
1047 opRegCopy(cUnit, rARG0, rARG2); // .ne case - arg0 <= class
1048 opReg(cUnit, kOpBlx, rTgt); // .ne case: helper(class, ref->class)
buzbee31a4a6f2012-02-28 15:36:15 -08001049#endif
buzbee0398c422012-03-02 15:22:47 -08001050 oatClobberCalleeSave(cUnit);
1051 /* branch targets here */
buzbee31a4a6f2012-02-28 15:36:15 -08001052 LIR* target = newLIR0(cUnit, kPseudoTargetLabel);
buzbee31a4a6f2012-02-28 15:36:15 -08001053 RegLocation rlResult = oatGetReturn(cUnit);
1054 storeValue(cUnit, rlDest, rlResult);
buzbee0398c422012-03-02 15:22:47 -08001055 branch1->target = target;
1056#if !defined(TARGET_ARM)
1057 branchover->target = target;
1058#endif
buzbee31a4a6f2012-02-28 15:36:15 -08001059}
1060
1061void genCheckCast(CompilationUnit* cUnit, MIR* mir, RegLocation rlSrc)
1062{
1063 oatFlushAllRegs(cUnit);
1064 // May generate a call - use explicit registers
1065 oatLockCallTemps(cUnit);
1066 uint32_t type_idx = mir->dalvikInsn.vB;
1067 loadCurrMethodDirect(cUnit, rARG1); // rARG1 <= current Method*
1068 int classReg = rARG2; // rARG2 will hold the Class*
1069 if (!cUnit->compiler->CanAccessTypeWithoutChecks(cUnit->method_idx,
1070 cUnit->dex_cache,
1071 *cUnit->dex_file,
1072 type_idx)) {
1073 // Check we have access to type_idx and if not throw IllegalAccessError,
1074 // returns Class* in rRET0
1075 int rTgt = loadHelper(cUnit, OFFSETOF_MEMBER(Thread,
1076 pInitializeTypeAndVerifyAccessFromCode));
1077 loadConstant(cUnit, rARG0, type_idx);
1078 callRuntimeHelper(cUnit, rTgt); // InitializeTypeAndVerifyAccess(idx, method)
buzbee82488f52012-03-02 08:20:26 -08001079 opRegCopy(cUnit, classReg, rRET0); // Align usage with fast path
buzbee31a4a6f2012-02-28 15:36:15 -08001080 } else {
1081 // Load dex cache entry into classReg (rARG2)
1082 loadWordDisp(cUnit, rARG1,
1083 Method::DexCacheResolvedTypesOffset().Int32Value(),
1084 classReg);
1085 int32_t offset_of_type =
1086 Array::DataOffset(sizeof(Class*)).Int32Value() +
1087 (sizeof(Class*) * type_idx);
1088 loadWordDisp(cUnit, classReg, offset_of_type, classReg);
1089 if (!cUnit->compiler->CanAssumeTypeIsPresentInDexCache(
1090 cUnit->dex_cache, type_idx)) {
1091 // Need to test presence of type in dex cache at runtime
buzbee82488f52012-03-02 08:20:26 -08001092 LIR* hopBranch = opCmpImmBranch(cUnit, kCondNe, classReg, 0, NULL);
buzbee31a4a6f2012-02-28 15:36:15 -08001093 // Not resolved
buzbee5de34942012-03-01 14:51:57 -08001094 // Call out to helper, which will return resolved type in rARG0
1095 int rTgt = loadHelper(cUnit, OFFSETOF_MEMBER(Thread, pInitializeTypeFromCode));
1096 loadConstant(cUnit, rARG0, type_idx);
1097 callRuntimeHelper(cUnit, rTgt); // InitializeTypeFromCode(idx, method)
buzbee82488f52012-03-02 08:20:26 -08001098 opRegCopy(cUnit, classReg, rARG0); // Align usage with fast path
buzbee31a4a6f2012-02-28 15:36:15 -08001099 // Rejoin code paths
1100 LIR* hopTarget = newLIR0(cUnit, kPseudoTargetLabel);
buzbee31a4a6f2012-02-28 15:36:15 -08001101 hopBranch->target = (LIR*)hopTarget;
1102 }
1103 }
buzbee5de34942012-03-01 14:51:57 -08001104 // At this point, classReg (rARG2) has class
buzbee31a4a6f2012-02-28 15:36:15 -08001105 loadValueDirectFixed(cUnit, rlSrc, rARG0); // rARG0 <= ref
1106 /* Null is OK - continue */
buzbee82488f52012-03-02 08:20:26 -08001107 LIR* branch1 = opCmpImmBranch(cUnit, kCondEq, rARG0, 0, NULL);
buzbee31a4a6f2012-02-28 15:36:15 -08001108 /* load object->clazz */
1109 DCHECK_EQ(Object::ClassOffset().Int32Value(), 0);
1110 loadWordDisp(cUnit, rARG0, Object::ClassOffset().Int32Value(), rARG1);
1111 /* rARG1 now contains object->clazz */
1112 int rTgt = loadHelper(cUnit, OFFSETOF_MEMBER(Thread,
1113 pCheckCastFromCode));
Ian Rogersb5d09b22012-03-06 22:14:17 -08001114#if defined(TARGET_MIPS) || defined(TARGET_X86)
buzbee82488f52012-03-02 08:20:26 -08001115 LIR* branch2 = opCmpBranch(cUnit, kCondEq, rARG1, classReg, NULL);
buzbee5de34942012-03-01 14:51:57 -08001116#else
buzbee31a4a6f2012-02-28 15:36:15 -08001117 opRegReg(cUnit, kOpCmp, rARG1, classReg);
buzbee82488f52012-03-02 08:20:26 -08001118 LIR* branch2 = opCondBranch(cUnit, kCondEq, NULL); /* If eq, trivial yes */
buzbee5de34942012-03-01 14:51:57 -08001119#endif
buzbee82488f52012-03-02 08:20:26 -08001120 opRegCopy(cUnit, rARG0, rARG1);
1121 opRegCopy(cUnit, rARG1, rARG2);
buzbee31a4a6f2012-02-28 15:36:15 -08001122 callRuntimeHelper(cUnit, rTgt);
1123 /* branch target here */
1124 LIR* target = newLIR0(cUnit, kPseudoTargetLabel);
buzbee31a4a6f2012-02-28 15:36:15 -08001125 branch1->target = (LIR*)target;
1126 branch2->target = (LIR*)target;
1127}
1128
1129
1130void genThrow(CompilationUnit* cUnit, MIR* mir, RegLocation rlSrc)
1131{
1132 oatFlushAllRegs(cUnit);
1133 int rTgt = loadHelper(cUnit, OFFSETOF_MEMBER(Thread, pDeliverException));
1134 loadValueDirectFixed(cUnit, rlSrc, rARG0); // Get exception object
1135 callRuntimeHelper(cUnit, rTgt); // art_deliver_exception(exception);
1136}
1137
1138/*
1139 * Generate array store
1140 *
1141 */
1142void genArrayObjPut(CompilationUnit* cUnit, MIR* mir, RegLocation rlArray,
1143 RegLocation rlIndex, RegLocation rlSrc, int scale)
1144{
1145 RegisterClass regClass = oatRegClassBySize(kWord);
1146 int lenOffset = Array::LengthOffset().Int32Value();
1147 int dataOffset = Array::DataOffset(sizeof(Object*)).Int32Value();
1148
1149 oatFlushAllRegs(cUnit);
1150 /* Make sure it's a legal object Put. Use direct regs at first */
1151 loadValueDirectFixed(cUnit, rlArray, rARG1);
1152 loadValueDirectFixed(cUnit, rlSrc, rARG0);
1153
1154 /* null array object? */
1155 genNullCheck(cUnit, rlArray.sRegLow, rARG1, mir);
1156 int rTgt = loadHelper(cUnit, OFFSETOF_MEMBER(Thread,
1157 pCanPutArrayElementFromCode));
1158 /* Get the array's clazz */
1159 loadWordDisp(cUnit, rARG1, Object::ClassOffset().Int32Value(), rARG1);
1160 callRuntimeHelper(cUnit, rTgt);
1161 oatFreeTemp(cUnit, rARG0);
1162 oatFreeTemp(cUnit, rARG1);
1163
1164 // Now, redo loadValues in case they didn't survive the call
1165
1166 int regPtr;
1167 rlArray = loadValue(cUnit, rlArray, kCoreReg);
1168 rlIndex = loadValue(cUnit, rlIndex, kCoreReg);
1169
1170 if (oatIsTemp(cUnit, rlArray.lowReg)) {
1171 oatClobber(cUnit, rlArray.lowReg);
1172 regPtr = rlArray.lowReg;
1173 } else {
1174 regPtr = oatAllocTemp(cUnit);
buzbee82488f52012-03-02 08:20:26 -08001175 opRegCopy(cUnit, regPtr, rlArray.lowReg);
buzbee31a4a6f2012-02-28 15:36:15 -08001176 }
1177
1178 if (!(mir->optimizationFlags & MIR_IGNORE_RANGE_CHECK)) {
1179 int regLen = oatAllocTemp(cUnit);
1180 //NOTE: max live temps(4) here.
1181 /* Get len */
1182 loadWordDisp(cUnit, rlArray.lowReg, lenOffset, regLen);
1183 /* regPtr -> array data */
1184 opRegImm(cUnit, kOpAdd, regPtr, dataOffset);
1185 genRegRegCheck(cUnit, kCondCs, rlIndex.lowReg, regLen, mir,
1186 kThrowArrayBounds);
1187 oatFreeTemp(cUnit, regLen);
1188 } else {
1189 /* regPtr -> array data */
1190 opRegImm(cUnit, kOpAdd, regPtr, dataOffset);
1191 }
1192 /* at this point, regPtr points to array, 2 live temps */
1193 rlSrc = loadValue(cUnit, rlSrc, regClass);
1194 storeBaseIndexed(cUnit, regPtr, rlIndex.lowReg, rlSrc.lowReg,
1195 scale, kWord);
1196}
1197
1198/*
1199 * Generate array load
1200 */
1201void genArrayGet(CompilationUnit* cUnit, MIR* mir, OpSize size,
1202 RegLocation rlArray, RegLocation rlIndex,
1203 RegLocation rlDest, int scale)
1204{
1205 RegisterClass regClass = oatRegClassBySize(size);
1206 int lenOffset = Array::LengthOffset().Int32Value();
1207 int dataOffset;
1208 RegLocation rlResult;
1209 rlArray = loadValue(cUnit, rlArray, kCoreReg);
1210 rlIndex = loadValue(cUnit, rlIndex, kCoreReg);
buzbee31a4a6f2012-02-28 15:36:15 -08001211
1212 if (size == kLong || size == kDouble) {
1213 dataOffset = Array::DataOffset(sizeof(int64_t)).Int32Value();
1214 } else {
1215 dataOffset = Array::DataOffset(sizeof(int32_t)).Int32Value();
1216 }
1217
1218 /* null object? */
1219 genNullCheck(cUnit, rlArray.sRegLow, rlArray.lowReg, mir);
1220
Ian Rogersb5d09b22012-03-06 22:14:17 -08001221#if defined(TARGET_X86)
1222 if (!(mir->optimizationFlags & MIR_IGNORE_RANGE_CHECK)) {
1223 /* if (rlIndex >= [rlArray + lenOffset]) goto kThrowArrayBounds */
1224 genRegMemCheck(cUnit, kCondUge, rlIndex.lowReg, rlArray.lowReg,
1225 lenOffset, mir, kThrowArrayBounds);
1226 }
1227 if ((size == kLong) || (size == kDouble)) {
1228 rlResult = oatEvalLoc(cUnit, rlDest, regClass, true);
1229 loadBaseIndexedDisp(cUnit, NULL, rlArray.lowReg, rlIndex.lowReg, scale, dataOffset,
1230 rlResult.lowReg, rlResult.highReg, size, INVALID_SREG);
buzbee31a4a6f2012-02-28 15:36:15 -08001231
Ian Rogersb5d09b22012-03-06 22:14:17 -08001232 storeValueWide(cUnit, rlDest, rlResult);
1233 } else {
1234 rlResult = oatEvalLoc(cUnit, rlDest, regClass, true);
1235
1236 loadBaseIndexedDisp(cUnit, NULL, rlArray.lowReg, rlIndex.lowReg, scale, dataOffset,
1237 rlResult.lowReg, INVALID_REG, size, INVALID_SREG);
1238
1239 storeValue(cUnit, rlDest, rlResult);
1240 }
1241#else
1242 int regPtr = oatAllocTemp(cUnit);
buzbee31a4a6f2012-02-28 15:36:15 -08001243 if (!(mir->optimizationFlags & MIR_IGNORE_RANGE_CHECK)) {
1244 int regLen = oatAllocTemp(cUnit);
1245 /* Get len */
1246 loadWordDisp(cUnit, rlArray.lowReg, lenOffset, regLen);
1247 /* regPtr -> array data */
1248 opRegRegImm(cUnit, kOpAdd, regPtr, rlArray.lowReg, dataOffset);
Ian Rogersb5d09b22012-03-06 22:14:17 -08001249 // TODO: change kCondCS to a more meaningful name, is the sense of
1250 // carry-set/clear flipped?
buzbee31a4a6f2012-02-28 15:36:15 -08001251 genRegRegCheck(cUnit, kCondCs, rlIndex.lowReg, regLen, mir,
1252 kThrowArrayBounds);
1253 oatFreeTemp(cUnit, regLen);
1254 } else {
1255 /* regPtr -> array data */
1256 opRegRegImm(cUnit, kOpAdd, regPtr, rlArray.lowReg, dataOffset);
1257 }
1258 oatFreeTemp(cUnit, rlArray.lowReg);
1259 if ((size == kLong) || (size == kDouble)) {
1260 if (scale) {
1261 int rNewIndex = oatAllocTemp(cUnit);
1262 opRegRegImm(cUnit, kOpLsl, rNewIndex, rlIndex.lowReg, scale);
1263 opRegReg(cUnit, kOpAdd, regPtr, rNewIndex);
1264 oatFreeTemp(cUnit, rNewIndex);
1265 } else {
1266 opRegReg(cUnit, kOpAdd, regPtr, rlIndex.lowReg);
1267 }
1268 oatFreeTemp(cUnit, rlIndex.lowReg);
1269 rlResult = oatEvalLoc(cUnit, rlDest, regClass, true);
1270
1271 loadPair(cUnit, regPtr, rlResult.lowReg, rlResult.highReg);
1272
1273 oatFreeTemp(cUnit, regPtr);
1274 storeValueWide(cUnit, rlDest, rlResult);
1275 } else {
1276 rlResult = oatEvalLoc(cUnit, rlDest, regClass, true);
1277
1278 loadBaseIndexed(cUnit, regPtr, rlIndex.lowReg, rlResult.lowReg,
1279 scale, size);
1280
1281 oatFreeTemp(cUnit, regPtr);
1282 storeValue(cUnit, rlDest, rlResult);
1283 }
Ian Rogersb5d09b22012-03-06 22:14:17 -08001284#endif
buzbee31a4a6f2012-02-28 15:36:15 -08001285}
1286
1287/*
1288 * Generate array store
1289 *
1290 */
1291void genArrayPut(CompilationUnit* cUnit, MIR* mir, OpSize size,
1292 RegLocation rlArray, RegLocation rlIndex,
1293 RegLocation rlSrc, int scale)
1294{
1295 RegisterClass regClass = oatRegClassBySize(size);
1296 int lenOffset = Array::LengthOffset().Int32Value();
1297 int dataOffset;
1298
1299 if (size == kLong || size == kDouble) {
1300 dataOffset = Array::DataOffset(sizeof(int64_t)).Int32Value();
1301 } else {
1302 dataOffset = Array::DataOffset(sizeof(int32_t)).Int32Value();
1303 }
1304
1305 int regPtr;
1306 rlArray = loadValue(cUnit, rlArray, kCoreReg);
1307 rlIndex = loadValue(cUnit, rlIndex, kCoreReg);
1308
1309 if (oatIsTemp(cUnit, rlArray.lowReg)) {
1310 oatClobber(cUnit, rlArray.lowReg);
1311 regPtr = rlArray.lowReg;
1312 } else {
1313 regPtr = oatAllocTemp(cUnit);
buzbee82488f52012-03-02 08:20:26 -08001314 opRegCopy(cUnit, regPtr, rlArray.lowReg);
buzbee31a4a6f2012-02-28 15:36:15 -08001315 }
1316
1317 /* null object? */
1318 genNullCheck(cUnit, rlArray.sRegLow, rlArray.lowReg, mir);
1319
1320 if (!(mir->optimizationFlags & MIR_IGNORE_RANGE_CHECK)) {
1321 int regLen = oatAllocTemp(cUnit);
1322 //NOTE: max live temps(4) here.
1323 /* Get len */
1324 loadWordDisp(cUnit, rlArray.lowReg, lenOffset, regLen);
1325 /* regPtr -> array data */
1326 opRegImm(cUnit, kOpAdd, regPtr, dataOffset);
1327 genRegRegCheck(cUnit, kCondCs, rlIndex.lowReg, regLen, mir,
1328 kThrowArrayBounds);
1329 oatFreeTemp(cUnit, regLen);
1330 } else {
1331 /* regPtr -> array data */
1332 opRegImm(cUnit, kOpAdd, regPtr, dataOffset);
1333 }
1334 /* at this point, regPtr points to array, 2 live temps */
1335 if ((size == kLong) || (size == kDouble)) {
1336 //TUNING: specific wide routine that can handle fp regs
1337 if (scale) {
1338 int rNewIndex = oatAllocTemp(cUnit);
1339 opRegRegImm(cUnit, kOpLsl, rNewIndex, rlIndex.lowReg, scale);
1340 opRegReg(cUnit, kOpAdd, regPtr, rNewIndex);
1341 oatFreeTemp(cUnit, rNewIndex);
1342 } else {
1343 opRegReg(cUnit, kOpAdd, regPtr, rlIndex.lowReg);
1344 }
1345 rlSrc = loadValueWide(cUnit, rlSrc, regClass);
1346
1347 storePair(cUnit, regPtr, rlSrc.lowReg, rlSrc.highReg);
1348
1349 oatFreeTemp(cUnit, regPtr);
1350 } else {
1351 rlSrc = loadValue(cUnit, rlSrc, regClass);
1352
1353 storeBaseIndexed(cUnit, regPtr, rlIndex.lowReg, rlSrc.lowReg,
1354 scale, size);
1355 }
1356}
1357
1358void genLong3Addr(CompilationUnit* cUnit, MIR* mir, OpKind firstOp,
1359 OpKind secondOp, RegLocation rlDest,
1360 RegLocation rlSrc1, RegLocation rlSrc2)
1361{
1362 RegLocation rlResult;
1363#if defined(TARGET_ARM)
1364 /*
1365 * NOTE: This is the one place in the code in which we might have
1366 * as many as six live temporary registers. There are 5 in the normal
1367 * set for Arm. Until we have spill capabilities, temporarily add
1368 * lr to the temp set. It is safe to do this locally, but note that
1369 * lr is used explicitly elsewhere in the code generator and cannot
1370 * normally be used as a general temp register.
1371 */
1372 oatMarkTemp(cUnit, rLR); // Add lr to the temp pool
1373 oatFreeTemp(cUnit, rLR); // and make it available
1374#endif
1375 rlSrc1 = loadValueWide(cUnit, rlSrc1, kCoreReg);
1376 rlSrc2 = loadValueWide(cUnit, rlSrc2, kCoreReg);
1377 rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
1378 // The longs may overlap - use intermediate temp if so
1379 if (rlResult.lowReg == rlSrc1.highReg) {
1380 int tReg = oatAllocTemp(cUnit);
buzbee82488f52012-03-02 08:20:26 -08001381 opRegCopy(cUnit, tReg, rlSrc1.highReg);
buzbee31a4a6f2012-02-28 15:36:15 -08001382 opRegRegReg(cUnit, firstOp, rlResult.lowReg, rlSrc1.lowReg,
1383 rlSrc2.lowReg);
1384 opRegRegReg(cUnit, secondOp, rlResult.highReg, tReg,
1385 rlSrc2.highReg);
1386 oatFreeTemp(cUnit, tReg);
1387 } else {
1388 opRegRegReg(cUnit, firstOp, rlResult.lowReg, rlSrc1.lowReg,
1389 rlSrc2.lowReg);
1390 opRegRegReg(cUnit, secondOp, rlResult.highReg, rlSrc1.highReg,
1391 rlSrc2.highReg);
1392 }
1393 /*
1394 * NOTE: If rlDest refers to a frame variable in a large frame, the
1395 * following storeValueWide might need to allocate a temp register.
1396 * To further work around the lack of a spill capability, explicitly
1397 * free any temps from rlSrc1 & rlSrc2 that aren't still live in rlResult.
1398 * Remove when spill is functional.
1399 */
1400 freeRegLocTemps(cUnit, rlResult, rlSrc1);
1401 freeRegLocTemps(cUnit, rlResult, rlSrc2);
1402 storeValueWide(cUnit, rlDest, rlResult);
1403#if defined(TARGET_ARM)
1404 oatClobber(cUnit, rLR);
1405 oatUnmarkTemp(cUnit, rLR); // Remove lr from the temp pool
1406#endif
1407}
1408
1409
1410bool genShiftOpLong(CompilationUnit* cUnit, MIR* mir, RegLocation rlDest,
1411 RegLocation rlSrc1, RegLocation rlShift)
1412{
1413 int funcOffset;
1414
1415 switch( mir->dalvikInsn.opcode) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001416 case Instruction::SHL_LONG:
1417 case Instruction::SHL_LONG_2ADDR:
buzbee31a4a6f2012-02-28 15:36:15 -08001418 funcOffset = OFFSETOF_MEMBER(Thread, pShlLong);
1419 break;
Elliott Hughesadb8c672012-03-06 16:49:32 -08001420 case Instruction::SHR_LONG:
1421 case Instruction::SHR_LONG_2ADDR:
buzbee31a4a6f2012-02-28 15:36:15 -08001422 funcOffset = OFFSETOF_MEMBER(Thread, pShrLong);
1423 break;
Elliott Hughesadb8c672012-03-06 16:49:32 -08001424 case Instruction::USHR_LONG:
1425 case Instruction::USHR_LONG_2ADDR:
buzbee31a4a6f2012-02-28 15:36:15 -08001426 funcOffset = OFFSETOF_MEMBER(Thread, pUshrLong);
1427 break;
1428 default:
1429 LOG(FATAL) << "Unexpected case";
1430 return true;
1431 }
1432 oatFlushAllRegs(cUnit); /* Send everything to home location */
1433 int rTgt = loadHelper(cUnit, funcOffset);
1434 loadValueDirectWideFixed(cUnit, rlSrc1, rARG0, rARG1);
1435 loadValueDirect(cUnit, rlShift, rARG2);
1436 callRuntimeHelper(cUnit, rTgt);
1437 RegLocation rlResult = oatGetReturnWide(cUnit);
1438 storeValueWide(cUnit, rlDest, rlResult);
1439 return false;
1440}
1441
1442
1443bool genArithOpInt(CompilationUnit* cUnit, MIR* mir, RegLocation rlDest,
1444 RegLocation rlSrc1, RegLocation rlSrc2)
1445{
1446 OpKind op = kOpBkpt;
1447 bool callOut = false;
1448 bool checkZero = false;
1449 bool unary = false;
buzbee31a4a6f2012-02-28 15:36:15 -08001450 RegLocation rlResult;
1451 bool shiftOp = false;
Ian Rogersb5d09b22012-03-06 22:14:17 -08001452 int funcOffset;
1453 int retReg = rRET0;
buzbee31a4a6f2012-02-28 15:36:15 -08001454 switch (mir->dalvikInsn.opcode) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001455 case Instruction::NEG_INT:
buzbee31a4a6f2012-02-28 15:36:15 -08001456 op = kOpNeg;
1457 unary = true;
1458 break;
Elliott Hughesadb8c672012-03-06 16:49:32 -08001459 case Instruction::NOT_INT:
buzbee31a4a6f2012-02-28 15:36:15 -08001460 op = kOpMvn;
1461 unary = true;
1462 break;
Elliott Hughesadb8c672012-03-06 16:49:32 -08001463 case Instruction::ADD_INT:
1464 case Instruction::ADD_INT_2ADDR:
buzbee31a4a6f2012-02-28 15:36:15 -08001465 op = kOpAdd;
1466 break;
Elliott Hughesadb8c672012-03-06 16:49:32 -08001467 case Instruction::SUB_INT:
1468 case Instruction::SUB_INT_2ADDR:
buzbee31a4a6f2012-02-28 15:36:15 -08001469 op = kOpSub;
1470 break;
Elliott Hughesadb8c672012-03-06 16:49:32 -08001471 case Instruction::MUL_INT:
1472 case Instruction::MUL_INT_2ADDR:
buzbee31a4a6f2012-02-28 15:36:15 -08001473 op = kOpMul;
1474 break;
Elliott Hughesadb8c672012-03-06 16:49:32 -08001475 case Instruction::DIV_INT:
1476 case Instruction::DIV_INT_2ADDR:
buzbee31a4a6f2012-02-28 15:36:15 -08001477 checkZero = true;
Ian Rogersb5d09b22012-03-06 22:14:17 -08001478 op = kOpDiv;
1479 callOut = true;
buzbee31a4a6f2012-02-28 15:36:15 -08001480 funcOffset = OFFSETOF_MEMBER(Thread, pIdiv);
1481 retReg = rRET0;
1482 break;
buzbee5de34942012-03-01 14:51:57 -08001483 /* NOTE: returns in rARG1 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08001484 case Instruction::REM_INT:
1485 case Instruction::REM_INT_2ADDR:
buzbee31a4a6f2012-02-28 15:36:15 -08001486 checkZero = true;
Ian Rogersb5d09b22012-03-06 22:14:17 -08001487 op = kOpRem;
1488 callOut = true;
buzbee31a4a6f2012-02-28 15:36:15 -08001489 funcOffset = OFFSETOF_MEMBER(Thread, pIdivmod);
1490 retReg = rRET1;
1491 break;
Elliott Hughesadb8c672012-03-06 16:49:32 -08001492 case Instruction::AND_INT:
1493 case Instruction::AND_INT_2ADDR:
buzbee31a4a6f2012-02-28 15:36:15 -08001494 op = kOpAnd;
1495 break;
Elliott Hughesadb8c672012-03-06 16:49:32 -08001496 case Instruction::OR_INT:
1497 case Instruction::OR_INT_2ADDR:
buzbee31a4a6f2012-02-28 15:36:15 -08001498 op = kOpOr;
1499 break;
Elliott Hughesadb8c672012-03-06 16:49:32 -08001500 case Instruction::XOR_INT:
1501 case Instruction::XOR_INT_2ADDR:
buzbee31a4a6f2012-02-28 15:36:15 -08001502 op = kOpXor;
1503 break;
Elliott Hughesadb8c672012-03-06 16:49:32 -08001504 case Instruction::SHL_INT:
1505 case Instruction::SHL_INT_2ADDR:
buzbee31a4a6f2012-02-28 15:36:15 -08001506 shiftOp = true;
1507 op = kOpLsl;
1508 break;
Elliott Hughesadb8c672012-03-06 16:49:32 -08001509 case Instruction::SHR_INT:
1510 case Instruction::SHR_INT_2ADDR:
buzbee31a4a6f2012-02-28 15:36:15 -08001511 shiftOp = true;
1512 op = kOpAsr;
1513 break;
Elliott Hughesadb8c672012-03-06 16:49:32 -08001514 case Instruction::USHR_INT:
1515 case Instruction::USHR_INT_2ADDR:
buzbee31a4a6f2012-02-28 15:36:15 -08001516 shiftOp = true;
1517 op = kOpLsr;
1518 break;
1519 default:
1520 LOG(FATAL) << "Invalid word arith op: " <<
1521 (int)mir->dalvikInsn.opcode;
1522 }
1523 if (!callOut) {
1524 rlSrc1 = loadValue(cUnit, rlSrc1, kCoreReg);
1525 if (unary) {
1526 rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
1527 opRegReg(cUnit, op, rlResult.lowReg,
1528 rlSrc1.lowReg);
1529 } else {
1530 rlSrc2 = loadValue(cUnit, rlSrc2, kCoreReg);
Ian Rogersb5d09b22012-03-06 22:14:17 -08001531#if defined(TARGET_X86)
1532 rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
1533 opRegRegReg(cUnit, op, rlResult.lowReg,
1534 rlSrc1.lowReg, rlSrc2.lowReg);
1535#else
buzbee31a4a6f2012-02-28 15:36:15 -08001536 if (shiftOp) {
1537 int tReg = oatAllocTemp(cUnit);
1538 opRegRegImm(cUnit, kOpAnd, tReg, rlSrc2.lowReg, 31);
1539 rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
1540 opRegRegReg(cUnit, op, rlResult.lowReg,
1541 rlSrc1.lowReg, tReg);
1542 oatFreeTemp(cUnit, tReg);
1543 } else {
1544 rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
1545 opRegRegReg(cUnit, op, rlResult.lowReg,
1546 rlSrc1.lowReg, rlSrc2.lowReg);
1547 }
Ian Rogersb5d09b22012-03-06 22:14:17 -08001548#endif
buzbee31a4a6f2012-02-28 15:36:15 -08001549 }
1550 storeValue(cUnit, rlDest, rlResult);
1551 } else {
1552 RegLocation rlResult;
1553 oatFlushAllRegs(cUnit); /* Send everything to home location */
1554 loadValueDirectFixed(cUnit, rlSrc2, rRET1);
1555 int rTgt = loadHelper(cUnit, funcOffset);
1556 loadValueDirectFixed(cUnit, rlSrc1, rARG0);
1557 if (checkZero) {
1558 genImmedCheck(cUnit, kCondEq, rARG1, 0, mir, kThrowDivZero);
1559 }
1560 callRuntimeHelper(cUnit, rTgt);
1561 if (retReg == rRET0)
1562 rlResult = oatGetReturn(cUnit);
1563 else
1564 rlResult = oatGetReturnAlt(cUnit);
1565 storeValue(cUnit, rlDest, rlResult);
1566 }
1567 return false;
1568}
1569
1570/*
1571 * The following are the first-level codegen routines that analyze the format
1572 * of each bytecode then either dispatch special purpose codegen routines
1573 * or produce corresponding Thumb instructions directly.
1574 */
1575
1576bool isPowerOfTwo(int x)
1577{
1578 return (x & (x - 1)) == 0;
1579}
1580
1581// Returns true if no more than two bits are set in 'x'.
1582bool isPopCountLE2(unsigned int x)
1583{
1584 x &= x - 1;
1585 return (x & (x - 1)) == 0;
1586}
1587
1588// Returns the index of the lowest set bit in 'x'.
1589int lowestSetBit(unsigned int x) {
1590 int bit_posn = 0;
1591 while ((x & 0xf) == 0) {
1592 bit_posn += 4;
1593 x >>= 4;
1594 }
1595 while ((x & 1) == 0) {
1596 bit_posn++;
1597 x >>= 1;
1598 }
1599 return bit_posn;
1600}
1601
1602// Returns true if it added instructions to 'cUnit' to divide 'rlSrc' by 'lit'
1603// and store the result in 'rlDest'.
Elliott Hughesadb8c672012-03-06 16:49:32 -08001604bool handleEasyDivide(CompilationUnit* cUnit, Instruction::Code dalvikOpcode,
buzbee31a4a6f2012-02-28 15:36:15 -08001605 RegLocation rlSrc, RegLocation rlDest, int lit)
1606{
1607 if (lit < 2 || !isPowerOfTwo(lit)) {
1608 return false;
1609 }
1610 int k = lowestSetBit(lit);
1611 if (k >= 30) {
1612 // Avoid special cases.
1613 return false;
1614 }
Elliott Hughesadb8c672012-03-06 16:49:32 -08001615 bool div = (dalvikOpcode == Instruction::DIV_INT_LIT8 ||
1616 dalvikOpcode == Instruction::DIV_INT_LIT16);
buzbee31a4a6f2012-02-28 15:36:15 -08001617 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
1618 RegLocation rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
1619 if (div) {
1620 int tReg = oatAllocTemp(cUnit);
1621 if (lit == 2) {
1622 // Division by 2 is by far the most common division by constant.
1623 opRegRegImm(cUnit, kOpLsr, tReg, rlSrc.lowReg, 32 - k);
1624 opRegRegReg(cUnit, kOpAdd, tReg, tReg, rlSrc.lowReg);
1625 opRegRegImm(cUnit, kOpAsr, rlResult.lowReg, tReg, k);
1626 } else {
1627 opRegRegImm(cUnit, kOpAsr, tReg, rlSrc.lowReg, 31);
1628 opRegRegImm(cUnit, kOpLsr, tReg, tReg, 32 - k);
1629 opRegRegReg(cUnit, kOpAdd, tReg, tReg, rlSrc.lowReg);
1630 opRegRegImm(cUnit, kOpAsr, rlResult.lowReg, tReg, k);
1631 }
1632 } else {
1633 int cReg = oatAllocTemp(cUnit);
1634 loadConstant(cUnit, cReg, lit - 1);
1635 int tReg1 = oatAllocTemp(cUnit);
1636 int tReg2 = oatAllocTemp(cUnit);
1637 if (lit == 2) {
1638 opRegRegImm(cUnit, kOpLsr, tReg1, rlSrc.lowReg, 32 - k);
1639 opRegRegReg(cUnit, kOpAdd, tReg2, tReg1, rlSrc.lowReg);
1640 opRegRegReg(cUnit, kOpAnd, tReg2, tReg2, cReg);
1641 opRegRegReg(cUnit, kOpSub, rlResult.lowReg, tReg2, tReg1);
1642 } else {
1643 opRegRegImm(cUnit, kOpAsr, tReg1, rlSrc.lowReg, 31);
1644 opRegRegImm(cUnit, kOpLsr, tReg1, tReg1, 32 - k);
1645 opRegRegReg(cUnit, kOpAdd, tReg2, tReg1, rlSrc.lowReg);
1646 opRegRegReg(cUnit, kOpAnd, tReg2, tReg2, cReg);
1647 opRegRegReg(cUnit, kOpSub, rlResult.lowReg, tReg2, tReg1);
1648 }
1649 }
1650 storeValue(cUnit, rlDest, rlResult);
1651 return true;
1652}
1653
1654void genMultiplyByTwoBitMultiplier(CompilationUnit* cUnit, RegLocation rlSrc,
1655 RegLocation rlResult, int lit,
1656 int firstBit, int secondBit)
1657{
buzbee0398c422012-03-02 15:22:47 -08001658#if defined(TARGET_ARM)
buzbee31a4a6f2012-02-28 15:36:15 -08001659 opRegRegRegShift(cUnit, kOpAdd, rlResult.lowReg, rlSrc.lowReg, rlSrc.lowReg,
1660 encodeShift(kArmLsl, secondBit - firstBit));
buzbee0398c422012-03-02 15:22:47 -08001661#else
1662 int tReg = oatAllocTemp(cUnit);
1663 opRegRegImm(cUnit, kOpLsl, tReg, rlSrc.lowReg, secondBit - firstBit);
1664 opRegRegReg(cUnit, kOpAdd, rlResult.lowReg, rlSrc.lowReg, tReg);
1665 oatFreeTemp(cUnit, tReg);
buzbee5de34942012-03-01 14:51:57 -08001666#endif
buzbee31a4a6f2012-02-28 15:36:15 -08001667 if (firstBit != 0) {
1668 opRegRegImm(cUnit, kOpLsl, rlResult.lowReg, rlResult.lowReg, firstBit);
1669 }
1670}
1671
1672// Returns true if it added instructions to 'cUnit' to multiply 'rlSrc' by 'lit'
1673// and store the result in 'rlDest'.
1674bool handleEasyMultiply(CompilationUnit* cUnit, RegLocation rlSrc,
1675 RegLocation rlDest, int lit)
1676{
1677 // Can we simplify this multiplication?
1678 bool powerOfTwo = false;
1679 bool popCountLE2 = false;
1680 bool powerOfTwoMinusOne = false;
1681 if (lit < 2) {
1682 // Avoid special cases.
1683 return false;
1684 } else if (isPowerOfTwo(lit)) {
1685 powerOfTwo = true;
1686 } else if (isPopCountLE2(lit)) {
1687 popCountLE2 = true;
1688 } else if (isPowerOfTwo(lit + 1)) {
1689 powerOfTwoMinusOne = true;
1690 } else {
1691 return false;
1692 }
1693 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
1694 RegLocation rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
1695 if (powerOfTwo) {
1696 // Shift.
1697 opRegRegImm(cUnit, kOpLsl, rlResult.lowReg, rlSrc.lowReg,
1698 lowestSetBit(lit));
1699 } else if (popCountLE2) {
1700 // Shift and add and shift.
1701 int firstBit = lowestSetBit(lit);
1702 int secondBit = lowestSetBit(lit ^ (1 << firstBit));
1703 genMultiplyByTwoBitMultiplier(cUnit, rlSrc, rlResult, lit,
1704 firstBit, secondBit);
1705 } else {
1706 // Reverse subtract: (src << (shift + 1)) - src.
1707 DCHECK(powerOfTwoMinusOne);
1708 // TUNING: rsb dst, src, src lsl#lowestSetBit(lit + 1)
1709 int tReg = oatAllocTemp(cUnit);
1710 opRegRegImm(cUnit, kOpLsl, tReg, rlSrc.lowReg, lowestSetBit(lit + 1));
1711 opRegRegReg(cUnit, kOpSub, rlResult.lowReg, tReg, rlSrc.lowReg);
1712 }
1713 storeValue(cUnit, rlDest, rlResult);
1714 return true;
1715}
1716
1717bool genArithOpIntLit(CompilationUnit* cUnit, MIR* mir, RegLocation rlDest,
1718 RegLocation rlSrc, int lit)
1719{
Elliott Hughesadb8c672012-03-06 16:49:32 -08001720 Instruction::Code dalvikOpcode = mir->dalvikInsn.opcode;
buzbee31a4a6f2012-02-28 15:36:15 -08001721 RegLocation rlResult;
1722 OpKind op = (OpKind)0; /* Make gcc happy */
1723 int shiftOp = false;
1724 bool isDiv = false;
1725 int funcOffset;
1726 int rTgt;
1727
1728 switch (dalvikOpcode) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001729 case Instruction::RSUB_INT_LIT8:
1730 case Instruction::RSUB_INT: {
buzbee31a4a6f2012-02-28 15:36:15 -08001731 int tReg;
1732 //TUNING: add support for use of Arm rsub op
1733 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
1734 tReg = oatAllocTemp(cUnit);
1735 loadConstant(cUnit, tReg, lit);
1736 rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
1737 opRegRegReg(cUnit, kOpSub, rlResult.lowReg,
1738 tReg, rlSrc.lowReg);
1739 storeValue(cUnit, rlDest, rlResult);
1740 return false;
1741 break;
1742 }
1743
Elliott Hughesadb8c672012-03-06 16:49:32 -08001744 case Instruction::ADD_INT_LIT8:
1745 case Instruction::ADD_INT_LIT16:
buzbee31a4a6f2012-02-28 15:36:15 -08001746 op = kOpAdd;
1747 break;
Elliott Hughesadb8c672012-03-06 16:49:32 -08001748 case Instruction::MUL_INT_LIT8:
1749 case Instruction::MUL_INT_LIT16: {
buzbee31a4a6f2012-02-28 15:36:15 -08001750 if (handleEasyMultiply(cUnit, rlSrc, rlDest, lit)) {
1751 return false;
1752 }
1753 op = kOpMul;
1754 break;
1755 }
Elliott Hughesadb8c672012-03-06 16:49:32 -08001756 case Instruction::AND_INT_LIT8:
1757 case Instruction::AND_INT_LIT16:
buzbee31a4a6f2012-02-28 15:36:15 -08001758 op = kOpAnd;
1759 break;
Elliott Hughesadb8c672012-03-06 16:49:32 -08001760 case Instruction::OR_INT_LIT8:
1761 case Instruction::OR_INT_LIT16:
buzbee31a4a6f2012-02-28 15:36:15 -08001762 op = kOpOr;
1763 break;
Elliott Hughesadb8c672012-03-06 16:49:32 -08001764 case Instruction::XOR_INT_LIT8:
1765 case Instruction::XOR_INT_LIT16:
buzbee31a4a6f2012-02-28 15:36:15 -08001766 op = kOpXor;
1767 break;
Elliott Hughesadb8c672012-03-06 16:49:32 -08001768 case Instruction::SHL_INT_LIT8:
buzbee31a4a6f2012-02-28 15:36:15 -08001769 lit &= 31;
1770 shiftOp = true;
1771 op = kOpLsl;
1772 break;
Elliott Hughesadb8c672012-03-06 16:49:32 -08001773 case Instruction::SHR_INT_LIT8:
buzbee31a4a6f2012-02-28 15:36:15 -08001774 lit &= 31;
1775 shiftOp = true;
1776 op = kOpAsr;
1777 break;
Elliott Hughesadb8c672012-03-06 16:49:32 -08001778 case Instruction::USHR_INT_LIT8:
buzbee31a4a6f2012-02-28 15:36:15 -08001779 lit &= 31;
1780 shiftOp = true;
1781 op = kOpLsr;
1782 break;
1783
Elliott Hughesadb8c672012-03-06 16:49:32 -08001784 case Instruction::DIV_INT_LIT8:
1785 case Instruction::DIV_INT_LIT16:
1786 case Instruction::REM_INT_LIT8:
1787 case Instruction::REM_INT_LIT16:
buzbee31a4a6f2012-02-28 15:36:15 -08001788 if (lit == 0) {
1789 genImmedCheck(cUnit, kCondAl, 0, 0, mir, kThrowDivZero);
1790 return false;
1791 }
1792 if (handleEasyDivide(cUnit, dalvikOpcode, rlSrc, rlDest, lit)) {
1793 return false;
1794 }
1795 oatFlushAllRegs(cUnit); /* Everything to home location */
1796 loadValueDirectFixed(cUnit, rlSrc, rARG0);
1797 oatClobber(cUnit, rARG0);
Elliott Hughesadb8c672012-03-06 16:49:32 -08001798 if ((dalvikOpcode == Instruction::DIV_INT_LIT8) ||
1799 (dalvikOpcode == Instruction::DIV_INT_LIT16)) {
buzbee31a4a6f2012-02-28 15:36:15 -08001800 funcOffset = OFFSETOF_MEMBER(Thread, pIdiv);
1801 isDiv = true;
1802 } else {
1803 funcOffset = OFFSETOF_MEMBER(Thread, pIdivmod);
1804 isDiv = false;
1805 }
1806 rTgt = loadHelper(cUnit, funcOffset);
1807 loadConstant(cUnit, rARG1, lit);
1808 callRuntimeHelper(cUnit, rTgt);
1809 if (isDiv)
1810 rlResult = oatGetReturn(cUnit);
1811 else
1812 rlResult = oatGetReturnAlt(cUnit);
1813 storeValue(cUnit, rlDest, rlResult);
1814 return false;
1815 break;
1816 default:
1817 return true;
1818 }
1819 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
1820 rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
1821 // Avoid shifts by literal 0 - no support in Thumb. Change to copy
1822 if (shiftOp && (lit == 0)) {
buzbee82488f52012-03-02 08:20:26 -08001823 opRegCopy(cUnit, rlResult.lowReg, rlSrc.lowReg);
buzbee31a4a6f2012-02-28 15:36:15 -08001824 } else {
1825 opRegRegImm(cUnit, op, rlResult.lowReg, rlSrc.lowReg, lit);
1826 }
1827 storeValue(cUnit, rlDest, rlResult);
1828 return false;
1829}
1830
1831bool genArithOpLong(CompilationUnit* cUnit, MIR* mir, RegLocation rlDest,
1832 RegLocation rlSrc1, RegLocation rlSrc2)
1833{
1834 RegLocation rlResult;
1835 OpKind firstOp = kOpBkpt;
1836 OpKind secondOp = kOpBkpt;
1837 bool callOut = false;
1838 bool checkZero = false;
1839 int funcOffset;
1840 int retReg = rRET0;
1841
1842 switch (mir->dalvikInsn.opcode) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001843 case Instruction::NOT_LONG:
buzbee31a4a6f2012-02-28 15:36:15 -08001844 rlSrc2 = loadValueWide(cUnit, rlSrc2, kCoreReg);
1845 rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
1846 // Check for destructive overlap
1847 if (rlResult.lowReg == rlSrc2.highReg) {
1848 int tReg = oatAllocTemp(cUnit);
buzbee82488f52012-03-02 08:20:26 -08001849 opRegCopy(cUnit, tReg, rlSrc2.highReg);
buzbee31a4a6f2012-02-28 15:36:15 -08001850 opRegReg(cUnit, kOpMvn, rlResult.lowReg, rlSrc2.lowReg);
1851 opRegReg(cUnit, kOpMvn, rlResult.highReg, tReg);
1852 oatFreeTemp(cUnit, tReg);
1853 } else {
1854 opRegReg(cUnit, kOpMvn, rlResult.lowReg, rlSrc2.lowReg);
1855 opRegReg(cUnit, kOpMvn, rlResult.highReg, rlSrc2.highReg);
1856 }
1857 storeValueWide(cUnit, rlDest, rlResult);
1858 return false;
1859 break;
Elliott Hughesadb8c672012-03-06 16:49:32 -08001860 case Instruction::ADD_LONG:
1861 case Instruction::ADD_LONG_2ADDR:
buzbeec5159d52012-03-03 11:48:39 -08001862#if defined(TARGET_MIPS)
1863 return genAddLong(cUnit, mir, rlDest, rlSrc1, rlSrc2);
1864#else
buzbee31a4a6f2012-02-28 15:36:15 -08001865 firstOp = kOpAdd;
1866 secondOp = kOpAdc;
1867 break;
buzbeec5159d52012-03-03 11:48:39 -08001868#endif
Elliott Hughesadb8c672012-03-06 16:49:32 -08001869 case Instruction::SUB_LONG:
1870 case Instruction::SUB_LONG_2ADDR:
buzbeec5159d52012-03-03 11:48:39 -08001871#if defined(TARGET_MIPS)
1872 return genSubLong(cUnit, mir, rlDest, rlSrc1, rlSrc2);
1873#else
buzbee31a4a6f2012-02-28 15:36:15 -08001874 firstOp = kOpSub;
1875 secondOp = kOpSbc;
1876 break;
buzbeec5159d52012-03-03 11:48:39 -08001877#endif
Elliott Hughesadb8c672012-03-06 16:49:32 -08001878 case Instruction::MUL_LONG:
1879 case Instruction::MUL_LONG_2ADDR:
buzbee31a4a6f2012-02-28 15:36:15 -08001880 callOut = true;
1881 retReg = rRET0;
1882 funcOffset = OFFSETOF_MEMBER(Thread, pLmul);
1883 break;
Elliott Hughesadb8c672012-03-06 16:49:32 -08001884 case Instruction::DIV_LONG:
1885 case Instruction::DIV_LONG_2ADDR:
buzbee31a4a6f2012-02-28 15:36:15 -08001886 callOut = true;
1887 checkZero = true;
1888 retReg = rRET0;
1889 funcOffset = OFFSETOF_MEMBER(Thread, pLdivmod);
1890 break;
1891 /* NOTE - result is in rARG2/rARG3 instead of rRET0/rRET1 */
1892 // FIXME: is true, or could be made true, or other targets?
Elliott Hughesadb8c672012-03-06 16:49:32 -08001893 case Instruction::REM_LONG:
1894 case Instruction::REM_LONG_2ADDR:
buzbee31a4a6f2012-02-28 15:36:15 -08001895 callOut = true;
1896 checkZero = true;
1897 funcOffset = OFFSETOF_MEMBER(Thread, pLdivmod);
1898 retReg = rARG2;
1899 break;
Elliott Hughesadb8c672012-03-06 16:49:32 -08001900 case Instruction::AND_LONG_2ADDR:
1901 case Instruction::AND_LONG:
buzbee31a4a6f2012-02-28 15:36:15 -08001902 firstOp = kOpAnd;
1903 secondOp = kOpAnd;
1904 break;
Elliott Hughesadb8c672012-03-06 16:49:32 -08001905 case Instruction::OR_LONG:
1906 case Instruction::OR_LONG_2ADDR:
buzbee31a4a6f2012-02-28 15:36:15 -08001907 firstOp = kOpOr;
1908 secondOp = kOpOr;
1909 break;
Elliott Hughesadb8c672012-03-06 16:49:32 -08001910 case Instruction::XOR_LONG:
1911 case Instruction::XOR_LONG_2ADDR:
buzbee31a4a6f2012-02-28 15:36:15 -08001912 firstOp = kOpXor;
1913 secondOp = kOpXor;
1914 break;
Elliott Hughesadb8c672012-03-06 16:49:32 -08001915 case Instruction::NEG_LONG: {
buzbeec5159d52012-03-03 11:48:39 -08001916 return genNegLong(cUnit, mir, rlDest, rlSrc2);
buzbee31a4a6f2012-02-28 15:36:15 -08001917 }
1918 default:
1919 LOG(FATAL) << "Invalid long arith op";
1920 }
1921 if (!callOut) {
1922 genLong3Addr(cUnit, mir, firstOp, secondOp, rlDest, rlSrc1, rlSrc2);
1923 } else {
1924 int rTgt;
1925 oatFlushAllRegs(cUnit); /* Send everything to home location */
1926 if (checkZero) {
Ian Rogersb5d09b22012-03-06 22:14:17 -08001927#if defined(TARGET_X86)
1928 UNIMPLEMENTED(FATAL);
1929#else
buzbee31a4a6f2012-02-28 15:36:15 -08001930 loadValueDirectWideFixed(cUnit, rlSrc2, rARG2, rARG3);
Ian Rogersb5d09b22012-03-06 22:14:17 -08001931#endif
buzbee31a4a6f2012-02-28 15:36:15 -08001932 rTgt = loadHelper(cUnit, funcOffset);
1933 loadValueDirectWideFixed(cUnit, rlSrc1, rARG0, rARG1);
1934 int tReg = oatAllocTemp(cUnit);
1935#if defined(TARGET_ARM)
1936 newLIR4(cUnit, kThumb2OrrRRRs, tReg, rARG2, rARG3, 0);
1937 oatFreeTemp(cUnit, tReg);
1938 genCheck(cUnit, kCondEq, mir, kThrowDivZero);
1939#else
Ian Rogersb5d09b22012-03-06 22:14:17 -08001940#if defined(TARGET_X86)
1941 UNIMPLEMENTED(FATAL);
1942#else
buzbee31a4a6f2012-02-28 15:36:15 -08001943 opRegRegReg(cUnit, kOpOr, tReg, rARG2, rARG3);
Ian Rogersb5d09b22012-03-06 22:14:17 -08001944#endif
buzbee5de34942012-03-01 14:51:57 -08001945 genImmedCheck(cUnit, kCondEq, tReg, 0, mir, kThrowDivZero);
buzbee31a4a6f2012-02-28 15:36:15 -08001946 oatFreeTemp(cUnit, tReg);
1947#endif
1948 } else {
1949 rTgt = loadHelper(cUnit, funcOffset);
1950 loadValueDirectWideFixed(cUnit, rlSrc1, rARG0, rARG1);
Ian Rogersb5d09b22012-03-06 22:14:17 -08001951#if defined(TARGET_X86)
1952 UNIMPLEMENTED(FATAL);
1953#else
buzbee31a4a6f2012-02-28 15:36:15 -08001954 loadValueDirectWideFixed(cUnit, rlSrc2, rARG2, rARG3);
Ian Rogersb5d09b22012-03-06 22:14:17 -08001955#endif
buzbee31a4a6f2012-02-28 15:36:15 -08001956 }
1957 callRuntimeHelper(cUnit, rTgt);
1958 // Adjust return regs in to handle case of rem returning rARG2/rARG3
1959 if (retReg == rRET0)
1960 rlResult = oatGetReturnWide(cUnit);
1961 else
1962 rlResult = oatGetReturnWideAlt(cUnit);
1963 storeValueWide(cUnit, rlDest, rlResult);
1964 }
1965 return false;
1966}
1967
1968bool genConversionCall(CompilationUnit* cUnit, MIR* mir, int funcOffset,
1969 int srcSize, int tgtSize)
1970{
1971 /*
1972 * Don't optimize the register usage since it calls out to support
1973 * functions
1974 */
1975 RegLocation rlSrc;
1976 RegLocation rlDest;
1977 oatFlushAllRegs(cUnit); /* Send everything to home location */
1978 int rTgt = loadHelper(cUnit, funcOffset);
1979 if (srcSize == 1) {
1980 rlSrc = oatGetSrc(cUnit, mir, 0);
1981 loadValueDirectFixed(cUnit, rlSrc, rARG0);
1982 } else {
1983 rlSrc = oatGetSrcWide(cUnit, mir, 0, 1);
1984 loadValueDirectWideFixed(cUnit, rlSrc, rARG0, rARG1);
1985 }
1986 callRuntimeHelper(cUnit, rTgt);
1987 if (tgtSize == 1) {
1988 RegLocation rlResult;
1989 rlDest = oatGetDest(cUnit, mir, 0);
1990 rlResult = oatGetReturn(cUnit);
1991 storeValue(cUnit, rlDest, rlResult);
1992 } else {
1993 RegLocation rlResult;
1994 rlDest = oatGetDestWide(cUnit, mir, 0, 1);
1995 rlResult = oatGetReturnWide(cUnit);
1996 storeValueWide(cUnit, rlDest, rlResult);
1997 }
1998 return false;
1999}
2000
2001void genNegFloat(CompilationUnit* cUnit, RegLocation rlDest, RegLocation rlSrc);
2002bool genArithOpFloatPortable(CompilationUnit* cUnit, MIR* mir,
2003 RegLocation rlDest, RegLocation rlSrc1,
2004 RegLocation rlSrc2)
2005{
2006 RegLocation rlResult;
2007 int funcOffset;
2008
2009 switch (mir->dalvikInsn.opcode) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002010 case Instruction::ADD_FLOAT_2ADDR:
2011 case Instruction::ADD_FLOAT:
buzbee31a4a6f2012-02-28 15:36:15 -08002012 funcOffset = OFFSETOF_MEMBER(Thread, pFadd);
2013 break;
Elliott Hughesadb8c672012-03-06 16:49:32 -08002014 case Instruction::SUB_FLOAT_2ADDR:
2015 case Instruction::SUB_FLOAT:
buzbee31a4a6f2012-02-28 15:36:15 -08002016 funcOffset = OFFSETOF_MEMBER(Thread, pFsub);
2017 break;
Elliott Hughesadb8c672012-03-06 16:49:32 -08002018 case Instruction::DIV_FLOAT_2ADDR:
2019 case Instruction::DIV_FLOAT:
buzbee31a4a6f2012-02-28 15:36:15 -08002020 funcOffset = OFFSETOF_MEMBER(Thread, pFdiv);
2021 break;
Elliott Hughesadb8c672012-03-06 16:49:32 -08002022 case Instruction::MUL_FLOAT_2ADDR:
2023 case Instruction::MUL_FLOAT:
buzbee31a4a6f2012-02-28 15:36:15 -08002024 funcOffset = OFFSETOF_MEMBER(Thread, pFmul);
2025 break;
Elliott Hughesadb8c672012-03-06 16:49:32 -08002026 case Instruction::REM_FLOAT_2ADDR:
2027 case Instruction::REM_FLOAT:
buzbee31a4a6f2012-02-28 15:36:15 -08002028 funcOffset = OFFSETOF_MEMBER(Thread, pFmodf);
2029 break;
Elliott Hughesadb8c672012-03-06 16:49:32 -08002030 case Instruction::NEG_FLOAT: {
buzbee31a4a6f2012-02-28 15:36:15 -08002031 genNegFloat(cUnit, rlDest, rlSrc1);
2032 return false;
2033 }
2034 default:
2035 return true;
2036 }
2037 oatFlushAllRegs(cUnit); /* Send everything to home location */
2038 int rTgt = loadHelper(cUnit, funcOffset);
2039 loadValueDirectFixed(cUnit, rlSrc1, rARG0);
2040 loadValueDirectFixed(cUnit, rlSrc2, rARG1);
2041 callRuntimeHelper(cUnit, rTgt);
2042 rlResult = oatGetReturn(cUnit);
2043 storeValue(cUnit, rlDest, rlResult);
2044 return false;
2045}
2046
2047void genNegDouble(CompilationUnit* cUnit, RegLocation rlDst, RegLocation rlSrc);
2048bool genArithOpDoublePortable(CompilationUnit* cUnit, MIR* mir,
2049 RegLocation rlDest, RegLocation rlSrc1,
2050 RegLocation rlSrc2)
2051{
2052 RegLocation rlResult;
2053 int funcOffset;
2054
2055 switch (mir->dalvikInsn.opcode) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002056 case Instruction::ADD_DOUBLE_2ADDR:
2057 case Instruction::ADD_DOUBLE:
buzbee31a4a6f2012-02-28 15:36:15 -08002058 funcOffset = OFFSETOF_MEMBER(Thread, pDadd);
2059 break;
Elliott Hughesadb8c672012-03-06 16:49:32 -08002060 case Instruction::SUB_DOUBLE_2ADDR:
2061 case Instruction::SUB_DOUBLE:
buzbee31a4a6f2012-02-28 15:36:15 -08002062 funcOffset = OFFSETOF_MEMBER(Thread, pDsub);
2063 break;
Elliott Hughesadb8c672012-03-06 16:49:32 -08002064 case Instruction::DIV_DOUBLE_2ADDR:
2065 case Instruction::DIV_DOUBLE:
buzbee31a4a6f2012-02-28 15:36:15 -08002066 funcOffset = OFFSETOF_MEMBER(Thread, pDdiv);
2067 break;
Elliott Hughesadb8c672012-03-06 16:49:32 -08002068 case Instruction::MUL_DOUBLE_2ADDR:
2069 case Instruction::MUL_DOUBLE:
buzbee31a4a6f2012-02-28 15:36:15 -08002070 funcOffset = OFFSETOF_MEMBER(Thread, pDmul);
2071 break;
Elliott Hughesadb8c672012-03-06 16:49:32 -08002072 case Instruction::REM_DOUBLE_2ADDR:
2073 case Instruction::REM_DOUBLE:
buzbee31a4a6f2012-02-28 15:36:15 -08002074 funcOffset = OFFSETOF_MEMBER(Thread, pFmod);
2075 break;
Elliott Hughesadb8c672012-03-06 16:49:32 -08002076 case Instruction::NEG_DOUBLE: {
buzbee31a4a6f2012-02-28 15:36:15 -08002077 genNegDouble(cUnit, rlDest, rlSrc1);
2078 return false;
2079 }
2080 default:
2081 return true;
2082 }
2083 oatFlushAllRegs(cUnit); /* Send everything to home location */
2084 int rTgt = loadHelper(cUnit, funcOffset);
2085 loadValueDirectWideFixed(cUnit, rlSrc1, rARG0, rARG1);
Ian Rogersb5d09b22012-03-06 22:14:17 -08002086#if defined(TARGET_X86)
2087 UNIMPLEMENTED(FATAL);
2088#else
buzbee31a4a6f2012-02-28 15:36:15 -08002089 loadValueDirectWideFixed(cUnit, rlSrc2, rARG2, rARG3);
Ian Rogersb5d09b22012-03-06 22:14:17 -08002090#endif
buzbee31a4a6f2012-02-28 15:36:15 -08002091 callRuntimeHelper(cUnit, rTgt);
2092 rlResult = oatGetReturnWide(cUnit);
2093 storeValueWide(cUnit, rlDest, rlResult);
2094 return false;
2095}
2096
2097bool genConversionPortable(CompilationUnit* cUnit, MIR* mir)
2098{
Elliott Hughesadb8c672012-03-06 16:49:32 -08002099 Instruction::Code opcode = mir->dalvikInsn.opcode;
buzbee31a4a6f2012-02-28 15:36:15 -08002100
2101 switch (opcode) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002102 case Instruction::INT_TO_FLOAT:
buzbee31a4a6f2012-02-28 15:36:15 -08002103 return genConversionCall(cUnit, mir, OFFSETOF_MEMBER(Thread, pI2f),
2104 1, 1);
Elliott Hughesadb8c672012-03-06 16:49:32 -08002105 case Instruction::FLOAT_TO_INT:
buzbee31a4a6f2012-02-28 15:36:15 -08002106 return genConversionCall(cUnit, mir, OFFSETOF_MEMBER(Thread, pF2iz),
2107 1, 1);
Elliott Hughesadb8c672012-03-06 16:49:32 -08002108 case Instruction::DOUBLE_TO_FLOAT:
buzbee31a4a6f2012-02-28 15:36:15 -08002109 return genConversionCall(cUnit, mir, OFFSETOF_MEMBER(Thread, pD2f),
2110 2, 1);
Elliott Hughesadb8c672012-03-06 16:49:32 -08002111 case Instruction::FLOAT_TO_DOUBLE:
buzbee31a4a6f2012-02-28 15:36:15 -08002112 return genConversionCall(cUnit, mir, OFFSETOF_MEMBER(Thread, pF2d),
2113 1, 2);
Elliott Hughesadb8c672012-03-06 16:49:32 -08002114 case Instruction::INT_TO_DOUBLE:
buzbee31a4a6f2012-02-28 15:36:15 -08002115 return genConversionCall(cUnit, mir, OFFSETOF_MEMBER(Thread, pI2d),
2116 1, 2);
Elliott Hughesadb8c672012-03-06 16:49:32 -08002117 case Instruction::DOUBLE_TO_INT:
buzbee31a4a6f2012-02-28 15:36:15 -08002118 return genConversionCall(cUnit, mir, OFFSETOF_MEMBER(Thread, pD2iz),
2119 2, 1);
Elliott Hughesadb8c672012-03-06 16:49:32 -08002120 case Instruction::FLOAT_TO_LONG:
buzbee31a4a6f2012-02-28 15:36:15 -08002121 return genConversionCall(cUnit, mir, OFFSETOF_MEMBER(Thread,
2122 pF2l), 1, 2);
Elliott Hughesadb8c672012-03-06 16:49:32 -08002123 case Instruction::LONG_TO_FLOAT:
buzbee31a4a6f2012-02-28 15:36:15 -08002124 return genConversionCall(cUnit, mir, OFFSETOF_MEMBER(Thread, pL2f),
2125 2, 1);
Elliott Hughesadb8c672012-03-06 16:49:32 -08002126 case Instruction::DOUBLE_TO_LONG:
buzbee31a4a6f2012-02-28 15:36:15 -08002127 return genConversionCall(cUnit, mir, OFFSETOF_MEMBER(Thread,
2128 pD2l), 2, 2);
Elliott Hughesadb8c672012-03-06 16:49:32 -08002129 case Instruction::LONG_TO_DOUBLE:
buzbee31a4a6f2012-02-28 15:36:15 -08002130 return genConversionCall(cUnit, mir, OFFSETOF_MEMBER(Thread, pL2d),
2131 2, 2);
2132 default:
2133 return true;
2134 }
2135 return false;
2136}
2137
2138/*
2139 * Generate callout to updateDebugger. Note that we're overloading
2140 * the use of rSUSPEND here. When the debugger is active, this
2141 * register holds the address of the update function. So, if it's
2142 * non-null, we call out to it.
2143 *
2144 * Note also that rRET0 and rRET1 must be preserved across this
2145 * code. This must be handled by the stub.
2146 */
2147void genDebuggerUpdate(CompilationUnit* cUnit, int32_t offset)
2148{
2149 // Following DCHECK verifies that dPC is in range of single load immediate
2150 DCHECK((offset == DEBUGGER_METHOD_ENTRY) ||
2151 (offset == DEBUGGER_METHOD_EXIT) || ((offset & 0xffff) == offset));
2152 oatClobberCalleeSave(cUnit);
2153#if defined(TARGET_ARM)
2154 opRegImm(cUnit, kOpCmp, rSUSPEND, 0);
buzbee82488f52012-03-02 08:20:26 -08002155 opIT(cUnit, kArmCondNe, "T");
buzbee31a4a6f2012-02-28 15:36:15 -08002156 loadConstant(cUnit, rARG2, offset); // arg2 <- Entry code
2157 opReg(cUnit, kOpBlx, rSUSPEND);
Ian Rogersb5d09b22012-03-06 22:14:17 -08002158#elif defined(TARGET_X86)
2159 UNIMPLEMENTED(FATAL);
buzbee31a4a6f2012-02-28 15:36:15 -08002160#else
buzbee82488f52012-03-02 08:20:26 -08002161 LIR* branch = opCmpImmBranch(cUnit, kCondEq, rSUSPEND, 0, NULL);
buzbee31a4a6f2012-02-28 15:36:15 -08002162 loadConstant(cUnit, rARG2, offset);
2163 opReg(cUnit, kOpBlx, rSUSPEND);
2164 LIR* target = newLIR0(cUnit, kPseudoTargetLabel);
buzbee31a4a6f2012-02-28 15:36:15 -08002165 branch->target = (LIR*)target;
2166#endif
2167 oatFreeTemp(cUnit, rARG2);
2168}
2169
2170/* Check if we need to check for pending suspend request */
2171void genSuspendTest(CompilationUnit* cUnit, MIR* mir)
2172{
2173 if (NO_SUSPEND || (mir->optimizationFlags & MIR_IGNORE_SUSPEND_CHECK)) {
2174 return;
2175 }
2176 oatFlushAllRegs(cUnit);
buzbee31a4a6f2012-02-28 15:36:15 -08002177 if (cUnit->genDebugger) {
2178 // If generating code for the debugger, always check for suspension
buzbee86a4bce2012-03-06 18:15:00 -08002179 int rTgt = loadHelper(cUnit, OFFSETOF_MEMBER(Thread,
2180 pTestSuspendFromCode));
2181 opReg(cUnit, kOpBlx, rTgt);
2182 // Refresh rSUSPEND
Ian Rogersb5d09b22012-03-06 22:14:17 -08002183#if defined(TARGET_X86)
2184 UNIMPLEMENTED(FATAL);
2185#else
2186
buzbee86a4bce2012-03-06 18:15:00 -08002187 loadWordDisp(cUnit, rSELF,
2188 OFFSETOF_MEMBER(Thread, pUpdateDebuggerFromCode),
2189 rSUSPEND);
Ian Rogersb5d09b22012-03-06 22:14:17 -08002190#endif
buzbee31a4a6f2012-02-28 15:36:15 -08002191 } else {
Ian Rogersb5d09b22012-03-06 22:14:17 -08002192 LIR* branch = NULL;
buzbee31a4a6f2012-02-28 15:36:15 -08002193#if defined(TARGET_ARM)
2194 // In non-debug case, only check periodically
2195 newLIR2(cUnit, kThumbSubRI8, rSUSPEND, 1);
buzbee82488f52012-03-02 08:20:26 -08002196 branch = opCondBranch(cUnit, kCondEq, NULL);
Ian Rogersb5d09b22012-03-06 22:14:17 -08002197#elif defined(TARGET_X86)
2198 newLIR2(cUnit, kX86Cmp32TI, Thread::SuspendCountOffset().Int32Value(), 0);
2199 branch = opCondBranch(cUnit, kCondNe, NULL);
buzbee31a4a6f2012-02-28 15:36:15 -08002200#else
2201 opRegImm(cUnit, kOpSub, rSUSPEND, 1);
buzbee82488f52012-03-02 08:20:26 -08002202 branch = opCmpImmBranch(cUnit, kCondEq, rSUSPEND, 0, NULL);
buzbee31a4a6f2012-02-28 15:36:15 -08002203#endif
buzbee86a4bce2012-03-06 18:15:00 -08002204 LIR* retLab = newLIR0(cUnit, kPseudoTargetLabel);
2205 LIR* target = rawLIR(cUnit, cUnit->currentDalvikOffset,
2206 kPseudoSuspendTarget, (intptr_t)retLab, mir->offset);
2207 branch->target = (LIR*)target;
2208 oatInsertGrowableList(cUnit, &cUnit->suspendLaunchpads, (intptr_t)target);
buzbee31a4a6f2012-02-28 15:36:15 -08002209 }
buzbee31a4a6f2012-02-28 15:36:15 -08002210}
2211
2212} // namespace art