blob: e4c45367c142bcf0e421da23f0ef693e6b2f161a [file] [log] [blame]
Ben Chengba4fc8b2009-06-01 13:00:29 -07001/*
2 * Copyright (C) 2009 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Bill Buzbee50a6bf22009-07-08 13:08:04 -070017/*
18 * This file contains codegen and support common to all supported
19 * ARM variants. It is included by:
20 *
21 * Codegen-$(TARGET_ARCH_VARIANT).c
22 *
23 * which combines this common code with specific support found in the
24 * applicable directory below this one.
25 */
26
buzbee919eb062010-07-12 12:59:22 -070027/*
28 * Mark garbage collection card. Skip if the value we're storing is null.
29 */
30static void markCard(CompilationUnit *cUnit, int valReg, int tgtAddrReg)
31{
32 int regCardBase = dvmCompilerAllocTemp(cUnit);
33 int regCardNo = dvmCompilerAllocTemp(cUnit);
buzbee8f8109a2010-08-31 10:16:35 -070034 ArmLIR *branchOver = genCmpImmBranch(cUnit, kArmCondEq, valReg, 0);
Ben Cheng20d7e6c2011-02-18 17:12:42 -080035 loadWordDisp(cUnit, r6SELF, offsetof(Thread, cardTable),
buzbee919eb062010-07-12 12:59:22 -070036 regCardBase);
37 opRegRegImm(cUnit, kOpLsr, regCardNo, tgtAddrReg, GC_CARD_SHIFT);
38 storeBaseIndexed(cUnit, regCardBase, regCardNo, regCardBase, 0,
39 kUnsignedByte);
40 ArmLIR *target = newLIR0(cUnit, kArmPseudoTargetLabel);
41 target->defMask = ENCODE_ALL;
42 branchOver->generic.target = (LIR *)target;
buzbeebaf196a2010-08-04 10:13:15 -070043 dvmCompilerFreeTemp(cUnit, regCardBase);
44 dvmCompilerFreeTemp(cUnit, regCardNo);
buzbee919eb062010-07-12 12:59:22 -070045}
46
Ben Cheng5d90c202009-11-22 23:31:11 -080047static bool genConversionCall(CompilationUnit *cUnit, MIR *mir, void *funct,
48 int srcSize, int tgtSize)
49{
50 /*
51 * Don't optimize the register usage since it calls out to template
52 * functions
53 */
54 RegLocation rlSrc;
55 RegLocation rlDest;
Bill Buzbeec6f10662010-02-09 11:16:15 -080056 dvmCompilerFlushAllRegs(cUnit); /* Send everything to home location */
Ben Cheng5d90c202009-11-22 23:31:11 -080057 if (srcSize == 1) {
Bill Buzbeec6f10662010-02-09 11:16:15 -080058 rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
Ben Cheng5d90c202009-11-22 23:31:11 -080059 loadValueDirectFixed(cUnit, rlSrc, r0);
60 } else {
Bill Buzbeec6f10662010-02-09 11:16:15 -080061 rlSrc = dvmCompilerGetSrcWide(cUnit, mir, 0, 1);
Ben Cheng5d90c202009-11-22 23:31:11 -080062 loadValueDirectWideFixed(cUnit, rlSrc, r0, r1);
63 }
Ben Chengbd1326d2010-04-02 15:04:53 -070064 LOAD_FUNC_ADDR(cUnit, r2, (int)funct);
Ben Cheng5d90c202009-11-22 23:31:11 -080065 opReg(cUnit, kOpBlx, r2);
Elliott Hughes6a555132010-02-25 15:41:42 -080066 dvmCompilerClobberCallRegs(cUnit);
Ben Cheng5d90c202009-11-22 23:31:11 -080067 if (tgtSize == 1) {
68 RegLocation rlResult;
Bill Buzbeec6f10662010-02-09 11:16:15 -080069 rlDest = dvmCompilerGetDest(cUnit, mir, 0);
70 rlResult = dvmCompilerGetReturn(cUnit);
Ben Cheng5d90c202009-11-22 23:31:11 -080071 storeValue(cUnit, rlDest, rlResult);
72 } else {
73 RegLocation rlResult;
Bill Buzbeec6f10662010-02-09 11:16:15 -080074 rlDest = dvmCompilerGetDestWide(cUnit, mir, 0, 1);
75 rlResult = dvmCompilerGetReturnWide(cUnit);
Ben Cheng5d90c202009-11-22 23:31:11 -080076 storeValueWide(cUnit, rlDest, rlResult);
77 }
78 return false;
79}
Ben Chengba4fc8b2009-06-01 13:00:29 -070080
Ben Cheng5d90c202009-11-22 23:31:11 -080081static bool genArithOpFloatPortable(CompilationUnit *cUnit, MIR *mir,
82 RegLocation rlDest, RegLocation rlSrc1,
83 RegLocation rlSrc2)
84{
85 RegLocation rlResult;
86 void* funct;
87
Dan Bornstein9a1f8162010-12-01 17:02:26 -080088 switch (mir->dalvikInsn.opcode) {
Ben Cheng5d90c202009-11-22 23:31:11 -080089 case OP_ADD_FLOAT_2ADDR:
90 case OP_ADD_FLOAT:
91 funct = (void*) __aeabi_fadd;
92 break;
93 case OP_SUB_FLOAT_2ADDR:
94 case OP_SUB_FLOAT:
95 funct = (void*) __aeabi_fsub;
96 break;
97 case OP_DIV_FLOAT_2ADDR:
98 case OP_DIV_FLOAT:
99 funct = (void*) __aeabi_fdiv;
100 break;
101 case OP_MUL_FLOAT_2ADDR:
102 case OP_MUL_FLOAT:
103 funct = (void*) __aeabi_fmul;
104 break;
105 case OP_REM_FLOAT_2ADDR:
106 case OP_REM_FLOAT:
107 funct = (void*) fmodf;
108 break;
109 case OP_NEG_FLOAT: {
110 genNegFloat(cUnit, rlDest, rlSrc1);
111 return false;
112 }
113 default:
114 return true;
115 }
Bill Buzbeec6f10662010-02-09 11:16:15 -0800116 dvmCompilerFlushAllRegs(cUnit); /* Send everything to home location */
Ben Cheng5d90c202009-11-22 23:31:11 -0800117 loadValueDirectFixed(cUnit, rlSrc1, r0);
118 loadValueDirectFixed(cUnit, rlSrc2, r1);
Ben Chengbd1326d2010-04-02 15:04:53 -0700119 LOAD_FUNC_ADDR(cUnit, r2, (int)funct);
Ben Cheng5d90c202009-11-22 23:31:11 -0800120 opReg(cUnit, kOpBlx, r2);
Elliott Hughes6a555132010-02-25 15:41:42 -0800121 dvmCompilerClobberCallRegs(cUnit);
Bill Buzbeec6f10662010-02-09 11:16:15 -0800122 rlResult = dvmCompilerGetReturn(cUnit);
Ben Cheng5d90c202009-11-22 23:31:11 -0800123 storeValue(cUnit, rlDest, rlResult);
124 return false;
125}
126
127static bool genArithOpDoublePortable(CompilationUnit *cUnit, MIR *mir,
128 RegLocation rlDest, RegLocation rlSrc1,
129 RegLocation rlSrc2)
130{
131 RegLocation rlResult;
132 void* funct;
133
Dan Bornstein9a1f8162010-12-01 17:02:26 -0800134 switch (mir->dalvikInsn.opcode) {
Ben Cheng5d90c202009-11-22 23:31:11 -0800135 case OP_ADD_DOUBLE_2ADDR:
136 case OP_ADD_DOUBLE:
137 funct = (void*) __aeabi_dadd;
138 break;
139 case OP_SUB_DOUBLE_2ADDR:
140 case OP_SUB_DOUBLE:
141 funct = (void*) __aeabi_dsub;
142 break;
143 case OP_DIV_DOUBLE_2ADDR:
144 case OP_DIV_DOUBLE:
145 funct = (void*) __aeabi_ddiv;
146 break;
147 case OP_MUL_DOUBLE_2ADDR:
148 case OP_MUL_DOUBLE:
149 funct = (void*) __aeabi_dmul;
150 break;
151 case OP_REM_DOUBLE_2ADDR:
152 case OP_REM_DOUBLE:
153 funct = (void*) fmod;
154 break;
155 case OP_NEG_DOUBLE: {
156 genNegDouble(cUnit, rlDest, rlSrc1);
157 return false;
158 }
159 default:
160 return true;
161 }
Bill Buzbeec6f10662010-02-09 11:16:15 -0800162 dvmCompilerFlushAllRegs(cUnit); /* Send everything to home location */
Ben Cheng20d7e6c2011-02-18 17:12:42 -0800163 LOAD_FUNC_ADDR(cUnit, r14lr, (int)funct);
Ben Cheng5d90c202009-11-22 23:31:11 -0800164 loadValueDirectWideFixed(cUnit, rlSrc1, r0, r1);
165 loadValueDirectWideFixed(cUnit, rlSrc2, r2, r3);
Ben Cheng20d7e6c2011-02-18 17:12:42 -0800166 opReg(cUnit, kOpBlx, r14lr);
Elliott Hughes6a555132010-02-25 15:41:42 -0800167 dvmCompilerClobberCallRegs(cUnit);
Bill Buzbeec6f10662010-02-09 11:16:15 -0800168 rlResult = dvmCompilerGetReturnWide(cUnit);
Ben Cheng5d90c202009-11-22 23:31:11 -0800169 storeValueWide(cUnit, rlDest, rlResult);
Ben Chengd72564c2011-02-08 17:09:25 -0800170#if defined(WITH_SELF_VERIFICATION)
171 cUnit->usesLinkRegister = true;
172#endif
Ben Cheng5d90c202009-11-22 23:31:11 -0800173 return false;
174}
175
176static bool genConversionPortable(CompilationUnit *cUnit, MIR *mir)
177{
Dan Bornstein9a1f8162010-12-01 17:02:26 -0800178 Opcode opcode = mir->dalvikInsn.opcode;
Ben Cheng5d90c202009-11-22 23:31:11 -0800179
Dan Bornstein9a1f8162010-12-01 17:02:26 -0800180 switch (opcode) {
Ben Cheng5d90c202009-11-22 23:31:11 -0800181 case OP_INT_TO_FLOAT:
182 return genConversionCall(cUnit, mir, (void*)__aeabi_i2f, 1, 1);
183 case OP_FLOAT_TO_INT:
184 return genConversionCall(cUnit, mir, (void*)__aeabi_f2iz, 1, 1);
185 case OP_DOUBLE_TO_FLOAT:
186 return genConversionCall(cUnit, mir, (void*)__aeabi_d2f, 2, 1);
187 case OP_FLOAT_TO_DOUBLE:
188 return genConversionCall(cUnit, mir, (void*)__aeabi_f2d, 1, 2);
189 case OP_INT_TO_DOUBLE:
190 return genConversionCall(cUnit, mir, (void*)__aeabi_i2d, 1, 2);
191 case OP_DOUBLE_TO_INT:
192 return genConversionCall(cUnit, mir, (void*)__aeabi_d2iz, 2, 1);
193 case OP_FLOAT_TO_LONG:
194 return genConversionCall(cUnit, mir, (void*)dvmJitf2l, 1, 2);
195 case OP_LONG_TO_FLOAT:
196 return genConversionCall(cUnit, mir, (void*)__aeabi_l2f, 2, 1);
197 case OP_DOUBLE_TO_LONG:
198 return genConversionCall(cUnit, mir, (void*)dvmJitd2l, 2, 2);
199 case OP_LONG_TO_DOUBLE:
200 return genConversionCall(cUnit, mir, (void*)__aeabi_l2d, 2, 2);
201 default:
202 return true;
203 }
204 return false;
205}
Ben Chengba4fc8b2009-06-01 13:00:29 -0700206
Jeff Hao97319a82009-08-12 16:57:15 -0700207#if defined(WITH_SELF_VERIFICATION)
Dan Bornstein9a1f8162010-12-01 17:02:26 -0800208static void selfVerificationBranchInsert(LIR *currentLIR, ArmOpcode opcode,
jeffhao9e45c0b2010-02-03 10:24:05 -0800209 int dest, int src1)
Jeff Hao97319a82009-08-12 16:57:15 -0700210{
Carl Shapirofc75f3e2010-12-07 11:43:38 -0800211 ArmLIR *insn = (ArmLIR *) dvmCompilerNew(sizeof(ArmLIR), true);
Dan Bornstein9a1f8162010-12-01 17:02:26 -0800212 insn->opcode = opcode;
jeffhao9e45c0b2010-02-03 10:24:05 -0800213 insn->operands[0] = dest;
214 insn->operands[1] = src1;
215 setupResourceMasks(insn);
216 dvmCompilerInsertLIRBefore(currentLIR, (LIR *) insn);
Jeff Hao97319a82009-08-12 16:57:15 -0700217}
218
Ben Chengd72564c2011-02-08 17:09:25 -0800219/*
220 * Example where r14 (LR) is preserved around a heap access under
221 * self-verification mode in Thumb2:
222 *
Ben Cheng20d7e6c2011-02-18 17:12:42 -0800223 * D/dalvikvm( 1538): 0x59414c5e (0026): ldr r14, [r15pc, #220] <-hoisted
Ben Chengd72564c2011-02-08 17:09:25 -0800224 * D/dalvikvm( 1538): 0x59414c62 (002a): mla r4, r0, r8, r4
225 * D/dalvikvm( 1538): 0x59414c66 (002e): adds r3, r4, r3
226 * D/dalvikvm( 1538): 0x59414c6a (0032): push <r5, r14> ---+
227 * D/dalvikvm( 1538): 0x59414c6c (0034): blx_1 0x5940f494 |
228 * D/dalvikvm( 1538): 0x59414c6e (0036): blx_2 see above <-MEM_OP_DECODE
229 * D/dalvikvm( 1538): 0x59414c70 (0038): ldr r10, [r9, #0] |
230 * D/dalvikvm( 1538): 0x59414c74 (003c): pop <r5, r14> ---+
231 * D/dalvikvm( 1538): 0x59414c78 (0040): mov r11, r10
232 * D/dalvikvm( 1538): 0x59414c7a (0042): asr r12, r11, #31
233 * D/dalvikvm( 1538): 0x59414c7e (0046): movs r0, r2
234 * D/dalvikvm( 1538): 0x59414c80 (0048): movs r1, r3
235 * D/dalvikvm( 1538): 0x59414c82 (004a): str r2, [r5, #16]
236 * D/dalvikvm( 1538): 0x59414c84 (004c): mov r2, r11
237 * D/dalvikvm( 1538): 0x59414c86 (004e): str r3, [r5, #20]
238 * D/dalvikvm( 1538): 0x59414c88 (0050): mov r3, r12
239 * D/dalvikvm( 1538): 0x59414c8a (0052): str r11, [r5, #24]
240 * D/dalvikvm( 1538): 0x59414c8e (0056): str r12, [r5, #28]
241 * D/dalvikvm( 1538): 0x59414c92 (005a): blx r14 <-use of LR
242 *
243 */
jeffhao9e45c0b2010-02-03 10:24:05 -0800244static void selfVerificationBranchInsertPass(CompilationUnit *cUnit)
Jeff Hao97319a82009-08-12 16:57:15 -0700245{
jeffhao9e45c0b2010-02-03 10:24:05 -0800246 ArmLIR *thisLIR;
Dan Bornstein9a1f8162010-12-01 17:02:26 -0800247 TemplateOpcode opcode = TEMPLATE_MEM_OP_DECODE;
Jeff Hao97319a82009-08-12 16:57:15 -0700248
jeffhao9e45c0b2010-02-03 10:24:05 -0800249 for (thisLIR = (ArmLIR *) cUnit->firstLIRInsn;
250 thisLIR != (ArmLIR *) cUnit->lastLIRInsn;
251 thisLIR = NEXT_LIR(thisLIR)) {
Ben Chengd72564c2011-02-08 17:09:25 -0800252 if (!thisLIR->flags.isNop && thisLIR->flags.insertWrapper) {
253 /*
254 * Push r5(FP) and r14(LR) onto stack. We need to make sure that
255 * SP is 8-byte aligned, and we use r5 as a temp to restore LR
256 * for Thumb-only target since LR cannot be directly accessed in
257 * Thumb mode. Another reason to choose r5 here is it is the Dalvik
258 * frame pointer and cannot be the target of the emulated heap
259 * load.
260 */
261 if (cUnit->usesLinkRegister) {
262 genSelfVerificationPreBranch(cUnit, thisLIR);
263 }
264
jeffhao9e45c0b2010-02-03 10:24:05 -0800265 /* Branch to mem op decode template */
266 selfVerificationBranchInsert((LIR *) thisLIR, kThumbBlx1,
Dan Bornstein9a1f8162010-12-01 17:02:26 -0800267 (int) gDvmJit.codeCache + templateEntryOffsets[opcode],
268 (int) gDvmJit.codeCache + templateEntryOffsets[opcode]);
jeffhao9e45c0b2010-02-03 10:24:05 -0800269 selfVerificationBranchInsert((LIR *) thisLIR, kThumbBlx2,
Dan Bornstein9a1f8162010-12-01 17:02:26 -0800270 (int) gDvmJit.codeCache + templateEntryOffsets[opcode],
271 (int) gDvmJit.codeCache + templateEntryOffsets[opcode]);
Ben Chengd72564c2011-02-08 17:09:25 -0800272
273 /* Restore LR */
274 if (cUnit->usesLinkRegister) {
275 genSelfVerificationPostBranch(cUnit, thisLIR);
276 }
Jeff Hao97319a82009-08-12 16:57:15 -0700277 }
278 }
Jeff Hao97319a82009-08-12 16:57:15 -0700279}
Jeff Hao97319a82009-08-12 16:57:15 -0700280#endif
281
Bill Buzbeebe6534f2010-03-12 16:01:35 -0800282/* Generate conditional branch instructions */
283static ArmLIR *genConditionalBranch(CompilationUnit *cUnit,
284 ArmConditionCode cond,
285 ArmLIR *target)
286{
287 ArmLIR *branch = opCondBranch(cUnit, cond);
288 branch->generic.target = (LIR *) target;
289 return branch;
290}
291
Ben Chengba4fc8b2009-06-01 13:00:29 -0700292/* Generate a unconditional branch to go to the interpreter */
Bill Buzbee89efc3d2009-07-28 11:22:22 -0700293static inline ArmLIR *genTrap(CompilationUnit *cUnit, int dOffset,
294 ArmLIR *pcrLabel)
Ben Chengba4fc8b2009-06-01 13:00:29 -0700295{
Bill Buzbee1465db52009-09-23 17:17:35 -0700296 ArmLIR *branch = opNone(cUnit, kOpUncondBr);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700297 return genCheckCommon(cUnit, dOffset, branch, pcrLabel);
298}
299
300/* Load a wide field from an object instance */
301static void genIGetWide(CompilationUnit *cUnit, MIR *mir, int fieldOffset)
302{
Bill Buzbeec6f10662010-02-09 11:16:15 -0800303 RegLocation rlObj = dvmCompilerGetSrc(cUnit, mir, 0);
304 RegLocation rlDest = dvmCompilerGetDestWide(cUnit, mir, 0, 1);
Bill Buzbee1465db52009-09-23 17:17:35 -0700305 RegLocation rlResult;
306 rlObj = loadValue(cUnit, rlObj, kCoreReg);
Bill Buzbeec6f10662010-02-09 11:16:15 -0800307 int regPtr = dvmCompilerAllocTemp(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700308
Bill Buzbee1465db52009-09-23 17:17:35 -0700309 assert(rlDest.wide);
Ben Chenge9695e52009-06-16 16:11:47 -0700310
Bill Buzbee1465db52009-09-23 17:17:35 -0700311 genNullCheck(cUnit, rlObj.sRegLow, rlObj.lowReg, mir->offset,
312 NULL);/* null object? */
313 opRegRegImm(cUnit, kOpAdd, regPtr, rlObj.lowReg, fieldOffset);
Bill Buzbeec6f10662010-02-09 11:16:15 -0800314 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kAnyReg, true);
Ben Cheng11d8f142010-03-24 15:24:19 -0700315
316 HEAP_ACCESS_SHADOW(true);
Bill Buzbee1465db52009-09-23 17:17:35 -0700317 loadPair(cUnit, regPtr, rlResult.lowReg, rlResult.highReg);
Ben Cheng11d8f142010-03-24 15:24:19 -0700318 HEAP_ACCESS_SHADOW(false);
319
Bill Buzbeec6f10662010-02-09 11:16:15 -0800320 dvmCompilerFreeTemp(cUnit, regPtr);
Bill Buzbee1465db52009-09-23 17:17:35 -0700321 storeValueWide(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700322}
323
324/* Store a wide field to an object instance */
325static void genIPutWide(CompilationUnit *cUnit, MIR *mir, int fieldOffset)
326{
Bill Buzbeec6f10662010-02-09 11:16:15 -0800327 RegLocation rlSrc = dvmCompilerGetSrcWide(cUnit, mir, 0, 1);
328 RegLocation rlObj = dvmCompilerGetSrc(cUnit, mir, 2);
Bill Buzbee1465db52009-09-23 17:17:35 -0700329 rlObj = loadValue(cUnit, rlObj, kCoreReg);
330 int regPtr;
331 rlSrc = loadValueWide(cUnit, rlSrc, kAnyReg);
332 genNullCheck(cUnit, rlObj.sRegLow, rlObj.lowReg, mir->offset,
333 NULL);/* null object? */
Bill Buzbeec6f10662010-02-09 11:16:15 -0800334 regPtr = dvmCompilerAllocTemp(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -0700335 opRegRegImm(cUnit, kOpAdd, regPtr, rlObj.lowReg, fieldOffset);
Ben Cheng11d8f142010-03-24 15:24:19 -0700336
337 HEAP_ACCESS_SHADOW(true);
Bill Buzbee1465db52009-09-23 17:17:35 -0700338 storePair(cUnit, regPtr, rlSrc.lowReg, rlSrc.highReg);
Ben Cheng11d8f142010-03-24 15:24:19 -0700339 HEAP_ACCESS_SHADOW(false);
340
Bill Buzbeec6f10662010-02-09 11:16:15 -0800341 dvmCompilerFreeTemp(cUnit, regPtr);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700342}
343
344/*
345 * Load a field from an object instance
346 *
Ben Chengba4fc8b2009-06-01 13:00:29 -0700347 */
Bill Buzbee270c1d62009-08-13 16:58:07 -0700348static void genIGet(CompilationUnit *cUnit, MIR *mir, OpSize size,
buzbeeecf8f6e2010-07-20 14:53:42 -0700349 int fieldOffset, bool isVolatile)
Ben Chengba4fc8b2009-06-01 13:00:29 -0700350{
Bill Buzbee1465db52009-09-23 17:17:35 -0700351 RegLocation rlResult;
Bill Buzbee749e8162010-07-07 06:55:56 -0700352 RegisterClass regClass = dvmCompilerRegClassBySize(size);
Bill Buzbeec6f10662010-02-09 11:16:15 -0800353 RegLocation rlObj = dvmCompilerGetSrc(cUnit, mir, 0);
354 RegLocation rlDest = dvmCompilerGetDest(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -0700355 rlObj = loadValue(cUnit, rlObj, kCoreReg);
Bill Buzbee749e8162010-07-07 06:55:56 -0700356 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, regClass, true);
Bill Buzbee1465db52009-09-23 17:17:35 -0700357 genNullCheck(cUnit, rlObj.sRegLow, rlObj.lowReg, mir->offset,
358 NULL);/* null object? */
Ben Cheng11d8f142010-03-24 15:24:19 -0700359
360 HEAP_ACCESS_SHADOW(true);
Ben Cheng5d90c202009-11-22 23:31:11 -0800361 loadBaseDisp(cUnit, mir, rlObj.lowReg, fieldOffset, rlResult.lowReg,
362 size, rlObj.sRegLow);
Ben Cheng11d8f142010-03-24 15:24:19 -0700363 HEAP_ACCESS_SHADOW(false);
buzbeeecf8f6e2010-07-20 14:53:42 -0700364 if (isVolatile) {
buzbee2ce33c92010-11-01 15:53:27 -0700365 dvmCompilerGenMemBarrier(cUnit, kSY);
buzbeeecf8f6e2010-07-20 14:53:42 -0700366 }
Ben Cheng11d8f142010-03-24 15:24:19 -0700367
Bill Buzbee1465db52009-09-23 17:17:35 -0700368 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700369}
370
371/*
372 * Store a field to an object instance
373 *
Ben Chengba4fc8b2009-06-01 13:00:29 -0700374 */
Bill Buzbee270c1d62009-08-13 16:58:07 -0700375static void genIPut(CompilationUnit *cUnit, MIR *mir, OpSize size,
buzbeeecf8f6e2010-07-20 14:53:42 -0700376 int fieldOffset, bool isObject, bool isVolatile)
Ben Chengba4fc8b2009-06-01 13:00:29 -0700377{
Bill Buzbee749e8162010-07-07 06:55:56 -0700378 RegisterClass regClass = dvmCompilerRegClassBySize(size);
Bill Buzbeec6f10662010-02-09 11:16:15 -0800379 RegLocation rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
380 RegLocation rlObj = dvmCompilerGetSrc(cUnit, mir, 1);
Bill Buzbee1465db52009-09-23 17:17:35 -0700381 rlObj = loadValue(cUnit, rlObj, kCoreReg);
Bill Buzbee749e8162010-07-07 06:55:56 -0700382 rlSrc = loadValue(cUnit, rlSrc, regClass);
Bill Buzbee1465db52009-09-23 17:17:35 -0700383 genNullCheck(cUnit, rlObj.sRegLow, rlObj.lowReg, mir->offset,
384 NULL);/* null object? */
Ben Cheng11d8f142010-03-24 15:24:19 -0700385
buzbeeecf8f6e2010-07-20 14:53:42 -0700386 if (isVolatile) {
buzbee2ce33c92010-11-01 15:53:27 -0700387 dvmCompilerGenMemBarrier(cUnit, kSY);
buzbeeecf8f6e2010-07-20 14:53:42 -0700388 }
Ben Cheng11d8f142010-03-24 15:24:19 -0700389 HEAP_ACCESS_SHADOW(true);
Bill Buzbee1465db52009-09-23 17:17:35 -0700390 storeBaseDisp(cUnit, rlObj.lowReg, fieldOffset, rlSrc.lowReg, size);
Ben Cheng11d8f142010-03-24 15:24:19 -0700391 HEAP_ACCESS_SHADOW(false);
buzbee919eb062010-07-12 12:59:22 -0700392 if (isObject) {
393 /* NOTE: marking card based on object head */
394 markCard(cUnit, rlSrc.lowReg, rlObj.lowReg);
395 }
Ben Chengba4fc8b2009-06-01 13:00:29 -0700396}
397
398
Ben Chengba4fc8b2009-06-01 13:00:29 -0700399/*
400 * Generate array load
Ben Chengba4fc8b2009-06-01 13:00:29 -0700401 */
Bill Buzbee270c1d62009-08-13 16:58:07 -0700402static void genArrayGet(CompilationUnit *cUnit, MIR *mir, OpSize size,
Bill Buzbee1465db52009-09-23 17:17:35 -0700403 RegLocation rlArray, RegLocation rlIndex,
404 RegLocation rlDest, int scale)
Ben Chengba4fc8b2009-06-01 13:00:29 -0700405{
Bill Buzbee749e8162010-07-07 06:55:56 -0700406 RegisterClass regClass = dvmCompilerRegClassBySize(size);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700407 int lenOffset = offsetof(ArrayObject, length);
408 int dataOffset = offsetof(ArrayObject, contents);
Bill Buzbee1465db52009-09-23 17:17:35 -0700409 RegLocation rlResult;
410 rlArray = loadValue(cUnit, rlArray, kCoreReg);
411 rlIndex = loadValue(cUnit, rlIndex, kCoreReg);
412 int regPtr;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700413
414 /* null object? */
Ben Cheng4238ec22009-08-24 16:32:22 -0700415 ArmLIR * pcrLabel = NULL;
416
417 if (!(mir->OptimizationFlags & MIR_IGNORE_NULL_CHECK)) {
Bill Buzbee1465db52009-09-23 17:17:35 -0700418 pcrLabel = genNullCheck(cUnit, rlArray.sRegLow,
419 rlArray.lowReg, mir->offset, NULL);
Ben Cheng4238ec22009-08-24 16:32:22 -0700420 }
421
Bill Buzbeec6f10662010-02-09 11:16:15 -0800422 regPtr = dvmCompilerAllocTemp(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -0700423
Ben Cheng4238ec22009-08-24 16:32:22 -0700424 if (!(mir->OptimizationFlags & MIR_IGNORE_RANGE_CHECK)) {
Bill Buzbeec6f10662010-02-09 11:16:15 -0800425 int regLen = dvmCompilerAllocTemp(cUnit);
Ben Cheng4238ec22009-08-24 16:32:22 -0700426 /* Get len */
Bill Buzbee1465db52009-09-23 17:17:35 -0700427 loadWordDisp(cUnit, rlArray.lowReg, lenOffset, regLen);
428 /* regPtr -> array data */
429 opRegRegImm(cUnit, kOpAdd, regPtr, rlArray.lowReg, dataOffset);
430 genBoundsCheck(cUnit, rlIndex.lowReg, regLen, mir->offset,
431 pcrLabel);
Bill Buzbeec6f10662010-02-09 11:16:15 -0800432 dvmCompilerFreeTemp(cUnit, regLen);
Ben Cheng4238ec22009-08-24 16:32:22 -0700433 } else {
Bill Buzbee1465db52009-09-23 17:17:35 -0700434 /* regPtr -> array data */
435 opRegRegImm(cUnit, kOpAdd, regPtr, rlArray.lowReg, dataOffset);
Ben Cheng4238ec22009-08-24 16:32:22 -0700436 }
Bill Buzbee1465db52009-09-23 17:17:35 -0700437 if ((size == kLong) || (size == kDouble)) {
438 if (scale) {
Bill Buzbeec6f10662010-02-09 11:16:15 -0800439 int rNewIndex = dvmCompilerAllocTemp(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -0700440 opRegRegImm(cUnit, kOpLsl, rNewIndex, rlIndex.lowReg, scale);
441 opRegReg(cUnit, kOpAdd, regPtr, rNewIndex);
Bill Buzbeec6f10662010-02-09 11:16:15 -0800442 dvmCompilerFreeTemp(cUnit, rNewIndex);
Bill Buzbee1465db52009-09-23 17:17:35 -0700443 } else {
444 opRegReg(cUnit, kOpAdd, regPtr, rlIndex.lowReg);
445 }
Bill Buzbee749e8162010-07-07 06:55:56 -0700446 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, regClass, true);
Ben Cheng11d8f142010-03-24 15:24:19 -0700447
448 HEAP_ACCESS_SHADOW(true);
Bill Buzbee1465db52009-09-23 17:17:35 -0700449 loadPair(cUnit, regPtr, rlResult.lowReg, rlResult.highReg);
Ben Cheng11d8f142010-03-24 15:24:19 -0700450 HEAP_ACCESS_SHADOW(false);
451
Bill Buzbeec6f10662010-02-09 11:16:15 -0800452 dvmCompilerFreeTemp(cUnit, regPtr);
Bill Buzbee1465db52009-09-23 17:17:35 -0700453 storeValueWide(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700454 } else {
Bill Buzbee749e8162010-07-07 06:55:56 -0700455 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, regClass, true);
Ben Cheng11d8f142010-03-24 15:24:19 -0700456
457 HEAP_ACCESS_SHADOW(true);
Bill Buzbee1465db52009-09-23 17:17:35 -0700458 loadBaseIndexed(cUnit, regPtr, rlIndex.lowReg, rlResult.lowReg,
459 scale, size);
Ben Cheng11d8f142010-03-24 15:24:19 -0700460 HEAP_ACCESS_SHADOW(false);
461
Bill Buzbeec6f10662010-02-09 11:16:15 -0800462 dvmCompilerFreeTemp(cUnit, regPtr);
Bill Buzbee1465db52009-09-23 17:17:35 -0700463 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700464 }
465}
466
Ben Chengba4fc8b2009-06-01 13:00:29 -0700467/*
468 * Generate array store
469 *
Ben Chengba4fc8b2009-06-01 13:00:29 -0700470 */
Bill Buzbee270c1d62009-08-13 16:58:07 -0700471static void genArrayPut(CompilationUnit *cUnit, MIR *mir, OpSize size,
Bill Buzbee1465db52009-09-23 17:17:35 -0700472 RegLocation rlArray, RegLocation rlIndex,
473 RegLocation rlSrc, int scale)
Ben Chengba4fc8b2009-06-01 13:00:29 -0700474{
Bill Buzbee749e8162010-07-07 06:55:56 -0700475 RegisterClass regClass = dvmCompilerRegClassBySize(size);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700476 int lenOffset = offsetof(ArrayObject, length);
477 int dataOffset = offsetof(ArrayObject, contents);
478
Bill Buzbee1465db52009-09-23 17:17:35 -0700479 int regPtr;
480 rlArray = loadValue(cUnit, rlArray, kCoreReg);
481 rlIndex = loadValue(cUnit, rlIndex, kCoreReg);
Ben Chenge9695e52009-06-16 16:11:47 -0700482
Bill Buzbeec6f10662010-02-09 11:16:15 -0800483 if (dvmCompilerIsTemp(cUnit, rlArray.lowReg)) {
484 dvmCompilerClobber(cUnit, rlArray.lowReg);
Bill Buzbee1465db52009-09-23 17:17:35 -0700485 regPtr = rlArray.lowReg;
486 } else {
Bill Buzbeec6f10662010-02-09 11:16:15 -0800487 regPtr = dvmCompilerAllocTemp(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -0700488 genRegCopy(cUnit, regPtr, rlArray.lowReg);
489 }
Ben Chenge9695e52009-06-16 16:11:47 -0700490
Ben Cheng1efc9c52009-06-08 18:25:27 -0700491 /* null object? */
Ben Cheng4238ec22009-08-24 16:32:22 -0700492 ArmLIR * pcrLabel = NULL;
493
494 if (!(mir->OptimizationFlags & MIR_IGNORE_NULL_CHECK)) {
Bill Buzbee1465db52009-09-23 17:17:35 -0700495 pcrLabel = genNullCheck(cUnit, rlArray.sRegLow, rlArray.lowReg,
496 mir->offset, NULL);
Ben Cheng4238ec22009-08-24 16:32:22 -0700497 }
498
499 if (!(mir->OptimizationFlags & MIR_IGNORE_RANGE_CHECK)) {
Bill Buzbeec6f10662010-02-09 11:16:15 -0800500 int regLen = dvmCompilerAllocTemp(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -0700501 //NOTE: max live temps(4) here.
Ben Cheng4238ec22009-08-24 16:32:22 -0700502 /* Get len */
Bill Buzbee1465db52009-09-23 17:17:35 -0700503 loadWordDisp(cUnit, rlArray.lowReg, lenOffset, regLen);
504 /* regPtr -> array data */
505 opRegImm(cUnit, kOpAdd, regPtr, dataOffset);
506 genBoundsCheck(cUnit, rlIndex.lowReg, regLen, mir->offset,
507 pcrLabel);
Bill Buzbeec6f10662010-02-09 11:16:15 -0800508 dvmCompilerFreeTemp(cUnit, regLen);
Ben Cheng4238ec22009-08-24 16:32:22 -0700509 } else {
Bill Buzbee1465db52009-09-23 17:17:35 -0700510 /* regPtr -> array data */
511 opRegImm(cUnit, kOpAdd, regPtr, dataOffset);
Ben Cheng4238ec22009-08-24 16:32:22 -0700512 }
Bill Buzbee1465db52009-09-23 17:17:35 -0700513 /* at this point, regPtr points to array, 2 live temps */
Bill Buzbee1465db52009-09-23 17:17:35 -0700514 if ((size == kLong) || (size == kDouble)) {
515 //TODO: need specific wide routine that can handle fp regs
516 if (scale) {
Bill Buzbeec6f10662010-02-09 11:16:15 -0800517 int rNewIndex = dvmCompilerAllocTemp(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -0700518 opRegRegImm(cUnit, kOpLsl, rNewIndex, rlIndex.lowReg, scale);
519 opRegReg(cUnit, kOpAdd, regPtr, rNewIndex);
Bill Buzbeec6f10662010-02-09 11:16:15 -0800520 dvmCompilerFreeTemp(cUnit, rNewIndex);
Bill Buzbee1465db52009-09-23 17:17:35 -0700521 } else {
522 opRegReg(cUnit, kOpAdd, regPtr, rlIndex.lowReg);
523 }
Bill Buzbee749e8162010-07-07 06:55:56 -0700524 rlSrc = loadValueWide(cUnit, rlSrc, regClass);
Ben Cheng11d8f142010-03-24 15:24:19 -0700525
526 HEAP_ACCESS_SHADOW(true);
Bill Buzbee1465db52009-09-23 17:17:35 -0700527 storePair(cUnit, regPtr, rlSrc.lowReg, rlSrc.highReg);
Ben Cheng11d8f142010-03-24 15:24:19 -0700528 HEAP_ACCESS_SHADOW(false);
529
Bill Buzbeec6f10662010-02-09 11:16:15 -0800530 dvmCompilerFreeTemp(cUnit, regPtr);
Bill Buzbee270c1d62009-08-13 16:58:07 -0700531 } else {
Bill Buzbee749e8162010-07-07 06:55:56 -0700532 rlSrc = loadValue(cUnit, rlSrc, regClass);
Ben Cheng11d8f142010-03-24 15:24:19 -0700533
534 HEAP_ACCESS_SHADOW(true);
Bill Buzbee1465db52009-09-23 17:17:35 -0700535 storeBaseIndexed(cUnit, regPtr, rlIndex.lowReg, rlSrc.lowReg,
536 scale, size);
Ben Cheng11d8f142010-03-24 15:24:19 -0700537 HEAP_ACCESS_SHADOW(false);
jeffhao9e45c0b2010-02-03 10:24:05 -0800538 }
Ben Chengba4fc8b2009-06-01 13:00:29 -0700539}
540
Bill Buzbeebe6534f2010-03-12 16:01:35 -0800541/*
542 * Generate array object store
543 * Must use explicit register allocation here because of
544 * call-out to dvmCanPutArrayElement
545 */
546static void genArrayObjectPut(CompilationUnit *cUnit, MIR *mir,
547 RegLocation rlArray, RegLocation rlIndex,
548 RegLocation rlSrc, int scale)
549{
550 int lenOffset = offsetof(ArrayObject, length);
551 int dataOffset = offsetof(ArrayObject, contents);
552
553 dvmCompilerFlushAllRegs(cUnit);
554
555 int regLen = r0;
556 int regPtr = r4PC; /* Preserved across call */
557 int regArray = r1;
558 int regIndex = r7; /* Preserved across call */
559
560 loadValueDirectFixed(cUnit, rlArray, regArray);
561 loadValueDirectFixed(cUnit, rlIndex, regIndex);
562
563 /* null object? */
564 ArmLIR * pcrLabel = NULL;
565
566 if (!(mir->OptimizationFlags & MIR_IGNORE_NULL_CHECK)) {
567 pcrLabel = genNullCheck(cUnit, rlArray.sRegLow, regArray,
568 mir->offset, NULL);
569 }
570
571 if (!(mir->OptimizationFlags & MIR_IGNORE_RANGE_CHECK)) {
572 /* Get len */
573 loadWordDisp(cUnit, regArray, lenOffset, regLen);
574 /* regPtr -> array data */
575 opRegRegImm(cUnit, kOpAdd, regPtr, regArray, dataOffset);
576 genBoundsCheck(cUnit, regIndex, regLen, mir->offset,
577 pcrLabel);
578 } else {
579 /* regPtr -> array data */
580 opRegRegImm(cUnit, kOpAdd, regPtr, regArray, dataOffset);
581 }
582
583 /* Get object to store */
584 loadValueDirectFixed(cUnit, rlSrc, r0);
Ben Chengbd1326d2010-04-02 15:04:53 -0700585 LOAD_FUNC_ADDR(cUnit, r2, (int)dvmCanPutArrayElement);
Bill Buzbeebe6534f2010-03-12 16:01:35 -0800586
587 /* Are we storing null? If so, avoid check */
buzbee8f8109a2010-08-31 10:16:35 -0700588 ArmLIR *branchOver = genCmpImmBranch(cUnit, kArmCondEq, r0, 0);
Bill Buzbeebe6534f2010-03-12 16:01:35 -0800589
590 /* Make sure the types are compatible */
591 loadWordDisp(cUnit, regArray, offsetof(Object, clazz), r1);
592 loadWordDisp(cUnit, r0, offsetof(Object, clazz), r0);
593 opReg(cUnit, kOpBlx, r2);
594 dvmCompilerClobberCallRegs(cUnit);
Bill Buzbee900a3af2010-03-16 12:41:43 -0700595
596 /*
597 * Using fixed registers here, and counting on r4 and r7 being
598 * preserved across the above call. Tell the register allocation
599 * utilities about the regs we are using directly
600 */
601 dvmCompilerLockTemp(cUnit, regPtr); // r4PC
602 dvmCompilerLockTemp(cUnit, regIndex); // r7
603 dvmCompilerLockTemp(cUnit, r0);
buzbee919eb062010-07-12 12:59:22 -0700604 dvmCompilerLockTemp(cUnit, r1);
Bill Buzbee900a3af2010-03-16 12:41:43 -0700605
Bill Buzbeebe6534f2010-03-12 16:01:35 -0800606 /* Bad? - roll back and re-execute if so */
607 genRegImmCheck(cUnit, kArmCondEq, r0, 0, mir->offset, pcrLabel);
608
buzbee919eb062010-07-12 12:59:22 -0700609 /* Resume here - must reload element & array, regPtr & index preserved */
Bill Buzbeebe6534f2010-03-12 16:01:35 -0800610 loadValueDirectFixed(cUnit, rlSrc, r0);
buzbee919eb062010-07-12 12:59:22 -0700611 loadValueDirectFixed(cUnit, rlArray, r1);
Bill Buzbeebe6534f2010-03-12 16:01:35 -0800612
613 ArmLIR *target = newLIR0(cUnit, kArmPseudoTargetLabel);
614 target->defMask = ENCODE_ALL;
615 branchOver->generic.target = (LIR *) target;
616
Ben Cheng11d8f142010-03-24 15:24:19 -0700617 HEAP_ACCESS_SHADOW(true);
Bill Buzbeebe6534f2010-03-12 16:01:35 -0800618 storeBaseIndexed(cUnit, regPtr, regIndex, r0,
619 scale, kWord);
Ben Cheng11d8f142010-03-24 15:24:19 -0700620 HEAP_ACCESS_SHADOW(false);
buzbee919eb062010-07-12 12:59:22 -0700621
buzbeebaf196a2010-08-04 10:13:15 -0700622 dvmCompilerFreeTemp(cUnit, regPtr);
623 dvmCompilerFreeTemp(cUnit, regIndex);
624
buzbee919eb062010-07-12 12:59:22 -0700625 /* NOTE: marking card here based on object head */
626 markCard(cUnit, r0, r1);
Bill Buzbeebe6534f2010-03-12 16:01:35 -0800627}
628
Ben Cheng5d90c202009-11-22 23:31:11 -0800629static bool genShiftOpLong(CompilationUnit *cUnit, MIR *mir,
630 RegLocation rlDest, RegLocation rlSrc1,
631 RegLocation rlShift)
Ben Chengba4fc8b2009-06-01 13:00:29 -0700632{
Ben Chenge9695e52009-06-16 16:11:47 -0700633 /*
634 * Don't mess with the regsiters here as there is a particular calling
635 * convention to the out-of-line handler.
636 */
Bill Buzbee1465db52009-09-23 17:17:35 -0700637 RegLocation rlResult;
638
639 loadValueDirectWideFixed(cUnit, rlSrc1, r0, r1);
640 loadValueDirect(cUnit, rlShift, r2);
Dan Bornstein9a1f8162010-12-01 17:02:26 -0800641 switch( mir->dalvikInsn.opcode) {
Ben Chenge9695e52009-06-16 16:11:47 -0700642 case OP_SHL_LONG:
643 case OP_SHL_LONG_2ADDR:
644 genDispatchToHandler(cUnit, TEMPLATE_SHL_LONG);
645 break;
646 case OP_SHR_LONG:
647 case OP_SHR_LONG_2ADDR:
648 genDispatchToHandler(cUnit, TEMPLATE_SHR_LONG);
649 break;
650 case OP_USHR_LONG:
651 case OP_USHR_LONG_2ADDR:
652 genDispatchToHandler(cUnit, TEMPLATE_USHR_LONG);
653 break;
654 default:
655 return true;
656 }
Bill Buzbeec6f10662010-02-09 11:16:15 -0800657 rlResult = dvmCompilerGetReturnWide(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -0700658 storeValueWide(cUnit, rlDest, rlResult);
Ben Chenge9695e52009-06-16 16:11:47 -0700659 return false;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700660}
Ben Chenge9695e52009-06-16 16:11:47 -0700661
Ben Cheng5d90c202009-11-22 23:31:11 -0800662static bool genArithOpLong(CompilationUnit *cUnit, MIR *mir,
663 RegLocation rlDest, RegLocation rlSrc1,
664 RegLocation rlSrc2)
Ben Chengba4fc8b2009-06-01 13:00:29 -0700665{
Bill Buzbee1465db52009-09-23 17:17:35 -0700666 RegLocation rlResult;
667 OpKind firstOp = kOpBkpt;
668 OpKind secondOp = kOpBkpt;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700669 bool callOut = false;
670 void *callTgt;
671 int retReg = r0;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700672
Dan Bornstein9a1f8162010-12-01 17:02:26 -0800673 switch (mir->dalvikInsn.opcode) {
Ben Chengba4fc8b2009-06-01 13:00:29 -0700674 case OP_NOT_LONG:
Bill Buzbee1465db52009-09-23 17:17:35 -0700675 rlSrc2 = loadValueWide(cUnit, rlSrc2, kCoreReg);
Bill Buzbeec6f10662010-02-09 11:16:15 -0800676 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Bill Buzbee1465db52009-09-23 17:17:35 -0700677 opRegReg(cUnit, kOpMvn, rlResult.lowReg, rlSrc2.lowReg);
678 opRegReg(cUnit, kOpMvn, rlResult.highReg, rlSrc2.highReg);
679 storeValueWide(cUnit, rlDest, rlResult);
680 return false;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700681 break;
682 case OP_ADD_LONG:
683 case OP_ADD_LONG_2ADDR:
Bill Buzbee1465db52009-09-23 17:17:35 -0700684 firstOp = kOpAdd;
685 secondOp = kOpAdc;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700686 break;
687 case OP_SUB_LONG:
688 case OP_SUB_LONG_2ADDR:
Bill Buzbee1465db52009-09-23 17:17:35 -0700689 firstOp = kOpSub;
690 secondOp = kOpSbc;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700691 break;
692 case OP_MUL_LONG:
693 case OP_MUL_LONG_2ADDR:
Bill Buzbee1465db52009-09-23 17:17:35 -0700694 genMulLong(cUnit, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700695 return false;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700696 case OP_DIV_LONG:
697 case OP_DIV_LONG_2ADDR:
698 callOut = true;
699 retReg = r0;
700 callTgt = (void*)__aeabi_ldivmod;
701 break;
702 /* NOTE - result is in r2/r3 instead of r0/r1 */
703 case OP_REM_LONG:
704 case OP_REM_LONG_2ADDR:
705 callOut = true;
706 callTgt = (void*)__aeabi_ldivmod;
707 retReg = r2;
708 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700709 case OP_AND_LONG_2ADDR:
Bill Buzbee1465db52009-09-23 17:17:35 -0700710 case OP_AND_LONG:
711 firstOp = kOpAnd;
712 secondOp = kOpAnd;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700713 break;
714 case OP_OR_LONG:
715 case OP_OR_LONG_2ADDR:
Bill Buzbee1465db52009-09-23 17:17:35 -0700716 firstOp = kOpOr;
717 secondOp = kOpOr;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700718 break;
719 case OP_XOR_LONG:
720 case OP_XOR_LONG_2ADDR:
Bill Buzbee1465db52009-09-23 17:17:35 -0700721 firstOp = kOpXor;
722 secondOp = kOpXor;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700723 break;
Ben Chenge9695e52009-06-16 16:11:47 -0700724 case OP_NEG_LONG: {
Bill Buzbee51ecf602010-01-14 14:27:52 -0800725 //TUNING: can improve this using Thumb2 code
Bill Buzbeec6f10662010-02-09 11:16:15 -0800726 int tReg = dvmCompilerAllocTemp(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -0700727 rlSrc2 = loadValueWide(cUnit, rlSrc2, kCoreReg);
Bill Buzbeec6f10662010-02-09 11:16:15 -0800728 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Ben Chengbd1326d2010-04-02 15:04:53 -0700729 loadConstantNoClobber(cUnit, tReg, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -0700730 opRegRegReg(cUnit, kOpSub, rlResult.lowReg,
Bill Buzbee51ecf602010-01-14 14:27:52 -0800731 tReg, rlSrc2.lowReg);
732 opRegReg(cUnit, kOpSbc, tReg, rlSrc2.highReg);
733 genRegCopy(cUnit, rlResult.highReg, tReg);
Bill Buzbee1465db52009-09-23 17:17:35 -0700734 storeValueWide(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700735 return false;
Ben Chenge9695e52009-06-16 16:11:47 -0700736 }
Ben Chengba4fc8b2009-06-01 13:00:29 -0700737 default:
738 LOGE("Invalid long arith op");
Bill Buzbeefc519dc2010-03-06 23:30:57 -0800739 dvmCompilerAbort(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700740 }
741 if (!callOut) {
Bill Buzbee80cef862010-03-25 10:38:34 -0700742 genLong3Addr(cUnit, mir, firstOp, secondOp, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700743 } else {
Bill Buzbee1465db52009-09-23 17:17:35 -0700744 // Adjust return regs in to handle case of rem returning r2/r3
Bill Buzbeec6f10662010-02-09 11:16:15 -0800745 dvmCompilerFlushAllRegs(cUnit); /* Send everything to home location */
Bill Buzbee1465db52009-09-23 17:17:35 -0700746 loadValueDirectWideFixed(cUnit, rlSrc1, r0, r1);
Ben Cheng20d7e6c2011-02-18 17:12:42 -0800747 LOAD_FUNC_ADDR(cUnit, r14lr, (int) callTgt);
Bill Buzbee1465db52009-09-23 17:17:35 -0700748 loadValueDirectWideFixed(cUnit, rlSrc2, r2, r3);
Ben Cheng20d7e6c2011-02-18 17:12:42 -0800749 opReg(cUnit, kOpBlx, r14lr);
Elliott Hughes6a555132010-02-25 15:41:42 -0800750 dvmCompilerClobberCallRegs(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -0700751 if (retReg == r0)
Bill Buzbeec6f10662010-02-09 11:16:15 -0800752 rlResult = dvmCompilerGetReturnWide(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -0700753 else
Bill Buzbeec6f10662010-02-09 11:16:15 -0800754 rlResult = dvmCompilerGetReturnWideAlt(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -0700755 storeValueWide(cUnit, rlDest, rlResult);
Ben Chengd72564c2011-02-08 17:09:25 -0800756#if defined(WITH_SELF_VERIFICATION)
757 cUnit->usesLinkRegister = true;
758#endif
Ben Chengba4fc8b2009-06-01 13:00:29 -0700759 }
760 return false;
761}
762
Ben Cheng5d90c202009-11-22 23:31:11 -0800763static bool genArithOpInt(CompilationUnit *cUnit, MIR *mir,
764 RegLocation rlDest, RegLocation rlSrc1,
765 RegLocation rlSrc2)
Ben Chengba4fc8b2009-06-01 13:00:29 -0700766{
Bill Buzbee1465db52009-09-23 17:17:35 -0700767 OpKind op = kOpBkpt;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700768 bool callOut = false;
769 bool checkZero = false;
Bill Buzbee1465db52009-09-23 17:17:35 -0700770 bool unary = false;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700771 int retReg = r0;
772 void *callTgt;
Bill Buzbee1465db52009-09-23 17:17:35 -0700773 RegLocation rlResult;
Bill Buzbee0e605272009-12-01 14:28:05 -0800774 bool shiftOp = false;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700775
Dan Bornstein9a1f8162010-12-01 17:02:26 -0800776 switch (mir->dalvikInsn.opcode) {
Ben Chengba4fc8b2009-06-01 13:00:29 -0700777 case OP_NEG_INT:
Bill Buzbee1465db52009-09-23 17:17:35 -0700778 op = kOpNeg;
779 unary = true;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700780 break;
781 case OP_NOT_INT:
Bill Buzbee1465db52009-09-23 17:17:35 -0700782 op = kOpMvn;
783 unary = true;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700784 break;
785 case OP_ADD_INT:
786 case OP_ADD_INT_2ADDR:
Bill Buzbee1465db52009-09-23 17:17:35 -0700787 op = kOpAdd;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700788 break;
789 case OP_SUB_INT:
790 case OP_SUB_INT_2ADDR:
Bill Buzbee1465db52009-09-23 17:17:35 -0700791 op = kOpSub;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700792 break;
793 case OP_MUL_INT:
794 case OP_MUL_INT_2ADDR:
Bill Buzbee1465db52009-09-23 17:17:35 -0700795 op = kOpMul;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700796 break;
797 case OP_DIV_INT:
798 case OP_DIV_INT_2ADDR:
799 callOut = true;
800 checkZero = true;
801 callTgt = __aeabi_idiv;
802 retReg = r0;
803 break;
804 /* NOTE: returns in r1 */
805 case OP_REM_INT:
806 case OP_REM_INT_2ADDR:
807 callOut = true;
808 checkZero = true;
809 callTgt = __aeabi_idivmod;
810 retReg = r1;
811 break;
812 case OP_AND_INT:
813 case OP_AND_INT_2ADDR:
Bill Buzbee1465db52009-09-23 17:17:35 -0700814 op = kOpAnd;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700815 break;
816 case OP_OR_INT:
817 case OP_OR_INT_2ADDR:
Bill Buzbee1465db52009-09-23 17:17:35 -0700818 op = kOpOr;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700819 break;
820 case OP_XOR_INT:
821 case OP_XOR_INT_2ADDR:
Bill Buzbee1465db52009-09-23 17:17:35 -0700822 op = kOpXor;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700823 break;
824 case OP_SHL_INT:
825 case OP_SHL_INT_2ADDR:
Bill Buzbee0e605272009-12-01 14:28:05 -0800826 shiftOp = true;
Bill Buzbee1465db52009-09-23 17:17:35 -0700827 op = kOpLsl;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700828 break;
829 case OP_SHR_INT:
830 case OP_SHR_INT_2ADDR:
Bill Buzbee0e605272009-12-01 14:28:05 -0800831 shiftOp = true;
Bill Buzbee1465db52009-09-23 17:17:35 -0700832 op = kOpAsr;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700833 break;
834 case OP_USHR_INT:
835 case OP_USHR_INT_2ADDR:
Bill Buzbee0e605272009-12-01 14:28:05 -0800836 shiftOp = true;
Bill Buzbee1465db52009-09-23 17:17:35 -0700837 op = kOpLsr;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700838 break;
839 default:
840 LOGE("Invalid word arith op: 0x%x(%d)",
Dan Bornstein9a1f8162010-12-01 17:02:26 -0800841 mir->dalvikInsn.opcode, mir->dalvikInsn.opcode);
Bill Buzbeefc519dc2010-03-06 23:30:57 -0800842 dvmCompilerAbort(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700843 }
844 if (!callOut) {
Bill Buzbee1465db52009-09-23 17:17:35 -0700845 rlSrc1 = loadValue(cUnit, rlSrc1, kCoreReg);
846 if (unary) {
Bill Buzbeec6f10662010-02-09 11:16:15 -0800847 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Bill Buzbee1465db52009-09-23 17:17:35 -0700848 opRegReg(cUnit, op, rlResult.lowReg,
849 rlSrc1.lowReg);
Ben Chenge9695e52009-06-16 16:11:47 -0700850 } else {
Bill Buzbee1465db52009-09-23 17:17:35 -0700851 rlSrc2 = loadValue(cUnit, rlSrc2, kCoreReg);
Bill Buzbee0e605272009-12-01 14:28:05 -0800852 if (shiftOp) {
Bill Buzbeec6f10662010-02-09 11:16:15 -0800853 int tReg = dvmCompilerAllocTemp(cUnit);
Bill Buzbee0e605272009-12-01 14:28:05 -0800854 opRegRegImm(cUnit, kOpAnd, tReg, rlSrc2.lowReg, 31);
Bill Buzbeec6f10662010-02-09 11:16:15 -0800855 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Bill Buzbee0e605272009-12-01 14:28:05 -0800856 opRegRegReg(cUnit, op, rlResult.lowReg,
857 rlSrc1.lowReg, tReg);
Bill Buzbeec6f10662010-02-09 11:16:15 -0800858 dvmCompilerFreeTemp(cUnit, tReg);
Bill Buzbee0e605272009-12-01 14:28:05 -0800859 } else {
Bill Buzbeec6f10662010-02-09 11:16:15 -0800860 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Bill Buzbee0e605272009-12-01 14:28:05 -0800861 opRegRegReg(cUnit, op, rlResult.lowReg,
862 rlSrc1.lowReg, rlSrc2.lowReg);
863 }
Ben Chenge9695e52009-06-16 16:11:47 -0700864 }
Bill Buzbee1465db52009-09-23 17:17:35 -0700865 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700866 } else {
Bill Buzbee1465db52009-09-23 17:17:35 -0700867 RegLocation rlResult;
Bill Buzbeec6f10662010-02-09 11:16:15 -0800868 dvmCompilerFlushAllRegs(cUnit); /* Send everything to home location */
Bill Buzbee1465db52009-09-23 17:17:35 -0700869 loadValueDirectFixed(cUnit, rlSrc2, r1);
Ben Chengbd1326d2010-04-02 15:04:53 -0700870 LOAD_FUNC_ADDR(cUnit, r2, (int) callTgt);
Bill Buzbee1465db52009-09-23 17:17:35 -0700871 loadValueDirectFixed(cUnit, rlSrc1, r0);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700872 if (checkZero) {
Bill Buzbee1465db52009-09-23 17:17:35 -0700873 genNullCheck(cUnit, rlSrc2.sRegLow, r1, mir->offset, NULL);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700874 }
Bill Buzbee1465db52009-09-23 17:17:35 -0700875 opReg(cUnit, kOpBlx, r2);
Elliott Hughes6a555132010-02-25 15:41:42 -0800876 dvmCompilerClobberCallRegs(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -0700877 if (retReg == r0)
Bill Buzbeec6f10662010-02-09 11:16:15 -0800878 rlResult = dvmCompilerGetReturn(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -0700879 else
Bill Buzbeec6f10662010-02-09 11:16:15 -0800880 rlResult = dvmCompilerGetReturnAlt(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -0700881 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700882 }
883 return false;
884}
885
Ben Cheng5d90c202009-11-22 23:31:11 -0800886static bool genArithOp(CompilationUnit *cUnit, MIR *mir)
Ben Chengba4fc8b2009-06-01 13:00:29 -0700887{
Dan Bornstein9a1f8162010-12-01 17:02:26 -0800888 Opcode opcode = mir->dalvikInsn.opcode;
Bill Buzbee1465db52009-09-23 17:17:35 -0700889 RegLocation rlDest;
890 RegLocation rlSrc1;
891 RegLocation rlSrc2;
892 /* Deduce sizes of operands */
893 if (mir->ssaRep->numUses == 2) {
Bill Buzbeec6f10662010-02-09 11:16:15 -0800894 rlSrc1 = dvmCompilerGetSrc(cUnit, mir, 0);
895 rlSrc2 = dvmCompilerGetSrc(cUnit, mir, 1);
Bill Buzbee1465db52009-09-23 17:17:35 -0700896 } else if (mir->ssaRep->numUses == 3) {
Bill Buzbeec6f10662010-02-09 11:16:15 -0800897 rlSrc1 = dvmCompilerGetSrcWide(cUnit, mir, 0, 1);
898 rlSrc2 = dvmCompilerGetSrc(cUnit, mir, 2);
Bill Buzbee1465db52009-09-23 17:17:35 -0700899 } else {
Bill Buzbeec6f10662010-02-09 11:16:15 -0800900 rlSrc1 = dvmCompilerGetSrcWide(cUnit, mir, 0, 1);
901 rlSrc2 = dvmCompilerGetSrcWide(cUnit, mir, 2, 3);
Bill Buzbee1465db52009-09-23 17:17:35 -0700902 assert(mir->ssaRep->numUses == 4);
903 }
904 if (mir->ssaRep->numDefs == 1) {
Bill Buzbeec6f10662010-02-09 11:16:15 -0800905 rlDest = dvmCompilerGetDest(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -0700906 } else {
907 assert(mir->ssaRep->numDefs == 2);
Bill Buzbeec6f10662010-02-09 11:16:15 -0800908 rlDest = dvmCompilerGetDestWide(cUnit, mir, 0, 1);
Bill Buzbee1465db52009-09-23 17:17:35 -0700909 }
Ben Chengba4fc8b2009-06-01 13:00:29 -0700910
Dan Bornstein9a1f8162010-12-01 17:02:26 -0800911 if ((opcode >= OP_ADD_LONG_2ADDR) && (opcode <= OP_XOR_LONG_2ADDR)) {
Ben Cheng5d90c202009-11-22 23:31:11 -0800912 return genArithOpLong(cUnit,mir, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700913 }
Dan Bornstein9a1f8162010-12-01 17:02:26 -0800914 if ((opcode >= OP_ADD_LONG) && (opcode <= OP_XOR_LONG)) {
Ben Cheng5d90c202009-11-22 23:31:11 -0800915 return genArithOpLong(cUnit,mir, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700916 }
Dan Bornstein9a1f8162010-12-01 17:02:26 -0800917 if ((opcode >= OP_SHL_LONG_2ADDR) && (opcode <= OP_USHR_LONG_2ADDR)) {
Ben Cheng5d90c202009-11-22 23:31:11 -0800918 return genShiftOpLong(cUnit,mir, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700919 }
Dan Bornstein9a1f8162010-12-01 17:02:26 -0800920 if ((opcode >= OP_SHL_LONG) && (opcode <= OP_USHR_LONG)) {
Ben Cheng5d90c202009-11-22 23:31:11 -0800921 return genShiftOpLong(cUnit,mir, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700922 }
Dan Bornstein9a1f8162010-12-01 17:02:26 -0800923 if ((opcode >= OP_ADD_INT_2ADDR) && (opcode <= OP_USHR_INT_2ADDR)) {
Ben Cheng5d90c202009-11-22 23:31:11 -0800924 return genArithOpInt(cUnit,mir, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700925 }
Dan Bornstein9a1f8162010-12-01 17:02:26 -0800926 if ((opcode >= OP_ADD_INT) && (opcode <= OP_USHR_INT)) {
Ben Cheng5d90c202009-11-22 23:31:11 -0800927 return genArithOpInt(cUnit,mir, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700928 }
Dan Bornstein9a1f8162010-12-01 17:02:26 -0800929 if ((opcode >= OP_ADD_FLOAT_2ADDR) && (opcode <= OP_REM_FLOAT_2ADDR)) {
Ben Cheng5d90c202009-11-22 23:31:11 -0800930 return genArithOpFloat(cUnit,mir, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700931 }
Dan Bornstein9a1f8162010-12-01 17:02:26 -0800932 if ((opcode >= OP_ADD_FLOAT) && (opcode <= OP_REM_FLOAT)) {
Ben Cheng5d90c202009-11-22 23:31:11 -0800933 return genArithOpFloat(cUnit, mir, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700934 }
Dan Bornstein9a1f8162010-12-01 17:02:26 -0800935 if ((opcode >= OP_ADD_DOUBLE_2ADDR) && (opcode <= OP_REM_DOUBLE_2ADDR)) {
Ben Cheng5d90c202009-11-22 23:31:11 -0800936 return genArithOpDouble(cUnit,mir, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700937 }
Dan Bornstein9a1f8162010-12-01 17:02:26 -0800938 if ((opcode >= OP_ADD_DOUBLE) && (opcode <= OP_REM_DOUBLE)) {
Ben Cheng5d90c202009-11-22 23:31:11 -0800939 return genArithOpDouble(cUnit,mir, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700940 }
941 return true;
942}
943
Bill Buzbee1465db52009-09-23 17:17:35 -0700944/* Generate unconditional branch instructions */
945static ArmLIR *genUnconditionalBranch(CompilationUnit *cUnit, ArmLIR *target)
946{
947 ArmLIR *branch = opNone(cUnit, kOpUncondBr);
948 branch->generic.target = (LIR *) target;
949 return branch;
950}
951
Bill Buzbee1465db52009-09-23 17:17:35 -0700952/* Perform the actual operation for OP_RETURN_* */
953static void genReturnCommon(CompilationUnit *cUnit, MIR *mir)
954{
Ben Cheng20d7e6c2011-02-18 17:12:42 -0800955 genDispatchToHandler(cUnit, gDvmJit.methodTraceSupport ?
956 TEMPLATE_RETURN_PROF : TEMPLATE_RETURN);
Ben Cheng978738d2010-05-13 13:45:57 -0700957#if defined(WITH_JIT_TUNING)
Ben Cheng20d7e6c2011-02-18 17:12:42 -0800958 gDvmJit.returnOp++;
Bill Buzbee1465db52009-09-23 17:17:35 -0700959#endif
Ben Cheng20d7e6c2011-02-18 17:12:42 -0800960 int dPC = (int) (cUnit->method->insns + mir->offset);
961 /* Insert branch, but defer setting of target */
962 ArmLIR *branch = genUnconditionalBranch(cUnit, NULL);
963 /* Set up the place holder to reconstruct this Dalvik PC */
964 ArmLIR *pcrLabel = (ArmLIR *) dvmCompilerNew(sizeof(ArmLIR), true);
965 pcrLabel->opcode = kArmPseudoPCReconstructionCell;
966 pcrLabel->operands[0] = dPC;
967 pcrLabel->operands[1] = mir->offset;
968 /* Insert the place holder to the growable list */
969 dvmInsertGrowableList(&cUnit->pcReconstructionList, (intptr_t) pcrLabel);
970 /* Branch to the PC reconstruction code */
971 branch->generic.target = (LIR *) pcrLabel;
Bill Buzbee1465db52009-09-23 17:17:35 -0700972}
973
Ben Chengba4fc8b2009-06-01 13:00:29 -0700974static void genProcessArgsNoRange(CompilationUnit *cUnit, MIR *mir,
975 DecodedInstruction *dInsn,
Bill Buzbee89efc3d2009-07-28 11:22:22 -0700976 ArmLIR **pcrLabel)
Ben Chengba4fc8b2009-06-01 13:00:29 -0700977{
978 unsigned int i;
979 unsigned int regMask = 0;
Bill Buzbee1465db52009-09-23 17:17:35 -0700980 RegLocation rlArg;
981 int numDone = 0;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700982
Bill Buzbee1465db52009-09-23 17:17:35 -0700983 /*
984 * Load arguments to r0..r4. Note that these registers may contain
985 * live values, so we clobber them immediately after loading to prevent
986 * them from being used as sources for subsequent loads.
987 */
Bill Buzbeec6f10662010-02-09 11:16:15 -0800988 dvmCompilerLockAllTemps(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700989 for (i = 0; i < dInsn->vA; i++) {
990 regMask |= 1 << i;
Bill Buzbeec6f10662010-02-09 11:16:15 -0800991 rlArg = dvmCompilerGetSrc(cUnit, mir, numDone++);
Bill Buzbee1465db52009-09-23 17:17:35 -0700992 loadValueDirectFixed(cUnit, rlArg, i);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700993 }
994 if (regMask) {
995 /* Up to 5 args are pushed on top of FP - sizeofStackSaveArea */
Ben Cheng20d7e6c2011-02-18 17:12:42 -0800996 opRegRegImm(cUnit, kOpSub, r7, r5FP,
Bill Buzbee1465db52009-09-23 17:17:35 -0700997 sizeof(StackSaveArea) + (dInsn->vA << 2));
Ben Chengba4fc8b2009-06-01 13:00:29 -0700998 /* generate null check */
999 if (pcrLabel) {
Bill Buzbeec6f10662010-02-09 11:16:15 -08001000 *pcrLabel = genNullCheck(cUnit, dvmCompilerSSASrc(mir, 0), r0,
Bill Buzbee1465db52009-09-23 17:17:35 -07001001 mir->offset, NULL);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001002 }
Bill Buzbee270c1d62009-08-13 16:58:07 -07001003 storeMultiple(cUnit, r7, regMask);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001004 }
1005}
1006
1007static void genProcessArgsRange(CompilationUnit *cUnit, MIR *mir,
1008 DecodedInstruction *dInsn,
Bill Buzbee89efc3d2009-07-28 11:22:22 -07001009 ArmLIR **pcrLabel)
Ben Chengba4fc8b2009-06-01 13:00:29 -07001010{
1011 int srcOffset = dInsn->vC << 2;
1012 int numArgs = dInsn->vA;
1013 int regMask;
Bill Buzbee1465db52009-09-23 17:17:35 -07001014
1015 /*
1016 * Note: here, all promoted registers will have been flushed
1017 * back to the Dalvik base locations, so register usage restrictins
1018 * are lifted. All parms loaded from original Dalvik register
1019 * region - even though some might conceivably have valid copies
1020 * cached in a preserved register.
1021 */
Bill Buzbeec6f10662010-02-09 11:16:15 -08001022 dvmCompilerLockAllTemps(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07001023
Ben Chengba4fc8b2009-06-01 13:00:29 -07001024 /*
Ben Cheng20d7e6c2011-02-18 17:12:42 -08001025 * r4PC : &r5FP[vC]
Ben Chengba4fc8b2009-06-01 13:00:29 -07001026 * r7: &newFP[0]
1027 */
Ben Cheng20d7e6c2011-02-18 17:12:42 -08001028 opRegRegImm(cUnit, kOpAdd, r4PC, r5FP, srcOffset);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001029 /* load [r0 .. min(numArgs,4)] */
1030 regMask = (1 << ((numArgs < 4) ? numArgs : 4)) - 1;
Ben Chengd7d426a2009-09-22 11:23:36 -07001031 /*
1032 * Protect the loadMultiple instruction from being reordered with other
1033 * Dalvik stack accesses.
jeffhao71eee1f2011-01-04 14:18:54 -08001034 *
1035 * This code is also shared by the invoke jumbo instructions, and this
1036 * does not need to be done if the invoke jumbo has no arguments.
Ben Chengd7d426a2009-09-22 11:23:36 -07001037 */
jeffhao71eee1f2011-01-04 14:18:54 -08001038 if (numArgs != 0) loadMultiple(cUnit, r4PC, regMask);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001039
Ben Cheng20d7e6c2011-02-18 17:12:42 -08001040 opRegRegImm(cUnit, kOpSub, r7, r5FP,
Bill Buzbee1465db52009-09-23 17:17:35 -07001041 sizeof(StackSaveArea) + (numArgs << 2));
Ben Chengba4fc8b2009-06-01 13:00:29 -07001042 /* generate null check */
1043 if (pcrLabel) {
Bill Buzbeec6f10662010-02-09 11:16:15 -08001044 *pcrLabel = genNullCheck(cUnit, dvmCompilerSSASrc(mir, 0), r0,
Bill Buzbee1465db52009-09-23 17:17:35 -07001045 mir->offset, NULL);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001046 }
1047
1048 /*
1049 * Handle remaining 4n arguments:
1050 * store previously loaded 4 values and load the next 4 values
1051 */
1052 if (numArgs >= 8) {
Bill Buzbee89efc3d2009-07-28 11:22:22 -07001053 ArmLIR *loopLabel = NULL;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001054 /*
1055 * r0 contains "this" and it will be used later, so push it to the stack
Ben Cheng20d7e6c2011-02-18 17:12:42 -08001056 * first. Pushing r5FP is just for stack alignment purposes.
Ben Chengba4fc8b2009-06-01 13:00:29 -07001057 */
Ben Cheng20d7e6c2011-02-18 17:12:42 -08001058 opImm(cUnit, kOpPush, (1 << r0 | 1 << r5FP));
Ben Chengba4fc8b2009-06-01 13:00:29 -07001059 /* No need to generate the loop structure if numArgs <= 11 */
1060 if (numArgs > 11) {
1061 loadConstant(cUnit, 5, ((numArgs - 4) >> 2) << 2);
Bill Buzbee1465db52009-09-23 17:17:35 -07001062 loopLabel = newLIR0(cUnit, kArmPseudoTargetLabel);
Ben Chengd7d426a2009-09-22 11:23:36 -07001063 loopLabel->defMask = ENCODE_ALL;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001064 }
Bill Buzbee270c1d62009-08-13 16:58:07 -07001065 storeMultiple(cUnit, r7, regMask);
Ben Chengd7d426a2009-09-22 11:23:36 -07001066 /*
1067 * Protect the loadMultiple instruction from being reordered with other
1068 * Dalvik stack accesses.
1069 */
Bill Buzbee270c1d62009-08-13 16:58:07 -07001070 loadMultiple(cUnit, r4PC, regMask);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001071 /* No need to generate the loop structure if numArgs <= 11 */
1072 if (numArgs > 11) {
Ben Cheng20d7e6c2011-02-18 17:12:42 -08001073 opRegImm(cUnit, kOpSub, r5FP, 4);
Bill Buzbee1465db52009-09-23 17:17:35 -07001074 genConditionalBranch(cUnit, kArmCondNe, loopLabel);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001075 }
1076 }
1077
1078 /* Save the last batch of loaded values */
jeffhao71eee1f2011-01-04 14:18:54 -08001079 if (numArgs != 0) storeMultiple(cUnit, r7, regMask);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001080
1081 /* Generate the loop epilogue - don't use r0 */
1082 if ((numArgs > 4) && (numArgs % 4)) {
1083 regMask = ((1 << (numArgs & 0x3)) - 1) << 1;
Ben Chengd7d426a2009-09-22 11:23:36 -07001084 /*
1085 * Protect the loadMultiple instruction from being reordered with other
1086 * Dalvik stack accesses.
1087 */
Bill Buzbee270c1d62009-08-13 16:58:07 -07001088 loadMultiple(cUnit, r4PC, regMask);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001089 }
1090 if (numArgs >= 8)
Ben Cheng20d7e6c2011-02-18 17:12:42 -08001091 opImm(cUnit, kOpPop, (1 << r0 | 1 << r5FP));
Ben Chengba4fc8b2009-06-01 13:00:29 -07001092
1093 /* Save the modulo 4 arguments */
1094 if ((numArgs > 4) && (numArgs % 4)) {
Bill Buzbee270c1d62009-08-13 16:58:07 -07001095 storeMultiple(cUnit, r7, regMask);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001096 }
1097}
1098
Ben Cheng38329f52009-07-07 14:19:20 -07001099/*
1100 * Generate code to setup the call stack then jump to the chaining cell if it
1101 * is not a native method.
1102 */
1103static void genInvokeSingletonCommon(CompilationUnit *cUnit, MIR *mir,
Bill Buzbee89efc3d2009-07-28 11:22:22 -07001104 BasicBlock *bb, ArmLIR *labelList,
1105 ArmLIR *pcrLabel,
Ben Cheng38329f52009-07-07 14:19:20 -07001106 const Method *calleeMethod)
Ben Chengba4fc8b2009-06-01 13:00:29 -07001107{
Bill Buzbee1465db52009-09-23 17:17:35 -07001108 /*
1109 * Note: all Dalvik register state should be flushed to
1110 * memory by the point, so register usage restrictions no
1111 * longer apply. All temp & preserved registers may be used.
1112 */
Bill Buzbeec6f10662010-02-09 11:16:15 -08001113 dvmCompilerLockAllTemps(cUnit);
Bill Buzbee89efc3d2009-07-28 11:22:22 -07001114 ArmLIR *retChainingCell = &labelList[bb->fallThrough->id];
Ben Chengba4fc8b2009-06-01 13:00:29 -07001115
1116 /* r1 = &retChainingCell */
Ben Cheng20d7e6c2011-02-18 17:12:42 -08001117 ArmLIR *addrRetChain = opRegRegImm(cUnit, kOpAdd, r1, r15pc, 0);
Ben Chengc8293e72010-10-12 11:50:10 -07001118
Ben Chengba4fc8b2009-06-01 13:00:29 -07001119 /* r4PC = dalvikCallsite */
1120 loadConstant(cUnit, r4PC,
1121 (int) (cUnit->method->insns + mir->offset));
1122 addrRetChain->generic.target = (LIR *) retChainingCell;
Ben Chengc8293e72010-10-12 11:50:10 -07001123
1124 /* r7 = calleeMethod->registersSize */
1125 loadConstant(cUnit, r7, calleeMethod->registersSize);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001126 /*
Ben Cheng38329f52009-07-07 14:19:20 -07001127 * r0 = calleeMethod (loaded upon calling genInvokeSingletonCommon)
Ben Chengba4fc8b2009-06-01 13:00:29 -07001128 * r1 = &ChainingCell
Ben Chengc8293e72010-10-12 11:50:10 -07001129 * r2 = calleeMethod->outsSize (to be loaded later for Java callees)
Ben Chengba4fc8b2009-06-01 13:00:29 -07001130 * r4PC = callsiteDPC
Ben Chengc8293e72010-10-12 11:50:10 -07001131 * r7 = calleeMethod->registersSize
Ben Chengba4fc8b2009-06-01 13:00:29 -07001132 */
1133 if (dvmIsNativeMethod(calleeMethod)) {
buzbee18fba342011-01-19 15:31:15 -08001134 genDispatchToHandler(cUnit, gDvmJit.methodTraceSupport ?
1135 TEMPLATE_INVOKE_METHOD_NATIVE_PROF :
1136 TEMPLATE_INVOKE_METHOD_NATIVE);
Ben Cheng978738d2010-05-13 13:45:57 -07001137#if defined(WITH_JIT_TUNING)
Ben Cheng38329f52009-07-07 14:19:20 -07001138 gDvmJit.invokeNative++;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001139#endif
1140 } else {
Ben Chengc8293e72010-10-12 11:50:10 -07001141 /* For Java callees, set up r2 to be calleeMethod->outsSize */
1142 loadConstant(cUnit, r2, calleeMethod->outsSize);
buzbee18fba342011-01-19 15:31:15 -08001143 genDispatchToHandler(cUnit, gDvmJit.methodTraceSupport ?
1144 TEMPLATE_INVOKE_METHOD_CHAIN_PROF :
1145 TEMPLATE_INVOKE_METHOD_CHAIN);
Ben Cheng978738d2010-05-13 13:45:57 -07001146#if defined(WITH_JIT_TUNING)
Ben Cheng86717f72010-03-05 15:27:21 -08001147 gDvmJit.invokeMonomorphic++;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001148#endif
Ben Cheng38329f52009-07-07 14:19:20 -07001149 /* Branch to the chaining cell */
Ben Chengba4fc8b2009-06-01 13:00:29 -07001150 genUnconditionalBranch(cUnit, &labelList[bb->taken->id]);
1151 }
1152 /* Handle exceptions using the interpreter */
1153 genTrap(cUnit, mir->offset, pcrLabel);
1154}
1155
Ben Cheng38329f52009-07-07 14:19:20 -07001156/*
1157 * Generate code to check the validity of a predicted chain and take actions
1158 * based on the result.
1159 *
1160 * 0x426a99aa : ldr r4, [pc, #72] --> r4 <- dalvikPC of this invoke
1161 * 0x426a99ac : add r1, pc, #32 --> r1 <- &retChainingCell
1162 * 0x426a99ae : add r2, pc, #40 --> r2 <- &predictedChainingCell
1163 * 0x426a99b0 : blx_1 0x426a918c --+ TEMPLATE_INVOKE_METHOD_PREDICTED_CHAIN
1164 * 0x426a99b2 : blx_2 see above --+
1165 * 0x426a99b4 : b 0x426a99d8 --> off to the predicted chain
1166 * 0x426a99b6 : b 0x426a99c8 --> punt to the interpreter
1167 * 0x426a99b8 : ldr r0, [r7, #44] --> r0 <- this->class->vtable[methodIdx]
1168 * 0x426a99ba : cmp r1, #0 --> compare r1 (rechain count) against 0
1169 * 0x426a99bc : bgt 0x426a99c2 --> >=0? don't rechain
Ben Chengaf5aa1f2011-01-04 15:37:04 -08001170 * 0x426a99be : ldr r7, [pc, #off]--+ dvmJitToPatchPredictedChain
Ben Cheng38329f52009-07-07 14:19:20 -07001171 * 0x426a99c0 : blx r7 --+
1172 * 0x426a99c2 : add r1, pc, #12 --> r1 <- &retChainingCell
1173 * 0x426a99c4 : blx_1 0x426a9098 --+ TEMPLATE_INVOKE_METHOD_NO_OPT
1174 * 0x426a99c6 : blx_2 see above --+
1175 */
1176static void genInvokeVirtualCommon(CompilationUnit *cUnit, MIR *mir,
1177 int methodIndex,
Bill Buzbee89efc3d2009-07-28 11:22:22 -07001178 ArmLIR *retChainingCell,
1179 ArmLIR *predChainingCell,
1180 ArmLIR *pcrLabel)
Ben Cheng38329f52009-07-07 14:19:20 -07001181{
Bill Buzbee1465db52009-09-23 17:17:35 -07001182 /*
1183 * Note: all Dalvik register state should be flushed to
1184 * memory by the point, so register usage restrictions no
1185 * longer apply. Lock temps to prevent them from being
1186 * allocated by utility routines.
1187 */
Bill Buzbeec6f10662010-02-09 11:16:15 -08001188 dvmCompilerLockAllTemps(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07001189
Ben Cheng04517042011-03-14 11:16:21 -07001190 /*
1191 * For verbose printing, store the method pointer in operands[1] first as
1192 * operands[0] will be clobbered in dvmCompilerMIR2LIR.
1193 */
1194 predChainingCell->operands[1] = (int) mir->meta.callsiteInfo->method;
1195
Ben Cheng38329f52009-07-07 14:19:20 -07001196 /* "this" is already left in r0 by genProcessArgs* */
1197
1198 /* r4PC = dalvikCallsite */
1199 loadConstant(cUnit, r4PC,
1200 (int) (cUnit->method->insns + mir->offset));
1201
1202 /* r1 = &retChainingCell */
Ben Cheng20d7e6c2011-02-18 17:12:42 -08001203 ArmLIR *addrRetChain = opRegRegImm(cUnit, kOpAdd, r1, r15pc, 0);
Ben Cheng38329f52009-07-07 14:19:20 -07001204 addrRetChain->generic.target = (LIR *) retChainingCell;
1205
1206 /* r2 = &predictedChainingCell */
Ben Cheng20d7e6c2011-02-18 17:12:42 -08001207 ArmLIR *predictedChainingCell = opRegRegImm(cUnit, kOpAdd, r2, r15pc, 0);
Ben Cheng38329f52009-07-07 14:19:20 -07001208 predictedChainingCell->generic.target = (LIR *) predChainingCell;
1209
buzbee18fba342011-01-19 15:31:15 -08001210 genDispatchToHandler(cUnit, gDvmJit.methodTraceSupport ?
1211 TEMPLATE_INVOKE_METHOD_PREDICTED_CHAIN_PROF :
1212 TEMPLATE_INVOKE_METHOD_PREDICTED_CHAIN);
Ben Cheng38329f52009-07-07 14:19:20 -07001213
1214 /* return through lr - jump to the chaining cell */
1215 genUnconditionalBranch(cUnit, predChainingCell);
1216
1217 /*
1218 * null-check on "this" may have been eliminated, but we still need a PC-
1219 * reconstruction label for stack overflow bailout.
1220 */
1221 if (pcrLabel == NULL) {
1222 int dPC = (int) (cUnit->method->insns + mir->offset);
Carl Shapirofc75f3e2010-12-07 11:43:38 -08001223 pcrLabel = (ArmLIR *) dvmCompilerNew(sizeof(ArmLIR), true);
Dan Bornstein9a1f8162010-12-01 17:02:26 -08001224 pcrLabel->opcode = kArmPseudoPCReconstructionCell;
Ben Cheng38329f52009-07-07 14:19:20 -07001225 pcrLabel->operands[0] = dPC;
1226 pcrLabel->operands[1] = mir->offset;
1227 /* Insert the place holder to the growable list */
Ben Cheng00603072010-10-28 11:13:58 -07001228 dvmInsertGrowableList(&cUnit->pcReconstructionList,
1229 (intptr_t) pcrLabel);
Ben Cheng38329f52009-07-07 14:19:20 -07001230 }
1231
1232 /* return through lr+2 - punt to the interpreter */
1233 genUnconditionalBranch(cUnit, pcrLabel);
1234
1235 /*
1236 * return through lr+4 - fully resolve the callee method.
1237 * r1 <- count
1238 * r2 <- &predictedChainCell
1239 * r3 <- this->class
1240 * r4 <- dPC
1241 * r7 <- this->class->vtable
1242 */
1243
1244 /* r0 <- calleeMethod */
Bill Buzbee270c1d62009-08-13 16:58:07 -07001245 loadWordDisp(cUnit, r7, methodIndex * 4, r0);
Ben Cheng38329f52009-07-07 14:19:20 -07001246
1247 /* Check if rechain limit is reached */
buzbee8f8109a2010-08-31 10:16:35 -07001248 ArmLIR *bypassRechaining = genCmpImmBranch(cUnit, kArmCondGt, r1, 0);
Ben Cheng38329f52009-07-07 14:19:20 -07001249
Ben Chengaf5aa1f2011-01-04 15:37:04 -08001250 LOAD_FUNC_ADDR(cUnit, r7, (int) dvmJitToPatchPredictedChain);
Ben Cheng38329f52009-07-07 14:19:20 -07001251
Ben Cheng20d7e6c2011-02-18 17:12:42 -08001252 genRegCopy(cUnit, r1, r6SELF);
Ben Chengb88ec3c2010-05-17 12:50:33 -07001253
Ben Cheng38329f52009-07-07 14:19:20 -07001254 /*
1255 * r0 = calleeMethod
1256 * r2 = &predictedChainingCell
1257 * r3 = class
1258 *
1259 * &returnChainingCell has been loaded into r1 but is not needed
1260 * when patching the chaining cell and will be clobbered upon
1261 * returning so it will be reconstructed again.
1262 */
Bill Buzbee1465db52009-09-23 17:17:35 -07001263 opReg(cUnit, kOpBlx, r7);
Ben Cheng38329f52009-07-07 14:19:20 -07001264
1265 /* r1 = &retChainingCell */
Ben Cheng20d7e6c2011-02-18 17:12:42 -08001266 addrRetChain = opRegRegImm(cUnit, kOpAdd, r1, r15pc, 0);
Ben Cheng38329f52009-07-07 14:19:20 -07001267 addrRetChain->generic.target = (LIR *) retChainingCell;
1268
1269 bypassRechaining->generic.target = (LIR *) addrRetChain;
1270 /*
1271 * r0 = calleeMethod,
1272 * r1 = &ChainingCell,
1273 * r4PC = callsiteDPC,
1274 */
buzbee18fba342011-01-19 15:31:15 -08001275 genDispatchToHandler(cUnit, gDvmJit.methodTraceSupport ?
1276 TEMPLATE_INVOKE_METHOD_NO_OPT_PROF :
1277 TEMPLATE_INVOKE_METHOD_NO_OPT);
Ben Cheng978738d2010-05-13 13:45:57 -07001278#if defined(WITH_JIT_TUNING)
Ben Cheng86717f72010-03-05 15:27:21 -08001279 gDvmJit.invokePolymorphic++;
Ben Cheng38329f52009-07-07 14:19:20 -07001280#endif
1281 /* Handle exceptions using the interpreter */
1282 genTrap(cUnit, mir->offset, pcrLabel);
1283}
1284
Ben Cheng20d7e6c2011-02-18 17:12:42 -08001285/* "this" pointer is already in r0 */
1286static void genInvokeVirtualWholeMethod(CompilationUnit *cUnit,
1287 MIR *mir,
1288 void *calleeAddr,
1289 ArmLIR *retChainingCell)
1290{
1291 CallsiteInfo *callsiteInfo = mir->meta.callsiteInfo;
1292 dvmCompilerLockAllTemps(cUnit);
1293
Ben Cheng385828e2011-03-04 16:48:33 -08001294 loadClassPointer(cUnit, r1, (int) callsiteInfo);
Ben Cheng20d7e6c2011-02-18 17:12:42 -08001295
1296 loadWordDisp(cUnit, r0, offsetof(Object, clazz), r2);
1297 /* Branch to the slow path if classes are not equal */
1298 opRegReg(cUnit, kOpCmp, r1, r2);
1299 /*
1300 * Set the misPredBranchOver target so that it will be generated when the
1301 * code for the non-optimized invoke is generated.
1302 */
1303 ArmLIR *classCheck = opCondBranch(cUnit, kArmCondNe);
1304
1305 /* r0 = the Dalvik PC of the callsite */
1306 loadConstant(cUnit, r0, (int) (cUnit->method->insns + mir->offset));
1307
1308 newLIR2(cUnit, kThumbBl1, (int) calleeAddr, (int) calleeAddr);
1309 newLIR2(cUnit, kThumbBl2, (int) calleeAddr, (int) calleeAddr);
1310 genUnconditionalBranch(cUnit, retChainingCell);
1311
1312 /* Target of slow path */
1313 ArmLIR *slowPathLabel = newLIR0(cUnit, kArmPseudoTargetLabel);
1314
1315 slowPathLabel->defMask = ENCODE_ALL;
1316 classCheck->generic.target = (LIR *) slowPathLabel;
1317
1318 // FIXME
1319 cUnit->printMe = true;
1320}
1321
1322static void genInvokeSingletonWholeMethod(CompilationUnit *cUnit,
1323 MIR *mir,
1324 void *calleeAddr,
1325 ArmLIR *retChainingCell)
1326{
1327 /* r0 = the Dalvik PC of the callsite */
1328 loadConstant(cUnit, r0, (int) (cUnit->method->insns + mir->offset));
1329
1330 newLIR2(cUnit, kThumbBl1, (int) calleeAddr, (int) calleeAddr);
1331 newLIR2(cUnit, kThumbBl2, (int) calleeAddr, (int) calleeAddr);
1332 genUnconditionalBranch(cUnit, retChainingCell);
1333
1334 // FIXME
1335 cUnit->printMe = true;
1336}
1337
Ben Chengba4fc8b2009-06-01 13:00:29 -07001338/* Geneate a branch to go back to the interpreter */
1339static void genPuntToInterp(CompilationUnit *cUnit, unsigned int offset)
1340{
1341 /* r0 = dalvik pc */
Bill Buzbeec6f10662010-02-09 11:16:15 -08001342 dvmCompilerFlushAllRegs(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001343 loadConstant(cUnit, r0, (int) (cUnit->method->insns + offset));
Ben Cheng20d7e6c2011-02-18 17:12:42 -08001344 loadWordDisp(cUnit, r6SELF, offsetof(Thread,
Bill Buzbee270c1d62009-08-13 16:58:07 -07001345 jitToInterpEntries.dvmJitToInterpPunt), r1);
Bill Buzbee1465db52009-09-23 17:17:35 -07001346 opReg(cUnit, kOpBlx, r1);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001347}
1348
1349/*
1350 * Attempt to single step one instruction using the interpreter and return
1351 * to the compiled code for the next Dalvik instruction
1352 */
1353static void genInterpSingleStep(CompilationUnit *cUnit, MIR *mir)
1354{
Dan Bornsteine4852762010-12-02 12:45:00 -08001355 int flags = dexGetFlagsFromOpcode(mir->dalvikInsn.opcode);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001356 int flagsToCheck = kInstrCanBranch | kInstrCanSwitch | kInstrCanReturn |
1357 kInstrCanThrow;
Bill Buzbee1465db52009-09-23 17:17:35 -07001358
Ben Cheng32115a92011-03-22 14:09:09 -07001359 // Single stepping is considered loop mode breaker
1360 if (cUnit->jitMode == kJitLoop) {
1361 cUnit->quitLoopMode = true;
1362 return;
1363 }
1364
Bill Buzbee45273872010-03-11 11:12:15 -08001365 //If already optimized out, just ignore
Dan Bornstein9a1f8162010-12-01 17:02:26 -08001366 if (mir->dalvikInsn.opcode == OP_NOP)
Bill Buzbee45273872010-03-11 11:12:15 -08001367 return;
1368
Bill Buzbee1465db52009-09-23 17:17:35 -07001369 //Ugly, but necessary. Flush all Dalvik regs so Interp can find them
Bill Buzbeec6f10662010-02-09 11:16:15 -08001370 dvmCompilerFlushAllRegs(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07001371
Ben Chengba4fc8b2009-06-01 13:00:29 -07001372 if ((mir->next == NULL) || (flags & flagsToCheck)) {
1373 genPuntToInterp(cUnit, mir->offset);
1374 return;
1375 }
buzbee9f601a92011-02-11 17:48:20 -08001376 int entryAddr = offsetof(Thread,
Ben Chengba4fc8b2009-06-01 13:00:29 -07001377 jitToInterpEntries.dvmJitToInterpSingleStep);
Ben Cheng20d7e6c2011-02-18 17:12:42 -08001378 loadWordDisp(cUnit, r6SELF, entryAddr, r2);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001379 /* r0 = dalvik pc */
1380 loadConstant(cUnit, r0, (int) (cUnit->method->insns + mir->offset));
1381 /* r1 = dalvik pc of following instruction */
1382 loadConstant(cUnit, r1, (int) (cUnit->method->insns + mir->next->offset));
Bill Buzbee1465db52009-09-23 17:17:35 -07001383 opReg(cUnit, kOpBlx, r2);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001384}
1385
Carl Shapiro01605d22011-02-01 11:32:44 -08001386#if defined(_ARMV5TE) || defined(_ARMV5TE_VFP)
Bill Buzbeec1d9ed42010-02-02 11:04:33 -08001387/*
1388 * To prevent a thread in a monitor wait from blocking the Jit from
1389 * resetting the code cache, heavyweight monitor lock will not
1390 * be allowed to return to an existing translation. Instead, we will
1391 * handle them by branching to a handler, which will in turn call the
1392 * runtime lock routine and then branch directly back to the
1393 * interpreter main loop. Given the high cost of the heavyweight
1394 * lock operation, this additional cost should be slight (especially when
1395 * considering that we expect the vast majority of lock operations to
1396 * use the fast-path thin lock bypass).
1397 */
Ben Cheng5d90c202009-11-22 23:31:11 -08001398static void genMonitorPortable(CompilationUnit *cUnit, MIR *mir)
Bill Buzbee270c1d62009-08-13 16:58:07 -07001399{
Dan Bornstein9a1f8162010-12-01 17:02:26 -08001400 bool isEnter = (mir->dalvikInsn.opcode == OP_MONITOR_ENTER);
Bill Buzbee1465db52009-09-23 17:17:35 -07001401 genExportPC(cUnit, mir);
Bill Buzbeec6f10662010-02-09 11:16:15 -08001402 dvmCompilerFlushAllRegs(cUnit); /* Send everything to home location */
1403 RegLocation rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07001404 loadValueDirectFixed(cUnit, rlSrc, r1);
Ben Cheng20d7e6c2011-02-18 17:12:42 -08001405 genRegCopy(cUnit, r0, r6SELF);
Bill Buzbeec1d9ed42010-02-02 11:04:33 -08001406 genNullCheck(cUnit, rlSrc.sRegLow, r1, mir->offset, NULL);
Bill Buzbeeefbd3c52009-11-04 22:18:40 -08001407 if (isEnter) {
Bill Buzbeec1d9ed42010-02-02 11:04:33 -08001408 /* Get dPC of next insn */
1409 loadConstant(cUnit, r4PC, (int)(cUnit->method->insns + mir->offset +
Dan Bornsteine4852762010-12-02 12:45:00 -08001410 dexGetWidthFromOpcode(OP_MONITOR_ENTER)));
Bill Buzbeec1d9ed42010-02-02 11:04:33 -08001411 genDispatchToHandler(cUnit, TEMPLATE_MONITOR_ENTER);
Bill Buzbee1465db52009-09-23 17:17:35 -07001412 } else {
Ben Chengbd1326d2010-04-02 15:04:53 -07001413 LOAD_FUNC_ADDR(cUnit, r2, (int)dvmUnlockObject);
Bill Buzbeec1d9ed42010-02-02 11:04:33 -08001414 /* Do the call */
1415 opReg(cUnit, kOpBlx, r2);
buzbee8f8109a2010-08-31 10:16:35 -07001416 /* Did we throw? */
1417 ArmLIR *branchOver = genCmpImmBranch(cUnit, kArmCondNe, r0, 0);
Bill Buzbee6bbdd6b2010-02-16 14:40:01 -08001418 loadConstant(cUnit, r0,
1419 (int) (cUnit->method->insns + mir->offset +
Dan Bornsteine4852762010-12-02 12:45:00 -08001420 dexGetWidthFromOpcode(OP_MONITOR_EXIT)));
Bill Buzbee6bbdd6b2010-02-16 14:40:01 -08001421 genDispatchToHandler(cUnit, TEMPLATE_THROW_EXCEPTION_COMMON);
1422 ArmLIR *target = newLIR0(cUnit, kArmPseudoTargetLabel);
1423 target->defMask = ENCODE_ALL;
1424 branchOver->generic.target = (LIR *) target;
Elliott Hughes6a555132010-02-25 15:41:42 -08001425 dvmCompilerClobberCallRegs(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07001426 }
Bill Buzbee270c1d62009-08-13 16:58:07 -07001427}
Ben Chengfc075c22010-05-28 15:20:08 -07001428#endif
Bill Buzbee270c1d62009-08-13 16:58:07 -07001429
Ben Chengba4fc8b2009-06-01 13:00:29 -07001430/*
buzbee9a3147c2011-03-02 15:43:48 -08001431 * Fetch *self->info.breakFlags. If the breakFlags are non-zero,
Ben Cheng7ab74e12011-02-03 14:02:06 -08001432 * punt to the interpreter.
1433 */
1434static void genSuspendPoll(CompilationUnit *cUnit, MIR *mir)
1435{
1436 int rTemp = dvmCompilerAllocTemp(cUnit);
1437 ArmLIR *ld;
buzbee9a3147c2011-03-02 15:43:48 -08001438 ld = loadBaseDisp(cUnit, NULL, r6SELF,
1439 offsetof(Thread, interpBreak.ctl.breakFlags),
1440 rTemp, kUnsignedByte, INVALID_SREG);
Ben Cheng7ab74e12011-02-03 14:02:06 -08001441 setMemRefType(ld, true /* isLoad */, kMustNotAlias);
Ben Cheng7ab74e12011-02-03 14:02:06 -08001442 genRegImmCheck(cUnit, kArmCondNe, rTemp, 0, mir->offset, NULL);
1443}
1444
1445/*
Ben Chengba4fc8b2009-06-01 13:00:29 -07001446 * The following are the first-level codegen routines that analyze the format
1447 * of each bytecode then either dispatch special purpose codegen routines
1448 * or produce corresponding Thumb instructions directly.
1449 */
1450
1451static bool handleFmt10t_Fmt20t_Fmt30t(CompilationUnit *cUnit, MIR *mir,
Bill Buzbee89efc3d2009-07-28 11:22:22 -07001452 BasicBlock *bb, ArmLIR *labelList)
Ben Chengba4fc8b2009-06-01 13:00:29 -07001453{
Ben Cheng7ab74e12011-02-03 14:02:06 -08001454 /* backward branch? */
1455 bool backwardBranch = (bb->taken->startOffset <= mir->offset);
1456
Ben Cheng32115a92011-03-22 14:09:09 -07001457 if (backwardBranch &&
1458 (gDvmJit.genSuspendPoll || cUnit->jitMode == kJitLoop)) {
Ben Cheng7ab74e12011-02-03 14:02:06 -08001459 genSuspendPoll(cUnit, mir);
1460 }
1461
1462 int numPredecessors = dvmCountSetBits(bb->taken->predecessors);
1463 /*
1464 * Things could be hoisted out of the taken block into the predecessor, so
1465 * make sure it is dominated by the predecessor.
1466 */
1467 if (numPredecessors == 1 && bb->taken->visited == false &&
Ben Cheng20d7e6c2011-02-18 17:12:42 -08001468 bb->taken->blockType == kDalvikByteCode) {
Ben Cheng7ab74e12011-02-03 14:02:06 -08001469 cUnit->nextCodegenBlock = bb->taken;
1470 } else {
1471 /* For OP_GOTO, OP_GOTO_16, and OP_GOTO_32 */
1472 genUnconditionalBranch(cUnit, &labelList[bb->taken->id]);
1473 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07001474 return false;
1475}
1476
1477static bool handleFmt10x(CompilationUnit *cUnit, MIR *mir)
1478{
Dan Bornstein9a1f8162010-12-01 17:02:26 -08001479 Opcode dalvikOpcode = mir->dalvikInsn.opcode;
1480 if ((dalvikOpcode >= OP_UNUSED_3E) && (dalvikOpcode <= OP_UNUSED_43)) {
1481 LOGE("Codegen: got unused opcode 0x%x\n",dalvikOpcode);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001482 return true;
1483 }
Dan Bornstein9a1f8162010-12-01 17:02:26 -08001484 switch (dalvikOpcode) {
Andy McFadden291758c2010-09-10 08:04:52 -07001485 case OP_RETURN_VOID_BARRIER:
buzbee2ce33c92010-11-01 15:53:27 -07001486 dvmCompilerGenMemBarrier(cUnit, kST);
1487 // Intentional fallthrough
1488 case OP_RETURN_VOID:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001489 genReturnCommon(cUnit,mir);
1490 break;
1491 case OP_UNUSED_73:
1492 case OP_UNUSED_79:
1493 case OP_UNUSED_7A:
Dan Bornstein90f15432010-12-02 16:46:25 -08001494 case OP_DISPATCH_FF:
Dan Bornstein9a1f8162010-12-01 17:02:26 -08001495 LOGE("Codegen: got unused opcode 0x%x\n",dalvikOpcode);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001496 return true;
1497 case OP_NOP:
1498 break;
1499 default:
1500 return true;
1501 }
1502 return false;
1503}
1504
1505static bool handleFmt11n_Fmt31i(CompilationUnit *cUnit, MIR *mir)
1506{
Bill Buzbee1465db52009-09-23 17:17:35 -07001507 RegLocation rlDest;
1508 RegLocation rlResult;
1509 if (mir->ssaRep->numDefs == 2) {
Bill Buzbeec6f10662010-02-09 11:16:15 -08001510 rlDest = dvmCompilerGetDestWide(cUnit, mir, 0, 1);
Bill Buzbee1465db52009-09-23 17:17:35 -07001511 } else {
Bill Buzbeec6f10662010-02-09 11:16:15 -08001512 rlDest = dvmCompilerGetDest(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07001513 }
Ben Chenge9695e52009-06-16 16:11:47 -07001514
Dan Bornstein9a1f8162010-12-01 17:02:26 -08001515 switch (mir->dalvikInsn.opcode) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07001516 case OP_CONST:
Ben Chenge9695e52009-06-16 16:11:47 -07001517 case OP_CONST_4: {
Bill Buzbeec6f10662010-02-09 11:16:15 -08001518 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kAnyReg, true);
Ben Chengbd1326d2010-04-02 15:04:53 -07001519 loadConstantNoClobber(cUnit, rlResult.lowReg, mir->dalvikInsn.vB);
Bill Buzbee1465db52009-09-23 17:17:35 -07001520 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001521 break;
Ben Chenge9695e52009-06-16 16:11:47 -07001522 }
1523 case OP_CONST_WIDE_32: {
Bill Buzbee1465db52009-09-23 17:17:35 -07001524 //TUNING: single routine to load constant pair for support doubles
Bill Buzbee964a7b02010-01-28 12:54:19 -08001525 //TUNING: load 0/-1 separately to avoid load dependency
Bill Buzbeec6f10662010-02-09 11:16:15 -08001526 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Ben Chengbd1326d2010-04-02 15:04:53 -07001527 loadConstantNoClobber(cUnit, rlResult.lowReg, mir->dalvikInsn.vB);
Bill Buzbee1465db52009-09-23 17:17:35 -07001528 opRegRegImm(cUnit, kOpAsr, rlResult.highReg,
1529 rlResult.lowReg, 31);
1530 storeValueWide(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001531 break;
Ben Chenge9695e52009-06-16 16:11:47 -07001532 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07001533 default:
1534 return true;
1535 }
1536 return false;
1537}
1538
1539static bool handleFmt21h(CompilationUnit *cUnit, MIR *mir)
1540{
Bill Buzbee1465db52009-09-23 17:17:35 -07001541 RegLocation rlDest;
1542 RegLocation rlResult;
1543 if (mir->ssaRep->numDefs == 2) {
Bill Buzbeec6f10662010-02-09 11:16:15 -08001544 rlDest = dvmCompilerGetDestWide(cUnit, mir, 0, 1);
Bill Buzbee1465db52009-09-23 17:17:35 -07001545 } else {
Bill Buzbeec6f10662010-02-09 11:16:15 -08001546 rlDest = dvmCompilerGetDest(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07001547 }
Bill Buzbeec6f10662010-02-09 11:16:15 -08001548 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kAnyReg, true);
Ben Chenge9695e52009-06-16 16:11:47 -07001549
Dan Bornstein9a1f8162010-12-01 17:02:26 -08001550 switch (mir->dalvikInsn.opcode) {
Ben Chenge9695e52009-06-16 16:11:47 -07001551 case OP_CONST_HIGH16: {
Ben Chengbd1326d2010-04-02 15:04:53 -07001552 loadConstantNoClobber(cUnit, rlResult.lowReg,
1553 mir->dalvikInsn.vB << 16);
Bill Buzbee1465db52009-09-23 17:17:35 -07001554 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001555 break;
Ben Chenge9695e52009-06-16 16:11:47 -07001556 }
1557 case OP_CONST_WIDE_HIGH16: {
Bill Buzbee1465db52009-09-23 17:17:35 -07001558 loadConstantValueWide(cUnit, rlResult.lowReg, rlResult.highReg,
1559 0, mir->dalvikInsn.vB << 16);
1560 storeValueWide(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001561 break;
Ben Chenge9695e52009-06-16 16:11:47 -07001562 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07001563 default:
1564 return true;
1565 }
1566 return false;
1567}
1568
jeffhao71eee1f2011-01-04 14:18:54 -08001569static bool handleFmt20bc_Fmt40sc(CompilationUnit *cUnit, MIR *mir)
Ben Chengba4fc8b2009-06-01 13:00:29 -07001570{
jeffhao71eee1f2011-01-04 14:18:54 -08001571 /* For OP_THROW_VERIFICATION_ERROR & OP_THROW_VERIFICATION_ERROR_JUMBO */
Ben Chengba4fc8b2009-06-01 13:00:29 -07001572 genInterpSingleStep(cUnit, mir);
1573 return false;
1574}
1575
jeffhao71eee1f2011-01-04 14:18:54 -08001576static bool handleFmt21c_Fmt31c_Fmt41c(CompilationUnit *cUnit, MIR *mir)
Ben Chengba4fc8b2009-06-01 13:00:29 -07001577{
Bill Buzbee1465db52009-09-23 17:17:35 -07001578 RegLocation rlResult;
1579 RegLocation rlDest;
1580 RegLocation rlSrc;
Ben Chenge9695e52009-06-16 16:11:47 -07001581
Dan Bornstein9a1f8162010-12-01 17:02:26 -08001582 switch (mir->dalvikInsn.opcode) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07001583 case OP_CONST_STRING_JUMBO:
1584 case OP_CONST_STRING: {
1585 void *strPtr = (void*)
1586 (cUnit->method->clazz->pDvmDex->pResStrings[mir->dalvikInsn.vB]);
Ben Chengdd6e8702010-05-07 13:05:47 -07001587
1588 if (strPtr == NULL) {
Ben Cheng32115a92011-03-22 14:09:09 -07001589 BAIL_LOOP_COMPILATION();
Ben Chengdd6e8702010-05-07 13:05:47 -07001590 LOGE("Unexpected null string");
1591 dvmAbort();
1592 }
1593
Bill Buzbeec6f10662010-02-09 11:16:15 -08001594 rlDest = dvmCompilerGetDest(cUnit, mir, 0);
1595 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Ben Chengbd1326d2010-04-02 15:04:53 -07001596 loadConstantNoClobber(cUnit, rlResult.lowReg, (int) strPtr );
Bill Buzbee1465db52009-09-23 17:17:35 -07001597 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001598 break;
1599 }
jeffhao71eee1f2011-01-04 14:18:54 -08001600 case OP_CONST_CLASS:
1601 case OP_CONST_CLASS_JUMBO: {
Ben Chengba4fc8b2009-06-01 13:00:29 -07001602 void *classPtr = (void*)
1603 (cUnit->method->clazz->pDvmDex->pResClasses[mir->dalvikInsn.vB]);
Ben Chengdd6e8702010-05-07 13:05:47 -07001604
1605 if (classPtr == NULL) {
Ben Cheng32115a92011-03-22 14:09:09 -07001606 BAIL_LOOP_COMPILATION();
Ben Chengdd6e8702010-05-07 13:05:47 -07001607 LOGE("Unexpected null class");
1608 dvmAbort();
1609 }
1610
Bill Buzbeec6f10662010-02-09 11:16:15 -08001611 rlDest = dvmCompilerGetDest(cUnit, mir, 0);
1612 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Ben Chengbd1326d2010-04-02 15:04:53 -07001613 loadConstantNoClobber(cUnit, rlResult.lowReg, (int) classPtr );
Bill Buzbee1465db52009-09-23 17:17:35 -07001614 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001615 break;
1616 }
jeffhao71eee1f2011-01-04 14:18:54 -08001617 case OP_SGET:
buzbeeecf8f6e2010-07-20 14:53:42 -07001618 case OP_SGET_VOLATILE:
buzbeebd7865b2011-03-31 10:55:04 -07001619 case OP_SGET_VOLATILE_JUMBO:
jeffhao71eee1f2011-01-04 14:18:54 -08001620 case OP_SGET_JUMBO:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001621 case OP_SGET_OBJECT:
jeffhao71eee1f2011-01-04 14:18:54 -08001622 case OP_SGET_OBJECT_VOLATILE:
buzbeebd7865b2011-03-31 10:55:04 -07001623 case OP_SGET_OBJECT_VOLATILE_JUMBO:
jeffhao71eee1f2011-01-04 14:18:54 -08001624 case OP_SGET_OBJECT_JUMBO:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001625 case OP_SGET_BOOLEAN:
jeffhao71eee1f2011-01-04 14:18:54 -08001626 case OP_SGET_BOOLEAN_JUMBO:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001627 case OP_SGET_CHAR:
jeffhao71eee1f2011-01-04 14:18:54 -08001628 case OP_SGET_CHAR_JUMBO:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001629 case OP_SGET_BYTE:
jeffhao71eee1f2011-01-04 14:18:54 -08001630 case OP_SGET_BYTE_JUMBO:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001631 case OP_SGET_SHORT:
jeffhao71eee1f2011-01-04 14:18:54 -08001632 case OP_SGET_SHORT_JUMBO: {
Bill Buzbee50a6bf22009-07-08 13:08:04 -07001633 int valOffset = offsetof(StaticField, value);
Bill Buzbeec6f10662010-02-09 11:16:15 -08001634 int tReg = dvmCompilerAllocTemp(cUnit);
buzbeeecf8f6e2010-07-20 14:53:42 -07001635 bool isVolatile;
Ben Cheng7a2697d2010-06-07 13:44:23 -07001636 const Method *method = (mir->OptimizationFlags & MIR_CALLEE) ?
1637 mir->meta.calleeMethod : cUnit->method;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001638 void *fieldPtr = (void*)
Ben Cheng7a2697d2010-06-07 13:44:23 -07001639 (method->clazz->pDvmDex->pResFields[mir->dalvikInsn.vB]);
buzbeebd7865b2011-03-31 10:55:04 -07001640 Opcode opcode = mir->dalvikInsn.opcode;
Ben Chengdd6e8702010-05-07 13:05:47 -07001641
1642 if (fieldPtr == NULL) {
Ben Cheng32115a92011-03-22 14:09:09 -07001643 BAIL_LOOP_COMPILATION();
Ben Chengdd6e8702010-05-07 13:05:47 -07001644 LOGE("Unexpected null static field");
1645 dvmAbort();
1646 }
1647
buzbeebd7865b2011-03-31 10:55:04 -07001648 isVolatile = (opcode == OP_SGET_VOLATILE) ||
buzbee3a56e9d2011-03-31 11:18:28 -07001649 (opcode == OP_SGET_VOLATILE_JUMBO) ||
buzbeebd7865b2011-03-31 10:55:04 -07001650 (opcode == OP_SGET_OBJECT_VOLATILE) ||
buzbee8b94be12011-04-04 12:25:57 -07001651 (opcode == OP_SGET_OBJECT_VOLATILE_JUMBO);
Joe Onorato80d118a2011-04-08 15:32:34 -07001652
buzbee8b94be12011-04-04 12:25:57 -07001653 assert(isVolatile == dvmIsVolatileField((Field *) fieldPtr));
buzbeeecf8f6e2010-07-20 14:53:42 -07001654
Bill Buzbeec6f10662010-02-09 11:16:15 -08001655 rlDest = dvmCompilerGetDest(cUnit, mir, 0);
1656 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kAnyReg, true);
Bill Buzbee1465db52009-09-23 17:17:35 -07001657 loadConstant(cUnit, tReg, (int) fieldPtr + valOffset);
Ben Cheng11d8f142010-03-24 15:24:19 -07001658
buzbeeecf8f6e2010-07-20 14:53:42 -07001659 if (isVolatile) {
buzbee2ce33c92010-11-01 15:53:27 -07001660 dvmCompilerGenMemBarrier(cUnit, kSY);
buzbeeecf8f6e2010-07-20 14:53:42 -07001661 }
Ben Cheng11d8f142010-03-24 15:24:19 -07001662 HEAP_ACCESS_SHADOW(true);
Bill Buzbee1465db52009-09-23 17:17:35 -07001663 loadWordDisp(cUnit, tReg, 0, rlResult.lowReg);
Ben Cheng11d8f142010-03-24 15:24:19 -07001664 HEAP_ACCESS_SHADOW(false);
1665
Bill Buzbee1465db52009-09-23 17:17:35 -07001666 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001667 break;
1668 }
jeffhao71eee1f2011-01-04 14:18:54 -08001669 case OP_SGET_WIDE:
1670 case OP_SGET_WIDE_JUMBO: {
Bill Buzbee50a6bf22009-07-08 13:08:04 -07001671 int valOffset = offsetof(StaticField, value);
Ben Cheng7a2697d2010-06-07 13:44:23 -07001672 const Method *method = (mir->OptimizationFlags & MIR_CALLEE) ?
1673 mir->meta.calleeMethod : cUnit->method;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001674 void *fieldPtr = (void*)
Ben Cheng7a2697d2010-06-07 13:44:23 -07001675 (method->clazz->pDvmDex->pResFields[mir->dalvikInsn.vB]);
Ben Chengdd6e8702010-05-07 13:05:47 -07001676
1677 if (fieldPtr == NULL) {
Ben Cheng32115a92011-03-22 14:09:09 -07001678 BAIL_LOOP_COMPILATION();
Ben Chengdd6e8702010-05-07 13:05:47 -07001679 LOGE("Unexpected null static field");
1680 dvmAbort();
1681 }
1682
Bill Buzbeec6f10662010-02-09 11:16:15 -08001683 int tReg = dvmCompilerAllocTemp(cUnit);
Bill Buzbeec6f10662010-02-09 11:16:15 -08001684 rlDest = dvmCompilerGetDestWide(cUnit, mir, 0, 1);
1685 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kAnyReg, true);
Bill Buzbee1465db52009-09-23 17:17:35 -07001686 loadConstant(cUnit, tReg, (int) fieldPtr + valOffset);
Ben Cheng11d8f142010-03-24 15:24:19 -07001687
1688 HEAP_ACCESS_SHADOW(true);
Bill Buzbee1465db52009-09-23 17:17:35 -07001689 loadPair(cUnit, tReg, rlResult.lowReg, rlResult.highReg);
Ben Cheng11d8f142010-03-24 15:24:19 -07001690 HEAP_ACCESS_SHADOW(false);
1691
Bill Buzbee1465db52009-09-23 17:17:35 -07001692 storeValueWide(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001693 break;
1694 }
jeffhao71eee1f2011-01-04 14:18:54 -08001695 case OP_SPUT:
1696 case OP_SPUT_VOLATILE:
buzbeebd7865b2011-03-31 10:55:04 -07001697 case OP_SPUT_VOLATILE_JUMBO:
jeffhao71eee1f2011-01-04 14:18:54 -08001698 case OP_SPUT_JUMBO:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001699 case OP_SPUT_OBJECT:
buzbeeddc7d292010-09-02 17:16:24 -07001700 case OP_SPUT_OBJECT_VOLATILE:
buzbeebd7865b2011-03-31 10:55:04 -07001701 case OP_SPUT_OBJECT_VOLATILE_JUMBO:
jeffhao71eee1f2011-01-04 14:18:54 -08001702 case OP_SPUT_OBJECT_JUMBO:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001703 case OP_SPUT_BOOLEAN:
jeffhao71eee1f2011-01-04 14:18:54 -08001704 case OP_SPUT_BOOLEAN_JUMBO:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001705 case OP_SPUT_CHAR:
jeffhao71eee1f2011-01-04 14:18:54 -08001706 case OP_SPUT_CHAR_JUMBO:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001707 case OP_SPUT_BYTE:
jeffhao71eee1f2011-01-04 14:18:54 -08001708 case OP_SPUT_BYTE_JUMBO:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001709 case OP_SPUT_SHORT:
jeffhao71eee1f2011-01-04 14:18:54 -08001710 case OP_SPUT_SHORT_JUMBO: {
Bill Buzbee50a6bf22009-07-08 13:08:04 -07001711 int valOffset = offsetof(StaticField, value);
Bill Buzbeec6f10662010-02-09 11:16:15 -08001712 int tReg = dvmCompilerAllocTemp(cUnit);
buzbeed3b0a4b2010-09-27 11:30:22 -07001713 int objHead;
buzbeeecf8f6e2010-07-20 14:53:42 -07001714 bool isVolatile;
buzbeed3b0a4b2010-09-27 11:30:22 -07001715 bool isSputObject;
Ben Cheng7a2697d2010-06-07 13:44:23 -07001716 const Method *method = (mir->OptimizationFlags & MIR_CALLEE) ?
1717 mir->meta.calleeMethod : cUnit->method;
1718 void *fieldPtr = (void*)
1719 (method->clazz->pDvmDex->pResFields[mir->dalvikInsn.vB]);
buzbeebd7865b2011-03-31 10:55:04 -07001720 Opcode opcode = mir->dalvikInsn.opcode;
Ben Chenge9695e52009-06-16 16:11:47 -07001721
Ben Cheng32115a92011-03-22 14:09:09 -07001722 if (fieldPtr == NULL) {
1723 BAIL_LOOP_COMPILATION();
1724 LOGE("Unexpected null static field");
1725 dvmAbort();
1726 }
1727
buzbeebd7865b2011-03-31 10:55:04 -07001728 isVolatile = (opcode == OP_SPUT_VOLATILE) ||
1729 (opcode == OP_SPUT_VOLATILE_JUMBO) ||
1730 (opcode == OP_SPUT_OBJECT_VOLATILE) ||
buzbee8b94be12011-04-04 12:25:57 -07001731 (opcode == OP_SPUT_OBJECT_VOLATILE_JUMBO);
Joe Onorato80d118a2011-04-08 15:32:34 -07001732
buzbee8b94be12011-04-04 12:25:57 -07001733 assert(isVolatile == dvmIsVolatileField((Field *) fieldPtr));
buzbeeecf8f6e2010-07-20 14:53:42 -07001734
buzbeebd7865b2011-03-31 10:55:04 -07001735 isSputObject = (opcode == OP_SPUT_OBJECT) ||
1736 (opcode == OP_SPUT_OBJECT_JUMBO) ||
1737 (opcode == OP_SPUT_OBJECT_VOLATILE) ||
1738 (opcode == OP_SPUT_OBJECT_VOLATILE_JUMBO);
buzbeed3b0a4b2010-09-27 11:30:22 -07001739
Bill Buzbeec6f10662010-02-09 11:16:15 -08001740 rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07001741 rlSrc = loadValue(cUnit, rlSrc, kAnyReg);
buzbeeb78c76f2010-09-30 19:08:20 -07001742 loadConstant(cUnit, tReg, (int) fieldPtr);
buzbeed3b0a4b2010-09-27 11:30:22 -07001743 if (isSputObject) {
1744 objHead = dvmCompilerAllocTemp(cUnit);
buzbeeb78c76f2010-09-30 19:08:20 -07001745 loadWordDisp(cUnit, tReg, offsetof(Field, clazz), objHead);
buzbeed3b0a4b2010-09-27 11:30:22 -07001746 }
Ben Cheng11d8f142010-03-24 15:24:19 -07001747 HEAP_ACCESS_SHADOW(true);
buzbeeb78c76f2010-09-30 19:08:20 -07001748 storeWordDisp(cUnit, tReg, valOffset ,rlSrc.lowReg);
buzbeed3b0a4b2010-09-27 11:30:22 -07001749 dvmCompilerFreeTemp(cUnit, tReg);
Ben Cheng11d8f142010-03-24 15:24:19 -07001750 HEAP_ACCESS_SHADOW(false);
buzbeeecf8f6e2010-07-20 14:53:42 -07001751 if (isVolatile) {
buzbee2ce33c92010-11-01 15:53:27 -07001752 dvmCompilerGenMemBarrier(cUnit, kSY);
buzbeeecf8f6e2010-07-20 14:53:42 -07001753 }
buzbeed3b0a4b2010-09-27 11:30:22 -07001754 if (isSputObject) {
buzbeeb78c76f2010-09-30 19:08:20 -07001755 /* NOTE: marking card based sfield->clazz */
buzbeed3b0a4b2010-09-27 11:30:22 -07001756 markCard(cUnit, rlSrc.lowReg, objHead);
1757 dvmCompilerFreeTemp(cUnit, objHead);
buzbee919eb062010-07-12 12:59:22 -07001758 }
Ben Cheng11d8f142010-03-24 15:24:19 -07001759
Ben Chengba4fc8b2009-06-01 13:00:29 -07001760 break;
1761 }
jeffhao71eee1f2011-01-04 14:18:54 -08001762 case OP_SPUT_WIDE:
1763 case OP_SPUT_WIDE_JUMBO: {
Bill Buzbeec6f10662010-02-09 11:16:15 -08001764 int tReg = dvmCompilerAllocTemp(cUnit);
Bill Buzbee50a6bf22009-07-08 13:08:04 -07001765 int valOffset = offsetof(StaticField, value);
Ben Cheng7a2697d2010-06-07 13:44:23 -07001766 const Method *method = (mir->OptimizationFlags & MIR_CALLEE) ?
1767 mir->meta.calleeMethod : cUnit->method;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001768 void *fieldPtr = (void*)
Ben Cheng7a2697d2010-06-07 13:44:23 -07001769 (method->clazz->pDvmDex->pResFields[mir->dalvikInsn.vB]);
Ben Chenge9695e52009-06-16 16:11:47 -07001770
Ben Chengdd6e8702010-05-07 13:05:47 -07001771 if (fieldPtr == NULL) {
Ben Cheng32115a92011-03-22 14:09:09 -07001772 BAIL_LOOP_COMPILATION();
Ben Chengdd6e8702010-05-07 13:05:47 -07001773 LOGE("Unexpected null static field");
1774 dvmAbort();
1775 }
1776
Bill Buzbeec6f10662010-02-09 11:16:15 -08001777 rlSrc = dvmCompilerGetSrcWide(cUnit, mir, 0, 1);
Bill Buzbee1465db52009-09-23 17:17:35 -07001778 rlSrc = loadValueWide(cUnit, rlSrc, kAnyReg);
1779 loadConstant(cUnit, tReg, (int) fieldPtr + valOffset);
Ben Cheng11d8f142010-03-24 15:24:19 -07001780
1781 HEAP_ACCESS_SHADOW(true);
Bill Buzbee1465db52009-09-23 17:17:35 -07001782 storePair(cUnit, tReg, rlSrc.lowReg, rlSrc.highReg);
Ben Cheng11d8f142010-03-24 15:24:19 -07001783 HEAP_ACCESS_SHADOW(false);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001784 break;
1785 }
jeffhao71eee1f2011-01-04 14:18:54 -08001786 case OP_NEW_INSTANCE:
1787 case OP_NEW_INSTANCE_JUMBO: {
Ben Chenge9695e52009-06-16 16:11:47 -07001788 /*
1789 * Obey the calling convention and don't mess with the register
1790 * usage.
1791 */
Carl Shapirofc75f3e2010-12-07 11:43:38 -08001792 ClassObject *classPtr = (ClassObject *)
Ben Chengba4fc8b2009-06-01 13:00:29 -07001793 (cUnit->method->clazz->pDvmDex->pResClasses[mir->dalvikInsn.vB]);
Ben Chengdd6e8702010-05-07 13:05:47 -07001794
1795 if (classPtr == NULL) {
Ben Cheng32115a92011-03-22 14:09:09 -07001796 BAIL_LOOP_COMPILATION();
Ben Chengdd6e8702010-05-07 13:05:47 -07001797 LOGE("Unexpected null class");
1798 dvmAbort();
1799 }
1800
Ben Cheng79d173c2009-09-29 16:12:51 -07001801 /*
1802 * If it is going to throw, it should not make to the trace to begin
Bill Buzbee1465db52009-09-23 17:17:35 -07001803 * with. However, Alloc might throw, so we need to genExportPC()
Ben Cheng79d173c2009-09-29 16:12:51 -07001804 */
1805 assert((classPtr->accessFlags & (ACC_INTERFACE|ACC_ABSTRACT)) == 0);
Bill Buzbeec6f10662010-02-09 11:16:15 -08001806 dvmCompilerFlushAllRegs(cUnit); /* Everything to home location */
Bill Buzbee1465db52009-09-23 17:17:35 -07001807 genExportPC(cUnit, mir);
Ben Chengbd1326d2010-04-02 15:04:53 -07001808 LOAD_FUNC_ADDR(cUnit, r2, (int)dvmAllocObject);
Ben Chenge9695e52009-06-16 16:11:47 -07001809 loadConstant(cUnit, r0, (int) classPtr);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001810 loadConstant(cUnit, r1, ALLOC_DONT_TRACK);
Bill Buzbee1465db52009-09-23 17:17:35 -07001811 opReg(cUnit, kOpBlx, r2);
Elliott Hughes6a555132010-02-25 15:41:42 -08001812 dvmCompilerClobberCallRegs(cUnit);
Ben Cheng4f489172009-09-27 17:08:35 -07001813 /* generate a branch over if allocation is successful */
buzbee8f8109a2010-08-31 10:16:35 -07001814 ArmLIR *branchOver = genCmpImmBranch(cUnit, kArmCondNe, r0, 0);
Ben Cheng4f489172009-09-27 17:08:35 -07001815 /*
1816 * OOM exception needs to be thrown here and cannot re-execute
1817 */
1818 loadConstant(cUnit, r0,
1819 (int) (cUnit->method->insns + mir->offset));
1820 genDispatchToHandler(cUnit, TEMPLATE_THROW_EXCEPTION_COMMON);
1821 /* noreturn */
1822
Bill Buzbee1465db52009-09-23 17:17:35 -07001823 ArmLIR *target = newLIR0(cUnit, kArmPseudoTargetLabel);
Ben Cheng4f489172009-09-27 17:08:35 -07001824 target->defMask = ENCODE_ALL;
1825 branchOver->generic.target = (LIR *) target;
Bill Buzbeec6f10662010-02-09 11:16:15 -08001826 rlDest = dvmCompilerGetDest(cUnit, mir, 0);
1827 rlResult = dvmCompilerGetReturn(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07001828 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001829 break;
1830 }
jeffhao71eee1f2011-01-04 14:18:54 -08001831 case OP_CHECK_CAST:
1832 case OP_CHECK_CAST_JUMBO: {
Ben Chenge9695e52009-06-16 16:11:47 -07001833 /*
1834 * Obey the calling convention and don't mess with the register
1835 * usage.
1836 */
Ben Chengba4fc8b2009-06-01 13:00:29 -07001837 ClassObject *classPtr =
1838 (cUnit->method->clazz->pDvmDex->pResClasses[mir->dalvikInsn.vB]);
Bill Buzbee4df41a52009-11-12 17:07:16 -08001839 /*
1840 * Note: It is possible that classPtr is NULL at this point,
1841 * even though this instruction has been successfully interpreted.
1842 * If the previous interpretation had a null source, the
1843 * interpreter would not have bothered to resolve the clazz.
1844 * Bail out to the interpreter in this case, and log it
1845 * so that we can tell if it happens frequently.
1846 */
1847 if (classPtr == NULL) {
Ben Cheng32115a92011-03-22 14:09:09 -07001848 BAIL_LOOP_COMPILATION();
1849 LOGVV("null clazz in OP_CHECK_CAST, single-stepping");
1850 genInterpSingleStep(cUnit, mir);
1851 return false;
Bill Buzbee4df41a52009-11-12 17:07:16 -08001852 }
Bill Buzbeec6f10662010-02-09 11:16:15 -08001853 dvmCompilerFlushAllRegs(cUnit); /* Everything to home location */
Ben Chengba4fc8b2009-06-01 13:00:29 -07001854 loadConstant(cUnit, r1, (int) classPtr );
Bill Buzbeec6f10662010-02-09 11:16:15 -08001855 rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07001856 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
buzbee8f8109a2010-08-31 10:16:35 -07001857 /* Null? */
1858 ArmLIR *branch1 = genCmpImmBranch(cUnit, kArmCondEq,
1859 rlSrc.lowReg, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07001860 /*
1861 * rlSrc.lowReg now contains object->clazz. Note that
1862 * it could have been allocated r0, but we're okay so long
1863 * as we don't do anything desctructive until r0 is loaded
1864 * with clazz.
1865 */
Ben Chengba4fc8b2009-06-01 13:00:29 -07001866 /* r0 now contains object->clazz */
Bill Buzbee1465db52009-09-23 17:17:35 -07001867 loadWordDisp(cUnit, rlSrc.lowReg, offsetof(Object, clazz), r0);
Ben Chengbd1326d2010-04-02 15:04:53 -07001868 LOAD_FUNC_ADDR(cUnit, r2, (int)dvmInstanceofNonTrivial);
Bill Buzbee1465db52009-09-23 17:17:35 -07001869 opRegReg(cUnit, kOpCmp, r0, r1);
1870 ArmLIR *branch2 = opCondBranch(cUnit, kArmCondEq);
1871 opReg(cUnit, kOpBlx, r2);
Elliott Hughes6a555132010-02-25 15:41:42 -08001872 dvmCompilerClobberCallRegs(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07001873 /*
1874 * If null, check cast failed - punt to the interpreter. Because
1875 * interpreter will be the one throwing, we don't need to
1876 * genExportPC() here.
1877 */
Bill Buzbee270c1d62009-08-13 16:58:07 -07001878 genZeroCheck(cUnit, r0, mir->offset, NULL);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001879 /* check cast passed - branch target here */
Bill Buzbee1465db52009-09-23 17:17:35 -07001880 ArmLIR *target = newLIR0(cUnit, kArmPseudoTargetLabel);
Ben Chengd7d426a2009-09-22 11:23:36 -07001881 target->defMask = ENCODE_ALL;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001882 branch1->generic.target = (LIR *)target;
1883 branch2->generic.target = (LIR *)target;
1884 break;
1885 }
buzbee4d92e682010-07-29 15:24:14 -07001886 case OP_SGET_WIDE_VOLATILE:
buzbeebd7865b2011-03-31 10:55:04 -07001887 case OP_SGET_WIDE_VOLATILE_JUMBO:
buzbee4d92e682010-07-29 15:24:14 -07001888 case OP_SPUT_WIDE_VOLATILE:
buzbeebd7865b2011-03-31 10:55:04 -07001889 case OP_SPUT_WIDE_VOLATILE_JUMBO:
buzbee4d92e682010-07-29 15:24:14 -07001890 genInterpSingleStep(cUnit, mir);
1891 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001892 default:
1893 return true;
1894 }
1895 return false;
1896}
1897
Ben Cheng7a2697d2010-06-07 13:44:23 -07001898/*
1899 * A typical example of inlined getter/setter from a monomorphic callsite:
1900 *
1901 * D/dalvikvm( 289): -------- dalvik offset: 0x0000 @ invoke-static (I)
1902 * D/dalvikvm( 289): -------- dalvik offset: 0x0000 @ sget-object (C) v0, ...
1903 * D/dalvikvm( 289): 0x4427fc22 (0002): ldr r0, [pc, #56]
1904 * D/dalvikvm( 289): 0x4427fc24 (0004): ldr r1, [r0, #0]
1905 * D/dalvikvm( 289): 0x4427fc26 (0006): str r1, [r5, #0]
1906 * D/dalvikvm( 289): 0x4427fc28 (0008): .align4
1907 * D/dalvikvm( 289): L0x0003:
1908 * D/dalvikvm( 289): -------- dalvik offset: 0x0003 @ move-result-object (I) v0
1909 *
1910 * Note the invoke-static and move-result-object with the (I) notation are
1911 * turned into no-op.
1912 */
Ben Chengba4fc8b2009-06-01 13:00:29 -07001913static bool handleFmt11x(CompilationUnit *cUnit, MIR *mir)
1914{
Dan Bornstein9a1f8162010-12-01 17:02:26 -08001915 Opcode dalvikOpcode = mir->dalvikInsn.opcode;
Bill Buzbee1465db52009-09-23 17:17:35 -07001916 RegLocation rlResult;
Dan Bornstein9a1f8162010-12-01 17:02:26 -08001917 switch (dalvikOpcode) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07001918 case OP_MOVE_EXCEPTION: {
Ben Chengba4fc8b2009-06-01 13:00:29 -07001919 int exOffset = offsetof(Thread, exception);
Bill Buzbeec6f10662010-02-09 11:16:15 -08001920 int resetReg = dvmCompilerAllocTemp(cUnit);
1921 RegLocation rlDest = dvmCompilerGetDest(cUnit, mir, 0);
1922 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Ben Cheng20d7e6c2011-02-18 17:12:42 -08001923 loadWordDisp(cUnit, r6SELF, exOffset, rlResult.lowReg);
Bill Buzbeef9f33282009-11-22 12:45:30 -08001924 loadConstant(cUnit, resetReg, 0);
Ben Cheng20d7e6c2011-02-18 17:12:42 -08001925 storeWordDisp(cUnit, r6SELF, exOffset, resetReg);
Bill Buzbee1465db52009-09-23 17:17:35 -07001926 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001927 break;
1928 }
1929 case OP_MOVE_RESULT:
1930 case OP_MOVE_RESULT_OBJECT: {
Ben Cheng7a2697d2010-06-07 13:44:23 -07001931 /* An inlined move result is effectively no-op */
1932 if (mir->OptimizationFlags & MIR_INLINED)
1933 break;
Bill Buzbeec6f10662010-02-09 11:16:15 -08001934 RegLocation rlDest = dvmCompilerGetDest(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07001935 RegLocation rlSrc = LOC_DALVIK_RETURN_VAL;
1936 rlSrc.fp = rlDest.fp;
1937 storeValue(cUnit, rlDest, rlSrc);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001938 break;
1939 }
1940 case OP_MOVE_RESULT_WIDE: {
Ben Cheng7a2697d2010-06-07 13:44:23 -07001941 /* An inlined move result is effectively no-op */
1942 if (mir->OptimizationFlags & MIR_INLINED)
1943 break;
Bill Buzbeec6f10662010-02-09 11:16:15 -08001944 RegLocation rlDest = dvmCompilerGetDestWide(cUnit, mir, 0, 1);
Bill Buzbee1465db52009-09-23 17:17:35 -07001945 RegLocation rlSrc = LOC_DALVIK_RETURN_VAL_WIDE;
1946 rlSrc.fp = rlDest.fp;
1947 storeValueWide(cUnit, rlDest, rlSrc);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001948 break;
1949 }
1950 case OP_RETURN_WIDE: {
Bill Buzbeec6f10662010-02-09 11:16:15 -08001951 RegLocation rlSrc = dvmCompilerGetSrcWide(cUnit, mir, 0, 1);
Bill Buzbee1465db52009-09-23 17:17:35 -07001952 RegLocation rlDest = LOC_DALVIK_RETURN_VAL_WIDE;
1953 rlDest.fp = rlSrc.fp;
1954 storeValueWide(cUnit, rlDest, rlSrc);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001955 genReturnCommon(cUnit,mir);
1956 break;
1957 }
1958 case OP_RETURN:
1959 case OP_RETURN_OBJECT: {
Bill Buzbeec6f10662010-02-09 11:16:15 -08001960 RegLocation rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07001961 RegLocation rlDest = LOC_DALVIK_RETURN_VAL;
1962 rlDest.fp = rlSrc.fp;
1963 storeValue(cUnit, rlDest, rlSrc);
Ben Cheng20d7e6c2011-02-18 17:12:42 -08001964 genReturnCommon(cUnit, mir);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001965 break;
1966 }
Bill Buzbee1465db52009-09-23 17:17:35 -07001967 case OP_MONITOR_EXIT:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001968 case OP_MONITOR_ENTER:
Ben Cheng5d90c202009-11-22 23:31:11 -08001969 genMonitor(cUnit, mir);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001970 break;
Ben Cheng20d7e6c2011-02-18 17:12:42 -08001971 case OP_THROW:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001972 genInterpSingleStep(cUnit, mir);
1973 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001974 default:
1975 return true;
1976 }
1977 return false;
1978}
1979
Bill Buzbeed45ba372009-06-15 17:00:57 -07001980static bool handleFmt12x(CompilationUnit *cUnit, MIR *mir)
1981{
Dan Bornstein9a1f8162010-12-01 17:02:26 -08001982 Opcode opcode = mir->dalvikInsn.opcode;
Bill Buzbee1465db52009-09-23 17:17:35 -07001983 RegLocation rlDest;
1984 RegLocation rlSrc;
1985 RegLocation rlResult;
Bill Buzbeed45ba372009-06-15 17:00:57 -07001986
Dan Bornstein9a1f8162010-12-01 17:02:26 -08001987 if ( (opcode >= OP_ADD_INT_2ADDR) && (opcode <= OP_REM_DOUBLE_2ADDR)) {
Ben Cheng5d90c202009-11-22 23:31:11 -08001988 return genArithOp( cUnit, mir );
Ben Chengba4fc8b2009-06-01 13:00:29 -07001989 }
1990
Bill Buzbee1465db52009-09-23 17:17:35 -07001991 if (mir->ssaRep->numUses == 2)
Bill Buzbeec6f10662010-02-09 11:16:15 -08001992 rlSrc = dvmCompilerGetSrcWide(cUnit, mir, 0, 1);
Bill Buzbee1465db52009-09-23 17:17:35 -07001993 else
Bill Buzbeec6f10662010-02-09 11:16:15 -08001994 rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07001995 if (mir->ssaRep->numDefs == 2)
Bill Buzbeec6f10662010-02-09 11:16:15 -08001996 rlDest = dvmCompilerGetDestWide(cUnit, mir, 0, 1);
Bill Buzbee1465db52009-09-23 17:17:35 -07001997 else
Bill Buzbeec6f10662010-02-09 11:16:15 -08001998 rlDest = dvmCompilerGetDest(cUnit, mir, 0);
Ben Chenge9695e52009-06-16 16:11:47 -07001999
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002000 switch (opcode) {
Bill Buzbee1465db52009-09-23 17:17:35 -07002001 case OP_DOUBLE_TO_INT:
Ben Chengba4fc8b2009-06-01 13:00:29 -07002002 case OP_INT_TO_FLOAT:
Ben Chengba4fc8b2009-06-01 13:00:29 -07002003 case OP_FLOAT_TO_INT:
Ben Chengba4fc8b2009-06-01 13:00:29 -07002004 case OP_DOUBLE_TO_FLOAT:
Ben Chengba4fc8b2009-06-01 13:00:29 -07002005 case OP_FLOAT_TO_DOUBLE:
Ben Chengba4fc8b2009-06-01 13:00:29 -07002006 case OP_INT_TO_DOUBLE:
Ben Chengba4fc8b2009-06-01 13:00:29 -07002007 case OP_FLOAT_TO_LONG:
Ben Chengba4fc8b2009-06-01 13:00:29 -07002008 case OP_LONG_TO_FLOAT:
Ben Chengba4fc8b2009-06-01 13:00:29 -07002009 case OP_DOUBLE_TO_LONG:
Ben Chengba4fc8b2009-06-01 13:00:29 -07002010 case OP_LONG_TO_DOUBLE:
Ben Cheng5d90c202009-11-22 23:31:11 -08002011 return genConversion(cUnit, mir);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002012 case OP_NEG_INT:
2013 case OP_NOT_INT:
Ben Cheng5d90c202009-11-22 23:31:11 -08002014 return genArithOpInt(cUnit, mir, rlDest, rlSrc, rlSrc);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002015 case OP_NEG_LONG:
2016 case OP_NOT_LONG:
Ben Cheng5d90c202009-11-22 23:31:11 -08002017 return genArithOpLong(cUnit, mir, rlDest, rlSrc, rlSrc);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002018 case OP_NEG_FLOAT:
Ben Cheng5d90c202009-11-22 23:31:11 -08002019 return genArithOpFloat(cUnit, mir, rlDest, rlSrc, rlSrc);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002020 case OP_NEG_DOUBLE:
Ben Cheng5d90c202009-11-22 23:31:11 -08002021 return genArithOpDouble(cUnit, mir, rlDest, rlSrc, rlSrc);
Bill Buzbee1465db52009-09-23 17:17:35 -07002022 case OP_MOVE_WIDE:
2023 storeValueWide(cUnit, rlDest, rlSrc);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002024 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07002025 case OP_INT_TO_LONG:
Bill Buzbeec6f10662010-02-09 11:16:15 -08002026 rlSrc = dvmCompilerUpdateLoc(cUnit, rlSrc);
2027 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Bill Buzbee964a7b02010-01-28 12:54:19 -08002028 //TUNING: shouldn't loadValueDirect already check for phys reg?
Bill Buzbee1465db52009-09-23 17:17:35 -07002029 if (rlSrc.location == kLocPhysReg) {
2030 genRegCopy(cUnit, rlResult.lowReg, rlSrc.lowReg);
2031 } else {
2032 loadValueDirect(cUnit, rlSrc, rlResult.lowReg);
2033 }
2034 opRegRegImm(cUnit, kOpAsr, rlResult.highReg,
2035 rlResult.lowReg, 31);
2036 storeValueWide(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002037 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07002038 case OP_LONG_TO_INT:
Bill Buzbeec6f10662010-02-09 11:16:15 -08002039 rlSrc = dvmCompilerUpdateLocWide(cUnit, rlSrc);
2040 rlSrc = dvmCompilerWideToNarrow(cUnit, rlSrc);
Bill Buzbee1465db52009-09-23 17:17:35 -07002041 // Intentional fallthrough
Ben Chengba4fc8b2009-06-01 13:00:29 -07002042 case OP_MOVE:
2043 case OP_MOVE_OBJECT:
Bill Buzbee1465db52009-09-23 17:17:35 -07002044 storeValue(cUnit, rlDest, rlSrc);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002045 break;
2046 case OP_INT_TO_BYTE:
Bill Buzbee1465db52009-09-23 17:17:35 -07002047 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
Bill Buzbeec6f10662010-02-09 11:16:15 -08002048 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Bill Buzbee1465db52009-09-23 17:17:35 -07002049 opRegReg(cUnit, kOp2Byte, rlResult.lowReg, rlSrc.lowReg);
2050 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002051 break;
2052 case OP_INT_TO_SHORT:
Bill Buzbee1465db52009-09-23 17:17:35 -07002053 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
Bill Buzbeec6f10662010-02-09 11:16:15 -08002054 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Bill Buzbee1465db52009-09-23 17:17:35 -07002055 opRegReg(cUnit, kOp2Short, rlResult.lowReg, rlSrc.lowReg);
2056 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002057 break;
2058 case OP_INT_TO_CHAR:
Bill Buzbee1465db52009-09-23 17:17:35 -07002059 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
Bill Buzbeec6f10662010-02-09 11:16:15 -08002060 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Bill Buzbee1465db52009-09-23 17:17:35 -07002061 opRegReg(cUnit, kOp2Char, rlResult.lowReg, rlSrc.lowReg);
2062 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002063 break;
2064 case OP_ARRAY_LENGTH: {
2065 int lenOffset = offsetof(ArrayObject, length);
Bill Buzbee1465db52009-09-23 17:17:35 -07002066 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
2067 genNullCheck(cUnit, rlSrc.sRegLow, rlSrc.lowReg,
2068 mir->offset, NULL);
Bill Buzbeec6f10662010-02-09 11:16:15 -08002069 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Bill Buzbee1465db52009-09-23 17:17:35 -07002070 loadWordDisp(cUnit, rlSrc.lowReg, lenOffset,
2071 rlResult.lowReg);
2072 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002073 break;
2074 }
2075 default:
2076 return true;
2077 }
2078 return false;
2079}
2080
2081static bool handleFmt21s(CompilationUnit *cUnit, MIR *mir)
2082{
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002083 Opcode dalvikOpcode = mir->dalvikInsn.opcode;
Bill Buzbee1465db52009-09-23 17:17:35 -07002084 RegLocation rlDest;
2085 RegLocation rlResult;
2086 int BBBB = mir->dalvikInsn.vB;
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002087 if (dalvikOpcode == OP_CONST_WIDE_16) {
Bill Buzbeec6f10662010-02-09 11:16:15 -08002088 rlDest = dvmCompilerGetDestWide(cUnit, mir, 0, 1);
2089 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Ben Chengbd1326d2010-04-02 15:04:53 -07002090 loadConstantNoClobber(cUnit, rlResult.lowReg, BBBB);
Bill Buzbee964a7b02010-01-28 12:54:19 -08002091 //TUNING: do high separately to avoid load dependency
Bill Buzbee1465db52009-09-23 17:17:35 -07002092 opRegRegImm(cUnit, kOpAsr, rlResult.highReg, rlResult.lowReg, 31);
2093 storeValueWide(cUnit, rlDest, rlResult);
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002094 } else if (dalvikOpcode == OP_CONST_16) {
Bill Buzbeec6f10662010-02-09 11:16:15 -08002095 rlDest = dvmCompilerGetDest(cUnit, mir, 0);
2096 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kAnyReg, true);
Ben Chengbd1326d2010-04-02 15:04:53 -07002097 loadConstantNoClobber(cUnit, rlResult.lowReg, BBBB);
Bill Buzbee1465db52009-09-23 17:17:35 -07002098 storeValue(cUnit, rlDest, rlResult);
2099 } else
Ben Chengba4fc8b2009-06-01 13:00:29 -07002100 return true;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002101 return false;
2102}
2103
2104/* Compare agaist zero */
2105static bool handleFmt21t(CompilationUnit *cUnit, MIR *mir, BasicBlock *bb,
Bill Buzbee89efc3d2009-07-28 11:22:22 -07002106 ArmLIR *labelList)
Ben Chengba4fc8b2009-06-01 13:00:29 -07002107{
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002108 Opcode dalvikOpcode = mir->dalvikInsn.opcode;
Bill Buzbee89efc3d2009-07-28 11:22:22 -07002109 ArmConditionCode cond;
Ben Cheng7ab74e12011-02-03 14:02:06 -08002110 /* backward branch? */
2111 bool backwardBranch = (bb->taken->startOffset <= mir->offset);
2112
Ben Cheng32115a92011-03-22 14:09:09 -07002113 if (backwardBranch &&
2114 (gDvmJit.genSuspendPoll || cUnit->jitMode == kJitLoop)) {
Ben Cheng7ab74e12011-02-03 14:02:06 -08002115 genSuspendPoll(cUnit, mir);
2116 }
2117
Bill Buzbeec6f10662010-02-09 11:16:15 -08002118 RegLocation rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07002119 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
Ben Cheng7ab74e12011-02-03 14:02:06 -08002120
Bill Buzbee1465db52009-09-23 17:17:35 -07002121 opRegImm(cUnit, kOpCmp, rlSrc.lowReg, 0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002122
Bill Buzbee270c1d62009-08-13 16:58:07 -07002123//TUNING: break this out to allow use of Thumb2 CB[N]Z
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002124 switch (dalvikOpcode) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07002125 case OP_IF_EQZ:
Bill Buzbee1465db52009-09-23 17:17:35 -07002126 cond = kArmCondEq;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002127 break;
2128 case OP_IF_NEZ:
Bill Buzbee1465db52009-09-23 17:17:35 -07002129 cond = kArmCondNe;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002130 break;
2131 case OP_IF_LTZ:
Bill Buzbee1465db52009-09-23 17:17:35 -07002132 cond = kArmCondLt;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002133 break;
2134 case OP_IF_GEZ:
Bill Buzbee1465db52009-09-23 17:17:35 -07002135 cond = kArmCondGe;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002136 break;
2137 case OP_IF_GTZ:
Bill Buzbee1465db52009-09-23 17:17:35 -07002138 cond = kArmCondGt;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002139 break;
2140 case OP_IF_LEZ:
Bill Buzbee1465db52009-09-23 17:17:35 -07002141 cond = kArmCondLe;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002142 break;
2143 default:
2144 cond = 0;
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002145 LOGE("Unexpected opcode (%d) for Fmt21t\n", dalvikOpcode);
Bill Buzbeefc519dc2010-03-06 23:30:57 -08002146 dvmCompilerAbort(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002147 }
2148 genConditionalBranch(cUnit, cond, &labelList[bb->taken->id]);
2149 /* This mostly likely will be optimized away in a later phase */
2150 genUnconditionalBranch(cUnit, &labelList[bb->fallThrough->id]);
2151 return false;
2152}
2153
Elliott Hughesb4c05972010-02-24 16:36:18 -08002154static bool isPowerOfTwo(int x)
2155{
2156 return (x & (x - 1)) == 0;
2157}
2158
2159// Returns true if no more than two bits are set in 'x'.
2160static bool isPopCountLE2(unsigned int x)
2161{
2162 x &= x - 1;
2163 return (x & (x - 1)) == 0;
2164}
2165
2166// Returns the index of the lowest set bit in 'x'.
2167static int lowestSetBit(unsigned int x) {
2168 int bit_posn = 0;
2169 while ((x & 0xf) == 0) {
2170 bit_posn += 4;
2171 x >>= 4;
Bill Buzbee78cb0e22010-02-11 14:04:53 -08002172 }
Elliott Hughesb4c05972010-02-24 16:36:18 -08002173 while ((x & 1) == 0) {
2174 bit_posn++;
2175 x >>= 1;
2176 }
2177 return bit_posn;
2178}
2179
Elliott Hughes672511b2010-04-26 17:40:13 -07002180// Returns true if it added instructions to 'cUnit' to divide 'rlSrc' by 'lit'
2181// and store the result in 'rlDest'.
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002182static bool handleEasyDivide(CompilationUnit *cUnit, Opcode dalvikOpcode,
Elliott Hughes672511b2010-04-26 17:40:13 -07002183 RegLocation rlSrc, RegLocation rlDest, int lit)
2184{
2185 if (lit < 2 || !isPowerOfTwo(lit)) {
2186 return false;
2187 }
2188 int k = lowestSetBit(lit);
2189 if (k >= 30) {
2190 // Avoid special cases.
2191 return false;
2192 }
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002193 bool div = (dalvikOpcode == OP_DIV_INT_LIT8 || dalvikOpcode == OP_DIV_INT_LIT16);
Elliott Hughes672511b2010-04-26 17:40:13 -07002194 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
2195 RegLocation rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Elliott Hughes9c457022010-04-28 16:15:38 -07002196 if (div) {
2197 int tReg = dvmCompilerAllocTemp(cUnit);
2198 if (lit == 2) {
2199 // Division by 2 is by far the most common division by constant.
2200 opRegRegImm(cUnit, kOpLsr, tReg, rlSrc.lowReg, 32 - k);
2201 opRegRegReg(cUnit, kOpAdd, tReg, tReg, rlSrc.lowReg);
2202 opRegRegImm(cUnit, kOpAsr, rlResult.lowReg, tReg, k);
2203 } else {
2204 opRegRegImm(cUnit, kOpAsr, tReg, rlSrc.lowReg, 31);
2205 opRegRegImm(cUnit, kOpLsr, tReg, tReg, 32 - k);
2206 opRegRegReg(cUnit, kOpAdd, tReg, tReg, rlSrc.lowReg);
2207 opRegRegImm(cUnit, kOpAsr, rlResult.lowReg, tReg, k);
2208 }
Elliott Hughes672511b2010-04-26 17:40:13 -07002209 } else {
Elliott Hughes9c457022010-04-28 16:15:38 -07002210 int cReg = dvmCompilerAllocTemp(cUnit);
2211 loadConstant(cUnit, cReg, lit - 1);
2212 int tReg1 = dvmCompilerAllocTemp(cUnit);
2213 int tReg2 = dvmCompilerAllocTemp(cUnit);
2214 if (lit == 2) {
2215 opRegRegImm(cUnit, kOpLsr, tReg1, rlSrc.lowReg, 32 - k);
2216 opRegRegReg(cUnit, kOpAdd, tReg2, tReg1, rlSrc.lowReg);
2217 opRegRegReg(cUnit, kOpAnd, tReg2, tReg2, cReg);
2218 opRegRegReg(cUnit, kOpSub, rlResult.lowReg, tReg2, tReg1);
2219 } else {
2220 opRegRegImm(cUnit, kOpAsr, tReg1, rlSrc.lowReg, 31);
2221 opRegRegImm(cUnit, kOpLsr, tReg1, tReg1, 32 - k);
2222 opRegRegReg(cUnit, kOpAdd, tReg2, tReg1, rlSrc.lowReg);
2223 opRegRegReg(cUnit, kOpAnd, tReg2, tReg2, cReg);
2224 opRegRegReg(cUnit, kOpSub, rlResult.lowReg, tReg2, tReg1);
2225 }
Elliott Hughes672511b2010-04-26 17:40:13 -07002226 }
2227 storeValue(cUnit, rlDest, rlResult);
2228 return true;
2229}
2230
Elliott Hughesb4c05972010-02-24 16:36:18 -08002231// Returns true if it added instructions to 'cUnit' to multiply 'rlSrc' by 'lit'
2232// and store the result in 'rlDest'.
2233static bool handleEasyMultiply(CompilationUnit *cUnit,
2234 RegLocation rlSrc, RegLocation rlDest, int lit)
2235{
2236 // Can we simplify this multiplication?
2237 bool powerOfTwo = false;
2238 bool popCountLE2 = false;
2239 bool powerOfTwoMinusOne = false;
2240 if (lit < 2) {
2241 // Avoid special cases.
2242 return false;
2243 } else if (isPowerOfTwo(lit)) {
2244 powerOfTwo = true;
2245 } else if (isPopCountLE2(lit)) {
2246 popCountLE2 = true;
2247 } else if (isPowerOfTwo(lit + 1)) {
2248 powerOfTwoMinusOne = true;
2249 } else {
2250 return false;
2251 }
2252 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
2253 RegLocation rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
2254 if (powerOfTwo) {
2255 // Shift.
2256 opRegRegImm(cUnit, kOpLsl, rlResult.lowReg, rlSrc.lowReg,
2257 lowestSetBit(lit));
2258 } else if (popCountLE2) {
2259 // Shift and add and shift.
2260 int firstBit = lowestSetBit(lit);
2261 int secondBit = lowestSetBit(lit ^ (1 << firstBit));
2262 genMultiplyByTwoBitMultiplier(cUnit, rlSrc, rlResult, lit,
2263 firstBit, secondBit);
2264 } else {
2265 // Reverse subtract: (src << (shift + 1)) - src.
2266 assert(powerOfTwoMinusOne);
2267 // TODO: rsb dst, src, src lsl#lowestSetBit(lit + 1)
2268 int tReg = dvmCompilerAllocTemp(cUnit);
2269 opRegRegImm(cUnit, kOpLsl, tReg, rlSrc.lowReg, lowestSetBit(lit + 1));
2270 opRegRegReg(cUnit, kOpSub, rlResult.lowReg, tReg, rlSrc.lowReg);
2271 }
2272 storeValue(cUnit, rlDest, rlResult);
2273 return true;
Bill Buzbee78cb0e22010-02-11 14:04:53 -08002274}
2275
Ben Chengba4fc8b2009-06-01 13:00:29 -07002276static bool handleFmt22b_Fmt22s(CompilationUnit *cUnit, MIR *mir)
2277{
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002278 Opcode dalvikOpcode = mir->dalvikInsn.opcode;
Bill Buzbeec6f10662010-02-09 11:16:15 -08002279 RegLocation rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
2280 RegLocation rlDest = dvmCompilerGetDest(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07002281 RegLocation rlResult;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002282 int lit = mir->dalvikInsn.vC;
Ben Cheng4f489172009-09-27 17:08:35 -07002283 OpKind op = 0; /* Make gcc happy */
Bill Buzbee1465db52009-09-23 17:17:35 -07002284 int shiftOp = false;
2285 bool isDiv = false;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002286
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002287 switch (dalvikOpcode) {
Bill Buzbee1465db52009-09-23 17:17:35 -07002288 case OP_RSUB_INT_LIT8:
2289 case OP_RSUB_INT: {
2290 int tReg;
2291 //TUNING: add support for use of Arm rsub op
2292 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
Bill Buzbeec6f10662010-02-09 11:16:15 -08002293 tReg = dvmCompilerAllocTemp(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07002294 loadConstant(cUnit, tReg, lit);
Bill Buzbeec6f10662010-02-09 11:16:15 -08002295 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Bill Buzbee1465db52009-09-23 17:17:35 -07002296 opRegRegReg(cUnit, kOpSub, rlResult.lowReg,
2297 tReg, rlSrc.lowReg);
2298 storeValue(cUnit, rlDest, rlResult);
2299 return false;
2300 break;
2301 }
2302
Ben Chengba4fc8b2009-06-01 13:00:29 -07002303 case OP_ADD_INT_LIT8:
2304 case OP_ADD_INT_LIT16:
Bill Buzbee1465db52009-09-23 17:17:35 -07002305 op = kOpAdd;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002306 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002307 case OP_MUL_INT_LIT8:
Bill Buzbee78cb0e22010-02-11 14:04:53 -08002308 case OP_MUL_INT_LIT16: {
Elliott Hughesb4c05972010-02-24 16:36:18 -08002309 if (handleEasyMultiply(cUnit, rlSrc, rlDest, lit)) {
2310 return false;
Bill Buzbee78cb0e22010-02-11 14:04:53 -08002311 }
Elliott Hughesb4c05972010-02-24 16:36:18 -08002312 op = kOpMul;
Bill Buzbee1465db52009-09-23 17:17:35 -07002313 break;
Bill Buzbee78cb0e22010-02-11 14:04:53 -08002314 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07002315 case OP_AND_INT_LIT8:
2316 case OP_AND_INT_LIT16:
Bill Buzbee1465db52009-09-23 17:17:35 -07002317 op = kOpAnd;
2318 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002319 case OP_OR_INT_LIT8:
2320 case OP_OR_INT_LIT16:
Bill Buzbee1465db52009-09-23 17:17:35 -07002321 op = kOpOr;
2322 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002323 case OP_XOR_INT_LIT8:
2324 case OP_XOR_INT_LIT16:
Bill Buzbee1465db52009-09-23 17:17:35 -07002325 op = kOpXor;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002326 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002327 case OP_SHL_INT_LIT8:
Bill Buzbee0e605272009-12-01 14:28:05 -08002328 lit &= 31;
Bill Buzbee1465db52009-09-23 17:17:35 -07002329 shiftOp = true;
2330 op = kOpLsl;
2331 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002332 case OP_SHR_INT_LIT8:
Bill Buzbee0e605272009-12-01 14:28:05 -08002333 lit &= 31;
Bill Buzbee1465db52009-09-23 17:17:35 -07002334 shiftOp = true;
2335 op = kOpAsr;
2336 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002337 case OP_USHR_INT_LIT8:
Bill Buzbee0e605272009-12-01 14:28:05 -08002338 lit &= 31;
Bill Buzbee1465db52009-09-23 17:17:35 -07002339 shiftOp = true;
2340 op = kOpLsr;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002341 break;
2342
2343 case OP_DIV_INT_LIT8:
2344 case OP_DIV_INT_LIT16:
Ben Chengba4fc8b2009-06-01 13:00:29 -07002345 case OP_REM_INT_LIT8:
2346 case OP_REM_INT_LIT16:
2347 if (lit == 0) {
2348 /* Let the interpreter deal with div by 0 */
2349 genInterpSingleStep(cUnit, mir);
2350 return false;
2351 }
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002352 if (handleEasyDivide(cUnit, dalvikOpcode, rlSrc, rlDest, lit)) {
Elliott Hughes672511b2010-04-26 17:40:13 -07002353 return false;
2354 }
Bill Buzbeec6f10662010-02-09 11:16:15 -08002355 dvmCompilerFlushAllRegs(cUnit); /* Everything to home location */
Bill Buzbee1465db52009-09-23 17:17:35 -07002356 loadValueDirectFixed(cUnit, rlSrc, r0);
Bill Buzbeec6f10662010-02-09 11:16:15 -08002357 dvmCompilerClobber(cUnit, r0);
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002358 if ((dalvikOpcode == OP_DIV_INT_LIT8) ||
2359 (dalvikOpcode == OP_DIV_INT_LIT16)) {
Ben Chengbd1326d2010-04-02 15:04:53 -07002360 LOAD_FUNC_ADDR(cUnit, r2, (int)__aeabi_idiv);
Bill Buzbee1465db52009-09-23 17:17:35 -07002361 isDiv = true;
2362 } else {
Ben Chengbd1326d2010-04-02 15:04:53 -07002363 LOAD_FUNC_ADDR(cUnit, r2, (int)__aeabi_idivmod);
Bill Buzbee1465db52009-09-23 17:17:35 -07002364 isDiv = false;
2365 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07002366 loadConstant(cUnit, r1, lit);
Bill Buzbee1465db52009-09-23 17:17:35 -07002367 opReg(cUnit, kOpBlx, r2);
Elliott Hughes6a555132010-02-25 15:41:42 -08002368 dvmCompilerClobberCallRegs(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07002369 if (isDiv)
Bill Buzbeec6f10662010-02-09 11:16:15 -08002370 rlResult = dvmCompilerGetReturn(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07002371 else
Bill Buzbeec6f10662010-02-09 11:16:15 -08002372 rlResult = dvmCompilerGetReturnAlt(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07002373 storeValue(cUnit, rlDest, rlResult);
2374 return false;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002375 break;
2376 default:
2377 return true;
2378 }
Bill Buzbee1465db52009-09-23 17:17:35 -07002379 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
Bill Buzbeec6f10662010-02-09 11:16:15 -08002380 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Bill Buzbee1465db52009-09-23 17:17:35 -07002381 // Avoid shifts by literal 0 - no support in Thumb. Change to copy
2382 if (shiftOp && (lit == 0)) {
2383 genRegCopy(cUnit, rlResult.lowReg, rlSrc.lowReg);
2384 } else {
2385 opRegRegImm(cUnit, op, rlResult.lowReg, rlSrc.lowReg, lit);
2386 }
2387 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002388 return false;
2389}
2390
jeffhao71eee1f2011-01-04 14:18:54 -08002391static bool handleFmt22c_Fmt52c(CompilationUnit *cUnit, MIR *mir)
Ben Chengba4fc8b2009-06-01 13:00:29 -07002392{
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002393 Opcode dalvikOpcode = mir->dalvikInsn.opcode;
buzbee4d92e682010-07-29 15:24:14 -07002394 int fieldOffset = -1;
buzbeeecf8f6e2010-07-20 14:53:42 -07002395 bool isVolatile = false;
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002396 switch (dalvikOpcode) {
buzbee4d92e682010-07-29 15:24:14 -07002397 /*
2398 * Wide volatiles currently handled via single step.
2399 * Add them here if generating in-line code.
2400 * case OP_IGET_WIDE_VOLATILE:
buzbeebd7865b2011-03-31 10:55:04 -07002401 * case OP_IGET_WIDE_VOLATILE_JUMBO:
buzbee4d92e682010-07-29 15:24:14 -07002402 * case OP_IPUT_WIDE_VOLATILE:
buzbeebd7865b2011-03-31 10:55:04 -07002403 * case OP_IPUT_WIDE_VOLATILE_JUMBO:
buzbee4d92e682010-07-29 15:24:14 -07002404 */
buzbee4d92e682010-07-29 15:24:14 -07002405 case OP_IGET_VOLATILE:
buzbeebd7865b2011-03-31 10:55:04 -07002406 case OP_IGET_VOLATILE_JUMBO:
buzbee8b94be12011-04-04 12:25:57 -07002407 case OP_IGET_OBJECT_VOLATILE:
2408 case OP_IGET_OBJECT_VOLATILE_JUMBO:
2409 case OP_IPUT_VOLATILE:
2410 case OP_IPUT_VOLATILE_JUMBO:
2411 case OP_IPUT_OBJECT_VOLATILE:
2412 case OP_IPUT_OBJECT_VOLATILE_JUMBO:
2413 isVolatile = true;
2414 // NOTE: intentional fallthrough
2415 case OP_IGET:
jeffhao71eee1f2011-01-04 14:18:54 -08002416 case OP_IGET_JUMBO:
buzbee4d92e682010-07-29 15:24:14 -07002417 case OP_IGET_WIDE:
jeffhao71eee1f2011-01-04 14:18:54 -08002418 case OP_IGET_WIDE_JUMBO:
buzbee4d92e682010-07-29 15:24:14 -07002419 case OP_IGET_OBJECT:
jeffhao71eee1f2011-01-04 14:18:54 -08002420 case OP_IGET_OBJECT_JUMBO:
buzbee4d92e682010-07-29 15:24:14 -07002421 case OP_IGET_BOOLEAN:
jeffhao71eee1f2011-01-04 14:18:54 -08002422 case OP_IGET_BOOLEAN_JUMBO:
buzbee4d92e682010-07-29 15:24:14 -07002423 case OP_IGET_BYTE:
jeffhao71eee1f2011-01-04 14:18:54 -08002424 case OP_IGET_BYTE_JUMBO:
buzbee4d92e682010-07-29 15:24:14 -07002425 case OP_IGET_CHAR:
jeffhao71eee1f2011-01-04 14:18:54 -08002426 case OP_IGET_CHAR_JUMBO:
buzbee4d92e682010-07-29 15:24:14 -07002427 case OP_IGET_SHORT:
jeffhao71eee1f2011-01-04 14:18:54 -08002428 case OP_IGET_SHORT_JUMBO:
buzbee4d92e682010-07-29 15:24:14 -07002429 case OP_IPUT:
jeffhao71eee1f2011-01-04 14:18:54 -08002430 case OP_IPUT_JUMBO:
buzbee4d92e682010-07-29 15:24:14 -07002431 case OP_IPUT_WIDE:
jeffhao71eee1f2011-01-04 14:18:54 -08002432 case OP_IPUT_WIDE_JUMBO:
buzbee4d92e682010-07-29 15:24:14 -07002433 case OP_IPUT_OBJECT:
jeffhao71eee1f2011-01-04 14:18:54 -08002434 case OP_IPUT_OBJECT_JUMBO:
buzbee4d92e682010-07-29 15:24:14 -07002435 case OP_IPUT_BOOLEAN:
jeffhao71eee1f2011-01-04 14:18:54 -08002436 case OP_IPUT_BOOLEAN_JUMBO:
buzbee4d92e682010-07-29 15:24:14 -07002437 case OP_IPUT_BYTE:
jeffhao71eee1f2011-01-04 14:18:54 -08002438 case OP_IPUT_BYTE_JUMBO:
buzbee4d92e682010-07-29 15:24:14 -07002439 case OP_IPUT_CHAR:
jeffhao71eee1f2011-01-04 14:18:54 -08002440 case OP_IPUT_CHAR_JUMBO:
2441 case OP_IPUT_SHORT:
2442 case OP_IPUT_SHORT_JUMBO: {
Ben Cheng7a2697d2010-06-07 13:44:23 -07002443 const Method *method = (mir->OptimizationFlags & MIR_CALLEE) ?
2444 mir->meta.calleeMethod : cUnit->method;
buzbee4d92e682010-07-29 15:24:14 -07002445 Field *fieldPtr =
Ben Cheng7a2697d2010-06-07 13:44:23 -07002446 method->clazz->pDvmDex->pResFields[mir->dalvikInsn.vC];
Ben Chengba4fc8b2009-06-01 13:00:29 -07002447
buzbee4d92e682010-07-29 15:24:14 -07002448 if (fieldPtr == NULL) {
Ben Cheng32115a92011-03-22 14:09:09 -07002449 BAIL_LOOP_COMPILATION();
buzbee4d92e682010-07-29 15:24:14 -07002450 LOGE("Unexpected null instance field");
2451 dvmAbort();
2452 }
Joe Onorato80d118a2011-04-08 15:32:34 -07002453 assert(isVolatile == dvmIsVolatileField(fieldPtr));
buzbee4d92e682010-07-29 15:24:14 -07002454 fieldOffset = ((InstField *)fieldPtr)->byteOffset;
2455 break;
Ben Chengdd6e8702010-05-07 13:05:47 -07002456 }
buzbee4d92e682010-07-29 15:24:14 -07002457 default:
2458 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002459 }
buzbee4d92e682010-07-29 15:24:14 -07002460
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002461 switch (dalvikOpcode) {
jeffhao71eee1f2011-01-04 14:18:54 -08002462 case OP_NEW_ARRAY:
2463 case OP_NEW_ARRAY_JUMBO: {
Bill Buzbee1465db52009-09-23 17:17:35 -07002464 // Generates a call - use explicit registers
Bill Buzbeec6f10662010-02-09 11:16:15 -08002465 RegLocation rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
2466 RegLocation rlDest = dvmCompilerGetDest(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07002467 RegLocation rlResult;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002468 void *classPtr = (void*)
2469 (cUnit->method->clazz->pDvmDex->pResClasses[mir->dalvikInsn.vC]);
Ben Chengdd6e8702010-05-07 13:05:47 -07002470
2471 if (classPtr == NULL) {
Ben Cheng32115a92011-03-22 14:09:09 -07002472 BAIL_LOOP_COMPILATION();
Ben Chengdd6e8702010-05-07 13:05:47 -07002473 LOGE("Unexpected null class");
2474 dvmAbort();
2475 }
2476
Bill Buzbeec6f10662010-02-09 11:16:15 -08002477 dvmCompilerFlushAllRegs(cUnit); /* Everything to home location */
Bill Buzbee1465db52009-09-23 17:17:35 -07002478 genExportPC(cUnit, mir);
2479 loadValueDirectFixed(cUnit, rlSrc, r1); /* Len */
Ben Chengba4fc8b2009-06-01 13:00:29 -07002480 loadConstant(cUnit, r0, (int) classPtr );
Ben Chengbd1326d2010-04-02 15:04:53 -07002481 LOAD_FUNC_ADDR(cUnit, r3, (int)dvmAllocArrayByClass);
Ben Cheng4f489172009-09-27 17:08:35 -07002482 /*
2483 * "len < 0": bail to the interpreter to re-execute the
2484 * instruction
2485 */
Carl Shapiroe3c01da2010-05-20 22:54:18 -07002486 genRegImmCheck(cUnit, kArmCondMi, r1, 0, mir->offset, NULL);
Bill Buzbee270c1d62009-08-13 16:58:07 -07002487 loadConstant(cUnit, r2, ALLOC_DONT_TRACK);
Bill Buzbee1465db52009-09-23 17:17:35 -07002488 opReg(cUnit, kOpBlx, r3);
Elliott Hughes6a555132010-02-25 15:41:42 -08002489 dvmCompilerClobberCallRegs(cUnit);
Ben Cheng4f489172009-09-27 17:08:35 -07002490 /* generate a branch over if allocation is successful */
buzbee8f8109a2010-08-31 10:16:35 -07002491 ArmLIR *branchOver = genCmpImmBranch(cUnit, kArmCondNe, r0, 0);
Ben Cheng4f489172009-09-27 17:08:35 -07002492 /*
2493 * OOM exception needs to be thrown here and cannot re-execute
2494 */
2495 loadConstant(cUnit, r0,
2496 (int) (cUnit->method->insns + mir->offset));
2497 genDispatchToHandler(cUnit, TEMPLATE_THROW_EXCEPTION_COMMON);
2498 /* noreturn */
2499
Bill Buzbee1465db52009-09-23 17:17:35 -07002500 ArmLIR *target = newLIR0(cUnit, kArmPseudoTargetLabel);
Ben Cheng4f489172009-09-27 17:08:35 -07002501 target->defMask = ENCODE_ALL;
2502 branchOver->generic.target = (LIR *) target;
Bill Buzbeec6f10662010-02-09 11:16:15 -08002503 rlResult = dvmCompilerGetReturn(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07002504 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002505 break;
2506 }
jeffhao71eee1f2011-01-04 14:18:54 -08002507 case OP_INSTANCE_OF:
2508 case OP_INSTANCE_OF_JUMBO: {
Bill Buzbee1465db52009-09-23 17:17:35 -07002509 // May generate a call - use explicit registers
Bill Buzbeec6f10662010-02-09 11:16:15 -08002510 RegLocation rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
2511 RegLocation rlDest = dvmCompilerGetDest(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07002512 RegLocation rlResult;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002513 ClassObject *classPtr =
2514 (cUnit->method->clazz->pDvmDex->pResClasses[mir->dalvikInsn.vC]);
Bill Buzbee480e6782010-01-27 15:43:08 -08002515 /*
2516 * Note: It is possible that classPtr is NULL at this point,
2517 * even though this instruction has been successfully interpreted.
2518 * If the previous interpretation had a null source, the
2519 * interpreter would not have bothered to resolve the clazz.
2520 * Bail out to the interpreter in this case, and log it
2521 * so that we can tell if it happens frequently.
2522 */
2523 if (classPtr == NULL) {
Ben Cheng32115a92011-03-22 14:09:09 -07002524 BAIL_LOOP_COMPILATION();
Bill Buzbee480e6782010-01-27 15:43:08 -08002525 LOGD("null clazz in OP_INSTANCE_OF, single-stepping");
2526 genInterpSingleStep(cUnit, mir);
2527 break;
2528 }
Bill Buzbeec6f10662010-02-09 11:16:15 -08002529 dvmCompilerFlushAllRegs(cUnit); /* Everything to home location */
Bill Buzbee1465db52009-09-23 17:17:35 -07002530 loadValueDirectFixed(cUnit, rlSrc, r0); /* Ref */
Ben Chengba4fc8b2009-06-01 13:00:29 -07002531 loadConstant(cUnit, r2, (int) classPtr );
Ben Cheng752c7942009-06-22 10:50:07 -07002532 /* When taken r0 has NULL which can be used for store directly */
buzbee8f8109a2010-08-31 10:16:35 -07002533 ArmLIR *branch1 = genCmpImmBranch(cUnit, kArmCondEq, r0, 0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002534 /* r1 now contains object->clazz */
Bill Buzbee270c1d62009-08-13 16:58:07 -07002535 loadWordDisp(cUnit, r0, offsetof(Object, clazz), r1);
Bill Buzbee1465db52009-09-23 17:17:35 -07002536 /* r1 now contains object->clazz */
Ben Chengbd1326d2010-04-02 15:04:53 -07002537 LOAD_FUNC_ADDR(cUnit, r3, (int)dvmInstanceofNonTrivial);
Ben Cheng752c7942009-06-22 10:50:07 -07002538 loadConstant(cUnit, r0, 1); /* Assume true */
Bill Buzbee1465db52009-09-23 17:17:35 -07002539 opRegReg(cUnit, kOpCmp, r1, r2);
2540 ArmLIR *branch2 = opCondBranch(cUnit, kArmCondEq);
2541 genRegCopy(cUnit, r0, r1);
2542 genRegCopy(cUnit, r1, r2);
2543 opReg(cUnit, kOpBlx, r3);
Elliott Hughes6a555132010-02-25 15:41:42 -08002544 dvmCompilerClobberCallRegs(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002545 /* branch target here */
Bill Buzbee1465db52009-09-23 17:17:35 -07002546 ArmLIR *target = newLIR0(cUnit, kArmPseudoTargetLabel);
Ben Chengd7d426a2009-09-22 11:23:36 -07002547 target->defMask = ENCODE_ALL;
Bill Buzbeec6f10662010-02-09 11:16:15 -08002548 rlResult = dvmCompilerGetReturn(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07002549 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002550 branch1->generic.target = (LIR *)target;
2551 branch2->generic.target = (LIR *)target;
2552 break;
2553 }
2554 case OP_IGET_WIDE:
jeffhao71eee1f2011-01-04 14:18:54 -08002555 case OP_IGET_WIDE_JUMBO:
Ben Chengba4fc8b2009-06-01 13:00:29 -07002556 genIGetWide(cUnit, mir, fieldOffset);
2557 break;
buzbeeecf8f6e2010-07-20 14:53:42 -07002558 case OP_IGET_VOLATILE:
buzbeebd7865b2011-03-31 10:55:04 -07002559 case OP_IGET_VOLATILE_JUMBO:
buzbeeecf8f6e2010-07-20 14:53:42 -07002560 case OP_IGET_OBJECT_VOLATILE:
buzbeebd7865b2011-03-31 10:55:04 -07002561 case OP_IGET_OBJECT_VOLATILE_JUMBO:
Joe Onorato80d118a2011-04-08 15:32:34 -07002562 isVolatile = true;
2563 // NOTE: intentional fallthrough
Ben Chengba4fc8b2009-06-01 13:00:29 -07002564 case OP_IGET:
jeffhao71eee1f2011-01-04 14:18:54 -08002565 case OP_IGET_JUMBO:
Ben Chengba4fc8b2009-06-01 13:00:29 -07002566 case OP_IGET_OBJECT:
jeffhao71eee1f2011-01-04 14:18:54 -08002567 case OP_IGET_OBJECT_JUMBO:
Ben Chengba4fc8b2009-06-01 13:00:29 -07002568 case OP_IGET_BOOLEAN:
jeffhao71eee1f2011-01-04 14:18:54 -08002569 case OP_IGET_BOOLEAN_JUMBO:
Ben Chengba4fc8b2009-06-01 13:00:29 -07002570 case OP_IGET_BYTE:
jeffhao71eee1f2011-01-04 14:18:54 -08002571 case OP_IGET_BYTE_JUMBO:
Ben Chengba4fc8b2009-06-01 13:00:29 -07002572 case OP_IGET_CHAR:
jeffhao71eee1f2011-01-04 14:18:54 -08002573 case OP_IGET_CHAR_JUMBO:
Ben Chengba4fc8b2009-06-01 13:00:29 -07002574 case OP_IGET_SHORT:
jeffhao71eee1f2011-01-04 14:18:54 -08002575 case OP_IGET_SHORT_JUMBO:
buzbee3272e2f2010-09-09 14:07:01 -07002576 genIGet(cUnit, mir, kWord, fieldOffset, isVolatile);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002577 break;
2578 case OP_IPUT_WIDE:
jeffhao71eee1f2011-01-04 14:18:54 -08002579 case OP_IPUT_WIDE_JUMBO:
Ben Chengba4fc8b2009-06-01 13:00:29 -07002580 genIPutWide(cUnit, mir, fieldOffset);
2581 break;
Carl Shapiro49f30642011-04-04 10:20:43 -07002582 case OP_IPUT_VOLATILE:
2583 case OP_IPUT_VOLATILE_JUMBO:
Joe Onorato80d118a2011-04-08 15:32:34 -07002584 isVolatile = true;
2585 // NOTE: intentional fallthrough
Ben Chengba4fc8b2009-06-01 13:00:29 -07002586 case OP_IPUT:
jeffhao71eee1f2011-01-04 14:18:54 -08002587 case OP_IPUT_JUMBO:
buzbee3272e2f2010-09-09 14:07:01 -07002588 case OP_IPUT_BOOLEAN:
jeffhao71eee1f2011-01-04 14:18:54 -08002589 case OP_IPUT_BOOLEAN_JUMBO:
2590 case OP_IPUT_BYTE:
2591 case OP_IPUT_BYTE_JUMBO:
2592 case OP_IPUT_CHAR:
2593 case OP_IPUT_CHAR_JUMBO:
2594 case OP_IPUT_SHORT:
2595 case OP_IPUT_SHORT_JUMBO:
buzbeeecf8f6e2010-07-20 14:53:42 -07002596 genIPut(cUnit, mir, kWord, fieldOffset, false, isVolatile);
buzbee919eb062010-07-12 12:59:22 -07002597 break;
buzbeeecf8f6e2010-07-20 14:53:42 -07002598 case OP_IPUT_OBJECT_VOLATILE:
buzbeebd7865b2011-03-31 10:55:04 -07002599 case OP_IPUT_OBJECT_VOLATILE_JUMBO:
Joe Onorato80d118a2011-04-08 15:32:34 -07002600 isVolatile = true;
2601 // NOTE: intentional fallthrough
Ben Chengba4fc8b2009-06-01 13:00:29 -07002602 case OP_IPUT_OBJECT:
jeffhao71eee1f2011-01-04 14:18:54 -08002603 case OP_IPUT_OBJECT_JUMBO:
buzbeeecf8f6e2010-07-20 14:53:42 -07002604 genIPut(cUnit, mir, kWord, fieldOffset, true, isVolatile);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002605 break;
Bill Buzbeeb16344a2010-03-15 17:19:12 -07002606 case OP_IGET_WIDE_VOLATILE:
buzbeebd7865b2011-03-31 10:55:04 -07002607 case OP_IGET_WIDE_VOLATILE_JUMBO:
Bill Buzbeeb16344a2010-03-15 17:19:12 -07002608 case OP_IPUT_WIDE_VOLATILE:
buzbeebd7865b2011-03-31 10:55:04 -07002609 case OP_IPUT_WIDE_VOLATILE_JUMBO:
Bill Buzbeeb16344a2010-03-15 17:19:12 -07002610 genInterpSingleStep(cUnit, mir);
2611 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002612 default:
2613 return true;
2614 }
2615 return false;
2616}
2617
2618static bool handleFmt22cs(CompilationUnit *cUnit, MIR *mir)
2619{
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002620 Opcode dalvikOpcode = mir->dalvikInsn.opcode;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002621 int fieldOffset = mir->dalvikInsn.vC;
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002622 switch (dalvikOpcode) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07002623 case OP_IGET_QUICK:
2624 case OP_IGET_OBJECT_QUICK:
buzbeeecf8f6e2010-07-20 14:53:42 -07002625 genIGet(cUnit, mir, kWord, fieldOffset, false);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002626 break;
2627 case OP_IPUT_QUICK:
buzbeeecf8f6e2010-07-20 14:53:42 -07002628 genIPut(cUnit, mir, kWord, fieldOffset, false, false);
buzbee919eb062010-07-12 12:59:22 -07002629 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002630 case OP_IPUT_OBJECT_QUICK:
buzbeeecf8f6e2010-07-20 14:53:42 -07002631 genIPut(cUnit, mir, kWord, fieldOffset, true, false);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002632 break;
2633 case OP_IGET_WIDE_QUICK:
2634 genIGetWide(cUnit, mir, fieldOffset);
2635 break;
2636 case OP_IPUT_WIDE_QUICK:
2637 genIPutWide(cUnit, mir, fieldOffset);
2638 break;
2639 default:
2640 return true;
2641 }
2642 return false;
2643
2644}
2645
2646/* Compare agaist zero */
2647static bool handleFmt22t(CompilationUnit *cUnit, MIR *mir, BasicBlock *bb,
Bill Buzbee89efc3d2009-07-28 11:22:22 -07002648 ArmLIR *labelList)
Ben Chengba4fc8b2009-06-01 13:00:29 -07002649{
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002650 Opcode dalvikOpcode = mir->dalvikInsn.opcode;
Bill Buzbee89efc3d2009-07-28 11:22:22 -07002651 ArmConditionCode cond;
Ben Cheng7ab74e12011-02-03 14:02:06 -08002652 /* backward branch? */
2653 bool backwardBranch = (bb->taken->startOffset <= mir->offset);
2654
Ben Cheng32115a92011-03-22 14:09:09 -07002655 if (backwardBranch &&
2656 (gDvmJit.genSuspendPoll || cUnit->jitMode == kJitLoop)) {
Ben Cheng7ab74e12011-02-03 14:02:06 -08002657 genSuspendPoll(cUnit, mir);
2658 }
2659
Bill Buzbeec6f10662010-02-09 11:16:15 -08002660 RegLocation rlSrc1 = dvmCompilerGetSrc(cUnit, mir, 0);
2661 RegLocation rlSrc2 = dvmCompilerGetSrc(cUnit, mir, 1);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002662
Bill Buzbee1465db52009-09-23 17:17:35 -07002663 rlSrc1 = loadValue(cUnit, rlSrc1, kCoreReg);
2664 rlSrc2 = loadValue(cUnit, rlSrc2, kCoreReg);
Ben Cheng7ab74e12011-02-03 14:02:06 -08002665
Bill Buzbee1465db52009-09-23 17:17:35 -07002666 opRegReg(cUnit, kOpCmp, rlSrc1.lowReg, rlSrc2.lowReg);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002667
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002668 switch (dalvikOpcode) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07002669 case OP_IF_EQ:
Bill Buzbee1465db52009-09-23 17:17:35 -07002670 cond = kArmCondEq;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002671 break;
2672 case OP_IF_NE:
Bill Buzbee1465db52009-09-23 17:17:35 -07002673 cond = kArmCondNe;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002674 break;
2675 case OP_IF_LT:
Bill Buzbee1465db52009-09-23 17:17:35 -07002676 cond = kArmCondLt;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002677 break;
2678 case OP_IF_GE:
Bill Buzbee1465db52009-09-23 17:17:35 -07002679 cond = kArmCondGe;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002680 break;
2681 case OP_IF_GT:
Bill Buzbee1465db52009-09-23 17:17:35 -07002682 cond = kArmCondGt;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002683 break;
2684 case OP_IF_LE:
Bill Buzbee1465db52009-09-23 17:17:35 -07002685 cond = kArmCondLe;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002686 break;
2687 default:
2688 cond = 0;
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002689 LOGE("Unexpected opcode (%d) for Fmt22t\n", dalvikOpcode);
Bill Buzbeefc519dc2010-03-06 23:30:57 -08002690 dvmCompilerAbort(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002691 }
2692 genConditionalBranch(cUnit, cond, &labelList[bb->taken->id]);
2693 /* This mostly likely will be optimized away in a later phase */
2694 genUnconditionalBranch(cUnit, &labelList[bb->fallThrough->id]);
2695 return false;
2696}
2697
2698static bool handleFmt22x_Fmt32x(CompilationUnit *cUnit, MIR *mir)
2699{
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002700 Opcode opcode = mir->dalvikInsn.opcode;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002701
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002702 switch (opcode) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07002703 case OP_MOVE_16:
2704 case OP_MOVE_OBJECT_16:
2705 case OP_MOVE_FROM16:
Ben Chenge9695e52009-06-16 16:11:47 -07002706 case OP_MOVE_OBJECT_FROM16: {
Bill Buzbeec6f10662010-02-09 11:16:15 -08002707 storeValue(cUnit, dvmCompilerGetDest(cUnit, mir, 0),
2708 dvmCompilerGetSrc(cUnit, mir, 0));
Ben Chengba4fc8b2009-06-01 13:00:29 -07002709 break;
Ben Chenge9695e52009-06-16 16:11:47 -07002710 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07002711 case OP_MOVE_WIDE_16:
Ben Chenge9695e52009-06-16 16:11:47 -07002712 case OP_MOVE_WIDE_FROM16: {
Bill Buzbeec6f10662010-02-09 11:16:15 -08002713 storeValueWide(cUnit, dvmCompilerGetDestWide(cUnit, mir, 0, 1),
2714 dvmCompilerGetSrcWide(cUnit, mir, 0, 1));
Ben Chengba4fc8b2009-06-01 13:00:29 -07002715 break;
Ben Chenge9695e52009-06-16 16:11:47 -07002716 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07002717 default:
2718 return true;
2719 }
2720 return false;
2721}
2722
2723static bool handleFmt23x(CompilationUnit *cUnit, MIR *mir)
2724{
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002725 Opcode opcode = mir->dalvikInsn.opcode;
Bill Buzbee1465db52009-09-23 17:17:35 -07002726 RegLocation rlSrc1;
2727 RegLocation rlSrc2;
2728 RegLocation rlDest;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002729
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002730 if ( (opcode >= OP_ADD_INT) && (opcode <= OP_REM_DOUBLE)) {
Ben Cheng5d90c202009-11-22 23:31:11 -08002731 return genArithOp( cUnit, mir );
Ben Chengba4fc8b2009-06-01 13:00:29 -07002732 }
2733
Bill Buzbee1465db52009-09-23 17:17:35 -07002734 /* APUTs have 3 sources and no targets */
2735 if (mir->ssaRep->numDefs == 0) {
2736 if (mir->ssaRep->numUses == 3) {
Bill Buzbeec6f10662010-02-09 11:16:15 -08002737 rlDest = dvmCompilerGetSrc(cUnit, mir, 0);
2738 rlSrc1 = dvmCompilerGetSrc(cUnit, mir, 1);
2739 rlSrc2 = dvmCompilerGetSrc(cUnit, mir, 2);
Bill Buzbee1465db52009-09-23 17:17:35 -07002740 } else {
2741 assert(mir->ssaRep->numUses == 4);
Bill Buzbeec6f10662010-02-09 11:16:15 -08002742 rlDest = dvmCompilerGetSrcWide(cUnit, mir, 0, 1);
2743 rlSrc1 = dvmCompilerGetSrc(cUnit, mir, 2);
2744 rlSrc2 = dvmCompilerGetSrc(cUnit, mir, 3);
Bill Buzbee1465db52009-09-23 17:17:35 -07002745 }
2746 } else {
2747 /* Two sources and 1 dest. Deduce the operand sizes */
2748 if (mir->ssaRep->numUses == 4) {
Bill Buzbeec6f10662010-02-09 11:16:15 -08002749 rlSrc1 = dvmCompilerGetSrcWide(cUnit, mir, 0, 1);
2750 rlSrc2 = dvmCompilerGetSrcWide(cUnit, mir, 2, 3);
Bill Buzbee1465db52009-09-23 17:17:35 -07002751 } else {
2752 assert(mir->ssaRep->numUses == 2);
Bill Buzbeec6f10662010-02-09 11:16:15 -08002753 rlSrc1 = dvmCompilerGetSrc(cUnit, mir, 0);
2754 rlSrc2 = dvmCompilerGetSrc(cUnit, mir, 1);
Bill Buzbee1465db52009-09-23 17:17:35 -07002755 }
2756 if (mir->ssaRep->numDefs == 2) {
Bill Buzbeec6f10662010-02-09 11:16:15 -08002757 rlDest = dvmCompilerGetDestWide(cUnit, mir, 0, 1);
Bill Buzbee1465db52009-09-23 17:17:35 -07002758 } else {
2759 assert(mir->ssaRep->numDefs == 1);
Bill Buzbeec6f10662010-02-09 11:16:15 -08002760 rlDest = dvmCompilerGetDest(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07002761 }
2762 }
2763
2764
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002765 switch (opcode) {
Bill Buzbeed45ba372009-06-15 17:00:57 -07002766 case OP_CMPL_FLOAT:
2767 case OP_CMPG_FLOAT:
2768 case OP_CMPL_DOUBLE:
2769 case OP_CMPG_DOUBLE:
Ben Cheng5d90c202009-11-22 23:31:11 -08002770 return genCmpFP(cUnit, mir, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002771 case OP_CMP_LONG:
Bill Buzbee1465db52009-09-23 17:17:35 -07002772 genCmpLong(cUnit, mir, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002773 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002774 case OP_AGET_WIDE:
Bill Buzbee1465db52009-09-23 17:17:35 -07002775 genArrayGet(cUnit, mir, kLong, rlSrc1, rlSrc2, rlDest, 3);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002776 break;
2777 case OP_AGET:
2778 case OP_AGET_OBJECT:
Bill Buzbee1465db52009-09-23 17:17:35 -07002779 genArrayGet(cUnit, mir, kWord, rlSrc1, rlSrc2, rlDest, 2);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002780 break;
2781 case OP_AGET_BOOLEAN:
Bill Buzbee1465db52009-09-23 17:17:35 -07002782 genArrayGet(cUnit, mir, kUnsignedByte, rlSrc1, rlSrc2, rlDest, 0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002783 break;
2784 case OP_AGET_BYTE:
Bill Buzbee1465db52009-09-23 17:17:35 -07002785 genArrayGet(cUnit, mir, kSignedByte, rlSrc1, rlSrc2, rlDest, 0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002786 break;
2787 case OP_AGET_CHAR:
Bill Buzbee1465db52009-09-23 17:17:35 -07002788 genArrayGet(cUnit, mir, kUnsignedHalf, rlSrc1, rlSrc2, rlDest, 1);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002789 break;
2790 case OP_AGET_SHORT:
Bill Buzbee1465db52009-09-23 17:17:35 -07002791 genArrayGet(cUnit, mir, kSignedHalf, rlSrc1, rlSrc2, rlDest, 1);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002792 break;
2793 case OP_APUT_WIDE:
Bill Buzbee1465db52009-09-23 17:17:35 -07002794 genArrayPut(cUnit, mir, kLong, rlSrc1, rlSrc2, rlDest, 3);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002795 break;
2796 case OP_APUT:
Bill Buzbee1465db52009-09-23 17:17:35 -07002797 genArrayPut(cUnit, mir, kWord, rlSrc1, rlSrc2, rlDest, 2);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002798 break;
Bill Buzbeebe6534f2010-03-12 16:01:35 -08002799 case OP_APUT_OBJECT:
2800 genArrayObjectPut(cUnit, mir, rlSrc1, rlSrc2, rlDest, 2);
2801 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002802 case OP_APUT_SHORT:
2803 case OP_APUT_CHAR:
Bill Buzbee1465db52009-09-23 17:17:35 -07002804 genArrayPut(cUnit, mir, kUnsignedHalf, rlSrc1, rlSrc2, rlDest, 1);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002805 break;
2806 case OP_APUT_BYTE:
2807 case OP_APUT_BOOLEAN:
Bill Buzbee1465db52009-09-23 17:17:35 -07002808 genArrayPut(cUnit, mir, kUnsignedByte, rlSrc1, rlSrc2, rlDest, 0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002809 break;
2810 default:
2811 return true;
2812 }
2813 return false;
2814}
2815
Ben Cheng6c10a972009-10-29 14:39:18 -07002816/*
2817 * Find the matching case.
2818 *
2819 * return values:
2820 * r0 (low 32-bit): pc of the chaining cell corresponding to the resolved case,
2821 * including default which is placed at MIN(size, MAX_CHAINED_SWITCH_CASES).
2822 * r1 (high 32-bit): the branch offset of the matching case (only for indexes
2823 * above MAX_CHAINED_SWITCH_CASES).
2824 *
2825 * Instructions around the call are:
2826 *
2827 * mov r2, pc
2828 * blx &findPackedSwitchIndex
2829 * mov pc, r0
2830 * .align4
Bill Buzbeebd047242010-05-13 13:02:53 -07002831 * chaining cell for case 0 [12 bytes]
2832 * chaining cell for case 1 [12 bytes]
Ben Cheng6c10a972009-10-29 14:39:18 -07002833 * :
Bill Buzbeebd047242010-05-13 13:02:53 -07002834 * chaining cell for case MIN(size, MAX_CHAINED_SWITCH_CASES)-1 [12 bytes]
Ben Cheng6c10a972009-10-29 14:39:18 -07002835 * chaining cell for case default [8 bytes]
2836 * noChain exit
2837 */
Ben Chengbd1326d2010-04-02 15:04:53 -07002838static s8 findPackedSwitchIndex(const u2* switchData, int testVal, int pc)
Ben Cheng6c10a972009-10-29 14:39:18 -07002839{
2840 int size;
2841 int firstKey;
2842 const int *entries;
2843 int index;
2844 int jumpIndex;
2845 int caseDPCOffset = 0;
2846 /* In Thumb mode pc is 4 ahead of the "mov r2, pc" instruction */
2847 int chainingPC = (pc + 4) & ~3;
2848
2849 /*
2850 * Packed switch data format:
2851 * ushort ident = 0x0100 magic value
2852 * ushort size number of entries in the table
2853 * int first_key first (and lowest) switch case value
2854 * int targets[size] branch targets, relative to switch opcode
2855 *
2856 * Total size is (4+size*2) 16-bit code units.
2857 */
2858 size = switchData[1];
2859 assert(size > 0);
2860
2861 firstKey = switchData[2];
2862 firstKey |= switchData[3] << 16;
2863
2864
2865 /* The entries are guaranteed to be aligned on a 32-bit boundary;
2866 * we can treat them as a native int array.
2867 */
2868 entries = (const int*) &switchData[4];
2869 assert(((u4)entries & 0x3) == 0);
2870
2871 index = testVal - firstKey;
2872
2873 /* Jump to the default cell */
2874 if (index < 0 || index >= size) {
2875 jumpIndex = MIN(size, MAX_CHAINED_SWITCH_CASES);
2876 /* Jump to the non-chaining exit point */
2877 } else if (index >= MAX_CHAINED_SWITCH_CASES) {
2878 jumpIndex = MAX_CHAINED_SWITCH_CASES + 1;
2879 caseDPCOffset = entries[index];
2880 /* Jump to the inline chaining cell */
2881 } else {
2882 jumpIndex = index;
2883 }
2884
Bill Buzbeebd047242010-05-13 13:02:53 -07002885 chainingPC += jumpIndex * CHAIN_CELL_NORMAL_SIZE;
Ben Cheng6c10a972009-10-29 14:39:18 -07002886 return (((s8) caseDPCOffset) << 32) | (u8) chainingPC;
2887}
2888
2889/* See comments for findPackedSwitchIndex */
Ben Chengbd1326d2010-04-02 15:04:53 -07002890static s8 findSparseSwitchIndex(const u2* switchData, int testVal, int pc)
Ben Cheng6c10a972009-10-29 14:39:18 -07002891{
2892 int size;
2893 const int *keys;
2894 const int *entries;
2895 int chainingPC = (pc + 4) & ~3;
2896 int i;
2897
2898 /*
2899 * Sparse switch data format:
2900 * ushort ident = 0x0200 magic value
2901 * ushort size number of entries in the table; > 0
2902 * int keys[size] keys, sorted low-to-high; 32-bit aligned
2903 * int targets[size] branch targets, relative to switch opcode
2904 *
2905 * Total size is (2+size*4) 16-bit code units.
2906 */
2907
2908 size = switchData[1];
2909 assert(size > 0);
2910
2911 /* The keys are guaranteed to be aligned on a 32-bit boundary;
2912 * we can treat them as a native int array.
2913 */
2914 keys = (const int*) &switchData[2];
2915 assert(((u4)keys & 0x3) == 0);
2916
2917 /* The entries are guaranteed to be aligned on a 32-bit boundary;
2918 * we can treat them as a native int array.
2919 */
2920 entries = keys + size;
2921 assert(((u4)entries & 0x3) == 0);
2922
2923 /*
2924 * Run through the list of keys, which are guaranteed to
2925 * be sorted low-to-high.
2926 *
2927 * Most tables have 3-4 entries. Few have more than 10. A binary
2928 * search here is probably not useful.
2929 */
2930 for (i = 0; i < size; i++) {
2931 int k = keys[i];
2932 if (k == testVal) {
2933 /* MAX_CHAINED_SWITCH_CASES + 1 is the start of the overflow case */
2934 int jumpIndex = (i < MAX_CHAINED_SWITCH_CASES) ?
2935 i : MAX_CHAINED_SWITCH_CASES + 1;
Bill Buzbeebd047242010-05-13 13:02:53 -07002936 chainingPC += jumpIndex * CHAIN_CELL_NORMAL_SIZE;
Ben Cheng6c10a972009-10-29 14:39:18 -07002937 return (((s8) entries[i]) << 32) | (u8) chainingPC;
2938 } else if (k > testVal) {
2939 break;
2940 }
2941 }
Bill Buzbeebd047242010-05-13 13:02:53 -07002942 return chainingPC + MIN(size, MAX_CHAINED_SWITCH_CASES) *
2943 CHAIN_CELL_NORMAL_SIZE;
Ben Cheng6c10a972009-10-29 14:39:18 -07002944}
2945
Ben Chengba4fc8b2009-06-01 13:00:29 -07002946static bool handleFmt31t(CompilationUnit *cUnit, MIR *mir)
2947{
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002948 Opcode dalvikOpcode = mir->dalvikInsn.opcode;
2949 switch (dalvikOpcode) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07002950 case OP_FILL_ARRAY_DATA: {
Bill Buzbeec6f10662010-02-09 11:16:15 -08002951 RegLocation rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07002952 // Making a call - use explicit registers
Bill Buzbeec6f10662010-02-09 11:16:15 -08002953 dvmCompilerFlushAllRegs(cUnit); /* Everything to home location */
Bill Buzbee1465db52009-09-23 17:17:35 -07002954 genExportPC(cUnit, mir);
2955 loadValueDirectFixed(cUnit, rlSrc, r0);
Ben Chengbd1326d2010-04-02 15:04:53 -07002956 LOAD_FUNC_ADDR(cUnit, r2, (int)dvmInterpHandleFillArrayData);
Ben Cheng6c10a972009-10-29 14:39:18 -07002957 loadConstant(cUnit, r1,
2958 (int) (cUnit->method->insns + mir->offset + mir->dalvikInsn.vB));
Bill Buzbee1465db52009-09-23 17:17:35 -07002959 opReg(cUnit, kOpBlx, r2);
Elliott Hughes6a555132010-02-25 15:41:42 -08002960 dvmCompilerClobberCallRegs(cUnit);
Bill Buzbeefd023aa2009-11-02 09:23:49 -08002961 /* generate a branch over if successful */
buzbee8f8109a2010-08-31 10:16:35 -07002962 ArmLIR *branchOver = genCmpImmBranch(cUnit, kArmCondNe, r0, 0);
Bill Buzbeefd023aa2009-11-02 09:23:49 -08002963 loadConstant(cUnit, r0,
2964 (int) (cUnit->method->insns + mir->offset));
2965 genDispatchToHandler(cUnit, TEMPLATE_THROW_EXCEPTION_COMMON);
2966 ArmLIR *target = newLIR0(cUnit, kArmPseudoTargetLabel);
2967 target->defMask = ENCODE_ALL;
2968 branchOver->generic.target = (LIR *) target;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002969 break;
2970 }
2971 /*
Ben Cheng6c10a972009-10-29 14:39:18 -07002972 * Compute the goto target of up to
2973 * MIN(switchSize, MAX_CHAINED_SWITCH_CASES) + 1 chaining cells.
2974 * See the comment before findPackedSwitchIndex for the code layout.
Ben Chengba4fc8b2009-06-01 13:00:29 -07002975 */
2976 case OP_PACKED_SWITCH:
2977 case OP_SPARSE_SWITCH: {
Bill Buzbeec6f10662010-02-09 11:16:15 -08002978 RegLocation rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
2979 dvmCompilerFlushAllRegs(cUnit); /* Everything to home location */
Bill Buzbee1465db52009-09-23 17:17:35 -07002980 loadValueDirectFixed(cUnit, rlSrc, r1);
Bill Buzbeec6f10662010-02-09 11:16:15 -08002981 dvmCompilerLockAllTemps(cUnit);
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002982 if (dalvikOpcode == OP_PACKED_SWITCH) {
Ben Chengbd1326d2010-04-02 15:04:53 -07002983 LOAD_FUNC_ADDR(cUnit, r4PC, (int)findPackedSwitchIndex);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002984 } else {
Ben Chengbd1326d2010-04-02 15:04:53 -07002985 LOAD_FUNC_ADDR(cUnit, r4PC, (int)findSparseSwitchIndex);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002986 }
Ben Cheng6c10a972009-10-29 14:39:18 -07002987 /* r0 <- Addr of the switch data */
2988 loadConstant(cUnit, r0,
2989 (int) (cUnit->method->insns + mir->offset + mir->dalvikInsn.vB));
2990 /* r2 <- pc of the instruction following the blx */
Ben Cheng20d7e6c2011-02-18 17:12:42 -08002991 opRegReg(cUnit, kOpMov, r2, r15pc);
Bill Buzbee1465db52009-09-23 17:17:35 -07002992 opReg(cUnit, kOpBlx, r4PC);
Elliott Hughes6a555132010-02-25 15:41:42 -08002993 dvmCompilerClobberCallRegs(cUnit);
Ben Cheng6c10a972009-10-29 14:39:18 -07002994 /* pc <- computed goto target */
Ben Cheng20d7e6c2011-02-18 17:12:42 -08002995 opRegReg(cUnit, kOpMov, r15pc, r0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002996 break;
2997 }
2998 default:
2999 return true;
3000 }
3001 return false;
3002}
3003
Ben Cheng7a2697d2010-06-07 13:44:23 -07003004/*
3005 * See the example of predicted inlining listed before the
3006 * genValidationForPredictedInline function. The function here takes care the
3007 * branch over at 0x4858de78 and the misprediction target at 0x4858de7a.
3008 */
3009static void genLandingPadForMispredictedCallee(CompilationUnit *cUnit, MIR *mir,
3010 BasicBlock *bb,
3011 ArmLIR *labelList)
3012{
3013 BasicBlock *fallThrough = bb->fallThrough;
3014
3015 /* Bypass the move-result block if there is one */
3016 if (fallThrough->firstMIRInsn) {
3017 assert(fallThrough->firstMIRInsn->OptimizationFlags & MIR_INLINED_PRED);
3018 fallThrough = fallThrough->fallThrough;
3019 }
3020 /* Generate a branch over if the predicted inlining is correct */
3021 genUnconditionalBranch(cUnit, &labelList[fallThrough->id]);
3022
3023 /* Reset the register state */
3024 dvmCompilerResetRegPool(cUnit);
3025 dvmCompilerClobberAllRegs(cUnit);
3026 dvmCompilerResetNullCheck(cUnit);
3027
3028 /* Target for the slow invoke path */
3029 ArmLIR *target = newLIR0(cUnit, kArmPseudoTargetLabel);
3030 target->defMask = ENCODE_ALL;
3031 /* Hook up the target to the verification branch */
3032 mir->meta.callsiteInfo->misPredBranchOver->target = (LIR *) target;
3033}
3034
jeffhao71eee1f2011-01-04 14:18:54 -08003035static bool handleFmt35c_3rc_5rc(CompilationUnit *cUnit, MIR *mir,
3036 BasicBlock *bb, ArmLIR *labelList)
Ben Chengba4fc8b2009-06-01 13:00:29 -07003037{
Bill Buzbee9bc3df32009-07-30 10:52:29 -07003038 ArmLIR *retChainingCell = NULL;
Bill Buzbee89efc3d2009-07-28 11:22:22 -07003039 ArmLIR *pcrLabel = NULL;
Ben Chengba4fc8b2009-06-01 13:00:29 -07003040
Ben Cheng7a2697d2010-06-07 13:44:23 -07003041 /* An invoke with the MIR_INLINED is effectively a no-op */
3042 if (mir->OptimizationFlags & MIR_INLINED)
3043 return false;
3044
Bill Buzbeef4ce16f2009-07-28 13:28:25 -07003045 if (bb->fallThrough != NULL)
3046 retChainingCell = &labelList[bb->fallThrough->id];
3047
Ben Chengba4fc8b2009-06-01 13:00:29 -07003048 DecodedInstruction *dInsn = &mir->dalvikInsn;
Dan Bornstein9a1f8162010-12-01 17:02:26 -08003049 switch (mir->dalvikInsn.opcode) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07003050 /*
3051 * calleeMethod = this->clazz->vtable[
3052 * method->clazz->pDvmDex->pResMethods[BBBB]->methodIndex
3053 * ]
3054 */
3055 case OP_INVOKE_VIRTUAL:
jeffhao71eee1f2011-01-04 14:18:54 -08003056 case OP_INVOKE_VIRTUAL_RANGE:
3057 case OP_INVOKE_VIRTUAL_JUMBO: {
Bill Buzbee89efc3d2009-07-28 11:22:22 -07003058 ArmLIR *predChainingCell = &labelList[bb->taken->id];
Ben Chengba4fc8b2009-06-01 13:00:29 -07003059 int methodIndex =
3060 cUnit->method->clazz->pDvmDex->pResMethods[dInsn->vB]->
3061 methodIndex;
3062
Ben Cheng7a2697d2010-06-07 13:44:23 -07003063 /*
3064 * If the invoke has non-null misPredBranchOver, we need to generate
3065 * the non-inlined version of the invoke here to handle the
3066 * mispredicted case.
3067 */
3068 if (mir->meta.callsiteInfo->misPredBranchOver) {
3069 genLandingPadForMispredictedCallee(cUnit, mir, bb, labelList);
3070 }
3071
Dan Bornstein9a1f8162010-12-01 17:02:26 -08003072 if (mir->dalvikInsn.opcode == OP_INVOKE_VIRTUAL)
Ben Chengba4fc8b2009-06-01 13:00:29 -07003073 genProcessArgsNoRange(cUnit, mir, dInsn, &pcrLabel);
3074 else
3075 genProcessArgsRange(cUnit, mir, dInsn, &pcrLabel);
3076
Ben Cheng38329f52009-07-07 14:19:20 -07003077 genInvokeVirtualCommon(cUnit, mir, methodIndex,
3078 retChainingCell,
3079 predChainingCell,
3080 pcrLabel);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003081 break;
3082 }
3083 /*
3084 * calleeMethod = method->clazz->super->vtable[method->clazz->pDvmDex
3085 * ->pResMethods[BBBB]->methodIndex]
3086 */
Ben Chengba4fc8b2009-06-01 13:00:29 -07003087 case OP_INVOKE_SUPER:
jeffhao71eee1f2011-01-04 14:18:54 -08003088 case OP_INVOKE_SUPER_RANGE:
3089 case OP_INVOKE_SUPER_JUMBO: {
Ben Cheng7a2697d2010-06-07 13:44:23 -07003090 /* Grab the method ptr directly from what the interpreter sees */
3091 const Method *calleeMethod = mir->meta.callsiteInfo->method;
3092 assert(calleeMethod == cUnit->method->clazz->super->vtable[
3093 cUnit->method->clazz->pDvmDex->
3094 pResMethods[dInsn->vB]->methodIndex]);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003095
Dan Bornstein9a1f8162010-12-01 17:02:26 -08003096 if (mir->dalvikInsn.opcode == OP_INVOKE_SUPER)
Ben Chengba4fc8b2009-06-01 13:00:29 -07003097 genProcessArgsNoRange(cUnit, mir, dInsn, &pcrLabel);
3098 else
3099 genProcessArgsRange(cUnit, mir, dInsn, &pcrLabel);
3100
Ben Cheng20d7e6c2011-02-18 17:12:42 -08003101 if (mir->OptimizationFlags & MIR_INVOKE_METHOD_JIT) {
3102 const Method *calleeMethod = mir->meta.callsiteInfo->method;
3103 void *calleeAddr = dvmJitGetMethodAddr(calleeMethod->insns);
3104 assert(calleeAddr);
3105 genInvokeSingletonWholeMethod(cUnit, mir, calleeAddr,
3106 retChainingCell);
3107 } else {
3108 /* r0 = calleeMethod */
3109 loadConstant(cUnit, r0, (int) calleeMethod);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003110
Ben Cheng20d7e6c2011-02-18 17:12:42 -08003111 genInvokeSingletonCommon(cUnit, mir, bb, labelList, pcrLabel,
3112 calleeMethod);
3113 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07003114 break;
3115 }
3116 /* calleeMethod = method->clazz->pDvmDex->pResMethods[BBBB] */
3117 case OP_INVOKE_DIRECT:
jeffhao71eee1f2011-01-04 14:18:54 -08003118 case OP_INVOKE_DIRECT_RANGE:
3119 case OP_INVOKE_DIRECT_JUMBO: {
Ben Cheng7a2697d2010-06-07 13:44:23 -07003120 /* Grab the method ptr directly from what the interpreter sees */
3121 const Method *calleeMethod = mir->meta.callsiteInfo->method;
3122 assert(calleeMethod ==
3123 cUnit->method->clazz->pDvmDex->pResMethods[dInsn->vB]);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003124
Dan Bornstein9a1f8162010-12-01 17:02:26 -08003125 if (mir->dalvikInsn.opcode == OP_INVOKE_DIRECT)
Ben Chengba4fc8b2009-06-01 13:00:29 -07003126 genProcessArgsNoRange(cUnit, mir, dInsn, &pcrLabel);
3127 else
3128 genProcessArgsRange(cUnit, mir, dInsn, &pcrLabel);
3129
3130 /* r0 = calleeMethod */
3131 loadConstant(cUnit, r0, (int) calleeMethod);
3132
Ben Cheng38329f52009-07-07 14:19:20 -07003133 genInvokeSingletonCommon(cUnit, mir, bb, labelList, pcrLabel,
3134 calleeMethod);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003135 break;
3136 }
3137 /* calleeMethod = method->clazz->pDvmDex->pResMethods[BBBB] */
3138 case OP_INVOKE_STATIC:
jeffhao71eee1f2011-01-04 14:18:54 -08003139 case OP_INVOKE_STATIC_RANGE:
3140 case OP_INVOKE_STATIC_JUMBO: {
Ben Cheng7a2697d2010-06-07 13:44:23 -07003141 /* Grab the method ptr directly from what the interpreter sees */
3142 const Method *calleeMethod = mir->meta.callsiteInfo->method;
3143 assert(calleeMethod ==
3144 cUnit->method->clazz->pDvmDex->pResMethods[dInsn->vB]);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003145
Dan Bornstein9a1f8162010-12-01 17:02:26 -08003146 if (mir->dalvikInsn.opcode == OP_INVOKE_STATIC)
Ben Chengba4fc8b2009-06-01 13:00:29 -07003147 genProcessArgsNoRange(cUnit, mir, dInsn,
3148 NULL /* no null check */);
3149 else
3150 genProcessArgsRange(cUnit, mir, dInsn,
3151 NULL /* no null check */);
3152
Ben Cheng20d7e6c2011-02-18 17:12:42 -08003153 if (mir->OptimizationFlags & MIR_INVOKE_METHOD_JIT) {
3154 const Method *calleeMethod = mir->meta.callsiteInfo->method;
3155 void *calleeAddr = dvmJitGetMethodAddr(calleeMethod->insns);
3156 assert(calleeAddr);
3157 genInvokeSingletonWholeMethod(cUnit, mir, calleeAddr,
3158 retChainingCell);
3159 } else {
3160 /* r0 = calleeMethod */
3161 loadConstant(cUnit, r0, (int) calleeMethod);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003162
Ben Cheng20d7e6c2011-02-18 17:12:42 -08003163 genInvokeSingletonCommon(cUnit, mir, bb, labelList, pcrLabel,
3164 calleeMethod);
3165 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07003166 break;
3167 }
Ben Cheng09e50c92010-05-02 10:45:32 -07003168 /*
Ben Chengba4fc8b2009-06-01 13:00:29 -07003169 * calleeMethod = dvmFindInterfaceMethodInCache(this->clazz,
3170 * BBBB, method, method->clazz->pDvmDex)
Ben Cheng38329f52009-07-07 14:19:20 -07003171 *
Ben Cheng09e50c92010-05-02 10:45:32 -07003172 * The following is an example of generated code for
3173 * "invoke-interface v0"
Ben Cheng38329f52009-07-07 14:19:20 -07003174 *
Ben Cheng09e50c92010-05-02 10:45:32 -07003175 * -------- dalvik offset: 0x0008 @ invoke-interface v0
3176 * 0x47357e36 : ldr r0, [r5, #0] --+
3177 * 0x47357e38 : sub r7,r5,#24 |
3178 * 0x47357e3c : cmp r0, #0 | genProcessArgsNoRange
3179 * 0x47357e3e : beq 0x47357e82 |
3180 * 0x47357e40 : stmia r7, <r0> --+
3181 * 0x47357e42 : ldr r4, [pc, #120] --> r4 <- dalvikPC of this invoke
3182 * 0x47357e44 : add r1, pc, #64 --> r1 <- &retChainingCell
3183 * 0x47357e46 : add r2, pc, #72 --> r2 <- &predictedChainingCell
3184 * 0x47357e48 : blx_1 0x47348190 --+ TEMPLATE_INVOKE_METHOD_
3185 * 0x47357e4a : blx_2 see above --+ PREDICTED_CHAIN
3186 * 0x47357e4c : b 0x47357e90 --> off to the predicted chain
3187 * 0x47357e4e : b 0x47357e82 --> punt to the interpreter
3188 * 0x47357e50 : mov r8, r1 --+
3189 * 0x47357e52 : mov r9, r2 |
3190 * 0x47357e54 : ldr r2, [pc, #96] |
3191 * 0x47357e56 : mov r10, r3 |
3192 * 0x47357e58 : movs r0, r3 | dvmFindInterfaceMethodInCache
3193 * 0x47357e5a : ldr r3, [pc, #88] |
3194 * 0x47357e5c : ldr r7, [pc, #80] |
3195 * 0x47357e5e : mov r1, #1452 |
3196 * 0x47357e62 : blx r7 --+
3197 * 0x47357e64 : cmp r0, #0 --> calleeMethod == NULL?
3198 * 0x47357e66 : bne 0x47357e6e --> branch over the throw if !r0
3199 * 0x47357e68 : ldr r0, [pc, #80] --> load Dalvik PC of the invoke
3200 * 0x47357e6a : blx_1 0x47348494 --+ TEMPLATE_THROW_EXCEPTION_
3201 * 0x47357e6c : blx_2 see above --+ COMMON
3202 * 0x47357e6e : mov r1, r8 --> r1 <- &retChainingCell
3203 * 0x47357e70 : cmp r1, #0 --> compare against 0
3204 * 0x47357e72 : bgt 0x47357e7c --> >=0? don't rechain
Ben Chengaf5aa1f2011-01-04 15:37:04 -08003205 * 0x47357e74 : ldr r7, [pc, #off] --+
Ben Cheng09e50c92010-05-02 10:45:32 -07003206 * 0x47357e76 : mov r2, r9 | dvmJitToPatchPredictedChain
3207 * 0x47357e78 : mov r3, r10 |
3208 * 0x47357e7a : blx r7 --+
3209 * 0x47357e7c : add r1, pc, #8 --> r1 <- &retChainingCell
3210 * 0x47357e7e : blx_1 0x4734809c --+ TEMPLATE_INVOKE_METHOD_NO_OPT
3211 * 0x47357e80 : blx_2 see above --+
3212 * -------- reconstruct dalvik PC : 0x425719dc @ +0x0008
3213 * 0x47357e82 : ldr r0, [pc, #56]
Ben Cheng38329f52009-07-07 14:19:20 -07003214 * Exception_Handling:
Ben Cheng09e50c92010-05-02 10:45:32 -07003215 * 0x47357e84 : ldr r1, [r6, #92]
3216 * 0x47357e86 : blx r1
3217 * 0x47357e88 : .align4
3218 * -------- chaining cell (hot): 0x000b
3219 * 0x47357e88 : ldr r0, [r6, #104]
3220 * 0x47357e8a : blx r0
3221 * 0x47357e8c : data 0x19e2(6626)
3222 * 0x47357e8e : data 0x4257(16983)
3223 * 0x47357e90 : .align4
Ben Cheng38329f52009-07-07 14:19:20 -07003224 * -------- chaining cell (predicted)
Ben Cheng09e50c92010-05-02 10:45:32 -07003225 * 0x47357e90 : data 0xe7fe(59390) --> will be patched into bx
3226 * 0x47357e92 : data 0x0000(0)
3227 * 0x47357e94 : data 0x0000(0) --> class
3228 * 0x47357e96 : data 0x0000(0)
3229 * 0x47357e98 : data 0x0000(0) --> method
3230 * 0x47357e9a : data 0x0000(0)
3231 * 0x47357e9c : data 0x0000(0) --> rechain count
3232 * 0x47357e9e : data 0x0000(0)
3233 * -------- end of chaining cells (0x006c)
3234 * 0x47357eb0 : .word (0xad03e369)
3235 * 0x47357eb4 : .word (0x28a90)
3236 * 0x47357eb8 : .word (0x41a63394)
3237 * 0x47357ebc : .word (0x425719dc)
Ben Chengba4fc8b2009-06-01 13:00:29 -07003238 */
3239 case OP_INVOKE_INTERFACE:
jeffhao71eee1f2011-01-04 14:18:54 -08003240 case OP_INVOKE_INTERFACE_RANGE:
3241 case OP_INVOKE_INTERFACE_JUMBO: {
Bill Buzbee89efc3d2009-07-28 11:22:22 -07003242 ArmLIR *predChainingCell = &labelList[bb->taken->id];
Ben Chengba4fc8b2009-06-01 13:00:29 -07003243
Ben Cheng7a2697d2010-06-07 13:44:23 -07003244 /*
3245 * If the invoke has non-null misPredBranchOver, we need to generate
3246 * the non-inlined version of the invoke here to handle the
3247 * mispredicted case.
3248 */
3249 if (mir->meta.callsiteInfo->misPredBranchOver) {
3250 genLandingPadForMispredictedCallee(cUnit, mir, bb, labelList);
3251 }
Bill Buzbee1465db52009-09-23 17:17:35 -07003252
Dan Bornstein9a1f8162010-12-01 17:02:26 -08003253 if (mir->dalvikInsn.opcode == OP_INVOKE_INTERFACE)
Ben Chengba4fc8b2009-06-01 13:00:29 -07003254 genProcessArgsNoRange(cUnit, mir, dInsn, &pcrLabel);
3255 else
3256 genProcessArgsRange(cUnit, mir, dInsn, &pcrLabel);
3257
Ben Cheng38329f52009-07-07 14:19:20 -07003258 /* "this" is already left in r0 by genProcessArgs* */
3259
3260 /* r4PC = dalvikCallsite */
3261 loadConstant(cUnit, r4PC,
3262 (int) (cUnit->method->insns + mir->offset));
3263
3264 /* r1 = &retChainingCell */
Bill Buzbee270c1d62009-08-13 16:58:07 -07003265 ArmLIR *addrRetChain =
Ben Cheng20d7e6c2011-02-18 17:12:42 -08003266 opRegRegImm(cUnit, kOpAdd, r1, r15pc, 0);
Ben Cheng38329f52009-07-07 14:19:20 -07003267 addrRetChain->generic.target = (LIR *) retChainingCell;
3268
3269 /* r2 = &predictedChainingCell */
Bill Buzbee89efc3d2009-07-28 11:22:22 -07003270 ArmLIR *predictedChainingCell =
Ben Cheng20d7e6c2011-02-18 17:12:42 -08003271 opRegRegImm(cUnit, kOpAdd, r2, r15pc, 0);
Ben Cheng38329f52009-07-07 14:19:20 -07003272 predictedChainingCell->generic.target = (LIR *) predChainingCell;
3273
buzbee18fba342011-01-19 15:31:15 -08003274 genDispatchToHandler(cUnit, gDvmJit.methodTraceSupport ?
3275 TEMPLATE_INVOKE_METHOD_PREDICTED_CHAIN_PROF :
3276 TEMPLATE_INVOKE_METHOD_PREDICTED_CHAIN);
Ben Cheng38329f52009-07-07 14:19:20 -07003277
3278 /* return through lr - jump to the chaining cell */
3279 genUnconditionalBranch(cUnit, predChainingCell);
3280
3281 /*
3282 * null-check on "this" may have been eliminated, but we still need
3283 * a PC-reconstruction label for stack overflow bailout.
3284 */
3285 if (pcrLabel == NULL) {
3286 int dPC = (int) (cUnit->method->insns + mir->offset);
Carl Shapirofc75f3e2010-12-07 11:43:38 -08003287 pcrLabel = (ArmLIR *) dvmCompilerNew(sizeof(ArmLIR), true);
Dan Bornstein9a1f8162010-12-01 17:02:26 -08003288 pcrLabel->opcode = kArmPseudoPCReconstructionCell;
Ben Cheng38329f52009-07-07 14:19:20 -07003289 pcrLabel->operands[0] = dPC;
3290 pcrLabel->operands[1] = mir->offset;
3291 /* Insert the place holder to the growable list */
Ben Cheng00603072010-10-28 11:13:58 -07003292 dvmInsertGrowableList(&cUnit->pcReconstructionList,
3293 (intptr_t) pcrLabel);
Ben Cheng38329f52009-07-07 14:19:20 -07003294 }
3295
3296 /* return through lr+2 - punt to the interpreter */
3297 genUnconditionalBranch(cUnit, pcrLabel);
3298
3299 /*
3300 * return through lr+4 - fully resolve the callee method.
3301 * r1 <- count
3302 * r2 <- &predictedChainCell
3303 * r3 <- this->class
3304 * r4 <- dPC
3305 * r7 <- this->class->vtable
3306 */
3307
3308 /* Save count, &predictedChainCell, and class to high regs first */
Bill Buzbee1465db52009-09-23 17:17:35 -07003309 genRegCopy(cUnit, r8, r1);
3310 genRegCopy(cUnit, r9, r2);
3311 genRegCopy(cUnit, r10, r3);
Ben Cheng38329f52009-07-07 14:19:20 -07003312
Ben Chengba4fc8b2009-06-01 13:00:29 -07003313 /* r0 now contains this->clazz */
Bill Buzbee1465db52009-09-23 17:17:35 -07003314 genRegCopy(cUnit, r0, r3);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003315
3316 /* r1 = BBBB */
3317 loadConstant(cUnit, r1, dInsn->vB);
3318
3319 /* r2 = method (caller) */
3320 loadConstant(cUnit, r2, (int) cUnit->method);
3321
3322 /* r3 = pDvmDex */
3323 loadConstant(cUnit, r3, (int) cUnit->method->clazz->pDvmDex);
3324
Ben Chengbd1326d2010-04-02 15:04:53 -07003325 LOAD_FUNC_ADDR(cUnit, r7,
3326 (intptr_t) dvmFindInterfaceMethodInCache);
Bill Buzbee1465db52009-09-23 17:17:35 -07003327 opReg(cUnit, kOpBlx, r7);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003328 /* r0 = calleeMethod (returned from dvmFindInterfaceMethodInCache */
3329
Ben Cheng09e50c92010-05-02 10:45:32 -07003330 dvmCompilerClobberCallRegs(cUnit);
3331 /* generate a branch over if the interface method is resolved */
buzbee8f8109a2010-08-31 10:16:35 -07003332 ArmLIR *branchOver = genCmpImmBranch(cUnit, kArmCondNe, r0, 0);
Ben Cheng09e50c92010-05-02 10:45:32 -07003333 /*
3334 * calleeMethod == NULL -> throw
3335 */
3336 loadConstant(cUnit, r0,
3337 (int) (cUnit->method->insns + mir->offset));
3338 genDispatchToHandler(cUnit, TEMPLATE_THROW_EXCEPTION_COMMON);
3339 /* noreturn */
3340
3341 ArmLIR *target = newLIR0(cUnit, kArmPseudoTargetLabel);
3342 target->defMask = ENCODE_ALL;
3343 branchOver->generic.target = (LIR *) target;
3344
Bill Buzbee1465db52009-09-23 17:17:35 -07003345 genRegCopy(cUnit, r1, r8);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003346
Ben Cheng38329f52009-07-07 14:19:20 -07003347 /* Check if rechain limit is reached */
buzbee8f8109a2010-08-31 10:16:35 -07003348 ArmLIR *bypassRechaining = genCmpImmBranch(cUnit, kArmCondGt,
3349 r1, 0);
Ben Cheng38329f52009-07-07 14:19:20 -07003350
Ben Chengaf5aa1f2011-01-04 15:37:04 -08003351 LOAD_FUNC_ADDR(cUnit, r7, (int) dvmJitToPatchPredictedChain);
Ben Cheng38329f52009-07-07 14:19:20 -07003352
Ben Cheng20d7e6c2011-02-18 17:12:42 -08003353 genRegCopy(cUnit, r1, r6SELF);
Bill Buzbee1465db52009-09-23 17:17:35 -07003354 genRegCopy(cUnit, r2, r9);
3355 genRegCopy(cUnit, r3, r10);
Ben Cheng38329f52009-07-07 14:19:20 -07003356
3357 /*
3358 * r0 = calleeMethod
3359 * r2 = &predictedChainingCell
3360 * r3 = class
3361 *
3362 * &returnChainingCell has been loaded into r1 but is not needed
3363 * when patching the chaining cell and will be clobbered upon
3364 * returning so it will be reconstructed again.
3365 */
Bill Buzbee1465db52009-09-23 17:17:35 -07003366 opReg(cUnit, kOpBlx, r7);
Ben Cheng38329f52009-07-07 14:19:20 -07003367
3368 /* r1 = &retChainingCell */
Ben Cheng20d7e6c2011-02-18 17:12:42 -08003369 addrRetChain = opRegRegImm(cUnit, kOpAdd, r1, r15pc, 0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003370 addrRetChain->generic.target = (LIR *) retChainingCell;
Ben Cheng38329f52009-07-07 14:19:20 -07003371
3372 bypassRechaining->generic.target = (LIR *) addrRetChain;
3373
Ben Chengba4fc8b2009-06-01 13:00:29 -07003374 /*
3375 * r0 = this, r1 = calleeMethod,
3376 * r1 = &ChainingCell,
3377 * r4PC = callsiteDPC,
3378 */
buzbee18fba342011-01-19 15:31:15 -08003379 genDispatchToHandler(cUnit, gDvmJit.methodTraceSupport ?
3380 TEMPLATE_INVOKE_METHOD_NO_OPT_PROF :
3381 TEMPLATE_INVOKE_METHOD_NO_OPT);
Ben Cheng978738d2010-05-13 13:45:57 -07003382#if defined(WITH_JIT_TUNING)
Ben Cheng86717f72010-03-05 15:27:21 -08003383 gDvmJit.invokePolymorphic++;
Ben Chengba4fc8b2009-06-01 13:00:29 -07003384#endif
3385 /* Handle exceptions using the interpreter */
3386 genTrap(cUnit, mir->offset, pcrLabel);
3387 break;
3388 }
buzbeebd7865b2011-03-31 10:55:04 -07003389 case OP_INVOKE_OBJECT_INIT_JUMBO:
3390 case OP_INVOKE_OBJECT_INIT_RANGE:
Ben Chengba4fc8b2009-06-01 13:00:29 -07003391 case OP_FILLED_NEW_ARRAY:
jeffhao71eee1f2011-01-04 14:18:54 -08003392 case OP_FILLED_NEW_ARRAY_RANGE:
3393 case OP_FILLED_NEW_ARRAY_JUMBO: {
Ben Chengba4fc8b2009-06-01 13:00:29 -07003394 /* Just let the interpreter deal with these */
3395 genInterpSingleStep(cUnit, mir);
3396 break;
3397 }
3398 default:
3399 return true;
3400 }
3401 return false;
3402}
3403
3404static bool handleFmt35ms_3rms(CompilationUnit *cUnit, MIR *mir,
Bill Buzbee89efc3d2009-07-28 11:22:22 -07003405 BasicBlock *bb, ArmLIR *labelList)
Ben Chengba4fc8b2009-06-01 13:00:29 -07003406{
Bill Buzbee89efc3d2009-07-28 11:22:22 -07003407 ArmLIR *pcrLabel = NULL;
Ben Chengba4fc8b2009-06-01 13:00:29 -07003408
Ben Cheng7a2697d2010-06-07 13:44:23 -07003409 /* An invoke with the MIR_INLINED is effectively a no-op */
3410 if (mir->OptimizationFlags & MIR_INLINED)
3411 return false;
3412
Ben Chengba4fc8b2009-06-01 13:00:29 -07003413 DecodedInstruction *dInsn = &mir->dalvikInsn;
Dan Bornstein9a1f8162010-12-01 17:02:26 -08003414 switch (mir->dalvikInsn.opcode) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07003415 /* calleeMethod = this->clazz->vtable[BBBB] */
3416 case OP_INVOKE_VIRTUAL_QUICK_RANGE:
3417 case OP_INVOKE_VIRTUAL_QUICK: {
3418 int methodIndex = dInsn->vB;
Bill Buzbeea8589332010-12-27 09:31:21 -08003419 ArmLIR *retChainingCell = &labelList[bb->fallThrough->id];
3420 ArmLIR *predChainingCell = &labelList[bb->taken->id];
Ben Cheng7a2697d2010-06-07 13:44:23 -07003421
3422 /*
3423 * If the invoke has non-null misPredBranchOver, we need to generate
3424 * the non-inlined version of the invoke here to handle the
3425 * mispredicted case.
3426 */
3427 if (mir->meta.callsiteInfo->misPredBranchOver) {
3428 genLandingPadForMispredictedCallee(cUnit, mir, bb, labelList);
3429 }
3430
Dan Bornstein9a1f8162010-12-01 17:02:26 -08003431 if (mir->dalvikInsn.opcode == OP_INVOKE_VIRTUAL_QUICK)
Ben Chengba4fc8b2009-06-01 13:00:29 -07003432 genProcessArgsNoRange(cUnit, mir, dInsn, &pcrLabel);
3433 else
3434 genProcessArgsRange(cUnit, mir, dInsn, &pcrLabel);
3435
Ben Chengcfdeca32011-01-14 11:36:46 -08003436
3437 if (mir->OptimizationFlags & MIR_INVOKE_METHOD_JIT) {
3438 const Method *calleeMethod = mir->meta.callsiteInfo->method;
3439 void *calleeAddr = dvmJitGetMethodAddr(calleeMethod->insns);
Ben Cheng20d7e6c2011-02-18 17:12:42 -08003440 assert(calleeAddr);
3441 genInvokeVirtualWholeMethod(cUnit, mir, calleeAddr,
3442 retChainingCell);
Ben Chengcfdeca32011-01-14 11:36:46 -08003443 }
3444
Ben Cheng38329f52009-07-07 14:19:20 -07003445 genInvokeVirtualCommon(cUnit, mir, methodIndex,
3446 retChainingCell,
3447 predChainingCell,
3448 pcrLabel);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003449 break;
3450 }
3451 /* calleeMethod = method->clazz->super->vtable[BBBB] */
3452 case OP_INVOKE_SUPER_QUICK:
3453 case OP_INVOKE_SUPER_QUICK_RANGE: {
Ben Cheng7a2697d2010-06-07 13:44:23 -07003454 /* Grab the method ptr directly from what the interpreter sees */
3455 const Method *calleeMethod = mir->meta.callsiteInfo->method;
3456 assert(calleeMethod ==
3457 cUnit->method->clazz->super->vtable[dInsn->vB]);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003458
Dan Bornstein9a1f8162010-12-01 17:02:26 -08003459 if (mir->dalvikInsn.opcode == OP_INVOKE_SUPER_QUICK)
Ben Chengba4fc8b2009-06-01 13:00:29 -07003460 genProcessArgsNoRange(cUnit, mir, dInsn, &pcrLabel);
3461 else
3462 genProcessArgsRange(cUnit, mir, dInsn, &pcrLabel);
3463
3464 /* r0 = calleeMethod */
3465 loadConstant(cUnit, r0, (int) calleeMethod);
3466
Ben Cheng38329f52009-07-07 14:19:20 -07003467 genInvokeSingletonCommon(cUnit, mir, bb, labelList, pcrLabel,
3468 calleeMethod);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003469 break;
3470 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07003471 default:
3472 return true;
3473 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07003474 return false;
3475}
3476
3477/*
Bill Buzbeefd023aa2009-11-02 09:23:49 -08003478 * This operation is complex enough that we'll do it partly inline
3479 * and partly with a handler. NOTE: the handler uses hardcoded
3480 * values for string object offsets and must be revisitied if the
3481 * layout changes.
3482 */
3483static bool genInlinedCompareTo(CompilationUnit *cUnit, MIR *mir)
3484{
3485#if defined(USE_GLOBAL_STRING_DEFS)
Elliott Hughes7e914f12011-01-19 18:18:42 -08003486 return handleExecuteInlineC(cUnit, mir);
Bill Buzbeefd023aa2009-11-02 09:23:49 -08003487#else
3488 ArmLIR *rollback;
Bill Buzbeec6f10662010-02-09 11:16:15 -08003489 RegLocation rlThis = dvmCompilerGetSrc(cUnit, mir, 0);
3490 RegLocation rlComp = dvmCompilerGetSrc(cUnit, mir, 1);
Bill Buzbeefd023aa2009-11-02 09:23:49 -08003491
3492 loadValueDirectFixed(cUnit, rlThis, r0);
3493 loadValueDirectFixed(cUnit, rlComp, r1);
3494 /* Test objects for NULL */
3495 rollback = genNullCheck(cUnit, rlThis.sRegLow, r0, mir->offset, NULL);
3496 genNullCheck(cUnit, rlComp.sRegLow, r1, mir->offset, rollback);
3497 /*
3498 * TUNING: we could check for object pointer equality before invoking
3499 * handler. Unclear whether the gain would be worth the added code size
3500 * expansion.
3501 */
3502 genDispatchToHandler(cUnit, TEMPLATE_STRING_COMPARETO);
Bill Buzbeec6f10662010-02-09 11:16:15 -08003503 storeValue(cUnit, inlinedTarget(cUnit, mir, false),
3504 dvmCompilerGetReturn(cUnit));
Elliott Hughes7e914f12011-01-19 18:18:42 -08003505 return false;
Bill Buzbeefd023aa2009-11-02 09:23:49 -08003506#endif
3507}
3508
Elliott Hughes2bdbcb62010-04-12 14:29:37 -07003509static bool genInlinedFastIndexOf(CompilationUnit *cUnit, MIR *mir)
Bill Buzbeefd023aa2009-11-02 09:23:49 -08003510{
3511#if defined(USE_GLOBAL_STRING_DEFS)
Elliott Hughes7e914f12011-01-19 18:18:42 -08003512 return handleExecuteInlineC(cUnit, mir);
Bill Buzbeefd023aa2009-11-02 09:23:49 -08003513#else
Bill Buzbeec6f10662010-02-09 11:16:15 -08003514 RegLocation rlThis = dvmCompilerGetSrc(cUnit, mir, 0);
3515 RegLocation rlChar = dvmCompilerGetSrc(cUnit, mir, 1);
Bill Buzbeefd023aa2009-11-02 09:23:49 -08003516
3517 loadValueDirectFixed(cUnit, rlThis, r0);
3518 loadValueDirectFixed(cUnit, rlChar, r1);
Elliott Hughes2bdbcb62010-04-12 14:29:37 -07003519 RegLocation rlStart = dvmCompilerGetSrc(cUnit, mir, 2);
3520 loadValueDirectFixed(cUnit, rlStart, r2);
Bill Buzbeefd023aa2009-11-02 09:23:49 -08003521 /* Test objects for NULL */
3522 genNullCheck(cUnit, rlThis.sRegLow, r0, mir->offset, NULL);
3523 genDispatchToHandler(cUnit, TEMPLATE_STRING_INDEXOF);
Bill Buzbeec6f10662010-02-09 11:16:15 -08003524 storeValue(cUnit, inlinedTarget(cUnit, mir, false),
3525 dvmCompilerGetReturn(cUnit));
Elliott Hughes7e914f12011-01-19 18:18:42 -08003526 return false;
Bill Buzbeefd023aa2009-11-02 09:23:49 -08003527#endif
3528}
3529
Elliott Hughesee34f592010-04-05 18:13:52 -07003530// Generates an inlined String.isEmpty or String.length.
3531static bool genInlinedStringIsEmptyOrLength(CompilationUnit *cUnit, MIR *mir,
3532 bool isEmpty)
Bill Buzbee1f748632010-03-02 16:14:41 -08003533{
Elliott Hughesee34f592010-04-05 18:13:52 -07003534 // dst = src.length();
Bill Buzbee1f748632010-03-02 16:14:41 -08003535 RegLocation rlObj = dvmCompilerGetSrc(cUnit, mir, 0);
3536 RegLocation rlDest = inlinedTarget(cUnit, mir, false);
3537 rlObj = loadValue(cUnit, rlObj, kCoreReg);
3538 RegLocation rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
3539 genNullCheck(cUnit, rlObj.sRegLow, rlObj.lowReg, mir->offset, NULL);
3540 loadWordDisp(cUnit, rlObj.lowReg, gDvm.offJavaLangString_count,
3541 rlResult.lowReg);
Elliott Hughesee34f592010-04-05 18:13:52 -07003542 if (isEmpty) {
3543 // dst = (dst == 0);
3544 int tReg = dvmCompilerAllocTemp(cUnit);
3545 opRegReg(cUnit, kOpNeg, tReg, rlResult.lowReg);
3546 opRegRegReg(cUnit, kOpAdc, rlResult.lowReg, rlResult.lowReg, tReg);
3547 }
Bill Buzbee1f748632010-03-02 16:14:41 -08003548 storeValue(cUnit, rlDest, rlResult);
3549 return false;
3550}
3551
Elliott Hughesee34f592010-04-05 18:13:52 -07003552static bool genInlinedStringLength(CompilationUnit *cUnit, MIR *mir)
3553{
3554 return genInlinedStringIsEmptyOrLength(cUnit, mir, false);
3555}
3556
3557static bool genInlinedStringIsEmpty(CompilationUnit *cUnit, MIR *mir)
3558{
3559 return genInlinedStringIsEmptyOrLength(cUnit, mir, true);
3560}
3561
Bill Buzbee1f748632010-03-02 16:14:41 -08003562static bool genInlinedStringCharAt(CompilationUnit *cUnit, MIR *mir)
3563{
3564 int contents = offsetof(ArrayObject, contents);
3565 RegLocation rlObj = dvmCompilerGetSrc(cUnit, mir, 0);
3566 RegLocation rlIdx = dvmCompilerGetSrc(cUnit, mir, 1);
3567 RegLocation rlDest = inlinedTarget(cUnit, mir, false);
3568 RegLocation rlResult;
3569 rlObj = loadValue(cUnit, rlObj, kCoreReg);
3570 rlIdx = loadValue(cUnit, rlIdx, kCoreReg);
3571 int regMax = dvmCompilerAllocTemp(cUnit);
3572 int regOff = dvmCompilerAllocTemp(cUnit);
3573 int regPtr = dvmCompilerAllocTemp(cUnit);
3574 ArmLIR *pcrLabel = genNullCheck(cUnit, rlObj.sRegLow, rlObj.lowReg,
3575 mir->offset, NULL);
3576 loadWordDisp(cUnit, rlObj.lowReg, gDvm.offJavaLangString_count, regMax);
3577 loadWordDisp(cUnit, rlObj.lowReg, gDvm.offJavaLangString_offset, regOff);
3578 loadWordDisp(cUnit, rlObj.lowReg, gDvm.offJavaLangString_value, regPtr);
3579 genBoundsCheck(cUnit, rlIdx.lowReg, regMax, mir->offset, pcrLabel);
3580 dvmCompilerFreeTemp(cUnit, regMax);
3581 opRegImm(cUnit, kOpAdd, regPtr, contents);
3582 opRegReg(cUnit, kOpAdd, regOff, rlIdx.lowReg);
3583 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
3584 loadBaseIndexed(cUnit, regPtr, regOff, rlResult.lowReg, 1, kUnsignedHalf);
3585 storeValue(cUnit, rlDest, rlResult);
3586 return false;
3587}
3588
3589static bool genInlinedAbsInt(CompilationUnit *cUnit, MIR *mir)
3590{
3591 RegLocation rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
3592 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
Elliott Hughese22bd842010-08-20 18:47:36 -07003593 RegLocation rlDest = inlinedTarget(cUnit, mir, false);
Bill Buzbee1f748632010-03-02 16:14:41 -08003594 RegLocation rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
3595 int signReg = dvmCompilerAllocTemp(cUnit);
3596 /*
3597 * abs(x) = y<=x>>31, (x+y)^y.
3598 * Thumb2's IT block also yields 3 instructions, but imposes
3599 * scheduling constraints.
3600 */
3601 opRegRegImm(cUnit, kOpAsr, signReg, rlSrc.lowReg, 31);
3602 opRegRegReg(cUnit, kOpAdd, rlResult.lowReg, rlSrc.lowReg, signReg);
3603 opRegReg(cUnit, kOpXor, rlResult.lowReg, signReg);
3604 storeValue(cUnit, rlDest, rlResult);
3605 return false;
3606}
3607
3608static bool genInlinedAbsLong(CompilationUnit *cUnit, MIR *mir)
3609{
3610 RegLocation rlSrc = dvmCompilerGetSrcWide(cUnit, mir, 0, 1);
3611 RegLocation rlDest = inlinedTargetWide(cUnit, mir, false);
3612 rlSrc = loadValueWide(cUnit, rlSrc, kCoreReg);
3613 RegLocation rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
3614 int signReg = dvmCompilerAllocTemp(cUnit);
3615 /*
3616 * abs(x) = y<=x>>31, (x+y)^y.
3617 * Thumb2 IT block allows slightly shorter sequence,
3618 * but introduces a scheduling barrier. Stick with this
3619 * mechanism for now.
3620 */
3621 opRegRegImm(cUnit, kOpAsr, signReg, rlSrc.highReg, 31);
3622 opRegRegReg(cUnit, kOpAdd, rlResult.lowReg, rlSrc.lowReg, signReg);
3623 opRegRegReg(cUnit, kOpAdc, rlResult.highReg, rlSrc.highReg, signReg);
3624 opRegReg(cUnit, kOpXor, rlResult.lowReg, signReg);
3625 opRegReg(cUnit, kOpXor, rlResult.highReg, signReg);
3626 storeValueWide(cUnit, rlDest, rlResult);
3627 return false;
3628}
Bill Buzbeefd023aa2009-11-02 09:23:49 -08003629
Elliott Hughese22bd842010-08-20 18:47:36 -07003630static bool genInlinedIntFloatConversion(CompilationUnit *cUnit, MIR *mir)
3631{
3632 // Just move from source to destination...
3633 RegLocation rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
3634 RegLocation rlDest = inlinedTarget(cUnit, mir, false);
3635 storeValue(cUnit, rlDest, rlSrc);
3636 return false;
3637}
3638
3639static bool genInlinedLongDoubleConversion(CompilationUnit *cUnit, MIR *mir)
3640{
3641 // Just move from source to destination...
3642 RegLocation rlSrc = dvmCompilerGetSrcWide(cUnit, mir, 0, 1);
3643 RegLocation rlDest = inlinedTargetWide(cUnit, mir, false);
3644 storeValueWide(cUnit, rlDest, rlSrc);
3645 return false;
3646}
3647
Bill Buzbeefd023aa2009-11-02 09:23:49 -08003648/*
Elliott Hughes7e914f12011-01-19 18:18:42 -08003649 * JITs a call to a C function.
3650 * TODO: use this for faster native method invocation for simple native
3651 * methods (http://b/3069458).
3652 */
3653static bool handleExecuteInlineC(CompilationUnit *cUnit, MIR *mir)
3654{
3655 DecodedInstruction *dInsn = &mir->dalvikInsn;
3656 int operation = dInsn->vB;
3657 unsigned int i;
3658 const InlineOperation* inLineTable = dvmGetInlineOpsTable();
3659 uintptr_t fn = (int) inLineTable[operation].func;
3660 if (fn == 0) {
3661 dvmCompilerAbort(cUnit);
3662 }
3663 dvmCompilerFlushAllRegs(cUnit); /* Everything to home location */
3664 dvmCompilerClobberCallRegs(cUnit);
3665 dvmCompilerClobber(cUnit, r4PC);
3666 dvmCompilerClobber(cUnit, r7);
buzbee9f601a92011-02-11 17:48:20 -08003667 int offset = offsetof(Thread, retval);
Ben Cheng20d7e6c2011-02-18 17:12:42 -08003668 opRegRegImm(cUnit, kOpAdd, r4PC, r6SELF, offset);
Elliott Hughes7e914f12011-01-19 18:18:42 -08003669 opImm(cUnit, kOpPush, (1<<r4PC) | (1<<r7));
3670 LOAD_FUNC_ADDR(cUnit, r4PC, fn);
3671 genExportPC(cUnit, mir);
3672 for (i=0; i < dInsn->vA; i++) {
3673 loadValueDirect(cUnit, dvmCompilerGetSrc(cUnit, mir, i), i);
3674 }
3675 opReg(cUnit, kOpBlx, r4PC);
Ben Cheng20d7e6c2011-02-18 17:12:42 -08003676 opRegImm(cUnit, kOpAdd, r13sp, 8);
Elliott Hughes7e914f12011-01-19 18:18:42 -08003677 /* NULL? */
3678 ArmLIR *branchOver = genCmpImmBranch(cUnit, kArmCondNe, r0, 0);
3679 loadConstant(cUnit, r0, (int) (cUnit->method->insns + mir->offset));
3680 genDispatchToHandler(cUnit, TEMPLATE_THROW_EXCEPTION_COMMON);
3681 ArmLIR *target = newLIR0(cUnit, kArmPseudoTargetLabel);
3682 target->defMask = ENCODE_ALL;
3683 branchOver->generic.target = (LIR *) target;
3684 return false;
3685}
3686
3687/*
Bill Buzbeece46c942009-11-20 15:41:34 -08003688 * NOTE: Handles both range and non-range versions (arguments
3689 * have already been normalized by this point).
Ben Chengba4fc8b2009-06-01 13:00:29 -07003690 */
Bill Buzbeece46c942009-11-20 15:41:34 -08003691static bool handleExecuteInline(CompilationUnit *cUnit, MIR *mir)
Ben Chengba4fc8b2009-06-01 13:00:29 -07003692{
3693 DecodedInstruction *dInsn = &mir->dalvikInsn;
Elliott Hughes7e914f12011-01-19 18:18:42 -08003694 assert(dInsn->opcode == OP_EXECUTE_INLINE_RANGE ||
3695 dInsn->opcode == OP_EXECUTE_INLINE);
3696 switch (dInsn->vB) {
3697 case INLINE_EMPTYINLINEMETHOD:
3698 return false; /* Nop */
3699
3700 /* These ones we potentially JIT inline. */
3701 case INLINE_STRING_LENGTH:
3702 return genInlinedStringLength(cUnit, mir);
3703 case INLINE_STRING_IS_EMPTY:
3704 return genInlinedStringIsEmpty(cUnit, mir);
3705 case INLINE_MATH_ABS_INT:
3706 return genInlinedAbsInt(cUnit, mir);
3707 case INLINE_MATH_ABS_LONG:
3708 return genInlinedAbsLong(cUnit, mir);
3709 case INLINE_MATH_MIN_INT:
3710 return genInlinedMinMaxInt(cUnit, mir, true);
3711 case INLINE_MATH_MAX_INT:
3712 return genInlinedMinMaxInt(cUnit, mir, false);
3713 case INLINE_STRING_CHARAT:
3714 return genInlinedStringCharAt(cUnit, mir);
3715 case INLINE_MATH_SQRT:
3716 return genInlineSqrt(cUnit, mir);
3717 case INLINE_MATH_ABS_FLOAT:
3718 return genInlinedAbsFloat(cUnit, mir);
3719 case INLINE_MATH_ABS_DOUBLE:
3720 return genInlinedAbsDouble(cUnit, mir);
3721 case INLINE_STRING_COMPARETO:
3722 return genInlinedCompareTo(cUnit, mir);
3723 case INLINE_STRING_FASTINDEXOF_II:
3724 return genInlinedFastIndexOf(cUnit, mir);
3725 case INLINE_FLOAT_TO_RAW_INT_BITS:
3726 case INLINE_INT_BITS_TO_FLOAT:
3727 return genInlinedIntFloatConversion(cUnit, mir);
3728 case INLINE_DOUBLE_TO_RAW_LONG_BITS:
3729 case INLINE_LONG_BITS_TO_DOUBLE:
3730 return genInlinedLongDoubleConversion(cUnit, mir);
3731
3732 /*
3733 * These ones we just JIT a call to a C function for.
3734 * TODO: special-case these in the other "invoke" call paths.
3735 */
3736 case INLINE_STRING_EQUALS:
3737 case INLINE_MATH_COS:
3738 case INLINE_MATH_SIN:
3739 case INLINE_FLOAT_TO_INT_BITS:
3740 case INLINE_DOUBLE_TO_LONG_BITS:
3741 return handleExecuteInlineC(cUnit, mir);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003742 }
Elliott Hughes7e914f12011-01-19 18:18:42 -08003743 dvmCompilerAbort(cUnit);
3744 return false; // Not reachable; keeps compiler happy.
Ben Chengba4fc8b2009-06-01 13:00:29 -07003745}
3746
3747static bool handleFmt51l(CompilationUnit *cUnit, MIR *mir)
3748{
Bill Buzbee1465db52009-09-23 17:17:35 -07003749 //TUNING: We're using core regs here - not optimal when target is a double
Bill Buzbeec6f10662010-02-09 11:16:15 -08003750 RegLocation rlDest = dvmCompilerGetDestWide(cUnit, mir, 0, 1);
3751 RegLocation rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Ben Chengbd1326d2010-04-02 15:04:53 -07003752 loadConstantNoClobber(cUnit, rlResult.lowReg,
3753 mir->dalvikInsn.vB_wide & 0xFFFFFFFFUL);
3754 loadConstantNoClobber(cUnit, rlResult.highReg,
3755 (mir->dalvikInsn.vB_wide>>32) & 0xFFFFFFFFUL);
Bill Buzbee1465db52009-09-23 17:17:35 -07003756 storeValueWide(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003757 return false;
3758}
3759
Ben Chengba4fc8b2009-06-01 13:00:29 -07003760/*
3761 * The following are special processing routines that handle transfer of
3762 * controls between compiled code and the interpreter. Certain VM states like
3763 * Dalvik PC and special-purpose registers are reconstructed here.
3764 */
3765
Bill Buzbeebd047242010-05-13 13:02:53 -07003766/*
3767 * Insert a
3768 * b .+4
3769 * nop
3770 * pair at the beginning of a chaining cell. This serves as the
3771 * switch branch that selects between reverting to the interpreter or
3772 * not. Once the cell is chained to a translation, the cell will
3773 * contain a 32-bit branch. Subsequent chain/unchain operations will
3774 * then only alter that first 16-bits - the "b .+4" for unchaining,
3775 * and the restoration of the first half of the 32-bit branch for
3776 * rechaining.
3777 */
3778static void insertChainingSwitch(CompilationUnit *cUnit)
3779{
3780 ArmLIR *branch = newLIR0(cUnit, kThumbBUncond);
3781 newLIR2(cUnit, kThumbOrr, r0, r0);
3782 ArmLIR *target = newLIR0(cUnit, kArmPseudoTargetLabel);
3783 target->defMask = ENCODE_ALL;
3784 branch->generic.target = (LIR *) target;
3785}
3786
Ben Cheng1efc9c52009-06-08 18:25:27 -07003787/* Chaining cell for code that may need warmup. */
3788static void handleNormalChainingCell(CompilationUnit *cUnit,
3789 unsigned int offset)
Ben Chengba4fc8b2009-06-01 13:00:29 -07003790{
Ben Cheng11d8f142010-03-24 15:24:19 -07003791 /*
3792 * Use raw instruction constructors to guarantee that the generated
3793 * instructions fit the predefined cell size.
3794 */
Bill Buzbeebd047242010-05-13 13:02:53 -07003795 insertChainingSwitch(cUnit);
Ben Cheng20d7e6c2011-02-18 17:12:42 -08003796 newLIR3(cUnit, kThumbLdrRRI5, r0, r6SELF,
buzbee9f601a92011-02-11 17:48:20 -08003797 offsetof(Thread,
Ben Cheng11d8f142010-03-24 15:24:19 -07003798 jitToInterpEntries.dvmJitToInterpNormal) >> 2);
3799 newLIR1(cUnit, kThumbBlxR, r0);
Ben Cheng385828e2011-03-04 16:48:33 -08003800 addWordData(cUnit, NULL, (int) (cUnit->method->insns + offset));
Ben Chengba4fc8b2009-06-01 13:00:29 -07003801}
3802
3803/*
Ben Cheng1efc9c52009-06-08 18:25:27 -07003804 * Chaining cell for instructions that immediately following already translated
3805 * code.
Ben Chengba4fc8b2009-06-01 13:00:29 -07003806 */
Ben Cheng1efc9c52009-06-08 18:25:27 -07003807static void handleHotChainingCell(CompilationUnit *cUnit,
3808 unsigned int offset)
Ben Chengba4fc8b2009-06-01 13:00:29 -07003809{
Ben Cheng11d8f142010-03-24 15:24:19 -07003810 /*
3811 * Use raw instruction constructors to guarantee that the generated
3812 * instructions fit the predefined cell size.
3813 */
Bill Buzbeebd047242010-05-13 13:02:53 -07003814 insertChainingSwitch(cUnit);
Ben Cheng20d7e6c2011-02-18 17:12:42 -08003815 newLIR3(cUnit, kThumbLdrRRI5, r0, r6SELF,
buzbee9f601a92011-02-11 17:48:20 -08003816 offsetof(Thread,
Ben Cheng11d8f142010-03-24 15:24:19 -07003817 jitToInterpEntries.dvmJitToInterpTraceSelect) >> 2);
3818 newLIR1(cUnit, kThumbBlxR, r0);
Ben Cheng385828e2011-03-04 16:48:33 -08003819 addWordData(cUnit, NULL, (int) (cUnit->method->insns + offset));
Ben Chengba4fc8b2009-06-01 13:00:29 -07003820}
3821
Jeff Hao97319a82009-08-12 16:57:15 -07003822/* Chaining cell for branches that branch back into the same basic block */
3823static void handleBackwardBranchChainingCell(CompilationUnit *cUnit,
3824 unsigned int offset)
3825{
Ben Cheng11d8f142010-03-24 15:24:19 -07003826 /*
3827 * Use raw instruction constructors to guarantee that the generated
3828 * instructions fit the predefined cell size.
3829 */
Bill Buzbeebd047242010-05-13 13:02:53 -07003830 insertChainingSwitch(cUnit);
Bill Buzbee9c4b7c82009-09-10 10:10:38 -07003831#if defined(WITH_SELF_VERIFICATION)
Ben Cheng20d7e6c2011-02-18 17:12:42 -08003832 newLIR3(cUnit, kThumbLdrRRI5, r0, r6SELF,
buzbee9f601a92011-02-11 17:48:20 -08003833 offsetof(Thread,
Ben Cheng40094c12010-02-24 20:58:44 -08003834 jitToInterpEntries.dvmJitToInterpBackwardBranch) >> 2);
Bill Buzbee9c4b7c82009-09-10 10:10:38 -07003835#else
Ben Cheng20d7e6c2011-02-18 17:12:42 -08003836 newLIR3(cUnit, kThumbLdrRRI5, r0, r6SELF,
buzbee9f601a92011-02-11 17:48:20 -08003837 offsetof(Thread, jitToInterpEntries.dvmJitToInterpNormal) >> 2);
Bill Buzbee9c4b7c82009-09-10 10:10:38 -07003838#endif
Bill Buzbee1465db52009-09-23 17:17:35 -07003839 newLIR1(cUnit, kThumbBlxR, r0);
Ben Cheng385828e2011-03-04 16:48:33 -08003840 addWordData(cUnit, NULL, (int) (cUnit->method->insns + offset));
Jeff Hao97319a82009-08-12 16:57:15 -07003841}
3842
Ben Chengba4fc8b2009-06-01 13:00:29 -07003843/* Chaining cell for monomorphic method invocations. */
Ben Cheng38329f52009-07-07 14:19:20 -07003844static void handleInvokeSingletonChainingCell(CompilationUnit *cUnit,
3845 const Method *callee)
Ben Chengba4fc8b2009-06-01 13:00:29 -07003846{
Ben Cheng11d8f142010-03-24 15:24:19 -07003847 /*
3848 * Use raw instruction constructors to guarantee that the generated
3849 * instructions fit the predefined cell size.
3850 */
Bill Buzbeebd047242010-05-13 13:02:53 -07003851 insertChainingSwitch(cUnit);
Ben Cheng20d7e6c2011-02-18 17:12:42 -08003852 newLIR3(cUnit, kThumbLdrRRI5, r0, r6SELF,
buzbee9f601a92011-02-11 17:48:20 -08003853 offsetof(Thread,
Ben Cheng11d8f142010-03-24 15:24:19 -07003854 jitToInterpEntries.dvmJitToInterpTraceSelect) >> 2);
3855 newLIR1(cUnit, kThumbBlxR, r0);
Ben Cheng385828e2011-03-04 16:48:33 -08003856 addWordData(cUnit, NULL, (int) (callee->insns));
Ben Chengba4fc8b2009-06-01 13:00:29 -07003857}
3858
Ben Cheng38329f52009-07-07 14:19:20 -07003859/* Chaining cell for monomorphic method invocations. */
3860static void handleInvokePredictedChainingCell(CompilationUnit *cUnit)
3861{
3862
3863 /* Should not be executed in the initial state */
Ben Cheng385828e2011-03-04 16:48:33 -08003864 addWordData(cUnit, NULL, PREDICTED_CHAIN_BX_PAIR_INIT);
Ben Cheng38329f52009-07-07 14:19:20 -07003865 /* To be filled: class */
Ben Cheng385828e2011-03-04 16:48:33 -08003866 addWordData(cUnit, NULL, PREDICTED_CHAIN_CLAZZ_INIT);
Ben Cheng38329f52009-07-07 14:19:20 -07003867 /* To be filled: method */
Ben Cheng385828e2011-03-04 16:48:33 -08003868 addWordData(cUnit, NULL, PREDICTED_CHAIN_METHOD_INIT);
Ben Cheng38329f52009-07-07 14:19:20 -07003869 /*
3870 * Rechain count. The initial value of 0 here will trigger chaining upon
3871 * the first invocation of this callsite.
3872 */
Ben Cheng385828e2011-03-04 16:48:33 -08003873 addWordData(cUnit, NULL, PREDICTED_CHAIN_COUNTER_INIT);
Ben Cheng38329f52009-07-07 14:19:20 -07003874}
3875
Ben Chengba4fc8b2009-06-01 13:00:29 -07003876/* Load the Dalvik PC into r0 and jump to the specified target */
3877static void handlePCReconstruction(CompilationUnit *cUnit,
Bill Buzbee89efc3d2009-07-28 11:22:22 -07003878 ArmLIR *targetLabel)
Ben Chengba4fc8b2009-06-01 13:00:29 -07003879{
Bill Buzbee89efc3d2009-07-28 11:22:22 -07003880 ArmLIR **pcrLabel =
3881 (ArmLIR **) cUnit->pcReconstructionList.elemList;
Ben Chengba4fc8b2009-06-01 13:00:29 -07003882 int numElems = cUnit->pcReconstructionList.numUsed;
3883 int i;
3884 for (i = 0; i < numElems; i++) {
3885 dvmCompilerAppendLIR(cUnit, (LIR *) pcrLabel[i]);
3886 /* r0 = dalvik PC */
3887 loadConstant(cUnit, r0, pcrLabel[i]->operands[0]);
3888 genUnconditionalBranch(cUnit, targetLabel);
3889 }
3890}
3891
Bill Buzbee1465db52009-09-23 17:17:35 -07003892static char *extendedMIROpNames[kMirOpLast - kMirOpFirst] = {
3893 "kMirOpPhi",
3894 "kMirOpNullNRangeUpCheck",
3895 "kMirOpNullNRangeDownCheck",
3896 "kMirOpLowerBound",
3897 "kMirOpPunt",
Ben Cheng7a2697d2010-06-07 13:44:23 -07003898 "kMirOpCheckInlinePrediction",
Ben Cheng4238ec22009-08-24 16:32:22 -07003899};
3900
3901/*
3902 * vA = arrayReg;
3903 * vB = idxReg;
3904 * vC = endConditionReg;
3905 * arg[0] = maxC
3906 * arg[1] = minC
3907 * arg[2] = loopBranchConditionCode
3908 */
3909static void genHoistedChecksForCountUpLoop(CompilationUnit *cUnit, MIR *mir)
3910{
Bill Buzbee1465db52009-09-23 17:17:35 -07003911 /*
3912 * NOTE: these synthesized blocks don't have ssa names assigned
3913 * for Dalvik registers. However, because they dominate the following
3914 * blocks we can simply use the Dalvik name w/ subscript 0 as the
3915 * ssa name.
3916 */
Ben Cheng4238ec22009-08-24 16:32:22 -07003917 DecodedInstruction *dInsn = &mir->dalvikInsn;
3918 const int lenOffset = offsetof(ArrayObject, length);
Ben Cheng4238ec22009-08-24 16:32:22 -07003919 const int maxC = dInsn->arg[0];
Bill Buzbee1465db52009-09-23 17:17:35 -07003920 int regLength;
3921 RegLocation rlArray = cUnit->regLocation[mir->dalvikInsn.vA];
3922 RegLocation rlIdxEnd = cUnit->regLocation[mir->dalvikInsn.vC];
Ben Cheng4238ec22009-08-24 16:32:22 -07003923
3924 /* regArray <- arrayRef */
Bill Buzbee1465db52009-09-23 17:17:35 -07003925 rlArray = loadValue(cUnit, rlArray, kCoreReg);
3926 rlIdxEnd = loadValue(cUnit, rlIdxEnd, kCoreReg);
3927 genRegImmCheck(cUnit, kArmCondEq, rlArray.lowReg, 0, 0,
Ben Cheng4238ec22009-08-24 16:32:22 -07003928 (ArmLIR *) cUnit->loopAnalysis->branchToPCR);
3929
3930 /* regLength <- len(arrayRef) */
Bill Buzbeec6f10662010-02-09 11:16:15 -08003931 regLength = dvmCompilerAllocTemp(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07003932 loadWordDisp(cUnit, rlArray.lowReg, lenOffset, regLength);
Ben Cheng4238ec22009-08-24 16:32:22 -07003933
3934 int delta = maxC;
3935 /*
3936 * If the loop end condition is ">=" instead of ">", then the largest value
3937 * of the index is "endCondition - 1".
3938 */
3939 if (dInsn->arg[2] == OP_IF_GE) {
3940 delta--;
3941 }
3942
3943 if (delta) {
Bill Buzbeec6f10662010-02-09 11:16:15 -08003944 int tReg = dvmCompilerAllocTemp(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07003945 opRegRegImm(cUnit, kOpAdd, tReg, rlIdxEnd.lowReg, delta);
3946 rlIdxEnd.lowReg = tReg;
Bill Buzbeec6f10662010-02-09 11:16:15 -08003947 dvmCompilerFreeTemp(cUnit, tReg);
Ben Cheng4238ec22009-08-24 16:32:22 -07003948 }
3949 /* Punt if "regIdxEnd < len(Array)" is false */
Bill Buzbee1465db52009-09-23 17:17:35 -07003950 genRegRegCheck(cUnit, kArmCondGe, rlIdxEnd.lowReg, regLength, 0,
Ben Cheng0fd31e42009-09-03 14:40:16 -07003951 (ArmLIR *) cUnit->loopAnalysis->branchToPCR);
Ben Cheng4238ec22009-08-24 16:32:22 -07003952}
3953
3954/*
3955 * vA = arrayReg;
3956 * vB = idxReg;
3957 * vC = endConditionReg;
3958 * arg[0] = maxC
3959 * arg[1] = minC
3960 * arg[2] = loopBranchConditionCode
3961 */
3962static void genHoistedChecksForCountDownLoop(CompilationUnit *cUnit, MIR *mir)
3963{
3964 DecodedInstruction *dInsn = &mir->dalvikInsn;
3965 const int lenOffset = offsetof(ArrayObject, length);
Bill Buzbeec6f10662010-02-09 11:16:15 -08003966 const int regLength = dvmCompilerAllocTemp(cUnit);
Ben Cheng4238ec22009-08-24 16:32:22 -07003967 const int maxC = dInsn->arg[0];
Bill Buzbee1465db52009-09-23 17:17:35 -07003968 RegLocation rlArray = cUnit->regLocation[mir->dalvikInsn.vA];
3969 RegLocation rlIdxInit = cUnit->regLocation[mir->dalvikInsn.vB];
Ben Cheng4238ec22009-08-24 16:32:22 -07003970
3971 /* regArray <- arrayRef */
Bill Buzbee1465db52009-09-23 17:17:35 -07003972 rlArray = loadValue(cUnit, rlArray, kCoreReg);
3973 rlIdxInit = loadValue(cUnit, rlIdxInit, kCoreReg);
3974 genRegImmCheck(cUnit, kArmCondEq, rlArray.lowReg, 0, 0,
Ben Cheng4238ec22009-08-24 16:32:22 -07003975 (ArmLIR *) cUnit->loopAnalysis->branchToPCR);
3976
3977 /* regLength <- len(arrayRef) */
Bill Buzbee1465db52009-09-23 17:17:35 -07003978 loadWordDisp(cUnit, rlArray.lowReg, lenOffset, regLength);
Ben Cheng4238ec22009-08-24 16:32:22 -07003979
3980 if (maxC) {
Bill Buzbeec6f10662010-02-09 11:16:15 -08003981 int tReg = dvmCompilerAllocTemp(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07003982 opRegRegImm(cUnit, kOpAdd, tReg, rlIdxInit.lowReg, maxC);
3983 rlIdxInit.lowReg = tReg;
Bill Buzbeec6f10662010-02-09 11:16:15 -08003984 dvmCompilerFreeTemp(cUnit, tReg);
Ben Cheng4238ec22009-08-24 16:32:22 -07003985 }
3986
3987 /* Punt if "regIdxInit < len(Array)" is false */
Bill Buzbee1465db52009-09-23 17:17:35 -07003988 genRegRegCheck(cUnit, kArmCondGe, rlIdxInit.lowReg, regLength, 0,
Ben Cheng0fd31e42009-09-03 14:40:16 -07003989 (ArmLIR *) cUnit->loopAnalysis->branchToPCR);
Ben Cheng4238ec22009-08-24 16:32:22 -07003990}
3991
3992/*
3993 * vA = idxReg;
3994 * vB = minC;
3995 */
3996static void genHoistedLowerBoundCheck(CompilationUnit *cUnit, MIR *mir)
3997{
3998 DecodedInstruction *dInsn = &mir->dalvikInsn;
Ben Cheng4238ec22009-08-24 16:32:22 -07003999 const int minC = dInsn->vB;
Bill Buzbee1465db52009-09-23 17:17:35 -07004000 RegLocation rlIdx = cUnit->regLocation[mir->dalvikInsn.vA];
Ben Cheng4238ec22009-08-24 16:32:22 -07004001
4002 /* regIdx <- initial index value */
Bill Buzbee1465db52009-09-23 17:17:35 -07004003 rlIdx = loadValue(cUnit, rlIdx, kCoreReg);
Ben Cheng4238ec22009-08-24 16:32:22 -07004004
4005 /* Punt if "regIdxInit + minC >= 0" is false */
Bill Buzbee1465db52009-09-23 17:17:35 -07004006 genRegImmCheck(cUnit, kArmCondLt, rlIdx.lowReg, -minC, 0,
Ben Cheng4238ec22009-08-24 16:32:22 -07004007 (ArmLIR *) cUnit->loopAnalysis->branchToPCR);
4008}
4009
Ben Cheng7a2697d2010-06-07 13:44:23 -07004010/*
4011 * vC = this
4012 *
4013 * A predicted inlining target looks like the following, where instructions
4014 * between 0x4858de66 and 0x4858de72 are checking if the predicted class
4015 * matches "this", and the verificaion code is generated by this routine.
4016 *
4017 * (C) means the instruction is inlined from the callee, and (PI) means the
4018 * instruction is the predicted inlined invoke, whose corresponding
4019 * instructions are still generated to handle the mispredicted case.
4020 *
4021 * D/dalvikvm( 86): -------- kMirOpCheckInlinePrediction
4022 * D/dalvikvm( 86): 0x4858de66 (0002): ldr r0, [r5, #68]
4023 * D/dalvikvm( 86): 0x4858de68 (0004): ldr r1, [pc, #140]
4024 * D/dalvikvm( 86): 0x4858de6a (0006): cmp r0, #0
4025 * D/dalvikvm( 86): 0x4858de6c (0008): beq 0x4858deb2
4026 * D/dalvikvm( 86): 0x4858de6e (000a): ldr r2, [r0, #0]
4027 * D/dalvikvm( 86): 0x4858de70 (000c): cmp r1, r2
4028 * D/dalvikvm( 86): 0x4858de72 (000e): bne 0x4858de7a
4029 * D/dalvikvm( 86): -------- dalvik offset: 0x004c @ +iget-object-quick (C)
4030 * v4, v17, (#8)
4031 * D/dalvikvm( 86): 0x4858de74 (0010): ldr r3, [r0, #8]
4032 * D/dalvikvm( 86): 0x4858de76 (0012): str r3, [r5, #16]
4033 * D/dalvikvm( 86): -------- dalvik offset: 0x004c @
4034 * +invoke-virtual-quick/range (PI) v17..v17
4035 * D/dalvikvm( 86): 0x4858de78 (0014): b 0x4858debc
4036 * D/dalvikvm( 86): 0x4858de7a (0016): add r4,r5,#68
4037 * D/dalvikvm( 86): -------- BARRIER
4038 * D/dalvikvm( 86): 0x4858de7e (001a): ldmia r4, <r0>
4039 * D/dalvikvm( 86): -------- BARRIER
4040 * D/dalvikvm( 86): 0x4858de80 (001c): sub r7,r5,#24
4041 * D/dalvikvm( 86): 0x4858de84 (0020): cmp r0, #0
4042 * D/dalvikvm( 86): 0x4858de86 (0022): beq 0x4858deb6
4043 * D/dalvikvm( 86): -------- BARRIER
4044 * D/dalvikvm( 86): 0x4858de88 (0024): stmia r7, <r0>
4045 * D/dalvikvm( 86): -------- BARRIER
4046 * D/dalvikvm( 86): 0x4858de8a (0026): ldr r4, [pc, #104]
4047 * D/dalvikvm( 86): 0x4858de8c (0028): add r1, pc, #28
4048 * D/dalvikvm( 86): 0x4858de8e (002a): add r2, pc, #56
4049 * D/dalvikvm( 86): 0x4858de90 (002c): blx_1 0x48589198
4050 * D/dalvikvm( 86): 0x4858de92 (002e): blx_2 see above
4051 * D/dalvikvm( 86): 0x4858de94 (0030): b 0x4858dec8
4052 * D/dalvikvm( 86): 0x4858de96 (0032): b 0x4858deb6
4053 * D/dalvikvm( 86): 0x4858de98 (0034): ldr r0, [r7, #72]
4054 * D/dalvikvm( 86): 0x4858de9a (0036): cmp r1, #0
4055 * D/dalvikvm( 86): 0x4858de9c (0038): bgt 0x4858dea4
4056 * D/dalvikvm( 86): 0x4858de9e (003a): ldr r7, [r6, #116]
4057 * D/dalvikvm( 86): 0x4858dea0 (003c): movs r1, r6
4058 * D/dalvikvm( 86): 0x4858dea2 (003e): blx r7
4059 * D/dalvikvm( 86): 0x4858dea4 (0040): add r1, pc, #4
4060 * D/dalvikvm( 86): 0x4858dea6 (0042): blx_1 0x485890a0
4061 * D/dalvikvm( 86): 0x4858dea8 (0044): blx_2 see above
4062 * D/dalvikvm( 86): 0x4858deaa (0046): b 0x4858deb6
4063 * D/dalvikvm( 86): 0x4858deac (0048): .align4
4064 * D/dalvikvm( 86): L0x004f:
4065 * D/dalvikvm( 86): -------- dalvik offset: 0x004f @ move-result-object (PI)
4066 * v4, (#0), (#0)
4067 * D/dalvikvm( 86): 0x4858deac (0048): ldr r4, [r6, #8]
4068 * D/dalvikvm( 86): 0x4858deae (004a): str r4, [r5, #16]
4069 * D/dalvikvm( 86): 0x4858deb0 (004c): b 0x4858debc
4070 * D/dalvikvm( 86): -------- reconstruct dalvik PC : 0x42beefcc @ +0x004c
4071 * D/dalvikvm( 86): 0x4858deb2 (004e): ldr r0, [pc, #64]
4072 * D/dalvikvm( 86): 0x4858deb4 (0050): b 0x4858deb8
4073 * D/dalvikvm( 86): -------- reconstruct dalvik PC : 0x42beefcc @ +0x004c
4074 * D/dalvikvm( 86): 0x4858deb6 (0052): ldr r0, [pc, #60]
4075 * D/dalvikvm( 86): Exception_Handling:
4076 * D/dalvikvm( 86): 0x4858deb8 (0054): ldr r1, [r6, #100]
4077 * D/dalvikvm( 86): 0x4858deba (0056): blx r1
4078 * D/dalvikvm( 86): 0x4858debc (0058): .align4
4079 * D/dalvikvm( 86): -------- chaining cell (hot): 0x0050
4080 * D/dalvikvm( 86): 0x4858debc (0058): b 0x4858dec0
4081 * D/dalvikvm( 86): 0x4858debe (005a): orrs r0, r0
4082 * D/dalvikvm( 86): 0x4858dec0 (005c): ldr r0, [r6, #112]
4083 * D/dalvikvm( 86): 0x4858dec2 (005e): blx r0
4084 * D/dalvikvm( 86): 0x4858dec4 (0060): data 0xefd4(61396)
4085 * D/dalvikvm( 86): 0x4858dec6 (0062): data 0x42be(17086)
4086 * D/dalvikvm( 86): 0x4858dec8 (0064): .align4
4087 * D/dalvikvm( 86): -------- chaining cell (predicted)
4088 * D/dalvikvm( 86): 0x4858dec8 (0064): data 0xe7fe(59390)
4089 * D/dalvikvm( 86): 0x4858deca (0066): data 0x0000(0)
4090 * D/dalvikvm( 86): 0x4858decc (0068): data 0x0000(0)
4091 * D/dalvikvm( 86): 0x4858dece (006a): data 0x0000(0)
4092 * :
4093 */
4094static void genValidationForPredictedInline(CompilationUnit *cUnit, MIR *mir)
4095{
4096 CallsiteInfo *callsiteInfo = mir->meta.callsiteInfo;
4097 RegLocation rlThis = cUnit->regLocation[mir->dalvikInsn.vC];
4098
4099 rlThis = loadValue(cUnit, rlThis, kCoreReg);
4100 int regPredictedClass = dvmCompilerAllocTemp(cUnit);
Ben Cheng385828e2011-03-04 16:48:33 -08004101 loadClassPointer(cUnit, regPredictedClass, (int) callsiteInfo);
Ben Cheng7a2697d2010-06-07 13:44:23 -07004102 genNullCheck(cUnit, rlThis.sRegLow, rlThis.lowReg, mir->offset,
4103 NULL);/* null object? */
4104 int regActualClass = dvmCompilerAllocTemp(cUnit);
4105 loadWordDisp(cUnit, rlThis.lowReg, offsetof(Object, clazz), regActualClass);
4106 opRegReg(cUnit, kOpCmp, regPredictedClass, regActualClass);
4107 /*
4108 * Set the misPredBranchOver target so that it will be generated when the
4109 * code for the non-optimized invoke is generated.
4110 */
4111 callsiteInfo->misPredBranchOver = (LIR *) opCondBranch(cUnit, kArmCondNe);
4112}
4113
Ben Cheng4238ec22009-08-24 16:32:22 -07004114/* Extended MIR instructions like PHI */
4115static void handleExtendedMIR(CompilationUnit *cUnit, MIR *mir)
4116{
Dan Bornstein9a1f8162010-12-01 17:02:26 -08004117 int opOffset = mir->dalvikInsn.opcode - kMirOpFirst;
Carl Shapirofc75f3e2010-12-07 11:43:38 -08004118 char *msg = (char *)dvmCompilerNew(strlen(extendedMIROpNames[opOffset]) + 1,
4119 false);
Ben Cheng4238ec22009-08-24 16:32:22 -07004120 strcpy(msg, extendedMIROpNames[opOffset]);
Bill Buzbee1465db52009-09-23 17:17:35 -07004121 newLIR1(cUnit, kArmPseudoExtended, (int) msg);
Ben Cheng4238ec22009-08-24 16:32:22 -07004122
Dan Bornstein9a1f8162010-12-01 17:02:26 -08004123 switch (mir->dalvikInsn.opcode) {
Bill Buzbee1465db52009-09-23 17:17:35 -07004124 case kMirOpPhi: {
Ben Cheng4238ec22009-08-24 16:32:22 -07004125 char *ssaString = dvmCompilerGetSSAString(cUnit, mir->ssaRep);
Bill Buzbee1465db52009-09-23 17:17:35 -07004126 newLIR1(cUnit, kArmPseudoSSARep, (int) ssaString);
Ben Cheng4238ec22009-08-24 16:32:22 -07004127 break;
4128 }
Bill Buzbee1465db52009-09-23 17:17:35 -07004129 case kMirOpNullNRangeUpCheck: {
Ben Cheng4238ec22009-08-24 16:32:22 -07004130 genHoistedChecksForCountUpLoop(cUnit, mir);
4131 break;
4132 }
Bill Buzbee1465db52009-09-23 17:17:35 -07004133 case kMirOpNullNRangeDownCheck: {
Ben Cheng4238ec22009-08-24 16:32:22 -07004134 genHoistedChecksForCountDownLoop(cUnit, mir);
4135 break;
4136 }
Bill Buzbee1465db52009-09-23 17:17:35 -07004137 case kMirOpLowerBound: {
Ben Cheng4238ec22009-08-24 16:32:22 -07004138 genHoistedLowerBoundCheck(cUnit, mir);
4139 break;
4140 }
Bill Buzbee1465db52009-09-23 17:17:35 -07004141 case kMirOpPunt: {
Ben Cheng4238ec22009-08-24 16:32:22 -07004142 genUnconditionalBranch(cUnit,
4143 (ArmLIR *) cUnit->loopAnalysis->branchToPCR);
4144 break;
4145 }
Ben Cheng7a2697d2010-06-07 13:44:23 -07004146 case kMirOpCheckInlinePrediction: {
4147 genValidationForPredictedInline(cUnit, mir);
4148 break;
4149 }
Ben Cheng4238ec22009-08-24 16:32:22 -07004150 default:
4151 break;
4152 }
4153}
4154
4155/*
4156 * Create a PC-reconstruction cell for the starting offset of this trace.
4157 * Since the PCR cell is placed near the end of the compiled code which is
4158 * usually out of range for a conditional branch, we put two branches (one
4159 * branch over to the loop body and one layover branch to the actual PCR) at the
4160 * end of the entry block.
4161 */
4162static void setupLoopEntryBlock(CompilationUnit *cUnit, BasicBlock *entry,
4163 ArmLIR *bodyLabel)
4164{
4165 /* Set up the place holder to reconstruct this Dalvik PC */
Carl Shapirofc75f3e2010-12-07 11:43:38 -08004166 ArmLIR *pcrLabel = (ArmLIR *) dvmCompilerNew(sizeof(ArmLIR), true);
Dan Bornstein9a1f8162010-12-01 17:02:26 -08004167 pcrLabel->opcode = kArmPseudoPCReconstructionCell;
Ben Cheng4238ec22009-08-24 16:32:22 -07004168 pcrLabel->operands[0] =
4169 (int) (cUnit->method->insns + entry->startOffset);
4170 pcrLabel->operands[1] = entry->startOffset;
4171 /* Insert the place holder to the growable list */
Ben Cheng00603072010-10-28 11:13:58 -07004172 dvmInsertGrowableList(&cUnit->pcReconstructionList, (intptr_t) pcrLabel);
Ben Cheng4238ec22009-08-24 16:32:22 -07004173
4174 /*
4175 * Next, create two branches - one branch over to the loop body and the
4176 * other branch to the PCR cell to punt.
4177 */
Carl Shapirofc75f3e2010-12-07 11:43:38 -08004178 ArmLIR *branchToBody = (ArmLIR *) dvmCompilerNew(sizeof(ArmLIR), true);
Dan Bornstein9a1f8162010-12-01 17:02:26 -08004179 branchToBody->opcode = kThumbBUncond;
Ben Cheng4238ec22009-08-24 16:32:22 -07004180 branchToBody->generic.target = (LIR *) bodyLabel;
Ben Chengdcf3e5d2009-09-11 13:42:05 -07004181 setupResourceMasks(branchToBody);
Ben Cheng4238ec22009-08-24 16:32:22 -07004182 cUnit->loopAnalysis->branchToBody = (LIR *) branchToBody;
4183
Carl Shapirofc75f3e2010-12-07 11:43:38 -08004184 ArmLIR *branchToPCR = (ArmLIR *) dvmCompilerNew(sizeof(ArmLIR), true);
Dan Bornstein9a1f8162010-12-01 17:02:26 -08004185 branchToPCR->opcode = kThumbBUncond;
Ben Cheng4238ec22009-08-24 16:32:22 -07004186 branchToPCR->generic.target = (LIR *) pcrLabel;
Ben Chengdcf3e5d2009-09-11 13:42:05 -07004187 setupResourceMasks(branchToPCR);
Ben Cheng4238ec22009-08-24 16:32:22 -07004188 cUnit->loopAnalysis->branchToPCR = (LIR *) branchToPCR;
4189}
4190
Ben Chengd5adae12010-03-26 17:45:28 -07004191#if defined(WITH_SELF_VERIFICATION)
4192static bool selfVerificationPuntOps(MIR *mir)
4193{
4194 DecodedInstruction *decInsn = &mir->dalvikInsn;
Ben Cheng7a2697d2010-06-07 13:44:23 -07004195
Ben Chengd5adae12010-03-26 17:45:28 -07004196 /*
4197 * All opcodes that can throw exceptions and use the
4198 * TEMPLATE_THROW_EXCEPTION_COMMON template should be excluded in the trace
4199 * under self-verification mode.
4200 */
Ben Cheng072b5d02011-03-31 10:44:44 -07004201 switch (decInsn->opcode) {
4202 case OP_MONITOR_ENTER:
4203 case OP_MONITOR_EXIT:
4204 case OP_NEW_INSTANCE:
4205 case OP_NEW_INSTANCE_JUMBO:
4206 case OP_NEW_ARRAY:
4207 case OP_NEW_ARRAY_JUMBO:
4208 case OP_CHECK_CAST:
4209 case OP_CHECK_CAST_JUMBO:
4210 case OP_MOVE_EXCEPTION:
4211 case OP_FILL_ARRAY_DATA:
4212 case OP_EXECUTE_INLINE:
4213 case OP_EXECUTE_INLINE_RANGE:
4214 return true;
4215 default:
4216 return false;
4217 }
Ben Chengd5adae12010-03-26 17:45:28 -07004218}
4219#endif
4220
Ben Chengba4fc8b2009-06-01 13:00:29 -07004221void dvmCompilerMIR2LIR(CompilationUnit *cUnit)
4222{
4223 /* Used to hold the labels of each block */
Bill Buzbee89efc3d2009-07-28 11:22:22 -07004224 ArmLIR *labelList =
Carl Shapirofc75f3e2010-12-07 11:43:38 -08004225 (ArmLIR *) dvmCompilerNew(sizeof(ArmLIR) * cUnit->numBlocks, true);
Ben Chengcec26f62010-01-15 15:29:33 -08004226 GrowableList chainingListByType[kChainingCellGap];
Ben Chengba4fc8b2009-06-01 13:00:29 -07004227 int i;
4228
4229 /*
Ben Cheng38329f52009-07-07 14:19:20 -07004230 * Initialize various types chaining lists.
Ben Chengba4fc8b2009-06-01 13:00:29 -07004231 */
Ben Chengcec26f62010-01-15 15:29:33 -08004232 for (i = 0; i < kChainingCellGap; i++) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07004233 dvmInitGrowableList(&chainingListByType[i], 2);
4234 }
4235
Ben Cheng7ab74e12011-02-03 14:02:06 -08004236 /* Clear the visited flag for each block */
4237 dvmCompilerDataFlowAnalysisDispatcher(cUnit, dvmCompilerClearVisitedFlag,
4238 kAllNodes, false /* isIterative */);
4239
Ben Cheng00603072010-10-28 11:13:58 -07004240 GrowableListIterator iterator;
4241 dvmGrowableListIteratorInit(&cUnit->blockList, &iterator);
Ben Chengba4fc8b2009-06-01 13:00:29 -07004242
buzbee2e152ba2010-12-15 16:32:35 -08004243 /* Traces start with a profiling entry point. Generate it here */
4244 cUnit->profileCodeSize = genTraceProfileEntry(cUnit);
Ben Cheng1efc9c52009-06-08 18:25:27 -07004245
Ben Chengba4fc8b2009-06-01 13:00:29 -07004246 /* Handle the content in each basic block */
Ben Cheng00603072010-10-28 11:13:58 -07004247 for (i = 0; ; i++) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07004248 MIR *mir;
Ben Cheng00603072010-10-28 11:13:58 -07004249 BasicBlock *bb = (BasicBlock *) dvmGrowableListIteratorNext(&iterator);
4250 if (bb == NULL) break;
Ben Cheng7ab74e12011-02-03 14:02:06 -08004251 if (bb->visited == true) continue;
Ben Chengba4fc8b2009-06-01 13:00:29 -07004252
Ben Cheng00603072010-10-28 11:13:58 -07004253 labelList[i].operands[0] = bb->startOffset;
Ben Chengba4fc8b2009-06-01 13:00:29 -07004254
Ben Cheng00603072010-10-28 11:13:58 -07004255 if (bb->blockType >= kChainingCellGap) {
4256 if (bb->isFallThroughFromInvoke == true) {
Ben Chengd44faf52010-06-02 15:33:51 -07004257 /* Align this block first since it is a return chaining cell */
4258 newLIR0(cUnit, kArmPseudoPseudoAlign4);
4259 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07004260 /*
4261 * Append the label pseudo LIR first. Chaining cells will be handled
4262 * separately afterwards.
4263 */
4264 dvmCompilerAppendLIR(cUnit, (LIR *) &labelList[i]);
4265 }
4266
Ben Cheng32115a92011-03-22 14:09:09 -07004267 if (bb->blockType == kEntryBlock) {
Dan Bornstein9a1f8162010-12-01 17:02:26 -08004268 labelList[i].opcode = kArmPseudoEntryBlock;
Ben Cheng00603072010-10-28 11:13:58 -07004269 if (bb->firstMIRInsn == NULL) {
Ben Cheng4238ec22009-08-24 16:32:22 -07004270 continue;
4271 } else {
Ben Cheng00603072010-10-28 11:13:58 -07004272 setupLoopEntryBlock(cUnit, bb,
4273 &labelList[bb->fallThrough->id]);
Ben Cheng4238ec22009-08-24 16:32:22 -07004274 }
Ben Cheng32115a92011-03-22 14:09:09 -07004275 } else if (bb->blockType == kExitBlock) {
Dan Bornstein9a1f8162010-12-01 17:02:26 -08004276 labelList[i].opcode = kArmPseudoExitBlock;
Ben Cheng4238ec22009-08-24 16:32:22 -07004277 goto gen_fallthrough;
Ben Cheng00603072010-10-28 11:13:58 -07004278 } else if (bb->blockType == kDalvikByteCode) {
Ben Cheng32115a92011-03-22 14:09:09 -07004279 if (bb->hidden == true) continue;
Dan Bornstein9a1f8162010-12-01 17:02:26 -08004280 labelList[i].opcode = kArmPseudoNormalBlockLabel;
Ben Chenge9695e52009-06-16 16:11:47 -07004281 /* Reset the register state */
Bill Buzbeec6f10662010-02-09 11:16:15 -08004282 dvmCompilerResetRegPool(cUnit);
4283 dvmCompilerClobberAllRegs(cUnit);
4284 dvmCompilerResetNullCheck(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -07004285 } else {
Ben Cheng00603072010-10-28 11:13:58 -07004286 switch (bb->blockType) {
Bill Buzbee1465db52009-09-23 17:17:35 -07004287 case kChainingCellNormal:
Dan Bornstein9a1f8162010-12-01 17:02:26 -08004288 labelList[i].opcode = kArmPseudoChainingCellNormal;
Ben Chengba4fc8b2009-06-01 13:00:29 -07004289 /* handle the codegen later */
4290 dvmInsertGrowableList(
Ben Cheng00603072010-10-28 11:13:58 -07004291 &chainingListByType[kChainingCellNormal], i);
Ben Chengba4fc8b2009-06-01 13:00:29 -07004292 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07004293 case kChainingCellInvokeSingleton:
Dan Bornstein9a1f8162010-12-01 17:02:26 -08004294 labelList[i].opcode =
Ben Chenga4973592010-03-31 11:59:18 -07004295 kArmPseudoChainingCellInvokeSingleton;
Ben Chengba4fc8b2009-06-01 13:00:29 -07004296 labelList[i].operands[0] =
Ben Cheng00603072010-10-28 11:13:58 -07004297 (int) bb->containingMethod;
Ben Chengba4fc8b2009-06-01 13:00:29 -07004298 /* handle the codegen later */
4299 dvmInsertGrowableList(
Ben Cheng00603072010-10-28 11:13:58 -07004300 &chainingListByType[kChainingCellInvokeSingleton], i);
Ben Cheng38329f52009-07-07 14:19:20 -07004301 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07004302 case kChainingCellInvokePredicted:
Dan Bornstein9a1f8162010-12-01 17:02:26 -08004303 labelList[i].opcode =
Ben Chenga4973592010-03-31 11:59:18 -07004304 kArmPseudoChainingCellInvokePredicted;
Ben Cheng04517042011-03-14 11:16:21 -07004305 /*
4306 * Move the cached method pointer from operand 1 to 0.
4307 * Operand 0 was clobbered earlier in this routine to store
4308 * the block starting offset, which is not applicable to
4309 * predicted chaining cell.
4310 */
4311 labelList[i].operands[0] = labelList[i].operands[1];
Ben Cheng38329f52009-07-07 14:19:20 -07004312 /* handle the codegen later */
4313 dvmInsertGrowableList(
Ben Cheng00603072010-10-28 11:13:58 -07004314 &chainingListByType[kChainingCellInvokePredicted], i);
Ben Chengba4fc8b2009-06-01 13:00:29 -07004315 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07004316 case kChainingCellHot:
Dan Bornstein9a1f8162010-12-01 17:02:26 -08004317 labelList[i].opcode =
Ben Chenga4973592010-03-31 11:59:18 -07004318 kArmPseudoChainingCellHot;
Ben Chengba4fc8b2009-06-01 13:00:29 -07004319 /* handle the codegen later */
4320 dvmInsertGrowableList(
Ben Cheng00603072010-10-28 11:13:58 -07004321 &chainingListByType[kChainingCellHot], i);
Ben Chengba4fc8b2009-06-01 13:00:29 -07004322 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07004323 case kPCReconstruction:
Ben Chengba4fc8b2009-06-01 13:00:29 -07004324 /* Make sure exception handling block is next */
Dan Bornstein9a1f8162010-12-01 17:02:26 -08004325 labelList[i].opcode =
Ben Chenga4973592010-03-31 11:59:18 -07004326 kArmPseudoPCReconstructionBlockLabel;
Ben Cheng32115a92011-03-22 14:09:09 -07004327 handlePCReconstruction(cUnit,
4328 &labelList[cUnit->puntBlock->id]);
Ben Chengba4fc8b2009-06-01 13:00:29 -07004329 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07004330 case kExceptionHandling:
Dan Bornstein9a1f8162010-12-01 17:02:26 -08004331 labelList[i].opcode = kArmPseudoEHBlockLabel;
Ben Chengba4fc8b2009-06-01 13:00:29 -07004332 if (cUnit->pcReconstructionList.numUsed) {
Ben Cheng20d7e6c2011-02-18 17:12:42 -08004333 loadWordDisp(cUnit, r6SELF, offsetof(Thread,
Bill Buzbee270c1d62009-08-13 16:58:07 -07004334 jitToInterpEntries.dvmJitToInterpPunt),
4335 r1);
Bill Buzbee1465db52009-09-23 17:17:35 -07004336 opReg(cUnit, kOpBlx, r1);
Ben Chengba4fc8b2009-06-01 13:00:29 -07004337 }
4338 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07004339 case kChainingCellBackwardBranch:
Dan Bornstein9a1f8162010-12-01 17:02:26 -08004340 labelList[i].opcode =
Ben Chenga4973592010-03-31 11:59:18 -07004341 kArmPseudoChainingCellBackwardBranch;
Jeff Hao97319a82009-08-12 16:57:15 -07004342 /* handle the codegen later */
4343 dvmInsertGrowableList(
Bill Buzbee1465db52009-09-23 17:17:35 -07004344 &chainingListByType[kChainingCellBackwardBranch],
Ben Cheng00603072010-10-28 11:13:58 -07004345 i);
Jeff Hao97319a82009-08-12 16:57:15 -07004346 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07004347 default:
4348 break;
4349 }
4350 continue;
4351 }
Ben Chenge9695e52009-06-16 16:11:47 -07004352
Bill Buzbee89efc3d2009-07-28 11:22:22 -07004353 ArmLIR *headLIR = NULL;
Ben Cheng7ab74e12011-02-03 14:02:06 -08004354 BasicBlock *nextBB = bb;
Ben Chenge9695e52009-06-16 16:11:47 -07004355
Ben Cheng7ab74e12011-02-03 14:02:06 -08004356 /*
4357 * Try to build a longer optimization unit. Currently if the previous
4358 * block ends with a goto, we continue adding instructions and don't
4359 * reset the register allocation pool.
4360 */
4361 for (; nextBB != NULL; nextBB = cUnit->nextCodegenBlock) {
4362 bb = nextBB;
4363 bb->visited = true;
4364 cUnit->nextCodegenBlock = NULL;
Bill Buzbee1465db52009-09-23 17:17:35 -07004365
Ben Cheng7ab74e12011-02-03 14:02:06 -08004366 for (mir = bb->firstMIRInsn; mir; mir = mir->next) {
Bill Buzbee1465db52009-09-23 17:17:35 -07004367
Ben Cheng7ab74e12011-02-03 14:02:06 -08004368 dvmCompilerResetRegPool(cUnit);
4369 if (gDvmJit.disableOpt & (1 << kTrackLiveTemps)) {
4370 dvmCompilerClobberAllRegs(cUnit);
Ben Cheng80211d22011-01-14 10:23:37 -08004371 }
Ben Cheng4238ec22009-08-24 16:32:22 -07004372
Ben Cheng7ab74e12011-02-03 14:02:06 -08004373 if (gDvmJit.disableOpt & (1 << kSuppressLoads)) {
4374 dvmCompilerResetDefTracking(cUnit);
4375 }
Ben Cheng4238ec22009-08-24 16:32:22 -07004376
Ben Cheng7ab74e12011-02-03 14:02:06 -08004377 if (mir->dalvikInsn.opcode >= kMirOpFirst) {
4378 handleExtendedMIR(cUnit, mir);
4379 continue;
4380 }
4381
4382
4383 Opcode dalvikOpcode = mir->dalvikInsn.opcode;
4384 InstructionFormat dalvikFormat =
4385 dexGetFormatFromOpcode(dalvikOpcode);
4386 char *note;
4387 if (mir->OptimizationFlags & MIR_INLINED) {
4388 note = " (I)";
4389 } else if (mir->OptimizationFlags & MIR_INLINED_PRED) {
4390 note = " (PI)";
4391 } else if (mir->OptimizationFlags & MIR_CALLEE) {
4392 note = " (C)";
4393 } else {
4394 note = NULL;
4395 }
4396
4397 ArmLIR *boundaryLIR;
4398
4399 /*
4400 * Don't generate the boundary LIR unless we are debugging this
4401 * trace or we need a scheduling barrier.
4402 */
4403 if (headLIR == NULL || cUnit->printMe == true) {
4404 boundaryLIR =
4405 newLIR2(cUnit, kArmPseudoDalvikByteCodeBoundary,
4406 mir->offset,
4407 (int) dvmCompilerGetDalvikDisassembly(
4408 &mir->dalvikInsn, note));
4409 /* Remember the first LIR for this block */
4410 if (headLIR == NULL) {
4411 headLIR = boundaryLIR;
4412 /* Set the first boundaryLIR as a scheduling barrier */
4413 headLIR->defMask = ENCODE_ALL;
4414 }
4415 }
4416
4417 /*
4418 * Don't generate the SSA annotation unless verbose mode is on
4419 */
4420 if (cUnit->printMe && mir->ssaRep) {
4421 char *ssaString = dvmCompilerGetSSAString(cUnit,
4422 mir->ssaRep);
4423 newLIR1(cUnit, kArmPseudoSSARep, (int) ssaString);
4424 }
4425
4426 bool notHandled;
4427 /*
4428 * Debugging: screen the opcode first to see if it is in the
4429 * do[-not]-compile list
4430 */
4431 bool singleStepMe = SINGLE_STEP_OP(dalvikOpcode);
Ben Chengd5adae12010-03-26 17:45:28 -07004432#if defined(WITH_SELF_VERIFICATION)
Ben Cheng7ab74e12011-02-03 14:02:06 -08004433 if (singleStepMe == false) {
4434 singleStepMe = selfVerificationPuntOps(mir);
4435 }
Ben Chengd5adae12010-03-26 17:45:28 -07004436#endif
Ben Cheng7ab74e12011-02-03 14:02:06 -08004437 if (singleStepMe || cUnit->allSingleStep) {
4438 notHandled = false;
4439 genInterpSingleStep(cUnit, mir);
4440 } else {
4441 opcodeCoverage[dalvikOpcode]++;
4442 switch (dalvikFormat) {
4443 case kFmt10t:
4444 case kFmt20t:
4445 case kFmt30t:
4446 notHandled = handleFmt10t_Fmt20t_Fmt30t(cUnit,
4447 mir, bb, labelList);
4448 break;
4449 case kFmt10x:
4450 notHandled = handleFmt10x(cUnit, mir);
4451 break;
4452 case kFmt11n:
4453 case kFmt31i:
4454 notHandled = handleFmt11n_Fmt31i(cUnit, mir);
4455 break;
4456 case kFmt11x:
4457 notHandled = handleFmt11x(cUnit, mir);
4458 break;
4459 case kFmt12x:
4460 notHandled = handleFmt12x(cUnit, mir);
4461 break;
4462 case kFmt20bc:
4463 case kFmt40sc:
4464 notHandled = handleFmt20bc_Fmt40sc(cUnit, mir);
4465 break;
4466 case kFmt21c:
4467 case kFmt31c:
4468 case kFmt41c:
4469 notHandled = handleFmt21c_Fmt31c_Fmt41c(cUnit, mir);
4470 break;
4471 case kFmt21h:
4472 notHandled = handleFmt21h(cUnit, mir);
4473 break;
4474 case kFmt21s:
4475 notHandled = handleFmt21s(cUnit, mir);
4476 break;
4477 case kFmt21t:
4478 notHandled = handleFmt21t(cUnit, mir, bb,
Ben Chengba4fc8b2009-06-01 13:00:29 -07004479 labelList);
Ben Cheng7ab74e12011-02-03 14:02:06 -08004480 break;
4481 case kFmt22b:
4482 case kFmt22s:
4483 notHandled = handleFmt22b_Fmt22s(cUnit, mir);
4484 break;
4485 case kFmt22c:
4486 case kFmt52c:
4487 notHandled = handleFmt22c_Fmt52c(cUnit, mir);
4488 break;
4489 case kFmt22cs:
4490 notHandled = handleFmt22cs(cUnit, mir);
4491 break;
4492 case kFmt22t:
4493 notHandled = handleFmt22t(cUnit, mir, bb,
4494 labelList);
4495 break;
4496 case kFmt22x:
4497 case kFmt32x:
4498 notHandled = handleFmt22x_Fmt32x(cUnit, mir);
4499 break;
4500 case kFmt23x:
4501 notHandled = handleFmt23x(cUnit, mir);
4502 break;
4503 case kFmt31t:
4504 notHandled = handleFmt31t(cUnit, mir);
4505 break;
4506 case kFmt3rc:
4507 case kFmt35c:
4508 case kFmt5rc:
4509 notHandled = handleFmt35c_3rc_5rc(cUnit, mir, bb,
4510 labelList);
4511 break;
4512 case kFmt3rms:
4513 case kFmt35ms:
4514 notHandled = handleFmt35ms_3rms(cUnit, mir, bb,
4515 labelList);
4516 break;
4517 case kFmt35mi:
4518 case kFmt3rmi:
4519 notHandled = handleExecuteInline(cUnit, mir);
4520 break;
4521 case kFmt51l:
4522 notHandled = handleFmt51l(cUnit, mir);
4523 break;
4524 default:
4525 notHandled = true;
4526 break;
4527 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07004528 }
Ben Cheng7ab74e12011-02-03 14:02:06 -08004529 if (notHandled) {
4530 LOGE("%#06x: Opcode 0x%x (%s) / Fmt %d not handled\n",
4531 mir->offset,
4532 dalvikOpcode, dexGetOpcodeName(dalvikOpcode),
4533 dalvikFormat);
4534 dvmCompilerAbort(cUnit);
4535 break;
4536 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07004537 }
4538 }
Ben Cheng4238ec22009-08-24 16:32:22 -07004539
Ben Cheng32115a92011-03-22 14:09:09 -07004540 if (bb->blockType == kEntryBlock) {
Ben Cheng4238ec22009-08-24 16:32:22 -07004541 dvmCompilerAppendLIR(cUnit,
4542 (LIR *) cUnit->loopAnalysis->branchToBody);
4543 dvmCompilerAppendLIR(cUnit,
4544 (LIR *) cUnit->loopAnalysis->branchToPCR);
4545 }
4546
4547 if (headLIR) {
4548 /*
4549 * Eliminate redundant loads/stores and delay stores into later
4550 * slots
4551 */
4552 dvmCompilerApplyLocalOptimizations(cUnit, (LIR *) headLIR,
4553 cUnit->lastLIRInsn);
4554 }
4555
4556gen_fallthrough:
Ben Cheng1efc9c52009-06-08 18:25:27 -07004557 /*
4558 * Check if the block is terminated due to trace length constraint -
4559 * insert an unconditional branch to the chaining cell.
4560 */
Ben Cheng00603072010-10-28 11:13:58 -07004561 if (bb->needFallThroughBranch) {
Ben Cheng7ab74e12011-02-03 14:02:06 -08004562 genUnconditionalBranch(cUnit, &labelList[bb->fallThrough->id]);
Ben Cheng1efc9c52009-06-08 18:25:27 -07004563 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07004564 }
4565
Ben Chenge9695e52009-06-16 16:11:47 -07004566 /* Handle the chaining cells in predefined order */
Ben Chengcec26f62010-01-15 15:29:33 -08004567 for (i = 0; i < kChainingCellGap; i++) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07004568 size_t j;
4569 int *blockIdList = (int *) chainingListByType[i].elemList;
4570
4571 cUnit->numChainingCells[i] = chainingListByType[i].numUsed;
4572
4573 /* No chaining cells of this type */
4574 if (cUnit->numChainingCells[i] == 0)
4575 continue;
4576
4577 /* Record the first LIR for a new type of chaining cell */
4578 cUnit->firstChainingLIR[i] = (LIR *) &labelList[blockIdList[0]];
4579
4580 for (j = 0; j < chainingListByType[i].numUsed; j++) {
4581 int blockId = blockIdList[j];
Ben Cheng00603072010-10-28 11:13:58 -07004582 BasicBlock *chainingBlock =
4583 (BasicBlock *) dvmGrowableListGetElement(&cUnit->blockList,
4584 blockId);
Ben Chengba4fc8b2009-06-01 13:00:29 -07004585
4586 /* Align this chaining cell first */
Bill Buzbee1465db52009-09-23 17:17:35 -07004587 newLIR0(cUnit, kArmPseudoPseudoAlign4);
Ben Chengba4fc8b2009-06-01 13:00:29 -07004588
4589 /* Insert the pseudo chaining instruction */
4590 dvmCompilerAppendLIR(cUnit, (LIR *) &labelList[blockId]);
4591
4592
Ben Cheng00603072010-10-28 11:13:58 -07004593 switch (chainingBlock->blockType) {
Bill Buzbee1465db52009-09-23 17:17:35 -07004594 case kChainingCellNormal:
Ben Cheng00603072010-10-28 11:13:58 -07004595 handleNormalChainingCell(cUnit, chainingBlock->startOffset);
Ben Chengba4fc8b2009-06-01 13:00:29 -07004596 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07004597 case kChainingCellInvokeSingleton:
Ben Cheng38329f52009-07-07 14:19:20 -07004598 handleInvokeSingletonChainingCell(cUnit,
Ben Cheng00603072010-10-28 11:13:58 -07004599 chainingBlock->containingMethod);
Ben Chengba4fc8b2009-06-01 13:00:29 -07004600 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07004601 case kChainingCellInvokePredicted:
Ben Cheng38329f52009-07-07 14:19:20 -07004602 handleInvokePredictedChainingCell(cUnit);
4603 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07004604 case kChainingCellHot:
Ben Cheng00603072010-10-28 11:13:58 -07004605 handleHotChainingCell(cUnit, chainingBlock->startOffset);
Ben Chengba4fc8b2009-06-01 13:00:29 -07004606 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07004607 case kChainingCellBackwardBranch:
Jeff Hao97319a82009-08-12 16:57:15 -07004608 handleBackwardBranchChainingCell(cUnit,
Ben Cheng00603072010-10-28 11:13:58 -07004609 chainingBlock->startOffset);
Jeff Hao97319a82009-08-12 16:57:15 -07004610 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07004611 default:
Ben Cheng00603072010-10-28 11:13:58 -07004612 LOGE("Bad blocktype %d", chainingBlock->blockType);
Bill Buzbeefc519dc2010-03-06 23:30:57 -08004613 dvmCompilerAbort(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -07004614 }
4615 }
4616 }
Ben Chenge9695e52009-06-16 16:11:47 -07004617
Ben Chengcec26f62010-01-15 15:29:33 -08004618 /* Mark the bottom of chaining cells */
4619 cUnit->chainingCellBottom = (LIR *) newLIR0(cUnit, kArmChainingCellBottom);
4620
Ben Cheng6c10a972009-10-29 14:39:18 -07004621 /*
4622 * Generate the branch to the dvmJitToInterpNoChain entry point at the end
4623 * of all chaining cells for the overflow cases.
4624 */
4625 if (cUnit->switchOverflowPad) {
4626 loadConstant(cUnit, r0, (int) cUnit->switchOverflowPad);
Ben Cheng20d7e6c2011-02-18 17:12:42 -08004627 loadWordDisp(cUnit, r6SELF, offsetof(Thread,
Ben Cheng6c10a972009-10-29 14:39:18 -07004628 jitToInterpEntries.dvmJitToInterpNoChain), r2);
4629 opRegReg(cUnit, kOpAdd, r1, r1);
4630 opRegRegReg(cUnit, kOpAdd, r4PC, r0, r1);
Ben Cheng978738d2010-05-13 13:45:57 -07004631#if defined(WITH_JIT_TUNING)
Ben Cheng6c10a972009-10-29 14:39:18 -07004632 loadConstant(cUnit, r0, kSwitchOverflow);
4633#endif
4634 opReg(cUnit, kOpBlx, r2);
4635 }
4636
Ben Chenge9695e52009-06-16 16:11:47 -07004637 dvmCompilerApplyGlobalOptimizations(cUnit);
jeffhao9e45c0b2010-02-03 10:24:05 -08004638
4639#if defined(WITH_SELF_VERIFICATION)
4640 selfVerificationBranchInsertPass(cUnit);
4641#endif
Ben Chengba4fc8b2009-06-01 13:00:29 -07004642}
4643
buzbee2e152ba2010-12-15 16:32:35 -08004644/*
4645 * Accept the work and start compiling. Returns true if compilation
4646 * is attempted.
4647 */
Bill Buzbee716f1202009-07-23 13:22:09 -07004648bool dvmCompilerDoWork(CompilerWorkOrder *work)
Ben Chengba4fc8b2009-06-01 13:00:29 -07004649{
Carl Shapirofc75f3e2010-12-07 11:43:38 -08004650 JitTraceDescription *desc;
buzbee2e152ba2010-12-15 16:32:35 -08004651 bool isCompile;
4652 bool success = true;
Ben Chengba4fc8b2009-06-01 13:00:29 -07004653
Ben Cheng6999d842010-01-26 16:46:15 -08004654 if (gDvmJit.codeCacheFull) {
Ben Chengccd6c012009-10-15 14:52:45 -07004655 return false;
4656 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07004657
Ben Chengccd6c012009-10-15 14:52:45 -07004658 switch (work->kind) {
Ben Chengccd6c012009-10-15 14:52:45 -07004659 case kWorkOrderTrace:
buzbee2e152ba2010-12-15 16:32:35 -08004660 isCompile = true;
Ben Chengccd6c012009-10-15 14:52:45 -07004661 /* Start compilation with maximally allowed trace length */
Carl Shapirofc75f3e2010-12-07 11:43:38 -08004662 desc = (JitTraceDescription *)work->info;
buzbee2e152ba2010-12-15 16:32:35 -08004663 success = dvmCompileTrace(desc, JIT_MAX_TRACE_LEN, &work->result,
4664 work->bailPtr, 0 /* no hints */);
Ben Chengccd6c012009-10-15 14:52:45 -07004665 break;
4666 case kWorkOrderTraceDebug: {
4667 bool oldPrintMe = gDvmJit.printMe;
4668 gDvmJit.printMe = true;
buzbee2e152ba2010-12-15 16:32:35 -08004669 isCompile = true;
Ben Chengccd6c012009-10-15 14:52:45 -07004670 /* Start compilation with maximally allowed trace length */
Carl Shapirofc75f3e2010-12-07 11:43:38 -08004671 desc = (JitTraceDescription *)work->info;
buzbee2e152ba2010-12-15 16:32:35 -08004672 success = dvmCompileTrace(desc, JIT_MAX_TRACE_LEN, &work->result,
4673 work->bailPtr, 0 /* no hints */);
Elliott Hughes672511b2010-04-26 17:40:13 -07004674 gDvmJit.printMe = oldPrintMe;
Ben Chengccd6c012009-10-15 14:52:45 -07004675 break;
4676 }
buzbee2e152ba2010-12-15 16:32:35 -08004677 case kWorkOrderProfileMode:
4678 dvmJitChangeProfileMode((TraceProfilingModes)work->info);
4679 isCompile = false;
4680 break;
Ben Chengccd6c012009-10-15 14:52:45 -07004681 default:
buzbee2e152ba2010-12-15 16:32:35 -08004682 isCompile = false;
Bill Buzbeefc519dc2010-03-06 23:30:57 -08004683 LOGE("Jit: unknown work order type");
Elliott Hughes672511b2010-04-26 17:40:13 -07004684 assert(0); // Bail if debug build, discard otherwise
Ben Chengccd6c012009-10-15 14:52:45 -07004685 }
buzbee2e152ba2010-12-15 16:32:35 -08004686 if (!success)
4687 work->result.codeAddress = NULL;
4688 return isCompile;
Ben Chengba4fc8b2009-06-01 13:00:29 -07004689}
4690
Ben Chengba4fc8b2009-06-01 13:00:29 -07004691/* Architectural-specific debugging helpers go here */
4692void dvmCompilerArchDump(void)
4693{
4694 /* Print compiled opcode in this VM instance */
4695 int i, start, streak;
4696 char buf[1024];
4697
4698 streak = i = 0;
4699 buf[0] = 0;
Dan Bornsteinccaab182010-12-03 15:32:40 -08004700 while (opcodeCoverage[i] == 0 && i < kNumPackedOpcodes) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07004701 i++;
4702 }
Dan Bornsteinccaab182010-12-03 15:32:40 -08004703 if (i == kNumPackedOpcodes) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07004704 return;
4705 }
Dan Bornsteinccaab182010-12-03 15:32:40 -08004706 for (start = i++, streak = 1; i < kNumPackedOpcodes; i++) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07004707 if (opcodeCoverage[i]) {
4708 streak++;
4709 } else {
4710 if (streak == 1) {
4711 sprintf(buf+strlen(buf), "%x,", start);
4712 } else {
4713 sprintf(buf+strlen(buf), "%x-%x,", start, start + streak - 1);
4714 }
4715 streak = 0;
Dan Bornsteinccaab182010-12-03 15:32:40 -08004716 while (opcodeCoverage[i] == 0 && i < kNumPackedOpcodes) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07004717 i++;
4718 }
Dan Bornsteinccaab182010-12-03 15:32:40 -08004719 if (i < kNumPackedOpcodes) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07004720 streak = 1;
4721 start = i;
4722 }
4723 }
4724 }
4725 if (streak) {
4726 if (streak == 1) {
4727 sprintf(buf+strlen(buf), "%x", start);
4728 } else {
4729 sprintf(buf+strlen(buf), "%x-%x", start, start + streak - 1);
4730 }
4731 }
4732 if (strlen(buf)) {
Ben Cheng8b258bf2009-06-24 17:27:07 -07004733 LOGD("dalvik.vm.jit.op = %s", buf);
Ben Chengba4fc8b2009-06-01 13:00:29 -07004734 }
4735}
Ben Chengd7d426a2009-09-22 11:23:36 -07004736
4737/* Common initialization routine for an architecture family */
4738bool dvmCompilerArchInit()
4739{
4740 int i;
4741
Bill Buzbee1465db52009-09-23 17:17:35 -07004742 for (i = 0; i < kArmLast; i++) {
Dan Bornstein9a1f8162010-12-01 17:02:26 -08004743 if (EncodingMap[i].opcode != i) {
Ben Chengd7d426a2009-09-22 11:23:36 -07004744 LOGE("Encoding order for %s is wrong: expecting %d, seeing %d",
Dan Bornstein9a1f8162010-12-01 17:02:26 -08004745 EncodingMap[i].name, i, EncodingMap[i].opcode);
Bill Buzbeefc519dc2010-03-06 23:30:57 -08004746 dvmAbort(); // OK to dvmAbort - build error
Ben Chengd7d426a2009-09-22 11:23:36 -07004747 }
4748 }
4749
Ben Cheng5d90c202009-11-22 23:31:11 -08004750 return dvmCompilerArchVariantInit();
4751}
4752
4753void *dvmCompilerGetInterpretTemplate()
4754{
4755 return (void*) ((int)gDvmJit.codeCache +
4756 templateEntryOffsets[TEMPLATE_INTERPRET]);
4757}
4758
Bill Buzbee1b3da592011-02-03 07:38:22 -08004759JitInstructionSetType dvmCompilerGetInterpretTemplateSet()
4760{
4761 return DALVIK_JIT_ARM;
4762}
4763
buzbeebff121a2010-08-04 15:25:06 -07004764/* Needed by the Assembler */
4765void dvmCompilerSetupResourceMasks(ArmLIR *lir)
4766{
4767 setupResourceMasks(lir);
4768}
4769
Ben Cheng5d90c202009-11-22 23:31:11 -08004770/* Needed by the ld/st optmizatons */
4771ArmLIR* dvmCompilerRegCopyNoInsert(CompilationUnit *cUnit, int rDest, int rSrc)
4772{
4773 return genRegCopyNoInsert(cUnit, rDest, rSrc);
4774}
4775
4776/* Needed by the register allocator */
4777ArmLIR* dvmCompilerRegCopy(CompilationUnit *cUnit, int rDest, int rSrc)
4778{
4779 return genRegCopy(cUnit, rDest, rSrc);
4780}
4781
4782/* Needed by the register allocator */
4783void dvmCompilerRegCopyWide(CompilationUnit *cUnit, int destLo, int destHi,
4784 int srcLo, int srcHi)
4785{
4786 genRegCopyWide(cUnit, destLo, destHi, srcLo, srcHi);
4787}
4788
4789void dvmCompilerFlushRegImpl(CompilationUnit *cUnit, int rBase,
4790 int displacement, int rSrc, OpSize size)
4791{
4792 storeBaseDisp(cUnit, rBase, displacement, rSrc, size);
4793}
4794
4795void dvmCompilerFlushRegWideImpl(CompilationUnit *cUnit, int rBase,
4796 int displacement, int rSrcLo, int rSrcHi)
4797{
4798 storeBaseDispWide(cUnit, rBase, displacement, rSrcLo, rSrcHi);
Ben Chengd7d426a2009-09-22 11:23:36 -07004799}