blob: ade41977247982cab06c2b81d6991e08786c15ad [file] [log] [blame]
Ben Chengba4fc8b2009-06-01 13:00:29 -07001/*
2 * Copyright (C) 2009 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Bill Buzbee50a6bf22009-07-08 13:08:04 -070017/*
18 * This file contains codegen and support common to all supported
19 * ARM variants. It is included by:
20 *
21 * Codegen-$(TARGET_ARCH_VARIANT).c
22 *
23 * which combines this common code with specific support found in the
24 * applicable directory below this one.
25 */
26
buzbee919eb062010-07-12 12:59:22 -070027/*
28 * Mark garbage collection card. Skip if the value we're storing is null.
29 */
30static void markCard(CompilationUnit *cUnit, int valReg, int tgtAddrReg)
31{
32 int regCardBase = dvmCompilerAllocTemp(cUnit);
33 int regCardNo = dvmCompilerAllocTemp(cUnit);
buzbee8f8109a2010-08-31 10:16:35 -070034 ArmLIR *branchOver = genCmpImmBranch(cUnit, kArmCondEq, valReg, 0);
buzbee919eb062010-07-12 12:59:22 -070035 loadWordDisp(cUnit, rGLUE, offsetof(InterpState, cardTable),
36 regCardBase);
37 opRegRegImm(cUnit, kOpLsr, regCardNo, tgtAddrReg, GC_CARD_SHIFT);
38 storeBaseIndexed(cUnit, regCardBase, regCardNo, regCardBase, 0,
39 kUnsignedByte);
40 ArmLIR *target = newLIR0(cUnit, kArmPseudoTargetLabel);
41 target->defMask = ENCODE_ALL;
42 branchOver->generic.target = (LIR *)target;
buzbeebaf196a2010-08-04 10:13:15 -070043 dvmCompilerFreeTemp(cUnit, regCardBase);
44 dvmCompilerFreeTemp(cUnit, regCardNo);
buzbee919eb062010-07-12 12:59:22 -070045}
46
Ben Cheng5d90c202009-11-22 23:31:11 -080047static bool genConversionCall(CompilationUnit *cUnit, MIR *mir, void *funct,
48 int srcSize, int tgtSize)
49{
50 /*
51 * Don't optimize the register usage since it calls out to template
52 * functions
53 */
54 RegLocation rlSrc;
55 RegLocation rlDest;
Bill Buzbeec6f10662010-02-09 11:16:15 -080056 dvmCompilerFlushAllRegs(cUnit); /* Send everything to home location */
Ben Cheng5d90c202009-11-22 23:31:11 -080057 if (srcSize == 1) {
Bill Buzbeec6f10662010-02-09 11:16:15 -080058 rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
Ben Cheng5d90c202009-11-22 23:31:11 -080059 loadValueDirectFixed(cUnit, rlSrc, r0);
60 } else {
Bill Buzbeec6f10662010-02-09 11:16:15 -080061 rlSrc = dvmCompilerGetSrcWide(cUnit, mir, 0, 1);
Ben Cheng5d90c202009-11-22 23:31:11 -080062 loadValueDirectWideFixed(cUnit, rlSrc, r0, r1);
63 }
Ben Chengbd1326d2010-04-02 15:04:53 -070064 LOAD_FUNC_ADDR(cUnit, r2, (int)funct);
Ben Cheng5d90c202009-11-22 23:31:11 -080065 opReg(cUnit, kOpBlx, r2);
Elliott Hughes6a555132010-02-25 15:41:42 -080066 dvmCompilerClobberCallRegs(cUnit);
Ben Cheng5d90c202009-11-22 23:31:11 -080067 if (tgtSize == 1) {
68 RegLocation rlResult;
Bill Buzbeec6f10662010-02-09 11:16:15 -080069 rlDest = dvmCompilerGetDest(cUnit, mir, 0);
70 rlResult = dvmCompilerGetReturn(cUnit);
Ben Cheng5d90c202009-11-22 23:31:11 -080071 storeValue(cUnit, rlDest, rlResult);
72 } else {
73 RegLocation rlResult;
Bill Buzbeec6f10662010-02-09 11:16:15 -080074 rlDest = dvmCompilerGetDestWide(cUnit, mir, 0, 1);
75 rlResult = dvmCompilerGetReturnWide(cUnit);
Ben Cheng5d90c202009-11-22 23:31:11 -080076 storeValueWide(cUnit, rlDest, rlResult);
77 }
78 return false;
79}
Ben Chengba4fc8b2009-06-01 13:00:29 -070080
Ben Cheng5d90c202009-11-22 23:31:11 -080081static bool genArithOpFloatPortable(CompilationUnit *cUnit, MIR *mir,
82 RegLocation rlDest, RegLocation rlSrc1,
83 RegLocation rlSrc2)
84{
85 RegLocation rlResult;
86 void* funct;
87
Dan Bornstein9a1f8162010-12-01 17:02:26 -080088 switch (mir->dalvikInsn.opcode) {
Ben Cheng5d90c202009-11-22 23:31:11 -080089 case OP_ADD_FLOAT_2ADDR:
90 case OP_ADD_FLOAT:
91 funct = (void*) __aeabi_fadd;
92 break;
93 case OP_SUB_FLOAT_2ADDR:
94 case OP_SUB_FLOAT:
95 funct = (void*) __aeabi_fsub;
96 break;
97 case OP_DIV_FLOAT_2ADDR:
98 case OP_DIV_FLOAT:
99 funct = (void*) __aeabi_fdiv;
100 break;
101 case OP_MUL_FLOAT_2ADDR:
102 case OP_MUL_FLOAT:
103 funct = (void*) __aeabi_fmul;
104 break;
105 case OP_REM_FLOAT_2ADDR:
106 case OP_REM_FLOAT:
107 funct = (void*) fmodf;
108 break;
109 case OP_NEG_FLOAT: {
110 genNegFloat(cUnit, rlDest, rlSrc1);
111 return false;
112 }
113 default:
114 return true;
115 }
Bill Buzbeec6f10662010-02-09 11:16:15 -0800116 dvmCompilerFlushAllRegs(cUnit); /* Send everything to home location */
Ben Cheng5d90c202009-11-22 23:31:11 -0800117 loadValueDirectFixed(cUnit, rlSrc1, r0);
118 loadValueDirectFixed(cUnit, rlSrc2, r1);
Ben Chengbd1326d2010-04-02 15:04:53 -0700119 LOAD_FUNC_ADDR(cUnit, r2, (int)funct);
Ben Cheng5d90c202009-11-22 23:31:11 -0800120 opReg(cUnit, kOpBlx, r2);
Elliott Hughes6a555132010-02-25 15:41:42 -0800121 dvmCompilerClobberCallRegs(cUnit);
Bill Buzbeec6f10662010-02-09 11:16:15 -0800122 rlResult = dvmCompilerGetReturn(cUnit);
Ben Cheng5d90c202009-11-22 23:31:11 -0800123 storeValue(cUnit, rlDest, rlResult);
124 return false;
125}
126
127static bool genArithOpDoublePortable(CompilationUnit *cUnit, MIR *mir,
128 RegLocation rlDest, RegLocation rlSrc1,
129 RegLocation rlSrc2)
130{
131 RegLocation rlResult;
132 void* funct;
133
Dan Bornstein9a1f8162010-12-01 17:02:26 -0800134 switch (mir->dalvikInsn.opcode) {
Ben Cheng5d90c202009-11-22 23:31:11 -0800135 case OP_ADD_DOUBLE_2ADDR:
136 case OP_ADD_DOUBLE:
137 funct = (void*) __aeabi_dadd;
138 break;
139 case OP_SUB_DOUBLE_2ADDR:
140 case OP_SUB_DOUBLE:
141 funct = (void*) __aeabi_dsub;
142 break;
143 case OP_DIV_DOUBLE_2ADDR:
144 case OP_DIV_DOUBLE:
145 funct = (void*) __aeabi_ddiv;
146 break;
147 case OP_MUL_DOUBLE_2ADDR:
148 case OP_MUL_DOUBLE:
149 funct = (void*) __aeabi_dmul;
150 break;
151 case OP_REM_DOUBLE_2ADDR:
152 case OP_REM_DOUBLE:
153 funct = (void*) fmod;
154 break;
155 case OP_NEG_DOUBLE: {
156 genNegDouble(cUnit, rlDest, rlSrc1);
157 return false;
158 }
159 default:
160 return true;
161 }
Bill Buzbeec6f10662010-02-09 11:16:15 -0800162 dvmCompilerFlushAllRegs(cUnit); /* Send everything to home location */
Ben Chengbd1326d2010-04-02 15:04:53 -0700163 LOAD_FUNC_ADDR(cUnit, rlr, (int)funct);
Ben Cheng5d90c202009-11-22 23:31:11 -0800164 loadValueDirectWideFixed(cUnit, rlSrc1, r0, r1);
165 loadValueDirectWideFixed(cUnit, rlSrc2, r2, r3);
166 opReg(cUnit, kOpBlx, rlr);
Elliott Hughes6a555132010-02-25 15:41:42 -0800167 dvmCompilerClobberCallRegs(cUnit);
Bill Buzbeec6f10662010-02-09 11:16:15 -0800168 rlResult = dvmCompilerGetReturnWide(cUnit);
Ben Cheng5d90c202009-11-22 23:31:11 -0800169 storeValueWide(cUnit, rlDest, rlResult);
Ben Chengd72564c2011-02-08 17:09:25 -0800170#if defined(WITH_SELF_VERIFICATION)
171 cUnit->usesLinkRegister = true;
172#endif
Ben Cheng5d90c202009-11-22 23:31:11 -0800173 return false;
174}
175
176static bool genConversionPortable(CompilationUnit *cUnit, MIR *mir)
177{
Dan Bornstein9a1f8162010-12-01 17:02:26 -0800178 Opcode opcode = mir->dalvikInsn.opcode;
Ben Cheng5d90c202009-11-22 23:31:11 -0800179
Dan Bornstein9a1f8162010-12-01 17:02:26 -0800180 switch (opcode) {
Ben Cheng5d90c202009-11-22 23:31:11 -0800181 case OP_INT_TO_FLOAT:
182 return genConversionCall(cUnit, mir, (void*)__aeabi_i2f, 1, 1);
183 case OP_FLOAT_TO_INT:
184 return genConversionCall(cUnit, mir, (void*)__aeabi_f2iz, 1, 1);
185 case OP_DOUBLE_TO_FLOAT:
186 return genConversionCall(cUnit, mir, (void*)__aeabi_d2f, 2, 1);
187 case OP_FLOAT_TO_DOUBLE:
188 return genConversionCall(cUnit, mir, (void*)__aeabi_f2d, 1, 2);
189 case OP_INT_TO_DOUBLE:
190 return genConversionCall(cUnit, mir, (void*)__aeabi_i2d, 1, 2);
191 case OP_DOUBLE_TO_INT:
192 return genConversionCall(cUnit, mir, (void*)__aeabi_d2iz, 2, 1);
193 case OP_FLOAT_TO_LONG:
194 return genConversionCall(cUnit, mir, (void*)dvmJitf2l, 1, 2);
195 case OP_LONG_TO_FLOAT:
196 return genConversionCall(cUnit, mir, (void*)__aeabi_l2f, 2, 1);
197 case OP_DOUBLE_TO_LONG:
198 return genConversionCall(cUnit, mir, (void*)dvmJitd2l, 2, 2);
199 case OP_LONG_TO_DOUBLE:
200 return genConversionCall(cUnit, mir, (void*)__aeabi_l2d, 2, 2);
201 default:
202 return true;
203 }
204 return false;
205}
Ben Chengba4fc8b2009-06-01 13:00:29 -0700206
Jeff Hao97319a82009-08-12 16:57:15 -0700207#if defined(WITH_SELF_VERIFICATION)
Dan Bornstein9a1f8162010-12-01 17:02:26 -0800208static void selfVerificationBranchInsert(LIR *currentLIR, ArmOpcode opcode,
jeffhao9e45c0b2010-02-03 10:24:05 -0800209 int dest, int src1)
Jeff Hao97319a82009-08-12 16:57:15 -0700210{
Carl Shapirofc75f3e2010-12-07 11:43:38 -0800211 ArmLIR *insn = (ArmLIR *) dvmCompilerNew(sizeof(ArmLIR), true);
Dan Bornstein9a1f8162010-12-01 17:02:26 -0800212 insn->opcode = opcode;
jeffhao9e45c0b2010-02-03 10:24:05 -0800213 insn->operands[0] = dest;
214 insn->operands[1] = src1;
215 setupResourceMasks(insn);
216 dvmCompilerInsertLIRBefore(currentLIR, (LIR *) insn);
Jeff Hao97319a82009-08-12 16:57:15 -0700217}
218
Ben Chengd72564c2011-02-08 17:09:25 -0800219/*
220 * Example where r14 (LR) is preserved around a heap access under
221 * self-verification mode in Thumb2:
222 *
223 * D/dalvikvm( 1538): 0x59414c5e (0026): ldr r14, [rpc, #220] <-hoisted
224 * D/dalvikvm( 1538): 0x59414c62 (002a): mla r4, r0, r8, r4
225 * D/dalvikvm( 1538): 0x59414c66 (002e): adds r3, r4, r3
226 * D/dalvikvm( 1538): 0x59414c6a (0032): push <r5, r14> ---+
227 * D/dalvikvm( 1538): 0x59414c6c (0034): blx_1 0x5940f494 |
228 * D/dalvikvm( 1538): 0x59414c6e (0036): blx_2 see above <-MEM_OP_DECODE
229 * D/dalvikvm( 1538): 0x59414c70 (0038): ldr r10, [r9, #0] |
230 * D/dalvikvm( 1538): 0x59414c74 (003c): pop <r5, r14> ---+
231 * D/dalvikvm( 1538): 0x59414c78 (0040): mov r11, r10
232 * D/dalvikvm( 1538): 0x59414c7a (0042): asr r12, r11, #31
233 * D/dalvikvm( 1538): 0x59414c7e (0046): movs r0, r2
234 * D/dalvikvm( 1538): 0x59414c80 (0048): movs r1, r3
235 * D/dalvikvm( 1538): 0x59414c82 (004a): str r2, [r5, #16]
236 * D/dalvikvm( 1538): 0x59414c84 (004c): mov r2, r11
237 * D/dalvikvm( 1538): 0x59414c86 (004e): str r3, [r5, #20]
238 * D/dalvikvm( 1538): 0x59414c88 (0050): mov r3, r12
239 * D/dalvikvm( 1538): 0x59414c8a (0052): str r11, [r5, #24]
240 * D/dalvikvm( 1538): 0x59414c8e (0056): str r12, [r5, #28]
241 * D/dalvikvm( 1538): 0x59414c92 (005a): blx r14 <-use of LR
242 *
243 */
jeffhao9e45c0b2010-02-03 10:24:05 -0800244static void selfVerificationBranchInsertPass(CompilationUnit *cUnit)
Jeff Hao97319a82009-08-12 16:57:15 -0700245{
jeffhao9e45c0b2010-02-03 10:24:05 -0800246 ArmLIR *thisLIR;
Dan Bornstein9a1f8162010-12-01 17:02:26 -0800247 TemplateOpcode opcode = TEMPLATE_MEM_OP_DECODE;
Jeff Hao97319a82009-08-12 16:57:15 -0700248
jeffhao9e45c0b2010-02-03 10:24:05 -0800249 for (thisLIR = (ArmLIR *) cUnit->firstLIRInsn;
250 thisLIR != (ArmLIR *) cUnit->lastLIRInsn;
251 thisLIR = NEXT_LIR(thisLIR)) {
Ben Chengd72564c2011-02-08 17:09:25 -0800252 if (!thisLIR->flags.isNop && thisLIR->flags.insertWrapper) {
253 /*
254 * Push r5(FP) and r14(LR) onto stack. We need to make sure that
255 * SP is 8-byte aligned, and we use r5 as a temp to restore LR
256 * for Thumb-only target since LR cannot be directly accessed in
257 * Thumb mode. Another reason to choose r5 here is it is the Dalvik
258 * frame pointer and cannot be the target of the emulated heap
259 * load.
260 */
261 if (cUnit->usesLinkRegister) {
262 genSelfVerificationPreBranch(cUnit, thisLIR);
263 }
264
jeffhao9e45c0b2010-02-03 10:24:05 -0800265 /* Branch to mem op decode template */
266 selfVerificationBranchInsert((LIR *) thisLIR, kThumbBlx1,
Dan Bornstein9a1f8162010-12-01 17:02:26 -0800267 (int) gDvmJit.codeCache + templateEntryOffsets[opcode],
268 (int) gDvmJit.codeCache + templateEntryOffsets[opcode]);
jeffhao9e45c0b2010-02-03 10:24:05 -0800269 selfVerificationBranchInsert((LIR *) thisLIR, kThumbBlx2,
Dan Bornstein9a1f8162010-12-01 17:02:26 -0800270 (int) gDvmJit.codeCache + templateEntryOffsets[opcode],
271 (int) gDvmJit.codeCache + templateEntryOffsets[opcode]);
Ben Chengd72564c2011-02-08 17:09:25 -0800272
273 /* Restore LR */
274 if (cUnit->usesLinkRegister) {
275 genSelfVerificationPostBranch(cUnit, thisLIR);
276 }
Jeff Hao97319a82009-08-12 16:57:15 -0700277 }
278 }
Jeff Hao97319a82009-08-12 16:57:15 -0700279}
Jeff Hao97319a82009-08-12 16:57:15 -0700280#endif
281
Bill Buzbeebe6534f2010-03-12 16:01:35 -0800282/* Generate conditional branch instructions */
283static ArmLIR *genConditionalBranch(CompilationUnit *cUnit,
284 ArmConditionCode cond,
285 ArmLIR *target)
286{
287 ArmLIR *branch = opCondBranch(cUnit, cond);
288 branch->generic.target = (LIR *) target;
289 return branch;
290}
291
Ben Chengba4fc8b2009-06-01 13:00:29 -0700292/* Generate a unconditional branch to go to the interpreter */
Bill Buzbee89efc3d2009-07-28 11:22:22 -0700293static inline ArmLIR *genTrap(CompilationUnit *cUnit, int dOffset,
294 ArmLIR *pcrLabel)
Ben Chengba4fc8b2009-06-01 13:00:29 -0700295{
Bill Buzbee1465db52009-09-23 17:17:35 -0700296 ArmLIR *branch = opNone(cUnit, kOpUncondBr);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700297 return genCheckCommon(cUnit, dOffset, branch, pcrLabel);
298}
299
300/* Load a wide field from an object instance */
301static void genIGetWide(CompilationUnit *cUnit, MIR *mir, int fieldOffset)
302{
Bill Buzbeec6f10662010-02-09 11:16:15 -0800303 RegLocation rlObj = dvmCompilerGetSrc(cUnit, mir, 0);
304 RegLocation rlDest = dvmCompilerGetDestWide(cUnit, mir, 0, 1);
Bill Buzbee1465db52009-09-23 17:17:35 -0700305 RegLocation rlResult;
306 rlObj = loadValue(cUnit, rlObj, kCoreReg);
Bill Buzbeec6f10662010-02-09 11:16:15 -0800307 int regPtr = dvmCompilerAllocTemp(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700308
Bill Buzbee1465db52009-09-23 17:17:35 -0700309 assert(rlDest.wide);
Ben Chenge9695e52009-06-16 16:11:47 -0700310
Bill Buzbee1465db52009-09-23 17:17:35 -0700311 genNullCheck(cUnit, rlObj.sRegLow, rlObj.lowReg, mir->offset,
312 NULL);/* null object? */
313 opRegRegImm(cUnit, kOpAdd, regPtr, rlObj.lowReg, fieldOffset);
Bill Buzbeec6f10662010-02-09 11:16:15 -0800314 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kAnyReg, true);
Ben Cheng11d8f142010-03-24 15:24:19 -0700315
316 HEAP_ACCESS_SHADOW(true);
Bill Buzbee1465db52009-09-23 17:17:35 -0700317 loadPair(cUnit, regPtr, rlResult.lowReg, rlResult.highReg);
Ben Cheng11d8f142010-03-24 15:24:19 -0700318 HEAP_ACCESS_SHADOW(false);
319
Bill Buzbeec6f10662010-02-09 11:16:15 -0800320 dvmCompilerFreeTemp(cUnit, regPtr);
Bill Buzbee1465db52009-09-23 17:17:35 -0700321 storeValueWide(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700322}
323
324/* Store a wide field to an object instance */
325static void genIPutWide(CompilationUnit *cUnit, MIR *mir, int fieldOffset)
326{
Bill Buzbeec6f10662010-02-09 11:16:15 -0800327 RegLocation rlSrc = dvmCompilerGetSrcWide(cUnit, mir, 0, 1);
328 RegLocation rlObj = dvmCompilerGetSrc(cUnit, mir, 2);
Bill Buzbee1465db52009-09-23 17:17:35 -0700329 rlObj = loadValue(cUnit, rlObj, kCoreReg);
330 int regPtr;
331 rlSrc = loadValueWide(cUnit, rlSrc, kAnyReg);
332 genNullCheck(cUnit, rlObj.sRegLow, rlObj.lowReg, mir->offset,
333 NULL);/* null object? */
Bill Buzbeec6f10662010-02-09 11:16:15 -0800334 regPtr = dvmCompilerAllocTemp(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -0700335 opRegRegImm(cUnit, kOpAdd, regPtr, rlObj.lowReg, fieldOffset);
Ben Cheng11d8f142010-03-24 15:24:19 -0700336
337 HEAP_ACCESS_SHADOW(true);
Bill Buzbee1465db52009-09-23 17:17:35 -0700338 storePair(cUnit, regPtr, rlSrc.lowReg, rlSrc.highReg);
Ben Cheng11d8f142010-03-24 15:24:19 -0700339 HEAP_ACCESS_SHADOW(false);
340
Bill Buzbeec6f10662010-02-09 11:16:15 -0800341 dvmCompilerFreeTemp(cUnit, regPtr);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700342}
343
344/*
345 * Load a field from an object instance
346 *
Ben Chengba4fc8b2009-06-01 13:00:29 -0700347 */
Bill Buzbee270c1d62009-08-13 16:58:07 -0700348static void genIGet(CompilationUnit *cUnit, MIR *mir, OpSize size,
buzbeeecf8f6e2010-07-20 14:53:42 -0700349 int fieldOffset, bool isVolatile)
Ben Chengba4fc8b2009-06-01 13:00:29 -0700350{
Bill Buzbee1465db52009-09-23 17:17:35 -0700351 RegLocation rlResult;
Bill Buzbee749e8162010-07-07 06:55:56 -0700352 RegisterClass regClass = dvmCompilerRegClassBySize(size);
Bill Buzbeec6f10662010-02-09 11:16:15 -0800353 RegLocation rlObj = dvmCompilerGetSrc(cUnit, mir, 0);
354 RegLocation rlDest = dvmCompilerGetDest(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -0700355 rlObj = loadValue(cUnit, rlObj, kCoreReg);
Bill Buzbee749e8162010-07-07 06:55:56 -0700356 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, regClass, true);
Bill Buzbee1465db52009-09-23 17:17:35 -0700357 genNullCheck(cUnit, rlObj.sRegLow, rlObj.lowReg, mir->offset,
358 NULL);/* null object? */
Ben Cheng11d8f142010-03-24 15:24:19 -0700359
360 HEAP_ACCESS_SHADOW(true);
Ben Cheng5d90c202009-11-22 23:31:11 -0800361 loadBaseDisp(cUnit, mir, rlObj.lowReg, fieldOffset, rlResult.lowReg,
362 size, rlObj.sRegLow);
Ben Cheng11d8f142010-03-24 15:24:19 -0700363 HEAP_ACCESS_SHADOW(false);
buzbeeecf8f6e2010-07-20 14:53:42 -0700364 if (isVolatile) {
buzbee2ce33c92010-11-01 15:53:27 -0700365 dvmCompilerGenMemBarrier(cUnit, kSY);
buzbeeecf8f6e2010-07-20 14:53:42 -0700366 }
Ben Cheng11d8f142010-03-24 15:24:19 -0700367
Bill Buzbee1465db52009-09-23 17:17:35 -0700368 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700369}
370
371/*
372 * Store a field to an object instance
373 *
Ben Chengba4fc8b2009-06-01 13:00:29 -0700374 */
Bill Buzbee270c1d62009-08-13 16:58:07 -0700375static void genIPut(CompilationUnit *cUnit, MIR *mir, OpSize size,
buzbeeecf8f6e2010-07-20 14:53:42 -0700376 int fieldOffset, bool isObject, bool isVolatile)
Ben Chengba4fc8b2009-06-01 13:00:29 -0700377{
Bill Buzbee749e8162010-07-07 06:55:56 -0700378 RegisterClass regClass = dvmCompilerRegClassBySize(size);
Bill Buzbeec6f10662010-02-09 11:16:15 -0800379 RegLocation rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
380 RegLocation rlObj = dvmCompilerGetSrc(cUnit, mir, 1);
Bill Buzbee1465db52009-09-23 17:17:35 -0700381 rlObj = loadValue(cUnit, rlObj, kCoreReg);
Bill Buzbee749e8162010-07-07 06:55:56 -0700382 rlSrc = loadValue(cUnit, rlSrc, regClass);
Bill Buzbee1465db52009-09-23 17:17:35 -0700383 genNullCheck(cUnit, rlObj.sRegLow, rlObj.lowReg, mir->offset,
384 NULL);/* null object? */
Ben Cheng11d8f142010-03-24 15:24:19 -0700385
buzbeeecf8f6e2010-07-20 14:53:42 -0700386 if (isVolatile) {
buzbee2ce33c92010-11-01 15:53:27 -0700387 dvmCompilerGenMemBarrier(cUnit, kSY);
buzbeeecf8f6e2010-07-20 14:53:42 -0700388 }
Ben Cheng11d8f142010-03-24 15:24:19 -0700389 HEAP_ACCESS_SHADOW(true);
Bill Buzbee1465db52009-09-23 17:17:35 -0700390 storeBaseDisp(cUnit, rlObj.lowReg, fieldOffset, rlSrc.lowReg, size);
Ben Cheng11d8f142010-03-24 15:24:19 -0700391 HEAP_ACCESS_SHADOW(false);
buzbee919eb062010-07-12 12:59:22 -0700392 if (isObject) {
393 /* NOTE: marking card based on object head */
394 markCard(cUnit, rlSrc.lowReg, rlObj.lowReg);
395 }
Ben Chengba4fc8b2009-06-01 13:00:29 -0700396}
397
398
Ben Chengba4fc8b2009-06-01 13:00:29 -0700399/*
400 * Generate array load
Ben Chengba4fc8b2009-06-01 13:00:29 -0700401 */
Bill Buzbee270c1d62009-08-13 16:58:07 -0700402static void genArrayGet(CompilationUnit *cUnit, MIR *mir, OpSize size,
Bill Buzbee1465db52009-09-23 17:17:35 -0700403 RegLocation rlArray, RegLocation rlIndex,
404 RegLocation rlDest, int scale)
Ben Chengba4fc8b2009-06-01 13:00:29 -0700405{
Bill Buzbee749e8162010-07-07 06:55:56 -0700406 RegisterClass regClass = dvmCompilerRegClassBySize(size);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700407 int lenOffset = offsetof(ArrayObject, length);
408 int dataOffset = offsetof(ArrayObject, contents);
Bill Buzbee1465db52009-09-23 17:17:35 -0700409 RegLocation rlResult;
410 rlArray = loadValue(cUnit, rlArray, kCoreReg);
411 rlIndex = loadValue(cUnit, rlIndex, kCoreReg);
412 int regPtr;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700413
414 /* null object? */
Ben Cheng4238ec22009-08-24 16:32:22 -0700415 ArmLIR * pcrLabel = NULL;
416
417 if (!(mir->OptimizationFlags & MIR_IGNORE_NULL_CHECK)) {
Bill Buzbee1465db52009-09-23 17:17:35 -0700418 pcrLabel = genNullCheck(cUnit, rlArray.sRegLow,
419 rlArray.lowReg, mir->offset, NULL);
Ben Cheng4238ec22009-08-24 16:32:22 -0700420 }
421
Bill Buzbeec6f10662010-02-09 11:16:15 -0800422 regPtr = dvmCompilerAllocTemp(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -0700423
Ben Cheng4238ec22009-08-24 16:32:22 -0700424 if (!(mir->OptimizationFlags & MIR_IGNORE_RANGE_CHECK)) {
Bill Buzbeec6f10662010-02-09 11:16:15 -0800425 int regLen = dvmCompilerAllocTemp(cUnit);
Ben Cheng4238ec22009-08-24 16:32:22 -0700426 /* Get len */
Bill Buzbee1465db52009-09-23 17:17:35 -0700427 loadWordDisp(cUnit, rlArray.lowReg, lenOffset, regLen);
428 /* regPtr -> array data */
429 opRegRegImm(cUnit, kOpAdd, regPtr, rlArray.lowReg, dataOffset);
430 genBoundsCheck(cUnit, rlIndex.lowReg, regLen, mir->offset,
431 pcrLabel);
Bill Buzbeec6f10662010-02-09 11:16:15 -0800432 dvmCompilerFreeTemp(cUnit, regLen);
Ben Cheng4238ec22009-08-24 16:32:22 -0700433 } else {
Bill Buzbee1465db52009-09-23 17:17:35 -0700434 /* regPtr -> array data */
435 opRegRegImm(cUnit, kOpAdd, regPtr, rlArray.lowReg, dataOffset);
Ben Cheng4238ec22009-08-24 16:32:22 -0700436 }
Bill Buzbee1465db52009-09-23 17:17:35 -0700437 if ((size == kLong) || (size == kDouble)) {
438 if (scale) {
Bill Buzbeec6f10662010-02-09 11:16:15 -0800439 int rNewIndex = dvmCompilerAllocTemp(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -0700440 opRegRegImm(cUnit, kOpLsl, rNewIndex, rlIndex.lowReg, scale);
441 opRegReg(cUnit, kOpAdd, regPtr, rNewIndex);
Bill Buzbeec6f10662010-02-09 11:16:15 -0800442 dvmCompilerFreeTemp(cUnit, rNewIndex);
Bill Buzbee1465db52009-09-23 17:17:35 -0700443 } else {
444 opRegReg(cUnit, kOpAdd, regPtr, rlIndex.lowReg);
445 }
Bill Buzbee749e8162010-07-07 06:55:56 -0700446 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, regClass, true);
Ben Cheng11d8f142010-03-24 15:24:19 -0700447
448 HEAP_ACCESS_SHADOW(true);
Bill Buzbee1465db52009-09-23 17:17:35 -0700449 loadPair(cUnit, regPtr, rlResult.lowReg, rlResult.highReg);
Ben Cheng11d8f142010-03-24 15:24:19 -0700450 HEAP_ACCESS_SHADOW(false);
451
Bill Buzbeec6f10662010-02-09 11:16:15 -0800452 dvmCompilerFreeTemp(cUnit, regPtr);
Bill Buzbee1465db52009-09-23 17:17:35 -0700453 storeValueWide(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700454 } else {
Bill Buzbee749e8162010-07-07 06:55:56 -0700455 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, regClass, true);
Ben Cheng11d8f142010-03-24 15:24:19 -0700456
457 HEAP_ACCESS_SHADOW(true);
Bill Buzbee1465db52009-09-23 17:17:35 -0700458 loadBaseIndexed(cUnit, regPtr, rlIndex.lowReg, rlResult.lowReg,
459 scale, size);
Ben Cheng11d8f142010-03-24 15:24:19 -0700460 HEAP_ACCESS_SHADOW(false);
461
Bill Buzbeec6f10662010-02-09 11:16:15 -0800462 dvmCompilerFreeTemp(cUnit, regPtr);
Bill Buzbee1465db52009-09-23 17:17:35 -0700463 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700464 }
465}
466
Ben Chengba4fc8b2009-06-01 13:00:29 -0700467/*
468 * Generate array store
469 *
Ben Chengba4fc8b2009-06-01 13:00:29 -0700470 */
Bill Buzbee270c1d62009-08-13 16:58:07 -0700471static void genArrayPut(CompilationUnit *cUnit, MIR *mir, OpSize size,
Bill Buzbee1465db52009-09-23 17:17:35 -0700472 RegLocation rlArray, RegLocation rlIndex,
473 RegLocation rlSrc, int scale)
Ben Chengba4fc8b2009-06-01 13:00:29 -0700474{
Bill Buzbee749e8162010-07-07 06:55:56 -0700475 RegisterClass regClass = dvmCompilerRegClassBySize(size);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700476 int lenOffset = offsetof(ArrayObject, length);
477 int dataOffset = offsetof(ArrayObject, contents);
478
Bill Buzbee1465db52009-09-23 17:17:35 -0700479 int regPtr;
480 rlArray = loadValue(cUnit, rlArray, kCoreReg);
481 rlIndex = loadValue(cUnit, rlIndex, kCoreReg);
Ben Chenge9695e52009-06-16 16:11:47 -0700482
Bill Buzbeec6f10662010-02-09 11:16:15 -0800483 if (dvmCompilerIsTemp(cUnit, rlArray.lowReg)) {
484 dvmCompilerClobber(cUnit, rlArray.lowReg);
Bill Buzbee1465db52009-09-23 17:17:35 -0700485 regPtr = rlArray.lowReg;
486 } else {
Bill Buzbeec6f10662010-02-09 11:16:15 -0800487 regPtr = dvmCompilerAllocTemp(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -0700488 genRegCopy(cUnit, regPtr, rlArray.lowReg);
489 }
Ben Chenge9695e52009-06-16 16:11:47 -0700490
Ben Cheng1efc9c52009-06-08 18:25:27 -0700491 /* null object? */
Ben Cheng4238ec22009-08-24 16:32:22 -0700492 ArmLIR * pcrLabel = NULL;
493
494 if (!(mir->OptimizationFlags & MIR_IGNORE_NULL_CHECK)) {
Bill Buzbee1465db52009-09-23 17:17:35 -0700495 pcrLabel = genNullCheck(cUnit, rlArray.sRegLow, rlArray.lowReg,
496 mir->offset, NULL);
Ben Cheng4238ec22009-08-24 16:32:22 -0700497 }
498
499 if (!(mir->OptimizationFlags & MIR_IGNORE_RANGE_CHECK)) {
Bill Buzbeec6f10662010-02-09 11:16:15 -0800500 int regLen = dvmCompilerAllocTemp(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -0700501 //NOTE: max live temps(4) here.
Ben Cheng4238ec22009-08-24 16:32:22 -0700502 /* Get len */
Bill Buzbee1465db52009-09-23 17:17:35 -0700503 loadWordDisp(cUnit, rlArray.lowReg, lenOffset, regLen);
504 /* regPtr -> array data */
505 opRegImm(cUnit, kOpAdd, regPtr, dataOffset);
506 genBoundsCheck(cUnit, rlIndex.lowReg, regLen, mir->offset,
507 pcrLabel);
Bill Buzbeec6f10662010-02-09 11:16:15 -0800508 dvmCompilerFreeTemp(cUnit, regLen);
Ben Cheng4238ec22009-08-24 16:32:22 -0700509 } else {
Bill Buzbee1465db52009-09-23 17:17:35 -0700510 /* regPtr -> array data */
511 opRegImm(cUnit, kOpAdd, regPtr, dataOffset);
Ben Cheng4238ec22009-08-24 16:32:22 -0700512 }
Bill Buzbee1465db52009-09-23 17:17:35 -0700513 /* at this point, regPtr points to array, 2 live temps */
Bill Buzbee1465db52009-09-23 17:17:35 -0700514 if ((size == kLong) || (size == kDouble)) {
515 //TODO: need specific wide routine that can handle fp regs
516 if (scale) {
Bill Buzbeec6f10662010-02-09 11:16:15 -0800517 int rNewIndex = dvmCompilerAllocTemp(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -0700518 opRegRegImm(cUnit, kOpLsl, rNewIndex, rlIndex.lowReg, scale);
519 opRegReg(cUnit, kOpAdd, regPtr, rNewIndex);
Bill Buzbeec6f10662010-02-09 11:16:15 -0800520 dvmCompilerFreeTemp(cUnit, rNewIndex);
Bill Buzbee1465db52009-09-23 17:17:35 -0700521 } else {
522 opRegReg(cUnit, kOpAdd, regPtr, rlIndex.lowReg);
523 }
Bill Buzbee749e8162010-07-07 06:55:56 -0700524 rlSrc = loadValueWide(cUnit, rlSrc, regClass);
Ben Cheng11d8f142010-03-24 15:24:19 -0700525
526 HEAP_ACCESS_SHADOW(true);
Bill Buzbee1465db52009-09-23 17:17:35 -0700527 storePair(cUnit, regPtr, rlSrc.lowReg, rlSrc.highReg);
Ben Cheng11d8f142010-03-24 15:24:19 -0700528 HEAP_ACCESS_SHADOW(false);
529
Bill Buzbeec6f10662010-02-09 11:16:15 -0800530 dvmCompilerFreeTemp(cUnit, regPtr);
Bill Buzbee270c1d62009-08-13 16:58:07 -0700531 } else {
Bill Buzbee749e8162010-07-07 06:55:56 -0700532 rlSrc = loadValue(cUnit, rlSrc, regClass);
Ben Cheng11d8f142010-03-24 15:24:19 -0700533
534 HEAP_ACCESS_SHADOW(true);
Bill Buzbee1465db52009-09-23 17:17:35 -0700535 storeBaseIndexed(cUnit, regPtr, rlIndex.lowReg, rlSrc.lowReg,
536 scale, size);
Ben Cheng11d8f142010-03-24 15:24:19 -0700537 HEAP_ACCESS_SHADOW(false);
jeffhao9e45c0b2010-02-03 10:24:05 -0800538 }
Ben Chengba4fc8b2009-06-01 13:00:29 -0700539}
540
Bill Buzbeebe6534f2010-03-12 16:01:35 -0800541/*
542 * Generate array object store
543 * Must use explicit register allocation here because of
544 * call-out to dvmCanPutArrayElement
545 */
546static void genArrayObjectPut(CompilationUnit *cUnit, MIR *mir,
547 RegLocation rlArray, RegLocation rlIndex,
548 RegLocation rlSrc, int scale)
549{
550 int lenOffset = offsetof(ArrayObject, length);
551 int dataOffset = offsetof(ArrayObject, contents);
552
553 dvmCompilerFlushAllRegs(cUnit);
554
555 int regLen = r0;
556 int regPtr = r4PC; /* Preserved across call */
557 int regArray = r1;
558 int regIndex = r7; /* Preserved across call */
559
560 loadValueDirectFixed(cUnit, rlArray, regArray);
561 loadValueDirectFixed(cUnit, rlIndex, regIndex);
562
563 /* null object? */
564 ArmLIR * pcrLabel = NULL;
565
566 if (!(mir->OptimizationFlags & MIR_IGNORE_NULL_CHECK)) {
567 pcrLabel = genNullCheck(cUnit, rlArray.sRegLow, regArray,
568 mir->offset, NULL);
569 }
570
571 if (!(mir->OptimizationFlags & MIR_IGNORE_RANGE_CHECK)) {
572 /* Get len */
573 loadWordDisp(cUnit, regArray, lenOffset, regLen);
574 /* regPtr -> array data */
575 opRegRegImm(cUnit, kOpAdd, regPtr, regArray, dataOffset);
576 genBoundsCheck(cUnit, regIndex, regLen, mir->offset,
577 pcrLabel);
578 } else {
579 /* regPtr -> array data */
580 opRegRegImm(cUnit, kOpAdd, regPtr, regArray, dataOffset);
581 }
582
583 /* Get object to store */
584 loadValueDirectFixed(cUnit, rlSrc, r0);
Ben Chengbd1326d2010-04-02 15:04:53 -0700585 LOAD_FUNC_ADDR(cUnit, r2, (int)dvmCanPutArrayElement);
Bill Buzbeebe6534f2010-03-12 16:01:35 -0800586
587 /* Are we storing null? If so, avoid check */
buzbee8f8109a2010-08-31 10:16:35 -0700588 ArmLIR *branchOver = genCmpImmBranch(cUnit, kArmCondEq, r0, 0);
Bill Buzbeebe6534f2010-03-12 16:01:35 -0800589
590 /* Make sure the types are compatible */
591 loadWordDisp(cUnit, regArray, offsetof(Object, clazz), r1);
592 loadWordDisp(cUnit, r0, offsetof(Object, clazz), r0);
593 opReg(cUnit, kOpBlx, r2);
594 dvmCompilerClobberCallRegs(cUnit);
Bill Buzbee900a3af2010-03-16 12:41:43 -0700595
596 /*
597 * Using fixed registers here, and counting on r4 and r7 being
598 * preserved across the above call. Tell the register allocation
599 * utilities about the regs we are using directly
600 */
601 dvmCompilerLockTemp(cUnit, regPtr); // r4PC
602 dvmCompilerLockTemp(cUnit, regIndex); // r7
603 dvmCompilerLockTemp(cUnit, r0);
buzbee919eb062010-07-12 12:59:22 -0700604 dvmCompilerLockTemp(cUnit, r1);
Bill Buzbee900a3af2010-03-16 12:41:43 -0700605
Bill Buzbeebe6534f2010-03-12 16:01:35 -0800606 /* Bad? - roll back and re-execute if so */
607 genRegImmCheck(cUnit, kArmCondEq, r0, 0, mir->offset, pcrLabel);
608
buzbee919eb062010-07-12 12:59:22 -0700609 /* Resume here - must reload element & array, regPtr & index preserved */
Bill Buzbeebe6534f2010-03-12 16:01:35 -0800610 loadValueDirectFixed(cUnit, rlSrc, r0);
buzbee919eb062010-07-12 12:59:22 -0700611 loadValueDirectFixed(cUnit, rlArray, r1);
Bill Buzbeebe6534f2010-03-12 16:01:35 -0800612
613 ArmLIR *target = newLIR0(cUnit, kArmPseudoTargetLabel);
614 target->defMask = ENCODE_ALL;
615 branchOver->generic.target = (LIR *) target;
616
Ben Cheng11d8f142010-03-24 15:24:19 -0700617 HEAP_ACCESS_SHADOW(true);
Bill Buzbeebe6534f2010-03-12 16:01:35 -0800618 storeBaseIndexed(cUnit, regPtr, regIndex, r0,
619 scale, kWord);
Ben Cheng11d8f142010-03-24 15:24:19 -0700620 HEAP_ACCESS_SHADOW(false);
buzbee919eb062010-07-12 12:59:22 -0700621
buzbeebaf196a2010-08-04 10:13:15 -0700622 dvmCompilerFreeTemp(cUnit, regPtr);
623 dvmCompilerFreeTemp(cUnit, regIndex);
624
buzbee919eb062010-07-12 12:59:22 -0700625 /* NOTE: marking card here based on object head */
626 markCard(cUnit, r0, r1);
Bill Buzbeebe6534f2010-03-12 16:01:35 -0800627}
628
Ben Cheng5d90c202009-11-22 23:31:11 -0800629static bool genShiftOpLong(CompilationUnit *cUnit, MIR *mir,
630 RegLocation rlDest, RegLocation rlSrc1,
631 RegLocation rlShift)
Ben Chengba4fc8b2009-06-01 13:00:29 -0700632{
Ben Chenge9695e52009-06-16 16:11:47 -0700633 /*
634 * Don't mess with the regsiters here as there is a particular calling
635 * convention to the out-of-line handler.
636 */
Bill Buzbee1465db52009-09-23 17:17:35 -0700637 RegLocation rlResult;
638
639 loadValueDirectWideFixed(cUnit, rlSrc1, r0, r1);
640 loadValueDirect(cUnit, rlShift, r2);
Dan Bornstein9a1f8162010-12-01 17:02:26 -0800641 switch( mir->dalvikInsn.opcode) {
Ben Chenge9695e52009-06-16 16:11:47 -0700642 case OP_SHL_LONG:
643 case OP_SHL_LONG_2ADDR:
644 genDispatchToHandler(cUnit, TEMPLATE_SHL_LONG);
645 break;
646 case OP_SHR_LONG:
647 case OP_SHR_LONG_2ADDR:
648 genDispatchToHandler(cUnit, TEMPLATE_SHR_LONG);
649 break;
650 case OP_USHR_LONG:
651 case OP_USHR_LONG_2ADDR:
652 genDispatchToHandler(cUnit, TEMPLATE_USHR_LONG);
653 break;
654 default:
655 return true;
656 }
Bill Buzbeec6f10662010-02-09 11:16:15 -0800657 rlResult = dvmCompilerGetReturnWide(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -0700658 storeValueWide(cUnit, rlDest, rlResult);
Ben Chenge9695e52009-06-16 16:11:47 -0700659 return false;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700660}
Ben Chenge9695e52009-06-16 16:11:47 -0700661
Ben Cheng5d90c202009-11-22 23:31:11 -0800662static bool genArithOpLong(CompilationUnit *cUnit, MIR *mir,
663 RegLocation rlDest, RegLocation rlSrc1,
664 RegLocation rlSrc2)
Ben Chengba4fc8b2009-06-01 13:00:29 -0700665{
Bill Buzbee1465db52009-09-23 17:17:35 -0700666 RegLocation rlResult;
667 OpKind firstOp = kOpBkpt;
668 OpKind secondOp = kOpBkpt;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700669 bool callOut = false;
670 void *callTgt;
671 int retReg = r0;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700672
Dan Bornstein9a1f8162010-12-01 17:02:26 -0800673 switch (mir->dalvikInsn.opcode) {
Ben Chengba4fc8b2009-06-01 13:00:29 -0700674 case OP_NOT_LONG:
Bill Buzbee1465db52009-09-23 17:17:35 -0700675 rlSrc2 = loadValueWide(cUnit, rlSrc2, kCoreReg);
Bill Buzbeec6f10662010-02-09 11:16:15 -0800676 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Bill Buzbee1465db52009-09-23 17:17:35 -0700677 opRegReg(cUnit, kOpMvn, rlResult.lowReg, rlSrc2.lowReg);
678 opRegReg(cUnit, kOpMvn, rlResult.highReg, rlSrc2.highReg);
679 storeValueWide(cUnit, rlDest, rlResult);
680 return false;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700681 break;
682 case OP_ADD_LONG:
683 case OP_ADD_LONG_2ADDR:
Bill Buzbee1465db52009-09-23 17:17:35 -0700684 firstOp = kOpAdd;
685 secondOp = kOpAdc;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700686 break;
687 case OP_SUB_LONG:
688 case OP_SUB_LONG_2ADDR:
Bill Buzbee1465db52009-09-23 17:17:35 -0700689 firstOp = kOpSub;
690 secondOp = kOpSbc;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700691 break;
692 case OP_MUL_LONG:
693 case OP_MUL_LONG_2ADDR:
Bill Buzbee1465db52009-09-23 17:17:35 -0700694 genMulLong(cUnit, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700695 return false;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700696 case OP_DIV_LONG:
697 case OP_DIV_LONG_2ADDR:
698 callOut = true;
699 retReg = r0;
700 callTgt = (void*)__aeabi_ldivmod;
701 break;
702 /* NOTE - result is in r2/r3 instead of r0/r1 */
703 case OP_REM_LONG:
704 case OP_REM_LONG_2ADDR:
705 callOut = true;
706 callTgt = (void*)__aeabi_ldivmod;
707 retReg = r2;
708 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700709 case OP_AND_LONG_2ADDR:
Bill Buzbee1465db52009-09-23 17:17:35 -0700710 case OP_AND_LONG:
711 firstOp = kOpAnd;
712 secondOp = kOpAnd;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700713 break;
714 case OP_OR_LONG:
715 case OP_OR_LONG_2ADDR:
Bill Buzbee1465db52009-09-23 17:17:35 -0700716 firstOp = kOpOr;
717 secondOp = kOpOr;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700718 break;
719 case OP_XOR_LONG:
720 case OP_XOR_LONG_2ADDR:
Bill Buzbee1465db52009-09-23 17:17:35 -0700721 firstOp = kOpXor;
722 secondOp = kOpXor;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700723 break;
Ben Chenge9695e52009-06-16 16:11:47 -0700724 case OP_NEG_LONG: {
Bill Buzbee51ecf602010-01-14 14:27:52 -0800725 //TUNING: can improve this using Thumb2 code
Bill Buzbeec6f10662010-02-09 11:16:15 -0800726 int tReg = dvmCompilerAllocTemp(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -0700727 rlSrc2 = loadValueWide(cUnit, rlSrc2, kCoreReg);
Bill Buzbeec6f10662010-02-09 11:16:15 -0800728 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Ben Chengbd1326d2010-04-02 15:04:53 -0700729 loadConstantNoClobber(cUnit, tReg, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -0700730 opRegRegReg(cUnit, kOpSub, rlResult.lowReg,
Bill Buzbee51ecf602010-01-14 14:27:52 -0800731 tReg, rlSrc2.lowReg);
732 opRegReg(cUnit, kOpSbc, tReg, rlSrc2.highReg);
733 genRegCopy(cUnit, rlResult.highReg, tReg);
Bill Buzbee1465db52009-09-23 17:17:35 -0700734 storeValueWide(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700735 return false;
Ben Chenge9695e52009-06-16 16:11:47 -0700736 }
Ben Chengba4fc8b2009-06-01 13:00:29 -0700737 default:
738 LOGE("Invalid long arith op");
Bill Buzbeefc519dc2010-03-06 23:30:57 -0800739 dvmCompilerAbort(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700740 }
741 if (!callOut) {
Bill Buzbee80cef862010-03-25 10:38:34 -0700742 genLong3Addr(cUnit, mir, firstOp, secondOp, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700743 } else {
Bill Buzbee1465db52009-09-23 17:17:35 -0700744 // Adjust return regs in to handle case of rem returning r2/r3
Bill Buzbeec6f10662010-02-09 11:16:15 -0800745 dvmCompilerFlushAllRegs(cUnit); /* Send everything to home location */
Bill Buzbee1465db52009-09-23 17:17:35 -0700746 loadValueDirectWideFixed(cUnit, rlSrc1, r0, r1);
Ben Chengbd1326d2010-04-02 15:04:53 -0700747 LOAD_FUNC_ADDR(cUnit, rlr, (int) callTgt);
Bill Buzbee1465db52009-09-23 17:17:35 -0700748 loadValueDirectWideFixed(cUnit, rlSrc2, r2, r3);
749 opReg(cUnit, kOpBlx, rlr);
Elliott Hughes6a555132010-02-25 15:41:42 -0800750 dvmCompilerClobberCallRegs(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -0700751 if (retReg == r0)
Bill Buzbeec6f10662010-02-09 11:16:15 -0800752 rlResult = dvmCompilerGetReturnWide(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -0700753 else
Bill Buzbeec6f10662010-02-09 11:16:15 -0800754 rlResult = dvmCompilerGetReturnWideAlt(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -0700755 storeValueWide(cUnit, rlDest, rlResult);
Ben Chengd72564c2011-02-08 17:09:25 -0800756#if defined(WITH_SELF_VERIFICATION)
757 cUnit->usesLinkRegister = true;
758#endif
Ben Chengba4fc8b2009-06-01 13:00:29 -0700759 }
760 return false;
761}
762
Ben Cheng5d90c202009-11-22 23:31:11 -0800763static bool genArithOpInt(CompilationUnit *cUnit, MIR *mir,
764 RegLocation rlDest, RegLocation rlSrc1,
765 RegLocation rlSrc2)
Ben Chengba4fc8b2009-06-01 13:00:29 -0700766{
Bill Buzbee1465db52009-09-23 17:17:35 -0700767 OpKind op = kOpBkpt;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700768 bool callOut = false;
769 bool checkZero = false;
Bill Buzbee1465db52009-09-23 17:17:35 -0700770 bool unary = false;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700771 int retReg = r0;
772 void *callTgt;
Bill Buzbee1465db52009-09-23 17:17:35 -0700773 RegLocation rlResult;
Bill Buzbee0e605272009-12-01 14:28:05 -0800774 bool shiftOp = false;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700775
Dan Bornstein9a1f8162010-12-01 17:02:26 -0800776 switch (mir->dalvikInsn.opcode) {
Ben Chengba4fc8b2009-06-01 13:00:29 -0700777 case OP_NEG_INT:
Bill Buzbee1465db52009-09-23 17:17:35 -0700778 op = kOpNeg;
779 unary = true;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700780 break;
781 case OP_NOT_INT:
Bill Buzbee1465db52009-09-23 17:17:35 -0700782 op = kOpMvn;
783 unary = true;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700784 break;
785 case OP_ADD_INT:
786 case OP_ADD_INT_2ADDR:
Bill Buzbee1465db52009-09-23 17:17:35 -0700787 op = kOpAdd;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700788 break;
789 case OP_SUB_INT:
790 case OP_SUB_INT_2ADDR:
Bill Buzbee1465db52009-09-23 17:17:35 -0700791 op = kOpSub;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700792 break;
793 case OP_MUL_INT:
794 case OP_MUL_INT_2ADDR:
Bill Buzbee1465db52009-09-23 17:17:35 -0700795 op = kOpMul;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700796 break;
797 case OP_DIV_INT:
798 case OP_DIV_INT_2ADDR:
799 callOut = true;
800 checkZero = true;
801 callTgt = __aeabi_idiv;
802 retReg = r0;
803 break;
804 /* NOTE: returns in r1 */
805 case OP_REM_INT:
806 case OP_REM_INT_2ADDR:
807 callOut = true;
808 checkZero = true;
809 callTgt = __aeabi_idivmod;
810 retReg = r1;
811 break;
812 case OP_AND_INT:
813 case OP_AND_INT_2ADDR:
Bill Buzbee1465db52009-09-23 17:17:35 -0700814 op = kOpAnd;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700815 break;
816 case OP_OR_INT:
817 case OP_OR_INT_2ADDR:
Bill Buzbee1465db52009-09-23 17:17:35 -0700818 op = kOpOr;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700819 break;
820 case OP_XOR_INT:
821 case OP_XOR_INT_2ADDR:
Bill Buzbee1465db52009-09-23 17:17:35 -0700822 op = kOpXor;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700823 break;
824 case OP_SHL_INT:
825 case OP_SHL_INT_2ADDR:
Bill Buzbee0e605272009-12-01 14:28:05 -0800826 shiftOp = true;
Bill Buzbee1465db52009-09-23 17:17:35 -0700827 op = kOpLsl;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700828 break;
829 case OP_SHR_INT:
830 case OP_SHR_INT_2ADDR:
Bill Buzbee0e605272009-12-01 14:28:05 -0800831 shiftOp = true;
Bill Buzbee1465db52009-09-23 17:17:35 -0700832 op = kOpAsr;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700833 break;
834 case OP_USHR_INT:
835 case OP_USHR_INT_2ADDR:
Bill Buzbee0e605272009-12-01 14:28:05 -0800836 shiftOp = true;
Bill Buzbee1465db52009-09-23 17:17:35 -0700837 op = kOpLsr;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700838 break;
839 default:
840 LOGE("Invalid word arith op: 0x%x(%d)",
Dan Bornstein9a1f8162010-12-01 17:02:26 -0800841 mir->dalvikInsn.opcode, mir->dalvikInsn.opcode);
Bill Buzbeefc519dc2010-03-06 23:30:57 -0800842 dvmCompilerAbort(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700843 }
844 if (!callOut) {
Bill Buzbee1465db52009-09-23 17:17:35 -0700845 rlSrc1 = loadValue(cUnit, rlSrc1, kCoreReg);
846 if (unary) {
Bill Buzbeec6f10662010-02-09 11:16:15 -0800847 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Bill Buzbee1465db52009-09-23 17:17:35 -0700848 opRegReg(cUnit, op, rlResult.lowReg,
849 rlSrc1.lowReg);
Ben Chenge9695e52009-06-16 16:11:47 -0700850 } else {
Bill Buzbee1465db52009-09-23 17:17:35 -0700851 rlSrc2 = loadValue(cUnit, rlSrc2, kCoreReg);
Bill Buzbee0e605272009-12-01 14:28:05 -0800852 if (shiftOp) {
Bill Buzbeec6f10662010-02-09 11:16:15 -0800853 int tReg = dvmCompilerAllocTemp(cUnit);
Bill Buzbee0e605272009-12-01 14:28:05 -0800854 opRegRegImm(cUnit, kOpAnd, tReg, rlSrc2.lowReg, 31);
Bill Buzbeec6f10662010-02-09 11:16:15 -0800855 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Bill Buzbee0e605272009-12-01 14:28:05 -0800856 opRegRegReg(cUnit, op, rlResult.lowReg,
857 rlSrc1.lowReg, tReg);
Bill Buzbeec6f10662010-02-09 11:16:15 -0800858 dvmCompilerFreeTemp(cUnit, tReg);
Bill Buzbee0e605272009-12-01 14:28:05 -0800859 } else {
Bill Buzbeec6f10662010-02-09 11:16:15 -0800860 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Bill Buzbee0e605272009-12-01 14:28:05 -0800861 opRegRegReg(cUnit, op, rlResult.lowReg,
862 rlSrc1.lowReg, rlSrc2.lowReg);
863 }
Ben Chenge9695e52009-06-16 16:11:47 -0700864 }
Bill Buzbee1465db52009-09-23 17:17:35 -0700865 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700866 } else {
Bill Buzbee1465db52009-09-23 17:17:35 -0700867 RegLocation rlResult;
Bill Buzbeec6f10662010-02-09 11:16:15 -0800868 dvmCompilerFlushAllRegs(cUnit); /* Send everything to home location */
Bill Buzbee1465db52009-09-23 17:17:35 -0700869 loadValueDirectFixed(cUnit, rlSrc2, r1);
Ben Chengbd1326d2010-04-02 15:04:53 -0700870 LOAD_FUNC_ADDR(cUnit, r2, (int) callTgt);
Bill Buzbee1465db52009-09-23 17:17:35 -0700871 loadValueDirectFixed(cUnit, rlSrc1, r0);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700872 if (checkZero) {
Bill Buzbee1465db52009-09-23 17:17:35 -0700873 genNullCheck(cUnit, rlSrc2.sRegLow, r1, mir->offset, NULL);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700874 }
Bill Buzbee1465db52009-09-23 17:17:35 -0700875 opReg(cUnit, kOpBlx, r2);
Elliott Hughes6a555132010-02-25 15:41:42 -0800876 dvmCompilerClobberCallRegs(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -0700877 if (retReg == r0)
Bill Buzbeec6f10662010-02-09 11:16:15 -0800878 rlResult = dvmCompilerGetReturn(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -0700879 else
Bill Buzbeec6f10662010-02-09 11:16:15 -0800880 rlResult = dvmCompilerGetReturnAlt(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -0700881 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700882 }
883 return false;
884}
885
Ben Cheng5d90c202009-11-22 23:31:11 -0800886static bool genArithOp(CompilationUnit *cUnit, MIR *mir)
Ben Chengba4fc8b2009-06-01 13:00:29 -0700887{
Dan Bornstein9a1f8162010-12-01 17:02:26 -0800888 Opcode opcode = mir->dalvikInsn.opcode;
Bill Buzbee1465db52009-09-23 17:17:35 -0700889 RegLocation rlDest;
890 RegLocation rlSrc1;
891 RegLocation rlSrc2;
892 /* Deduce sizes of operands */
893 if (mir->ssaRep->numUses == 2) {
Bill Buzbeec6f10662010-02-09 11:16:15 -0800894 rlSrc1 = dvmCompilerGetSrc(cUnit, mir, 0);
895 rlSrc2 = dvmCompilerGetSrc(cUnit, mir, 1);
Bill Buzbee1465db52009-09-23 17:17:35 -0700896 } else if (mir->ssaRep->numUses == 3) {
Bill Buzbeec6f10662010-02-09 11:16:15 -0800897 rlSrc1 = dvmCompilerGetSrcWide(cUnit, mir, 0, 1);
898 rlSrc2 = dvmCompilerGetSrc(cUnit, mir, 2);
Bill Buzbee1465db52009-09-23 17:17:35 -0700899 } else {
Bill Buzbeec6f10662010-02-09 11:16:15 -0800900 rlSrc1 = dvmCompilerGetSrcWide(cUnit, mir, 0, 1);
901 rlSrc2 = dvmCompilerGetSrcWide(cUnit, mir, 2, 3);
Bill Buzbee1465db52009-09-23 17:17:35 -0700902 assert(mir->ssaRep->numUses == 4);
903 }
904 if (mir->ssaRep->numDefs == 1) {
Bill Buzbeec6f10662010-02-09 11:16:15 -0800905 rlDest = dvmCompilerGetDest(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -0700906 } else {
907 assert(mir->ssaRep->numDefs == 2);
Bill Buzbeec6f10662010-02-09 11:16:15 -0800908 rlDest = dvmCompilerGetDestWide(cUnit, mir, 0, 1);
Bill Buzbee1465db52009-09-23 17:17:35 -0700909 }
Ben Chengba4fc8b2009-06-01 13:00:29 -0700910
Dan Bornstein9a1f8162010-12-01 17:02:26 -0800911 if ((opcode >= OP_ADD_LONG_2ADDR) && (opcode <= OP_XOR_LONG_2ADDR)) {
Ben Cheng5d90c202009-11-22 23:31:11 -0800912 return genArithOpLong(cUnit,mir, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700913 }
Dan Bornstein9a1f8162010-12-01 17:02:26 -0800914 if ((opcode >= OP_ADD_LONG) && (opcode <= OP_XOR_LONG)) {
Ben Cheng5d90c202009-11-22 23:31:11 -0800915 return genArithOpLong(cUnit,mir, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700916 }
Dan Bornstein9a1f8162010-12-01 17:02:26 -0800917 if ((opcode >= OP_SHL_LONG_2ADDR) && (opcode <= OP_USHR_LONG_2ADDR)) {
Ben Cheng5d90c202009-11-22 23:31:11 -0800918 return genShiftOpLong(cUnit,mir, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700919 }
Dan Bornstein9a1f8162010-12-01 17:02:26 -0800920 if ((opcode >= OP_SHL_LONG) && (opcode <= OP_USHR_LONG)) {
Ben Cheng5d90c202009-11-22 23:31:11 -0800921 return genShiftOpLong(cUnit,mir, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700922 }
Dan Bornstein9a1f8162010-12-01 17:02:26 -0800923 if ((opcode >= OP_ADD_INT_2ADDR) && (opcode <= OP_USHR_INT_2ADDR)) {
Ben Cheng5d90c202009-11-22 23:31:11 -0800924 return genArithOpInt(cUnit,mir, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700925 }
Dan Bornstein9a1f8162010-12-01 17:02:26 -0800926 if ((opcode >= OP_ADD_INT) && (opcode <= OP_USHR_INT)) {
Ben Cheng5d90c202009-11-22 23:31:11 -0800927 return genArithOpInt(cUnit,mir, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700928 }
Dan Bornstein9a1f8162010-12-01 17:02:26 -0800929 if ((opcode >= OP_ADD_FLOAT_2ADDR) && (opcode <= OP_REM_FLOAT_2ADDR)) {
Ben Cheng5d90c202009-11-22 23:31:11 -0800930 return genArithOpFloat(cUnit,mir, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700931 }
Dan Bornstein9a1f8162010-12-01 17:02:26 -0800932 if ((opcode >= OP_ADD_FLOAT) && (opcode <= OP_REM_FLOAT)) {
Ben Cheng5d90c202009-11-22 23:31:11 -0800933 return genArithOpFloat(cUnit, mir, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700934 }
Dan Bornstein9a1f8162010-12-01 17:02:26 -0800935 if ((opcode >= OP_ADD_DOUBLE_2ADDR) && (opcode <= OP_REM_DOUBLE_2ADDR)) {
Ben Cheng5d90c202009-11-22 23:31:11 -0800936 return genArithOpDouble(cUnit,mir, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700937 }
Dan Bornstein9a1f8162010-12-01 17:02:26 -0800938 if ((opcode >= OP_ADD_DOUBLE) && (opcode <= OP_REM_DOUBLE)) {
Ben Cheng5d90c202009-11-22 23:31:11 -0800939 return genArithOpDouble(cUnit,mir, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700940 }
941 return true;
942}
943
Bill Buzbee1465db52009-09-23 17:17:35 -0700944/* Generate unconditional branch instructions */
945static ArmLIR *genUnconditionalBranch(CompilationUnit *cUnit, ArmLIR *target)
946{
947 ArmLIR *branch = opNone(cUnit, kOpUncondBr);
948 branch->generic.target = (LIR *) target;
949 return branch;
950}
951
Bill Buzbee1465db52009-09-23 17:17:35 -0700952/* Perform the actual operation for OP_RETURN_* */
953static void genReturnCommon(CompilationUnit *cUnit, MIR *mir)
954{
Ben Chengcfdeca32011-01-14 11:36:46 -0800955 if (!cUnit->methodJitMode) {
956 genDispatchToHandler(cUnit, gDvmJit.methodTraceSupport ?
957 TEMPLATE_RETURN_PROF :
958 TEMPLATE_RETURN);
Ben Cheng978738d2010-05-13 13:45:57 -0700959#if defined(WITH_JIT_TUNING)
Ben Chengcfdeca32011-01-14 11:36:46 -0800960 gDvmJit.returnOp++;
Bill Buzbee1465db52009-09-23 17:17:35 -0700961#endif
Ben Chengcfdeca32011-01-14 11:36:46 -0800962 int dPC = (int) (cUnit->method->insns + mir->offset);
963 /* Insert branch, but defer setting of target */
964 ArmLIR *branch = genUnconditionalBranch(cUnit, NULL);
965 /* Set up the place holder to reconstruct this Dalvik PC */
966 ArmLIR *pcrLabel = (ArmLIR *) dvmCompilerNew(sizeof(ArmLIR), true);
967 pcrLabel->opcode = kArmPseudoPCReconstructionCell;
968 pcrLabel->operands[0] = dPC;
969 pcrLabel->operands[1] = mir->offset;
970 /* Insert the place holder to the growable list */
971 dvmInsertGrowableList(&cUnit->pcReconstructionList,
972 (intptr_t) pcrLabel);
973 /* Branch to the PC reconstruction code */
974 branch->generic.target = (LIR *) pcrLabel;
975 }
976 /* TODO: Move result to InterpState for non-void returns */
Bill Buzbee1465db52009-09-23 17:17:35 -0700977}
978
Ben Chengba4fc8b2009-06-01 13:00:29 -0700979static void genProcessArgsNoRange(CompilationUnit *cUnit, MIR *mir,
980 DecodedInstruction *dInsn,
Bill Buzbee89efc3d2009-07-28 11:22:22 -0700981 ArmLIR **pcrLabel)
Ben Chengba4fc8b2009-06-01 13:00:29 -0700982{
983 unsigned int i;
984 unsigned int regMask = 0;
Bill Buzbee1465db52009-09-23 17:17:35 -0700985 RegLocation rlArg;
986 int numDone = 0;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700987
Bill Buzbee1465db52009-09-23 17:17:35 -0700988 /*
989 * Load arguments to r0..r4. Note that these registers may contain
990 * live values, so we clobber them immediately after loading to prevent
991 * them from being used as sources for subsequent loads.
992 */
Bill Buzbeec6f10662010-02-09 11:16:15 -0800993 dvmCompilerLockAllTemps(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700994 for (i = 0; i < dInsn->vA; i++) {
995 regMask |= 1 << i;
Bill Buzbeec6f10662010-02-09 11:16:15 -0800996 rlArg = dvmCompilerGetSrc(cUnit, mir, numDone++);
Bill Buzbee1465db52009-09-23 17:17:35 -0700997 loadValueDirectFixed(cUnit, rlArg, i);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700998 }
999 if (regMask) {
1000 /* Up to 5 args are pushed on top of FP - sizeofStackSaveArea */
Bill Buzbee1465db52009-09-23 17:17:35 -07001001 opRegRegImm(cUnit, kOpSub, r7, rFP,
1002 sizeof(StackSaveArea) + (dInsn->vA << 2));
Ben Chengba4fc8b2009-06-01 13:00:29 -07001003 /* generate null check */
1004 if (pcrLabel) {
Bill Buzbeec6f10662010-02-09 11:16:15 -08001005 *pcrLabel = genNullCheck(cUnit, dvmCompilerSSASrc(mir, 0), r0,
Bill Buzbee1465db52009-09-23 17:17:35 -07001006 mir->offset, NULL);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001007 }
Bill Buzbee270c1d62009-08-13 16:58:07 -07001008 storeMultiple(cUnit, r7, regMask);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001009 }
1010}
1011
1012static void genProcessArgsRange(CompilationUnit *cUnit, MIR *mir,
1013 DecodedInstruction *dInsn,
Bill Buzbee89efc3d2009-07-28 11:22:22 -07001014 ArmLIR **pcrLabel)
Ben Chengba4fc8b2009-06-01 13:00:29 -07001015{
1016 int srcOffset = dInsn->vC << 2;
1017 int numArgs = dInsn->vA;
1018 int regMask;
Bill Buzbee1465db52009-09-23 17:17:35 -07001019
1020 /*
1021 * Note: here, all promoted registers will have been flushed
1022 * back to the Dalvik base locations, so register usage restrictins
1023 * are lifted. All parms loaded from original Dalvik register
1024 * region - even though some might conceivably have valid copies
1025 * cached in a preserved register.
1026 */
Bill Buzbeec6f10662010-02-09 11:16:15 -08001027 dvmCompilerLockAllTemps(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07001028
Ben Chengba4fc8b2009-06-01 13:00:29 -07001029 /*
1030 * r4PC : &rFP[vC]
1031 * r7: &newFP[0]
1032 */
Bill Buzbee1465db52009-09-23 17:17:35 -07001033 opRegRegImm(cUnit, kOpAdd, r4PC, rFP, srcOffset);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001034 /* load [r0 .. min(numArgs,4)] */
1035 regMask = (1 << ((numArgs < 4) ? numArgs : 4)) - 1;
Ben Chengd7d426a2009-09-22 11:23:36 -07001036 /*
1037 * Protect the loadMultiple instruction from being reordered with other
1038 * Dalvik stack accesses.
jeffhao71eee1f2011-01-04 14:18:54 -08001039 *
1040 * This code is also shared by the invoke jumbo instructions, and this
1041 * does not need to be done if the invoke jumbo has no arguments.
Ben Chengd7d426a2009-09-22 11:23:36 -07001042 */
jeffhao71eee1f2011-01-04 14:18:54 -08001043 if (numArgs != 0) loadMultiple(cUnit, r4PC, regMask);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001044
Bill Buzbee1465db52009-09-23 17:17:35 -07001045 opRegRegImm(cUnit, kOpSub, r7, rFP,
1046 sizeof(StackSaveArea) + (numArgs << 2));
Ben Chengba4fc8b2009-06-01 13:00:29 -07001047 /* generate null check */
1048 if (pcrLabel) {
Bill Buzbeec6f10662010-02-09 11:16:15 -08001049 *pcrLabel = genNullCheck(cUnit, dvmCompilerSSASrc(mir, 0), r0,
Bill Buzbee1465db52009-09-23 17:17:35 -07001050 mir->offset, NULL);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001051 }
1052
1053 /*
1054 * Handle remaining 4n arguments:
1055 * store previously loaded 4 values and load the next 4 values
1056 */
1057 if (numArgs >= 8) {
Bill Buzbee89efc3d2009-07-28 11:22:22 -07001058 ArmLIR *loopLabel = NULL;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001059 /*
1060 * r0 contains "this" and it will be used later, so push it to the stack
Bill Buzbee270c1d62009-08-13 16:58:07 -07001061 * first. Pushing r5 (rFP) is just for stack alignment purposes.
Ben Chengba4fc8b2009-06-01 13:00:29 -07001062 */
Bill Buzbee1465db52009-09-23 17:17:35 -07001063 opImm(cUnit, kOpPush, (1 << r0 | 1 << rFP));
Ben Chengba4fc8b2009-06-01 13:00:29 -07001064 /* No need to generate the loop structure if numArgs <= 11 */
1065 if (numArgs > 11) {
1066 loadConstant(cUnit, 5, ((numArgs - 4) >> 2) << 2);
Bill Buzbee1465db52009-09-23 17:17:35 -07001067 loopLabel = newLIR0(cUnit, kArmPseudoTargetLabel);
Ben Chengd7d426a2009-09-22 11:23:36 -07001068 loopLabel->defMask = ENCODE_ALL;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001069 }
Bill Buzbee270c1d62009-08-13 16:58:07 -07001070 storeMultiple(cUnit, r7, regMask);
Ben Chengd7d426a2009-09-22 11:23:36 -07001071 /*
1072 * Protect the loadMultiple instruction from being reordered with other
1073 * Dalvik stack accesses.
1074 */
Bill Buzbee270c1d62009-08-13 16:58:07 -07001075 loadMultiple(cUnit, r4PC, regMask);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001076 /* No need to generate the loop structure if numArgs <= 11 */
1077 if (numArgs > 11) {
Bill Buzbee1465db52009-09-23 17:17:35 -07001078 opRegImm(cUnit, kOpSub, rFP, 4);
1079 genConditionalBranch(cUnit, kArmCondNe, loopLabel);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001080 }
1081 }
1082
1083 /* Save the last batch of loaded values */
jeffhao71eee1f2011-01-04 14:18:54 -08001084 if (numArgs != 0) storeMultiple(cUnit, r7, regMask);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001085
1086 /* Generate the loop epilogue - don't use r0 */
1087 if ((numArgs > 4) && (numArgs % 4)) {
1088 regMask = ((1 << (numArgs & 0x3)) - 1) << 1;
Ben Chengd7d426a2009-09-22 11:23:36 -07001089 /*
1090 * Protect the loadMultiple instruction from being reordered with other
1091 * Dalvik stack accesses.
1092 */
Bill Buzbee270c1d62009-08-13 16:58:07 -07001093 loadMultiple(cUnit, r4PC, regMask);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001094 }
1095 if (numArgs >= 8)
Bill Buzbee1465db52009-09-23 17:17:35 -07001096 opImm(cUnit, kOpPop, (1 << r0 | 1 << rFP));
Ben Chengba4fc8b2009-06-01 13:00:29 -07001097
1098 /* Save the modulo 4 arguments */
1099 if ((numArgs > 4) && (numArgs % 4)) {
Bill Buzbee270c1d62009-08-13 16:58:07 -07001100 storeMultiple(cUnit, r7, regMask);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001101 }
1102}
1103
Ben Cheng38329f52009-07-07 14:19:20 -07001104/*
1105 * Generate code to setup the call stack then jump to the chaining cell if it
1106 * is not a native method.
1107 */
1108static void genInvokeSingletonCommon(CompilationUnit *cUnit, MIR *mir,
Bill Buzbee89efc3d2009-07-28 11:22:22 -07001109 BasicBlock *bb, ArmLIR *labelList,
1110 ArmLIR *pcrLabel,
Ben Cheng38329f52009-07-07 14:19:20 -07001111 const Method *calleeMethod)
Ben Chengba4fc8b2009-06-01 13:00:29 -07001112{
Bill Buzbee1465db52009-09-23 17:17:35 -07001113 /*
1114 * Note: all Dalvik register state should be flushed to
1115 * memory by the point, so register usage restrictions no
1116 * longer apply. All temp & preserved registers may be used.
1117 */
Bill Buzbeec6f10662010-02-09 11:16:15 -08001118 dvmCompilerLockAllTemps(cUnit);
Bill Buzbee89efc3d2009-07-28 11:22:22 -07001119 ArmLIR *retChainingCell = &labelList[bb->fallThrough->id];
Ben Chengba4fc8b2009-06-01 13:00:29 -07001120
1121 /* r1 = &retChainingCell */
Bill Buzbee1465db52009-09-23 17:17:35 -07001122 ArmLIR *addrRetChain = opRegRegImm(cUnit, kOpAdd, r1, rpc, 0);
Ben Chengc8293e72010-10-12 11:50:10 -07001123
Ben Chengba4fc8b2009-06-01 13:00:29 -07001124 /* r4PC = dalvikCallsite */
1125 loadConstant(cUnit, r4PC,
1126 (int) (cUnit->method->insns + mir->offset));
1127 addrRetChain->generic.target = (LIR *) retChainingCell;
Ben Chengc8293e72010-10-12 11:50:10 -07001128
1129 /* r7 = calleeMethod->registersSize */
1130 loadConstant(cUnit, r7, calleeMethod->registersSize);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001131 /*
Ben Cheng38329f52009-07-07 14:19:20 -07001132 * r0 = calleeMethod (loaded upon calling genInvokeSingletonCommon)
Ben Chengba4fc8b2009-06-01 13:00:29 -07001133 * r1 = &ChainingCell
Ben Chengc8293e72010-10-12 11:50:10 -07001134 * r2 = calleeMethod->outsSize (to be loaded later for Java callees)
Ben Chengba4fc8b2009-06-01 13:00:29 -07001135 * r4PC = callsiteDPC
Ben Chengc8293e72010-10-12 11:50:10 -07001136 * r7 = calleeMethod->registersSize
Ben Chengba4fc8b2009-06-01 13:00:29 -07001137 */
1138 if (dvmIsNativeMethod(calleeMethod)) {
buzbee18fba342011-01-19 15:31:15 -08001139 genDispatchToHandler(cUnit, gDvmJit.methodTraceSupport ?
1140 TEMPLATE_INVOKE_METHOD_NATIVE_PROF :
1141 TEMPLATE_INVOKE_METHOD_NATIVE);
Ben Cheng978738d2010-05-13 13:45:57 -07001142#if defined(WITH_JIT_TUNING)
Ben Cheng38329f52009-07-07 14:19:20 -07001143 gDvmJit.invokeNative++;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001144#endif
1145 } else {
Ben Chengc8293e72010-10-12 11:50:10 -07001146 /* For Java callees, set up r2 to be calleeMethod->outsSize */
1147 loadConstant(cUnit, r2, calleeMethod->outsSize);
buzbee18fba342011-01-19 15:31:15 -08001148 genDispatchToHandler(cUnit, gDvmJit.methodTraceSupport ?
1149 TEMPLATE_INVOKE_METHOD_CHAIN_PROF :
1150 TEMPLATE_INVOKE_METHOD_CHAIN);
Ben Cheng978738d2010-05-13 13:45:57 -07001151#if defined(WITH_JIT_TUNING)
Ben Cheng86717f72010-03-05 15:27:21 -08001152 gDvmJit.invokeMonomorphic++;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001153#endif
Ben Cheng38329f52009-07-07 14:19:20 -07001154 /* Branch to the chaining cell */
Ben Chengba4fc8b2009-06-01 13:00:29 -07001155 genUnconditionalBranch(cUnit, &labelList[bb->taken->id]);
1156 }
1157 /* Handle exceptions using the interpreter */
1158 genTrap(cUnit, mir->offset, pcrLabel);
1159}
1160
Ben Cheng38329f52009-07-07 14:19:20 -07001161/*
1162 * Generate code to check the validity of a predicted chain and take actions
1163 * based on the result.
1164 *
1165 * 0x426a99aa : ldr r4, [pc, #72] --> r4 <- dalvikPC of this invoke
1166 * 0x426a99ac : add r1, pc, #32 --> r1 <- &retChainingCell
1167 * 0x426a99ae : add r2, pc, #40 --> r2 <- &predictedChainingCell
1168 * 0x426a99b0 : blx_1 0x426a918c --+ TEMPLATE_INVOKE_METHOD_PREDICTED_CHAIN
1169 * 0x426a99b2 : blx_2 see above --+
1170 * 0x426a99b4 : b 0x426a99d8 --> off to the predicted chain
1171 * 0x426a99b6 : b 0x426a99c8 --> punt to the interpreter
1172 * 0x426a99b8 : ldr r0, [r7, #44] --> r0 <- this->class->vtable[methodIdx]
1173 * 0x426a99ba : cmp r1, #0 --> compare r1 (rechain count) against 0
1174 * 0x426a99bc : bgt 0x426a99c2 --> >=0? don't rechain
Ben Chengaf5aa1f2011-01-04 15:37:04 -08001175 * 0x426a99be : ldr r7, [pc, #off]--+ dvmJitToPatchPredictedChain
Ben Cheng38329f52009-07-07 14:19:20 -07001176 * 0x426a99c0 : blx r7 --+
1177 * 0x426a99c2 : add r1, pc, #12 --> r1 <- &retChainingCell
1178 * 0x426a99c4 : blx_1 0x426a9098 --+ TEMPLATE_INVOKE_METHOD_NO_OPT
1179 * 0x426a99c6 : blx_2 see above --+
1180 */
1181static void genInvokeVirtualCommon(CompilationUnit *cUnit, MIR *mir,
1182 int methodIndex,
Bill Buzbee89efc3d2009-07-28 11:22:22 -07001183 ArmLIR *retChainingCell,
1184 ArmLIR *predChainingCell,
1185 ArmLIR *pcrLabel)
Ben Cheng38329f52009-07-07 14:19:20 -07001186{
Bill Buzbee1465db52009-09-23 17:17:35 -07001187 /*
1188 * Note: all Dalvik register state should be flushed to
1189 * memory by the point, so register usage restrictions no
1190 * longer apply. Lock temps to prevent them from being
1191 * allocated by utility routines.
1192 */
Bill Buzbeec6f10662010-02-09 11:16:15 -08001193 dvmCompilerLockAllTemps(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07001194
Ben Cheng38329f52009-07-07 14:19:20 -07001195 /* "this" is already left in r0 by genProcessArgs* */
1196
1197 /* r4PC = dalvikCallsite */
1198 loadConstant(cUnit, r4PC,
1199 (int) (cUnit->method->insns + mir->offset));
1200
1201 /* r1 = &retChainingCell */
Bill Buzbee1465db52009-09-23 17:17:35 -07001202 ArmLIR *addrRetChain = opRegRegImm(cUnit, kOpAdd, r1, rpc, 0);
Ben Cheng38329f52009-07-07 14:19:20 -07001203 addrRetChain->generic.target = (LIR *) retChainingCell;
1204
1205 /* r2 = &predictedChainingCell */
Bill Buzbee1465db52009-09-23 17:17:35 -07001206 ArmLIR *predictedChainingCell = opRegRegImm(cUnit, kOpAdd, r2, rpc, 0);
Ben Cheng38329f52009-07-07 14:19:20 -07001207 predictedChainingCell->generic.target = (LIR *) predChainingCell;
1208
buzbee18fba342011-01-19 15:31:15 -08001209 genDispatchToHandler(cUnit, gDvmJit.methodTraceSupport ?
1210 TEMPLATE_INVOKE_METHOD_PREDICTED_CHAIN_PROF :
1211 TEMPLATE_INVOKE_METHOD_PREDICTED_CHAIN);
Ben Cheng38329f52009-07-07 14:19:20 -07001212
1213 /* return through lr - jump to the chaining cell */
1214 genUnconditionalBranch(cUnit, predChainingCell);
1215
1216 /*
1217 * null-check on "this" may have been eliminated, but we still need a PC-
1218 * reconstruction label for stack overflow bailout.
1219 */
1220 if (pcrLabel == NULL) {
1221 int dPC = (int) (cUnit->method->insns + mir->offset);
Carl Shapirofc75f3e2010-12-07 11:43:38 -08001222 pcrLabel = (ArmLIR *) dvmCompilerNew(sizeof(ArmLIR), true);
Dan Bornstein9a1f8162010-12-01 17:02:26 -08001223 pcrLabel->opcode = kArmPseudoPCReconstructionCell;
Ben Cheng38329f52009-07-07 14:19:20 -07001224 pcrLabel->operands[0] = dPC;
1225 pcrLabel->operands[1] = mir->offset;
1226 /* Insert the place holder to the growable list */
Ben Cheng00603072010-10-28 11:13:58 -07001227 dvmInsertGrowableList(&cUnit->pcReconstructionList,
1228 (intptr_t) pcrLabel);
Ben Cheng38329f52009-07-07 14:19:20 -07001229 }
1230
1231 /* return through lr+2 - punt to the interpreter */
1232 genUnconditionalBranch(cUnit, pcrLabel);
1233
1234 /*
1235 * return through lr+4 - fully resolve the callee method.
1236 * r1 <- count
1237 * r2 <- &predictedChainCell
1238 * r3 <- this->class
1239 * r4 <- dPC
1240 * r7 <- this->class->vtable
1241 */
1242
1243 /* r0 <- calleeMethod */
Bill Buzbee270c1d62009-08-13 16:58:07 -07001244 loadWordDisp(cUnit, r7, methodIndex * 4, r0);
Ben Cheng38329f52009-07-07 14:19:20 -07001245
1246 /* Check if rechain limit is reached */
buzbee8f8109a2010-08-31 10:16:35 -07001247 ArmLIR *bypassRechaining = genCmpImmBranch(cUnit, kArmCondGt, r1, 0);
Ben Cheng38329f52009-07-07 14:19:20 -07001248
Ben Chengaf5aa1f2011-01-04 15:37:04 -08001249 LOAD_FUNC_ADDR(cUnit, r7, (int) dvmJitToPatchPredictedChain);
Ben Cheng38329f52009-07-07 14:19:20 -07001250
Ben Chengb88ec3c2010-05-17 12:50:33 -07001251 genRegCopy(cUnit, r1, rGLUE);
1252
Ben Cheng38329f52009-07-07 14:19:20 -07001253 /*
1254 * r0 = calleeMethod
1255 * r2 = &predictedChainingCell
1256 * r3 = class
1257 *
1258 * &returnChainingCell has been loaded into r1 but is not needed
1259 * when patching the chaining cell and will be clobbered upon
1260 * returning so it will be reconstructed again.
1261 */
Bill Buzbee1465db52009-09-23 17:17:35 -07001262 opReg(cUnit, kOpBlx, r7);
Ben Cheng38329f52009-07-07 14:19:20 -07001263
1264 /* r1 = &retChainingCell */
Bill Buzbee1465db52009-09-23 17:17:35 -07001265 addrRetChain = opRegRegImm(cUnit, kOpAdd, r1, rpc, 0);
Ben Cheng38329f52009-07-07 14:19:20 -07001266 addrRetChain->generic.target = (LIR *) retChainingCell;
1267
1268 bypassRechaining->generic.target = (LIR *) addrRetChain;
1269 /*
1270 * r0 = calleeMethod,
1271 * r1 = &ChainingCell,
1272 * r4PC = callsiteDPC,
1273 */
buzbee18fba342011-01-19 15:31:15 -08001274 genDispatchToHandler(cUnit, gDvmJit.methodTraceSupport ?
1275 TEMPLATE_INVOKE_METHOD_NO_OPT_PROF :
1276 TEMPLATE_INVOKE_METHOD_NO_OPT);
Ben Cheng978738d2010-05-13 13:45:57 -07001277#if defined(WITH_JIT_TUNING)
Ben Cheng86717f72010-03-05 15:27:21 -08001278 gDvmJit.invokePolymorphic++;
Ben Cheng38329f52009-07-07 14:19:20 -07001279#endif
1280 /* Handle exceptions using the interpreter */
1281 genTrap(cUnit, mir->offset, pcrLabel);
1282}
1283
Ben Chengba4fc8b2009-06-01 13:00:29 -07001284/* Geneate a branch to go back to the interpreter */
1285static void genPuntToInterp(CompilationUnit *cUnit, unsigned int offset)
1286{
1287 /* r0 = dalvik pc */
Bill Buzbeec6f10662010-02-09 11:16:15 -08001288 dvmCompilerFlushAllRegs(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001289 loadConstant(cUnit, r0, (int) (cUnit->method->insns + offset));
Bill Buzbee270c1d62009-08-13 16:58:07 -07001290 loadWordDisp(cUnit, rGLUE, offsetof(InterpState,
1291 jitToInterpEntries.dvmJitToInterpPunt), r1);
Bill Buzbee1465db52009-09-23 17:17:35 -07001292 opReg(cUnit, kOpBlx, r1);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001293}
1294
1295/*
1296 * Attempt to single step one instruction using the interpreter and return
1297 * to the compiled code for the next Dalvik instruction
1298 */
1299static void genInterpSingleStep(CompilationUnit *cUnit, MIR *mir)
1300{
Dan Bornsteine4852762010-12-02 12:45:00 -08001301 int flags = dexGetFlagsFromOpcode(mir->dalvikInsn.opcode);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001302 int flagsToCheck = kInstrCanBranch | kInstrCanSwitch | kInstrCanReturn |
1303 kInstrCanThrow;
Bill Buzbee1465db52009-09-23 17:17:35 -07001304
Bill Buzbee45273872010-03-11 11:12:15 -08001305 //If already optimized out, just ignore
Dan Bornstein9a1f8162010-12-01 17:02:26 -08001306 if (mir->dalvikInsn.opcode == OP_NOP)
Bill Buzbee45273872010-03-11 11:12:15 -08001307 return;
1308
Bill Buzbee1465db52009-09-23 17:17:35 -07001309 //Ugly, but necessary. Flush all Dalvik regs so Interp can find them
Bill Buzbeec6f10662010-02-09 11:16:15 -08001310 dvmCompilerFlushAllRegs(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07001311
Ben Chengba4fc8b2009-06-01 13:00:29 -07001312 if ((mir->next == NULL) || (flags & flagsToCheck)) {
1313 genPuntToInterp(cUnit, mir->offset);
1314 return;
1315 }
1316 int entryAddr = offsetof(InterpState,
1317 jitToInterpEntries.dvmJitToInterpSingleStep);
Bill Buzbee270c1d62009-08-13 16:58:07 -07001318 loadWordDisp(cUnit, rGLUE, entryAddr, r2);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001319 /* r0 = dalvik pc */
1320 loadConstant(cUnit, r0, (int) (cUnit->method->insns + mir->offset));
1321 /* r1 = dalvik pc of following instruction */
1322 loadConstant(cUnit, r1, (int) (cUnit->method->insns + mir->next->offset));
Bill Buzbee1465db52009-09-23 17:17:35 -07001323 opReg(cUnit, kOpBlx, r2);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001324}
1325
Carl Shapiro01605d22011-02-01 11:32:44 -08001326#if defined(_ARMV5TE) || defined(_ARMV5TE_VFP)
Bill Buzbeec1d9ed42010-02-02 11:04:33 -08001327/*
1328 * To prevent a thread in a monitor wait from blocking the Jit from
1329 * resetting the code cache, heavyweight monitor lock will not
1330 * be allowed to return to an existing translation. Instead, we will
1331 * handle them by branching to a handler, which will in turn call the
1332 * runtime lock routine and then branch directly back to the
1333 * interpreter main loop. Given the high cost of the heavyweight
1334 * lock operation, this additional cost should be slight (especially when
1335 * considering that we expect the vast majority of lock operations to
1336 * use the fast-path thin lock bypass).
1337 */
Ben Cheng5d90c202009-11-22 23:31:11 -08001338static void genMonitorPortable(CompilationUnit *cUnit, MIR *mir)
Bill Buzbee270c1d62009-08-13 16:58:07 -07001339{
Dan Bornstein9a1f8162010-12-01 17:02:26 -08001340 bool isEnter = (mir->dalvikInsn.opcode == OP_MONITOR_ENTER);
Bill Buzbee1465db52009-09-23 17:17:35 -07001341 genExportPC(cUnit, mir);
Bill Buzbeec6f10662010-02-09 11:16:15 -08001342 dvmCompilerFlushAllRegs(cUnit); /* Send everything to home location */
1343 RegLocation rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07001344 loadValueDirectFixed(cUnit, rlSrc, r1);
1345 loadWordDisp(cUnit, rGLUE, offsetof(InterpState, self), r0);
Bill Buzbeec1d9ed42010-02-02 11:04:33 -08001346 genNullCheck(cUnit, rlSrc.sRegLow, r1, mir->offset, NULL);
Bill Buzbeeefbd3c52009-11-04 22:18:40 -08001347 if (isEnter) {
Bill Buzbeec1d9ed42010-02-02 11:04:33 -08001348 /* Get dPC of next insn */
1349 loadConstant(cUnit, r4PC, (int)(cUnit->method->insns + mir->offset +
Dan Bornsteine4852762010-12-02 12:45:00 -08001350 dexGetWidthFromOpcode(OP_MONITOR_ENTER)));
Bill Buzbeec1d9ed42010-02-02 11:04:33 -08001351 genDispatchToHandler(cUnit, TEMPLATE_MONITOR_ENTER);
Bill Buzbee1465db52009-09-23 17:17:35 -07001352 } else {
Ben Chengbd1326d2010-04-02 15:04:53 -07001353 LOAD_FUNC_ADDR(cUnit, r2, (int)dvmUnlockObject);
Bill Buzbeec1d9ed42010-02-02 11:04:33 -08001354 /* Do the call */
1355 opReg(cUnit, kOpBlx, r2);
buzbee8f8109a2010-08-31 10:16:35 -07001356 /* Did we throw? */
1357 ArmLIR *branchOver = genCmpImmBranch(cUnit, kArmCondNe, r0, 0);
Bill Buzbee6bbdd6b2010-02-16 14:40:01 -08001358 loadConstant(cUnit, r0,
1359 (int) (cUnit->method->insns + mir->offset +
Dan Bornsteine4852762010-12-02 12:45:00 -08001360 dexGetWidthFromOpcode(OP_MONITOR_EXIT)));
Bill Buzbee6bbdd6b2010-02-16 14:40:01 -08001361 genDispatchToHandler(cUnit, TEMPLATE_THROW_EXCEPTION_COMMON);
1362 ArmLIR *target = newLIR0(cUnit, kArmPseudoTargetLabel);
1363 target->defMask = ENCODE_ALL;
1364 branchOver->generic.target = (LIR *) target;
Elliott Hughes6a555132010-02-25 15:41:42 -08001365 dvmCompilerClobberCallRegs(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07001366 }
Bill Buzbee270c1d62009-08-13 16:58:07 -07001367}
Ben Chengfc075c22010-05-28 15:20:08 -07001368#endif
Bill Buzbee270c1d62009-08-13 16:58:07 -07001369
Ben Chengba4fc8b2009-06-01 13:00:29 -07001370/*
Ben Cheng7ab74e12011-02-03 14:02:06 -08001371 * Fetch *InterpState->pSelfSuspendCount. If the suspend count is non-zero,
1372 * punt to the interpreter.
1373 */
1374static void genSuspendPoll(CompilationUnit *cUnit, MIR *mir)
1375{
1376 int rTemp = dvmCompilerAllocTemp(cUnit);
1377 ArmLIR *ld;
1378 ld = loadWordDisp(cUnit, rGLUE, offsetof(InterpState, pSelfSuspendCount),
1379 rTemp);
1380 setMemRefType(ld, true /* isLoad */, kMustNotAlias);
1381 ld = loadWordDisp(cUnit, rTemp, 0, rTemp);
1382 setMemRefType(ld, true /* isLoad */, kMustNotAlias);
1383 genRegImmCheck(cUnit, kArmCondNe, rTemp, 0, mir->offset, NULL);
1384}
1385
1386/*
Ben Chengba4fc8b2009-06-01 13:00:29 -07001387 * The following are the first-level codegen routines that analyze the format
1388 * of each bytecode then either dispatch special purpose codegen routines
1389 * or produce corresponding Thumb instructions directly.
1390 */
1391
1392static bool handleFmt10t_Fmt20t_Fmt30t(CompilationUnit *cUnit, MIR *mir,
Bill Buzbee89efc3d2009-07-28 11:22:22 -07001393 BasicBlock *bb, ArmLIR *labelList)
Ben Chengba4fc8b2009-06-01 13:00:29 -07001394{
Ben Cheng7ab74e12011-02-03 14:02:06 -08001395 /* backward branch? */
1396 bool backwardBranch = (bb->taken->startOffset <= mir->offset);
1397
1398 if (backwardBranch && gDvmJit.genSuspendPoll) {
1399 genSuspendPoll(cUnit, mir);
1400 }
1401
1402 int numPredecessors = dvmCountSetBits(bb->taken->predecessors);
1403 /*
1404 * Things could be hoisted out of the taken block into the predecessor, so
1405 * make sure it is dominated by the predecessor.
1406 */
1407 if (numPredecessors == 1 && bb->taken->visited == false &&
1408 bb->taken->blockType == kDalvikByteCode &&
1409 cUnit->methodJitMode == false ) {
1410 cUnit->nextCodegenBlock = bb->taken;
1411 } else {
1412 /* For OP_GOTO, OP_GOTO_16, and OP_GOTO_32 */
1413 genUnconditionalBranch(cUnit, &labelList[bb->taken->id]);
1414 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07001415 return false;
1416}
1417
1418static bool handleFmt10x(CompilationUnit *cUnit, MIR *mir)
1419{
Dan Bornstein9a1f8162010-12-01 17:02:26 -08001420 Opcode dalvikOpcode = mir->dalvikInsn.opcode;
1421 if ((dalvikOpcode >= OP_UNUSED_3E) && (dalvikOpcode <= OP_UNUSED_43)) {
1422 LOGE("Codegen: got unused opcode 0x%x\n",dalvikOpcode);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001423 return true;
1424 }
Dan Bornstein9a1f8162010-12-01 17:02:26 -08001425 switch (dalvikOpcode) {
Andy McFadden291758c2010-09-10 08:04:52 -07001426 case OP_RETURN_VOID_BARRIER:
buzbee2ce33c92010-11-01 15:53:27 -07001427 dvmCompilerGenMemBarrier(cUnit, kST);
1428 // Intentional fallthrough
1429 case OP_RETURN_VOID:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001430 genReturnCommon(cUnit,mir);
1431 break;
1432 case OP_UNUSED_73:
1433 case OP_UNUSED_79:
1434 case OP_UNUSED_7A:
Dan Bornstein90f15432010-12-02 16:46:25 -08001435 case OP_DISPATCH_FF:
Dan Bornstein9a1f8162010-12-01 17:02:26 -08001436 LOGE("Codegen: got unused opcode 0x%x\n",dalvikOpcode);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001437 return true;
1438 case OP_NOP:
1439 break;
1440 default:
1441 return true;
1442 }
1443 return false;
1444}
1445
1446static bool handleFmt11n_Fmt31i(CompilationUnit *cUnit, MIR *mir)
1447{
Bill Buzbee1465db52009-09-23 17:17:35 -07001448 RegLocation rlDest;
1449 RegLocation rlResult;
1450 if (mir->ssaRep->numDefs == 2) {
Bill Buzbeec6f10662010-02-09 11:16:15 -08001451 rlDest = dvmCompilerGetDestWide(cUnit, mir, 0, 1);
Bill Buzbee1465db52009-09-23 17:17:35 -07001452 } else {
Bill Buzbeec6f10662010-02-09 11:16:15 -08001453 rlDest = dvmCompilerGetDest(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07001454 }
Ben Chenge9695e52009-06-16 16:11:47 -07001455
Dan Bornstein9a1f8162010-12-01 17:02:26 -08001456 switch (mir->dalvikInsn.opcode) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07001457 case OP_CONST:
Ben Chenge9695e52009-06-16 16:11:47 -07001458 case OP_CONST_4: {
Bill Buzbeec6f10662010-02-09 11:16:15 -08001459 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kAnyReg, true);
Ben Chengbd1326d2010-04-02 15:04:53 -07001460 loadConstantNoClobber(cUnit, rlResult.lowReg, mir->dalvikInsn.vB);
Bill Buzbee1465db52009-09-23 17:17:35 -07001461 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001462 break;
Ben Chenge9695e52009-06-16 16:11:47 -07001463 }
1464 case OP_CONST_WIDE_32: {
Bill Buzbee1465db52009-09-23 17:17:35 -07001465 //TUNING: single routine to load constant pair for support doubles
Bill Buzbee964a7b02010-01-28 12:54:19 -08001466 //TUNING: load 0/-1 separately to avoid load dependency
Bill Buzbeec6f10662010-02-09 11:16:15 -08001467 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Ben Chengbd1326d2010-04-02 15:04:53 -07001468 loadConstantNoClobber(cUnit, rlResult.lowReg, mir->dalvikInsn.vB);
Bill Buzbee1465db52009-09-23 17:17:35 -07001469 opRegRegImm(cUnit, kOpAsr, rlResult.highReg,
1470 rlResult.lowReg, 31);
1471 storeValueWide(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001472 break;
Ben Chenge9695e52009-06-16 16:11:47 -07001473 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07001474 default:
1475 return true;
1476 }
1477 return false;
1478}
1479
1480static bool handleFmt21h(CompilationUnit *cUnit, MIR *mir)
1481{
Bill Buzbee1465db52009-09-23 17:17:35 -07001482 RegLocation rlDest;
1483 RegLocation rlResult;
1484 if (mir->ssaRep->numDefs == 2) {
Bill Buzbeec6f10662010-02-09 11:16:15 -08001485 rlDest = dvmCompilerGetDestWide(cUnit, mir, 0, 1);
Bill Buzbee1465db52009-09-23 17:17:35 -07001486 } else {
Bill Buzbeec6f10662010-02-09 11:16:15 -08001487 rlDest = dvmCompilerGetDest(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07001488 }
Bill Buzbeec6f10662010-02-09 11:16:15 -08001489 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kAnyReg, true);
Ben Chenge9695e52009-06-16 16:11:47 -07001490
Dan Bornstein9a1f8162010-12-01 17:02:26 -08001491 switch (mir->dalvikInsn.opcode) {
Ben Chenge9695e52009-06-16 16:11:47 -07001492 case OP_CONST_HIGH16: {
Ben Chengbd1326d2010-04-02 15:04:53 -07001493 loadConstantNoClobber(cUnit, rlResult.lowReg,
1494 mir->dalvikInsn.vB << 16);
Bill Buzbee1465db52009-09-23 17:17:35 -07001495 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001496 break;
Ben Chenge9695e52009-06-16 16:11:47 -07001497 }
1498 case OP_CONST_WIDE_HIGH16: {
Bill Buzbee1465db52009-09-23 17:17:35 -07001499 loadConstantValueWide(cUnit, rlResult.lowReg, rlResult.highReg,
1500 0, mir->dalvikInsn.vB << 16);
1501 storeValueWide(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001502 break;
Ben Chenge9695e52009-06-16 16:11:47 -07001503 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07001504 default:
1505 return true;
1506 }
1507 return false;
1508}
1509
jeffhao71eee1f2011-01-04 14:18:54 -08001510static bool handleFmt20bc_Fmt40sc(CompilationUnit *cUnit, MIR *mir)
Ben Chengba4fc8b2009-06-01 13:00:29 -07001511{
jeffhao71eee1f2011-01-04 14:18:54 -08001512 /* For OP_THROW_VERIFICATION_ERROR & OP_THROW_VERIFICATION_ERROR_JUMBO */
Ben Chengba4fc8b2009-06-01 13:00:29 -07001513 genInterpSingleStep(cUnit, mir);
1514 return false;
1515}
1516
jeffhao71eee1f2011-01-04 14:18:54 -08001517static bool handleFmt21c_Fmt31c_Fmt41c(CompilationUnit *cUnit, MIR *mir)
Ben Chengba4fc8b2009-06-01 13:00:29 -07001518{
Bill Buzbee1465db52009-09-23 17:17:35 -07001519 RegLocation rlResult;
1520 RegLocation rlDest;
1521 RegLocation rlSrc;
Ben Chenge9695e52009-06-16 16:11:47 -07001522
Dan Bornstein9a1f8162010-12-01 17:02:26 -08001523 switch (mir->dalvikInsn.opcode) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07001524 case OP_CONST_STRING_JUMBO:
1525 case OP_CONST_STRING: {
1526 void *strPtr = (void*)
1527 (cUnit->method->clazz->pDvmDex->pResStrings[mir->dalvikInsn.vB]);
Ben Chengdd6e8702010-05-07 13:05:47 -07001528
1529 if (strPtr == NULL) {
1530 LOGE("Unexpected null string");
1531 dvmAbort();
1532 }
1533
Bill Buzbeec6f10662010-02-09 11:16:15 -08001534 rlDest = dvmCompilerGetDest(cUnit, mir, 0);
1535 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Ben Chengbd1326d2010-04-02 15:04:53 -07001536 loadConstantNoClobber(cUnit, rlResult.lowReg, (int) strPtr );
Bill Buzbee1465db52009-09-23 17:17:35 -07001537 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001538 break;
1539 }
jeffhao71eee1f2011-01-04 14:18:54 -08001540 case OP_CONST_CLASS:
1541 case OP_CONST_CLASS_JUMBO: {
Ben Chengba4fc8b2009-06-01 13:00:29 -07001542 void *classPtr = (void*)
1543 (cUnit->method->clazz->pDvmDex->pResClasses[mir->dalvikInsn.vB]);
Ben Chengdd6e8702010-05-07 13:05:47 -07001544
1545 if (classPtr == NULL) {
1546 LOGE("Unexpected null class");
1547 dvmAbort();
1548 }
1549
Bill Buzbeec6f10662010-02-09 11:16:15 -08001550 rlDest = dvmCompilerGetDest(cUnit, mir, 0);
1551 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Ben Chengbd1326d2010-04-02 15:04:53 -07001552 loadConstantNoClobber(cUnit, rlResult.lowReg, (int) classPtr );
Bill Buzbee1465db52009-09-23 17:17:35 -07001553 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001554 break;
1555 }
jeffhao71eee1f2011-01-04 14:18:54 -08001556 case OP_SGET:
buzbeeecf8f6e2010-07-20 14:53:42 -07001557 case OP_SGET_VOLATILE:
jeffhao71eee1f2011-01-04 14:18:54 -08001558 case OP_SGET_JUMBO:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001559 case OP_SGET_OBJECT:
jeffhao71eee1f2011-01-04 14:18:54 -08001560 case OP_SGET_OBJECT_VOLATILE:
1561 case OP_SGET_OBJECT_JUMBO:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001562 case OP_SGET_BOOLEAN:
jeffhao71eee1f2011-01-04 14:18:54 -08001563 case OP_SGET_BOOLEAN_JUMBO:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001564 case OP_SGET_CHAR:
jeffhao71eee1f2011-01-04 14:18:54 -08001565 case OP_SGET_CHAR_JUMBO:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001566 case OP_SGET_BYTE:
jeffhao71eee1f2011-01-04 14:18:54 -08001567 case OP_SGET_BYTE_JUMBO:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001568 case OP_SGET_SHORT:
jeffhao71eee1f2011-01-04 14:18:54 -08001569 case OP_SGET_SHORT_JUMBO: {
Bill Buzbee50a6bf22009-07-08 13:08:04 -07001570 int valOffset = offsetof(StaticField, value);
Bill Buzbeec6f10662010-02-09 11:16:15 -08001571 int tReg = dvmCompilerAllocTemp(cUnit);
buzbeeecf8f6e2010-07-20 14:53:42 -07001572 bool isVolatile;
Ben Cheng7a2697d2010-06-07 13:44:23 -07001573 const Method *method = (mir->OptimizationFlags & MIR_CALLEE) ?
1574 mir->meta.calleeMethod : cUnit->method;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001575 void *fieldPtr = (void*)
Ben Cheng7a2697d2010-06-07 13:44:23 -07001576 (method->clazz->pDvmDex->pResFields[mir->dalvikInsn.vB]);
Ben Chengdd6e8702010-05-07 13:05:47 -07001577
1578 if (fieldPtr == NULL) {
1579 LOGE("Unexpected null static field");
1580 dvmAbort();
1581 }
1582
Dan Bornstein9a1f8162010-12-01 17:02:26 -08001583 isVolatile = (mir->dalvikInsn.opcode == OP_SGET_VOLATILE) ||
1584 (mir->dalvikInsn.opcode == OP_SGET_OBJECT_VOLATILE) ||
Carl Shapirofc75f3e2010-12-07 11:43:38 -08001585 dvmIsVolatileField((Field *) fieldPtr);
buzbeeecf8f6e2010-07-20 14:53:42 -07001586
Bill Buzbeec6f10662010-02-09 11:16:15 -08001587 rlDest = dvmCompilerGetDest(cUnit, mir, 0);
1588 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kAnyReg, true);
Bill Buzbee1465db52009-09-23 17:17:35 -07001589 loadConstant(cUnit, tReg, (int) fieldPtr + valOffset);
Ben Cheng11d8f142010-03-24 15:24:19 -07001590
buzbeeecf8f6e2010-07-20 14:53:42 -07001591 if (isVolatile) {
buzbee2ce33c92010-11-01 15:53:27 -07001592 dvmCompilerGenMemBarrier(cUnit, kSY);
buzbeeecf8f6e2010-07-20 14:53:42 -07001593 }
Ben Cheng11d8f142010-03-24 15:24:19 -07001594 HEAP_ACCESS_SHADOW(true);
Bill Buzbee1465db52009-09-23 17:17:35 -07001595 loadWordDisp(cUnit, tReg, 0, rlResult.lowReg);
Ben Cheng11d8f142010-03-24 15:24:19 -07001596 HEAP_ACCESS_SHADOW(false);
1597
Bill Buzbee1465db52009-09-23 17:17:35 -07001598 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001599 break;
1600 }
jeffhao71eee1f2011-01-04 14:18:54 -08001601 case OP_SGET_WIDE:
1602 case OP_SGET_WIDE_JUMBO: {
Bill Buzbee50a6bf22009-07-08 13:08:04 -07001603 int valOffset = offsetof(StaticField, value);
Ben Cheng7a2697d2010-06-07 13:44:23 -07001604 const Method *method = (mir->OptimizationFlags & MIR_CALLEE) ?
1605 mir->meta.calleeMethod : cUnit->method;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001606 void *fieldPtr = (void*)
Ben Cheng7a2697d2010-06-07 13:44:23 -07001607 (method->clazz->pDvmDex->pResFields[mir->dalvikInsn.vB]);
Ben Chengdd6e8702010-05-07 13:05:47 -07001608
1609 if (fieldPtr == NULL) {
1610 LOGE("Unexpected null static field");
1611 dvmAbort();
1612 }
1613
Bill Buzbeec6f10662010-02-09 11:16:15 -08001614 int tReg = dvmCompilerAllocTemp(cUnit);
Bill Buzbeec6f10662010-02-09 11:16:15 -08001615 rlDest = dvmCompilerGetDestWide(cUnit, mir, 0, 1);
1616 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kAnyReg, true);
Bill Buzbee1465db52009-09-23 17:17:35 -07001617 loadConstant(cUnit, tReg, (int) fieldPtr + valOffset);
Ben Cheng11d8f142010-03-24 15:24:19 -07001618
1619 HEAP_ACCESS_SHADOW(true);
Bill Buzbee1465db52009-09-23 17:17:35 -07001620 loadPair(cUnit, tReg, rlResult.lowReg, rlResult.highReg);
Ben Cheng11d8f142010-03-24 15:24:19 -07001621 HEAP_ACCESS_SHADOW(false);
1622
Bill Buzbee1465db52009-09-23 17:17:35 -07001623 storeValueWide(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001624 break;
1625 }
jeffhao71eee1f2011-01-04 14:18:54 -08001626 case OP_SPUT:
1627 case OP_SPUT_VOLATILE:
1628 case OP_SPUT_JUMBO:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001629 case OP_SPUT_OBJECT:
buzbeeddc7d292010-09-02 17:16:24 -07001630 case OP_SPUT_OBJECT_VOLATILE:
jeffhao71eee1f2011-01-04 14:18:54 -08001631 case OP_SPUT_OBJECT_JUMBO:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001632 case OP_SPUT_BOOLEAN:
jeffhao71eee1f2011-01-04 14:18:54 -08001633 case OP_SPUT_BOOLEAN_JUMBO:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001634 case OP_SPUT_CHAR:
jeffhao71eee1f2011-01-04 14:18:54 -08001635 case OP_SPUT_CHAR_JUMBO:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001636 case OP_SPUT_BYTE:
jeffhao71eee1f2011-01-04 14:18:54 -08001637 case OP_SPUT_BYTE_JUMBO:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001638 case OP_SPUT_SHORT:
jeffhao71eee1f2011-01-04 14:18:54 -08001639 case OP_SPUT_SHORT_JUMBO: {
Bill Buzbee50a6bf22009-07-08 13:08:04 -07001640 int valOffset = offsetof(StaticField, value);
Bill Buzbeec6f10662010-02-09 11:16:15 -08001641 int tReg = dvmCompilerAllocTemp(cUnit);
buzbeed3b0a4b2010-09-27 11:30:22 -07001642 int objHead;
buzbeeecf8f6e2010-07-20 14:53:42 -07001643 bool isVolatile;
buzbeed3b0a4b2010-09-27 11:30:22 -07001644 bool isSputObject;
Ben Cheng7a2697d2010-06-07 13:44:23 -07001645 const Method *method = (mir->OptimizationFlags & MIR_CALLEE) ?
1646 mir->meta.calleeMethod : cUnit->method;
1647 void *fieldPtr = (void*)
1648 (method->clazz->pDvmDex->pResFields[mir->dalvikInsn.vB]);
Ben Chenge9695e52009-06-16 16:11:47 -07001649
Dan Bornstein9a1f8162010-12-01 17:02:26 -08001650 isVolatile = (mir->dalvikInsn.opcode == OP_SPUT_VOLATILE) ||
1651 (mir->dalvikInsn.opcode == OP_SPUT_OBJECT_VOLATILE) ||
Carl Shapirofc75f3e2010-12-07 11:43:38 -08001652 dvmIsVolatileField((Field *) fieldPtr);
buzbeeecf8f6e2010-07-20 14:53:42 -07001653
Dan Bornstein9a1f8162010-12-01 17:02:26 -08001654 isSputObject = (mir->dalvikInsn.opcode == OP_SPUT_OBJECT) ||
jeffhao71eee1f2011-01-04 14:18:54 -08001655 (mir->dalvikInsn.opcode == OP_SPUT_OBJECT_JUMBO) ||
Dan Bornstein9a1f8162010-12-01 17:02:26 -08001656 (mir->dalvikInsn.opcode == OP_SPUT_OBJECT_VOLATILE);
buzbeed3b0a4b2010-09-27 11:30:22 -07001657
Ben Chengdd6e8702010-05-07 13:05:47 -07001658 if (fieldPtr == NULL) {
1659 LOGE("Unexpected null static field");
1660 dvmAbort();
1661 }
1662
Bill Buzbeec6f10662010-02-09 11:16:15 -08001663 rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07001664 rlSrc = loadValue(cUnit, rlSrc, kAnyReg);
buzbeeb78c76f2010-09-30 19:08:20 -07001665 loadConstant(cUnit, tReg, (int) fieldPtr);
buzbeed3b0a4b2010-09-27 11:30:22 -07001666 if (isSputObject) {
1667 objHead = dvmCompilerAllocTemp(cUnit);
buzbeeb78c76f2010-09-30 19:08:20 -07001668 loadWordDisp(cUnit, tReg, offsetof(Field, clazz), objHead);
buzbeed3b0a4b2010-09-27 11:30:22 -07001669 }
Ben Cheng11d8f142010-03-24 15:24:19 -07001670 HEAP_ACCESS_SHADOW(true);
buzbeeb78c76f2010-09-30 19:08:20 -07001671 storeWordDisp(cUnit, tReg, valOffset ,rlSrc.lowReg);
buzbeed3b0a4b2010-09-27 11:30:22 -07001672 dvmCompilerFreeTemp(cUnit, tReg);
Ben Cheng11d8f142010-03-24 15:24:19 -07001673 HEAP_ACCESS_SHADOW(false);
buzbeeecf8f6e2010-07-20 14:53:42 -07001674 if (isVolatile) {
buzbee2ce33c92010-11-01 15:53:27 -07001675 dvmCompilerGenMemBarrier(cUnit, kSY);
buzbeeecf8f6e2010-07-20 14:53:42 -07001676 }
buzbeed3b0a4b2010-09-27 11:30:22 -07001677 if (isSputObject) {
buzbeeb78c76f2010-09-30 19:08:20 -07001678 /* NOTE: marking card based sfield->clazz */
buzbeed3b0a4b2010-09-27 11:30:22 -07001679 markCard(cUnit, rlSrc.lowReg, objHead);
1680 dvmCompilerFreeTemp(cUnit, objHead);
buzbee919eb062010-07-12 12:59:22 -07001681 }
Ben Cheng11d8f142010-03-24 15:24:19 -07001682
Ben Chengba4fc8b2009-06-01 13:00:29 -07001683 break;
1684 }
jeffhao71eee1f2011-01-04 14:18:54 -08001685 case OP_SPUT_WIDE:
1686 case OP_SPUT_WIDE_JUMBO: {
Bill Buzbeec6f10662010-02-09 11:16:15 -08001687 int tReg = dvmCompilerAllocTemp(cUnit);
Bill Buzbee50a6bf22009-07-08 13:08:04 -07001688 int valOffset = offsetof(StaticField, value);
Ben Cheng7a2697d2010-06-07 13:44:23 -07001689 const Method *method = (mir->OptimizationFlags & MIR_CALLEE) ?
1690 mir->meta.calleeMethod : cUnit->method;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001691 void *fieldPtr = (void*)
Ben Cheng7a2697d2010-06-07 13:44:23 -07001692 (method->clazz->pDvmDex->pResFields[mir->dalvikInsn.vB]);
Ben Chenge9695e52009-06-16 16:11:47 -07001693
Ben Chengdd6e8702010-05-07 13:05:47 -07001694 if (fieldPtr == NULL) {
1695 LOGE("Unexpected null static field");
1696 dvmAbort();
1697 }
1698
Bill Buzbeec6f10662010-02-09 11:16:15 -08001699 rlSrc = dvmCompilerGetSrcWide(cUnit, mir, 0, 1);
Bill Buzbee1465db52009-09-23 17:17:35 -07001700 rlSrc = loadValueWide(cUnit, rlSrc, kAnyReg);
1701 loadConstant(cUnit, tReg, (int) fieldPtr + valOffset);
Ben Cheng11d8f142010-03-24 15:24:19 -07001702
1703 HEAP_ACCESS_SHADOW(true);
Bill Buzbee1465db52009-09-23 17:17:35 -07001704 storePair(cUnit, tReg, rlSrc.lowReg, rlSrc.highReg);
Ben Cheng11d8f142010-03-24 15:24:19 -07001705 HEAP_ACCESS_SHADOW(false);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001706 break;
1707 }
jeffhao71eee1f2011-01-04 14:18:54 -08001708 case OP_NEW_INSTANCE:
1709 case OP_NEW_INSTANCE_JUMBO: {
Ben Chenge9695e52009-06-16 16:11:47 -07001710 /*
1711 * Obey the calling convention and don't mess with the register
1712 * usage.
1713 */
Carl Shapirofc75f3e2010-12-07 11:43:38 -08001714 ClassObject *classPtr = (ClassObject *)
Ben Chengba4fc8b2009-06-01 13:00:29 -07001715 (cUnit->method->clazz->pDvmDex->pResClasses[mir->dalvikInsn.vB]);
Ben Chengdd6e8702010-05-07 13:05:47 -07001716
1717 if (classPtr == NULL) {
1718 LOGE("Unexpected null class");
1719 dvmAbort();
1720 }
1721
Ben Cheng79d173c2009-09-29 16:12:51 -07001722 /*
1723 * If it is going to throw, it should not make to the trace to begin
Bill Buzbee1465db52009-09-23 17:17:35 -07001724 * with. However, Alloc might throw, so we need to genExportPC()
Ben Cheng79d173c2009-09-29 16:12:51 -07001725 */
1726 assert((classPtr->accessFlags & (ACC_INTERFACE|ACC_ABSTRACT)) == 0);
Bill Buzbeec6f10662010-02-09 11:16:15 -08001727 dvmCompilerFlushAllRegs(cUnit); /* Everything to home location */
Bill Buzbee1465db52009-09-23 17:17:35 -07001728 genExportPC(cUnit, mir);
Ben Chengbd1326d2010-04-02 15:04:53 -07001729 LOAD_FUNC_ADDR(cUnit, r2, (int)dvmAllocObject);
Ben Chenge9695e52009-06-16 16:11:47 -07001730 loadConstant(cUnit, r0, (int) classPtr);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001731 loadConstant(cUnit, r1, ALLOC_DONT_TRACK);
Bill Buzbee1465db52009-09-23 17:17:35 -07001732 opReg(cUnit, kOpBlx, r2);
Elliott Hughes6a555132010-02-25 15:41:42 -08001733 dvmCompilerClobberCallRegs(cUnit);
Ben Cheng4f489172009-09-27 17:08:35 -07001734 /* generate a branch over if allocation is successful */
buzbee8f8109a2010-08-31 10:16:35 -07001735 ArmLIR *branchOver = genCmpImmBranch(cUnit, kArmCondNe, r0, 0);
Ben Cheng4f489172009-09-27 17:08:35 -07001736 /*
1737 * OOM exception needs to be thrown here and cannot re-execute
1738 */
1739 loadConstant(cUnit, r0,
1740 (int) (cUnit->method->insns + mir->offset));
1741 genDispatchToHandler(cUnit, TEMPLATE_THROW_EXCEPTION_COMMON);
1742 /* noreturn */
1743
Bill Buzbee1465db52009-09-23 17:17:35 -07001744 ArmLIR *target = newLIR0(cUnit, kArmPseudoTargetLabel);
Ben Cheng4f489172009-09-27 17:08:35 -07001745 target->defMask = ENCODE_ALL;
1746 branchOver->generic.target = (LIR *) target;
Bill Buzbeec6f10662010-02-09 11:16:15 -08001747 rlDest = dvmCompilerGetDest(cUnit, mir, 0);
1748 rlResult = dvmCompilerGetReturn(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07001749 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001750 break;
1751 }
jeffhao71eee1f2011-01-04 14:18:54 -08001752 case OP_CHECK_CAST:
1753 case OP_CHECK_CAST_JUMBO: {
Ben Chenge9695e52009-06-16 16:11:47 -07001754 /*
1755 * Obey the calling convention and don't mess with the register
1756 * usage.
1757 */
Ben Chengba4fc8b2009-06-01 13:00:29 -07001758 ClassObject *classPtr =
1759 (cUnit->method->clazz->pDvmDex->pResClasses[mir->dalvikInsn.vB]);
Bill Buzbee4df41a52009-11-12 17:07:16 -08001760 /*
1761 * Note: It is possible that classPtr is NULL at this point,
1762 * even though this instruction has been successfully interpreted.
1763 * If the previous interpretation had a null source, the
1764 * interpreter would not have bothered to resolve the clazz.
1765 * Bail out to the interpreter in this case, and log it
1766 * so that we can tell if it happens frequently.
1767 */
1768 if (classPtr == NULL) {
Ben Cheng11d8f142010-03-24 15:24:19 -07001769 LOGVV("null clazz in OP_CHECK_CAST, single-stepping");
Bill Buzbee4df41a52009-11-12 17:07:16 -08001770 genInterpSingleStep(cUnit, mir);
1771 return false;
1772 }
Bill Buzbeec6f10662010-02-09 11:16:15 -08001773 dvmCompilerFlushAllRegs(cUnit); /* Everything to home location */
Ben Chengba4fc8b2009-06-01 13:00:29 -07001774 loadConstant(cUnit, r1, (int) classPtr );
Bill Buzbeec6f10662010-02-09 11:16:15 -08001775 rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07001776 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
buzbee8f8109a2010-08-31 10:16:35 -07001777 /* Null? */
1778 ArmLIR *branch1 = genCmpImmBranch(cUnit, kArmCondEq,
1779 rlSrc.lowReg, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07001780 /*
1781 * rlSrc.lowReg now contains object->clazz. Note that
1782 * it could have been allocated r0, but we're okay so long
1783 * as we don't do anything desctructive until r0 is loaded
1784 * with clazz.
1785 */
Ben Chengba4fc8b2009-06-01 13:00:29 -07001786 /* r0 now contains object->clazz */
Bill Buzbee1465db52009-09-23 17:17:35 -07001787 loadWordDisp(cUnit, rlSrc.lowReg, offsetof(Object, clazz), r0);
Ben Chengbd1326d2010-04-02 15:04:53 -07001788 LOAD_FUNC_ADDR(cUnit, r2, (int)dvmInstanceofNonTrivial);
Bill Buzbee1465db52009-09-23 17:17:35 -07001789 opRegReg(cUnit, kOpCmp, r0, r1);
1790 ArmLIR *branch2 = opCondBranch(cUnit, kArmCondEq);
1791 opReg(cUnit, kOpBlx, r2);
Elliott Hughes6a555132010-02-25 15:41:42 -08001792 dvmCompilerClobberCallRegs(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07001793 /*
1794 * If null, check cast failed - punt to the interpreter. Because
1795 * interpreter will be the one throwing, we don't need to
1796 * genExportPC() here.
1797 */
Bill Buzbee270c1d62009-08-13 16:58:07 -07001798 genZeroCheck(cUnit, r0, mir->offset, NULL);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001799 /* check cast passed - branch target here */
Bill Buzbee1465db52009-09-23 17:17:35 -07001800 ArmLIR *target = newLIR0(cUnit, kArmPseudoTargetLabel);
Ben Chengd7d426a2009-09-22 11:23:36 -07001801 target->defMask = ENCODE_ALL;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001802 branch1->generic.target = (LIR *)target;
1803 branch2->generic.target = (LIR *)target;
1804 break;
1805 }
buzbee4d92e682010-07-29 15:24:14 -07001806 case OP_SGET_WIDE_VOLATILE:
1807 case OP_SPUT_WIDE_VOLATILE:
1808 genInterpSingleStep(cUnit, mir);
1809 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001810 default:
1811 return true;
1812 }
1813 return false;
1814}
1815
Ben Cheng7a2697d2010-06-07 13:44:23 -07001816/*
1817 * A typical example of inlined getter/setter from a monomorphic callsite:
1818 *
1819 * D/dalvikvm( 289): -------- dalvik offset: 0x0000 @ invoke-static (I)
1820 * D/dalvikvm( 289): -------- dalvik offset: 0x0000 @ sget-object (C) v0, ...
1821 * D/dalvikvm( 289): 0x4427fc22 (0002): ldr r0, [pc, #56]
1822 * D/dalvikvm( 289): 0x4427fc24 (0004): ldr r1, [r0, #0]
1823 * D/dalvikvm( 289): 0x4427fc26 (0006): str r1, [r5, #0]
1824 * D/dalvikvm( 289): 0x4427fc28 (0008): .align4
1825 * D/dalvikvm( 289): L0x0003:
1826 * D/dalvikvm( 289): -------- dalvik offset: 0x0003 @ move-result-object (I) v0
1827 *
1828 * Note the invoke-static and move-result-object with the (I) notation are
1829 * turned into no-op.
1830 */
Ben Chengba4fc8b2009-06-01 13:00:29 -07001831static bool handleFmt11x(CompilationUnit *cUnit, MIR *mir)
1832{
Dan Bornstein9a1f8162010-12-01 17:02:26 -08001833 Opcode dalvikOpcode = mir->dalvikInsn.opcode;
Bill Buzbee1465db52009-09-23 17:17:35 -07001834 RegLocation rlResult;
Dan Bornstein9a1f8162010-12-01 17:02:26 -08001835 switch (dalvikOpcode) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07001836 case OP_MOVE_EXCEPTION: {
1837 int offset = offsetof(InterpState, self);
1838 int exOffset = offsetof(Thread, exception);
Bill Buzbeec6f10662010-02-09 11:16:15 -08001839 int selfReg = dvmCompilerAllocTemp(cUnit);
1840 int resetReg = dvmCompilerAllocTemp(cUnit);
1841 RegLocation rlDest = dvmCompilerGetDest(cUnit, mir, 0);
1842 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Bill Buzbee1465db52009-09-23 17:17:35 -07001843 loadWordDisp(cUnit, rGLUE, offset, selfReg);
Bill Buzbeef9f33282009-11-22 12:45:30 -08001844 loadConstant(cUnit, resetReg, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07001845 loadWordDisp(cUnit, selfReg, exOffset, rlResult.lowReg);
Bill Buzbeef9f33282009-11-22 12:45:30 -08001846 storeWordDisp(cUnit, selfReg, exOffset, resetReg);
Bill Buzbee1465db52009-09-23 17:17:35 -07001847 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001848 break;
1849 }
1850 case OP_MOVE_RESULT:
1851 case OP_MOVE_RESULT_OBJECT: {
Ben Cheng7a2697d2010-06-07 13:44:23 -07001852 /* An inlined move result is effectively no-op */
1853 if (mir->OptimizationFlags & MIR_INLINED)
1854 break;
Bill Buzbeec6f10662010-02-09 11:16:15 -08001855 RegLocation rlDest = dvmCompilerGetDest(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07001856 RegLocation rlSrc = LOC_DALVIK_RETURN_VAL;
1857 rlSrc.fp = rlDest.fp;
1858 storeValue(cUnit, rlDest, rlSrc);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001859 break;
1860 }
1861 case OP_MOVE_RESULT_WIDE: {
Ben Cheng7a2697d2010-06-07 13:44:23 -07001862 /* An inlined move result is effectively no-op */
1863 if (mir->OptimizationFlags & MIR_INLINED)
1864 break;
Bill Buzbeec6f10662010-02-09 11:16:15 -08001865 RegLocation rlDest = dvmCompilerGetDestWide(cUnit, mir, 0, 1);
Bill Buzbee1465db52009-09-23 17:17:35 -07001866 RegLocation rlSrc = LOC_DALVIK_RETURN_VAL_WIDE;
1867 rlSrc.fp = rlDest.fp;
1868 storeValueWide(cUnit, rlDest, rlSrc);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001869 break;
1870 }
1871 case OP_RETURN_WIDE: {
Bill Buzbeec6f10662010-02-09 11:16:15 -08001872 RegLocation rlSrc = dvmCompilerGetSrcWide(cUnit, mir, 0, 1);
Bill Buzbee1465db52009-09-23 17:17:35 -07001873 RegLocation rlDest = LOC_DALVIK_RETURN_VAL_WIDE;
1874 rlDest.fp = rlSrc.fp;
1875 storeValueWide(cUnit, rlDest, rlSrc);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001876 genReturnCommon(cUnit,mir);
1877 break;
1878 }
1879 case OP_RETURN:
1880 case OP_RETURN_OBJECT: {
Bill Buzbeec6f10662010-02-09 11:16:15 -08001881 RegLocation rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07001882 RegLocation rlDest = LOC_DALVIK_RETURN_VAL;
1883 rlDest.fp = rlSrc.fp;
1884 storeValue(cUnit, rlDest, rlSrc);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001885 genReturnCommon(cUnit,mir);
1886 break;
1887 }
Bill Buzbee1465db52009-09-23 17:17:35 -07001888 case OP_MONITOR_EXIT:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001889 case OP_MONITOR_ENTER:
Ben Cheng5d90c202009-11-22 23:31:11 -08001890 genMonitor(cUnit, mir);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001891 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001892 case OP_THROW: {
1893 genInterpSingleStep(cUnit, mir);
1894 break;
1895 }
1896 default:
1897 return true;
1898 }
1899 return false;
1900}
1901
Bill Buzbeed45ba372009-06-15 17:00:57 -07001902static bool handleFmt12x(CompilationUnit *cUnit, MIR *mir)
1903{
Dan Bornstein9a1f8162010-12-01 17:02:26 -08001904 Opcode opcode = mir->dalvikInsn.opcode;
Bill Buzbee1465db52009-09-23 17:17:35 -07001905 RegLocation rlDest;
1906 RegLocation rlSrc;
1907 RegLocation rlResult;
Bill Buzbeed45ba372009-06-15 17:00:57 -07001908
Dan Bornstein9a1f8162010-12-01 17:02:26 -08001909 if ( (opcode >= OP_ADD_INT_2ADDR) && (opcode <= OP_REM_DOUBLE_2ADDR)) {
Ben Cheng5d90c202009-11-22 23:31:11 -08001910 return genArithOp( cUnit, mir );
Ben Chengba4fc8b2009-06-01 13:00:29 -07001911 }
1912
Bill Buzbee1465db52009-09-23 17:17:35 -07001913 if (mir->ssaRep->numUses == 2)
Bill Buzbeec6f10662010-02-09 11:16:15 -08001914 rlSrc = dvmCompilerGetSrcWide(cUnit, mir, 0, 1);
Bill Buzbee1465db52009-09-23 17:17:35 -07001915 else
Bill Buzbeec6f10662010-02-09 11:16:15 -08001916 rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07001917 if (mir->ssaRep->numDefs == 2)
Bill Buzbeec6f10662010-02-09 11:16:15 -08001918 rlDest = dvmCompilerGetDestWide(cUnit, mir, 0, 1);
Bill Buzbee1465db52009-09-23 17:17:35 -07001919 else
Bill Buzbeec6f10662010-02-09 11:16:15 -08001920 rlDest = dvmCompilerGetDest(cUnit, mir, 0);
Ben Chenge9695e52009-06-16 16:11:47 -07001921
Dan Bornstein9a1f8162010-12-01 17:02:26 -08001922 switch (opcode) {
Bill Buzbee1465db52009-09-23 17:17:35 -07001923 case OP_DOUBLE_TO_INT:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001924 case OP_INT_TO_FLOAT:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001925 case OP_FLOAT_TO_INT:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001926 case OP_DOUBLE_TO_FLOAT:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001927 case OP_FLOAT_TO_DOUBLE:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001928 case OP_INT_TO_DOUBLE:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001929 case OP_FLOAT_TO_LONG:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001930 case OP_LONG_TO_FLOAT:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001931 case OP_DOUBLE_TO_LONG:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001932 case OP_LONG_TO_DOUBLE:
Ben Cheng5d90c202009-11-22 23:31:11 -08001933 return genConversion(cUnit, mir);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001934 case OP_NEG_INT:
1935 case OP_NOT_INT:
Ben Cheng5d90c202009-11-22 23:31:11 -08001936 return genArithOpInt(cUnit, mir, rlDest, rlSrc, rlSrc);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001937 case OP_NEG_LONG:
1938 case OP_NOT_LONG:
Ben Cheng5d90c202009-11-22 23:31:11 -08001939 return genArithOpLong(cUnit, mir, rlDest, rlSrc, rlSrc);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001940 case OP_NEG_FLOAT:
Ben Cheng5d90c202009-11-22 23:31:11 -08001941 return genArithOpFloat(cUnit, mir, rlDest, rlSrc, rlSrc);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001942 case OP_NEG_DOUBLE:
Ben Cheng5d90c202009-11-22 23:31:11 -08001943 return genArithOpDouble(cUnit, mir, rlDest, rlSrc, rlSrc);
Bill Buzbee1465db52009-09-23 17:17:35 -07001944 case OP_MOVE_WIDE:
1945 storeValueWide(cUnit, rlDest, rlSrc);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001946 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07001947 case OP_INT_TO_LONG:
Bill Buzbeec6f10662010-02-09 11:16:15 -08001948 rlSrc = dvmCompilerUpdateLoc(cUnit, rlSrc);
1949 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Bill Buzbee964a7b02010-01-28 12:54:19 -08001950 //TUNING: shouldn't loadValueDirect already check for phys reg?
Bill Buzbee1465db52009-09-23 17:17:35 -07001951 if (rlSrc.location == kLocPhysReg) {
1952 genRegCopy(cUnit, rlResult.lowReg, rlSrc.lowReg);
1953 } else {
1954 loadValueDirect(cUnit, rlSrc, rlResult.lowReg);
1955 }
1956 opRegRegImm(cUnit, kOpAsr, rlResult.highReg,
1957 rlResult.lowReg, 31);
1958 storeValueWide(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001959 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07001960 case OP_LONG_TO_INT:
Bill Buzbeec6f10662010-02-09 11:16:15 -08001961 rlSrc = dvmCompilerUpdateLocWide(cUnit, rlSrc);
1962 rlSrc = dvmCompilerWideToNarrow(cUnit, rlSrc);
Bill Buzbee1465db52009-09-23 17:17:35 -07001963 // Intentional fallthrough
Ben Chengba4fc8b2009-06-01 13:00:29 -07001964 case OP_MOVE:
1965 case OP_MOVE_OBJECT:
Bill Buzbee1465db52009-09-23 17:17:35 -07001966 storeValue(cUnit, rlDest, rlSrc);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001967 break;
1968 case OP_INT_TO_BYTE:
Bill Buzbee1465db52009-09-23 17:17:35 -07001969 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
Bill Buzbeec6f10662010-02-09 11:16:15 -08001970 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Bill Buzbee1465db52009-09-23 17:17:35 -07001971 opRegReg(cUnit, kOp2Byte, rlResult.lowReg, rlSrc.lowReg);
1972 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001973 break;
1974 case OP_INT_TO_SHORT:
Bill Buzbee1465db52009-09-23 17:17:35 -07001975 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
Bill Buzbeec6f10662010-02-09 11:16:15 -08001976 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Bill Buzbee1465db52009-09-23 17:17:35 -07001977 opRegReg(cUnit, kOp2Short, rlResult.lowReg, rlSrc.lowReg);
1978 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001979 break;
1980 case OP_INT_TO_CHAR:
Bill Buzbee1465db52009-09-23 17:17:35 -07001981 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
Bill Buzbeec6f10662010-02-09 11:16:15 -08001982 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Bill Buzbee1465db52009-09-23 17:17:35 -07001983 opRegReg(cUnit, kOp2Char, rlResult.lowReg, rlSrc.lowReg);
1984 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001985 break;
1986 case OP_ARRAY_LENGTH: {
1987 int lenOffset = offsetof(ArrayObject, length);
Bill Buzbee1465db52009-09-23 17:17:35 -07001988 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
1989 genNullCheck(cUnit, rlSrc.sRegLow, rlSrc.lowReg,
1990 mir->offset, NULL);
Bill Buzbeec6f10662010-02-09 11:16:15 -08001991 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Bill Buzbee1465db52009-09-23 17:17:35 -07001992 loadWordDisp(cUnit, rlSrc.lowReg, lenOffset,
1993 rlResult.lowReg);
1994 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001995 break;
1996 }
1997 default:
1998 return true;
1999 }
2000 return false;
2001}
2002
2003static bool handleFmt21s(CompilationUnit *cUnit, MIR *mir)
2004{
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002005 Opcode dalvikOpcode = mir->dalvikInsn.opcode;
Bill Buzbee1465db52009-09-23 17:17:35 -07002006 RegLocation rlDest;
2007 RegLocation rlResult;
2008 int BBBB = mir->dalvikInsn.vB;
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002009 if (dalvikOpcode == OP_CONST_WIDE_16) {
Bill Buzbeec6f10662010-02-09 11:16:15 -08002010 rlDest = dvmCompilerGetDestWide(cUnit, mir, 0, 1);
2011 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Ben Chengbd1326d2010-04-02 15:04:53 -07002012 loadConstantNoClobber(cUnit, rlResult.lowReg, BBBB);
Bill Buzbee964a7b02010-01-28 12:54:19 -08002013 //TUNING: do high separately to avoid load dependency
Bill Buzbee1465db52009-09-23 17:17:35 -07002014 opRegRegImm(cUnit, kOpAsr, rlResult.highReg, rlResult.lowReg, 31);
2015 storeValueWide(cUnit, rlDest, rlResult);
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002016 } else if (dalvikOpcode == OP_CONST_16) {
Bill Buzbeec6f10662010-02-09 11:16:15 -08002017 rlDest = dvmCompilerGetDest(cUnit, mir, 0);
2018 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kAnyReg, true);
Ben Chengbd1326d2010-04-02 15:04:53 -07002019 loadConstantNoClobber(cUnit, rlResult.lowReg, BBBB);
Bill Buzbee1465db52009-09-23 17:17:35 -07002020 storeValue(cUnit, rlDest, rlResult);
2021 } else
Ben Chengba4fc8b2009-06-01 13:00:29 -07002022 return true;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002023 return false;
2024}
2025
2026/* Compare agaist zero */
2027static bool handleFmt21t(CompilationUnit *cUnit, MIR *mir, BasicBlock *bb,
Bill Buzbee89efc3d2009-07-28 11:22:22 -07002028 ArmLIR *labelList)
Ben Chengba4fc8b2009-06-01 13:00:29 -07002029{
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002030 Opcode dalvikOpcode = mir->dalvikInsn.opcode;
Bill Buzbee89efc3d2009-07-28 11:22:22 -07002031 ArmConditionCode cond;
Ben Cheng7ab74e12011-02-03 14:02:06 -08002032 /* backward branch? */
2033 bool backwardBranch = (bb->taken->startOffset <= mir->offset);
2034
2035 if (backwardBranch && gDvmJit.genSuspendPoll) {
2036 genSuspendPoll(cUnit, mir);
2037 }
2038
Bill Buzbeec6f10662010-02-09 11:16:15 -08002039 RegLocation rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07002040 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
Ben Cheng7ab74e12011-02-03 14:02:06 -08002041
Bill Buzbee1465db52009-09-23 17:17:35 -07002042 opRegImm(cUnit, kOpCmp, rlSrc.lowReg, 0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002043
Bill Buzbee270c1d62009-08-13 16:58:07 -07002044//TUNING: break this out to allow use of Thumb2 CB[N]Z
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002045 switch (dalvikOpcode) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07002046 case OP_IF_EQZ:
Bill Buzbee1465db52009-09-23 17:17:35 -07002047 cond = kArmCondEq;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002048 break;
2049 case OP_IF_NEZ:
Bill Buzbee1465db52009-09-23 17:17:35 -07002050 cond = kArmCondNe;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002051 break;
2052 case OP_IF_LTZ:
Bill Buzbee1465db52009-09-23 17:17:35 -07002053 cond = kArmCondLt;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002054 break;
2055 case OP_IF_GEZ:
Bill Buzbee1465db52009-09-23 17:17:35 -07002056 cond = kArmCondGe;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002057 break;
2058 case OP_IF_GTZ:
Bill Buzbee1465db52009-09-23 17:17:35 -07002059 cond = kArmCondGt;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002060 break;
2061 case OP_IF_LEZ:
Bill Buzbee1465db52009-09-23 17:17:35 -07002062 cond = kArmCondLe;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002063 break;
2064 default:
2065 cond = 0;
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002066 LOGE("Unexpected opcode (%d) for Fmt21t\n", dalvikOpcode);
Bill Buzbeefc519dc2010-03-06 23:30:57 -08002067 dvmCompilerAbort(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002068 }
2069 genConditionalBranch(cUnit, cond, &labelList[bb->taken->id]);
2070 /* This mostly likely will be optimized away in a later phase */
2071 genUnconditionalBranch(cUnit, &labelList[bb->fallThrough->id]);
2072 return false;
2073}
2074
Elliott Hughesb4c05972010-02-24 16:36:18 -08002075static bool isPowerOfTwo(int x)
2076{
2077 return (x & (x - 1)) == 0;
2078}
2079
2080// Returns true if no more than two bits are set in 'x'.
2081static bool isPopCountLE2(unsigned int x)
2082{
2083 x &= x - 1;
2084 return (x & (x - 1)) == 0;
2085}
2086
2087// Returns the index of the lowest set bit in 'x'.
2088static int lowestSetBit(unsigned int x) {
2089 int bit_posn = 0;
2090 while ((x & 0xf) == 0) {
2091 bit_posn += 4;
2092 x >>= 4;
Bill Buzbee78cb0e22010-02-11 14:04:53 -08002093 }
Elliott Hughesb4c05972010-02-24 16:36:18 -08002094 while ((x & 1) == 0) {
2095 bit_posn++;
2096 x >>= 1;
2097 }
2098 return bit_posn;
2099}
2100
Elliott Hughes672511b2010-04-26 17:40:13 -07002101// Returns true if it added instructions to 'cUnit' to divide 'rlSrc' by 'lit'
2102// and store the result in 'rlDest'.
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002103static bool handleEasyDivide(CompilationUnit *cUnit, Opcode dalvikOpcode,
Elliott Hughes672511b2010-04-26 17:40:13 -07002104 RegLocation rlSrc, RegLocation rlDest, int lit)
2105{
2106 if (lit < 2 || !isPowerOfTwo(lit)) {
2107 return false;
2108 }
2109 int k = lowestSetBit(lit);
2110 if (k >= 30) {
2111 // Avoid special cases.
2112 return false;
2113 }
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002114 bool div = (dalvikOpcode == OP_DIV_INT_LIT8 || dalvikOpcode == OP_DIV_INT_LIT16);
Elliott Hughes672511b2010-04-26 17:40:13 -07002115 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
2116 RegLocation rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Elliott Hughes9c457022010-04-28 16:15:38 -07002117 if (div) {
2118 int tReg = dvmCompilerAllocTemp(cUnit);
2119 if (lit == 2) {
2120 // Division by 2 is by far the most common division by constant.
2121 opRegRegImm(cUnit, kOpLsr, tReg, rlSrc.lowReg, 32 - k);
2122 opRegRegReg(cUnit, kOpAdd, tReg, tReg, rlSrc.lowReg);
2123 opRegRegImm(cUnit, kOpAsr, rlResult.lowReg, tReg, k);
2124 } else {
2125 opRegRegImm(cUnit, kOpAsr, tReg, rlSrc.lowReg, 31);
2126 opRegRegImm(cUnit, kOpLsr, tReg, tReg, 32 - k);
2127 opRegRegReg(cUnit, kOpAdd, tReg, tReg, rlSrc.lowReg);
2128 opRegRegImm(cUnit, kOpAsr, rlResult.lowReg, tReg, k);
2129 }
Elliott Hughes672511b2010-04-26 17:40:13 -07002130 } else {
Elliott Hughes9c457022010-04-28 16:15:38 -07002131 int cReg = dvmCompilerAllocTemp(cUnit);
2132 loadConstant(cUnit, cReg, lit - 1);
2133 int tReg1 = dvmCompilerAllocTemp(cUnit);
2134 int tReg2 = dvmCompilerAllocTemp(cUnit);
2135 if (lit == 2) {
2136 opRegRegImm(cUnit, kOpLsr, tReg1, rlSrc.lowReg, 32 - k);
2137 opRegRegReg(cUnit, kOpAdd, tReg2, tReg1, rlSrc.lowReg);
2138 opRegRegReg(cUnit, kOpAnd, tReg2, tReg2, cReg);
2139 opRegRegReg(cUnit, kOpSub, rlResult.lowReg, tReg2, tReg1);
2140 } else {
2141 opRegRegImm(cUnit, kOpAsr, tReg1, rlSrc.lowReg, 31);
2142 opRegRegImm(cUnit, kOpLsr, tReg1, tReg1, 32 - k);
2143 opRegRegReg(cUnit, kOpAdd, tReg2, tReg1, rlSrc.lowReg);
2144 opRegRegReg(cUnit, kOpAnd, tReg2, tReg2, cReg);
2145 opRegRegReg(cUnit, kOpSub, rlResult.lowReg, tReg2, tReg1);
2146 }
Elliott Hughes672511b2010-04-26 17:40:13 -07002147 }
2148 storeValue(cUnit, rlDest, rlResult);
2149 return true;
2150}
2151
Elliott Hughesb4c05972010-02-24 16:36:18 -08002152// Returns true if it added instructions to 'cUnit' to multiply 'rlSrc' by 'lit'
2153// and store the result in 'rlDest'.
2154static bool handleEasyMultiply(CompilationUnit *cUnit,
2155 RegLocation rlSrc, RegLocation rlDest, int lit)
2156{
2157 // Can we simplify this multiplication?
2158 bool powerOfTwo = false;
2159 bool popCountLE2 = false;
2160 bool powerOfTwoMinusOne = false;
2161 if (lit < 2) {
2162 // Avoid special cases.
2163 return false;
2164 } else if (isPowerOfTwo(lit)) {
2165 powerOfTwo = true;
2166 } else if (isPopCountLE2(lit)) {
2167 popCountLE2 = true;
2168 } else if (isPowerOfTwo(lit + 1)) {
2169 powerOfTwoMinusOne = true;
2170 } else {
2171 return false;
2172 }
2173 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
2174 RegLocation rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
2175 if (powerOfTwo) {
2176 // Shift.
2177 opRegRegImm(cUnit, kOpLsl, rlResult.lowReg, rlSrc.lowReg,
2178 lowestSetBit(lit));
2179 } else if (popCountLE2) {
2180 // Shift and add and shift.
2181 int firstBit = lowestSetBit(lit);
2182 int secondBit = lowestSetBit(lit ^ (1 << firstBit));
2183 genMultiplyByTwoBitMultiplier(cUnit, rlSrc, rlResult, lit,
2184 firstBit, secondBit);
2185 } else {
2186 // Reverse subtract: (src << (shift + 1)) - src.
2187 assert(powerOfTwoMinusOne);
2188 // TODO: rsb dst, src, src lsl#lowestSetBit(lit + 1)
2189 int tReg = dvmCompilerAllocTemp(cUnit);
2190 opRegRegImm(cUnit, kOpLsl, tReg, rlSrc.lowReg, lowestSetBit(lit + 1));
2191 opRegRegReg(cUnit, kOpSub, rlResult.lowReg, tReg, rlSrc.lowReg);
2192 }
2193 storeValue(cUnit, rlDest, rlResult);
2194 return true;
Bill Buzbee78cb0e22010-02-11 14:04:53 -08002195}
2196
Ben Chengba4fc8b2009-06-01 13:00:29 -07002197static bool handleFmt22b_Fmt22s(CompilationUnit *cUnit, MIR *mir)
2198{
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002199 Opcode dalvikOpcode = mir->dalvikInsn.opcode;
Bill Buzbeec6f10662010-02-09 11:16:15 -08002200 RegLocation rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
2201 RegLocation rlDest = dvmCompilerGetDest(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07002202 RegLocation rlResult;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002203 int lit = mir->dalvikInsn.vC;
Ben Cheng4f489172009-09-27 17:08:35 -07002204 OpKind op = 0; /* Make gcc happy */
Bill Buzbee1465db52009-09-23 17:17:35 -07002205 int shiftOp = false;
2206 bool isDiv = false;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002207
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002208 switch (dalvikOpcode) {
Bill Buzbee1465db52009-09-23 17:17:35 -07002209 case OP_RSUB_INT_LIT8:
2210 case OP_RSUB_INT: {
2211 int tReg;
2212 //TUNING: add support for use of Arm rsub op
2213 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
Bill Buzbeec6f10662010-02-09 11:16:15 -08002214 tReg = dvmCompilerAllocTemp(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07002215 loadConstant(cUnit, tReg, lit);
Bill Buzbeec6f10662010-02-09 11:16:15 -08002216 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Bill Buzbee1465db52009-09-23 17:17:35 -07002217 opRegRegReg(cUnit, kOpSub, rlResult.lowReg,
2218 tReg, rlSrc.lowReg);
2219 storeValue(cUnit, rlDest, rlResult);
2220 return false;
2221 break;
2222 }
2223
Ben Chengba4fc8b2009-06-01 13:00:29 -07002224 case OP_ADD_INT_LIT8:
2225 case OP_ADD_INT_LIT16:
Bill Buzbee1465db52009-09-23 17:17:35 -07002226 op = kOpAdd;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002227 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002228 case OP_MUL_INT_LIT8:
Bill Buzbee78cb0e22010-02-11 14:04:53 -08002229 case OP_MUL_INT_LIT16: {
Elliott Hughesb4c05972010-02-24 16:36:18 -08002230 if (handleEasyMultiply(cUnit, rlSrc, rlDest, lit)) {
2231 return false;
Bill Buzbee78cb0e22010-02-11 14:04:53 -08002232 }
Elliott Hughesb4c05972010-02-24 16:36:18 -08002233 op = kOpMul;
Bill Buzbee1465db52009-09-23 17:17:35 -07002234 break;
Bill Buzbee78cb0e22010-02-11 14:04:53 -08002235 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07002236 case OP_AND_INT_LIT8:
2237 case OP_AND_INT_LIT16:
Bill Buzbee1465db52009-09-23 17:17:35 -07002238 op = kOpAnd;
2239 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002240 case OP_OR_INT_LIT8:
2241 case OP_OR_INT_LIT16:
Bill Buzbee1465db52009-09-23 17:17:35 -07002242 op = kOpOr;
2243 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002244 case OP_XOR_INT_LIT8:
2245 case OP_XOR_INT_LIT16:
Bill Buzbee1465db52009-09-23 17:17:35 -07002246 op = kOpXor;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002247 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002248 case OP_SHL_INT_LIT8:
Bill Buzbee0e605272009-12-01 14:28:05 -08002249 lit &= 31;
Bill Buzbee1465db52009-09-23 17:17:35 -07002250 shiftOp = true;
2251 op = kOpLsl;
2252 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002253 case OP_SHR_INT_LIT8:
Bill Buzbee0e605272009-12-01 14:28:05 -08002254 lit &= 31;
Bill Buzbee1465db52009-09-23 17:17:35 -07002255 shiftOp = true;
2256 op = kOpAsr;
2257 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002258 case OP_USHR_INT_LIT8:
Bill Buzbee0e605272009-12-01 14:28:05 -08002259 lit &= 31;
Bill Buzbee1465db52009-09-23 17:17:35 -07002260 shiftOp = true;
2261 op = kOpLsr;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002262 break;
2263
2264 case OP_DIV_INT_LIT8:
2265 case OP_DIV_INT_LIT16:
Ben Chengba4fc8b2009-06-01 13:00:29 -07002266 case OP_REM_INT_LIT8:
2267 case OP_REM_INT_LIT16:
2268 if (lit == 0) {
2269 /* Let the interpreter deal with div by 0 */
2270 genInterpSingleStep(cUnit, mir);
2271 return false;
2272 }
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002273 if (handleEasyDivide(cUnit, dalvikOpcode, rlSrc, rlDest, lit)) {
Elliott Hughes672511b2010-04-26 17:40:13 -07002274 return false;
2275 }
Bill Buzbeec6f10662010-02-09 11:16:15 -08002276 dvmCompilerFlushAllRegs(cUnit); /* Everything to home location */
Bill Buzbee1465db52009-09-23 17:17:35 -07002277 loadValueDirectFixed(cUnit, rlSrc, r0);
Bill Buzbeec6f10662010-02-09 11:16:15 -08002278 dvmCompilerClobber(cUnit, r0);
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002279 if ((dalvikOpcode == OP_DIV_INT_LIT8) ||
2280 (dalvikOpcode == OP_DIV_INT_LIT16)) {
Ben Chengbd1326d2010-04-02 15:04:53 -07002281 LOAD_FUNC_ADDR(cUnit, r2, (int)__aeabi_idiv);
Bill Buzbee1465db52009-09-23 17:17:35 -07002282 isDiv = true;
2283 } else {
Ben Chengbd1326d2010-04-02 15:04:53 -07002284 LOAD_FUNC_ADDR(cUnit, r2, (int)__aeabi_idivmod);
Bill Buzbee1465db52009-09-23 17:17:35 -07002285 isDiv = false;
2286 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07002287 loadConstant(cUnit, r1, lit);
Bill Buzbee1465db52009-09-23 17:17:35 -07002288 opReg(cUnit, kOpBlx, r2);
Elliott Hughes6a555132010-02-25 15:41:42 -08002289 dvmCompilerClobberCallRegs(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07002290 if (isDiv)
Bill Buzbeec6f10662010-02-09 11:16:15 -08002291 rlResult = dvmCompilerGetReturn(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07002292 else
Bill Buzbeec6f10662010-02-09 11:16:15 -08002293 rlResult = dvmCompilerGetReturnAlt(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07002294 storeValue(cUnit, rlDest, rlResult);
2295 return false;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002296 break;
2297 default:
2298 return true;
2299 }
Bill Buzbee1465db52009-09-23 17:17:35 -07002300 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
Bill Buzbeec6f10662010-02-09 11:16:15 -08002301 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Bill Buzbee1465db52009-09-23 17:17:35 -07002302 // Avoid shifts by literal 0 - no support in Thumb. Change to copy
2303 if (shiftOp && (lit == 0)) {
2304 genRegCopy(cUnit, rlResult.lowReg, rlSrc.lowReg);
2305 } else {
2306 opRegRegImm(cUnit, op, rlResult.lowReg, rlSrc.lowReg, lit);
2307 }
2308 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002309 return false;
2310}
2311
jeffhao71eee1f2011-01-04 14:18:54 -08002312static bool handleFmt22c_Fmt52c(CompilationUnit *cUnit, MIR *mir)
Ben Chengba4fc8b2009-06-01 13:00:29 -07002313{
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002314 Opcode dalvikOpcode = mir->dalvikInsn.opcode;
buzbee4d92e682010-07-29 15:24:14 -07002315 int fieldOffset = -1;
buzbeeecf8f6e2010-07-20 14:53:42 -07002316 bool isVolatile = false;
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002317 switch (dalvikOpcode) {
buzbee4d92e682010-07-29 15:24:14 -07002318 /*
2319 * Wide volatiles currently handled via single step.
2320 * Add them here if generating in-line code.
2321 * case OP_IGET_WIDE_VOLATILE:
2322 * case OP_IPUT_WIDE_VOLATILE:
2323 */
2324 case OP_IGET:
2325 case OP_IGET_VOLATILE:
jeffhao71eee1f2011-01-04 14:18:54 -08002326 case OP_IGET_JUMBO:
buzbee4d92e682010-07-29 15:24:14 -07002327 case OP_IGET_WIDE:
jeffhao71eee1f2011-01-04 14:18:54 -08002328 case OP_IGET_WIDE_JUMBO:
buzbee4d92e682010-07-29 15:24:14 -07002329 case OP_IGET_OBJECT:
2330 case OP_IGET_OBJECT_VOLATILE:
jeffhao71eee1f2011-01-04 14:18:54 -08002331 case OP_IGET_OBJECT_JUMBO:
buzbee4d92e682010-07-29 15:24:14 -07002332 case OP_IGET_BOOLEAN:
jeffhao71eee1f2011-01-04 14:18:54 -08002333 case OP_IGET_BOOLEAN_JUMBO:
buzbee4d92e682010-07-29 15:24:14 -07002334 case OP_IGET_BYTE:
jeffhao71eee1f2011-01-04 14:18:54 -08002335 case OP_IGET_BYTE_JUMBO:
buzbee4d92e682010-07-29 15:24:14 -07002336 case OP_IGET_CHAR:
jeffhao71eee1f2011-01-04 14:18:54 -08002337 case OP_IGET_CHAR_JUMBO:
buzbee4d92e682010-07-29 15:24:14 -07002338 case OP_IGET_SHORT:
jeffhao71eee1f2011-01-04 14:18:54 -08002339 case OP_IGET_SHORT_JUMBO:
buzbee4d92e682010-07-29 15:24:14 -07002340 case OP_IPUT:
2341 case OP_IPUT_VOLATILE:
jeffhao71eee1f2011-01-04 14:18:54 -08002342 case OP_IPUT_JUMBO:
buzbee4d92e682010-07-29 15:24:14 -07002343 case OP_IPUT_WIDE:
jeffhao71eee1f2011-01-04 14:18:54 -08002344 case OP_IPUT_WIDE_JUMBO:
buzbee4d92e682010-07-29 15:24:14 -07002345 case OP_IPUT_OBJECT:
2346 case OP_IPUT_OBJECT_VOLATILE:
jeffhao71eee1f2011-01-04 14:18:54 -08002347 case OP_IPUT_OBJECT_JUMBO:
buzbee4d92e682010-07-29 15:24:14 -07002348 case OP_IPUT_BOOLEAN:
jeffhao71eee1f2011-01-04 14:18:54 -08002349 case OP_IPUT_BOOLEAN_JUMBO:
buzbee4d92e682010-07-29 15:24:14 -07002350 case OP_IPUT_BYTE:
jeffhao71eee1f2011-01-04 14:18:54 -08002351 case OP_IPUT_BYTE_JUMBO:
buzbee4d92e682010-07-29 15:24:14 -07002352 case OP_IPUT_CHAR:
jeffhao71eee1f2011-01-04 14:18:54 -08002353 case OP_IPUT_CHAR_JUMBO:
2354 case OP_IPUT_SHORT:
2355 case OP_IPUT_SHORT_JUMBO: {
Ben Cheng7a2697d2010-06-07 13:44:23 -07002356 const Method *method = (mir->OptimizationFlags & MIR_CALLEE) ?
2357 mir->meta.calleeMethod : cUnit->method;
buzbee4d92e682010-07-29 15:24:14 -07002358 Field *fieldPtr =
Ben Cheng7a2697d2010-06-07 13:44:23 -07002359 method->clazz->pDvmDex->pResFields[mir->dalvikInsn.vC];
Ben Chengba4fc8b2009-06-01 13:00:29 -07002360
buzbee4d92e682010-07-29 15:24:14 -07002361 if (fieldPtr == NULL) {
2362 LOGE("Unexpected null instance field");
2363 dvmAbort();
2364 }
2365 isVolatile = dvmIsVolatileField(fieldPtr);
2366 fieldOffset = ((InstField *)fieldPtr)->byteOffset;
2367 break;
Ben Chengdd6e8702010-05-07 13:05:47 -07002368 }
buzbee4d92e682010-07-29 15:24:14 -07002369 default:
2370 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002371 }
buzbee4d92e682010-07-29 15:24:14 -07002372
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002373 switch (dalvikOpcode) {
jeffhao71eee1f2011-01-04 14:18:54 -08002374 case OP_NEW_ARRAY:
2375 case OP_NEW_ARRAY_JUMBO: {
Bill Buzbee1465db52009-09-23 17:17:35 -07002376 // Generates a call - use explicit registers
Bill Buzbeec6f10662010-02-09 11:16:15 -08002377 RegLocation rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
2378 RegLocation rlDest = dvmCompilerGetDest(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07002379 RegLocation rlResult;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002380 void *classPtr = (void*)
2381 (cUnit->method->clazz->pDvmDex->pResClasses[mir->dalvikInsn.vC]);
Ben Chengdd6e8702010-05-07 13:05:47 -07002382
2383 if (classPtr == NULL) {
2384 LOGE("Unexpected null class");
2385 dvmAbort();
2386 }
2387
Bill Buzbeec6f10662010-02-09 11:16:15 -08002388 dvmCompilerFlushAllRegs(cUnit); /* Everything to home location */
Bill Buzbee1465db52009-09-23 17:17:35 -07002389 genExportPC(cUnit, mir);
2390 loadValueDirectFixed(cUnit, rlSrc, r1); /* Len */
Ben Chengba4fc8b2009-06-01 13:00:29 -07002391 loadConstant(cUnit, r0, (int) classPtr );
Ben Chengbd1326d2010-04-02 15:04:53 -07002392 LOAD_FUNC_ADDR(cUnit, r3, (int)dvmAllocArrayByClass);
Ben Cheng4f489172009-09-27 17:08:35 -07002393 /*
2394 * "len < 0": bail to the interpreter to re-execute the
2395 * instruction
2396 */
Carl Shapiroe3c01da2010-05-20 22:54:18 -07002397 genRegImmCheck(cUnit, kArmCondMi, r1, 0, mir->offset, NULL);
Bill Buzbee270c1d62009-08-13 16:58:07 -07002398 loadConstant(cUnit, r2, ALLOC_DONT_TRACK);
Bill Buzbee1465db52009-09-23 17:17:35 -07002399 opReg(cUnit, kOpBlx, r3);
Elliott Hughes6a555132010-02-25 15:41:42 -08002400 dvmCompilerClobberCallRegs(cUnit);
Ben Cheng4f489172009-09-27 17:08:35 -07002401 /* generate a branch over if allocation is successful */
buzbee8f8109a2010-08-31 10:16:35 -07002402 ArmLIR *branchOver = genCmpImmBranch(cUnit, kArmCondNe, r0, 0);
Ben Cheng4f489172009-09-27 17:08:35 -07002403 /*
2404 * OOM exception needs to be thrown here and cannot re-execute
2405 */
2406 loadConstant(cUnit, r0,
2407 (int) (cUnit->method->insns + mir->offset));
2408 genDispatchToHandler(cUnit, TEMPLATE_THROW_EXCEPTION_COMMON);
2409 /* noreturn */
2410
Bill Buzbee1465db52009-09-23 17:17:35 -07002411 ArmLIR *target = newLIR0(cUnit, kArmPseudoTargetLabel);
Ben Cheng4f489172009-09-27 17:08:35 -07002412 target->defMask = ENCODE_ALL;
2413 branchOver->generic.target = (LIR *) target;
Bill Buzbeec6f10662010-02-09 11:16:15 -08002414 rlResult = dvmCompilerGetReturn(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07002415 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002416 break;
2417 }
jeffhao71eee1f2011-01-04 14:18:54 -08002418 case OP_INSTANCE_OF:
2419 case OP_INSTANCE_OF_JUMBO: {
Bill Buzbee1465db52009-09-23 17:17:35 -07002420 // May generate a call - use explicit registers
Bill Buzbeec6f10662010-02-09 11:16:15 -08002421 RegLocation rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
2422 RegLocation rlDest = dvmCompilerGetDest(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07002423 RegLocation rlResult;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002424 ClassObject *classPtr =
2425 (cUnit->method->clazz->pDvmDex->pResClasses[mir->dalvikInsn.vC]);
Bill Buzbee480e6782010-01-27 15:43:08 -08002426 /*
2427 * Note: It is possible that classPtr is NULL at this point,
2428 * even though this instruction has been successfully interpreted.
2429 * If the previous interpretation had a null source, the
2430 * interpreter would not have bothered to resolve the clazz.
2431 * Bail out to the interpreter in this case, and log it
2432 * so that we can tell if it happens frequently.
2433 */
2434 if (classPtr == NULL) {
2435 LOGD("null clazz in OP_INSTANCE_OF, single-stepping");
2436 genInterpSingleStep(cUnit, mir);
2437 break;
2438 }
Bill Buzbeec6f10662010-02-09 11:16:15 -08002439 dvmCompilerFlushAllRegs(cUnit); /* Everything to home location */
Bill Buzbee1465db52009-09-23 17:17:35 -07002440 loadValueDirectFixed(cUnit, rlSrc, r0); /* Ref */
Ben Chengba4fc8b2009-06-01 13:00:29 -07002441 loadConstant(cUnit, r2, (int) classPtr );
Ben Cheng752c7942009-06-22 10:50:07 -07002442 /* When taken r0 has NULL which can be used for store directly */
buzbee8f8109a2010-08-31 10:16:35 -07002443 ArmLIR *branch1 = genCmpImmBranch(cUnit, kArmCondEq, r0, 0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002444 /* r1 now contains object->clazz */
Bill Buzbee270c1d62009-08-13 16:58:07 -07002445 loadWordDisp(cUnit, r0, offsetof(Object, clazz), r1);
Bill Buzbee1465db52009-09-23 17:17:35 -07002446 /* r1 now contains object->clazz */
Ben Chengbd1326d2010-04-02 15:04:53 -07002447 LOAD_FUNC_ADDR(cUnit, r3, (int)dvmInstanceofNonTrivial);
Ben Cheng752c7942009-06-22 10:50:07 -07002448 loadConstant(cUnit, r0, 1); /* Assume true */
Bill Buzbee1465db52009-09-23 17:17:35 -07002449 opRegReg(cUnit, kOpCmp, r1, r2);
2450 ArmLIR *branch2 = opCondBranch(cUnit, kArmCondEq);
2451 genRegCopy(cUnit, r0, r1);
2452 genRegCopy(cUnit, r1, r2);
2453 opReg(cUnit, kOpBlx, r3);
Elliott Hughes6a555132010-02-25 15:41:42 -08002454 dvmCompilerClobberCallRegs(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002455 /* branch target here */
Bill Buzbee1465db52009-09-23 17:17:35 -07002456 ArmLIR *target = newLIR0(cUnit, kArmPseudoTargetLabel);
Ben Chengd7d426a2009-09-22 11:23:36 -07002457 target->defMask = ENCODE_ALL;
Bill Buzbeec6f10662010-02-09 11:16:15 -08002458 rlResult = dvmCompilerGetReturn(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07002459 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002460 branch1->generic.target = (LIR *)target;
2461 branch2->generic.target = (LIR *)target;
2462 break;
2463 }
2464 case OP_IGET_WIDE:
jeffhao71eee1f2011-01-04 14:18:54 -08002465 case OP_IGET_WIDE_JUMBO:
Ben Chengba4fc8b2009-06-01 13:00:29 -07002466 genIGetWide(cUnit, mir, fieldOffset);
2467 break;
buzbeeecf8f6e2010-07-20 14:53:42 -07002468 case OP_IGET_VOLATILE:
2469 case OP_IGET_OBJECT_VOLATILE:
2470 isVolatile = true;
2471 // NOTE: intentional fallthrough
Ben Chengba4fc8b2009-06-01 13:00:29 -07002472 case OP_IGET:
jeffhao71eee1f2011-01-04 14:18:54 -08002473 case OP_IGET_JUMBO:
Ben Chengba4fc8b2009-06-01 13:00:29 -07002474 case OP_IGET_OBJECT:
jeffhao71eee1f2011-01-04 14:18:54 -08002475 case OP_IGET_OBJECT_JUMBO:
Ben Chengba4fc8b2009-06-01 13:00:29 -07002476 case OP_IGET_BOOLEAN:
jeffhao71eee1f2011-01-04 14:18:54 -08002477 case OP_IGET_BOOLEAN_JUMBO:
Ben Chengba4fc8b2009-06-01 13:00:29 -07002478 case OP_IGET_BYTE:
jeffhao71eee1f2011-01-04 14:18:54 -08002479 case OP_IGET_BYTE_JUMBO:
Ben Chengba4fc8b2009-06-01 13:00:29 -07002480 case OP_IGET_CHAR:
jeffhao71eee1f2011-01-04 14:18:54 -08002481 case OP_IGET_CHAR_JUMBO:
Ben Chengba4fc8b2009-06-01 13:00:29 -07002482 case OP_IGET_SHORT:
jeffhao71eee1f2011-01-04 14:18:54 -08002483 case OP_IGET_SHORT_JUMBO:
buzbee3272e2f2010-09-09 14:07:01 -07002484 genIGet(cUnit, mir, kWord, fieldOffset, isVolatile);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002485 break;
2486 case OP_IPUT_WIDE:
jeffhao71eee1f2011-01-04 14:18:54 -08002487 case OP_IPUT_WIDE_JUMBO:
Ben Chengba4fc8b2009-06-01 13:00:29 -07002488 genIPutWide(cUnit, mir, fieldOffset);
2489 break;
2490 case OP_IPUT:
jeffhao71eee1f2011-01-04 14:18:54 -08002491 case OP_IPUT_JUMBO:
buzbee3272e2f2010-09-09 14:07:01 -07002492 case OP_IPUT_BOOLEAN:
jeffhao71eee1f2011-01-04 14:18:54 -08002493 case OP_IPUT_BOOLEAN_JUMBO:
2494 case OP_IPUT_BYTE:
2495 case OP_IPUT_BYTE_JUMBO:
2496 case OP_IPUT_CHAR:
2497 case OP_IPUT_CHAR_JUMBO:
2498 case OP_IPUT_SHORT:
2499 case OP_IPUT_SHORT_JUMBO:
buzbeeecf8f6e2010-07-20 14:53:42 -07002500 genIPut(cUnit, mir, kWord, fieldOffset, false, isVolatile);
buzbee919eb062010-07-12 12:59:22 -07002501 break;
buzbee4d92e682010-07-29 15:24:14 -07002502 case OP_IPUT_VOLATILE:
buzbeeecf8f6e2010-07-20 14:53:42 -07002503 case OP_IPUT_OBJECT_VOLATILE:
2504 isVolatile = true;
2505 // NOTE: intentional fallthrough
Ben Chengba4fc8b2009-06-01 13:00:29 -07002506 case OP_IPUT_OBJECT:
jeffhao71eee1f2011-01-04 14:18:54 -08002507 case OP_IPUT_OBJECT_JUMBO:
buzbeeecf8f6e2010-07-20 14:53:42 -07002508 genIPut(cUnit, mir, kWord, fieldOffset, true, isVolatile);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002509 break;
Bill Buzbeeb16344a2010-03-15 17:19:12 -07002510 case OP_IGET_WIDE_VOLATILE:
2511 case OP_IPUT_WIDE_VOLATILE:
Bill Buzbeeb16344a2010-03-15 17:19:12 -07002512 genInterpSingleStep(cUnit, mir);
2513 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002514 default:
2515 return true;
2516 }
2517 return false;
2518}
2519
2520static bool handleFmt22cs(CompilationUnit *cUnit, MIR *mir)
2521{
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002522 Opcode dalvikOpcode = mir->dalvikInsn.opcode;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002523 int fieldOffset = mir->dalvikInsn.vC;
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002524 switch (dalvikOpcode) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07002525 case OP_IGET_QUICK:
2526 case OP_IGET_OBJECT_QUICK:
buzbeeecf8f6e2010-07-20 14:53:42 -07002527 genIGet(cUnit, mir, kWord, fieldOffset, false);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002528 break;
2529 case OP_IPUT_QUICK:
buzbeeecf8f6e2010-07-20 14:53:42 -07002530 genIPut(cUnit, mir, kWord, fieldOffset, false, false);
buzbee919eb062010-07-12 12:59:22 -07002531 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002532 case OP_IPUT_OBJECT_QUICK:
buzbeeecf8f6e2010-07-20 14:53:42 -07002533 genIPut(cUnit, mir, kWord, fieldOffset, true, false);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002534 break;
2535 case OP_IGET_WIDE_QUICK:
2536 genIGetWide(cUnit, mir, fieldOffset);
2537 break;
2538 case OP_IPUT_WIDE_QUICK:
2539 genIPutWide(cUnit, mir, fieldOffset);
2540 break;
2541 default:
2542 return true;
2543 }
2544 return false;
2545
2546}
2547
2548/* Compare agaist zero */
2549static bool handleFmt22t(CompilationUnit *cUnit, MIR *mir, BasicBlock *bb,
Bill Buzbee89efc3d2009-07-28 11:22:22 -07002550 ArmLIR *labelList)
Ben Chengba4fc8b2009-06-01 13:00:29 -07002551{
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002552 Opcode dalvikOpcode = mir->dalvikInsn.opcode;
Bill Buzbee89efc3d2009-07-28 11:22:22 -07002553 ArmConditionCode cond;
Ben Cheng7ab74e12011-02-03 14:02:06 -08002554 /* backward branch? */
2555 bool backwardBranch = (bb->taken->startOffset <= mir->offset);
2556
2557 if (backwardBranch && gDvmJit.genSuspendPoll) {
2558 genSuspendPoll(cUnit, mir);
2559 }
2560
Bill Buzbeec6f10662010-02-09 11:16:15 -08002561 RegLocation rlSrc1 = dvmCompilerGetSrc(cUnit, mir, 0);
2562 RegLocation rlSrc2 = dvmCompilerGetSrc(cUnit, mir, 1);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002563
Bill Buzbee1465db52009-09-23 17:17:35 -07002564 rlSrc1 = loadValue(cUnit, rlSrc1, kCoreReg);
2565 rlSrc2 = loadValue(cUnit, rlSrc2, kCoreReg);
Ben Cheng7ab74e12011-02-03 14:02:06 -08002566
Bill Buzbee1465db52009-09-23 17:17:35 -07002567 opRegReg(cUnit, kOpCmp, rlSrc1.lowReg, rlSrc2.lowReg);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002568
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002569 switch (dalvikOpcode) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07002570 case OP_IF_EQ:
Bill Buzbee1465db52009-09-23 17:17:35 -07002571 cond = kArmCondEq;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002572 break;
2573 case OP_IF_NE:
Bill Buzbee1465db52009-09-23 17:17:35 -07002574 cond = kArmCondNe;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002575 break;
2576 case OP_IF_LT:
Bill Buzbee1465db52009-09-23 17:17:35 -07002577 cond = kArmCondLt;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002578 break;
2579 case OP_IF_GE:
Bill Buzbee1465db52009-09-23 17:17:35 -07002580 cond = kArmCondGe;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002581 break;
2582 case OP_IF_GT:
Bill Buzbee1465db52009-09-23 17:17:35 -07002583 cond = kArmCondGt;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002584 break;
2585 case OP_IF_LE:
Bill Buzbee1465db52009-09-23 17:17:35 -07002586 cond = kArmCondLe;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002587 break;
2588 default:
2589 cond = 0;
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002590 LOGE("Unexpected opcode (%d) for Fmt22t\n", dalvikOpcode);
Bill Buzbeefc519dc2010-03-06 23:30:57 -08002591 dvmCompilerAbort(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002592 }
2593 genConditionalBranch(cUnit, cond, &labelList[bb->taken->id]);
2594 /* This mostly likely will be optimized away in a later phase */
2595 genUnconditionalBranch(cUnit, &labelList[bb->fallThrough->id]);
2596 return false;
2597}
2598
2599static bool handleFmt22x_Fmt32x(CompilationUnit *cUnit, MIR *mir)
2600{
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002601 Opcode opcode = mir->dalvikInsn.opcode;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002602
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002603 switch (opcode) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07002604 case OP_MOVE_16:
2605 case OP_MOVE_OBJECT_16:
2606 case OP_MOVE_FROM16:
Ben Chenge9695e52009-06-16 16:11:47 -07002607 case OP_MOVE_OBJECT_FROM16: {
Bill Buzbeec6f10662010-02-09 11:16:15 -08002608 storeValue(cUnit, dvmCompilerGetDest(cUnit, mir, 0),
2609 dvmCompilerGetSrc(cUnit, mir, 0));
Ben Chengba4fc8b2009-06-01 13:00:29 -07002610 break;
Ben Chenge9695e52009-06-16 16:11:47 -07002611 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07002612 case OP_MOVE_WIDE_16:
Ben Chenge9695e52009-06-16 16:11:47 -07002613 case OP_MOVE_WIDE_FROM16: {
Bill Buzbeec6f10662010-02-09 11:16:15 -08002614 storeValueWide(cUnit, dvmCompilerGetDestWide(cUnit, mir, 0, 1),
2615 dvmCompilerGetSrcWide(cUnit, mir, 0, 1));
Ben Chengba4fc8b2009-06-01 13:00:29 -07002616 break;
Ben Chenge9695e52009-06-16 16:11:47 -07002617 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07002618 default:
2619 return true;
2620 }
2621 return false;
2622}
2623
2624static bool handleFmt23x(CompilationUnit *cUnit, MIR *mir)
2625{
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002626 Opcode opcode = mir->dalvikInsn.opcode;
Bill Buzbee1465db52009-09-23 17:17:35 -07002627 RegLocation rlSrc1;
2628 RegLocation rlSrc2;
2629 RegLocation rlDest;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002630
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002631 if ( (opcode >= OP_ADD_INT) && (opcode <= OP_REM_DOUBLE)) {
Ben Cheng5d90c202009-11-22 23:31:11 -08002632 return genArithOp( cUnit, mir );
Ben Chengba4fc8b2009-06-01 13:00:29 -07002633 }
2634
Bill Buzbee1465db52009-09-23 17:17:35 -07002635 /* APUTs have 3 sources and no targets */
2636 if (mir->ssaRep->numDefs == 0) {
2637 if (mir->ssaRep->numUses == 3) {
Bill Buzbeec6f10662010-02-09 11:16:15 -08002638 rlDest = dvmCompilerGetSrc(cUnit, mir, 0);
2639 rlSrc1 = dvmCompilerGetSrc(cUnit, mir, 1);
2640 rlSrc2 = dvmCompilerGetSrc(cUnit, mir, 2);
Bill Buzbee1465db52009-09-23 17:17:35 -07002641 } else {
2642 assert(mir->ssaRep->numUses == 4);
Bill Buzbeec6f10662010-02-09 11:16:15 -08002643 rlDest = dvmCompilerGetSrcWide(cUnit, mir, 0, 1);
2644 rlSrc1 = dvmCompilerGetSrc(cUnit, mir, 2);
2645 rlSrc2 = dvmCompilerGetSrc(cUnit, mir, 3);
Bill Buzbee1465db52009-09-23 17:17:35 -07002646 }
2647 } else {
2648 /* Two sources and 1 dest. Deduce the operand sizes */
2649 if (mir->ssaRep->numUses == 4) {
Bill Buzbeec6f10662010-02-09 11:16:15 -08002650 rlSrc1 = dvmCompilerGetSrcWide(cUnit, mir, 0, 1);
2651 rlSrc2 = dvmCompilerGetSrcWide(cUnit, mir, 2, 3);
Bill Buzbee1465db52009-09-23 17:17:35 -07002652 } else {
2653 assert(mir->ssaRep->numUses == 2);
Bill Buzbeec6f10662010-02-09 11:16:15 -08002654 rlSrc1 = dvmCompilerGetSrc(cUnit, mir, 0);
2655 rlSrc2 = dvmCompilerGetSrc(cUnit, mir, 1);
Bill Buzbee1465db52009-09-23 17:17:35 -07002656 }
2657 if (mir->ssaRep->numDefs == 2) {
Bill Buzbeec6f10662010-02-09 11:16:15 -08002658 rlDest = dvmCompilerGetDestWide(cUnit, mir, 0, 1);
Bill Buzbee1465db52009-09-23 17:17:35 -07002659 } else {
2660 assert(mir->ssaRep->numDefs == 1);
Bill Buzbeec6f10662010-02-09 11:16:15 -08002661 rlDest = dvmCompilerGetDest(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07002662 }
2663 }
2664
2665
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002666 switch (opcode) {
Bill Buzbeed45ba372009-06-15 17:00:57 -07002667 case OP_CMPL_FLOAT:
2668 case OP_CMPG_FLOAT:
2669 case OP_CMPL_DOUBLE:
2670 case OP_CMPG_DOUBLE:
Ben Cheng5d90c202009-11-22 23:31:11 -08002671 return genCmpFP(cUnit, mir, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002672 case OP_CMP_LONG:
Bill Buzbee1465db52009-09-23 17:17:35 -07002673 genCmpLong(cUnit, mir, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002674 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002675 case OP_AGET_WIDE:
Bill Buzbee1465db52009-09-23 17:17:35 -07002676 genArrayGet(cUnit, mir, kLong, rlSrc1, rlSrc2, rlDest, 3);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002677 break;
2678 case OP_AGET:
2679 case OP_AGET_OBJECT:
Bill Buzbee1465db52009-09-23 17:17:35 -07002680 genArrayGet(cUnit, mir, kWord, rlSrc1, rlSrc2, rlDest, 2);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002681 break;
2682 case OP_AGET_BOOLEAN:
Bill Buzbee1465db52009-09-23 17:17:35 -07002683 genArrayGet(cUnit, mir, kUnsignedByte, rlSrc1, rlSrc2, rlDest, 0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002684 break;
2685 case OP_AGET_BYTE:
Bill Buzbee1465db52009-09-23 17:17:35 -07002686 genArrayGet(cUnit, mir, kSignedByte, rlSrc1, rlSrc2, rlDest, 0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002687 break;
2688 case OP_AGET_CHAR:
Bill Buzbee1465db52009-09-23 17:17:35 -07002689 genArrayGet(cUnit, mir, kUnsignedHalf, rlSrc1, rlSrc2, rlDest, 1);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002690 break;
2691 case OP_AGET_SHORT:
Bill Buzbee1465db52009-09-23 17:17:35 -07002692 genArrayGet(cUnit, mir, kSignedHalf, rlSrc1, rlSrc2, rlDest, 1);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002693 break;
2694 case OP_APUT_WIDE:
Bill Buzbee1465db52009-09-23 17:17:35 -07002695 genArrayPut(cUnit, mir, kLong, rlSrc1, rlSrc2, rlDest, 3);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002696 break;
2697 case OP_APUT:
Bill Buzbee1465db52009-09-23 17:17:35 -07002698 genArrayPut(cUnit, mir, kWord, rlSrc1, rlSrc2, rlDest, 2);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002699 break;
Bill Buzbeebe6534f2010-03-12 16:01:35 -08002700 case OP_APUT_OBJECT:
2701 genArrayObjectPut(cUnit, mir, rlSrc1, rlSrc2, rlDest, 2);
2702 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002703 case OP_APUT_SHORT:
2704 case OP_APUT_CHAR:
Bill Buzbee1465db52009-09-23 17:17:35 -07002705 genArrayPut(cUnit, mir, kUnsignedHalf, rlSrc1, rlSrc2, rlDest, 1);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002706 break;
2707 case OP_APUT_BYTE:
2708 case OP_APUT_BOOLEAN:
Bill Buzbee1465db52009-09-23 17:17:35 -07002709 genArrayPut(cUnit, mir, kUnsignedByte, rlSrc1, rlSrc2, rlDest, 0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002710 break;
2711 default:
2712 return true;
2713 }
2714 return false;
2715}
2716
Ben Cheng6c10a972009-10-29 14:39:18 -07002717/*
2718 * Find the matching case.
2719 *
2720 * return values:
2721 * r0 (low 32-bit): pc of the chaining cell corresponding to the resolved case,
2722 * including default which is placed at MIN(size, MAX_CHAINED_SWITCH_CASES).
2723 * r1 (high 32-bit): the branch offset of the matching case (only for indexes
2724 * above MAX_CHAINED_SWITCH_CASES).
2725 *
2726 * Instructions around the call are:
2727 *
2728 * mov r2, pc
2729 * blx &findPackedSwitchIndex
2730 * mov pc, r0
2731 * .align4
Bill Buzbeebd047242010-05-13 13:02:53 -07002732 * chaining cell for case 0 [12 bytes]
2733 * chaining cell for case 1 [12 bytes]
Ben Cheng6c10a972009-10-29 14:39:18 -07002734 * :
Bill Buzbeebd047242010-05-13 13:02:53 -07002735 * chaining cell for case MIN(size, MAX_CHAINED_SWITCH_CASES)-1 [12 bytes]
Ben Cheng6c10a972009-10-29 14:39:18 -07002736 * chaining cell for case default [8 bytes]
2737 * noChain exit
2738 */
Ben Chengbd1326d2010-04-02 15:04:53 -07002739static s8 findPackedSwitchIndex(const u2* switchData, int testVal, int pc)
Ben Cheng6c10a972009-10-29 14:39:18 -07002740{
2741 int size;
2742 int firstKey;
2743 const int *entries;
2744 int index;
2745 int jumpIndex;
2746 int caseDPCOffset = 0;
2747 /* In Thumb mode pc is 4 ahead of the "mov r2, pc" instruction */
2748 int chainingPC = (pc + 4) & ~3;
2749
2750 /*
2751 * Packed switch data format:
2752 * ushort ident = 0x0100 magic value
2753 * ushort size number of entries in the table
2754 * int first_key first (and lowest) switch case value
2755 * int targets[size] branch targets, relative to switch opcode
2756 *
2757 * Total size is (4+size*2) 16-bit code units.
2758 */
2759 size = switchData[1];
2760 assert(size > 0);
2761
2762 firstKey = switchData[2];
2763 firstKey |= switchData[3] << 16;
2764
2765
2766 /* The entries are guaranteed to be aligned on a 32-bit boundary;
2767 * we can treat them as a native int array.
2768 */
2769 entries = (const int*) &switchData[4];
2770 assert(((u4)entries & 0x3) == 0);
2771
2772 index = testVal - firstKey;
2773
2774 /* Jump to the default cell */
2775 if (index < 0 || index >= size) {
2776 jumpIndex = MIN(size, MAX_CHAINED_SWITCH_CASES);
2777 /* Jump to the non-chaining exit point */
2778 } else if (index >= MAX_CHAINED_SWITCH_CASES) {
2779 jumpIndex = MAX_CHAINED_SWITCH_CASES + 1;
2780 caseDPCOffset = entries[index];
2781 /* Jump to the inline chaining cell */
2782 } else {
2783 jumpIndex = index;
2784 }
2785
Bill Buzbeebd047242010-05-13 13:02:53 -07002786 chainingPC += jumpIndex * CHAIN_CELL_NORMAL_SIZE;
Ben Cheng6c10a972009-10-29 14:39:18 -07002787 return (((s8) caseDPCOffset) << 32) | (u8) chainingPC;
2788}
2789
2790/* See comments for findPackedSwitchIndex */
Ben Chengbd1326d2010-04-02 15:04:53 -07002791static s8 findSparseSwitchIndex(const u2* switchData, int testVal, int pc)
Ben Cheng6c10a972009-10-29 14:39:18 -07002792{
2793 int size;
2794 const int *keys;
2795 const int *entries;
2796 int chainingPC = (pc + 4) & ~3;
2797 int i;
2798
2799 /*
2800 * Sparse switch data format:
2801 * ushort ident = 0x0200 magic value
2802 * ushort size number of entries in the table; > 0
2803 * int keys[size] keys, sorted low-to-high; 32-bit aligned
2804 * int targets[size] branch targets, relative to switch opcode
2805 *
2806 * Total size is (2+size*4) 16-bit code units.
2807 */
2808
2809 size = switchData[1];
2810 assert(size > 0);
2811
2812 /* The keys are guaranteed to be aligned on a 32-bit boundary;
2813 * we can treat them as a native int array.
2814 */
2815 keys = (const int*) &switchData[2];
2816 assert(((u4)keys & 0x3) == 0);
2817
2818 /* The entries are guaranteed to be aligned on a 32-bit boundary;
2819 * we can treat them as a native int array.
2820 */
2821 entries = keys + size;
2822 assert(((u4)entries & 0x3) == 0);
2823
2824 /*
2825 * Run through the list of keys, which are guaranteed to
2826 * be sorted low-to-high.
2827 *
2828 * Most tables have 3-4 entries. Few have more than 10. A binary
2829 * search here is probably not useful.
2830 */
2831 for (i = 0; i < size; i++) {
2832 int k = keys[i];
2833 if (k == testVal) {
2834 /* MAX_CHAINED_SWITCH_CASES + 1 is the start of the overflow case */
2835 int jumpIndex = (i < MAX_CHAINED_SWITCH_CASES) ?
2836 i : MAX_CHAINED_SWITCH_CASES + 1;
Bill Buzbeebd047242010-05-13 13:02:53 -07002837 chainingPC += jumpIndex * CHAIN_CELL_NORMAL_SIZE;
Ben Cheng6c10a972009-10-29 14:39:18 -07002838 return (((s8) entries[i]) << 32) | (u8) chainingPC;
2839 } else if (k > testVal) {
2840 break;
2841 }
2842 }
Bill Buzbeebd047242010-05-13 13:02:53 -07002843 return chainingPC + MIN(size, MAX_CHAINED_SWITCH_CASES) *
2844 CHAIN_CELL_NORMAL_SIZE;
Ben Cheng6c10a972009-10-29 14:39:18 -07002845}
2846
Ben Chengba4fc8b2009-06-01 13:00:29 -07002847static bool handleFmt31t(CompilationUnit *cUnit, MIR *mir)
2848{
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002849 Opcode dalvikOpcode = mir->dalvikInsn.opcode;
2850 switch (dalvikOpcode) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07002851 case OP_FILL_ARRAY_DATA: {
Bill Buzbeec6f10662010-02-09 11:16:15 -08002852 RegLocation rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07002853 // Making a call - use explicit registers
Bill Buzbeec6f10662010-02-09 11:16:15 -08002854 dvmCompilerFlushAllRegs(cUnit); /* Everything to home location */
Bill Buzbee1465db52009-09-23 17:17:35 -07002855 genExportPC(cUnit, mir);
2856 loadValueDirectFixed(cUnit, rlSrc, r0);
Ben Chengbd1326d2010-04-02 15:04:53 -07002857 LOAD_FUNC_ADDR(cUnit, r2, (int)dvmInterpHandleFillArrayData);
Ben Cheng6c10a972009-10-29 14:39:18 -07002858 loadConstant(cUnit, r1,
2859 (int) (cUnit->method->insns + mir->offset + mir->dalvikInsn.vB));
Bill Buzbee1465db52009-09-23 17:17:35 -07002860 opReg(cUnit, kOpBlx, r2);
Elliott Hughes6a555132010-02-25 15:41:42 -08002861 dvmCompilerClobberCallRegs(cUnit);
Bill Buzbeefd023aa2009-11-02 09:23:49 -08002862 /* generate a branch over if successful */
buzbee8f8109a2010-08-31 10:16:35 -07002863 ArmLIR *branchOver = genCmpImmBranch(cUnit, kArmCondNe, r0, 0);
Bill Buzbeefd023aa2009-11-02 09:23:49 -08002864 loadConstant(cUnit, r0,
2865 (int) (cUnit->method->insns + mir->offset));
2866 genDispatchToHandler(cUnit, TEMPLATE_THROW_EXCEPTION_COMMON);
2867 ArmLIR *target = newLIR0(cUnit, kArmPseudoTargetLabel);
2868 target->defMask = ENCODE_ALL;
2869 branchOver->generic.target = (LIR *) target;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002870 break;
2871 }
2872 /*
Ben Cheng6c10a972009-10-29 14:39:18 -07002873 * Compute the goto target of up to
2874 * MIN(switchSize, MAX_CHAINED_SWITCH_CASES) + 1 chaining cells.
2875 * See the comment before findPackedSwitchIndex for the code layout.
Ben Chengba4fc8b2009-06-01 13:00:29 -07002876 */
2877 case OP_PACKED_SWITCH:
2878 case OP_SPARSE_SWITCH: {
Bill Buzbeec6f10662010-02-09 11:16:15 -08002879 RegLocation rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
2880 dvmCompilerFlushAllRegs(cUnit); /* Everything to home location */
Bill Buzbee1465db52009-09-23 17:17:35 -07002881 loadValueDirectFixed(cUnit, rlSrc, r1);
Bill Buzbeec6f10662010-02-09 11:16:15 -08002882 dvmCompilerLockAllTemps(cUnit);
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002883 if (dalvikOpcode == OP_PACKED_SWITCH) {
Ben Chengbd1326d2010-04-02 15:04:53 -07002884 LOAD_FUNC_ADDR(cUnit, r4PC, (int)findPackedSwitchIndex);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002885 } else {
Ben Chengbd1326d2010-04-02 15:04:53 -07002886 LOAD_FUNC_ADDR(cUnit, r4PC, (int)findSparseSwitchIndex);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002887 }
Ben Cheng6c10a972009-10-29 14:39:18 -07002888 /* r0 <- Addr of the switch data */
2889 loadConstant(cUnit, r0,
2890 (int) (cUnit->method->insns + mir->offset + mir->dalvikInsn.vB));
2891 /* r2 <- pc of the instruction following the blx */
2892 opRegReg(cUnit, kOpMov, r2, rpc);
Bill Buzbee1465db52009-09-23 17:17:35 -07002893 opReg(cUnit, kOpBlx, r4PC);
Elliott Hughes6a555132010-02-25 15:41:42 -08002894 dvmCompilerClobberCallRegs(cUnit);
Ben Cheng6c10a972009-10-29 14:39:18 -07002895 /* pc <- computed goto target */
2896 opRegReg(cUnit, kOpMov, rpc, r0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002897 break;
2898 }
2899 default:
2900 return true;
2901 }
2902 return false;
2903}
2904
Ben Cheng7a2697d2010-06-07 13:44:23 -07002905/*
2906 * See the example of predicted inlining listed before the
2907 * genValidationForPredictedInline function. The function here takes care the
2908 * branch over at 0x4858de78 and the misprediction target at 0x4858de7a.
2909 */
2910static void genLandingPadForMispredictedCallee(CompilationUnit *cUnit, MIR *mir,
2911 BasicBlock *bb,
2912 ArmLIR *labelList)
2913{
2914 BasicBlock *fallThrough = bb->fallThrough;
2915
2916 /* Bypass the move-result block if there is one */
2917 if (fallThrough->firstMIRInsn) {
2918 assert(fallThrough->firstMIRInsn->OptimizationFlags & MIR_INLINED_PRED);
2919 fallThrough = fallThrough->fallThrough;
2920 }
2921 /* Generate a branch over if the predicted inlining is correct */
2922 genUnconditionalBranch(cUnit, &labelList[fallThrough->id]);
2923
2924 /* Reset the register state */
2925 dvmCompilerResetRegPool(cUnit);
2926 dvmCompilerClobberAllRegs(cUnit);
2927 dvmCompilerResetNullCheck(cUnit);
2928
2929 /* Target for the slow invoke path */
2930 ArmLIR *target = newLIR0(cUnit, kArmPseudoTargetLabel);
2931 target->defMask = ENCODE_ALL;
2932 /* Hook up the target to the verification branch */
2933 mir->meta.callsiteInfo->misPredBranchOver->target = (LIR *) target;
2934}
2935
jeffhao71eee1f2011-01-04 14:18:54 -08002936static bool handleFmt35c_3rc_5rc(CompilationUnit *cUnit, MIR *mir,
2937 BasicBlock *bb, ArmLIR *labelList)
Ben Chengba4fc8b2009-06-01 13:00:29 -07002938{
Bill Buzbee9bc3df32009-07-30 10:52:29 -07002939 ArmLIR *retChainingCell = NULL;
Bill Buzbee89efc3d2009-07-28 11:22:22 -07002940 ArmLIR *pcrLabel = NULL;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002941
Ben Cheng7a2697d2010-06-07 13:44:23 -07002942 /* An invoke with the MIR_INLINED is effectively a no-op */
2943 if (mir->OptimizationFlags & MIR_INLINED)
2944 return false;
2945
Bill Buzbeef4ce16f2009-07-28 13:28:25 -07002946 if (bb->fallThrough != NULL)
2947 retChainingCell = &labelList[bb->fallThrough->id];
2948
Ben Chengba4fc8b2009-06-01 13:00:29 -07002949 DecodedInstruction *dInsn = &mir->dalvikInsn;
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002950 switch (mir->dalvikInsn.opcode) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07002951 /*
2952 * calleeMethod = this->clazz->vtable[
2953 * method->clazz->pDvmDex->pResMethods[BBBB]->methodIndex
2954 * ]
2955 */
2956 case OP_INVOKE_VIRTUAL:
jeffhao71eee1f2011-01-04 14:18:54 -08002957 case OP_INVOKE_VIRTUAL_RANGE:
2958 case OP_INVOKE_VIRTUAL_JUMBO: {
Bill Buzbee89efc3d2009-07-28 11:22:22 -07002959 ArmLIR *predChainingCell = &labelList[bb->taken->id];
Ben Chengba4fc8b2009-06-01 13:00:29 -07002960 int methodIndex =
2961 cUnit->method->clazz->pDvmDex->pResMethods[dInsn->vB]->
2962 methodIndex;
2963
Ben Cheng7a2697d2010-06-07 13:44:23 -07002964 /*
2965 * If the invoke has non-null misPredBranchOver, we need to generate
2966 * the non-inlined version of the invoke here to handle the
2967 * mispredicted case.
2968 */
2969 if (mir->meta.callsiteInfo->misPredBranchOver) {
2970 genLandingPadForMispredictedCallee(cUnit, mir, bb, labelList);
2971 }
2972
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002973 if (mir->dalvikInsn.opcode == OP_INVOKE_VIRTUAL)
Ben Chengba4fc8b2009-06-01 13:00:29 -07002974 genProcessArgsNoRange(cUnit, mir, dInsn, &pcrLabel);
2975 else
2976 genProcessArgsRange(cUnit, mir, dInsn, &pcrLabel);
2977
Ben Cheng38329f52009-07-07 14:19:20 -07002978 genInvokeVirtualCommon(cUnit, mir, methodIndex,
2979 retChainingCell,
2980 predChainingCell,
2981 pcrLabel);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002982 break;
2983 }
2984 /*
2985 * calleeMethod = method->clazz->super->vtable[method->clazz->pDvmDex
2986 * ->pResMethods[BBBB]->methodIndex]
2987 */
Ben Chengba4fc8b2009-06-01 13:00:29 -07002988 case OP_INVOKE_SUPER:
jeffhao71eee1f2011-01-04 14:18:54 -08002989 case OP_INVOKE_SUPER_RANGE:
2990 case OP_INVOKE_SUPER_JUMBO: {
Ben Cheng7a2697d2010-06-07 13:44:23 -07002991 /* Grab the method ptr directly from what the interpreter sees */
2992 const Method *calleeMethod = mir->meta.callsiteInfo->method;
2993 assert(calleeMethod == cUnit->method->clazz->super->vtable[
2994 cUnit->method->clazz->pDvmDex->
2995 pResMethods[dInsn->vB]->methodIndex]);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002996
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002997 if (mir->dalvikInsn.opcode == OP_INVOKE_SUPER)
Ben Chengba4fc8b2009-06-01 13:00:29 -07002998 genProcessArgsNoRange(cUnit, mir, dInsn, &pcrLabel);
2999 else
3000 genProcessArgsRange(cUnit, mir, dInsn, &pcrLabel);
3001
3002 /* r0 = calleeMethod */
3003 loadConstant(cUnit, r0, (int) calleeMethod);
3004
Ben Cheng38329f52009-07-07 14:19:20 -07003005 genInvokeSingletonCommon(cUnit, mir, bb, labelList, pcrLabel,
3006 calleeMethod);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003007 break;
3008 }
3009 /* calleeMethod = method->clazz->pDvmDex->pResMethods[BBBB] */
3010 case OP_INVOKE_DIRECT:
jeffhao71eee1f2011-01-04 14:18:54 -08003011 case OP_INVOKE_DIRECT_RANGE:
3012 case OP_INVOKE_DIRECT_JUMBO: {
Ben Cheng7a2697d2010-06-07 13:44:23 -07003013 /* Grab the method ptr directly from what the interpreter sees */
3014 const Method *calleeMethod = mir->meta.callsiteInfo->method;
3015 assert(calleeMethod ==
3016 cUnit->method->clazz->pDvmDex->pResMethods[dInsn->vB]);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003017
Dan Bornstein9a1f8162010-12-01 17:02:26 -08003018 if (mir->dalvikInsn.opcode == OP_INVOKE_DIRECT)
Ben Chengba4fc8b2009-06-01 13:00:29 -07003019 genProcessArgsNoRange(cUnit, mir, dInsn, &pcrLabel);
3020 else
3021 genProcessArgsRange(cUnit, mir, dInsn, &pcrLabel);
3022
3023 /* r0 = calleeMethod */
3024 loadConstant(cUnit, r0, (int) calleeMethod);
3025
Ben Cheng38329f52009-07-07 14:19:20 -07003026 genInvokeSingletonCommon(cUnit, mir, bb, labelList, pcrLabel,
3027 calleeMethod);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003028 break;
3029 }
3030 /* calleeMethod = method->clazz->pDvmDex->pResMethods[BBBB] */
3031 case OP_INVOKE_STATIC:
jeffhao71eee1f2011-01-04 14:18:54 -08003032 case OP_INVOKE_STATIC_RANGE:
3033 case OP_INVOKE_STATIC_JUMBO: {
Ben Cheng7a2697d2010-06-07 13:44:23 -07003034 /* Grab the method ptr directly from what the interpreter sees */
3035 const Method *calleeMethod = mir->meta.callsiteInfo->method;
3036 assert(calleeMethod ==
3037 cUnit->method->clazz->pDvmDex->pResMethods[dInsn->vB]);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003038
Dan Bornstein9a1f8162010-12-01 17:02:26 -08003039 if (mir->dalvikInsn.opcode == OP_INVOKE_STATIC)
Ben Chengba4fc8b2009-06-01 13:00:29 -07003040 genProcessArgsNoRange(cUnit, mir, dInsn,
3041 NULL /* no null check */);
3042 else
3043 genProcessArgsRange(cUnit, mir, dInsn,
3044 NULL /* no null check */);
3045
3046 /* r0 = calleeMethod */
3047 loadConstant(cUnit, r0, (int) calleeMethod);
3048
Ben Cheng38329f52009-07-07 14:19:20 -07003049 genInvokeSingletonCommon(cUnit, mir, bb, labelList, pcrLabel,
3050 calleeMethod);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003051 break;
3052 }
Ben Cheng09e50c92010-05-02 10:45:32 -07003053 /*
Ben Chengba4fc8b2009-06-01 13:00:29 -07003054 * calleeMethod = dvmFindInterfaceMethodInCache(this->clazz,
3055 * BBBB, method, method->clazz->pDvmDex)
Ben Cheng38329f52009-07-07 14:19:20 -07003056 *
Ben Cheng09e50c92010-05-02 10:45:32 -07003057 * The following is an example of generated code for
3058 * "invoke-interface v0"
Ben Cheng38329f52009-07-07 14:19:20 -07003059 *
Ben Cheng09e50c92010-05-02 10:45:32 -07003060 * -------- dalvik offset: 0x0008 @ invoke-interface v0
3061 * 0x47357e36 : ldr r0, [r5, #0] --+
3062 * 0x47357e38 : sub r7,r5,#24 |
3063 * 0x47357e3c : cmp r0, #0 | genProcessArgsNoRange
3064 * 0x47357e3e : beq 0x47357e82 |
3065 * 0x47357e40 : stmia r7, <r0> --+
3066 * 0x47357e42 : ldr r4, [pc, #120] --> r4 <- dalvikPC of this invoke
3067 * 0x47357e44 : add r1, pc, #64 --> r1 <- &retChainingCell
3068 * 0x47357e46 : add r2, pc, #72 --> r2 <- &predictedChainingCell
3069 * 0x47357e48 : blx_1 0x47348190 --+ TEMPLATE_INVOKE_METHOD_
3070 * 0x47357e4a : blx_2 see above --+ PREDICTED_CHAIN
3071 * 0x47357e4c : b 0x47357e90 --> off to the predicted chain
3072 * 0x47357e4e : b 0x47357e82 --> punt to the interpreter
3073 * 0x47357e50 : mov r8, r1 --+
3074 * 0x47357e52 : mov r9, r2 |
3075 * 0x47357e54 : ldr r2, [pc, #96] |
3076 * 0x47357e56 : mov r10, r3 |
3077 * 0x47357e58 : movs r0, r3 | dvmFindInterfaceMethodInCache
3078 * 0x47357e5a : ldr r3, [pc, #88] |
3079 * 0x47357e5c : ldr r7, [pc, #80] |
3080 * 0x47357e5e : mov r1, #1452 |
3081 * 0x47357e62 : blx r7 --+
3082 * 0x47357e64 : cmp r0, #0 --> calleeMethod == NULL?
3083 * 0x47357e66 : bne 0x47357e6e --> branch over the throw if !r0
3084 * 0x47357e68 : ldr r0, [pc, #80] --> load Dalvik PC of the invoke
3085 * 0x47357e6a : blx_1 0x47348494 --+ TEMPLATE_THROW_EXCEPTION_
3086 * 0x47357e6c : blx_2 see above --+ COMMON
3087 * 0x47357e6e : mov r1, r8 --> r1 <- &retChainingCell
3088 * 0x47357e70 : cmp r1, #0 --> compare against 0
3089 * 0x47357e72 : bgt 0x47357e7c --> >=0? don't rechain
Ben Chengaf5aa1f2011-01-04 15:37:04 -08003090 * 0x47357e74 : ldr r7, [pc, #off] --+
Ben Cheng09e50c92010-05-02 10:45:32 -07003091 * 0x47357e76 : mov r2, r9 | dvmJitToPatchPredictedChain
3092 * 0x47357e78 : mov r3, r10 |
3093 * 0x47357e7a : blx r7 --+
3094 * 0x47357e7c : add r1, pc, #8 --> r1 <- &retChainingCell
3095 * 0x47357e7e : blx_1 0x4734809c --+ TEMPLATE_INVOKE_METHOD_NO_OPT
3096 * 0x47357e80 : blx_2 see above --+
3097 * -------- reconstruct dalvik PC : 0x425719dc @ +0x0008
3098 * 0x47357e82 : ldr r0, [pc, #56]
Ben Cheng38329f52009-07-07 14:19:20 -07003099 * Exception_Handling:
Ben Cheng09e50c92010-05-02 10:45:32 -07003100 * 0x47357e84 : ldr r1, [r6, #92]
3101 * 0x47357e86 : blx r1
3102 * 0x47357e88 : .align4
3103 * -------- chaining cell (hot): 0x000b
3104 * 0x47357e88 : ldr r0, [r6, #104]
3105 * 0x47357e8a : blx r0
3106 * 0x47357e8c : data 0x19e2(6626)
3107 * 0x47357e8e : data 0x4257(16983)
3108 * 0x47357e90 : .align4
Ben Cheng38329f52009-07-07 14:19:20 -07003109 * -------- chaining cell (predicted)
Ben Cheng09e50c92010-05-02 10:45:32 -07003110 * 0x47357e90 : data 0xe7fe(59390) --> will be patched into bx
3111 * 0x47357e92 : data 0x0000(0)
3112 * 0x47357e94 : data 0x0000(0) --> class
3113 * 0x47357e96 : data 0x0000(0)
3114 * 0x47357e98 : data 0x0000(0) --> method
3115 * 0x47357e9a : data 0x0000(0)
3116 * 0x47357e9c : data 0x0000(0) --> rechain count
3117 * 0x47357e9e : data 0x0000(0)
3118 * -------- end of chaining cells (0x006c)
3119 * 0x47357eb0 : .word (0xad03e369)
3120 * 0x47357eb4 : .word (0x28a90)
3121 * 0x47357eb8 : .word (0x41a63394)
3122 * 0x47357ebc : .word (0x425719dc)
Ben Chengba4fc8b2009-06-01 13:00:29 -07003123 */
3124 case OP_INVOKE_INTERFACE:
jeffhao71eee1f2011-01-04 14:18:54 -08003125 case OP_INVOKE_INTERFACE_RANGE:
3126 case OP_INVOKE_INTERFACE_JUMBO: {
Bill Buzbee89efc3d2009-07-28 11:22:22 -07003127 ArmLIR *predChainingCell = &labelList[bb->taken->id];
Ben Chengba4fc8b2009-06-01 13:00:29 -07003128
Ben Cheng7a2697d2010-06-07 13:44:23 -07003129 /*
3130 * If the invoke has non-null misPredBranchOver, we need to generate
3131 * the non-inlined version of the invoke here to handle the
3132 * mispredicted case.
3133 */
3134 if (mir->meta.callsiteInfo->misPredBranchOver) {
3135 genLandingPadForMispredictedCallee(cUnit, mir, bb, labelList);
3136 }
Bill Buzbee1465db52009-09-23 17:17:35 -07003137
Dan Bornstein9a1f8162010-12-01 17:02:26 -08003138 if (mir->dalvikInsn.opcode == OP_INVOKE_INTERFACE)
Ben Chengba4fc8b2009-06-01 13:00:29 -07003139 genProcessArgsNoRange(cUnit, mir, dInsn, &pcrLabel);
3140 else
3141 genProcessArgsRange(cUnit, mir, dInsn, &pcrLabel);
3142
Ben Cheng38329f52009-07-07 14:19:20 -07003143 /* "this" is already left in r0 by genProcessArgs* */
3144
3145 /* r4PC = dalvikCallsite */
3146 loadConstant(cUnit, r4PC,
3147 (int) (cUnit->method->insns + mir->offset));
3148
3149 /* r1 = &retChainingCell */
Bill Buzbee270c1d62009-08-13 16:58:07 -07003150 ArmLIR *addrRetChain =
Bill Buzbee1465db52009-09-23 17:17:35 -07003151 opRegRegImm(cUnit, kOpAdd, r1, rpc, 0);
Ben Cheng38329f52009-07-07 14:19:20 -07003152 addrRetChain->generic.target = (LIR *) retChainingCell;
3153
3154 /* r2 = &predictedChainingCell */
Bill Buzbee89efc3d2009-07-28 11:22:22 -07003155 ArmLIR *predictedChainingCell =
Bill Buzbee1465db52009-09-23 17:17:35 -07003156 opRegRegImm(cUnit, kOpAdd, r2, rpc, 0);
Ben Cheng38329f52009-07-07 14:19:20 -07003157 predictedChainingCell->generic.target = (LIR *) predChainingCell;
3158
buzbee18fba342011-01-19 15:31:15 -08003159 genDispatchToHandler(cUnit, gDvmJit.methodTraceSupport ?
3160 TEMPLATE_INVOKE_METHOD_PREDICTED_CHAIN_PROF :
3161 TEMPLATE_INVOKE_METHOD_PREDICTED_CHAIN);
Ben Cheng38329f52009-07-07 14:19:20 -07003162
3163 /* return through lr - jump to the chaining cell */
3164 genUnconditionalBranch(cUnit, predChainingCell);
3165
3166 /*
3167 * null-check on "this" may have been eliminated, but we still need
3168 * a PC-reconstruction label for stack overflow bailout.
3169 */
3170 if (pcrLabel == NULL) {
3171 int dPC = (int) (cUnit->method->insns + mir->offset);
Carl Shapirofc75f3e2010-12-07 11:43:38 -08003172 pcrLabel = (ArmLIR *) dvmCompilerNew(sizeof(ArmLIR), true);
Dan Bornstein9a1f8162010-12-01 17:02:26 -08003173 pcrLabel->opcode = kArmPseudoPCReconstructionCell;
Ben Cheng38329f52009-07-07 14:19:20 -07003174 pcrLabel->operands[0] = dPC;
3175 pcrLabel->operands[1] = mir->offset;
3176 /* Insert the place holder to the growable list */
Ben Cheng00603072010-10-28 11:13:58 -07003177 dvmInsertGrowableList(&cUnit->pcReconstructionList,
3178 (intptr_t) pcrLabel);
Ben Cheng38329f52009-07-07 14:19:20 -07003179 }
3180
3181 /* return through lr+2 - punt to the interpreter */
3182 genUnconditionalBranch(cUnit, pcrLabel);
3183
3184 /*
3185 * return through lr+4 - fully resolve the callee method.
3186 * r1 <- count
3187 * r2 <- &predictedChainCell
3188 * r3 <- this->class
3189 * r4 <- dPC
3190 * r7 <- this->class->vtable
3191 */
3192
3193 /* Save count, &predictedChainCell, and class to high regs first */
Bill Buzbee1465db52009-09-23 17:17:35 -07003194 genRegCopy(cUnit, r8, r1);
3195 genRegCopy(cUnit, r9, r2);
3196 genRegCopy(cUnit, r10, r3);
Ben Cheng38329f52009-07-07 14:19:20 -07003197
Ben Chengba4fc8b2009-06-01 13:00:29 -07003198 /* r0 now contains this->clazz */
Bill Buzbee1465db52009-09-23 17:17:35 -07003199 genRegCopy(cUnit, r0, r3);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003200
3201 /* r1 = BBBB */
3202 loadConstant(cUnit, r1, dInsn->vB);
3203
3204 /* r2 = method (caller) */
3205 loadConstant(cUnit, r2, (int) cUnit->method);
3206
3207 /* r3 = pDvmDex */
3208 loadConstant(cUnit, r3, (int) cUnit->method->clazz->pDvmDex);
3209
Ben Chengbd1326d2010-04-02 15:04:53 -07003210 LOAD_FUNC_ADDR(cUnit, r7,
3211 (intptr_t) dvmFindInterfaceMethodInCache);
Bill Buzbee1465db52009-09-23 17:17:35 -07003212 opReg(cUnit, kOpBlx, r7);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003213 /* r0 = calleeMethod (returned from dvmFindInterfaceMethodInCache */
3214
Ben Cheng09e50c92010-05-02 10:45:32 -07003215 dvmCompilerClobberCallRegs(cUnit);
3216 /* generate a branch over if the interface method is resolved */
buzbee8f8109a2010-08-31 10:16:35 -07003217 ArmLIR *branchOver = genCmpImmBranch(cUnit, kArmCondNe, r0, 0);
Ben Cheng09e50c92010-05-02 10:45:32 -07003218 /*
3219 * calleeMethod == NULL -> throw
3220 */
3221 loadConstant(cUnit, r0,
3222 (int) (cUnit->method->insns + mir->offset));
3223 genDispatchToHandler(cUnit, TEMPLATE_THROW_EXCEPTION_COMMON);
3224 /* noreturn */
3225
3226 ArmLIR *target = newLIR0(cUnit, kArmPseudoTargetLabel);
3227 target->defMask = ENCODE_ALL;
3228 branchOver->generic.target = (LIR *) target;
3229
Bill Buzbee1465db52009-09-23 17:17:35 -07003230 genRegCopy(cUnit, r1, r8);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003231
Ben Cheng38329f52009-07-07 14:19:20 -07003232 /* Check if rechain limit is reached */
buzbee8f8109a2010-08-31 10:16:35 -07003233 ArmLIR *bypassRechaining = genCmpImmBranch(cUnit, kArmCondGt,
3234 r1, 0);
Ben Cheng38329f52009-07-07 14:19:20 -07003235
Ben Chengaf5aa1f2011-01-04 15:37:04 -08003236 LOAD_FUNC_ADDR(cUnit, r7, (int) dvmJitToPatchPredictedChain);
Ben Cheng38329f52009-07-07 14:19:20 -07003237
Ben Chengb88ec3c2010-05-17 12:50:33 -07003238 genRegCopy(cUnit, r1, rGLUE);
Bill Buzbee1465db52009-09-23 17:17:35 -07003239 genRegCopy(cUnit, r2, r9);
3240 genRegCopy(cUnit, r3, r10);
Ben Cheng38329f52009-07-07 14:19:20 -07003241
3242 /*
3243 * r0 = calleeMethod
3244 * r2 = &predictedChainingCell
3245 * r3 = class
3246 *
3247 * &returnChainingCell has been loaded into r1 but is not needed
3248 * when patching the chaining cell and will be clobbered upon
3249 * returning so it will be reconstructed again.
3250 */
Bill Buzbee1465db52009-09-23 17:17:35 -07003251 opReg(cUnit, kOpBlx, r7);
Ben Cheng38329f52009-07-07 14:19:20 -07003252
3253 /* r1 = &retChainingCell */
Bill Buzbee1465db52009-09-23 17:17:35 -07003254 addrRetChain = opRegRegImm(cUnit, kOpAdd, r1, rpc, 0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003255 addrRetChain->generic.target = (LIR *) retChainingCell;
Ben Cheng38329f52009-07-07 14:19:20 -07003256
3257 bypassRechaining->generic.target = (LIR *) addrRetChain;
3258
Ben Chengba4fc8b2009-06-01 13:00:29 -07003259 /*
3260 * r0 = this, r1 = calleeMethod,
3261 * r1 = &ChainingCell,
3262 * r4PC = callsiteDPC,
3263 */
buzbee18fba342011-01-19 15:31:15 -08003264 genDispatchToHandler(cUnit, gDvmJit.methodTraceSupport ?
3265 TEMPLATE_INVOKE_METHOD_NO_OPT_PROF :
3266 TEMPLATE_INVOKE_METHOD_NO_OPT);
Ben Cheng978738d2010-05-13 13:45:57 -07003267#if defined(WITH_JIT_TUNING)
Ben Cheng86717f72010-03-05 15:27:21 -08003268 gDvmJit.invokePolymorphic++;
Ben Chengba4fc8b2009-06-01 13:00:29 -07003269#endif
3270 /* Handle exceptions using the interpreter */
3271 genTrap(cUnit, mir->offset, pcrLabel);
3272 break;
3273 }
3274 /* NOP */
Andy McFadden750d1102011-02-11 15:26:10 -08003275 case OP_INVOKE_OBJECT_INIT: {
buzbee18fba342011-01-19 15:31:15 -08003276 if (gDvmJit.methodTraceSupport)
3277 genInterpSingleStep(cUnit, mir);
3278 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07003279 }
3280 case OP_FILLED_NEW_ARRAY:
jeffhao71eee1f2011-01-04 14:18:54 -08003281 case OP_FILLED_NEW_ARRAY_RANGE:
3282 case OP_FILLED_NEW_ARRAY_JUMBO: {
Ben Chengba4fc8b2009-06-01 13:00:29 -07003283 /* Just let the interpreter deal with these */
3284 genInterpSingleStep(cUnit, mir);
3285 break;
3286 }
3287 default:
3288 return true;
3289 }
3290 return false;
3291}
3292
Ben Chengcfdeca32011-01-14 11:36:46 -08003293/* "this" pointer is already in r0 */
3294static void genValidationForMethodCallee(CompilationUnit *cUnit, MIR *mir,
3295 ArmLIR **classCheck)
3296{
3297 CallsiteInfo *callsiteInfo = mir->meta.callsiteInfo;
3298 dvmCompilerLockAllTemps(cUnit);
3299
3300 loadConstant(cUnit, r1, (int) callsiteInfo->clazz);
3301
3302 loadWordDisp(cUnit, r0, offsetof(Object, clazz), r2);
3303 /* Branch to the slow path if classes are not equal */
3304 opRegReg(cUnit, kOpCmp, r1, r2);
3305 /*
3306 * Set the misPredBranchOver target so that it will be generated when the
3307 * code for the non-optimized invoke is generated.
3308 */
3309 *classCheck = opCondBranch(cUnit, kArmCondNe);
3310}
3311
Ben Chengba4fc8b2009-06-01 13:00:29 -07003312static bool handleFmt35ms_3rms(CompilationUnit *cUnit, MIR *mir,
Bill Buzbee89efc3d2009-07-28 11:22:22 -07003313 BasicBlock *bb, ArmLIR *labelList)
Ben Chengba4fc8b2009-06-01 13:00:29 -07003314{
Bill Buzbee89efc3d2009-07-28 11:22:22 -07003315 ArmLIR *pcrLabel = NULL;
Ben Chengba4fc8b2009-06-01 13:00:29 -07003316
Ben Cheng7a2697d2010-06-07 13:44:23 -07003317 /* An invoke with the MIR_INLINED is effectively a no-op */
3318 if (mir->OptimizationFlags & MIR_INLINED)
3319 return false;
3320
Ben Chengba4fc8b2009-06-01 13:00:29 -07003321 DecodedInstruction *dInsn = &mir->dalvikInsn;
Dan Bornstein9a1f8162010-12-01 17:02:26 -08003322 switch (mir->dalvikInsn.opcode) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07003323 /* calleeMethod = this->clazz->vtable[BBBB] */
3324 case OP_INVOKE_VIRTUAL_QUICK_RANGE:
3325 case OP_INVOKE_VIRTUAL_QUICK: {
3326 int methodIndex = dInsn->vB;
Bill Buzbeea8589332010-12-27 09:31:21 -08003327 ArmLIR *retChainingCell = &labelList[bb->fallThrough->id];
3328 ArmLIR *predChainingCell = &labelList[bb->taken->id];
Ben Cheng7a2697d2010-06-07 13:44:23 -07003329
3330 /*
3331 * If the invoke has non-null misPredBranchOver, we need to generate
3332 * the non-inlined version of the invoke here to handle the
3333 * mispredicted case.
3334 */
3335 if (mir->meta.callsiteInfo->misPredBranchOver) {
3336 genLandingPadForMispredictedCallee(cUnit, mir, bb, labelList);
3337 }
3338
Dan Bornstein9a1f8162010-12-01 17:02:26 -08003339 if (mir->dalvikInsn.opcode == OP_INVOKE_VIRTUAL_QUICK)
Ben Chengba4fc8b2009-06-01 13:00:29 -07003340 genProcessArgsNoRange(cUnit, mir, dInsn, &pcrLabel);
3341 else
3342 genProcessArgsRange(cUnit, mir, dInsn, &pcrLabel);
3343
Ben Chengcfdeca32011-01-14 11:36:46 -08003344
3345 if (mir->OptimizationFlags & MIR_INVOKE_METHOD_JIT) {
3346 const Method *calleeMethod = mir->meta.callsiteInfo->method;
3347 void *calleeAddr = dvmJitGetMethodAddr(calleeMethod->insns);
3348 if (calleeAddr) {
3349 ArmLIR *classCheck;
3350 cUnit->printMe = true;
3351 genValidationForMethodCallee(cUnit, mir, &classCheck);
3352 newLIR2(cUnit, kThumbBl1, (int) calleeAddr,
3353 (int) calleeAddr);
3354 newLIR2(cUnit, kThumbBl2, (int) calleeAddr,
3355 (int) calleeAddr);
3356 genUnconditionalBranch(cUnit, retChainingCell);
3357
3358 /* Target of slow path */
3359 ArmLIR *slowPathLabel = newLIR0(cUnit,
3360 kArmPseudoTargetLabel);
3361
3362 slowPathLabel->defMask = ENCODE_ALL;
3363 classCheck->generic.target = (LIR *) slowPathLabel;
3364 }
3365 }
3366
Ben Cheng38329f52009-07-07 14:19:20 -07003367 genInvokeVirtualCommon(cUnit, mir, methodIndex,
3368 retChainingCell,
3369 predChainingCell,
3370 pcrLabel);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003371 break;
3372 }
3373 /* calleeMethod = method->clazz->super->vtable[BBBB] */
3374 case OP_INVOKE_SUPER_QUICK:
3375 case OP_INVOKE_SUPER_QUICK_RANGE: {
Ben Cheng7a2697d2010-06-07 13:44:23 -07003376 /* Grab the method ptr directly from what the interpreter sees */
3377 const Method *calleeMethod = mir->meta.callsiteInfo->method;
3378 assert(calleeMethod ==
3379 cUnit->method->clazz->super->vtable[dInsn->vB]);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003380
Dan Bornstein9a1f8162010-12-01 17:02:26 -08003381 if (mir->dalvikInsn.opcode == OP_INVOKE_SUPER_QUICK)
Ben Chengba4fc8b2009-06-01 13:00:29 -07003382 genProcessArgsNoRange(cUnit, mir, dInsn, &pcrLabel);
3383 else
3384 genProcessArgsRange(cUnit, mir, dInsn, &pcrLabel);
3385
3386 /* r0 = calleeMethod */
3387 loadConstant(cUnit, r0, (int) calleeMethod);
3388
Ben Cheng38329f52009-07-07 14:19:20 -07003389 genInvokeSingletonCommon(cUnit, mir, bb, labelList, pcrLabel,
3390 calleeMethod);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003391 break;
3392 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07003393 default:
3394 return true;
3395 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07003396 return false;
3397}
3398
3399/*
Bill Buzbeefd023aa2009-11-02 09:23:49 -08003400 * This operation is complex enough that we'll do it partly inline
3401 * and partly with a handler. NOTE: the handler uses hardcoded
3402 * values for string object offsets and must be revisitied if the
3403 * layout changes.
3404 */
3405static bool genInlinedCompareTo(CompilationUnit *cUnit, MIR *mir)
3406{
3407#if defined(USE_GLOBAL_STRING_DEFS)
Elliott Hughes7e914f12011-01-19 18:18:42 -08003408 return handleExecuteInlineC(cUnit, mir);
Bill Buzbeefd023aa2009-11-02 09:23:49 -08003409#else
3410 ArmLIR *rollback;
Bill Buzbeec6f10662010-02-09 11:16:15 -08003411 RegLocation rlThis = dvmCompilerGetSrc(cUnit, mir, 0);
3412 RegLocation rlComp = dvmCompilerGetSrc(cUnit, mir, 1);
Bill Buzbeefd023aa2009-11-02 09:23:49 -08003413
3414 loadValueDirectFixed(cUnit, rlThis, r0);
3415 loadValueDirectFixed(cUnit, rlComp, r1);
3416 /* Test objects for NULL */
3417 rollback = genNullCheck(cUnit, rlThis.sRegLow, r0, mir->offset, NULL);
3418 genNullCheck(cUnit, rlComp.sRegLow, r1, mir->offset, rollback);
3419 /*
3420 * TUNING: we could check for object pointer equality before invoking
3421 * handler. Unclear whether the gain would be worth the added code size
3422 * expansion.
3423 */
3424 genDispatchToHandler(cUnit, TEMPLATE_STRING_COMPARETO);
Bill Buzbeec6f10662010-02-09 11:16:15 -08003425 storeValue(cUnit, inlinedTarget(cUnit, mir, false),
3426 dvmCompilerGetReturn(cUnit));
Elliott Hughes7e914f12011-01-19 18:18:42 -08003427 return false;
Bill Buzbeefd023aa2009-11-02 09:23:49 -08003428#endif
3429}
3430
Elliott Hughes2bdbcb62010-04-12 14:29:37 -07003431static bool genInlinedFastIndexOf(CompilationUnit *cUnit, MIR *mir)
Bill Buzbeefd023aa2009-11-02 09:23:49 -08003432{
3433#if defined(USE_GLOBAL_STRING_DEFS)
Elliott Hughes7e914f12011-01-19 18:18:42 -08003434 return handleExecuteInlineC(cUnit, mir);
Bill Buzbeefd023aa2009-11-02 09:23:49 -08003435#else
Bill Buzbeec6f10662010-02-09 11:16:15 -08003436 RegLocation rlThis = dvmCompilerGetSrc(cUnit, mir, 0);
3437 RegLocation rlChar = dvmCompilerGetSrc(cUnit, mir, 1);
Bill Buzbeefd023aa2009-11-02 09:23:49 -08003438
3439 loadValueDirectFixed(cUnit, rlThis, r0);
3440 loadValueDirectFixed(cUnit, rlChar, r1);
Elliott Hughes2bdbcb62010-04-12 14:29:37 -07003441 RegLocation rlStart = dvmCompilerGetSrc(cUnit, mir, 2);
3442 loadValueDirectFixed(cUnit, rlStart, r2);
Bill Buzbeefd023aa2009-11-02 09:23:49 -08003443 /* Test objects for NULL */
3444 genNullCheck(cUnit, rlThis.sRegLow, r0, mir->offset, NULL);
3445 genDispatchToHandler(cUnit, TEMPLATE_STRING_INDEXOF);
Bill Buzbeec6f10662010-02-09 11:16:15 -08003446 storeValue(cUnit, inlinedTarget(cUnit, mir, false),
3447 dvmCompilerGetReturn(cUnit));
Elliott Hughes7e914f12011-01-19 18:18:42 -08003448 return false;
Bill Buzbeefd023aa2009-11-02 09:23:49 -08003449#endif
3450}
3451
Elliott Hughesee34f592010-04-05 18:13:52 -07003452// Generates an inlined String.isEmpty or String.length.
3453static bool genInlinedStringIsEmptyOrLength(CompilationUnit *cUnit, MIR *mir,
3454 bool isEmpty)
Bill Buzbee1f748632010-03-02 16:14:41 -08003455{
Elliott Hughesee34f592010-04-05 18:13:52 -07003456 // dst = src.length();
Bill Buzbee1f748632010-03-02 16:14:41 -08003457 RegLocation rlObj = dvmCompilerGetSrc(cUnit, mir, 0);
3458 RegLocation rlDest = inlinedTarget(cUnit, mir, false);
3459 rlObj = loadValue(cUnit, rlObj, kCoreReg);
3460 RegLocation rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
3461 genNullCheck(cUnit, rlObj.sRegLow, rlObj.lowReg, mir->offset, NULL);
3462 loadWordDisp(cUnit, rlObj.lowReg, gDvm.offJavaLangString_count,
3463 rlResult.lowReg);
Elliott Hughesee34f592010-04-05 18:13:52 -07003464 if (isEmpty) {
3465 // dst = (dst == 0);
3466 int tReg = dvmCompilerAllocTemp(cUnit);
3467 opRegReg(cUnit, kOpNeg, tReg, rlResult.lowReg);
3468 opRegRegReg(cUnit, kOpAdc, rlResult.lowReg, rlResult.lowReg, tReg);
3469 }
Bill Buzbee1f748632010-03-02 16:14:41 -08003470 storeValue(cUnit, rlDest, rlResult);
3471 return false;
3472}
3473
Elliott Hughesee34f592010-04-05 18:13:52 -07003474static bool genInlinedStringLength(CompilationUnit *cUnit, MIR *mir)
3475{
3476 return genInlinedStringIsEmptyOrLength(cUnit, mir, false);
3477}
3478
3479static bool genInlinedStringIsEmpty(CompilationUnit *cUnit, MIR *mir)
3480{
3481 return genInlinedStringIsEmptyOrLength(cUnit, mir, true);
3482}
3483
Bill Buzbee1f748632010-03-02 16:14:41 -08003484static bool genInlinedStringCharAt(CompilationUnit *cUnit, MIR *mir)
3485{
3486 int contents = offsetof(ArrayObject, contents);
3487 RegLocation rlObj = dvmCompilerGetSrc(cUnit, mir, 0);
3488 RegLocation rlIdx = dvmCompilerGetSrc(cUnit, mir, 1);
3489 RegLocation rlDest = inlinedTarget(cUnit, mir, false);
3490 RegLocation rlResult;
3491 rlObj = loadValue(cUnit, rlObj, kCoreReg);
3492 rlIdx = loadValue(cUnit, rlIdx, kCoreReg);
3493 int regMax = dvmCompilerAllocTemp(cUnit);
3494 int regOff = dvmCompilerAllocTemp(cUnit);
3495 int regPtr = dvmCompilerAllocTemp(cUnit);
3496 ArmLIR *pcrLabel = genNullCheck(cUnit, rlObj.sRegLow, rlObj.lowReg,
3497 mir->offset, NULL);
3498 loadWordDisp(cUnit, rlObj.lowReg, gDvm.offJavaLangString_count, regMax);
3499 loadWordDisp(cUnit, rlObj.lowReg, gDvm.offJavaLangString_offset, regOff);
3500 loadWordDisp(cUnit, rlObj.lowReg, gDvm.offJavaLangString_value, regPtr);
3501 genBoundsCheck(cUnit, rlIdx.lowReg, regMax, mir->offset, pcrLabel);
3502 dvmCompilerFreeTemp(cUnit, regMax);
3503 opRegImm(cUnit, kOpAdd, regPtr, contents);
3504 opRegReg(cUnit, kOpAdd, regOff, rlIdx.lowReg);
3505 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
3506 loadBaseIndexed(cUnit, regPtr, regOff, rlResult.lowReg, 1, kUnsignedHalf);
3507 storeValue(cUnit, rlDest, rlResult);
3508 return false;
3509}
3510
3511static bool genInlinedAbsInt(CompilationUnit *cUnit, MIR *mir)
3512{
3513 RegLocation rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
3514 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
Elliott Hughese22bd842010-08-20 18:47:36 -07003515 RegLocation rlDest = inlinedTarget(cUnit, mir, false);
Bill Buzbee1f748632010-03-02 16:14:41 -08003516 RegLocation rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
3517 int signReg = dvmCompilerAllocTemp(cUnit);
3518 /*
3519 * abs(x) = y<=x>>31, (x+y)^y.
3520 * Thumb2's IT block also yields 3 instructions, but imposes
3521 * scheduling constraints.
3522 */
3523 opRegRegImm(cUnit, kOpAsr, signReg, rlSrc.lowReg, 31);
3524 opRegRegReg(cUnit, kOpAdd, rlResult.lowReg, rlSrc.lowReg, signReg);
3525 opRegReg(cUnit, kOpXor, rlResult.lowReg, signReg);
3526 storeValue(cUnit, rlDest, rlResult);
3527 return false;
3528}
3529
3530static bool genInlinedAbsLong(CompilationUnit *cUnit, MIR *mir)
3531{
3532 RegLocation rlSrc = dvmCompilerGetSrcWide(cUnit, mir, 0, 1);
3533 RegLocation rlDest = inlinedTargetWide(cUnit, mir, false);
3534 rlSrc = loadValueWide(cUnit, rlSrc, kCoreReg);
3535 RegLocation rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
3536 int signReg = dvmCompilerAllocTemp(cUnit);
3537 /*
3538 * abs(x) = y<=x>>31, (x+y)^y.
3539 * Thumb2 IT block allows slightly shorter sequence,
3540 * but introduces a scheduling barrier. Stick with this
3541 * mechanism for now.
3542 */
3543 opRegRegImm(cUnit, kOpAsr, signReg, rlSrc.highReg, 31);
3544 opRegRegReg(cUnit, kOpAdd, rlResult.lowReg, rlSrc.lowReg, signReg);
3545 opRegRegReg(cUnit, kOpAdc, rlResult.highReg, rlSrc.highReg, signReg);
3546 opRegReg(cUnit, kOpXor, rlResult.lowReg, signReg);
3547 opRegReg(cUnit, kOpXor, rlResult.highReg, signReg);
3548 storeValueWide(cUnit, rlDest, rlResult);
3549 return false;
3550}
Bill Buzbeefd023aa2009-11-02 09:23:49 -08003551
Elliott Hughese22bd842010-08-20 18:47:36 -07003552static bool genInlinedIntFloatConversion(CompilationUnit *cUnit, MIR *mir)
3553{
3554 // Just move from source to destination...
3555 RegLocation rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
3556 RegLocation rlDest = inlinedTarget(cUnit, mir, false);
3557 storeValue(cUnit, rlDest, rlSrc);
3558 return false;
3559}
3560
3561static bool genInlinedLongDoubleConversion(CompilationUnit *cUnit, MIR *mir)
3562{
3563 // Just move from source to destination...
3564 RegLocation rlSrc = dvmCompilerGetSrcWide(cUnit, mir, 0, 1);
3565 RegLocation rlDest = inlinedTargetWide(cUnit, mir, false);
3566 storeValueWide(cUnit, rlDest, rlSrc);
3567 return false;
3568}
3569
Bill Buzbeefd023aa2009-11-02 09:23:49 -08003570/*
Elliott Hughes7e914f12011-01-19 18:18:42 -08003571 * JITs a call to a C function.
3572 * TODO: use this for faster native method invocation for simple native
3573 * methods (http://b/3069458).
3574 */
3575static bool handleExecuteInlineC(CompilationUnit *cUnit, MIR *mir)
3576{
3577 DecodedInstruction *dInsn = &mir->dalvikInsn;
3578 int operation = dInsn->vB;
3579 unsigned int i;
3580 const InlineOperation* inLineTable = dvmGetInlineOpsTable();
3581 uintptr_t fn = (int) inLineTable[operation].func;
3582 if (fn == 0) {
3583 dvmCompilerAbort(cUnit);
3584 }
3585 dvmCompilerFlushAllRegs(cUnit); /* Everything to home location */
3586 dvmCompilerClobberCallRegs(cUnit);
3587 dvmCompilerClobber(cUnit, r4PC);
3588 dvmCompilerClobber(cUnit, r7);
3589 int offset = offsetof(InterpState, retval);
3590 opRegRegImm(cUnit, kOpAdd, r4PC, rGLUE, offset);
3591 opImm(cUnit, kOpPush, (1<<r4PC) | (1<<r7));
3592 LOAD_FUNC_ADDR(cUnit, r4PC, fn);
3593 genExportPC(cUnit, mir);
3594 for (i=0; i < dInsn->vA; i++) {
3595 loadValueDirect(cUnit, dvmCompilerGetSrc(cUnit, mir, i), i);
3596 }
3597 opReg(cUnit, kOpBlx, r4PC);
3598 opRegImm(cUnit, kOpAdd, r13, 8);
3599 /* NULL? */
3600 ArmLIR *branchOver = genCmpImmBranch(cUnit, kArmCondNe, r0, 0);
3601 loadConstant(cUnit, r0, (int) (cUnit->method->insns + mir->offset));
3602 genDispatchToHandler(cUnit, TEMPLATE_THROW_EXCEPTION_COMMON);
3603 ArmLIR *target = newLIR0(cUnit, kArmPseudoTargetLabel);
3604 target->defMask = ENCODE_ALL;
3605 branchOver->generic.target = (LIR *) target;
3606 return false;
3607}
3608
3609/*
Bill Buzbeece46c942009-11-20 15:41:34 -08003610 * NOTE: Handles both range and non-range versions (arguments
3611 * have already been normalized by this point).
Ben Chengba4fc8b2009-06-01 13:00:29 -07003612 */
Bill Buzbeece46c942009-11-20 15:41:34 -08003613static bool handleExecuteInline(CompilationUnit *cUnit, MIR *mir)
Ben Chengba4fc8b2009-06-01 13:00:29 -07003614{
3615 DecodedInstruction *dInsn = &mir->dalvikInsn;
Elliott Hughes7e914f12011-01-19 18:18:42 -08003616 assert(dInsn->opcode == OP_EXECUTE_INLINE_RANGE ||
3617 dInsn->opcode == OP_EXECUTE_INLINE);
3618 switch (dInsn->vB) {
3619 case INLINE_EMPTYINLINEMETHOD:
3620 return false; /* Nop */
3621
3622 /* These ones we potentially JIT inline. */
3623 case INLINE_STRING_LENGTH:
3624 return genInlinedStringLength(cUnit, mir);
3625 case INLINE_STRING_IS_EMPTY:
3626 return genInlinedStringIsEmpty(cUnit, mir);
3627 case INLINE_MATH_ABS_INT:
3628 return genInlinedAbsInt(cUnit, mir);
3629 case INLINE_MATH_ABS_LONG:
3630 return genInlinedAbsLong(cUnit, mir);
3631 case INLINE_MATH_MIN_INT:
3632 return genInlinedMinMaxInt(cUnit, mir, true);
3633 case INLINE_MATH_MAX_INT:
3634 return genInlinedMinMaxInt(cUnit, mir, false);
3635 case INLINE_STRING_CHARAT:
3636 return genInlinedStringCharAt(cUnit, mir);
3637 case INLINE_MATH_SQRT:
3638 return genInlineSqrt(cUnit, mir);
3639 case INLINE_MATH_ABS_FLOAT:
3640 return genInlinedAbsFloat(cUnit, mir);
3641 case INLINE_MATH_ABS_DOUBLE:
3642 return genInlinedAbsDouble(cUnit, mir);
3643 case INLINE_STRING_COMPARETO:
3644 return genInlinedCompareTo(cUnit, mir);
3645 case INLINE_STRING_FASTINDEXOF_II:
3646 return genInlinedFastIndexOf(cUnit, mir);
3647 case INLINE_FLOAT_TO_RAW_INT_BITS:
3648 case INLINE_INT_BITS_TO_FLOAT:
3649 return genInlinedIntFloatConversion(cUnit, mir);
3650 case INLINE_DOUBLE_TO_RAW_LONG_BITS:
3651 case INLINE_LONG_BITS_TO_DOUBLE:
3652 return genInlinedLongDoubleConversion(cUnit, mir);
3653
3654 /*
3655 * These ones we just JIT a call to a C function for.
3656 * TODO: special-case these in the other "invoke" call paths.
3657 */
3658 case INLINE_STRING_EQUALS:
3659 case INLINE_MATH_COS:
3660 case INLINE_MATH_SIN:
3661 case INLINE_FLOAT_TO_INT_BITS:
3662 case INLINE_DOUBLE_TO_LONG_BITS:
3663 return handleExecuteInlineC(cUnit, mir);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003664 }
Elliott Hughes7e914f12011-01-19 18:18:42 -08003665 dvmCompilerAbort(cUnit);
3666 return false; // Not reachable; keeps compiler happy.
Ben Chengba4fc8b2009-06-01 13:00:29 -07003667}
3668
3669static bool handleFmt51l(CompilationUnit *cUnit, MIR *mir)
3670{
Bill Buzbee1465db52009-09-23 17:17:35 -07003671 //TUNING: We're using core regs here - not optimal when target is a double
Bill Buzbeec6f10662010-02-09 11:16:15 -08003672 RegLocation rlDest = dvmCompilerGetDestWide(cUnit, mir, 0, 1);
3673 RegLocation rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Ben Chengbd1326d2010-04-02 15:04:53 -07003674 loadConstantNoClobber(cUnit, rlResult.lowReg,
3675 mir->dalvikInsn.vB_wide & 0xFFFFFFFFUL);
3676 loadConstantNoClobber(cUnit, rlResult.highReg,
3677 (mir->dalvikInsn.vB_wide>>32) & 0xFFFFFFFFUL);
Bill Buzbee1465db52009-09-23 17:17:35 -07003678 storeValueWide(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003679 return false;
3680}
3681
Ben Chengba4fc8b2009-06-01 13:00:29 -07003682/*
3683 * The following are special processing routines that handle transfer of
3684 * controls between compiled code and the interpreter. Certain VM states like
3685 * Dalvik PC and special-purpose registers are reconstructed here.
3686 */
3687
Bill Buzbeebd047242010-05-13 13:02:53 -07003688/*
3689 * Insert a
3690 * b .+4
3691 * nop
3692 * pair at the beginning of a chaining cell. This serves as the
3693 * switch branch that selects between reverting to the interpreter or
3694 * not. Once the cell is chained to a translation, the cell will
3695 * contain a 32-bit branch. Subsequent chain/unchain operations will
3696 * then only alter that first 16-bits - the "b .+4" for unchaining,
3697 * and the restoration of the first half of the 32-bit branch for
3698 * rechaining.
3699 */
3700static void insertChainingSwitch(CompilationUnit *cUnit)
3701{
3702 ArmLIR *branch = newLIR0(cUnit, kThumbBUncond);
3703 newLIR2(cUnit, kThumbOrr, r0, r0);
3704 ArmLIR *target = newLIR0(cUnit, kArmPseudoTargetLabel);
3705 target->defMask = ENCODE_ALL;
3706 branch->generic.target = (LIR *) target;
3707}
3708
Ben Cheng1efc9c52009-06-08 18:25:27 -07003709/* Chaining cell for code that may need warmup. */
3710static void handleNormalChainingCell(CompilationUnit *cUnit,
3711 unsigned int offset)
Ben Chengba4fc8b2009-06-01 13:00:29 -07003712{
Ben Cheng11d8f142010-03-24 15:24:19 -07003713 /*
3714 * Use raw instruction constructors to guarantee that the generated
3715 * instructions fit the predefined cell size.
3716 */
Bill Buzbeebd047242010-05-13 13:02:53 -07003717 insertChainingSwitch(cUnit);
Ben Cheng11d8f142010-03-24 15:24:19 -07003718 newLIR3(cUnit, kThumbLdrRRI5, r0, rGLUE,
3719 offsetof(InterpState,
3720 jitToInterpEntries.dvmJitToInterpNormal) >> 2);
3721 newLIR1(cUnit, kThumbBlxR, r0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003722 addWordData(cUnit, (int) (cUnit->method->insns + offset), true);
3723}
3724
3725/*
Ben Cheng1efc9c52009-06-08 18:25:27 -07003726 * Chaining cell for instructions that immediately following already translated
3727 * code.
Ben Chengba4fc8b2009-06-01 13:00:29 -07003728 */
Ben Cheng1efc9c52009-06-08 18:25:27 -07003729static void handleHotChainingCell(CompilationUnit *cUnit,
3730 unsigned int offset)
Ben Chengba4fc8b2009-06-01 13:00:29 -07003731{
Ben Cheng11d8f142010-03-24 15:24:19 -07003732 /*
3733 * Use raw instruction constructors to guarantee that the generated
3734 * instructions fit the predefined cell size.
3735 */
Bill Buzbeebd047242010-05-13 13:02:53 -07003736 insertChainingSwitch(cUnit);
Ben Cheng11d8f142010-03-24 15:24:19 -07003737 newLIR3(cUnit, kThumbLdrRRI5, r0, rGLUE,
3738 offsetof(InterpState,
3739 jitToInterpEntries.dvmJitToInterpTraceSelect) >> 2);
3740 newLIR1(cUnit, kThumbBlxR, r0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003741 addWordData(cUnit, (int) (cUnit->method->insns + offset), true);
3742}
3743
Jeff Hao97319a82009-08-12 16:57:15 -07003744/* Chaining cell for branches that branch back into the same basic block */
3745static void handleBackwardBranchChainingCell(CompilationUnit *cUnit,
3746 unsigned int offset)
3747{
Ben Cheng11d8f142010-03-24 15:24:19 -07003748 /*
3749 * Use raw instruction constructors to guarantee that the generated
3750 * instructions fit the predefined cell size.
3751 */
Bill Buzbeebd047242010-05-13 13:02:53 -07003752 insertChainingSwitch(cUnit);
Bill Buzbee9c4b7c82009-09-10 10:10:38 -07003753#if defined(WITH_SELF_VERIFICATION)
Bill Buzbee1465db52009-09-23 17:17:35 -07003754 newLIR3(cUnit, kThumbLdrRRI5, r0, rGLUE,
Ben Cheng40094c12010-02-24 20:58:44 -08003755 offsetof(InterpState,
3756 jitToInterpEntries.dvmJitToInterpBackwardBranch) >> 2);
Bill Buzbee9c4b7c82009-09-10 10:10:38 -07003757#else
Bill Buzbee1465db52009-09-23 17:17:35 -07003758 newLIR3(cUnit, kThumbLdrRRI5, r0, rGLUE,
Bill Buzbee9c4b7c82009-09-10 10:10:38 -07003759 offsetof(InterpState, jitToInterpEntries.dvmJitToInterpNormal) >> 2);
3760#endif
Bill Buzbee1465db52009-09-23 17:17:35 -07003761 newLIR1(cUnit, kThumbBlxR, r0);
Jeff Hao97319a82009-08-12 16:57:15 -07003762 addWordData(cUnit, (int) (cUnit->method->insns + offset), true);
3763}
3764
Ben Chengba4fc8b2009-06-01 13:00:29 -07003765/* Chaining cell for monomorphic method invocations. */
Ben Cheng38329f52009-07-07 14:19:20 -07003766static void handleInvokeSingletonChainingCell(CompilationUnit *cUnit,
3767 const Method *callee)
Ben Chengba4fc8b2009-06-01 13:00:29 -07003768{
Ben Cheng11d8f142010-03-24 15:24:19 -07003769 /*
3770 * Use raw instruction constructors to guarantee that the generated
3771 * instructions fit the predefined cell size.
3772 */
Bill Buzbeebd047242010-05-13 13:02:53 -07003773 insertChainingSwitch(cUnit);
Ben Cheng11d8f142010-03-24 15:24:19 -07003774 newLIR3(cUnit, kThumbLdrRRI5, r0, rGLUE,
3775 offsetof(InterpState,
3776 jitToInterpEntries.dvmJitToInterpTraceSelect) >> 2);
3777 newLIR1(cUnit, kThumbBlxR, r0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003778 addWordData(cUnit, (int) (callee->insns), true);
3779}
3780
Ben Cheng38329f52009-07-07 14:19:20 -07003781/* Chaining cell for monomorphic method invocations. */
3782static void handleInvokePredictedChainingCell(CompilationUnit *cUnit)
3783{
3784
3785 /* Should not be executed in the initial state */
3786 addWordData(cUnit, PREDICTED_CHAIN_BX_PAIR_INIT, true);
3787 /* To be filled: class */
3788 addWordData(cUnit, PREDICTED_CHAIN_CLAZZ_INIT, true);
3789 /* To be filled: method */
3790 addWordData(cUnit, PREDICTED_CHAIN_METHOD_INIT, true);
3791 /*
3792 * Rechain count. The initial value of 0 here will trigger chaining upon
3793 * the first invocation of this callsite.
3794 */
3795 addWordData(cUnit, PREDICTED_CHAIN_COUNTER_INIT, true);
3796}
3797
Ben Chengba4fc8b2009-06-01 13:00:29 -07003798/* Load the Dalvik PC into r0 and jump to the specified target */
3799static void handlePCReconstruction(CompilationUnit *cUnit,
Bill Buzbee89efc3d2009-07-28 11:22:22 -07003800 ArmLIR *targetLabel)
Ben Chengba4fc8b2009-06-01 13:00:29 -07003801{
Bill Buzbee89efc3d2009-07-28 11:22:22 -07003802 ArmLIR **pcrLabel =
3803 (ArmLIR **) cUnit->pcReconstructionList.elemList;
Ben Chengba4fc8b2009-06-01 13:00:29 -07003804 int numElems = cUnit->pcReconstructionList.numUsed;
3805 int i;
3806 for (i = 0; i < numElems; i++) {
3807 dvmCompilerAppendLIR(cUnit, (LIR *) pcrLabel[i]);
3808 /* r0 = dalvik PC */
3809 loadConstant(cUnit, r0, pcrLabel[i]->operands[0]);
3810 genUnconditionalBranch(cUnit, targetLabel);
3811 }
3812}
3813
Bill Buzbee1465db52009-09-23 17:17:35 -07003814static char *extendedMIROpNames[kMirOpLast - kMirOpFirst] = {
3815 "kMirOpPhi",
3816 "kMirOpNullNRangeUpCheck",
3817 "kMirOpNullNRangeDownCheck",
3818 "kMirOpLowerBound",
3819 "kMirOpPunt",
Ben Cheng7a2697d2010-06-07 13:44:23 -07003820 "kMirOpCheckInlinePrediction",
Ben Cheng4238ec22009-08-24 16:32:22 -07003821};
3822
3823/*
3824 * vA = arrayReg;
3825 * vB = idxReg;
3826 * vC = endConditionReg;
3827 * arg[0] = maxC
3828 * arg[1] = minC
3829 * arg[2] = loopBranchConditionCode
3830 */
3831static void genHoistedChecksForCountUpLoop(CompilationUnit *cUnit, MIR *mir)
3832{
Bill Buzbee1465db52009-09-23 17:17:35 -07003833 /*
3834 * NOTE: these synthesized blocks don't have ssa names assigned
3835 * for Dalvik registers. However, because they dominate the following
3836 * blocks we can simply use the Dalvik name w/ subscript 0 as the
3837 * ssa name.
3838 */
Ben Cheng4238ec22009-08-24 16:32:22 -07003839 DecodedInstruction *dInsn = &mir->dalvikInsn;
3840 const int lenOffset = offsetof(ArrayObject, length);
Ben Cheng4238ec22009-08-24 16:32:22 -07003841 const int maxC = dInsn->arg[0];
Bill Buzbee1465db52009-09-23 17:17:35 -07003842 int regLength;
3843 RegLocation rlArray = cUnit->regLocation[mir->dalvikInsn.vA];
3844 RegLocation rlIdxEnd = cUnit->regLocation[mir->dalvikInsn.vC];
Ben Cheng4238ec22009-08-24 16:32:22 -07003845
3846 /* regArray <- arrayRef */
Bill Buzbee1465db52009-09-23 17:17:35 -07003847 rlArray = loadValue(cUnit, rlArray, kCoreReg);
3848 rlIdxEnd = loadValue(cUnit, rlIdxEnd, kCoreReg);
3849 genRegImmCheck(cUnit, kArmCondEq, rlArray.lowReg, 0, 0,
Ben Cheng4238ec22009-08-24 16:32:22 -07003850 (ArmLIR *) cUnit->loopAnalysis->branchToPCR);
3851
3852 /* regLength <- len(arrayRef) */
Bill Buzbeec6f10662010-02-09 11:16:15 -08003853 regLength = dvmCompilerAllocTemp(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07003854 loadWordDisp(cUnit, rlArray.lowReg, lenOffset, regLength);
Ben Cheng4238ec22009-08-24 16:32:22 -07003855
3856 int delta = maxC;
3857 /*
3858 * If the loop end condition is ">=" instead of ">", then the largest value
3859 * of the index is "endCondition - 1".
3860 */
3861 if (dInsn->arg[2] == OP_IF_GE) {
3862 delta--;
3863 }
3864
3865 if (delta) {
Bill Buzbeec6f10662010-02-09 11:16:15 -08003866 int tReg = dvmCompilerAllocTemp(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07003867 opRegRegImm(cUnit, kOpAdd, tReg, rlIdxEnd.lowReg, delta);
3868 rlIdxEnd.lowReg = tReg;
Bill Buzbeec6f10662010-02-09 11:16:15 -08003869 dvmCompilerFreeTemp(cUnit, tReg);
Ben Cheng4238ec22009-08-24 16:32:22 -07003870 }
3871 /* Punt if "regIdxEnd < len(Array)" is false */
Bill Buzbee1465db52009-09-23 17:17:35 -07003872 genRegRegCheck(cUnit, kArmCondGe, rlIdxEnd.lowReg, regLength, 0,
Ben Cheng0fd31e42009-09-03 14:40:16 -07003873 (ArmLIR *) cUnit->loopAnalysis->branchToPCR);
Ben Cheng4238ec22009-08-24 16:32:22 -07003874}
3875
3876/*
3877 * vA = arrayReg;
3878 * vB = idxReg;
3879 * vC = endConditionReg;
3880 * arg[0] = maxC
3881 * arg[1] = minC
3882 * arg[2] = loopBranchConditionCode
3883 */
3884static void genHoistedChecksForCountDownLoop(CompilationUnit *cUnit, MIR *mir)
3885{
3886 DecodedInstruction *dInsn = &mir->dalvikInsn;
3887 const int lenOffset = offsetof(ArrayObject, length);
Bill Buzbeec6f10662010-02-09 11:16:15 -08003888 const int regLength = dvmCompilerAllocTemp(cUnit);
Ben Cheng4238ec22009-08-24 16:32:22 -07003889 const int maxC = dInsn->arg[0];
Bill Buzbee1465db52009-09-23 17:17:35 -07003890 RegLocation rlArray = cUnit->regLocation[mir->dalvikInsn.vA];
3891 RegLocation rlIdxInit = cUnit->regLocation[mir->dalvikInsn.vB];
Ben Cheng4238ec22009-08-24 16:32:22 -07003892
3893 /* regArray <- arrayRef */
Bill Buzbee1465db52009-09-23 17:17:35 -07003894 rlArray = loadValue(cUnit, rlArray, kCoreReg);
3895 rlIdxInit = loadValue(cUnit, rlIdxInit, kCoreReg);
3896 genRegImmCheck(cUnit, kArmCondEq, rlArray.lowReg, 0, 0,
Ben Cheng4238ec22009-08-24 16:32:22 -07003897 (ArmLIR *) cUnit->loopAnalysis->branchToPCR);
3898
3899 /* regLength <- len(arrayRef) */
Bill Buzbee1465db52009-09-23 17:17:35 -07003900 loadWordDisp(cUnit, rlArray.lowReg, lenOffset, regLength);
Ben Cheng4238ec22009-08-24 16:32:22 -07003901
3902 if (maxC) {
Bill Buzbeec6f10662010-02-09 11:16:15 -08003903 int tReg = dvmCompilerAllocTemp(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07003904 opRegRegImm(cUnit, kOpAdd, tReg, rlIdxInit.lowReg, maxC);
3905 rlIdxInit.lowReg = tReg;
Bill Buzbeec6f10662010-02-09 11:16:15 -08003906 dvmCompilerFreeTemp(cUnit, tReg);
Ben Cheng4238ec22009-08-24 16:32:22 -07003907 }
3908
3909 /* Punt if "regIdxInit < len(Array)" is false */
Bill Buzbee1465db52009-09-23 17:17:35 -07003910 genRegRegCheck(cUnit, kArmCondGe, rlIdxInit.lowReg, regLength, 0,
Ben Cheng0fd31e42009-09-03 14:40:16 -07003911 (ArmLIR *) cUnit->loopAnalysis->branchToPCR);
Ben Cheng4238ec22009-08-24 16:32:22 -07003912}
3913
3914/*
3915 * vA = idxReg;
3916 * vB = minC;
3917 */
3918static void genHoistedLowerBoundCheck(CompilationUnit *cUnit, MIR *mir)
3919{
3920 DecodedInstruction *dInsn = &mir->dalvikInsn;
Ben Cheng4238ec22009-08-24 16:32:22 -07003921 const int minC = dInsn->vB;
Bill Buzbee1465db52009-09-23 17:17:35 -07003922 RegLocation rlIdx = cUnit->regLocation[mir->dalvikInsn.vA];
Ben Cheng4238ec22009-08-24 16:32:22 -07003923
3924 /* regIdx <- initial index value */
Bill Buzbee1465db52009-09-23 17:17:35 -07003925 rlIdx = loadValue(cUnit, rlIdx, kCoreReg);
Ben Cheng4238ec22009-08-24 16:32:22 -07003926
3927 /* Punt if "regIdxInit + minC >= 0" is false */
Bill Buzbee1465db52009-09-23 17:17:35 -07003928 genRegImmCheck(cUnit, kArmCondLt, rlIdx.lowReg, -minC, 0,
Ben Cheng4238ec22009-08-24 16:32:22 -07003929 (ArmLIR *) cUnit->loopAnalysis->branchToPCR);
3930}
3931
Ben Cheng7a2697d2010-06-07 13:44:23 -07003932/*
3933 * vC = this
3934 *
3935 * A predicted inlining target looks like the following, where instructions
3936 * between 0x4858de66 and 0x4858de72 are checking if the predicted class
3937 * matches "this", and the verificaion code is generated by this routine.
3938 *
3939 * (C) means the instruction is inlined from the callee, and (PI) means the
3940 * instruction is the predicted inlined invoke, whose corresponding
3941 * instructions are still generated to handle the mispredicted case.
3942 *
3943 * D/dalvikvm( 86): -------- kMirOpCheckInlinePrediction
3944 * D/dalvikvm( 86): 0x4858de66 (0002): ldr r0, [r5, #68]
3945 * D/dalvikvm( 86): 0x4858de68 (0004): ldr r1, [pc, #140]
3946 * D/dalvikvm( 86): 0x4858de6a (0006): cmp r0, #0
3947 * D/dalvikvm( 86): 0x4858de6c (0008): beq 0x4858deb2
3948 * D/dalvikvm( 86): 0x4858de6e (000a): ldr r2, [r0, #0]
3949 * D/dalvikvm( 86): 0x4858de70 (000c): cmp r1, r2
3950 * D/dalvikvm( 86): 0x4858de72 (000e): bne 0x4858de7a
3951 * D/dalvikvm( 86): -------- dalvik offset: 0x004c @ +iget-object-quick (C)
3952 * v4, v17, (#8)
3953 * D/dalvikvm( 86): 0x4858de74 (0010): ldr r3, [r0, #8]
3954 * D/dalvikvm( 86): 0x4858de76 (0012): str r3, [r5, #16]
3955 * D/dalvikvm( 86): -------- dalvik offset: 0x004c @
3956 * +invoke-virtual-quick/range (PI) v17..v17
3957 * D/dalvikvm( 86): 0x4858de78 (0014): b 0x4858debc
3958 * D/dalvikvm( 86): 0x4858de7a (0016): add r4,r5,#68
3959 * D/dalvikvm( 86): -------- BARRIER
3960 * D/dalvikvm( 86): 0x4858de7e (001a): ldmia r4, <r0>
3961 * D/dalvikvm( 86): -------- BARRIER
3962 * D/dalvikvm( 86): 0x4858de80 (001c): sub r7,r5,#24
3963 * D/dalvikvm( 86): 0x4858de84 (0020): cmp r0, #0
3964 * D/dalvikvm( 86): 0x4858de86 (0022): beq 0x4858deb6
3965 * D/dalvikvm( 86): -------- BARRIER
3966 * D/dalvikvm( 86): 0x4858de88 (0024): stmia r7, <r0>
3967 * D/dalvikvm( 86): -------- BARRIER
3968 * D/dalvikvm( 86): 0x4858de8a (0026): ldr r4, [pc, #104]
3969 * D/dalvikvm( 86): 0x4858de8c (0028): add r1, pc, #28
3970 * D/dalvikvm( 86): 0x4858de8e (002a): add r2, pc, #56
3971 * D/dalvikvm( 86): 0x4858de90 (002c): blx_1 0x48589198
3972 * D/dalvikvm( 86): 0x4858de92 (002e): blx_2 see above
3973 * D/dalvikvm( 86): 0x4858de94 (0030): b 0x4858dec8
3974 * D/dalvikvm( 86): 0x4858de96 (0032): b 0x4858deb6
3975 * D/dalvikvm( 86): 0x4858de98 (0034): ldr r0, [r7, #72]
3976 * D/dalvikvm( 86): 0x4858de9a (0036): cmp r1, #0
3977 * D/dalvikvm( 86): 0x4858de9c (0038): bgt 0x4858dea4
3978 * D/dalvikvm( 86): 0x4858de9e (003a): ldr r7, [r6, #116]
3979 * D/dalvikvm( 86): 0x4858dea0 (003c): movs r1, r6
3980 * D/dalvikvm( 86): 0x4858dea2 (003e): blx r7
3981 * D/dalvikvm( 86): 0x4858dea4 (0040): add r1, pc, #4
3982 * D/dalvikvm( 86): 0x4858dea6 (0042): blx_1 0x485890a0
3983 * D/dalvikvm( 86): 0x4858dea8 (0044): blx_2 see above
3984 * D/dalvikvm( 86): 0x4858deaa (0046): b 0x4858deb6
3985 * D/dalvikvm( 86): 0x4858deac (0048): .align4
3986 * D/dalvikvm( 86): L0x004f:
3987 * D/dalvikvm( 86): -------- dalvik offset: 0x004f @ move-result-object (PI)
3988 * v4, (#0), (#0)
3989 * D/dalvikvm( 86): 0x4858deac (0048): ldr r4, [r6, #8]
3990 * D/dalvikvm( 86): 0x4858deae (004a): str r4, [r5, #16]
3991 * D/dalvikvm( 86): 0x4858deb0 (004c): b 0x4858debc
3992 * D/dalvikvm( 86): -------- reconstruct dalvik PC : 0x42beefcc @ +0x004c
3993 * D/dalvikvm( 86): 0x4858deb2 (004e): ldr r0, [pc, #64]
3994 * D/dalvikvm( 86): 0x4858deb4 (0050): b 0x4858deb8
3995 * D/dalvikvm( 86): -------- reconstruct dalvik PC : 0x42beefcc @ +0x004c
3996 * D/dalvikvm( 86): 0x4858deb6 (0052): ldr r0, [pc, #60]
3997 * D/dalvikvm( 86): Exception_Handling:
3998 * D/dalvikvm( 86): 0x4858deb8 (0054): ldr r1, [r6, #100]
3999 * D/dalvikvm( 86): 0x4858deba (0056): blx r1
4000 * D/dalvikvm( 86): 0x4858debc (0058): .align4
4001 * D/dalvikvm( 86): -------- chaining cell (hot): 0x0050
4002 * D/dalvikvm( 86): 0x4858debc (0058): b 0x4858dec0
4003 * D/dalvikvm( 86): 0x4858debe (005a): orrs r0, r0
4004 * D/dalvikvm( 86): 0x4858dec0 (005c): ldr r0, [r6, #112]
4005 * D/dalvikvm( 86): 0x4858dec2 (005e): blx r0
4006 * D/dalvikvm( 86): 0x4858dec4 (0060): data 0xefd4(61396)
4007 * D/dalvikvm( 86): 0x4858dec6 (0062): data 0x42be(17086)
4008 * D/dalvikvm( 86): 0x4858dec8 (0064): .align4
4009 * D/dalvikvm( 86): -------- chaining cell (predicted)
4010 * D/dalvikvm( 86): 0x4858dec8 (0064): data 0xe7fe(59390)
4011 * D/dalvikvm( 86): 0x4858deca (0066): data 0x0000(0)
4012 * D/dalvikvm( 86): 0x4858decc (0068): data 0x0000(0)
4013 * D/dalvikvm( 86): 0x4858dece (006a): data 0x0000(0)
4014 * :
4015 */
4016static void genValidationForPredictedInline(CompilationUnit *cUnit, MIR *mir)
4017{
4018 CallsiteInfo *callsiteInfo = mir->meta.callsiteInfo;
4019 RegLocation rlThis = cUnit->regLocation[mir->dalvikInsn.vC];
4020
4021 rlThis = loadValue(cUnit, rlThis, kCoreReg);
4022 int regPredictedClass = dvmCompilerAllocTemp(cUnit);
4023 loadConstant(cUnit, regPredictedClass, (int) callsiteInfo->clazz);
4024 genNullCheck(cUnit, rlThis.sRegLow, rlThis.lowReg, mir->offset,
4025 NULL);/* null object? */
4026 int regActualClass = dvmCompilerAllocTemp(cUnit);
4027 loadWordDisp(cUnit, rlThis.lowReg, offsetof(Object, clazz), regActualClass);
4028 opRegReg(cUnit, kOpCmp, regPredictedClass, regActualClass);
4029 /*
4030 * Set the misPredBranchOver target so that it will be generated when the
4031 * code for the non-optimized invoke is generated.
4032 */
4033 callsiteInfo->misPredBranchOver = (LIR *) opCondBranch(cUnit, kArmCondNe);
4034}
4035
Ben Cheng4238ec22009-08-24 16:32:22 -07004036/* Extended MIR instructions like PHI */
4037static void handleExtendedMIR(CompilationUnit *cUnit, MIR *mir)
4038{
Dan Bornstein9a1f8162010-12-01 17:02:26 -08004039 int opOffset = mir->dalvikInsn.opcode - kMirOpFirst;
Carl Shapirofc75f3e2010-12-07 11:43:38 -08004040 char *msg = (char *)dvmCompilerNew(strlen(extendedMIROpNames[opOffset]) + 1,
4041 false);
Ben Cheng4238ec22009-08-24 16:32:22 -07004042 strcpy(msg, extendedMIROpNames[opOffset]);
Bill Buzbee1465db52009-09-23 17:17:35 -07004043 newLIR1(cUnit, kArmPseudoExtended, (int) msg);
Ben Cheng4238ec22009-08-24 16:32:22 -07004044
Dan Bornstein9a1f8162010-12-01 17:02:26 -08004045 switch (mir->dalvikInsn.opcode) {
Bill Buzbee1465db52009-09-23 17:17:35 -07004046 case kMirOpPhi: {
Ben Cheng4238ec22009-08-24 16:32:22 -07004047 char *ssaString = dvmCompilerGetSSAString(cUnit, mir->ssaRep);
Bill Buzbee1465db52009-09-23 17:17:35 -07004048 newLIR1(cUnit, kArmPseudoSSARep, (int) ssaString);
Ben Cheng4238ec22009-08-24 16:32:22 -07004049 break;
4050 }
Bill Buzbee1465db52009-09-23 17:17:35 -07004051 case kMirOpNullNRangeUpCheck: {
Ben Cheng4238ec22009-08-24 16:32:22 -07004052 genHoistedChecksForCountUpLoop(cUnit, mir);
4053 break;
4054 }
Bill Buzbee1465db52009-09-23 17:17:35 -07004055 case kMirOpNullNRangeDownCheck: {
Ben Cheng4238ec22009-08-24 16:32:22 -07004056 genHoistedChecksForCountDownLoop(cUnit, mir);
4057 break;
4058 }
Bill Buzbee1465db52009-09-23 17:17:35 -07004059 case kMirOpLowerBound: {
Ben Cheng4238ec22009-08-24 16:32:22 -07004060 genHoistedLowerBoundCheck(cUnit, mir);
4061 break;
4062 }
Bill Buzbee1465db52009-09-23 17:17:35 -07004063 case kMirOpPunt: {
Ben Cheng4238ec22009-08-24 16:32:22 -07004064 genUnconditionalBranch(cUnit,
4065 (ArmLIR *) cUnit->loopAnalysis->branchToPCR);
4066 break;
4067 }
Ben Cheng7a2697d2010-06-07 13:44:23 -07004068 case kMirOpCheckInlinePrediction: {
4069 genValidationForPredictedInline(cUnit, mir);
4070 break;
4071 }
Ben Cheng4238ec22009-08-24 16:32:22 -07004072 default:
4073 break;
4074 }
4075}
4076
4077/*
4078 * Create a PC-reconstruction cell for the starting offset of this trace.
4079 * Since the PCR cell is placed near the end of the compiled code which is
4080 * usually out of range for a conditional branch, we put two branches (one
4081 * branch over to the loop body and one layover branch to the actual PCR) at the
4082 * end of the entry block.
4083 */
4084static void setupLoopEntryBlock(CompilationUnit *cUnit, BasicBlock *entry,
4085 ArmLIR *bodyLabel)
4086{
4087 /* Set up the place holder to reconstruct this Dalvik PC */
Carl Shapirofc75f3e2010-12-07 11:43:38 -08004088 ArmLIR *pcrLabel = (ArmLIR *) dvmCompilerNew(sizeof(ArmLIR), true);
Dan Bornstein9a1f8162010-12-01 17:02:26 -08004089 pcrLabel->opcode = kArmPseudoPCReconstructionCell;
Ben Cheng4238ec22009-08-24 16:32:22 -07004090 pcrLabel->operands[0] =
4091 (int) (cUnit->method->insns + entry->startOffset);
4092 pcrLabel->operands[1] = entry->startOffset;
4093 /* Insert the place holder to the growable list */
Ben Cheng00603072010-10-28 11:13:58 -07004094 dvmInsertGrowableList(&cUnit->pcReconstructionList, (intptr_t) pcrLabel);
Ben Cheng4238ec22009-08-24 16:32:22 -07004095
4096 /*
4097 * Next, create two branches - one branch over to the loop body and the
4098 * other branch to the PCR cell to punt.
4099 */
Carl Shapirofc75f3e2010-12-07 11:43:38 -08004100 ArmLIR *branchToBody = (ArmLIR *) dvmCompilerNew(sizeof(ArmLIR), true);
Dan Bornstein9a1f8162010-12-01 17:02:26 -08004101 branchToBody->opcode = kThumbBUncond;
Ben Cheng4238ec22009-08-24 16:32:22 -07004102 branchToBody->generic.target = (LIR *) bodyLabel;
Ben Chengdcf3e5d2009-09-11 13:42:05 -07004103 setupResourceMasks(branchToBody);
Ben Cheng4238ec22009-08-24 16:32:22 -07004104 cUnit->loopAnalysis->branchToBody = (LIR *) branchToBody;
4105
Carl Shapirofc75f3e2010-12-07 11:43:38 -08004106 ArmLIR *branchToPCR = (ArmLIR *) dvmCompilerNew(sizeof(ArmLIR), true);
Dan Bornstein9a1f8162010-12-01 17:02:26 -08004107 branchToPCR->opcode = kThumbBUncond;
Ben Cheng4238ec22009-08-24 16:32:22 -07004108 branchToPCR->generic.target = (LIR *) pcrLabel;
Ben Chengdcf3e5d2009-09-11 13:42:05 -07004109 setupResourceMasks(branchToPCR);
Ben Cheng4238ec22009-08-24 16:32:22 -07004110 cUnit->loopAnalysis->branchToPCR = (LIR *) branchToPCR;
4111}
4112
Ben Chengd5adae12010-03-26 17:45:28 -07004113#if defined(WITH_SELF_VERIFICATION)
4114static bool selfVerificationPuntOps(MIR *mir)
4115{
4116 DecodedInstruction *decInsn = &mir->dalvikInsn;
Dan Bornstein9a1f8162010-12-01 17:02:26 -08004117 Opcode op = decInsn->opcode;
Ben Cheng7a2697d2010-06-07 13:44:23 -07004118
Ben Chengd5adae12010-03-26 17:45:28 -07004119 /*
4120 * All opcodes that can throw exceptions and use the
4121 * TEMPLATE_THROW_EXCEPTION_COMMON template should be excluded in the trace
4122 * under self-verification mode.
4123 */
4124 return (op == OP_MONITOR_ENTER || op == OP_MONITOR_EXIT ||
4125 op == OP_NEW_INSTANCE || op == OP_NEW_ARRAY ||
4126 op == OP_CHECK_CAST || op == OP_MOVE_EXCEPTION ||
4127 op == OP_FILL_ARRAY_DATA || op == OP_EXECUTE_INLINE ||
Ben Cheng7a2697d2010-06-07 13:44:23 -07004128 op == OP_EXECUTE_INLINE_RANGE);
Ben Chengd5adae12010-03-26 17:45:28 -07004129}
4130#endif
4131
Ben Chengba4fc8b2009-06-01 13:00:29 -07004132void dvmCompilerMIR2LIR(CompilationUnit *cUnit)
4133{
4134 /* Used to hold the labels of each block */
Bill Buzbee89efc3d2009-07-28 11:22:22 -07004135 ArmLIR *labelList =
Carl Shapirofc75f3e2010-12-07 11:43:38 -08004136 (ArmLIR *) dvmCompilerNew(sizeof(ArmLIR) * cUnit->numBlocks, true);
Ben Chengcec26f62010-01-15 15:29:33 -08004137 GrowableList chainingListByType[kChainingCellGap];
Ben Chengba4fc8b2009-06-01 13:00:29 -07004138 int i;
4139
4140 /*
Ben Cheng38329f52009-07-07 14:19:20 -07004141 * Initialize various types chaining lists.
Ben Chengba4fc8b2009-06-01 13:00:29 -07004142 */
Ben Chengcec26f62010-01-15 15:29:33 -08004143 for (i = 0; i < kChainingCellGap; i++) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07004144 dvmInitGrowableList(&chainingListByType[i], 2);
4145 }
4146
Ben Cheng7ab74e12011-02-03 14:02:06 -08004147 /* Clear the visited flag for each block */
4148 dvmCompilerDataFlowAnalysisDispatcher(cUnit, dvmCompilerClearVisitedFlag,
4149 kAllNodes, false /* isIterative */);
4150
Ben Cheng00603072010-10-28 11:13:58 -07004151 GrowableListIterator iterator;
4152 dvmGrowableListIteratorInit(&cUnit->blockList, &iterator);
Ben Chengba4fc8b2009-06-01 13:00:29 -07004153
buzbee2e152ba2010-12-15 16:32:35 -08004154 /* Traces start with a profiling entry point. Generate it here */
4155 cUnit->profileCodeSize = genTraceProfileEntry(cUnit);
Ben Cheng1efc9c52009-06-08 18:25:27 -07004156
Ben Chengba4fc8b2009-06-01 13:00:29 -07004157 /* Handle the content in each basic block */
Ben Cheng00603072010-10-28 11:13:58 -07004158 for (i = 0; ; i++) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07004159 MIR *mir;
Ben Cheng00603072010-10-28 11:13:58 -07004160 BasicBlock *bb = (BasicBlock *) dvmGrowableListIteratorNext(&iterator);
4161 if (bb == NULL) break;
Ben Cheng7ab74e12011-02-03 14:02:06 -08004162 if (bb->visited == true) continue;
Ben Chengba4fc8b2009-06-01 13:00:29 -07004163
Ben Cheng00603072010-10-28 11:13:58 -07004164 labelList[i].operands[0] = bb->startOffset;
Ben Chengba4fc8b2009-06-01 13:00:29 -07004165
Ben Cheng00603072010-10-28 11:13:58 -07004166 if (bb->blockType >= kChainingCellGap) {
4167 if (bb->isFallThroughFromInvoke == true) {
Ben Chengd44faf52010-06-02 15:33:51 -07004168 /* Align this block first since it is a return chaining cell */
4169 newLIR0(cUnit, kArmPseudoPseudoAlign4);
4170 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07004171 /*
4172 * Append the label pseudo LIR first. Chaining cells will be handled
4173 * separately afterwards.
4174 */
4175 dvmCompilerAppendLIR(cUnit, (LIR *) &labelList[i]);
4176 }
4177
Ben Cheng00603072010-10-28 11:13:58 -07004178 if (bb->blockType == kTraceEntryBlock) {
Dan Bornstein9a1f8162010-12-01 17:02:26 -08004179 labelList[i].opcode = kArmPseudoEntryBlock;
Ben Cheng00603072010-10-28 11:13:58 -07004180 if (bb->firstMIRInsn == NULL) {
Ben Cheng4238ec22009-08-24 16:32:22 -07004181 continue;
4182 } else {
Ben Cheng00603072010-10-28 11:13:58 -07004183 setupLoopEntryBlock(cUnit, bb,
4184 &labelList[bb->fallThrough->id]);
Ben Cheng4238ec22009-08-24 16:32:22 -07004185 }
Ben Cheng00603072010-10-28 11:13:58 -07004186 } else if (bb->blockType == kTraceExitBlock) {
Dan Bornstein9a1f8162010-12-01 17:02:26 -08004187 labelList[i].opcode = kArmPseudoExitBlock;
Ben Cheng4238ec22009-08-24 16:32:22 -07004188 goto gen_fallthrough;
Ben Cheng00603072010-10-28 11:13:58 -07004189 } else if (bb->blockType == kDalvikByteCode) {
Dan Bornstein9a1f8162010-12-01 17:02:26 -08004190 labelList[i].opcode = kArmPseudoNormalBlockLabel;
Ben Chenge9695e52009-06-16 16:11:47 -07004191 /* Reset the register state */
Bill Buzbeec6f10662010-02-09 11:16:15 -08004192 dvmCompilerResetRegPool(cUnit);
4193 dvmCompilerClobberAllRegs(cUnit);
4194 dvmCompilerResetNullCheck(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -07004195 } else {
Ben Cheng00603072010-10-28 11:13:58 -07004196 switch (bb->blockType) {
Bill Buzbee1465db52009-09-23 17:17:35 -07004197 case kChainingCellNormal:
Dan Bornstein9a1f8162010-12-01 17:02:26 -08004198 labelList[i].opcode = kArmPseudoChainingCellNormal;
Ben Chengba4fc8b2009-06-01 13:00:29 -07004199 /* handle the codegen later */
4200 dvmInsertGrowableList(
Ben Cheng00603072010-10-28 11:13:58 -07004201 &chainingListByType[kChainingCellNormal], i);
Ben Chengba4fc8b2009-06-01 13:00:29 -07004202 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07004203 case kChainingCellInvokeSingleton:
Dan Bornstein9a1f8162010-12-01 17:02:26 -08004204 labelList[i].opcode =
Ben Chenga4973592010-03-31 11:59:18 -07004205 kArmPseudoChainingCellInvokeSingleton;
Ben Chengba4fc8b2009-06-01 13:00:29 -07004206 labelList[i].operands[0] =
Ben Cheng00603072010-10-28 11:13:58 -07004207 (int) bb->containingMethod;
Ben Chengba4fc8b2009-06-01 13:00:29 -07004208 /* handle the codegen later */
4209 dvmInsertGrowableList(
Ben Cheng00603072010-10-28 11:13:58 -07004210 &chainingListByType[kChainingCellInvokeSingleton], i);
Ben Cheng38329f52009-07-07 14:19:20 -07004211 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07004212 case kChainingCellInvokePredicted:
Dan Bornstein9a1f8162010-12-01 17:02:26 -08004213 labelList[i].opcode =
Ben Chenga4973592010-03-31 11:59:18 -07004214 kArmPseudoChainingCellInvokePredicted;
Ben Cheng38329f52009-07-07 14:19:20 -07004215 /* handle the codegen later */
4216 dvmInsertGrowableList(
Ben Cheng00603072010-10-28 11:13:58 -07004217 &chainingListByType[kChainingCellInvokePredicted], i);
Ben Chengba4fc8b2009-06-01 13:00:29 -07004218 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07004219 case kChainingCellHot:
Dan Bornstein9a1f8162010-12-01 17:02:26 -08004220 labelList[i].opcode =
Ben Chenga4973592010-03-31 11:59:18 -07004221 kArmPseudoChainingCellHot;
Ben Chengba4fc8b2009-06-01 13:00:29 -07004222 /* handle the codegen later */
4223 dvmInsertGrowableList(
Ben Cheng00603072010-10-28 11:13:58 -07004224 &chainingListByType[kChainingCellHot], i);
Ben Chengba4fc8b2009-06-01 13:00:29 -07004225 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07004226 case kPCReconstruction:
Ben Chengba4fc8b2009-06-01 13:00:29 -07004227 /* Make sure exception handling block is next */
Dan Bornstein9a1f8162010-12-01 17:02:26 -08004228 labelList[i].opcode =
Ben Chenga4973592010-03-31 11:59:18 -07004229 kArmPseudoPCReconstructionBlockLabel;
Ben Chengba4fc8b2009-06-01 13:00:29 -07004230 assert (i == cUnit->numBlocks - 2);
4231 handlePCReconstruction(cUnit, &labelList[i+1]);
4232 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07004233 case kExceptionHandling:
Dan Bornstein9a1f8162010-12-01 17:02:26 -08004234 labelList[i].opcode = kArmPseudoEHBlockLabel;
Ben Chengba4fc8b2009-06-01 13:00:29 -07004235 if (cUnit->pcReconstructionList.numUsed) {
Bill Buzbee270c1d62009-08-13 16:58:07 -07004236 loadWordDisp(cUnit, rGLUE, offsetof(InterpState,
4237 jitToInterpEntries.dvmJitToInterpPunt),
4238 r1);
Bill Buzbee1465db52009-09-23 17:17:35 -07004239 opReg(cUnit, kOpBlx, r1);
Ben Chengba4fc8b2009-06-01 13:00:29 -07004240 }
4241 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07004242 case kChainingCellBackwardBranch:
Dan Bornstein9a1f8162010-12-01 17:02:26 -08004243 labelList[i].opcode =
Ben Chenga4973592010-03-31 11:59:18 -07004244 kArmPseudoChainingCellBackwardBranch;
Jeff Hao97319a82009-08-12 16:57:15 -07004245 /* handle the codegen later */
4246 dvmInsertGrowableList(
Bill Buzbee1465db52009-09-23 17:17:35 -07004247 &chainingListByType[kChainingCellBackwardBranch],
Ben Cheng00603072010-10-28 11:13:58 -07004248 i);
Jeff Hao97319a82009-08-12 16:57:15 -07004249 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07004250 default:
4251 break;
4252 }
4253 continue;
4254 }
Ben Chenge9695e52009-06-16 16:11:47 -07004255
Bill Buzbee89efc3d2009-07-28 11:22:22 -07004256 ArmLIR *headLIR = NULL;
Ben Cheng7ab74e12011-02-03 14:02:06 -08004257 BasicBlock *nextBB = bb;
Ben Chenge9695e52009-06-16 16:11:47 -07004258
Ben Cheng7ab74e12011-02-03 14:02:06 -08004259 /*
4260 * Try to build a longer optimization unit. Currently if the previous
4261 * block ends with a goto, we continue adding instructions and don't
4262 * reset the register allocation pool.
4263 */
4264 for (; nextBB != NULL; nextBB = cUnit->nextCodegenBlock) {
4265 bb = nextBB;
4266 bb->visited = true;
4267 cUnit->nextCodegenBlock = NULL;
Bill Buzbee1465db52009-09-23 17:17:35 -07004268
Ben Cheng7ab74e12011-02-03 14:02:06 -08004269 for (mir = bb->firstMIRInsn; mir; mir = mir->next) {
Bill Buzbee1465db52009-09-23 17:17:35 -07004270
Ben Cheng7ab74e12011-02-03 14:02:06 -08004271 dvmCompilerResetRegPool(cUnit);
4272 if (gDvmJit.disableOpt & (1 << kTrackLiveTemps)) {
4273 dvmCompilerClobberAllRegs(cUnit);
Ben Cheng80211d22011-01-14 10:23:37 -08004274 }
Ben Cheng4238ec22009-08-24 16:32:22 -07004275
Ben Cheng7ab74e12011-02-03 14:02:06 -08004276 if (gDvmJit.disableOpt & (1 << kSuppressLoads)) {
4277 dvmCompilerResetDefTracking(cUnit);
4278 }
Ben Cheng4238ec22009-08-24 16:32:22 -07004279
Ben Cheng7ab74e12011-02-03 14:02:06 -08004280 if (mir->dalvikInsn.opcode >= kMirOpFirst) {
4281 handleExtendedMIR(cUnit, mir);
4282 continue;
4283 }
4284
4285
4286 Opcode dalvikOpcode = mir->dalvikInsn.opcode;
4287 InstructionFormat dalvikFormat =
4288 dexGetFormatFromOpcode(dalvikOpcode);
4289 char *note;
4290 if (mir->OptimizationFlags & MIR_INLINED) {
4291 note = " (I)";
4292 } else if (mir->OptimizationFlags & MIR_INLINED_PRED) {
4293 note = " (PI)";
4294 } else if (mir->OptimizationFlags & MIR_CALLEE) {
4295 note = " (C)";
4296 } else {
4297 note = NULL;
4298 }
4299
4300 ArmLIR *boundaryLIR;
4301
4302 /*
4303 * Don't generate the boundary LIR unless we are debugging this
4304 * trace or we need a scheduling barrier.
4305 */
4306 if (headLIR == NULL || cUnit->printMe == true) {
4307 boundaryLIR =
4308 newLIR2(cUnit, kArmPseudoDalvikByteCodeBoundary,
4309 mir->offset,
4310 (int) dvmCompilerGetDalvikDisassembly(
4311 &mir->dalvikInsn, note));
4312 /* Remember the first LIR for this block */
4313 if (headLIR == NULL) {
4314 headLIR = boundaryLIR;
4315 /* Set the first boundaryLIR as a scheduling barrier */
4316 headLIR->defMask = ENCODE_ALL;
4317 }
4318 }
4319
4320 /*
4321 * Don't generate the SSA annotation unless verbose mode is on
4322 */
4323 if (cUnit->printMe && mir->ssaRep) {
4324 char *ssaString = dvmCompilerGetSSAString(cUnit,
4325 mir->ssaRep);
4326 newLIR1(cUnit, kArmPseudoSSARep, (int) ssaString);
4327 }
4328
4329 bool notHandled;
4330 /*
4331 * Debugging: screen the opcode first to see if it is in the
4332 * do[-not]-compile list
4333 */
4334 bool singleStepMe = SINGLE_STEP_OP(dalvikOpcode);
Ben Chengd5adae12010-03-26 17:45:28 -07004335#if defined(WITH_SELF_VERIFICATION)
Ben Cheng7ab74e12011-02-03 14:02:06 -08004336 if (singleStepMe == false) {
4337 singleStepMe = selfVerificationPuntOps(mir);
4338 }
Ben Chengd5adae12010-03-26 17:45:28 -07004339#endif
Ben Cheng7ab74e12011-02-03 14:02:06 -08004340 if (singleStepMe || cUnit->allSingleStep) {
4341 notHandled = false;
4342 genInterpSingleStep(cUnit, mir);
4343 } else {
4344 opcodeCoverage[dalvikOpcode]++;
4345 switch (dalvikFormat) {
4346 case kFmt10t:
4347 case kFmt20t:
4348 case kFmt30t:
4349 notHandled = handleFmt10t_Fmt20t_Fmt30t(cUnit,
4350 mir, bb, labelList);
4351 break;
4352 case kFmt10x:
4353 notHandled = handleFmt10x(cUnit, mir);
4354 break;
4355 case kFmt11n:
4356 case kFmt31i:
4357 notHandled = handleFmt11n_Fmt31i(cUnit, mir);
4358 break;
4359 case kFmt11x:
4360 notHandled = handleFmt11x(cUnit, mir);
4361 break;
4362 case kFmt12x:
4363 notHandled = handleFmt12x(cUnit, mir);
4364 break;
4365 case kFmt20bc:
4366 case kFmt40sc:
4367 notHandled = handleFmt20bc_Fmt40sc(cUnit, mir);
4368 break;
4369 case kFmt21c:
4370 case kFmt31c:
4371 case kFmt41c:
4372 notHandled = handleFmt21c_Fmt31c_Fmt41c(cUnit, mir);
4373 break;
4374 case kFmt21h:
4375 notHandled = handleFmt21h(cUnit, mir);
4376 break;
4377 case kFmt21s:
4378 notHandled = handleFmt21s(cUnit, mir);
4379 break;
4380 case kFmt21t:
4381 notHandled = handleFmt21t(cUnit, mir, bb,
Ben Chengba4fc8b2009-06-01 13:00:29 -07004382 labelList);
Ben Cheng7ab74e12011-02-03 14:02:06 -08004383 break;
4384 case kFmt22b:
4385 case kFmt22s:
4386 notHandled = handleFmt22b_Fmt22s(cUnit, mir);
4387 break;
4388 case kFmt22c:
4389 case kFmt52c:
4390 notHandled = handleFmt22c_Fmt52c(cUnit, mir);
4391 break;
4392 case kFmt22cs:
4393 notHandled = handleFmt22cs(cUnit, mir);
4394 break;
4395 case kFmt22t:
4396 notHandled = handleFmt22t(cUnit, mir, bb,
4397 labelList);
4398 break;
4399 case kFmt22x:
4400 case kFmt32x:
4401 notHandled = handleFmt22x_Fmt32x(cUnit, mir);
4402 break;
4403 case kFmt23x:
4404 notHandled = handleFmt23x(cUnit, mir);
4405 break;
4406 case kFmt31t:
4407 notHandled = handleFmt31t(cUnit, mir);
4408 break;
4409 case kFmt3rc:
4410 case kFmt35c:
4411 case kFmt5rc:
4412 notHandled = handleFmt35c_3rc_5rc(cUnit, mir, bb,
4413 labelList);
4414 break;
4415 case kFmt3rms:
4416 case kFmt35ms:
4417 notHandled = handleFmt35ms_3rms(cUnit, mir, bb,
4418 labelList);
4419 break;
4420 case kFmt35mi:
4421 case kFmt3rmi:
4422 notHandled = handleExecuteInline(cUnit, mir);
4423 break;
4424 case kFmt51l:
4425 notHandled = handleFmt51l(cUnit, mir);
4426 break;
4427 default:
4428 notHandled = true;
4429 break;
4430 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07004431 }
Ben Cheng7ab74e12011-02-03 14:02:06 -08004432 if (notHandled) {
4433 LOGE("%#06x: Opcode 0x%x (%s) / Fmt %d not handled\n",
4434 mir->offset,
4435 dalvikOpcode, dexGetOpcodeName(dalvikOpcode),
4436 dalvikFormat);
4437 dvmCompilerAbort(cUnit);
4438 break;
4439 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07004440 }
4441 }
Ben Cheng4238ec22009-08-24 16:32:22 -07004442
Ben Cheng00603072010-10-28 11:13:58 -07004443 if (bb->blockType == kTraceEntryBlock) {
Ben Cheng4238ec22009-08-24 16:32:22 -07004444 dvmCompilerAppendLIR(cUnit,
4445 (LIR *) cUnit->loopAnalysis->branchToBody);
4446 dvmCompilerAppendLIR(cUnit,
4447 (LIR *) cUnit->loopAnalysis->branchToPCR);
4448 }
4449
4450 if (headLIR) {
4451 /*
4452 * Eliminate redundant loads/stores and delay stores into later
4453 * slots
4454 */
4455 dvmCompilerApplyLocalOptimizations(cUnit, (LIR *) headLIR,
4456 cUnit->lastLIRInsn);
4457 }
4458
4459gen_fallthrough:
Ben Cheng1efc9c52009-06-08 18:25:27 -07004460 /*
4461 * Check if the block is terminated due to trace length constraint -
4462 * insert an unconditional branch to the chaining cell.
4463 */
Ben Cheng00603072010-10-28 11:13:58 -07004464 if (bb->needFallThroughBranch) {
Ben Cheng7ab74e12011-02-03 14:02:06 -08004465 genUnconditionalBranch(cUnit, &labelList[bb->fallThrough->id]);
Ben Cheng1efc9c52009-06-08 18:25:27 -07004466 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07004467 }
4468
Ben Chenge9695e52009-06-16 16:11:47 -07004469 /* Handle the chaining cells in predefined order */
Ben Chengcec26f62010-01-15 15:29:33 -08004470 for (i = 0; i < kChainingCellGap; i++) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07004471 size_t j;
4472 int *blockIdList = (int *) chainingListByType[i].elemList;
4473
4474 cUnit->numChainingCells[i] = chainingListByType[i].numUsed;
4475
4476 /* No chaining cells of this type */
4477 if (cUnit->numChainingCells[i] == 0)
4478 continue;
4479
4480 /* Record the first LIR for a new type of chaining cell */
4481 cUnit->firstChainingLIR[i] = (LIR *) &labelList[blockIdList[0]];
4482
4483 for (j = 0; j < chainingListByType[i].numUsed; j++) {
4484 int blockId = blockIdList[j];
Ben Cheng00603072010-10-28 11:13:58 -07004485 BasicBlock *chainingBlock =
4486 (BasicBlock *) dvmGrowableListGetElement(&cUnit->blockList,
4487 blockId);
Ben Chengba4fc8b2009-06-01 13:00:29 -07004488
4489 /* Align this chaining cell first */
Bill Buzbee1465db52009-09-23 17:17:35 -07004490 newLIR0(cUnit, kArmPseudoPseudoAlign4);
Ben Chengba4fc8b2009-06-01 13:00:29 -07004491
4492 /* Insert the pseudo chaining instruction */
4493 dvmCompilerAppendLIR(cUnit, (LIR *) &labelList[blockId]);
4494
4495
Ben Cheng00603072010-10-28 11:13:58 -07004496 switch (chainingBlock->blockType) {
Bill Buzbee1465db52009-09-23 17:17:35 -07004497 case kChainingCellNormal:
Ben Cheng00603072010-10-28 11:13:58 -07004498 handleNormalChainingCell(cUnit, chainingBlock->startOffset);
Ben Chengba4fc8b2009-06-01 13:00:29 -07004499 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07004500 case kChainingCellInvokeSingleton:
Ben Cheng38329f52009-07-07 14:19:20 -07004501 handleInvokeSingletonChainingCell(cUnit,
Ben Cheng00603072010-10-28 11:13:58 -07004502 chainingBlock->containingMethod);
Ben Chengba4fc8b2009-06-01 13:00:29 -07004503 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07004504 case kChainingCellInvokePredicted:
Ben Cheng38329f52009-07-07 14:19:20 -07004505 handleInvokePredictedChainingCell(cUnit);
4506 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07004507 case kChainingCellHot:
Ben Cheng00603072010-10-28 11:13:58 -07004508 handleHotChainingCell(cUnit, chainingBlock->startOffset);
Ben Chengba4fc8b2009-06-01 13:00:29 -07004509 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07004510 case kChainingCellBackwardBranch:
Jeff Hao97319a82009-08-12 16:57:15 -07004511 handleBackwardBranchChainingCell(cUnit,
Ben Cheng00603072010-10-28 11:13:58 -07004512 chainingBlock->startOffset);
Jeff Hao97319a82009-08-12 16:57:15 -07004513 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07004514 default:
Ben Cheng00603072010-10-28 11:13:58 -07004515 LOGE("Bad blocktype %d", chainingBlock->blockType);
Bill Buzbeefc519dc2010-03-06 23:30:57 -08004516 dvmCompilerAbort(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -07004517 }
4518 }
4519 }
Ben Chenge9695e52009-06-16 16:11:47 -07004520
Ben Chengcec26f62010-01-15 15:29:33 -08004521 /* Mark the bottom of chaining cells */
4522 cUnit->chainingCellBottom = (LIR *) newLIR0(cUnit, kArmChainingCellBottom);
4523
Ben Cheng6c10a972009-10-29 14:39:18 -07004524 /*
4525 * Generate the branch to the dvmJitToInterpNoChain entry point at the end
4526 * of all chaining cells for the overflow cases.
4527 */
4528 if (cUnit->switchOverflowPad) {
4529 loadConstant(cUnit, r0, (int) cUnit->switchOverflowPad);
4530 loadWordDisp(cUnit, rGLUE, offsetof(InterpState,
4531 jitToInterpEntries.dvmJitToInterpNoChain), r2);
4532 opRegReg(cUnit, kOpAdd, r1, r1);
4533 opRegRegReg(cUnit, kOpAdd, r4PC, r0, r1);
Ben Cheng978738d2010-05-13 13:45:57 -07004534#if defined(WITH_JIT_TUNING)
Ben Cheng6c10a972009-10-29 14:39:18 -07004535 loadConstant(cUnit, r0, kSwitchOverflow);
4536#endif
4537 opReg(cUnit, kOpBlx, r2);
4538 }
4539
Ben Chenge9695e52009-06-16 16:11:47 -07004540 dvmCompilerApplyGlobalOptimizations(cUnit);
jeffhao9e45c0b2010-02-03 10:24:05 -08004541
4542#if defined(WITH_SELF_VERIFICATION)
4543 selfVerificationBranchInsertPass(cUnit);
4544#endif
Ben Chengba4fc8b2009-06-01 13:00:29 -07004545}
4546
buzbee2e152ba2010-12-15 16:32:35 -08004547/*
4548 * Accept the work and start compiling. Returns true if compilation
4549 * is attempted.
4550 */
Bill Buzbee716f1202009-07-23 13:22:09 -07004551bool dvmCompilerDoWork(CompilerWorkOrder *work)
Ben Chengba4fc8b2009-06-01 13:00:29 -07004552{
Carl Shapirofc75f3e2010-12-07 11:43:38 -08004553 JitTraceDescription *desc;
buzbee2e152ba2010-12-15 16:32:35 -08004554 bool isCompile;
4555 bool success = true;
Ben Chengba4fc8b2009-06-01 13:00:29 -07004556
Ben Cheng6999d842010-01-26 16:46:15 -08004557 if (gDvmJit.codeCacheFull) {
Ben Chengccd6c012009-10-15 14:52:45 -07004558 return false;
4559 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07004560
Ben Chengccd6c012009-10-15 14:52:45 -07004561 switch (work->kind) {
Ben Chengccd6c012009-10-15 14:52:45 -07004562 case kWorkOrderTrace:
buzbee2e152ba2010-12-15 16:32:35 -08004563 isCompile = true;
Ben Chengccd6c012009-10-15 14:52:45 -07004564 /* Start compilation with maximally allowed trace length */
Carl Shapirofc75f3e2010-12-07 11:43:38 -08004565 desc = (JitTraceDescription *)work->info;
buzbee2e152ba2010-12-15 16:32:35 -08004566 success = dvmCompileTrace(desc, JIT_MAX_TRACE_LEN, &work->result,
4567 work->bailPtr, 0 /* no hints */);
Ben Chengccd6c012009-10-15 14:52:45 -07004568 break;
4569 case kWorkOrderTraceDebug: {
4570 bool oldPrintMe = gDvmJit.printMe;
4571 gDvmJit.printMe = true;
buzbee2e152ba2010-12-15 16:32:35 -08004572 isCompile = true;
Ben Chengccd6c012009-10-15 14:52:45 -07004573 /* Start compilation with maximally allowed trace length */
Carl Shapirofc75f3e2010-12-07 11:43:38 -08004574 desc = (JitTraceDescription *)work->info;
buzbee2e152ba2010-12-15 16:32:35 -08004575 success = dvmCompileTrace(desc, JIT_MAX_TRACE_LEN, &work->result,
4576 work->bailPtr, 0 /* no hints */);
Elliott Hughes672511b2010-04-26 17:40:13 -07004577 gDvmJit.printMe = oldPrintMe;
Ben Chengccd6c012009-10-15 14:52:45 -07004578 break;
4579 }
buzbee2e152ba2010-12-15 16:32:35 -08004580 case kWorkOrderProfileMode:
4581 dvmJitChangeProfileMode((TraceProfilingModes)work->info);
4582 isCompile = false;
4583 break;
Ben Chengccd6c012009-10-15 14:52:45 -07004584 default:
buzbee2e152ba2010-12-15 16:32:35 -08004585 isCompile = false;
Bill Buzbeefc519dc2010-03-06 23:30:57 -08004586 LOGE("Jit: unknown work order type");
Elliott Hughes672511b2010-04-26 17:40:13 -07004587 assert(0); // Bail if debug build, discard otherwise
Ben Chengccd6c012009-10-15 14:52:45 -07004588 }
buzbee2e152ba2010-12-15 16:32:35 -08004589 if (!success)
4590 work->result.codeAddress = NULL;
4591 return isCompile;
Ben Chengba4fc8b2009-06-01 13:00:29 -07004592}
4593
Ben Chengba4fc8b2009-06-01 13:00:29 -07004594/* Architectural-specific debugging helpers go here */
4595void dvmCompilerArchDump(void)
4596{
4597 /* Print compiled opcode in this VM instance */
4598 int i, start, streak;
4599 char buf[1024];
4600
4601 streak = i = 0;
4602 buf[0] = 0;
Dan Bornsteinccaab182010-12-03 15:32:40 -08004603 while (opcodeCoverage[i] == 0 && i < kNumPackedOpcodes) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07004604 i++;
4605 }
Dan Bornsteinccaab182010-12-03 15:32:40 -08004606 if (i == kNumPackedOpcodes) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07004607 return;
4608 }
Dan Bornsteinccaab182010-12-03 15:32:40 -08004609 for (start = i++, streak = 1; i < kNumPackedOpcodes; i++) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07004610 if (opcodeCoverage[i]) {
4611 streak++;
4612 } else {
4613 if (streak == 1) {
4614 sprintf(buf+strlen(buf), "%x,", start);
4615 } else {
4616 sprintf(buf+strlen(buf), "%x-%x,", start, start + streak - 1);
4617 }
4618 streak = 0;
Dan Bornsteinccaab182010-12-03 15:32:40 -08004619 while (opcodeCoverage[i] == 0 && i < kNumPackedOpcodes) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07004620 i++;
4621 }
Dan Bornsteinccaab182010-12-03 15:32:40 -08004622 if (i < kNumPackedOpcodes) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07004623 streak = 1;
4624 start = i;
4625 }
4626 }
4627 }
4628 if (streak) {
4629 if (streak == 1) {
4630 sprintf(buf+strlen(buf), "%x", start);
4631 } else {
4632 sprintf(buf+strlen(buf), "%x-%x", start, start + streak - 1);
4633 }
4634 }
4635 if (strlen(buf)) {
Ben Cheng8b258bf2009-06-24 17:27:07 -07004636 LOGD("dalvik.vm.jit.op = %s", buf);
Ben Chengba4fc8b2009-06-01 13:00:29 -07004637 }
4638}
Ben Chengd7d426a2009-09-22 11:23:36 -07004639
4640/* Common initialization routine for an architecture family */
4641bool dvmCompilerArchInit()
4642{
4643 int i;
4644
Bill Buzbee1465db52009-09-23 17:17:35 -07004645 for (i = 0; i < kArmLast; i++) {
Dan Bornstein9a1f8162010-12-01 17:02:26 -08004646 if (EncodingMap[i].opcode != i) {
Ben Chengd7d426a2009-09-22 11:23:36 -07004647 LOGE("Encoding order for %s is wrong: expecting %d, seeing %d",
Dan Bornstein9a1f8162010-12-01 17:02:26 -08004648 EncodingMap[i].name, i, EncodingMap[i].opcode);
Bill Buzbeefc519dc2010-03-06 23:30:57 -08004649 dvmAbort(); // OK to dvmAbort - build error
Ben Chengd7d426a2009-09-22 11:23:36 -07004650 }
4651 }
4652
Ben Cheng5d90c202009-11-22 23:31:11 -08004653 return dvmCompilerArchVariantInit();
4654}
4655
4656void *dvmCompilerGetInterpretTemplate()
4657{
4658 return (void*) ((int)gDvmJit.codeCache +
4659 templateEntryOffsets[TEMPLATE_INTERPRET]);
4660}
4661
Bill Buzbee1b3da592011-02-03 07:38:22 -08004662JitInstructionSetType dvmCompilerGetInterpretTemplateSet()
4663{
4664 return DALVIK_JIT_ARM;
4665}
4666
buzbeebff121a2010-08-04 15:25:06 -07004667/* Needed by the Assembler */
4668void dvmCompilerSetupResourceMasks(ArmLIR *lir)
4669{
4670 setupResourceMasks(lir);
4671}
4672
Ben Cheng5d90c202009-11-22 23:31:11 -08004673/* Needed by the ld/st optmizatons */
4674ArmLIR* dvmCompilerRegCopyNoInsert(CompilationUnit *cUnit, int rDest, int rSrc)
4675{
4676 return genRegCopyNoInsert(cUnit, rDest, rSrc);
4677}
4678
4679/* Needed by the register allocator */
4680ArmLIR* dvmCompilerRegCopy(CompilationUnit *cUnit, int rDest, int rSrc)
4681{
4682 return genRegCopy(cUnit, rDest, rSrc);
4683}
4684
4685/* Needed by the register allocator */
4686void dvmCompilerRegCopyWide(CompilationUnit *cUnit, int destLo, int destHi,
4687 int srcLo, int srcHi)
4688{
4689 genRegCopyWide(cUnit, destLo, destHi, srcLo, srcHi);
4690}
4691
4692void dvmCompilerFlushRegImpl(CompilationUnit *cUnit, int rBase,
4693 int displacement, int rSrc, OpSize size)
4694{
4695 storeBaseDisp(cUnit, rBase, displacement, rSrc, size);
4696}
4697
4698void dvmCompilerFlushRegWideImpl(CompilationUnit *cUnit, int rBase,
4699 int displacement, int rSrcLo, int rSrcHi)
4700{
4701 storeBaseDispWide(cUnit, rBase, displacement, rSrcLo, rSrcHi);
Ben Chengd7d426a2009-09-22 11:23:36 -07004702}