blob: 2937cf24102841639f286c080ea242d2232e71cc [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 Cheng04517042011-03-14 11:16:21 -07001190 /*
1191 * For verbose printing, store the method pointer in operands[1] first as
1192 * operands[0] will be clobbered in dvmCompilerMIR2LIR.
1193 */
1194 predChainingCell->operands[1] = (int) mir->meta.callsiteInfo->method;
1195
Ben Cheng38329f52009-07-07 14:19:20 -07001196 /* "this" is already left in r0 by genProcessArgs* */
1197
1198 /* r4PC = dalvikCallsite */
1199 loadConstant(cUnit, r4PC,
1200 (int) (cUnit->method->insns + mir->offset));
1201
1202 /* r1 = &retChainingCell */
Ben Cheng20d7e6c2011-02-18 17:12:42 -08001203 ArmLIR *addrRetChain = opRegRegImm(cUnit, kOpAdd, r1, r15pc, 0);
Ben Cheng38329f52009-07-07 14:19:20 -07001204 addrRetChain->generic.target = (LIR *) retChainingCell;
1205
1206 /* r2 = &predictedChainingCell */
Ben Cheng20d7e6c2011-02-18 17:12:42 -08001207 ArmLIR *predictedChainingCell = opRegRegImm(cUnit, kOpAdd, r2, r15pc, 0);
Ben Cheng38329f52009-07-07 14:19:20 -07001208 predictedChainingCell->generic.target = (LIR *) predChainingCell;
1209
buzbee18fba342011-01-19 15:31:15 -08001210 genDispatchToHandler(cUnit, gDvmJit.methodTraceSupport ?
1211 TEMPLATE_INVOKE_METHOD_PREDICTED_CHAIN_PROF :
1212 TEMPLATE_INVOKE_METHOD_PREDICTED_CHAIN);
Ben Cheng38329f52009-07-07 14:19:20 -07001213
1214 /* return through lr - jump to the chaining cell */
1215 genUnconditionalBranch(cUnit, predChainingCell);
1216
1217 /*
1218 * null-check on "this" may have been eliminated, but we still need a PC-
1219 * reconstruction label for stack overflow bailout.
1220 */
1221 if (pcrLabel == NULL) {
1222 int dPC = (int) (cUnit->method->insns + mir->offset);
Carl Shapirofc75f3e2010-12-07 11:43:38 -08001223 pcrLabel = (ArmLIR *) dvmCompilerNew(sizeof(ArmLIR), true);
Dan Bornstein9a1f8162010-12-01 17:02:26 -08001224 pcrLabel->opcode = kArmPseudoPCReconstructionCell;
Ben Cheng38329f52009-07-07 14:19:20 -07001225 pcrLabel->operands[0] = dPC;
1226 pcrLabel->operands[1] = mir->offset;
1227 /* Insert the place holder to the growable list */
Ben Cheng00603072010-10-28 11:13:58 -07001228 dvmInsertGrowableList(&cUnit->pcReconstructionList,
1229 (intptr_t) pcrLabel);
Ben Cheng38329f52009-07-07 14:19:20 -07001230 }
1231
1232 /* return through lr+2 - punt to the interpreter */
1233 genUnconditionalBranch(cUnit, pcrLabel);
1234
1235 /*
1236 * return through lr+4 - fully resolve the callee method.
1237 * r1 <- count
1238 * r2 <- &predictedChainCell
1239 * r3 <- this->class
1240 * r4 <- dPC
1241 * r7 <- this->class->vtable
1242 */
1243
1244 /* r0 <- calleeMethod */
Bill Buzbee270c1d62009-08-13 16:58:07 -07001245 loadWordDisp(cUnit, r7, methodIndex * 4, r0);
Ben Cheng38329f52009-07-07 14:19:20 -07001246
1247 /* Check if rechain limit is reached */
buzbee8f8109a2010-08-31 10:16:35 -07001248 ArmLIR *bypassRechaining = genCmpImmBranch(cUnit, kArmCondGt, r1, 0);
Ben Cheng38329f52009-07-07 14:19:20 -07001249
Ben Chengaf5aa1f2011-01-04 15:37:04 -08001250 LOAD_FUNC_ADDR(cUnit, r7, (int) dvmJitToPatchPredictedChain);
Ben Cheng38329f52009-07-07 14:19:20 -07001251
Ben Cheng20d7e6c2011-02-18 17:12:42 -08001252 genRegCopy(cUnit, r1, r6SELF);
Ben Chengb88ec3c2010-05-17 12:50:33 -07001253
Ben Cheng38329f52009-07-07 14:19:20 -07001254 /*
1255 * r0 = calleeMethod
1256 * r2 = &predictedChainingCell
1257 * r3 = class
1258 *
1259 * &returnChainingCell has been loaded into r1 but is not needed
1260 * when patching the chaining cell and will be clobbered upon
1261 * returning so it will be reconstructed again.
1262 */
Bill Buzbee1465db52009-09-23 17:17:35 -07001263 opReg(cUnit, kOpBlx, r7);
Ben Cheng38329f52009-07-07 14:19:20 -07001264
1265 /* r1 = &retChainingCell */
Ben Cheng20d7e6c2011-02-18 17:12:42 -08001266 addrRetChain = opRegRegImm(cUnit, kOpAdd, r1, r15pc, 0);
Ben Cheng38329f52009-07-07 14:19:20 -07001267 addrRetChain->generic.target = (LIR *) retChainingCell;
1268
1269 bypassRechaining->generic.target = (LIR *) addrRetChain;
1270 /*
1271 * r0 = calleeMethod,
1272 * r1 = &ChainingCell,
1273 * r4PC = callsiteDPC,
1274 */
buzbee18fba342011-01-19 15:31:15 -08001275 genDispatchToHandler(cUnit, gDvmJit.methodTraceSupport ?
1276 TEMPLATE_INVOKE_METHOD_NO_OPT_PROF :
1277 TEMPLATE_INVOKE_METHOD_NO_OPT);
Ben Cheng978738d2010-05-13 13:45:57 -07001278#if defined(WITH_JIT_TUNING)
Ben Cheng86717f72010-03-05 15:27:21 -08001279 gDvmJit.invokePolymorphic++;
Ben Cheng38329f52009-07-07 14:19:20 -07001280#endif
1281 /* Handle exceptions using the interpreter */
1282 genTrap(cUnit, mir->offset, pcrLabel);
1283}
1284
Ben Cheng20d7e6c2011-02-18 17:12:42 -08001285/* "this" pointer is already in r0 */
1286static void genInvokeVirtualWholeMethod(CompilationUnit *cUnit,
1287 MIR *mir,
1288 void *calleeAddr,
1289 ArmLIR *retChainingCell)
1290{
1291 CallsiteInfo *callsiteInfo = mir->meta.callsiteInfo;
1292 dvmCompilerLockAllTemps(cUnit);
1293
Ben Cheng385828e2011-03-04 16:48:33 -08001294 loadClassPointer(cUnit, r1, (int) callsiteInfo);
Ben Cheng20d7e6c2011-02-18 17:12:42 -08001295
1296 loadWordDisp(cUnit, r0, offsetof(Object, clazz), r2);
1297 /* Branch to the slow path if classes are not equal */
1298 opRegReg(cUnit, kOpCmp, r1, r2);
1299 /*
1300 * Set the misPredBranchOver target so that it will be generated when the
1301 * code for the non-optimized invoke is generated.
1302 */
1303 ArmLIR *classCheck = opCondBranch(cUnit, kArmCondNe);
1304
1305 /* r0 = the Dalvik PC of the callsite */
1306 loadConstant(cUnit, r0, (int) (cUnit->method->insns + mir->offset));
1307
1308 newLIR2(cUnit, kThumbBl1, (int) calleeAddr, (int) calleeAddr);
1309 newLIR2(cUnit, kThumbBl2, (int) calleeAddr, (int) calleeAddr);
1310 genUnconditionalBranch(cUnit, retChainingCell);
1311
1312 /* Target of slow path */
1313 ArmLIR *slowPathLabel = newLIR0(cUnit, kArmPseudoTargetLabel);
1314
1315 slowPathLabel->defMask = ENCODE_ALL;
1316 classCheck->generic.target = (LIR *) slowPathLabel;
1317
1318 // FIXME
1319 cUnit->printMe = true;
1320}
1321
1322static void genInvokeSingletonWholeMethod(CompilationUnit *cUnit,
1323 MIR *mir,
1324 void *calleeAddr,
1325 ArmLIR *retChainingCell)
1326{
1327 /* r0 = the Dalvik PC of the callsite */
1328 loadConstant(cUnit, r0, (int) (cUnit->method->insns + mir->offset));
1329
1330 newLIR2(cUnit, kThumbBl1, (int) calleeAddr, (int) calleeAddr);
1331 newLIR2(cUnit, kThumbBl2, (int) calleeAddr, (int) calleeAddr);
1332 genUnconditionalBranch(cUnit, retChainingCell);
1333
1334 // FIXME
1335 cUnit->printMe = true;
1336}
1337
Ben Chengba4fc8b2009-06-01 13:00:29 -07001338/* Geneate a branch to go back to the interpreter */
1339static void genPuntToInterp(CompilationUnit *cUnit, unsigned int offset)
1340{
1341 /* r0 = dalvik pc */
Bill Buzbeec6f10662010-02-09 11:16:15 -08001342 dvmCompilerFlushAllRegs(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001343 loadConstant(cUnit, r0, (int) (cUnit->method->insns + offset));
Ben Cheng20d7e6c2011-02-18 17:12:42 -08001344 loadWordDisp(cUnit, r6SELF, offsetof(Thread,
Bill Buzbee270c1d62009-08-13 16:58:07 -07001345 jitToInterpEntries.dvmJitToInterpPunt), r1);
Bill Buzbee1465db52009-09-23 17:17:35 -07001346 opReg(cUnit, kOpBlx, r1);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001347}
1348
1349/*
1350 * Attempt to single step one instruction using the interpreter and return
1351 * to the compiled code for the next Dalvik instruction
1352 */
1353static void genInterpSingleStep(CompilationUnit *cUnit, MIR *mir)
1354{
Dan Bornsteine4852762010-12-02 12:45:00 -08001355 int flags = dexGetFlagsFromOpcode(mir->dalvikInsn.opcode);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001356 int flagsToCheck = kInstrCanBranch | kInstrCanSwitch | kInstrCanReturn |
1357 kInstrCanThrow;
Bill Buzbee1465db52009-09-23 17:17:35 -07001358
Bill Buzbee45273872010-03-11 11:12:15 -08001359 //If already optimized out, just ignore
Dan Bornstein9a1f8162010-12-01 17:02:26 -08001360 if (mir->dalvikInsn.opcode == OP_NOP)
Bill Buzbee45273872010-03-11 11:12:15 -08001361 return;
1362
Bill Buzbee1465db52009-09-23 17:17:35 -07001363 //Ugly, but necessary. Flush all Dalvik regs so Interp can find them
Bill Buzbeec6f10662010-02-09 11:16:15 -08001364 dvmCompilerFlushAllRegs(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07001365
Ben Chengba4fc8b2009-06-01 13:00:29 -07001366 if ((mir->next == NULL) || (flags & flagsToCheck)) {
1367 genPuntToInterp(cUnit, mir->offset);
1368 return;
1369 }
buzbee9f601a92011-02-11 17:48:20 -08001370 int entryAddr = offsetof(Thread,
Ben Chengba4fc8b2009-06-01 13:00:29 -07001371 jitToInterpEntries.dvmJitToInterpSingleStep);
Ben Cheng20d7e6c2011-02-18 17:12:42 -08001372 loadWordDisp(cUnit, r6SELF, entryAddr, r2);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001373 /* r0 = dalvik pc */
1374 loadConstant(cUnit, r0, (int) (cUnit->method->insns + mir->offset));
1375 /* r1 = dalvik pc of following instruction */
1376 loadConstant(cUnit, r1, (int) (cUnit->method->insns + mir->next->offset));
Bill Buzbee1465db52009-09-23 17:17:35 -07001377 opReg(cUnit, kOpBlx, r2);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001378}
1379
Carl Shapiro01605d22011-02-01 11:32:44 -08001380#if defined(_ARMV5TE) || defined(_ARMV5TE_VFP)
Bill Buzbeec1d9ed42010-02-02 11:04:33 -08001381/*
1382 * To prevent a thread in a monitor wait from blocking the Jit from
1383 * resetting the code cache, heavyweight monitor lock will not
1384 * be allowed to return to an existing translation. Instead, we will
1385 * handle them by branching to a handler, which will in turn call the
1386 * runtime lock routine and then branch directly back to the
1387 * interpreter main loop. Given the high cost of the heavyweight
1388 * lock operation, this additional cost should be slight (especially when
1389 * considering that we expect the vast majority of lock operations to
1390 * use the fast-path thin lock bypass).
1391 */
Ben Cheng5d90c202009-11-22 23:31:11 -08001392static void genMonitorPortable(CompilationUnit *cUnit, MIR *mir)
Bill Buzbee270c1d62009-08-13 16:58:07 -07001393{
Dan Bornstein9a1f8162010-12-01 17:02:26 -08001394 bool isEnter = (mir->dalvikInsn.opcode == OP_MONITOR_ENTER);
Bill Buzbee1465db52009-09-23 17:17:35 -07001395 genExportPC(cUnit, mir);
Bill Buzbeec6f10662010-02-09 11:16:15 -08001396 dvmCompilerFlushAllRegs(cUnit); /* Send everything to home location */
1397 RegLocation rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07001398 loadValueDirectFixed(cUnit, rlSrc, r1);
Ben Cheng20d7e6c2011-02-18 17:12:42 -08001399 genRegCopy(cUnit, r0, r6SELF);
Bill Buzbeec1d9ed42010-02-02 11:04:33 -08001400 genNullCheck(cUnit, rlSrc.sRegLow, r1, mir->offset, NULL);
Bill Buzbeeefbd3c52009-11-04 22:18:40 -08001401 if (isEnter) {
Bill Buzbeec1d9ed42010-02-02 11:04:33 -08001402 /* Get dPC of next insn */
1403 loadConstant(cUnit, r4PC, (int)(cUnit->method->insns + mir->offset +
Dan Bornsteine4852762010-12-02 12:45:00 -08001404 dexGetWidthFromOpcode(OP_MONITOR_ENTER)));
Bill Buzbeec1d9ed42010-02-02 11:04:33 -08001405 genDispatchToHandler(cUnit, TEMPLATE_MONITOR_ENTER);
Bill Buzbee1465db52009-09-23 17:17:35 -07001406 } else {
Ben Chengbd1326d2010-04-02 15:04:53 -07001407 LOAD_FUNC_ADDR(cUnit, r2, (int)dvmUnlockObject);
Bill Buzbeec1d9ed42010-02-02 11:04:33 -08001408 /* Do the call */
1409 opReg(cUnit, kOpBlx, r2);
buzbee8f8109a2010-08-31 10:16:35 -07001410 /* Did we throw? */
1411 ArmLIR *branchOver = genCmpImmBranch(cUnit, kArmCondNe, r0, 0);
Bill Buzbee6bbdd6b2010-02-16 14:40:01 -08001412 loadConstant(cUnit, r0,
1413 (int) (cUnit->method->insns + mir->offset +
Dan Bornsteine4852762010-12-02 12:45:00 -08001414 dexGetWidthFromOpcode(OP_MONITOR_EXIT)));
Bill Buzbee6bbdd6b2010-02-16 14:40:01 -08001415 genDispatchToHandler(cUnit, TEMPLATE_THROW_EXCEPTION_COMMON);
1416 ArmLIR *target = newLIR0(cUnit, kArmPseudoTargetLabel);
1417 target->defMask = ENCODE_ALL;
1418 branchOver->generic.target = (LIR *) target;
Elliott Hughes6a555132010-02-25 15:41:42 -08001419 dvmCompilerClobberCallRegs(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07001420 }
Bill Buzbee270c1d62009-08-13 16:58:07 -07001421}
Ben Chengfc075c22010-05-28 15:20:08 -07001422#endif
Bill Buzbee270c1d62009-08-13 16:58:07 -07001423
Ben Chengba4fc8b2009-06-01 13:00:29 -07001424/*
buzbee9f601a92011-02-11 17:48:20 -08001425 * Fetch *self->suspendCount. If the suspend count is non-zero,
Ben Cheng7ab74e12011-02-03 14:02:06 -08001426 * punt to the interpreter.
1427 */
1428static void genSuspendPoll(CompilationUnit *cUnit, MIR *mir)
1429{
1430 int rTemp = dvmCompilerAllocTemp(cUnit);
1431 ArmLIR *ld;
Ben Cheng20d7e6c2011-02-18 17:12:42 -08001432 ld = loadWordDisp(cUnit, r6SELF, offsetof(Thread, suspendCount),
Ben Cheng7ab74e12011-02-03 14:02:06 -08001433 rTemp);
1434 setMemRefType(ld, true /* isLoad */, kMustNotAlias);
Ben Cheng7ab74e12011-02-03 14:02:06 -08001435 genRegImmCheck(cUnit, kArmCondNe, rTemp, 0, mir->offset, NULL);
1436}
1437
1438/*
Ben Chengba4fc8b2009-06-01 13:00:29 -07001439 * The following are the first-level codegen routines that analyze the format
1440 * of each bytecode then either dispatch special purpose codegen routines
1441 * or produce corresponding Thumb instructions directly.
1442 */
1443
1444static bool handleFmt10t_Fmt20t_Fmt30t(CompilationUnit *cUnit, MIR *mir,
Bill Buzbee89efc3d2009-07-28 11:22:22 -07001445 BasicBlock *bb, ArmLIR *labelList)
Ben Chengba4fc8b2009-06-01 13:00:29 -07001446{
Ben Cheng7ab74e12011-02-03 14:02:06 -08001447 /* backward branch? */
1448 bool backwardBranch = (bb->taken->startOffset <= mir->offset);
1449
1450 if (backwardBranch && gDvmJit.genSuspendPoll) {
1451 genSuspendPoll(cUnit, mir);
1452 }
1453
1454 int numPredecessors = dvmCountSetBits(bb->taken->predecessors);
1455 /*
1456 * Things could be hoisted out of the taken block into the predecessor, so
1457 * make sure it is dominated by the predecessor.
1458 */
1459 if (numPredecessors == 1 && bb->taken->visited == false &&
Ben Cheng20d7e6c2011-02-18 17:12:42 -08001460 bb->taken->blockType == kDalvikByteCode) {
Ben Cheng7ab74e12011-02-03 14:02:06 -08001461 cUnit->nextCodegenBlock = bb->taken;
1462 } else {
1463 /* For OP_GOTO, OP_GOTO_16, and OP_GOTO_32 */
1464 genUnconditionalBranch(cUnit, &labelList[bb->taken->id]);
1465 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07001466 return false;
1467}
1468
1469static bool handleFmt10x(CompilationUnit *cUnit, MIR *mir)
1470{
Dan Bornstein9a1f8162010-12-01 17:02:26 -08001471 Opcode dalvikOpcode = mir->dalvikInsn.opcode;
1472 if ((dalvikOpcode >= OP_UNUSED_3E) && (dalvikOpcode <= OP_UNUSED_43)) {
1473 LOGE("Codegen: got unused opcode 0x%x\n",dalvikOpcode);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001474 return true;
1475 }
Dan Bornstein9a1f8162010-12-01 17:02:26 -08001476 switch (dalvikOpcode) {
Andy McFadden291758c2010-09-10 08:04:52 -07001477 case OP_RETURN_VOID_BARRIER:
buzbee2ce33c92010-11-01 15:53:27 -07001478 dvmCompilerGenMemBarrier(cUnit, kST);
1479 // Intentional fallthrough
1480 case OP_RETURN_VOID:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001481 genReturnCommon(cUnit,mir);
1482 break;
1483 case OP_UNUSED_73:
1484 case OP_UNUSED_79:
1485 case OP_UNUSED_7A:
Dan Bornstein90f15432010-12-02 16:46:25 -08001486 case OP_DISPATCH_FF:
Dan Bornstein9a1f8162010-12-01 17:02:26 -08001487 LOGE("Codegen: got unused opcode 0x%x\n",dalvikOpcode);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001488 return true;
1489 case OP_NOP:
1490 break;
1491 default:
1492 return true;
1493 }
1494 return false;
1495}
1496
1497static bool handleFmt11n_Fmt31i(CompilationUnit *cUnit, MIR *mir)
1498{
Bill Buzbee1465db52009-09-23 17:17:35 -07001499 RegLocation rlDest;
1500 RegLocation rlResult;
1501 if (mir->ssaRep->numDefs == 2) {
Bill Buzbeec6f10662010-02-09 11:16:15 -08001502 rlDest = dvmCompilerGetDestWide(cUnit, mir, 0, 1);
Bill Buzbee1465db52009-09-23 17:17:35 -07001503 } else {
Bill Buzbeec6f10662010-02-09 11:16:15 -08001504 rlDest = dvmCompilerGetDest(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07001505 }
Ben Chenge9695e52009-06-16 16:11:47 -07001506
Dan Bornstein9a1f8162010-12-01 17:02:26 -08001507 switch (mir->dalvikInsn.opcode) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07001508 case OP_CONST:
Ben Chenge9695e52009-06-16 16:11:47 -07001509 case OP_CONST_4: {
Bill Buzbeec6f10662010-02-09 11:16:15 -08001510 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kAnyReg, true);
Ben Chengbd1326d2010-04-02 15:04:53 -07001511 loadConstantNoClobber(cUnit, rlResult.lowReg, mir->dalvikInsn.vB);
Bill Buzbee1465db52009-09-23 17:17:35 -07001512 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001513 break;
Ben Chenge9695e52009-06-16 16:11:47 -07001514 }
1515 case OP_CONST_WIDE_32: {
Bill Buzbee1465db52009-09-23 17:17:35 -07001516 //TUNING: single routine to load constant pair for support doubles
Bill Buzbee964a7b02010-01-28 12:54:19 -08001517 //TUNING: load 0/-1 separately to avoid load dependency
Bill Buzbeec6f10662010-02-09 11:16:15 -08001518 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Ben Chengbd1326d2010-04-02 15:04:53 -07001519 loadConstantNoClobber(cUnit, rlResult.lowReg, mir->dalvikInsn.vB);
Bill Buzbee1465db52009-09-23 17:17:35 -07001520 opRegRegImm(cUnit, kOpAsr, rlResult.highReg,
1521 rlResult.lowReg, 31);
1522 storeValueWide(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001523 break;
Ben Chenge9695e52009-06-16 16:11:47 -07001524 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07001525 default:
1526 return true;
1527 }
1528 return false;
1529}
1530
1531static bool handleFmt21h(CompilationUnit *cUnit, MIR *mir)
1532{
Bill Buzbee1465db52009-09-23 17:17:35 -07001533 RegLocation rlDest;
1534 RegLocation rlResult;
1535 if (mir->ssaRep->numDefs == 2) {
Bill Buzbeec6f10662010-02-09 11:16:15 -08001536 rlDest = dvmCompilerGetDestWide(cUnit, mir, 0, 1);
Bill Buzbee1465db52009-09-23 17:17:35 -07001537 } else {
Bill Buzbeec6f10662010-02-09 11:16:15 -08001538 rlDest = dvmCompilerGetDest(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07001539 }
Bill Buzbeec6f10662010-02-09 11:16:15 -08001540 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kAnyReg, true);
Ben Chenge9695e52009-06-16 16:11:47 -07001541
Dan Bornstein9a1f8162010-12-01 17:02:26 -08001542 switch (mir->dalvikInsn.opcode) {
Ben Chenge9695e52009-06-16 16:11:47 -07001543 case OP_CONST_HIGH16: {
Ben Chengbd1326d2010-04-02 15:04:53 -07001544 loadConstantNoClobber(cUnit, rlResult.lowReg,
1545 mir->dalvikInsn.vB << 16);
Bill Buzbee1465db52009-09-23 17:17:35 -07001546 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001547 break;
Ben Chenge9695e52009-06-16 16:11:47 -07001548 }
1549 case OP_CONST_WIDE_HIGH16: {
Bill Buzbee1465db52009-09-23 17:17:35 -07001550 loadConstantValueWide(cUnit, rlResult.lowReg, rlResult.highReg,
1551 0, mir->dalvikInsn.vB << 16);
1552 storeValueWide(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001553 break;
Ben Chenge9695e52009-06-16 16:11:47 -07001554 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07001555 default:
1556 return true;
1557 }
1558 return false;
1559}
1560
jeffhao71eee1f2011-01-04 14:18:54 -08001561static bool handleFmt20bc_Fmt40sc(CompilationUnit *cUnit, MIR *mir)
Ben Chengba4fc8b2009-06-01 13:00:29 -07001562{
jeffhao71eee1f2011-01-04 14:18:54 -08001563 /* For OP_THROW_VERIFICATION_ERROR & OP_THROW_VERIFICATION_ERROR_JUMBO */
Ben Chengba4fc8b2009-06-01 13:00:29 -07001564 genInterpSingleStep(cUnit, mir);
1565 return false;
1566}
1567
jeffhao71eee1f2011-01-04 14:18:54 -08001568static bool handleFmt21c_Fmt31c_Fmt41c(CompilationUnit *cUnit, MIR *mir)
Ben Chengba4fc8b2009-06-01 13:00:29 -07001569{
Bill Buzbee1465db52009-09-23 17:17:35 -07001570 RegLocation rlResult;
1571 RegLocation rlDest;
1572 RegLocation rlSrc;
Ben Chenge9695e52009-06-16 16:11:47 -07001573
Dan Bornstein9a1f8162010-12-01 17:02:26 -08001574 switch (mir->dalvikInsn.opcode) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07001575 case OP_CONST_STRING_JUMBO:
1576 case OP_CONST_STRING: {
1577 void *strPtr = (void*)
1578 (cUnit->method->clazz->pDvmDex->pResStrings[mir->dalvikInsn.vB]);
Ben Chengdd6e8702010-05-07 13:05:47 -07001579
1580 if (strPtr == NULL) {
1581 LOGE("Unexpected null string");
1582 dvmAbort();
1583 }
1584
Bill Buzbeec6f10662010-02-09 11:16:15 -08001585 rlDest = dvmCompilerGetDest(cUnit, mir, 0);
1586 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Ben Chengbd1326d2010-04-02 15:04:53 -07001587 loadConstantNoClobber(cUnit, rlResult.lowReg, (int) strPtr );
Bill Buzbee1465db52009-09-23 17:17:35 -07001588 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001589 break;
1590 }
jeffhao71eee1f2011-01-04 14:18:54 -08001591 case OP_CONST_CLASS:
1592 case OP_CONST_CLASS_JUMBO: {
Ben Chengba4fc8b2009-06-01 13:00:29 -07001593 void *classPtr = (void*)
1594 (cUnit->method->clazz->pDvmDex->pResClasses[mir->dalvikInsn.vB]);
Ben Chengdd6e8702010-05-07 13:05:47 -07001595
1596 if (classPtr == NULL) {
1597 LOGE("Unexpected null class");
1598 dvmAbort();
1599 }
1600
Bill Buzbeec6f10662010-02-09 11:16:15 -08001601 rlDest = dvmCompilerGetDest(cUnit, mir, 0);
1602 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Ben Chengbd1326d2010-04-02 15:04:53 -07001603 loadConstantNoClobber(cUnit, rlResult.lowReg, (int) classPtr );
Bill Buzbee1465db52009-09-23 17:17:35 -07001604 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001605 break;
1606 }
jeffhao71eee1f2011-01-04 14:18:54 -08001607 case OP_SGET:
buzbeeecf8f6e2010-07-20 14:53:42 -07001608 case OP_SGET_VOLATILE:
jeffhao71eee1f2011-01-04 14:18:54 -08001609 case OP_SGET_JUMBO:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001610 case OP_SGET_OBJECT:
jeffhao71eee1f2011-01-04 14:18:54 -08001611 case OP_SGET_OBJECT_VOLATILE:
1612 case OP_SGET_OBJECT_JUMBO:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001613 case OP_SGET_BOOLEAN:
jeffhao71eee1f2011-01-04 14:18:54 -08001614 case OP_SGET_BOOLEAN_JUMBO:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001615 case OP_SGET_CHAR:
jeffhao71eee1f2011-01-04 14:18:54 -08001616 case OP_SGET_CHAR_JUMBO:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001617 case OP_SGET_BYTE:
jeffhao71eee1f2011-01-04 14:18:54 -08001618 case OP_SGET_BYTE_JUMBO:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001619 case OP_SGET_SHORT:
jeffhao71eee1f2011-01-04 14:18:54 -08001620 case OP_SGET_SHORT_JUMBO: {
Bill Buzbee50a6bf22009-07-08 13:08:04 -07001621 int valOffset = offsetof(StaticField, value);
Bill Buzbeec6f10662010-02-09 11:16:15 -08001622 int tReg = dvmCompilerAllocTemp(cUnit);
buzbeeecf8f6e2010-07-20 14:53:42 -07001623 bool isVolatile;
Ben Cheng7a2697d2010-06-07 13:44:23 -07001624 const Method *method = (mir->OptimizationFlags & MIR_CALLEE) ?
1625 mir->meta.calleeMethod : cUnit->method;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001626 void *fieldPtr = (void*)
Ben Cheng7a2697d2010-06-07 13:44:23 -07001627 (method->clazz->pDvmDex->pResFields[mir->dalvikInsn.vB]);
Ben Chengdd6e8702010-05-07 13:05:47 -07001628
1629 if (fieldPtr == NULL) {
1630 LOGE("Unexpected null static field");
1631 dvmAbort();
1632 }
1633
Dan Bornstein9a1f8162010-12-01 17:02:26 -08001634 isVolatile = (mir->dalvikInsn.opcode == OP_SGET_VOLATILE) ||
1635 (mir->dalvikInsn.opcode == OP_SGET_OBJECT_VOLATILE) ||
Carl Shapirofc75f3e2010-12-07 11:43:38 -08001636 dvmIsVolatileField((Field *) fieldPtr);
buzbeeecf8f6e2010-07-20 14:53:42 -07001637
Bill Buzbeec6f10662010-02-09 11:16:15 -08001638 rlDest = dvmCompilerGetDest(cUnit, mir, 0);
1639 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kAnyReg, true);
Bill Buzbee1465db52009-09-23 17:17:35 -07001640 loadConstant(cUnit, tReg, (int) fieldPtr + valOffset);
Ben Cheng11d8f142010-03-24 15:24:19 -07001641
buzbeeecf8f6e2010-07-20 14:53:42 -07001642 if (isVolatile) {
buzbee2ce33c92010-11-01 15:53:27 -07001643 dvmCompilerGenMemBarrier(cUnit, kSY);
buzbeeecf8f6e2010-07-20 14:53:42 -07001644 }
Ben Cheng11d8f142010-03-24 15:24:19 -07001645 HEAP_ACCESS_SHADOW(true);
Bill Buzbee1465db52009-09-23 17:17:35 -07001646 loadWordDisp(cUnit, tReg, 0, rlResult.lowReg);
Ben Cheng11d8f142010-03-24 15:24:19 -07001647 HEAP_ACCESS_SHADOW(false);
1648
Bill Buzbee1465db52009-09-23 17:17:35 -07001649 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001650 break;
1651 }
jeffhao71eee1f2011-01-04 14:18:54 -08001652 case OP_SGET_WIDE:
1653 case OP_SGET_WIDE_JUMBO: {
Bill Buzbee50a6bf22009-07-08 13:08:04 -07001654 int valOffset = offsetof(StaticField, value);
Ben Cheng7a2697d2010-06-07 13:44:23 -07001655 const Method *method = (mir->OptimizationFlags & MIR_CALLEE) ?
1656 mir->meta.calleeMethod : cUnit->method;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001657 void *fieldPtr = (void*)
Ben Cheng7a2697d2010-06-07 13:44:23 -07001658 (method->clazz->pDvmDex->pResFields[mir->dalvikInsn.vB]);
Ben Chengdd6e8702010-05-07 13:05:47 -07001659
1660 if (fieldPtr == NULL) {
1661 LOGE("Unexpected null static field");
1662 dvmAbort();
1663 }
1664
Bill Buzbeec6f10662010-02-09 11:16:15 -08001665 int tReg = dvmCompilerAllocTemp(cUnit);
Bill Buzbeec6f10662010-02-09 11:16:15 -08001666 rlDest = dvmCompilerGetDestWide(cUnit, mir, 0, 1);
1667 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kAnyReg, true);
Bill Buzbee1465db52009-09-23 17:17:35 -07001668 loadConstant(cUnit, tReg, (int) fieldPtr + valOffset);
Ben Cheng11d8f142010-03-24 15:24:19 -07001669
1670 HEAP_ACCESS_SHADOW(true);
Bill Buzbee1465db52009-09-23 17:17:35 -07001671 loadPair(cUnit, tReg, rlResult.lowReg, rlResult.highReg);
Ben Cheng11d8f142010-03-24 15:24:19 -07001672 HEAP_ACCESS_SHADOW(false);
1673
Bill Buzbee1465db52009-09-23 17:17:35 -07001674 storeValueWide(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001675 break;
1676 }
jeffhao71eee1f2011-01-04 14:18:54 -08001677 case OP_SPUT:
1678 case OP_SPUT_VOLATILE:
1679 case OP_SPUT_JUMBO:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001680 case OP_SPUT_OBJECT:
buzbeeddc7d292010-09-02 17:16:24 -07001681 case OP_SPUT_OBJECT_VOLATILE:
jeffhao71eee1f2011-01-04 14:18:54 -08001682 case OP_SPUT_OBJECT_JUMBO:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001683 case OP_SPUT_BOOLEAN:
jeffhao71eee1f2011-01-04 14:18:54 -08001684 case OP_SPUT_BOOLEAN_JUMBO:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001685 case OP_SPUT_CHAR:
jeffhao71eee1f2011-01-04 14:18:54 -08001686 case OP_SPUT_CHAR_JUMBO:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001687 case OP_SPUT_BYTE:
jeffhao71eee1f2011-01-04 14:18:54 -08001688 case OP_SPUT_BYTE_JUMBO:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001689 case OP_SPUT_SHORT:
jeffhao71eee1f2011-01-04 14:18:54 -08001690 case OP_SPUT_SHORT_JUMBO: {
Bill Buzbee50a6bf22009-07-08 13:08:04 -07001691 int valOffset = offsetof(StaticField, value);
Bill Buzbeec6f10662010-02-09 11:16:15 -08001692 int tReg = dvmCompilerAllocTemp(cUnit);
buzbeed3b0a4b2010-09-27 11:30:22 -07001693 int objHead;
buzbeeecf8f6e2010-07-20 14:53:42 -07001694 bool isVolatile;
buzbeed3b0a4b2010-09-27 11:30:22 -07001695 bool isSputObject;
Ben Cheng7a2697d2010-06-07 13:44:23 -07001696 const Method *method = (mir->OptimizationFlags & MIR_CALLEE) ?
1697 mir->meta.calleeMethod : cUnit->method;
1698 void *fieldPtr = (void*)
1699 (method->clazz->pDvmDex->pResFields[mir->dalvikInsn.vB]);
Ben Chenge9695e52009-06-16 16:11:47 -07001700
Dan Bornstein9a1f8162010-12-01 17:02:26 -08001701 isVolatile = (mir->dalvikInsn.opcode == OP_SPUT_VOLATILE) ||
1702 (mir->dalvikInsn.opcode == OP_SPUT_OBJECT_VOLATILE) ||
Carl Shapirofc75f3e2010-12-07 11:43:38 -08001703 dvmIsVolatileField((Field *) fieldPtr);
buzbeeecf8f6e2010-07-20 14:53:42 -07001704
Dan Bornstein9a1f8162010-12-01 17:02:26 -08001705 isSputObject = (mir->dalvikInsn.opcode == OP_SPUT_OBJECT) ||
jeffhao71eee1f2011-01-04 14:18:54 -08001706 (mir->dalvikInsn.opcode == OP_SPUT_OBJECT_JUMBO) ||
Dan Bornstein9a1f8162010-12-01 17:02:26 -08001707 (mir->dalvikInsn.opcode == OP_SPUT_OBJECT_VOLATILE);
buzbeed3b0a4b2010-09-27 11:30:22 -07001708
Ben Chengdd6e8702010-05-07 13:05:47 -07001709 if (fieldPtr == NULL) {
1710 LOGE("Unexpected null static field");
1711 dvmAbort();
1712 }
1713
Bill Buzbeec6f10662010-02-09 11:16:15 -08001714 rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07001715 rlSrc = loadValue(cUnit, rlSrc, kAnyReg);
buzbeeb78c76f2010-09-30 19:08:20 -07001716 loadConstant(cUnit, tReg, (int) fieldPtr);
buzbeed3b0a4b2010-09-27 11:30:22 -07001717 if (isSputObject) {
1718 objHead = dvmCompilerAllocTemp(cUnit);
buzbeeb78c76f2010-09-30 19:08:20 -07001719 loadWordDisp(cUnit, tReg, offsetof(Field, clazz), objHead);
buzbeed3b0a4b2010-09-27 11:30:22 -07001720 }
Ben Cheng11d8f142010-03-24 15:24:19 -07001721 HEAP_ACCESS_SHADOW(true);
buzbeeb78c76f2010-09-30 19:08:20 -07001722 storeWordDisp(cUnit, tReg, valOffset ,rlSrc.lowReg);
buzbeed3b0a4b2010-09-27 11:30:22 -07001723 dvmCompilerFreeTemp(cUnit, tReg);
Ben Cheng11d8f142010-03-24 15:24:19 -07001724 HEAP_ACCESS_SHADOW(false);
buzbeeecf8f6e2010-07-20 14:53:42 -07001725 if (isVolatile) {
buzbee2ce33c92010-11-01 15:53:27 -07001726 dvmCompilerGenMemBarrier(cUnit, kSY);
buzbeeecf8f6e2010-07-20 14:53:42 -07001727 }
buzbeed3b0a4b2010-09-27 11:30:22 -07001728 if (isSputObject) {
buzbeeb78c76f2010-09-30 19:08:20 -07001729 /* NOTE: marking card based sfield->clazz */
buzbeed3b0a4b2010-09-27 11:30:22 -07001730 markCard(cUnit, rlSrc.lowReg, objHead);
1731 dvmCompilerFreeTemp(cUnit, objHead);
buzbee919eb062010-07-12 12:59:22 -07001732 }
Ben Cheng11d8f142010-03-24 15:24:19 -07001733
Ben Chengba4fc8b2009-06-01 13:00:29 -07001734 break;
1735 }
jeffhao71eee1f2011-01-04 14:18:54 -08001736 case OP_SPUT_WIDE:
1737 case OP_SPUT_WIDE_JUMBO: {
Bill Buzbeec6f10662010-02-09 11:16:15 -08001738 int tReg = dvmCompilerAllocTemp(cUnit);
Bill Buzbee50a6bf22009-07-08 13:08:04 -07001739 int valOffset = offsetof(StaticField, value);
Ben Cheng7a2697d2010-06-07 13:44:23 -07001740 const Method *method = (mir->OptimizationFlags & MIR_CALLEE) ?
1741 mir->meta.calleeMethod : cUnit->method;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001742 void *fieldPtr = (void*)
Ben Cheng7a2697d2010-06-07 13:44:23 -07001743 (method->clazz->pDvmDex->pResFields[mir->dalvikInsn.vB]);
Ben Chenge9695e52009-06-16 16:11:47 -07001744
Ben Chengdd6e8702010-05-07 13:05:47 -07001745 if (fieldPtr == NULL) {
1746 LOGE("Unexpected null static field");
1747 dvmAbort();
1748 }
1749
Bill Buzbeec6f10662010-02-09 11:16:15 -08001750 rlSrc = dvmCompilerGetSrcWide(cUnit, mir, 0, 1);
Bill Buzbee1465db52009-09-23 17:17:35 -07001751 rlSrc = loadValueWide(cUnit, rlSrc, kAnyReg);
1752 loadConstant(cUnit, tReg, (int) fieldPtr + valOffset);
Ben Cheng11d8f142010-03-24 15:24:19 -07001753
1754 HEAP_ACCESS_SHADOW(true);
Bill Buzbee1465db52009-09-23 17:17:35 -07001755 storePair(cUnit, tReg, rlSrc.lowReg, rlSrc.highReg);
Ben Cheng11d8f142010-03-24 15:24:19 -07001756 HEAP_ACCESS_SHADOW(false);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001757 break;
1758 }
jeffhao71eee1f2011-01-04 14:18:54 -08001759 case OP_NEW_INSTANCE:
1760 case OP_NEW_INSTANCE_JUMBO: {
Ben Chenge9695e52009-06-16 16:11:47 -07001761 /*
1762 * Obey the calling convention and don't mess with the register
1763 * usage.
1764 */
Carl Shapirofc75f3e2010-12-07 11:43:38 -08001765 ClassObject *classPtr = (ClassObject *)
Ben Chengba4fc8b2009-06-01 13:00:29 -07001766 (cUnit->method->clazz->pDvmDex->pResClasses[mir->dalvikInsn.vB]);
Ben Chengdd6e8702010-05-07 13:05:47 -07001767
1768 if (classPtr == NULL) {
1769 LOGE("Unexpected null class");
1770 dvmAbort();
1771 }
1772
Ben Cheng79d173c2009-09-29 16:12:51 -07001773 /*
1774 * If it is going to throw, it should not make to the trace to begin
Bill Buzbee1465db52009-09-23 17:17:35 -07001775 * with. However, Alloc might throw, so we need to genExportPC()
Ben Cheng79d173c2009-09-29 16:12:51 -07001776 */
1777 assert((classPtr->accessFlags & (ACC_INTERFACE|ACC_ABSTRACT)) == 0);
Bill Buzbeec6f10662010-02-09 11:16:15 -08001778 dvmCompilerFlushAllRegs(cUnit); /* Everything to home location */
Bill Buzbee1465db52009-09-23 17:17:35 -07001779 genExportPC(cUnit, mir);
Ben Chengbd1326d2010-04-02 15:04:53 -07001780 LOAD_FUNC_ADDR(cUnit, r2, (int)dvmAllocObject);
Ben Chenge9695e52009-06-16 16:11:47 -07001781 loadConstant(cUnit, r0, (int) classPtr);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001782 loadConstant(cUnit, r1, ALLOC_DONT_TRACK);
Bill Buzbee1465db52009-09-23 17:17:35 -07001783 opReg(cUnit, kOpBlx, r2);
Elliott Hughes6a555132010-02-25 15:41:42 -08001784 dvmCompilerClobberCallRegs(cUnit);
Ben Cheng4f489172009-09-27 17:08:35 -07001785 /* generate a branch over if allocation is successful */
buzbee8f8109a2010-08-31 10:16:35 -07001786 ArmLIR *branchOver = genCmpImmBranch(cUnit, kArmCondNe, r0, 0);
Ben Cheng4f489172009-09-27 17:08:35 -07001787 /*
1788 * OOM exception needs to be thrown here and cannot re-execute
1789 */
1790 loadConstant(cUnit, r0,
1791 (int) (cUnit->method->insns + mir->offset));
1792 genDispatchToHandler(cUnit, TEMPLATE_THROW_EXCEPTION_COMMON);
1793 /* noreturn */
1794
Bill Buzbee1465db52009-09-23 17:17:35 -07001795 ArmLIR *target = newLIR0(cUnit, kArmPseudoTargetLabel);
Ben Cheng4f489172009-09-27 17:08:35 -07001796 target->defMask = ENCODE_ALL;
1797 branchOver->generic.target = (LIR *) target;
Bill Buzbeec6f10662010-02-09 11:16:15 -08001798 rlDest = dvmCompilerGetDest(cUnit, mir, 0);
1799 rlResult = dvmCompilerGetReturn(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07001800 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001801 break;
1802 }
jeffhao71eee1f2011-01-04 14:18:54 -08001803 case OP_CHECK_CAST:
1804 case OP_CHECK_CAST_JUMBO: {
Ben Chenge9695e52009-06-16 16:11:47 -07001805 /*
1806 * Obey the calling convention and don't mess with the register
1807 * usage.
1808 */
Ben Chengba4fc8b2009-06-01 13:00:29 -07001809 ClassObject *classPtr =
1810 (cUnit->method->clazz->pDvmDex->pResClasses[mir->dalvikInsn.vB]);
Bill Buzbee4df41a52009-11-12 17:07:16 -08001811 /*
1812 * Note: It is possible that classPtr is NULL at this point,
1813 * even though this instruction has been successfully interpreted.
1814 * If the previous interpretation had a null source, the
1815 * interpreter would not have bothered to resolve the clazz.
1816 * Bail out to the interpreter in this case, and log it
1817 * so that we can tell if it happens frequently.
1818 */
1819 if (classPtr == NULL) {
Ben Cheng11d8f142010-03-24 15:24:19 -07001820 LOGVV("null clazz in OP_CHECK_CAST, single-stepping");
Bill Buzbee4df41a52009-11-12 17:07:16 -08001821 genInterpSingleStep(cUnit, mir);
1822 return false;
1823 }
Bill Buzbeec6f10662010-02-09 11:16:15 -08001824 dvmCompilerFlushAllRegs(cUnit); /* Everything to home location */
Ben Chengba4fc8b2009-06-01 13:00:29 -07001825 loadConstant(cUnit, r1, (int) classPtr );
Bill Buzbeec6f10662010-02-09 11:16:15 -08001826 rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07001827 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
buzbee8f8109a2010-08-31 10:16:35 -07001828 /* Null? */
1829 ArmLIR *branch1 = genCmpImmBranch(cUnit, kArmCondEq,
1830 rlSrc.lowReg, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07001831 /*
1832 * rlSrc.lowReg now contains object->clazz. Note that
1833 * it could have been allocated r0, but we're okay so long
1834 * as we don't do anything desctructive until r0 is loaded
1835 * with clazz.
1836 */
Ben Chengba4fc8b2009-06-01 13:00:29 -07001837 /* r0 now contains object->clazz */
Bill Buzbee1465db52009-09-23 17:17:35 -07001838 loadWordDisp(cUnit, rlSrc.lowReg, offsetof(Object, clazz), r0);
Ben Chengbd1326d2010-04-02 15:04:53 -07001839 LOAD_FUNC_ADDR(cUnit, r2, (int)dvmInstanceofNonTrivial);
Bill Buzbee1465db52009-09-23 17:17:35 -07001840 opRegReg(cUnit, kOpCmp, r0, r1);
1841 ArmLIR *branch2 = opCondBranch(cUnit, kArmCondEq);
1842 opReg(cUnit, kOpBlx, r2);
Elliott Hughes6a555132010-02-25 15:41:42 -08001843 dvmCompilerClobberCallRegs(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07001844 /*
1845 * If null, check cast failed - punt to the interpreter. Because
1846 * interpreter will be the one throwing, we don't need to
1847 * genExportPC() here.
1848 */
Bill Buzbee270c1d62009-08-13 16:58:07 -07001849 genZeroCheck(cUnit, r0, mir->offset, NULL);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001850 /* check cast passed - branch target here */
Bill Buzbee1465db52009-09-23 17:17:35 -07001851 ArmLIR *target = newLIR0(cUnit, kArmPseudoTargetLabel);
Ben Chengd7d426a2009-09-22 11:23:36 -07001852 target->defMask = ENCODE_ALL;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001853 branch1->generic.target = (LIR *)target;
1854 branch2->generic.target = (LIR *)target;
1855 break;
1856 }
buzbee4d92e682010-07-29 15:24:14 -07001857 case OP_SGET_WIDE_VOLATILE:
1858 case OP_SPUT_WIDE_VOLATILE:
1859 genInterpSingleStep(cUnit, mir);
1860 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001861 default:
1862 return true;
1863 }
1864 return false;
1865}
1866
Ben Cheng7a2697d2010-06-07 13:44:23 -07001867/*
1868 * A typical example of inlined getter/setter from a monomorphic callsite:
1869 *
1870 * D/dalvikvm( 289): -------- dalvik offset: 0x0000 @ invoke-static (I)
1871 * D/dalvikvm( 289): -------- dalvik offset: 0x0000 @ sget-object (C) v0, ...
1872 * D/dalvikvm( 289): 0x4427fc22 (0002): ldr r0, [pc, #56]
1873 * D/dalvikvm( 289): 0x4427fc24 (0004): ldr r1, [r0, #0]
1874 * D/dalvikvm( 289): 0x4427fc26 (0006): str r1, [r5, #0]
1875 * D/dalvikvm( 289): 0x4427fc28 (0008): .align4
1876 * D/dalvikvm( 289): L0x0003:
1877 * D/dalvikvm( 289): -------- dalvik offset: 0x0003 @ move-result-object (I) v0
1878 *
1879 * Note the invoke-static and move-result-object with the (I) notation are
1880 * turned into no-op.
1881 */
Ben Chengba4fc8b2009-06-01 13:00:29 -07001882static bool handleFmt11x(CompilationUnit *cUnit, MIR *mir)
1883{
Dan Bornstein9a1f8162010-12-01 17:02:26 -08001884 Opcode dalvikOpcode = mir->dalvikInsn.opcode;
Bill Buzbee1465db52009-09-23 17:17:35 -07001885 RegLocation rlResult;
Dan Bornstein9a1f8162010-12-01 17:02:26 -08001886 switch (dalvikOpcode) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07001887 case OP_MOVE_EXCEPTION: {
Ben Chengba4fc8b2009-06-01 13:00:29 -07001888 int exOffset = offsetof(Thread, exception);
Bill Buzbeec6f10662010-02-09 11:16:15 -08001889 int resetReg = dvmCompilerAllocTemp(cUnit);
1890 RegLocation rlDest = dvmCompilerGetDest(cUnit, mir, 0);
1891 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Ben Cheng20d7e6c2011-02-18 17:12:42 -08001892 loadWordDisp(cUnit, r6SELF, exOffset, rlResult.lowReg);
Bill Buzbeef9f33282009-11-22 12:45:30 -08001893 loadConstant(cUnit, resetReg, 0);
Ben Cheng20d7e6c2011-02-18 17:12:42 -08001894 storeWordDisp(cUnit, r6SELF, exOffset, resetReg);
Bill Buzbee1465db52009-09-23 17:17:35 -07001895 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001896 break;
1897 }
1898 case OP_MOVE_RESULT:
1899 case OP_MOVE_RESULT_OBJECT: {
Ben Cheng7a2697d2010-06-07 13:44:23 -07001900 /* An inlined move result is effectively no-op */
1901 if (mir->OptimizationFlags & MIR_INLINED)
1902 break;
Bill Buzbeec6f10662010-02-09 11:16:15 -08001903 RegLocation rlDest = dvmCompilerGetDest(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07001904 RegLocation rlSrc = LOC_DALVIK_RETURN_VAL;
1905 rlSrc.fp = rlDest.fp;
1906 storeValue(cUnit, rlDest, rlSrc);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001907 break;
1908 }
1909 case OP_MOVE_RESULT_WIDE: {
Ben Cheng7a2697d2010-06-07 13:44:23 -07001910 /* An inlined move result is effectively no-op */
1911 if (mir->OptimizationFlags & MIR_INLINED)
1912 break;
Bill Buzbeec6f10662010-02-09 11:16:15 -08001913 RegLocation rlDest = dvmCompilerGetDestWide(cUnit, mir, 0, 1);
Bill Buzbee1465db52009-09-23 17:17:35 -07001914 RegLocation rlSrc = LOC_DALVIK_RETURN_VAL_WIDE;
1915 rlSrc.fp = rlDest.fp;
1916 storeValueWide(cUnit, rlDest, rlSrc);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001917 break;
1918 }
1919 case OP_RETURN_WIDE: {
Bill Buzbeec6f10662010-02-09 11:16:15 -08001920 RegLocation rlSrc = dvmCompilerGetSrcWide(cUnit, mir, 0, 1);
Bill Buzbee1465db52009-09-23 17:17:35 -07001921 RegLocation rlDest = LOC_DALVIK_RETURN_VAL_WIDE;
1922 rlDest.fp = rlSrc.fp;
1923 storeValueWide(cUnit, rlDest, rlSrc);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001924 genReturnCommon(cUnit,mir);
1925 break;
1926 }
1927 case OP_RETURN:
1928 case OP_RETURN_OBJECT: {
Bill Buzbeec6f10662010-02-09 11:16:15 -08001929 RegLocation rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07001930 RegLocation rlDest = LOC_DALVIK_RETURN_VAL;
1931 rlDest.fp = rlSrc.fp;
1932 storeValue(cUnit, rlDest, rlSrc);
Ben Cheng20d7e6c2011-02-18 17:12:42 -08001933 genReturnCommon(cUnit, mir);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001934 break;
1935 }
Bill Buzbee1465db52009-09-23 17:17:35 -07001936 case OP_MONITOR_EXIT:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001937 case OP_MONITOR_ENTER:
Ben Cheng5d90c202009-11-22 23:31:11 -08001938 genMonitor(cUnit, mir);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001939 break;
Ben Cheng20d7e6c2011-02-18 17:12:42 -08001940 case OP_THROW:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001941 genInterpSingleStep(cUnit, mir);
1942 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001943 default:
1944 return true;
1945 }
1946 return false;
1947}
1948
Bill Buzbeed45ba372009-06-15 17:00:57 -07001949static bool handleFmt12x(CompilationUnit *cUnit, MIR *mir)
1950{
Dan Bornstein9a1f8162010-12-01 17:02:26 -08001951 Opcode opcode = mir->dalvikInsn.opcode;
Bill Buzbee1465db52009-09-23 17:17:35 -07001952 RegLocation rlDest;
1953 RegLocation rlSrc;
1954 RegLocation rlResult;
Bill Buzbeed45ba372009-06-15 17:00:57 -07001955
Dan Bornstein9a1f8162010-12-01 17:02:26 -08001956 if ( (opcode >= OP_ADD_INT_2ADDR) && (opcode <= OP_REM_DOUBLE_2ADDR)) {
Ben Cheng5d90c202009-11-22 23:31:11 -08001957 return genArithOp( cUnit, mir );
Ben Chengba4fc8b2009-06-01 13:00:29 -07001958 }
1959
Bill Buzbee1465db52009-09-23 17:17:35 -07001960 if (mir->ssaRep->numUses == 2)
Bill Buzbeec6f10662010-02-09 11:16:15 -08001961 rlSrc = dvmCompilerGetSrcWide(cUnit, mir, 0, 1);
Bill Buzbee1465db52009-09-23 17:17:35 -07001962 else
Bill Buzbeec6f10662010-02-09 11:16:15 -08001963 rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07001964 if (mir->ssaRep->numDefs == 2)
Bill Buzbeec6f10662010-02-09 11:16:15 -08001965 rlDest = dvmCompilerGetDestWide(cUnit, mir, 0, 1);
Bill Buzbee1465db52009-09-23 17:17:35 -07001966 else
Bill Buzbeec6f10662010-02-09 11:16:15 -08001967 rlDest = dvmCompilerGetDest(cUnit, mir, 0);
Ben Chenge9695e52009-06-16 16:11:47 -07001968
Dan Bornstein9a1f8162010-12-01 17:02:26 -08001969 switch (opcode) {
Bill Buzbee1465db52009-09-23 17:17:35 -07001970 case OP_DOUBLE_TO_INT:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001971 case OP_INT_TO_FLOAT:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001972 case OP_FLOAT_TO_INT:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001973 case OP_DOUBLE_TO_FLOAT:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001974 case OP_FLOAT_TO_DOUBLE:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001975 case OP_INT_TO_DOUBLE:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001976 case OP_FLOAT_TO_LONG:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001977 case OP_LONG_TO_FLOAT:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001978 case OP_DOUBLE_TO_LONG:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001979 case OP_LONG_TO_DOUBLE:
Ben Cheng5d90c202009-11-22 23:31:11 -08001980 return genConversion(cUnit, mir);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001981 case OP_NEG_INT:
1982 case OP_NOT_INT:
Ben Cheng5d90c202009-11-22 23:31:11 -08001983 return genArithOpInt(cUnit, mir, rlDest, rlSrc, rlSrc);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001984 case OP_NEG_LONG:
1985 case OP_NOT_LONG:
Ben Cheng5d90c202009-11-22 23:31:11 -08001986 return genArithOpLong(cUnit, mir, rlDest, rlSrc, rlSrc);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001987 case OP_NEG_FLOAT:
Ben Cheng5d90c202009-11-22 23:31:11 -08001988 return genArithOpFloat(cUnit, mir, rlDest, rlSrc, rlSrc);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001989 case OP_NEG_DOUBLE:
Ben Cheng5d90c202009-11-22 23:31:11 -08001990 return genArithOpDouble(cUnit, mir, rlDest, rlSrc, rlSrc);
Bill Buzbee1465db52009-09-23 17:17:35 -07001991 case OP_MOVE_WIDE:
1992 storeValueWide(cUnit, rlDest, rlSrc);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001993 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07001994 case OP_INT_TO_LONG:
Bill Buzbeec6f10662010-02-09 11:16:15 -08001995 rlSrc = dvmCompilerUpdateLoc(cUnit, rlSrc);
1996 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Bill Buzbee964a7b02010-01-28 12:54:19 -08001997 //TUNING: shouldn't loadValueDirect already check for phys reg?
Bill Buzbee1465db52009-09-23 17:17:35 -07001998 if (rlSrc.location == kLocPhysReg) {
1999 genRegCopy(cUnit, rlResult.lowReg, rlSrc.lowReg);
2000 } else {
2001 loadValueDirect(cUnit, rlSrc, rlResult.lowReg);
2002 }
2003 opRegRegImm(cUnit, kOpAsr, rlResult.highReg,
2004 rlResult.lowReg, 31);
2005 storeValueWide(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002006 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07002007 case OP_LONG_TO_INT:
Bill Buzbeec6f10662010-02-09 11:16:15 -08002008 rlSrc = dvmCompilerUpdateLocWide(cUnit, rlSrc);
2009 rlSrc = dvmCompilerWideToNarrow(cUnit, rlSrc);
Bill Buzbee1465db52009-09-23 17:17:35 -07002010 // Intentional fallthrough
Ben Chengba4fc8b2009-06-01 13:00:29 -07002011 case OP_MOVE:
2012 case OP_MOVE_OBJECT:
Bill Buzbee1465db52009-09-23 17:17:35 -07002013 storeValue(cUnit, rlDest, rlSrc);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002014 break;
2015 case OP_INT_TO_BYTE:
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, kOp2Byte, rlResult.lowReg, rlSrc.lowReg);
2019 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002020 break;
2021 case OP_INT_TO_SHORT:
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, kOp2Short, rlResult.lowReg, rlSrc.lowReg);
2025 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002026 break;
2027 case OP_INT_TO_CHAR:
Bill Buzbee1465db52009-09-23 17:17:35 -07002028 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
Bill Buzbeec6f10662010-02-09 11:16:15 -08002029 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Bill Buzbee1465db52009-09-23 17:17:35 -07002030 opRegReg(cUnit, kOp2Char, rlResult.lowReg, rlSrc.lowReg);
2031 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002032 break;
2033 case OP_ARRAY_LENGTH: {
2034 int lenOffset = offsetof(ArrayObject, length);
Bill Buzbee1465db52009-09-23 17:17:35 -07002035 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
2036 genNullCheck(cUnit, rlSrc.sRegLow, rlSrc.lowReg,
2037 mir->offset, NULL);
Bill Buzbeec6f10662010-02-09 11:16:15 -08002038 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Bill Buzbee1465db52009-09-23 17:17:35 -07002039 loadWordDisp(cUnit, rlSrc.lowReg, lenOffset,
2040 rlResult.lowReg);
2041 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002042 break;
2043 }
2044 default:
2045 return true;
2046 }
2047 return false;
2048}
2049
2050static bool handleFmt21s(CompilationUnit *cUnit, MIR *mir)
2051{
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002052 Opcode dalvikOpcode = mir->dalvikInsn.opcode;
Bill Buzbee1465db52009-09-23 17:17:35 -07002053 RegLocation rlDest;
2054 RegLocation rlResult;
2055 int BBBB = mir->dalvikInsn.vB;
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002056 if (dalvikOpcode == OP_CONST_WIDE_16) {
Bill Buzbeec6f10662010-02-09 11:16:15 -08002057 rlDest = dvmCompilerGetDestWide(cUnit, mir, 0, 1);
2058 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Ben Chengbd1326d2010-04-02 15:04:53 -07002059 loadConstantNoClobber(cUnit, rlResult.lowReg, BBBB);
Bill Buzbee964a7b02010-01-28 12:54:19 -08002060 //TUNING: do high separately to avoid load dependency
Bill Buzbee1465db52009-09-23 17:17:35 -07002061 opRegRegImm(cUnit, kOpAsr, rlResult.highReg, rlResult.lowReg, 31);
2062 storeValueWide(cUnit, rlDest, rlResult);
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002063 } else if (dalvikOpcode == OP_CONST_16) {
Bill Buzbeec6f10662010-02-09 11:16:15 -08002064 rlDest = dvmCompilerGetDest(cUnit, mir, 0);
2065 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kAnyReg, true);
Ben Chengbd1326d2010-04-02 15:04:53 -07002066 loadConstantNoClobber(cUnit, rlResult.lowReg, BBBB);
Bill Buzbee1465db52009-09-23 17:17:35 -07002067 storeValue(cUnit, rlDest, rlResult);
2068 } else
Ben Chengba4fc8b2009-06-01 13:00:29 -07002069 return true;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002070 return false;
2071}
2072
2073/* Compare agaist zero */
2074static bool handleFmt21t(CompilationUnit *cUnit, MIR *mir, BasicBlock *bb,
Bill Buzbee89efc3d2009-07-28 11:22:22 -07002075 ArmLIR *labelList)
Ben Chengba4fc8b2009-06-01 13:00:29 -07002076{
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002077 Opcode dalvikOpcode = mir->dalvikInsn.opcode;
Bill Buzbee89efc3d2009-07-28 11:22:22 -07002078 ArmConditionCode cond;
Ben Cheng7ab74e12011-02-03 14:02:06 -08002079 /* backward branch? */
2080 bool backwardBranch = (bb->taken->startOffset <= mir->offset);
2081
2082 if (backwardBranch && gDvmJit.genSuspendPoll) {
2083 genSuspendPoll(cUnit, mir);
2084 }
2085
Bill Buzbeec6f10662010-02-09 11:16:15 -08002086 RegLocation rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07002087 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
Ben Cheng7ab74e12011-02-03 14:02:06 -08002088
Bill Buzbee1465db52009-09-23 17:17:35 -07002089 opRegImm(cUnit, kOpCmp, rlSrc.lowReg, 0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002090
Bill Buzbee270c1d62009-08-13 16:58:07 -07002091//TUNING: break this out to allow use of Thumb2 CB[N]Z
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002092 switch (dalvikOpcode) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07002093 case OP_IF_EQZ:
Bill Buzbee1465db52009-09-23 17:17:35 -07002094 cond = kArmCondEq;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002095 break;
2096 case OP_IF_NEZ:
Bill Buzbee1465db52009-09-23 17:17:35 -07002097 cond = kArmCondNe;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002098 break;
2099 case OP_IF_LTZ:
Bill Buzbee1465db52009-09-23 17:17:35 -07002100 cond = kArmCondLt;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002101 break;
2102 case OP_IF_GEZ:
Bill Buzbee1465db52009-09-23 17:17:35 -07002103 cond = kArmCondGe;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002104 break;
2105 case OP_IF_GTZ:
Bill Buzbee1465db52009-09-23 17:17:35 -07002106 cond = kArmCondGt;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002107 break;
2108 case OP_IF_LEZ:
Bill Buzbee1465db52009-09-23 17:17:35 -07002109 cond = kArmCondLe;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002110 break;
2111 default:
2112 cond = 0;
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002113 LOGE("Unexpected opcode (%d) for Fmt21t\n", dalvikOpcode);
Bill Buzbeefc519dc2010-03-06 23:30:57 -08002114 dvmCompilerAbort(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002115 }
2116 genConditionalBranch(cUnit, cond, &labelList[bb->taken->id]);
2117 /* This mostly likely will be optimized away in a later phase */
2118 genUnconditionalBranch(cUnit, &labelList[bb->fallThrough->id]);
2119 return false;
2120}
2121
Elliott Hughesb4c05972010-02-24 16:36:18 -08002122static bool isPowerOfTwo(int x)
2123{
2124 return (x & (x - 1)) == 0;
2125}
2126
2127// Returns true if no more than two bits are set in 'x'.
2128static bool isPopCountLE2(unsigned int x)
2129{
2130 x &= x - 1;
2131 return (x & (x - 1)) == 0;
2132}
2133
2134// Returns the index of the lowest set bit in 'x'.
2135static int lowestSetBit(unsigned int x) {
2136 int bit_posn = 0;
2137 while ((x & 0xf) == 0) {
2138 bit_posn += 4;
2139 x >>= 4;
Bill Buzbee78cb0e22010-02-11 14:04:53 -08002140 }
Elliott Hughesb4c05972010-02-24 16:36:18 -08002141 while ((x & 1) == 0) {
2142 bit_posn++;
2143 x >>= 1;
2144 }
2145 return bit_posn;
2146}
2147
Elliott Hughes672511b2010-04-26 17:40:13 -07002148// Returns true if it added instructions to 'cUnit' to divide 'rlSrc' by 'lit'
2149// and store the result in 'rlDest'.
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002150static bool handleEasyDivide(CompilationUnit *cUnit, Opcode dalvikOpcode,
Elliott Hughes672511b2010-04-26 17:40:13 -07002151 RegLocation rlSrc, RegLocation rlDest, int lit)
2152{
2153 if (lit < 2 || !isPowerOfTwo(lit)) {
2154 return false;
2155 }
2156 int k = lowestSetBit(lit);
2157 if (k >= 30) {
2158 // Avoid special cases.
2159 return false;
2160 }
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002161 bool div = (dalvikOpcode == OP_DIV_INT_LIT8 || dalvikOpcode == OP_DIV_INT_LIT16);
Elliott Hughes672511b2010-04-26 17:40:13 -07002162 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
2163 RegLocation rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Elliott Hughes9c457022010-04-28 16:15:38 -07002164 if (div) {
2165 int tReg = dvmCompilerAllocTemp(cUnit);
2166 if (lit == 2) {
2167 // Division by 2 is by far the most common division by constant.
2168 opRegRegImm(cUnit, kOpLsr, tReg, rlSrc.lowReg, 32 - k);
2169 opRegRegReg(cUnit, kOpAdd, tReg, tReg, rlSrc.lowReg);
2170 opRegRegImm(cUnit, kOpAsr, rlResult.lowReg, tReg, k);
2171 } else {
2172 opRegRegImm(cUnit, kOpAsr, tReg, rlSrc.lowReg, 31);
2173 opRegRegImm(cUnit, kOpLsr, tReg, tReg, 32 - k);
2174 opRegRegReg(cUnit, kOpAdd, tReg, tReg, rlSrc.lowReg);
2175 opRegRegImm(cUnit, kOpAsr, rlResult.lowReg, tReg, k);
2176 }
Elliott Hughes672511b2010-04-26 17:40:13 -07002177 } else {
Elliott Hughes9c457022010-04-28 16:15:38 -07002178 int cReg = dvmCompilerAllocTemp(cUnit);
2179 loadConstant(cUnit, cReg, lit - 1);
2180 int tReg1 = dvmCompilerAllocTemp(cUnit);
2181 int tReg2 = dvmCompilerAllocTemp(cUnit);
2182 if (lit == 2) {
2183 opRegRegImm(cUnit, kOpLsr, tReg1, rlSrc.lowReg, 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 } else {
2188 opRegRegImm(cUnit, kOpAsr, tReg1, rlSrc.lowReg, 31);
2189 opRegRegImm(cUnit, kOpLsr, tReg1, tReg1, 32 - k);
2190 opRegRegReg(cUnit, kOpAdd, tReg2, tReg1, rlSrc.lowReg);
2191 opRegRegReg(cUnit, kOpAnd, tReg2, tReg2, cReg);
2192 opRegRegReg(cUnit, kOpSub, rlResult.lowReg, tReg2, tReg1);
2193 }
Elliott Hughes672511b2010-04-26 17:40:13 -07002194 }
2195 storeValue(cUnit, rlDest, rlResult);
2196 return true;
2197}
2198
Elliott Hughesb4c05972010-02-24 16:36:18 -08002199// Returns true if it added instructions to 'cUnit' to multiply 'rlSrc' by 'lit'
2200// and store the result in 'rlDest'.
2201static bool handleEasyMultiply(CompilationUnit *cUnit,
2202 RegLocation rlSrc, RegLocation rlDest, int lit)
2203{
2204 // Can we simplify this multiplication?
2205 bool powerOfTwo = false;
2206 bool popCountLE2 = false;
2207 bool powerOfTwoMinusOne = false;
2208 if (lit < 2) {
2209 // Avoid special cases.
2210 return false;
2211 } else if (isPowerOfTwo(lit)) {
2212 powerOfTwo = true;
2213 } else if (isPopCountLE2(lit)) {
2214 popCountLE2 = true;
2215 } else if (isPowerOfTwo(lit + 1)) {
2216 powerOfTwoMinusOne = true;
2217 } else {
2218 return false;
2219 }
2220 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
2221 RegLocation rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
2222 if (powerOfTwo) {
2223 // Shift.
2224 opRegRegImm(cUnit, kOpLsl, rlResult.lowReg, rlSrc.lowReg,
2225 lowestSetBit(lit));
2226 } else if (popCountLE2) {
2227 // Shift and add and shift.
2228 int firstBit = lowestSetBit(lit);
2229 int secondBit = lowestSetBit(lit ^ (1 << firstBit));
2230 genMultiplyByTwoBitMultiplier(cUnit, rlSrc, rlResult, lit,
2231 firstBit, secondBit);
2232 } else {
2233 // Reverse subtract: (src << (shift + 1)) - src.
2234 assert(powerOfTwoMinusOne);
2235 // TODO: rsb dst, src, src lsl#lowestSetBit(lit + 1)
2236 int tReg = dvmCompilerAllocTemp(cUnit);
2237 opRegRegImm(cUnit, kOpLsl, tReg, rlSrc.lowReg, lowestSetBit(lit + 1));
2238 opRegRegReg(cUnit, kOpSub, rlResult.lowReg, tReg, rlSrc.lowReg);
2239 }
2240 storeValue(cUnit, rlDest, rlResult);
2241 return true;
Bill Buzbee78cb0e22010-02-11 14:04:53 -08002242}
2243
Ben Chengba4fc8b2009-06-01 13:00:29 -07002244static bool handleFmt22b_Fmt22s(CompilationUnit *cUnit, MIR *mir)
2245{
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002246 Opcode dalvikOpcode = mir->dalvikInsn.opcode;
Bill Buzbeec6f10662010-02-09 11:16:15 -08002247 RegLocation rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
2248 RegLocation rlDest = dvmCompilerGetDest(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07002249 RegLocation rlResult;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002250 int lit = mir->dalvikInsn.vC;
Ben Cheng4f489172009-09-27 17:08:35 -07002251 OpKind op = 0; /* Make gcc happy */
Bill Buzbee1465db52009-09-23 17:17:35 -07002252 int shiftOp = false;
2253 bool isDiv = false;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002254
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002255 switch (dalvikOpcode) {
Bill Buzbee1465db52009-09-23 17:17:35 -07002256 case OP_RSUB_INT_LIT8:
2257 case OP_RSUB_INT: {
2258 int tReg;
2259 //TUNING: add support for use of Arm rsub op
2260 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
Bill Buzbeec6f10662010-02-09 11:16:15 -08002261 tReg = dvmCompilerAllocTemp(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07002262 loadConstant(cUnit, tReg, lit);
Bill Buzbeec6f10662010-02-09 11:16:15 -08002263 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Bill Buzbee1465db52009-09-23 17:17:35 -07002264 opRegRegReg(cUnit, kOpSub, rlResult.lowReg,
2265 tReg, rlSrc.lowReg);
2266 storeValue(cUnit, rlDest, rlResult);
2267 return false;
2268 break;
2269 }
2270
Ben Chengba4fc8b2009-06-01 13:00:29 -07002271 case OP_ADD_INT_LIT8:
2272 case OP_ADD_INT_LIT16:
Bill Buzbee1465db52009-09-23 17:17:35 -07002273 op = kOpAdd;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002274 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002275 case OP_MUL_INT_LIT8:
Bill Buzbee78cb0e22010-02-11 14:04:53 -08002276 case OP_MUL_INT_LIT16: {
Elliott Hughesb4c05972010-02-24 16:36:18 -08002277 if (handleEasyMultiply(cUnit, rlSrc, rlDest, lit)) {
2278 return false;
Bill Buzbee78cb0e22010-02-11 14:04:53 -08002279 }
Elliott Hughesb4c05972010-02-24 16:36:18 -08002280 op = kOpMul;
Bill Buzbee1465db52009-09-23 17:17:35 -07002281 break;
Bill Buzbee78cb0e22010-02-11 14:04:53 -08002282 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07002283 case OP_AND_INT_LIT8:
2284 case OP_AND_INT_LIT16:
Bill Buzbee1465db52009-09-23 17:17:35 -07002285 op = kOpAnd;
2286 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002287 case OP_OR_INT_LIT8:
2288 case OP_OR_INT_LIT16:
Bill Buzbee1465db52009-09-23 17:17:35 -07002289 op = kOpOr;
2290 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002291 case OP_XOR_INT_LIT8:
2292 case OP_XOR_INT_LIT16:
Bill Buzbee1465db52009-09-23 17:17:35 -07002293 op = kOpXor;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002294 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002295 case OP_SHL_INT_LIT8:
Bill Buzbee0e605272009-12-01 14:28:05 -08002296 lit &= 31;
Bill Buzbee1465db52009-09-23 17:17:35 -07002297 shiftOp = true;
2298 op = kOpLsl;
2299 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002300 case OP_SHR_INT_LIT8:
Bill Buzbee0e605272009-12-01 14:28:05 -08002301 lit &= 31;
Bill Buzbee1465db52009-09-23 17:17:35 -07002302 shiftOp = true;
2303 op = kOpAsr;
2304 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002305 case OP_USHR_INT_LIT8:
Bill Buzbee0e605272009-12-01 14:28:05 -08002306 lit &= 31;
Bill Buzbee1465db52009-09-23 17:17:35 -07002307 shiftOp = true;
2308 op = kOpLsr;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002309 break;
2310
2311 case OP_DIV_INT_LIT8:
2312 case OP_DIV_INT_LIT16:
Ben Chengba4fc8b2009-06-01 13:00:29 -07002313 case OP_REM_INT_LIT8:
2314 case OP_REM_INT_LIT16:
2315 if (lit == 0) {
2316 /* Let the interpreter deal with div by 0 */
2317 genInterpSingleStep(cUnit, mir);
2318 return false;
2319 }
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002320 if (handleEasyDivide(cUnit, dalvikOpcode, rlSrc, rlDest, lit)) {
Elliott Hughes672511b2010-04-26 17:40:13 -07002321 return false;
2322 }
Bill Buzbeec6f10662010-02-09 11:16:15 -08002323 dvmCompilerFlushAllRegs(cUnit); /* Everything to home location */
Bill Buzbee1465db52009-09-23 17:17:35 -07002324 loadValueDirectFixed(cUnit, rlSrc, r0);
Bill Buzbeec6f10662010-02-09 11:16:15 -08002325 dvmCompilerClobber(cUnit, r0);
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002326 if ((dalvikOpcode == OP_DIV_INT_LIT8) ||
2327 (dalvikOpcode == OP_DIV_INT_LIT16)) {
Ben Chengbd1326d2010-04-02 15:04:53 -07002328 LOAD_FUNC_ADDR(cUnit, r2, (int)__aeabi_idiv);
Bill Buzbee1465db52009-09-23 17:17:35 -07002329 isDiv = true;
2330 } else {
Ben Chengbd1326d2010-04-02 15:04:53 -07002331 LOAD_FUNC_ADDR(cUnit, r2, (int)__aeabi_idivmod);
Bill Buzbee1465db52009-09-23 17:17:35 -07002332 isDiv = false;
2333 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07002334 loadConstant(cUnit, r1, lit);
Bill Buzbee1465db52009-09-23 17:17:35 -07002335 opReg(cUnit, kOpBlx, r2);
Elliott Hughes6a555132010-02-25 15:41:42 -08002336 dvmCompilerClobberCallRegs(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07002337 if (isDiv)
Bill Buzbeec6f10662010-02-09 11:16:15 -08002338 rlResult = dvmCompilerGetReturn(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07002339 else
Bill Buzbeec6f10662010-02-09 11:16:15 -08002340 rlResult = dvmCompilerGetReturnAlt(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07002341 storeValue(cUnit, rlDest, rlResult);
2342 return false;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002343 break;
2344 default:
2345 return true;
2346 }
Bill Buzbee1465db52009-09-23 17:17:35 -07002347 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
Bill Buzbeec6f10662010-02-09 11:16:15 -08002348 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Bill Buzbee1465db52009-09-23 17:17:35 -07002349 // Avoid shifts by literal 0 - no support in Thumb. Change to copy
2350 if (shiftOp && (lit == 0)) {
2351 genRegCopy(cUnit, rlResult.lowReg, rlSrc.lowReg);
2352 } else {
2353 opRegRegImm(cUnit, op, rlResult.lowReg, rlSrc.lowReg, lit);
2354 }
2355 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002356 return false;
2357}
2358
jeffhao71eee1f2011-01-04 14:18:54 -08002359static bool handleFmt22c_Fmt52c(CompilationUnit *cUnit, MIR *mir)
Ben Chengba4fc8b2009-06-01 13:00:29 -07002360{
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002361 Opcode dalvikOpcode = mir->dalvikInsn.opcode;
buzbee4d92e682010-07-29 15:24:14 -07002362 int fieldOffset = -1;
buzbeeecf8f6e2010-07-20 14:53:42 -07002363 bool isVolatile = false;
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002364 switch (dalvikOpcode) {
buzbee4d92e682010-07-29 15:24:14 -07002365 /*
2366 * Wide volatiles currently handled via single step.
2367 * Add them here if generating in-line code.
2368 * case OP_IGET_WIDE_VOLATILE:
2369 * case OP_IPUT_WIDE_VOLATILE:
2370 */
2371 case OP_IGET:
2372 case OP_IGET_VOLATILE:
jeffhao71eee1f2011-01-04 14:18:54 -08002373 case OP_IGET_JUMBO:
buzbee4d92e682010-07-29 15:24:14 -07002374 case OP_IGET_WIDE:
jeffhao71eee1f2011-01-04 14:18:54 -08002375 case OP_IGET_WIDE_JUMBO:
buzbee4d92e682010-07-29 15:24:14 -07002376 case OP_IGET_OBJECT:
2377 case OP_IGET_OBJECT_VOLATILE:
jeffhao71eee1f2011-01-04 14:18:54 -08002378 case OP_IGET_OBJECT_JUMBO:
buzbee4d92e682010-07-29 15:24:14 -07002379 case OP_IGET_BOOLEAN:
jeffhao71eee1f2011-01-04 14:18:54 -08002380 case OP_IGET_BOOLEAN_JUMBO:
buzbee4d92e682010-07-29 15:24:14 -07002381 case OP_IGET_BYTE:
jeffhao71eee1f2011-01-04 14:18:54 -08002382 case OP_IGET_BYTE_JUMBO:
buzbee4d92e682010-07-29 15:24:14 -07002383 case OP_IGET_CHAR:
jeffhao71eee1f2011-01-04 14:18:54 -08002384 case OP_IGET_CHAR_JUMBO:
buzbee4d92e682010-07-29 15:24:14 -07002385 case OP_IGET_SHORT:
jeffhao71eee1f2011-01-04 14:18:54 -08002386 case OP_IGET_SHORT_JUMBO:
buzbee4d92e682010-07-29 15:24:14 -07002387 case OP_IPUT:
2388 case OP_IPUT_VOLATILE:
jeffhao71eee1f2011-01-04 14:18:54 -08002389 case OP_IPUT_JUMBO:
buzbee4d92e682010-07-29 15:24:14 -07002390 case OP_IPUT_WIDE:
jeffhao71eee1f2011-01-04 14:18:54 -08002391 case OP_IPUT_WIDE_JUMBO:
buzbee4d92e682010-07-29 15:24:14 -07002392 case OP_IPUT_OBJECT:
2393 case OP_IPUT_OBJECT_VOLATILE:
jeffhao71eee1f2011-01-04 14:18:54 -08002394 case OP_IPUT_OBJECT_JUMBO:
buzbee4d92e682010-07-29 15:24:14 -07002395 case OP_IPUT_BOOLEAN:
jeffhao71eee1f2011-01-04 14:18:54 -08002396 case OP_IPUT_BOOLEAN_JUMBO:
buzbee4d92e682010-07-29 15:24:14 -07002397 case OP_IPUT_BYTE:
jeffhao71eee1f2011-01-04 14:18:54 -08002398 case OP_IPUT_BYTE_JUMBO:
buzbee4d92e682010-07-29 15:24:14 -07002399 case OP_IPUT_CHAR:
jeffhao71eee1f2011-01-04 14:18:54 -08002400 case OP_IPUT_CHAR_JUMBO:
2401 case OP_IPUT_SHORT:
2402 case OP_IPUT_SHORT_JUMBO: {
Ben Cheng7a2697d2010-06-07 13:44:23 -07002403 const Method *method = (mir->OptimizationFlags & MIR_CALLEE) ?
2404 mir->meta.calleeMethod : cUnit->method;
buzbee4d92e682010-07-29 15:24:14 -07002405 Field *fieldPtr =
Ben Cheng7a2697d2010-06-07 13:44:23 -07002406 method->clazz->pDvmDex->pResFields[mir->dalvikInsn.vC];
Ben Chengba4fc8b2009-06-01 13:00:29 -07002407
buzbee4d92e682010-07-29 15:24:14 -07002408 if (fieldPtr == NULL) {
2409 LOGE("Unexpected null instance field");
2410 dvmAbort();
2411 }
2412 isVolatile = dvmIsVolatileField(fieldPtr);
2413 fieldOffset = ((InstField *)fieldPtr)->byteOffset;
2414 break;
Ben Chengdd6e8702010-05-07 13:05:47 -07002415 }
buzbee4d92e682010-07-29 15:24:14 -07002416 default:
2417 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002418 }
buzbee4d92e682010-07-29 15:24:14 -07002419
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002420 switch (dalvikOpcode) {
jeffhao71eee1f2011-01-04 14:18:54 -08002421 case OP_NEW_ARRAY:
2422 case OP_NEW_ARRAY_JUMBO: {
Bill Buzbee1465db52009-09-23 17:17:35 -07002423 // Generates a call - use explicit registers
Bill Buzbeec6f10662010-02-09 11:16:15 -08002424 RegLocation rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
2425 RegLocation rlDest = dvmCompilerGetDest(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07002426 RegLocation rlResult;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002427 void *classPtr = (void*)
2428 (cUnit->method->clazz->pDvmDex->pResClasses[mir->dalvikInsn.vC]);
Ben Chengdd6e8702010-05-07 13:05:47 -07002429
2430 if (classPtr == NULL) {
2431 LOGE("Unexpected null class");
2432 dvmAbort();
2433 }
2434
Bill Buzbeec6f10662010-02-09 11:16:15 -08002435 dvmCompilerFlushAllRegs(cUnit); /* Everything to home location */
Bill Buzbee1465db52009-09-23 17:17:35 -07002436 genExportPC(cUnit, mir);
2437 loadValueDirectFixed(cUnit, rlSrc, r1); /* Len */
Ben Chengba4fc8b2009-06-01 13:00:29 -07002438 loadConstant(cUnit, r0, (int) classPtr );
Ben Chengbd1326d2010-04-02 15:04:53 -07002439 LOAD_FUNC_ADDR(cUnit, r3, (int)dvmAllocArrayByClass);
Ben Cheng4f489172009-09-27 17:08:35 -07002440 /*
2441 * "len < 0": bail to the interpreter to re-execute the
2442 * instruction
2443 */
Carl Shapiroe3c01da2010-05-20 22:54:18 -07002444 genRegImmCheck(cUnit, kArmCondMi, r1, 0, mir->offset, NULL);
Bill Buzbee270c1d62009-08-13 16:58:07 -07002445 loadConstant(cUnit, r2, ALLOC_DONT_TRACK);
Bill Buzbee1465db52009-09-23 17:17:35 -07002446 opReg(cUnit, kOpBlx, r3);
Elliott Hughes6a555132010-02-25 15:41:42 -08002447 dvmCompilerClobberCallRegs(cUnit);
Ben Cheng4f489172009-09-27 17:08:35 -07002448 /* generate a branch over if allocation is successful */
buzbee8f8109a2010-08-31 10:16:35 -07002449 ArmLIR *branchOver = genCmpImmBranch(cUnit, kArmCondNe, r0, 0);
Ben Cheng4f489172009-09-27 17:08:35 -07002450 /*
2451 * OOM exception needs to be thrown here and cannot re-execute
2452 */
2453 loadConstant(cUnit, r0,
2454 (int) (cUnit->method->insns + mir->offset));
2455 genDispatchToHandler(cUnit, TEMPLATE_THROW_EXCEPTION_COMMON);
2456 /* noreturn */
2457
Bill Buzbee1465db52009-09-23 17:17:35 -07002458 ArmLIR *target = newLIR0(cUnit, kArmPseudoTargetLabel);
Ben Cheng4f489172009-09-27 17:08:35 -07002459 target->defMask = ENCODE_ALL;
2460 branchOver->generic.target = (LIR *) target;
Bill Buzbeec6f10662010-02-09 11:16:15 -08002461 rlResult = dvmCompilerGetReturn(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07002462 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002463 break;
2464 }
jeffhao71eee1f2011-01-04 14:18:54 -08002465 case OP_INSTANCE_OF:
2466 case OP_INSTANCE_OF_JUMBO: {
Bill Buzbee1465db52009-09-23 17:17:35 -07002467 // May generate a call - use explicit registers
Bill Buzbeec6f10662010-02-09 11:16:15 -08002468 RegLocation rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
2469 RegLocation rlDest = dvmCompilerGetDest(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07002470 RegLocation rlResult;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002471 ClassObject *classPtr =
2472 (cUnit->method->clazz->pDvmDex->pResClasses[mir->dalvikInsn.vC]);
Bill Buzbee480e6782010-01-27 15:43:08 -08002473 /*
2474 * Note: It is possible that classPtr is NULL at this point,
2475 * even though this instruction has been successfully interpreted.
2476 * If the previous interpretation had a null source, the
2477 * interpreter would not have bothered to resolve the clazz.
2478 * Bail out to the interpreter in this case, and log it
2479 * so that we can tell if it happens frequently.
2480 */
2481 if (classPtr == NULL) {
2482 LOGD("null clazz in OP_INSTANCE_OF, single-stepping");
2483 genInterpSingleStep(cUnit, mir);
2484 break;
2485 }
Bill Buzbeec6f10662010-02-09 11:16:15 -08002486 dvmCompilerFlushAllRegs(cUnit); /* Everything to home location */
Bill Buzbee1465db52009-09-23 17:17:35 -07002487 loadValueDirectFixed(cUnit, rlSrc, r0); /* Ref */
Ben Chengba4fc8b2009-06-01 13:00:29 -07002488 loadConstant(cUnit, r2, (int) classPtr );
Ben Cheng752c7942009-06-22 10:50:07 -07002489 /* When taken r0 has NULL which can be used for store directly */
buzbee8f8109a2010-08-31 10:16:35 -07002490 ArmLIR *branch1 = genCmpImmBranch(cUnit, kArmCondEq, r0, 0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002491 /* r1 now contains object->clazz */
Bill Buzbee270c1d62009-08-13 16:58:07 -07002492 loadWordDisp(cUnit, r0, offsetof(Object, clazz), r1);
Bill Buzbee1465db52009-09-23 17:17:35 -07002493 /* r1 now contains object->clazz */
Ben Chengbd1326d2010-04-02 15:04:53 -07002494 LOAD_FUNC_ADDR(cUnit, r3, (int)dvmInstanceofNonTrivial);
Ben Cheng752c7942009-06-22 10:50:07 -07002495 loadConstant(cUnit, r0, 1); /* Assume true */
Bill Buzbee1465db52009-09-23 17:17:35 -07002496 opRegReg(cUnit, kOpCmp, r1, r2);
2497 ArmLIR *branch2 = opCondBranch(cUnit, kArmCondEq);
2498 genRegCopy(cUnit, r0, r1);
2499 genRegCopy(cUnit, r1, r2);
2500 opReg(cUnit, kOpBlx, r3);
Elliott Hughes6a555132010-02-25 15:41:42 -08002501 dvmCompilerClobberCallRegs(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002502 /* branch target here */
Bill Buzbee1465db52009-09-23 17:17:35 -07002503 ArmLIR *target = newLIR0(cUnit, kArmPseudoTargetLabel);
Ben Chengd7d426a2009-09-22 11:23:36 -07002504 target->defMask = ENCODE_ALL;
Bill Buzbeec6f10662010-02-09 11:16:15 -08002505 rlResult = dvmCompilerGetReturn(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07002506 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002507 branch1->generic.target = (LIR *)target;
2508 branch2->generic.target = (LIR *)target;
2509 break;
2510 }
2511 case OP_IGET_WIDE:
jeffhao71eee1f2011-01-04 14:18:54 -08002512 case OP_IGET_WIDE_JUMBO:
Ben Chengba4fc8b2009-06-01 13:00:29 -07002513 genIGetWide(cUnit, mir, fieldOffset);
2514 break;
buzbeeecf8f6e2010-07-20 14:53:42 -07002515 case OP_IGET_VOLATILE:
2516 case OP_IGET_OBJECT_VOLATILE:
2517 isVolatile = true;
2518 // NOTE: intentional fallthrough
Ben Chengba4fc8b2009-06-01 13:00:29 -07002519 case OP_IGET:
jeffhao71eee1f2011-01-04 14:18:54 -08002520 case OP_IGET_JUMBO:
Ben Chengba4fc8b2009-06-01 13:00:29 -07002521 case OP_IGET_OBJECT:
jeffhao71eee1f2011-01-04 14:18:54 -08002522 case OP_IGET_OBJECT_JUMBO:
Ben Chengba4fc8b2009-06-01 13:00:29 -07002523 case OP_IGET_BOOLEAN:
jeffhao71eee1f2011-01-04 14:18:54 -08002524 case OP_IGET_BOOLEAN_JUMBO:
Ben Chengba4fc8b2009-06-01 13:00:29 -07002525 case OP_IGET_BYTE:
jeffhao71eee1f2011-01-04 14:18:54 -08002526 case OP_IGET_BYTE_JUMBO:
Ben Chengba4fc8b2009-06-01 13:00:29 -07002527 case OP_IGET_CHAR:
jeffhao71eee1f2011-01-04 14:18:54 -08002528 case OP_IGET_CHAR_JUMBO:
Ben Chengba4fc8b2009-06-01 13:00:29 -07002529 case OP_IGET_SHORT:
jeffhao71eee1f2011-01-04 14:18:54 -08002530 case OP_IGET_SHORT_JUMBO:
buzbee3272e2f2010-09-09 14:07:01 -07002531 genIGet(cUnit, mir, kWord, fieldOffset, isVolatile);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002532 break;
2533 case OP_IPUT_WIDE:
jeffhao71eee1f2011-01-04 14:18:54 -08002534 case OP_IPUT_WIDE_JUMBO:
Ben Chengba4fc8b2009-06-01 13:00:29 -07002535 genIPutWide(cUnit, mir, fieldOffset);
2536 break;
2537 case OP_IPUT:
jeffhao71eee1f2011-01-04 14:18:54 -08002538 case OP_IPUT_JUMBO:
buzbee3272e2f2010-09-09 14:07:01 -07002539 case OP_IPUT_BOOLEAN:
jeffhao71eee1f2011-01-04 14:18:54 -08002540 case OP_IPUT_BOOLEAN_JUMBO:
2541 case OP_IPUT_BYTE:
2542 case OP_IPUT_BYTE_JUMBO:
2543 case OP_IPUT_CHAR:
2544 case OP_IPUT_CHAR_JUMBO:
2545 case OP_IPUT_SHORT:
2546 case OP_IPUT_SHORT_JUMBO:
buzbeeecf8f6e2010-07-20 14:53:42 -07002547 genIPut(cUnit, mir, kWord, fieldOffset, false, isVolatile);
buzbee919eb062010-07-12 12:59:22 -07002548 break;
buzbee4d92e682010-07-29 15:24:14 -07002549 case OP_IPUT_VOLATILE:
buzbeeecf8f6e2010-07-20 14:53:42 -07002550 case OP_IPUT_OBJECT_VOLATILE:
2551 isVolatile = true;
2552 // NOTE: intentional fallthrough
Ben Chengba4fc8b2009-06-01 13:00:29 -07002553 case OP_IPUT_OBJECT:
jeffhao71eee1f2011-01-04 14:18:54 -08002554 case OP_IPUT_OBJECT_JUMBO:
buzbeeecf8f6e2010-07-20 14:53:42 -07002555 genIPut(cUnit, mir, kWord, fieldOffset, true, isVolatile);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002556 break;
Bill Buzbeeb16344a2010-03-15 17:19:12 -07002557 case OP_IGET_WIDE_VOLATILE:
2558 case OP_IPUT_WIDE_VOLATILE:
Bill Buzbeeb16344a2010-03-15 17:19:12 -07002559 genInterpSingleStep(cUnit, mir);
2560 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002561 default:
2562 return true;
2563 }
2564 return false;
2565}
2566
2567static bool handleFmt22cs(CompilationUnit *cUnit, MIR *mir)
2568{
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002569 Opcode dalvikOpcode = mir->dalvikInsn.opcode;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002570 int fieldOffset = mir->dalvikInsn.vC;
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002571 switch (dalvikOpcode) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07002572 case OP_IGET_QUICK:
2573 case OP_IGET_OBJECT_QUICK:
buzbeeecf8f6e2010-07-20 14:53:42 -07002574 genIGet(cUnit, mir, kWord, fieldOffset, false);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002575 break;
2576 case OP_IPUT_QUICK:
buzbeeecf8f6e2010-07-20 14:53:42 -07002577 genIPut(cUnit, mir, kWord, fieldOffset, false, false);
buzbee919eb062010-07-12 12:59:22 -07002578 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002579 case OP_IPUT_OBJECT_QUICK:
buzbeeecf8f6e2010-07-20 14:53:42 -07002580 genIPut(cUnit, mir, kWord, fieldOffset, true, false);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002581 break;
2582 case OP_IGET_WIDE_QUICK:
2583 genIGetWide(cUnit, mir, fieldOffset);
2584 break;
2585 case OP_IPUT_WIDE_QUICK:
2586 genIPutWide(cUnit, mir, fieldOffset);
2587 break;
2588 default:
2589 return true;
2590 }
2591 return false;
2592
2593}
2594
2595/* Compare agaist zero */
2596static bool handleFmt22t(CompilationUnit *cUnit, MIR *mir, BasicBlock *bb,
Bill Buzbee89efc3d2009-07-28 11:22:22 -07002597 ArmLIR *labelList)
Ben Chengba4fc8b2009-06-01 13:00:29 -07002598{
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002599 Opcode dalvikOpcode = mir->dalvikInsn.opcode;
Bill Buzbee89efc3d2009-07-28 11:22:22 -07002600 ArmConditionCode cond;
Ben Cheng7ab74e12011-02-03 14:02:06 -08002601 /* backward branch? */
2602 bool backwardBranch = (bb->taken->startOffset <= mir->offset);
2603
2604 if (backwardBranch && gDvmJit.genSuspendPoll) {
2605 genSuspendPoll(cUnit, mir);
2606 }
2607
Bill Buzbeec6f10662010-02-09 11:16:15 -08002608 RegLocation rlSrc1 = dvmCompilerGetSrc(cUnit, mir, 0);
2609 RegLocation rlSrc2 = dvmCompilerGetSrc(cUnit, mir, 1);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002610
Bill Buzbee1465db52009-09-23 17:17:35 -07002611 rlSrc1 = loadValue(cUnit, rlSrc1, kCoreReg);
2612 rlSrc2 = loadValue(cUnit, rlSrc2, kCoreReg);
Ben Cheng7ab74e12011-02-03 14:02:06 -08002613
Bill Buzbee1465db52009-09-23 17:17:35 -07002614 opRegReg(cUnit, kOpCmp, rlSrc1.lowReg, rlSrc2.lowReg);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002615
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002616 switch (dalvikOpcode) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07002617 case OP_IF_EQ:
Bill Buzbee1465db52009-09-23 17:17:35 -07002618 cond = kArmCondEq;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002619 break;
2620 case OP_IF_NE:
Bill Buzbee1465db52009-09-23 17:17:35 -07002621 cond = kArmCondNe;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002622 break;
2623 case OP_IF_LT:
Bill Buzbee1465db52009-09-23 17:17:35 -07002624 cond = kArmCondLt;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002625 break;
2626 case OP_IF_GE:
Bill Buzbee1465db52009-09-23 17:17:35 -07002627 cond = kArmCondGe;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002628 break;
2629 case OP_IF_GT:
Bill Buzbee1465db52009-09-23 17:17:35 -07002630 cond = kArmCondGt;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002631 break;
2632 case OP_IF_LE:
Bill Buzbee1465db52009-09-23 17:17:35 -07002633 cond = kArmCondLe;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002634 break;
2635 default:
2636 cond = 0;
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002637 LOGE("Unexpected opcode (%d) for Fmt22t\n", dalvikOpcode);
Bill Buzbeefc519dc2010-03-06 23:30:57 -08002638 dvmCompilerAbort(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002639 }
2640 genConditionalBranch(cUnit, cond, &labelList[bb->taken->id]);
2641 /* This mostly likely will be optimized away in a later phase */
2642 genUnconditionalBranch(cUnit, &labelList[bb->fallThrough->id]);
2643 return false;
2644}
2645
2646static bool handleFmt22x_Fmt32x(CompilationUnit *cUnit, MIR *mir)
2647{
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002648 Opcode opcode = mir->dalvikInsn.opcode;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002649
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002650 switch (opcode) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07002651 case OP_MOVE_16:
2652 case OP_MOVE_OBJECT_16:
2653 case OP_MOVE_FROM16:
Ben Chenge9695e52009-06-16 16:11:47 -07002654 case OP_MOVE_OBJECT_FROM16: {
Bill Buzbeec6f10662010-02-09 11:16:15 -08002655 storeValue(cUnit, dvmCompilerGetDest(cUnit, mir, 0),
2656 dvmCompilerGetSrc(cUnit, mir, 0));
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 case OP_MOVE_WIDE_16:
Ben Chenge9695e52009-06-16 16:11:47 -07002660 case OP_MOVE_WIDE_FROM16: {
Bill Buzbeec6f10662010-02-09 11:16:15 -08002661 storeValueWide(cUnit, dvmCompilerGetDestWide(cUnit, mir, 0, 1),
2662 dvmCompilerGetSrcWide(cUnit, mir, 0, 1));
Ben Chengba4fc8b2009-06-01 13:00:29 -07002663 break;
Ben Chenge9695e52009-06-16 16:11:47 -07002664 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07002665 default:
2666 return true;
2667 }
2668 return false;
2669}
2670
2671static bool handleFmt23x(CompilationUnit *cUnit, MIR *mir)
2672{
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002673 Opcode opcode = mir->dalvikInsn.opcode;
Bill Buzbee1465db52009-09-23 17:17:35 -07002674 RegLocation rlSrc1;
2675 RegLocation rlSrc2;
2676 RegLocation rlDest;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002677
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002678 if ( (opcode >= OP_ADD_INT) && (opcode <= OP_REM_DOUBLE)) {
Ben Cheng5d90c202009-11-22 23:31:11 -08002679 return genArithOp( cUnit, mir );
Ben Chengba4fc8b2009-06-01 13:00:29 -07002680 }
2681
Bill Buzbee1465db52009-09-23 17:17:35 -07002682 /* APUTs have 3 sources and no targets */
2683 if (mir->ssaRep->numDefs == 0) {
2684 if (mir->ssaRep->numUses == 3) {
Bill Buzbeec6f10662010-02-09 11:16:15 -08002685 rlDest = dvmCompilerGetSrc(cUnit, mir, 0);
2686 rlSrc1 = dvmCompilerGetSrc(cUnit, mir, 1);
2687 rlSrc2 = dvmCompilerGetSrc(cUnit, mir, 2);
Bill Buzbee1465db52009-09-23 17:17:35 -07002688 } else {
2689 assert(mir->ssaRep->numUses == 4);
Bill Buzbeec6f10662010-02-09 11:16:15 -08002690 rlDest = dvmCompilerGetSrcWide(cUnit, mir, 0, 1);
2691 rlSrc1 = dvmCompilerGetSrc(cUnit, mir, 2);
2692 rlSrc2 = dvmCompilerGetSrc(cUnit, mir, 3);
Bill Buzbee1465db52009-09-23 17:17:35 -07002693 }
2694 } else {
2695 /* Two sources and 1 dest. Deduce the operand sizes */
2696 if (mir->ssaRep->numUses == 4) {
Bill Buzbeec6f10662010-02-09 11:16:15 -08002697 rlSrc1 = dvmCompilerGetSrcWide(cUnit, mir, 0, 1);
2698 rlSrc2 = dvmCompilerGetSrcWide(cUnit, mir, 2, 3);
Bill Buzbee1465db52009-09-23 17:17:35 -07002699 } else {
2700 assert(mir->ssaRep->numUses == 2);
Bill Buzbeec6f10662010-02-09 11:16:15 -08002701 rlSrc1 = dvmCompilerGetSrc(cUnit, mir, 0);
2702 rlSrc2 = dvmCompilerGetSrc(cUnit, mir, 1);
Bill Buzbee1465db52009-09-23 17:17:35 -07002703 }
2704 if (mir->ssaRep->numDefs == 2) {
Bill Buzbeec6f10662010-02-09 11:16:15 -08002705 rlDest = dvmCompilerGetDestWide(cUnit, mir, 0, 1);
Bill Buzbee1465db52009-09-23 17:17:35 -07002706 } else {
2707 assert(mir->ssaRep->numDefs == 1);
Bill Buzbeec6f10662010-02-09 11:16:15 -08002708 rlDest = dvmCompilerGetDest(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07002709 }
2710 }
2711
2712
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002713 switch (opcode) {
Bill Buzbeed45ba372009-06-15 17:00:57 -07002714 case OP_CMPL_FLOAT:
2715 case OP_CMPG_FLOAT:
2716 case OP_CMPL_DOUBLE:
2717 case OP_CMPG_DOUBLE:
Ben Cheng5d90c202009-11-22 23:31:11 -08002718 return genCmpFP(cUnit, mir, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002719 case OP_CMP_LONG:
Bill Buzbee1465db52009-09-23 17:17:35 -07002720 genCmpLong(cUnit, mir, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002721 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002722 case OP_AGET_WIDE:
Bill Buzbee1465db52009-09-23 17:17:35 -07002723 genArrayGet(cUnit, mir, kLong, rlSrc1, rlSrc2, rlDest, 3);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002724 break;
2725 case OP_AGET:
2726 case OP_AGET_OBJECT:
Bill Buzbee1465db52009-09-23 17:17:35 -07002727 genArrayGet(cUnit, mir, kWord, rlSrc1, rlSrc2, rlDest, 2);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002728 break;
2729 case OP_AGET_BOOLEAN:
Bill Buzbee1465db52009-09-23 17:17:35 -07002730 genArrayGet(cUnit, mir, kUnsignedByte, rlSrc1, rlSrc2, rlDest, 0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002731 break;
2732 case OP_AGET_BYTE:
Bill Buzbee1465db52009-09-23 17:17:35 -07002733 genArrayGet(cUnit, mir, kSignedByte, rlSrc1, rlSrc2, rlDest, 0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002734 break;
2735 case OP_AGET_CHAR:
Bill Buzbee1465db52009-09-23 17:17:35 -07002736 genArrayGet(cUnit, mir, kUnsignedHalf, rlSrc1, rlSrc2, rlDest, 1);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002737 break;
2738 case OP_AGET_SHORT:
Bill Buzbee1465db52009-09-23 17:17:35 -07002739 genArrayGet(cUnit, mir, kSignedHalf, rlSrc1, rlSrc2, rlDest, 1);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002740 break;
2741 case OP_APUT_WIDE:
Bill Buzbee1465db52009-09-23 17:17:35 -07002742 genArrayPut(cUnit, mir, kLong, rlSrc1, rlSrc2, rlDest, 3);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002743 break;
2744 case OP_APUT:
Bill Buzbee1465db52009-09-23 17:17:35 -07002745 genArrayPut(cUnit, mir, kWord, rlSrc1, rlSrc2, rlDest, 2);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002746 break;
Bill Buzbeebe6534f2010-03-12 16:01:35 -08002747 case OP_APUT_OBJECT:
2748 genArrayObjectPut(cUnit, mir, rlSrc1, rlSrc2, rlDest, 2);
2749 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002750 case OP_APUT_SHORT:
2751 case OP_APUT_CHAR:
Bill Buzbee1465db52009-09-23 17:17:35 -07002752 genArrayPut(cUnit, mir, kUnsignedHalf, rlSrc1, rlSrc2, rlDest, 1);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002753 break;
2754 case OP_APUT_BYTE:
2755 case OP_APUT_BOOLEAN:
Bill Buzbee1465db52009-09-23 17:17:35 -07002756 genArrayPut(cUnit, mir, kUnsignedByte, rlSrc1, rlSrc2, rlDest, 0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002757 break;
2758 default:
2759 return true;
2760 }
2761 return false;
2762}
2763
Ben Cheng6c10a972009-10-29 14:39:18 -07002764/*
2765 * Find the matching case.
2766 *
2767 * return values:
2768 * r0 (low 32-bit): pc of the chaining cell corresponding to the resolved case,
2769 * including default which is placed at MIN(size, MAX_CHAINED_SWITCH_CASES).
2770 * r1 (high 32-bit): the branch offset of the matching case (only for indexes
2771 * above MAX_CHAINED_SWITCH_CASES).
2772 *
2773 * Instructions around the call are:
2774 *
2775 * mov r2, pc
2776 * blx &findPackedSwitchIndex
2777 * mov pc, r0
2778 * .align4
Bill Buzbeebd047242010-05-13 13:02:53 -07002779 * chaining cell for case 0 [12 bytes]
2780 * chaining cell for case 1 [12 bytes]
Ben Cheng6c10a972009-10-29 14:39:18 -07002781 * :
Bill Buzbeebd047242010-05-13 13:02:53 -07002782 * chaining cell for case MIN(size, MAX_CHAINED_SWITCH_CASES)-1 [12 bytes]
Ben Cheng6c10a972009-10-29 14:39:18 -07002783 * chaining cell for case default [8 bytes]
2784 * noChain exit
2785 */
Ben Chengbd1326d2010-04-02 15:04:53 -07002786static s8 findPackedSwitchIndex(const u2* switchData, int testVal, int pc)
Ben Cheng6c10a972009-10-29 14:39:18 -07002787{
2788 int size;
2789 int firstKey;
2790 const int *entries;
2791 int index;
2792 int jumpIndex;
2793 int caseDPCOffset = 0;
2794 /* In Thumb mode pc is 4 ahead of the "mov r2, pc" instruction */
2795 int chainingPC = (pc + 4) & ~3;
2796
2797 /*
2798 * Packed switch data format:
2799 * ushort ident = 0x0100 magic value
2800 * ushort size number of entries in the table
2801 * int first_key first (and lowest) switch case value
2802 * int targets[size] branch targets, relative to switch opcode
2803 *
2804 * Total size is (4+size*2) 16-bit code units.
2805 */
2806 size = switchData[1];
2807 assert(size > 0);
2808
2809 firstKey = switchData[2];
2810 firstKey |= switchData[3] << 16;
2811
2812
2813 /* The entries are guaranteed to be aligned on a 32-bit boundary;
2814 * we can treat them as a native int array.
2815 */
2816 entries = (const int*) &switchData[4];
2817 assert(((u4)entries & 0x3) == 0);
2818
2819 index = testVal - firstKey;
2820
2821 /* Jump to the default cell */
2822 if (index < 0 || index >= size) {
2823 jumpIndex = MIN(size, MAX_CHAINED_SWITCH_CASES);
2824 /* Jump to the non-chaining exit point */
2825 } else if (index >= MAX_CHAINED_SWITCH_CASES) {
2826 jumpIndex = MAX_CHAINED_SWITCH_CASES + 1;
2827 caseDPCOffset = entries[index];
2828 /* Jump to the inline chaining cell */
2829 } else {
2830 jumpIndex = index;
2831 }
2832
Bill Buzbeebd047242010-05-13 13:02:53 -07002833 chainingPC += jumpIndex * CHAIN_CELL_NORMAL_SIZE;
Ben Cheng6c10a972009-10-29 14:39:18 -07002834 return (((s8) caseDPCOffset) << 32) | (u8) chainingPC;
2835}
2836
2837/* See comments for findPackedSwitchIndex */
Ben Chengbd1326d2010-04-02 15:04:53 -07002838static s8 findSparseSwitchIndex(const u2* switchData, int testVal, int pc)
Ben Cheng6c10a972009-10-29 14:39:18 -07002839{
2840 int size;
2841 const int *keys;
2842 const int *entries;
2843 int chainingPC = (pc + 4) & ~3;
2844 int i;
2845
2846 /*
2847 * Sparse switch data format:
2848 * ushort ident = 0x0200 magic value
2849 * ushort size number of entries in the table; > 0
2850 * int keys[size] keys, sorted low-to-high; 32-bit aligned
2851 * int targets[size] branch targets, relative to switch opcode
2852 *
2853 * Total size is (2+size*4) 16-bit code units.
2854 */
2855
2856 size = switchData[1];
2857 assert(size > 0);
2858
2859 /* The keys are guaranteed to be aligned on a 32-bit boundary;
2860 * we can treat them as a native int array.
2861 */
2862 keys = (const int*) &switchData[2];
2863 assert(((u4)keys & 0x3) == 0);
2864
2865 /* The entries are guaranteed to be aligned on a 32-bit boundary;
2866 * we can treat them as a native int array.
2867 */
2868 entries = keys + size;
2869 assert(((u4)entries & 0x3) == 0);
2870
2871 /*
2872 * Run through the list of keys, which are guaranteed to
2873 * be sorted low-to-high.
2874 *
2875 * Most tables have 3-4 entries. Few have more than 10. A binary
2876 * search here is probably not useful.
2877 */
2878 for (i = 0; i < size; i++) {
2879 int k = keys[i];
2880 if (k == testVal) {
2881 /* MAX_CHAINED_SWITCH_CASES + 1 is the start of the overflow case */
2882 int jumpIndex = (i < MAX_CHAINED_SWITCH_CASES) ?
2883 i : MAX_CHAINED_SWITCH_CASES + 1;
Bill Buzbeebd047242010-05-13 13:02:53 -07002884 chainingPC += jumpIndex * CHAIN_CELL_NORMAL_SIZE;
Ben Cheng6c10a972009-10-29 14:39:18 -07002885 return (((s8) entries[i]) << 32) | (u8) chainingPC;
2886 } else if (k > testVal) {
2887 break;
2888 }
2889 }
Bill Buzbeebd047242010-05-13 13:02:53 -07002890 return chainingPC + MIN(size, MAX_CHAINED_SWITCH_CASES) *
2891 CHAIN_CELL_NORMAL_SIZE;
Ben Cheng6c10a972009-10-29 14:39:18 -07002892}
2893
Ben Chengba4fc8b2009-06-01 13:00:29 -07002894static bool handleFmt31t(CompilationUnit *cUnit, MIR *mir)
2895{
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002896 Opcode dalvikOpcode = mir->dalvikInsn.opcode;
2897 switch (dalvikOpcode) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07002898 case OP_FILL_ARRAY_DATA: {
Bill Buzbeec6f10662010-02-09 11:16:15 -08002899 RegLocation rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07002900 // Making a call - use explicit registers
Bill Buzbeec6f10662010-02-09 11:16:15 -08002901 dvmCompilerFlushAllRegs(cUnit); /* Everything to home location */
Bill Buzbee1465db52009-09-23 17:17:35 -07002902 genExportPC(cUnit, mir);
2903 loadValueDirectFixed(cUnit, rlSrc, r0);
Ben Chengbd1326d2010-04-02 15:04:53 -07002904 LOAD_FUNC_ADDR(cUnit, r2, (int)dvmInterpHandleFillArrayData);
Ben Cheng6c10a972009-10-29 14:39:18 -07002905 loadConstant(cUnit, r1,
2906 (int) (cUnit->method->insns + mir->offset + mir->dalvikInsn.vB));
Bill Buzbee1465db52009-09-23 17:17:35 -07002907 opReg(cUnit, kOpBlx, r2);
Elliott Hughes6a555132010-02-25 15:41:42 -08002908 dvmCompilerClobberCallRegs(cUnit);
Bill Buzbeefd023aa2009-11-02 09:23:49 -08002909 /* generate a branch over if successful */
buzbee8f8109a2010-08-31 10:16:35 -07002910 ArmLIR *branchOver = genCmpImmBranch(cUnit, kArmCondNe, r0, 0);
Bill Buzbeefd023aa2009-11-02 09:23:49 -08002911 loadConstant(cUnit, r0,
2912 (int) (cUnit->method->insns + mir->offset));
2913 genDispatchToHandler(cUnit, TEMPLATE_THROW_EXCEPTION_COMMON);
2914 ArmLIR *target = newLIR0(cUnit, kArmPseudoTargetLabel);
2915 target->defMask = ENCODE_ALL;
2916 branchOver->generic.target = (LIR *) target;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002917 break;
2918 }
2919 /*
Ben Cheng6c10a972009-10-29 14:39:18 -07002920 * Compute the goto target of up to
2921 * MIN(switchSize, MAX_CHAINED_SWITCH_CASES) + 1 chaining cells.
2922 * See the comment before findPackedSwitchIndex for the code layout.
Ben Chengba4fc8b2009-06-01 13:00:29 -07002923 */
2924 case OP_PACKED_SWITCH:
2925 case OP_SPARSE_SWITCH: {
Bill Buzbeec6f10662010-02-09 11:16:15 -08002926 RegLocation rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
2927 dvmCompilerFlushAllRegs(cUnit); /* Everything to home location */
Bill Buzbee1465db52009-09-23 17:17:35 -07002928 loadValueDirectFixed(cUnit, rlSrc, r1);
Bill Buzbeec6f10662010-02-09 11:16:15 -08002929 dvmCompilerLockAllTemps(cUnit);
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002930 if (dalvikOpcode == OP_PACKED_SWITCH) {
Ben Chengbd1326d2010-04-02 15:04:53 -07002931 LOAD_FUNC_ADDR(cUnit, r4PC, (int)findPackedSwitchIndex);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002932 } else {
Ben Chengbd1326d2010-04-02 15:04:53 -07002933 LOAD_FUNC_ADDR(cUnit, r4PC, (int)findSparseSwitchIndex);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002934 }
Ben Cheng6c10a972009-10-29 14:39:18 -07002935 /* r0 <- Addr of the switch data */
2936 loadConstant(cUnit, r0,
2937 (int) (cUnit->method->insns + mir->offset + mir->dalvikInsn.vB));
2938 /* r2 <- pc of the instruction following the blx */
Ben Cheng20d7e6c2011-02-18 17:12:42 -08002939 opRegReg(cUnit, kOpMov, r2, r15pc);
Bill Buzbee1465db52009-09-23 17:17:35 -07002940 opReg(cUnit, kOpBlx, r4PC);
Elliott Hughes6a555132010-02-25 15:41:42 -08002941 dvmCompilerClobberCallRegs(cUnit);
Ben Cheng6c10a972009-10-29 14:39:18 -07002942 /* pc <- computed goto target */
Ben Cheng20d7e6c2011-02-18 17:12:42 -08002943 opRegReg(cUnit, kOpMov, r15pc, r0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002944 break;
2945 }
2946 default:
2947 return true;
2948 }
2949 return false;
2950}
2951
Ben Cheng7a2697d2010-06-07 13:44:23 -07002952/*
2953 * See the example of predicted inlining listed before the
2954 * genValidationForPredictedInline function. The function here takes care the
2955 * branch over at 0x4858de78 and the misprediction target at 0x4858de7a.
2956 */
2957static void genLandingPadForMispredictedCallee(CompilationUnit *cUnit, MIR *mir,
2958 BasicBlock *bb,
2959 ArmLIR *labelList)
2960{
2961 BasicBlock *fallThrough = bb->fallThrough;
2962
2963 /* Bypass the move-result block if there is one */
2964 if (fallThrough->firstMIRInsn) {
2965 assert(fallThrough->firstMIRInsn->OptimizationFlags & MIR_INLINED_PRED);
2966 fallThrough = fallThrough->fallThrough;
2967 }
2968 /* Generate a branch over if the predicted inlining is correct */
2969 genUnconditionalBranch(cUnit, &labelList[fallThrough->id]);
2970
2971 /* Reset the register state */
2972 dvmCompilerResetRegPool(cUnit);
2973 dvmCompilerClobberAllRegs(cUnit);
2974 dvmCompilerResetNullCheck(cUnit);
2975
2976 /* Target for the slow invoke path */
2977 ArmLIR *target = newLIR0(cUnit, kArmPseudoTargetLabel);
2978 target->defMask = ENCODE_ALL;
2979 /* Hook up the target to the verification branch */
2980 mir->meta.callsiteInfo->misPredBranchOver->target = (LIR *) target;
2981}
2982
jeffhao71eee1f2011-01-04 14:18:54 -08002983static bool handleFmt35c_3rc_5rc(CompilationUnit *cUnit, MIR *mir,
2984 BasicBlock *bb, ArmLIR *labelList)
Ben Chengba4fc8b2009-06-01 13:00:29 -07002985{
Bill Buzbee9bc3df32009-07-30 10:52:29 -07002986 ArmLIR *retChainingCell = NULL;
Bill Buzbee89efc3d2009-07-28 11:22:22 -07002987 ArmLIR *pcrLabel = NULL;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002988
Ben Cheng7a2697d2010-06-07 13:44:23 -07002989 /* An invoke with the MIR_INLINED is effectively a no-op */
2990 if (mir->OptimizationFlags & MIR_INLINED)
2991 return false;
2992
Bill Buzbeef4ce16f2009-07-28 13:28:25 -07002993 if (bb->fallThrough != NULL)
2994 retChainingCell = &labelList[bb->fallThrough->id];
2995
Ben Chengba4fc8b2009-06-01 13:00:29 -07002996 DecodedInstruction *dInsn = &mir->dalvikInsn;
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002997 switch (mir->dalvikInsn.opcode) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07002998 /*
2999 * calleeMethod = this->clazz->vtable[
3000 * method->clazz->pDvmDex->pResMethods[BBBB]->methodIndex
3001 * ]
3002 */
3003 case OP_INVOKE_VIRTUAL:
jeffhao71eee1f2011-01-04 14:18:54 -08003004 case OP_INVOKE_VIRTUAL_RANGE:
3005 case OP_INVOKE_VIRTUAL_JUMBO: {
Bill Buzbee89efc3d2009-07-28 11:22:22 -07003006 ArmLIR *predChainingCell = &labelList[bb->taken->id];
Ben Chengba4fc8b2009-06-01 13:00:29 -07003007 int methodIndex =
3008 cUnit->method->clazz->pDvmDex->pResMethods[dInsn->vB]->
3009 methodIndex;
3010
Ben Cheng7a2697d2010-06-07 13:44:23 -07003011 /*
3012 * If the invoke has non-null misPredBranchOver, we need to generate
3013 * the non-inlined version of the invoke here to handle the
3014 * mispredicted case.
3015 */
3016 if (mir->meta.callsiteInfo->misPredBranchOver) {
3017 genLandingPadForMispredictedCallee(cUnit, mir, bb, labelList);
3018 }
3019
Dan Bornstein9a1f8162010-12-01 17:02:26 -08003020 if (mir->dalvikInsn.opcode == OP_INVOKE_VIRTUAL)
Ben Chengba4fc8b2009-06-01 13:00:29 -07003021 genProcessArgsNoRange(cUnit, mir, dInsn, &pcrLabel);
3022 else
3023 genProcessArgsRange(cUnit, mir, dInsn, &pcrLabel);
3024
Ben Cheng38329f52009-07-07 14:19:20 -07003025 genInvokeVirtualCommon(cUnit, mir, methodIndex,
3026 retChainingCell,
3027 predChainingCell,
3028 pcrLabel);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003029 break;
3030 }
3031 /*
3032 * calleeMethod = method->clazz->super->vtable[method->clazz->pDvmDex
3033 * ->pResMethods[BBBB]->methodIndex]
3034 */
Ben Chengba4fc8b2009-06-01 13:00:29 -07003035 case OP_INVOKE_SUPER:
jeffhao71eee1f2011-01-04 14:18:54 -08003036 case OP_INVOKE_SUPER_RANGE:
3037 case OP_INVOKE_SUPER_JUMBO: {
Ben Cheng7a2697d2010-06-07 13:44:23 -07003038 /* Grab the method ptr directly from what the interpreter sees */
3039 const Method *calleeMethod = mir->meta.callsiteInfo->method;
3040 assert(calleeMethod == cUnit->method->clazz->super->vtable[
3041 cUnit->method->clazz->pDvmDex->
3042 pResMethods[dInsn->vB]->methodIndex]);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003043
Dan Bornstein9a1f8162010-12-01 17:02:26 -08003044 if (mir->dalvikInsn.opcode == OP_INVOKE_SUPER)
Ben Chengba4fc8b2009-06-01 13:00:29 -07003045 genProcessArgsNoRange(cUnit, mir, dInsn, &pcrLabel);
3046 else
3047 genProcessArgsRange(cUnit, mir, dInsn, &pcrLabel);
3048
Ben Cheng20d7e6c2011-02-18 17:12:42 -08003049 if (mir->OptimizationFlags & MIR_INVOKE_METHOD_JIT) {
3050 const Method *calleeMethod = mir->meta.callsiteInfo->method;
3051 void *calleeAddr = dvmJitGetMethodAddr(calleeMethod->insns);
3052 assert(calleeAddr);
3053 genInvokeSingletonWholeMethod(cUnit, mir, calleeAddr,
3054 retChainingCell);
3055 } else {
3056 /* r0 = calleeMethod */
3057 loadConstant(cUnit, r0, (int) calleeMethod);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003058
Ben Cheng20d7e6c2011-02-18 17:12:42 -08003059 genInvokeSingletonCommon(cUnit, mir, bb, labelList, pcrLabel,
3060 calleeMethod);
3061 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07003062 break;
3063 }
3064 /* calleeMethod = method->clazz->pDvmDex->pResMethods[BBBB] */
3065 case OP_INVOKE_DIRECT:
jeffhao71eee1f2011-01-04 14:18:54 -08003066 case OP_INVOKE_DIRECT_RANGE:
3067 case OP_INVOKE_DIRECT_JUMBO: {
Ben Cheng7a2697d2010-06-07 13:44:23 -07003068 /* Grab the method ptr directly from what the interpreter sees */
3069 const Method *calleeMethod = mir->meta.callsiteInfo->method;
3070 assert(calleeMethod ==
3071 cUnit->method->clazz->pDvmDex->pResMethods[dInsn->vB]);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003072
Dan Bornstein9a1f8162010-12-01 17:02:26 -08003073 if (mir->dalvikInsn.opcode == OP_INVOKE_DIRECT)
Ben Chengba4fc8b2009-06-01 13:00:29 -07003074 genProcessArgsNoRange(cUnit, mir, dInsn, &pcrLabel);
3075 else
3076 genProcessArgsRange(cUnit, mir, dInsn, &pcrLabel);
3077
3078 /* r0 = calleeMethod */
3079 loadConstant(cUnit, r0, (int) calleeMethod);
3080
Ben Cheng38329f52009-07-07 14:19:20 -07003081 genInvokeSingletonCommon(cUnit, mir, bb, labelList, pcrLabel,
3082 calleeMethod);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003083 break;
3084 }
3085 /* calleeMethod = method->clazz->pDvmDex->pResMethods[BBBB] */
3086 case OP_INVOKE_STATIC:
jeffhao71eee1f2011-01-04 14:18:54 -08003087 case OP_INVOKE_STATIC_RANGE:
3088 case OP_INVOKE_STATIC_JUMBO: {
Ben Cheng7a2697d2010-06-07 13:44:23 -07003089 /* Grab the method ptr directly from what the interpreter sees */
3090 const Method *calleeMethod = mir->meta.callsiteInfo->method;
3091 assert(calleeMethod ==
3092 cUnit->method->clazz->pDvmDex->pResMethods[dInsn->vB]);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003093
Dan Bornstein9a1f8162010-12-01 17:02:26 -08003094 if (mir->dalvikInsn.opcode == OP_INVOKE_STATIC)
Ben Chengba4fc8b2009-06-01 13:00:29 -07003095 genProcessArgsNoRange(cUnit, mir, dInsn,
3096 NULL /* no null check */);
3097 else
3098 genProcessArgsRange(cUnit, mir, dInsn,
3099 NULL /* no null check */);
3100
Ben Cheng20d7e6c2011-02-18 17:12:42 -08003101 if (mir->OptimizationFlags & MIR_INVOKE_METHOD_JIT) {
3102 const Method *calleeMethod = mir->meta.callsiteInfo->method;
3103 void *calleeAddr = dvmJitGetMethodAddr(calleeMethod->insns);
3104 assert(calleeAddr);
3105 genInvokeSingletonWholeMethod(cUnit, mir, calleeAddr,
3106 retChainingCell);
3107 } else {
3108 /* r0 = calleeMethod */
3109 loadConstant(cUnit, r0, (int) calleeMethod);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003110
Ben Cheng20d7e6c2011-02-18 17:12:42 -08003111 genInvokeSingletonCommon(cUnit, mir, bb, labelList, pcrLabel,
3112 calleeMethod);
3113 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07003114 break;
3115 }
Ben Cheng09e50c92010-05-02 10:45:32 -07003116 /*
Ben Chengba4fc8b2009-06-01 13:00:29 -07003117 * calleeMethod = dvmFindInterfaceMethodInCache(this->clazz,
3118 * BBBB, method, method->clazz->pDvmDex)
Ben Cheng38329f52009-07-07 14:19:20 -07003119 *
Ben Cheng09e50c92010-05-02 10:45:32 -07003120 * The following is an example of generated code for
3121 * "invoke-interface v0"
Ben Cheng38329f52009-07-07 14:19:20 -07003122 *
Ben Cheng09e50c92010-05-02 10:45:32 -07003123 * -------- dalvik offset: 0x0008 @ invoke-interface v0
3124 * 0x47357e36 : ldr r0, [r5, #0] --+
3125 * 0x47357e38 : sub r7,r5,#24 |
3126 * 0x47357e3c : cmp r0, #0 | genProcessArgsNoRange
3127 * 0x47357e3e : beq 0x47357e82 |
3128 * 0x47357e40 : stmia r7, <r0> --+
3129 * 0x47357e42 : ldr r4, [pc, #120] --> r4 <- dalvikPC of this invoke
3130 * 0x47357e44 : add r1, pc, #64 --> r1 <- &retChainingCell
3131 * 0x47357e46 : add r2, pc, #72 --> r2 <- &predictedChainingCell
3132 * 0x47357e48 : blx_1 0x47348190 --+ TEMPLATE_INVOKE_METHOD_
3133 * 0x47357e4a : blx_2 see above --+ PREDICTED_CHAIN
3134 * 0x47357e4c : b 0x47357e90 --> off to the predicted chain
3135 * 0x47357e4e : b 0x47357e82 --> punt to the interpreter
3136 * 0x47357e50 : mov r8, r1 --+
3137 * 0x47357e52 : mov r9, r2 |
3138 * 0x47357e54 : ldr r2, [pc, #96] |
3139 * 0x47357e56 : mov r10, r3 |
3140 * 0x47357e58 : movs r0, r3 | dvmFindInterfaceMethodInCache
3141 * 0x47357e5a : ldr r3, [pc, #88] |
3142 * 0x47357e5c : ldr r7, [pc, #80] |
3143 * 0x47357e5e : mov r1, #1452 |
3144 * 0x47357e62 : blx r7 --+
3145 * 0x47357e64 : cmp r0, #0 --> calleeMethod == NULL?
3146 * 0x47357e66 : bne 0x47357e6e --> branch over the throw if !r0
3147 * 0x47357e68 : ldr r0, [pc, #80] --> load Dalvik PC of the invoke
3148 * 0x47357e6a : blx_1 0x47348494 --+ TEMPLATE_THROW_EXCEPTION_
3149 * 0x47357e6c : blx_2 see above --+ COMMON
3150 * 0x47357e6e : mov r1, r8 --> r1 <- &retChainingCell
3151 * 0x47357e70 : cmp r1, #0 --> compare against 0
3152 * 0x47357e72 : bgt 0x47357e7c --> >=0? don't rechain
Ben Chengaf5aa1f2011-01-04 15:37:04 -08003153 * 0x47357e74 : ldr r7, [pc, #off] --+
Ben Cheng09e50c92010-05-02 10:45:32 -07003154 * 0x47357e76 : mov r2, r9 | dvmJitToPatchPredictedChain
3155 * 0x47357e78 : mov r3, r10 |
3156 * 0x47357e7a : blx r7 --+
3157 * 0x47357e7c : add r1, pc, #8 --> r1 <- &retChainingCell
3158 * 0x47357e7e : blx_1 0x4734809c --+ TEMPLATE_INVOKE_METHOD_NO_OPT
3159 * 0x47357e80 : blx_2 see above --+
3160 * -------- reconstruct dalvik PC : 0x425719dc @ +0x0008
3161 * 0x47357e82 : ldr r0, [pc, #56]
Ben Cheng38329f52009-07-07 14:19:20 -07003162 * Exception_Handling:
Ben Cheng09e50c92010-05-02 10:45:32 -07003163 * 0x47357e84 : ldr r1, [r6, #92]
3164 * 0x47357e86 : blx r1
3165 * 0x47357e88 : .align4
3166 * -------- chaining cell (hot): 0x000b
3167 * 0x47357e88 : ldr r0, [r6, #104]
3168 * 0x47357e8a : blx r0
3169 * 0x47357e8c : data 0x19e2(6626)
3170 * 0x47357e8e : data 0x4257(16983)
3171 * 0x47357e90 : .align4
Ben Cheng38329f52009-07-07 14:19:20 -07003172 * -------- chaining cell (predicted)
Ben Cheng09e50c92010-05-02 10:45:32 -07003173 * 0x47357e90 : data 0xe7fe(59390) --> will be patched into bx
3174 * 0x47357e92 : data 0x0000(0)
3175 * 0x47357e94 : data 0x0000(0) --> class
3176 * 0x47357e96 : data 0x0000(0)
3177 * 0x47357e98 : data 0x0000(0) --> method
3178 * 0x47357e9a : data 0x0000(0)
3179 * 0x47357e9c : data 0x0000(0) --> rechain count
3180 * 0x47357e9e : data 0x0000(0)
3181 * -------- end of chaining cells (0x006c)
3182 * 0x47357eb0 : .word (0xad03e369)
3183 * 0x47357eb4 : .word (0x28a90)
3184 * 0x47357eb8 : .word (0x41a63394)
3185 * 0x47357ebc : .word (0x425719dc)
Ben Chengba4fc8b2009-06-01 13:00:29 -07003186 */
3187 case OP_INVOKE_INTERFACE:
jeffhao71eee1f2011-01-04 14:18:54 -08003188 case OP_INVOKE_INTERFACE_RANGE:
3189 case OP_INVOKE_INTERFACE_JUMBO: {
Bill Buzbee89efc3d2009-07-28 11:22:22 -07003190 ArmLIR *predChainingCell = &labelList[bb->taken->id];
Ben Chengba4fc8b2009-06-01 13:00:29 -07003191
Ben Cheng7a2697d2010-06-07 13:44:23 -07003192 /*
3193 * If the invoke has non-null misPredBranchOver, we need to generate
3194 * the non-inlined version of the invoke here to handle the
3195 * mispredicted case.
3196 */
3197 if (mir->meta.callsiteInfo->misPredBranchOver) {
3198 genLandingPadForMispredictedCallee(cUnit, mir, bb, labelList);
3199 }
Bill Buzbee1465db52009-09-23 17:17:35 -07003200
Dan Bornstein9a1f8162010-12-01 17:02:26 -08003201 if (mir->dalvikInsn.opcode == OP_INVOKE_INTERFACE)
Ben Chengba4fc8b2009-06-01 13:00:29 -07003202 genProcessArgsNoRange(cUnit, mir, dInsn, &pcrLabel);
3203 else
3204 genProcessArgsRange(cUnit, mir, dInsn, &pcrLabel);
3205
Ben Cheng38329f52009-07-07 14:19:20 -07003206 /* "this" is already left in r0 by genProcessArgs* */
3207
3208 /* r4PC = dalvikCallsite */
3209 loadConstant(cUnit, r4PC,
3210 (int) (cUnit->method->insns + mir->offset));
3211
3212 /* r1 = &retChainingCell */
Bill Buzbee270c1d62009-08-13 16:58:07 -07003213 ArmLIR *addrRetChain =
Ben Cheng20d7e6c2011-02-18 17:12:42 -08003214 opRegRegImm(cUnit, kOpAdd, r1, r15pc, 0);
Ben Cheng38329f52009-07-07 14:19:20 -07003215 addrRetChain->generic.target = (LIR *) retChainingCell;
3216
3217 /* r2 = &predictedChainingCell */
Bill Buzbee89efc3d2009-07-28 11:22:22 -07003218 ArmLIR *predictedChainingCell =
Ben Cheng20d7e6c2011-02-18 17:12:42 -08003219 opRegRegImm(cUnit, kOpAdd, r2, r15pc, 0);
Ben Cheng38329f52009-07-07 14:19:20 -07003220 predictedChainingCell->generic.target = (LIR *) predChainingCell;
3221
buzbee18fba342011-01-19 15:31:15 -08003222 genDispatchToHandler(cUnit, gDvmJit.methodTraceSupport ?
3223 TEMPLATE_INVOKE_METHOD_PREDICTED_CHAIN_PROF :
3224 TEMPLATE_INVOKE_METHOD_PREDICTED_CHAIN);
Ben Cheng38329f52009-07-07 14:19:20 -07003225
3226 /* return through lr - jump to the chaining cell */
3227 genUnconditionalBranch(cUnit, predChainingCell);
3228
3229 /*
3230 * null-check on "this" may have been eliminated, but we still need
3231 * a PC-reconstruction label for stack overflow bailout.
3232 */
3233 if (pcrLabel == NULL) {
3234 int dPC = (int) (cUnit->method->insns + mir->offset);
Carl Shapirofc75f3e2010-12-07 11:43:38 -08003235 pcrLabel = (ArmLIR *) dvmCompilerNew(sizeof(ArmLIR), true);
Dan Bornstein9a1f8162010-12-01 17:02:26 -08003236 pcrLabel->opcode = kArmPseudoPCReconstructionCell;
Ben Cheng38329f52009-07-07 14:19:20 -07003237 pcrLabel->operands[0] = dPC;
3238 pcrLabel->operands[1] = mir->offset;
3239 /* Insert the place holder to the growable list */
Ben Cheng00603072010-10-28 11:13:58 -07003240 dvmInsertGrowableList(&cUnit->pcReconstructionList,
3241 (intptr_t) pcrLabel);
Ben Cheng38329f52009-07-07 14:19:20 -07003242 }
3243
3244 /* return through lr+2 - punt to the interpreter */
3245 genUnconditionalBranch(cUnit, pcrLabel);
3246
3247 /*
3248 * return through lr+4 - fully resolve the callee method.
3249 * r1 <- count
3250 * r2 <- &predictedChainCell
3251 * r3 <- this->class
3252 * r4 <- dPC
3253 * r7 <- this->class->vtable
3254 */
3255
3256 /* Save count, &predictedChainCell, and class to high regs first */
Bill Buzbee1465db52009-09-23 17:17:35 -07003257 genRegCopy(cUnit, r8, r1);
3258 genRegCopy(cUnit, r9, r2);
3259 genRegCopy(cUnit, r10, r3);
Ben Cheng38329f52009-07-07 14:19:20 -07003260
Ben Chengba4fc8b2009-06-01 13:00:29 -07003261 /* r0 now contains this->clazz */
Bill Buzbee1465db52009-09-23 17:17:35 -07003262 genRegCopy(cUnit, r0, r3);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003263
3264 /* r1 = BBBB */
3265 loadConstant(cUnit, r1, dInsn->vB);
3266
3267 /* r2 = method (caller) */
3268 loadConstant(cUnit, r2, (int) cUnit->method);
3269
3270 /* r3 = pDvmDex */
3271 loadConstant(cUnit, r3, (int) cUnit->method->clazz->pDvmDex);
3272
Ben Chengbd1326d2010-04-02 15:04:53 -07003273 LOAD_FUNC_ADDR(cUnit, r7,
3274 (intptr_t) dvmFindInterfaceMethodInCache);
Bill Buzbee1465db52009-09-23 17:17:35 -07003275 opReg(cUnit, kOpBlx, r7);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003276 /* r0 = calleeMethod (returned from dvmFindInterfaceMethodInCache */
3277
Ben Cheng09e50c92010-05-02 10:45:32 -07003278 dvmCompilerClobberCallRegs(cUnit);
3279 /* generate a branch over if the interface method is resolved */
buzbee8f8109a2010-08-31 10:16:35 -07003280 ArmLIR *branchOver = genCmpImmBranch(cUnit, kArmCondNe, r0, 0);
Ben Cheng09e50c92010-05-02 10:45:32 -07003281 /*
3282 * calleeMethod == NULL -> throw
3283 */
3284 loadConstant(cUnit, r0,
3285 (int) (cUnit->method->insns + mir->offset));
3286 genDispatchToHandler(cUnit, TEMPLATE_THROW_EXCEPTION_COMMON);
3287 /* noreturn */
3288
3289 ArmLIR *target = newLIR0(cUnit, kArmPseudoTargetLabel);
3290 target->defMask = ENCODE_ALL;
3291 branchOver->generic.target = (LIR *) target;
3292
Bill Buzbee1465db52009-09-23 17:17:35 -07003293 genRegCopy(cUnit, r1, r8);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003294
Ben Cheng38329f52009-07-07 14:19:20 -07003295 /* Check if rechain limit is reached */
buzbee8f8109a2010-08-31 10:16:35 -07003296 ArmLIR *bypassRechaining = genCmpImmBranch(cUnit, kArmCondGt,
3297 r1, 0);
Ben Cheng38329f52009-07-07 14:19:20 -07003298
Ben Chengaf5aa1f2011-01-04 15:37:04 -08003299 LOAD_FUNC_ADDR(cUnit, r7, (int) dvmJitToPatchPredictedChain);
Ben Cheng38329f52009-07-07 14:19:20 -07003300
Ben Cheng20d7e6c2011-02-18 17:12:42 -08003301 genRegCopy(cUnit, r1, r6SELF);
Bill Buzbee1465db52009-09-23 17:17:35 -07003302 genRegCopy(cUnit, r2, r9);
3303 genRegCopy(cUnit, r3, r10);
Ben Cheng38329f52009-07-07 14:19:20 -07003304
3305 /*
3306 * r0 = calleeMethod
3307 * r2 = &predictedChainingCell
3308 * r3 = class
3309 *
3310 * &returnChainingCell has been loaded into r1 but is not needed
3311 * when patching the chaining cell and will be clobbered upon
3312 * returning so it will be reconstructed again.
3313 */
Bill Buzbee1465db52009-09-23 17:17:35 -07003314 opReg(cUnit, kOpBlx, r7);
Ben Cheng38329f52009-07-07 14:19:20 -07003315
3316 /* r1 = &retChainingCell */
Ben Cheng20d7e6c2011-02-18 17:12:42 -08003317 addrRetChain = opRegRegImm(cUnit, kOpAdd, r1, r15pc, 0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003318 addrRetChain->generic.target = (LIR *) retChainingCell;
Ben Cheng38329f52009-07-07 14:19:20 -07003319
3320 bypassRechaining->generic.target = (LIR *) addrRetChain;
3321
Ben Chengba4fc8b2009-06-01 13:00:29 -07003322 /*
3323 * r0 = this, r1 = calleeMethod,
3324 * r1 = &ChainingCell,
3325 * r4PC = callsiteDPC,
3326 */
buzbee18fba342011-01-19 15:31:15 -08003327 genDispatchToHandler(cUnit, gDvmJit.methodTraceSupport ?
3328 TEMPLATE_INVOKE_METHOD_NO_OPT_PROF :
3329 TEMPLATE_INVOKE_METHOD_NO_OPT);
Ben Cheng978738d2010-05-13 13:45:57 -07003330#if defined(WITH_JIT_TUNING)
Ben Cheng86717f72010-03-05 15:27:21 -08003331 gDvmJit.invokePolymorphic++;
Ben Chengba4fc8b2009-06-01 13:00:29 -07003332#endif
3333 /* Handle exceptions using the interpreter */
3334 genTrap(cUnit, mir->offset, pcrLabel);
3335 break;
3336 }
Andy McFadden0346e9d2011-03-01 15:47:46 -08003337 case OP_INVOKE_OBJECT_INIT_RANGE: {
Andy McFadden6af2ddd2011-02-16 16:50:40 -08003338 genInterpSingleStep(cUnit, mir);
buzbee18fba342011-01-19 15:31:15 -08003339 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07003340 }
3341 case OP_FILLED_NEW_ARRAY:
jeffhao71eee1f2011-01-04 14:18:54 -08003342 case OP_FILLED_NEW_ARRAY_RANGE:
3343 case OP_FILLED_NEW_ARRAY_JUMBO: {
Ben Chengba4fc8b2009-06-01 13:00:29 -07003344 /* Just let the interpreter deal with these */
3345 genInterpSingleStep(cUnit, mir);
3346 break;
3347 }
3348 default:
3349 return true;
3350 }
3351 return false;
3352}
3353
3354static bool handleFmt35ms_3rms(CompilationUnit *cUnit, MIR *mir,
Bill Buzbee89efc3d2009-07-28 11:22:22 -07003355 BasicBlock *bb, ArmLIR *labelList)
Ben Chengba4fc8b2009-06-01 13:00:29 -07003356{
Bill Buzbee89efc3d2009-07-28 11:22:22 -07003357 ArmLIR *pcrLabel = NULL;
Ben Chengba4fc8b2009-06-01 13:00:29 -07003358
Ben Cheng7a2697d2010-06-07 13:44:23 -07003359 /* An invoke with the MIR_INLINED is effectively a no-op */
3360 if (mir->OptimizationFlags & MIR_INLINED)
3361 return false;
3362
Ben Chengba4fc8b2009-06-01 13:00:29 -07003363 DecodedInstruction *dInsn = &mir->dalvikInsn;
Dan Bornstein9a1f8162010-12-01 17:02:26 -08003364 switch (mir->dalvikInsn.opcode) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07003365 /* calleeMethod = this->clazz->vtable[BBBB] */
3366 case OP_INVOKE_VIRTUAL_QUICK_RANGE:
3367 case OP_INVOKE_VIRTUAL_QUICK: {
3368 int methodIndex = dInsn->vB;
Bill Buzbeea8589332010-12-27 09:31:21 -08003369 ArmLIR *retChainingCell = &labelList[bb->fallThrough->id];
3370 ArmLIR *predChainingCell = &labelList[bb->taken->id];
Ben Cheng7a2697d2010-06-07 13:44:23 -07003371
3372 /*
3373 * If the invoke has non-null misPredBranchOver, we need to generate
3374 * the non-inlined version of the invoke here to handle the
3375 * mispredicted case.
3376 */
3377 if (mir->meta.callsiteInfo->misPredBranchOver) {
3378 genLandingPadForMispredictedCallee(cUnit, mir, bb, labelList);
3379 }
3380
Dan Bornstein9a1f8162010-12-01 17:02:26 -08003381 if (mir->dalvikInsn.opcode == OP_INVOKE_VIRTUAL_QUICK)
Ben Chengba4fc8b2009-06-01 13:00:29 -07003382 genProcessArgsNoRange(cUnit, mir, dInsn, &pcrLabel);
3383 else
3384 genProcessArgsRange(cUnit, mir, dInsn, &pcrLabel);
3385
Ben Chengcfdeca32011-01-14 11:36:46 -08003386
3387 if (mir->OptimizationFlags & MIR_INVOKE_METHOD_JIT) {
3388 const Method *calleeMethod = mir->meta.callsiteInfo->method;
3389 void *calleeAddr = dvmJitGetMethodAddr(calleeMethod->insns);
Ben Cheng20d7e6c2011-02-18 17:12:42 -08003390 assert(calleeAddr);
3391 genInvokeVirtualWholeMethod(cUnit, mir, calleeAddr,
3392 retChainingCell);
Ben Chengcfdeca32011-01-14 11:36:46 -08003393 }
3394
Ben Cheng38329f52009-07-07 14:19:20 -07003395 genInvokeVirtualCommon(cUnit, mir, methodIndex,
3396 retChainingCell,
3397 predChainingCell,
3398 pcrLabel);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003399 break;
3400 }
3401 /* calleeMethod = method->clazz->super->vtable[BBBB] */
3402 case OP_INVOKE_SUPER_QUICK:
3403 case OP_INVOKE_SUPER_QUICK_RANGE: {
Ben Cheng7a2697d2010-06-07 13:44:23 -07003404 /* Grab the method ptr directly from what the interpreter sees */
3405 const Method *calleeMethod = mir->meta.callsiteInfo->method;
3406 assert(calleeMethod ==
3407 cUnit->method->clazz->super->vtable[dInsn->vB]);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003408
Dan Bornstein9a1f8162010-12-01 17:02:26 -08003409 if (mir->dalvikInsn.opcode == OP_INVOKE_SUPER_QUICK)
Ben Chengba4fc8b2009-06-01 13:00:29 -07003410 genProcessArgsNoRange(cUnit, mir, dInsn, &pcrLabel);
3411 else
3412 genProcessArgsRange(cUnit, mir, dInsn, &pcrLabel);
3413
3414 /* r0 = calleeMethod */
3415 loadConstant(cUnit, r0, (int) calleeMethod);
3416
Ben Cheng38329f52009-07-07 14:19:20 -07003417 genInvokeSingletonCommon(cUnit, mir, bb, labelList, pcrLabel,
3418 calleeMethod);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003419 break;
3420 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07003421 default:
3422 return true;
3423 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07003424 return false;
3425}
3426
3427/*
Bill Buzbeefd023aa2009-11-02 09:23:49 -08003428 * This operation is complex enough that we'll do it partly inline
3429 * and partly with a handler. NOTE: the handler uses hardcoded
3430 * values for string object offsets and must be revisitied if the
3431 * layout changes.
3432 */
3433static bool genInlinedCompareTo(CompilationUnit *cUnit, MIR *mir)
3434{
3435#if defined(USE_GLOBAL_STRING_DEFS)
Elliott Hughes7e914f12011-01-19 18:18:42 -08003436 return handleExecuteInlineC(cUnit, mir);
Bill Buzbeefd023aa2009-11-02 09:23:49 -08003437#else
3438 ArmLIR *rollback;
Bill Buzbeec6f10662010-02-09 11:16:15 -08003439 RegLocation rlThis = dvmCompilerGetSrc(cUnit, mir, 0);
3440 RegLocation rlComp = dvmCompilerGetSrc(cUnit, mir, 1);
Bill Buzbeefd023aa2009-11-02 09:23:49 -08003441
3442 loadValueDirectFixed(cUnit, rlThis, r0);
3443 loadValueDirectFixed(cUnit, rlComp, r1);
3444 /* Test objects for NULL */
3445 rollback = genNullCheck(cUnit, rlThis.sRegLow, r0, mir->offset, NULL);
3446 genNullCheck(cUnit, rlComp.sRegLow, r1, mir->offset, rollback);
3447 /*
3448 * TUNING: we could check for object pointer equality before invoking
3449 * handler. Unclear whether the gain would be worth the added code size
3450 * expansion.
3451 */
3452 genDispatchToHandler(cUnit, TEMPLATE_STRING_COMPARETO);
Bill Buzbeec6f10662010-02-09 11:16:15 -08003453 storeValue(cUnit, inlinedTarget(cUnit, mir, false),
3454 dvmCompilerGetReturn(cUnit));
Elliott Hughes7e914f12011-01-19 18:18:42 -08003455 return false;
Bill Buzbeefd023aa2009-11-02 09:23:49 -08003456#endif
3457}
3458
Elliott Hughes2bdbcb62010-04-12 14:29:37 -07003459static bool genInlinedFastIndexOf(CompilationUnit *cUnit, MIR *mir)
Bill Buzbeefd023aa2009-11-02 09:23:49 -08003460{
3461#if defined(USE_GLOBAL_STRING_DEFS)
Elliott Hughes7e914f12011-01-19 18:18:42 -08003462 return handleExecuteInlineC(cUnit, mir);
Bill Buzbeefd023aa2009-11-02 09:23:49 -08003463#else
Bill Buzbeec6f10662010-02-09 11:16:15 -08003464 RegLocation rlThis = dvmCompilerGetSrc(cUnit, mir, 0);
3465 RegLocation rlChar = dvmCompilerGetSrc(cUnit, mir, 1);
Bill Buzbeefd023aa2009-11-02 09:23:49 -08003466
3467 loadValueDirectFixed(cUnit, rlThis, r0);
3468 loadValueDirectFixed(cUnit, rlChar, r1);
Elliott Hughes2bdbcb62010-04-12 14:29:37 -07003469 RegLocation rlStart = dvmCompilerGetSrc(cUnit, mir, 2);
3470 loadValueDirectFixed(cUnit, rlStart, r2);
Bill Buzbeefd023aa2009-11-02 09:23:49 -08003471 /* Test objects for NULL */
3472 genNullCheck(cUnit, rlThis.sRegLow, r0, mir->offset, NULL);
3473 genDispatchToHandler(cUnit, TEMPLATE_STRING_INDEXOF);
Bill Buzbeec6f10662010-02-09 11:16:15 -08003474 storeValue(cUnit, inlinedTarget(cUnit, mir, false),
3475 dvmCompilerGetReturn(cUnit));
Elliott Hughes7e914f12011-01-19 18:18:42 -08003476 return false;
Bill Buzbeefd023aa2009-11-02 09:23:49 -08003477#endif
3478}
3479
Elliott Hughesee34f592010-04-05 18:13:52 -07003480// Generates an inlined String.isEmpty or String.length.
3481static bool genInlinedStringIsEmptyOrLength(CompilationUnit *cUnit, MIR *mir,
3482 bool isEmpty)
Bill Buzbee1f748632010-03-02 16:14:41 -08003483{
Elliott Hughesee34f592010-04-05 18:13:52 -07003484 // dst = src.length();
Bill Buzbee1f748632010-03-02 16:14:41 -08003485 RegLocation rlObj = dvmCompilerGetSrc(cUnit, mir, 0);
3486 RegLocation rlDest = inlinedTarget(cUnit, mir, false);
3487 rlObj = loadValue(cUnit, rlObj, kCoreReg);
3488 RegLocation rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
3489 genNullCheck(cUnit, rlObj.sRegLow, rlObj.lowReg, mir->offset, NULL);
3490 loadWordDisp(cUnit, rlObj.lowReg, gDvm.offJavaLangString_count,
3491 rlResult.lowReg);
Elliott Hughesee34f592010-04-05 18:13:52 -07003492 if (isEmpty) {
3493 // dst = (dst == 0);
3494 int tReg = dvmCompilerAllocTemp(cUnit);
3495 opRegReg(cUnit, kOpNeg, tReg, rlResult.lowReg);
3496 opRegRegReg(cUnit, kOpAdc, rlResult.lowReg, rlResult.lowReg, tReg);
3497 }
Bill Buzbee1f748632010-03-02 16:14:41 -08003498 storeValue(cUnit, rlDest, rlResult);
3499 return false;
3500}
3501
Elliott Hughesee34f592010-04-05 18:13:52 -07003502static bool genInlinedStringLength(CompilationUnit *cUnit, MIR *mir)
3503{
3504 return genInlinedStringIsEmptyOrLength(cUnit, mir, false);
3505}
3506
3507static bool genInlinedStringIsEmpty(CompilationUnit *cUnit, MIR *mir)
3508{
3509 return genInlinedStringIsEmptyOrLength(cUnit, mir, true);
3510}
3511
Bill Buzbee1f748632010-03-02 16:14:41 -08003512static bool genInlinedStringCharAt(CompilationUnit *cUnit, MIR *mir)
3513{
3514 int contents = offsetof(ArrayObject, contents);
3515 RegLocation rlObj = dvmCompilerGetSrc(cUnit, mir, 0);
3516 RegLocation rlIdx = dvmCompilerGetSrc(cUnit, mir, 1);
3517 RegLocation rlDest = inlinedTarget(cUnit, mir, false);
3518 RegLocation rlResult;
3519 rlObj = loadValue(cUnit, rlObj, kCoreReg);
3520 rlIdx = loadValue(cUnit, rlIdx, kCoreReg);
3521 int regMax = dvmCompilerAllocTemp(cUnit);
3522 int regOff = dvmCompilerAllocTemp(cUnit);
3523 int regPtr = dvmCompilerAllocTemp(cUnit);
3524 ArmLIR *pcrLabel = genNullCheck(cUnit, rlObj.sRegLow, rlObj.lowReg,
3525 mir->offset, NULL);
3526 loadWordDisp(cUnit, rlObj.lowReg, gDvm.offJavaLangString_count, regMax);
3527 loadWordDisp(cUnit, rlObj.lowReg, gDvm.offJavaLangString_offset, regOff);
3528 loadWordDisp(cUnit, rlObj.lowReg, gDvm.offJavaLangString_value, regPtr);
3529 genBoundsCheck(cUnit, rlIdx.lowReg, regMax, mir->offset, pcrLabel);
3530 dvmCompilerFreeTemp(cUnit, regMax);
3531 opRegImm(cUnit, kOpAdd, regPtr, contents);
3532 opRegReg(cUnit, kOpAdd, regOff, rlIdx.lowReg);
3533 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
3534 loadBaseIndexed(cUnit, regPtr, regOff, rlResult.lowReg, 1, kUnsignedHalf);
3535 storeValue(cUnit, rlDest, rlResult);
3536 return false;
3537}
3538
3539static bool genInlinedAbsInt(CompilationUnit *cUnit, MIR *mir)
3540{
3541 RegLocation rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
3542 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
Elliott Hughese22bd842010-08-20 18:47:36 -07003543 RegLocation rlDest = inlinedTarget(cUnit, mir, false);
Bill Buzbee1f748632010-03-02 16:14:41 -08003544 RegLocation rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
3545 int signReg = dvmCompilerAllocTemp(cUnit);
3546 /*
3547 * abs(x) = y<=x>>31, (x+y)^y.
3548 * Thumb2's IT block also yields 3 instructions, but imposes
3549 * scheduling constraints.
3550 */
3551 opRegRegImm(cUnit, kOpAsr, signReg, rlSrc.lowReg, 31);
3552 opRegRegReg(cUnit, kOpAdd, rlResult.lowReg, rlSrc.lowReg, signReg);
3553 opRegReg(cUnit, kOpXor, rlResult.lowReg, signReg);
3554 storeValue(cUnit, rlDest, rlResult);
3555 return false;
3556}
3557
3558static bool genInlinedAbsLong(CompilationUnit *cUnit, MIR *mir)
3559{
3560 RegLocation rlSrc = dvmCompilerGetSrcWide(cUnit, mir, 0, 1);
3561 RegLocation rlDest = inlinedTargetWide(cUnit, mir, false);
3562 rlSrc = loadValueWide(cUnit, rlSrc, kCoreReg);
3563 RegLocation rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
3564 int signReg = dvmCompilerAllocTemp(cUnit);
3565 /*
3566 * abs(x) = y<=x>>31, (x+y)^y.
3567 * Thumb2 IT block allows slightly shorter sequence,
3568 * but introduces a scheduling barrier. Stick with this
3569 * mechanism for now.
3570 */
3571 opRegRegImm(cUnit, kOpAsr, signReg, rlSrc.highReg, 31);
3572 opRegRegReg(cUnit, kOpAdd, rlResult.lowReg, rlSrc.lowReg, signReg);
3573 opRegRegReg(cUnit, kOpAdc, rlResult.highReg, rlSrc.highReg, signReg);
3574 opRegReg(cUnit, kOpXor, rlResult.lowReg, signReg);
3575 opRegReg(cUnit, kOpXor, rlResult.highReg, signReg);
3576 storeValueWide(cUnit, rlDest, rlResult);
3577 return false;
3578}
Bill Buzbeefd023aa2009-11-02 09:23:49 -08003579
Elliott Hughese22bd842010-08-20 18:47:36 -07003580static bool genInlinedIntFloatConversion(CompilationUnit *cUnit, MIR *mir)
3581{
3582 // Just move from source to destination...
3583 RegLocation rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
3584 RegLocation rlDest = inlinedTarget(cUnit, mir, false);
3585 storeValue(cUnit, rlDest, rlSrc);
3586 return false;
3587}
3588
3589static bool genInlinedLongDoubleConversion(CompilationUnit *cUnit, MIR *mir)
3590{
3591 // Just move from source to destination...
3592 RegLocation rlSrc = dvmCompilerGetSrcWide(cUnit, mir, 0, 1);
3593 RegLocation rlDest = inlinedTargetWide(cUnit, mir, false);
3594 storeValueWide(cUnit, rlDest, rlSrc);
3595 return false;
3596}
3597
Bill Buzbeefd023aa2009-11-02 09:23:49 -08003598/*
Elliott Hughes7e914f12011-01-19 18:18:42 -08003599 * JITs a call to a C function.
3600 * TODO: use this for faster native method invocation for simple native
3601 * methods (http://b/3069458).
3602 */
3603static bool handleExecuteInlineC(CompilationUnit *cUnit, MIR *mir)
3604{
3605 DecodedInstruction *dInsn = &mir->dalvikInsn;
3606 int operation = dInsn->vB;
3607 unsigned int i;
3608 const InlineOperation* inLineTable = dvmGetInlineOpsTable();
3609 uintptr_t fn = (int) inLineTable[operation].func;
3610 if (fn == 0) {
3611 dvmCompilerAbort(cUnit);
3612 }
3613 dvmCompilerFlushAllRegs(cUnit); /* Everything to home location */
3614 dvmCompilerClobberCallRegs(cUnit);
3615 dvmCompilerClobber(cUnit, r4PC);
3616 dvmCompilerClobber(cUnit, r7);
buzbee9f601a92011-02-11 17:48:20 -08003617 int offset = offsetof(Thread, retval);
Ben Cheng20d7e6c2011-02-18 17:12:42 -08003618 opRegRegImm(cUnit, kOpAdd, r4PC, r6SELF, offset);
Elliott Hughes7e914f12011-01-19 18:18:42 -08003619 opImm(cUnit, kOpPush, (1<<r4PC) | (1<<r7));
3620 LOAD_FUNC_ADDR(cUnit, r4PC, fn);
3621 genExportPC(cUnit, mir);
3622 for (i=0; i < dInsn->vA; i++) {
3623 loadValueDirect(cUnit, dvmCompilerGetSrc(cUnit, mir, i), i);
3624 }
3625 opReg(cUnit, kOpBlx, r4PC);
Ben Cheng20d7e6c2011-02-18 17:12:42 -08003626 opRegImm(cUnit, kOpAdd, r13sp, 8);
Elliott Hughes7e914f12011-01-19 18:18:42 -08003627 /* NULL? */
3628 ArmLIR *branchOver = genCmpImmBranch(cUnit, kArmCondNe, r0, 0);
3629 loadConstant(cUnit, r0, (int) (cUnit->method->insns + mir->offset));
3630 genDispatchToHandler(cUnit, TEMPLATE_THROW_EXCEPTION_COMMON);
3631 ArmLIR *target = newLIR0(cUnit, kArmPseudoTargetLabel);
3632 target->defMask = ENCODE_ALL;
3633 branchOver->generic.target = (LIR *) target;
3634 return false;
3635}
3636
3637/*
Bill Buzbeece46c942009-11-20 15:41:34 -08003638 * NOTE: Handles both range and non-range versions (arguments
3639 * have already been normalized by this point).
Ben Chengba4fc8b2009-06-01 13:00:29 -07003640 */
Bill Buzbeece46c942009-11-20 15:41:34 -08003641static bool handleExecuteInline(CompilationUnit *cUnit, MIR *mir)
Ben Chengba4fc8b2009-06-01 13:00:29 -07003642{
3643 DecodedInstruction *dInsn = &mir->dalvikInsn;
Elliott Hughes7e914f12011-01-19 18:18:42 -08003644 assert(dInsn->opcode == OP_EXECUTE_INLINE_RANGE ||
3645 dInsn->opcode == OP_EXECUTE_INLINE);
3646 switch (dInsn->vB) {
3647 case INLINE_EMPTYINLINEMETHOD:
3648 return false; /* Nop */
3649
3650 /* These ones we potentially JIT inline. */
3651 case INLINE_STRING_LENGTH:
3652 return genInlinedStringLength(cUnit, mir);
3653 case INLINE_STRING_IS_EMPTY:
3654 return genInlinedStringIsEmpty(cUnit, mir);
3655 case INLINE_MATH_ABS_INT:
3656 return genInlinedAbsInt(cUnit, mir);
3657 case INLINE_MATH_ABS_LONG:
3658 return genInlinedAbsLong(cUnit, mir);
3659 case INLINE_MATH_MIN_INT:
3660 return genInlinedMinMaxInt(cUnit, mir, true);
3661 case INLINE_MATH_MAX_INT:
3662 return genInlinedMinMaxInt(cUnit, mir, false);
3663 case INLINE_STRING_CHARAT:
3664 return genInlinedStringCharAt(cUnit, mir);
3665 case INLINE_MATH_SQRT:
3666 return genInlineSqrt(cUnit, mir);
3667 case INLINE_MATH_ABS_FLOAT:
3668 return genInlinedAbsFloat(cUnit, mir);
3669 case INLINE_MATH_ABS_DOUBLE:
3670 return genInlinedAbsDouble(cUnit, mir);
3671 case INLINE_STRING_COMPARETO:
3672 return genInlinedCompareTo(cUnit, mir);
3673 case INLINE_STRING_FASTINDEXOF_II:
3674 return genInlinedFastIndexOf(cUnit, mir);
3675 case INLINE_FLOAT_TO_RAW_INT_BITS:
3676 case INLINE_INT_BITS_TO_FLOAT:
3677 return genInlinedIntFloatConversion(cUnit, mir);
3678 case INLINE_DOUBLE_TO_RAW_LONG_BITS:
3679 case INLINE_LONG_BITS_TO_DOUBLE:
3680 return genInlinedLongDoubleConversion(cUnit, mir);
3681
3682 /*
3683 * These ones we just JIT a call to a C function for.
3684 * TODO: special-case these in the other "invoke" call paths.
3685 */
3686 case INLINE_STRING_EQUALS:
3687 case INLINE_MATH_COS:
3688 case INLINE_MATH_SIN:
3689 case INLINE_FLOAT_TO_INT_BITS:
3690 case INLINE_DOUBLE_TO_LONG_BITS:
3691 return handleExecuteInlineC(cUnit, mir);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003692 }
Elliott Hughes7e914f12011-01-19 18:18:42 -08003693 dvmCompilerAbort(cUnit);
3694 return false; // Not reachable; keeps compiler happy.
Ben Chengba4fc8b2009-06-01 13:00:29 -07003695}
3696
3697static bool handleFmt51l(CompilationUnit *cUnit, MIR *mir)
3698{
Bill Buzbee1465db52009-09-23 17:17:35 -07003699 //TUNING: We're using core regs here - not optimal when target is a double
Bill Buzbeec6f10662010-02-09 11:16:15 -08003700 RegLocation rlDest = dvmCompilerGetDestWide(cUnit, mir, 0, 1);
3701 RegLocation rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Ben Chengbd1326d2010-04-02 15:04:53 -07003702 loadConstantNoClobber(cUnit, rlResult.lowReg,
3703 mir->dalvikInsn.vB_wide & 0xFFFFFFFFUL);
3704 loadConstantNoClobber(cUnit, rlResult.highReg,
3705 (mir->dalvikInsn.vB_wide>>32) & 0xFFFFFFFFUL);
Bill Buzbee1465db52009-09-23 17:17:35 -07003706 storeValueWide(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003707 return false;
3708}
3709
Ben Chengba4fc8b2009-06-01 13:00:29 -07003710/*
3711 * The following are special processing routines that handle transfer of
3712 * controls between compiled code and the interpreter. Certain VM states like
3713 * Dalvik PC and special-purpose registers are reconstructed here.
3714 */
3715
Bill Buzbeebd047242010-05-13 13:02:53 -07003716/*
3717 * Insert a
3718 * b .+4
3719 * nop
3720 * pair at the beginning of a chaining cell. This serves as the
3721 * switch branch that selects between reverting to the interpreter or
3722 * not. Once the cell is chained to a translation, the cell will
3723 * contain a 32-bit branch. Subsequent chain/unchain operations will
3724 * then only alter that first 16-bits - the "b .+4" for unchaining,
3725 * and the restoration of the first half of the 32-bit branch for
3726 * rechaining.
3727 */
3728static void insertChainingSwitch(CompilationUnit *cUnit)
3729{
3730 ArmLIR *branch = newLIR0(cUnit, kThumbBUncond);
3731 newLIR2(cUnit, kThumbOrr, r0, r0);
3732 ArmLIR *target = newLIR0(cUnit, kArmPseudoTargetLabel);
3733 target->defMask = ENCODE_ALL;
3734 branch->generic.target = (LIR *) target;
3735}
3736
Ben Cheng1efc9c52009-06-08 18:25:27 -07003737/* Chaining cell for code that may need warmup. */
3738static void handleNormalChainingCell(CompilationUnit *cUnit,
3739 unsigned int offset)
Ben Chengba4fc8b2009-06-01 13:00:29 -07003740{
Ben Cheng11d8f142010-03-24 15:24:19 -07003741 /*
3742 * Use raw instruction constructors to guarantee that the generated
3743 * instructions fit the predefined cell size.
3744 */
Bill Buzbeebd047242010-05-13 13:02:53 -07003745 insertChainingSwitch(cUnit);
Ben Cheng20d7e6c2011-02-18 17:12:42 -08003746 newLIR3(cUnit, kThumbLdrRRI5, r0, r6SELF,
buzbee9f601a92011-02-11 17:48:20 -08003747 offsetof(Thread,
Ben Cheng11d8f142010-03-24 15:24:19 -07003748 jitToInterpEntries.dvmJitToInterpNormal) >> 2);
3749 newLIR1(cUnit, kThumbBlxR, r0);
Ben Cheng385828e2011-03-04 16:48:33 -08003750 addWordData(cUnit, NULL, (int) (cUnit->method->insns + offset));
Ben Chengba4fc8b2009-06-01 13:00:29 -07003751}
3752
3753/*
Ben Cheng1efc9c52009-06-08 18:25:27 -07003754 * Chaining cell for instructions that immediately following already translated
3755 * code.
Ben Chengba4fc8b2009-06-01 13:00:29 -07003756 */
Ben Cheng1efc9c52009-06-08 18:25:27 -07003757static void handleHotChainingCell(CompilationUnit *cUnit,
3758 unsigned int offset)
Ben Chengba4fc8b2009-06-01 13:00:29 -07003759{
Ben Cheng11d8f142010-03-24 15:24:19 -07003760 /*
3761 * Use raw instruction constructors to guarantee that the generated
3762 * instructions fit the predefined cell size.
3763 */
Bill Buzbeebd047242010-05-13 13:02:53 -07003764 insertChainingSwitch(cUnit);
Ben Cheng20d7e6c2011-02-18 17:12:42 -08003765 newLIR3(cUnit, kThumbLdrRRI5, r0, r6SELF,
buzbee9f601a92011-02-11 17:48:20 -08003766 offsetof(Thread,
Ben Cheng11d8f142010-03-24 15:24:19 -07003767 jitToInterpEntries.dvmJitToInterpTraceSelect) >> 2);
3768 newLIR1(cUnit, kThumbBlxR, r0);
Ben Cheng385828e2011-03-04 16:48:33 -08003769 addWordData(cUnit, NULL, (int) (cUnit->method->insns + offset));
Ben Chengba4fc8b2009-06-01 13:00:29 -07003770}
3771
Jeff Hao97319a82009-08-12 16:57:15 -07003772/* Chaining cell for branches that branch back into the same basic block */
3773static void handleBackwardBranchChainingCell(CompilationUnit *cUnit,
3774 unsigned int offset)
3775{
Ben Cheng11d8f142010-03-24 15:24:19 -07003776 /*
3777 * Use raw instruction constructors to guarantee that the generated
3778 * instructions fit the predefined cell size.
3779 */
Bill Buzbeebd047242010-05-13 13:02:53 -07003780 insertChainingSwitch(cUnit);
Bill Buzbee9c4b7c82009-09-10 10:10:38 -07003781#if defined(WITH_SELF_VERIFICATION)
Ben Cheng20d7e6c2011-02-18 17:12:42 -08003782 newLIR3(cUnit, kThumbLdrRRI5, r0, r6SELF,
buzbee9f601a92011-02-11 17:48:20 -08003783 offsetof(Thread,
Ben Cheng40094c12010-02-24 20:58:44 -08003784 jitToInterpEntries.dvmJitToInterpBackwardBranch) >> 2);
Bill Buzbee9c4b7c82009-09-10 10:10:38 -07003785#else
Ben Cheng20d7e6c2011-02-18 17:12:42 -08003786 newLIR3(cUnit, kThumbLdrRRI5, r0, r6SELF,
buzbee9f601a92011-02-11 17:48:20 -08003787 offsetof(Thread, jitToInterpEntries.dvmJitToInterpNormal) >> 2);
Bill Buzbee9c4b7c82009-09-10 10:10:38 -07003788#endif
Bill Buzbee1465db52009-09-23 17:17:35 -07003789 newLIR1(cUnit, kThumbBlxR, r0);
Ben Cheng385828e2011-03-04 16:48:33 -08003790 addWordData(cUnit, NULL, (int) (cUnit->method->insns + offset));
Jeff Hao97319a82009-08-12 16:57:15 -07003791}
3792
Ben Chengba4fc8b2009-06-01 13:00:29 -07003793/* Chaining cell for monomorphic method invocations. */
Ben Cheng38329f52009-07-07 14:19:20 -07003794static void handleInvokeSingletonChainingCell(CompilationUnit *cUnit,
3795 const Method *callee)
Ben Chengba4fc8b2009-06-01 13:00:29 -07003796{
Ben Cheng11d8f142010-03-24 15:24:19 -07003797 /*
3798 * Use raw instruction constructors to guarantee that the generated
3799 * instructions fit the predefined cell size.
3800 */
Bill Buzbeebd047242010-05-13 13:02:53 -07003801 insertChainingSwitch(cUnit);
Ben Cheng20d7e6c2011-02-18 17:12:42 -08003802 newLIR3(cUnit, kThumbLdrRRI5, r0, r6SELF,
buzbee9f601a92011-02-11 17:48:20 -08003803 offsetof(Thread,
Ben Cheng11d8f142010-03-24 15:24:19 -07003804 jitToInterpEntries.dvmJitToInterpTraceSelect) >> 2);
3805 newLIR1(cUnit, kThumbBlxR, r0);
Ben Cheng385828e2011-03-04 16:48:33 -08003806 addWordData(cUnit, NULL, (int) (callee->insns));
Ben Chengba4fc8b2009-06-01 13:00:29 -07003807}
3808
Ben Cheng38329f52009-07-07 14:19:20 -07003809/* Chaining cell for monomorphic method invocations. */
3810static void handleInvokePredictedChainingCell(CompilationUnit *cUnit)
3811{
3812
3813 /* Should not be executed in the initial state */
Ben Cheng385828e2011-03-04 16:48:33 -08003814 addWordData(cUnit, NULL, PREDICTED_CHAIN_BX_PAIR_INIT);
Ben Cheng38329f52009-07-07 14:19:20 -07003815 /* To be filled: class */
Ben Cheng385828e2011-03-04 16:48:33 -08003816 addWordData(cUnit, NULL, PREDICTED_CHAIN_CLAZZ_INIT);
Ben Cheng38329f52009-07-07 14:19:20 -07003817 /* To be filled: method */
Ben Cheng385828e2011-03-04 16:48:33 -08003818 addWordData(cUnit, NULL, PREDICTED_CHAIN_METHOD_INIT);
Ben Cheng38329f52009-07-07 14:19:20 -07003819 /*
3820 * Rechain count. The initial value of 0 here will trigger chaining upon
3821 * the first invocation of this callsite.
3822 */
Ben Cheng385828e2011-03-04 16:48:33 -08003823 addWordData(cUnit, NULL, PREDICTED_CHAIN_COUNTER_INIT);
Ben Cheng38329f52009-07-07 14:19:20 -07003824}
3825
Ben Chengba4fc8b2009-06-01 13:00:29 -07003826/* Load the Dalvik PC into r0 and jump to the specified target */
3827static void handlePCReconstruction(CompilationUnit *cUnit,
Bill Buzbee89efc3d2009-07-28 11:22:22 -07003828 ArmLIR *targetLabel)
Ben Chengba4fc8b2009-06-01 13:00:29 -07003829{
Bill Buzbee89efc3d2009-07-28 11:22:22 -07003830 ArmLIR **pcrLabel =
3831 (ArmLIR **) cUnit->pcReconstructionList.elemList;
Ben Chengba4fc8b2009-06-01 13:00:29 -07003832 int numElems = cUnit->pcReconstructionList.numUsed;
3833 int i;
3834 for (i = 0; i < numElems; i++) {
3835 dvmCompilerAppendLIR(cUnit, (LIR *) pcrLabel[i]);
3836 /* r0 = dalvik PC */
3837 loadConstant(cUnit, r0, pcrLabel[i]->operands[0]);
3838 genUnconditionalBranch(cUnit, targetLabel);
3839 }
3840}
3841
Bill Buzbee1465db52009-09-23 17:17:35 -07003842static char *extendedMIROpNames[kMirOpLast - kMirOpFirst] = {
3843 "kMirOpPhi",
3844 "kMirOpNullNRangeUpCheck",
3845 "kMirOpNullNRangeDownCheck",
3846 "kMirOpLowerBound",
3847 "kMirOpPunt",
Ben Cheng7a2697d2010-06-07 13:44:23 -07003848 "kMirOpCheckInlinePrediction",
Ben Cheng4238ec22009-08-24 16:32:22 -07003849};
3850
3851/*
3852 * vA = arrayReg;
3853 * vB = idxReg;
3854 * vC = endConditionReg;
3855 * arg[0] = maxC
3856 * arg[1] = minC
3857 * arg[2] = loopBranchConditionCode
3858 */
3859static void genHoistedChecksForCountUpLoop(CompilationUnit *cUnit, MIR *mir)
3860{
Bill Buzbee1465db52009-09-23 17:17:35 -07003861 /*
3862 * NOTE: these synthesized blocks don't have ssa names assigned
3863 * for Dalvik registers. However, because they dominate the following
3864 * blocks we can simply use the Dalvik name w/ subscript 0 as the
3865 * ssa name.
3866 */
Ben Cheng4238ec22009-08-24 16:32:22 -07003867 DecodedInstruction *dInsn = &mir->dalvikInsn;
3868 const int lenOffset = offsetof(ArrayObject, length);
Ben Cheng4238ec22009-08-24 16:32:22 -07003869 const int maxC = dInsn->arg[0];
Bill Buzbee1465db52009-09-23 17:17:35 -07003870 int regLength;
3871 RegLocation rlArray = cUnit->regLocation[mir->dalvikInsn.vA];
3872 RegLocation rlIdxEnd = cUnit->regLocation[mir->dalvikInsn.vC];
Ben Cheng4238ec22009-08-24 16:32:22 -07003873
3874 /* regArray <- arrayRef */
Bill Buzbee1465db52009-09-23 17:17:35 -07003875 rlArray = loadValue(cUnit, rlArray, kCoreReg);
3876 rlIdxEnd = loadValue(cUnit, rlIdxEnd, kCoreReg);
3877 genRegImmCheck(cUnit, kArmCondEq, rlArray.lowReg, 0, 0,
Ben Cheng4238ec22009-08-24 16:32:22 -07003878 (ArmLIR *) cUnit->loopAnalysis->branchToPCR);
3879
3880 /* regLength <- len(arrayRef) */
Bill Buzbeec6f10662010-02-09 11:16:15 -08003881 regLength = dvmCompilerAllocTemp(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07003882 loadWordDisp(cUnit, rlArray.lowReg, lenOffset, regLength);
Ben Cheng4238ec22009-08-24 16:32:22 -07003883
3884 int delta = maxC;
3885 /*
3886 * If the loop end condition is ">=" instead of ">", then the largest value
3887 * of the index is "endCondition - 1".
3888 */
3889 if (dInsn->arg[2] == OP_IF_GE) {
3890 delta--;
3891 }
3892
3893 if (delta) {
Bill Buzbeec6f10662010-02-09 11:16:15 -08003894 int tReg = dvmCompilerAllocTemp(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07003895 opRegRegImm(cUnit, kOpAdd, tReg, rlIdxEnd.lowReg, delta);
3896 rlIdxEnd.lowReg = tReg;
Bill Buzbeec6f10662010-02-09 11:16:15 -08003897 dvmCompilerFreeTemp(cUnit, tReg);
Ben Cheng4238ec22009-08-24 16:32:22 -07003898 }
3899 /* Punt if "regIdxEnd < len(Array)" is false */
Bill Buzbee1465db52009-09-23 17:17:35 -07003900 genRegRegCheck(cUnit, kArmCondGe, rlIdxEnd.lowReg, regLength, 0,
Ben Cheng0fd31e42009-09-03 14:40:16 -07003901 (ArmLIR *) cUnit->loopAnalysis->branchToPCR);
Ben Cheng4238ec22009-08-24 16:32:22 -07003902}
3903
3904/*
3905 * vA = arrayReg;
3906 * vB = idxReg;
3907 * vC = endConditionReg;
3908 * arg[0] = maxC
3909 * arg[1] = minC
3910 * arg[2] = loopBranchConditionCode
3911 */
3912static void genHoistedChecksForCountDownLoop(CompilationUnit *cUnit, MIR *mir)
3913{
3914 DecodedInstruction *dInsn = &mir->dalvikInsn;
3915 const int lenOffset = offsetof(ArrayObject, length);
Bill Buzbeec6f10662010-02-09 11:16:15 -08003916 const int regLength = dvmCompilerAllocTemp(cUnit);
Ben Cheng4238ec22009-08-24 16:32:22 -07003917 const int maxC = dInsn->arg[0];
Bill Buzbee1465db52009-09-23 17:17:35 -07003918 RegLocation rlArray = cUnit->regLocation[mir->dalvikInsn.vA];
3919 RegLocation rlIdxInit = cUnit->regLocation[mir->dalvikInsn.vB];
Ben Cheng4238ec22009-08-24 16:32:22 -07003920
3921 /* regArray <- arrayRef */
Bill Buzbee1465db52009-09-23 17:17:35 -07003922 rlArray = loadValue(cUnit, rlArray, kCoreReg);
3923 rlIdxInit = loadValue(cUnit, rlIdxInit, kCoreReg);
3924 genRegImmCheck(cUnit, kArmCondEq, rlArray.lowReg, 0, 0,
Ben Cheng4238ec22009-08-24 16:32:22 -07003925 (ArmLIR *) cUnit->loopAnalysis->branchToPCR);
3926
3927 /* regLength <- len(arrayRef) */
Bill Buzbee1465db52009-09-23 17:17:35 -07003928 loadWordDisp(cUnit, rlArray.lowReg, lenOffset, regLength);
Ben Cheng4238ec22009-08-24 16:32:22 -07003929
3930 if (maxC) {
Bill Buzbeec6f10662010-02-09 11:16:15 -08003931 int tReg = dvmCompilerAllocTemp(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07003932 opRegRegImm(cUnit, kOpAdd, tReg, rlIdxInit.lowReg, maxC);
3933 rlIdxInit.lowReg = tReg;
Bill Buzbeec6f10662010-02-09 11:16:15 -08003934 dvmCompilerFreeTemp(cUnit, tReg);
Ben Cheng4238ec22009-08-24 16:32:22 -07003935 }
3936
3937 /* Punt if "regIdxInit < len(Array)" is false */
Bill Buzbee1465db52009-09-23 17:17:35 -07003938 genRegRegCheck(cUnit, kArmCondGe, rlIdxInit.lowReg, regLength, 0,
Ben Cheng0fd31e42009-09-03 14:40:16 -07003939 (ArmLIR *) cUnit->loopAnalysis->branchToPCR);
Ben Cheng4238ec22009-08-24 16:32:22 -07003940}
3941
3942/*
3943 * vA = idxReg;
3944 * vB = minC;
3945 */
3946static void genHoistedLowerBoundCheck(CompilationUnit *cUnit, MIR *mir)
3947{
3948 DecodedInstruction *dInsn = &mir->dalvikInsn;
Ben Cheng4238ec22009-08-24 16:32:22 -07003949 const int minC = dInsn->vB;
Bill Buzbee1465db52009-09-23 17:17:35 -07003950 RegLocation rlIdx = cUnit->regLocation[mir->dalvikInsn.vA];
Ben Cheng4238ec22009-08-24 16:32:22 -07003951
3952 /* regIdx <- initial index value */
Bill Buzbee1465db52009-09-23 17:17:35 -07003953 rlIdx = loadValue(cUnit, rlIdx, kCoreReg);
Ben Cheng4238ec22009-08-24 16:32:22 -07003954
3955 /* Punt if "regIdxInit + minC >= 0" is false */
Bill Buzbee1465db52009-09-23 17:17:35 -07003956 genRegImmCheck(cUnit, kArmCondLt, rlIdx.lowReg, -minC, 0,
Ben Cheng4238ec22009-08-24 16:32:22 -07003957 (ArmLIR *) cUnit->loopAnalysis->branchToPCR);
3958}
3959
Ben Cheng7a2697d2010-06-07 13:44:23 -07003960/*
3961 * vC = this
3962 *
3963 * A predicted inlining target looks like the following, where instructions
3964 * between 0x4858de66 and 0x4858de72 are checking if the predicted class
3965 * matches "this", and the verificaion code is generated by this routine.
3966 *
3967 * (C) means the instruction is inlined from the callee, and (PI) means the
3968 * instruction is the predicted inlined invoke, whose corresponding
3969 * instructions are still generated to handle the mispredicted case.
3970 *
3971 * D/dalvikvm( 86): -------- kMirOpCheckInlinePrediction
3972 * D/dalvikvm( 86): 0x4858de66 (0002): ldr r0, [r5, #68]
3973 * D/dalvikvm( 86): 0x4858de68 (0004): ldr r1, [pc, #140]
3974 * D/dalvikvm( 86): 0x4858de6a (0006): cmp r0, #0
3975 * D/dalvikvm( 86): 0x4858de6c (0008): beq 0x4858deb2
3976 * D/dalvikvm( 86): 0x4858de6e (000a): ldr r2, [r0, #0]
3977 * D/dalvikvm( 86): 0x4858de70 (000c): cmp r1, r2
3978 * D/dalvikvm( 86): 0x4858de72 (000e): bne 0x4858de7a
3979 * D/dalvikvm( 86): -------- dalvik offset: 0x004c @ +iget-object-quick (C)
3980 * v4, v17, (#8)
3981 * D/dalvikvm( 86): 0x4858de74 (0010): ldr r3, [r0, #8]
3982 * D/dalvikvm( 86): 0x4858de76 (0012): str r3, [r5, #16]
3983 * D/dalvikvm( 86): -------- dalvik offset: 0x004c @
3984 * +invoke-virtual-quick/range (PI) v17..v17
3985 * D/dalvikvm( 86): 0x4858de78 (0014): b 0x4858debc
3986 * D/dalvikvm( 86): 0x4858de7a (0016): add r4,r5,#68
3987 * D/dalvikvm( 86): -------- BARRIER
3988 * D/dalvikvm( 86): 0x4858de7e (001a): ldmia r4, <r0>
3989 * D/dalvikvm( 86): -------- BARRIER
3990 * D/dalvikvm( 86): 0x4858de80 (001c): sub r7,r5,#24
3991 * D/dalvikvm( 86): 0x4858de84 (0020): cmp r0, #0
3992 * D/dalvikvm( 86): 0x4858de86 (0022): beq 0x4858deb6
3993 * D/dalvikvm( 86): -------- BARRIER
3994 * D/dalvikvm( 86): 0x4858de88 (0024): stmia r7, <r0>
3995 * D/dalvikvm( 86): -------- BARRIER
3996 * D/dalvikvm( 86): 0x4858de8a (0026): ldr r4, [pc, #104]
3997 * D/dalvikvm( 86): 0x4858de8c (0028): add r1, pc, #28
3998 * D/dalvikvm( 86): 0x4858de8e (002a): add r2, pc, #56
3999 * D/dalvikvm( 86): 0x4858de90 (002c): blx_1 0x48589198
4000 * D/dalvikvm( 86): 0x4858de92 (002e): blx_2 see above
4001 * D/dalvikvm( 86): 0x4858de94 (0030): b 0x4858dec8
4002 * D/dalvikvm( 86): 0x4858de96 (0032): b 0x4858deb6
4003 * D/dalvikvm( 86): 0x4858de98 (0034): ldr r0, [r7, #72]
4004 * D/dalvikvm( 86): 0x4858de9a (0036): cmp r1, #0
4005 * D/dalvikvm( 86): 0x4858de9c (0038): bgt 0x4858dea4
4006 * D/dalvikvm( 86): 0x4858de9e (003a): ldr r7, [r6, #116]
4007 * D/dalvikvm( 86): 0x4858dea0 (003c): movs r1, r6
4008 * D/dalvikvm( 86): 0x4858dea2 (003e): blx r7
4009 * D/dalvikvm( 86): 0x4858dea4 (0040): add r1, pc, #4
4010 * D/dalvikvm( 86): 0x4858dea6 (0042): blx_1 0x485890a0
4011 * D/dalvikvm( 86): 0x4858dea8 (0044): blx_2 see above
4012 * D/dalvikvm( 86): 0x4858deaa (0046): b 0x4858deb6
4013 * D/dalvikvm( 86): 0x4858deac (0048): .align4
4014 * D/dalvikvm( 86): L0x004f:
4015 * D/dalvikvm( 86): -------- dalvik offset: 0x004f @ move-result-object (PI)
4016 * v4, (#0), (#0)
4017 * D/dalvikvm( 86): 0x4858deac (0048): ldr r4, [r6, #8]
4018 * D/dalvikvm( 86): 0x4858deae (004a): str r4, [r5, #16]
4019 * D/dalvikvm( 86): 0x4858deb0 (004c): b 0x4858debc
4020 * D/dalvikvm( 86): -------- reconstruct dalvik PC : 0x42beefcc @ +0x004c
4021 * D/dalvikvm( 86): 0x4858deb2 (004e): ldr r0, [pc, #64]
4022 * D/dalvikvm( 86): 0x4858deb4 (0050): b 0x4858deb8
4023 * D/dalvikvm( 86): -------- reconstruct dalvik PC : 0x42beefcc @ +0x004c
4024 * D/dalvikvm( 86): 0x4858deb6 (0052): ldr r0, [pc, #60]
4025 * D/dalvikvm( 86): Exception_Handling:
4026 * D/dalvikvm( 86): 0x4858deb8 (0054): ldr r1, [r6, #100]
4027 * D/dalvikvm( 86): 0x4858deba (0056): blx r1
4028 * D/dalvikvm( 86): 0x4858debc (0058): .align4
4029 * D/dalvikvm( 86): -------- chaining cell (hot): 0x0050
4030 * D/dalvikvm( 86): 0x4858debc (0058): b 0x4858dec0
4031 * D/dalvikvm( 86): 0x4858debe (005a): orrs r0, r0
4032 * D/dalvikvm( 86): 0x4858dec0 (005c): ldr r0, [r6, #112]
4033 * D/dalvikvm( 86): 0x4858dec2 (005e): blx r0
4034 * D/dalvikvm( 86): 0x4858dec4 (0060): data 0xefd4(61396)
4035 * D/dalvikvm( 86): 0x4858dec6 (0062): data 0x42be(17086)
4036 * D/dalvikvm( 86): 0x4858dec8 (0064): .align4
4037 * D/dalvikvm( 86): -------- chaining cell (predicted)
4038 * D/dalvikvm( 86): 0x4858dec8 (0064): data 0xe7fe(59390)
4039 * D/dalvikvm( 86): 0x4858deca (0066): data 0x0000(0)
4040 * D/dalvikvm( 86): 0x4858decc (0068): data 0x0000(0)
4041 * D/dalvikvm( 86): 0x4858dece (006a): data 0x0000(0)
4042 * :
4043 */
4044static void genValidationForPredictedInline(CompilationUnit *cUnit, MIR *mir)
4045{
4046 CallsiteInfo *callsiteInfo = mir->meta.callsiteInfo;
4047 RegLocation rlThis = cUnit->regLocation[mir->dalvikInsn.vC];
4048
4049 rlThis = loadValue(cUnit, rlThis, kCoreReg);
4050 int regPredictedClass = dvmCompilerAllocTemp(cUnit);
Ben Cheng385828e2011-03-04 16:48:33 -08004051 loadClassPointer(cUnit, regPredictedClass, (int) callsiteInfo);
Ben Cheng7a2697d2010-06-07 13:44:23 -07004052 genNullCheck(cUnit, rlThis.sRegLow, rlThis.lowReg, mir->offset,
4053 NULL);/* null object? */
4054 int regActualClass = dvmCompilerAllocTemp(cUnit);
4055 loadWordDisp(cUnit, rlThis.lowReg, offsetof(Object, clazz), regActualClass);
4056 opRegReg(cUnit, kOpCmp, regPredictedClass, regActualClass);
4057 /*
4058 * Set the misPredBranchOver target so that it will be generated when the
4059 * code for the non-optimized invoke is generated.
4060 */
4061 callsiteInfo->misPredBranchOver = (LIR *) opCondBranch(cUnit, kArmCondNe);
4062}
4063
Ben Cheng4238ec22009-08-24 16:32:22 -07004064/* Extended MIR instructions like PHI */
4065static void handleExtendedMIR(CompilationUnit *cUnit, MIR *mir)
4066{
Dan Bornstein9a1f8162010-12-01 17:02:26 -08004067 int opOffset = mir->dalvikInsn.opcode - kMirOpFirst;
Carl Shapirofc75f3e2010-12-07 11:43:38 -08004068 char *msg = (char *)dvmCompilerNew(strlen(extendedMIROpNames[opOffset]) + 1,
4069 false);
Ben Cheng4238ec22009-08-24 16:32:22 -07004070 strcpy(msg, extendedMIROpNames[opOffset]);
Bill Buzbee1465db52009-09-23 17:17:35 -07004071 newLIR1(cUnit, kArmPseudoExtended, (int) msg);
Ben Cheng4238ec22009-08-24 16:32:22 -07004072
Dan Bornstein9a1f8162010-12-01 17:02:26 -08004073 switch (mir->dalvikInsn.opcode) {
Bill Buzbee1465db52009-09-23 17:17:35 -07004074 case kMirOpPhi: {
Ben Cheng4238ec22009-08-24 16:32:22 -07004075 char *ssaString = dvmCompilerGetSSAString(cUnit, mir->ssaRep);
Bill Buzbee1465db52009-09-23 17:17:35 -07004076 newLIR1(cUnit, kArmPseudoSSARep, (int) ssaString);
Ben Cheng4238ec22009-08-24 16:32:22 -07004077 break;
4078 }
Bill Buzbee1465db52009-09-23 17:17:35 -07004079 case kMirOpNullNRangeUpCheck: {
Ben Cheng4238ec22009-08-24 16:32:22 -07004080 genHoistedChecksForCountUpLoop(cUnit, mir);
4081 break;
4082 }
Bill Buzbee1465db52009-09-23 17:17:35 -07004083 case kMirOpNullNRangeDownCheck: {
Ben Cheng4238ec22009-08-24 16:32:22 -07004084 genHoistedChecksForCountDownLoop(cUnit, mir);
4085 break;
4086 }
Bill Buzbee1465db52009-09-23 17:17:35 -07004087 case kMirOpLowerBound: {
Ben Cheng4238ec22009-08-24 16:32:22 -07004088 genHoistedLowerBoundCheck(cUnit, mir);
4089 break;
4090 }
Bill Buzbee1465db52009-09-23 17:17:35 -07004091 case kMirOpPunt: {
Ben Cheng4238ec22009-08-24 16:32:22 -07004092 genUnconditionalBranch(cUnit,
4093 (ArmLIR *) cUnit->loopAnalysis->branchToPCR);
4094 break;
4095 }
Ben Cheng7a2697d2010-06-07 13:44:23 -07004096 case kMirOpCheckInlinePrediction: {
4097 genValidationForPredictedInline(cUnit, mir);
4098 break;
4099 }
Ben Cheng4238ec22009-08-24 16:32:22 -07004100 default:
4101 break;
4102 }
4103}
4104
4105/*
4106 * Create a PC-reconstruction cell for the starting offset of this trace.
4107 * Since the PCR cell is placed near the end of the compiled code which is
4108 * usually out of range for a conditional branch, we put two branches (one
4109 * branch over to the loop body and one layover branch to the actual PCR) at the
4110 * end of the entry block.
4111 */
4112static void setupLoopEntryBlock(CompilationUnit *cUnit, BasicBlock *entry,
4113 ArmLIR *bodyLabel)
4114{
4115 /* Set up the place holder to reconstruct this Dalvik PC */
Carl Shapirofc75f3e2010-12-07 11:43:38 -08004116 ArmLIR *pcrLabel = (ArmLIR *) dvmCompilerNew(sizeof(ArmLIR), true);
Dan Bornstein9a1f8162010-12-01 17:02:26 -08004117 pcrLabel->opcode = kArmPseudoPCReconstructionCell;
Ben Cheng4238ec22009-08-24 16:32:22 -07004118 pcrLabel->operands[0] =
4119 (int) (cUnit->method->insns + entry->startOffset);
4120 pcrLabel->operands[1] = entry->startOffset;
4121 /* Insert the place holder to the growable list */
Ben Cheng00603072010-10-28 11:13:58 -07004122 dvmInsertGrowableList(&cUnit->pcReconstructionList, (intptr_t) pcrLabel);
Ben Cheng4238ec22009-08-24 16:32:22 -07004123
4124 /*
4125 * Next, create two branches - one branch over to the loop body and the
4126 * other branch to the PCR cell to punt.
4127 */
Carl Shapirofc75f3e2010-12-07 11:43:38 -08004128 ArmLIR *branchToBody = (ArmLIR *) dvmCompilerNew(sizeof(ArmLIR), true);
Dan Bornstein9a1f8162010-12-01 17:02:26 -08004129 branchToBody->opcode = kThumbBUncond;
Ben Cheng4238ec22009-08-24 16:32:22 -07004130 branchToBody->generic.target = (LIR *) bodyLabel;
Ben Chengdcf3e5d2009-09-11 13:42:05 -07004131 setupResourceMasks(branchToBody);
Ben Cheng4238ec22009-08-24 16:32:22 -07004132 cUnit->loopAnalysis->branchToBody = (LIR *) branchToBody;
4133
Carl Shapirofc75f3e2010-12-07 11:43:38 -08004134 ArmLIR *branchToPCR = (ArmLIR *) dvmCompilerNew(sizeof(ArmLIR), true);
Dan Bornstein9a1f8162010-12-01 17:02:26 -08004135 branchToPCR->opcode = kThumbBUncond;
Ben Cheng4238ec22009-08-24 16:32:22 -07004136 branchToPCR->generic.target = (LIR *) pcrLabel;
Ben Chengdcf3e5d2009-09-11 13:42:05 -07004137 setupResourceMasks(branchToPCR);
Ben Cheng4238ec22009-08-24 16:32:22 -07004138 cUnit->loopAnalysis->branchToPCR = (LIR *) branchToPCR;
4139}
4140
Ben Chengd5adae12010-03-26 17:45:28 -07004141#if defined(WITH_SELF_VERIFICATION)
4142static bool selfVerificationPuntOps(MIR *mir)
4143{
4144 DecodedInstruction *decInsn = &mir->dalvikInsn;
Dan Bornstein9a1f8162010-12-01 17:02:26 -08004145 Opcode op = decInsn->opcode;
Ben Cheng7a2697d2010-06-07 13:44:23 -07004146
Ben Chengd5adae12010-03-26 17:45:28 -07004147 /*
4148 * All opcodes that can throw exceptions and use the
4149 * TEMPLATE_THROW_EXCEPTION_COMMON template should be excluded in the trace
4150 * under self-verification mode.
4151 */
4152 return (op == OP_MONITOR_ENTER || op == OP_MONITOR_EXIT ||
4153 op == OP_NEW_INSTANCE || op == OP_NEW_ARRAY ||
4154 op == OP_CHECK_CAST || op == OP_MOVE_EXCEPTION ||
4155 op == OP_FILL_ARRAY_DATA || op == OP_EXECUTE_INLINE ||
Ben Cheng7a2697d2010-06-07 13:44:23 -07004156 op == OP_EXECUTE_INLINE_RANGE);
Ben Chengd5adae12010-03-26 17:45:28 -07004157}
4158#endif
4159
Ben Chengba4fc8b2009-06-01 13:00:29 -07004160void dvmCompilerMIR2LIR(CompilationUnit *cUnit)
4161{
4162 /* Used to hold the labels of each block */
Bill Buzbee89efc3d2009-07-28 11:22:22 -07004163 ArmLIR *labelList =
Carl Shapirofc75f3e2010-12-07 11:43:38 -08004164 (ArmLIR *) dvmCompilerNew(sizeof(ArmLIR) * cUnit->numBlocks, true);
Ben Chengcec26f62010-01-15 15:29:33 -08004165 GrowableList chainingListByType[kChainingCellGap];
Ben Chengba4fc8b2009-06-01 13:00:29 -07004166 int i;
4167
4168 /*
Ben Cheng38329f52009-07-07 14:19:20 -07004169 * Initialize various types chaining lists.
Ben Chengba4fc8b2009-06-01 13:00:29 -07004170 */
Ben Chengcec26f62010-01-15 15:29:33 -08004171 for (i = 0; i < kChainingCellGap; i++) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07004172 dvmInitGrowableList(&chainingListByType[i], 2);
4173 }
4174
Ben Cheng7ab74e12011-02-03 14:02:06 -08004175 /* Clear the visited flag for each block */
4176 dvmCompilerDataFlowAnalysisDispatcher(cUnit, dvmCompilerClearVisitedFlag,
4177 kAllNodes, false /* isIterative */);
4178
Ben Cheng00603072010-10-28 11:13:58 -07004179 GrowableListIterator iterator;
4180 dvmGrowableListIteratorInit(&cUnit->blockList, &iterator);
Ben Chengba4fc8b2009-06-01 13:00:29 -07004181
buzbee2e152ba2010-12-15 16:32:35 -08004182 /* Traces start with a profiling entry point. Generate it here */
4183 cUnit->profileCodeSize = genTraceProfileEntry(cUnit);
Ben Cheng1efc9c52009-06-08 18:25:27 -07004184
Ben Chengba4fc8b2009-06-01 13:00:29 -07004185 /* Handle the content in each basic block */
Ben Cheng00603072010-10-28 11:13:58 -07004186 for (i = 0; ; i++) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07004187 MIR *mir;
Ben Cheng00603072010-10-28 11:13:58 -07004188 BasicBlock *bb = (BasicBlock *) dvmGrowableListIteratorNext(&iterator);
4189 if (bb == NULL) break;
Ben Cheng7ab74e12011-02-03 14:02:06 -08004190 if (bb->visited == true) continue;
Ben Chengba4fc8b2009-06-01 13:00:29 -07004191
Ben Cheng00603072010-10-28 11:13:58 -07004192 labelList[i].operands[0] = bb->startOffset;
Ben Chengba4fc8b2009-06-01 13:00:29 -07004193
Ben Cheng00603072010-10-28 11:13:58 -07004194 if (bb->blockType >= kChainingCellGap) {
4195 if (bb->isFallThroughFromInvoke == true) {
Ben Chengd44faf52010-06-02 15:33:51 -07004196 /* Align this block first since it is a return chaining cell */
4197 newLIR0(cUnit, kArmPseudoPseudoAlign4);
4198 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07004199 /*
4200 * Append the label pseudo LIR first. Chaining cells will be handled
4201 * separately afterwards.
4202 */
4203 dvmCompilerAppendLIR(cUnit, (LIR *) &labelList[i]);
4204 }
4205
Ben Cheng00603072010-10-28 11:13:58 -07004206 if (bb->blockType == kTraceEntryBlock) {
Dan Bornstein9a1f8162010-12-01 17:02:26 -08004207 labelList[i].opcode = kArmPseudoEntryBlock;
Ben Cheng00603072010-10-28 11:13:58 -07004208 if (bb->firstMIRInsn == NULL) {
Ben Cheng4238ec22009-08-24 16:32:22 -07004209 continue;
4210 } else {
Ben Cheng00603072010-10-28 11:13:58 -07004211 setupLoopEntryBlock(cUnit, bb,
4212 &labelList[bb->fallThrough->id]);
Ben Cheng4238ec22009-08-24 16:32:22 -07004213 }
Ben Cheng00603072010-10-28 11:13:58 -07004214 } else if (bb->blockType == kTraceExitBlock) {
Dan Bornstein9a1f8162010-12-01 17:02:26 -08004215 labelList[i].opcode = kArmPseudoExitBlock;
Ben Cheng4238ec22009-08-24 16:32:22 -07004216 goto gen_fallthrough;
Ben Cheng00603072010-10-28 11:13:58 -07004217 } else if (bb->blockType == kDalvikByteCode) {
Dan Bornstein9a1f8162010-12-01 17:02:26 -08004218 labelList[i].opcode = kArmPseudoNormalBlockLabel;
Ben Chenge9695e52009-06-16 16:11:47 -07004219 /* Reset the register state */
Bill Buzbeec6f10662010-02-09 11:16:15 -08004220 dvmCompilerResetRegPool(cUnit);
4221 dvmCompilerClobberAllRegs(cUnit);
4222 dvmCompilerResetNullCheck(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -07004223 } else {
Ben Cheng00603072010-10-28 11:13:58 -07004224 switch (bb->blockType) {
Bill Buzbee1465db52009-09-23 17:17:35 -07004225 case kChainingCellNormal:
Dan Bornstein9a1f8162010-12-01 17:02:26 -08004226 labelList[i].opcode = kArmPseudoChainingCellNormal;
Ben Chengba4fc8b2009-06-01 13:00:29 -07004227 /* handle the codegen later */
4228 dvmInsertGrowableList(
Ben Cheng00603072010-10-28 11:13:58 -07004229 &chainingListByType[kChainingCellNormal], i);
Ben Chengba4fc8b2009-06-01 13:00:29 -07004230 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07004231 case kChainingCellInvokeSingleton:
Dan Bornstein9a1f8162010-12-01 17:02:26 -08004232 labelList[i].opcode =
Ben Chenga4973592010-03-31 11:59:18 -07004233 kArmPseudoChainingCellInvokeSingleton;
Ben Chengba4fc8b2009-06-01 13:00:29 -07004234 labelList[i].operands[0] =
Ben Cheng00603072010-10-28 11:13:58 -07004235 (int) bb->containingMethod;
Ben Chengba4fc8b2009-06-01 13:00:29 -07004236 /* handle the codegen later */
4237 dvmInsertGrowableList(
Ben Cheng00603072010-10-28 11:13:58 -07004238 &chainingListByType[kChainingCellInvokeSingleton], i);
Ben Cheng38329f52009-07-07 14:19:20 -07004239 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07004240 case kChainingCellInvokePredicted:
Dan Bornstein9a1f8162010-12-01 17:02:26 -08004241 labelList[i].opcode =
Ben Chenga4973592010-03-31 11:59:18 -07004242 kArmPseudoChainingCellInvokePredicted;
Ben Cheng04517042011-03-14 11:16:21 -07004243 /*
4244 * Move the cached method pointer from operand 1 to 0.
4245 * Operand 0 was clobbered earlier in this routine to store
4246 * the block starting offset, which is not applicable to
4247 * predicted chaining cell.
4248 */
4249 labelList[i].operands[0] = labelList[i].operands[1];
Ben Cheng38329f52009-07-07 14:19:20 -07004250 /* handle the codegen later */
4251 dvmInsertGrowableList(
Ben Cheng00603072010-10-28 11:13:58 -07004252 &chainingListByType[kChainingCellInvokePredicted], i);
Ben Chengba4fc8b2009-06-01 13:00:29 -07004253 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07004254 case kChainingCellHot:
Dan Bornstein9a1f8162010-12-01 17:02:26 -08004255 labelList[i].opcode =
Ben Chenga4973592010-03-31 11:59:18 -07004256 kArmPseudoChainingCellHot;
Ben Chengba4fc8b2009-06-01 13:00:29 -07004257 /* handle the codegen later */
4258 dvmInsertGrowableList(
Ben Cheng00603072010-10-28 11:13:58 -07004259 &chainingListByType[kChainingCellHot], i);
Ben Chengba4fc8b2009-06-01 13:00:29 -07004260 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07004261 case kPCReconstruction:
Ben Chengba4fc8b2009-06-01 13:00:29 -07004262 /* Make sure exception handling block is next */
Dan Bornstein9a1f8162010-12-01 17:02:26 -08004263 labelList[i].opcode =
Ben Chenga4973592010-03-31 11:59:18 -07004264 kArmPseudoPCReconstructionBlockLabel;
Ben Chengba4fc8b2009-06-01 13:00:29 -07004265 assert (i == cUnit->numBlocks - 2);
4266 handlePCReconstruction(cUnit, &labelList[i+1]);
4267 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07004268 case kExceptionHandling:
Dan Bornstein9a1f8162010-12-01 17:02:26 -08004269 labelList[i].opcode = kArmPseudoEHBlockLabel;
Ben Chengba4fc8b2009-06-01 13:00:29 -07004270 if (cUnit->pcReconstructionList.numUsed) {
Ben Cheng20d7e6c2011-02-18 17:12:42 -08004271 loadWordDisp(cUnit, r6SELF, offsetof(Thread,
Bill Buzbee270c1d62009-08-13 16:58:07 -07004272 jitToInterpEntries.dvmJitToInterpPunt),
4273 r1);
Bill Buzbee1465db52009-09-23 17:17:35 -07004274 opReg(cUnit, kOpBlx, r1);
Ben Chengba4fc8b2009-06-01 13:00:29 -07004275 }
4276 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07004277 case kChainingCellBackwardBranch:
Dan Bornstein9a1f8162010-12-01 17:02:26 -08004278 labelList[i].opcode =
Ben Chenga4973592010-03-31 11:59:18 -07004279 kArmPseudoChainingCellBackwardBranch;
Jeff Hao97319a82009-08-12 16:57:15 -07004280 /* handle the codegen later */
4281 dvmInsertGrowableList(
Bill Buzbee1465db52009-09-23 17:17:35 -07004282 &chainingListByType[kChainingCellBackwardBranch],
Ben Cheng00603072010-10-28 11:13:58 -07004283 i);
Jeff Hao97319a82009-08-12 16:57:15 -07004284 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07004285 default:
4286 break;
4287 }
4288 continue;
4289 }
Ben Chenge9695e52009-06-16 16:11:47 -07004290
Bill Buzbee89efc3d2009-07-28 11:22:22 -07004291 ArmLIR *headLIR = NULL;
Ben Cheng7ab74e12011-02-03 14:02:06 -08004292 BasicBlock *nextBB = bb;
Ben Chenge9695e52009-06-16 16:11:47 -07004293
Ben Cheng7ab74e12011-02-03 14:02:06 -08004294 /*
4295 * Try to build a longer optimization unit. Currently if the previous
4296 * block ends with a goto, we continue adding instructions and don't
4297 * reset the register allocation pool.
4298 */
4299 for (; nextBB != NULL; nextBB = cUnit->nextCodegenBlock) {
4300 bb = nextBB;
4301 bb->visited = true;
4302 cUnit->nextCodegenBlock = NULL;
Bill Buzbee1465db52009-09-23 17:17:35 -07004303
Ben Cheng7ab74e12011-02-03 14:02:06 -08004304 for (mir = bb->firstMIRInsn; mir; mir = mir->next) {
Bill Buzbee1465db52009-09-23 17:17:35 -07004305
Ben Cheng7ab74e12011-02-03 14:02:06 -08004306 dvmCompilerResetRegPool(cUnit);
4307 if (gDvmJit.disableOpt & (1 << kTrackLiveTemps)) {
4308 dvmCompilerClobberAllRegs(cUnit);
Ben Cheng80211d22011-01-14 10:23:37 -08004309 }
Ben Cheng4238ec22009-08-24 16:32:22 -07004310
Ben Cheng7ab74e12011-02-03 14:02:06 -08004311 if (gDvmJit.disableOpt & (1 << kSuppressLoads)) {
4312 dvmCompilerResetDefTracking(cUnit);
4313 }
Ben Cheng4238ec22009-08-24 16:32:22 -07004314
Ben Cheng7ab74e12011-02-03 14:02:06 -08004315 if (mir->dalvikInsn.opcode >= kMirOpFirst) {
4316 handleExtendedMIR(cUnit, mir);
4317 continue;
4318 }
4319
4320
4321 Opcode dalvikOpcode = mir->dalvikInsn.opcode;
4322 InstructionFormat dalvikFormat =
4323 dexGetFormatFromOpcode(dalvikOpcode);
4324 char *note;
4325 if (mir->OptimizationFlags & MIR_INLINED) {
4326 note = " (I)";
4327 } else if (mir->OptimizationFlags & MIR_INLINED_PRED) {
4328 note = " (PI)";
4329 } else if (mir->OptimizationFlags & MIR_CALLEE) {
4330 note = " (C)";
4331 } else {
4332 note = NULL;
4333 }
4334
4335 ArmLIR *boundaryLIR;
4336
4337 /*
4338 * Don't generate the boundary LIR unless we are debugging this
4339 * trace or we need a scheduling barrier.
4340 */
4341 if (headLIR == NULL || cUnit->printMe == true) {
4342 boundaryLIR =
4343 newLIR2(cUnit, kArmPseudoDalvikByteCodeBoundary,
4344 mir->offset,
4345 (int) dvmCompilerGetDalvikDisassembly(
4346 &mir->dalvikInsn, note));
4347 /* Remember the first LIR for this block */
4348 if (headLIR == NULL) {
4349 headLIR = boundaryLIR;
4350 /* Set the first boundaryLIR as a scheduling barrier */
4351 headLIR->defMask = ENCODE_ALL;
4352 }
4353 }
4354
4355 /*
4356 * Don't generate the SSA annotation unless verbose mode is on
4357 */
4358 if (cUnit->printMe && mir->ssaRep) {
4359 char *ssaString = dvmCompilerGetSSAString(cUnit,
4360 mir->ssaRep);
4361 newLIR1(cUnit, kArmPseudoSSARep, (int) ssaString);
4362 }
4363
4364 bool notHandled;
4365 /*
4366 * Debugging: screen the opcode first to see if it is in the
4367 * do[-not]-compile list
4368 */
4369 bool singleStepMe = SINGLE_STEP_OP(dalvikOpcode);
Ben Chengd5adae12010-03-26 17:45:28 -07004370#if defined(WITH_SELF_VERIFICATION)
Ben Cheng7ab74e12011-02-03 14:02:06 -08004371 if (singleStepMe == false) {
4372 singleStepMe = selfVerificationPuntOps(mir);
4373 }
Ben Chengd5adae12010-03-26 17:45:28 -07004374#endif
Ben Cheng7ab74e12011-02-03 14:02:06 -08004375 if (singleStepMe || cUnit->allSingleStep) {
4376 notHandled = false;
4377 genInterpSingleStep(cUnit, mir);
4378 } else {
4379 opcodeCoverage[dalvikOpcode]++;
4380 switch (dalvikFormat) {
4381 case kFmt10t:
4382 case kFmt20t:
4383 case kFmt30t:
4384 notHandled = handleFmt10t_Fmt20t_Fmt30t(cUnit,
4385 mir, bb, labelList);
4386 break;
4387 case kFmt10x:
4388 notHandled = handleFmt10x(cUnit, mir);
4389 break;
4390 case kFmt11n:
4391 case kFmt31i:
4392 notHandled = handleFmt11n_Fmt31i(cUnit, mir);
4393 break;
4394 case kFmt11x:
4395 notHandled = handleFmt11x(cUnit, mir);
4396 break;
4397 case kFmt12x:
4398 notHandled = handleFmt12x(cUnit, mir);
4399 break;
4400 case kFmt20bc:
4401 case kFmt40sc:
4402 notHandled = handleFmt20bc_Fmt40sc(cUnit, mir);
4403 break;
4404 case kFmt21c:
4405 case kFmt31c:
4406 case kFmt41c:
4407 notHandled = handleFmt21c_Fmt31c_Fmt41c(cUnit, mir);
4408 break;
4409 case kFmt21h:
4410 notHandled = handleFmt21h(cUnit, mir);
4411 break;
4412 case kFmt21s:
4413 notHandled = handleFmt21s(cUnit, mir);
4414 break;
4415 case kFmt21t:
4416 notHandled = handleFmt21t(cUnit, mir, bb,
Ben Chengba4fc8b2009-06-01 13:00:29 -07004417 labelList);
Ben Cheng7ab74e12011-02-03 14:02:06 -08004418 break;
4419 case kFmt22b:
4420 case kFmt22s:
4421 notHandled = handleFmt22b_Fmt22s(cUnit, mir);
4422 break;
4423 case kFmt22c:
4424 case kFmt52c:
4425 notHandled = handleFmt22c_Fmt52c(cUnit, mir);
4426 break;
4427 case kFmt22cs:
4428 notHandled = handleFmt22cs(cUnit, mir);
4429 break;
4430 case kFmt22t:
4431 notHandled = handleFmt22t(cUnit, mir, bb,
4432 labelList);
4433 break;
4434 case kFmt22x:
4435 case kFmt32x:
4436 notHandled = handleFmt22x_Fmt32x(cUnit, mir);
4437 break;
4438 case kFmt23x:
4439 notHandled = handleFmt23x(cUnit, mir);
4440 break;
4441 case kFmt31t:
4442 notHandled = handleFmt31t(cUnit, mir);
4443 break;
4444 case kFmt3rc:
4445 case kFmt35c:
4446 case kFmt5rc:
4447 notHandled = handleFmt35c_3rc_5rc(cUnit, mir, bb,
4448 labelList);
4449 break;
4450 case kFmt3rms:
4451 case kFmt35ms:
4452 notHandled = handleFmt35ms_3rms(cUnit, mir, bb,
4453 labelList);
4454 break;
4455 case kFmt35mi:
4456 case kFmt3rmi:
4457 notHandled = handleExecuteInline(cUnit, mir);
4458 break;
4459 case kFmt51l:
4460 notHandled = handleFmt51l(cUnit, mir);
4461 break;
4462 default:
4463 notHandled = true;
4464 break;
4465 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07004466 }
Ben Cheng7ab74e12011-02-03 14:02:06 -08004467 if (notHandled) {
4468 LOGE("%#06x: Opcode 0x%x (%s) / Fmt %d not handled\n",
4469 mir->offset,
4470 dalvikOpcode, dexGetOpcodeName(dalvikOpcode),
4471 dalvikFormat);
4472 dvmCompilerAbort(cUnit);
4473 break;
4474 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07004475 }
4476 }
Ben Cheng4238ec22009-08-24 16:32:22 -07004477
Ben Cheng00603072010-10-28 11:13:58 -07004478 if (bb->blockType == kTraceEntryBlock) {
Ben Cheng4238ec22009-08-24 16:32:22 -07004479 dvmCompilerAppendLIR(cUnit,
4480 (LIR *) cUnit->loopAnalysis->branchToBody);
4481 dvmCompilerAppendLIR(cUnit,
4482 (LIR *) cUnit->loopAnalysis->branchToPCR);
4483 }
4484
4485 if (headLIR) {
4486 /*
4487 * Eliminate redundant loads/stores and delay stores into later
4488 * slots
4489 */
4490 dvmCompilerApplyLocalOptimizations(cUnit, (LIR *) headLIR,
4491 cUnit->lastLIRInsn);
4492 }
4493
4494gen_fallthrough:
Ben Cheng1efc9c52009-06-08 18:25:27 -07004495 /*
4496 * Check if the block is terminated due to trace length constraint -
4497 * insert an unconditional branch to the chaining cell.
4498 */
Ben Cheng00603072010-10-28 11:13:58 -07004499 if (bb->needFallThroughBranch) {
Ben Cheng7ab74e12011-02-03 14:02:06 -08004500 genUnconditionalBranch(cUnit, &labelList[bb->fallThrough->id]);
Ben Cheng1efc9c52009-06-08 18:25:27 -07004501 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07004502 }
4503
Ben Chenge9695e52009-06-16 16:11:47 -07004504 /* Handle the chaining cells in predefined order */
Ben Chengcec26f62010-01-15 15:29:33 -08004505 for (i = 0; i < kChainingCellGap; i++) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07004506 size_t j;
4507 int *blockIdList = (int *) chainingListByType[i].elemList;
4508
4509 cUnit->numChainingCells[i] = chainingListByType[i].numUsed;
4510
4511 /* No chaining cells of this type */
4512 if (cUnit->numChainingCells[i] == 0)
4513 continue;
4514
4515 /* Record the first LIR for a new type of chaining cell */
4516 cUnit->firstChainingLIR[i] = (LIR *) &labelList[blockIdList[0]];
4517
4518 for (j = 0; j < chainingListByType[i].numUsed; j++) {
4519 int blockId = blockIdList[j];
Ben Cheng00603072010-10-28 11:13:58 -07004520 BasicBlock *chainingBlock =
4521 (BasicBlock *) dvmGrowableListGetElement(&cUnit->blockList,
4522 blockId);
Ben Chengba4fc8b2009-06-01 13:00:29 -07004523
4524 /* Align this chaining cell first */
Bill Buzbee1465db52009-09-23 17:17:35 -07004525 newLIR0(cUnit, kArmPseudoPseudoAlign4);
Ben Chengba4fc8b2009-06-01 13:00:29 -07004526
4527 /* Insert the pseudo chaining instruction */
4528 dvmCompilerAppendLIR(cUnit, (LIR *) &labelList[blockId]);
4529
4530
Ben Cheng00603072010-10-28 11:13:58 -07004531 switch (chainingBlock->blockType) {
Bill Buzbee1465db52009-09-23 17:17:35 -07004532 case kChainingCellNormal:
Ben Cheng00603072010-10-28 11:13:58 -07004533 handleNormalChainingCell(cUnit, chainingBlock->startOffset);
Ben Chengba4fc8b2009-06-01 13:00:29 -07004534 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07004535 case kChainingCellInvokeSingleton:
Ben Cheng38329f52009-07-07 14:19:20 -07004536 handleInvokeSingletonChainingCell(cUnit,
Ben Cheng00603072010-10-28 11:13:58 -07004537 chainingBlock->containingMethod);
Ben Chengba4fc8b2009-06-01 13:00:29 -07004538 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07004539 case kChainingCellInvokePredicted:
Ben Cheng38329f52009-07-07 14:19:20 -07004540 handleInvokePredictedChainingCell(cUnit);
4541 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07004542 case kChainingCellHot:
Ben Cheng00603072010-10-28 11:13:58 -07004543 handleHotChainingCell(cUnit, chainingBlock->startOffset);
Ben Chengba4fc8b2009-06-01 13:00:29 -07004544 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07004545 case kChainingCellBackwardBranch:
Jeff Hao97319a82009-08-12 16:57:15 -07004546 handleBackwardBranchChainingCell(cUnit,
Ben Cheng00603072010-10-28 11:13:58 -07004547 chainingBlock->startOffset);
Jeff Hao97319a82009-08-12 16:57:15 -07004548 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07004549 default:
Ben Cheng00603072010-10-28 11:13:58 -07004550 LOGE("Bad blocktype %d", chainingBlock->blockType);
Bill Buzbeefc519dc2010-03-06 23:30:57 -08004551 dvmCompilerAbort(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -07004552 }
4553 }
4554 }
Ben Chenge9695e52009-06-16 16:11:47 -07004555
Ben Chengcec26f62010-01-15 15:29:33 -08004556 /* Mark the bottom of chaining cells */
4557 cUnit->chainingCellBottom = (LIR *) newLIR0(cUnit, kArmChainingCellBottom);
4558
Ben Cheng6c10a972009-10-29 14:39:18 -07004559 /*
4560 * Generate the branch to the dvmJitToInterpNoChain entry point at the end
4561 * of all chaining cells for the overflow cases.
4562 */
4563 if (cUnit->switchOverflowPad) {
4564 loadConstant(cUnit, r0, (int) cUnit->switchOverflowPad);
Ben Cheng20d7e6c2011-02-18 17:12:42 -08004565 loadWordDisp(cUnit, r6SELF, offsetof(Thread,
Ben Cheng6c10a972009-10-29 14:39:18 -07004566 jitToInterpEntries.dvmJitToInterpNoChain), r2);
4567 opRegReg(cUnit, kOpAdd, r1, r1);
4568 opRegRegReg(cUnit, kOpAdd, r4PC, r0, r1);
Ben Cheng978738d2010-05-13 13:45:57 -07004569#if defined(WITH_JIT_TUNING)
Ben Cheng6c10a972009-10-29 14:39:18 -07004570 loadConstant(cUnit, r0, kSwitchOverflow);
4571#endif
4572 opReg(cUnit, kOpBlx, r2);
4573 }
4574
Ben Chenge9695e52009-06-16 16:11:47 -07004575 dvmCompilerApplyGlobalOptimizations(cUnit);
jeffhao9e45c0b2010-02-03 10:24:05 -08004576
4577#if defined(WITH_SELF_VERIFICATION)
4578 selfVerificationBranchInsertPass(cUnit);
4579#endif
Ben Chengba4fc8b2009-06-01 13:00:29 -07004580}
4581
buzbee2e152ba2010-12-15 16:32:35 -08004582/*
4583 * Accept the work and start compiling. Returns true if compilation
4584 * is attempted.
4585 */
Bill Buzbee716f1202009-07-23 13:22:09 -07004586bool dvmCompilerDoWork(CompilerWorkOrder *work)
Ben Chengba4fc8b2009-06-01 13:00:29 -07004587{
Carl Shapirofc75f3e2010-12-07 11:43:38 -08004588 JitTraceDescription *desc;
buzbee2e152ba2010-12-15 16:32:35 -08004589 bool isCompile;
4590 bool success = true;
Ben Chengba4fc8b2009-06-01 13:00:29 -07004591
Ben Cheng6999d842010-01-26 16:46:15 -08004592 if (gDvmJit.codeCacheFull) {
Ben Chengccd6c012009-10-15 14:52:45 -07004593 return false;
4594 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07004595
Ben Chengccd6c012009-10-15 14:52:45 -07004596 switch (work->kind) {
Ben Chengccd6c012009-10-15 14:52:45 -07004597 case kWorkOrderTrace:
buzbee2e152ba2010-12-15 16:32:35 -08004598 isCompile = true;
Ben Chengccd6c012009-10-15 14:52:45 -07004599 /* Start compilation with maximally allowed trace length */
Carl Shapirofc75f3e2010-12-07 11:43:38 -08004600 desc = (JitTraceDescription *)work->info;
buzbee2e152ba2010-12-15 16:32:35 -08004601 success = dvmCompileTrace(desc, JIT_MAX_TRACE_LEN, &work->result,
4602 work->bailPtr, 0 /* no hints */);
Ben Chengccd6c012009-10-15 14:52:45 -07004603 break;
4604 case kWorkOrderTraceDebug: {
4605 bool oldPrintMe = gDvmJit.printMe;
4606 gDvmJit.printMe = true;
buzbee2e152ba2010-12-15 16:32:35 -08004607 isCompile = true;
Ben Chengccd6c012009-10-15 14:52:45 -07004608 /* Start compilation with maximally allowed trace length */
Carl Shapirofc75f3e2010-12-07 11:43:38 -08004609 desc = (JitTraceDescription *)work->info;
buzbee2e152ba2010-12-15 16:32:35 -08004610 success = dvmCompileTrace(desc, JIT_MAX_TRACE_LEN, &work->result,
4611 work->bailPtr, 0 /* no hints */);
Elliott Hughes672511b2010-04-26 17:40:13 -07004612 gDvmJit.printMe = oldPrintMe;
Ben Chengccd6c012009-10-15 14:52:45 -07004613 break;
4614 }
buzbee2e152ba2010-12-15 16:32:35 -08004615 case kWorkOrderProfileMode:
4616 dvmJitChangeProfileMode((TraceProfilingModes)work->info);
4617 isCompile = false;
4618 break;
Ben Chengccd6c012009-10-15 14:52:45 -07004619 default:
buzbee2e152ba2010-12-15 16:32:35 -08004620 isCompile = false;
Bill Buzbeefc519dc2010-03-06 23:30:57 -08004621 LOGE("Jit: unknown work order type");
Elliott Hughes672511b2010-04-26 17:40:13 -07004622 assert(0); // Bail if debug build, discard otherwise
Ben Chengccd6c012009-10-15 14:52:45 -07004623 }
buzbee2e152ba2010-12-15 16:32:35 -08004624 if (!success)
4625 work->result.codeAddress = NULL;
4626 return isCompile;
Ben Chengba4fc8b2009-06-01 13:00:29 -07004627}
4628
Ben Chengba4fc8b2009-06-01 13:00:29 -07004629/* Architectural-specific debugging helpers go here */
4630void dvmCompilerArchDump(void)
4631{
4632 /* Print compiled opcode in this VM instance */
4633 int i, start, streak;
4634 char buf[1024];
4635
4636 streak = i = 0;
4637 buf[0] = 0;
Dan Bornsteinccaab182010-12-03 15:32:40 -08004638 while (opcodeCoverage[i] == 0 && i < kNumPackedOpcodes) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07004639 i++;
4640 }
Dan Bornsteinccaab182010-12-03 15:32:40 -08004641 if (i == kNumPackedOpcodes) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07004642 return;
4643 }
Dan Bornsteinccaab182010-12-03 15:32:40 -08004644 for (start = i++, streak = 1; i < kNumPackedOpcodes; i++) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07004645 if (opcodeCoverage[i]) {
4646 streak++;
4647 } else {
4648 if (streak == 1) {
4649 sprintf(buf+strlen(buf), "%x,", start);
4650 } else {
4651 sprintf(buf+strlen(buf), "%x-%x,", start, start + streak - 1);
4652 }
4653 streak = 0;
Dan Bornsteinccaab182010-12-03 15:32:40 -08004654 while (opcodeCoverage[i] == 0 && i < kNumPackedOpcodes) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07004655 i++;
4656 }
Dan Bornsteinccaab182010-12-03 15:32:40 -08004657 if (i < kNumPackedOpcodes) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07004658 streak = 1;
4659 start = i;
4660 }
4661 }
4662 }
4663 if (streak) {
4664 if (streak == 1) {
4665 sprintf(buf+strlen(buf), "%x", start);
4666 } else {
4667 sprintf(buf+strlen(buf), "%x-%x", start, start + streak - 1);
4668 }
4669 }
4670 if (strlen(buf)) {
Ben Cheng8b258bf2009-06-24 17:27:07 -07004671 LOGD("dalvik.vm.jit.op = %s", buf);
Ben Chengba4fc8b2009-06-01 13:00:29 -07004672 }
4673}
Ben Chengd7d426a2009-09-22 11:23:36 -07004674
4675/* Common initialization routine for an architecture family */
4676bool dvmCompilerArchInit()
4677{
4678 int i;
4679
Bill Buzbee1465db52009-09-23 17:17:35 -07004680 for (i = 0; i < kArmLast; i++) {
Dan Bornstein9a1f8162010-12-01 17:02:26 -08004681 if (EncodingMap[i].opcode != i) {
Ben Chengd7d426a2009-09-22 11:23:36 -07004682 LOGE("Encoding order for %s is wrong: expecting %d, seeing %d",
Dan Bornstein9a1f8162010-12-01 17:02:26 -08004683 EncodingMap[i].name, i, EncodingMap[i].opcode);
Bill Buzbeefc519dc2010-03-06 23:30:57 -08004684 dvmAbort(); // OK to dvmAbort - build error
Ben Chengd7d426a2009-09-22 11:23:36 -07004685 }
4686 }
4687
Ben Cheng5d90c202009-11-22 23:31:11 -08004688 return dvmCompilerArchVariantInit();
4689}
4690
4691void *dvmCompilerGetInterpretTemplate()
4692{
4693 return (void*) ((int)gDvmJit.codeCache +
4694 templateEntryOffsets[TEMPLATE_INTERPRET]);
4695}
4696
Bill Buzbee1b3da592011-02-03 07:38:22 -08004697JitInstructionSetType dvmCompilerGetInterpretTemplateSet()
4698{
4699 return DALVIK_JIT_ARM;
4700}
4701
buzbeebff121a2010-08-04 15:25:06 -07004702/* Needed by the Assembler */
4703void dvmCompilerSetupResourceMasks(ArmLIR *lir)
4704{
4705 setupResourceMasks(lir);
4706}
4707
Ben Cheng5d90c202009-11-22 23:31:11 -08004708/* Needed by the ld/st optmizatons */
4709ArmLIR* dvmCompilerRegCopyNoInsert(CompilationUnit *cUnit, int rDest, int rSrc)
4710{
4711 return genRegCopyNoInsert(cUnit, rDest, rSrc);
4712}
4713
4714/* Needed by the register allocator */
4715ArmLIR* dvmCompilerRegCopy(CompilationUnit *cUnit, int rDest, int rSrc)
4716{
4717 return genRegCopy(cUnit, rDest, rSrc);
4718}
4719
4720/* Needed by the register allocator */
4721void dvmCompilerRegCopyWide(CompilationUnit *cUnit, int destLo, int destHi,
4722 int srcLo, int srcHi)
4723{
4724 genRegCopyWide(cUnit, destLo, destHi, srcLo, srcHi);
4725}
4726
4727void dvmCompilerFlushRegImpl(CompilationUnit *cUnit, int rBase,
4728 int displacement, int rSrc, OpSize size)
4729{
4730 storeBaseDisp(cUnit, rBase, displacement, rSrc, size);
4731}
4732
4733void dvmCompilerFlushRegWideImpl(CompilationUnit *cUnit, int rBase,
4734 int displacement, int rSrcLo, int rSrcHi)
4735{
4736 storeBaseDispWide(cUnit, rBase, displacement, rSrcLo, rSrcHi);
Ben Chengd7d426a2009-09-22 11:23:36 -07004737}