blob: 1272f9bad7e9953bbf4ad29a8a6f3d0a13230860 [file] [log] [blame]
Ben Chengba4fc8b2009-06-01 13:00:29 -07001/*
2 * Copyright (C) 2009 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Bill Buzbee50a6bf22009-07-08 13:08:04 -070017/*
18 * This file contains codegen and support common to all supported
19 * ARM variants. It is included by:
20 *
21 * Codegen-$(TARGET_ARCH_VARIANT).c
22 *
23 * which combines this common code with specific support found in the
24 * applicable directory below this one.
25 */
26
buzbee919eb062010-07-12 12:59:22 -070027/*
28 * Mark garbage collection card. Skip if the value we're storing is null.
29 */
30static void markCard(CompilationUnit *cUnit, int valReg, int tgtAddrReg)
31{
32 int regCardBase = dvmCompilerAllocTemp(cUnit);
33 int regCardNo = dvmCompilerAllocTemp(cUnit);
buzbee8f8109a2010-08-31 10:16:35 -070034 ArmLIR *branchOver = genCmpImmBranch(cUnit, kArmCondEq, valReg, 0);
Ben Cheng20d7e6c2011-02-18 17:12:42 -080035 loadWordDisp(cUnit, r6SELF, offsetof(Thread, cardTable),
buzbee919eb062010-07-12 12:59:22 -070036 regCardBase);
37 opRegRegImm(cUnit, kOpLsr, regCardNo, tgtAddrReg, GC_CARD_SHIFT);
38 storeBaseIndexed(cUnit, regCardBase, regCardNo, regCardBase, 0,
39 kUnsignedByte);
40 ArmLIR *target = newLIR0(cUnit, kArmPseudoTargetLabel);
41 target->defMask = ENCODE_ALL;
42 branchOver->generic.target = (LIR *)target;
buzbeebaf196a2010-08-04 10:13:15 -070043 dvmCompilerFreeTemp(cUnit, regCardBase);
44 dvmCompilerFreeTemp(cUnit, regCardNo);
buzbee919eb062010-07-12 12:59:22 -070045}
46
Ben Cheng5d90c202009-11-22 23:31:11 -080047static bool genConversionCall(CompilationUnit *cUnit, MIR *mir, void *funct,
48 int srcSize, int tgtSize)
49{
50 /*
51 * Don't optimize the register usage since it calls out to template
52 * functions
53 */
54 RegLocation rlSrc;
55 RegLocation rlDest;
Bill Buzbeec6f10662010-02-09 11:16:15 -080056 dvmCompilerFlushAllRegs(cUnit); /* Send everything to home location */
Ben Cheng5d90c202009-11-22 23:31:11 -080057 if (srcSize == 1) {
Bill Buzbeec6f10662010-02-09 11:16:15 -080058 rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
Ben Cheng5d90c202009-11-22 23:31:11 -080059 loadValueDirectFixed(cUnit, rlSrc, r0);
60 } else {
Bill Buzbeec6f10662010-02-09 11:16:15 -080061 rlSrc = dvmCompilerGetSrcWide(cUnit, mir, 0, 1);
Ben Cheng5d90c202009-11-22 23:31:11 -080062 loadValueDirectWideFixed(cUnit, rlSrc, r0, r1);
63 }
Ben Chengbd1326d2010-04-02 15:04:53 -070064 LOAD_FUNC_ADDR(cUnit, r2, (int)funct);
Ben Cheng5d90c202009-11-22 23:31:11 -080065 opReg(cUnit, kOpBlx, r2);
Elliott Hughes6a555132010-02-25 15:41:42 -080066 dvmCompilerClobberCallRegs(cUnit);
Ben Cheng5d90c202009-11-22 23:31:11 -080067 if (tgtSize == 1) {
68 RegLocation rlResult;
Bill Buzbeec6f10662010-02-09 11:16:15 -080069 rlDest = dvmCompilerGetDest(cUnit, mir, 0);
70 rlResult = dvmCompilerGetReturn(cUnit);
Ben Cheng5d90c202009-11-22 23:31:11 -080071 storeValue(cUnit, rlDest, rlResult);
72 } else {
73 RegLocation rlResult;
Bill Buzbeec6f10662010-02-09 11:16:15 -080074 rlDest = dvmCompilerGetDestWide(cUnit, mir, 0, 1);
75 rlResult = dvmCompilerGetReturnWide(cUnit);
Ben Cheng5d90c202009-11-22 23:31:11 -080076 storeValueWide(cUnit, rlDest, rlResult);
77 }
78 return false;
79}
Ben Chengba4fc8b2009-06-01 13:00:29 -070080
Ben Cheng5d90c202009-11-22 23:31:11 -080081static bool genArithOpFloatPortable(CompilationUnit *cUnit, MIR *mir,
82 RegLocation rlDest, RegLocation rlSrc1,
83 RegLocation rlSrc2)
84{
85 RegLocation rlResult;
86 void* funct;
87
Dan Bornstein9a1f8162010-12-01 17:02:26 -080088 switch (mir->dalvikInsn.opcode) {
Ben Cheng5d90c202009-11-22 23:31:11 -080089 case OP_ADD_FLOAT_2ADDR:
90 case OP_ADD_FLOAT:
91 funct = (void*) __aeabi_fadd;
92 break;
93 case OP_SUB_FLOAT_2ADDR:
94 case OP_SUB_FLOAT:
95 funct = (void*) __aeabi_fsub;
96 break;
97 case OP_DIV_FLOAT_2ADDR:
98 case OP_DIV_FLOAT:
99 funct = (void*) __aeabi_fdiv;
100 break;
101 case OP_MUL_FLOAT_2ADDR:
102 case OP_MUL_FLOAT:
103 funct = (void*) __aeabi_fmul;
104 break;
105 case OP_REM_FLOAT_2ADDR:
106 case OP_REM_FLOAT:
107 funct = (void*) fmodf;
108 break;
109 case OP_NEG_FLOAT: {
110 genNegFloat(cUnit, rlDest, rlSrc1);
111 return false;
112 }
113 default:
114 return true;
115 }
Bill Buzbeec6f10662010-02-09 11:16:15 -0800116 dvmCompilerFlushAllRegs(cUnit); /* Send everything to home location */
Ben Cheng5d90c202009-11-22 23:31:11 -0800117 loadValueDirectFixed(cUnit, rlSrc1, r0);
118 loadValueDirectFixed(cUnit, rlSrc2, r1);
Ben Chengbd1326d2010-04-02 15:04:53 -0700119 LOAD_FUNC_ADDR(cUnit, r2, (int)funct);
Ben Cheng5d90c202009-11-22 23:31:11 -0800120 opReg(cUnit, kOpBlx, r2);
Elliott Hughes6a555132010-02-25 15:41:42 -0800121 dvmCompilerClobberCallRegs(cUnit);
Bill Buzbeec6f10662010-02-09 11:16:15 -0800122 rlResult = dvmCompilerGetReturn(cUnit);
Ben Cheng5d90c202009-11-22 23:31:11 -0800123 storeValue(cUnit, rlDest, rlResult);
124 return false;
125}
126
127static bool genArithOpDoublePortable(CompilationUnit *cUnit, MIR *mir,
128 RegLocation rlDest, RegLocation rlSrc1,
129 RegLocation rlSrc2)
130{
131 RegLocation rlResult;
132 void* funct;
133
Dan Bornstein9a1f8162010-12-01 17:02:26 -0800134 switch (mir->dalvikInsn.opcode) {
Ben Cheng5d90c202009-11-22 23:31:11 -0800135 case OP_ADD_DOUBLE_2ADDR:
136 case OP_ADD_DOUBLE:
137 funct = (void*) __aeabi_dadd;
138 break;
139 case OP_SUB_DOUBLE_2ADDR:
140 case OP_SUB_DOUBLE:
141 funct = (void*) __aeabi_dsub;
142 break;
143 case OP_DIV_DOUBLE_2ADDR:
144 case OP_DIV_DOUBLE:
145 funct = (void*) __aeabi_ddiv;
146 break;
147 case OP_MUL_DOUBLE_2ADDR:
148 case OP_MUL_DOUBLE:
149 funct = (void*) __aeabi_dmul;
150 break;
151 case OP_REM_DOUBLE_2ADDR:
152 case OP_REM_DOUBLE:
153 funct = (void*) fmod;
154 break;
155 case OP_NEG_DOUBLE: {
156 genNegDouble(cUnit, rlDest, rlSrc1);
157 return false;
158 }
159 default:
160 return true;
161 }
Bill Buzbeec6f10662010-02-09 11:16:15 -0800162 dvmCompilerFlushAllRegs(cUnit); /* Send everything to home location */
Ben Cheng20d7e6c2011-02-18 17:12:42 -0800163 LOAD_FUNC_ADDR(cUnit, r14lr, (int)funct);
Ben Cheng5d90c202009-11-22 23:31:11 -0800164 loadValueDirectWideFixed(cUnit, rlSrc1, r0, r1);
165 loadValueDirectWideFixed(cUnit, rlSrc2, r2, r3);
Ben Cheng20d7e6c2011-02-18 17:12:42 -0800166 opReg(cUnit, kOpBlx, r14lr);
Elliott Hughes6a555132010-02-25 15:41:42 -0800167 dvmCompilerClobberCallRegs(cUnit);
Bill Buzbeec6f10662010-02-09 11:16:15 -0800168 rlResult = dvmCompilerGetReturnWide(cUnit);
Ben Cheng5d90c202009-11-22 23:31:11 -0800169 storeValueWide(cUnit, rlDest, rlResult);
Ben Chengd72564c2011-02-08 17:09:25 -0800170#if defined(WITH_SELF_VERIFICATION)
171 cUnit->usesLinkRegister = true;
172#endif
Ben Cheng5d90c202009-11-22 23:31:11 -0800173 return false;
174}
175
176static bool genConversionPortable(CompilationUnit *cUnit, MIR *mir)
177{
Dan Bornstein9a1f8162010-12-01 17:02:26 -0800178 Opcode opcode = mir->dalvikInsn.opcode;
Ben Cheng5d90c202009-11-22 23:31:11 -0800179
Dan Bornstein9a1f8162010-12-01 17:02:26 -0800180 switch (opcode) {
Ben Cheng5d90c202009-11-22 23:31:11 -0800181 case OP_INT_TO_FLOAT:
182 return genConversionCall(cUnit, mir, (void*)__aeabi_i2f, 1, 1);
183 case OP_FLOAT_TO_INT:
184 return genConversionCall(cUnit, mir, (void*)__aeabi_f2iz, 1, 1);
185 case OP_DOUBLE_TO_FLOAT:
186 return genConversionCall(cUnit, mir, (void*)__aeabi_d2f, 2, 1);
187 case OP_FLOAT_TO_DOUBLE:
188 return genConversionCall(cUnit, mir, (void*)__aeabi_f2d, 1, 2);
189 case OP_INT_TO_DOUBLE:
190 return genConversionCall(cUnit, mir, (void*)__aeabi_i2d, 1, 2);
191 case OP_DOUBLE_TO_INT:
192 return genConversionCall(cUnit, mir, (void*)__aeabi_d2iz, 2, 1);
193 case OP_FLOAT_TO_LONG:
194 return genConversionCall(cUnit, mir, (void*)dvmJitf2l, 1, 2);
195 case OP_LONG_TO_FLOAT:
196 return genConversionCall(cUnit, mir, (void*)__aeabi_l2f, 2, 1);
197 case OP_DOUBLE_TO_LONG:
198 return genConversionCall(cUnit, mir, (void*)dvmJitd2l, 2, 2);
199 case OP_LONG_TO_DOUBLE:
200 return genConversionCall(cUnit, mir, (void*)__aeabi_l2d, 2, 2);
201 default:
202 return true;
203 }
204 return false;
205}
Ben Chengba4fc8b2009-06-01 13:00:29 -0700206
Jeff Hao97319a82009-08-12 16:57:15 -0700207#if defined(WITH_SELF_VERIFICATION)
Dan Bornstein9a1f8162010-12-01 17:02:26 -0800208static void selfVerificationBranchInsert(LIR *currentLIR, ArmOpcode opcode,
jeffhao9e45c0b2010-02-03 10:24:05 -0800209 int dest, int src1)
Jeff Hao97319a82009-08-12 16:57:15 -0700210{
Carl Shapirofc75f3e2010-12-07 11:43:38 -0800211 ArmLIR *insn = (ArmLIR *) dvmCompilerNew(sizeof(ArmLIR), true);
Dan Bornstein9a1f8162010-12-01 17:02:26 -0800212 insn->opcode = opcode;
jeffhao9e45c0b2010-02-03 10:24:05 -0800213 insn->operands[0] = dest;
214 insn->operands[1] = src1;
215 setupResourceMasks(insn);
216 dvmCompilerInsertLIRBefore(currentLIR, (LIR *) insn);
Jeff Hao97319a82009-08-12 16:57:15 -0700217}
218
Ben Chengd72564c2011-02-08 17:09:25 -0800219/*
220 * Example where r14 (LR) is preserved around a heap access under
221 * self-verification mode in Thumb2:
222 *
Ben Cheng20d7e6c2011-02-18 17:12:42 -0800223 * D/dalvikvm( 1538): 0x59414c5e (0026): ldr r14, [r15pc, #220] <-hoisted
Ben Chengd72564c2011-02-08 17:09:25 -0800224 * D/dalvikvm( 1538): 0x59414c62 (002a): mla r4, r0, r8, r4
225 * D/dalvikvm( 1538): 0x59414c66 (002e): adds r3, r4, r3
226 * D/dalvikvm( 1538): 0x59414c6a (0032): push <r5, r14> ---+
227 * D/dalvikvm( 1538): 0x59414c6c (0034): blx_1 0x5940f494 |
228 * D/dalvikvm( 1538): 0x59414c6e (0036): blx_2 see above <-MEM_OP_DECODE
229 * D/dalvikvm( 1538): 0x59414c70 (0038): ldr r10, [r9, #0] |
230 * D/dalvikvm( 1538): 0x59414c74 (003c): pop <r5, r14> ---+
231 * D/dalvikvm( 1538): 0x59414c78 (0040): mov r11, r10
232 * D/dalvikvm( 1538): 0x59414c7a (0042): asr r12, r11, #31
233 * D/dalvikvm( 1538): 0x59414c7e (0046): movs r0, r2
234 * D/dalvikvm( 1538): 0x59414c80 (0048): movs r1, r3
235 * D/dalvikvm( 1538): 0x59414c82 (004a): str r2, [r5, #16]
236 * D/dalvikvm( 1538): 0x59414c84 (004c): mov r2, r11
237 * D/dalvikvm( 1538): 0x59414c86 (004e): str r3, [r5, #20]
238 * D/dalvikvm( 1538): 0x59414c88 (0050): mov r3, r12
239 * D/dalvikvm( 1538): 0x59414c8a (0052): str r11, [r5, #24]
240 * D/dalvikvm( 1538): 0x59414c8e (0056): str r12, [r5, #28]
241 * D/dalvikvm( 1538): 0x59414c92 (005a): blx r14 <-use of LR
242 *
243 */
jeffhao9e45c0b2010-02-03 10:24:05 -0800244static void selfVerificationBranchInsertPass(CompilationUnit *cUnit)
Jeff Hao97319a82009-08-12 16:57:15 -0700245{
jeffhao9e45c0b2010-02-03 10:24:05 -0800246 ArmLIR *thisLIR;
Dan Bornstein9a1f8162010-12-01 17:02:26 -0800247 TemplateOpcode opcode = TEMPLATE_MEM_OP_DECODE;
Jeff Hao97319a82009-08-12 16:57:15 -0700248
jeffhao9e45c0b2010-02-03 10:24:05 -0800249 for (thisLIR = (ArmLIR *) cUnit->firstLIRInsn;
250 thisLIR != (ArmLIR *) cUnit->lastLIRInsn;
251 thisLIR = NEXT_LIR(thisLIR)) {
Ben Chengd72564c2011-02-08 17:09:25 -0800252 if (!thisLIR->flags.isNop && thisLIR->flags.insertWrapper) {
253 /*
254 * Push r5(FP) and r14(LR) onto stack. We need to make sure that
255 * SP is 8-byte aligned, and we use r5 as a temp to restore LR
256 * for Thumb-only target since LR cannot be directly accessed in
257 * Thumb mode. Another reason to choose r5 here is it is the Dalvik
258 * frame pointer and cannot be the target of the emulated heap
259 * load.
260 */
261 if (cUnit->usesLinkRegister) {
262 genSelfVerificationPreBranch(cUnit, thisLIR);
263 }
264
jeffhao9e45c0b2010-02-03 10:24:05 -0800265 /* Branch to mem op decode template */
266 selfVerificationBranchInsert((LIR *) thisLIR, kThumbBlx1,
Dan Bornstein9a1f8162010-12-01 17:02:26 -0800267 (int) gDvmJit.codeCache + templateEntryOffsets[opcode],
268 (int) gDvmJit.codeCache + templateEntryOffsets[opcode]);
jeffhao9e45c0b2010-02-03 10:24:05 -0800269 selfVerificationBranchInsert((LIR *) thisLIR, kThumbBlx2,
Dan Bornstein9a1f8162010-12-01 17:02:26 -0800270 (int) gDvmJit.codeCache + templateEntryOffsets[opcode],
271 (int) gDvmJit.codeCache + templateEntryOffsets[opcode]);
Ben Chengd72564c2011-02-08 17:09:25 -0800272
273 /* Restore LR */
274 if (cUnit->usesLinkRegister) {
275 genSelfVerificationPostBranch(cUnit, thisLIR);
276 }
Jeff Hao97319a82009-08-12 16:57:15 -0700277 }
278 }
Jeff Hao97319a82009-08-12 16:57:15 -0700279}
Jeff Hao97319a82009-08-12 16:57:15 -0700280#endif
281
Bill Buzbeebe6534f2010-03-12 16:01:35 -0800282/* Generate conditional branch instructions */
283static ArmLIR *genConditionalBranch(CompilationUnit *cUnit,
284 ArmConditionCode cond,
285 ArmLIR *target)
286{
287 ArmLIR *branch = opCondBranch(cUnit, cond);
288 branch->generic.target = (LIR *) target;
289 return branch;
290}
291
Ben Chengba4fc8b2009-06-01 13:00:29 -0700292/* Generate a unconditional branch to go to the interpreter */
Bill Buzbee89efc3d2009-07-28 11:22:22 -0700293static inline ArmLIR *genTrap(CompilationUnit *cUnit, int dOffset,
294 ArmLIR *pcrLabel)
Ben Chengba4fc8b2009-06-01 13:00:29 -0700295{
Bill Buzbee1465db52009-09-23 17:17:35 -0700296 ArmLIR *branch = opNone(cUnit, kOpUncondBr);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700297 return genCheckCommon(cUnit, dOffset, branch, pcrLabel);
298}
299
300/* Load a wide field from an object instance */
301static void genIGetWide(CompilationUnit *cUnit, MIR *mir, int fieldOffset)
302{
Bill Buzbeec6f10662010-02-09 11:16:15 -0800303 RegLocation rlObj = dvmCompilerGetSrc(cUnit, mir, 0);
304 RegLocation rlDest = dvmCompilerGetDestWide(cUnit, mir, 0, 1);
Bill Buzbee1465db52009-09-23 17:17:35 -0700305 RegLocation rlResult;
306 rlObj = loadValue(cUnit, rlObj, kCoreReg);
Bill Buzbeec6f10662010-02-09 11:16:15 -0800307 int regPtr = dvmCompilerAllocTemp(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700308
Bill Buzbee1465db52009-09-23 17:17:35 -0700309 assert(rlDest.wide);
Ben Chenge9695e52009-06-16 16:11:47 -0700310
Bill Buzbee1465db52009-09-23 17:17:35 -0700311 genNullCheck(cUnit, rlObj.sRegLow, rlObj.lowReg, mir->offset,
312 NULL);/* null object? */
313 opRegRegImm(cUnit, kOpAdd, regPtr, rlObj.lowReg, fieldOffset);
Bill Buzbeec6f10662010-02-09 11:16:15 -0800314 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kAnyReg, true);
Ben Cheng11d8f142010-03-24 15:24:19 -0700315
316 HEAP_ACCESS_SHADOW(true);
Bill Buzbee1465db52009-09-23 17:17:35 -0700317 loadPair(cUnit, regPtr, rlResult.lowReg, rlResult.highReg);
Ben Cheng11d8f142010-03-24 15:24:19 -0700318 HEAP_ACCESS_SHADOW(false);
319
Bill Buzbeec6f10662010-02-09 11:16:15 -0800320 dvmCompilerFreeTemp(cUnit, regPtr);
Bill Buzbee1465db52009-09-23 17:17:35 -0700321 storeValueWide(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700322}
323
324/* Store a wide field to an object instance */
325static void genIPutWide(CompilationUnit *cUnit, MIR *mir, int fieldOffset)
326{
Bill Buzbeec6f10662010-02-09 11:16:15 -0800327 RegLocation rlSrc = dvmCompilerGetSrcWide(cUnit, mir, 0, 1);
328 RegLocation rlObj = dvmCompilerGetSrc(cUnit, mir, 2);
Bill Buzbee1465db52009-09-23 17:17:35 -0700329 rlObj = loadValue(cUnit, rlObj, kCoreReg);
330 int regPtr;
331 rlSrc = loadValueWide(cUnit, rlSrc, kAnyReg);
332 genNullCheck(cUnit, rlObj.sRegLow, rlObj.lowReg, mir->offset,
333 NULL);/* null object? */
Bill Buzbeec6f10662010-02-09 11:16:15 -0800334 regPtr = dvmCompilerAllocTemp(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -0700335 opRegRegImm(cUnit, kOpAdd, regPtr, rlObj.lowReg, fieldOffset);
Ben Cheng11d8f142010-03-24 15:24:19 -0700336
337 HEAP_ACCESS_SHADOW(true);
Bill Buzbee1465db52009-09-23 17:17:35 -0700338 storePair(cUnit, regPtr, rlSrc.lowReg, rlSrc.highReg);
Ben Cheng11d8f142010-03-24 15:24:19 -0700339 HEAP_ACCESS_SHADOW(false);
340
Bill Buzbeec6f10662010-02-09 11:16:15 -0800341 dvmCompilerFreeTemp(cUnit, regPtr);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700342}
343
344/*
345 * Load a field from an object instance
346 *
Ben Chengba4fc8b2009-06-01 13:00:29 -0700347 */
Bill Buzbee270c1d62009-08-13 16:58:07 -0700348static void genIGet(CompilationUnit *cUnit, MIR *mir, OpSize size,
buzbeeecf8f6e2010-07-20 14:53:42 -0700349 int fieldOffset, bool isVolatile)
Ben Chengba4fc8b2009-06-01 13:00:29 -0700350{
Bill Buzbee1465db52009-09-23 17:17:35 -0700351 RegLocation rlResult;
Bill Buzbee749e8162010-07-07 06:55:56 -0700352 RegisterClass regClass = dvmCompilerRegClassBySize(size);
Bill Buzbeec6f10662010-02-09 11:16:15 -0800353 RegLocation rlObj = dvmCompilerGetSrc(cUnit, mir, 0);
354 RegLocation rlDest = dvmCompilerGetDest(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -0700355 rlObj = loadValue(cUnit, rlObj, kCoreReg);
Bill Buzbee749e8162010-07-07 06:55:56 -0700356 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, regClass, true);
Bill Buzbee1465db52009-09-23 17:17:35 -0700357 genNullCheck(cUnit, rlObj.sRegLow, rlObj.lowReg, mir->offset,
358 NULL);/* null object? */
Ben Cheng11d8f142010-03-24 15:24:19 -0700359
360 HEAP_ACCESS_SHADOW(true);
Ben Cheng5d90c202009-11-22 23:31:11 -0800361 loadBaseDisp(cUnit, mir, rlObj.lowReg, fieldOffset, rlResult.lowReg,
362 size, rlObj.sRegLow);
Ben Cheng11d8f142010-03-24 15:24:19 -0700363 HEAP_ACCESS_SHADOW(false);
buzbeeecf8f6e2010-07-20 14:53:42 -0700364 if (isVolatile) {
buzbee2ce33c92010-11-01 15:53:27 -0700365 dvmCompilerGenMemBarrier(cUnit, kSY);
buzbeeecf8f6e2010-07-20 14:53:42 -0700366 }
Ben Cheng11d8f142010-03-24 15:24:19 -0700367
Bill Buzbee1465db52009-09-23 17:17:35 -0700368 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700369}
370
371/*
372 * Store a field to an object instance
373 *
Ben Chengba4fc8b2009-06-01 13:00:29 -0700374 */
Bill Buzbee270c1d62009-08-13 16:58:07 -0700375static void genIPut(CompilationUnit *cUnit, MIR *mir, OpSize size,
buzbeeecf8f6e2010-07-20 14:53:42 -0700376 int fieldOffset, bool isObject, bool isVolatile)
Ben Chengba4fc8b2009-06-01 13:00:29 -0700377{
Bill Buzbee749e8162010-07-07 06:55:56 -0700378 RegisterClass regClass = dvmCompilerRegClassBySize(size);
Bill Buzbeec6f10662010-02-09 11:16:15 -0800379 RegLocation rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
380 RegLocation rlObj = dvmCompilerGetSrc(cUnit, mir, 1);
Bill Buzbee1465db52009-09-23 17:17:35 -0700381 rlObj = loadValue(cUnit, rlObj, kCoreReg);
Bill Buzbee749e8162010-07-07 06:55:56 -0700382 rlSrc = loadValue(cUnit, rlSrc, regClass);
Bill Buzbee1465db52009-09-23 17:17:35 -0700383 genNullCheck(cUnit, rlObj.sRegLow, rlObj.lowReg, mir->offset,
384 NULL);/* null object? */
Ben Cheng11d8f142010-03-24 15:24:19 -0700385
buzbeeecf8f6e2010-07-20 14:53:42 -0700386 if (isVolatile) {
buzbee2ce33c92010-11-01 15:53:27 -0700387 dvmCompilerGenMemBarrier(cUnit, kSY);
buzbeeecf8f6e2010-07-20 14:53:42 -0700388 }
Ben Cheng11d8f142010-03-24 15:24:19 -0700389 HEAP_ACCESS_SHADOW(true);
Bill Buzbee1465db52009-09-23 17:17:35 -0700390 storeBaseDisp(cUnit, rlObj.lowReg, fieldOffset, rlSrc.lowReg, size);
Ben Cheng11d8f142010-03-24 15:24:19 -0700391 HEAP_ACCESS_SHADOW(false);
buzbee919eb062010-07-12 12:59:22 -0700392 if (isObject) {
393 /* NOTE: marking card based on object head */
394 markCard(cUnit, rlSrc.lowReg, rlObj.lowReg);
395 }
Ben Chengba4fc8b2009-06-01 13:00:29 -0700396}
397
398
Ben Chengba4fc8b2009-06-01 13:00:29 -0700399/*
400 * Generate array load
Ben Chengba4fc8b2009-06-01 13:00:29 -0700401 */
Bill Buzbee270c1d62009-08-13 16:58:07 -0700402static void genArrayGet(CompilationUnit *cUnit, MIR *mir, OpSize size,
Bill Buzbee1465db52009-09-23 17:17:35 -0700403 RegLocation rlArray, RegLocation rlIndex,
404 RegLocation rlDest, int scale)
Ben Chengba4fc8b2009-06-01 13:00:29 -0700405{
Bill Buzbee749e8162010-07-07 06:55:56 -0700406 RegisterClass regClass = dvmCompilerRegClassBySize(size);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700407 int lenOffset = offsetof(ArrayObject, length);
408 int dataOffset = offsetof(ArrayObject, contents);
Bill Buzbee1465db52009-09-23 17:17:35 -0700409 RegLocation rlResult;
410 rlArray = loadValue(cUnit, rlArray, kCoreReg);
411 rlIndex = loadValue(cUnit, rlIndex, kCoreReg);
412 int regPtr;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700413
414 /* null object? */
Ben Cheng4238ec22009-08-24 16:32:22 -0700415 ArmLIR * pcrLabel = NULL;
416
417 if (!(mir->OptimizationFlags & MIR_IGNORE_NULL_CHECK)) {
Bill Buzbee1465db52009-09-23 17:17:35 -0700418 pcrLabel = genNullCheck(cUnit, rlArray.sRegLow,
419 rlArray.lowReg, mir->offset, NULL);
Ben Cheng4238ec22009-08-24 16:32:22 -0700420 }
421
Bill Buzbeec6f10662010-02-09 11:16:15 -0800422 regPtr = dvmCompilerAllocTemp(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -0700423
Ben Cheng4238ec22009-08-24 16:32:22 -0700424 if (!(mir->OptimizationFlags & MIR_IGNORE_RANGE_CHECK)) {
Bill Buzbeec6f10662010-02-09 11:16:15 -0800425 int regLen = dvmCompilerAllocTemp(cUnit);
Ben Cheng4238ec22009-08-24 16:32:22 -0700426 /* Get len */
Bill Buzbee1465db52009-09-23 17:17:35 -0700427 loadWordDisp(cUnit, rlArray.lowReg, lenOffset, regLen);
428 /* regPtr -> array data */
429 opRegRegImm(cUnit, kOpAdd, regPtr, rlArray.lowReg, dataOffset);
430 genBoundsCheck(cUnit, rlIndex.lowReg, regLen, mir->offset,
431 pcrLabel);
Bill Buzbeec6f10662010-02-09 11:16:15 -0800432 dvmCompilerFreeTemp(cUnit, regLen);
Ben Cheng4238ec22009-08-24 16:32:22 -0700433 } else {
Bill Buzbee1465db52009-09-23 17:17:35 -0700434 /* regPtr -> array data */
435 opRegRegImm(cUnit, kOpAdd, regPtr, rlArray.lowReg, dataOffset);
Ben Cheng4238ec22009-08-24 16:32:22 -0700436 }
Bill Buzbee1465db52009-09-23 17:17:35 -0700437 if ((size == kLong) || (size == kDouble)) {
438 if (scale) {
Bill Buzbeec6f10662010-02-09 11:16:15 -0800439 int rNewIndex = dvmCompilerAllocTemp(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -0700440 opRegRegImm(cUnit, kOpLsl, rNewIndex, rlIndex.lowReg, scale);
441 opRegReg(cUnit, kOpAdd, regPtr, rNewIndex);
Bill Buzbeec6f10662010-02-09 11:16:15 -0800442 dvmCompilerFreeTemp(cUnit, rNewIndex);
Bill Buzbee1465db52009-09-23 17:17:35 -0700443 } else {
444 opRegReg(cUnit, kOpAdd, regPtr, rlIndex.lowReg);
445 }
Bill Buzbee749e8162010-07-07 06:55:56 -0700446 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, regClass, true);
Ben Cheng11d8f142010-03-24 15:24:19 -0700447
448 HEAP_ACCESS_SHADOW(true);
Bill Buzbee1465db52009-09-23 17:17:35 -0700449 loadPair(cUnit, regPtr, rlResult.lowReg, rlResult.highReg);
Ben Cheng11d8f142010-03-24 15:24:19 -0700450 HEAP_ACCESS_SHADOW(false);
451
Bill Buzbeec6f10662010-02-09 11:16:15 -0800452 dvmCompilerFreeTemp(cUnit, regPtr);
Bill Buzbee1465db52009-09-23 17:17:35 -0700453 storeValueWide(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700454 } else {
Bill Buzbee749e8162010-07-07 06:55:56 -0700455 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, regClass, true);
Ben Cheng11d8f142010-03-24 15:24:19 -0700456
457 HEAP_ACCESS_SHADOW(true);
Bill Buzbee1465db52009-09-23 17:17:35 -0700458 loadBaseIndexed(cUnit, regPtr, rlIndex.lowReg, rlResult.lowReg,
459 scale, size);
Ben Cheng11d8f142010-03-24 15:24:19 -0700460 HEAP_ACCESS_SHADOW(false);
461
Bill Buzbeec6f10662010-02-09 11:16:15 -0800462 dvmCompilerFreeTemp(cUnit, regPtr);
Bill Buzbee1465db52009-09-23 17:17:35 -0700463 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700464 }
465}
466
Ben Chengba4fc8b2009-06-01 13:00:29 -0700467/*
468 * Generate array store
469 *
Ben Chengba4fc8b2009-06-01 13:00:29 -0700470 */
Bill Buzbee270c1d62009-08-13 16:58:07 -0700471static void genArrayPut(CompilationUnit *cUnit, MIR *mir, OpSize size,
Bill Buzbee1465db52009-09-23 17:17:35 -0700472 RegLocation rlArray, RegLocation rlIndex,
473 RegLocation rlSrc, int scale)
Ben Chengba4fc8b2009-06-01 13:00:29 -0700474{
Bill Buzbee749e8162010-07-07 06:55:56 -0700475 RegisterClass regClass = dvmCompilerRegClassBySize(size);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700476 int lenOffset = offsetof(ArrayObject, length);
477 int dataOffset = offsetof(ArrayObject, contents);
478
Bill Buzbee1465db52009-09-23 17:17:35 -0700479 int regPtr;
480 rlArray = loadValue(cUnit, rlArray, kCoreReg);
481 rlIndex = loadValue(cUnit, rlIndex, kCoreReg);
Ben Chenge9695e52009-06-16 16:11:47 -0700482
Bill Buzbeec6f10662010-02-09 11:16:15 -0800483 if (dvmCompilerIsTemp(cUnit, rlArray.lowReg)) {
484 dvmCompilerClobber(cUnit, rlArray.lowReg);
Bill Buzbee1465db52009-09-23 17:17:35 -0700485 regPtr = rlArray.lowReg;
486 } else {
Bill Buzbeec6f10662010-02-09 11:16:15 -0800487 regPtr = dvmCompilerAllocTemp(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -0700488 genRegCopy(cUnit, regPtr, rlArray.lowReg);
489 }
Ben Chenge9695e52009-06-16 16:11:47 -0700490
Ben Cheng1efc9c52009-06-08 18:25:27 -0700491 /* null object? */
Ben Cheng4238ec22009-08-24 16:32:22 -0700492 ArmLIR * pcrLabel = NULL;
493
494 if (!(mir->OptimizationFlags & MIR_IGNORE_NULL_CHECK)) {
Bill Buzbee1465db52009-09-23 17:17:35 -0700495 pcrLabel = genNullCheck(cUnit, rlArray.sRegLow, rlArray.lowReg,
496 mir->offset, NULL);
Ben Cheng4238ec22009-08-24 16:32:22 -0700497 }
498
499 if (!(mir->OptimizationFlags & MIR_IGNORE_RANGE_CHECK)) {
Bill Buzbeec6f10662010-02-09 11:16:15 -0800500 int regLen = dvmCompilerAllocTemp(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -0700501 //NOTE: max live temps(4) here.
Ben Cheng4238ec22009-08-24 16:32:22 -0700502 /* Get len */
Bill Buzbee1465db52009-09-23 17:17:35 -0700503 loadWordDisp(cUnit, rlArray.lowReg, lenOffset, regLen);
504 /* regPtr -> array data */
505 opRegImm(cUnit, kOpAdd, regPtr, dataOffset);
506 genBoundsCheck(cUnit, rlIndex.lowReg, regLen, mir->offset,
507 pcrLabel);
Bill Buzbeec6f10662010-02-09 11:16:15 -0800508 dvmCompilerFreeTemp(cUnit, regLen);
Ben Cheng4238ec22009-08-24 16:32:22 -0700509 } else {
Bill Buzbee1465db52009-09-23 17:17:35 -0700510 /* regPtr -> array data */
511 opRegImm(cUnit, kOpAdd, regPtr, dataOffset);
Ben Cheng4238ec22009-08-24 16:32:22 -0700512 }
Bill Buzbee1465db52009-09-23 17:17:35 -0700513 /* at this point, regPtr points to array, 2 live temps */
Bill Buzbee1465db52009-09-23 17:17:35 -0700514 if ((size == kLong) || (size == kDouble)) {
515 //TODO: need specific wide routine that can handle fp regs
516 if (scale) {
Bill Buzbeec6f10662010-02-09 11:16:15 -0800517 int rNewIndex = dvmCompilerAllocTemp(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -0700518 opRegRegImm(cUnit, kOpLsl, rNewIndex, rlIndex.lowReg, scale);
519 opRegReg(cUnit, kOpAdd, regPtr, rNewIndex);
Bill Buzbeec6f10662010-02-09 11:16:15 -0800520 dvmCompilerFreeTemp(cUnit, rNewIndex);
Bill Buzbee1465db52009-09-23 17:17:35 -0700521 } else {
522 opRegReg(cUnit, kOpAdd, regPtr, rlIndex.lowReg);
523 }
Bill Buzbee749e8162010-07-07 06:55:56 -0700524 rlSrc = loadValueWide(cUnit, rlSrc, regClass);
Ben Cheng11d8f142010-03-24 15:24:19 -0700525
526 HEAP_ACCESS_SHADOW(true);
Bill Buzbee1465db52009-09-23 17:17:35 -0700527 storePair(cUnit, regPtr, rlSrc.lowReg, rlSrc.highReg);
Ben Cheng11d8f142010-03-24 15:24:19 -0700528 HEAP_ACCESS_SHADOW(false);
529
Bill Buzbeec6f10662010-02-09 11:16:15 -0800530 dvmCompilerFreeTemp(cUnit, regPtr);
Bill Buzbee270c1d62009-08-13 16:58:07 -0700531 } else {
Bill Buzbee749e8162010-07-07 06:55:56 -0700532 rlSrc = loadValue(cUnit, rlSrc, regClass);
Ben Cheng11d8f142010-03-24 15:24:19 -0700533
534 HEAP_ACCESS_SHADOW(true);
Bill Buzbee1465db52009-09-23 17:17:35 -0700535 storeBaseIndexed(cUnit, regPtr, rlIndex.lowReg, rlSrc.lowReg,
536 scale, size);
Ben Cheng11d8f142010-03-24 15:24:19 -0700537 HEAP_ACCESS_SHADOW(false);
jeffhao9e45c0b2010-02-03 10:24:05 -0800538 }
Ben Chengba4fc8b2009-06-01 13:00:29 -0700539}
540
Bill Buzbeebe6534f2010-03-12 16:01:35 -0800541/*
542 * Generate array object store
543 * Must use explicit register allocation here because of
544 * call-out to dvmCanPutArrayElement
545 */
546static void genArrayObjectPut(CompilationUnit *cUnit, MIR *mir,
547 RegLocation rlArray, RegLocation rlIndex,
548 RegLocation rlSrc, int scale)
549{
550 int lenOffset = offsetof(ArrayObject, length);
551 int dataOffset = offsetof(ArrayObject, contents);
552
553 dvmCompilerFlushAllRegs(cUnit);
554
555 int regLen = r0;
556 int regPtr = r4PC; /* Preserved across call */
557 int regArray = r1;
558 int regIndex = r7; /* Preserved across call */
559
560 loadValueDirectFixed(cUnit, rlArray, regArray);
561 loadValueDirectFixed(cUnit, rlIndex, regIndex);
562
563 /* null object? */
564 ArmLIR * pcrLabel = NULL;
565
566 if (!(mir->OptimizationFlags & MIR_IGNORE_NULL_CHECK)) {
567 pcrLabel = genNullCheck(cUnit, rlArray.sRegLow, regArray,
568 mir->offset, NULL);
569 }
570
571 if (!(mir->OptimizationFlags & MIR_IGNORE_RANGE_CHECK)) {
572 /* Get len */
573 loadWordDisp(cUnit, regArray, lenOffset, regLen);
574 /* regPtr -> array data */
575 opRegRegImm(cUnit, kOpAdd, regPtr, regArray, dataOffset);
576 genBoundsCheck(cUnit, regIndex, regLen, mir->offset,
577 pcrLabel);
578 } else {
579 /* regPtr -> array data */
580 opRegRegImm(cUnit, kOpAdd, regPtr, regArray, dataOffset);
581 }
582
583 /* Get object to store */
584 loadValueDirectFixed(cUnit, rlSrc, r0);
Ben Chengbd1326d2010-04-02 15:04:53 -0700585 LOAD_FUNC_ADDR(cUnit, r2, (int)dvmCanPutArrayElement);
Bill Buzbeebe6534f2010-03-12 16:01:35 -0800586
587 /* Are we storing null? If so, avoid check */
buzbee8f8109a2010-08-31 10:16:35 -0700588 ArmLIR *branchOver = genCmpImmBranch(cUnit, kArmCondEq, r0, 0);
Bill Buzbeebe6534f2010-03-12 16:01:35 -0800589
590 /* Make sure the types are compatible */
591 loadWordDisp(cUnit, regArray, offsetof(Object, clazz), r1);
592 loadWordDisp(cUnit, r0, offsetof(Object, clazz), r0);
593 opReg(cUnit, kOpBlx, r2);
594 dvmCompilerClobberCallRegs(cUnit);
Bill Buzbee900a3af2010-03-16 12:41:43 -0700595
596 /*
597 * Using fixed registers here, and counting on r4 and r7 being
598 * preserved across the above call. Tell the register allocation
599 * utilities about the regs we are using directly
600 */
601 dvmCompilerLockTemp(cUnit, regPtr); // r4PC
602 dvmCompilerLockTemp(cUnit, regIndex); // r7
603 dvmCompilerLockTemp(cUnit, r0);
buzbee919eb062010-07-12 12:59:22 -0700604 dvmCompilerLockTemp(cUnit, r1);
Bill Buzbee900a3af2010-03-16 12:41:43 -0700605
Bill Buzbeebe6534f2010-03-12 16:01:35 -0800606 /* Bad? - roll back and re-execute if so */
607 genRegImmCheck(cUnit, kArmCondEq, r0, 0, mir->offset, pcrLabel);
608
buzbee919eb062010-07-12 12:59:22 -0700609 /* Resume here - must reload element & array, regPtr & index preserved */
Bill Buzbeebe6534f2010-03-12 16:01:35 -0800610 loadValueDirectFixed(cUnit, rlSrc, r0);
buzbee919eb062010-07-12 12:59:22 -0700611 loadValueDirectFixed(cUnit, rlArray, r1);
Bill Buzbeebe6534f2010-03-12 16:01:35 -0800612
613 ArmLIR *target = newLIR0(cUnit, kArmPseudoTargetLabel);
614 target->defMask = ENCODE_ALL;
615 branchOver->generic.target = (LIR *) target;
616
Ben Cheng11d8f142010-03-24 15:24:19 -0700617 HEAP_ACCESS_SHADOW(true);
Bill Buzbeebe6534f2010-03-12 16:01:35 -0800618 storeBaseIndexed(cUnit, regPtr, regIndex, r0,
619 scale, kWord);
Ben Cheng11d8f142010-03-24 15:24:19 -0700620 HEAP_ACCESS_SHADOW(false);
buzbee919eb062010-07-12 12:59:22 -0700621
buzbeebaf196a2010-08-04 10:13:15 -0700622 dvmCompilerFreeTemp(cUnit, regPtr);
623 dvmCompilerFreeTemp(cUnit, regIndex);
624
buzbee919eb062010-07-12 12:59:22 -0700625 /* NOTE: marking card here based on object head */
626 markCard(cUnit, r0, r1);
Bill Buzbeebe6534f2010-03-12 16:01:35 -0800627}
628
Ben Cheng5d90c202009-11-22 23:31:11 -0800629static bool genShiftOpLong(CompilationUnit *cUnit, MIR *mir,
630 RegLocation rlDest, RegLocation rlSrc1,
631 RegLocation rlShift)
Ben Chengba4fc8b2009-06-01 13:00:29 -0700632{
Ben Chenge9695e52009-06-16 16:11:47 -0700633 /*
634 * Don't mess with the regsiters here as there is a particular calling
635 * convention to the out-of-line handler.
636 */
Bill Buzbee1465db52009-09-23 17:17:35 -0700637 RegLocation rlResult;
638
639 loadValueDirectWideFixed(cUnit, rlSrc1, r0, r1);
640 loadValueDirect(cUnit, rlShift, r2);
Dan Bornstein9a1f8162010-12-01 17:02:26 -0800641 switch( mir->dalvikInsn.opcode) {
Ben Chenge9695e52009-06-16 16:11:47 -0700642 case OP_SHL_LONG:
643 case OP_SHL_LONG_2ADDR:
644 genDispatchToHandler(cUnit, TEMPLATE_SHL_LONG);
645 break;
646 case OP_SHR_LONG:
647 case OP_SHR_LONG_2ADDR:
648 genDispatchToHandler(cUnit, TEMPLATE_SHR_LONG);
649 break;
650 case OP_USHR_LONG:
651 case OP_USHR_LONG_2ADDR:
652 genDispatchToHandler(cUnit, TEMPLATE_USHR_LONG);
653 break;
654 default:
655 return true;
656 }
Bill Buzbeec6f10662010-02-09 11:16:15 -0800657 rlResult = dvmCompilerGetReturnWide(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -0700658 storeValueWide(cUnit, rlDest, rlResult);
Ben Chenge9695e52009-06-16 16:11:47 -0700659 return false;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700660}
Ben Chenge9695e52009-06-16 16:11:47 -0700661
Ben Cheng5d90c202009-11-22 23:31:11 -0800662static bool genArithOpLong(CompilationUnit *cUnit, MIR *mir,
663 RegLocation rlDest, RegLocation rlSrc1,
664 RegLocation rlSrc2)
Ben Chengba4fc8b2009-06-01 13:00:29 -0700665{
Bill Buzbee1465db52009-09-23 17:17:35 -0700666 RegLocation rlResult;
667 OpKind firstOp = kOpBkpt;
668 OpKind secondOp = kOpBkpt;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700669 bool callOut = false;
670 void *callTgt;
671 int retReg = r0;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700672
Dan Bornstein9a1f8162010-12-01 17:02:26 -0800673 switch (mir->dalvikInsn.opcode) {
Ben Chengba4fc8b2009-06-01 13:00:29 -0700674 case OP_NOT_LONG:
Bill Buzbee1465db52009-09-23 17:17:35 -0700675 rlSrc2 = loadValueWide(cUnit, rlSrc2, kCoreReg);
Bill Buzbeec6f10662010-02-09 11:16:15 -0800676 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Bill Buzbee1465db52009-09-23 17:17:35 -0700677 opRegReg(cUnit, kOpMvn, rlResult.lowReg, rlSrc2.lowReg);
678 opRegReg(cUnit, kOpMvn, rlResult.highReg, rlSrc2.highReg);
679 storeValueWide(cUnit, rlDest, rlResult);
680 return false;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700681 break;
682 case OP_ADD_LONG:
683 case OP_ADD_LONG_2ADDR:
Bill Buzbee1465db52009-09-23 17:17:35 -0700684 firstOp = kOpAdd;
685 secondOp = kOpAdc;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700686 break;
687 case OP_SUB_LONG:
688 case OP_SUB_LONG_2ADDR:
Bill Buzbee1465db52009-09-23 17:17:35 -0700689 firstOp = kOpSub;
690 secondOp = kOpSbc;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700691 break;
692 case OP_MUL_LONG:
693 case OP_MUL_LONG_2ADDR:
Bill Buzbee1465db52009-09-23 17:17:35 -0700694 genMulLong(cUnit, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700695 return false;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700696 case OP_DIV_LONG:
697 case OP_DIV_LONG_2ADDR:
698 callOut = true;
699 retReg = r0;
700 callTgt = (void*)__aeabi_ldivmod;
701 break;
702 /* NOTE - result is in r2/r3 instead of r0/r1 */
703 case OP_REM_LONG:
704 case OP_REM_LONG_2ADDR:
705 callOut = true;
706 callTgt = (void*)__aeabi_ldivmod;
707 retReg = r2;
708 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700709 case OP_AND_LONG_2ADDR:
Bill Buzbee1465db52009-09-23 17:17:35 -0700710 case OP_AND_LONG:
711 firstOp = kOpAnd;
712 secondOp = kOpAnd;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700713 break;
714 case OP_OR_LONG:
715 case OP_OR_LONG_2ADDR:
Bill Buzbee1465db52009-09-23 17:17:35 -0700716 firstOp = kOpOr;
717 secondOp = kOpOr;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700718 break;
719 case OP_XOR_LONG:
720 case OP_XOR_LONG_2ADDR:
Bill Buzbee1465db52009-09-23 17:17:35 -0700721 firstOp = kOpXor;
722 secondOp = kOpXor;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700723 break;
Ben Chenge9695e52009-06-16 16:11:47 -0700724 case OP_NEG_LONG: {
Bill Buzbee51ecf602010-01-14 14:27:52 -0800725 //TUNING: can improve this using Thumb2 code
Bill Buzbeec6f10662010-02-09 11:16:15 -0800726 int tReg = dvmCompilerAllocTemp(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -0700727 rlSrc2 = loadValueWide(cUnit, rlSrc2, kCoreReg);
Bill Buzbeec6f10662010-02-09 11:16:15 -0800728 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Ben Chengbd1326d2010-04-02 15:04:53 -0700729 loadConstantNoClobber(cUnit, tReg, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -0700730 opRegRegReg(cUnit, kOpSub, rlResult.lowReg,
Bill Buzbee51ecf602010-01-14 14:27:52 -0800731 tReg, rlSrc2.lowReg);
732 opRegReg(cUnit, kOpSbc, tReg, rlSrc2.highReg);
733 genRegCopy(cUnit, rlResult.highReg, tReg);
Bill Buzbee1465db52009-09-23 17:17:35 -0700734 storeValueWide(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700735 return false;
Ben Chenge9695e52009-06-16 16:11:47 -0700736 }
Ben Chengba4fc8b2009-06-01 13:00:29 -0700737 default:
738 LOGE("Invalid long arith op");
Bill Buzbeefc519dc2010-03-06 23:30:57 -0800739 dvmCompilerAbort(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700740 }
741 if (!callOut) {
Bill Buzbee80cef862010-03-25 10:38:34 -0700742 genLong3Addr(cUnit, mir, firstOp, secondOp, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700743 } else {
Bill Buzbee1465db52009-09-23 17:17:35 -0700744 // Adjust return regs in to handle case of rem returning r2/r3
Bill Buzbeec6f10662010-02-09 11:16:15 -0800745 dvmCompilerFlushAllRegs(cUnit); /* Send everything to home location */
Bill Buzbee1465db52009-09-23 17:17:35 -0700746 loadValueDirectWideFixed(cUnit, rlSrc1, r0, r1);
Ben Cheng20d7e6c2011-02-18 17:12:42 -0800747 LOAD_FUNC_ADDR(cUnit, r14lr, (int) callTgt);
Bill Buzbee1465db52009-09-23 17:17:35 -0700748 loadValueDirectWideFixed(cUnit, rlSrc2, r2, r3);
Ben Cheng20d7e6c2011-02-18 17:12:42 -0800749 opReg(cUnit, kOpBlx, r14lr);
Elliott Hughes6a555132010-02-25 15:41:42 -0800750 dvmCompilerClobberCallRegs(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -0700751 if (retReg == r0)
Bill Buzbeec6f10662010-02-09 11:16:15 -0800752 rlResult = dvmCompilerGetReturnWide(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -0700753 else
Bill Buzbeec6f10662010-02-09 11:16:15 -0800754 rlResult = dvmCompilerGetReturnWideAlt(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -0700755 storeValueWide(cUnit, rlDest, rlResult);
Ben Chengd72564c2011-02-08 17:09:25 -0800756#if defined(WITH_SELF_VERIFICATION)
757 cUnit->usesLinkRegister = true;
758#endif
Ben Chengba4fc8b2009-06-01 13:00:29 -0700759 }
760 return false;
761}
762
Ben Cheng5d90c202009-11-22 23:31:11 -0800763static bool genArithOpInt(CompilationUnit *cUnit, MIR *mir,
764 RegLocation rlDest, RegLocation rlSrc1,
765 RegLocation rlSrc2)
Ben Chengba4fc8b2009-06-01 13:00:29 -0700766{
Bill Buzbee1465db52009-09-23 17:17:35 -0700767 OpKind op = kOpBkpt;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700768 bool callOut = false;
769 bool checkZero = false;
Bill Buzbee1465db52009-09-23 17:17:35 -0700770 bool unary = false;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700771 int retReg = r0;
772 void *callTgt;
Bill Buzbee1465db52009-09-23 17:17:35 -0700773 RegLocation rlResult;
Bill Buzbee0e605272009-12-01 14:28:05 -0800774 bool shiftOp = false;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700775
Dan Bornstein9a1f8162010-12-01 17:02:26 -0800776 switch (mir->dalvikInsn.opcode) {
Ben Chengba4fc8b2009-06-01 13:00:29 -0700777 case OP_NEG_INT:
Bill Buzbee1465db52009-09-23 17:17:35 -0700778 op = kOpNeg;
779 unary = true;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700780 break;
781 case OP_NOT_INT:
Bill Buzbee1465db52009-09-23 17:17:35 -0700782 op = kOpMvn;
783 unary = true;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700784 break;
785 case OP_ADD_INT:
786 case OP_ADD_INT_2ADDR:
Bill Buzbee1465db52009-09-23 17:17:35 -0700787 op = kOpAdd;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700788 break;
789 case OP_SUB_INT:
790 case OP_SUB_INT_2ADDR:
Bill Buzbee1465db52009-09-23 17:17:35 -0700791 op = kOpSub;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700792 break;
793 case OP_MUL_INT:
794 case OP_MUL_INT_2ADDR:
Bill Buzbee1465db52009-09-23 17:17:35 -0700795 op = kOpMul;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700796 break;
797 case OP_DIV_INT:
798 case OP_DIV_INT_2ADDR:
799 callOut = true;
800 checkZero = true;
801 callTgt = __aeabi_idiv;
802 retReg = r0;
803 break;
804 /* NOTE: returns in r1 */
805 case OP_REM_INT:
806 case OP_REM_INT_2ADDR:
807 callOut = true;
808 checkZero = true;
809 callTgt = __aeabi_idivmod;
810 retReg = r1;
811 break;
812 case OP_AND_INT:
813 case OP_AND_INT_2ADDR:
Bill Buzbee1465db52009-09-23 17:17:35 -0700814 op = kOpAnd;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700815 break;
816 case OP_OR_INT:
817 case OP_OR_INT_2ADDR:
Bill Buzbee1465db52009-09-23 17:17:35 -0700818 op = kOpOr;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700819 break;
820 case OP_XOR_INT:
821 case OP_XOR_INT_2ADDR:
Bill Buzbee1465db52009-09-23 17:17:35 -0700822 op = kOpXor;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700823 break;
824 case OP_SHL_INT:
825 case OP_SHL_INT_2ADDR:
Bill Buzbee0e605272009-12-01 14:28:05 -0800826 shiftOp = true;
Bill Buzbee1465db52009-09-23 17:17:35 -0700827 op = kOpLsl;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700828 break;
829 case OP_SHR_INT:
830 case OP_SHR_INT_2ADDR:
Bill Buzbee0e605272009-12-01 14:28:05 -0800831 shiftOp = true;
Bill Buzbee1465db52009-09-23 17:17:35 -0700832 op = kOpAsr;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700833 break;
834 case OP_USHR_INT:
835 case OP_USHR_INT_2ADDR:
Bill Buzbee0e605272009-12-01 14:28:05 -0800836 shiftOp = true;
Bill Buzbee1465db52009-09-23 17:17:35 -0700837 op = kOpLsr;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700838 break;
839 default:
840 LOGE("Invalid word arith op: 0x%x(%d)",
Dan Bornstein9a1f8162010-12-01 17:02:26 -0800841 mir->dalvikInsn.opcode, mir->dalvikInsn.opcode);
Bill Buzbeefc519dc2010-03-06 23:30:57 -0800842 dvmCompilerAbort(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700843 }
844 if (!callOut) {
Bill Buzbee1465db52009-09-23 17:17:35 -0700845 rlSrc1 = loadValue(cUnit, rlSrc1, kCoreReg);
846 if (unary) {
Bill Buzbeec6f10662010-02-09 11:16:15 -0800847 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Bill Buzbee1465db52009-09-23 17:17:35 -0700848 opRegReg(cUnit, op, rlResult.lowReg,
849 rlSrc1.lowReg);
Ben Chenge9695e52009-06-16 16:11:47 -0700850 } else {
Bill Buzbee1465db52009-09-23 17:17:35 -0700851 rlSrc2 = loadValue(cUnit, rlSrc2, kCoreReg);
Bill Buzbee0e605272009-12-01 14:28:05 -0800852 if (shiftOp) {
Bill Buzbeec6f10662010-02-09 11:16:15 -0800853 int tReg = dvmCompilerAllocTemp(cUnit);
Bill Buzbee0e605272009-12-01 14:28:05 -0800854 opRegRegImm(cUnit, kOpAnd, tReg, rlSrc2.lowReg, 31);
Bill Buzbeec6f10662010-02-09 11:16:15 -0800855 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Bill Buzbee0e605272009-12-01 14:28:05 -0800856 opRegRegReg(cUnit, op, rlResult.lowReg,
857 rlSrc1.lowReg, tReg);
Bill Buzbeec6f10662010-02-09 11:16:15 -0800858 dvmCompilerFreeTemp(cUnit, tReg);
Bill Buzbee0e605272009-12-01 14:28:05 -0800859 } else {
Bill Buzbeec6f10662010-02-09 11:16:15 -0800860 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Bill Buzbee0e605272009-12-01 14:28:05 -0800861 opRegRegReg(cUnit, op, rlResult.lowReg,
862 rlSrc1.lowReg, rlSrc2.lowReg);
863 }
Ben Chenge9695e52009-06-16 16:11:47 -0700864 }
Bill Buzbee1465db52009-09-23 17:17:35 -0700865 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700866 } else {
Bill Buzbee1465db52009-09-23 17:17:35 -0700867 RegLocation rlResult;
Bill Buzbeec6f10662010-02-09 11:16:15 -0800868 dvmCompilerFlushAllRegs(cUnit); /* Send everything to home location */
Bill Buzbee1465db52009-09-23 17:17:35 -0700869 loadValueDirectFixed(cUnit, rlSrc2, r1);
Ben Chengbd1326d2010-04-02 15:04:53 -0700870 LOAD_FUNC_ADDR(cUnit, r2, (int) callTgt);
Bill Buzbee1465db52009-09-23 17:17:35 -0700871 loadValueDirectFixed(cUnit, rlSrc1, r0);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700872 if (checkZero) {
Bill Buzbee1465db52009-09-23 17:17:35 -0700873 genNullCheck(cUnit, rlSrc2.sRegLow, r1, mir->offset, NULL);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700874 }
Bill Buzbee1465db52009-09-23 17:17:35 -0700875 opReg(cUnit, kOpBlx, r2);
Elliott Hughes6a555132010-02-25 15:41:42 -0800876 dvmCompilerClobberCallRegs(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -0700877 if (retReg == r0)
Bill Buzbeec6f10662010-02-09 11:16:15 -0800878 rlResult = dvmCompilerGetReturn(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -0700879 else
Bill Buzbeec6f10662010-02-09 11:16:15 -0800880 rlResult = dvmCompilerGetReturnAlt(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -0700881 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700882 }
883 return false;
884}
885
Ben Cheng5d90c202009-11-22 23:31:11 -0800886static bool genArithOp(CompilationUnit *cUnit, MIR *mir)
Ben Chengba4fc8b2009-06-01 13:00:29 -0700887{
Dan Bornstein9a1f8162010-12-01 17:02:26 -0800888 Opcode opcode = mir->dalvikInsn.opcode;
Bill Buzbee1465db52009-09-23 17:17:35 -0700889 RegLocation rlDest;
890 RegLocation rlSrc1;
891 RegLocation rlSrc2;
892 /* Deduce sizes of operands */
893 if (mir->ssaRep->numUses == 2) {
Bill Buzbeec6f10662010-02-09 11:16:15 -0800894 rlSrc1 = dvmCompilerGetSrc(cUnit, mir, 0);
895 rlSrc2 = dvmCompilerGetSrc(cUnit, mir, 1);
Bill Buzbee1465db52009-09-23 17:17:35 -0700896 } else if (mir->ssaRep->numUses == 3) {
Bill Buzbeec6f10662010-02-09 11:16:15 -0800897 rlSrc1 = dvmCompilerGetSrcWide(cUnit, mir, 0, 1);
898 rlSrc2 = dvmCompilerGetSrc(cUnit, mir, 2);
Bill Buzbee1465db52009-09-23 17:17:35 -0700899 } else {
Bill Buzbeec6f10662010-02-09 11:16:15 -0800900 rlSrc1 = dvmCompilerGetSrcWide(cUnit, mir, 0, 1);
901 rlSrc2 = dvmCompilerGetSrcWide(cUnit, mir, 2, 3);
Bill Buzbee1465db52009-09-23 17:17:35 -0700902 assert(mir->ssaRep->numUses == 4);
903 }
904 if (mir->ssaRep->numDefs == 1) {
Bill Buzbeec6f10662010-02-09 11:16:15 -0800905 rlDest = dvmCompilerGetDest(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -0700906 } else {
907 assert(mir->ssaRep->numDefs == 2);
Bill Buzbeec6f10662010-02-09 11:16:15 -0800908 rlDest = dvmCompilerGetDestWide(cUnit, mir, 0, 1);
Bill Buzbee1465db52009-09-23 17:17:35 -0700909 }
Ben Chengba4fc8b2009-06-01 13:00:29 -0700910
Dan Bornstein9a1f8162010-12-01 17:02:26 -0800911 if ((opcode >= OP_ADD_LONG_2ADDR) && (opcode <= OP_XOR_LONG_2ADDR)) {
Ben Cheng5d90c202009-11-22 23:31:11 -0800912 return genArithOpLong(cUnit,mir, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700913 }
Dan Bornstein9a1f8162010-12-01 17:02:26 -0800914 if ((opcode >= OP_ADD_LONG) && (opcode <= OP_XOR_LONG)) {
Ben Cheng5d90c202009-11-22 23:31:11 -0800915 return genArithOpLong(cUnit,mir, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700916 }
Dan Bornstein9a1f8162010-12-01 17:02:26 -0800917 if ((opcode >= OP_SHL_LONG_2ADDR) && (opcode <= OP_USHR_LONG_2ADDR)) {
Ben Cheng5d90c202009-11-22 23:31:11 -0800918 return genShiftOpLong(cUnit,mir, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700919 }
Dan Bornstein9a1f8162010-12-01 17:02:26 -0800920 if ((opcode >= OP_SHL_LONG) && (opcode <= OP_USHR_LONG)) {
Ben Cheng5d90c202009-11-22 23:31:11 -0800921 return genShiftOpLong(cUnit,mir, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700922 }
Dan Bornstein9a1f8162010-12-01 17:02:26 -0800923 if ((opcode >= OP_ADD_INT_2ADDR) && (opcode <= OP_USHR_INT_2ADDR)) {
Ben Cheng5d90c202009-11-22 23:31:11 -0800924 return genArithOpInt(cUnit,mir, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700925 }
Dan Bornstein9a1f8162010-12-01 17:02:26 -0800926 if ((opcode >= OP_ADD_INT) && (opcode <= OP_USHR_INT)) {
Ben Cheng5d90c202009-11-22 23:31:11 -0800927 return genArithOpInt(cUnit,mir, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700928 }
Dan Bornstein9a1f8162010-12-01 17:02:26 -0800929 if ((opcode >= OP_ADD_FLOAT_2ADDR) && (opcode <= OP_REM_FLOAT_2ADDR)) {
Ben Cheng5d90c202009-11-22 23:31:11 -0800930 return genArithOpFloat(cUnit,mir, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700931 }
Dan Bornstein9a1f8162010-12-01 17:02:26 -0800932 if ((opcode >= OP_ADD_FLOAT) && (opcode <= OP_REM_FLOAT)) {
Ben Cheng5d90c202009-11-22 23:31:11 -0800933 return genArithOpFloat(cUnit, mir, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700934 }
Dan Bornstein9a1f8162010-12-01 17:02:26 -0800935 if ((opcode >= OP_ADD_DOUBLE_2ADDR) && (opcode <= OP_REM_DOUBLE_2ADDR)) {
Ben Cheng5d90c202009-11-22 23:31:11 -0800936 return genArithOpDouble(cUnit,mir, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700937 }
Dan Bornstein9a1f8162010-12-01 17:02:26 -0800938 if ((opcode >= OP_ADD_DOUBLE) && (opcode <= OP_REM_DOUBLE)) {
Ben Cheng5d90c202009-11-22 23:31:11 -0800939 return genArithOpDouble(cUnit,mir, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700940 }
941 return true;
942}
943
Bill Buzbee1465db52009-09-23 17:17:35 -0700944/* Generate unconditional branch instructions */
945static ArmLIR *genUnconditionalBranch(CompilationUnit *cUnit, ArmLIR *target)
946{
947 ArmLIR *branch = opNone(cUnit, kOpUncondBr);
948 branch->generic.target = (LIR *) target;
949 return branch;
950}
951
Bill Buzbee1465db52009-09-23 17:17:35 -0700952/* Perform the actual operation for OP_RETURN_* */
953static void genReturnCommon(CompilationUnit *cUnit, MIR *mir)
954{
Ben Cheng20d7e6c2011-02-18 17:12:42 -0800955 genDispatchToHandler(cUnit, gDvmJit.methodTraceSupport ?
956 TEMPLATE_RETURN_PROF : TEMPLATE_RETURN);
Ben Cheng978738d2010-05-13 13:45:57 -0700957#if defined(WITH_JIT_TUNING)
Ben Cheng20d7e6c2011-02-18 17:12:42 -0800958 gDvmJit.returnOp++;
Bill Buzbee1465db52009-09-23 17:17:35 -0700959#endif
Ben Cheng20d7e6c2011-02-18 17:12:42 -0800960 int dPC = (int) (cUnit->method->insns + mir->offset);
961 /* Insert branch, but defer setting of target */
962 ArmLIR *branch = genUnconditionalBranch(cUnit, NULL);
963 /* Set up the place holder to reconstruct this Dalvik PC */
964 ArmLIR *pcrLabel = (ArmLIR *) dvmCompilerNew(sizeof(ArmLIR), true);
965 pcrLabel->opcode = kArmPseudoPCReconstructionCell;
966 pcrLabel->operands[0] = dPC;
967 pcrLabel->operands[1] = mir->offset;
968 /* Insert the place holder to the growable list */
969 dvmInsertGrowableList(&cUnit->pcReconstructionList, (intptr_t) pcrLabel);
970 /* Branch to the PC reconstruction code */
971 branch->generic.target = (LIR *) pcrLabel;
Bill Buzbee1465db52009-09-23 17:17:35 -0700972}
973
Ben Chengba4fc8b2009-06-01 13:00:29 -0700974static void genProcessArgsNoRange(CompilationUnit *cUnit, MIR *mir,
975 DecodedInstruction *dInsn,
Bill Buzbee89efc3d2009-07-28 11:22:22 -0700976 ArmLIR **pcrLabel)
Ben Chengba4fc8b2009-06-01 13:00:29 -0700977{
978 unsigned int i;
979 unsigned int regMask = 0;
Bill Buzbee1465db52009-09-23 17:17:35 -0700980 RegLocation rlArg;
981 int numDone = 0;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700982
Bill Buzbee1465db52009-09-23 17:17:35 -0700983 /*
984 * Load arguments to r0..r4. Note that these registers may contain
985 * live values, so we clobber them immediately after loading to prevent
986 * them from being used as sources for subsequent loads.
987 */
Bill Buzbeec6f10662010-02-09 11:16:15 -0800988 dvmCompilerLockAllTemps(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700989 for (i = 0; i < dInsn->vA; i++) {
990 regMask |= 1 << i;
Bill Buzbeec6f10662010-02-09 11:16:15 -0800991 rlArg = dvmCompilerGetSrc(cUnit, mir, numDone++);
Bill Buzbee1465db52009-09-23 17:17:35 -0700992 loadValueDirectFixed(cUnit, rlArg, i);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700993 }
994 if (regMask) {
995 /* Up to 5 args are pushed on top of FP - sizeofStackSaveArea */
Ben Cheng20d7e6c2011-02-18 17:12:42 -0800996 opRegRegImm(cUnit, kOpSub, r7, r5FP,
Bill Buzbee1465db52009-09-23 17:17:35 -0700997 sizeof(StackSaveArea) + (dInsn->vA << 2));
Ben Chengba4fc8b2009-06-01 13:00:29 -0700998 /* generate null check */
999 if (pcrLabel) {
Bill Buzbeec6f10662010-02-09 11:16:15 -08001000 *pcrLabel = genNullCheck(cUnit, dvmCompilerSSASrc(mir, 0), r0,
Bill Buzbee1465db52009-09-23 17:17:35 -07001001 mir->offset, NULL);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001002 }
Bill Buzbee270c1d62009-08-13 16:58:07 -07001003 storeMultiple(cUnit, r7, regMask);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001004 }
1005}
1006
1007static void genProcessArgsRange(CompilationUnit *cUnit, MIR *mir,
1008 DecodedInstruction *dInsn,
Bill Buzbee89efc3d2009-07-28 11:22:22 -07001009 ArmLIR **pcrLabel)
Ben Chengba4fc8b2009-06-01 13:00:29 -07001010{
1011 int srcOffset = dInsn->vC << 2;
1012 int numArgs = dInsn->vA;
1013 int regMask;
Bill Buzbee1465db52009-09-23 17:17:35 -07001014
1015 /*
1016 * Note: here, all promoted registers will have been flushed
1017 * back to the Dalvik base locations, so register usage restrictins
1018 * are lifted. All parms loaded from original Dalvik register
1019 * region - even though some might conceivably have valid copies
1020 * cached in a preserved register.
1021 */
Bill Buzbeec6f10662010-02-09 11:16:15 -08001022 dvmCompilerLockAllTemps(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07001023
Ben Chengba4fc8b2009-06-01 13:00:29 -07001024 /*
Ben Cheng20d7e6c2011-02-18 17:12:42 -08001025 * r4PC : &r5FP[vC]
Ben Chengba4fc8b2009-06-01 13:00:29 -07001026 * r7: &newFP[0]
1027 */
Ben Cheng20d7e6c2011-02-18 17:12:42 -08001028 opRegRegImm(cUnit, kOpAdd, r4PC, r5FP, srcOffset);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001029 /* load [r0 .. min(numArgs,4)] */
1030 regMask = (1 << ((numArgs < 4) ? numArgs : 4)) - 1;
Ben Chengd7d426a2009-09-22 11:23:36 -07001031 /*
1032 * Protect the loadMultiple instruction from being reordered with other
1033 * Dalvik stack accesses.
jeffhao71eee1f2011-01-04 14:18:54 -08001034 *
1035 * This code is also shared by the invoke jumbo instructions, and this
1036 * does not need to be done if the invoke jumbo has no arguments.
Ben Chengd7d426a2009-09-22 11:23:36 -07001037 */
jeffhao71eee1f2011-01-04 14:18:54 -08001038 if (numArgs != 0) loadMultiple(cUnit, r4PC, regMask);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001039
Ben Cheng20d7e6c2011-02-18 17:12:42 -08001040 opRegRegImm(cUnit, kOpSub, r7, r5FP,
Bill Buzbee1465db52009-09-23 17:17:35 -07001041 sizeof(StackSaveArea) + (numArgs << 2));
Ben Chengba4fc8b2009-06-01 13:00:29 -07001042 /* generate null check */
1043 if (pcrLabel) {
Bill Buzbeec6f10662010-02-09 11:16:15 -08001044 *pcrLabel = genNullCheck(cUnit, dvmCompilerSSASrc(mir, 0), r0,
Bill Buzbee1465db52009-09-23 17:17:35 -07001045 mir->offset, NULL);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001046 }
1047
1048 /*
1049 * Handle remaining 4n arguments:
1050 * store previously loaded 4 values and load the next 4 values
1051 */
1052 if (numArgs >= 8) {
Bill Buzbee89efc3d2009-07-28 11:22:22 -07001053 ArmLIR *loopLabel = NULL;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001054 /*
1055 * r0 contains "this" and it will be used later, so push it to the stack
Ben Cheng20d7e6c2011-02-18 17:12:42 -08001056 * first. Pushing r5FP is just for stack alignment purposes.
Ben Chengba4fc8b2009-06-01 13:00:29 -07001057 */
Ben Cheng20d7e6c2011-02-18 17:12:42 -08001058 opImm(cUnit, kOpPush, (1 << r0 | 1 << r5FP));
Ben Chengba4fc8b2009-06-01 13:00:29 -07001059 /* No need to generate the loop structure if numArgs <= 11 */
1060 if (numArgs > 11) {
1061 loadConstant(cUnit, 5, ((numArgs - 4) >> 2) << 2);
Bill Buzbee1465db52009-09-23 17:17:35 -07001062 loopLabel = newLIR0(cUnit, kArmPseudoTargetLabel);
Ben Chengd7d426a2009-09-22 11:23:36 -07001063 loopLabel->defMask = ENCODE_ALL;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001064 }
Bill Buzbee270c1d62009-08-13 16:58:07 -07001065 storeMultiple(cUnit, r7, regMask);
Ben Chengd7d426a2009-09-22 11:23:36 -07001066 /*
1067 * Protect the loadMultiple instruction from being reordered with other
1068 * Dalvik stack accesses.
1069 */
Bill Buzbee270c1d62009-08-13 16:58:07 -07001070 loadMultiple(cUnit, r4PC, regMask);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001071 /* No need to generate the loop structure if numArgs <= 11 */
1072 if (numArgs > 11) {
Ben Cheng20d7e6c2011-02-18 17:12:42 -08001073 opRegImm(cUnit, kOpSub, r5FP, 4);
Bill Buzbee1465db52009-09-23 17:17:35 -07001074 genConditionalBranch(cUnit, kArmCondNe, loopLabel);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001075 }
1076 }
1077
1078 /* Save the last batch of loaded values */
jeffhao71eee1f2011-01-04 14:18:54 -08001079 if (numArgs != 0) storeMultiple(cUnit, r7, regMask);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001080
1081 /* Generate the loop epilogue - don't use r0 */
1082 if ((numArgs > 4) && (numArgs % 4)) {
1083 regMask = ((1 << (numArgs & 0x3)) - 1) << 1;
Ben Chengd7d426a2009-09-22 11:23:36 -07001084 /*
1085 * Protect the loadMultiple instruction from being reordered with other
1086 * Dalvik stack accesses.
1087 */
Bill Buzbee270c1d62009-08-13 16:58:07 -07001088 loadMultiple(cUnit, r4PC, regMask);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001089 }
1090 if (numArgs >= 8)
Ben Cheng20d7e6c2011-02-18 17:12:42 -08001091 opImm(cUnit, kOpPop, (1 << r0 | 1 << r5FP));
Ben Chengba4fc8b2009-06-01 13:00:29 -07001092
1093 /* Save the modulo 4 arguments */
1094 if ((numArgs > 4) && (numArgs % 4)) {
Bill Buzbee270c1d62009-08-13 16:58:07 -07001095 storeMultiple(cUnit, r7, regMask);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001096 }
1097}
1098
Ben Cheng38329f52009-07-07 14:19:20 -07001099/*
1100 * Generate code to setup the call stack then jump to the chaining cell if it
1101 * is not a native method.
1102 */
1103static void genInvokeSingletonCommon(CompilationUnit *cUnit, MIR *mir,
Bill Buzbee89efc3d2009-07-28 11:22:22 -07001104 BasicBlock *bb, ArmLIR *labelList,
1105 ArmLIR *pcrLabel,
Ben Cheng38329f52009-07-07 14:19:20 -07001106 const Method *calleeMethod)
Ben Chengba4fc8b2009-06-01 13:00:29 -07001107{
Bill Buzbee1465db52009-09-23 17:17:35 -07001108 /*
1109 * Note: all Dalvik register state should be flushed to
1110 * memory by the point, so register usage restrictions no
1111 * longer apply. All temp & preserved registers may be used.
1112 */
Bill Buzbeec6f10662010-02-09 11:16:15 -08001113 dvmCompilerLockAllTemps(cUnit);
Bill Buzbee89efc3d2009-07-28 11:22:22 -07001114 ArmLIR *retChainingCell = &labelList[bb->fallThrough->id];
Ben Chengba4fc8b2009-06-01 13:00:29 -07001115
1116 /* r1 = &retChainingCell */
Ben Cheng20d7e6c2011-02-18 17:12:42 -08001117 ArmLIR *addrRetChain = opRegRegImm(cUnit, kOpAdd, r1, r15pc, 0);
Ben Chengc8293e72010-10-12 11:50:10 -07001118
Ben Chengba4fc8b2009-06-01 13:00:29 -07001119 /* r4PC = dalvikCallsite */
1120 loadConstant(cUnit, r4PC,
1121 (int) (cUnit->method->insns + mir->offset));
1122 addrRetChain->generic.target = (LIR *) retChainingCell;
Ben Chengc8293e72010-10-12 11:50:10 -07001123
1124 /* r7 = calleeMethod->registersSize */
1125 loadConstant(cUnit, r7, calleeMethod->registersSize);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001126 /*
Ben Cheng38329f52009-07-07 14:19:20 -07001127 * r0 = calleeMethod (loaded upon calling genInvokeSingletonCommon)
Ben Chengba4fc8b2009-06-01 13:00:29 -07001128 * r1 = &ChainingCell
Ben Chengc8293e72010-10-12 11:50:10 -07001129 * r2 = calleeMethod->outsSize (to be loaded later for Java callees)
Ben Chengba4fc8b2009-06-01 13:00:29 -07001130 * r4PC = callsiteDPC
Ben Chengc8293e72010-10-12 11:50:10 -07001131 * r7 = calleeMethod->registersSize
Ben Chengba4fc8b2009-06-01 13:00:29 -07001132 */
1133 if (dvmIsNativeMethod(calleeMethod)) {
buzbee18fba342011-01-19 15:31:15 -08001134 genDispatchToHandler(cUnit, gDvmJit.methodTraceSupport ?
1135 TEMPLATE_INVOKE_METHOD_NATIVE_PROF :
1136 TEMPLATE_INVOKE_METHOD_NATIVE);
Ben Cheng978738d2010-05-13 13:45:57 -07001137#if defined(WITH_JIT_TUNING)
Ben Cheng38329f52009-07-07 14:19:20 -07001138 gDvmJit.invokeNative++;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001139#endif
1140 } else {
Ben Chengc8293e72010-10-12 11:50:10 -07001141 /* For Java callees, set up r2 to be calleeMethod->outsSize */
1142 loadConstant(cUnit, r2, calleeMethod->outsSize);
buzbee18fba342011-01-19 15:31:15 -08001143 genDispatchToHandler(cUnit, gDvmJit.methodTraceSupport ?
1144 TEMPLATE_INVOKE_METHOD_CHAIN_PROF :
1145 TEMPLATE_INVOKE_METHOD_CHAIN);
Ben Cheng978738d2010-05-13 13:45:57 -07001146#if defined(WITH_JIT_TUNING)
Ben Cheng86717f72010-03-05 15:27:21 -08001147 gDvmJit.invokeMonomorphic++;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001148#endif
Ben Cheng38329f52009-07-07 14:19:20 -07001149 /* Branch to the chaining cell */
Ben Chengba4fc8b2009-06-01 13:00:29 -07001150 genUnconditionalBranch(cUnit, &labelList[bb->taken->id]);
1151 }
1152 /* Handle exceptions using the interpreter */
1153 genTrap(cUnit, mir->offset, pcrLabel);
1154}
1155
Ben Cheng38329f52009-07-07 14:19:20 -07001156/*
1157 * Generate code to check the validity of a predicted chain and take actions
1158 * based on the result.
1159 *
1160 * 0x426a99aa : ldr r4, [pc, #72] --> r4 <- dalvikPC of this invoke
1161 * 0x426a99ac : add r1, pc, #32 --> r1 <- &retChainingCell
1162 * 0x426a99ae : add r2, pc, #40 --> r2 <- &predictedChainingCell
1163 * 0x426a99b0 : blx_1 0x426a918c --+ TEMPLATE_INVOKE_METHOD_PREDICTED_CHAIN
1164 * 0x426a99b2 : blx_2 see above --+
1165 * 0x426a99b4 : b 0x426a99d8 --> off to the predicted chain
1166 * 0x426a99b6 : b 0x426a99c8 --> punt to the interpreter
1167 * 0x426a99b8 : ldr r0, [r7, #44] --> r0 <- this->class->vtable[methodIdx]
1168 * 0x426a99ba : cmp r1, #0 --> compare r1 (rechain count) against 0
1169 * 0x426a99bc : bgt 0x426a99c2 --> >=0? don't rechain
Ben Chengaf5aa1f2011-01-04 15:37:04 -08001170 * 0x426a99be : ldr r7, [pc, #off]--+ dvmJitToPatchPredictedChain
Ben Cheng38329f52009-07-07 14:19:20 -07001171 * 0x426a99c0 : blx r7 --+
1172 * 0x426a99c2 : add r1, pc, #12 --> r1 <- &retChainingCell
1173 * 0x426a99c4 : blx_1 0x426a9098 --+ TEMPLATE_INVOKE_METHOD_NO_OPT
1174 * 0x426a99c6 : blx_2 see above --+
1175 */
1176static void genInvokeVirtualCommon(CompilationUnit *cUnit, MIR *mir,
1177 int methodIndex,
Bill Buzbee89efc3d2009-07-28 11:22:22 -07001178 ArmLIR *retChainingCell,
1179 ArmLIR *predChainingCell,
1180 ArmLIR *pcrLabel)
Ben Cheng38329f52009-07-07 14:19:20 -07001181{
Bill Buzbee1465db52009-09-23 17:17:35 -07001182 /*
1183 * Note: all Dalvik register state should be flushed to
1184 * memory by the point, so register usage restrictions no
1185 * longer apply. Lock temps to prevent them from being
1186 * allocated by utility routines.
1187 */
Bill Buzbeec6f10662010-02-09 11:16:15 -08001188 dvmCompilerLockAllTemps(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07001189
Ben Cheng04517042011-03-14 11:16:21 -07001190 /*
1191 * For verbose printing, store the method pointer in operands[1] first as
1192 * operands[0] will be clobbered in dvmCompilerMIR2LIR.
1193 */
1194 predChainingCell->operands[1] = (int) mir->meta.callsiteInfo->method;
1195
Ben Cheng38329f52009-07-07 14:19:20 -07001196 /* "this" is already left in r0 by genProcessArgs* */
1197
1198 /* r4PC = dalvikCallsite */
1199 loadConstant(cUnit, r4PC,
1200 (int) (cUnit->method->insns + mir->offset));
1201
1202 /* r1 = &retChainingCell */
Ben Cheng20d7e6c2011-02-18 17:12:42 -08001203 ArmLIR *addrRetChain = opRegRegImm(cUnit, kOpAdd, r1, r15pc, 0);
Ben Cheng38329f52009-07-07 14:19:20 -07001204 addrRetChain->generic.target = (LIR *) retChainingCell;
1205
1206 /* r2 = &predictedChainingCell */
Ben Cheng20d7e6c2011-02-18 17:12:42 -08001207 ArmLIR *predictedChainingCell = opRegRegImm(cUnit, kOpAdd, r2, r15pc, 0);
Ben Cheng38329f52009-07-07 14:19:20 -07001208 predictedChainingCell->generic.target = (LIR *) predChainingCell;
1209
buzbee18fba342011-01-19 15:31:15 -08001210 genDispatchToHandler(cUnit, gDvmJit.methodTraceSupport ?
1211 TEMPLATE_INVOKE_METHOD_PREDICTED_CHAIN_PROF :
1212 TEMPLATE_INVOKE_METHOD_PREDICTED_CHAIN);
Ben Cheng38329f52009-07-07 14:19:20 -07001213
1214 /* return through lr - jump to the chaining cell */
1215 genUnconditionalBranch(cUnit, predChainingCell);
1216
1217 /*
1218 * null-check on "this" may have been eliminated, but we still need a PC-
1219 * reconstruction label for stack overflow bailout.
1220 */
1221 if (pcrLabel == NULL) {
1222 int dPC = (int) (cUnit->method->insns + mir->offset);
Carl Shapirofc75f3e2010-12-07 11:43:38 -08001223 pcrLabel = (ArmLIR *) dvmCompilerNew(sizeof(ArmLIR), true);
Dan Bornstein9a1f8162010-12-01 17:02:26 -08001224 pcrLabel->opcode = kArmPseudoPCReconstructionCell;
Ben Cheng38329f52009-07-07 14:19:20 -07001225 pcrLabel->operands[0] = dPC;
1226 pcrLabel->operands[1] = mir->offset;
1227 /* Insert the place holder to the growable list */
Ben Cheng00603072010-10-28 11:13:58 -07001228 dvmInsertGrowableList(&cUnit->pcReconstructionList,
1229 (intptr_t) pcrLabel);
Ben Cheng38329f52009-07-07 14:19:20 -07001230 }
1231
1232 /* return through lr+2 - punt to the interpreter */
1233 genUnconditionalBranch(cUnit, pcrLabel);
1234
1235 /*
1236 * return through lr+4 - fully resolve the callee method.
1237 * r1 <- count
1238 * r2 <- &predictedChainCell
1239 * r3 <- this->class
1240 * r4 <- dPC
1241 * r7 <- this->class->vtable
1242 */
1243
1244 /* r0 <- calleeMethod */
Bill Buzbee270c1d62009-08-13 16:58:07 -07001245 loadWordDisp(cUnit, r7, methodIndex * 4, r0);
Ben Cheng38329f52009-07-07 14:19:20 -07001246
1247 /* Check if rechain limit is reached */
buzbee8f8109a2010-08-31 10:16:35 -07001248 ArmLIR *bypassRechaining = genCmpImmBranch(cUnit, kArmCondGt, r1, 0);
Ben Cheng38329f52009-07-07 14:19:20 -07001249
Ben Chengaf5aa1f2011-01-04 15:37:04 -08001250 LOAD_FUNC_ADDR(cUnit, r7, (int) dvmJitToPatchPredictedChain);
Ben Cheng38329f52009-07-07 14:19:20 -07001251
Ben Cheng20d7e6c2011-02-18 17:12:42 -08001252 genRegCopy(cUnit, r1, r6SELF);
Ben Chengb88ec3c2010-05-17 12:50:33 -07001253
Ben Cheng38329f52009-07-07 14:19:20 -07001254 /*
1255 * r0 = calleeMethod
1256 * r2 = &predictedChainingCell
1257 * r3 = class
1258 *
1259 * &returnChainingCell has been loaded into r1 but is not needed
1260 * when patching the chaining cell and will be clobbered upon
1261 * returning so it will be reconstructed again.
1262 */
Bill Buzbee1465db52009-09-23 17:17:35 -07001263 opReg(cUnit, kOpBlx, r7);
Ben Cheng38329f52009-07-07 14:19:20 -07001264
1265 /* r1 = &retChainingCell */
Ben Cheng20d7e6c2011-02-18 17:12:42 -08001266 addrRetChain = opRegRegImm(cUnit, kOpAdd, r1, r15pc, 0);
Ben Cheng38329f52009-07-07 14:19:20 -07001267 addrRetChain->generic.target = (LIR *) retChainingCell;
1268
1269 bypassRechaining->generic.target = (LIR *) addrRetChain;
1270 /*
1271 * r0 = calleeMethod,
1272 * r1 = &ChainingCell,
1273 * r4PC = callsiteDPC,
1274 */
buzbee18fba342011-01-19 15:31:15 -08001275 genDispatchToHandler(cUnit, gDvmJit.methodTraceSupport ?
1276 TEMPLATE_INVOKE_METHOD_NO_OPT_PROF :
1277 TEMPLATE_INVOKE_METHOD_NO_OPT);
Ben Cheng978738d2010-05-13 13:45:57 -07001278#if defined(WITH_JIT_TUNING)
Ben Cheng86717f72010-03-05 15:27:21 -08001279 gDvmJit.invokePolymorphic++;
Ben Cheng38329f52009-07-07 14:19:20 -07001280#endif
1281 /* Handle exceptions using the interpreter */
1282 genTrap(cUnit, mir->offset, pcrLabel);
1283}
1284
Ben Cheng20d7e6c2011-02-18 17:12:42 -08001285/* "this" pointer is already in r0 */
1286static void genInvokeVirtualWholeMethod(CompilationUnit *cUnit,
1287 MIR *mir,
1288 void *calleeAddr,
1289 ArmLIR *retChainingCell)
1290{
1291 CallsiteInfo *callsiteInfo = mir->meta.callsiteInfo;
1292 dvmCompilerLockAllTemps(cUnit);
1293
Ben Cheng385828e2011-03-04 16:48:33 -08001294 loadClassPointer(cUnit, r1, (int) callsiteInfo);
Ben Cheng20d7e6c2011-02-18 17:12:42 -08001295
1296 loadWordDisp(cUnit, r0, offsetof(Object, clazz), r2);
1297 /* Branch to the slow path if classes are not equal */
1298 opRegReg(cUnit, kOpCmp, r1, r2);
1299 /*
1300 * Set the misPredBranchOver target so that it will be generated when the
1301 * code for the non-optimized invoke is generated.
1302 */
1303 ArmLIR *classCheck = opCondBranch(cUnit, kArmCondNe);
1304
1305 /* r0 = the Dalvik PC of the callsite */
1306 loadConstant(cUnit, r0, (int) (cUnit->method->insns + mir->offset));
1307
1308 newLIR2(cUnit, kThumbBl1, (int) calleeAddr, (int) calleeAddr);
1309 newLIR2(cUnit, kThumbBl2, (int) calleeAddr, (int) calleeAddr);
1310 genUnconditionalBranch(cUnit, retChainingCell);
1311
1312 /* Target of slow path */
1313 ArmLIR *slowPathLabel = newLIR0(cUnit, kArmPseudoTargetLabel);
1314
1315 slowPathLabel->defMask = ENCODE_ALL;
1316 classCheck->generic.target = (LIR *) slowPathLabel;
1317
1318 // FIXME
1319 cUnit->printMe = true;
1320}
1321
1322static void genInvokeSingletonWholeMethod(CompilationUnit *cUnit,
1323 MIR *mir,
1324 void *calleeAddr,
1325 ArmLIR *retChainingCell)
1326{
1327 /* r0 = the Dalvik PC of the callsite */
1328 loadConstant(cUnit, r0, (int) (cUnit->method->insns + mir->offset));
1329
1330 newLIR2(cUnit, kThumbBl1, (int) calleeAddr, (int) calleeAddr);
1331 newLIR2(cUnit, kThumbBl2, (int) calleeAddr, (int) calleeAddr);
1332 genUnconditionalBranch(cUnit, retChainingCell);
1333
1334 // FIXME
1335 cUnit->printMe = true;
1336}
1337
Ben Chengba4fc8b2009-06-01 13:00:29 -07001338/* Geneate a branch to go back to the interpreter */
1339static void genPuntToInterp(CompilationUnit *cUnit, unsigned int offset)
1340{
1341 /* r0 = dalvik pc */
Bill Buzbeec6f10662010-02-09 11:16:15 -08001342 dvmCompilerFlushAllRegs(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001343 loadConstant(cUnit, r0, (int) (cUnit->method->insns + offset));
Ben Cheng20d7e6c2011-02-18 17:12:42 -08001344 loadWordDisp(cUnit, r6SELF, offsetof(Thread,
Bill Buzbee270c1d62009-08-13 16:58:07 -07001345 jitToInterpEntries.dvmJitToInterpPunt), r1);
Bill Buzbee1465db52009-09-23 17:17:35 -07001346 opReg(cUnit, kOpBlx, r1);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001347}
1348
1349/*
1350 * Attempt to single step one instruction using the interpreter and return
1351 * to the compiled code for the next Dalvik instruction
1352 */
1353static void genInterpSingleStep(CompilationUnit *cUnit, MIR *mir)
1354{
Dan Bornsteine4852762010-12-02 12:45:00 -08001355 int flags = dexGetFlagsFromOpcode(mir->dalvikInsn.opcode);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001356 int flagsToCheck = kInstrCanBranch | kInstrCanSwitch | kInstrCanReturn |
1357 kInstrCanThrow;
Bill Buzbee1465db52009-09-23 17:17:35 -07001358
Bill Buzbee45273872010-03-11 11:12:15 -08001359 //If already optimized out, just ignore
Dan Bornstein9a1f8162010-12-01 17:02:26 -08001360 if (mir->dalvikInsn.opcode == OP_NOP)
Bill Buzbee45273872010-03-11 11:12:15 -08001361 return;
1362
Bill Buzbee1465db52009-09-23 17:17:35 -07001363 //Ugly, but necessary. Flush all Dalvik regs so Interp can find them
Bill Buzbeec6f10662010-02-09 11:16:15 -08001364 dvmCompilerFlushAllRegs(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07001365
Ben Chengba4fc8b2009-06-01 13:00:29 -07001366 if ((mir->next == NULL) || (flags & flagsToCheck)) {
1367 genPuntToInterp(cUnit, mir->offset);
1368 return;
1369 }
buzbee9f601a92011-02-11 17:48:20 -08001370 int entryAddr = offsetof(Thread,
Ben Chengba4fc8b2009-06-01 13:00:29 -07001371 jitToInterpEntries.dvmJitToInterpSingleStep);
Ben Cheng20d7e6c2011-02-18 17:12:42 -08001372 loadWordDisp(cUnit, r6SELF, entryAddr, r2);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001373 /* r0 = dalvik pc */
1374 loadConstant(cUnit, r0, (int) (cUnit->method->insns + mir->offset));
1375 /* r1 = dalvik pc of following instruction */
1376 loadConstant(cUnit, r1, (int) (cUnit->method->insns + mir->next->offset));
Bill Buzbee1465db52009-09-23 17:17:35 -07001377 opReg(cUnit, kOpBlx, r2);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001378}
1379
Carl Shapiro01605d22011-02-01 11:32:44 -08001380#if defined(_ARMV5TE) || defined(_ARMV5TE_VFP)
Bill Buzbeec1d9ed42010-02-02 11:04:33 -08001381/*
1382 * To prevent a thread in a monitor wait from blocking the Jit from
1383 * resetting the code cache, heavyweight monitor lock will not
1384 * be allowed to return to an existing translation. Instead, we will
1385 * handle them by branching to a handler, which will in turn call the
1386 * runtime lock routine and then branch directly back to the
1387 * interpreter main loop. Given the high cost of the heavyweight
1388 * lock operation, this additional cost should be slight (especially when
1389 * considering that we expect the vast majority of lock operations to
1390 * use the fast-path thin lock bypass).
1391 */
Ben Cheng5d90c202009-11-22 23:31:11 -08001392static void genMonitorPortable(CompilationUnit *cUnit, MIR *mir)
Bill Buzbee270c1d62009-08-13 16:58:07 -07001393{
Dan Bornstein9a1f8162010-12-01 17:02:26 -08001394 bool isEnter = (mir->dalvikInsn.opcode == OP_MONITOR_ENTER);
Bill Buzbee1465db52009-09-23 17:17:35 -07001395 genExportPC(cUnit, mir);
Bill Buzbeec6f10662010-02-09 11:16:15 -08001396 dvmCompilerFlushAllRegs(cUnit); /* Send everything to home location */
1397 RegLocation rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07001398 loadValueDirectFixed(cUnit, rlSrc, r1);
Ben Cheng20d7e6c2011-02-18 17:12:42 -08001399 genRegCopy(cUnit, r0, r6SELF);
Bill Buzbeec1d9ed42010-02-02 11:04:33 -08001400 genNullCheck(cUnit, rlSrc.sRegLow, r1, mir->offset, NULL);
Bill Buzbeeefbd3c52009-11-04 22:18:40 -08001401 if (isEnter) {
Bill Buzbeec1d9ed42010-02-02 11:04:33 -08001402 /* Get dPC of next insn */
1403 loadConstant(cUnit, r4PC, (int)(cUnit->method->insns + mir->offset +
Dan Bornsteine4852762010-12-02 12:45:00 -08001404 dexGetWidthFromOpcode(OP_MONITOR_ENTER)));
Bill Buzbeec1d9ed42010-02-02 11:04:33 -08001405 genDispatchToHandler(cUnit, TEMPLATE_MONITOR_ENTER);
Bill Buzbee1465db52009-09-23 17:17:35 -07001406 } else {
Ben Chengbd1326d2010-04-02 15:04:53 -07001407 LOAD_FUNC_ADDR(cUnit, r2, (int)dvmUnlockObject);
Bill Buzbeec1d9ed42010-02-02 11:04:33 -08001408 /* Do the call */
1409 opReg(cUnit, kOpBlx, r2);
buzbee8f8109a2010-08-31 10:16:35 -07001410 /* Did we throw? */
1411 ArmLIR *branchOver = genCmpImmBranch(cUnit, kArmCondNe, r0, 0);
Bill Buzbee6bbdd6b2010-02-16 14:40:01 -08001412 loadConstant(cUnit, r0,
1413 (int) (cUnit->method->insns + mir->offset +
Dan Bornsteine4852762010-12-02 12:45:00 -08001414 dexGetWidthFromOpcode(OP_MONITOR_EXIT)));
Bill Buzbee6bbdd6b2010-02-16 14:40:01 -08001415 genDispatchToHandler(cUnit, TEMPLATE_THROW_EXCEPTION_COMMON);
1416 ArmLIR *target = newLIR0(cUnit, kArmPseudoTargetLabel);
1417 target->defMask = ENCODE_ALL;
1418 branchOver->generic.target = (LIR *) target;
Elliott Hughes6a555132010-02-25 15:41:42 -08001419 dvmCompilerClobberCallRegs(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07001420 }
Bill Buzbee270c1d62009-08-13 16:58:07 -07001421}
Ben Chengfc075c22010-05-28 15:20:08 -07001422#endif
Bill Buzbee270c1d62009-08-13 16:58:07 -07001423
Ben Chengba4fc8b2009-06-01 13:00:29 -07001424/*
buzbee9a3147c2011-03-02 15:43:48 -08001425 * Fetch *self->info.breakFlags. If the breakFlags are non-zero,
Ben Cheng7ab74e12011-02-03 14:02:06 -08001426 * punt to the interpreter.
1427 */
1428static void genSuspendPoll(CompilationUnit *cUnit, MIR *mir)
1429{
1430 int rTemp = dvmCompilerAllocTemp(cUnit);
1431 ArmLIR *ld;
buzbee9a3147c2011-03-02 15:43:48 -08001432 ld = loadBaseDisp(cUnit, NULL, r6SELF,
1433 offsetof(Thread, interpBreak.ctl.breakFlags),
1434 rTemp, kUnsignedByte, INVALID_SREG);
Ben Cheng7ab74e12011-02-03 14:02:06 -08001435 setMemRefType(ld, true /* isLoad */, kMustNotAlias);
Ben Cheng7ab74e12011-02-03 14:02:06 -08001436 genRegImmCheck(cUnit, kArmCondNe, rTemp, 0, mir->offset, NULL);
1437}
1438
1439/*
Ben Chengba4fc8b2009-06-01 13:00:29 -07001440 * The following are the first-level codegen routines that analyze the format
1441 * of each bytecode then either dispatch special purpose codegen routines
1442 * or produce corresponding Thumb instructions directly.
1443 */
1444
1445static bool handleFmt10t_Fmt20t_Fmt30t(CompilationUnit *cUnit, MIR *mir,
Bill Buzbee89efc3d2009-07-28 11:22:22 -07001446 BasicBlock *bb, ArmLIR *labelList)
Ben Chengba4fc8b2009-06-01 13:00:29 -07001447{
Ben Cheng7ab74e12011-02-03 14:02:06 -08001448 /* backward branch? */
1449 bool backwardBranch = (bb->taken->startOffset <= mir->offset);
1450
1451 if (backwardBranch && gDvmJit.genSuspendPoll) {
1452 genSuspendPoll(cUnit, mir);
1453 }
1454
1455 int numPredecessors = dvmCountSetBits(bb->taken->predecessors);
1456 /*
1457 * Things could be hoisted out of the taken block into the predecessor, so
1458 * make sure it is dominated by the predecessor.
1459 */
1460 if (numPredecessors == 1 && bb->taken->visited == false &&
Ben Cheng20d7e6c2011-02-18 17:12:42 -08001461 bb->taken->blockType == kDalvikByteCode) {
Ben Cheng7ab74e12011-02-03 14:02:06 -08001462 cUnit->nextCodegenBlock = bb->taken;
1463 } else {
1464 /* For OP_GOTO, OP_GOTO_16, and OP_GOTO_32 */
1465 genUnconditionalBranch(cUnit, &labelList[bb->taken->id]);
1466 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07001467 return false;
1468}
1469
1470static bool handleFmt10x(CompilationUnit *cUnit, MIR *mir)
1471{
Dan Bornstein9a1f8162010-12-01 17:02:26 -08001472 Opcode dalvikOpcode = mir->dalvikInsn.opcode;
1473 if ((dalvikOpcode >= OP_UNUSED_3E) && (dalvikOpcode <= OP_UNUSED_43)) {
1474 LOGE("Codegen: got unused opcode 0x%x\n",dalvikOpcode);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001475 return true;
1476 }
Dan Bornstein9a1f8162010-12-01 17:02:26 -08001477 switch (dalvikOpcode) {
Andy McFadden291758c2010-09-10 08:04:52 -07001478 case OP_RETURN_VOID_BARRIER:
buzbee2ce33c92010-11-01 15:53:27 -07001479 dvmCompilerGenMemBarrier(cUnit, kST);
1480 // Intentional fallthrough
1481 case OP_RETURN_VOID:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001482 genReturnCommon(cUnit,mir);
1483 break;
1484 case OP_UNUSED_73:
1485 case OP_UNUSED_79:
1486 case OP_UNUSED_7A:
Dan Bornstein90f15432010-12-02 16:46:25 -08001487 case OP_DISPATCH_FF:
Dan Bornstein9a1f8162010-12-01 17:02:26 -08001488 LOGE("Codegen: got unused opcode 0x%x\n",dalvikOpcode);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001489 return true;
1490 case OP_NOP:
1491 break;
1492 default:
1493 return true;
1494 }
1495 return false;
1496}
1497
1498static bool handleFmt11n_Fmt31i(CompilationUnit *cUnit, MIR *mir)
1499{
Bill Buzbee1465db52009-09-23 17:17:35 -07001500 RegLocation rlDest;
1501 RegLocation rlResult;
1502 if (mir->ssaRep->numDefs == 2) {
Bill Buzbeec6f10662010-02-09 11:16:15 -08001503 rlDest = dvmCompilerGetDestWide(cUnit, mir, 0, 1);
Bill Buzbee1465db52009-09-23 17:17:35 -07001504 } else {
Bill Buzbeec6f10662010-02-09 11:16:15 -08001505 rlDest = dvmCompilerGetDest(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07001506 }
Ben Chenge9695e52009-06-16 16:11:47 -07001507
Dan Bornstein9a1f8162010-12-01 17:02:26 -08001508 switch (mir->dalvikInsn.opcode) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07001509 case OP_CONST:
Ben Chenge9695e52009-06-16 16:11:47 -07001510 case OP_CONST_4: {
Bill Buzbeec6f10662010-02-09 11:16:15 -08001511 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kAnyReg, true);
Ben Chengbd1326d2010-04-02 15:04:53 -07001512 loadConstantNoClobber(cUnit, rlResult.lowReg, mir->dalvikInsn.vB);
Bill Buzbee1465db52009-09-23 17:17:35 -07001513 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001514 break;
Ben Chenge9695e52009-06-16 16:11:47 -07001515 }
1516 case OP_CONST_WIDE_32: {
Bill Buzbee1465db52009-09-23 17:17:35 -07001517 //TUNING: single routine to load constant pair for support doubles
Bill Buzbee964a7b02010-01-28 12:54:19 -08001518 //TUNING: load 0/-1 separately to avoid load dependency
Bill Buzbeec6f10662010-02-09 11:16:15 -08001519 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Ben Chengbd1326d2010-04-02 15:04:53 -07001520 loadConstantNoClobber(cUnit, rlResult.lowReg, mir->dalvikInsn.vB);
Bill Buzbee1465db52009-09-23 17:17:35 -07001521 opRegRegImm(cUnit, kOpAsr, rlResult.highReg,
1522 rlResult.lowReg, 31);
1523 storeValueWide(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001524 break;
Ben Chenge9695e52009-06-16 16:11:47 -07001525 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07001526 default:
1527 return true;
1528 }
1529 return false;
1530}
1531
1532static bool handleFmt21h(CompilationUnit *cUnit, MIR *mir)
1533{
Bill Buzbee1465db52009-09-23 17:17:35 -07001534 RegLocation rlDest;
1535 RegLocation rlResult;
1536 if (mir->ssaRep->numDefs == 2) {
Bill Buzbeec6f10662010-02-09 11:16:15 -08001537 rlDest = dvmCompilerGetDestWide(cUnit, mir, 0, 1);
Bill Buzbee1465db52009-09-23 17:17:35 -07001538 } else {
Bill Buzbeec6f10662010-02-09 11:16:15 -08001539 rlDest = dvmCompilerGetDest(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07001540 }
Bill Buzbeec6f10662010-02-09 11:16:15 -08001541 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kAnyReg, true);
Ben Chenge9695e52009-06-16 16:11:47 -07001542
Dan Bornstein9a1f8162010-12-01 17:02:26 -08001543 switch (mir->dalvikInsn.opcode) {
Ben Chenge9695e52009-06-16 16:11:47 -07001544 case OP_CONST_HIGH16: {
Ben Chengbd1326d2010-04-02 15:04:53 -07001545 loadConstantNoClobber(cUnit, rlResult.lowReg,
1546 mir->dalvikInsn.vB << 16);
Bill Buzbee1465db52009-09-23 17:17:35 -07001547 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001548 break;
Ben Chenge9695e52009-06-16 16:11:47 -07001549 }
1550 case OP_CONST_WIDE_HIGH16: {
Bill Buzbee1465db52009-09-23 17:17:35 -07001551 loadConstantValueWide(cUnit, rlResult.lowReg, rlResult.highReg,
1552 0, mir->dalvikInsn.vB << 16);
1553 storeValueWide(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001554 break;
Ben Chenge9695e52009-06-16 16:11:47 -07001555 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07001556 default:
1557 return true;
1558 }
1559 return false;
1560}
1561
jeffhao71eee1f2011-01-04 14:18:54 -08001562static bool handleFmt20bc_Fmt40sc(CompilationUnit *cUnit, MIR *mir)
Ben Chengba4fc8b2009-06-01 13:00:29 -07001563{
jeffhao71eee1f2011-01-04 14:18:54 -08001564 /* For OP_THROW_VERIFICATION_ERROR & OP_THROW_VERIFICATION_ERROR_JUMBO */
Ben Chengba4fc8b2009-06-01 13:00:29 -07001565 genInterpSingleStep(cUnit, mir);
1566 return false;
1567}
1568
jeffhao71eee1f2011-01-04 14:18:54 -08001569static bool handleFmt21c_Fmt31c_Fmt41c(CompilationUnit *cUnit, MIR *mir)
Ben Chengba4fc8b2009-06-01 13:00:29 -07001570{
Bill Buzbee1465db52009-09-23 17:17:35 -07001571 RegLocation rlResult;
1572 RegLocation rlDest;
1573 RegLocation rlSrc;
Ben Chenge9695e52009-06-16 16:11:47 -07001574
Dan Bornstein9a1f8162010-12-01 17:02:26 -08001575 switch (mir->dalvikInsn.opcode) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07001576 case OP_CONST_STRING_JUMBO:
1577 case OP_CONST_STRING: {
1578 void *strPtr = (void*)
1579 (cUnit->method->clazz->pDvmDex->pResStrings[mir->dalvikInsn.vB]);
Ben Chengdd6e8702010-05-07 13:05:47 -07001580
1581 if (strPtr == NULL) {
1582 LOGE("Unexpected null string");
1583 dvmAbort();
1584 }
1585
Bill Buzbeec6f10662010-02-09 11:16:15 -08001586 rlDest = dvmCompilerGetDest(cUnit, mir, 0);
1587 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Ben Chengbd1326d2010-04-02 15:04:53 -07001588 loadConstantNoClobber(cUnit, rlResult.lowReg, (int) strPtr );
Bill Buzbee1465db52009-09-23 17:17:35 -07001589 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001590 break;
1591 }
jeffhao71eee1f2011-01-04 14:18:54 -08001592 case OP_CONST_CLASS:
1593 case OP_CONST_CLASS_JUMBO: {
Ben Chengba4fc8b2009-06-01 13:00:29 -07001594 void *classPtr = (void*)
1595 (cUnit->method->clazz->pDvmDex->pResClasses[mir->dalvikInsn.vB]);
Ben Chengdd6e8702010-05-07 13:05:47 -07001596
1597 if (classPtr == NULL) {
1598 LOGE("Unexpected null class");
1599 dvmAbort();
1600 }
1601
Bill Buzbeec6f10662010-02-09 11:16:15 -08001602 rlDest = dvmCompilerGetDest(cUnit, mir, 0);
1603 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Ben Chengbd1326d2010-04-02 15:04:53 -07001604 loadConstantNoClobber(cUnit, rlResult.lowReg, (int) classPtr );
Bill Buzbee1465db52009-09-23 17:17:35 -07001605 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001606 break;
1607 }
jeffhao71eee1f2011-01-04 14:18:54 -08001608 case OP_SGET:
buzbeeecf8f6e2010-07-20 14:53:42 -07001609 case OP_SGET_VOLATILE:
jeffhao71eee1f2011-01-04 14:18:54 -08001610 case OP_SGET_JUMBO:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001611 case OP_SGET_OBJECT:
jeffhao71eee1f2011-01-04 14:18:54 -08001612 case OP_SGET_OBJECT_VOLATILE:
1613 case OP_SGET_OBJECT_JUMBO:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001614 case OP_SGET_BOOLEAN:
jeffhao71eee1f2011-01-04 14:18:54 -08001615 case OP_SGET_BOOLEAN_JUMBO:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001616 case OP_SGET_CHAR:
jeffhao71eee1f2011-01-04 14:18:54 -08001617 case OP_SGET_CHAR_JUMBO:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001618 case OP_SGET_BYTE:
jeffhao71eee1f2011-01-04 14:18:54 -08001619 case OP_SGET_BYTE_JUMBO:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001620 case OP_SGET_SHORT:
jeffhao71eee1f2011-01-04 14:18:54 -08001621 case OP_SGET_SHORT_JUMBO: {
Bill Buzbee50a6bf22009-07-08 13:08:04 -07001622 int valOffset = offsetof(StaticField, value);
Bill Buzbeec6f10662010-02-09 11:16:15 -08001623 int tReg = dvmCompilerAllocTemp(cUnit);
buzbeeecf8f6e2010-07-20 14:53:42 -07001624 bool isVolatile;
Ben Cheng7a2697d2010-06-07 13:44:23 -07001625 const Method *method = (mir->OptimizationFlags & MIR_CALLEE) ?
1626 mir->meta.calleeMethod : cUnit->method;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001627 void *fieldPtr = (void*)
Ben Cheng7a2697d2010-06-07 13:44:23 -07001628 (method->clazz->pDvmDex->pResFields[mir->dalvikInsn.vB]);
Ben Chengdd6e8702010-05-07 13:05:47 -07001629
1630 if (fieldPtr == NULL) {
1631 LOGE("Unexpected null static field");
1632 dvmAbort();
1633 }
1634
Dan Bornstein9a1f8162010-12-01 17:02:26 -08001635 isVolatile = (mir->dalvikInsn.opcode == OP_SGET_VOLATILE) ||
1636 (mir->dalvikInsn.opcode == OP_SGET_OBJECT_VOLATILE) ||
Carl Shapirofc75f3e2010-12-07 11:43:38 -08001637 dvmIsVolatileField((Field *) fieldPtr);
buzbeeecf8f6e2010-07-20 14:53:42 -07001638
Bill Buzbeec6f10662010-02-09 11:16:15 -08001639 rlDest = dvmCompilerGetDest(cUnit, mir, 0);
1640 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kAnyReg, true);
Bill Buzbee1465db52009-09-23 17:17:35 -07001641 loadConstant(cUnit, tReg, (int) fieldPtr + valOffset);
Ben Cheng11d8f142010-03-24 15:24:19 -07001642
buzbeeecf8f6e2010-07-20 14:53:42 -07001643 if (isVolatile) {
buzbee2ce33c92010-11-01 15:53:27 -07001644 dvmCompilerGenMemBarrier(cUnit, kSY);
buzbeeecf8f6e2010-07-20 14:53:42 -07001645 }
Ben Cheng11d8f142010-03-24 15:24:19 -07001646 HEAP_ACCESS_SHADOW(true);
Bill Buzbee1465db52009-09-23 17:17:35 -07001647 loadWordDisp(cUnit, tReg, 0, rlResult.lowReg);
Ben Cheng11d8f142010-03-24 15:24:19 -07001648 HEAP_ACCESS_SHADOW(false);
1649
Bill Buzbee1465db52009-09-23 17:17:35 -07001650 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001651 break;
1652 }
jeffhao71eee1f2011-01-04 14:18:54 -08001653 case OP_SGET_WIDE:
1654 case OP_SGET_WIDE_JUMBO: {
Bill Buzbee50a6bf22009-07-08 13:08:04 -07001655 int valOffset = offsetof(StaticField, value);
Ben Cheng7a2697d2010-06-07 13:44:23 -07001656 const Method *method = (mir->OptimizationFlags & MIR_CALLEE) ?
1657 mir->meta.calleeMethod : cUnit->method;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001658 void *fieldPtr = (void*)
Ben Cheng7a2697d2010-06-07 13:44:23 -07001659 (method->clazz->pDvmDex->pResFields[mir->dalvikInsn.vB]);
Ben Chengdd6e8702010-05-07 13:05:47 -07001660
1661 if (fieldPtr == NULL) {
1662 LOGE("Unexpected null static field");
1663 dvmAbort();
1664 }
1665
Bill Buzbeec6f10662010-02-09 11:16:15 -08001666 int tReg = dvmCompilerAllocTemp(cUnit);
Bill Buzbeec6f10662010-02-09 11:16:15 -08001667 rlDest = dvmCompilerGetDestWide(cUnit, mir, 0, 1);
1668 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kAnyReg, true);
Bill Buzbee1465db52009-09-23 17:17:35 -07001669 loadConstant(cUnit, tReg, (int) fieldPtr + valOffset);
Ben Cheng11d8f142010-03-24 15:24:19 -07001670
1671 HEAP_ACCESS_SHADOW(true);
Bill Buzbee1465db52009-09-23 17:17:35 -07001672 loadPair(cUnit, tReg, rlResult.lowReg, rlResult.highReg);
Ben Cheng11d8f142010-03-24 15:24:19 -07001673 HEAP_ACCESS_SHADOW(false);
1674
Bill Buzbee1465db52009-09-23 17:17:35 -07001675 storeValueWide(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001676 break;
1677 }
jeffhao71eee1f2011-01-04 14:18:54 -08001678 case OP_SPUT:
1679 case OP_SPUT_VOLATILE:
1680 case OP_SPUT_JUMBO:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001681 case OP_SPUT_OBJECT:
buzbeeddc7d292010-09-02 17:16:24 -07001682 case OP_SPUT_OBJECT_VOLATILE:
jeffhao71eee1f2011-01-04 14:18:54 -08001683 case OP_SPUT_OBJECT_JUMBO:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001684 case OP_SPUT_BOOLEAN:
jeffhao71eee1f2011-01-04 14:18:54 -08001685 case OP_SPUT_BOOLEAN_JUMBO:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001686 case OP_SPUT_CHAR:
jeffhao71eee1f2011-01-04 14:18:54 -08001687 case OP_SPUT_CHAR_JUMBO:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001688 case OP_SPUT_BYTE:
jeffhao71eee1f2011-01-04 14:18:54 -08001689 case OP_SPUT_BYTE_JUMBO:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001690 case OP_SPUT_SHORT:
jeffhao71eee1f2011-01-04 14:18:54 -08001691 case OP_SPUT_SHORT_JUMBO: {
Bill Buzbee50a6bf22009-07-08 13:08:04 -07001692 int valOffset = offsetof(StaticField, value);
Bill Buzbeec6f10662010-02-09 11:16:15 -08001693 int tReg = dvmCompilerAllocTemp(cUnit);
buzbeed3b0a4b2010-09-27 11:30:22 -07001694 int objHead;
buzbeeecf8f6e2010-07-20 14:53:42 -07001695 bool isVolatile;
buzbeed3b0a4b2010-09-27 11:30:22 -07001696 bool isSputObject;
Ben Cheng7a2697d2010-06-07 13:44:23 -07001697 const Method *method = (mir->OptimizationFlags & MIR_CALLEE) ?
1698 mir->meta.calleeMethod : cUnit->method;
1699 void *fieldPtr = (void*)
1700 (method->clazz->pDvmDex->pResFields[mir->dalvikInsn.vB]);
Ben Chenge9695e52009-06-16 16:11:47 -07001701
Dan Bornstein9a1f8162010-12-01 17:02:26 -08001702 isVolatile = (mir->dalvikInsn.opcode == OP_SPUT_VOLATILE) ||
1703 (mir->dalvikInsn.opcode == OP_SPUT_OBJECT_VOLATILE) ||
Carl Shapirofc75f3e2010-12-07 11:43:38 -08001704 dvmIsVolatileField((Field *) fieldPtr);
buzbeeecf8f6e2010-07-20 14:53:42 -07001705
Dan Bornstein9a1f8162010-12-01 17:02:26 -08001706 isSputObject = (mir->dalvikInsn.opcode == OP_SPUT_OBJECT) ||
jeffhao71eee1f2011-01-04 14:18:54 -08001707 (mir->dalvikInsn.opcode == OP_SPUT_OBJECT_JUMBO) ||
Dan Bornstein9a1f8162010-12-01 17:02:26 -08001708 (mir->dalvikInsn.opcode == OP_SPUT_OBJECT_VOLATILE);
buzbeed3b0a4b2010-09-27 11:30:22 -07001709
Ben Chengdd6e8702010-05-07 13:05:47 -07001710 if (fieldPtr == NULL) {
1711 LOGE("Unexpected null static field");
1712 dvmAbort();
1713 }
1714
Bill Buzbeec6f10662010-02-09 11:16:15 -08001715 rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07001716 rlSrc = loadValue(cUnit, rlSrc, kAnyReg);
buzbeeb78c76f2010-09-30 19:08:20 -07001717 loadConstant(cUnit, tReg, (int) fieldPtr);
buzbeed3b0a4b2010-09-27 11:30:22 -07001718 if (isSputObject) {
1719 objHead = dvmCompilerAllocTemp(cUnit);
buzbeeb78c76f2010-09-30 19:08:20 -07001720 loadWordDisp(cUnit, tReg, offsetof(Field, clazz), objHead);
buzbeed3b0a4b2010-09-27 11:30:22 -07001721 }
Ben Cheng11d8f142010-03-24 15:24:19 -07001722 HEAP_ACCESS_SHADOW(true);
buzbeeb78c76f2010-09-30 19:08:20 -07001723 storeWordDisp(cUnit, tReg, valOffset ,rlSrc.lowReg);
buzbeed3b0a4b2010-09-27 11:30:22 -07001724 dvmCompilerFreeTemp(cUnit, tReg);
Ben Cheng11d8f142010-03-24 15:24:19 -07001725 HEAP_ACCESS_SHADOW(false);
buzbeeecf8f6e2010-07-20 14:53:42 -07001726 if (isVolatile) {
buzbee2ce33c92010-11-01 15:53:27 -07001727 dvmCompilerGenMemBarrier(cUnit, kSY);
buzbeeecf8f6e2010-07-20 14:53:42 -07001728 }
buzbeed3b0a4b2010-09-27 11:30:22 -07001729 if (isSputObject) {
buzbeeb78c76f2010-09-30 19:08:20 -07001730 /* NOTE: marking card based sfield->clazz */
buzbeed3b0a4b2010-09-27 11:30:22 -07001731 markCard(cUnit, rlSrc.lowReg, objHead);
1732 dvmCompilerFreeTemp(cUnit, objHead);
buzbee919eb062010-07-12 12:59:22 -07001733 }
Ben Cheng11d8f142010-03-24 15:24:19 -07001734
Ben Chengba4fc8b2009-06-01 13:00:29 -07001735 break;
1736 }
jeffhao71eee1f2011-01-04 14:18:54 -08001737 case OP_SPUT_WIDE:
1738 case OP_SPUT_WIDE_JUMBO: {
Bill Buzbeec6f10662010-02-09 11:16:15 -08001739 int tReg = dvmCompilerAllocTemp(cUnit);
Bill Buzbee50a6bf22009-07-08 13:08:04 -07001740 int valOffset = offsetof(StaticField, value);
Ben Cheng7a2697d2010-06-07 13:44:23 -07001741 const Method *method = (mir->OptimizationFlags & MIR_CALLEE) ?
1742 mir->meta.calleeMethod : cUnit->method;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001743 void *fieldPtr = (void*)
Ben Cheng7a2697d2010-06-07 13:44:23 -07001744 (method->clazz->pDvmDex->pResFields[mir->dalvikInsn.vB]);
Ben Chenge9695e52009-06-16 16:11:47 -07001745
Ben Chengdd6e8702010-05-07 13:05:47 -07001746 if (fieldPtr == NULL) {
1747 LOGE("Unexpected null static field");
1748 dvmAbort();
1749 }
1750
Bill Buzbeec6f10662010-02-09 11:16:15 -08001751 rlSrc = dvmCompilerGetSrcWide(cUnit, mir, 0, 1);
Bill Buzbee1465db52009-09-23 17:17:35 -07001752 rlSrc = loadValueWide(cUnit, rlSrc, kAnyReg);
1753 loadConstant(cUnit, tReg, (int) fieldPtr + valOffset);
Ben Cheng11d8f142010-03-24 15:24:19 -07001754
1755 HEAP_ACCESS_SHADOW(true);
Bill Buzbee1465db52009-09-23 17:17:35 -07001756 storePair(cUnit, tReg, rlSrc.lowReg, rlSrc.highReg);
Ben Cheng11d8f142010-03-24 15:24:19 -07001757 HEAP_ACCESS_SHADOW(false);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001758 break;
1759 }
jeffhao71eee1f2011-01-04 14:18:54 -08001760 case OP_NEW_INSTANCE:
1761 case OP_NEW_INSTANCE_JUMBO: {
Ben Chenge9695e52009-06-16 16:11:47 -07001762 /*
1763 * Obey the calling convention and don't mess with the register
1764 * usage.
1765 */
Carl Shapirofc75f3e2010-12-07 11:43:38 -08001766 ClassObject *classPtr = (ClassObject *)
Ben Chengba4fc8b2009-06-01 13:00:29 -07001767 (cUnit->method->clazz->pDvmDex->pResClasses[mir->dalvikInsn.vB]);
Ben Chengdd6e8702010-05-07 13:05:47 -07001768
1769 if (classPtr == NULL) {
1770 LOGE("Unexpected null class");
1771 dvmAbort();
1772 }
1773
Ben Cheng79d173c2009-09-29 16:12:51 -07001774 /*
1775 * If it is going to throw, it should not make to the trace to begin
Bill Buzbee1465db52009-09-23 17:17:35 -07001776 * with. However, Alloc might throw, so we need to genExportPC()
Ben Cheng79d173c2009-09-29 16:12:51 -07001777 */
1778 assert((classPtr->accessFlags & (ACC_INTERFACE|ACC_ABSTRACT)) == 0);
Bill Buzbeec6f10662010-02-09 11:16:15 -08001779 dvmCompilerFlushAllRegs(cUnit); /* Everything to home location */
Bill Buzbee1465db52009-09-23 17:17:35 -07001780 genExportPC(cUnit, mir);
Ben Chengbd1326d2010-04-02 15:04:53 -07001781 LOAD_FUNC_ADDR(cUnit, r2, (int)dvmAllocObject);
Ben Chenge9695e52009-06-16 16:11:47 -07001782 loadConstant(cUnit, r0, (int) classPtr);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001783 loadConstant(cUnit, r1, ALLOC_DONT_TRACK);
Bill Buzbee1465db52009-09-23 17:17:35 -07001784 opReg(cUnit, kOpBlx, r2);
Elliott Hughes6a555132010-02-25 15:41:42 -08001785 dvmCompilerClobberCallRegs(cUnit);
Ben Cheng4f489172009-09-27 17:08:35 -07001786 /* generate a branch over if allocation is successful */
buzbee8f8109a2010-08-31 10:16:35 -07001787 ArmLIR *branchOver = genCmpImmBranch(cUnit, kArmCondNe, r0, 0);
Ben Cheng4f489172009-09-27 17:08:35 -07001788 /*
1789 * OOM exception needs to be thrown here and cannot re-execute
1790 */
1791 loadConstant(cUnit, r0,
1792 (int) (cUnit->method->insns + mir->offset));
1793 genDispatchToHandler(cUnit, TEMPLATE_THROW_EXCEPTION_COMMON);
1794 /* noreturn */
1795
Bill Buzbee1465db52009-09-23 17:17:35 -07001796 ArmLIR *target = newLIR0(cUnit, kArmPseudoTargetLabel);
Ben Cheng4f489172009-09-27 17:08:35 -07001797 target->defMask = ENCODE_ALL;
1798 branchOver->generic.target = (LIR *) target;
Bill Buzbeec6f10662010-02-09 11:16:15 -08001799 rlDest = dvmCompilerGetDest(cUnit, mir, 0);
1800 rlResult = dvmCompilerGetReturn(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07001801 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001802 break;
1803 }
jeffhao71eee1f2011-01-04 14:18:54 -08001804 case OP_CHECK_CAST:
1805 case OP_CHECK_CAST_JUMBO: {
Ben Chenge9695e52009-06-16 16:11:47 -07001806 /*
1807 * Obey the calling convention and don't mess with the register
1808 * usage.
1809 */
Ben Chengba4fc8b2009-06-01 13:00:29 -07001810 ClassObject *classPtr =
1811 (cUnit->method->clazz->pDvmDex->pResClasses[mir->dalvikInsn.vB]);
Bill Buzbee4df41a52009-11-12 17:07:16 -08001812 /*
1813 * Note: It is possible that classPtr is NULL at this point,
1814 * even though this instruction has been successfully interpreted.
1815 * If the previous interpretation had a null source, the
1816 * interpreter would not have bothered to resolve the clazz.
1817 * Bail out to the interpreter in this case, and log it
1818 * so that we can tell if it happens frequently.
1819 */
1820 if (classPtr == NULL) {
Ben Cheng11d8f142010-03-24 15:24:19 -07001821 LOGVV("null clazz in OP_CHECK_CAST, single-stepping");
Bill Buzbee4df41a52009-11-12 17:07:16 -08001822 genInterpSingleStep(cUnit, mir);
1823 return false;
1824 }
Bill Buzbeec6f10662010-02-09 11:16:15 -08001825 dvmCompilerFlushAllRegs(cUnit); /* Everything to home location */
Ben Chengba4fc8b2009-06-01 13:00:29 -07001826 loadConstant(cUnit, r1, (int) classPtr );
Bill Buzbeec6f10662010-02-09 11:16:15 -08001827 rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07001828 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
buzbee8f8109a2010-08-31 10:16:35 -07001829 /* Null? */
1830 ArmLIR *branch1 = genCmpImmBranch(cUnit, kArmCondEq,
1831 rlSrc.lowReg, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07001832 /*
1833 * rlSrc.lowReg now contains object->clazz. Note that
1834 * it could have been allocated r0, but we're okay so long
1835 * as we don't do anything desctructive until r0 is loaded
1836 * with clazz.
1837 */
Ben Chengba4fc8b2009-06-01 13:00:29 -07001838 /* r0 now contains object->clazz */
Bill Buzbee1465db52009-09-23 17:17:35 -07001839 loadWordDisp(cUnit, rlSrc.lowReg, offsetof(Object, clazz), r0);
Ben Chengbd1326d2010-04-02 15:04:53 -07001840 LOAD_FUNC_ADDR(cUnit, r2, (int)dvmInstanceofNonTrivial);
Bill Buzbee1465db52009-09-23 17:17:35 -07001841 opRegReg(cUnit, kOpCmp, r0, r1);
1842 ArmLIR *branch2 = opCondBranch(cUnit, kArmCondEq);
1843 opReg(cUnit, kOpBlx, r2);
Elliott Hughes6a555132010-02-25 15:41:42 -08001844 dvmCompilerClobberCallRegs(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07001845 /*
1846 * If null, check cast failed - punt to the interpreter. Because
1847 * interpreter will be the one throwing, we don't need to
1848 * genExportPC() here.
1849 */
Bill Buzbee270c1d62009-08-13 16:58:07 -07001850 genZeroCheck(cUnit, r0, mir->offset, NULL);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001851 /* check cast passed - branch target here */
Bill Buzbee1465db52009-09-23 17:17:35 -07001852 ArmLIR *target = newLIR0(cUnit, kArmPseudoTargetLabel);
Ben Chengd7d426a2009-09-22 11:23:36 -07001853 target->defMask = ENCODE_ALL;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001854 branch1->generic.target = (LIR *)target;
1855 branch2->generic.target = (LIR *)target;
1856 break;
1857 }
buzbee4d92e682010-07-29 15:24:14 -07001858 case OP_SGET_WIDE_VOLATILE:
1859 case OP_SPUT_WIDE_VOLATILE:
1860 genInterpSingleStep(cUnit, mir);
1861 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001862 default:
1863 return true;
1864 }
1865 return false;
1866}
1867
Ben Cheng7a2697d2010-06-07 13:44:23 -07001868/*
1869 * A typical example of inlined getter/setter from a monomorphic callsite:
1870 *
1871 * D/dalvikvm( 289): -------- dalvik offset: 0x0000 @ invoke-static (I)
1872 * D/dalvikvm( 289): -------- dalvik offset: 0x0000 @ sget-object (C) v0, ...
1873 * D/dalvikvm( 289): 0x4427fc22 (0002): ldr r0, [pc, #56]
1874 * D/dalvikvm( 289): 0x4427fc24 (0004): ldr r1, [r0, #0]
1875 * D/dalvikvm( 289): 0x4427fc26 (0006): str r1, [r5, #0]
1876 * D/dalvikvm( 289): 0x4427fc28 (0008): .align4
1877 * D/dalvikvm( 289): L0x0003:
1878 * D/dalvikvm( 289): -------- dalvik offset: 0x0003 @ move-result-object (I) v0
1879 *
1880 * Note the invoke-static and move-result-object with the (I) notation are
1881 * turned into no-op.
1882 */
Ben Chengba4fc8b2009-06-01 13:00:29 -07001883static bool handleFmt11x(CompilationUnit *cUnit, MIR *mir)
1884{
Dan Bornstein9a1f8162010-12-01 17:02:26 -08001885 Opcode dalvikOpcode = mir->dalvikInsn.opcode;
Bill Buzbee1465db52009-09-23 17:17:35 -07001886 RegLocation rlResult;
Dan Bornstein9a1f8162010-12-01 17:02:26 -08001887 switch (dalvikOpcode) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07001888 case OP_MOVE_EXCEPTION: {
Ben Chengba4fc8b2009-06-01 13:00:29 -07001889 int exOffset = offsetof(Thread, exception);
Bill Buzbeec6f10662010-02-09 11:16:15 -08001890 int resetReg = dvmCompilerAllocTemp(cUnit);
1891 RegLocation rlDest = dvmCompilerGetDest(cUnit, mir, 0);
1892 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Ben Cheng20d7e6c2011-02-18 17:12:42 -08001893 loadWordDisp(cUnit, r6SELF, exOffset, rlResult.lowReg);
Bill Buzbeef9f33282009-11-22 12:45:30 -08001894 loadConstant(cUnit, resetReg, 0);
Ben Cheng20d7e6c2011-02-18 17:12:42 -08001895 storeWordDisp(cUnit, r6SELF, exOffset, resetReg);
Bill Buzbee1465db52009-09-23 17:17:35 -07001896 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001897 break;
1898 }
1899 case OP_MOVE_RESULT:
1900 case OP_MOVE_RESULT_OBJECT: {
Ben Cheng7a2697d2010-06-07 13:44:23 -07001901 /* An inlined move result is effectively no-op */
1902 if (mir->OptimizationFlags & MIR_INLINED)
1903 break;
Bill Buzbeec6f10662010-02-09 11:16:15 -08001904 RegLocation rlDest = dvmCompilerGetDest(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07001905 RegLocation rlSrc = LOC_DALVIK_RETURN_VAL;
1906 rlSrc.fp = rlDest.fp;
1907 storeValue(cUnit, rlDest, rlSrc);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001908 break;
1909 }
1910 case OP_MOVE_RESULT_WIDE: {
Ben Cheng7a2697d2010-06-07 13:44:23 -07001911 /* An inlined move result is effectively no-op */
1912 if (mir->OptimizationFlags & MIR_INLINED)
1913 break;
Bill Buzbeec6f10662010-02-09 11:16:15 -08001914 RegLocation rlDest = dvmCompilerGetDestWide(cUnit, mir, 0, 1);
Bill Buzbee1465db52009-09-23 17:17:35 -07001915 RegLocation rlSrc = LOC_DALVIK_RETURN_VAL_WIDE;
1916 rlSrc.fp = rlDest.fp;
1917 storeValueWide(cUnit, rlDest, rlSrc);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001918 break;
1919 }
1920 case OP_RETURN_WIDE: {
Bill Buzbeec6f10662010-02-09 11:16:15 -08001921 RegLocation rlSrc = dvmCompilerGetSrcWide(cUnit, mir, 0, 1);
Bill Buzbee1465db52009-09-23 17:17:35 -07001922 RegLocation rlDest = LOC_DALVIK_RETURN_VAL_WIDE;
1923 rlDest.fp = rlSrc.fp;
1924 storeValueWide(cUnit, rlDest, rlSrc);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001925 genReturnCommon(cUnit,mir);
1926 break;
1927 }
1928 case OP_RETURN:
1929 case OP_RETURN_OBJECT: {
Bill Buzbeec6f10662010-02-09 11:16:15 -08001930 RegLocation rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07001931 RegLocation rlDest = LOC_DALVIK_RETURN_VAL;
1932 rlDest.fp = rlSrc.fp;
1933 storeValue(cUnit, rlDest, rlSrc);
Ben Cheng20d7e6c2011-02-18 17:12:42 -08001934 genReturnCommon(cUnit, mir);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001935 break;
1936 }
Bill Buzbee1465db52009-09-23 17:17:35 -07001937 case OP_MONITOR_EXIT:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001938 case OP_MONITOR_ENTER:
Ben Cheng5d90c202009-11-22 23:31:11 -08001939 genMonitor(cUnit, mir);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001940 break;
Ben Cheng20d7e6c2011-02-18 17:12:42 -08001941 case OP_THROW:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001942 genInterpSingleStep(cUnit, mir);
1943 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001944 default:
1945 return true;
1946 }
1947 return false;
1948}
1949
Bill Buzbeed45ba372009-06-15 17:00:57 -07001950static bool handleFmt12x(CompilationUnit *cUnit, MIR *mir)
1951{
Dan Bornstein9a1f8162010-12-01 17:02:26 -08001952 Opcode opcode = mir->dalvikInsn.opcode;
Bill Buzbee1465db52009-09-23 17:17:35 -07001953 RegLocation rlDest;
1954 RegLocation rlSrc;
1955 RegLocation rlResult;
Bill Buzbeed45ba372009-06-15 17:00:57 -07001956
Dan Bornstein9a1f8162010-12-01 17:02:26 -08001957 if ( (opcode >= OP_ADD_INT_2ADDR) && (opcode <= OP_REM_DOUBLE_2ADDR)) {
Ben Cheng5d90c202009-11-22 23:31:11 -08001958 return genArithOp( cUnit, mir );
Ben Chengba4fc8b2009-06-01 13:00:29 -07001959 }
1960
Bill Buzbee1465db52009-09-23 17:17:35 -07001961 if (mir->ssaRep->numUses == 2)
Bill Buzbeec6f10662010-02-09 11:16:15 -08001962 rlSrc = dvmCompilerGetSrcWide(cUnit, mir, 0, 1);
Bill Buzbee1465db52009-09-23 17:17:35 -07001963 else
Bill Buzbeec6f10662010-02-09 11:16:15 -08001964 rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07001965 if (mir->ssaRep->numDefs == 2)
Bill Buzbeec6f10662010-02-09 11:16:15 -08001966 rlDest = dvmCompilerGetDestWide(cUnit, mir, 0, 1);
Bill Buzbee1465db52009-09-23 17:17:35 -07001967 else
Bill Buzbeec6f10662010-02-09 11:16:15 -08001968 rlDest = dvmCompilerGetDest(cUnit, mir, 0);
Ben Chenge9695e52009-06-16 16:11:47 -07001969
Dan Bornstein9a1f8162010-12-01 17:02:26 -08001970 switch (opcode) {
Bill Buzbee1465db52009-09-23 17:17:35 -07001971 case OP_DOUBLE_TO_INT:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001972 case OP_INT_TO_FLOAT:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001973 case OP_FLOAT_TO_INT:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001974 case OP_DOUBLE_TO_FLOAT:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001975 case OP_FLOAT_TO_DOUBLE:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001976 case OP_INT_TO_DOUBLE:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001977 case OP_FLOAT_TO_LONG:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001978 case OP_LONG_TO_FLOAT:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001979 case OP_DOUBLE_TO_LONG:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001980 case OP_LONG_TO_DOUBLE:
Ben Cheng5d90c202009-11-22 23:31:11 -08001981 return genConversion(cUnit, mir);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001982 case OP_NEG_INT:
1983 case OP_NOT_INT:
Ben Cheng5d90c202009-11-22 23:31:11 -08001984 return genArithOpInt(cUnit, mir, rlDest, rlSrc, rlSrc);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001985 case OP_NEG_LONG:
1986 case OP_NOT_LONG:
Ben Cheng5d90c202009-11-22 23:31:11 -08001987 return genArithOpLong(cUnit, mir, rlDest, rlSrc, rlSrc);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001988 case OP_NEG_FLOAT:
Ben Cheng5d90c202009-11-22 23:31:11 -08001989 return genArithOpFloat(cUnit, mir, rlDest, rlSrc, rlSrc);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001990 case OP_NEG_DOUBLE:
Ben Cheng5d90c202009-11-22 23:31:11 -08001991 return genArithOpDouble(cUnit, mir, rlDest, rlSrc, rlSrc);
Bill Buzbee1465db52009-09-23 17:17:35 -07001992 case OP_MOVE_WIDE:
1993 storeValueWide(cUnit, rlDest, rlSrc);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001994 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07001995 case OP_INT_TO_LONG:
Bill Buzbeec6f10662010-02-09 11:16:15 -08001996 rlSrc = dvmCompilerUpdateLoc(cUnit, rlSrc);
1997 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Bill Buzbee964a7b02010-01-28 12:54:19 -08001998 //TUNING: shouldn't loadValueDirect already check for phys reg?
Bill Buzbee1465db52009-09-23 17:17:35 -07001999 if (rlSrc.location == kLocPhysReg) {
2000 genRegCopy(cUnit, rlResult.lowReg, rlSrc.lowReg);
2001 } else {
2002 loadValueDirect(cUnit, rlSrc, rlResult.lowReg);
2003 }
2004 opRegRegImm(cUnit, kOpAsr, rlResult.highReg,
2005 rlResult.lowReg, 31);
2006 storeValueWide(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002007 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07002008 case OP_LONG_TO_INT:
Bill Buzbeec6f10662010-02-09 11:16:15 -08002009 rlSrc = dvmCompilerUpdateLocWide(cUnit, rlSrc);
2010 rlSrc = dvmCompilerWideToNarrow(cUnit, rlSrc);
Bill Buzbee1465db52009-09-23 17:17:35 -07002011 // Intentional fallthrough
Ben Chengba4fc8b2009-06-01 13:00:29 -07002012 case OP_MOVE:
2013 case OP_MOVE_OBJECT:
Bill Buzbee1465db52009-09-23 17:17:35 -07002014 storeValue(cUnit, rlDest, rlSrc);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002015 break;
2016 case OP_INT_TO_BYTE:
Bill Buzbee1465db52009-09-23 17:17:35 -07002017 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
Bill Buzbeec6f10662010-02-09 11:16:15 -08002018 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Bill Buzbee1465db52009-09-23 17:17:35 -07002019 opRegReg(cUnit, kOp2Byte, rlResult.lowReg, rlSrc.lowReg);
2020 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002021 break;
2022 case OP_INT_TO_SHORT:
Bill Buzbee1465db52009-09-23 17:17:35 -07002023 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
Bill Buzbeec6f10662010-02-09 11:16:15 -08002024 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Bill Buzbee1465db52009-09-23 17:17:35 -07002025 opRegReg(cUnit, kOp2Short, rlResult.lowReg, rlSrc.lowReg);
2026 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002027 break;
2028 case OP_INT_TO_CHAR:
Bill Buzbee1465db52009-09-23 17:17:35 -07002029 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
Bill Buzbeec6f10662010-02-09 11:16:15 -08002030 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Bill Buzbee1465db52009-09-23 17:17:35 -07002031 opRegReg(cUnit, kOp2Char, rlResult.lowReg, rlSrc.lowReg);
2032 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002033 break;
2034 case OP_ARRAY_LENGTH: {
2035 int lenOffset = offsetof(ArrayObject, length);
Bill Buzbee1465db52009-09-23 17:17:35 -07002036 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
2037 genNullCheck(cUnit, rlSrc.sRegLow, rlSrc.lowReg,
2038 mir->offset, NULL);
Bill Buzbeec6f10662010-02-09 11:16:15 -08002039 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Bill Buzbee1465db52009-09-23 17:17:35 -07002040 loadWordDisp(cUnit, rlSrc.lowReg, lenOffset,
2041 rlResult.lowReg);
2042 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002043 break;
2044 }
2045 default:
2046 return true;
2047 }
2048 return false;
2049}
2050
2051static bool handleFmt21s(CompilationUnit *cUnit, MIR *mir)
2052{
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002053 Opcode dalvikOpcode = mir->dalvikInsn.opcode;
Bill Buzbee1465db52009-09-23 17:17:35 -07002054 RegLocation rlDest;
2055 RegLocation rlResult;
2056 int BBBB = mir->dalvikInsn.vB;
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002057 if (dalvikOpcode == OP_CONST_WIDE_16) {
Bill Buzbeec6f10662010-02-09 11:16:15 -08002058 rlDest = dvmCompilerGetDestWide(cUnit, mir, 0, 1);
2059 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Ben Chengbd1326d2010-04-02 15:04:53 -07002060 loadConstantNoClobber(cUnit, rlResult.lowReg, BBBB);
Bill Buzbee964a7b02010-01-28 12:54:19 -08002061 //TUNING: do high separately to avoid load dependency
Bill Buzbee1465db52009-09-23 17:17:35 -07002062 opRegRegImm(cUnit, kOpAsr, rlResult.highReg, rlResult.lowReg, 31);
2063 storeValueWide(cUnit, rlDest, rlResult);
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002064 } else if (dalvikOpcode == OP_CONST_16) {
Bill Buzbeec6f10662010-02-09 11:16:15 -08002065 rlDest = dvmCompilerGetDest(cUnit, mir, 0);
2066 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kAnyReg, true);
Ben Chengbd1326d2010-04-02 15:04:53 -07002067 loadConstantNoClobber(cUnit, rlResult.lowReg, BBBB);
Bill Buzbee1465db52009-09-23 17:17:35 -07002068 storeValue(cUnit, rlDest, rlResult);
2069 } else
Ben Chengba4fc8b2009-06-01 13:00:29 -07002070 return true;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002071 return false;
2072}
2073
2074/* Compare agaist zero */
2075static bool handleFmt21t(CompilationUnit *cUnit, MIR *mir, BasicBlock *bb,
Bill Buzbee89efc3d2009-07-28 11:22:22 -07002076 ArmLIR *labelList)
Ben Chengba4fc8b2009-06-01 13:00:29 -07002077{
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002078 Opcode dalvikOpcode = mir->dalvikInsn.opcode;
Bill Buzbee89efc3d2009-07-28 11:22:22 -07002079 ArmConditionCode cond;
Ben Cheng7ab74e12011-02-03 14:02:06 -08002080 /* backward branch? */
2081 bool backwardBranch = (bb->taken->startOffset <= mir->offset);
2082
2083 if (backwardBranch && gDvmJit.genSuspendPoll) {
2084 genSuspendPoll(cUnit, mir);
2085 }
2086
Bill Buzbeec6f10662010-02-09 11:16:15 -08002087 RegLocation rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07002088 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
Ben Cheng7ab74e12011-02-03 14:02:06 -08002089
Bill Buzbee1465db52009-09-23 17:17:35 -07002090 opRegImm(cUnit, kOpCmp, rlSrc.lowReg, 0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002091
Bill Buzbee270c1d62009-08-13 16:58:07 -07002092//TUNING: break this out to allow use of Thumb2 CB[N]Z
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002093 switch (dalvikOpcode) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07002094 case OP_IF_EQZ:
Bill Buzbee1465db52009-09-23 17:17:35 -07002095 cond = kArmCondEq;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002096 break;
2097 case OP_IF_NEZ:
Bill Buzbee1465db52009-09-23 17:17:35 -07002098 cond = kArmCondNe;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002099 break;
2100 case OP_IF_LTZ:
Bill Buzbee1465db52009-09-23 17:17:35 -07002101 cond = kArmCondLt;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002102 break;
2103 case OP_IF_GEZ:
Bill Buzbee1465db52009-09-23 17:17:35 -07002104 cond = kArmCondGe;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002105 break;
2106 case OP_IF_GTZ:
Bill Buzbee1465db52009-09-23 17:17:35 -07002107 cond = kArmCondGt;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002108 break;
2109 case OP_IF_LEZ:
Bill Buzbee1465db52009-09-23 17:17:35 -07002110 cond = kArmCondLe;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002111 break;
2112 default:
2113 cond = 0;
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002114 LOGE("Unexpected opcode (%d) for Fmt21t\n", dalvikOpcode);
Bill Buzbeefc519dc2010-03-06 23:30:57 -08002115 dvmCompilerAbort(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002116 }
2117 genConditionalBranch(cUnit, cond, &labelList[bb->taken->id]);
2118 /* This mostly likely will be optimized away in a later phase */
2119 genUnconditionalBranch(cUnit, &labelList[bb->fallThrough->id]);
2120 return false;
2121}
2122
Elliott Hughesb4c05972010-02-24 16:36:18 -08002123static bool isPowerOfTwo(int x)
2124{
2125 return (x & (x - 1)) == 0;
2126}
2127
2128// Returns true if no more than two bits are set in 'x'.
2129static bool isPopCountLE2(unsigned int x)
2130{
2131 x &= x - 1;
2132 return (x & (x - 1)) == 0;
2133}
2134
2135// Returns the index of the lowest set bit in 'x'.
2136static int lowestSetBit(unsigned int x) {
2137 int bit_posn = 0;
2138 while ((x & 0xf) == 0) {
2139 bit_posn += 4;
2140 x >>= 4;
Bill Buzbee78cb0e22010-02-11 14:04:53 -08002141 }
Elliott Hughesb4c05972010-02-24 16:36:18 -08002142 while ((x & 1) == 0) {
2143 bit_posn++;
2144 x >>= 1;
2145 }
2146 return bit_posn;
2147}
2148
Elliott Hughes672511b2010-04-26 17:40:13 -07002149// Returns true if it added instructions to 'cUnit' to divide 'rlSrc' by 'lit'
2150// and store the result in 'rlDest'.
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002151static bool handleEasyDivide(CompilationUnit *cUnit, Opcode dalvikOpcode,
Elliott Hughes672511b2010-04-26 17:40:13 -07002152 RegLocation rlSrc, RegLocation rlDest, int lit)
2153{
2154 if (lit < 2 || !isPowerOfTwo(lit)) {
2155 return false;
2156 }
2157 int k = lowestSetBit(lit);
2158 if (k >= 30) {
2159 // Avoid special cases.
2160 return false;
2161 }
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002162 bool div = (dalvikOpcode == OP_DIV_INT_LIT8 || dalvikOpcode == OP_DIV_INT_LIT16);
Elliott Hughes672511b2010-04-26 17:40:13 -07002163 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
2164 RegLocation rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Elliott Hughes9c457022010-04-28 16:15:38 -07002165 if (div) {
2166 int tReg = dvmCompilerAllocTemp(cUnit);
2167 if (lit == 2) {
2168 // Division by 2 is by far the most common division by constant.
2169 opRegRegImm(cUnit, kOpLsr, tReg, rlSrc.lowReg, 32 - k);
2170 opRegRegReg(cUnit, kOpAdd, tReg, tReg, rlSrc.lowReg);
2171 opRegRegImm(cUnit, kOpAsr, rlResult.lowReg, tReg, k);
2172 } else {
2173 opRegRegImm(cUnit, kOpAsr, tReg, rlSrc.lowReg, 31);
2174 opRegRegImm(cUnit, kOpLsr, tReg, tReg, 32 - k);
2175 opRegRegReg(cUnit, kOpAdd, tReg, tReg, rlSrc.lowReg);
2176 opRegRegImm(cUnit, kOpAsr, rlResult.lowReg, tReg, k);
2177 }
Elliott Hughes672511b2010-04-26 17:40:13 -07002178 } else {
Elliott Hughes9c457022010-04-28 16:15:38 -07002179 int cReg = dvmCompilerAllocTemp(cUnit);
2180 loadConstant(cUnit, cReg, lit - 1);
2181 int tReg1 = dvmCompilerAllocTemp(cUnit);
2182 int tReg2 = dvmCompilerAllocTemp(cUnit);
2183 if (lit == 2) {
2184 opRegRegImm(cUnit, kOpLsr, tReg1, rlSrc.lowReg, 32 - k);
2185 opRegRegReg(cUnit, kOpAdd, tReg2, tReg1, rlSrc.lowReg);
2186 opRegRegReg(cUnit, kOpAnd, tReg2, tReg2, cReg);
2187 opRegRegReg(cUnit, kOpSub, rlResult.lowReg, tReg2, tReg1);
2188 } else {
2189 opRegRegImm(cUnit, kOpAsr, tReg1, rlSrc.lowReg, 31);
2190 opRegRegImm(cUnit, kOpLsr, tReg1, tReg1, 32 - k);
2191 opRegRegReg(cUnit, kOpAdd, tReg2, tReg1, rlSrc.lowReg);
2192 opRegRegReg(cUnit, kOpAnd, tReg2, tReg2, cReg);
2193 opRegRegReg(cUnit, kOpSub, rlResult.lowReg, tReg2, tReg1);
2194 }
Elliott Hughes672511b2010-04-26 17:40:13 -07002195 }
2196 storeValue(cUnit, rlDest, rlResult);
2197 return true;
2198}
2199
Elliott Hughesb4c05972010-02-24 16:36:18 -08002200// Returns true if it added instructions to 'cUnit' to multiply 'rlSrc' by 'lit'
2201// and store the result in 'rlDest'.
2202static bool handleEasyMultiply(CompilationUnit *cUnit,
2203 RegLocation rlSrc, RegLocation rlDest, int lit)
2204{
2205 // Can we simplify this multiplication?
2206 bool powerOfTwo = false;
2207 bool popCountLE2 = false;
2208 bool powerOfTwoMinusOne = false;
2209 if (lit < 2) {
2210 // Avoid special cases.
2211 return false;
2212 } else if (isPowerOfTwo(lit)) {
2213 powerOfTwo = true;
2214 } else if (isPopCountLE2(lit)) {
2215 popCountLE2 = true;
2216 } else if (isPowerOfTwo(lit + 1)) {
2217 powerOfTwoMinusOne = true;
2218 } else {
2219 return false;
2220 }
2221 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
2222 RegLocation rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
2223 if (powerOfTwo) {
2224 // Shift.
2225 opRegRegImm(cUnit, kOpLsl, rlResult.lowReg, rlSrc.lowReg,
2226 lowestSetBit(lit));
2227 } else if (popCountLE2) {
2228 // Shift and add and shift.
2229 int firstBit = lowestSetBit(lit);
2230 int secondBit = lowestSetBit(lit ^ (1 << firstBit));
2231 genMultiplyByTwoBitMultiplier(cUnit, rlSrc, rlResult, lit,
2232 firstBit, secondBit);
2233 } else {
2234 // Reverse subtract: (src << (shift + 1)) - src.
2235 assert(powerOfTwoMinusOne);
2236 // TODO: rsb dst, src, src lsl#lowestSetBit(lit + 1)
2237 int tReg = dvmCompilerAllocTemp(cUnit);
2238 opRegRegImm(cUnit, kOpLsl, tReg, rlSrc.lowReg, lowestSetBit(lit + 1));
2239 opRegRegReg(cUnit, kOpSub, rlResult.lowReg, tReg, rlSrc.lowReg);
2240 }
2241 storeValue(cUnit, rlDest, rlResult);
2242 return true;
Bill Buzbee78cb0e22010-02-11 14:04:53 -08002243}
2244
Ben Chengba4fc8b2009-06-01 13:00:29 -07002245static bool handleFmt22b_Fmt22s(CompilationUnit *cUnit, MIR *mir)
2246{
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002247 Opcode dalvikOpcode = mir->dalvikInsn.opcode;
Bill Buzbeec6f10662010-02-09 11:16:15 -08002248 RegLocation rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
2249 RegLocation rlDest = dvmCompilerGetDest(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07002250 RegLocation rlResult;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002251 int lit = mir->dalvikInsn.vC;
Ben Cheng4f489172009-09-27 17:08:35 -07002252 OpKind op = 0; /* Make gcc happy */
Bill Buzbee1465db52009-09-23 17:17:35 -07002253 int shiftOp = false;
2254 bool isDiv = false;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002255
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002256 switch (dalvikOpcode) {
Bill Buzbee1465db52009-09-23 17:17:35 -07002257 case OP_RSUB_INT_LIT8:
2258 case OP_RSUB_INT: {
2259 int tReg;
2260 //TUNING: add support for use of Arm rsub op
2261 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
Bill Buzbeec6f10662010-02-09 11:16:15 -08002262 tReg = dvmCompilerAllocTemp(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07002263 loadConstant(cUnit, tReg, lit);
Bill Buzbeec6f10662010-02-09 11:16:15 -08002264 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Bill Buzbee1465db52009-09-23 17:17:35 -07002265 opRegRegReg(cUnit, kOpSub, rlResult.lowReg,
2266 tReg, rlSrc.lowReg);
2267 storeValue(cUnit, rlDest, rlResult);
2268 return false;
2269 break;
2270 }
2271
Ben Chengba4fc8b2009-06-01 13:00:29 -07002272 case OP_ADD_INT_LIT8:
2273 case OP_ADD_INT_LIT16:
Bill Buzbee1465db52009-09-23 17:17:35 -07002274 op = kOpAdd;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002275 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002276 case OP_MUL_INT_LIT8:
Bill Buzbee78cb0e22010-02-11 14:04:53 -08002277 case OP_MUL_INT_LIT16: {
Elliott Hughesb4c05972010-02-24 16:36:18 -08002278 if (handleEasyMultiply(cUnit, rlSrc, rlDest, lit)) {
2279 return false;
Bill Buzbee78cb0e22010-02-11 14:04:53 -08002280 }
Elliott Hughesb4c05972010-02-24 16:36:18 -08002281 op = kOpMul;
Bill Buzbee1465db52009-09-23 17:17:35 -07002282 break;
Bill Buzbee78cb0e22010-02-11 14:04:53 -08002283 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07002284 case OP_AND_INT_LIT8:
2285 case OP_AND_INT_LIT16:
Bill Buzbee1465db52009-09-23 17:17:35 -07002286 op = kOpAnd;
2287 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002288 case OP_OR_INT_LIT8:
2289 case OP_OR_INT_LIT16:
Bill Buzbee1465db52009-09-23 17:17:35 -07002290 op = kOpOr;
2291 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002292 case OP_XOR_INT_LIT8:
2293 case OP_XOR_INT_LIT16:
Bill Buzbee1465db52009-09-23 17:17:35 -07002294 op = kOpXor;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002295 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002296 case OP_SHL_INT_LIT8:
Bill Buzbee0e605272009-12-01 14:28:05 -08002297 lit &= 31;
Bill Buzbee1465db52009-09-23 17:17:35 -07002298 shiftOp = true;
2299 op = kOpLsl;
2300 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002301 case OP_SHR_INT_LIT8:
Bill Buzbee0e605272009-12-01 14:28:05 -08002302 lit &= 31;
Bill Buzbee1465db52009-09-23 17:17:35 -07002303 shiftOp = true;
2304 op = kOpAsr;
2305 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002306 case OP_USHR_INT_LIT8:
Bill Buzbee0e605272009-12-01 14:28:05 -08002307 lit &= 31;
Bill Buzbee1465db52009-09-23 17:17:35 -07002308 shiftOp = true;
2309 op = kOpLsr;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002310 break;
2311
2312 case OP_DIV_INT_LIT8:
2313 case OP_DIV_INT_LIT16:
Ben Chengba4fc8b2009-06-01 13:00:29 -07002314 case OP_REM_INT_LIT8:
2315 case OP_REM_INT_LIT16:
2316 if (lit == 0) {
2317 /* Let the interpreter deal with div by 0 */
2318 genInterpSingleStep(cUnit, mir);
2319 return false;
2320 }
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002321 if (handleEasyDivide(cUnit, dalvikOpcode, rlSrc, rlDest, lit)) {
Elliott Hughes672511b2010-04-26 17:40:13 -07002322 return false;
2323 }
Bill Buzbeec6f10662010-02-09 11:16:15 -08002324 dvmCompilerFlushAllRegs(cUnit); /* Everything to home location */
Bill Buzbee1465db52009-09-23 17:17:35 -07002325 loadValueDirectFixed(cUnit, rlSrc, r0);
Bill Buzbeec6f10662010-02-09 11:16:15 -08002326 dvmCompilerClobber(cUnit, r0);
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002327 if ((dalvikOpcode == OP_DIV_INT_LIT8) ||
2328 (dalvikOpcode == OP_DIV_INT_LIT16)) {
Ben Chengbd1326d2010-04-02 15:04:53 -07002329 LOAD_FUNC_ADDR(cUnit, r2, (int)__aeabi_idiv);
Bill Buzbee1465db52009-09-23 17:17:35 -07002330 isDiv = true;
2331 } else {
Ben Chengbd1326d2010-04-02 15:04:53 -07002332 LOAD_FUNC_ADDR(cUnit, r2, (int)__aeabi_idivmod);
Bill Buzbee1465db52009-09-23 17:17:35 -07002333 isDiv = false;
2334 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07002335 loadConstant(cUnit, r1, lit);
Bill Buzbee1465db52009-09-23 17:17:35 -07002336 opReg(cUnit, kOpBlx, r2);
Elliott Hughes6a555132010-02-25 15:41:42 -08002337 dvmCompilerClobberCallRegs(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07002338 if (isDiv)
Bill Buzbeec6f10662010-02-09 11:16:15 -08002339 rlResult = dvmCompilerGetReturn(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07002340 else
Bill Buzbeec6f10662010-02-09 11:16:15 -08002341 rlResult = dvmCompilerGetReturnAlt(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07002342 storeValue(cUnit, rlDest, rlResult);
2343 return false;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002344 break;
2345 default:
2346 return true;
2347 }
Bill Buzbee1465db52009-09-23 17:17:35 -07002348 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
Bill Buzbeec6f10662010-02-09 11:16:15 -08002349 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Bill Buzbee1465db52009-09-23 17:17:35 -07002350 // Avoid shifts by literal 0 - no support in Thumb. Change to copy
2351 if (shiftOp && (lit == 0)) {
2352 genRegCopy(cUnit, rlResult.lowReg, rlSrc.lowReg);
2353 } else {
2354 opRegRegImm(cUnit, op, rlResult.lowReg, rlSrc.lowReg, lit);
2355 }
2356 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002357 return false;
2358}
2359
jeffhao71eee1f2011-01-04 14:18:54 -08002360static bool handleFmt22c_Fmt52c(CompilationUnit *cUnit, MIR *mir)
Ben Chengba4fc8b2009-06-01 13:00:29 -07002361{
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002362 Opcode dalvikOpcode = mir->dalvikInsn.opcode;
buzbee4d92e682010-07-29 15:24:14 -07002363 int fieldOffset = -1;
buzbeeecf8f6e2010-07-20 14:53:42 -07002364 bool isVolatile = false;
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002365 switch (dalvikOpcode) {
buzbee4d92e682010-07-29 15:24:14 -07002366 /*
2367 * Wide volatiles currently handled via single step.
2368 * Add them here if generating in-line code.
2369 * case OP_IGET_WIDE_VOLATILE:
2370 * case OP_IPUT_WIDE_VOLATILE:
2371 */
2372 case OP_IGET:
2373 case OP_IGET_VOLATILE:
jeffhao71eee1f2011-01-04 14:18:54 -08002374 case OP_IGET_JUMBO:
buzbee4d92e682010-07-29 15:24:14 -07002375 case OP_IGET_WIDE:
jeffhao71eee1f2011-01-04 14:18:54 -08002376 case OP_IGET_WIDE_JUMBO:
buzbee4d92e682010-07-29 15:24:14 -07002377 case OP_IGET_OBJECT:
2378 case OP_IGET_OBJECT_VOLATILE:
jeffhao71eee1f2011-01-04 14:18:54 -08002379 case OP_IGET_OBJECT_JUMBO:
buzbee4d92e682010-07-29 15:24:14 -07002380 case OP_IGET_BOOLEAN:
jeffhao71eee1f2011-01-04 14:18:54 -08002381 case OP_IGET_BOOLEAN_JUMBO:
buzbee4d92e682010-07-29 15:24:14 -07002382 case OP_IGET_BYTE:
jeffhao71eee1f2011-01-04 14:18:54 -08002383 case OP_IGET_BYTE_JUMBO:
buzbee4d92e682010-07-29 15:24:14 -07002384 case OP_IGET_CHAR:
jeffhao71eee1f2011-01-04 14:18:54 -08002385 case OP_IGET_CHAR_JUMBO:
buzbee4d92e682010-07-29 15:24:14 -07002386 case OP_IGET_SHORT:
jeffhao71eee1f2011-01-04 14:18:54 -08002387 case OP_IGET_SHORT_JUMBO:
buzbee4d92e682010-07-29 15:24:14 -07002388 case OP_IPUT:
2389 case OP_IPUT_VOLATILE:
jeffhao71eee1f2011-01-04 14:18:54 -08002390 case OP_IPUT_JUMBO:
buzbee4d92e682010-07-29 15:24:14 -07002391 case OP_IPUT_WIDE:
jeffhao71eee1f2011-01-04 14:18:54 -08002392 case OP_IPUT_WIDE_JUMBO:
buzbee4d92e682010-07-29 15:24:14 -07002393 case OP_IPUT_OBJECT:
2394 case OP_IPUT_OBJECT_VOLATILE:
jeffhao71eee1f2011-01-04 14:18:54 -08002395 case OP_IPUT_OBJECT_JUMBO:
buzbee4d92e682010-07-29 15:24:14 -07002396 case OP_IPUT_BOOLEAN:
jeffhao71eee1f2011-01-04 14:18:54 -08002397 case OP_IPUT_BOOLEAN_JUMBO:
buzbee4d92e682010-07-29 15:24:14 -07002398 case OP_IPUT_BYTE:
jeffhao71eee1f2011-01-04 14:18:54 -08002399 case OP_IPUT_BYTE_JUMBO:
buzbee4d92e682010-07-29 15:24:14 -07002400 case OP_IPUT_CHAR:
jeffhao71eee1f2011-01-04 14:18:54 -08002401 case OP_IPUT_CHAR_JUMBO:
2402 case OP_IPUT_SHORT:
2403 case OP_IPUT_SHORT_JUMBO: {
Ben Cheng7a2697d2010-06-07 13:44:23 -07002404 const Method *method = (mir->OptimizationFlags & MIR_CALLEE) ?
2405 mir->meta.calleeMethod : cUnit->method;
buzbee4d92e682010-07-29 15:24:14 -07002406 Field *fieldPtr =
Ben Cheng7a2697d2010-06-07 13:44:23 -07002407 method->clazz->pDvmDex->pResFields[mir->dalvikInsn.vC];
Ben Chengba4fc8b2009-06-01 13:00:29 -07002408
buzbee4d92e682010-07-29 15:24:14 -07002409 if (fieldPtr == NULL) {
2410 LOGE("Unexpected null instance field");
2411 dvmAbort();
2412 }
2413 isVolatile = dvmIsVolatileField(fieldPtr);
2414 fieldOffset = ((InstField *)fieldPtr)->byteOffset;
2415 break;
Ben Chengdd6e8702010-05-07 13:05:47 -07002416 }
buzbee4d92e682010-07-29 15:24:14 -07002417 default:
2418 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002419 }
buzbee4d92e682010-07-29 15:24:14 -07002420
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002421 switch (dalvikOpcode) {
jeffhao71eee1f2011-01-04 14:18:54 -08002422 case OP_NEW_ARRAY:
2423 case OP_NEW_ARRAY_JUMBO: {
Bill Buzbee1465db52009-09-23 17:17:35 -07002424 // Generates a call - use explicit registers
Bill Buzbeec6f10662010-02-09 11:16:15 -08002425 RegLocation rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
2426 RegLocation rlDest = dvmCompilerGetDest(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07002427 RegLocation rlResult;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002428 void *classPtr = (void*)
2429 (cUnit->method->clazz->pDvmDex->pResClasses[mir->dalvikInsn.vC]);
Ben Chengdd6e8702010-05-07 13:05:47 -07002430
2431 if (classPtr == NULL) {
2432 LOGE("Unexpected null class");
2433 dvmAbort();
2434 }
2435
Bill Buzbeec6f10662010-02-09 11:16:15 -08002436 dvmCompilerFlushAllRegs(cUnit); /* Everything to home location */
Bill Buzbee1465db52009-09-23 17:17:35 -07002437 genExportPC(cUnit, mir);
2438 loadValueDirectFixed(cUnit, rlSrc, r1); /* Len */
Ben Chengba4fc8b2009-06-01 13:00:29 -07002439 loadConstant(cUnit, r0, (int) classPtr );
Ben Chengbd1326d2010-04-02 15:04:53 -07002440 LOAD_FUNC_ADDR(cUnit, r3, (int)dvmAllocArrayByClass);
Ben Cheng4f489172009-09-27 17:08:35 -07002441 /*
2442 * "len < 0": bail to the interpreter to re-execute the
2443 * instruction
2444 */
Carl Shapiroe3c01da2010-05-20 22:54:18 -07002445 genRegImmCheck(cUnit, kArmCondMi, r1, 0, mir->offset, NULL);
Bill Buzbee270c1d62009-08-13 16:58:07 -07002446 loadConstant(cUnit, r2, ALLOC_DONT_TRACK);
Bill Buzbee1465db52009-09-23 17:17:35 -07002447 opReg(cUnit, kOpBlx, r3);
Elliott Hughes6a555132010-02-25 15:41:42 -08002448 dvmCompilerClobberCallRegs(cUnit);
Ben Cheng4f489172009-09-27 17:08:35 -07002449 /* generate a branch over if allocation is successful */
buzbee8f8109a2010-08-31 10:16:35 -07002450 ArmLIR *branchOver = genCmpImmBranch(cUnit, kArmCondNe, r0, 0);
Ben Cheng4f489172009-09-27 17:08:35 -07002451 /*
2452 * OOM exception needs to be thrown here and cannot re-execute
2453 */
2454 loadConstant(cUnit, r0,
2455 (int) (cUnit->method->insns + mir->offset));
2456 genDispatchToHandler(cUnit, TEMPLATE_THROW_EXCEPTION_COMMON);
2457 /* noreturn */
2458
Bill Buzbee1465db52009-09-23 17:17:35 -07002459 ArmLIR *target = newLIR0(cUnit, kArmPseudoTargetLabel);
Ben Cheng4f489172009-09-27 17:08:35 -07002460 target->defMask = ENCODE_ALL;
2461 branchOver->generic.target = (LIR *) target;
Bill Buzbeec6f10662010-02-09 11:16:15 -08002462 rlResult = dvmCompilerGetReturn(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07002463 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002464 break;
2465 }
jeffhao71eee1f2011-01-04 14:18:54 -08002466 case OP_INSTANCE_OF:
2467 case OP_INSTANCE_OF_JUMBO: {
Bill Buzbee1465db52009-09-23 17:17:35 -07002468 // May generate a call - use explicit registers
Bill Buzbeec6f10662010-02-09 11:16:15 -08002469 RegLocation rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
2470 RegLocation rlDest = dvmCompilerGetDest(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07002471 RegLocation rlResult;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002472 ClassObject *classPtr =
2473 (cUnit->method->clazz->pDvmDex->pResClasses[mir->dalvikInsn.vC]);
Bill Buzbee480e6782010-01-27 15:43:08 -08002474 /*
2475 * Note: It is possible that classPtr is NULL at this point,
2476 * even though this instruction has been successfully interpreted.
2477 * If the previous interpretation had a null source, the
2478 * interpreter would not have bothered to resolve the clazz.
2479 * Bail out to the interpreter in this case, and log it
2480 * so that we can tell if it happens frequently.
2481 */
2482 if (classPtr == NULL) {
2483 LOGD("null clazz in OP_INSTANCE_OF, single-stepping");
2484 genInterpSingleStep(cUnit, mir);
2485 break;
2486 }
Bill Buzbeec6f10662010-02-09 11:16:15 -08002487 dvmCompilerFlushAllRegs(cUnit); /* Everything to home location */
Bill Buzbee1465db52009-09-23 17:17:35 -07002488 loadValueDirectFixed(cUnit, rlSrc, r0); /* Ref */
Ben Chengba4fc8b2009-06-01 13:00:29 -07002489 loadConstant(cUnit, r2, (int) classPtr );
Ben Cheng752c7942009-06-22 10:50:07 -07002490 /* When taken r0 has NULL which can be used for store directly */
buzbee8f8109a2010-08-31 10:16:35 -07002491 ArmLIR *branch1 = genCmpImmBranch(cUnit, kArmCondEq, r0, 0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002492 /* r1 now contains object->clazz */
Bill Buzbee270c1d62009-08-13 16:58:07 -07002493 loadWordDisp(cUnit, r0, offsetof(Object, clazz), r1);
Bill Buzbee1465db52009-09-23 17:17:35 -07002494 /* r1 now contains object->clazz */
Ben Chengbd1326d2010-04-02 15:04:53 -07002495 LOAD_FUNC_ADDR(cUnit, r3, (int)dvmInstanceofNonTrivial);
Ben Cheng752c7942009-06-22 10:50:07 -07002496 loadConstant(cUnit, r0, 1); /* Assume true */
Bill Buzbee1465db52009-09-23 17:17:35 -07002497 opRegReg(cUnit, kOpCmp, r1, r2);
2498 ArmLIR *branch2 = opCondBranch(cUnit, kArmCondEq);
2499 genRegCopy(cUnit, r0, r1);
2500 genRegCopy(cUnit, r1, r2);
2501 opReg(cUnit, kOpBlx, r3);
Elliott Hughes6a555132010-02-25 15:41:42 -08002502 dvmCompilerClobberCallRegs(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002503 /* branch target here */
Bill Buzbee1465db52009-09-23 17:17:35 -07002504 ArmLIR *target = newLIR0(cUnit, kArmPseudoTargetLabel);
Ben Chengd7d426a2009-09-22 11:23:36 -07002505 target->defMask = ENCODE_ALL;
Bill Buzbeec6f10662010-02-09 11:16:15 -08002506 rlResult = dvmCompilerGetReturn(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07002507 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002508 branch1->generic.target = (LIR *)target;
2509 branch2->generic.target = (LIR *)target;
2510 break;
2511 }
2512 case OP_IGET_WIDE:
jeffhao71eee1f2011-01-04 14:18:54 -08002513 case OP_IGET_WIDE_JUMBO:
Ben Chengba4fc8b2009-06-01 13:00:29 -07002514 genIGetWide(cUnit, mir, fieldOffset);
2515 break;
buzbeeecf8f6e2010-07-20 14:53:42 -07002516 case OP_IGET_VOLATILE:
2517 case OP_IGET_OBJECT_VOLATILE:
2518 isVolatile = true;
2519 // NOTE: intentional fallthrough
Ben Chengba4fc8b2009-06-01 13:00:29 -07002520 case OP_IGET:
jeffhao71eee1f2011-01-04 14:18:54 -08002521 case OP_IGET_JUMBO:
Ben Chengba4fc8b2009-06-01 13:00:29 -07002522 case OP_IGET_OBJECT:
jeffhao71eee1f2011-01-04 14:18:54 -08002523 case OP_IGET_OBJECT_JUMBO:
Ben Chengba4fc8b2009-06-01 13:00:29 -07002524 case OP_IGET_BOOLEAN:
jeffhao71eee1f2011-01-04 14:18:54 -08002525 case OP_IGET_BOOLEAN_JUMBO:
Ben Chengba4fc8b2009-06-01 13:00:29 -07002526 case OP_IGET_BYTE:
jeffhao71eee1f2011-01-04 14:18:54 -08002527 case OP_IGET_BYTE_JUMBO:
Ben Chengba4fc8b2009-06-01 13:00:29 -07002528 case OP_IGET_CHAR:
jeffhao71eee1f2011-01-04 14:18:54 -08002529 case OP_IGET_CHAR_JUMBO:
Ben Chengba4fc8b2009-06-01 13:00:29 -07002530 case OP_IGET_SHORT:
jeffhao71eee1f2011-01-04 14:18:54 -08002531 case OP_IGET_SHORT_JUMBO:
buzbee3272e2f2010-09-09 14:07:01 -07002532 genIGet(cUnit, mir, kWord, fieldOffset, isVolatile);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002533 break;
2534 case OP_IPUT_WIDE:
jeffhao71eee1f2011-01-04 14:18:54 -08002535 case OP_IPUT_WIDE_JUMBO:
Ben Chengba4fc8b2009-06-01 13:00:29 -07002536 genIPutWide(cUnit, mir, fieldOffset);
2537 break;
2538 case OP_IPUT:
jeffhao71eee1f2011-01-04 14:18:54 -08002539 case OP_IPUT_JUMBO:
buzbee3272e2f2010-09-09 14:07:01 -07002540 case OP_IPUT_BOOLEAN:
jeffhao71eee1f2011-01-04 14:18:54 -08002541 case OP_IPUT_BOOLEAN_JUMBO:
2542 case OP_IPUT_BYTE:
2543 case OP_IPUT_BYTE_JUMBO:
2544 case OP_IPUT_CHAR:
2545 case OP_IPUT_CHAR_JUMBO:
2546 case OP_IPUT_SHORT:
2547 case OP_IPUT_SHORT_JUMBO:
buzbeeecf8f6e2010-07-20 14:53:42 -07002548 genIPut(cUnit, mir, kWord, fieldOffset, false, isVolatile);
buzbee919eb062010-07-12 12:59:22 -07002549 break;
buzbee4d92e682010-07-29 15:24:14 -07002550 case OP_IPUT_VOLATILE:
buzbeeecf8f6e2010-07-20 14:53:42 -07002551 case OP_IPUT_OBJECT_VOLATILE:
2552 isVolatile = true;
2553 // NOTE: intentional fallthrough
Ben Chengba4fc8b2009-06-01 13:00:29 -07002554 case OP_IPUT_OBJECT:
jeffhao71eee1f2011-01-04 14:18:54 -08002555 case OP_IPUT_OBJECT_JUMBO:
buzbeeecf8f6e2010-07-20 14:53:42 -07002556 genIPut(cUnit, mir, kWord, fieldOffset, true, isVolatile);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002557 break;
Bill Buzbeeb16344a2010-03-15 17:19:12 -07002558 case OP_IGET_WIDE_VOLATILE:
2559 case OP_IPUT_WIDE_VOLATILE:
Bill Buzbeeb16344a2010-03-15 17:19:12 -07002560 genInterpSingleStep(cUnit, mir);
2561 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002562 default:
2563 return true;
2564 }
2565 return false;
2566}
2567
2568static bool handleFmt22cs(CompilationUnit *cUnit, MIR *mir)
2569{
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002570 Opcode dalvikOpcode = mir->dalvikInsn.opcode;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002571 int fieldOffset = mir->dalvikInsn.vC;
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002572 switch (dalvikOpcode) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07002573 case OP_IGET_QUICK:
2574 case OP_IGET_OBJECT_QUICK:
buzbeeecf8f6e2010-07-20 14:53:42 -07002575 genIGet(cUnit, mir, kWord, fieldOffset, false);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002576 break;
2577 case OP_IPUT_QUICK:
buzbeeecf8f6e2010-07-20 14:53:42 -07002578 genIPut(cUnit, mir, kWord, fieldOffset, false, false);
buzbee919eb062010-07-12 12:59:22 -07002579 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002580 case OP_IPUT_OBJECT_QUICK:
buzbeeecf8f6e2010-07-20 14:53:42 -07002581 genIPut(cUnit, mir, kWord, fieldOffset, true, false);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002582 break;
2583 case OP_IGET_WIDE_QUICK:
2584 genIGetWide(cUnit, mir, fieldOffset);
2585 break;
2586 case OP_IPUT_WIDE_QUICK:
2587 genIPutWide(cUnit, mir, fieldOffset);
2588 break;
2589 default:
2590 return true;
2591 }
2592 return false;
2593
2594}
2595
2596/* Compare agaist zero */
2597static bool handleFmt22t(CompilationUnit *cUnit, MIR *mir, BasicBlock *bb,
Bill Buzbee89efc3d2009-07-28 11:22:22 -07002598 ArmLIR *labelList)
Ben Chengba4fc8b2009-06-01 13:00:29 -07002599{
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002600 Opcode dalvikOpcode = mir->dalvikInsn.opcode;
Bill Buzbee89efc3d2009-07-28 11:22:22 -07002601 ArmConditionCode cond;
Ben Cheng7ab74e12011-02-03 14:02:06 -08002602 /* backward branch? */
2603 bool backwardBranch = (bb->taken->startOffset <= mir->offset);
2604
2605 if (backwardBranch && gDvmJit.genSuspendPoll) {
2606 genSuspendPoll(cUnit, mir);
2607 }
2608
Bill Buzbeec6f10662010-02-09 11:16:15 -08002609 RegLocation rlSrc1 = dvmCompilerGetSrc(cUnit, mir, 0);
2610 RegLocation rlSrc2 = dvmCompilerGetSrc(cUnit, mir, 1);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002611
Bill Buzbee1465db52009-09-23 17:17:35 -07002612 rlSrc1 = loadValue(cUnit, rlSrc1, kCoreReg);
2613 rlSrc2 = loadValue(cUnit, rlSrc2, kCoreReg);
Ben Cheng7ab74e12011-02-03 14:02:06 -08002614
Bill Buzbee1465db52009-09-23 17:17:35 -07002615 opRegReg(cUnit, kOpCmp, rlSrc1.lowReg, rlSrc2.lowReg);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002616
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002617 switch (dalvikOpcode) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07002618 case OP_IF_EQ:
Bill Buzbee1465db52009-09-23 17:17:35 -07002619 cond = kArmCondEq;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002620 break;
2621 case OP_IF_NE:
Bill Buzbee1465db52009-09-23 17:17:35 -07002622 cond = kArmCondNe;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002623 break;
2624 case OP_IF_LT:
Bill Buzbee1465db52009-09-23 17:17:35 -07002625 cond = kArmCondLt;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002626 break;
2627 case OP_IF_GE:
Bill Buzbee1465db52009-09-23 17:17:35 -07002628 cond = kArmCondGe;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002629 break;
2630 case OP_IF_GT:
Bill Buzbee1465db52009-09-23 17:17:35 -07002631 cond = kArmCondGt;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002632 break;
2633 case OP_IF_LE:
Bill Buzbee1465db52009-09-23 17:17:35 -07002634 cond = kArmCondLe;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002635 break;
2636 default:
2637 cond = 0;
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002638 LOGE("Unexpected opcode (%d) for Fmt22t\n", dalvikOpcode);
Bill Buzbeefc519dc2010-03-06 23:30:57 -08002639 dvmCompilerAbort(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002640 }
2641 genConditionalBranch(cUnit, cond, &labelList[bb->taken->id]);
2642 /* This mostly likely will be optimized away in a later phase */
2643 genUnconditionalBranch(cUnit, &labelList[bb->fallThrough->id]);
2644 return false;
2645}
2646
2647static bool handleFmt22x_Fmt32x(CompilationUnit *cUnit, MIR *mir)
2648{
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002649 Opcode opcode = mir->dalvikInsn.opcode;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002650
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002651 switch (opcode) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07002652 case OP_MOVE_16:
2653 case OP_MOVE_OBJECT_16:
2654 case OP_MOVE_FROM16:
Ben Chenge9695e52009-06-16 16:11:47 -07002655 case OP_MOVE_OBJECT_FROM16: {
Bill Buzbeec6f10662010-02-09 11:16:15 -08002656 storeValue(cUnit, dvmCompilerGetDest(cUnit, mir, 0),
2657 dvmCompilerGetSrc(cUnit, mir, 0));
Ben Chengba4fc8b2009-06-01 13:00:29 -07002658 break;
Ben Chenge9695e52009-06-16 16:11:47 -07002659 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07002660 case OP_MOVE_WIDE_16:
Ben Chenge9695e52009-06-16 16:11:47 -07002661 case OP_MOVE_WIDE_FROM16: {
Bill Buzbeec6f10662010-02-09 11:16:15 -08002662 storeValueWide(cUnit, dvmCompilerGetDestWide(cUnit, mir, 0, 1),
2663 dvmCompilerGetSrcWide(cUnit, mir, 0, 1));
Ben Chengba4fc8b2009-06-01 13:00:29 -07002664 break;
Ben Chenge9695e52009-06-16 16:11:47 -07002665 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07002666 default:
2667 return true;
2668 }
2669 return false;
2670}
2671
2672static bool handleFmt23x(CompilationUnit *cUnit, MIR *mir)
2673{
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002674 Opcode opcode = mir->dalvikInsn.opcode;
Bill Buzbee1465db52009-09-23 17:17:35 -07002675 RegLocation rlSrc1;
2676 RegLocation rlSrc2;
2677 RegLocation rlDest;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002678
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002679 if ( (opcode >= OP_ADD_INT) && (opcode <= OP_REM_DOUBLE)) {
Ben Cheng5d90c202009-11-22 23:31:11 -08002680 return genArithOp( cUnit, mir );
Ben Chengba4fc8b2009-06-01 13:00:29 -07002681 }
2682
Bill Buzbee1465db52009-09-23 17:17:35 -07002683 /* APUTs have 3 sources and no targets */
2684 if (mir->ssaRep->numDefs == 0) {
2685 if (mir->ssaRep->numUses == 3) {
Bill Buzbeec6f10662010-02-09 11:16:15 -08002686 rlDest = dvmCompilerGetSrc(cUnit, mir, 0);
2687 rlSrc1 = dvmCompilerGetSrc(cUnit, mir, 1);
2688 rlSrc2 = dvmCompilerGetSrc(cUnit, mir, 2);
Bill Buzbee1465db52009-09-23 17:17:35 -07002689 } else {
2690 assert(mir->ssaRep->numUses == 4);
Bill Buzbeec6f10662010-02-09 11:16:15 -08002691 rlDest = dvmCompilerGetSrcWide(cUnit, mir, 0, 1);
2692 rlSrc1 = dvmCompilerGetSrc(cUnit, mir, 2);
2693 rlSrc2 = dvmCompilerGetSrc(cUnit, mir, 3);
Bill Buzbee1465db52009-09-23 17:17:35 -07002694 }
2695 } else {
2696 /* Two sources and 1 dest. Deduce the operand sizes */
2697 if (mir->ssaRep->numUses == 4) {
Bill Buzbeec6f10662010-02-09 11:16:15 -08002698 rlSrc1 = dvmCompilerGetSrcWide(cUnit, mir, 0, 1);
2699 rlSrc2 = dvmCompilerGetSrcWide(cUnit, mir, 2, 3);
Bill Buzbee1465db52009-09-23 17:17:35 -07002700 } else {
2701 assert(mir->ssaRep->numUses == 2);
Bill Buzbeec6f10662010-02-09 11:16:15 -08002702 rlSrc1 = dvmCompilerGetSrc(cUnit, mir, 0);
2703 rlSrc2 = dvmCompilerGetSrc(cUnit, mir, 1);
Bill Buzbee1465db52009-09-23 17:17:35 -07002704 }
2705 if (mir->ssaRep->numDefs == 2) {
Bill Buzbeec6f10662010-02-09 11:16:15 -08002706 rlDest = dvmCompilerGetDestWide(cUnit, mir, 0, 1);
Bill Buzbee1465db52009-09-23 17:17:35 -07002707 } else {
2708 assert(mir->ssaRep->numDefs == 1);
Bill Buzbeec6f10662010-02-09 11:16:15 -08002709 rlDest = dvmCompilerGetDest(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07002710 }
2711 }
2712
2713
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002714 switch (opcode) {
Bill Buzbeed45ba372009-06-15 17:00:57 -07002715 case OP_CMPL_FLOAT:
2716 case OP_CMPG_FLOAT:
2717 case OP_CMPL_DOUBLE:
2718 case OP_CMPG_DOUBLE:
Ben Cheng5d90c202009-11-22 23:31:11 -08002719 return genCmpFP(cUnit, mir, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002720 case OP_CMP_LONG:
Bill Buzbee1465db52009-09-23 17:17:35 -07002721 genCmpLong(cUnit, mir, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002722 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002723 case OP_AGET_WIDE:
Bill Buzbee1465db52009-09-23 17:17:35 -07002724 genArrayGet(cUnit, mir, kLong, rlSrc1, rlSrc2, rlDest, 3);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002725 break;
2726 case OP_AGET:
2727 case OP_AGET_OBJECT:
Bill Buzbee1465db52009-09-23 17:17:35 -07002728 genArrayGet(cUnit, mir, kWord, rlSrc1, rlSrc2, rlDest, 2);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002729 break;
2730 case OP_AGET_BOOLEAN:
Bill Buzbee1465db52009-09-23 17:17:35 -07002731 genArrayGet(cUnit, mir, kUnsignedByte, rlSrc1, rlSrc2, rlDest, 0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002732 break;
2733 case OP_AGET_BYTE:
Bill Buzbee1465db52009-09-23 17:17:35 -07002734 genArrayGet(cUnit, mir, kSignedByte, rlSrc1, rlSrc2, rlDest, 0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002735 break;
2736 case OP_AGET_CHAR:
Bill Buzbee1465db52009-09-23 17:17:35 -07002737 genArrayGet(cUnit, mir, kUnsignedHalf, rlSrc1, rlSrc2, rlDest, 1);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002738 break;
2739 case OP_AGET_SHORT:
Bill Buzbee1465db52009-09-23 17:17:35 -07002740 genArrayGet(cUnit, mir, kSignedHalf, rlSrc1, rlSrc2, rlDest, 1);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002741 break;
2742 case OP_APUT_WIDE:
Bill Buzbee1465db52009-09-23 17:17:35 -07002743 genArrayPut(cUnit, mir, kLong, rlSrc1, rlSrc2, rlDest, 3);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002744 break;
2745 case OP_APUT:
Bill Buzbee1465db52009-09-23 17:17:35 -07002746 genArrayPut(cUnit, mir, kWord, rlSrc1, rlSrc2, rlDest, 2);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002747 break;
Bill Buzbeebe6534f2010-03-12 16:01:35 -08002748 case OP_APUT_OBJECT:
2749 genArrayObjectPut(cUnit, mir, rlSrc1, rlSrc2, rlDest, 2);
2750 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002751 case OP_APUT_SHORT:
2752 case OP_APUT_CHAR:
Bill Buzbee1465db52009-09-23 17:17:35 -07002753 genArrayPut(cUnit, mir, kUnsignedHalf, rlSrc1, rlSrc2, rlDest, 1);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002754 break;
2755 case OP_APUT_BYTE:
2756 case OP_APUT_BOOLEAN:
Bill Buzbee1465db52009-09-23 17:17:35 -07002757 genArrayPut(cUnit, mir, kUnsignedByte, rlSrc1, rlSrc2, rlDest, 0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002758 break;
2759 default:
2760 return true;
2761 }
2762 return false;
2763}
2764
Ben Cheng6c10a972009-10-29 14:39:18 -07002765/*
2766 * Find the matching case.
2767 *
2768 * return values:
2769 * r0 (low 32-bit): pc of the chaining cell corresponding to the resolved case,
2770 * including default which is placed at MIN(size, MAX_CHAINED_SWITCH_CASES).
2771 * r1 (high 32-bit): the branch offset of the matching case (only for indexes
2772 * above MAX_CHAINED_SWITCH_CASES).
2773 *
2774 * Instructions around the call are:
2775 *
2776 * mov r2, pc
2777 * blx &findPackedSwitchIndex
2778 * mov pc, r0
2779 * .align4
Bill Buzbeebd047242010-05-13 13:02:53 -07002780 * chaining cell for case 0 [12 bytes]
2781 * chaining cell for case 1 [12 bytes]
Ben Cheng6c10a972009-10-29 14:39:18 -07002782 * :
Bill Buzbeebd047242010-05-13 13:02:53 -07002783 * chaining cell for case MIN(size, MAX_CHAINED_SWITCH_CASES)-1 [12 bytes]
Ben Cheng6c10a972009-10-29 14:39:18 -07002784 * chaining cell for case default [8 bytes]
2785 * noChain exit
2786 */
Ben Chengbd1326d2010-04-02 15:04:53 -07002787static s8 findPackedSwitchIndex(const u2* switchData, int testVal, int pc)
Ben Cheng6c10a972009-10-29 14:39:18 -07002788{
2789 int size;
2790 int firstKey;
2791 const int *entries;
2792 int index;
2793 int jumpIndex;
2794 int caseDPCOffset = 0;
2795 /* In Thumb mode pc is 4 ahead of the "mov r2, pc" instruction */
2796 int chainingPC = (pc + 4) & ~3;
2797
2798 /*
2799 * Packed switch data format:
2800 * ushort ident = 0x0100 magic value
2801 * ushort size number of entries in the table
2802 * int first_key first (and lowest) switch case value
2803 * int targets[size] branch targets, relative to switch opcode
2804 *
2805 * Total size is (4+size*2) 16-bit code units.
2806 */
2807 size = switchData[1];
2808 assert(size > 0);
2809
2810 firstKey = switchData[2];
2811 firstKey |= switchData[3] << 16;
2812
2813
2814 /* The entries are guaranteed to be aligned on a 32-bit boundary;
2815 * we can treat them as a native int array.
2816 */
2817 entries = (const int*) &switchData[4];
2818 assert(((u4)entries & 0x3) == 0);
2819
2820 index = testVal - firstKey;
2821
2822 /* Jump to the default cell */
2823 if (index < 0 || index >= size) {
2824 jumpIndex = MIN(size, MAX_CHAINED_SWITCH_CASES);
2825 /* Jump to the non-chaining exit point */
2826 } else if (index >= MAX_CHAINED_SWITCH_CASES) {
2827 jumpIndex = MAX_CHAINED_SWITCH_CASES + 1;
2828 caseDPCOffset = entries[index];
2829 /* Jump to the inline chaining cell */
2830 } else {
2831 jumpIndex = index;
2832 }
2833
Bill Buzbeebd047242010-05-13 13:02:53 -07002834 chainingPC += jumpIndex * CHAIN_CELL_NORMAL_SIZE;
Ben Cheng6c10a972009-10-29 14:39:18 -07002835 return (((s8) caseDPCOffset) << 32) | (u8) chainingPC;
2836}
2837
2838/* See comments for findPackedSwitchIndex */
Ben Chengbd1326d2010-04-02 15:04:53 -07002839static s8 findSparseSwitchIndex(const u2* switchData, int testVal, int pc)
Ben Cheng6c10a972009-10-29 14:39:18 -07002840{
2841 int size;
2842 const int *keys;
2843 const int *entries;
2844 int chainingPC = (pc + 4) & ~3;
2845 int i;
2846
2847 /*
2848 * Sparse switch data format:
2849 * ushort ident = 0x0200 magic value
2850 * ushort size number of entries in the table; > 0
2851 * int keys[size] keys, sorted low-to-high; 32-bit aligned
2852 * int targets[size] branch targets, relative to switch opcode
2853 *
2854 * Total size is (2+size*4) 16-bit code units.
2855 */
2856
2857 size = switchData[1];
2858 assert(size > 0);
2859
2860 /* The keys are guaranteed to be aligned on a 32-bit boundary;
2861 * we can treat them as a native int array.
2862 */
2863 keys = (const int*) &switchData[2];
2864 assert(((u4)keys & 0x3) == 0);
2865
2866 /* The entries are guaranteed to be aligned on a 32-bit boundary;
2867 * we can treat them as a native int array.
2868 */
2869 entries = keys + size;
2870 assert(((u4)entries & 0x3) == 0);
2871
2872 /*
2873 * Run through the list of keys, which are guaranteed to
2874 * be sorted low-to-high.
2875 *
2876 * Most tables have 3-4 entries. Few have more than 10. A binary
2877 * search here is probably not useful.
2878 */
2879 for (i = 0; i < size; i++) {
2880 int k = keys[i];
2881 if (k == testVal) {
2882 /* MAX_CHAINED_SWITCH_CASES + 1 is the start of the overflow case */
2883 int jumpIndex = (i < MAX_CHAINED_SWITCH_CASES) ?
2884 i : MAX_CHAINED_SWITCH_CASES + 1;
Bill Buzbeebd047242010-05-13 13:02:53 -07002885 chainingPC += jumpIndex * CHAIN_CELL_NORMAL_SIZE;
Ben Cheng6c10a972009-10-29 14:39:18 -07002886 return (((s8) entries[i]) << 32) | (u8) chainingPC;
2887 } else if (k > testVal) {
2888 break;
2889 }
2890 }
Bill Buzbeebd047242010-05-13 13:02:53 -07002891 return chainingPC + MIN(size, MAX_CHAINED_SWITCH_CASES) *
2892 CHAIN_CELL_NORMAL_SIZE;
Ben Cheng6c10a972009-10-29 14:39:18 -07002893}
2894
Ben Chengba4fc8b2009-06-01 13:00:29 -07002895static bool handleFmt31t(CompilationUnit *cUnit, MIR *mir)
2896{
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002897 Opcode dalvikOpcode = mir->dalvikInsn.opcode;
2898 switch (dalvikOpcode) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07002899 case OP_FILL_ARRAY_DATA: {
Bill Buzbeec6f10662010-02-09 11:16:15 -08002900 RegLocation rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07002901 // Making a call - use explicit registers
Bill Buzbeec6f10662010-02-09 11:16:15 -08002902 dvmCompilerFlushAllRegs(cUnit); /* Everything to home location */
Bill Buzbee1465db52009-09-23 17:17:35 -07002903 genExportPC(cUnit, mir);
2904 loadValueDirectFixed(cUnit, rlSrc, r0);
Ben Chengbd1326d2010-04-02 15:04:53 -07002905 LOAD_FUNC_ADDR(cUnit, r2, (int)dvmInterpHandleFillArrayData);
Ben Cheng6c10a972009-10-29 14:39:18 -07002906 loadConstant(cUnit, r1,
2907 (int) (cUnit->method->insns + mir->offset + mir->dalvikInsn.vB));
Bill Buzbee1465db52009-09-23 17:17:35 -07002908 opReg(cUnit, kOpBlx, r2);
Elliott Hughes6a555132010-02-25 15:41:42 -08002909 dvmCompilerClobberCallRegs(cUnit);
Bill Buzbeefd023aa2009-11-02 09:23:49 -08002910 /* generate a branch over if successful */
buzbee8f8109a2010-08-31 10:16:35 -07002911 ArmLIR *branchOver = genCmpImmBranch(cUnit, kArmCondNe, r0, 0);
Bill Buzbeefd023aa2009-11-02 09:23:49 -08002912 loadConstant(cUnit, r0,
2913 (int) (cUnit->method->insns + mir->offset));
2914 genDispatchToHandler(cUnit, TEMPLATE_THROW_EXCEPTION_COMMON);
2915 ArmLIR *target = newLIR0(cUnit, kArmPseudoTargetLabel);
2916 target->defMask = ENCODE_ALL;
2917 branchOver->generic.target = (LIR *) target;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002918 break;
2919 }
2920 /*
Ben Cheng6c10a972009-10-29 14:39:18 -07002921 * Compute the goto target of up to
2922 * MIN(switchSize, MAX_CHAINED_SWITCH_CASES) + 1 chaining cells.
2923 * See the comment before findPackedSwitchIndex for the code layout.
Ben Chengba4fc8b2009-06-01 13:00:29 -07002924 */
2925 case OP_PACKED_SWITCH:
2926 case OP_SPARSE_SWITCH: {
Bill Buzbeec6f10662010-02-09 11:16:15 -08002927 RegLocation rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
2928 dvmCompilerFlushAllRegs(cUnit); /* Everything to home location */
Bill Buzbee1465db52009-09-23 17:17:35 -07002929 loadValueDirectFixed(cUnit, rlSrc, r1);
Bill Buzbeec6f10662010-02-09 11:16:15 -08002930 dvmCompilerLockAllTemps(cUnit);
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002931 if (dalvikOpcode == OP_PACKED_SWITCH) {
Ben Chengbd1326d2010-04-02 15:04:53 -07002932 LOAD_FUNC_ADDR(cUnit, r4PC, (int)findPackedSwitchIndex);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002933 } else {
Ben Chengbd1326d2010-04-02 15:04:53 -07002934 LOAD_FUNC_ADDR(cUnit, r4PC, (int)findSparseSwitchIndex);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002935 }
Ben Cheng6c10a972009-10-29 14:39:18 -07002936 /* r0 <- Addr of the switch data */
2937 loadConstant(cUnit, r0,
2938 (int) (cUnit->method->insns + mir->offset + mir->dalvikInsn.vB));
2939 /* r2 <- pc of the instruction following the blx */
Ben Cheng20d7e6c2011-02-18 17:12:42 -08002940 opRegReg(cUnit, kOpMov, r2, r15pc);
Bill Buzbee1465db52009-09-23 17:17:35 -07002941 opReg(cUnit, kOpBlx, r4PC);
Elliott Hughes6a555132010-02-25 15:41:42 -08002942 dvmCompilerClobberCallRegs(cUnit);
Ben Cheng6c10a972009-10-29 14:39:18 -07002943 /* pc <- computed goto target */
Ben Cheng20d7e6c2011-02-18 17:12:42 -08002944 opRegReg(cUnit, kOpMov, r15pc, r0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002945 break;
2946 }
2947 default:
2948 return true;
2949 }
2950 return false;
2951}
2952
Ben Cheng7a2697d2010-06-07 13:44:23 -07002953/*
2954 * See the example of predicted inlining listed before the
2955 * genValidationForPredictedInline function. The function here takes care the
2956 * branch over at 0x4858de78 and the misprediction target at 0x4858de7a.
2957 */
2958static void genLandingPadForMispredictedCallee(CompilationUnit *cUnit, MIR *mir,
2959 BasicBlock *bb,
2960 ArmLIR *labelList)
2961{
2962 BasicBlock *fallThrough = bb->fallThrough;
2963
2964 /* Bypass the move-result block if there is one */
2965 if (fallThrough->firstMIRInsn) {
2966 assert(fallThrough->firstMIRInsn->OptimizationFlags & MIR_INLINED_PRED);
2967 fallThrough = fallThrough->fallThrough;
2968 }
2969 /* Generate a branch over if the predicted inlining is correct */
2970 genUnconditionalBranch(cUnit, &labelList[fallThrough->id]);
2971
2972 /* Reset the register state */
2973 dvmCompilerResetRegPool(cUnit);
2974 dvmCompilerClobberAllRegs(cUnit);
2975 dvmCompilerResetNullCheck(cUnit);
2976
2977 /* Target for the slow invoke path */
2978 ArmLIR *target = newLIR0(cUnit, kArmPseudoTargetLabel);
2979 target->defMask = ENCODE_ALL;
2980 /* Hook up the target to the verification branch */
2981 mir->meta.callsiteInfo->misPredBranchOver->target = (LIR *) target;
2982}
2983
jeffhao71eee1f2011-01-04 14:18:54 -08002984static bool handleFmt35c_3rc_5rc(CompilationUnit *cUnit, MIR *mir,
2985 BasicBlock *bb, ArmLIR *labelList)
Ben Chengba4fc8b2009-06-01 13:00:29 -07002986{
Bill Buzbee9bc3df32009-07-30 10:52:29 -07002987 ArmLIR *retChainingCell = NULL;
Bill Buzbee89efc3d2009-07-28 11:22:22 -07002988 ArmLIR *pcrLabel = NULL;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002989
Ben Cheng7a2697d2010-06-07 13:44:23 -07002990 /* An invoke with the MIR_INLINED is effectively a no-op */
2991 if (mir->OptimizationFlags & MIR_INLINED)
2992 return false;
2993
Bill Buzbeef4ce16f2009-07-28 13:28:25 -07002994 if (bb->fallThrough != NULL)
2995 retChainingCell = &labelList[bb->fallThrough->id];
2996
Ben Chengba4fc8b2009-06-01 13:00:29 -07002997 DecodedInstruction *dInsn = &mir->dalvikInsn;
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002998 switch (mir->dalvikInsn.opcode) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07002999 /*
3000 * calleeMethod = this->clazz->vtable[
3001 * method->clazz->pDvmDex->pResMethods[BBBB]->methodIndex
3002 * ]
3003 */
3004 case OP_INVOKE_VIRTUAL:
jeffhao71eee1f2011-01-04 14:18:54 -08003005 case OP_INVOKE_VIRTUAL_RANGE:
3006 case OP_INVOKE_VIRTUAL_JUMBO: {
Bill Buzbee89efc3d2009-07-28 11:22:22 -07003007 ArmLIR *predChainingCell = &labelList[bb->taken->id];
Ben Chengba4fc8b2009-06-01 13:00:29 -07003008 int methodIndex =
3009 cUnit->method->clazz->pDvmDex->pResMethods[dInsn->vB]->
3010 methodIndex;
3011
Ben Cheng7a2697d2010-06-07 13:44:23 -07003012 /*
3013 * If the invoke has non-null misPredBranchOver, we need to generate
3014 * the non-inlined version of the invoke here to handle the
3015 * mispredicted case.
3016 */
3017 if (mir->meta.callsiteInfo->misPredBranchOver) {
3018 genLandingPadForMispredictedCallee(cUnit, mir, bb, labelList);
3019 }
3020
Dan Bornstein9a1f8162010-12-01 17:02:26 -08003021 if (mir->dalvikInsn.opcode == OP_INVOKE_VIRTUAL)
Ben Chengba4fc8b2009-06-01 13:00:29 -07003022 genProcessArgsNoRange(cUnit, mir, dInsn, &pcrLabel);
3023 else
3024 genProcessArgsRange(cUnit, mir, dInsn, &pcrLabel);
3025
Ben Cheng38329f52009-07-07 14:19:20 -07003026 genInvokeVirtualCommon(cUnit, mir, methodIndex,
3027 retChainingCell,
3028 predChainingCell,
3029 pcrLabel);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003030 break;
3031 }
3032 /*
3033 * calleeMethod = method->clazz->super->vtable[method->clazz->pDvmDex
3034 * ->pResMethods[BBBB]->methodIndex]
3035 */
Ben Chengba4fc8b2009-06-01 13:00:29 -07003036 case OP_INVOKE_SUPER:
jeffhao71eee1f2011-01-04 14:18:54 -08003037 case OP_INVOKE_SUPER_RANGE:
3038 case OP_INVOKE_SUPER_JUMBO: {
Ben Cheng7a2697d2010-06-07 13:44:23 -07003039 /* Grab the method ptr directly from what the interpreter sees */
3040 const Method *calleeMethod = mir->meta.callsiteInfo->method;
3041 assert(calleeMethod == cUnit->method->clazz->super->vtable[
3042 cUnit->method->clazz->pDvmDex->
3043 pResMethods[dInsn->vB]->methodIndex]);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003044
Dan Bornstein9a1f8162010-12-01 17:02:26 -08003045 if (mir->dalvikInsn.opcode == OP_INVOKE_SUPER)
Ben Chengba4fc8b2009-06-01 13:00:29 -07003046 genProcessArgsNoRange(cUnit, mir, dInsn, &pcrLabel);
3047 else
3048 genProcessArgsRange(cUnit, mir, dInsn, &pcrLabel);
3049
Ben Cheng20d7e6c2011-02-18 17:12:42 -08003050 if (mir->OptimizationFlags & MIR_INVOKE_METHOD_JIT) {
3051 const Method *calleeMethod = mir->meta.callsiteInfo->method;
3052 void *calleeAddr = dvmJitGetMethodAddr(calleeMethod->insns);
3053 assert(calleeAddr);
3054 genInvokeSingletonWholeMethod(cUnit, mir, calleeAddr,
3055 retChainingCell);
3056 } else {
3057 /* r0 = calleeMethod */
3058 loadConstant(cUnit, r0, (int) calleeMethod);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003059
Ben Cheng20d7e6c2011-02-18 17:12:42 -08003060 genInvokeSingletonCommon(cUnit, mir, bb, labelList, pcrLabel,
3061 calleeMethod);
3062 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07003063 break;
3064 }
3065 /* calleeMethod = method->clazz->pDvmDex->pResMethods[BBBB] */
3066 case OP_INVOKE_DIRECT:
jeffhao71eee1f2011-01-04 14:18:54 -08003067 case OP_INVOKE_DIRECT_RANGE:
3068 case OP_INVOKE_DIRECT_JUMBO: {
Ben Cheng7a2697d2010-06-07 13:44:23 -07003069 /* Grab the method ptr directly from what the interpreter sees */
3070 const Method *calleeMethod = mir->meta.callsiteInfo->method;
3071 assert(calleeMethod ==
3072 cUnit->method->clazz->pDvmDex->pResMethods[dInsn->vB]);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003073
Dan Bornstein9a1f8162010-12-01 17:02:26 -08003074 if (mir->dalvikInsn.opcode == OP_INVOKE_DIRECT)
Ben Chengba4fc8b2009-06-01 13:00:29 -07003075 genProcessArgsNoRange(cUnit, mir, dInsn, &pcrLabel);
3076 else
3077 genProcessArgsRange(cUnit, mir, dInsn, &pcrLabel);
3078
3079 /* r0 = calleeMethod */
3080 loadConstant(cUnit, r0, (int) calleeMethod);
3081
Ben Cheng38329f52009-07-07 14:19:20 -07003082 genInvokeSingletonCommon(cUnit, mir, bb, labelList, pcrLabel,
3083 calleeMethod);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003084 break;
3085 }
3086 /* calleeMethod = method->clazz->pDvmDex->pResMethods[BBBB] */
3087 case OP_INVOKE_STATIC:
jeffhao71eee1f2011-01-04 14:18:54 -08003088 case OP_INVOKE_STATIC_RANGE:
3089 case OP_INVOKE_STATIC_JUMBO: {
Ben Cheng7a2697d2010-06-07 13:44:23 -07003090 /* Grab the method ptr directly from what the interpreter sees */
3091 const Method *calleeMethod = mir->meta.callsiteInfo->method;
3092 assert(calleeMethod ==
3093 cUnit->method->clazz->pDvmDex->pResMethods[dInsn->vB]);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003094
Dan Bornstein9a1f8162010-12-01 17:02:26 -08003095 if (mir->dalvikInsn.opcode == OP_INVOKE_STATIC)
Ben Chengba4fc8b2009-06-01 13:00:29 -07003096 genProcessArgsNoRange(cUnit, mir, dInsn,
3097 NULL /* no null check */);
3098 else
3099 genProcessArgsRange(cUnit, mir, dInsn,
3100 NULL /* no null check */);
3101
Ben Cheng20d7e6c2011-02-18 17:12:42 -08003102 if (mir->OptimizationFlags & MIR_INVOKE_METHOD_JIT) {
3103 const Method *calleeMethod = mir->meta.callsiteInfo->method;
3104 void *calleeAddr = dvmJitGetMethodAddr(calleeMethod->insns);
3105 assert(calleeAddr);
3106 genInvokeSingletonWholeMethod(cUnit, mir, calleeAddr,
3107 retChainingCell);
3108 } else {
3109 /* r0 = calleeMethod */
3110 loadConstant(cUnit, r0, (int) calleeMethod);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003111
Ben Cheng20d7e6c2011-02-18 17:12:42 -08003112 genInvokeSingletonCommon(cUnit, mir, bb, labelList, pcrLabel,
3113 calleeMethod);
3114 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07003115 break;
3116 }
Ben Cheng09e50c92010-05-02 10:45:32 -07003117 /*
Ben Chengba4fc8b2009-06-01 13:00:29 -07003118 * calleeMethod = dvmFindInterfaceMethodInCache(this->clazz,
3119 * BBBB, method, method->clazz->pDvmDex)
Ben Cheng38329f52009-07-07 14:19:20 -07003120 *
Ben Cheng09e50c92010-05-02 10:45:32 -07003121 * The following is an example of generated code for
3122 * "invoke-interface v0"
Ben Cheng38329f52009-07-07 14:19:20 -07003123 *
Ben Cheng09e50c92010-05-02 10:45:32 -07003124 * -------- dalvik offset: 0x0008 @ invoke-interface v0
3125 * 0x47357e36 : ldr r0, [r5, #0] --+
3126 * 0x47357e38 : sub r7,r5,#24 |
3127 * 0x47357e3c : cmp r0, #0 | genProcessArgsNoRange
3128 * 0x47357e3e : beq 0x47357e82 |
3129 * 0x47357e40 : stmia r7, <r0> --+
3130 * 0x47357e42 : ldr r4, [pc, #120] --> r4 <- dalvikPC of this invoke
3131 * 0x47357e44 : add r1, pc, #64 --> r1 <- &retChainingCell
3132 * 0x47357e46 : add r2, pc, #72 --> r2 <- &predictedChainingCell
3133 * 0x47357e48 : blx_1 0x47348190 --+ TEMPLATE_INVOKE_METHOD_
3134 * 0x47357e4a : blx_2 see above --+ PREDICTED_CHAIN
3135 * 0x47357e4c : b 0x47357e90 --> off to the predicted chain
3136 * 0x47357e4e : b 0x47357e82 --> punt to the interpreter
3137 * 0x47357e50 : mov r8, r1 --+
3138 * 0x47357e52 : mov r9, r2 |
3139 * 0x47357e54 : ldr r2, [pc, #96] |
3140 * 0x47357e56 : mov r10, r3 |
3141 * 0x47357e58 : movs r0, r3 | dvmFindInterfaceMethodInCache
3142 * 0x47357e5a : ldr r3, [pc, #88] |
3143 * 0x47357e5c : ldr r7, [pc, #80] |
3144 * 0x47357e5e : mov r1, #1452 |
3145 * 0x47357e62 : blx r7 --+
3146 * 0x47357e64 : cmp r0, #0 --> calleeMethod == NULL?
3147 * 0x47357e66 : bne 0x47357e6e --> branch over the throw if !r0
3148 * 0x47357e68 : ldr r0, [pc, #80] --> load Dalvik PC of the invoke
3149 * 0x47357e6a : blx_1 0x47348494 --+ TEMPLATE_THROW_EXCEPTION_
3150 * 0x47357e6c : blx_2 see above --+ COMMON
3151 * 0x47357e6e : mov r1, r8 --> r1 <- &retChainingCell
3152 * 0x47357e70 : cmp r1, #0 --> compare against 0
3153 * 0x47357e72 : bgt 0x47357e7c --> >=0? don't rechain
Ben Chengaf5aa1f2011-01-04 15:37:04 -08003154 * 0x47357e74 : ldr r7, [pc, #off] --+
Ben Cheng09e50c92010-05-02 10:45:32 -07003155 * 0x47357e76 : mov r2, r9 | dvmJitToPatchPredictedChain
3156 * 0x47357e78 : mov r3, r10 |
3157 * 0x47357e7a : blx r7 --+
3158 * 0x47357e7c : add r1, pc, #8 --> r1 <- &retChainingCell
3159 * 0x47357e7e : blx_1 0x4734809c --+ TEMPLATE_INVOKE_METHOD_NO_OPT
3160 * 0x47357e80 : blx_2 see above --+
3161 * -------- reconstruct dalvik PC : 0x425719dc @ +0x0008
3162 * 0x47357e82 : ldr r0, [pc, #56]
Ben Cheng38329f52009-07-07 14:19:20 -07003163 * Exception_Handling:
Ben Cheng09e50c92010-05-02 10:45:32 -07003164 * 0x47357e84 : ldr r1, [r6, #92]
3165 * 0x47357e86 : blx r1
3166 * 0x47357e88 : .align4
3167 * -------- chaining cell (hot): 0x000b
3168 * 0x47357e88 : ldr r0, [r6, #104]
3169 * 0x47357e8a : blx r0
3170 * 0x47357e8c : data 0x19e2(6626)
3171 * 0x47357e8e : data 0x4257(16983)
3172 * 0x47357e90 : .align4
Ben Cheng38329f52009-07-07 14:19:20 -07003173 * -------- chaining cell (predicted)
Ben Cheng09e50c92010-05-02 10:45:32 -07003174 * 0x47357e90 : data 0xe7fe(59390) --> will be patched into bx
3175 * 0x47357e92 : data 0x0000(0)
3176 * 0x47357e94 : data 0x0000(0) --> class
3177 * 0x47357e96 : data 0x0000(0)
3178 * 0x47357e98 : data 0x0000(0) --> method
3179 * 0x47357e9a : data 0x0000(0)
3180 * 0x47357e9c : data 0x0000(0) --> rechain count
3181 * 0x47357e9e : data 0x0000(0)
3182 * -------- end of chaining cells (0x006c)
3183 * 0x47357eb0 : .word (0xad03e369)
3184 * 0x47357eb4 : .word (0x28a90)
3185 * 0x47357eb8 : .word (0x41a63394)
3186 * 0x47357ebc : .word (0x425719dc)
Ben Chengba4fc8b2009-06-01 13:00:29 -07003187 */
3188 case OP_INVOKE_INTERFACE:
jeffhao71eee1f2011-01-04 14:18:54 -08003189 case OP_INVOKE_INTERFACE_RANGE:
3190 case OP_INVOKE_INTERFACE_JUMBO: {
Bill Buzbee89efc3d2009-07-28 11:22:22 -07003191 ArmLIR *predChainingCell = &labelList[bb->taken->id];
Ben Chengba4fc8b2009-06-01 13:00:29 -07003192
Ben Cheng7a2697d2010-06-07 13:44:23 -07003193 /*
3194 * If the invoke has non-null misPredBranchOver, we need to generate
3195 * the non-inlined version of the invoke here to handle the
3196 * mispredicted case.
3197 */
3198 if (mir->meta.callsiteInfo->misPredBranchOver) {
3199 genLandingPadForMispredictedCallee(cUnit, mir, bb, labelList);
3200 }
Bill Buzbee1465db52009-09-23 17:17:35 -07003201
Dan Bornstein9a1f8162010-12-01 17:02:26 -08003202 if (mir->dalvikInsn.opcode == OP_INVOKE_INTERFACE)
Ben Chengba4fc8b2009-06-01 13:00:29 -07003203 genProcessArgsNoRange(cUnit, mir, dInsn, &pcrLabel);
3204 else
3205 genProcessArgsRange(cUnit, mir, dInsn, &pcrLabel);
3206
Ben Cheng38329f52009-07-07 14:19:20 -07003207 /* "this" is already left in r0 by genProcessArgs* */
3208
3209 /* r4PC = dalvikCallsite */
3210 loadConstant(cUnit, r4PC,
3211 (int) (cUnit->method->insns + mir->offset));
3212
3213 /* r1 = &retChainingCell */
Bill Buzbee270c1d62009-08-13 16:58:07 -07003214 ArmLIR *addrRetChain =
Ben Cheng20d7e6c2011-02-18 17:12:42 -08003215 opRegRegImm(cUnit, kOpAdd, r1, r15pc, 0);
Ben Cheng38329f52009-07-07 14:19:20 -07003216 addrRetChain->generic.target = (LIR *) retChainingCell;
3217
3218 /* r2 = &predictedChainingCell */
Bill Buzbee89efc3d2009-07-28 11:22:22 -07003219 ArmLIR *predictedChainingCell =
Ben Cheng20d7e6c2011-02-18 17:12:42 -08003220 opRegRegImm(cUnit, kOpAdd, r2, r15pc, 0);
Ben Cheng38329f52009-07-07 14:19:20 -07003221 predictedChainingCell->generic.target = (LIR *) predChainingCell;
3222
buzbee18fba342011-01-19 15:31:15 -08003223 genDispatchToHandler(cUnit, gDvmJit.methodTraceSupport ?
3224 TEMPLATE_INVOKE_METHOD_PREDICTED_CHAIN_PROF :
3225 TEMPLATE_INVOKE_METHOD_PREDICTED_CHAIN);
Ben Cheng38329f52009-07-07 14:19:20 -07003226
3227 /* return through lr - jump to the chaining cell */
3228 genUnconditionalBranch(cUnit, predChainingCell);
3229
3230 /*
3231 * null-check on "this" may have been eliminated, but we still need
3232 * a PC-reconstruction label for stack overflow bailout.
3233 */
3234 if (pcrLabel == NULL) {
3235 int dPC = (int) (cUnit->method->insns + mir->offset);
Carl Shapirofc75f3e2010-12-07 11:43:38 -08003236 pcrLabel = (ArmLIR *) dvmCompilerNew(sizeof(ArmLIR), true);
Dan Bornstein9a1f8162010-12-01 17:02:26 -08003237 pcrLabel->opcode = kArmPseudoPCReconstructionCell;
Ben Cheng38329f52009-07-07 14:19:20 -07003238 pcrLabel->operands[0] = dPC;
3239 pcrLabel->operands[1] = mir->offset;
3240 /* Insert the place holder to the growable list */
Ben Cheng00603072010-10-28 11:13:58 -07003241 dvmInsertGrowableList(&cUnit->pcReconstructionList,
3242 (intptr_t) pcrLabel);
Ben Cheng38329f52009-07-07 14:19:20 -07003243 }
3244
3245 /* return through lr+2 - punt to the interpreter */
3246 genUnconditionalBranch(cUnit, pcrLabel);
3247
3248 /*
3249 * return through lr+4 - fully resolve the callee method.
3250 * r1 <- count
3251 * r2 <- &predictedChainCell
3252 * r3 <- this->class
3253 * r4 <- dPC
3254 * r7 <- this->class->vtable
3255 */
3256
3257 /* Save count, &predictedChainCell, and class to high regs first */
Bill Buzbee1465db52009-09-23 17:17:35 -07003258 genRegCopy(cUnit, r8, r1);
3259 genRegCopy(cUnit, r9, r2);
3260 genRegCopy(cUnit, r10, r3);
Ben Cheng38329f52009-07-07 14:19:20 -07003261
Ben Chengba4fc8b2009-06-01 13:00:29 -07003262 /* r0 now contains this->clazz */
Bill Buzbee1465db52009-09-23 17:17:35 -07003263 genRegCopy(cUnit, r0, r3);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003264
3265 /* r1 = BBBB */
3266 loadConstant(cUnit, r1, dInsn->vB);
3267
3268 /* r2 = method (caller) */
3269 loadConstant(cUnit, r2, (int) cUnit->method);
3270
3271 /* r3 = pDvmDex */
3272 loadConstant(cUnit, r3, (int) cUnit->method->clazz->pDvmDex);
3273
Ben Chengbd1326d2010-04-02 15:04:53 -07003274 LOAD_FUNC_ADDR(cUnit, r7,
3275 (intptr_t) dvmFindInterfaceMethodInCache);
Bill Buzbee1465db52009-09-23 17:17:35 -07003276 opReg(cUnit, kOpBlx, r7);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003277 /* r0 = calleeMethod (returned from dvmFindInterfaceMethodInCache */
3278
Ben Cheng09e50c92010-05-02 10:45:32 -07003279 dvmCompilerClobberCallRegs(cUnit);
3280 /* generate a branch over if the interface method is resolved */
buzbee8f8109a2010-08-31 10:16:35 -07003281 ArmLIR *branchOver = genCmpImmBranch(cUnit, kArmCondNe, r0, 0);
Ben Cheng09e50c92010-05-02 10:45:32 -07003282 /*
3283 * calleeMethod == NULL -> throw
3284 */
3285 loadConstant(cUnit, r0,
3286 (int) (cUnit->method->insns + mir->offset));
3287 genDispatchToHandler(cUnit, TEMPLATE_THROW_EXCEPTION_COMMON);
3288 /* noreturn */
3289
3290 ArmLIR *target = newLIR0(cUnit, kArmPseudoTargetLabel);
3291 target->defMask = ENCODE_ALL;
3292 branchOver->generic.target = (LIR *) target;
3293
Bill Buzbee1465db52009-09-23 17:17:35 -07003294 genRegCopy(cUnit, r1, r8);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003295
Ben Cheng38329f52009-07-07 14:19:20 -07003296 /* Check if rechain limit is reached */
buzbee8f8109a2010-08-31 10:16:35 -07003297 ArmLIR *bypassRechaining = genCmpImmBranch(cUnit, kArmCondGt,
3298 r1, 0);
Ben Cheng38329f52009-07-07 14:19:20 -07003299
Ben Chengaf5aa1f2011-01-04 15:37:04 -08003300 LOAD_FUNC_ADDR(cUnit, r7, (int) dvmJitToPatchPredictedChain);
Ben Cheng38329f52009-07-07 14:19:20 -07003301
Ben Cheng20d7e6c2011-02-18 17:12:42 -08003302 genRegCopy(cUnit, r1, r6SELF);
Bill Buzbee1465db52009-09-23 17:17:35 -07003303 genRegCopy(cUnit, r2, r9);
3304 genRegCopy(cUnit, r3, r10);
Ben Cheng38329f52009-07-07 14:19:20 -07003305
3306 /*
3307 * r0 = calleeMethod
3308 * r2 = &predictedChainingCell
3309 * r3 = class
3310 *
3311 * &returnChainingCell has been loaded into r1 but is not needed
3312 * when patching the chaining cell and will be clobbered upon
3313 * returning so it will be reconstructed again.
3314 */
Bill Buzbee1465db52009-09-23 17:17:35 -07003315 opReg(cUnit, kOpBlx, r7);
Ben Cheng38329f52009-07-07 14:19:20 -07003316
3317 /* r1 = &retChainingCell */
Ben Cheng20d7e6c2011-02-18 17:12:42 -08003318 addrRetChain = opRegRegImm(cUnit, kOpAdd, r1, r15pc, 0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003319 addrRetChain->generic.target = (LIR *) retChainingCell;
Ben Cheng38329f52009-07-07 14:19:20 -07003320
3321 bypassRechaining->generic.target = (LIR *) addrRetChain;
3322
Ben Chengba4fc8b2009-06-01 13:00:29 -07003323 /*
3324 * r0 = this, r1 = calleeMethod,
3325 * r1 = &ChainingCell,
3326 * r4PC = callsiteDPC,
3327 */
buzbee18fba342011-01-19 15:31:15 -08003328 genDispatchToHandler(cUnit, gDvmJit.methodTraceSupport ?
3329 TEMPLATE_INVOKE_METHOD_NO_OPT_PROF :
3330 TEMPLATE_INVOKE_METHOD_NO_OPT);
Ben Cheng978738d2010-05-13 13:45:57 -07003331#if defined(WITH_JIT_TUNING)
Ben Cheng86717f72010-03-05 15:27:21 -08003332 gDvmJit.invokePolymorphic++;
Ben Chengba4fc8b2009-06-01 13:00:29 -07003333#endif
3334 /* Handle exceptions using the interpreter */
3335 genTrap(cUnit, mir->offset, pcrLabel);
3336 break;
3337 }
Andy McFadden0346e9d2011-03-01 15:47:46 -08003338 case OP_INVOKE_OBJECT_INIT_RANGE: {
Andy McFadden6af2ddd2011-02-16 16:50:40 -08003339 genInterpSingleStep(cUnit, mir);
buzbee18fba342011-01-19 15:31:15 -08003340 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07003341 }
3342 case OP_FILLED_NEW_ARRAY:
jeffhao71eee1f2011-01-04 14:18:54 -08003343 case OP_FILLED_NEW_ARRAY_RANGE:
3344 case OP_FILLED_NEW_ARRAY_JUMBO: {
Ben Chengba4fc8b2009-06-01 13:00:29 -07003345 /* Just let the interpreter deal with these */
3346 genInterpSingleStep(cUnit, mir);
3347 break;
3348 }
3349 default:
3350 return true;
3351 }
3352 return false;
3353}
3354
3355static bool handleFmt35ms_3rms(CompilationUnit *cUnit, MIR *mir,
Bill Buzbee89efc3d2009-07-28 11:22:22 -07003356 BasicBlock *bb, ArmLIR *labelList)
Ben Chengba4fc8b2009-06-01 13:00:29 -07003357{
Bill Buzbee89efc3d2009-07-28 11:22:22 -07003358 ArmLIR *pcrLabel = NULL;
Ben Chengba4fc8b2009-06-01 13:00:29 -07003359
Ben Cheng7a2697d2010-06-07 13:44:23 -07003360 /* An invoke with the MIR_INLINED is effectively a no-op */
3361 if (mir->OptimizationFlags & MIR_INLINED)
3362 return false;
3363
Ben Chengba4fc8b2009-06-01 13:00:29 -07003364 DecodedInstruction *dInsn = &mir->dalvikInsn;
Dan Bornstein9a1f8162010-12-01 17:02:26 -08003365 switch (mir->dalvikInsn.opcode) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07003366 /* calleeMethod = this->clazz->vtable[BBBB] */
3367 case OP_INVOKE_VIRTUAL_QUICK_RANGE:
3368 case OP_INVOKE_VIRTUAL_QUICK: {
3369 int methodIndex = dInsn->vB;
Bill Buzbeea8589332010-12-27 09:31:21 -08003370 ArmLIR *retChainingCell = &labelList[bb->fallThrough->id];
3371 ArmLIR *predChainingCell = &labelList[bb->taken->id];
Ben Cheng7a2697d2010-06-07 13:44:23 -07003372
3373 /*
3374 * If the invoke has non-null misPredBranchOver, we need to generate
3375 * the non-inlined version of the invoke here to handle the
3376 * mispredicted case.
3377 */
3378 if (mir->meta.callsiteInfo->misPredBranchOver) {
3379 genLandingPadForMispredictedCallee(cUnit, mir, bb, labelList);
3380 }
3381
Dan Bornstein9a1f8162010-12-01 17:02:26 -08003382 if (mir->dalvikInsn.opcode == OP_INVOKE_VIRTUAL_QUICK)
Ben Chengba4fc8b2009-06-01 13:00:29 -07003383 genProcessArgsNoRange(cUnit, mir, dInsn, &pcrLabel);
3384 else
3385 genProcessArgsRange(cUnit, mir, dInsn, &pcrLabel);
3386
Ben Chengcfdeca32011-01-14 11:36:46 -08003387
3388 if (mir->OptimizationFlags & MIR_INVOKE_METHOD_JIT) {
3389 const Method *calleeMethod = mir->meta.callsiteInfo->method;
3390 void *calleeAddr = dvmJitGetMethodAddr(calleeMethod->insns);
Ben Cheng20d7e6c2011-02-18 17:12:42 -08003391 assert(calleeAddr);
3392 genInvokeVirtualWholeMethod(cUnit, mir, calleeAddr,
3393 retChainingCell);
Ben Chengcfdeca32011-01-14 11:36:46 -08003394 }
3395
Ben Cheng38329f52009-07-07 14:19:20 -07003396 genInvokeVirtualCommon(cUnit, mir, methodIndex,
3397 retChainingCell,
3398 predChainingCell,
3399 pcrLabel);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003400 break;
3401 }
3402 /* calleeMethod = method->clazz->super->vtable[BBBB] */
3403 case OP_INVOKE_SUPER_QUICK:
3404 case OP_INVOKE_SUPER_QUICK_RANGE: {
Ben Cheng7a2697d2010-06-07 13:44:23 -07003405 /* Grab the method ptr directly from what the interpreter sees */
3406 const Method *calleeMethod = mir->meta.callsiteInfo->method;
3407 assert(calleeMethod ==
3408 cUnit->method->clazz->super->vtable[dInsn->vB]);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003409
Dan Bornstein9a1f8162010-12-01 17:02:26 -08003410 if (mir->dalvikInsn.opcode == OP_INVOKE_SUPER_QUICK)
Ben Chengba4fc8b2009-06-01 13:00:29 -07003411 genProcessArgsNoRange(cUnit, mir, dInsn, &pcrLabel);
3412 else
3413 genProcessArgsRange(cUnit, mir, dInsn, &pcrLabel);
3414
3415 /* r0 = calleeMethod */
3416 loadConstant(cUnit, r0, (int) calleeMethod);
3417
Ben Cheng38329f52009-07-07 14:19:20 -07003418 genInvokeSingletonCommon(cUnit, mir, bb, labelList, pcrLabel,
3419 calleeMethod);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003420 break;
3421 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07003422 default:
3423 return true;
3424 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07003425 return false;
3426}
3427
3428/*
Bill Buzbeefd023aa2009-11-02 09:23:49 -08003429 * This operation is complex enough that we'll do it partly inline
3430 * and partly with a handler. NOTE: the handler uses hardcoded
3431 * values for string object offsets and must be revisitied if the
3432 * layout changes.
3433 */
3434static bool genInlinedCompareTo(CompilationUnit *cUnit, MIR *mir)
3435{
3436#if defined(USE_GLOBAL_STRING_DEFS)
Elliott Hughes7e914f12011-01-19 18:18:42 -08003437 return handleExecuteInlineC(cUnit, mir);
Bill Buzbeefd023aa2009-11-02 09:23:49 -08003438#else
3439 ArmLIR *rollback;
Bill Buzbeec6f10662010-02-09 11:16:15 -08003440 RegLocation rlThis = dvmCompilerGetSrc(cUnit, mir, 0);
3441 RegLocation rlComp = dvmCompilerGetSrc(cUnit, mir, 1);
Bill Buzbeefd023aa2009-11-02 09:23:49 -08003442
3443 loadValueDirectFixed(cUnit, rlThis, r0);
3444 loadValueDirectFixed(cUnit, rlComp, r1);
3445 /* Test objects for NULL */
3446 rollback = genNullCheck(cUnit, rlThis.sRegLow, r0, mir->offset, NULL);
3447 genNullCheck(cUnit, rlComp.sRegLow, r1, mir->offset, rollback);
3448 /*
3449 * TUNING: we could check for object pointer equality before invoking
3450 * handler. Unclear whether the gain would be worth the added code size
3451 * expansion.
3452 */
3453 genDispatchToHandler(cUnit, TEMPLATE_STRING_COMPARETO);
Bill Buzbeec6f10662010-02-09 11:16:15 -08003454 storeValue(cUnit, inlinedTarget(cUnit, mir, false),
3455 dvmCompilerGetReturn(cUnit));
Elliott Hughes7e914f12011-01-19 18:18:42 -08003456 return false;
Bill Buzbeefd023aa2009-11-02 09:23:49 -08003457#endif
3458}
3459
Elliott Hughes2bdbcb62010-04-12 14:29:37 -07003460static bool genInlinedFastIndexOf(CompilationUnit *cUnit, MIR *mir)
Bill Buzbeefd023aa2009-11-02 09:23:49 -08003461{
3462#if defined(USE_GLOBAL_STRING_DEFS)
Elliott Hughes7e914f12011-01-19 18:18:42 -08003463 return handleExecuteInlineC(cUnit, mir);
Bill Buzbeefd023aa2009-11-02 09:23:49 -08003464#else
Bill Buzbeec6f10662010-02-09 11:16:15 -08003465 RegLocation rlThis = dvmCompilerGetSrc(cUnit, mir, 0);
3466 RegLocation rlChar = dvmCompilerGetSrc(cUnit, mir, 1);
Bill Buzbeefd023aa2009-11-02 09:23:49 -08003467
3468 loadValueDirectFixed(cUnit, rlThis, r0);
3469 loadValueDirectFixed(cUnit, rlChar, r1);
Elliott Hughes2bdbcb62010-04-12 14:29:37 -07003470 RegLocation rlStart = dvmCompilerGetSrc(cUnit, mir, 2);
3471 loadValueDirectFixed(cUnit, rlStart, r2);
Bill Buzbeefd023aa2009-11-02 09:23:49 -08003472 /* Test objects for NULL */
3473 genNullCheck(cUnit, rlThis.sRegLow, r0, mir->offset, NULL);
3474 genDispatchToHandler(cUnit, TEMPLATE_STRING_INDEXOF);
Bill Buzbeec6f10662010-02-09 11:16:15 -08003475 storeValue(cUnit, inlinedTarget(cUnit, mir, false),
3476 dvmCompilerGetReturn(cUnit));
Elliott Hughes7e914f12011-01-19 18:18:42 -08003477 return false;
Bill Buzbeefd023aa2009-11-02 09:23:49 -08003478#endif
3479}
3480
Elliott Hughesee34f592010-04-05 18:13:52 -07003481// Generates an inlined String.isEmpty or String.length.
3482static bool genInlinedStringIsEmptyOrLength(CompilationUnit *cUnit, MIR *mir,
3483 bool isEmpty)
Bill Buzbee1f748632010-03-02 16:14:41 -08003484{
Elliott Hughesee34f592010-04-05 18:13:52 -07003485 // dst = src.length();
Bill Buzbee1f748632010-03-02 16:14:41 -08003486 RegLocation rlObj = dvmCompilerGetSrc(cUnit, mir, 0);
3487 RegLocation rlDest = inlinedTarget(cUnit, mir, false);
3488 rlObj = loadValue(cUnit, rlObj, kCoreReg);
3489 RegLocation rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
3490 genNullCheck(cUnit, rlObj.sRegLow, rlObj.lowReg, mir->offset, NULL);
3491 loadWordDisp(cUnit, rlObj.lowReg, gDvm.offJavaLangString_count,
3492 rlResult.lowReg);
Elliott Hughesee34f592010-04-05 18:13:52 -07003493 if (isEmpty) {
3494 // dst = (dst == 0);
3495 int tReg = dvmCompilerAllocTemp(cUnit);
3496 opRegReg(cUnit, kOpNeg, tReg, rlResult.lowReg);
3497 opRegRegReg(cUnit, kOpAdc, rlResult.lowReg, rlResult.lowReg, tReg);
3498 }
Bill Buzbee1f748632010-03-02 16:14:41 -08003499 storeValue(cUnit, rlDest, rlResult);
3500 return false;
3501}
3502
Elliott Hughesee34f592010-04-05 18:13:52 -07003503static bool genInlinedStringLength(CompilationUnit *cUnit, MIR *mir)
3504{
3505 return genInlinedStringIsEmptyOrLength(cUnit, mir, false);
3506}
3507
3508static bool genInlinedStringIsEmpty(CompilationUnit *cUnit, MIR *mir)
3509{
3510 return genInlinedStringIsEmptyOrLength(cUnit, mir, true);
3511}
3512
Bill Buzbee1f748632010-03-02 16:14:41 -08003513static bool genInlinedStringCharAt(CompilationUnit *cUnit, MIR *mir)
3514{
3515 int contents = offsetof(ArrayObject, contents);
3516 RegLocation rlObj = dvmCompilerGetSrc(cUnit, mir, 0);
3517 RegLocation rlIdx = dvmCompilerGetSrc(cUnit, mir, 1);
3518 RegLocation rlDest = inlinedTarget(cUnit, mir, false);
3519 RegLocation rlResult;
3520 rlObj = loadValue(cUnit, rlObj, kCoreReg);
3521 rlIdx = loadValue(cUnit, rlIdx, kCoreReg);
3522 int regMax = dvmCompilerAllocTemp(cUnit);
3523 int regOff = dvmCompilerAllocTemp(cUnit);
3524 int regPtr = dvmCompilerAllocTemp(cUnit);
3525 ArmLIR *pcrLabel = genNullCheck(cUnit, rlObj.sRegLow, rlObj.lowReg,
3526 mir->offset, NULL);
3527 loadWordDisp(cUnit, rlObj.lowReg, gDvm.offJavaLangString_count, regMax);
3528 loadWordDisp(cUnit, rlObj.lowReg, gDvm.offJavaLangString_offset, regOff);
3529 loadWordDisp(cUnit, rlObj.lowReg, gDvm.offJavaLangString_value, regPtr);
3530 genBoundsCheck(cUnit, rlIdx.lowReg, regMax, mir->offset, pcrLabel);
3531 dvmCompilerFreeTemp(cUnit, regMax);
3532 opRegImm(cUnit, kOpAdd, regPtr, contents);
3533 opRegReg(cUnit, kOpAdd, regOff, rlIdx.lowReg);
3534 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
3535 loadBaseIndexed(cUnit, regPtr, regOff, rlResult.lowReg, 1, kUnsignedHalf);
3536 storeValue(cUnit, rlDest, rlResult);
3537 return false;
3538}
3539
3540static bool genInlinedAbsInt(CompilationUnit *cUnit, MIR *mir)
3541{
3542 RegLocation rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
3543 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
Elliott Hughese22bd842010-08-20 18:47:36 -07003544 RegLocation rlDest = inlinedTarget(cUnit, mir, false);
Bill Buzbee1f748632010-03-02 16:14:41 -08003545 RegLocation rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
3546 int signReg = dvmCompilerAllocTemp(cUnit);
3547 /*
3548 * abs(x) = y<=x>>31, (x+y)^y.
3549 * Thumb2's IT block also yields 3 instructions, but imposes
3550 * scheduling constraints.
3551 */
3552 opRegRegImm(cUnit, kOpAsr, signReg, rlSrc.lowReg, 31);
3553 opRegRegReg(cUnit, kOpAdd, rlResult.lowReg, rlSrc.lowReg, signReg);
3554 opRegReg(cUnit, kOpXor, rlResult.lowReg, signReg);
3555 storeValue(cUnit, rlDest, rlResult);
3556 return false;
3557}
3558
3559static bool genInlinedAbsLong(CompilationUnit *cUnit, MIR *mir)
3560{
3561 RegLocation rlSrc = dvmCompilerGetSrcWide(cUnit, mir, 0, 1);
3562 RegLocation rlDest = inlinedTargetWide(cUnit, mir, false);
3563 rlSrc = loadValueWide(cUnit, rlSrc, kCoreReg);
3564 RegLocation rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
3565 int signReg = dvmCompilerAllocTemp(cUnit);
3566 /*
3567 * abs(x) = y<=x>>31, (x+y)^y.
3568 * Thumb2 IT block allows slightly shorter sequence,
3569 * but introduces a scheduling barrier. Stick with this
3570 * mechanism for now.
3571 */
3572 opRegRegImm(cUnit, kOpAsr, signReg, rlSrc.highReg, 31);
3573 opRegRegReg(cUnit, kOpAdd, rlResult.lowReg, rlSrc.lowReg, signReg);
3574 opRegRegReg(cUnit, kOpAdc, rlResult.highReg, rlSrc.highReg, signReg);
3575 opRegReg(cUnit, kOpXor, rlResult.lowReg, signReg);
3576 opRegReg(cUnit, kOpXor, rlResult.highReg, signReg);
3577 storeValueWide(cUnit, rlDest, rlResult);
3578 return false;
3579}
Bill Buzbeefd023aa2009-11-02 09:23:49 -08003580
Elliott Hughese22bd842010-08-20 18:47:36 -07003581static bool genInlinedIntFloatConversion(CompilationUnit *cUnit, MIR *mir)
3582{
3583 // Just move from source to destination...
3584 RegLocation rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
3585 RegLocation rlDest = inlinedTarget(cUnit, mir, false);
3586 storeValue(cUnit, rlDest, rlSrc);
3587 return false;
3588}
3589
3590static bool genInlinedLongDoubleConversion(CompilationUnit *cUnit, MIR *mir)
3591{
3592 // Just move from source to destination...
3593 RegLocation rlSrc = dvmCompilerGetSrcWide(cUnit, mir, 0, 1);
3594 RegLocation rlDest = inlinedTargetWide(cUnit, mir, false);
3595 storeValueWide(cUnit, rlDest, rlSrc);
3596 return false;
3597}
3598
Bill Buzbeefd023aa2009-11-02 09:23:49 -08003599/*
Elliott Hughes7e914f12011-01-19 18:18:42 -08003600 * JITs a call to a C function.
3601 * TODO: use this for faster native method invocation for simple native
3602 * methods (http://b/3069458).
3603 */
3604static bool handleExecuteInlineC(CompilationUnit *cUnit, MIR *mir)
3605{
3606 DecodedInstruction *dInsn = &mir->dalvikInsn;
3607 int operation = dInsn->vB;
3608 unsigned int i;
3609 const InlineOperation* inLineTable = dvmGetInlineOpsTable();
3610 uintptr_t fn = (int) inLineTable[operation].func;
3611 if (fn == 0) {
3612 dvmCompilerAbort(cUnit);
3613 }
3614 dvmCompilerFlushAllRegs(cUnit); /* Everything to home location */
3615 dvmCompilerClobberCallRegs(cUnit);
3616 dvmCompilerClobber(cUnit, r4PC);
3617 dvmCompilerClobber(cUnit, r7);
buzbee9f601a92011-02-11 17:48:20 -08003618 int offset = offsetof(Thread, retval);
Ben Cheng20d7e6c2011-02-18 17:12:42 -08003619 opRegRegImm(cUnit, kOpAdd, r4PC, r6SELF, offset);
Elliott Hughes7e914f12011-01-19 18:18:42 -08003620 opImm(cUnit, kOpPush, (1<<r4PC) | (1<<r7));
3621 LOAD_FUNC_ADDR(cUnit, r4PC, fn);
3622 genExportPC(cUnit, mir);
3623 for (i=0; i < dInsn->vA; i++) {
3624 loadValueDirect(cUnit, dvmCompilerGetSrc(cUnit, mir, i), i);
3625 }
3626 opReg(cUnit, kOpBlx, r4PC);
Ben Cheng20d7e6c2011-02-18 17:12:42 -08003627 opRegImm(cUnit, kOpAdd, r13sp, 8);
Elliott Hughes7e914f12011-01-19 18:18:42 -08003628 /* NULL? */
3629 ArmLIR *branchOver = genCmpImmBranch(cUnit, kArmCondNe, r0, 0);
3630 loadConstant(cUnit, r0, (int) (cUnit->method->insns + mir->offset));
3631 genDispatchToHandler(cUnit, TEMPLATE_THROW_EXCEPTION_COMMON);
3632 ArmLIR *target = newLIR0(cUnit, kArmPseudoTargetLabel);
3633 target->defMask = ENCODE_ALL;
3634 branchOver->generic.target = (LIR *) target;
3635 return false;
3636}
3637
3638/*
Bill Buzbeece46c942009-11-20 15:41:34 -08003639 * NOTE: Handles both range and non-range versions (arguments
3640 * have already been normalized by this point).
Ben Chengba4fc8b2009-06-01 13:00:29 -07003641 */
Bill Buzbeece46c942009-11-20 15:41:34 -08003642static bool handleExecuteInline(CompilationUnit *cUnit, MIR *mir)
Ben Chengba4fc8b2009-06-01 13:00:29 -07003643{
3644 DecodedInstruction *dInsn = &mir->dalvikInsn;
Elliott Hughes7e914f12011-01-19 18:18:42 -08003645 assert(dInsn->opcode == OP_EXECUTE_INLINE_RANGE ||
3646 dInsn->opcode == OP_EXECUTE_INLINE);
3647 switch (dInsn->vB) {
3648 case INLINE_EMPTYINLINEMETHOD:
3649 return false; /* Nop */
3650
3651 /* These ones we potentially JIT inline. */
3652 case INLINE_STRING_LENGTH:
3653 return genInlinedStringLength(cUnit, mir);
3654 case INLINE_STRING_IS_EMPTY:
3655 return genInlinedStringIsEmpty(cUnit, mir);
3656 case INLINE_MATH_ABS_INT:
3657 return genInlinedAbsInt(cUnit, mir);
3658 case INLINE_MATH_ABS_LONG:
3659 return genInlinedAbsLong(cUnit, mir);
3660 case INLINE_MATH_MIN_INT:
3661 return genInlinedMinMaxInt(cUnit, mir, true);
3662 case INLINE_MATH_MAX_INT:
3663 return genInlinedMinMaxInt(cUnit, mir, false);
3664 case INLINE_STRING_CHARAT:
3665 return genInlinedStringCharAt(cUnit, mir);
3666 case INLINE_MATH_SQRT:
3667 return genInlineSqrt(cUnit, mir);
3668 case INLINE_MATH_ABS_FLOAT:
3669 return genInlinedAbsFloat(cUnit, mir);
3670 case INLINE_MATH_ABS_DOUBLE:
3671 return genInlinedAbsDouble(cUnit, mir);
3672 case INLINE_STRING_COMPARETO:
3673 return genInlinedCompareTo(cUnit, mir);
3674 case INLINE_STRING_FASTINDEXOF_II:
3675 return genInlinedFastIndexOf(cUnit, mir);
3676 case INLINE_FLOAT_TO_RAW_INT_BITS:
3677 case INLINE_INT_BITS_TO_FLOAT:
3678 return genInlinedIntFloatConversion(cUnit, mir);
3679 case INLINE_DOUBLE_TO_RAW_LONG_BITS:
3680 case INLINE_LONG_BITS_TO_DOUBLE:
3681 return genInlinedLongDoubleConversion(cUnit, mir);
3682
3683 /*
3684 * These ones we just JIT a call to a C function for.
3685 * TODO: special-case these in the other "invoke" call paths.
3686 */
3687 case INLINE_STRING_EQUALS:
3688 case INLINE_MATH_COS:
3689 case INLINE_MATH_SIN:
3690 case INLINE_FLOAT_TO_INT_BITS:
3691 case INLINE_DOUBLE_TO_LONG_BITS:
3692 return handleExecuteInlineC(cUnit, mir);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003693 }
Elliott Hughes7e914f12011-01-19 18:18:42 -08003694 dvmCompilerAbort(cUnit);
3695 return false; // Not reachable; keeps compiler happy.
Ben Chengba4fc8b2009-06-01 13:00:29 -07003696}
3697
3698static bool handleFmt51l(CompilationUnit *cUnit, MIR *mir)
3699{
Bill Buzbee1465db52009-09-23 17:17:35 -07003700 //TUNING: We're using core regs here - not optimal when target is a double
Bill Buzbeec6f10662010-02-09 11:16:15 -08003701 RegLocation rlDest = dvmCompilerGetDestWide(cUnit, mir, 0, 1);
3702 RegLocation rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Ben Chengbd1326d2010-04-02 15:04:53 -07003703 loadConstantNoClobber(cUnit, rlResult.lowReg,
3704 mir->dalvikInsn.vB_wide & 0xFFFFFFFFUL);
3705 loadConstantNoClobber(cUnit, rlResult.highReg,
3706 (mir->dalvikInsn.vB_wide>>32) & 0xFFFFFFFFUL);
Bill Buzbee1465db52009-09-23 17:17:35 -07003707 storeValueWide(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003708 return false;
3709}
3710
Ben Chengba4fc8b2009-06-01 13:00:29 -07003711/*
3712 * The following are special processing routines that handle transfer of
3713 * controls between compiled code and the interpreter. Certain VM states like
3714 * Dalvik PC and special-purpose registers are reconstructed here.
3715 */
3716
Bill Buzbeebd047242010-05-13 13:02:53 -07003717/*
3718 * Insert a
3719 * b .+4
3720 * nop
3721 * pair at the beginning of a chaining cell. This serves as the
3722 * switch branch that selects between reverting to the interpreter or
3723 * not. Once the cell is chained to a translation, the cell will
3724 * contain a 32-bit branch. Subsequent chain/unchain operations will
3725 * then only alter that first 16-bits - the "b .+4" for unchaining,
3726 * and the restoration of the first half of the 32-bit branch for
3727 * rechaining.
3728 */
3729static void insertChainingSwitch(CompilationUnit *cUnit)
3730{
3731 ArmLIR *branch = newLIR0(cUnit, kThumbBUncond);
3732 newLIR2(cUnit, kThumbOrr, r0, r0);
3733 ArmLIR *target = newLIR0(cUnit, kArmPseudoTargetLabel);
3734 target->defMask = ENCODE_ALL;
3735 branch->generic.target = (LIR *) target;
3736}
3737
Ben Cheng1efc9c52009-06-08 18:25:27 -07003738/* Chaining cell for code that may need warmup. */
3739static void handleNormalChainingCell(CompilationUnit *cUnit,
3740 unsigned int offset)
Ben Chengba4fc8b2009-06-01 13:00:29 -07003741{
Ben Cheng11d8f142010-03-24 15:24:19 -07003742 /*
3743 * Use raw instruction constructors to guarantee that the generated
3744 * instructions fit the predefined cell size.
3745 */
Bill Buzbeebd047242010-05-13 13:02:53 -07003746 insertChainingSwitch(cUnit);
Ben Cheng20d7e6c2011-02-18 17:12:42 -08003747 newLIR3(cUnit, kThumbLdrRRI5, r0, r6SELF,
buzbee9f601a92011-02-11 17:48:20 -08003748 offsetof(Thread,
Ben Cheng11d8f142010-03-24 15:24:19 -07003749 jitToInterpEntries.dvmJitToInterpNormal) >> 2);
3750 newLIR1(cUnit, kThumbBlxR, r0);
Ben Cheng385828e2011-03-04 16:48:33 -08003751 addWordData(cUnit, NULL, (int) (cUnit->method->insns + offset));
Ben Chengba4fc8b2009-06-01 13:00:29 -07003752}
3753
3754/*
Ben Cheng1efc9c52009-06-08 18:25:27 -07003755 * Chaining cell for instructions that immediately following already translated
3756 * code.
Ben Chengba4fc8b2009-06-01 13:00:29 -07003757 */
Ben Cheng1efc9c52009-06-08 18:25:27 -07003758static void handleHotChainingCell(CompilationUnit *cUnit,
3759 unsigned int offset)
Ben Chengba4fc8b2009-06-01 13:00:29 -07003760{
Ben Cheng11d8f142010-03-24 15:24:19 -07003761 /*
3762 * Use raw instruction constructors to guarantee that the generated
3763 * instructions fit the predefined cell size.
3764 */
Bill Buzbeebd047242010-05-13 13:02:53 -07003765 insertChainingSwitch(cUnit);
Ben Cheng20d7e6c2011-02-18 17:12:42 -08003766 newLIR3(cUnit, kThumbLdrRRI5, r0, r6SELF,
buzbee9f601a92011-02-11 17:48:20 -08003767 offsetof(Thread,
Ben Cheng11d8f142010-03-24 15:24:19 -07003768 jitToInterpEntries.dvmJitToInterpTraceSelect) >> 2);
3769 newLIR1(cUnit, kThumbBlxR, r0);
Ben Cheng385828e2011-03-04 16:48:33 -08003770 addWordData(cUnit, NULL, (int) (cUnit->method->insns + offset));
Ben Chengba4fc8b2009-06-01 13:00:29 -07003771}
3772
Jeff Hao97319a82009-08-12 16:57:15 -07003773/* Chaining cell for branches that branch back into the same basic block */
3774static void handleBackwardBranchChainingCell(CompilationUnit *cUnit,
3775 unsigned int offset)
3776{
Ben Cheng11d8f142010-03-24 15:24:19 -07003777 /*
3778 * Use raw instruction constructors to guarantee that the generated
3779 * instructions fit the predefined cell size.
3780 */
Bill Buzbeebd047242010-05-13 13:02:53 -07003781 insertChainingSwitch(cUnit);
Bill Buzbee9c4b7c82009-09-10 10:10:38 -07003782#if defined(WITH_SELF_VERIFICATION)
Ben Cheng20d7e6c2011-02-18 17:12:42 -08003783 newLIR3(cUnit, kThumbLdrRRI5, r0, r6SELF,
buzbee9f601a92011-02-11 17:48:20 -08003784 offsetof(Thread,
Ben Cheng40094c12010-02-24 20:58:44 -08003785 jitToInterpEntries.dvmJitToInterpBackwardBranch) >> 2);
Bill Buzbee9c4b7c82009-09-10 10:10:38 -07003786#else
Ben Cheng20d7e6c2011-02-18 17:12:42 -08003787 newLIR3(cUnit, kThumbLdrRRI5, r0, r6SELF,
buzbee9f601a92011-02-11 17:48:20 -08003788 offsetof(Thread, jitToInterpEntries.dvmJitToInterpNormal) >> 2);
Bill Buzbee9c4b7c82009-09-10 10:10:38 -07003789#endif
Bill Buzbee1465db52009-09-23 17:17:35 -07003790 newLIR1(cUnit, kThumbBlxR, r0);
Ben Cheng385828e2011-03-04 16:48:33 -08003791 addWordData(cUnit, NULL, (int) (cUnit->method->insns + offset));
Jeff Hao97319a82009-08-12 16:57:15 -07003792}
3793
Ben Chengba4fc8b2009-06-01 13:00:29 -07003794/* Chaining cell for monomorphic method invocations. */
Ben Cheng38329f52009-07-07 14:19:20 -07003795static void handleInvokeSingletonChainingCell(CompilationUnit *cUnit,
3796 const Method *callee)
Ben Chengba4fc8b2009-06-01 13:00:29 -07003797{
Ben Cheng11d8f142010-03-24 15:24:19 -07003798 /*
3799 * Use raw instruction constructors to guarantee that the generated
3800 * instructions fit the predefined cell size.
3801 */
Bill Buzbeebd047242010-05-13 13:02:53 -07003802 insertChainingSwitch(cUnit);
Ben Cheng20d7e6c2011-02-18 17:12:42 -08003803 newLIR3(cUnit, kThumbLdrRRI5, r0, r6SELF,
buzbee9f601a92011-02-11 17:48:20 -08003804 offsetof(Thread,
Ben Cheng11d8f142010-03-24 15:24:19 -07003805 jitToInterpEntries.dvmJitToInterpTraceSelect) >> 2);
3806 newLIR1(cUnit, kThumbBlxR, r0);
Ben Cheng385828e2011-03-04 16:48:33 -08003807 addWordData(cUnit, NULL, (int) (callee->insns));
Ben Chengba4fc8b2009-06-01 13:00:29 -07003808}
3809
Ben Cheng38329f52009-07-07 14:19:20 -07003810/* Chaining cell for monomorphic method invocations. */
3811static void handleInvokePredictedChainingCell(CompilationUnit *cUnit)
3812{
3813
3814 /* Should not be executed in the initial state */
Ben Cheng385828e2011-03-04 16:48:33 -08003815 addWordData(cUnit, NULL, PREDICTED_CHAIN_BX_PAIR_INIT);
Ben Cheng38329f52009-07-07 14:19:20 -07003816 /* To be filled: class */
Ben Cheng385828e2011-03-04 16:48:33 -08003817 addWordData(cUnit, NULL, PREDICTED_CHAIN_CLAZZ_INIT);
Ben Cheng38329f52009-07-07 14:19:20 -07003818 /* To be filled: method */
Ben Cheng385828e2011-03-04 16:48:33 -08003819 addWordData(cUnit, NULL, PREDICTED_CHAIN_METHOD_INIT);
Ben Cheng38329f52009-07-07 14:19:20 -07003820 /*
3821 * Rechain count. The initial value of 0 here will trigger chaining upon
3822 * the first invocation of this callsite.
3823 */
Ben Cheng385828e2011-03-04 16:48:33 -08003824 addWordData(cUnit, NULL, PREDICTED_CHAIN_COUNTER_INIT);
Ben Cheng38329f52009-07-07 14:19:20 -07003825}
3826
Ben Chengba4fc8b2009-06-01 13:00:29 -07003827/* Load the Dalvik PC into r0 and jump to the specified target */
3828static void handlePCReconstruction(CompilationUnit *cUnit,
Bill Buzbee89efc3d2009-07-28 11:22:22 -07003829 ArmLIR *targetLabel)
Ben Chengba4fc8b2009-06-01 13:00:29 -07003830{
Bill Buzbee89efc3d2009-07-28 11:22:22 -07003831 ArmLIR **pcrLabel =
3832 (ArmLIR **) cUnit->pcReconstructionList.elemList;
Ben Chengba4fc8b2009-06-01 13:00:29 -07003833 int numElems = cUnit->pcReconstructionList.numUsed;
3834 int i;
3835 for (i = 0; i < numElems; i++) {
3836 dvmCompilerAppendLIR(cUnit, (LIR *) pcrLabel[i]);
3837 /* r0 = dalvik PC */
3838 loadConstant(cUnit, r0, pcrLabel[i]->operands[0]);
3839 genUnconditionalBranch(cUnit, targetLabel);
3840 }
3841}
3842
Bill Buzbee1465db52009-09-23 17:17:35 -07003843static char *extendedMIROpNames[kMirOpLast - kMirOpFirst] = {
3844 "kMirOpPhi",
3845 "kMirOpNullNRangeUpCheck",
3846 "kMirOpNullNRangeDownCheck",
3847 "kMirOpLowerBound",
3848 "kMirOpPunt",
Ben Cheng7a2697d2010-06-07 13:44:23 -07003849 "kMirOpCheckInlinePrediction",
Ben Cheng4238ec22009-08-24 16:32:22 -07003850};
3851
3852/*
3853 * vA = arrayReg;
3854 * vB = idxReg;
3855 * vC = endConditionReg;
3856 * arg[0] = maxC
3857 * arg[1] = minC
3858 * arg[2] = loopBranchConditionCode
3859 */
3860static void genHoistedChecksForCountUpLoop(CompilationUnit *cUnit, MIR *mir)
3861{
Bill Buzbee1465db52009-09-23 17:17:35 -07003862 /*
3863 * NOTE: these synthesized blocks don't have ssa names assigned
3864 * for Dalvik registers. However, because they dominate the following
3865 * blocks we can simply use the Dalvik name w/ subscript 0 as the
3866 * ssa name.
3867 */
Ben Cheng4238ec22009-08-24 16:32:22 -07003868 DecodedInstruction *dInsn = &mir->dalvikInsn;
3869 const int lenOffset = offsetof(ArrayObject, length);
Ben Cheng4238ec22009-08-24 16:32:22 -07003870 const int maxC = dInsn->arg[0];
Bill Buzbee1465db52009-09-23 17:17:35 -07003871 int regLength;
3872 RegLocation rlArray = cUnit->regLocation[mir->dalvikInsn.vA];
3873 RegLocation rlIdxEnd = cUnit->regLocation[mir->dalvikInsn.vC];
Ben Cheng4238ec22009-08-24 16:32:22 -07003874
3875 /* regArray <- arrayRef */
Bill Buzbee1465db52009-09-23 17:17:35 -07003876 rlArray = loadValue(cUnit, rlArray, kCoreReg);
3877 rlIdxEnd = loadValue(cUnit, rlIdxEnd, kCoreReg);
3878 genRegImmCheck(cUnit, kArmCondEq, rlArray.lowReg, 0, 0,
Ben Cheng4238ec22009-08-24 16:32:22 -07003879 (ArmLIR *) cUnit->loopAnalysis->branchToPCR);
3880
3881 /* regLength <- len(arrayRef) */
Bill Buzbeec6f10662010-02-09 11:16:15 -08003882 regLength = dvmCompilerAllocTemp(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07003883 loadWordDisp(cUnit, rlArray.lowReg, lenOffset, regLength);
Ben Cheng4238ec22009-08-24 16:32:22 -07003884
3885 int delta = maxC;
3886 /*
3887 * If the loop end condition is ">=" instead of ">", then the largest value
3888 * of the index is "endCondition - 1".
3889 */
3890 if (dInsn->arg[2] == OP_IF_GE) {
3891 delta--;
3892 }
3893
3894 if (delta) {
Bill Buzbeec6f10662010-02-09 11:16:15 -08003895 int tReg = dvmCompilerAllocTemp(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07003896 opRegRegImm(cUnit, kOpAdd, tReg, rlIdxEnd.lowReg, delta);
3897 rlIdxEnd.lowReg = tReg;
Bill Buzbeec6f10662010-02-09 11:16:15 -08003898 dvmCompilerFreeTemp(cUnit, tReg);
Ben Cheng4238ec22009-08-24 16:32:22 -07003899 }
3900 /* Punt if "regIdxEnd < len(Array)" is false */
Bill Buzbee1465db52009-09-23 17:17:35 -07003901 genRegRegCheck(cUnit, kArmCondGe, rlIdxEnd.lowReg, regLength, 0,
Ben Cheng0fd31e42009-09-03 14:40:16 -07003902 (ArmLIR *) cUnit->loopAnalysis->branchToPCR);
Ben Cheng4238ec22009-08-24 16:32:22 -07003903}
3904
3905/*
3906 * vA = arrayReg;
3907 * vB = idxReg;
3908 * vC = endConditionReg;
3909 * arg[0] = maxC
3910 * arg[1] = minC
3911 * arg[2] = loopBranchConditionCode
3912 */
3913static void genHoistedChecksForCountDownLoop(CompilationUnit *cUnit, MIR *mir)
3914{
3915 DecodedInstruction *dInsn = &mir->dalvikInsn;
3916 const int lenOffset = offsetof(ArrayObject, length);
Bill Buzbeec6f10662010-02-09 11:16:15 -08003917 const int regLength = dvmCompilerAllocTemp(cUnit);
Ben Cheng4238ec22009-08-24 16:32:22 -07003918 const int maxC = dInsn->arg[0];
Bill Buzbee1465db52009-09-23 17:17:35 -07003919 RegLocation rlArray = cUnit->regLocation[mir->dalvikInsn.vA];
3920 RegLocation rlIdxInit = cUnit->regLocation[mir->dalvikInsn.vB];
Ben Cheng4238ec22009-08-24 16:32:22 -07003921
3922 /* regArray <- arrayRef */
Bill Buzbee1465db52009-09-23 17:17:35 -07003923 rlArray = loadValue(cUnit, rlArray, kCoreReg);
3924 rlIdxInit = loadValue(cUnit, rlIdxInit, kCoreReg);
3925 genRegImmCheck(cUnit, kArmCondEq, rlArray.lowReg, 0, 0,
Ben Cheng4238ec22009-08-24 16:32:22 -07003926 (ArmLIR *) cUnit->loopAnalysis->branchToPCR);
3927
3928 /* regLength <- len(arrayRef) */
Bill Buzbee1465db52009-09-23 17:17:35 -07003929 loadWordDisp(cUnit, rlArray.lowReg, lenOffset, regLength);
Ben Cheng4238ec22009-08-24 16:32:22 -07003930
3931 if (maxC) {
Bill Buzbeec6f10662010-02-09 11:16:15 -08003932 int tReg = dvmCompilerAllocTemp(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07003933 opRegRegImm(cUnit, kOpAdd, tReg, rlIdxInit.lowReg, maxC);
3934 rlIdxInit.lowReg = tReg;
Bill Buzbeec6f10662010-02-09 11:16:15 -08003935 dvmCompilerFreeTemp(cUnit, tReg);
Ben Cheng4238ec22009-08-24 16:32:22 -07003936 }
3937
3938 /* Punt if "regIdxInit < len(Array)" is false */
Bill Buzbee1465db52009-09-23 17:17:35 -07003939 genRegRegCheck(cUnit, kArmCondGe, rlIdxInit.lowReg, regLength, 0,
Ben Cheng0fd31e42009-09-03 14:40:16 -07003940 (ArmLIR *) cUnit->loopAnalysis->branchToPCR);
Ben Cheng4238ec22009-08-24 16:32:22 -07003941}
3942
3943/*
3944 * vA = idxReg;
3945 * vB = minC;
3946 */
3947static void genHoistedLowerBoundCheck(CompilationUnit *cUnit, MIR *mir)
3948{
3949 DecodedInstruction *dInsn = &mir->dalvikInsn;
Ben Cheng4238ec22009-08-24 16:32:22 -07003950 const int minC = dInsn->vB;
Bill Buzbee1465db52009-09-23 17:17:35 -07003951 RegLocation rlIdx = cUnit->regLocation[mir->dalvikInsn.vA];
Ben Cheng4238ec22009-08-24 16:32:22 -07003952
3953 /* regIdx <- initial index value */
Bill Buzbee1465db52009-09-23 17:17:35 -07003954 rlIdx = loadValue(cUnit, rlIdx, kCoreReg);
Ben Cheng4238ec22009-08-24 16:32:22 -07003955
3956 /* Punt if "regIdxInit + minC >= 0" is false */
Bill Buzbee1465db52009-09-23 17:17:35 -07003957 genRegImmCheck(cUnit, kArmCondLt, rlIdx.lowReg, -minC, 0,
Ben Cheng4238ec22009-08-24 16:32:22 -07003958 (ArmLIR *) cUnit->loopAnalysis->branchToPCR);
3959}
3960
Ben Cheng7a2697d2010-06-07 13:44:23 -07003961/*
3962 * vC = this
3963 *
3964 * A predicted inlining target looks like the following, where instructions
3965 * between 0x4858de66 and 0x4858de72 are checking if the predicted class
3966 * matches "this", and the verificaion code is generated by this routine.
3967 *
3968 * (C) means the instruction is inlined from the callee, and (PI) means the
3969 * instruction is the predicted inlined invoke, whose corresponding
3970 * instructions are still generated to handle the mispredicted case.
3971 *
3972 * D/dalvikvm( 86): -------- kMirOpCheckInlinePrediction
3973 * D/dalvikvm( 86): 0x4858de66 (0002): ldr r0, [r5, #68]
3974 * D/dalvikvm( 86): 0x4858de68 (0004): ldr r1, [pc, #140]
3975 * D/dalvikvm( 86): 0x4858de6a (0006): cmp r0, #0
3976 * D/dalvikvm( 86): 0x4858de6c (0008): beq 0x4858deb2
3977 * D/dalvikvm( 86): 0x4858de6e (000a): ldr r2, [r0, #0]
3978 * D/dalvikvm( 86): 0x4858de70 (000c): cmp r1, r2
3979 * D/dalvikvm( 86): 0x4858de72 (000e): bne 0x4858de7a
3980 * D/dalvikvm( 86): -------- dalvik offset: 0x004c @ +iget-object-quick (C)
3981 * v4, v17, (#8)
3982 * D/dalvikvm( 86): 0x4858de74 (0010): ldr r3, [r0, #8]
3983 * D/dalvikvm( 86): 0x4858de76 (0012): str r3, [r5, #16]
3984 * D/dalvikvm( 86): -------- dalvik offset: 0x004c @
3985 * +invoke-virtual-quick/range (PI) v17..v17
3986 * D/dalvikvm( 86): 0x4858de78 (0014): b 0x4858debc
3987 * D/dalvikvm( 86): 0x4858de7a (0016): add r4,r5,#68
3988 * D/dalvikvm( 86): -------- BARRIER
3989 * D/dalvikvm( 86): 0x4858de7e (001a): ldmia r4, <r0>
3990 * D/dalvikvm( 86): -------- BARRIER
3991 * D/dalvikvm( 86): 0x4858de80 (001c): sub r7,r5,#24
3992 * D/dalvikvm( 86): 0x4858de84 (0020): cmp r0, #0
3993 * D/dalvikvm( 86): 0x4858de86 (0022): beq 0x4858deb6
3994 * D/dalvikvm( 86): -------- BARRIER
3995 * D/dalvikvm( 86): 0x4858de88 (0024): stmia r7, <r0>
3996 * D/dalvikvm( 86): -------- BARRIER
3997 * D/dalvikvm( 86): 0x4858de8a (0026): ldr r4, [pc, #104]
3998 * D/dalvikvm( 86): 0x4858de8c (0028): add r1, pc, #28
3999 * D/dalvikvm( 86): 0x4858de8e (002a): add r2, pc, #56
4000 * D/dalvikvm( 86): 0x4858de90 (002c): blx_1 0x48589198
4001 * D/dalvikvm( 86): 0x4858de92 (002e): blx_2 see above
4002 * D/dalvikvm( 86): 0x4858de94 (0030): b 0x4858dec8
4003 * D/dalvikvm( 86): 0x4858de96 (0032): b 0x4858deb6
4004 * D/dalvikvm( 86): 0x4858de98 (0034): ldr r0, [r7, #72]
4005 * D/dalvikvm( 86): 0x4858de9a (0036): cmp r1, #0
4006 * D/dalvikvm( 86): 0x4858de9c (0038): bgt 0x4858dea4
4007 * D/dalvikvm( 86): 0x4858de9e (003a): ldr r7, [r6, #116]
4008 * D/dalvikvm( 86): 0x4858dea0 (003c): movs r1, r6
4009 * D/dalvikvm( 86): 0x4858dea2 (003e): blx r7
4010 * D/dalvikvm( 86): 0x4858dea4 (0040): add r1, pc, #4
4011 * D/dalvikvm( 86): 0x4858dea6 (0042): blx_1 0x485890a0
4012 * D/dalvikvm( 86): 0x4858dea8 (0044): blx_2 see above
4013 * D/dalvikvm( 86): 0x4858deaa (0046): b 0x4858deb6
4014 * D/dalvikvm( 86): 0x4858deac (0048): .align4
4015 * D/dalvikvm( 86): L0x004f:
4016 * D/dalvikvm( 86): -------- dalvik offset: 0x004f @ move-result-object (PI)
4017 * v4, (#0), (#0)
4018 * D/dalvikvm( 86): 0x4858deac (0048): ldr r4, [r6, #8]
4019 * D/dalvikvm( 86): 0x4858deae (004a): str r4, [r5, #16]
4020 * D/dalvikvm( 86): 0x4858deb0 (004c): b 0x4858debc
4021 * D/dalvikvm( 86): -------- reconstruct dalvik PC : 0x42beefcc @ +0x004c
4022 * D/dalvikvm( 86): 0x4858deb2 (004e): ldr r0, [pc, #64]
4023 * D/dalvikvm( 86): 0x4858deb4 (0050): b 0x4858deb8
4024 * D/dalvikvm( 86): -------- reconstruct dalvik PC : 0x42beefcc @ +0x004c
4025 * D/dalvikvm( 86): 0x4858deb6 (0052): ldr r0, [pc, #60]
4026 * D/dalvikvm( 86): Exception_Handling:
4027 * D/dalvikvm( 86): 0x4858deb8 (0054): ldr r1, [r6, #100]
4028 * D/dalvikvm( 86): 0x4858deba (0056): blx r1
4029 * D/dalvikvm( 86): 0x4858debc (0058): .align4
4030 * D/dalvikvm( 86): -------- chaining cell (hot): 0x0050
4031 * D/dalvikvm( 86): 0x4858debc (0058): b 0x4858dec0
4032 * D/dalvikvm( 86): 0x4858debe (005a): orrs r0, r0
4033 * D/dalvikvm( 86): 0x4858dec0 (005c): ldr r0, [r6, #112]
4034 * D/dalvikvm( 86): 0x4858dec2 (005e): blx r0
4035 * D/dalvikvm( 86): 0x4858dec4 (0060): data 0xefd4(61396)
4036 * D/dalvikvm( 86): 0x4858dec6 (0062): data 0x42be(17086)
4037 * D/dalvikvm( 86): 0x4858dec8 (0064): .align4
4038 * D/dalvikvm( 86): -------- chaining cell (predicted)
4039 * D/dalvikvm( 86): 0x4858dec8 (0064): data 0xe7fe(59390)
4040 * D/dalvikvm( 86): 0x4858deca (0066): data 0x0000(0)
4041 * D/dalvikvm( 86): 0x4858decc (0068): data 0x0000(0)
4042 * D/dalvikvm( 86): 0x4858dece (006a): data 0x0000(0)
4043 * :
4044 */
4045static void genValidationForPredictedInline(CompilationUnit *cUnit, MIR *mir)
4046{
4047 CallsiteInfo *callsiteInfo = mir->meta.callsiteInfo;
4048 RegLocation rlThis = cUnit->regLocation[mir->dalvikInsn.vC];
4049
4050 rlThis = loadValue(cUnit, rlThis, kCoreReg);
4051 int regPredictedClass = dvmCompilerAllocTemp(cUnit);
Ben Cheng385828e2011-03-04 16:48:33 -08004052 loadClassPointer(cUnit, regPredictedClass, (int) callsiteInfo);
Ben Cheng7a2697d2010-06-07 13:44:23 -07004053 genNullCheck(cUnit, rlThis.sRegLow, rlThis.lowReg, mir->offset,
4054 NULL);/* null object? */
4055 int regActualClass = dvmCompilerAllocTemp(cUnit);
4056 loadWordDisp(cUnit, rlThis.lowReg, offsetof(Object, clazz), regActualClass);
4057 opRegReg(cUnit, kOpCmp, regPredictedClass, regActualClass);
4058 /*
4059 * Set the misPredBranchOver target so that it will be generated when the
4060 * code for the non-optimized invoke is generated.
4061 */
4062 callsiteInfo->misPredBranchOver = (LIR *) opCondBranch(cUnit, kArmCondNe);
4063}
4064
Ben Cheng4238ec22009-08-24 16:32:22 -07004065/* Extended MIR instructions like PHI */
4066static void handleExtendedMIR(CompilationUnit *cUnit, MIR *mir)
4067{
Dan Bornstein9a1f8162010-12-01 17:02:26 -08004068 int opOffset = mir->dalvikInsn.opcode - kMirOpFirst;
Carl Shapirofc75f3e2010-12-07 11:43:38 -08004069 char *msg = (char *)dvmCompilerNew(strlen(extendedMIROpNames[opOffset]) + 1,
4070 false);
Ben Cheng4238ec22009-08-24 16:32:22 -07004071 strcpy(msg, extendedMIROpNames[opOffset]);
Bill Buzbee1465db52009-09-23 17:17:35 -07004072 newLIR1(cUnit, kArmPseudoExtended, (int) msg);
Ben Cheng4238ec22009-08-24 16:32:22 -07004073
Dan Bornstein9a1f8162010-12-01 17:02:26 -08004074 switch (mir->dalvikInsn.opcode) {
Bill Buzbee1465db52009-09-23 17:17:35 -07004075 case kMirOpPhi: {
Ben Cheng4238ec22009-08-24 16:32:22 -07004076 char *ssaString = dvmCompilerGetSSAString(cUnit, mir->ssaRep);
Bill Buzbee1465db52009-09-23 17:17:35 -07004077 newLIR1(cUnit, kArmPseudoSSARep, (int) ssaString);
Ben Cheng4238ec22009-08-24 16:32:22 -07004078 break;
4079 }
Bill Buzbee1465db52009-09-23 17:17:35 -07004080 case kMirOpNullNRangeUpCheck: {
Ben Cheng4238ec22009-08-24 16:32:22 -07004081 genHoistedChecksForCountUpLoop(cUnit, mir);
4082 break;
4083 }
Bill Buzbee1465db52009-09-23 17:17:35 -07004084 case kMirOpNullNRangeDownCheck: {
Ben Cheng4238ec22009-08-24 16:32:22 -07004085 genHoistedChecksForCountDownLoop(cUnit, mir);
4086 break;
4087 }
Bill Buzbee1465db52009-09-23 17:17:35 -07004088 case kMirOpLowerBound: {
Ben Cheng4238ec22009-08-24 16:32:22 -07004089 genHoistedLowerBoundCheck(cUnit, mir);
4090 break;
4091 }
Bill Buzbee1465db52009-09-23 17:17:35 -07004092 case kMirOpPunt: {
Ben Cheng4238ec22009-08-24 16:32:22 -07004093 genUnconditionalBranch(cUnit,
4094 (ArmLIR *) cUnit->loopAnalysis->branchToPCR);
4095 break;
4096 }
Ben Cheng7a2697d2010-06-07 13:44:23 -07004097 case kMirOpCheckInlinePrediction: {
4098 genValidationForPredictedInline(cUnit, mir);
4099 break;
4100 }
Ben Cheng4238ec22009-08-24 16:32:22 -07004101 default:
4102 break;
4103 }
4104}
4105
4106/*
4107 * Create a PC-reconstruction cell for the starting offset of this trace.
4108 * Since the PCR cell is placed near the end of the compiled code which is
4109 * usually out of range for a conditional branch, we put two branches (one
4110 * branch over to the loop body and one layover branch to the actual PCR) at the
4111 * end of the entry block.
4112 */
4113static void setupLoopEntryBlock(CompilationUnit *cUnit, BasicBlock *entry,
4114 ArmLIR *bodyLabel)
4115{
4116 /* Set up the place holder to reconstruct this Dalvik PC */
Carl Shapirofc75f3e2010-12-07 11:43:38 -08004117 ArmLIR *pcrLabel = (ArmLIR *) dvmCompilerNew(sizeof(ArmLIR), true);
Dan Bornstein9a1f8162010-12-01 17:02:26 -08004118 pcrLabel->opcode = kArmPseudoPCReconstructionCell;
Ben Cheng4238ec22009-08-24 16:32:22 -07004119 pcrLabel->operands[0] =
4120 (int) (cUnit->method->insns + entry->startOffset);
4121 pcrLabel->operands[1] = entry->startOffset;
4122 /* Insert the place holder to the growable list */
Ben Cheng00603072010-10-28 11:13:58 -07004123 dvmInsertGrowableList(&cUnit->pcReconstructionList, (intptr_t) pcrLabel);
Ben Cheng4238ec22009-08-24 16:32:22 -07004124
4125 /*
4126 * Next, create two branches - one branch over to the loop body and the
4127 * other branch to the PCR cell to punt.
4128 */
Carl Shapirofc75f3e2010-12-07 11:43:38 -08004129 ArmLIR *branchToBody = (ArmLIR *) dvmCompilerNew(sizeof(ArmLIR), true);
Dan Bornstein9a1f8162010-12-01 17:02:26 -08004130 branchToBody->opcode = kThumbBUncond;
Ben Cheng4238ec22009-08-24 16:32:22 -07004131 branchToBody->generic.target = (LIR *) bodyLabel;
Ben Chengdcf3e5d2009-09-11 13:42:05 -07004132 setupResourceMasks(branchToBody);
Ben Cheng4238ec22009-08-24 16:32:22 -07004133 cUnit->loopAnalysis->branchToBody = (LIR *) branchToBody;
4134
Carl Shapirofc75f3e2010-12-07 11:43:38 -08004135 ArmLIR *branchToPCR = (ArmLIR *) dvmCompilerNew(sizeof(ArmLIR), true);
Dan Bornstein9a1f8162010-12-01 17:02:26 -08004136 branchToPCR->opcode = kThumbBUncond;
Ben Cheng4238ec22009-08-24 16:32:22 -07004137 branchToPCR->generic.target = (LIR *) pcrLabel;
Ben Chengdcf3e5d2009-09-11 13:42:05 -07004138 setupResourceMasks(branchToPCR);
Ben Cheng4238ec22009-08-24 16:32:22 -07004139 cUnit->loopAnalysis->branchToPCR = (LIR *) branchToPCR;
4140}
4141
Ben Chengd5adae12010-03-26 17:45:28 -07004142#if defined(WITH_SELF_VERIFICATION)
4143static bool selfVerificationPuntOps(MIR *mir)
4144{
4145 DecodedInstruction *decInsn = &mir->dalvikInsn;
Dan Bornstein9a1f8162010-12-01 17:02:26 -08004146 Opcode op = decInsn->opcode;
Ben Cheng7a2697d2010-06-07 13:44:23 -07004147
Ben Chengd5adae12010-03-26 17:45:28 -07004148 /*
4149 * All opcodes that can throw exceptions and use the
4150 * TEMPLATE_THROW_EXCEPTION_COMMON template should be excluded in the trace
4151 * under self-verification mode.
4152 */
4153 return (op == OP_MONITOR_ENTER || op == OP_MONITOR_EXIT ||
4154 op == OP_NEW_INSTANCE || op == OP_NEW_ARRAY ||
4155 op == OP_CHECK_CAST || op == OP_MOVE_EXCEPTION ||
4156 op == OP_FILL_ARRAY_DATA || op == OP_EXECUTE_INLINE ||
Ben Cheng7a2697d2010-06-07 13:44:23 -07004157 op == OP_EXECUTE_INLINE_RANGE);
Ben Chengd5adae12010-03-26 17:45:28 -07004158}
4159#endif
4160
Ben Chengba4fc8b2009-06-01 13:00:29 -07004161void dvmCompilerMIR2LIR(CompilationUnit *cUnit)
4162{
4163 /* Used to hold the labels of each block */
Bill Buzbee89efc3d2009-07-28 11:22:22 -07004164 ArmLIR *labelList =
Carl Shapirofc75f3e2010-12-07 11:43:38 -08004165 (ArmLIR *) dvmCompilerNew(sizeof(ArmLIR) * cUnit->numBlocks, true);
Ben Chengcec26f62010-01-15 15:29:33 -08004166 GrowableList chainingListByType[kChainingCellGap];
Ben Chengba4fc8b2009-06-01 13:00:29 -07004167 int i;
4168
4169 /*
Ben Cheng38329f52009-07-07 14:19:20 -07004170 * Initialize various types chaining lists.
Ben Chengba4fc8b2009-06-01 13:00:29 -07004171 */
Ben Chengcec26f62010-01-15 15:29:33 -08004172 for (i = 0; i < kChainingCellGap; i++) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07004173 dvmInitGrowableList(&chainingListByType[i], 2);
4174 }
4175
Ben Cheng7ab74e12011-02-03 14:02:06 -08004176 /* Clear the visited flag for each block */
4177 dvmCompilerDataFlowAnalysisDispatcher(cUnit, dvmCompilerClearVisitedFlag,
4178 kAllNodes, false /* isIterative */);
4179
Ben Cheng00603072010-10-28 11:13:58 -07004180 GrowableListIterator iterator;
4181 dvmGrowableListIteratorInit(&cUnit->blockList, &iterator);
Ben Chengba4fc8b2009-06-01 13:00:29 -07004182
buzbee2e152ba2010-12-15 16:32:35 -08004183 /* Traces start with a profiling entry point. Generate it here */
4184 cUnit->profileCodeSize = genTraceProfileEntry(cUnit);
Ben Cheng1efc9c52009-06-08 18:25:27 -07004185
Ben Chengba4fc8b2009-06-01 13:00:29 -07004186 /* Handle the content in each basic block */
Ben Cheng00603072010-10-28 11:13:58 -07004187 for (i = 0; ; i++) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07004188 MIR *mir;
Ben Cheng00603072010-10-28 11:13:58 -07004189 BasicBlock *bb = (BasicBlock *) dvmGrowableListIteratorNext(&iterator);
4190 if (bb == NULL) break;
Ben Cheng7ab74e12011-02-03 14:02:06 -08004191 if (bb->visited == true) continue;
Ben Chengba4fc8b2009-06-01 13:00:29 -07004192
Ben Cheng00603072010-10-28 11:13:58 -07004193 labelList[i].operands[0] = bb->startOffset;
Ben Chengba4fc8b2009-06-01 13:00:29 -07004194
Ben Cheng00603072010-10-28 11:13:58 -07004195 if (bb->blockType >= kChainingCellGap) {
4196 if (bb->isFallThroughFromInvoke == true) {
Ben Chengd44faf52010-06-02 15:33:51 -07004197 /* Align this block first since it is a return chaining cell */
4198 newLIR0(cUnit, kArmPseudoPseudoAlign4);
4199 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07004200 /*
4201 * Append the label pseudo LIR first. Chaining cells will be handled
4202 * separately afterwards.
4203 */
4204 dvmCompilerAppendLIR(cUnit, (LIR *) &labelList[i]);
4205 }
4206
Ben Cheng00603072010-10-28 11:13:58 -07004207 if (bb->blockType == kTraceEntryBlock) {
Dan Bornstein9a1f8162010-12-01 17:02:26 -08004208 labelList[i].opcode = kArmPseudoEntryBlock;
Ben Cheng00603072010-10-28 11:13:58 -07004209 if (bb->firstMIRInsn == NULL) {
Ben Cheng4238ec22009-08-24 16:32:22 -07004210 continue;
4211 } else {
Ben Cheng00603072010-10-28 11:13:58 -07004212 setupLoopEntryBlock(cUnit, bb,
4213 &labelList[bb->fallThrough->id]);
Ben Cheng4238ec22009-08-24 16:32:22 -07004214 }
Ben Cheng00603072010-10-28 11:13:58 -07004215 } else if (bb->blockType == kTraceExitBlock) {
Dan Bornstein9a1f8162010-12-01 17:02:26 -08004216 labelList[i].opcode = kArmPseudoExitBlock;
Ben Cheng4238ec22009-08-24 16:32:22 -07004217 goto gen_fallthrough;
Ben Cheng00603072010-10-28 11:13:58 -07004218 } else if (bb->blockType == kDalvikByteCode) {
Dan Bornstein9a1f8162010-12-01 17:02:26 -08004219 labelList[i].opcode = kArmPseudoNormalBlockLabel;
Ben Chenge9695e52009-06-16 16:11:47 -07004220 /* Reset the register state */
Bill Buzbeec6f10662010-02-09 11:16:15 -08004221 dvmCompilerResetRegPool(cUnit);
4222 dvmCompilerClobberAllRegs(cUnit);
4223 dvmCompilerResetNullCheck(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -07004224 } else {
Ben Cheng00603072010-10-28 11:13:58 -07004225 switch (bb->blockType) {
Bill Buzbee1465db52009-09-23 17:17:35 -07004226 case kChainingCellNormal:
Dan Bornstein9a1f8162010-12-01 17:02:26 -08004227 labelList[i].opcode = kArmPseudoChainingCellNormal;
Ben Chengba4fc8b2009-06-01 13:00:29 -07004228 /* handle the codegen later */
4229 dvmInsertGrowableList(
Ben Cheng00603072010-10-28 11:13:58 -07004230 &chainingListByType[kChainingCellNormal], i);
Ben Chengba4fc8b2009-06-01 13:00:29 -07004231 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07004232 case kChainingCellInvokeSingleton:
Dan Bornstein9a1f8162010-12-01 17:02:26 -08004233 labelList[i].opcode =
Ben Chenga4973592010-03-31 11:59:18 -07004234 kArmPseudoChainingCellInvokeSingleton;
Ben Chengba4fc8b2009-06-01 13:00:29 -07004235 labelList[i].operands[0] =
Ben Cheng00603072010-10-28 11:13:58 -07004236 (int) bb->containingMethod;
Ben Chengba4fc8b2009-06-01 13:00:29 -07004237 /* handle the codegen later */
4238 dvmInsertGrowableList(
Ben Cheng00603072010-10-28 11:13:58 -07004239 &chainingListByType[kChainingCellInvokeSingleton], i);
Ben Cheng38329f52009-07-07 14:19:20 -07004240 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07004241 case kChainingCellInvokePredicted:
Dan Bornstein9a1f8162010-12-01 17:02:26 -08004242 labelList[i].opcode =
Ben Chenga4973592010-03-31 11:59:18 -07004243 kArmPseudoChainingCellInvokePredicted;
Ben Cheng04517042011-03-14 11:16:21 -07004244 /*
4245 * Move the cached method pointer from operand 1 to 0.
4246 * Operand 0 was clobbered earlier in this routine to store
4247 * the block starting offset, which is not applicable to
4248 * predicted chaining cell.
4249 */
4250 labelList[i].operands[0] = labelList[i].operands[1];
Ben Cheng38329f52009-07-07 14:19:20 -07004251 /* handle the codegen later */
4252 dvmInsertGrowableList(
Ben Cheng00603072010-10-28 11:13:58 -07004253 &chainingListByType[kChainingCellInvokePredicted], i);
Ben Chengba4fc8b2009-06-01 13:00:29 -07004254 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07004255 case kChainingCellHot:
Dan Bornstein9a1f8162010-12-01 17:02:26 -08004256 labelList[i].opcode =
Ben Chenga4973592010-03-31 11:59:18 -07004257 kArmPseudoChainingCellHot;
Ben Chengba4fc8b2009-06-01 13:00:29 -07004258 /* handle the codegen later */
4259 dvmInsertGrowableList(
Ben Cheng00603072010-10-28 11:13:58 -07004260 &chainingListByType[kChainingCellHot], i);
Ben Chengba4fc8b2009-06-01 13:00:29 -07004261 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07004262 case kPCReconstruction:
Ben Chengba4fc8b2009-06-01 13:00:29 -07004263 /* Make sure exception handling block is next */
Dan Bornstein9a1f8162010-12-01 17:02:26 -08004264 labelList[i].opcode =
Ben Chenga4973592010-03-31 11:59:18 -07004265 kArmPseudoPCReconstructionBlockLabel;
Ben Chengba4fc8b2009-06-01 13:00:29 -07004266 assert (i == cUnit->numBlocks - 2);
4267 handlePCReconstruction(cUnit, &labelList[i+1]);
4268 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07004269 case kExceptionHandling:
Dan Bornstein9a1f8162010-12-01 17:02:26 -08004270 labelList[i].opcode = kArmPseudoEHBlockLabel;
Ben Chengba4fc8b2009-06-01 13:00:29 -07004271 if (cUnit->pcReconstructionList.numUsed) {
Ben Cheng20d7e6c2011-02-18 17:12:42 -08004272 loadWordDisp(cUnit, r6SELF, offsetof(Thread,
Bill Buzbee270c1d62009-08-13 16:58:07 -07004273 jitToInterpEntries.dvmJitToInterpPunt),
4274 r1);
Bill Buzbee1465db52009-09-23 17:17:35 -07004275 opReg(cUnit, kOpBlx, r1);
Ben Chengba4fc8b2009-06-01 13:00:29 -07004276 }
4277 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07004278 case kChainingCellBackwardBranch:
Dan Bornstein9a1f8162010-12-01 17:02:26 -08004279 labelList[i].opcode =
Ben Chenga4973592010-03-31 11:59:18 -07004280 kArmPseudoChainingCellBackwardBranch;
Jeff Hao97319a82009-08-12 16:57:15 -07004281 /* handle the codegen later */
4282 dvmInsertGrowableList(
Bill Buzbee1465db52009-09-23 17:17:35 -07004283 &chainingListByType[kChainingCellBackwardBranch],
Ben Cheng00603072010-10-28 11:13:58 -07004284 i);
Jeff Hao97319a82009-08-12 16:57:15 -07004285 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07004286 default:
4287 break;
4288 }
4289 continue;
4290 }
Ben Chenge9695e52009-06-16 16:11:47 -07004291
Bill Buzbee89efc3d2009-07-28 11:22:22 -07004292 ArmLIR *headLIR = NULL;
Ben Cheng7ab74e12011-02-03 14:02:06 -08004293 BasicBlock *nextBB = bb;
Ben Chenge9695e52009-06-16 16:11:47 -07004294
Ben Cheng7ab74e12011-02-03 14:02:06 -08004295 /*
4296 * Try to build a longer optimization unit. Currently if the previous
4297 * block ends with a goto, we continue adding instructions and don't
4298 * reset the register allocation pool.
4299 */
4300 for (; nextBB != NULL; nextBB = cUnit->nextCodegenBlock) {
4301 bb = nextBB;
4302 bb->visited = true;
4303 cUnit->nextCodegenBlock = NULL;
Bill Buzbee1465db52009-09-23 17:17:35 -07004304
Ben Cheng7ab74e12011-02-03 14:02:06 -08004305 for (mir = bb->firstMIRInsn; mir; mir = mir->next) {
Bill Buzbee1465db52009-09-23 17:17:35 -07004306
Ben Cheng7ab74e12011-02-03 14:02:06 -08004307 dvmCompilerResetRegPool(cUnit);
4308 if (gDvmJit.disableOpt & (1 << kTrackLiveTemps)) {
4309 dvmCompilerClobberAllRegs(cUnit);
Ben Cheng80211d22011-01-14 10:23:37 -08004310 }
Ben Cheng4238ec22009-08-24 16:32:22 -07004311
Ben Cheng7ab74e12011-02-03 14:02:06 -08004312 if (gDvmJit.disableOpt & (1 << kSuppressLoads)) {
4313 dvmCompilerResetDefTracking(cUnit);
4314 }
Ben Cheng4238ec22009-08-24 16:32:22 -07004315
Ben Cheng7ab74e12011-02-03 14:02:06 -08004316 if (mir->dalvikInsn.opcode >= kMirOpFirst) {
4317 handleExtendedMIR(cUnit, mir);
4318 continue;
4319 }
4320
4321
4322 Opcode dalvikOpcode = mir->dalvikInsn.opcode;
4323 InstructionFormat dalvikFormat =
4324 dexGetFormatFromOpcode(dalvikOpcode);
4325 char *note;
4326 if (mir->OptimizationFlags & MIR_INLINED) {
4327 note = " (I)";
4328 } else if (mir->OptimizationFlags & MIR_INLINED_PRED) {
4329 note = " (PI)";
4330 } else if (mir->OptimizationFlags & MIR_CALLEE) {
4331 note = " (C)";
4332 } else {
4333 note = NULL;
4334 }
4335
4336 ArmLIR *boundaryLIR;
4337
4338 /*
4339 * Don't generate the boundary LIR unless we are debugging this
4340 * trace or we need a scheduling barrier.
4341 */
4342 if (headLIR == NULL || cUnit->printMe == true) {
4343 boundaryLIR =
4344 newLIR2(cUnit, kArmPseudoDalvikByteCodeBoundary,
4345 mir->offset,
4346 (int) dvmCompilerGetDalvikDisassembly(
4347 &mir->dalvikInsn, note));
4348 /* Remember the first LIR for this block */
4349 if (headLIR == NULL) {
4350 headLIR = boundaryLIR;
4351 /* Set the first boundaryLIR as a scheduling barrier */
4352 headLIR->defMask = ENCODE_ALL;
4353 }
4354 }
4355
4356 /*
4357 * Don't generate the SSA annotation unless verbose mode is on
4358 */
4359 if (cUnit->printMe && mir->ssaRep) {
4360 char *ssaString = dvmCompilerGetSSAString(cUnit,
4361 mir->ssaRep);
4362 newLIR1(cUnit, kArmPseudoSSARep, (int) ssaString);
4363 }
4364
4365 bool notHandled;
4366 /*
4367 * Debugging: screen the opcode first to see if it is in the
4368 * do[-not]-compile list
4369 */
4370 bool singleStepMe = SINGLE_STEP_OP(dalvikOpcode);
Ben Chengd5adae12010-03-26 17:45:28 -07004371#if defined(WITH_SELF_VERIFICATION)
Ben Cheng7ab74e12011-02-03 14:02:06 -08004372 if (singleStepMe == false) {
4373 singleStepMe = selfVerificationPuntOps(mir);
4374 }
Ben Chengd5adae12010-03-26 17:45:28 -07004375#endif
Ben Cheng7ab74e12011-02-03 14:02:06 -08004376 if (singleStepMe || cUnit->allSingleStep) {
4377 notHandled = false;
4378 genInterpSingleStep(cUnit, mir);
4379 } else {
4380 opcodeCoverage[dalvikOpcode]++;
4381 switch (dalvikFormat) {
4382 case kFmt10t:
4383 case kFmt20t:
4384 case kFmt30t:
4385 notHandled = handleFmt10t_Fmt20t_Fmt30t(cUnit,
4386 mir, bb, labelList);
4387 break;
4388 case kFmt10x:
4389 notHandled = handleFmt10x(cUnit, mir);
4390 break;
4391 case kFmt11n:
4392 case kFmt31i:
4393 notHandled = handleFmt11n_Fmt31i(cUnit, mir);
4394 break;
4395 case kFmt11x:
4396 notHandled = handleFmt11x(cUnit, mir);
4397 break;
4398 case kFmt12x:
4399 notHandled = handleFmt12x(cUnit, mir);
4400 break;
4401 case kFmt20bc:
4402 case kFmt40sc:
4403 notHandled = handleFmt20bc_Fmt40sc(cUnit, mir);
4404 break;
4405 case kFmt21c:
4406 case kFmt31c:
4407 case kFmt41c:
4408 notHandled = handleFmt21c_Fmt31c_Fmt41c(cUnit, mir);
4409 break;
4410 case kFmt21h:
4411 notHandled = handleFmt21h(cUnit, mir);
4412 break;
4413 case kFmt21s:
4414 notHandled = handleFmt21s(cUnit, mir);
4415 break;
4416 case kFmt21t:
4417 notHandled = handleFmt21t(cUnit, mir, bb,
Ben Chengba4fc8b2009-06-01 13:00:29 -07004418 labelList);
Ben Cheng7ab74e12011-02-03 14:02:06 -08004419 break;
4420 case kFmt22b:
4421 case kFmt22s:
4422 notHandled = handleFmt22b_Fmt22s(cUnit, mir);
4423 break;
4424 case kFmt22c:
4425 case kFmt52c:
4426 notHandled = handleFmt22c_Fmt52c(cUnit, mir);
4427 break;
4428 case kFmt22cs:
4429 notHandled = handleFmt22cs(cUnit, mir);
4430 break;
4431 case kFmt22t:
4432 notHandled = handleFmt22t(cUnit, mir, bb,
4433 labelList);
4434 break;
4435 case kFmt22x:
4436 case kFmt32x:
4437 notHandled = handleFmt22x_Fmt32x(cUnit, mir);
4438 break;
4439 case kFmt23x:
4440 notHandled = handleFmt23x(cUnit, mir);
4441 break;
4442 case kFmt31t:
4443 notHandled = handleFmt31t(cUnit, mir);
4444 break;
4445 case kFmt3rc:
4446 case kFmt35c:
4447 case kFmt5rc:
4448 notHandled = handleFmt35c_3rc_5rc(cUnit, mir, bb,
4449 labelList);
4450 break;
4451 case kFmt3rms:
4452 case kFmt35ms:
4453 notHandled = handleFmt35ms_3rms(cUnit, mir, bb,
4454 labelList);
4455 break;
4456 case kFmt35mi:
4457 case kFmt3rmi:
4458 notHandled = handleExecuteInline(cUnit, mir);
4459 break;
4460 case kFmt51l:
4461 notHandled = handleFmt51l(cUnit, mir);
4462 break;
4463 default:
4464 notHandled = true;
4465 break;
4466 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07004467 }
Ben Cheng7ab74e12011-02-03 14:02:06 -08004468 if (notHandled) {
4469 LOGE("%#06x: Opcode 0x%x (%s) / Fmt %d not handled\n",
4470 mir->offset,
4471 dalvikOpcode, dexGetOpcodeName(dalvikOpcode),
4472 dalvikFormat);
4473 dvmCompilerAbort(cUnit);
4474 break;
4475 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07004476 }
4477 }
Ben Cheng4238ec22009-08-24 16:32:22 -07004478
Ben Cheng00603072010-10-28 11:13:58 -07004479 if (bb->blockType == kTraceEntryBlock) {
Ben Cheng4238ec22009-08-24 16:32:22 -07004480 dvmCompilerAppendLIR(cUnit,
4481 (LIR *) cUnit->loopAnalysis->branchToBody);
4482 dvmCompilerAppendLIR(cUnit,
4483 (LIR *) cUnit->loopAnalysis->branchToPCR);
4484 }
4485
4486 if (headLIR) {
4487 /*
4488 * Eliminate redundant loads/stores and delay stores into later
4489 * slots
4490 */
4491 dvmCompilerApplyLocalOptimizations(cUnit, (LIR *) headLIR,
4492 cUnit->lastLIRInsn);
4493 }
4494
4495gen_fallthrough:
Ben Cheng1efc9c52009-06-08 18:25:27 -07004496 /*
4497 * Check if the block is terminated due to trace length constraint -
4498 * insert an unconditional branch to the chaining cell.
4499 */
Ben Cheng00603072010-10-28 11:13:58 -07004500 if (bb->needFallThroughBranch) {
Ben Cheng7ab74e12011-02-03 14:02:06 -08004501 genUnconditionalBranch(cUnit, &labelList[bb->fallThrough->id]);
Ben Cheng1efc9c52009-06-08 18:25:27 -07004502 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07004503 }
4504
Ben Chenge9695e52009-06-16 16:11:47 -07004505 /* Handle the chaining cells in predefined order */
Ben Chengcec26f62010-01-15 15:29:33 -08004506 for (i = 0; i < kChainingCellGap; i++) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07004507 size_t j;
4508 int *blockIdList = (int *) chainingListByType[i].elemList;
4509
4510 cUnit->numChainingCells[i] = chainingListByType[i].numUsed;
4511
4512 /* No chaining cells of this type */
4513 if (cUnit->numChainingCells[i] == 0)
4514 continue;
4515
4516 /* Record the first LIR for a new type of chaining cell */
4517 cUnit->firstChainingLIR[i] = (LIR *) &labelList[blockIdList[0]];
4518
4519 for (j = 0; j < chainingListByType[i].numUsed; j++) {
4520 int blockId = blockIdList[j];
Ben Cheng00603072010-10-28 11:13:58 -07004521 BasicBlock *chainingBlock =
4522 (BasicBlock *) dvmGrowableListGetElement(&cUnit->blockList,
4523 blockId);
Ben Chengba4fc8b2009-06-01 13:00:29 -07004524
4525 /* Align this chaining cell first */
Bill Buzbee1465db52009-09-23 17:17:35 -07004526 newLIR0(cUnit, kArmPseudoPseudoAlign4);
Ben Chengba4fc8b2009-06-01 13:00:29 -07004527
4528 /* Insert the pseudo chaining instruction */
4529 dvmCompilerAppendLIR(cUnit, (LIR *) &labelList[blockId]);
4530
4531
Ben Cheng00603072010-10-28 11:13:58 -07004532 switch (chainingBlock->blockType) {
Bill Buzbee1465db52009-09-23 17:17:35 -07004533 case kChainingCellNormal:
Ben Cheng00603072010-10-28 11:13:58 -07004534 handleNormalChainingCell(cUnit, chainingBlock->startOffset);
Ben Chengba4fc8b2009-06-01 13:00:29 -07004535 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07004536 case kChainingCellInvokeSingleton:
Ben Cheng38329f52009-07-07 14:19:20 -07004537 handleInvokeSingletonChainingCell(cUnit,
Ben Cheng00603072010-10-28 11:13:58 -07004538 chainingBlock->containingMethod);
Ben Chengba4fc8b2009-06-01 13:00:29 -07004539 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07004540 case kChainingCellInvokePredicted:
Ben Cheng38329f52009-07-07 14:19:20 -07004541 handleInvokePredictedChainingCell(cUnit);
4542 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07004543 case kChainingCellHot:
Ben Cheng00603072010-10-28 11:13:58 -07004544 handleHotChainingCell(cUnit, chainingBlock->startOffset);
Ben Chengba4fc8b2009-06-01 13:00:29 -07004545 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07004546 case kChainingCellBackwardBranch:
Jeff Hao97319a82009-08-12 16:57:15 -07004547 handleBackwardBranchChainingCell(cUnit,
Ben Cheng00603072010-10-28 11:13:58 -07004548 chainingBlock->startOffset);
Jeff Hao97319a82009-08-12 16:57:15 -07004549 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07004550 default:
Ben Cheng00603072010-10-28 11:13:58 -07004551 LOGE("Bad blocktype %d", chainingBlock->blockType);
Bill Buzbeefc519dc2010-03-06 23:30:57 -08004552 dvmCompilerAbort(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -07004553 }
4554 }
4555 }
Ben Chenge9695e52009-06-16 16:11:47 -07004556
Ben Chengcec26f62010-01-15 15:29:33 -08004557 /* Mark the bottom of chaining cells */
4558 cUnit->chainingCellBottom = (LIR *) newLIR0(cUnit, kArmChainingCellBottom);
4559
Ben Cheng6c10a972009-10-29 14:39:18 -07004560 /*
4561 * Generate the branch to the dvmJitToInterpNoChain entry point at the end
4562 * of all chaining cells for the overflow cases.
4563 */
4564 if (cUnit->switchOverflowPad) {
4565 loadConstant(cUnit, r0, (int) cUnit->switchOverflowPad);
Ben Cheng20d7e6c2011-02-18 17:12:42 -08004566 loadWordDisp(cUnit, r6SELF, offsetof(Thread,
Ben Cheng6c10a972009-10-29 14:39:18 -07004567 jitToInterpEntries.dvmJitToInterpNoChain), r2);
4568 opRegReg(cUnit, kOpAdd, r1, r1);
4569 opRegRegReg(cUnit, kOpAdd, r4PC, r0, r1);
Ben Cheng978738d2010-05-13 13:45:57 -07004570#if defined(WITH_JIT_TUNING)
Ben Cheng6c10a972009-10-29 14:39:18 -07004571 loadConstant(cUnit, r0, kSwitchOverflow);
4572#endif
4573 opReg(cUnit, kOpBlx, r2);
4574 }
4575
Ben Chenge9695e52009-06-16 16:11:47 -07004576 dvmCompilerApplyGlobalOptimizations(cUnit);
jeffhao9e45c0b2010-02-03 10:24:05 -08004577
4578#if defined(WITH_SELF_VERIFICATION)
4579 selfVerificationBranchInsertPass(cUnit);
4580#endif
Ben Chengba4fc8b2009-06-01 13:00:29 -07004581}
4582
buzbee2e152ba2010-12-15 16:32:35 -08004583/*
4584 * Accept the work and start compiling. Returns true if compilation
4585 * is attempted.
4586 */
Bill Buzbee716f1202009-07-23 13:22:09 -07004587bool dvmCompilerDoWork(CompilerWorkOrder *work)
Ben Chengba4fc8b2009-06-01 13:00:29 -07004588{
Carl Shapirofc75f3e2010-12-07 11:43:38 -08004589 JitTraceDescription *desc;
buzbee2e152ba2010-12-15 16:32:35 -08004590 bool isCompile;
4591 bool success = true;
Ben Chengba4fc8b2009-06-01 13:00:29 -07004592
Ben Cheng6999d842010-01-26 16:46:15 -08004593 if (gDvmJit.codeCacheFull) {
Ben Chengccd6c012009-10-15 14:52:45 -07004594 return false;
4595 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07004596
Ben Chengccd6c012009-10-15 14:52:45 -07004597 switch (work->kind) {
Ben Chengccd6c012009-10-15 14:52:45 -07004598 case kWorkOrderTrace:
buzbee2e152ba2010-12-15 16:32:35 -08004599 isCompile = true;
Ben Chengccd6c012009-10-15 14:52:45 -07004600 /* Start compilation with maximally allowed trace length */
Carl Shapirofc75f3e2010-12-07 11:43:38 -08004601 desc = (JitTraceDescription *)work->info;
buzbee2e152ba2010-12-15 16:32:35 -08004602 success = dvmCompileTrace(desc, JIT_MAX_TRACE_LEN, &work->result,
4603 work->bailPtr, 0 /* no hints */);
Ben Chengccd6c012009-10-15 14:52:45 -07004604 break;
4605 case kWorkOrderTraceDebug: {
4606 bool oldPrintMe = gDvmJit.printMe;
4607 gDvmJit.printMe = true;
buzbee2e152ba2010-12-15 16:32:35 -08004608 isCompile = true;
Ben Chengccd6c012009-10-15 14:52:45 -07004609 /* Start compilation with maximally allowed trace length */
Carl Shapirofc75f3e2010-12-07 11:43:38 -08004610 desc = (JitTraceDescription *)work->info;
buzbee2e152ba2010-12-15 16:32:35 -08004611 success = dvmCompileTrace(desc, JIT_MAX_TRACE_LEN, &work->result,
4612 work->bailPtr, 0 /* no hints */);
Elliott Hughes672511b2010-04-26 17:40:13 -07004613 gDvmJit.printMe = oldPrintMe;
Ben Chengccd6c012009-10-15 14:52:45 -07004614 break;
4615 }
buzbee2e152ba2010-12-15 16:32:35 -08004616 case kWorkOrderProfileMode:
4617 dvmJitChangeProfileMode((TraceProfilingModes)work->info);
4618 isCompile = false;
4619 break;
Ben Chengccd6c012009-10-15 14:52:45 -07004620 default:
buzbee2e152ba2010-12-15 16:32:35 -08004621 isCompile = false;
Bill Buzbeefc519dc2010-03-06 23:30:57 -08004622 LOGE("Jit: unknown work order type");
Elliott Hughes672511b2010-04-26 17:40:13 -07004623 assert(0); // Bail if debug build, discard otherwise
Ben Chengccd6c012009-10-15 14:52:45 -07004624 }
buzbee2e152ba2010-12-15 16:32:35 -08004625 if (!success)
4626 work->result.codeAddress = NULL;
4627 return isCompile;
Ben Chengba4fc8b2009-06-01 13:00:29 -07004628}
4629
Ben Chengba4fc8b2009-06-01 13:00:29 -07004630/* Architectural-specific debugging helpers go here */
4631void dvmCompilerArchDump(void)
4632{
4633 /* Print compiled opcode in this VM instance */
4634 int i, start, streak;
4635 char buf[1024];
4636
4637 streak = i = 0;
4638 buf[0] = 0;
Dan Bornsteinccaab182010-12-03 15:32:40 -08004639 while (opcodeCoverage[i] == 0 && i < kNumPackedOpcodes) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07004640 i++;
4641 }
Dan Bornsteinccaab182010-12-03 15:32:40 -08004642 if (i == kNumPackedOpcodes) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07004643 return;
4644 }
Dan Bornsteinccaab182010-12-03 15:32:40 -08004645 for (start = i++, streak = 1; i < kNumPackedOpcodes; i++) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07004646 if (opcodeCoverage[i]) {
4647 streak++;
4648 } else {
4649 if (streak == 1) {
4650 sprintf(buf+strlen(buf), "%x,", start);
4651 } else {
4652 sprintf(buf+strlen(buf), "%x-%x,", start, start + streak - 1);
4653 }
4654 streak = 0;
Dan Bornsteinccaab182010-12-03 15:32:40 -08004655 while (opcodeCoverage[i] == 0 && i < kNumPackedOpcodes) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07004656 i++;
4657 }
Dan Bornsteinccaab182010-12-03 15:32:40 -08004658 if (i < kNumPackedOpcodes) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07004659 streak = 1;
4660 start = i;
4661 }
4662 }
4663 }
4664 if (streak) {
4665 if (streak == 1) {
4666 sprintf(buf+strlen(buf), "%x", start);
4667 } else {
4668 sprintf(buf+strlen(buf), "%x-%x", start, start + streak - 1);
4669 }
4670 }
4671 if (strlen(buf)) {
Ben Cheng8b258bf2009-06-24 17:27:07 -07004672 LOGD("dalvik.vm.jit.op = %s", buf);
Ben Chengba4fc8b2009-06-01 13:00:29 -07004673 }
4674}
Ben Chengd7d426a2009-09-22 11:23:36 -07004675
4676/* Common initialization routine for an architecture family */
4677bool dvmCompilerArchInit()
4678{
4679 int i;
4680
Bill Buzbee1465db52009-09-23 17:17:35 -07004681 for (i = 0; i < kArmLast; i++) {
Dan Bornstein9a1f8162010-12-01 17:02:26 -08004682 if (EncodingMap[i].opcode != i) {
Ben Chengd7d426a2009-09-22 11:23:36 -07004683 LOGE("Encoding order for %s is wrong: expecting %d, seeing %d",
Dan Bornstein9a1f8162010-12-01 17:02:26 -08004684 EncodingMap[i].name, i, EncodingMap[i].opcode);
Bill Buzbeefc519dc2010-03-06 23:30:57 -08004685 dvmAbort(); // OK to dvmAbort - build error
Ben Chengd7d426a2009-09-22 11:23:36 -07004686 }
4687 }
4688
Ben Cheng5d90c202009-11-22 23:31:11 -08004689 return dvmCompilerArchVariantInit();
4690}
4691
4692void *dvmCompilerGetInterpretTemplate()
4693{
4694 return (void*) ((int)gDvmJit.codeCache +
4695 templateEntryOffsets[TEMPLATE_INTERPRET]);
4696}
4697
Bill Buzbee1b3da592011-02-03 07:38:22 -08004698JitInstructionSetType dvmCompilerGetInterpretTemplateSet()
4699{
4700 return DALVIK_JIT_ARM;
4701}
4702
buzbeebff121a2010-08-04 15:25:06 -07004703/* Needed by the Assembler */
4704void dvmCompilerSetupResourceMasks(ArmLIR *lir)
4705{
4706 setupResourceMasks(lir);
4707}
4708
Ben Cheng5d90c202009-11-22 23:31:11 -08004709/* Needed by the ld/st optmizatons */
4710ArmLIR* dvmCompilerRegCopyNoInsert(CompilationUnit *cUnit, int rDest, int rSrc)
4711{
4712 return genRegCopyNoInsert(cUnit, rDest, rSrc);
4713}
4714
4715/* Needed by the register allocator */
4716ArmLIR* dvmCompilerRegCopy(CompilationUnit *cUnit, int rDest, int rSrc)
4717{
4718 return genRegCopy(cUnit, rDest, rSrc);
4719}
4720
4721/* Needed by the register allocator */
4722void dvmCompilerRegCopyWide(CompilationUnit *cUnit, int destLo, int destHi,
4723 int srcLo, int srcHi)
4724{
4725 genRegCopyWide(cUnit, destLo, destHi, srcLo, srcHi);
4726}
4727
4728void dvmCompilerFlushRegImpl(CompilationUnit *cUnit, int rBase,
4729 int displacement, int rSrc, OpSize size)
4730{
4731 storeBaseDisp(cUnit, rBase, displacement, rSrc, size);
4732}
4733
4734void dvmCompilerFlushRegWideImpl(CompilationUnit *cUnit, int rBase,
4735 int displacement, int rSrcLo, int rSrcHi)
4736{
4737 storeBaseDispWide(cUnit, rBase, displacement, rSrcLo, rSrcHi);
Ben Chengd7d426a2009-09-22 11:23:36 -07004738}