blob: e94fc1175fa9eb396457cd11875fd278706e6749 [file] [log] [blame]
Ben Chengba4fc8b2009-06-01 13:00:29 -07001/*
2 * Copyright (C) 2009 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Bill Buzbee50a6bf22009-07-08 13:08:04 -070017/*
18 * This file contains codegen and support common to all supported
19 * ARM variants. It is included by:
20 *
21 * Codegen-$(TARGET_ARCH_VARIANT).c
22 *
23 * which combines this common code with specific support found in the
24 * applicable directory below this one.
25 */
26
buzbee919eb062010-07-12 12:59:22 -070027/*
28 * Mark garbage collection card. Skip if the value we're storing is null.
29 */
30static void markCard(CompilationUnit *cUnit, int valReg, int tgtAddrReg)
31{
32 int regCardBase = dvmCompilerAllocTemp(cUnit);
33 int regCardNo = dvmCompilerAllocTemp(cUnit);
buzbee8f8109a2010-08-31 10:16:35 -070034 ArmLIR *branchOver = genCmpImmBranch(cUnit, kArmCondEq, valReg, 0);
Ben Cheng20d7e6c2011-02-18 17:12:42 -080035 loadWordDisp(cUnit, r6SELF, offsetof(Thread, cardTable),
buzbee919eb062010-07-12 12:59:22 -070036 regCardBase);
37 opRegRegImm(cUnit, kOpLsr, regCardNo, tgtAddrReg, GC_CARD_SHIFT);
38 storeBaseIndexed(cUnit, regCardBase, regCardNo, regCardBase, 0,
39 kUnsignedByte);
40 ArmLIR *target = newLIR0(cUnit, kArmPseudoTargetLabel);
41 target->defMask = ENCODE_ALL;
42 branchOver->generic.target = (LIR *)target;
buzbeebaf196a2010-08-04 10:13:15 -070043 dvmCompilerFreeTemp(cUnit, regCardBase);
44 dvmCompilerFreeTemp(cUnit, regCardNo);
buzbee919eb062010-07-12 12:59:22 -070045}
46
Ben Cheng5d90c202009-11-22 23:31:11 -080047static bool genConversionCall(CompilationUnit *cUnit, MIR *mir, void *funct,
48 int srcSize, int tgtSize)
49{
50 /*
51 * Don't optimize the register usage since it calls out to template
52 * functions
53 */
54 RegLocation rlSrc;
55 RegLocation rlDest;
Bill Buzbeec6f10662010-02-09 11:16:15 -080056 dvmCompilerFlushAllRegs(cUnit); /* Send everything to home location */
Ben Cheng5d90c202009-11-22 23:31:11 -080057 if (srcSize == 1) {
Bill Buzbeec6f10662010-02-09 11:16:15 -080058 rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
Ben Cheng5d90c202009-11-22 23:31:11 -080059 loadValueDirectFixed(cUnit, rlSrc, r0);
60 } else {
Bill Buzbeec6f10662010-02-09 11:16:15 -080061 rlSrc = dvmCompilerGetSrcWide(cUnit, mir, 0, 1);
Ben Cheng5d90c202009-11-22 23:31:11 -080062 loadValueDirectWideFixed(cUnit, rlSrc, r0, r1);
63 }
Ben Chengbd1326d2010-04-02 15:04:53 -070064 LOAD_FUNC_ADDR(cUnit, r2, (int)funct);
Ben Cheng5d90c202009-11-22 23:31:11 -080065 opReg(cUnit, kOpBlx, r2);
Elliott Hughes6a555132010-02-25 15:41:42 -080066 dvmCompilerClobberCallRegs(cUnit);
Ben Cheng5d90c202009-11-22 23:31:11 -080067 if (tgtSize == 1) {
68 RegLocation rlResult;
Bill Buzbeec6f10662010-02-09 11:16:15 -080069 rlDest = dvmCompilerGetDest(cUnit, mir, 0);
70 rlResult = dvmCompilerGetReturn(cUnit);
Ben Cheng5d90c202009-11-22 23:31:11 -080071 storeValue(cUnit, rlDest, rlResult);
72 } else {
73 RegLocation rlResult;
Bill Buzbeec6f10662010-02-09 11:16:15 -080074 rlDest = dvmCompilerGetDestWide(cUnit, mir, 0, 1);
75 rlResult = dvmCompilerGetReturnWide(cUnit);
Ben Cheng5d90c202009-11-22 23:31:11 -080076 storeValueWide(cUnit, rlDest, rlResult);
77 }
78 return false;
79}
Ben Chengba4fc8b2009-06-01 13:00:29 -070080
Ben Cheng5d90c202009-11-22 23:31:11 -080081static bool genArithOpFloatPortable(CompilationUnit *cUnit, MIR *mir,
82 RegLocation rlDest, RegLocation rlSrc1,
83 RegLocation rlSrc2)
84{
85 RegLocation rlResult;
86 void* funct;
87
Dan Bornstein9a1f8162010-12-01 17:02:26 -080088 switch (mir->dalvikInsn.opcode) {
Ben Cheng5d90c202009-11-22 23:31:11 -080089 case OP_ADD_FLOAT_2ADDR:
90 case OP_ADD_FLOAT:
91 funct = (void*) __aeabi_fadd;
92 break;
93 case OP_SUB_FLOAT_2ADDR:
94 case OP_SUB_FLOAT:
95 funct = (void*) __aeabi_fsub;
96 break;
97 case OP_DIV_FLOAT_2ADDR:
98 case OP_DIV_FLOAT:
99 funct = (void*) __aeabi_fdiv;
100 break;
101 case OP_MUL_FLOAT_2ADDR:
102 case OP_MUL_FLOAT:
103 funct = (void*) __aeabi_fmul;
104 break;
105 case OP_REM_FLOAT_2ADDR:
106 case OP_REM_FLOAT:
107 funct = (void*) fmodf;
108 break;
109 case OP_NEG_FLOAT: {
110 genNegFloat(cUnit, rlDest, rlSrc1);
111 return false;
112 }
113 default:
114 return true;
115 }
Bill Buzbeec6f10662010-02-09 11:16:15 -0800116 dvmCompilerFlushAllRegs(cUnit); /* Send everything to home location */
Ben Cheng5d90c202009-11-22 23:31:11 -0800117 loadValueDirectFixed(cUnit, rlSrc1, r0);
118 loadValueDirectFixed(cUnit, rlSrc2, r1);
Ben Chengbd1326d2010-04-02 15:04:53 -0700119 LOAD_FUNC_ADDR(cUnit, r2, (int)funct);
Ben Cheng5d90c202009-11-22 23:31:11 -0800120 opReg(cUnit, kOpBlx, r2);
Elliott Hughes6a555132010-02-25 15:41:42 -0800121 dvmCompilerClobberCallRegs(cUnit);
Bill Buzbeec6f10662010-02-09 11:16:15 -0800122 rlResult = dvmCompilerGetReturn(cUnit);
Ben Cheng5d90c202009-11-22 23:31:11 -0800123 storeValue(cUnit, rlDest, rlResult);
124 return false;
125}
126
127static bool genArithOpDoublePortable(CompilationUnit *cUnit, MIR *mir,
128 RegLocation rlDest, RegLocation rlSrc1,
129 RegLocation rlSrc2)
130{
131 RegLocation rlResult;
132 void* funct;
133
Dan Bornstein9a1f8162010-12-01 17:02:26 -0800134 switch (mir->dalvikInsn.opcode) {
Ben Cheng5d90c202009-11-22 23:31:11 -0800135 case OP_ADD_DOUBLE_2ADDR:
136 case OP_ADD_DOUBLE:
137 funct = (void*) __aeabi_dadd;
138 break;
139 case OP_SUB_DOUBLE_2ADDR:
140 case OP_SUB_DOUBLE:
141 funct = (void*) __aeabi_dsub;
142 break;
143 case OP_DIV_DOUBLE_2ADDR:
144 case OP_DIV_DOUBLE:
145 funct = (void*) __aeabi_ddiv;
146 break;
147 case OP_MUL_DOUBLE_2ADDR:
148 case OP_MUL_DOUBLE:
149 funct = (void*) __aeabi_dmul;
150 break;
151 case OP_REM_DOUBLE_2ADDR:
152 case OP_REM_DOUBLE:
153 funct = (void*) fmod;
154 break;
155 case OP_NEG_DOUBLE: {
156 genNegDouble(cUnit, rlDest, rlSrc1);
157 return false;
158 }
159 default:
160 return true;
161 }
Bill Buzbeec6f10662010-02-09 11:16:15 -0800162 dvmCompilerFlushAllRegs(cUnit); /* Send everything to home location */
Ben Cheng20d7e6c2011-02-18 17:12:42 -0800163 LOAD_FUNC_ADDR(cUnit, r14lr, (int)funct);
Ben Cheng5d90c202009-11-22 23:31:11 -0800164 loadValueDirectWideFixed(cUnit, rlSrc1, r0, r1);
165 loadValueDirectWideFixed(cUnit, rlSrc2, r2, r3);
Ben Cheng20d7e6c2011-02-18 17:12:42 -0800166 opReg(cUnit, kOpBlx, r14lr);
Elliott Hughes6a555132010-02-25 15:41:42 -0800167 dvmCompilerClobberCallRegs(cUnit);
Bill Buzbeec6f10662010-02-09 11:16:15 -0800168 rlResult = dvmCompilerGetReturnWide(cUnit);
Ben Cheng5d90c202009-11-22 23:31:11 -0800169 storeValueWide(cUnit, rlDest, rlResult);
Ben Chengd72564c2011-02-08 17:09:25 -0800170#if defined(WITH_SELF_VERIFICATION)
171 cUnit->usesLinkRegister = true;
172#endif
Ben Cheng5d90c202009-11-22 23:31:11 -0800173 return false;
174}
175
176static bool genConversionPortable(CompilationUnit *cUnit, MIR *mir)
177{
Dan Bornstein9a1f8162010-12-01 17:02:26 -0800178 Opcode opcode = mir->dalvikInsn.opcode;
Ben Cheng5d90c202009-11-22 23:31:11 -0800179
Dan Bornstein9a1f8162010-12-01 17:02:26 -0800180 switch (opcode) {
Ben Cheng5d90c202009-11-22 23:31:11 -0800181 case OP_INT_TO_FLOAT:
182 return genConversionCall(cUnit, mir, (void*)__aeabi_i2f, 1, 1);
183 case OP_FLOAT_TO_INT:
184 return genConversionCall(cUnit, mir, (void*)__aeabi_f2iz, 1, 1);
185 case OP_DOUBLE_TO_FLOAT:
186 return genConversionCall(cUnit, mir, (void*)__aeabi_d2f, 2, 1);
187 case OP_FLOAT_TO_DOUBLE:
188 return genConversionCall(cUnit, mir, (void*)__aeabi_f2d, 1, 2);
189 case OP_INT_TO_DOUBLE:
190 return genConversionCall(cUnit, mir, (void*)__aeabi_i2d, 1, 2);
191 case OP_DOUBLE_TO_INT:
192 return genConversionCall(cUnit, mir, (void*)__aeabi_d2iz, 2, 1);
193 case OP_FLOAT_TO_LONG:
194 return genConversionCall(cUnit, mir, (void*)dvmJitf2l, 1, 2);
195 case OP_LONG_TO_FLOAT:
196 return genConversionCall(cUnit, mir, (void*)__aeabi_l2f, 2, 1);
197 case OP_DOUBLE_TO_LONG:
198 return genConversionCall(cUnit, mir, (void*)dvmJitd2l, 2, 2);
199 case OP_LONG_TO_DOUBLE:
200 return genConversionCall(cUnit, mir, (void*)__aeabi_l2d, 2, 2);
201 default:
202 return true;
203 }
204 return false;
205}
Ben Chengba4fc8b2009-06-01 13:00:29 -0700206
Jeff Hao97319a82009-08-12 16:57:15 -0700207#if defined(WITH_SELF_VERIFICATION)
Dan Bornstein9a1f8162010-12-01 17:02:26 -0800208static void selfVerificationBranchInsert(LIR *currentLIR, ArmOpcode opcode,
jeffhao9e45c0b2010-02-03 10:24:05 -0800209 int dest, int src1)
Jeff Hao97319a82009-08-12 16:57:15 -0700210{
Carl Shapirofc75f3e2010-12-07 11:43:38 -0800211 ArmLIR *insn = (ArmLIR *) dvmCompilerNew(sizeof(ArmLIR), true);
Dan Bornstein9a1f8162010-12-01 17:02:26 -0800212 insn->opcode = opcode;
jeffhao9e45c0b2010-02-03 10:24:05 -0800213 insn->operands[0] = dest;
214 insn->operands[1] = src1;
215 setupResourceMasks(insn);
216 dvmCompilerInsertLIRBefore(currentLIR, (LIR *) insn);
Jeff Hao97319a82009-08-12 16:57:15 -0700217}
218
Ben Chengd72564c2011-02-08 17:09:25 -0800219/*
220 * Example where r14 (LR) is preserved around a heap access under
221 * self-verification mode in Thumb2:
222 *
Ben Cheng20d7e6c2011-02-18 17:12:42 -0800223 * D/dalvikvm( 1538): 0x59414c5e (0026): ldr r14, [r15pc, #220] <-hoisted
Ben Chengd72564c2011-02-08 17:09:25 -0800224 * D/dalvikvm( 1538): 0x59414c62 (002a): mla r4, r0, r8, r4
225 * D/dalvikvm( 1538): 0x59414c66 (002e): adds r3, r4, r3
226 * D/dalvikvm( 1538): 0x59414c6a (0032): push <r5, r14> ---+
227 * D/dalvikvm( 1538): 0x59414c6c (0034): blx_1 0x5940f494 |
228 * D/dalvikvm( 1538): 0x59414c6e (0036): blx_2 see above <-MEM_OP_DECODE
229 * D/dalvikvm( 1538): 0x59414c70 (0038): ldr r10, [r9, #0] |
230 * D/dalvikvm( 1538): 0x59414c74 (003c): pop <r5, r14> ---+
231 * D/dalvikvm( 1538): 0x59414c78 (0040): mov r11, r10
232 * D/dalvikvm( 1538): 0x59414c7a (0042): asr r12, r11, #31
233 * D/dalvikvm( 1538): 0x59414c7e (0046): movs r0, r2
234 * D/dalvikvm( 1538): 0x59414c80 (0048): movs r1, r3
235 * D/dalvikvm( 1538): 0x59414c82 (004a): str r2, [r5, #16]
236 * D/dalvikvm( 1538): 0x59414c84 (004c): mov r2, r11
237 * D/dalvikvm( 1538): 0x59414c86 (004e): str r3, [r5, #20]
238 * D/dalvikvm( 1538): 0x59414c88 (0050): mov r3, r12
239 * D/dalvikvm( 1538): 0x59414c8a (0052): str r11, [r5, #24]
240 * D/dalvikvm( 1538): 0x59414c8e (0056): str r12, [r5, #28]
241 * D/dalvikvm( 1538): 0x59414c92 (005a): blx r14 <-use of LR
242 *
243 */
jeffhao9e45c0b2010-02-03 10:24:05 -0800244static void selfVerificationBranchInsertPass(CompilationUnit *cUnit)
Jeff Hao97319a82009-08-12 16:57:15 -0700245{
jeffhao9e45c0b2010-02-03 10:24:05 -0800246 ArmLIR *thisLIR;
Dan Bornstein9a1f8162010-12-01 17:02:26 -0800247 TemplateOpcode opcode = TEMPLATE_MEM_OP_DECODE;
Jeff Hao97319a82009-08-12 16:57:15 -0700248
jeffhao9e45c0b2010-02-03 10:24:05 -0800249 for (thisLIR = (ArmLIR *) cUnit->firstLIRInsn;
250 thisLIR != (ArmLIR *) cUnit->lastLIRInsn;
251 thisLIR = NEXT_LIR(thisLIR)) {
Ben Chengd72564c2011-02-08 17:09:25 -0800252 if (!thisLIR->flags.isNop && thisLIR->flags.insertWrapper) {
253 /*
254 * Push r5(FP) and r14(LR) onto stack. We need to make sure that
255 * SP is 8-byte aligned, and we use r5 as a temp to restore LR
256 * for Thumb-only target since LR cannot be directly accessed in
257 * Thumb mode. Another reason to choose r5 here is it is the Dalvik
258 * frame pointer and cannot be the target of the emulated heap
259 * load.
260 */
261 if (cUnit->usesLinkRegister) {
262 genSelfVerificationPreBranch(cUnit, thisLIR);
263 }
264
jeffhao9e45c0b2010-02-03 10:24:05 -0800265 /* Branch to mem op decode template */
266 selfVerificationBranchInsert((LIR *) thisLIR, kThumbBlx1,
Dan Bornstein9a1f8162010-12-01 17:02:26 -0800267 (int) gDvmJit.codeCache + templateEntryOffsets[opcode],
268 (int) gDvmJit.codeCache + templateEntryOffsets[opcode]);
jeffhao9e45c0b2010-02-03 10:24:05 -0800269 selfVerificationBranchInsert((LIR *) thisLIR, kThumbBlx2,
Dan Bornstein9a1f8162010-12-01 17:02:26 -0800270 (int) gDvmJit.codeCache + templateEntryOffsets[opcode],
271 (int) gDvmJit.codeCache + templateEntryOffsets[opcode]);
Ben Chengd72564c2011-02-08 17:09:25 -0800272
273 /* Restore LR */
274 if (cUnit->usesLinkRegister) {
275 genSelfVerificationPostBranch(cUnit, thisLIR);
276 }
Jeff Hao97319a82009-08-12 16:57:15 -0700277 }
278 }
Jeff Hao97319a82009-08-12 16:57:15 -0700279}
Jeff Hao97319a82009-08-12 16:57:15 -0700280#endif
281
Bill Buzbeebe6534f2010-03-12 16:01:35 -0800282/* Generate conditional branch instructions */
283static ArmLIR *genConditionalBranch(CompilationUnit *cUnit,
284 ArmConditionCode cond,
285 ArmLIR *target)
286{
287 ArmLIR *branch = opCondBranch(cUnit, cond);
288 branch->generic.target = (LIR *) target;
289 return branch;
290}
291
Ben Chengba4fc8b2009-06-01 13:00:29 -0700292/* Generate a unconditional branch to go to the interpreter */
Bill Buzbee89efc3d2009-07-28 11:22:22 -0700293static inline ArmLIR *genTrap(CompilationUnit *cUnit, int dOffset,
294 ArmLIR *pcrLabel)
Ben Chengba4fc8b2009-06-01 13:00:29 -0700295{
Bill Buzbee1465db52009-09-23 17:17:35 -0700296 ArmLIR *branch = opNone(cUnit, kOpUncondBr);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700297 return genCheckCommon(cUnit, dOffset, branch, pcrLabel);
298}
299
300/* Load a wide field from an object instance */
301static void genIGetWide(CompilationUnit *cUnit, MIR *mir, int fieldOffset)
302{
Bill Buzbeec6f10662010-02-09 11:16:15 -0800303 RegLocation rlObj = dvmCompilerGetSrc(cUnit, mir, 0);
304 RegLocation rlDest = dvmCompilerGetDestWide(cUnit, mir, 0, 1);
Bill Buzbee1465db52009-09-23 17:17:35 -0700305 RegLocation rlResult;
306 rlObj = loadValue(cUnit, rlObj, kCoreReg);
Bill Buzbeec6f10662010-02-09 11:16:15 -0800307 int regPtr = dvmCompilerAllocTemp(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700308
Bill Buzbee1465db52009-09-23 17:17:35 -0700309 assert(rlDest.wide);
Ben Chenge9695e52009-06-16 16:11:47 -0700310
Bill Buzbee1465db52009-09-23 17:17:35 -0700311 genNullCheck(cUnit, rlObj.sRegLow, rlObj.lowReg, mir->offset,
312 NULL);/* null object? */
313 opRegRegImm(cUnit, kOpAdd, regPtr, rlObj.lowReg, fieldOffset);
Bill Buzbeec6f10662010-02-09 11:16:15 -0800314 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kAnyReg, true);
Ben Cheng11d8f142010-03-24 15:24:19 -0700315
316 HEAP_ACCESS_SHADOW(true);
Bill Buzbee1465db52009-09-23 17:17:35 -0700317 loadPair(cUnit, regPtr, rlResult.lowReg, rlResult.highReg);
Ben Cheng11d8f142010-03-24 15:24:19 -0700318 HEAP_ACCESS_SHADOW(false);
319
Bill Buzbeec6f10662010-02-09 11:16:15 -0800320 dvmCompilerFreeTemp(cUnit, regPtr);
Bill Buzbee1465db52009-09-23 17:17:35 -0700321 storeValueWide(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700322}
323
324/* Store a wide field to an object instance */
325static void genIPutWide(CompilationUnit *cUnit, MIR *mir, int fieldOffset)
326{
Bill Buzbeec6f10662010-02-09 11:16:15 -0800327 RegLocation rlSrc = dvmCompilerGetSrcWide(cUnit, mir, 0, 1);
328 RegLocation rlObj = dvmCompilerGetSrc(cUnit, mir, 2);
Bill Buzbee1465db52009-09-23 17:17:35 -0700329 rlObj = loadValue(cUnit, rlObj, kCoreReg);
330 int regPtr;
331 rlSrc = loadValueWide(cUnit, rlSrc, kAnyReg);
332 genNullCheck(cUnit, rlObj.sRegLow, rlObj.lowReg, mir->offset,
333 NULL);/* null object? */
Bill Buzbeec6f10662010-02-09 11:16:15 -0800334 regPtr = dvmCompilerAllocTemp(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -0700335 opRegRegImm(cUnit, kOpAdd, regPtr, rlObj.lowReg, fieldOffset);
Ben Cheng11d8f142010-03-24 15:24:19 -0700336
337 HEAP_ACCESS_SHADOW(true);
Bill Buzbee1465db52009-09-23 17:17:35 -0700338 storePair(cUnit, regPtr, rlSrc.lowReg, rlSrc.highReg);
Ben Cheng11d8f142010-03-24 15:24:19 -0700339 HEAP_ACCESS_SHADOW(false);
340
Bill Buzbeec6f10662010-02-09 11:16:15 -0800341 dvmCompilerFreeTemp(cUnit, regPtr);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700342}
343
344/*
345 * Load a field from an object instance
346 *
Ben Chengba4fc8b2009-06-01 13:00:29 -0700347 */
Bill Buzbee270c1d62009-08-13 16:58:07 -0700348static void genIGet(CompilationUnit *cUnit, MIR *mir, OpSize size,
buzbeeecf8f6e2010-07-20 14:53:42 -0700349 int fieldOffset, bool isVolatile)
Ben Chengba4fc8b2009-06-01 13:00:29 -0700350{
Bill Buzbee1465db52009-09-23 17:17:35 -0700351 RegLocation rlResult;
Bill Buzbee749e8162010-07-07 06:55:56 -0700352 RegisterClass regClass = dvmCompilerRegClassBySize(size);
Bill Buzbeec6f10662010-02-09 11:16:15 -0800353 RegLocation rlObj = dvmCompilerGetSrc(cUnit, mir, 0);
354 RegLocation rlDest = dvmCompilerGetDest(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -0700355 rlObj = loadValue(cUnit, rlObj, kCoreReg);
Bill Buzbee749e8162010-07-07 06:55:56 -0700356 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, regClass, true);
Bill Buzbee1465db52009-09-23 17:17:35 -0700357 genNullCheck(cUnit, rlObj.sRegLow, rlObj.lowReg, mir->offset,
358 NULL);/* null object? */
Ben Cheng11d8f142010-03-24 15:24:19 -0700359
360 HEAP_ACCESS_SHADOW(true);
Ben Cheng5d90c202009-11-22 23:31:11 -0800361 loadBaseDisp(cUnit, mir, rlObj.lowReg, fieldOffset, rlResult.lowReg,
362 size, rlObj.sRegLow);
Ben Cheng11d8f142010-03-24 15:24:19 -0700363 HEAP_ACCESS_SHADOW(false);
buzbeeecf8f6e2010-07-20 14:53:42 -0700364 if (isVolatile) {
buzbee2ce33c92010-11-01 15:53:27 -0700365 dvmCompilerGenMemBarrier(cUnit, kSY);
buzbeeecf8f6e2010-07-20 14:53:42 -0700366 }
Ben Cheng11d8f142010-03-24 15:24:19 -0700367
Bill Buzbee1465db52009-09-23 17:17:35 -0700368 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700369}
370
371/*
372 * Store a field to an object instance
373 *
Ben Chengba4fc8b2009-06-01 13:00:29 -0700374 */
Bill Buzbee270c1d62009-08-13 16:58:07 -0700375static void genIPut(CompilationUnit *cUnit, MIR *mir, OpSize size,
buzbeeecf8f6e2010-07-20 14:53:42 -0700376 int fieldOffset, bool isObject, bool isVolatile)
Ben Chengba4fc8b2009-06-01 13:00:29 -0700377{
Bill Buzbee749e8162010-07-07 06:55:56 -0700378 RegisterClass regClass = dvmCompilerRegClassBySize(size);
Bill Buzbeec6f10662010-02-09 11:16:15 -0800379 RegLocation rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
380 RegLocation rlObj = dvmCompilerGetSrc(cUnit, mir, 1);
Bill Buzbee1465db52009-09-23 17:17:35 -0700381 rlObj = loadValue(cUnit, rlObj, kCoreReg);
Bill Buzbee749e8162010-07-07 06:55:56 -0700382 rlSrc = loadValue(cUnit, rlSrc, regClass);
Bill Buzbee1465db52009-09-23 17:17:35 -0700383 genNullCheck(cUnit, rlObj.sRegLow, rlObj.lowReg, mir->offset,
384 NULL);/* null object? */
Ben Cheng11d8f142010-03-24 15:24:19 -0700385
buzbeeecf8f6e2010-07-20 14:53:42 -0700386 if (isVolatile) {
buzbee2ce33c92010-11-01 15:53:27 -0700387 dvmCompilerGenMemBarrier(cUnit, kSY);
buzbeeecf8f6e2010-07-20 14:53:42 -0700388 }
Ben Cheng11d8f142010-03-24 15:24:19 -0700389 HEAP_ACCESS_SHADOW(true);
Bill Buzbee1465db52009-09-23 17:17:35 -0700390 storeBaseDisp(cUnit, rlObj.lowReg, fieldOffset, rlSrc.lowReg, size);
Ben Cheng11d8f142010-03-24 15:24:19 -0700391 HEAP_ACCESS_SHADOW(false);
buzbee919eb062010-07-12 12:59:22 -0700392 if (isObject) {
393 /* NOTE: marking card based on object head */
394 markCard(cUnit, rlSrc.lowReg, rlObj.lowReg);
395 }
Ben Chengba4fc8b2009-06-01 13:00:29 -0700396}
397
398
Ben Chengba4fc8b2009-06-01 13:00:29 -0700399/*
400 * Generate array load
Ben Chengba4fc8b2009-06-01 13:00:29 -0700401 */
Bill Buzbee270c1d62009-08-13 16:58:07 -0700402static void genArrayGet(CompilationUnit *cUnit, MIR *mir, OpSize size,
Bill Buzbee1465db52009-09-23 17:17:35 -0700403 RegLocation rlArray, RegLocation rlIndex,
404 RegLocation rlDest, int scale)
Ben Chengba4fc8b2009-06-01 13:00:29 -0700405{
Bill Buzbee749e8162010-07-07 06:55:56 -0700406 RegisterClass regClass = dvmCompilerRegClassBySize(size);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700407 int lenOffset = offsetof(ArrayObject, length);
408 int dataOffset = offsetof(ArrayObject, contents);
Bill Buzbee1465db52009-09-23 17:17:35 -0700409 RegLocation rlResult;
410 rlArray = loadValue(cUnit, rlArray, kCoreReg);
411 rlIndex = loadValue(cUnit, rlIndex, kCoreReg);
412 int regPtr;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700413
414 /* null object? */
Ben Cheng4238ec22009-08-24 16:32:22 -0700415 ArmLIR * pcrLabel = NULL;
416
417 if (!(mir->OptimizationFlags & MIR_IGNORE_NULL_CHECK)) {
Bill Buzbee1465db52009-09-23 17:17:35 -0700418 pcrLabel = genNullCheck(cUnit, rlArray.sRegLow,
419 rlArray.lowReg, mir->offset, NULL);
Ben Cheng4238ec22009-08-24 16:32:22 -0700420 }
421
Bill Buzbeec6f10662010-02-09 11:16:15 -0800422 regPtr = dvmCompilerAllocTemp(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -0700423
Ben Cheng4238ec22009-08-24 16:32:22 -0700424 if (!(mir->OptimizationFlags & MIR_IGNORE_RANGE_CHECK)) {
Bill Buzbeec6f10662010-02-09 11:16:15 -0800425 int regLen = dvmCompilerAllocTemp(cUnit);
Ben Cheng4238ec22009-08-24 16:32:22 -0700426 /* Get len */
Bill Buzbee1465db52009-09-23 17:17:35 -0700427 loadWordDisp(cUnit, rlArray.lowReg, lenOffset, regLen);
428 /* regPtr -> array data */
429 opRegRegImm(cUnit, kOpAdd, regPtr, rlArray.lowReg, dataOffset);
430 genBoundsCheck(cUnit, rlIndex.lowReg, regLen, mir->offset,
431 pcrLabel);
Bill Buzbeec6f10662010-02-09 11:16:15 -0800432 dvmCompilerFreeTemp(cUnit, regLen);
Ben Cheng4238ec22009-08-24 16:32:22 -0700433 } else {
Bill Buzbee1465db52009-09-23 17:17:35 -0700434 /* regPtr -> array data */
435 opRegRegImm(cUnit, kOpAdd, regPtr, rlArray.lowReg, dataOffset);
Ben Cheng4238ec22009-08-24 16:32:22 -0700436 }
Bill Buzbee1465db52009-09-23 17:17:35 -0700437 if ((size == kLong) || (size == kDouble)) {
438 if (scale) {
Bill Buzbeec6f10662010-02-09 11:16:15 -0800439 int rNewIndex = dvmCompilerAllocTemp(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -0700440 opRegRegImm(cUnit, kOpLsl, rNewIndex, rlIndex.lowReg, scale);
441 opRegReg(cUnit, kOpAdd, regPtr, rNewIndex);
Bill Buzbeec6f10662010-02-09 11:16:15 -0800442 dvmCompilerFreeTemp(cUnit, rNewIndex);
Bill Buzbee1465db52009-09-23 17:17:35 -0700443 } else {
444 opRegReg(cUnit, kOpAdd, regPtr, rlIndex.lowReg);
445 }
Bill Buzbee749e8162010-07-07 06:55:56 -0700446 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, regClass, true);
Ben Cheng11d8f142010-03-24 15:24:19 -0700447
448 HEAP_ACCESS_SHADOW(true);
Bill Buzbee1465db52009-09-23 17:17:35 -0700449 loadPair(cUnit, regPtr, rlResult.lowReg, rlResult.highReg);
Ben Cheng11d8f142010-03-24 15:24:19 -0700450 HEAP_ACCESS_SHADOW(false);
451
Bill Buzbeec6f10662010-02-09 11:16:15 -0800452 dvmCompilerFreeTemp(cUnit, regPtr);
Bill Buzbee1465db52009-09-23 17:17:35 -0700453 storeValueWide(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700454 } else {
Bill Buzbee749e8162010-07-07 06:55:56 -0700455 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, regClass, true);
Ben Cheng11d8f142010-03-24 15:24:19 -0700456
457 HEAP_ACCESS_SHADOW(true);
Bill Buzbee1465db52009-09-23 17:17:35 -0700458 loadBaseIndexed(cUnit, regPtr, rlIndex.lowReg, rlResult.lowReg,
459 scale, size);
Ben Cheng11d8f142010-03-24 15:24:19 -0700460 HEAP_ACCESS_SHADOW(false);
461
Bill Buzbeec6f10662010-02-09 11:16:15 -0800462 dvmCompilerFreeTemp(cUnit, regPtr);
Bill Buzbee1465db52009-09-23 17:17:35 -0700463 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700464 }
465}
466
Ben Chengba4fc8b2009-06-01 13:00:29 -0700467/*
468 * Generate array store
469 *
Ben Chengba4fc8b2009-06-01 13:00:29 -0700470 */
Bill Buzbee270c1d62009-08-13 16:58:07 -0700471static void genArrayPut(CompilationUnit *cUnit, MIR *mir, OpSize size,
Bill Buzbee1465db52009-09-23 17:17:35 -0700472 RegLocation rlArray, RegLocation rlIndex,
473 RegLocation rlSrc, int scale)
Ben Chengba4fc8b2009-06-01 13:00:29 -0700474{
Bill Buzbee749e8162010-07-07 06:55:56 -0700475 RegisterClass regClass = dvmCompilerRegClassBySize(size);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700476 int lenOffset = offsetof(ArrayObject, length);
477 int dataOffset = offsetof(ArrayObject, contents);
478
Bill Buzbee1465db52009-09-23 17:17:35 -0700479 int regPtr;
480 rlArray = loadValue(cUnit, rlArray, kCoreReg);
481 rlIndex = loadValue(cUnit, rlIndex, kCoreReg);
Ben Chenge9695e52009-06-16 16:11:47 -0700482
Bill Buzbeec6f10662010-02-09 11:16:15 -0800483 if (dvmCompilerIsTemp(cUnit, rlArray.lowReg)) {
484 dvmCompilerClobber(cUnit, rlArray.lowReg);
Bill Buzbee1465db52009-09-23 17:17:35 -0700485 regPtr = rlArray.lowReg;
486 } else {
Bill Buzbeec6f10662010-02-09 11:16:15 -0800487 regPtr = dvmCompilerAllocTemp(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -0700488 genRegCopy(cUnit, regPtr, rlArray.lowReg);
489 }
Ben Chenge9695e52009-06-16 16:11:47 -0700490
Ben Cheng1efc9c52009-06-08 18:25:27 -0700491 /* null object? */
Ben Cheng4238ec22009-08-24 16:32:22 -0700492 ArmLIR * pcrLabel = NULL;
493
494 if (!(mir->OptimizationFlags & MIR_IGNORE_NULL_CHECK)) {
Bill Buzbee1465db52009-09-23 17:17:35 -0700495 pcrLabel = genNullCheck(cUnit, rlArray.sRegLow, rlArray.lowReg,
496 mir->offset, NULL);
Ben Cheng4238ec22009-08-24 16:32:22 -0700497 }
498
499 if (!(mir->OptimizationFlags & MIR_IGNORE_RANGE_CHECK)) {
Bill Buzbeec6f10662010-02-09 11:16:15 -0800500 int regLen = dvmCompilerAllocTemp(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -0700501 //NOTE: max live temps(4) here.
Ben Cheng4238ec22009-08-24 16:32:22 -0700502 /* Get len */
Bill Buzbee1465db52009-09-23 17:17:35 -0700503 loadWordDisp(cUnit, rlArray.lowReg, lenOffset, regLen);
504 /* regPtr -> array data */
505 opRegImm(cUnit, kOpAdd, regPtr, dataOffset);
506 genBoundsCheck(cUnit, rlIndex.lowReg, regLen, mir->offset,
507 pcrLabel);
Bill Buzbeec6f10662010-02-09 11:16:15 -0800508 dvmCompilerFreeTemp(cUnit, regLen);
Ben Cheng4238ec22009-08-24 16:32:22 -0700509 } else {
Bill Buzbee1465db52009-09-23 17:17:35 -0700510 /* regPtr -> array data */
511 opRegImm(cUnit, kOpAdd, regPtr, dataOffset);
Ben Cheng4238ec22009-08-24 16:32:22 -0700512 }
Bill Buzbee1465db52009-09-23 17:17:35 -0700513 /* at this point, regPtr points to array, 2 live temps */
Bill Buzbee1465db52009-09-23 17:17:35 -0700514 if ((size == kLong) || (size == kDouble)) {
515 //TODO: need specific wide routine that can handle fp regs
516 if (scale) {
Bill Buzbeec6f10662010-02-09 11:16:15 -0800517 int rNewIndex = dvmCompilerAllocTemp(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -0700518 opRegRegImm(cUnit, kOpLsl, rNewIndex, rlIndex.lowReg, scale);
519 opRegReg(cUnit, kOpAdd, regPtr, rNewIndex);
Bill Buzbeec6f10662010-02-09 11:16:15 -0800520 dvmCompilerFreeTemp(cUnit, rNewIndex);
Bill Buzbee1465db52009-09-23 17:17:35 -0700521 } else {
522 opRegReg(cUnit, kOpAdd, regPtr, rlIndex.lowReg);
523 }
Bill Buzbee749e8162010-07-07 06:55:56 -0700524 rlSrc = loadValueWide(cUnit, rlSrc, regClass);
Ben Cheng11d8f142010-03-24 15:24:19 -0700525
526 HEAP_ACCESS_SHADOW(true);
Bill Buzbee1465db52009-09-23 17:17:35 -0700527 storePair(cUnit, regPtr, rlSrc.lowReg, rlSrc.highReg);
Ben Cheng11d8f142010-03-24 15:24:19 -0700528 HEAP_ACCESS_SHADOW(false);
529
Bill Buzbeec6f10662010-02-09 11:16:15 -0800530 dvmCompilerFreeTemp(cUnit, regPtr);
Bill Buzbee270c1d62009-08-13 16:58:07 -0700531 } else {
Bill Buzbee749e8162010-07-07 06:55:56 -0700532 rlSrc = loadValue(cUnit, rlSrc, regClass);
Ben Cheng11d8f142010-03-24 15:24:19 -0700533
534 HEAP_ACCESS_SHADOW(true);
Bill Buzbee1465db52009-09-23 17:17:35 -0700535 storeBaseIndexed(cUnit, regPtr, rlIndex.lowReg, rlSrc.lowReg,
536 scale, size);
Ben Cheng11d8f142010-03-24 15:24:19 -0700537 HEAP_ACCESS_SHADOW(false);
jeffhao9e45c0b2010-02-03 10:24:05 -0800538 }
Ben Chengba4fc8b2009-06-01 13:00:29 -0700539}
540
Bill Buzbeebe6534f2010-03-12 16:01:35 -0800541/*
542 * Generate array object store
543 * Must use explicit register allocation here because of
544 * call-out to dvmCanPutArrayElement
545 */
546static void genArrayObjectPut(CompilationUnit *cUnit, MIR *mir,
547 RegLocation rlArray, RegLocation rlIndex,
548 RegLocation rlSrc, int scale)
549{
550 int lenOffset = offsetof(ArrayObject, length);
551 int dataOffset = offsetof(ArrayObject, contents);
552
553 dvmCompilerFlushAllRegs(cUnit);
554
555 int regLen = r0;
556 int regPtr = r4PC; /* Preserved across call */
557 int regArray = r1;
558 int regIndex = r7; /* Preserved across call */
559
560 loadValueDirectFixed(cUnit, rlArray, regArray);
561 loadValueDirectFixed(cUnit, rlIndex, regIndex);
562
563 /* null object? */
564 ArmLIR * pcrLabel = NULL;
565
566 if (!(mir->OptimizationFlags & MIR_IGNORE_NULL_CHECK)) {
567 pcrLabel = genNullCheck(cUnit, rlArray.sRegLow, regArray,
568 mir->offset, NULL);
569 }
570
571 if (!(mir->OptimizationFlags & MIR_IGNORE_RANGE_CHECK)) {
572 /* Get len */
573 loadWordDisp(cUnit, regArray, lenOffset, regLen);
574 /* regPtr -> array data */
575 opRegRegImm(cUnit, kOpAdd, regPtr, regArray, dataOffset);
576 genBoundsCheck(cUnit, regIndex, regLen, mir->offset,
577 pcrLabel);
578 } else {
579 /* regPtr -> array data */
580 opRegRegImm(cUnit, kOpAdd, regPtr, regArray, dataOffset);
581 }
582
583 /* Get object to store */
584 loadValueDirectFixed(cUnit, rlSrc, r0);
Ben Chengbd1326d2010-04-02 15:04:53 -0700585 LOAD_FUNC_ADDR(cUnit, r2, (int)dvmCanPutArrayElement);
Bill Buzbeebe6534f2010-03-12 16:01:35 -0800586
587 /* Are we storing null? If so, avoid check */
buzbee8f8109a2010-08-31 10:16:35 -0700588 ArmLIR *branchOver = genCmpImmBranch(cUnit, kArmCondEq, r0, 0);
Bill Buzbeebe6534f2010-03-12 16:01:35 -0800589
590 /* Make sure the types are compatible */
591 loadWordDisp(cUnit, regArray, offsetof(Object, clazz), r1);
592 loadWordDisp(cUnit, r0, offsetof(Object, clazz), r0);
593 opReg(cUnit, kOpBlx, r2);
594 dvmCompilerClobberCallRegs(cUnit);
Bill Buzbee900a3af2010-03-16 12:41:43 -0700595
596 /*
597 * Using fixed registers here, and counting on r4 and r7 being
598 * preserved across the above call. Tell the register allocation
599 * utilities about the regs we are using directly
600 */
601 dvmCompilerLockTemp(cUnit, regPtr); // r4PC
602 dvmCompilerLockTemp(cUnit, regIndex); // r7
603 dvmCompilerLockTemp(cUnit, r0);
buzbee919eb062010-07-12 12:59:22 -0700604 dvmCompilerLockTemp(cUnit, r1);
Bill Buzbee900a3af2010-03-16 12:41:43 -0700605
Bill Buzbeebe6534f2010-03-12 16:01:35 -0800606 /* Bad? - roll back and re-execute if so */
607 genRegImmCheck(cUnit, kArmCondEq, r0, 0, mir->offset, pcrLabel);
608
buzbee919eb062010-07-12 12:59:22 -0700609 /* Resume here - must reload element & array, regPtr & index preserved */
Bill Buzbeebe6534f2010-03-12 16:01:35 -0800610 loadValueDirectFixed(cUnit, rlSrc, r0);
buzbee919eb062010-07-12 12:59:22 -0700611 loadValueDirectFixed(cUnit, rlArray, r1);
Bill Buzbeebe6534f2010-03-12 16:01:35 -0800612
613 ArmLIR *target = newLIR0(cUnit, kArmPseudoTargetLabel);
614 target->defMask = ENCODE_ALL;
615 branchOver->generic.target = (LIR *) target;
616
Ben Cheng11d8f142010-03-24 15:24:19 -0700617 HEAP_ACCESS_SHADOW(true);
Bill Buzbeebe6534f2010-03-12 16:01:35 -0800618 storeBaseIndexed(cUnit, regPtr, regIndex, r0,
619 scale, kWord);
Ben Cheng11d8f142010-03-24 15:24:19 -0700620 HEAP_ACCESS_SHADOW(false);
buzbee919eb062010-07-12 12:59:22 -0700621
buzbeebaf196a2010-08-04 10:13:15 -0700622 dvmCompilerFreeTemp(cUnit, regPtr);
623 dvmCompilerFreeTemp(cUnit, regIndex);
624
buzbee919eb062010-07-12 12:59:22 -0700625 /* NOTE: marking card here based on object head */
626 markCard(cUnit, r0, r1);
Bill Buzbeebe6534f2010-03-12 16:01:35 -0800627}
628
Ben Cheng5d90c202009-11-22 23:31:11 -0800629static bool genShiftOpLong(CompilationUnit *cUnit, MIR *mir,
630 RegLocation rlDest, RegLocation rlSrc1,
631 RegLocation rlShift)
Ben Chengba4fc8b2009-06-01 13:00:29 -0700632{
Ben Chenge9695e52009-06-16 16:11:47 -0700633 /*
634 * Don't mess with the regsiters here as there is a particular calling
635 * convention to the out-of-line handler.
636 */
Bill Buzbee1465db52009-09-23 17:17:35 -0700637 RegLocation rlResult;
638
639 loadValueDirectWideFixed(cUnit, rlSrc1, r0, r1);
640 loadValueDirect(cUnit, rlShift, r2);
Dan Bornstein9a1f8162010-12-01 17:02:26 -0800641 switch( mir->dalvikInsn.opcode) {
Ben Chenge9695e52009-06-16 16:11:47 -0700642 case OP_SHL_LONG:
643 case OP_SHL_LONG_2ADDR:
644 genDispatchToHandler(cUnit, TEMPLATE_SHL_LONG);
645 break;
646 case OP_SHR_LONG:
647 case OP_SHR_LONG_2ADDR:
648 genDispatchToHandler(cUnit, TEMPLATE_SHR_LONG);
649 break;
650 case OP_USHR_LONG:
651 case OP_USHR_LONG_2ADDR:
652 genDispatchToHandler(cUnit, TEMPLATE_USHR_LONG);
653 break;
654 default:
655 return true;
656 }
Bill Buzbeec6f10662010-02-09 11:16:15 -0800657 rlResult = dvmCompilerGetReturnWide(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -0700658 storeValueWide(cUnit, rlDest, rlResult);
Ben Chenge9695e52009-06-16 16:11:47 -0700659 return false;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700660}
Ben Chenge9695e52009-06-16 16:11:47 -0700661
Ben Cheng5d90c202009-11-22 23:31:11 -0800662static bool genArithOpLong(CompilationUnit *cUnit, MIR *mir,
663 RegLocation rlDest, RegLocation rlSrc1,
664 RegLocation rlSrc2)
Ben Chengba4fc8b2009-06-01 13:00:29 -0700665{
Bill Buzbee1465db52009-09-23 17:17:35 -0700666 RegLocation rlResult;
667 OpKind firstOp = kOpBkpt;
668 OpKind secondOp = kOpBkpt;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700669 bool callOut = false;
670 void *callTgt;
671 int retReg = r0;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700672
Dan Bornstein9a1f8162010-12-01 17:02:26 -0800673 switch (mir->dalvikInsn.opcode) {
Ben Chengba4fc8b2009-06-01 13:00:29 -0700674 case OP_NOT_LONG:
Bill Buzbee1465db52009-09-23 17:17:35 -0700675 rlSrc2 = loadValueWide(cUnit, rlSrc2, kCoreReg);
Bill Buzbeec6f10662010-02-09 11:16:15 -0800676 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Bill Buzbee1465db52009-09-23 17:17:35 -0700677 opRegReg(cUnit, kOpMvn, rlResult.lowReg, rlSrc2.lowReg);
678 opRegReg(cUnit, kOpMvn, rlResult.highReg, rlSrc2.highReg);
679 storeValueWide(cUnit, rlDest, rlResult);
680 return false;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700681 break;
682 case OP_ADD_LONG:
683 case OP_ADD_LONG_2ADDR:
Bill Buzbee1465db52009-09-23 17:17:35 -0700684 firstOp = kOpAdd;
685 secondOp = kOpAdc;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700686 break;
687 case OP_SUB_LONG:
688 case OP_SUB_LONG_2ADDR:
Bill Buzbee1465db52009-09-23 17:17:35 -0700689 firstOp = kOpSub;
690 secondOp = kOpSbc;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700691 break;
692 case OP_MUL_LONG:
693 case OP_MUL_LONG_2ADDR:
Bill Buzbee1465db52009-09-23 17:17:35 -0700694 genMulLong(cUnit, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700695 return false;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700696 case OP_DIV_LONG:
697 case OP_DIV_LONG_2ADDR:
698 callOut = true;
699 retReg = r0;
700 callTgt = (void*)__aeabi_ldivmod;
701 break;
702 /* NOTE - result is in r2/r3 instead of r0/r1 */
703 case OP_REM_LONG:
704 case OP_REM_LONG_2ADDR:
705 callOut = true;
706 callTgt = (void*)__aeabi_ldivmod;
707 retReg = r2;
708 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700709 case OP_AND_LONG_2ADDR:
Bill Buzbee1465db52009-09-23 17:17:35 -0700710 case OP_AND_LONG:
711 firstOp = kOpAnd;
712 secondOp = kOpAnd;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700713 break;
714 case OP_OR_LONG:
715 case OP_OR_LONG_2ADDR:
Bill Buzbee1465db52009-09-23 17:17:35 -0700716 firstOp = kOpOr;
717 secondOp = kOpOr;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700718 break;
719 case OP_XOR_LONG:
720 case OP_XOR_LONG_2ADDR:
Bill Buzbee1465db52009-09-23 17:17:35 -0700721 firstOp = kOpXor;
722 secondOp = kOpXor;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700723 break;
Ben Chenge9695e52009-06-16 16:11:47 -0700724 case OP_NEG_LONG: {
Bill Buzbee51ecf602010-01-14 14:27:52 -0800725 //TUNING: can improve this using Thumb2 code
Bill Buzbeec6f10662010-02-09 11:16:15 -0800726 int tReg = dvmCompilerAllocTemp(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -0700727 rlSrc2 = loadValueWide(cUnit, rlSrc2, kCoreReg);
Bill Buzbeec6f10662010-02-09 11:16:15 -0800728 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Ben Chengbd1326d2010-04-02 15:04:53 -0700729 loadConstantNoClobber(cUnit, tReg, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -0700730 opRegRegReg(cUnit, kOpSub, rlResult.lowReg,
Bill Buzbee51ecf602010-01-14 14:27:52 -0800731 tReg, rlSrc2.lowReg);
732 opRegReg(cUnit, kOpSbc, tReg, rlSrc2.highReg);
733 genRegCopy(cUnit, rlResult.highReg, tReg);
Bill Buzbee1465db52009-09-23 17:17:35 -0700734 storeValueWide(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700735 return false;
Ben Chenge9695e52009-06-16 16:11:47 -0700736 }
Ben Chengba4fc8b2009-06-01 13:00:29 -0700737 default:
738 LOGE("Invalid long arith op");
Bill Buzbeefc519dc2010-03-06 23:30:57 -0800739 dvmCompilerAbort(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700740 }
741 if (!callOut) {
Bill Buzbee80cef862010-03-25 10:38:34 -0700742 genLong3Addr(cUnit, mir, firstOp, secondOp, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700743 } else {
Bill Buzbee1465db52009-09-23 17:17:35 -0700744 // Adjust return regs in to handle case of rem returning r2/r3
Bill Buzbeec6f10662010-02-09 11:16:15 -0800745 dvmCompilerFlushAllRegs(cUnit); /* Send everything to home location */
Bill Buzbee1465db52009-09-23 17:17:35 -0700746 loadValueDirectWideFixed(cUnit, rlSrc1, r0, r1);
Ben Cheng20d7e6c2011-02-18 17:12:42 -0800747 LOAD_FUNC_ADDR(cUnit, r14lr, (int) callTgt);
Bill Buzbee1465db52009-09-23 17:17:35 -0700748 loadValueDirectWideFixed(cUnit, rlSrc2, r2, r3);
Ben Cheng20d7e6c2011-02-18 17:12:42 -0800749 opReg(cUnit, kOpBlx, r14lr);
Elliott Hughes6a555132010-02-25 15:41:42 -0800750 dvmCompilerClobberCallRegs(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -0700751 if (retReg == r0)
Bill Buzbeec6f10662010-02-09 11:16:15 -0800752 rlResult = dvmCompilerGetReturnWide(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -0700753 else
Bill Buzbeec6f10662010-02-09 11:16:15 -0800754 rlResult = dvmCompilerGetReturnWideAlt(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -0700755 storeValueWide(cUnit, rlDest, rlResult);
Ben Chengd72564c2011-02-08 17:09:25 -0800756#if defined(WITH_SELF_VERIFICATION)
757 cUnit->usesLinkRegister = true;
758#endif
Ben Chengba4fc8b2009-06-01 13:00:29 -0700759 }
760 return false;
761}
762
Ben Cheng5d90c202009-11-22 23:31:11 -0800763static bool genArithOpInt(CompilationUnit *cUnit, MIR *mir,
764 RegLocation rlDest, RegLocation rlSrc1,
765 RegLocation rlSrc2)
Ben Chengba4fc8b2009-06-01 13:00:29 -0700766{
Bill Buzbee1465db52009-09-23 17:17:35 -0700767 OpKind op = kOpBkpt;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700768 bool callOut = false;
769 bool checkZero = false;
Bill Buzbee1465db52009-09-23 17:17:35 -0700770 bool unary = false;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700771 int retReg = r0;
772 void *callTgt;
Bill Buzbee1465db52009-09-23 17:17:35 -0700773 RegLocation rlResult;
Bill Buzbee0e605272009-12-01 14:28:05 -0800774 bool shiftOp = false;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700775
Dan Bornstein9a1f8162010-12-01 17:02:26 -0800776 switch (mir->dalvikInsn.opcode) {
Ben Chengba4fc8b2009-06-01 13:00:29 -0700777 case OP_NEG_INT:
Bill Buzbee1465db52009-09-23 17:17:35 -0700778 op = kOpNeg;
779 unary = true;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700780 break;
781 case OP_NOT_INT:
Bill Buzbee1465db52009-09-23 17:17:35 -0700782 op = kOpMvn;
783 unary = true;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700784 break;
785 case OP_ADD_INT:
786 case OP_ADD_INT_2ADDR:
Bill Buzbee1465db52009-09-23 17:17:35 -0700787 op = kOpAdd;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700788 break;
789 case OP_SUB_INT:
790 case OP_SUB_INT_2ADDR:
Bill Buzbee1465db52009-09-23 17:17:35 -0700791 op = kOpSub;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700792 break;
793 case OP_MUL_INT:
794 case OP_MUL_INT_2ADDR:
Bill Buzbee1465db52009-09-23 17:17:35 -0700795 op = kOpMul;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700796 break;
797 case OP_DIV_INT:
798 case OP_DIV_INT_2ADDR:
799 callOut = true;
800 checkZero = true;
801 callTgt = __aeabi_idiv;
802 retReg = r0;
803 break;
804 /* NOTE: returns in r1 */
805 case OP_REM_INT:
806 case OP_REM_INT_2ADDR:
807 callOut = true;
808 checkZero = true;
809 callTgt = __aeabi_idivmod;
810 retReg = r1;
811 break;
812 case OP_AND_INT:
813 case OP_AND_INT_2ADDR:
Bill Buzbee1465db52009-09-23 17:17:35 -0700814 op = kOpAnd;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700815 break;
816 case OP_OR_INT:
817 case OP_OR_INT_2ADDR:
Bill Buzbee1465db52009-09-23 17:17:35 -0700818 op = kOpOr;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700819 break;
820 case OP_XOR_INT:
821 case OP_XOR_INT_2ADDR:
Bill Buzbee1465db52009-09-23 17:17:35 -0700822 op = kOpXor;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700823 break;
824 case OP_SHL_INT:
825 case OP_SHL_INT_2ADDR:
Bill Buzbee0e605272009-12-01 14:28:05 -0800826 shiftOp = true;
Bill Buzbee1465db52009-09-23 17:17:35 -0700827 op = kOpLsl;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700828 break;
829 case OP_SHR_INT:
830 case OP_SHR_INT_2ADDR:
Bill Buzbee0e605272009-12-01 14:28:05 -0800831 shiftOp = true;
Bill Buzbee1465db52009-09-23 17:17:35 -0700832 op = kOpAsr;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700833 break;
834 case OP_USHR_INT:
835 case OP_USHR_INT_2ADDR:
Bill Buzbee0e605272009-12-01 14:28:05 -0800836 shiftOp = true;
Bill Buzbee1465db52009-09-23 17:17:35 -0700837 op = kOpLsr;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700838 break;
839 default:
840 LOGE("Invalid word arith op: 0x%x(%d)",
Dan Bornstein9a1f8162010-12-01 17:02:26 -0800841 mir->dalvikInsn.opcode, mir->dalvikInsn.opcode);
Bill Buzbeefc519dc2010-03-06 23:30:57 -0800842 dvmCompilerAbort(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700843 }
844 if (!callOut) {
Bill Buzbee1465db52009-09-23 17:17:35 -0700845 rlSrc1 = loadValue(cUnit, rlSrc1, kCoreReg);
846 if (unary) {
Bill Buzbeec6f10662010-02-09 11:16:15 -0800847 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Bill Buzbee1465db52009-09-23 17:17:35 -0700848 opRegReg(cUnit, op, rlResult.lowReg,
849 rlSrc1.lowReg);
Ben Chenge9695e52009-06-16 16:11:47 -0700850 } else {
Bill Buzbee1465db52009-09-23 17:17:35 -0700851 rlSrc2 = loadValue(cUnit, rlSrc2, kCoreReg);
Bill Buzbee0e605272009-12-01 14:28:05 -0800852 if (shiftOp) {
Bill Buzbeec6f10662010-02-09 11:16:15 -0800853 int tReg = dvmCompilerAllocTemp(cUnit);
Bill Buzbee0e605272009-12-01 14:28:05 -0800854 opRegRegImm(cUnit, kOpAnd, tReg, rlSrc2.lowReg, 31);
Bill Buzbeec6f10662010-02-09 11:16:15 -0800855 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Bill Buzbee0e605272009-12-01 14:28:05 -0800856 opRegRegReg(cUnit, op, rlResult.lowReg,
857 rlSrc1.lowReg, tReg);
Bill Buzbeec6f10662010-02-09 11:16:15 -0800858 dvmCompilerFreeTemp(cUnit, tReg);
Bill Buzbee0e605272009-12-01 14:28:05 -0800859 } else {
Bill Buzbeec6f10662010-02-09 11:16:15 -0800860 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Bill Buzbee0e605272009-12-01 14:28:05 -0800861 opRegRegReg(cUnit, op, rlResult.lowReg,
862 rlSrc1.lowReg, rlSrc2.lowReg);
863 }
Ben Chenge9695e52009-06-16 16:11:47 -0700864 }
Bill Buzbee1465db52009-09-23 17:17:35 -0700865 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700866 } else {
Bill Buzbee1465db52009-09-23 17:17:35 -0700867 RegLocation rlResult;
Bill Buzbeec6f10662010-02-09 11:16:15 -0800868 dvmCompilerFlushAllRegs(cUnit); /* Send everything to home location */
Bill Buzbee1465db52009-09-23 17:17:35 -0700869 loadValueDirectFixed(cUnit, rlSrc2, r1);
Ben Chengbd1326d2010-04-02 15:04:53 -0700870 LOAD_FUNC_ADDR(cUnit, r2, (int) callTgt);
Bill Buzbee1465db52009-09-23 17:17:35 -0700871 loadValueDirectFixed(cUnit, rlSrc1, r0);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700872 if (checkZero) {
Bill Buzbee1465db52009-09-23 17:17:35 -0700873 genNullCheck(cUnit, rlSrc2.sRegLow, r1, mir->offset, NULL);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700874 }
Bill Buzbee1465db52009-09-23 17:17:35 -0700875 opReg(cUnit, kOpBlx, r2);
Elliott Hughes6a555132010-02-25 15:41:42 -0800876 dvmCompilerClobberCallRegs(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -0700877 if (retReg == r0)
Bill Buzbeec6f10662010-02-09 11:16:15 -0800878 rlResult = dvmCompilerGetReturn(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -0700879 else
Bill Buzbeec6f10662010-02-09 11:16:15 -0800880 rlResult = dvmCompilerGetReturnAlt(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -0700881 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700882 }
883 return false;
884}
885
Ben Cheng5d90c202009-11-22 23:31:11 -0800886static bool genArithOp(CompilationUnit *cUnit, MIR *mir)
Ben Chengba4fc8b2009-06-01 13:00:29 -0700887{
Dan Bornstein9a1f8162010-12-01 17:02:26 -0800888 Opcode opcode = mir->dalvikInsn.opcode;
Bill Buzbee1465db52009-09-23 17:17:35 -0700889 RegLocation rlDest;
890 RegLocation rlSrc1;
891 RegLocation rlSrc2;
892 /* Deduce sizes of operands */
893 if (mir->ssaRep->numUses == 2) {
Bill Buzbeec6f10662010-02-09 11:16:15 -0800894 rlSrc1 = dvmCompilerGetSrc(cUnit, mir, 0);
895 rlSrc2 = dvmCompilerGetSrc(cUnit, mir, 1);
Bill Buzbee1465db52009-09-23 17:17:35 -0700896 } else if (mir->ssaRep->numUses == 3) {
Bill Buzbeec6f10662010-02-09 11:16:15 -0800897 rlSrc1 = dvmCompilerGetSrcWide(cUnit, mir, 0, 1);
898 rlSrc2 = dvmCompilerGetSrc(cUnit, mir, 2);
Bill Buzbee1465db52009-09-23 17:17:35 -0700899 } else {
Bill Buzbeec6f10662010-02-09 11:16:15 -0800900 rlSrc1 = dvmCompilerGetSrcWide(cUnit, mir, 0, 1);
901 rlSrc2 = dvmCompilerGetSrcWide(cUnit, mir, 2, 3);
Bill Buzbee1465db52009-09-23 17:17:35 -0700902 assert(mir->ssaRep->numUses == 4);
903 }
904 if (mir->ssaRep->numDefs == 1) {
Bill Buzbeec6f10662010-02-09 11:16:15 -0800905 rlDest = dvmCompilerGetDest(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -0700906 } else {
907 assert(mir->ssaRep->numDefs == 2);
Bill Buzbeec6f10662010-02-09 11:16:15 -0800908 rlDest = dvmCompilerGetDestWide(cUnit, mir, 0, 1);
Bill Buzbee1465db52009-09-23 17:17:35 -0700909 }
Ben Chengba4fc8b2009-06-01 13:00:29 -0700910
Dan Bornstein9a1f8162010-12-01 17:02:26 -0800911 if ((opcode >= OP_ADD_LONG_2ADDR) && (opcode <= OP_XOR_LONG_2ADDR)) {
Ben Cheng5d90c202009-11-22 23:31:11 -0800912 return genArithOpLong(cUnit,mir, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700913 }
Dan Bornstein9a1f8162010-12-01 17:02:26 -0800914 if ((opcode >= OP_ADD_LONG) && (opcode <= OP_XOR_LONG)) {
Ben Cheng5d90c202009-11-22 23:31:11 -0800915 return genArithOpLong(cUnit,mir, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700916 }
Dan Bornstein9a1f8162010-12-01 17:02:26 -0800917 if ((opcode >= OP_SHL_LONG_2ADDR) && (opcode <= OP_USHR_LONG_2ADDR)) {
Ben Cheng5d90c202009-11-22 23:31:11 -0800918 return genShiftOpLong(cUnit,mir, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700919 }
Dan Bornstein9a1f8162010-12-01 17:02:26 -0800920 if ((opcode >= OP_SHL_LONG) && (opcode <= OP_USHR_LONG)) {
Ben Cheng5d90c202009-11-22 23:31:11 -0800921 return genShiftOpLong(cUnit,mir, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700922 }
Dan Bornstein9a1f8162010-12-01 17:02:26 -0800923 if ((opcode >= OP_ADD_INT_2ADDR) && (opcode <= OP_USHR_INT_2ADDR)) {
Ben Cheng5d90c202009-11-22 23:31:11 -0800924 return genArithOpInt(cUnit,mir, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700925 }
Dan Bornstein9a1f8162010-12-01 17:02:26 -0800926 if ((opcode >= OP_ADD_INT) && (opcode <= OP_USHR_INT)) {
Ben Cheng5d90c202009-11-22 23:31:11 -0800927 return genArithOpInt(cUnit,mir, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700928 }
Dan Bornstein9a1f8162010-12-01 17:02:26 -0800929 if ((opcode >= OP_ADD_FLOAT_2ADDR) && (opcode <= OP_REM_FLOAT_2ADDR)) {
Ben Cheng5d90c202009-11-22 23:31:11 -0800930 return genArithOpFloat(cUnit,mir, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700931 }
Dan Bornstein9a1f8162010-12-01 17:02:26 -0800932 if ((opcode >= OP_ADD_FLOAT) && (opcode <= OP_REM_FLOAT)) {
Ben Cheng5d90c202009-11-22 23:31:11 -0800933 return genArithOpFloat(cUnit, mir, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700934 }
Dan Bornstein9a1f8162010-12-01 17:02:26 -0800935 if ((opcode >= OP_ADD_DOUBLE_2ADDR) && (opcode <= OP_REM_DOUBLE_2ADDR)) {
Ben Cheng5d90c202009-11-22 23:31:11 -0800936 return genArithOpDouble(cUnit,mir, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700937 }
Dan Bornstein9a1f8162010-12-01 17:02:26 -0800938 if ((opcode >= OP_ADD_DOUBLE) && (opcode <= OP_REM_DOUBLE)) {
Ben Cheng5d90c202009-11-22 23:31:11 -0800939 return genArithOpDouble(cUnit,mir, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700940 }
941 return true;
942}
943
Bill Buzbee1465db52009-09-23 17:17:35 -0700944/* Generate unconditional branch instructions */
945static ArmLIR *genUnconditionalBranch(CompilationUnit *cUnit, ArmLIR *target)
946{
947 ArmLIR *branch = opNone(cUnit, kOpUncondBr);
948 branch->generic.target = (LIR *) target;
949 return branch;
950}
951
Bill Buzbee1465db52009-09-23 17:17:35 -0700952/* Perform the actual operation for OP_RETURN_* */
953static void genReturnCommon(CompilationUnit *cUnit, MIR *mir)
954{
Ben Cheng20d7e6c2011-02-18 17:12:42 -0800955 genDispatchToHandler(cUnit, gDvmJit.methodTraceSupport ?
956 TEMPLATE_RETURN_PROF : TEMPLATE_RETURN);
Ben Cheng978738d2010-05-13 13:45:57 -0700957#if defined(WITH_JIT_TUNING)
Ben Cheng20d7e6c2011-02-18 17:12:42 -0800958 gDvmJit.returnOp++;
Bill Buzbee1465db52009-09-23 17:17:35 -0700959#endif
Ben Cheng20d7e6c2011-02-18 17:12:42 -0800960 int dPC = (int) (cUnit->method->insns + mir->offset);
961 /* Insert branch, but defer setting of target */
962 ArmLIR *branch = genUnconditionalBranch(cUnit, NULL);
963 /* Set up the place holder to reconstruct this Dalvik PC */
964 ArmLIR *pcrLabel = (ArmLIR *) dvmCompilerNew(sizeof(ArmLIR), true);
965 pcrLabel->opcode = kArmPseudoPCReconstructionCell;
966 pcrLabel->operands[0] = dPC;
967 pcrLabel->operands[1] = mir->offset;
968 /* Insert the place holder to the growable list */
969 dvmInsertGrowableList(&cUnit->pcReconstructionList, (intptr_t) pcrLabel);
970 /* Branch to the PC reconstruction code */
971 branch->generic.target = (LIR *) pcrLabel;
Bill Buzbee1465db52009-09-23 17:17:35 -0700972}
973
Ben Chengba4fc8b2009-06-01 13:00:29 -0700974static void genProcessArgsNoRange(CompilationUnit *cUnit, MIR *mir,
975 DecodedInstruction *dInsn,
Bill Buzbee89efc3d2009-07-28 11:22:22 -0700976 ArmLIR **pcrLabel)
Ben Chengba4fc8b2009-06-01 13:00:29 -0700977{
978 unsigned int i;
979 unsigned int regMask = 0;
Bill Buzbee1465db52009-09-23 17:17:35 -0700980 RegLocation rlArg;
981 int numDone = 0;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700982
Bill Buzbee1465db52009-09-23 17:17:35 -0700983 /*
984 * Load arguments to r0..r4. Note that these registers may contain
985 * live values, so we clobber them immediately after loading to prevent
986 * them from being used as sources for subsequent loads.
987 */
Bill Buzbeec6f10662010-02-09 11:16:15 -0800988 dvmCompilerLockAllTemps(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700989 for (i = 0; i < dInsn->vA; i++) {
990 regMask |= 1 << i;
Bill Buzbeec6f10662010-02-09 11:16:15 -0800991 rlArg = dvmCompilerGetSrc(cUnit, mir, numDone++);
Bill Buzbee1465db52009-09-23 17:17:35 -0700992 loadValueDirectFixed(cUnit, rlArg, i);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700993 }
994 if (regMask) {
995 /* Up to 5 args are pushed on top of FP - sizeofStackSaveArea */
Ben Cheng20d7e6c2011-02-18 17:12:42 -0800996 opRegRegImm(cUnit, kOpSub, r7, r5FP,
Bill Buzbee1465db52009-09-23 17:17:35 -0700997 sizeof(StackSaveArea) + (dInsn->vA << 2));
Ben Chengba4fc8b2009-06-01 13:00:29 -0700998 /* generate null check */
999 if (pcrLabel) {
Bill Buzbeec6f10662010-02-09 11:16:15 -08001000 *pcrLabel = genNullCheck(cUnit, dvmCompilerSSASrc(mir, 0), r0,
Bill Buzbee1465db52009-09-23 17:17:35 -07001001 mir->offset, NULL);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001002 }
Bill Buzbee270c1d62009-08-13 16:58:07 -07001003 storeMultiple(cUnit, r7, regMask);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001004 }
1005}
1006
1007static void genProcessArgsRange(CompilationUnit *cUnit, MIR *mir,
1008 DecodedInstruction *dInsn,
Bill Buzbee89efc3d2009-07-28 11:22:22 -07001009 ArmLIR **pcrLabel)
Ben Chengba4fc8b2009-06-01 13:00:29 -07001010{
1011 int srcOffset = dInsn->vC << 2;
1012 int numArgs = dInsn->vA;
1013 int regMask;
Bill Buzbee1465db52009-09-23 17:17:35 -07001014
1015 /*
1016 * Note: here, all promoted registers will have been flushed
1017 * back to the Dalvik base locations, so register usage restrictins
1018 * are lifted. All parms loaded from original Dalvik register
1019 * region - even though some might conceivably have valid copies
1020 * cached in a preserved register.
1021 */
Bill Buzbeec6f10662010-02-09 11:16:15 -08001022 dvmCompilerLockAllTemps(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07001023
Ben Chengba4fc8b2009-06-01 13:00:29 -07001024 /*
Ben Cheng20d7e6c2011-02-18 17:12:42 -08001025 * r4PC : &r5FP[vC]
Ben Chengba4fc8b2009-06-01 13:00:29 -07001026 * r7: &newFP[0]
1027 */
Ben Cheng20d7e6c2011-02-18 17:12:42 -08001028 opRegRegImm(cUnit, kOpAdd, r4PC, r5FP, srcOffset);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001029 /* load [r0 .. min(numArgs,4)] */
1030 regMask = (1 << ((numArgs < 4) ? numArgs : 4)) - 1;
Ben Chengd7d426a2009-09-22 11:23:36 -07001031 /*
1032 * Protect the loadMultiple instruction from being reordered with other
1033 * Dalvik stack accesses.
jeffhao71eee1f2011-01-04 14:18:54 -08001034 *
1035 * This code is also shared by the invoke jumbo instructions, and this
1036 * does not need to be done if the invoke jumbo has no arguments.
Ben Chengd7d426a2009-09-22 11:23:36 -07001037 */
jeffhao71eee1f2011-01-04 14:18:54 -08001038 if (numArgs != 0) loadMultiple(cUnit, r4PC, regMask);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001039
Ben Cheng20d7e6c2011-02-18 17:12:42 -08001040 opRegRegImm(cUnit, kOpSub, r7, r5FP,
Bill Buzbee1465db52009-09-23 17:17:35 -07001041 sizeof(StackSaveArea) + (numArgs << 2));
Ben Chengba4fc8b2009-06-01 13:00:29 -07001042 /* generate null check */
1043 if (pcrLabel) {
Bill Buzbeec6f10662010-02-09 11:16:15 -08001044 *pcrLabel = genNullCheck(cUnit, dvmCompilerSSASrc(mir, 0), r0,
Bill Buzbee1465db52009-09-23 17:17:35 -07001045 mir->offset, NULL);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001046 }
1047
1048 /*
1049 * Handle remaining 4n arguments:
1050 * store previously loaded 4 values and load the next 4 values
1051 */
1052 if (numArgs >= 8) {
Bill Buzbee89efc3d2009-07-28 11:22:22 -07001053 ArmLIR *loopLabel = NULL;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001054 /*
1055 * r0 contains "this" and it will be used later, so push it to the stack
Ben Cheng20d7e6c2011-02-18 17:12:42 -08001056 * first. Pushing r5FP is just for stack alignment purposes.
Ben Chengba4fc8b2009-06-01 13:00:29 -07001057 */
Ben Cheng20d7e6c2011-02-18 17:12:42 -08001058 opImm(cUnit, kOpPush, (1 << r0 | 1 << r5FP));
Ben Chengba4fc8b2009-06-01 13:00:29 -07001059 /* No need to generate the loop structure if numArgs <= 11 */
1060 if (numArgs > 11) {
1061 loadConstant(cUnit, 5, ((numArgs - 4) >> 2) << 2);
Bill Buzbee1465db52009-09-23 17:17:35 -07001062 loopLabel = newLIR0(cUnit, kArmPseudoTargetLabel);
Ben Chengd7d426a2009-09-22 11:23:36 -07001063 loopLabel->defMask = ENCODE_ALL;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001064 }
Bill Buzbee270c1d62009-08-13 16:58:07 -07001065 storeMultiple(cUnit, r7, regMask);
Ben Chengd7d426a2009-09-22 11:23:36 -07001066 /*
1067 * Protect the loadMultiple instruction from being reordered with other
1068 * Dalvik stack accesses.
1069 */
Bill Buzbee270c1d62009-08-13 16:58:07 -07001070 loadMultiple(cUnit, r4PC, regMask);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001071 /* No need to generate the loop structure if numArgs <= 11 */
1072 if (numArgs > 11) {
Ben Cheng20d7e6c2011-02-18 17:12:42 -08001073 opRegImm(cUnit, kOpSub, r5FP, 4);
Bill Buzbee1465db52009-09-23 17:17:35 -07001074 genConditionalBranch(cUnit, kArmCondNe, loopLabel);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001075 }
1076 }
1077
1078 /* Save the last batch of loaded values */
jeffhao71eee1f2011-01-04 14:18:54 -08001079 if (numArgs != 0) storeMultiple(cUnit, r7, regMask);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001080
1081 /* Generate the loop epilogue - don't use r0 */
1082 if ((numArgs > 4) && (numArgs % 4)) {
1083 regMask = ((1 << (numArgs & 0x3)) - 1) << 1;
Ben Chengd7d426a2009-09-22 11:23:36 -07001084 /*
1085 * Protect the loadMultiple instruction from being reordered with other
1086 * Dalvik stack accesses.
1087 */
Bill Buzbee270c1d62009-08-13 16:58:07 -07001088 loadMultiple(cUnit, r4PC, regMask);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001089 }
1090 if (numArgs >= 8)
Ben Cheng20d7e6c2011-02-18 17:12:42 -08001091 opImm(cUnit, kOpPop, (1 << r0 | 1 << r5FP));
Ben Chengba4fc8b2009-06-01 13:00:29 -07001092
1093 /* Save the modulo 4 arguments */
1094 if ((numArgs > 4) && (numArgs % 4)) {
Bill Buzbee270c1d62009-08-13 16:58:07 -07001095 storeMultiple(cUnit, r7, regMask);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001096 }
1097}
1098
Ben Cheng38329f52009-07-07 14:19:20 -07001099/*
1100 * Generate code to setup the call stack then jump to the chaining cell if it
1101 * is not a native method.
1102 */
1103static void genInvokeSingletonCommon(CompilationUnit *cUnit, MIR *mir,
Bill Buzbee89efc3d2009-07-28 11:22:22 -07001104 BasicBlock *bb, ArmLIR *labelList,
1105 ArmLIR *pcrLabel,
Ben Cheng38329f52009-07-07 14:19:20 -07001106 const Method *calleeMethod)
Ben Chengba4fc8b2009-06-01 13:00:29 -07001107{
Bill Buzbee1465db52009-09-23 17:17:35 -07001108 /*
1109 * Note: all Dalvik register state should be flushed to
1110 * memory by the point, so register usage restrictions no
1111 * longer apply. All temp & preserved registers may be used.
1112 */
Bill Buzbeec6f10662010-02-09 11:16:15 -08001113 dvmCompilerLockAllTemps(cUnit);
Bill Buzbee89efc3d2009-07-28 11:22:22 -07001114 ArmLIR *retChainingCell = &labelList[bb->fallThrough->id];
Ben Chengba4fc8b2009-06-01 13:00:29 -07001115
1116 /* r1 = &retChainingCell */
Ben Cheng20d7e6c2011-02-18 17:12:42 -08001117 ArmLIR *addrRetChain = opRegRegImm(cUnit, kOpAdd, r1, r15pc, 0);
Ben Chengc8293e72010-10-12 11:50:10 -07001118
Ben Chengba4fc8b2009-06-01 13:00:29 -07001119 /* r4PC = dalvikCallsite */
1120 loadConstant(cUnit, r4PC,
1121 (int) (cUnit->method->insns + mir->offset));
1122 addrRetChain->generic.target = (LIR *) retChainingCell;
Ben Chengc8293e72010-10-12 11:50:10 -07001123
1124 /* r7 = calleeMethod->registersSize */
1125 loadConstant(cUnit, r7, calleeMethod->registersSize);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001126 /*
Ben Cheng38329f52009-07-07 14:19:20 -07001127 * r0 = calleeMethod (loaded upon calling genInvokeSingletonCommon)
Ben Chengba4fc8b2009-06-01 13:00:29 -07001128 * r1 = &ChainingCell
Ben Chengc8293e72010-10-12 11:50:10 -07001129 * r2 = calleeMethod->outsSize (to be loaded later for Java callees)
Ben Chengba4fc8b2009-06-01 13:00:29 -07001130 * r4PC = callsiteDPC
Ben Chengc8293e72010-10-12 11:50:10 -07001131 * r7 = calleeMethod->registersSize
Ben Chengba4fc8b2009-06-01 13:00:29 -07001132 */
1133 if (dvmIsNativeMethod(calleeMethod)) {
buzbee18fba342011-01-19 15:31:15 -08001134 genDispatchToHandler(cUnit, gDvmJit.methodTraceSupport ?
1135 TEMPLATE_INVOKE_METHOD_NATIVE_PROF :
1136 TEMPLATE_INVOKE_METHOD_NATIVE);
Ben Cheng978738d2010-05-13 13:45:57 -07001137#if defined(WITH_JIT_TUNING)
Ben Cheng38329f52009-07-07 14:19:20 -07001138 gDvmJit.invokeNative++;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001139#endif
1140 } else {
Ben Chengc8293e72010-10-12 11:50:10 -07001141 /* For Java callees, set up r2 to be calleeMethod->outsSize */
1142 loadConstant(cUnit, r2, calleeMethod->outsSize);
buzbee18fba342011-01-19 15:31:15 -08001143 genDispatchToHandler(cUnit, gDvmJit.methodTraceSupport ?
1144 TEMPLATE_INVOKE_METHOD_CHAIN_PROF :
1145 TEMPLATE_INVOKE_METHOD_CHAIN);
Ben Cheng978738d2010-05-13 13:45:57 -07001146#if defined(WITH_JIT_TUNING)
Ben Cheng86717f72010-03-05 15:27:21 -08001147 gDvmJit.invokeMonomorphic++;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001148#endif
Ben Cheng38329f52009-07-07 14:19:20 -07001149 /* Branch to the chaining cell */
Ben Chengba4fc8b2009-06-01 13:00:29 -07001150 genUnconditionalBranch(cUnit, &labelList[bb->taken->id]);
1151 }
1152 /* Handle exceptions using the interpreter */
1153 genTrap(cUnit, mir->offset, pcrLabel);
1154}
1155
Ben Cheng38329f52009-07-07 14:19:20 -07001156/*
1157 * Generate code to check the validity of a predicted chain and take actions
1158 * based on the result.
1159 *
1160 * 0x426a99aa : ldr r4, [pc, #72] --> r4 <- dalvikPC of this invoke
1161 * 0x426a99ac : add r1, pc, #32 --> r1 <- &retChainingCell
1162 * 0x426a99ae : add r2, pc, #40 --> r2 <- &predictedChainingCell
1163 * 0x426a99b0 : blx_1 0x426a918c --+ TEMPLATE_INVOKE_METHOD_PREDICTED_CHAIN
1164 * 0x426a99b2 : blx_2 see above --+
1165 * 0x426a99b4 : b 0x426a99d8 --> off to the predicted chain
1166 * 0x426a99b6 : b 0x426a99c8 --> punt to the interpreter
1167 * 0x426a99b8 : ldr r0, [r7, #44] --> r0 <- this->class->vtable[methodIdx]
1168 * 0x426a99ba : cmp r1, #0 --> compare r1 (rechain count) against 0
1169 * 0x426a99bc : bgt 0x426a99c2 --> >=0? don't rechain
Ben Chengaf5aa1f2011-01-04 15:37:04 -08001170 * 0x426a99be : ldr r7, [pc, #off]--+ dvmJitToPatchPredictedChain
Ben Cheng38329f52009-07-07 14:19:20 -07001171 * 0x426a99c0 : blx r7 --+
1172 * 0x426a99c2 : add r1, pc, #12 --> r1 <- &retChainingCell
1173 * 0x426a99c4 : blx_1 0x426a9098 --+ TEMPLATE_INVOKE_METHOD_NO_OPT
1174 * 0x426a99c6 : blx_2 see above --+
1175 */
1176static void genInvokeVirtualCommon(CompilationUnit *cUnit, MIR *mir,
1177 int methodIndex,
Bill Buzbee89efc3d2009-07-28 11:22:22 -07001178 ArmLIR *retChainingCell,
1179 ArmLIR *predChainingCell,
1180 ArmLIR *pcrLabel)
Ben Cheng38329f52009-07-07 14:19:20 -07001181{
Bill Buzbee1465db52009-09-23 17:17:35 -07001182 /*
1183 * Note: all Dalvik register state should be flushed to
1184 * memory by the point, so register usage restrictions no
1185 * longer apply. Lock temps to prevent them from being
1186 * allocated by utility routines.
1187 */
Bill Buzbeec6f10662010-02-09 11:16:15 -08001188 dvmCompilerLockAllTemps(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07001189
Ben Cheng04517042011-03-14 11:16:21 -07001190 /*
1191 * For verbose printing, store the method pointer in operands[1] first as
1192 * operands[0] will be clobbered in dvmCompilerMIR2LIR.
1193 */
1194 predChainingCell->operands[1] = (int) mir->meta.callsiteInfo->method;
1195
Ben Cheng38329f52009-07-07 14:19:20 -07001196 /* "this" is already left in r0 by genProcessArgs* */
1197
1198 /* r4PC = dalvikCallsite */
1199 loadConstant(cUnit, r4PC,
1200 (int) (cUnit->method->insns + mir->offset));
1201
1202 /* r1 = &retChainingCell */
Ben Cheng20d7e6c2011-02-18 17:12:42 -08001203 ArmLIR *addrRetChain = opRegRegImm(cUnit, kOpAdd, r1, r15pc, 0);
Ben Cheng38329f52009-07-07 14:19:20 -07001204 addrRetChain->generic.target = (LIR *) retChainingCell;
1205
1206 /* r2 = &predictedChainingCell */
Ben Cheng20d7e6c2011-02-18 17:12:42 -08001207 ArmLIR *predictedChainingCell = opRegRegImm(cUnit, kOpAdd, r2, r15pc, 0);
Ben Cheng38329f52009-07-07 14:19:20 -07001208 predictedChainingCell->generic.target = (LIR *) predChainingCell;
1209
buzbee18fba342011-01-19 15:31:15 -08001210 genDispatchToHandler(cUnit, gDvmJit.methodTraceSupport ?
1211 TEMPLATE_INVOKE_METHOD_PREDICTED_CHAIN_PROF :
1212 TEMPLATE_INVOKE_METHOD_PREDICTED_CHAIN);
Ben Cheng38329f52009-07-07 14:19:20 -07001213
1214 /* return through lr - jump to the chaining cell */
1215 genUnconditionalBranch(cUnit, predChainingCell);
1216
1217 /*
1218 * null-check on "this" may have been eliminated, but we still need a PC-
1219 * reconstruction label for stack overflow bailout.
1220 */
1221 if (pcrLabel == NULL) {
1222 int dPC = (int) (cUnit->method->insns + mir->offset);
Carl Shapirofc75f3e2010-12-07 11:43:38 -08001223 pcrLabel = (ArmLIR *) dvmCompilerNew(sizeof(ArmLIR), true);
Dan Bornstein9a1f8162010-12-01 17:02:26 -08001224 pcrLabel->opcode = kArmPseudoPCReconstructionCell;
Ben Cheng38329f52009-07-07 14:19:20 -07001225 pcrLabel->operands[0] = dPC;
1226 pcrLabel->operands[1] = mir->offset;
1227 /* Insert the place holder to the growable list */
Ben Cheng00603072010-10-28 11:13:58 -07001228 dvmInsertGrowableList(&cUnit->pcReconstructionList,
1229 (intptr_t) pcrLabel);
Ben Cheng38329f52009-07-07 14:19:20 -07001230 }
1231
1232 /* return through lr+2 - punt to the interpreter */
1233 genUnconditionalBranch(cUnit, pcrLabel);
1234
1235 /*
1236 * return through lr+4 - fully resolve the callee method.
1237 * r1 <- count
1238 * r2 <- &predictedChainCell
1239 * r3 <- this->class
1240 * r4 <- dPC
1241 * r7 <- this->class->vtable
1242 */
1243
1244 /* r0 <- calleeMethod */
Bill Buzbee270c1d62009-08-13 16:58:07 -07001245 loadWordDisp(cUnit, r7, methodIndex * 4, r0);
Ben Cheng38329f52009-07-07 14:19:20 -07001246
1247 /* Check if rechain limit is reached */
buzbee8f8109a2010-08-31 10:16:35 -07001248 ArmLIR *bypassRechaining = genCmpImmBranch(cUnit, kArmCondGt, r1, 0);
Ben Cheng38329f52009-07-07 14:19:20 -07001249
Ben Chengaf5aa1f2011-01-04 15:37:04 -08001250 LOAD_FUNC_ADDR(cUnit, r7, (int) dvmJitToPatchPredictedChain);
Ben Cheng38329f52009-07-07 14:19:20 -07001251
Ben Cheng20d7e6c2011-02-18 17:12:42 -08001252 genRegCopy(cUnit, r1, r6SELF);
Ben Chengb88ec3c2010-05-17 12:50:33 -07001253
Ben Cheng38329f52009-07-07 14:19:20 -07001254 /*
1255 * r0 = calleeMethod
1256 * r2 = &predictedChainingCell
1257 * r3 = class
1258 *
1259 * &returnChainingCell has been loaded into r1 but is not needed
1260 * when patching the chaining cell and will be clobbered upon
1261 * returning so it will be reconstructed again.
1262 */
Bill Buzbee1465db52009-09-23 17:17:35 -07001263 opReg(cUnit, kOpBlx, r7);
Ben Cheng38329f52009-07-07 14:19:20 -07001264
1265 /* r1 = &retChainingCell */
Ben Cheng20d7e6c2011-02-18 17:12:42 -08001266 addrRetChain = opRegRegImm(cUnit, kOpAdd, r1, r15pc, 0);
Ben Cheng38329f52009-07-07 14:19:20 -07001267 addrRetChain->generic.target = (LIR *) retChainingCell;
1268
1269 bypassRechaining->generic.target = (LIR *) addrRetChain;
1270 /*
1271 * r0 = calleeMethod,
1272 * r1 = &ChainingCell,
1273 * r4PC = callsiteDPC,
1274 */
buzbee18fba342011-01-19 15:31:15 -08001275 genDispatchToHandler(cUnit, gDvmJit.methodTraceSupport ?
1276 TEMPLATE_INVOKE_METHOD_NO_OPT_PROF :
1277 TEMPLATE_INVOKE_METHOD_NO_OPT);
Ben Cheng978738d2010-05-13 13:45:57 -07001278#if defined(WITH_JIT_TUNING)
Ben Cheng86717f72010-03-05 15:27:21 -08001279 gDvmJit.invokePolymorphic++;
Ben Cheng38329f52009-07-07 14:19:20 -07001280#endif
1281 /* Handle exceptions using the interpreter */
1282 genTrap(cUnit, mir->offset, pcrLabel);
1283}
1284
Ben Cheng20d7e6c2011-02-18 17:12:42 -08001285/* "this" pointer is already in r0 */
1286static void genInvokeVirtualWholeMethod(CompilationUnit *cUnit,
1287 MIR *mir,
1288 void *calleeAddr,
1289 ArmLIR *retChainingCell)
1290{
1291 CallsiteInfo *callsiteInfo = mir->meta.callsiteInfo;
1292 dvmCompilerLockAllTemps(cUnit);
1293
Ben Cheng385828e2011-03-04 16:48:33 -08001294 loadClassPointer(cUnit, r1, (int) callsiteInfo);
Ben Cheng20d7e6c2011-02-18 17:12:42 -08001295
1296 loadWordDisp(cUnit, r0, offsetof(Object, clazz), r2);
1297 /* Branch to the slow path if classes are not equal */
1298 opRegReg(cUnit, kOpCmp, r1, r2);
1299 /*
1300 * Set the misPredBranchOver target so that it will be generated when the
1301 * code for the non-optimized invoke is generated.
1302 */
1303 ArmLIR *classCheck = opCondBranch(cUnit, kArmCondNe);
1304
1305 /* r0 = the Dalvik PC of the callsite */
1306 loadConstant(cUnit, r0, (int) (cUnit->method->insns + mir->offset));
1307
1308 newLIR2(cUnit, kThumbBl1, (int) calleeAddr, (int) calleeAddr);
1309 newLIR2(cUnit, kThumbBl2, (int) calleeAddr, (int) calleeAddr);
1310 genUnconditionalBranch(cUnit, retChainingCell);
1311
1312 /* Target of slow path */
1313 ArmLIR *slowPathLabel = newLIR0(cUnit, kArmPseudoTargetLabel);
1314
1315 slowPathLabel->defMask = ENCODE_ALL;
1316 classCheck->generic.target = (LIR *) slowPathLabel;
1317
1318 // FIXME
1319 cUnit->printMe = true;
1320}
1321
1322static void genInvokeSingletonWholeMethod(CompilationUnit *cUnit,
1323 MIR *mir,
1324 void *calleeAddr,
1325 ArmLIR *retChainingCell)
1326{
1327 /* r0 = the Dalvik PC of the callsite */
1328 loadConstant(cUnit, r0, (int) (cUnit->method->insns + mir->offset));
1329
1330 newLIR2(cUnit, kThumbBl1, (int) calleeAddr, (int) calleeAddr);
1331 newLIR2(cUnit, kThumbBl2, (int) calleeAddr, (int) calleeAddr);
1332 genUnconditionalBranch(cUnit, retChainingCell);
1333
1334 // FIXME
1335 cUnit->printMe = true;
1336}
1337
Ben Chengba4fc8b2009-06-01 13:00:29 -07001338/* Geneate a branch to go back to the interpreter */
1339static void genPuntToInterp(CompilationUnit *cUnit, unsigned int offset)
1340{
1341 /* r0 = dalvik pc */
Bill Buzbeec6f10662010-02-09 11:16:15 -08001342 dvmCompilerFlushAllRegs(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001343 loadConstant(cUnit, r0, (int) (cUnit->method->insns + offset));
Ben Cheng20d7e6c2011-02-18 17:12:42 -08001344 loadWordDisp(cUnit, r6SELF, offsetof(Thread,
Bill Buzbee270c1d62009-08-13 16:58:07 -07001345 jitToInterpEntries.dvmJitToInterpPunt), r1);
Bill Buzbee1465db52009-09-23 17:17:35 -07001346 opReg(cUnit, kOpBlx, r1);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001347}
1348
1349/*
1350 * Attempt to single step one instruction using the interpreter and return
1351 * to the compiled code for the next Dalvik instruction
1352 */
1353static void genInterpSingleStep(CompilationUnit *cUnit, MIR *mir)
1354{
Dan Bornsteine4852762010-12-02 12:45:00 -08001355 int flags = dexGetFlagsFromOpcode(mir->dalvikInsn.opcode);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001356 int flagsToCheck = kInstrCanBranch | kInstrCanSwitch | kInstrCanReturn |
1357 kInstrCanThrow;
Bill Buzbee1465db52009-09-23 17:17:35 -07001358
Ben Cheng32115a92011-03-22 14:09:09 -07001359 // Single stepping is considered loop mode breaker
1360 if (cUnit->jitMode == kJitLoop) {
1361 cUnit->quitLoopMode = true;
1362 return;
1363 }
1364
Bill Buzbee45273872010-03-11 11:12:15 -08001365 //If already optimized out, just ignore
Dan Bornstein9a1f8162010-12-01 17:02:26 -08001366 if (mir->dalvikInsn.opcode == OP_NOP)
Bill Buzbee45273872010-03-11 11:12:15 -08001367 return;
1368
Bill Buzbee1465db52009-09-23 17:17:35 -07001369 //Ugly, but necessary. Flush all Dalvik regs so Interp can find them
Bill Buzbeec6f10662010-02-09 11:16:15 -08001370 dvmCompilerFlushAllRegs(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07001371
Ben Chengba4fc8b2009-06-01 13:00:29 -07001372 if ((mir->next == NULL) || (flags & flagsToCheck)) {
1373 genPuntToInterp(cUnit, mir->offset);
1374 return;
1375 }
buzbee9f601a92011-02-11 17:48:20 -08001376 int entryAddr = offsetof(Thread,
Ben Chengba4fc8b2009-06-01 13:00:29 -07001377 jitToInterpEntries.dvmJitToInterpSingleStep);
Ben Cheng20d7e6c2011-02-18 17:12:42 -08001378 loadWordDisp(cUnit, r6SELF, entryAddr, r2);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001379 /* r0 = dalvik pc */
1380 loadConstant(cUnit, r0, (int) (cUnit->method->insns + mir->offset));
1381 /* r1 = dalvik pc of following instruction */
1382 loadConstant(cUnit, r1, (int) (cUnit->method->insns + mir->next->offset));
Bill Buzbee1465db52009-09-23 17:17:35 -07001383 opReg(cUnit, kOpBlx, r2);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001384}
1385
Carl Shapiro01605d22011-02-01 11:32:44 -08001386#if defined(_ARMV5TE) || defined(_ARMV5TE_VFP)
Bill Buzbeec1d9ed42010-02-02 11:04:33 -08001387/*
1388 * To prevent a thread in a monitor wait from blocking the Jit from
1389 * resetting the code cache, heavyweight monitor lock will not
1390 * be allowed to return to an existing translation. Instead, we will
1391 * handle them by branching to a handler, which will in turn call the
1392 * runtime lock routine and then branch directly back to the
1393 * interpreter main loop. Given the high cost of the heavyweight
1394 * lock operation, this additional cost should be slight (especially when
1395 * considering that we expect the vast majority of lock operations to
1396 * use the fast-path thin lock bypass).
1397 */
Ben Cheng5d90c202009-11-22 23:31:11 -08001398static void genMonitorPortable(CompilationUnit *cUnit, MIR *mir)
Bill Buzbee270c1d62009-08-13 16:58:07 -07001399{
Dan Bornstein9a1f8162010-12-01 17:02:26 -08001400 bool isEnter = (mir->dalvikInsn.opcode == OP_MONITOR_ENTER);
Bill Buzbee1465db52009-09-23 17:17:35 -07001401 genExportPC(cUnit, mir);
Bill Buzbeec6f10662010-02-09 11:16:15 -08001402 dvmCompilerFlushAllRegs(cUnit); /* Send everything to home location */
1403 RegLocation rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07001404 loadValueDirectFixed(cUnit, rlSrc, r1);
Ben Cheng20d7e6c2011-02-18 17:12:42 -08001405 genRegCopy(cUnit, r0, r6SELF);
Bill Buzbeec1d9ed42010-02-02 11:04:33 -08001406 genNullCheck(cUnit, rlSrc.sRegLow, r1, mir->offset, NULL);
Bill Buzbeeefbd3c52009-11-04 22:18:40 -08001407 if (isEnter) {
Bill Buzbeec1d9ed42010-02-02 11:04:33 -08001408 /* Get dPC of next insn */
1409 loadConstant(cUnit, r4PC, (int)(cUnit->method->insns + mir->offset +
Dan Bornsteine4852762010-12-02 12:45:00 -08001410 dexGetWidthFromOpcode(OP_MONITOR_ENTER)));
Bill Buzbeec1d9ed42010-02-02 11:04:33 -08001411 genDispatchToHandler(cUnit, TEMPLATE_MONITOR_ENTER);
Bill Buzbee1465db52009-09-23 17:17:35 -07001412 } else {
Ben Chengbd1326d2010-04-02 15:04:53 -07001413 LOAD_FUNC_ADDR(cUnit, r2, (int)dvmUnlockObject);
Bill Buzbeec1d9ed42010-02-02 11:04:33 -08001414 /* Do the call */
1415 opReg(cUnit, kOpBlx, r2);
buzbee8f8109a2010-08-31 10:16:35 -07001416 /* Did we throw? */
1417 ArmLIR *branchOver = genCmpImmBranch(cUnit, kArmCondNe, r0, 0);
Bill Buzbee6bbdd6b2010-02-16 14:40:01 -08001418 loadConstant(cUnit, r0,
1419 (int) (cUnit->method->insns + mir->offset +
Dan Bornsteine4852762010-12-02 12:45:00 -08001420 dexGetWidthFromOpcode(OP_MONITOR_EXIT)));
Bill Buzbee6bbdd6b2010-02-16 14:40:01 -08001421 genDispatchToHandler(cUnit, TEMPLATE_THROW_EXCEPTION_COMMON);
1422 ArmLIR *target = newLIR0(cUnit, kArmPseudoTargetLabel);
1423 target->defMask = ENCODE_ALL;
1424 branchOver->generic.target = (LIR *) target;
Elliott Hughes6a555132010-02-25 15:41:42 -08001425 dvmCompilerClobberCallRegs(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07001426 }
Bill Buzbee270c1d62009-08-13 16:58:07 -07001427}
Ben Chengfc075c22010-05-28 15:20:08 -07001428#endif
Bill Buzbee270c1d62009-08-13 16:58:07 -07001429
Ben Chengba4fc8b2009-06-01 13:00:29 -07001430/*
buzbee9a3147c2011-03-02 15:43:48 -08001431 * Fetch *self->info.breakFlags. If the breakFlags are non-zero,
Ben Cheng7ab74e12011-02-03 14:02:06 -08001432 * punt to the interpreter.
1433 */
1434static void genSuspendPoll(CompilationUnit *cUnit, MIR *mir)
1435{
1436 int rTemp = dvmCompilerAllocTemp(cUnit);
1437 ArmLIR *ld;
buzbee9a3147c2011-03-02 15:43:48 -08001438 ld = loadBaseDisp(cUnit, NULL, r6SELF,
1439 offsetof(Thread, interpBreak.ctl.breakFlags),
1440 rTemp, kUnsignedByte, INVALID_SREG);
Ben Cheng7ab74e12011-02-03 14:02:06 -08001441 setMemRefType(ld, true /* isLoad */, kMustNotAlias);
Ben Cheng7ab74e12011-02-03 14:02:06 -08001442 genRegImmCheck(cUnit, kArmCondNe, rTemp, 0, mir->offset, NULL);
1443}
1444
1445/*
Ben Chengba4fc8b2009-06-01 13:00:29 -07001446 * The following are the first-level codegen routines that analyze the format
1447 * of each bytecode then either dispatch special purpose codegen routines
1448 * or produce corresponding Thumb instructions directly.
1449 */
1450
1451static bool handleFmt10t_Fmt20t_Fmt30t(CompilationUnit *cUnit, MIR *mir,
Bill Buzbee89efc3d2009-07-28 11:22:22 -07001452 BasicBlock *bb, ArmLIR *labelList)
Ben Chengba4fc8b2009-06-01 13:00:29 -07001453{
Ben Cheng7ab74e12011-02-03 14:02:06 -08001454 /* backward branch? */
1455 bool backwardBranch = (bb->taken->startOffset <= mir->offset);
1456
Ben Cheng32115a92011-03-22 14:09:09 -07001457 if (backwardBranch &&
1458 (gDvmJit.genSuspendPoll || cUnit->jitMode == kJitLoop)) {
Ben Cheng7ab74e12011-02-03 14:02:06 -08001459 genSuspendPoll(cUnit, mir);
1460 }
1461
1462 int numPredecessors = dvmCountSetBits(bb->taken->predecessors);
1463 /*
1464 * Things could be hoisted out of the taken block into the predecessor, so
1465 * make sure it is dominated by the predecessor.
1466 */
1467 if (numPredecessors == 1 && bb->taken->visited == false &&
Ben Cheng20d7e6c2011-02-18 17:12:42 -08001468 bb->taken->blockType == kDalvikByteCode) {
Ben Cheng7ab74e12011-02-03 14:02:06 -08001469 cUnit->nextCodegenBlock = bb->taken;
1470 } else {
1471 /* For OP_GOTO, OP_GOTO_16, and OP_GOTO_32 */
1472 genUnconditionalBranch(cUnit, &labelList[bb->taken->id]);
1473 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07001474 return false;
1475}
1476
1477static bool handleFmt10x(CompilationUnit *cUnit, MIR *mir)
1478{
Dan Bornstein9a1f8162010-12-01 17:02:26 -08001479 Opcode dalvikOpcode = mir->dalvikInsn.opcode;
1480 if ((dalvikOpcode >= OP_UNUSED_3E) && (dalvikOpcode <= OP_UNUSED_43)) {
1481 LOGE("Codegen: got unused opcode 0x%x\n",dalvikOpcode);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001482 return true;
1483 }
Dan Bornstein9a1f8162010-12-01 17:02:26 -08001484 switch (dalvikOpcode) {
Andy McFadden291758c2010-09-10 08:04:52 -07001485 case OP_RETURN_VOID_BARRIER:
buzbee2ce33c92010-11-01 15:53:27 -07001486 dvmCompilerGenMemBarrier(cUnit, kST);
1487 // Intentional fallthrough
1488 case OP_RETURN_VOID:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001489 genReturnCommon(cUnit,mir);
1490 break;
1491 case OP_UNUSED_73:
1492 case OP_UNUSED_79:
1493 case OP_UNUSED_7A:
Dan Bornstein90f15432010-12-02 16:46:25 -08001494 case OP_DISPATCH_FF:
Dan Bornstein9a1f8162010-12-01 17:02:26 -08001495 LOGE("Codegen: got unused opcode 0x%x\n",dalvikOpcode);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001496 return true;
1497 case OP_NOP:
1498 break;
1499 default:
1500 return true;
1501 }
1502 return false;
1503}
1504
1505static bool handleFmt11n_Fmt31i(CompilationUnit *cUnit, MIR *mir)
1506{
Bill Buzbee1465db52009-09-23 17:17:35 -07001507 RegLocation rlDest;
1508 RegLocation rlResult;
1509 if (mir->ssaRep->numDefs == 2) {
Bill Buzbeec6f10662010-02-09 11:16:15 -08001510 rlDest = dvmCompilerGetDestWide(cUnit, mir, 0, 1);
Bill Buzbee1465db52009-09-23 17:17:35 -07001511 } else {
Bill Buzbeec6f10662010-02-09 11:16:15 -08001512 rlDest = dvmCompilerGetDest(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07001513 }
Ben Chenge9695e52009-06-16 16:11:47 -07001514
Dan Bornstein9a1f8162010-12-01 17:02:26 -08001515 switch (mir->dalvikInsn.opcode) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07001516 case OP_CONST:
Ben Chenge9695e52009-06-16 16:11:47 -07001517 case OP_CONST_4: {
Bill Buzbeec6f10662010-02-09 11:16:15 -08001518 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kAnyReg, true);
Ben Chengbd1326d2010-04-02 15:04:53 -07001519 loadConstantNoClobber(cUnit, rlResult.lowReg, mir->dalvikInsn.vB);
Bill Buzbee1465db52009-09-23 17:17:35 -07001520 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001521 break;
Ben Chenge9695e52009-06-16 16:11:47 -07001522 }
1523 case OP_CONST_WIDE_32: {
Bill Buzbee1465db52009-09-23 17:17:35 -07001524 //TUNING: single routine to load constant pair for support doubles
Bill Buzbee964a7b02010-01-28 12:54:19 -08001525 //TUNING: load 0/-1 separately to avoid load dependency
Bill Buzbeec6f10662010-02-09 11:16:15 -08001526 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Ben Chengbd1326d2010-04-02 15:04:53 -07001527 loadConstantNoClobber(cUnit, rlResult.lowReg, mir->dalvikInsn.vB);
Bill Buzbee1465db52009-09-23 17:17:35 -07001528 opRegRegImm(cUnit, kOpAsr, rlResult.highReg,
1529 rlResult.lowReg, 31);
1530 storeValueWide(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001531 break;
Ben Chenge9695e52009-06-16 16:11:47 -07001532 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07001533 default:
1534 return true;
1535 }
1536 return false;
1537}
1538
1539static bool handleFmt21h(CompilationUnit *cUnit, MIR *mir)
1540{
Bill Buzbee1465db52009-09-23 17:17:35 -07001541 RegLocation rlDest;
1542 RegLocation rlResult;
1543 if (mir->ssaRep->numDefs == 2) {
Bill Buzbeec6f10662010-02-09 11:16:15 -08001544 rlDest = dvmCompilerGetDestWide(cUnit, mir, 0, 1);
Bill Buzbee1465db52009-09-23 17:17:35 -07001545 } else {
Bill Buzbeec6f10662010-02-09 11:16:15 -08001546 rlDest = dvmCompilerGetDest(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07001547 }
Bill Buzbeec6f10662010-02-09 11:16:15 -08001548 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kAnyReg, true);
Ben Chenge9695e52009-06-16 16:11:47 -07001549
Dan Bornstein9a1f8162010-12-01 17:02:26 -08001550 switch (mir->dalvikInsn.opcode) {
Ben Chenge9695e52009-06-16 16:11:47 -07001551 case OP_CONST_HIGH16: {
Ben Chengbd1326d2010-04-02 15:04:53 -07001552 loadConstantNoClobber(cUnit, rlResult.lowReg,
1553 mir->dalvikInsn.vB << 16);
Bill Buzbee1465db52009-09-23 17:17:35 -07001554 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001555 break;
Ben Chenge9695e52009-06-16 16:11:47 -07001556 }
1557 case OP_CONST_WIDE_HIGH16: {
Bill Buzbee1465db52009-09-23 17:17:35 -07001558 loadConstantValueWide(cUnit, rlResult.lowReg, rlResult.highReg,
1559 0, mir->dalvikInsn.vB << 16);
1560 storeValueWide(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001561 break;
Ben Chenge9695e52009-06-16 16:11:47 -07001562 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07001563 default:
1564 return true;
1565 }
1566 return false;
1567}
1568
jeffhao71eee1f2011-01-04 14:18:54 -08001569static bool handleFmt20bc_Fmt40sc(CompilationUnit *cUnit, MIR *mir)
Ben Chengba4fc8b2009-06-01 13:00:29 -07001570{
jeffhao71eee1f2011-01-04 14:18:54 -08001571 /* For OP_THROW_VERIFICATION_ERROR & OP_THROW_VERIFICATION_ERROR_JUMBO */
Ben Chengba4fc8b2009-06-01 13:00:29 -07001572 genInterpSingleStep(cUnit, mir);
1573 return false;
1574}
1575
jeffhao71eee1f2011-01-04 14:18:54 -08001576static bool handleFmt21c_Fmt31c_Fmt41c(CompilationUnit *cUnit, MIR *mir)
Ben Chengba4fc8b2009-06-01 13:00:29 -07001577{
Bill Buzbee1465db52009-09-23 17:17:35 -07001578 RegLocation rlResult;
1579 RegLocation rlDest;
1580 RegLocation rlSrc;
Ben Chenge9695e52009-06-16 16:11:47 -07001581
Dan Bornstein9a1f8162010-12-01 17:02:26 -08001582 switch (mir->dalvikInsn.opcode) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07001583 case OP_CONST_STRING_JUMBO:
1584 case OP_CONST_STRING: {
1585 void *strPtr = (void*)
1586 (cUnit->method->clazz->pDvmDex->pResStrings[mir->dalvikInsn.vB]);
Ben Chengdd6e8702010-05-07 13:05:47 -07001587
1588 if (strPtr == NULL) {
Ben Cheng32115a92011-03-22 14:09:09 -07001589 BAIL_LOOP_COMPILATION();
Ben Chengdd6e8702010-05-07 13:05:47 -07001590 LOGE("Unexpected null string");
1591 dvmAbort();
1592 }
1593
Bill Buzbeec6f10662010-02-09 11:16:15 -08001594 rlDest = dvmCompilerGetDest(cUnit, mir, 0);
1595 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Ben Chengbd1326d2010-04-02 15:04:53 -07001596 loadConstantNoClobber(cUnit, rlResult.lowReg, (int) strPtr );
Bill Buzbee1465db52009-09-23 17:17:35 -07001597 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001598 break;
1599 }
jeffhao71eee1f2011-01-04 14:18:54 -08001600 case OP_CONST_CLASS:
1601 case OP_CONST_CLASS_JUMBO: {
Ben Chengba4fc8b2009-06-01 13:00:29 -07001602 void *classPtr = (void*)
1603 (cUnit->method->clazz->pDvmDex->pResClasses[mir->dalvikInsn.vB]);
Ben Chengdd6e8702010-05-07 13:05:47 -07001604
1605 if (classPtr == NULL) {
Ben Cheng32115a92011-03-22 14:09:09 -07001606 BAIL_LOOP_COMPILATION();
Ben Chengdd6e8702010-05-07 13:05:47 -07001607 LOGE("Unexpected null class");
1608 dvmAbort();
1609 }
1610
Bill Buzbeec6f10662010-02-09 11:16:15 -08001611 rlDest = dvmCompilerGetDest(cUnit, mir, 0);
1612 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Ben Chengbd1326d2010-04-02 15:04:53 -07001613 loadConstantNoClobber(cUnit, rlResult.lowReg, (int) classPtr );
Bill Buzbee1465db52009-09-23 17:17:35 -07001614 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001615 break;
1616 }
jeffhao71eee1f2011-01-04 14:18:54 -08001617 case OP_SGET:
buzbeeecf8f6e2010-07-20 14:53:42 -07001618 case OP_SGET_VOLATILE:
buzbeebd7865b2011-03-31 10:55:04 -07001619 case OP_SGET_VOLATILE_JUMBO:
jeffhao71eee1f2011-01-04 14:18:54 -08001620 case OP_SGET_JUMBO:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001621 case OP_SGET_OBJECT:
jeffhao71eee1f2011-01-04 14:18:54 -08001622 case OP_SGET_OBJECT_VOLATILE:
buzbeebd7865b2011-03-31 10:55:04 -07001623 case OP_SGET_OBJECT_VOLATILE_JUMBO:
jeffhao71eee1f2011-01-04 14:18:54 -08001624 case OP_SGET_OBJECT_JUMBO:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001625 case OP_SGET_BOOLEAN:
jeffhao71eee1f2011-01-04 14:18:54 -08001626 case OP_SGET_BOOLEAN_JUMBO:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001627 case OP_SGET_CHAR:
jeffhao71eee1f2011-01-04 14:18:54 -08001628 case OP_SGET_CHAR_JUMBO:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001629 case OP_SGET_BYTE:
jeffhao71eee1f2011-01-04 14:18:54 -08001630 case OP_SGET_BYTE_JUMBO:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001631 case OP_SGET_SHORT:
jeffhao71eee1f2011-01-04 14:18:54 -08001632 case OP_SGET_SHORT_JUMBO: {
Bill Buzbee50a6bf22009-07-08 13:08:04 -07001633 int valOffset = offsetof(StaticField, value);
Bill Buzbeec6f10662010-02-09 11:16:15 -08001634 int tReg = dvmCompilerAllocTemp(cUnit);
buzbeeecf8f6e2010-07-20 14:53:42 -07001635 bool isVolatile;
Ben Cheng7a2697d2010-06-07 13:44:23 -07001636 const Method *method = (mir->OptimizationFlags & MIR_CALLEE) ?
1637 mir->meta.calleeMethod : cUnit->method;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001638 void *fieldPtr = (void*)
Ben Cheng7a2697d2010-06-07 13:44:23 -07001639 (method->clazz->pDvmDex->pResFields[mir->dalvikInsn.vB]);
Ben Chengdd6e8702010-05-07 13:05:47 -07001640
1641 if (fieldPtr == NULL) {
Ben Cheng32115a92011-03-22 14:09:09 -07001642 BAIL_LOOP_COMPILATION();
Ben Chengdd6e8702010-05-07 13:05:47 -07001643 LOGE("Unexpected null static field");
1644 dvmAbort();
1645 }
1646
buzbeee3c0b542011-04-07 15:22:06 -07001647 /*
1648 * On SMP systems, Dalvik opcodes found to be referencing
1649 * volatile fields are rewritten to their _VOLATILE variant.
1650 * However, this does not happen on non-SMP systems. The JIT
1651 * still needs to know about volatility to avoid unsafe
1652 * optimizations so we determine volatility based on either
1653 * the opcode or the field access flags.
1654 */
1655#if ANDROID_SMP != 0
1656 Opcode opcode = mir->dalvikInsn.opcode;
buzbeebd7865b2011-03-31 10:55:04 -07001657 isVolatile = (opcode == OP_SGET_VOLATILE) ||
buzbee3a56e9d2011-03-31 11:18:28 -07001658 (opcode == OP_SGET_VOLATILE_JUMBO) ||
buzbeebd7865b2011-03-31 10:55:04 -07001659 (opcode == OP_SGET_OBJECT_VOLATILE) ||
buzbee8b94be12011-04-04 12:25:57 -07001660 (opcode == OP_SGET_OBJECT_VOLATILE_JUMBO);
buzbee8b94be12011-04-04 12:25:57 -07001661 assert(isVolatile == dvmIsVolatileField((Field *) fieldPtr));
buzbeee3c0b542011-04-07 15:22:06 -07001662#else
1663 isVolatile = dvmIsVolatileField((Field *) fieldPtr);
1664#endif
buzbeeecf8f6e2010-07-20 14:53:42 -07001665
Bill Buzbeec6f10662010-02-09 11:16:15 -08001666 rlDest = dvmCompilerGetDest(cUnit, mir, 0);
1667 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kAnyReg, true);
Bill Buzbee1465db52009-09-23 17:17:35 -07001668 loadConstant(cUnit, tReg, (int) fieldPtr + valOffset);
Ben Cheng11d8f142010-03-24 15:24:19 -07001669
buzbeeecf8f6e2010-07-20 14:53:42 -07001670 if (isVolatile) {
buzbee2ce33c92010-11-01 15:53:27 -07001671 dvmCompilerGenMemBarrier(cUnit, kSY);
buzbeeecf8f6e2010-07-20 14:53:42 -07001672 }
Ben Cheng11d8f142010-03-24 15:24:19 -07001673 HEAP_ACCESS_SHADOW(true);
Bill Buzbee1465db52009-09-23 17:17:35 -07001674 loadWordDisp(cUnit, tReg, 0, rlResult.lowReg);
Ben Cheng11d8f142010-03-24 15:24:19 -07001675 HEAP_ACCESS_SHADOW(false);
1676
Bill Buzbee1465db52009-09-23 17:17:35 -07001677 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001678 break;
1679 }
jeffhao71eee1f2011-01-04 14:18:54 -08001680 case OP_SGET_WIDE:
1681 case OP_SGET_WIDE_JUMBO: {
Bill Buzbee50a6bf22009-07-08 13:08:04 -07001682 int valOffset = offsetof(StaticField, value);
Ben Cheng7a2697d2010-06-07 13:44:23 -07001683 const Method *method = (mir->OptimizationFlags & MIR_CALLEE) ?
1684 mir->meta.calleeMethod : cUnit->method;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001685 void *fieldPtr = (void*)
Ben Cheng7a2697d2010-06-07 13:44:23 -07001686 (method->clazz->pDvmDex->pResFields[mir->dalvikInsn.vB]);
Ben Chengdd6e8702010-05-07 13:05:47 -07001687
1688 if (fieldPtr == NULL) {
Ben Cheng32115a92011-03-22 14:09:09 -07001689 BAIL_LOOP_COMPILATION();
Ben Chengdd6e8702010-05-07 13:05:47 -07001690 LOGE("Unexpected null static field");
1691 dvmAbort();
1692 }
1693
Bill Buzbeec6f10662010-02-09 11:16:15 -08001694 int tReg = dvmCompilerAllocTemp(cUnit);
Bill Buzbeec6f10662010-02-09 11:16:15 -08001695 rlDest = dvmCompilerGetDestWide(cUnit, mir, 0, 1);
1696 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kAnyReg, true);
Bill Buzbee1465db52009-09-23 17:17:35 -07001697 loadConstant(cUnit, tReg, (int) fieldPtr + valOffset);
Ben Cheng11d8f142010-03-24 15:24:19 -07001698
1699 HEAP_ACCESS_SHADOW(true);
Bill Buzbee1465db52009-09-23 17:17:35 -07001700 loadPair(cUnit, tReg, rlResult.lowReg, rlResult.highReg);
Ben Cheng11d8f142010-03-24 15:24:19 -07001701 HEAP_ACCESS_SHADOW(false);
1702
Bill Buzbee1465db52009-09-23 17:17:35 -07001703 storeValueWide(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001704 break;
1705 }
jeffhao71eee1f2011-01-04 14:18:54 -08001706 case OP_SPUT:
1707 case OP_SPUT_VOLATILE:
buzbeebd7865b2011-03-31 10:55:04 -07001708 case OP_SPUT_VOLATILE_JUMBO:
jeffhao71eee1f2011-01-04 14:18:54 -08001709 case OP_SPUT_JUMBO:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001710 case OP_SPUT_OBJECT:
buzbeeddc7d292010-09-02 17:16:24 -07001711 case OP_SPUT_OBJECT_VOLATILE:
buzbeebd7865b2011-03-31 10:55:04 -07001712 case OP_SPUT_OBJECT_VOLATILE_JUMBO:
jeffhao71eee1f2011-01-04 14:18:54 -08001713 case OP_SPUT_OBJECT_JUMBO:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001714 case OP_SPUT_BOOLEAN:
jeffhao71eee1f2011-01-04 14:18:54 -08001715 case OP_SPUT_BOOLEAN_JUMBO:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001716 case OP_SPUT_CHAR:
jeffhao71eee1f2011-01-04 14:18:54 -08001717 case OP_SPUT_CHAR_JUMBO:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001718 case OP_SPUT_BYTE:
jeffhao71eee1f2011-01-04 14:18:54 -08001719 case OP_SPUT_BYTE_JUMBO:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001720 case OP_SPUT_SHORT:
jeffhao71eee1f2011-01-04 14:18:54 -08001721 case OP_SPUT_SHORT_JUMBO: {
Bill Buzbee50a6bf22009-07-08 13:08:04 -07001722 int valOffset = offsetof(StaticField, value);
Bill Buzbeec6f10662010-02-09 11:16:15 -08001723 int tReg = dvmCompilerAllocTemp(cUnit);
buzbeed3b0a4b2010-09-27 11:30:22 -07001724 int objHead;
buzbeeecf8f6e2010-07-20 14:53:42 -07001725 bool isVolatile;
buzbeed3b0a4b2010-09-27 11:30:22 -07001726 bool isSputObject;
Ben Cheng7a2697d2010-06-07 13:44:23 -07001727 const Method *method = (mir->OptimizationFlags & MIR_CALLEE) ?
1728 mir->meta.calleeMethod : cUnit->method;
1729 void *fieldPtr = (void*)
1730 (method->clazz->pDvmDex->pResFields[mir->dalvikInsn.vB]);
buzbeebd7865b2011-03-31 10:55:04 -07001731 Opcode opcode = mir->dalvikInsn.opcode;
Ben Chenge9695e52009-06-16 16:11:47 -07001732
Ben Cheng32115a92011-03-22 14:09:09 -07001733 if (fieldPtr == NULL) {
1734 BAIL_LOOP_COMPILATION();
1735 LOGE("Unexpected null static field");
1736 dvmAbort();
1737 }
1738
buzbeee3c0b542011-04-07 15:22:06 -07001739#if ANDROID_SMP != 0
buzbeebd7865b2011-03-31 10:55:04 -07001740 isVolatile = (opcode == OP_SPUT_VOLATILE) ||
1741 (opcode == OP_SPUT_VOLATILE_JUMBO) ||
1742 (opcode == OP_SPUT_OBJECT_VOLATILE) ||
buzbee8b94be12011-04-04 12:25:57 -07001743 (opcode == OP_SPUT_OBJECT_VOLATILE_JUMBO);
buzbee8b94be12011-04-04 12:25:57 -07001744 assert(isVolatile == dvmIsVolatileField((Field *) fieldPtr));
buzbeee3c0b542011-04-07 15:22:06 -07001745#else
1746 isVolatile = dvmIsVolatileField((Field *) fieldPtr);
1747#endif
buzbeeecf8f6e2010-07-20 14:53:42 -07001748
buzbeebd7865b2011-03-31 10:55:04 -07001749 isSputObject = (opcode == OP_SPUT_OBJECT) ||
1750 (opcode == OP_SPUT_OBJECT_JUMBO) ||
1751 (opcode == OP_SPUT_OBJECT_VOLATILE) ||
1752 (opcode == OP_SPUT_OBJECT_VOLATILE_JUMBO);
buzbeed3b0a4b2010-09-27 11:30:22 -07001753
Bill Buzbeec6f10662010-02-09 11:16:15 -08001754 rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07001755 rlSrc = loadValue(cUnit, rlSrc, kAnyReg);
buzbeeb78c76f2010-09-30 19:08:20 -07001756 loadConstant(cUnit, tReg, (int) fieldPtr);
buzbeed3b0a4b2010-09-27 11:30:22 -07001757 if (isSputObject) {
1758 objHead = dvmCompilerAllocTemp(cUnit);
buzbeeb78c76f2010-09-30 19:08:20 -07001759 loadWordDisp(cUnit, tReg, offsetof(Field, clazz), objHead);
buzbeed3b0a4b2010-09-27 11:30:22 -07001760 }
Ben Cheng11d8f142010-03-24 15:24:19 -07001761 HEAP_ACCESS_SHADOW(true);
buzbeeb78c76f2010-09-30 19:08:20 -07001762 storeWordDisp(cUnit, tReg, valOffset ,rlSrc.lowReg);
buzbeed3b0a4b2010-09-27 11:30:22 -07001763 dvmCompilerFreeTemp(cUnit, tReg);
Ben Cheng11d8f142010-03-24 15:24:19 -07001764 HEAP_ACCESS_SHADOW(false);
buzbeeecf8f6e2010-07-20 14:53:42 -07001765 if (isVolatile) {
buzbee2ce33c92010-11-01 15:53:27 -07001766 dvmCompilerGenMemBarrier(cUnit, kSY);
buzbeeecf8f6e2010-07-20 14:53:42 -07001767 }
buzbeed3b0a4b2010-09-27 11:30:22 -07001768 if (isSputObject) {
buzbeeb78c76f2010-09-30 19:08:20 -07001769 /* NOTE: marking card based sfield->clazz */
buzbeed3b0a4b2010-09-27 11:30:22 -07001770 markCard(cUnit, rlSrc.lowReg, objHead);
1771 dvmCompilerFreeTemp(cUnit, objHead);
buzbee919eb062010-07-12 12:59:22 -07001772 }
Ben Cheng11d8f142010-03-24 15:24:19 -07001773
Ben Chengba4fc8b2009-06-01 13:00:29 -07001774 break;
1775 }
jeffhao71eee1f2011-01-04 14:18:54 -08001776 case OP_SPUT_WIDE:
1777 case OP_SPUT_WIDE_JUMBO: {
Bill Buzbeec6f10662010-02-09 11:16:15 -08001778 int tReg = dvmCompilerAllocTemp(cUnit);
Bill Buzbee50a6bf22009-07-08 13:08:04 -07001779 int valOffset = offsetof(StaticField, value);
Ben Cheng7a2697d2010-06-07 13:44:23 -07001780 const Method *method = (mir->OptimizationFlags & MIR_CALLEE) ?
1781 mir->meta.calleeMethod : cUnit->method;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001782 void *fieldPtr = (void*)
Ben Cheng7a2697d2010-06-07 13:44:23 -07001783 (method->clazz->pDvmDex->pResFields[mir->dalvikInsn.vB]);
Ben Chenge9695e52009-06-16 16:11:47 -07001784
Ben Chengdd6e8702010-05-07 13:05:47 -07001785 if (fieldPtr == NULL) {
Ben Cheng32115a92011-03-22 14:09:09 -07001786 BAIL_LOOP_COMPILATION();
Ben Chengdd6e8702010-05-07 13:05:47 -07001787 LOGE("Unexpected null static field");
1788 dvmAbort();
1789 }
1790
Bill Buzbeec6f10662010-02-09 11:16:15 -08001791 rlSrc = dvmCompilerGetSrcWide(cUnit, mir, 0, 1);
Bill Buzbee1465db52009-09-23 17:17:35 -07001792 rlSrc = loadValueWide(cUnit, rlSrc, kAnyReg);
1793 loadConstant(cUnit, tReg, (int) fieldPtr + valOffset);
Ben Cheng11d8f142010-03-24 15:24:19 -07001794
1795 HEAP_ACCESS_SHADOW(true);
Bill Buzbee1465db52009-09-23 17:17:35 -07001796 storePair(cUnit, tReg, rlSrc.lowReg, rlSrc.highReg);
Ben Cheng11d8f142010-03-24 15:24:19 -07001797 HEAP_ACCESS_SHADOW(false);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001798 break;
1799 }
jeffhao71eee1f2011-01-04 14:18:54 -08001800 case OP_NEW_INSTANCE:
1801 case OP_NEW_INSTANCE_JUMBO: {
Ben Chenge9695e52009-06-16 16:11:47 -07001802 /*
1803 * Obey the calling convention and don't mess with the register
1804 * usage.
1805 */
Carl Shapirofc75f3e2010-12-07 11:43:38 -08001806 ClassObject *classPtr = (ClassObject *)
Ben Chengba4fc8b2009-06-01 13:00:29 -07001807 (cUnit->method->clazz->pDvmDex->pResClasses[mir->dalvikInsn.vB]);
Ben Chengdd6e8702010-05-07 13:05:47 -07001808
1809 if (classPtr == NULL) {
Ben Cheng32115a92011-03-22 14:09:09 -07001810 BAIL_LOOP_COMPILATION();
Ben Chengdd6e8702010-05-07 13:05:47 -07001811 LOGE("Unexpected null class");
1812 dvmAbort();
1813 }
1814
Ben Cheng79d173c2009-09-29 16:12:51 -07001815 /*
1816 * If it is going to throw, it should not make to the trace to begin
Bill Buzbee1465db52009-09-23 17:17:35 -07001817 * with. However, Alloc might throw, so we need to genExportPC()
Ben Cheng79d173c2009-09-29 16:12:51 -07001818 */
1819 assert((classPtr->accessFlags & (ACC_INTERFACE|ACC_ABSTRACT)) == 0);
Bill Buzbeec6f10662010-02-09 11:16:15 -08001820 dvmCompilerFlushAllRegs(cUnit); /* Everything to home location */
Bill Buzbee1465db52009-09-23 17:17:35 -07001821 genExportPC(cUnit, mir);
Ben Chengbd1326d2010-04-02 15:04:53 -07001822 LOAD_FUNC_ADDR(cUnit, r2, (int)dvmAllocObject);
Ben Chenge9695e52009-06-16 16:11:47 -07001823 loadConstant(cUnit, r0, (int) classPtr);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001824 loadConstant(cUnit, r1, ALLOC_DONT_TRACK);
Bill Buzbee1465db52009-09-23 17:17:35 -07001825 opReg(cUnit, kOpBlx, r2);
Elliott Hughes6a555132010-02-25 15:41:42 -08001826 dvmCompilerClobberCallRegs(cUnit);
Ben Cheng4f489172009-09-27 17:08:35 -07001827 /* generate a branch over if allocation is successful */
buzbee8f8109a2010-08-31 10:16:35 -07001828 ArmLIR *branchOver = genCmpImmBranch(cUnit, kArmCondNe, r0, 0);
Ben Cheng4f489172009-09-27 17:08:35 -07001829 /*
1830 * OOM exception needs to be thrown here and cannot re-execute
1831 */
1832 loadConstant(cUnit, r0,
1833 (int) (cUnit->method->insns + mir->offset));
1834 genDispatchToHandler(cUnit, TEMPLATE_THROW_EXCEPTION_COMMON);
1835 /* noreturn */
1836
Bill Buzbee1465db52009-09-23 17:17:35 -07001837 ArmLIR *target = newLIR0(cUnit, kArmPseudoTargetLabel);
Ben Cheng4f489172009-09-27 17:08:35 -07001838 target->defMask = ENCODE_ALL;
1839 branchOver->generic.target = (LIR *) target;
Bill Buzbeec6f10662010-02-09 11:16:15 -08001840 rlDest = dvmCompilerGetDest(cUnit, mir, 0);
1841 rlResult = dvmCompilerGetReturn(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07001842 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001843 break;
1844 }
jeffhao71eee1f2011-01-04 14:18:54 -08001845 case OP_CHECK_CAST:
1846 case OP_CHECK_CAST_JUMBO: {
Ben Chenge9695e52009-06-16 16:11:47 -07001847 /*
1848 * Obey the calling convention and don't mess with the register
1849 * usage.
1850 */
Ben Chengba4fc8b2009-06-01 13:00:29 -07001851 ClassObject *classPtr =
1852 (cUnit->method->clazz->pDvmDex->pResClasses[mir->dalvikInsn.vB]);
Bill Buzbee4df41a52009-11-12 17:07:16 -08001853 /*
1854 * Note: It is possible that classPtr is NULL at this point,
1855 * even though this instruction has been successfully interpreted.
1856 * If the previous interpretation had a null source, the
1857 * interpreter would not have bothered to resolve the clazz.
1858 * Bail out to the interpreter in this case, and log it
1859 * so that we can tell if it happens frequently.
1860 */
1861 if (classPtr == NULL) {
Ben Cheng32115a92011-03-22 14:09:09 -07001862 BAIL_LOOP_COMPILATION();
1863 LOGVV("null clazz in OP_CHECK_CAST, single-stepping");
1864 genInterpSingleStep(cUnit, mir);
1865 return false;
Bill Buzbee4df41a52009-11-12 17:07:16 -08001866 }
Bill Buzbeec6f10662010-02-09 11:16:15 -08001867 dvmCompilerFlushAllRegs(cUnit); /* Everything to home location */
Ben Chengba4fc8b2009-06-01 13:00:29 -07001868 loadConstant(cUnit, r1, (int) classPtr );
Bill Buzbeec6f10662010-02-09 11:16:15 -08001869 rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07001870 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
buzbee8f8109a2010-08-31 10:16:35 -07001871 /* Null? */
1872 ArmLIR *branch1 = genCmpImmBranch(cUnit, kArmCondEq,
1873 rlSrc.lowReg, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07001874 /*
1875 * rlSrc.lowReg now contains object->clazz. Note that
1876 * it could have been allocated r0, but we're okay so long
1877 * as we don't do anything desctructive until r0 is loaded
1878 * with clazz.
1879 */
Ben Chengba4fc8b2009-06-01 13:00:29 -07001880 /* r0 now contains object->clazz */
Bill Buzbee1465db52009-09-23 17:17:35 -07001881 loadWordDisp(cUnit, rlSrc.lowReg, offsetof(Object, clazz), r0);
Ben Chengbd1326d2010-04-02 15:04:53 -07001882 LOAD_FUNC_ADDR(cUnit, r2, (int)dvmInstanceofNonTrivial);
Bill Buzbee1465db52009-09-23 17:17:35 -07001883 opRegReg(cUnit, kOpCmp, r0, r1);
1884 ArmLIR *branch2 = opCondBranch(cUnit, kArmCondEq);
1885 opReg(cUnit, kOpBlx, r2);
Elliott Hughes6a555132010-02-25 15:41:42 -08001886 dvmCompilerClobberCallRegs(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07001887 /*
1888 * If null, check cast failed - punt to the interpreter. Because
1889 * interpreter will be the one throwing, we don't need to
1890 * genExportPC() here.
1891 */
Bill Buzbee270c1d62009-08-13 16:58:07 -07001892 genZeroCheck(cUnit, r0, mir->offset, NULL);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001893 /* check cast passed - branch target here */
Bill Buzbee1465db52009-09-23 17:17:35 -07001894 ArmLIR *target = newLIR0(cUnit, kArmPseudoTargetLabel);
Ben Chengd7d426a2009-09-22 11:23:36 -07001895 target->defMask = ENCODE_ALL;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001896 branch1->generic.target = (LIR *)target;
1897 branch2->generic.target = (LIR *)target;
1898 break;
1899 }
buzbee4d92e682010-07-29 15:24:14 -07001900 case OP_SGET_WIDE_VOLATILE:
buzbeebd7865b2011-03-31 10:55:04 -07001901 case OP_SGET_WIDE_VOLATILE_JUMBO:
buzbee4d92e682010-07-29 15:24:14 -07001902 case OP_SPUT_WIDE_VOLATILE:
buzbeebd7865b2011-03-31 10:55:04 -07001903 case OP_SPUT_WIDE_VOLATILE_JUMBO:
buzbee4d92e682010-07-29 15:24:14 -07001904 genInterpSingleStep(cUnit, mir);
1905 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001906 default:
1907 return true;
1908 }
1909 return false;
1910}
1911
Ben Cheng7a2697d2010-06-07 13:44:23 -07001912/*
1913 * A typical example of inlined getter/setter from a monomorphic callsite:
1914 *
1915 * D/dalvikvm( 289): -------- dalvik offset: 0x0000 @ invoke-static (I)
1916 * D/dalvikvm( 289): -------- dalvik offset: 0x0000 @ sget-object (C) v0, ...
1917 * D/dalvikvm( 289): 0x4427fc22 (0002): ldr r0, [pc, #56]
1918 * D/dalvikvm( 289): 0x4427fc24 (0004): ldr r1, [r0, #0]
1919 * D/dalvikvm( 289): 0x4427fc26 (0006): str r1, [r5, #0]
1920 * D/dalvikvm( 289): 0x4427fc28 (0008): .align4
1921 * D/dalvikvm( 289): L0x0003:
1922 * D/dalvikvm( 289): -------- dalvik offset: 0x0003 @ move-result-object (I) v0
1923 *
1924 * Note the invoke-static and move-result-object with the (I) notation are
1925 * turned into no-op.
1926 */
Ben Chengba4fc8b2009-06-01 13:00:29 -07001927static bool handleFmt11x(CompilationUnit *cUnit, MIR *mir)
1928{
Dan Bornstein9a1f8162010-12-01 17:02:26 -08001929 Opcode dalvikOpcode = mir->dalvikInsn.opcode;
Bill Buzbee1465db52009-09-23 17:17:35 -07001930 RegLocation rlResult;
Dan Bornstein9a1f8162010-12-01 17:02:26 -08001931 switch (dalvikOpcode) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07001932 case OP_MOVE_EXCEPTION: {
Ben Chengba4fc8b2009-06-01 13:00:29 -07001933 int exOffset = offsetof(Thread, exception);
Bill Buzbeec6f10662010-02-09 11:16:15 -08001934 int resetReg = dvmCompilerAllocTemp(cUnit);
1935 RegLocation rlDest = dvmCompilerGetDest(cUnit, mir, 0);
1936 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Ben Cheng20d7e6c2011-02-18 17:12:42 -08001937 loadWordDisp(cUnit, r6SELF, exOffset, rlResult.lowReg);
Bill Buzbeef9f33282009-11-22 12:45:30 -08001938 loadConstant(cUnit, resetReg, 0);
Ben Cheng20d7e6c2011-02-18 17:12:42 -08001939 storeWordDisp(cUnit, r6SELF, exOffset, resetReg);
Bill Buzbee1465db52009-09-23 17:17:35 -07001940 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001941 break;
1942 }
1943 case OP_MOVE_RESULT:
1944 case OP_MOVE_RESULT_OBJECT: {
Ben Cheng7a2697d2010-06-07 13:44:23 -07001945 /* An inlined move result is effectively no-op */
1946 if (mir->OptimizationFlags & MIR_INLINED)
1947 break;
Bill Buzbeec6f10662010-02-09 11:16:15 -08001948 RegLocation rlDest = dvmCompilerGetDest(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07001949 RegLocation rlSrc = LOC_DALVIK_RETURN_VAL;
1950 rlSrc.fp = rlDest.fp;
1951 storeValue(cUnit, rlDest, rlSrc);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001952 break;
1953 }
1954 case OP_MOVE_RESULT_WIDE: {
Ben Cheng7a2697d2010-06-07 13:44:23 -07001955 /* An inlined move result is effectively no-op */
1956 if (mir->OptimizationFlags & MIR_INLINED)
1957 break;
Bill Buzbeec6f10662010-02-09 11:16:15 -08001958 RegLocation rlDest = dvmCompilerGetDestWide(cUnit, mir, 0, 1);
Bill Buzbee1465db52009-09-23 17:17:35 -07001959 RegLocation rlSrc = LOC_DALVIK_RETURN_VAL_WIDE;
1960 rlSrc.fp = rlDest.fp;
1961 storeValueWide(cUnit, rlDest, rlSrc);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001962 break;
1963 }
1964 case OP_RETURN_WIDE: {
Bill Buzbeec6f10662010-02-09 11:16:15 -08001965 RegLocation rlSrc = dvmCompilerGetSrcWide(cUnit, mir, 0, 1);
Bill Buzbee1465db52009-09-23 17:17:35 -07001966 RegLocation rlDest = LOC_DALVIK_RETURN_VAL_WIDE;
1967 rlDest.fp = rlSrc.fp;
1968 storeValueWide(cUnit, rlDest, rlSrc);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001969 genReturnCommon(cUnit,mir);
1970 break;
1971 }
1972 case OP_RETURN:
1973 case OP_RETURN_OBJECT: {
Bill Buzbeec6f10662010-02-09 11:16:15 -08001974 RegLocation rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07001975 RegLocation rlDest = LOC_DALVIK_RETURN_VAL;
1976 rlDest.fp = rlSrc.fp;
1977 storeValue(cUnit, rlDest, rlSrc);
Ben Cheng20d7e6c2011-02-18 17:12:42 -08001978 genReturnCommon(cUnit, mir);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001979 break;
1980 }
Bill Buzbee1465db52009-09-23 17:17:35 -07001981 case OP_MONITOR_EXIT:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001982 case OP_MONITOR_ENTER:
Ben Cheng5d90c202009-11-22 23:31:11 -08001983 genMonitor(cUnit, mir);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001984 break;
Ben Cheng20d7e6c2011-02-18 17:12:42 -08001985 case OP_THROW:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001986 genInterpSingleStep(cUnit, mir);
1987 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001988 default:
1989 return true;
1990 }
1991 return false;
1992}
1993
Bill Buzbeed45ba372009-06-15 17:00:57 -07001994static bool handleFmt12x(CompilationUnit *cUnit, MIR *mir)
1995{
Dan Bornstein9a1f8162010-12-01 17:02:26 -08001996 Opcode opcode = mir->dalvikInsn.opcode;
Bill Buzbee1465db52009-09-23 17:17:35 -07001997 RegLocation rlDest;
1998 RegLocation rlSrc;
1999 RegLocation rlResult;
Bill Buzbeed45ba372009-06-15 17:00:57 -07002000
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002001 if ( (opcode >= OP_ADD_INT_2ADDR) && (opcode <= OP_REM_DOUBLE_2ADDR)) {
Ben Cheng5d90c202009-11-22 23:31:11 -08002002 return genArithOp( cUnit, mir );
Ben Chengba4fc8b2009-06-01 13:00:29 -07002003 }
2004
Bill Buzbee1465db52009-09-23 17:17:35 -07002005 if (mir->ssaRep->numUses == 2)
Bill Buzbeec6f10662010-02-09 11:16:15 -08002006 rlSrc = dvmCompilerGetSrcWide(cUnit, mir, 0, 1);
Bill Buzbee1465db52009-09-23 17:17:35 -07002007 else
Bill Buzbeec6f10662010-02-09 11:16:15 -08002008 rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07002009 if (mir->ssaRep->numDefs == 2)
Bill Buzbeec6f10662010-02-09 11:16:15 -08002010 rlDest = dvmCompilerGetDestWide(cUnit, mir, 0, 1);
Bill Buzbee1465db52009-09-23 17:17:35 -07002011 else
Bill Buzbeec6f10662010-02-09 11:16:15 -08002012 rlDest = dvmCompilerGetDest(cUnit, mir, 0);
Ben Chenge9695e52009-06-16 16:11:47 -07002013
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002014 switch (opcode) {
Bill Buzbee1465db52009-09-23 17:17:35 -07002015 case OP_DOUBLE_TO_INT:
Ben Chengba4fc8b2009-06-01 13:00:29 -07002016 case OP_INT_TO_FLOAT:
Ben Chengba4fc8b2009-06-01 13:00:29 -07002017 case OP_FLOAT_TO_INT:
Ben Chengba4fc8b2009-06-01 13:00:29 -07002018 case OP_DOUBLE_TO_FLOAT:
Ben Chengba4fc8b2009-06-01 13:00:29 -07002019 case OP_FLOAT_TO_DOUBLE:
Ben Chengba4fc8b2009-06-01 13:00:29 -07002020 case OP_INT_TO_DOUBLE:
Ben Chengba4fc8b2009-06-01 13:00:29 -07002021 case OP_FLOAT_TO_LONG:
Ben Chengba4fc8b2009-06-01 13:00:29 -07002022 case OP_LONG_TO_FLOAT:
Ben Chengba4fc8b2009-06-01 13:00:29 -07002023 case OP_DOUBLE_TO_LONG:
Ben Chengba4fc8b2009-06-01 13:00:29 -07002024 case OP_LONG_TO_DOUBLE:
Ben Cheng5d90c202009-11-22 23:31:11 -08002025 return genConversion(cUnit, mir);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002026 case OP_NEG_INT:
2027 case OP_NOT_INT:
Ben Cheng5d90c202009-11-22 23:31:11 -08002028 return genArithOpInt(cUnit, mir, rlDest, rlSrc, rlSrc);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002029 case OP_NEG_LONG:
2030 case OP_NOT_LONG:
Ben Cheng5d90c202009-11-22 23:31:11 -08002031 return genArithOpLong(cUnit, mir, rlDest, rlSrc, rlSrc);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002032 case OP_NEG_FLOAT:
Ben Cheng5d90c202009-11-22 23:31:11 -08002033 return genArithOpFloat(cUnit, mir, rlDest, rlSrc, rlSrc);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002034 case OP_NEG_DOUBLE:
Ben Cheng5d90c202009-11-22 23:31:11 -08002035 return genArithOpDouble(cUnit, mir, rlDest, rlSrc, rlSrc);
Bill Buzbee1465db52009-09-23 17:17:35 -07002036 case OP_MOVE_WIDE:
2037 storeValueWide(cUnit, rlDest, rlSrc);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002038 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07002039 case OP_INT_TO_LONG:
Bill Buzbeec6f10662010-02-09 11:16:15 -08002040 rlSrc = dvmCompilerUpdateLoc(cUnit, rlSrc);
2041 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Bill Buzbee964a7b02010-01-28 12:54:19 -08002042 //TUNING: shouldn't loadValueDirect already check for phys reg?
Bill Buzbee1465db52009-09-23 17:17:35 -07002043 if (rlSrc.location == kLocPhysReg) {
2044 genRegCopy(cUnit, rlResult.lowReg, rlSrc.lowReg);
2045 } else {
2046 loadValueDirect(cUnit, rlSrc, rlResult.lowReg);
2047 }
2048 opRegRegImm(cUnit, kOpAsr, rlResult.highReg,
2049 rlResult.lowReg, 31);
2050 storeValueWide(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002051 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07002052 case OP_LONG_TO_INT:
Bill Buzbeec6f10662010-02-09 11:16:15 -08002053 rlSrc = dvmCompilerUpdateLocWide(cUnit, rlSrc);
2054 rlSrc = dvmCompilerWideToNarrow(cUnit, rlSrc);
Bill Buzbee1465db52009-09-23 17:17:35 -07002055 // Intentional fallthrough
Ben Chengba4fc8b2009-06-01 13:00:29 -07002056 case OP_MOVE:
2057 case OP_MOVE_OBJECT:
Bill Buzbee1465db52009-09-23 17:17:35 -07002058 storeValue(cUnit, rlDest, rlSrc);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002059 break;
2060 case OP_INT_TO_BYTE:
Bill Buzbee1465db52009-09-23 17:17:35 -07002061 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
Bill Buzbeec6f10662010-02-09 11:16:15 -08002062 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Bill Buzbee1465db52009-09-23 17:17:35 -07002063 opRegReg(cUnit, kOp2Byte, rlResult.lowReg, rlSrc.lowReg);
2064 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002065 break;
2066 case OP_INT_TO_SHORT:
Bill Buzbee1465db52009-09-23 17:17:35 -07002067 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
Bill Buzbeec6f10662010-02-09 11:16:15 -08002068 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Bill Buzbee1465db52009-09-23 17:17:35 -07002069 opRegReg(cUnit, kOp2Short, rlResult.lowReg, rlSrc.lowReg);
2070 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002071 break;
2072 case OP_INT_TO_CHAR:
Bill Buzbee1465db52009-09-23 17:17:35 -07002073 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
Bill Buzbeec6f10662010-02-09 11:16:15 -08002074 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Bill Buzbee1465db52009-09-23 17:17:35 -07002075 opRegReg(cUnit, kOp2Char, rlResult.lowReg, rlSrc.lowReg);
2076 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002077 break;
2078 case OP_ARRAY_LENGTH: {
2079 int lenOffset = offsetof(ArrayObject, length);
Bill Buzbee1465db52009-09-23 17:17:35 -07002080 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
2081 genNullCheck(cUnit, rlSrc.sRegLow, rlSrc.lowReg,
2082 mir->offset, NULL);
Bill Buzbeec6f10662010-02-09 11:16:15 -08002083 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Bill Buzbee1465db52009-09-23 17:17:35 -07002084 loadWordDisp(cUnit, rlSrc.lowReg, lenOffset,
2085 rlResult.lowReg);
2086 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002087 break;
2088 }
2089 default:
2090 return true;
2091 }
2092 return false;
2093}
2094
2095static bool handleFmt21s(CompilationUnit *cUnit, MIR *mir)
2096{
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002097 Opcode dalvikOpcode = mir->dalvikInsn.opcode;
Bill Buzbee1465db52009-09-23 17:17:35 -07002098 RegLocation rlDest;
2099 RegLocation rlResult;
2100 int BBBB = mir->dalvikInsn.vB;
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002101 if (dalvikOpcode == OP_CONST_WIDE_16) {
Bill Buzbeec6f10662010-02-09 11:16:15 -08002102 rlDest = dvmCompilerGetDestWide(cUnit, mir, 0, 1);
2103 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Ben Chengbd1326d2010-04-02 15:04:53 -07002104 loadConstantNoClobber(cUnit, rlResult.lowReg, BBBB);
Bill Buzbee964a7b02010-01-28 12:54:19 -08002105 //TUNING: do high separately to avoid load dependency
Bill Buzbee1465db52009-09-23 17:17:35 -07002106 opRegRegImm(cUnit, kOpAsr, rlResult.highReg, rlResult.lowReg, 31);
2107 storeValueWide(cUnit, rlDest, rlResult);
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002108 } else if (dalvikOpcode == OP_CONST_16) {
Bill Buzbeec6f10662010-02-09 11:16:15 -08002109 rlDest = dvmCompilerGetDest(cUnit, mir, 0);
2110 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kAnyReg, true);
Ben Chengbd1326d2010-04-02 15:04:53 -07002111 loadConstantNoClobber(cUnit, rlResult.lowReg, BBBB);
Bill Buzbee1465db52009-09-23 17:17:35 -07002112 storeValue(cUnit, rlDest, rlResult);
2113 } else
Ben Chengba4fc8b2009-06-01 13:00:29 -07002114 return true;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002115 return false;
2116}
2117
2118/* Compare agaist zero */
2119static bool handleFmt21t(CompilationUnit *cUnit, MIR *mir, BasicBlock *bb,
Bill Buzbee89efc3d2009-07-28 11:22:22 -07002120 ArmLIR *labelList)
Ben Chengba4fc8b2009-06-01 13:00:29 -07002121{
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002122 Opcode dalvikOpcode = mir->dalvikInsn.opcode;
Bill Buzbee89efc3d2009-07-28 11:22:22 -07002123 ArmConditionCode cond;
Ben Cheng7ab74e12011-02-03 14:02:06 -08002124 /* backward branch? */
2125 bool backwardBranch = (bb->taken->startOffset <= mir->offset);
2126
Ben Cheng32115a92011-03-22 14:09:09 -07002127 if (backwardBranch &&
2128 (gDvmJit.genSuspendPoll || cUnit->jitMode == kJitLoop)) {
Ben Cheng7ab74e12011-02-03 14:02:06 -08002129 genSuspendPoll(cUnit, mir);
2130 }
2131
Bill Buzbeec6f10662010-02-09 11:16:15 -08002132 RegLocation rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07002133 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
Ben Cheng7ab74e12011-02-03 14:02:06 -08002134
Bill Buzbee1465db52009-09-23 17:17:35 -07002135 opRegImm(cUnit, kOpCmp, rlSrc.lowReg, 0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002136
Bill Buzbee270c1d62009-08-13 16:58:07 -07002137//TUNING: break this out to allow use of Thumb2 CB[N]Z
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002138 switch (dalvikOpcode) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07002139 case OP_IF_EQZ:
Bill Buzbee1465db52009-09-23 17:17:35 -07002140 cond = kArmCondEq;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002141 break;
2142 case OP_IF_NEZ:
Bill Buzbee1465db52009-09-23 17:17:35 -07002143 cond = kArmCondNe;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002144 break;
2145 case OP_IF_LTZ:
Bill Buzbee1465db52009-09-23 17:17:35 -07002146 cond = kArmCondLt;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002147 break;
2148 case OP_IF_GEZ:
Bill Buzbee1465db52009-09-23 17:17:35 -07002149 cond = kArmCondGe;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002150 break;
2151 case OP_IF_GTZ:
Bill Buzbee1465db52009-09-23 17:17:35 -07002152 cond = kArmCondGt;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002153 break;
2154 case OP_IF_LEZ:
Bill Buzbee1465db52009-09-23 17:17:35 -07002155 cond = kArmCondLe;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002156 break;
2157 default:
2158 cond = 0;
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002159 LOGE("Unexpected opcode (%d) for Fmt21t\n", dalvikOpcode);
Bill Buzbeefc519dc2010-03-06 23:30:57 -08002160 dvmCompilerAbort(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002161 }
2162 genConditionalBranch(cUnit, cond, &labelList[bb->taken->id]);
2163 /* This mostly likely will be optimized away in a later phase */
2164 genUnconditionalBranch(cUnit, &labelList[bb->fallThrough->id]);
2165 return false;
2166}
2167
Elliott Hughesb4c05972010-02-24 16:36:18 -08002168static bool isPowerOfTwo(int x)
2169{
2170 return (x & (x - 1)) == 0;
2171}
2172
2173// Returns true if no more than two bits are set in 'x'.
2174static bool isPopCountLE2(unsigned int x)
2175{
2176 x &= x - 1;
2177 return (x & (x - 1)) == 0;
2178}
2179
2180// Returns the index of the lowest set bit in 'x'.
2181static int lowestSetBit(unsigned int x) {
2182 int bit_posn = 0;
2183 while ((x & 0xf) == 0) {
2184 bit_posn += 4;
2185 x >>= 4;
Bill Buzbee78cb0e22010-02-11 14:04:53 -08002186 }
Elliott Hughesb4c05972010-02-24 16:36:18 -08002187 while ((x & 1) == 0) {
2188 bit_posn++;
2189 x >>= 1;
2190 }
2191 return bit_posn;
2192}
2193
Elliott Hughes672511b2010-04-26 17:40:13 -07002194// Returns true if it added instructions to 'cUnit' to divide 'rlSrc' by 'lit'
2195// and store the result in 'rlDest'.
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002196static bool handleEasyDivide(CompilationUnit *cUnit, Opcode dalvikOpcode,
Elliott Hughes672511b2010-04-26 17:40:13 -07002197 RegLocation rlSrc, RegLocation rlDest, int lit)
2198{
2199 if (lit < 2 || !isPowerOfTwo(lit)) {
2200 return false;
2201 }
2202 int k = lowestSetBit(lit);
2203 if (k >= 30) {
2204 // Avoid special cases.
2205 return false;
2206 }
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002207 bool div = (dalvikOpcode == OP_DIV_INT_LIT8 || dalvikOpcode == OP_DIV_INT_LIT16);
Elliott Hughes672511b2010-04-26 17:40:13 -07002208 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
2209 RegLocation rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Elliott Hughes9c457022010-04-28 16:15:38 -07002210 if (div) {
2211 int tReg = dvmCompilerAllocTemp(cUnit);
2212 if (lit == 2) {
2213 // Division by 2 is by far the most common division by constant.
2214 opRegRegImm(cUnit, kOpLsr, tReg, rlSrc.lowReg, 32 - k);
2215 opRegRegReg(cUnit, kOpAdd, tReg, tReg, rlSrc.lowReg);
2216 opRegRegImm(cUnit, kOpAsr, rlResult.lowReg, tReg, k);
2217 } else {
2218 opRegRegImm(cUnit, kOpAsr, tReg, rlSrc.lowReg, 31);
2219 opRegRegImm(cUnit, kOpLsr, tReg, tReg, 32 - k);
2220 opRegRegReg(cUnit, kOpAdd, tReg, tReg, rlSrc.lowReg);
2221 opRegRegImm(cUnit, kOpAsr, rlResult.lowReg, tReg, k);
2222 }
Elliott Hughes672511b2010-04-26 17:40:13 -07002223 } else {
Elliott Hughes9c457022010-04-28 16:15:38 -07002224 int cReg = dvmCompilerAllocTemp(cUnit);
2225 loadConstant(cUnit, cReg, lit - 1);
2226 int tReg1 = dvmCompilerAllocTemp(cUnit);
2227 int tReg2 = dvmCompilerAllocTemp(cUnit);
2228 if (lit == 2) {
2229 opRegRegImm(cUnit, kOpLsr, tReg1, rlSrc.lowReg, 32 - k);
2230 opRegRegReg(cUnit, kOpAdd, tReg2, tReg1, rlSrc.lowReg);
2231 opRegRegReg(cUnit, kOpAnd, tReg2, tReg2, cReg);
2232 opRegRegReg(cUnit, kOpSub, rlResult.lowReg, tReg2, tReg1);
2233 } else {
2234 opRegRegImm(cUnit, kOpAsr, tReg1, rlSrc.lowReg, 31);
2235 opRegRegImm(cUnit, kOpLsr, tReg1, tReg1, 32 - k);
2236 opRegRegReg(cUnit, kOpAdd, tReg2, tReg1, rlSrc.lowReg);
2237 opRegRegReg(cUnit, kOpAnd, tReg2, tReg2, cReg);
2238 opRegRegReg(cUnit, kOpSub, rlResult.lowReg, tReg2, tReg1);
2239 }
Elliott Hughes672511b2010-04-26 17:40:13 -07002240 }
2241 storeValue(cUnit, rlDest, rlResult);
2242 return true;
2243}
2244
Elliott Hughesb4c05972010-02-24 16:36:18 -08002245// Returns true if it added instructions to 'cUnit' to multiply 'rlSrc' by 'lit'
2246// and store the result in 'rlDest'.
2247static bool handleEasyMultiply(CompilationUnit *cUnit,
2248 RegLocation rlSrc, RegLocation rlDest, int lit)
2249{
2250 // Can we simplify this multiplication?
2251 bool powerOfTwo = false;
2252 bool popCountLE2 = false;
2253 bool powerOfTwoMinusOne = false;
2254 if (lit < 2) {
2255 // Avoid special cases.
2256 return false;
2257 } else if (isPowerOfTwo(lit)) {
2258 powerOfTwo = true;
2259 } else if (isPopCountLE2(lit)) {
2260 popCountLE2 = true;
2261 } else if (isPowerOfTwo(lit + 1)) {
2262 powerOfTwoMinusOne = true;
2263 } else {
2264 return false;
2265 }
2266 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
2267 RegLocation rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
2268 if (powerOfTwo) {
2269 // Shift.
2270 opRegRegImm(cUnit, kOpLsl, rlResult.lowReg, rlSrc.lowReg,
2271 lowestSetBit(lit));
2272 } else if (popCountLE2) {
2273 // Shift and add and shift.
2274 int firstBit = lowestSetBit(lit);
2275 int secondBit = lowestSetBit(lit ^ (1 << firstBit));
2276 genMultiplyByTwoBitMultiplier(cUnit, rlSrc, rlResult, lit,
2277 firstBit, secondBit);
2278 } else {
2279 // Reverse subtract: (src << (shift + 1)) - src.
2280 assert(powerOfTwoMinusOne);
2281 // TODO: rsb dst, src, src lsl#lowestSetBit(lit + 1)
2282 int tReg = dvmCompilerAllocTemp(cUnit);
2283 opRegRegImm(cUnit, kOpLsl, tReg, rlSrc.lowReg, lowestSetBit(lit + 1));
2284 opRegRegReg(cUnit, kOpSub, rlResult.lowReg, tReg, rlSrc.lowReg);
2285 }
2286 storeValue(cUnit, rlDest, rlResult);
2287 return true;
Bill Buzbee78cb0e22010-02-11 14:04:53 -08002288}
2289
Ben Chengba4fc8b2009-06-01 13:00:29 -07002290static bool handleFmt22b_Fmt22s(CompilationUnit *cUnit, MIR *mir)
2291{
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002292 Opcode dalvikOpcode = mir->dalvikInsn.opcode;
Bill Buzbeec6f10662010-02-09 11:16:15 -08002293 RegLocation rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
2294 RegLocation rlDest = dvmCompilerGetDest(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07002295 RegLocation rlResult;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002296 int lit = mir->dalvikInsn.vC;
Ben Cheng4f489172009-09-27 17:08:35 -07002297 OpKind op = 0; /* Make gcc happy */
Bill Buzbee1465db52009-09-23 17:17:35 -07002298 int shiftOp = false;
2299 bool isDiv = false;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002300
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002301 switch (dalvikOpcode) {
Bill Buzbee1465db52009-09-23 17:17:35 -07002302 case OP_RSUB_INT_LIT8:
2303 case OP_RSUB_INT: {
2304 int tReg;
2305 //TUNING: add support for use of Arm rsub op
2306 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
Bill Buzbeec6f10662010-02-09 11:16:15 -08002307 tReg = dvmCompilerAllocTemp(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07002308 loadConstant(cUnit, tReg, lit);
Bill Buzbeec6f10662010-02-09 11:16:15 -08002309 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Bill Buzbee1465db52009-09-23 17:17:35 -07002310 opRegRegReg(cUnit, kOpSub, rlResult.lowReg,
2311 tReg, rlSrc.lowReg);
2312 storeValue(cUnit, rlDest, rlResult);
2313 return false;
2314 break;
2315 }
2316
Ben Chengba4fc8b2009-06-01 13:00:29 -07002317 case OP_ADD_INT_LIT8:
2318 case OP_ADD_INT_LIT16:
Bill Buzbee1465db52009-09-23 17:17:35 -07002319 op = kOpAdd;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002320 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002321 case OP_MUL_INT_LIT8:
Bill Buzbee78cb0e22010-02-11 14:04:53 -08002322 case OP_MUL_INT_LIT16: {
Elliott Hughesb4c05972010-02-24 16:36:18 -08002323 if (handleEasyMultiply(cUnit, rlSrc, rlDest, lit)) {
2324 return false;
Bill Buzbee78cb0e22010-02-11 14:04:53 -08002325 }
Elliott Hughesb4c05972010-02-24 16:36:18 -08002326 op = kOpMul;
Bill Buzbee1465db52009-09-23 17:17:35 -07002327 break;
Bill Buzbee78cb0e22010-02-11 14:04:53 -08002328 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07002329 case OP_AND_INT_LIT8:
2330 case OP_AND_INT_LIT16:
Bill Buzbee1465db52009-09-23 17:17:35 -07002331 op = kOpAnd;
2332 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002333 case OP_OR_INT_LIT8:
2334 case OP_OR_INT_LIT16:
Bill Buzbee1465db52009-09-23 17:17:35 -07002335 op = kOpOr;
2336 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002337 case OP_XOR_INT_LIT8:
2338 case OP_XOR_INT_LIT16:
Bill Buzbee1465db52009-09-23 17:17:35 -07002339 op = kOpXor;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002340 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002341 case OP_SHL_INT_LIT8:
Bill Buzbee0e605272009-12-01 14:28:05 -08002342 lit &= 31;
Bill Buzbee1465db52009-09-23 17:17:35 -07002343 shiftOp = true;
2344 op = kOpLsl;
2345 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002346 case OP_SHR_INT_LIT8:
Bill Buzbee0e605272009-12-01 14:28:05 -08002347 lit &= 31;
Bill Buzbee1465db52009-09-23 17:17:35 -07002348 shiftOp = true;
2349 op = kOpAsr;
2350 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002351 case OP_USHR_INT_LIT8:
Bill Buzbee0e605272009-12-01 14:28:05 -08002352 lit &= 31;
Bill Buzbee1465db52009-09-23 17:17:35 -07002353 shiftOp = true;
2354 op = kOpLsr;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002355 break;
2356
2357 case OP_DIV_INT_LIT8:
2358 case OP_DIV_INT_LIT16:
Ben Chengba4fc8b2009-06-01 13:00:29 -07002359 case OP_REM_INT_LIT8:
2360 case OP_REM_INT_LIT16:
2361 if (lit == 0) {
2362 /* Let the interpreter deal with div by 0 */
2363 genInterpSingleStep(cUnit, mir);
2364 return false;
2365 }
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002366 if (handleEasyDivide(cUnit, dalvikOpcode, rlSrc, rlDest, lit)) {
Elliott Hughes672511b2010-04-26 17:40:13 -07002367 return false;
2368 }
Bill Buzbeec6f10662010-02-09 11:16:15 -08002369 dvmCompilerFlushAllRegs(cUnit); /* Everything to home location */
Bill Buzbee1465db52009-09-23 17:17:35 -07002370 loadValueDirectFixed(cUnit, rlSrc, r0);
Bill Buzbeec6f10662010-02-09 11:16:15 -08002371 dvmCompilerClobber(cUnit, r0);
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002372 if ((dalvikOpcode == OP_DIV_INT_LIT8) ||
2373 (dalvikOpcode == OP_DIV_INT_LIT16)) {
Ben Chengbd1326d2010-04-02 15:04:53 -07002374 LOAD_FUNC_ADDR(cUnit, r2, (int)__aeabi_idiv);
Bill Buzbee1465db52009-09-23 17:17:35 -07002375 isDiv = true;
2376 } else {
Ben Chengbd1326d2010-04-02 15:04:53 -07002377 LOAD_FUNC_ADDR(cUnit, r2, (int)__aeabi_idivmod);
Bill Buzbee1465db52009-09-23 17:17:35 -07002378 isDiv = false;
2379 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07002380 loadConstant(cUnit, r1, lit);
Bill Buzbee1465db52009-09-23 17:17:35 -07002381 opReg(cUnit, kOpBlx, r2);
Elliott Hughes6a555132010-02-25 15:41:42 -08002382 dvmCompilerClobberCallRegs(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07002383 if (isDiv)
Bill Buzbeec6f10662010-02-09 11:16:15 -08002384 rlResult = dvmCompilerGetReturn(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07002385 else
Bill Buzbeec6f10662010-02-09 11:16:15 -08002386 rlResult = dvmCompilerGetReturnAlt(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07002387 storeValue(cUnit, rlDest, rlResult);
2388 return false;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002389 break;
2390 default:
2391 return true;
2392 }
Bill Buzbee1465db52009-09-23 17:17:35 -07002393 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
Bill Buzbeec6f10662010-02-09 11:16:15 -08002394 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Bill Buzbee1465db52009-09-23 17:17:35 -07002395 // Avoid shifts by literal 0 - no support in Thumb. Change to copy
2396 if (shiftOp && (lit == 0)) {
2397 genRegCopy(cUnit, rlResult.lowReg, rlSrc.lowReg);
2398 } else {
2399 opRegRegImm(cUnit, op, rlResult.lowReg, rlSrc.lowReg, lit);
2400 }
2401 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002402 return false;
2403}
2404
jeffhao71eee1f2011-01-04 14:18:54 -08002405static bool handleFmt22c_Fmt52c(CompilationUnit *cUnit, MIR *mir)
Ben Chengba4fc8b2009-06-01 13:00:29 -07002406{
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002407 Opcode dalvikOpcode = mir->dalvikInsn.opcode;
buzbee4d92e682010-07-29 15:24:14 -07002408 int fieldOffset = -1;
buzbeeecf8f6e2010-07-20 14:53:42 -07002409 bool isVolatile = false;
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002410 switch (dalvikOpcode) {
buzbee4d92e682010-07-29 15:24:14 -07002411 /*
2412 * Wide volatiles currently handled via single step.
2413 * Add them here if generating in-line code.
2414 * case OP_IGET_WIDE_VOLATILE:
buzbeebd7865b2011-03-31 10:55:04 -07002415 * case OP_IGET_WIDE_VOLATILE_JUMBO:
buzbee4d92e682010-07-29 15:24:14 -07002416 * case OP_IPUT_WIDE_VOLATILE:
buzbeebd7865b2011-03-31 10:55:04 -07002417 * case OP_IPUT_WIDE_VOLATILE_JUMBO:
buzbee4d92e682010-07-29 15:24:14 -07002418 */
buzbee4d92e682010-07-29 15:24:14 -07002419 case OP_IGET_VOLATILE:
buzbeebd7865b2011-03-31 10:55:04 -07002420 case OP_IGET_VOLATILE_JUMBO:
buzbee8b94be12011-04-04 12:25:57 -07002421 case OP_IGET_OBJECT_VOLATILE:
2422 case OP_IGET_OBJECT_VOLATILE_JUMBO:
2423 case OP_IPUT_VOLATILE:
2424 case OP_IPUT_VOLATILE_JUMBO:
2425 case OP_IPUT_OBJECT_VOLATILE:
2426 case OP_IPUT_OBJECT_VOLATILE_JUMBO:
buzbeee3c0b542011-04-07 15:22:06 -07002427#if ANDROID_SMP != 0
buzbee8b94be12011-04-04 12:25:57 -07002428 isVolatile = true;
2429 // NOTE: intentional fallthrough
buzbeee3c0b542011-04-07 15:22:06 -07002430#endif
buzbee8b94be12011-04-04 12:25:57 -07002431 case OP_IGET:
jeffhao71eee1f2011-01-04 14:18:54 -08002432 case OP_IGET_JUMBO:
buzbee4d92e682010-07-29 15:24:14 -07002433 case OP_IGET_WIDE:
jeffhao71eee1f2011-01-04 14:18:54 -08002434 case OP_IGET_WIDE_JUMBO:
buzbee4d92e682010-07-29 15:24:14 -07002435 case OP_IGET_OBJECT:
jeffhao71eee1f2011-01-04 14:18:54 -08002436 case OP_IGET_OBJECT_JUMBO:
buzbee4d92e682010-07-29 15:24:14 -07002437 case OP_IGET_BOOLEAN:
jeffhao71eee1f2011-01-04 14:18:54 -08002438 case OP_IGET_BOOLEAN_JUMBO:
buzbee4d92e682010-07-29 15:24:14 -07002439 case OP_IGET_BYTE:
jeffhao71eee1f2011-01-04 14:18:54 -08002440 case OP_IGET_BYTE_JUMBO:
buzbee4d92e682010-07-29 15:24:14 -07002441 case OP_IGET_CHAR:
jeffhao71eee1f2011-01-04 14:18:54 -08002442 case OP_IGET_CHAR_JUMBO:
buzbee4d92e682010-07-29 15:24:14 -07002443 case OP_IGET_SHORT:
jeffhao71eee1f2011-01-04 14:18:54 -08002444 case OP_IGET_SHORT_JUMBO:
buzbee4d92e682010-07-29 15:24:14 -07002445 case OP_IPUT:
jeffhao71eee1f2011-01-04 14:18:54 -08002446 case OP_IPUT_JUMBO:
buzbee4d92e682010-07-29 15:24:14 -07002447 case OP_IPUT_WIDE:
jeffhao71eee1f2011-01-04 14:18:54 -08002448 case OP_IPUT_WIDE_JUMBO:
buzbee4d92e682010-07-29 15:24:14 -07002449 case OP_IPUT_OBJECT:
jeffhao71eee1f2011-01-04 14:18:54 -08002450 case OP_IPUT_OBJECT_JUMBO:
buzbee4d92e682010-07-29 15:24:14 -07002451 case OP_IPUT_BOOLEAN:
jeffhao71eee1f2011-01-04 14:18:54 -08002452 case OP_IPUT_BOOLEAN_JUMBO:
buzbee4d92e682010-07-29 15:24:14 -07002453 case OP_IPUT_BYTE:
jeffhao71eee1f2011-01-04 14:18:54 -08002454 case OP_IPUT_BYTE_JUMBO:
buzbee4d92e682010-07-29 15:24:14 -07002455 case OP_IPUT_CHAR:
jeffhao71eee1f2011-01-04 14:18:54 -08002456 case OP_IPUT_CHAR_JUMBO:
2457 case OP_IPUT_SHORT:
2458 case OP_IPUT_SHORT_JUMBO: {
Ben Cheng7a2697d2010-06-07 13:44:23 -07002459 const Method *method = (mir->OptimizationFlags & MIR_CALLEE) ?
2460 mir->meta.calleeMethod : cUnit->method;
buzbee4d92e682010-07-29 15:24:14 -07002461 Field *fieldPtr =
Ben Cheng7a2697d2010-06-07 13:44:23 -07002462 method->clazz->pDvmDex->pResFields[mir->dalvikInsn.vC];
Ben Chengba4fc8b2009-06-01 13:00:29 -07002463
buzbee4d92e682010-07-29 15:24:14 -07002464 if (fieldPtr == NULL) {
Ben Cheng32115a92011-03-22 14:09:09 -07002465 BAIL_LOOP_COMPILATION();
buzbee4d92e682010-07-29 15:24:14 -07002466 LOGE("Unexpected null instance field");
2467 dvmAbort();
2468 }
buzbeee3c0b542011-04-07 15:22:06 -07002469
2470#if ANDROID_SMP != 0
2471 assert(isVolatile == dvmIsVolatileField((Field *) fieldPtr));
2472#else
2473 isVolatile = dvmIsVolatileField((Field *) fieldPtr);
2474#endif
buzbee4d92e682010-07-29 15:24:14 -07002475 fieldOffset = ((InstField *)fieldPtr)->byteOffset;
2476 break;
Ben Chengdd6e8702010-05-07 13:05:47 -07002477 }
buzbee4d92e682010-07-29 15:24:14 -07002478 default:
2479 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002480 }
buzbee4d92e682010-07-29 15:24:14 -07002481
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002482 switch (dalvikOpcode) {
jeffhao71eee1f2011-01-04 14:18:54 -08002483 case OP_NEW_ARRAY:
2484 case OP_NEW_ARRAY_JUMBO: {
Bill Buzbee1465db52009-09-23 17:17:35 -07002485 // Generates a call - use explicit registers
Bill Buzbeec6f10662010-02-09 11:16:15 -08002486 RegLocation rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
2487 RegLocation rlDest = dvmCompilerGetDest(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07002488 RegLocation rlResult;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002489 void *classPtr = (void*)
2490 (cUnit->method->clazz->pDvmDex->pResClasses[mir->dalvikInsn.vC]);
Ben Chengdd6e8702010-05-07 13:05:47 -07002491
2492 if (classPtr == NULL) {
Ben Cheng32115a92011-03-22 14:09:09 -07002493 BAIL_LOOP_COMPILATION();
Ben Chengdd6e8702010-05-07 13:05:47 -07002494 LOGE("Unexpected null class");
2495 dvmAbort();
2496 }
2497
Bill Buzbeec6f10662010-02-09 11:16:15 -08002498 dvmCompilerFlushAllRegs(cUnit); /* Everything to home location */
Bill Buzbee1465db52009-09-23 17:17:35 -07002499 genExportPC(cUnit, mir);
2500 loadValueDirectFixed(cUnit, rlSrc, r1); /* Len */
Ben Chengba4fc8b2009-06-01 13:00:29 -07002501 loadConstant(cUnit, r0, (int) classPtr );
Ben Chengbd1326d2010-04-02 15:04:53 -07002502 LOAD_FUNC_ADDR(cUnit, r3, (int)dvmAllocArrayByClass);
Ben Cheng4f489172009-09-27 17:08:35 -07002503 /*
2504 * "len < 0": bail to the interpreter to re-execute the
2505 * instruction
2506 */
Carl Shapiroe3c01da2010-05-20 22:54:18 -07002507 genRegImmCheck(cUnit, kArmCondMi, r1, 0, mir->offset, NULL);
Bill Buzbee270c1d62009-08-13 16:58:07 -07002508 loadConstant(cUnit, r2, ALLOC_DONT_TRACK);
Bill Buzbee1465db52009-09-23 17:17:35 -07002509 opReg(cUnit, kOpBlx, r3);
Elliott Hughes6a555132010-02-25 15:41:42 -08002510 dvmCompilerClobberCallRegs(cUnit);
Ben Cheng4f489172009-09-27 17:08:35 -07002511 /* generate a branch over if allocation is successful */
buzbee8f8109a2010-08-31 10:16:35 -07002512 ArmLIR *branchOver = genCmpImmBranch(cUnit, kArmCondNe, r0, 0);
Ben Cheng4f489172009-09-27 17:08:35 -07002513 /*
2514 * OOM exception needs to be thrown here and cannot re-execute
2515 */
2516 loadConstant(cUnit, r0,
2517 (int) (cUnit->method->insns + mir->offset));
2518 genDispatchToHandler(cUnit, TEMPLATE_THROW_EXCEPTION_COMMON);
2519 /* noreturn */
2520
Bill Buzbee1465db52009-09-23 17:17:35 -07002521 ArmLIR *target = newLIR0(cUnit, kArmPseudoTargetLabel);
Ben Cheng4f489172009-09-27 17:08:35 -07002522 target->defMask = ENCODE_ALL;
2523 branchOver->generic.target = (LIR *) target;
Bill Buzbeec6f10662010-02-09 11:16:15 -08002524 rlResult = dvmCompilerGetReturn(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07002525 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002526 break;
2527 }
jeffhao71eee1f2011-01-04 14:18:54 -08002528 case OP_INSTANCE_OF:
2529 case OP_INSTANCE_OF_JUMBO: {
Bill Buzbee1465db52009-09-23 17:17:35 -07002530 // May generate a call - use explicit registers
Bill Buzbeec6f10662010-02-09 11:16:15 -08002531 RegLocation rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
2532 RegLocation rlDest = dvmCompilerGetDest(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07002533 RegLocation rlResult;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002534 ClassObject *classPtr =
2535 (cUnit->method->clazz->pDvmDex->pResClasses[mir->dalvikInsn.vC]);
Bill Buzbee480e6782010-01-27 15:43:08 -08002536 /*
2537 * Note: It is possible that classPtr is NULL at this point,
2538 * even though this instruction has been successfully interpreted.
2539 * If the previous interpretation had a null source, the
2540 * interpreter would not have bothered to resolve the clazz.
2541 * Bail out to the interpreter in this case, and log it
2542 * so that we can tell if it happens frequently.
2543 */
2544 if (classPtr == NULL) {
Ben Cheng32115a92011-03-22 14:09:09 -07002545 BAIL_LOOP_COMPILATION();
Bill Buzbee480e6782010-01-27 15:43:08 -08002546 LOGD("null clazz in OP_INSTANCE_OF, single-stepping");
2547 genInterpSingleStep(cUnit, mir);
2548 break;
2549 }
Bill Buzbeec6f10662010-02-09 11:16:15 -08002550 dvmCompilerFlushAllRegs(cUnit); /* Everything to home location */
Bill Buzbee1465db52009-09-23 17:17:35 -07002551 loadValueDirectFixed(cUnit, rlSrc, r0); /* Ref */
Ben Chengba4fc8b2009-06-01 13:00:29 -07002552 loadConstant(cUnit, r2, (int) classPtr );
Ben Cheng752c7942009-06-22 10:50:07 -07002553 /* When taken r0 has NULL which can be used for store directly */
buzbee8f8109a2010-08-31 10:16:35 -07002554 ArmLIR *branch1 = genCmpImmBranch(cUnit, kArmCondEq, r0, 0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002555 /* r1 now contains object->clazz */
Bill Buzbee270c1d62009-08-13 16:58:07 -07002556 loadWordDisp(cUnit, r0, offsetof(Object, clazz), r1);
Bill Buzbee1465db52009-09-23 17:17:35 -07002557 /* r1 now contains object->clazz */
Ben Chengbd1326d2010-04-02 15:04:53 -07002558 LOAD_FUNC_ADDR(cUnit, r3, (int)dvmInstanceofNonTrivial);
Ben Cheng752c7942009-06-22 10:50:07 -07002559 loadConstant(cUnit, r0, 1); /* Assume true */
Bill Buzbee1465db52009-09-23 17:17:35 -07002560 opRegReg(cUnit, kOpCmp, r1, r2);
2561 ArmLIR *branch2 = opCondBranch(cUnit, kArmCondEq);
2562 genRegCopy(cUnit, r0, r1);
2563 genRegCopy(cUnit, r1, r2);
2564 opReg(cUnit, kOpBlx, r3);
Elliott Hughes6a555132010-02-25 15:41:42 -08002565 dvmCompilerClobberCallRegs(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002566 /* branch target here */
Bill Buzbee1465db52009-09-23 17:17:35 -07002567 ArmLIR *target = newLIR0(cUnit, kArmPseudoTargetLabel);
Ben Chengd7d426a2009-09-22 11:23:36 -07002568 target->defMask = ENCODE_ALL;
Bill Buzbeec6f10662010-02-09 11:16:15 -08002569 rlResult = dvmCompilerGetReturn(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07002570 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002571 branch1->generic.target = (LIR *)target;
2572 branch2->generic.target = (LIR *)target;
2573 break;
2574 }
2575 case OP_IGET_WIDE:
jeffhao71eee1f2011-01-04 14:18:54 -08002576 case OP_IGET_WIDE_JUMBO:
Ben Chengba4fc8b2009-06-01 13:00:29 -07002577 genIGetWide(cUnit, mir, fieldOffset);
2578 break;
buzbeeecf8f6e2010-07-20 14:53:42 -07002579 case OP_IGET_VOLATILE:
buzbeebd7865b2011-03-31 10:55:04 -07002580 case OP_IGET_VOLATILE_JUMBO:
buzbeeecf8f6e2010-07-20 14:53:42 -07002581 case OP_IGET_OBJECT_VOLATILE:
buzbeebd7865b2011-03-31 10:55:04 -07002582 case OP_IGET_OBJECT_VOLATILE_JUMBO:
Ben Chengba4fc8b2009-06-01 13:00:29 -07002583 case OP_IGET:
jeffhao71eee1f2011-01-04 14:18:54 -08002584 case OP_IGET_JUMBO:
Ben Chengba4fc8b2009-06-01 13:00:29 -07002585 case OP_IGET_OBJECT:
jeffhao71eee1f2011-01-04 14:18:54 -08002586 case OP_IGET_OBJECT_JUMBO:
Ben Chengba4fc8b2009-06-01 13:00:29 -07002587 case OP_IGET_BOOLEAN:
jeffhao71eee1f2011-01-04 14:18:54 -08002588 case OP_IGET_BOOLEAN_JUMBO:
Ben Chengba4fc8b2009-06-01 13:00:29 -07002589 case OP_IGET_BYTE:
jeffhao71eee1f2011-01-04 14:18:54 -08002590 case OP_IGET_BYTE_JUMBO:
Ben Chengba4fc8b2009-06-01 13:00:29 -07002591 case OP_IGET_CHAR:
jeffhao71eee1f2011-01-04 14:18:54 -08002592 case OP_IGET_CHAR_JUMBO:
Ben Chengba4fc8b2009-06-01 13:00:29 -07002593 case OP_IGET_SHORT:
jeffhao71eee1f2011-01-04 14:18:54 -08002594 case OP_IGET_SHORT_JUMBO:
buzbee3272e2f2010-09-09 14:07:01 -07002595 genIGet(cUnit, mir, kWord, fieldOffset, isVolatile);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002596 break;
2597 case OP_IPUT_WIDE:
jeffhao71eee1f2011-01-04 14:18:54 -08002598 case OP_IPUT_WIDE_JUMBO:
Ben Chengba4fc8b2009-06-01 13:00:29 -07002599 genIPutWide(cUnit, mir, fieldOffset);
2600 break;
Carl Shapiro49f30642011-04-04 10:20:43 -07002601 case OP_IPUT_VOLATILE:
2602 case OP_IPUT_VOLATILE_JUMBO:
Ben Chengba4fc8b2009-06-01 13:00:29 -07002603 case OP_IPUT:
jeffhao71eee1f2011-01-04 14:18:54 -08002604 case OP_IPUT_JUMBO:
buzbee3272e2f2010-09-09 14:07:01 -07002605 case OP_IPUT_BOOLEAN:
jeffhao71eee1f2011-01-04 14:18:54 -08002606 case OP_IPUT_BOOLEAN_JUMBO:
2607 case OP_IPUT_BYTE:
2608 case OP_IPUT_BYTE_JUMBO:
2609 case OP_IPUT_CHAR:
2610 case OP_IPUT_CHAR_JUMBO:
2611 case OP_IPUT_SHORT:
2612 case OP_IPUT_SHORT_JUMBO:
buzbeeecf8f6e2010-07-20 14:53:42 -07002613 genIPut(cUnit, mir, kWord, fieldOffset, false, isVolatile);
buzbee919eb062010-07-12 12:59:22 -07002614 break;
buzbeeecf8f6e2010-07-20 14:53:42 -07002615 case OP_IPUT_OBJECT_VOLATILE:
buzbeebd7865b2011-03-31 10:55:04 -07002616 case OP_IPUT_OBJECT_VOLATILE_JUMBO:
Ben Chengba4fc8b2009-06-01 13:00:29 -07002617 case OP_IPUT_OBJECT:
jeffhao71eee1f2011-01-04 14:18:54 -08002618 case OP_IPUT_OBJECT_JUMBO:
buzbeeecf8f6e2010-07-20 14:53:42 -07002619 genIPut(cUnit, mir, kWord, fieldOffset, true, isVolatile);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002620 break;
Bill Buzbeeb16344a2010-03-15 17:19:12 -07002621 case OP_IGET_WIDE_VOLATILE:
buzbeebd7865b2011-03-31 10:55:04 -07002622 case OP_IGET_WIDE_VOLATILE_JUMBO:
Bill Buzbeeb16344a2010-03-15 17:19:12 -07002623 case OP_IPUT_WIDE_VOLATILE:
buzbeebd7865b2011-03-31 10:55:04 -07002624 case OP_IPUT_WIDE_VOLATILE_JUMBO:
Bill Buzbeeb16344a2010-03-15 17:19:12 -07002625 genInterpSingleStep(cUnit, mir);
2626 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002627 default:
2628 return true;
2629 }
2630 return false;
2631}
2632
2633static bool handleFmt22cs(CompilationUnit *cUnit, MIR *mir)
2634{
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002635 Opcode dalvikOpcode = mir->dalvikInsn.opcode;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002636 int fieldOffset = mir->dalvikInsn.vC;
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002637 switch (dalvikOpcode) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07002638 case OP_IGET_QUICK:
2639 case OP_IGET_OBJECT_QUICK:
buzbeeecf8f6e2010-07-20 14:53:42 -07002640 genIGet(cUnit, mir, kWord, fieldOffset, false);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002641 break;
2642 case OP_IPUT_QUICK:
buzbeeecf8f6e2010-07-20 14:53:42 -07002643 genIPut(cUnit, mir, kWord, fieldOffset, false, false);
buzbee919eb062010-07-12 12:59:22 -07002644 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002645 case OP_IPUT_OBJECT_QUICK:
buzbeeecf8f6e2010-07-20 14:53:42 -07002646 genIPut(cUnit, mir, kWord, fieldOffset, true, false);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002647 break;
2648 case OP_IGET_WIDE_QUICK:
2649 genIGetWide(cUnit, mir, fieldOffset);
2650 break;
2651 case OP_IPUT_WIDE_QUICK:
2652 genIPutWide(cUnit, mir, fieldOffset);
2653 break;
2654 default:
2655 return true;
2656 }
2657 return false;
2658
2659}
2660
2661/* Compare agaist zero */
2662static bool handleFmt22t(CompilationUnit *cUnit, MIR *mir, BasicBlock *bb,
Bill Buzbee89efc3d2009-07-28 11:22:22 -07002663 ArmLIR *labelList)
Ben Chengba4fc8b2009-06-01 13:00:29 -07002664{
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002665 Opcode dalvikOpcode = mir->dalvikInsn.opcode;
Bill Buzbee89efc3d2009-07-28 11:22:22 -07002666 ArmConditionCode cond;
Ben Cheng7ab74e12011-02-03 14:02:06 -08002667 /* backward branch? */
2668 bool backwardBranch = (bb->taken->startOffset <= mir->offset);
2669
Ben Cheng32115a92011-03-22 14:09:09 -07002670 if (backwardBranch &&
2671 (gDvmJit.genSuspendPoll || cUnit->jitMode == kJitLoop)) {
Ben Cheng7ab74e12011-02-03 14:02:06 -08002672 genSuspendPoll(cUnit, mir);
2673 }
2674
Bill Buzbeec6f10662010-02-09 11:16:15 -08002675 RegLocation rlSrc1 = dvmCompilerGetSrc(cUnit, mir, 0);
2676 RegLocation rlSrc2 = dvmCompilerGetSrc(cUnit, mir, 1);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002677
Bill Buzbee1465db52009-09-23 17:17:35 -07002678 rlSrc1 = loadValue(cUnit, rlSrc1, kCoreReg);
2679 rlSrc2 = loadValue(cUnit, rlSrc2, kCoreReg);
Ben Cheng7ab74e12011-02-03 14:02:06 -08002680
Bill Buzbee1465db52009-09-23 17:17:35 -07002681 opRegReg(cUnit, kOpCmp, rlSrc1.lowReg, rlSrc2.lowReg);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002682
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002683 switch (dalvikOpcode) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07002684 case OP_IF_EQ:
Bill Buzbee1465db52009-09-23 17:17:35 -07002685 cond = kArmCondEq;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002686 break;
2687 case OP_IF_NE:
Bill Buzbee1465db52009-09-23 17:17:35 -07002688 cond = kArmCondNe;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002689 break;
2690 case OP_IF_LT:
Bill Buzbee1465db52009-09-23 17:17:35 -07002691 cond = kArmCondLt;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002692 break;
2693 case OP_IF_GE:
Bill Buzbee1465db52009-09-23 17:17:35 -07002694 cond = kArmCondGe;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002695 break;
2696 case OP_IF_GT:
Bill Buzbee1465db52009-09-23 17:17:35 -07002697 cond = kArmCondGt;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002698 break;
2699 case OP_IF_LE:
Bill Buzbee1465db52009-09-23 17:17:35 -07002700 cond = kArmCondLe;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002701 break;
2702 default:
2703 cond = 0;
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002704 LOGE("Unexpected opcode (%d) for Fmt22t\n", dalvikOpcode);
Bill Buzbeefc519dc2010-03-06 23:30:57 -08002705 dvmCompilerAbort(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002706 }
2707 genConditionalBranch(cUnit, cond, &labelList[bb->taken->id]);
2708 /* This mostly likely will be optimized away in a later phase */
2709 genUnconditionalBranch(cUnit, &labelList[bb->fallThrough->id]);
2710 return false;
2711}
2712
2713static bool handleFmt22x_Fmt32x(CompilationUnit *cUnit, MIR *mir)
2714{
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002715 Opcode opcode = mir->dalvikInsn.opcode;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002716
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002717 switch (opcode) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07002718 case OP_MOVE_16:
2719 case OP_MOVE_OBJECT_16:
2720 case OP_MOVE_FROM16:
Ben Chenge9695e52009-06-16 16:11:47 -07002721 case OP_MOVE_OBJECT_FROM16: {
Bill Buzbeec6f10662010-02-09 11:16:15 -08002722 storeValue(cUnit, dvmCompilerGetDest(cUnit, mir, 0),
2723 dvmCompilerGetSrc(cUnit, mir, 0));
Ben Chengba4fc8b2009-06-01 13:00:29 -07002724 break;
Ben Chenge9695e52009-06-16 16:11:47 -07002725 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07002726 case OP_MOVE_WIDE_16:
Ben Chenge9695e52009-06-16 16:11:47 -07002727 case OP_MOVE_WIDE_FROM16: {
Bill Buzbeec6f10662010-02-09 11:16:15 -08002728 storeValueWide(cUnit, dvmCompilerGetDestWide(cUnit, mir, 0, 1),
2729 dvmCompilerGetSrcWide(cUnit, mir, 0, 1));
Ben Chengba4fc8b2009-06-01 13:00:29 -07002730 break;
Ben Chenge9695e52009-06-16 16:11:47 -07002731 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07002732 default:
2733 return true;
2734 }
2735 return false;
2736}
2737
2738static bool handleFmt23x(CompilationUnit *cUnit, MIR *mir)
2739{
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002740 Opcode opcode = mir->dalvikInsn.opcode;
Bill Buzbee1465db52009-09-23 17:17:35 -07002741 RegLocation rlSrc1;
2742 RegLocation rlSrc2;
2743 RegLocation rlDest;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002744
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002745 if ( (opcode >= OP_ADD_INT) && (opcode <= OP_REM_DOUBLE)) {
Ben Cheng5d90c202009-11-22 23:31:11 -08002746 return genArithOp( cUnit, mir );
Ben Chengba4fc8b2009-06-01 13:00:29 -07002747 }
2748
Bill Buzbee1465db52009-09-23 17:17:35 -07002749 /* APUTs have 3 sources and no targets */
2750 if (mir->ssaRep->numDefs == 0) {
2751 if (mir->ssaRep->numUses == 3) {
Bill Buzbeec6f10662010-02-09 11:16:15 -08002752 rlDest = dvmCompilerGetSrc(cUnit, mir, 0);
2753 rlSrc1 = dvmCompilerGetSrc(cUnit, mir, 1);
2754 rlSrc2 = dvmCompilerGetSrc(cUnit, mir, 2);
Bill Buzbee1465db52009-09-23 17:17:35 -07002755 } else {
2756 assert(mir->ssaRep->numUses == 4);
Bill Buzbeec6f10662010-02-09 11:16:15 -08002757 rlDest = dvmCompilerGetSrcWide(cUnit, mir, 0, 1);
2758 rlSrc1 = dvmCompilerGetSrc(cUnit, mir, 2);
2759 rlSrc2 = dvmCompilerGetSrc(cUnit, mir, 3);
Bill Buzbee1465db52009-09-23 17:17:35 -07002760 }
2761 } else {
2762 /* Two sources and 1 dest. Deduce the operand sizes */
2763 if (mir->ssaRep->numUses == 4) {
Bill Buzbeec6f10662010-02-09 11:16:15 -08002764 rlSrc1 = dvmCompilerGetSrcWide(cUnit, mir, 0, 1);
2765 rlSrc2 = dvmCompilerGetSrcWide(cUnit, mir, 2, 3);
Bill Buzbee1465db52009-09-23 17:17:35 -07002766 } else {
2767 assert(mir->ssaRep->numUses == 2);
Bill Buzbeec6f10662010-02-09 11:16:15 -08002768 rlSrc1 = dvmCompilerGetSrc(cUnit, mir, 0);
2769 rlSrc2 = dvmCompilerGetSrc(cUnit, mir, 1);
Bill Buzbee1465db52009-09-23 17:17:35 -07002770 }
2771 if (mir->ssaRep->numDefs == 2) {
Bill Buzbeec6f10662010-02-09 11:16:15 -08002772 rlDest = dvmCompilerGetDestWide(cUnit, mir, 0, 1);
Bill Buzbee1465db52009-09-23 17:17:35 -07002773 } else {
2774 assert(mir->ssaRep->numDefs == 1);
Bill Buzbeec6f10662010-02-09 11:16:15 -08002775 rlDest = dvmCompilerGetDest(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07002776 }
2777 }
2778
2779
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002780 switch (opcode) {
Bill Buzbeed45ba372009-06-15 17:00:57 -07002781 case OP_CMPL_FLOAT:
2782 case OP_CMPG_FLOAT:
2783 case OP_CMPL_DOUBLE:
2784 case OP_CMPG_DOUBLE:
Ben Cheng5d90c202009-11-22 23:31:11 -08002785 return genCmpFP(cUnit, mir, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002786 case OP_CMP_LONG:
Bill Buzbee1465db52009-09-23 17:17:35 -07002787 genCmpLong(cUnit, mir, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002788 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002789 case OP_AGET_WIDE:
Bill Buzbee1465db52009-09-23 17:17:35 -07002790 genArrayGet(cUnit, mir, kLong, rlSrc1, rlSrc2, rlDest, 3);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002791 break;
2792 case OP_AGET:
2793 case OP_AGET_OBJECT:
Bill Buzbee1465db52009-09-23 17:17:35 -07002794 genArrayGet(cUnit, mir, kWord, rlSrc1, rlSrc2, rlDest, 2);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002795 break;
2796 case OP_AGET_BOOLEAN:
Bill Buzbee1465db52009-09-23 17:17:35 -07002797 genArrayGet(cUnit, mir, kUnsignedByte, rlSrc1, rlSrc2, rlDest, 0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002798 break;
2799 case OP_AGET_BYTE:
Bill Buzbee1465db52009-09-23 17:17:35 -07002800 genArrayGet(cUnit, mir, kSignedByte, rlSrc1, rlSrc2, rlDest, 0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002801 break;
2802 case OP_AGET_CHAR:
Bill Buzbee1465db52009-09-23 17:17:35 -07002803 genArrayGet(cUnit, mir, kUnsignedHalf, rlSrc1, rlSrc2, rlDest, 1);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002804 break;
2805 case OP_AGET_SHORT:
Bill Buzbee1465db52009-09-23 17:17:35 -07002806 genArrayGet(cUnit, mir, kSignedHalf, rlSrc1, rlSrc2, rlDest, 1);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002807 break;
2808 case OP_APUT_WIDE:
Bill Buzbee1465db52009-09-23 17:17:35 -07002809 genArrayPut(cUnit, mir, kLong, rlSrc1, rlSrc2, rlDest, 3);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002810 break;
2811 case OP_APUT:
Bill Buzbee1465db52009-09-23 17:17:35 -07002812 genArrayPut(cUnit, mir, kWord, rlSrc1, rlSrc2, rlDest, 2);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002813 break;
Bill Buzbeebe6534f2010-03-12 16:01:35 -08002814 case OP_APUT_OBJECT:
2815 genArrayObjectPut(cUnit, mir, rlSrc1, rlSrc2, rlDest, 2);
2816 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002817 case OP_APUT_SHORT:
2818 case OP_APUT_CHAR:
Bill Buzbee1465db52009-09-23 17:17:35 -07002819 genArrayPut(cUnit, mir, kUnsignedHalf, rlSrc1, rlSrc2, rlDest, 1);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002820 break;
2821 case OP_APUT_BYTE:
2822 case OP_APUT_BOOLEAN:
Bill Buzbee1465db52009-09-23 17:17:35 -07002823 genArrayPut(cUnit, mir, kUnsignedByte, rlSrc1, rlSrc2, rlDest, 0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002824 break;
2825 default:
2826 return true;
2827 }
2828 return false;
2829}
2830
Ben Cheng6c10a972009-10-29 14:39:18 -07002831/*
2832 * Find the matching case.
2833 *
2834 * return values:
2835 * r0 (low 32-bit): pc of the chaining cell corresponding to the resolved case,
2836 * including default which is placed at MIN(size, MAX_CHAINED_SWITCH_CASES).
2837 * r1 (high 32-bit): the branch offset of the matching case (only for indexes
2838 * above MAX_CHAINED_SWITCH_CASES).
2839 *
2840 * Instructions around the call are:
2841 *
2842 * mov r2, pc
2843 * blx &findPackedSwitchIndex
2844 * mov pc, r0
2845 * .align4
Bill Buzbeebd047242010-05-13 13:02:53 -07002846 * chaining cell for case 0 [12 bytes]
2847 * chaining cell for case 1 [12 bytes]
Ben Cheng6c10a972009-10-29 14:39:18 -07002848 * :
Bill Buzbeebd047242010-05-13 13:02:53 -07002849 * chaining cell for case MIN(size, MAX_CHAINED_SWITCH_CASES)-1 [12 bytes]
Ben Cheng6c10a972009-10-29 14:39:18 -07002850 * chaining cell for case default [8 bytes]
2851 * noChain exit
2852 */
Ben Chengbd1326d2010-04-02 15:04:53 -07002853static s8 findPackedSwitchIndex(const u2* switchData, int testVal, int pc)
Ben Cheng6c10a972009-10-29 14:39:18 -07002854{
2855 int size;
2856 int firstKey;
2857 const int *entries;
2858 int index;
2859 int jumpIndex;
2860 int caseDPCOffset = 0;
2861 /* In Thumb mode pc is 4 ahead of the "mov r2, pc" instruction */
2862 int chainingPC = (pc + 4) & ~3;
2863
2864 /*
2865 * Packed switch data format:
2866 * ushort ident = 0x0100 magic value
2867 * ushort size number of entries in the table
2868 * int first_key first (and lowest) switch case value
2869 * int targets[size] branch targets, relative to switch opcode
2870 *
2871 * Total size is (4+size*2) 16-bit code units.
2872 */
2873 size = switchData[1];
2874 assert(size > 0);
2875
2876 firstKey = switchData[2];
2877 firstKey |= switchData[3] << 16;
2878
2879
2880 /* The entries are guaranteed to be aligned on a 32-bit boundary;
2881 * we can treat them as a native int array.
2882 */
2883 entries = (const int*) &switchData[4];
2884 assert(((u4)entries & 0x3) == 0);
2885
2886 index = testVal - firstKey;
2887
2888 /* Jump to the default cell */
2889 if (index < 0 || index >= size) {
2890 jumpIndex = MIN(size, MAX_CHAINED_SWITCH_CASES);
2891 /* Jump to the non-chaining exit point */
2892 } else if (index >= MAX_CHAINED_SWITCH_CASES) {
2893 jumpIndex = MAX_CHAINED_SWITCH_CASES + 1;
2894 caseDPCOffset = entries[index];
2895 /* Jump to the inline chaining cell */
2896 } else {
2897 jumpIndex = index;
2898 }
2899
Bill Buzbeebd047242010-05-13 13:02:53 -07002900 chainingPC += jumpIndex * CHAIN_CELL_NORMAL_SIZE;
Ben Cheng6c10a972009-10-29 14:39:18 -07002901 return (((s8) caseDPCOffset) << 32) | (u8) chainingPC;
2902}
2903
2904/* See comments for findPackedSwitchIndex */
Ben Chengbd1326d2010-04-02 15:04:53 -07002905static s8 findSparseSwitchIndex(const u2* switchData, int testVal, int pc)
Ben Cheng6c10a972009-10-29 14:39:18 -07002906{
2907 int size;
2908 const int *keys;
2909 const int *entries;
2910 int chainingPC = (pc + 4) & ~3;
2911 int i;
2912
2913 /*
2914 * Sparse switch data format:
2915 * ushort ident = 0x0200 magic value
2916 * ushort size number of entries in the table; > 0
2917 * int keys[size] keys, sorted low-to-high; 32-bit aligned
2918 * int targets[size] branch targets, relative to switch opcode
2919 *
2920 * Total size is (2+size*4) 16-bit code units.
2921 */
2922
2923 size = switchData[1];
2924 assert(size > 0);
2925
2926 /* The keys are guaranteed to be aligned on a 32-bit boundary;
2927 * we can treat them as a native int array.
2928 */
2929 keys = (const int*) &switchData[2];
2930 assert(((u4)keys & 0x3) == 0);
2931
2932 /* The entries are guaranteed to be aligned on a 32-bit boundary;
2933 * we can treat them as a native int array.
2934 */
2935 entries = keys + size;
2936 assert(((u4)entries & 0x3) == 0);
2937
2938 /*
2939 * Run through the list of keys, which are guaranteed to
2940 * be sorted low-to-high.
2941 *
2942 * Most tables have 3-4 entries. Few have more than 10. A binary
2943 * search here is probably not useful.
2944 */
2945 for (i = 0; i < size; i++) {
2946 int k = keys[i];
2947 if (k == testVal) {
2948 /* MAX_CHAINED_SWITCH_CASES + 1 is the start of the overflow case */
2949 int jumpIndex = (i < MAX_CHAINED_SWITCH_CASES) ?
2950 i : MAX_CHAINED_SWITCH_CASES + 1;
Bill Buzbeebd047242010-05-13 13:02:53 -07002951 chainingPC += jumpIndex * CHAIN_CELL_NORMAL_SIZE;
Ben Cheng6c10a972009-10-29 14:39:18 -07002952 return (((s8) entries[i]) << 32) | (u8) chainingPC;
2953 } else if (k > testVal) {
2954 break;
2955 }
2956 }
Bill Buzbeebd047242010-05-13 13:02:53 -07002957 return chainingPC + MIN(size, MAX_CHAINED_SWITCH_CASES) *
2958 CHAIN_CELL_NORMAL_SIZE;
Ben Cheng6c10a972009-10-29 14:39:18 -07002959}
2960
Ben Chengba4fc8b2009-06-01 13:00:29 -07002961static bool handleFmt31t(CompilationUnit *cUnit, MIR *mir)
2962{
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002963 Opcode dalvikOpcode = mir->dalvikInsn.opcode;
2964 switch (dalvikOpcode) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07002965 case OP_FILL_ARRAY_DATA: {
Bill Buzbeec6f10662010-02-09 11:16:15 -08002966 RegLocation rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07002967 // Making a call - use explicit registers
Bill Buzbeec6f10662010-02-09 11:16:15 -08002968 dvmCompilerFlushAllRegs(cUnit); /* Everything to home location */
Bill Buzbee1465db52009-09-23 17:17:35 -07002969 genExportPC(cUnit, mir);
2970 loadValueDirectFixed(cUnit, rlSrc, r0);
Ben Chengbd1326d2010-04-02 15:04:53 -07002971 LOAD_FUNC_ADDR(cUnit, r2, (int)dvmInterpHandleFillArrayData);
Ben Cheng6c10a972009-10-29 14:39:18 -07002972 loadConstant(cUnit, r1,
2973 (int) (cUnit->method->insns + mir->offset + mir->dalvikInsn.vB));
Bill Buzbee1465db52009-09-23 17:17:35 -07002974 opReg(cUnit, kOpBlx, r2);
Elliott Hughes6a555132010-02-25 15:41:42 -08002975 dvmCompilerClobberCallRegs(cUnit);
Bill Buzbeefd023aa2009-11-02 09:23:49 -08002976 /* generate a branch over if successful */
buzbee8f8109a2010-08-31 10:16:35 -07002977 ArmLIR *branchOver = genCmpImmBranch(cUnit, kArmCondNe, r0, 0);
Bill Buzbeefd023aa2009-11-02 09:23:49 -08002978 loadConstant(cUnit, r0,
2979 (int) (cUnit->method->insns + mir->offset));
2980 genDispatchToHandler(cUnit, TEMPLATE_THROW_EXCEPTION_COMMON);
2981 ArmLIR *target = newLIR0(cUnit, kArmPseudoTargetLabel);
2982 target->defMask = ENCODE_ALL;
2983 branchOver->generic.target = (LIR *) target;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002984 break;
2985 }
2986 /*
Ben Cheng6c10a972009-10-29 14:39:18 -07002987 * Compute the goto target of up to
2988 * MIN(switchSize, MAX_CHAINED_SWITCH_CASES) + 1 chaining cells.
2989 * See the comment before findPackedSwitchIndex for the code layout.
Ben Chengba4fc8b2009-06-01 13:00:29 -07002990 */
2991 case OP_PACKED_SWITCH:
2992 case OP_SPARSE_SWITCH: {
Bill Buzbeec6f10662010-02-09 11:16:15 -08002993 RegLocation rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
2994 dvmCompilerFlushAllRegs(cUnit); /* Everything to home location */
Bill Buzbee1465db52009-09-23 17:17:35 -07002995 loadValueDirectFixed(cUnit, rlSrc, r1);
Bill Buzbeec6f10662010-02-09 11:16:15 -08002996 dvmCompilerLockAllTemps(cUnit);
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002997 if (dalvikOpcode == OP_PACKED_SWITCH) {
Ben Chengbd1326d2010-04-02 15:04:53 -07002998 LOAD_FUNC_ADDR(cUnit, r4PC, (int)findPackedSwitchIndex);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002999 } else {
Ben Chengbd1326d2010-04-02 15:04:53 -07003000 LOAD_FUNC_ADDR(cUnit, r4PC, (int)findSparseSwitchIndex);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003001 }
Ben Cheng6c10a972009-10-29 14:39:18 -07003002 /* r0 <- Addr of the switch data */
3003 loadConstant(cUnit, r0,
3004 (int) (cUnit->method->insns + mir->offset + mir->dalvikInsn.vB));
3005 /* r2 <- pc of the instruction following the blx */
Ben Cheng20d7e6c2011-02-18 17:12:42 -08003006 opRegReg(cUnit, kOpMov, r2, r15pc);
Bill Buzbee1465db52009-09-23 17:17:35 -07003007 opReg(cUnit, kOpBlx, r4PC);
Elliott Hughes6a555132010-02-25 15:41:42 -08003008 dvmCompilerClobberCallRegs(cUnit);
Ben Cheng6c10a972009-10-29 14:39:18 -07003009 /* pc <- computed goto target */
Ben Cheng20d7e6c2011-02-18 17:12:42 -08003010 opRegReg(cUnit, kOpMov, r15pc, r0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003011 break;
3012 }
3013 default:
3014 return true;
3015 }
3016 return false;
3017}
3018
Ben Cheng7a2697d2010-06-07 13:44:23 -07003019/*
3020 * See the example of predicted inlining listed before the
3021 * genValidationForPredictedInline function. The function here takes care the
3022 * branch over at 0x4858de78 and the misprediction target at 0x4858de7a.
3023 */
3024static void genLandingPadForMispredictedCallee(CompilationUnit *cUnit, MIR *mir,
3025 BasicBlock *bb,
3026 ArmLIR *labelList)
3027{
3028 BasicBlock *fallThrough = bb->fallThrough;
3029
3030 /* Bypass the move-result block if there is one */
3031 if (fallThrough->firstMIRInsn) {
3032 assert(fallThrough->firstMIRInsn->OptimizationFlags & MIR_INLINED_PRED);
3033 fallThrough = fallThrough->fallThrough;
3034 }
3035 /* Generate a branch over if the predicted inlining is correct */
3036 genUnconditionalBranch(cUnit, &labelList[fallThrough->id]);
3037
3038 /* Reset the register state */
3039 dvmCompilerResetRegPool(cUnit);
3040 dvmCompilerClobberAllRegs(cUnit);
3041 dvmCompilerResetNullCheck(cUnit);
3042
3043 /* Target for the slow invoke path */
3044 ArmLIR *target = newLIR0(cUnit, kArmPseudoTargetLabel);
3045 target->defMask = ENCODE_ALL;
3046 /* Hook up the target to the verification branch */
3047 mir->meta.callsiteInfo->misPredBranchOver->target = (LIR *) target;
3048}
3049
jeffhao71eee1f2011-01-04 14:18:54 -08003050static bool handleFmt35c_3rc_5rc(CompilationUnit *cUnit, MIR *mir,
3051 BasicBlock *bb, ArmLIR *labelList)
Ben Chengba4fc8b2009-06-01 13:00:29 -07003052{
Bill Buzbee9bc3df32009-07-30 10:52:29 -07003053 ArmLIR *retChainingCell = NULL;
Bill Buzbee89efc3d2009-07-28 11:22:22 -07003054 ArmLIR *pcrLabel = NULL;
Ben Chengba4fc8b2009-06-01 13:00:29 -07003055
Ben Cheng7a2697d2010-06-07 13:44:23 -07003056 /* An invoke with the MIR_INLINED is effectively a no-op */
3057 if (mir->OptimizationFlags & MIR_INLINED)
3058 return false;
3059
Bill Buzbeef4ce16f2009-07-28 13:28:25 -07003060 if (bb->fallThrough != NULL)
3061 retChainingCell = &labelList[bb->fallThrough->id];
3062
Ben Chengba4fc8b2009-06-01 13:00:29 -07003063 DecodedInstruction *dInsn = &mir->dalvikInsn;
Dan Bornstein9a1f8162010-12-01 17:02:26 -08003064 switch (mir->dalvikInsn.opcode) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07003065 /*
3066 * calleeMethod = this->clazz->vtable[
3067 * method->clazz->pDvmDex->pResMethods[BBBB]->methodIndex
3068 * ]
3069 */
3070 case OP_INVOKE_VIRTUAL:
jeffhao71eee1f2011-01-04 14:18:54 -08003071 case OP_INVOKE_VIRTUAL_RANGE:
3072 case OP_INVOKE_VIRTUAL_JUMBO: {
Bill Buzbee89efc3d2009-07-28 11:22:22 -07003073 ArmLIR *predChainingCell = &labelList[bb->taken->id];
Ben Chengba4fc8b2009-06-01 13:00:29 -07003074 int methodIndex =
3075 cUnit->method->clazz->pDvmDex->pResMethods[dInsn->vB]->
3076 methodIndex;
3077
Ben Cheng7a2697d2010-06-07 13:44:23 -07003078 /*
3079 * If the invoke has non-null misPredBranchOver, we need to generate
3080 * the non-inlined version of the invoke here to handle the
3081 * mispredicted case.
3082 */
3083 if (mir->meta.callsiteInfo->misPredBranchOver) {
3084 genLandingPadForMispredictedCallee(cUnit, mir, bb, labelList);
3085 }
3086
Dan Bornstein9a1f8162010-12-01 17:02:26 -08003087 if (mir->dalvikInsn.opcode == OP_INVOKE_VIRTUAL)
Ben Chengba4fc8b2009-06-01 13:00:29 -07003088 genProcessArgsNoRange(cUnit, mir, dInsn, &pcrLabel);
3089 else
3090 genProcessArgsRange(cUnit, mir, dInsn, &pcrLabel);
3091
Ben Cheng38329f52009-07-07 14:19:20 -07003092 genInvokeVirtualCommon(cUnit, mir, methodIndex,
3093 retChainingCell,
3094 predChainingCell,
3095 pcrLabel);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003096 break;
3097 }
3098 /*
3099 * calleeMethod = method->clazz->super->vtable[method->clazz->pDvmDex
3100 * ->pResMethods[BBBB]->methodIndex]
3101 */
Ben Chengba4fc8b2009-06-01 13:00:29 -07003102 case OP_INVOKE_SUPER:
jeffhao71eee1f2011-01-04 14:18:54 -08003103 case OP_INVOKE_SUPER_RANGE:
3104 case OP_INVOKE_SUPER_JUMBO: {
Ben Cheng7a2697d2010-06-07 13:44:23 -07003105 /* Grab the method ptr directly from what the interpreter sees */
3106 const Method *calleeMethod = mir->meta.callsiteInfo->method;
3107 assert(calleeMethod == cUnit->method->clazz->super->vtable[
3108 cUnit->method->clazz->pDvmDex->
3109 pResMethods[dInsn->vB]->methodIndex]);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003110
Dan Bornstein9a1f8162010-12-01 17:02:26 -08003111 if (mir->dalvikInsn.opcode == OP_INVOKE_SUPER)
Ben Chengba4fc8b2009-06-01 13:00:29 -07003112 genProcessArgsNoRange(cUnit, mir, dInsn, &pcrLabel);
3113 else
3114 genProcessArgsRange(cUnit, mir, dInsn, &pcrLabel);
3115
Ben Cheng20d7e6c2011-02-18 17:12:42 -08003116 if (mir->OptimizationFlags & MIR_INVOKE_METHOD_JIT) {
3117 const Method *calleeMethod = mir->meta.callsiteInfo->method;
3118 void *calleeAddr = dvmJitGetMethodAddr(calleeMethod->insns);
3119 assert(calleeAddr);
3120 genInvokeSingletonWholeMethod(cUnit, mir, calleeAddr,
3121 retChainingCell);
3122 } else {
3123 /* r0 = calleeMethod */
3124 loadConstant(cUnit, r0, (int) calleeMethod);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003125
Ben Cheng20d7e6c2011-02-18 17:12:42 -08003126 genInvokeSingletonCommon(cUnit, mir, bb, labelList, pcrLabel,
3127 calleeMethod);
3128 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07003129 break;
3130 }
3131 /* calleeMethod = method->clazz->pDvmDex->pResMethods[BBBB] */
3132 case OP_INVOKE_DIRECT:
jeffhao71eee1f2011-01-04 14:18:54 -08003133 case OP_INVOKE_DIRECT_RANGE:
3134 case OP_INVOKE_DIRECT_JUMBO: {
Ben Cheng7a2697d2010-06-07 13:44:23 -07003135 /* Grab the method ptr directly from what the interpreter sees */
3136 const Method *calleeMethod = mir->meta.callsiteInfo->method;
3137 assert(calleeMethod ==
3138 cUnit->method->clazz->pDvmDex->pResMethods[dInsn->vB]);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003139
Dan Bornstein9a1f8162010-12-01 17:02:26 -08003140 if (mir->dalvikInsn.opcode == OP_INVOKE_DIRECT)
Ben Chengba4fc8b2009-06-01 13:00:29 -07003141 genProcessArgsNoRange(cUnit, mir, dInsn, &pcrLabel);
3142 else
3143 genProcessArgsRange(cUnit, mir, dInsn, &pcrLabel);
3144
3145 /* r0 = calleeMethod */
3146 loadConstant(cUnit, r0, (int) calleeMethod);
3147
Ben Cheng38329f52009-07-07 14:19:20 -07003148 genInvokeSingletonCommon(cUnit, mir, bb, labelList, pcrLabel,
3149 calleeMethod);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003150 break;
3151 }
3152 /* calleeMethod = method->clazz->pDvmDex->pResMethods[BBBB] */
3153 case OP_INVOKE_STATIC:
jeffhao71eee1f2011-01-04 14:18:54 -08003154 case OP_INVOKE_STATIC_RANGE:
3155 case OP_INVOKE_STATIC_JUMBO: {
Ben Cheng7a2697d2010-06-07 13:44:23 -07003156 /* Grab the method ptr directly from what the interpreter sees */
3157 const Method *calleeMethod = mir->meta.callsiteInfo->method;
3158 assert(calleeMethod ==
3159 cUnit->method->clazz->pDvmDex->pResMethods[dInsn->vB]);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003160
Dan Bornstein9a1f8162010-12-01 17:02:26 -08003161 if (mir->dalvikInsn.opcode == OP_INVOKE_STATIC)
Ben Chengba4fc8b2009-06-01 13:00:29 -07003162 genProcessArgsNoRange(cUnit, mir, dInsn,
3163 NULL /* no null check */);
3164 else
3165 genProcessArgsRange(cUnit, mir, dInsn,
3166 NULL /* no null check */);
3167
Ben Cheng20d7e6c2011-02-18 17:12:42 -08003168 if (mir->OptimizationFlags & MIR_INVOKE_METHOD_JIT) {
3169 const Method *calleeMethod = mir->meta.callsiteInfo->method;
3170 void *calleeAddr = dvmJitGetMethodAddr(calleeMethod->insns);
3171 assert(calleeAddr);
3172 genInvokeSingletonWholeMethod(cUnit, mir, calleeAddr,
3173 retChainingCell);
3174 } else {
3175 /* r0 = calleeMethod */
3176 loadConstant(cUnit, r0, (int) calleeMethod);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003177
Ben Cheng20d7e6c2011-02-18 17:12:42 -08003178 genInvokeSingletonCommon(cUnit, mir, bb, labelList, pcrLabel,
3179 calleeMethod);
3180 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07003181 break;
3182 }
Ben Cheng09e50c92010-05-02 10:45:32 -07003183 /*
Ben Chengba4fc8b2009-06-01 13:00:29 -07003184 * calleeMethod = dvmFindInterfaceMethodInCache(this->clazz,
3185 * BBBB, method, method->clazz->pDvmDex)
Ben Cheng38329f52009-07-07 14:19:20 -07003186 *
Ben Cheng09e50c92010-05-02 10:45:32 -07003187 * The following is an example of generated code for
3188 * "invoke-interface v0"
Ben Cheng38329f52009-07-07 14:19:20 -07003189 *
Ben Cheng09e50c92010-05-02 10:45:32 -07003190 * -------- dalvik offset: 0x0008 @ invoke-interface v0
3191 * 0x47357e36 : ldr r0, [r5, #0] --+
3192 * 0x47357e38 : sub r7,r5,#24 |
3193 * 0x47357e3c : cmp r0, #0 | genProcessArgsNoRange
3194 * 0x47357e3e : beq 0x47357e82 |
3195 * 0x47357e40 : stmia r7, <r0> --+
3196 * 0x47357e42 : ldr r4, [pc, #120] --> r4 <- dalvikPC of this invoke
3197 * 0x47357e44 : add r1, pc, #64 --> r1 <- &retChainingCell
3198 * 0x47357e46 : add r2, pc, #72 --> r2 <- &predictedChainingCell
3199 * 0x47357e48 : blx_1 0x47348190 --+ TEMPLATE_INVOKE_METHOD_
3200 * 0x47357e4a : blx_2 see above --+ PREDICTED_CHAIN
3201 * 0x47357e4c : b 0x47357e90 --> off to the predicted chain
3202 * 0x47357e4e : b 0x47357e82 --> punt to the interpreter
3203 * 0x47357e50 : mov r8, r1 --+
3204 * 0x47357e52 : mov r9, r2 |
3205 * 0x47357e54 : ldr r2, [pc, #96] |
3206 * 0x47357e56 : mov r10, r3 |
3207 * 0x47357e58 : movs r0, r3 | dvmFindInterfaceMethodInCache
3208 * 0x47357e5a : ldr r3, [pc, #88] |
3209 * 0x47357e5c : ldr r7, [pc, #80] |
3210 * 0x47357e5e : mov r1, #1452 |
3211 * 0x47357e62 : blx r7 --+
3212 * 0x47357e64 : cmp r0, #0 --> calleeMethod == NULL?
3213 * 0x47357e66 : bne 0x47357e6e --> branch over the throw if !r0
3214 * 0x47357e68 : ldr r0, [pc, #80] --> load Dalvik PC of the invoke
3215 * 0x47357e6a : blx_1 0x47348494 --+ TEMPLATE_THROW_EXCEPTION_
3216 * 0x47357e6c : blx_2 see above --+ COMMON
3217 * 0x47357e6e : mov r1, r8 --> r1 <- &retChainingCell
3218 * 0x47357e70 : cmp r1, #0 --> compare against 0
3219 * 0x47357e72 : bgt 0x47357e7c --> >=0? don't rechain
Ben Chengaf5aa1f2011-01-04 15:37:04 -08003220 * 0x47357e74 : ldr r7, [pc, #off] --+
Ben Cheng09e50c92010-05-02 10:45:32 -07003221 * 0x47357e76 : mov r2, r9 | dvmJitToPatchPredictedChain
3222 * 0x47357e78 : mov r3, r10 |
3223 * 0x47357e7a : blx r7 --+
3224 * 0x47357e7c : add r1, pc, #8 --> r1 <- &retChainingCell
3225 * 0x47357e7e : blx_1 0x4734809c --+ TEMPLATE_INVOKE_METHOD_NO_OPT
3226 * 0x47357e80 : blx_2 see above --+
3227 * -------- reconstruct dalvik PC : 0x425719dc @ +0x0008
3228 * 0x47357e82 : ldr r0, [pc, #56]
Ben Cheng38329f52009-07-07 14:19:20 -07003229 * Exception_Handling:
Ben Cheng09e50c92010-05-02 10:45:32 -07003230 * 0x47357e84 : ldr r1, [r6, #92]
3231 * 0x47357e86 : blx r1
3232 * 0x47357e88 : .align4
3233 * -------- chaining cell (hot): 0x000b
3234 * 0x47357e88 : ldr r0, [r6, #104]
3235 * 0x47357e8a : blx r0
3236 * 0x47357e8c : data 0x19e2(6626)
3237 * 0x47357e8e : data 0x4257(16983)
3238 * 0x47357e90 : .align4
Ben Cheng38329f52009-07-07 14:19:20 -07003239 * -------- chaining cell (predicted)
Ben Cheng09e50c92010-05-02 10:45:32 -07003240 * 0x47357e90 : data 0xe7fe(59390) --> will be patched into bx
3241 * 0x47357e92 : data 0x0000(0)
3242 * 0x47357e94 : data 0x0000(0) --> class
3243 * 0x47357e96 : data 0x0000(0)
3244 * 0x47357e98 : data 0x0000(0) --> method
3245 * 0x47357e9a : data 0x0000(0)
3246 * 0x47357e9c : data 0x0000(0) --> rechain count
3247 * 0x47357e9e : data 0x0000(0)
3248 * -------- end of chaining cells (0x006c)
3249 * 0x47357eb0 : .word (0xad03e369)
3250 * 0x47357eb4 : .word (0x28a90)
3251 * 0x47357eb8 : .word (0x41a63394)
3252 * 0x47357ebc : .word (0x425719dc)
Ben Chengba4fc8b2009-06-01 13:00:29 -07003253 */
3254 case OP_INVOKE_INTERFACE:
jeffhao71eee1f2011-01-04 14:18:54 -08003255 case OP_INVOKE_INTERFACE_RANGE:
3256 case OP_INVOKE_INTERFACE_JUMBO: {
Bill Buzbee89efc3d2009-07-28 11:22:22 -07003257 ArmLIR *predChainingCell = &labelList[bb->taken->id];
Ben Chengba4fc8b2009-06-01 13:00:29 -07003258
Ben Cheng7a2697d2010-06-07 13:44:23 -07003259 /*
3260 * If the invoke has non-null misPredBranchOver, we need to generate
3261 * the non-inlined version of the invoke here to handle the
3262 * mispredicted case.
3263 */
3264 if (mir->meta.callsiteInfo->misPredBranchOver) {
3265 genLandingPadForMispredictedCallee(cUnit, mir, bb, labelList);
3266 }
Bill Buzbee1465db52009-09-23 17:17:35 -07003267
Dan Bornstein9a1f8162010-12-01 17:02:26 -08003268 if (mir->dalvikInsn.opcode == OP_INVOKE_INTERFACE)
Ben Chengba4fc8b2009-06-01 13:00:29 -07003269 genProcessArgsNoRange(cUnit, mir, dInsn, &pcrLabel);
3270 else
3271 genProcessArgsRange(cUnit, mir, dInsn, &pcrLabel);
3272
Ben Cheng38329f52009-07-07 14:19:20 -07003273 /* "this" is already left in r0 by genProcessArgs* */
3274
3275 /* r4PC = dalvikCallsite */
3276 loadConstant(cUnit, r4PC,
3277 (int) (cUnit->method->insns + mir->offset));
3278
3279 /* r1 = &retChainingCell */
Bill Buzbee270c1d62009-08-13 16:58:07 -07003280 ArmLIR *addrRetChain =
Ben Cheng20d7e6c2011-02-18 17:12:42 -08003281 opRegRegImm(cUnit, kOpAdd, r1, r15pc, 0);
Ben Cheng38329f52009-07-07 14:19:20 -07003282 addrRetChain->generic.target = (LIR *) retChainingCell;
3283
3284 /* r2 = &predictedChainingCell */
Bill Buzbee89efc3d2009-07-28 11:22:22 -07003285 ArmLIR *predictedChainingCell =
Ben Cheng20d7e6c2011-02-18 17:12:42 -08003286 opRegRegImm(cUnit, kOpAdd, r2, r15pc, 0);
Ben Cheng38329f52009-07-07 14:19:20 -07003287 predictedChainingCell->generic.target = (LIR *) predChainingCell;
3288
buzbee18fba342011-01-19 15:31:15 -08003289 genDispatchToHandler(cUnit, gDvmJit.methodTraceSupport ?
3290 TEMPLATE_INVOKE_METHOD_PREDICTED_CHAIN_PROF :
3291 TEMPLATE_INVOKE_METHOD_PREDICTED_CHAIN);
Ben Cheng38329f52009-07-07 14:19:20 -07003292
3293 /* return through lr - jump to the chaining cell */
3294 genUnconditionalBranch(cUnit, predChainingCell);
3295
3296 /*
3297 * null-check on "this" may have been eliminated, but we still need
3298 * a PC-reconstruction label for stack overflow bailout.
3299 */
3300 if (pcrLabel == NULL) {
3301 int dPC = (int) (cUnit->method->insns + mir->offset);
Carl Shapirofc75f3e2010-12-07 11:43:38 -08003302 pcrLabel = (ArmLIR *) dvmCompilerNew(sizeof(ArmLIR), true);
Dan Bornstein9a1f8162010-12-01 17:02:26 -08003303 pcrLabel->opcode = kArmPseudoPCReconstructionCell;
Ben Cheng38329f52009-07-07 14:19:20 -07003304 pcrLabel->operands[0] = dPC;
3305 pcrLabel->operands[1] = mir->offset;
3306 /* Insert the place holder to the growable list */
Ben Cheng00603072010-10-28 11:13:58 -07003307 dvmInsertGrowableList(&cUnit->pcReconstructionList,
3308 (intptr_t) pcrLabel);
Ben Cheng38329f52009-07-07 14:19:20 -07003309 }
3310
3311 /* return through lr+2 - punt to the interpreter */
3312 genUnconditionalBranch(cUnit, pcrLabel);
3313
3314 /*
3315 * return through lr+4 - fully resolve the callee method.
3316 * r1 <- count
3317 * r2 <- &predictedChainCell
3318 * r3 <- this->class
3319 * r4 <- dPC
3320 * r7 <- this->class->vtable
3321 */
3322
3323 /* Save count, &predictedChainCell, and class to high regs first */
Bill Buzbee1465db52009-09-23 17:17:35 -07003324 genRegCopy(cUnit, r8, r1);
3325 genRegCopy(cUnit, r9, r2);
3326 genRegCopy(cUnit, r10, r3);
Ben Cheng38329f52009-07-07 14:19:20 -07003327
Ben Chengba4fc8b2009-06-01 13:00:29 -07003328 /* r0 now contains this->clazz */
Bill Buzbee1465db52009-09-23 17:17:35 -07003329 genRegCopy(cUnit, r0, r3);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003330
3331 /* r1 = BBBB */
3332 loadConstant(cUnit, r1, dInsn->vB);
3333
3334 /* r2 = method (caller) */
3335 loadConstant(cUnit, r2, (int) cUnit->method);
3336
3337 /* r3 = pDvmDex */
3338 loadConstant(cUnit, r3, (int) cUnit->method->clazz->pDvmDex);
3339
Ben Chengbd1326d2010-04-02 15:04:53 -07003340 LOAD_FUNC_ADDR(cUnit, r7,
3341 (intptr_t) dvmFindInterfaceMethodInCache);
Bill Buzbee1465db52009-09-23 17:17:35 -07003342 opReg(cUnit, kOpBlx, r7);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003343 /* r0 = calleeMethod (returned from dvmFindInterfaceMethodInCache */
3344
Ben Cheng09e50c92010-05-02 10:45:32 -07003345 dvmCompilerClobberCallRegs(cUnit);
3346 /* generate a branch over if the interface method is resolved */
buzbee8f8109a2010-08-31 10:16:35 -07003347 ArmLIR *branchOver = genCmpImmBranch(cUnit, kArmCondNe, r0, 0);
Ben Cheng09e50c92010-05-02 10:45:32 -07003348 /*
3349 * calleeMethod == NULL -> throw
3350 */
3351 loadConstant(cUnit, r0,
3352 (int) (cUnit->method->insns + mir->offset));
3353 genDispatchToHandler(cUnit, TEMPLATE_THROW_EXCEPTION_COMMON);
3354 /* noreturn */
3355
3356 ArmLIR *target = newLIR0(cUnit, kArmPseudoTargetLabel);
3357 target->defMask = ENCODE_ALL;
3358 branchOver->generic.target = (LIR *) target;
3359
Bill Buzbee1465db52009-09-23 17:17:35 -07003360 genRegCopy(cUnit, r1, r8);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003361
Ben Cheng38329f52009-07-07 14:19:20 -07003362 /* Check if rechain limit is reached */
buzbee8f8109a2010-08-31 10:16:35 -07003363 ArmLIR *bypassRechaining = genCmpImmBranch(cUnit, kArmCondGt,
3364 r1, 0);
Ben Cheng38329f52009-07-07 14:19:20 -07003365
Ben Chengaf5aa1f2011-01-04 15:37:04 -08003366 LOAD_FUNC_ADDR(cUnit, r7, (int) dvmJitToPatchPredictedChain);
Ben Cheng38329f52009-07-07 14:19:20 -07003367
Ben Cheng20d7e6c2011-02-18 17:12:42 -08003368 genRegCopy(cUnit, r1, r6SELF);
Bill Buzbee1465db52009-09-23 17:17:35 -07003369 genRegCopy(cUnit, r2, r9);
3370 genRegCopy(cUnit, r3, r10);
Ben Cheng38329f52009-07-07 14:19:20 -07003371
3372 /*
3373 * r0 = calleeMethod
3374 * r2 = &predictedChainingCell
3375 * r3 = class
3376 *
3377 * &returnChainingCell has been loaded into r1 but is not needed
3378 * when patching the chaining cell and will be clobbered upon
3379 * returning so it will be reconstructed again.
3380 */
Bill Buzbee1465db52009-09-23 17:17:35 -07003381 opReg(cUnit, kOpBlx, r7);
Ben Cheng38329f52009-07-07 14:19:20 -07003382
3383 /* r1 = &retChainingCell */
Ben Cheng20d7e6c2011-02-18 17:12:42 -08003384 addrRetChain = opRegRegImm(cUnit, kOpAdd, r1, r15pc, 0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003385 addrRetChain->generic.target = (LIR *) retChainingCell;
Ben Cheng38329f52009-07-07 14:19:20 -07003386
3387 bypassRechaining->generic.target = (LIR *) addrRetChain;
3388
Ben Chengba4fc8b2009-06-01 13:00:29 -07003389 /*
3390 * r0 = this, r1 = calleeMethod,
3391 * r1 = &ChainingCell,
3392 * r4PC = callsiteDPC,
3393 */
buzbee18fba342011-01-19 15:31:15 -08003394 genDispatchToHandler(cUnit, gDvmJit.methodTraceSupport ?
3395 TEMPLATE_INVOKE_METHOD_NO_OPT_PROF :
3396 TEMPLATE_INVOKE_METHOD_NO_OPT);
Ben Cheng978738d2010-05-13 13:45:57 -07003397#if defined(WITH_JIT_TUNING)
Ben Cheng86717f72010-03-05 15:27:21 -08003398 gDvmJit.invokePolymorphic++;
Ben Chengba4fc8b2009-06-01 13:00:29 -07003399#endif
3400 /* Handle exceptions using the interpreter */
3401 genTrap(cUnit, mir->offset, pcrLabel);
3402 break;
3403 }
buzbeebd7865b2011-03-31 10:55:04 -07003404 case OP_INVOKE_OBJECT_INIT_JUMBO:
3405 case OP_INVOKE_OBJECT_INIT_RANGE:
Ben Chengba4fc8b2009-06-01 13:00:29 -07003406 case OP_FILLED_NEW_ARRAY:
jeffhao71eee1f2011-01-04 14:18:54 -08003407 case OP_FILLED_NEW_ARRAY_RANGE:
3408 case OP_FILLED_NEW_ARRAY_JUMBO: {
Ben Chengba4fc8b2009-06-01 13:00:29 -07003409 /* Just let the interpreter deal with these */
3410 genInterpSingleStep(cUnit, mir);
3411 break;
3412 }
3413 default:
3414 return true;
3415 }
3416 return false;
3417}
3418
3419static bool handleFmt35ms_3rms(CompilationUnit *cUnit, MIR *mir,
Bill Buzbee89efc3d2009-07-28 11:22:22 -07003420 BasicBlock *bb, ArmLIR *labelList)
Ben Chengba4fc8b2009-06-01 13:00:29 -07003421{
Bill Buzbee89efc3d2009-07-28 11:22:22 -07003422 ArmLIR *pcrLabel = NULL;
Ben Chengba4fc8b2009-06-01 13:00:29 -07003423
Ben Cheng7a2697d2010-06-07 13:44:23 -07003424 /* An invoke with the MIR_INLINED is effectively a no-op */
3425 if (mir->OptimizationFlags & MIR_INLINED)
3426 return false;
3427
Ben Chengba4fc8b2009-06-01 13:00:29 -07003428 DecodedInstruction *dInsn = &mir->dalvikInsn;
Dan Bornstein9a1f8162010-12-01 17:02:26 -08003429 switch (mir->dalvikInsn.opcode) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07003430 /* calleeMethod = this->clazz->vtable[BBBB] */
3431 case OP_INVOKE_VIRTUAL_QUICK_RANGE:
3432 case OP_INVOKE_VIRTUAL_QUICK: {
3433 int methodIndex = dInsn->vB;
Bill Buzbeea8589332010-12-27 09:31:21 -08003434 ArmLIR *retChainingCell = &labelList[bb->fallThrough->id];
3435 ArmLIR *predChainingCell = &labelList[bb->taken->id];
Ben Cheng7a2697d2010-06-07 13:44:23 -07003436
3437 /*
3438 * If the invoke has non-null misPredBranchOver, we need to generate
3439 * the non-inlined version of the invoke here to handle the
3440 * mispredicted case.
3441 */
3442 if (mir->meta.callsiteInfo->misPredBranchOver) {
3443 genLandingPadForMispredictedCallee(cUnit, mir, bb, labelList);
3444 }
3445
Dan Bornstein9a1f8162010-12-01 17:02:26 -08003446 if (mir->dalvikInsn.opcode == OP_INVOKE_VIRTUAL_QUICK)
Ben Chengba4fc8b2009-06-01 13:00:29 -07003447 genProcessArgsNoRange(cUnit, mir, dInsn, &pcrLabel);
3448 else
3449 genProcessArgsRange(cUnit, mir, dInsn, &pcrLabel);
3450
Ben Chengcfdeca32011-01-14 11:36:46 -08003451
3452 if (mir->OptimizationFlags & MIR_INVOKE_METHOD_JIT) {
3453 const Method *calleeMethod = mir->meta.callsiteInfo->method;
3454 void *calleeAddr = dvmJitGetMethodAddr(calleeMethod->insns);
Ben Cheng20d7e6c2011-02-18 17:12:42 -08003455 assert(calleeAddr);
3456 genInvokeVirtualWholeMethod(cUnit, mir, calleeAddr,
3457 retChainingCell);
Ben Chengcfdeca32011-01-14 11:36:46 -08003458 }
3459
Ben Cheng38329f52009-07-07 14:19:20 -07003460 genInvokeVirtualCommon(cUnit, mir, methodIndex,
3461 retChainingCell,
3462 predChainingCell,
3463 pcrLabel);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003464 break;
3465 }
3466 /* calleeMethod = method->clazz->super->vtable[BBBB] */
3467 case OP_INVOKE_SUPER_QUICK:
3468 case OP_INVOKE_SUPER_QUICK_RANGE: {
Ben Cheng7a2697d2010-06-07 13:44:23 -07003469 /* Grab the method ptr directly from what the interpreter sees */
3470 const Method *calleeMethod = mir->meta.callsiteInfo->method;
3471 assert(calleeMethod ==
3472 cUnit->method->clazz->super->vtable[dInsn->vB]);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003473
Dan Bornstein9a1f8162010-12-01 17:02:26 -08003474 if (mir->dalvikInsn.opcode == OP_INVOKE_SUPER_QUICK)
Ben Chengba4fc8b2009-06-01 13:00:29 -07003475 genProcessArgsNoRange(cUnit, mir, dInsn, &pcrLabel);
3476 else
3477 genProcessArgsRange(cUnit, mir, dInsn, &pcrLabel);
3478
3479 /* r0 = calleeMethod */
3480 loadConstant(cUnit, r0, (int) calleeMethod);
3481
Ben Cheng38329f52009-07-07 14:19:20 -07003482 genInvokeSingletonCommon(cUnit, mir, bb, labelList, pcrLabel,
3483 calleeMethod);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003484 break;
3485 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07003486 default:
3487 return true;
3488 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07003489 return false;
3490}
3491
3492/*
Bill Buzbeefd023aa2009-11-02 09:23:49 -08003493 * This operation is complex enough that we'll do it partly inline
3494 * and partly with a handler. NOTE: the handler uses hardcoded
3495 * values for string object offsets and must be revisitied if the
3496 * layout changes.
3497 */
3498static bool genInlinedCompareTo(CompilationUnit *cUnit, MIR *mir)
3499{
3500#if defined(USE_GLOBAL_STRING_DEFS)
Elliott Hughes7e914f12011-01-19 18:18:42 -08003501 return handleExecuteInlineC(cUnit, mir);
Bill Buzbeefd023aa2009-11-02 09:23:49 -08003502#else
3503 ArmLIR *rollback;
Bill Buzbeec6f10662010-02-09 11:16:15 -08003504 RegLocation rlThis = dvmCompilerGetSrc(cUnit, mir, 0);
3505 RegLocation rlComp = dvmCompilerGetSrc(cUnit, mir, 1);
Bill Buzbeefd023aa2009-11-02 09:23:49 -08003506
3507 loadValueDirectFixed(cUnit, rlThis, r0);
3508 loadValueDirectFixed(cUnit, rlComp, r1);
3509 /* Test objects for NULL */
3510 rollback = genNullCheck(cUnit, rlThis.sRegLow, r0, mir->offset, NULL);
3511 genNullCheck(cUnit, rlComp.sRegLow, r1, mir->offset, rollback);
3512 /*
3513 * TUNING: we could check for object pointer equality before invoking
3514 * handler. Unclear whether the gain would be worth the added code size
3515 * expansion.
3516 */
3517 genDispatchToHandler(cUnit, TEMPLATE_STRING_COMPARETO);
Bill Buzbeec6f10662010-02-09 11:16:15 -08003518 storeValue(cUnit, inlinedTarget(cUnit, mir, false),
3519 dvmCompilerGetReturn(cUnit));
Elliott Hughes7e914f12011-01-19 18:18:42 -08003520 return false;
Bill Buzbeefd023aa2009-11-02 09:23:49 -08003521#endif
3522}
3523
Elliott Hughes2bdbcb62010-04-12 14:29:37 -07003524static bool genInlinedFastIndexOf(CompilationUnit *cUnit, MIR *mir)
Bill Buzbeefd023aa2009-11-02 09:23:49 -08003525{
3526#if defined(USE_GLOBAL_STRING_DEFS)
Elliott Hughes7e914f12011-01-19 18:18:42 -08003527 return handleExecuteInlineC(cUnit, mir);
Bill Buzbeefd023aa2009-11-02 09:23:49 -08003528#else
Bill Buzbeec6f10662010-02-09 11:16:15 -08003529 RegLocation rlThis = dvmCompilerGetSrc(cUnit, mir, 0);
3530 RegLocation rlChar = dvmCompilerGetSrc(cUnit, mir, 1);
Bill Buzbeefd023aa2009-11-02 09:23:49 -08003531
3532 loadValueDirectFixed(cUnit, rlThis, r0);
3533 loadValueDirectFixed(cUnit, rlChar, r1);
Elliott Hughes2bdbcb62010-04-12 14:29:37 -07003534 RegLocation rlStart = dvmCompilerGetSrc(cUnit, mir, 2);
3535 loadValueDirectFixed(cUnit, rlStart, r2);
Bill Buzbeefd023aa2009-11-02 09:23:49 -08003536 /* Test objects for NULL */
3537 genNullCheck(cUnit, rlThis.sRegLow, r0, mir->offset, NULL);
3538 genDispatchToHandler(cUnit, TEMPLATE_STRING_INDEXOF);
Bill Buzbeec6f10662010-02-09 11:16:15 -08003539 storeValue(cUnit, inlinedTarget(cUnit, mir, false),
3540 dvmCompilerGetReturn(cUnit));
Elliott Hughes7e914f12011-01-19 18:18:42 -08003541 return false;
Bill Buzbeefd023aa2009-11-02 09:23:49 -08003542#endif
3543}
3544
Elliott Hughesee34f592010-04-05 18:13:52 -07003545// Generates an inlined String.isEmpty or String.length.
3546static bool genInlinedStringIsEmptyOrLength(CompilationUnit *cUnit, MIR *mir,
3547 bool isEmpty)
Bill Buzbee1f748632010-03-02 16:14:41 -08003548{
Elliott Hughesee34f592010-04-05 18:13:52 -07003549 // dst = src.length();
Bill Buzbee1f748632010-03-02 16:14:41 -08003550 RegLocation rlObj = dvmCompilerGetSrc(cUnit, mir, 0);
3551 RegLocation rlDest = inlinedTarget(cUnit, mir, false);
3552 rlObj = loadValue(cUnit, rlObj, kCoreReg);
3553 RegLocation rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
3554 genNullCheck(cUnit, rlObj.sRegLow, rlObj.lowReg, mir->offset, NULL);
3555 loadWordDisp(cUnit, rlObj.lowReg, gDvm.offJavaLangString_count,
3556 rlResult.lowReg);
Elliott Hughesee34f592010-04-05 18:13:52 -07003557 if (isEmpty) {
3558 // dst = (dst == 0);
3559 int tReg = dvmCompilerAllocTemp(cUnit);
3560 opRegReg(cUnit, kOpNeg, tReg, rlResult.lowReg);
3561 opRegRegReg(cUnit, kOpAdc, rlResult.lowReg, rlResult.lowReg, tReg);
3562 }
Bill Buzbee1f748632010-03-02 16:14:41 -08003563 storeValue(cUnit, rlDest, rlResult);
3564 return false;
3565}
3566
Elliott Hughesee34f592010-04-05 18:13:52 -07003567static bool genInlinedStringLength(CompilationUnit *cUnit, MIR *mir)
3568{
3569 return genInlinedStringIsEmptyOrLength(cUnit, mir, false);
3570}
3571
3572static bool genInlinedStringIsEmpty(CompilationUnit *cUnit, MIR *mir)
3573{
3574 return genInlinedStringIsEmptyOrLength(cUnit, mir, true);
3575}
3576
Bill Buzbee1f748632010-03-02 16:14:41 -08003577static bool genInlinedStringCharAt(CompilationUnit *cUnit, MIR *mir)
3578{
3579 int contents = offsetof(ArrayObject, contents);
3580 RegLocation rlObj = dvmCompilerGetSrc(cUnit, mir, 0);
3581 RegLocation rlIdx = dvmCompilerGetSrc(cUnit, mir, 1);
3582 RegLocation rlDest = inlinedTarget(cUnit, mir, false);
3583 RegLocation rlResult;
3584 rlObj = loadValue(cUnit, rlObj, kCoreReg);
3585 rlIdx = loadValue(cUnit, rlIdx, kCoreReg);
3586 int regMax = dvmCompilerAllocTemp(cUnit);
3587 int regOff = dvmCompilerAllocTemp(cUnit);
3588 int regPtr = dvmCompilerAllocTemp(cUnit);
3589 ArmLIR *pcrLabel = genNullCheck(cUnit, rlObj.sRegLow, rlObj.lowReg,
3590 mir->offset, NULL);
3591 loadWordDisp(cUnit, rlObj.lowReg, gDvm.offJavaLangString_count, regMax);
3592 loadWordDisp(cUnit, rlObj.lowReg, gDvm.offJavaLangString_offset, regOff);
3593 loadWordDisp(cUnit, rlObj.lowReg, gDvm.offJavaLangString_value, regPtr);
3594 genBoundsCheck(cUnit, rlIdx.lowReg, regMax, mir->offset, pcrLabel);
3595 dvmCompilerFreeTemp(cUnit, regMax);
3596 opRegImm(cUnit, kOpAdd, regPtr, contents);
3597 opRegReg(cUnit, kOpAdd, regOff, rlIdx.lowReg);
3598 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
3599 loadBaseIndexed(cUnit, regPtr, regOff, rlResult.lowReg, 1, kUnsignedHalf);
3600 storeValue(cUnit, rlDest, rlResult);
3601 return false;
3602}
3603
3604static bool genInlinedAbsInt(CompilationUnit *cUnit, MIR *mir)
3605{
3606 RegLocation rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
3607 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
Elliott Hughese22bd842010-08-20 18:47:36 -07003608 RegLocation rlDest = inlinedTarget(cUnit, mir, false);
Bill Buzbee1f748632010-03-02 16:14:41 -08003609 RegLocation rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
3610 int signReg = dvmCompilerAllocTemp(cUnit);
3611 /*
3612 * abs(x) = y<=x>>31, (x+y)^y.
3613 * Thumb2's IT block also yields 3 instructions, but imposes
3614 * scheduling constraints.
3615 */
3616 opRegRegImm(cUnit, kOpAsr, signReg, rlSrc.lowReg, 31);
3617 opRegRegReg(cUnit, kOpAdd, rlResult.lowReg, rlSrc.lowReg, signReg);
3618 opRegReg(cUnit, kOpXor, rlResult.lowReg, signReg);
3619 storeValue(cUnit, rlDest, rlResult);
3620 return false;
3621}
3622
3623static bool genInlinedAbsLong(CompilationUnit *cUnit, MIR *mir)
3624{
3625 RegLocation rlSrc = dvmCompilerGetSrcWide(cUnit, mir, 0, 1);
3626 RegLocation rlDest = inlinedTargetWide(cUnit, mir, false);
3627 rlSrc = loadValueWide(cUnit, rlSrc, kCoreReg);
3628 RegLocation rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
3629 int signReg = dvmCompilerAllocTemp(cUnit);
3630 /*
3631 * abs(x) = y<=x>>31, (x+y)^y.
3632 * Thumb2 IT block allows slightly shorter sequence,
3633 * but introduces a scheduling barrier. Stick with this
3634 * mechanism for now.
3635 */
3636 opRegRegImm(cUnit, kOpAsr, signReg, rlSrc.highReg, 31);
3637 opRegRegReg(cUnit, kOpAdd, rlResult.lowReg, rlSrc.lowReg, signReg);
3638 opRegRegReg(cUnit, kOpAdc, rlResult.highReg, rlSrc.highReg, signReg);
3639 opRegReg(cUnit, kOpXor, rlResult.lowReg, signReg);
3640 opRegReg(cUnit, kOpXor, rlResult.highReg, signReg);
3641 storeValueWide(cUnit, rlDest, rlResult);
3642 return false;
3643}
Bill Buzbeefd023aa2009-11-02 09:23:49 -08003644
Elliott Hughese22bd842010-08-20 18:47:36 -07003645static bool genInlinedIntFloatConversion(CompilationUnit *cUnit, MIR *mir)
3646{
3647 // Just move from source to destination...
3648 RegLocation rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
3649 RegLocation rlDest = inlinedTarget(cUnit, mir, false);
3650 storeValue(cUnit, rlDest, rlSrc);
3651 return false;
3652}
3653
3654static bool genInlinedLongDoubleConversion(CompilationUnit *cUnit, MIR *mir)
3655{
3656 // Just move from source to destination...
3657 RegLocation rlSrc = dvmCompilerGetSrcWide(cUnit, mir, 0, 1);
3658 RegLocation rlDest = inlinedTargetWide(cUnit, mir, false);
3659 storeValueWide(cUnit, rlDest, rlSrc);
3660 return false;
3661}
3662
Bill Buzbeefd023aa2009-11-02 09:23:49 -08003663/*
Elliott Hughes7e914f12011-01-19 18:18:42 -08003664 * JITs a call to a C function.
3665 * TODO: use this for faster native method invocation for simple native
3666 * methods (http://b/3069458).
3667 */
3668static bool handleExecuteInlineC(CompilationUnit *cUnit, MIR *mir)
3669{
3670 DecodedInstruction *dInsn = &mir->dalvikInsn;
3671 int operation = dInsn->vB;
3672 unsigned int i;
3673 const InlineOperation* inLineTable = dvmGetInlineOpsTable();
3674 uintptr_t fn = (int) inLineTable[operation].func;
3675 if (fn == 0) {
3676 dvmCompilerAbort(cUnit);
3677 }
3678 dvmCompilerFlushAllRegs(cUnit); /* Everything to home location */
3679 dvmCompilerClobberCallRegs(cUnit);
3680 dvmCompilerClobber(cUnit, r4PC);
3681 dvmCompilerClobber(cUnit, r7);
buzbee9f601a92011-02-11 17:48:20 -08003682 int offset = offsetof(Thread, retval);
Ben Cheng20d7e6c2011-02-18 17:12:42 -08003683 opRegRegImm(cUnit, kOpAdd, r4PC, r6SELF, offset);
Elliott Hughes7e914f12011-01-19 18:18:42 -08003684 opImm(cUnit, kOpPush, (1<<r4PC) | (1<<r7));
3685 LOAD_FUNC_ADDR(cUnit, r4PC, fn);
3686 genExportPC(cUnit, mir);
3687 for (i=0; i < dInsn->vA; i++) {
3688 loadValueDirect(cUnit, dvmCompilerGetSrc(cUnit, mir, i), i);
3689 }
3690 opReg(cUnit, kOpBlx, r4PC);
Ben Cheng20d7e6c2011-02-18 17:12:42 -08003691 opRegImm(cUnit, kOpAdd, r13sp, 8);
Elliott Hughes7e914f12011-01-19 18:18:42 -08003692 /* NULL? */
3693 ArmLIR *branchOver = genCmpImmBranch(cUnit, kArmCondNe, r0, 0);
3694 loadConstant(cUnit, r0, (int) (cUnit->method->insns + mir->offset));
3695 genDispatchToHandler(cUnit, TEMPLATE_THROW_EXCEPTION_COMMON);
3696 ArmLIR *target = newLIR0(cUnit, kArmPseudoTargetLabel);
3697 target->defMask = ENCODE_ALL;
3698 branchOver->generic.target = (LIR *) target;
3699 return false;
3700}
3701
3702/*
Bill Buzbeece46c942009-11-20 15:41:34 -08003703 * NOTE: Handles both range and non-range versions (arguments
3704 * have already been normalized by this point).
Ben Chengba4fc8b2009-06-01 13:00:29 -07003705 */
Bill Buzbeece46c942009-11-20 15:41:34 -08003706static bool handleExecuteInline(CompilationUnit *cUnit, MIR *mir)
Ben Chengba4fc8b2009-06-01 13:00:29 -07003707{
3708 DecodedInstruction *dInsn = &mir->dalvikInsn;
Elliott Hughes7e914f12011-01-19 18:18:42 -08003709 assert(dInsn->opcode == OP_EXECUTE_INLINE_RANGE ||
3710 dInsn->opcode == OP_EXECUTE_INLINE);
3711 switch (dInsn->vB) {
3712 case INLINE_EMPTYINLINEMETHOD:
3713 return false; /* Nop */
3714
3715 /* These ones we potentially JIT inline. */
3716 case INLINE_STRING_LENGTH:
3717 return genInlinedStringLength(cUnit, mir);
3718 case INLINE_STRING_IS_EMPTY:
3719 return genInlinedStringIsEmpty(cUnit, mir);
3720 case INLINE_MATH_ABS_INT:
3721 return genInlinedAbsInt(cUnit, mir);
3722 case INLINE_MATH_ABS_LONG:
3723 return genInlinedAbsLong(cUnit, mir);
3724 case INLINE_MATH_MIN_INT:
3725 return genInlinedMinMaxInt(cUnit, mir, true);
3726 case INLINE_MATH_MAX_INT:
3727 return genInlinedMinMaxInt(cUnit, mir, false);
3728 case INLINE_STRING_CHARAT:
3729 return genInlinedStringCharAt(cUnit, mir);
3730 case INLINE_MATH_SQRT:
3731 return genInlineSqrt(cUnit, mir);
3732 case INLINE_MATH_ABS_FLOAT:
3733 return genInlinedAbsFloat(cUnit, mir);
3734 case INLINE_MATH_ABS_DOUBLE:
3735 return genInlinedAbsDouble(cUnit, mir);
3736 case INLINE_STRING_COMPARETO:
3737 return genInlinedCompareTo(cUnit, mir);
3738 case INLINE_STRING_FASTINDEXOF_II:
3739 return genInlinedFastIndexOf(cUnit, mir);
3740 case INLINE_FLOAT_TO_RAW_INT_BITS:
3741 case INLINE_INT_BITS_TO_FLOAT:
3742 return genInlinedIntFloatConversion(cUnit, mir);
3743 case INLINE_DOUBLE_TO_RAW_LONG_BITS:
3744 case INLINE_LONG_BITS_TO_DOUBLE:
3745 return genInlinedLongDoubleConversion(cUnit, mir);
3746
3747 /*
3748 * These ones we just JIT a call to a C function for.
3749 * TODO: special-case these in the other "invoke" call paths.
3750 */
3751 case INLINE_STRING_EQUALS:
3752 case INLINE_MATH_COS:
3753 case INLINE_MATH_SIN:
3754 case INLINE_FLOAT_TO_INT_BITS:
3755 case INLINE_DOUBLE_TO_LONG_BITS:
3756 return handleExecuteInlineC(cUnit, mir);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003757 }
Elliott Hughes7e914f12011-01-19 18:18:42 -08003758 dvmCompilerAbort(cUnit);
3759 return false; // Not reachable; keeps compiler happy.
Ben Chengba4fc8b2009-06-01 13:00:29 -07003760}
3761
3762static bool handleFmt51l(CompilationUnit *cUnit, MIR *mir)
3763{
Bill Buzbee1465db52009-09-23 17:17:35 -07003764 //TUNING: We're using core regs here - not optimal when target is a double
Bill Buzbeec6f10662010-02-09 11:16:15 -08003765 RegLocation rlDest = dvmCompilerGetDestWide(cUnit, mir, 0, 1);
3766 RegLocation rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Ben Chengbd1326d2010-04-02 15:04:53 -07003767 loadConstantNoClobber(cUnit, rlResult.lowReg,
3768 mir->dalvikInsn.vB_wide & 0xFFFFFFFFUL);
3769 loadConstantNoClobber(cUnit, rlResult.highReg,
3770 (mir->dalvikInsn.vB_wide>>32) & 0xFFFFFFFFUL);
Bill Buzbee1465db52009-09-23 17:17:35 -07003771 storeValueWide(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003772 return false;
3773}
3774
Ben Chengba4fc8b2009-06-01 13:00:29 -07003775/*
3776 * The following are special processing routines that handle transfer of
3777 * controls between compiled code and the interpreter. Certain VM states like
3778 * Dalvik PC and special-purpose registers are reconstructed here.
3779 */
3780
Bill Buzbeebd047242010-05-13 13:02:53 -07003781/*
3782 * Insert a
3783 * b .+4
3784 * nop
3785 * pair at the beginning of a chaining cell. This serves as the
3786 * switch branch that selects between reverting to the interpreter or
3787 * not. Once the cell is chained to a translation, the cell will
3788 * contain a 32-bit branch. Subsequent chain/unchain operations will
3789 * then only alter that first 16-bits - the "b .+4" for unchaining,
3790 * and the restoration of the first half of the 32-bit branch for
3791 * rechaining.
3792 */
3793static void insertChainingSwitch(CompilationUnit *cUnit)
3794{
3795 ArmLIR *branch = newLIR0(cUnit, kThumbBUncond);
3796 newLIR2(cUnit, kThumbOrr, r0, r0);
3797 ArmLIR *target = newLIR0(cUnit, kArmPseudoTargetLabel);
3798 target->defMask = ENCODE_ALL;
3799 branch->generic.target = (LIR *) target;
3800}
3801
Ben Cheng1efc9c52009-06-08 18:25:27 -07003802/* Chaining cell for code that may need warmup. */
3803static void handleNormalChainingCell(CompilationUnit *cUnit,
3804 unsigned int offset)
Ben Chengba4fc8b2009-06-01 13:00:29 -07003805{
Ben Cheng11d8f142010-03-24 15:24:19 -07003806 /*
3807 * Use raw instruction constructors to guarantee that the generated
3808 * instructions fit the predefined cell size.
3809 */
Bill Buzbeebd047242010-05-13 13:02:53 -07003810 insertChainingSwitch(cUnit);
Ben Cheng20d7e6c2011-02-18 17:12:42 -08003811 newLIR3(cUnit, kThumbLdrRRI5, r0, r6SELF,
buzbee9f601a92011-02-11 17:48:20 -08003812 offsetof(Thread,
Ben Cheng11d8f142010-03-24 15:24:19 -07003813 jitToInterpEntries.dvmJitToInterpNormal) >> 2);
3814 newLIR1(cUnit, kThumbBlxR, r0);
Ben Cheng385828e2011-03-04 16:48:33 -08003815 addWordData(cUnit, NULL, (int) (cUnit->method->insns + offset));
Ben Chengba4fc8b2009-06-01 13:00:29 -07003816}
3817
3818/*
Ben Cheng1efc9c52009-06-08 18:25:27 -07003819 * Chaining cell for instructions that immediately following already translated
3820 * code.
Ben Chengba4fc8b2009-06-01 13:00:29 -07003821 */
Ben Cheng1efc9c52009-06-08 18:25:27 -07003822static void handleHotChainingCell(CompilationUnit *cUnit,
3823 unsigned int offset)
Ben Chengba4fc8b2009-06-01 13:00:29 -07003824{
Ben Cheng11d8f142010-03-24 15:24:19 -07003825 /*
3826 * Use raw instruction constructors to guarantee that the generated
3827 * instructions fit the predefined cell size.
3828 */
Bill Buzbeebd047242010-05-13 13:02:53 -07003829 insertChainingSwitch(cUnit);
Ben Cheng20d7e6c2011-02-18 17:12:42 -08003830 newLIR3(cUnit, kThumbLdrRRI5, r0, r6SELF,
buzbee9f601a92011-02-11 17:48:20 -08003831 offsetof(Thread,
Ben Cheng11d8f142010-03-24 15:24:19 -07003832 jitToInterpEntries.dvmJitToInterpTraceSelect) >> 2);
3833 newLIR1(cUnit, kThumbBlxR, r0);
Ben Cheng385828e2011-03-04 16:48:33 -08003834 addWordData(cUnit, NULL, (int) (cUnit->method->insns + offset));
Ben Chengba4fc8b2009-06-01 13:00:29 -07003835}
3836
Jeff Hao97319a82009-08-12 16:57:15 -07003837/* Chaining cell for branches that branch back into the same basic block */
3838static void handleBackwardBranchChainingCell(CompilationUnit *cUnit,
3839 unsigned int offset)
3840{
Ben Cheng11d8f142010-03-24 15:24:19 -07003841 /*
3842 * Use raw instruction constructors to guarantee that the generated
3843 * instructions fit the predefined cell size.
3844 */
Bill Buzbeebd047242010-05-13 13:02:53 -07003845 insertChainingSwitch(cUnit);
Bill Buzbee9c4b7c82009-09-10 10:10:38 -07003846#if defined(WITH_SELF_VERIFICATION)
Ben Cheng20d7e6c2011-02-18 17:12:42 -08003847 newLIR3(cUnit, kThumbLdrRRI5, r0, r6SELF,
buzbee9f601a92011-02-11 17:48:20 -08003848 offsetof(Thread,
Ben Cheng40094c12010-02-24 20:58:44 -08003849 jitToInterpEntries.dvmJitToInterpBackwardBranch) >> 2);
Bill Buzbee9c4b7c82009-09-10 10:10:38 -07003850#else
Ben Cheng20d7e6c2011-02-18 17:12:42 -08003851 newLIR3(cUnit, kThumbLdrRRI5, r0, r6SELF,
buzbee9f601a92011-02-11 17:48:20 -08003852 offsetof(Thread, jitToInterpEntries.dvmJitToInterpNormal) >> 2);
Bill Buzbee9c4b7c82009-09-10 10:10:38 -07003853#endif
Bill Buzbee1465db52009-09-23 17:17:35 -07003854 newLIR1(cUnit, kThumbBlxR, r0);
Ben Cheng385828e2011-03-04 16:48:33 -08003855 addWordData(cUnit, NULL, (int) (cUnit->method->insns + offset));
Jeff Hao97319a82009-08-12 16:57:15 -07003856}
3857
Ben Chengba4fc8b2009-06-01 13:00:29 -07003858/* Chaining cell for monomorphic method invocations. */
Ben Cheng38329f52009-07-07 14:19:20 -07003859static void handleInvokeSingletonChainingCell(CompilationUnit *cUnit,
3860 const Method *callee)
Ben Chengba4fc8b2009-06-01 13:00:29 -07003861{
Ben Cheng11d8f142010-03-24 15:24:19 -07003862 /*
3863 * Use raw instruction constructors to guarantee that the generated
3864 * instructions fit the predefined cell size.
3865 */
Bill Buzbeebd047242010-05-13 13:02:53 -07003866 insertChainingSwitch(cUnit);
Ben Cheng20d7e6c2011-02-18 17:12:42 -08003867 newLIR3(cUnit, kThumbLdrRRI5, r0, r6SELF,
buzbee9f601a92011-02-11 17:48:20 -08003868 offsetof(Thread,
Ben Cheng11d8f142010-03-24 15:24:19 -07003869 jitToInterpEntries.dvmJitToInterpTraceSelect) >> 2);
3870 newLIR1(cUnit, kThumbBlxR, r0);
Ben Cheng385828e2011-03-04 16:48:33 -08003871 addWordData(cUnit, NULL, (int) (callee->insns));
Ben Chengba4fc8b2009-06-01 13:00:29 -07003872}
3873
Ben Cheng38329f52009-07-07 14:19:20 -07003874/* Chaining cell for monomorphic method invocations. */
3875static void handleInvokePredictedChainingCell(CompilationUnit *cUnit)
3876{
3877
3878 /* Should not be executed in the initial state */
Ben Cheng385828e2011-03-04 16:48:33 -08003879 addWordData(cUnit, NULL, PREDICTED_CHAIN_BX_PAIR_INIT);
Ben Cheng38329f52009-07-07 14:19:20 -07003880 /* To be filled: class */
Ben Cheng385828e2011-03-04 16:48:33 -08003881 addWordData(cUnit, NULL, PREDICTED_CHAIN_CLAZZ_INIT);
Ben Cheng38329f52009-07-07 14:19:20 -07003882 /* To be filled: method */
Ben Cheng385828e2011-03-04 16:48:33 -08003883 addWordData(cUnit, NULL, PREDICTED_CHAIN_METHOD_INIT);
Ben Cheng38329f52009-07-07 14:19:20 -07003884 /*
3885 * Rechain count. The initial value of 0 here will trigger chaining upon
3886 * the first invocation of this callsite.
3887 */
Ben Cheng385828e2011-03-04 16:48:33 -08003888 addWordData(cUnit, NULL, PREDICTED_CHAIN_COUNTER_INIT);
Ben Cheng38329f52009-07-07 14:19:20 -07003889}
3890
Ben Chengba4fc8b2009-06-01 13:00:29 -07003891/* Load the Dalvik PC into r0 and jump to the specified target */
3892static void handlePCReconstruction(CompilationUnit *cUnit,
Bill Buzbee89efc3d2009-07-28 11:22:22 -07003893 ArmLIR *targetLabel)
Ben Chengba4fc8b2009-06-01 13:00:29 -07003894{
Bill Buzbee89efc3d2009-07-28 11:22:22 -07003895 ArmLIR **pcrLabel =
3896 (ArmLIR **) cUnit->pcReconstructionList.elemList;
Ben Chengba4fc8b2009-06-01 13:00:29 -07003897 int numElems = cUnit->pcReconstructionList.numUsed;
3898 int i;
Ben Cheng9f541852011-04-21 23:09:39 -07003899
3900 /*
3901 * We should never reach here through fall-through code, so insert
3902 * a bomb to signal troubles immediately.
3903 */
3904 if (numElems) {
3905 newLIR0(cUnit, kThumbUndefined);
3906 }
3907
Ben Chengba4fc8b2009-06-01 13:00:29 -07003908 for (i = 0; i < numElems; i++) {
3909 dvmCompilerAppendLIR(cUnit, (LIR *) pcrLabel[i]);
3910 /* r0 = dalvik PC */
3911 loadConstant(cUnit, r0, pcrLabel[i]->operands[0]);
3912 genUnconditionalBranch(cUnit, targetLabel);
3913 }
3914}
3915
Bill Buzbee1465db52009-09-23 17:17:35 -07003916static char *extendedMIROpNames[kMirOpLast - kMirOpFirst] = {
3917 "kMirOpPhi",
3918 "kMirOpNullNRangeUpCheck",
3919 "kMirOpNullNRangeDownCheck",
3920 "kMirOpLowerBound",
3921 "kMirOpPunt",
Ben Cheng7a2697d2010-06-07 13:44:23 -07003922 "kMirOpCheckInlinePrediction",
Ben Cheng4238ec22009-08-24 16:32:22 -07003923};
3924
3925/*
3926 * vA = arrayReg;
3927 * vB = idxReg;
3928 * vC = endConditionReg;
3929 * arg[0] = maxC
3930 * arg[1] = minC
3931 * arg[2] = loopBranchConditionCode
3932 */
3933static void genHoistedChecksForCountUpLoop(CompilationUnit *cUnit, MIR *mir)
3934{
Bill Buzbee1465db52009-09-23 17:17:35 -07003935 /*
3936 * NOTE: these synthesized blocks don't have ssa names assigned
3937 * for Dalvik registers. However, because they dominate the following
3938 * blocks we can simply use the Dalvik name w/ subscript 0 as the
3939 * ssa name.
3940 */
Ben Cheng4238ec22009-08-24 16:32:22 -07003941 DecodedInstruction *dInsn = &mir->dalvikInsn;
3942 const int lenOffset = offsetof(ArrayObject, length);
Ben Cheng4238ec22009-08-24 16:32:22 -07003943 const int maxC = dInsn->arg[0];
Bill Buzbee1465db52009-09-23 17:17:35 -07003944 int regLength;
3945 RegLocation rlArray = cUnit->regLocation[mir->dalvikInsn.vA];
3946 RegLocation rlIdxEnd = cUnit->regLocation[mir->dalvikInsn.vC];
Ben Cheng4238ec22009-08-24 16:32:22 -07003947
3948 /* regArray <- arrayRef */
Bill Buzbee1465db52009-09-23 17:17:35 -07003949 rlArray = loadValue(cUnit, rlArray, kCoreReg);
3950 rlIdxEnd = loadValue(cUnit, rlIdxEnd, kCoreReg);
3951 genRegImmCheck(cUnit, kArmCondEq, rlArray.lowReg, 0, 0,
Ben Cheng4238ec22009-08-24 16:32:22 -07003952 (ArmLIR *) cUnit->loopAnalysis->branchToPCR);
3953
3954 /* regLength <- len(arrayRef) */
Bill Buzbeec6f10662010-02-09 11:16:15 -08003955 regLength = dvmCompilerAllocTemp(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07003956 loadWordDisp(cUnit, rlArray.lowReg, lenOffset, regLength);
Ben Cheng4238ec22009-08-24 16:32:22 -07003957
3958 int delta = maxC;
3959 /*
3960 * If the loop end condition is ">=" instead of ">", then the largest value
3961 * of the index is "endCondition - 1".
3962 */
3963 if (dInsn->arg[2] == OP_IF_GE) {
3964 delta--;
3965 }
3966
3967 if (delta) {
Bill Buzbeec6f10662010-02-09 11:16:15 -08003968 int tReg = dvmCompilerAllocTemp(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07003969 opRegRegImm(cUnit, kOpAdd, tReg, rlIdxEnd.lowReg, delta);
3970 rlIdxEnd.lowReg = tReg;
Bill Buzbeec6f10662010-02-09 11:16:15 -08003971 dvmCompilerFreeTemp(cUnit, tReg);
Ben Cheng4238ec22009-08-24 16:32:22 -07003972 }
3973 /* Punt if "regIdxEnd < len(Array)" is false */
Bill Buzbee1465db52009-09-23 17:17:35 -07003974 genRegRegCheck(cUnit, kArmCondGe, rlIdxEnd.lowReg, regLength, 0,
Ben Cheng0fd31e42009-09-03 14:40:16 -07003975 (ArmLIR *) cUnit->loopAnalysis->branchToPCR);
Ben Cheng4238ec22009-08-24 16:32:22 -07003976}
3977
3978/*
3979 * vA = arrayReg;
3980 * vB = idxReg;
3981 * vC = endConditionReg;
3982 * arg[0] = maxC
3983 * arg[1] = minC
3984 * arg[2] = loopBranchConditionCode
3985 */
3986static void genHoistedChecksForCountDownLoop(CompilationUnit *cUnit, MIR *mir)
3987{
3988 DecodedInstruction *dInsn = &mir->dalvikInsn;
3989 const int lenOffset = offsetof(ArrayObject, length);
Bill Buzbeec6f10662010-02-09 11:16:15 -08003990 const int regLength = dvmCompilerAllocTemp(cUnit);
Ben Cheng4238ec22009-08-24 16:32:22 -07003991 const int maxC = dInsn->arg[0];
Bill Buzbee1465db52009-09-23 17:17:35 -07003992 RegLocation rlArray = cUnit->regLocation[mir->dalvikInsn.vA];
3993 RegLocation rlIdxInit = cUnit->regLocation[mir->dalvikInsn.vB];
Ben Cheng4238ec22009-08-24 16:32:22 -07003994
3995 /* regArray <- arrayRef */
Bill Buzbee1465db52009-09-23 17:17:35 -07003996 rlArray = loadValue(cUnit, rlArray, kCoreReg);
3997 rlIdxInit = loadValue(cUnit, rlIdxInit, kCoreReg);
3998 genRegImmCheck(cUnit, kArmCondEq, rlArray.lowReg, 0, 0,
Ben Cheng4238ec22009-08-24 16:32:22 -07003999 (ArmLIR *) cUnit->loopAnalysis->branchToPCR);
4000
4001 /* regLength <- len(arrayRef) */
Bill Buzbee1465db52009-09-23 17:17:35 -07004002 loadWordDisp(cUnit, rlArray.lowReg, lenOffset, regLength);
Ben Cheng4238ec22009-08-24 16:32:22 -07004003
4004 if (maxC) {
Bill Buzbeec6f10662010-02-09 11:16:15 -08004005 int tReg = dvmCompilerAllocTemp(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07004006 opRegRegImm(cUnit, kOpAdd, tReg, rlIdxInit.lowReg, maxC);
4007 rlIdxInit.lowReg = tReg;
Bill Buzbeec6f10662010-02-09 11:16:15 -08004008 dvmCompilerFreeTemp(cUnit, tReg);
Ben Cheng4238ec22009-08-24 16:32:22 -07004009 }
4010
4011 /* Punt if "regIdxInit < len(Array)" is false */
Bill Buzbee1465db52009-09-23 17:17:35 -07004012 genRegRegCheck(cUnit, kArmCondGe, rlIdxInit.lowReg, regLength, 0,
Ben Cheng0fd31e42009-09-03 14:40:16 -07004013 (ArmLIR *) cUnit->loopAnalysis->branchToPCR);
Ben Cheng4238ec22009-08-24 16:32:22 -07004014}
4015
4016/*
4017 * vA = idxReg;
4018 * vB = minC;
4019 */
4020static void genHoistedLowerBoundCheck(CompilationUnit *cUnit, MIR *mir)
4021{
4022 DecodedInstruction *dInsn = &mir->dalvikInsn;
Ben Cheng4238ec22009-08-24 16:32:22 -07004023 const int minC = dInsn->vB;
Bill Buzbee1465db52009-09-23 17:17:35 -07004024 RegLocation rlIdx = cUnit->regLocation[mir->dalvikInsn.vA];
Ben Cheng4238ec22009-08-24 16:32:22 -07004025
4026 /* regIdx <- initial index value */
Bill Buzbee1465db52009-09-23 17:17:35 -07004027 rlIdx = loadValue(cUnit, rlIdx, kCoreReg);
Ben Cheng4238ec22009-08-24 16:32:22 -07004028
4029 /* Punt if "regIdxInit + minC >= 0" is false */
Bill Buzbee1465db52009-09-23 17:17:35 -07004030 genRegImmCheck(cUnit, kArmCondLt, rlIdx.lowReg, -minC, 0,
Ben Cheng4238ec22009-08-24 16:32:22 -07004031 (ArmLIR *) cUnit->loopAnalysis->branchToPCR);
4032}
4033
Ben Cheng7a2697d2010-06-07 13:44:23 -07004034/*
4035 * vC = this
4036 *
4037 * A predicted inlining target looks like the following, where instructions
4038 * between 0x4858de66 and 0x4858de72 are checking if the predicted class
4039 * matches "this", and the verificaion code is generated by this routine.
4040 *
4041 * (C) means the instruction is inlined from the callee, and (PI) means the
4042 * instruction is the predicted inlined invoke, whose corresponding
4043 * instructions are still generated to handle the mispredicted case.
4044 *
4045 * D/dalvikvm( 86): -------- kMirOpCheckInlinePrediction
4046 * D/dalvikvm( 86): 0x4858de66 (0002): ldr r0, [r5, #68]
4047 * D/dalvikvm( 86): 0x4858de68 (0004): ldr r1, [pc, #140]
4048 * D/dalvikvm( 86): 0x4858de6a (0006): cmp r0, #0
4049 * D/dalvikvm( 86): 0x4858de6c (0008): beq 0x4858deb2
4050 * D/dalvikvm( 86): 0x4858de6e (000a): ldr r2, [r0, #0]
4051 * D/dalvikvm( 86): 0x4858de70 (000c): cmp r1, r2
4052 * D/dalvikvm( 86): 0x4858de72 (000e): bne 0x4858de7a
4053 * D/dalvikvm( 86): -------- dalvik offset: 0x004c @ +iget-object-quick (C)
4054 * v4, v17, (#8)
4055 * D/dalvikvm( 86): 0x4858de74 (0010): ldr r3, [r0, #8]
4056 * D/dalvikvm( 86): 0x4858de76 (0012): str r3, [r5, #16]
4057 * D/dalvikvm( 86): -------- dalvik offset: 0x004c @
4058 * +invoke-virtual-quick/range (PI) v17..v17
4059 * D/dalvikvm( 86): 0x4858de78 (0014): b 0x4858debc
4060 * D/dalvikvm( 86): 0x4858de7a (0016): add r4,r5,#68
4061 * D/dalvikvm( 86): -------- BARRIER
4062 * D/dalvikvm( 86): 0x4858de7e (001a): ldmia r4, <r0>
4063 * D/dalvikvm( 86): -------- BARRIER
4064 * D/dalvikvm( 86): 0x4858de80 (001c): sub r7,r5,#24
4065 * D/dalvikvm( 86): 0x4858de84 (0020): cmp r0, #0
4066 * D/dalvikvm( 86): 0x4858de86 (0022): beq 0x4858deb6
4067 * D/dalvikvm( 86): -------- BARRIER
4068 * D/dalvikvm( 86): 0x4858de88 (0024): stmia r7, <r0>
4069 * D/dalvikvm( 86): -------- BARRIER
4070 * D/dalvikvm( 86): 0x4858de8a (0026): ldr r4, [pc, #104]
4071 * D/dalvikvm( 86): 0x4858de8c (0028): add r1, pc, #28
4072 * D/dalvikvm( 86): 0x4858de8e (002a): add r2, pc, #56
4073 * D/dalvikvm( 86): 0x4858de90 (002c): blx_1 0x48589198
4074 * D/dalvikvm( 86): 0x4858de92 (002e): blx_2 see above
4075 * D/dalvikvm( 86): 0x4858de94 (0030): b 0x4858dec8
4076 * D/dalvikvm( 86): 0x4858de96 (0032): b 0x4858deb6
4077 * D/dalvikvm( 86): 0x4858de98 (0034): ldr r0, [r7, #72]
4078 * D/dalvikvm( 86): 0x4858de9a (0036): cmp r1, #0
4079 * D/dalvikvm( 86): 0x4858de9c (0038): bgt 0x4858dea4
4080 * D/dalvikvm( 86): 0x4858de9e (003a): ldr r7, [r6, #116]
4081 * D/dalvikvm( 86): 0x4858dea0 (003c): movs r1, r6
4082 * D/dalvikvm( 86): 0x4858dea2 (003e): blx r7
4083 * D/dalvikvm( 86): 0x4858dea4 (0040): add r1, pc, #4
4084 * D/dalvikvm( 86): 0x4858dea6 (0042): blx_1 0x485890a0
4085 * D/dalvikvm( 86): 0x4858dea8 (0044): blx_2 see above
4086 * D/dalvikvm( 86): 0x4858deaa (0046): b 0x4858deb6
4087 * D/dalvikvm( 86): 0x4858deac (0048): .align4
4088 * D/dalvikvm( 86): L0x004f:
4089 * D/dalvikvm( 86): -------- dalvik offset: 0x004f @ move-result-object (PI)
4090 * v4, (#0), (#0)
4091 * D/dalvikvm( 86): 0x4858deac (0048): ldr r4, [r6, #8]
4092 * D/dalvikvm( 86): 0x4858deae (004a): str r4, [r5, #16]
4093 * D/dalvikvm( 86): 0x4858deb0 (004c): b 0x4858debc
4094 * D/dalvikvm( 86): -------- reconstruct dalvik PC : 0x42beefcc @ +0x004c
4095 * D/dalvikvm( 86): 0x4858deb2 (004e): ldr r0, [pc, #64]
4096 * D/dalvikvm( 86): 0x4858deb4 (0050): b 0x4858deb8
4097 * D/dalvikvm( 86): -------- reconstruct dalvik PC : 0x42beefcc @ +0x004c
4098 * D/dalvikvm( 86): 0x4858deb6 (0052): ldr r0, [pc, #60]
4099 * D/dalvikvm( 86): Exception_Handling:
4100 * D/dalvikvm( 86): 0x4858deb8 (0054): ldr r1, [r6, #100]
4101 * D/dalvikvm( 86): 0x4858deba (0056): blx r1
4102 * D/dalvikvm( 86): 0x4858debc (0058): .align4
4103 * D/dalvikvm( 86): -------- chaining cell (hot): 0x0050
4104 * D/dalvikvm( 86): 0x4858debc (0058): b 0x4858dec0
4105 * D/dalvikvm( 86): 0x4858debe (005a): orrs r0, r0
4106 * D/dalvikvm( 86): 0x4858dec0 (005c): ldr r0, [r6, #112]
4107 * D/dalvikvm( 86): 0x4858dec2 (005e): blx r0
4108 * D/dalvikvm( 86): 0x4858dec4 (0060): data 0xefd4(61396)
4109 * D/dalvikvm( 86): 0x4858dec6 (0062): data 0x42be(17086)
4110 * D/dalvikvm( 86): 0x4858dec8 (0064): .align4
4111 * D/dalvikvm( 86): -------- chaining cell (predicted)
4112 * D/dalvikvm( 86): 0x4858dec8 (0064): data 0xe7fe(59390)
4113 * D/dalvikvm( 86): 0x4858deca (0066): data 0x0000(0)
4114 * D/dalvikvm( 86): 0x4858decc (0068): data 0x0000(0)
4115 * D/dalvikvm( 86): 0x4858dece (006a): data 0x0000(0)
4116 * :
4117 */
4118static void genValidationForPredictedInline(CompilationUnit *cUnit, MIR *mir)
4119{
4120 CallsiteInfo *callsiteInfo = mir->meta.callsiteInfo;
4121 RegLocation rlThis = cUnit->regLocation[mir->dalvikInsn.vC];
4122
4123 rlThis = loadValue(cUnit, rlThis, kCoreReg);
4124 int regPredictedClass = dvmCompilerAllocTemp(cUnit);
Ben Cheng385828e2011-03-04 16:48:33 -08004125 loadClassPointer(cUnit, regPredictedClass, (int) callsiteInfo);
Ben Cheng7a2697d2010-06-07 13:44:23 -07004126 genNullCheck(cUnit, rlThis.sRegLow, rlThis.lowReg, mir->offset,
4127 NULL);/* null object? */
4128 int regActualClass = dvmCompilerAllocTemp(cUnit);
4129 loadWordDisp(cUnit, rlThis.lowReg, offsetof(Object, clazz), regActualClass);
4130 opRegReg(cUnit, kOpCmp, regPredictedClass, regActualClass);
4131 /*
4132 * Set the misPredBranchOver target so that it will be generated when the
4133 * code for the non-optimized invoke is generated.
4134 */
4135 callsiteInfo->misPredBranchOver = (LIR *) opCondBranch(cUnit, kArmCondNe);
4136}
4137
Ben Cheng4238ec22009-08-24 16:32:22 -07004138/* Extended MIR instructions like PHI */
4139static void handleExtendedMIR(CompilationUnit *cUnit, MIR *mir)
4140{
Dan Bornstein9a1f8162010-12-01 17:02:26 -08004141 int opOffset = mir->dalvikInsn.opcode - kMirOpFirst;
Carl Shapirofc75f3e2010-12-07 11:43:38 -08004142 char *msg = (char *)dvmCompilerNew(strlen(extendedMIROpNames[opOffset]) + 1,
4143 false);
Ben Cheng4238ec22009-08-24 16:32:22 -07004144 strcpy(msg, extendedMIROpNames[opOffset]);
Bill Buzbee1465db52009-09-23 17:17:35 -07004145 newLIR1(cUnit, kArmPseudoExtended, (int) msg);
Ben Cheng4238ec22009-08-24 16:32:22 -07004146
Dan Bornstein9a1f8162010-12-01 17:02:26 -08004147 switch (mir->dalvikInsn.opcode) {
Bill Buzbee1465db52009-09-23 17:17:35 -07004148 case kMirOpPhi: {
Ben Cheng4238ec22009-08-24 16:32:22 -07004149 char *ssaString = dvmCompilerGetSSAString(cUnit, mir->ssaRep);
Bill Buzbee1465db52009-09-23 17:17:35 -07004150 newLIR1(cUnit, kArmPseudoSSARep, (int) ssaString);
Ben Cheng4238ec22009-08-24 16:32:22 -07004151 break;
4152 }
Bill Buzbee1465db52009-09-23 17:17:35 -07004153 case kMirOpNullNRangeUpCheck: {
Ben Cheng4238ec22009-08-24 16:32:22 -07004154 genHoistedChecksForCountUpLoop(cUnit, mir);
4155 break;
4156 }
Bill Buzbee1465db52009-09-23 17:17:35 -07004157 case kMirOpNullNRangeDownCheck: {
Ben Cheng4238ec22009-08-24 16:32:22 -07004158 genHoistedChecksForCountDownLoop(cUnit, mir);
4159 break;
4160 }
Bill Buzbee1465db52009-09-23 17:17:35 -07004161 case kMirOpLowerBound: {
Ben Cheng4238ec22009-08-24 16:32:22 -07004162 genHoistedLowerBoundCheck(cUnit, mir);
4163 break;
4164 }
Bill Buzbee1465db52009-09-23 17:17:35 -07004165 case kMirOpPunt: {
Ben Cheng4238ec22009-08-24 16:32:22 -07004166 genUnconditionalBranch(cUnit,
4167 (ArmLIR *) cUnit->loopAnalysis->branchToPCR);
4168 break;
4169 }
Ben Cheng7a2697d2010-06-07 13:44:23 -07004170 case kMirOpCheckInlinePrediction: {
4171 genValidationForPredictedInline(cUnit, mir);
4172 break;
4173 }
Ben Cheng4238ec22009-08-24 16:32:22 -07004174 default:
4175 break;
4176 }
4177}
4178
4179/*
4180 * Create a PC-reconstruction cell for the starting offset of this trace.
4181 * Since the PCR cell is placed near the end of the compiled code which is
4182 * usually out of range for a conditional branch, we put two branches (one
4183 * branch over to the loop body and one layover branch to the actual PCR) at the
4184 * end of the entry block.
4185 */
4186static void setupLoopEntryBlock(CompilationUnit *cUnit, BasicBlock *entry,
4187 ArmLIR *bodyLabel)
4188{
4189 /* Set up the place holder to reconstruct this Dalvik PC */
Carl Shapirofc75f3e2010-12-07 11:43:38 -08004190 ArmLIR *pcrLabel = (ArmLIR *) dvmCompilerNew(sizeof(ArmLIR), true);
Dan Bornstein9a1f8162010-12-01 17:02:26 -08004191 pcrLabel->opcode = kArmPseudoPCReconstructionCell;
Ben Cheng4238ec22009-08-24 16:32:22 -07004192 pcrLabel->operands[0] =
4193 (int) (cUnit->method->insns + entry->startOffset);
4194 pcrLabel->operands[1] = entry->startOffset;
4195 /* Insert the place holder to the growable list */
Ben Cheng00603072010-10-28 11:13:58 -07004196 dvmInsertGrowableList(&cUnit->pcReconstructionList, (intptr_t) pcrLabel);
Ben Cheng4238ec22009-08-24 16:32:22 -07004197
4198 /*
4199 * Next, create two branches - one branch over to the loop body and the
4200 * other branch to the PCR cell to punt.
4201 */
Carl Shapirofc75f3e2010-12-07 11:43:38 -08004202 ArmLIR *branchToBody = (ArmLIR *) dvmCompilerNew(sizeof(ArmLIR), true);
Dan Bornstein9a1f8162010-12-01 17:02:26 -08004203 branchToBody->opcode = kThumbBUncond;
Ben Cheng4238ec22009-08-24 16:32:22 -07004204 branchToBody->generic.target = (LIR *) bodyLabel;
Ben Chengdcf3e5d2009-09-11 13:42:05 -07004205 setupResourceMasks(branchToBody);
Ben Cheng4238ec22009-08-24 16:32:22 -07004206 cUnit->loopAnalysis->branchToBody = (LIR *) branchToBody;
4207
Carl Shapirofc75f3e2010-12-07 11:43:38 -08004208 ArmLIR *branchToPCR = (ArmLIR *) dvmCompilerNew(sizeof(ArmLIR), true);
Dan Bornstein9a1f8162010-12-01 17:02:26 -08004209 branchToPCR->opcode = kThumbBUncond;
Ben Cheng4238ec22009-08-24 16:32:22 -07004210 branchToPCR->generic.target = (LIR *) pcrLabel;
Ben Chengdcf3e5d2009-09-11 13:42:05 -07004211 setupResourceMasks(branchToPCR);
Ben Cheng4238ec22009-08-24 16:32:22 -07004212 cUnit->loopAnalysis->branchToPCR = (LIR *) branchToPCR;
4213}
4214
Ben Chengd5adae12010-03-26 17:45:28 -07004215#if defined(WITH_SELF_VERIFICATION)
4216static bool selfVerificationPuntOps(MIR *mir)
4217{
4218 DecodedInstruction *decInsn = &mir->dalvikInsn;
Ben Cheng7a2697d2010-06-07 13:44:23 -07004219
Ben Chengd5adae12010-03-26 17:45:28 -07004220 /*
4221 * All opcodes that can throw exceptions and use the
4222 * TEMPLATE_THROW_EXCEPTION_COMMON template should be excluded in the trace
4223 * under self-verification mode.
4224 */
Ben Cheng072b5d02011-03-31 10:44:44 -07004225 switch (decInsn->opcode) {
4226 case OP_MONITOR_ENTER:
4227 case OP_MONITOR_EXIT:
4228 case OP_NEW_INSTANCE:
4229 case OP_NEW_INSTANCE_JUMBO:
4230 case OP_NEW_ARRAY:
4231 case OP_NEW_ARRAY_JUMBO:
4232 case OP_CHECK_CAST:
4233 case OP_CHECK_CAST_JUMBO:
4234 case OP_MOVE_EXCEPTION:
4235 case OP_FILL_ARRAY_DATA:
4236 case OP_EXECUTE_INLINE:
4237 case OP_EXECUTE_INLINE_RANGE:
4238 return true;
4239 default:
4240 return false;
4241 }
Ben Chengd5adae12010-03-26 17:45:28 -07004242}
4243#endif
4244
Ben Chengba4fc8b2009-06-01 13:00:29 -07004245void dvmCompilerMIR2LIR(CompilationUnit *cUnit)
4246{
4247 /* Used to hold the labels of each block */
Bill Buzbee89efc3d2009-07-28 11:22:22 -07004248 ArmLIR *labelList =
Carl Shapirofc75f3e2010-12-07 11:43:38 -08004249 (ArmLIR *) dvmCompilerNew(sizeof(ArmLIR) * cUnit->numBlocks, true);
Ben Chengcec26f62010-01-15 15:29:33 -08004250 GrowableList chainingListByType[kChainingCellGap];
Ben Chengba4fc8b2009-06-01 13:00:29 -07004251 int i;
4252
4253 /*
Ben Cheng38329f52009-07-07 14:19:20 -07004254 * Initialize various types chaining lists.
Ben Chengba4fc8b2009-06-01 13:00:29 -07004255 */
Ben Chengcec26f62010-01-15 15:29:33 -08004256 for (i = 0; i < kChainingCellGap; i++) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07004257 dvmInitGrowableList(&chainingListByType[i], 2);
4258 }
4259
Ben Cheng7ab74e12011-02-03 14:02:06 -08004260 /* Clear the visited flag for each block */
4261 dvmCompilerDataFlowAnalysisDispatcher(cUnit, dvmCompilerClearVisitedFlag,
4262 kAllNodes, false /* isIterative */);
4263
Ben Cheng00603072010-10-28 11:13:58 -07004264 GrowableListIterator iterator;
4265 dvmGrowableListIteratorInit(&cUnit->blockList, &iterator);
Ben Chengba4fc8b2009-06-01 13:00:29 -07004266
buzbee2e152ba2010-12-15 16:32:35 -08004267 /* Traces start with a profiling entry point. Generate it here */
4268 cUnit->profileCodeSize = genTraceProfileEntry(cUnit);
Ben Cheng1efc9c52009-06-08 18:25:27 -07004269
Ben Chengba4fc8b2009-06-01 13:00:29 -07004270 /* Handle the content in each basic block */
Ben Cheng00603072010-10-28 11:13:58 -07004271 for (i = 0; ; i++) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07004272 MIR *mir;
Ben Cheng00603072010-10-28 11:13:58 -07004273 BasicBlock *bb = (BasicBlock *) dvmGrowableListIteratorNext(&iterator);
4274 if (bb == NULL) break;
Ben Cheng7ab74e12011-02-03 14:02:06 -08004275 if (bb->visited == true) continue;
Ben Chengba4fc8b2009-06-01 13:00:29 -07004276
Ben Cheng00603072010-10-28 11:13:58 -07004277 labelList[i].operands[0] = bb->startOffset;
Ben Chengba4fc8b2009-06-01 13:00:29 -07004278
Ben Cheng00603072010-10-28 11:13:58 -07004279 if (bb->blockType >= kChainingCellGap) {
4280 if (bb->isFallThroughFromInvoke == true) {
Ben Chengd44faf52010-06-02 15:33:51 -07004281 /* Align this block first since it is a return chaining cell */
4282 newLIR0(cUnit, kArmPseudoPseudoAlign4);
4283 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07004284 /*
4285 * Append the label pseudo LIR first. Chaining cells will be handled
4286 * separately afterwards.
4287 */
4288 dvmCompilerAppendLIR(cUnit, (LIR *) &labelList[i]);
4289 }
4290
Ben Cheng32115a92011-03-22 14:09:09 -07004291 if (bb->blockType == kEntryBlock) {
Dan Bornstein9a1f8162010-12-01 17:02:26 -08004292 labelList[i].opcode = kArmPseudoEntryBlock;
Ben Cheng00603072010-10-28 11:13:58 -07004293 if (bb->firstMIRInsn == NULL) {
Ben Cheng4238ec22009-08-24 16:32:22 -07004294 continue;
4295 } else {
Ben Cheng00603072010-10-28 11:13:58 -07004296 setupLoopEntryBlock(cUnit, bb,
4297 &labelList[bb->fallThrough->id]);
Ben Cheng4238ec22009-08-24 16:32:22 -07004298 }
Ben Cheng32115a92011-03-22 14:09:09 -07004299 } else if (bb->blockType == kExitBlock) {
Dan Bornstein9a1f8162010-12-01 17:02:26 -08004300 labelList[i].opcode = kArmPseudoExitBlock;
Ben Cheng4238ec22009-08-24 16:32:22 -07004301 goto gen_fallthrough;
Ben Cheng00603072010-10-28 11:13:58 -07004302 } else if (bb->blockType == kDalvikByteCode) {
Ben Cheng32115a92011-03-22 14:09:09 -07004303 if (bb->hidden == true) continue;
Dan Bornstein9a1f8162010-12-01 17:02:26 -08004304 labelList[i].opcode = kArmPseudoNormalBlockLabel;
Ben Chenge9695e52009-06-16 16:11:47 -07004305 /* Reset the register state */
Bill Buzbeec6f10662010-02-09 11:16:15 -08004306 dvmCompilerResetRegPool(cUnit);
4307 dvmCompilerClobberAllRegs(cUnit);
4308 dvmCompilerResetNullCheck(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -07004309 } else {
Ben Cheng00603072010-10-28 11:13:58 -07004310 switch (bb->blockType) {
Bill Buzbee1465db52009-09-23 17:17:35 -07004311 case kChainingCellNormal:
Dan Bornstein9a1f8162010-12-01 17:02:26 -08004312 labelList[i].opcode = kArmPseudoChainingCellNormal;
Ben Chengba4fc8b2009-06-01 13:00:29 -07004313 /* handle the codegen later */
4314 dvmInsertGrowableList(
Ben Cheng00603072010-10-28 11:13:58 -07004315 &chainingListByType[kChainingCellNormal], i);
Ben Chengba4fc8b2009-06-01 13:00:29 -07004316 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07004317 case kChainingCellInvokeSingleton:
Dan Bornstein9a1f8162010-12-01 17:02:26 -08004318 labelList[i].opcode =
Ben Chenga4973592010-03-31 11:59:18 -07004319 kArmPseudoChainingCellInvokeSingleton;
Ben Chengba4fc8b2009-06-01 13:00:29 -07004320 labelList[i].operands[0] =
Ben Cheng00603072010-10-28 11:13:58 -07004321 (int) bb->containingMethod;
Ben Chengba4fc8b2009-06-01 13:00:29 -07004322 /* handle the codegen later */
4323 dvmInsertGrowableList(
Ben Cheng00603072010-10-28 11:13:58 -07004324 &chainingListByType[kChainingCellInvokeSingleton], i);
Ben Cheng38329f52009-07-07 14:19:20 -07004325 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07004326 case kChainingCellInvokePredicted:
Dan Bornstein9a1f8162010-12-01 17:02:26 -08004327 labelList[i].opcode =
Ben Chenga4973592010-03-31 11:59:18 -07004328 kArmPseudoChainingCellInvokePredicted;
Ben Cheng04517042011-03-14 11:16:21 -07004329 /*
4330 * Move the cached method pointer from operand 1 to 0.
4331 * Operand 0 was clobbered earlier in this routine to store
4332 * the block starting offset, which is not applicable to
4333 * predicted chaining cell.
4334 */
4335 labelList[i].operands[0] = labelList[i].operands[1];
Ben Cheng38329f52009-07-07 14:19:20 -07004336 /* handle the codegen later */
4337 dvmInsertGrowableList(
Ben Cheng00603072010-10-28 11:13:58 -07004338 &chainingListByType[kChainingCellInvokePredicted], i);
Ben Chengba4fc8b2009-06-01 13:00:29 -07004339 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07004340 case kChainingCellHot:
Dan Bornstein9a1f8162010-12-01 17:02:26 -08004341 labelList[i].opcode =
Ben Chenga4973592010-03-31 11:59:18 -07004342 kArmPseudoChainingCellHot;
Ben Chengba4fc8b2009-06-01 13:00:29 -07004343 /* handle the codegen later */
4344 dvmInsertGrowableList(
Ben Cheng00603072010-10-28 11:13:58 -07004345 &chainingListByType[kChainingCellHot], i);
Ben Chengba4fc8b2009-06-01 13:00:29 -07004346 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07004347 case kPCReconstruction:
Ben Chengba4fc8b2009-06-01 13:00:29 -07004348 /* Make sure exception handling block is next */
Dan Bornstein9a1f8162010-12-01 17:02:26 -08004349 labelList[i].opcode =
Ben Chenga4973592010-03-31 11:59:18 -07004350 kArmPseudoPCReconstructionBlockLabel;
Ben Cheng32115a92011-03-22 14:09:09 -07004351 handlePCReconstruction(cUnit,
4352 &labelList[cUnit->puntBlock->id]);
Ben Chengba4fc8b2009-06-01 13:00:29 -07004353 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07004354 case kExceptionHandling:
Dan Bornstein9a1f8162010-12-01 17:02:26 -08004355 labelList[i].opcode = kArmPseudoEHBlockLabel;
Ben Chengba4fc8b2009-06-01 13:00:29 -07004356 if (cUnit->pcReconstructionList.numUsed) {
Ben Cheng20d7e6c2011-02-18 17:12:42 -08004357 loadWordDisp(cUnit, r6SELF, offsetof(Thread,
Bill Buzbee270c1d62009-08-13 16:58:07 -07004358 jitToInterpEntries.dvmJitToInterpPunt),
4359 r1);
Bill Buzbee1465db52009-09-23 17:17:35 -07004360 opReg(cUnit, kOpBlx, r1);
Ben Chengba4fc8b2009-06-01 13:00:29 -07004361 }
4362 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07004363 case kChainingCellBackwardBranch:
Dan Bornstein9a1f8162010-12-01 17:02:26 -08004364 labelList[i].opcode =
Ben Chenga4973592010-03-31 11:59:18 -07004365 kArmPseudoChainingCellBackwardBranch;
Jeff Hao97319a82009-08-12 16:57:15 -07004366 /* handle the codegen later */
4367 dvmInsertGrowableList(
Bill Buzbee1465db52009-09-23 17:17:35 -07004368 &chainingListByType[kChainingCellBackwardBranch],
Ben Cheng00603072010-10-28 11:13:58 -07004369 i);
Jeff Hao97319a82009-08-12 16:57:15 -07004370 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07004371 default:
4372 break;
4373 }
4374 continue;
4375 }
Ben Chenge9695e52009-06-16 16:11:47 -07004376
Bill Buzbee89efc3d2009-07-28 11:22:22 -07004377 ArmLIR *headLIR = NULL;
Ben Cheng7ab74e12011-02-03 14:02:06 -08004378 BasicBlock *nextBB = bb;
Ben Chenge9695e52009-06-16 16:11:47 -07004379
Ben Cheng7ab74e12011-02-03 14:02:06 -08004380 /*
4381 * Try to build a longer optimization unit. Currently if the previous
4382 * block ends with a goto, we continue adding instructions and don't
4383 * reset the register allocation pool.
4384 */
4385 for (; nextBB != NULL; nextBB = cUnit->nextCodegenBlock) {
4386 bb = nextBB;
4387 bb->visited = true;
4388 cUnit->nextCodegenBlock = NULL;
Bill Buzbee1465db52009-09-23 17:17:35 -07004389
Ben Cheng7ab74e12011-02-03 14:02:06 -08004390 for (mir = bb->firstMIRInsn; mir; mir = mir->next) {
Bill Buzbee1465db52009-09-23 17:17:35 -07004391
Ben Cheng7ab74e12011-02-03 14:02:06 -08004392 dvmCompilerResetRegPool(cUnit);
4393 if (gDvmJit.disableOpt & (1 << kTrackLiveTemps)) {
4394 dvmCompilerClobberAllRegs(cUnit);
Ben Cheng80211d22011-01-14 10:23:37 -08004395 }
Ben Cheng4238ec22009-08-24 16:32:22 -07004396
Ben Cheng7ab74e12011-02-03 14:02:06 -08004397 if (gDvmJit.disableOpt & (1 << kSuppressLoads)) {
4398 dvmCompilerResetDefTracking(cUnit);
4399 }
Ben Cheng4238ec22009-08-24 16:32:22 -07004400
Ben Cheng7ab74e12011-02-03 14:02:06 -08004401 if (mir->dalvikInsn.opcode >= kMirOpFirst) {
4402 handleExtendedMIR(cUnit, mir);
4403 continue;
4404 }
4405
4406
4407 Opcode dalvikOpcode = mir->dalvikInsn.opcode;
4408 InstructionFormat dalvikFormat =
4409 dexGetFormatFromOpcode(dalvikOpcode);
4410 char *note;
4411 if (mir->OptimizationFlags & MIR_INLINED) {
4412 note = " (I)";
4413 } else if (mir->OptimizationFlags & MIR_INLINED_PRED) {
4414 note = " (PI)";
4415 } else if (mir->OptimizationFlags & MIR_CALLEE) {
4416 note = " (C)";
4417 } else {
4418 note = NULL;
4419 }
4420
4421 ArmLIR *boundaryLIR;
4422
4423 /*
4424 * Don't generate the boundary LIR unless we are debugging this
4425 * trace or we need a scheduling barrier.
4426 */
4427 if (headLIR == NULL || cUnit->printMe == true) {
4428 boundaryLIR =
4429 newLIR2(cUnit, kArmPseudoDalvikByteCodeBoundary,
4430 mir->offset,
4431 (int) dvmCompilerGetDalvikDisassembly(
4432 &mir->dalvikInsn, note));
4433 /* Remember the first LIR for this block */
4434 if (headLIR == NULL) {
4435 headLIR = boundaryLIR;
4436 /* Set the first boundaryLIR as a scheduling barrier */
4437 headLIR->defMask = ENCODE_ALL;
4438 }
4439 }
4440
4441 /*
4442 * Don't generate the SSA annotation unless verbose mode is on
4443 */
4444 if (cUnit->printMe && mir->ssaRep) {
4445 char *ssaString = dvmCompilerGetSSAString(cUnit,
4446 mir->ssaRep);
4447 newLIR1(cUnit, kArmPseudoSSARep, (int) ssaString);
4448 }
4449
4450 bool notHandled;
4451 /*
4452 * Debugging: screen the opcode first to see if it is in the
4453 * do[-not]-compile list
4454 */
4455 bool singleStepMe = SINGLE_STEP_OP(dalvikOpcode);
Ben Chengd5adae12010-03-26 17:45:28 -07004456#if defined(WITH_SELF_VERIFICATION)
Ben Cheng7ab74e12011-02-03 14:02:06 -08004457 if (singleStepMe == false) {
4458 singleStepMe = selfVerificationPuntOps(mir);
4459 }
Ben Chengd5adae12010-03-26 17:45:28 -07004460#endif
Ben Cheng7ab74e12011-02-03 14:02:06 -08004461 if (singleStepMe || cUnit->allSingleStep) {
4462 notHandled = false;
4463 genInterpSingleStep(cUnit, mir);
4464 } else {
4465 opcodeCoverage[dalvikOpcode]++;
4466 switch (dalvikFormat) {
4467 case kFmt10t:
4468 case kFmt20t:
4469 case kFmt30t:
4470 notHandled = handleFmt10t_Fmt20t_Fmt30t(cUnit,
4471 mir, bb, labelList);
4472 break;
4473 case kFmt10x:
4474 notHandled = handleFmt10x(cUnit, mir);
4475 break;
4476 case kFmt11n:
4477 case kFmt31i:
4478 notHandled = handleFmt11n_Fmt31i(cUnit, mir);
4479 break;
4480 case kFmt11x:
4481 notHandled = handleFmt11x(cUnit, mir);
4482 break;
4483 case kFmt12x:
4484 notHandled = handleFmt12x(cUnit, mir);
4485 break;
4486 case kFmt20bc:
4487 case kFmt40sc:
4488 notHandled = handleFmt20bc_Fmt40sc(cUnit, mir);
4489 break;
4490 case kFmt21c:
4491 case kFmt31c:
4492 case kFmt41c:
4493 notHandled = handleFmt21c_Fmt31c_Fmt41c(cUnit, mir);
4494 break;
4495 case kFmt21h:
4496 notHandled = handleFmt21h(cUnit, mir);
4497 break;
4498 case kFmt21s:
4499 notHandled = handleFmt21s(cUnit, mir);
4500 break;
4501 case kFmt21t:
4502 notHandled = handleFmt21t(cUnit, mir, bb,
Ben Chengba4fc8b2009-06-01 13:00:29 -07004503 labelList);
Ben Cheng7ab74e12011-02-03 14:02:06 -08004504 break;
4505 case kFmt22b:
4506 case kFmt22s:
4507 notHandled = handleFmt22b_Fmt22s(cUnit, mir);
4508 break;
4509 case kFmt22c:
4510 case kFmt52c:
4511 notHandled = handleFmt22c_Fmt52c(cUnit, mir);
4512 break;
4513 case kFmt22cs:
4514 notHandled = handleFmt22cs(cUnit, mir);
4515 break;
4516 case kFmt22t:
4517 notHandled = handleFmt22t(cUnit, mir, bb,
4518 labelList);
4519 break;
4520 case kFmt22x:
4521 case kFmt32x:
4522 notHandled = handleFmt22x_Fmt32x(cUnit, mir);
4523 break;
4524 case kFmt23x:
4525 notHandled = handleFmt23x(cUnit, mir);
4526 break;
4527 case kFmt31t:
4528 notHandled = handleFmt31t(cUnit, mir);
4529 break;
4530 case kFmt3rc:
4531 case kFmt35c:
4532 case kFmt5rc:
4533 notHandled = handleFmt35c_3rc_5rc(cUnit, mir, bb,
4534 labelList);
4535 break;
4536 case kFmt3rms:
4537 case kFmt35ms:
4538 notHandled = handleFmt35ms_3rms(cUnit, mir, bb,
4539 labelList);
4540 break;
4541 case kFmt35mi:
4542 case kFmt3rmi:
4543 notHandled = handleExecuteInline(cUnit, mir);
4544 break;
4545 case kFmt51l:
4546 notHandled = handleFmt51l(cUnit, mir);
4547 break;
4548 default:
4549 notHandled = true;
4550 break;
4551 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07004552 }
Ben Cheng7ab74e12011-02-03 14:02:06 -08004553 if (notHandled) {
4554 LOGE("%#06x: Opcode 0x%x (%s) / Fmt %d not handled\n",
4555 mir->offset,
4556 dalvikOpcode, dexGetOpcodeName(dalvikOpcode),
4557 dalvikFormat);
4558 dvmCompilerAbort(cUnit);
4559 break;
4560 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07004561 }
4562 }
Ben Cheng4238ec22009-08-24 16:32:22 -07004563
Ben Cheng32115a92011-03-22 14:09:09 -07004564 if (bb->blockType == kEntryBlock) {
Ben Cheng4238ec22009-08-24 16:32:22 -07004565 dvmCompilerAppendLIR(cUnit,
4566 (LIR *) cUnit->loopAnalysis->branchToBody);
4567 dvmCompilerAppendLIR(cUnit,
4568 (LIR *) cUnit->loopAnalysis->branchToPCR);
4569 }
4570
4571 if (headLIR) {
4572 /*
4573 * Eliminate redundant loads/stores and delay stores into later
4574 * slots
4575 */
4576 dvmCompilerApplyLocalOptimizations(cUnit, (LIR *) headLIR,
4577 cUnit->lastLIRInsn);
Ben Chengde9e2b92011-04-26 10:00:22 -07004578 /* Reset headLIR which is also the optimization boundary */
4579 headLIR = NULL;
Ben Cheng4238ec22009-08-24 16:32:22 -07004580 }
4581
4582gen_fallthrough:
Ben Cheng1efc9c52009-06-08 18:25:27 -07004583 /*
4584 * Check if the block is terminated due to trace length constraint -
4585 * insert an unconditional branch to the chaining cell.
4586 */
Ben Cheng00603072010-10-28 11:13:58 -07004587 if (bb->needFallThroughBranch) {
Ben Cheng7ab74e12011-02-03 14:02:06 -08004588 genUnconditionalBranch(cUnit, &labelList[bb->fallThrough->id]);
Ben Cheng1efc9c52009-06-08 18:25:27 -07004589 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07004590 }
4591
Ben Chenge9695e52009-06-16 16:11:47 -07004592 /* Handle the chaining cells in predefined order */
Ben Chengcec26f62010-01-15 15:29:33 -08004593 for (i = 0; i < kChainingCellGap; i++) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07004594 size_t j;
4595 int *blockIdList = (int *) chainingListByType[i].elemList;
4596
4597 cUnit->numChainingCells[i] = chainingListByType[i].numUsed;
4598
4599 /* No chaining cells of this type */
4600 if (cUnit->numChainingCells[i] == 0)
4601 continue;
4602
4603 /* Record the first LIR for a new type of chaining cell */
4604 cUnit->firstChainingLIR[i] = (LIR *) &labelList[blockIdList[0]];
4605
4606 for (j = 0; j < chainingListByType[i].numUsed; j++) {
4607 int blockId = blockIdList[j];
Ben Cheng00603072010-10-28 11:13:58 -07004608 BasicBlock *chainingBlock =
4609 (BasicBlock *) dvmGrowableListGetElement(&cUnit->blockList,
4610 blockId);
Ben Chengba4fc8b2009-06-01 13:00:29 -07004611
4612 /* Align this chaining cell first */
Bill Buzbee1465db52009-09-23 17:17:35 -07004613 newLIR0(cUnit, kArmPseudoPseudoAlign4);
Ben Chengba4fc8b2009-06-01 13:00:29 -07004614
4615 /* Insert the pseudo chaining instruction */
4616 dvmCompilerAppendLIR(cUnit, (LIR *) &labelList[blockId]);
4617
4618
Ben Cheng00603072010-10-28 11:13:58 -07004619 switch (chainingBlock->blockType) {
Bill Buzbee1465db52009-09-23 17:17:35 -07004620 case kChainingCellNormal:
Ben Cheng00603072010-10-28 11:13:58 -07004621 handleNormalChainingCell(cUnit, chainingBlock->startOffset);
Ben Chengba4fc8b2009-06-01 13:00:29 -07004622 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07004623 case kChainingCellInvokeSingleton:
Ben Cheng38329f52009-07-07 14:19:20 -07004624 handleInvokeSingletonChainingCell(cUnit,
Ben Cheng00603072010-10-28 11:13:58 -07004625 chainingBlock->containingMethod);
Ben Chengba4fc8b2009-06-01 13:00:29 -07004626 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07004627 case kChainingCellInvokePredicted:
Ben Cheng38329f52009-07-07 14:19:20 -07004628 handleInvokePredictedChainingCell(cUnit);
4629 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07004630 case kChainingCellHot:
Ben Cheng00603072010-10-28 11:13:58 -07004631 handleHotChainingCell(cUnit, chainingBlock->startOffset);
Ben Chengba4fc8b2009-06-01 13:00:29 -07004632 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07004633 case kChainingCellBackwardBranch:
Jeff Hao97319a82009-08-12 16:57:15 -07004634 handleBackwardBranchChainingCell(cUnit,
Ben Cheng00603072010-10-28 11:13:58 -07004635 chainingBlock->startOffset);
Jeff Hao97319a82009-08-12 16:57:15 -07004636 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07004637 default:
Ben Cheng00603072010-10-28 11:13:58 -07004638 LOGE("Bad blocktype %d", chainingBlock->blockType);
Bill Buzbeefc519dc2010-03-06 23:30:57 -08004639 dvmCompilerAbort(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -07004640 }
4641 }
4642 }
Ben Chenge9695e52009-06-16 16:11:47 -07004643
Ben Chengcec26f62010-01-15 15:29:33 -08004644 /* Mark the bottom of chaining cells */
4645 cUnit->chainingCellBottom = (LIR *) newLIR0(cUnit, kArmChainingCellBottom);
4646
Ben Cheng6c10a972009-10-29 14:39:18 -07004647 /*
4648 * Generate the branch to the dvmJitToInterpNoChain entry point at the end
4649 * of all chaining cells for the overflow cases.
4650 */
4651 if (cUnit->switchOverflowPad) {
4652 loadConstant(cUnit, r0, (int) cUnit->switchOverflowPad);
Ben Cheng20d7e6c2011-02-18 17:12:42 -08004653 loadWordDisp(cUnit, r6SELF, offsetof(Thread,
Ben Cheng6c10a972009-10-29 14:39:18 -07004654 jitToInterpEntries.dvmJitToInterpNoChain), r2);
4655 opRegReg(cUnit, kOpAdd, r1, r1);
4656 opRegRegReg(cUnit, kOpAdd, r4PC, r0, r1);
Ben Cheng978738d2010-05-13 13:45:57 -07004657#if defined(WITH_JIT_TUNING)
Ben Cheng6c10a972009-10-29 14:39:18 -07004658 loadConstant(cUnit, r0, kSwitchOverflow);
4659#endif
4660 opReg(cUnit, kOpBlx, r2);
4661 }
4662
Ben Chenge9695e52009-06-16 16:11:47 -07004663 dvmCompilerApplyGlobalOptimizations(cUnit);
jeffhao9e45c0b2010-02-03 10:24:05 -08004664
4665#if defined(WITH_SELF_VERIFICATION)
4666 selfVerificationBranchInsertPass(cUnit);
4667#endif
Ben Chengba4fc8b2009-06-01 13:00:29 -07004668}
4669
buzbee2e152ba2010-12-15 16:32:35 -08004670/*
4671 * Accept the work and start compiling. Returns true if compilation
4672 * is attempted.
4673 */
Bill Buzbee716f1202009-07-23 13:22:09 -07004674bool dvmCompilerDoWork(CompilerWorkOrder *work)
Ben Chengba4fc8b2009-06-01 13:00:29 -07004675{
Carl Shapirofc75f3e2010-12-07 11:43:38 -08004676 JitTraceDescription *desc;
buzbee2e152ba2010-12-15 16:32:35 -08004677 bool isCompile;
4678 bool success = true;
Ben Chengba4fc8b2009-06-01 13:00:29 -07004679
Ben Cheng6999d842010-01-26 16:46:15 -08004680 if (gDvmJit.codeCacheFull) {
Ben Chengccd6c012009-10-15 14:52:45 -07004681 return false;
4682 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07004683
Ben Chengccd6c012009-10-15 14:52:45 -07004684 switch (work->kind) {
Ben Chengccd6c012009-10-15 14:52:45 -07004685 case kWorkOrderTrace:
buzbee2e152ba2010-12-15 16:32:35 -08004686 isCompile = true;
Ben Chengccd6c012009-10-15 14:52:45 -07004687 /* Start compilation with maximally allowed trace length */
Carl Shapirofc75f3e2010-12-07 11:43:38 -08004688 desc = (JitTraceDescription *)work->info;
buzbee2e152ba2010-12-15 16:32:35 -08004689 success = dvmCompileTrace(desc, JIT_MAX_TRACE_LEN, &work->result,
4690 work->bailPtr, 0 /* no hints */);
Ben Chengccd6c012009-10-15 14:52:45 -07004691 break;
4692 case kWorkOrderTraceDebug: {
4693 bool oldPrintMe = gDvmJit.printMe;
4694 gDvmJit.printMe = true;
buzbee2e152ba2010-12-15 16:32:35 -08004695 isCompile = true;
Ben Chengccd6c012009-10-15 14:52:45 -07004696 /* Start compilation with maximally allowed trace length */
Carl Shapirofc75f3e2010-12-07 11:43:38 -08004697 desc = (JitTraceDescription *)work->info;
buzbee2e152ba2010-12-15 16:32:35 -08004698 success = dvmCompileTrace(desc, JIT_MAX_TRACE_LEN, &work->result,
4699 work->bailPtr, 0 /* no hints */);
Elliott Hughes672511b2010-04-26 17:40:13 -07004700 gDvmJit.printMe = oldPrintMe;
Ben Chengccd6c012009-10-15 14:52:45 -07004701 break;
4702 }
buzbee2e152ba2010-12-15 16:32:35 -08004703 case kWorkOrderProfileMode:
4704 dvmJitChangeProfileMode((TraceProfilingModes)work->info);
4705 isCompile = false;
4706 break;
Ben Chengccd6c012009-10-15 14:52:45 -07004707 default:
buzbee2e152ba2010-12-15 16:32:35 -08004708 isCompile = false;
Bill Buzbeefc519dc2010-03-06 23:30:57 -08004709 LOGE("Jit: unknown work order type");
Elliott Hughes672511b2010-04-26 17:40:13 -07004710 assert(0); // Bail if debug build, discard otherwise
Ben Chengccd6c012009-10-15 14:52:45 -07004711 }
buzbee2e152ba2010-12-15 16:32:35 -08004712 if (!success)
4713 work->result.codeAddress = NULL;
4714 return isCompile;
Ben Chengba4fc8b2009-06-01 13:00:29 -07004715}
4716
Ben Chengba4fc8b2009-06-01 13:00:29 -07004717/* Architectural-specific debugging helpers go here */
4718void dvmCompilerArchDump(void)
4719{
4720 /* Print compiled opcode in this VM instance */
4721 int i, start, streak;
4722 char buf[1024];
4723
4724 streak = i = 0;
4725 buf[0] = 0;
Dan Bornsteinccaab182010-12-03 15:32:40 -08004726 while (opcodeCoverage[i] == 0 && i < kNumPackedOpcodes) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07004727 i++;
4728 }
Dan Bornsteinccaab182010-12-03 15:32:40 -08004729 if (i == kNumPackedOpcodes) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07004730 return;
4731 }
Dan Bornsteinccaab182010-12-03 15:32:40 -08004732 for (start = i++, streak = 1; i < kNumPackedOpcodes; i++) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07004733 if (opcodeCoverage[i]) {
4734 streak++;
4735 } else {
4736 if (streak == 1) {
4737 sprintf(buf+strlen(buf), "%x,", start);
4738 } else {
4739 sprintf(buf+strlen(buf), "%x-%x,", start, start + streak - 1);
4740 }
4741 streak = 0;
Dan Bornsteinccaab182010-12-03 15:32:40 -08004742 while (opcodeCoverage[i] == 0 && i < kNumPackedOpcodes) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07004743 i++;
4744 }
Dan Bornsteinccaab182010-12-03 15:32:40 -08004745 if (i < kNumPackedOpcodes) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07004746 streak = 1;
4747 start = i;
4748 }
4749 }
4750 }
4751 if (streak) {
4752 if (streak == 1) {
4753 sprintf(buf+strlen(buf), "%x", start);
4754 } else {
4755 sprintf(buf+strlen(buf), "%x-%x", start, start + streak - 1);
4756 }
4757 }
4758 if (strlen(buf)) {
Ben Cheng8b258bf2009-06-24 17:27:07 -07004759 LOGD("dalvik.vm.jit.op = %s", buf);
Ben Chengba4fc8b2009-06-01 13:00:29 -07004760 }
4761}
Ben Chengd7d426a2009-09-22 11:23:36 -07004762
4763/* Common initialization routine for an architecture family */
4764bool dvmCompilerArchInit()
4765{
4766 int i;
4767
Bill Buzbee1465db52009-09-23 17:17:35 -07004768 for (i = 0; i < kArmLast; i++) {
Dan Bornstein9a1f8162010-12-01 17:02:26 -08004769 if (EncodingMap[i].opcode != i) {
Ben Chengd7d426a2009-09-22 11:23:36 -07004770 LOGE("Encoding order for %s is wrong: expecting %d, seeing %d",
Dan Bornstein9a1f8162010-12-01 17:02:26 -08004771 EncodingMap[i].name, i, EncodingMap[i].opcode);
Bill Buzbeefc519dc2010-03-06 23:30:57 -08004772 dvmAbort(); // OK to dvmAbort - build error
Ben Chengd7d426a2009-09-22 11:23:36 -07004773 }
4774 }
4775
Ben Cheng5d90c202009-11-22 23:31:11 -08004776 return dvmCompilerArchVariantInit();
4777}
4778
4779void *dvmCompilerGetInterpretTemplate()
4780{
4781 return (void*) ((int)gDvmJit.codeCache +
4782 templateEntryOffsets[TEMPLATE_INTERPRET]);
4783}
4784
Bill Buzbee1b3da592011-02-03 07:38:22 -08004785JitInstructionSetType dvmCompilerGetInterpretTemplateSet()
4786{
4787 return DALVIK_JIT_ARM;
4788}
4789
buzbeebff121a2010-08-04 15:25:06 -07004790/* Needed by the Assembler */
4791void dvmCompilerSetupResourceMasks(ArmLIR *lir)
4792{
4793 setupResourceMasks(lir);
4794}
4795
Ben Cheng5d90c202009-11-22 23:31:11 -08004796/* Needed by the ld/st optmizatons */
4797ArmLIR* dvmCompilerRegCopyNoInsert(CompilationUnit *cUnit, int rDest, int rSrc)
4798{
4799 return genRegCopyNoInsert(cUnit, rDest, rSrc);
4800}
4801
4802/* Needed by the register allocator */
4803ArmLIR* dvmCompilerRegCopy(CompilationUnit *cUnit, int rDest, int rSrc)
4804{
4805 return genRegCopy(cUnit, rDest, rSrc);
4806}
4807
4808/* Needed by the register allocator */
4809void dvmCompilerRegCopyWide(CompilationUnit *cUnit, int destLo, int destHi,
4810 int srcLo, int srcHi)
4811{
4812 genRegCopyWide(cUnit, destLo, destHi, srcLo, srcHi);
4813}
4814
4815void dvmCompilerFlushRegImpl(CompilationUnit *cUnit, int rBase,
4816 int displacement, int rSrc, OpSize size)
4817{
4818 storeBaseDisp(cUnit, rBase, displacement, rSrc, size);
4819}
4820
4821void dvmCompilerFlushRegWideImpl(CompilationUnit *cUnit, int rBase,
4822 int displacement, int rSrcLo, int rSrcHi)
4823{
4824 storeBaseDispWide(cUnit, rBase, displacement, rSrcLo, rSrcHi);
Ben Chengd7d426a2009-09-22 11:23:36 -07004825}