blob: 465eade83cfe4e0c1c1ec1c58d78d1572b4a0550 [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) ||
1651 (opcode == OP_SGET_OBJECT_VOLATILE_JUMBO) ||
Carl Shapirofc75f3e2010-12-07 11:43:38 -08001652 dvmIsVolatileField((Field *) fieldPtr);
buzbeeecf8f6e2010-07-20 14:53:42 -07001653
Bill Buzbeec6f10662010-02-09 11:16:15 -08001654 rlDest = dvmCompilerGetDest(cUnit, mir, 0);
1655 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kAnyReg, true);
Bill Buzbee1465db52009-09-23 17:17:35 -07001656 loadConstant(cUnit, tReg, (int) fieldPtr + valOffset);
Ben Cheng11d8f142010-03-24 15:24:19 -07001657
buzbeeecf8f6e2010-07-20 14:53:42 -07001658 if (isVolatile) {
buzbee2ce33c92010-11-01 15:53:27 -07001659 dvmCompilerGenMemBarrier(cUnit, kSY);
buzbeeecf8f6e2010-07-20 14:53:42 -07001660 }
Ben Cheng11d8f142010-03-24 15:24:19 -07001661 HEAP_ACCESS_SHADOW(true);
Bill Buzbee1465db52009-09-23 17:17:35 -07001662 loadWordDisp(cUnit, tReg, 0, rlResult.lowReg);
Ben Cheng11d8f142010-03-24 15:24:19 -07001663 HEAP_ACCESS_SHADOW(false);
1664
Bill Buzbee1465db52009-09-23 17:17:35 -07001665 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001666 break;
1667 }
jeffhao71eee1f2011-01-04 14:18:54 -08001668 case OP_SGET_WIDE:
1669 case OP_SGET_WIDE_JUMBO: {
Bill Buzbee50a6bf22009-07-08 13:08:04 -07001670 int valOffset = offsetof(StaticField, value);
Ben Cheng7a2697d2010-06-07 13:44:23 -07001671 const Method *method = (mir->OptimizationFlags & MIR_CALLEE) ?
1672 mir->meta.calleeMethod : cUnit->method;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001673 void *fieldPtr = (void*)
Ben Cheng7a2697d2010-06-07 13:44:23 -07001674 (method->clazz->pDvmDex->pResFields[mir->dalvikInsn.vB]);
Ben Chengdd6e8702010-05-07 13:05:47 -07001675
1676 if (fieldPtr == NULL) {
Ben Cheng32115a92011-03-22 14:09:09 -07001677 BAIL_LOOP_COMPILATION();
Ben Chengdd6e8702010-05-07 13:05:47 -07001678 LOGE("Unexpected null static field");
1679 dvmAbort();
1680 }
1681
Bill Buzbeec6f10662010-02-09 11:16:15 -08001682 int tReg = dvmCompilerAllocTemp(cUnit);
Bill Buzbeec6f10662010-02-09 11:16:15 -08001683 rlDest = dvmCompilerGetDestWide(cUnit, mir, 0, 1);
1684 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kAnyReg, true);
Bill Buzbee1465db52009-09-23 17:17:35 -07001685 loadConstant(cUnit, tReg, (int) fieldPtr + valOffset);
Ben Cheng11d8f142010-03-24 15:24:19 -07001686
1687 HEAP_ACCESS_SHADOW(true);
Bill Buzbee1465db52009-09-23 17:17:35 -07001688 loadPair(cUnit, tReg, rlResult.lowReg, rlResult.highReg);
Ben Cheng11d8f142010-03-24 15:24:19 -07001689 HEAP_ACCESS_SHADOW(false);
1690
Bill Buzbee1465db52009-09-23 17:17:35 -07001691 storeValueWide(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001692 break;
1693 }
jeffhao71eee1f2011-01-04 14:18:54 -08001694 case OP_SPUT:
1695 case OP_SPUT_VOLATILE:
buzbeebd7865b2011-03-31 10:55:04 -07001696 case OP_SPUT_VOLATILE_JUMBO:
jeffhao71eee1f2011-01-04 14:18:54 -08001697 case OP_SPUT_JUMBO:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001698 case OP_SPUT_OBJECT:
buzbeeddc7d292010-09-02 17:16:24 -07001699 case OP_SPUT_OBJECT_VOLATILE:
buzbeebd7865b2011-03-31 10:55:04 -07001700 case OP_SPUT_OBJECT_VOLATILE_JUMBO:
jeffhao71eee1f2011-01-04 14:18:54 -08001701 case OP_SPUT_OBJECT_JUMBO:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001702 case OP_SPUT_BOOLEAN:
jeffhao71eee1f2011-01-04 14:18:54 -08001703 case OP_SPUT_BOOLEAN_JUMBO:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001704 case OP_SPUT_CHAR:
jeffhao71eee1f2011-01-04 14:18:54 -08001705 case OP_SPUT_CHAR_JUMBO:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001706 case OP_SPUT_BYTE:
jeffhao71eee1f2011-01-04 14:18:54 -08001707 case OP_SPUT_BYTE_JUMBO:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001708 case OP_SPUT_SHORT:
jeffhao71eee1f2011-01-04 14:18:54 -08001709 case OP_SPUT_SHORT_JUMBO: {
Bill Buzbee50a6bf22009-07-08 13:08:04 -07001710 int valOffset = offsetof(StaticField, value);
Bill Buzbeec6f10662010-02-09 11:16:15 -08001711 int tReg = dvmCompilerAllocTemp(cUnit);
buzbeed3b0a4b2010-09-27 11:30:22 -07001712 int objHead;
buzbeeecf8f6e2010-07-20 14:53:42 -07001713 bool isVolatile;
buzbeed3b0a4b2010-09-27 11:30:22 -07001714 bool isSputObject;
Ben Cheng7a2697d2010-06-07 13:44:23 -07001715 const Method *method = (mir->OptimizationFlags & MIR_CALLEE) ?
1716 mir->meta.calleeMethod : cUnit->method;
1717 void *fieldPtr = (void*)
1718 (method->clazz->pDvmDex->pResFields[mir->dalvikInsn.vB]);
buzbeebd7865b2011-03-31 10:55:04 -07001719 Opcode opcode = mir->dalvikInsn.opcode;
Ben Chenge9695e52009-06-16 16:11:47 -07001720
Ben Cheng32115a92011-03-22 14:09:09 -07001721 if (fieldPtr == NULL) {
1722 BAIL_LOOP_COMPILATION();
1723 LOGE("Unexpected null static field");
1724 dvmAbort();
1725 }
1726
buzbeebd7865b2011-03-31 10:55:04 -07001727 isVolatile = (opcode == OP_SPUT_VOLATILE) ||
1728 (opcode == OP_SPUT_VOLATILE_JUMBO) ||
1729 (opcode == OP_SPUT_OBJECT_VOLATILE) ||
1730 (opcode == OP_SPUT_OBJECT_VOLATILE_JUMBO) ||
Carl Shapirofc75f3e2010-12-07 11:43:38 -08001731 dvmIsVolatileField((Field *) fieldPtr);
buzbeeecf8f6e2010-07-20 14:53:42 -07001732
buzbeebd7865b2011-03-31 10:55:04 -07001733 isSputObject = (opcode == OP_SPUT_OBJECT) ||
1734 (opcode == OP_SPUT_OBJECT_JUMBO) ||
1735 (opcode == OP_SPUT_OBJECT_VOLATILE) ||
1736 (opcode == OP_SPUT_OBJECT_VOLATILE_JUMBO);
buzbeed3b0a4b2010-09-27 11:30:22 -07001737
Bill Buzbeec6f10662010-02-09 11:16:15 -08001738 rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07001739 rlSrc = loadValue(cUnit, rlSrc, kAnyReg);
buzbeeb78c76f2010-09-30 19:08:20 -07001740 loadConstant(cUnit, tReg, (int) fieldPtr);
buzbeed3b0a4b2010-09-27 11:30:22 -07001741 if (isSputObject) {
1742 objHead = dvmCompilerAllocTemp(cUnit);
buzbeeb78c76f2010-09-30 19:08:20 -07001743 loadWordDisp(cUnit, tReg, offsetof(Field, clazz), objHead);
buzbeed3b0a4b2010-09-27 11:30:22 -07001744 }
Ben Cheng11d8f142010-03-24 15:24:19 -07001745 HEAP_ACCESS_SHADOW(true);
buzbeeb78c76f2010-09-30 19:08:20 -07001746 storeWordDisp(cUnit, tReg, valOffset ,rlSrc.lowReg);
buzbeed3b0a4b2010-09-27 11:30:22 -07001747 dvmCompilerFreeTemp(cUnit, tReg);
Ben Cheng11d8f142010-03-24 15:24:19 -07001748 HEAP_ACCESS_SHADOW(false);
buzbeeecf8f6e2010-07-20 14:53:42 -07001749 if (isVolatile) {
buzbee2ce33c92010-11-01 15:53:27 -07001750 dvmCompilerGenMemBarrier(cUnit, kSY);
buzbeeecf8f6e2010-07-20 14:53:42 -07001751 }
buzbeed3b0a4b2010-09-27 11:30:22 -07001752 if (isSputObject) {
buzbeeb78c76f2010-09-30 19:08:20 -07001753 /* NOTE: marking card based sfield->clazz */
buzbeed3b0a4b2010-09-27 11:30:22 -07001754 markCard(cUnit, rlSrc.lowReg, objHead);
1755 dvmCompilerFreeTemp(cUnit, objHead);
buzbee919eb062010-07-12 12:59:22 -07001756 }
Ben Cheng11d8f142010-03-24 15:24:19 -07001757
Ben Chengba4fc8b2009-06-01 13:00:29 -07001758 break;
1759 }
jeffhao71eee1f2011-01-04 14:18:54 -08001760 case OP_SPUT_WIDE:
1761 case OP_SPUT_WIDE_JUMBO: {
Bill Buzbeec6f10662010-02-09 11:16:15 -08001762 int tReg = dvmCompilerAllocTemp(cUnit);
Bill Buzbee50a6bf22009-07-08 13:08:04 -07001763 int valOffset = offsetof(StaticField, value);
Ben Cheng7a2697d2010-06-07 13:44:23 -07001764 const Method *method = (mir->OptimizationFlags & MIR_CALLEE) ?
1765 mir->meta.calleeMethod : cUnit->method;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001766 void *fieldPtr = (void*)
Ben Cheng7a2697d2010-06-07 13:44:23 -07001767 (method->clazz->pDvmDex->pResFields[mir->dalvikInsn.vB]);
Ben Chenge9695e52009-06-16 16:11:47 -07001768
Ben Chengdd6e8702010-05-07 13:05:47 -07001769 if (fieldPtr == NULL) {
Ben Cheng32115a92011-03-22 14:09:09 -07001770 BAIL_LOOP_COMPILATION();
Ben Chengdd6e8702010-05-07 13:05:47 -07001771 LOGE("Unexpected null static field");
1772 dvmAbort();
1773 }
1774
Bill Buzbeec6f10662010-02-09 11:16:15 -08001775 rlSrc = dvmCompilerGetSrcWide(cUnit, mir, 0, 1);
Bill Buzbee1465db52009-09-23 17:17:35 -07001776 rlSrc = loadValueWide(cUnit, rlSrc, kAnyReg);
1777 loadConstant(cUnit, tReg, (int) fieldPtr + valOffset);
Ben Cheng11d8f142010-03-24 15:24:19 -07001778
1779 HEAP_ACCESS_SHADOW(true);
Bill Buzbee1465db52009-09-23 17:17:35 -07001780 storePair(cUnit, tReg, rlSrc.lowReg, rlSrc.highReg);
Ben Cheng11d8f142010-03-24 15:24:19 -07001781 HEAP_ACCESS_SHADOW(false);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001782 break;
1783 }
jeffhao71eee1f2011-01-04 14:18:54 -08001784 case OP_NEW_INSTANCE:
1785 case OP_NEW_INSTANCE_JUMBO: {
Ben Chenge9695e52009-06-16 16:11:47 -07001786 /*
1787 * Obey the calling convention and don't mess with the register
1788 * usage.
1789 */
Carl Shapirofc75f3e2010-12-07 11:43:38 -08001790 ClassObject *classPtr = (ClassObject *)
Ben Chengba4fc8b2009-06-01 13:00:29 -07001791 (cUnit->method->clazz->pDvmDex->pResClasses[mir->dalvikInsn.vB]);
Ben Chengdd6e8702010-05-07 13:05:47 -07001792
1793 if (classPtr == NULL) {
Ben Cheng32115a92011-03-22 14:09:09 -07001794 BAIL_LOOP_COMPILATION();
Ben Chengdd6e8702010-05-07 13:05:47 -07001795 LOGE("Unexpected null class");
1796 dvmAbort();
1797 }
1798
Ben Cheng79d173c2009-09-29 16:12:51 -07001799 /*
1800 * If it is going to throw, it should not make to the trace to begin
Bill Buzbee1465db52009-09-23 17:17:35 -07001801 * with. However, Alloc might throw, so we need to genExportPC()
Ben Cheng79d173c2009-09-29 16:12:51 -07001802 */
1803 assert((classPtr->accessFlags & (ACC_INTERFACE|ACC_ABSTRACT)) == 0);
Bill Buzbeec6f10662010-02-09 11:16:15 -08001804 dvmCompilerFlushAllRegs(cUnit); /* Everything to home location */
Bill Buzbee1465db52009-09-23 17:17:35 -07001805 genExportPC(cUnit, mir);
Ben Chengbd1326d2010-04-02 15:04:53 -07001806 LOAD_FUNC_ADDR(cUnit, r2, (int)dvmAllocObject);
Ben Chenge9695e52009-06-16 16:11:47 -07001807 loadConstant(cUnit, r0, (int) classPtr);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001808 loadConstant(cUnit, r1, ALLOC_DONT_TRACK);
Bill Buzbee1465db52009-09-23 17:17:35 -07001809 opReg(cUnit, kOpBlx, r2);
Elliott Hughes6a555132010-02-25 15:41:42 -08001810 dvmCompilerClobberCallRegs(cUnit);
Ben Cheng4f489172009-09-27 17:08:35 -07001811 /* generate a branch over if allocation is successful */
buzbee8f8109a2010-08-31 10:16:35 -07001812 ArmLIR *branchOver = genCmpImmBranch(cUnit, kArmCondNe, r0, 0);
Ben Cheng4f489172009-09-27 17:08:35 -07001813 /*
1814 * OOM exception needs to be thrown here and cannot re-execute
1815 */
1816 loadConstant(cUnit, r0,
1817 (int) (cUnit->method->insns + mir->offset));
1818 genDispatchToHandler(cUnit, TEMPLATE_THROW_EXCEPTION_COMMON);
1819 /* noreturn */
1820
Bill Buzbee1465db52009-09-23 17:17:35 -07001821 ArmLIR *target = newLIR0(cUnit, kArmPseudoTargetLabel);
Ben Cheng4f489172009-09-27 17:08:35 -07001822 target->defMask = ENCODE_ALL;
1823 branchOver->generic.target = (LIR *) target;
Bill Buzbeec6f10662010-02-09 11:16:15 -08001824 rlDest = dvmCompilerGetDest(cUnit, mir, 0);
1825 rlResult = dvmCompilerGetReturn(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07001826 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001827 break;
1828 }
jeffhao71eee1f2011-01-04 14:18:54 -08001829 case OP_CHECK_CAST:
1830 case OP_CHECK_CAST_JUMBO: {
Ben Chenge9695e52009-06-16 16:11:47 -07001831 /*
1832 * Obey the calling convention and don't mess with the register
1833 * usage.
1834 */
Ben Chengba4fc8b2009-06-01 13:00:29 -07001835 ClassObject *classPtr =
1836 (cUnit->method->clazz->pDvmDex->pResClasses[mir->dalvikInsn.vB]);
Bill Buzbee4df41a52009-11-12 17:07:16 -08001837 /*
1838 * Note: It is possible that classPtr is NULL at this point,
1839 * even though this instruction has been successfully interpreted.
1840 * If the previous interpretation had a null source, the
1841 * interpreter would not have bothered to resolve the clazz.
1842 * Bail out to the interpreter in this case, and log it
1843 * so that we can tell if it happens frequently.
1844 */
1845 if (classPtr == NULL) {
Ben Cheng32115a92011-03-22 14:09:09 -07001846 BAIL_LOOP_COMPILATION();
1847 LOGVV("null clazz in OP_CHECK_CAST, single-stepping");
1848 genInterpSingleStep(cUnit, mir);
1849 return false;
Bill Buzbee4df41a52009-11-12 17:07:16 -08001850 }
Bill Buzbeec6f10662010-02-09 11:16:15 -08001851 dvmCompilerFlushAllRegs(cUnit); /* Everything to home location */
Ben Chengba4fc8b2009-06-01 13:00:29 -07001852 loadConstant(cUnit, r1, (int) classPtr );
Bill Buzbeec6f10662010-02-09 11:16:15 -08001853 rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07001854 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
buzbee8f8109a2010-08-31 10:16:35 -07001855 /* Null? */
1856 ArmLIR *branch1 = genCmpImmBranch(cUnit, kArmCondEq,
1857 rlSrc.lowReg, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07001858 /*
1859 * rlSrc.lowReg now contains object->clazz. Note that
1860 * it could have been allocated r0, but we're okay so long
1861 * as we don't do anything desctructive until r0 is loaded
1862 * with clazz.
1863 */
Ben Chengba4fc8b2009-06-01 13:00:29 -07001864 /* r0 now contains object->clazz */
Bill Buzbee1465db52009-09-23 17:17:35 -07001865 loadWordDisp(cUnit, rlSrc.lowReg, offsetof(Object, clazz), r0);
Ben Chengbd1326d2010-04-02 15:04:53 -07001866 LOAD_FUNC_ADDR(cUnit, r2, (int)dvmInstanceofNonTrivial);
Bill Buzbee1465db52009-09-23 17:17:35 -07001867 opRegReg(cUnit, kOpCmp, r0, r1);
1868 ArmLIR *branch2 = opCondBranch(cUnit, kArmCondEq);
1869 opReg(cUnit, kOpBlx, r2);
Elliott Hughes6a555132010-02-25 15:41:42 -08001870 dvmCompilerClobberCallRegs(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07001871 /*
1872 * If null, check cast failed - punt to the interpreter. Because
1873 * interpreter will be the one throwing, we don't need to
1874 * genExportPC() here.
1875 */
Bill Buzbee270c1d62009-08-13 16:58:07 -07001876 genZeroCheck(cUnit, r0, mir->offset, NULL);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001877 /* check cast passed - branch target here */
Bill Buzbee1465db52009-09-23 17:17:35 -07001878 ArmLIR *target = newLIR0(cUnit, kArmPseudoTargetLabel);
Ben Chengd7d426a2009-09-22 11:23:36 -07001879 target->defMask = ENCODE_ALL;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001880 branch1->generic.target = (LIR *)target;
1881 branch2->generic.target = (LIR *)target;
1882 break;
1883 }
buzbee4d92e682010-07-29 15:24:14 -07001884 case OP_SGET_WIDE_VOLATILE:
buzbeebd7865b2011-03-31 10:55:04 -07001885 case OP_SGET_WIDE_VOLATILE_JUMBO:
buzbee4d92e682010-07-29 15:24:14 -07001886 case OP_SPUT_WIDE_VOLATILE:
buzbeebd7865b2011-03-31 10:55:04 -07001887 case OP_SPUT_WIDE_VOLATILE_JUMBO:
buzbee4d92e682010-07-29 15:24:14 -07001888 genInterpSingleStep(cUnit, mir);
1889 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001890 default:
1891 return true;
1892 }
1893 return false;
1894}
1895
Ben Cheng7a2697d2010-06-07 13:44:23 -07001896/*
1897 * A typical example of inlined getter/setter from a monomorphic callsite:
1898 *
1899 * D/dalvikvm( 289): -------- dalvik offset: 0x0000 @ invoke-static (I)
1900 * D/dalvikvm( 289): -------- dalvik offset: 0x0000 @ sget-object (C) v0, ...
1901 * D/dalvikvm( 289): 0x4427fc22 (0002): ldr r0, [pc, #56]
1902 * D/dalvikvm( 289): 0x4427fc24 (0004): ldr r1, [r0, #0]
1903 * D/dalvikvm( 289): 0x4427fc26 (0006): str r1, [r5, #0]
1904 * D/dalvikvm( 289): 0x4427fc28 (0008): .align4
1905 * D/dalvikvm( 289): L0x0003:
1906 * D/dalvikvm( 289): -------- dalvik offset: 0x0003 @ move-result-object (I) v0
1907 *
1908 * Note the invoke-static and move-result-object with the (I) notation are
1909 * turned into no-op.
1910 */
Ben Chengba4fc8b2009-06-01 13:00:29 -07001911static bool handleFmt11x(CompilationUnit *cUnit, MIR *mir)
1912{
Dan Bornstein9a1f8162010-12-01 17:02:26 -08001913 Opcode dalvikOpcode = mir->dalvikInsn.opcode;
Bill Buzbee1465db52009-09-23 17:17:35 -07001914 RegLocation rlResult;
Dan Bornstein9a1f8162010-12-01 17:02:26 -08001915 switch (dalvikOpcode) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07001916 case OP_MOVE_EXCEPTION: {
Ben Chengba4fc8b2009-06-01 13:00:29 -07001917 int exOffset = offsetof(Thread, exception);
Bill Buzbeec6f10662010-02-09 11:16:15 -08001918 int resetReg = dvmCompilerAllocTemp(cUnit);
1919 RegLocation rlDest = dvmCompilerGetDest(cUnit, mir, 0);
1920 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Ben Cheng20d7e6c2011-02-18 17:12:42 -08001921 loadWordDisp(cUnit, r6SELF, exOffset, rlResult.lowReg);
Bill Buzbeef9f33282009-11-22 12:45:30 -08001922 loadConstant(cUnit, resetReg, 0);
Ben Cheng20d7e6c2011-02-18 17:12:42 -08001923 storeWordDisp(cUnit, r6SELF, exOffset, resetReg);
Bill Buzbee1465db52009-09-23 17:17:35 -07001924 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001925 break;
1926 }
1927 case OP_MOVE_RESULT:
1928 case OP_MOVE_RESULT_OBJECT: {
Ben Cheng7a2697d2010-06-07 13:44:23 -07001929 /* An inlined move result is effectively no-op */
1930 if (mir->OptimizationFlags & MIR_INLINED)
1931 break;
Bill Buzbeec6f10662010-02-09 11:16:15 -08001932 RegLocation rlDest = dvmCompilerGetDest(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07001933 RegLocation rlSrc = LOC_DALVIK_RETURN_VAL;
1934 rlSrc.fp = rlDest.fp;
1935 storeValue(cUnit, rlDest, rlSrc);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001936 break;
1937 }
1938 case OP_MOVE_RESULT_WIDE: {
Ben Cheng7a2697d2010-06-07 13:44:23 -07001939 /* An inlined move result is effectively no-op */
1940 if (mir->OptimizationFlags & MIR_INLINED)
1941 break;
Bill Buzbeec6f10662010-02-09 11:16:15 -08001942 RegLocation rlDest = dvmCompilerGetDestWide(cUnit, mir, 0, 1);
Bill Buzbee1465db52009-09-23 17:17:35 -07001943 RegLocation rlSrc = LOC_DALVIK_RETURN_VAL_WIDE;
1944 rlSrc.fp = rlDest.fp;
1945 storeValueWide(cUnit, rlDest, rlSrc);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001946 break;
1947 }
1948 case OP_RETURN_WIDE: {
Bill Buzbeec6f10662010-02-09 11:16:15 -08001949 RegLocation rlSrc = dvmCompilerGetSrcWide(cUnit, mir, 0, 1);
Bill Buzbee1465db52009-09-23 17:17:35 -07001950 RegLocation rlDest = LOC_DALVIK_RETURN_VAL_WIDE;
1951 rlDest.fp = rlSrc.fp;
1952 storeValueWide(cUnit, rlDest, rlSrc);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001953 genReturnCommon(cUnit,mir);
1954 break;
1955 }
1956 case OP_RETURN:
1957 case OP_RETURN_OBJECT: {
Bill Buzbeec6f10662010-02-09 11:16:15 -08001958 RegLocation rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07001959 RegLocation rlDest = LOC_DALVIK_RETURN_VAL;
1960 rlDest.fp = rlSrc.fp;
1961 storeValue(cUnit, rlDest, rlSrc);
Ben Cheng20d7e6c2011-02-18 17:12:42 -08001962 genReturnCommon(cUnit, mir);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001963 break;
1964 }
Bill Buzbee1465db52009-09-23 17:17:35 -07001965 case OP_MONITOR_EXIT:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001966 case OP_MONITOR_ENTER:
Ben Cheng5d90c202009-11-22 23:31:11 -08001967 genMonitor(cUnit, mir);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001968 break;
Ben Cheng20d7e6c2011-02-18 17:12:42 -08001969 case OP_THROW:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001970 genInterpSingleStep(cUnit, mir);
1971 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001972 default:
1973 return true;
1974 }
1975 return false;
1976}
1977
Bill Buzbeed45ba372009-06-15 17:00:57 -07001978static bool handleFmt12x(CompilationUnit *cUnit, MIR *mir)
1979{
Dan Bornstein9a1f8162010-12-01 17:02:26 -08001980 Opcode opcode = mir->dalvikInsn.opcode;
Bill Buzbee1465db52009-09-23 17:17:35 -07001981 RegLocation rlDest;
1982 RegLocation rlSrc;
1983 RegLocation rlResult;
Bill Buzbeed45ba372009-06-15 17:00:57 -07001984
Dan Bornstein9a1f8162010-12-01 17:02:26 -08001985 if ( (opcode >= OP_ADD_INT_2ADDR) && (opcode <= OP_REM_DOUBLE_2ADDR)) {
Ben Cheng5d90c202009-11-22 23:31:11 -08001986 return genArithOp( cUnit, mir );
Ben Chengba4fc8b2009-06-01 13:00:29 -07001987 }
1988
Bill Buzbee1465db52009-09-23 17:17:35 -07001989 if (mir->ssaRep->numUses == 2)
Bill Buzbeec6f10662010-02-09 11:16:15 -08001990 rlSrc = dvmCompilerGetSrcWide(cUnit, mir, 0, 1);
Bill Buzbee1465db52009-09-23 17:17:35 -07001991 else
Bill Buzbeec6f10662010-02-09 11:16:15 -08001992 rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07001993 if (mir->ssaRep->numDefs == 2)
Bill Buzbeec6f10662010-02-09 11:16:15 -08001994 rlDest = dvmCompilerGetDestWide(cUnit, mir, 0, 1);
Bill Buzbee1465db52009-09-23 17:17:35 -07001995 else
Bill Buzbeec6f10662010-02-09 11:16:15 -08001996 rlDest = dvmCompilerGetDest(cUnit, mir, 0);
Ben Chenge9695e52009-06-16 16:11:47 -07001997
Dan Bornstein9a1f8162010-12-01 17:02:26 -08001998 switch (opcode) {
Bill Buzbee1465db52009-09-23 17:17:35 -07001999 case OP_DOUBLE_TO_INT:
Ben Chengba4fc8b2009-06-01 13:00:29 -07002000 case OP_INT_TO_FLOAT:
Ben Chengba4fc8b2009-06-01 13:00:29 -07002001 case OP_FLOAT_TO_INT:
Ben Chengba4fc8b2009-06-01 13:00:29 -07002002 case OP_DOUBLE_TO_FLOAT:
Ben Chengba4fc8b2009-06-01 13:00:29 -07002003 case OP_FLOAT_TO_DOUBLE:
Ben Chengba4fc8b2009-06-01 13:00:29 -07002004 case OP_INT_TO_DOUBLE:
Ben Chengba4fc8b2009-06-01 13:00:29 -07002005 case OP_FLOAT_TO_LONG:
Ben Chengba4fc8b2009-06-01 13:00:29 -07002006 case OP_LONG_TO_FLOAT:
Ben Chengba4fc8b2009-06-01 13:00:29 -07002007 case OP_DOUBLE_TO_LONG:
Ben Chengba4fc8b2009-06-01 13:00:29 -07002008 case OP_LONG_TO_DOUBLE:
Ben Cheng5d90c202009-11-22 23:31:11 -08002009 return genConversion(cUnit, mir);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002010 case OP_NEG_INT:
2011 case OP_NOT_INT:
Ben Cheng5d90c202009-11-22 23:31:11 -08002012 return genArithOpInt(cUnit, mir, rlDest, rlSrc, rlSrc);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002013 case OP_NEG_LONG:
2014 case OP_NOT_LONG:
Ben Cheng5d90c202009-11-22 23:31:11 -08002015 return genArithOpLong(cUnit, mir, rlDest, rlSrc, rlSrc);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002016 case OP_NEG_FLOAT:
Ben Cheng5d90c202009-11-22 23:31:11 -08002017 return genArithOpFloat(cUnit, mir, rlDest, rlSrc, rlSrc);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002018 case OP_NEG_DOUBLE:
Ben Cheng5d90c202009-11-22 23:31:11 -08002019 return genArithOpDouble(cUnit, mir, rlDest, rlSrc, rlSrc);
Bill Buzbee1465db52009-09-23 17:17:35 -07002020 case OP_MOVE_WIDE:
2021 storeValueWide(cUnit, rlDest, rlSrc);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002022 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07002023 case OP_INT_TO_LONG:
Bill Buzbeec6f10662010-02-09 11:16:15 -08002024 rlSrc = dvmCompilerUpdateLoc(cUnit, rlSrc);
2025 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Bill Buzbee964a7b02010-01-28 12:54:19 -08002026 //TUNING: shouldn't loadValueDirect already check for phys reg?
Bill Buzbee1465db52009-09-23 17:17:35 -07002027 if (rlSrc.location == kLocPhysReg) {
2028 genRegCopy(cUnit, rlResult.lowReg, rlSrc.lowReg);
2029 } else {
2030 loadValueDirect(cUnit, rlSrc, rlResult.lowReg);
2031 }
2032 opRegRegImm(cUnit, kOpAsr, rlResult.highReg,
2033 rlResult.lowReg, 31);
2034 storeValueWide(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002035 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07002036 case OP_LONG_TO_INT:
Bill Buzbeec6f10662010-02-09 11:16:15 -08002037 rlSrc = dvmCompilerUpdateLocWide(cUnit, rlSrc);
2038 rlSrc = dvmCompilerWideToNarrow(cUnit, rlSrc);
Bill Buzbee1465db52009-09-23 17:17:35 -07002039 // Intentional fallthrough
Ben Chengba4fc8b2009-06-01 13:00:29 -07002040 case OP_MOVE:
2041 case OP_MOVE_OBJECT:
Bill Buzbee1465db52009-09-23 17:17:35 -07002042 storeValue(cUnit, rlDest, rlSrc);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002043 break;
2044 case OP_INT_TO_BYTE:
Bill Buzbee1465db52009-09-23 17:17:35 -07002045 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
Bill Buzbeec6f10662010-02-09 11:16:15 -08002046 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Bill Buzbee1465db52009-09-23 17:17:35 -07002047 opRegReg(cUnit, kOp2Byte, rlResult.lowReg, rlSrc.lowReg);
2048 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002049 break;
2050 case OP_INT_TO_SHORT:
Bill Buzbee1465db52009-09-23 17:17:35 -07002051 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
Bill Buzbeec6f10662010-02-09 11:16:15 -08002052 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Bill Buzbee1465db52009-09-23 17:17:35 -07002053 opRegReg(cUnit, kOp2Short, rlResult.lowReg, rlSrc.lowReg);
2054 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002055 break;
2056 case OP_INT_TO_CHAR:
Bill Buzbee1465db52009-09-23 17:17:35 -07002057 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
Bill Buzbeec6f10662010-02-09 11:16:15 -08002058 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Bill Buzbee1465db52009-09-23 17:17:35 -07002059 opRegReg(cUnit, kOp2Char, rlResult.lowReg, rlSrc.lowReg);
2060 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002061 break;
2062 case OP_ARRAY_LENGTH: {
2063 int lenOffset = offsetof(ArrayObject, length);
Bill Buzbee1465db52009-09-23 17:17:35 -07002064 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
2065 genNullCheck(cUnit, rlSrc.sRegLow, rlSrc.lowReg,
2066 mir->offset, NULL);
Bill Buzbeec6f10662010-02-09 11:16:15 -08002067 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Bill Buzbee1465db52009-09-23 17:17:35 -07002068 loadWordDisp(cUnit, rlSrc.lowReg, lenOffset,
2069 rlResult.lowReg);
2070 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002071 break;
2072 }
2073 default:
2074 return true;
2075 }
2076 return false;
2077}
2078
2079static bool handleFmt21s(CompilationUnit *cUnit, MIR *mir)
2080{
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002081 Opcode dalvikOpcode = mir->dalvikInsn.opcode;
Bill Buzbee1465db52009-09-23 17:17:35 -07002082 RegLocation rlDest;
2083 RegLocation rlResult;
2084 int BBBB = mir->dalvikInsn.vB;
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002085 if (dalvikOpcode == OP_CONST_WIDE_16) {
Bill Buzbeec6f10662010-02-09 11:16:15 -08002086 rlDest = dvmCompilerGetDestWide(cUnit, mir, 0, 1);
2087 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Ben Chengbd1326d2010-04-02 15:04:53 -07002088 loadConstantNoClobber(cUnit, rlResult.lowReg, BBBB);
Bill Buzbee964a7b02010-01-28 12:54:19 -08002089 //TUNING: do high separately to avoid load dependency
Bill Buzbee1465db52009-09-23 17:17:35 -07002090 opRegRegImm(cUnit, kOpAsr, rlResult.highReg, rlResult.lowReg, 31);
2091 storeValueWide(cUnit, rlDest, rlResult);
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002092 } else if (dalvikOpcode == OP_CONST_16) {
Bill Buzbeec6f10662010-02-09 11:16:15 -08002093 rlDest = dvmCompilerGetDest(cUnit, mir, 0);
2094 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kAnyReg, true);
Ben Chengbd1326d2010-04-02 15:04:53 -07002095 loadConstantNoClobber(cUnit, rlResult.lowReg, BBBB);
Bill Buzbee1465db52009-09-23 17:17:35 -07002096 storeValue(cUnit, rlDest, rlResult);
2097 } else
Ben Chengba4fc8b2009-06-01 13:00:29 -07002098 return true;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002099 return false;
2100}
2101
2102/* Compare agaist zero */
2103static bool handleFmt21t(CompilationUnit *cUnit, MIR *mir, BasicBlock *bb,
Bill Buzbee89efc3d2009-07-28 11:22:22 -07002104 ArmLIR *labelList)
Ben Chengba4fc8b2009-06-01 13:00:29 -07002105{
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002106 Opcode dalvikOpcode = mir->dalvikInsn.opcode;
Bill Buzbee89efc3d2009-07-28 11:22:22 -07002107 ArmConditionCode cond;
Ben Cheng7ab74e12011-02-03 14:02:06 -08002108 /* backward branch? */
2109 bool backwardBranch = (bb->taken->startOffset <= mir->offset);
2110
Ben Cheng32115a92011-03-22 14:09:09 -07002111 if (backwardBranch &&
2112 (gDvmJit.genSuspendPoll || cUnit->jitMode == kJitLoop)) {
Ben Cheng7ab74e12011-02-03 14:02:06 -08002113 genSuspendPoll(cUnit, mir);
2114 }
2115
Bill Buzbeec6f10662010-02-09 11:16:15 -08002116 RegLocation rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07002117 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
Ben Cheng7ab74e12011-02-03 14:02:06 -08002118
Bill Buzbee1465db52009-09-23 17:17:35 -07002119 opRegImm(cUnit, kOpCmp, rlSrc.lowReg, 0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002120
Bill Buzbee270c1d62009-08-13 16:58:07 -07002121//TUNING: break this out to allow use of Thumb2 CB[N]Z
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002122 switch (dalvikOpcode) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07002123 case OP_IF_EQZ:
Bill Buzbee1465db52009-09-23 17:17:35 -07002124 cond = kArmCondEq;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002125 break;
2126 case OP_IF_NEZ:
Bill Buzbee1465db52009-09-23 17:17:35 -07002127 cond = kArmCondNe;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002128 break;
2129 case OP_IF_LTZ:
Bill Buzbee1465db52009-09-23 17:17:35 -07002130 cond = kArmCondLt;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002131 break;
2132 case OP_IF_GEZ:
Bill Buzbee1465db52009-09-23 17:17:35 -07002133 cond = kArmCondGe;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002134 break;
2135 case OP_IF_GTZ:
Bill Buzbee1465db52009-09-23 17:17:35 -07002136 cond = kArmCondGt;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002137 break;
2138 case OP_IF_LEZ:
Bill Buzbee1465db52009-09-23 17:17:35 -07002139 cond = kArmCondLe;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002140 break;
2141 default:
2142 cond = 0;
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002143 LOGE("Unexpected opcode (%d) for Fmt21t\n", dalvikOpcode);
Bill Buzbeefc519dc2010-03-06 23:30:57 -08002144 dvmCompilerAbort(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002145 }
2146 genConditionalBranch(cUnit, cond, &labelList[bb->taken->id]);
2147 /* This mostly likely will be optimized away in a later phase */
2148 genUnconditionalBranch(cUnit, &labelList[bb->fallThrough->id]);
2149 return false;
2150}
2151
Elliott Hughesb4c05972010-02-24 16:36:18 -08002152static bool isPowerOfTwo(int x)
2153{
2154 return (x & (x - 1)) == 0;
2155}
2156
2157// Returns true if no more than two bits are set in 'x'.
2158static bool isPopCountLE2(unsigned int x)
2159{
2160 x &= x - 1;
2161 return (x & (x - 1)) == 0;
2162}
2163
2164// Returns the index of the lowest set bit in 'x'.
2165static int lowestSetBit(unsigned int x) {
2166 int bit_posn = 0;
2167 while ((x & 0xf) == 0) {
2168 bit_posn += 4;
2169 x >>= 4;
Bill Buzbee78cb0e22010-02-11 14:04:53 -08002170 }
Elliott Hughesb4c05972010-02-24 16:36:18 -08002171 while ((x & 1) == 0) {
2172 bit_posn++;
2173 x >>= 1;
2174 }
2175 return bit_posn;
2176}
2177
Elliott Hughes672511b2010-04-26 17:40:13 -07002178// Returns true if it added instructions to 'cUnit' to divide 'rlSrc' by 'lit'
2179// and store the result in 'rlDest'.
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002180static bool handleEasyDivide(CompilationUnit *cUnit, Opcode dalvikOpcode,
Elliott Hughes672511b2010-04-26 17:40:13 -07002181 RegLocation rlSrc, RegLocation rlDest, int lit)
2182{
2183 if (lit < 2 || !isPowerOfTwo(lit)) {
2184 return false;
2185 }
2186 int k = lowestSetBit(lit);
2187 if (k >= 30) {
2188 // Avoid special cases.
2189 return false;
2190 }
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002191 bool div = (dalvikOpcode == OP_DIV_INT_LIT8 || dalvikOpcode == OP_DIV_INT_LIT16);
Elliott Hughes672511b2010-04-26 17:40:13 -07002192 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
2193 RegLocation rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Elliott Hughes9c457022010-04-28 16:15:38 -07002194 if (div) {
2195 int tReg = dvmCompilerAllocTemp(cUnit);
2196 if (lit == 2) {
2197 // Division by 2 is by far the most common division by constant.
2198 opRegRegImm(cUnit, kOpLsr, tReg, rlSrc.lowReg, 32 - k);
2199 opRegRegReg(cUnit, kOpAdd, tReg, tReg, rlSrc.lowReg);
2200 opRegRegImm(cUnit, kOpAsr, rlResult.lowReg, tReg, k);
2201 } else {
2202 opRegRegImm(cUnit, kOpAsr, tReg, rlSrc.lowReg, 31);
2203 opRegRegImm(cUnit, kOpLsr, tReg, tReg, 32 - k);
2204 opRegRegReg(cUnit, kOpAdd, tReg, tReg, rlSrc.lowReg);
2205 opRegRegImm(cUnit, kOpAsr, rlResult.lowReg, tReg, k);
2206 }
Elliott Hughes672511b2010-04-26 17:40:13 -07002207 } else {
Elliott Hughes9c457022010-04-28 16:15:38 -07002208 int cReg = dvmCompilerAllocTemp(cUnit);
2209 loadConstant(cUnit, cReg, lit - 1);
2210 int tReg1 = dvmCompilerAllocTemp(cUnit);
2211 int tReg2 = dvmCompilerAllocTemp(cUnit);
2212 if (lit == 2) {
2213 opRegRegImm(cUnit, kOpLsr, tReg1, rlSrc.lowReg, 32 - k);
2214 opRegRegReg(cUnit, kOpAdd, tReg2, tReg1, rlSrc.lowReg);
2215 opRegRegReg(cUnit, kOpAnd, tReg2, tReg2, cReg);
2216 opRegRegReg(cUnit, kOpSub, rlResult.lowReg, tReg2, tReg1);
2217 } else {
2218 opRegRegImm(cUnit, kOpAsr, tReg1, rlSrc.lowReg, 31);
2219 opRegRegImm(cUnit, kOpLsr, tReg1, tReg1, 32 - k);
2220 opRegRegReg(cUnit, kOpAdd, tReg2, tReg1, rlSrc.lowReg);
2221 opRegRegReg(cUnit, kOpAnd, tReg2, tReg2, cReg);
2222 opRegRegReg(cUnit, kOpSub, rlResult.lowReg, tReg2, tReg1);
2223 }
Elliott Hughes672511b2010-04-26 17:40:13 -07002224 }
2225 storeValue(cUnit, rlDest, rlResult);
2226 return true;
2227}
2228
Elliott Hughesb4c05972010-02-24 16:36:18 -08002229// Returns true if it added instructions to 'cUnit' to multiply 'rlSrc' by 'lit'
2230// and store the result in 'rlDest'.
2231static bool handleEasyMultiply(CompilationUnit *cUnit,
2232 RegLocation rlSrc, RegLocation rlDest, int lit)
2233{
2234 // Can we simplify this multiplication?
2235 bool powerOfTwo = false;
2236 bool popCountLE2 = false;
2237 bool powerOfTwoMinusOne = false;
2238 if (lit < 2) {
2239 // Avoid special cases.
2240 return false;
2241 } else if (isPowerOfTwo(lit)) {
2242 powerOfTwo = true;
2243 } else if (isPopCountLE2(lit)) {
2244 popCountLE2 = true;
2245 } else if (isPowerOfTwo(lit + 1)) {
2246 powerOfTwoMinusOne = true;
2247 } else {
2248 return false;
2249 }
2250 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
2251 RegLocation rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
2252 if (powerOfTwo) {
2253 // Shift.
2254 opRegRegImm(cUnit, kOpLsl, rlResult.lowReg, rlSrc.lowReg,
2255 lowestSetBit(lit));
2256 } else if (popCountLE2) {
2257 // Shift and add and shift.
2258 int firstBit = lowestSetBit(lit);
2259 int secondBit = lowestSetBit(lit ^ (1 << firstBit));
2260 genMultiplyByTwoBitMultiplier(cUnit, rlSrc, rlResult, lit,
2261 firstBit, secondBit);
2262 } else {
2263 // Reverse subtract: (src << (shift + 1)) - src.
2264 assert(powerOfTwoMinusOne);
2265 // TODO: rsb dst, src, src lsl#lowestSetBit(lit + 1)
2266 int tReg = dvmCompilerAllocTemp(cUnit);
2267 opRegRegImm(cUnit, kOpLsl, tReg, rlSrc.lowReg, lowestSetBit(lit + 1));
2268 opRegRegReg(cUnit, kOpSub, rlResult.lowReg, tReg, rlSrc.lowReg);
2269 }
2270 storeValue(cUnit, rlDest, rlResult);
2271 return true;
Bill Buzbee78cb0e22010-02-11 14:04:53 -08002272}
2273
Ben Chengba4fc8b2009-06-01 13:00:29 -07002274static bool handleFmt22b_Fmt22s(CompilationUnit *cUnit, MIR *mir)
2275{
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002276 Opcode dalvikOpcode = mir->dalvikInsn.opcode;
Bill Buzbeec6f10662010-02-09 11:16:15 -08002277 RegLocation rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
2278 RegLocation rlDest = dvmCompilerGetDest(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07002279 RegLocation rlResult;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002280 int lit = mir->dalvikInsn.vC;
Ben Cheng4f489172009-09-27 17:08:35 -07002281 OpKind op = 0; /* Make gcc happy */
Bill Buzbee1465db52009-09-23 17:17:35 -07002282 int shiftOp = false;
2283 bool isDiv = false;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002284
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002285 switch (dalvikOpcode) {
Bill Buzbee1465db52009-09-23 17:17:35 -07002286 case OP_RSUB_INT_LIT8:
2287 case OP_RSUB_INT: {
2288 int tReg;
2289 //TUNING: add support for use of Arm rsub op
2290 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
Bill Buzbeec6f10662010-02-09 11:16:15 -08002291 tReg = dvmCompilerAllocTemp(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07002292 loadConstant(cUnit, tReg, lit);
Bill Buzbeec6f10662010-02-09 11:16:15 -08002293 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Bill Buzbee1465db52009-09-23 17:17:35 -07002294 opRegRegReg(cUnit, kOpSub, rlResult.lowReg,
2295 tReg, rlSrc.lowReg);
2296 storeValue(cUnit, rlDest, rlResult);
2297 return false;
2298 break;
2299 }
2300
Ben Chengba4fc8b2009-06-01 13:00:29 -07002301 case OP_ADD_INT_LIT8:
2302 case OP_ADD_INT_LIT16:
Bill Buzbee1465db52009-09-23 17:17:35 -07002303 op = kOpAdd;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002304 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002305 case OP_MUL_INT_LIT8:
Bill Buzbee78cb0e22010-02-11 14:04:53 -08002306 case OP_MUL_INT_LIT16: {
Elliott Hughesb4c05972010-02-24 16:36:18 -08002307 if (handleEasyMultiply(cUnit, rlSrc, rlDest, lit)) {
2308 return false;
Bill Buzbee78cb0e22010-02-11 14:04:53 -08002309 }
Elliott Hughesb4c05972010-02-24 16:36:18 -08002310 op = kOpMul;
Bill Buzbee1465db52009-09-23 17:17:35 -07002311 break;
Bill Buzbee78cb0e22010-02-11 14:04:53 -08002312 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07002313 case OP_AND_INT_LIT8:
2314 case OP_AND_INT_LIT16:
Bill Buzbee1465db52009-09-23 17:17:35 -07002315 op = kOpAnd;
2316 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002317 case OP_OR_INT_LIT8:
2318 case OP_OR_INT_LIT16:
Bill Buzbee1465db52009-09-23 17:17:35 -07002319 op = kOpOr;
2320 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002321 case OP_XOR_INT_LIT8:
2322 case OP_XOR_INT_LIT16:
Bill Buzbee1465db52009-09-23 17:17:35 -07002323 op = kOpXor;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002324 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002325 case OP_SHL_INT_LIT8:
Bill Buzbee0e605272009-12-01 14:28:05 -08002326 lit &= 31;
Bill Buzbee1465db52009-09-23 17:17:35 -07002327 shiftOp = true;
2328 op = kOpLsl;
2329 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002330 case OP_SHR_INT_LIT8:
Bill Buzbee0e605272009-12-01 14:28:05 -08002331 lit &= 31;
Bill Buzbee1465db52009-09-23 17:17:35 -07002332 shiftOp = true;
2333 op = kOpAsr;
2334 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002335 case OP_USHR_INT_LIT8:
Bill Buzbee0e605272009-12-01 14:28:05 -08002336 lit &= 31;
Bill Buzbee1465db52009-09-23 17:17:35 -07002337 shiftOp = true;
2338 op = kOpLsr;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002339 break;
2340
2341 case OP_DIV_INT_LIT8:
2342 case OP_DIV_INT_LIT16:
Ben Chengba4fc8b2009-06-01 13:00:29 -07002343 case OP_REM_INT_LIT8:
2344 case OP_REM_INT_LIT16:
2345 if (lit == 0) {
2346 /* Let the interpreter deal with div by 0 */
2347 genInterpSingleStep(cUnit, mir);
2348 return false;
2349 }
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002350 if (handleEasyDivide(cUnit, dalvikOpcode, rlSrc, rlDest, lit)) {
Elliott Hughes672511b2010-04-26 17:40:13 -07002351 return false;
2352 }
Bill Buzbeec6f10662010-02-09 11:16:15 -08002353 dvmCompilerFlushAllRegs(cUnit); /* Everything to home location */
Bill Buzbee1465db52009-09-23 17:17:35 -07002354 loadValueDirectFixed(cUnit, rlSrc, r0);
Bill Buzbeec6f10662010-02-09 11:16:15 -08002355 dvmCompilerClobber(cUnit, r0);
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002356 if ((dalvikOpcode == OP_DIV_INT_LIT8) ||
2357 (dalvikOpcode == OP_DIV_INT_LIT16)) {
Ben Chengbd1326d2010-04-02 15:04:53 -07002358 LOAD_FUNC_ADDR(cUnit, r2, (int)__aeabi_idiv);
Bill Buzbee1465db52009-09-23 17:17:35 -07002359 isDiv = true;
2360 } else {
Ben Chengbd1326d2010-04-02 15:04:53 -07002361 LOAD_FUNC_ADDR(cUnit, r2, (int)__aeabi_idivmod);
Bill Buzbee1465db52009-09-23 17:17:35 -07002362 isDiv = false;
2363 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07002364 loadConstant(cUnit, r1, lit);
Bill Buzbee1465db52009-09-23 17:17:35 -07002365 opReg(cUnit, kOpBlx, r2);
Elliott Hughes6a555132010-02-25 15:41:42 -08002366 dvmCompilerClobberCallRegs(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07002367 if (isDiv)
Bill Buzbeec6f10662010-02-09 11:16:15 -08002368 rlResult = dvmCompilerGetReturn(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07002369 else
Bill Buzbeec6f10662010-02-09 11:16:15 -08002370 rlResult = dvmCompilerGetReturnAlt(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07002371 storeValue(cUnit, rlDest, rlResult);
2372 return false;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002373 break;
2374 default:
2375 return true;
2376 }
Bill Buzbee1465db52009-09-23 17:17:35 -07002377 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
Bill Buzbeec6f10662010-02-09 11:16:15 -08002378 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Bill Buzbee1465db52009-09-23 17:17:35 -07002379 // Avoid shifts by literal 0 - no support in Thumb. Change to copy
2380 if (shiftOp && (lit == 0)) {
2381 genRegCopy(cUnit, rlResult.lowReg, rlSrc.lowReg);
2382 } else {
2383 opRegRegImm(cUnit, op, rlResult.lowReg, rlSrc.lowReg, lit);
2384 }
2385 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002386 return false;
2387}
2388
jeffhao71eee1f2011-01-04 14:18:54 -08002389static bool handleFmt22c_Fmt52c(CompilationUnit *cUnit, MIR *mir)
Ben Chengba4fc8b2009-06-01 13:00:29 -07002390{
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002391 Opcode dalvikOpcode = mir->dalvikInsn.opcode;
buzbee4d92e682010-07-29 15:24:14 -07002392 int fieldOffset = -1;
buzbeeecf8f6e2010-07-20 14:53:42 -07002393 bool isVolatile = false;
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002394 switch (dalvikOpcode) {
buzbee4d92e682010-07-29 15:24:14 -07002395 /*
2396 * Wide volatiles currently handled via single step.
2397 * Add them here if generating in-line code.
2398 * case OP_IGET_WIDE_VOLATILE:
buzbeebd7865b2011-03-31 10:55:04 -07002399 * case OP_IGET_WIDE_VOLATILE_JUMBO:
buzbee4d92e682010-07-29 15:24:14 -07002400 * case OP_IPUT_WIDE_VOLATILE:
buzbeebd7865b2011-03-31 10:55:04 -07002401 * case OP_IPUT_WIDE_VOLATILE_JUMBO:
buzbee4d92e682010-07-29 15:24:14 -07002402 */
2403 case OP_IGET:
2404 case OP_IGET_VOLATILE:
buzbeebd7865b2011-03-31 10:55:04 -07002405 case OP_IGET_VOLATILE_JUMBO:
jeffhao71eee1f2011-01-04 14:18:54 -08002406 case OP_IGET_JUMBO:
buzbee4d92e682010-07-29 15:24:14 -07002407 case OP_IGET_WIDE:
jeffhao71eee1f2011-01-04 14:18:54 -08002408 case OP_IGET_WIDE_JUMBO:
buzbee4d92e682010-07-29 15:24:14 -07002409 case OP_IGET_OBJECT:
2410 case OP_IGET_OBJECT_VOLATILE:
buzbeebd7865b2011-03-31 10:55:04 -07002411 case OP_IGET_OBJECT_VOLATILE_JUMBO:
jeffhao71eee1f2011-01-04 14:18:54 -08002412 case OP_IGET_OBJECT_JUMBO:
buzbee4d92e682010-07-29 15:24:14 -07002413 case OP_IGET_BOOLEAN:
jeffhao71eee1f2011-01-04 14:18:54 -08002414 case OP_IGET_BOOLEAN_JUMBO:
buzbee4d92e682010-07-29 15:24:14 -07002415 case OP_IGET_BYTE:
jeffhao71eee1f2011-01-04 14:18:54 -08002416 case OP_IGET_BYTE_JUMBO:
buzbee4d92e682010-07-29 15:24:14 -07002417 case OP_IGET_CHAR:
jeffhao71eee1f2011-01-04 14:18:54 -08002418 case OP_IGET_CHAR_JUMBO:
buzbee4d92e682010-07-29 15:24:14 -07002419 case OP_IGET_SHORT:
jeffhao71eee1f2011-01-04 14:18:54 -08002420 case OP_IGET_SHORT_JUMBO:
buzbee4d92e682010-07-29 15:24:14 -07002421 case OP_IPUT:
2422 case OP_IPUT_VOLATILE:
buzbeebd7865b2011-03-31 10:55:04 -07002423 case OP_IPUT_VOLATILE_JUMBO:
jeffhao71eee1f2011-01-04 14:18:54 -08002424 case OP_IPUT_JUMBO:
buzbee4d92e682010-07-29 15:24:14 -07002425 case OP_IPUT_WIDE:
jeffhao71eee1f2011-01-04 14:18:54 -08002426 case OP_IPUT_WIDE_JUMBO:
buzbee4d92e682010-07-29 15:24:14 -07002427 case OP_IPUT_OBJECT:
2428 case OP_IPUT_OBJECT_VOLATILE:
buzbeebd7865b2011-03-31 10:55:04 -07002429 case OP_IPUT_OBJECT_VOLATILE_JUMBO:
jeffhao71eee1f2011-01-04 14:18:54 -08002430 case OP_IPUT_OBJECT_JUMBO:
buzbee4d92e682010-07-29 15:24:14 -07002431 case OP_IPUT_BOOLEAN:
jeffhao71eee1f2011-01-04 14:18:54 -08002432 case OP_IPUT_BOOLEAN_JUMBO:
buzbee4d92e682010-07-29 15:24:14 -07002433 case OP_IPUT_BYTE:
jeffhao71eee1f2011-01-04 14:18:54 -08002434 case OP_IPUT_BYTE_JUMBO:
buzbee4d92e682010-07-29 15:24:14 -07002435 case OP_IPUT_CHAR:
jeffhao71eee1f2011-01-04 14:18:54 -08002436 case OP_IPUT_CHAR_JUMBO:
2437 case OP_IPUT_SHORT:
2438 case OP_IPUT_SHORT_JUMBO: {
Ben Cheng7a2697d2010-06-07 13:44:23 -07002439 const Method *method = (mir->OptimizationFlags & MIR_CALLEE) ?
2440 mir->meta.calleeMethod : cUnit->method;
buzbee4d92e682010-07-29 15:24:14 -07002441 Field *fieldPtr =
Ben Cheng7a2697d2010-06-07 13:44:23 -07002442 method->clazz->pDvmDex->pResFields[mir->dalvikInsn.vC];
Ben Chengba4fc8b2009-06-01 13:00:29 -07002443
buzbee4d92e682010-07-29 15:24:14 -07002444 if (fieldPtr == NULL) {
Ben Cheng32115a92011-03-22 14:09:09 -07002445 BAIL_LOOP_COMPILATION();
buzbee4d92e682010-07-29 15:24:14 -07002446 LOGE("Unexpected null instance field");
2447 dvmAbort();
2448 }
2449 isVolatile = dvmIsVolatileField(fieldPtr);
2450 fieldOffset = ((InstField *)fieldPtr)->byteOffset;
2451 break;
Ben Chengdd6e8702010-05-07 13:05:47 -07002452 }
buzbee4d92e682010-07-29 15:24:14 -07002453 default:
2454 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002455 }
buzbee4d92e682010-07-29 15:24:14 -07002456
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002457 switch (dalvikOpcode) {
jeffhao71eee1f2011-01-04 14:18:54 -08002458 case OP_NEW_ARRAY:
2459 case OP_NEW_ARRAY_JUMBO: {
Bill Buzbee1465db52009-09-23 17:17:35 -07002460 // Generates a call - use explicit registers
Bill Buzbeec6f10662010-02-09 11:16:15 -08002461 RegLocation rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
2462 RegLocation rlDest = dvmCompilerGetDest(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07002463 RegLocation rlResult;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002464 void *classPtr = (void*)
2465 (cUnit->method->clazz->pDvmDex->pResClasses[mir->dalvikInsn.vC]);
Ben Chengdd6e8702010-05-07 13:05:47 -07002466
2467 if (classPtr == NULL) {
Ben Cheng32115a92011-03-22 14:09:09 -07002468 BAIL_LOOP_COMPILATION();
Ben Chengdd6e8702010-05-07 13:05:47 -07002469 LOGE("Unexpected null class");
2470 dvmAbort();
2471 }
2472
Bill Buzbeec6f10662010-02-09 11:16:15 -08002473 dvmCompilerFlushAllRegs(cUnit); /* Everything to home location */
Bill Buzbee1465db52009-09-23 17:17:35 -07002474 genExportPC(cUnit, mir);
2475 loadValueDirectFixed(cUnit, rlSrc, r1); /* Len */
Ben Chengba4fc8b2009-06-01 13:00:29 -07002476 loadConstant(cUnit, r0, (int) classPtr );
Ben Chengbd1326d2010-04-02 15:04:53 -07002477 LOAD_FUNC_ADDR(cUnit, r3, (int)dvmAllocArrayByClass);
Ben Cheng4f489172009-09-27 17:08:35 -07002478 /*
2479 * "len < 0": bail to the interpreter to re-execute the
2480 * instruction
2481 */
Carl Shapiroe3c01da2010-05-20 22:54:18 -07002482 genRegImmCheck(cUnit, kArmCondMi, r1, 0, mir->offset, NULL);
Bill Buzbee270c1d62009-08-13 16:58:07 -07002483 loadConstant(cUnit, r2, ALLOC_DONT_TRACK);
Bill Buzbee1465db52009-09-23 17:17:35 -07002484 opReg(cUnit, kOpBlx, r3);
Elliott Hughes6a555132010-02-25 15:41:42 -08002485 dvmCompilerClobberCallRegs(cUnit);
Ben Cheng4f489172009-09-27 17:08:35 -07002486 /* generate a branch over if allocation is successful */
buzbee8f8109a2010-08-31 10:16:35 -07002487 ArmLIR *branchOver = genCmpImmBranch(cUnit, kArmCondNe, r0, 0);
Ben Cheng4f489172009-09-27 17:08:35 -07002488 /*
2489 * OOM exception needs to be thrown here and cannot re-execute
2490 */
2491 loadConstant(cUnit, r0,
2492 (int) (cUnit->method->insns + mir->offset));
2493 genDispatchToHandler(cUnit, TEMPLATE_THROW_EXCEPTION_COMMON);
2494 /* noreturn */
2495
Bill Buzbee1465db52009-09-23 17:17:35 -07002496 ArmLIR *target = newLIR0(cUnit, kArmPseudoTargetLabel);
Ben Cheng4f489172009-09-27 17:08:35 -07002497 target->defMask = ENCODE_ALL;
2498 branchOver->generic.target = (LIR *) target;
Bill Buzbeec6f10662010-02-09 11:16:15 -08002499 rlResult = dvmCompilerGetReturn(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07002500 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002501 break;
2502 }
jeffhao71eee1f2011-01-04 14:18:54 -08002503 case OP_INSTANCE_OF:
2504 case OP_INSTANCE_OF_JUMBO: {
Bill Buzbee1465db52009-09-23 17:17:35 -07002505 // May generate a call - use explicit registers
Bill Buzbeec6f10662010-02-09 11:16:15 -08002506 RegLocation rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
2507 RegLocation rlDest = dvmCompilerGetDest(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07002508 RegLocation rlResult;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002509 ClassObject *classPtr =
2510 (cUnit->method->clazz->pDvmDex->pResClasses[mir->dalvikInsn.vC]);
Bill Buzbee480e6782010-01-27 15:43:08 -08002511 /*
2512 * Note: It is possible that classPtr is NULL at this point,
2513 * even though this instruction has been successfully interpreted.
2514 * If the previous interpretation had a null source, the
2515 * interpreter would not have bothered to resolve the clazz.
2516 * Bail out to the interpreter in this case, and log it
2517 * so that we can tell if it happens frequently.
2518 */
2519 if (classPtr == NULL) {
Ben Cheng32115a92011-03-22 14:09:09 -07002520 BAIL_LOOP_COMPILATION();
Bill Buzbee480e6782010-01-27 15:43:08 -08002521 LOGD("null clazz in OP_INSTANCE_OF, single-stepping");
2522 genInterpSingleStep(cUnit, mir);
2523 break;
2524 }
Bill Buzbeec6f10662010-02-09 11:16:15 -08002525 dvmCompilerFlushAllRegs(cUnit); /* Everything to home location */
Bill Buzbee1465db52009-09-23 17:17:35 -07002526 loadValueDirectFixed(cUnit, rlSrc, r0); /* Ref */
Ben Chengba4fc8b2009-06-01 13:00:29 -07002527 loadConstant(cUnit, r2, (int) classPtr );
Ben Cheng752c7942009-06-22 10:50:07 -07002528 /* When taken r0 has NULL which can be used for store directly */
buzbee8f8109a2010-08-31 10:16:35 -07002529 ArmLIR *branch1 = genCmpImmBranch(cUnit, kArmCondEq, r0, 0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002530 /* r1 now contains object->clazz */
Bill Buzbee270c1d62009-08-13 16:58:07 -07002531 loadWordDisp(cUnit, r0, offsetof(Object, clazz), r1);
Bill Buzbee1465db52009-09-23 17:17:35 -07002532 /* r1 now contains object->clazz */
Ben Chengbd1326d2010-04-02 15:04:53 -07002533 LOAD_FUNC_ADDR(cUnit, r3, (int)dvmInstanceofNonTrivial);
Ben Cheng752c7942009-06-22 10:50:07 -07002534 loadConstant(cUnit, r0, 1); /* Assume true */
Bill Buzbee1465db52009-09-23 17:17:35 -07002535 opRegReg(cUnit, kOpCmp, r1, r2);
2536 ArmLIR *branch2 = opCondBranch(cUnit, kArmCondEq);
2537 genRegCopy(cUnit, r0, r1);
2538 genRegCopy(cUnit, r1, r2);
2539 opReg(cUnit, kOpBlx, r3);
Elliott Hughes6a555132010-02-25 15:41:42 -08002540 dvmCompilerClobberCallRegs(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002541 /* branch target here */
Bill Buzbee1465db52009-09-23 17:17:35 -07002542 ArmLIR *target = newLIR0(cUnit, kArmPseudoTargetLabel);
Ben Chengd7d426a2009-09-22 11:23:36 -07002543 target->defMask = ENCODE_ALL;
Bill Buzbeec6f10662010-02-09 11:16:15 -08002544 rlResult = dvmCompilerGetReturn(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07002545 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002546 branch1->generic.target = (LIR *)target;
2547 branch2->generic.target = (LIR *)target;
2548 break;
2549 }
2550 case OP_IGET_WIDE:
jeffhao71eee1f2011-01-04 14:18:54 -08002551 case OP_IGET_WIDE_JUMBO:
Ben Chengba4fc8b2009-06-01 13:00:29 -07002552 genIGetWide(cUnit, mir, fieldOffset);
2553 break;
buzbeeecf8f6e2010-07-20 14:53:42 -07002554 case OP_IGET_VOLATILE:
buzbeebd7865b2011-03-31 10:55:04 -07002555 case OP_IGET_VOLATILE_JUMBO:
buzbeeecf8f6e2010-07-20 14:53:42 -07002556 case OP_IGET_OBJECT_VOLATILE:
buzbeebd7865b2011-03-31 10:55:04 -07002557 case OP_IGET_OBJECT_VOLATILE_JUMBO:
buzbeeecf8f6e2010-07-20 14:53:42 -07002558 isVolatile = true;
2559 // NOTE: intentional fallthrough
Ben Chengba4fc8b2009-06-01 13:00:29 -07002560 case OP_IGET:
jeffhao71eee1f2011-01-04 14:18:54 -08002561 case OP_IGET_JUMBO:
Ben Chengba4fc8b2009-06-01 13:00:29 -07002562 case OP_IGET_OBJECT:
jeffhao71eee1f2011-01-04 14:18:54 -08002563 case OP_IGET_OBJECT_JUMBO:
Ben Chengba4fc8b2009-06-01 13:00:29 -07002564 case OP_IGET_BOOLEAN:
jeffhao71eee1f2011-01-04 14:18:54 -08002565 case OP_IGET_BOOLEAN_JUMBO:
Ben Chengba4fc8b2009-06-01 13:00:29 -07002566 case OP_IGET_BYTE:
jeffhao71eee1f2011-01-04 14:18:54 -08002567 case OP_IGET_BYTE_JUMBO:
Ben Chengba4fc8b2009-06-01 13:00:29 -07002568 case OP_IGET_CHAR:
jeffhao71eee1f2011-01-04 14:18:54 -08002569 case OP_IGET_CHAR_JUMBO:
Ben Chengba4fc8b2009-06-01 13:00:29 -07002570 case OP_IGET_SHORT:
jeffhao71eee1f2011-01-04 14:18:54 -08002571 case OP_IGET_SHORT_JUMBO:
buzbee3272e2f2010-09-09 14:07:01 -07002572 genIGet(cUnit, mir, kWord, fieldOffset, isVolatile);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002573 break;
2574 case OP_IPUT_WIDE:
jeffhao71eee1f2011-01-04 14:18:54 -08002575 case OP_IPUT_WIDE_JUMBO:
Ben Chengba4fc8b2009-06-01 13:00:29 -07002576 genIPutWide(cUnit, mir, fieldOffset);
2577 break;
Carl Shapiro49f30642011-04-04 10:20:43 -07002578 case OP_IPUT_VOLATILE:
2579 case OP_IPUT_VOLATILE_JUMBO:
2580 isVolatile = true;
2581 // NOTE: intentional fallthrough
Ben Chengba4fc8b2009-06-01 13:00:29 -07002582 case OP_IPUT:
jeffhao71eee1f2011-01-04 14:18:54 -08002583 case OP_IPUT_JUMBO:
buzbee3272e2f2010-09-09 14:07:01 -07002584 case OP_IPUT_BOOLEAN:
jeffhao71eee1f2011-01-04 14:18:54 -08002585 case OP_IPUT_BOOLEAN_JUMBO:
2586 case OP_IPUT_BYTE:
2587 case OP_IPUT_BYTE_JUMBO:
2588 case OP_IPUT_CHAR:
2589 case OP_IPUT_CHAR_JUMBO:
2590 case OP_IPUT_SHORT:
2591 case OP_IPUT_SHORT_JUMBO:
buzbeeecf8f6e2010-07-20 14:53:42 -07002592 genIPut(cUnit, mir, kWord, fieldOffset, false, isVolatile);
buzbee919eb062010-07-12 12:59:22 -07002593 break;
buzbeeecf8f6e2010-07-20 14:53:42 -07002594 case OP_IPUT_OBJECT_VOLATILE:
buzbeebd7865b2011-03-31 10:55:04 -07002595 case OP_IPUT_OBJECT_VOLATILE_JUMBO:
buzbeeecf8f6e2010-07-20 14:53:42 -07002596 isVolatile = true;
2597 // NOTE: intentional fallthrough
Ben Chengba4fc8b2009-06-01 13:00:29 -07002598 case OP_IPUT_OBJECT:
jeffhao71eee1f2011-01-04 14:18:54 -08002599 case OP_IPUT_OBJECT_JUMBO:
buzbeeecf8f6e2010-07-20 14:53:42 -07002600 genIPut(cUnit, mir, kWord, fieldOffset, true, isVolatile);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002601 break;
Bill Buzbeeb16344a2010-03-15 17:19:12 -07002602 case OP_IGET_WIDE_VOLATILE:
buzbeebd7865b2011-03-31 10:55:04 -07002603 case OP_IGET_WIDE_VOLATILE_JUMBO:
Bill Buzbeeb16344a2010-03-15 17:19:12 -07002604 case OP_IPUT_WIDE_VOLATILE:
buzbeebd7865b2011-03-31 10:55:04 -07002605 case OP_IPUT_WIDE_VOLATILE_JUMBO:
Bill Buzbeeb16344a2010-03-15 17:19:12 -07002606 genInterpSingleStep(cUnit, mir);
2607 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002608 default:
2609 return true;
2610 }
2611 return false;
2612}
2613
2614static bool handleFmt22cs(CompilationUnit *cUnit, MIR *mir)
2615{
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002616 Opcode dalvikOpcode = mir->dalvikInsn.opcode;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002617 int fieldOffset = mir->dalvikInsn.vC;
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002618 switch (dalvikOpcode) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07002619 case OP_IGET_QUICK:
2620 case OP_IGET_OBJECT_QUICK:
buzbeeecf8f6e2010-07-20 14:53:42 -07002621 genIGet(cUnit, mir, kWord, fieldOffset, false);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002622 break;
2623 case OP_IPUT_QUICK:
buzbeeecf8f6e2010-07-20 14:53:42 -07002624 genIPut(cUnit, mir, kWord, fieldOffset, false, false);
buzbee919eb062010-07-12 12:59:22 -07002625 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002626 case OP_IPUT_OBJECT_QUICK:
buzbeeecf8f6e2010-07-20 14:53:42 -07002627 genIPut(cUnit, mir, kWord, fieldOffset, true, false);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002628 break;
2629 case OP_IGET_WIDE_QUICK:
2630 genIGetWide(cUnit, mir, fieldOffset);
2631 break;
2632 case OP_IPUT_WIDE_QUICK:
2633 genIPutWide(cUnit, mir, fieldOffset);
2634 break;
2635 default:
2636 return true;
2637 }
2638 return false;
2639
2640}
2641
2642/* Compare agaist zero */
2643static bool handleFmt22t(CompilationUnit *cUnit, MIR *mir, BasicBlock *bb,
Bill Buzbee89efc3d2009-07-28 11:22:22 -07002644 ArmLIR *labelList)
Ben Chengba4fc8b2009-06-01 13:00:29 -07002645{
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002646 Opcode dalvikOpcode = mir->dalvikInsn.opcode;
Bill Buzbee89efc3d2009-07-28 11:22:22 -07002647 ArmConditionCode cond;
Ben Cheng7ab74e12011-02-03 14:02:06 -08002648 /* backward branch? */
2649 bool backwardBranch = (bb->taken->startOffset <= mir->offset);
2650
Ben Cheng32115a92011-03-22 14:09:09 -07002651 if (backwardBranch &&
2652 (gDvmJit.genSuspendPoll || cUnit->jitMode == kJitLoop)) {
Ben Cheng7ab74e12011-02-03 14:02:06 -08002653 genSuspendPoll(cUnit, mir);
2654 }
2655
Bill Buzbeec6f10662010-02-09 11:16:15 -08002656 RegLocation rlSrc1 = dvmCompilerGetSrc(cUnit, mir, 0);
2657 RegLocation rlSrc2 = dvmCompilerGetSrc(cUnit, mir, 1);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002658
Bill Buzbee1465db52009-09-23 17:17:35 -07002659 rlSrc1 = loadValue(cUnit, rlSrc1, kCoreReg);
2660 rlSrc2 = loadValue(cUnit, rlSrc2, kCoreReg);
Ben Cheng7ab74e12011-02-03 14:02:06 -08002661
Bill Buzbee1465db52009-09-23 17:17:35 -07002662 opRegReg(cUnit, kOpCmp, rlSrc1.lowReg, rlSrc2.lowReg);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002663
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002664 switch (dalvikOpcode) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07002665 case OP_IF_EQ:
Bill Buzbee1465db52009-09-23 17:17:35 -07002666 cond = kArmCondEq;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002667 break;
2668 case OP_IF_NE:
Bill Buzbee1465db52009-09-23 17:17:35 -07002669 cond = kArmCondNe;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002670 break;
2671 case OP_IF_LT:
Bill Buzbee1465db52009-09-23 17:17:35 -07002672 cond = kArmCondLt;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002673 break;
2674 case OP_IF_GE:
Bill Buzbee1465db52009-09-23 17:17:35 -07002675 cond = kArmCondGe;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002676 break;
2677 case OP_IF_GT:
Bill Buzbee1465db52009-09-23 17:17:35 -07002678 cond = kArmCondGt;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002679 break;
2680 case OP_IF_LE:
Bill Buzbee1465db52009-09-23 17:17:35 -07002681 cond = kArmCondLe;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002682 break;
2683 default:
2684 cond = 0;
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002685 LOGE("Unexpected opcode (%d) for Fmt22t\n", dalvikOpcode);
Bill Buzbeefc519dc2010-03-06 23:30:57 -08002686 dvmCompilerAbort(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002687 }
2688 genConditionalBranch(cUnit, cond, &labelList[bb->taken->id]);
2689 /* This mostly likely will be optimized away in a later phase */
2690 genUnconditionalBranch(cUnit, &labelList[bb->fallThrough->id]);
2691 return false;
2692}
2693
2694static bool handleFmt22x_Fmt32x(CompilationUnit *cUnit, MIR *mir)
2695{
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002696 Opcode opcode = mir->dalvikInsn.opcode;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002697
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002698 switch (opcode) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07002699 case OP_MOVE_16:
2700 case OP_MOVE_OBJECT_16:
2701 case OP_MOVE_FROM16:
Ben Chenge9695e52009-06-16 16:11:47 -07002702 case OP_MOVE_OBJECT_FROM16: {
Bill Buzbeec6f10662010-02-09 11:16:15 -08002703 storeValue(cUnit, dvmCompilerGetDest(cUnit, mir, 0),
2704 dvmCompilerGetSrc(cUnit, mir, 0));
Ben Chengba4fc8b2009-06-01 13:00:29 -07002705 break;
Ben Chenge9695e52009-06-16 16:11:47 -07002706 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07002707 case OP_MOVE_WIDE_16:
Ben Chenge9695e52009-06-16 16:11:47 -07002708 case OP_MOVE_WIDE_FROM16: {
Bill Buzbeec6f10662010-02-09 11:16:15 -08002709 storeValueWide(cUnit, dvmCompilerGetDestWide(cUnit, mir, 0, 1),
2710 dvmCompilerGetSrcWide(cUnit, mir, 0, 1));
Ben Chengba4fc8b2009-06-01 13:00:29 -07002711 break;
Ben Chenge9695e52009-06-16 16:11:47 -07002712 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07002713 default:
2714 return true;
2715 }
2716 return false;
2717}
2718
2719static bool handleFmt23x(CompilationUnit *cUnit, MIR *mir)
2720{
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002721 Opcode opcode = mir->dalvikInsn.opcode;
Bill Buzbee1465db52009-09-23 17:17:35 -07002722 RegLocation rlSrc1;
2723 RegLocation rlSrc2;
2724 RegLocation rlDest;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002725
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002726 if ( (opcode >= OP_ADD_INT) && (opcode <= OP_REM_DOUBLE)) {
Ben Cheng5d90c202009-11-22 23:31:11 -08002727 return genArithOp( cUnit, mir );
Ben Chengba4fc8b2009-06-01 13:00:29 -07002728 }
2729
Bill Buzbee1465db52009-09-23 17:17:35 -07002730 /* APUTs have 3 sources and no targets */
2731 if (mir->ssaRep->numDefs == 0) {
2732 if (mir->ssaRep->numUses == 3) {
Bill Buzbeec6f10662010-02-09 11:16:15 -08002733 rlDest = dvmCompilerGetSrc(cUnit, mir, 0);
2734 rlSrc1 = dvmCompilerGetSrc(cUnit, mir, 1);
2735 rlSrc2 = dvmCompilerGetSrc(cUnit, mir, 2);
Bill Buzbee1465db52009-09-23 17:17:35 -07002736 } else {
2737 assert(mir->ssaRep->numUses == 4);
Bill Buzbeec6f10662010-02-09 11:16:15 -08002738 rlDest = dvmCompilerGetSrcWide(cUnit, mir, 0, 1);
2739 rlSrc1 = dvmCompilerGetSrc(cUnit, mir, 2);
2740 rlSrc2 = dvmCompilerGetSrc(cUnit, mir, 3);
Bill Buzbee1465db52009-09-23 17:17:35 -07002741 }
2742 } else {
2743 /* Two sources and 1 dest. Deduce the operand sizes */
2744 if (mir->ssaRep->numUses == 4) {
Bill Buzbeec6f10662010-02-09 11:16:15 -08002745 rlSrc1 = dvmCompilerGetSrcWide(cUnit, mir, 0, 1);
2746 rlSrc2 = dvmCompilerGetSrcWide(cUnit, mir, 2, 3);
Bill Buzbee1465db52009-09-23 17:17:35 -07002747 } else {
2748 assert(mir->ssaRep->numUses == 2);
Bill Buzbeec6f10662010-02-09 11:16:15 -08002749 rlSrc1 = dvmCompilerGetSrc(cUnit, mir, 0);
2750 rlSrc2 = dvmCompilerGetSrc(cUnit, mir, 1);
Bill Buzbee1465db52009-09-23 17:17:35 -07002751 }
2752 if (mir->ssaRep->numDefs == 2) {
Bill Buzbeec6f10662010-02-09 11:16:15 -08002753 rlDest = dvmCompilerGetDestWide(cUnit, mir, 0, 1);
Bill Buzbee1465db52009-09-23 17:17:35 -07002754 } else {
2755 assert(mir->ssaRep->numDefs == 1);
Bill Buzbeec6f10662010-02-09 11:16:15 -08002756 rlDest = dvmCompilerGetDest(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07002757 }
2758 }
2759
2760
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002761 switch (opcode) {
Bill Buzbeed45ba372009-06-15 17:00:57 -07002762 case OP_CMPL_FLOAT:
2763 case OP_CMPG_FLOAT:
2764 case OP_CMPL_DOUBLE:
2765 case OP_CMPG_DOUBLE:
Ben Cheng5d90c202009-11-22 23:31:11 -08002766 return genCmpFP(cUnit, mir, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002767 case OP_CMP_LONG:
Bill Buzbee1465db52009-09-23 17:17:35 -07002768 genCmpLong(cUnit, mir, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002769 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002770 case OP_AGET_WIDE:
Bill Buzbee1465db52009-09-23 17:17:35 -07002771 genArrayGet(cUnit, mir, kLong, rlSrc1, rlSrc2, rlDest, 3);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002772 break;
2773 case OP_AGET:
2774 case OP_AGET_OBJECT:
Bill Buzbee1465db52009-09-23 17:17:35 -07002775 genArrayGet(cUnit, mir, kWord, rlSrc1, rlSrc2, rlDest, 2);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002776 break;
2777 case OP_AGET_BOOLEAN:
Bill Buzbee1465db52009-09-23 17:17:35 -07002778 genArrayGet(cUnit, mir, kUnsignedByte, rlSrc1, rlSrc2, rlDest, 0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002779 break;
2780 case OP_AGET_BYTE:
Bill Buzbee1465db52009-09-23 17:17:35 -07002781 genArrayGet(cUnit, mir, kSignedByte, rlSrc1, rlSrc2, rlDest, 0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002782 break;
2783 case OP_AGET_CHAR:
Bill Buzbee1465db52009-09-23 17:17:35 -07002784 genArrayGet(cUnit, mir, kUnsignedHalf, rlSrc1, rlSrc2, rlDest, 1);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002785 break;
2786 case OP_AGET_SHORT:
Bill Buzbee1465db52009-09-23 17:17:35 -07002787 genArrayGet(cUnit, mir, kSignedHalf, rlSrc1, rlSrc2, rlDest, 1);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002788 break;
2789 case OP_APUT_WIDE:
Bill Buzbee1465db52009-09-23 17:17:35 -07002790 genArrayPut(cUnit, mir, kLong, rlSrc1, rlSrc2, rlDest, 3);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002791 break;
2792 case OP_APUT:
Bill Buzbee1465db52009-09-23 17:17:35 -07002793 genArrayPut(cUnit, mir, kWord, rlSrc1, rlSrc2, rlDest, 2);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002794 break;
Bill Buzbeebe6534f2010-03-12 16:01:35 -08002795 case OP_APUT_OBJECT:
2796 genArrayObjectPut(cUnit, mir, rlSrc1, rlSrc2, rlDest, 2);
2797 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002798 case OP_APUT_SHORT:
2799 case OP_APUT_CHAR:
Bill Buzbee1465db52009-09-23 17:17:35 -07002800 genArrayPut(cUnit, mir, kUnsignedHalf, rlSrc1, rlSrc2, rlDest, 1);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002801 break;
2802 case OP_APUT_BYTE:
2803 case OP_APUT_BOOLEAN:
Bill Buzbee1465db52009-09-23 17:17:35 -07002804 genArrayPut(cUnit, mir, kUnsignedByte, rlSrc1, rlSrc2, rlDest, 0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002805 break;
2806 default:
2807 return true;
2808 }
2809 return false;
2810}
2811
Ben Cheng6c10a972009-10-29 14:39:18 -07002812/*
2813 * Find the matching case.
2814 *
2815 * return values:
2816 * r0 (low 32-bit): pc of the chaining cell corresponding to the resolved case,
2817 * including default which is placed at MIN(size, MAX_CHAINED_SWITCH_CASES).
2818 * r1 (high 32-bit): the branch offset of the matching case (only for indexes
2819 * above MAX_CHAINED_SWITCH_CASES).
2820 *
2821 * Instructions around the call are:
2822 *
2823 * mov r2, pc
2824 * blx &findPackedSwitchIndex
2825 * mov pc, r0
2826 * .align4
Bill Buzbeebd047242010-05-13 13:02:53 -07002827 * chaining cell for case 0 [12 bytes]
2828 * chaining cell for case 1 [12 bytes]
Ben Cheng6c10a972009-10-29 14:39:18 -07002829 * :
Bill Buzbeebd047242010-05-13 13:02:53 -07002830 * chaining cell for case MIN(size, MAX_CHAINED_SWITCH_CASES)-1 [12 bytes]
Ben Cheng6c10a972009-10-29 14:39:18 -07002831 * chaining cell for case default [8 bytes]
2832 * noChain exit
2833 */
Ben Chengbd1326d2010-04-02 15:04:53 -07002834static s8 findPackedSwitchIndex(const u2* switchData, int testVal, int pc)
Ben Cheng6c10a972009-10-29 14:39:18 -07002835{
2836 int size;
2837 int firstKey;
2838 const int *entries;
2839 int index;
2840 int jumpIndex;
2841 int caseDPCOffset = 0;
2842 /* In Thumb mode pc is 4 ahead of the "mov r2, pc" instruction */
2843 int chainingPC = (pc + 4) & ~3;
2844
2845 /*
2846 * Packed switch data format:
2847 * ushort ident = 0x0100 magic value
2848 * ushort size number of entries in the table
2849 * int first_key first (and lowest) switch case value
2850 * int targets[size] branch targets, relative to switch opcode
2851 *
2852 * Total size is (4+size*2) 16-bit code units.
2853 */
2854 size = switchData[1];
2855 assert(size > 0);
2856
2857 firstKey = switchData[2];
2858 firstKey |= switchData[3] << 16;
2859
2860
2861 /* The entries are guaranteed to be aligned on a 32-bit boundary;
2862 * we can treat them as a native int array.
2863 */
2864 entries = (const int*) &switchData[4];
2865 assert(((u4)entries & 0x3) == 0);
2866
2867 index = testVal - firstKey;
2868
2869 /* Jump to the default cell */
2870 if (index < 0 || index >= size) {
2871 jumpIndex = MIN(size, MAX_CHAINED_SWITCH_CASES);
2872 /* Jump to the non-chaining exit point */
2873 } else if (index >= MAX_CHAINED_SWITCH_CASES) {
2874 jumpIndex = MAX_CHAINED_SWITCH_CASES + 1;
2875 caseDPCOffset = entries[index];
2876 /* Jump to the inline chaining cell */
2877 } else {
2878 jumpIndex = index;
2879 }
2880
Bill Buzbeebd047242010-05-13 13:02:53 -07002881 chainingPC += jumpIndex * CHAIN_CELL_NORMAL_SIZE;
Ben Cheng6c10a972009-10-29 14:39:18 -07002882 return (((s8) caseDPCOffset) << 32) | (u8) chainingPC;
2883}
2884
2885/* See comments for findPackedSwitchIndex */
Ben Chengbd1326d2010-04-02 15:04:53 -07002886static s8 findSparseSwitchIndex(const u2* switchData, int testVal, int pc)
Ben Cheng6c10a972009-10-29 14:39:18 -07002887{
2888 int size;
2889 const int *keys;
2890 const int *entries;
2891 int chainingPC = (pc + 4) & ~3;
2892 int i;
2893
2894 /*
2895 * Sparse switch data format:
2896 * ushort ident = 0x0200 magic value
2897 * ushort size number of entries in the table; > 0
2898 * int keys[size] keys, sorted low-to-high; 32-bit aligned
2899 * int targets[size] branch targets, relative to switch opcode
2900 *
2901 * Total size is (2+size*4) 16-bit code units.
2902 */
2903
2904 size = switchData[1];
2905 assert(size > 0);
2906
2907 /* The keys are guaranteed to be aligned on a 32-bit boundary;
2908 * we can treat them as a native int array.
2909 */
2910 keys = (const int*) &switchData[2];
2911 assert(((u4)keys & 0x3) == 0);
2912
2913 /* The entries are guaranteed to be aligned on a 32-bit boundary;
2914 * we can treat them as a native int array.
2915 */
2916 entries = keys + size;
2917 assert(((u4)entries & 0x3) == 0);
2918
2919 /*
2920 * Run through the list of keys, which are guaranteed to
2921 * be sorted low-to-high.
2922 *
2923 * Most tables have 3-4 entries. Few have more than 10. A binary
2924 * search here is probably not useful.
2925 */
2926 for (i = 0; i < size; i++) {
2927 int k = keys[i];
2928 if (k == testVal) {
2929 /* MAX_CHAINED_SWITCH_CASES + 1 is the start of the overflow case */
2930 int jumpIndex = (i < MAX_CHAINED_SWITCH_CASES) ?
2931 i : MAX_CHAINED_SWITCH_CASES + 1;
Bill Buzbeebd047242010-05-13 13:02:53 -07002932 chainingPC += jumpIndex * CHAIN_CELL_NORMAL_SIZE;
Ben Cheng6c10a972009-10-29 14:39:18 -07002933 return (((s8) entries[i]) << 32) | (u8) chainingPC;
2934 } else if (k > testVal) {
2935 break;
2936 }
2937 }
Bill Buzbeebd047242010-05-13 13:02:53 -07002938 return chainingPC + MIN(size, MAX_CHAINED_SWITCH_CASES) *
2939 CHAIN_CELL_NORMAL_SIZE;
Ben Cheng6c10a972009-10-29 14:39:18 -07002940}
2941
Ben Chengba4fc8b2009-06-01 13:00:29 -07002942static bool handleFmt31t(CompilationUnit *cUnit, MIR *mir)
2943{
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002944 Opcode dalvikOpcode = mir->dalvikInsn.opcode;
2945 switch (dalvikOpcode) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07002946 case OP_FILL_ARRAY_DATA: {
Bill Buzbeec6f10662010-02-09 11:16:15 -08002947 RegLocation rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07002948 // Making a call - use explicit registers
Bill Buzbeec6f10662010-02-09 11:16:15 -08002949 dvmCompilerFlushAllRegs(cUnit); /* Everything to home location */
Bill Buzbee1465db52009-09-23 17:17:35 -07002950 genExportPC(cUnit, mir);
2951 loadValueDirectFixed(cUnit, rlSrc, r0);
Ben Chengbd1326d2010-04-02 15:04:53 -07002952 LOAD_FUNC_ADDR(cUnit, r2, (int)dvmInterpHandleFillArrayData);
Ben Cheng6c10a972009-10-29 14:39:18 -07002953 loadConstant(cUnit, r1,
2954 (int) (cUnit->method->insns + mir->offset + mir->dalvikInsn.vB));
Bill Buzbee1465db52009-09-23 17:17:35 -07002955 opReg(cUnit, kOpBlx, r2);
Elliott Hughes6a555132010-02-25 15:41:42 -08002956 dvmCompilerClobberCallRegs(cUnit);
Bill Buzbeefd023aa2009-11-02 09:23:49 -08002957 /* generate a branch over if successful */
buzbee8f8109a2010-08-31 10:16:35 -07002958 ArmLIR *branchOver = genCmpImmBranch(cUnit, kArmCondNe, r0, 0);
Bill Buzbeefd023aa2009-11-02 09:23:49 -08002959 loadConstant(cUnit, r0,
2960 (int) (cUnit->method->insns + mir->offset));
2961 genDispatchToHandler(cUnit, TEMPLATE_THROW_EXCEPTION_COMMON);
2962 ArmLIR *target = newLIR0(cUnit, kArmPseudoTargetLabel);
2963 target->defMask = ENCODE_ALL;
2964 branchOver->generic.target = (LIR *) target;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002965 break;
2966 }
2967 /*
Ben Cheng6c10a972009-10-29 14:39:18 -07002968 * Compute the goto target of up to
2969 * MIN(switchSize, MAX_CHAINED_SWITCH_CASES) + 1 chaining cells.
2970 * See the comment before findPackedSwitchIndex for the code layout.
Ben Chengba4fc8b2009-06-01 13:00:29 -07002971 */
2972 case OP_PACKED_SWITCH:
2973 case OP_SPARSE_SWITCH: {
Bill Buzbeec6f10662010-02-09 11:16:15 -08002974 RegLocation rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
2975 dvmCompilerFlushAllRegs(cUnit); /* Everything to home location */
Bill Buzbee1465db52009-09-23 17:17:35 -07002976 loadValueDirectFixed(cUnit, rlSrc, r1);
Bill Buzbeec6f10662010-02-09 11:16:15 -08002977 dvmCompilerLockAllTemps(cUnit);
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002978 if (dalvikOpcode == OP_PACKED_SWITCH) {
Ben Chengbd1326d2010-04-02 15:04:53 -07002979 LOAD_FUNC_ADDR(cUnit, r4PC, (int)findPackedSwitchIndex);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002980 } else {
Ben Chengbd1326d2010-04-02 15:04:53 -07002981 LOAD_FUNC_ADDR(cUnit, r4PC, (int)findSparseSwitchIndex);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002982 }
Ben Cheng6c10a972009-10-29 14:39:18 -07002983 /* r0 <- Addr of the switch data */
2984 loadConstant(cUnit, r0,
2985 (int) (cUnit->method->insns + mir->offset + mir->dalvikInsn.vB));
2986 /* r2 <- pc of the instruction following the blx */
Ben Cheng20d7e6c2011-02-18 17:12:42 -08002987 opRegReg(cUnit, kOpMov, r2, r15pc);
Bill Buzbee1465db52009-09-23 17:17:35 -07002988 opReg(cUnit, kOpBlx, r4PC);
Elliott Hughes6a555132010-02-25 15:41:42 -08002989 dvmCompilerClobberCallRegs(cUnit);
Ben Cheng6c10a972009-10-29 14:39:18 -07002990 /* pc <- computed goto target */
Ben Cheng20d7e6c2011-02-18 17:12:42 -08002991 opRegReg(cUnit, kOpMov, r15pc, r0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002992 break;
2993 }
2994 default:
2995 return true;
2996 }
2997 return false;
2998}
2999
Ben Cheng7a2697d2010-06-07 13:44:23 -07003000/*
3001 * See the example of predicted inlining listed before the
3002 * genValidationForPredictedInline function. The function here takes care the
3003 * branch over at 0x4858de78 and the misprediction target at 0x4858de7a.
3004 */
3005static void genLandingPadForMispredictedCallee(CompilationUnit *cUnit, MIR *mir,
3006 BasicBlock *bb,
3007 ArmLIR *labelList)
3008{
3009 BasicBlock *fallThrough = bb->fallThrough;
3010
3011 /* Bypass the move-result block if there is one */
3012 if (fallThrough->firstMIRInsn) {
3013 assert(fallThrough->firstMIRInsn->OptimizationFlags & MIR_INLINED_PRED);
3014 fallThrough = fallThrough->fallThrough;
3015 }
3016 /* Generate a branch over if the predicted inlining is correct */
3017 genUnconditionalBranch(cUnit, &labelList[fallThrough->id]);
3018
3019 /* Reset the register state */
3020 dvmCompilerResetRegPool(cUnit);
3021 dvmCompilerClobberAllRegs(cUnit);
3022 dvmCompilerResetNullCheck(cUnit);
3023
3024 /* Target for the slow invoke path */
3025 ArmLIR *target = newLIR0(cUnit, kArmPseudoTargetLabel);
3026 target->defMask = ENCODE_ALL;
3027 /* Hook up the target to the verification branch */
3028 mir->meta.callsiteInfo->misPredBranchOver->target = (LIR *) target;
3029}
3030
jeffhao71eee1f2011-01-04 14:18:54 -08003031static bool handleFmt35c_3rc_5rc(CompilationUnit *cUnit, MIR *mir,
3032 BasicBlock *bb, ArmLIR *labelList)
Ben Chengba4fc8b2009-06-01 13:00:29 -07003033{
Bill Buzbee9bc3df32009-07-30 10:52:29 -07003034 ArmLIR *retChainingCell = NULL;
Bill Buzbee89efc3d2009-07-28 11:22:22 -07003035 ArmLIR *pcrLabel = NULL;
Ben Chengba4fc8b2009-06-01 13:00:29 -07003036
Ben Cheng7a2697d2010-06-07 13:44:23 -07003037 /* An invoke with the MIR_INLINED is effectively a no-op */
3038 if (mir->OptimizationFlags & MIR_INLINED)
3039 return false;
3040
Bill Buzbeef4ce16f2009-07-28 13:28:25 -07003041 if (bb->fallThrough != NULL)
3042 retChainingCell = &labelList[bb->fallThrough->id];
3043
Ben Chengba4fc8b2009-06-01 13:00:29 -07003044 DecodedInstruction *dInsn = &mir->dalvikInsn;
Dan Bornstein9a1f8162010-12-01 17:02:26 -08003045 switch (mir->dalvikInsn.opcode) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07003046 /*
3047 * calleeMethod = this->clazz->vtable[
3048 * method->clazz->pDvmDex->pResMethods[BBBB]->methodIndex
3049 * ]
3050 */
3051 case OP_INVOKE_VIRTUAL:
jeffhao71eee1f2011-01-04 14:18:54 -08003052 case OP_INVOKE_VIRTUAL_RANGE:
3053 case OP_INVOKE_VIRTUAL_JUMBO: {
Bill Buzbee89efc3d2009-07-28 11:22:22 -07003054 ArmLIR *predChainingCell = &labelList[bb->taken->id];
Ben Chengba4fc8b2009-06-01 13:00:29 -07003055 int methodIndex =
3056 cUnit->method->clazz->pDvmDex->pResMethods[dInsn->vB]->
3057 methodIndex;
3058
Ben Cheng7a2697d2010-06-07 13:44:23 -07003059 /*
3060 * If the invoke has non-null misPredBranchOver, we need to generate
3061 * the non-inlined version of the invoke here to handle the
3062 * mispredicted case.
3063 */
3064 if (mir->meta.callsiteInfo->misPredBranchOver) {
3065 genLandingPadForMispredictedCallee(cUnit, mir, bb, labelList);
3066 }
3067
Dan Bornstein9a1f8162010-12-01 17:02:26 -08003068 if (mir->dalvikInsn.opcode == OP_INVOKE_VIRTUAL)
Ben Chengba4fc8b2009-06-01 13:00:29 -07003069 genProcessArgsNoRange(cUnit, mir, dInsn, &pcrLabel);
3070 else
3071 genProcessArgsRange(cUnit, mir, dInsn, &pcrLabel);
3072
Ben Cheng38329f52009-07-07 14:19:20 -07003073 genInvokeVirtualCommon(cUnit, mir, methodIndex,
3074 retChainingCell,
3075 predChainingCell,
3076 pcrLabel);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003077 break;
3078 }
3079 /*
3080 * calleeMethod = method->clazz->super->vtable[method->clazz->pDvmDex
3081 * ->pResMethods[BBBB]->methodIndex]
3082 */
Ben Chengba4fc8b2009-06-01 13:00:29 -07003083 case OP_INVOKE_SUPER:
jeffhao71eee1f2011-01-04 14:18:54 -08003084 case OP_INVOKE_SUPER_RANGE:
3085 case OP_INVOKE_SUPER_JUMBO: {
Ben Cheng7a2697d2010-06-07 13:44:23 -07003086 /* Grab the method ptr directly from what the interpreter sees */
3087 const Method *calleeMethod = mir->meta.callsiteInfo->method;
3088 assert(calleeMethod == cUnit->method->clazz->super->vtable[
3089 cUnit->method->clazz->pDvmDex->
3090 pResMethods[dInsn->vB]->methodIndex]);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003091
Dan Bornstein9a1f8162010-12-01 17:02:26 -08003092 if (mir->dalvikInsn.opcode == OP_INVOKE_SUPER)
Ben Chengba4fc8b2009-06-01 13:00:29 -07003093 genProcessArgsNoRange(cUnit, mir, dInsn, &pcrLabel);
3094 else
3095 genProcessArgsRange(cUnit, mir, dInsn, &pcrLabel);
3096
Ben Cheng20d7e6c2011-02-18 17:12:42 -08003097 if (mir->OptimizationFlags & MIR_INVOKE_METHOD_JIT) {
3098 const Method *calleeMethod = mir->meta.callsiteInfo->method;
3099 void *calleeAddr = dvmJitGetMethodAddr(calleeMethod->insns);
3100 assert(calleeAddr);
3101 genInvokeSingletonWholeMethod(cUnit, mir, calleeAddr,
3102 retChainingCell);
3103 } else {
3104 /* r0 = calleeMethod */
3105 loadConstant(cUnit, r0, (int) calleeMethod);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003106
Ben Cheng20d7e6c2011-02-18 17:12:42 -08003107 genInvokeSingletonCommon(cUnit, mir, bb, labelList, pcrLabel,
3108 calleeMethod);
3109 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07003110 break;
3111 }
3112 /* calleeMethod = method->clazz->pDvmDex->pResMethods[BBBB] */
3113 case OP_INVOKE_DIRECT:
jeffhao71eee1f2011-01-04 14:18:54 -08003114 case OP_INVOKE_DIRECT_RANGE:
3115 case OP_INVOKE_DIRECT_JUMBO: {
Ben Cheng7a2697d2010-06-07 13:44:23 -07003116 /* Grab the method ptr directly from what the interpreter sees */
3117 const Method *calleeMethod = mir->meta.callsiteInfo->method;
3118 assert(calleeMethod ==
3119 cUnit->method->clazz->pDvmDex->pResMethods[dInsn->vB]);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003120
Dan Bornstein9a1f8162010-12-01 17:02:26 -08003121 if (mir->dalvikInsn.opcode == OP_INVOKE_DIRECT)
Ben Chengba4fc8b2009-06-01 13:00:29 -07003122 genProcessArgsNoRange(cUnit, mir, dInsn, &pcrLabel);
3123 else
3124 genProcessArgsRange(cUnit, mir, dInsn, &pcrLabel);
3125
3126 /* r0 = calleeMethod */
3127 loadConstant(cUnit, r0, (int) calleeMethod);
3128
Ben Cheng38329f52009-07-07 14:19:20 -07003129 genInvokeSingletonCommon(cUnit, mir, bb, labelList, pcrLabel,
3130 calleeMethod);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003131 break;
3132 }
3133 /* calleeMethod = method->clazz->pDvmDex->pResMethods[BBBB] */
3134 case OP_INVOKE_STATIC:
jeffhao71eee1f2011-01-04 14:18:54 -08003135 case OP_INVOKE_STATIC_RANGE:
3136 case OP_INVOKE_STATIC_JUMBO: {
Ben Cheng7a2697d2010-06-07 13:44:23 -07003137 /* Grab the method ptr directly from what the interpreter sees */
3138 const Method *calleeMethod = mir->meta.callsiteInfo->method;
3139 assert(calleeMethod ==
3140 cUnit->method->clazz->pDvmDex->pResMethods[dInsn->vB]);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003141
Dan Bornstein9a1f8162010-12-01 17:02:26 -08003142 if (mir->dalvikInsn.opcode == OP_INVOKE_STATIC)
Ben Chengba4fc8b2009-06-01 13:00:29 -07003143 genProcessArgsNoRange(cUnit, mir, dInsn,
3144 NULL /* no null check */);
3145 else
3146 genProcessArgsRange(cUnit, mir, dInsn,
3147 NULL /* no null check */);
3148
Ben Cheng20d7e6c2011-02-18 17:12:42 -08003149 if (mir->OptimizationFlags & MIR_INVOKE_METHOD_JIT) {
3150 const Method *calleeMethod = mir->meta.callsiteInfo->method;
3151 void *calleeAddr = dvmJitGetMethodAddr(calleeMethod->insns);
3152 assert(calleeAddr);
3153 genInvokeSingletonWholeMethod(cUnit, mir, calleeAddr,
3154 retChainingCell);
3155 } else {
3156 /* r0 = calleeMethod */
3157 loadConstant(cUnit, r0, (int) calleeMethod);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003158
Ben Cheng20d7e6c2011-02-18 17:12:42 -08003159 genInvokeSingletonCommon(cUnit, mir, bb, labelList, pcrLabel,
3160 calleeMethod);
3161 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07003162 break;
3163 }
Ben Cheng09e50c92010-05-02 10:45:32 -07003164 /*
Ben Chengba4fc8b2009-06-01 13:00:29 -07003165 * calleeMethod = dvmFindInterfaceMethodInCache(this->clazz,
3166 * BBBB, method, method->clazz->pDvmDex)
Ben Cheng38329f52009-07-07 14:19:20 -07003167 *
Ben Cheng09e50c92010-05-02 10:45:32 -07003168 * The following is an example of generated code for
3169 * "invoke-interface v0"
Ben Cheng38329f52009-07-07 14:19:20 -07003170 *
Ben Cheng09e50c92010-05-02 10:45:32 -07003171 * -------- dalvik offset: 0x0008 @ invoke-interface v0
3172 * 0x47357e36 : ldr r0, [r5, #0] --+
3173 * 0x47357e38 : sub r7,r5,#24 |
3174 * 0x47357e3c : cmp r0, #0 | genProcessArgsNoRange
3175 * 0x47357e3e : beq 0x47357e82 |
3176 * 0x47357e40 : stmia r7, <r0> --+
3177 * 0x47357e42 : ldr r4, [pc, #120] --> r4 <- dalvikPC of this invoke
3178 * 0x47357e44 : add r1, pc, #64 --> r1 <- &retChainingCell
3179 * 0x47357e46 : add r2, pc, #72 --> r2 <- &predictedChainingCell
3180 * 0x47357e48 : blx_1 0x47348190 --+ TEMPLATE_INVOKE_METHOD_
3181 * 0x47357e4a : blx_2 see above --+ PREDICTED_CHAIN
3182 * 0x47357e4c : b 0x47357e90 --> off to the predicted chain
3183 * 0x47357e4e : b 0x47357e82 --> punt to the interpreter
3184 * 0x47357e50 : mov r8, r1 --+
3185 * 0x47357e52 : mov r9, r2 |
3186 * 0x47357e54 : ldr r2, [pc, #96] |
3187 * 0x47357e56 : mov r10, r3 |
3188 * 0x47357e58 : movs r0, r3 | dvmFindInterfaceMethodInCache
3189 * 0x47357e5a : ldr r3, [pc, #88] |
3190 * 0x47357e5c : ldr r7, [pc, #80] |
3191 * 0x47357e5e : mov r1, #1452 |
3192 * 0x47357e62 : blx r7 --+
3193 * 0x47357e64 : cmp r0, #0 --> calleeMethod == NULL?
3194 * 0x47357e66 : bne 0x47357e6e --> branch over the throw if !r0
3195 * 0x47357e68 : ldr r0, [pc, #80] --> load Dalvik PC of the invoke
3196 * 0x47357e6a : blx_1 0x47348494 --+ TEMPLATE_THROW_EXCEPTION_
3197 * 0x47357e6c : blx_2 see above --+ COMMON
3198 * 0x47357e6e : mov r1, r8 --> r1 <- &retChainingCell
3199 * 0x47357e70 : cmp r1, #0 --> compare against 0
3200 * 0x47357e72 : bgt 0x47357e7c --> >=0? don't rechain
Ben Chengaf5aa1f2011-01-04 15:37:04 -08003201 * 0x47357e74 : ldr r7, [pc, #off] --+
Ben Cheng09e50c92010-05-02 10:45:32 -07003202 * 0x47357e76 : mov r2, r9 | dvmJitToPatchPredictedChain
3203 * 0x47357e78 : mov r3, r10 |
3204 * 0x47357e7a : blx r7 --+
3205 * 0x47357e7c : add r1, pc, #8 --> r1 <- &retChainingCell
3206 * 0x47357e7e : blx_1 0x4734809c --+ TEMPLATE_INVOKE_METHOD_NO_OPT
3207 * 0x47357e80 : blx_2 see above --+
3208 * -------- reconstruct dalvik PC : 0x425719dc @ +0x0008
3209 * 0x47357e82 : ldr r0, [pc, #56]
Ben Cheng38329f52009-07-07 14:19:20 -07003210 * Exception_Handling:
Ben Cheng09e50c92010-05-02 10:45:32 -07003211 * 0x47357e84 : ldr r1, [r6, #92]
3212 * 0x47357e86 : blx r1
3213 * 0x47357e88 : .align4
3214 * -------- chaining cell (hot): 0x000b
3215 * 0x47357e88 : ldr r0, [r6, #104]
3216 * 0x47357e8a : blx r0
3217 * 0x47357e8c : data 0x19e2(6626)
3218 * 0x47357e8e : data 0x4257(16983)
3219 * 0x47357e90 : .align4
Ben Cheng38329f52009-07-07 14:19:20 -07003220 * -------- chaining cell (predicted)
Ben Cheng09e50c92010-05-02 10:45:32 -07003221 * 0x47357e90 : data 0xe7fe(59390) --> will be patched into bx
3222 * 0x47357e92 : data 0x0000(0)
3223 * 0x47357e94 : data 0x0000(0) --> class
3224 * 0x47357e96 : data 0x0000(0)
3225 * 0x47357e98 : data 0x0000(0) --> method
3226 * 0x47357e9a : data 0x0000(0)
3227 * 0x47357e9c : data 0x0000(0) --> rechain count
3228 * 0x47357e9e : data 0x0000(0)
3229 * -------- end of chaining cells (0x006c)
3230 * 0x47357eb0 : .word (0xad03e369)
3231 * 0x47357eb4 : .word (0x28a90)
3232 * 0x47357eb8 : .word (0x41a63394)
3233 * 0x47357ebc : .word (0x425719dc)
Ben Chengba4fc8b2009-06-01 13:00:29 -07003234 */
3235 case OP_INVOKE_INTERFACE:
jeffhao71eee1f2011-01-04 14:18:54 -08003236 case OP_INVOKE_INTERFACE_RANGE:
3237 case OP_INVOKE_INTERFACE_JUMBO: {
Bill Buzbee89efc3d2009-07-28 11:22:22 -07003238 ArmLIR *predChainingCell = &labelList[bb->taken->id];
Ben Chengba4fc8b2009-06-01 13:00:29 -07003239
Ben Cheng7a2697d2010-06-07 13:44:23 -07003240 /*
3241 * If the invoke has non-null misPredBranchOver, we need to generate
3242 * the non-inlined version of the invoke here to handle the
3243 * mispredicted case.
3244 */
3245 if (mir->meta.callsiteInfo->misPredBranchOver) {
3246 genLandingPadForMispredictedCallee(cUnit, mir, bb, labelList);
3247 }
Bill Buzbee1465db52009-09-23 17:17:35 -07003248
Dan Bornstein9a1f8162010-12-01 17:02:26 -08003249 if (mir->dalvikInsn.opcode == OP_INVOKE_INTERFACE)
Ben Chengba4fc8b2009-06-01 13:00:29 -07003250 genProcessArgsNoRange(cUnit, mir, dInsn, &pcrLabel);
3251 else
3252 genProcessArgsRange(cUnit, mir, dInsn, &pcrLabel);
3253
Ben Cheng38329f52009-07-07 14:19:20 -07003254 /* "this" is already left in r0 by genProcessArgs* */
3255
3256 /* r4PC = dalvikCallsite */
3257 loadConstant(cUnit, r4PC,
3258 (int) (cUnit->method->insns + mir->offset));
3259
3260 /* r1 = &retChainingCell */
Bill Buzbee270c1d62009-08-13 16:58:07 -07003261 ArmLIR *addrRetChain =
Ben Cheng20d7e6c2011-02-18 17:12:42 -08003262 opRegRegImm(cUnit, kOpAdd, r1, r15pc, 0);
Ben Cheng38329f52009-07-07 14:19:20 -07003263 addrRetChain->generic.target = (LIR *) retChainingCell;
3264
3265 /* r2 = &predictedChainingCell */
Bill Buzbee89efc3d2009-07-28 11:22:22 -07003266 ArmLIR *predictedChainingCell =
Ben Cheng20d7e6c2011-02-18 17:12:42 -08003267 opRegRegImm(cUnit, kOpAdd, r2, r15pc, 0);
Ben Cheng38329f52009-07-07 14:19:20 -07003268 predictedChainingCell->generic.target = (LIR *) predChainingCell;
3269
buzbee18fba342011-01-19 15:31:15 -08003270 genDispatchToHandler(cUnit, gDvmJit.methodTraceSupport ?
3271 TEMPLATE_INVOKE_METHOD_PREDICTED_CHAIN_PROF :
3272 TEMPLATE_INVOKE_METHOD_PREDICTED_CHAIN);
Ben Cheng38329f52009-07-07 14:19:20 -07003273
3274 /* return through lr - jump to the chaining cell */
3275 genUnconditionalBranch(cUnit, predChainingCell);
3276
3277 /*
3278 * null-check on "this" may have been eliminated, but we still need
3279 * a PC-reconstruction label for stack overflow bailout.
3280 */
3281 if (pcrLabel == NULL) {
3282 int dPC = (int) (cUnit->method->insns + mir->offset);
Carl Shapirofc75f3e2010-12-07 11:43:38 -08003283 pcrLabel = (ArmLIR *) dvmCompilerNew(sizeof(ArmLIR), true);
Dan Bornstein9a1f8162010-12-01 17:02:26 -08003284 pcrLabel->opcode = kArmPseudoPCReconstructionCell;
Ben Cheng38329f52009-07-07 14:19:20 -07003285 pcrLabel->operands[0] = dPC;
3286 pcrLabel->operands[1] = mir->offset;
3287 /* Insert the place holder to the growable list */
Ben Cheng00603072010-10-28 11:13:58 -07003288 dvmInsertGrowableList(&cUnit->pcReconstructionList,
3289 (intptr_t) pcrLabel);
Ben Cheng38329f52009-07-07 14:19:20 -07003290 }
3291
3292 /* return through lr+2 - punt to the interpreter */
3293 genUnconditionalBranch(cUnit, pcrLabel);
3294
3295 /*
3296 * return through lr+4 - fully resolve the callee method.
3297 * r1 <- count
3298 * r2 <- &predictedChainCell
3299 * r3 <- this->class
3300 * r4 <- dPC
3301 * r7 <- this->class->vtable
3302 */
3303
3304 /* Save count, &predictedChainCell, and class to high regs first */
Bill Buzbee1465db52009-09-23 17:17:35 -07003305 genRegCopy(cUnit, r8, r1);
3306 genRegCopy(cUnit, r9, r2);
3307 genRegCopy(cUnit, r10, r3);
Ben Cheng38329f52009-07-07 14:19:20 -07003308
Ben Chengba4fc8b2009-06-01 13:00:29 -07003309 /* r0 now contains this->clazz */
Bill Buzbee1465db52009-09-23 17:17:35 -07003310 genRegCopy(cUnit, r0, r3);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003311
3312 /* r1 = BBBB */
3313 loadConstant(cUnit, r1, dInsn->vB);
3314
3315 /* r2 = method (caller) */
3316 loadConstant(cUnit, r2, (int) cUnit->method);
3317
3318 /* r3 = pDvmDex */
3319 loadConstant(cUnit, r3, (int) cUnit->method->clazz->pDvmDex);
3320
Ben Chengbd1326d2010-04-02 15:04:53 -07003321 LOAD_FUNC_ADDR(cUnit, r7,
3322 (intptr_t) dvmFindInterfaceMethodInCache);
Bill Buzbee1465db52009-09-23 17:17:35 -07003323 opReg(cUnit, kOpBlx, r7);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003324 /* r0 = calleeMethod (returned from dvmFindInterfaceMethodInCache */
3325
Ben Cheng09e50c92010-05-02 10:45:32 -07003326 dvmCompilerClobberCallRegs(cUnit);
3327 /* generate a branch over if the interface method is resolved */
buzbee8f8109a2010-08-31 10:16:35 -07003328 ArmLIR *branchOver = genCmpImmBranch(cUnit, kArmCondNe, r0, 0);
Ben Cheng09e50c92010-05-02 10:45:32 -07003329 /*
3330 * calleeMethod == NULL -> throw
3331 */
3332 loadConstant(cUnit, r0,
3333 (int) (cUnit->method->insns + mir->offset));
3334 genDispatchToHandler(cUnit, TEMPLATE_THROW_EXCEPTION_COMMON);
3335 /* noreturn */
3336
3337 ArmLIR *target = newLIR0(cUnit, kArmPseudoTargetLabel);
3338 target->defMask = ENCODE_ALL;
3339 branchOver->generic.target = (LIR *) target;
3340
Bill Buzbee1465db52009-09-23 17:17:35 -07003341 genRegCopy(cUnit, r1, r8);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003342
Ben Cheng38329f52009-07-07 14:19:20 -07003343 /* Check if rechain limit is reached */
buzbee8f8109a2010-08-31 10:16:35 -07003344 ArmLIR *bypassRechaining = genCmpImmBranch(cUnit, kArmCondGt,
3345 r1, 0);
Ben Cheng38329f52009-07-07 14:19:20 -07003346
Ben Chengaf5aa1f2011-01-04 15:37:04 -08003347 LOAD_FUNC_ADDR(cUnit, r7, (int) dvmJitToPatchPredictedChain);
Ben Cheng38329f52009-07-07 14:19:20 -07003348
Ben Cheng20d7e6c2011-02-18 17:12:42 -08003349 genRegCopy(cUnit, r1, r6SELF);
Bill Buzbee1465db52009-09-23 17:17:35 -07003350 genRegCopy(cUnit, r2, r9);
3351 genRegCopy(cUnit, r3, r10);
Ben Cheng38329f52009-07-07 14:19:20 -07003352
3353 /*
3354 * r0 = calleeMethod
3355 * r2 = &predictedChainingCell
3356 * r3 = class
3357 *
3358 * &returnChainingCell has been loaded into r1 but is not needed
3359 * when patching the chaining cell and will be clobbered upon
3360 * returning so it will be reconstructed again.
3361 */
Bill Buzbee1465db52009-09-23 17:17:35 -07003362 opReg(cUnit, kOpBlx, r7);
Ben Cheng38329f52009-07-07 14:19:20 -07003363
3364 /* r1 = &retChainingCell */
Ben Cheng20d7e6c2011-02-18 17:12:42 -08003365 addrRetChain = opRegRegImm(cUnit, kOpAdd, r1, r15pc, 0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003366 addrRetChain->generic.target = (LIR *) retChainingCell;
Ben Cheng38329f52009-07-07 14:19:20 -07003367
3368 bypassRechaining->generic.target = (LIR *) addrRetChain;
3369
Ben Chengba4fc8b2009-06-01 13:00:29 -07003370 /*
3371 * r0 = this, r1 = calleeMethod,
3372 * r1 = &ChainingCell,
3373 * r4PC = callsiteDPC,
3374 */
buzbee18fba342011-01-19 15:31:15 -08003375 genDispatchToHandler(cUnit, gDvmJit.methodTraceSupport ?
3376 TEMPLATE_INVOKE_METHOD_NO_OPT_PROF :
3377 TEMPLATE_INVOKE_METHOD_NO_OPT);
Ben Cheng978738d2010-05-13 13:45:57 -07003378#if defined(WITH_JIT_TUNING)
Ben Cheng86717f72010-03-05 15:27:21 -08003379 gDvmJit.invokePolymorphic++;
Ben Chengba4fc8b2009-06-01 13:00:29 -07003380#endif
3381 /* Handle exceptions using the interpreter */
3382 genTrap(cUnit, mir->offset, pcrLabel);
3383 break;
3384 }
buzbeebd7865b2011-03-31 10:55:04 -07003385 case OP_INVOKE_OBJECT_INIT_JUMBO:
3386 case OP_INVOKE_OBJECT_INIT_RANGE:
Ben Chengba4fc8b2009-06-01 13:00:29 -07003387 case OP_FILLED_NEW_ARRAY:
jeffhao71eee1f2011-01-04 14:18:54 -08003388 case OP_FILLED_NEW_ARRAY_RANGE:
3389 case OP_FILLED_NEW_ARRAY_JUMBO: {
Ben Chengba4fc8b2009-06-01 13:00:29 -07003390 /* Just let the interpreter deal with these */
3391 genInterpSingleStep(cUnit, mir);
3392 break;
3393 }
3394 default:
3395 return true;
3396 }
3397 return false;
3398}
3399
3400static bool handleFmt35ms_3rms(CompilationUnit *cUnit, MIR *mir,
Bill Buzbee89efc3d2009-07-28 11:22:22 -07003401 BasicBlock *bb, ArmLIR *labelList)
Ben Chengba4fc8b2009-06-01 13:00:29 -07003402{
Bill Buzbee89efc3d2009-07-28 11:22:22 -07003403 ArmLIR *pcrLabel = NULL;
Ben Chengba4fc8b2009-06-01 13:00:29 -07003404
Ben Cheng7a2697d2010-06-07 13:44:23 -07003405 /* An invoke with the MIR_INLINED is effectively a no-op */
3406 if (mir->OptimizationFlags & MIR_INLINED)
3407 return false;
3408
Ben Chengba4fc8b2009-06-01 13:00:29 -07003409 DecodedInstruction *dInsn = &mir->dalvikInsn;
Dan Bornstein9a1f8162010-12-01 17:02:26 -08003410 switch (mir->dalvikInsn.opcode) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07003411 /* calleeMethod = this->clazz->vtable[BBBB] */
3412 case OP_INVOKE_VIRTUAL_QUICK_RANGE:
3413 case OP_INVOKE_VIRTUAL_QUICK: {
3414 int methodIndex = dInsn->vB;
Bill Buzbeea8589332010-12-27 09:31:21 -08003415 ArmLIR *retChainingCell = &labelList[bb->fallThrough->id];
3416 ArmLIR *predChainingCell = &labelList[bb->taken->id];
Ben Cheng7a2697d2010-06-07 13:44:23 -07003417
3418 /*
3419 * If the invoke has non-null misPredBranchOver, we need to generate
3420 * the non-inlined version of the invoke here to handle the
3421 * mispredicted case.
3422 */
3423 if (mir->meta.callsiteInfo->misPredBranchOver) {
3424 genLandingPadForMispredictedCallee(cUnit, mir, bb, labelList);
3425 }
3426
Dan Bornstein9a1f8162010-12-01 17:02:26 -08003427 if (mir->dalvikInsn.opcode == OP_INVOKE_VIRTUAL_QUICK)
Ben Chengba4fc8b2009-06-01 13:00:29 -07003428 genProcessArgsNoRange(cUnit, mir, dInsn, &pcrLabel);
3429 else
3430 genProcessArgsRange(cUnit, mir, dInsn, &pcrLabel);
3431
Ben Chengcfdeca32011-01-14 11:36:46 -08003432
3433 if (mir->OptimizationFlags & MIR_INVOKE_METHOD_JIT) {
3434 const Method *calleeMethod = mir->meta.callsiteInfo->method;
3435 void *calleeAddr = dvmJitGetMethodAddr(calleeMethod->insns);
Ben Cheng20d7e6c2011-02-18 17:12:42 -08003436 assert(calleeAddr);
3437 genInvokeVirtualWholeMethod(cUnit, mir, calleeAddr,
3438 retChainingCell);
Ben Chengcfdeca32011-01-14 11:36:46 -08003439 }
3440
Ben Cheng38329f52009-07-07 14:19:20 -07003441 genInvokeVirtualCommon(cUnit, mir, methodIndex,
3442 retChainingCell,
3443 predChainingCell,
3444 pcrLabel);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003445 break;
3446 }
3447 /* calleeMethod = method->clazz->super->vtable[BBBB] */
3448 case OP_INVOKE_SUPER_QUICK:
3449 case OP_INVOKE_SUPER_QUICK_RANGE: {
Ben Cheng7a2697d2010-06-07 13:44:23 -07003450 /* Grab the method ptr directly from what the interpreter sees */
3451 const Method *calleeMethod = mir->meta.callsiteInfo->method;
3452 assert(calleeMethod ==
3453 cUnit->method->clazz->super->vtable[dInsn->vB]);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003454
Dan Bornstein9a1f8162010-12-01 17:02:26 -08003455 if (mir->dalvikInsn.opcode == OP_INVOKE_SUPER_QUICK)
Ben Chengba4fc8b2009-06-01 13:00:29 -07003456 genProcessArgsNoRange(cUnit, mir, dInsn, &pcrLabel);
3457 else
3458 genProcessArgsRange(cUnit, mir, dInsn, &pcrLabel);
3459
3460 /* r0 = calleeMethod */
3461 loadConstant(cUnit, r0, (int) calleeMethod);
3462
Ben Cheng38329f52009-07-07 14:19:20 -07003463 genInvokeSingletonCommon(cUnit, mir, bb, labelList, pcrLabel,
3464 calleeMethod);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003465 break;
3466 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07003467 default:
3468 return true;
3469 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07003470 return false;
3471}
3472
3473/*
Bill Buzbeefd023aa2009-11-02 09:23:49 -08003474 * This operation is complex enough that we'll do it partly inline
3475 * and partly with a handler. NOTE: the handler uses hardcoded
3476 * values for string object offsets and must be revisitied if the
3477 * layout changes.
3478 */
3479static bool genInlinedCompareTo(CompilationUnit *cUnit, MIR *mir)
3480{
3481#if defined(USE_GLOBAL_STRING_DEFS)
Elliott Hughes7e914f12011-01-19 18:18:42 -08003482 return handleExecuteInlineC(cUnit, mir);
Bill Buzbeefd023aa2009-11-02 09:23:49 -08003483#else
3484 ArmLIR *rollback;
Bill Buzbeec6f10662010-02-09 11:16:15 -08003485 RegLocation rlThis = dvmCompilerGetSrc(cUnit, mir, 0);
3486 RegLocation rlComp = dvmCompilerGetSrc(cUnit, mir, 1);
Bill Buzbeefd023aa2009-11-02 09:23:49 -08003487
3488 loadValueDirectFixed(cUnit, rlThis, r0);
3489 loadValueDirectFixed(cUnit, rlComp, r1);
3490 /* Test objects for NULL */
3491 rollback = genNullCheck(cUnit, rlThis.sRegLow, r0, mir->offset, NULL);
3492 genNullCheck(cUnit, rlComp.sRegLow, r1, mir->offset, rollback);
3493 /*
3494 * TUNING: we could check for object pointer equality before invoking
3495 * handler. Unclear whether the gain would be worth the added code size
3496 * expansion.
3497 */
3498 genDispatchToHandler(cUnit, TEMPLATE_STRING_COMPARETO);
Bill Buzbeec6f10662010-02-09 11:16:15 -08003499 storeValue(cUnit, inlinedTarget(cUnit, mir, false),
3500 dvmCompilerGetReturn(cUnit));
Elliott Hughes7e914f12011-01-19 18:18:42 -08003501 return false;
Bill Buzbeefd023aa2009-11-02 09:23:49 -08003502#endif
3503}
3504
Elliott Hughes2bdbcb62010-04-12 14:29:37 -07003505static bool genInlinedFastIndexOf(CompilationUnit *cUnit, MIR *mir)
Bill Buzbeefd023aa2009-11-02 09:23:49 -08003506{
3507#if defined(USE_GLOBAL_STRING_DEFS)
Elliott Hughes7e914f12011-01-19 18:18:42 -08003508 return handleExecuteInlineC(cUnit, mir);
Bill Buzbeefd023aa2009-11-02 09:23:49 -08003509#else
Bill Buzbeec6f10662010-02-09 11:16:15 -08003510 RegLocation rlThis = dvmCompilerGetSrc(cUnit, mir, 0);
3511 RegLocation rlChar = dvmCompilerGetSrc(cUnit, mir, 1);
Bill Buzbeefd023aa2009-11-02 09:23:49 -08003512
3513 loadValueDirectFixed(cUnit, rlThis, r0);
3514 loadValueDirectFixed(cUnit, rlChar, r1);
Elliott Hughes2bdbcb62010-04-12 14:29:37 -07003515 RegLocation rlStart = dvmCompilerGetSrc(cUnit, mir, 2);
3516 loadValueDirectFixed(cUnit, rlStart, r2);
Bill Buzbeefd023aa2009-11-02 09:23:49 -08003517 /* Test objects for NULL */
3518 genNullCheck(cUnit, rlThis.sRegLow, r0, mir->offset, NULL);
3519 genDispatchToHandler(cUnit, TEMPLATE_STRING_INDEXOF);
Bill Buzbeec6f10662010-02-09 11:16:15 -08003520 storeValue(cUnit, inlinedTarget(cUnit, mir, false),
3521 dvmCompilerGetReturn(cUnit));
Elliott Hughes7e914f12011-01-19 18:18:42 -08003522 return false;
Bill Buzbeefd023aa2009-11-02 09:23:49 -08003523#endif
3524}
3525
Elliott Hughesee34f592010-04-05 18:13:52 -07003526// Generates an inlined String.isEmpty or String.length.
3527static bool genInlinedStringIsEmptyOrLength(CompilationUnit *cUnit, MIR *mir,
3528 bool isEmpty)
Bill Buzbee1f748632010-03-02 16:14:41 -08003529{
Elliott Hughesee34f592010-04-05 18:13:52 -07003530 // dst = src.length();
Bill Buzbee1f748632010-03-02 16:14:41 -08003531 RegLocation rlObj = dvmCompilerGetSrc(cUnit, mir, 0);
3532 RegLocation rlDest = inlinedTarget(cUnit, mir, false);
3533 rlObj = loadValue(cUnit, rlObj, kCoreReg);
3534 RegLocation rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
3535 genNullCheck(cUnit, rlObj.sRegLow, rlObj.lowReg, mir->offset, NULL);
3536 loadWordDisp(cUnit, rlObj.lowReg, gDvm.offJavaLangString_count,
3537 rlResult.lowReg);
Elliott Hughesee34f592010-04-05 18:13:52 -07003538 if (isEmpty) {
3539 // dst = (dst == 0);
3540 int tReg = dvmCompilerAllocTemp(cUnit);
3541 opRegReg(cUnit, kOpNeg, tReg, rlResult.lowReg);
3542 opRegRegReg(cUnit, kOpAdc, rlResult.lowReg, rlResult.lowReg, tReg);
3543 }
Bill Buzbee1f748632010-03-02 16:14:41 -08003544 storeValue(cUnit, rlDest, rlResult);
3545 return false;
3546}
3547
Elliott Hughesee34f592010-04-05 18:13:52 -07003548static bool genInlinedStringLength(CompilationUnit *cUnit, MIR *mir)
3549{
3550 return genInlinedStringIsEmptyOrLength(cUnit, mir, false);
3551}
3552
3553static bool genInlinedStringIsEmpty(CompilationUnit *cUnit, MIR *mir)
3554{
3555 return genInlinedStringIsEmptyOrLength(cUnit, mir, true);
3556}
3557
Bill Buzbee1f748632010-03-02 16:14:41 -08003558static bool genInlinedStringCharAt(CompilationUnit *cUnit, MIR *mir)
3559{
3560 int contents = offsetof(ArrayObject, contents);
3561 RegLocation rlObj = dvmCompilerGetSrc(cUnit, mir, 0);
3562 RegLocation rlIdx = dvmCompilerGetSrc(cUnit, mir, 1);
3563 RegLocation rlDest = inlinedTarget(cUnit, mir, false);
3564 RegLocation rlResult;
3565 rlObj = loadValue(cUnit, rlObj, kCoreReg);
3566 rlIdx = loadValue(cUnit, rlIdx, kCoreReg);
3567 int regMax = dvmCompilerAllocTemp(cUnit);
3568 int regOff = dvmCompilerAllocTemp(cUnit);
3569 int regPtr = dvmCompilerAllocTemp(cUnit);
3570 ArmLIR *pcrLabel = genNullCheck(cUnit, rlObj.sRegLow, rlObj.lowReg,
3571 mir->offset, NULL);
3572 loadWordDisp(cUnit, rlObj.lowReg, gDvm.offJavaLangString_count, regMax);
3573 loadWordDisp(cUnit, rlObj.lowReg, gDvm.offJavaLangString_offset, regOff);
3574 loadWordDisp(cUnit, rlObj.lowReg, gDvm.offJavaLangString_value, regPtr);
3575 genBoundsCheck(cUnit, rlIdx.lowReg, regMax, mir->offset, pcrLabel);
3576 dvmCompilerFreeTemp(cUnit, regMax);
3577 opRegImm(cUnit, kOpAdd, regPtr, contents);
3578 opRegReg(cUnit, kOpAdd, regOff, rlIdx.lowReg);
3579 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
3580 loadBaseIndexed(cUnit, regPtr, regOff, rlResult.lowReg, 1, kUnsignedHalf);
3581 storeValue(cUnit, rlDest, rlResult);
3582 return false;
3583}
3584
3585static bool genInlinedAbsInt(CompilationUnit *cUnit, MIR *mir)
3586{
3587 RegLocation rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
3588 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
Elliott Hughese22bd842010-08-20 18:47:36 -07003589 RegLocation rlDest = inlinedTarget(cUnit, mir, false);
Bill Buzbee1f748632010-03-02 16:14:41 -08003590 RegLocation rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
3591 int signReg = dvmCompilerAllocTemp(cUnit);
3592 /*
3593 * abs(x) = y<=x>>31, (x+y)^y.
3594 * Thumb2's IT block also yields 3 instructions, but imposes
3595 * scheduling constraints.
3596 */
3597 opRegRegImm(cUnit, kOpAsr, signReg, rlSrc.lowReg, 31);
3598 opRegRegReg(cUnit, kOpAdd, rlResult.lowReg, rlSrc.lowReg, signReg);
3599 opRegReg(cUnit, kOpXor, rlResult.lowReg, signReg);
3600 storeValue(cUnit, rlDest, rlResult);
3601 return false;
3602}
3603
3604static bool genInlinedAbsLong(CompilationUnit *cUnit, MIR *mir)
3605{
3606 RegLocation rlSrc = dvmCompilerGetSrcWide(cUnit, mir, 0, 1);
3607 RegLocation rlDest = inlinedTargetWide(cUnit, mir, false);
3608 rlSrc = loadValueWide(cUnit, rlSrc, kCoreReg);
3609 RegLocation rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
3610 int signReg = dvmCompilerAllocTemp(cUnit);
3611 /*
3612 * abs(x) = y<=x>>31, (x+y)^y.
3613 * Thumb2 IT block allows slightly shorter sequence,
3614 * but introduces a scheduling barrier. Stick with this
3615 * mechanism for now.
3616 */
3617 opRegRegImm(cUnit, kOpAsr, signReg, rlSrc.highReg, 31);
3618 opRegRegReg(cUnit, kOpAdd, rlResult.lowReg, rlSrc.lowReg, signReg);
3619 opRegRegReg(cUnit, kOpAdc, rlResult.highReg, rlSrc.highReg, signReg);
3620 opRegReg(cUnit, kOpXor, rlResult.lowReg, signReg);
3621 opRegReg(cUnit, kOpXor, rlResult.highReg, signReg);
3622 storeValueWide(cUnit, rlDest, rlResult);
3623 return false;
3624}
Bill Buzbeefd023aa2009-11-02 09:23:49 -08003625
Elliott Hughese22bd842010-08-20 18:47:36 -07003626static bool genInlinedIntFloatConversion(CompilationUnit *cUnit, MIR *mir)
3627{
3628 // Just move from source to destination...
3629 RegLocation rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
3630 RegLocation rlDest = inlinedTarget(cUnit, mir, false);
3631 storeValue(cUnit, rlDest, rlSrc);
3632 return false;
3633}
3634
3635static bool genInlinedLongDoubleConversion(CompilationUnit *cUnit, MIR *mir)
3636{
3637 // Just move from source to destination...
3638 RegLocation rlSrc = dvmCompilerGetSrcWide(cUnit, mir, 0, 1);
3639 RegLocation rlDest = inlinedTargetWide(cUnit, mir, false);
3640 storeValueWide(cUnit, rlDest, rlSrc);
3641 return false;
3642}
3643
Bill Buzbeefd023aa2009-11-02 09:23:49 -08003644/*
Elliott Hughes7e914f12011-01-19 18:18:42 -08003645 * JITs a call to a C function.
3646 * TODO: use this for faster native method invocation for simple native
3647 * methods (http://b/3069458).
3648 */
3649static bool handleExecuteInlineC(CompilationUnit *cUnit, MIR *mir)
3650{
3651 DecodedInstruction *dInsn = &mir->dalvikInsn;
3652 int operation = dInsn->vB;
3653 unsigned int i;
3654 const InlineOperation* inLineTable = dvmGetInlineOpsTable();
3655 uintptr_t fn = (int) inLineTable[operation].func;
3656 if (fn == 0) {
3657 dvmCompilerAbort(cUnit);
3658 }
3659 dvmCompilerFlushAllRegs(cUnit); /* Everything to home location */
3660 dvmCompilerClobberCallRegs(cUnit);
3661 dvmCompilerClobber(cUnit, r4PC);
3662 dvmCompilerClobber(cUnit, r7);
buzbee9f601a92011-02-11 17:48:20 -08003663 int offset = offsetof(Thread, retval);
Ben Cheng20d7e6c2011-02-18 17:12:42 -08003664 opRegRegImm(cUnit, kOpAdd, r4PC, r6SELF, offset);
Elliott Hughes7e914f12011-01-19 18:18:42 -08003665 opImm(cUnit, kOpPush, (1<<r4PC) | (1<<r7));
3666 LOAD_FUNC_ADDR(cUnit, r4PC, fn);
3667 genExportPC(cUnit, mir);
3668 for (i=0; i < dInsn->vA; i++) {
3669 loadValueDirect(cUnit, dvmCompilerGetSrc(cUnit, mir, i), i);
3670 }
3671 opReg(cUnit, kOpBlx, r4PC);
Ben Cheng20d7e6c2011-02-18 17:12:42 -08003672 opRegImm(cUnit, kOpAdd, r13sp, 8);
Elliott Hughes7e914f12011-01-19 18:18:42 -08003673 /* NULL? */
3674 ArmLIR *branchOver = genCmpImmBranch(cUnit, kArmCondNe, r0, 0);
3675 loadConstant(cUnit, r0, (int) (cUnit->method->insns + mir->offset));
3676 genDispatchToHandler(cUnit, TEMPLATE_THROW_EXCEPTION_COMMON);
3677 ArmLIR *target = newLIR0(cUnit, kArmPseudoTargetLabel);
3678 target->defMask = ENCODE_ALL;
3679 branchOver->generic.target = (LIR *) target;
3680 return false;
3681}
3682
3683/*
Bill Buzbeece46c942009-11-20 15:41:34 -08003684 * NOTE: Handles both range and non-range versions (arguments
3685 * have already been normalized by this point).
Ben Chengba4fc8b2009-06-01 13:00:29 -07003686 */
Bill Buzbeece46c942009-11-20 15:41:34 -08003687static bool handleExecuteInline(CompilationUnit *cUnit, MIR *mir)
Ben Chengba4fc8b2009-06-01 13:00:29 -07003688{
3689 DecodedInstruction *dInsn = &mir->dalvikInsn;
Elliott Hughes7e914f12011-01-19 18:18:42 -08003690 assert(dInsn->opcode == OP_EXECUTE_INLINE_RANGE ||
3691 dInsn->opcode == OP_EXECUTE_INLINE);
3692 switch (dInsn->vB) {
3693 case INLINE_EMPTYINLINEMETHOD:
3694 return false; /* Nop */
3695
3696 /* These ones we potentially JIT inline. */
3697 case INLINE_STRING_LENGTH:
3698 return genInlinedStringLength(cUnit, mir);
3699 case INLINE_STRING_IS_EMPTY:
3700 return genInlinedStringIsEmpty(cUnit, mir);
3701 case INLINE_MATH_ABS_INT:
3702 return genInlinedAbsInt(cUnit, mir);
3703 case INLINE_MATH_ABS_LONG:
3704 return genInlinedAbsLong(cUnit, mir);
3705 case INLINE_MATH_MIN_INT:
3706 return genInlinedMinMaxInt(cUnit, mir, true);
3707 case INLINE_MATH_MAX_INT:
3708 return genInlinedMinMaxInt(cUnit, mir, false);
3709 case INLINE_STRING_CHARAT:
3710 return genInlinedStringCharAt(cUnit, mir);
3711 case INLINE_MATH_SQRT:
3712 return genInlineSqrt(cUnit, mir);
3713 case INLINE_MATH_ABS_FLOAT:
3714 return genInlinedAbsFloat(cUnit, mir);
3715 case INLINE_MATH_ABS_DOUBLE:
3716 return genInlinedAbsDouble(cUnit, mir);
3717 case INLINE_STRING_COMPARETO:
3718 return genInlinedCompareTo(cUnit, mir);
3719 case INLINE_STRING_FASTINDEXOF_II:
3720 return genInlinedFastIndexOf(cUnit, mir);
3721 case INLINE_FLOAT_TO_RAW_INT_BITS:
3722 case INLINE_INT_BITS_TO_FLOAT:
3723 return genInlinedIntFloatConversion(cUnit, mir);
3724 case INLINE_DOUBLE_TO_RAW_LONG_BITS:
3725 case INLINE_LONG_BITS_TO_DOUBLE:
3726 return genInlinedLongDoubleConversion(cUnit, mir);
3727
3728 /*
3729 * These ones we just JIT a call to a C function for.
3730 * TODO: special-case these in the other "invoke" call paths.
3731 */
3732 case INLINE_STRING_EQUALS:
3733 case INLINE_MATH_COS:
3734 case INLINE_MATH_SIN:
3735 case INLINE_FLOAT_TO_INT_BITS:
3736 case INLINE_DOUBLE_TO_LONG_BITS:
3737 return handleExecuteInlineC(cUnit, mir);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003738 }
Elliott Hughes7e914f12011-01-19 18:18:42 -08003739 dvmCompilerAbort(cUnit);
3740 return false; // Not reachable; keeps compiler happy.
Ben Chengba4fc8b2009-06-01 13:00:29 -07003741}
3742
3743static bool handleFmt51l(CompilationUnit *cUnit, MIR *mir)
3744{
Bill Buzbee1465db52009-09-23 17:17:35 -07003745 //TUNING: We're using core regs here - not optimal when target is a double
Bill Buzbeec6f10662010-02-09 11:16:15 -08003746 RegLocation rlDest = dvmCompilerGetDestWide(cUnit, mir, 0, 1);
3747 RegLocation rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Ben Chengbd1326d2010-04-02 15:04:53 -07003748 loadConstantNoClobber(cUnit, rlResult.lowReg,
3749 mir->dalvikInsn.vB_wide & 0xFFFFFFFFUL);
3750 loadConstantNoClobber(cUnit, rlResult.highReg,
3751 (mir->dalvikInsn.vB_wide>>32) & 0xFFFFFFFFUL);
Bill Buzbee1465db52009-09-23 17:17:35 -07003752 storeValueWide(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003753 return false;
3754}
3755
Ben Chengba4fc8b2009-06-01 13:00:29 -07003756/*
3757 * The following are special processing routines that handle transfer of
3758 * controls between compiled code and the interpreter. Certain VM states like
3759 * Dalvik PC and special-purpose registers are reconstructed here.
3760 */
3761
Bill Buzbeebd047242010-05-13 13:02:53 -07003762/*
3763 * Insert a
3764 * b .+4
3765 * nop
3766 * pair at the beginning of a chaining cell. This serves as the
3767 * switch branch that selects between reverting to the interpreter or
3768 * not. Once the cell is chained to a translation, the cell will
3769 * contain a 32-bit branch. Subsequent chain/unchain operations will
3770 * then only alter that first 16-bits - the "b .+4" for unchaining,
3771 * and the restoration of the first half of the 32-bit branch for
3772 * rechaining.
3773 */
3774static void insertChainingSwitch(CompilationUnit *cUnit)
3775{
3776 ArmLIR *branch = newLIR0(cUnit, kThumbBUncond);
3777 newLIR2(cUnit, kThumbOrr, r0, r0);
3778 ArmLIR *target = newLIR0(cUnit, kArmPseudoTargetLabel);
3779 target->defMask = ENCODE_ALL;
3780 branch->generic.target = (LIR *) target;
3781}
3782
Ben Cheng1efc9c52009-06-08 18:25:27 -07003783/* Chaining cell for code that may need warmup. */
3784static void handleNormalChainingCell(CompilationUnit *cUnit,
3785 unsigned int offset)
Ben Chengba4fc8b2009-06-01 13:00:29 -07003786{
Ben Cheng11d8f142010-03-24 15:24:19 -07003787 /*
3788 * Use raw instruction constructors to guarantee that the generated
3789 * instructions fit the predefined cell size.
3790 */
Bill Buzbeebd047242010-05-13 13:02:53 -07003791 insertChainingSwitch(cUnit);
Ben Cheng20d7e6c2011-02-18 17:12:42 -08003792 newLIR3(cUnit, kThumbLdrRRI5, r0, r6SELF,
buzbee9f601a92011-02-11 17:48:20 -08003793 offsetof(Thread,
Ben Cheng11d8f142010-03-24 15:24:19 -07003794 jitToInterpEntries.dvmJitToInterpNormal) >> 2);
3795 newLIR1(cUnit, kThumbBlxR, r0);
Ben Cheng385828e2011-03-04 16:48:33 -08003796 addWordData(cUnit, NULL, (int) (cUnit->method->insns + offset));
Ben Chengba4fc8b2009-06-01 13:00:29 -07003797}
3798
3799/*
Ben Cheng1efc9c52009-06-08 18:25:27 -07003800 * Chaining cell for instructions that immediately following already translated
3801 * code.
Ben Chengba4fc8b2009-06-01 13:00:29 -07003802 */
Ben Cheng1efc9c52009-06-08 18:25:27 -07003803static void handleHotChainingCell(CompilationUnit *cUnit,
3804 unsigned int offset)
Ben Chengba4fc8b2009-06-01 13:00:29 -07003805{
Ben Cheng11d8f142010-03-24 15:24:19 -07003806 /*
3807 * Use raw instruction constructors to guarantee that the generated
3808 * instructions fit the predefined cell size.
3809 */
Bill Buzbeebd047242010-05-13 13:02:53 -07003810 insertChainingSwitch(cUnit);
Ben Cheng20d7e6c2011-02-18 17:12:42 -08003811 newLIR3(cUnit, kThumbLdrRRI5, r0, r6SELF,
buzbee9f601a92011-02-11 17:48:20 -08003812 offsetof(Thread,
Ben Cheng11d8f142010-03-24 15:24:19 -07003813 jitToInterpEntries.dvmJitToInterpTraceSelect) >> 2);
3814 newLIR1(cUnit, kThumbBlxR, r0);
Ben Cheng385828e2011-03-04 16:48:33 -08003815 addWordData(cUnit, NULL, (int) (cUnit->method->insns + offset));
Ben Chengba4fc8b2009-06-01 13:00:29 -07003816}
3817
Jeff Hao97319a82009-08-12 16:57:15 -07003818/* Chaining cell for branches that branch back into the same basic block */
3819static void handleBackwardBranchChainingCell(CompilationUnit *cUnit,
3820 unsigned int offset)
3821{
Ben Cheng11d8f142010-03-24 15:24:19 -07003822 /*
3823 * Use raw instruction constructors to guarantee that the generated
3824 * instructions fit the predefined cell size.
3825 */
Bill Buzbeebd047242010-05-13 13:02:53 -07003826 insertChainingSwitch(cUnit);
Bill Buzbee9c4b7c82009-09-10 10:10:38 -07003827#if defined(WITH_SELF_VERIFICATION)
Ben Cheng20d7e6c2011-02-18 17:12:42 -08003828 newLIR3(cUnit, kThumbLdrRRI5, r0, r6SELF,
buzbee9f601a92011-02-11 17:48:20 -08003829 offsetof(Thread,
Ben Cheng40094c12010-02-24 20:58:44 -08003830 jitToInterpEntries.dvmJitToInterpBackwardBranch) >> 2);
Bill Buzbee9c4b7c82009-09-10 10:10:38 -07003831#else
Ben Cheng20d7e6c2011-02-18 17:12:42 -08003832 newLIR3(cUnit, kThumbLdrRRI5, r0, r6SELF,
buzbee9f601a92011-02-11 17:48:20 -08003833 offsetof(Thread, jitToInterpEntries.dvmJitToInterpNormal) >> 2);
Bill Buzbee9c4b7c82009-09-10 10:10:38 -07003834#endif
Bill Buzbee1465db52009-09-23 17:17:35 -07003835 newLIR1(cUnit, kThumbBlxR, r0);
Ben Cheng385828e2011-03-04 16:48:33 -08003836 addWordData(cUnit, NULL, (int) (cUnit->method->insns + offset));
Jeff Hao97319a82009-08-12 16:57:15 -07003837}
3838
Ben Chengba4fc8b2009-06-01 13:00:29 -07003839/* Chaining cell for monomorphic method invocations. */
Ben Cheng38329f52009-07-07 14:19:20 -07003840static void handleInvokeSingletonChainingCell(CompilationUnit *cUnit,
3841 const Method *callee)
Ben Chengba4fc8b2009-06-01 13:00:29 -07003842{
Ben Cheng11d8f142010-03-24 15:24:19 -07003843 /*
3844 * Use raw instruction constructors to guarantee that the generated
3845 * instructions fit the predefined cell size.
3846 */
Bill Buzbeebd047242010-05-13 13:02:53 -07003847 insertChainingSwitch(cUnit);
Ben Cheng20d7e6c2011-02-18 17:12:42 -08003848 newLIR3(cUnit, kThumbLdrRRI5, r0, r6SELF,
buzbee9f601a92011-02-11 17:48:20 -08003849 offsetof(Thread,
Ben Cheng11d8f142010-03-24 15:24:19 -07003850 jitToInterpEntries.dvmJitToInterpTraceSelect) >> 2);
3851 newLIR1(cUnit, kThumbBlxR, r0);
Ben Cheng385828e2011-03-04 16:48:33 -08003852 addWordData(cUnit, NULL, (int) (callee->insns));
Ben Chengba4fc8b2009-06-01 13:00:29 -07003853}
3854
Ben Cheng38329f52009-07-07 14:19:20 -07003855/* Chaining cell for monomorphic method invocations. */
3856static void handleInvokePredictedChainingCell(CompilationUnit *cUnit)
3857{
3858
3859 /* Should not be executed in the initial state */
Ben Cheng385828e2011-03-04 16:48:33 -08003860 addWordData(cUnit, NULL, PREDICTED_CHAIN_BX_PAIR_INIT);
Ben Cheng38329f52009-07-07 14:19:20 -07003861 /* To be filled: class */
Ben Cheng385828e2011-03-04 16:48:33 -08003862 addWordData(cUnit, NULL, PREDICTED_CHAIN_CLAZZ_INIT);
Ben Cheng38329f52009-07-07 14:19:20 -07003863 /* To be filled: method */
Ben Cheng385828e2011-03-04 16:48:33 -08003864 addWordData(cUnit, NULL, PREDICTED_CHAIN_METHOD_INIT);
Ben Cheng38329f52009-07-07 14:19:20 -07003865 /*
3866 * Rechain count. The initial value of 0 here will trigger chaining upon
3867 * the first invocation of this callsite.
3868 */
Ben Cheng385828e2011-03-04 16:48:33 -08003869 addWordData(cUnit, NULL, PREDICTED_CHAIN_COUNTER_INIT);
Ben Cheng38329f52009-07-07 14:19:20 -07003870}
3871
Ben Chengba4fc8b2009-06-01 13:00:29 -07003872/* Load the Dalvik PC into r0 and jump to the specified target */
3873static void handlePCReconstruction(CompilationUnit *cUnit,
Bill Buzbee89efc3d2009-07-28 11:22:22 -07003874 ArmLIR *targetLabel)
Ben Chengba4fc8b2009-06-01 13:00:29 -07003875{
Bill Buzbee89efc3d2009-07-28 11:22:22 -07003876 ArmLIR **pcrLabel =
3877 (ArmLIR **) cUnit->pcReconstructionList.elemList;
Ben Chengba4fc8b2009-06-01 13:00:29 -07003878 int numElems = cUnit->pcReconstructionList.numUsed;
3879 int i;
3880 for (i = 0; i < numElems; i++) {
3881 dvmCompilerAppendLIR(cUnit, (LIR *) pcrLabel[i]);
3882 /* r0 = dalvik PC */
3883 loadConstant(cUnit, r0, pcrLabel[i]->operands[0]);
3884 genUnconditionalBranch(cUnit, targetLabel);
3885 }
3886}
3887
Bill Buzbee1465db52009-09-23 17:17:35 -07003888static char *extendedMIROpNames[kMirOpLast - kMirOpFirst] = {
3889 "kMirOpPhi",
3890 "kMirOpNullNRangeUpCheck",
3891 "kMirOpNullNRangeDownCheck",
3892 "kMirOpLowerBound",
3893 "kMirOpPunt",
Ben Cheng7a2697d2010-06-07 13:44:23 -07003894 "kMirOpCheckInlinePrediction",
Ben Cheng4238ec22009-08-24 16:32:22 -07003895};
3896
3897/*
3898 * vA = arrayReg;
3899 * vB = idxReg;
3900 * vC = endConditionReg;
3901 * arg[0] = maxC
3902 * arg[1] = minC
3903 * arg[2] = loopBranchConditionCode
3904 */
3905static void genHoistedChecksForCountUpLoop(CompilationUnit *cUnit, MIR *mir)
3906{
Bill Buzbee1465db52009-09-23 17:17:35 -07003907 /*
3908 * NOTE: these synthesized blocks don't have ssa names assigned
3909 * for Dalvik registers. However, because they dominate the following
3910 * blocks we can simply use the Dalvik name w/ subscript 0 as the
3911 * ssa name.
3912 */
Ben Cheng4238ec22009-08-24 16:32:22 -07003913 DecodedInstruction *dInsn = &mir->dalvikInsn;
3914 const int lenOffset = offsetof(ArrayObject, length);
Ben Cheng4238ec22009-08-24 16:32:22 -07003915 const int maxC = dInsn->arg[0];
Bill Buzbee1465db52009-09-23 17:17:35 -07003916 int regLength;
3917 RegLocation rlArray = cUnit->regLocation[mir->dalvikInsn.vA];
3918 RegLocation rlIdxEnd = cUnit->regLocation[mir->dalvikInsn.vC];
Ben Cheng4238ec22009-08-24 16:32:22 -07003919
3920 /* regArray <- arrayRef */
Bill Buzbee1465db52009-09-23 17:17:35 -07003921 rlArray = loadValue(cUnit, rlArray, kCoreReg);
3922 rlIdxEnd = loadValue(cUnit, rlIdxEnd, kCoreReg);
3923 genRegImmCheck(cUnit, kArmCondEq, rlArray.lowReg, 0, 0,
Ben Cheng4238ec22009-08-24 16:32:22 -07003924 (ArmLIR *) cUnit->loopAnalysis->branchToPCR);
3925
3926 /* regLength <- len(arrayRef) */
Bill Buzbeec6f10662010-02-09 11:16:15 -08003927 regLength = dvmCompilerAllocTemp(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07003928 loadWordDisp(cUnit, rlArray.lowReg, lenOffset, regLength);
Ben Cheng4238ec22009-08-24 16:32:22 -07003929
3930 int delta = maxC;
3931 /*
3932 * If the loop end condition is ">=" instead of ">", then the largest value
3933 * of the index is "endCondition - 1".
3934 */
3935 if (dInsn->arg[2] == OP_IF_GE) {
3936 delta--;
3937 }
3938
3939 if (delta) {
Bill Buzbeec6f10662010-02-09 11:16:15 -08003940 int tReg = dvmCompilerAllocTemp(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07003941 opRegRegImm(cUnit, kOpAdd, tReg, rlIdxEnd.lowReg, delta);
3942 rlIdxEnd.lowReg = tReg;
Bill Buzbeec6f10662010-02-09 11:16:15 -08003943 dvmCompilerFreeTemp(cUnit, tReg);
Ben Cheng4238ec22009-08-24 16:32:22 -07003944 }
3945 /* Punt if "regIdxEnd < len(Array)" is false */
Bill Buzbee1465db52009-09-23 17:17:35 -07003946 genRegRegCheck(cUnit, kArmCondGe, rlIdxEnd.lowReg, regLength, 0,
Ben Cheng0fd31e42009-09-03 14:40:16 -07003947 (ArmLIR *) cUnit->loopAnalysis->branchToPCR);
Ben Cheng4238ec22009-08-24 16:32:22 -07003948}
3949
3950/*
3951 * vA = arrayReg;
3952 * vB = idxReg;
3953 * vC = endConditionReg;
3954 * arg[0] = maxC
3955 * arg[1] = minC
3956 * arg[2] = loopBranchConditionCode
3957 */
3958static void genHoistedChecksForCountDownLoop(CompilationUnit *cUnit, MIR *mir)
3959{
3960 DecodedInstruction *dInsn = &mir->dalvikInsn;
3961 const int lenOffset = offsetof(ArrayObject, length);
Bill Buzbeec6f10662010-02-09 11:16:15 -08003962 const int regLength = dvmCompilerAllocTemp(cUnit);
Ben Cheng4238ec22009-08-24 16:32:22 -07003963 const int maxC = dInsn->arg[0];
Bill Buzbee1465db52009-09-23 17:17:35 -07003964 RegLocation rlArray = cUnit->regLocation[mir->dalvikInsn.vA];
3965 RegLocation rlIdxInit = cUnit->regLocation[mir->dalvikInsn.vB];
Ben Cheng4238ec22009-08-24 16:32:22 -07003966
3967 /* regArray <- arrayRef */
Bill Buzbee1465db52009-09-23 17:17:35 -07003968 rlArray = loadValue(cUnit, rlArray, kCoreReg);
3969 rlIdxInit = loadValue(cUnit, rlIdxInit, kCoreReg);
3970 genRegImmCheck(cUnit, kArmCondEq, rlArray.lowReg, 0, 0,
Ben Cheng4238ec22009-08-24 16:32:22 -07003971 (ArmLIR *) cUnit->loopAnalysis->branchToPCR);
3972
3973 /* regLength <- len(arrayRef) */
Bill Buzbee1465db52009-09-23 17:17:35 -07003974 loadWordDisp(cUnit, rlArray.lowReg, lenOffset, regLength);
Ben Cheng4238ec22009-08-24 16:32:22 -07003975
3976 if (maxC) {
Bill Buzbeec6f10662010-02-09 11:16:15 -08003977 int tReg = dvmCompilerAllocTemp(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07003978 opRegRegImm(cUnit, kOpAdd, tReg, rlIdxInit.lowReg, maxC);
3979 rlIdxInit.lowReg = tReg;
Bill Buzbeec6f10662010-02-09 11:16:15 -08003980 dvmCompilerFreeTemp(cUnit, tReg);
Ben Cheng4238ec22009-08-24 16:32:22 -07003981 }
3982
3983 /* Punt if "regIdxInit < len(Array)" is false */
Bill Buzbee1465db52009-09-23 17:17:35 -07003984 genRegRegCheck(cUnit, kArmCondGe, rlIdxInit.lowReg, regLength, 0,
Ben Cheng0fd31e42009-09-03 14:40:16 -07003985 (ArmLIR *) cUnit->loopAnalysis->branchToPCR);
Ben Cheng4238ec22009-08-24 16:32:22 -07003986}
3987
3988/*
3989 * vA = idxReg;
3990 * vB = minC;
3991 */
3992static void genHoistedLowerBoundCheck(CompilationUnit *cUnit, MIR *mir)
3993{
3994 DecodedInstruction *dInsn = &mir->dalvikInsn;
Ben Cheng4238ec22009-08-24 16:32:22 -07003995 const int minC = dInsn->vB;
Bill Buzbee1465db52009-09-23 17:17:35 -07003996 RegLocation rlIdx = cUnit->regLocation[mir->dalvikInsn.vA];
Ben Cheng4238ec22009-08-24 16:32:22 -07003997
3998 /* regIdx <- initial index value */
Bill Buzbee1465db52009-09-23 17:17:35 -07003999 rlIdx = loadValue(cUnit, rlIdx, kCoreReg);
Ben Cheng4238ec22009-08-24 16:32:22 -07004000
4001 /* Punt if "regIdxInit + minC >= 0" is false */
Bill Buzbee1465db52009-09-23 17:17:35 -07004002 genRegImmCheck(cUnit, kArmCondLt, rlIdx.lowReg, -minC, 0,
Ben Cheng4238ec22009-08-24 16:32:22 -07004003 (ArmLIR *) cUnit->loopAnalysis->branchToPCR);
4004}
4005
Ben Cheng7a2697d2010-06-07 13:44:23 -07004006/*
4007 * vC = this
4008 *
4009 * A predicted inlining target looks like the following, where instructions
4010 * between 0x4858de66 and 0x4858de72 are checking if the predicted class
4011 * matches "this", and the verificaion code is generated by this routine.
4012 *
4013 * (C) means the instruction is inlined from the callee, and (PI) means the
4014 * instruction is the predicted inlined invoke, whose corresponding
4015 * instructions are still generated to handle the mispredicted case.
4016 *
4017 * D/dalvikvm( 86): -------- kMirOpCheckInlinePrediction
4018 * D/dalvikvm( 86): 0x4858de66 (0002): ldr r0, [r5, #68]
4019 * D/dalvikvm( 86): 0x4858de68 (0004): ldr r1, [pc, #140]
4020 * D/dalvikvm( 86): 0x4858de6a (0006): cmp r0, #0
4021 * D/dalvikvm( 86): 0x4858de6c (0008): beq 0x4858deb2
4022 * D/dalvikvm( 86): 0x4858de6e (000a): ldr r2, [r0, #0]
4023 * D/dalvikvm( 86): 0x4858de70 (000c): cmp r1, r2
4024 * D/dalvikvm( 86): 0x4858de72 (000e): bne 0x4858de7a
4025 * D/dalvikvm( 86): -------- dalvik offset: 0x004c @ +iget-object-quick (C)
4026 * v4, v17, (#8)
4027 * D/dalvikvm( 86): 0x4858de74 (0010): ldr r3, [r0, #8]
4028 * D/dalvikvm( 86): 0x4858de76 (0012): str r3, [r5, #16]
4029 * D/dalvikvm( 86): -------- dalvik offset: 0x004c @
4030 * +invoke-virtual-quick/range (PI) v17..v17
4031 * D/dalvikvm( 86): 0x4858de78 (0014): b 0x4858debc
4032 * D/dalvikvm( 86): 0x4858de7a (0016): add r4,r5,#68
4033 * D/dalvikvm( 86): -------- BARRIER
4034 * D/dalvikvm( 86): 0x4858de7e (001a): ldmia r4, <r0>
4035 * D/dalvikvm( 86): -------- BARRIER
4036 * D/dalvikvm( 86): 0x4858de80 (001c): sub r7,r5,#24
4037 * D/dalvikvm( 86): 0x4858de84 (0020): cmp r0, #0
4038 * D/dalvikvm( 86): 0x4858de86 (0022): beq 0x4858deb6
4039 * D/dalvikvm( 86): -------- BARRIER
4040 * D/dalvikvm( 86): 0x4858de88 (0024): stmia r7, <r0>
4041 * D/dalvikvm( 86): -------- BARRIER
4042 * D/dalvikvm( 86): 0x4858de8a (0026): ldr r4, [pc, #104]
4043 * D/dalvikvm( 86): 0x4858de8c (0028): add r1, pc, #28
4044 * D/dalvikvm( 86): 0x4858de8e (002a): add r2, pc, #56
4045 * D/dalvikvm( 86): 0x4858de90 (002c): blx_1 0x48589198
4046 * D/dalvikvm( 86): 0x4858de92 (002e): blx_2 see above
4047 * D/dalvikvm( 86): 0x4858de94 (0030): b 0x4858dec8
4048 * D/dalvikvm( 86): 0x4858de96 (0032): b 0x4858deb6
4049 * D/dalvikvm( 86): 0x4858de98 (0034): ldr r0, [r7, #72]
4050 * D/dalvikvm( 86): 0x4858de9a (0036): cmp r1, #0
4051 * D/dalvikvm( 86): 0x4858de9c (0038): bgt 0x4858dea4
4052 * D/dalvikvm( 86): 0x4858de9e (003a): ldr r7, [r6, #116]
4053 * D/dalvikvm( 86): 0x4858dea0 (003c): movs r1, r6
4054 * D/dalvikvm( 86): 0x4858dea2 (003e): blx r7
4055 * D/dalvikvm( 86): 0x4858dea4 (0040): add r1, pc, #4
4056 * D/dalvikvm( 86): 0x4858dea6 (0042): blx_1 0x485890a0
4057 * D/dalvikvm( 86): 0x4858dea8 (0044): blx_2 see above
4058 * D/dalvikvm( 86): 0x4858deaa (0046): b 0x4858deb6
4059 * D/dalvikvm( 86): 0x4858deac (0048): .align4
4060 * D/dalvikvm( 86): L0x004f:
4061 * D/dalvikvm( 86): -------- dalvik offset: 0x004f @ move-result-object (PI)
4062 * v4, (#0), (#0)
4063 * D/dalvikvm( 86): 0x4858deac (0048): ldr r4, [r6, #8]
4064 * D/dalvikvm( 86): 0x4858deae (004a): str r4, [r5, #16]
4065 * D/dalvikvm( 86): 0x4858deb0 (004c): b 0x4858debc
4066 * D/dalvikvm( 86): -------- reconstruct dalvik PC : 0x42beefcc @ +0x004c
4067 * D/dalvikvm( 86): 0x4858deb2 (004e): ldr r0, [pc, #64]
4068 * D/dalvikvm( 86): 0x4858deb4 (0050): b 0x4858deb8
4069 * D/dalvikvm( 86): -------- reconstruct dalvik PC : 0x42beefcc @ +0x004c
4070 * D/dalvikvm( 86): 0x4858deb6 (0052): ldr r0, [pc, #60]
4071 * D/dalvikvm( 86): Exception_Handling:
4072 * D/dalvikvm( 86): 0x4858deb8 (0054): ldr r1, [r6, #100]
4073 * D/dalvikvm( 86): 0x4858deba (0056): blx r1
4074 * D/dalvikvm( 86): 0x4858debc (0058): .align4
4075 * D/dalvikvm( 86): -------- chaining cell (hot): 0x0050
4076 * D/dalvikvm( 86): 0x4858debc (0058): b 0x4858dec0
4077 * D/dalvikvm( 86): 0x4858debe (005a): orrs r0, r0
4078 * D/dalvikvm( 86): 0x4858dec0 (005c): ldr r0, [r6, #112]
4079 * D/dalvikvm( 86): 0x4858dec2 (005e): blx r0
4080 * D/dalvikvm( 86): 0x4858dec4 (0060): data 0xefd4(61396)
4081 * D/dalvikvm( 86): 0x4858dec6 (0062): data 0x42be(17086)
4082 * D/dalvikvm( 86): 0x4858dec8 (0064): .align4
4083 * D/dalvikvm( 86): -------- chaining cell (predicted)
4084 * D/dalvikvm( 86): 0x4858dec8 (0064): data 0xe7fe(59390)
4085 * D/dalvikvm( 86): 0x4858deca (0066): data 0x0000(0)
4086 * D/dalvikvm( 86): 0x4858decc (0068): data 0x0000(0)
4087 * D/dalvikvm( 86): 0x4858dece (006a): data 0x0000(0)
4088 * :
4089 */
4090static void genValidationForPredictedInline(CompilationUnit *cUnit, MIR *mir)
4091{
4092 CallsiteInfo *callsiteInfo = mir->meta.callsiteInfo;
4093 RegLocation rlThis = cUnit->regLocation[mir->dalvikInsn.vC];
4094
4095 rlThis = loadValue(cUnit, rlThis, kCoreReg);
4096 int regPredictedClass = dvmCompilerAllocTemp(cUnit);
Ben Cheng385828e2011-03-04 16:48:33 -08004097 loadClassPointer(cUnit, regPredictedClass, (int) callsiteInfo);
Ben Cheng7a2697d2010-06-07 13:44:23 -07004098 genNullCheck(cUnit, rlThis.sRegLow, rlThis.lowReg, mir->offset,
4099 NULL);/* null object? */
4100 int regActualClass = dvmCompilerAllocTemp(cUnit);
4101 loadWordDisp(cUnit, rlThis.lowReg, offsetof(Object, clazz), regActualClass);
4102 opRegReg(cUnit, kOpCmp, regPredictedClass, regActualClass);
4103 /*
4104 * Set the misPredBranchOver target so that it will be generated when the
4105 * code for the non-optimized invoke is generated.
4106 */
4107 callsiteInfo->misPredBranchOver = (LIR *) opCondBranch(cUnit, kArmCondNe);
4108}
4109
Ben Cheng4238ec22009-08-24 16:32:22 -07004110/* Extended MIR instructions like PHI */
4111static void handleExtendedMIR(CompilationUnit *cUnit, MIR *mir)
4112{
Dan Bornstein9a1f8162010-12-01 17:02:26 -08004113 int opOffset = mir->dalvikInsn.opcode - kMirOpFirst;
Carl Shapirofc75f3e2010-12-07 11:43:38 -08004114 char *msg = (char *)dvmCompilerNew(strlen(extendedMIROpNames[opOffset]) + 1,
4115 false);
Ben Cheng4238ec22009-08-24 16:32:22 -07004116 strcpy(msg, extendedMIROpNames[opOffset]);
Bill Buzbee1465db52009-09-23 17:17:35 -07004117 newLIR1(cUnit, kArmPseudoExtended, (int) msg);
Ben Cheng4238ec22009-08-24 16:32:22 -07004118
Dan Bornstein9a1f8162010-12-01 17:02:26 -08004119 switch (mir->dalvikInsn.opcode) {
Bill Buzbee1465db52009-09-23 17:17:35 -07004120 case kMirOpPhi: {
Ben Cheng4238ec22009-08-24 16:32:22 -07004121 char *ssaString = dvmCompilerGetSSAString(cUnit, mir->ssaRep);
Bill Buzbee1465db52009-09-23 17:17:35 -07004122 newLIR1(cUnit, kArmPseudoSSARep, (int) ssaString);
Ben Cheng4238ec22009-08-24 16:32:22 -07004123 break;
4124 }
Bill Buzbee1465db52009-09-23 17:17:35 -07004125 case kMirOpNullNRangeUpCheck: {
Ben Cheng4238ec22009-08-24 16:32:22 -07004126 genHoistedChecksForCountUpLoop(cUnit, mir);
4127 break;
4128 }
Bill Buzbee1465db52009-09-23 17:17:35 -07004129 case kMirOpNullNRangeDownCheck: {
Ben Cheng4238ec22009-08-24 16:32:22 -07004130 genHoistedChecksForCountDownLoop(cUnit, mir);
4131 break;
4132 }
Bill Buzbee1465db52009-09-23 17:17:35 -07004133 case kMirOpLowerBound: {
Ben Cheng4238ec22009-08-24 16:32:22 -07004134 genHoistedLowerBoundCheck(cUnit, mir);
4135 break;
4136 }
Bill Buzbee1465db52009-09-23 17:17:35 -07004137 case kMirOpPunt: {
Ben Cheng4238ec22009-08-24 16:32:22 -07004138 genUnconditionalBranch(cUnit,
4139 (ArmLIR *) cUnit->loopAnalysis->branchToPCR);
4140 break;
4141 }
Ben Cheng7a2697d2010-06-07 13:44:23 -07004142 case kMirOpCheckInlinePrediction: {
4143 genValidationForPredictedInline(cUnit, mir);
4144 break;
4145 }
Ben Cheng4238ec22009-08-24 16:32:22 -07004146 default:
4147 break;
4148 }
4149}
4150
4151/*
4152 * Create a PC-reconstruction cell for the starting offset of this trace.
4153 * Since the PCR cell is placed near the end of the compiled code which is
4154 * usually out of range for a conditional branch, we put two branches (one
4155 * branch over to the loop body and one layover branch to the actual PCR) at the
4156 * end of the entry block.
4157 */
4158static void setupLoopEntryBlock(CompilationUnit *cUnit, BasicBlock *entry,
4159 ArmLIR *bodyLabel)
4160{
4161 /* Set up the place holder to reconstruct this Dalvik PC */
Carl Shapirofc75f3e2010-12-07 11:43:38 -08004162 ArmLIR *pcrLabel = (ArmLIR *) dvmCompilerNew(sizeof(ArmLIR), true);
Dan Bornstein9a1f8162010-12-01 17:02:26 -08004163 pcrLabel->opcode = kArmPseudoPCReconstructionCell;
Ben Cheng4238ec22009-08-24 16:32:22 -07004164 pcrLabel->operands[0] =
4165 (int) (cUnit->method->insns + entry->startOffset);
4166 pcrLabel->operands[1] = entry->startOffset;
4167 /* Insert the place holder to the growable list */
Ben Cheng00603072010-10-28 11:13:58 -07004168 dvmInsertGrowableList(&cUnit->pcReconstructionList, (intptr_t) pcrLabel);
Ben Cheng4238ec22009-08-24 16:32:22 -07004169
4170 /*
4171 * Next, create two branches - one branch over to the loop body and the
4172 * other branch to the PCR cell to punt.
4173 */
Carl Shapirofc75f3e2010-12-07 11:43:38 -08004174 ArmLIR *branchToBody = (ArmLIR *) dvmCompilerNew(sizeof(ArmLIR), true);
Dan Bornstein9a1f8162010-12-01 17:02:26 -08004175 branchToBody->opcode = kThumbBUncond;
Ben Cheng4238ec22009-08-24 16:32:22 -07004176 branchToBody->generic.target = (LIR *) bodyLabel;
Ben Chengdcf3e5d2009-09-11 13:42:05 -07004177 setupResourceMasks(branchToBody);
Ben Cheng4238ec22009-08-24 16:32:22 -07004178 cUnit->loopAnalysis->branchToBody = (LIR *) branchToBody;
4179
Carl Shapirofc75f3e2010-12-07 11:43:38 -08004180 ArmLIR *branchToPCR = (ArmLIR *) dvmCompilerNew(sizeof(ArmLIR), true);
Dan Bornstein9a1f8162010-12-01 17:02:26 -08004181 branchToPCR->opcode = kThumbBUncond;
Ben Cheng4238ec22009-08-24 16:32:22 -07004182 branchToPCR->generic.target = (LIR *) pcrLabel;
Ben Chengdcf3e5d2009-09-11 13:42:05 -07004183 setupResourceMasks(branchToPCR);
Ben Cheng4238ec22009-08-24 16:32:22 -07004184 cUnit->loopAnalysis->branchToPCR = (LIR *) branchToPCR;
4185}
4186
Ben Chengd5adae12010-03-26 17:45:28 -07004187#if defined(WITH_SELF_VERIFICATION)
4188static bool selfVerificationPuntOps(MIR *mir)
4189{
4190 DecodedInstruction *decInsn = &mir->dalvikInsn;
Ben Cheng7a2697d2010-06-07 13:44:23 -07004191
Ben Chengd5adae12010-03-26 17:45:28 -07004192 /*
4193 * All opcodes that can throw exceptions and use the
4194 * TEMPLATE_THROW_EXCEPTION_COMMON template should be excluded in the trace
4195 * under self-verification mode.
4196 */
Ben Cheng072b5d02011-03-31 10:44:44 -07004197 switch (decInsn->opcode) {
4198 case OP_MONITOR_ENTER:
4199 case OP_MONITOR_EXIT:
4200 case OP_NEW_INSTANCE:
4201 case OP_NEW_INSTANCE_JUMBO:
4202 case OP_NEW_ARRAY:
4203 case OP_NEW_ARRAY_JUMBO:
4204 case OP_CHECK_CAST:
4205 case OP_CHECK_CAST_JUMBO:
4206 case OP_MOVE_EXCEPTION:
4207 case OP_FILL_ARRAY_DATA:
4208 case OP_EXECUTE_INLINE:
4209 case OP_EXECUTE_INLINE_RANGE:
4210 return true;
4211 default:
4212 return false;
4213 }
Ben Chengd5adae12010-03-26 17:45:28 -07004214}
4215#endif
4216
Ben Chengba4fc8b2009-06-01 13:00:29 -07004217void dvmCompilerMIR2LIR(CompilationUnit *cUnit)
4218{
4219 /* Used to hold the labels of each block */
Bill Buzbee89efc3d2009-07-28 11:22:22 -07004220 ArmLIR *labelList =
Carl Shapirofc75f3e2010-12-07 11:43:38 -08004221 (ArmLIR *) dvmCompilerNew(sizeof(ArmLIR) * cUnit->numBlocks, true);
Ben Chengcec26f62010-01-15 15:29:33 -08004222 GrowableList chainingListByType[kChainingCellGap];
Ben Chengba4fc8b2009-06-01 13:00:29 -07004223 int i;
4224
4225 /*
Ben Cheng38329f52009-07-07 14:19:20 -07004226 * Initialize various types chaining lists.
Ben Chengba4fc8b2009-06-01 13:00:29 -07004227 */
Ben Chengcec26f62010-01-15 15:29:33 -08004228 for (i = 0; i < kChainingCellGap; i++) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07004229 dvmInitGrowableList(&chainingListByType[i], 2);
4230 }
4231
Ben Cheng7ab74e12011-02-03 14:02:06 -08004232 /* Clear the visited flag for each block */
4233 dvmCompilerDataFlowAnalysisDispatcher(cUnit, dvmCompilerClearVisitedFlag,
4234 kAllNodes, false /* isIterative */);
4235
Ben Cheng00603072010-10-28 11:13:58 -07004236 GrowableListIterator iterator;
4237 dvmGrowableListIteratorInit(&cUnit->blockList, &iterator);
Ben Chengba4fc8b2009-06-01 13:00:29 -07004238
buzbee2e152ba2010-12-15 16:32:35 -08004239 /* Traces start with a profiling entry point. Generate it here */
4240 cUnit->profileCodeSize = genTraceProfileEntry(cUnit);
Ben Cheng1efc9c52009-06-08 18:25:27 -07004241
Ben Chengba4fc8b2009-06-01 13:00:29 -07004242 /* Handle the content in each basic block */
Ben Cheng00603072010-10-28 11:13:58 -07004243 for (i = 0; ; i++) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07004244 MIR *mir;
Ben Cheng00603072010-10-28 11:13:58 -07004245 BasicBlock *bb = (BasicBlock *) dvmGrowableListIteratorNext(&iterator);
4246 if (bb == NULL) break;
Ben Cheng7ab74e12011-02-03 14:02:06 -08004247 if (bb->visited == true) continue;
Ben Chengba4fc8b2009-06-01 13:00:29 -07004248
Ben Cheng00603072010-10-28 11:13:58 -07004249 labelList[i].operands[0] = bb->startOffset;
Ben Chengba4fc8b2009-06-01 13:00:29 -07004250
Ben Cheng00603072010-10-28 11:13:58 -07004251 if (bb->blockType >= kChainingCellGap) {
4252 if (bb->isFallThroughFromInvoke == true) {
Ben Chengd44faf52010-06-02 15:33:51 -07004253 /* Align this block first since it is a return chaining cell */
4254 newLIR0(cUnit, kArmPseudoPseudoAlign4);
4255 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07004256 /*
4257 * Append the label pseudo LIR first. Chaining cells will be handled
4258 * separately afterwards.
4259 */
4260 dvmCompilerAppendLIR(cUnit, (LIR *) &labelList[i]);
4261 }
4262
Ben Cheng32115a92011-03-22 14:09:09 -07004263 if (bb->blockType == kEntryBlock) {
Dan Bornstein9a1f8162010-12-01 17:02:26 -08004264 labelList[i].opcode = kArmPseudoEntryBlock;
Ben Cheng00603072010-10-28 11:13:58 -07004265 if (bb->firstMIRInsn == NULL) {
Ben Cheng4238ec22009-08-24 16:32:22 -07004266 continue;
4267 } else {
Ben Cheng00603072010-10-28 11:13:58 -07004268 setupLoopEntryBlock(cUnit, bb,
4269 &labelList[bb->fallThrough->id]);
Ben Cheng4238ec22009-08-24 16:32:22 -07004270 }
Ben Cheng32115a92011-03-22 14:09:09 -07004271 } else if (bb->blockType == kExitBlock) {
Dan Bornstein9a1f8162010-12-01 17:02:26 -08004272 labelList[i].opcode = kArmPseudoExitBlock;
Ben Cheng4238ec22009-08-24 16:32:22 -07004273 goto gen_fallthrough;
Ben Cheng00603072010-10-28 11:13:58 -07004274 } else if (bb->blockType == kDalvikByteCode) {
Ben Cheng32115a92011-03-22 14:09:09 -07004275 if (bb->hidden == true) continue;
Dan Bornstein9a1f8162010-12-01 17:02:26 -08004276 labelList[i].opcode = kArmPseudoNormalBlockLabel;
Ben Chenge9695e52009-06-16 16:11:47 -07004277 /* Reset the register state */
Bill Buzbeec6f10662010-02-09 11:16:15 -08004278 dvmCompilerResetRegPool(cUnit);
4279 dvmCompilerClobberAllRegs(cUnit);
4280 dvmCompilerResetNullCheck(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -07004281 } else {
Ben Cheng00603072010-10-28 11:13:58 -07004282 switch (bb->blockType) {
Bill Buzbee1465db52009-09-23 17:17:35 -07004283 case kChainingCellNormal:
Dan Bornstein9a1f8162010-12-01 17:02:26 -08004284 labelList[i].opcode = kArmPseudoChainingCellNormal;
Ben Chengba4fc8b2009-06-01 13:00:29 -07004285 /* handle the codegen later */
4286 dvmInsertGrowableList(
Ben Cheng00603072010-10-28 11:13:58 -07004287 &chainingListByType[kChainingCellNormal], i);
Ben Chengba4fc8b2009-06-01 13:00:29 -07004288 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07004289 case kChainingCellInvokeSingleton:
Dan Bornstein9a1f8162010-12-01 17:02:26 -08004290 labelList[i].opcode =
Ben Chenga4973592010-03-31 11:59:18 -07004291 kArmPseudoChainingCellInvokeSingleton;
Ben Chengba4fc8b2009-06-01 13:00:29 -07004292 labelList[i].operands[0] =
Ben Cheng00603072010-10-28 11:13:58 -07004293 (int) bb->containingMethod;
Ben Chengba4fc8b2009-06-01 13:00:29 -07004294 /* handle the codegen later */
4295 dvmInsertGrowableList(
Ben Cheng00603072010-10-28 11:13:58 -07004296 &chainingListByType[kChainingCellInvokeSingleton], i);
Ben Cheng38329f52009-07-07 14:19:20 -07004297 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07004298 case kChainingCellInvokePredicted:
Dan Bornstein9a1f8162010-12-01 17:02:26 -08004299 labelList[i].opcode =
Ben Chenga4973592010-03-31 11:59:18 -07004300 kArmPseudoChainingCellInvokePredicted;
Ben Cheng04517042011-03-14 11:16:21 -07004301 /*
4302 * Move the cached method pointer from operand 1 to 0.
4303 * Operand 0 was clobbered earlier in this routine to store
4304 * the block starting offset, which is not applicable to
4305 * predicted chaining cell.
4306 */
4307 labelList[i].operands[0] = labelList[i].operands[1];
Ben Cheng38329f52009-07-07 14:19:20 -07004308 /* handle the codegen later */
4309 dvmInsertGrowableList(
Ben Cheng00603072010-10-28 11:13:58 -07004310 &chainingListByType[kChainingCellInvokePredicted], i);
Ben Chengba4fc8b2009-06-01 13:00:29 -07004311 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07004312 case kChainingCellHot:
Dan Bornstein9a1f8162010-12-01 17:02:26 -08004313 labelList[i].opcode =
Ben Chenga4973592010-03-31 11:59:18 -07004314 kArmPseudoChainingCellHot;
Ben Chengba4fc8b2009-06-01 13:00:29 -07004315 /* handle the codegen later */
4316 dvmInsertGrowableList(
Ben Cheng00603072010-10-28 11:13:58 -07004317 &chainingListByType[kChainingCellHot], i);
Ben Chengba4fc8b2009-06-01 13:00:29 -07004318 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07004319 case kPCReconstruction:
Ben Chengba4fc8b2009-06-01 13:00:29 -07004320 /* Make sure exception handling block is next */
Dan Bornstein9a1f8162010-12-01 17:02:26 -08004321 labelList[i].opcode =
Ben Chenga4973592010-03-31 11:59:18 -07004322 kArmPseudoPCReconstructionBlockLabel;
Ben Cheng32115a92011-03-22 14:09:09 -07004323 handlePCReconstruction(cUnit,
4324 &labelList[cUnit->puntBlock->id]);
Ben Chengba4fc8b2009-06-01 13:00:29 -07004325 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07004326 case kExceptionHandling:
Dan Bornstein9a1f8162010-12-01 17:02:26 -08004327 labelList[i].opcode = kArmPseudoEHBlockLabel;
Ben Chengba4fc8b2009-06-01 13:00:29 -07004328 if (cUnit->pcReconstructionList.numUsed) {
Ben Cheng20d7e6c2011-02-18 17:12:42 -08004329 loadWordDisp(cUnit, r6SELF, offsetof(Thread,
Bill Buzbee270c1d62009-08-13 16:58:07 -07004330 jitToInterpEntries.dvmJitToInterpPunt),
4331 r1);
Bill Buzbee1465db52009-09-23 17:17:35 -07004332 opReg(cUnit, kOpBlx, r1);
Ben Chengba4fc8b2009-06-01 13:00:29 -07004333 }
4334 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07004335 case kChainingCellBackwardBranch:
Dan Bornstein9a1f8162010-12-01 17:02:26 -08004336 labelList[i].opcode =
Ben Chenga4973592010-03-31 11:59:18 -07004337 kArmPseudoChainingCellBackwardBranch;
Jeff Hao97319a82009-08-12 16:57:15 -07004338 /* handle the codegen later */
4339 dvmInsertGrowableList(
Bill Buzbee1465db52009-09-23 17:17:35 -07004340 &chainingListByType[kChainingCellBackwardBranch],
Ben Cheng00603072010-10-28 11:13:58 -07004341 i);
Jeff Hao97319a82009-08-12 16:57:15 -07004342 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07004343 default:
4344 break;
4345 }
4346 continue;
4347 }
Ben Chenge9695e52009-06-16 16:11:47 -07004348
Bill Buzbee89efc3d2009-07-28 11:22:22 -07004349 ArmLIR *headLIR = NULL;
Ben Cheng7ab74e12011-02-03 14:02:06 -08004350 BasicBlock *nextBB = bb;
Ben Chenge9695e52009-06-16 16:11:47 -07004351
Ben Cheng7ab74e12011-02-03 14:02:06 -08004352 /*
4353 * Try to build a longer optimization unit. Currently if the previous
4354 * block ends with a goto, we continue adding instructions and don't
4355 * reset the register allocation pool.
4356 */
4357 for (; nextBB != NULL; nextBB = cUnit->nextCodegenBlock) {
4358 bb = nextBB;
4359 bb->visited = true;
4360 cUnit->nextCodegenBlock = NULL;
Bill Buzbee1465db52009-09-23 17:17:35 -07004361
Ben Cheng7ab74e12011-02-03 14:02:06 -08004362 for (mir = bb->firstMIRInsn; mir; mir = mir->next) {
Bill Buzbee1465db52009-09-23 17:17:35 -07004363
Ben Cheng7ab74e12011-02-03 14:02:06 -08004364 dvmCompilerResetRegPool(cUnit);
4365 if (gDvmJit.disableOpt & (1 << kTrackLiveTemps)) {
4366 dvmCompilerClobberAllRegs(cUnit);
Ben Cheng80211d22011-01-14 10:23:37 -08004367 }
Ben Cheng4238ec22009-08-24 16:32:22 -07004368
Ben Cheng7ab74e12011-02-03 14:02:06 -08004369 if (gDvmJit.disableOpt & (1 << kSuppressLoads)) {
4370 dvmCompilerResetDefTracking(cUnit);
4371 }
Ben Cheng4238ec22009-08-24 16:32:22 -07004372
Ben Cheng7ab74e12011-02-03 14:02:06 -08004373 if (mir->dalvikInsn.opcode >= kMirOpFirst) {
4374 handleExtendedMIR(cUnit, mir);
4375 continue;
4376 }
4377
4378
4379 Opcode dalvikOpcode = mir->dalvikInsn.opcode;
4380 InstructionFormat dalvikFormat =
4381 dexGetFormatFromOpcode(dalvikOpcode);
4382 char *note;
4383 if (mir->OptimizationFlags & MIR_INLINED) {
4384 note = " (I)";
4385 } else if (mir->OptimizationFlags & MIR_INLINED_PRED) {
4386 note = " (PI)";
4387 } else if (mir->OptimizationFlags & MIR_CALLEE) {
4388 note = " (C)";
4389 } else {
4390 note = NULL;
4391 }
4392
4393 ArmLIR *boundaryLIR;
4394
4395 /*
4396 * Don't generate the boundary LIR unless we are debugging this
4397 * trace or we need a scheduling barrier.
4398 */
4399 if (headLIR == NULL || cUnit->printMe == true) {
4400 boundaryLIR =
4401 newLIR2(cUnit, kArmPseudoDalvikByteCodeBoundary,
4402 mir->offset,
4403 (int) dvmCompilerGetDalvikDisassembly(
4404 &mir->dalvikInsn, note));
4405 /* Remember the first LIR for this block */
4406 if (headLIR == NULL) {
4407 headLIR = boundaryLIR;
4408 /* Set the first boundaryLIR as a scheduling barrier */
4409 headLIR->defMask = ENCODE_ALL;
4410 }
4411 }
4412
4413 /*
4414 * Don't generate the SSA annotation unless verbose mode is on
4415 */
4416 if (cUnit->printMe && mir->ssaRep) {
4417 char *ssaString = dvmCompilerGetSSAString(cUnit,
4418 mir->ssaRep);
4419 newLIR1(cUnit, kArmPseudoSSARep, (int) ssaString);
4420 }
4421
4422 bool notHandled;
4423 /*
4424 * Debugging: screen the opcode first to see if it is in the
4425 * do[-not]-compile list
4426 */
4427 bool singleStepMe = SINGLE_STEP_OP(dalvikOpcode);
Ben Chengd5adae12010-03-26 17:45:28 -07004428#if defined(WITH_SELF_VERIFICATION)
Ben Cheng7ab74e12011-02-03 14:02:06 -08004429 if (singleStepMe == false) {
4430 singleStepMe = selfVerificationPuntOps(mir);
4431 }
Ben Chengd5adae12010-03-26 17:45:28 -07004432#endif
Ben Cheng7ab74e12011-02-03 14:02:06 -08004433 if (singleStepMe || cUnit->allSingleStep) {
4434 notHandled = false;
4435 genInterpSingleStep(cUnit, mir);
4436 } else {
4437 opcodeCoverage[dalvikOpcode]++;
4438 switch (dalvikFormat) {
4439 case kFmt10t:
4440 case kFmt20t:
4441 case kFmt30t:
4442 notHandled = handleFmt10t_Fmt20t_Fmt30t(cUnit,
4443 mir, bb, labelList);
4444 break;
4445 case kFmt10x:
4446 notHandled = handleFmt10x(cUnit, mir);
4447 break;
4448 case kFmt11n:
4449 case kFmt31i:
4450 notHandled = handleFmt11n_Fmt31i(cUnit, mir);
4451 break;
4452 case kFmt11x:
4453 notHandled = handleFmt11x(cUnit, mir);
4454 break;
4455 case kFmt12x:
4456 notHandled = handleFmt12x(cUnit, mir);
4457 break;
4458 case kFmt20bc:
4459 case kFmt40sc:
4460 notHandled = handleFmt20bc_Fmt40sc(cUnit, mir);
4461 break;
4462 case kFmt21c:
4463 case kFmt31c:
4464 case kFmt41c:
4465 notHandled = handleFmt21c_Fmt31c_Fmt41c(cUnit, mir);
4466 break;
4467 case kFmt21h:
4468 notHandled = handleFmt21h(cUnit, mir);
4469 break;
4470 case kFmt21s:
4471 notHandled = handleFmt21s(cUnit, mir);
4472 break;
4473 case kFmt21t:
4474 notHandled = handleFmt21t(cUnit, mir, bb,
Ben Chengba4fc8b2009-06-01 13:00:29 -07004475 labelList);
Ben Cheng7ab74e12011-02-03 14:02:06 -08004476 break;
4477 case kFmt22b:
4478 case kFmt22s:
4479 notHandled = handleFmt22b_Fmt22s(cUnit, mir);
4480 break;
4481 case kFmt22c:
4482 case kFmt52c:
4483 notHandled = handleFmt22c_Fmt52c(cUnit, mir);
4484 break;
4485 case kFmt22cs:
4486 notHandled = handleFmt22cs(cUnit, mir);
4487 break;
4488 case kFmt22t:
4489 notHandled = handleFmt22t(cUnit, mir, bb,
4490 labelList);
4491 break;
4492 case kFmt22x:
4493 case kFmt32x:
4494 notHandled = handleFmt22x_Fmt32x(cUnit, mir);
4495 break;
4496 case kFmt23x:
4497 notHandled = handleFmt23x(cUnit, mir);
4498 break;
4499 case kFmt31t:
4500 notHandled = handleFmt31t(cUnit, mir);
4501 break;
4502 case kFmt3rc:
4503 case kFmt35c:
4504 case kFmt5rc:
4505 notHandled = handleFmt35c_3rc_5rc(cUnit, mir, bb,
4506 labelList);
4507 break;
4508 case kFmt3rms:
4509 case kFmt35ms:
4510 notHandled = handleFmt35ms_3rms(cUnit, mir, bb,
4511 labelList);
4512 break;
4513 case kFmt35mi:
4514 case kFmt3rmi:
4515 notHandled = handleExecuteInline(cUnit, mir);
4516 break;
4517 case kFmt51l:
4518 notHandled = handleFmt51l(cUnit, mir);
4519 break;
4520 default:
4521 notHandled = true;
4522 break;
4523 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07004524 }
Ben Cheng7ab74e12011-02-03 14:02:06 -08004525 if (notHandled) {
4526 LOGE("%#06x: Opcode 0x%x (%s) / Fmt %d not handled\n",
4527 mir->offset,
4528 dalvikOpcode, dexGetOpcodeName(dalvikOpcode),
4529 dalvikFormat);
4530 dvmCompilerAbort(cUnit);
4531 break;
4532 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07004533 }
4534 }
Ben Cheng4238ec22009-08-24 16:32:22 -07004535
Ben Cheng32115a92011-03-22 14:09:09 -07004536 if (bb->blockType == kEntryBlock) {
Ben Cheng4238ec22009-08-24 16:32:22 -07004537 dvmCompilerAppendLIR(cUnit,
4538 (LIR *) cUnit->loopAnalysis->branchToBody);
4539 dvmCompilerAppendLIR(cUnit,
4540 (LIR *) cUnit->loopAnalysis->branchToPCR);
4541 }
4542
4543 if (headLIR) {
4544 /*
4545 * Eliminate redundant loads/stores and delay stores into later
4546 * slots
4547 */
4548 dvmCompilerApplyLocalOptimizations(cUnit, (LIR *) headLIR,
4549 cUnit->lastLIRInsn);
4550 }
4551
4552gen_fallthrough:
Ben Cheng1efc9c52009-06-08 18:25:27 -07004553 /*
4554 * Check if the block is terminated due to trace length constraint -
4555 * insert an unconditional branch to the chaining cell.
4556 */
Ben Cheng00603072010-10-28 11:13:58 -07004557 if (bb->needFallThroughBranch) {
Ben Cheng7ab74e12011-02-03 14:02:06 -08004558 genUnconditionalBranch(cUnit, &labelList[bb->fallThrough->id]);
Ben Cheng1efc9c52009-06-08 18:25:27 -07004559 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07004560 }
4561
Ben Chenge9695e52009-06-16 16:11:47 -07004562 /* Handle the chaining cells in predefined order */
Ben Chengcec26f62010-01-15 15:29:33 -08004563 for (i = 0; i < kChainingCellGap; i++) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07004564 size_t j;
4565 int *blockIdList = (int *) chainingListByType[i].elemList;
4566
4567 cUnit->numChainingCells[i] = chainingListByType[i].numUsed;
4568
4569 /* No chaining cells of this type */
4570 if (cUnit->numChainingCells[i] == 0)
4571 continue;
4572
4573 /* Record the first LIR for a new type of chaining cell */
4574 cUnit->firstChainingLIR[i] = (LIR *) &labelList[blockIdList[0]];
4575
4576 for (j = 0; j < chainingListByType[i].numUsed; j++) {
4577 int blockId = blockIdList[j];
Ben Cheng00603072010-10-28 11:13:58 -07004578 BasicBlock *chainingBlock =
4579 (BasicBlock *) dvmGrowableListGetElement(&cUnit->blockList,
4580 blockId);
Ben Chengba4fc8b2009-06-01 13:00:29 -07004581
4582 /* Align this chaining cell first */
Bill Buzbee1465db52009-09-23 17:17:35 -07004583 newLIR0(cUnit, kArmPseudoPseudoAlign4);
Ben Chengba4fc8b2009-06-01 13:00:29 -07004584
4585 /* Insert the pseudo chaining instruction */
4586 dvmCompilerAppendLIR(cUnit, (LIR *) &labelList[blockId]);
4587
4588
Ben Cheng00603072010-10-28 11:13:58 -07004589 switch (chainingBlock->blockType) {
Bill Buzbee1465db52009-09-23 17:17:35 -07004590 case kChainingCellNormal:
Ben Cheng00603072010-10-28 11:13:58 -07004591 handleNormalChainingCell(cUnit, chainingBlock->startOffset);
Ben Chengba4fc8b2009-06-01 13:00:29 -07004592 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07004593 case kChainingCellInvokeSingleton:
Ben Cheng38329f52009-07-07 14:19:20 -07004594 handleInvokeSingletonChainingCell(cUnit,
Ben Cheng00603072010-10-28 11:13:58 -07004595 chainingBlock->containingMethod);
Ben Chengba4fc8b2009-06-01 13:00:29 -07004596 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07004597 case kChainingCellInvokePredicted:
Ben Cheng38329f52009-07-07 14:19:20 -07004598 handleInvokePredictedChainingCell(cUnit);
4599 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07004600 case kChainingCellHot:
Ben Cheng00603072010-10-28 11:13:58 -07004601 handleHotChainingCell(cUnit, chainingBlock->startOffset);
Ben Chengba4fc8b2009-06-01 13:00:29 -07004602 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07004603 case kChainingCellBackwardBranch:
Jeff Hao97319a82009-08-12 16:57:15 -07004604 handleBackwardBranchChainingCell(cUnit,
Ben Cheng00603072010-10-28 11:13:58 -07004605 chainingBlock->startOffset);
Jeff Hao97319a82009-08-12 16:57:15 -07004606 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07004607 default:
Ben Cheng00603072010-10-28 11:13:58 -07004608 LOGE("Bad blocktype %d", chainingBlock->blockType);
Bill Buzbeefc519dc2010-03-06 23:30:57 -08004609 dvmCompilerAbort(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -07004610 }
4611 }
4612 }
Ben Chenge9695e52009-06-16 16:11:47 -07004613
Ben Chengcec26f62010-01-15 15:29:33 -08004614 /* Mark the bottom of chaining cells */
4615 cUnit->chainingCellBottom = (LIR *) newLIR0(cUnit, kArmChainingCellBottom);
4616
Ben Cheng6c10a972009-10-29 14:39:18 -07004617 /*
4618 * Generate the branch to the dvmJitToInterpNoChain entry point at the end
4619 * of all chaining cells for the overflow cases.
4620 */
4621 if (cUnit->switchOverflowPad) {
4622 loadConstant(cUnit, r0, (int) cUnit->switchOverflowPad);
Ben Cheng20d7e6c2011-02-18 17:12:42 -08004623 loadWordDisp(cUnit, r6SELF, offsetof(Thread,
Ben Cheng6c10a972009-10-29 14:39:18 -07004624 jitToInterpEntries.dvmJitToInterpNoChain), r2);
4625 opRegReg(cUnit, kOpAdd, r1, r1);
4626 opRegRegReg(cUnit, kOpAdd, r4PC, r0, r1);
Ben Cheng978738d2010-05-13 13:45:57 -07004627#if defined(WITH_JIT_TUNING)
Ben Cheng6c10a972009-10-29 14:39:18 -07004628 loadConstant(cUnit, r0, kSwitchOverflow);
4629#endif
4630 opReg(cUnit, kOpBlx, r2);
4631 }
4632
Ben Chenge9695e52009-06-16 16:11:47 -07004633 dvmCompilerApplyGlobalOptimizations(cUnit);
jeffhao9e45c0b2010-02-03 10:24:05 -08004634
4635#if defined(WITH_SELF_VERIFICATION)
4636 selfVerificationBranchInsertPass(cUnit);
4637#endif
Ben Chengba4fc8b2009-06-01 13:00:29 -07004638}
4639
buzbee2e152ba2010-12-15 16:32:35 -08004640/*
4641 * Accept the work and start compiling. Returns true if compilation
4642 * is attempted.
4643 */
Bill Buzbee716f1202009-07-23 13:22:09 -07004644bool dvmCompilerDoWork(CompilerWorkOrder *work)
Ben Chengba4fc8b2009-06-01 13:00:29 -07004645{
Carl Shapirofc75f3e2010-12-07 11:43:38 -08004646 JitTraceDescription *desc;
buzbee2e152ba2010-12-15 16:32:35 -08004647 bool isCompile;
4648 bool success = true;
Ben Chengba4fc8b2009-06-01 13:00:29 -07004649
Ben Cheng6999d842010-01-26 16:46:15 -08004650 if (gDvmJit.codeCacheFull) {
Ben Chengccd6c012009-10-15 14:52:45 -07004651 return false;
4652 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07004653
Ben Chengccd6c012009-10-15 14:52:45 -07004654 switch (work->kind) {
Ben Chengccd6c012009-10-15 14:52:45 -07004655 case kWorkOrderTrace:
buzbee2e152ba2010-12-15 16:32:35 -08004656 isCompile = true;
Ben Chengccd6c012009-10-15 14:52:45 -07004657 /* Start compilation with maximally allowed trace length */
Carl Shapirofc75f3e2010-12-07 11:43:38 -08004658 desc = (JitTraceDescription *)work->info;
buzbee2e152ba2010-12-15 16:32:35 -08004659 success = dvmCompileTrace(desc, JIT_MAX_TRACE_LEN, &work->result,
4660 work->bailPtr, 0 /* no hints */);
Ben Chengccd6c012009-10-15 14:52:45 -07004661 break;
4662 case kWorkOrderTraceDebug: {
4663 bool oldPrintMe = gDvmJit.printMe;
4664 gDvmJit.printMe = true;
buzbee2e152ba2010-12-15 16:32:35 -08004665 isCompile = true;
Ben Chengccd6c012009-10-15 14:52:45 -07004666 /* Start compilation with maximally allowed trace length */
Carl Shapirofc75f3e2010-12-07 11:43:38 -08004667 desc = (JitTraceDescription *)work->info;
buzbee2e152ba2010-12-15 16:32:35 -08004668 success = dvmCompileTrace(desc, JIT_MAX_TRACE_LEN, &work->result,
4669 work->bailPtr, 0 /* no hints */);
Elliott Hughes672511b2010-04-26 17:40:13 -07004670 gDvmJit.printMe = oldPrintMe;
Ben Chengccd6c012009-10-15 14:52:45 -07004671 break;
4672 }
buzbee2e152ba2010-12-15 16:32:35 -08004673 case kWorkOrderProfileMode:
4674 dvmJitChangeProfileMode((TraceProfilingModes)work->info);
4675 isCompile = false;
4676 break;
Ben Chengccd6c012009-10-15 14:52:45 -07004677 default:
buzbee2e152ba2010-12-15 16:32:35 -08004678 isCompile = false;
Bill Buzbeefc519dc2010-03-06 23:30:57 -08004679 LOGE("Jit: unknown work order type");
Elliott Hughes672511b2010-04-26 17:40:13 -07004680 assert(0); // Bail if debug build, discard otherwise
Ben Chengccd6c012009-10-15 14:52:45 -07004681 }
buzbee2e152ba2010-12-15 16:32:35 -08004682 if (!success)
4683 work->result.codeAddress = NULL;
4684 return isCompile;
Ben Chengba4fc8b2009-06-01 13:00:29 -07004685}
4686
Ben Chengba4fc8b2009-06-01 13:00:29 -07004687/* Architectural-specific debugging helpers go here */
4688void dvmCompilerArchDump(void)
4689{
4690 /* Print compiled opcode in this VM instance */
4691 int i, start, streak;
4692 char buf[1024];
4693
4694 streak = i = 0;
4695 buf[0] = 0;
Dan Bornsteinccaab182010-12-03 15:32:40 -08004696 while (opcodeCoverage[i] == 0 && i < kNumPackedOpcodes) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07004697 i++;
4698 }
Dan Bornsteinccaab182010-12-03 15:32:40 -08004699 if (i == kNumPackedOpcodes) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07004700 return;
4701 }
Dan Bornsteinccaab182010-12-03 15:32:40 -08004702 for (start = i++, streak = 1; i < kNumPackedOpcodes; i++) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07004703 if (opcodeCoverage[i]) {
4704 streak++;
4705 } else {
4706 if (streak == 1) {
4707 sprintf(buf+strlen(buf), "%x,", start);
4708 } else {
4709 sprintf(buf+strlen(buf), "%x-%x,", start, start + streak - 1);
4710 }
4711 streak = 0;
Dan Bornsteinccaab182010-12-03 15:32:40 -08004712 while (opcodeCoverage[i] == 0 && i < kNumPackedOpcodes) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07004713 i++;
4714 }
Dan Bornsteinccaab182010-12-03 15:32:40 -08004715 if (i < kNumPackedOpcodes) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07004716 streak = 1;
4717 start = i;
4718 }
4719 }
4720 }
4721 if (streak) {
4722 if (streak == 1) {
4723 sprintf(buf+strlen(buf), "%x", start);
4724 } else {
4725 sprintf(buf+strlen(buf), "%x-%x", start, start + streak - 1);
4726 }
4727 }
4728 if (strlen(buf)) {
Ben Cheng8b258bf2009-06-24 17:27:07 -07004729 LOGD("dalvik.vm.jit.op = %s", buf);
Ben Chengba4fc8b2009-06-01 13:00:29 -07004730 }
4731}
Ben Chengd7d426a2009-09-22 11:23:36 -07004732
4733/* Common initialization routine for an architecture family */
4734bool dvmCompilerArchInit()
4735{
4736 int i;
4737
Bill Buzbee1465db52009-09-23 17:17:35 -07004738 for (i = 0; i < kArmLast; i++) {
Dan Bornstein9a1f8162010-12-01 17:02:26 -08004739 if (EncodingMap[i].opcode != i) {
Ben Chengd7d426a2009-09-22 11:23:36 -07004740 LOGE("Encoding order for %s is wrong: expecting %d, seeing %d",
Dan Bornstein9a1f8162010-12-01 17:02:26 -08004741 EncodingMap[i].name, i, EncodingMap[i].opcode);
Bill Buzbeefc519dc2010-03-06 23:30:57 -08004742 dvmAbort(); // OK to dvmAbort - build error
Ben Chengd7d426a2009-09-22 11:23:36 -07004743 }
4744 }
4745
Ben Cheng5d90c202009-11-22 23:31:11 -08004746 return dvmCompilerArchVariantInit();
4747}
4748
4749void *dvmCompilerGetInterpretTemplate()
4750{
4751 return (void*) ((int)gDvmJit.codeCache +
4752 templateEntryOffsets[TEMPLATE_INTERPRET]);
4753}
4754
Bill Buzbee1b3da592011-02-03 07:38:22 -08004755JitInstructionSetType dvmCompilerGetInterpretTemplateSet()
4756{
4757 return DALVIK_JIT_ARM;
4758}
4759
buzbeebff121a2010-08-04 15:25:06 -07004760/* Needed by the Assembler */
4761void dvmCompilerSetupResourceMasks(ArmLIR *lir)
4762{
4763 setupResourceMasks(lir);
4764}
4765
Ben Cheng5d90c202009-11-22 23:31:11 -08004766/* Needed by the ld/st optmizatons */
4767ArmLIR* dvmCompilerRegCopyNoInsert(CompilationUnit *cUnit, int rDest, int rSrc)
4768{
4769 return genRegCopyNoInsert(cUnit, rDest, rSrc);
4770}
4771
4772/* Needed by the register allocator */
4773ArmLIR* dvmCompilerRegCopy(CompilationUnit *cUnit, int rDest, int rSrc)
4774{
4775 return genRegCopy(cUnit, rDest, rSrc);
4776}
4777
4778/* Needed by the register allocator */
4779void dvmCompilerRegCopyWide(CompilationUnit *cUnit, int destLo, int destHi,
4780 int srcLo, int srcHi)
4781{
4782 genRegCopyWide(cUnit, destLo, destHi, srcLo, srcHi);
4783}
4784
4785void dvmCompilerFlushRegImpl(CompilationUnit *cUnit, int rBase,
4786 int displacement, int rSrc, OpSize size)
4787{
4788 storeBaseDisp(cUnit, rBase, displacement, rSrc, size);
4789}
4790
4791void dvmCompilerFlushRegWideImpl(CompilationUnit *cUnit, int rBase,
4792 int displacement, int rSrcLo, int rSrcHi)
4793{
4794 storeBaseDispWide(cUnit, rBase, displacement, rSrcLo, rSrcHi);
Ben Chengd7d426a2009-09-22 11:23:36 -07004795}