blob: cc81d1da8f09bb70582650e0815cf20203f5748b [file] [log] [blame]
Ben Chengba4fc8b2009-06-01 13:00:29 -07001/*
2 * Copyright (C) 2009 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
Bill Buzbee50a6bf22009-07-08 13:08:04 -070017/*
18 * This file contains codegen and support common to all supported
19 * ARM variants. It is included by:
20 *
21 * Codegen-$(TARGET_ARCH_VARIANT).c
22 *
23 * which combines this common code with specific support found in the
24 * applicable directory below this one.
25 */
26
buzbee919eb062010-07-12 12:59:22 -070027/*
28 * Mark garbage collection card. Skip if the value we're storing is null.
29 */
30static void markCard(CompilationUnit *cUnit, int valReg, int tgtAddrReg)
31{
32 int regCardBase = dvmCompilerAllocTemp(cUnit);
33 int regCardNo = dvmCompilerAllocTemp(cUnit);
buzbee8f8109a2010-08-31 10:16:35 -070034 ArmLIR *branchOver = genCmpImmBranch(cUnit, kArmCondEq, valReg, 0);
Ben Cheng20d7e6c2011-02-18 17:12:42 -080035 loadWordDisp(cUnit, r6SELF, offsetof(Thread, cardTable),
buzbee919eb062010-07-12 12:59:22 -070036 regCardBase);
37 opRegRegImm(cUnit, kOpLsr, regCardNo, tgtAddrReg, GC_CARD_SHIFT);
38 storeBaseIndexed(cUnit, regCardBase, regCardNo, regCardBase, 0,
39 kUnsignedByte);
40 ArmLIR *target = newLIR0(cUnit, kArmPseudoTargetLabel);
41 target->defMask = ENCODE_ALL;
42 branchOver->generic.target = (LIR *)target;
buzbeebaf196a2010-08-04 10:13:15 -070043 dvmCompilerFreeTemp(cUnit, regCardBase);
44 dvmCompilerFreeTemp(cUnit, regCardNo);
buzbee919eb062010-07-12 12:59:22 -070045}
46
Ben Cheng5d90c202009-11-22 23:31:11 -080047static bool genConversionCall(CompilationUnit *cUnit, MIR *mir, void *funct,
48 int srcSize, int tgtSize)
49{
50 /*
51 * Don't optimize the register usage since it calls out to template
52 * functions
53 */
54 RegLocation rlSrc;
55 RegLocation rlDest;
Bill Buzbeec6f10662010-02-09 11:16:15 -080056 dvmCompilerFlushAllRegs(cUnit); /* Send everything to home location */
Ben Cheng5d90c202009-11-22 23:31:11 -080057 if (srcSize == 1) {
Bill Buzbeec6f10662010-02-09 11:16:15 -080058 rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
Ben Cheng5d90c202009-11-22 23:31:11 -080059 loadValueDirectFixed(cUnit, rlSrc, r0);
60 } else {
Bill Buzbeec6f10662010-02-09 11:16:15 -080061 rlSrc = dvmCompilerGetSrcWide(cUnit, mir, 0, 1);
Ben Cheng5d90c202009-11-22 23:31:11 -080062 loadValueDirectWideFixed(cUnit, rlSrc, r0, r1);
63 }
Ben Chengbd1326d2010-04-02 15:04:53 -070064 LOAD_FUNC_ADDR(cUnit, r2, (int)funct);
Ben Cheng5d90c202009-11-22 23:31:11 -080065 opReg(cUnit, kOpBlx, r2);
Elliott Hughes6a555132010-02-25 15:41:42 -080066 dvmCompilerClobberCallRegs(cUnit);
Ben Cheng5d90c202009-11-22 23:31:11 -080067 if (tgtSize == 1) {
68 RegLocation rlResult;
Bill Buzbeec6f10662010-02-09 11:16:15 -080069 rlDest = dvmCompilerGetDest(cUnit, mir, 0);
70 rlResult = dvmCompilerGetReturn(cUnit);
Ben Cheng5d90c202009-11-22 23:31:11 -080071 storeValue(cUnit, rlDest, rlResult);
72 } else {
73 RegLocation rlResult;
Bill Buzbeec6f10662010-02-09 11:16:15 -080074 rlDest = dvmCompilerGetDestWide(cUnit, mir, 0, 1);
75 rlResult = dvmCompilerGetReturnWide(cUnit);
Ben Cheng5d90c202009-11-22 23:31:11 -080076 storeValueWide(cUnit, rlDest, rlResult);
77 }
78 return false;
79}
Ben Chengba4fc8b2009-06-01 13:00:29 -070080
Ben Cheng5d90c202009-11-22 23:31:11 -080081static bool genArithOpFloatPortable(CompilationUnit *cUnit, MIR *mir,
82 RegLocation rlDest, RegLocation rlSrc1,
83 RegLocation rlSrc2)
84{
85 RegLocation rlResult;
86 void* funct;
87
Dan Bornstein9a1f8162010-12-01 17:02:26 -080088 switch (mir->dalvikInsn.opcode) {
Ben Cheng5d90c202009-11-22 23:31:11 -080089 case OP_ADD_FLOAT_2ADDR:
90 case OP_ADD_FLOAT:
91 funct = (void*) __aeabi_fadd;
92 break;
93 case OP_SUB_FLOAT_2ADDR:
94 case OP_SUB_FLOAT:
95 funct = (void*) __aeabi_fsub;
96 break;
97 case OP_DIV_FLOAT_2ADDR:
98 case OP_DIV_FLOAT:
99 funct = (void*) __aeabi_fdiv;
100 break;
101 case OP_MUL_FLOAT_2ADDR:
102 case OP_MUL_FLOAT:
103 funct = (void*) __aeabi_fmul;
104 break;
105 case OP_REM_FLOAT_2ADDR:
106 case OP_REM_FLOAT:
107 funct = (void*) fmodf;
108 break;
109 case OP_NEG_FLOAT: {
110 genNegFloat(cUnit, rlDest, rlSrc1);
111 return false;
112 }
113 default:
114 return true;
115 }
Bill Buzbeec6f10662010-02-09 11:16:15 -0800116 dvmCompilerFlushAllRegs(cUnit); /* Send everything to home location */
Ben Cheng5d90c202009-11-22 23:31:11 -0800117 loadValueDirectFixed(cUnit, rlSrc1, r0);
118 loadValueDirectFixed(cUnit, rlSrc2, r1);
Ben Chengbd1326d2010-04-02 15:04:53 -0700119 LOAD_FUNC_ADDR(cUnit, r2, (int)funct);
Ben Cheng5d90c202009-11-22 23:31:11 -0800120 opReg(cUnit, kOpBlx, r2);
Elliott Hughes6a555132010-02-25 15:41:42 -0800121 dvmCompilerClobberCallRegs(cUnit);
Bill Buzbeec6f10662010-02-09 11:16:15 -0800122 rlResult = dvmCompilerGetReturn(cUnit);
Ben Cheng5d90c202009-11-22 23:31:11 -0800123 storeValue(cUnit, rlDest, rlResult);
124 return false;
125}
126
127static bool genArithOpDoublePortable(CompilationUnit *cUnit, MIR *mir,
128 RegLocation rlDest, RegLocation rlSrc1,
129 RegLocation rlSrc2)
130{
131 RegLocation rlResult;
132 void* funct;
133
Dan Bornstein9a1f8162010-12-01 17:02:26 -0800134 switch (mir->dalvikInsn.opcode) {
Ben Cheng5d90c202009-11-22 23:31:11 -0800135 case OP_ADD_DOUBLE_2ADDR:
136 case OP_ADD_DOUBLE:
137 funct = (void*) __aeabi_dadd;
138 break;
139 case OP_SUB_DOUBLE_2ADDR:
140 case OP_SUB_DOUBLE:
141 funct = (void*) __aeabi_dsub;
142 break;
143 case OP_DIV_DOUBLE_2ADDR:
144 case OP_DIV_DOUBLE:
145 funct = (void*) __aeabi_ddiv;
146 break;
147 case OP_MUL_DOUBLE_2ADDR:
148 case OP_MUL_DOUBLE:
149 funct = (void*) __aeabi_dmul;
150 break;
151 case OP_REM_DOUBLE_2ADDR:
152 case OP_REM_DOUBLE:
153 funct = (void*) fmod;
154 break;
155 case OP_NEG_DOUBLE: {
156 genNegDouble(cUnit, rlDest, rlSrc1);
157 return false;
158 }
159 default:
160 return true;
161 }
Bill Buzbeec6f10662010-02-09 11:16:15 -0800162 dvmCompilerFlushAllRegs(cUnit); /* Send everything to home location */
Ben Cheng20d7e6c2011-02-18 17:12:42 -0800163 LOAD_FUNC_ADDR(cUnit, r14lr, (int)funct);
Ben Cheng5d90c202009-11-22 23:31:11 -0800164 loadValueDirectWideFixed(cUnit, rlSrc1, r0, r1);
165 loadValueDirectWideFixed(cUnit, rlSrc2, r2, r3);
Ben Cheng20d7e6c2011-02-18 17:12:42 -0800166 opReg(cUnit, kOpBlx, r14lr);
Elliott Hughes6a555132010-02-25 15:41:42 -0800167 dvmCompilerClobberCallRegs(cUnit);
Bill Buzbeec6f10662010-02-09 11:16:15 -0800168 rlResult = dvmCompilerGetReturnWide(cUnit);
Ben Cheng5d90c202009-11-22 23:31:11 -0800169 storeValueWide(cUnit, rlDest, rlResult);
Ben Chengd72564c2011-02-08 17:09:25 -0800170#if defined(WITH_SELF_VERIFICATION)
171 cUnit->usesLinkRegister = true;
172#endif
Ben Cheng5d90c202009-11-22 23:31:11 -0800173 return false;
174}
175
176static bool genConversionPortable(CompilationUnit *cUnit, MIR *mir)
177{
Dan Bornstein9a1f8162010-12-01 17:02:26 -0800178 Opcode opcode = mir->dalvikInsn.opcode;
Ben Cheng5d90c202009-11-22 23:31:11 -0800179
Dan Bornstein9a1f8162010-12-01 17:02:26 -0800180 switch (opcode) {
Ben Cheng5d90c202009-11-22 23:31:11 -0800181 case OP_INT_TO_FLOAT:
182 return genConversionCall(cUnit, mir, (void*)__aeabi_i2f, 1, 1);
183 case OP_FLOAT_TO_INT:
184 return genConversionCall(cUnit, mir, (void*)__aeabi_f2iz, 1, 1);
185 case OP_DOUBLE_TO_FLOAT:
186 return genConversionCall(cUnit, mir, (void*)__aeabi_d2f, 2, 1);
187 case OP_FLOAT_TO_DOUBLE:
188 return genConversionCall(cUnit, mir, (void*)__aeabi_f2d, 1, 2);
189 case OP_INT_TO_DOUBLE:
190 return genConversionCall(cUnit, mir, (void*)__aeabi_i2d, 1, 2);
191 case OP_DOUBLE_TO_INT:
192 return genConversionCall(cUnit, mir, (void*)__aeabi_d2iz, 2, 1);
193 case OP_FLOAT_TO_LONG:
194 return genConversionCall(cUnit, mir, (void*)dvmJitf2l, 1, 2);
195 case OP_LONG_TO_FLOAT:
196 return genConversionCall(cUnit, mir, (void*)__aeabi_l2f, 2, 1);
197 case OP_DOUBLE_TO_LONG:
198 return genConversionCall(cUnit, mir, (void*)dvmJitd2l, 2, 2);
199 case OP_LONG_TO_DOUBLE:
200 return genConversionCall(cUnit, mir, (void*)__aeabi_l2d, 2, 2);
201 default:
202 return true;
203 }
204 return false;
205}
Ben Chengba4fc8b2009-06-01 13:00:29 -0700206
Jeff Hao97319a82009-08-12 16:57:15 -0700207#if defined(WITH_SELF_VERIFICATION)
Dan Bornstein9a1f8162010-12-01 17:02:26 -0800208static void selfVerificationBranchInsert(LIR *currentLIR, ArmOpcode opcode,
jeffhao9e45c0b2010-02-03 10:24:05 -0800209 int dest, int src1)
Jeff Hao97319a82009-08-12 16:57:15 -0700210{
Carl Shapirofc75f3e2010-12-07 11:43:38 -0800211 ArmLIR *insn = (ArmLIR *) dvmCompilerNew(sizeof(ArmLIR), true);
Dan Bornstein9a1f8162010-12-01 17:02:26 -0800212 insn->opcode = opcode;
jeffhao9e45c0b2010-02-03 10:24:05 -0800213 insn->operands[0] = dest;
214 insn->operands[1] = src1;
215 setupResourceMasks(insn);
216 dvmCompilerInsertLIRBefore(currentLIR, (LIR *) insn);
Jeff Hao97319a82009-08-12 16:57:15 -0700217}
218
Ben Chengd72564c2011-02-08 17:09:25 -0800219/*
220 * Example where r14 (LR) is preserved around a heap access under
221 * self-verification mode in Thumb2:
222 *
Ben Cheng20d7e6c2011-02-18 17:12:42 -0800223 * D/dalvikvm( 1538): 0x59414c5e (0026): ldr r14, [r15pc, #220] <-hoisted
Ben Chengd72564c2011-02-08 17:09:25 -0800224 * D/dalvikvm( 1538): 0x59414c62 (002a): mla r4, r0, r8, r4
225 * D/dalvikvm( 1538): 0x59414c66 (002e): adds r3, r4, r3
226 * D/dalvikvm( 1538): 0x59414c6a (0032): push <r5, r14> ---+
227 * D/dalvikvm( 1538): 0x59414c6c (0034): blx_1 0x5940f494 |
228 * D/dalvikvm( 1538): 0x59414c6e (0036): blx_2 see above <-MEM_OP_DECODE
229 * D/dalvikvm( 1538): 0x59414c70 (0038): ldr r10, [r9, #0] |
230 * D/dalvikvm( 1538): 0x59414c74 (003c): pop <r5, r14> ---+
231 * D/dalvikvm( 1538): 0x59414c78 (0040): mov r11, r10
232 * D/dalvikvm( 1538): 0x59414c7a (0042): asr r12, r11, #31
233 * D/dalvikvm( 1538): 0x59414c7e (0046): movs r0, r2
234 * D/dalvikvm( 1538): 0x59414c80 (0048): movs r1, r3
235 * D/dalvikvm( 1538): 0x59414c82 (004a): str r2, [r5, #16]
236 * D/dalvikvm( 1538): 0x59414c84 (004c): mov r2, r11
237 * D/dalvikvm( 1538): 0x59414c86 (004e): str r3, [r5, #20]
238 * D/dalvikvm( 1538): 0x59414c88 (0050): mov r3, r12
239 * D/dalvikvm( 1538): 0x59414c8a (0052): str r11, [r5, #24]
240 * D/dalvikvm( 1538): 0x59414c8e (0056): str r12, [r5, #28]
241 * D/dalvikvm( 1538): 0x59414c92 (005a): blx r14 <-use of LR
242 *
243 */
jeffhao9e45c0b2010-02-03 10:24:05 -0800244static void selfVerificationBranchInsertPass(CompilationUnit *cUnit)
Jeff Hao97319a82009-08-12 16:57:15 -0700245{
jeffhao9e45c0b2010-02-03 10:24:05 -0800246 ArmLIR *thisLIR;
Dan Bornstein9a1f8162010-12-01 17:02:26 -0800247 TemplateOpcode opcode = TEMPLATE_MEM_OP_DECODE;
Jeff Hao97319a82009-08-12 16:57:15 -0700248
jeffhao9e45c0b2010-02-03 10:24:05 -0800249 for (thisLIR = (ArmLIR *) cUnit->firstLIRInsn;
250 thisLIR != (ArmLIR *) cUnit->lastLIRInsn;
251 thisLIR = NEXT_LIR(thisLIR)) {
Ben Chengd72564c2011-02-08 17:09:25 -0800252 if (!thisLIR->flags.isNop && thisLIR->flags.insertWrapper) {
253 /*
254 * Push r5(FP) and r14(LR) onto stack. We need to make sure that
255 * SP is 8-byte aligned, and we use r5 as a temp to restore LR
256 * for Thumb-only target since LR cannot be directly accessed in
257 * Thumb mode. Another reason to choose r5 here is it is the Dalvik
258 * frame pointer and cannot be the target of the emulated heap
259 * load.
260 */
261 if (cUnit->usesLinkRegister) {
262 genSelfVerificationPreBranch(cUnit, thisLIR);
263 }
264
jeffhao9e45c0b2010-02-03 10:24:05 -0800265 /* Branch to mem op decode template */
266 selfVerificationBranchInsert((LIR *) thisLIR, kThumbBlx1,
Dan Bornstein9a1f8162010-12-01 17:02:26 -0800267 (int) gDvmJit.codeCache + templateEntryOffsets[opcode],
268 (int) gDvmJit.codeCache + templateEntryOffsets[opcode]);
jeffhao9e45c0b2010-02-03 10:24:05 -0800269 selfVerificationBranchInsert((LIR *) thisLIR, kThumbBlx2,
Dan Bornstein9a1f8162010-12-01 17:02:26 -0800270 (int) gDvmJit.codeCache + templateEntryOffsets[opcode],
271 (int) gDvmJit.codeCache + templateEntryOffsets[opcode]);
Ben Chengd72564c2011-02-08 17:09:25 -0800272
273 /* Restore LR */
274 if (cUnit->usesLinkRegister) {
275 genSelfVerificationPostBranch(cUnit, thisLIR);
276 }
Jeff Hao97319a82009-08-12 16:57:15 -0700277 }
278 }
Jeff Hao97319a82009-08-12 16:57:15 -0700279}
Jeff Hao97319a82009-08-12 16:57:15 -0700280#endif
281
Bill Buzbeebe6534f2010-03-12 16:01:35 -0800282/* Generate conditional branch instructions */
283static ArmLIR *genConditionalBranch(CompilationUnit *cUnit,
284 ArmConditionCode cond,
285 ArmLIR *target)
286{
287 ArmLIR *branch = opCondBranch(cUnit, cond);
288 branch->generic.target = (LIR *) target;
289 return branch;
290}
291
Ben Chengba4fc8b2009-06-01 13:00:29 -0700292/* Generate a unconditional branch to go to the interpreter */
Bill Buzbee89efc3d2009-07-28 11:22:22 -0700293static inline ArmLIR *genTrap(CompilationUnit *cUnit, int dOffset,
294 ArmLIR *pcrLabel)
Ben Chengba4fc8b2009-06-01 13:00:29 -0700295{
Bill Buzbee1465db52009-09-23 17:17:35 -0700296 ArmLIR *branch = opNone(cUnit, kOpUncondBr);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700297 return genCheckCommon(cUnit, dOffset, branch, pcrLabel);
298}
299
300/* Load a wide field from an object instance */
301static void genIGetWide(CompilationUnit *cUnit, MIR *mir, int fieldOffset)
302{
Bill Buzbeec6f10662010-02-09 11:16:15 -0800303 RegLocation rlObj = dvmCompilerGetSrc(cUnit, mir, 0);
304 RegLocation rlDest = dvmCompilerGetDestWide(cUnit, mir, 0, 1);
Bill Buzbee1465db52009-09-23 17:17:35 -0700305 RegLocation rlResult;
306 rlObj = loadValue(cUnit, rlObj, kCoreReg);
Bill Buzbeec6f10662010-02-09 11:16:15 -0800307 int regPtr = dvmCompilerAllocTemp(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700308
Bill Buzbee1465db52009-09-23 17:17:35 -0700309 assert(rlDest.wide);
Ben Chenge9695e52009-06-16 16:11:47 -0700310
Bill Buzbee1465db52009-09-23 17:17:35 -0700311 genNullCheck(cUnit, rlObj.sRegLow, rlObj.lowReg, mir->offset,
312 NULL);/* null object? */
313 opRegRegImm(cUnit, kOpAdd, regPtr, rlObj.lowReg, fieldOffset);
Bill Buzbeec6f10662010-02-09 11:16:15 -0800314 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kAnyReg, true);
Ben Cheng11d8f142010-03-24 15:24:19 -0700315
316 HEAP_ACCESS_SHADOW(true);
Bill Buzbee1465db52009-09-23 17:17:35 -0700317 loadPair(cUnit, regPtr, rlResult.lowReg, rlResult.highReg);
Ben Cheng11d8f142010-03-24 15:24:19 -0700318 HEAP_ACCESS_SHADOW(false);
319
Bill Buzbeec6f10662010-02-09 11:16:15 -0800320 dvmCompilerFreeTemp(cUnit, regPtr);
Bill Buzbee1465db52009-09-23 17:17:35 -0700321 storeValueWide(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700322}
323
324/* Store a wide field to an object instance */
325static void genIPutWide(CompilationUnit *cUnit, MIR *mir, int fieldOffset)
326{
Bill Buzbeec6f10662010-02-09 11:16:15 -0800327 RegLocation rlSrc = dvmCompilerGetSrcWide(cUnit, mir, 0, 1);
328 RegLocation rlObj = dvmCompilerGetSrc(cUnit, mir, 2);
Bill Buzbee1465db52009-09-23 17:17:35 -0700329 rlObj = loadValue(cUnit, rlObj, kCoreReg);
330 int regPtr;
331 rlSrc = loadValueWide(cUnit, rlSrc, kAnyReg);
332 genNullCheck(cUnit, rlObj.sRegLow, rlObj.lowReg, mir->offset,
333 NULL);/* null object? */
Bill Buzbeec6f10662010-02-09 11:16:15 -0800334 regPtr = dvmCompilerAllocTemp(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -0700335 opRegRegImm(cUnit, kOpAdd, regPtr, rlObj.lowReg, fieldOffset);
Ben Cheng11d8f142010-03-24 15:24:19 -0700336
337 HEAP_ACCESS_SHADOW(true);
Bill Buzbee1465db52009-09-23 17:17:35 -0700338 storePair(cUnit, regPtr, rlSrc.lowReg, rlSrc.highReg);
Ben Cheng11d8f142010-03-24 15:24:19 -0700339 HEAP_ACCESS_SHADOW(false);
340
Bill Buzbeec6f10662010-02-09 11:16:15 -0800341 dvmCompilerFreeTemp(cUnit, regPtr);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700342}
343
344/*
345 * Load a field from an object instance
346 *
Ben Chengba4fc8b2009-06-01 13:00:29 -0700347 */
Bill Buzbee270c1d62009-08-13 16:58:07 -0700348static void genIGet(CompilationUnit *cUnit, MIR *mir, OpSize size,
buzbeeecf8f6e2010-07-20 14:53:42 -0700349 int fieldOffset, bool isVolatile)
Ben Chengba4fc8b2009-06-01 13:00:29 -0700350{
Bill Buzbee1465db52009-09-23 17:17:35 -0700351 RegLocation rlResult;
Bill Buzbee749e8162010-07-07 06:55:56 -0700352 RegisterClass regClass = dvmCompilerRegClassBySize(size);
Bill Buzbeec6f10662010-02-09 11:16:15 -0800353 RegLocation rlObj = dvmCompilerGetSrc(cUnit, mir, 0);
354 RegLocation rlDest = dvmCompilerGetDest(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -0700355 rlObj = loadValue(cUnit, rlObj, kCoreReg);
Bill Buzbee749e8162010-07-07 06:55:56 -0700356 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, regClass, true);
Bill Buzbee1465db52009-09-23 17:17:35 -0700357 genNullCheck(cUnit, rlObj.sRegLow, rlObj.lowReg, mir->offset,
358 NULL);/* null object? */
Ben Cheng11d8f142010-03-24 15:24:19 -0700359
360 HEAP_ACCESS_SHADOW(true);
Ben Cheng5d90c202009-11-22 23:31:11 -0800361 loadBaseDisp(cUnit, mir, rlObj.lowReg, fieldOffset, rlResult.lowReg,
362 size, rlObj.sRegLow);
Ben Cheng11d8f142010-03-24 15:24:19 -0700363 HEAP_ACCESS_SHADOW(false);
buzbeeecf8f6e2010-07-20 14:53:42 -0700364 if (isVolatile) {
buzbee2ce33c92010-11-01 15:53:27 -0700365 dvmCompilerGenMemBarrier(cUnit, kSY);
buzbeeecf8f6e2010-07-20 14:53:42 -0700366 }
Ben Cheng11d8f142010-03-24 15:24:19 -0700367
Bill Buzbee1465db52009-09-23 17:17:35 -0700368 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700369}
370
371/*
372 * Store a field to an object instance
373 *
Ben Chengba4fc8b2009-06-01 13:00:29 -0700374 */
Bill Buzbee270c1d62009-08-13 16:58:07 -0700375static void genIPut(CompilationUnit *cUnit, MIR *mir, OpSize size,
buzbeeecf8f6e2010-07-20 14:53:42 -0700376 int fieldOffset, bool isObject, bool isVolatile)
Ben Chengba4fc8b2009-06-01 13:00:29 -0700377{
Bill Buzbee749e8162010-07-07 06:55:56 -0700378 RegisterClass regClass = dvmCompilerRegClassBySize(size);
Bill Buzbeec6f10662010-02-09 11:16:15 -0800379 RegLocation rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
380 RegLocation rlObj = dvmCompilerGetSrc(cUnit, mir, 1);
Bill Buzbee1465db52009-09-23 17:17:35 -0700381 rlObj = loadValue(cUnit, rlObj, kCoreReg);
Bill Buzbee749e8162010-07-07 06:55:56 -0700382 rlSrc = loadValue(cUnit, rlSrc, regClass);
Bill Buzbee1465db52009-09-23 17:17:35 -0700383 genNullCheck(cUnit, rlObj.sRegLow, rlObj.lowReg, mir->offset,
384 NULL);/* null object? */
Ben Cheng11d8f142010-03-24 15:24:19 -0700385
buzbeeecf8f6e2010-07-20 14:53:42 -0700386 if (isVolatile) {
buzbee2ce33c92010-11-01 15:53:27 -0700387 dvmCompilerGenMemBarrier(cUnit, kSY);
buzbeeecf8f6e2010-07-20 14:53:42 -0700388 }
Ben Cheng11d8f142010-03-24 15:24:19 -0700389 HEAP_ACCESS_SHADOW(true);
Bill Buzbee1465db52009-09-23 17:17:35 -0700390 storeBaseDisp(cUnit, rlObj.lowReg, fieldOffset, rlSrc.lowReg, size);
Ben Cheng11d8f142010-03-24 15:24:19 -0700391 HEAP_ACCESS_SHADOW(false);
buzbee919eb062010-07-12 12:59:22 -0700392 if (isObject) {
393 /* NOTE: marking card based on object head */
394 markCard(cUnit, rlSrc.lowReg, rlObj.lowReg);
395 }
Ben Chengba4fc8b2009-06-01 13:00:29 -0700396}
397
398
Ben Chengba4fc8b2009-06-01 13:00:29 -0700399/*
400 * Generate array load
Ben Chengba4fc8b2009-06-01 13:00:29 -0700401 */
Bill Buzbee270c1d62009-08-13 16:58:07 -0700402static void genArrayGet(CompilationUnit *cUnit, MIR *mir, OpSize size,
Bill Buzbee1465db52009-09-23 17:17:35 -0700403 RegLocation rlArray, RegLocation rlIndex,
404 RegLocation rlDest, int scale)
Ben Chengba4fc8b2009-06-01 13:00:29 -0700405{
Bill Buzbee749e8162010-07-07 06:55:56 -0700406 RegisterClass regClass = dvmCompilerRegClassBySize(size);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700407 int lenOffset = offsetof(ArrayObject, length);
408 int dataOffset = offsetof(ArrayObject, contents);
Bill Buzbee1465db52009-09-23 17:17:35 -0700409 RegLocation rlResult;
410 rlArray = loadValue(cUnit, rlArray, kCoreReg);
411 rlIndex = loadValue(cUnit, rlIndex, kCoreReg);
412 int regPtr;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700413
414 /* null object? */
Ben Cheng4238ec22009-08-24 16:32:22 -0700415 ArmLIR * pcrLabel = NULL;
416
417 if (!(mir->OptimizationFlags & MIR_IGNORE_NULL_CHECK)) {
Bill Buzbee1465db52009-09-23 17:17:35 -0700418 pcrLabel = genNullCheck(cUnit, rlArray.sRegLow,
419 rlArray.lowReg, mir->offset, NULL);
Ben Cheng4238ec22009-08-24 16:32:22 -0700420 }
421
Bill Buzbeec6f10662010-02-09 11:16:15 -0800422 regPtr = dvmCompilerAllocTemp(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -0700423
Ben Cheng4238ec22009-08-24 16:32:22 -0700424 if (!(mir->OptimizationFlags & MIR_IGNORE_RANGE_CHECK)) {
Bill Buzbeec6f10662010-02-09 11:16:15 -0800425 int regLen = dvmCompilerAllocTemp(cUnit);
Ben Cheng4238ec22009-08-24 16:32:22 -0700426 /* Get len */
Bill Buzbee1465db52009-09-23 17:17:35 -0700427 loadWordDisp(cUnit, rlArray.lowReg, lenOffset, regLen);
428 /* regPtr -> array data */
429 opRegRegImm(cUnit, kOpAdd, regPtr, rlArray.lowReg, dataOffset);
430 genBoundsCheck(cUnit, rlIndex.lowReg, regLen, mir->offset,
431 pcrLabel);
Bill Buzbeec6f10662010-02-09 11:16:15 -0800432 dvmCompilerFreeTemp(cUnit, regLen);
Ben Cheng4238ec22009-08-24 16:32:22 -0700433 } else {
Bill Buzbee1465db52009-09-23 17:17:35 -0700434 /* regPtr -> array data */
435 opRegRegImm(cUnit, kOpAdd, regPtr, rlArray.lowReg, dataOffset);
Ben Cheng4238ec22009-08-24 16:32:22 -0700436 }
Bill Buzbee1465db52009-09-23 17:17:35 -0700437 if ((size == kLong) || (size == kDouble)) {
438 if (scale) {
Bill Buzbeec6f10662010-02-09 11:16:15 -0800439 int rNewIndex = dvmCompilerAllocTemp(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -0700440 opRegRegImm(cUnit, kOpLsl, rNewIndex, rlIndex.lowReg, scale);
441 opRegReg(cUnit, kOpAdd, regPtr, rNewIndex);
Bill Buzbeec6f10662010-02-09 11:16:15 -0800442 dvmCompilerFreeTemp(cUnit, rNewIndex);
Bill Buzbee1465db52009-09-23 17:17:35 -0700443 } else {
444 opRegReg(cUnit, kOpAdd, regPtr, rlIndex.lowReg);
445 }
Bill Buzbee749e8162010-07-07 06:55:56 -0700446 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, regClass, true);
Ben Cheng11d8f142010-03-24 15:24:19 -0700447
448 HEAP_ACCESS_SHADOW(true);
Bill Buzbee1465db52009-09-23 17:17:35 -0700449 loadPair(cUnit, regPtr, rlResult.lowReg, rlResult.highReg);
Ben Cheng11d8f142010-03-24 15:24:19 -0700450 HEAP_ACCESS_SHADOW(false);
451
Bill Buzbeec6f10662010-02-09 11:16:15 -0800452 dvmCompilerFreeTemp(cUnit, regPtr);
Bill Buzbee1465db52009-09-23 17:17:35 -0700453 storeValueWide(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700454 } else {
Bill Buzbee749e8162010-07-07 06:55:56 -0700455 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, regClass, true);
Ben Cheng11d8f142010-03-24 15:24:19 -0700456
457 HEAP_ACCESS_SHADOW(true);
Bill Buzbee1465db52009-09-23 17:17:35 -0700458 loadBaseIndexed(cUnit, regPtr, rlIndex.lowReg, rlResult.lowReg,
459 scale, size);
Ben Cheng11d8f142010-03-24 15:24:19 -0700460 HEAP_ACCESS_SHADOW(false);
461
Bill Buzbeec6f10662010-02-09 11:16:15 -0800462 dvmCompilerFreeTemp(cUnit, regPtr);
Bill Buzbee1465db52009-09-23 17:17:35 -0700463 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700464 }
465}
466
Ben Chengba4fc8b2009-06-01 13:00:29 -0700467/*
468 * Generate array store
469 *
Ben Chengba4fc8b2009-06-01 13:00:29 -0700470 */
Bill Buzbee270c1d62009-08-13 16:58:07 -0700471static void genArrayPut(CompilationUnit *cUnit, MIR *mir, OpSize size,
Bill Buzbee1465db52009-09-23 17:17:35 -0700472 RegLocation rlArray, RegLocation rlIndex,
473 RegLocation rlSrc, int scale)
Ben Chengba4fc8b2009-06-01 13:00:29 -0700474{
Bill Buzbee749e8162010-07-07 06:55:56 -0700475 RegisterClass regClass = dvmCompilerRegClassBySize(size);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700476 int lenOffset = offsetof(ArrayObject, length);
477 int dataOffset = offsetof(ArrayObject, contents);
478
Bill Buzbee1465db52009-09-23 17:17:35 -0700479 int regPtr;
480 rlArray = loadValue(cUnit, rlArray, kCoreReg);
481 rlIndex = loadValue(cUnit, rlIndex, kCoreReg);
Ben Chenge9695e52009-06-16 16:11:47 -0700482
Bill Buzbeec6f10662010-02-09 11:16:15 -0800483 if (dvmCompilerIsTemp(cUnit, rlArray.lowReg)) {
484 dvmCompilerClobber(cUnit, rlArray.lowReg);
Bill Buzbee1465db52009-09-23 17:17:35 -0700485 regPtr = rlArray.lowReg;
486 } else {
Bill Buzbeec6f10662010-02-09 11:16:15 -0800487 regPtr = dvmCompilerAllocTemp(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -0700488 genRegCopy(cUnit, regPtr, rlArray.lowReg);
489 }
Ben Chenge9695e52009-06-16 16:11:47 -0700490
Ben Cheng1efc9c52009-06-08 18:25:27 -0700491 /* null object? */
Ben Cheng4238ec22009-08-24 16:32:22 -0700492 ArmLIR * pcrLabel = NULL;
493
494 if (!(mir->OptimizationFlags & MIR_IGNORE_NULL_CHECK)) {
Bill Buzbee1465db52009-09-23 17:17:35 -0700495 pcrLabel = genNullCheck(cUnit, rlArray.sRegLow, rlArray.lowReg,
496 mir->offset, NULL);
Ben Cheng4238ec22009-08-24 16:32:22 -0700497 }
498
499 if (!(mir->OptimizationFlags & MIR_IGNORE_RANGE_CHECK)) {
Bill Buzbeec6f10662010-02-09 11:16:15 -0800500 int regLen = dvmCompilerAllocTemp(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -0700501 //NOTE: max live temps(4) here.
Ben Cheng4238ec22009-08-24 16:32:22 -0700502 /* Get len */
Bill Buzbee1465db52009-09-23 17:17:35 -0700503 loadWordDisp(cUnit, rlArray.lowReg, lenOffset, regLen);
504 /* regPtr -> array data */
505 opRegImm(cUnit, kOpAdd, regPtr, dataOffset);
506 genBoundsCheck(cUnit, rlIndex.lowReg, regLen, mir->offset,
507 pcrLabel);
Bill Buzbeec6f10662010-02-09 11:16:15 -0800508 dvmCompilerFreeTemp(cUnit, regLen);
Ben Cheng4238ec22009-08-24 16:32:22 -0700509 } else {
Bill Buzbee1465db52009-09-23 17:17:35 -0700510 /* regPtr -> array data */
511 opRegImm(cUnit, kOpAdd, regPtr, dataOffset);
Ben Cheng4238ec22009-08-24 16:32:22 -0700512 }
Bill Buzbee1465db52009-09-23 17:17:35 -0700513 /* at this point, regPtr points to array, 2 live temps */
Bill Buzbee1465db52009-09-23 17:17:35 -0700514 if ((size == kLong) || (size == kDouble)) {
515 //TODO: need specific wide routine that can handle fp regs
516 if (scale) {
Bill Buzbeec6f10662010-02-09 11:16:15 -0800517 int rNewIndex = dvmCompilerAllocTemp(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -0700518 opRegRegImm(cUnit, kOpLsl, rNewIndex, rlIndex.lowReg, scale);
519 opRegReg(cUnit, kOpAdd, regPtr, rNewIndex);
Bill Buzbeec6f10662010-02-09 11:16:15 -0800520 dvmCompilerFreeTemp(cUnit, rNewIndex);
Bill Buzbee1465db52009-09-23 17:17:35 -0700521 } else {
522 opRegReg(cUnit, kOpAdd, regPtr, rlIndex.lowReg);
523 }
Bill Buzbee749e8162010-07-07 06:55:56 -0700524 rlSrc = loadValueWide(cUnit, rlSrc, regClass);
Ben Cheng11d8f142010-03-24 15:24:19 -0700525
526 HEAP_ACCESS_SHADOW(true);
Bill Buzbee1465db52009-09-23 17:17:35 -0700527 storePair(cUnit, regPtr, rlSrc.lowReg, rlSrc.highReg);
Ben Cheng11d8f142010-03-24 15:24:19 -0700528 HEAP_ACCESS_SHADOW(false);
529
Bill Buzbeec6f10662010-02-09 11:16:15 -0800530 dvmCompilerFreeTemp(cUnit, regPtr);
Bill Buzbee270c1d62009-08-13 16:58:07 -0700531 } else {
Bill Buzbee749e8162010-07-07 06:55:56 -0700532 rlSrc = loadValue(cUnit, rlSrc, regClass);
Ben Cheng11d8f142010-03-24 15:24:19 -0700533
534 HEAP_ACCESS_SHADOW(true);
Bill Buzbee1465db52009-09-23 17:17:35 -0700535 storeBaseIndexed(cUnit, regPtr, rlIndex.lowReg, rlSrc.lowReg,
536 scale, size);
Ben Cheng11d8f142010-03-24 15:24:19 -0700537 HEAP_ACCESS_SHADOW(false);
jeffhao9e45c0b2010-02-03 10:24:05 -0800538 }
Ben Chengba4fc8b2009-06-01 13:00:29 -0700539}
540
Bill Buzbeebe6534f2010-03-12 16:01:35 -0800541/*
542 * Generate array object store
543 * Must use explicit register allocation here because of
544 * call-out to dvmCanPutArrayElement
545 */
546static void genArrayObjectPut(CompilationUnit *cUnit, MIR *mir,
547 RegLocation rlArray, RegLocation rlIndex,
548 RegLocation rlSrc, int scale)
549{
550 int lenOffset = offsetof(ArrayObject, length);
551 int dataOffset = offsetof(ArrayObject, contents);
552
553 dvmCompilerFlushAllRegs(cUnit);
554
555 int regLen = r0;
556 int regPtr = r4PC; /* Preserved across call */
557 int regArray = r1;
558 int regIndex = r7; /* Preserved across call */
559
560 loadValueDirectFixed(cUnit, rlArray, regArray);
561 loadValueDirectFixed(cUnit, rlIndex, regIndex);
562
563 /* null object? */
564 ArmLIR * pcrLabel = NULL;
565
566 if (!(mir->OptimizationFlags & MIR_IGNORE_NULL_CHECK)) {
567 pcrLabel = genNullCheck(cUnit, rlArray.sRegLow, regArray,
568 mir->offset, NULL);
569 }
570
571 if (!(mir->OptimizationFlags & MIR_IGNORE_RANGE_CHECK)) {
572 /* Get len */
573 loadWordDisp(cUnit, regArray, lenOffset, regLen);
574 /* regPtr -> array data */
575 opRegRegImm(cUnit, kOpAdd, regPtr, regArray, dataOffset);
576 genBoundsCheck(cUnit, regIndex, regLen, mir->offset,
577 pcrLabel);
578 } else {
579 /* regPtr -> array data */
580 opRegRegImm(cUnit, kOpAdd, regPtr, regArray, dataOffset);
581 }
582
583 /* Get object to store */
584 loadValueDirectFixed(cUnit, rlSrc, r0);
Ben Chengbd1326d2010-04-02 15:04:53 -0700585 LOAD_FUNC_ADDR(cUnit, r2, (int)dvmCanPutArrayElement);
Bill Buzbeebe6534f2010-03-12 16:01:35 -0800586
587 /* Are we storing null? If so, avoid check */
buzbee8f8109a2010-08-31 10:16:35 -0700588 ArmLIR *branchOver = genCmpImmBranch(cUnit, kArmCondEq, r0, 0);
Bill Buzbeebe6534f2010-03-12 16:01:35 -0800589
590 /* Make sure the types are compatible */
591 loadWordDisp(cUnit, regArray, offsetof(Object, clazz), r1);
592 loadWordDisp(cUnit, r0, offsetof(Object, clazz), r0);
593 opReg(cUnit, kOpBlx, r2);
594 dvmCompilerClobberCallRegs(cUnit);
Bill Buzbee900a3af2010-03-16 12:41:43 -0700595
596 /*
597 * Using fixed registers here, and counting on r4 and r7 being
598 * preserved across the above call. Tell the register allocation
599 * utilities about the regs we are using directly
600 */
601 dvmCompilerLockTemp(cUnit, regPtr); // r4PC
602 dvmCompilerLockTemp(cUnit, regIndex); // r7
603 dvmCompilerLockTemp(cUnit, r0);
buzbee919eb062010-07-12 12:59:22 -0700604 dvmCompilerLockTemp(cUnit, r1);
Bill Buzbee900a3af2010-03-16 12:41:43 -0700605
Bill Buzbeebe6534f2010-03-12 16:01:35 -0800606 /* Bad? - roll back and re-execute if so */
607 genRegImmCheck(cUnit, kArmCondEq, r0, 0, mir->offset, pcrLabel);
608
buzbee919eb062010-07-12 12:59:22 -0700609 /* Resume here - must reload element & array, regPtr & index preserved */
Bill Buzbeebe6534f2010-03-12 16:01:35 -0800610 loadValueDirectFixed(cUnit, rlSrc, r0);
buzbee919eb062010-07-12 12:59:22 -0700611 loadValueDirectFixed(cUnit, rlArray, r1);
Bill Buzbeebe6534f2010-03-12 16:01:35 -0800612
613 ArmLIR *target = newLIR0(cUnit, kArmPseudoTargetLabel);
614 target->defMask = ENCODE_ALL;
615 branchOver->generic.target = (LIR *) target;
616
Ben Cheng11d8f142010-03-24 15:24:19 -0700617 HEAP_ACCESS_SHADOW(true);
Bill Buzbeebe6534f2010-03-12 16:01:35 -0800618 storeBaseIndexed(cUnit, regPtr, regIndex, r0,
619 scale, kWord);
Ben Cheng11d8f142010-03-24 15:24:19 -0700620 HEAP_ACCESS_SHADOW(false);
buzbee919eb062010-07-12 12:59:22 -0700621
buzbeebaf196a2010-08-04 10:13:15 -0700622 dvmCompilerFreeTemp(cUnit, regPtr);
623 dvmCompilerFreeTemp(cUnit, regIndex);
624
buzbee919eb062010-07-12 12:59:22 -0700625 /* NOTE: marking card here based on object head */
626 markCard(cUnit, r0, r1);
Bill Buzbeebe6534f2010-03-12 16:01:35 -0800627}
628
Ben Cheng5d90c202009-11-22 23:31:11 -0800629static bool genShiftOpLong(CompilationUnit *cUnit, MIR *mir,
630 RegLocation rlDest, RegLocation rlSrc1,
631 RegLocation rlShift)
Ben Chengba4fc8b2009-06-01 13:00:29 -0700632{
Ben Chenge9695e52009-06-16 16:11:47 -0700633 /*
634 * Don't mess with the regsiters here as there is a particular calling
635 * convention to the out-of-line handler.
636 */
Bill Buzbee1465db52009-09-23 17:17:35 -0700637 RegLocation rlResult;
638
639 loadValueDirectWideFixed(cUnit, rlSrc1, r0, r1);
640 loadValueDirect(cUnit, rlShift, r2);
Dan Bornstein9a1f8162010-12-01 17:02:26 -0800641 switch( mir->dalvikInsn.opcode) {
Ben Chenge9695e52009-06-16 16:11:47 -0700642 case OP_SHL_LONG:
643 case OP_SHL_LONG_2ADDR:
644 genDispatchToHandler(cUnit, TEMPLATE_SHL_LONG);
645 break;
646 case OP_SHR_LONG:
647 case OP_SHR_LONG_2ADDR:
648 genDispatchToHandler(cUnit, TEMPLATE_SHR_LONG);
649 break;
650 case OP_USHR_LONG:
651 case OP_USHR_LONG_2ADDR:
652 genDispatchToHandler(cUnit, TEMPLATE_USHR_LONG);
653 break;
654 default:
655 return true;
656 }
Bill Buzbeec6f10662010-02-09 11:16:15 -0800657 rlResult = dvmCompilerGetReturnWide(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -0700658 storeValueWide(cUnit, rlDest, rlResult);
Ben Chenge9695e52009-06-16 16:11:47 -0700659 return false;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700660}
Ben Chenge9695e52009-06-16 16:11:47 -0700661
Ben Cheng5d90c202009-11-22 23:31:11 -0800662static bool genArithOpLong(CompilationUnit *cUnit, MIR *mir,
663 RegLocation rlDest, RegLocation rlSrc1,
664 RegLocation rlSrc2)
Ben Chengba4fc8b2009-06-01 13:00:29 -0700665{
Bill Buzbee1465db52009-09-23 17:17:35 -0700666 RegLocation rlResult;
667 OpKind firstOp = kOpBkpt;
668 OpKind secondOp = kOpBkpt;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700669 bool callOut = false;
670 void *callTgt;
671 int retReg = r0;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700672
Dan Bornstein9a1f8162010-12-01 17:02:26 -0800673 switch (mir->dalvikInsn.opcode) {
Ben Chengba4fc8b2009-06-01 13:00:29 -0700674 case OP_NOT_LONG:
Bill Buzbee1465db52009-09-23 17:17:35 -0700675 rlSrc2 = loadValueWide(cUnit, rlSrc2, kCoreReg);
Bill Buzbeec6f10662010-02-09 11:16:15 -0800676 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Bill Buzbee1465db52009-09-23 17:17:35 -0700677 opRegReg(cUnit, kOpMvn, rlResult.lowReg, rlSrc2.lowReg);
678 opRegReg(cUnit, kOpMvn, rlResult.highReg, rlSrc2.highReg);
679 storeValueWide(cUnit, rlDest, rlResult);
680 return false;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700681 break;
682 case OP_ADD_LONG:
683 case OP_ADD_LONG_2ADDR:
Bill Buzbee1465db52009-09-23 17:17:35 -0700684 firstOp = kOpAdd;
685 secondOp = kOpAdc;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700686 break;
687 case OP_SUB_LONG:
688 case OP_SUB_LONG_2ADDR:
Bill Buzbee1465db52009-09-23 17:17:35 -0700689 firstOp = kOpSub;
690 secondOp = kOpSbc;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700691 break;
692 case OP_MUL_LONG:
693 case OP_MUL_LONG_2ADDR:
Bill Buzbee1465db52009-09-23 17:17:35 -0700694 genMulLong(cUnit, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700695 return false;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700696 case OP_DIV_LONG:
697 case OP_DIV_LONG_2ADDR:
698 callOut = true;
699 retReg = r0;
700 callTgt = (void*)__aeabi_ldivmod;
701 break;
702 /* NOTE - result is in r2/r3 instead of r0/r1 */
703 case OP_REM_LONG:
704 case OP_REM_LONG_2ADDR:
705 callOut = true;
706 callTgt = (void*)__aeabi_ldivmod;
707 retReg = r2;
708 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700709 case OP_AND_LONG_2ADDR:
Bill Buzbee1465db52009-09-23 17:17:35 -0700710 case OP_AND_LONG:
711 firstOp = kOpAnd;
712 secondOp = kOpAnd;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700713 break;
714 case OP_OR_LONG:
715 case OP_OR_LONG_2ADDR:
Bill Buzbee1465db52009-09-23 17:17:35 -0700716 firstOp = kOpOr;
717 secondOp = kOpOr;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700718 break;
719 case OP_XOR_LONG:
720 case OP_XOR_LONG_2ADDR:
Bill Buzbee1465db52009-09-23 17:17:35 -0700721 firstOp = kOpXor;
722 secondOp = kOpXor;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700723 break;
Ben Chenge9695e52009-06-16 16:11:47 -0700724 case OP_NEG_LONG: {
Bill Buzbee51ecf602010-01-14 14:27:52 -0800725 //TUNING: can improve this using Thumb2 code
Bill Buzbeec6f10662010-02-09 11:16:15 -0800726 int tReg = dvmCompilerAllocTemp(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -0700727 rlSrc2 = loadValueWide(cUnit, rlSrc2, kCoreReg);
Bill Buzbeec6f10662010-02-09 11:16:15 -0800728 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Ben Chengbd1326d2010-04-02 15:04:53 -0700729 loadConstantNoClobber(cUnit, tReg, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -0700730 opRegRegReg(cUnit, kOpSub, rlResult.lowReg,
Bill Buzbee51ecf602010-01-14 14:27:52 -0800731 tReg, rlSrc2.lowReg);
732 opRegReg(cUnit, kOpSbc, tReg, rlSrc2.highReg);
733 genRegCopy(cUnit, rlResult.highReg, tReg);
Bill Buzbee1465db52009-09-23 17:17:35 -0700734 storeValueWide(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700735 return false;
Ben Chenge9695e52009-06-16 16:11:47 -0700736 }
Ben Chengba4fc8b2009-06-01 13:00:29 -0700737 default:
738 LOGE("Invalid long arith op");
Bill Buzbeefc519dc2010-03-06 23:30:57 -0800739 dvmCompilerAbort(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700740 }
741 if (!callOut) {
Bill Buzbee80cef862010-03-25 10:38:34 -0700742 genLong3Addr(cUnit, mir, firstOp, secondOp, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700743 } else {
Bill Buzbee1465db52009-09-23 17:17:35 -0700744 // Adjust return regs in to handle case of rem returning r2/r3
Bill Buzbeec6f10662010-02-09 11:16:15 -0800745 dvmCompilerFlushAllRegs(cUnit); /* Send everything to home location */
Bill Buzbee1465db52009-09-23 17:17:35 -0700746 loadValueDirectWideFixed(cUnit, rlSrc1, r0, r1);
Ben Cheng20d7e6c2011-02-18 17:12:42 -0800747 LOAD_FUNC_ADDR(cUnit, r14lr, (int) callTgt);
Bill Buzbee1465db52009-09-23 17:17:35 -0700748 loadValueDirectWideFixed(cUnit, rlSrc2, r2, r3);
Ben Cheng20d7e6c2011-02-18 17:12:42 -0800749 opReg(cUnit, kOpBlx, r14lr);
Elliott Hughes6a555132010-02-25 15:41:42 -0800750 dvmCompilerClobberCallRegs(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -0700751 if (retReg == r0)
Bill Buzbeec6f10662010-02-09 11:16:15 -0800752 rlResult = dvmCompilerGetReturnWide(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -0700753 else
Bill Buzbeec6f10662010-02-09 11:16:15 -0800754 rlResult = dvmCompilerGetReturnWideAlt(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -0700755 storeValueWide(cUnit, rlDest, rlResult);
Ben Chengd72564c2011-02-08 17:09:25 -0800756#if defined(WITH_SELF_VERIFICATION)
757 cUnit->usesLinkRegister = true;
758#endif
Ben Chengba4fc8b2009-06-01 13:00:29 -0700759 }
760 return false;
761}
762
Ben Cheng5d90c202009-11-22 23:31:11 -0800763static bool genArithOpInt(CompilationUnit *cUnit, MIR *mir,
764 RegLocation rlDest, RegLocation rlSrc1,
765 RegLocation rlSrc2)
Ben Chengba4fc8b2009-06-01 13:00:29 -0700766{
Bill Buzbee1465db52009-09-23 17:17:35 -0700767 OpKind op = kOpBkpt;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700768 bool callOut = false;
769 bool checkZero = false;
Bill Buzbee1465db52009-09-23 17:17:35 -0700770 bool unary = false;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700771 int retReg = r0;
772 void *callTgt;
Bill Buzbee1465db52009-09-23 17:17:35 -0700773 RegLocation rlResult;
Bill Buzbee0e605272009-12-01 14:28:05 -0800774 bool shiftOp = false;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700775
Dan Bornstein9a1f8162010-12-01 17:02:26 -0800776 switch (mir->dalvikInsn.opcode) {
Ben Chengba4fc8b2009-06-01 13:00:29 -0700777 case OP_NEG_INT:
Bill Buzbee1465db52009-09-23 17:17:35 -0700778 op = kOpNeg;
779 unary = true;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700780 break;
781 case OP_NOT_INT:
Bill Buzbee1465db52009-09-23 17:17:35 -0700782 op = kOpMvn;
783 unary = true;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700784 break;
785 case OP_ADD_INT:
786 case OP_ADD_INT_2ADDR:
Bill Buzbee1465db52009-09-23 17:17:35 -0700787 op = kOpAdd;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700788 break;
789 case OP_SUB_INT:
790 case OP_SUB_INT_2ADDR:
Bill Buzbee1465db52009-09-23 17:17:35 -0700791 op = kOpSub;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700792 break;
793 case OP_MUL_INT:
794 case OP_MUL_INT_2ADDR:
Bill Buzbee1465db52009-09-23 17:17:35 -0700795 op = kOpMul;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700796 break;
797 case OP_DIV_INT:
798 case OP_DIV_INT_2ADDR:
799 callOut = true;
800 checkZero = true;
801 callTgt = __aeabi_idiv;
802 retReg = r0;
803 break;
804 /* NOTE: returns in r1 */
805 case OP_REM_INT:
806 case OP_REM_INT_2ADDR:
807 callOut = true;
808 checkZero = true;
809 callTgt = __aeabi_idivmod;
810 retReg = r1;
811 break;
812 case OP_AND_INT:
813 case OP_AND_INT_2ADDR:
Bill Buzbee1465db52009-09-23 17:17:35 -0700814 op = kOpAnd;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700815 break;
816 case OP_OR_INT:
817 case OP_OR_INT_2ADDR:
Bill Buzbee1465db52009-09-23 17:17:35 -0700818 op = kOpOr;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700819 break;
820 case OP_XOR_INT:
821 case OP_XOR_INT_2ADDR:
Bill Buzbee1465db52009-09-23 17:17:35 -0700822 op = kOpXor;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700823 break;
824 case OP_SHL_INT:
825 case OP_SHL_INT_2ADDR:
Bill Buzbee0e605272009-12-01 14:28:05 -0800826 shiftOp = true;
Bill Buzbee1465db52009-09-23 17:17:35 -0700827 op = kOpLsl;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700828 break;
829 case OP_SHR_INT:
830 case OP_SHR_INT_2ADDR:
Bill Buzbee0e605272009-12-01 14:28:05 -0800831 shiftOp = true;
Bill Buzbee1465db52009-09-23 17:17:35 -0700832 op = kOpAsr;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700833 break;
834 case OP_USHR_INT:
835 case OP_USHR_INT_2ADDR:
Bill Buzbee0e605272009-12-01 14:28:05 -0800836 shiftOp = true;
Bill Buzbee1465db52009-09-23 17:17:35 -0700837 op = kOpLsr;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700838 break;
839 default:
840 LOGE("Invalid word arith op: 0x%x(%d)",
Dan Bornstein9a1f8162010-12-01 17:02:26 -0800841 mir->dalvikInsn.opcode, mir->dalvikInsn.opcode);
Bill Buzbeefc519dc2010-03-06 23:30:57 -0800842 dvmCompilerAbort(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700843 }
844 if (!callOut) {
Bill Buzbee1465db52009-09-23 17:17:35 -0700845 rlSrc1 = loadValue(cUnit, rlSrc1, kCoreReg);
846 if (unary) {
Bill Buzbeec6f10662010-02-09 11:16:15 -0800847 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Bill Buzbee1465db52009-09-23 17:17:35 -0700848 opRegReg(cUnit, op, rlResult.lowReg,
849 rlSrc1.lowReg);
Ben Chenge9695e52009-06-16 16:11:47 -0700850 } else {
Bill Buzbee1465db52009-09-23 17:17:35 -0700851 rlSrc2 = loadValue(cUnit, rlSrc2, kCoreReg);
Bill Buzbee0e605272009-12-01 14:28:05 -0800852 if (shiftOp) {
Bill Buzbeec6f10662010-02-09 11:16:15 -0800853 int tReg = dvmCompilerAllocTemp(cUnit);
Bill Buzbee0e605272009-12-01 14:28:05 -0800854 opRegRegImm(cUnit, kOpAnd, tReg, rlSrc2.lowReg, 31);
Bill Buzbeec6f10662010-02-09 11:16:15 -0800855 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Bill Buzbee0e605272009-12-01 14:28:05 -0800856 opRegRegReg(cUnit, op, rlResult.lowReg,
857 rlSrc1.lowReg, tReg);
Bill Buzbeec6f10662010-02-09 11:16:15 -0800858 dvmCompilerFreeTemp(cUnit, tReg);
Bill Buzbee0e605272009-12-01 14:28:05 -0800859 } else {
Bill Buzbeec6f10662010-02-09 11:16:15 -0800860 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Bill Buzbee0e605272009-12-01 14:28:05 -0800861 opRegRegReg(cUnit, op, rlResult.lowReg,
862 rlSrc1.lowReg, rlSrc2.lowReg);
863 }
Ben Chenge9695e52009-06-16 16:11:47 -0700864 }
Bill Buzbee1465db52009-09-23 17:17:35 -0700865 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700866 } else {
Bill Buzbee1465db52009-09-23 17:17:35 -0700867 RegLocation rlResult;
Bill Buzbeec6f10662010-02-09 11:16:15 -0800868 dvmCompilerFlushAllRegs(cUnit); /* Send everything to home location */
Bill Buzbee1465db52009-09-23 17:17:35 -0700869 loadValueDirectFixed(cUnit, rlSrc2, r1);
Ben Chengbd1326d2010-04-02 15:04:53 -0700870 LOAD_FUNC_ADDR(cUnit, r2, (int) callTgt);
Bill Buzbee1465db52009-09-23 17:17:35 -0700871 loadValueDirectFixed(cUnit, rlSrc1, r0);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700872 if (checkZero) {
Bill Buzbee1465db52009-09-23 17:17:35 -0700873 genNullCheck(cUnit, rlSrc2.sRegLow, r1, mir->offset, NULL);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700874 }
Bill Buzbee1465db52009-09-23 17:17:35 -0700875 opReg(cUnit, kOpBlx, r2);
Elliott Hughes6a555132010-02-25 15:41:42 -0800876 dvmCompilerClobberCallRegs(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -0700877 if (retReg == r0)
Bill Buzbeec6f10662010-02-09 11:16:15 -0800878 rlResult = dvmCompilerGetReturn(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -0700879 else
Bill Buzbeec6f10662010-02-09 11:16:15 -0800880 rlResult = dvmCompilerGetReturnAlt(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -0700881 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700882 }
883 return false;
884}
885
Ben Cheng5d90c202009-11-22 23:31:11 -0800886static bool genArithOp(CompilationUnit *cUnit, MIR *mir)
Ben Chengba4fc8b2009-06-01 13:00:29 -0700887{
Dan Bornstein9a1f8162010-12-01 17:02:26 -0800888 Opcode opcode = mir->dalvikInsn.opcode;
Bill Buzbee1465db52009-09-23 17:17:35 -0700889 RegLocation rlDest;
890 RegLocation rlSrc1;
891 RegLocation rlSrc2;
892 /* Deduce sizes of operands */
893 if (mir->ssaRep->numUses == 2) {
Bill Buzbeec6f10662010-02-09 11:16:15 -0800894 rlSrc1 = dvmCompilerGetSrc(cUnit, mir, 0);
895 rlSrc2 = dvmCompilerGetSrc(cUnit, mir, 1);
Bill Buzbee1465db52009-09-23 17:17:35 -0700896 } else if (mir->ssaRep->numUses == 3) {
Bill Buzbeec6f10662010-02-09 11:16:15 -0800897 rlSrc1 = dvmCompilerGetSrcWide(cUnit, mir, 0, 1);
898 rlSrc2 = dvmCompilerGetSrc(cUnit, mir, 2);
Bill Buzbee1465db52009-09-23 17:17:35 -0700899 } else {
Bill Buzbeec6f10662010-02-09 11:16:15 -0800900 rlSrc1 = dvmCompilerGetSrcWide(cUnit, mir, 0, 1);
901 rlSrc2 = dvmCompilerGetSrcWide(cUnit, mir, 2, 3);
Bill Buzbee1465db52009-09-23 17:17:35 -0700902 assert(mir->ssaRep->numUses == 4);
903 }
904 if (mir->ssaRep->numDefs == 1) {
Bill Buzbeec6f10662010-02-09 11:16:15 -0800905 rlDest = dvmCompilerGetDest(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -0700906 } else {
907 assert(mir->ssaRep->numDefs == 2);
Bill Buzbeec6f10662010-02-09 11:16:15 -0800908 rlDest = dvmCompilerGetDestWide(cUnit, mir, 0, 1);
Bill Buzbee1465db52009-09-23 17:17:35 -0700909 }
Ben Chengba4fc8b2009-06-01 13:00:29 -0700910
Dan Bornstein9a1f8162010-12-01 17:02:26 -0800911 if ((opcode >= OP_ADD_LONG_2ADDR) && (opcode <= OP_XOR_LONG_2ADDR)) {
Ben Cheng5d90c202009-11-22 23:31:11 -0800912 return genArithOpLong(cUnit,mir, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700913 }
Dan Bornstein9a1f8162010-12-01 17:02:26 -0800914 if ((opcode >= OP_ADD_LONG) && (opcode <= OP_XOR_LONG)) {
Ben Cheng5d90c202009-11-22 23:31:11 -0800915 return genArithOpLong(cUnit,mir, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700916 }
Dan Bornstein9a1f8162010-12-01 17:02:26 -0800917 if ((opcode >= OP_SHL_LONG_2ADDR) && (opcode <= OP_USHR_LONG_2ADDR)) {
Ben Cheng5d90c202009-11-22 23:31:11 -0800918 return genShiftOpLong(cUnit,mir, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700919 }
Dan Bornstein9a1f8162010-12-01 17:02:26 -0800920 if ((opcode >= OP_SHL_LONG) && (opcode <= OP_USHR_LONG)) {
Ben Cheng5d90c202009-11-22 23:31:11 -0800921 return genShiftOpLong(cUnit,mir, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700922 }
Dan Bornstein9a1f8162010-12-01 17:02:26 -0800923 if ((opcode >= OP_ADD_INT_2ADDR) && (opcode <= OP_USHR_INT_2ADDR)) {
Ben Cheng5d90c202009-11-22 23:31:11 -0800924 return genArithOpInt(cUnit,mir, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700925 }
Dan Bornstein9a1f8162010-12-01 17:02:26 -0800926 if ((opcode >= OP_ADD_INT) && (opcode <= OP_USHR_INT)) {
Ben Cheng5d90c202009-11-22 23:31:11 -0800927 return genArithOpInt(cUnit,mir, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700928 }
Dan Bornstein9a1f8162010-12-01 17:02:26 -0800929 if ((opcode >= OP_ADD_FLOAT_2ADDR) && (opcode <= OP_REM_FLOAT_2ADDR)) {
Ben Cheng5d90c202009-11-22 23:31:11 -0800930 return genArithOpFloat(cUnit,mir, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700931 }
Dan Bornstein9a1f8162010-12-01 17:02:26 -0800932 if ((opcode >= OP_ADD_FLOAT) && (opcode <= OP_REM_FLOAT)) {
Ben Cheng5d90c202009-11-22 23:31:11 -0800933 return genArithOpFloat(cUnit, mir, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700934 }
Dan Bornstein9a1f8162010-12-01 17:02:26 -0800935 if ((opcode >= OP_ADD_DOUBLE_2ADDR) && (opcode <= OP_REM_DOUBLE_2ADDR)) {
Ben Cheng5d90c202009-11-22 23:31:11 -0800936 return genArithOpDouble(cUnit,mir, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700937 }
Dan Bornstein9a1f8162010-12-01 17:02:26 -0800938 if ((opcode >= OP_ADD_DOUBLE) && (opcode <= OP_REM_DOUBLE)) {
Ben Cheng5d90c202009-11-22 23:31:11 -0800939 return genArithOpDouble(cUnit,mir, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700940 }
941 return true;
942}
943
Bill Buzbee1465db52009-09-23 17:17:35 -0700944/* Generate unconditional branch instructions */
945static ArmLIR *genUnconditionalBranch(CompilationUnit *cUnit, ArmLIR *target)
946{
947 ArmLIR *branch = opNone(cUnit, kOpUncondBr);
948 branch->generic.target = (LIR *) target;
949 return branch;
950}
951
Bill Buzbee1465db52009-09-23 17:17:35 -0700952/* Perform the actual operation for OP_RETURN_* */
953static void genReturnCommon(CompilationUnit *cUnit, MIR *mir)
954{
Ben Cheng20d7e6c2011-02-18 17:12:42 -0800955 genDispatchToHandler(cUnit, gDvmJit.methodTraceSupport ?
956 TEMPLATE_RETURN_PROF : TEMPLATE_RETURN);
Ben Cheng978738d2010-05-13 13:45:57 -0700957#if defined(WITH_JIT_TUNING)
Ben Cheng20d7e6c2011-02-18 17:12:42 -0800958 gDvmJit.returnOp++;
Bill Buzbee1465db52009-09-23 17:17:35 -0700959#endif
Ben Cheng20d7e6c2011-02-18 17:12:42 -0800960 int dPC = (int) (cUnit->method->insns + mir->offset);
961 /* Insert branch, but defer setting of target */
962 ArmLIR *branch = genUnconditionalBranch(cUnit, NULL);
963 /* Set up the place holder to reconstruct this Dalvik PC */
964 ArmLIR *pcrLabel = (ArmLIR *) dvmCompilerNew(sizeof(ArmLIR), true);
965 pcrLabel->opcode = kArmPseudoPCReconstructionCell;
966 pcrLabel->operands[0] = dPC;
967 pcrLabel->operands[1] = mir->offset;
968 /* Insert the place holder to the growable list */
969 dvmInsertGrowableList(&cUnit->pcReconstructionList, (intptr_t) pcrLabel);
970 /* Branch to the PC reconstruction code */
971 branch->generic.target = (LIR *) pcrLabel;
Bill Buzbee1465db52009-09-23 17:17:35 -0700972}
973
Ben Chengba4fc8b2009-06-01 13:00:29 -0700974static void genProcessArgsNoRange(CompilationUnit *cUnit, MIR *mir,
975 DecodedInstruction *dInsn,
Bill Buzbee89efc3d2009-07-28 11:22:22 -0700976 ArmLIR **pcrLabel)
Ben Chengba4fc8b2009-06-01 13:00:29 -0700977{
978 unsigned int i;
979 unsigned int regMask = 0;
Bill Buzbee1465db52009-09-23 17:17:35 -0700980 RegLocation rlArg;
981 int numDone = 0;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700982
Bill Buzbee1465db52009-09-23 17:17:35 -0700983 /*
984 * Load arguments to r0..r4. Note that these registers may contain
985 * live values, so we clobber them immediately after loading to prevent
986 * them from being used as sources for subsequent loads.
987 */
Bill Buzbeec6f10662010-02-09 11:16:15 -0800988 dvmCompilerLockAllTemps(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700989 for (i = 0; i < dInsn->vA; i++) {
990 regMask |= 1 << i;
Bill Buzbeec6f10662010-02-09 11:16:15 -0800991 rlArg = dvmCompilerGetSrc(cUnit, mir, numDone++);
Bill Buzbee1465db52009-09-23 17:17:35 -0700992 loadValueDirectFixed(cUnit, rlArg, i);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700993 }
994 if (regMask) {
995 /* Up to 5 args are pushed on top of FP - sizeofStackSaveArea */
Ben Cheng20d7e6c2011-02-18 17:12:42 -0800996 opRegRegImm(cUnit, kOpSub, r7, r5FP,
Bill Buzbee1465db52009-09-23 17:17:35 -0700997 sizeof(StackSaveArea) + (dInsn->vA << 2));
Ben Chengba4fc8b2009-06-01 13:00:29 -0700998 /* generate null check */
999 if (pcrLabel) {
Bill Buzbeec6f10662010-02-09 11:16:15 -08001000 *pcrLabel = genNullCheck(cUnit, dvmCompilerSSASrc(mir, 0), r0,
Bill Buzbee1465db52009-09-23 17:17:35 -07001001 mir->offset, NULL);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001002 }
Bill Buzbee270c1d62009-08-13 16:58:07 -07001003 storeMultiple(cUnit, r7, regMask);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001004 }
1005}
1006
1007static void genProcessArgsRange(CompilationUnit *cUnit, MIR *mir,
1008 DecodedInstruction *dInsn,
Bill Buzbee89efc3d2009-07-28 11:22:22 -07001009 ArmLIR **pcrLabel)
Ben Chengba4fc8b2009-06-01 13:00:29 -07001010{
1011 int srcOffset = dInsn->vC << 2;
1012 int numArgs = dInsn->vA;
1013 int regMask;
Bill Buzbee1465db52009-09-23 17:17:35 -07001014
1015 /*
1016 * Note: here, all promoted registers will have been flushed
1017 * back to the Dalvik base locations, so register usage restrictins
1018 * are lifted. All parms loaded from original Dalvik register
1019 * region - even though some might conceivably have valid copies
1020 * cached in a preserved register.
1021 */
Bill Buzbeec6f10662010-02-09 11:16:15 -08001022 dvmCompilerLockAllTemps(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07001023
Ben Chengba4fc8b2009-06-01 13:00:29 -07001024 /*
Ben Cheng20d7e6c2011-02-18 17:12:42 -08001025 * r4PC : &r5FP[vC]
Ben Chengba4fc8b2009-06-01 13:00:29 -07001026 * r7: &newFP[0]
1027 */
Ben Cheng20d7e6c2011-02-18 17:12:42 -08001028 opRegRegImm(cUnit, kOpAdd, r4PC, r5FP, srcOffset);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001029 /* load [r0 .. min(numArgs,4)] */
1030 regMask = (1 << ((numArgs < 4) ? numArgs : 4)) - 1;
Ben Chengd7d426a2009-09-22 11:23:36 -07001031 /*
1032 * Protect the loadMultiple instruction from being reordered with other
1033 * Dalvik stack accesses.
jeffhao71eee1f2011-01-04 14:18:54 -08001034 *
1035 * This code is also shared by the invoke jumbo instructions, and this
1036 * does not need to be done if the invoke jumbo has no arguments.
Ben Chengd7d426a2009-09-22 11:23:36 -07001037 */
jeffhao71eee1f2011-01-04 14:18:54 -08001038 if (numArgs != 0) loadMultiple(cUnit, r4PC, regMask);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001039
Ben Cheng20d7e6c2011-02-18 17:12:42 -08001040 opRegRegImm(cUnit, kOpSub, r7, r5FP,
Bill Buzbee1465db52009-09-23 17:17:35 -07001041 sizeof(StackSaveArea) + (numArgs << 2));
Ben Chengba4fc8b2009-06-01 13:00:29 -07001042 /* generate null check */
1043 if (pcrLabel) {
Bill Buzbeec6f10662010-02-09 11:16:15 -08001044 *pcrLabel = genNullCheck(cUnit, dvmCompilerSSASrc(mir, 0), r0,
Bill Buzbee1465db52009-09-23 17:17:35 -07001045 mir->offset, NULL);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001046 }
1047
1048 /*
1049 * Handle remaining 4n arguments:
1050 * store previously loaded 4 values and load the next 4 values
1051 */
1052 if (numArgs >= 8) {
Bill Buzbee89efc3d2009-07-28 11:22:22 -07001053 ArmLIR *loopLabel = NULL;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001054 /*
1055 * r0 contains "this" and it will be used later, so push it to the stack
Ben Cheng20d7e6c2011-02-18 17:12:42 -08001056 * first. Pushing r5FP is just for stack alignment purposes.
Ben Chengba4fc8b2009-06-01 13:00:29 -07001057 */
Ben Cheng20d7e6c2011-02-18 17:12:42 -08001058 opImm(cUnit, kOpPush, (1 << r0 | 1 << r5FP));
Ben Chengba4fc8b2009-06-01 13:00:29 -07001059 /* No need to generate the loop structure if numArgs <= 11 */
1060 if (numArgs > 11) {
1061 loadConstant(cUnit, 5, ((numArgs - 4) >> 2) << 2);
Bill Buzbee1465db52009-09-23 17:17:35 -07001062 loopLabel = newLIR0(cUnit, kArmPseudoTargetLabel);
Ben Chengd7d426a2009-09-22 11:23:36 -07001063 loopLabel->defMask = ENCODE_ALL;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001064 }
Bill Buzbee270c1d62009-08-13 16:58:07 -07001065 storeMultiple(cUnit, r7, regMask);
Ben Chengd7d426a2009-09-22 11:23:36 -07001066 /*
1067 * Protect the loadMultiple instruction from being reordered with other
1068 * Dalvik stack accesses.
1069 */
Bill Buzbee270c1d62009-08-13 16:58:07 -07001070 loadMultiple(cUnit, r4PC, regMask);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001071 /* No need to generate the loop structure if numArgs <= 11 */
1072 if (numArgs > 11) {
Ben Cheng20d7e6c2011-02-18 17:12:42 -08001073 opRegImm(cUnit, kOpSub, r5FP, 4);
Bill Buzbee1465db52009-09-23 17:17:35 -07001074 genConditionalBranch(cUnit, kArmCondNe, loopLabel);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001075 }
1076 }
1077
1078 /* Save the last batch of loaded values */
jeffhao71eee1f2011-01-04 14:18:54 -08001079 if (numArgs != 0) storeMultiple(cUnit, r7, regMask);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001080
1081 /* Generate the loop epilogue - don't use r0 */
1082 if ((numArgs > 4) && (numArgs % 4)) {
1083 regMask = ((1 << (numArgs & 0x3)) - 1) << 1;
Ben Chengd7d426a2009-09-22 11:23:36 -07001084 /*
1085 * Protect the loadMultiple instruction from being reordered with other
1086 * Dalvik stack accesses.
1087 */
Bill Buzbee270c1d62009-08-13 16:58:07 -07001088 loadMultiple(cUnit, r4PC, regMask);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001089 }
1090 if (numArgs >= 8)
Ben Cheng20d7e6c2011-02-18 17:12:42 -08001091 opImm(cUnit, kOpPop, (1 << r0 | 1 << r5FP));
Ben Chengba4fc8b2009-06-01 13:00:29 -07001092
1093 /* Save the modulo 4 arguments */
1094 if ((numArgs > 4) && (numArgs % 4)) {
Bill Buzbee270c1d62009-08-13 16:58:07 -07001095 storeMultiple(cUnit, r7, regMask);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001096 }
1097}
1098
Ben Cheng38329f52009-07-07 14:19:20 -07001099/*
1100 * Generate code to setup the call stack then jump to the chaining cell if it
1101 * is not a native method.
1102 */
1103static void genInvokeSingletonCommon(CompilationUnit *cUnit, MIR *mir,
Bill Buzbee89efc3d2009-07-28 11:22:22 -07001104 BasicBlock *bb, ArmLIR *labelList,
1105 ArmLIR *pcrLabel,
Ben Cheng38329f52009-07-07 14:19:20 -07001106 const Method *calleeMethod)
Ben Chengba4fc8b2009-06-01 13:00:29 -07001107{
Bill Buzbee1465db52009-09-23 17:17:35 -07001108 /*
1109 * Note: all Dalvik register state should be flushed to
1110 * memory by the point, so register usage restrictions no
1111 * longer apply. All temp & preserved registers may be used.
1112 */
Bill Buzbeec6f10662010-02-09 11:16:15 -08001113 dvmCompilerLockAllTemps(cUnit);
Bill Buzbee89efc3d2009-07-28 11:22:22 -07001114 ArmLIR *retChainingCell = &labelList[bb->fallThrough->id];
Ben Chengba4fc8b2009-06-01 13:00:29 -07001115
1116 /* r1 = &retChainingCell */
Ben Cheng20d7e6c2011-02-18 17:12:42 -08001117 ArmLIR *addrRetChain = opRegRegImm(cUnit, kOpAdd, r1, r15pc, 0);
Ben Chengc8293e72010-10-12 11:50:10 -07001118
Ben Chengba4fc8b2009-06-01 13:00:29 -07001119 /* r4PC = dalvikCallsite */
1120 loadConstant(cUnit, r4PC,
1121 (int) (cUnit->method->insns + mir->offset));
1122 addrRetChain->generic.target = (LIR *) retChainingCell;
Ben Chengc8293e72010-10-12 11:50:10 -07001123
1124 /* r7 = calleeMethod->registersSize */
1125 loadConstant(cUnit, r7, calleeMethod->registersSize);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001126 /*
Ben Cheng38329f52009-07-07 14:19:20 -07001127 * r0 = calleeMethod (loaded upon calling genInvokeSingletonCommon)
Ben Chengba4fc8b2009-06-01 13:00:29 -07001128 * r1 = &ChainingCell
Ben Chengc8293e72010-10-12 11:50:10 -07001129 * r2 = calleeMethod->outsSize (to be loaded later for Java callees)
Ben Chengba4fc8b2009-06-01 13:00:29 -07001130 * r4PC = callsiteDPC
Ben Chengc8293e72010-10-12 11:50:10 -07001131 * r7 = calleeMethod->registersSize
Ben Chengba4fc8b2009-06-01 13:00:29 -07001132 */
1133 if (dvmIsNativeMethod(calleeMethod)) {
buzbee18fba342011-01-19 15:31:15 -08001134 genDispatchToHandler(cUnit, gDvmJit.methodTraceSupport ?
1135 TEMPLATE_INVOKE_METHOD_NATIVE_PROF :
1136 TEMPLATE_INVOKE_METHOD_NATIVE);
Ben Cheng978738d2010-05-13 13:45:57 -07001137#if defined(WITH_JIT_TUNING)
Ben Cheng38329f52009-07-07 14:19:20 -07001138 gDvmJit.invokeNative++;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001139#endif
1140 } else {
Ben Chengc8293e72010-10-12 11:50:10 -07001141 /* For Java callees, set up r2 to be calleeMethod->outsSize */
1142 loadConstant(cUnit, r2, calleeMethod->outsSize);
buzbee18fba342011-01-19 15:31:15 -08001143 genDispatchToHandler(cUnit, gDvmJit.methodTraceSupport ?
1144 TEMPLATE_INVOKE_METHOD_CHAIN_PROF :
1145 TEMPLATE_INVOKE_METHOD_CHAIN);
Ben Cheng978738d2010-05-13 13:45:57 -07001146#if defined(WITH_JIT_TUNING)
Ben Cheng86717f72010-03-05 15:27:21 -08001147 gDvmJit.invokeMonomorphic++;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001148#endif
Ben Cheng38329f52009-07-07 14:19:20 -07001149 /* Branch to the chaining cell */
Ben Chengba4fc8b2009-06-01 13:00:29 -07001150 genUnconditionalBranch(cUnit, &labelList[bb->taken->id]);
1151 }
1152 /* Handle exceptions using the interpreter */
1153 genTrap(cUnit, mir->offset, pcrLabel);
1154}
1155
Ben Cheng38329f52009-07-07 14:19:20 -07001156/*
1157 * Generate code to check the validity of a predicted chain and take actions
1158 * based on the result.
1159 *
1160 * 0x426a99aa : ldr r4, [pc, #72] --> r4 <- dalvikPC of this invoke
1161 * 0x426a99ac : add r1, pc, #32 --> r1 <- &retChainingCell
1162 * 0x426a99ae : add r2, pc, #40 --> r2 <- &predictedChainingCell
1163 * 0x426a99b0 : blx_1 0x426a918c --+ TEMPLATE_INVOKE_METHOD_PREDICTED_CHAIN
1164 * 0x426a99b2 : blx_2 see above --+
1165 * 0x426a99b4 : b 0x426a99d8 --> off to the predicted chain
1166 * 0x426a99b6 : b 0x426a99c8 --> punt to the interpreter
1167 * 0x426a99b8 : ldr r0, [r7, #44] --> r0 <- this->class->vtable[methodIdx]
1168 * 0x426a99ba : cmp r1, #0 --> compare r1 (rechain count) against 0
1169 * 0x426a99bc : bgt 0x426a99c2 --> >=0? don't rechain
Ben Chengaf5aa1f2011-01-04 15:37:04 -08001170 * 0x426a99be : ldr r7, [pc, #off]--+ dvmJitToPatchPredictedChain
Ben Cheng38329f52009-07-07 14:19:20 -07001171 * 0x426a99c0 : blx r7 --+
1172 * 0x426a99c2 : add r1, pc, #12 --> r1 <- &retChainingCell
1173 * 0x426a99c4 : blx_1 0x426a9098 --+ TEMPLATE_INVOKE_METHOD_NO_OPT
1174 * 0x426a99c6 : blx_2 see above --+
1175 */
1176static void genInvokeVirtualCommon(CompilationUnit *cUnit, MIR *mir,
1177 int methodIndex,
Bill Buzbee89efc3d2009-07-28 11:22:22 -07001178 ArmLIR *retChainingCell,
1179 ArmLIR *predChainingCell,
1180 ArmLIR *pcrLabel)
Ben Cheng38329f52009-07-07 14:19:20 -07001181{
Bill Buzbee1465db52009-09-23 17:17:35 -07001182 /*
1183 * Note: all Dalvik register state should be flushed to
1184 * memory by the point, so register usage restrictions no
1185 * longer apply. Lock temps to prevent them from being
1186 * allocated by utility routines.
1187 */
Bill Buzbeec6f10662010-02-09 11:16:15 -08001188 dvmCompilerLockAllTemps(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07001189
Ben Cheng38329f52009-07-07 14:19:20 -07001190 /* "this" is already left in r0 by genProcessArgs* */
1191
1192 /* r4PC = dalvikCallsite */
1193 loadConstant(cUnit, r4PC,
1194 (int) (cUnit->method->insns + mir->offset));
1195
1196 /* r1 = &retChainingCell */
Ben Cheng20d7e6c2011-02-18 17:12:42 -08001197 ArmLIR *addrRetChain = opRegRegImm(cUnit, kOpAdd, r1, r15pc, 0);
Ben Cheng38329f52009-07-07 14:19:20 -07001198 addrRetChain->generic.target = (LIR *) retChainingCell;
1199
1200 /* r2 = &predictedChainingCell */
Ben Cheng20d7e6c2011-02-18 17:12:42 -08001201 ArmLIR *predictedChainingCell = opRegRegImm(cUnit, kOpAdd, r2, r15pc, 0);
Ben Cheng38329f52009-07-07 14:19:20 -07001202 predictedChainingCell->generic.target = (LIR *) predChainingCell;
1203
buzbee18fba342011-01-19 15:31:15 -08001204 genDispatchToHandler(cUnit, gDvmJit.methodTraceSupport ?
1205 TEMPLATE_INVOKE_METHOD_PREDICTED_CHAIN_PROF :
1206 TEMPLATE_INVOKE_METHOD_PREDICTED_CHAIN);
Ben Cheng38329f52009-07-07 14:19:20 -07001207
1208 /* return through lr - jump to the chaining cell */
1209 genUnconditionalBranch(cUnit, predChainingCell);
1210
1211 /*
1212 * null-check on "this" may have been eliminated, but we still need a PC-
1213 * reconstruction label for stack overflow bailout.
1214 */
1215 if (pcrLabel == NULL) {
1216 int dPC = (int) (cUnit->method->insns + mir->offset);
Carl Shapirofc75f3e2010-12-07 11:43:38 -08001217 pcrLabel = (ArmLIR *) dvmCompilerNew(sizeof(ArmLIR), true);
Dan Bornstein9a1f8162010-12-01 17:02:26 -08001218 pcrLabel->opcode = kArmPseudoPCReconstructionCell;
Ben Cheng38329f52009-07-07 14:19:20 -07001219 pcrLabel->operands[0] = dPC;
1220 pcrLabel->operands[1] = mir->offset;
1221 /* Insert the place holder to the growable list */
Ben Cheng00603072010-10-28 11:13:58 -07001222 dvmInsertGrowableList(&cUnit->pcReconstructionList,
1223 (intptr_t) pcrLabel);
Ben Cheng38329f52009-07-07 14:19:20 -07001224 }
1225
1226 /* return through lr+2 - punt to the interpreter */
1227 genUnconditionalBranch(cUnit, pcrLabel);
1228
1229 /*
1230 * return through lr+4 - fully resolve the callee method.
1231 * r1 <- count
1232 * r2 <- &predictedChainCell
1233 * r3 <- this->class
1234 * r4 <- dPC
1235 * r7 <- this->class->vtable
1236 */
1237
1238 /* r0 <- calleeMethod */
Bill Buzbee270c1d62009-08-13 16:58:07 -07001239 loadWordDisp(cUnit, r7, methodIndex * 4, r0);
Ben Cheng38329f52009-07-07 14:19:20 -07001240
1241 /* Check if rechain limit is reached */
buzbee8f8109a2010-08-31 10:16:35 -07001242 ArmLIR *bypassRechaining = genCmpImmBranch(cUnit, kArmCondGt, r1, 0);
Ben Cheng38329f52009-07-07 14:19:20 -07001243
Ben Chengaf5aa1f2011-01-04 15:37:04 -08001244 LOAD_FUNC_ADDR(cUnit, r7, (int) dvmJitToPatchPredictedChain);
Ben Cheng38329f52009-07-07 14:19:20 -07001245
Ben Cheng20d7e6c2011-02-18 17:12:42 -08001246 genRegCopy(cUnit, r1, r6SELF);
Ben Chengb88ec3c2010-05-17 12:50:33 -07001247
Ben Cheng38329f52009-07-07 14:19:20 -07001248 /*
1249 * r0 = calleeMethod
1250 * r2 = &predictedChainingCell
1251 * r3 = class
1252 *
1253 * &returnChainingCell has been loaded into r1 but is not needed
1254 * when patching the chaining cell and will be clobbered upon
1255 * returning so it will be reconstructed again.
1256 */
Bill Buzbee1465db52009-09-23 17:17:35 -07001257 opReg(cUnit, kOpBlx, r7);
Ben Cheng38329f52009-07-07 14:19:20 -07001258
1259 /* r1 = &retChainingCell */
Ben Cheng20d7e6c2011-02-18 17:12:42 -08001260 addrRetChain = opRegRegImm(cUnit, kOpAdd, r1, r15pc, 0);
Ben Cheng38329f52009-07-07 14:19:20 -07001261 addrRetChain->generic.target = (LIR *) retChainingCell;
1262
1263 bypassRechaining->generic.target = (LIR *) addrRetChain;
1264 /*
1265 * r0 = calleeMethod,
1266 * r1 = &ChainingCell,
1267 * r4PC = callsiteDPC,
1268 */
buzbee18fba342011-01-19 15:31:15 -08001269 genDispatchToHandler(cUnit, gDvmJit.methodTraceSupport ?
1270 TEMPLATE_INVOKE_METHOD_NO_OPT_PROF :
1271 TEMPLATE_INVOKE_METHOD_NO_OPT);
Ben Cheng978738d2010-05-13 13:45:57 -07001272#if defined(WITH_JIT_TUNING)
Ben Cheng86717f72010-03-05 15:27:21 -08001273 gDvmJit.invokePolymorphic++;
Ben Cheng38329f52009-07-07 14:19:20 -07001274#endif
1275 /* Handle exceptions using the interpreter */
1276 genTrap(cUnit, mir->offset, pcrLabel);
1277}
1278
Ben Cheng20d7e6c2011-02-18 17:12:42 -08001279/* "this" pointer is already in r0 */
1280static void genInvokeVirtualWholeMethod(CompilationUnit *cUnit,
1281 MIR *mir,
1282 void *calleeAddr,
1283 ArmLIR *retChainingCell)
1284{
1285 CallsiteInfo *callsiteInfo = mir->meta.callsiteInfo;
1286 dvmCompilerLockAllTemps(cUnit);
1287
1288 loadConstant(cUnit, r1, (int) callsiteInfo->clazz);
1289
1290 loadWordDisp(cUnit, r0, offsetof(Object, clazz), r2);
1291 /* Branch to the slow path if classes are not equal */
1292 opRegReg(cUnit, kOpCmp, r1, r2);
1293 /*
1294 * Set the misPredBranchOver target so that it will be generated when the
1295 * code for the non-optimized invoke is generated.
1296 */
1297 ArmLIR *classCheck = opCondBranch(cUnit, kArmCondNe);
1298
1299 /* r0 = the Dalvik PC of the callsite */
1300 loadConstant(cUnit, r0, (int) (cUnit->method->insns + mir->offset));
1301
1302 newLIR2(cUnit, kThumbBl1, (int) calleeAddr, (int) calleeAddr);
1303 newLIR2(cUnit, kThumbBl2, (int) calleeAddr, (int) calleeAddr);
1304 genUnconditionalBranch(cUnit, retChainingCell);
1305
1306 /* Target of slow path */
1307 ArmLIR *slowPathLabel = newLIR0(cUnit, kArmPseudoTargetLabel);
1308
1309 slowPathLabel->defMask = ENCODE_ALL;
1310 classCheck->generic.target = (LIR *) slowPathLabel;
1311
1312 // FIXME
1313 cUnit->printMe = true;
1314}
1315
1316static void genInvokeSingletonWholeMethod(CompilationUnit *cUnit,
1317 MIR *mir,
1318 void *calleeAddr,
1319 ArmLIR *retChainingCell)
1320{
1321 /* r0 = the Dalvik PC of the callsite */
1322 loadConstant(cUnit, r0, (int) (cUnit->method->insns + mir->offset));
1323
1324 newLIR2(cUnit, kThumbBl1, (int) calleeAddr, (int) calleeAddr);
1325 newLIR2(cUnit, kThumbBl2, (int) calleeAddr, (int) calleeAddr);
1326 genUnconditionalBranch(cUnit, retChainingCell);
1327
1328 // FIXME
1329 cUnit->printMe = true;
1330}
1331
Ben Chengba4fc8b2009-06-01 13:00:29 -07001332/* Geneate a branch to go back to the interpreter */
1333static void genPuntToInterp(CompilationUnit *cUnit, unsigned int offset)
1334{
1335 /* r0 = dalvik pc */
Bill Buzbeec6f10662010-02-09 11:16:15 -08001336 dvmCompilerFlushAllRegs(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001337 loadConstant(cUnit, r0, (int) (cUnit->method->insns + offset));
Ben Cheng20d7e6c2011-02-18 17:12:42 -08001338 loadWordDisp(cUnit, r6SELF, offsetof(Thread,
Bill Buzbee270c1d62009-08-13 16:58:07 -07001339 jitToInterpEntries.dvmJitToInterpPunt), r1);
Bill Buzbee1465db52009-09-23 17:17:35 -07001340 opReg(cUnit, kOpBlx, r1);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001341}
1342
1343/*
1344 * Attempt to single step one instruction using the interpreter and return
1345 * to the compiled code for the next Dalvik instruction
1346 */
1347static void genInterpSingleStep(CompilationUnit *cUnit, MIR *mir)
1348{
Dan Bornsteine4852762010-12-02 12:45:00 -08001349 int flags = dexGetFlagsFromOpcode(mir->dalvikInsn.opcode);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001350 int flagsToCheck = kInstrCanBranch | kInstrCanSwitch | kInstrCanReturn |
1351 kInstrCanThrow;
Bill Buzbee1465db52009-09-23 17:17:35 -07001352
Bill Buzbee45273872010-03-11 11:12:15 -08001353 //If already optimized out, just ignore
Dan Bornstein9a1f8162010-12-01 17:02:26 -08001354 if (mir->dalvikInsn.opcode == OP_NOP)
Bill Buzbee45273872010-03-11 11:12:15 -08001355 return;
1356
Bill Buzbee1465db52009-09-23 17:17:35 -07001357 //Ugly, but necessary. Flush all Dalvik regs so Interp can find them
Bill Buzbeec6f10662010-02-09 11:16:15 -08001358 dvmCompilerFlushAllRegs(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07001359
Ben Chengba4fc8b2009-06-01 13:00:29 -07001360 if ((mir->next == NULL) || (flags & flagsToCheck)) {
1361 genPuntToInterp(cUnit, mir->offset);
1362 return;
1363 }
buzbee9f601a92011-02-11 17:48:20 -08001364 int entryAddr = offsetof(Thread,
Ben Chengba4fc8b2009-06-01 13:00:29 -07001365 jitToInterpEntries.dvmJitToInterpSingleStep);
Ben Cheng20d7e6c2011-02-18 17:12:42 -08001366 loadWordDisp(cUnit, r6SELF, entryAddr, r2);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001367 /* r0 = dalvik pc */
1368 loadConstant(cUnit, r0, (int) (cUnit->method->insns + mir->offset));
1369 /* r1 = dalvik pc of following instruction */
1370 loadConstant(cUnit, r1, (int) (cUnit->method->insns + mir->next->offset));
Bill Buzbee1465db52009-09-23 17:17:35 -07001371 opReg(cUnit, kOpBlx, r2);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001372}
1373
Carl Shapiro01605d22011-02-01 11:32:44 -08001374#if defined(_ARMV5TE) || defined(_ARMV5TE_VFP)
Bill Buzbeec1d9ed42010-02-02 11:04:33 -08001375/*
1376 * To prevent a thread in a monitor wait from blocking the Jit from
1377 * resetting the code cache, heavyweight monitor lock will not
1378 * be allowed to return to an existing translation. Instead, we will
1379 * handle them by branching to a handler, which will in turn call the
1380 * runtime lock routine and then branch directly back to the
1381 * interpreter main loop. Given the high cost of the heavyweight
1382 * lock operation, this additional cost should be slight (especially when
1383 * considering that we expect the vast majority of lock operations to
1384 * use the fast-path thin lock bypass).
1385 */
Ben Cheng5d90c202009-11-22 23:31:11 -08001386static void genMonitorPortable(CompilationUnit *cUnit, MIR *mir)
Bill Buzbee270c1d62009-08-13 16:58:07 -07001387{
Dan Bornstein9a1f8162010-12-01 17:02:26 -08001388 bool isEnter = (mir->dalvikInsn.opcode == OP_MONITOR_ENTER);
Bill Buzbee1465db52009-09-23 17:17:35 -07001389 genExportPC(cUnit, mir);
Bill Buzbeec6f10662010-02-09 11:16:15 -08001390 dvmCompilerFlushAllRegs(cUnit); /* Send everything to home location */
1391 RegLocation rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07001392 loadValueDirectFixed(cUnit, rlSrc, r1);
Ben Cheng20d7e6c2011-02-18 17:12:42 -08001393 genRegCopy(cUnit, r0, r6SELF);
Bill Buzbeec1d9ed42010-02-02 11:04:33 -08001394 genNullCheck(cUnit, rlSrc.sRegLow, r1, mir->offset, NULL);
Bill Buzbeeefbd3c52009-11-04 22:18:40 -08001395 if (isEnter) {
Bill Buzbeec1d9ed42010-02-02 11:04:33 -08001396 /* Get dPC of next insn */
1397 loadConstant(cUnit, r4PC, (int)(cUnit->method->insns + mir->offset +
Dan Bornsteine4852762010-12-02 12:45:00 -08001398 dexGetWidthFromOpcode(OP_MONITOR_ENTER)));
Bill Buzbeec1d9ed42010-02-02 11:04:33 -08001399 genDispatchToHandler(cUnit, TEMPLATE_MONITOR_ENTER);
Bill Buzbee1465db52009-09-23 17:17:35 -07001400 } else {
Ben Chengbd1326d2010-04-02 15:04:53 -07001401 LOAD_FUNC_ADDR(cUnit, r2, (int)dvmUnlockObject);
Bill Buzbeec1d9ed42010-02-02 11:04:33 -08001402 /* Do the call */
1403 opReg(cUnit, kOpBlx, r2);
buzbee8f8109a2010-08-31 10:16:35 -07001404 /* Did we throw? */
1405 ArmLIR *branchOver = genCmpImmBranch(cUnit, kArmCondNe, r0, 0);
Bill Buzbee6bbdd6b2010-02-16 14:40:01 -08001406 loadConstant(cUnit, r0,
1407 (int) (cUnit->method->insns + mir->offset +
Dan Bornsteine4852762010-12-02 12:45:00 -08001408 dexGetWidthFromOpcode(OP_MONITOR_EXIT)));
Bill Buzbee6bbdd6b2010-02-16 14:40:01 -08001409 genDispatchToHandler(cUnit, TEMPLATE_THROW_EXCEPTION_COMMON);
1410 ArmLIR *target = newLIR0(cUnit, kArmPseudoTargetLabel);
1411 target->defMask = ENCODE_ALL;
1412 branchOver->generic.target = (LIR *) target;
Elliott Hughes6a555132010-02-25 15:41:42 -08001413 dvmCompilerClobberCallRegs(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07001414 }
Bill Buzbee270c1d62009-08-13 16:58:07 -07001415}
Ben Chengfc075c22010-05-28 15:20:08 -07001416#endif
Bill Buzbee270c1d62009-08-13 16:58:07 -07001417
Ben Chengba4fc8b2009-06-01 13:00:29 -07001418/*
buzbee9f601a92011-02-11 17:48:20 -08001419 * Fetch *self->suspendCount. If the suspend count is non-zero,
Ben Cheng7ab74e12011-02-03 14:02:06 -08001420 * punt to the interpreter.
1421 */
1422static void genSuspendPoll(CompilationUnit *cUnit, MIR *mir)
1423{
1424 int rTemp = dvmCompilerAllocTemp(cUnit);
1425 ArmLIR *ld;
Ben Cheng20d7e6c2011-02-18 17:12:42 -08001426 ld = loadWordDisp(cUnit, r6SELF, offsetof(Thread, suspendCount),
Ben Cheng7ab74e12011-02-03 14:02:06 -08001427 rTemp);
1428 setMemRefType(ld, true /* isLoad */, kMustNotAlias);
Ben Cheng7ab74e12011-02-03 14:02:06 -08001429 genRegImmCheck(cUnit, kArmCondNe, rTemp, 0, mir->offset, NULL);
1430}
1431
1432/*
Ben Chengba4fc8b2009-06-01 13:00:29 -07001433 * The following are the first-level codegen routines that analyze the format
1434 * of each bytecode then either dispatch special purpose codegen routines
1435 * or produce corresponding Thumb instructions directly.
1436 */
1437
1438static bool handleFmt10t_Fmt20t_Fmt30t(CompilationUnit *cUnit, MIR *mir,
Bill Buzbee89efc3d2009-07-28 11:22:22 -07001439 BasicBlock *bb, ArmLIR *labelList)
Ben Chengba4fc8b2009-06-01 13:00:29 -07001440{
Ben Cheng7ab74e12011-02-03 14:02:06 -08001441 /* backward branch? */
1442 bool backwardBranch = (bb->taken->startOffset <= mir->offset);
1443
1444 if (backwardBranch && gDvmJit.genSuspendPoll) {
1445 genSuspendPoll(cUnit, mir);
1446 }
1447
1448 int numPredecessors = dvmCountSetBits(bb->taken->predecessors);
1449 /*
1450 * Things could be hoisted out of the taken block into the predecessor, so
1451 * make sure it is dominated by the predecessor.
1452 */
1453 if (numPredecessors == 1 && bb->taken->visited == false &&
Ben Cheng20d7e6c2011-02-18 17:12:42 -08001454 bb->taken->blockType == kDalvikByteCode) {
Ben Cheng7ab74e12011-02-03 14:02:06 -08001455 cUnit->nextCodegenBlock = bb->taken;
1456 } else {
1457 /* For OP_GOTO, OP_GOTO_16, and OP_GOTO_32 */
1458 genUnconditionalBranch(cUnit, &labelList[bb->taken->id]);
1459 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07001460 return false;
1461}
1462
1463static bool handleFmt10x(CompilationUnit *cUnit, MIR *mir)
1464{
Dan Bornstein9a1f8162010-12-01 17:02:26 -08001465 Opcode dalvikOpcode = mir->dalvikInsn.opcode;
1466 if ((dalvikOpcode >= OP_UNUSED_3E) && (dalvikOpcode <= OP_UNUSED_43)) {
1467 LOGE("Codegen: got unused opcode 0x%x\n",dalvikOpcode);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001468 return true;
1469 }
Dan Bornstein9a1f8162010-12-01 17:02:26 -08001470 switch (dalvikOpcode) {
Andy McFadden291758c2010-09-10 08:04:52 -07001471 case OP_RETURN_VOID_BARRIER:
buzbee2ce33c92010-11-01 15:53:27 -07001472 dvmCompilerGenMemBarrier(cUnit, kST);
1473 // Intentional fallthrough
1474 case OP_RETURN_VOID:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001475 genReturnCommon(cUnit,mir);
1476 break;
1477 case OP_UNUSED_73:
1478 case OP_UNUSED_79:
1479 case OP_UNUSED_7A:
Dan Bornstein90f15432010-12-02 16:46:25 -08001480 case OP_DISPATCH_FF:
Dan Bornstein9a1f8162010-12-01 17:02:26 -08001481 LOGE("Codegen: got unused opcode 0x%x\n",dalvikOpcode);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001482 return true;
1483 case OP_NOP:
1484 break;
1485 default:
1486 return true;
1487 }
1488 return false;
1489}
1490
1491static bool handleFmt11n_Fmt31i(CompilationUnit *cUnit, MIR *mir)
1492{
Bill Buzbee1465db52009-09-23 17:17:35 -07001493 RegLocation rlDest;
1494 RegLocation rlResult;
1495 if (mir->ssaRep->numDefs == 2) {
Bill Buzbeec6f10662010-02-09 11:16:15 -08001496 rlDest = dvmCompilerGetDestWide(cUnit, mir, 0, 1);
Bill Buzbee1465db52009-09-23 17:17:35 -07001497 } else {
Bill Buzbeec6f10662010-02-09 11:16:15 -08001498 rlDest = dvmCompilerGetDest(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07001499 }
Ben Chenge9695e52009-06-16 16:11:47 -07001500
Dan Bornstein9a1f8162010-12-01 17:02:26 -08001501 switch (mir->dalvikInsn.opcode) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07001502 case OP_CONST:
Ben Chenge9695e52009-06-16 16:11:47 -07001503 case OP_CONST_4: {
Bill Buzbeec6f10662010-02-09 11:16:15 -08001504 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kAnyReg, true);
Ben Chengbd1326d2010-04-02 15:04:53 -07001505 loadConstantNoClobber(cUnit, rlResult.lowReg, mir->dalvikInsn.vB);
Bill Buzbee1465db52009-09-23 17:17:35 -07001506 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001507 break;
Ben Chenge9695e52009-06-16 16:11:47 -07001508 }
1509 case OP_CONST_WIDE_32: {
Bill Buzbee1465db52009-09-23 17:17:35 -07001510 //TUNING: single routine to load constant pair for support doubles
Bill Buzbee964a7b02010-01-28 12:54:19 -08001511 //TUNING: load 0/-1 separately to avoid load dependency
Bill Buzbeec6f10662010-02-09 11:16:15 -08001512 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Ben Chengbd1326d2010-04-02 15:04:53 -07001513 loadConstantNoClobber(cUnit, rlResult.lowReg, mir->dalvikInsn.vB);
Bill Buzbee1465db52009-09-23 17:17:35 -07001514 opRegRegImm(cUnit, kOpAsr, rlResult.highReg,
1515 rlResult.lowReg, 31);
1516 storeValueWide(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001517 break;
Ben Chenge9695e52009-06-16 16:11:47 -07001518 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07001519 default:
1520 return true;
1521 }
1522 return false;
1523}
1524
1525static bool handleFmt21h(CompilationUnit *cUnit, MIR *mir)
1526{
Bill Buzbee1465db52009-09-23 17:17:35 -07001527 RegLocation rlDest;
1528 RegLocation rlResult;
1529 if (mir->ssaRep->numDefs == 2) {
Bill Buzbeec6f10662010-02-09 11:16:15 -08001530 rlDest = dvmCompilerGetDestWide(cUnit, mir, 0, 1);
Bill Buzbee1465db52009-09-23 17:17:35 -07001531 } else {
Bill Buzbeec6f10662010-02-09 11:16:15 -08001532 rlDest = dvmCompilerGetDest(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07001533 }
Bill Buzbeec6f10662010-02-09 11:16:15 -08001534 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kAnyReg, true);
Ben Chenge9695e52009-06-16 16:11:47 -07001535
Dan Bornstein9a1f8162010-12-01 17:02:26 -08001536 switch (mir->dalvikInsn.opcode) {
Ben Chenge9695e52009-06-16 16:11:47 -07001537 case OP_CONST_HIGH16: {
Ben Chengbd1326d2010-04-02 15:04:53 -07001538 loadConstantNoClobber(cUnit, rlResult.lowReg,
1539 mir->dalvikInsn.vB << 16);
Bill Buzbee1465db52009-09-23 17:17:35 -07001540 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001541 break;
Ben Chenge9695e52009-06-16 16:11:47 -07001542 }
1543 case OP_CONST_WIDE_HIGH16: {
Bill Buzbee1465db52009-09-23 17:17:35 -07001544 loadConstantValueWide(cUnit, rlResult.lowReg, rlResult.highReg,
1545 0, mir->dalvikInsn.vB << 16);
1546 storeValueWide(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001547 break;
Ben Chenge9695e52009-06-16 16:11:47 -07001548 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07001549 default:
1550 return true;
1551 }
1552 return false;
1553}
1554
jeffhao71eee1f2011-01-04 14:18:54 -08001555static bool handleFmt20bc_Fmt40sc(CompilationUnit *cUnit, MIR *mir)
Ben Chengba4fc8b2009-06-01 13:00:29 -07001556{
jeffhao71eee1f2011-01-04 14:18:54 -08001557 /* For OP_THROW_VERIFICATION_ERROR & OP_THROW_VERIFICATION_ERROR_JUMBO */
Ben Chengba4fc8b2009-06-01 13:00:29 -07001558 genInterpSingleStep(cUnit, mir);
1559 return false;
1560}
1561
jeffhao71eee1f2011-01-04 14:18:54 -08001562static bool handleFmt21c_Fmt31c_Fmt41c(CompilationUnit *cUnit, MIR *mir)
Ben Chengba4fc8b2009-06-01 13:00:29 -07001563{
Bill Buzbee1465db52009-09-23 17:17:35 -07001564 RegLocation rlResult;
1565 RegLocation rlDest;
1566 RegLocation rlSrc;
Ben Chenge9695e52009-06-16 16:11:47 -07001567
Dan Bornstein9a1f8162010-12-01 17:02:26 -08001568 switch (mir->dalvikInsn.opcode) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07001569 case OP_CONST_STRING_JUMBO:
1570 case OP_CONST_STRING: {
1571 void *strPtr = (void*)
1572 (cUnit->method->clazz->pDvmDex->pResStrings[mir->dalvikInsn.vB]);
Ben Chengdd6e8702010-05-07 13:05:47 -07001573
1574 if (strPtr == NULL) {
1575 LOGE("Unexpected null string");
1576 dvmAbort();
1577 }
1578
Bill Buzbeec6f10662010-02-09 11:16:15 -08001579 rlDest = dvmCompilerGetDest(cUnit, mir, 0);
1580 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Ben Chengbd1326d2010-04-02 15:04:53 -07001581 loadConstantNoClobber(cUnit, rlResult.lowReg, (int) strPtr );
Bill Buzbee1465db52009-09-23 17:17:35 -07001582 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001583 break;
1584 }
jeffhao71eee1f2011-01-04 14:18:54 -08001585 case OP_CONST_CLASS:
1586 case OP_CONST_CLASS_JUMBO: {
Ben Chengba4fc8b2009-06-01 13:00:29 -07001587 void *classPtr = (void*)
1588 (cUnit->method->clazz->pDvmDex->pResClasses[mir->dalvikInsn.vB]);
Ben Chengdd6e8702010-05-07 13:05:47 -07001589
1590 if (classPtr == NULL) {
1591 LOGE("Unexpected null class");
1592 dvmAbort();
1593 }
1594
Bill Buzbeec6f10662010-02-09 11:16:15 -08001595 rlDest = dvmCompilerGetDest(cUnit, mir, 0);
1596 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Ben Chengbd1326d2010-04-02 15:04:53 -07001597 loadConstantNoClobber(cUnit, rlResult.lowReg, (int) classPtr );
Bill Buzbee1465db52009-09-23 17:17:35 -07001598 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001599 break;
1600 }
jeffhao71eee1f2011-01-04 14:18:54 -08001601 case OP_SGET:
buzbeeecf8f6e2010-07-20 14:53:42 -07001602 case OP_SGET_VOLATILE:
jeffhao71eee1f2011-01-04 14:18:54 -08001603 case OP_SGET_JUMBO:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001604 case OP_SGET_OBJECT:
jeffhao71eee1f2011-01-04 14:18:54 -08001605 case OP_SGET_OBJECT_VOLATILE:
1606 case OP_SGET_OBJECT_JUMBO:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001607 case OP_SGET_BOOLEAN:
jeffhao71eee1f2011-01-04 14:18:54 -08001608 case OP_SGET_BOOLEAN_JUMBO:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001609 case OP_SGET_CHAR:
jeffhao71eee1f2011-01-04 14:18:54 -08001610 case OP_SGET_CHAR_JUMBO:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001611 case OP_SGET_BYTE:
jeffhao71eee1f2011-01-04 14:18:54 -08001612 case OP_SGET_BYTE_JUMBO:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001613 case OP_SGET_SHORT:
jeffhao71eee1f2011-01-04 14:18:54 -08001614 case OP_SGET_SHORT_JUMBO: {
Bill Buzbee50a6bf22009-07-08 13:08:04 -07001615 int valOffset = offsetof(StaticField, value);
Bill Buzbeec6f10662010-02-09 11:16:15 -08001616 int tReg = dvmCompilerAllocTemp(cUnit);
buzbeeecf8f6e2010-07-20 14:53:42 -07001617 bool isVolatile;
Ben Cheng7a2697d2010-06-07 13:44:23 -07001618 const Method *method = (mir->OptimizationFlags & MIR_CALLEE) ?
1619 mir->meta.calleeMethod : cUnit->method;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001620 void *fieldPtr = (void*)
Ben Cheng7a2697d2010-06-07 13:44:23 -07001621 (method->clazz->pDvmDex->pResFields[mir->dalvikInsn.vB]);
Ben Chengdd6e8702010-05-07 13:05:47 -07001622
1623 if (fieldPtr == NULL) {
1624 LOGE("Unexpected null static field");
1625 dvmAbort();
1626 }
1627
Dan Bornstein9a1f8162010-12-01 17:02:26 -08001628 isVolatile = (mir->dalvikInsn.opcode == OP_SGET_VOLATILE) ||
1629 (mir->dalvikInsn.opcode == OP_SGET_OBJECT_VOLATILE) ||
Carl Shapirofc75f3e2010-12-07 11:43:38 -08001630 dvmIsVolatileField((Field *) fieldPtr);
buzbeeecf8f6e2010-07-20 14:53:42 -07001631
Bill Buzbeec6f10662010-02-09 11:16:15 -08001632 rlDest = dvmCompilerGetDest(cUnit, mir, 0);
1633 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kAnyReg, true);
Bill Buzbee1465db52009-09-23 17:17:35 -07001634 loadConstant(cUnit, tReg, (int) fieldPtr + valOffset);
Ben Cheng11d8f142010-03-24 15:24:19 -07001635
buzbeeecf8f6e2010-07-20 14:53:42 -07001636 if (isVolatile) {
buzbee2ce33c92010-11-01 15:53:27 -07001637 dvmCompilerGenMemBarrier(cUnit, kSY);
buzbeeecf8f6e2010-07-20 14:53:42 -07001638 }
Ben Cheng11d8f142010-03-24 15:24:19 -07001639 HEAP_ACCESS_SHADOW(true);
Bill Buzbee1465db52009-09-23 17:17:35 -07001640 loadWordDisp(cUnit, tReg, 0, rlResult.lowReg);
Ben Cheng11d8f142010-03-24 15:24:19 -07001641 HEAP_ACCESS_SHADOW(false);
1642
Bill Buzbee1465db52009-09-23 17:17:35 -07001643 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001644 break;
1645 }
jeffhao71eee1f2011-01-04 14:18:54 -08001646 case OP_SGET_WIDE:
1647 case OP_SGET_WIDE_JUMBO: {
Bill Buzbee50a6bf22009-07-08 13:08:04 -07001648 int valOffset = offsetof(StaticField, value);
Ben Cheng7a2697d2010-06-07 13:44:23 -07001649 const Method *method = (mir->OptimizationFlags & MIR_CALLEE) ?
1650 mir->meta.calleeMethod : cUnit->method;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001651 void *fieldPtr = (void*)
Ben Cheng7a2697d2010-06-07 13:44:23 -07001652 (method->clazz->pDvmDex->pResFields[mir->dalvikInsn.vB]);
Ben Chengdd6e8702010-05-07 13:05:47 -07001653
1654 if (fieldPtr == NULL) {
1655 LOGE("Unexpected null static field");
1656 dvmAbort();
1657 }
1658
Bill Buzbeec6f10662010-02-09 11:16:15 -08001659 int tReg = dvmCompilerAllocTemp(cUnit);
Bill Buzbeec6f10662010-02-09 11:16:15 -08001660 rlDest = dvmCompilerGetDestWide(cUnit, mir, 0, 1);
1661 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kAnyReg, true);
Bill Buzbee1465db52009-09-23 17:17:35 -07001662 loadConstant(cUnit, tReg, (int) fieldPtr + valOffset);
Ben Cheng11d8f142010-03-24 15:24:19 -07001663
1664 HEAP_ACCESS_SHADOW(true);
Bill Buzbee1465db52009-09-23 17:17:35 -07001665 loadPair(cUnit, tReg, rlResult.lowReg, rlResult.highReg);
Ben Cheng11d8f142010-03-24 15:24:19 -07001666 HEAP_ACCESS_SHADOW(false);
1667
Bill Buzbee1465db52009-09-23 17:17:35 -07001668 storeValueWide(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001669 break;
1670 }
jeffhao71eee1f2011-01-04 14:18:54 -08001671 case OP_SPUT:
1672 case OP_SPUT_VOLATILE:
1673 case OP_SPUT_JUMBO:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001674 case OP_SPUT_OBJECT:
buzbeeddc7d292010-09-02 17:16:24 -07001675 case OP_SPUT_OBJECT_VOLATILE:
jeffhao71eee1f2011-01-04 14:18:54 -08001676 case OP_SPUT_OBJECT_JUMBO:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001677 case OP_SPUT_BOOLEAN:
jeffhao71eee1f2011-01-04 14:18:54 -08001678 case OP_SPUT_BOOLEAN_JUMBO:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001679 case OP_SPUT_CHAR:
jeffhao71eee1f2011-01-04 14:18:54 -08001680 case OP_SPUT_CHAR_JUMBO:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001681 case OP_SPUT_BYTE:
jeffhao71eee1f2011-01-04 14:18:54 -08001682 case OP_SPUT_BYTE_JUMBO:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001683 case OP_SPUT_SHORT:
jeffhao71eee1f2011-01-04 14:18:54 -08001684 case OP_SPUT_SHORT_JUMBO: {
Bill Buzbee50a6bf22009-07-08 13:08:04 -07001685 int valOffset = offsetof(StaticField, value);
Bill Buzbeec6f10662010-02-09 11:16:15 -08001686 int tReg = dvmCompilerAllocTemp(cUnit);
buzbeed3b0a4b2010-09-27 11:30:22 -07001687 int objHead;
buzbeeecf8f6e2010-07-20 14:53:42 -07001688 bool isVolatile;
buzbeed3b0a4b2010-09-27 11:30:22 -07001689 bool isSputObject;
Ben Cheng7a2697d2010-06-07 13:44:23 -07001690 const Method *method = (mir->OptimizationFlags & MIR_CALLEE) ?
1691 mir->meta.calleeMethod : cUnit->method;
1692 void *fieldPtr = (void*)
1693 (method->clazz->pDvmDex->pResFields[mir->dalvikInsn.vB]);
Ben Chenge9695e52009-06-16 16:11:47 -07001694
Dan Bornstein9a1f8162010-12-01 17:02:26 -08001695 isVolatile = (mir->dalvikInsn.opcode == OP_SPUT_VOLATILE) ||
1696 (mir->dalvikInsn.opcode == OP_SPUT_OBJECT_VOLATILE) ||
Carl Shapirofc75f3e2010-12-07 11:43:38 -08001697 dvmIsVolatileField((Field *) fieldPtr);
buzbeeecf8f6e2010-07-20 14:53:42 -07001698
Dan Bornstein9a1f8162010-12-01 17:02:26 -08001699 isSputObject = (mir->dalvikInsn.opcode == OP_SPUT_OBJECT) ||
jeffhao71eee1f2011-01-04 14:18:54 -08001700 (mir->dalvikInsn.opcode == OP_SPUT_OBJECT_JUMBO) ||
Dan Bornstein9a1f8162010-12-01 17:02:26 -08001701 (mir->dalvikInsn.opcode == OP_SPUT_OBJECT_VOLATILE);
buzbeed3b0a4b2010-09-27 11:30:22 -07001702
Ben Chengdd6e8702010-05-07 13:05:47 -07001703 if (fieldPtr == NULL) {
1704 LOGE("Unexpected null static field");
1705 dvmAbort();
1706 }
1707
Bill Buzbeec6f10662010-02-09 11:16:15 -08001708 rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07001709 rlSrc = loadValue(cUnit, rlSrc, kAnyReg);
buzbeeb78c76f2010-09-30 19:08:20 -07001710 loadConstant(cUnit, tReg, (int) fieldPtr);
buzbeed3b0a4b2010-09-27 11:30:22 -07001711 if (isSputObject) {
1712 objHead = dvmCompilerAllocTemp(cUnit);
buzbeeb78c76f2010-09-30 19:08:20 -07001713 loadWordDisp(cUnit, tReg, offsetof(Field, clazz), objHead);
buzbeed3b0a4b2010-09-27 11:30:22 -07001714 }
Ben Cheng11d8f142010-03-24 15:24:19 -07001715 HEAP_ACCESS_SHADOW(true);
buzbeeb78c76f2010-09-30 19:08:20 -07001716 storeWordDisp(cUnit, tReg, valOffset ,rlSrc.lowReg);
buzbeed3b0a4b2010-09-27 11:30:22 -07001717 dvmCompilerFreeTemp(cUnit, tReg);
Ben Cheng11d8f142010-03-24 15:24:19 -07001718 HEAP_ACCESS_SHADOW(false);
buzbeeecf8f6e2010-07-20 14:53:42 -07001719 if (isVolatile) {
buzbee2ce33c92010-11-01 15:53:27 -07001720 dvmCompilerGenMemBarrier(cUnit, kSY);
buzbeeecf8f6e2010-07-20 14:53:42 -07001721 }
buzbeed3b0a4b2010-09-27 11:30:22 -07001722 if (isSputObject) {
buzbeeb78c76f2010-09-30 19:08:20 -07001723 /* NOTE: marking card based sfield->clazz */
buzbeed3b0a4b2010-09-27 11:30:22 -07001724 markCard(cUnit, rlSrc.lowReg, objHead);
1725 dvmCompilerFreeTemp(cUnit, objHead);
buzbee919eb062010-07-12 12:59:22 -07001726 }
Ben Cheng11d8f142010-03-24 15:24:19 -07001727
Ben Chengba4fc8b2009-06-01 13:00:29 -07001728 break;
1729 }
jeffhao71eee1f2011-01-04 14:18:54 -08001730 case OP_SPUT_WIDE:
1731 case OP_SPUT_WIDE_JUMBO: {
Bill Buzbeec6f10662010-02-09 11:16:15 -08001732 int tReg = dvmCompilerAllocTemp(cUnit);
Bill Buzbee50a6bf22009-07-08 13:08:04 -07001733 int valOffset = offsetof(StaticField, value);
Ben Cheng7a2697d2010-06-07 13:44:23 -07001734 const Method *method = (mir->OptimizationFlags & MIR_CALLEE) ?
1735 mir->meta.calleeMethod : cUnit->method;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001736 void *fieldPtr = (void*)
Ben Cheng7a2697d2010-06-07 13:44:23 -07001737 (method->clazz->pDvmDex->pResFields[mir->dalvikInsn.vB]);
Ben Chenge9695e52009-06-16 16:11:47 -07001738
Ben Chengdd6e8702010-05-07 13:05:47 -07001739 if (fieldPtr == NULL) {
1740 LOGE("Unexpected null static field");
1741 dvmAbort();
1742 }
1743
Bill Buzbeec6f10662010-02-09 11:16:15 -08001744 rlSrc = dvmCompilerGetSrcWide(cUnit, mir, 0, 1);
Bill Buzbee1465db52009-09-23 17:17:35 -07001745 rlSrc = loadValueWide(cUnit, rlSrc, kAnyReg);
1746 loadConstant(cUnit, tReg, (int) fieldPtr + valOffset);
Ben Cheng11d8f142010-03-24 15:24:19 -07001747
1748 HEAP_ACCESS_SHADOW(true);
Bill Buzbee1465db52009-09-23 17:17:35 -07001749 storePair(cUnit, tReg, rlSrc.lowReg, rlSrc.highReg);
Ben Cheng11d8f142010-03-24 15:24:19 -07001750 HEAP_ACCESS_SHADOW(false);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001751 break;
1752 }
jeffhao71eee1f2011-01-04 14:18:54 -08001753 case OP_NEW_INSTANCE:
1754 case OP_NEW_INSTANCE_JUMBO: {
Ben Chenge9695e52009-06-16 16:11:47 -07001755 /*
1756 * Obey the calling convention and don't mess with the register
1757 * usage.
1758 */
Carl Shapirofc75f3e2010-12-07 11:43:38 -08001759 ClassObject *classPtr = (ClassObject *)
Ben Chengba4fc8b2009-06-01 13:00:29 -07001760 (cUnit->method->clazz->pDvmDex->pResClasses[mir->dalvikInsn.vB]);
Ben Chengdd6e8702010-05-07 13:05:47 -07001761
1762 if (classPtr == NULL) {
1763 LOGE("Unexpected null class");
1764 dvmAbort();
1765 }
1766
Ben Cheng79d173c2009-09-29 16:12:51 -07001767 /*
1768 * If it is going to throw, it should not make to the trace to begin
Bill Buzbee1465db52009-09-23 17:17:35 -07001769 * with. However, Alloc might throw, so we need to genExportPC()
Ben Cheng79d173c2009-09-29 16:12:51 -07001770 */
1771 assert((classPtr->accessFlags & (ACC_INTERFACE|ACC_ABSTRACT)) == 0);
Bill Buzbeec6f10662010-02-09 11:16:15 -08001772 dvmCompilerFlushAllRegs(cUnit); /* Everything to home location */
Bill Buzbee1465db52009-09-23 17:17:35 -07001773 genExportPC(cUnit, mir);
Ben Chengbd1326d2010-04-02 15:04:53 -07001774 LOAD_FUNC_ADDR(cUnit, r2, (int)dvmAllocObject);
Ben Chenge9695e52009-06-16 16:11:47 -07001775 loadConstant(cUnit, r0, (int) classPtr);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001776 loadConstant(cUnit, r1, ALLOC_DONT_TRACK);
Bill Buzbee1465db52009-09-23 17:17:35 -07001777 opReg(cUnit, kOpBlx, r2);
Elliott Hughes6a555132010-02-25 15:41:42 -08001778 dvmCompilerClobberCallRegs(cUnit);
Ben Cheng4f489172009-09-27 17:08:35 -07001779 /* generate a branch over if allocation is successful */
buzbee8f8109a2010-08-31 10:16:35 -07001780 ArmLIR *branchOver = genCmpImmBranch(cUnit, kArmCondNe, r0, 0);
Ben Cheng4f489172009-09-27 17:08:35 -07001781 /*
1782 * OOM exception needs to be thrown here and cannot re-execute
1783 */
1784 loadConstant(cUnit, r0,
1785 (int) (cUnit->method->insns + mir->offset));
1786 genDispatchToHandler(cUnit, TEMPLATE_THROW_EXCEPTION_COMMON);
1787 /* noreturn */
1788
Bill Buzbee1465db52009-09-23 17:17:35 -07001789 ArmLIR *target = newLIR0(cUnit, kArmPseudoTargetLabel);
Ben Cheng4f489172009-09-27 17:08:35 -07001790 target->defMask = ENCODE_ALL;
1791 branchOver->generic.target = (LIR *) target;
Bill Buzbeec6f10662010-02-09 11:16:15 -08001792 rlDest = dvmCompilerGetDest(cUnit, mir, 0);
1793 rlResult = dvmCompilerGetReturn(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07001794 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001795 break;
1796 }
jeffhao71eee1f2011-01-04 14:18:54 -08001797 case OP_CHECK_CAST:
1798 case OP_CHECK_CAST_JUMBO: {
Ben Chenge9695e52009-06-16 16:11:47 -07001799 /*
1800 * Obey the calling convention and don't mess with the register
1801 * usage.
1802 */
Ben Chengba4fc8b2009-06-01 13:00:29 -07001803 ClassObject *classPtr =
1804 (cUnit->method->clazz->pDvmDex->pResClasses[mir->dalvikInsn.vB]);
Bill Buzbee4df41a52009-11-12 17:07:16 -08001805 /*
1806 * Note: It is possible that classPtr is NULL at this point,
1807 * even though this instruction has been successfully interpreted.
1808 * If the previous interpretation had a null source, the
1809 * interpreter would not have bothered to resolve the clazz.
1810 * Bail out to the interpreter in this case, and log it
1811 * so that we can tell if it happens frequently.
1812 */
1813 if (classPtr == NULL) {
Ben Cheng11d8f142010-03-24 15:24:19 -07001814 LOGVV("null clazz in OP_CHECK_CAST, single-stepping");
Bill Buzbee4df41a52009-11-12 17:07:16 -08001815 genInterpSingleStep(cUnit, mir);
1816 return false;
1817 }
Bill Buzbeec6f10662010-02-09 11:16:15 -08001818 dvmCompilerFlushAllRegs(cUnit); /* Everything to home location */
Ben Chengba4fc8b2009-06-01 13:00:29 -07001819 loadConstant(cUnit, r1, (int) classPtr );
Bill Buzbeec6f10662010-02-09 11:16:15 -08001820 rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07001821 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
buzbee8f8109a2010-08-31 10:16:35 -07001822 /* Null? */
1823 ArmLIR *branch1 = genCmpImmBranch(cUnit, kArmCondEq,
1824 rlSrc.lowReg, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07001825 /*
1826 * rlSrc.lowReg now contains object->clazz. Note that
1827 * it could have been allocated r0, but we're okay so long
1828 * as we don't do anything desctructive until r0 is loaded
1829 * with clazz.
1830 */
Ben Chengba4fc8b2009-06-01 13:00:29 -07001831 /* r0 now contains object->clazz */
Bill Buzbee1465db52009-09-23 17:17:35 -07001832 loadWordDisp(cUnit, rlSrc.lowReg, offsetof(Object, clazz), r0);
Ben Chengbd1326d2010-04-02 15:04:53 -07001833 LOAD_FUNC_ADDR(cUnit, r2, (int)dvmInstanceofNonTrivial);
Bill Buzbee1465db52009-09-23 17:17:35 -07001834 opRegReg(cUnit, kOpCmp, r0, r1);
1835 ArmLIR *branch2 = opCondBranch(cUnit, kArmCondEq);
1836 opReg(cUnit, kOpBlx, r2);
Elliott Hughes6a555132010-02-25 15:41:42 -08001837 dvmCompilerClobberCallRegs(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07001838 /*
1839 * If null, check cast failed - punt to the interpreter. Because
1840 * interpreter will be the one throwing, we don't need to
1841 * genExportPC() here.
1842 */
Bill Buzbee270c1d62009-08-13 16:58:07 -07001843 genZeroCheck(cUnit, r0, mir->offset, NULL);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001844 /* check cast passed - branch target here */
Bill Buzbee1465db52009-09-23 17:17:35 -07001845 ArmLIR *target = newLIR0(cUnit, kArmPseudoTargetLabel);
Ben Chengd7d426a2009-09-22 11:23:36 -07001846 target->defMask = ENCODE_ALL;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001847 branch1->generic.target = (LIR *)target;
1848 branch2->generic.target = (LIR *)target;
1849 break;
1850 }
buzbee4d92e682010-07-29 15:24:14 -07001851 case OP_SGET_WIDE_VOLATILE:
1852 case OP_SPUT_WIDE_VOLATILE:
1853 genInterpSingleStep(cUnit, mir);
1854 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001855 default:
1856 return true;
1857 }
1858 return false;
1859}
1860
Ben Cheng7a2697d2010-06-07 13:44:23 -07001861/*
1862 * A typical example of inlined getter/setter from a monomorphic callsite:
1863 *
1864 * D/dalvikvm( 289): -------- dalvik offset: 0x0000 @ invoke-static (I)
1865 * D/dalvikvm( 289): -------- dalvik offset: 0x0000 @ sget-object (C) v0, ...
1866 * D/dalvikvm( 289): 0x4427fc22 (0002): ldr r0, [pc, #56]
1867 * D/dalvikvm( 289): 0x4427fc24 (0004): ldr r1, [r0, #0]
1868 * D/dalvikvm( 289): 0x4427fc26 (0006): str r1, [r5, #0]
1869 * D/dalvikvm( 289): 0x4427fc28 (0008): .align4
1870 * D/dalvikvm( 289): L0x0003:
1871 * D/dalvikvm( 289): -------- dalvik offset: 0x0003 @ move-result-object (I) v0
1872 *
1873 * Note the invoke-static and move-result-object with the (I) notation are
1874 * turned into no-op.
1875 */
Ben Chengba4fc8b2009-06-01 13:00:29 -07001876static bool handleFmt11x(CompilationUnit *cUnit, MIR *mir)
1877{
Dan Bornstein9a1f8162010-12-01 17:02:26 -08001878 Opcode dalvikOpcode = mir->dalvikInsn.opcode;
Bill Buzbee1465db52009-09-23 17:17:35 -07001879 RegLocation rlResult;
Dan Bornstein9a1f8162010-12-01 17:02:26 -08001880 switch (dalvikOpcode) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07001881 case OP_MOVE_EXCEPTION: {
Ben Chengba4fc8b2009-06-01 13:00:29 -07001882 int exOffset = offsetof(Thread, exception);
Bill Buzbeec6f10662010-02-09 11:16:15 -08001883 int resetReg = dvmCompilerAllocTemp(cUnit);
1884 RegLocation rlDest = dvmCompilerGetDest(cUnit, mir, 0);
1885 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Ben Cheng20d7e6c2011-02-18 17:12:42 -08001886 loadWordDisp(cUnit, r6SELF, exOffset, rlResult.lowReg);
Bill Buzbeef9f33282009-11-22 12:45:30 -08001887 loadConstant(cUnit, resetReg, 0);
Ben Cheng20d7e6c2011-02-18 17:12:42 -08001888 storeWordDisp(cUnit, r6SELF, exOffset, resetReg);
Bill Buzbee1465db52009-09-23 17:17:35 -07001889 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001890 break;
1891 }
1892 case OP_MOVE_RESULT:
1893 case OP_MOVE_RESULT_OBJECT: {
Ben Cheng7a2697d2010-06-07 13:44:23 -07001894 /* An inlined move result is effectively no-op */
1895 if (mir->OptimizationFlags & MIR_INLINED)
1896 break;
Bill Buzbeec6f10662010-02-09 11:16:15 -08001897 RegLocation rlDest = dvmCompilerGetDest(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07001898 RegLocation rlSrc = LOC_DALVIK_RETURN_VAL;
1899 rlSrc.fp = rlDest.fp;
1900 storeValue(cUnit, rlDest, rlSrc);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001901 break;
1902 }
1903 case OP_MOVE_RESULT_WIDE: {
Ben Cheng7a2697d2010-06-07 13:44:23 -07001904 /* An inlined move result is effectively no-op */
1905 if (mir->OptimizationFlags & MIR_INLINED)
1906 break;
Bill Buzbeec6f10662010-02-09 11:16:15 -08001907 RegLocation rlDest = dvmCompilerGetDestWide(cUnit, mir, 0, 1);
Bill Buzbee1465db52009-09-23 17:17:35 -07001908 RegLocation rlSrc = LOC_DALVIK_RETURN_VAL_WIDE;
1909 rlSrc.fp = rlDest.fp;
1910 storeValueWide(cUnit, rlDest, rlSrc);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001911 break;
1912 }
1913 case OP_RETURN_WIDE: {
Bill Buzbeec6f10662010-02-09 11:16:15 -08001914 RegLocation rlSrc = dvmCompilerGetSrcWide(cUnit, mir, 0, 1);
Bill Buzbee1465db52009-09-23 17:17:35 -07001915 RegLocation rlDest = LOC_DALVIK_RETURN_VAL_WIDE;
1916 rlDest.fp = rlSrc.fp;
1917 storeValueWide(cUnit, rlDest, rlSrc);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001918 genReturnCommon(cUnit,mir);
1919 break;
1920 }
1921 case OP_RETURN:
1922 case OP_RETURN_OBJECT: {
Bill Buzbeec6f10662010-02-09 11:16:15 -08001923 RegLocation rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07001924 RegLocation rlDest = LOC_DALVIK_RETURN_VAL;
1925 rlDest.fp = rlSrc.fp;
1926 storeValue(cUnit, rlDest, rlSrc);
Ben Cheng20d7e6c2011-02-18 17:12:42 -08001927 genReturnCommon(cUnit, mir);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001928 break;
1929 }
Bill Buzbee1465db52009-09-23 17:17:35 -07001930 case OP_MONITOR_EXIT:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001931 case OP_MONITOR_ENTER:
Ben Cheng5d90c202009-11-22 23:31:11 -08001932 genMonitor(cUnit, mir);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001933 break;
Ben Cheng20d7e6c2011-02-18 17:12:42 -08001934 case OP_THROW:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001935 genInterpSingleStep(cUnit, mir);
1936 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001937 default:
1938 return true;
1939 }
1940 return false;
1941}
1942
Bill Buzbeed45ba372009-06-15 17:00:57 -07001943static bool handleFmt12x(CompilationUnit *cUnit, MIR *mir)
1944{
Dan Bornstein9a1f8162010-12-01 17:02:26 -08001945 Opcode opcode = mir->dalvikInsn.opcode;
Bill Buzbee1465db52009-09-23 17:17:35 -07001946 RegLocation rlDest;
1947 RegLocation rlSrc;
1948 RegLocation rlResult;
Bill Buzbeed45ba372009-06-15 17:00:57 -07001949
Dan Bornstein9a1f8162010-12-01 17:02:26 -08001950 if ( (opcode >= OP_ADD_INT_2ADDR) && (opcode <= OP_REM_DOUBLE_2ADDR)) {
Ben Cheng5d90c202009-11-22 23:31:11 -08001951 return genArithOp( cUnit, mir );
Ben Chengba4fc8b2009-06-01 13:00:29 -07001952 }
1953
Bill Buzbee1465db52009-09-23 17:17:35 -07001954 if (mir->ssaRep->numUses == 2)
Bill Buzbeec6f10662010-02-09 11:16:15 -08001955 rlSrc = dvmCompilerGetSrcWide(cUnit, mir, 0, 1);
Bill Buzbee1465db52009-09-23 17:17:35 -07001956 else
Bill Buzbeec6f10662010-02-09 11:16:15 -08001957 rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07001958 if (mir->ssaRep->numDefs == 2)
Bill Buzbeec6f10662010-02-09 11:16:15 -08001959 rlDest = dvmCompilerGetDestWide(cUnit, mir, 0, 1);
Bill Buzbee1465db52009-09-23 17:17:35 -07001960 else
Bill Buzbeec6f10662010-02-09 11:16:15 -08001961 rlDest = dvmCompilerGetDest(cUnit, mir, 0);
Ben Chenge9695e52009-06-16 16:11:47 -07001962
Dan Bornstein9a1f8162010-12-01 17:02:26 -08001963 switch (opcode) {
Bill Buzbee1465db52009-09-23 17:17:35 -07001964 case OP_DOUBLE_TO_INT:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001965 case OP_INT_TO_FLOAT:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001966 case OP_FLOAT_TO_INT:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001967 case OP_DOUBLE_TO_FLOAT:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001968 case OP_FLOAT_TO_DOUBLE:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001969 case OP_INT_TO_DOUBLE:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001970 case OP_FLOAT_TO_LONG:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001971 case OP_LONG_TO_FLOAT:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001972 case OP_DOUBLE_TO_LONG:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001973 case OP_LONG_TO_DOUBLE:
Ben Cheng5d90c202009-11-22 23:31:11 -08001974 return genConversion(cUnit, mir);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001975 case OP_NEG_INT:
1976 case OP_NOT_INT:
Ben Cheng5d90c202009-11-22 23:31:11 -08001977 return genArithOpInt(cUnit, mir, rlDest, rlSrc, rlSrc);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001978 case OP_NEG_LONG:
1979 case OP_NOT_LONG:
Ben Cheng5d90c202009-11-22 23:31:11 -08001980 return genArithOpLong(cUnit, mir, rlDest, rlSrc, rlSrc);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001981 case OP_NEG_FLOAT:
Ben Cheng5d90c202009-11-22 23:31:11 -08001982 return genArithOpFloat(cUnit, mir, rlDest, rlSrc, rlSrc);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001983 case OP_NEG_DOUBLE:
Ben Cheng5d90c202009-11-22 23:31:11 -08001984 return genArithOpDouble(cUnit, mir, rlDest, rlSrc, rlSrc);
Bill Buzbee1465db52009-09-23 17:17:35 -07001985 case OP_MOVE_WIDE:
1986 storeValueWide(cUnit, rlDest, rlSrc);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001987 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07001988 case OP_INT_TO_LONG:
Bill Buzbeec6f10662010-02-09 11:16:15 -08001989 rlSrc = dvmCompilerUpdateLoc(cUnit, rlSrc);
1990 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Bill Buzbee964a7b02010-01-28 12:54:19 -08001991 //TUNING: shouldn't loadValueDirect already check for phys reg?
Bill Buzbee1465db52009-09-23 17:17:35 -07001992 if (rlSrc.location == kLocPhysReg) {
1993 genRegCopy(cUnit, rlResult.lowReg, rlSrc.lowReg);
1994 } else {
1995 loadValueDirect(cUnit, rlSrc, rlResult.lowReg);
1996 }
1997 opRegRegImm(cUnit, kOpAsr, rlResult.highReg,
1998 rlResult.lowReg, 31);
1999 storeValueWide(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002000 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07002001 case OP_LONG_TO_INT:
Bill Buzbeec6f10662010-02-09 11:16:15 -08002002 rlSrc = dvmCompilerUpdateLocWide(cUnit, rlSrc);
2003 rlSrc = dvmCompilerWideToNarrow(cUnit, rlSrc);
Bill Buzbee1465db52009-09-23 17:17:35 -07002004 // Intentional fallthrough
Ben Chengba4fc8b2009-06-01 13:00:29 -07002005 case OP_MOVE:
2006 case OP_MOVE_OBJECT:
Bill Buzbee1465db52009-09-23 17:17:35 -07002007 storeValue(cUnit, rlDest, rlSrc);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002008 break;
2009 case OP_INT_TO_BYTE:
Bill Buzbee1465db52009-09-23 17:17:35 -07002010 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
Bill Buzbeec6f10662010-02-09 11:16:15 -08002011 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Bill Buzbee1465db52009-09-23 17:17:35 -07002012 opRegReg(cUnit, kOp2Byte, rlResult.lowReg, rlSrc.lowReg);
2013 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002014 break;
2015 case OP_INT_TO_SHORT:
Bill Buzbee1465db52009-09-23 17:17:35 -07002016 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
Bill Buzbeec6f10662010-02-09 11:16:15 -08002017 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Bill Buzbee1465db52009-09-23 17:17:35 -07002018 opRegReg(cUnit, kOp2Short, rlResult.lowReg, rlSrc.lowReg);
2019 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002020 break;
2021 case OP_INT_TO_CHAR:
Bill Buzbee1465db52009-09-23 17:17:35 -07002022 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
Bill Buzbeec6f10662010-02-09 11:16:15 -08002023 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Bill Buzbee1465db52009-09-23 17:17:35 -07002024 opRegReg(cUnit, kOp2Char, rlResult.lowReg, rlSrc.lowReg);
2025 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002026 break;
2027 case OP_ARRAY_LENGTH: {
2028 int lenOffset = offsetof(ArrayObject, length);
Bill Buzbee1465db52009-09-23 17:17:35 -07002029 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
2030 genNullCheck(cUnit, rlSrc.sRegLow, rlSrc.lowReg,
2031 mir->offset, NULL);
Bill Buzbeec6f10662010-02-09 11:16:15 -08002032 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Bill Buzbee1465db52009-09-23 17:17:35 -07002033 loadWordDisp(cUnit, rlSrc.lowReg, lenOffset,
2034 rlResult.lowReg);
2035 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002036 break;
2037 }
2038 default:
2039 return true;
2040 }
2041 return false;
2042}
2043
2044static bool handleFmt21s(CompilationUnit *cUnit, MIR *mir)
2045{
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002046 Opcode dalvikOpcode = mir->dalvikInsn.opcode;
Bill Buzbee1465db52009-09-23 17:17:35 -07002047 RegLocation rlDest;
2048 RegLocation rlResult;
2049 int BBBB = mir->dalvikInsn.vB;
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002050 if (dalvikOpcode == OP_CONST_WIDE_16) {
Bill Buzbeec6f10662010-02-09 11:16:15 -08002051 rlDest = dvmCompilerGetDestWide(cUnit, mir, 0, 1);
2052 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Ben Chengbd1326d2010-04-02 15:04:53 -07002053 loadConstantNoClobber(cUnit, rlResult.lowReg, BBBB);
Bill Buzbee964a7b02010-01-28 12:54:19 -08002054 //TUNING: do high separately to avoid load dependency
Bill Buzbee1465db52009-09-23 17:17:35 -07002055 opRegRegImm(cUnit, kOpAsr, rlResult.highReg, rlResult.lowReg, 31);
2056 storeValueWide(cUnit, rlDest, rlResult);
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002057 } else if (dalvikOpcode == OP_CONST_16) {
Bill Buzbeec6f10662010-02-09 11:16:15 -08002058 rlDest = dvmCompilerGetDest(cUnit, mir, 0);
2059 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kAnyReg, true);
Ben Chengbd1326d2010-04-02 15:04:53 -07002060 loadConstantNoClobber(cUnit, rlResult.lowReg, BBBB);
Bill Buzbee1465db52009-09-23 17:17:35 -07002061 storeValue(cUnit, rlDest, rlResult);
2062 } else
Ben Chengba4fc8b2009-06-01 13:00:29 -07002063 return true;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002064 return false;
2065}
2066
2067/* Compare agaist zero */
2068static bool handleFmt21t(CompilationUnit *cUnit, MIR *mir, BasicBlock *bb,
Bill Buzbee89efc3d2009-07-28 11:22:22 -07002069 ArmLIR *labelList)
Ben Chengba4fc8b2009-06-01 13:00:29 -07002070{
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002071 Opcode dalvikOpcode = mir->dalvikInsn.opcode;
Bill Buzbee89efc3d2009-07-28 11:22:22 -07002072 ArmConditionCode cond;
Ben Cheng7ab74e12011-02-03 14:02:06 -08002073 /* backward branch? */
2074 bool backwardBranch = (bb->taken->startOffset <= mir->offset);
2075
2076 if (backwardBranch && gDvmJit.genSuspendPoll) {
2077 genSuspendPoll(cUnit, mir);
2078 }
2079
Bill Buzbeec6f10662010-02-09 11:16:15 -08002080 RegLocation rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07002081 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
Ben Cheng7ab74e12011-02-03 14:02:06 -08002082
Bill Buzbee1465db52009-09-23 17:17:35 -07002083 opRegImm(cUnit, kOpCmp, rlSrc.lowReg, 0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002084
Bill Buzbee270c1d62009-08-13 16:58:07 -07002085//TUNING: break this out to allow use of Thumb2 CB[N]Z
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002086 switch (dalvikOpcode) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07002087 case OP_IF_EQZ:
Bill Buzbee1465db52009-09-23 17:17:35 -07002088 cond = kArmCondEq;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002089 break;
2090 case OP_IF_NEZ:
Bill Buzbee1465db52009-09-23 17:17:35 -07002091 cond = kArmCondNe;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002092 break;
2093 case OP_IF_LTZ:
Bill Buzbee1465db52009-09-23 17:17:35 -07002094 cond = kArmCondLt;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002095 break;
2096 case OP_IF_GEZ:
Bill Buzbee1465db52009-09-23 17:17:35 -07002097 cond = kArmCondGe;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002098 break;
2099 case OP_IF_GTZ:
Bill Buzbee1465db52009-09-23 17:17:35 -07002100 cond = kArmCondGt;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002101 break;
2102 case OP_IF_LEZ:
Bill Buzbee1465db52009-09-23 17:17:35 -07002103 cond = kArmCondLe;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002104 break;
2105 default:
2106 cond = 0;
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002107 LOGE("Unexpected opcode (%d) for Fmt21t\n", dalvikOpcode);
Bill Buzbeefc519dc2010-03-06 23:30:57 -08002108 dvmCompilerAbort(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002109 }
2110 genConditionalBranch(cUnit, cond, &labelList[bb->taken->id]);
2111 /* This mostly likely will be optimized away in a later phase */
2112 genUnconditionalBranch(cUnit, &labelList[bb->fallThrough->id]);
2113 return false;
2114}
2115
Elliott Hughesb4c05972010-02-24 16:36:18 -08002116static bool isPowerOfTwo(int x)
2117{
2118 return (x & (x - 1)) == 0;
2119}
2120
2121// Returns true if no more than two bits are set in 'x'.
2122static bool isPopCountLE2(unsigned int x)
2123{
2124 x &= x - 1;
2125 return (x & (x - 1)) == 0;
2126}
2127
2128// Returns the index of the lowest set bit in 'x'.
2129static int lowestSetBit(unsigned int x) {
2130 int bit_posn = 0;
2131 while ((x & 0xf) == 0) {
2132 bit_posn += 4;
2133 x >>= 4;
Bill Buzbee78cb0e22010-02-11 14:04:53 -08002134 }
Elliott Hughesb4c05972010-02-24 16:36:18 -08002135 while ((x & 1) == 0) {
2136 bit_posn++;
2137 x >>= 1;
2138 }
2139 return bit_posn;
2140}
2141
Elliott Hughes672511b2010-04-26 17:40:13 -07002142// Returns true if it added instructions to 'cUnit' to divide 'rlSrc' by 'lit'
2143// and store the result in 'rlDest'.
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002144static bool handleEasyDivide(CompilationUnit *cUnit, Opcode dalvikOpcode,
Elliott Hughes672511b2010-04-26 17:40:13 -07002145 RegLocation rlSrc, RegLocation rlDest, int lit)
2146{
2147 if (lit < 2 || !isPowerOfTwo(lit)) {
2148 return false;
2149 }
2150 int k = lowestSetBit(lit);
2151 if (k >= 30) {
2152 // Avoid special cases.
2153 return false;
2154 }
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002155 bool div = (dalvikOpcode == OP_DIV_INT_LIT8 || dalvikOpcode == OP_DIV_INT_LIT16);
Elliott Hughes672511b2010-04-26 17:40:13 -07002156 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
2157 RegLocation rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Elliott Hughes9c457022010-04-28 16:15:38 -07002158 if (div) {
2159 int tReg = dvmCompilerAllocTemp(cUnit);
2160 if (lit == 2) {
2161 // Division by 2 is by far the most common division by constant.
2162 opRegRegImm(cUnit, kOpLsr, tReg, rlSrc.lowReg, 32 - k);
2163 opRegRegReg(cUnit, kOpAdd, tReg, tReg, rlSrc.lowReg);
2164 opRegRegImm(cUnit, kOpAsr, rlResult.lowReg, tReg, k);
2165 } else {
2166 opRegRegImm(cUnit, kOpAsr, tReg, rlSrc.lowReg, 31);
2167 opRegRegImm(cUnit, kOpLsr, tReg, tReg, 32 - k);
2168 opRegRegReg(cUnit, kOpAdd, tReg, tReg, rlSrc.lowReg);
2169 opRegRegImm(cUnit, kOpAsr, rlResult.lowReg, tReg, k);
2170 }
Elliott Hughes672511b2010-04-26 17:40:13 -07002171 } else {
Elliott Hughes9c457022010-04-28 16:15:38 -07002172 int cReg = dvmCompilerAllocTemp(cUnit);
2173 loadConstant(cUnit, cReg, lit - 1);
2174 int tReg1 = dvmCompilerAllocTemp(cUnit);
2175 int tReg2 = dvmCompilerAllocTemp(cUnit);
2176 if (lit == 2) {
2177 opRegRegImm(cUnit, kOpLsr, tReg1, rlSrc.lowReg, 32 - k);
2178 opRegRegReg(cUnit, kOpAdd, tReg2, tReg1, rlSrc.lowReg);
2179 opRegRegReg(cUnit, kOpAnd, tReg2, tReg2, cReg);
2180 opRegRegReg(cUnit, kOpSub, rlResult.lowReg, tReg2, tReg1);
2181 } else {
2182 opRegRegImm(cUnit, kOpAsr, tReg1, rlSrc.lowReg, 31);
2183 opRegRegImm(cUnit, kOpLsr, tReg1, tReg1, 32 - k);
2184 opRegRegReg(cUnit, kOpAdd, tReg2, tReg1, rlSrc.lowReg);
2185 opRegRegReg(cUnit, kOpAnd, tReg2, tReg2, cReg);
2186 opRegRegReg(cUnit, kOpSub, rlResult.lowReg, tReg2, tReg1);
2187 }
Elliott Hughes672511b2010-04-26 17:40:13 -07002188 }
2189 storeValue(cUnit, rlDest, rlResult);
2190 return true;
2191}
2192
Elliott Hughesb4c05972010-02-24 16:36:18 -08002193// Returns true if it added instructions to 'cUnit' to multiply 'rlSrc' by 'lit'
2194// and store the result in 'rlDest'.
2195static bool handleEasyMultiply(CompilationUnit *cUnit,
2196 RegLocation rlSrc, RegLocation rlDest, int lit)
2197{
2198 // Can we simplify this multiplication?
2199 bool powerOfTwo = false;
2200 bool popCountLE2 = false;
2201 bool powerOfTwoMinusOne = false;
2202 if (lit < 2) {
2203 // Avoid special cases.
2204 return false;
2205 } else if (isPowerOfTwo(lit)) {
2206 powerOfTwo = true;
2207 } else if (isPopCountLE2(lit)) {
2208 popCountLE2 = true;
2209 } else if (isPowerOfTwo(lit + 1)) {
2210 powerOfTwoMinusOne = true;
2211 } else {
2212 return false;
2213 }
2214 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
2215 RegLocation rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
2216 if (powerOfTwo) {
2217 // Shift.
2218 opRegRegImm(cUnit, kOpLsl, rlResult.lowReg, rlSrc.lowReg,
2219 lowestSetBit(lit));
2220 } else if (popCountLE2) {
2221 // Shift and add and shift.
2222 int firstBit = lowestSetBit(lit);
2223 int secondBit = lowestSetBit(lit ^ (1 << firstBit));
2224 genMultiplyByTwoBitMultiplier(cUnit, rlSrc, rlResult, lit,
2225 firstBit, secondBit);
2226 } else {
2227 // Reverse subtract: (src << (shift + 1)) - src.
2228 assert(powerOfTwoMinusOne);
2229 // TODO: rsb dst, src, src lsl#lowestSetBit(lit + 1)
2230 int tReg = dvmCompilerAllocTemp(cUnit);
2231 opRegRegImm(cUnit, kOpLsl, tReg, rlSrc.lowReg, lowestSetBit(lit + 1));
2232 opRegRegReg(cUnit, kOpSub, rlResult.lowReg, tReg, rlSrc.lowReg);
2233 }
2234 storeValue(cUnit, rlDest, rlResult);
2235 return true;
Bill Buzbee78cb0e22010-02-11 14:04:53 -08002236}
2237
Ben Chengba4fc8b2009-06-01 13:00:29 -07002238static bool handleFmt22b_Fmt22s(CompilationUnit *cUnit, MIR *mir)
2239{
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002240 Opcode dalvikOpcode = mir->dalvikInsn.opcode;
Bill Buzbeec6f10662010-02-09 11:16:15 -08002241 RegLocation rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
2242 RegLocation rlDest = dvmCompilerGetDest(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07002243 RegLocation rlResult;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002244 int lit = mir->dalvikInsn.vC;
Ben Cheng4f489172009-09-27 17:08:35 -07002245 OpKind op = 0; /* Make gcc happy */
Bill Buzbee1465db52009-09-23 17:17:35 -07002246 int shiftOp = false;
2247 bool isDiv = false;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002248
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002249 switch (dalvikOpcode) {
Bill Buzbee1465db52009-09-23 17:17:35 -07002250 case OP_RSUB_INT_LIT8:
2251 case OP_RSUB_INT: {
2252 int tReg;
2253 //TUNING: add support for use of Arm rsub op
2254 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
Bill Buzbeec6f10662010-02-09 11:16:15 -08002255 tReg = dvmCompilerAllocTemp(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07002256 loadConstant(cUnit, tReg, lit);
Bill Buzbeec6f10662010-02-09 11:16:15 -08002257 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Bill Buzbee1465db52009-09-23 17:17:35 -07002258 opRegRegReg(cUnit, kOpSub, rlResult.lowReg,
2259 tReg, rlSrc.lowReg);
2260 storeValue(cUnit, rlDest, rlResult);
2261 return false;
2262 break;
2263 }
2264
Ben Chengba4fc8b2009-06-01 13:00:29 -07002265 case OP_ADD_INT_LIT8:
2266 case OP_ADD_INT_LIT16:
Bill Buzbee1465db52009-09-23 17:17:35 -07002267 op = kOpAdd;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002268 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002269 case OP_MUL_INT_LIT8:
Bill Buzbee78cb0e22010-02-11 14:04:53 -08002270 case OP_MUL_INT_LIT16: {
Elliott Hughesb4c05972010-02-24 16:36:18 -08002271 if (handleEasyMultiply(cUnit, rlSrc, rlDest, lit)) {
2272 return false;
Bill Buzbee78cb0e22010-02-11 14:04:53 -08002273 }
Elliott Hughesb4c05972010-02-24 16:36:18 -08002274 op = kOpMul;
Bill Buzbee1465db52009-09-23 17:17:35 -07002275 break;
Bill Buzbee78cb0e22010-02-11 14:04:53 -08002276 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07002277 case OP_AND_INT_LIT8:
2278 case OP_AND_INT_LIT16:
Bill Buzbee1465db52009-09-23 17:17:35 -07002279 op = kOpAnd;
2280 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002281 case OP_OR_INT_LIT8:
2282 case OP_OR_INT_LIT16:
Bill Buzbee1465db52009-09-23 17:17:35 -07002283 op = kOpOr;
2284 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002285 case OP_XOR_INT_LIT8:
2286 case OP_XOR_INT_LIT16:
Bill Buzbee1465db52009-09-23 17:17:35 -07002287 op = kOpXor;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002288 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002289 case OP_SHL_INT_LIT8:
Bill Buzbee0e605272009-12-01 14:28:05 -08002290 lit &= 31;
Bill Buzbee1465db52009-09-23 17:17:35 -07002291 shiftOp = true;
2292 op = kOpLsl;
2293 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002294 case OP_SHR_INT_LIT8:
Bill Buzbee0e605272009-12-01 14:28:05 -08002295 lit &= 31;
Bill Buzbee1465db52009-09-23 17:17:35 -07002296 shiftOp = true;
2297 op = kOpAsr;
2298 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002299 case OP_USHR_INT_LIT8:
Bill Buzbee0e605272009-12-01 14:28:05 -08002300 lit &= 31;
Bill Buzbee1465db52009-09-23 17:17:35 -07002301 shiftOp = true;
2302 op = kOpLsr;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002303 break;
2304
2305 case OP_DIV_INT_LIT8:
2306 case OP_DIV_INT_LIT16:
Ben Chengba4fc8b2009-06-01 13:00:29 -07002307 case OP_REM_INT_LIT8:
2308 case OP_REM_INT_LIT16:
2309 if (lit == 0) {
2310 /* Let the interpreter deal with div by 0 */
2311 genInterpSingleStep(cUnit, mir);
2312 return false;
2313 }
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002314 if (handleEasyDivide(cUnit, dalvikOpcode, rlSrc, rlDest, lit)) {
Elliott Hughes672511b2010-04-26 17:40:13 -07002315 return false;
2316 }
Bill Buzbeec6f10662010-02-09 11:16:15 -08002317 dvmCompilerFlushAllRegs(cUnit); /* Everything to home location */
Bill Buzbee1465db52009-09-23 17:17:35 -07002318 loadValueDirectFixed(cUnit, rlSrc, r0);
Bill Buzbeec6f10662010-02-09 11:16:15 -08002319 dvmCompilerClobber(cUnit, r0);
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002320 if ((dalvikOpcode == OP_DIV_INT_LIT8) ||
2321 (dalvikOpcode == OP_DIV_INT_LIT16)) {
Ben Chengbd1326d2010-04-02 15:04:53 -07002322 LOAD_FUNC_ADDR(cUnit, r2, (int)__aeabi_idiv);
Bill Buzbee1465db52009-09-23 17:17:35 -07002323 isDiv = true;
2324 } else {
Ben Chengbd1326d2010-04-02 15:04:53 -07002325 LOAD_FUNC_ADDR(cUnit, r2, (int)__aeabi_idivmod);
Bill Buzbee1465db52009-09-23 17:17:35 -07002326 isDiv = false;
2327 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07002328 loadConstant(cUnit, r1, lit);
Bill Buzbee1465db52009-09-23 17:17:35 -07002329 opReg(cUnit, kOpBlx, r2);
Elliott Hughes6a555132010-02-25 15:41:42 -08002330 dvmCompilerClobberCallRegs(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07002331 if (isDiv)
Bill Buzbeec6f10662010-02-09 11:16:15 -08002332 rlResult = dvmCompilerGetReturn(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07002333 else
Bill Buzbeec6f10662010-02-09 11:16:15 -08002334 rlResult = dvmCompilerGetReturnAlt(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07002335 storeValue(cUnit, rlDest, rlResult);
2336 return false;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002337 break;
2338 default:
2339 return true;
2340 }
Bill Buzbee1465db52009-09-23 17:17:35 -07002341 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
Bill Buzbeec6f10662010-02-09 11:16:15 -08002342 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Bill Buzbee1465db52009-09-23 17:17:35 -07002343 // Avoid shifts by literal 0 - no support in Thumb. Change to copy
2344 if (shiftOp && (lit == 0)) {
2345 genRegCopy(cUnit, rlResult.lowReg, rlSrc.lowReg);
2346 } else {
2347 opRegRegImm(cUnit, op, rlResult.lowReg, rlSrc.lowReg, lit);
2348 }
2349 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002350 return false;
2351}
2352
jeffhao71eee1f2011-01-04 14:18:54 -08002353static bool handleFmt22c_Fmt52c(CompilationUnit *cUnit, MIR *mir)
Ben Chengba4fc8b2009-06-01 13:00:29 -07002354{
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002355 Opcode dalvikOpcode = mir->dalvikInsn.opcode;
buzbee4d92e682010-07-29 15:24:14 -07002356 int fieldOffset = -1;
buzbeeecf8f6e2010-07-20 14:53:42 -07002357 bool isVolatile = false;
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002358 switch (dalvikOpcode) {
buzbee4d92e682010-07-29 15:24:14 -07002359 /*
2360 * Wide volatiles currently handled via single step.
2361 * Add them here if generating in-line code.
2362 * case OP_IGET_WIDE_VOLATILE:
2363 * case OP_IPUT_WIDE_VOLATILE:
2364 */
2365 case OP_IGET:
2366 case OP_IGET_VOLATILE:
jeffhao71eee1f2011-01-04 14:18:54 -08002367 case OP_IGET_JUMBO:
buzbee4d92e682010-07-29 15:24:14 -07002368 case OP_IGET_WIDE:
jeffhao71eee1f2011-01-04 14:18:54 -08002369 case OP_IGET_WIDE_JUMBO:
buzbee4d92e682010-07-29 15:24:14 -07002370 case OP_IGET_OBJECT:
2371 case OP_IGET_OBJECT_VOLATILE:
jeffhao71eee1f2011-01-04 14:18:54 -08002372 case OP_IGET_OBJECT_JUMBO:
buzbee4d92e682010-07-29 15:24:14 -07002373 case OP_IGET_BOOLEAN:
jeffhao71eee1f2011-01-04 14:18:54 -08002374 case OP_IGET_BOOLEAN_JUMBO:
buzbee4d92e682010-07-29 15:24:14 -07002375 case OP_IGET_BYTE:
jeffhao71eee1f2011-01-04 14:18:54 -08002376 case OP_IGET_BYTE_JUMBO:
buzbee4d92e682010-07-29 15:24:14 -07002377 case OP_IGET_CHAR:
jeffhao71eee1f2011-01-04 14:18:54 -08002378 case OP_IGET_CHAR_JUMBO:
buzbee4d92e682010-07-29 15:24:14 -07002379 case OP_IGET_SHORT:
jeffhao71eee1f2011-01-04 14:18:54 -08002380 case OP_IGET_SHORT_JUMBO:
buzbee4d92e682010-07-29 15:24:14 -07002381 case OP_IPUT:
2382 case OP_IPUT_VOLATILE:
jeffhao71eee1f2011-01-04 14:18:54 -08002383 case OP_IPUT_JUMBO:
buzbee4d92e682010-07-29 15:24:14 -07002384 case OP_IPUT_WIDE:
jeffhao71eee1f2011-01-04 14:18:54 -08002385 case OP_IPUT_WIDE_JUMBO:
buzbee4d92e682010-07-29 15:24:14 -07002386 case OP_IPUT_OBJECT:
2387 case OP_IPUT_OBJECT_VOLATILE:
jeffhao71eee1f2011-01-04 14:18:54 -08002388 case OP_IPUT_OBJECT_JUMBO:
buzbee4d92e682010-07-29 15:24:14 -07002389 case OP_IPUT_BOOLEAN:
jeffhao71eee1f2011-01-04 14:18:54 -08002390 case OP_IPUT_BOOLEAN_JUMBO:
buzbee4d92e682010-07-29 15:24:14 -07002391 case OP_IPUT_BYTE:
jeffhao71eee1f2011-01-04 14:18:54 -08002392 case OP_IPUT_BYTE_JUMBO:
buzbee4d92e682010-07-29 15:24:14 -07002393 case OP_IPUT_CHAR:
jeffhao71eee1f2011-01-04 14:18:54 -08002394 case OP_IPUT_CHAR_JUMBO:
2395 case OP_IPUT_SHORT:
2396 case OP_IPUT_SHORT_JUMBO: {
Ben Cheng7a2697d2010-06-07 13:44:23 -07002397 const Method *method = (mir->OptimizationFlags & MIR_CALLEE) ?
2398 mir->meta.calleeMethod : cUnit->method;
buzbee4d92e682010-07-29 15:24:14 -07002399 Field *fieldPtr =
Ben Cheng7a2697d2010-06-07 13:44:23 -07002400 method->clazz->pDvmDex->pResFields[mir->dalvikInsn.vC];
Ben Chengba4fc8b2009-06-01 13:00:29 -07002401
buzbee4d92e682010-07-29 15:24:14 -07002402 if (fieldPtr == NULL) {
2403 LOGE("Unexpected null instance field");
2404 dvmAbort();
2405 }
2406 isVolatile = dvmIsVolatileField(fieldPtr);
2407 fieldOffset = ((InstField *)fieldPtr)->byteOffset;
2408 break;
Ben Chengdd6e8702010-05-07 13:05:47 -07002409 }
buzbee4d92e682010-07-29 15:24:14 -07002410 default:
2411 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002412 }
buzbee4d92e682010-07-29 15:24:14 -07002413
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002414 switch (dalvikOpcode) {
jeffhao71eee1f2011-01-04 14:18:54 -08002415 case OP_NEW_ARRAY:
2416 case OP_NEW_ARRAY_JUMBO: {
Bill Buzbee1465db52009-09-23 17:17:35 -07002417 // Generates a call - use explicit registers
Bill Buzbeec6f10662010-02-09 11:16:15 -08002418 RegLocation rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
2419 RegLocation rlDest = dvmCompilerGetDest(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07002420 RegLocation rlResult;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002421 void *classPtr = (void*)
2422 (cUnit->method->clazz->pDvmDex->pResClasses[mir->dalvikInsn.vC]);
Ben Chengdd6e8702010-05-07 13:05:47 -07002423
2424 if (classPtr == NULL) {
2425 LOGE("Unexpected null class");
2426 dvmAbort();
2427 }
2428
Bill Buzbeec6f10662010-02-09 11:16:15 -08002429 dvmCompilerFlushAllRegs(cUnit); /* Everything to home location */
Bill Buzbee1465db52009-09-23 17:17:35 -07002430 genExportPC(cUnit, mir);
2431 loadValueDirectFixed(cUnit, rlSrc, r1); /* Len */
Ben Chengba4fc8b2009-06-01 13:00:29 -07002432 loadConstant(cUnit, r0, (int) classPtr );
Ben Chengbd1326d2010-04-02 15:04:53 -07002433 LOAD_FUNC_ADDR(cUnit, r3, (int)dvmAllocArrayByClass);
Ben Cheng4f489172009-09-27 17:08:35 -07002434 /*
2435 * "len < 0": bail to the interpreter to re-execute the
2436 * instruction
2437 */
Carl Shapiroe3c01da2010-05-20 22:54:18 -07002438 genRegImmCheck(cUnit, kArmCondMi, r1, 0, mir->offset, NULL);
Bill Buzbee270c1d62009-08-13 16:58:07 -07002439 loadConstant(cUnit, r2, ALLOC_DONT_TRACK);
Bill Buzbee1465db52009-09-23 17:17:35 -07002440 opReg(cUnit, kOpBlx, r3);
Elliott Hughes6a555132010-02-25 15:41:42 -08002441 dvmCompilerClobberCallRegs(cUnit);
Ben Cheng4f489172009-09-27 17:08:35 -07002442 /* generate a branch over if allocation is successful */
buzbee8f8109a2010-08-31 10:16:35 -07002443 ArmLIR *branchOver = genCmpImmBranch(cUnit, kArmCondNe, r0, 0);
Ben Cheng4f489172009-09-27 17:08:35 -07002444 /*
2445 * OOM exception needs to be thrown here and cannot re-execute
2446 */
2447 loadConstant(cUnit, r0,
2448 (int) (cUnit->method->insns + mir->offset));
2449 genDispatchToHandler(cUnit, TEMPLATE_THROW_EXCEPTION_COMMON);
2450 /* noreturn */
2451
Bill Buzbee1465db52009-09-23 17:17:35 -07002452 ArmLIR *target = newLIR0(cUnit, kArmPseudoTargetLabel);
Ben Cheng4f489172009-09-27 17:08:35 -07002453 target->defMask = ENCODE_ALL;
2454 branchOver->generic.target = (LIR *) target;
Bill Buzbeec6f10662010-02-09 11:16:15 -08002455 rlResult = dvmCompilerGetReturn(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07002456 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002457 break;
2458 }
jeffhao71eee1f2011-01-04 14:18:54 -08002459 case OP_INSTANCE_OF:
2460 case OP_INSTANCE_OF_JUMBO: {
Bill Buzbee1465db52009-09-23 17:17:35 -07002461 // May generate a call - use explicit registers
Bill Buzbeec6f10662010-02-09 11:16:15 -08002462 RegLocation rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
2463 RegLocation rlDest = dvmCompilerGetDest(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07002464 RegLocation rlResult;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002465 ClassObject *classPtr =
2466 (cUnit->method->clazz->pDvmDex->pResClasses[mir->dalvikInsn.vC]);
Bill Buzbee480e6782010-01-27 15:43:08 -08002467 /*
2468 * Note: It is possible that classPtr is NULL at this point,
2469 * even though this instruction has been successfully interpreted.
2470 * If the previous interpretation had a null source, the
2471 * interpreter would not have bothered to resolve the clazz.
2472 * Bail out to the interpreter in this case, and log it
2473 * so that we can tell if it happens frequently.
2474 */
2475 if (classPtr == NULL) {
2476 LOGD("null clazz in OP_INSTANCE_OF, single-stepping");
2477 genInterpSingleStep(cUnit, mir);
2478 break;
2479 }
Bill Buzbeec6f10662010-02-09 11:16:15 -08002480 dvmCompilerFlushAllRegs(cUnit); /* Everything to home location */
Bill Buzbee1465db52009-09-23 17:17:35 -07002481 loadValueDirectFixed(cUnit, rlSrc, r0); /* Ref */
Ben Chengba4fc8b2009-06-01 13:00:29 -07002482 loadConstant(cUnit, r2, (int) classPtr );
Ben Cheng752c7942009-06-22 10:50:07 -07002483 /* When taken r0 has NULL which can be used for store directly */
buzbee8f8109a2010-08-31 10:16:35 -07002484 ArmLIR *branch1 = genCmpImmBranch(cUnit, kArmCondEq, r0, 0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002485 /* r1 now contains object->clazz */
Bill Buzbee270c1d62009-08-13 16:58:07 -07002486 loadWordDisp(cUnit, r0, offsetof(Object, clazz), r1);
Bill Buzbee1465db52009-09-23 17:17:35 -07002487 /* r1 now contains object->clazz */
Ben Chengbd1326d2010-04-02 15:04:53 -07002488 LOAD_FUNC_ADDR(cUnit, r3, (int)dvmInstanceofNonTrivial);
Ben Cheng752c7942009-06-22 10:50:07 -07002489 loadConstant(cUnit, r0, 1); /* Assume true */
Bill Buzbee1465db52009-09-23 17:17:35 -07002490 opRegReg(cUnit, kOpCmp, r1, r2);
2491 ArmLIR *branch2 = opCondBranch(cUnit, kArmCondEq);
2492 genRegCopy(cUnit, r0, r1);
2493 genRegCopy(cUnit, r1, r2);
2494 opReg(cUnit, kOpBlx, r3);
Elliott Hughes6a555132010-02-25 15:41:42 -08002495 dvmCompilerClobberCallRegs(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002496 /* branch target here */
Bill Buzbee1465db52009-09-23 17:17:35 -07002497 ArmLIR *target = newLIR0(cUnit, kArmPseudoTargetLabel);
Ben Chengd7d426a2009-09-22 11:23:36 -07002498 target->defMask = ENCODE_ALL;
Bill Buzbeec6f10662010-02-09 11:16:15 -08002499 rlResult = dvmCompilerGetReturn(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07002500 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002501 branch1->generic.target = (LIR *)target;
2502 branch2->generic.target = (LIR *)target;
2503 break;
2504 }
2505 case OP_IGET_WIDE:
jeffhao71eee1f2011-01-04 14:18:54 -08002506 case OP_IGET_WIDE_JUMBO:
Ben Chengba4fc8b2009-06-01 13:00:29 -07002507 genIGetWide(cUnit, mir, fieldOffset);
2508 break;
buzbeeecf8f6e2010-07-20 14:53:42 -07002509 case OP_IGET_VOLATILE:
2510 case OP_IGET_OBJECT_VOLATILE:
2511 isVolatile = true;
2512 // NOTE: intentional fallthrough
Ben Chengba4fc8b2009-06-01 13:00:29 -07002513 case OP_IGET:
jeffhao71eee1f2011-01-04 14:18:54 -08002514 case OP_IGET_JUMBO:
Ben Chengba4fc8b2009-06-01 13:00:29 -07002515 case OP_IGET_OBJECT:
jeffhao71eee1f2011-01-04 14:18:54 -08002516 case OP_IGET_OBJECT_JUMBO:
Ben Chengba4fc8b2009-06-01 13:00:29 -07002517 case OP_IGET_BOOLEAN:
jeffhao71eee1f2011-01-04 14:18:54 -08002518 case OP_IGET_BOOLEAN_JUMBO:
Ben Chengba4fc8b2009-06-01 13:00:29 -07002519 case OP_IGET_BYTE:
jeffhao71eee1f2011-01-04 14:18:54 -08002520 case OP_IGET_BYTE_JUMBO:
Ben Chengba4fc8b2009-06-01 13:00:29 -07002521 case OP_IGET_CHAR:
jeffhao71eee1f2011-01-04 14:18:54 -08002522 case OP_IGET_CHAR_JUMBO:
Ben Chengba4fc8b2009-06-01 13:00:29 -07002523 case OP_IGET_SHORT:
jeffhao71eee1f2011-01-04 14:18:54 -08002524 case OP_IGET_SHORT_JUMBO:
buzbee3272e2f2010-09-09 14:07:01 -07002525 genIGet(cUnit, mir, kWord, fieldOffset, isVolatile);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002526 break;
2527 case OP_IPUT_WIDE:
jeffhao71eee1f2011-01-04 14:18:54 -08002528 case OP_IPUT_WIDE_JUMBO:
Ben Chengba4fc8b2009-06-01 13:00:29 -07002529 genIPutWide(cUnit, mir, fieldOffset);
2530 break;
2531 case OP_IPUT:
jeffhao71eee1f2011-01-04 14:18:54 -08002532 case OP_IPUT_JUMBO:
buzbee3272e2f2010-09-09 14:07:01 -07002533 case OP_IPUT_BOOLEAN:
jeffhao71eee1f2011-01-04 14:18:54 -08002534 case OP_IPUT_BOOLEAN_JUMBO:
2535 case OP_IPUT_BYTE:
2536 case OP_IPUT_BYTE_JUMBO:
2537 case OP_IPUT_CHAR:
2538 case OP_IPUT_CHAR_JUMBO:
2539 case OP_IPUT_SHORT:
2540 case OP_IPUT_SHORT_JUMBO:
buzbeeecf8f6e2010-07-20 14:53:42 -07002541 genIPut(cUnit, mir, kWord, fieldOffset, false, isVolatile);
buzbee919eb062010-07-12 12:59:22 -07002542 break;
buzbee4d92e682010-07-29 15:24:14 -07002543 case OP_IPUT_VOLATILE:
buzbeeecf8f6e2010-07-20 14:53:42 -07002544 case OP_IPUT_OBJECT_VOLATILE:
2545 isVolatile = true;
2546 // NOTE: intentional fallthrough
Ben Chengba4fc8b2009-06-01 13:00:29 -07002547 case OP_IPUT_OBJECT:
jeffhao71eee1f2011-01-04 14:18:54 -08002548 case OP_IPUT_OBJECT_JUMBO:
buzbeeecf8f6e2010-07-20 14:53:42 -07002549 genIPut(cUnit, mir, kWord, fieldOffset, true, isVolatile);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002550 break;
Bill Buzbeeb16344a2010-03-15 17:19:12 -07002551 case OP_IGET_WIDE_VOLATILE:
2552 case OP_IPUT_WIDE_VOLATILE:
Bill Buzbeeb16344a2010-03-15 17:19:12 -07002553 genInterpSingleStep(cUnit, mir);
2554 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002555 default:
2556 return true;
2557 }
2558 return false;
2559}
2560
2561static bool handleFmt22cs(CompilationUnit *cUnit, MIR *mir)
2562{
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002563 Opcode dalvikOpcode = mir->dalvikInsn.opcode;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002564 int fieldOffset = mir->dalvikInsn.vC;
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002565 switch (dalvikOpcode) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07002566 case OP_IGET_QUICK:
2567 case OP_IGET_OBJECT_QUICK:
buzbeeecf8f6e2010-07-20 14:53:42 -07002568 genIGet(cUnit, mir, kWord, fieldOffset, false);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002569 break;
2570 case OP_IPUT_QUICK:
buzbeeecf8f6e2010-07-20 14:53:42 -07002571 genIPut(cUnit, mir, kWord, fieldOffset, false, false);
buzbee919eb062010-07-12 12:59:22 -07002572 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002573 case OP_IPUT_OBJECT_QUICK:
buzbeeecf8f6e2010-07-20 14:53:42 -07002574 genIPut(cUnit, mir, kWord, fieldOffset, true, false);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002575 break;
2576 case OP_IGET_WIDE_QUICK:
2577 genIGetWide(cUnit, mir, fieldOffset);
2578 break;
2579 case OP_IPUT_WIDE_QUICK:
2580 genIPutWide(cUnit, mir, fieldOffset);
2581 break;
2582 default:
2583 return true;
2584 }
2585 return false;
2586
2587}
2588
2589/* Compare agaist zero */
2590static bool handleFmt22t(CompilationUnit *cUnit, MIR *mir, BasicBlock *bb,
Bill Buzbee89efc3d2009-07-28 11:22:22 -07002591 ArmLIR *labelList)
Ben Chengba4fc8b2009-06-01 13:00:29 -07002592{
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002593 Opcode dalvikOpcode = mir->dalvikInsn.opcode;
Bill Buzbee89efc3d2009-07-28 11:22:22 -07002594 ArmConditionCode cond;
Ben Cheng7ab74e12011-02-03 14:02:06 -08002595 /* backward branch? */
2596 bool backwardBranch = (bb->taken->startOffset <= mir->offset);
2597
2598 if (backwardBranch && gDvmJit.genSuspendPoll) {
2599 genSuspendPoll(cUnit, mir);
2600 }
2601
Bill Buzbeec6f10662010-02-09 11:16:15 -08002602 RegLocation rlSrc1 = dvmCompilerGetSrc(cUnit, mir, 0);
2603 RegLocation rlSrc2 = dvmCompilerGetSrc(cUnit, mir, 1);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002604
Bill Buzbee1465db52009-09-23 17:17:35 -07002605 rlSrc1 = loadValue(cUnit, rlSrc1, kCoreReg);
2606 rlSrc2 = loadValue(cUnit, rlSrc2, kCoreReg);
Ben Cheng7ab74e12011-02-03 14:02:06 -08002607
Bill Buzbee1465db52009-09-23 17:17:35 -07002608 opRegReg(cUnit, kOpCmp, rlSrc1.lowReg, rlSrc2.lowReg);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002609
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002610 switch (dalvikOpcode) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07002611 case OP_IF_EQ:
Bill Buzbee1465db52009-09-23 17:17:35 -07002612 cond = kArmCondEq;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002613 break;
2614 case OP_IF_NE:
Bill Buzbee1465db52009-09-23 17:17:35 -07002615 cond = kArmCondNe;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002616 break;
2617 case OP_IF_LT:
Bill Buzbee1465db52009-09-23 17:17:35 -07002618 cond = kArmCondLt;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002619 break;
2620 case OP_IF_GE:
Bill Buzbee1465db52009-09-23 17:17:35 -07002621 cond = kArmCondGe;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002622 break;
2623 case OP_IF_GT:
Bill Buzbee1465db52009-09-23 17:17:35 -07002624 cond = kArmCondGt;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002625 break;
2626 case OP_IF_LE:
Bill Buzbee1465db52009-09-23 17:17:35 -07002627 cond = kArmCondLe;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002628 break;
2629 default:
2630 cond = 0;
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002631 LOGE("Unexpected opcode (%d) for Fmt22t\n", dalvikOpcode);
Bill Buzbeefc519dc2010-03-06 23:30:57 -08002632 dvmCompilerAbort(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002633 }
2634 genConditionalBranch(cUnit, cond, &labelList[bb->taken->id]);
2635 /* This mostly likely will be optimized away in a later phase */
2636 genUnconditionalBranch(cUnit, &labelList[bb->fallThrough->id]);
2637 return false;
2638}
2639
2640static bool handleFmt22x_Fmt32x(CompilationUnit *cUnit, MIR *mir)
2641{
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002642 Opcode opcode = mir->dalvikInsn.opcode;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002643
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002644 switch (opcode) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07002645 case OP_MOVE_16:
2646 case OP_MOVE_OBJECT_16:
2647 case OP_MOVE_FROM16:
Ben Chenge9695e52009-06-16 16:11:47 -07002648 case OP_MOVE_OBJECT_FROM16: {
Bill Buzbeec6f10662010-02-09 11:16:15 -08002649 storeValue(cUnit, dvmCompilerGetDest(cUnit, mir, 0),
2650 dvmCompilerGetSrc(cUnit, mir, 0));
Ben Chengba4fc8b2009-06-01 13:00:29 -07002651 break;
Ben Chenge9695e52009-06-16 16:11:47 -07002652 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07002653 case OP_MOVE_WIDE_16:
Ben Chenge9695e52009-06-16 16:11:47 -07002654 case OP_MOVE_WIDE_FROM16: {
Bill Buzbeec6f10662010-02-09 11:16:15 -08002655 storeValueWide(cUnit, dvmCompilerGetDestWide(cUnit, mir, 0, 1),
2656 dvmCompilerGetSrcWide(cUnit, mir, 0, 1));
Ben Chengba4fc8b2009-06-01 13:00:29 -07002657 break;
Ben Chenge9695e52009-06-16 16:11:47 -07002658 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07002659 default:
2660 return true;
2661 }
2662 return false;
2663}
2664
2665static bool handleFmt23x(CompilationUnit *cUnit, MIR *mir)
2666{
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002667 Opcode opcode = mir->dalvikInsn.opcode;
Bill Buzbee1465db52009-09-23 17:17:35 -07002668 RegLocation rlSrc1;
2669 RegLocation rlSrc2;
2670 RegLocation rlDest;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002671
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002672 if ( (opcode >= OP_ADD_INT) && (opcode <= OP_REM_DOUBLE)) {
Ben Cheng5d90c202009-11-22 23:31:11 -08002673 return genArithOp( cUnit, mir );
Ben Chengba4fc8b2009-06-01 13:00:29 -07002674 }
2675
Bill Buzbee1465db52009-09-23 17:17:35 -07002676 /* APUTs have 3 sources and no targets */
2677 if (mir->ssaRep->numDefs == 0) {
2678 if (mir->ssaRep->numUses == 3) {
Bill Buzbeec6f10662010-02-09 11:16:15 -08002679 rlDest = dvmCompilerGetSrc(cUnit, mir, 0);
2680 rlSrc1 = dvmCompilerGetSrc(cUnit, mir, 1);
2681 rlSrc2 = dvmCompilerGetSrc(cUnit, mir, 2);
Bill Buzbee1465db52009-09-23 17:17:35 -07002682 } else {
2683 assert(mir->ssaRep->numUses == 4);
Bill Buzbeec6f10662010-02-09 11:16:15 -08002684 rlDest = dvmCompilerGetSrcWide(cUnit, mir, 0, 1);
2685 rlSrc1 = dvmCompilerGetSrc(cUnit, mir, 2);
2686 rlSrc2 = dvmCompilerGetSrc(cUnit, mir, 3);
Bill Buzbee1465db52009-09-23 17:17:35 -07002687 }
2688 } else {
2689 /* Two sources and 1 dest. Deduce the operand sizes */
2690 if (mir->ssaRep->numUses == 4) {
Bill Buzbeec6f10662010-02-09 11:16:15 -08002691 rlSrc1 = dvmCompilerGetSrcWide(cUnit, mir, 0, 1);
2692 rlSrc2 = dvmCompilerGetSrcWide(cUnit, mir, 2, 3);
Bill Buzbee1465db52009-09-23 17:17:35 -07002693 } else {
2694 assert(mir->ssaRep->numUses == 2);
Bill Buzbeec6f10662010-02-09 11:16:15 -08002695 rlSrc1 = dvmCompilerGetSrc(cUnit, mir, 0);
2696 rlSrc2 = dvmCompilerGetSrc(cUnit, mir, 1);
Bill Buzbee1465db52009-09-23 17:17:35 -07002697 }
2698 if (mir->ssaRep->numDefs == 2) {
Bill Buzbeec6f10662010-02-09 11:16:15 -08002699 rlDest = dvmCompilerGetDestWide(cUnit, mir, 0, 1);
Bill Buzbee1465db52009-09-23 17:17:35 -07002700 } else {
2701 assert(mir->ssaRep->numDefs == 1);
Bill Buzbeec6f10662010-02-09 11:16:15 -08002702 rlDest = dvmCompilerGetDest(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07002703 }
2704 }
2705
2706
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002707 switch (opcode) {
Bill Buzbeed45ba372009-06-15 17:00:57 -07002708 case OP_CMPL_FLOAT:
2709 case OP_CMPG_FLOAT:
2710 case OP_CMPL_DOUBLE:
2711 case OP_CMPG_DOUBLE:
Ben Cheng5d90c202009-11-22 23:31:11 -08002712 return genCmpFP(cUnit, mir, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002713 case OP_CMP_LONG:
Bill Buzbee1465db52009-09-23 17:17:35 -07002714 genCmpLong(cUnit, mir, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002715 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002716 case OP_AGET_WIDE:
Bill Buzbee1465db52009-09-23 17:17:35 -07002717 genArrayGet(cUnit, mir, kLong, rlSrc1, rlSrc2, rlDest, 3);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002718 break;
2719 case OP_AGET:
2720 case OP_AGET_OBJECT:
Bill Buzbee1465db52009-09-23 17:17:35 -07002721 genArrayGet(cUnit, mir, kWord, rlSrc1, rlSrc2, rlDest, 2);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002722 break;
2723 case OP_AGET_BOOLEAN:
Bill Buzbee1465db52009-09-23 17:17:35 -07002724 genArrayGet(cUnit, mir, kUnsignedByte, rlSrc1, rlSrc2, rlDest, 0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002725 break;
2726 case OP_AGET_BYTE:
Bill Buzbee1465db52009-09-23 17:17:35 -07002727 genArrayGet(cUnit, mir, kSignedByte, rlSrc1, rlSrc2, rlDest, 0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002728 break;
2729 case OP_AGET_CHAR:
Bill Buzbee1465db52009-09-23 17:17:35 -07002730 genArrayGet(cUnit, mir, kUnsignedHalf, rlSrc1, rlSrc2, rlDest, 1);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002731 break;
2732 case OP_AGET_SHORT:
Bill Buzbee1465db52009-09-23 17:17:35 -07002733 genArrayGet(cUnit, mir, kSignedHalf, rlSrc1, rlSrc2, rlDest, 1);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002734 break;
2735 case OP_APUT_WIDE:
Bill Buzbee1465db52009-09-23 17:17:35 -07002736 genArrayPut(cUnit, mir, kLong, rlSrc1, rlSrc2, rlDest, 3);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002737 break;
2738 case OP_APUT:
Bill Buzbee1465db52009-09-23 17:17:35 -07002739 genArrayPut(cUnit, mir, kWord, rlSrc1, rlSrc2, rlDest, 2);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002740 break;
Bill Buzbeebe6534f2010-03-12 16:01:35 -08002741 case OP_APUT_OBJECT:
2742 genArrayObjectPut(cUnit, mir, rlSrc1, rlSrc2, rlDest, 2);
2743 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002744 case OP_APUT_SHORT:
2745 case OP_APUT_CHAR:
Bill Buzbee1465db52009-09-23 17:17:35 -07002746 genArrayPut(cUnit, mir, kUnsignedHalf, rlSrc1, rlSrc2, rlDest, 1);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002747 break;
2748 case OP_APUT_BYTE:
2749 case OP_APUT_BOOLEAN:
Bill Buzbee1465db52009-09-23 17:17:35 -07002750 genArrayPut(cUnit, mir, kUnsignedByte, rlSrc1, rlSrc2, rlDest, 0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002751 break;
2752 default:
2753 return true;
2754 }
2755 return false;
2756}
2757
Ben Cheng6c10a972009-10-29 14:39:18 -07002758/*
2759 * Find the matching case.
2760 *
2761 * return values:
2762 * r0 (low 32-bit): pc of the chaining cell corresponding to the resolved case,
2763 * including default which is placed at MIN(size, MAX_CHAINED_SWITCH_CASES).
2764 * r1 (high 32-bit): the branch offset of the matching case (only for indexes
2765 * above MAX_CHAINED_SWITCH_CASES).
2766 *
2767 * Instructions around the call are:
2768 *
2769 * mov r2, pc
2770 * blx &findPackedSwitchIndex
2771 * mov pc, r0
2772 * .align4
Bill Buzbeebd047242010-05-13 13:02:53 -07002773 * chaining cell for case 0 [12 bytes]
2774 * chaining cell for case 1 [12 bytes]
Ben Cheng6c10a972009-10-29 14:39:18 -07002775 * :
Bill Buzbeebd047242010-05-13 13:02:53 -07002776 * chaining cell for case MIN(size, MAX_CHAINED_SWITCH_CASES)-1 [12 bytes]
Ben Cheng6c10a972009-10-29 14:39:18 -07002777 * chaining cell for case default [8 bytes]
2778 * noChain exit
2779 */
Ben Chengbd1326d2010-04-02 15:04:53 -07002780static s8 findPackedSwitchIndex(const u2* switchData, int testVal, int pc)
Ben Cheng6c10a972009-10-29 14:39:18 -07002781{
2782 int size;
2783 int firstKey;
2784 const int *entries;
2785 int index;
2786 int jumpIndex;
2787 int caseDPCOffset = 0;
2788 /* In Thumb mode pc is 4 ahead of the "mov r2, pc" instruction */
2789 int chainingPC = (pc + 4) & ~3;
2790
2791 /*
2792 * Packed switch data format:
2793 * ushort ident = 0x0100 magic value
2794 * ushort size number of entries in the table
2795 * int first_key first (and lowest) switch case value
2796 * int targets[size] branch targets, relative to switch opcode
2797 *
2798 * Total size is (4+size*2) 16-bit code units.
2799 */
2800 size = switchData[1];
2801 assert(size > 0);
2802
2803 firstKey = switchData[2];
2804 firstKey |= switchData[3] << 16;
2805
2806
2807 /* The entries are guaranteed to be aligned on a 32-bit boundary;
2808 * we can treat them as a native int array.
2809 */
2810 entries = (const int*) &switchData[4];
2811 assert(((u4)entries & 0x3) == 0);
2812
2813 index = testVal - firstKey;
2814
2815 /* Jump to the default cell */
2816 if (index < 0 || index >= size) {
2817 jumpIndex = MIN(size, MAX_CHAINED_SWITCH_CASES);
2818 /* Jump to the non-chaining exit point */
2819 } else if (index >= MAX_CHAINED_SWITCH_CASES) {
2820 jumpIndex = MAX_CHAINED_SWITCH_CASES + 1;
2821 caseDPCOffset = entries[index];
2822 /* Jump to the inline chaining cell */
2823 } else {
2824 jumpIndex = index;
2825 }
2826
Bill Buzbeebd047242010-05-13 13:02:53 -07002827 chainingPC += jumpIndex * CHAIN_CELL_NORMAL_SIZE;
Ben Cheng6c10a972009-10-29 14:39:18 -07002828 return (((s8) caseDPCOffset) << 32) | (u8) chainingPC;
2829}
2830
2831/* See comments for findPackedSwitchIndex */
Ben Chengbd1326d2010-04-02 15:04:53 -07002832static s8 findSparseSwitchIndex(const u2* switchData, int testVal, int pc)
Ben Cheng6c10a972009-10-29 14:39:18 -07002833{
2834 int size;
2835 const int *keys;
2836 const int *entries;
2837 int chainingPC = (pc + 4) & ~3;
2838 int i;
2839
2840 /*
2841 * Sparse switch data format:
2842 * ushort ident = 0x0200 magic value
2843 * ushort size number of entries in the table; > 0
2844 * int keys[size] keys, sorted low-to-high; 32-bit aligned
2845 * int targets[size] branch targets, relative to switch opcode
2846 *
2847 * Total size is (2+size*4) 16-bit code units.
2848 */
2849
2850 size = switchData[1];
2851 assert(size > 0);
2852
2853 /* The keys are guaranteed to be aligned on a 32-bit boundary;
2854 * we can treat them as a native int array.
2855 */
2856 keys = (const int*) &switchData[2];
2857 assert(((u4)keys & 0x3) == 0);
2858
2859 /* The entries are guaranteed to be aligned on a 32-bit boundary;
2860 * we can treat them as a native int array.
2861 */
2862 entries = keys + size;
2863 assert(((u4)entries & 0x3) == 0);
2864
2865 /*
2866 * Run through the list of keys, which are guaranteed to
2867 * be sorted low-to-high.
2868 *
2869 * Most tables have 3-4 entries. Few have more than 10. A binary
2870 * search here is probably not useful.
2871 */
2872 for (i = 0; i < size; i++) {
2873 int k = keys[i];
2874 if (k == testVal) {
2875 /* MAX_CHAINED_SWITCH_CASES + 1 is the start of the overflow case */
2876 int jumpIndex = (i < MAX_CHAINED_SWITCH_CASES) ?
2877 i : MAX_CHAINED_SWITCH_CASES + 1;
Bill Buzbeebd047242010-05-13 13:02:53 -07002878 chainingPC += jumpIndex * CHAIN_CELL_NORMAL_SIZE;
Ben Cheng6c10a972009-10-29 14:39:18 -07002879 return (((s8) entries[i]) << 32) | (u8) chainingPC;
2880 } else if (k > testVal) {
2881 break;
2882 }
2883 }
Bill Buzbeebd047242010-05-13 13:02:53 -07002884 return chainingPC + MIN(size, MAX_CHAINED_SWITCH_CASES) *
2885 CHAIN_CELL_NORMAL_SIZE;
Ben Cheng6c10a972009-10-29 14:39:18 -07002886}
2887
Ben Chengba4fc8b2009-06-01 13:00:29 -07002888static bool handleFmt31t(CompilationUnit *cUnit, MIR *mir)
2889{
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002890 Opcode dalvikOpcode = mir->dalvikInsn.opcode;
2891 switch (dalvikOpcode) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07002892 case OP_FILL_ARRAY_DATA: {
Bill Buzbeec6f10662010-02-09 11:16:15 -08002893 RegLocation rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07002894 // Making a call - use explicit registers
Bill Buzbeec6f10662010-02-09 11:16:15 -08002895 dvmCompilerFlushAllRegs(cUnit); /* Everything to home location */
Bill Buzbee1465db52009-09-23 17:17:35 -07002896 genExportPC(cUnit, mir);
2897 loadValueDirectFixed(cUnit, rlSrc, r0);
Ben Chengbd1326d2010-04-02 15:04:53 -07002898 LOAD_FUNC_ADDR(cUnit, r2, (int)dvmInterpHandleFillArrayData);
Ben Cheng6c10a972009-10-29 14:39:18 -07002899 loadConstant(cUnit, r1,
2900 (int) (cUnit->method->insns + mir->offset + mir->dalvikInsn.vB));
Bill Buzbee1465db52009-09-23 17:17:35 -07002901 opReg(cUnit, kOpBlx, r2);
Elliott Hughes6a555132010-02-25 15:41:42 -08002902 dvmCompilerClobberCallRegs(cUnit);
Bill Buzbeefd023aa2009-11-02 09:23:49 -08002903 /* generate a branch over if successful */
buzbee8f8109a2010-08-31 10:16:35 -07002904 ArmLIR *branchOver = genCmpImmBranch(cUnit, kArmCondNe, r0, 0);
Bill Buzbeefd023aa2009-11-02 09:23:49 -08002905 loadConstant(cUnit, r0,
2906 (int) (cUnit->method->insns + mir->offset));
2907 genDispatchToHandler(cUnit, TEMPLATE_THROW_EXCEPTION_COMMON);
2908 ArmLIR *target = newLIR0(cUnit, kArmPseudoTargetLabel);
2909 target->defMask = ENCODE_ALL;
2910 branchOver->generic.target = (LIR *) target;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002911 break;
2912 }
2913 /*
Ben Cheng6c10a972009-10-29 14:39:18 -07002914 * Compute the goto target of up to
2915 * MIN(switchSize, MAX_CHAINED_SWITCH_CASES) + 1 chaining cells.
2916 * See the comment before findPackedSwitchIndex for the code layout.
Ben Chengba4fc8b2009-06-01 13:00:29 -07002917 */
2918 case OP_PACKED_SWITCH:
2919 case OP_SPARSE_SWITCH: {
Bill Buzbeec6f10662010-02-09 11:16:15 -08002920 RegLocation rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
2921 dvmCompilerFlushAllRegs(cUnit); /* Everything to home location */
Bill Buzbee1465db52009-09-23 17:17:35 -07002922 loadValueDirectFixed(cUnit, rlSrc, r1);
Bill Buzbeec6f10662010-02-09 11:16:15 -08002923 dvmCompilerLockAllTemps(cUnit);
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002924 if (dalvikOpcode == OP_PACKED_SWITCH) {
Ben Chengbd1326d2010-04-02 15:04:53 -07002925 LOAD_FUNC_ADDR(cUnit, r4PC, (int)findPackedSwitchIndex);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002926 } else {
Ben Chengbd1326d2010-04-02 15:04:53 -07002927 LOAD_FUNC_ADDR(cUnit, r4PC, (int)findSparseSwitchIndex);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002928 }
Ben Cheng6c10a972009-10-29 14:39:18 -07002929 /* r0 <- Addr of the switch data */
2930 loadConstant(cUnit, r0,
2931 (int) (cUnit->method->insns + mir->offset + mir->dalvikInsn.vB));
2932 /* r2 <- pc of the instruction following the blx */
Ben Cheng20d7e6c2011-02-18 17:12:42 -08002933 opRegReg(cUnit, kOpMov, r2, r15pc);
Bill Buzbee1465db52009-09-23 17:17:35 -07002934 opReg(cUnit, kOpBlx, r4PC);
Elliott Hughes6a555132010-02-25 15:41:42 -08002935 dvmCompilerClobberCallRegs(cUnit);
Ben Cheng6c10a972009-10-29 14:39:18 -07002936 /* pc <- computed goto target */
Ben Cheng20d7e6c2011-02-18 17:12:42 -08002937 opRegReg(cUnit, kOpMov, r15pc, r0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002938 break;
2939 }
2940 default:
2941 return true;
2942 }
2943 return false;
2944}
2945
Ben Cheng7a2697d2010-06-07 13:44:23 -07002946/*
2947 * See the example of predicted inlining listed before the
2948 * genValidationForPredictedInline function. The function here takes care the
2949 * branch over at 0x4858de78 and the misprediction target at 0x4858de7a.
2950 */
2951static void genLandingPadForMispredictedCallee(CompilationUnit *cUnit, MIR *mir,
2952 BasicBlock *bb,
2953 ArmLIR *labelList)
2954{
2955 BasicBlock *fallThrough = bb->fallThrough;
2956
2957 /* Bypass the move-result block if there is one */
2958 if (fallThrough->firstMIRInsn) {
2959 assert(fallThrough->firstMIRInsn->OptimizationFlags & MIR_INLINED_PRED);
2960 fallThrough = fallThrough->fallThrough;
2961 }
2962 /* Generate a branch over if the predicted inlining is correct */
2963 genUnconditionalBranch(cUnit, &labelList[fallThrough->id]);
2964
2965 /* Reset the register state */
2966 dvmCompilerResetRegPool(cUnit);
2967 dvmCompilerClobberAllRegs(cUnit);
2968 dvmCompilerResetNullCheck(cUnit);
2969
2970 /* Target for the slow invoke path */
2971 ArmLIR *target = newLIR0(cUnit, kArmPseudoTargetLabel);
2972 target->defMask = ENCODE_ALL;
2973 /* Hook up the target to the verification branch */
2974 mir->meta.callsiteInfo->misPredBranchOver->target = (LIR *) target;
2975}
2976
jeffhao71eee1f2011-01-04 14:18:54 -08002977static bool handleFmt35c_3rc_5rc(CompilationUnit *cUnit, MIR *mir,
2978 BasicBlock *bb, ArmLIR *labelList)
Ben Chengba4fc8b2009-06-01 13:00:29 -07002979{
Bill Buzbee9bc3df32009-07-30 10:52:29 -07002980 ArmLIR *retChainingCell = NULL;
Bill Buzbee89efc3d2009-07-28 11:22:22 -07002981 ArmLIR *pcrLabel = NULL;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002982
Ben Cheng7a2697d2010-06-07 13:44:23 -07002983 /* An invoke with the MIR_INLINED is effectively a no-op */
2984 if (mir->OptimizationFlags & MIR_INLINED)
2985 return false;
2986
Bill Buzbeef4ce16f2009-07-28 13:28:25 -07002987 if (bb->fallThrough != NULL)
2988 retChainingCell = &labelList[bb->fallThrough->id];
2989
Ben Chengba4fc8b2009-06-01 13:00:29 -07002990 DecodedInstruction *dInsn = &mir->dalvikInsn;
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002991 switch (mir->dalvikInsn.opcode) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07002992 /*
2993 * calleeMethod = this->clazz->vtable[
2994 * method->clazz->pDvmDex->pResMethods[BBBB]->methodIndex
2995 * ]
2996 */
2997 case OP_INVOKE_VIRTUAL:
jeffhao71eee1f2011-01-04 14:18:54 -08002998 case OP_INVOKE_VIRTUAL_RANGE:
2999 case OP_INVOKE_VIRTUAL_JUMBO: {
Bill Buzbee89efc3d2009-07-28 11:22:22 -07003000 ArmLIR *predChainingCell = &labelList[bb->taken->id];
Ben Chengba4fc8b2009-06-01 13:00:29 -07003001 int methodIndex =
3002 cUnit->method->clazz->pDvmDex->pResMethods[dInsn->vB]->
3003 methodIndex;
3004
Ben Cheng7a2697d2010-06-07 13:44:23 -07003005 /*
3006 * If the invoke has non-null misPredBranchOver, we need to generate
3007 * the non-inlined version of the invoke here to handle the
3008 * mispredicted case.
3009 */
3010 if (mir->meta.callsiteInfo->misPredBranchOver) {
3011 genLandingPadForMispredictedCallee(cUnit, mir, bb, labelList);
3012 }
3013
Dan Bornstein9a1f8162010-12-01 17:02:26 -08003014 if (mir->dalvikInsn.opcode == OP_INVOKE_VIRTUAL)
Ben Chengba4fc8b2009-06-01 13:00:29 -07003015 genProcessArgsNoRange(cUnit, mir, dInsn, &pcrLabel);
3016 else
3017 genProcessArgsRange(cUnit, mir, dInsn, &pcrLabel);
3018
Ben Cheng38329f52009-07-07 14:19:20 -07003019 genInvokeVirtualCommon(cUnit, mir, methodIndex,
3020 retChainingCell,
3021 predChainingCell,
3022 pcrLabel);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003023 break;
3024 }
3025 /*
3026 * calleeMethod = method->clazz->super->vtable[method->clazz->pDvmDex
3027 * ->pResMethods[BBBB]->methodIndex]
3028 */
Ben Chengba4fc8b2009-06-01 13:00:29 -07003029 case OP_INVOKE_SUPER:
jeffhao71eee1f2011-01-04 14:18:54 -08003030 case OP_INVOKE_SUPER_RANGE:
3031 case OP_INVOKE_SUPER_JUMBO: {
Ben Cheng7a2697d2010-06-07 13:44:23 -07003032 /* Grab the method ptr directly from what the interpreter sees */
3033 const Method *calleeMethod = mir->meta.callsiteInfo->method;
3034 assert(calleeMethod == cUnit->method->clazz->super->vtable[
3035 cUnit->method->clazz->pDvmDex->
3036 pResMethods[dInsn->vB]->methodIndex]);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003037
Dan Bornstein9a1f8162010-12-01 17:02:26 -08003038 if (mir->dalvikInsn.opcode == OP_INVOKE_SUPER)
Ben Chengba4fc8b2009-06-01 13:00:29 -07003039 genProcessArgsNoRange(cUnit, mir, dInsn, &pcrLabel);
3040 else
3041 genProcessArgsRange(cUnit, mir, dInsn, &pcrLabel);
3042
Ben Cheng20d7e6c2011-02-18 17:12:42 -08003043 if (mir->OptimizationFlags & MIR_INVOKE_METHOD_JIT) {
3044 const Method *calleeMethod = mir->meta.callsiteInfo->method;
3045 void *calleeAddr = dvmJitGetMethodAddr(calleeMethod->insns);
3046 assert(calleeAddr);
3047 genInvokeSingletonWholeMethod(cUnit, mir, calleeAddr,
3048 retChainingCell);
3049 } else {
3050 /* r0 = calleeMethod */
3051 loadConstant(cUnit, r0, (int) calleeMethod);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003052
Ben Cheng20d7e6c2011-02-18 17:12:42 -08003053 genInvokeSingletonCommon(cUnit, mir, bb, labelList, pcrLabel,
3054 calleeMethod);
3055 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07003056 break;
3057 }
3058 /* calleeMethod = method->clazz->pDvmDex->pResMethods[BBBB] */
3059 case OP_INVOKE_DIRECT:
jeffhao71eee1f2011-01-04 14:18:54 -08003060 case OP_INVOKE_DIRECT_RANGE:
3061 case OP_INVOKE_DIRECT_JUMBO: {
Ben Cheng7a2697d2010-06-07 13:44:23 -07003062 /* Grab the method ptr directly from what the interpreter sees */
3063 const Method *calleeMethod = mir->meta.callsiteInfo->method;
3064 assert(calleeMethod ==
3065 cUnit->method->clazz->pDvmDex->pResMethods[dInsn->vB]);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003066
Dan Bornstein9a1f8162010-12-01 17:02:26 -08003067 if (mir->dalvikInsn.opcode == OP_INVOKE_DIRECT)
Ben Chengba4fc8b2009-06-01 13:00:29 -07003068 genProcessArgsNoRange(cUnit, mir, dInsn, &pcrLabel);
3069 else
3070 genProcessArgsRange(cUnit, mir, dInsn, &pcrLabel);
3071
3072 /* r0 = calleeMethod */
3073 loadConstant(cUnit, r0, (int) calleeMethod);
3074
Ben Cheng38329f52009-07-07 14:19:20 -07003075 genInvokeSingletonCommon(cUnit, mir, bb, labelList, pcrLabel,
3076 calleeMethod);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003077 break;
3078 }
3079 /* calleeMethod = method->clazz->pDvmDex->pResMethods[BBBB] */
3080 case OP_INVOKE_STATIC:
jeffhao71eee1f2011-01-04 14:18:54 -08003081 case OP_INVOKE_STATIC_RANGE:
3082 case OP_INVOKE_STATIC_JUMBO: {
Ben Cheng7a2697d2010-06-07 13:44:23 -07003083 /* Grab the method ptr directly from what the interpreter sees */
3084 const Method *calleeMethod = mir->meta.callsiteInfo->method;
3085 assert(calleeMethod ==
3086 cUnit->method->clazz->pDvmDex->pResMethods[dInsn->vB]);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003087
Dan Bornstein9a1f8162010-12-01 17:02:26 -08003088 if (mir->dalvikInsn.opcode == OP_INVOKE_STATIC)
Ben Chengba4fc8b2009-06-01 13:00:29 -07003089 genProcessArgsNoRange(cUnit, mir, dInsn,
3090 NULL /* no null check */);
3091 else
3092 genProcessArgsRange(cUnit, mir, dInsn,
3093 NULL /* no null check */);
3094
Ben Cheng20d7e6c2011-02-18 17:12:42 -08003095 if (mir->OptimizationFlags & MIR_INVOKE_METHOD_JIT) {
3096 const Method *calleeMethod = mir->meta.callsiteInfo->method;
3097 void *calleeAddr = dvmJitGetMethodAddr(calleeMethod->insns);
3098 assert(calleeAddr);
3099 genInvokeSingletonWholeMethod(cUnit, mir, calleeAddr,
3100 retChainingCell);
3101 } else {
3102 /* r0 = calleeMethod */
3103 loadConstant(cUnit, r0, (int) calleeMethod);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003104
Ben Cheng20d7e6c2011-02-18 17:12:42 -08003105 genInvokeSingletonCommon(cUnit, mir, bb, labelList, pcrLabel,
3106 calleeMethod);
3107 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07003108 break;
3109 }
Ben Cheng09e50c92010-05-02 10:45:32 -07003110 /*
Ben Chengba4fc8b2009-06-01 13:00:29 -07003111 * calleeMethod = dvmFindInterfaceMethodInCache(this->clazz,
3112 * BBBB, method, method->clazz->pDvmDex)
Ben Cheng38329f52009-07-07 14:19:20 -07003113 *
Ben Cheng09e50c92010-05-02 10:45:32 -07003114 * The following is an example of generated code for
3115 * "invoke-interface v0"
Ben Cheng38329f52009-07-07 14:19:20 -07003116 *
Ben Cheng09e50c92010-05-02 10:45:32 -07003117 * -------- dalvik offset: 0x0008 @ invoke-interface v0
3118 * 0x47357e36 : ldr r0, [r5, #0] --+
3119 * 0x47357e38 : sub r7,r5,#24 |
3120 * 0x47357e3c : cmp r0, #0 | genProcessArgsNoRange
3121 * 0x47357e3e : beq 0x47357e82 |
3122 * 0x47357e40 : stmia r7, <r0> --+
3123 * 0x47357e42 : ldr r4, [pc, #120] --> r4 <- dalvikPC of this invoke
3124 * 0x47357e44 : add r1, pc, #64 --> r1 <- &retChainingCell
3125 * 0x47357e46 : add r2, pc, #72 --> r2 <- &predictedChainingCell
3126 * 0x47357e48 : blx_1 0x47348190 --+ TEMPLATE_INVOKE_METHOD_
3127 * 0x47357e4a : blx_2 see above --+ PREDICTED_CHAIN
3128 * 0x47357e4c : b 0x47357e90 --> off to the predicted chain
3129 * 0x47357e4e : b 0x47357e82 --> punt to the interpreter
3130 * 0x47357e50 : mov r8, r1 --+
3131 * 0x47357e52 : mov r9, r2 |
3132 * 0x47357e54 : ldr r2, [pc, #96] |
3133 * 0x47357e56 : mov r10, r3 |
3134 * 0x47357e58 : movs r0, r3 | dvmFindInterfaceMethodInCache
3135 * 0x47357e5a : ldr r3, [pc, #88] |
3136 * 0x47357e5c : ldr r7, [pc, #80] |
3137 * 0x47357e5e : mov r1, #1452 |
3138 * 0x47357e62 : blx r7 --+
3139 * 0x47357e64 : cmp r0, #0 --> calleeMethod == NULL?
3140 * 0x47357e66 : bne 0x47357e6e --> branch over the throw if !r0
3141 * 0x47357e68 : ldr r0, [pc, #80] --> load Dalvik PC of the invoke
3142 * 0x47357e6a : blx_1 0x47348494 --+ TEMPLATE_THROW_EXCEPTION_
3143 * 0x47357e6c : blx_2 see above --+ COMMON
3144 * 0x47357e6e : mov r1, r8 --> r1 <- &retChainingCell
3145 * 0x47357e70 : cmp r1, #0 --> compare against 0
3146 * 0x47357e72 : bgt 0x47357e7c --> >=0? don't rechain
Ben Chengaf5aa1f2011-01-04 15:37:04 -08003147 * 0x47357e74 : ldr r7, [pc, #off] --+
Ben Cheng09e50c92010-05-02 10:45:32 -07003148 * 0x47357e76 : mov r2, r9 | dvmJitToPatchPredictedChain
3149 * 0x47357e78 : mov r3, r10 |
3150 * 0x47357e7a : blx r7 --+
3151 * 0x47357e7c : add r1, pc, #8 --> r1 <- &retChainingCell
3152 * 0x47357e7e : blx_1 0x4734809c --+ TEMPLATE_INVOKE_METHOD_NO_OPT
3153 * 0x47357e80 : blx_2 see above --+
3154 * -------- reconstruct dalvik PC : 0x425719dc @ +0x0008
3155 * 0x47357e82 : ldr r0, [pc, #56]
Ben Cheng38329f52009-07-07 14:19:20 -07003156 * Exception_Handling:
Ben Cheng09e50c92010-05-02 10:45:32 -07003157 * 0x47357e84 : ldr r1, [r6, #92]
3158 * 0x47357e86 : blx r1
3159 * 0x47357e88 : .align4
3160 * -------- chaining cell (hot): 0x000b
3161 * 0x47357e88 : ldr r0, [r6, #104]
3162 * 0x47357e8a : blx r0
3163 * 0x47357e8c : data 0x19e2(6626)
3164 * 0x47357e8e : data 0x4257(16983)
3165 * 0x47357e90 : .align4
Ben Cheng38329f52009-07-07 14:19:20 -07003166 * -------- chaining cell (predicted)
Ben Cheng09e50c92010-05-02 10:45:32 -07003167 * 0x47357e90 : data 0xe7fe(59390) --> will be patched into bx
3168 * 0x47357e92 : data 0x0000(0)
3169 * 0x47357e94 : data 0x0000(0) --> class
3170 * 0x47357e96 : data 0x0000(0)
3171 * 0x47357e98 : data 0x0000(0) --> method
3172 * 0x47357e9a : data 0x0000(0)
3173 * 0x47357e9c : data 0x0000(0) --> rechain count
3174 * 0x47357e9e : data 0x0000(0)
3175 * -------- end of chaining cells (0x006c)
3176 * 0x47357eb0 : .word (0xad03e369)
3177 * 0x47357eb4 : .word (0x28a90)
3178 * 0x47357eb8 : .word (0x41a63394)
3179 * 0x47357ebc : .word (0x425719dc)
Ben Chengba4fc8b2009-06-01 13:00:29 -07003180 */
3181 case OP_INVOKE_INTERFACE:
jeffhao71eee1f2011-01-04 14:18:54 -08003182 case OP_INVOKE_INTERFACE_RANGE:
3183 case OP_INVOKE_INTERFACE_JUMBO: {
Bill Buzbee89efc3d2009-07-28 11:22:22 -07003184 ArmLIR *predChainingCell = &labelList[bb->taken->id];
Ben Chengba4fc8b2009-06-01 13:00:29 -07003185
Ben Cheng7a2697d2010-06-07 13:44:23 -07003186 /*
3187 * If the invoke has non-null misPredBranchOver, we need to generate
3188 * the non-inlined version of the invoke here to handle the
3189 * mispredicted case.
3190 */
3191 if (mir->meta.callsiteInfo->misPredBranchOver) {
3192 genLandingPadForMispredictedCallee(cUnit, mir, bb, labelList);
3193 }
Bill Buzbee1465db52009-09-23 17:17:35 -07003194
Dan Bornstein9a1f8162010-12-01 17:02:26 -08003195 if (mir->dalvikInsn.opcode == OP_INVOKE_INTERFACE)
Ben Chengba4fc8b2009-06-01 13:00:29 -07003196 genProcessArgsNoRange(cUnit, mir, dInsn, &pcrLabel);
3197 else
3198 genProcessArgsRange(cUnit, mir, dInsn, &pcrLabel);
3199
Ben Cheng38329f52009-07-07 14:19:20 -07003200 /* "this" is already left in r0 by genProcessArgs* */
3201
3202 /* r4PC = dalvikCallsite */
3203 loadConstant(cUnit, r4PC,
3204 (int) (cUnit->method->insns + mir->offset));
3205
3206 /* r1 = &retChainingCell */
Bill Buzbee270c1d62009-08-13 16:58:07 -07003207 ArmLIR *addrRetChain =
Ben Cheng20d7e6c2011-02-18 17:12:42 -08003208 opRegRegImm(cUnit, kOpAdd, r1, r15pc, 0);
Ben Cheng38329f52009-07-07 14:19:20 -07003209 addrRetChain->generic.target = (LIR *) retChainingCell;
3210
3211 /* r2 = &predictedChainingCell */
Bill Buzbee89efc3d2009-07-28 11:22:22 -07003212 ArmLIR *predictedChainingCell =
Ben Cheng20d7e6c2011-02-18 17:12:42 -08003213 opRegRegImm(cUnit, kOpAdd, r2, r15pc, 0);
Ben Cheng38329f52009-07-07 14:19:20 -07003214 predictedChainingCell->generic.target = (LIR *) predChainingCell;
3215
buzbee18fba342011-01-19 15:31:15 -08003216 genDispatchToHandler(cUnit, gDvmJit.methodTraceSupport ?
3217 TEMPLATE_INVOKE_METHOD_PREDICTED_CHAIN_PROF :
3218 TEMPLATE_INVOKE_METHOD_PREDICTED_CHAIN);
Ben Cheng38329f52009-07-07 14:19:20 -07003219
3220 /* return through lr - jump to the chaining cell */
3221 genUnconditionalBranch(cUnit, predChainingCell);
3222
3223 /*
3224 * null-check on "this" may have been eliminated, but we still need
3225 * a PC-reconstruction label for stack overflow bailout.
3226 */
3227 if (pcrLabel == NULL) {
3228 int dPC = (int) (cUnit->method->insns + mir->offset);
Carl Shapirofc75f3e2010-12-07 11:43:38 -08003229 pcrLabel = (ArmLIR *) dvmCompilerNew(sizeof(ArmLIR), true);
Dan Bornstein9a1f8162010-12-01 17:02:26 -08003230 pcrLabel->opcode = kArmPseudoPCReconstructionCell;
Ben Cheng38329f52009-07-07 14:19:20 -07003231 pcrLabel->operands[0] = dPC;
3232 pcrLabel->operands[1] = mir->offset;
3233 /* Insert the place holder to the growable list */
Ben Cheng00603072010-10-28 11:13:58 -07003234 dvmInsertGrowableList(&cUnit->pcReconstructionList,
3235 (intptr_t) pcrLabel);
Ben Cheng38329f52009-07-07 14:19:20 -07003236 }
3237
3238 /* return through lr+2 - punt to the interpreter */
3239 genUnconditionalBranch(cUnit, pcrLabel);
3240
3241 /*
3242 * return through lr+4 - fully resolve the callee method.
3243 * r1 <- count
3244 * r2 <- &predictedChainCell
3245 * r3 <- this->class
3246 * r4 <- dPC
3247 * r7 <- this->class->vtable
3248 */
3249
3250 /* Save count, &predictedChainCell, and class to high regs first */
Bill Buzbee1465db52009-09-23 17:17:35 -07003251 genRegCopy(cUnit, r8, r1);
3252 genRegCopy(cUnit, r9, r2);
3253 genRegCopy(cUnit, r10, r3);
Ben Cheng38329f52009-07-07 14:19:20 -07003254
Ben Chengba4fc8b2009-06-01 13:00:29 -07003255 /* r0 now contains this->clazz */
Bill Buzbee1465db52009-09-23 17:17:35 -07003256 genRegCopy(cUnit, r0, r3);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003257
3258 /* r1 = BBBB */
3259 loadConstant(cUnit, r1, dInsn->vB);
3260
3261 /* r2 = method (caller) */
3262 loadConstant(cUnit, r2, (int) cUnit->method);
3263
3264 /* r3 = pDvmDex */
3265 loadConstant(cUnit, r3, (int) cUnit->method->clazz->pDvmDex);
3266
Ben Chengbd1326d2010-04-02 15:04:53 -07003267 LOAD_FUNC_ADDR(cUnit, r7,
3268 (intptr_t) dvmFindInterfaceMethodInCache);
Bill Buzbee1465db52009-09-23 17:17:35 -07003269 opReg(cUnit, kOpBlx, r7);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003270 /* r0 = calleeMethod (returned from dvmFindInterfaceMethodInCache */
3271
Ben Cheng09e50c92010-05-02 10:45:32 -07003272 dvmCompilerClobberCallRegs(cUnit);
3273 /* generate a branch over if the interface method is resolved */
buzbee8f8109a2010-08-31 10:16:35 -07003274 ArmLIR *branchOver = genCmpImmBranch(cUnit, kArmCondNe, r0, 0);
Ben Cheng09e50c92010-05-02 10:45:32 -07003275 /*
3276 * calleeMethod == NULL -> throw
3277 */
3278 loadConstant(cUnit, r0,
3279 (int) (cUnit->method->insns + mir->offset));
3280 genDispatchToHandler(cUnit, TEMPLATE_THROW_EXCEPTION_COMMON);
3281 /* noreturn */
3282
3283 ArmLIR *target = newLIR0(cUnit, kArmPseudoTargetLabel);
3284 target->defMask = ENCODE_ALL;
3285 branchOver->generic.target = (LIR *) target;
3286
Bill Buzbee1465db52009-09-23 17:17:35 -07003287 genRegCopy(cUnit, r1, r8);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003288
Ben Cheng38329f52009-07-07 14:19:20 -07003289 /* Check if rechain limit is reached */
buzbee8f8109a2010-08-31 10:16:35 -07003290 ArmLIR *bypassRechaining = genCmpImmBranch(cUnit, kArmCondGt,
3291 r1, 0);
Ben Cheng38329f52009-07-07 14:19:20 -07003292
Ben Chengaf5aa1f2011-01-04 15:37:04 -08003293 LOAD_FUNC_ADDR(cUnit, r7, (int) dvmJitToPatchPredictedChain);
Ben Cheng38329f52009-07-07 14:19:20 -07003294
Ben Cheng20d7e6c2011-02-18 17:12:42 -08003295 genRegCopy(cUnit, r1, r6SELF);
Bill Buzbee1465db52009-09-23 17:17:35 -07003296 genRegCopy(cUnit, r2, r9);
3297 genRegCopy(cUnit, r3, r10);
Ben Cheng38329f52009-07-07 14:19:20 -07003298
3299 /*
3300 * r0 = calleeMethod
3301 * r2 = &predictedChainingCell
3302 * r3 = class
3303 *
3304 * &returnChainingCell has been loaded into r1 but is not needed
3305 * when patching the chaining cell and will be clobbered upon
3306 * returning so it will be reconstructed again.
3307 */
Bill Buzbee1465db52009-09-23 17:17:35 -07003308 opReg(cUnit, kOpBlx, r7);
Ben Cheng38329f52009-07-07 14:19:20 -07003309
3310 /* r1 = &retChainingCell */
Ben Cheng20d7e6c2011-02-18 17:12:42 -08003311 addrRetChain = opRegRegImm(cUnit, kOpAdd, r1, r15pc, 0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003312 addrRetChain->generic.target = (LIR *) retChainingCell;
Ben Cheng38329f52009-07-07 14:19:20 -07003313
3314 bypassRechaining->generic.target = (LIR *) addrRetChain;
3315
Ben Chengba4fc8b2009-06-01 13:00:29 -07003316 /*
3317 * r0 = this, r1 = calleeMethod,
3318 * r1 = &ChainingCell,
3319 * r4PC = callsiteDPC,
3320 */
buzbee18fba342011-01-19 15:31:15 -08003321 genDispatchToHandler(cUnit, gDvmJit.methodTraceSupport ?
3322 TEMPLATE_INVOKE_METHOD_NO_OPT_PROF :
3323 TEMPLATE_INVOKE_METHOD_NO_OPT);
Ben Cheng978738d2010-05-13 13:45:57 -07003324#if defined(WITH_JIT_TUNING)
Ben Cheng86717f72010-03-05 15:27:21 -08003325 gDvmJit.invokePolymorphic++;
Ben Chengba4fc8b2009-06-01 13:00:29 -07003326#endif
3327 /* Handle exceptions using the interpreter */
3328 genTrap(cUnit, mir->offset, pcrLabel);
3329 break;
3330 }
Andy McFadden0346e9d2011-03-01 15:47:46 -08003331 case OP_INVOKE_OBJECT_INIT_RANGE: {
Andy McFadden6af2ddd2011-02-16 16:50:40 -08003332 genInterpSingleStep(cUnit, mir);
buzbee18fba342011-01-19 15:31:15 -08003333 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07003334 }
3335 case OP_FILLED_NEW_ARRAY:
jeffhao71eee1f2011-01-04 14:18:54 -08003336 case OP_FILLED_NEW_ARRAY_RANGE:
3337 case OP_FILLED_NEW_ARRAY_JUMBO: {
Ben Chengba4fc8b2009-06-01 13:00:29 -07003338 /* Just let the interpreter deal with these */
3339 genInterpSingleStep(cUnit, mir);
3340 break;
3341 }
3342 default:
3343 return true;
3344 }
3345 return false;
3346}
3347
3348static bool handleFmt35ms_3rms(CompilationUnit *cUnit, MIR *mir,
Bill Buzbee89efc3d2009-07-28 11:22:22 -07003349 BasicBlock *bb, ArmLIR *labelList)
Ben Chengba4fc8b2009-06-01 13:00:29 -07003350{
Bill Buzbee89efc3d2009-07-28 11:22:22 -07003351 ArmLIR *pcrLabel = NULL;
Ben Chengba4fc8b2009-06-01 13:00:29 -07003352
Ben Cheng7a2697d2010-06-07 13:44:23 -07003353 /* An invoke with the MIR_INLINED is effectively a no-op */
3354 if (mir->OptimizationFlags & MIR_INLINED)
3355 return false;
3356
Ben Chengba4fc8b2009-06-01 13:00:29 -07003357 DecodedInstruction *dInsn = &mir->dalvikInsn;
Dan Bornstein9a1f8162010-12-01 17:02:26 -08003358 switch (mir->dalvikInsn.opcode) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07003359 /* calleeMethod = this->clazz->vtable[BBBB] */
3360 case OP_INVOKE_VIRTUAL_QUICK_RANGE:
3361 case OP_INVOKE_VIRTUAL_QUICK: {
3362 int methodIndex = dInsn->vB;
Bill Buzbeea8589332010-12-27 09:31:21 -08003363 ArmLIR *retChainingCell = &labelList[bb->fallThrough->id];
3364 ArmLIR *predChainingCell = &labelList[bb->taken->id];
Ben Cheng7a2697d2010-06-07 13:44:23 -07003365
3366 /*
3367 * If the invoke has non-null misPredBranchOver, we need to generate
3368 * the non-inlined version of the invoke here to handle the
3369 * mispredicted case.
3370 */
3371 if (mir->meta.callsiteInfo->misPredBranchOver) {
3372 genLandingPadForMispredictedCallee(cUnit, mir, bb, labelList);
3373 }
3374
Dan Bornstein9a1f8162010-12-01 17:02:26 -08003375 if (mir->dalvikInsn.opcode == OP_INVOKE_VIRTUAL_QUICK)
Ben Chengba4fc8b2009-06-01 13:00:29 -07003376 genProcessArgsNoRange(cUnit, mir, dInsn, &pcrLabel);
3377 else
3378 genProcessArgsRange(cUnit, mir, dInsn, &pcrLabel);
3379
Ben Chengcfdeca32011-01-14 11:36:46 -08003380
3381 if (mir->OptimizationFlags & MIR_INVOKE_METHOD_JIT) {
3382 const Method *calleeMethod = mir->meta.callsiteInfo->method;
3383 void *calleeAddr = dvmJitGetMethodAddr(calleeMethod->insns);
Ben Cheng20d7e6c2011-02-18 17:12:42 -08003384 assert(calleeAddr);
3385 genInvokeVirtualWholeMethod(cUnit, mir, calleeAddr,
3386 retChainingCell);
Ben Chengcfdeca32011-01-14 11:36:46 -08003387 }
3388
Ben Cheng38329f52009-07-07 14:19:20 -07003389 genInvokeVirtualCommon(cUnit, mir, methodIndex,
3390 retChainingCell,
3391 predChainingCell,
3392 pcrLabel);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003393 break;
3394 }
3395 /* calleeMethod = method->clazz->super->vtable[BBBB] */
3396 case OP_INVOKE_SUPER_QUICK:
3397 case OP_INVOKE_SUPER_QUICK_RANGE: {
Ben Cheng7a2697d2010-06-07 13:44:23 -07003398 /* Grab the method ptr directly from what the interpreter sees */
3399 const Method *calleeMethod = mir->meta.callsiteInfo->method;
3400 assert(calleeMethod ==
3401 cUnit->method->clazz->super->vtable[dInsn->vB]);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003402
Dan Bornstein9a1f8162010-12-01 17:02:26 -08003403 if (mir->dalvikInsn.opcode == OP_INVOKE_SUPER_QUICK)
Ben Chengba4fc8b2009-06-01 13:00:29 -07003404 genProcessArgsNoRange(cUnit, mir, dInsn, &pcrLabel);
3405 else
3406 genProcessArgsRange(cUnit, mir, dInsn, &pcrLabel);
3407
3408 /* r0 = calleeMethod */
3409 loadConstant(cUnit, r0, (int) calleeMethod);
3410
Ben Cheng38329f52009-07-07 14:19:20 -07003411 genInvokeSingletonCommon(cUnit, mir, bb, labelList, pcrLabel,
3412 calleeMethod);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003413 break;
3414 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07003415 default:
3416 return true;
3417 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07003418 return false;
3419}
3420
3421/*
Bill Buzbeefd023aa2009-11-02 09:23:49 -08003422 * This operation is complex enough that we'll do it partly inline
3423 * and partly with a handler. NOTE: the handler uses hardcoded
3424 * values for string object offsets and must be revisitied if the
3425 * layout changes.
3426 */
3427static bool genInlinedCompareTo(CompilationUnit *cUnit, MIR *mir)
3428{
3429#if defined(USE_GLOBAL_STRING_DEFS)
Elliott Hughes7e914f12011-01-19 18:18:42 -08003430 return handleExecuteInlineC(cUnit, mir);
Bill Buzbeefd023aa2009-11-02 09:23:49 -08003431#else
3432 ArmLIR *rollback;
Bill Buzbeec6f10662010-02-09 11:16:15 -08003433 RegLocation rlThis = dvmCompilerGetSrc(cUnit, mir, 0);
3434 RegLocation rlComp = dvmCompilerGetSrc(cUnit, mir, 1);
Bill Buzbeefd023aa2009-11-02 09:23:49 -08003435
3436 loadValueDirectFixed(cUnit, rlThis, r0);
3437 loadValueDirectFixed(cUnit, rlComp, r1);
3438 /* Test objects for NULL */
3439 rollback = genNullCheck(cUnit, rlThis.sRegLow, r0, mir->offset, NULL);
3440 genNullCheck(cUnit, rlComp.sRegLow, r1, mir->offset, rollback);
3441 /*
3442 * TUNING: we could check for object pointer equality before invoking
3443 * handler. Unclear whether the gain would be worth the added code size
3444 * expansion.
3445 */
3446 genDispatchToHandler(cUnit, TEMPLATE_STRING_COMPARETO);
Bill Buzbeec6f10662010-02-09 11:16:15 -08003447 storeValue(cUnit, inlinedTarget(cUnit, mir, false),
3448 dvmCompilerGetReturn(cUnit));
Elliott Hughes7e914f12011-01-19 18:18:42 -08003449 return false;
Bill Buzbeefd023aa2009-11-02 09:23:49 -08003450#endif
3451}
3452
Elliott Hughes2bdbcb62010-04-12 14:29:37 -07003453static bool genInlinedFastIndexOf(CompilationUnit *cUnit, MIR *mir)
Bill Buzbeefd023aa2009-11-02 09:23:49 -08003454{
3455#if defined(USE_GLOBAL_STRING_DEFS)
Elliott Hughes7e914f12011-01-19 18:18:42 -08003456 return handleExecuteInlineC(cUnit, mir);
Bill Buzbeefd023aa2009-11-02 09:23:49 -08003457#else
Bill Buzbeec6f10662010-02-09 11:16:15 -08003458 RegLocation rlThis = dvmCompilerGetSrc(cUnit, mir, 0);
3459 RegLocation rlChar = dvmCompilerGetSrc(cUnit, mir, 1);
Bill Buzbeefd023aa2009-11-02 09:23:49 -08003460
3461 loadValueDirectFixed(cUnit, rlThis, r0);
3462 loadValueDirectFixed(cUnit, rlChar, r1);
Elliott Hughes2bdbcb62010-04-12 14:29:37 -07003463 RegLocation rlStart = dvmCompilerGetSrc(cUnit, mir, 2);
3464 loadValueDirectFixed(cUnit, rlStart, r2);
Bill Buzbeefd023aa2009-11-02 09:23:49 -08003465 /* Test objects for NULL */
3466 genNullCheck(cUnit, rlThis.sRegLow, r0, mir->offset, NULL);
3467 genDispatchToHandler(cUnit, TEMPLATE_STRING_INDEXOF);
Bill Buzbeec6f10662010-02-09 11:16:15 -08003468 storeValue(cUnit, inlinedTarget(cUnit, mir, false),
3469 dvmCompilerGetReturn(cUnit));
Elliott Hughes7e914f12011-01-19 18:18:42 -08003470 return false;
Bill Buzbeefd023aa2009-11-02 09:23:49 -08003471#endif
3472}
3473
Elliott Hughesee34f592010-04-05 18:13:52 -07003474// Generates an inlined String.isEmpty or String.length.
3475static bool genInlinedStringIsEmptyOrLength(CompilationUnit *cUnit, MIR *mir,
3476 bool isEmpty)
Bill Buzbee1f748632010-03-02 16:14:41 -08003477{
Elliott Hughesee34f592010-04-05 18:13:52 -07003478 // dst = src.length();
Bill Buzbee1f748632010-03-02 16:14:41 -08003479 RegLocation rlObj = dvmCompilerGetSrc(cUnit, mir, 0);
3480 RegLocation rlDest = inlinedTarget(cUnit, mir, false);
3481 rlObj = loadValue(cUnit, rlObj, kCoreReg);
3482 RegLocation rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
3483 genNullCheck(cUnit, rlObj.sRegLow, rlObj.lowReg, mir->offset, NULL);
3484 loadWordDisp(cUnit, rlObj.lowReg, gDvm.offJavaLangString_count,
3485 rlResult.lowReg);
Elliott Hughesee34f592010-04-05 18:13:52 -07003486 if (isEmpty) {
3487 // dst = (dst == 0);
3488 int tReg = dvmCompilerAllocTemp(cUnit);
3489 opRegReg(cUnit, kOpNeg, tReg, rlResult.lowReg);
3490 opRegRegReg(cUnit, kOpAdc, rlResult.lowReg, rlResult.lowReg, tReg);
3491 }
Bill Buzbee1f748632010-03-02 16:14:41 -08003492 storeValue(cUnit, rlDest, rlResult);
3493 return false;
3494}
3495
Elliott Hughesee34f592010-04-05 18:13:52 -07003496static bool genInlinedStringLength(CompilationUnit *cUnit, MIR *mir)
3497{
3498 return genInlinedStringIsEmptyOrLength(cUnit, mir, false);
3499}
3500
3501static bool genInlinedStringIsEmpty(CompilationUnit *cUnit, MIR *mir)
3502{
3503 return genInlinedStringIsEmptyOrLength(cUnit, mir, true);
3504}
3505
Bill Buzbee1f748632010-03-02 16:14:41 -08003506static bool genInlinedStringCharAt(CompilationUnit *cUnit, MIR *mir)
3507{
3508 int contents = offsetof(ArrayObject, contents);
3509 RegLocation rlObj = dvmCompilerGetSrc(cUnit, mir, 0);
3510 RegLocation rlIdx = dvmCompilerGetSrc(cUnit, mir, 1);
3511 RegLocation rlDest = inlinedTarget(cUnit, mir, false);
3512 RegLocation rlResult;
3513 rlObj = loadValue(cUnit, rlObj, kCoreReg);
3514 rlIdx = loadValue(cUnit, rlIdx, kCoreReg);
3515 int regMax = dvmCompilerAllocTemp(cUnit);
3516 int regOff = dvmCompilerAllocTemp(cUnit);
3517 int regPtr = dvmCompilerAllocTemp(cUnit);
3518 ArmLIR *pcrLabel = genNullCheck(cUnit, rlObj.sRegLow, rlObj.lowReg,
3519 mir->offset, NULL);
3520 loadWordDisp(cUnit, rlObj.lowReg, gDvm.offJavaLangString_count, regMax);
3521 loadWordDisp(cUnit, rlObj.lowReg, gDvm.offJavaLangString_offset, regOff);
3522 loadWordDisp(cUnit, rlObj.lowReg, gDvm.offJavaLangString_value, regPtr);
3523 genBoundsCheck(cUnit, rlIdx.lowReg, regMax, mir->offset, pcrLabel);
3524 dvmCompilerFreeTemp(cUnit, regMax);
3525 opRegImm(cUnit, kOpAdd, regPtr, contents);
3526 opRegReg(cUnit, kOpAdd, regOff, rlIdx.lowReg);
3527 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
3528 loadBaseIndexed(cUnit, regPtr, regOff, rlResult.lowReg, 1, kUnsignedHalf);
3529 storeValue(cUnit, rlDest, rlResult);
3530 return false;
3531}
3532
3533static bool genInlinedAbsInt(CompilationUnit *cUnit, MIR *mir)
3534{
3535 RegLocation rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
3536 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
Elliott Hughese22bd842010-08-20 18:47:36 -07003537 RegLocation rlDest = inlinedTarget(cUnit, mir, false);
Bill Buzbee1f748632010-03-02 16:14:41 -08003538 RegLocation rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
3539 int signReg = dvmCompilerAllocTemp(cUnit);
3540 /*
3541 * abs(x) = y<=x>>31, (x+y)^y.
3542 * Thumb2's IT block also yields 3 instructions, but imposes
3543 * scheduling constraints.
3544 */
3545 opRegRegImm(cUnit, kOpAsr, signReg, rlSrc.lowReg, 31);
3546 opRegRegReg(cUnit, kOpAdd, rlResult.lowReg, rlSrc.lowReg, signReg);
3547 opRegReg(cUnit, kOpXor, rlResult.lowReg, signReg);
3548 storeValue(cUnit, rlDest, rlResult);
3549 return false;
3550}
3551
3552static bool genInlinedAbsLong(CompilationUnit *cUnit, MIR *mir)
3553{
3554 RegLocation rlSrc = dvmCompilerGetSrcWide(cUnit, mir, 0, 1);
3555 RegLocation rlDest = inlinedTargetWide(cUnit, mir, false);
3556 rlSrc = loadValueWide(cUnit, rlSrc, kCoreReg);
3557 RegLocation rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
3558 int signReg = dvmCompilerAllocTemp(cUnit);
3559 /*
3560 * abs(x) = y<=x>>31, (x+y)^y.
3561 * Thumb2 IT block allows slightly shorter sequence,
3562 * but introduces a scheduling barrier. Stick with this
3563 * mechanism for now.
3564 */
3565 opRegRegImm(cUnit, kOpAsr, signReg, rlSrc.highReg, 31);
3566 opRegRegReg(cUnit, kOpAdd, rlResult.lowReg, rlSrc.lowReg, signReg);
3567 opRegRegReg(cUnit, kOpAdc, rlResult.highReg, rlSrc.highReg, signReg);
3568 opRegReg(cUnit, kOpXor, rlResult.lowReg, signReg);
3569 opRegReg(cUnit, kOpXor, rlResult.highReg, signReg);
3570 storeValueWide(cUnit, rlDest, rlResult);
3571 return false;
3572}
Bill Buzbeefd023aa2009-11-02 09:23:49 -08003573
Elliott Hughese22bd842010-08-20 18:47:36 -07003574static bool genInlinedIntFloatConversion(CompilationUnit *cUnit, MIR *mir)
3575{
3576 // Just move from source to destination...
3577 RegLocation rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
3578 RegLocation rlDest = inlinedTarget(cUnit, mir, false);
3579 storeValue(cUnit, rlDest, rlSrc);
3580 return false;
3581}
3582
3583static bool genInlinedLongDoubleConversion(CompilationUnit *cUnit, MIR *mir)
3584{
3585 // Just move from source to destination...
3586 RegLocation rlSrc = dvmCompilerGetSrcWide(cUnit, mir, 0, 1);
3587 RegLocation rlDest = inlinedTargetWide(cUnit, mir, false);
3588 storeValueWide(cUnit, rlDest, rlSrc);
3589 return false;
3590}
3591
Bill Buzbeefd023aa2009-11-02 09:23:49 -08003592/*
Elliott Hughes7e914f12011-01-19 18:18:42 -08003593 * JITs a call to a C function.
3594 * TODO: use this for faster native method invocation for simple native
3595 * methods (http://b/3069458).
3596 */
3597static bool handleExecuteInlineC(CompilationUnit *cUnit, MIR *mir)
3598{
3599 DecodedInstruction *dInsn = &mir->dalvikInsn;
3600 int operation = dInsn->vB;
3601 unsigned int i;
3602 const InlineOperation* inLineTable = dvmGetInlineOpsTable();
3603 uintptr_t fn = (int) inLineTable[operation].func;
3604 if (fn == 0) {
3605 dvmCompilerAbort(cUnit);
3606 }
3607 dvmCompilerFlushAllRegs(cUnit); /* Everything to home location */
3608 dvmCompilerClobberCallRegs(cUnit);
3609 dvmCompilerClobber(cUnit, r4PC);
3610 dvmCompilerClobber(cUnit, r7);
buzbee9f601a92011-02-11 17:48:20 -08003611 int offset = offsetof(Thread, retval);
Ben Cheng20d7e6c2011-02-18 17:12:42 -08003612 opRegRegImm(cUnit, kOpAdd, r4PC, r6SELF, offset);
Elliott Hughes7e914f12011-01-19 18:18:42 -08003613 opImm(cUnit, kOpPush, (1<<r4PC) | (1<<r7));
3614 LOAD_FUNC_ADDR(cUnit, r4PC, fn);
3615 genExportPC(cUnit, mir);
3616 for (i=0; i < dInsn->vA; i++) {
3617 loadValueDirect(cUnit, dvmCompilerGetSrc(cUnit, mir, i), i);
3618 }
3619 opReg(cUnit, kOpBlx, r4PC);
Ben Cheng20d7e6c2011-02-18 17:12:42 -08003620 opRegImm(cUnit, kOpAdd, r13sp, 8);
Elliott Hughes7e914f12011-01-19 18:18:42 -08003621 /* NULL? */
3622 ArmLIR *branchOver = genCmpImmBranch(cUnit, kArmCondNe, r0, 0);
3623 loadConstant(cUnit, r0, (int) (cUnit->method->insns + mir->offset));
3624 genDispatchToHandler(cUnit, TEMPLATE_THROW_EXCEPTION_COMMON);
3625 ArmLIR *target = newLIR0(cUnit, kArmPseudoTargetLabel);
3626 target->defMask = ENCODE_ALL;
3627 branchOver->generic.target = (LIR *) target;
3628 return false;
3629}
3630
3631/*
Bill Buzbeece46c942009-11-20 15:41:34 -08003632 * NOTE: Handles both range and non-range versions (arguments
3633 * have already been normalized by this point).
Ben Chengba4fc8b2009-06-01 13:00:29 -07003634 */
Bill Buzbeece46c942009-11-20 15:41:34 -08003635static bool handleExecuteInline(CompilationUnit *cUnit, MIR *mir)
Ben Chengba4fc8b2009-06-01 13:00:29 -07003636{
3637 DecodedInstruction *dInsn = &mir->dalvikInsn;
Elliott Hughes7e914f12011-01-19 18:18:42 -08003638 assert(dInsn->opcode == OP_EXECUTE_INLINE_RANGE ||
3639 dInsn->opcode == OP_EXECUTE_INLINE);
3640 switch (dInsn->vB) {
3641 case INLINE_EMPTYINLINEMETHOD:
3642 return false; /* Nop */
3643
3644 /* These ones we potentially JIT inline. */
3645 case INLINE_STRING_LENGTH:
3646 return genInlinedStringLength(cUnit, mir);
3647 case INLINE_STRING_IS_EMPTY:
3648 return genInlinedStringIsEmpty(cUnit, mir);
3649 case INLINE_MATH_ABS_INT:
3650 return genInlinedAbsInt(cUnit, mir);
3651 case INLINE_MATH_ABS_LONG:
3652 return genInlinedAbsLong(cUnit, mir);
3653 case INLINE_MATH_MIN_INT:
3654 return genInlinedMinMaxInt(cUnit, mir, true);
3655 case INLINE_MATH_MAX_INT:
3656 return genInlinedMinMaxInt(cUnit, mir, false);
3657 case INLINE_STRING_CHARAT:
3658 return genInlinedStringCharAt(cUnit, mir);
3659 case INLINE_MATH_SQRT:
3660 return genInlineSqrt(cUnit, mir);
3661 case INLINE_MATH_ABS_FLOAT:
3662 return genInlinedAbsFloat(cUnit, mir);
3663 case INLINE_MATH_ABS_DOUBLE:
3664 return genInlinedAbsDouble(cUnit, mir);
3665 case INLINE_STRING_COMPARETO:
3666 return genInlinedCompareTo(cUnit, mir);
3667 case INLINE_STRING_FASTINDEXOF_II:
3668 return genInlinedFastIndexOf(cUnit, mir);
3669 case INLINE_FLOAT_TO_RAW_INT_BITS:
3670 case INLINE_INT_BITS_TO_FLOAT:
3671 return genInlinedIntFloatConversion(cUnit, mir);
3672 case INLINE_DOUBLE_TO_RAW_LONG_BITS:
3673 case INLINE_LONG_BITS_TO_DOUBLE:
3674 return genInlinedLongDoubleConversion(cUnit, mir);
3675
3676 /*
3677 * These ones we just JIT a call to a C function for.
3678 * TODO: special-case these in the other "invoke" call paths.
3679 */
3680 case INLINE_STRING_EQUALS:
3681 case INLINE_MATH_COS:
3682 case INLINE_MATH_SIN:
3683 case INLINE_FLOAT_TO_INT_BITS:
3684 case INLINE_DOUBLE_TO_LONG_BITS:
3685 return handleExecuteInlineC(cUnit, mir);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003686 }
Elliott Hughes7e914f12011-01-19 18:18:42 -08003687 dvmCompilerAbort(cUnit);
3688 return false; // Not reachable; keeps compiler happy.
Ben Chengba4fc8b2009-06-01 13:00:29 -07003689}
3690
3691static bool handleFmt51l(CompilationUnit *cUnit, MIR *mir)
3692{
Bill Buzbee1465db52009-09-23 17:17:35 -07003693 //TUNING: We're using core regs here - not optimal when target is a double
Bill Buzbeec6f10662010-02-09 11:16:15 -08003694 RegLocation rlDest = dvmCompilerGetDestWide(cUnit, mir, 0, 1);
3695 RegLocation rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Ben Chengbd1326d2010-04-02 15:04:53 -07003696 loadConstantNoClobber(cUnit, rlResult.lowReg,
3697 mir->dalvikInsn.vB_wide & 0xFFFFFFFFUL);
3698 loadConstantNoClobber(cUnit, rlResult.highReg,
3699 (mir->dalvikInsn.vB_wide>>32) & 0xFFFFFFFFUL);
Bill Buzbee1465db52009-09-23 17:17:35 -07003700 storeValueWide(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003701 return false;
3702}
3703
Ben Chengba4fc8b2009-06-01 13:00:29 -07003704/*
3705 * The following are special processing routines that handle transfer of
3706 * controls between compiled code and the interpreter. Certain VM states like
3707 * Dalvik PC and special-purpose registers are reconstructed here.
3708 */
3709
Bill Buzbeebd047242010-05-13 13:02:53 -07003710/*
3711 * Insert a
3712 * b .+4
3713 * nop
3714 * pair at the beginning of a chaining cell. This serves as the
3715 * switch branch that selects between reverting to the interpreter or
3716 * not. Once the cell is chained to a translation, the cell will
3717 * contain a 32-bit branch. Subsequent chain/unchain operations will
3718 * then only alter that first 16-bits - the "b .+4" for unchaining,
3719 * and the restoration of the first half of the 32-bit branch for
3720 * rechaining.
3721 */
3722static void insertChainingSwitch(CompilationUnit *cUnit)
3723{
3724 ArmLIR *branch = newLIR0(cUnit, kThumbBUncond);
3725 newLIR2(cUnit, kThumbOrr, r0, r0);
3726 ArmLIR *target = newLIR0(cUnit, kArmPseudoTargetLabel);
3727 target->defMask = ENCODE_ALL;
3728 branch->generic.target = (LIR *) target;
3729}
3730
Ben Cheng1efc9c52009-06-08 18:25:27 -07003731/* Chaining cell for code that may need warmup. */
3732static void handleNormalChainingCell(CompilationUnit *cUnit,
3733 unsigned int offset)
Ben Chengba4fc8b2009-06-01 13:00:29 -07003734{
Ben Cheng11d8f142010-03-24 15:24:19 -07003735 /*
3736 * Use raw instruction constructors to guarantee that the generated
3737 * instructions fit the predefined cell size.
3738 */
Bill Buzbeebd047242010-05-13 13:02:53 -07003739 insertChainingSwitch(cUnit);
Ben Cheng20d7e6c2011-02-18 17:12:42 -08003740 newLIR3(cUnit, kThumbLdrRRI5, r0, r6SELF,
buzbee9f601a92011-02-11 17:48:20 -08003741 offsetof(Thread,
Ben Cheng11d8f142010-03-24 15:24:19 -07003742 jitToInterpEntries.dvmJitToInterpNormal) >> 2);
3743 newLIR1(cUnit, kThumbBlxR, r0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003744 addWordData(cUnit, (int) (cUnit->method->insns + offset), true);
3745}
3746
3747/*
Ben Cheng1efc9c52009-06-08 18:25:27 -07003748 * Chaining cell for instructions that immediately following already translated
3749 * code.
Ben Chengba4fc8b2009-06-01 13:00:29 -07003750 */
Ben Cheng1efc9c52009-06-08 18:25:27 -07003751static void handleHotChainingCell(CompilationUnit *cUnit,
3752 unsigned int offset)
Ben Chengba4fc8b2009-06-01 13:00:29 -07003753{
Ben Cheng11d8f142010-03-24 15:24:19 -07003754 /*
3755 * Use raw instruction constructors to guarantee that the generated
3756 * instructions fit the predefined cell size.
3757 */
Bill Buzbeebd047242010-05-13 13:02:53 -07003758 insertChainingSwitch(cUnit);
Ben Cheng20d7e6c2011-02-18 17:12:42 -08003759 newLIR3(cUnit, kThumbLdrRRI5, r0, r6SELF,
buzbee9f601a92011-02-11 17:48:20 -08003760 offsetof(Thread,
Ben Cheng11d8f142010-03-24 15:24:19 -07003761 jitToInterpEntries.dvmJitToInterpTraceSelect) >> 2);
3762 newLIR1(cUnit, kThumbBlxR, r0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003763 addWordData(cUnit, (int) (cUnit->method->insns + offset), true);
3764}
3765
Jeff Hao97319a82009-08-12 16:57:15 -07003766/* Chaining cell for branches that branch back into the same basic block */
3767static void handleBackwardBranchChainingCell(CompilationUnit *cUnit,
3768 unsigned int offset)
3769{
Ben Cheng11d8f142010-03-24 15:24:19 -07003770 /*
3771 * Use raw instruction constructors to guarantee that the generated
3772 * instructions fit the predefined cell size.
3773 */
Bill Buzbeebd047242010-05-13 13:02:53 -07003774 insertChainingSwitch(cUnit);
Bill Buzbee9c4b7c82009-09-10 10:10:38 -07003775#if defined(WITH_SELF_VERIFICATION)
Ben Cheng20d7e6c2011-02-18 17:12:42 -08003776 newLIR3(cUnit, kThumbLdrRRI5, r0, r6SELF,
buzbee9f601a92011-02-11 17:48:20 -08003777 offsetof(Thread,
Ben Cheng40094c12010-02-24 20:58:44 -08003778 jitToInterpEntries.dvmJitToInterpBackwardBranch) >> 2);
Bill Buzbee9c4b7c82009-09-10 10:10:38 -07003779#else
Ben Cheng20d7e6c2011-02-18 17:12:42 -08003780 newLIR3(cUnit, kThumbLdrRRI5, r0, r6SELF,
buzbee9f601a92011-02-11 17:48:20 -08003781 offsetof(Thread, jitToInterpEntries.dvmJitToInterpNormal) >> 2);
Bill Buzbee9c4b7c82009-09-10 10:10:38 -07003782#endif
Bill Buzbee1465db52009-09-23 17:17:35 -07003783 newLIR1(cUnit, kThumbBlxR, r0);
Jeff Hao97319a82009-08-12 16:57:15 -07003784 addWordData(cUnit, (int) (cUnit->method->insns + offset), true);
3785}
3786
Ben Chengba4fc8b2009-06-01 13:00:29 -07003787/* Chaining cell for monomorphic method invocations. */
Ben Cheng38329f52009-07-07 14:19:20 -07003788static void handleInvokeSingletonChainingCell(CompilationUnit *cUnit,
3789 const Method *callee)
Ben Chengba4fc8b2009-06-01 13:00:29 -07003790{
Ben Cheng11d8f142010-03-24 15:24:19 -07003791 /*
3792 * Use raw instruction constructors to guarantee that the generated
3793 * instructions fit the predefined cell size.
3794 */
Bill Buzbeebd047242010-05-13 13:02:53 -07003795 insertChainingSwitch(cUnit);
Ben Cheng20d7e6c2011-02-18 17:12:42 -08003796 newLIR3(cUnit, kThumbLdrRRI5, r0, r6SELF,
buzbee9f601a92011-02-11 17:48:20 -08003797 offsetof(Thread,
Ben Cheng11d8f142010-03-24 15:24:19 -07003798 jitToInterpEntries.dvmJitToInterpTraceSelect) >> 2);
3799 newLIR1(cUnit, kThumbBlxR, r0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003800 addWordData(cUnit, (int) (callee->insns), true);
3801}
3802
Ben Cheng38329f52009-07-07 14:19:20 -07003803/* Chaining cell for monomorphic method invocations. */
3804static void handleInvokePredictedChainingCell(CompilationUnit *cUnit)
3805{
3806
3807 /* Should not be executed in the initial state */
3808 addWordData(cUnit, PREDICTED_CHAIN_BX_PAIR_INIT, true);
3809 /* To be filled: class */
3810 addWordData(cUnit, PREDICTED_CHAIN_CLAZZ_INIT, true);
3811 /* To be filled: method */
3812 addWordData(cUnit, PREDICTED_CHAIN_METHOD_INIT, true);
3813 /*
3814 * Rechain count. The initial value of 0 here will trigger chaining upon
3815 * the first invocation of this callsite.
3816 */
3817 addWordData(cUnit, PREDICTED_CHAIN_COUNTER_INIT, true);
3818}
3819
Ben Chengba4fc8b2009-06-01 13:00:29 -07003820/* Load the Dalvik PC into r0 and jump to the specified target */
3821static void handlePCReconstruction(CompilationUnit *cUnit,
Bill Buzbee89efc3d2009-07-28 11:22:22 -07003822 ArmLIR *targetLabel)
Ben Chengba4fc8b2009-06-01 13:00:29 -07003823{
Bill Buzbee89efc3d2009-07-28 11:22:22 -07003824 ArmLIR **pcrLabel =
3825 (ArmLIR **) cUnit->pcReconstructionList.elemList;
Ben Chengba4fc8b2009-06-01 13:00:29 -07003826 int numElems = cUnit->pcReconstructionList.numUsed;
3827 int i;
3828 for (i = 0; i < numElems; i++) {
3829 dvmCompilerAppendLIR(cUnit, (LIR *) pcrLabel[i]);
3830 /* r0 = dalvik PC */
3831 loadConstant(cUnit, r0, pcrLabel[i]->operands[0]);
3832 genUnconditionalBranch(cUnit, targetLabel);
3833 }
3834}
3835
Bill Buzbee1465db52009-09-23 17:17:35 -07003836static char *extendedMIROpNames[kMirOpLast - kMirOpFirst] = {
3837 "kMirOpPhi",
3838 "kMirOpNullNRangeUpCheck",
3839 "kMirOpNullNRangeDownCheck",
3840 "kMirOpLowerBound",
3841 "kMirOpPunt",
Ben Cheng7a2697d2010-06-07 13:44:23 -07003842 "kMirOpCheckInlinePrediction",
Ben Cheng4238ec22009-08-24 16:32:22 -07003843};
3844
3845/*
3846 * vA = arrayReg;
3847 * vB = idxReg;
3848 * vC = endConditionReg;
3849 * arg[0] = maxC
3850 * arg[1] = minC
3851 * arg[2] = loopBranchConditionCode
3852 */
3853static void genHoistedChecksForCountUpLoop(CompilationUnit *cUnit, MIR *mir)
3854{
Bill Buzbee1465db52009-09-23 17:17:35 -07003855 /*
3856 * NOTE: these synthesized blocks don't have ssa names assigned
3857 * for Dalvik registers. However, because they dominate the following
3858 * blocks we can simply use the Dalvik name w/ subscript 0 as the
3859 * ssa name.
3860 */
Ben Cheng4238ec22009-08-24 16:32:22 -07003861 DecodedInstruction *dInsn = &mir->dalvikInsn;
3862 const int lenOffset = offsetof(ArrayObject, length);
Ben Cheng4238ec22009-08-24 16:32:22 -07003863 const int maxC = dInsn->arg[0];
Bill Buzbee1465db52009-09-23 17:17:35 -07003864 int regLength;
3865 RegLocation rlArray = cUnit->regLocation[mir->dalvikInsn.vA];
3866 RegLocation rlIdxEnd = cUnit->regLocation[mir->dalvikInsn.vC];
Ben Cheng4238ec22009-08-24 16:32:22 -07003867
3868 /* regArray <- arrayRef */
Bill Buzbee1465db52009-09-23 17:17:35 -07003869 rlArray = loadValue(cUnit, rlArray, kCoreReg);
3870 rlIdxEnd = loadValue(cUnit, rlIdxEnd, kCoreReg);
3871 genRegImmCheck(cUnit, kArmCondEq, rlArray.lowReg, 0, 0,
Ben Cheng4238ec22009-08-24 16:32:22 -07003872 (ArmLIR *) cUnit->loopAnalysis->branchToPCR);
3873
3874 /* regLength <- len(arrayRef) */
Bill Buzbeec6f10662010-02-09 11:16:15 -08003875 regLength = dvmCompilerAllocTemp(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07003876 loadWordDisp(cUnit, rlArray.lowReg, lenOffset, regLength);
Ben Cheng4238ec22009-08-24 16:32:22 -07003877
3878 int delta = maxC;
3879 /*
3880 * If the loop end condition is ">=" instead of ">", then the largest value
3881 * of the index is "endCondition - 1".
3882 */
3883 if (dInsn->arg[2] == OP_IF_GE) {
3884 delta--;
3885 }
3886
3887 if (delta) {
Bill Buzbeec6f10662010-02-09 11:16:15 -08003888 int tReg = dvmCompilerAllocTemp(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07003889 opRegRegImm(cUnit, kOpAdd, tReg, rlIdxEnd.lowReg, delta);
3890 rlIdxEnd.lowReg = tReg;
Bill Buzbeec6f10662010-02-09 11:16:15 -08003891 dvmCompilerFreeTemp(cUnit, tReg);
Ben Cheng4238ec22009-08-24 16:32:22 -07003892 }
3893 /* Punt if "regIdxEnd < len(Array)" is false */
Bill Buzbee1465db52009-09-23 17:17:35 -07003894 genRegRegCheck(cUnit, kArmCondGe, rlIdxEnd.lowReg, regLength, 0,
Ben Cheng0fd31e42009-09-03 14:40:16 -07003895 (ArmLIR *) cUnit->loopAnalysis->branchToPCR);
Ben Cheng4238ec22009-08-24 16:32:22 -07003896}
3897
3898/*
3899 * vA = arrayReg;
3900 * vB = idxReg;
3901 * vC = endConditionReg;
3902 * arg[0] = maxC
3903 * arg[1] = minC
3904 * arg[2] = loopBranchConditionCode
3905 */
3906static void genHoistedChecksForCountDownLoop(CompilationUnit *cUnit, MIR *mir)
3907{
3908 DecodedInstruction *dInsn = &mir->dalvikInsn;
3909 const int lenOffset = offsetof(ArrayObject, length);
Bill Buzbeec6f10662010-02-09 11:16:15 -08003910 const int regLength = dvmCompilerAllocTemp(cUnit);
Ben Cheng4238ec22009-08-24 16:32:22 -07003911 const int maxC = dInsn->arg[0];
Bill Buzbee1465db52009-09-23 17:17:35 -07003912 RegLocation rlArray = cUnit->regLocation[mir->dalvikInsn.vA];
3913 RegLocation rlIdxInit = cUnit->regLocation[mir->dalvikInsn.vB];
Ben Cheng4238ec22009-08-24 16:32:22 -07003914
3915 /* regArray <- arrayRef */
Bill Buzbee1465db52009-09-23 17:17:35 -07003916 rlArray = loadValue(cUnit, rlArray, kCoreReg);
3917 rlIdxInit = loadValue(cUnit, rlIdxInit, kCoreReg);
3918 genRegImmCheck(cUnit, kArmCondEq, rlArray.lowReg, 0, 0,
Ben Cheng4238ec22009-08-24 16:32:22 -07003919 (ArmLIR *) cUnit->loopAnalysis->branchToPCR);
3920
3921 /* regLength <- len(arrayRef) */
Bill Buzbee1465db52009-09-23 17:17:35 -07003922 loadWordDisp(cUnit, rlArray.lowReg, lenOffset, regLength);
Ben Cheng4238ec22009-08-24 16:32:22 -07003923
3924 if (maxC) {
Bill Buzbeec6f10662010-02-09 11:16:15 -08003925 int tReg = dvmCompilerAllocTemp(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07003926 opRegRegImm(cUnit, kOpAdd, tReg, rlIdxInit.lowReg, maxC);
3927 rlIdxInit.lowReg = tReg;
Bill Buzbeec6f10662010-02-09 11:16:15 -08003928 dvmCompilerFreeTemp(cUnit, tReg);
Ben Cheng4238ec22009-08-24 16:32:22 -07003929 }
3930
3931 /* Punt if "regIdxInit < len(Array)" is false */
Bill Buzbee1465db52009-09-23 17:17:35 -07003932 genRegRegCheck(cUnit, kArmCondGe, rlIdxInit.lowReg, regLength, 0,
Ben Cheng0fd31e42009-09-03 14:40:16 -07003933 (ArmLIR *) cUnit->loopAnalysis->branchToPCR);
Ben Cheng4238ec22009-08-24 16:32:22 -07003934}
3935
3936/*
3937 * vA = idxReg;
3938 * vB = minC;
3939 */
3940static void genHoistedLowerBoundCheck(CompilationUnit *cUnit, MIR *mir)
3941{
3942 DecodedInstruction *dInsn = &mir->dalvikInsn;
Ben Cheng4238ec22009-08-24 16:32:22 -07003943 const int minC = dInsn->vB;
Bill Buzbee1465db52009-09-23 17:17:35 -07003944 RegLocation rlIdx = cUnit->regLocation[mir->dalvikInsn.vA];
Ben Cheng4238ec22009-08-24 16:32:22 -07003945
3946 /* regIdx <- initial index value */
Bill Buzbee1465db52009-09-23 17:17:35 -07003947 rlIdx = loadValue(cUnit, rlIdx, kCoreReg);
Ben Cheng4238ec22009-08-24 16:32:22 -07003948
3949 /* Punt if "regIdxInit + minC >= 0" is false */
Bill Buzbee1465db52009-09-23 17:17:35 -07003950 genRegImmCheck(cUnit, kArmCondLt, rlIdx.lowReg, -minC, 0,
Ben Cheng4238ec22009-08-24 16:32:22 -07003951 (ArmLIR *) cUnit->loopAnalysis->branchToPCR);
3952}
3953
Ben Cheng7a2697d2010-06-07 13:44:23 -07003954/*
3955 * vC = this
3956 *
3957 * A predicted inlining target looks like the following, where instructions
3958 * between 0x4858de66 and 0x4858de72 are checking if the predicted class
3959 * matches "this", and the verificaion code is generated by this routine.
3960 *
3961 * (C) means the instruction is inlined from the callee, and (PI) means the
3962 * instruction is the predicted inlined invoke, whose corresponding
3963 * instructions are still generated to handle the mispredicted case.
3964 *
3965 * D/dalvikvm( 86): -------- kMirOpCheckInlinePrediction
3966 * D/dalvikvm( 86): 0x4858de66 (0002): ldr r0, [r5, #68]
3967 * D/dalvikvm( 86): 0x4858de68 (0004): ldr r1, [pc, #140]
3968 * D/dalvikvm( 86): 0x4858de6a (0006): cmp r0, #0
3969 * D/dalvikvm( 86): 0x4858de6c (0008): beq 0x4858deb2
3970 * D/dalvikvm( 86): 0x4858de6e (000a): ldr r2, [r0, #0]
3971 * D/dalvikvm( 86): 0x4858de70 (000c): cmp r1, r2
3972 * D/dalvikvm( 86): 0x4858de72 (000e): bne 0x4858de7a
3973 * D/dalvikvm( 86): -------- dalvik offset: 0x004c @ +iget-object-quick (C)
3974 * v4, v17, (#8)
3975 * D/dalvikvm( 86): 0x4858de74 (0010): ldr r3, [r0, #8]
3976 * D/dalvikvm( 86): 0x4858de76 (0012): str r3, [r5, #16]
3977 * D/dalvikvm( 86): -------- dalvik offset: 0x004c @
3978 * +invoke-virtual-quick/range (PI) v17..v17
3979 * D/dalvikvm( 86): 0x4858de78 (0014): b 0x4858debc
3980 * D/dalvikvm( 86): 0x4858de7a (0016): add r4,r5,#68
3981 * D/dalvikvm( 86): -------- BARRIER
3982 * D/dalvikvm( 86): 0x4858de7e (001a): ldmia r4, <r0>
3983 * D/dalvikvm( 86): -------- BARRIER
3984 * D/dalvikvm( 86): 0x4858de80 (001c): sub r7,r5,#24
3985 * D/dalvikvm( 86): 0x4858de84 (0020): cmp r0, #0
3986 * D/dalvikvm( 86): 0x4858de86 (0022): beq 0x4858deb6
3987 * D/dalvikvm( 86): -------- BARRIER
3988 * D/dalvikvm( 86): 0x4858de88 (0024): stmia r7, <r0>
3989 * D/dalvikvm( 86): -------- BARRIER
3990 * D/dalvikvm( 86): 0x4858de8a (0026): ldr r4, [pc, #104]
3991 * D/dalvikvm( 86): 0x4858de8c (0028): add r1, pc, #28
3992 * D/dalvikvm( 86): 0x4858de8e (002a): add r2, pc, #56
3993 * D/dalvikvm( 86): 0x4858de90 (002c): blx_1 0x48589198
3994 * D/dalvikvm( 86): 0x4858de92 (002e): blx_2 see above
3995 * D/dalvikvm( 86): 0x4858de94 (0030): b 0x4858dec8
3996 * D/dalvikvm( 86): 0x4858de96 (0032): b 0x4858deb6
3997 * D/dalvikvm( 86): 0x4858de98 (0034): ldr r0, [r7, #72]
3998 * D/dalvikvm( 86): 0x4858de9a (0036): cmp r1, #0
3999 * D/dalvikvm( 86): 0x4858de9c (0038): bgt 0x4858dea4
4000 * D/dalvikvm( 86): 0x4858de9e (003a): ldr r7, [r6, #116]
4001 * D/dalvikvm( 86): 0x4858dea0 (003c): movs r1, r6
4002 * D/dalvikvm( 86): 0x4858dea2 (003e): blx r7
4003 * D/dalvikvm( 86): 0x4858dea4 (0040): add r1, pc, #4
4004 * D/dalvikvm( 86): 0x4858dea6 (0042): blx_1 0x485890a0
4005 * D/dalvikvm( 86): 0x4858dea8 (0044): blx_2 see above
4006 * D/dalvikvm( 86): 0x4858deaa (0046): b 0x4858deb6
4007 * D/dalvikvm( 86): 0x4858deac (0048): .align4
4008 * D/dalvikvm( 86): L0x004f:
4009 * D/dalvikvm( 86): -------- dalvik offset: 0x004f @ move-result-object (PI)
4010 * v4, (#0), (#0)
4011 * D/dalvikvm( 86): 0x4858deac (0048): ldr r4, [r6, #8]
4012 * D/dalvikvm( 86): 0x4858deae (004a): str r4, [r5, #16]
4013 * D/dalvikvm( 86): 0x4858deb0 (004c): b 0x4858debc
4014 * D/dalvikvm( 86): -------- reconstruct dalvik PC : 0x42beefcc @ +0x004c
4015 * D/dalvikvm( 86): 0x4858deb2 (004e): ldr r0, [pc, #64]
4016 * D/dalvikvm( 86): 0x4858deb4 (0050): b 0x4858deb8
4017 * D/dalvikvm( 86): -------- reconstruct dalvik PC : 0x42beefcc @ +0x004c
4018 * D/dalvikvm( 86): 0x4858deb6 (0052): ldr r0, [pc, #60]
4019 * D/dalvikvm( 86): Exception_Handling:
4020 * D/dalvikvm( 86): 0x4858deb8 (0054): ldr r1, [r6, #100]
4021 * D/dalvikvm( 86): 0x4858deba (0056): blx r1
4022 * D/dalvikvm( 86): 0x4858debc (0058): .align4
4023 * D/dalvikvm( 86): -------- chaining cell (hot): 0x0050
4024 * D/dalvikvm( 86): 0x4858debc (0058): b 0x4858dec0
4025 * D/dalvikvm( 86): 0x4858debe (005a): orrs r0, r0
4026 * D/dalvikvm( 86): 0x4858dec0 (005c): ldr r0, [r6, #112]
4027 * D/dalvikvm( 86): 0x4858dec2 (005e): blx r0
4028 * D/dalvikvm( 86): 0x4858dec4 (0060): data 0xefd4(61396)
4029 * D/dalvikvm( 86): 0x4858dec6 (0062): data 0x42be(17086)
4030 * D/dalvikvm( 86): 0x4858dec8 (0064): .align4
4031 * D/dalvikvm( 86): -------- chaining cell (predicted)
4032 * D/dalvikvm( 86): 0x4858dec8 (0064): data 0xe7fe(59390)
4033 * D/dalvikvm( 86): 0x4858deca (0066): data 0x0000(0)
4034 * D/dalvikvm( 86): 0x4858decc (0068): data 0x0000(0)
4035 * D/dalvikvm( 86): 0x4858dece (006a): data 0x0000(0)
4036 * :
4037 */
4038static void genValidationForPredictedInline(CompilationUnit *cUnit, MIR *mir)
4039{
4040 CallsiteInfo *callsiteInfo = mir->meta.callsiteInfo;
4041 RegLocation rlThis = cUnit->regLocation[mir->dalvikInsn.vC];
4042
4043 rlThis = loadValue(cUnit, rlThis, kCoreReg);
4044 int regPredictedClass = dvmCompilerAllocTemp(cUnit);
4045 loadConstant(cUnit, regPredictedClass, (int) callsiteInfo->clazz);
4046 genNullCheck(cUnit, rlThis.sRegLow, rlThis.lowReg, mir->offset,
4047 NULL);/* null object? */
4048 int regActualClass = dvmCompilerAllocTemp(cUnit);
4049 loadWordDisp(cUnit, rlThis.lowReg, offsetof(Object, clazz), regActualClass);
4050 opRegReg(cUnit, kOpCmp, regPredictedClass, regActualClass);
4051 /*
4052 * Set the misPredBranchOver target so that it will be generated when the
4053 * code for the non-optimized invoke is generated.
4054 */
4055 callsiteInfo->misPredBranchOver = (LIR *) opCondBranch(cUnit, kArmCondNe);
4056}
4057
Ben Cheng4238ec22009-08-24 16:32:22 -07004058/* Extended MIR instructions like PHI */
4059static void handleExtendedMIR(CompilationUnit *cUnit, MIR *mir)
4060{
Dan Bornstein9a1f8162010-12-01 17:02:26 -08004061 int opOffset = mir->dalvikInsn.opcode - kMirOpFirst;
Carl Shapirofc75f3e2010-12-07 11:43:38 -08004062 char *msg = (char *)dvmCompilerNew(strlen(extendedMIROpNames[opOffset]) + 1,
4063 false);
Ben Cheng4238ec22009-08-24 16:32:22 -07004064 strcpy(msg, extendedMIROpNames[opOffset]);
Bill Buzbee1465db52009-09-23 17:17:35 -07004065 newLIR1(cUnit, kArmPseudoExtended, (int) msg);
Ben Cheng4238ec22009-08-24 16:32:22 -07004066
Dan Bornstein9a1f8162010-12-01 17:02:26 -08004067 switch (mir->dalvikInsn.opcode) {
Bill Buzbee1465db52009-09-23 17:17:35 -07004068 case kMirOpPhi: {
Ben Cheng4238ec22009-08-24 16:32:22 -07004069 char *ssaString = dvmCompilerGetSSAString(cUnit, mir->ssaRep);
Bill Buzbee1465db52009-09-23 17:17:35 -07004070 newLIR1(cUnit, kArmPseudoSSARep, (int) ssaString);
Ben Cheng4238ec22009-08-24 16:32:22 -07004071 break;
4072 }
Bill Buzbee1465db52009-09-23 17:17:35 -07004073 case kMirOpNullNRangeUpCheck: {
Ben Cheng4238ec22009-08-24 16:32:22 -07004074 genHoistedChecksForCountUpLoop(cUnit, mir);
4075 break;
4076 }
Bill Buzbee1465db52009-09-23 17:17:35 -07004077 case kMirOpNullNRangeDownCheck: {
Ben Cheng4238ec22009-08-24 16:32:22 -07004078 genHoistedChecksForCountDownLoop(cUnit, mir);
4079 break;
4080 }
Bill Buzbee1465db52009-09-23 17:17:35 -07004081 case kMirOpLowerBound: {
Ben Cheng4238ec22009-08-24 16:32:22 -07004082 genHoistedLowerBoundCheck(cUnit, mir);
4083 break;
4084 }
Bill Buzbee1465db52009-09-23 17:17:35 -07004085 case kMirOpPunt: {
Ben Cheng4238ec22009-08-24 16:32:22 -07004086 genUnconditionalBranch(cUnit,
4087 (ArmLIR *) cUnit->loopAnalysis->branchToPCR);
4088 break;
4089 }
Ben Cheng7a2697d2010-06-07 13:44:23 -07004090 case kMirOpCheckInlinePrediction: {
4091 genValidationForPredictedInline(cUnit, mir);
4092 break;
4093 }
Ben Cheng4238ec22009-08-24 16:32:22 -07004094 default:
4095 break;
4096 }
4097}
4098
4099/*
4100 * Create a PC-reconstruction cell for the starting offset of this trace.
4101 * Since the PCR cell is placed near the end of the compiled code which is
4102 * usually out of range for a conditional branch, we put two branches (one
4103 * branch over to the loop body and one layover branch to the actual PCR) at the
4104 * end of the entry block.
4105 */
4106static void setupLoopEntryBlock(CompilationUnit *cUnit, BasicBlock *entry,
4107 ArmLIR *bodyLabel)
4108{
4109 /* Set up the place holder to reconstruct this Dalvik PC */
Carl Shapirofc75f3e2010-12-07 11:43:38 -08004110 ArmLIR *pcrLabel = (ArmLIR *) dvmCompilerNew(sizeof(ArmLIR), true);
Dan Bornstein9a1f8162010-12-01 17:02:26 -08004111 pcrLabel->opcode = kArmPseudoPCReconstructionCell;
Ben Cheng4238ec22009-08-24 16:32:22 -07004112 pcrLabel->operands[0] =
4113 (int) (cUnit->method->insns + entry->startOffset);
4114 pcrLabel->operands[1] = entry->startOffset;
4115 /* Insert the place holder to the growable list */
Ben Cheng00603072010-10-28 11:13:58 -07004116 dvmInsertGrowableList(&cUnit->pcReconstructionList, (intptr_t) pcrLabel);
Ben Cheng4238ec22009-08-24 16:32:22 -07004117
4118 /*
4119 * Next, create two branches - one branch over to the loop body and the
4120 * other branch to the PCR cell to punt.
4121 */
Carl Shapirofc75f3e2010-12-07 11:43:38 -08004122 ArmLIR *branchToBody = (ArmLIR *) dvmCompilerNew(sizeof(ArmLIR), true);
Dan Bornstein9a1f8162010-12-01 17:02:26 -08004123 branchToBody->opcode = kThumbBUncond;
Ben Cheng4238ec22009-08-24 16:32:22 -07004124 branchToBody->generic.target = (LIR *) bodyLabel;
Ben Chengdcf3e5d2009-09-11 13:42:05 -07004125 setupResourceMasks(branchToBody);
Ben Cheng4238ec22009-08-24 16:32:22 -07004126 cUnit->loopAnalysis->branchToBody = (LIR *) branchToBody;
4127
Carl Shapirofc75f3e2010-12-07 11:43:38 -08004128 ArmLIR *branchToPCR = (ArmLIR *) dvmCompilerNew(sizeof(ArmLIR), true);
Dan Bornstein9a1f8162010-12-01 17:02:26 -08004129 branchToPCR->opcode = kThumbBUncond;
Ben Cheng4238ec22009-08-24 16:32:22 -07004130 branchToPCR->generic.target = (LIR *) pcrLabel;
Ben Chengdcf3e5d2009-09-11 13:42:05 -07004131 setupResourceMasks(branchToPCR);
Ben Cheng4238ec22009-08-24 16:32:22 -07004132 cUnit->loopAnalysis->branchToPCR = (LIR *) branchToPCR;
4133}
4134
Ben Chengd5adae12010-03-26 17:45:28 -07004135#if defined(WITH_SELF_VERIFICATION)
4136static bool selfVerificationPuntOps(MIR *mir)
4137{
4138 DecodedInstruction *decInsn = &mir->dalvikInsn;
Dan Bornstein9a1f8162010-12-01 17:02:26 -08004139 Opcode op = decInsn->opcode;
Ben Cheng7a2697d2010-06-07 13:44:23 -07004140
Ben Chengd5adae12010-03-26 17:45:28 -07004141 /*
4142 * All opcodes that can throw exceptions and use the
4143 * TEMPLATE_THROW_EXCEPTION_COMMON template should be excluded in the trace
4144 * under self-verification mode.
4145 */
4146 return (op == OP_MONITOR_ENTER || op == OP_MONITOR_EXIT ||
4147 op == OP_NEW_INSTANCE || op == OP_NEW_ARRAY ||
4148 op == OP_CHECK_CAST || op == OP_MOVE_EXCEPTION ||
4149 op == OP_FILL_ARRAY_DATA || op == OP_EXECUTE_INLINE ||
Ben Cheng7a2697d2010-06-07 13:44:23 -07004150 op == OP_EXECUTE_INLINE_RANGE);
Ben Chengd5adae12010-03-26 17:45:28 -07004151}
4152#endif
4153
Ben Chengba4fc8b2009-06-01 13:00:29 -07004154void dvmCompilerMIR2LIR(CompilationUnit *cUnit)
4155{
4156 /* Used to hold the labels of each block */
Bill Buzbee89efc3d2009-07-28 11:22:22 -07004157 ArmLIR *labelList =
Carl Shapirofc75f3e2010-12-07 11:43:38 -08004158 (ArmLIR *) dvmCompilerNew(sizeof(ArmLIR) * cUnit->numBlocks, true);
Ben Chengcec26f62010-01-15 15:29:33 -08004159 GrowableList chainingListByType[kChainingCellGap];
Ben Chengba4fc8b2009-06-01 13:00:29 -07004160 int i;
4161
4162 /*
Ben Cheng38329f52009-07-07 14:19:20 -07004163 * Initialize various types chaining lists.
Ben Chengba4fc8b2009-06-01 13:00:29 -07004164 */
Ben Chengcec26f62010-01-15 15:29:33 -08004165 for (i = 0; i < kChainingCellGap; i++) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07004166 dvmInitGrowableList(&chainingListByType[i], 2);
4167 }
4168
Ben Cheng7ab74e12011-02-03 14:02:06 -08004169 /* Clear the visited flag for each block */
4170 dvmCompilerDataFlowAnalysisDispatcher(cUnit, dvmCompilerClearVisitedFlag,
4171 kAllNodes, false /* isIterative */);
4172
Ben Cheng00603072010-10-28 11:13:58 -07004173 GrowableListIterator iterator;
4174 dvmGrowableListIteratorInit(&cUnit->blockList, &iterator);
Ben Chengba4fc8b2009-06-01 13:00:29 -07004175
buzbee2e152ba2010-12-15 16:32:35 -08004176 /* Traces start with a profiling entry point. Generate it here */
4177 cUnit->profileCodeSize = genTraceProfileEntry(cUnit);
Ben Cheng1efc9c52009-06-08 18:25:27 -07004178
Ben Chengba4fc8b2009-06-01 13:00:29 -07004179 /* Handle the content in each basic block */
Ben Cheng00603072010-10-28 11:13:58 -07004180 for (i = 0; ; i++) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07004181 MIR *mir;
Ben Cheng00603072010-10-28 11:13:58 -07004182 BasicBlock *bb = (BasicBlock *) dvmGrowableListIteratorNext(&iterator);
4183 if (bb == NULL) break;
Ben Cheng7ab74e12011-02-03 14:02:06 -08004184 if (bb->visited == true) continue;
Ben Chengba4fc8b2009-06-01 13:00:29 -07004185
Ben Cheng00603072010-10-28 11:13:58 -07004186 labelList[i].operands[0] = bb->startOffset;
Ben Chengba4fc8b2009-06-01 13:00:29 -07004187
Ben Cheng00603072010-10-28 11:13:58 -07004188 if (bb->blockType >= kChainingCellGap) {
4189 if (bb->isFallThroughFromInvoke == true) {
Ben Chengd44faf52010-06-02 15:33:51 -07004190 /* Align this block first since it is a return chaining cell */
4191 newLIR0(cUnit, kArmPseudoPseudoAlign4);
4192 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07004193 /*
4194 * Append the label pseudo LIR first. Chaining cells will be handled
4195 * separately afterwards.
4196 */
4197 dvmCompilerAppendLIR(cUnit, (LIR *) &labelList[i]);
4198 }
4199
Ben Cheng00603072010-10-28 11:13:58 -07004200 if (bb->blockType == kTraceEntryBlock) {
Dan Bornstein9a1f8162010-12-01 17:02:26 -08004201 labelList[i].opcode = kArmPseudoEntryBlock;
Ben Cheng00603072010-10-28 11:13:58 -07004202 if (bb->firstMIRInsn == NULL) {
Ben Cheng4238ec22009-08-24 16:32:22 -07004203 continue;
4204 } else {
Ben Cheng00603072010-10-28 11:13:58 -07004205 setupLoopEntryBlock(cUnit, bb,
4206 &labelList[bb->fallThrough->id]);
Ben Cheng4238ec22009-08-24 16:32:22 -07004207 }
Ben Cheng00603072010-10-28 11:13:58 -07004208 } else if (bb->blockType == kTraceExitBlock) {
Dan Bornstein9a1f8162010-12-01 17:02:26 -08004209 labelList[i].opcode = kArmPseudoExitBlock;
Ben Cheng4238ec22009-08-24 16:32:22 -07004210 goto gen_fallthrough;
Ben Cheng00603072010-10-28 11:13:58 -07004211 } else if (bb->blockType == kDalvikByteCode) {
Dan Bornstein9a1f8162010-12-01 17:02:26 -08004212 labelList[i].opcode = kArmPseudoNormalBlockLabel;
Ben Chenge9695e52009-06-16 16:11:47 -07004213 /* Reset the register state */
Bill Buzbeec6f10662010-02-09 11:16:15 -08004214 dvmCompilerResetRegPool(cUnit);
4215 dvmCompilerClobberAllRegs(cUnit);
4216 dvmCompilerResetNullCheck(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -07004217 } else {
Ben Cheng00603072010-10-28 11:13:58 -07004218 switch (bb->blockType) {
Bill Buzbee1465db52009-09-23 17:17:35 -07004219 case kChainingCellNormal:
Dan Bornstein9a1f8162010-12-01 17:02:26 -08004220 labelList[i].opcode = kArmPseudoChainingCellNormal;
Ben Chengba4fc8b2009-06-01 13:00:29 -07004221 /* handle the codegen later */
4222 dvmInsertGrowableList(
Ben Cheng00603072010-10-28 11:13:58 -07004223 &chainingListByType[kChainingCellNormal], i);
Ben Chengba4fc8b2009-06-01 13:00:29 -07004224 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07004225 case kChainingCellInvokeSingleton:
Dan Bornstein9a1f8162010-12-01 17:02:26 -08004226 labelList[i].opcode =
Ben Chenga4973592010-03-31 11:59:18 -07004227 kArmPseudoChainingCellInvokeSingleton;
Ben Chengba4fc8b2009-06-01 13:00:29 -07004228 labelList[i].operands[0] =
Ben Cheng00603072010-10-28 11:13:58 -07004229 (int) bb->containingMethod;
Ben Chengba4fc8b2009-06-01 13:00:29 -07004230 /* handle the codegen later */
4231 dvmInsertGrowableList(
Ben Cheng00603072010-10-28 11:13:58 -07004232 &chainingListByType[kChainingCellInvokeSingleton], i);
Ben Cheng38329f52009-07-07 14:19:20 -07004233 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07004234 case kChainingCellInvokePredicted:
Dan Bornstein9a1f8162010-12-01 17:02:26 -08004235 labelList[i].opcode =
Ben Chenga4973592010-03-31 11:59:18 -07004236 kArmPseudoChainingCellInvokePredicted;
Ben Cheng38329f52009-07-07 14:19:20 -07004237 /* handle the codegen later */
4238 dvmInsertGrowableList(
Ben Cheng00603072010-10-28 11:13:58 -07004239 &chainingListByType[kChainingCellInvokePredicted], i);
Ben Chengba4fc8b2009-06-01 13:00:29 -07004240 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07004241 case kChainingCellHot:
Dan Bornstein9a1f8162010-12-01 17:02:26 -08004242 labelList[i].opcode =
Ben Chenga4973592010-03-31 11:59:18 -07004243 kArmPseudoChainingCellHot;
Ben Chengba4fc8b2009-06-01 13:00:29 -07004244 /* handle the codegen later */
4245 dvmInsertGrowableList(
Ben Cheng00603072010-10-28 11:13:58 -07004246 &chainingListByType[kChainingCellHot], i);
Ben Chengba4fc8b2009-06-01 13:00:29 -07004247 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07004248 case kPCReconstruction:
Ben Chengba4fc8b2009-06-01 13:00:29 -07004249 /* Make sure exception handling block is next */
Dan Bornstein9a1f8162010-12-01 17:02:26 -08004250 labelList[i].opcode =
Ben Chenga4973592010-03-31 11:59:18 -07004251 kArmPseudoPCReconstructionBlockLabel;
Ben Chengba4fc8b2009-06-01 13:00:29 -07004252 assert (i == cUnit->numBlocks - 2);
4253 handlePCReconstruction(cUnit, &labelList[i+1]);
4254 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07004255 case kExceptionHandling:
Dan Bornstein9a1f8162010-12-01 17:02:26 -08004256 labelList[i].opcode = kArmPseudoEHBlockLabel;
Ben Chengba4fc8b2009-06-01 13:00:29 -07004257 if (cUnit->pcReconstructionList.numUsed) {
Ben Cheng20d7e6c2011-02-18 17:12:42 -08004258 loadWordDisp(cUnit, r6SELF, offsetof(Thread,
Bill Buzbee270c1d62009-08-13 16:58:07 -07004259 jitToInterpEntries.dvmJitToInterpPunt),
4260 r1);
Bill Buzbee1465db52009-09-23 17:17:35 -07004261 opReg(cUnit, kOpBlx, r1);
Ben Chengba4fc8b2009-06-01 13:00:29 -07004262 }
4263 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07004264 case kChainingCellBackwardBranch:
Dan Bornstein9a1f8162010-12-01 17:02:26 -08004265 labelList[i].opcode =
Ben Chenga4973592010-03-31 11:59:18 -07004266 kArmPseudoChainingCellBackwardBranch;
Jeff Hao97319a82009-08-12 16:57:15 -07004267 /* handle the codegen later */
4268 dvmInsertGrowableList(
Bill Buzbee1465db52009-09-23 17:17:35 -07004269 &chainingListByType[kChainingCellBackwardBranch],
Ben Cheng00603072010-10-28 11:13:58 -07004270 i);
Jeff Hao97319a82009-08-12 16:57:15 -07004271 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07004272 default:
4273 break;
4274 }
4275 continue;
4276 }
Ben Chenge9695e52009-06-16 16:11:47 -07004277
Bill Buzbee89efc3d2009-07-28 11:22:22 -07004278 ArmLIR *headLIR = NULL;
Ben Cheng7ab74e12011-02-03 14:02:06 -08004279 BasicBlock *nextBB = bb;
Ben Chenge9695e52009-06-16 16:11:47 -07004280
Ben Cheng7ab74e12011-02-03 14:02:06 -08004281 /*
4282 * Try to build a longer optimization unit. Currently if the previous
4283 * block ends with a goto, we continue adding instructions and don't
4284 * reset the register allocation pool.
4285 */
4286 for (; nextBB != NULL; nextBB = cUnit->nextCodegenBlock) {
4287 bb = nextBB;
4288 bb->visited = true;
4289 cUnit->nextCodegenBlock = NULL;
Bill Buzbee1465db52009-09-23 17:17:35 -07004290
Ben Cheng7ab74e12011-02-03 14:02:06 -08004291 for (mir = bb->firstMIRInsn; mir; mir = mir->next) {
Bill Buzbee1465db52009-09-23 17:17:35 -07004292
Ben Cheng7ab74e12011-02-03 14:02:06 -08004293 dvmCompilerResetRegPool(cUnit);
4294 if (gDvmJit.disableOpt & (1 << kTrackLiveTemps)) {
4295 dvmCompilerClobberAllRegs(cUnit);
Ben Cheng80211d22011-01-14 10:23:37 -08004296 }
Ben Cheng4238ec22009-08-24 16:32:22 -07004297
Ben Cheng7ab74e12011-02-03 14:02:06 -08004298 if (gDvmJit.disableOpt & (1 << kSuppressLoads)) {
4299 dvmCompilerResetDefTracking(cUnit);
4300 }
Ben Cheng4238ec22009-08-24 16:32:22 -07004301
Ben Cheng7ab74e12011-02-03 14:02:06 -08004302 if (mir->dalvikInsn.opcode >= kMirOpFirst) {
4303 handleExtendedMIR(cUnit, mir);
4304 continue;
4305 }
4306
4307
4308 Opcode dalvikOpcode = mir->dalvikInsn.opcode;
4309 InstructionFormat dalvikFormat =
4310 dexGetFormatFromOpcode(dalvikOpcode);
4311 char *note;
4312 if (mir->OptimizationFlags & MIR_INLINED) {
4313 note = " (I)";
4314 } else if (mir->OptimizationFlags & MIR_INLINED_PRED) {
4315 note = " (PI)";
4316 } else if (mir->OptimizationFlags & MIR_CALLEE) {
4317 note = " (C)";
4318 } else {
4319 note = NULL;
4320 }
4321
4322 ArmLIR *boundaryLIR;
4323
4324 /*
4325 * Don't generate the boundary LIR unless we are debugging this
4326 * trace or we need a scheduling barrier.
4327 */
4328 if (headLIR == NULL || cUnit->printMe == true) {
4329 boundaryLIR =
4330 newLIR2(cUnit, kArmPseudoDalvikByteCodeBoundary,
4331 mir->offset,
4332 (int) dvmCompilerGetDalvikDisassembly(
4333 &mir->dalvikInsn, note));
4334 /* Remember the first LIR for this block */
4335 if (headLIR == NULL) {
4336 headLIR = boundaryLIR;
4337 /* Set the first boundaryLIR as a scheduling barrier */
4338 headLIR->defMask = ENCODE_ALL;
4339 }
4340 }
4341
4342 /*
4343 * Don't generate the SSA annotation unless verbose mode is on
4344 */
4345 if (cUnit->printMe && mir->ssaRep) {
4346 char *ssaString = dvmCompilerGetSSAString(cUnit,
4347 mir->ssaRep);
4348 newLIR1(cUnit, kArmPseudoSSARep, (int) ssaString);
4349 }
4350
4351 bool notHandled;
4352 /*
4353 * Debugging: screen the opcode first to see if it is in the
4354 * do[-not]-compile list
4355 */
4356 bool singleStepMe = SINGLE_STEP_OP(dalvikOpcode);
Ben Chengd5adae12010-03-26 17:45:28 -07004357#if defined(WITH_SELF_VERIFICATION)
Ben Cheng7ab74e12011-02-03 14:02:06 -08004358 if (singleStepMe == false) {
4359 singleStepMe = selfVerificationPuntOps(mir);
4360 }
Ben Chengd5adae12010-03-26 17:45:28 -07004361#endif
Ben Cheng7ab74e12011-02-03 14:02:06 -08004362 if (singleStepMe || cUnit->allSingleStep) {
4363 notHandled = false;
4364 genInterpSingleStep(cUnit, mir);
4365 } else {
4366 opcodeCoverage[dalvikOpcode]++;
4367 switch (dalvikFormat) {
4368 case kFmt10t:
4369 case kFmt20t:
4370 case kFmt30t:
4371 notHandled = handleFmt10t_Fmt20t_Fmt30t(cUnit,
4372 mir, bb, labelList);
4373 break;
4374 case kFmt10x:
4375 notHandled = handleFmt10x(cUnit, mir);
4376 break;
4377 case kFmt11n:
4378 case kFmt31i:
4379 notHandled = handleFmt11n_Fmt31i(cUnit, mir);
4380 break;
4381 case kFmt11x:
4382 notHandled = handleFmt11x(cUnit, mir);
4383 break;
4384 case kFmt12x:
4385 notHandled = handleFmt12x(cUnit, mir);
4386 break;
4387 case kFmt20bc:
4388 case kFmt40sc:
4389 notHandled = handleFmt20bc_Fmt40sc(cUnit, mir);
4390 break;
4391 case kFmt21c:
4392 case kFmt31c:
4393 case kFmt41c:
4394 notHandled = handleFmt21c_Fmt31c_Fmt41c(cUnit, mir);
4395 break;
4396 case kFmt21h:
4397 notHandled = handleFmt21h(cUnit, mir);
4398 break;
4399 case kFmt21s:
4400 notHandled = handleFmt21s(cUnit, mir);
4401 break;
4402 case kFmt21t:
4403 notHandled = handleFmt21t(cUnit, mir, bb,
Ben Chengba4fc8b2009-06-01 13:00:29 -07004404 labelList);
Ben Cheng7ab74e12011-02-03 14:02:06 -08004405 break;
4406 case kFmt22b:
4407 case kFmt22s:
4408 notHandled = handleFmt22b_Fmt22s(cUnit, mir);
4409 break;
4410 case kFmt22c:
4411 case kFmt52c:
4412 notHandled = handleFmt22c_Fmt52c(cUnit, mir);
4413 break;
4414 case kFmt22cs:
4415 notHandled = handleFmt22cs(cUnit, mir);
4416 break;
4417 case kFmt22t:
4418 notHandled = handleFmt22t(cUnit, mir, bb,
4419 labelList);
4420 break;
4421 case kFmt22x:
4422 case kFmt32x:
4423 notHandled = handleFmt22x_Fmt32x(cUnit, mir);
4424 break;
4425 case kFmt23x:
4426 notHandled = handleFmt23x(cUnit, mir);
4427 break;
4428 case kFmt31t:
4429 notHandled = handleFmt31t(cUnit, mir);
4430 break;
4431 case kFmt3rc:
4432 case kFmt35c:
4433 case kFmt5rc:
4434 notHandled = handleFmt35c_3rc_5rc(cUnit, mir, bb,
4435 labelList);
4436 break;
4437 case kFmt3rms:
4438 case kFmt35ms:
4439 notHandled = handleFmt35ms_3rms(cUnit, mir, bb,
4440 labelList);
4441 break;
4442 case kFmt35mi:
4443 case kFmt3rmi:
4444 notHandled = handleExecuteInline(cUnit, mir);
4445 break;
4446 case kFmt51l:
4447 notHandled = handleFmt51l(cUnit, mir);
4448 break;
4449 default:
4450 notHandled = true;
4451 break;
4452 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07004453 }
Ben Cheng7ab74e12011-02-03 14:02:06 -08004454 if (notHandled) {
4455 LOGE("%#06x: Opcode 0x%x (%s) / Fmt %d not handled\n",
4456 mir->offset,
4457 dalvikOpcode, dexGetOpcodeName(dalvikOpcode),
4458 dalvikFormat);
4459 dvmCompilerAbort(cUnit);
4460 break;
4461 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07004462 }
4463 }
Ben Cheng4238ec22009-08-24 16:32:22 -07004464
Ben Cheng00603072010-10-28 11:13:58 -07004465 if (bb->blockType == kTraceEntryBlock) {
Ben Cheng4238ec22009-08-24 16:32:22 -07004466 dvmCompilerAppendLIR(cUnit,
4467 (LIR *) cUnit->loopAnalysis->branchToBody);
4468 dvmCompilerAppendLIR(cUnit,
4469 (LIR *) cUnit->loopAnalysis->branchToPCR);
4470 }
4471
4472 if (headLIR) {
4473 /*
4474 * Eliminate redundant loads/stores and delay stores into later
4475 * slots
4476 */
4477 dvmCompilerApplyLocalOptimizations(cUnit, (LIR *) headLIR,
4478 cUnit->lastLIRInsn);
4479 }
4480
4481gen_fallthrough:
Ben Cheng1efc9c52009-06-08 18:25:27 -07004482 /*
4483 * Check if the block is terminated due to trace length constraint -
4484 * insert an unconditional branch to the chaining cell.
4485 */
Ben Cheng00603072010-10-28 11:13:58 -07004486 if (bb->needFallThroughBranch) {
Ben Cheng7ab74e12011-02-03 14:02:06 -08004487 genUnconditionalBranch(cUnit, &labelList[bb->fallThrough->id]);
Ben Cheng1efc9c52009-06-08 18:25:27 -07004488 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07004489 }
4490
Ben Chenge9695e52009-06-16 16:11:47 -07004491 /* Handle the chaining cells in predefined order */
Ben Chengcec26f62010-01-15 15:29:33 -08004492 for (i = 0; i < kChainingCellGap; i++) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07004493 size_t j;
4494 int *blockIdList = (int *) chainingListByType[i].elemList;
4495
4496 cUnit->numChainingCells[i] = chainingListByType[i].numUsed;
4497
4498 /* No chaining cells of this type */
4499 if (cUnit->numChainingCells[i] == 0)
4500 continue;
4501
4502 /* Record the first LIR for a new type of chaining cell */
4503 cUnit->firstChainingLIR[i] = (LIR *) &labelList[blockIdList[0]];
4504
4505 for (j = 0; j < chainingListByType[i].numUsed; j++) {
4506 int blockId = blockIdList[j];
Ben Cheng00603072010-10-28 11:13:58 -07004507 BasicBlock *chainingBlock =
4508 (BasicBlock *) dvmGrowableListGetElement(&cUnit->blockList,
4509 blockId);
Ben Chengba4fc8b2009-06-01 13:00:29 -07004510
4511 /* Align this chaining cell first */
Bill Buzbee1465db52009-09-23 17:17:35 -07004512 newLIR0(cUnit, kArmPseudoPseudoAlign4);
Ben Chengba4fc8b2009-06-01 13:00:29 -07004513
4514 /* Insert the pseudo chaining instruction */
4515 dvmCompilerAppendLIR(cUnit, (LIR *) &labelList[blockId]);
4516
4517
Ben Cheng00603072010-10-28 11:13:58 -07004518 switch (chainingBlock->blockType) {
Bill Buzbee1465db52009-09-23 17:17:35 -07004519 case kChainingCellNormal:
Ben Cheng00603072010-10-28 11:13:58 -07004520 handleNormalChainingCell(cUnit, chainingBlock->startOffset);
Ben Chengba4fc8b2009-06-01 13:00:29 -07004521 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07004522 case kChainingCellInvokeSingleton:
Ben Cheng38329f52009-07-07 14:19:20 -07004523 handleInvokeSingletonChainingCell(cUnit,
Ben Cheng00603072010-10-28 11:13:58 -07004524 chainingBlock->containingMethod);
Ben Chengba4fc8b2009-06-01 13:00:29 -07004525 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07004526 case kChainingCellInvokePredicted:
Ben Cheng38329f52009-07-07 14:19:20 -07004527 handleInvokePredictedChainingCell(cUnit);
4528 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07004529 case kChainingCellHot:
Ben Cheng00603072010-10-28 11:13:58 -07004530 handleHotChainingCell(cUnit, chainingBlock->startOffset);
Ben Chengba4fc8b2009-06-01 13:00:29 -07004531 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07004532 case kChainingCellBackwardBranch:
Jeff Hao97319a82009-08-12 16:57:15 -07004533 handleBackwardBranchChainingCell(cUnit,
Ben Cheng00603072010-10-28 11:13:58 -07004534 chainingBlock->startOffset);
Jeff Hao97319a82009-08-12 16:57:15 -07004535 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07004536 default:
Ben Cheng00603072010-10-28 11:13:58 -07004537 LOGE("Bad blocktype %d", chainingBlock->blockType);
Bill Buzbeefc519dc2010-03-06 23:30:57 -08004538 dvmCompilerAbort(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -07004539 }
4540 }
4541 }
Ben Chenge9695e52009-06-16 16:11:47 -07004542
Ben Chengcec26f62010-01-15 15:29:33 -08004543 /* Mark the bottom of chaining cells */
4544 cUnit->chainingCellBottom = (LIR *) newLIR0(cUnit, kArmChainingCellBottom);
4545
Ben Cheng6c10a972009-10-29 14:39:18 -07004546 /*
4547 * Generate the branch to the dvmJitToInterpNoChain entry point at the end
4548 * of all chaining cells for the overflow cases.
4549 */
4550 if (cUnit->switchOverflowPad) {
4551 loadConstant(cUnit, r0, (int) cUnit->switchOverflowPad);
Ben Cheng20d7e6c2011-02-18 17:12:42 -08004552 loadWordDisp(cUnit, r6SELF, offsetof(Thread,
Ben Cheng6c10a972009-10-29 14:39:18 -07004553 jitToInterpEntries.dvmJitToInterpNoChain), r2);
4554 opRegReg(cUnit, kOpAdd, r1, r1);
4555 opRegRegReg(cUnit, kOpAdd, r4PC, r0, r1);
Ben Cheng978738d2010-05-13 13:45:57 -07004556#if defined(WITH_JIT_TUNING)
Ben Cheng6c10a972009-10-29 14:39:18 -07004557 loadConstant(cUnit, r0, kSwitchOverflow);
4558#endif
4559 opReg(cUnit, kOpBlx, r2);
4560 }
4561
Ben Chenge9695e52009-06-16 16:11:47 -07004562 dvmCompilerApplyGlobalOptimizations(cUnit);
jeffhao9e45c0b2010-02-03 10:24:05 -08004563
4564#if defined(WITH_SELF_VERIFICATION)
4565 selfVerificationBranchInsertPass(cUnit);
4566#endif
Ben Chengba4fc8b2009-06-01 13:00:29 -07004567}
4568
buzbee2e152ba2010-12-15 16:32:35 -08004569/*
4570 * Accept the work and start compiling. Returns true if compilation
4571 * is attempted.
4572 */
Bill Buzbee716f1202009-07-23 13:22:09 -07004573bool dvmCompilerDoWork(CompilerWorkOrder *work)
Ben Chengba4fc8b2009-06-01 13:00:29 -07004574{
Carl Shapirofc75f3e2010-12-07 11:43:38 -08004575 JitTraceDescription *desc;
buzbee2e152ba2010-12-15 16:32:35 -08004576 bool isCompile;
4577 bool success = true;
Ben Chengba4fc8b2009-06-01 13:00:29 -07004578
Ben Cheng6999d842010-01-26 16:46:15 -08004579 if (gDvmJit.codeCacheFull) {
Ben Chengccd6c012009-10-15 14:52:45 -07004580 return false;
4581 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07004582
Ben Chengccd6c012009-10-15 14:52:45 -07004583 switch (work->kind) {
Ben Chengccd6c012009-10-15 14:52:45 -07004584 case kWorkOrderTrace:
buzbee2e152ba2010-12-15 16:32:35 -08004585 isCompile = true;
Ben Chengccd6c012009-10-15 14:52:45 -07004586 /* Start compilation with maximally allowed trace length */
Carl Shapirofc75f3e2010-12-07 11:43:38 -08004587 desc = (JitTraceDescription *)work->info;
buzbee2e152ba2010-12-15 16:32:35 -08004588 success = dvmCompileTrace(desc, JIT_MAX_TRACE_LEN, &work->result,
4589 work->bailPtr, 0 /* no hints */);
Ben Chengccd6c012009-10-15 14:52:45 -07004590 break;
4591 case kWorkOrderTraceDebug: {
4592 bool oldPrintMe = gDvmJit.printMe;
4593 gDvmJit.printMe = true;
buzbee2e152ba2010-12-15 16:32:35 -08004594 isCompile = true;
Ben Chengccd6c012009-10-15 14:52:45 -07004595 /* Start compilation with maximally allowed trace length */
Carl Shapirofc75f3e2010-12-07 11:43:38 -08004596 desc = (JitTraceDescription *)work->info;
buzbee2e152ba2010-12-15 16:32:35 -08004597 success = dvmCompileTrace(desc, JIT_MAX_TRACE_LEN, &work->result,
4598 work->bailPtr, 0 /* no hints */);
Elliott Hughes672511b2010-04-26 17:40:13 -07004599 gDvmJit.printMe = oldPrintMe;
Ben Chengccd6c012009-10-15 14:52:45 -07004600 break;
4601 }
buzbee2e152ba2010-12-15 16:32:35 -08004602 case kWorkOrderProfileMode:
4603 dvmJitChangeProfileMode((TraceProfilingModes)work->info);
4604 isCompile = false;
4605 break;
Ben Chengccd6c012009-10-15 14:52:45 -07004606 default:
buzbee2e152ba2010-12-15 16:32:35 -08004607 isCompile = false;
Bill Buzbeefc519dc2010-03-06 23:30:57 -08004608 LOGE("Jit: unknown work order type");
Elliott Hughes672511b2010-04-26 17:40:13 -07004609 assert(0); // Bail if debug build, discard otherwise
Ben Chengccd6c012009-10-15 14:52:45 -07004610 }
buzbee2e152ba2010-12-15 16:32:35 -08004611 if (!success)
4612 work->result.codeAddress = NULL;
4613 return isCompile;
Ben Chengba4fc8b2009-06-01 13:00:29 -07004614}
4615
Ben Chengba4fc8b2009-06-01 13:00:29 -07004616/* Architectural-specific debugging helpers go here */
4617void dvmCompilerArchDump(void)
4618{
4619 /* Print compiled opcode in this VM instance */
4620 int i, start, streak;
4621 char buf[1024];
4622
4623 streak = i = 0;
4624 buf[0] = 0;
Dan Bornsteinccaab182010-12-03 15:32:40 -08004625 while (opcodeCoverage[i] == 0 && i < kNumPackedOpcodes) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07004626 i++;
4627 }
Dan Bornsteinccaab182010-12-03 15:32:40 -08004628 if (i == kNumPackedOpcodes) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07004629 return;
4630 }
Dan Bornsteinccaab182010-12-03 15:32:40 -08004631 for (start = i++, streak = 1; i < kNumPackedOpcodes; i++) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07004632 if (opcodeCoverage[i]) {
4633 streak++;
4634 } else {
4635 if (streak == 1) {
4636 sprintf(buf+strlen(buf), "%x,", start);
4637 } else {
4638 sprintf(buf+strlen(buf), "%x-%x,", start, start + streak - 1);
4639 }
4640 streak = 0;
Dan Bornsteinccaab182010-12-03 15:32:40 -08004641 while (opcodeCoverage[i] == 0 && i < kNumPackedOpcodes) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07004642 i++;
4643 }
Dan Bornsteinccaab182010-12-03 15:32:40 -08004644 if (i < kNumPackedOpcodes) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07004645 streak = 1;
4646 start = i;
4647 }
4648 }
4649 }
4650 if (streak) {
4651 if (streak == 1) {
4652 sprintf(buf+strlen(buf), "%x", start);
4653 } else {
4654 sprintf(buf+strlen(buf), "%x-%x", start, start + streak - 1);
4655 }
4656 }
4657 if (strlen(buf)) {
Ben Cheng8b258bf2009-06-24 17:27:07 -07004658 LOGD("dalvik.vm.jit.op = %s", buf);
Ben Chengba4fc8b2009-06-01 13:00:29 -07004659 }
4660}
Ben Chengd7d426a2009-09-22 11:23:36 -07004661
4662/* Common initialization routine for an architecture family */
4663bool dvmCompilerArchInit()
4664{
4665 int i;
4666
Bill Buzbee1465db52009-09-23 17:17:35 -07004667 for (i = 0; i < kArmLast; i++) {
Dan Bornstein9a1f8162010-12-01 17:02:26 -08004668 if (EncodingMap[i].opcode != i) {
Ben Chengd7d426a2009-09-22 11:23:36 -07004669 LOGE("Encoding order for %s is wrong: expecting %d, seeing %d",
Dan Bornstein9a1f8162010-12-01 17:02:26 -08004670 EncodingMap[i].name, i, EncodingMap[i].opcode);
Bill Buzbeefc519dc2010-03-06 23:30:57 -08004671 dvmAbort(); // OK to dvmAbort - build error
Ben Chengd7d426a2009-09-22 11:23:36 -07004672 }
4673 }
4674
Ben Cheng5d90c202009-11-22 23:31:11 -08004675 return dvmCompilerArchVariantInit();
4676}
4677
4678void *dvmCompilerGetInterpretTemplate()
4679{
4680 return (void*) ((int)gDvmJit.codeCache +
4681 templateEntryOffsets[TEMPLATE_INTERPRET]);
4682}
4683
Bill Buzbee1b3da592011-02-03 07:38:22 -08004684JitInstructionSetType dvmCompilerGetInterpretTemplateSet()
4685{
4686 return DALVIK_JIT_ARM;
4687}
4688
buzbeebff121a2010-08-04 15:25:06 -07004689/* Needed by the Assembler */
4690void dvmCompilerSetupResourceMasks(ArmLIR *lir)
4691{
4692 setupResourceMasks(lir);
4693}
4694
Ben Cheng5d90c202009-11-22 23:31:11 -08004695/* Needed by the ld/st optmizatons */
4696ArmLIR* dvmCompilerRegCopyNoInsert(CompilationUnit *cUnit, int rDest, int rSrc)
4697{
4698 return genRegCopyNoInsert(cUnit, rDest, rSrc);
4699}
4700
4701/* Needed by the register allocator */
4702ArmLIR* dvmCompilerRegCopy(CompilationUnit *cUnit, int rDest, int rSrc)
4703{
4704 return genRegCopy(cUnit, rDest, rSrc);
4705}
4706
4707/* Needed by the register allocator */
4708void dvmCompilerRegCopyWide(CompilationUnit *cUnit, int destLo, int destHi,
4709 int srcLo, int srcHi)
4710{
4711 genRegCopyWide(cUnit, destLo, destHi, srcLo, srcHi);
4712}
4713
4714void dvmCompilerFlushRegImpl(CompilationUnit *cUnit, int rBase,
4715 int displacement, int rSrc, OpSize size)
4716{
4717 storeBaseDisp(cUnit, rBase, displacement, rSrc, size);
4718}
4719
4720void dvmCompilerFlushRegWideImpl(CompilationUnit *cUnit, int rBase,
4721 int displacement, int rSrcLo, int rSrcHi)
4722{
4723 storeBaseDispWide(cUnit, rBase, displacement, rSrcLo, rSrcHi);
Ben Chengd7d426a2009-09-22 11:23:36 -07004724}