blob: 74f648bcbd9d1a290fd97156ae4f66821aaae8e0 [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);
buzbee919eb062010-07-12 12:59:22 -070035 loadWordDisp(cUnit, rGLUE, offsetof(InterpState, cardTable),
36 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 Chengbd1326d2010-04-02 15:04:53 -0700163 LOAD_FUNC_ADDR(cUnit, rlr, (int)funct);
Ben Cheng5d90c202009-11-22 23:31:11 -0800164 loadValueDirectWideFixed(cUnit, rlSrc1, r0, r1);
165 loadValueDirectWideFixed(cUnit, rlSrc2, r2, r3);
166 opReg(cUnit, kOpBlx, rlr);
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 *
223 * D/dalvikvm( 1538): 0x59414c5e (0026): ldr r14, [rpc, #220] <-hoisted
224 * 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 Chengbd1326d2010-04-02 15:04:53 -0700747 LOAD_FUNC_ADDR(cUnit, rlr, (int) callTgt);
Bill Buzbee1465db52009-09-23 17:17:35 -0700748 loadValueDirectWideFixed(cUnit, rlSrc2, r2, r3);
749 opReg(cUnit, kOpBlx, rlr);
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 Chengcfdeca32011-01-14 11:36:46 -0800955 if (!cUnit->methodJitMode) {
956 genDispatchToHandler(cUnit, gDvmJit.methodTraceSupport ?
957 TEMPLATE_RETURN_PROF :
958 TEMPLATE_RETURN);
Ben Cheng978738d2010-05-13 13:45:57 -0700959#if defined(WITH_JIT_TUNING)
Ben Chengcfdeca32011-01-14 11:36:46 -0800960 gDvmJit.returnOp++;
Bill Buzbee1465db52009-09-23 17:17:35 -0700961#endif
Ben Chengcfdeca32011-01-14 11:36:46 -0800962 int dPC = (int) (cUnit->method->insns + mir->offset);
963 /* Insert branch, but defer setting of target */
964 ArmLIR *branch = genUnconditionalBranch(cUnit, NULL);
965 /* Set up the place holder to reconstruct this Dalvik PC */
966 ArmLIR *pcrLabel = (ArmLIR *) dvmCompilerNew(sizeof(ArmLIR), true);
967 pcrLabel->opcode = kArmPseudoPCReconstructionCell;
968 pcrLabel->operands[0] = dPC;
969 pcrLabel->operands[1] = mir->offset;
970 /* Insert the place holder to the growable list */
971 dvmInsertGrowableList(&cUnit->pcReconstructionList,
972 (intptr_t) pcrLabel);
973 /* Branch to the PC reconstruction code */
974 branch->generic.target = (LIR *) pcrLabel;
975 }
976 /* TODO: Move result to InterpState for non-void returns */
Bill Buzbee1465db52009-09-23 17:17:35 -0700977}
978
Ben Chengba4fc8b2009-06-01 13:00:29 -0700979static void genProcessArgsNoRange(CompilationUnit *cUnit, MIR *mir,
980 DecodedInstruction *dInsn,
Bill Buzbee89efc3d2009-07-28 11:22:22 -0700981 ArmLIR **pcrLabel)
Ben Chengba4fc8b2009-06-01 13:00:29 -0700982{
983 unsigned int i;
984 unsigned int regMask = 0;
Bill Buzbee1465db52009-09-23 17:17:35 -0700985 RegLocation rlArg;
986 int numDone = 0;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700987
Bill Buzbee1465db52009-09-23 17:17:35 -0700988 /*
989 * Load arguments to r0..r4. Note that these registers may contain
990 * live values, so we clobber them immediately after loading to prevent
991 * them from being used as sources for subsequent loads.
992 */
Bill Buzbeec6f10662010-02-09 11:16:15 -0800993 dvmCompilerLockAllTemps(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700994 for (i = 0; i < dInsn->vA; i++) {
995 regMask |= 1 << i;
Bill Buzbeec6f10662010-02-09 11:16:15 -0800996 rlArg = dvmCompilerGetSrc(cUnit, mir, numDone++);
Bill Buzbee1465db52009-09-23 17:17:35 -0700997 loadValueDirectFixed(cUnit, rlArg, i);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700998 }
999 if (regMask) {
1000 /* Up to 5 args are pushed on top of FP - sizeofStackSaveArea */
Bill Buzbee1465db52009-09-23 17:17:35 -07001001 opRegRegImm(cUnit, kOpSub, r7, rFP,
1002 sizeof(StackSaveArea) + (dInsn->vA << 2));
Ben Chengba4fc8b2009-06-01 13:00:29 -07001003 /* generate null check */
1004 if (pcrLabel) {
Bill Buzbeec6f10662010-02-09 11:16:15 -08001005 *pcrLabel = genNullCheck(cUnit, dvmCompilerSSASrc(mir, 0), r0,
Bill Buzbee1465db52009-09-23 17:17:35 -07001006 mir->offset, NULL);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001007 }
Bill Buzbee270c1d62009-08-13 16:58:07 -07001008 storeMultiple(cUnit, r7, regMask);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001009 }
1010}
1011
1012static void genProcessArgsRange(CompilationUnit *cUnit, MIR *mir,
1013 DecodedInstruction *dInsn,
Bill Buzbee89efc3d2009-07-28 11:22:22 -07001014 ArmLIR **pcrLabel)
Ben Chengba4fc8b2009-06-01 13:00:29 -07001015{
1016 int srcOffset = dInsn->vC << 2;
1017 int numArgs = dInsn->vA;
1018 int regMask;
Bill Buzbee1465db52009-09-23 17:17:35 -07001019
1020 /*
1021 * Note: here, all promoted registers will have been flushed
1022 * back to the Dalvik base locations, so register usage restrictins
1023 * are lifted. All parms loaded from original Dalvik register
1024 * region - even though some might conceivably have valid copies
1025 * cached in a preserved register.
1026 */
Bill Buzbeec6f10662010-02-09 11:16:15 -08001027 dvmCompilerLockAllTemps(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07001028
Ben Chengba4fc8b2009-06-01 13:00:29 -07001029 /*
1030 * r4PC : &rFP[vC]
1031 * r7: &newFP[0]
1032 */
Bill Buzbee1465db52009-09-23 17:17:35 -07001033 opRegRegImm(cUnit, kOpAdd, r4PC, rFP, srcOffset);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001034 /* load [r0 .. min(numArgs,4)] */
1035 regMask = (1 << ((numArgs < 4) ? numArgs : 4)) - 1;
Ben Chengd7d426a2009-09-22 11:23:36 -07001036 /*
1037 * Protect the loadMultiple instruction from being reordered with other
1038 * Dalvik stack accesses.
jeffhao71eee1f2011-01-04 14:18:54 -08001039 *
1040 * This code is also shared by the invoke jumbo instructions, and this
1041 * does not need to be done if the invoke jumbo has no arguments.
Ben Chengd7d426a2009-09-22 11:23:36 -07001042 */
jeffhao71eee1f2011-01-04 14:18:54 -08001043 if (numArgs != 0) loadMultiple(cUnit, r4PC, regMask);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001044
Bill Buzbee1465db52009-09-23 17:17:35 -07001045 opRegRegImm(cUnit, kOpSub, r7, rFP,
1046 sizeof(StackSaveArea) + (numArgs << 2));
Ben Chengba4fc8b2009-06-01 13:00:29 -07001047 /* generate null check */
1048 if (pcrLabel) {
Bill Buzbeec6f10662010-02-09 11:16:15 -08001049 *pcrLabel = genNullCheck(cUnit, dvmCompilerSSASrc(mir, 0), r0,
Bill Buzbee1465db52009-09-23 17:17:35 -07001050 mir->offset, NULL);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001051 }
1052
1053 /*
1054 * Handle remaining 4n arguments:
1055 * store previously loaded 4 values and load the next 4 values
1056 */
1057 if (numArgs >= 8) {
Bill Buzbee89efc3d2009-07-28 11:22:22 -07001058 ArmLIR *loopLabel = NULL;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001059 /*
1060 * r0 contains "this" and it will be used later, so push it to the stack
Bill Buzbee270c1d62009-08-13 16:58:07 -07001061 * first. Pushing r5 (rFP) is just for stack alignment purposes.
Ben Chengba4fc8b2009-06-01 13:00:29 -07001062 */
Bill Buzbee1465db52009-09-23 17:17:35 -07001063 opImm(cUnit, kOpPush, (1 << r0 | 1 << rFP));
Ben Chengba4fc8b2009-06-01 13:00:29 -07001064 /* No need to generate the loop structure if numArgs <= 11 */
1065 if (numArgs > 11) {
1066 loadConstant(cUnit, 5, ((numArgs - 4) >> 2) << 2);
Bill Buzbee1465db52009-09-23 17:17:35 -07001067 loopLabel = newLIR0(cUnit, kArmPseudoTargetLabel);
Ben Chengd7d426a2009-09-22 11:23:36 -07001068 loopLabel->defMask = ENCODE_ALL;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001069 }
Bill Buzbee270c1d62009-08-13 16:58:07 -07001070 storeMultiple(cUnit, r7, regMask);
Ben Chengd7d426a2009-09-22 11:23:36 -07001071 /*
1072 * Protect the loadMultiple instruction from being reordered with other
1073 * Dalvik stack accesses.
1074 */
Bill Buzbee270c1d62009-08-13 16:58:07 -07001075 loadMultiple(cUnit, r4PC, regMask);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001076 /* No need to generate the loop structure if numArgs <= 11 */
1077 if (numArgs > 11) {
Bill Buzbee1465db52009-09-23 17:17:35 -07001078 opRegImm(cUnit, kOpSub, rFP, 4);
1079 genConditionalBranch(cUnit, kArmCondNe, loopLabel);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001080 }
1081 }
1082
1083 /* Save the last batch of loaded values */
jeffhao71eee1f2011-01-04 14:18:54 -08001084 if (numArgs != 0) storeMultiple(cUnit, r7, regMask);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001085
1086 /* Generate the loop epilogue - don't use r0 */
1087 if ((numArgs > 4) && (numArgs % 4)) {
1088 regMask = ((1 << (numArgs & 0x3)) - 1) << 1;
Ben Chengd7d426a2009-09-22 11:23:36 -07001089 /*
1090 * Protect the loadMultiple instruction from being reordered with other
1091 * Dalvik stack accesses.
1092 */
Bill Buzbee270c1d62009-08-13 16:58:07 -07001093 loadMultiple(cUnit, r4PC, regMask);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001094 }
1095 if (numArgs >= 8)
Bill Buzbee1465db52009-09-23 17:17:35 -07001096 opImm(cUnit, kOpPop, (1 << r0 | 1 << rFP));
Ben Chengba4fc8b2009-06-01 13:00:29 -07001097
1098 /* Save the modulo 4 arguments */
1099 if ((numArgs > 4) && (numArgs % 4)) {
Bill Buzbee270c1d62009-08-13 16:58:07 -07001100 storeMultiple(cUnit, r7, regMask);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001101 }
1102}
1103
Ben Cheng38329f52009-07-07 14:19:20 -07001104/*
1105 * Generate code to setup the call stack then jump to the chaining cell if it
1106 * is not a native method.
1107 */
1108static void genInvokeSingletonCommon(CompilationUnit *cUnit, MIR *mir,
Bill Buzbee89efc3d2009-07-28 11:22:22 -07001109 BasicBlock *bb, ArmLIR *labelList,
1110 ArmLIR *pcrLabel,
Ben Cheng38329f52009-07-07 14:19:20 -07001111 const Method *calleeMethod)
Ben Chengba4fc8b2009-06-01 13:00:29 -07001112{
Bill Buzbee1465db52009-09-23 17:17:35 -07001113 /*
1114 * Note: all Dalvik register state should be flushed to
1115 * memory by the point, so register usage restrictions no
1116 * longer apply. All temp & preserved registers may be used.
1117 */
Bill Buzbeec6f10662010-02-09 11:16:15 -08001118 dvmCompilerLockAllTemps(cUnit);
Bill Buzbee89efc3d2009-07-28 11:22:22 -07001119 ArmLIR *retChainingCell = &labelList[bb->fallThrough->id];
Ben Chengba4fc8b2009-06-01 13:00:29 -07001120
1121 /* r1 = &retChainingCell */
Bill Buzbee1465db52009-09-23 17:17:35 -07001122 ArmLIR *addrRetChain = opRegRegImm(cUnit, kOpAdd, r1, rpc, 0);
Ben Chengc8293e72010-10-12 11:50:10 -07001123
Ben Chengba4fc8b2009-06-01 13:00:29 -07001124 /* r4PC = dalvikCallsite */
1125 loadConstant(cUnit, r4PC,
1126 (int) (cUnit->method->insns + mir->offset));
1127 addrRetChain->generic.target = (LIR *) retChainingCell;
Ben Chengc8293e72010-10-12 11:50:10 -07001128
1129 /* r7 = calleeMethod->registersSize */
1130 loadConstant(cUnit, r7, calleeMethod->registersSize);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001131 /*
Ben Cheng38329f52009-07-07 14:19:20 -07001132 * r0 = calleeMethod (loaded upon calling genInvokeSingletonCommon)
Ben Chengba4fc8b2009-06-01 13:00:29 -07001133 * r1 = &ChainingCell
Ben Chengc8293e72010-10-12 11:50:10 -07001134 * r2 = calleeMethod->outsSize (to be loaded later for Java callees)
Ben Chengba4fc8b2009-06-01 13:00:29 -07001135 * r4PC = callsiteDPC
Ben Chengc8293e72010-10-12 11:50:10 -07001136 * r7 = calleeMethod->registersSize
Ben Chengba4fc8b2009-06-01 13:00:29 -07001137 */
1138 if (dvmIsNativeMethod(calleeMethod)) {
buzbee18fba342011-01-19 15:31:15 -08001139 genDispatchToHandler(cUnit, gDvmJit.methodTraceSupport ?
1140 TEMPLATE_INVOKE_METHOD_NATIVE_PROF :
1141 TEMPLATE_INVOKE_METHOD_NATIVE);
Ben Cheng978738d2010-05-13 13:45:57 -07001142#if defined(WITH_JIT_TUNING)
Ben Cheng38329f52009-07-07 14:19:20 -07001143 gDvmJit.invokeNative++;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001144#endif
1145 } else {
Ben Chengc8293e72010-10-12 11:50:10 -07001146 /* For Java callees, set up r2 to be calleeMethod->outsSize */
1147 loadConstant(cUnit, r2, calleeMethod->outsSize);
buzbee18fba342011-01-19 15:31:15 -08001148 genDispatchToHandler(cUnit, gDvmJit.methodTraceSupport ?
1149 TEMPLATE_INVOKE_METHOD_CHAIN_PROF :
1150 TEMPLATE_INVOKE_METHOD_CHAIN);
Ben Cheng978738d2010-05-13 13:45:57 -07001151#if defined(WITH_JIT_TUNING)
Ben Cheng86717f72010-03-05 15:27:21 -08001152 gDvmJit.invokeMonomorphic++;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001153#endif
Ben Cheng38329f52009-07-07 14:19:20 -07001154 /* Branch to the chaining cell */
Ben Chengba4fc8b2009-06-01 13:00:29 -07001155 genUnconditionalBranch(cUnit, &labelList[bb->taken->id]);
1156 }
1157 /* Handle exceptions using the interpreter */
1158 genTrap(cUnit, mir->offset, pcrLabel);
1159}
1160
Ben Cheng38329f52009-07-07 14:19:20 -07001161/*
1162 * Generate code to check the validity of a predicted chain and take actions
1163 * based on the result.
1164 *
1165 * 0x426a99aa : ldr r4, [pc, #72] --> r4 <- dalvikPC of this invoke
1166 * 0x426a99ac : add r1, pc, #32 --> r1 <- &retChainingCell
1167 * 0x426a99ae : add r2, pc, #40 --> r2 <- &predictedChainingCell
1168 * 0x426a99b0 : blx_1 0x426a918c --+ TEMPLATE_INVOKE_METHOD_PREDICTED_CHAIN
1169 * 0x426a99b2 : blx_2 see above --+
1170 * 0x426a99b4 : b 0x426a99d8 --> off to the predicted chain
1171 * 0x426a99b6 : b 0x426a99c8 --> punt to the interpreter
1172 * 0x426a99b8 : ldr r0, [r7, #44] --> r0 <- this->class->vtable[methodIdx]
1173 * 0x426a99ba : cmp r1, #0 --> compare r1 (rechain count) against 0
1174 * 0x426a99bc : bgt 0x426a99c2 --> >=0? don't rechain
Ben Chengaf5aa1f2011-01-04 15:37:04 -08001175 * 0x426a99be : ldr r7, [pc, #off]--+ dvmJitToPatchPredictedChain
Ben Cheng38329f52009-07-07 14:19:20 -07001176 * 0x426a99c0 : blx r7 --+
1177 * 0x426a99c2 : add r1, pc, #12 --> r1 <- &retChainingCell
1178 * 0x426a99c4 : blx_1 0x426a9098 --+ TEMPLATE_INVOKE_METHOD_NO_OPT
1179 * 0x426a99c6 : blx_2 see above --+
1180 */
1181static void genInvokeVirtualCommon(CompilationUnit *cUnit, MIR *mir,
1182 int methodIndex,
Bill Buzbee89efc3d2009-07-28 11:22:22 -07001183 ArmLIR *retChainingCell,
1184 ArmLIR *predChainingCell,
1185 ArmLIR *pcrLabel)
Ben Cheng38329f52009-07-07 14:19:20 -07001186{
Bill Buzbee1465db52009-09-23 17:17:35 -07001187 /*
1188 * Note: all Dalvik register state should be flushed to
1189 * memory by the point, so register usage restrictions no
1190 * longer apply. Lock temps to prevent them from being
1191 * allocated by utility routines.
1192 */
Bill Buzbeec6f10662010-02-09 11:16:15 -08001193 dvmCompilerLockAllTemps(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07001194
Ben Cheng38329f52009-07-07 14:19:20 -07001195 /* "this" is already left in r0 by genProcessArgs* */
1196
1197 /* r4PC = dalvikCallsite */
1198 loadConstant(cUnit, r4PC,
1199 (int) (cUnit->method->insns + mir->offset));
1200
1201 /* r1 = &retChainingCell */
Bill Buzbee1465db52009-09-23 17:17:35 -07001202 ArmLIR *addrRetChain = opRegRegImm(cUnit, kOpAdd, r1, rpc, 0);
Ben Cheng38329f52009-07-07 14:19:20 -07001203 addrRetChain->generic.target = (LIR *) retChainingCell;
1204
1205 /* r2 = &predictedChainingCell */
Bill Buzbee1465db52009-09-23 17:17:35 -07001206 ArmLIR *predictedChainingCell = opRegRegImm(cUnit, kOpAdd, r2, rpc, 0);
Ben Cheng38329f52009-07-07 14:19:20 -07001207 predictedChainingCell->generic.target = (LIR *) predChainingCell;
1208
buzbee18fba342011-01-19 15:31:15 -08001209 genDispatchToHandler(cUnit, gDvmJit.methodTraceSupport ?
1210 TEMPLATE_INVOKE_METHOD_PREDICTED_CHAIN_PROF :
1211 TEMPLATE_INVOKE_METHOD_PREDICTED_CHAIN);
Ben Cheng38329f52009-07-07 14:19:20 -07001212
1213 /* return through lr - jump to the chaining cell */
1214 genUnconditionalBranch(cUnit, predChainingCell);
1215
1216 /*
1217 * null-check on "this" may have been eliminated, but we still need a PC-
1218 * reconstruction label for stack overflow bailout.
1219 */
1220 if (pcrLabel == NULL) {
1221 int dPC = (int) (cUnit->method->insns + mir->offset);
Carl Shapirofc75f3e2010-12-07 11:43:38 -08001222 pcrLabel = (ArmLIR *) dvmCompilerNew(sizeof(ArmLIR), true);
Dan Bornstein9a1f8162010-12-01 17:02:26 -08001223 pcrLabel->opcode = kArmPseudoPCReconstructionCell;
Ben Cheng38329f52009-07-07 14:19:20 -07001224 pcrLabel->operands[0] = dPC;
1225 pcrLabel->operands[1] = mir->offset;
1226 /* Insert the place holder to the growable list */
Ben Cheng00603072010-10-28 11:13:58 -07001227 dvmInsertGrowableList(&cUnit->pcReconstructionList,
1228 (intptr_t) pcrLabel);
Ben Cheng38329f52009-07-07 14:19:20 -07001229 }
1230
1231 /* return through lr+2 - punt to the interpreter */
1232 genUnconditionalBranch(cUnit, pcrLabel);
1233
1234 /*
1235 * return through lr+4 - fully resolve the callee method.
1236 * r1 <- count
1237 * r2 <- &predictedChainCell
1238 * r3 <- this->class
1239 * r4 <- dPC
1240 * r7 <- this->class->vtable
1241 */
1242
1243 /* r0 <- calleeMethod */
Bill Buzbee270c1d62009-08-13 16:58:07 -07001244 loadWordDisp(cUnit, r7, methodIndex * 4, r0);
Ben Cheng38329f52009-07-07 14:19:20 -07001245
1246 /* Check if rechain limit is reached */
buzbee8f8109a2010-08-31 10:16:35 -07001247 ArmLIR *bypassRechaining = genCmpImmBranch(cUnit, kArmCondGt, r1, 0);
Ben Cheng38329f52009-07-07 14:19:20 -07001248
Ben Chengaf5aa1f2011-01-04 15:37:04 -08001249 LOAD_FUNC_ADDR(cUnit, r7, (int) dvmJitToPatchPredictedChain);
Ben Cheng38329f52009-07-07 14:19:20 -07001250
Ben Chengb88ec3c2010-05-17 12:50:33 -07001251 genRegCopy(cUnit, r1, rGLUE);
1252
Ben Cheng38329f52009-07-07 14:19:20 -07001253 /*
1254 * r0 = calleeMethod
1255 * r2 = &predictedChainingCell
1256 * r3 = class
1257 *
1258 * &returnChainingCell has been loaded into r1 but is not needed
1259 * when patching the chaining cell and will be clobbered upon
1260 * returning so it will be reconstructed again.
1261 */
Bill Buzbee1465db52009-09-23 17:17:35 -07001262 opReg(cUnit, kOpBlx, r7);
Ben Cheng38329f52009-07-07 14:19:20 -07001263
1264 /* r1 = &retChainingCell */
Bill Buzbee1465db52009-09-23 17:17:35 -07001265 addrRetChain = opRegRegImm(cUnit, kOpAdd, r1, rpc, 0);
Ben Cheng38329f52009-07-07 14:19:20 -07001266 addrRetChain->generic.target = (LIR *) retChainingCell;
1267
1268 bypassRechaining->generic.target = (LIR *) addrRetChain;
1269 /*
1270 * r0 = calleeMethod,
1271 * r1 = &ChainingCell,
1272 * r4PC = callsiteDPC,
1273 */
buzbee18fba342011-01-19 15:31:15 -08001274 genDispatchToHandler(cUnit, gDvmJit.methodTraceSupport ?
1275 TEMPLATE_INVOKE_METHOD_NO_OPT_PROF :
1276 TEMPLATE_INVOKE_METHOD_NO_OPT);
Ben Cheng978738d2010-05-13 13:45:57 -07001277#if defined(WITH_JIT_TUNING)
Ben Cheng86717f72010-03-05 15:27:21 -08001278 gDvmJit.invokePolymorphic++;
Ben Cheng38329f52009-07-07 14:19:20 -07001279#endif
1280 /* Handle exceptions using the interpreter */
1281 genTrap(cUnit, mir->offset, pcrLabel);
1282}
1283
Ben Chengba4fc8b2009-06-01 13:00:29 -07001284/* Geneate a branch to go back to the interpreter */
1285static void genPuntToInterp(CompilationUnit *cUnit, unsigned int offset)
1286{
1287 /* r0 = dalvik pc */
Bill Buzbeec6f10662010-02-09 11:16:15 -08001288 dvmCompilerFlushAllRegs(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001289 loadConstant(cUnit, r0, (int) (cUnit->method->insns + offset));
Bill Buzbee270c1d62009-08-13 16:58:07 -07001290 loadWordDisp(cUnit, rGLUE, offsetof(InterpState,
1291 jitToInterpEntries.dvmJitToInterpPunt), r1);
Bill Buzbee1465db52009-09-23 17:17:35 -07001292 opReg(cUnit, kOpBlx, r1);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001293}
1294
1295/*
1296 * Attempt to single step one instruction using the interpreter and return
1297 * to the compiled code for the next Dalvik instruction
1298 */
1299static void genInterpSingleStep(CompilationUnit *cUnit, MIR *mir)
1300{
Dan Bornsteine4852762010-12-02 12:45:00 -08001301 int flags = dexGetFlagsFromOpcode(mir->dalvikInsn.opcode);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001302 int flagsToCheck = kInstrCanBranch | kInstrCanSwitch | kInstrCanReturn |
1303 kInstrCanThrow;
Bill Buzbee1465db52009-09-23 17:17:35 -07001304
Bill Buzbee45273872010-03-11 11:12:15 -08001305 //If already optimized out, just ignore
Dan Bornstein9a1f8162010-12-01 17:02:26 -08001306 if (mir->dalvikInsn.opcode == OP_NOP)
Bill Buzbee45273872010-03-11 11:12:15 -08001307 return;
1308
Bill Buzbee1465db52009-09-23 17:17:35 -07001309 //Ugly, but necessary. Flush all Dalvik regs so Interp can find them
Bill Buzbeec6f10662010-02-09 11:16:15 -08001310 dvmCompilerFlushAllRegs(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07001311
Ben Chengba4fc8b2009-06-01 13:00:29 -07001312 if ((mir->next == NULL) || (flags & flagsToCheck)) {
1313 genPuntToInterp(cUnit, mir->offset);
1314 return;
1315 }
1316 int entryAddr = offsetof(InterpState,
1317 jitToInterpEntries.dvmJitToInterpSingleStep);
Bill Buzbee270c1d62009-08-13 16:58:07 -07001318 loadWordDisp(cUnit, rGLUE, entryAddr, r2);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001319 /* r0 = dalvik pc */
1320 loadConstant(cUnit, r0, (int) (cUnit->method->insns + mir->offset));
1321 /* r1 = dalvik pc of following instruction */
1322 loadConstant(cUnit, r1, (int) (cUnit->method->insns + mir->next->offset));
Bill Buzbee1465db52009-09-23 17:17:35 -07001323 opReg(cUnit, kOpBlx, r2);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001324}
1325
Carl Shapiro01605d22011-02-01 11:32:44 -08001326#if defined(_ARMV5TE) || defined(_ARMV5TE_VFP)
Bill Buzbeec1d9ed42010-02-02 11:04:33 -08001327/*
1328 * To prevent a thread in a monitor wait from blocking the Jit from
1329 * resetting the code cache, heavyweight monitor lock will not
1330 * be allowed to return to an existing translation. Instead, we will
1331 * handle them by branching to a handler, which will in turn call the
1332 * runtime lock routine and then branch directly back to the
1333 * interpreter main loop. Given the high cost of the heavyweight
1334 * lock operation, this additional cost should be slight (especially when
1335 * considering that we expect the vast majority of lock operations to
1336 * use the fast-path thin lock bypass).
1337 */
Ben Cheng5d90c202009-11-22 23:31:11 -08001338static void genMonitorPortable(CompilationUnit *cUnit, MIR *mir)
Bill Buzbee270c1d62009-08-13 16:58:07 -07001339{
Dan Bornstein9a1f8162010-12-01 17:02:26 -08001340 bool isEnter = (mir->dalvikInsn.opcode == OP_MONITOR_ENTER);
Bill Buzbee1465db52009-09-23 17:17:35 -07001341 genExportPC(cUnit, mir);
Bill Buzbeec6f10662010-02-09 11:16:15 -08001342 dvmCompilerFlushAllRegs(cUnit); /* Send everything to home location */
1343 RegLocation rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07001344 loadValueDirectFixed(cUnit, rlSrc, r1);
1345 loadWordDisp(cUnit, rGLUE, offsetof(InterpState, self), r0);
Bill Buzbeec1d9ed42010-02-02 11:04:33 -08001346 genNullCheck(cUnit, rlSrc.sRegLow, r1, mir->offset, NULL);
Bill Buzbeeefbd3c52009-11-04 22:18:40 -08001347 if (isEnter) {
Bill Buzbeec1d9ed42010-02-02 11:04:33 -08001348 /* Get dPC of next insn */
1349 loadConstant(cUnit, r4PC, (int)(cUnit->method->insns + mir->offset +
Dan Bornsteine4852762010-12-02 12:45:00 -08001350 dexGetWidthFromOpcode(OP_MONITOR_ENTER)));
Bill Buzbeec1d9ed42010-02-02 11:04:33 -08001351 genDispatchToHandler(cUnit, TEMPLATE_MONITOR_ENTER);
Bill Buzbee1465db52009-09-23 17:17:35 -07001352 } else {
Ben Chengbd1326d2010-04-02 15:04:53 -07001353 LOAD_FUNC_ADDR(cUnit, r2, (int)dvmUnlockObject);
Bill Buzbeec1d9ed42010-02-02 11:04:33 -08001354 /* Do the call */
1355 opReg(cUnit, kOpBlx, r2);
buzbee8f8109a2010-08-31 10:16:35 -07001356 /* Did we throw? */
1357 ArmLIR *branchOver = genCmpImmBranch(cUnit, kArmCondNe, r0, 0);
Bill Buzbee6bbdd6b2010-02-16 14:40:01 -08001358 loadConstant(cUnit, r0,
1359 (int) (cUnit->method->insns + mir->offset +
Dan Bornsteine4852762010-12-02 12:45:00 -08001360 dexGetWidthFromOpcode(OP_MONITOR_EXIT)));
Bill Buzbee6bbdd6b2010-02-16 14:40:01 -08001361 genDispatchToHandler(cUnit, TEMPLATE_THROW_EXCEPTION_COMMON);
1362 ArmLIR *target = newLIR0(cUnit, kArmPseudoTargetLabel);
1363 target->defMask = ENCODE_ALL;
1364 branchOver->generic.target = (LIR *) target;
Elliott Hughes6a555132010-02-25 15:41:42 -08001365 dvmCompilerClobberCallRegs(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07001366 }
Bill Buzbee270c1d62009-08-13 16:58:07 -07001367}
Ben Chengfc075c22010-05-28 15:20:08 -07001368#endif
Bill Buzbee270c1d62009-08-13 16:58:07 -07001369
Ben Chengba4fc8b2009-06-01 13:00:29 -07001370/*
1371 * The following are the first-level codegen routines that analyze the format
1372 * of each bytecode then either dispatch special purpose codegen routines
1373 * or produce corresponding Thumb instructions directly.
1374 */
1375
1376static bool handleFmt10t_Fmt20t_Fmt30t(CompilationUnit *cUnit, MIR *mir,
Bill Buzbee89efc3d2009-07-28 11:22:22 -07001377 BasicBlock *bb, ArmLIR *labelList)
Ben Chengba4fc8b2009-06-01 13:00:29 -07001378{
1379 /* For OP_GOTO, OP_GOTO_16, and OP_GOTO_32 */
1380 genUnconditionalBranch(cUnit, &labelList[bb->taken->id]);
1381 return false;
1382}
1383
1384static bool handleFmt10x(CompilationUnit *cUnit, MIR *mir)
1385{
Dan Bornstein9a1f8162010-12-01 17:02:26 -08001386 Opcode dalvikOpcode = mir->dalvikInsn.opcode;
1387 if ((dalvikOpcode >= OP_UNUSED_3E) && (dalvikOpcode <= OP_UNUSED_43)) {
1388 LOGE("Codegen: got unused opcode 0x%x\n",dalvikOpcode);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001389 return true;
1390 }
Dan Bornstein9a1f8162010-12-01 17:02:26 -08001391 switch (dalvikOpcode) {
Andy McFadden291758c2010-09-10 08:04:52 -07001392 case OP_RETURN_VOID_BARRIER:
buzbee2ce33c92010-11-01 15:53:27 -07001393 dvmCompilerGenMemBarrier(cUnit, kST);
1394 // Intentional fallthrough
1395 case OP_RETURN_VOID:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001396 genReturnCommon(cUnit,mir);
1397 break;
1398 case OP_UNUSED_73:
1399 case OP_UNUSED_79:
1400 case OP_UNUSED_7A:
Dan Bornstein90f15432010-12-02 16:46:25 -08001401 case OP_DISPATCH_FF:
Dan Bornstein9a1f8162010-12-01 17:02:26 -08001402 LOGE("Codegen: got unused opcode 0x%x\n",dalvikOpcode);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001403 return true;
1404 case OP_NOP:
1405 break;
1406 default:
1407 return true;
1408 }
1409 return false;
1410}
1411
1412static bool handleFmt11n_Fmt31i(CompilationUnit *cUnit, MIR *mir)
1413{
Bill Buzbee1465db52009-09-23 17:17:35 -07001414 RegLocation rlDest;
1415 RegLocation rlResult;
1416 if (mir->ssaRep->numDefs == 2) {
Bill Buzbeec6f10662010-02-09 11:16:15 -08001417 rlDest = dvmCompilerGetDestWide(cUnit, mir, 0, 1);
Bill Buzbee1465db52009-09-23 17:17:35 -07001418 } else {
Bill Buzbeec6f10662010-02-09 11:16:15 -08001419 rlDest = dvmCompilerGetDest(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07001420 }
Ben Chenge9695e52009-06-16 16:11:47 -07001421
Dan Bornstein9a1f8162010-12-01 17:02:26 -08001422 switch (mir->dalvikInsn.opcode) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07001423 case OP_CONST:
Ben Chenge9695e52009-06-16 16:11:47 -07001424 case OP_CONST_4: {
Bill Buzbeec6f10662010-02-09 11:16:15 -08001425 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kAnyReg, true);
Ben Chengbd1326d2010-04-02 15:04:53 -07001426 loadConstantNoClobber(cUnit, rlResult.lowReg, mir->dalvikInsn.vB);
Bill Buzbee1465db52009-09-23 17:17:35 -07001427 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001428 break;
Ben Chenge9695e52009-06-16 16:11:47 -07001429 }
1430 case OP_CONST_WIDE_32: {
Bill Buzbee1465db52009-09-23 17:17:35 -07001431 //TUNING: single routine to load constant pair for support doubles
Bill Buzbee964a7b02010-01-28 12:54:19 -08001432 //TUNING: load 0/-1 separately to avoid load dependency
Bill Buzbeec6f10662010-02-09 11:16:15 -08001433 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Ben Chengbd1326d2010-04-02 15:04:53 -07001434 loadConstantNoClobber(cUnit, rlResult.lowReg, mir->dalvikInsn.vB);
Bill Buzbee1465db52009-09-23 17:17:35 -07001435 opRegRegImm(cUnit, kOpAsr, rlResult.highReg,
1436 rlResult.lowReg, 31);
1437 storeValueWide(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001438 break;
Ben Chenge9695e52009-06-16 16:11:47 -07001439 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07001440 default:
1441 return true;
1442 }
1443 return false;
1444}
1445
1446static bool handleFmt21h(CompilationUnit *cUnit, MIR *mir)
1447{
Bill Buzbee1465db52009-09-23 17:17:35 -07001448 RegLocation rlDest;
1449 RegLocation rlResult;
1450 if (mir->ssaRep->numDefs == 2) {
Bill Buzbeec6f10662010-02-09 11:16:15 -08001451 rlDest = dvmCompilerGetDestWide(cUnit, mir, 0, 1);
Bill Buzbee1465db52009-09-23 17:17:35 -07001452 } else {
Bill Buzbeec6f10662010-02-09 11:16:15 -08001453 rlDest = dvmCompilerGetDest(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07001454 }
Bill Buzbeec6f10662010-02-09 11:16:15 -08001455 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kAnyReg, true);
Ben Chenge9695e52009-06-16 16:11:47 -07001456
Dan Bornstein9a1f8162010-12-01 17:02:26 -08001457 switch (mir->dalvikInsn.opcode) {
Ben Chenge9695e52009-06-16 16:11:47 -07001458 case OP_CONST_HIGH16: {
Ben Chengbd1326d2010-04-02 15:04:53 -07001459 loadConstantNoClobber(cUnit, rlResult.lowReg,
1460 mir->dalvikInsn.vB << 16);
Bill Buzbee1465db52009-09-23 17:17:35 -07001461 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001462 break;
Ben Chenge9695e52009-06-16 16:11:47 -07001463 }
1464 case OP_CONST_WIDE_HIGH16: {
Bill Buzbee1465db52009-09-23 17:17:35 -07001465 loadConstantValueWide(cUnit, rlResult.lowReg, rlResult.highReg,
1466 0, mir->dalvikInsn.vB << 16);
1467 storeValueWide(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001468 break;
Ben Chenge9695e52009-06-16 16:11:47 -07001469 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07001470 default:
1471 return true;
1472 }
1473 return false;
1474}
1475
jeffhao71eee1f2011-01-04 14:18:54 -08001476static bool handleFmt20bc_Fmt40sc(CompilationUnit *cUnit, MIR *mir)
Ben Chengba4fc8b2009-06-01 13:00:29 -07001477{
jeffhao71eee1f2011-01-04 14:18:54 -08001478 /* For OP_THROW_VERIFICATION_ERROR & OP_THROW_VERIFICATION_ERROR_JUMBO */
Ben Chengba4fc8b2009-06-01 13:00:29 -07001479 genInterpSingleStep(cUnit, mir);
1480 return false;
1481}
1482
jeffhao71eee1f2011-01-04 14:18:54 -08001483static bool handleFmt21c_Fmt31c_Fmt41c(CompilationUnit *cUnit, MIR *mir)
Ben Chengba4fc8b2009-06-01 13:00:29 -07001484{
Bill Buzbee1465db52009-09-23 17:17:35 -07001485 RegLocation rlResult;
1486 RegLocation rlDest;
1487 RegLocation rlSrc;
Ben Chenge9695e52009-06-16 16:11:47 -07001488
Dan Bornstein9a1f8162010-12-01 17:02:26 -08001489 switch (mir->dalvikInsn.opcode) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07001490 case OP_CONST_STRING_JUMBO:
1491 case OP_CONST_STRING: {
1492 void *strPtr = (void*)
1493 (cUnit->method->clazz->pDvmDex->pResStrings[mir->dalvikInsn.vB]);
Ben Chengdd6e8702010-05-07 13:05:47 -07001494
1495 if (strPtr == NULL) {
1496 LOGE("Unexpected null string");
1497 dvmAbort();
1498 }
1499
Bill Buzbeec6f10662010-02-09 11:16:15 -08001500 rlDest = dvmCompilerGetDest(cUnit, mir, 0);
1501 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Ben Chengbd1326d2010-04-02 15:04:53 -07001502 loadConstantNoClobber(cUnit, rlResult.lowReg, (int) strPtr );
Bill Buzbee1465db52009-09-23 17:17:35 -07001503 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001504 break;
1505 }
jeffhao71eee1f2011-01-04 14:18:54 -08001506 case OP_CONST_CLASS:
1507 case OP_CONST_CLASS_JUMBO: {
Ben Chengba4fc8b2009-06-01 13:00:29 -07001508 void *classPtr = (void*)
1509 (cUnit->method->clazz->pDvmDex->pResClasses[mir->dalvikInsn.vB]);
Ben Chengdd6e8702010-05-07 13:05:47 -07001510
1511 if (classPtr == NULL) {
1512 LOGE("Unexpected null class");
1513 dvmAbort();
1514 }
1515
Bill Buzbeec6f10662010-02-09 11:16:15 -08001516 rlDest = dvmCompilerGetDest(cUnit, mir, 0);
1517 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Ben Chengbd1326d2010-04-02 15:04:53 -07001518 loadConstantNoClobber(cUnit, rlResult.lowReg, (int) classPtr );
Bill Buzbee1465db52009-09-23 17:17:35 -07001519 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001520 break;
1521 }
jeffhao71eee1f2011-01-04 14:18:54 -08001522 case OP_SGET:
buzbeeecf8f6e2010-07-20 14:53:42 -07001523 case OP_SGET_VOLATILE:
jeffhao71eee1f2011-01-04 14:18:54 -08001524 case OP_SGET_JUMBO:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001525 case OP_SGET_OBJECT:
jeffhao71eee1f2011-01-04 14:18:54 -08001526 case OP_SGET_OBJECT_VOLATILE:
1527 case OP_SGET_OBJECT_JUMBO:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001528 case OP_SGET_BOOLEAN:
jeffhao71eee1f2011-01-04 14:18:54 -08001529 case OP_SGET_BOOLEAN_JUMBO:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001530 case OP_SGET_CHAR:
jeffhao71eee1f2011-01-04 14:18:54 -08001531 case OP_SGET_CHAR_JUMBO:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001532 case OP_SGET_BYTE:
jeffhao71eee1f2011-01-04 14:18:54 -08001533 case OP_SGET_BYTE_JUMBO:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001534 case OP_SGET_SHORT:
jeffhao71eee1f2011-01-04 14:18:54 -08001535 case OP_SGET_SHORT_JUMBO: {
Bill Buzbee50a6bf22009-07-08 13:08:04 -07001536 int valOffset = offsetof(StaticField, value);
Bill Buzbeec6f10662010-02-09 11:16:15 -08001537 int tReg = dvmCompilerAllocTemp(cUnit);
buzbeeecf8f6e2010-07-20 14:53:42 -07001538 bool isVolatile;
Ben Cheng7a2697d2010-06-07 13:44:23 -07001539 const Method *method = (mir->OptimizationFlags & MIR_CALLEE) ?
1540 mir->meta.calleeMethod : cUnit->method;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001541 void *fieldPtr = (void*)
Ben Cheng7a2697d2010-06-07 13:44:23 -07001542 (method->clazz->pDvmDex->pResFields[mir->dalvikInsn.vB]);
Ben Chengdd6e8702010-05-07 13:05:47 -07001543
1544 if (fieldPtr == NULL) {
1545 LOGE("Unexpected null static field");
1546 dvmAbort();
1547 }
1548
Dan Bornstein9a1f8162010-12-01 17:02:26 -08001549 isVolatile = (mir->dalvikInsn.opcode == OP_SGET_VOLATILE) ||
1550 (mir->dalvikInsn.opcode == OP_SGET_OBJECT_VOLATILE) ||
Carl Shapirofc75f3e2010-12-07 11:43:38 -08001551 dvmIsVolatileField((Field *) fieldPtr);
buzbeeecf8f6e2010-07-20 14:53:42 -07001552
Bill Buzbeec6f10662010-02-09 11:16:15 -08001553 rlDest = dvmCompilerGetDest(cUnit, mir, 0);
1554 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kAnyReg, true);
Bill Buzbee1465db52009-09-23 17:17:35 -07001555 loadConstant(cUnit, tReg, (int) fieldPtr + valOffset);
Ben Cheng11d8f142010-03-24 15:24:19 -07001556
buzbeeecf8f6e2010-07-20 14:53:42 -07001557 if (isVolatile) {
buzbee2ce33c92010-11-01 15:53:27 -07001558 dvmCompilerGenMemBarrier(cUnit, kSY);
buzbeeecf8f6e2010-07-20 14:53:42 -07001559 }
Ben Cheng11d8f142010-03-24 15:24:19 -07001560 HEAP_ACCESS_SHADOW(true);
Bill Buzbee1465db52009-09-23 17:17:35 -07001561 loadWordDisp(cUnit, tReg, 0, rlResult.lowReg);
Ben Cheng11d8f142010-03-24 15:24:19 -07001562 HEAP_ACCESS_SHADOW(false);
1563
Bill Buzbee1465db52009-09-23 17:17:35 -07001564 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001565 break;
1566 }
jeffhao71eee1f2011-01-04 14:18:54 -08001567 case OP_SGET_WIDE:
1568 case OP_SGET_WIDE_JUMBO: {
Bill Buzbee50a6bf22009-07-08 13:08:04 -07001569 int valOffset = offsetof(StaticField, value);
Ben Cheng7a2697d2010-06-07 13:44:23 -07001570 const Method *method = (mir->OptimizationFlags & MIR_CALLEE) ?
1571 mir->meta.calleeMethod : cUnit->method;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001572 void *fieldPtr = (void*)
Ben Cheng7a2697d2010-06-07 13:44:23 -07001573 (method->clazz->pDvmDex->pResFields[mir->dalvikInsn.vB]);
Ben Chengdd6e8702010-05-07 13:05:47 -07001574
1575 if (fieldPtr == NULL) {
1576 LOGE("Unexpected null static field");
1577 dvmAbort();
1578 }
1579
Bill Buzbeec6f10662010-02-09 11:16:15 -08001580 int tReg = dvmCompilerAllocTemp(cUnit);
Bill Buzbeec6f10662010-02-09 11:16:15 -08001581 rlDest = dvmCompilerGetDestWide(cUnit, mir, 0, 1);
1582 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kAnyReg, true);
Bill Buzbee1465db52009-09-23 17:17:35 -07001583 loadConstant(cUnit, tReg, (int) fieldPtr + valOffset);
Ben Cheng11d8f142010-03-24 15:24:19 -07001584
1585 HEAP_ACCESS_SHADOW(true);
Bill Buzbee1465db52009-09-23 17:17:35 -07001586 loadPair(cUnit, tReg, rlResult.lowReg, rlResult.highReg);
Ben Cheng11d8f142010-03-24 15:24:19 -07001587 HEAP_ACCESS_SHADOW(false);
1588
Bill Buzbee1465db52009-09-23 17:17:35 -07001589 storeValueWide(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001590 break;
1591 }
jeffhao71eee1f2011-01-04 14:18:54 -08001592 case OP_SPUT:
1593 case OP_SPUT_VOLATILE:
1594 case OP_SPUT_JUMBO:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001595 case OP_SPUT_OBJECT:
buzbeeddc7d292010-09-02 17:16:24 -07001596 case OP_SPUT_OBJECT_VOLATILE:
jeffhao71eee1f2011-01-04 14:18:54 -08001597 case OP_SPUT_OBJECT_JUMBO:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001598 case OP_SPUT_BOOLEAN:
jeffhao71eee1f2011-01-04 14:18:54 -08001599 case OP_SPUT_BOOLEAN_JUMBO:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001600 case OP_SPUT_CHAR:
jeffhao71eee1f2011-01-04 14:18:54 -08001601 case OP_SPUT_CHAR_JUMBO:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001602 case OP_SPUT_BYTE:
jeffhao71eee1f2011-01-04 14:18:54 -08001603 case OP_SPUT_BYTE_JUMBO:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001604 case OP_SPUT_SHORT:
jeffhao71eee1f2011-01-04 14:18:54 -08001605 case OP_SPUT_SHORT_JUMBO: {
Bill Buzbee50a6bf22009-07-08 13:08:04 -07001606 int valOffset = offsetof(StaticField, value);
Bill Buzbeec6f10662010-02-09 11:16:15 -08001607 int tReg = dvmCompilerAllocTemp(cUnit);
buzbeed3b0a4b2010-09-27 11:30:22 -07001608 int objHead;
buzbeeecf8f6e2010-07-20 14:53:42 -07001609 bool isVolatile;
buzbeed3b0a4b2010-09-27 11:30:22 -07001610 bool isSputObject;
Ben Cheng7a2697d2010-06-07 13:44:23 -07001611 const Method *method = (mir->OptimizationFlags & MIR_CALLEE) ?
1612 mir->meta.calleeMethod : cUnit->method;
1613 void *fieldPtr = (void*)
1614 (method->clazz->pDvmDex->pResFields[mir->dalvikInsn.vB]);
Ben Chenge9695e52009-06-16 16:11:47 -07001615
Dan Bornstein9a1f8162010-12-01 17:02:26 -08001616 isVolatile = (mir->dalvikInsn.opcode == OP_SPUT_VOLATILE) ||
1617 (mir->dalvikInsn.opcode == OP_SPUT_OBJECT_VOLATILE) ||
Carl Shapirofc75f3e2010-12-07 11:43:38 -08001618 dvmIsVolatileField((Field *) fieldPtr);
buzbeeecf8f6e2010-07-20 14:53:42 -07001619
Dan Bornstein9a1f8162010-12-01 17:02:26 -08001620 isSputObject = (mir->dalvikInsn.opcode == OP_SPUT_OBJECT) ||
jeffhao71eee1f2011-01-04 14:18:54 -08001621 (mir->dalvikInsn.opcode == OP_SPUT_OBJECT_JUMBO) ||
Dan Bornstein9a1f8162010-12-01 17:02:26 -08001622 (mir->dalvikInsn.opcode == OP_SPUT_OBJECT_VOLATILE);
buzbeed3b0a4b2010-09-27 11:30:22 -07001623
Ben Chengdd6e8702010-05-07 13:05:47 -07001624 if (fieldPtr == NULL) {
1625 LOGE("Unexpected null static field");
1626 dvmAbort();
1627 }
1628
Bill Buzbeec6f10662010-02-09 11:16:15 -08001629 rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07001630 rlSrc = loadValue(cUnit, rlSrc, kAnyReg);
buzbeeb78c76f2010-09-30 19:08:20 -07001631 loadConstant(cUnit, tReg, (int) fieldPtr);
buzbeed3b0a4b2010-09-27 11:30:22 -07001632 if (isSputObject) {
1633 objHead = dvmCompilerAllocTemp(cUnit);
buzbeeb78c76f2010-09-30 19:08:20 -07001634 loadWordDisp(cUnit, tReg, offsetof(Field, clazz), objHead);
buzbeed3b0a4b2010-09-27 11:30:22 -07001635 }
Ben Cheng11d8f142010-03-24 15:24:19 -07001636 HEAP_ACCESS_SHADOW(true);
buzbeeb78c76f2010-09-30 19:08:20 -07001637 storeWordDisp(cUnit, tReg, valOffset ,rlSrc.lowReg);
buzbeed3b0a4b2010-09-27 11:30:22 -07001638 dvmCompilerFreeTemp(cUnit, tReg);
Ben Cheng11d8f142010-03-24 15:24:19 -07001639 HEAP_ACCESS_SHADOW(false);
buzbeeecf8f6e2010-07-20 14:53:42 -07001640 if (isVolatile) {
buzbee2ce33c92010-11-01 15:53:27 -07001641 dvmCompilerGenMemBarrier(cUnit, kSY);
buzbeeecf8f6e2010-07-20 14:53:42 -07001642 }
buzbeed3b0a4b2010-09-27 11:30:22 -07001643 if (isSputObject) {
buzbeeb78c76f2010-09-30 19:08:20 -07001644 /* NOTE: marking card based sfield->clazz */
buzbeed3b0a4b2010-09-27 11:30:22 -07001645 markCard(cUnit, rlSrc.lowReg, objHead);
1646 dvmCompilerFreeTemp(cUnit, objHead);
buzbee919eb062010-07-12 12:59:22 -07001647 }
Ben Cheng11d8f142010-03-24 15:24:19 -07001648
Ben Chengba4fc8b2009-06-01 13:00:29 -07001649 break;
1650 }
jeffhao71eee1f2011-01-04 14:18:54 -08001651 case OP_SPUT_WIDE:
1652 case OP_SPUT_WIDE_JUMBO: {
Bill Buzbeec6f10662010-02-09 11:16:15 -08001653 int tReg = dvmCompilerAllocTemp(cUnit);
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 Chenge9695e52009-06-16 16:11:47 -07001659
Ben Chengdd6e8702010-05-07 13:05:47 -07001660 if (fieldPtr == NULL) {
1661 LOGE("Unexpected null static field");
1662 dvmAbort();
1663 }
1664
Bill Buzbeec6f10662010-02-09 11:16:15 -08001665 rlSrc = dvmCompilerGetSrcWide(cUnit, mir, 0, 1);
Bill Buzbee1465db52009-09-23 17:17:35 -07001666 rlSrc = loadValueWide(cUnit, rlSrc, kAnyReg);
1667 loadConstant(cUnit, tReg, (int) fieldPtr + valOffset);
Ben Cheng11d8f142010-03-24 15:24:19 -07001668
1669 HEAP_ACCESS_SHADOW(true);
Bill Buzbee1465db52009-09-23 17:17:35 -07001670 storePair(cUnit, tReg, rlSrc.lowReg, rlSrc.highReg);
Ben Cheng11d8f142010-03-24 15:24:19 -07001671 HEAP_ACCESS_SHADOW(false);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001672 break;
1673 }
jeffhao71eee1f2011-01-04 14:18:54 -08001674 case OP_NEW_INSTANCE:
1675 case OP_NEW_INSTANCE_JUMBO: {
Ben Chenge9695e52009-06-16 16:11:47 -07001676 /*
1677 * Obey the calling convention and don't mess with the register
1678 * usage.
1679 */
Carl Shapirofc75f3e2010-12-07 11:43:38 -08001680 ClassObject *classPtr = (ClassObject *)
Ben Chengba4fc8b2009-06-01 13:00:29 -07001681 (cUnit->method->clazz->pDvmDex->pResClasses[mir->dalvikInsn.vB]);
Ben Chengdd6e8702010-05-07 13:05:47 -07001682
1683 if (classPtr == NULL) {
1684 LOGE("Unexpected null class");
1685 dvmAbort();
1686 }
1687
Ben Cheng79d173c2009-09-29 16:12:51 -07001688 /*
1689 * If it is going to throw, it should not make to the trace to begin
Bill Buzbee1465db52009-09-23 17:17:35 -07001690 * with. However, Alloc might throw, so we need to genExportPC()
Ben Cheng79d173c2009-09-29 16:12:51 -07001691 */
1692 assert((classPtr->accessFlags & (ACC_INTERFACE|ACC_ABSTRACT)) == 0);
Bill Buzbeec6f10662010-02-09 11:16:15 -08001693 dvmCompilerFlushAllRegs(cUnit); /* Everything to home location */
Bill Buzbee1465db52009-09-23 17:17:35 -07001694 genExportPC(cUnit, mir);
Ben Chengbd1326d2010-04-02 15:04:53 -07001695 LOAD_FUNC_ADDR(cUnit, r2, (int)dvmAllocObject);
Ben Chenge9695e52009-06-16 16:11:47 -07001696 loadConstant(cUnit, r0, (int) classPtr);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001697 loadConstant(cUnit, r1, ALLOC_DONT_TRACK);
Bill Buzbee1465db52009-09-23 17:17:35 -07001698 opReg(cUnit, kOpBlx, r2);
Elliott Hughes6a555132010-02-25 15:41:42 -08001699 dvmCompilerClobberCallRegs(cUnit);
Ben Cheng4f489172009-09-27 17:08:35 -07001700 /* generate a branch over if allocation is successful */
buzbee8f8109a2010-08-31 10:16:35 -07001701 ArmLIR *branchOver = genCmpImmBranch(cUnit, kArmCondNe, r0, 0);
Ben Cheng4f489172009-09-27 17:08:35 -07001702 /*
1703 * OOM exception needs to be thrown here and cannot re-execute
1704 */
1705 loadConstant(cUnit, r0,
1706 (int) (cUnit->method->insns + mir->offset));
1707 genDispatchToHandler(cUnit, TEMPLATE_THROW_EXCEPTION_COMMON);
1708 /* noreturn */
1709
Bill Buzbee1465db52009-09-23 17:17:35 -07001710 ArmLIR *target = newLIR0(cUnit, kArmPseudoTargetLabel);
Ben Cheng4f489172009-09-27 17:08:35 -07001711 target->defMask = ENCODE_ALL;
1712 branchOver->generic.target = (LIR *) target;
Bill Buzbeec6f10662010-02-09 11:16:15 -08001713 rlDest = dvmCompilerGetDest(cUnit, mir, 0);
1714 rlResult = dvmCompilerGetReturn(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07001715 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001716 break;
1717 }
jeffhao71eee1f2011-01-04 14:18:54 -08001718 case OP_CHECK_CAST:
1719 case OP_CHECK_CAST_JUMBO: {
Ben Chenge9695e52009-06-16 16:11:47 -07001720 /*
1721 * Obey the calling convention and don't mess with the register
1722 * usage.
1723 */
Ben Chengba4fc8b2009-06-01 13:00:29 -07001724 ClassObject *classPtr =
1725 (cUnit->method->clazz->pDvmDex->pResClasses[mir->dalvikInsn.vB]);
Bill Buzbee4df41a52009-11-12 17:07:16 -08001726 /*
1727 * Note: It is possible that classPtr is NULL at this point,
1728 * even though this instruction has been successfully interpreted.
1729 * If the previous interpretation had a null source, the
1730 * interpreter would not have bothered to resolve the clazz.
1731 * Bail out to the interpreter in this case, and log it
1732 * so that we can tell if it happens frequently.
1733 */
1734 if (classPtr == NULL) {
Ben Cheng11d8f142010-03-24 15:24:19 -07001735 LOGVV("null clazz in OP_CHECK_CAST, single-stepping");
Bill Buzbee4df41a52009-11-12 17:07:16 -08001736 genInterpSingleStep(cUnit, mir);
1737 return false;
1738 }
Bill Buzbeec6f10662010-02-09 11:16:15 -08001739 dvmCompilerFlushAllRegs(cUnit); /* Everything to home location */
Ben Chengba4fc8b2009-06-01 13:00:29 -07001740 loadConstant(cUnit, r1, (int) classPtr );
Bill Buzbeec6f10662010-02-09 11:16:15 -08001741 rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07001742 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
buzbee8f8109a2010-08-31 10:16:35 -07001743 /* Null? */
1744 ArmLIR *branch1 = genCmpImmBranch(cUnit, kArmCondEq,
1745 rlSrc.lowReg, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07001746 /*
1747 * rlSrc.lowReg now contains object->clazz. Note that
1748 * it could have been allocated r0, but we're okay so long
1749 * as we don't do anything desctructive until r0 is loaded
1750 * with clazz.
1751 */
Ben Chengba4fc8b2009-06-01 13:00:29 -07001752 /* r0 now contains object->clazz */
Bill Buzbee1465db52009-09-23 17:17:35 -07001753 loadWordDisp(cUnit, rlSrc.lowReg, offsetof(Object, clazz), r0);
Ben Chengbd1326d2010-04-02 15:04:53 -07001754 LOAD_FUNC_ADDR(cUnit, r2, (int)dvmInstanceofNonTrivial);
Bill Buzbee1465db52009-09-23 17:17:35 -07001755 opRegReg(cUnit, kOpCmp, r0, r1);
1756 ArmLIR *branch2 = opCondBranch(cUnit, kArmCondEq);
1757 opReg(cUnit, kOpBlx, r2);
Elliott Hughes6a555132010-02-25 15:41:42 -08001758 dvmCompilerClobberCallRegs(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07001759 /*
1760 * If null, check cast failed - punt to the interpreter. Because
1761 * interpreter will be the one throwing, we don't need to
1762 * genExportPC() here.
1763 */
Bill Buzbee270c1d62009-08-13 16:58:07 -07001764 genZeroCheck(cUnit, r0, mir->offset, NULL);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001765 /* check cast passed - branch target here */
Bill Buzbee1465db52009-09-23 17:17:35 -07001766 ArmLIR *target = newLIR0(cUnit, kArmPseudoTargetLabel);
Ben Chengd7d426a2009-09-22 11:23:36 -07001767 target->defMask = ENCODE_ALL;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001768 branch1->generic.target = (LIR *)target;
1769 branch2->generic.target = (LIR *)target;
1770 break;
1771 }
buzbee4d92e682010-07-29 15:24:14 -07001772 case OP_SGET_WIDE_VOLATILE:
1773 case OP_SPUT_WIDE_VOLATILE:
1774 genInterpSingleStep(cUnit, mir);
1775 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001776 default:
1777 return true;
1778 }
1779 return false;
1780}
1781
Ben Cheng7a2697d2010-06-07 13:44:23 -07001782/*
1783 * A typical example of inlined getter/setter from a monomorphic callsite:
1784 *
1785 * D/dalvikvm( 289): -------- dalvik offset: 0x0000 @ invoke-static (I)
1786 * D/dalvikvm( 289): -------- dalvik offset: 0x0000 @ sget-object (C) v0, ...
1787 * D/dalvikvm( 289): 0x4427fc22 (0002): ldr r0, [pc, #56]
1788 * D/dalvikvm( 289): 0x4427fc24 (0004): ldr r1, [r0, #0]
1789 * D/dalvikvm( 289): 0x4427fc26 (0006): str r1, [r5, #0]
1790 * D/dalvikvm( 289): 0x4427fc28 (0008): .align4
1791 * D/dalvikvm( 289): L0x0003:
1792 * D/dalvikvm( 289): -------- dalvik offset: 0x0003 @ move-result-object (I) v0
1793 *
1794 * Note the invoke-static and move-result-object with the (I) notation are
1795 * turned into no-op.
1796 */
Ben Chengba4fc8b2009-06-01 13:00:29 -07001797static bool handleFmt11x(CompilationUnit *cUnit, MIR *mir)
1798{
Dan Bornstein9a1f8162010-12-01 17:02:26 -08001799 Opcode dalvikOpcode = mir->dalvikInsn.opcode;
Bill Buzbee1465db52009-09-23 17:17:35 -07001800 RegLocation rlResult;
Dan Bornstein9a1f8162010-12-01 17:02:26 -08001801 switch (dalvikOpcode) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07001802 case OP_MOVE_EXCEPTION: {
1803 int offset = offsetof(InterpState, self);
1804 int exOffset = offsetof(Thread, exception);
Bill Buzbeec6f10662010-02-09 11:16:15 -08001805 int selfReg = dvmCompilerAllocTemp(cUnit);
1806 int resetReg = dvmCompilerAllocTemp(cUnit);
1807 RegLocation rlDest = dvmCompilerGetDest(cUnit, mir, 0);
1808 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Bill Buzbee1465db52009-09-23 17:17:35 -07001809 loadWordDisp(cUnit, rGLUE, offset, selfReg);
Bill Buzbeef9f33282009-11-22 12:45:30 -08001810 loadConstant(cUnit, resetReg, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07001811 loadWordDisp(cUnit, selfReg, exOffset, rlResult.lowReg);
Bill Buzbeef9f33282009-11-22 12:45:30 -08001812 storeWordDisp(cUnit, selfReg, exOffset, resetReg);
Bill Buzbee1465db52009-09-23 17:17:35 -07001813 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001814 break;
1815 }
1816 case OP_MOVE_RESULT:
1817 case OP_MOVE_RESULT_OBJECT: {
Ben Cheng7a2697d2010-06-07 13:44:23 -07001818 /* An inlined move result is effectively no-op */
1819 if (mir->OptimizationFlags & MIR_INLINED)
1820 break;
Bill Buzbeec6f10662010-02-09 11:16:15 -08001821 RegLocation rlDest = dvmCompilerGetDest(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07001822 RegLocation rlSrc = LOC_DALVIK_RETURN_VAL;
1823 rlSrc.fp = rlDest.fp;
1824 storeValue(cUnit, rlDest, rlSrc);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001825 break;
1826 }
1827 case OP_MOVE_RESULT_WIDE: {
Ben Cheng7a2697d2010-06-07 13:44:23 -07001828 /* An inlined move result is effectively no-op */
1829 if (mir->OptimizationFlags & MIR_INLINED)
1830 break;
Bill Buzbeec6f10662010-02-09 11:16:15 -08001831 RegLocation rlDest = dvmCompilerGetDestWide(cUnit, mir, 0, 1);
Bill Buzbee1465db52009-09-23 17:17:35 -07001832 RegLocation rlSrc = LOC_DALVIK_RETURN_VAL_WIDE;
1833 rlSrc.fp = rlDest.fp;
1834 storeValueWide(cUnit, rlDest, rlSrc);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001835 break;
1836 }
1837 case OP_RETURN_WIDE: {
Bill Buzbeec6f10662010-02-09 11:16:15 -08001838 RegLocation rlSrc = dvmCompilerGetSrcWide(cUnit, mir, 0, 1);
Bill Buzbee1465db52009-09-23 17:17:35 -07001839 RegLocation rlDest = LOC_DALVIK_RETURN_VAL_WIDE;
1840 rlDest.fp = rlSrc.fp;
1841 storeValueWide(cUnit, rlDest, rlSrc);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001842 genReturnCommon(cUnit,mir);
1843 break;
1844 }
1845 case OP_RETURN:
1846 case OP_RETURN_OBJECT: {
Bill Buzbeec6f10662010-02-09 11:16:15 -08001847 RegLocation rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07001848 RegLocation rlDest = LOC_DALVIK_RETURN_VAL;
1849 rlDest.fp = rlSrc.fp;
1850 storeValue(cUnit, rlDest, rlSrc);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001851 genReturnCommon(cUnit,mir);
1852 break;
1853 }
Bill Buzbee1465db52009-09-23 17:17:35 -07001854 case OP_MONITOR_EXIT:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001855 case OP_MONITOR_ENTER:
Ben Cheng5d90c202009-11-22 23:31:11 -08001856 genMonitor(cUnit, mir);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001857 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001858 case OP_THROW: {
1859 genInterpSingleStep(cUnit, mir);
1860 break;
1861 }
1862 default:
1863 return true;
1864 }
1865 return false;
1866}
1867
Bill Buzbeed45ba372009-06-15 17:00:57 -07001868static bool handleFmt12x(CompilationUnit *cUnit, MIR *mir)
1869{
Dan Bornstein9a1f8162010-12-01 17:02:26 -08001870 Opcode opcode = mir->dalvikInsn.opcode;
Bill Buzbee1465db52009-09-23 17:17:35 -07001871 RegLocation rlDest;
1872 RegLocation rlSrc;
1873 RegLocation rlResult;
Bill Buzbeed45ba372009-06-15 17:00:57 -07001874
Dan Bornstein9a1f8162010-12-01 17:02:26 -08001875 if ( (opcode >= OP_ADD_INT_2ADDR) && (opcode <= OP_REM_DOUBLE_2ADDR)) {
Ben Cheng5d90c202009-11-22 23:31:11 -08001876 return genArithOp( cUnit, mir );
Ben Chengba4fc8b2009-06-01 13:00:29 -07001877 }
1878
Bill Buzbee1465db52009-09-23 17:17:35 -07001879 if (mir->ssaRep->numUses == 2)
Bill Buzbeec6f10662010-02-09 11:16:15 -08001880 rlSrc = dvmCompilerGetSrcWide(cUnit, mir, 0, 1);
Bill Buzbee1465db52009-09-23 17:17:35 -07001881 else
Bill Buzbeec6f10662010-02-09 11:16:15 -08001882 rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07001883 if (mir->ssaRep->numDefs == 2)
Bill Buzbeec6f10662010-02-09 11:16:15 -08001884 rlDest = dvmCompilerGetDestWide(cUnit, mir, 0, 1);
Bill Buzbee1465db52009-09-23 17:17:35 -07001885 else
Bill Buzbeec6f10662010-02-09 11:16:15 -08001886 rlDest = dvmCompilerGetDest(cUnit, mir, 0);
Ben Chenge9695e52009-06-16 16:11:47 -07001887
Dan Bornstein9a1f8162010-12-01 17:02:26 -08001888 switch (opcode) {
Bill Buzbee1465db52009-09-23 17:17:35 -07001889 case OP_DOUBLE_TO_INT:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001890 case OP_INT_TO_FLOAT:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001891 case OP_FLOAT_TO_INT:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001892 case OP_DOUBLE_TO_FLOAT:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001893 case OP_FLOAT_TO_DOUBLE:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001894 case OP_INT_TO_DOUBLE:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001895 case OP_FLOAT_TO_LONG:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001896 case OP_LONG_TO_FLOAT:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001897 case OP_DOUBLE_TO_LONG:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001898 case OP_LONG_TO_DOUBLE:
Ben Cheng5d90c202009-11-22 23:31:11 -08001899 return genConversion(cUnit, mir);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001900 case OP_NEG_INT:
1901 case OP_NOT_INT:
Ben Cheng5d90c202009-11-22 23:31:11 -08001902 return genArithOpInt(cUnit, mir, rlDest, rlSrc, rlSrc);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001903 case OP_NEG_LONG:
1904 case OP_NOT_LONG:
Ben Cheng5d90c202009-11-22 23:31:11 -08001905 return genArithOpLong(cUnit, mir, rlDest, rlSrc, rlSrc);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001906 case OP_NEG_FLOAT:
Ben Cheng5d90c202009-11-22 23:31:11 -08001907 return genArithOpFloat(cUnit, mir, rlDest, rlSrc, rlSrc);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001908 case OP_NEG_DOUBLE:
Ben Cheng5d90c202009-11-22 23:31:11 -08001909 return genArithOpDouble(cUnit, mir, rlDest, rlSrc, rlSrc);
Bill Buzbee1465db52009-09-23 17:17:35 -07001910 case OP_MOVE_WIDE:
1911 storeValueWide(cUnit, rlDest, rlSrc);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001912 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07001913 case OP_INT_TO_LONG:
Bill Buzbeec6f10662010-02-09 11:16:15 -08001914 rlSrc = dvmCompilerUpdateLoc(cUnit, rlSrc);
1915 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Bill Buzbee964a7b02010-01-28 12:54:19 -08001916 //TUNING: shouldn't loadValueDirect already check for phys reg?
Bill Buzbee1465db52009-09-23 17:17:35 -07001917 if (rlSrc.location == kLocPhysReg) {
1918 genRegCopy(cUnit, rlResult.lowReg, rlSrc.lowReg);
1919 } else {
1920 loadValueDirect(cUnit, rlSrc, rlResult.lowReg);
1921 }
1922 opRegRegImm(cUnit, kOpAsr, rlResult.highReg,
1923 rlResult.lowReg, 31);
1924 storeValueWide(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001925 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07001926 case OP_LONG_TO_INT:
Bill Buzbeec6f10662010-02-09 11:16:15 -08001927 rlSrc = dvmCompilerUpdateLocWide(cUnit, rlSrc);
1928 rlSrc = dvmCompilerWideToNarrow(cUnit, rlSrc);
Bill Buzbee1465db52009-09-23 17:17:35 -07001929 // Intentional fallthrough
Ben Chengba4fc8b2009-06-01 13:00:29 -07001930 case OP_MOVE:
1931 case OP_MOVE_OBJECT:
Bill Buzbee1465db52009-09-23 17:17:35 -07001932 storeValue(cUnit, rlDest, rlSrc);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001933 break;
1934 case OP_INT_TO_BYTE:
Bill Buzbee1465db52009-09-23 17:17:35 -07001935 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
Bill Buzbeec6f10662010-02-09 11:16:15 -08001936 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Bill Buzbee1465db52009-09-23 17:17:35 -07001937 opRegReg(cUnit, kOp2Byte, rlResult.lowReg, rlSrc.lowReg);
1938 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001939 break;
1940 case OP_INT_TO_SHORT:
Bill Buzbee1465db52009-09-23 17:17:35 -07001941 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
Bill Buzbeec6f10662010-02-09 11:16:15 -08001942 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Bill Buzbee1465db52009-09-23 17:17:35 -07001943 opRegReg(cUnit, kOp2Short, rlResult.lowReg, rlSrc.lowReg);
1944 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001945 break;
1946 case OP_INT_TO_CHAR:
Bill Buzbee1465db52009-09-23 17:17:35 -07001947 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
Bill Buzbeec6f10662010-02-09 11:16:15 -08001948 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Bill Buzbee1465db52009-09-23 17:17:35 -07001949 opRegReg(cUnit, kOp2Char, rlResult.lowReg, rlSrc.lowReg);
1950 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001951 break;
1952 case OP_ARRAY_LENGTH: {
1953 int lenOffset = offsetof(ArrayObject, length);
Bill Buzbee1465db52009-09-23 17:17:35 -07001954 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
1955 genNullCheck(cUnit, rlSrc.sRegLow, rlSrc.lowReg,
1956 mir->offset, NULL);
Bill Buzbeec6f10662010-02-09 11:16:15 -08001957 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Bill Buzbee1465db52009-09-23 17:17:35 -07001958 loadWordDisp(cUnit, rlSrc.lowReg, lenOffset,
1959 rlResult.lowReg);
1960 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001961 break;
1962 }
1963 default:
1964 return true;
1965 }
1966 return false;
1967}
1968
1969static bool handleFmt21s(CompilationUnit *cUnit, MIR *mir)
1970{
Dan Bornstein9a1f8162010-12-01 17:02:26 -08001971 Opcode dalvikOpcode = mir->dalvikInsn.opcode;
Bill Buzbee1465db52009-09-23 17:17:35 -07001972 RegLocation rlDest;
1973 RegLocation rlResult;
1974 int BBBB = mir->dalvikInsn.vB;
Dan Bornstein9a1f8162010-12-01 17:02:26 -08001975 if (dalvikOpcode == OP_CONST_WIDE_16) {
Bill Buzbeec6f10662010-02-09 11:16:15 -08001976 rlDest = dvmCompilerGetDestWide(cUnit, mir, 0, 1);
1977 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Ben Chengbd1326d2010-04-02 15:04:53 -07001978 loadConstantNoClobber(cUnit, rlResult.lowReg, BBBB);
Bill Buzbee964a7b02010-01-28 12:54:19 -08001979 //TUNING: do high separately to avoid load dependency
Bill Buzbee1465db52009-09-23 17:17:35 -07001980 opRegRegImm(cUnit, kOpAsr, rlResult.highReg, rlResult.lowReg, 31);
1981 storeValueWide(cUnit, rlDest, rlResult);
Dan Bornstein9a1f8162010-12-01 17:02:26 -08001982 } else if (dalvikOpcode == OP_CONST_16) {
Bill Buzbeec6f10662010-02-09 11:16:15 -08001983 rlDest = dvmCompilerGetDest(cUnit, mir, 0);
1984 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kAnyReg, true);
Ben Chengbd1326d2010-04-02 15:04:53 -07001985 loadConstantNoClobber(cUnit, rlResult.lowReg, BBBB);
Bill Buzbee1465db52009-09-23 17:17:35 -07001986 storeValue(cUnit, rlDest, rlResult);
1987 } else
Ben Chengba4fc8b2009-06-01 13:00:29 -07001988 return true;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001989 return false;
1990}
1991
1992/* Compare agaist zero */
1993static bool handleFmt21t(CompilationUnit *cUnit, MIR *mir, BasicBlock *bb,
Bill Buzbee89efc3d2009-07-28 11:22:22 -07001994 ArmLIR *labelList)
Ben Chengba4fc8b2009-06-01 13:00:29 -07001995{
Dan Bornstein9a1f8162010-12-01 17:02:26 -08001996 Opcode dalvikOpcode = mir->dalvikInsn.opcode;
Bill Buzbee89efc3d2009-07-28 11:22:22 -07001997 ArmConditionCode cond;
Bill Buzbeec6f10662010-02-09 11:16:15 -08001998 RegLocation rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07001999 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
2000 opRegImm(cUnit, kOpCmp, rlSrc.lowReg, 0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002001
Bill Buzbee270c1d62009-08-13 16:58:07 -07002002//TUNING: break this out to allow use of Thumb2 CB[N]Z
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002003 switch (dalvikOpcode) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07002004 case OP_IF_EQZ:
Bill Buzbee1465db52009-09-23 17:17:35 -07002005 cond = kArmCondEq;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002006 break;
2007 case OP_IF_NEZ:
Bill Buzbee1465db52009-09-23 17:17:35 -07002008 cond = kArmCondNe;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002009 break;
2010 case OP_IF_LTZ:
Bill Buzbee1465db52009-09-23 17:17:35 -07002011 cond = kArmCondLt;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002012 break;
2013 case OP_IF_GEZ:
Bill Buzbee1465db52009-09-23 17:17:35 -07002014 cond = kArmCondGe;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002015 break;
2016 case OP_IF_GTZ:
Bill Buzbee1465db52009-09-23 17:17:35 -07002017 cond = kArmCondGt;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002018 break;
2019 case OP_IF_LEZ:
Bill Buzbee1465db52009-09-23 17:17:35 -07002020 cond = kArmCondLe;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002021 break;
2022 default:
2023 cond = 0;
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002024 LOGE("Unexpected opcode (%d) for Fmt21t\n", dalvikOpcode);
Bill Buzbeefc519dc2010-03-06 23:30:57 -08002025 dvmCompilerAbort(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002026 }
2027 genConditionalBranch(cUnit, cond, &labelList[bb->taken->id]);
2028 /* This mostly likely will be optimized away in a later phase */
2029 genUnconditionalBranch(cUnit, &labelList[bb->fallThrough->id]);
2030 return false;
2031}
2032
Elliott Hughesb4c05972010-02-24 16:36:18 -08002033static bool isPowerOfTwo(int x)
2034{
2035 return (x & (x - 1)) == 0;
2036}
2037
2038// Returns true if no more than two bits are set in 'x'.
2039static bool isPopCountLE2(unsigned int x)
2040{
2041 x &= x - 1;
2042 return (x & (x - 1)) == 0;
2043}
2044
2045// Returns the index of the lowest set bit in 'x'.
2046static int lowestSetBit(unsigned int x) {
2047 int bit_posn = 0;
2048 while ((x & 0xf) == 0) {
2049 bit_posn += 4;
2050 x >>= 4;
Bill Buzbee78cb0e22010-02-11 14:04:53 -08002051 }
Elliott Hughesb4c05972010-02-24 16:36:18 -08002052 while ((x & 1) == 0) {
2053 bit_posn++;
2054 x >>= 1;
2055 }
2056 return bit_posn;
2057}
2058
Elliott Hughes672511b2010-04-26 17:40:13 -07002059// Returns true if it added instructions to 'cUnit' to divide 'rlSrc' by 'lit'
2060// and store the result in 'rlDest'.
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002061static bool handleEasyDivide(CompilationUnit *cUnit, Opcode dalvikOpcode,
Elliott Hughes672511b2010-04-26 17:40:13 -07002062 RegLocation rlSrc, RegLocation rlDest, int lit)
2063{
2064 if (lit < 2 || !isPowerOfTwo(lit)) {
2065 return false;
2066 }
2067 int k = lowestSetBit(lit);
2068 if (k >= 30) {
2069 // Avoid special cases.
2070 return false;
2071 }
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002072 bool div = (dalvikOpcode == OP_DIV_INT_LIT8 || dalvikOpcode == OP_DIV_INT_LIT16);
Elliott Hughes672511b2010-04-26 17:40:13 -07002073 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
2074 RegLocation rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Elliott Hughes9c457022010-04-28 16:15:38 -07002075 if (div) {
2076 int tReg = dvmCompilerAllocTemp(cUnit);
2077 if (lit == 2) {
2078 // Division by 2 is by far the most common division by constant.
2079 opRegRegImm(cUnit, kOpLsr, tReg, rlSrc.lowReg, 32 - k);
2080 opRegRegReg(cUnit, kOpAdd, tReg, tReg, rlSrc.lowReg);
2081 opRegRegImm(cUnit, kOpAsr, rlResult.lowReg, tReg, k);
2082 } else {
2083 opRegRegImm(cUnit, kOpAsr, tReg, rlSrc.lowReg, 31);
2084 opRegRegImm(cUnit, kOpLsr, tReg, tReg, 32 - k);
2085 opRegRegReg(cUnit, kOpAdd, tReg, tReg, rlSrc.lowReg);
2086 opRegRegImm(cUnit, kOpAsr, rlResult.lowReg, tReg, k);
2087 }
Elliott Hughes672511b2010-04-26 17:40:13 -07002088 } else {
Elliott Hughes9c457022010-04-28 16:15:38 -07002089 int cReg = dvmCompilerAllocTemp(cUnit);
2090 loadConstant(cUnit, cReg, lit - 1);
2091 int tReg1 = dvmCompilerAllocTemp(cUnit);
2092 int tReg2 = dvmCompilerAllocTemp(cUnit);
2093 if (lit == 2) {
2094 opRegRegImm(cUnit, kOpLsr, tReg1, rlSrc.lowReg, 32 - k);
2095 opRegRegReg(cUnit, kOpAdd, tReg2, tReg1, rlSrc.lowReg);
2096 opRegRegReg(cUnit, kOpAnd, tReg2, tReg2, cReg);
2097 opRegRegReg(cUnit, kOpSub, rlResult.lowReg, tReg2, tReg1);
2098 } else {
2099 opRegRegImm(cUnit, kOpAsr, tReg1, rlSrc.lowReg, 31);
2100 opRegRegImm(cUnit, kOpLsr, tReg1, tReg1, 32 - k);
2101 opRegRegReg(cUnit, kOpAdd, tReg2, tReg1, rlSrc.lowReg);
2102 opRegRegReg(cUnit, kOpAnd, tReg2, tReg2, cReg);
2103 opRegRegReg(cUnit, kOpSub, rlResult.lowReg, tReg2, tReg1);
2104 }
Elliott Hughes672511b2010-04-26 17:40:13 -07002105 }
2106 storeValue(cUnit, rlDest, rlResult);
2107 return true;
2108}
2109
Elliott Hughesb4c05972010-02-24 16:36:18 -08002110// Returns true if it added instructions to 'cUnit' to multiply 'rlSrc' by 'lit'
2111// and store the result in 'rlDest'.
2112static bool handleEasyMultiply(CompilationUnit *cUnit,
2113 RegLocation rlSrc, RegLocation rlDest, int lit)
2114{
2115 // Can we simplify this multiplication?
2116 bool powerOfTwo = false;
2117 bool popCountLE2 = false;
2118 bool powerOfTwoMinusOne = false;
2119 if (lit < 2) {
2120 // Avoid special cases.
2121 return false;
2122 } else if (isPowerOfTwo(lit)) {
2123 powerOfTwo = true;
2124 } else if (isPopCountLE2(lit)) {
2125 popCountLE2 = true;
2126 } else if (isPowerOfTwo(lit + 1)) {
2127 powerOfTwoMinusOne = true;
2128 } else {
2129 return false;
2130 }
2131 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
2132 RegLocation rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
2133 if (powerOfTwo) {
2134 // Shift.
2135 opRegRegImm(cUnit, kOpLsl, rlResult.lowReg, rlSrc.lowReg,
2136 lowestSetBit(lit));
2137 } else if (popCountLE2) {
2138 // Shift and add and shift.
2139 int firstBit = lowestSetBit(lit);
2140 int secondBit = lowestSetBit(lit ^ (1 << firstBit));
2141 genMultiplyByTwoBitMultiplier(cUnit, rlSrc, rlResult, lit,
2142 firstBit, secondBit);
2143 } else {
2144 // Reverse subtract: (src << (shift + 1)) - src.
2145 assert(powerOfTwoMinusOne);
2146 // TODO: rsb dst, src, src lsl#lowestSetBit(lit + 1)
2147 int tReg = dvmCompilerAllocTemp(cUnit);
2148 opRegRegImm(cUnit, kOpLsl, tReg, rlSrc.lowReg, lowestSetBit(lit + 1));
2149 opRegRegReg(cUnit, kOpSub, rlResult.lowReg, tReg, rlSrc.lowReg);
2150 }
2151 storeValue(cUnit, rlDest, rlResult);
2152 return true;
Bill Buzbee78cb0e22010-02-11 14:04:53 -08002153}
2154
Ben Chengba4fc8b2009-06-01 13:00:29 -07002155static bool handleFmt22b_Fmt22s(CompilationUnit *cUnit, MIR *mir)
2156{
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002157 Opcode dalvikOpcode = mir->dalvikInsn.opcode;
Bill Buzbeec6f10662010-02-09 11:16:15 -08002158 RegLocation rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
2159 RegLocation rlDest = dvmCompilerGetDest(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07002160 RegLocation rlResult;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002161 int lit = mir->dalvikInsn.vC;
Ben Cheng4f489172009-09-27 17:08:35 -07002162 OpKind op = 0; /* Make gcc happy */
Bill Buzbee1465db52009-09-23 17:17:35 -07002163 int shiftOp = false;
2164 bool isDiv = false;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002165
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002166 switch (dalvikOpcode) {
Bill Buzbee1465db52009-09-23 17:17:35 -07002167 case OP_RSUB_INT_LIT8:
2168 case OP_RSUB_INT: {
2169 int tReg;
2170 //TUNING: add support for use of Arm rsub op
2171 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
Bill Buzbeec6f10662010-02-09 11:16:15 -08002172 tReg = dvmCompilerAllocTemp(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07002173 loadConstant(cUnit, tReg, lit);
Bill Buzbeec6f10662010-02-09 11:16:15 -08002174 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Bill Buzbee1465db52009-09-23 17:17:35 -07002175 opRegRegReg(cUnit, kOpSub, rlResult.lowReg,
2176 tReg, rlSrc.lowReg);
2177 storeValue(cUnit, rlDest, rlResult);
2178 return false;
2179 break;
2180 }
2181
Ben Chengba4fc8b2009-06-01 13:00:29 -07002182 case OP_ADD_INT_LIT8:
2183 case OP_ADD_INT_LIT16:
Bill Buzbee1465db52009-09-23 17:17:35 -07002184 op = kOpAdd;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002185 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002186 case OP_MUL_INT_LIT8:
Bill Buzbee78cb0e22010-02-11 14:04:53 -08002187 case OP_MUL_INT_LIT16: {
Elliott Hughesb4c05972010-02-24 16:36:18 -08002188 if (handleEasyMultiply(cUnit, rlSrc, rlDest, lit)) {
2189 return false;
Bill Buzbee78cb0e22010-02-11 14:04:53 -08002190 }
Elliott Hughesb4c05972010-02-24 16:36:18 -08002191 op = kOpMul;
Bill Buzbee1465db52009-09-23 17:17:35 -07002192 break;
Bill Buzbee78cb0e22010-02-11 14:04:53 -08002193 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07002194 case OP_AND_INT_LIT8:
2195 case OP_AND_INT_LIT16:
Bill Buzbee1465db52009-09-23 17:17:35 -07002196 op = kOpAnd;
2197 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002198 case OP_OR_INT_LIT8:
2199 case OP_OR_INT_LIT16:
Bill Buzbee1465db52009-09-23 17:17:35 -07002200 op = kOpOr;
2201 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002202 case OP_XOR_INT_LIT8:
2203 case OP_XOR_INT_LIT16:
Bill Buzbee1465db52009-09-23 17:17:35 -07002204 op = kOpXor;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002205 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002206 case OP_SHL_INT_LIT8:
Bill Buzbee0e605272009-12-01 14:28:05 -08002207 lit &= 31;
Bill Buzbee1465db52009-09-23 17:17:35 -07002208 shiftOp = true;
2209 op = kOpLsl;
2210 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002211 case OP_SHR_INT_LIT8:
Bill Buzbee0e605272009-12-01 14:28:05 -08002212 lit &= 31;
Bill Buzbee1465db52009-09-23 17:17:35 -07002213 shiftOp = true;
2214 op = kOpAsr;
2215 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002216 case OP_USHR_INT_LIT8:
Bill Buzbee0e605272009-12-01 14:28:05 -08002217 lit &= 31;
Bill Buzbee1465db52009-09-23 17:17:35 -07002218 shiftOp = true;
2219 op = kOpLsr;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002220 break;
2221
2222 case OP_DIV_INT_LIT8:
2223 case OP_DIV_INT_LIT16:
Ben Chengba4fc8b2009-06-01 13:00:29 -07002224 case OP_REM_INT_LIT8:
2225 case OP_REM_INT_LIT16:
2226 if (lit == 0) {
2227 /* Let the interpreter deal with div by 0 */
2228 genInterpSingleStep(cUnit, mir);
2229 return false;
2230 }
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002231 if (handleEasyDivide(cUnit, dalvikOpcode, rlSrc, rlDest, lit)) {
Elliott Hughes672511b2010-04-26 17:40:13 -07002232 return false;
2233 }
Bill Buzbeec6f10662010-02-09 11:16:15 -08002234 dvmCompilerFlushAllRegs(cUnit); /* Everything to home location */
Bill Buzbee1465db52009-09-23 17:17:35 -07002235 loadValueDirectFixed(cUnit, rlSrc, r0);
Bill Buzbeec6f10662010-02-09 11:16:15 -08002236 dvmCompilerClobber(cUnit, r0);
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002237 if ((dalvikOpcode == OP_DIV_INT_LIT8) ||
2238 (dalvikOpcode == OP_DIV_INT_LIT16)) {
Ben Chengbd1326d2010-04-02 15:04:53 -07002239 LOAD_FUNC_ADDR(cUnit, r2, (int)__aeabi_idiv);
Bill Buzbee1465db52009-09-23 17:17:35 -07002240 isDiv = true;
2241 } else {
Ben Chengbd1326d2010-04-02 15:04:53 -07002242 LOAD_FUNC_ADDR(cUnit, r2, (int)__aeabi_idivmod);
Bill Buzbee1465db52009-09-23 17:17:35 -07002243 isDiv = false;
2244 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07002245 loadConstant(cUnit, r1, lit);
Bill Buzbee1465db52009-09-23 17:17:35 -07002246 opReg(cUnit, kOpBlx, r2);
Elliott Hughes6a555132010-02-25 15:41:42 -08002247 dvmCompilerClobberCallRegs(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07002248 if (isDiv)
Bill Buzbeec6f10662010-02-09 11:16:15 -08002249 rlResult = dvmCompilerGetReturn(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07002250 else
Bill Buzbeec6f10662010-02-09 11:16:15 -08002251 rlResult = dvmCompilerGetReturnAlt(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07002252 storeValue(cUnit, rlDest, rlResult);
2253 return false;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002254 break;
2255 default:
2256 return true;
2257 }
Bill Buzbee1465db52009-09-23 17:17:35 -07002258 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
Bill Buzbeec6f10662010-02-09 11:16:15 -08002259 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Bill Buzbee1465db52009-09-23 17:17:35 -07002260 // Avoid shifts by literal 0 - no support in Thumb. Change to copy
2261 if (shiftOp && (lit == 0)) {
2262 genRegCopy(cUnit, rlResult.lowReg, rlSrc.lowReg);
2263 } else {
2264 opRegRegImm(cUnit, op, rlResult.lowReg, rlSrc.lowReg, lit);
2265 }
2266 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002267 return false;
2268}
2269
jeffhao71eee1f2011-01-04 14:18:54 -08002270static bool handleFmt22c_Fmt52c(CompilationUnit *cUnit, MIR *mir)
Ben Chengba4fc8b2009-06-01 13:00:29 -07002271{
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002272 Opcode dalvikOpcode = mir->dalvikInsn.opcode;
buzbee4d92e682010-07-29 15:24:14 -07002273 int fieldOffset = -1;
buzbeeecf8f6e2010-07-20 14:53:42 -07002274 bool isVolatile = false;
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002275 switch (dalvikOpcode) {
buzbee4d92e682010-07-29 15:24:14 -07002276 /*
2277 * Wide volatiles currently handled via single step.
2278 * Add them here if generating in-line code.
2279 * case OP_IGET_WIDE_VOLATILE:
2280 * case OP_IPUT_WIDE_VOLATILE:
2281 */
2282 case OP_IGET:
2283 case OP_IGET_VOLATILE:
jeffhao71eee1f2011-01-04 14:18:54 -08002284 case OP_IGET_JUMBO:
buzbee4d92e682010-07-29 15:24:14 -07002285 case OP_IGET_WIDE:
jeffhao71eee1f2011-01-04 14:18:54 -08002286 case OP_IGET_WIDE_JUMBO:
buzbee4d92e682010-07-29 15:24:14 -07002287 case OP_IGET_OBJECT:
2288 case OP_IGET_OBJECT_VOLATILE:
jeffhao71eee1f2011-01-04 14:18:54 -08002289 case OP_IGET_OBJECT_JUMBO:
buzbee4d92e682010-07-29 15:24:14 -07002290 case OP_IGET_BOOLEAN:
jeffhao71eee1f2011-01-04 14:18:54 -08002291 case OP_IGET_BOOLEAN_JUMBO:
buzbee4d92e682010-07-29 15:24:14 -07002292 case OP_IGET_BYTE:
jeffhao71eee1f2011-01-04 14:18:54 -08002293 case OP_IGET_BYTE_JUMBO:
buzbee4d92e682010-07-29 15:24:14 -07002294 case OP_IGET_CHAR:
jeffhao71eee1f2011-01-04 14:18:54 -08002295 case OP_IGET_CHAR_JUMBO:
buzbee4d92e682010-07-29 15:24:14 -07002296 case OP_IGET_SHORT:
jeffhao71eee1f2011-01-04 14:18:54 -08002297 case OP_IGET_SHORT_JUMBO:
buzbee4d92e682010-07-29 15:24:14 -07002298 case OP_IPUT:
2299 case OP_IPUT_VOLATILE:
jeffhao71eee1f2011-01-04 14:18:54 -08002300 case OP_IPUT_JUMBO:
buzbee4d92e682010-07-29 15:24:14 -07002301 case OP_IPUT_WIDE:
jeffhao71eee1f2011-01-04 14:18:54 -08002302 case OP_IPUT_WIDE_JUMBO:
buzbee4d92e682010-07-29 15:24:14 -07002303 case OP_IPUT_OBJECT:
2304 case OP_IPUT_OBJECT_VOLATILE:
jeffhao71eee1f2011-01-04 14:18:54 -08002305 case OP_IPUT_OBJECT_JUMBO:
buzbee4d92e682010-07-29 15:24:14 -07002306 case OP_IPUT_BOOLEAN:
jeffhao71eee1f2011-01-04 14:18:54 -08002307 case OP_IPUT_BOOLEAN_JUMBO:
buzbee4d92e682010-07-29 15:24:14 -07002308 case OP_IPUT_BYTE:
jeffhao71eee1f2011-01-04 14:18:54 -08002309 case OP_IPUT_BYTE_JUMBO:
buzbee4d92e682010-07-29 15:24:14 -07002310 case OP_IPUT_CHAR:
jeffhao71eee1f2011-01-04 14:18:54 -08002311 case OP_IPUT_CHAR_JUMBO:
2312 case OP_IPUT_SHORT:
2313 case OP_IPUT_SHORT_JUMBO: {
Ben Cheng7a2697d2010-06-07 13:44:23 -07002314 const Method *method = (mir->OptimizationFlags & MIR_CALLEE) ?
2315 mir->meta.calleeMethod : cUnit->method;
buzbee4d92e682010-07-29 15:24:14 -07002316 Field *fieldPtr =
Ben Cheng7a2697d2010-06-07 13:44:23 -07002317 method->clazz->pDvmDex->pResFields[mir->dalvikInsn.vC];
Ben Chengba4fc8b2009-06-01 13:00:29 -07002318
buzbee4d92e682010-07-29 15:24:14 -07002319 if (fieldPtr == NULL) {
2320 LOGE("Unexpected null instance field");
2321 dvmAbort();
2322 }
2323 isVolatile = dvmIsVolatileField(fieldPtr);
2324 fieldOffset = ((InstField *)fieldPtr)->byteOffset;
2325 break;
Ben Chengdd6e8702010-05-07 13:05:47 -07002326 }
buzbee4d92e682010-07-29 15:24:14 -07002327 default:
2328 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002329 }
buzbee4d92e682010-07-29 15:24:14 -07002330
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002331 switch (dalvikOpcode) {
jeffhao71eee1f2011-01-04 14:18:54 -08002332 case OP_NEW_ARRAY:
2333 case OP_NEW_ARRAY_JUMBO: {
Bill Buzbee1465db52009-09-23 17:17:35 -07002334 // Generates a call - use explicit registers
Bill Buzbeec6f10662010-02-09 11:16:15 -08002335 RegLocation rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
2336 RegLocation rlDest = dvmCompilerGetDest(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07002337 RegLocation rlResult;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002338 void *classPtr = (void*)
2339 (cUnit->method->clazz->pDvmDex->pResClasses[mir->dalvikInsn.vC]);
Ben Chengdd6e8702010-05-07 13:05:47 -07002340
2341 if (classPtr == NULL) {
2342 LOGE("Unexpected null class");
2343 dvmAbort();
2344 }
2345
Bill Buzbeec6f10662010-02-09 11:16:15 -08002346 dvmCompilerFlushAllRegs(cUnit); /* Everything to home location */
Bill Buzbee1465db52009-09-23 17:17:35 -07002347 genExportPC(cUnit, mir);
2348 loadValueDirectFixed(cUnit, rlSrc, r1); /* Len */
Ben Chengba4fc8b2009-06-01 13:00:29 -07002349 loadConstant(cUnit, r0, (int) classPtr );
Ben Chengbd1326d2010-04-02 15:04:53 -07002350 LOAD_FUNC_ADDR(cUnit, r3, (int)dvmAllocArrayByClass);
Ben Cheng4f489172009-09-27 17:08:35 -07002351 /*
2352 * "len < 0": bail to the interpreter to re-execute the
2353 * instruction
2354 */
Carl Shapiroe3c01da2010-05-20 22:54:18 -07002355 genRegImmCheck(cUnit, kArmCondMi, r1, 0, mir->offset, NULL);
Bill Buzbee270c1d62009-08-13 16:58:07 -07002356 loadConstant(cUnit, r2, ALLOC_DONT_TRACK);
Bill Buzbee1465db52009-09-23 17:17:35 -07002357 opReg(cUnit, kOpBlx, r3);
Elliott Hughes6a555132010-02-25 15:41:42 -08002358 dvmCompilerClobberCallRegs(cUnit);
Ben Cheng4f489172009-09-27 17:08:35 -07002359 /* generate a branch over if allocation is successful */
buzbee8f8109a2010-08-31 10:16:35 -07002360 ArmLIR *branchOver = genCmpImmBranch(cUnit, kArmCondNe, r0, 0);
Ben Cheng4f489172009-09-27 17:08:35 -07002361 /*
2362 * OOM exception needs to be thrown here and cannot re-execute
2363 */
2364 loadConstant(cUnit, r0,
2365 (int) (cUnit->method->insns + mir->offset));
2366 genDispatchToHandler(cUnit, TEMPLATE_THROW_EXCEPTION_COMMON);
2367 /* noreturn */
2368
Bill Buzbee1465db52009-09-23 17:17:35 -07002369 ArmLIR *target = newLIR0(cUnit, kArmPseudoTargetLabel);
Ben Cheng4f489172009-09-27 17:08:35 -07002370 target->defMask = ENCODE_ALL;
2371 branchOver->generic.target = (LIR *) target;
Bill Buzbeec6f10662010-02-09 11:16:15 -08002372 rlResult = dvmCompilerGetReturn(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07002373 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002374 break;
2375 }
jeffhao71eee1f2011-01-04 14:18:54 -08002376 case OP_INSTANCE_OF:
2377 case OP_INSTANCE_OF_JUMBO: {
Bill Buzbee1465db52009-09-23 17:17:35 -07002378 // May generate a call - use explicit registers
Bill Buzbeec6f10662010-02-09 11:16:15 -08002379 RegLocation rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
2380 RegLocation rlDest = dvmCompilerGetDest(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07002381 RegLocation rlResult;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002382 ClassObject *classPtr =
2383 (cUnit->method->clazz->pDvmDex->pResClasses[mir->dalvikInsn.vC]);
Bill Buzbee480e6782010-01-27 15:43:08 -08002384 /*
2385 * Note: It is possible that classPtr is NULL at this point,
2386 * even though this instruction has been successfully interpreted.
2387 * If the previous interpretation had a null source, the
2388 * interpreter would not have bothered to resolve the clazz.
2389 * Bail out to the interpreter in this case, and log it
2390 * so that we can tell if it happens frequently.
2391 */
2392 if (classPtr == NULL) {
2393 LOGD("null clazz in OP_INSTANCE_OF, single-stepping");
2394 genInterpSingleStep(cUnit, mir);
2395 break;
2396 }
Bill Buzbeec6f10662010-02-09 11:16:15 -08002397 dvmCompilerFlushAllRegs(cUnit); /* Everything to home location */
Bill Buzbee1465db52009-09-23 17:17:35 -07002398 loadValueDirectFixed(cUnit, rlSrc, r0); /* Ref */
Ben Chengba4fc8b2009-06-01 13:00:29 -07002399 loadConstant(cUnit, r2, (int) classPtr );
Ben Cheng752c7942009-06-22 10:50:07 -07002400 /* When taken r0 has NULL which can be used for store directly */
buzbee8f8109a2010-08-31 10:16:35 -07002401 ArmLIR *branch1 = genCmpImmBranch(cUnit, kArmCondEq, r0, 0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002402 /* r1 now contains object->clazz */
Bill Buzbee270c1d62009-08-13 16:58:07 -07002403 loadWordDisp(cUnit, r0, offsetof(Object, clazz), r1);
Bill Buzbee1465db52009-09-23 17:17:35 -07002404 /* r1 now contains object->clazz */
Ben Chengbd1326d2010-04-02 15:04:53 -07002405 LOAD_FUNC_ADDR(cUnit, r3, (int)dvmInstanceofNonTrivial);
Ben Cheng752c7942009-06-22 10:50:07 -07002406 loadConstant(cUnit, r0, 1); /* Assume true */
Bill Buzbee1465db52009-09-23 17:17:35 -07002407 opRegReg(cUnit, kOpCmp, r1, r2);
2408 ArmLIR *branch2 = opCondBranch(cUnit, kArmCondEq);
2409 genRegCopy(cUnit, r0, r1);
2410 genRegCopy(cUnit, r1, r2);
2411 opReg(cUnit, kOpBlx, r3);
Elliott Hughes6a555132010-02-25 15:41:42 -08002412 dvmCompilerClobberCallRegs(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002413 /* branch target here */
Bill Buzbee1465db52009-09-23 17:17:35 -07002414 ArmLIR *target = newLIR0(cUnit, kArmPseudoTargetLabel);
Ben Chengd7d426a2009-09-22 11:23:36 -07002415 target->defMask = ENCODE_ALL;
Bill Buzbeec6f10662010-02-09 11:16:15 -08002416 rlResult = dvmCompilerGetReturn(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07002417 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002418 branch1->generic.target = (LIR *)target;
2419 branch2->generic.target = (LIR *)target;
2420 break;
2421 }
2422 case OP_IGET_WIDE:
jeffhao71eee1f2011-01-04 14:18:54 -08002423 case OP_IGET_WIDE_JUMBO:
Ben Chengba4fc8b2009-06-01 13:00:29 -07002424 genIGetWide(cUnit, mir, fieldOffset);
2425 break;
buzbeeecf8f6e2010-07-20 14:53:42 -07002426 case OP_IGET_VOLATILE:
2427 case OP_IGET_OBJECT_VOLATILE:
2428 isVolatile = true;
2429 // NOTE: intentional fallthrough
Ben Chengba4fc8b2009-06-01 13:00:29 -07002430 case OP_IGET:
jeffhao71eee1f2011-01-04 14:18:54 -08002431 case OP_IGET_JUMBO:
Ben Chengba4fc8b2009-06-01 13:00:29 -07002432 case OP_IGET_OBJECT:
jeffhao71eee1f2011-01-04 14:18:54 -08002433 case OP_IGET_OBJECT_JUMBO:
Ben Chengba4fc8b2009-06-01 13:00:29 -07002434 case OP_IGET_BOOLEAN:
jeffhao71eee1f2011-01-04 14:18:54 -08002435 case OP_IGET_BOOLEAN_JUMBO:
Ben Chengba4fc8b2009-06-01 13:00:29 -07002436 case OP_IGET_BYTE:
jeffhao71eee1f2011-01-04 14:18:54 -08002437 case OP_IGET_BYTE_JUMBO:
Ben Chengba4fc8b2009-06-01 13:00:29 -07002438 case OP_IGET_CHAR:
jeffhao71eee1f2011-01-04 14:18:54 -08002439 case OP_IGET_CHAR_JUMBO:
Ben Chengba4fc8b2009-06-01 13:00:29 -07002440 case OP_IGET_SHORT:
jeffhao71eee1f2011-01-04 14:18:54 -08002441 case OP_IGET_SHORT_JUMBO:
buzbee3272e2f2010-09-09 14:07:01 -07002442 genIGet(cUnit, mir, kWord, fieldOffset, isVolatile);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002443 break;
2444 case OP_IPUT_WIDE:
jeffhao71eee1f2011-01-04 14:18:54 -08002445 case OP_IPUT_WIDE_JUMBO:
Ben Chengba4fc8b2009-06-01 13:00:29 -07002446 genIPutWide(cUnit, mir, fieldOffset);
2447 break;
2448 case OP_IPUT:
jeffhao71eee1f2011-01-04 14:18:54 -08002449 case OP_IPUT_JUMBO:
buzbee3272e2f2010-09-09 14:07:01 -07002450 case OP_IPUT_BOOLEAN:
jeffhao71eee1f2011-01-04 14:18:54 -08002451 case OP_IPUT_BOOLEAN_JUMBO:
2452 case OP_IPUT_BYTE:
2453 case OP_IPUT_BYTE_JUMBO:
2454 case OP_IPUT_CHAR:
2455 case OP_IPUT_CHAR_JUMBO:
2456 case OP_IPUT_SHORT:
2457 case OP_IPUT_SHORT_JUMBO:
buzbeeecf8f6e2010-07-20 14:53:42 -07002458 genIPut(cUnit, mir, kWord, fieldOffset, false, isVolatile);
buzbee919eb062010-07-12 12:59:22 -07002459 break;
buzbee4d92e682010-07-29 15:24:14 -07002460 case OP_IPUT_VOLATILE:
buzbeeecf8f6e2010-07-20 14:53:42 -07002461 case OP_IPUT_OBJECT_VOLATILE:
2462 isVolatile = true;
2463 // NOTE: intentional fallthrough
Ben Chengba4fc8b2009-06-01 13:00:29 -07002464 case OP_IPUT_OBJECT:
jeffhao71eee1f2011-01-04 14:18:54 -08002465 case OP_IPUT_OBJECT_JUMBO:
buzbeeecf8f6e2010-07-20 14:53:42 -07002466 genIPut(cUnit, mir, kWord, fieldOffset, true, isVolatile);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002467 break;
Bill Buzbeeb16344a2010-03-15 17:19:12 -07002468 case OP_IGET_WIDE_VOLATILE:
2469 case OP_IPUT_WIDE_VOLATILE:
Bill Buzbeeb16344a2010-03-15 17:19:12 -07002470 genInterpSingleStep(cUnit, mir);
2471 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002472 default:
2473 return true;
2474 }
2475 return false;
2476}
2477
2478static bool handleFmt22cs(CompilationUnit *cUnit, MIR *mir)
2479{
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002480 Opcode dalvikOpcode = mir->dalvikInsn.opcode;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002481 int fieldOffset = mir->dalvikInsn.vC;
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002482 switch (dalvikOpcode) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07002483 case OP_IGET_QUICK:
2484 case OP_IGET_OBJECT_QUICK:
buzbeeecf8f6e2010-07-20 14:53:42 -07002485 genIGet(cUnit, mir, kWord, fieldOffset, false);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002486 break;
2487 case OP_IPUT_QUICK:
buzbeeecf8f6e2010-07-20 14:53:42 -07002488 genIPut(cUnit, mir, kWord, fieldOffset, false, false);
buzbee919eb062010-07-12 12:59:22 -07002489 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002490 case OP_IPUT_OBJECT_QUICK:
buzbeeecf8f6e2010-07-20 14:53:42 -07002491 genIPut(cUnit, mir, kWord, fieldOffset, true, false);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002492 break;
2493 case OP_IGET_WIDE_QUICK:
2494 genIGetWide(cUnit, mir, fieldOffset);
2495 break;
2496 case OP_IPUT_WIDE_QUICK:
2497 genIPutWide(cUnit, mir, fieldOffset);
2498 break;
2499 default:
2500 return true;
2501 }
2502 return false;
2503
2504}
2505
2506/* Compare agaist zero */
2507static bool handleFmt22t(CompilationUnit *cUnit, MIR *mir, BasicBlock *bb,
Bill Buzbee89efc3d2009-07-28 11:22:22 -07002508 ArmLIR *labelList)
Ben Chengba4fc8b2009-06-01 13:00:29 -07002509{
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002510 Opcode dalvikOpcode = mir->dalvikInsn.opcode;
Bill Buzbee89efc3d2009-07-28 11:22:22 -07002511 ArmConditionCode cond;
Bill Buzbeec6f10662010-02-09 11:16:15 -08002512 RegLocation rlSrc1 = dvmCompilerGetSrc(cUnit, mir, 0);
2513 RegLocation rlSrc2 = dvmCompilerGetSrc(cUnit, mir, 1);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002514
Bill Buzbee1465db52009-09-23 17:17:35 -07002515 rlSrc1 = loadValue(cUnit, rlSrc1, kCoreReg);
2516 rlSrc2 = loadValue(cUnit, rlSrc2, kCoreReg);
2517 opRegReg(cUnit, kOpCmp, rlSrc1.lowReg, rlSrc2.lowReg);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002518
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002519 switch (dalvikOpcode) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07002520 case OP_IF_EQ:
Bill Buzbee1465db52009-09-23 17:17:35 -07002521 cond = kArmCondEq;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002522 break;
2523 case OP_IF_NE:
Bill Buzbee1465db52009-09-23 17:17:35 -07002524 cond = kArmCondNe;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002525 break;
2526 case OP_IF_LT:
Bill Buzbee1465db52009-09-23 17:17:35 -07002527 cond = kArmCondLt;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002528 break;
2529 case OP_IF_GE:
Bill Buzbee1465db52009-09-23 17:17:35 -07002530 cond = kArmCondGe;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002531 break;
2532 case OP_IF_GT:
Bill Buzbee1465db52009-09-23 17:17:35 -07002533 cond = kArmCondGt;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002534 break;
2535 case OP_IF_LE:
Bill Buzbee1465db52009-09-23 17:17:35 -07002536 cond = kArmCondLe;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002537 break;
2538 default:
2539 cond = 0;
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002540 LOGE("Unexpected opcode (%d) for Fmt22t\n", dalvikOpcode);
Bill Buzbeefc519dc2010-03-06 23:30:57 -08002541 dvmCompilerAbort(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002542 }
2543 genConditionalBranch(cUnit, cond, &labelList[bb->taken->id]);
2544 /* This mostly likely will be optimized away in a later phase */
2545 genUnconditionalBranch(cUnit, &labelList[bb->fallThrough->id]);
2546 return false;
2547}
2548
2549static bool handleFmt22x_Fmt32x(CompilationUnit *cUnit, MIR *mir)
2550{
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002551 Opcode opcode = mir->dalvikInsn.opcode;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002552
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002553 switch (opcode) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07002554 case OP_MOVE_16:
2555 case OP_MOVE_OBJECT_16:
2556 case OP_MOVE_FROM16:
Ben Chenge9695e52009-06-16 16:11:47 -07002557 case OP_MOVE_OBJECT_FROM16: {
Bill Buzbeec6f10662010-02-09 11:16:15 -08002558 storeValue(cUnit, dvmCompilerGetDest(cUnit, mir, 0),
2559 dvmCompilerGetSrc(cUnit, mir, 0));
Ben Chengba4fc8b2009-06-01 13:00:29 -07002560 break;
Ben Chenge9695e52009-06-16 16:11:47 -07002561 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07002562 case OP_MOVE_WIDE_16:
Ben Chenge9695e52009-06-16 16:11:47 -07002563 case OP_MOVE_WIDE_FROM16: {
Bill Buzbeec6f10662010-02-09 11:16:15 -08002564 storeValueWide(cUnit, dvmCompilerGetDestWide(cUnit, mir, 0, 1),
2565 dvmCompilerGetSrcWide(cUnit, mir, 0, 1));
Ben Chengba4fc8b2009-06-01 13:00:29 -07002566 break;
Ben Chenge9695e52009-06-16 16:11:47 -07002567 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07002568 default:
2569 return true;
2570 }
2571 return false;
2572}
2573
2574static bool handleFmt23x(CompilationUnit *cUnit, MIR *mir)
2575{
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002576 Opcode opcode = mir->dalvikInsn.opcode;
Bill Buzbee1465db52009-09-23 17:17:35 -07002577 RegLocation rlSrc1;
2578 RegLocation rlSrc2;
2579 RegLocation rlDest;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002580
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002581 if ( (opcode >= OP_ADD_INT) && (opcode <= OP_REM_DOUBLE)) {
Ben Cheng5d90c202009-11-22 23:31:11 -08002582 return genArithOp( cUnit, mir );
Ben Chengba4fc8b2009-06-01 13:00:29 -07002583 }
2584
Bill Buzbee1465db52009-09-23 17:17:35 -07002585 /* APUTs have 3 sources and no targets */
2586 if (mir->ssaRep->numDefs == 0) {
2587 if (mir->ssaRep->numUses == 3) {
Bill Buzbeec6f10662010-02-09 11:16:15 -08002588 rlDest = dvmCompilerGetSrc(cUnit, mir, 0);
2589 rlSrc1 = dvmCompilerGetSrc(cUnit, mir, 1);
2590 rlSrc2 = dvmCompilerGetSrc(cUnit, mir, 2);
Bill Buzbee1465db52009-09-23 17:17:35 -07002591 } else {
2592 assert(mir->ssaRep->numUses == 4);
Bill Buzbeec6f10662010-02-09 11:16:15 -08002593 rlDest = dvmCompilerGetSrcWide(cUnit, mir, 0, 1);
2594 rlSrc1 = dvmCompilerGetSrc(cUnit, mir, 2);
2595 rlSrc2 = dvmCompilerGetSrc(cUnit, mir, 3);
Bill Buzbee1465db52009-09-23 17:17:35 -07002596 }
2597 } else {
2598 /* Two sources and 1 dest. Deduce the operand sizes */
2599 if (mir->ssaRep->numUses == 4) {
Bill Buzbeec6f10662010-02-09 11:16:15 -08002600 rlSrc1 = dvmCompilerGetSrcWide(cUnit, mir, 0, 1);
2601 rlSrc2 = dvmCompilerGetSrcWide(cUnit, mir, 2, 3);
Bill Buzbee1465db52009-09-23 17:17:35 -07002602 } else {
2603 assert(mir->ssaRep->numUses == 2);
Bill Buzbeec6f10662010-02-09 11:16:15 -08002604 rlSrc1 = dvmCompilerGetSrc(cUnit, mir, 0);
2605 rlSrc2 = dvmCompilerGetSrc(cUnit, mir, 1);
Bill Buzbee1465db52009-09-23 17:17:35 -07002606 }
2607 if (mir->ssaRep->numDefs == 2) {
Bill Buzbeec6f10662010-02-09 11:16:15 -08002608 rlDest = dvmCompilerGetDestWide(cUnit, mir, 0, 1);
Bill Buzbee1465db52009-09-23 17:17:35 -07002609 } else {
2610 assert(mir->ssaRep->numDefs == 1);
Bill Buzbeec6f10662010-02-09 11:16:15 -08002611 rlDest = dvmCompilerGetDest(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07002612 }
2613 }
2614
2615
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002616 switch (opcode) {
Bill Buzbeed45ba372009-06-15 17:00:57 -07002617 case OP_CMPL_FLOAT:
2618 case OP_CMPG_FLOAT:
2619 case OP_CMPL_DOUBLE:
2620 case OP_CMPG_DOUBLE:
Ben Cheng5d90c202009-11-22 23:31:11 -08002621 return genCmpFP(cUnit, mir, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002622 case OP_CMP_LONG:
Bill Buzbee1465db52009-09-23 17:17:35 -07002623 genCmpLong(cUnit, mir, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002624 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002625 case OP_AGET_WIDE:
Bill Buzbee1465db52009-09-23 17:17:35 -07002626 genArrayGet(cUnit, mir, kLong, rlSrc1, rlSrc2, rlDest, 3);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002627 break;
2628 case OP_AGET:
2629 case OP_AGET_OBJECT:
Bill Buzbee1465db52009-09-23 17:17:35 -07002630 genArrayGet(cUnit, mir, kWord, rlSrc1, rlSrc2, rlDest, 2);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002631 break;
2632 case OP_AGET_BOOLEAN:
Bill Buzbee1465db52009-09-23 17:17:35 -07002633 genArrayGet(cUnit, mir, kUnsignedByte, rlSrc1, rlSrc2, rlDest, 0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002634 break;
2635 case OP_AGET_BYTE:
Bill Buzbee1465db52009-09-23 17:17:35 -07002636 genArrayGet(cUnit, mir, kSignedByte, rlSrc1, rlSrc2, rlDest, 0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002637 break;
2638 case OP_AGET_CHAR:
Bill Buzbee1465db52009-09-23 17:17:35 -07002639 genArrayGet(cUnit, mir, kUnsignedHalf, rlSrc1, rlSrc2, rlDest, 1);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002640 break;
2641 case OP_AGET_SHORT:
Bill Buzbee1465db52009-09-23 17:17:35 -07002642 genArrayGet(cUnit, mir, kSignedHalf, rlSrc1, rlSrc2, rlDest, 1);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002643 break;
2644 case OP_APUT_WIDE:
Bill Buzbee1465db52009-09-23 17:17:35 -07002645 genArrayPut(cUnit, mir, kLong, rlSrc1, rlSrc2, rlDest, 3);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002646 break;
2647 case OP_APUT:
Bill Buzbee1465db52009-09-23 17:17:35 -07002648 genArrayPut(cUnit, mir, kWord, rlSrc1, rlSrc2, rlDest, 2);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002649 break;
Bill Buzbeebe6534f2010-03-12 16:01:35 -08002650 case OP_APUT_OBJECT:
2651 genArrayObjectPut(cUnit, mir, rlSrc1, rlSrc2, rlDest, 2);
2652 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002653 case OP_APUT_SHORT:
2654 case OP_APUT_CHAR:
Bill Buzbee1465db52009-09-23 17:17:35 -07002655 genArrayPut(cUnit, mir, kUnsignedHalf, rlSrc1, rlSrc2, rlDest, 1);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002656 break;
2657 case OP_APUT_BYTE:
2658 case OP_APUT_BOOLEAN:
Bill Buzbee1465db52009-09-23 17:17:35 -07002659 genArrayPut(cUnit, mir, kUnsignedByte, rlSrc1, rlSrc2, rlDest, 0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002660 break;
2661 default:
2662 return true;
2663 }
2664 return false;
2665}
2666
Ben Cheng6c10a972009-10-29 14:39:18 -07002667/*
2668 * Find the matching case.
2669 *
2670 * return values:
2671 * r0 (low 32-bit): pc of the chaining cell corresponding to the resolved case,
2672 * including default which is placed at MIN(size, MAX_CHAINED_SWITCH_CASES).
2673 * r1 (high 32-bit): the branch offset of the matching case (only for indexes
2674 * above MAX_CHAINED_SWITCH_CASES).
2675 *
2676 * Instructions around the call are:
2677 *
2678 * mov r2, pc
2679 * blx &findPackedSwitchIndex
2680 * mov pc, r0
2681 * .align4
Bill Buzbeebd047242010-05-13 13:02:53 -07002682 * chaining cell for case 0 [12 bytes]
2683 * chaining cell for case 1 [12 bytes]
Ben Cheng6c10a972009-10-29 14:39:18 -07002684 * :
Bill Buzbeebd047242010-05-13 13:02:53 -07002685 * chaining cell for case MIN(size, MAX_CHAINED_SWITCH_CASES)-1 [12 bytes]
Ben Cheng6c10a972009-10-29 14:39:18 -07002686 * chaining cell for case default [8 bytes]
2687 * noChain exit
2688 */
Ben Chengbd1326d2010-04-02 15:04:53 -07002689static s8 findPackedSwitchIndex(const u2* switchData, int testVal, int pc)
Ben Cheng6c10a972009-10-29 14:39:18 -07002690{
2691 int size;
2692 int firstKey;
2693 const int *entries;
2694 int index;
2695 int jumpIndex;
2696 int caseDPCOffset = 0;
2697 /* In Thumb mode pc is 4 ahead of the "mov r2, pc" instruction */
2698 int chainingPC = (pc + 4) & ~3;
2699
2700 /*
2701 * Packed switch data format:
2702 * ushort ident = 0x0100 magic value
2703 * ushort size number of entries in the table
2704 * int first_key first (and lowest) switch case value
2705 * int targets[size] branch targets, relative to switch opcode
2706 *
2707 * Total size is (4+size*2) 16-bit code units.
2708 */
2709 size = switchData[1];
2710 assert(size > 0);
2711
2712 firstKey = switchData[2];
2713 firstKey |= switchData[3] << 16;
2714
2715
2716 /* The entries are guaranteed to be aligned on a 32-bit boundary;
2717 * we can treat them as a native int array.
2718 */
2719 entries = (const int*) &switchData[4];
2720 assert(((u4)entries & 0x3) == 0);
2721
2722 index = testVal - firstKey;
2723
2724 /* Jump to the default cell */
2725 if (index < 0 || index >= size) {
2726 jumpIndex = MIN(size, MAX_CHAINED_SWITCH_CASES);
2727 /* Jump to the non-chaining exit point */
2728 } else if (index >= MAX_CHAINED_SWITCH_CASES) {
2729 jumpIndex = MAX_CHAINED_SWITCH_CASES + 1;
2730 caseDPCOffset = entries[index];
2731 /* Jump to the inline chaining cell */
2732 } else {
2733 jumpIndex = index;
2734 }
2735
Bill Buzbeebd047242010-05-13 13:02:53 -07002736 chainingPC += jumpIndex * CHAIN_CELL_NORMAL_SIZE;
Ben Cheng6c10a972009-10-29 14:39:18 -07002737 return (((s8) caseDPCOffset) << 32) | (u8) chainingPC;
2738}
2739
2740/* See comments for findPackedSwitchIndex */
Ben Chengbd1326d2010-04-02 15:04:53 -07002741static s8 findSparseSwitchIndex(const u2* switchData, int testVal, int pc)
Ben Cheng6c10a972009-10-29 14:39:18 -07002742{
2743 int size;
2744 const int *keys;
2745 const int *entries;
2746 int chainingPC = (pc + 4) & ~3;
2747 int i;
2748
2749 /*
2750 * Sparse switch data format:
2751 * ushort ident = 0x0200 magic value
2752 * ushort size number of entries in the table; > 0
2753 * int keys[size] keys, sorted low-to-high; 32-bit aligned
2754 * int targets[size] branch targets, relative to switch opcode
2755 *
2756 * Total size is (2+size*4) 16-bit code units.
2757 */
2758
2759 size = switchData[1];
2760 assert(size > 0);
2761
2762 /* The keys are guaranteed to be aligned on a 32-bit boundary;
2763 * we can treat them as a native int array.
2764 */
2765 keys = (const int*) &switchData[2];
2766 assert(((u4)keys & 0x3) == 0);
2767
2768 /* The entries are guaranteed to be aligned on a 32-bit boundary;
2769 * we can treat them as a native int array.
2770 */
2771 entries = keys + size;
2772 assert(((u4)entries & 0x3) == 0);
2773
2774 /*
2775 * Run through the list of keys, which are guaranteed to
2776 * be sorted low-to-high.
2777 *
2778 * Most tables have 3-4 entries. Few have more than 10. A binary
2779 * search here is probably not useful.
2780 */
2781 for (i = 0; i < size; i++) {
2782 int k = keys[i];
2783 if (k == testVal) {
2784 /* MAX_CHAINED_SWITCH_CASES + 1 is the start of the overflow case */
2785 int jumpIndex = (i < MAX_CHAINED_SWITCH_CASES) ?
2786 i : MAX_CHAINED_SWITCH_CASES + 1;
Bill Buzbeebd047242010-05-13 13:02:53 -07002787 chainingPC += jumpIndex * CHAIN_CELL_NORMAL_SIZE;
Ben Cheng6c10a972009-10-29 14:39:18 -07002788 return (((s8) entries[i]) << 32) | (u8) chainingPC;
2789 } else if (k > testVal) {
2790 break;
2791 }
2792 }
Bill Buzbeebd047242010-05-13 13:02:53 -07002793 return chainingPC + MIN(size, MAX_CHAINED_SWITCH_CASES) *
2794 CHAIN_CELL_NORMAL_SIZE;
Ben Cheng6c10a972009-10-29 14:39:18 -07002795}
2796
Ben Chengba4fc8b2009-06-01 13:00:29 -07002797static bool handleFmt31t(CompilationUnit *cUnit, MIR *mir)
2798{
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002799 Opcode dalvikOpcode = mir->dalvikInsn.opcode;
2800 switch (dalvikOpcode) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07002801 case OP_FILL_ARRAY_DATA: {
Bill Buzbeec6f10662010-02-09 11:16:15 -08002802 RegLocation rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07002803 // Making a call - use explicit registers
Bill Buzbeec6f10662010-02-09 11:16:15 -08002804 dvmCompilerFlushAllRegs(cUnit); /* Everything to home location */
Bill Buzbee1465db52009-09-23 17:17:35 -07002805 genExportPC(cUnit, mir);
2806 loadValueDirectFixed(cUnit, rlSrc, r0);
Ben Chengbd1326d2010-04-02 15:04:53 -07002807 LOAD_FUNC_ADDR(cUnit, r2, (int)dvmInterpHandleFillArrayData);
Ben Cheng6c10a972009-10-29 14:39:18 -07002808 loadConstant(cUnit, r1,
2809 (int) (cUnit->method->insns + mir->offset + mir->dalvikInsn.vB));
Bill Buzbee1465db52009-09-23 17:17:35 -07002810 opReg(cUnit, kOpBlx, r2);
Elliott Hughes6a555132010-02-25 15:41:42 -08002811 dvmCompilerClobberCallRegs(cUnit);
Bill Buzbeefd023aa2009-11-02 09:23:49 -08002812 /* generate a branch over if successful */
buzbee8f8109a2010-08-31 10:16:35 -07002813 ArmLIR *branchOver = genCmpImmBranch(cUnit, kArmCondNe, r0, 0);
Bill Buzbeefd023aa2009-11-02 09:23:49 -08002814 loadConstant(cUnit, r0,
2815 (int) (cUnit->method->insns + mir->offset));
2816 genDispatchToHandler(cUnit, TEMPLATE_THROW_EXCEPTION_COMMON);
2817 ArmLIR *target = newLIR0(cUnit, kArmPseudoTargetLabel);
2818 target->defMask = ENCODE_ALL;
2819 branchOver->generic.target = (LIR *) target;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002820 break;
2821 }
2822 /*
Ben Cheng6c10a972009-10-29 14:39:18 -07002823 * Compute the goto target of up to
2824 * MIN(switchSize, MAX_CHAINED_SWITCH_CASES) + 1 chaining cells.
2825 * See the comment before findPackedSwitchIndex for the code layout.
Ben Chengba4fc8b2009-06-01 13:00:29 -07002826 */
2827 case OP_PACKED_SWITCH:
2828 case OP_SPARSE_SWITCH: {
Bill Buzbeec6f10662010-02-09 11:16:15 -08002829 RegLocation rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
2830 dvmCompilerFlushAllRegs(cUnit); /* Everything to home location */
Bill Buzbee1465db52009-09-23 17:17:35 -07002831 loadValueDirectFixed(cUnit, rlSrc, r1);
Bill Buzbeec6f10662010-02-09 11:16:15 -08002832 dvmCompilerLockAllTemps(cUnit);
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002833 if (dalvikOpcode == OP_PACKED_SWITCH) {
Ben Chengbd1326d2010-04-02 15:04:53 -07002834 LOAD_FUNC_ADDR(cUnit, r4PC, (int)findPackedSwitchIndex);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002835 } else {
Ben Chengbd1326d2010-04-02 15:04:53 -07002836 LOAD_FUNC_ADDR(cUnit, r4PC, (int)findSparseSwitchIndex);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002837 }
Ben Cheng6c10a972009-10-29 14:39:18 -07002838 /* r0 <- Addr of the switch data */
2839 loadConstant(cUnit, r0,
2840 (int) (cUnit->method->insns + mir->offset + mir->dalvikInsn.vB));
2841 /* r2 <- pc of the instruction following the blx */
2842 opRegReg(cUnit, kOpMov, r2, rpc);
Bill Buzbee1465db52009-09-23 17:17:35 -07002843 opReg(cUnit, kOpBlx, r4PC);
Elliott Hughes6a555132010-02-25 15:41:42 -08002844 dvmCompilerClobberCallRegs(cUnit);
Ben Cheng6c10a972009-10-29 14:39:18 -07002845 /* pc <- computed goto target */
2846 opRegReg(cUnit, kOpMov, rpc, r0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002847 break;
2848 }
2849 default:
2850 return true;
2851 }
2852 return false;
2853}
2854
Ben Cheng7a2697d2010-06-07 13:44:23 -07002855/*
2856 * See the example of predicted inlining listed before the
2857 * genValidationForPredictedInline function. The function here takes care the
2858 * branch over at 0x4858de78 and the misprediction target at 0x4858de7a.
2859 */
2860static void genLandingPadForMispredictedCallee(CompilationUnit *cUnit, MIR *mir,
2861 BasicBlock *bb,
2862 ArmLIR *labelList)
2863{
2864 BasicBlock *fallThrough = bb->fallThrough;
2865
2866 /* Bypass the move-result block if there is one */
2867 if (fallThrough->firstMIRInsn) {
2868 assert(fallThrough->firstMIRInsn->OptimizationFlags & MIR_INLINED_PRED);
2869 fallThrough = fallThrough->fallThrough;
2870 }
2871 /* Generate a branch over if the predicted inlining is correct */
2872 genUnconditionalBranch(cUnit, &labelList[fallThrough->id]);
2873
2874 /* Reset the register state */
2875 dvmCompilerResetRegPool(cUnit);
2876 dvmCompilerClobberAllRegs(cUnit);
2877 dvmCompilerResetNullCheck(cUnit);
2878
2879 /* Target for the slow invoke path */
2880 ArmLIR *target = newLIR0(cUnit, kArmPseudoTargetLabel);
2881 target->defMask = ENCODE_ALL;
2882 /* Hook up the target to the verification branch */
2883 mir->meta.callsiteInfo->misPredBranchOver->target = (LIR *) target;
2884}
2885
jeffhao71eee1f2011-01-04 14:18:54 -08002886static bool handleFmt35c_3rc_5rc(CompilationUnit *cUnit, MIR *mir,
2887 BasicBlock *bb, ArmLIR *labelList)
Ben Chengba4fc8b2009-06-01 13:00:29 -07002888{
Bill Buzbee9bc3df32009-07-30 10:52:29 -07002889 ArmLIR *retChainingCell = NULL;
Bill Buzbee89efc3d2009-07-28 11:22:22 -07002890 ArmLIR *pcrLabel = NULL;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002891
Ben Cheng7a2697d2010-06-07 13:44:23 -07002892 /* An invoke with the MIR_INLINED is effectively a no-op */
2893 if (mir->OptimizationFlags & MIR_INLINED)
2894 return false;
2895
Bill Buzbeef4ce16f2009-07-28 13:28:25 -07002896 if (bb->fallThrough != NULL)
2897 retChainingCell = &labelList[bb->fallThrough->id];
2898
Ben Chengba4fc8b2009-06-01 13:00:29 -07002899 DecodedInstruction *dInsn = &mir->dalvikInsn;
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002900 switch (mir->dalvikInsn.opcode) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07002901 /*
2902 * calleeMethod = this->clazz->vtable[
2903 * method->clazz->pDvmDex->pResMethods[BBBB]->methodIndex
2904 * ]
2905 */
2906 case OP_INVOKE_VIRTUAL:
jeffhao71eee1f2011-01-04 14:18:54 -08002907 case OP_INVOKE_VIRTUAL_RANGE:
2908 case OP_INVOKE_VIRTUAL_JUMBO: {
Bill Buzbee89efc3d2009-07-28 11:22:22 -07002909 ArmLIR *predChainingCell = &labelList[bb->taken->id];
Ben Chengba4fc8b2009-06-01 13:00:29 -07002910 int methodIndex =
2911 cUnit->method->clazz->pDvmDex->pResMethods[dInsn->vB]->
2912 methodIndex;
2913
Ben Cheng7a2697d2010-06-07 13:44:23 -07002914 /*
2915 * If the invoke has non-null misPredBranchOver, we need to generate
2916 * the non-inlined version of the invoke here to handle the
2917 * mispredicted case.
2918 */
2919 if (mir->meta.callsiteInfo->misPredBranchOver) {
2920 genLandingPadForMispredictedCallee(cUnit, mir, bb, labelList);
2921 }
2922
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002923 if (mir->dalvikInsn.opcode == OP_INVOKE_VIRTUAL)
Ben Chengba4fc8b2009-06-01 13:00:29 -07002924 genProcessArgsNoRange(cUnit, mir, dInsn, &pcrLabel);
2925 else
2926 genProcessArgsRange(cUnit, mir, dInsn, &pcrLabel);
2927
Ben Cheng38329f52009-07-07 14:19:20 -07002928 genInvokeVirtualCommon(cUnit, mir, methodIndex,
2929 retChainingCell,
2930 predChainingCell,
2931 pcrLabel);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002932 break;
2933 }
2934 /*
2935 * calleeMethod = method->clazz->super->vtable[method->clazz->pDvmDex
2936 * ->pResMethods[BBBB]->methodIndex]
2937 */
Ben Chengba4fc8b2009-06-01 13:00:29 -07002938 case OP_INVOKE_SUPER:
jeffhao71eee1f2011-01-04 14:18:54 -08002939 case OP_INVOKE_SUPER_RANGE:
2940 case OP_INVOKE_SUPER_JUMBO: {
Ben Cheng7a2697d2010-06-07 13:44:23 -07002941 /* Grab the method ptr directly from what the interpreter sees */
2942 const Method *calleeMethod = mir->meta.callsiteInfo->method;
2943 assert(calleeMethod == cUnit->method->clazz->super->vtable[
2944 cUnit->method->clazz->pDvmDex->
2945 pResMethods[dInsn->vB]->methodIndex]);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002946
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002947 if (mir->dalvikInsn.opcode == OP_INVOKE_SUPER)
Ben Chengba4fc8b2009-06-01 13:00:29 -07002948 genProcessArgsNoRange(cUnit, mir, dInsn, &pcrLabel);
2949 else
2950 genProcessArgsRange(cUnit, mir, dInsn, &pcrLabel);
2951
2952 /* r0 = calleeMethod */
2953 loadConstant(cUnit, r0, (int) calleeMethod);
2954
Ben Cheng38329f52009-07-07 14:19:20 -07002955 genInvokeSingletonCommon(cUnit, mir, bb, labelList, pcrLabel,
2956 calleeMethod);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002957 break;
2958 }
2959 /* calleeMethod = method->clazz->pDvmDex->pResMethods[BBBB] */
2960 case OP_INVOKE_DIRECT:
jeffhao71eee1f2011-01-04 14:18:54 -08002961 case OP_INVOKE_DIRECT_RANGE:
2962 case OP_INVOKE_DIRECT_JUMBO: {
Ben Cheng7a2697d2010-06-07 13:44:23 -07002963 /* Grab the method ptr directly from what the interpreter sees */
2964 const Method *calleeMethod = mir->meta.callsiteInfo->method;
2965 assert(calleeMethod ==
2966 cUnit->method->clazz->pDvmDex->pResMethods[dInsn->vB]);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002967
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002968 if (mir->dalvikInsn.opcode == OP_INVOKE_DIRECT)
Ben Chengba4fc8b2009-06-01 13:00:29 -07002969 genProcessArgsNoRange(cUnit, mir, dInsn, &pcrLabel);
2970 else
2971 genProcessArgsRange(cUnit, mir, dInsn, &pcrLabel);
2972
2973 /* r0 = calleeMethod */
2974 loadConstant(cUnit, r0, (int) calleeMethod);
2975
Ben Cheng38329f52009-07-07 14:19:20 -07002976 genInvokeSingletonCommon(cUnit, mir, bb, labelList, pcrLabel,
2977 calleeMethod);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002978 break;
2979 }
2980 /* calleeMethod = method->clazz->pDvmDex->pResMethods[BBBB] */
2981 case OP_INVOKE_STATIC:
jeffhao71eee1f2011-01-04 14:18:54 -08002982 case OP_INVOKE_STATIC_RANGE:
2983 case OP_INVOKE_STATIC_JUMBO: {
Ben Cheng7a2697d2010-06-07 13:44:23 -07002984 /* Grab the method ptr directly from what the interpreter sees */
2985 const Method *calleeMethod = mir->meta.callsiteInfo->method;
2986 assert(calleeMethod ==
2987 cUnit->method->clazz->pDvmDex->pResMethods[dInsn->vB]);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002988
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002989 if (mir->dalvikInsn.opcode == OP_INVOKE_STATIC)
Ben Chengba4fc8b2009-06-01 13:00:29 -07002990 genProcessArgsNoRange(cUnit, mir, dInsn,
2991 NULL /* no null check */);
2992 else
2993 genProcessArgsRange(cUnit, mir, dInsn,
2994 NULL /* no null check */);
2995
2996 /* r0 = calleeMethod */
2997 loadConstant(cUnit, r0, (int) calleeMethod);
2998
Ben Cheng38329f52009-07-07 14:19:20 -07002999 genInvokeSingletonCommon(cUnit, mir, bb, labelList, pcrLabel,
3000 calleeMethod);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003001 break;
3002 }
Ben Cheng09e50c92010-05-02 10:45:32 -07003003 /*
Ben Chengba4fc8b2009-06-01 13:00:29 -07003004 * calleeMethod = dvmFindInterfaceMethodInCache(this->clazz,
3005 * BBBB, method, method->clazz->pDvmDex)
Ben Cheng38329f52009-07-07 14:19:20 -07003006 *
Ben Cheng09e50c92010-05-02 10:45:32 -07003007 * The following is an example of generated code for
3008 * "invoke-interface v0"
Ben Cheng38329f52009-07-07 14:19:20 -07003009 *
Ben Cheng09e50c92010-05-02 10:45:32 -07003010 * -------- dalvik offset: 0x0008 @ invoke-interface v0
3011 * 0x47357e36 : ldr r0, [r5, #0] --+
3012 * 0x47357e38 : sub r7,r5,#24 |
3013 * 0x47357e3c : cmp r0, #0 | genProcessArgsNoRange
3014 * 0x47357e3e : beq 0x47357e82 |
3015 * 0x47357e40 : stmia r7, <r0> --+
3016 * 0x47357e42 : ldr r4, [pc, #120] --> r4 <- dalvikPC of this invoke
3017 * 0x47357e44 : add r1, pc, #64 --> r1 <- &retChainingCell
3018 * 0x47357e46 : add r2, pc, #72 --> r2 <- &predictedChainingCell
3019 * 0x47357e48 : blx_1 0x47348190 --+ TEMPLATE_INVOKE_METHOD_
3020 * 0x47357e4a : blx_2 see above --+ PREDICTED_CHAIN
3021 * 0x47357e4c : b 0x47357e90 --> off to the predicted chain
3022 * 0x47357e4e : b 0x47357e82 --> punt to the interpreter
3023 * 0x47357e50 : mov r8, r1 --+
3024 * 0x47357e52 : mov r9, r2 |
3025 * 0x47357e54 : ldr r2, [pc, #96] |
3026 * 0x47357e56 : mov r10, r3 |
3027 * 0x47357e58 : movs r0, r3 | dvmFindInterfaceMethodInCache
3028 * 0x47357e5a : ldr r3, [pc, #88] |
3029 * 0x47357e5c : ldr r7, [pc, #80] |
3030 * 0x47357e5e : mov r1, #1452 |
3031 * 0x47357e62 : blx r7 --+
3032 * 0x47357e64 : cmp r0, #0 --> calleeMethod == NULL?
3033 * 0x47357e66 : bne 0x47357e6e --> branch over the throw if !r0
3034 * 0x47357e68 : ldr r0, [pc, #80] --> load Dalvik PC of the invoke
3035 * 0x47357e6a : blx_1 0x47348494 --+ TEMPLATE_THROW_EXCEPTION_
3036 * 0x47357e6c : blx_2 see above --+ COMMON
3037 * 0x47357e6e : mov r1, r8 --> r1 <- &retChainingCell
3038 * 0x47357e70 : cmp r1, #0 --> compare against 0
3039 * 0x47357e72 : bgt 0x47357e7c --> >=0? don't rechain
Ben Chengaf5aa1f2011-01-04 15:37:04 -08003040 * 0x47357e74 : ldr r7, [pc, #off] --+
Ben Cheng09e50c92010-05-02 10:45:32 -07003041 * 0x47357e76 : mov r2, r9 | dvmJitToPatchPredictedChain
3042 * 0x47357e78 : mov r3, r10 |
3043 * 0x47357e7a : blx r7 --+
3044 * 0x47357e7c : add r1, pc, #8 --> r1 <- &retChainingCell
3045 * 0x47357e7e : blx_1 0x4734809c --+ TEMPLATE_INVOKE_METHOD_NO_OPT
3046 * 0x47357e80 : blx_2 see above --+
3047 * -------- reconstruct dalvik PC : 0x425719dc @ +0x0008
3048 * 0x47357e82 : ldr r0, [pc, #56]
Ben Cheng38329f52009-07-07 14:19:20 -07003049 * Exception_Handling:
Ben Cheng09e50c92010-05-02 10:45:32 -07003050 * 0x47357e84 : ldr r1, [r6, #92]
3051 * 0x47357e86 : blx r1
3052 * 0x47357e88 : .align4
3053 * -------- chaining cell (hot): 0x000b
3054 * 0x47357e88 : ldr r0, [r6, #104]
3055 * 0x47357e8a : blx r0
3056 * 0x47357e8c : data 0x19e2(6626)
3057 * 0x47357e8e : data 0x4257(16983)
3058 * 0x47357e90 : .align4
Ben Cheng38329f52009-07-07 14:19:20 -07003059 * -------- chaining cell (predicted)
Ben Cheng09e50c92010-05-02 10:45:32 -07003060 * 0x47357e90 : data 0xe7fe(59390) --> will be patched into bx
3061 * 0x47357e92 : data 0x0000(0)
3062 * 0x47357e94 : data 0x0000(0) --> class
3063 * 0x47357e96 : data 0x0000(0)
3064 * 0x47357e98 : data 0x0000(0) --> method
3065 * 0x47357e9a : data 0x0000(0)
3066 * 0x47357e9c : data 0x0000(0) --> rechain count
3067 * 0x47357e9e : data 0x0000(0)
3068 * -------- end of chaining cells (0x006c)
3069 * 0x47357eb0 : .word (0xad03e369)
3070 * 0x47357eb4 : .word (0x28a90)
3071 * 0x47357eb8 : .word (0x41a63394)
3072 * 0x47357ebc : .word (0x425719dc)
Ben Chengba4fc8b2009-06-01 13:00:29 -07003073 */
3074 case OP_INVOKE_INTERFACE:
jeffhao71eee1f2011-01-04 14:18:54 -08003075 case OP_INVOKE_INTERFACE_RANGE:
3076 case OP_INVOKE_INTERFACE_JUMBO: {
Bill Buzbee89efc3d2009-07-28 11:22:22 -07003077 ArmLIR *predChainingCell = &labelList[bb->taken->id];
Ben Chengba4fc8b2009-06-01 13:00:29 -07003078
Ben Cheng7a2697d2010-06-07 13:44:23 -07003079 /*
3080 * If the invoke has non-null misPredBranchOver, we need to generate
3081 * the non-inlined version of the invoke here to handle the
3082 * mispredicted case.
3083 */
3084 if (mir->meta.callsiteInfo->misPredBranchOver) {
3085 genLandingPadForMispredictedCallee(cUnit, mir, bb, labelList);
3086 }
Bill Buzbee1465db52009-09-23 17:17:35 -07003087
Dan Bornstein9a1f8162010-12-01 17:02:26 -08003088 if (mir->dalvikInsn.opcode == OP_INVOKE_INTERFACE)
Ben Chengba4fc8b2009-06-01 13:00:29 -07003089 genProcessArgsNoRange(cUnit, mir, dInsn, &pcrLabel);
3090 else
3091 genProcessArgsRange(cUnit, mir, dInsn, &pcrLabel);
3092
Ben Cheng38329f52009-07-07 14:19:20 -07003093 /* "this" is already left in r0 by genProcessArgs* */
3094
3095 /* r4PC = dalvikCallsite */
3096 loadConstant(cUnit, r4PC,
3097 (int) (cUnit->method->insns + mir->offset));
3098
3099 /* r1 = &retChainingCell */
Bill Buzbee270c1d62009-08-13 16:58:07 -07003100 ArmLIR *addrRetChain =
Bill Buzbee1465db52009-09-23 17:17:35 -07003101 opRegRegImm(cUnit, kOpAdd, r1, rpc, 0);
Ben Cheng38329f52009-07-07 14:19:20 -07003102 addrRetChain->generic.target = (LIR *) retChainingCell;
3103
3104 /* r2 = &predictedChainingCell */
Bill Buzbee89efc3d2009-07-28 11:22:22 -07003105 ArmLIR *predictedChainingCell =
Bill Buzbee1465db52009-09-23 17:17:35 -07003106 opRegRegImm(cUnit, kOpAdd, r2, rpc, 0);
Ben Cheng38329f52009-07-07 14:19:20 -07003107 predictedChainingCell->generic.target = (LIR *) predChainingCell;
3108
buzbee18fba342011-01-19 15:31:15 -08003109 genDispatchToHandler(cUnit, gDvmJit.methodTraceSupport ?
3110 TEMPLATE_INVOKE_METHOD_PREDICTED_CHAIN_PROF :
3111 TEMPLATE_INVOKE_METHOD_PREDICTED_CHAIN);
Ben Cheng38329f52009-07-07 14:19:20 -07003112
3113 /* return through lr - jump to the chaining cell */
3114 genUnconditionalBranch(cUnit, predChainingCell);
3115
3116 /*
3117 * null-check on "this" may have been eliminated, but we still need
3118 * a PC-reconstruction label for stack overflow bailout.
3119 */
3120 if (pcrLabel == NULL) {
3121 int dPC = (int) (cUnit->method->insns + mir->offset);
Carl Shapirofc75f3e2010-12-07 11:43:38 -08003122 pcrLabel = (ArmLIR *) dvmCompilerNew(sizeof(ArmLIR), true);
Dan Bornstein9a1f8162010-12-01 17:02:26 -08003123 pcrLabel->opcode = kArmPseudoPCReconstructionCell;
Ben Cheng38329f52009-07-07 14:19:20 -07003124 pcrLabel->operands[0] = dPC;
3125 pcrLabel->operands[1] = mir->offset;
3126 /* Insert the place holder to the growable list */
Ben Cheng00603072010-10-28 11:13:58 -07003127 dvmInsertGrowableList(&cUnit->pcReconstructionList,
3128 (intptr_t) pcrLabel);
Ben Cheng38329f52009-07-07 14:19:20 -07003129 }
3130
3131 /* return through lr+2 - punt to the interpreter */
3132 genUnconditionalBranch(cUnit, pcrLabel);
3133
3134 /*
3135 * return through lr+4 - fully resolve the callee method.
3136 * r1 <- count
3137 * r2 <- &predictedChainCell
3138 * r3 <- this->class
3139 * r4 <- dPC
3140 * r7 <- this->class->vtable
3141 */
3142
3143 /* Save count, &predictedChainCell, and class to high regs first */
Bill Buzbee1465db52009-09-23 17:17:35 -07003144 genRegCopy(cUnit, r8, r1);
3145 genRegCopy(cUnit, r9, r2);
3146 genRegCopy(cUnit, r10, r3);
Ben Cheng38329f52009-07-07 14:19:20 -07003147
Ben Chengba4fc8b2009-06-01 13:00:29 -07003148 /* r0 now contains this->clazz */
Bill Buzbee1465db52009-09-23 17:17:35 -07003149 genRegCopy(cUnit, r0, r3);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003150
3151 /* r1 = BBBB */
3152 loadConstant(cUnit, r1, dInsn->vB);
3153
3154 /* r2 = method (caller) */
3155 loadConstant(cUnit, r2, (int) cUnit->method);
3156
3157 /* r3 = pDvmDex */
3158 loadConstant(cUnit, r3, (int) cUnit->method->clazz->pDvmDex);
3159
Ben Chengbd1326d2010-04-02 15:04:53 -07003160 LOAD_FUNC_ADDR(cUnit, r7,
3161 (intptr_t) dvmFindInterfaceMethodInCache);
Bill Buzbee1465db52009-09-23 17:17:35 -07003162 opReg(cUnit, kOpBlx, r7);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003163 /* r0 = calleeMethod (returned from dvmFindInterfaceMethodInCache */
3164
Ben Cheng09e50c92010-05-02 10:45:32 -07003165 dvmCompilerClobberCallRegs(cUnit);
3166 /* generate a branch over if the interface method is resolved */
buzbee8f8109a2010-08-31 10:16:35 -07003167 ArmLIR *branchOver = genCmpImmBranch(cUnit, kArmCondNe, r0, 0);
Ben Cheng09e50c92010-05-02 10:45:32 -07003168 /*
3169 * calleeMethod == NULL -> throw
3170 */
3171 loadConstant(cUnit, r0,
3172 (int) (cUnit->method->insns + mir->offset));
3173 genDispatchToHandler(cUnit, TEMPLATE_THROW_EXCEPTION_COMMON);
3174 /* noreturn */
3175
3176 ArmLIR *target = newLIR0(cUnit, kArmPseudoTargetLabel);
3177 target->defMask = ENCODE_ALL;
3178 branchOver->generic.target = (LIR *) target;
3179
Bill Buzbee1465db52009-09-23 17:17:35 -07003180 genRegCopy(cUnit, r1, r8);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003181
Ben Cheng38329f52009-07-07 14:19:20 -07003182 /* Check if rechain limit is reached */
buzbee8f8109a2010-08-31 10:16:35 -07003183 ArmLIR *bypassRechaining = genCmpImmBranch(cUnit, kArmCondGt,
3184 r1, 0);
Ben Cheng38329f52009-07-07 14:19:20 -07003185
Ben Chengaf5aa1f2011-01-04 15:37:04 -08003186 LOAD_FUNC_ADDR(cUnit, r7, (int) dvmJitToPatchPredictedChain);
Ben Cheng38329f52009-07-07 14:19:20 -07003187
Ben Chengb88ec3c2010-05-17 12:50:33 -07003188 genRegCopy(cUnit, r1, rGLUE);
Bill Buzbee1465db52009-09-23 17:17:35 -07003189 genRegCopy(cUnit, r2, r9);
3190 genRegCopy(cUnit, r3, r10);
Ben Cheng38329f52009-07-07 14:19:20 -07003191
3192 /*
3193 * r0 = calleeMethod
3194 * r2 = &predictedChainingCell
3195 * r3 = class
3196 *
3197 * &returnChainingCell has been loaded into r1 but is not needed
3198 * when patching the chaining cell and will be clobbered upon
3199 * returning so it will be reconstructed again.
3200 */
Bill Buzbee1465db52009-09-23 17:17:35 -07003201 opReg(cUnit, kOpBlx, r7);
Ben Cheng38329f52009-07-07 14:19:20 -07003202
3203 /* r1 = &retChainingCell */
Bill Buzbee1465db52009-09-23 17:17:35 -07003204 addrRetChain = opRegRegImm(cUnit, kOpAdd, r1, rpc, 0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003205 addrRetChain->generic.target = (LIR *) retChainingCell;
Ben Cheng38329f52009-07-07 14:19:20 -07003206
3207 bypassRechaining->generic.target = (LIR *) addrRetChain;
3208
Ben Chengba4fc8b2009-06-01 13:00:29 -07003209 /*
3210 * r0 = this, r1 = calleeMethod,
3211 * r1 = &ChainingCell,
3212 * r4PC = callsiteDPC,
3213 */
buzbee18fba342011-01-19 15:31:15 -08003214 genDispatchToHandler(cUnit, gDvmJit.methodTraceSupport ?
3215 TEMPLATE_INVOKE_METHOD_NO_OPT_PROF :
3216 TEMPLATE_INVOKE_METHOD_NO_OPT);
Ben Cheng978738d2010-05-13 13:45:57 -07003217#if defined(WITH_JIT_TUNING)
Ben Cheng86717f72010-03-05 15:27:21 -08003218 gDvmJit.invokePolymorphic++;
Ben Chengba4fc8b2009-06-01 13:00:29 -07003219#endif
3220 /* Handle exceptions using the interpreter */
3221 genTrap(cUnit, mir->offset, pcrLabel);
3222 break;
3223 }
3224 /* NOP */
3225 case OP_INVOKE_DIRECT_EMPTY: {
buzbee18fba342011-01-19 15:31:15 -08003226 if (gDvmJit.methodTraceSupport)
3227 genInterpSingleStep(cUnit, mir);
3228 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07003229 }
3230 case OP_FILLED_NEW_ARRAY:
jeffhao71eee1f2011-01-04 14:18:54 -08003231 case OP_FILLED_NEW_ARRAY_RANGE:
3232 case OP_FILLED_NEW_ARRAY_JUMBO: {
Ben Chengba4fc8b2009-06-01 13:00:29 -07003233 /* Just let the interpreter deal with these */
3234 genInterpSingleStep(cUnit, mir);
3235 break;
3236 }
3237 default:
3238 return true;
3239 }
3240 return false;
3241}
3242
Ben Chengcfdeca32011-01-14 11:36:46 -08003243/* "this" pointer is already in r0 */
3244static void genValidationForMethodCallee(CompilationUnit *cUnit, MIR *mir,
3245 ArmLIR **classCheck)
3246{
3247 CallsiteInfo *callsiteInfo = mir->meta.callsiteInfo;
3248 dvmCompilerLockAllTemps(cUnit);
3249
3250 loadConstant(cUnit, r1, (int) callsiteInfo->clazz);
3251
3252 loadWordDisp(cUnit, r0, offsetof(Object, clazz), r2);
3253 /* Branch to the slow path if classes are not equal */
3254 opRegReg(cUnit, kOpCmp, r1, r2);
3255 /*
3256 * Set the misPredBranchOver target so that it will be generated when the
3257 * code for the non-optimized invoke is generated.
3258 */
3259 *classCheck = opCondBranch(cUnit, kArmCondNe);
3260}
3261
Ben Chengba4fc8b2009-06-01 13:00:29 -07003262static bool handleFmt35ms_3rms(CompilationUnit *cUnit, MIR *mir,
Bill Buzbee89efc3d2009-07-28 11:22:22 -07003263 BasicBlock *bb, ArmLIR *labelList)
Ben Chengba4fc8b2009-06-01 13:00:29 -07003264{
Bill Buzbee89efc3d2009-07-28 11:22:22 -07003265 ArmLIR *pcrLabel = NULL;
Ben Chengba4fc8b2009-06-01 13:00:29 -07003266
Ben Cheng7a2697d2010-06-07 13:44:23 -07003267 /* An invoke with the MIR_INLINED is effectively a no-op */
3268 if (mir->OptimizationFlags & MIR_INLINED)
3269 return false;
3270
Ben Chengba4fc8b2009-06-01 13:00:29 -07003271 DecodedInstruction *dInsn = &mir->dalvikInsn;
Dan Bornstein9a1f8162010-12-01 17:02:26 -08003272 switch (mir->dalvikInsn.opcode) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07003273 /* calleeMethod = this->clazz->vtable[BBBB] */
3274 case OP_INVOKE_VIRTUAL_QUICK_RANGE:
3275 case OP_INVOKE_VIRTUAL_QUICK: {
3276 int methodIndex = dInsn->vB;
Bill Buzbeea8589332010-12-27 09:31:21 -08003277 ArmLIR *retChainingCell = &labelList[bb->fallThrough->id];
3278 ArmLIR *predChainingCell = &labelList[bb->taken->id];
Ben Cheng7a2697d2010-06-07 13:44:23 -07003279
3280 /*
3281 * If the invoke has non-null misPredBranchOver, we need to generate
3282 * the non-inlined version of the invoke here to handle the
3283 * mispredicted case.
3284 */
3285 if (mir->meta.callsiteInfo->misPredBranchOver) {
3286 genLandingPadForMispredictedCallee(cUnit, mir, bb, labelList);
3287 }
3288
Dan Bornstein9a1f8162010-12-01 17:02:26 -08003289 if (mir->dalvikInsn.opcode == OP_INVOKE_VIRTUAL_QUICK)
Ben Chengba4fc8b2009-06-01 13:00:29 -07003290 genProcessArgsNoRange(cUnit, mir, dInsn, &pcrLabel);
3291 else
3292 genProcessArgsRange(cUnit, mir, dInsn, &pcrLabel);
3293
Ben Chengcfdeca32011-01-14 11:36:46 -08003294
3295 if (mir->OptimizationFlags & MIR_INVOKE_METHOD_JIT) {
3296 const Method *calleeMethod = mir->meta.callsiteInfo->method;
3297 void *calleeAddr = dvmJitGetMethodAddr(calleeMethod->insns);
3298 if (calleeAddr) {
3299 ArmLIR *classCheck;
3300 cUnit->printMe = true;
3301 genValidationForMethodCallee(cUnit, mir, &classCheck);
3302 newLIR2(cUnit, kThumbBl1, (int) calleeAddr,
3303 (int) calleeAddr);
3304 newLIR2(cUnit, kThumbBl2, (int) calleeAddr,
3305 (int) calleeAddr);
3306 genUnconditionalBranch(cUnit, retChainingCell);
3307
3308 /* Target of slow path */
3309 ArmLIR *slowPathLabel = newLIR0(cUnit,
3310 kArmPseudoTargetLabel);
3311
3312 slowPathLabel->defMask = ENCODE_ALL;
3313 classCheck->generic.target = (LIR *) slowPathLabel;
3314 }
3315 }
3316
Ben Cheng38329f52009-07-07 14:19:20 -07003317 genInvokeVirtualCommon(cUnit, mir, methodIndex,
3318 retChainingCell,
3319 predChainingCell,
3320 pcrLabel);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003321 break;
3322 }
3323 /* calleeMethod = method->clazz->super->vtable[BBBB] */
3324 case OP_INVOKE_SUPER_QUICK:
3325 case OP_INVOKE_SUPER_QUICK_RANGE: {
Ben Cheng7a2697d2010-06-07 13:44:23 -07003326 /* Grab the method ptr directly from what the interpreter sees */
3327 const Method *calleeMethod = mir->meta.callsiteInfo->method;
3328 assert(calleeMethod ==
3329 cUnit->method->clazz->super->vtable[dInsn->vB]);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003330
Dan Bornstein9a1f8162010-12-01 17:02:26 -08003331 if (mir->dalvikInsn.opcode == OP_INVOKE_SUPER_QUICK)
Ben Chengba4fc8b2009-06-01 13:00:29 -07003332 genProcessArgsNoRange(cUnit, mir, dInsn, &pcrLabel);
3333 else
3334 genProcessArgsRange(cUnit, mir, dInsn, &pcrLabel);
3335
3336 /* r0 = calleeMethod */
3337 loadConstant(cUnit, r0, (int) calleeMethod);
3338
Ben Cheng38329f52009-07-07 14:19:20 -07003339 genInvokeSingletonCommon(cUnit, mir, bb, labelList, pcrLabel,
3340 calleeMethod);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003341 break;
3342 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07003343 default:
3344 return true;
3345 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07003346 return false;
3347}
3348
3349/*
Bill Buzbeefd023aa2009-11-02 09:23:49 -08003350 * This operation is complex enough that we'll do it partly inline
3351 * and partly with a handler. NOTE: the handler uses hardcoded
3352 * values for string object offsets and must be revisitied if the
3353 * layout changes.
3354 */
3355static bool genInlinedCompareTo(CompilationUnit *cUnit, MIR *mir)
3356{
3357#if defined(USE_GLOBAL_STRING_DEFS)
Elliott Hughes7e914f12011-01-19 18:18:42 -08003358 return handleExecuteInlineC(cUnit, mir);
Bill Buzbeefd023aa2009-11-02 09:23:49 -08003359#else
3360 ArmLIR *rollback;
Bill Buzbeec6f10662010-02-09 11:16:15 -08003361 RegLocation rlThis = dvmCompilerGetSrc(cUnit, mir, 0);
3362 RegLocation rlComp = dvmCompilerGetSrc(cUnit, mir, 1);
Bill Buzbeefd023aa2009-11-02 09:23:49 -08003363
3364 loadValueDirectFixed(cUnit, rlThis, r0);
3365 loadValueDirectFixed(cUnit, rlComp, r1);
3366 /* Test objects for NULL */
3367 rollback = genNullCheck(cUnit, rlThis.sRegLow, r0, mir->offset, NULL);
3368 genNullCheck(cUnit, rlComp.sRegLow, r1, mir->offset, rollback);
3369 /*
3370 * TUNING: we could check for object pointer equality before invoking
3371 * handler. Unclear whether the gain would be worth the added code size
3372 * expansion.
3373 */
3374 genDispatchToHandler(cUnit, TEMPLATE_STRING_COMPARETO);
Bill Buzbeec6f10662010-02-09 11:16:15 -08003375 storeValue(cUnit, inlinedTarget(cUnit, mir, false),
3376 dvmCompilerGetReturn(cUnit));
Elliott Hughes7e914f12011-01-19 18:18:42 -08003377 return false;
Bill Buzbeefd023aa2009-11-02 09:23:49 -08003378#endif
3379}
3380
Elliott Hughes2bdbcb62010-04-12 14:29:37 -07003381static bool genInlinedFastIndexOf(CompilationUnit *cUnit, MIR *mir)
Bill Buzbeefd023aa2009-11-02 09:23:49 -08003382{
3383#if defined(USE_GLOBAL_STRING_DEFS)
Elliott Hughes7e914f12011-01-19 18:18:42 -08003384 return handleExecuteInlineC(cUnit, mir);
Bill Buzbeefd023aa2009-11-02 09:23:49 -08003385#else
Bill Buzbeec6f10662010-02-09 11:16:15 -08003386 RegLocation rlThis = dvmCompilerGetSrc(cUnit, mir, 0);
3387 RegLocation rlChar = dvmCompilerGetSrc(cUnit, mir, 1);
Bill Buzbeefd023aa2009-11-02 09:23:49 -08003388
3389 loadValueDirectFixed(cUnit, rlThis, r0);
3390 loadValueDirectFixed(cUnit, rlChar, r1);
Elliott Hughes2bdbcb62010-04-12 14:29:37 -07003391 RegLocation rlStart = dvmCompilerGetSrc(cUnit, mir, 2);
3392 loadValueDirectFixed(cUnit, rlStart, r2);
Bill Buzbeefd023aa2009-11-02 09:23:49 -08003393 /* Test objects for NULL */
3394 genNullCheck(cUnit, rlThis.sRegLow, r0, mir->offset, NULL);
3395 genDispatchToHandler(cUnit, TEMPLATE_STRING_INDEXOF);
Bill Buzbeec6f10662010-02-09 11:16:15 -08003396 storeValue(cUnit, inlinedTarget(cUnit, mir, false),
3397 dvmCompilerGetReturn(cUnit));
Elliott Hughes7e914f12011-01-19 18:18:42 -08003398 return false;
Bill Buzbeefd023aa2009-11-02 09:23:49 -08003399#endif
3400}
3401
Elliott Hughesee34f592010-04-05 18:13:52 -07003402// Generates an inlined String.isEmpty or String.length.
3403static bool genInlinedStringIsEmptyOrLength(CompilationUnit *cUnit, MIR *mir,
3404 bool isEmpty)
Bill Buzbee1f748632010-03-02 16:14:41 -08003405{
Elliott Hughesee34f592010-04-05 18:13:52 -07003406 // dst = src.length();
Bill Buzbee1f748632010-03-02 16:14:41 -08003407 RegLocation rlObj = dvmCompilerGetSrc(cUnit, mir, 0);
3408 RegLocation rlDest = inlinedTarget(cUnit, mir, false);
3409 rlObj = loadValue(cUnit, rlObj, kCoreReg);
3410 RegLocation rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
3411 genNullCheck(cUnit, rlObj.sRegLow, rlObj.lowReg, mir->offset, NULL);
3412 loadWordDisp(cUnit, rlObj.lowReg, gDvm.offJavaLangString_count,
3413 rlResult.lowReg);
Elliott Hughesee34f592010-04-05 18:13:52 -07003414 if (isEmpty) {
3415 // dst = (dst == 0);
3416 int tReg = dvmCompilerAllocTemp(cUnit);
3417 opRegReg(cUnit, kOpNeg, tReg, rlResult.lowReg);
3418 opRegRegReg(cUnit, kOpAdc, rlResult.lowReg, rlResult.lowReg, tReg);
3419 }
Bill Buzbee1f748632010-03-02 16:14:41 -08003420 storeValue(cUnit, rlDest, rlResult);
3421 return false;
3422}
3423
Elliott Hughesee34f592010-04-05 18:13:52 -07003424static bool genInlinedStringLength(CompilationUnit *cUnit, MIR *mir)
3425{
3426 return genInlinedStringIsEmptyOrLength(cUnit, mir, false);
3427}
3428
3429static bool genInlinedStringIsEmpty(CompilationUnit *cUnit, MIR *mir)
3430{
3431 return genInlinedStringIsEmptyOrLength(cUnit, mir, true);
3432}
3433
Bill Buzbee1f748632010-03-02 16:14:41 -08003434static bool genInlinedStringCharAt(CompilationUnit *cUnit, MIR *mir)
3435{
3436 int contents = offsetof(ArrayObject, contents);
3437 RegLocation rlObj = dvmCompilerGetSrc(cUnit, mir, 0);
3438 RegLocation rlIdx = dvmCompilerGetSrc(cUnit, mir, 1);
3439 RegLocation rlDest = inlinedTarget(cUnit, mir, false);
3440 RegLocation rlResult;
3441 rlObj = loadValue(cUnit, rlObj, kCoreReg);
3442 rlIdx = loadValue(cUnit, rlIdx, kCoreReg);
3443 int regMax = dvmCompilerAllocTemp(cUnit);
3444 int regOff = dvmCompilerAllocTemp(cUnit);
3445 int regPtr = dvmCompilerAllocTemp(cUnit);
3446 ArmLIR *pcrLabel = genNullCheck(cUnit, rlObj.sRegLow, rlObj.lowReg,
3447 mir->offset, NULL);
3448 loadWordDisp(cUnit, rlObj.lowReg, gDvm.offJavaLangString_count, regMax);
3449 loadWordDisp(cUnit, rlObj.lowReg, gDvm.offJavaLangString_offset, regOff);
3450 loadWordDisp(cUnit, rlObj.lowReg, gDvm.offJavaLangString_value, regPtr);
3451 genBoundsCheck(cUnit, rlIdx.lowReg, regMax, mir->offset, pcrLabel);
3452 dvmCompilerFreeTemp(cUnit, regMax);
3453 opRegImm(cUnit, kOpAdd, regPtr, contents);
3454 opRegReg(cUnit, kOpAdd, regOff, rlIdx.lowReg);
3455 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
3456 loadBaseIndexed(cUnit, regPtr, regOff, rlResult.lowReg, 1, kUnsignedHalf);
3457 storeValue(cUnit, rlDest, rlResult);
3458 return false;
3459}
3460
3461static bool genInlinedAbsInt(CompilationUnit *cUnit, MIR *mir)
3462{
3463 RegLocation rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
3464 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
Elliott Hughese22bd842010-08-20 18:47:36 -07003465 RegLocation rlDest = inlinedTarget(cUnit, mir, false);
Bill Buzbee1f748632010-03-02 16:14:41 -08003466 RegLocation rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
3467 int signReg = dvmCompilerAllocTemp(cUnit);
3468 /*
3469 * abs(x) = y<=x>>31, (x+y)^y.
3470 * Thumb2's IT block also yields 3 instructions, but imposes
3471 * scheduling constraints.
3472 */
3473 opRegRegImm(cUnit, kOpAsr, signReg, rlSrc.lowReg, 31);
3474 opRegRegReg(cUnit, kOpAdd, rlResult.lowReg, rlSrc.lowReg, signReg);
3475 opRegReg(cUnit, kOpXor, rlResult.lowReg, signReg);
3476 storeValue(cUnit, rlDest, rlResult);
3477 return false;
3478}
3479
3480static bool genInlinedAbsLong(CompilationUnit *cUnit, MIR *mir)
3481{
3482 RegLocation rlSrc = dvmCompilerGetSrcWide(cUnit, mir, 0, 1);
3483 RegLocation rlDest = inlinedTargetWide(cUnit, mir, false);
3484 rlSrc = loadValueWide(cUnit, rlSrc, kCoreReg);
3485 RegLocation rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
3486 int signReg = dvmCompilerAllocTemp(cUnit);
3487 /*
3488 * abs(x) = y<=x>>31, (x+y)^y.
3489 * Thumb2 IT block allows slightly shorter sequence,
3490 * but introduces a scheduling barrier. Stick with this
3491 * mechanism for now.
3492 */
3493 opRegRegImm(cUnit, kOpAsr, signReg, rlSrc.highReg, 31);
3494 opRegRegReg(cUnit, kOpAdd, rlResult.lowReg, rlSrc.lowReg, signReg);
3495 opRegRegReg(cUnit, kOpAdc, rlResult.highReg, rlSrc.highReg, signReg);
3496 opRegReg(cUnit, kOpXor, rlResult.lowReg, signReg);
3497 opRegReg(cUnit, kOpXor, rlResult.highReg, signReg);
3498 storeValueWide(cUnit, rlDest, rlResult);
3499 return false;
3500}
Bill Buzbeefd023aa2009-11-02 09:23:49 -08003501
Elliott Hughese22bd842010-08-20 18:47:36 -07003502static bool genInlinedIntFloatConversion(CompilationUnit *cUnit, MIR *mir)
3503{
3504 // Just move from source to destination...
3505 RegLocation rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
3506 RegLocation rlDest = inlinedTarget(cUnit, mir, false);
3507 storeValue(cUnit, rlDest, rlSrc);
3508 return false;
3509}
3510
3511static bool genInlinedLongDoubleConversion(CompilationUnit *cUnit, MIR *mir)
3512{
3513 // Just move from source to destination...
3514 RegLocation rlSrc = dvmCompilerGetSrcWide(cUnit, mir, 0, 1);
3515 RegLocation rlDest = inlinedTargetWide(cUnit, mir, false);
3516 storeValueWide(cUnit, rlDest, rlSrc);
3517 return false;
3518}
3519
Bill Buzbeefd023aa2009-11-02 09:23:49 -08003520/*
Elliott Hughes7e914f12011-01-19 18:18:42 -08003521 * JITs a call to a C function.
3522 * TODO: use this for faster native method invocation for simple native
3523 * methods (http://b/3069458).
3524 */
3525static bool handleExecuteInlineC(CompilationUnit *cUnit, MIR *mir)
3526{
3527 DecodedInstruction *dInsn = &mir->dalvikInsn;
3528 int operation = dInsn->vB;
3529 unsigned int i;
3530 const InlineOperation* inLineTable = dvmGetInlineOpsTable();
3531 uintptr_t fn = (int) inLineTable[operation].func;
3532 if (fn == 0) {
3533 dvmCompilerAbort(cUnit);
3534 }
3535 dvmCompilerFlushAllRegs(cUnit); /* Everything to home location */
3536 dvmCompilerClobberCallRegs(cUnit);
3537 dvmCompilerClobber(cUnit, r4PC);
3538 dvmCompilerClobber(cUnit, r7);
3539 int offset = offsetof(InterpState, retval);
3540 opRegRegImm(cUnit, kOpAdd, r4PC, rGLUE, offset);
3541 opImm(cUnit, kOpPush, (1<<r4PC) | (1<<r7));
3542 LOAD_FUNC_ADDR(cUnit, r4PC, fn);
3543 genExportPC(cUnit, mir);
3544 for (i=0; i < dInsn->vA; i++) {
3545 loadValueDirect(cUnit, dvmCompilerGetSrc(cUnit, mir, i), i);
3546 }
3547 opReg(cUnit, kOpBlx, r4PC);
3548 opRegImm(cUnit, kOpAdd, r13, 8);
3549 /* NULL? */
3550 ArmLIR *branchOver = genCmpImmBranch(cUnit, kArmCondNe, r0, 0);
3551 loadConstant(cUnit, r0, (int) (cUnit->method->insns + mir->offset));
3552 genDispatchToHandler(cUnit, TEMPLATE_THROW_EXCEPTION_COMMON);
3553 ArmLIR *target = newLIR0(cUnit, kArmPseudoTargetLabel);
3554 target->defMask = ENCODE_ALL;
3555 branchOver->generic.target = (LIR *) target;
3556 return false;
3557}
3558
3559/*
Bill Buzbeece46c942009-11-20 15:41:34 -08003560 * NOTE: Handles both range and non-range versions (arguments
3561 * have already been normalized by this point).
Ben Chengba4fc8b2009-06-01 13:00:29 -07003562 */
Bill Buzbeece46c942009-11-20 15:41:34 -08003563static bool handleExecuteInline(CompilationUnit *cUnit, MIR *mir)
Ben Chengba4fc8b2009-06-01 13:00:29 -07003564{
3565 DecodedInstruction *dInsn = &mir->dalvikInsn;
Elliott Hughes7e914f12011-01-19 18:18:42 -08003566 assert(dInsn->opcode == OP_EXECUTE_INLINE_RANGE ||
3567 dInsn->opcode == OP_EXECUTE_INLINE);
3568 switch (dInsn->vB) {
3569 case INLINE_EMPTYINLINEMETHOD:
3570 return false; /* Nop */
3571
3572 /* These ones we potentially JIT inline. */
3573 case INLINE_STRING_LENGTH:
3574 return genInlinedStringLength(cUnit, mir);
3575 case INLINE_STRING_IS_EMPTY:
3576 return genInlinedStringIsEmpty(cUnit, mir);
3577 case INLINE_MATH_ABS_INT:
3578 return genInlinedAbsInt(cUnit, mir);
3579 case INLINE_MATH_ABS_LONG:
3580 return genInlinedAbsLong(cUnit, mir);
3581 case INLINE_MATH_MIN_INT:
3582 return genInlinedMinMaxInt(cUnit, mir, true);
3583 case INLINE_MATH_MAX_INT:
3584 return genInlinedMinMaxInt(cUnit, mir, false);
3585 case INLINE_STRING_CHARAT:
3586 return genInlinedStringCharAt(cUnit, mir);
3587 case INLINE_MATH_SQRT:
3588 return genInlineSqrt(cUnit, mir);
3589 case INLINE_MATH_ABS_FLOAT:
3590 return genInlinedAbsFloat(cUnit, mir);
3591 case INLINE_MATH_ABS_DOUBLE:
3592 return genInlinedAbsDouble(cUnit, mir);
3593 case INLINE_STRING_COMPARETO:
3594 return genInlinedCompareTo(cUnit, mir);
3595 case INLINE_STRING_FASTINDEXOF_II:
3596 return genInlinedFastIndexOf(cUnit, mir);
3597 case INLINE_FLOAT_TO_RAW_INT_BITS:
3598 case INLINE_INT_BITS_TO_FLOAT:
3599 return genInlinedIntFloatConversion(cUnit, mir);
3600 case INLINE_DOUBLE_TO_RAW_LONG_BITS:
3601 case INLINE_LONG_BITS_TO_DOUBLE:
3602 return genInlinedLongDoubleConversion(cUnit, mir);
3603
3604 /*
3605 * These ones we just JIT a call to a C function for.
3606 * TODO: special-case these in the other "invoke" call paths.
3607 */
3608 case INLINE_STRING_EQUALS:
3609 case INLINE_MATH_COS:
3610 case INLINE_MATH_SIN:
3611 case INLINE_FLOAT_TO_INT_BITS:
3612 case INLINE_DOUBLE_TO_LONG_BITS:
3613 return handleExecuteInlineC(cUnit, mir);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003614 }
Elliott Hughes7e914f12011-01-19 18:18:42 -08003615 dvmCompilerAbort(cUnit);
3616 return false; // Not reachable; keeps compiler happy.
Ben Chengba4fc8b2009-06-01 13:00:29 -07003617}
3618
3619static bool handleFmt51l(CompilationUnit *cUnit, MIR *mir)
3620{
Bill Buzbee1465db52009-09-23 17:17:35 -07003621 //TUNING: We're using core regs here - not optimal when target is a double
Bill Buzbeec6f10662010-02-09 11:16:15 -08003622 RegLocation rlDest = dvmCompilerGetDestWide(cUnit, mir, 0, 1);
3623 RegLocation rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Ben Chengbd1326d2010-04-02 15:04:53 -07003624 loadConstantNoClobber(cUnit, rlResult.lowReg,
3625 mir->dalvikInsn.vB_wide & 0xFFFFFFFFUL);
3626 loadConstantNoClobber(cUnit, rlResult.highReg,
3627 (mir->dalvikInsn.vB_wide>>32) & 0xFFFFFFFFUL);
Bill Buzbee1465db52009-09-23 17:17:35 -07003628 storeValueWide(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003629 return false;
3630}
3631
Ben Chengba4fc8b2009-06-01 13:00:29 -07003632/*
3633 * The following are special processing routines that handle transfer of
3634 * controls between compiled code and the interpreter. Certain VM states like
3635 * Dalvik PC and special-purpose registers are reconstructed here.
3636 */
3637
Bill Buzbeebd047242010-05-13 13:02:53 -07003638/*
3639 * Insert a
3640 * b .+4
3641 * nop
3642 * pair at the beginning of a chaining cell. This serves as the
3643 * switch branch that selects between reverting to the interpreter or
3644 * not. Once the cell is chained to a translation, the cell will
3645 * contain a 32-bit branch. Subsequent chain/unchain operations will
3646 * then only alter that first 16-bits - the "b .+4" for unchaining,
3647 * and the restoration of the first half of the 32-bit branch for
3648 * rechaining.
3649 */
3650static void insertChainingSwitch(CompilationUnit *cUnit)
3651{
3652 ArmLIR *branch = newLIR0(cUnit, kThumbBUncond);
3653 newLIR2(cUnit, kThumbOrr, r0, r0);
3654 ArmLIR *target = newLIR0(cUnit, kArmPseudoTargetLabel);
3655 target->defMask = ENCODE_ALL;
3656 branch->generic.target = (LIR *) target;
3657}
3658
Ben Cheng1efc9c52009-06-08 18:25:27 -07003659/* Chaining cell for code that may need warmup. */
3660static void handleNormalChainingCell(CompilationUnit *cUnit,
3661 unsigned int offset)
Ben Chengba4fc8b2009-06-01 13:00:29 -07003662{
Ben Cheng11d8f142010-03-24 15:24:19 -07003663 /*
3664 * Use raw instruction constructors to guarantee that the generated
3665 * instructions fit the predefined cell size.
3666 */
Bill Buzbeebd047242010-05-13 13:02:53 -07003667 insertChainingSwitch(cUnit);
Ben Cheng11d8f142010-03-24 15:24:19 -07003668 newLIR3(cUnit, kThumbLdrRRI5, r0, rGLUE,
3669 offsetof(InterpState,
3670 jitToInterpEntries.dvmJitToInterpNormal) >> 2);
3671 newLIR1(cUnit, kThumbBlxR, r0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003672 addWordData(cUnit, (int) (cUnit->method->insns + offset), true);
3673}
3674
3675/*
Ben Cheng1efc9c52009-06-08 18:25:27 -07003676 * Chaining cell for instructions that immediately following already translated
3677 * code.
Ben Chengba4fc8b2009-06-01 13:00:29 -07003678 */
Ben Cheng1efc9c52009-06-08 18:25:27 -07003679static void handleHotChainingCell(CompilationUnit *cUnit,
3680 unsigned int offset)
Ben Chengba4fc8b2009-06-01 13:00:29 -07003681{
Ben Cheng11d8f142010-03-24 15:24:19 -07003682 /*
3683 * Use raw instruction constructors to guarantee that the generated
3684 * instructions fit the predefined cell size.
3685 */
Bill Buzbeebd047242010-05-13 13:02:53 -07003686 insertChainingSwitch(cUnit);
Ben Cheng11d8f142010-03-24 15:24:19 -07003687 newLIR3(cUnit, kThumbLdrRRI5, r0, rGLUE,
3688 offsetof(InterpState,
3689 jitToInterpEntries.dvmJitToInterpTraceSelect) >> 2);
3690 newLIR1(cUnit, kThumbBlxR, r0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003691 addWordData(cUnit, (int) (cUnit->method->insns + offset), true);
3692}
3693
Jeff Hao97319a82009-08-12 16:57:15 -07003694/* Chaining cell for branches that branch back into the same basic block */
3695static void handleBackwardBranchChainingCell(CompilationUnit *cUnit,
3696 unsigned int offset)
3697{
Ben Cheng11d8f142010-03-24 15:24:19 -07003698 /*
3699 * Use raw instruction constructors to guarantee that the generated
3700 * instructions fit the predefined cell size.
3701 */
Bill Buzbeebd047242010-05-13 13:02:53 -07003702 insertChainingSwitch(cUnit);
Bill Buzbee9c4b7c82009-09-10 10:10:38 -07003703#if defined(WITH_SELF_VERIFICATION)
Bill Buzbee1465db52009-09-23 17:17:35 -07003704 newLIR3(cUnit, kThumbLdrRRI5, r0, rGLUE,
Ben Cheng40094c12010-02-24 20:58:44 -08003705 offsetof(InterpState,
3706 jitToInterpEntries.dvmJitToInterpBackwardBranch) >> 2);
Bill Buzbee9c4b7c82009-09-10 10:10:38 -07003707#else
Bill Buzbee1465db52009-09-23 17:17:35 -07003708 newLIR3(cUnit, kThumbLdrRRI5, r0, rGLUE,
Bill Buzbee9c4b7c82009-09-10 10:10:38 -07003709 offsetof(InterpState, jitToInterpEntries.dvmJitToInterpNormal) >> 2);
3710#endif
Bill Buzbee1465db52009-09-23 17:17:35 -07003711 newLIR1(cUnit, kThumbBlxR, r0);
Jeff Hao97319a82009-08-12 16:57:15 -07003712 addWordData(cUnit, (int) (cUnit->method->insns + offset), true);
3713}
3714
Ben Chengba4fc8b2009-06-01 13:00:29 -07003715/* Chaining cell for monomorphic method invocations. */
Ben Cheng38329f52009-07-07 14:19:20 -07003716static void handleInvokeSingletonChainingCell(CompilationUnit *cUnit,
3717 const Method *callee)
Ben Chengba4fc8b2009-06-01 13:00:29 -07003718{
Ben Cheng11d8f142010-03-24 15:24:19 -07003719 /*
3720 * Use raw instruction constructors to guarantee that the generated
3721 * instructions fit the predefined cell size.
3722 */
Bill Buzbeebd047242010-05-13 13:02:53 -07003723 insertChainingSwitch(cUnit);
Ben Cheng11d8f142010-03-24 15:24:19 -07003724 newLIR3(cUnit, kThumbLdrRRI5, r0, rGLUE,
3725 offsetof(InterpState,
3726 jitToInterpEntries.dvmJitToInterpTraceSelect) >> 2);
3727 newLIR1(cUnit, kThumbBlxR, r0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003728 addWordData(cUnit, (int) (callee->insns), true);
3729}
3730
Ben Cheng38329f52009-07-07 14:19:20 -07003731/* Chaining cell for monomorphic method invocations. */
3732static void handleInvokePredictedChainingCell(CompilationUnit *cUnit)
3733{
3734
3735 /* Should not be executed in the initial state */
3736 addWordData(cUnit, PREDICTED_CHAIN_BX_PAIR_INIT, true);
3737 /* To be filled: class */
3738 addWordData(cUnit, PREDICTED_CHAIN_CLAZZ_INIT, true);
3739 /* To be filled: method */
3740 addWordData(cUnit, PREDICTED_CHAIN_METHOD_INIT, true);
3741 /*
3742 * Rechain count. The initial value of 0 here will trigger chaining upon
3743 * the first invocation of this callsite.
3744 */
3745 addWordData(cUnit, PREDICTED_CHAIN_COUNTER_INIT, true);
3746}
3747
Ben Chengba4fc8b2009-06-01 13:00:29 -07003748/* Load the Dalvik PC into r0 and jump to the specified target */
3749static void handlePCReconstruction(CompilationUnit *cUnit,
Bill Buzbee89efc3d2009-07-28 11:22:22 -07003750 ArmLIR *targetLabel)
Ben Chengba4fc8b2009-06-01 13:00:29 -07003751{
Bill Buzbee89efc3d2009-07-28 11:22:22 -07003752 ArmLIR **pcrLabel =
3753 (ArmLIR **) cUnit->pcReconstructionList.elemList;
Ben Chengba4fc8b2009-06-01 13:00:29 -07003754 int numElems = cUnit->pcReconstructionList.numUsed;
3755 int i;
3756 for (i = 0; i < numElems; i++) {
3757 dvmCompilerAppendLIR(cUnit, (LIR *) pcrLabel[i]);
3758 /* r0 = dalvik PC */
3759 loadConstant(cUnit, r0, pcrLabel[i]->operands[0]);
3760 genUnconditionalBranch(cUnit, targetLabel);
3761 }
3762}
3763
Bill Buzbee1465db52009-09-23 17:17:35 -07003764static char *extendedMIROpNames[kMirOpLast - kMirOpFirst] = {
3765 "kMirOpPhi",
3766 "kMirOpNullNRangeUpCheck",
3767 "kMirOpNullNRangeDownCheck",
3768 "kMirOpLowerBound",
3769 "kMirOpPunt",
Ben Cheng7a2697d2010-06-07 13:44:23 -07003770 "kMirOpCheckInlinePrediction",
Ben Cheng4238ec22009-08-24 16:32:22 -07003771};
3772
3773/*
3774 * vA = arrayReg;
3775 * vB = idxReg;
3776 * vC = endConditionReg;
3777 * arg[0] = maxC
3778 * arg[1] = minC
3779 * arg[2] = loopBranchConditionCode
3780 */
3781static void genHoistedChecksForCountUpLoop(CompilationUnit *cUnit, MIR *mir)
3782{
Bill Buzbee1465db52009-09-23 17:17:35 -07003783 /*
3784 * NOTE: these synthesized blocks don't have ssa names assigned
3785 * for Dalvik registers. However, because they dominate the following
3786 * blocks we can simply use the Dalvik name w/ subscript 0 as the
3787 * ssa name.
3788 */
Ben Cheng4238ec22009-08-24 16:32:22 -07003789 DecodedInstruction *dInsn = &mir->dalvikInsn;
3790 const int lenOffset = offsetof(ArrayObject, length);
Ben Cheng4238ec22009-08-24 16:32:22 -07003791 const int maxC = dInsn->arg[0];
Bill Buzbee1465db52009-09-23 17:17:35 -07003792 int regLength;
3793 RegLocation rlArray = cUnit->regLocation[mir->dalvikInsn.vA];
3794 RegLocation rlIdxEnd = cUnit->regLocation[mir->dalvikInsn.vC];
Ben Cheng4238ec22009-08-24 16:32:22 -07003795
3796 /* regArray <- arrayRef */
Bill Buzbee1465db52009-09-23 17:17:35 -07003797 rlArray = loadValue(cUnit, rlArray, kCoreReg);
3798 rlIdxEnd = loadValue(cUnit, rlIdxEnd, kCoreReg);
3799 genRegImmCheck(cUnit, kArmCondEq, rlArray.lowReg, 0, 0,
Ben Cheng4238ec22009-08-24 16:32:22 -07003800 (ArmLIR *) cUnit->loopAnalysis->branchToPCR);
3801
3802 /* regLength <- len(arrayRef) */
Bill Buzbeec6f10662010-02-09 11:16:15 -08003803 regLength = dvmCompilerAllocTemp(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07003804 loadWordDisp(cUnit, rlArray.lowReg, lenOffset, regLength);
Ben Cheng4238ec22009-08-24 16:32:22 -07003805
3806 int delta = maxC;
3807 /*
3808 * If the loop end condition is ">=" instead of ">", then the largest value
3809 * of the index is "endCondition - 1".
3810 */
3811 if (dInsn->arg[2] == OP_IF_GE) {
3812 delta--;
3813 }
3814
3815 if (delta) {
Bill Buzbeec6f10662010-02-09 11:16:15 -08003816 int tReg = dvmCompilerAllocTemp(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07003817 opRegRegImm(cUnit, kOpAdd, tReg, rlIdxEnd.lowReg, delta);
3818 rlIdxEnd.lowReg = tReg;
Bill Buzbeec6f10662010-02-09 11:16:15 -08003819 dvmCompilerFreeTemp(cUnit, tReg);
Ben Cheng4238ec22009-08-24 16:32:22 -07003820 }
3821 /* Punt if "regIdxEnd < len(Array)" is false */
Bill Buzbee1465db52009-09-23 17:17:35 -07003822 genRegRegCheck(cUnit, kArmCondGe, rlIdxEnd.lowReg, regLength, 0,
Ben Cheng0fd31e42009-09-03 14:40:16 -07003823 (ArmLIR *) cUnit->loopAnalysis->branchToPCR);
Ben Cheng4238ec22009-08-24 16:32:22 -07003824}
3825
3826/*
3827 * vA = arrayReg;
3828 * vB = idxReg;
3829 * vC = endConditionReg;
3830 * arg[0] = maxC
3831 * arg[1] = minC
3832 * arg[2] = loopBranchConditionCode
3833 */
3834static void genHoistedChecksForCountDownLoop(CompilationUnit *cUnit, MIR *mir)
3835{
3836 DecodedInstruction *dInsn = &mir->dalvikInsn;
3837 const int lenOffset = offsetof(ArrayObject, length);
Bill Buzbeec6f10662010-02-09 11:16:15 -08003838 const int regLength = dvmCompilerAllocTemp(cUnit);
Ben Cheng4238ec22009-08-24 16:32:22 -07003839 const int maxC = dInsn->arg[0];
Bill Buzbee1465db52009-09-23 17:17:35 -07003840 RegLocation rlArray = cUnit->regLocation[mir->dalvikInsn.vA];
3841 RegLocation rlIdxInit = cUnit->regLocation[mir->dalvikInsn.vB];
Ben Cheng4238ec22009-08-24 16:32:22 -07003842
3843 /* regArray <- arrayRef */
Bill Buzbee1465db52009-09-23 17:17:35 -07003844 rlArray = loadValue(cUnit, rlArray, kCoreReg);
3845 rlIdxInit = loadValue(cUnit, rlIdxInit, kCoreReg);
3846 genRegImmCheck(cUnit, kArmCondEq, rlArray.lowReg, 0, 0,
Ben Cheng4238ec22009-08-24 16:32:22 -07003847 (ArmLIR *) cUnit->loopAnalysis->branchToPCR);
3848
3849 /* regLength <- len(arrayRef) */
Bill Buzbee1465db52009-09-23 17:17:35 -07003850 loadWordDisp(cUnit, rlArray.lowReg, lenOffset, regLength);
Ben Cheng4238ec22009-08-24 16:32:22 -07003851
3852 if (maxC) {
Bill Buzbeec6f10662010-02-09 11:16:15 -08003853 int tReg = dvmCompilerAllocTemp(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07003854 opRegRegImm(cUnit, kOpAdd, tReg, rlIdxInit.lowReg, maxC);
3855 rlIdxInit.lowReg = tReg;
Bill Buzbeec6f10662010-02-09 11:16:15 -08003856 dvmCompilerFreeTemp(cUnit, tReg);
Ben Cheng4238ec22009-08-24 16:32:22 -07003857 }
3858
3859 /* Punt if "regIdxInit < len(Array)" is false */
Bill Buzbee1465db52009-09-23 17:17:35 -07003860 genRegRegCheck(cUnit, kArmCondGe, rlIdxInit.lowReg, regLength, 0,
Ben Cheng0fd31e42009-09-03 14:40:16 -07003861 (ArmLIR *) cUnit->loopAnalysis->branchToPCR);
Ben Cheng4238ec22009-08-24 16:32:22 -07003862}
3863
3864/*
3865 * vA = idxReg;
3866 * vB = minC;
3867 */
3868static void genHoistedLowerBoundCheck(CompilationUnit *cUnit, MIR *mir)
3869{
3870 DecodedInstruction *dInsn = &mir->dalvikInsn;
Ben Cheng4238ec22009-08-24 16:32:22 -07003871 const int minC = dInsn->vB;
Bill Buzbee1465db52009-09-23 17:17:35 -07003872 RegLocation rlIdx = cUnit->regLocation[mir->dalvikInsn.vA];
Ben Cheng4238ec22009-08-24 16:32:22 -07003873
3874 /* regIdx <- initial index value */
Bill Buzbee1465db52009-09-23 17:17:35 -07003875 rlIdx = loadValue(cUnit, rlIdx, kCoreReg);
Ben Cheng4238ec22009-08-24 16:32:22 -07003876
3877 /* Punt if "regIdxInit + minC >= 0" is false */
Bill Buzbee1465db52009-09-23 17:17:35 -07003878 genRegImmCheck(cUnit, kArmCondLt, rlIdx.lowReg, -minC, 0,
Ben Cheng4238ec22009-08-24 16:32:22 -07003879 (ArmLIR *) cUnit->loopAnalysis->branchToPCR);
3880}
3881
Ben Cheng7a2697d2010-06-07 13:44:23 -07003882/*
3883 * vC = this
3884 *
3885 * A predicted inlining target looks like the following, where instructions
3886 * between 0x4858de66 and 0x4858de72 are checking if the predicted class
3887 * matches "this", and the verificaion code is generated by this routine.
3888 *
3889 * (C) means the instruction is inlined from the callee, and (PI) means the
3890 * instruction is the predicted inlined invoke, whose corresponding
3891 * instructions are still generated to handle the mispredicted case.
3892 *
3893 * D/dalvikvm( 86): -------- kMirOpCheckInlinePrediction
3894 * D/dalvikvm( 86): 0x4858de66 (0002): ldr r0, [r5, #68]
3895 * D/dalvikvm( 86): 0x4858de68 (0004): ldr r1, [pc, #140]
3896 * D/dalvikvm( 86): 0x4858de6a (0006): cmp r0, #0
3897 * D/dalvikvm( 86): 0x4858de6c (0008): beq 0x4858deb2
3898 * D/dalvikvm( 86): 0x4858de6e (000a): ldr r2, [r0, #0]
3899 * D/dalvikvm( 86): 0x4858de70 (000c): cmp r1, r2
3900 * D/dalvikvm( 86): 0x4858de72 (000e): bne 0x4858de7a
3901 * D/dalvikvm( 86): -------- dalvik offset: 0x004c @ +iget-object-quick (C)
3902 * v4, v17, (#8)
3903 * D/dalvikvm( 86): 0x4858de74 (0010): ldr r3, [r0, #8]
3904 * D/dalvikvm( 86): 0x4858de76 (0012): str r3, [r5, #16]
3905 * D/dalvikvm( 86): -------- dalvik offset: 0x004c @
3906 * +invoke-virtual-quick/range (PI) v17..v17
3907 * D/dalvikvm( 86): 0x4858de78 (0014): b 0x4858debc
3908 * D/dalvikvm( 86): 0x4858de7a (0016): add r4,r5,#68
3909 * D/dalvikvm( 86): -------- BARRIER
3910 * D/dalvikvm( 86): 0x4858de7e (001a): ldmia r4, <r0>
3911 * D/dalvikvm( 86): -------- BARRIER
3912 * D/dalvikvm( 86): 0x4858de80 (001c): sub r7,r5,#24
3913 * D/dalvikvm( 86): 0x4858de84 (0020): cmp r0, #0
3914 * D/dalvikvm( 86): 0x4858de86 (0022): beq 0x4858deb6
3915 * D/dalvikvm( 86): -------- BARRIER
3916 * D/dalvikvm( 86): 0x4858de88 (0024): stmia r7, <r0>
3917 * D/dalvikvm( 86): -------- BARRIER
3918 * D/dalvikvm( 86): 0x4858de8a (0026): ldr r4, [pc, #104]
3919 * D/dalvikvm( 86): 0x4858de8c (0028): add r1, pc, #28
3920 * D/dalvikvm( 86): 0x4858de8e (002a): add r2, pc, #56
3921 * D/dalvikvm( 86): 0x4858de90 (002c): blx_1 0x48589198
3922 * D/dalvikvm( 86): 0x4858de92 (002e): blx_2 see above
3923 * D/dalvikvm( 86): 0x4858de94 (0030): b 0x4858dec8
3924 * D/dalvikvm( 86): 0x4858de96 (0032): b 0x4858deb6
3925 * D/dalvikvm( 86): 0x4858de98 (0034): ldr r0, [r7, #72]
3926 * D/dalvikvm( 86): 0x4858de9a (0036): cmp r1, #0
3927 * D/dalvikvm( 86): 0x4858de9c (0038): bgt 0x4858dea4
3928 * D/dalvikvm( 86): 0x4858de9e (003a): ldr r7, [r6, #116]
3929 * D/dalvikvm( 86): 0x4858dea0 (003c): movs r1, r6
3930 * D/dalvikvm( 86): 0x4858dea2 (003e): blx r7
3931 * D/dalvikvm( 86): 0x4858dea4 (0040): add r1, pc, #4
3932 * D/dalvikvm( 86): 0x4858dea6 (0042): blx_1 0x485890a0
3933 * D/dalvikvm( 86): 0x4858dea8 (0044): blx_2 see above
3934 * D/dalvikvm( 86): 0x4858deaa (0046): b 0x4858deb6
3935 * D/dalvikvm( 86): 0x4858deac (0048): .align4
3936 * D/dalvikvm( 86): L0x004f:
3937 * D/dalvikvm( 86): -------- dalvik offset: 0x004f @ move-result-object (PI)
3938 * v4, (#0), (#0)
3939 * D/dalvikvm( 86): 0x4858deac (0048): ldr r4, [r6, #8]
3940 * D/dalvikvm( 86): 0x4858deae (004a): str r4, [r5, #16]
3941 * D/dalvikvm( 86): 0x4858deb0 (004c): b 0x4858debc
3942 * D/dalvikvm( 86): -------- reconstruct dalvik PC : 0x42beefcc @ +0x004c
3943 * D/dalvikvm( 86): 0x4858deb2 (004e): ldr r0, [pc, #64]
3944 * D/dalvikvm( 86): 0x4858deb4 (0050): b 0x4858deb8
3945 * D/dalvikvm( 86): -------- reconstruct dalvik PC : 0x42beefcc @ +0x004c
3946 * D/dalvikvm( 86): 0x4858deb6 (0052): ldr r0, [pc, #60]
3947 * D/dalvikvm( 86): Exception_Handling:
3948 * D/dalvikvm( 86): 0x4858deb8 (0054): ldr r1, [r6, #100]
3949 * D/dalvikvm( 86): 0x4858deba (0056): blx r1
3950 * D/dalvikvm( 86): 0x4858debc (0058): .align4
3951 * D/dalvikvm( 86): -------- chaining cell (hot): 0x0050
3952 * D/dalvikvm( 86): 0x4858debc (0058): b 0x4858dec0
3953 * D/dalvikvm( 86): 0x4858debe (005a): orrs r0, r0
3954 * D/dalvikvm( 86): 0x4858dec0 (005c): ldr r0, [r6, #112]
3955 * D/dalvikvm( 86): 0x4858dec2 (005e): blx r0
3956 * D/dalvikvm( 86): 0x4858dec4 (0060): data 0xefd4(61396)
3957 * D/dalvikvm( 86): 0x4858dec6 (0062): data 0x42be(17086)
3958 * D/dalvikvm( 86): 0x4858dec8 (0064): .align4
3959 * D/dalvikvm( 86): -------- chaining cell (predicted)
3960 * D/dalvikvm( 86): 0x4858dec8 (0064): data 0xe7fe(59390)
3961 * D/dalvikvm( 86): 0x4858deca (0066): data 0x0000(0)
3962 * D/dalvikvm( 86): 0x4858decc (0068): data 0x0000(0)
3963 * D/dalvikvm( 86): 0x4858dece (006a): data 0x0000(0)
3964 * :
3965 */
3966static void genValidationForPredictedInline(CompilationUnit *cUnit, MIR *mir)
3967{
3968 CallsiteInfo *callsiteInfo = mir->meta.callsiteInfo;
3969 RegLocation rlThis = cUnit->regLocation[mir->dalvikInsn.vC];
3970
3971 rlThis = loadValue(cUnit, rlThis, kCoreReg);
3972 int regPredictedClass = dvmCompilerAllocTemp(cUnit);
3973 loadConstant(cUnit, regPredictedClass, (int) callsiteInfo->clazz);
3974 genNullCheck(cUnit, rlThis.sRegLow, rlThis.lowReg, mir->offset,
3975 NULL);/* null object? */
3976 int regActualClass = dvmCompilerAllocTemp(cUnit);
3977 loadWordDisp(cUnit, rlThis.lowReg, offsetof(Object, clazz), regActualClass);
3978 opRegReg(cUnit, kOpCmp, regPredictedClass, regActualClass);
3979 /*
3980 * Set the misPredBranchOver target so that it will be generated when the
3981 * code for the non-optimized invoke is generated.
3982 */
3983 callsiteInfo->misPredBranchOver = (LIR *) opCondBranch(cUnit, kArmCondNe);
3984}
3985
Ben Cheng4238ec22009-08-24 16:32:22 -07003986/* Extended MIR instructions like PHI */
3987static void handleExtendedMIR(CompilationUnit *cUnit, MIR *mir)
3988{
Dan Bornstein9a1f8162010-12-01 17:02:26 -08003989 int opOffset = mir->dalvikInsn.opcode - kMirOpFirst;
Carl Shapirofc75f3e2010-12-07 11:43:38 -08003990 char *msg = (char *)dvmCompilerNew(strlen(extendedMIROpNames[opOffset]) + 1,
3991 false);
Ben Cheng4238ec22009-08-24 16:32:22 -07003992 strcpy(msg, extendedMIROpNames[opOffset]);
Bill Buzbee1465db52009-09-23 17:17:35 -07003993 newLIR1(cUnit, kArmPseudoExtended, (int) msg);
Ben Cheng4238ec22009-08-24 16:32:22 -07003994
Dan Bornstein9a1f8162010-12-01 17:02:26 -08003995 switch (mir->dalvikInsn.opcode) {
Bill Buzbee1465db52009-09-23 17:17:35 -07003996 case kMirOpPhi: {
Ben Cheng4238ec22009-08-24 16:32:22 -07003997 char *ssaString = dvmCompilerGetSSAString(cUnit, mir->ssaRep);
Bill Buzbee1465db52009-09-23 17:17:35 -07003998 newLIR1(cUnit, kArmPseudoSSARep, (int) ssaString);
Ben Cheng4238ec22009-08-24 16:32:22 -07003999 break;
4000 }
Bill Buzbee1465db52009-09-23 17:17:35 -07004001 case kMirOpNullNRangeUpCheck: {
Ben Cheng4238ec22009-08-24 16:32:22 -07004002 genHoistedChecksForCountUpLoop(cUnit, mir);
4003 break;
4004 }
Bill Buzbee1465db52009-09-23 17:17:35 -07004005 case kMirOpNullNRangeDownCheck: {
Ben Cheng4238ec22009-08-24 16:32:22 -07004006 genHoistedChecksForCountDownLoop(cUnit, mir);
4007 break;
4008 }
Bill Buzbee1465db52009-09-23 17:17:35 -07004009 case kMirOpLowerBound: {
Ben Cheng4238ec22009-08-24 16:32:22 -07004010 genHoistedLowerBoundCheck(cUnit, mir);
4011 break;
4012 }
Bill Buzbee1465db52009-09-23 17:17:35 -07004013 case kMirOpPunt: {
Ben Cheng4238ec22009-08-24 16:32:22 -07004014 genUnconditionalBranch(cUnit,
4015 (ArmLIR *) cUnit->loopAnalysis->branchToPCR);
4016 break;
4017 }
Ben Cheng7a2697d2010-06-07 13:44:23 -07004018 case kMirOpCheckInlinePrediction: {
4019 genValidationForPredictedInline(cUnit, mir);
4020 break;
4021 }
Ben Cheng4238ec22009-08-24 16:32:22 -07004022 default:
4023 break;
4024 }
4025}
4026
4027/*
4028 * Create a PC-reconstruction cell for the starting offset of this trace.
4029 * Since the PCR cell is placed near the end of the compiled code which is
4030 * usually out of range for a conditional branch, we put two branches (one
4031 * branch over to the loop body and one layover branch to the actual PCR) at the
4032 * end of the entry block.
4033 */
4034static void setupLoopEntryBlock(CompilationUnit *cUnit, BasicBlock *entry,
4035 ArmLIR *bodyLabel)
4036{
4037 /* Set up the place holder to reconstruct this Dalvik PC */
Carl Shapirofc75f3e2010-12-07 11:43:38 -08004038 ArmLIR *pcrLabel = (ArmLIR *) dvmCompilerNew(sizeof(ArmLIR), true);
Dan Bornstein9a1f8162010-12-01 17:02:26 -08004039 pcrLabel->opcode = kArmPseudoPCReconstructionCell;
Ben Cheng4238ec22009-08-24 16:32:22 -07004040 pcrLabel->operands[0] =
4041 (int) (cUnit->method->insns + entry->startOffset);
4042 pcrLabel->operands[1] = entry->startOffset;
4043 /* Insert the place holder to the growable list */
Ben Cheng00603072010-10-28 11:13:58 -07004044 dvmInsertGrowableList(&cUnit->pcReconstructionList, (intptr_t) pcrLabel);
Ben Cheng4238ec22009-08-24 16:32:22 -07004045
4046 /*
4047 * Next, create two branches - one branch over to the loop body and the
4048 * other branch to the PCR cell to punt.
4049 */
Carl Shapirofc75f3e2010-12-07 11:43:38 -08004050 ArmLIR *branchToBody = (ArmLIR *) dvmCompilerNew(sizeof(ArmLIR), true);
Dan Bornstein9a1f8162010-12-01 17:02:26 -08004051 branchToBody->opcode = kThumbBUncond;
Ben Cheng4238ec22009-08-24 16:32:22 -07004052 branchToBody->generic.target = (LIR *) bodyLabel;
Ben Chengdcf3e5d2009-09-11 13:42:05 -07004053 setupResourceMasks(branchToBody);
Ben Cheng4238ec22009-08-24 16:32:22 -07004054 cUnit->loopAnalysis->branchToBody = (LIR *) branchToBody;
4055
Carl Shapirofc75f3e2010-12-07 11:43:38 -08004056 ArmLIR *branchToPCR = (ArmLIR *) dvmCompilerNew(sizeof(ArmLIR), true);
Dan Bornstein9a1f8162010-12-01 17:02:26 -08004057 branchToPCR->opcode = kThumbBUncond;
Ben Cheng4238ec22009-08-24 16:32:22 -07004058 branchToPCR->generic.target = (LIR *) pcrLabel;
Ben Chengdcf3e5d2009-09-11 13:42:05 -07004059 setupResourceMasks(branchToPCR);
Ben Cheng4238ec22009-08-24 16:32:22 -07004060 cUnit->loopAnalysis->branchToPCR = (LIR *) branchToPCR;
4061}
4062
Ben Chengd5adae12010-03-26 17:45:28 -07004063#if defined(WITH_SELF_VERIFICATION)
4064static bool selfVerificationPuntOps(MIR *mir)
4065{
4066 DecodedInstruction *decInsn = &mir->dalvikInsn;
Dan Bornstein9a1f8162010-12-01 17:02:26 -08004067 Opcode op = decInsn->opcode;
Ben Cheng7a2697d2010-06-07 13:44:23 -07004068
Ben Chengd5adae12010-03-26 17:45:28 -07004069 /*
4070 * All opcodes that can throw exceptions and use the
4071 * TEMPLATE_THROW_EXCEPTION_COMMON template should be excluded in the trace
4072 * under self-verification mode.
4073 */
4074 return (op == OP_MONITOR_ENTER || op == OP_MONITOR_EXIT ||
4075 op == OP_NEW_INSTANCE || op == OP_NEW_ARRAY ||
4076 op == OP_CHECK_CAST || op == OP_MOVE_EXCEPTION ||
4077 op == OP_FILL_ARRAY_DATA || op == OP_EXECUTE_INLINE ||
Ben Cheng7a2697d2010-06-07 13:44:23 -07004078 op == OP_EXECUTE_INLINE_RANGE);
Ben Chengd5adae12010-03-26 17:45:28 -07004079}
4080#endif
4081
Ben Chengba4fc8b2009-06-01 13:00:29 -07004082void dvmCompilerMIR2LIR(CompilationUnit *cUnit)
4083{
4084 /* Used to hold the labels of each block */
Bill Buzbee89efc3d2009-07-28 11:22:22 -07004085 ArmLIR *labelList =
Carl Shapirofc75f3e2010-12-07 11:43:38 -08004086 (ArmLIR *) dvmCompilerNew(sizeof(ArmLIR) * cUnit->numBlocks, true);
Ben Chengcec26f62010-01-15 15:29:33 -08004087 GrowableList chainingListByType[kChainingCellGap];
Ben Chengba4fc8b2009-06-01 13:00:29 -07004088 int i;
4089
4090 /*
Ben Cheng38329f52009-07-07 14:19:20 -07004091 * Initialize various types chaining lists.
Ben Chengba4fc8b2009-06-01 13:00:29 -07004092 */
Ben Chengcec26f62010-01-15 15:29:33 -08004093 for (i = 0; i < kChainingCellGap; i++) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07004094 dvmInitGrowableList(&chainingListByType[i], 2);
4095 }
4096
Ben Cheng00603072010-10-28 11:13:58 -07004097 GrowableListIterator iterator;
4098 dvmGrowableListIteratorInit(&cUnit->blockList, &iterator);
Ben Chengba4fc8b2009-06-01 13:00:29 -07004099
buzbee2e152ba2010-12-15 16:32:35 -08004100 /* Traces start with a profiling entry point. Generate it here */
4101 cUnit->profileCodeSize = genTraceProfileEntry(cUnit);
Ben Cheng1efc9c52009-06-08 18:25:27 -07004102
Ben Chengba4fc8b2009-06-01 13:00:29 -07004103 /* Handle the content in each basic block */
Ben Cheng00603072010-10-28 11:13:58 -07004104 for (i = 0; ; i++) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07004105 MIR *mir;
Ben Cheng00603072010-10-28 11:13:58 -07004106 BasicBlock *bb = (BasicBlock *) dvmGrowableListIteratorNext(&iterator);
4107 if (bb == NULL) break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07004108
Ben Cheng00603072010-10-28 11:13:58 -07004109 labelList[i].operands[0] = bb->startOffset;
Ben Chengba4fc8b2009-06-01 13:00:29 -07004110
Ben Cheng00603072010-10-28 11:13:58 -07004111 if (bb->blockType >= kChainingCellGap) {
4112 if (bb->isFallThroughFromInvoke == true) {
Ben Chengd44faf52010-06-02 15:33:51 -07004113 /* Align this block first since it is a return chaining cell */
4114 newLIR0(cUnit, kArmPseudoPseudoAlign4);
4115 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07004116 /*
4117 * Append the label pseudo LIR first. Chaining cells will be handled
4118 * separately afterwards.
4119 */
4120 dvmCompilerAppendLIR(cUnit, (LIR *) &labelList[i]);
4121 }
4122
Ben Cheng00603072010-10-28 11:13:58 -07004123 if (bb->blockType == kTraceEntryBlock) {
Dan Bornstein9a1f8162010-12-01 17:02:26 -08004124 labelList[i].opcode = kArmPseudoEntryBlock;
Ben Cheng00603072010-10-28 11:13:58 -07004125 if (bb->firstMIRInsn == NULL) {
Ben Cheng4238ec22009-08-24 16:32:22 -07004126 continue;
4127 } else {
Ben Cheng00603072010-10-28 11:13:58 -07004128 setupLoopEntryBlock(cUnit, bb,
4129 &labelList[bb->fallThrough->id]);
Ben Cheng4238ec22009-08-24 16:32:22 -07004130 }
Ben Cheng00603072010-10-28 11:13:58 -07004131 } else if (bb->blockType == kTraceExitBlock) {
Dan Bornstein9a1f8162010-12-01 17:02:26 -08004132 labelList[i].opcode = kArmPseudoExitBlock;
Ben Cheng4238ec22009-08-24 16:32:22 -07004133 goto gen_fallthrough;
Ben Cheng00603072010-10-28 11:13:58 -07004134 } else if (bb->blockType == kDalvikByteCode) {
Dan Bornstein9a1f8162010-12-01 17:02:26 -08004135 labelList[i].opcode = kArmPseudoNormalBlockLabel;
Ben Chenge9695e52009-06-16 16:11:47 -07004136 /* Reset the register state */
Bill Buzbeec6f10662010-02-09 11:16:15 -08004137 dvmCompilerResetRegPool(cUnit);
4138 dvmCompilerClobberAllRegs(cUnit);
4139 dvmCompilerResetNullCheck(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -07004140 } else {
Ben Cheng00603072010-10-28 11:13:58 -07004141 switch (bb->blockType) {
Bill Buzbee1465db52009-09-23 17:17:35 -07004142 case kChainingCellNormal:
Dan Bornstein9a1f8162010-12-01 17:02:26 -08004143 labelList[i].opcode = kArmPseudoChainingCellNormal;
Ben Chengba4fc8b2009-06-01 13:00:29 -07004144 /* handle the codegen later */
4145 dvmInsertGrowableList(
Ben Cheng00603072010-10-28 11:13:58 -07004146 &chainingListByType[kChainingCellNormal], i);
Ben Chengba4fc8b2009-06-01 13:00:29 -07004147 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07004148 case kChainingCellInvokeSingleton:
Dan Bornstein9a1f8162010-12-01 17:02:26 -08004149 labelList[i].opcode =
Ben Chenga4973592010-03-31 11:59:18 -07004150 kArmPseudoChainingCellInvokeSingleton;
Ben Chengba4fc8b2009-06-01 13:00:29 -07004151 labelList[i].operands[0] =
Ben Cheng00603072010-10-28 11:13:58 -07004152 (int) bb->containingMethod;
Ben Chengba4fc8b2009-06-01 13:00:29 -07004153 /* handle the codegen later */
4154 dvmInsertGrowableList(
Ben Cheng00603072010-10-28 11:13:58 -07004155 &chainingListByType[kChainingCellInvokeSingleton], i);
Ben Cheng38329f52009-07-07 14:19:20 -07004156 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07004157 case kChainingCellInvokePredicted:
Dan Bornstein9a1f8162010-12-01 17:02:26 -08004158 labelList[i].opcode =
Ben Chenga4973592010-03-31 11:59:18 -07004159 kArmPseudoChainingCellInvokePredicted;
Ben Cheng38329f52009-07-07 14:19:20 -07004160 /* handle the codegen later */
4161 dvmInsertGrowableList(
Ben Cheng00603072010-10-28 11:13:58 -07004162 &chainingListByType[kChainingCellInvokePredicted], i);
Ben Chengba4fc8b2009-06-01 13:00:29 -07004163 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07004164 case kChainingCellHot:
Dan Bornstein9a1f8162010-12-01 17:02:26 -08004165 labelList[i].opcode =
Ben Chenga4973592010-03-31 11:59:18 -07004166 kArmPseudoChainingCellHot;
Ben Chengba4fc8b2009-06-01 13:00:29 -07004167 /* handle the codegen later */
4168 dvmInsertGrowableList(
Ben Cheng00603072010-10-28 11:13:58 -07004169 &chainingListByType[kChainingCellHot], i);
Ben Chengba4fc8b2009-06-01 13:00:29 -07004170 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07004171 case kPCReconstruction:
Ben Chengba4fc8b2009-06-01 13:00:29 -07004172 /* Make sure exception handling block is next */
Dan Bornstein9a1f8162010-12-01 17:02:26 -08004173 labelList[i].opcode =
Ben Chenga4973592010-03-31 11:59:18 -07004174 kArmPseudoPCReconstructionBlockLabel;
Ben Chengba4fc8b2009-06-01 13:00:29 -07004175 assert (i == cUnit->numBlocks - 2);
4176 handlePCReconstruction(cUnit, &labelList[i+1]);
4177 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07004178 case kExceptionHandling:
Dan Bornstein9a1f8162010-12-01 17:02:26 -08004179 labelList[i].opcode = kArmPseudoEHBlockLabel;
Ben Chengba4fc8b2009-06-01 13:00:29 -07004180 if (cUnit->pcReconstructionList.numUsed) {
Bill Buzbee270c1d62009-08-13 16:58:07 -07004181 loadWordDisp(cUnit, rGLUE, offsetof(InterpState,
4182 jitToInterpEntries.dvmJitToInterpPunt),
4183 r1);
Bill Buzbee1465db52009-09-23 17:17:35 -07004184 opReg(cUnit, kOpBlx, r1);
Ben Chengba4fc8b2009-06-01 13:00:29 -07004185 }
4186 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07004187 case kChainingCellBackwardBranch:
Dan Bornstein9a1f8162010-12-01 17:02:26 -08004188 labelList[i].opcode =
Ben Chenga4973592010-03-31 11:59:18 -07004189 kArmPseudoChainingCellBackwardBranch;
Jeff Hao97319a82009-08-12 16:57:15 -07004190 /* handle the codegen later */
4191 dvmInsertGrowableList(
Bill Buzbee1465db52009-09-23 17:17:35 -07004192 &chainingListByType[kChainingCellBackwardBranch],
Ben Cheng00603072010-10-28 11:13:58 -07004193 i);
Jeff Hao97319a82009-08-12 16:57:15 -07004194 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07004195 default:
4196 break;
4197 }
4198 continue;
4199 }
Ben Chenge9695e52009-06-16 16:11:47 -07004200
Bill Buzbee89efc3d2009-07-28 11:22:22 -07004201 ArmLIR *headLIR = NULL;
Ben Chenge9695e52009-06-16 16:11:47 -07004202
Ben Cheng00603072010-10-28 11:13:58 -07004203 for (mir = bb->firstMIRInsn; mir; mir = mir->next) {
Bill Buzbee1465db52009-09-23 17:17:35 -07004204
Bill Buzbeec6f10662010-02-09 11:16:15 -08004205 dvmCompilerResetRegPool(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07004206 if (gDvmJit.disableOpt & (1 << kTrackLiveTemps)) {
Bill Buzbeec6f10662010-02-09 11:16:15 -08004207 dvmCompilerClobberAllRegs(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07004208 }
4209
4210 if (gDvmJit.disableOpt & (1 << kSuppressLoads)) {
Bill Buzbeec6f10662010-02-09 11:16:15 -08004211 dvmCompilerResetDefTracking(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07004212 }
4213
Dan Bornstein9a1f8162010-12-01 17:02:26 -08004214 if (mir->dalvikInsn.opcode >= kMirOpFirst) {
Ben Cheng4238ec22009-08-24 16:32:22 -07004215 handleExtendedMIR(cUnit, mir);
4216 continue;
4217 }
4218
Bill Buzbee1465db52009-09-23 17:17:35 -07004219
Dan Bornstein9a1f8162010-12-01 17:02:26 -08004220 Opcode dalvikOpcode = mir->dalvikInsn.opcode;
Dan Bornsteine4852762010-12-02 12:45:00 -08004221 InstructionFormat dalvikFormat = dexGetFormatFromOpcode(dalvikOpcode);
Ben Cheng7a2697d2010-06-07 13:44:23 -07004222 char *note;
4223 if (mir->OptimizationFlags & MIR_INLINED) {
4224 note = " (I)";
4225 } else if (mir->OptimizationFlags & MIR_INLINED_PRED) {
4226 note = " (PI)";
4227 } else if (mir->OptimizationFlags & MIR_CALLEE) {
4228 note = " (C)";
4229 } else {
4230 note = NULL;
4231 }
4232
Ben Cheng80211d22011-01-14 10:23:37 -08004233 ArmLIR *boundaryLIR;
4234
4235 /*
4236 * Don't generate the boundary LIR unless we are debugging this
4237 * trace or we need a scheduling barrier.
4238 */
4239 if (headLIR == NULL || cUnit->printMe == true) {
4240 boundaryLIR =
4241 newLIR2(cUnit, kArmPseudoDalvikByteCodeBoundary,
4242 mir->offset,
4243 (int) dvmCompilerGetDalvikDisassembly(
4244 &mir->dalvikInsn, note));
4245 /* Remember the first LIR for this block */
4246 if (headLIR == NULL) {
4247 headLIR = boundaryLIR;
4248 /* Set the first boundaryLIR as a scheduling barrier */
4249 headLIR->defMask = ENCODE_ALL;
4250 }
Ben Cheng4238ec22009-08-24 16:32:22 -07004251 }
4252
Ben Cheng80211d22011-01-14 10:23:37 -08004253 /* Don't generate the SSA annotation unless verbose mode is on */
4254 if (cUnit->printMe && mir->ssaRep) {
4255 char *ssaString = dvmCompilerGetSSAString(cUnit, mir->ssaRep);
4256 newLIR1(cUnit, kArmPseudoSSARep, (int) ssaString);
Ben Chenge9695e52009-06-16 16:11:47 -07004257 }
Ben Cheng4238ec22009-08-24 16:32:22 -07004258
Ben Chengba4fc8b2009-06-01 13:00:29 -07004259 bool notHandled;
4260 /*
4261 * Debugging: screen the opcode first to see if it is in the
4262 * do[-not]-compile list
4263 */
Dan Bornstein9a1f8162010-12-01 17:02:26 -08004264 bool singleStepMe = SINGLE_STEP_OP(dalvikOpcode);
Ben Chengd5adae12010-03-26 17:45:28 -07004265#if defined(WITH_SELF_VERIFICATION)
4266 if (singleStepMe == false) {
4267 singleStepMe = selfVerificationPuntOps(mir);
4268 }
4269#endif
Ben Chengba4fc8b2009-06-01 13:00:29 -07004270 if (singleStepMe || cUnit->allSingleStep) {
4271 notHandled = false;
4272 genInterpSingleStep(cUnit, mir);
4273 } else {
Dan Bornstein9a1f8162010-12-01 17:02:26 -08004274 opcodeCoverage[dalvikOpcode]++;
Ben Chengba4fc8b2009-06-01 13:00:29 -07004275 switch (dalvikFormat) {
4276 case kFmt10t:
4277 case kFmt20t:
4278 case kFmt30t:
4279 notHandled = handleFmt10t_Fmt20t_Fmt30t(cUnit,
Ben Cheng00603072010-10-28 11:13:58 -07004280 mir, bb, labelList);
Ben Chengba4fc8b2009-06-01 13:00:29 -07004281 break;
4282 case kFmt10x:
4283 notHandled = handleFmt10x(cUnit, mir);
4284 break;
4285 case kFmt11n:
4286 case kFmt31i:
4287 notHandled = handleFmt11n_Fmt31i(cUnit, mir);
4288 break;
4289 case kFmt11x:
4290 notHandled = handleFmt11x(cUnit, mir);
4291 break;
4292 case kFmt12x:
4293 notHandled = handleFmt12x(cUnit, mir);
4294 break;
4295 case kFmt20bc:
jeffhao71eee1f2011-01-04 14:18:54 -08004296 case kFmt40sc:
4297 notHandled = handleFmt20bc_Fmt40sc(cUnit, mir);
Ben Chengba4fc8b2009-06-01 13:00:29 -07004298 break;
4299 case kFmt21c:
4300 case kFmt31c:
jeffhao71eee1f2011-01-04 14:18:54 -08004301 case kFmt41c:
4302 notHandled = handleFmt21c_Fmt31c_Fmt41c(cUnit, mir);
Ben Chengba4fc8b2009-06-01 13:00:29 -07004303 break;
4304 case kFmt21h:
4305 notHandled = handleFmt21h(cUnit, mir);
4306 break;
4307 case kFmt21s:
4308 notHandled = handleFmt21s(cUnit, mir);
4309 break;
4310 case kFmt21t:
Ben Cheng00603072010-10-28 11:13:58 -07004311 notHandled = handleFmt21t(cUnit, mir, bb, labelList);
Ben Chengba4fc8b2009-06-01 13:00:29 -07004312 break;
4313 case kFmt22b:
4314 case kFmt22s:
4315 notHandled = handleFmt22b_Fmt22s(cUnit, mir);
4316 break;
4317 case kFmt22c:
jeffhao71eee1f2011-01-04 14:18:54 -08004318 case kFmt52c:
4319 notHandled = handleFmt22c_Fmt52c(cUnit, mir);
Ben Chengba4fc8b2009-06-01 13:00:29 -07004320 break;
4321 case kFmt22cs:
4322 notHandled = handleFmt22cs(cUnit, mir);
4323 break;
4324 case kFmt22t:
Ben Cheng00603072010-10-28 11:13:58 -07004325 notHandled = handleFmt22t(cUnit, mir, bb, labelList);
Ben Chengba4fc8b2009-06-01 13:00:29 -07004326 break;
4327 case kFmt22x:
4328 case kFmt32x:
4329 notHandled = handleFmt22x_Fmt32x(cUnit, mir);
4330 break;
4331 case kFmt23x:
4332 notHandled = handleFmt23x(cUnit, mir);
4333 break;
4334 case kFmt31t:
4335 notHandled = handleFmt31t(cUnit, mir);
4336 break;
4337 case kFmt3rc:
4338 case kFmt35c:
jeffhao71eee1f2011-01-04 14:18:54 -08004339 case kFmt5rc:
4340 notHandled = handleFmt35c_3rc_5rc(cUnit, mir, bb,
Ben Chengba4fc8b2009-06-01 13:00:29 -07004341 labelList);
4342 break;
4343 case kFmt3rms:
4344 case kFmt35ms:
Ben Cheng00603072010-10-28 11:13:58 -07004345 notHandled = handleFmt35ms_3rms(cUnit, mir, bb,
Ben Chengba4fc8b2009-06-01 13:00:29 -07004346 labelList);
4347 break;
Dan Bornstein7b3e9b02010-11-09 17:15:10 -08004348 case kFmt35mi:
4349 case kFmt3rmi:
Bill Buzbeece46c942009-11-20 15:41:34 -08004350 notHandled = handleExecuteInline(cUnit, mir);
Andy McFaddenb0a05412009-11-19 10:23:41 -08004351 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07004352 case kFmt51l:
4353 notHandled = handleFmt51l(cUnit, mir);
4354 break;
4355 default:
4356 notHandled = true;
4357 break;
4358 }
4359 }
4360 if (notHandled) {
4361 LOGE("%#06x: Opcode 0x%x (%s) / Fmt %d not handled\n",
4362 mir->offset,
Dan Bornstein9a1f8162010-12-01 17:02:26 -08004363 dalvikOpcode, dexGetOpcodeName(dalvikOpcode),
Ben Chengba4fc8b2009-06-01 13:00:29 -07004364 dalvikFormat);
Bill Buzbeefc519dc2010-03-06 23:30:57 -08004365 dvmCompilerAbort(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -07004366 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07004367 }
4368 }
Ben Cheng4238ec22009-08-24 16:32:22 -07004369
Ben Cheng00603072010-10-28 11:13:58 -07004370 if (bb->blockType == kTraceEntryBlock) {
Ben Cheng4238ec22009-08-24 16:32:22 -07004371 dvmCompilerAppendLIR(cUnit,
4372 (LIR *) cUnit->loopAnalysis->branchToBody);
4373 dvmCompilerAppendLIR(cUnit,
4374 (LIR *) cUnit->loopAnalysis->branchToPCR);
4375 }
4376
4377 if (headLIR) {
4378 /*
4379 * Eliminate redundant loads/stores and delay stores into later
4380 * slots
4381 */
4382 dvmCompilerApplyLocalOptimizations(cUnit, (LIR *) headLIR,
4383 cUnit->lastLIRInsn);
4384 }
4385
4386gen_fallthrough:
Ben Cheng1efc9c52009-06-08 18:25:27 -07004387 /*
4388 * Check if the block is terminated due to trace length constraint -
4389 * insert an unconditional branch to the chaining cell.
4390 */
Ben Cheng00603072010-10-28 11:13:58 -07004391 if (bb->needFallThroughBranch) {
Ben Cheng1efc9c52009-06-08 18:25:27 -07004392 genUnconditionalBranch(cUnit,
Ben Cheng00603072010-10-28 11:13:58 -07004393 &labelList[bb->fallThrough->id]);
Ben Cheng1efc9c52009-06-08 18:25:27 -07004394 }
4395
Ben Chengba4fc8b2009-06-01 13:00:29 -07004396 }
4397
Ben Chenge9695e52009-06-16 16:11:47 -07004398 /* Handle the chaining cells in predefined order */
Ben Chengcec26f62010-01-15 15:29:33 -08004399 for (i = 0; i < kChainingCellGap; i++) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07004400 size_t j;
4401 int *blockIdList = (int *) chainingListByType[i].elemList;
4402
4403 cUnit->numChainingCells[i] = chainingListByType[i].numUsed;
4404
4405 /* No chaining cells of this type */
4406 if (cUnit->numChainingCells[i] == 0)
4407 continue;
4408
4409 /* Record the first LIR for a new type of chaining cell */
4410 cUnit->firstChainingLIR[i] = (LIR *) &labelList[blockIdList[0]];
4411
4412 for (j = 0; j < chainingListByType[i].numUsed; j++) {
4413 int blockId = blockIdList[j];
Ben Cheng00603072010-10-28 11:13:58 -07004414 BasicBlock *chainingBlock =
4415 (BasicBlock *) dvmGrowableListGetElement(&cUnit->blockList,
4416 blockId);
Ben Chengba4fc8b2009-06-01 13:00:29 -07004417
4418 /* Align this chaining cell first */
Bill Buzbee1465db52009-09-23 17:17:35 -07004419 newLIR0(cUnit, kArmPseudoPseudoAlign4);
Ben Chengba4fc8b2009-06-01 13:00:29 -07004420
4421 /* Insert the pseudo chaining instruction */
4422 dvmCompilerAppendLIR(cUnit, (LIR *) &labelList[blockId]);
4423
4424
Ben Cheng00603072010-10-28 11:13:58 -07004425 switch (chainingBlock->blockType) {
Bill Buzbee1465db52009-09-23 17:17:35 -07004426 case kChainingCellNormal:
Ben Cheng00603072010-10-28 11:13:58 -07004427 handleNormalChainingCell(cUnit, chainingBlock->startOffset);
Ben Chengba4fc8b2009-06-01 13:00:29 -07004428 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07004429 case kChainingCellInvokeSingleton:
Ben Cheng38329f52009-07-07 14:19:20 -07004430 handleInvokeSingletonChainingCell(cUnit,
Ben Cheng00603072010-10-28 11:13:58 -07004431 chainingBlock->containingMethod);
Ben Chengba4fc8b2009-06-01 13:00:29 -07004432 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07004433 case kChainingCellInvokePredicted:
Ben Cheng38329f52009-07-07 14:19:20 -07004434 handleInvokePredictedChainingCell(cUnit);
4435 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07004436 case kChainingCellHot:
Ben Cheng00603072010-10-28 11:13:58 -07004437 handleHotChainingCell(cUnit, chainingBlock->startOffset);
Ben Chengba4fc8b2009-06-01 13:00:29 -07004438 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07004439 case kChainingCellBackwardBranch:
Jeff Hao97319a82009-08-12 16:57:15 -07004440 handleBackwardBranchChainingCell(cUnit,
Ben Cheng00603072010-10-28 11:13:58 -07004441 chainingBlock->startOffset);
Jeff Hao97319a82009-08-12 16:57:15 -07004442 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07004443 default:
Ben Cheng00603072010-10-28 11:13:58 -07004444 LOGE("Bad blocktype %d", chainingBlock->blockType);
Bill Buzbeefc519dc2010-03-06 23:30:57 -08004445 dvmCompilerAbort(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -07004446 }
4447 }
4448 }
Ben Chenge9695e52009-06-16 16:11:47 -07004449
Ben Chengcec26f62010-01-15 15:29:33 -08004450 /* Mark the bottom of chaining cells */
4451 cUnit->chainingCellBottom = (LIR *) newLIR0(cUnit, kArmChainingCellBottom);
4452
Ben Cheng6c10a972009-10-29 14:39:18 -07004453 /*
4454 * Generate the branch to the dvmJitToInterpNoChain entry point at the end
4455 * of all chaining cells for the overflow cases.
4456 */
4457 if (cUnit->switchOverflowPad) {
4458 loadConstant(cUnit, r0, (int) cUnit->switchOverflowPad);
4459 loadWordDisp(cUnit, rGLUE, offsetof(InterpState,
4460 jitToInterpEntries.dvmJitToInterpNoChain), r2);
4461 opRegReg(cUnit, kOpAdd, r1, r1);
4462 opRegRegReg(cUnit, kOpAdd, r4PC, r0, r1);
Ben Cheng978738d2010-05-13 13:45:57 -07004463#if defined(WITH_JIT_TUNING)
Ben Cheng6c10a972009-10-29 14:39:18 -07004464 loadConstant(cUnit, r0, kSwitchOverflow);
4465#endif
4466 opReg(cUnit, kOpBlx, r2);
4467 }
4468
Ben Chenge9695e52009-06-16 16:11:47 -07004469 dvmCompilerApplyGlobalOptimizations(cUnit);
jeffhao9e45c0b2010-02-03 10:24:05 -08004470
4471#if defined(WITH_SELF_VERIFICATION)
4472 selfVerificationBranchInsertPass(cUnit);
4473#endif
Ben Chengba4fc8b2009-06-01 13:00:29 -07004474}
4475
buzbee2e152ba2010-12-15 16:32:35 -08004476/*
4477 * Accept the work and start compiling. Returns true if compilation
4478 * is attempted.
4479 */
Bill Buzbee716f1202009-07-23 13:22:09 -07004480bool dvmCompilerDoWork(CompilerWorkOrder *work)
Ben Chengba4fc8b2009-06-01 13:00:29 -07004481{
Carl Shapirofc75f3e2010-12-07 11:43:38 -08004482 JitTraceDescription *desc;
buzbee2e152ba2010-12-15 16:32:35 -08004483 bool isCompile;
4484 bool success = true;
Ben Chengba4fc8b2009-06-01 13:00:29 -07004485
Ben Cheng6999d842010-01-26 16:46:15 -08004486 if (gDvmJit.codeCacheFull) {
Ben Chengccd6c012009-10-15 14:52:45 -07004487 return false;
4488 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07004489
Ben Chengccd6c012009-10-15 14:52:45 -07004490 switch (work->kind) {
Ben Chengccd6c012009-10-15 14:52:45 -07004491 case kWorkOrderTrace:
buzbee2e152ba2010-12-15 16:32:35 -08004492 isCompile = true;
Ben Chengccd6c012009-10-15 14:52:45 -07004493 /* Start compilation with maximally allowed trace length */
Carl Shapirofc75f3e2010-12-07 11:43:38 -08004494 desc = (JitTraceDescription *)work->info;
buzbee2e152ba2010-12-15 16:32:35 -08004495 success = dvmCompileTrace(desc, JIT_MAX_TRACE_LEN, &work->result,
4496 work->bailPtr, 0 /* no hints */);
Ben Chengccd6c012009-10-15 14:52:45 -07004497 break;
4498 case kWorkOrderTraceDebug: {
4499 bool oldPrintMe = gDvmJit.printMe;
4500 gDvmJit.printMe = true;
buzbee2e152ba2010-12-15 16:32:35 -08004501 isCompile = true;
Ben Chengccd6c012009-10-15 14:52:45 -07004502 /* Start compilation with maximally allowed trace length */
Carl Shapirofc75f3e2010-12-07 11:43:38 -08004503 desc = (JitTraceDescription *)work->info;
buzbee2e152ba2010-12-15 16:32:35 -08004504 success = dvmCompileTrace(desc, JIT_MAX_TRACE_LEN, &work->result,
4505 work->bailPtr, 0 /* no hints */);
Elliott Hughes672511b2010-04-26 17:40:13 -07004506 gDvmJit.printMe = oldPrintMe;
Ben Chengccd6c012009-10-15 14:52:45 -07004507 break;
4508 }
buzbee2e152ba2010-12-15 16:32:35 -08004509 case kWorkOrderProfileMode:
4510 dvmJitChangeProfileMode((TraceProfilingModes)work->info);
4511 isCompile = false;
4512 break;
Ben Chengccd6c012009-10-15 14:52:45 -07004513 default:
buzbee2e152ba2010-12-15 16:32:35 -08004514 isCompile = false;
Bill Buzbeefc519dc2010-03-06 23:30:57 -08004515 LOGE("Jit: unknown work order type");
Elliott Hughes672511b2010-04-26 17:40:13 -07004516 assert(0); // Bail if debug build, discard otherwise
Ben Chengccd6c012009-10-15 14:52:45 -07004517 }
buzbee2e152ba2010-12-15 16:32:35 -08004518 if (!success)
4519 work->result.codeAddress = NULL;
4520 return isCompile;
Ben Chengba4fc8b2009-06-01 13:00:29 -07004521}
4522
Ben Chengba4fc8b2009-06-01 13:00:29 -07004523/* Architectural-specific debugging helpers go here */
4524void dvmCompilerArchDump(void)
4525{
4526 /* Print compiled opcode in this VM instance */
4527 int i, start, streak;
4528 char buf[1024];
4529
4530 streak = i = 0;
4531 buf[0] = 0;
Dan Bornsteinccaab182010-12-03 15:32:40 -08004532 while (opcodeCoverage[i] == 0 && i < kNumPackedOpcodes) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07004533 i++;
4534 }
Dan Bornsteinccaab182010-12-03 15:32:40 -08004535 if (i == kNumPackedOpcodes) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07004536 return;
4537 }
Dan Bornsteinccaab182010-12-03 15:32:40 -08004538 for (start = i++, streak = 1; i < kNumPackedOpcodes; i++) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07004539 if (opcodeCoverage[i]) {
4540 streak++;
4541 } else {
4542 if (streak == 1) {
4543 sprintf(buf+strlen(buf), "%x,", start);
4544 } else {
4545 sprintf(buf+strlen(buf), "%x-%x,", start, start + streak - 1);
4546 }
4547 streak = 0;
Dan Bornsteinccaab182010-12-03 15:32:40 -08004548 while (opcodeCoverage[i] == 0 && i < kNumPackedOpcodes) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07004549 i++;
4550 }
Dan Bornsteinccaab182010-12-03 15:32:40 -08004551 if (i < kNumPackedOpcodes) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07004552 streak = 1;
4553 start = i;
4554 }
4555 }
4556 }
4557 if (streak) {
4558 if (streak == 1) {
4559 sprintf(buf+strlen(buf), "%x", start);
4560 } else {
4561 sprintf(buf+strlen(buf), "%x-%x", start, start + streak - 1);
4562 }
4563 }
4564 if (strlen(buf)) {
Ben Cheng8b258bf2009-06-24 17:27:07 -07004565 LOGD("dalvik.vm.jit.op = %s", buf);
Ben Chengba4fc8b2009-06-01 13:00:29 -07004566 }
4567}
Ben Chengd7d426a2009-09-22 11:23:36 -07004568
4569/* Common initialization routine for an architecture family */
4570bool dvmCompilerArchInit()
4571{
4572 int i;
4573
Bill Buzbee1465db52009-09-23 17:17:35 -07004574 for (i = 0; i < kArmLast; i++) {
Dan Bornstein9a1f8162010-12-01 17:02:26 -08004575 if (EncodingMap[i].opcode != i) {
Ben Chengd7d426a2009-09-22 11:23:36 -07004576 LOGE("Encoding order for %s is wrong: expecting %d, seeing %d",
Dan Bornstein9a1f8162010-12-01 17:02:26 -08004577 EncodingMap[i].name, i, EncodingMap[i].opcode);
Bill Buzbeefc519dc2010-03-06 23:30:57 -08004578 dvmAbort(); // OK to dvmAbort - build error
Ben Chengd7d426a2009-09-22 11:23:36 -07004579 }
4580 }
4581
Ben Cheng5d90c202009-11-22 23:31:11 -08004582 return dvmCompilerArchVariantInit();
4583}
4584
4585void *dvmCompilerGetInterpretTemplate()
4586{
4587 return (void*) ((int)gDvmJit.codeCache +
4588 templateEntryOffsets[TEMPLATE_INTERPRET]);
4589}
4590
Bill Buzbee1b3da592011-02-03 07:38:22 -08004591JitInstructionSetType dvmCompilerGetInterpretTemplateSet()
4592{
4593 return DALVIK_JIT_ARM;
4594}
4595
buzbeebff121a2010-08-04 15:25:06 -07004596/* Needed by the Assembler */
4597void dvmCompilerSetupResourceMasks(ArmLIR *lir)
4598{
4599 setupResourceMasks(lir);
4600}
4601
Ben Cheng5d90c202009-11-22 23:31:11 -08004602/* Needed by the ld/st optmizatons */
4603ArmLIR* dvmCompilerRegCopyNoInsert(CompilationUnit *cUnit, int rDest, int rSrc)
4604{
4605 return genRegCopyNoInsert(cUnit, rDest, rSrc);
4606}
4607
4608/* Needed by the register allocator */
4609ArmLIR* dvmCompilerRegCopy(CompilationUnit *cUnit, int rDest, int rSrc)
4610{
4611 return genRegCopy(cUnit, rDest, rSrc);
4612}
4613
4614/* Needed by the register allocator */
4615void dvmCompilerRegCopyWide(CompilationUnit *cUnit, int destLo, int destHi,
4616 int srcLo, int srcHi)
4617{
4618 genRegCopyWide(cUnit, destLo, destHi, srcLo, srcHi);
4619}
4620
4621void dvmCompilerFlushRegImpl(CompilationUnit *cUnit, int rBase,
4622 int displacement, int rSrc, OpSize size)
4623{
4624 storeBaseDisp(cUnit, rBase, displacement, rSrc, size);
4625}
4626
4627void dvmCompilerFlushRegWideImpl(CompilationUnit *cUnit, int rBase,
4628 int displacement, int rSrcLo, int rSrcHi)
4629{
4630 storeBaseDispWide(cUnit, rBase, displacement, rSrcLo, rSrcHi);
Ben Chengd7d426a2009-09-22 11:23:36 -07004631}