blob: 02e6f877ea5b6eeec9ace3809300083ce830b87e [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);
buzbee9f601a92011-02-11 17:48:20 -080035 loadWordDisp(cUnit, rSELF, offsetof(Thread, cardTable),
buzbee919eb062010-07-12 12:59:22 -070036 regCardBase);
37 opRegRegImm(cUnit, kOpLsr, regCardNo, tgtAddrReg, GC_CARD_SHIFT);
38 storeBaseIndexed(cUnit, regCardBase, regCardNo, regCardBase, 0,
39 kUnsignedByte);
40 ArmLIR *target = newLIR0(cUnit, kArmPseudoTargetLabel);
41 target->defMask = ENCODE_ALL;
42 branchOver->generic.target = (LIR *)target;
buzbeebaf196a2010-08-04 10:13:15 -070043 dvmCompilerFreeTemp(cUnit, regCardBase);
44 dvmCompilerFreeTemp(cUnit, regCardNo);
buzbee919eb062010-07-12 12:59:22 -070045}
46
Ben Cheng5d90c202009-11-22 23:31:11 -080047static bool genConversionCall(CompilationUnit *cUnit, MIR *mir, void *funct,
48 int srcSize, int tgtSize)
49{
50 /*
51 * Don't optimize the register usage since it calls out to template
52 * functions
53 */
54 RegLocation rlSrc;
55 RegLocation rlDest;
Bill Buzbeec6f10662010-02-09 11:16:15 -080056 dvmCompilerFlushAllRegs(cUnit); /* Send everything to home location */
Ben Cheng5d90c202009-11-22 23:31:11 -080057 if (srcSize == 1) {
Bill Buzbeec6f10662010-02-09 11:16:15 -080058 rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
Ben Cheng5d90c202009-11-22 23:31:11 -080059 loadValueDirectFixed(cUnit, rlSrc, r0);
60 } else {
Bill Buzbeec6f10662010-02-09 11:16:15 -080061 rlSrc = dvmCompilerGetSrcWide(cUnit, mir, 0, 1);
Ben Cheng5d90c202009-11-22 23:31:11 -080062 loadValueDirectWideFixed(cUnit, rlSrc, r0, r1);
63 }
Ben Chengbd1326d2010-04-02 15:04:53 -070064 LOAD_FUNC_ADDR(cUnit, r2, (int)funct);
Ben Cheng5d90c202009-11-22 23:31:11 -080065 opReg(cUnit, kOpBlx, r2);
Elliott Hughes6a555132010-02-25 15:41:42 -080066 dvmCompilerClobberCallRegs(cUnit);
Ben Cheng5d90c202009-11-22 23:31:11 -080067 if (tgtSize == 1) {
68 RegLocation rlResult;
Bill Buzbeec6f10662010-02-09 11:16:15 -080069 rlDest = dvmCompilerGetDest(cUnit, mir, 0);
70 rlResult = dvmCompilerGetReturn(cUnit);
Ben Cheng5d90c202009-11-22 23:31:11 -080071 storeValue(cUnit, rlDest, rlResult);
72 } else {
73 RegLocation rlResult;
Bill Buzbeec6f10662010-02-09 11:16:15 -080074 rlDest = dvmCompilerGetDestWide(cUnit, mir, 0, 1);
75 rlResult = dvmCompilerGetReturnWide(cUnit);
Ben Cheng5d90c202009-11-22 23:31:11 -080076 storeValueWide(cUnit, rlDest, rlResult);
77 }
78 return false;
79}
Ben Chengba4fc8b2009-06-01 13:00:29 -070080
Ben Cheng5d90c202009-11-22 23:31:11 -080081static bool genArithOpFloatPortable(CompilationUnit *cUnit, MIR *mir,
82 RegLocation rlDest, RegLocation rlSrc1,
83 RegLocation rlSrc2)
84{
85 RegLocation rlResult;
86 void* funct;
87
Dan Bornstein9a1f8162010-12-01 17:02:26 -080088 switch (mir->dalvikInsn.opcode) {
Ben Cheng5d90c202009-11-22 23:31:11 -080089 case OP_ADD_FLOAT_2ADDR:
90 case OP_ADD_FLOAT:
91 funct = (void*) __aeabi_fadd;
92 break;
93 case OP_SUB_FLOAT_2ADDR:
94 case OP_SUB_FLOAT:
95 funct = (void*) __aeabi_fsub;
96 break;
97 case OP_DIV_FLOAT_2ADDR:
98 case OP_DIV_FLOAT:
99 funct = (void*) __aeabi_fdiv;
100 break;
101 case OP_MUL_FLOAT_2ADDR:
102 case OP_MUL_FLOAT:
103 funct = (void*) __aeabi_fmul;
104 break;
105 case OP_REM_FLOAT_2ADDR:
106 case OP_REM_FLOAT:
107 funct = (void*) fmodf;
108 break;
109 case OP_NEG_FLOAT: {
110 genNegFloat(cUnit, rlDest, rlSrc1);
111 return false;
112 }
113 default:
114 return true;
115 }
Bill Buzbeec6f10662010-02-09 11:16:15 -0800116 dvmCompilerFlushAllRegs(cUnit); /* Send everything to home location */
Ben Cheng5d90c202009-11-22 23:31:11 -0800117 loadValueDirectFixed(cUnit, rlSrc1, r0);
118 loadValueDirectFixed(cUnit, rlSrc2, r1);
Ben Chengbd1326d2010-04-02 15:04:53 -0700119 LOAD_FUNC_ADDR(cUnit, r2, (int)funct);
Ben Cheng5d90c202009-11-22 23:31:11 -0800120 opReg(cUnit, kOpBlx, r2);
Elliott Hughes6a555132010-02-25 15:41:42 -0800121 dvmCompilerClobberCallRegs(cUnit);
Bill Buzbeec6f10662010-02-09 11:16:15 -0800122 rlResult = dvmCompilerGetReturn(cUnit);
Ben Cheng5d90c202009-11-22 23:31:11 -0800123 storeValue(cUnit, rlDest, rlResult);
124 return false;
125}
126
127static bool genArithOpDoublePortable(CompilationUnit *cUnit, MIR *mir,
128 RegLocation rlDest, RegLocation rlSrc1,
129 RegLocation rlSrc2)
130{
131 RegLocation rlResult;
132 void* funct;
133
Dan Bornstein9a1f8162010-12-01 17:02:26 -0800134 switch (mir->dalvikInsn.opcode) {
Ben Cheng5d90c202009-11-22 23:31:11 -0800135 case OP_ADD_DOUBLE_2ADDR:
136 case OP_ADD_DOUBLE:
137 funct = (void*) __aeabi_dadd;
138 break;
139 case OP_SUB_DOUBLE_2ADDR:
140 case OP_SUB_DOUBLE:
141 funct = (void*) __aeabi_dsub;
142 break;
143 case OP_DIV_DOUBLE_2ADDR:
144 case OP_DIV_DOUBLE:
145 funct = (void*) __aeabi_ddiv;
146 break;
147 case OP_MUL_DOUBLE_2ADDR:
148 case OP_MUL_DOUBLE:
149 funct = (void*) __aeabi_dmul;
150 break;
151 case OP_REM_DOUBLE_2ADDR:
152 case OP_REM_DOUBLE:
153 funct = (void*) fmod;
154 break;
155 case OP_NEG_DOUBLE: {
156 genNegDouble(cUnit, rlDest, rlSrc1);
157 return false;
158 }
159 default:
160 return true;
161 }
Bill Buzbeec6f10662010-02-09 11:16:15 -0800162 dvmCompilerFlushAllRegs(cUnit); /* Send everything to home location */
Ben 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 }
buzbee9f601a92011-02-11 17:48:20 -0800976 /* TODO: Move result to Thread 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
buzbee9f601a92011-02-11 17:48:20 -08001251 genRegCopy(cUnit, r1, rSELF);
Ben Chengb88ec3c2010-05-17 12:50:33 -07001252
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));
buzbee9f601a92011-02-11 17:48:20 -08001290 loadWordDisp(cUnit, rSELF, offsetof(Thread,
Bill Buzbee270c1d62009-08-13 16:58:07 -07001291 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 }
buzbee9f601a92011-02-11 17:48:20 -08001316 int entryAddr = offsetof(Thread,
Ben Chengba4fc8b2009-06-01 13:00:29 -07001317 jitToInterpEntries.dvmJitToInterpSingleStep);
buzbee9f601a92011-02-11 17:48:20 -08001318 loadWordDisp(cUnit, rSELF, 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);
buzbee9f601a92011-02-11 17:48:20 -08001345 genRegCopy(cUnit, r0, rSELF);
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/*
buzbee9f601a92011-02-11 17:48:20 -08001371 * Fetch *self->suspendCount. If the suspend count is non-zero,
Ben Cheng7ab74e12011-02-03 14:02:06 -08001372 * punt to the interpreter.
1373 */
1374static void genSuspendPoll(CompilationUnit *cUnit, MIR *mir)
1375{
1376 int rTemp = dvmCompilerAllocTemp(cUnit);
1377 ArmLIR *ld;
buzbee9f601a92011-02-11 17:48:20 -08001378 ld = loadWordDisp(cUnit, rSELF, offsetof(Thread, suspendCount),
Ben Cheng7ab74e12011-02-03 14:02:06 -08001379 rTemp);
1380 setMemRefType(ld, true /* isLoad */, kMustNotAlias);
Ben Cheng7ab74e12011-02-03 14:02:06 -08001381 genRegImmCheck(cUnit, kArmCondNe, rTemp, 0, mir->offset, NULL);
1382}
1383
1384/*
Ben Chengba4fc8b2009-06-01 13:00:29 -07001385 * The following are the first-level codegen routines that analyze the format
1386 * of each bytecode then either dispatch special purpose codegen routines
1387 * or produce corresponding Thumb instructions directly.
1388 */
1389
1390static bool handleFmt10t_Fmt20t_Fmt30t(CompilationUnit *cUnit, MIR *mir,
Bill Buzbee89efc3d2009-07-28 11:22:22 -07001391 BasicBlock *bb, ArmLIR *labelList)
Ben Chengba4fc8b2009-06-01 13:00:29 -07001392{
Ben Cheng7ab74e12011-02-03 14:02:06 -08001393 /* backward branch? */
1394 bool backwardBranch = (bb->taken->startOffset <= mir->offset);
1395
1396 if (backwardBranch && gDvmJit.genSuspendPoll) {
1397 genSuspendPoll(cUnit, mir);
1398 }
1399
1400 int numPredecessors = dvmCountSetBits(bb->taken->predecessors);
1401 /*
1402 * Things could be hoisted out of the taken block into the predecessor, so
1403 * make sure it is dominated by the predecessor.
1404 */
1405 if (numPredecessors == 1 && bb->taken->visited == false &&
1406 bb->taken->blockType == kDalvikByteCode &&
1407 cUnit->methodJitMode == false ) {
1408 cUnit->nextCodegenBlock = bb->taken;
1409 } else {
1410 /* For OP_GOTO, OP_GOTO_16, and OP_GOTO_32 */
1411 genUnconditionalBranch(cUnit, &labelList[bb->taken->id]);
1412 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07001413 return false;
1414}
1415
1416static bool handleFmt10x(CompilationUnit *cUnit, MIR *mir)
1417{
Dan Bornstein9a1f8162010-12-01 17:02:26 -08001418 Opcode dalvikOpcode = mir->dalvikInsn.opcode;
1419 if ((dalvikOpcode >= OP_UNUSED_3E) && (dalvikOpcode <= OP_UNUSED_43)) {
1420 LOGE("Codegen: got unused opcode 0x%x\n",dalvikOpcode);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001421 return true;
1422 }
Dan Bornstein9a1f8162010-12-01 17:02:26 -08001423 switch (dalvikOpcode) {
Andy McFadden291758c2010-09-10 08:04:52 -07001424 case OP_RETURN_VOID_BARRIER:
buzbee2ce33c92010-11-01 15:53:27 -07001425 dvmCompilerGenMemBarrier(cUnit, kST);
1426 // Intentional fallthrough
1427 case OP_RETURN_VOID:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001428 genReturnCommon(cUnit,mir);
1429 break;
1430 case OP_UNUSED_73:
1431 case OP_UNUSED_79:
1432 case OP_UNUSED_7A:
Dan Bornstein90f15432010-12-02 16:46:25 -08001433 case OP_DISPATCH_FF:
Dan Bornstein9a1f8162010-12-01 17:02:26 -08001434 LOGE("Codegen: got unused opcode 0x%x\n",dalvikOpcode);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001435 return true;
1436 case OP_NOP:
1437 break;
1438 default:
1439 return true;
1440 }
1441 return false;
1442}
1443
1444static bool handleFmt11n_Fmt31i(CompilationUnit *cUnit, MIR *mir)
1445{
Bill Buzbee1465db52009-09-23 17:17:35 -07001446 RegLocation rlDest;
1447 RegLocation rlResult;
1448 if (mir->ssaRep->numDefs == 2) {
Bill Buzbeec6f10662010-02-09 11:16:15 -08001449 rlDest = dvmCompilerGetDestWide(cUnit, mir, 0, 1);
Bill Buzbee1465db52009-09-23 17:17:35 -07001450 } else {
Bill Buzbeec6f10662010-02-09 11:16:15 -08001451 rlDest = dvmCompilerGetDest(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07001452 }
Ben Chenge9695e52009-06-16 16:11:47 -07001453
Dan Bornstein9a1f8162010-12-01 17:02:26 -08001454 switch (mir->dalvikInsn.opcode) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07001455 case OP_CONST:
Ben Chenge9695e52009-06-16 16:11:47 -07001456 case OP_CONST_4: {
Bill Buzbeec6f10662010-02-09 11:16:15 -08001457 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kAnyReg, true);
Ben Chengbd1326d2010-04-02 15:04:53 -07001458 loadConstantNoClobber(cUnit, rlResult.lowReg, mir->dalvikInsn.vB);
Bill Buzbee1465db52009-09-23 17:17:35 -07001459 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001460 break;
Ben Chenge9695e52009-06-16 16:11:47 -07001461 }
1462 case OP_CONST_WIDE_32: {
Bill Buzbee1465db52009-09-23 17:17:35 -07001463 //TUNING: single routine to load constant pair for support doubles
Bill Buzbee964a7b02010-01-28 12:54:19 -08001464 //TUNING: load 0/-1 separately to avoid load dependency
Bill Buzbeec6f10662010-02-09 11:16:15 -08001465 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Ben Chengbd1326d2010-04-02 15:04:53 -07001466 loadConstantNoClobber(cUnit, rlResult.lowReg, mir->dalvikInsn.vB);
Bill Buzbee1465db52009-09-23 17:17:35 -07001467 opRegRegImm(cUnit, kOpAsr, rlResult.highReg,
1468 rlResult.lowReg, 31);
1469 storeValueWide(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001470 break;
Ben Chenge9695e52009-06-16 16:11:47 -07001471 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07001472 default:
1473 return true;
1474 }
1475 return false;
1476}
1477
1478static bool handleFmt21h(CompilationUnit *cUnit, MIR *mir)
1479{
Bill Buzbee1465db52009-09-23 17:17:35 -07001480 RegLocation rlDest;
1481 RegLocation rlResult;
1482 if (mir->ssaRep->numDefs == 2) {
Bill Buzbeec6f10662010-02-09 11:16:15 -08001483 rlDest = dvmCompilerGetDestWide(cUnit, mir, 0, 1);
Bill Buzbee1465db52009-09-23 17:17:35 -07001484 } else {
Bill Buzbeec6f10662010-02-09 11:16:15 -08001485 rlDest = dvmCompilerGetDest(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07001486 }
Bill Buzbeec6f10662010-02-09 11:16:15 -08001487 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kAnyReg, true);
Ben Chenge9695e52009-06-16 16:11:47 -07001488
Dan Bornstein9a1f8162010-12-01 17:02:26 -08001489 switch (mir->dalvikInsn.opcode) {
Ben Chenge9695e52009-06-16 16:11:47 -07001490 case OP_CONST_HIGH16: {
Ben Chengbd1326d2010-04-02 15:04:53 -07001491 loadConstantNoClobber(cUnit, rlResult.lowReg,
1492 mir->dalvikInsn.vB << 16);
Bill Buzbee1465db52009-09-23 17:17:35 -07001493 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001494 break;
Ben Chenge9695e52009-06-16 16:11:47 -07001495 }
1496 case OP_CONST_WIDE_HIGH16: {
Bill Buzbee1465db52009-09-23 17:17:35 -07001497 loadConstantValueWide(cUnit, rlResult.lowReg, rlResult.highReg,
1498 0, mir->dalvikInsn.vB << 16);
1499 storeValueWide(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001500 break;
Ben Chenge9695e52009-06-16 16:11:47 -07001501 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07001502 default:
1503 return true;
1504 }
1505 return false;
1506}
1507
jeffhao71eee1f2011-01-04 14:18:54 -08001508static bool handleFmt20bc_Fmt40sc(CompilationUnit *cUnit, MIR *mir)
Ben Chengba4fc8b2009-06-01 13:00:29 -07001509{
jeffhao71eee1f2011-01-04 14:18:54 -08001510 /* For OP_THROW_VERIFICATION_ERROR & OP_THROW_VERIFICATION_ERROR_JUMBO */
Ben Chengba4fc8b2009-06-01 13:00:29 -07001511 genInterpSingleStep(cUnit, mir);
1512 return false;
1513}
1514
jeffhao71eee1f2011-01-04 14:18:54 -08001515static bool handleFmt21c_Fmt31c_Fmt41c(CompilationUnit *cUnit, MIR *mir)
Ben Chengba4fc8b2009-06-01 13:00:29 -07001516{
Bill Buzbee1465db52009-09-23 17:17:35 -07001517 RegLocation rlResult;
1518 RegLocation rlDest;
1519 RegLocation rlSrc;
Ben Chenge9695e52009-06-16 16:11:47 -07001520
Dan Bornstein9a1f8162010-12-01 17:02:26 -08001521 switch (mir->dalvikInsn.opcode) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07001522 case OP_CONST_STRING_JUMBO:
1523 case OP_CONST_STRING: {
1524 void *strPtr = (void*)
1525 (cUnit->method->clazz->pDvmDex->pResStrings[mir->dalvikInsn.vB]);
Ben Chengdd6e8702010-05-07 13:05:47 -07001526
1527 if (strPtr == NULL) {
1528 LOGE("Unexpected null string");
1529 dvmAbort();
1530 }
1531
Bill Buzbeec6f10662010-02-09 11:16:15 -08001532 rlDest = dvmCompilerGetDest(cUnit, mir, 0);
1533 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Ben Chengbd1326d2010-04-02 15:04:53 -07001534 loadConstantNoClobber(cUnit, rlResult.lowReg, (int) strPtr );
Bill Buzbee1465db52009-09-23 17:17:35 -07001535 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001536 break;
1537 }
jeffhao71eee1f2011-01-04 14:18:54 -08001538 case OP_CONST_CLASS:
1539 case OP_CONST_CLASS_JUMBO: {
Ben Chengba4fc8b2009-06-01 13:00:29 -07001540 void *classPtr = (void*)
1541 (cUnit->method->clazz->pDvmDex->pResClasses[mir->dalvikInsn.vB]);
Ben Chengdd6e8702010-05-07 13:05:47 -07001542
1543 if (classPtr == NULL) {
1544 LOGE("Unexpected null class");
1545 dvmAbort();
1546 }
1547
Bill Buzbeec6f10662010-02-09 11:16:15 -08001548 rlDest = dvmCompilerGetDest(cUnit, mir, 0);
1549 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Ben Chengbd1326d2010-04-02 15:04:53 -07001550 loadConstantNoClobber(cUnit, rlResult.lowReg, (int) classPtr );
Bill Buzbee1465db52009-09-23 17:17:35 -07001551 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001552 break;
1553 }
jeffhao71eee1f2011-01-04 14:18:54 -08001554 case OP_SGET:
buzbeeecf8f6e2010-07-20 14:53:42 -07001555 case OP_SGET_VOLATILE:
jeffhao71eee1f2011-01-04 14:18:54 -08001556 case OP_SGET_JUMBO:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001557 case OP_SGET_OBJECT:
jeffhao71eee1f2011-01-04 14:18:54 -08001558 case OP_SGET_OBJECT_VOLATILE:
1559 case OP_SGET_OBJECT_JUMBO:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001560 case OP_SGET_BOOLEAN:
jeffhao71eee1f2011-01-04 14:18:54 -08001561 case OP_SGET_BOOLEAN_JUMBO:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001562 case OP_SGET_CHAR:
jeffhao71eee1f2011-01-04 14:18:54 -08001563 case OP_SGET_CHAR_JUMBO:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001564 case OP_SGET_BYTE:
jeffhao71eee1f2011-01-04 14:18:54 -08001565 case OP_SGET_BYTE_JUMBO:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001566 case OP_SGET_SHORT:
jeffhao71eee1f2011-01-04 14:18:54 -08001567 case OP_SGET_SHORT_JUMBO: {
Bill Buzbee50a6bf22009-07-08 13:08:04 -07001568 int valOffset = offsetof(StaticField, value);
Bill Buzbeec6f10662010-02-09 11:16:15 -08001569 int tReg = dvmCompilerAllocTemp(cUnit);
buzbeeecf8f6e2010-07-20 14:53:42 -07001570 bool isVolatile;
Ben Cheng7a2697d2010-06-07 13:44:23 -07001571 const Method *method = (mir->OptimizationFlags & MIR_CALLEE) ?
1572 mir->meta.calleeMethod : cUnit->method;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001573 void *fieldPtr = (void*)
Ben Cheng7a2697d2010-06-07 13:44:23 -07001574 (method->clazz->pDvmDex->pResFields[mir->dalvikInsn.vB]);
Ben Chengdd6e8702010-05-07 13:05:47 -07001575
1576 if (fieldPtr == NULL) {
1577 LOGE("Unexpected null static field");
1578 dvmAbort();
1579 }
1580
Dan Bornstein9a1f8162010-12-01 17:02:26 -08001581 isVolatile = (mir->dalvikInsn.opcode == OP_SGET_VOLATILE) ||
1582 (mir->dalvikInsn.opcode == OP_SGET_OBJECT_VOLATILE) ||
Carl Shapirofc75f3e2010-12-07 11:43:38 -08001583 dvmIsVolatileField((Field *) fieldPtr);
buzbeeecf8f6e2010-07-20 14:53:42 -07001584
Bill Buzbeec6f10662010-02-09 11:16:15 -08001585 rlDest = dvmCompilerGetDest(cUnit, mir, 0);
1586 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kAnyReg, true);
Bill Buzbee1465db52009-09-23 17:17:35 -07001587 loadConstant(cUnit, tReg, (int) fieldPtr + valOffset);
Ben Cheng11d8f142010-03-24 15:24:19 -07001588
buzbeeecf8f6e2010-07-20 14:53:42 -07001589 if (isVolatile) {
buzbee2ce33c92010-11-01 15:53:27 -07001590 dvmCompilerGenMemBarrier(cUnit, kSY);
buzbeeecf8f6e2010-07-20 14:53:42 -07001591 }
Ben Cheng11d8f142010-03-24 15:24:19 -07001592 HEAP_ACCESS_SHADOW(true);
Bill Buzbee1465db52009-09-23 17:17:35 -07001593 loadWordDisp(cUnit, tReg, 0, rlResult.lowReg);
Ben Cheng11d8f142010-03-24 15:24:19 -07001594 HEAP_ACCESS_SHADOW(false);
1595
Bill Buzbee1465db52009-09-23 17:17:35 -07001596 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001597 break;
1598 }
jeffhao71eee1f2011-01-04 14:18:54 -08001599 case OP_SGET_WIDE:
1600 case OP_SGET_WIDE_JUMBO: {
Bill Buzbee50a6bf22009-07-08 13:08:04 -07001601 int valOffset = offsetof(StaticField, value);
Ben Cheng7a2697d2010-06-07 13:44:23 -07001602 const Method *method = (mir->OptimizationFlags & MIR_CALLEE) ?
1603 mir->meta.calleeMethod : cUnit->method;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001604 void *fieldPtr = (void*)
Ben Cheng7a2697d2010-06-07 13:44:23 -07001605 (method->clazz->pDvmDex->pResFields[mir->dalvikInsn.vB]);
Ben Chengdd6e8702010-05-07 13:05:47 -07001606
1607 if (fieldPtr == NULL) {
1608 LOGE("Unexpected null static field");
1609 dvmAbort();
1610 }
1611
Bill Buzbeec6f10662010-02-09 11:16:15 -08001612 int tReg = dvmCompilerAllocTemp(cUnit);
Bill Buzbeec6f10662010-02-09 11:16:15 -08001613 rlDest = dvmCompilerGetDestWide(cUnit, mir, 0, 1);
1614 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kAnyReg, true);
Bill Buzbee1465db52009-09-23 17:17:35 -07001615 loadConstant(cUnit, tReg, (int) fieldPtr + valOffset);
Ben Cheng11d8f142010-03-24 15:24:19 -07001616
1617 HEAP_ACCESS_SHADOW(true);
Bill Buzbee1465db52009-09-23 17:17:35 -07001618 loadPair(cUnit, tReg, rlResult.lowReg, rlResult.highReg);
Ben Cheng11d8f142010-03-24 15:24:19 -07001619 HEAP_ACCESS_SHADOW(false);
1620
Bill Buzbee1465db52009-09-23 17:17:35 -07001621 storeValueWide(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001622 break;
1623 }
jeffhao71eee1f2011-01-04 14:18:54 -08001624 case OP_SPUT:
1625 case OP_SPUT_VOLATILE:
1626 case OP_SPUT_JUMBO:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001627 case OP_SPUT_OBJECT:
buzbeeddc7d292010-09-02 17:16:24 -07001628 case OP_SPUT_OBJECT_VOLATILE:
jeffhao71eee1f2011-01-04 14:18:54 -08001629 case OP_SPUT_OBJECT_JUMBO:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001630 case OP_SPUT_BOOLEAN:
jeffhao71eee1f2011-01-04 14:18:54 -08001631 case OP_SPUT_BOOLEAN_JUMBO:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001632 case OP_SPUT_CHAR:
jeffhao71eee1f2011-01-04 14:18:54 -08001633 case OP_SPUT_CHAR_JUMBO:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001634 case OP_SPUT_BYTE:
jeffhao71eee1f2011-01-04 14:18:54 -08001635 case OP_SPUT_BYTE_JUMBO:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001636 case OP_SPUT_SHORT:
jeffhao71eee1f2011-01-04 14:18:54 -08001637 case OP_SPUT_SHORT_JUMBO: {
Bill Buzbee50a6bf22009-07-08 13:08:04 -07001638 int valOffset = offsetof(StaticField, value);
Bill Buzbeec6f10662010-02-09 11:16:15 -08001639 int tReg = dvmCompilerAllocTemp(cUnit);
buzbeed3b0a4b2010-09-27 11:30:22 -07001640 int objHead;
buzbeeecf8f6e2010-07-20 14:53:42 -07001641 bool isVolatile;
buzbeed3b0a4b2010-09-27 11:30:22 -07001642 bool isSputObject;
Ben Cheng7a2697d2010-06-07 13:44:23 -07001643 const Method *method = (mir->OptimizationFlags & MIR_CALLEE) ?
1644 mir->meta.calleeMethod : cUnit->method;
1645 void *fieldPtr = (void*)
1646 (method->clazz->pDvmDex->pResFields[mir->dalvikInsn.vB]);
Ben Chenge9695e52009-06-16 16:11:47 -07001647
Dan Bornstein9a1f8162010-12-01 17:02:26 -08001648 isVolatile = (mir->dalvikInsn.opcode == OP_SPUT_VOLATILE) ||
1649 (mir->dalvikInsn.opcode == OP_SPUT_OBJECT_VOLATILE) ||
Carl Shapirofc75f3e2010-12-07 11:43:38 -08001650 dvmIsVolatileField((Field *) fieldPtr);
buzbeeecf8f6e2010-07-20 14:53:42 -07001651
Dan Bornstein9a1f8162010-12-01 17:02:26 -08001652 isSputObject = (mir->dalvikInsn.opcode == OP_SPUT_OBJECT) ||
jeffhao71eee1f2011-01-04 14:18:54 -08001653 (mir->dalvikInsn.opcode == OP_SPUT_OBJECT_JUMBO) ||
Dan Bornstein9a1f8162010-12-01 17:02:26 -08001654 (mir->dalvikInsn.opcode == OP_SPUT_OBJECT_VOLATILE);
buzbeed3b0a4b2010-09-27 11:30:22 -07001655
Ben Chengdd6e8702010-05-07 13:05:47 -07001656 if (fieldPtr == NULL) {
1657 LOGE("Unexpected null static field");
1658 dvmAbort();
1659 }
1660
Bill Buzbeec6f10662010-02-09 11:16:15 -08001661 rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07001662 rlSrc = loadValue(cUnit, rlSrc, kAnyReg);
buzbeeb78c76f2010-09-30 19:08:20 -07001663 loadConstant(cUnit, tReg, (int) fieldPtr);
buzbeed3b0a4b2010-09-27 11:30:22 -07001664 if (isSputObject) {
1665 objHead = dvmCompilerAllocTemp(cUnit);
buzbeeb78c76f2010-09-30 19:08:20 -07001666 loadWordDisp(cUnit, tReg, offsetof(Field, clazz), objHead);
buzbeed3b0a4b2010-09-27 11:30:22 -07001667 }
Ben Cheng11d8f142010-03-24 15:24:19 -07001668 HEAP_ACCESS_SHADOW(true);
buzbeeb78c76f2010-09-30 19:08:20 -07001669 storeWordDisp(cUnit, tReg, valOffset ,rlSrc.lowReg);
buzbeed3b0a4b2010-09-27 11:30:22 -07001670 dvmCompilerFreeTemp(cUnit, tReg);
Ben Cheng11d8f142010-03-24 15:24:19 -07001671 HEAP_ACCESS_SHADOW(false);
buzbeeecf8f6e2010-07-20 14:53:42 -07001672 if (isVolatile) {
buzbee2ce33c92010-11-01 15:53:27 -07001673 dvmCompilerGenMemBarrier(cUnit, kSY);
buzbeeecf8f6e2010-07-20 14:53:42 -07001674 }
buzbeed3b0a4b2010-09-27 11:30:22 -07001675 if (isSputObject) {
buzbeeb78c76f2010-09-30 19:08:20 -07001676 /* NOTE: marking card based sfield->clazz */
buzbeed3b0a4b2010-09-27 11:30:22 -07001677 markCard(cUnit, rlSrc.lowReg, objHead);
1678 dvmCompilerFreeTemp(cUnit, objHead);
buzbee919eb062010-07-12 12:59:22 -07001679 }
Ben Cheng11d8f142010-03-24 15:24:19 -07001680
Ben Chengba4fc8b2009-06-01 13:00:29 -07001681 break;
1682 }
jeffhao71eee1f2011-01-04 14:18:54 -08001683 case OP_SPUT_WIDE:
1684 case OP_SPUT_WIDE_JUMBO: {
Bill Buzbeec6f10662010-02-09 11:16:15 -08001685 int tReg = dvmCompilerAllocTemp(cUnit);
Bill Buzbee50a6bf22009-07-08 13:08:04 -07001686 int valOffset = offsetof(StaticField, value);
Ben Cheng7a2697d2010-06-07 13:44:23 -07001687 const Method *method = (mir->OptimizationFlags & MIR_CALLEE) ?
1688 mir->meta.calleeMethod : cUnit->method;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001689 void *fieldPtr = (void*)
Ben Cheng7a2697d2010-06-07 13:44:23 -07001690 (method->clazz->pDvmDex->pResFields[mir->dalvikInsn.vB]);
Ben Chenge9695e52009-06-16 16:11:47 -07001691
Ben Chengdd6e8702010-05-07 13:05:47 -07001692 if (fieldPtr == NULL) {
1693 LOGE("Unexpected null static field");
1694 dvmAbort();
1695 }
1696
Bill Buzbeec6f10662010-02-09 11:16:15 -08001697 rlSrc = dvmCompilerGetSrcWide(cUnit, mir, 0, 1);
Bill Buzbee1465db52009-09-23 17:17:35 -07001698 rlSrc = loadValueWide(cUnit, rlSrc, kAnyReg);
1699 loadConstant(cUnit, tReg, (int) fieldPtr + valOffset);
Ben Cheng11d8f142010-03-24 15:24:19 -07001700
1701 HEAP_ACCESS_SHADOW(true);
Bill Buzbee1465db52009-09-23 17:17:35 -07001702 storePair(cUnit, tReg, rlSrc.lowReg, rlSrc.highReg);
Ben Cheng11d8f142010-03-24 15:24:19 -07001703 HEAP_ACCESS_SHADOW(false);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001704 break;
1705 }
jeffhao71eee1f2011-01-04 14:18:54 -08001706 case OP_NEW_INSTANCE:
1707 case OP_NEW_INSTANCE_JUMBO: {
Ben Chenge9695e52009-06-16 16:11:47 -07001708 /*
1709 * Obey the calling convention and don't mess with the register
1710 * usage.
1711 */
Carl Shapirofc75f3e2010-12-07 11:43:38 -08001712 ClassObject *classPtr = (ClassObject *)
Ben Chengba4fc8b2009-06-01 13:00:29 -07001713 (cUnit->method->clazz->pDvmDex->pResClasses[mir->dalvikInsn.vB]);
Ben Chengdd6e8702010-05-07 13:05:47 -07001714
1715 if (classPtr == NULL) {
1716 LOGE("Unexpected null class");
1717 dvmAbort();
1718 }
1719
Ben Cheng79d173c2009-09-29 16:12:51 -07001720 /*
1721 * If it is going to throw, it should not make to the trace to begin
Bill Buzbee1465db52009-09-23 17:17:35 -07001722 * with. However, Alloc might throw, so we need to genExportPC()
Ben Cheng79d173c2009-09-29 16:12:51 -07001723 */
1724 assert((classPtr->accessFlags & (ACC_INTERFACE|ACC_ABSTRACT)) == 0);
Bill Buzbeec6f10662010-02-09 11:16:15 -08001725 dvmCompilerFlushAllRegs(cUnit); /* Everything to home location */
Bill Buzbee1465db52009-09-23 17:17:35 -07001726 genExportPC(cUnit, mir);
Ben Chengbd1326d2010-04-02 15:04:53 -07001727 LOAD_FUNC_ADDR(cUnit, r2, (int)dvmAllocObject);
Ben Chenge9695e52009-06-16 16:11:47 -07001728 loadConstant(cUnit, r0, (int) classPtr);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001729 loadConstant(cUnit, r1, ALLOC_DONT_TRACK);
Bill Buzbee1465db52009-09-23 17:17:35 -07001730 opReg(cUnit, kOpBlx, r2);
Elliott Hughes6a555132010-02-25 15:41:42 -08001731 dvmCompilerClobberCallRegs(cUnit);
Ben Cheng4f489172009-09-27 17:08:35 -07001732 /* generate a branch over if allocation is successful */
buzbee8f8109a2010-08-31 10:16:35 -07001733 ArmLIR *branchOver = genCmpImmBranch(cUnit, kArmCondNe, r0, 0);
Ben Cheng4f489172009-09-27 17:08:35 -07001734 /*
1735 * OOM exception needs to be thrown here and cannot re-execute
1736 */
1737 loadConstant(cUnit, r0,
1738 (int) (cUnit->method->insns + mir->offset));
1739 genDispatchToHandler(cUnit, TEMPLATE_THROW_EXCEPTION_COMMON);
1740 /* noreturn */
1741
Bill Buzbee1465db52009-09-23 17:17:35 -07001742 ArmLIR *target = newLIR0(cUnit, kArmPseudoTargetLabel);
Ben Cheng4f489172009-09-27 17:08:35 -07001743 target->defMask = ENCODE_ALL;
1744 branchOver->generic.target = (LIR *) target;
Bill Buzbeec6f10662010-02-09 11:16:15 -08001745 rlDest = dvmCompilerGetDest(cUnit, mir, 0);
1746 rlResult = dvmCompilerGetReturn(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07001747 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001748 break;
1749 }
jeffhao71eee1f2011-01-04 14:18:54 -08001750 case OP_CHECK_CAST:
1751 case OP_CHECK_CAST_JUMBO: {
Ben Chenge9695e52009-06-16 16:11:47 -07001752 /*
1753 * Obey the calling convention and don't mess with the register
1754 * usage.
1755 */
Ben Chengba4fc8b2009-06-01 13:00:29 -07001756 ClassObject *classPtr =
1757 (cUnit->method->clazz->pDvmDex->pResClasses[mir->dalvikInsn.vB]);
Bill Buzbee4df41a52009-11-12 17:07:16 -08001758 /*
1759 * Note: It is possible that classPtr is NULL at this point,
1760 * even though this instruction has been successfully interpreted.
1761 * If the previous interpretation had a null source, the
1762 * interpreter would not have bothered to resolve the clazz.
1763 * Bail out to the interpreter in this case, and log it
1764 * so that we can tell if it happens frequently.
1765 */
1766 if (classPtr == NULL) {
Ben Cheng11d8f142010-03-24 15:24:19 -07001767 LOGVV("null clazz in OP_CHECK_CAST, single-stepping");
Bill Buzbee4df41a52009-11-12 17:07:16 -08001768 genInterpSingleStep(cUnit, mir);
1769 return false;
1770 }
Bill Buzbeec6f10662010-02-09 11:16:15 -08001771 dvmCompilerFlushAllRegs(cUnit); /* Everything to home location */
Ben Chengba4fc8b2009-06-01 13:00:29 -07001772 loadConstant(cUnit, r1, (int) classPtr );
Bill Buzbeec6f10662010-02-09 11:16:15 -08001773 rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07001774 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
buzbee8f8109a2010-08-31 10:16:35 -07001775 /* Null? */
1776 ArmLIR *branch1 = genCmpImmBranch(cUnit, kArmCondEq,
1777 rlSrc.lowReg, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07001778 /*
1779 * rlSrc.lowReg now contains object->clazz. Note that
1780 * it could have been allocated r0, but we're okay so long
1781 * as we don't do anything desctructive until r0 is loaded
1782 * with clazz.
1783 */
Ben Chengba4fc8b2009-06-01 13:00:29 -07001784 /* r0 now contains object->clazz */
Bill Buzbee1465db52009-09-23 17:17:35 -07001785 loadWordDisp(cUnit, rlSrc.lowReg, offsetof(Object, clazz), r0);
Ben Chengbd1326d2010-04-02 15:04:53 -07001786 LOAD_FUNC_ADDR(cUnit, r2, (int)dvmInstanceofNonTrivial);
Bill Buzbee1465db52009-09-23 17:17:35 -07001787 opRegReg(cUnit, kOpCmp, r0, r1);
1788 ArmLIR *branch2 = opCondBranch(cUnit, kArmCondEq);
1789 opReg(cUnit, kOpBlx, r2);
Elliott Hughes6a555132010-02-25 15:41:42 -08001790 dvmCompilerClobberCallRegs(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07001791 /*
1792 * If null, check cast failed - punt to the interpreter. Because
1793 * interpreter will be the one throwing, we don't need to
1794 * genExportPC() here.
1795 */
Bill Buzbee270c1d62009-08-13 16:58:07 -07001796 genZeroCheck(cUnit, r0, mir->offset, NULL);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001797 /* check cast passed - branch target here */
Bill Buzbee1465db52009-09-23 17:17:35 -07001798 ArmLIR *target = newLIR0(cUnit, kArmPseudoTargetLabel);
Ben Chengd7d426a2009-09-22 11:23:36 -07001799 target->defMask = ENCODE_ALL;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001800 branch1->generic.target = (LIR *)target;
1801 branch2->generic.target = (LIR *)target;
1802 break;
1803 }
buzbee4d92e682010-07-29 15:24:14 -07001804 case OP_SGET_WIDE_VOLATILE:
1805 case OP_SPUT_WIDE_VOLATILE:
1806 genInterpSingleStep(cUnit, mir);
1807 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001808 default:
1809 return true;
1810 }
1811 return false;
1812}
1813
Ben Cheng7a2697d2010-06-07 13:44:23 -07001814/*
1815 * A typical example of inlined getter/setter from a monomorphic callsite:
1816 *
1817 * D/dalvikvm( 289): -------- dalvik offset: 0x0000 @ invoke-static (I)
1818 * D/dalvikvm( 289): -------- dalvik offset: 0x0000 @ sget-object (C) v0, ...
1819 * D/dalvikvm( 289): 0x4427fc22 (0002): ldr r0, [pc, #56]
1820 * D/dalvikvm( 289): 0x4427fc24 (0004): ldr r1, [r0, #0]
1821 * D/dalvikvm( 289): 0x4427fc26 (0006): str r1, [r5, #0]
1822 * D/dalvikvm( 289): 0x4427fc28 (0008): .align4
1823 * D/dalvikvm( 289): L0x0003:
1824 * D/dalvikvm( 289): -------- dalvik offset: 0x0003 @ move-result-object (I) v0
1825 *
1826 * Note the invoke-static and move-result-object with the (I) notation are
1827 * turned into no-op.
1828 */
Ben Chengba4fc8b2009-06-01 13:00:29 -07001829static bool handleFmt11x(CompilationUnit *cUnit, MIR *mir)
1830{
Dan Bornstein9a1f8162010-12-01 17:02:26 -08001831 Opcode dalvikOpcode = mir->dalvikInsn.opcode;
Bill Buzbee1465db52009-09-23 17:17:35 -07001832 RegLocation rlResult;
Dan Bornstein9a1f8162010-12-01 17:02:26 -08001833 switch (dalvikOpcode) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07001834 case OP_MOVE_EXCEPTION: {
Ben Chengba4fc8b2009-06-01 13:00:29 -07001835 int exOffset = offsetof(Thread, exception);
Bill Buzbeec6f10662010-02-09 11:16:15 -08001836 int resetReg = dvmCompilerAllocTemp(cUnit);
1837 RegLocation rlDest = dvmCompilerGetDest(cUnit, mir, 0);
1838 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
buzbee9f601a92011-02-11 17:48:20 -08001839 loadWordDisp(cUnit, rSELF, exOffset, rlResult.lowReg);
Bill Buzbeef9f33282009-11-22 12:45:30 -08001840 loadConstant(cUnit, resetReg, 0);
buzbee9f601a92011-02-11 17:48:20 -08001841 storeWordDisp(cUnit, rSELF, exOffset, resetReg);
Bill Buzbee1465db52009-09-23 17:17:35 -07001842 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001843 break;
1844 }
1845 case OP_MOVE_RESULT:
1846 case OP_MOVE_RESULT_OBJECT: {
Ben Cheng7a2697d2010-06-07 13:44:23 -07001847 /* An inlined move result is effectively no-op */
1848 if (mir->OptimizationFlags & MIR_INLINED)
1849 break;
Bill Buzbeec6f10662010-02-09 11:16:15 -08001850 RegLocation rlDest = dvmCompilerGetDest(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07001851 RegLocation rlSrc = LOC_DALVIK_RETURN_VAL;
1852 rlSrc.fp = rlDest.fp;
1853 storeValue(cUnit, rlDest, rlSrc);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001854 break;
1855 }
1856 case OP_MOVE_RESULT_WIDE: {
Ben Cheng7a2697d2010-06-07 13:44:23 -07001857 /* An inlined move result is effectively no-op */
1858 if (mir->OptimizationFlags & MIR_INLINED)
1859 break;
Bill Buzbeec6f10662010-02-09 11:16:15 -08001860 RegLocation rlDest = dvmCompilerGetDestWide(cUnit, mir, 0, 1);
Bill Buzbee1465db52009-09-23 17:17:35 -07001861 RegLocation rlSrc = LOC_DALVIK_RETURN_VAL_WIDE;
1862 rlSrc.fp = rlDest.fp;
1863 storeValueWide(cUnit, rlDest, rlSrc);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001864 break;
1865 }
1866 case OP_RETURN_WIDE: {
Bill Buzbeec6f10662010-02-09 11:16:15 -08001867 RegLocation rlSrc = dvmCompilerGetSrcWide(cUnit, mir, 0, 1);
Bill Buzbee1465db52009-09-23 17:17:35 -07001868 RegLocation rlDest = LOC_DALVIK_RETURN_VAL_WIDE;
1869 rlDest.fp = rlSrc.fp;
1870 storeValueWide(cUnit, rlDest, rlSrc);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001871 genReturnCommon(cUnit,mir);
1872 break;
1873 }
1874 case OP_RETURN:
1875 case OP_RETURN_OBJECT: {
Bill Buzbeec6f10662010-02-09 11:16:15 -08001876 RegLocation rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07001877 RegLocation rlDest = LOC_DALVIK_RETURN_VAL;
1878 rlDest.fp = rlSrc.fp;
1879 storeValue(cUnit, rlDest, rlSrc);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001880 genReturnCommon(cUnit,mir);
1881 break;
1882 }
Bill Buzbee1465db52009-09-23 17:17:35 -07001883 case OP_MONITOR_EXIT:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001884 case OP_MONITOR_ENTER:
Ben Cheng5d90c202009-11-22 23:31:11 -08001885 genMonitor(cUnit, mir);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001886 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001887 case OP_THROW: {
1888 genInterpSingleStep(cUnit, mir);
1889 break;
1890 }
1891 default:
1892 return true;
1893 }
1894 return false;
1895}
1896
Bill Buzbeed45ba372009-06-15 17:00:57 -07001897static bool handleFmt12x(CompilationUnit *cUnit, MIR *mir)
1898{
Dan Bornstein9a1f8162010-12-01 17:02:26 -08001899 Opcode opcode = mir->dalvikInsn.opcode;
Bill Buzbee1465db52009-09-23 17:17:35 -07001900 RegLocation rlDest;
1901 RegLocation rlSrc;
1902 RegLocation rlResult;
Bill Buzbeed45ba372009-06-15 17:00:57 -07001903
Dan Bornstein9a1f8162010-12-01 17:02:26 -08001904 if ( (opcode >= OP_ADD_INT_2ADDR) && (opcode <= OP_REM_DOUBLE_2ADDR)) {
Ben Cheng5d90c202009-11-22 23:31:11 -08001905 return genArithOp( cUnit, mir );
Ben Chengba4fc8b2009-06-01 13:00:29 -07001906 }
1907
Bill Buzbee1465db52009-09-23 17:17:35 -07001908 if (mir->ssaRep->numUses == 2)
Bill Buzbeec6f10662010-02-09 11:16:15 -08001909 rlSrc = dvmCompilerGetSrcWide(cUnit, mir, 0, 1);
Bill Buzbee1465db52009-09-23 17:17:35 -07001910 else
Bill Buzbeec6f10662010-02-09 11:16:15 -08001911 rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07001912 if (mir->ssaRep->numDefs == 2)
Bill Buzbeec6f10662010-02-09 11:16:15 -08001913 rlDest = dvmCompilerGetDestWide(cUnit, mir, 0, 1);
Bill Buzbee1465db52009-09-23 17:17:35 -07001914 else
Bill Buzbeec6f10662010-02-09 11:16:15 -08001915 rlDest = dvmCompilerGetDest(cUnit, mir, 0);
Ben Chenge9695e52009-06-16 16:11:47 -07001916
Dan Bornstein9a1f8162010-12-01 17:02:26 -08001917 switch (opcode) {
Bill Buzbee1465db52009-09-23 17:17:35 -07001918 case OP_DOUBLE_TO_INT:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001919 case OP_INT_TO_FLOAT:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001920 case OP_FLOAT_TO_INT:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001921 case OP_DOUBLE_TO_FLOAT:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001922 case OP_FLOAT_TO_DOUBLE:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001923 case OP_INT_TO_DOUBLE:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001924 case OP_FLOAT_TO_LONG:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001925 case OP_LONG_TO_FLOAT:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001926 case OP_DOUBLE_TO_LONG:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001927 case OP_LONG_TO_DOUBLE:
Ben Cheng5d90c202009-11-22 23:31:11 -08001928 return genConversion(cUnit, mir);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001929 case OP_NEG_INT:
1930 case OP_NOT_INT:
Ben Cheng5d90c202009-11-22 23:31:11 -08001931 return genArithOpInt(cUnit, mir, rlDest, rlSrc, rlSrc);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001932 case OP_NEG_LONG:
1933 case OP_NOT_LONG:
Ben Cheng5d90c202009-11-22 23:31:11 -08001934 return genArithOpLong(cUnit, mir, rlDest, rlSrc, rlSrc);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001935 case OP_NEG_FLOAT:
Ben Cheng5d90c202009-11-22 23:31:11 -08001936 return genArithOpFloat(cUnit, mir, rlDest, rlSrc, rlSrc);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001937 case OP_NEG_DOUBLE:
Ben Cheng5d90c202009-11-22 23:31:11 -08001938 return genArithOpDouble(cUnit, mir, rlDest, rlSrc, rlSrc);
Bill Buzbee1465db52009-09-23 17:17:35 -07001939 case OP_MOVE_WIDE:
1940 storeValueWide(cUnit, rlDest, rlSrc);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001941 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07001942 case OP_INT_TO_LONG:
Bill Buzbeec6f10662010-02-09 11:16:15 -08001943 rlSrc = dvmCompilerUpdateLoc(cUnit, rlSrc);
1944 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Bill Buzbee964a7b02010-01-28 12:54:19 -08001945 //TUNING: shouldn't loadValueDirect already check for phys reg?
Bill Buzbee1465db52009-09-23 17:17:35 -07001946 if (rlSrc.location == kLocPhysReg) {
1947 genRegCopy(cUnit, rlResult.lowReg, rlSrc.lowReg);
1948 } else {
1949 loadValueDirect(cUnit, rlSrc, rlResult.lowReg);
1950 }
1951 opRegRegImm(cUnit, kOpAsr, rlResult.highReg,
1952 rlResult.lowReg, 31);
1953 storeValueWide(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001954 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07001955 case OP_LONG_TO_INT:
Bill Buzbeec6f10662010-02-09 11:16:15 -08001956 rlSrc = dvmCompilerUpdateLocWide(cUnit, rlSrc);
1957 rlSrc = dvmCompilerWideToNarrow(cUnit, rlSrc);
Bill Buzbee1465db52009-09-23 17:17:35 -07001958 // Intentional fallthrough
Ben Chengba4fc8b2009-06-01 13:00:29 -07001959 case OP_MOVE:
1960 case OP_MOVE_OBJECT:
Bill Buzbee1465db52009-09-23 17:17:35 -07001961 storeValue(cUnit, rlDest, rlSrc);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001962 break;
1963 case OP_INT_TO_BYTE:
Bill Buzbee1465db52009-09-23 17:17:35 -07001964 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
Bill Buzbeec6f10662010-02-09 11:16:15 -08001965 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Bill Buzbee1465db52009-09-23 17:17:35 -07001966 opRegReg(cUnit, kOp2Byte, rlResult.lowReg, rlSrc.lowReg);
1967 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001968 break;
1969 case OP_INT_TO_SHORT:
Bill Buzbee1465db52009-09-23 17:17:35 -07001970 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
Bill Buzbeec6f10662010-02-09 11:16:15 -08001971 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Bill Buzbee1465db52009-09-23 17:17:35 -07001972 opRegReg(cUnit, kOp2Short, rlResult.lowReg, rlSrc.lowReg);
1973 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001974 break;
1975 case OP_INT_TO_CHAR:
Bill Buzbee1465db52009-09-23 17:17:35 -07001976 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
Bill Buzbeec6f10662010-02-09 11:16:15 -08001977 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Bill Buzbee1465db52009-09-23 17:17:35 -07001978 opRegReg(cUnit, kOp2Char, rlResult.lowReg, rlSrc.lowReg);
1979 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001980 break;
1981 case OP_ARRAY_LENGTH: {
1982 int lenOffset = offsetof(ArrayObject, length);
Bill Buzbee1465db52009-09-23 17:17:35 -07001983 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
1984 genNullCheck(cUnit, rlSrc.sRegLow, rlSrc.lowReg,
1985 mir->offset, NULL);
Bill Buzbeec6f10662010-02-09 11:16:15 -08001986 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Bill Buzbee1465db52009-09-23 17:17:35 -07001987 loadWordDisp(cUnit, rlSrc.lowReg, lenOffset,
1988 rlResult.lowReg);
1989 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001990 break;
1991 }
1992 default:
1993 return true;
1994 }
1995 return false;
1996}
1997
1998static bool handleFmt21s(CompilationUnit *cUnit, MIR *mir)
1999{
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002000 Opcode dalvikOpcode = mir->dalvikInsn.opcode;
Bill Buzbee1465db52009-09-23 17:17:35 -07002001 RegLocation rlDest;
2002 RegLocation rlResult;
2003 int BBBB = mir->dalvikInsn.vB;
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002004 if (dalvikOpcode == OP_CONST_WIDE_16) {
Bill Buzbeec6f10662010-02-09 11:16:15 -08002005 rlDest = dvmCompilerGetDestWide(cUnit, mir, 0, 1);
2006 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Ben Chengbd1326d2010-04-02 15:04:53 -07002007 loadConstantNoClobber(cUnit, rlResult.lowReg, BBBB);
Bill Buzbee964a7b02010-01-28 12:54:19 -08002008 //TUNING: do high separately to avoid load dependency
Bill Buzbee1465db52009-09-23 17:17:35 -07002009 opRegRegImm(cUnit, kOpAsr, rlResult.highReg, rlResult.lowReg, 31);
2010 storeValueWide(cUnit, rlDest, rlResult);
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002011 } else if (dalvikOpcode == OP_CONST_16) {
Bill Buzbeec6f10662010-02-09 11:16:15 -08002012 rlDest = dvmCompilerGetDest(cUnit, mir, 0);
2013 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kAnyReg, true);
Ben Chengbd1326d2010-04-02 15:04:53 -07002014 loadConstantNoClobber(cUnit, rlResult.lowReg, BBBB);
Bill Buzbee1465db52009-09-23 17:17:35 -07002015 storeValue(cUnit, rlDest, rlResult);
2016 } else
Ben Chengba4fc8b2009-06-01 13:00:29 -07002017 return true;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002018 return false;
2019}
2020
2021/* Compare agaist zero */
2022static bool handleFmt21t(CompilationUnit *cUnit, MIR *mir, BasicBlock *bb,
Bill Buzbee89efc3d2009-07-28 11:22:22 -07002023 ArmLIR *labelList)
Ben Chengba4fc8b2009-06-01 13:00:29 -07002024{
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002025 Opcode dalvikOpcode = mir->dalvikInsn.opcode;
Bill Buzbee89efc3d2009-07-28 11:22:22 -07002026 ArmConditionCode cond;
Ben Cheng7ab74e12011-02-03 14:02:06 -08002027 /* backward branch? */
2028 bool backwardBranch = (bb->taken->startOffset <= mir->offset);
2029
2030 if (backwardBranch && gDvmJit.genSuspendPoll) {
2031 genSuspendPoll(cUnit, mir);
2032 }
2033
Bill Buzbeec6f10662010-02-09 11:16:15 -08002034 RegLocation rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07002035 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
Ben Cheng7ab74e12011-02-03 14:02:06 -08002036
Bill Buzbee1465db52009-09-23 17:17:35 -07002037 opRegImm(cUnit, kOpCmp, rlSrc.lowReg, 0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002038
Bill Buzbee270c1d62009-08-13 16:58:07 -07002039//TUNING: break this out to allow use of Thumb2 CB[N]Z
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002040 switch (dalvikOpcode) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07002041 case OP_IF_EQZ:
Bill Buzbee1465db52009-09-23 17:17:35 -07002042 cond = kArmCondEq;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002043 break;
2044 case OP_IF_NEZ:
Bill Buzbee1465db52009-09-23 17:17:35 -07002045 cond = kArmCondNe;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002046 break;
2047 case OP_IF_LTZ:
Bill Buzbee1465db52009-09-23 17:17:35 -07002048 cond = kArmCondLt;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002049 break;
2050 case OP_IF_GEZ:
Bill Buzbee1465db52009-09-23 17:17:35 -07002051 cond = kArmCondGe;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002052 break;
2053 case OP_IF_GTZ:
Bill Buzbee1465db52009-09-23 17:17:35 -07002054 cond = kArmCondGt;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002055 break;
2056 case OP_IF_LEZ:
Bill Buzbee1465db52009-09-23 17:17:35 -07002057 cond = kArmCondLe;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002058 break;
2059 default:
2060 cond = 0;
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002061 LOGE("Unexpected opcode (%d) for Fmt21t\n", dalvikOpcode);
Bill Buzbeefc519dc2010-03-06 23:30:57 -08002062 dvmCompilerAbort(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002063 }
2064 genConditionalBranch(cUnit, cond, &labelList[bb->taken->id]);
2065 /* This mostly likely will be optimized away in a later phase */
2066 genUnconditionalBranch(cUnit, &labelList[bb->fallThrough->id]);
2067 return false;
2068}
2069
Elliott Hughesb4c05972010-02-24 16:36:18 -08002070static bool isPowerOfTwo(int x)
2071{
2072 return (x & (x - 1)) == 0;
2073}
2074
2075// Returns true if no more than two bits are set in 'x'.
2076static bool isPopCountLE2(unsigned int x)
2077{
2078 x &= x - 1;
2079 return (x & (x - 1)) == 0;
2080}
2081
2082// Returns the index of the lowest set bit in 'x'.
2083static int lowestSetBit(unsigned int x) {
2084 int bit_posn = 0;
2085 while ((x & 0xf) == 0) {
2086 bit_posn += 4;
2087 x >>= 4;
Bill Buzbee78cb0e22010-02-11 14:04:53 -08002088 }
Elliott Hughesb4c05972010-02-24 16:36:18 -08002089 while ((x & 1) == 0) {
2090 bit_posn++;
2091 x >>= 1;
2092 }
2093 return bit_posn;
2094}
2095
Elliott Hughes672511b2010-04-26 17:40:13 -07002096// Returns true if it added instructions to 'cUnit' to divide 'rlSrc' by 'lit'
2097// and store the result in 'rlDest'.
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002098static bool handleEasyDivide(CompilationUnit *cUnit, Opcode dalvikOpcode,
Elliott Hughes672511b2010-04-26 17:40:13 -07002099 RegLocation rlSrc, RegLocation rlDest, int lit)
2100{
2101 if (lit < 2 || !isPowerOfTwo(lit)) {
2102 return false;
2103 }
2104 int k = lowestSetBit(lit);
2105 if (k >= 30) {
2106 // Avoid special cases.
2107 return false;
2108 }
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002109 bool div = (dalvikOpcode == OP_DIV_INT_LIT8 || dalvikOpcode == OP_DIV_INT_LIT16);
Elliott Hughes672511b2010-04-26 17:40:13 -07002110 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
2111 RegLocation rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Elliott Hughes9c457022010-04-28 16:15:38 -07002112 if (div) {
2113 int tReg = dvmCompilerAllocTemp(cUnit);
2114 if (lit == 2) {
2115 // Division by 2 is by far the most common division by constant.
2116 opRegRegImm(cUnit, kOpLsr, tReg, rlSrc.lowReg, 32 - k);
2117 opRegRegReg(cUnit, kOpAdd, tReg, tReg, rlSrc.lowReg);
2118 opRegRegImm(cUnit, kOpAsr, rlResult.lowReg, tReg, k);
2119 } else {
2120 opRegRegImm(cUnit, kOpAsr, tReg, rlSrc.lowReg, 31);
2121 opRegRegImm(cUnit, kOpLsr, tReg, tReg, 32 - k);
2122 opRegRegReg(cUnit, kOpAdd, tReg, tReg, rlSrc.lowReg);
2123 opRegRegImm(cUnit, kOpAsr, rlResult.lowReg, tReg, k);
2124 }
Elliott Hughes672511b2010-04-26 17:40:13 -07002125 } else {
Elliott Hughes9c457022010-04-28 16:15:38 -07002126 int cReg = dvmCompilerAllocTemp(cUnit);
2127 loadConstant(cUnit, cReg, lit - 1);
2128 int tReg1 = dvmCompilerAllocTemp(cUnit);
2129 int tReg2 = dvmCompilerAllocTemp(cUnit);
2130 if (lit == 2) {
2131 opRegRegImm(cUnit, kOpLsr, tReg1, rlSrc.lowReg, 32 - k);
2132 opRegRegReg(cUnit, kOpAdd, tReg2, tReg1, rlSrc.lowReg);
2133 opRegRegReg(cUnit, kOpAnd, tReg2, tReg2, cReg);
2134 opRegRegReg(cUnit, kOpSub, rlResult.lowReg, tReg2, tReg1);
2135 } else {
2136 opRegRegImm(cUnit, kOpAsr, tReg1, rlSrc.lowReg, 31);
2137 opRegRegImm(cUnit, kOpLsr, tReg1, tReg1, 32 - k);
2138 opRegRegReg(cUnit, kOpAdd, tReg2, tReg1, rlSrc.lowReg);
2139 opRegRegReg(cUnit, kOpAnd, tReg2, tReg2, cReg);
2140 opRegRegReg(cUnit, kOpSub, rlResult.lowReg, tReg2, tReg1);
2141 }
Elliott Hughes672511b2010-04-26 17:40:13 -07002142 }
2143 storeValue(cUnit, rlDest, rlResult);
2144 return true;
2145}
2146
Elliott Hughesb4c05972010-02-24 16:36:18 -08002147// Returns true if it added instructions to 'cUnit' to multiply 'rlSrc' by 'lit'
2148// and store the result in 'rlDest'.
2149static bool handleEasyMultiply(CompilationUnit *cUnit,
2150 RegLocation rlSrc, RegLocation rlDest, int lit)
2151{
2152 // Can we simplify this multiplication?
2153 bool powerOfTwo = false;
2154 bool popCountLE2 = false;
2155 bool powerOfTwoMinusOne = false;
2156 if (lit < 2) {
2157 // Avoid special cases.
2158 return false;
2159 } else if (isPowerOfTwo(lit)) {
2160 powerOfTwo = true;
2161 } else if (isPopCountLE2(lit)) {
2162 popCountLE2 = true;
2163 } else if (isPowerOfTwo(lit + 1)) {
2164 powerOfTwoMinusOne = true;
2165 } else {
2166 return false;
2167 }
2168 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
2169 RegLocation rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
2170 if (powerOfTwo) {
2171 // Shift.
2172 opRegRegImm(cUnit, kOpLsl, rlResult.lowReg, rlSrc.lowReg,
2173 lowestSetBit(lit));
2174 } else if (popCountLE2) {
2175 // Shift and add and shift.
2176 int firstBit = lowestSetBit(lit);
2177 int secondBit = lowestSetBit(lit ^ (1 << firstBit));
2178 genMultiplyByTwoBitMultiplier(cUnit, rlSrc, rlResult, lit,
2179 firstBit, secondBit);
2180 } else {
2181 // Reverse subtract: (src << (shift + 1)) - src.
2182 assert(powerOfTwoMinusOne);
2183 // TODO: rsb dst, src, src lsl#lowestSetBit(lit + 1)
2184 int tReg = dvmCompilerAllocTemp(cUnit);
2185 opRegRegImm(cUnit, kOpLsl, tReg, rlSrc.lowReg, lowestSetBit(lit + 1));
2186 opRegRegReg(cUnit, kOpSub, rlResult.lowReg, tReg, rlSrc.lowReg);
2187 }
2188 storeValue(cUnit, rlDest, rlResult);
2189 return true;
Bill Buzbee78cb0e22010-02-11 14:04:53 -08002190}
2191
Ben Chengba4fc8b2009-06-01 13:00:29 -07002192static bool handleFmt22b_Fmt22s(CompilationUnit *cUnit, MIR *mir)
2193{
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002194 Opcode dalvikOpcode = mir->dalvikInsn.opcode;
Bill Buzbeec6f10662010-02-09 11:16:15 -08002195 RegLocation rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
2196 RegLocation rlDest = dvmCompilerGetDest(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07002197 RegLocation rlResult;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002198 int lit = mir->dalvikInsn.vC;
Ben Cheng4f489172009-09-27 17:08:35 -07002199 OpKind op = 0; /* Make gcc happy */
Bill Buzbee1465db52009-09-23 17:17:35 -07002200 int shiftOp = false;
2201 bool isDiv = false;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002202
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002203 switch (dalvikOpcode) {
Bill Buzbee1465db52009-09-23 17:17:35 -07002204 case OP_RSUB_INT_LIT8:
2205 case OP_RSUB_INT: {
2206 int tReg;
2207 //TUNING: add support for use of Arm rsub op
2208 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
Bill Buzbeec6f10662010-02-09 11:16:15 -08002209 tReg = dvmCompilerAllocTemp(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07002210 loadConstant(cUnit, tReg, lit);
Bill Buzbeec6f10662010-02-09 11:16:15 -08002211 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Bill Buzbee1465db52009-09-23 17:17:35 -07002212 opRegRegReg(cUnit, kOpSub, rlResult.lowReg,
2213 tReg, rlSrc.lowReg);
2214 storeValue(cUnit, rlDest, rlResult);
2215 return false;
2216 break;
2217 }
2218
Ben Chengba4fc8b2009-06-01 13:00:29 -07002219 case OP_ADD_INT_LIT8:
2220 case OP_ADD_INT_LIT16:
Bill Buzbee1465db52009-09-23 17:17:35 -07002221 op = kOpAdd;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002222 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002223 case OP_MUL_INT_LIT8:
Bill Buzbee78cb0e22010-02-11 14:04:53 -08002224 case OP_MUL_INT_LIT16: {
Elliott Hughesb4c05972010-02-24 16:36:18 -08002225 if (handleEasyMultiply(cUnit, rlSrc, rlDest, lit)) {
2226 return false;
Bill Buzbee78cb0e22010-02-11 14:04:53 -08002227 }
Elliott Hughesb4c05972010-02-24 16:36:18 -08002228 op = kOpMul;
Bill Buzbee1465db52009-09-23 17:17:35 -07002229 break;
Bill Buzbee78cb0e22010-02-11 14:04:53 -08002230 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07002231 case OP_AND_INT_LIT8:
2232 case OP_AND_INT_LIT16:
Bill Buzbee1465db52009-09-23 17:17:35 -07002233 op = kOpAnd;
2234 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002235 case OP_OR_INT_LIT8:
2236 case OP_OR_INT_LIT16:
Bill Buzbee1465db52009-09-23 17:17:35 -07002237 op = kOpOr;
2238 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002239 case OP_XOR_INT_LIT8:
2240 case OP_XOR_INT_LIT16:
Bill Buzbee1465db52009-09-23 17:17:35 -07002241 op = kOpXor;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002242 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002243 case OP_SHL_INT_LIT8:
Bill Buzbee0e605272009-12-01 14:28:05 -08002244 lit &= 31;
Bill Buzbee1465db52009-09-23 17:17:35 -07002245 shiftOp = true;
2246 op = kOpLsl;
2247 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002248 case OP_SHR_INT_LIT8:
Bill Buzbee0e605272009-12-01 14:28:05 -08002249 lit &= 31;
Bill Buzbee1465db52009-09-23 17:17:35 -07002250 shiftOp = true;
2251 op = kOpAsr;
2252 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002253 case OP_USHR_INT_LIT8:
Bill Buzbee0e605272009-12-01 14:28:05 -08002254 lit &= 31;
Bill Buzbee1465db52009-09-23 17:17:35 -07002255 shiftOp = true;
2256 op = kOpLsr;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002257 break;
2258
2259 case OP_DIV_INT_LIT8:
2260 case OP_DIV_INT_LIT16:
Ben Chengba4fc8b2009-06-01 13:00:29 -07002261 case OP_REM_INT_LIT8:
2262 case OP_REM_INT_LIT16:
2263 if (lit == 0) {
2264 /* Let the interpreter deal with div by 0 */
2265 genInterpSingleStep(cUnit, mir);
2266 return false;
2267 }
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002268 if (handleEasyDivide(cUnit, dalvikOpcode, rlSrc, rlDest, lit)) {
Elliott Hughes672511b2010-04-26 17:40:13 -07002269 return false;
2270 }
Bill Buzbeec6f10662010-02-09 11:16:15 -08002271 dvmCompilerFlushAllRegs(cUnit); /* Everything to home location */
Bill Buzbee1465db52009-09-23 17:17:35 -07002272 loadValueDirectFixed(cUnit, rlSrc, r0);
Bill Buzbeec6f10662010-02-09 11:16:15 -08002273 dvmCompilerClobber(cUnit, r0);
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002274 if ((dalvikOpcode == OP_DIV_INT_LIT8) ||
2275 (dalvikOpcode == OP_DIV_INT_LIT16)) {
Ben Chengbd1326d2010-04-02 15:04:53 -07002276 LOAD_FUNC_ADDR(cUnit, r2, (int)__aeabi_idiv);
Bill Buzbee1465db52009-09-23 17:17:35 -07002277 isDiv = true;
2278 } else {
Ben Chengbd1326d2010-04-02 15:04:53 -07002279 LOAD_FUNC_ADDR(cUnit, r2, (int)__aeabi_idivmod);
Bill Buzbee1465db52009-09-23 17:17:35 -07002280 isDiv = false;
2281 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07002282 loadConstant(cUnit, r1, lit);
Bill Buzbee1465db52009-09-23 17:17:35 -07002283 opReg(cUnit, kOpBlx, r2);
Elliott Hughes6a555132010-02-25 15:41:42 -08002284 dvmCompilerClobberCallRegs(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07002285 if (isDiv)
Bill Buzbeec6f10662010-02-09 11:16:15 -08002286 rlResult = dvmCompilerGetReturn(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07002287 else
Bill Buzbeec6f10662010-02-09 11:16:15 -08002288 rlResult = dvmCompilerGetReturnAlt(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07002289 storeValue(cUnit, rlDest, rlResult);
2290 return false;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002291 break;
2292 default:
2293 return true;
2294 }
Bill Buzbee1465db52009-09-23 17:17:35 -07002295 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
Bill Buzbeec6f10662010-02-09 11:16:15 -08002296 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Bill Buzbee1465db52009-09-23 17:17:35 -07002297 // Avoid shifts by literal 0 - no support in Thumb. Change to copy
2298 if (shiftOp && (lit == 0)) {
2299 genRegCopy(cUnit, rlResult.lowReg, rlSrc.lowReg);
2300 } else {
2301 opRegRegImm(cUnit, op, rlResult.lowReg, rlSrc.lowReg, lit);
2302 }
2303 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002304 return false;
2305}
2306
jeffhao71eee1f2011-01-04 14:18:54 -08002307static bool handleFmt22c_Fmt52c(CompilationUnit *cUnit, MIR *mir)
Ben Chengba4fc8b2009-06-01 13:00:29 -07002308{
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002309 Opcode dalvikOpcode = mir->dalvikInsn.opcode;
buzbee4d92e682010-07-29 15:24:14 -07002310 int fieldOffset = -1;
buzbeeecf8f6e2010-07-20 14:53:42 -07002311 bool isVolatile = false;
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002312 switch (dalvikOpcode) {
buzbee4d92e682010-07-29 15:24:14 -07002313 /*
2314 * Wide volatiles currently handled via single step.
2315 * Add them here if generating in-line code.
2316 * case OP_IGET_WIDE_VOLATILE:
2317 * case OP_IPUT_WIDE_VOLATILE:
2318 */
2319 case OP_IGET:
2320 case OP_IGET_VOLATILE:
jeffhao71eee1f2011-01-04 14:18:54 -08002321 case OP_IGET_JUMBO:
buzbee4d92e682010-07-29 15:24:14 -07002322 case OP_IGET_WIDE:
jeffhao71eee1f2011-01-04 14:18:54 -08002323 case OP_IGET_WIDE_JUMBO:
buzbee4d92e682010-07-29 15:24:14 -07002324 case OP_IGET_OBJECT:
2325 case OP_IGET_OBJECT_VOLATILE:
jeffhao71eee1f2011-01-04 14:18:54 -08002326 case OP_IGET_OBJECT_JUMBO:
buzbee4d92e682010-07-29 15:24:14 -07002327 case OP_IGET_BOOLEAN:
jeffhao71eee1f2011-01-04 14:18:54 -08002328 case OP_IGET_BOOLEAN_JUMBO:
buzbee4d92e682010-07-29 15:24:14 -07002329 case OP_IGET_BYTE:
jeffhao71eee1f2011-01-04 14:18:54 -08002330 case OP_IGET_BYTE_JUMBO:
buzbee4d92e682010-07-29 15:24:14 -07002331 case OP_IGET_CHAR:
jeffhao71eee1f2011-01-04 14:18:54 -08002332 case OP_IGET_CHAR_JUMBO:
buzbee4d92e682010-07-29 15:24:14 -07002333 case OP_IGET_SHORT:
jeffhao71eee1f2011-01-04 14:18:54 -08002334 case OP_IGET_SHORT_JUMBO:
buzbee4d92e682010-07-29 15:24:14 -07002335 case OP_IPUT:
2336 case OP_IPUT_VOLATILE:
jeffhao71eee1f2011-01-04 14:18:54 -08002337 case OP_IPUT_JUMBO:
buzbee4d92e682010-07-29 15:24:14 -07002338 case OP_IPUT_WIDE:
jeffhao71eee1f2011-01-04 14:18:54 -08002339 case OP_IPUT_WIDE_JUMBO:
buzbee4d92e682010-07-29 15:24:14 -07002340 case OP_IPUT_OBJECT:
2341 case OP_IPUT_OBJECT_VOLATILE:
jeffhao71eee1f2011-01-04 14:18:54 -08002342 case OP_IPUT_OBJECT_JUMBO:
buzbee4d92e682010-07-29 15:24:14 -07002343 case OP_IPUT_BOOLEAN:
jeffhao71eee1f2011-01-04 14:18:54 -08002344 case OP_IPUT_BOOLEAN_JUMBO:
buzbee4d92e682010-07-29 15:24:14 -07002345 case OP_IPUT_BYTE:
jeffhao71eee1f2011-01-04 14:18:54 -08002346 case OP_IPUT_BYTE_JUMBO:
buzbee4d92e682010-07-29 15:24:14 -07002347 case OP_IPUT_CHAR:
jeffhao71eee1f2011-01-04 14:18:54 -08002348 case OP_IPUT_CHAR_JUMBO:
2349 case OP_IPUT_SHORT:
2350 case OP_IPUT_SHORT_JUMBO: {
Ben Cheng7a2697d2010-06-07 13:44:23 -07002351 const Method *method = (mir->OptimizationFlags & MIR_CALLEE) ?
2352 mir->meta.calleeMethod : cUnit->method;
buzbee4d92e682010-07-29 15:24:14 -07002353 Field *fieldPtr =
Ben Cheng7a2697d2010-06-07 13:44:23 -07002354 method->clazz->pDvmDex->pResFields[mir->dalvikInsn.vC];
Ben Chengba4fc8b2009-06-01 13:00:29 -07002355
buzbee4d92e682010-07-29 15:24:14 -07002356 if (fieldPtr == NULL) {
2357 LOGE("Unexpected null instance field");
2358 dvmAbort();
2359 }
2360 isVolatile = dvmIsVolatileField(fieldPtr);
2361 fieldOffset = ((InstField *)fieldPtr)->byteOffset;
2362 break;
Ben Chengdd6e8702010-05-07 13:05:47 -07002363 }
buzbee4d92e682010-07-29 15:24:14 -07002364 default:
2365 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002366 }
buzbee4d92e682010-07-29 15:24:14 -07002367
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002368 switch (dalvikOpcode) {
jeffhao71eee1f2011-01-04 14:18:54 -08002369 case OP_NEW_ARRAY:
2370 case OP_NEW_ARRAY_JUMBO: {
Bill Buzbee1465db52009-09-23 17:17:35 -07002371 // Generates a call - use explicit registers
Bill Buzbeec6f10662010-02-09 11:16:15 -08002372 RegLocation rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
2373 RegLocation rlDest = dvmCompilerGetDest(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07002374 RegLocation rlResult;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002375 void *classPtr = (void*)
2376 (cUnit->method->clazz->pDvmDex->pResClasses[mir->dalvikInsn.vC]);
Ben Chengdd6e8702010-05-07 13:05:47 -07002377
2378 if (classPtr == NULL) {
2379 LOGE("Unexpected null class");
2380 dvmAbort();
2381 }
2382
Bill Buzbeec6f10662010-02-09 11:16:15 -08002383 dvmCompilerFlushAllRegs(cUnit); /* Everything to home location */
Bill Buzbee1465db52009-09-23 17:17:35 -07002384 genExportPC(cUnit, mir);
2385 loadValueDirectFixed(cUnit, rlSrc, r1); /* Len */
Ben Chengba4fc8b2009-06-01 13:00:29 -07002386 loadConstant(cUnit, r0, (int) classPtr );
Ben Chengbd1326d2010-04-02 15:04:53 -07002387 LOAD_FUNC_ADDR(cUnit, r3, (int)dvmAllocArrayByClass);
Ben Cheng4f489172009-09-27 17:08:35 -07002388 /*
2389 * "len < 0": bail to the interpreter to re-execute the
2390 * instruction
2391 */
Carl Shapiroe3c01da2010-05-20 22:54:18 -07002392 genRegImmCheck(cUnit, kArmCondMi, r1, 0, mir->offset, NULL);
Bill Buzbee270c1d62009-08-13 16:58:07 -07002393 loadConstant(cUnit, r2, ALLOC_DONT_TRACK);
Bill Buzbee1465db52009-09-23 17:17:35 -07002394 opReg(cUnit, kOpBlx, r3);
Elliott Hughes6a555132010-02-25 15:41:42 -08002395 dvmCompilerClobberCallRegs(cUnit);
Ben Cheng4f489172009-09-27 17:08:35 -07002396 /* generate a branch over if allocation is successful */
buzbee8f8109a2010-08-31 10:16:35 -07002397 ArmLIR *branchOver = genCmpImmBranch(cUnit, kArmCondNe, r0, 0);
Ben Cheng4f489172009-09-27 17:08:35 -07002398 /*
2399 * OOM exception needs to be thrown here and cannot re-execute
2400 */
2401 loadConstant(cUnit, r0,
2402 (int) (cUnit->method->insns + mir->offset));
2403 genDispatchToHandler(cUnit, TEMPLATE_THROW_EXCEPTION_COMMON);
2404 /* noreturn */
2405
Bill Buzbee1465db52009-09-23 17:17:35 -07002406 ArmLIR *target = newLIR0(cUnit, kArmPseudoTargetLabel);
Ben Cheng4f489172009-09-27 17:08:35 -07002407 target->defMask = ENCODE_ALL;
2408 branchOver->generic.target = (LIR *) target;
Bill Buzbeec6f10662010-02-09 11:16:15 -08002409 rlResult = dvmCompilerGetReturn(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07002410 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002411 break;
2412 }
jeffhao71eee1f2011-01-04 14:18:54 -08002413 case OP_INSTANCE_OF:
2414 case OP_INSTANCE_OF_JUMBO: {
Bill Buzbee1465db52009-09-23 17:17:35 -07002415 // May generate a call - use explicit registers
Bill Buzbeec6f10662010-02-09 11:16:15 -08002416 RegLocation rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
2417 RegLocation rlDest = dvmCompilerGetDest(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07002418 RegLocation rlResult;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002419 ClassObject *classPtr =
2420 (cUnit->method->clazz->pDvmDex->pResClasses[mir->dalvikInsn.vC]);
Bill Buzbee480e6782010-01-27 15:43:08 -08002421 /*
2422 * Note: It is possible that classPtr is NULL at this point,
2423 * even though this instruction has been successfully interpreted.
2424 * If the previous interpretation had a null source, the
2425 * interpreter would not have bothered to resolve the clazz.
2426 * Bail out to the interpreter in this case, and log it
2427 * so that we can tell if it happens frequently.
2428 */
2429 if (classPtr == NULL) {
2430 LOGD("null clazz in OP_INSTANCE_OF, single-stepping");
2431 genInterpSingleStep(cUnit, mir);
2432 break;
2433 }
Bill Buzbeec6f10662010-02-09 11:16:15 -08002434 dvmCompilerFlushAllRegs(cUnit); /* Everything to home location */
Bill Buzbee1465db52009-09-23 17:17:35 -07002435 loadValueDirectFixed(cUnit, rlSrc, r0); /* Ref */
Ben Chengba4fc8b2009-06-01 13:00:29 -07002436 loadConstant(cUnit, r2, (int) classPtr );
Ben Cheng752c7942009-06-22 10:50:07 -07002437 /* When taken r0 has NULL which can be used for store directly */
buzbee8f8109a2010-08-31 10:16:35 -07002438 ArmLIR *branch1 = genCmpImmBranch(cUnit, kArmCondEq, r0, 0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002439 /* r1 now contains object->clazz */
Bill Buzbee270c1d62009-08-13 16:58:07 -07002440 loadWordDisp(cUnit, r0, offsetof(Object, clazz), r1);
Bill Buzbee1465db52009-09-23 17:17:35 -07002441 /* r1 now contains object->clazz */
Ben Chengbd1326d2010-04-02 15:04:53 -07002442 LOAD_FUNC_ADDR(cUnit, r3, (int)dvmInstanceofNonTrivial);
Ben Cheng752c7942009-06-22 10:50:07 -07002443 loadConstant(cUnit, r0, 1); /* Assume true */
Bill Buzbee1465db52009-09-23 17:17:35 -07002444 opRegReg(cUnit, kOpCmp, r1, r2);
2445 ArmLIR *branch2 = opCondBranch(cUnit, kArmCondEq);
2446 genRegCopy(cUnit, r0, r1);
2447 genRegCopy(cUnit, r1, r2);
2448 opReg(cUnit, kOpBlx, r3);
Elliott Hughes6a555132010-02-25 15:41:42 -08002449 dvmCompilerClobberCallRegs(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002450 /* branch target here */
Bill Buzbee1465db52009-09-23 17:17:35 -07002451 ArmLIR *target = newLIR0(cUnit, kArmPseudoTargetLabel);
Ben Chengd7d426a2009-09-22 11:23:36 -07002452 target->defMask = ENCODE_ALL;
Bill Buzbeec6f10662010-02-09 11:16:15 -08002453 rlResult = dvmCompilerGetReturn(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07002454 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002455 branch1->generic.target = (LIR *)target;
2456 branch2->generic.target = (LIR *)target;
2457 break;
2458 }
2459 case OP_IGET_WIDE:
jeffhao71eee1f2011-01-04 14:18:54 -08002460 case OP_IGET_WIDE_JUMBO:
Ben Chengba4fc8b2009-06-01 13:00:29 -07002461 genIGetWide(cUnit, mir, fieldOffset);
2462 break;
buzbeeecf8f6e2010-07-20 14:53:42 -07002463 case OP_IGET_VOLATILE:
2464 case OP_IGET_OBJECT_VOLATILE:
2465 isVolatile = true;
2466 // NOTE: intentional fallthrough
Ben Chengba4fc8b2009-06-01 13:00:29 -07002467 case OP_IGET:
jeffhao71eee1f2011-01-04 14:18:54 -08002468 case OP_IGET_JUMBO:
Ben Chengba4fc8b2009-06-01 13:00:29 -07002469 case OP_IGET_OBJECT:
jeffhao71eee1f2011-01-04 14:18:54 -08002470 case OP_IGET_OBJECT_JUMBO:
Ben Chengba4fc8b2009-06-01 13:00:29 -07002471 case OP_IGET_BOOLEAN:
jeffhao71eee1f2011-01-04 14:18:54 -08002472 case OP_IGET_BOOLEAN_JUMBO:
Ben Chengba4fc8b2009-06-01 13:00:29 -07002473 case OP_IGET_BYTE:
jeffhao71eee1f2011-01-04 14:18:54 -08002474 case OP_IGET_BYTE_JUMBO:
Ben Chengba4fc8b2009-06-01 13:00:29 -07002475 case OP_IGET_CHAR:
jeffhao71eee1f2011-01-04 14:18:54 -08002476 case OP_IGET_CHAR_JUMBO:
Ben Chengba4fc8b2009-06-01 13:00:29 -07002477 case OP_IGET_SHORT:
jeffhao71eee1f2011-01-04 14:18:54 -08002478 case OP_IGET_SHORT_JUMBO:
buzbee3272e2f2010-09-09 14:07:01 -07002479 genIGet(cUnit, mir, kWord, fieldOffset, isVolatile);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002480 break;
2481 case OP_IPUT_WIDE:
jeffhao71eee1f2011-01-04 14:18:54 -08002482 case OP_IPUT_WIDE_JUMBO:
Ben Chengba4fc8b2009-06-01 13:00:29 -07002483 genIPutWide(cUnit, mir, fieldOffset);
2484 break;
2485 case OP_IPUT:
jeffhao71eee1f2011-01-04 14:18:54 -08002486 case OP_IPUT_JUMBO:
buzbee3272e2f2010-09-09 14:07:01 -07002487 case OP_IPUT_BOOLEAN:
jeffhao71eee1f2011-01-04 14:18:54 -08002488 case OP_IPUT_BOOLEAN_JUMBO:
2489 case OP_IPUT_BYTE:
2490 case OP_IPUT_BYTE_JUMBO:
2491 case OP_IPUT_CHAR:
2492 case OP_IPUT_CHAR_JUMBO:
2493 case OP_IPUT_SHORT:
2494 case OP_IPUT_SHORT_JUMBO:
buzbeeecf8f6e2010-07-20 14:53:42 -07002495 genIPut(cUnit, mir, kWord, fieldOffset, false, isVolatile);
buzbee919eb062010-07-12 12:59:22 -07002496 break;
buzbee4d92e682010-07-29 15:24:14 -07002497 case OP_IPUT_VOLATILE:
buzbeeecf8f6e2010-07-20 14:53:42 -07002498 case OP_IPUT_OBJECT_VOLATILE:
2499 isVolatile = true;
2500 // NOTE: intentional fallthrough
Ben Chengba4fc8b2009-06-01 13:00:29 -07002501 case OP_IPUT_OBJECT:
jeffhao71eee1f2011-01-04 14:18:54 -08002502 case OP_IPUT_OBJECT_JUMBO:
buzbeeecf8f6e2010-07-20 14:53:42 -07002503 genIPut(cUnit, mir, kWord, fieldOffset, true, isVolatile);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002504 break;
Bill Buzbeeb16344a2010-03-15 17:19:12 -07002505 case OP_IGET_WIDE_VOLATILE:
2506 case OP_IPUT_WIDE_VOLATILE:
Bill Buzbeeb16344a2010-03-15 17:19:12 -07002507 genInterpSingleStep(cUnit, mir);
2508 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002509 default:
2510 return true;
2511 }
2512 return false;
2513}
2514
2515static bool handleFmt22cs(CompilationUnit *cUnit, MIR *mir)
2516{
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002517 Opcode dalvikOpcode = mir->dalvikInsn.opcode;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002518 int fieldOffset = mir->dalvikInsn.vC;
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002519 switch (dalvikOpcode) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07002520 case OP_IGET_QUICK:
2521 case OP_IGET_OBJECT_QUICK:
buzbeeecf8f6e2010-07-20 14:53:42 -07002522 genIGet(cUnit, mir, kWord, fieldOffset, false);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002523 break;
2524 case OP_IPUT_QUICK:
buzbeeecf8f6e2010-07-20 14:53:42 -07002525 genIPut(cUnit, mir, kWord, fieldOffset, false, false);
buzbee919eb062010-07-12 12:59:22 -07002526 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002527 case OP_IPUT_OBJECT_QUICK:
buzbeeecf8f6e2010-07-20 14:53:42 -07002528 genIPut(cUnit, mir, kWord, fieldOffset, true, false);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002529 break;
2530 case OP_IGET_WIDE_QUICK:
2531 genIGetWide(cUnit, mir, fieldOffset);
2532 break;
2533 case OP_IPUT_WIDE_QUICK:
2534 genIPutWide(cUnit, mir, fieldOffset);
2535 break;
2536 default:
2537 return true;
2538 }
2539 return false;
2540
2541}
2542
2543/* Compare agaist zero */
2544static bool handleFmt22t(CompilationUnit *cUnit, MIR *mir, BasicBlock *bb,
Bill Buzbee89efc3d2009-07-28 11:22:22 -07002545 ArmLIR *labelList)
Ben Chengba4fc8b2009-06-01 13:00:29 -07002546{
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002547 Opcode dalvikOpcode = mir->dalvikInsn.opcode;
Bill Buzbee89efc3d2009-07-28 11:22:22 -07002548 ArmConditionCode cond;
Ben Cheng7ab74e12011-02-03 14:02:06 -08002549 /* backward branch? */
2550 bool backwardBranch = (bb->taken->startOffset <= mir->offset);
2551
2552 if (backwardBranch && gDvmJit.genSuspendPoll) {
2553 genSuspendPoll(cUnit, mir);
2554 }
2555
Bill Buzbeec6f10662010-02-09 11:16:15 -08002556 RegLocation rlSrc1 = dvmCompilerGetSrc(cUnit, mir, 0);
2557 RegLocation rlSrc2 = dvmCompilerGetSrc(cUnit, mir, 1);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002558
Bill Buzbee1465db52009-09-23 17:17:35 -07002559 rlSrc1 = loadValue(cUnit, rlSrc1, kCoreReg);
2560 rlSrc2 = loadValue(cUnit, rlSrc2, kCoreReg);
Ben Cheng7ab74e12011-02-03 14:02:06 -08002561
Bill Buzbee1465db52009-09-23 17:17:35 -07002562 opRegReg(cUnit, kOpCmp, rlSrc1.lowReg, rlSrc2.lowReg);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002563
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002564 switch (dalvikOpcode) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07002565 case OP_IF_EQ:
Bill Buzbee1465db52009-09-23 17:17:35 -07002566 cond = kArmCondEq;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002567 break;
2568 case OP_IF_NE:
Bill Buzbee1465db52009-09-23 17:17:35 -07002569 cond = kArmCondNe;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002570 break;
2571 case OP_IF_LT:
Bill Buzbee1465db52009-09-23 17:17:35 -07002572 cond = kArmCondLt;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002573 break;
2574 case OP_IF_GE:
Bill Buzbee1465db52009-09-23 17:17:35 -07002575 cond = kArmCondGe;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002576 break;
2577 case OP_IF_GT:
Bill Buzbee1465db52009-09-23 17:17:35 -07002578 cond = kArmCondGt;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002579 break;
2580 case OP_IF_LE:
Bill Buzbee1465db52009-09-23 17:17:35 -07002581 cond = kArmCondLe;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002582 break;
2583 default:
2584 cond = 0;
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002585 LOGE("Unexpected opcode (%d) for Fmt22t\n", dalvikOpcode);
Bill Buzbeefc519dc2010-03-06 23:30:57 -08002586 dvmCompilerAbort(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002587 }
2588 genConditionalBranch(cUnit, cond, &labelList[bb->taken->id]);
2589 /* This mostly likely will be optimized away in a later phase */
2590 genUnconditionalBranch(cUnit, &labelList[bb->fallThrough->id]);
2591 return false;
2592}
2593
2594static bool handleFmt22x_Fmt32x(CompilationUnit *cUnit, MIR *mir)
2595{
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002596 Opcode opcode = mir->dalvikInsn.opcode;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002597
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002598 switch (opcode) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07002599 case OP_MOVE_16:
2600 case OP_MOVE_OBJECT_16:
2601 case OP_MOVE_FROM16:
Ben Chenge9695e52009-06-16 16:11:47 -07002602 case OP_MOVE_OBJECT_FROM16: {
Bill Buzbeec6f10662010-02-09 11:16:15 -08002603 storeValue(cUnit, dvmCompilerGetDest(cUnit, mir, 0),
2604 dvmCompilerGetSrc(cUnit, mir, 0));
Ben Chengba4fc8b2009-06-01 13:00:29 -07002605 break;
Ben Chenge9695e52009-06-16 16:11:47 -07002606 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07002607 case OP_MOVE_WIDE_16:
Ben Chenge9695e52009-06-16 16:11:47 -07002608 case OP_MOVE_WIDE_FROM16: {
Bill Buzbeec6f10662010-02-09 11:16:15 -08002609 storeValueWide(cUnit, dvmCompilerGetDestWide(cUnit, mir, 0, 1),
2610 dvmCompilerGetSrcWide(cUnit, mir, 0, 1));
Ben Chengba4fc8b2009-06-01 13:00:29 -07002611 break;
Ben Chenge9695e52009-06-16 16:11:47 -07002612 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07002613 default:
2614 return true;
2615 }
2616 return false;
2617}
2618
2619static bool handleFmt23x(CompilationUnit *cUnit, MIR *mir)
2620{
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002621 Opcode opcode = mir->dalvikInsn.opcode;
Bill Buzbee1465db52009-09-23 17:17:35 -07002622 RegLocation rlSrc1;
2623 RegLocation rlSrc2;
2624 RegLocation rlDest;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002625
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002626 if ( (opcode >= OP_ADD_INT) && (opcode <= OP_REM_DOUBLE)) {
Ben Cheng5d90c202009-11-22 23:31:11 -08002627 return genArithOp( cUnit, mir );
Ben Chengba4fc8b2009-06-01 13:00:29 -07002628 }
2629
Bill Buzbee1465db52009-09-23 17:17:35 -07002630 /* APUTs have 3 sources and no targets */
2631 if (mir->ssaRep->numDefs == 0) {
2632 if (mir->ssaRep->numUses == 3) {
Bill Buzbeec6f10662010-02-09 11:16:15 -08002633 rlDest = dvmCompilerGetSrc(cUnit, mir, 0);
2634 rlSrc1 = dvmCompilerGetSrc(cUnit, mir, 1);
2635 rlSrc2 = dvmCompilerGetSrc(cUnit, mir, 2);
Bill Buzbee1465db52009-09-23 17:17:35 -07002636 } else {
2637 assert(mir->ssaRep->numUses == 4);
Bill Buzbeec6f10662010-02-09 11:16:15 -08002638 rlDest = dvmCompilerGetSrcWide(cUnit, mir, 0, 1);
2639 rlSrc1 = dvmCompilerGetSrc(cUnit, mir, 2);
2640 rlSrc2 = dvmCompilerGetSrc(cUnit, mir, 3);
Bill Buzbee1465db52009-09-23 17:17:35 -07002641 }
2642 } else {
2643 /* Two sources and 1 dest. Deduce the operand sizes */
2644 if (mir->ssaRep->numUses == 4) {
Bill Buzbeec6f10662010-02-09 11:16:15 -08002645 rlSrc1 = dvmCompilerGetSrcWide(cUnit, mir, 0, 1);
2646 rlSrc2 = dvmCompilerGetSrcWide(cUnit, mir, 2, 3);
Bill Buzbee1465db52009-09-23 17:17:35 -07002647 } else {
2648 assert(mir->ssaRep->numUses == 2);
Bill Buzbeec6f10662010-02-09 11:16:15 -08002649 rlSrc1 = dvmCompilerGetSrc(cUnit, mir, 0);
2650 rlSrc2 = dvmCompilerGetSrc(cUnit, mir, 1);
Bill Buzbee1465db52009-09-23 17:17:35 -07002651 }
2652 if (mir->ssaRep->numDefs == 2) {
Bill Buzbeec6f10662010-02-09 11:16:15 -08002653 rlDest = dvmCompilerGetDestWide(cUnit, mir, 0, 1);
Bill Buzbee1465db52009-09-23 17:17:35 -07002654 } else {
2655 assert(mir->ssaRep->numDefs == 1);
Bill Buzbeec6f10662010-02-09 11:16:15 -08002656 rlDest = dvmCompilerGetDest(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07002657 }
2658 }
2659
2660
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002661 switch (opcode) {
Bill Buzbeed45ba372009-06-15 17:00:57 -07002662 case OP_CMPL_FLOAT:
2663 case OP_CMPG_FLOAT:
2664 case OP_CMPL_DOUBLE:
2665 case OP_CMPG_DOUBLE:
Ben Cheng5d90c202009-11-22 23:31:11 -08002666 return genCmpFP(cUnit, mir, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002667 case OP_CMP_LONG:
Bill Buzbee1465db52009-09-23 17:17:35 -07002668 genCmpLong(cUnit, mir, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002669 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002670 case OP_AGET_WIDE:
Bill Buzbee1465db52009-09-23 17:17:35 -07002671 genArrayGet(cUnit, mir, kLong, rlSrc1, rlSrc2, rlDest, 3);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002672 break;
2673 case OP_AGET:
2674 case OP_AGET_OBJECT:
Bill Buzbee1465db52009-09-23 17:17:35 -07002675 genArrayGet(cUnit, mir, kWord, rlSrc1, rlSrc2, rlDest, 2);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002676 break;
2677 case OP_AGET_BOOLEAN:
Bill Buzbee1465db52009-09-23 17:17:35 -07002678 genArrayGet(cUnit, mir, kUnsignedByte, rlSrc1, rlSrc2, rlDest, 0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002679 break;
2680 case OP_AGET_BYTE:
Bill Buzbee1465db52009-09-23 17:17:35 -07002681 genArrayGet(cUnit, mir, kSignedByte, rlSrc1, rlSrc2, rlDest, 0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002682 break;
2683 case OP_AGET_CHAR:
Bill Buzbee1465db52009-09-23 17:17:35 -07002684 genArrayGet(cUnit, mir, kUnsignedHalf, rlSrc1, rlSrc2, rlDest, 1);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002685 break;
2686 case OP_AGET_SHORT:
Bill Buzbee1465db52009-09-23 17:17:35 -07002687 genArrayGet(cUnit, mir, kSignedHalf, rlSrc1, rlSrc2, rlDest, 1);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002688 break;
2689 case OP_APUT_WIDE:
Bill Buzbee1465db52009-09-23 17:17:35 -07002690 genArrayPut(cUnit, mir, kLong, rlSrc1, rlSrc2, rlDest, 3);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002691 break;
2692 case OP_APUT:
Bill Buzbee1465db52009-09-23 17:17:35 -07002693 genArrayPut(cUnit, mir, kWord, rlSrc1, rlSrc2, rlDest, 2);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002694 break;
Bill Buzbeebe6534f2010-03-12 16:01:35 -08002695 case OP_APUT_OBJECT:
2696 genArrayObjectPut(cUnit, mir, rlSrc1, rlSrc2, rlDest, 2);
2697 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002698 case OP_APUT_SHORT:
2699 case OP_APUT_CHAR:
Bill Buzbee1465db52009-09-23 17:17:35 -07002700 genArrayPut(cUnit, mir, kUnsignedHalf, rlSrc1, rlSrc2, rlDest, 1);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002701 break;
2702 case OP_APUT_BYTE:
2703 case OP_APUT_BOOLEAN:
Bill Buzbee1465db52009-09-23 17:17:35 -07002704 genArrayPut(cUnit, mir, kUnsignedByte, rlSrc1, rlSrc2, rlDest, 0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002705 break;
2706 default:
2707 return true;
2708 }
2709 return false;
2710}
2711
Ben Cheng6c10a972009-10-29 14:39:18 -07002712/*
2713 * Find the matching case.
2714 *
2715 * return values:
2716 * r0 (low 32-bit): pc of the chaining cell corresponding to the resolved case,
2717 * including default which is placed at MIN(size, MAX_CHAINED_SWITCH_CASES).
2718 * r1 (high 32-bit): the branch offset of the matching case (only for indexes
2719 * above MAX_CHAINED_SWITCH_CASES).
2720 *
2721 * Instructions around the call are:
2722 *
2723 * mov r2, pc
2724 * blx &findPackedSwitchIndex
2725 * mov pc, r0
2726 * .align4
Bill Buzbeebd047242010-05-13 13:02:53 -07002727 * chaining cell for case 0 [12 bytes]
2728 * chaining cell for case 1 [12 bytes]
Ben Cheng6c10a972009-10-29 14:39:18 -07002729 * :
Bill Buzbeebd047242010-05-13 13:02:53 -07002730 * chaining cell for case MIN(size, MAX_CHAINED_SWITCH_CASES)-1 [12 bytes]
Ben Cheng6c10a972009-10-29 14:39:18 -07002731 * chaining cell for case default [8 bytes]
2732 * noChain exit
2733 */
Ben Chengbd1326d2010-04-02 15:04:53 -07002734static s8 findPackedSwitchIndex(const u2* switchData, int testVal, int pc)
Ben Cheng6c10a972009-10-29 14:39:18 -07002735{
2736 int size;
2737 int firstKey;
2738 const int *entries;
2739 int index;
2740 int jumpIndex;
2741 int caseDPCOffset = 0;
2742 /* In Thumb mode pc is 4 ahead of the "mov r2, pc" instruction */
2743 int chainingPC = (pc + 4) & ~3;
2744
2745 /*
2746 * Packed switch data format:
2747 * ushort ident = 0x0100 magic value
2748 * ushort size number of entries in the table
2749 * int first_key first (and lowest) switch case value
2750 * int targets[size] branch targets, relative to switch opcode
2751 *
2752 * Total size is (4+size*2) 16-bit code units.
2753 */
2754 size = switchData[1];
2755 assert(size > 0);
2756
2757 firstKey = switchData[2];
2758 firstKey |= switchData[3] << 16;
2759
2760
2761 /* The entries are guaranteed to be aligned on a 32-bit boundary;
2762 * we can treat them as a native int array.
2763 */
2764 entries = (const int*) &switchData[4];
2765 assert(((u4)entries & 0x3) == 0);
2766
2767 index = testVal - firstKey;
2768
2769 /* Jump to the default cell */
2770 if (index < 0 || index >= size) {
2771 jumpIndex = MIN(size, MAX_CHAINED_SWITCH_CASES);
2772 /* Jump to the non-chaining exit point */
2773 } else if (index >= MAX_CHAINED_SWITCH_CASES) {
2774 jumpIndex = MAX_CHAINED_SWITCH_CASES + 1;
2775 caseDPCOffset = entries[index];
2776 /* Jump to the inline chaining cell */
2777 } else {
2778 jumpIndex = index;
2779 }
2780
Bill Buzbeebd047242010-05-13 13:02:53 -07002781 chainingPC += jumpIndex * CHAIN_CELL_NORMAL_SIZE;
Ben Cheng6c10a972009-10-29 14:39:18 -07002782 return (((s8) caseDPCOffset) << 32) | (u8) chainingPC;
2783}
2784
2785/* See comments for findPackedSwitchIndex */
Ben Chengbd1326d2010-04-02 15:04:53 -07002786static s8 findSparseSwitchIndex(const u2* switchData, int testVal, int pc)
Ben Cheng6c10a972009-10-29 14:39:18 -07002787{
2788 int size;
2789 const int *keys;
2790 const int *entries;
2791 int chainingPC = (pc + 4) & ~3;
2792 int i;
2793
2794 /*
2795 * Sparse switch data format:
2796 * ushort ident = 0x0200 magic value
2797 * ushort size number of entries in the table; > 0
2798 * int keys[size] keys, sorted low-to-high; 32-bit aligned
2799 * int targets[size] branch targets, relative to switch opcode
2800 *
2801 * Total size is (2+size*4) 16-bit code units.
2802 */
2803
2804 size = switchData[1];
2805 assert(size > 0);
2806
2807 /* The keys are guaranteed to be aligned on a 32-bit boundary;
2808 * we can treat them as a native int array.
2809 */
2810 keys = (const int*) &switchData[2];
2811 assert(((u4)keys & 0x3) == 0);
2812
2813 /* The entries are guaranteed to be aligned on a 32-bit boundary;
2814 * we can treat them as a native int array.
2815 */
2816 entries = keys + size;
2817 assert(((u4)entries & 0x3) == 0);
2818
2819 /*
2820 * Run through the list of keys, which are guaranteed to
2821 * be sorted low-to-high.
2822 *
2823 * Most tables have 3-4 entries. Few have more than 10. A binary
2824 * search here is probably not useful.
2825 */
2826 for (i = 0; i < size; i++) {
2827 int k = keys[i];
2828 if (k == testVal) {
2829 /* MAX_CHAINED_SWITCH_CASES + 1 is the start of the overflow case */
2830 int jumpIndex = (i < MAX_CHAINED_SWITCH_CASES) ?
2831 i : MAX_CHAINED_SWITCH_CASES + 1;
Bill Buzbeebd047242010-05-13 13:02:53 -07002832 chainingPC += jumpIndex * CHAIN_CELL_NORMAL_SIZE;
Ben Cheng6c10a972009-10-29 14:39:18 -07002833 return (((s8) entries[i]) << 32) | (u8) chainingPC;
2834 } else if (k > testVal) {
2835 break;
2836 }
2837 }
Bill Buzbeebd047242010-05-13 13:02:53 -07002838 return chainingPC + MIN(size, MAX_CHAINED_SWITCH_CASES) *
2839 CHAIN_CELL_NORMAL_SIZE;
Ben Cheng6c10a972009-10-29 14:39:18 -07002840}
2841
Ben Chengba4fc8b2009-06-01 13:00:29 -07002842static bool handleFmt31t(CompilationUnit *cUnit, MIR *mir)
2843{
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002844 Opcode dalvikOpcode = mir->dalvikInsn.opcode;
2845 switch (dalvikOpcode) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07002846 case OP_FILL_ARRAY_DATA: {
Bill Buzbeec6f10662010-02-09 11:16:15 -08002847 RegLocation rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07002848 // Making a call - use explicit registers
Bill Buzbeec6f10662010-02-09 11:16:15 -08002849 dvmCompilerFlushAllRegs(cUnit); /* Everything to home location */
Bill Buzbee1465db52009-09-23 17:17:35 -07002850 genExportPC(cUnit, mir);
2851 loadValueDirectFixed(cUnit, rlSrc, r0);
Ben Chengbd1326d2010-04-02 15:04:53 -07002852 LOAD_FUNC_ADDR(cUnit, r2, (int)dvmInterpHandleFillArrayData);
Ben Cheng6c10a972009-10-29 14:39:18 -07002853 loadConstant(cUnit, r1,
2854 (int) (cUnit->method->insns + mir->offset + mir->dalvikInsn.vB));
Bill Buzbee1465db52009-09-23 17:17:35 -07002855 opReg(cUnit, kOpBlx, r2);
Elliott Hughes6a555132010-02-25 15:41:42 -08002856 dvmCompilerClobberCallRegs(cUnit);
Bill Buzbeefd023aa2009-11-02 09:23:49 -08002857 /* generate a branch over if successful */
buzbee8f8109a2010-08-31 10:16:35 -07002858 ArmLIR *branchOver = genCmpImmBranch(cUnit, kArmCondNe, r0, 0);
Bill Buzbeefd023aa2009-11-02 09:23:49 -08002859 loadConstant(cUnit, r0,
2860 (int) (cUnit->method->insns + mir->offset));
2861 genDispatchToHandler(cUnit, TEMPLATE_THROW_EXCEPTION_COMMON);
2862 ArmLIR *target = newLIR0(cUnit, kArmPseudoTargetLabel);
2863 target->defMask = ENCODE_ALL;
2864 branchOver->generic.target = (LIR *) target;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002865 break;
2866 }
2867 /*
Ben Cheng6c10a972009-10-29 14:39:18 -07002868 * Compute the goto target of up to
2869 * MIN(switchSize, MAX_CHAINED_SWITCH_CASES) + 1 chaining cells.
2870 * See the comment before findPackedSwitchIndex for the code layout.
Ben Chengba4fc8b2009-06-01 13:00:29 -07002871 */
2872 case OP_PACKED_SWITCH:
2873 case OP_SPARSE_SWITCH: {
Bill Buzbeec6f10662010-02-09 11:16:15 -08002874 RegLocation rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
2875 dvmCompilerFlushAllRegs(cUnit); /* Everything to home location */
Bill Buzbee1465db52009-09-23 17:17:35 -07002876 loadValueDirectFixed(cUnit, rlSrc, r1);
Bill Buzbeec6f10662010-02-09 11:16:15 -08002877 dvmCompilerLockAllTemps(cUnit);
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002878 if (dalvikOpcode == OP_PACKED_SWITCH) {
Ben Chengbd1326d2010-04-02 15:04:53 -07002879 LOAD_FUNC_ADDR(cUnit, r4PC, (int)findPackedSwitchIndex);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002880 } else {
Ben Chengbd1326d2010-04-02 15:04:53 -07002881 LOAD_FUNC_ADDR(cUnit, r4PC, (int)findSparseSwitchIndex);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002882 }
Ben Cheng6c10a972009-10-29 14:39:18 -07002883 /* r0 <- Addr of the switch data */
2884 loadConstant(cUnit, r0,
2885 (int) (cUnit->method->insns + mir->offset + mir->dalvikInsn.vB));
2886 /* r2 <- pc of the instruction following the blx */
2887 opRegReg(cUnit, kOpMov, r2, rpc);
Bill Buzbee1465db52009-09-23 17:17:35 -07002888 opReg(cUnit, kOpBlx, r4PC);
Elliott Hughes6a555132010-02-25 15:41:42 -08002889 dvmCompilerClobberCallRegs(cUnit);
Ben Cheng6c10a972009-10-29 14:39:18 -07002890 /* pc <- computed goto target */
2891 opRegReg(cUnit, kOpMov, rpc, r0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002892 break;
2893 }
2894 default:
2895 return true;
2896 }
2897 return false;
2898}
2899
Ben Cheng7a2697d2010-06-07 13:44:23 -07002900/*
2901 * See the example of predicted inlining listed before the
2902 * genValidationForPredictedInline function. The function here takes care the
2903 * branch over at 0x4858de78 and the misprediction target at 0x4858de7a.
2904 */
2905static void genLandingPadForMispredictedCallee(CompilationUnit *cUnit, MIR *mir,
2906 BasicBlock *bb,
2907 ArmLIR *labelList)
2908{
2909 BasicBlock *fallThrough = bb->fallThrough;
2910
2911 /* Bypass the move-result block if there is one */
2912 if (fallThrough->firstMIRInsn) {
2913 assert(fallThrough->firstMIRInsn->OptimizationFlags & MIR_INLINED_PRED);
2914 fallThrough = fallThrough->fallThrough;
2915 }
2916 /* Generate a branch over if the predicted inlining is correct */
2917 genUnconditionalBranch(cUnit, &labelList[fallThrough->id]);
2918
2919 /* Reset the register state */
2920 dvmCompilerResetRegPool(cUnit);
2921 dvmCompilerClobberAllRegs(cUnit);
2922 dvmCompilerResetNullCheck(cUnit);
2923
2924 /* Target for the slow invoke path */
2925 ArmLIR *target = newLIR0(cUnit, kArmPseudoTargetLabel);
2926 target->defMask = ENCODE_ALL;
2927 /* Hook up the target to the verification branch */
2928 mir->meta.callsiteInfo->misPredBranchOver->target = (LIR *) target;
2929}
2930
jeffhao71eee1f2011-01-04 14:18:54 -08002931static bool handleFmt35c_3rc_5rc(CompilationUnit *cUnit, MIR *mir,
2932 BasicBlock *bb, ArmLIR *labelList)
Ben Chengba4fc8b2009-06-01 13:00:29 -07002933{
Bill Buzbee9bc3df32009-07-30 10:52:29 -07002934 ArmLIR *retChainingCell = NULL;
Bill Buzbee89efc3d2009-07-28 11:22:22 -07002935 ArmLIR *pcrLabel = NULL;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002936
Ben Cheng7a2697d2010-06-07 13:44:23 -07002937 /* An invoke with the MIR_INLINED is effectively a no-op */
2938 if (mir->OptimizationFlags & MIR_INLINED)
2939 return false;
2940
Bill Buzbeef4ce16f2009-07-28 13:28:25 -07002941 if (bb->fallThrough != NULL)
2942 retChainingCell = &labelList[bb->fallThrough->id];
2943
Ben Chengba4fc8b2009-06-01 13:00:29 -07002944 DecodedInstruction *dInsn = &mir->dalvikInsn;
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002945 switch (mir->dalvikInsn.opcode) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07002946 /*
2947 * calleeMethod = this->clazz->vtable[
2948 * method->clazz->pDvmDex->pResMethods[BBBB]->methodIndex
2949 * ]
2950 */
2951 case OP_INVOKE_VIRTUAL:
jeffhao71eee1f2011-01-04 14:18:54 -08002952 case OP_INVOKE_VIRTUAL_RANGE:
2953 case OP_INVOKE_VIRTUAL_JUMBO: {
Bill Buzbee89efc3d2009-07-28 11:22:22 -07002954 ArmLIR *predChainingCell = &labelList[bb->taken->id];
Ben Chengba4fc8b2009-06-01 13:00:29 -07002955 int methodIndex =
2956 cUnit->method->clazz->pDvmDex->pResMethods[dInsn->vB]->
2957 methodIndex;
2958
Ben Cheng7a2697d2010-06-07 13:44:23 -07002959 /*
2960 * If the invoke has non-null misPredBranchOver, we need to generate
2961 * the non-inlined version of the invoke here to handle the
2962 * mispredicted case.
2963 */
2964 if (mir->meta.callsiteInfo->misPredBranchOver) {
2965 genLandingPadForMispredictedCallee(cUnit, mir, bb, labelList);
2966 }
2967
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002968 if (mir->dalvikInsn.opcode == OP_INVOKE_VIRTUAL)
Ben Chengba4fc8b2009-06-01 13:00:29 -07002969 genProcessArgsNoRange(cUnit, mir, dInsn, &pcrLabel);
2970 else
2971 genProcessArgsRange(cUnit, mir, dInsn, &pcrLabel);
2972
Ben Cheng38329f52009-07-07 14:19:20 -07002973 genInvokeVirtualCommon(cUnit, mir, methodIndex,
2974 retChainingCell,
2975 predChainingCell,
2976 pcrLabel);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002977 break;
2978 }
2979 /*
2980 * calleeMethod = method->clazz->super->vtable[method->clazz->pDvmDex
2981 * ->pResMethods[BBBB]->methodIndex]
2982 */
Ben Chengba4fc8b2009-06-01 13:00:29 -07002983 case OP_INVOKE_SUPER:
jeffhao71eee1f2011-01-04 14:18:54 -08002984 case OP_INVOKE_SUPER_RANGE:
2985 case OP_INVOKE_SUPER_JUMBO: {
Ben Cheng7a2697d2010-06-07 13:44:23 -07002986 /* Grab the method ptr directly from what the interpreter sees */
2987 const Method *calleeMethod = mir->meta.callsiteInfo->method;
2988 assert(calleeMethod == cUnit->method->clazz->super->vtable[
2989 cUnit->method->clazz->pDvmDex->
2990 pResMethods[dInsn->vB]->methodIndex]);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002991
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002992 if (mir->dalvikInsn.opcode == OP_INVOKE_SUPER)
Ben Chengba4fc8b2009-06-01 13:00:29 -07002993 genProcessArgsNoRange(cUnit, mir, dInsn, &pcrLabel);
2994 else
2995 genProcessArgsRange(cUnit, mir, dInsn, &pcrLabel);
2996
2997 /* r0 = calleeMethod */
2998 loadConstant(cUnit, r0, (int) calleeMethod);
2999
Ben Cheng38329f52009-07-07 14:19:20 -07003000 genInvokeSingletonCommon(cUnit, mir, bb, labelList, pcrLabel,
3001 calleeMethod);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003002 break;
3003 }
3004 /* calleeMethod = method->clazz->pDvmDex->pResMethods[BBBB] */
3005 case OP_INVOKE_DIRECT:
jeffhao71eee1f2011-01-04 14:18:54 -08003006 case OP_INVOKE_DIRECT_RANGE:
3007 case OP_INVOKE_DIRECT_JUMBO: {
Ben Cheng7a2697d2010-06-07 13:44:23 -07003008 /* Grab the method ptr directly from what the interpreter sees */
3009 const Method *calleeMethod = mir->meta.callsiteInfo->method;
3010 assert(calleeMethod ==
3011 cUnit->method->clazz->pDvmDex->pResMethods[dInsn->vB]);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003012
Dan Bornstein9a1f8162010-12-01 17:02:26 -08003013 if (mir->dalvikInsn.opcode == OP_INVOKE_DIRECT)
Ben Chengba4fc8b2009-06-01 13:00:29 -07003014 genProcessArgsNoRange(cUnit, mir, dInsn, &pcrLabel);
3015 else
3016 genProcessArgsRange(cUnit, mir, dInsn, &pcrLabel);
3017
3018 /* r0 = calleeMethod */
3019 loadConstant(cUnit, r0, (int) calleeMethod);
3020
Ben Cheng38329f52009-07-07 14:19:20 -07003021 genInvokeSingletonCommon(cUnit, mir, bb, labelList, pcrLabel,
3022 calleeMethod);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003023 break;
3024 }
3025 /* calleeMethod = method->clazz->pDvmDex->pResMethods[BBBB] */
3026 case OP_INVOKE_STATIC:
jeffhao71eee1f2011-01-04 14:18:54 -08003027 case OP_INVOKE_STATIC_RANGE:
3028 case OP_INVOKE_STATIC_JUMBO: {
Ben Cheng7a2697d2010-06-07 13:44:23 -07003029 /* Grab the method ptr directly from what the interpreter sees */
3030 const Method *calleeMethod = mir->meta.callsiteInfo->method;
3031 assert(calleeMethod ==
3032 cUnit->method->clazz->pDvmDex->pResMethods[dInsn->vB]);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003033
Dan Bornstein9a1f8162010-12-01 17:02:26 -08003034 if (mir->dalvikInsn.opcode == OP_INVOKE_STATIC)
Ben Chengba4fc8b2009-06-01 13:00:29 -07003035 genProcessArgsNoRange(cUnit, mir, dInsn,
3036 NULL /* no null check */);
3037 else
3038 genProcessArgsRange(cUnit, mir, dInsn,
3039 NULL /* no null check */);
3040
3041 /* r0 = calleeMethod */
3042 loadConstant(cUnit, r0, (int) calleeMethod);
3043
Ben Cheng38329f52009-07-07 14:19:20 -07003044 genInvokeSingletonCommon(cUnit, mir, bb, labelList, pcrLabel,
3045 calleeMethod);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003046 break;
3047 }
Ben Cheng09e50c92010-05-02 10:45:32 -07003048 /*
Ben Chengba4fc8b2009-06-01 13:00:29 -07003049 * calleeMethod = dvmFindInterfaceMethodInCache(this->clazz,
3050 * BBBB, method, method->clazz->pDvmDex)
Ben Cheng38329f52009-07-07 14:19:20 -07003051 *
Ben Cheng09e50c92010-05-02 10:45:32 -07003052 * The following is an example of generated code for
3053 * "invoke-interface v0"
Ben Cheng38329f52009-07-07 14:19:20 -07003054 *
Ben Cheng09e50c92010-05-02 10:45:32 -07003055 * -------- dalvik offset: 0x0008 @ invoke-interface v0
3056 * 0x47357e36 : ldr r0, [r5, #0] --+
3057 * 0x47357e38 : sub r7,r5,#24 |
3058 * 0x47357e3c : cmp r0, #0 | genProcessArgsNoRange
3059 * 0x47357e3e : beq 0x47357e82 |
3060 * 0x47357e40 : stmia r7, <r0> --+
3061 * 0x47357e42 : ldr r4, [pc, #120] --> r4 <- dalvikPC of this invoke
3062 * 0x47357e44 : add r1, pc, #64 --> r1 <- &retChainingCell
3063 * 0x47357e46 : add r2, pc, #72 --> r2 <- &predictedChainingCell
3064 * 0x47357e48 : blx_1 0x47348190 --+ TEMPLATE_INVOKE_METHOD_
3065 * 0x47357e4a : blx_2 see above --+ PREDICTED_CHAIN
3066 * 0x47357e4c : b 0x47357e90 --> off to the predicted chain
3067 * 0x47357e4e : b 0x47357e82 --> punt to the interpreter
3068 * 0x47357e50 : mov r8, r1 --+
3069 * 0x47357e52 : mov r9, r2 |
3070 * 0x47357e54 : ldr r2, [pc, #96] |
3071 * 0x47357e56 : mov r10, r3 |
3072 * 0x47357e58 : movs r0, r3 | dvmFindInterfaceMethodInCache
3073 * 0x47357e5a : ldr r3, [pc, #88] |
3074 * 0x47357e5c : ldr r7, [pc, #80] |
3075 * 0x47357e5e : mov r1, #1452 |
3076 * 0x47357e62 : blx r7 --+
3077 * 0x47357e64 : cmp r0, #0 --> calleeMethod == NULL?
3078 * 0x47357e66 : bne 0x47357e6e --> branch over the throw if !r0
3079 * 0x47357e68 : ldr r0, [pc, #80] --> load Dalvik PC of the invoke
3080 * 0x47357e6a : blx_1 0x47348494 --+ TEMPLATE_THROW_EXCEPTION_
3081 * 0x47357e6c : blx_2 see above --+ COMMON
3082 * 0x47357e6e : mov r1, r8 --> r1 <- &retChainingCell
3083 * 0x47357e70 : cmp r1, #0 --> compare against 0
3084 * 0x47357e72 : bgt 0x47357e7c --> >=0? don't rechain
Ben Chengaf5aa1f2011-01-04 15:37:04 -08003085 * 0x47357e74 : ldr r7, [pc, #off] --+
Ben Cheng09e50c92010-05-02 10:45:32 -07003086 * 0x47357e76 : mov r2, r9 | dvmJitToPatchPredictedChain
3087 * 0x47357e78 : mov r3, r10 |
3088 * 0x47357e7a : blx r7 --+
3089 * 0x47357e7c : add r1, pc, #8 --> r1 <- &retChainingCell
3090 * 0x47357e7e : blx_1 0x4734809c --+ TEMPLATE_INVOKE_METHOD_NO_OPT
3091 * 0x47357e80 : blx_2 see above --+
3092 * -------- reconstruct dalvik PC : 0x425719dc @ +0x0008
3093 * 0x47357e82 : ldr r0, [pc, #56]
Ben Cheng38329f52009-07-07 14:19:20 -07003094 * Exception_Handling:
Ben Cheng09e50c92010-05-02 10:45:32 -07003095 * 0x47357e84 : ldr r1, [r6, #92]
3096 * 0x47357e86 : blx r1
3097 * 0x47357e88 : .align4
3098 * -------- chaining cell (hot): 0x000b
3099 * 0x47357e88 : ldr r0, [r6, #104]
3100 * 0x47357e8a : blx r0
3101 * 0x47357e8c : data 0x19e2(6626)
3102 * 0x47357e8e : data 0x4257(16983)
3103 * 0x47357e90 : .align4
Ben Cheng38329f52009-07-07 14:19:20 -07003104 * -------- chaining cell (predicted)
Ben Cheng09e50c92010-05-02 10:45:32 -07003105 * 0x47357e90 : data 0xe7fe(59390) --> will be patched into bx
3106 * 0x47357e92 : data 0x0000(0)
3107 * 0x47357e94 : data 0x0000(0) --> class
3108 * 0x47357e96 : data 0x0000(0)
3109 * 0x47357e98 : data 0x0000(0) --> method
3110 * 0x47357e9a : data 0x0000(0)
3111 * 0x47357e9c : data 0x0000(0) --> rechain count
3112 * 0x47357e9e : data 0x0000(0)
3113 * -------- end of chaining cells (0x006c)
3114 * 0x47357eb0 : .word (0xad03e369)
3115 * 0x47357eb4 : .word (0x28a90)
3116 * 0x47357eb8 : .word (0x41a63394)
3117 * 0x47357ebc : .word (0x425719dc)
Ben Chengba4fc8b2009-06-01 13:00:29 -07003118 */
3119 case OP_INVOKE_INTERFACE:
jeffhao71eee1f2011-01-04 14:18:54 -08003120 case OP_INVOKE_INTERFACE_RANGE:
3121 case OP_INVOKE_INTERFACE_JUMBO: {
Bill Buzbee89efc3d2009-07-28 11:22:22 -07003122 ArmLIR *predChainingCell = &labelList[bb->taken->id];
Ben Chengba4fc8b2009-06-01 13:00:29 -07003123
Ben Cheng7a2697d2010-06-07 13:44:23 -07003124 /*
3125 * If the invoke has non-null misPredBranchOver, we need to generate
3126 * the non-inlined version of the invoke here to handle the
3127 * mispredicted case.
3128 */
3129 if (mir->meta.callsiteInfo->misPredBranchOver) {
3130 genLandingPadForMispredictedCallee(cUnit, mir, bb, labelList);
3131 }
Bill Buzbee1465db52009-09-23 17:17:35 -07003132
Dan Bornstein9a1f8162010-12-01 17:02:26 -08003133 if (mir->dalvikInsn.opcode == OP_INVOKE_INTERFACE)
Ben Chengba4fc8b2009-06-01 13:00:29 -07003134 genProcessArgsNoRange(cUnit, mir, dInsn, &pcrLabel);
3135 else
3136 genProcessArgsRange(cUnit, mir, dInsn, &pcrLabel);
3137
Ben Cheng38329f52009-07-07 14:19:20 -07003138 /* "this" is already left in r0 by genProcessArgs* */
3139
3140 /* r4PC = dalvikCallsite */
3141 loadConstant(cUnit, r4PC,
3142 (int) (cUnit->method->insns + mir->offset));
3143
3144 /* r1 = &retChainingCell */
Bill Buzbee270c1d62009-08-13 16:58:07 -07003145 ArmLIR *addrRetChain =
Bill Buzbee1465db52009-09-23 17:17:35 -07003146 opRegRegImm(cUnit, kOpAdd, r1, rpc, 0);
Ben Cheng38329f52009-07-07 14:19:20 -07003147 addrRetChain->generic.target = (LIR *) retChainingCell;
3148
3149 /* r2 = &predictedChainingCell */
Bill Buzbee89efc3d2009-07-28 11:22:22 -07003150 ArmLIR *predictedChainingCell =
Bill Buzbee1465db52009-09-23 17:17:35 -07003151 opRegRegImm(cUnit, kOpAdd, r2, rpc, 0);
Ben Cheng38329f52009-07-07 14:19:20 -07003152 predictedChainingCell->generic.target = (LIR *) predChainingCell;
3153
buzbee18fba342011-01-19 15:31:15 -08003154 genDispatchToHandler(cUnit, gDvmJit.methodTraceSupport ?
3155 TEMPLATE_INVOKE_METHOD_PREDICTED_CHAIN_PROF :
3156 TEMPLATE_INVOKE_METHOD_PREDICTED_CHAIN);
Ben Cheng38329f52009-07-07 14:19:20 -07003157
3158 /* return through lr - jump to the chaining cell */
3159 genUnconditionalBranch(cUnit, predChainingCell);
3160
3161 /*
3162 * null-check on "this" may have been eliminated, but we still need
3163 * a PC-reconstruction label for stack overflow bailout.
3164 */
3165 if (pcrLabel == NULL) {
3166 int dPC = (int) (cUnit->method->insns + mir->offset);
Carl Shapirofc75f3e2010-12-07 11:43:38 -08003167 pcrLabel = (ArmLIR *) dvmCompilerNew(sizeof(ArmLIR), true);
Dan Bornstein9a1f8162010-12-01 17:02:26 -08003168 pcrLabel->opcode = kArmPseudoPCReconstructionCell;
Ben Cheng38329f52009-07-07 14:19:20 -07003169 pcrLabel->operands[0] = dPC;
3170 pcrLabel->operands[1] = mir->offset;
3171 /* Insert the place holder to the growable list */
Ben Cheng00603072010-10-28 11:13:58 -07003172 dvmInsertGrowableList(&cUnit->pcReconstructionList,
3173 (intptr_t) pcrLabel);
Ben Cheng38329f52009-07-07 14:19:20 -07003174 }
3175
3176 /* return through lr+2 - punt to the interpreter */
3177 genUnconditionalBranch(cUnit, pcrLabel);
3178
3179 /*
3180 * return through lr+4 - fully resolve the callee method.
3181 * r1 <- count
3182 * r2 <- &predictedChainCell
3183 * r3 <- this->class
3184 * r4 <- dPC
3185 * r7 <- this->class->vtable
3186 */
3187
3188 /* Save count, &predictedChainCell, and class to high regs first */
Bill Buzbee1465db52009-09-23 17:17:35 -07003189 genRegCopy(cUnit, r8, r1);
3190 genRegCopy(cUnit, r9, r2);
3191 genRegCopy(cUnit, r10, r3);
Ben Cheng38329f52009-07-07 14:19:20 -07003192
Ben Chengba4fc8b2009-06-01 13:00:29 -07003193 /* r0 now contains this->clazz */
Bill Buzbee1465db52009-09-23 17:17:35 -07003194 genRegCopy(cUnit, r0, r3);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003195
3196 /* r1 = BBBB */
3197 loadConstant(cUnit, r1, dInsn->vB);
3198
3199 /* r2 = method (caller) */
3200 loadConstant(cUnit, r2, (int) cUnit->method);
3201
3202 /* r3 = pDvmDex */
3203 loadConstant(cUnit, r3, (int) cUnit->method->clazz->pDvmDex);
3204
Ben Chengbd1326d2010-04-02 15:04:53 -07003205 LOAD_FUNC_ADDR(cUnit, r7,
3206 (intptr_t) dvmFindInterfaceMethodInCache);
Bill Buzbee1465db52009-09-23 17:17:35 -07003207 opReg(cUnit, kOpBlx, r7);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003208 /* r0 = calleeMethod (returned from dvmFindInterfaceMethodInCache */
3209
Ben Cheng09e50c92010-05-02 10:45:32 -07003210 dvmCompilerClobberCallRegs(cUnit);
3211 /* generate a branch over if the interface method is resolved */
buzbee8f8109a2010-08-31 10:16:35 -07003212 ArmLIR *branchOver = genCmpImmBranch(cUnit, kArmCondNe, r0, 0);
Ben Cheng09e50c92010-05-02 10:45:32 -07003213 /*
3214 * calleeMethod == NULL -> throw
3215 */
3216 loadConstant(cUnit, r0,
3217 (int) (cUnit->method->insns + mir->offset));
3218 genDispatchToHandler(cUnit, TEMPLATE_THROW_EXCEPTION_COMMON);
3219 /* noreturn */
3220
3221 ArmLIR *target = newLIR0(cUnit, kArmPseudoTargetLabel);
3222 target->defMask = ENCODE_ALL;
3223 branchOver->generic.target = (LIR *) target;
3224
Bill Buzbee1465db52009-09-23 17:17:35 -07003225 genRegCopy(cUnit, r1, r8);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003226
Ben Cheng38329f52009-07-07 14:19:20 -07003227 /* Check if rechain limit is reached */
buzbee8f8109a2010-08-31 10:16:35 -07003228 ArmLIR *bypassRechaining = genCmpImmBranch(cUnit, kArmCondGt,
3229 r1, 0);
Ben Cheng38329f52009-07-07 14:19:20 -07003230
Ben Chengaf5aa1f2011-01-04 15:37:04 -08003231 LOAD_FUNC_ADDR(cUnit, r7, (int) dvmJitToPatchPredictedChain);
Ben Cheng38329f52009-07-07 14:19:20 -07003232
buzbee9f601a92011-02-11 17:48:20 -08003233 genRegCopy(cUnit, r1, rSELF);
Bill Buzbee1465db52009-09-23 17:17:35 -07003234 genRegCopy(cUnit, r2, r9);
3235 genRegCopy(cUnit, r3, r10);
Ben Cheng38329f52009-07-07 14:19:20 -07003236
3237 /*
3238 * r0 = calleeMethod
3239 * r2 = &predictedChainingCell
3240 * r3 = class
3241 *
3242 * &returnChainingCell has been loaded into r1 but is not needed
3243 * when patching the chaining cell and will be clobbered upon
3244 * returning so it will be reconstructed again.
3245 */
Bill Buzbee1465db52009-09-23 17:17:35 -07003246 opReg(cUnit, kOpBlx, r7);
Ben Cheng38329f52009-07-07 14:19:20 -07003247
3248 /* r1 = &retChainingCell */
Bill Buzbee1465db52009-09-23 17:17:35 -07003249 addrRetChain = opRegRegImm(cUnit, kOpAdd, r1, rpc, 0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003250 addrRetChain->generic.target = (LIR *) retChainingCell;
Ben Cheng38329f52009-07-07 14:19:20 -07003251
3252 bypassRechaining->generic.target = (LIR *) addrRetChain;
3253
Ben Chengba4fc8b2009-06-01 13:00:29 -07003254 /*
3255 * r0 = this, r1 = calleeMethod,
3256 * r1 = &ChainingCell,
3257 * r4PC = callsiteDPC,
3258 */
buzbee18fba342011-01-19 15:31:15 -08003259 genDispatchToHandler(cUnit, gDvmJit.methodTraceSupport ?
3260 TEMPLATE_INVOKE_METHOD_NO_OPT_PROF :
3261 TEMPLATE_INVOKE_METHOD_NO_OPT);
Ben Cheng978738d2010-05-13 13:45:57 -07003262#if defined(WITH_JIT_TUNING)
Ben Cheng86717f72010-03-05 15:27:21 -08003263 gDvmJit.invokePolymorphic++;
Ben Chengba4fc8b2009-06-01 13:00:29 -07003264#endif
3265 /* Handle exceptions using the interpreter */
3266 genTrap(cUnit, mir->offset, pcrLabel);
3267 break;
3268 }
Andy McFadden750d1102011-02-11 15:26:10 -08003269 case OP_INVOKE_OBJECT_INIT: {
Andy McFadden6af2ddd2011-02-16 16:50:40 -08003270 genInterpSingleStep(cUnit, mir);
buzbee18fba342011-01-19 15:31:15 -08003271 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07003272 }
3273 case OP_FILLED_NEW_ARRAY:
jeffhao71eee1f2011-01-04 14:18:54 -08003274 case OP_FILLED_NEW_ARRAY_RANGE:
3275 case OP_FILLED_NEW_ARRAY_JUMBO: {
Ben Chengba4fc8b2009-06-01 13:00:29 -07003276 /* Just let the interpreter deal with these */
3277 genInterpSingleStep(cUnit, mir);
3278 break;
3279 }
3280 default:
3281 return true;
3282 }
3283 return false;
3284}
3285
Ben Chengcfdeca32011-01-14 11:36:46 -08003286/* "this" pointer is already in r0 */
3287static void genValidationForMethodCallee(CompilationUnit *cUnit, MIR *mir,
3288 ArmLIR **classCheck)
3289{
3290 CallsiteInfo *callsiteInfo = mir->meta.callsiteInfo;
3291 dvmCompilerLockAllTemps(cUnit);
3292
3293 loadConstant(cUnit, r1, (int) callsiteInfo->clazz);
3294
3295 loadWordDisp(cUnit, r0, offsetof(Object, clazz), r2);
3296 /* Branch to the slow path if classes are not equal */
3297 opRegReg(cUnit, kOpCmp, r1, r2);
3298 /*
3299 * Set the misPredBranchOver target so that it will be generated when the
3300 * code for the non-optimized invoke is generated.
3301 */
3302 *classCheck = opCondBranch(cUnit, kArmCondNe);
3303}
3304
Ben Chengba4fc8b2009-06-01 13:00:29 -07003305static bool handleFmt35ms_3rms(CompilationUnit *cUnit, MIR *mir,
Bill Buzbee89efc3d2009-07-28 11:22:22 -07003306 BasicBlock *bb, ArmLIR *labelList)
Ben Chengba4fc8b2009-06-01 13:00:29 -07003307{
Bill Buzbee89efc3d2009-07-28 11:22:22 -07003308 ArmLIR *pcrLabel = NULL;
Ben Chengba4fc8b2009-06-01 13:00:29 -07003309
Ben Cheng7a2697d2010-06-07 13:44:23 -07003310 /* An invoke with the MIR_INLINED is effectively a no-op */
3311 if (mir->OptimizationFlags & MIR_INLINED)
3312 return false;
3313
Ben Chengba4fc8b2009-06-01 13:00:29 -07003314 DecodedInstruction *dInsn = &mir->dalvikInsn;
Dan Bornstein9a1f8162010-12-01 17:02:26 -08003315 switch (mir->dalvikInsn.opcode) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07003316 /* calleeMethod = this->clazz->vtable[BBBB] */
3317 case OP_INVOKE_VIRTUAL_QUICK_RANGE:
3318 case OP_INVOKE_VIRTUAL_QUICK: {
3319 int methodIndex = dInsn->vB;
Bill Buzbeea8589332010-12-27 09:31:21 -08003320 ArmLIR *retChainingCell = &labelList[bb->fallThrough->id];
3321 ArmLIR *predChainingCell = &labelList[bb->taken->id];
Ben Cheng7a2697d2010-06-07 13:44:23 -07003322
3323 /*
3324 * If the invoke has non-null misPredBranchOver, we need to generate
3325 * the non-inlined version of the invoke here to handle the
3326 * mispredicted case.
3327 */
3328 if (mir->meta.callsiteInfo->misPredBranchOver) {
3329 genLandingPadForMispredictedCallee(cUnit, mir, bb, labelList);
3330 }
3331
Dan Bornstein9a1f8162010-12-01 17:02:26 -08003332 if (mir->dalvikInsn.opcode == OP_INVOKE_VIRTUAL_QUICK)
Ben Chengba4fc8b2009-06-01 13:00:29 -07003333 genProcessArgsNoRange(cUnit, mir, dInsn, &pcrLabel);
3334 else
3335 genProcessArgsRange(cUnit, mir, dInsn, &pcrLabel);
3336
Ben Chengcfdeca32011-01-14 11:36:46 -08003337
3338 if (mir->OptimizationFlags & MIR_INVOKE_METHOD_JIT) {
3339 const Method *calleeMethod = mir->meta.callsiteInfo->method;
3340 void *calleeAddr = dvmJitGetMethodAddr(calleeMethod->insns);
3341 if (calleeAddr) {
3342 ArmLIR *classCheck;
3343 cUnit->printMe = true;
3344 genValidationForMethodCallee(cUnit, mir, &classCheck);
3345 newLIR2(cUnit, kThumbBl1, (int) calleeAddr,
3346 (int) calleeAddr);
3347 newLIR2(cUnit, kThumbBl2, (int) calleeAddr,
3348 (int) calleeAddr);
3349 genUnconditionalBranch(cUnit, retChainingCell);
3350
3351 /* Target of slow path */
3352 ArmLIR *slowPathLabel = newLIR0(cUnit,
3353 kArmPseudoTargetLabel);
3354
3355 slowPathLabel->defMask = ENCODE_ALL;
3356 classCheck->generic.target = (LIR *) slowPathLabel;
3357 }
3358 }
3359
Ben Cheng38329f52009-07-07 14:19:20 -07003360 genInvokeVirtualCommon(cUnit, mir, methodIndex,
3361 retChainingCell,
3362 predChainingCell,
3363 pcrLabel);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003364 break;
3365 }
3366 /* calleeMethod = method->clazz->super->vtable[BBBB] */
3367 case OP_INVOKE_SUPER_QUICK:
3368 case OP_INVOKE_SUPER_QUICK_RANGE: {
Ben Cheng7a2697d2010-06-07 13:44:23 -07003369 /* Grab the method ptr directly from what the interpreter sees */
3370 const Method *calleeMethod = mir->meta.callsiteInfo->method;
3371 assert(calleeMethod ==
3372 cUnit->method->clazz->super->vtable[dInsn->vB]);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003373
Dan Bornstein9a1f8162010-12-01 17:02:26 -08003374 if (mir->dalvikInsn.opcode == OP_INVOKE_SUPER_QUICK)
Ben Chengba4fc8b2009-06-01 13:00:29 -07003375 genProcessArgsNoRange(cUnit, mir, dInsn, &pcrLabel);
3376 else
3377 genProcessArgsRange(cUnit, mir, dInsn, &pcrLabel);
3378
3379 /* r0 = calleeMethod */
3380 loadConstant(cUnit, r0, (int) calleeMethod);
3381
Ben Cheng38329f52009-07-07 14:19:20 -07003382 genInvokeSingletonCommon(cUnit, mir, bb, labelList, pcrLabel,
3383 calleeMethod);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003384 break;
3385 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07003386 default:
3387 return true;
3388 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07003389 return false;
3390}
3391
3392/*
Bill Buzbeefd023aa2009-11-02 09:23:49 -08003393 * This operation is complex enough that we'll do it partly inline
3394 * and partly with a handler. NOTE: the handler uses hardcoded
3395 * values for string object offsets and must be revisitied if the
3396 * layout changes.
3397 */
3398static bool genInlinedCompareTo(CompilationUnit *cUnit, MIR *mir)
3399{
3400#if defined(USE_GLOBAL_STRING_DEFS)
Elliott Hughes7e914f12011-01-19 18:18:42 -08003401 return handleExecuteInlineC(cUnit, mir);
Bill Buzbeefd023aa2009-11-02 09:23:49 -08003402#else
3403 ArmLIR *rollback;
Bill Buzbeec6f10662010-02-09 11:16:15 -08003404 RegLocation rlThis = dvmCompilerGetSrc(cUnit, mir, 0);
3405 RegLocation rlComp = dvmCompilerGetSrc(cUnit, mir, 1);
Bill Buzbeefd023aa2009-11-02 09:23:49 -08003406
3407 loadValueDirectFixed(cUnit, rlThis, r0);
3408 loadValueDirectFixed(cUnit, rlComp, r1);
3409 /* Test objects for NULL */
3410 rollback = genNullCheck(cUnit, rlThis.sRegLow, r0, mir->offset, NULL);
3411 genNullCheck(cUnit, rlComp.sRegLow, r1, mir->offset, rollback);
3412 /*
3413 * TUNING: we could check for object pointer equality before invoking
3414 * handler. Unclear whether the gain would be worth the added code size
3415 * expansion.
3416 */
3417 genDispatchToHandler(cUnit, TEMPLATE_STRING_COMPARETO);
Bill Buzbeec6f10662010-02-09 11:16:15 -08003418 storeValue(cUnit, inlinedTarget(cUnit, mir, false),
3419 dvmCompilerGetReturn(cUnit));
Elliott Hughes7e914f12011-01-19 18:18:42 -08003420 return false;
Bill Buzbeefd023aa2009-11-02 09:23:49 -08003421#endif
3422}
3423
Elliott Hughes2bdbcb62010-04-12 14:29:37 -07003424static bool genInlinedFastIndexOf(CompilationUnit *cUnit, MIR *mir)
Bill Buzbeefd023aa2009-11-02 09:23:49 -08003425{
3426#if defined(USE_GLOBAL_STRING_DEFS)
Elliott Hughes7e914f12011-01-19 18:18:42 -08003427 return handleExecuteInlineC(cUnit, mir);
Bill Buzbeefd023aa2009-11-02 09:23:49 -08003428#else
Bill Buzbeec6f10662010-02-09 11:16:15 -08003429 RegLocation rlThis = dvmCompilerGetSrc(cUnit, mir, 0);
3430 RegLocation rlChar = dvmCompilerGetSrc(cUnit, mir, 1);
Bill Buzbeefd023aa2009-11-02 09:23:49 -08003431
3432 loadValueDirectFixed(cUnit, rlThis, r0);
3433 loadValueDirectFixed(cUnit, rlChar, r1);
Elliott Hughes2bdbcb62010-04-12 14:29:37 -07003434 RegLocation rlStart = dvmCompilerGetSrc(cUnit, mir, 2);
3435 loadValueDirectFixed(cUnit, rlStart, r2);
Bill Buzbeefd023aa2009-11-02 09:23:49 -08003436 /* Test objects for NULL */
3437 genNullCheck(cUnit, rlThis.sRegLow, r0, mir->offset, NULL);
3438 genDispatchToHandler(cUnit, TEMPLATE_STRING_INDEXOF);
Bill Buzbeec6f10662010-02-09 11:16:15 -08003439 storeValue(cUnit, inlinedTarget(cUnit, mir, false),
3440 dvmCompilerGetReturn(cUnit));
Elliott Hughes7e914f12011-01-19 18:18:42 -08003441 return false;
Bill Buzbeefd023aa2009-11-02 09:23:49 -08003442#endif
3443}
3444
Elliott Hughesee34f592010-04-05 18:13:52 -07003445// Generates an inlined String.isEmpty or String.length.
3446static bool genInlinedStringIsEmptyOrLength(CompilationUnit *cUnit, MIR *mir,
3447 bool isEmpty)
Bill Buzbee1f748632010-03-02 16:14:41 -08003448{
Elliott Hughesee34f592010-04-05 18:13:52 -07003449 // dst = src.length();
Bill Buzbee1f748632010-03-02 16:14:41 -08003450 RegLocation rlObj = dvmCompilerGetSrc(cUnit, mir, 0);
3451 RegLocation rlDest = inlinedTarget(cUnit, mir, false);
3452 rlObj = loadValue(cUnit, rlObj, kCoreReg);
3453 RegLocation rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
3454 genNullCheck(cUnit, rlObj.sRegLow, rlObj.lowReg, mir->offset, NULL);
3455 loadWordDisp(cUnit, rlObj.lowReg, gDvm.offJavaLangString_count,
3456 rlResult.lowReg);
Elliott Hughesee34f592010-04-05 18:13:52 -07003457 if (isEmpty) {
3458 // dst = (dst == 0);
3459 int tReg = dvmCompilerAllocTemp(cUnit);
3460 opRegReg(cUnit, kOpNeg, tReg, rlResult.lowReg);
3461 opRegRegReg(cUnit, kOpAdc, rlResult.lowReg, rlResult.lowReg, tReg);
3462 }
Bill Buzbee1f748632010-03-02 16:14:41 -08003463 storeValue(cUnit, rlDest, rlResult);
3464 return false;
3465}
3466
Elliott Hughesee34f592010-04-05 18:13:52 -07003467static bool genInlinedStringLength(CompilationUnit *cUnit, MIR *mir)
3468{
3469 return genInlinedStringIsEmptyOrLength(cUnit, mir, false);
3470}
3471
3472static bool genInlinedStringIsEmpty(CompilationUnit *cUnit, MIR *mir)
3473{
3474 return genInlinedStringIsEmptyOrLength(cUnit, mir, true);
3475}
3476
Bill Buzbee1f748632010-03-02 16:14:41 -08003477static bool genInlinedStringCharAt(CompilationUnit *cUnit, MIR *mir)
3478{
3479 int contents = offsetof(ArrayObject, contents);
3480 RegLocation rlObj = dvmCompilerGetSrc(cUnit, mir, 0);
3481 RegLocation rlIdx = dvmCompilerGetSrc(cUnit, mir, 1);
3482 RegLocation rlDest = inlinedTarget(cUnit, mir, false);
3483 RegLocation rlResult;
3484 rlObj = loadValue(cUnit, rlObj, kCoreReg);
3485 rlIdx = loadValue(cUnit, rlIdx, kCoreReg);
3486 int regMax = dvmCompilerAllocTemp(cUnit);
3487 int regOff = dvmCompilerAllocTemp(cUnit);
3488 int regPtr = dvmCompilerAllocTemp(cUnit);
3489 ArmLIR *pcrLabel = genNullCheck(cUnit, rlObj.sRegLow, rlObj.lowReg,
3490 mir->offset, NULL);
3491 loadWordDisp(cUnit, rlObj.lowReg, gDvm.offJavaLangString_count, regMax);
3492 loadWordDisp(cUnit, rlObj.lowReg, gDvm.offJavaLangString_offset, regOff);
3493 loadWordDisp(cUnit, rlObj.lowReg, gDvm.offJavaLangString_value, regPtr);
3494 genBoundsCheck(cUnit, rlIdx.lowReg, regMax, mir->offset, pcrLabel);
3495 dvmCompilerFreeTemp(cUnit, regMax);
3496 opRegImm(cUnit, kOpAdd, regPtr, contents);
3497 opRegReg(cUnit, kOpAdd, regOff, rlIdx.lowReg);
3498 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
3499 loadBaseIndexed(cUnit, regPtr, regOff, rlResult.lowReg, 1, kUnsignedHalf);
3500 storeValue(cUnit, rlDest, rlResult);
3501 return false;
3502}
3503
3504static bool genInlinedAbsInt(CompilationUnit *cUnit, MIR *mir)
3505{
3506 RegLocation rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
3507 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
Elliott Hughese22bd842010-08-20 18:47:36 -07003508 RegLocation rlDest = inlinedTarget(cUnit, mir, false);
Bill Buzbee1f748632010-03-02 16:14:41 -08003509 RegLocation rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
3510 int signReg = dvmCompilerAllocTemp(cUnit);
3511 /*
3512 * abs(x) = y<=x>>31, (x+y)^y.
3513 * Thumb2's IT block also yields 3 instructions, but imposes
3514 * scheduling constraints.
3515 */
3516 opRegRegImm(cUnit, kOpAsr, signReg, rlSrc.lowReg, 31);
3517 opRegRegReg(cUnit, kOpAdd, rlResult.lowReg, rlSrc.lowReg, signReg);
3518 opRegReg(cUnit, kOpXor, rlResult.lowReg, signReg);
3519 storeValue(cUnit, rlDest, rlResult);
3520 return false;
3521}
3522
3523static bool genInlinedAbsLong(CompilationUnit *cUnit, MIR *mir)
3524{
3525 RegLocation rlSrc = dvmCompilerGetSrcWide(cUnit, mir, 0, 1);
3526 RegLocation rlDest = inlinedTargetWide(cUnit, mir, false);
3527 rlSrc = loadValueWide(cUnit, rlSrc, kCoreReg);
3528 RegLocation rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
3529 int signReg = dvmCompilerAllocTemp(cUnit);
3530 /*
3531 * abs(x) = y<=x>>31, (x+y)^y.
3532 * Thumb2 IT block allows slightly shorter sequence,
3533 * but introduces a scheduling barrier. Stick with this
3534 * mechanism for now.
3535 */
3536 opRegRegImm(cUnit, kOpAsr, signReg, rlSrc.highReg, 31);
3537 opRegRegReg(cUnit, kOpAdd, rlResult.lowReg, rlSrc.lowReg, signReg);
3538 opRegRegReg(cUnit, kOpAdc, rlResult.highReg, rlSrc.highReg, signReg);
3539 opRegReg(cUnit, kOpXor, rlResult.lowReg, signReg);
3540 opRegReg(cUnit, kOpXor, rlResult.highReg, signReg);
3541 storeValueWide(cUnit, rlDest, rlResult);
3542 return false;
3543}
Bill Buzbeefd023aa2009-11-02 09:23:49 -08003544
Elliott Hughese22bd842010-08-20 18:47:36 -07003545static bool genInlinedIntFloatConversion(CompilationUnit *cUnit, MIR *mir)
3546{
3547 // Just move from source to destination...
3548 RegLocation rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
3549 RegLocation rlDest = inlinedTarget(cUnit, mir, false);
3550 storeValue(cUnit, rlDest, rlSrc);
3551 return false;
3552}
3553
3554static bool genInlinedLongDoubleConversion(CompilationUnit *cUnit, MIR *mir)
3555{
3556 // Just move from source to destination...
3557 RegLocation rlSrc = dvmCompilerGetSrcWide(cUnit, mir, 0, 1);
3558 RegLocation rlDest = inlinedTargetWide(cUnit, mir, false);
3559 storeValueWide(cUnit, rlDest, rlSrc);
3560 return false;
3561}
3562
Bill Buzbeefd023aa2009-11-02 09:23:49 -08003563/*
Elliott Hughes7e914f12011-01-19 18:18:42 -08003564 * JITs a call to a C function.
3565 * TODO: use this for faster native method invocation for simple native
3566 * methods (http://b/3069458).
3567 */
3568static bool handleExecuteInlineC(CompilationUnit *cUnit, MIR *mir)
3569{
3570 DecodedInstruction *dInsn = &mir->dalvikInsn;
3571 int operation = dInsn->vB;
3572 unsigned int i;
3573 const InlineOperation* inLineTable = dvmGetInlineOpsTable();
3574 uintptr_t fn = (int) inLineTable[operation].func;
3575 if (fn == 0) {
3576 dvmCompilerAbort(cUnit);
3577 }
3578 dvmCompilerFlushAllRegs(cUnit); /* Everything to home location */
3579 dvmCompilerClobberCallRegs(cUnit);
3580 dvmCompilerClobber(cUnit, r4PC);
3581 dvmCompilerClobber(cUnit, r7);
buzbee9f601a92011-02-11 17:48:20 -08003582 int offset = offsetof(Thread, retval);
3583 opRegRegImm(cUnit, kOpAdd, r4PC, rSELF, offset);
Elliott Hughes7e914f12011-01-19 18:18:42 -08003584 opImm(cUnit, kOpPush, (1<<r4PC) | (1<<r7));
3585 LOAD_FUNC_ADDR(cUnit, r4PC, fn);
3586 genExportPC(cUnit, mir);
3587 for (i=0; i < dInsn->vA; i++) {
3588 loadValueDirect(cUnit, dvmCompilerGetSrc(cUnit, mir, i), i);
3589 }
3590 opReg(cUnit, kOpBlx, r4PC);
3591 opRegImm(cUnit, kOpAdd, r13, 8);
3592 /* NULL? */
3593 ArmLIR *branchOver = genCmpImmBranch(cUnit, kArmCondNe, r0, 0);
3594 loadConstant(cUnit, r0, (int) (cUnit->method->insns + mir->offset));
3595 genDispatchToHandler(cUnit, TEMPLATE_THROW_EXCEPTION_COMMON);
3596 ArmLIR *target = newLIR0(cUnit, kArmPseudoTargetLabel);
3597 target->defMask = ENCODE_ALL;
3598 branchOver->generic.target = (LIR *) target;
3599 return false;
3600}
3601
3602/*
Bill Buzbeece46c942009-11-20 15:41:34 -08003603 * NOTE: Handles both range and non-range versions (arguments
3604 * have already been normalized by this point).
Ben Chengba4fc8b2009-06-01 13:00:29 -07003605 */
Bill Buzbeece46c942009-11-20 15:41:34 -08003606static bool handleExecuteInline(CompilationUnit *cUnit, MIR *mir)
Ben Chengba4fc8b2009-06-01 13:00:29 -07003607{
3608 DecodedInstruction *dInsn = &mir->dalvikInsn;
Elliott Hughes7e914f12011-01-19 18:18:42 -08003609 assert(dInsn->opcode == OP_EXECUTE_INLINE_RANGE ||
3610 dInsn->opcode == OP_EXECUTE_INLINE);
3611 switch (dInsn->vB) {
3612 case INLINE_EMPTYINLINEMETHOD:
3613 return false; /* Nop */
3614
3615 /* These ones we potentially JIT inline. */
3616 case INLINE_STRING_LENGTH:
3617 return genInlinedStringLength(cUnit, mir);
3618 case INLINE_STRING_IS_EMPTY:
3619 return genInlinedStringIsEmpty(cUnit, mir);
3620 case INLINE_MATH_ABS_INT:
3621 return genInlinedAbsInt(cUnit, mir);
3622 case INLINE_MATH_ABS_LONG:
3623 return genInlinedAbsLong(cUnit, mir);
3624 case INLINE_MATH_MIN_INT:
3625 return genInlinedMinMaxInt(cUnit, mir, true);
3626 case INLINE_MATH_MAX_INT:
3627 return genInlinedMinMaxInt(cUnit, mir, false);
3628 case INLINE_STRING_CHARAT:
3629 return genInlinedStringCharAt(cUnit, mir);
3630 case INLINE_MATH_SQRT:
3631 return genInlineSqrt(cUnit, mir);
3632 case INLINE_MATH_ABS_FLOAT:
3633 return genInlinedAbsFloat(cUnit, mir);
3634 case INLINE_MATH_ABS_DOUBLE:
3635 return genInlinedAbsDouble(cUnit, mir);
3636 case INLINE_STRING_COMPARETO:
3637 return genInlinedCompareTo(cUnit, mir);
3638 case INLINE_STRING_FASTINDEXOF_II:
3639 return genInlinedFastIndexOf(cUnit, mir);
3640 case INLINE_FLOAT_TO_RAW_INT_BITS:
3641 case INLINE_INT_BITS_TO_FLOAT:
3642 return genInlinedIntFloatConversion(cUnit, mir);
3643 case INLINE_DOUBLE_TO_RAW_LONG_BITS:
3644 case INLINE_LONG_BITS_TO_DOUBLE:
3645 return genInlinedLongDoubleConversion(cUnit, mir);
3646
3647 /*
3648 * These ones we just JIT a call to a C function for.
3649 * TODO: special-case these in the other "invoke" call paths.
3650 */
3651 case INLINE_STRING_EQUALS:
3652 case INLINE_MATH_COS:
3653 case INLINE_MATH_SIN:
3654 case INLINE_FLOAT_TO_INT_BITS:
3655 case INLINE_DOUBLE_TO_LONG_BITS:
3656 return handleExecuteInlineC(cUnit, mir);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003657 }
Elliott Hughes7e914f12011-01-19 18:18:42 -08003658 dvmCompilerAbort(cUnit);
3659 return false; // Not reachable; keeps compiler happy.
Ben Chengba4fc8b2009-06-01 13:00:29 -07003660}
3661
3662static bool handleFmt51l(CompilationUnit *cUnit, MIR *mir)
3663{
Bill Buzbee1465db52009-09-23 17:17:35 -07003664 //TUNING: We're using core regs here - not optimal when target is a double
Bill Buzbeec6f10662010-02-09 11:16:15 -08003665 RegLocation rlDest = dvmCompilerGetDestWide(cUnit, mir, 0, 1);
3666 RegLocation rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Ben Chengbd1326d2010-04-02 15:04:53 -07003667 loadConstantNoClobber(cUnit, rlResult.lowReg,
3668 mir->dalvikInsn.vB_wide & 0xFFFFFFFFUL);
3669 loadConstantNoClobber(cUnit, rlResult.highReg,
3670 (mir->dalvikInsn.vB_wide>>32) & 0xFFFFFFFFUL);
Bill Buzbee1465db52009-09-23 17:17:35 -07003671 storeValueWide(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003672 return false;
3673}
3674
Ben Chengba4fc8b2009-06-01 13:00:29 -07003675/*
3676 * The following are special processing routines that handle transfer of
3677 * controls between compiled code and the interpreter. Certain VM states like
3678 * Dalvik PC and special-purpose registers are reconstructed here.
3679 */
3680
Bill Buzbeebd047242010-05-13 13:02:53 -07003681/*
3682 * Insert a
3683 * b .+4
3684 * nop
3685 * pair at the beginning of a chaining cell. This serves as the
3686 * switch branch that selects between reverting to the interpreter or
3687 * not. Once the cell is chained to a translation, the cell will
3688 * contain a 32-bit branch. Subsequent chain/unchain operations will
3689 * then only alter that first 16-bits - the "b .+4" for unchaining,
3690 * and the restoration of the first half of the 32-bit branch for
3691 * rechaining.
3692 */
3693static void insertChainingSwitch(CompilationUnit *cUnit)
3694{
3695 ArmLIR *branch = newLIR0(cUnit, kThumbBUncond);
3696 newLIR2(cUnit, kThumbOrr, r0, r0);
3697 ArmLIR *target = newLIR0(cUnit, kArmPseudoTargetLabel);
3698 target->defMask = ENCODE_ALL;
3699 branch->generic.target = (LIR *) target;
3700}
3701
Ben Cheng1efc9c52009-06-08 18:25:27 -07003702/* Chaining cell for code that may need warmup. */
3703static void handleNormalChainingCell(CompilationUnit *cUnit,
3704 unsigned int offset)
Ben Chengba4fc8b2009-06-01 13:00:29 -07003705{
Ben Cheng11d8f142010-03-24 15:24:19 -07003706 /*
3707 * Use raw instruction constructors to guarantee that the generated
3708 * instructions fit the predefined cell size.
3709 */
Bill Buzbeebd047242010-05-13 13:02:53 -07003710 insertChainingSwitch(cUnit);
buzbee9f601a92011-02-11 17:48:20 -08003711 newLIR3(cUnit, kThumbLdrRRI5, r0, rSELF,
3712 offsetof(Thread,
Ben Cheng11d8f142010-03-24 15:24:19 -07003713 jitToInterpEntries.dvmJitToInterpNormal) >> 2);
3714 newLIR1(cUnit, kThumbBlxR, r0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003715 addWordData(cUnit, (int) (cUnit->method->insns + offset), true);
3716}
3717
3718/*
Ben Cheng1efc9c52009-06-08 18:25:27 -07003719 * Chaining cell for instructions that immediately following already translated
3720 * code.
Ben Chengba4fc8b2009-06-01 13:00:29 -07003721 */
Ben Cheng1efc9c52009-06-08 18:25:27 -07003722static void handleHotChainingCell(CompilationUnit *cUnit,
3723 unsigned int offset)
Ben Chengba4fc8b2009-06-01 13:00:29 -07003724{
Ben Cheng11d8f142010-03-24 15:24:19 -07003725 /*
3726 * Use raw instruction constructors to guarantee that the generated
3727 * instructions fit the predefined cell size.
3728 */
Bill Buzbeebd047242010-05-13 13:02:53 -07003729 insertChainingSwitch(cUnit);
buzbee9f601a92011-02-11 17:48:20 -08003730 newLIR3(cUnit, kThumbLdrRRI5, r0, rSELF,
3731 offsetof(Thread,
Ben Cheng11d8f142010-03-24 15:24:19 -07003732 jitToInterpEntries.dvmJitToInterpTraceSelect) >> 2);
3733 newLIR1(cUnit, kThumbBlxR, r0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003734 addWordData(cUnit, (int) (cUnit->method->insns + offset), true);
3735}
3736
Jeff Hao97319a82009-08-12 16:57:15 -07003737/* Chaining cell for branches that branch back into the same basic block */
3738static void handleBackwardBranchChainingCell(CompilationUnit *cUnit,
3739 unsigned int offset)
3740{
Ben Cheng11d8f142010-03-24 15:24:19 -07003741 /*
3742 * Use raw instruction constructors to guarantee that the generated
3743 * instructions fit the predefined cell size.
3744 */
Bill Buzbeebd047242010-05-13 13:02:53 -07003745 insertChainingSwitch(cUnit);
Bill Buzbee9c4b7c82009-09-10 10:10:38 -07003746#if defined(WITH_SELF_VERIFICATION)
buzbee9f601a92011-02-11 17:48:20 -08003747 newLIR3(cUnit, kThumbLdrRRI5, r0, rSELF,
3748 offsetof(Thread,
Ben Cheng40094c12010-02-24 20:58:44 -08003749 jitToInterpEntries.dvmJitToInterpBackwardBranch) >> 2);
Bill Buzbee9c4b7c82009-09-10 10:10:38 -07003750#else
buzbee9f601a92011-02-11 17:48:20 -08003751 newLIR3(cUnit, kThumbLdrRRI5, r0, rSELF,
3752 offsetof(Thread, jitToInterpEntries.dvmJitToInterpNormal) >> 2);
Bill Buzbee9c4b7c82009-09-10 10:10:38 -07003753#endif
Bill Buzbee1465db52009-09-23 17:17:35 -07003754 newLIR1(cUnit, kThumbBlxR, r0);
Jeff Hao97319a82009-08-12 16:57:15 -07003755 addWordData(cUnit, (int) (cUnit->method->insns + offset), true);
3756}
3757
Ben Chengba4fc8b2009-06-01 13:00:29 -07003758/* Chaining cell for monomorphic method invocations. */
Ben Cheng38329f52009-07-07 14:19:20 -07003759static void handleInvokeSingletonChainingCell(CompilationUnit *cUnit,
3760 const Method *callee)
Ben Chengba4fc8b2009-06-01 13:00:29 -07003761{
Ben Cheng11d8f142010-03-24 15:24:19 -07003762 /*
3763 * Use raw instruction constructors to guarantee that the generated
3764 * instructions fit the predefined cell size.
3765 */
Bill Buzbeebd047242010-05-13 13:02:53 -07003766 insertChainingSwitch(cUnit);
buzbee9f601a92011-02-11 17:48:20 -08003767 newLIR3(cUnit, kThumbLdrRRI5, r0, rSELF,
3768 offsetof(Thread,
Ben Cheng11d8f142010-03-24 15:24:19 -07003769 jitToInterpEntries.dvmJitToInterpTraceSelect) >> 2);
3770 newLIR1(cUnit, kThumbBlxR, r0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003771 addWordData(cUnit, (int) (callee->insns), true);
3772}
3773
Ben Cheng38329f52009-07-07 14:19:20 -07003774/* Chaining cell for monomorphic method invocations. */
3775static void handleInvokePredictedChainingCell(CompilationUnit *cUnit)
3776{
3777
3778 /* Should not be executed in the initial state */
3779 addWordData(cUnit, PREDICTED_CHAIN_BX_PAIR_INIT, true);
3780 /* To be filled: class */
3781 addWordData(cUnit, PREDICTED_CHAIN_CLAZZ_INIT, true);
3782 /* To be filled: method */
3783 addWordData(cUnit, PREDICTED_CHAIN_METHOD_INIT, true);
3784 /*
3785 * Rechain count. The initial value of 0 here will trigger chaining upon
3786 * the first invocation of this callsite.
3787 */
3788 addWordData(cUnit, PREDICTED_CHAIN_COUNTER_INIT, true);
3789}
3790
Ben Chengba4fc8b2009-06-01 13:00:29 -07003791/* Load the Dalvik PC into r0 and jump to the specified target */
3792static void handlePCReconstruction(CompilationUnit *cUnit,
Bill Buzbee89efc3d2009-07-28 11:22:22 -07003793 ArmLIR *targetLabel)
Ben Chengba4fc8b2009-06-01 13:00:29 -07003794{
Bill Buzbee89efc3d2009-07-28 11:22:22 -07003795 ArmLIR **pcrLabel =
3796 (ArmLIR **) cUnit->pcReconstructionList.elemList;
Ben Chengba4fc8b2009-06-01 13:00:29 -07003797 int numElems = cUnit->pcReconstructionList.numUsed;
3798 int i;
3799 for (i = 0; i < numElems; i++) {
3800 dvmCompilerAppendLIR(cUnit, (LIR *) pcrLabel[i]);
3801 /* r0 = dalvik PC */
3802 loadConstant(cUnit, r0, pcrLabel[i]->operands[0]);
3803 genUnconditionalBranch(cUnit, targetLabel);
3804 }
3805}
3806
Bill Buzbee1465db52009-09-23 17:17:35 -07003807static char *extendedMIROpNames[kMirOpLast - kMirOpFirst] = {
3808 "kMirOpPhi",
3809 "kMirOpNullNRangeUpCheck",
3810 "kMirOpNullNRangeDownCheck",
3811 "kMirOpLowerBound",
3812 "kMirOpPunt",
Ben Cheng7a2697d2010-06-07 13:44:23 -07003813 "kMirOpCheckInlinePrediction",
Ben Cheng4238ec22009-08-24 16:32:22 -07003814};
3815
3816/*
3817 * vA = arrayReg;
3818 * vB = idxReg;
3819 * vC = endConditionReg;
3820 * arg[0] = maxC
3821 * arg[1] = minC
3822 * arg[2] = loopBranchConditionCode
3823 */
3824static void genHoistedChecksForCountUpLoop(CompilationUnit *cUnit, MIR *mir)
3825{
Bill Buzbee1465db52009-09-23 17:17:35 -07003826 /*
3827 * NOTE: these synthesized blocks don't have ssa names assigned
3828 * for Dalvik registers. However, because they dominate the following
3829 * blocks we can simply use the Dalvik name w/ subscript 0 as the
3830 * ssa name.
3831 */
Ben Cheng4238ec22009-08-24 16:32:22 -07003832 DecodedInstruction *dInsn = &mir->dalvikInsn;
3833 const int lenOffset = offsetof(ArrayObject, length);
Ben Cheng4238ec22009-08-24 16:32:22 -07003834 const int maxC = dInsn->arg[0];
Bill Buzbee1465db52009-09-23 17:17:35 -07003835 int regLength;
3836 RegLocation rlArray = cUnit->regLocation[mir->dalvikInsn.vA];
3837 RegLocation rlIdxEnd = cUnit->regLocation[mir->dalvikInsn.vC];
Ben Cheng4238ec22009-08-24 16:32:22 -07003838
3839 /* regArray <- arrayRef */
Bill Buzbee1465db52009-09-23 17:17:35 -07003840 rlArray = loadValue(cUnit, rlArray, kCoreReg);
3841 rlIdxEnd = loadValue(cUnit, rlIdxEnd, kCoreReg);
3842 genRegImmCheck(cUnit, kArmCondEq, rlArray.lowReg, 0, 0,
Ben Cheng4238ec22009-08-24 16:32:22 -07003843 (ArmLIR *) cUnit->loopAnalysis->branchToPCR);
3844
3845 /* regLength <- len(arrayRef) */
Bill Buzbeec6f10662010-02-09 11:16:15 -08003846 regLength = dvmCompilerAllocTemp(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07003847 loadWordDisp(cUnit, rlArray.lowReg, lenOffset, regLength);
Ben Cheng4238ec22009-08-24 16:32:22 -07003848
3849 int delta = maxC;
3850 /*
3851 * If the loop end condition is ">=" instead of ">", then the largest value
3852 * of the index is "endCondition - 1".
3853 */
3854 if (dInsn->arg[2] == OP_IF_GE) {
3855 delta--;
3856 }
3857
3858 if (delta) {
Bill Buzbeec6f10662010-02-09 11:16:15 -08003859 int tReg = dvmCompilerAllocTemp(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07003860 opRegRegImm(cUnit, kOpAdd, tReg, rlIdxEnd.lowReg, delta);
3861 rlIdxEnd.lowReg = tReg;
Bill Buzbeec6f10662010-02-09 11:16:15 -08003862 dvmCompilerFreeTemp(cUnit, tReg);
Ben Cheng4238ec22009-08-24 16:32:22 -07003863 }
3864 /* Punt if "regIdxEnd < len(Array)" is false */
Bill Buzbee1465db52009-09-23 17:17:35 -07003865 genRegRegCheck(cUnit, kArmCondGe, rlIdxEnd.lowReg, regLength, 0,
Ben Cheng0fd31e42009-09-03 14:40:16 -07003866 (ArmLIR *) cUnit->loopAnalysis->branchToPCR);
Ben Cheng4238ec22009-08-24 16:32:22 -07003867}
3868
3869/*
3870 * vA = arrayReg;
3871 * vB = idxReg;
3872 * vC = endConditionReg;
3873 * arg[0] = maxC
3874 * arg[1] = minC
3875 * arg[2] = loopBranchConditionCode
3876 */
3877static void genHoistedChecksForCountDownLoop(CompilationUnit *cUnit, MIR *mir)
3878{
3879 DecodedInstruction *dInsn = &mir->dalvikInsn;
3880 const int lenOffset = offsetof(ArrayObject, length);
Bill Buzbeec6f10662010-02-09 11:16:15 -08003881 const int regLength = dvmCompilerAllocTemp(cUnit);
Ben Cheng4238ec22009-08-24 16:32:22 -07003882 const int maxC = dInsn->arg[0];
Bill Buzbee1465db52009-09-23 17:17:35 -07003883 RegLocation rlArray = cUnit->regLocation[mir->dalvikInsn.vA];
3884 RegLocation rlIdxInit = cUnit->regLocation[mir->dalvikInsn.vB];
Ben Cheng4238ec22009-08-24 16:32:22 -07003885
3886 /* regArray <- arrayRef */
Bill Buzbee1465db52009-09-23 17:17:35 -07003887 rlArray = loadValue(cUnit, rlArray, kCoreReg);
3888 rlIdxInit = loadValue(cUnit, rlIdxInit, kCoreReg);
3889 genRegImmCheck(cUnit, kArmCondEq, rlArray.lowReg, 0, 0,
Ben Cheng4238ec22009-08-24 16:32:22 -07003890 (ArmLIR *) cUnit->loopAnalysis->branchToPCR);
3891
3892 /* regLength <- len(arrayRef) */
Bill Buzbee1465db52009-09-23 17:17:35 -07003893 loadWordDisp(cUnit, rlArray.lowReg, lenOffset, regLength);
Ben Cheng4238ec22009-08-24 16:32:22 -07003894
3895 if (maxC) {
Bill Buzbeec6f10662010-02-09 11:16:15 -08003896 int tReg = dvmCompilerAllocTemp(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07003897 opRegRegImm(cUnit, kOpAdd, tReg, rlIdxInit.lowReg, maxC);
3898 rlIdxInit.lowReg = tReg;
Bill Buzbeec6f10662010-02-09 11:16:15 -08003899 dvmCompilerFreeTemp(cUnit, tReg);
Ben Cheng4238ec22009-08-24 16:32:22 -07003900 }
3901
3902 /* Punt if "regIdxInit < len(Array)" is false */
Bill Buzbee1465db52009-09-23 17:17:35 -07003903 genRegRegCheck(cUnit, kArmCondGe, rlIdxInit.lowReg, regLength, 0,
Ben Cheng0fd31e42009-09-03 14:40:16 -07003904 (ArmLIR *) cUnit->loopAnalysis->branchToPCR);
Ben Cheng4238ec22009-08-24 16:32:22 -07003905}
3906
3907/*
3908 * vA = idxReg;
3909 * vB = minC;
3910 */
3911static void genHoistedLowerBoundCheck(CompilationUnit *cUnit, MIR *mir)
3912{
3913 DecodedInstruction *dInsn = &mir->dalvikInsn;
Ben Cheng4238ec22009-08-24 16:32:22 -07003914 const int minC = dInsn->vB;
Bill Buzbee1465db52009-09-23 17:17:35 -07003915 RegLocation rlIdx = cUnit->regLocation[mir->dalvikInsn.vA];
Ben Cheng4238ec22009-08-24 16:32:22 -07003916
3917 /* regIdx <- initial index value */
Bill Buzbee1465db52009-09-23 17:17:35 -07003918 rlIdx = loadValue(cUnit, rlIdx, kCoreReg);
Ben Cheng4238ec22009-08-24 16:32:22 -07003919
3920 /* Punt if "regIdxInit + minC >= 0" is false */
Bill Buzbee1465db52009-09-23 17:17:35 -07003921 genRegImmCheck(cUnit, kArmCondLt, rlIdx.lowReg, -minC, 0,
Ben Cheng4238ec22009-08-24 16:32:22 -07003922 (ArmLIR *) cUnit->loopAnalysis->branchToPCR);
3923}
3924
Ben Cheng7a2697d2010-06-07 13:44:23 -07003925/*
3926 * vC = this
3927 *
3928 * A predicted inlining target looks like the following, where instructions
3929 * between 0x4858de66 and 0x4858de72 are checking if the predicted class
3930 * matches "this", and the verificaion code is generated by this routine.
3931 *
3932 * (C) means the instruction is inlined from the callee, and (PI) means the
3933 * instruction is the predicted inlined invoke, whose corresponding
3934 * instructions are still generated to handle the mispredicted case.
3935 *
3936 * D/dalvikvm( 86): -------- kMirOpCheckInlinePrediction
3937 * D/dalvikvm( 86): 0x4858de66 (0002): ldr r0, [r5, #68]
3938 * D/dalvikvm( 86): 0x4858de68 (0004): ldr r1, [pc, #140]
3939 * D/dalvikvm( 86): 0x4858de6a (0006): cmp r0, #0
3940 * D/dalvikvm( 86): 0x4858de6c (0008): beq 0x4858deb2
3941 * D/dalvikvm( 86): 0x4858de6e (000a): ldr r2, [r0, #0]
3942 * D/dalvikvm( 86): 0x4858de70 (000c): cmp r1, r2
3943 * D/dalvikvm( 86): 0x4858de72 (000e): bne 0x4858de7a
3944 * D/dalvikvm( 86): -------- dalvik offset: 0x004c @ +iget-object-quick (C)
3945 * v4, v17, (#8)
3946 * D/dalvikvm( 86): 0x4858de74 (0010): ldr r3, [r0, #8]
3947 * D/dalvikvm( 86): 0x4858de76 (0012): str r3, [r5, #16]
3948 * D/dalvikvm( 86): -------- dalvik offset: 0x004c @
3949 * +invoke-virtual-quick/range (PI) v17..v17
3950 * D/dalvikvm( 86): 0x4858de78 (0014): b 0x4858debc
3951 * D/dalvikvm( 86): 0x4858de7a (0016): add r4,r5,#68
3952 * D/dalvikvm( 86): -------- BARRIER
3953 * D/dalvikvm( 86): 0x4858de7e (001a): ldmia r4, <r0>
3954 * D/dalvikvm( 86): -------- BARRIER
3955 * D/dalvikvm( 86): 0x4858de80 (001c): sub r7,r5,#24
3956 * D/dalvikvm( 86): 0x4858de84 (0020): cmp r0, #0
3957 * D/dalvikvm( 86): 0x4858de86 (0022): beq 0x4858deb6
3958 * D/dalvikvm( 86): -------- BARRIER
3959 * D/dalvikvm( 86): 0x4858de88 (0024): stmia r7, <r0>
3960 * D/dalvikvm( 86): -------- BARRIER
3961 * D/dalvikvm( 86): 0x4858de8a (0026): ldr r4, [pc, #104]
3962 * D/dalvikvm( 86): 0x4858de8c (0028): add r1, pc, #28
3963 * D/dalvikvm( 86): 0x4858de8e (002a): add r2, pc, #56
3964 * D/dalvikvm( 86): 0x4858de90 (002c): blx_1 0x48589198
3965 * D/dalvikvm( 86): 0x4858de92 (002e): blx_2 see above
3966 * D/dalvikvm( 86): 0x4858de94 (0030): b 0x4858dec8
3967 * D/dalvikvm( 86): 0x4858de96 (0032): b 0x4858deb6
3968 * D/dalvikvm( 86): 0x4858de98 (0034): ldr r0, [r7, #72]
3969 * D/dalvikvm( 86): 0x4858de9a (0036): cmp r1, #0
3970 * D/dalvikvm( 86): 0x4858de9c (0038): bgt 0x4858dea4
3971 * D/dalvikvm( 86): 0x4858de9e (003a): ldr r7, [r6, #116]
3972 * D/dalvikvm( 86): 0x4858dea0 (003c): movs r1, r6
3973 * D/dalvikvm( 86): 0x4858dea2 (003e): blx r7
3974 * D/dalvikvm( 86): 0x4858dea4 (0040): add r1, pc, #4
3975 * D/dalvikvm( 86): 0x4858dea6 (0042): blx_1 0x485890a0
3976 * D/dalvikvm( 86): 0x4858dea8 (0044): blx_2 see above
3977 * D/dalvikvm( 86): 0x4858deaa (0046): b 0x4858deb6
3978 * D/dalvikvm( 86): 0x4858deac (0048): .align4
3979 * D/dalvikvm( 86): L0x004f:
3980 * D/dalvikvm( 86): -------- dalvik offset: 0x004f @ move-result-object (PI)
3981 * v4, (#0), (#0)
3982 * D/dalvikvm( 86): 0x4858deac (0048): ldr r4, [r6, #8]
3983 * D/dalvikvm( 86): 0x4858deae (004a): str r4, [r5, #16]
3984 * D/dalvikvm( 86): 0x4858deb0 (004c): b 0x4858debc
3985 * D/dalvikvm( 86): -------- reconstruct dalvik PC : 0x42beefcc @ +0x004c
3986 * D/dalvikvm( 86): 0x4858deb2 (004e): ldr r0, [pc, #64]
3987 * D/dalvikvm( 86): 0x4858deb4 (0050): b 0x4858deb8
3988 * D/dalvikvm( 86): -------- reconstruct dalvik PC : 0x42beefcc @ +0x004c
3989 * D/dalvikvm( 86): 0x4858deb6 (0052): ldr r0, [pc, #60]
3990 * D/dalvikvm( 86): Exception_Handling:
3991 * D/dalvikvm( 86): 0x4858deb8 (0054): ldr r1, [r6, #100]
3992 * D/dalvikvm( 86): 0x4858deba (0056): blx r1
3993 * D/dalvikvm( 86): 0x4858debc (0058): .align4
3994 * D/dalvikvm( 86): -------- chaining cell (hot): 0x0050
3995 * D/dalvikvm( 86): 0x4858debc (0058): b 0x4858dec0
3996 * D/dalvikvm( 86): 0x4858debe (005a): orrs r0, r0
3997 * D/dalvikvm( 86): 0x4858dec0 (005c): ldr r0, [r6, #112]
3998 * D/dalvikvm( 86): 0x4858dec2 (005e): blx r0
3999 * D/dalvikvm( 86): 0x4858dec4 (0060): data 0xefd4(61396)
4000 * D/dalvikvm( 86): 0x4858dec6 (0062): data 0x42be(17086)
4001 * D/dalvikvm( 86): 0x4858dec8 (0064): .align4
4002 * D/dalvikvm( 86): -------- chaining cell (predicted)
4003 * D/dalvikvm( 86): 0x4858dec8 (0064): data 0xe7fe(59390)
4004 * D/dalvikvm( 86): 0x4858deca (0066): data 0x0000(0)
4005 * D/dalvikvm( 86): 0x4858decc (0068): data 0x0000(0)
4006 * D/dalvikvm( 86): 0x4858dece (006a): data 0x0000(0)
4007 * :
4008 */
4009static void genValidationForPredictedInline(CompilationUnit *cUnit, MIR *mir)
4010{
4011 CallsiteInfo *callsiteInfo = mir->meta.callsiteInfo;
4012 RegLocation rlThis = cUnit->regLocation[mir->dalvikInsn.vC];
4013
4014 rlThis = loadValue(cUnit, rlThis, kCoreReg);
4015 int regPredictedClass = dvmCompilerAllocTemp(cUnit);
4016 loadConstant(cUnit, regPredictedClass, (int) callsiteInfo->clazz);
4017 genNullCheck(cUnit, rlThis.sRegLow, rlThis.lowReg, mir->offset,
4018 NULL);/* null object? */
4019 int regActualClass = dvmCompilerAllocTemp(cUnit);
4020 loadWordDisp(cUnit, rlThis.lowReg, offsetof(Object, clazz), regActualClass);
4021 opRegReg(cUnit, kOpCmp, regPredictedClass, regActualClass);
4022 /*
4023 * Set the misPredBranchOver target so that it will be generated when the
4024 * code for the non-optimized invoke is generated.
4025 */
4026 callsiteInfo->misPredBranchOver = (LIR *) opCondBranch(cUnit, kArmCondNe);
4027}
4028
Ben Cheng4238ec22009-08-24 16:32:22 -07004029/* Extended MIR instructions like PHI */
4030static void handleExtendedMIR(CompilationUnit *cUnit, MIR *mir)
4031{
Dan Bornstein9a1f8162010-12-01 17:02:26 -08004032 int opOffset = mir->dalvikInsn.opcode - kMirOpFirst;
Carl Shapirofc75f3e2010-12-07 11:43:38 -08004033 char *msg = (char *)dvmCompilerNew(strlen(extendedMIROpNames[opOffset]) + 1,
4034 false);
Ben Cheng4238ec22009-08-24 16:32:22 -07004035 strcpy(msg, extendedMIROpNames[opOffset]);
Bill Buzbee1465db52009-09-23 17:17:35 -07004036 newLIR1(cUnit, kArmPseudoExtended, (int) msg);
Ben Cheng4238ec22009-08-24 16:32:22 -07004037
Dan Bornstein9a1f8162010-12-01 17:02:26 -08004038 switch (mir->dalvikInsn.opcode) {
Bill Buzbee1465db52009-09-23 17:17:35 -07004039 case kMirOpPhi: {
Ben Cheng4238ec22009-08-24 16:32:22 -07004040 char *ssaString = dvmCompilerGetSSAString(cUnit, mir->ssaRep);
Bill Buzbee1465db52009-09-23 17:17:35 -07004041 newLIR1(cUnit, kArmPseudoSSARep, (int) ssaString);
Ben Cheng4238ec22009-08-24 16:32:22 -07004042 break;
4043 }
Bill Buzbee1465db52009-09-23 17:17:35 -07004044 case kMirOpNullNRangeUpCheck: {
Ben Cheng4238ec22009-08-24 16:32:22 -07004045 genHoistedChecksForCountUpLoop(cUnit, mir);
4046 break;
4047 }
Bill Buzbee1465db52009-09-23 17:17:35 -07004048 case kMirOpNullNRangeDownCheck: {
Ben Cheng4238ec22009-08-24 16:32:22 -07004049 genHoistedChecksForCountDownLoop(cUnit, mir);
4050 break;
4051 }
Bill Buzbee1465db52009-09-23 17:17:35 -07004052 case kMirOpLowerBound: {
Ben Cheng4238ec22009-08-24 16:32:22 -07004053 genHoistedLowerBoundCheck(cUnit, mir);
4054 break;
4055 }
Bill Buzbee1465db52009-09-23 17:17:35 -07004056 case kMirOpPunt: {
Ben Cheng4238ec22009-08-24 16:32:22 -07004057 genUnconditionalBranch(cUnit,
4058 (ArmLIR *) cUnit->loopAnalysis->branchToPCR);
4059 break;
4060 }
Ben Cheng7a2697d2010-06-07 13:44:23 -07004061 case kMirOpCheckInlinePrediction: {
4062 genValidationForPredictedInline(cUnit, mir);
4063 break;
4064 }
Ben Cheng4238ec22009-08-24 16:32:22 -07004065 default:
4066 break;
4067 }
4068}
4069
4070/*
4071 * Create a PC-reconstruction cell for the starting offset of this trace.
4072 * Since the PCR cell is placed near the end of the compiled code which is
4073 * usually out of range for a conditional branch, we put two branches (one
4074 * branch over to the loop body and one layover branch to the actual PCR) at the
4075 * end of the entry block.
4076 */
4077static void setupLoopEntryBlock(CompilationUnit *cUnit, BasicBlock *entry,
4078 ArmLIR *bodyLabel)
4079{
4080 /* Set up the place holder to reconstruct this Dalvik PC */
Carl Shapirofc75f3e2010-12-07 11:43:38 -08004081 ArmLIR *pcrLabel = (ArmLIR *) dvmCompilerNew(sizeof(ArmLIR), true);
Dan Bornstein9a1f8162010-12-01 17:02:26 -08004082 pcrLabel->opcode = kArmPseudoPCReconstructionCell;
Ben Cheng4238ec22009-08-24 16:32:22 -07004083 pcrLabel->operands[0] =
4084 (int) (cUnit->method->insns + entry->startOffset);
4085 pcrLabel->operands[1] = entry->startOffset;
4086 /* Insert the place holder to the growable list */
Ben Cheng00603072010-10-28 11:13:58 -07004087 dvmInsertGrowableList(&cUnit->pcReconstructionList, (intptr_t) pcrLabel);
Ben Cheng4238ec22009-08-24 16:32:22 -07004088
4089 /*
4090 * Next, create two branches - one branch over to the loop body and the
4091 * other branch to the PCR cell to punt.
4092 */
Carl Shapirofc75f3e2010-12-07 11:43:38 -08004093 ArmLIR *branchToBody = (ArmLIR *) dvmCompilerNew(sizeof(ArmLIR), true);
Dan Bornstein9a1f8162010-12-01 17:02:26 -08004094 branchToBody->opcode = kThumbBUncond;
Ben Cheng4238ec22009-08-24 16:32:22 -07004095 branchToBody->generic.target = (LIR *) bodyLabel;
Ben Chengdcf3e5d2009-09-11 13:42:05 -07004096 setupResourceMasks(branchToBody);
Ben Cheng4238ec22009-08-24 16:32:22 -07004097 cUnit->loopAnalysis->branchToBody = (LIR *) branchToBody;
4098
Carl Shapirofc75f3e2010-12-07 11:43:38 -08004099 ArmLIR *branchToPCR = (ArmLIR *) dvmCompilerNew(sizeof(ArmLIR), true);
Dan Bornstein9a1f8162010-12-01 17:02:26 -08004100 branchToPCR->opcode = kThumbBUncond;
Ben Cheng4238ec22009-08-24 16:32:22 -07004101 branchToPCR->generic.target = (LIR *) pcrLabel;
Ben Chengdcf3e5d2009-09-11 13:42:05 -07004102 setupResourceMasks(branchToPCR);
Ben Cheng4238ec22009-08-24 16:32:22 -07004103 cUnit->loopAnalysis->branchToPCR = (LIR *) branchToPCR;
4104}
4105
Ben Chengd5adae12010-03-26 17:45:28 -07004106#if defined(WITH_SELF_VERIFICATION)
4107static bool selfVerificationPuntOps(MIR *mir)
4108{
4109 DecodedInstruction *decInsn = &mir->dalvikInsn;
Dan Bornstein9a1f8162010-12-01 17:02:26 -08004110 Opcode op = decInsn->opcode;
Ben Cheng7a2697d2010-06-07 13:44:23 -07004111
Ben Chengd5adae12010-03-26 17:45:28 -07004112 /*
4113 * All opcodes that can throw exceptions and use the
4114 * TEMPLATE_THROW_EXCEPTION_COMMON template should be excluded in the trace
4115 * under self-verification mode.
4116 */
4117 return (op == OP_MONITOR_ENTER || op == OP_MONITOR_EXIT ||
4118 op == OP_NEW_INSTANCE || op == OP_NEW_ARRAY ||
4119 op == OP_CHECK_CAST || op == OP_MOVE_EXCEPTION ||
4120 op == OP_FILL_ARRAY_DATA || op == OP_EXECUTE_INLINE ||
Ben Cheng7a2697d2010-06-07 13:44:23 -07004121 op == OP_EXECUTE_INLINE_RANGE);
Ben Chengd5adae12010-03-26 17:45:28 -07004122}
4123#endif
4124
Ben Chengba4fc8b2009-06-01 13:00:29 -07004125void dvmCompilerMIR2LIR(CompilationUnit *cUnit)
4126{
4127 /* Used to hold the labels of each block */
Bill Buzbee89efc3d2009-07-28 11:22:22 -07004128 ArmLIR *labelList =
Carl Shapirofc75f3e2010-12-07 11:43:38 -08004129 (ArmLIR *) dvmCompilerNew(sizeof(ArmLIR) * cUnit->numBlocks, true);
Ben Chengcec26f62010-01-15 15:29:33 -08004130 GrowableList chainingListByType[kChainingCellGap];
Ben Chengba4fc8b2009-06-01 13:00:29 -07004131 int i;
4132
4133 /*
Ben Cheng38329f52009-07-07 14:19:20 -07004134 * Initialize various types chaining lists.
Ben Chengba4fc8b2009-06-01 13:00:29 -07004135 */
Ben Chengcec26f62010-01-15 15:29:33 -08004136 for (i = 0; i < kChainingCellGap; i++) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07004137 dvmInitGrowableList(&chainingListByType[i], 2);
4138 }
4139
Ben Cheng7ab74e12011-02-03 14:02:06 -08004140 /* Clear the visited flag for each block */
4141 dvmCompilerDataFlowAnalysisDispatcher(cUnit, dvmCompilerClearVisitedFlag,
4142 kAllNodes, false /* isIterative */);
4143
Ben Cheng00603072010-10-28 11:13:58 -07004144 GrowableListIterator iterator;
4145 dvmGrowableListIteratorInit(&cUnit->blockList, &iterator);
Ben Chengba4fc8b2009-06-01 13:00:29 -07004146
buzbee2e152ba2010-12-15 16:32:35 -08004147 /* Traces start with a profiling entry point. Generate it here */
4148 cUnit->profileCodeSize = genTraceProfileEntry(cUnit);
Ben Cheng1efc9c52009-06-08 18:25:27 -07004149
Ben Chengba4fc8b2009-06-01 13:00:29 -07004150 /* Handle the content in each basic block */
Ben Cheng00603072010-10-28 11:13:58 -07004151 for (i = 0; ; i++) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07004152 MIR *mir;
Ben Cheng00603072010-10-28 11:13:58 -07004153 BasicBlock *bb = (BasicBlock *) dvmGrowableListIteratorNext(&iterator);
4154 if (bb == NULL) break;
Ben Cheng7ab74e12011-02-03 14:02:06 -08004155 if (bb->visited == true) continue;
Ben Chengba4fc8b2009-06-01 13:00:29 -07004156
Ben Cheng00603072010-10-28 11:13:58 -07004157 labelList[i].operands[0] = bb->startOffset;
Ben Chengba4fc8b2009-06-01 13:00:29 -07004158
Ben Cheng00603072010-10-28 11:13:58 -07004159 if (bb->blockType >= kChainingCellGap) {
4160 if (bb->isFallThroughFromInvoke == true) {
Ben Chengd44faf52010-06-02 15:33:51 -07004161 /* Align this block first since it is a return chaining cell */
4162 newLIR0(cUnit, kArmPseudoPseudoAlign4);
4163 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07004164 /*
4165 * Append the label pseudo LIR first. Chaining cells will be handled
4166 * separately afterwards.
4167 */
4168 dvmCompilerAppendLIR(cUnit, (LIR *) &labelList[i]);
4169 }
4170
Ben Cheng00603072010-10-28 11:13:58 -07004171 if (bb->blockType == kTraceEntryBlock) {
Dan Bornstein9a1f8162010-12-01 17:02:26 -08004172 labelList[i].opcode = kArmPseudoEntryBlock;
Ben Cheng00603072010-10-28 11:13:58 -07004173 if (bb->firstMIRInsn == NULL) {
Ben Cheng4238ec22009-08-24 16:32:22 -07004174 continue;
4175 } else {
Ben Cheng00603072010-10-28 11:13:58 -07004176 setupLoopEntryBlock(cUnit, bb,
4177 &labelList[bb->fallThrough->id]);
Ben Cheng4238ec22009-08-24 16:32:22 -07004178 }
Ben Cheng00603072010-10-28 11:13:58 -07004179 } else if (bb->blockType == kTraceExitBlock) {
Dan Bornstein9a1f8162010-12-01 17:02:26 -08004180 labelList[i].opcode = kArmPseudoExitBlock;
Ben Cheng4238ec22009-08-24 16:32:22 -07004181 goto gen_fallthrough;
Ben Cheng00603072010-10-28 11:13:58 -07004182 } else if (bb->blockType == kDalvikByteCode) {
Dan Bornstein9a1f8162010-12-01 17:02:26 -08004183 labelList[i].opcode = kArmPseudoNormalBlockLabel;
Ben Chenge9695e52009-06-16 16:11:47 -07004184 /* Reset the register state */
Bill Buzbeec6f10662010-02-09 11:16:15 -08004185 dvmCompilerResetRegPool(cUnit);
4186 dvmCompilerClobberAllRegs(cUnit);
4187 dvmCompilerResetNullCheck(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -07004188 } else {
Ben Cheng00603072010-10-28 11:13:58 -07004189 switch (bb->blockType) {
Bill Buzbee1465db52009-09-23 17:17:35 -07004190 case kChainingCellNormal:
Dan Bornstein9a1f8162010-12-01 17:02:26 -08004191 labelList[i].opcode = kArmPseudoChainingCellNormal;
Ben Chengba4fc8b2009-06-01 13:00:29 -07004192 /* handle the codegen later */
4193 dvmInsertGrowableList(
Ben Cheng00603072010-10-28 11:13:58 -07004194 &chainingListByType[kChainingCellNormal], i);
Ben Chengba4fc8b2009-06-01 13:00:29 -07004195 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07004196 case kChainingCellInvokeSingleton:
Dan Bornstein9a1f8162010-12-01 17:02:26 -08004197 labelList[i].opcode =
Ben Chenga4973592010-03-31 11:59:18 -07004198 kArmPseudoChainingCellInvokeSingleton;
Ben Chengba4fc8b2009-06-01 13:00:29 -07004199 labelList[i].operands[0] =
Ben Cheng00603072010-10-28 11:13:58 -07004200 (int) bb->containingMethod;
Ben Chengba4fc8b2009-06-01 13:00:29 -07004201 /* handle the codegen later */
4202 dvmInsertGrowableList(
Ben Cheng00603072010-10-28 11:13:58 -07004203 &chainingListByType[kChainingCellInvokeSingleton], i);
Ben Cheng38329f52009-07-07 14:19:20 -07004204 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07004205 case kChainingCellInvokePredicted:
Dan Bornstein9a1f8162010-12-01 17:02:26 -08004206 labelList[i].opcode =
Ben Chenga4973592010-03-31 11:59:18 -07004207 kArmPseudoChainingCellInvokePredicted;
Ben Cheng38329f52009-07-07 14:19:20 -07004208 /* handle the codegen later */
4209 dvmInsertGrowableList(
Ben Cheng00603072010-10-28 11:13:58 -07004210 &chainingListByType[kChainingCellInvokePredicted], i);
Ben Chengba4fc8b2009-06-01 13:00:29 -07004211 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07004212 case kChainingCellHot:
Dan Bornstein9a1f8162010-12-01 17:02:26 -08004213 labelList[i].opcode =
Ben Chenga4973592010-03-31 11:59:18 -07004214 kArmPseudoChainingCellHot;
Ben Chengba4fc8b2009-06-01 13:00:29 -07004215 /* handle the codegen later */
4216 dvmInsertGrowableList(
Ben Cheng00603072010-10-28 11:13:58 -07004217 &chainingListByType[kChainingCellHot], i);
Ben Chengba4fc8b2009-06-01 13:00:29 -07004218 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07004219 case kPCReconstruction:
Ben Chengba4fc8b2009-06-01 13:00:29 -07004220 /* Make sure exception handling block is next */
Dan Bornstein9a1f8162010-12-01 17:02:26 -08004221 labelList[i].opcode =
Ben Chenga4973592010-03-31 11:59:18 -07004222 kArmPseudoPCReconstructionBlockLabel;
Ben Chengba4fc8b2009-06-01 13:00:29 -07004223 assert (i == cUnit->numBlocks - 2);
4224 handlePCReconstruction(cUnit, &labelList[i+1]);
4225 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07004226 case kExceptionHandling:
Dan Bornstein9a1f8162010-12-01 17:02:26 -08004227 labelList[i].opcode = kArmPseudoEHBlockLabel;
Ben Chengba4fc8b2009-06-01 13:00:29 -07004228 if (cUnit->pcReconstructionList.numUsed) {
buzbee9f601a92011-02-11 17:48:20 -08004229 loadWordDisp(cUnit, rSELF, offsetof(Thread,
Bill Buzbee270c1d62009-08-13 16:58:07 -07004230 jitToInterpEntries.dvmJitToInterpPunt),
4231 r1);
Bill Buzbee1465db52009-09-23 17:17:35 -07004232 opReg(cUnit, kOpBlx, r1);
Ben Chengba4fc8b2009-06-01 13:00:29 -07004233 }
4234 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07004235 case kChainingCellBackwardBranch:
Dan Bornstein9a1f8162010-12-01 17:02:26 -08004236 labelList[i].opcode =
Ben Chenga4973592010-03-31 11:59:18 -07004237 kArmPseudoChainingCellBackwardBranch;
Jeff Hao97319a82009-08-12 16:57:15 -07004238 /* handle the codegen later */
4239 dvmInsertGrowableList(
Bill Buzbee1465db52009-09-23 17:17:35 -07004240 &chainingListByType[kChainingCellBackwardBranch],
Ben Cheng00603072010-10-28 11:13:58 -07004241 i);
Jeff Hao97319a82009-08-12 16:57:15 -07004242 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07004243 default:
4244 break;
4245 }
4246 continue;
4247 }
Ben Chenge9695e52009-06-16 16:11:47 -07004248
Bill Buzbee89efc3d2009-07-28 11:22:22 -07004249 ArmLIR *headLIR = NULL;
Ben Cheng7ab74e12011-02-03 14:02:06 -08004250 BasicBlock *nextBB = bb;
Ben Chenge9695e52009-06-16 16:11:47 -07004251
Ben Cheng7ab74e12011-02-03 14:02:06 -08004252 /*
4253 * Try to build a longer optimization unit. Currently if the previous
4254 * block ends with a goto, we continue adding instructions and don't
4255 * reset the register allocation pool.
4256 */
4257 for (; nextBB != NULL; nextBB = cUnit->nextCodegenBlock) {
4258 bb = nextBB;
4259 bb->visited = true;
4260 cUnit->nextCodegenBlock = NULL;
Bill Buzbee1465db52009-09-23 17:17:35 -07004261
Ben Cheng7ab74e12011-02-03 14:02:06 -08004262 for (mir = bb->firstMIRInsn; mir; mir = mir->next) {
Bill Buzbee1465db52009-09-23 17:17:35 -07004263
Ben Cheng7ab74e12011-02-03 14:02:06 -08004264 dvmCompilerResetRegPool(cUnit);
4265 if (gDvmJit.disableOpt & (1 << kTrackLiveTemps)) {
4266 dvmCompilerClobberAllRegs(cUnit);
Ben Cheng80211d22011-01-14 10:23:37 -08004267 }
Ben Cheng4238ec22009-08-24 16:32:22 -07004268
Ben Cheng7ab74e12011-02-03 14:02:06 -08004269 if (gDvmJit.disableOpt & (1 << kSuppressLoads)) {
4270 dvmCompilerResetDefTracking(cUnit);
4271 }
Ben Cheng4238ec22009-08-24 16:32:22 -07004272
Ben Cheng7ab74e12011-02-03 14:02:06 -08004273 if (mir->dalvikInsn.opcode >= kMirOpFirst) {
4274 handleExtendedMIR(cUnit, mir);
4275 continue;
4276 }
4277
4278
4279 Opcode dalvikOpcode = mir->dalvikInsn.opcode;
4280 InstructionFormat dalvikFormat =
4281 dexGetFormatFromOpcode(dalvikOpcode);
4282 char *note;
4283 if (mir->OptimizationFlags & MIR_INLINED) {
4284 note = " (I)";
4285 } else if (mir->OptimizationFlags & MIR_INLINED_PRED) {
4286 note = " (PI)";
4287 } else if (mir->OptimizationFlags & MIR_CALLEE) {
4288 note = " (C)";
4289 } else {
4290 note = NULL;
4291 }
4292
4293 ArmLIR *boundaryLIR;
4294
4295 /*
4296 * Don't generate the boundary LIR unless we are debugging this
4297 * trace or we need a scheduling barrier.
4298 */
4299 if (headLIR == NULL || cUnit->printMe == true) {
4300 boundaryLIR =
4301 newLIR2(cUnit, kArmPseudoDalvikByteCodeBoundary,
4302 mir->offset,
4303 (int) dvmCompilerGetDalvikDisassembly(
4304 &mir->dalvikInsn, note));
4305 /* Remember the first LIR for this block */
4306 if (headLIR == NULL) {
4307 headLIR = boundaryLIR;
4308 /* Set the first boundaryLIR as a scheduling barrier */
4309 headLIR->defMask = ENCODE_ALL;
4310 }
4311 }
4312
4313 /*
4314 * Don't generate the SSA annotation unless verbose mode is on
4315 */
4316 if (cUnit->printMe && mir->ssaRep) {
4317 char *ssaString = dvmCompilerGetSSAString(cUnit,
4318 mir->ssaRep);
4319 newLIR1(cUnit, kArmPseudoSSARep, (int) ssaString);
4320 }
4321
4322 bool notHandled;
4323 /*
4324 * Debugging: screen the opcode first to see if it is in the
4325 * do[-not]-compile list
4326 */
4327 bool singleStepMe = SINGLE_STEP_OP(dalvikOpcode);
Ben Chengd5adae12010-03-26 17:45:28 -07004328#if defined(WITH_SELF_VERIFICATION)
Ben Cheng7ab74e12011-02-03 14:02:06 -08004329 if (singleStepMe == false) {
4330 singleStepMe = selfVerificationPuntOps(mir);
4331 }
Ben Chengd5adae12010-03-26 17:45:28 -07004332#endif
Ben Cheng7ab74e12011-02-03 14:02:06 -08004333 if (singleStepMe || cUnit->allSingleStep) {
4334 notHandled = false;
4335 genInterpSingleStep(cUnit, mir);
4336 } else {
4337 opcodeCoverage[dalvikOpcode]++;
4338 switch (dalvikFormat) {
4339 case kFmt10t:
4340 case kFmt20t:
4341 case kFmt30t:
4342 notHandled = handleFmt10t_Fmt20t_Fmt30t(cUnit,
4343 mir, bb, labelList);
4344 break;
4345 case kFmt10x:
4346 notHandled = handleFmt10x(cUnit, mir);
4347 break;
4348 case kFmt11n:
4349 case kFmt31i:
4350 notHandled = handleFmt11n_Fmt31i(cUnit, mir);
4351 break;
4352 case kFmt11x:
4353 notHandled = handleFmt11x(cUnit, mir);
4354 break;
4355 case kFmt12x:
4356 notHandled = handleFmt12x(cUnit, mir);
4357 break;
4358 case kFmt20bc:
4359 case kFmt40sc:
4360 notHandled = handleFmt20bc_Fmt40sc(cUnit, mir);
4361 break;
4362 case kFmt21c:
4363 case kFmt31c:
4364 case kFmt41c:
4365 notHandled = handleFmt21c_Fmt31c_Fmt41c(cUnit, mir);
4366 break;
4367 case kFmt21h:
4368 notHandled = handleFmt21h(cUnit, mir);
4369 break;
4370 case kFmt21s:
4371 notHandled = handleFmt21s(cUnit, mir);
4372 break;
4373 case kFmt21t:
4374 notHandled = handleFmt21t(cUnit, mir, bb,
Ben Chengba4fc8b2009-06-01 13:00:29 -07004375 labelList);
Ben Cheng7ab74e12011-02-03 14:02:06 -08004376 break;
4377 case kFmt22b:
4378 case kFmt22s:
4379 notHandled = handleFmt22b_Fmt22s(cUnit, mir);
4380 break;
4381 case kFmt22c:
4382 case kFmt52c:
4383 notHandled = handleFmt22c_Fmt52c(cUnit, mir);
4384 break;
4385 case kFmt22cs:
4386 notHandled = handleFmt22cs(cUnit, mir);
4387 break;
4388 case kFmt22t:
4389 notHandled = handleFmt22t(cUnit, mir, bb,
4390 labelList);
4391 break;
4392 case kFmt22x:
4393 case kFmt32x:
4394 notHandled = handleFmt22x_Fmt32x(cUnit, mir);
4395 break;
4396 case kFmt23x:
4397 notHandled = handleFmt23x(cUnit, mir);
4398 break;
4399 case kFmt31t:
4400 notHandled = handleFmt31t(cUnit, mir);
4401 break;
4402 case kFmt3rc:
4403 case kFmt35c:
4404 case kFmt5rc:
4405 notHandled = handleFmt35c_3rc_5rc(cUnit, mir, bb,
4406 labelList);
4407 break;
4408 case kFmt3rms:
4409 case kFmt35ms:
4410 notHandled = handleFmt35ms_3rms(cUnit, mir, bb,
4411 labelList);
4412 break;
4413 case kFmt35mi:
4414 case kFmt3rmi:
4415 notHandled = handleExecuteInline(cUnit, mir);
4416 break;
4417 case kFmt51l:
4418 notHandled = handleFmt51l(cUnit, mir);
4419 break;
4420 default:
4421 notHandled = true;
4422 break;
4423 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07004424 }
Ben Cheng7ab74e12011-02-03 14:02:06 -08004425 if (notHandled) {
4426 LOGE("%#06x: Opcode 0x%x (%s) / Fmt %d not handled\n",
4427 mir->offset,
4428 dalvikOpcode, dexGetOpcodeName(dalvikOpcode),
4429 dalvikFormat);
4430 dvmCompilerAbort(cUnit);
4431 break;
4432 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07004433 }
4434 }
Ben Cheng4238ec22009-08-24 16:32:22 -07004435
Ben Cheng00603072010-10-28 11:13:58 -07004436 if (bb->blockType == kTraceEntryBlock) {
Ben Cheng4238ec22009-08-24 16:32:22 -07004437 dvmCompilerAppendLIR(cUnit,
4438 (LIR *) cUnit->loopAnalysis->branchToBody);
4439 dvmCompilerAppendLIR(cUnit,
4440 (LIR *) cUnit->loopAnalysis->branchToPCR);
4441 }
4442
4443 if (headLIR) {
4444 /*
4445 * Eliminate redundant loads/stores and delay stores into later
4446 * slots
4447 */
4448 dvmCompilerApplyLocalOptimizations(cUnit, (LIR *) headLIR,
4449 cUnit->lastLIRInsn);
4450 }
4451
4452gen_fallthrough:
Ben Cheng1efc9c52009-06-08 18:25:27 -07004453 /*
4454 * Check if the block is terminated due to trace length constraint -
4455 * insert an unconditional branch to the chaining cell.
4456 */
Ben Cheng00603072010-10-28 11:13:58 -07004457 if (bb->needFallThroughBranch) {
Ben Cheng7ab74e12011-02-03 14:02:06 -08004458 genUnconditionalBranch(cUnit, &labelList[bb->fallThrough->id]);
Ben Cheng1efc9c52009-06-08 18:25:27 -07004459 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07004460 }
4461
Ben Chenge9695e52009-06-16 16:11:47 -07004462 /* Handle the chaining cells in predefined order */
Ben Chengcec26f62010-01-15 15:29:33 -08004463 for (i = 0; i < kChainingCellGap; i++) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07004464 size_t j;
4465 int *blockIdList = (int *) chainingListByType[i].elemList;
4466
4467 cUnit->numChainingCells[i] = chainingListByType[i].numUsed;
4468
4469 /* No chaining cells of this type */
4470 if (cUnit->numChainingCells[i] == 0)
4471 continue;
4472
4473 /* Record the first LIR for a new type of chaining cell */
4474 cUnit->firstChainingLIR[i] = (LIR *) &labelList[blockIdList[0]];
4475
4476 for (j = 0; j < chainingListByType[i].numUsed; j++) {
4477 int blockId = blockIdList[j];
Ben Cheng00603072010-10-28 11:13:58 -07004478 BasicBlock *chainingBlock =
4479 (BasicBlock *) dvmGrowableListGetElement(&cUnit->blockList,
4480 blockId);
Ben Chengba4fc8b2009-06-01 13:00:29 -07004481
4482 /* Align this chaining cell first */
Bill Buzbee1465db52009-09-23 17:17:35 -07004483 newLIR0(cUnit, kArmPseudoPseudoAlign4);
Ben Chengba4fc8b2009-06-01 13:00:29 -07004484
4485 /* Insert the pseudo chaining instruction */
4486 dvmCompilerAppendLIR(cUnit, (LIR *) &labelList[blockId]);
4487
4488
Ben Cheng00603072010-10-28 11:13:58 -07004489 switch (chainingBlock->blockType) {
Bill Buzbee1465db52009-09-23 17:17:35 -07004490 case kChainingCellNormal:
Ben Cheng00603072010-10-28 11:13:58 -07004491 handleNormalChainingCell(cUnit, chainingBlock->startOffset);
Ben Chengba4fc8b2009-06-01 13:00:29 -07004492 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07004493 case kChainingCellInvokeSingleton:
Ben Cheng38329f52009-07-07 14:19:20 -07004494 handleInvokeSingletonChainingCell(cUnit,
Ben Cheng00603072010-10-28 11:13:58 -07004495 chainingBlock->containingMethod);
Ben Chengba4fc8b2009-06-01 13:00:29 -07004496 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07004497 case kChainingCellInvokePredicted:
Ben Cheng38329f52009-07-07 14:19:20 -07004498 handleInvokePredictedChainingCell(cUnit);
4499 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07004500 case kChainingCellHot:
Ben Cheng00603072010-10-28 11:13:58 -07004501 handleHotChainingCell(cUnit, chainingBlock->startOffset);
Ben Chengba4fc8b2009-06-01 13:00:29 -07004502 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07004503 case kChainingCellBackwardBranch:
Jeff Hao97319a82009-08-12 16:57:15 -07004504 handleBackwardBranchChainingCell(cUnit,
Ben Cheng00603072010-10-28 11:13:58 -07004505 chainingBlock->startOffset);
Jeff Hao97319a82009-08-12 16:57:15 -07004506 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07004507 default:
Ben Cheng00603072010-10-28 11:13:58 -07004508 LOGE("Bad blocktype %d", chainingBlock->blockType);
Bill Buzbeefc519dc2010-03-06 23:30:57 -08004509 dvmCompilerAbort(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -07004510 }
4511 }
4512 }
Ben Chenge9695e52009-06-16 16:11:47 -07004513
Ben Chengcec26f62010-01-15 15:29:33 -08004514 /* Mark the bottom of chaining cells */
4515 cUnit->chainingCellBottom = (LIR *) newLIR0(cUnit, kArmChainingCellBottom);
4516
Ben Cheng6c10a972009-10-29 14:39:18 -07004517 /*
4518 * Generate the branch to the dvmJitToInterpNoChain entry point at the end
4519 * of all chaining cells for the overflow cases.
4520 */
4521 if (cUnit->switchOverflowPad) {
4522 loadConstant(cUnit, r0, (int) cUnit->switchOverflowPad);
buzbee9f601a92011-02-11 17:48:20 -08004523 loadWordDisp(cUnit, rSELF, offsetof(Thread,
Ben Cheng6c10a972009-10-29 14:39:18 -07004524 jitToInterpEntries.dvmJitToInterpNoChain), r2);
4525 opRegReg(cUnit, kOpAdd, r1, r1);
4526 opRegRegReg(cUnit, kOpAdd, r4PC, r0, r1);
Ben Cheng978738d2010-05-13 13:45:57 -07004527#if defined(WITH_JIT_TUNING)
Ben Cheng6c10a972009-10-29 14:39:18 -07004528 loadConstant(cUnit, r0, kSwitchOverflow);
4529#endif
4530 opReg(cUnit, kOpBlx, r2);
4531 }
4532
Ben Chenge9695e52009-06-16 16:11:47 -07004533 dvmCompilerApplyGlobalOptimizations(cUnit);
jeffhao9e45c0b2010-02-03 10:24:05 -08004534
4535#if defined(WITH_SELF_VERIFICATION)
4536 selfVerificationBranchInsertPass(cUnit);
4537#endif
Ben Chengba4fc8b2009-06-01 13:00:29 -07004538}
4539
buzbee2e152ba2010-12-15 16:32:35 -08004540/*
4541 * Accept the work and start compiling. Returns true if compilation
4542 * is attempted.
4543 */
Bill Buzbee716f1202009-07-23 13:22:09 -07004544bool dvmCompilerDoWork(CompilerWorkOrder *work)
Ben Chengba4fc8b2009-06-01 13:00:29 -07004545{
Carl Shapirofc75f3e2010-12-07 11:43:38 -08004546 JitTraceDescription *desc;
buzbee2e152ba2010-12-15 16:32:35 -08004547 bool isCompile;
4548 bool success = true;
Ben Chengba4fc8b2009-06-01 13:00:29 -07004549
Ben Cheng6999d842010-01-26 16:46:15 -08004550 if (gDvmJit.codeCacheFull) {
Ben Chengccd6c012009-10-15 14:52:45 -07004551 return false;
4552 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07004553
Ben Chengccd6c012009-10-15 14:52:45 -07004554 switch (work->kind) {
Ben Chengccd6c012009-10-15 14:52:45 -07004555 case kWorkOrderTrace:
buzbee2e152ba2010-12-15 16:32:35 -08004556 isCompile = true;
Ben Chengccd6c012009-10-15 14:52:45 -07004557 /* Start compilation with maximally allowed trace length */
Carl Shapirofc75f3e2010-12-07 11:43:38 -08004558 desc = (JitTraceDescription *)work->info;
buzbee2e152ba2010-12-15 16:32:35 -08004559 success = dvmCompileTrace(desc, JIT_MAX_TRACE_LEN, &work->result,
4560 work->bailPtr, 0 /* no hints */);
Ben Chengccd6c012009-10-15 14:52:45 -07004561 break;
4562 case kWorkOrderTraceDebug: {
4563 bool oldPrintMe = gDvmJit.printMe;
4564 gDvmJit.printMe = true;
buzbee2e152ba2010-12-15 16:32:35 -08004565 isCompile = true;
Ben Chengccd6c012009-10-15 14:52:45 -07004566 /* Start compilation with maximally allowed trace length */
Carl Shapirofc75f3e2010-12-07 11:43:38 -08004567 desc = (JitTraceDescription *)work->info;
buzbee2e152ba2010-12-15 16:32:35 -08004568 success = dvmCompileTrace(desc, JIT_MAX_TRACE_LEN, &work->result,
4569 work->bailPtr, 0 /* no hints */);
Elliott Hughes672511b2010-04-26 17:40:13 -07004570 gDvmJit.printMe = oldPrintMe;
Ben Chengccd6c012009-10-15 14:52:45 -07004571 break;
4572 }
buzbee2e152ba2010-12-15 16:32:35 -08004573 case kWorkOrderProfileMode:
4574 dvmJitChangeProfileMode((TraceProfilingModes)work->info);
4575 isCompile = false;
4576 break;
Ben Chengccd6c012009-10-15 14:52:45 -07004577 default:
buzbee2e152ba2010-12-15 16:32:35 -08004578 isCompile = false;
Bill Buzbeefc519dc2010-03-06 23:30:57 -08004579 LOGE("Jit: unknown work order type");
Elliott Hughes672511b2010-04-26 17:40:13 -07004580 assert(0); // Bail if debug build, discard otherwise
Ben Chengccd6c012009-10-15 14:52:45 -07004581 }
buzbee2e152ba2010-12-15 16:32:35 -08004582 if (!success)
4583 work->result.codeAddress = NULL;
4584 return isCompile;
Ben Chengba4fc8b2009-06-01 13:00:29 -07004585}
4586
Ben Chengba4fc8b2009-06-01 13:00:29 -07004587/* Architectural-specific debugging helpers go here */
4588void dvmCompilerArchDump(void)
4589{
4590 /* Print compiled opcode in this VM instance */
4591 int i, start, streak;
4592 char buf[1024];
4593
4594 streak = i = 0;
4595 buf[0] = 0;
Dan Bornsteinccaab182010-12-03 15:32:40 -08004596 while (opcodeCoverage[i] == 0 && i < kNumPackedOpcodes) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07004597 i++;
4598 }
Dan Bornsteinccaab182010-12-03 15:32:40 -08004599 if (i == kNumPackedOpcodes) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07004600 return;
4601 }
Dan Bornsteinccaab182010-12-03 15:32:40 -08004602 for (start = i++, streak = 1; i < kNumPackedOpcodes; i++) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07004603 if (opcodeCoverage[i]) {
4604 streak++;
4605 } else {
4606 if (streak == 1) {
4607 sprintf(buf+strlen(buf), "%x,", start);
4608 } else {
4609 sprintf(buf+strlen(buf), "%x-%x,", start, start + streak - 1);
4610 }
4611 streak = 0;
Dan Bornsteinccaab182010-12-03 15:32:40 -08004612 while (opcodeCoverage[i] == 0 && i < kNumPackedOpcodes) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07004613 i++;
4614 }
Dan Bornsteinccaab182010-12-03 15:32:40 -08004615 if (i < kNumPackedOpcodes) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07004616 streak = 1;
4617 start = i;
4618 }
4619 }
4620 }
4621 if (streak) {
4622 if (streak == 1) {
4623 sprintf(buf+strlen(buf), "%x", start);
4624 } else {
4625 sprintf(buf+strlen(buf), "%x-%x", start, start + streak - 1);
4626 }
4627 }
4628 if (strlen(buf)) {
Ben Cheng8b258bf2009-06-24 17:27:07 -07004629 LOGD("dalvik.vm.jit.op = %s", buf);
Ben Chengba4fc8b2009-06-01 13:00:29 -07004630 }
4631}
Ben Chengd7d426a2009-09-22 11:23:36 -07004632
4633/* Common initialization routine for an architecture family */
4634bool dvmCompilerArchInit()
4635{
4636 int i;
4637
Bill Buzbee1465db52009-09-23 17:17:35 -07004638 for (i = 0; i < kArmLast; i++) {
Dan Bornstein9a1f8162010-12-01 17:02:26 -08004639 if (EncodingMap[i].opcode != i) {
Ben Chengd7d426a2009-09-22 11:23:36 -07004640 LOGE("Encoding order for %s is wrong: expecting %d, seeing %d",
Dan Bornstein9a1f8162010-12-01 17:02:26 -08004641 EncodingMap[i].name, i, EncodingMap[i].opcode);
Bill Buzbeefc519dc2010-03-06 23:30:57 -08004642 dvmAbort(); // OK to dvmAbort - build error
Ben Chengd7d426a2009-09-22 11:23:36 -07004643 }
4644 }
4645
Ben Cheng5d90c202009-11-22 23:31:11 -08004646 return dvmCompilerArchVariantInit();
4647}
4648
4649void *dvmCompilerGetInterpretTemplate()
4650{
4651 return (void*) ((int)gDvmJit.codeCache +
4652 templateEntryOffsets[TEMPLATE_INTERPRET]);
4653}
4654
Bill Buzbee1b3da592011-02-03 07:38:22 -08004655JitInstructionSetType dvmCompilerGetInterpretTemplateSet()
4656{
4657 return DALVIK_JIT_ARM;
4658}
4659
buzbeebff121a2010-08-04 15:25:06 -07004660/* Needed by the Assembler */
4661void dvmCompilerSetupResourceMasks(ArmLIR *lir)
4662{
4663 setupResourceMasks(lir);
4664}
4665
Ben Cheng5d90c202009-11-22 23:31:11 -08004666/* Needed by the ld/st optmizatons */
4667ArmLIR* dvmCompilerRegCopyNoInsert(CompilationUnit *cUnit, int rDest, int rSrc)
4668{
4669 return genRegCopyNoInsert(cUnit, rDest, rSrc);
4670}
4671
4672/* Needed by the register allocator */
4673ArmLIR* dvmCompilerRegCopy(CompilationUnit *cUnit, int rDest, int rSrc)
4674{
4675 return genRegCopy(cUnit, rDest, rSrc);
4676}
4677
4678/* Needed by the register allocator */
4679void dvmCompilerRegCopyWide(CompilationUnit *cUnit, int destLo, int destHi,
4680 int srcLo, int srcHi)
4681{
4682 genRegCopyWide(cUnit, destLo, destHi, srcLo, srcHi);
4683}
4684
4685void dvmCompilerFlushRegImpl(CompilationUnit *cUnit, int rBase,
4686 int displacement, int rSrc, OpSize size)
4687{
4688 storeBaseDisp(cUnit, rBase, displacement, rSrc, size);
4689}
4690
4691void dvmCompilerFlushRegWideImpl(CompilationUnit *cUnit, int rBase,
4692 int displacement, int rSrcLo, int rSrcHi)
4693{
4694 storeBaseDispWide(cUnit, rBase, displacement, rSrcLo, rSrcHi);
Ben Chengd7d426a2009-09-22 11:23:36 -07004695}