blob: 515e8afa9e48b6173ea5bb77f4a8965a4961c933 [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
Ben Cheng5d90c202009-11-22 23:31:11 -080027static bool genConversionCall(CompilationUnit *cUnit, MIR *mir, void *funct,
28 int srcSize, int tgtSize)
29{
30 /*
31 * Don't optimize the register usage since it calls out to template
32 * functions
33 */
34 RegLocation rlSrc;
35 RegLocation rlDest;
Bill Buzbeec6f10662010-02-09 11:16:15 -080036 dvmCompilerFlushAllRegs(cUnit); /* Send everything to home location */
Ben Cheng5d90c202009-11-22 23:31:11 -080037 if (srcSize == 1) {
Bill Buzbeec6f10662010-02-09 11:16:15 -080038 rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
Ben Cheng5d90c202009-11-22 23:31:11 -080039 loadValueDirectFixed(cUnit, rlSrc, r0);
40 } else {
Bill Buzbeec6f10662010-02-09 11:16:15 -080041 rlSrc = dvmCompilerGetSrcWide(cUnit, mir, 0, 1);
Ben Cheng5d90c202009-11-22 23:31:11 -080042 loadValueDirectWideFixed(cUnit, rlSrc, r0, r1);
43 }
Ben Chengbd1326d2010-04-02 15:04:53 -070044 LOAD_FUNC_ADDR(cUnit, r2, (int)funct);
Ben Cheng5d90c202009-11-22 23:31:11 -080045 opReg(cUnit, kOpBlx, r2);
Elliott Hughes6a555132010-02-25 15:41:42 -080046 dvmCompilerClobberCallRegs(cUnit);
Ben Cheng5d90c202009-11-22 23:31:11 -080047 if (tgtSize == 1) {
48 RegLocation rlResult;
Bill Buzbeec6f10662010-02-09 11:16:15 -080049 rlDest = dvmCompilerGetDest(cUnit, mir, 0);
50 rlResult = dvmCompilerGetReturn(cUnit);
Ben Cheng5d90c202009-11-22 23:31:11 -080051 storeValue(cUnit, rlDest, rlResult);
52 } else {
53 RegLocation rlResult;
Bill Buzbeec6f10662010-02-09 11:16:15 -080054 rlDest = dvmCompilerGetDestWide(cUnit, mir, 0, 1);
55 rlResult = dvmCompilerGetReturnWide(cUnit);
Ben Cheng5d90c202009-11-22 23:31:11 -080056 storeValueWide(cUnit, rlDest, rlResult);
57 }
58 return false;
59}
Ben Chengba4fc8b2009-06-01 13:00:29 -070060
Ben Chengba4fc8b2009-06-01 13:00:29 -070061
Ben Cheng5d90c202009-11-22 23:31:11 -080062static bool genArithOpFloatPortable(CompilationUnit *cUnit, MIR *mir,
63 RegLocation rlDest, RegLocation rlSrc1,
64 RegLocation rlSrc2)
65{
66 RegLocation rlResult;
67 void* funct;
68
Ben Cheng5d90c202009-11-22 23:31:11 -080069 switch (mir->dalvikInsn.opCode) {
70 case OP_ADD_FLOAT_2ADDR:
71 case OP_ADD_FLOAT:
72 funct = (void*) __aeabi_fadd;
73 break;
74 case OP_SUB_FLOAT_2ADDR:
75 case OP_SUB_FLOAT:
76 funct = (void*) __aeabi_fsub;
77 break;
78 case OP_DIV_FLOAT_2ADDR:
79 case OP_DIV_FLOAT:
80 funct = (void*) __aeabi_fdiv;
81 break;
82 case OP_MUL_FLOAT_2ADDR:
83 case OP_MUL_FLOAT:
84 funct = (void*) __aeabi_fmul;
85 break;
86 case OP_REM_FLOAT_2ADDR:
87 case OP_REM_FLOAT:
88 funct = (void*) fmodf;
89 break;
90 case OP_NEG_FLOAT: {
91 genNegFloat(cUnit, rlDest, rlSrc1);
92 return false;
93 }
94 default:
95 return true;
96 }
Bill Buzbeec6f10662010-02-09 11:16:15 -080097 dvmCompilerFlushAllRegs(cUnit); /* Send everything to home location */
Ben Cheng5d90c202009-11-22 23:31:11 -080098 loadValueDirectFixed(cUnit, rlSrc1, r0);
99 loadValueDirectFixed(cUnit, rlSrc2, r1);
Ben Chengbd1326d2010-04-02 15:04:53 -0700100 LOAD_FUNC_ADDR(cUnit, r2, (int)funct);
Ben Cheng5d90c202009-11-22 23:31:11 -0800101 opReg(cUnit, kOpBlx, r2);
Elliott Hughes6a555132010-02-25 15:41:42 -0800102 dvmCompilerClobberCallRegs(cUnit);
Bill Buzbeec6f10662010-02-09 11:16:15 -0800103 rlResult = dvmCompilerGetReturn(cUnit);
Ben Cheng5d90c202009-11-22 23:31:11 -0800104 storeValue(cUnit, rlDest, rlResult);
105 return false;
106}
107
108static bool genArithOpDoublePortable(CompilationUnit *cUnit, MIR *mir,
109 RegLocation rlDest, RegLocation rlSrc1,
110 RegLocation rlSrc2)
111{
112 RegLocation rlResult;
113 void* funct;
114
Ben Cheng5d90c202009-11-22 23:31:11 -0800115 switch (mir->dalvikInsn.opCode) {
116 case OP_ADD_DOUBLE_2ADDR:
117 case OP_ADD_DOUBLE:
118 funct = (void*) __aeabi_dadd;
119 break;
120 case OP_SUB_DOUBLE_2ADDR:
121 case OP_SUB_DOUBLE:
122 funct = (void*) __aeabi_dsub;
123 break;
124 case OP_DIV_DOUBLE_2ADDR:
125 case OP_DIV_DOUBLE:
126 funct = (void*) __aeabi_ddiv;
127 break;
128 case OP_MUL_DOUBLE_2ADDR:
129 case OP_MUL_DOUBLE:
130 funct = (void*) __aeabi_dmul;
131 break;
132 case OP_REM_DOUBLE_2ADDR:
133 case OP_REM_DOUBLE:
134 funct = (void*) fmod;
135 break;
136 case OP_NEG_DOUBLE: {
137 genNegDouble(cUnit, rlDest, rlSrc1);
138 return false;
139 }
140 default:
141 return true;
142 }
Bill Buzbeec6f10662010-02-09 11:16:15 -0800143 dvmCompilerFlushAllRegs(cUnit); /* Send everything to home location */
Ben Chengbd1326d2010-04-02 15:04:53 -0700144 LOAD_FUNC_ADDR(cUnit, rlr, (int)funct);
Ben Cheng5d90c202009-11-22 23:31:11 -0800145 loadValueDirectWideFixed(cUnit, rlSrc1, r0, r1);
146 loadValueDirectWideFixed(cUnit, rlSrc2, r2, r3);
147 opReg(cUnit, kOpBlx, rlr);
Elliott Hughes6a555132010-02-25 15:41:42 -0800148 dvmCompilerClobberCallRegs(cUnit);
Bill Buzbeec6f10662010-02-09 11:16:15 -0800149 rlResult = dvmCompilerGetReturnWide(cUnit);
Ben Cheng5d90c202009-11-22 23:31:11 -0800150 storeValueWide(cUnit, rlDest, rlResult);
151 return false;
152}
153
154static bool genConversionPortable(CompilationUnit *cUnit, MIR *mir)
155{
156 OpCode opCode = mir->dalvikInsn.opCode;
157
Ben Cheng5d90c202009-11-22 23:31:11 -0800158 switch (opCode) {
159 case OP_INT_TO_FLOAT:
160 return genConversionCall(cUnit, mir, (void*)__aeabi_i2f, 1, 1);
161 case OP_FLOAT_TO_INT:
162 return genConversionCall(cUnit, mir, (void*)__aeabi_f2iz, 1, 1);
163 case OP_DOUBLE_TO_FLOAT:
164 return genConversionCall(cUnit, mir, (void*)__aeabi_d2f, 2, 1);
165 case OP_FLOAT_TO_DOUBLE:
166 return genConversionCall(cUnit, mir, (void*)__aeabi_f2d, 1, 2);
167 case OP_INT_TO_DOUBLE:
168 return genConversionCall(cUnit, mir, (void*)__aeabi_i2d, 1, 2);
169 case OP_DOUBLE_TO_INT:
170 return genConversionCall(cUnit, mir, (void*)__aeabi_d2iz, 2, 1);
171 case OP_FLOAT_TO_LONG:
172 return genConversionCall(cUnit, mir, (void*)dvmJitf2l, 1, 2);
173 case OP_LONG_TO_FLOAT:
174 return genConversionCall(cUnit, mir, (void*)__aeabi_l2f, 2, 1);
175 case OP_DOUBLE_TO_LONG:
176 return genConversionCall(cUnit, mir, (void*)dvmJitd2l, 2, 2);
177 case OP_LONG_TO_DOUBLE:
178 return genConversionCall(cUnit, mir, (void*)__aeabi_l2d, 2, 2);
179 default:
180 return true;
181 }
182 return false;
183}
Ben Chengba4fc8b2009-06-01 13:00:29 -0700184
Jeff Hao97319a82009-08-12 16:57:15 -0700185#if defined(WITH_SELF_VERIFICATION)
jeffhao9e45c0b2010-02-03 10:24:05 -0800186static void selfVerificationBranchInsert(LIR *currentLIR, ArmOpCode opCode,
187 int dest, int src1)
Jeff Hao97319a82009-08-12 16:57:15 -0700188{
jeffhao9e45c0b2010-02-03 10:24:05 -0800189 ArmLIR *insn = dvmCompilerNew(sizeof(ArmLIR), true);
190 insn->opCode = opCode;
191 insn->operands[0] = dest;
192 insn->operands[1] = src1;
193 setupResourceMasks(insn);
194 dvmCompilerInsertLIRBefore(currentLIR, (LIR *) insn);
Jeff Hao97319a82009-08-12 16:57:15 -0700195}
196
jeffhao9e45c0b2010-02-03 10:24:05 -0800197static void selfVerificationBranchInsertPass(CompilationUnit *cUnit)
Jeff Hao97319a82009-08-12 16:57:15 -0700198{
jeffhao9e45c0b2010-02-03 10:24:05 -0800199 ArmLIR *thisLIR;
200 ArmLIR *branchLIR = dvmCompilerNew(sizeof(ArmLIR), true);
201 TemplateOpCode opCode = TEMPLATE_MEM_OP_DECODE;
Jeff Hao97319a82009-08-12 16:57:15 -0700202
jeffhao9e45c0b2010-02-03 10:24:05 -0800203 for (thisLIR = (ArmLIR *) cUnit->firstLIRInsn;
204 thisLIR != (ArmLIR *) cUnit->lastLIRInsn;
205 thisLIR = NEXT_LIR(thisLIR)) {
206 if (thisLIR->branchInsertSV) {
207 /* Branch to mem op decode template */
208 selfVerificationBranchInsert((LIR *) thisLIR, kThumbBlx1,
209 (int) gDvmJit.codeCache + templateEntryOffsets[opCode],
210 (int) gDvmJit.codeCache + templateEntryOffsets[opCode]);
211 selfVerificationBranchInsert((LIR *) thisLIR, kThumbBlx2,
212 (int) gDvmJit.codeCache + templateEntryOffsets[opCode],
213 (int) gDvmJit.codeCache + templateEntryOffsets[opCode]);
Jeff Hao97319a82009-08-12 16:57:15 -0700214 }
215 }
Jeff Hao97319a82009-08-12 16:57:15 -0700216}
Jeff Hao97319a82009-08-12 16:57:15 -0700217#endif
218
Bill Buzbeebe6534f2010-03-12 16:01:35 -0800219/* Generate conditional branch instructions */
220static ArmLIR *genConditionalBranch(CompilationUnit *cUnit,
221 ArmConditionCode cond,
222 ArmLIR *target)
223{
224 ArmLIR *branch = opCondBranch(cUnit, cond);
225 branch->generic.target = (LIR *) target;
226 return branch;
227}
228
Ben Chengba4fc8b2009-06-01 13:00:29 -0700229/* Generate a unconditional branch to go to the interpreter */
Bill Buzbee89efc3d2009-07-28 11:22:22 -0700230static inline ArmLIR *genTrap(CompilationUnit *cUnit, int dOffset,
231 ArmLIR *pcrLabel)
Ben Chengba4fc8b2009-06-01 13:00:29 -0700232{
Bill Buzbee1465db52009-09-23 17:17:35 -0700233 ArmLIR *branch = opNone(cUnit, kOpUncondBr);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700234 return genCheckCommon(cUnit, dOffset, branch, pcrLabel);
235}
236
237/* Load a wide field from an object instance */
238static void genIGetWide(CompilationUnit *cUnit, MIR *mir, int fieldOffset)
239{
240 DecodedInstruction *dInsn = &mir->dalvikInsn;
Bill Buzbeec6f10662010-02-09 11:16:15 -0800241 RegLocation rlObj = dvmCompilerGetSrc(cUnit, mir, 0);
242 RegLocation rlDest = dvmCompilerGetDestWide(cUnit, mir, 0, 1);
Bill Buzbee1465db52009-09-23 17:17:35 -0700243 RegLocation rlResult;
244 rlObj = loadValue(cUnit, rlObj, kCoreReg);
Bill Buzbeec6f10662010-02-09 11:16:15 -0800245 int regPtr = dvmCompilerAllocTemp(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700246
Bill Buzbee1465db52009-09-23 17:17:35 -0700247 assert(rlDest.wide);
Ben Chenge9695e52009-06-16 16:11:47 -0700248
Bill Buzbee1465db52009-09-23 17:17:35 -0700249 genNullCheck(cUnit, rlObj.sRegLow, rlObj.lowReg, mir->offset,
250 NULL);/* null object? */
251 opRegRegImm(cUnit, kOpAdd, regPtr, rlObj.lowReg, fieldOffset);
Bill Buzbeec6f10662010-02-09 11:16:15 -0800252 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kAnyReg, true);
Ben Cheng11d8f142010-03-24 15:24:19 -0700253
254 HEAP_ACCESS_SHADOW(true);
Bill Buzbee1465db52009-09-23 17:17:35 -0700255 loadPair(cUnit, regPtr, rlResult.lowReg, rlResult.highReg);
Ben Cheng11d8f142010-03-24 15:24:19 -0700256 HEAP_ACCESS_SHADOW(false);
257
Bill Buzbeec6f10662010-02-09 11:16:15 -0800258 dvmCompilerFreeTemp(cUnit, regPtr);
Bill Buzbee1465db52009-09-23 17:17:35 -0700259 storeValueWide(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700260}
261
262/* Store a wide field to an object instance */
263static void genIPutWide(CompilationUnit *cUnit, MIR *mir, int fieldOffset)
264{
265 DecodedInstruction *dInsn = &mir->dalvikInsn;
Bill Buzbeec6f10662010-02-09 11:16:15 -0800266 RegLocation rlSrc = dvmCompilerGetSrcWide(cUnit, mir, 0, 1);
267 RegLocation rlObj = dvmCompilerGetSrc(cUnit, mir, 2);
Bill Buzbee1465db52009-09-23 17:17:35 -0700268 rlObj = loadValue(cUnit, rlObj, kCoreReg);
269 int regPtr;
270 rlSrc = loadValueWide(cUnit, rlSrc, kAnyReg);
271 genNullCheck(cUnit, rlObj.sRegLow, rlObj.lowReg, mir->offset,
272 NULL);/* null object? */
Bill Buzbeec6f10662010-02-09 11:16:15 -0800273 regPtr = dvmCompilerAllocTemp(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -0700274 opRegRegImm(cUnit, kOpAdd, regPtr, rlObj.lowReg, fieldOffset);
Ben Cheng11d8f142010-03-24 15:24:19 -0700275
276 HEAP_ACCESS_SHADOW(true);
Bill Buzbee1465db52009-09-23 17:17:35 -0700277 storePair(cUnit, regPtr, rlSrc.lowReg, rlSrc.highReg);
Ben Cheng11d8f142010-03-24 15:24:19 -0700278 HEAP_ACCESS_SHADOW(false);
279
Bill Buzbeec6f10662010-02-09 11:16:15 -0800280 dvmCompilerFreeTemp(cUnit, regPtr);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700281}
282
283/*
284 * Load a field from an object instance
285 *
Ben Chengba4fc8b2009-06-01 13:00:29 -0700286 */
Bill Buzbee270c1d62009-08-13 16:58:07 -0700287static void genIGet(CompilationUnit *cUnit, MIR *mir, OpSize size,
Ben Chengba4fc8b2009-06-01 13:00:29 -0700288 int fieldOffset)
289{
Bill Buzbee1465db52009-09-23 17:17:35 -0700290 int regPtr;
291 RegLocation rlResult;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700292 DecodedInstruction *dInsn = &mir->dalvikInsn;
Bill Buzbeec6f10662010-02-09 11:16:15 -0800293 RegLocation rlObj = dvmCompilerGetSrc(cUnit, mir, 0);
294 RegLocation rlDest = dvmCompilerGetDest(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -0700295 rlObj = loadValue(cUnit, rlObj, kCoreReg);
Bill Buzbeec6f10662010-02-09 11:16:15 -0800296 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kAnyReg, true);
Bill Buzbee1465db52009-09-23 17:17:35 -0700297 genNullCheck(cUnit, rlObj.sRegLow, rlObj.lowReg, mir->offset,
298 NULL);/* null object? */
Ben Cheng11d8f142010-03-24 15:24:19 -0700299
300 HEAP_ACCESS_SHADOW(true);
Ben Cheng5d90c202009-11-22 23:31:11 -0800301 loadBaseDisp(cUnit, mir, rlObj.lowReg, fieldOffset, rlResult.lowReg,
302 size, rlObj.sRegLow);
Ben Cheng11d8f142010-03-24 15:24:19 -0700303 HEAP_ACCESS_SHADOW(false);
304
Bill Buzbee1465db52009-09-23 17:17:35 -0700305 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700306}
307
308/*
309 * Store a field to an object instance
310 *
Ben Chengba4fc8b2009-06-01 13:00:29 -0700311 */
Bill Buzbee270c1d62009-08-13 16:58:07 -0700312static void genIPut(CompilationUnit *cUnit, MIR *mir, OpSize size,
Ben Chengba4fc8b2009-06-01 13:00:29 -0700313 int fieldOffset)
314{
315 DecodedInstruction *dInsn = &mir->dalvikInsn;
Bill Buzbeec6f10662010-02-09 11:16:15 -0800316 RegLocation rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
317 RegLocation rlObj = dvmCompilerGetSrc(cUnit, mir, 1);
Bill Buzbee1465db52009-09-23 17:17:35 -0700318 rlObj = loadValue(cUnit, rlObj, kCoreReg);
319 rlSrc = loadValue(cUnit, rlSrc, kAnyReg);
320 int regPtr;
321 genNullCheck(cUnit, rlObj.sRegLow, rlObj.lowReg, mir->offset,
322 NULL);/* null object? */
Ben Cheng11d8f142010-03-24 15:24:19 -0700323
324 HEAP_ACCESS_SHADOW(true);
Bill Buzbee1465db52009-09-23 17:17:35 -0700325 storeBaseDisp(cUnit, rlObj.lowReg, fieldOffset, rlSrc.lowReg, size);
Ben Cheng11d8f142010-03-24 15:24:19 -0700326 HEAP_ACCESS_SHADOW(false);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700327}
328
329
Ben Chengba4fc8b2009-06-01 13:00:29 -0700330/*
331 * Generate array load
Ben Chengba4fc8b2009-06-01 13:00:29 -0700332 */
Bill Buzbee270c1d62009-08-13 16:58:07 -0700333static void genArrayGet(CompilationUnit *cUnit, MIR *mir, OpSize size,
Bill Buzbee1465db52009-09-23 17:17:35 -0700334 RegLocation rlArray, RegLocation rlIndex,
335 RegLocation rlDest, int scale)
Ben Chengba4fc8b2009-06-01 13:00:29 -0700336{
337 int lenOffset = offsetof(ArrayObject, length);
338 int dataOffset = offsetof(ArrayObject, contents);
Bill Buzbee1465db52009-09-23 17:17:35 -0700339 RegLocation rlResult;
340 rlArray = loadValue(cUnit, rlArray, kCoreReg);
341 rlIndex = loadValue(cUnit, rlIndex, kCoreReg);
342 int regPtr;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700343
344 /* null object? */
Ben Cheng4238ec22009-08-24 16:32:22 -0700345 ArmLIR * pcrLabel = NULL;
346
347 if (!(mir->OptimizationFlags & MIR_IGNORE_NULL_CHECK)) {
Bill Buzbee1465db52009-09-23 17:17:35 -0700348 pcrLabel = genNullCheck(cUnit, rlArray.sRegLow,
349 rlArray.lowReg, mir->offset, NULL);
Ben Cheng4238ec22009-08-24 16:32:22 -0700350 }
351
Bill Buzbeec6f10662010-02-09 11:16:15 -0800352 regPtr = dvmCompilerAllocTemp(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -0700353
Ben Cheng4238ec22009-08-24 16:32:22 -0700354 if (!(mir->OptimizationFlags & MIR_IGNORE_RANGE_CHECK)) {
Bill Buzbeec6f10662010-02-09 11:16:15 -0800355 int regLen = dvmCompilerAllocTemp(cUnit);
Ben Cheng4238ec22009-08-24 16:32:22 -0700356 /* Get len */
Bill Buzbee1465db52009-09-23 17:17:35 -0700357 loadWordDisp(cUnit, rlArray.lowReg, lenOffset, regLen);
358 /* regPtr -> array data */
359 opRegRegImm(cUnit, kOpAdd, regPtr, rlArray.lowReg, dataOffset);
360 genBoundsCheck(cUnit, rlIndex.lowReg, regLen, mir->offset,
361 pcrLabel);
Bill Buzbeec6f10662010-02-09 11:16:15 -0800362 dvmCompilerFreeTemp(cUnit, regLen);
Ben Cheng4238ec22009-08-24 16:32:22 -0700363 } else {
Bill Buzbee1465db52009-09-23 17:17:35 -0700364 /* regPtr -> array data */
365 opRegRegImm(cUnit, kOpAdd, regPtr, rlArray.lowReg, dataOffset);
Ben Cheng4238ec22009-08-24 16:32:22 -0700366 }
Bill Buzbee1465db52009-09-23 17:17:35 -0700367 if ((size == kLong) || (size == kDouble)) {
368 if (scale) {
Bill Buzbeec6f10662010-02-09 11:16:15 -0800369 int rNewIndex = dvmCompilerAllocTemp(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -0700370 opRegRegImm(cUnit, kOpLsl, rNewIndex, rlIndex.lowReg, scale);
371 opRegReg(cUnit, kOpAdd, regPtr, rNewIndex);
Bill Buzbeec6f10662010-02-09 11:16:15 -0800372 dvmCompilerFreeTemp(cUnit, rNewIndex);
Bill Buzbee1465db52009-09-23 17:17:35 -0700373 } else {
374 opRegReg(cUnit, kOpAdd, regPtr, rlIndex.lowReg);
375 }
Bill Buzbeec6f10662010-02-09 11:16:15 -0800376 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kAnyReg, true);
Ben Cheng11d8f142010-03-24 15:24:19 -0700377
378 HEAP_ACCESS_SHADOW(true);
Bill Buzbee1465db52009-09-23 17:17:35 -0700379 loadPair(cUnit, regPtr, rlResult.lowReg, rlResult.highReg);
Ben Cheng11d8f142010-03-24 15:24:19 -0700380 HEAP_ACCESS_SHADOW(false);
381
Bill Buzbeec6f10662010-02-09 11:16:15 -0800382 dvmCompilerFreeTemp(cUnit, regPtr);
Bill Buzbee1465db52009-09-23 17:17:35 -0700383 storeValueWide(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700384 } else {
Bill Buzbeec6f10662010-02-09 11:16:15 -0800385 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kAnyReg, true);
Ben Cheng11d8f142010-03-24 15:24:19 -0700386
387 HEAP_ACCESS_SHADOW(true);
Bill Buzbee1465db52009-09-23 17:17:35 -0700388 loadBaseIndexed(cUnit, regPtr, rlIndex.lowReg, rlResult.lowReg,
389 scale, size);
Ben Cheng11d8f142010-03-24 15:24:19 -0700390 HEAP_ACCESS_SHADOW(false);
391
Bill Buzbeec6f10662010-02-09 11:16:15 -0800392 dvmCompilerFreeTemp(cUnit, regPtr);
Bill Buzbee1465db52009-09-23 17:17:35 -0700393 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700394 }
395}
396
Ben Chengba4fc8b2009-06-01 13:00:29 -0700397/*
398 * Generate array store
399 *
Ben Chengba4fc8b2009-06-01 13:00:29 -0700400 */
Bill Buzbee270c1d62009-08-13 16:58:07 -0700401static void genArrayPut(CompilationUnit *cUnit, MIR *mir, OpSize size,
Bill Buzbee1465db52009-09-23 17:17:35 -0700402 RegLocation rlArray, RegLocation rlIndex,
403 RegLocation rlSrc, int scale)
Ben Chengba4fc8b2009-06-01 13:00:29 -0700404{
405 int lenOffset = offsetof(ArrayObject, length);
406 int dataOffset = offsetof(ArrayObject, contents);
407
Bill Buzbee1465db52009-09-23 17:17:35 -0700408 int regPtr;
409 rlArray = loadValue(cUnit, rlArray, kCoreReg);
410 rlIndex = loadValue(cUnit, rlIndex, kCoreReg);
Ben Chenge9695e52009-06-16 16:11:47 -0700411
Bill Buzbeec6f10662010-02-09 11:16:15 -0800412 if (dvmCompilerIsTemp(cUnit, rlArray.lowReg)) {
413 dvmCompilerClobber(cUnit, rlArray.lowReg);
Bill Buzbee1465db52009-09-23 17:17:35 -0700414 regPtr = rlArray.lowReg;
415 } else {
Bill Buzbeec6f10662010-02-09 11:16:15 -0800416 regPtr = dvmCompilerAllocTemp(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -0700417 genRegCopy(cUnit, regPtr, rlArray.lowReg);
418 }
Ben Chenge9695e52009-06-16 16:11:47 -0700419
Ben Cheng1efc9c52009-06-08 18:25:27 -0700420 /* null object? */
Ben Cheng4238ec22009-08-24 16:32:22 -0700421 ArmLIR * pcrLabel = NULL;
422
423 if (!(mir->OptimizationFlags & MIR_IGNORE_NULL_CHECK)) {
Bill Buzbee1465db52009-09-23 17:17:35 -0700424 pcrLabel = genNullCheck(cUnit, rlArray.sRegLow, rlArray.lowReg,
425 mir->offset, NULL);
Ben Cheng4238ec22009-08-24 16:32:22 -0700426 }
427
428 if (!(mir->OptimizationFlags & MIR_IGNORE_RANGE_CHECK)) {
Bill Buzbeec6f10662010-02-09 11:16:15 -0800429 int regLen = dvmCompilerAllocTemp(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -0700430 //NOTE: max live temps(4) here.
Ben Cheng4238ec22009-08-24 16:32:22 -0700431 /* Get len */
Bill Buzbee1465db52009-09-23 17:17:35 -0700432 loadWordDisp(cUnit, rlArray.lowReg, lenOffset, regLen);
433 /* regPtr -> array data */
434 opRegImm(cUnit, kOpAdd, regPtr, dataOffset);
435 genBoundsCheck(cUnit, rlIndex.lowReg, regLen, mir->offset,
436 pcrLabel);
Bill Buzbeec6f10662010-02-09 11:16:15 -0800437 dvmCompilerFreeTemp(cUnit, regLen);
Ben Cheng4238ec22009-08-24 16:32:22 -0700438 } else {
Bill Buzbee1465db52009-09-23 17:17:35 -0700439 /* regPtr -> array data */
440 opRegImm(cUnit, kOpAdd, regPtr, dataOffset);
Ben Cheng4238ec22009-08-24 16:32:22 -0700441 }
Bill Buzbee1465db52009-09-23 17:17:35 -0700442 /* at this point, regPtr points to array, 2 live temps */
Bill Buzbee1465db52009-09-23 17:17:35 -0700443 if ((size == kLong) || (size == kDouble)) {
444 //TODO: need specific wide routine that can handle fp regs
445 if (scale) {
Bill Buzbeec6f10662010-02-09 11:16:15 -0800446 int rNewIndex = dvmCompilerAllocTemp(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -0700447 opRegRegImm(cUnit, kOpLsl, rNewIndex, rlIndex.lowReg, scale);
448 opRegReg(cUnit, kOpAdd, regPtr, rNewIndex);
Bill Buzbeec6f10662010-02-09 11:16:15 -0800449 dvmCompilerFreeTemp(cUnit, rNewIndex);
Bill Buzbee1465db52009-09-23 17:17:35 -0700450 } else {
451 opRegReg(cUnit, kOpAdd, regPtr, rlIndex.lowReg);
452 }
453 rlSrc = loadValueWide(cUnit, rlSrc, kAnyReg);
Ben Cheng11d8f142010-03-24 15:24:19 -0700454
455 HEAP_ACCESS_SHADOW(true);
Bill Buzbee1465db52009-09-23 17:17:35 -0700456 storePair(cUnit, regPtr, rlSrc.lowReg, rlSrc.highReg);
Ben Cheng11d8f142010-03-24 15:24:19 -0700457 HEAP_ACCESS_SHADOW(false);
458
Bill Buzbeec6f10662010-02-09 11:16:15 -0800459 dvmCompilerFreeTemp(cUnit, regPtr);
Bill Buzbee270c1d62009-08-13 16:58:07 -0700460 } else {
Bill Buzbee1465db52009-09-23 17:17:35 -0700461 rlSrc = loadValue(cUnit, rlSrc, kAnyReg);
Ben Cheng11d8f142010-03-24 15:24:19 -0700462
463 HEAP_ACCESS_SHADOW(true);
Bill Buzbee1465db52009-09-23 17:17:35 -0700464 storeBaseIndexed(cUnit, regPtr, rlIndex.lowReg, rlSrc.lowReg,
465 scale, size);
Ben Cheng11d8f142010-03-24 15:24:19 -0700466 HEAP_ACCESS_SHADOW(false);
jeffhao9e45c0b2010-02-03 10:24:05 -0800467 }
Ben Chengba4fc8b2009-06-01 13:00:29 -0700468}
469
Bill Buzbeebe6534f2010-03-12 16:01:35 -0800470/*
471 * Generate array object store
472 * Must use explicit register allocation here because of
473 * call-out to dvmCanPutArrayElement
474 */
475static void genArrayObjectPut(CompilationUnit *cUnit, MIR *mir,
476 RegLocation rlArray, RegLocation rlIndex,
477 RegLocation rlSrc, int scale)
478{
479 int lenOffset = offsetof(ArrayObject, length);
480 int dataOffset = offsetof(ArrayObject, contents);
481
482 dvmCompilerFlushAllRegs(cUnit);
483
484 int regLen = r0;
485 int regPtr = r4PC; /* Preserved across call */
486 int regArray = r1;
487 int regIndex = r7; /* Preserved across call */
488
489 loadValueDirectFixed(cUnit, rlArray, regArray);
490 loadValueDirectFixed(cUnit, rlIndex, regIndex);
491
492 /* null object? */
493 ArmLIR * pcrLabel = NULL;
494
495 if (!(mir->OptimizationFlags & MIR_IGNORE_NULL_CHECK)) {
496 pcrLabel = genNullCheck(cUnit, rlArray.sRegLow, regArray,
497 mir->offset, NULL);
498 }
499
500 if (!(mir->OptimizationFlags & MIR_IGNORE_RANGE_CHECK)) {
501 /* Get len */
502 loadWordDisp(cUnit, regArray, lenOffset, regLen);
503 /* regPtr -> array data */
504 opRegRegImm(cUnit, kOpAdd, regPtr, regArray, dataOffset);
505 genBoundsCheck(cUnit, regIndex, regLen, mir->offset,
506 pcrLabel);
507 } else {
508 /* regPtr -> array data */
509 opRegRegImm(cUnit, kOpAdd, regPtr, regArray, dataOffset);
510 }
511
512 /* Get object to store */
513 loadValueDirectFixed(cUnit, rlSrc, r0);
Ben Chengbd1326d2010-04-02 15:04:53 -0700514 LOAD_FUNC_ADDR(cUnit, r2, (int)dvmCanPutArrayElement);
Bill Buzbeebe6534f2010-03-12 16:01:35 -0800515
516 /* Are we storing null? If so, avoid check */
517 opRegImm(cUnit, kOpCmp, r0, 0);
518 ArmLIR *branchOver = opCondBranch(cUnit, kArmCondEq);
519
520 /* Make sure the types are compatible */
521 loadWordDisp(cUnit, regArray, offsetof(Object, clazz), r1);
522 loadWordDisp(cUnit, r0, offsetof(Object, clazz), r0);
523 opReg(cUnit, kOpBlx, r2);
524 dvmCompilerClobberCallRegs(cUnit);
Bill Buzbee900a3af2010-03-16 12:41:43 -0700525
526 /*
527 * Using fixed registers here, and counting on r4 and r7 being
528 * preserved across the above call. Tell the register allocation
529 * utilities about the regs we are using directly
530 */
531 dvmCompilerLockTemp(cUnit, regPtr); // r4PC
532 dvmCompilerLockTemp(cUnit, regIndex); // r7
533 dvmCompilerLockTemp(cUnit, r0);
534
Bill Buzbeebe6534f2010-03-12 16:01:35 -0800535 /* Bad? - roll back and re-execute if so */
536 genRegImmCheck(cUnit, kArmCondEq, r0, 0, mir->offset, pcrLabel);
537
538 /* Resume here - must reload element, regPtr & index preserved */
539 loadValueDirectFixed(cUnit, rlSrc, r0);
540
541 ArmLIR *target = newLIR0(cUnit, kArmPseudoTargetLabel);
542 target->defMask = ENCODE_ALL;
543 branchOver->generic.target = (LIR *) target;
544
Ben Cheng11d8f142010-03-24 15:24:19 -0700545 HEAP_ACCESS_SHADOW(true);
Bill Buzbeebe6534f2010-03-12 16:01:35 -0800546 storeBaseIndexed(cUnit, regPtr, regIndex, r0,
547 scale, kWord);
Ben Cheng11d8f142010-03-24 15:24:19 -0700548 HEAP_ACCESS_SHADOW(false);
Bill Buzbeebe6534f2010-03-12 16:01:35 -0800549}
550
Ben Cheng5d90c202009-11-22 23:31:11 -0800551static bool genShiftOpLong(CompilationUnit *cUnit, MIR *mir,
552 RegLocation rlDest, RegLocation rlSrc1,
553 RegLocation rlShift)
Ben Chengba4fc8b2009-06-01 13:00:29 -0700554{
Ben Chenge9695e52009-06-16 16:11:47 -0700555 /*
556 * Don't mess with the regsiters here as there is a particular calling
557 * convention to the out-of-line handler.
558 */
Bill Buzbee1465db52009-09-23 17:17:35 -0700559 RegLocation rlResult;
560
561 loadValueDirectWideFixed(cUnit, rlSrc1, r0, r1);
562 loadValueDirect(cUnit, rlShift, r2);
Ben Chenge9695e52009-06-16 16:11:47 -0700563 switch( mir->dalvikInsn.opCode) {
564 case OP_SHL_LONG:
565 case OP_SHL_LONG_2ADDR:
566 genDispatchToHandler(cUnit, TEMPLATE_SHL_LONG);
567 break;
568 case OP_SHR_LONG:
569 case OP_SHR_LONG_2ADDR:
570 genDispatchToHandler(cUnit, TEMPLATE_SHR_LONG);
571 break;
572 case OP_USHR_LONG:
573 case OP_USHR_LONG_2ADDR:
574 genDispatchToHandler(cUnit, TEMPLATE_USHR_LONG);
575 break;
576 default:
577 return true;
578 }
Bill Buzbeec6f10662010-02-09 11:16:15 -0800579 rlResult = dvmCompilerGetReturnWide(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -0700580 storeValueWide(cUnit, rlDest, rlResult);
Ben Chenge9695e52009-06-16 16:11:47 -0700581 return false;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700582}
Ben Chenge9695e52009-06-16 16:11:47 -0700583
Ben Cheng5d90c202009-11-22 23:31:11 -0800584static bool genArithOpLong(CompilationUnit *cUnit, MIR *mir,
585 RegLocation rlDest, RegLocation rlSrc1,
586 RegLocation rlSrc2)
Ben Chengba4fc8b2009-06-01 13:00:29 -0700587{
Bill Buzbee1465db52009-09-23 17:17:35 -0700588 RegLocation rlResult;
589 OpKind firstOp = kOpBkpt;
590 OpKind secondOp = kOpBkpt;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700591 bool callOut = false;
592 void *callTgt;
593 int retReg = r0;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700594
595 switch (mir->dalvikInsn.opCode) {
596 case OP_NOT_LONG:
Bill Buzbee1465db52009-09-23 17:17:35 -0700597 rlSrc2 = loadValueWide(cUnit, rlSrc2, kCoreReg);
Bill Buzbeec6f10662010-02-09 11:16:15 -0800598 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Bill Buzbee1465db52009-09-23 17:17:35 -0700599 opRegReg(cUnit, kOpMvn, rlResult.lowReg, rlSrc2.lowReg);
600 opRegReg(cUnit, kOpMvn, rlResult.highReg, rlSrc2.highReg);
601 storeValueWide(cUnit, rlDest, rlResult);
602 return false;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700603 break;
604 case OP_ADD_LONG:
605 case OP_ADD_LONG_2ADDR:
Bill Buzbee1465db52009-09-23 17:17:35 -0700606 firstOp = kOpAdd;
607 secondOp = kOpAdc;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700608 break;
609 case OP_SUB_LONG:
610 case OP_SUB_LONG_2ADDR:
Bill Buzbee1465db52009-09-23 17:17:35 -0700611 firstOp = kOpSub;
612 secondOp = kOpSbc;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700613 break;
614 case OP_MUL_LONG:
615 case OP_MUL_LONG_2ADDR:
Bill Buzbee1465db52009-09-23 17:17:35 -0700616 genMulLong(cUnit, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700617 return false;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700618 case OP_DIV_LONG:
619 case OP_DIV_LONG_2ADDR:
620 callOut = true;
621 retReg = r0;
622 callTgt = (void*)__aeabi_ldivmod;
623 break;
624 /* NOTE - result is in r2/r3 instead of r0/r1 */
625 case OP_REM_LONG:
626 case OP_REM_LONG_2ADDR:
627 callOut = true;
628 callTgt = (void*)__aeabi_ldivmod;
629 retReg = r2;
630 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700631 case OP_AND_LONG_2ADDR:
Bill Buzbee1465db52009-09-23 17:17:35 -0700632 case OP_AND_LONG:
633 firstOp = kOpAnd;
634 secondOp = kOpAnd;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700635 break;
636 case OP_OR_LONG:
637 case OP_OR_LONG_2ADDR:
Bill Buzbee1465db52009-09-23 17:17:35 -0700638 firstOp = kOpOr;
639 secondOp = kOpOr;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700640 break;
641 case OP_XOR_LONG:
642 case OP_XOR_LONG_2ADDR:
Bill Buzbee1465db52009-09-23 17:17:35 -0700643 firstOp = kOpXor;
644 secondOp = kOpXor;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700645 break;
Ben Chenge9695e52009-06-16 16:11:47 -0700646 case OP_NEG_LONG: {
Bill Buzbee51ecf602010-01-14 14:27:52 -0800647 //TUNING: can improve this using Thumb2 code
Bill Buzbeec6f10662010-02-09 11:16:15 -0800648 int tReg = dvmCompilerAllocTemp(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -0700649 rlSrc2 = loadValueWide(cUnit, rlSrc2, kCoreReg);
Bill Buzbeec6f10662010-02-09 11:16:15 -0800650 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Ben Chengbd1326d2010-04-02 15:04:53 -0700651 loadConstantNoClobber(cUnit, tReg, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -0700652 opRegRegReg(cUnit, kOpSub, rlResult.lowReg,
Bill Buzbee51ecf602010-01-14 14:27:52 -0800653 tReg, rlSrc2.lowReg);
654 opRegReg(cUnit, kOpSbc, tReg, rlSrc2.highReg);
655 genRegCopy(cUnit, rlResult.highReg, tReg);
Bill Buzbee1465db52009-09-23 17:17:35 -0700656 storeValueWide(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700657 return false;
Ben Chenge9695e52009-06-16 16:11:47 -0700658 }
Ben Chengba4fc8b2009-06-01 13:00:29 -0700659 default:
660 LOGE("Invalid long arith op");
Bill Buzbeefc519dc2010-03-06 23:30:57 -0800661 dvmCompilerAbort(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700662 }
663 if (!callOut) {
Bill Buzbee80cef862010-03-25 10:38:34 -0700664 genLong3Addr(cUnit, mir, firstOp, secondOp, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700665 } else {
Bill Buzbee1465db52009-09-23 17:17:35 -0700666 // Adjust return regs in to handle case of rem returning r2/r3
Bill Buzbeec6f10662010-02-09 11:16:15 -0800667 dvmCompilerFlushAllRegs(cUnit); /* Send everything to home location */
Bill Buzbee1465db52009-09-23 17:17:35 -0700668 loadValueDirectWideFixed(cUnit, rlSrc1, r0, r1);
Ben Chengbd1326d2010-04-02 15:04:53 -0700669 LOAD_FUNC_ADDR(cUnit, rlr, (int) callTgt);
Bill Buzbee1465db52009-09-23 17:17:35 -0700670 loadValueDirectWideFixed(cUnit, rlSrc2, r2, r3);
671 opReg(cUnit, kOpBlx, rlr);
Elliott Hughes6a555132010-02-25 15:41:42 -0800672 dvmCompilerClobberCallRegs(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -0700673 if (retReg == r0)
Bill Buzbeec6f10662010-02-09 11:16:15 -0800674 rlResult = dvmCompilerGetReturnWide(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -0700675 else
Bill Buzbeec6f10662010-02-09 11:16:15 -0800676 rlResult = dvmCompilerGetReturnWideAlt(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -0700677 storeValueWide(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700678 }
679 return false;
680}
681
Ben Cheng5d90c202009-11-22 23:31:11 -0800682static bool genArithOpInt(CompilationUnit *cUnit, MIR *mir,
683 RegLocation rlDest, RegLocation rlSrc1,
684 RegLocation rlSrc2)
Ben Chengba4fc8b2009-06-01 13:00:29 -0700685{
Bill Buzbee1465db52009-09-23 17:17:35 -0700686 OpKind op = kOpBkpt;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700687 bool callOut = false;
688 bool checkZero = false;
Bill Buzbee1465db52009-09-23 17:17:35 -0700689 bool unary = false;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700690 int retReg = r0;
691 void *callTgt;
Bill Buzbee1465db52009-09-23 17:17:35 -0700692 RegLocation rlResult;
Bill Buzbee0e605272009-12-01 14:28:05 -0800693 bool shiftOp = false;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700694
Ben Chengba4fc8b2009-06-01 13:00:29 -0700695 switch (mir->dalvikInsn.opCode) {
696 case OP_NEG_INT:
Bill Buzbee1465db52009-09-23 17:17:35 -0700697 op = kOpNeg;
698 unary = true;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700699 break;
700 case OP_NOT_INT:
Bill Buzbee1465db52009-09-23 17:17:35 -0700701 op = kOpMvn;
702 unary = true;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700703 break;
704 case OP_ADD_INT:
705 case OP_ADD_INT_2ADDR:
Bill Buzbee1465db52009-09-23 17:17:35 -0700706 op = kOpAdd;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700707 break;
708 case OP_SUB_INT:
709 case OP_SUB_INT_2ADDR:
Bill Buzbee1465db52009-09-23 17:17:35 -0700710 op = kOpSub;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700711 break;
712 case OP_MUL_INT:
713 case OP_MUL_INT_2ADDR:
Bill Buzbee1465db52009-09-23 17:17:35 -0700714 op = kOpMul;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700715 break;
716 case OP_DIV_INT:
717 case OP_DIV_INT_2ADDR:
718 callOut = true;
719 checkZero = true;
720 callTgt = __aeabi_idiv;
721 retReg = r0;
722 break;
723 /* NOTE: returns in r1 */
724 case OP_REM_INT:
725 case OP_REM_INT_2ADDR:
726 callOut = true;
727 checkZero = true;
728 callTgt = __aeabi_idivmod;
729 retReg = r1;
730 break;
731 case OP_AND_INT:
732 case OP_AND_INT_2ADDR:
Bill Buzbee1465db52009-09-23 17:17:35 -0700733 op = kOpAnd;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700734 break;
735 case OP_OR_INT:
736 case OP_OR_INT_2ADDR:
Bill Buzbee1465db52009-09-23 17:17:35 -0700737 op = kOpOr;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700738 break;
739 case OP_XOR_INT:
740 case OP_XOR_INT_2ADDR:
Bill Buzbee1465db52009-09-23 17:17:35 -0700741 op = kOpXor;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700742 break;
743 case OP_SHL_INT:
744 case OP_SHL_INT_2ADDR:
Bill Buzbee0e605272009-12-01 14:28:05 -0800745 shiftOp = true;
Bill Buzbee1465db52009-09-23 17:17:35 -0700746 op = kOpLsl;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700747 break;
748 case OP_SHR_INT:
749 case OP_SHR_INT_2ADDR:
Bill Buzbee0e605272009-12-01 14:28:05 -0800750 shiftOp = true;
Bill Buzbee1465db52009-09-23 17:17:35 -0700751 op = kOpAsr;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700752 break;
753 case OP_USHR_INT:
754 case OP_USHR_INT_2ADDR:
Bill Buzbee0e605272009-12-01 14:28:05 -0800755 shiftOp = true;
Bill Buzbee1465db52009-09-23 17:17:35 -0700756 op = kOpLsr;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700757 break;
758 default:
759 LOGE("Invalid word arith op: 0x%x(%d)",
760 mir->dalvikInsn.opCode, mir->dalvikInsn.opCode);
Bill Buzbeefc519dc2010-03-06 23:30:57 -0800761 dvmCompilerAbort(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700762 }
763 if (!callOut) {
Bill Buzbee1465db52009-09-23 17:17:35 -0700764 rlSrc1 = loadValue(cUnit, rlSrc1, kCoreReg);
765 if (unary) {
Bill Buzbeec6f10662010-02-09 11:16:15 -0800766 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Bill Buzbee1465db52009-09-23 17:17:35 -0700767 opRegReg(cUnit, op, rlResult.lowReg,
768 rlSrc1.lowReg);
Ben Chenge9695e52009-06-16 16:11:47 -0700769 } else {
Bill Buzbee1465db52009-09-23 17:17:35 -0700770 rlSrc2 = loadValue(cUnit, rlSrc2, kCoreReg);
Bill Buzbee0e605272009-12-01 14:28:05 -0800771 if (shiftOp) {
Bill Buzbeec6f10662010-02-09 11:16:15 -0800772 int tReg = dvmCompilerAllocTemp(cUnit);
Bill Buzbee0e605272009-12-01 14:28:05 -0800773 opRegRegImm(cUnit, kOpAnd, tReg, rlSrc2.lowReg, 31);
Bill Buzbeec6f10662010-02-09 11:16:15 -0800774 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Bill Buzbee0e605272009-12-01 14:28:05 -0800775 opRegRegReg(cUnit, op, rlResult.lowReg,
776 rlSrc1.lowReg, tReg);
Bill Buzbeec6f10662010-02-09 11:16:15 -0800777 dvmCompilerFreeTemp(cUnit, tReg);
Bill Buzbee0e605272009-12-01 14:28:05 -0800778 } else {
Bill Buzbeec6f10662010-02-09 11:16:15 -0800779 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Bill Buzbee0e605272009-12-01 14:28:05 -0800780 opRegRegReg(cUnit, op, rlResult.lowReg,
781 rlSrc1.lowReg, rlSrc2.lowReg);
782 }
Ben Chenge9695e52009-06-16 16:11:47 -0700783 }
Bill Buzbee1465db52009-09-23 17:17:35 -0700784 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700785 } else {
Bill Buzbee1465db52009-09-23 17:17:35 -0700786 RegLocation rlResult;
Bill Buzbeec6f10662010-02-09 11:16:15 -0800787 dvmCompilerFlushAllRegs(cUnit); /* Send everything to home location */
Bill Buzbee1465db52009-09-23 17:17:35 -0700788 loadValueDirectFixed(cUnit, rlSrc2, r1);
Ben Chengbd1326d2010-04-02 15:04:53 -0700789 LOAD_FUNC_ADDR(cUnit, r2, (int) callTgt);
Bill Buzbee1465db52009-09-23 17:17:35 -0700790 loadValueDirectFixed(cUnit, rlSrc1, r0);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700791 if (checkZero) {
Bill Buzbee1465db52009-09-23 17:17:35 -0700792 genNullCheck(cUnit, rlSrc2.sRegLow, r1, mir->offset, NULL);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700793 }
Bill Buzbee1465db52009-09-23 17:17:35 -0700794 opReg(cUnit, kOpBlx, r2);
Elliott Hughes6a555132010-02-25 15:41:42 -0800795 dvmCompilerClobberCallRegs(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -0700796 if (retReg == r0)
Bill Buzbeec6f10662010-02-09 11:16:15 -0800797 rlResult = dvmCompilerGetReturn(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -0700798 else
Bill Buzbeec6f10662010-02-09 11:16:15 -0800799 rlResult = dvmCompilerGetReturnAlt(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -0700800 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700801 }
802 return false;
803}
804
Ben Cheng5d90c202009-11-22 23:31:11 -0800805static bool genArithOp(CompilationUnit *cUnit, MIR *mir)
Ben Chengba4fc8b2009-06-01 13:00:29 -0700806{
807 OpCode opCode = mir->dalvikInsn.opCode;
Bill Buzbee1465db52009-09-23 17:17:35 -0700808 RegLocation rlDest;
809 RegLocation rlSrc1;
810 RegLocation rlSrc2;
811 /* Deduce sizes of operands */
812 if (mir->ssaRep->numUses == 2) {
Bill Buzbeec6f10662010-02-09 11:16:15 -0800813 rlSrc1 = dvmCompilerGetSrc(cUnit, mir, 0);
814 rlSrc2 = dvmCompilerGetSrc(cUnit, mir, 1);
Bill Buzbee1465db52009-09-23 17:17:35 -0700815 } else if (mir->ssaRep->numUses == 3) {
Bill Buzbeec6f10662010-02-09 11:16:15 -0800816 rlSrc1 = dvmCompilerGetSrcWide(cUnit, mir, 0, 1);
817 rlSrc2 = dvmCompilerGetSrc(cUnit, mir, 2);
Bill Buzbee1465db52009-09-23 17:17:35 -0700818 } else {
Bill Buzbeec6f10662010-02-09 11:16:15 -0800819 rlSrc1 = dvmCompilerGetSrcWide(cUnit, mir, 0, 1);
820 rlSrc2 = dvmCompilerGetSrcWide(cUnit, mir, 2, 3);
Bill Buzbee1465db52009-09-23 17:17:35 -0700821 assert(mir->ssaRep->numUses == 4);
822 }
823 if (mir->ssaRep->numDefs == 1) {
Bill Buzbeec6f10662010-02-09 11:16:15 -0800824 rlDest = dvmCompilerGetDest(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -0700825 } else {
826 assert(mir->ssaRep->numDefs == 2);
Bill Buzbeec6f10662010-02-09 11:16:15 -0800827 rlDest = dvmCompilerGetDestWide(cUnit, mir, 0, 1);
Bill Buzbee1465db52009-09-23 17:17:35 -0700828 }
Ben Chengba4fc8b2009-06-01 13:00:29 -0700829
830 if ((opCode >= OP_ADD_LONG_2ADDR) && (opCode <= OP_XOR_LONG_2ADDR)) {
Ben Cheng5d90c202009-11-22 23:31:11 -0800831 return genArithOpLong(cUnit,mir, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700832 }
833 if ((opCode >= OP_ADD_LONG) && (opCode <= OP_XOR_LONG)) {
Ben Cheng5d90c202009-11-22 23:31:11 -0800834 return genArithOpLong(cUnit,mir, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700835 }
836 if ((opCode >= OP_SHL_LONG_2ADDR) && (opCode <= OP_USHR_LONG_2ADDR)) {
Ben Cheng5d90c202009-11-22 23:31:11 -0800837 return genShiftOpLong(cUnit,mir, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700838 }
839 if ((opCode >= OP_SHL_LONG) && (opCode <= OP_USHR_LONG)) {
Ben Cheng5d90c202009-11-22 23:31:11 -0800840 return genShiftOpLong(cUnit,mir, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700841 }
842 if ((opCode >= OP_ADD_INT_2ADDR) && (opCode <= OP_USHR_INT_2ADDR)) {
Ben Cheng5d90c202009-11-22 23:31:11 -0800843 return genArithOpInt(cUnit,mir, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700844 }
845 if ((opCode >= OP_ADD_INT) && (opCode <= OP_USHR_INT)) {
Ben Cheng5d90c202009-11-22 23:31:11 -0800846 return genArithOpInt(cUnit,mir, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700847 }
848 if ((opCode >= OP_ADD_FLOAT_2ADDR) && (opCode <= OP_REM_FLOAT_2ADDR)) {
Ben Cheng5d90c202009-11-22 23:31:11 -0800849 return genArithOpFloat(cUnit,mir, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700850 }
851 if ((opCode >= OP_ADD_FLOAT) && (opCode <= OP_REM_FLOAT)) {
Ben Cheng5d90c202009-11-22 23:31:11 -0800852 return genArithOpFloat(cUnit, mir, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700853 }
854 if ((opCode >= OP_ADD_DOUBLE_2ADDR) && (opCode <= OP_REM_DOUBLE_2ADDR)) {
Ben Cheng5d90c202009-11-22 23:31:11 -0800855 return genArithOpDouble(cUnit,mir, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700856 }
857 if ((opCode >= OP_ADD_DOUBLE) && (opCode <= OP_REM_DOUBLE)) {
Ben Cheng5d90c202009-11-22 23:31:11 -0800858 return genArithOpDouble(cUnit,mir, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700859 }
860 return true;
861}
862
Bill Buzbee1465db52009-09-23 17:17:35 -0700863/* Generate unconditional branch instructions */
864static ArmLIR *genUnconditionalBranch(CompilationUnit *cUnit, ArmLIR *target)
865{
866 ArmLIR *branch = opNone(cUnit, kOpUncondBr);
867 branch->generic.target = (LIR *) target;
868 return branch;
869}
870
Bill Buzbee1465db52009-09-23 17:17:35 -0700871/* Perform the actual operation for OP_RETURN_* */
872static void genReturnCommon(CompilationUnit *cUnit, MIR *mir)
873{
874 genDispatchToHandler(cUnit, TEMPLATE_RETURN);
Ben Cheng978738d2010-05-13 13:45:57 -0700875#if defined(WITH_JIT_TUNING)
Bill Buzbee1465db52009-09-23 17:17:35 -0700876 gDvmJit.returnOp++;
877#endif
878 int dPC = (int) (cUnit->method->insns + mir->offset);
879 /* Insert branch, but defer setting of target */
880 ArmLIR *branch = genUnconditionalBranch(cUnit, NULL);
881 /* Set up the place holder to reconstruct this Dalvik PC */
882 ArmLIR *pcrLabel = dvmCompilerNew(sizeof(ArmLIR), true);
Ben Chenga4973592010-03-31 11:59:18 -0700883 pcrLabel->opCode = kArmPseudoPCReconstructionCell;
Bill Buzbee1465db52009-09-23 17:17:35 -0700884 pcrLabel->operands[0] = dPC;
885 pcrLabel->operands[1] = mir->offset;
886 /* Insert the place holder to the growable list */
887 dvmInsertGrowableList(&cUnit->pcReconstructionList, pcrLabel);
888 /* Branch to the PC reconstruction code */
889 branch->generic.target = (LIR *) pcrLabel;
890}
891
Ben Chengba4fc8b2009-06-01 13:00:29 -0700892static void genProcessArgsNoRange(CompilationUnit *cUnit, MIR *mir,
893 DecodedInstruction *dInsn,
Bill Buzbee89efc3d2009-07-28 11:22:22 -0700894 ArmLIR **pcrLabel)
Ben Chengba4fc8b2009-06-01 13:00:29 -0700895{
896 unsigned int i;
897 unsigned int regMask = 0;
Bill Buzbee1465db52009-09-23 17:17:35 -0700898 RegLocation rlArg;
899 int numDone = 0;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700900
Bill Buzbee1465db52009-09-23 17:17:35 -0700901 /*
902 * Load arguments to r0..r4. Note that these registers may contain
903 * live values, so we clobber them immediately after loading to prevent
904 * them from being used as sources for subsequent loads.
905 */
Bill Buzbeec6f10662010-02-09 11:16:15 -0800906 dvmCompilerLockAllTemps(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700907 for (i = 0; i < dInsn->vA; i++) {
908 regMask |= 1 << i;
Bill Buzbeec6f10662010-02-09 11:16:15 -0800909 rlArg = dvmCompilerGetSrc(cUnit, mir, numDone++);
Bill Buzbee1465db52009-09-23 17:17:35 -0700910 loadValueDirectFixed(cUnit, rlArg, i);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700911 }
912 if (regMask) {
913 /* Up to 5 args are pushed on top of FP - sizeofStackSaveArea */
Bill Buzbee1465db52009-09-23 17:17:35 -0700914 opRegRegImm(cUnit, kOpSub, r7, rFP,
915 sizeof(StackSaveArea) + (dInsn->vA << 2));
Ben Chengba4fc8b2009-06-01 13:00:29 -0700916 /* generate null check */
917 if (pcrLabel) {
Bill Buzbeec6f10662010-02-09 11:16:15 -0800918 *pcrLabel = genNullCheck(cUnit, dvmCompilerSSASrc(mir, 0), r0,
Bill Buzbee1465db52009-09-23 17:17:35 -0700919 mir->offset, NULL);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700920 }
Bill Buzbee270c1d62009-08-13 16:58:07 -0700921 storeMultiple(cUnit, r7, regMask);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700922 }
923}
924
925static void genProcessArgsRange(CompilationUnit *cUnit, MIR *mir,
926 DecodedInstruction *dInsn,
Bill Buzbee89efc3d2009-07-28 11:22:22 -0700927 ArmLIR **pcrLabel)
Ben Chengba4fc8b2009-06-01 13:00:29 -0700928{
929 int srcOffset = dInsn->vC << 2;
930 int numArgs = dInsn->vA;
931 int regMask;
Bill Buzbee1465db52009-09-23 17:17:35 -0700932
933 /*
934 * Note: here, all promoted registers will have been flushed
935 * back to the Dalvik base locations, so register usage restrictins
936 * are lifted. All parms loaded from original Dalvik register
937 * region - even though some might conceivably have valid copies
938 * cached in a preserved register.
939 */
Bill Buzbeec6f10662010-02-09 11:16:15 -0800940 dvmCompilerLockAllTemps(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -0700941
Ben Chengba4fc8b2009-06-01 13:00:29 -0700942 /*
943 * r4PC : &rFP[vC]
944 * r7: &newFP[0]
945 */
Bill Buzbee1465db52009-09-23 17:17:35 -0700946 opRegRegImm(cUnit, kOpAdd, r4PC, rFP, srcOffset);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700947 /* load [r0 .. min(numArgs,4)] */
948 regMask = (1 << ((numArgs < 4) ? numArgs : 4)) - 1;
Ben Chengd7d426a2009-09-22 11:23:36 -0700949 /*
950 * Protect the loadMultiple instruction from being reordered with other
951 * Dalvik stack accesses.
952 */
Bill Buzbee270c1d62009-08-13 16:58:07 -0700953 loadMultiple(cUnit, r4PC, regMask);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700954
Bill Buzbee1465db52009-09-23 17:17:35 -0700955 opRegRegImm(cUnit, kOpSub, r7, rFP,
956 sizeof(StackSaveArea) + (numArgs << 2));
Ben Chengba4fc8b2009-06-01 13:00:29 -0700957 /* generate null check */
958 if (pcrLabel) {
Bill Buzbeec6f10662010-02-09 11:16:15 -0800959 *pcrLabel = genNullCheck(cUnit, dvmCompilerSSASrc(mir, 0), r0,
Bill Buzbee1465db52009-09-23 17:17:35 -0700960 mir->offset, NULL);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700961 }
962
963 /*
964 * Handle remaining 4n arguments:
965 * store previously loaded 4 values and load the next 4 values
966 */
967 if (numArgs >= 8) {
Bill Buzbee89efc3d2009-07-28 11:22:22 -0700968 ArmLIR *loopLabel = NULL;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700969 /*
970 * r0 contains "this" and it will be used later, so push it to the stack
Bill Buzbee270c1d62009-08-13 16:58:07 -0700971 * first. Pushing r5 (rFP) is just for stack alignment purposes.
Ben Chengba4fc8b2009-06-01 13:00:29 -0700972 */
Bill Buzbee1465db52009-09-23 17:17:35 -0700973 opImm(cUnit, kOpPush, (1 << r0 | 1 << rFP));
Ben Chengba4fc8b2009-06-01 13:00:29 -0700974 /* No need to generate the loop structure if numArgs <= 11 */
975 if (numArgs > 11) {
976 loadConstant(cUnit, 5, ((numArgs - 4) >> 2) << 2);
Bill Buzbee1465db52009-09-23 17:17:35 -0700977 loopLabel = newLIR0(cUnit, kArmPseudoTargetLabel);
Ben Chengd7d426a2009-09-22 11:23:36 -0700978 loopLabel->defMask = ENCODE_ALL;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700979 }
Bill Buzbee270c1d62009-08-13 16:58:07 -0700980 storeMultiple(cUnit, r7, regMask);
Ben Chengd7d426a2009-09-22 11:23:36 -0700981 /*
982 * Protect the loadMultiple instruction from being reordered with other
983 * Dalvik stack accesses.
984 */
Bill Buzbee270c1d62009-08-13 16:58:07 -0700985 loadMultiple(cUnit, r4PC, regMask);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700986 /* No need to generate the loop structure if numArgs <= 11 */
987 if (numArgs > 11) {
Bill Buzbee1465db52009-09-23 17:17:35 -0700988 opRegImm(cUnit, kOpSub, rFP, 4);
989 genConditionalBranch(cUnit, kArmCondNe, loopLabel);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700990 }
991 }
992
993 /* Save the last batch of loaded values */
Bill Buzbee270c1d62009-08-13 16:58:07 -0700994 storeMultiple(cUnit, r7, regMask);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700995
996 /* Generate the loop epilogue - don't use r0 */
997 if ((numArgs > 4) && (numArgs % 4)) {
998 regMask = ((1 << (numArgs & 0x3)) - 1) << 1;
Ben Chengd7d426a2009-09-22 11:23:36 -0700999 /*
1000 * Protect the loadMultiple instruction from being reordered with other
1001 * Dalvik stack accesses.
1002 */
Bill Buzbee270c1d62009-08-13 16:58:07 -07001003 loadMultiple(cUnit, r4PC, regMask);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001004 }
1005 if (numArgs >= 8)
Bill Buzbee1465db52009-09-23 17:17:35 -07001006 opImm(cUnit, kOpPop, (1 << r0 | 1 << rFP));
Ben Chengba4fc8b2009-06-01 13:00:29 -07001007
1008 /* Save the modulo 4 arguments */
1009 if ((numArgs > 4) && (numArgs % 4)) {
Bill Buzbee270c1d62009-08-13 16:58:07 -07001010 storeMultiple(cUnit, r7, regMask);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001011 }
1012}
1013
Ben Cheng38329f52009-07-07 14:19:20 -07001014/*
1015 * Generate code to setup the call stack then jump to the chaining cell if it
1016 * is not a native method.
1017 */
1018static void genInvokeSingletonCommon(CompilationUnit *cUnit, MIR *mir,
Bill Buzbee89efc3d2009-07-28 11:22:22 -07001019 BasicBlock *bb, ArmLIR *labelList,
1020 ArmLIR *pcrLabel,
Ben Cheng38329f52009-07-07 14:19:20 -07001021 const Method *calleeMethod)
Ben Chengba4fc8b2009-06-01 13:00:29 -07001022{
Bill Buzbee1465db52009-09-23 17:17:35 -07001023 /*
1024 * Note: all Dalvik register state should be flushed to
1025 * memory by the point, so register usage restrictions no
1026 * longer apply. All temp & preserved registers may be used.
1027 */
Bill Buzbeec6f10662010-02-09 11:16:15 -08001028 dvmCompilerLockAllTemps(cUnit);
Bill Buzbee89efc3d2009-07-28 11:22:22 -07001029 ArmLIR *retChainingCell = &labelList[bb->fallThrough->id];
Ben Chengba4fc8b2009-06-01 13:00:29 -07001030
1031 /* r1 = &retChainingCell */
Bill Buzbeec6f10662010-02-09 11:16:15 -08001032 dvmCompilerLockTemp(cUnit, r1);
Bill Buzbee1465db52009-09-23 17:17:35 -07001033 ArmLIR *addrRetChain = opRegRegImm(cUnit, kOpAdd, r1, rpc, 0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001034 /* r4PC = dalvikCallsite */
1035 loadConstant(cUnit, r4PC,
1036 (int) (cUnit->method->insns + mir->offset));
1037 addrRetChain->generic.target = (LIR *) retChainingCell;
1038 /*
Ben Cheng38329f52009-07-07 14:19:20 -07001039 * r0 = calleeMethod (loaded upon calling genInvokeSingletonCommon)
Ben Chengba4fc8b2009-06-01 13:00:29 -07001040 * r1 = &ChainingCell
1041 * r4PC = callsiteDPC
1042 */
1043 if (dvmIsNativeMethod(calleeMethod)) {
Ben Cheng38329f52009-07-07 14:19:20 -07001044 genDispatchToHandler(cUnit, TEMPLATE_INVOKE_METHOD_NATIVE);
Ben Cheng978738d2010-05-13 13:45:57 -07001045#if defined(WITH_JIT_TUNING)
Ben Cheng38329f52009-07-07 14:19:20 -07001046 gDvmJit.invokeNative++;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001047#endif
1048 } else {
1049 genDispatchToHandler(cUnit, TEMPLATE_INVOKE_METHOD_CHAIN);
Ben Cheng978738d2010-05-13 13:45:57 -07001050#if defined(WITH_JIT_TUNING)
Ben Cheng86717f72010-03-05 15:27:21 -08001051 gDvmJit.invokeMonomorphic++;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001052#endif
Ben Cheng38329f52009-07-07 14:19:20 -07001053 /* Branch to the chaining cell */
Ben Chengba4fc8b2009-06-01 13:00:29 -07001054 genUnconditionalBranch(cUnit, &labelList[bb->taken->id]);
1055 }
1056 /* Handle exceptions using the interpreter */
1057 genTrap(cUnit, mir->offset, pcrLabel);
1058}
1059
Ben Cheng38329f52009-07-07 14:19:20 -07001060/*
1061 * Generate code to check the validity of a predicted chain and take actions
1062 * based on the result.
1063 *
1064 * 0x426a99aa : ldr r4, [pc, #72] --> r4 <- dalvikPC of this invoke
1065 * 0x426a99ac : add r1, pc, #32 --> r1 <- &retChainingCell
1066 * 0x426a99ae : add r2, pc, #40 --> r2 <- &predictedChainingCell
1067 * 0x426a99b0 : blx_1 0x426a918c --+ TEMPLATE_INVOKE_METHOD_PREDICTED_CHAIN
1068 * 0x426a99b2 : blx_2 see above --+
1069 * 0x426a99b4 : b 0x426a99d8 --> off to the predicted chain
1070 * 0x426a99b6 : b 0x426a99c8 --> punt to the interpreter
1071 * 0x426a99b8 : ldr r0, [r7, #44] --> r0 <- this->class->vtable[methodIdx]
1072 * 0x426a99ba : cmp r1, #0 --> compare r1 (rechain count) against 0
1073 * 0x426a99bc : bgt 0x426a99c2 --> >=0? don't rechain
1074 * 0x426a99be : ldr r7, [r6, #96] --+ dvmJitToPatchPredictedChain
1075 * 0x426a99c0 : blx r7 --+
1076 * 0x426a99c2 : add r1, pc, #12 --> r1 <- &retChainingCell
1077 * 0x426a99c4 : blx_1 0x426a9098 --+ TEMPLATE_INVOKE_METHOD_NO_OPT
1078 * 0x426a99c6 : blx_2 see above --+
1079 */
1080static void genInvokeVirtualCommon(CompilationUnit *cUnit, MIR *mir,
1081 int methodIndex,
Bill Buzbee89efc3d2009-07-28 11:22:22 -07001082 ArmLIR *retChainingCell,
1083 ArmLIR *predChainingCell,
1084 ArmLIR *pcrLabel)
Ben Cheng38329f52009-07-07 14:19:20 -07001085{
Bill Buzbee1465db52009-09-23 17:17:35 -07001086 /*
1087 * Note: all Dalvik register state should be flushed to
1088 * memory by the point, so register usage restrictions no
1089 * longer apply. Lock temps to prevent them from being
1090 * allocated by utility routines.
1091 */
Bill Buzbeec6f10662010-02-09 11:16:15 -08001092 dvmCompilerLockAllTemps(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07001093
Ben Cheng38329f52009-07-07 14:19:20 -07001094 /* "this" is already left in r0 by genProcessArgs* */
1095
1096 /* r4PC = dalvikCallsite */
1097 loadConstant(cUnit, r4PC,
1098 (int) (cUnit->method->insns + mir->offset));
1099
1100 /* r1 = &retChainingCell */
Bill Buzbee1465db52009-09-23 17:17:35 -07001101 ArmLIR *addrRetChain = opRegRegImm(cUnit, kOpAdd, r1, rpc, 0);
Ben Cheng38329f52009-07-07 14:19:20 -07001102 addrRetChain->generic.target = (LIR *) retChainingCell;
1103
1104 /* r2 = &predictedChainingCell */
Bill Buzbee1465db52009-09-23 17:17:35 -07001105 ArmLIR *predictedChainingCell = opRegRegImm(cUnit, kOpAdd, r2, rpc, 0);
Ben Cheng38329f52009-07-07 14:19:20 -07001106 predictedChainingCell->generic.target = (LIR *) predChainingCell;
1107
1108 genDispatchToHandler(cUnit, TEMPLATE_INVOKE_METHOD_PREDICTED_CHAIN);
1109
1110 /* return through lr - jump to the chaining cell */
1111 genUnconditionalBranch(cUnit, predChainingCell);
1112
1113 /*
1114 * null-check on "this" may have been eliminated, but we still need a PC-
1115 * reconstruction label for stack overflow bailout.
1116 */
1117 if (pcrLabel == NULL) {
1118 int dPC = (int) (cUnit->method->insns + mir->offset);
Bill Buzbee89efc3d2009-07-28 11:22:22 -07001119 pcrLabel = dvmCompilerNew(sizeof(ArmLIR), true);
Ben Chenga4973592010-03-31 11:59:18 -07001120 pcrLabel->opCode = kArmPseudoPCReconstructionCell;
Ben Cheng38329f52009-07-07 14:19:20 -07001121 pcrLabel->operands[0] = dPC;
1122 pcrLabel->operands[1] = mir->offset;
1123 /* Insert the place holder to the growable list */
1124 dvmInsertGrowableList(&cUnit->pcReconstructionList, pcrLabel);
1125 }
1126
1127 /* return through lr+2 - punt to the interpreter */
1128 genUnconditionalBranch(cUnit, pcrLabel);
1129
1130 /*
1131 * return through lr+4 - fully resolve the callee method.
1132 * r1 <- count
1133 * r2 <- &predictedChainCell
1134 * r3 <- this->class
1135 * r4 <- dPC
1136 * r7 <- this->class->vtable
1137 */
1138
1139 /* r0 <- calleeMethod */
Bill Buzbee270c1d62009-08-13 16:58:07 -07001140 loadWordDisp(cUnit, r7, methodIndex * 4, r0);
Ben Cheng38329f52009-07-07 14:19:20 -07001141
1142 /* Check if rechain limit is reached */
Bill Buzbee1465db52009-09-23 17:17:35 -07001143 opRegImm(cUnit, kOpCmp, r1, 0);
Ben Cheng38329f52009-07-07 14:19:20 -07001144
Bill Buzbee1465db52009-09-23 17:17:35 -07001145 ArmLIR *bypassRechaining = opCondBranch(cUnit, kArmCondGt);
Ben Cheng38329f52009-07-07 14:19:20 -07001146
Bill Buzbee270c1d62009-08-13 16:58:07 -07001147 loadWordDisp(cUnit, rGLUE, offsetof(InterpState,
1148 jitToInterpEntries.dvmJitToPatchPredictedChain), r7);
Ben Cheng38329f52009-07-07 14:19:20 -07001149
1150 /*
1151 * r0 = calleeMethod
1152 * r2 = &predictedChainingCell
1153 * r3 = class
1154 *
1155 * &returnChainingCell has been loaded into r1 but is not needed
1156 * when patching the chaining cell and will be clobbered upon
1157 * returning so it will be reconstructed again.
1158 */
Bill Buzbee1465db52009-09-23 17:17:35 -07001159 opReg(cUnit, kOpBlx, r7);
Ben Cheng38329f52009-07-07 14:19:20 -07001160
1161 /* r1 = &retChainingCell */
Bill Buzbee1465db52009-09-23 17:17:35 -07001162 addrRetChain = opRegRegImm(cUnit, kOpAdd, r1, rpc, 0);
Ben Cheng38329f52009-07-07 14:19:20 -07001163 addrRetChain->generic.target = (LIR *) retChainingCell;
1164
1165 bypassRechaining->generic.target = (LIR *) addrRetChain;
1166 /*
1167 * r0 = calleeMethod,
1168 * r1 = &ChainingCell,
1169 * r4PC = callsiteDPC,
1170 */
1171 genDispatchToHandler(cUnit, TEMPLATE_INVOKE_METHOD_NO_OPT);
Ben Cheng978738d2010-05-13 13:45:57 -07001172#if defined(WITH_JIT_TUNING)
Ben Cheng86717f72010-03-05 15:27:21 -08001173 gDvmJit.invokePolymorphic++;
Ben Cheng38329f52009-07-07 14:19:20 -07001174#endif
1175 /* Handle exceptions using the interpreter */
1176 genTrap(cUnit, mir->offset, pcrLabel);
1177}
1178
1179/*
1180 * Up calling this function, "this" is stored in r0. The actual class will be
1181 * chased down off r0 and the predicted one will be retrieved through
1182 * predictedChainingCell then a comparison is performed to see whether the
1183 * previously established chaining is still valid.
1184 *
1185 * The return LIR is a branch based on the comparison result. The actual branch
1186 * target will be setup in the caller.
1187 */
Bill Buzbee89efc3d2009-07-28 11:22:22 -07001188static ArmLIR *genCheckPredictedChain(CompilationUnit *cUnit,
1189 ArmLIR *predChainingCell,
1190 ArmLIR *retChainingCell,
Ben Cheng38329f52009-07-07 14:19:20 -07001191 MIR *mir)
1192{
Bill Buzbee1465db52009-09-23 17:17:35 -07001193 /*
1194 * Note: all Dalvik register state should be flushed to
1195 * memory by the point, so register usage restrictions no
1196 * longer apply. All temp & preserved registers may be used.
1197 */
Bill Buzbeec6f10662010-02-09 11:16:15 -08001198 dvmCompilerLockAllTemps(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07001199
Ben Cheng38329f52009-07-07 14:19:20 -07001200 /* r3 now contains this->clazz */
Bill Buzbee270c1d62009-08-13 16:58:07 -07001201 loadWordDisp(cUnit, r0, offsetof(Object, clazz), r3);
Ben Cheng38329f52009-07-07 14:19:20 -07001202
1203 /*
1204 * r2 now contains predicted class. The starting offset of the
1205 * cached value is 4 bytes into the chaining cell.
1206 */
Bill Buzbee89efc3d2009-07-28 11:22:22 -07001207 ArmLIR *getPredictedClass =
Bill Buzbee270c1d62009-08-13 16:58:07 -07001208 loadWordDisp(cUnit, rpc, offsetof(PredictedChainingCell, clazz), r2);
Ben Cheng38329f52009-07-07 14:19:20 -07001209 getPredictedClass->generic.target = (LIR *) predChainingCell;
1210
1211 /*
1212 * r0 now contains predicted method. The starting offset of the
1213 * cached value is 8 bytes into the chaining cell.
1214 */
Bill Buzbee89efc3d2009-07-28 11:22:22 -07001215 ArmLIR *getPredictedMethod =
Bill Buzbee270c1d62009-08-13 16:58:07 -07001216 loadWordDisp(cUnit, rpc, offsetof(PredictedChainingCell, method), r0);
Ben Cheng38329f52009-07-07 14:19:20 -07001217 getPredictedMethod->generic.target = (LIR *) predChainingCell;
1218
1219 /* Load the stats counter to see if it is time to unchain and refresh */
Bill Buzbee89efc3d2009-07-28 11:22:22 -07001220 ArmLIR *getRechainingRequestCount =
Bill Buzbee270c1d62009-08-13 16:58:07 -07001221 loadWordDisp(cUnit, rpc, offsetof(PredictedChainingCell, counter), r7);
Ben Cheng38329f52009-07-07 14:19:20 -07001222 getRechainingRequestCount->generic.target =
1223 (LIR *) predChainingCell;
1224
1225 /* r4PC = dalvikCallsite */
1226 loadConstant(cUnit, r4PC,
1227 (int) (cUnit->method->insns + mir->offset));
1228
1229 /* r1 = &retChainingCell */
Bill Buzbee1465db52009-09-23 17:17:35 -07001230 ArmLIR *addrRetChain = opRegRegImm(cUnit, kOpAdd, r1, rpc, 0);
Ben Cheng38329f52009-07-07 14:19:20 -07001231 addrRetChain->generic.target = (LIR *) retChainingCell;
1232
1233 /* Check if r2 (predicted class) == r3 (actual class) */
Bill Buzbee1465db52009-09-23 17:17:35 -07001234 opRegReg(cUnit, kOpCmp, r2, r3);
Ben Cheng38329f52009-07-07 14:19:20 -07001235
Bill Buzbee1465db52009-09-23 17:17:35 -07001236 return opCondBranch(cUnit, kArmCondEq);
Ben Cheng38329f52009-07-07 14:19:20 -07001237}
1238
Ben Chengba4fc8b2009-06-01 13:00:29 -07001239/* Geneate a branch to go back to the interpreter */
1240static void genPuntToInterp(CompilationUnit *cUnit, unsigned int offset)
1241{
1242 /* r0 = dalvik pc */
Bill Buzbeec6f10662010-02-09 11:16:15 -08001243 dvmCompilerFlushAllRegs(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001244 loadConstant(cUnit, r0, (int) (cUnit->method->insns + offset));
Bill Buzbee270c1d62009-08-13 16:58:07 -07001245 loadWordDisp(cUnit, r0, offsetof(Object, clazz), r3);
1246 loadWordDisp(cUnit, rGLUE, offsetof(InterpState,
1247 jitToInterpEntries.dvmJitToInterpPunt), r1);
Bill Buzbee1465db52009-09-23 17:17:35 -07001248 opReg(cUnit, kOpBlx, r1);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001249}
1250
1251/*
1252 * Attempt to single step one instruction using the interpreter and return
1253 * to the compiled code for the next Dalvik instruction
1254 */
1255static void genInterpSingleStep(CompilationUnit *cUnit, MIR *mir)
1256{
1257 int flags = dexGetInstrFlags(gDvm.instrFlags, mir->dalvikInsn.opCode);
1258 int flagsToCheck = kInstrCanBranch | kInstrCanSwitch | kInstrCanReturn |
1259 kInstrCanThrow;
Bill Buzbee1465db52009-09-23 17:17:35 -07001260
Bill Buzbee45273872010-03-11 11:12:15 -08001261 //If already optimized out, just ignore
1262 if (mir->dalvikInsn.opCode == OP_NOP)
1263 return;
1264
Bill Buzbee1465db52009-09-23 17:17:35 -07001265 //Ugly, but necessary. Flush all Dalvik regs so Interp can find them
Bill Buzbeec6f10662010-02-09 11:16:15 -08001266 dvmCompilerFlushAllRegs(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07001267
Ben Chengba4fc8b2009-06-01 13:00:29 -07001268 if ((mir->next == NULL) || (flags & flagsToCheck)) {
1269 genPuntToInterp(cUnit, mir->offset);
1270 return;
1271 }
1272 int entryAddr = offsetof(InterpState,
1273 jitToInterpEntries.dvmJitToInterpSingleStep);
Bill Buzbee270c1d62009-08-13 16:58:07 -07001274 loadWordDisp(cUnit, rGLUE, entryAddr, r2);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001275 /* r0 = dalvik pc */
1276 loadConstant(cUnit, r0, (int) (cUnit->method->insns + mir->offset));
1277 /* r1 = dalvik pc of following instruction */
1278 loadConstant(cUnit, r1, (int) (cUnit->method->insns + mir->next->offset));
Bill Buzbee1465db52009-09-23 17:17:35 -07001279 opReg(cUnit, kOpBlx, r2);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001280}
1281
Bill Buzbeec1d9ed42010-02-02 11:04:33 -08001282/*
1283 * To prevent a thread in a monitor wait from blocking the Jit from
1284 * resetting the code cache, heavyweight monitor lock will not
1285 * be allowed to return to an existing translation. Instead, we will
1286 * handle them by branching to a handler, which will in turn call the
1287 * runtime lock routine and then branch directly back to the
1288 * interpreter main loop. Given the high cost of the heavyweight
1289 * lock operation, this additional cost should be slight (especially when
1290 * considering that we expect the vast majority of lock operations to
1291 * use the fast-path thin lock bypass).
1292 */
Ben Cheng5d90c202009-11-22 23:31:11 -08001293static void genMonitorPortable(CompilationUnit *cUnit, MIR *mir)
Bill Buzbee270c1d62009-08-13 16:58:07 -07001294{
Bill Buzbeeefbd3c52009-11-04 22:18:40 -08001295 bool isEnter = (mir->dalvikInsn.opCode == OP_MONITOR_ENTER);
Bill Buzbee1465db52009-09-23 17:17:35 -07001296 genExportPC(cUnit, mir);
Bill Buzbeec6f10662010-02-09 11:16:15 -08001297 dvmCompilerFlushAllRegs(cUnit); /* Send everything to home location */
1298 RegLocation rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07001299 loadValueDirectFixed(cUnit, rlSrc, r1);
1300 loadWordDisp(cUnit, rGLUE, offsetof(InterpState, self), r0);
Bill Buzbeec1d9ed42010-02-02 11:04:33 -08001301 genNullCheck(cUnit, rlSrc.sRegLow, r1, mir->offset, NULL);
Bill Buzbeeefbd3c52009-11-04 22:18:40 -08001302 if (isEnter) {
Bill Buzbeec1d9ed42010-02-02 11:04:33 -08001303 /* Get dPC of next insn */
1304 loadConstant(cUnit, r4PC, (int)(cUnit->method->insns + mir->offset +
1305 dexGetInstrWidthAbs(gDvm.instrWidth, OP_MONITOR_ENTER)));
1306#if defined(WITH_DEADLOCK_PREDICTION)
1307 genDispatchToHandler(cUnit, TEMPLATE_MONITOR_ENTER_DEBUG);
1308#else
1309 genDispatchToHandler(cUnit, TEMPLATE_MONITOR_ENTER);
1310#endif
Bill Buzbee1465db52009-09-23 17:17:35 -07001311 } else {
Ben Chengbd1326d2010-04-02 15:04:53 -07001312 LOAD_FUNC_ADDR(cUnit, r2, (int)dvmUnlockObject);
Bill Buzbeec1d9ed42010-02-02 11:04:33 -08001313 /* Do the call */
1314 opReg(cUnit, kOpBlx, r2);
Bill Buzbee6bbdd6b2010-02-16 14:40:01 -08001315 opRegImm(cUnit, kOpCmp, r0, 0); /* Did we throw? */
1316 ArmLIR *branchOver = opCondBranch(cUnit, kArmCondNe);
1317 loadConstant(cUnit, r0,
1318 (int) (cUnit->method->insns + mir->offset +
1319 dexGetInstrWidthAbs(gDvm.instrWidth, OP_MONITOR_EXIT)));
1320 genDispatchToHandler(cUnit, TEMPLATE_THROW_EXCEPTION_COMMON);
1321 ArmLIR *target = newLIR0(cUnit, kArmPseudoTargetLabel);
1322 target->defMask = ENCODE_ALL;
1323 branchOver->generic.target = (LIR *) target;
Elliott Hughes6a555132010-02-25 15:41:42 -08001324 dvmCompilerClobberCallRegs(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07001325 }
Bill Buzbee270c1d62009-08-13 16:58:07 -07001326}
1327
Ben Chengba4fc8b2009-06-01 13:00:29 -07001328/*
1329 * The following are the first-level codegen routines that analyze the format
1330 * of each bytecode then either dispatch special purpose codegen routines
1331 * or produce corresponding Thumb instructions directly.
1332 */
1333
1334static bool handleFmt10t_Fmt20t_Fmt30t(CompilationUnit *cUnit, MIR *mir,
Bill Buzbee89efc3d2009-07-28 11:22:22 -07001335 BasicBlock *bb, ArmLIR *labelList)
Ben Chengba4fc8b2009-06-01 13:00:29 -07001336{
1337 /* For OP_GOTO, OP_GOTO_16, and OP_GOTO_32 */
1338 genUnconditionalBranch(cUnit, &labelList[bb->taken->id]);
1339 return false;
1340}
1341
1342static bool handleFmt10x(CompilationUnit *cUnit, MIR *mir)
1343{
1344 OpCode dalvikOpCode = mir->dalvikInsn.opCode;
1345 if (((dalvikOpCode >= OP_UNUSED_3E) && (dalvikOpCode <= OP_UNUSED_43)) ||
Andy McFadden53878242010-03-05 07:24:27 -08001346 ((dalvikOpCode >= OP_UNUSED_E3) && (dalvikOpCode <= OP_UNUSED_E7))) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07001347 LOGE("Codegen: got unused opcode 0x%x\n",dalvikOpCode);
1348 return true;
1349 }
1350 switch (dalvikOpCode) {
1351 case OP_RETURN_VOID:
1352 genReturnCommon(cUnit,mir);
1353 break;
1354 case OP_UNUSED_73:
1355 case OP_UNUSED_79:
1356 case OP_UNUSED_7A:
1357 LOGE("Codegen: got unused opcode 0x%x\n",dalvikOpCode);
1358 return true;
1359 case OP_NOP:
1360 break;
1361 default:
1362 return true;
1363 }
1364 return false;
1365}
1366
1367static bool handleFmt11n_Fmt31i(CompilationUnit *cUnit, MIR *mir)
1368{
Bill Buzbee1465db52009-09-23 17:17:35 -07001369 RegLocation rlDest;
1370 RegLocation rlResult;
1371 if (mir->ssaRep->numDefs == 2) {
Bill Buzbeec6f10662010-02-09 11:16:15 -08001372 rlDest = dvmCompilerGetDestWide(cUnit, mir, 0, 1);
Bill Buzbee1465db52009-09-23 17:17:35 -07001373 } else {
Bill Buzbeec6f10662010-02-09 11:16:15 -08001374 rlDest = dvmCompilerGetDest(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07001375 }
Ben Chenge9695e52009-06-16 16:11:47 -07001376
Ben Chengba4fc8b2009-06-01 13:00:29 -07001377 switch (mir->dalvikInsn.opCode) {
1378 case OP_CONST:
Ben Chenge9695e52009-06-16 16:11:47 -07001379 case OP_CONST_4: {
Bill Buzbeec6f10662010-02-09 11:16:15 -08001380 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kAnyReg, true);
Ben Chengbd1326d2010-04-02 15:04:53 -07001381 loadConstantNoClobber(cUnit, rlResult.lowReg, mir->dalvikInsn.vB);
Bill Buzbee1465db52009-09-23 17:17:35 -07001382 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001383 break;
Ben Chenge9695e52009-06-16 16:11:47 -07001384 }
1385 case OP_CONST_WIDE_32: {
Bill Buzbee1465db52009-09-23 17:17:35 -07001386 //TUNING: single routine to load constant pair for support doubles
Bill Buzbee964a7b02010-01-28 12:54:19 -08001387 //TUNING: load 0/-1 separately to avoid load dependency
Bill Buzbeec6f10662010-02-09 11:16:15 -08001388 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Ben Chengbd1326d2010-04-02 15:04:53 -07001389 loadConstantNoClobber(cUnit, rlResult.lowReg, mir->dalvikInsn.vB);
Bill Buzbee1465db52009-09-23 17:17:35 -07001390 opRegRegImm(cUnit, kOpAsr, rlResult.highReg,
1391 rlResult.lowReg, 31);
1392 storeValueWide(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001393 break;
Ben Chenge9695e52009-06-16 16:11:47 -07001394 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07001395 default:
1396 return true;
1397 }
1398 return false;
1399}
1400
1401static bool handleFmt21h(CompilationUnit *cUnit, MIR *mir)
1402{
Bill Buzbee1465db52009-09-23 17:17:35 -07001403 RegLocation rlDest;
1404 RegLocation rlResult;
1405 if (mir->ssaRep->numDefs == 2) {
Bill Buzbeec6f10662010-02-09 11:16:15 -08001406 rlDest = dvmCompilerGetDestWide(cUnit, mir, 0, 1);
Bill Buzbee1465db52009-09-23 17:17:35 -07001407 } else {
Bill Buzbeec6f10662010-02-09 11:16:15 -08001408 rlDest = dvmCompilerGetDest(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07001409 }
Bill Buzbeec6f10662010-02-09 11:16:15 -08001410 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kAnyReg, true);
Ben Chenge9695e52009-06-16 16:11:47 -07001411
Ben Chengba4fc8b2009-06-01 13:00:29 -07001412 switch (mir->dalvikInsn.opCode) {
Ben Chenge9695e52009-06-16 16:11:47 -07001413 case OP_CONST_HIGH16: {
Ben Chengbd1326d2010-04-02 15:04:53 -07001414 loadConstantNoClobber(cUnit, rlResult.lowReg,
1415 mir->dalvikInsn.vB << 16);
Bill Buzbee1465db52009-09-23 17:17:35 -07001416 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001417 break;
Ben Chenge9695e52009-06-16 16:11:47 -07001418 }
1419 case OP_CONST_WIDE_HIGH16: {
Bill Buzbee1465db52009-09-23 17:17:35 -07001420 loadConstantValueWide(cUnit, rlResult.lowReg, rlResult.highReg,
1421 0, mir->dalvikInsn.vB << 16);
1422 storeValueWide(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001423 break;
Ben Chenge9695e52009-06-16 16:11:47 -07001424 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07001425 default:
1426 return true;
1427 }
1428 return false;
1429}
1430
1431static bool handleFmt20bc(CompilationUnit *cUnit, MIR *mir)
1432{
1433 /* For OP_THROW_VERIFICATION_ERROR */
1434 genInterpSingleStep(cUnit, mir);
1435 return false;
1436}
1437
1438static bool handleFmt21c_Fmt31c(CompilationUnit *cUnit, MIR *mir)
1439{
Bill Buzbee1465db52009-09-23 17:17:35 -07001440 RegLocation rlResult;
1441 RegLocation rlDest;
1442 RegLocation rlSrc;
Ben Chenge9695e52009-06-16 16:11:47 -07001443
Ben Chengba4fc8b2009-06-01 13:00:29 -07001444 switch (mir->dalvikInsn.opCode) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07001445 case OP_CONST_STRING_JUMBO:
1446 case OP_CONST_STRING: {
1447 void *strPtr = (void*)
1448 (cUnit->method->clazz->pDvmDex->pResStrings[mir->dalvikInsn.vB]);
Ben Chengdd6e8702010-05-07 13:05:47 -07001449
1450 if (strPtr == NULL) {
1451 LOGE("Unexpected null string");
1452 dvmAbort();
1453 }
1454
Bill Buzbeec6f10662010-02-09 11:16:15 -08001455 rlDest = dvmCompilerGetDest(cUnit, mir, 0);
1456 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Ben Chengbd1326d2010-04-02 15:04:53 -07001457 loadConstantNoClobber(cUnit, rlResult.lowReg, (int) strPtr );
Bill Buzbee1465db52009-09-23 17:17:35 -07001458 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001459 break;
1460 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07001461 case OP_CONST_CLASS: {
1462 void *classPtr = (void*)
1463 (cUnit->method->clazz->pDvmDex->pResClasses[mir->dalvikInsn.vB]);
Ben Chengdd6e8702010-05-07 13:05:47 -07001464
1465 if (classPtr == NULL) {
1466 LOGE("Unexpected null class");
1467 dvmAbort();
1468 }
1469
Bill Buzbeec6f10662010-02-09 11:16:15 -08001470 rlDest = dvmCompilerGetDest(cUnit, mir, 0);
1471 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Ben Chengbd1326d2010-04-02 15:04:53 -07001472 loadConstantNoClobber(cUnit, rlResult.lowReg, (int) classPtr );
Bill Buzbee1465db52009-09-23 17:17:35 -07001473 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001474 break;
1475 }
1476 case OP_SGET_OBJECT:
1477 case OP_SGET_BOOLEAN:
1478 case OP_SGET_CHAR:
1479 case OP_SGET_BYTE:
1480 case OP_SGET_SHORT:
1481 case OP_SGET: {
Bill Buzbee50a6bf22009-07-08 13:08:04 -07001482 int valOffset = offsetof(StaticField, value);
Bill Buzbeec6f10662010-02-09 11:16:15 -08001483 int tReg = dvmCompilerAllocTemp(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001484 void *fieldPtr = (void*)
1485 (cUnit->method->clazz->pDvmDex->pResFields[mir->dalvikInsn.vB]);
Ben Chengdd6e8702010-05-07 13:05:47 -07001486
1487 if (fieldPtr == NULL) {
1488 LOGE("Unexpected null static field");
1489 dvmAbort();
1490 }
1491
Bill Buzbeec6f10662010-02-09 11:16:15 -08001492 rlDest = dvmCompilerGetDest(cUnit, mir, 0);
1493 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kAnyReg, true);
Bill Buzbee1465db52009-09-23 17:17:35 -07001494 loadConstant(cUnit, tReg, (int) fieldPtr + valOffset);
Ben Cheng11d8f142010-03-24 15:24:19 -07001495
1496 HEAP_ACCESS_SHADOW(true);
Bill Buzbee1465db52009-09-23 17:17:35 -07001497 loadWordDisp(cUnit, tReg, 0, rlResult.lowReg);
Ben Cheng11d8f142010-03-24 15:24:19 -07001498 HEAP_ACCESS_SHADOW(false);
1499
Bill Buzbee1465db52009-09-23 17:17:35 -07001500 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001501 break;
1502 }
1503 case OP_SGET_WIDE: {
Bill Buzbee50a6bf22009-07-08 13:08:04 -07001504 int valOffset = offsetof(StaticField, value);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001505 void *fieldPtr = (void*)
1506 (cUnit->method->clazz->pDvmDex->pResFields[mir->dalvikInsn.vB]);
Ben Chengdd6e8702010-05-07 13:05:47 -07001507
1508 if (fieldPtr == NULL) {
1509 LOGE("Unexpected null static field");
1510 dvmAbort();
1511 }
1512
Bill Buzbeec6f10662010-02-09 11:16:15 -08001513 int tReg = dvmCompilerAllocTemp(cUnit);
Bill Buzbeec6f10662010-02-09 11:16:15 -08001514 rlDest = dvmCompilerGetDestWide(cUnit, mir, 0, 1);
1515 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kAnyReg, true);
Bill Buzbee1465db52009-09-23 17:17:35 -07001516 loadConstant(cUnit, tReg, (int) fieldPtr + valOffset);
Ben Cheng11d8f142010-03-24 15:24:19 -07001517
1518 HEAP_ACCESS_SHADOW(true);
Bill Buzbee1465db52009-09-23 17:17:35 -07001519 loadPair(cUnit, tReg, rlResult.lowReg, rlResult.highReg);
Ben Cheng11d8f142010-03-24 15:24:19 -07001520 HEAP_ACCESS_SHADOW(false);
1521
Bill Buzbee1465db52009-09-23 17:17:35 -07001522 storeValueWide(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001523 break;
1524 }
1525 case OP_SPUT_OBJECT:
1526 case OP_SPUT_BOOLEAN:
1527 case OP_SPUT_CHAR:
1528 case OP_SPUT_BYTE:
1529 case OP_SPUT_SHORT:
1530 case OP_SPUT: {
Bill Buzbee50a6bf22009-07-08 13:08:04 -07001531 int valOffset = offsetof(StaticField, value);
Bill Buzbeec6f10662010-02-09 11:16:15 -08001532 int tReg = dvmCompilerAllocTemp(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001533 void *fieldPtr = (void*)
1534 (cUnit->method->clazz->pDvmDex->pResFields[mir->dalvikInsn.vB]);
Ben Chenge9695e52009-06-16 16:11:47 -07001535
Ben Chengdd6e8702010-05-07 13:05:47 -07001536 if (fieldPtr == NULL) {
1537 LOGE("Unexpected null static field");
1538 dvmAbort();
1539 }
1540
Bill Buzbeec6f10662010-02-09 11:16:15 -08001541 rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07001542 rlSrc = loadValue(cUnit, rlSrc, kAnyReg);
1543 loadConstant(cUnit, tReg, (int) fieldPtr + valOffset);
Ben Cheng11d8f142010-03-24 15:24:19 -07001544
1545 HEAP_ACCESS_SHADOW(true);
Bill Buzbee1465db52009-09-23 17:17:35 -07001546 storeWordDisp(cUnit, tReg, 0 ,rlSrc.lowReg);
Ben Cheng11d8f142010-03-24 15:24:19 -07001547 HEAP_ACCESS_SHADOW(false);
1548
Ben Chengba4fc8b2009-06-01 13:00:29 -07001549 break;
1550 }
1551 case OP_SPUT_WIDE: {
Bill Buzbeec6f10662010-02-09 11:16:15 -08001552 int tReg = dvmCompilerAllocTemp(cUnit);
Bill Buzbee50a6bf22009-07-08 13:08:04 -07001553 int valOffset = offsetof(StaticField, value);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001554 void *fieldPtr = (void*)
1555 (cUnit->method->clazz->pDvmDex->pResFields[mir->dalvikInsn.vB]);
Ben Chenge9695e52009-06-16 16:11:47 -07001556
Ben Chengdd6e8702010-05-07 13:05:47 -07001557 if (fieldPtr == NULL) {
1558 LOGE("Unexpected null static field");
1559 dvmAbort();
1560 }
1561
Bill Buzbeec6f10662010-02-09 11:16:15 -08001562 rlSrc = dvmCompilerGetSrcWide(cUnit, mir, 0, 1);
Bill Buzbee1465db52009-09-23 17:17:35 -07001563 rlSrc = loadValueWide(cUnit, rlSrc, kAnyReg);
1564 loadConstant(cUnit, tReg, (int) fieldPtr + valOffset);
Ben Cheng11d8f142010-03-24 15:24:19 -07001565
1566 HEAP_ACCESS_SHADOW(true);
Bill Buzbee1465db52009-09-23 17:17:35 -07001567 storePair(cUnit, tReg, rlSrc.lowReg, rlSrc.highReg);
Ben Cheng11d8f142010-03-24 15:24:19 -07001568 HEAP_ACCESS_SHADOW(false);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001569 break;
1570 }
1571 case OP_NEW_INSTANCE: {
Ben Chenge9695e52009-06-16 16:11:47 -07001572 /*
1573 * Obey the calling convention and don't mess with the register
1574 * usage.
1575 */
Ben Chengba4fc8b2009-06-01 13:00:29 -07001576 ClassObject *classPtr = (void*)
1577 (cUnit->method->clazz->pDvmDex->pResClasses[mir->dalvikInsn.vB]);
Ben Chengdd6e8702010-05-07 13:05:47 -07001578
1579 if (classPtr == NULL) {
1580 LOGE("Unexpected null class");
1581 dvmAbort();
1582 }
1583
Ben Cheng79d173c2009-09-29 16:12:51 -07001584 /*
1585 * If it is going to throw, it should not make to the trace to begin
Bill Buzbee1465db52009-09-23 17:17:35 -07001586 * with. However, Alloc might throw, so we need to genExportPC()
Ben Cheng79d173c2009-09-29 16:12:51 -07001587 */
1588 assert((classPtr->accessFlags & (ACC_INTERFACE|ACC_ABSTRACT)) == 0);
Bill Buzbeec6f10662010-02-09 11:16:15 -08001589 dvmCompilerFlushAllRegs(cUnit); /* Everything to home location */
Bill Buzbee1465db52009-09-23 17:17:35 -07001590 genExportPC(cUnit, mir);
Ben Chengbd1326d2010-04-02 15:04:53 -07001591 LOAD_FUNC_ADDR(cUnit, r2, (int)dvmAllocObject);
Ben Chenge9695e52009-06-16 16:11:47 -07001592 loadConstant(cUnit, r0, (int) classPtr);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001593 loadConstant(cUnit, r1, ALLOC_DONT_TRACK);
Bill Buzbee1465db52009-09-23 17:17:35 -07001594 opReg(cUnit, kOpBlx, r2);
Elliott Hughes6a555132010-02-25 15:41:42 -08001595 dvmCompilerClobberCallRegs(cUnit);
Ben Cheng4f489172009-09-27 17:08:35 -07001596 /* generate a branch over if allocation is successful */
Bill Buzbee1465db52009-09-23 17:17:35 -07001597 opRegImm(cUnit, kOpCmp, r0, 0); /* NULL? */
1598 ArmLIR *branchOver = opCondBranch(cUnit, kArmCondNe);
Ben Cheng4f489172009-09-27 17:08:35 -07001599 /*
1600 * OOM exception needs to be thrown here and cannot re-execute
1601 */
1602 loadConstant(cUnit, r0,
1603 (int) (cUnit->method->insns + mir->offset));
1604 genDispatchToHandler(cUnit, TEMPLATE_THROW_EXCEPTION_COMMON);
1605 /* noreturn */
1606
Bill Buzbee1465db52009-09-23 17:17:35 -07001607 ArmLIR *target = newLIR0(cUnit, kArmPseudoTargetLabel);
Ben Cheng4f489172009-09-27 17:08:35 -07001608 target->defMask = ENCODE_ALL;
1609 branchOver->generic.target = (LIR *) target;
Bill Buzbeec6f10662010-02-09 11:16:15 -08001610 rlDest = dvmCompilerGetDest(cUnit, mir, 0);
1611 rlResult = dvmCompilerGetReturn(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07001612 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001613 break;
1614 }
1615 case OP_CHECK_CAST: {
Ben Chenge9695e52009-06-16 16:11:47 -07001616 /*
1617 * Obey the calling convention and don't mess with the register
1618 * usage.
1619 */
Ben Chengba4fc8b2009-06-01 13:00:29 -07001620 ClassObject *classPtr =
1621 (cUnit->method->clazz->pDvmDex->pResClasses[mir->dalvikInsn.vB]);
Bill Buzbee4df41a52009-11-12 17:07:16 -08001622 /*
1623 * Note: It is possible that classPtr is NULL at this point,
1624 * even though this instruction has been successfully interpreted.
1625 * If the previous interpretation had a null source, the
1626 * interpreter would not have bothered to resolve the clazz.
1627 * Bail out to the interpreter in this case, and log it
1628 * so that we can tell if it happens frequently.
1629 */
1630 if (classPtr == NULL) {
Ben Cheng11d8f142010-03-24 15:24:19 -07001631 LOGVV("null clazz in OP_CHECK_CAST, single-stepping");
Bill Buzbee4df41a52009-11-12 17:07:16 -08001632 genInterpSingleStep(cUnit, mir);
1633 return false;
1634 }
Bill Buzbeec6f10662010-02-09 11:16:15 -08001635 dvmCompilerFlushAllRegs(cUnit); /* Everything to home location */
Ben Chengba4fc8b2009-06-01 13:00:29 -07001636 loadConstant(cUnit, r1, (int) classPtr );
Bill Buzbeec6f10662010-02-09 11:16:15 -08001637 rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07001638 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
1639 opRegImm(cUnit, kOpCmp, rlSrc.lowReg, 0); /* Null? */
1640 ArmLIR *branch1 = opCondBranch(cUnit, kArmCondEq);
1641 /*
1642 * rlSrc.lowReg now contains object->clazz. Note that
1643 * it could have been allocated r0, but we're okay so long
1644 * as we don't do anything desctructive until r0 is loaded
1645 * with clazz.
1646 */
Ben Chengba4fc8b2009-06-01 13:00:29 -07001647 /* r0 now contains object->clazz */
Bill Buzbee1465db52009-09-23 17:17:35 -07001648 loadWordDisp(cUnit, rlSrc.lowReg, offsetof(Object, clazz), r0);
Ben Chengbd1326d2010-04-02 15:04:53 -07001649 LOAD_FUNC_ADDR(cUnit, r2, (int)dvmInstanceofNonTrivial);
Bill Buzbee1465db52009-09-23 17:17:35 -07001650 opRegReg(cUnit, kOpCmp, r0, r1);
1651 ArmLIR *branch2 = opCondBranch(cUnit, kArmCondEq);
1652 opReg(cUnit, kOpBlx, r2);
Elliott Hughes6a555132010-02-25 15:41:42 -08001653 dvmCompilerClobberCallRegs(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07001654 /*
1655 * If null, check cast failed - punt to the interpreter. Because
1656 * interpreter will be the one throwing, we don't need to
1657 * genExportPC() here.
1658 */
Bill Buzbee270c1d62009-08-13 16:58:07 -07001659 genZeroCheck(cUnit, r0, mir->offset, NULL);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001660 /* check cast passed - branch target here */
Bill Buzbee1465db52009-09-23 17:17:35 -07001661 ArmLIR *target = newLIR0(cUnit, kArmPseudoTargetLabel);
Ben Chengd7d426a2009-09-22 11:23:36 -07001662 target->defMask = ENCODE_ALL;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001663 branch1->generic.target = (LIR *)target;
1664 branch2->generic.target = (LIR *)target;
1665 break;
1666 }
1667 default:
1668 return true;
1669 }
1670 return false;
1671}
1672
1673static bool handleFmt11x(CompilationUnit *cUnit, MIR *mir)
1674{
1675 OpCode dalvikOpCode = mir->dalvikInsn.opCode;
Bill Buzbee1465db52009-09-23 17:17:35 -07001676 RegLocation rlResult;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001677 switch (dalvikOpCode) {
1678 case OP_MOVE_EXCEPTION: {
1679 int offset = offsetof(InterpState, self);
1680 int exOffset = offsetof(Thread, exception);
Bill Buzbeec6f10662010-02-09 11:16:15 -08001681 int selfReg = dvmCompilerAllocTemp(cUnit);
1682 int resetReg = dvmCompilerAllocTemp(cUnit);
1683 RegLocation rlDest = dvmCompilerGetDest(cUnit, mir, 0);
1684 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Bill Buzbee1465db52009-09-23 17:17:35 -07001685 loadWordDisp(cUnit, rGLUE, offset, selfReg);
Bill Buzbeef9f33282009-11-22 12:45:30 -08001686 loadConstant(cUnit, resetReg, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07001687 loadWordDisp(cUnit, selfReg, exOffset, rlResult.lowReg);
Bill Buzbeef9f33282009-11-22 12:45:30 -08001688 storeWordDisp(cUnit, selfReg, exOffset, resetReg);
Bill Buzbee1465db52009-09-23 17:17:35 -07001689 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001690 break;
1691 }
1692 case OP_MOVE_RESULT:
1693 case OP_MOVE_RESULT_OBJECT: {
Bill Buzbeec6f10662010-02-09 11:16:15 -08001694 RegLocation rlDest = dvmCompilerGetDest(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07001695 RegLocation rlSrc = LOC_DALVIK_RETURN_VAL;
1696 rlSrc.fp = rlDest.fp;
1697 storeValue(cUnit, rlDest, rlSrc);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001698 break;
1699 }
1700 case OP_MOVE_RESULT_WIDE: {
Bill Buzbeec6f10662010-02-09 11:16:15 -08001701 RegLocation rlDest = dvmCompilerGetDestWide(cUnit, mir, 0, 1);
Bill Buzbee1465db52009-09-23 17:17:35 -07001702 RegLocation rlSrc = LOC_DALVIK_RETURN_VAL_WIDE;
1703 rlSrc.fp = rlDest.fp;
1704 storeValueWide(cUnit, rlDest, rlSrc);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001705 break;
1706 }
1707 case OP_RETURN_WIDE: {
Bill Buzbeec6f10662010-02-09 11:16:15 -08001708 RegLocation rlSrc = dvmCompilerGetSrcWide(cUnit, mir, 0, 1);
Bill Buzbee1465db52009-09-23 17:17:35 -07001709 RegLocation rlDest = LOC_DALVIK_RETURN_VAL_WIDE;
1710 rlDest.fp = rlSrc.fp;
1711 storeValueWide(cUnit, rlDest, rlSrc);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001712 genReturnCommon(cUnit,mir);
1713 break;
1714 }
1715 case OP_RETURN:
1716 case OP_RETURN_OBJECT: {
Bill Buzbeec6f10662010-02-09 11:16:15 -08001717 RegLocation rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07001718 RegLocation rlDest = LOC_DALVIK_RETURN_VAL;
1719 rlDest.fp = rlSrc.fp;
1720 storeValue(cUnit, rlDest, rlSrc);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001721 genReturnCommon(cUnit,mir);
1722 break;
1723 }
Bill Buzbee1465db52009-09-23 17:17:35 -07001724 case OP_MONITOR_EXIT:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001725 case OP_MONITOR_ENTER:
Bill Buzbeed0937ef2009-12-22 16:15:39 -08001726#if defined(WITH_DEADLOCK_PREDICTION) || defined(WITH_MONITOR_TRACKING)
Ben Cheng5d90c202009-11-22 23:31:11 -08001727 genMonitorPortable(cUnit, mir);
Bill Buzbee1465db52009-09-23 17:17:35 -07001728#else
Ben Cheng5d90c202009-11-22 23:31:11 -08001729 genMonitor(cUnit, mir);
Bill Buzbee1465db52009-09-23 17:17:35 -07001730#endif
Ben Chengba4fc8b2009-06-01 13:00:29 -07001731 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001732 case OP_THROW: {
1733 genInterpSingleStep(cUnit, mir);
1734 break;
1735 }
1736 default:
1737 return true;
1738 }
1739 return false;
1740}
1741
Bill Buzbeed45ba372009-06-15 17:00:57 -07001742static bool handleFmt12x(CompilationUnit *cUnit, MIR *mir)
1743{
1744 OpCode opCode = mir->dalvikInsn.opCode;
Bill Buzbee1465db52009-09-23 17:17:35 -07001745 RegLocation rlDest;
1746 RegLocation rlSrc;
1747 RegLocation rlResult;
Bill Buzbeed45ba372009-06-15 17:00:57 -07001748
Ben Chengba4fc8b2009-06-01 13:00:29 -07001749 if ( (opCode >= OP_ADD_INT_2ADDR) && (opCode <= OP_REM_DOUBLE_2ADDR)) {
Ben Cheng5d90c202009-11-22 23:31:11 -08001750 return genArithOp( cUnit, mir );
Ben Chengba4fc8b2009-06-01 13:00:29 -07001751 }
1752
Bill Buzbee1465db52009-09-23 17:17:35 -07001753 if (mir->ssaRep->numUses == 2)
Bill Buzbeec6f10662010-02-09 11:16:15 -08001754 rlSrc = dvmCompilerGetSrcWide(cUnit, mir, 0, 1);
Bill Buzbee1465db52009-09-23 17:17:35 -07001755 else
Bill Buzbeec6f10662010-02-09 11:16:15 -08001756 rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07001757 if (mir->ssaRep->numDefs == 2)
Bill Buzbeec6f10662010-02-09 11:16:15 -08001758 rlDest = dvmCompilerGetDestWide(cUnit, mir, 0, 1);
Bill Buzbee1465db52009-09-23 17:17:35 -07001759 else
Bill Buzbeec6f10662010-02-09 11:16:15 -08001760 rlDest = dvmCompilerGetDest(cUnit, mir, 0);
Ben Chenge9695e52009-06-16 16:11:47 -07001761
Ben Chengba4fc8b2009-06-01 13:00:29 -07001762 switch (opCode) {
Bill Buzbee1465db52009-09-23 17:17:35 -07001763 case OP_DOUBLE_TO_INT:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001764 case OP_INT_TO_FLOAT:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001765 case OP_FLOAT_TO_INT:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001766 case OP_DOUBLE_TO_FLOAT:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001767 case OP_FLOAT_TO_DOUBLE:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001768 case OP_INT_TO_DOUBLE:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001769 case OP_FLOAT_TO_LONG:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001770 case OP_LONG_TO_FLOAT:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001771 case OP_DOUBLE_TO_LONG:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001772 case OP_LONG_TO_DOUBLE:
Ben Cheng5d90c202009-11-22 23:31:11 -08001773 return genConversion(cUnit, mir);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001774 case OP_NEG_INT:
1775 case OP_NOT_INT:
Ben Cheng5d90c202009-11-22 23:31:11 -08001776 return genArithOpInt(cUnit, mir, rlDest, rlSrc, rlSrc);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001777 case OP_NEG_LONG:
1778 case OP_NOT_LONG:
Ben Cheng5d90c202009-11-22 23:31:11 -08001779 return genArithOpLong(cUnit, mir, rlDest, rlSrc, rlSrc);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001780 case OP_NEG_FLOAT:
Ben Cheng5d90c202009-11-22 23:31:11 -08001781 return genArithOpFloat(cUnit, mir, rlDest, rlSrc, rlSrc);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001782 case OP_NEG_DOUBLE:
Ben Cheng5d90c202009-11-22 23:31:11 -08001783 return genArithOpDouble(cUnit, mir, rlDest, rlSrc, rlSrc);
Bill Buzbee1465db52009-09-23 17:17:35 -07001784 case OP_MOVE_WIDE:
1785 storeValueWide(cUnit, rlDest, rlSrc);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001786 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07001787 case OP_INT_TO_LONG:
Bill Buzbeec6f10662010-02-09 11:16:15 -08001788 rlSrc = dvmCompilerUpdateLoc(cUnit, rlSrc);
1789 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Bill Buzbee964a7b02010-01-28 12:54:19 -08001790 //TUNING: shouldn't loadValueDirect already check for phys reg?
Bill Buzbee1465db52009-09-23 17:17:35 -07001791 if (rlSrc.location == kLocPhysReg) {
1792 genRegCopy(cUnit, rlResult.lowReg, rlSrc.lowReg);
1793 } else {
1794 loadValueDirect(cUnit, rlSrc, rlResult.lowReg);
1795 }
1796 opRegRegImm(cUnit, kOpAsr, rlResult.highReg,
1797 rlResult.lowReg, 31);
1798 storeValueWide(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001799 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07001800 case OP_LONG_TO_INT:
Bill Buzbeec6f10662010-02-09 11:16:15 -08001801 rlSrc = dvmCompilerUpdateLocWide(cUnit, rlSrc);
1802 rlSrc = dvmCompilerWideToNarrow(cUnit, rlSrc);
Bill Buzbee1465db52009-09-23 17:17:35 -07001803 // Intentional fallthrough
Ben Chengba4fc8b2009-06-01 13:00:29 -07001804 case OP_MOVE:
1805 case OP_MOVE_OBJECT:
Bill Buzbee1465db52009-09-23 17:17:35 -07001806 storeValue(cUnit, rlDest, rlSrc);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001807 break;
1808 case OP_INT_TO_BYTE:
Bill Buzbee1465db52009-09-23 17:17:35 -07001809 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
Bill Buzbeec6f10662010-02-09 11:16:15 -08001810 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Bill Buzbee1465db52009-09-23 17:17:35 -07001811 opRegReg(cUnit, kOp2Byte, rlResult.lowReg, rlSrc.lowReg);
1812 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001813 break;
1814 case OP_INT_TO_SHORT:
Bill Buzbee1465db52009-09-23 17:17:35 -07001815 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
Bill Buzbeec6f10662010-02-09 11:16:15 -08001816 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Bill Buzbee1465db52009-09-23 17:17:35 -07001817 opRegReg(cUnit, kOp2Short, rlResult.lowReg, rlSrc.lowReg);
1818 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001819 break;
1820 case OP_INT_TO_CHAR:
Bill Buzbee1465db52009-09-23 17:17:35 -07001821 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
Bill Buzbeec6f10662010-02-09 11:16:15 -08001822 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Bill Buzbee1465db52009-09-23 17:17:35 -07001823 opRegReg(cUnit, kOp2Char, rlResult.lowReg, rlSrc.lowReg);
1824 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001825 break;
1826 case OP_ARRAY_LENGTH: {
1827 int lenOffset = offsetof(ArrayObject, length);
Bill Buzbee1465db52009-09-23 17:17:35 -07001828 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
1829 genNullCheck(cUnit, rlSrc.sRegLow, rlSrc.lowReg,
1830 mir->offset, NULL);
Bill Buzbeec6f10662010-02-09 11:16:15 -08001831 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Bill Buzbee1465db52009-09-23 17:17:35 -07001832 loadWordDisp(cUnit, rlSrc.lowReg, lenOffset,
1833 rlResult.lowReg);
1834 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001835 break;
1836 }
1837 default:
1838 return true;
1839 }
1840 return false;
1841}
1842
1843static bool handleFmt21s(CompilationUnit *cUnit, MIR *mir)
1844{
1845 OpCode dalvikOpCode = mir->dalvikInsn.opCode;
Bill Buzbee1465db52009-09-23 17:17:35 -07001846 RegLocation rlDest;
1847 RegLocation rlResult;
1848 int BBBB = mir->dalvikInsn.vB;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001849 if (dalvikOpCode == OP_CONST_WIDE_16) {
Bill Buzbeec6f10662010-02-09 11:16:15 -08001850 rlDest = dvmCompilerGetDestWide(cUnit, mir, 0, 1);
1851 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Ben Chengbd1326d2010-04-02 15:04:53 -07001852 loadConstantNoClobber(cUnit, rlResult.lowReg, BBBB);
Bill Buzbee964a7b02010-01-28 12:54:19 -08001853 //TUNING: do high separately to avoid load dependency
Bill Buzbee1465db52009-09-23 17:17:35 -07001854 opRegRegImm(cUnit, kOpAsr, rlResult.highReg, rlResult.lowReg, 31);
1855 storeValueWide(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001856 } else if (dalvikOpCode == OP_CONST_16) {
Bill Buzbeec6f10662010-02-09 11:16:15 -08001857 rlDest = dvmCompilerGetDest(cUnit, mir, 0);
1858 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kAnyReg, true);
Ben Chengbd1326d2010-04-02 15:04:53 -07001859 loadConstantNoClobber(cUnit, rlResult.lowReg, BBBB);
Bill Buzbee1465db52009-09-23 17:17:35 -07001860 storeValue(cUnit, rlDest, rlResult);
1861 } else
Ben Chengba4fc8b2009-06-01 13:00:29 -07001862 return true;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001863 return false;
1864}
1865
1866/* Compare agaist zero */
1867static bool handleFmt21t(CompilationUnit *cUnit, MIR *mir, BasicBlock *bb,
Bill Buzbee89efc3d2009-07-28 11:22:22 -07001868 ArmLIR *labelList)
Ben Chengba4fc8b2009-06-01 13:00:29 -07001869{
1870 OpCode dalvikOpCode = mir->dalvikInsn.opCode;
Bill Buzbee89efc3d2009-07-28 11:22:22 -07001871 ArmConditionCode cond;
Bill Buzbeec6f10662010-02-09 11:16:15 -08001872 RegLocation rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07001873 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
1874 opRegImm(cUnit, kOpCmp, rlSrc.lowReg, 0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001875
Bill Buzbee270c1d62009-08-13 16:58:07 -07001876//TUNING: break this out to allow use of Thumb2 CB[N]Z
Ben Chengba4fc8b2009-06-01 13:00:29 -07001877 switch (dalvikOpCode) {
1878 case OP_IF_EQZ:
Bill Buzbee1465db52009-09-23 17:17:35 -07001879 cond = kArmCondEq;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001880 break;
1881 case OP_IF_NEZ:
Bill Buzbee1465db52009-09-23 17:17:35 -07001882 cond = kArmCondNe;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001883 break;
1884 case OP_IF_LTZ:
Bill Buzbee1465db52009-09-23 17:17:35 -07001885 cond = kArmCondLt;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001886 break;
1887 case OP_IF_GEZ:
Bill Buzbee1465db52009-09-23 17:17:35 -07001888 cond = kArmCondGe;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001889 break;
1890 case OP_IF_GTZ:
Bill Buzbee1465db52009-09-23 17:17:35 -07001891 cond = kArmCondGt;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001892 break;
1893 case OP_IF_LEZ:
Bill Buzbee1465db52009-09-23 17:17:35 -07001894 cond = kArmCondLe;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001895 break;
1896 default:
1897 cond = 0;
1898 LOGE("Unexpected opcode (%d) for Fmt21t\n", dalvikOpCode);
Bill Buzbeefc519dc2010-03-06 23:30:57 -08001899 dvmCompilerAbort(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001900 }
1901 genConditionalBranch(cUnit, cond, &labelList[bb->taken->id]);
1902 /* This mostly likely will be optimized away in a later phase */
1903 genUnconditionalBranch(cUnit, &labelList[bb->fallThrough->id]);
1904 return false;
1905}
1906
Elliott Hughesb4c05972010-02-24 16:36:18 -08001907static bool isPowerOfTwo(int x)
1908{
1909 return (x & (x - 1)) == 0;
1910}
1911
1912// Returns true if no more than two bits are set in 'x'.
1913static bool isPopCountLE2(unsigned int x)
1914{
1915 x &= x - 1;
1916 return (x & (x - 1)) == 0;
1917}
1918
1919// Returns the index of the lowest set bit in 'x'.
1920static int lowestSetBit(unsigned int x) {
1921 int bit_posn = 0;
1922 while ((x & 0xf) == 0) {
1923 bit_posn += 4;
1924 x >>= 4;
Bill Buzbee78cb0e22010-02-11 14:04:53 -08001925 }
Elliott Hughesb4c05972010-02-24 16:36:18 -08001926 while ((x & 1) == 0) {
1927 bit_posn++;
1928 x >>= 1;
1929 }
1930 return bit_posn;
1931}
1932
Elliott Hughes672511b2010-04-26 17:40:13 -07001933// Returns true if it added instructions to 'cUnit' to divide 'rlSrc' by 'lit'
1934// and store the result in 'rlDest'.
Elliott Hughesc7ad9b22010-04-28 13:52:02 -07001935static bool handleEasyDivide(CompilationUnit *cUnit, OpCode dalvikOpCode,
Elliott Hughes672511b2010-04-26 17:40:13 -07001936 RegLocation rlSrc, RegLocation rlDest, int lit)
1937{
1938 if (lit < 2 || !isPowerOfTwo(lit)) {
1939 return false;
1940 }
1941 int k = lowestSetBit(lit);
1942 if (k >= 30) {
1943 // Avoid special cases.
1944 return false;
1945 }
Elliott Hughes9c457022010-04-28 16:15:38 -07001946 bool div = (dalvikOpCode == OP_DIV_INT_LIT8 || dalvikOpCode == OP_DIV_INT_LIT16);
Elliott Hughes672511b2010-04-26 17:40:13 -07001947 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
1948 RegLocation rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Elliott Hughes9c457022010-04-28 16:15:38 -07001949 if (div) {
1950 int tReg = dvmCompilerAllocTemp(cUnit);
1951 if (lit == 2) {
1952 // Division by 2 is by far the most common division by constant.
1953 opRegRegImm(cUnit, kOpLsr, tReg, rlSrc.lowReg, 32 - k);
1954 opRegRegReg(cUnit, kOpAdd, tReg, tReg, rlSrc.lowReg);
1955 opRegRegImm(cUnit, kOpAsr, rlResult.lowReg, tReg, k);
1956 } else {
1957 opRegRegImm(cUnit, kOpAsr, tReg, rlSrc.lowReg, 31);
1958 opRegRegImm(cUnit, kOpLsr, tReg, tReg, 32 - k);
1959 opRegRegReg(cUnit, kOpAdd, tReg, tReg, rlSrc.lowReg);
1960 opRegRegImm(cUnit, kOpAsr, rlResult.lowReg, tReg, k);
1961 }
Elliott Hughes672511b2010-04-26 17:40:13 -07001962 } else {
Elliott Hughes9c457022010-04-28 16:15:38 -07001963 int cReg = dvmCompilerAllocTemp(cUnit);
1964 loadConstant(cUnit, cReg, lit - 1);
1965 int tReg1 = dvmCompilerAllocTemp(cUnit);
1966 int tReg2 = dvmCompilerAllocTemp(cUnit);
1967 if (lit == 2) {
1968 opRegRegImm(cUnit, kOpLsr, tReg1, rlSrc.lowReg, 32 - k);
1969 opRegRegReg(cUnit, kOpAdd, tReg2, tReg1, rlSrc.lowReg);
1970 opRegRegReg(cUnit, kOpAnd, tReg2, tReg2, cReg);
1971 opRegRegReg(cUnit, kOpSub, rlResult.lowReg, tReg2, tReg1);
1972 } else {
1973 opRegRegImm(cUnit, kOpAsr, tReg1, rlSrc.lowReg, 31);
1974 opRegRegImm(cUnit, kOpLsr, tReg1, tReg1, 32 - k);
1975 opRegRegReg(cUnit, kOpAdd, tReg2, tReg1, rlSrc.lowReg);
1976 opRegRegReg(cUnit, kOpAnd, tReg2, tReg2, cReg);
1977 opRegRegReg(cUnit, kOpSub, rlResult.lowReg, tReg2, tReg1);
1978 }
Elliott Hughes672511b2010-04-26 17:40:13 -07001979 }
1980 storeValue(cUnit, rlDest, rlResult);
1981 return true;
1982}
1983
Elliott Hughesb4c05972010-02-24 16:36:18 -08001984// Returns true if it added instructions to 'cUnit' to multiply 'rlSrc' by 'lit'
1985// and store the result in 'rlDest'.
1986static bool handleEasyMultiply(CompilationUnit *cUnit,
1987 RegLocation rlSrc, RegLocation rlDest, int lit)
1988{
1989 // Can we simplify this multiplication?
1990 bool powerOfTwo = false;
1991 bool popCountLE2 = false;
1992 bool powerOfTwoMinusOne = false;
1993 if (lit < 2) {
1994 // Avoid special cases.
1995 return false;
1996 } else if (isPowerOfTwo(lit)) {
1997 powerOfTwo = true;
1998 } else if (isPopCountLE2(lit)) {
1999 popCountLE2 = true;
2000 } else if (isPowerOfTwo(lit + 1)) {
2001 powerOfTwoMinusOne = true;
2002 } else {
2003 return false;
2004 }
2005 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
2006 RegLocation rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
2007 if (powerOfTwo) {
2008 // Shift.
2009 opRegRegImm(cUnit, kOpLsl, rlResult.lowReg, rlSrc.lowReg,
2010 lowestSetBit(lit));
2011 } else if (popCountLE2) {
2012 // Shift and add and shift.
2013 int firstBit = lowestSetBit(lit);
2014 int secondBit = lowestSetBit(lit ^ (1 << firstBit));
2015 genMultiplyByTwoBitMultiplier(cUnit, rlSrc, rlResult, lit,
2016 firstBit, secondBit);
2017 } else {
2018 // Reverse subtract: (src << (shift + 1)) - src.
2019 assert(powerOfTwoMinusOne);
2020 // TODO: rsb dst, src, src lsl#lowestSetBit(lit + 1)
2021 int tReg = dvmCompilerAllocTemp(cUnit);
2022 opRegRegImm(cUnit, kOpLsl, tReg, rlSrc.lowReg, lowestSetBit(lit + 1));
2023 opRegRegReg(cUnit, kOpSub, rlResult.lowReg, tReg, rlSrc.lowReg);
2024 }
2025 storeValue(cUnit, rlDest, rlResult);
2026 return true;
Bill Buzbee78cb0e22010-02-11 14:04:53 -08002027}
2028
Ben Chengba4fc8b2009-06-01 13:00:29 -07002029static bool handleFmt22b_Fmt22s(CompilationUnit *cUnit, MIR *mir)
2030{
2031 OpCode dalvikOpCode = mir->dalvikInsn.opCode;
Bill Buzbeec6f10662010-02-09 11:16:15 -08002032 RegLocation rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
2033 RegLocation rlDest = dvmCompilerGetDest(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07002034 RegLocation rlResult;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002035 int lit = mir->dalvikInsn.vC;
Ben Cheng4f489172009-09-27 17:08:35 -07002036 OpKind op = 0; /* Make gcc happy */
Bill Buzbee1465db52009-09-23 17:17:35 -07002037 int shiftOp = false;
2038 bool isDiv = false;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002039
Ben Chengba4fc8b2009-06-01 13:00:29 -07002040 switch (dalvikOpCode) {
Bill Buzbee1465db52009-09-23 17:17:35 -07002041 case OP_RSUB_INT_LIT8:
2042 case OP_RSUB_INT: {
2043 int tReg;
2044 //TUNING: add support for use of Arm rsub op
2045 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
Bill Buzbeec6f10662010-02-09 11:16:15 -08002046 tReg = dvmCompilerAllocTemp(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07002047 loadConstant(cUnit, tReg, lit);
Bill Buzbeec6f10662010-02-09 11:16:15 -08002048 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Bill Buzbee1465db52009-09-23 17:17:35 -07002049 opRegRegReg(cUnit, kOpSub, rlResult.lowReg,
2050 tReg, rlSrc.lowReg);
2051 storeValue(cUnit, rlDest, rlResult);
2052 return false;
2053 break;
2054 }
2055
Ben Chengba4fc8b2009-06-01 13:00:29 -07002056 case OP_ADD_INT_LIT8:
2057 case OP_ADD_INT_LIT16:
Bill Buzbee1465db52009-09-23 17:17:35 -07002058 op = kOpAdd;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002059 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002060 case OP_MUL_INT_LIT8:
Bill Buzbee78cb0e22010-02-11 14:04:53 -08002061 case OP_MUL_INT_LIT16: {
Elliott Hughesb4c05972010-02-24 16:36:18 -08002062 if (handleEasyMultiply(cUnit, rlSrc, rlDest, lit)) {
2063 return false;
Bill Buzbee78cb0e22010-02-11 14:04:53 -08002064 }
Elliott Hughesb4c05972010-02-24 16:36:18 -08002065 op = kOpMul;
Bill Buzbee1465db52009-09-23 17:17:35 -07002066 break;
Bill Buzbee78cb0e22010-02-11 14:04:53 -08002067 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07002068 case OP_AND_INT_LIT8:
2069 case OP_AND_INT_LIT16:
Bill Buzbee1465db52009-09-23 17:17:35 -07002070 op = kOpAnd;
2071 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002072 case OP_OR_INT_LIT8:
2073 case OP_OR_INT_LIT16:
Bill Buzbee1465db52009-09-23 17:17:35 -07002074 op = kOpOr;
2075 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002076 case OP_XOR_INT_LIT8:
2077 case OP_XOR_INT_LIT16:
Bill Buzbee1465db52009-09-23 17:17:35 -07002078 op = kOpXor;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002079 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002080 case OP_SHL_INT_LIT8:
Bill Buzbee0e605272009-12-01 14:28:05 -08002081 lit &= 31;
Bill Buzbee1465db52009-09-23 17:17:35 -07002082 shiftOp = true;
2083 op = kOpLsl;
2084 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002085 case OP_SHR_INT_LIT8:
Bill Buzbee0e605272009-12-01 14:28:05 -08002086 lit &= 31;
Bill Buzbee1465db52009-09-23 17:17:35 -07002087 shiftOp = true;
2088 op = kOpAsr;
2089 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002090 case OP_USHR_INT_LIT8:
Bill Buzbee0e605272009-12-01 14:28:05 -08002091 lit &= 31;
Bill Buzbee1465db52009-09-23 17:17:35 -07002092 shiftOp = true;
2093 op = kOpLsr;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002094 break;
2095
2096 case OP_DIV_INT_LIT8:
2097 case OP_DIV_INT_LIT16:
Ben Chengba4fc8b2009-06-01 13:00:29 -07002098 case OP_REM_INT_LIT8:
2099 case OP_REM_INT_LIT16:
2100 if (lit == 0) {
2101 /* Let the interpreter deal with div by 0 */
2102 genInterpSingleStep(cUnit, mir);
2103 return false;
2104 }
Elliott Hughesc7ad9b22010-04-28 13:52:02 -07002105 if (handleEasyDivide(cUnit, dalvikOpCode, rlSrc, rlDest, lit)) {
Elliott Hughes672511b2010-04-26 17:40:13 -07002106 return false;
2107 }
Bill Buzbeec6f10662010-02-09 11:16:15 -08002108 dvmCompilerFlushAllRegs(cUnit); /* Everything to home location */
Bill Buzbee1465db52009-09-23 17:17:35 -07002109 loadValueDirectFixed(cUnit, rlSrc, r0);
Bill Buzbeec6f10662010-02-09 11:16:15 -08002110 dvmCompilerClobber(cUnit, r0);
Bill Buzbee1465db52009-09-23 17:17:35 -07002111 if ((dalvikOpCode == OP_DIV_INT_LIT8) ||
2112 (dalvikOpCode == OP_DIV_INT_LIT16)) {
Ben Chengbd1326d2010-04-02 15:04:53 -07002113 LOAD_FUNC_ADDR(cUnit, r2, (int)__aeabi_idiv);
Bill Buzbee1465db52009-09-23 17:17:35 -07002114 isDiv = true;
2115 } else {
Ben Chengbd1326d2010-04-02 15:04:53 -07002116 LOAD_FUNC_ADDR(cUnit, r2, (int)__aeabi_idivmod);
Bill Buzbee1465db52009-09-23 17:17:35 -07002117 isDiv = false;
2118 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07002119 loadConstant(cUnit, r1, lit);
Bill Buzbee1465db52009-09-23 17:17:35 -07002120 opReg(cUnit, kOpBlx, r2);
Elliott Hughes6a555132010-02-25 15:41:42 -08002121 dvmCompilerClobberCallRegs(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07002122 if (isDiv)
Bill Buzbeec6f10662010-02-09 11:16:15 -08002123 rlResult = dvmCompilerGetReturn(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07002124 else
Bill Buzbeec6f10662010-02-09 11:16:15 -08002125 rlResult = dvmCompilerGetReturnAlt(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07002126 storeValue(cUnit, rlDest, rlResult);
2127 return false;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002128 break;
2129 default:
2130 return true;
2131 }
Bill Buzbee1465db52009-09-23 17:17:35 -07002132 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
Bill Buzbeec6f10662010-02-09 11:16:15 -08002133 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Bill Buzbee1465db52009-09-23 17:17:35 -07002134 // Avoid shifts by literal 0 - no support in Thumb. Change to copy
2135 if (shiftOp && (lit == 0)) {
2136 genRegCopy(cUnit, rlResult.lowReg, rlSrc.lowReg);
2137 } else {
2138 opRegRegImm(cUnit, op, rlResult.lowReg, rlSrc.lowReg, lit);
2139 }
2140 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002141 return false;
2142}
2143
2144static bool handleFmt22c(CompilationUnit *cUnit, MIR *mir)
2145{
2146 OpCode dalvikOpCode = mir->dalvikInsn.opCode;
2147 int fieldOffset;
2148
2149 if (dalvikOpCode >= OP_IGET && dalvikOpCode <= OP_IPUT_SHORT) {
2150 InstField *pInstField = (InstField *)
2151 cUnit->method->clazz->pDvmDex->pResFields[mir->dalvikInsn.vC];
Ben Chengba4fc8b2009-06-01 13:00:29 -07002152
Ben Chengdd6e8702010-05-07 13:05:47 -07002153 if (pInstField == NULL) {
2154 LOGE("Unexpected null instance field");
2155 dvmAbort();
2156 }
2157
Ben Chengba4fc8b2009-06-01 13:00:29 -07002158 fieldOffset = pInstField->byteOffset;
2159 } else {
Ben Chenga0e7b602009-10-13 23:09:01 -07002160 /* Deliberately break the code while make the compiler happy */
2161 fieldOffset = -1;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002162 }
2163 switch (dalvikOpCode) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07002164 case OP_NEW_ARRAY: {
Bill Buzbee1465db52009-09-23 17:17:35 -07002165 // Generates a call - use explicit registers
Bill Buzbeec6f10662010-02-09 11:16:15 -08002166 RegLocation rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
2167 RegLocation rlDest = dvmCompilerGetDest(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07002168 RegLocation rlResult;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002169 void *classPtr = (void*)
2170 (cUnit->method->clazz->pDvmDex->pResClasses[mir->dalvikInsn.vC]);
Ben Chengdd6e8702010-05-07 13:05:47 -07002171
2172 if (classPtr == NULL) {
2173 LOGE("Unexpected null class");
2174 dvmAbort();
2175 }
2176
Bill Buzbeec6f10662010-02-09 11:16:15 -08002177 dvmCompilerFlushAllRegs(cUnit); /* Everything to home location */
Bill Buzbee1465db52009-09-23 17:17:35 -07002178 genExportPC(cUnit, mir);
2179 loadValueDirectFixed(cUnit, rlSrc, r1); /* Len */
Ben Chengba4fc8b2009-06-01 13:00:29 -07002180 loadConstant(cUnit, r0, (int) classPtr );
Ben Chengbd1326d2010-04-02 15:04:53 -07002181 LOAD_FUNC_ADDR(cUnit, r3, (int)dvmAllocArrayByClass);
Ben Cheng4f489172009-09-27 17:08:35 -07002182 /*
2183 * "len < 0": bail to the interpreter to re-execute the
2184 * instruction
2185 */
Bill Buzbee89efc3d2009-07-28 11:22:22 -07002186 ArmLIR *pcrLabel =
Bill Buzbee1465db52009-09-23 17:17:35 -07002187 genRegImmCheck(cUnit, kArmCondMi, r1, 0, mir->offset, NULL);
Bill Buzbee270c1d62009-08-13 16:58:07 -07002188 loadConstant(cUnit, r2, ALLOC_DONT_TRACK);
Bill Buzbee1465db52009-09-23 17:17:35 -07002189 opReg(cUnit, kOpBlx, r3);
Elliott Hughes6a555132010-02-25 15:41:42 -08002190 dvmCompilerClobberCallRegs(cUnit);
Ben Cheng4f489172009-09-27 17:08:35 -07002191 /* generate a branch over if allocation is successful */
Bill Buzbee1465db52009-09-23 17:17:35 -07002192 opRegImm(cUnit, kOpCmp, r0, 0); /* NULL? */
2193 ArmLIR *branchOver = opCondBranch(cUnit, kArmCondNe);
Ben Cheng4f489172009-09-27 17:08:35 -07002194 /*
2195 * OOM exception needs to be thrown here and cannot re-execute
2196 */
2197 loadConstant(cUnit, r0,
2198 (int) (cUnit->method->insns + mir->offset));
2199 genDispatchToHandler(cUnit, TEMPLATE_THROW_EXCEPTION_COMMON);
2200 /* noreturn */
2201
Bill Buzbee1465db52009-09-23 17:17:35 -07002202 ArmLIR *target = newLIR0(cUnit, kArmPseudoTargetLabel);
Ben Cheng4f489172009-09-27 17:08:35 -07002203 target->defMask = ENCODE_ALL;
2204 branchOver->generic.target = (LIR *) target;
Bill Buzbeec6f10662010-02-09 11:16:15 -08002205 rlResult = dvmCompilerGetReturn(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07002206 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002207 break;
2208 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07002209 case OP_INSTANCE_OF: {
Bill Buzbee1465db52009-09-23 17:17:35 -07002210 // May generate a call - use explicit registers
Bill Buzbeec6f10662010-02-09 11:16:15 -08002211 RegLocation rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
2212 RegLocation rlDest = dvmCompilerGetDest(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07002213 RegLocation rlResult;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002214 ClassObject *classPtr =
2215 (cUnit->method->clazz->pDvmDex->pResClasses[mir->dalvikInsn.vC]);
Bill Buzbee480e6782010-01-27 15:43:08 -08002216 /*
2217 * Note: It is possible that classPtr is NULL at this point,
2218 * even though this instruction has been successfully interpreted.
2219 * If the previous interpretation had a null source, the
2220 * interpreter would not have bothered to resolve the clazz.
2221 * Bail out to the interpreter in this case, and log it
2222 * so that we can tell if it happens frequently.
2223 */
2224 if (classPtr == NULL) {
2225 LOGD("null clazz in OP_INSTANCE_OF, single-stepping");
2226 genInterpSingleStep(cUnit, mir);
2227 break;
2228 }
Bill Buzbeec6f10662010-02-09 11:16:15 -08002229 dvmCompilerFlushAllRegs(cUnit); /* Everything to home location */
Bill Buzbee1465db52009-09-23 17:17:35 -07002230 loadValueDirectFixed(cUnit, rlSrc, r0); /* Ref */
Ben Chengba4fc8b2009-06-01 13:00:29 -07002231 loadConstant(cUnit, r2, (int) classPtr );
Bill Buzbee270c1d62009-08-13 16:58:07 -07002232//TUNING: compare to 0 primative to allow use of CB[N]Z
Bill Buzbee1465db52009-09-23 17:17:35 -07002233 opRegImm(cUnit, kOpCmp, r0, 0); /* NULL? */
Ben Cheng752c7942009-06-22 10:50:07 -07002234 /* When taken r0 has NULL which can be used for store directly */
Bill Buzbee1465db52009-09-23 17:17:35 -07002235 ArmLIR *branch1 = opCondBranch(cUnit, kArmCondEq);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002236 /* r1 now contains object->clazz */
Bill Buzbee270c1d62009-08-13 16:58:07 -07002237 loadWordDisp(cUnit, r0, offsetof(Object, clazz), r1);
Bill Buzbee1465db52009-09-23 17:17:35 -07002238 /* r1 now contains object->clazz */
Ben Chengbd1326d2010-04-02 15:04:53 -07002239 LOAD_FUNC_ADDR(cUnit, r3, (int)dvmInstanceofNonTrivial);
Ben Cheng752c7942009-06-22 10:50:07 -07002240 loadConstant(cUnit, r0, 1); /* Assume true */
Bill Buzbee1465db52009-09-23 17:17:35 -07002241 opRegReg(cUnit, kOpCmp, r1, r2);
2242 ArmLIR *branch2 = opCondBranch(cUnit, kArmCondEq);
2243 genRegCopy(cUnit, r0, r1);
2244 genRegCopy(cUnit, r1, r2);
2245 opReg(cUnit, kOpBlx, r3);
Elliott Hughes6a555132010-02-25 15:41:42 -08002246 dvmCompilerClobberCallRegs(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002247 /* branch target here */
Bill Buzbee1465db52009-09-23 17:17:35 -07002248 ArmLIR *target = newLIR0(cUnit, kArmPseudoTargetLabel);
Ben Chengd7d426a2009-09-22 11:23:36 -07002249 target->defMask = ENCODE_ALL;
Bill Buzbeec6f10662010-02-09 11:16:15 -08002250 rlResult = dvmCompilerGetReturn(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07002251 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002252 branch1->generic.target = (LIR *)target;
2253 branch2->generic.target = (LIR *)target;
2254 break;
2255 }
2256 case OP_IGET_WIDE:
2257 genIGetWide(cUnit, mir, fieldOffset);
2258 break;
2259 case OP_IGET:
2260 case OP_IGET_OBJECT:
Bill Buzbee1465db52009-09-23 17:17:35 -07002261 genIGet(cUnit, mir, kWord, fieldOffset);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002262 break;
2263 case OP_IGET_BOOLEAN:
Bill Buzbee1465db52009-09-23 17:17:35 -07002264 genIGet(cUnit, mir, kUnsignedByte, fieldOffset);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002265 break;
2266 case OP_IGET_BYTE:
Bill Buzbee1465db52009-09-23 17:17:35 -07002267 genIGet(cUnit, mir, kSignedByte, fieldOffset);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002268 break;
2269 case OP_IGET_CHAR:
Bill Buzbee1465db52009-09-23 17:17:35 -07002270 genIGet(cUnit, mir, kUnsignedHalf, fieldOffset);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002271 break;
2272 case OP_IGET_SHORT:
Bill Buzbee1465db52009-09-23 17:17:35 -07002273 genIGet(cUnit, mir, kSignedHalf, fieldOffset);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002274 break;
2275 case OP_IPUT_WIDE:
2276 genIPutWide(cUnit, mir, fieldOffset);
2277 break;
2278 case OP_IPUT:
2279 case OP_IPUT_OBJECT:
Bill Buzbee1465db52009-09-23 17:17:35 -07002280 genIPut(cUnit, mir, kWord, fieldOffset);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002281 break;
2282 case OP_IPUT_SHORT:
2283 case OP_IPUT_CHAR:
Bill Buzbee1465db52009-09-23 17:17:35 -07002284 genIPut(cUnit, mir, kUnsignedHalf, fieldOffset);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002285 break;
2286 case OP_IPUT_BYTE:
2287 case OP_IPUT_BOOLEAN:
Bill Buzbee1465db52009-09-23 17:17:35 -07002288 genIPut(cUnit, mir, kUnsignedByte, fieldOffset);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002289 break;
Bill Buzbeeb16344a2010-03-15 17:19:12 -07002290 case OP_IGET_WIDE_VOLATILE:
2291 case OP_IPUT_WIDE_VOLATILE:
2292 case OP_SGET_WIDE_VOLATILE:
2293 case OP_SPUT_WIDE_VOLATILE:
2294 genInterpSingleStep(cUnit, mir);
2295 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002296 default:
2297 return true;
2298 }
2299 return false;
2300}
2301
2302static bool handleFmt22cs(CompilationUnit *cUnit, MIR *mir)
2303{
2304 OpCode dalvikOpCode = mir->dalvikInsn.opCode;
2305 int fieldOffset = mir->dalvikInsn.vC;
2306 switch (dalvikOpCode) {
2307 case OP_IGET_QUICK:
2308 case OP_IGET_OBJECT_QUICK:
Bill Buzbee1465db52009-09-23 17:17:35 -07002309 genIGet(cUnit, mir, kWord, fieldOffset);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002310 break;
2311 case OP_IPUT_QUICK:
2312 case OP_IPUT_OBJECT_QUICK:
Bill Buzbee1465db52009-09-23 17:17:35 -07002313 genIPut(cUnit, mir, kWord, fieldOffset);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002314 break;
2315 case OP_IGET_WIDE_QUICK:
2316 genIGetWide(cUnit, mir, fieldOffset);
2317 break;
2318 case OP_IPUT_WIDE_QUICK:
2319 genIPutWide(cUnit, mir, fieldOffset);
2320 break;
2321 default:
2322 return true;
2323 }
2324 return false;
2325
2326}
2327
2328/* Compare agaist zero */
2329static bool handleFmt22t(CompilationUnit *cUnit, MIR *mir, BasicBlock *bb,
Bill Buzbee89efc3d2009-07-28 11:22:22 -07002330 ArmLIR *labelList)
Ben Chengba4fc8b2009-06-01 13:00:29 -07002331{
2332 OpCode dalvikOpCode = mir->dalvikInsn.opCode;
Bill Buzbee89efc3d2009-07-28 11:22:22 -07002333 ArmConditionCode cond;
Bill Buzbeec6f10662010-02-09 11:16:15 -08002334 RegLocation rlSrc1 = dvmCompilerGetSrc(cUnit, mir, 0);
2335 RegLocation rlSrc2 = dvmCompilerGetSrc(cUnit, mir, 1);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002336
Bill Buzbee1465db52009-09-23 17:17:35 -07002337 rlSrc1 = loadValue(cUnit, rlSrc1, kCoreReg);
2338 rlSrc2 = loadValue(cUnit, rlSrc2, kCoreReg);
2339 opRegReg(cUnit, kOpCmp, rlSrc1.lowReg, rlSrc2.lowReg);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002340
2341 switch (dalvikOpCode) {
2342 case OP_IF_EQ:
Bill Buzbee1465db52009-09-23 17:17:35 -07002343 cond = kArmCondEq;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002344 break;
2345 case OP_IF_NE:
Bill Buzbee1465db52009-09-23 17:17:35 -07002346 cond = kArmCondNe;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002347 break;
2348 case OP_IF_LT:
Bill Buzbee1465db52009-09-23 17:17:35 -07002349 cond = kArmCondLt;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002350 break;
2351 case OP_IF_GE:
Bill Buzbee1465db52009-09-23 17:17:35 -07002352 cond = kArmCondGe;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002353 break;
2354 case OP_IF_GT:
Bill Buzbee1465db52009-09-23 17:17:35 -07002355 cond = kArmCondGt;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002356 break;
2357 case OP_IF_LE:
Bill Buzbee1465db52009-09-23 17:17:35 -07002358 cond = kArmCondLe;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002359 break;
2360 default:
2361 cond = 0;
2362 LOGE("Unexpected opcode (%d) for Fmt22t\n", dalvikOpCode);
Bill Buzbeefc519dc2010-03-06 23:30:57 -08002363 dvmCompilerAbort(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002364 }
2365 genConditionalBranch(cUnit, cond, &labelList[bb->taken->id]);
2366 /* This mostly likely will be optimized away in a later phase */
2367 genUnconditionalBranch(cUnit, &labelList[bb->fallThrough->id]);
2368 return false;
2369}
2370
2371static bool handleFmt22x_Fmt32x(CompilationUnit *cUnit, MIR *mir)
2372{
2373 OpCode opCode = mir->dalvikInsn.opCode;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002374
2375 switch (opCode) {
2376 case OP_MOVE_16:
2377 case OP_MOVE_OBJECT_16:
2378 case OP_MOVE_FROM16:
Ben Chenge9695e52009-06-16 16:11:47 -07002379 case OP_MOVE_OBJECT_FROM16: {
Bill Buzbeec6f10662010-02-09 11:16:15 -08002380 storeValue(cUnit, dvmCompilerGetDest(cUnit, mir, 0),
2381 dvmCompilerGetSrc(cUnit, mir, 0));
Ben Chengba4fc8b2009-06-01 13:00:29 -07002382 break;
Ben Chenge9695e52009-06-16 16:11:47 -07002383 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07002384 case OP_MOVE_WIDE_16:
Ben Chenge9695e52009-06-16 16:11:47 -07002385 case OP_MOVE_WIDE_FROM16: {
Bill Buzbeec6f10662010-02-09 11:16:15 -08002386 storeValueWide(cUnit, dvmCompilerGetDestWide(cUnit, mir, 0, 1),
2387 dvmCompilerGetSrcWide(cUnit, mir, 0, 1));
Ben Chengba4fc8b2009-06-01 13:00:29 -07002388 break;
Ben Chenge9695e52009-06-16 16:11:47 -07002389 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07002390 default:
2391 return true;
2392 }
2393 return false;
2394}
2395
2396static bool handleFmt23x(CompilationUnit *cUnit, MIR *mir)
2397{
2398 OpCode opCode = mir->dalvikInsn.opCode;
Bill Buzbee1465db52009-09-23 17:17:35 -07002399 RegLocation rlSrc1;
2400 RegLocation rlSrc2;
2401 RegLocation rlDest;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002402
2403 if ( (opCode >= OP_ADD_INT) && (opCode <= OP_REM_DOUBLE)) {
Ben Cheng5d90c202009-11-22 23:31:11 -08002404 return genArithOp( cUnit, mir );
Ben Chengba4fc8b2009-06-01 13:00:29 -07002405 }
2406
Bill Buzbee1465db52009-09-23 17:17:35 -07002407 /* APUTs have 3 sources and no targets */
2408 if (mir->ssaRep->numDefs == 0) {
2409 if (mir->ssaRep->numUses == 3) {
Bill Buzbeec6f10662010-02-09 11:16:15 -08002410 rlDest = dvmCompilerGetSrc(cUnit, mir, 0);
2411 rlSrc1 = dvmCompilerGetSrc(cUnit, mir, 1);
2412 rlSrc2 = dvmCompilerGetSrc(cUnit, mir, 2);
Bill Buzbee1465db52009-09-23 17:17:35 -07002413 } else {
2414 assert(mir->ssaRep->numUses == 4);
Bill Buzbeec6f10662010-02-09 11:16:15 -08002415 rlDest = dvmCompilerGetSrcWide(cUnit, mir, 0, 1);
2416 rlSrc1 = dvmCompilerGetSrc(cUnit, mir, 2);
2417 rlSrc2 = dvmCompilerGetSrc(cUnit, mir, 3);
Bill Buzbee1465db52009-09-23 17:17:35 -07002418 }
2419 } else {
2420 /* Two sources and 1 dest. Deduce the operand sizes */
2421 if (mir->ssaRep->numUses == 4) {
Bill Buzbeec6f10662010-02-09 11:16:15 -08002422 rlSrc1 = dvmCompilerGetSrcWide(cUnit, mir, 0, 1);
2423 rlSrc2 = dvmCompilerGetSrcWide(cUnit, mir, 2, 3);
Bill Buzbee1465db52009-09-23 17:17:35 -07002424 } else {
2425 assert(mir->ssaRep->numUses == 2);
Bill Buzbeec6f10662010-02-09 11:16:15 -08002426 rlSrc1 = dvmCompilerGetSrc(cUnit, mir, 0);
2427 rlSrc2 = dvmCompilerGetSrc(cUnit, mir, 1);
Bill Buzbee1465db52009-09-23 17:17:35 -07002428 }
2429 if (mir->ssaRep->numDefs == 2) {
Bill Buzbeec6f10662010-02-09 11:16:15 -08002430 rlDest = dvmCompilerGetDestWide(cUnit, mir, 0, 1);
Bill Buzbee1465db52009-09-23 17:17:35 -07002431 } else {
2432 assert(mir->ssaRep->numDefs == 1);
Bill Buzbeec6f10662010-02-09 11:16:15 -08002433 rlDest = dvmCompilerGetDest(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07002434 }
2435 }
2436
2437
Ben Chengba4fc8b2009-06-01 13:00:29 -07002438 switch (opCode) {
Bill Buzbeed45ba372009-06-15 17:00:57 -07002439 case OP_CMPL_FLOAT:
2440 case OP_CMPG_FLOAT:
2441 case OP_CMPL_DOUBLE:
2442 case OP_CMPG_DOUBLE:
Ben Cheng5d90c202009-11-22 23:31:11 -08002443 return genCmpFP(cUnit, mir, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002444 case OP_CMP_LONG:
Bill Buzbee1465db52009-09-23 17:17:35 -07002445 genCmpLong(cUnit, mir, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002446 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002447 case OP_AGET_WIDE:
Bill Buzbee1465db52009-09-23 17:17:35 -07002448 genArrayGet(cUnit, mir, kLong, rlSrc1, rlSrc2, rlDest, 3);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002449 break;
2450 case OP_AGET:
2451 case OP_AGET_OBJECT:
Bill Buzbee1465db52009-09-23 17:17:35 -07002452 genArrayGet(cUnit, mir, kWord, rlSrc1, rlSrc2, rlDest, 2);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002453 break;
2454 case OP_AGET_BOOLEAN:
Bill Buzbee1465db52009-09-23 17:17:35 -07002455 genArrayGet(cUnit, mir, kUnsignedByte, rlSrc1, rlSrc2, rlDest, 0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002456 break;
2457 case OP_AGET_BYTE:
Bill Buzbee1465db52009-09-23 17:17:35 -07002458 genArrayGet(cUnit, mir, kSignedByte, rlSrc1, rlSrc2, rlDest, 0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002459 break;
2460 case OP_AGET_CHAR:
Bill Buzbee1465db52009-09-23 17:17:35 -07002461 genArrayGet(cUnit, mir, kUnsignedHalf, rlSrc1, rlSrc2, rlDest, 1);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002462 break;
2463 case OP_AGET_SHORT:
Bill Buzbee1465db52009-09-23 17:17:35 -07002464 genArrayGet(cUnit, mir, kSignedHalf, rlSrc1, rlSrc2, rlDest, 1);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002465 break;
2466 case OP_APUT_WIDE:
Bill Buzbee1465db52009-09-23 17:17:35 -07002467 genArrayPut(cUnit, mir, kLong, rlSrc1, rlSrc2, rlDest, 3);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002468 break;
2469 case OP_APUT:
Bill Buzbee1465db52009-09-23 17:17:35 -07002470 genArrayPut(cUnit, mir, kWord, rlSrc1, rlSrc2, rlDest, 2);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002471 break;
Bill Buzbeebe6534f2010-03-12 16:01:35 -08002472 case OP_APUT_OBJECT:
2473 genArrayObjectPut(cUnit, mir, rlSrc1, rlSrc2, rlDest, 2);
2474 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002475 case OP_APUT_SHORT:
2476 case OP_APUT_CHAR:
Bill Buzbee1465db52009-09-23 17:17:35 -07002477 genArrayPut(cUnit, mir, kUnsignedHalf, rlSrc1, rlSrc2, rlDest, 1);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002478 break;
2479 case OP_APUT_BYTE:
2480 case OP_APUT_BOOLEAN:
Bill Buzbee1465db52009-09-23 17:17:35 -07002481 genArrayPut(cUnit, mir, kUnsignedByte, rlSrc1, rlSrc2, rlDest, 0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002482 break;
2483 default:
2484 return true;
2485 }
2486 return false;
2487}
2488
Ben Cheng6c10a972009-10-29 14:39:18 -07002489/*
2490 * Find the matching case.
2491 *
2492 * return values:
2493 * r0 (low 32-bit): pc of the chaining cell corresponding to the resolved case,
2494 * including default which is placed at MIN(size, MAX_CHAINED_SWITCH_CASES).
2495 * r1 (high 32-bit): the branch offset of the matching case (only for indexes
2496 * above MAX_CHAINED_SWITCH_CASES).
2497 *
2498 * Instructions around the call are:
2499 *
2500 * mov r2, pc
2501 * blx &findPackedSwitchIndex
2502 * mov pc, r0
2503 * .align4
Bill Buzbeebd047242010-05-13 13:02:53 -07002504 * chaining cell for case 0 [12 bytes]
2505 * chaining cell for case 1 [12 bytes]
Ben Cheng6c10a972009-10-29 14:39:18 -07002506 * :
Bill Buzbeebd047242010-05-13 13:02:53 -07002507 * chaining cell for case MIN(size, MAX_CHAINED_SWITCH_CASES)-1 [12 bytes]
Ben Cheng6c10a972009-10-29 14:39:18 -07002508 * chaining cell for case default [8 bytes]
2509 * noChain exit
2510 */
Ben Chengbd1326d2010-04-02 15:04:53 -07002511static s8 findPackedSwitchIndex(const u2* switchData, int testVal, int pc)
Ben Cheng6c10a972009-10-29 14:39:18 -07002512{
2513 int size;
2514 int firstKey;
2515 const int *entries;
2516 int index;
2517 int jumpIndex;
2518 int caseDPCOffset = 0;
2519 /* In Thumb mode pc is 4 ahead of the "mov r2, pc" instruction */
2520 int chainingPC = (pc + 4) & ~3;
2521
2522 /*
2523 * Packed switch data format:
2524 * ushort ident = 0x0100 magic value
2525 * ushort size number of entries in the table
2526 * int first_key first (and lowest) switch case value
2527 * int targets[size] branch targets, relative to switch opcode
2528 *
2529 * Total size is (4+size*2) 16-bit code units.
2530 */
2531 size = switchData[1];
2532 assert(size > 0);
2533
2534 firstKey = switchData[2];
2535 firstKey |= switchData[3] << 16;
2536
2537
2538 /* The entries are guaranteed to be aligned on a 32-bit boundary;
2539 * we can treat them as a native int array.
2540 */
2541 entries = (const int*) &switchData[4];
2542 assert(((u4)entries & 0x3) == 0);
2543
2544 index = testVal - firstKey;
2545
2546 /* Jump to the default cell */
2547 if (index < 0 || index >= size) {
2548 jumpIndex = MIN(size, MAX_CHAINED_SWITCH_CASES);
2549 /* Jump to the non-chaining exit point */
2550 } else if (index >= MAX_CHAINED_SWITCH_CASES) {
2551 jumpIndex = MAX_CHAINED_SWITCH_CASES + 1;
2552 caseDPCOffset = entries[index];
2553 /* Jump to the inline chaining cell */
2554 } else {
2555 jumpIndex = index;
2556 }
2557
Bill Buzbeebd047242010-05-13 13:02:53 -07002558 chainingPC += jumpIndex * CHAIN_CELL_NORMAL_SIZE;
Ben Cheng6c10a972009-10-29 14:39:18 -07002559 return (((s8) caseDPCOffset) << 32) | (u8) chainingPC;
2560}
2561
2562/* See comments for findPackedSwitchIndex */
Ben Chengbd1326d2010-04-02 15:04:53 -07002563static s8 findSparseSwitchIndex(const u2* switchData, int testVal, int pc)
Ben Cheng6c10a972009-10-29 14:39:18 -07002564{
2565 int size;
2566 const int *keys;
2567 const int *entries;
2568 int chainingPC = (pc + 4) & ~3;
2569 int i;
2570
2571 /*
2572 * Sparse switch data format:
2573 * ushort ident = 0x0200 magic value
2574 * ushort size number of entries in the table; > 0
2575 * int keys[size] keys, sorted low-to-high; 32-bit aligned
2576 * int targets[size] branch targets, relative to switch opcode
2577 *
2578 * Total size is (2+size*4) 16-bit code units.
2579 */
2580
2581 size = switchData[1];
2582 assert(size > 0);
2583
2584 /* The keys are guaranteed to be aligned on a 32-bit boundary;
2585 * we can treat them as a native int array.
2586 */
2587 keys = (const int*) &switchData[2];
2588 assert(((u4)keys & 0x3) == 0);
2589
2590 /* The entries are guaranteed to be aligned on a 32-bit boundary;
2591 * we can treat them as a native int array.
2592 */
2593 entries = keys + size;
2594 assert(((u4)entries & 0x3) == 0);
2595
2596 /*
2597 * Run through the list of keys, which are guaranteed to
2598 * be sorted low-to-high.
2599 *
2600 * Most tables have 3-4 entries. Few have more than 10. A binary
2601 * search here is probably not useful.
2602 */
2603 for (i = 0; i < size; i++) {
2604 int k = keys[i];
2605 if (k == testVal) {
2606 /* MAX_CHAINED_SWITCH_CASES + 1 is the start of the overflow case */
2607 int jumpIndex = (i < MAX_CHAINED_SWITCH_CASES) ?
2608 i : MAX_CHAINED_SWITCH_CASES + 1;
Bill Buzbeebd047242010-05-13 13:02:53 -07002609 chainingPC += jumpIndex * CHAIN_CELL_NORMAL_SIZE;
Ben Cheng6c10a972009-10-29 14:39:18 -07002610 return (((s8) entries[i]) << 32) | (u8) chainingPC;
2611 } else if (k > testVal) {
2612 break;
2613 }
2614 }
Bill Buzbeebd047242010-05-13 13:02:53 -07002615 return chainingPC + MIN(size, MAX_CHAINED_SWITCH_CASES) *
2616 CHAIN_CELL_NORMAL_SIZE;
Ben Cheng6c10a972009-10-29 14:39:18 -07002617}
2618
Ben Chengba4fc8b2009-06-01 13:00:29 -07002619static bool handleFmt31t(CompilationUnit *cUnit, MIR *mir)
2620{
2621 OpCode dalvikOpCode = mir->dalvikInsn.opCode;
2622 switch (dalvikOpCode) {
2623 case OP_FILL_ARRAY_DATA: {
Bill Buzbeec6f10662010-02-09 11:16:15 -08002624 RegLocation rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07002625 // Making a call - use explicit registers
Bill Buzbeec6f10662010-02-09 11:16:15 -08002626 dvmCompilerFlushAllRegs(cUnit); /* Everything to home location */
Bill Buzbee1465db52009-09-23 17:17:35 -07002627 genExportPC(cUnit, mir);
2628 loadValueDirectFixed(cUnit, rlSrc, r0);
Ben Chengbd1326d2010-04-02 15:04:53 -07002629 LOAD_FUNC_ADDR(cUnit, r2, (int)dvmInterpHandleFillArrayData);
Ben Cheng6c10a972009-10-29 14:39:18 -07002630 loadConstant(cUnit, r1,
2631 (int) (cUnit->method->insns + mir->offset + mir->dalvikInsn.vB));
Bill Buzbee1465db52009-09-23 17:17:35 -07002632 opReg(cUnit, kOpBlx, r2);
Elliott Hughes6a555132010-02-25 15:41:42 -08002633 dvmCompilerClobberCallRegs(cUnit);
Bill Buzbeefd023aa2009-11-02 09:23:49 -08002634 /* generate a branch over if successful */
2635 opRegImm(cUnit, kOpCmp, r0, 0); /* NULL? */
2636 ArmLIR *branchOver = opCondBranch(cUnit, kArmCondNe);
2637 loadConstant(cUnit, r0,
2638 (int) (cUnit->method->insns + mir->offset));
2639 genDispatchToHandler(cUnit, TEMPLATE_THROW_EXCEPTION_COMMON);
2640 ArmLIR *target = newLIR0(cUnit, kArmPseudoTargetLabel);
2641 target->defMask = ENCODE_ALL;
2642 branchOver->generic.target = (LIR *) target;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002643 break;
2644 }
2645 /*
Ben Cheng6c10a972009-10-29 14:39:18 -07002646 * Compute the goto target of up to
2647 * MIN(switchSize, MAX_CHAINED_SWITCH_CASES) + 1 chaining cells.
2648 * See the comment before findPackedSwitchIndex for the code layout.
Ben Chengba4fc8b2009-06-01 13:00:29 -07002649 */
2650 case OP_PACKED_SWITCH:
2651 case OP_SPARSE_SWITCH: {
Bill Buzbeec6f10662010-02-09 11:16:15 -08002652 RegLocation rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
2653 dvmCompilerFlushAllRegs(cUnit); /* Everything to home location */
Bill Buzbee1465db52009-09-23 17:17:35 -07002654 loadValueDirectFixed(cUnit, rlSrc, r1);
Bill Buzbeec6f10662010-02-09 11:16:15 -08002655 dvmCompilerLockAllTemps(cUnit);
Ben Cheng6c10a972009-10-29 14:39:18 -07002656 const u2 *switchData =
2657 cUnit->method->insns + mir->offset + mir->dalvikInsn.vB;
2658 u2 size = switchData[1];
2659
Ben Chengba4fc8b2009-06-01 13:00:29 -07002660 if (dalvikOpCode == OP_PACKED_SWITCH) {
Ben Chengbd1326d2010-04-02 15:04:53 -07002661 LOAD_FUNC_ADDR(cUnit, r4PC, (int)findPackedSwitchIndex);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002662 } else {
Ben Chengbd1326d2010-04-02 15:04:53 -07002663 LOAD_FUNC_ADDR(cUnit, r4PC, (int)findSparseSwitchIndex);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002664 }
Ben Cheng6c10a972009-10-29 14:39:18 -07002665 /* r0 <- Addr of the switch data */
2666 loadConstant(cUnit, r0,
2667 (int) (cUnit->method->insns + mir->offset + mir->dalvikInsn.vB));
2668 /* r2 <- pc of the instruction following the blx */
2669 opRegReg(cUnit, kOpMov, r2, rpc);
Bill Buzbee1465db52009-09-23 17:17:35 -07002670 opReg(cUnit, kOpBlx, r4PC);
Elliott Hughes6a555132010-02-25 15:41:42 -08002671 dvmCompilerClobberCallRegs(cUnit);
Ben Cheng6c10a972009-10-29 14:39:18 -07002672 /* pc <- computed goto target */
2673 opRegReg(cUnit, kOpMov, rpc, r0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002674 break;
2675 }
2676 default:
2677 return true;
2678 }
2679 return false;
2680}
2681
2682static bool handleFmt35c_3rc(CompilationUnit *cUnit, MIR *mir, BasicBlock *bb,
Bill Buzbee89efc3d2009-07-28 11:22:22 -07002683 ArmLIR *labelList)
Ben Chengba4fc8b2009-06-01 13:00:29 -07002684{
Bill Buzbee9bc3df32009-07-30 10:52:29 -07002685 ArmLIR *retChainingCell = NULL;
Bill Buzbee89efc3d2009-07-28 11:22:22 -07002686 ArmLIR *pcrLabel = NULL;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002687
Bill Buzbeef4ce16f2009-07-28 13:28:25 -07002688 if (bb->fallThrough != NULL)
2689 retChainingCell = &labelList[bb->fallThrough->id];
2690
Ben Chengba4fc8b2009-06-01 13:00:29 -07002691 DecodedInstruction *dInsn = &mir->dalvikInsn;
2692 switch (mir->dalvikInsn.opCode) {
2693 /*
2694 * calleeMethod = this->clazz->vtable[
2695 * method->clazz->pDvmDex->pResMethods[BBBB]->methodIndex
2696 * ]
2697 */
2698 case OP_INVOKE_VIRTUAL:
2699 case OP_INVOKE_VIRTUAL_RANGE: {
Bill Buzbee89efc3d2009-07-28 11:22:22 -07002700 ArmLIR *predChainingCell = &labelList[bb->taken->id];
Ben Chengba4fc8b2009-06-01 13:00:29 -07002701 int methodIndex =
2702 cUnit->method->clazz->pDvmDex->pResMethods[dInsn->vB]->
2703 methodIndex;
2704
2705 if (mir->dalvikInsn.opCode == OP_INVOKE_VIRTUAL)
2706 genProcessArgsNoRange(cUnit, mir, dInsn, &pcrLabel);
2707 else
2708 genProcessArgsRange(cUnit, mir, dInsn, &pcrLabel);
2709
Ben Cheng38329f52009-07-07 14:19:20 -07002710 genInvokeVirtualCommon(cUnit, mir, methodIndex,
2711 retChainingCell,
2712 predChainingCell,
2713 pcrLabel);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002714 break;
2715 }
2716 /*
2717 * calleeMethod = method->clazz->super->vtable[method->clazz->pDvmDex
2718 * ->pResMethods[BBBB]->methodIndex]
2719 */
Ben Chengba4fc8b2009-06-01 13:00:29 -07002720 case OP_INVOKE_SUPER:
2721 case OP_INVOKE_SUPER_RANGE: {
2722 int mIndex = cUnit->method->clazz->pDvmDex->
2723 pResMethods[dInsn->vB]->methodIndex;
2724 const Method *calleeMethod =
2725 cUnit->method->clazz->super->vtable[mIndex];
2726
2727 if (mir->dalvikInsn.opCode == OP_INVOKE_SUPER)
2728 genProcessArgsNoRange(cUnit, mir, dInsn, &pcrLabel);
2729 else
2730 genProcessArgsRange(cUnit, mir, dInsn, &pcrLabel);
2731
2732 /* r0 = calleeMethod */
2733 loadConstant(cUnit, r0, (int) calleeMethod);
2734
Ben Cheng38329f52009-07-07 14:19:20 -07002735 genInvokeSingletonCommon(cUnit, mir, bb, labelList, pcrLabel,
2736 calleeMethod);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002737 break;
2738 }
2739 /* calleeMethod = method->clazz->pDvmDex->pResMethods[BBBB] */
2740 case OP_INVOKE_DIRECT:
2741 case OP_INVOKE_DIRECT_RANGE: {
2742 const Method *calleeMethod =
2743 cUnit->method->clazz->pDvmDex->pResMethods[dInsn->vB];
2744
2745 if (mir->dalvikInsn.opCode == OP_INVOKE_DIRECT)
2746 genProcessArgsNoRange(cUnit, mir, dInsn, &pcrLabel);
2747 else
2748 genProcessArgsRange(cUnit, mir, dInsn, &pcrLabel);
2749
2750 /* r0 = calleeMethod */
2751 loadConstant(cUnit, r0, (int) calleeMethod);
2752
Ben Cheng38329f52009-07-07 14:19:20 -07002753 genInvokeSingletonCommon(cUnit, mir, bb, labelList, pcrLabel,
2754 calleeMethod);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002755 break;
2756 }
2757 /* calleeMethod = method->clazz->pDvmDex->pResMethods[BBBB] */
2758 case OP_INVOKE_STATIC:
2759 case OP_INVOKE_STATIC_RANGE: {
2760 const Method *calleeMethod =
2761 cUnit->method->clazz->pDvmDex->pResMethods[dInsn->vB];
2762
2763 if (mir->dalvikInsn.opCode == OP_INVOKE_STATIC)
2764 genProcessArgsNoRange(cUnit, mir, dInsn,
2765 NULL /* no null check */);
2766 else
2767 genProcessArgsRange(cUnit, mir, dInsn,
2768 NULL /* no null check */);
2769
2770 /* r0 = calleeMethod */
2771 loadConstant(cUnit, r0, (int) calleeMethod);
2772
Ben Cheng38329f52009-07-07 14:19:20 -07002773 genInvokeSingletonCommon(cUnit, mir, bb, labelList, pcrLabel,
2774 calleeMethod);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002775 break;
2776 }
Ben Cheng09e50c92010-05-02 10:45:32 -07002777 /*
Ben Chengba4fc8b2009-06-01 13:00:29 -07002778 * calleeMethod = dvmFindInterfaceMethodInCache(this->clazz,
2779 * BBBB, method, method->clazz->pDvmDex)
Ben Cheng38329f52009-07-07 14:19:20 -07002780 *
Ben Cheng09e50c92010-05-02 10:45:32 -07002781 * The following is an example of generated code for
2782 * "invoke-interface v0"
Ben Cheng38329f52009-07-07 14:19:20 -07002783 *
Ben Cheng09e50c92010-05-02 10:45:32 -07002784 * -------- dalvik offset: 0x0008 @ invoke-interface v0
2785 * 0x47357e36 : ldr r0, [r5, #0] --+
2786 * 0x47357e38 : sub r7,r5,#24 |
2787 * 0x47357e3c : cmp r0, #0 | genProcessArgsNoRange
2788 * 0x47357e3e : beq 0x47357e82 |
2789 * 0x47357e40 : stmia r7, <r0> --+
2790 * 0x47357e42 : ldr r4, [pc, #120] --> r4 <- dalvikPC of this invoke
2791 * 0x47357e44 : add r1, pc, #64 --> r1 <- &retChainingCell
2792 * 0x47357e46 : add r2, pc, #72 --> r2 <- &predictedChainingCell
2793 * 0x47357e48 : blx_1 0x47348190 --+ TEMPLATE_INVOKE_METHOD_
2794 * 0x47357e4a : blx_2 see above --+ PREDICTED_CHAIN
2795 * 0x47357e4c : b 0x47357e90 --> off to the predicted chain
2796 * 0x47357e4e : b 0x47357e82 --> punt to the interpreter
2797 * 0x47357e50 : mov r8, r1 --+
2798 * 0x47357e52 : mov r9, r2 |
2799 * 0x47357e54 : ldr r2, [pc, #96] |
2800 * 0x47357e56 : mov r10, r3 |
2801 * 0x47357e58 : movs r0, r3 | dvmFindInterfaceMethodInCache
2802 * 0x47357e5a : ldr r3, [pc, #88] |
2803 * 0x47357e5c : ldr r7, [pc, #80] |
2804 * 0x47357e5e : mov r1, #1452 |
2805 * 0x47357e62 : blx r7 --+
2806 * 0x47357e64 : cmp r0, #0 --> calleeMethod == NULL?
2807 * 0x47357e66 : bne 0x47357e6e --> branch over the throw if !r0
2808 * 0x47357e68 : ldr r0, [pc, #80] --> load Dalvik PC of the invoke
2809 * 0x47357e6a : blx_1 0x47348494 --+ TEMPLATE_THROW_EXCEPTION_
2810 * 0x47357e6c : blx_2 see above --+ COMMON
2811 * 0x47357e6e : mov r1, r8 --> r1 <- &retChainingCell
2812 * 0x47357e70 : cmp r1, #0 --> compare against 0
2813 * 0x47357e72 : bgt 0x47357e7c --> >=0? don't rechain
2814 * 0x47357e74 : ldr r7, [r6, #108] --+
2815 * 0x47357e76 : mov r2, r9 | dvmJitToPatchPredictedChain
2816 * 0x47357e78 : mov r3, r10 |
2817 * 0x47357e7a : blx r7 --+
2818 * 0x47357e7c : add r1, pc, #8 --> r1 <- &retChainingCell
2819 * 0x47357e7e : blx_1 0x4734809c --+ TEMPLATE_INVOKE_METHOD_NO_OPT
2820 * 0x47357e80 : blx_2 see above --+
2821 * -------- reconstruct dalvik PC : 0x425719dc @ +0x0008
2822 * 0x47357e82 : ldr r0, [pc, #56]
Ben Cheng38329f52009-07-07 14:19:20 -07002823 * Exception_Handling:
Ben Cheng09e50c92010-05-02 10:45:32 -07002824 * 0x47357e84 : ldr r1, [r6, #92]
2825 * 0x47357e86 : blx r1
2826 * 0x47357e88 : .align4
2827 * -------- chaining cell (hot): 0x000b
2828 * 0x47357e88 : ldr r0, [r6, #104]
2829 * 0x47357e8a : blx r0
2830 * 0x47357e8c : data 0x19e2(6626)
2831 * 0x47357e8e : data 0x4257(16983)
2832 * 0x47357e90 : .align4
Ben Cheng38329f52009-07-07 14:19:20 -07002833 * -------- chaining cell (predicted)
Ben Cheng09e50c92010-05-02 10:45:32 -07002834 * 0x47357e90 : data 0xe7fe(59390) --> will be patched into bx
2835 * 0x47357e92 : data 0x0000(0)
2836 * 0x47357e94 : data 0x0000(0) --> class
2837 * 0x47357e96 : data 0x0000(0)
2838 * 0x47357e98 : data 0x0000(0) --> method
2839 * 0x47357e9a : data 0x0000(0)
2840 * 0x47357e9c : data 0x0000(0) --> rechain count
2841 * 0x47357e9e : data 0x0000(0)
2842 * -------- end of chaining cells (0x006c)
2843 * 0x47357eb0 : .word (0xad03e369)
2844 * 0x47357eb4 : .word (0x28a90)
2845 * 0x47357eb8 : .word (0x41a63394)
2846 * 0x47357ebc : .word (0x425719dc)
Ben Chengba4fc8b2009-06-01 13:00:29 -07002847 */
2848 case OP_INVOKE_INTERFACE:
2849 case OP_INVOKE_INTERFACE_RANGE: {
Bill Buzbee89efc3d2009-07-28 11:22:22 -07002850 ArmLIR *predChainingCell = &labelList[bb->taken->id];
Ben Chengba4fc8b2009-06-01 13:00:29 -07002851 int methodIndex = dInsn->vB;
2852
Bill Buzbee1465db52009-09-23 17:17:35 -07002853 /* Ensure that nothing is both live and dirty */
Bill Buzbeec6f10662010-02-09 11:16:15 -08002854 dvmCompilerFlushAllRegs(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07002855
Ben Chengba4fc8b2009-06-01 13:00:29 -07002856 if (mir->dalvikInsn.opCode == OP_INVOKE_INTERFACE)
2857 genProcessArgsNoRange(cUnit, mir, dInsn, &pcrLabel);
2858 else
2859 genProcessArgsRange(cUnit, mir, dInsn, &pcrLabel);
2860
Ben Cheng38329f52009-07-07 14:19:20 -07002861 /* "this" is already left in r0 by genProcessArgs* */
2862
2863 /* r4PC = dalvikCallsite */
2864 loadConstant(cUnit, r4PC,
2865 (int) (cUnit->method->insns + mir->offset));
2866
2867 /* r1 = &retChainingCell */
Bill Buzbee270c1d62009-08-13 16:58:07 -07002868 ArmLIR *addrRetChain =
Bill Buzbee1465db52009-09-23 17:17:35 -07002869 opRegRegImm(cUnit, kOpAdd, r1, rpc, 0);
Ben Cheng38329f52009-07-07 14:19:20 -07002870 addrRetChain->generic.target = (LIR *) retChainingCell;
2871
2872 /* r2 = &predictedChainingCell */
Bill Buzbee89efc3d2009-07-28 11:22:22 -07002873 ArmLIR *predictedChainingCell =
Bill Buzbee1465db52009-09-23 17:17:35 -07002874 opRegRegImm(cUnit, kOpAdd, r2, rpc, 0);
Ben Cheng38329f52009-07-07 14:19:20 -07002875 predictedChainingCell->generic.target = (LIR *) predChainingCell;
2876
2877 genDispatchToHandler(cUnit, TEMPLATE_INVOKE_METHOD_PREDICTED_CHAIN);
2878
2879 /* return through lr - jump to the chaining cell */
2880 genUnconditionalBranch(cUnit, predChainingCell);
2881
2882 /*
2883 * null-check on "this" may have been eliminated, but we still need
2884 * a PC-reconstruction label for stack overflow bailout.
2885 */
2886 if (pcrLabel == NULL) {
2887 int dPC = (int) (cUnit->method->insns + mir->offset);
Bill Buzbee89efc3d2009-07-28 11:22:22 -07002888 pcrLabel = dvmCompilerNew(sizeof(ArmLIR), true);
Ben Chenga4973592010-03-31 11:59:18 -07002889 pcrLabel->opCode = kArmPseudoPCReconstructionCell;
Ben Cheng38329f52009-07-07 14:19:20 -07002890 pcrLabel->operands[0] = dPC;
2891 pcrLabel->operands[1] = mir->offset;
2892 /* Insert the place holder to the growable list */
2893 dvmInsertGrowableList(&cUnit->pcReconstructionList, pcrLabel);
2894 }
2895
2896 /* return through lr+2 - punt to the interpreter */
2897 genUnconditionalBranch(cUnit, pcrLabel);
2898
2899 /*
2900 * return through lr+4 - fully resolve the callee method.
2901 * r1 <- count
2902 * r2 <- &predictedChainCell
2903 * r3 <- this->class
2904 * r4 <- dPC
2905 * r7 <- this->class->vtable
2906 */
2907
2908 /* Save count, &predictedChainCell, and class to high regs first */
Bill Buzbee1465db52009-09-23 17:17:35 -07002909 genRegCopy(cUnit, r8, r1);
2910 genRegCopy(cUnit, r9, r2);
2911 genRegCopy(cUnit, r10, r3);
Ben Cheng38329f52009-07-07 14:19:20 -07002912
Ben Chengba4fc8b2009-06-01 13:00:29 -07002913 /* r0 now contains this->clazz */
Bill Buzbee1465db52009-09-23 17:17:35 -07002914 genRegCopy(cUnit, r0, r3);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002915
2916 /* r1 = BBBB */
2917 loadConstant(cUnit, r1, dInsn->vB);
2918
2919 /* r2 = method (caller) */
2920 loadConstant(cUnit, r2, (int) cUnit->method);
2921
2922 /* r3 = pDvmDex */
2923 loadConstant(cUnit, r3, (int) cUnit->method->clazz->pDvmDex);
2924
Ben Chengbd1326d2010-04-02 15:04:53 -07002925 LOAD_FUNC_ADDR(cUnit, r7,
2926 (intptr_t) dvmFindInterfaceMethodInCache);
Bill Buzbee1465db52009-09-23 17:17:35 -07002927 opReg(cUnit, kOpBlx, r7);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002928 /* r0 = calleeMethod (returned from dvmFindInterfaceMethodInCache */
2929
Ben Cheng09e50c92010-05-02 10:45:32 -07002930 dvmCompilerClobberCallRegs(cUnit);
2931 /* generate a branch over if the interface method is resolved */
2932 opRegImm(cUnit, kOpCmp, r0, 0); /* NULL? */
2933 ArmLIR *branchOver = opCondBranch(cUnit, kArmCondNe);
2934 /*
2935 * calleeMethod == NULL -> throw
2936 */
2937 loadConstant(cUnit, r0,
2938 (int) (cUnit->method->insns + mir->offset));
2939 genDispatchToHandler(cUnit, TEMPLATE_THROW_EXCEPTION_COMMON);
2940 /* noreturn */
2941
2942 ArmLIR *target = newLIR0(cUnit, kArmPseudoTargetLabel);
2943 target->defMask = ENCODE_ALL;
2944 branchOver->generic.target = (LIR *) target;
2945
Bill Buzbee1465db52009-09-23 17:17:35 -07002946 genRegCopy(cUnit, r1, r8);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002947
Ben Cheng38329f52009-07-07 14:19:20 -07002948 /* Check if rechain limit is reached */
Bill Buzbee1465db52009-09-23 17:17:35 -07002949 opRegImm(cUnit, kOpCmp, r1, 0);
Ben Cheng38329f52009-07-07 14:19:20 -07002950
Bill Buzbee1465db52009-09-23 17:17:35 -07002951 ArmLIR *bypassRechaining = opCondBranch(cUnit, kArmCondGt);
Ben Cheng38329f52009-07-07 14:19:20 -07002952
Bill Buzbee270c1d62009-08-13 16:58:07 -07002953 loadWordDisp(cUnit, rGLUE, offsetof(InterpState,
2954 jitToInterpEntries.dvmJitToPatchPredictedChain), r7);
Ben Cheng38329f52009-07-07 14:19:20 -07002955
Bill Buzbee1465db52009-09-23 17:17:35 -07002956 genRegCopy(cUnit, r2, r9);
2957 genRegCopy(cUnit, r3, r10);
Ben Cheng38329f52009-07-07 14:19:20 -07002958
2959 /*
2960 * r0 = calleeMethod
2961 * r2 = &predictedChainingCell
2962 * r3 = class
2963 *
2964 * &returnChainingCell has been loaded into r1 but is not needed
2965 * when patching the chaining cell and will be clobbered upon
2966 * returning so it will be reconstructed again.
2967 */
Bill Buzbee1465db52009-09-23 17:17:35 -07002968 opReg(cUnit, kOpBlx, r7);
Ben Cheng38329f52009-07-07 14:19:20 -07002969
2970 /* r1 = &retChainingCell */
Bill Buzbee1465db52009-09-23 17:17:35 -07002971 addrRetChain = opRegRegImm(cUnit, kOpAdd, r1, rpc, 0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002972 addrRetChain->generic.target = (LIR *) retChainingCell;
Ben Cheng38329f52009-07-07 14:19:20 -07002973
2974 bypassRechaining->generic.target = (LIR *) addrRetChain;
2975
Ben Chengba4fc8b2009-06-01 13:00:29 -07002976 /*
2977 * r0 = this, r1 = calleeMethod,
2978 * r1 = &ChainingCell,
2979 * r4PC = callsiteDPC,
2980 */
2981 genDispatchToHandler(cUnit, TEMPLATE_INVOKE_METHOD_NO_OPT);
Ben Cheng978738d2010-05-13 13:45:57 -07002982#if defined(WITH_JIT_TUNING)
Ben Cheng86717f72010-03-05 15:27:21 -08002983 gDvmJit.invokePolymorphic++;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002984#endif
2985 /* Handle exceptions using the interpreter */
2986 genTrap(cUnit, mir->offset, pcrLabel);
2987 break;
2988 }
2989 /* NOP */
2990 case OP_INVOKE_DIRECT_EMPTY: {
2991 return false;
2992 }
2993 case OP_FILLED_NEW_ARRAY:
2994 case OP_FILLED_NEW_ARRAY_RANGE: {
2995 /* Just let the interpreter deal with these */
2996 genInterpSingleStep(cUnit, mir);
2997 break;
2998 }
2999 default:
3000 return true;
3001 }
3002 return false;
3003}
3004
3005static bool handleFmt35ms_3rms(CompilationUnit *cUnit, MIR *mir,
Bill Buzbee89efc3d2009-07-28 11:22:22 -07003006 BasicBlock *bb, ArmLIR *labelList)
Ben Chengba4fc8b2009-06-01 13:00:29 -07003007{
Bill Buzbee89efc3d2009-07-28 11:22:22 -07003008 ArmLIR *retChainingCell = &labelList[bb->fallThrough->id];
3009 ArmLIR *predChainingCell = &labelList[bb->taken->id];
3010 ArmLIR *pcrLabel = NULL;
Ben Chengba4fc8b2009-06-01 13:00:29 -07003011
3012 DecodedInstruction *dInsn = &mir->dalvikInsn;
3013 switch (mir->dalvikInsn.opCode) {
3014 /* calleeMethod = this->clazz->vtable[BBBB] */
3015 case OP_INVOKE_VIRTUAL_QUICK_RANGE:
3016 case OP_INVOKE_VIRTUAL_QUICK: {
3017 int methodIndex = dInsn->vB;
3018 if (mir->dalvikInsn.opCode == OP_INVOKE_VIRTUAL_QUICK)
3019 genProcessArgsNoRange(cUnit, mir, dInsn, &pcrLabel);
3020 else
3021 genProcessArgsRange(cUnit, mir, dInsn, &pcrLabel);
3022
Ben Cheng38329f52009-07-07 14:19:20 -07003023 genInvokeVirtualCommon(cUnit, mir, methodIndex,
3024 retChainingCell,
3025 predChainingCell,
3026 pcrLabel);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003027 break;
3028 }
3029 /* calleeMethod = method->clazz->super->vtable[BBBB] */
3030 case OP_INVOKE_SUPER_QUICK:
3031 case OP_INVOKE_SUPER_QUICK_RANGE: {
3032 const Method *calleeMethod =
3033 cUnit->method->clazz->super->vtable[dInsn->vB];
3034
3035 if (mir->dalvikInsn.opCode == OP_INVOKE_SUPER_QUICK)
3036 genProcessArgsNoRange(cUnit, mir, dInsn, &pcrLabel);
3037 else
3038 genProcessArgsRange(cUnit, mir, dInsn, &pcrLabel);
3039
3040 /* r0 = calleeMethod */
3041 loadConstant(cUnit, r0, (int) calleeMethod);
3042
Ben Cheng38329f52009-07-07 14:19:20 -07003043 genInvokeSingletonCommon(cUnit, mir, bb, labelList, pcrLabel,
3044 calleeMethod);
3045 /* Handle exceptions using the interpreter */
3046 genTrap(cUnit, mir->offset, pcrLabel);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003047 break;
3048 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07003049 default:
3050 return true;
3051 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07003052 return false;
3053}
3054
3055/*
Bill Buzbeefd023aa2009-11-02 09:23:49 -08003056 * This operation is complex enough that we'll do it partly inline
3057 * and partly with a handler. NOTE: the handler uses hardcoded
3058 * values for string object offsets and must be revisitied if the
3059 * layout changes.
3060 */
3061static bool genInlinedCompareTo(CompilationUnit *cUnit, MIR *mir)
3062{
3063#if defined(USE_GLOBAL_STRING_DEFS)
3064 return false;
3065#else
3066 ArmLIR *rollback;
Bill Buzbeec6f10662010-02-09 11:16:15 -08003067 RegLocation rlThis = dvmCompilerGetSrc(cUnit, mir, 0);
3068 RegLocation rlComp = dvmCompilerGetSrc(cUnit, mir, 1);
Bill Buzbeefd023aa2009-11-02 09:23:49 -08003069
3070 loadValueDirectFixed(cUnit, rlThis, r0);
3071 loadValueDirectFixed(cUnit, rlComp, r1);
3072 /* Test objects for NULL */
3073 rollback = genNullCheck(cUnit, rlThis.sRegLow, r0, mir->offset, NULL);
3074 genNullCheck(cUnit, rlComp.sRegLow, r1, mir->offset, rollback);
3075 /*
3076 * TUNING: we could check for object pointer equality before invoking
3077 * handler. Unclear whether the gain would be worth the added code size
3078 * expansion.
3079 */
3080 genDispatchToHandler(cUnit, TEMPLATE_STRING_COMPARETO);
Bill Buzbeec6f10662010-02-09 11:16:15 -08003081 storeValue(cUnit, inlinedTarget(cUnit, mir, false),
3082 dvmCompilerGetReturn(cUnit));
Bill Buzbeefd023aa2009-11-02 09:23:49 -08003083 return true;
3084#endif
3085}
3086
Elliott Hughes2bdbcb62010-04-12 14:29:37 -07003087static bool genInlinedFastIndexOf(CompilationUnit *cUnit, MIR *mir)
Bill Buzbeefd023aa2009-11-02 09:23:49 -08003088{
3089#if defined(USE_GLOBAL_STRING_DEFS)
3090 return false;
3091#else
Bill Buzbeec6f10662010-02-09 11:16:15 -08003092 RegLocation rlThis = dvmCompilerGetSrc(cUnit, mir, 0);
3093 RegLocation rlChar = dvmCompilerGetSrc(cUnit, mir, 1);
Bill Buzbeefd023aa2009-11-02 09:23:49 -08003094
3095 loadValueDirectFixed(cUnit, rlThis, r0);
3096 loadValueDirectFixed(cUnit, rlChar, r1);
Elliott Hughes2bdbcb62010-04-12 14:29:37 -07003097 RegLocation rlStart = dvmCompilerGetSrc(cUnit, mir, 2);
3098 loadValueDirectFixed(cUnit, rlStart, r2);
Bill Buzbeefd023aa2009-11-02 09:23:49 -08003099 /* Test objects for NULL */
3100 genNullCheck(cUnit, rlThis.sRegLow, r0, mir->offset, NULL);
3101 genDispatchToHandler(cUnit, TEMPLATE_STRING_INDEXOF);
Bill Buzbeec6f10662010-02-09 11:16:15 -08003102 storeValue(cUnit, inlinedTarget(cUnit, mir, false),
3103 dvmCompilerGetReturn(cUnit));
Bill Buzbeefd023aa2009-11-02 09:23:49 -08003104 return true;
3105#endif
3106}
3107
Elliott Hughesee34f592010-04-05 18:13:52 -07003108// Generates an inlined String.isEmpty or String.length.
3109static bool genInlinedStringIsEmptyOrLength(CompilationUnit *cUnit, MIR *mir,
3110 bool isEmpty)
Bill Buzbee1f748632010-03-02 16:14:41 -08003111{
Elliott Hughesee34f592010-04-05 18:13:52 -07003112 // dst = src.length();
Bill Buzbee1f748632010-03-02 16:14:41 -08003113 RegLocation rlObj = dvmCompilerGetSrc(cUnit, mir, 0);
3114 RegLocation rlDest = inlinedTarget(cUnit, mir, false);
3115 rlObj = loadValue(cUnit, rlObj, kCoreReg);
3116 RegLocation rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
3117 genNullCheck(cUnit, rlObj.sRegLow, rlObj.lowReg, mir->offset, NULL);
3118 loadWordDisp(cUnit, rlObj.lowReg, gDvm.offJavaLangString_count,
3119 rlResult.lowReg);
Elliott Hughesee34f592010-04-05 18:13:52 -07003120 if (isEmpty) {
3121 // dst = (dst == 0);
3122 int tReg = dvmCompilerAllocTemp(cUnit);
3123 opRegReg(cUnit, kOpNeg, tReg, rlResult.lowReg);
3124 opRegRegReg(cUnit, kOpAdc, rlResult.lowReg, rlResult.lowReg, tReg);
3125 }
Bill Buzbee1f748632010-03-02 16:14:41 -08003126 storeValue(cUnit, rlDest, rlResult);
3127 return false;
3128}
3129
Elliott Hughesee34f592010-04-05 18:13:52 -07003130static bool genInlinedStringLength(CompilationUnit *cUnit, MIR *mir)
3131{
3132 return genInlinedStringIsEmptyOrLength(cUnit, mir, false);
3133}
3134
3135static bool genInlinedStringIsEmpty(CompilationUnit *cUnit, MIR *mir)
3136{
3137 return genInlinedStringIsEmptyOrLength(cUnit, mir, true);
3138}
3139
Bill Buzbee1f748632010-03-02 16:14:41 -08003140static bool genInlinedStringCharAt(CompilationUnit *cUnit, MIR *mir)
3141{
3142 int contents = offsetof(ArrayObject, contents);
3143 RegLocation rlObj = dvmCompilerGetSrc(cUnit, mir, 0);
3144 RegLocation rlIdx = dvmCompilerGetSrc(cUnit, mir, 1);
3145 RegLocation rlDest = inlinedTarget(cUnit, mir, false);
3146 RegLocation rlResult;
3147 rlObj = loadValue(cUnit, rlObj, kCoreReg);
3148 rlIdx = loadValue(cUnit, rlIdx, kCoreReg);
3149 int regMax = dvmCompilerAllocTemp(cUnit);
3150 int regOff = dvmCompilerAllocTemp(cUnit);
3151 int regPtr = dvmCompilerAllocTemp(cUnit);
3152 ArmLIR *pcrLabel = genNullCheck(cUnit, rlObj.sRegLow, rlObj.lowReg,
3153 mir->offset, NULL);
3154 loadWordDisp(cUnit, rlObj.lowReg, gDvm.offJavaLangString_count, regMax);
3155 loadWordDisp(cUnit, rlObj.lowReg, gDvm.offJavaLangString_offset, regOff);
3156 loadWordDisp(cUnit, rlObj.lowReg, gDvm.offJavaLangString_value, regPtr);
3157 genBoundsCheck(cUnit, rlIdx.lowReg, regMax, mir->offset, pcrLabel);
3158 dvmCompilerFreeTemp(cUnit, regMax);
3159 opRegImm(cUnit, kOpAdd, regPtr, contents);
3160 opRegReg(cUnit, kOpAdd, regOff, rlIdx.lowReg);
3161 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
3162 loadBaseIndexed(cUnit, regPtr, regOff, rlResult.lowReg, 1, kUnsignedHalf);
3163 storeValue(cUnit, rlDest, rlResult);
3164 return false;
3165}
3166
3167static bool genInlinedAbsInt(CompilationUnit *cUnit, MIR *mir)
3168{
3169 RegLocation rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
3170 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
3171 RegLocation rlDest = inlinedTarget(cUnit, mir, false);;
3172 RegLocation rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
3173 int signReg = dvmCompilerAllocTemp(cUnit);
3174 /*
3175 * abs(x) = y<=x>>31, (x+y)^y.
3176 * Thumb2's IT block also yields 3 instructions, but imposes
3177 * scheduling constraints.
3178 */
3179 opRegRegImm(cUnit, kOpAsr, signReg, rlSrc.lowReg, 31);
3180 opRegRegReg(cUnit, kOpAdd, rlResult.lowReg, rlSrc.lowReg, signReg);
3181 opRegReg(cUnit, kOpXor, rlResult.lowReg, signReg);
3182 storeValue(cUnit, rlDest, rlResult);
3183 return false;
3184}
3185
3186static bool genInlinedAbsLong(CompilationUnit *cUnit, MIR *mir)
3187{
3188 RegLocation rlSrc = dvmCompilerGetSrcWide(cUnit, mir, 0, 1);
3189 RegLocation rlDest = inlinedTargetWide(cUnit, mir, false);
3190 rlSrc = loadValueWide(cUnit, rlSrc, kCoreReg);
3191 RegLocation rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
3192 int signReg = dvmCompilerAllocTemp(cUnit);
3193 /*
3194 * abs(x) = y<=x>>31, (x+y)^y.
3195 * Thumb2 IT block allows slightly shorter sequence,
3196 * but introduces a scheduling barrier. Stick with this
3197 * mechanism for now.
3198 */
3199 opRegRegImm(cUnit, kOpAsr, signReg, rlSrc.highReg, 31);
3200 opRegRegReg(cUnit, kOpAdd, rlResult.lowReg, rlSrc.lowReg, signReg);
3201 opRegRegReg(cUnit, kOpAdc, rlResult.highReg, rlSrc.highReg, signReg);
3202 opRegReg(cUnit, kOpXor, rlResult.lowReg, signReg);
3203 opRegReg(cUnit, kOpXor, rlResult.highReg, signReg);
3204 storeValueWide(cUnit, rlDest, rlResult);
3205 return false;
3206}
Bill Buzbeefd023aa2009-11-02 09:23:49 -08003207
3208/*
Bill Buzbeece46c942009-11-20 15:41:34 -08003209 * NOTE: Handles both range and non-range versions (arguments
3210 * have already been normalized by this point).
Ben Chengba4fc8b2009-06-01 13:00:29 -07003211 */
Bill Buzbeece46c942009-11-20 15:41:34 -08003212static bool handleExecuteInline(CompilationUnit *cUnit, MIR *mir)
Ben Chengba4fc8b2009-06-01 13:00:29 -07003213{
3214 DecodedInstruction *dInsn = &mir->dalvikInsn;
3215 switch( mir->dalvikInsn.opCode) {
Bill Buzbeece46c942009-11-20 15:41:34 -08003216 case OP_EXECUTE_INLINE_RANGE:
Ben Chengba4fc8b2009-06-01 13:00:29 -07003217 case OP_EXECUTE_INLINE: {
3218 unsigned int i;
3219 const InlineOperation* inLineTable = dvmGetInlineOpsTable();
Bill Buzbee50a6bf22009-07-08 13:08:04 -07003220 int offset = offsetof(InterpState, retval);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003221 int operation = dInsn->vB;
Bill Buzbee1465db52009-09-23 17:17:35 -07003222 int tReg1;
3223 int tReg2;
Bill Buzbee50a6bf22009-07-08 13:08:04 -07003224 switch (operation) {
3225 case INLINE_EMPTYINLINEMETHOD:
3226 return false; /* Nop */
3227 case INLINE_STRING_LENGTH:
3228 return genInlinedStringLength(cUnit, mir);
Elliott Hughesee34f592010-04-05 18:13:52 -07003229 case INLINE_STRING_IS_EMPTY:
3230 return genInlinedStringIsEmpty(cUnit, mir);
Bill Buzbee50a6bf22009-07-08 13:08:04 -07003231 case INLINE_MATH_ABS_INT:
3232 return genInlinedAbsInt(cUnit, mir);
3233 case INLINE_MATH_ABS_LONG:
3234 return genInlinedAbsLong(cUnit, mir);
3235 case INLINE_MATH_MIN_INT:
3236 return genInlinedMinMaxInt(cUnit, mir, true);
3237 case INLINE_MATH_MAX_INT:
3238 return genInlinedMinMaxInt(cUnit, mir, false);
3239 case INLINE_STRING_CHARAT:
3240 return genInlinedStringCharAt(cUnit, mir);
3241 case INLINE_MATH_SQRT:
3242 if (genInlineSqrt(cUnit, mir))
Bill Buzbee9727c3d2009-08-01 11:32:36 -07003243 return false;
Bill Buzbee50a6bf22009-07-08 13:08:04 -07003244 else
3245 break; /* Handle with C routine */
Bill Buzbee50a6bf22009-07-08 13:08:04 -07003246 case INLINE_MATH_ABS_FLOAT:
Bill Buzbee1465db52009-09-23 17:17:35 -07003247 if (genInlinedAbsFloat(cUnit, mir))
3248 return false;
3249 else
3250 break;
Bill Buzbee50a6bf22009-07-08 13:08:04 -07003251 case INLINE_MATH_ABS_DOUBLE:
Bill Buzbee1465db52009-09-23 17:17:35 -07003252 if (genInlinedAbsDouble(cUnit, mir))
3253 return false;
3254 else
3255 break;
Bill Buzbee50a6bf22009-07-08 13:08:04 -07003256 case INLINE_STRING_COMPARETO:
Bill Buzbeefd023aa2009-11-02 09:23:49 -08003257 if (genInlinedCompareTo(cUnit, mir))
3258 return false;
3259 else
3260 break;
Elliott Hughes2bdbcb62010-04-12 14:29:37 -07003261 case INLINE_STRING_FASTINDEXOF_II:
3262 if (genInlinedFastIndexOf(cUnit, mir))
Bill Buzbeefd023aa2009-11-02 09:23:49 -08003263 return false;
3264 else
3265 break;
3266 case INLINE_STRING_EQUALS:
3267 case INLINE_MATH_COS:
3268 case INLINE_MATH_SIN:
3269 break; /* Handle with C routine */
Bill Buzbee50a6bf22009-07-08 13:08:04 -07003270 default:
Bill Buzbeefc519dc2010-03-06 23:30:57 -08003271 dvmCompilerAbort(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003272 }
Bill Buzbeec6f10662010-02-09 11:16:15 -08003273 dvmCompilerFlushAllRegs(cUnit); /* Everything to home location */
Elliott Hughes6a555132010-02-25 15:41:42 -08003274 dvmCompilerClobberCallRegs(cUnit);
Bill Buzbeec6f10662010-02-09 11:16:15 -08003275 dvmCompilerClobber(cUnit, r4PC);
3276 dvmCompilerClobber(cUnit, r7);
Bill Buzbee1465db52009-09-23 17:17:35 -07003277 opRegRegImm(cUnit, kOpAdd, r4PC, rGLUE, offset);
3278 opImm(cUnit, kOpPush, (1<<r4PC) | (1<<r7));
Ben Chengbd1326d2010-04-02 15:04:53 -07003279 LOAD_FUNC_ADDR(cUnit, r4PC, (int)inLineTable[operation].func);
Bill Buzbee1465db52009-09-23 17:17:35 -07003280 genExportPC(cUnit, mir);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003281 for (i=0; i < dInsn->vA; i++) {
Bill Buzbeec6f10662010-02-09 11:16:15 -08003282 loadValueDirect(cUnit, dvmCompilerGetSrc(cUnit, mir, i), i);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003283 }
Bill Buzbee1465db52009-09-23 17:17:35 -07003284 opReg(cUnit, kOpBlx, r4PC);
3285 opRegImm(cUnit, kOpAdd, r13, 8);
Bill Buzbeece46c942009-11-20 15:41:34 -08003286 opRegImm(cUnit, kOpCmp, r0, 0); /* NULL? */
3287 ArmLIR *branchOver = opCondBranch(cUnit, kArmCondNe);
3288 loadConstant(cUnit, r0,
3289 (int) (cUnit->method->insns + mir->offset));
3290 genDispatchToHandler(cUnit, TEMPLATE_THROW_EXCEPTION_COMMON);
3291 ArmLIR *target = newLIR0(cUnit, kArmPseudoTargetLabel);
3292 target->defMask = ENCODE_ALL;
3293 branchOver->generic.target = (LIR *) target;
Ben Chengba4fc8b2009-06-01 13:00:29 -07003294 break;
3295 }
3296 default:
3297 return true;
3298 }
3299 return false;
3300}
3301
3302static bool handleFmt51l(CompilationUnit *cUnit, MIR *mir)
3303{
Bill Buzbee1465db52009-09-23 17:17:35 -07003304 //TUNING: We're using core regs here - not optimal when target is a double
Bill Buzbeec6f10662010-02-09 11:16:15 -08003305 RegLocation rlDest = dvmCompilerGetDestWide(cUnit, mir, 0, 1);
3306 RegLocation rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Ben Chengbd1326d2010-04-02 15:04:53 -07003307 loadConstantNoClobber(cUnit, rlResult.lowReg,
3308 mir->dalvikInsn.vB_wide & 0xFFFFFFFFUL);
3309 loadConstantNoClobber(cUnit, rlResult.highReg,
3310 (mir->dalvikInsn.vB_wide>>32) & 0xFFFFFFFFUL);
Bill Buzbee1465db52009-09-23 17:17:35 -07003311 storeValueWide(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003312 return false;
3313}
3314
Ben Chengba4fc8b2009-06-01 13:00:29 -07003315/*
3316 * The following are special processing routines that handle transfer of
3317 * controls between compiled code and the interpreter. Certain VM states like
3318 * Dalvik PC and special-purpose registers are reconstructed here.
3319 */
3320
Bill Buzbeebd047242010-05-13 13:02:53 -07003321/*
3322 * Insert a
3323 * b .+4
3324 * nop
3325 * pair at the beginning of a chaining cell. This serves as the
3326 * switch branch that selects between reverting to the interpreter or
3327 * not. Once the cell is chained to a translation, the cell will
3328 * contain a 32-bit branch. Subsequent chain/unchain operations will
3329 * then only alter that first 16-bits - the "b .+4" for unchaining,
3330 * and the restoration of the first half of the 32-bit branch for
3331 * rechaining.
3332 */
3333static void insertChainingSwitch(CompilationUnit *cUnit)
3334{
3335 ArmLIR *branch = newLIR0(cUnit, kThumbBUncond);
3336 newLIR2(cUnit, kThumbOrr, r0, r0);
3337 ArmLIR *target = newLIR0(cUnit, kArmPseudoTargetLabel);
3338 target->defMask = ENCODE_ALL;
3339 branch->generic.target = (LIR *) target;
3340}
3341
Ben Cheng1efc9c52009-06-08 18:25:27 -07003342/* Chaining cell for code that may need warmup. */
3343static void handleNormalChainingCell(CompilationUnit *cUnit,
3344 unsigned int offset)
Ben Chengba4fc8b2009-06-01 13:00:29 -07003345{
Ben Cheng11d8f142010-03-24 15:24:19 -07003346 /*
3347 * Use raw instruction constructors to guarantee that the generated
3348 * instructions fit the predefined cell size.
3349 */
Bill Buzbeebd047242010-05-13 13:02:53 -07003350 insertChainingSwitch(cUnit);
Ben Cheng11d8f142010-03-24 15:24:19 -07003351 newLIR3(cUnit, kThumbLdrRRI5, r0, rGLUE,
3352 offsetof(InterpState,
3353 jitToInterpEntries.dvmJitToInterpNormal) >> 2);
3354 newLIR1(cUnit, kThumbBlxR, r0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003355 addWordData(cUnit, (int) (cUnit->method->insns + offset), true);
3356}
3357
3358/*
Ben Cheng1efc9c52009-06-08 18:25:27 -07003359 * Chaining cell for instructions that immediately following already translated
3360 * code.
Ben Chengba4fc8b2009-06-01 13:00:29 -07003361 */
Ben Cheng1efc9c52009-06-08 18:25:27 -07003362static void handleHotChainingCell(CompilationUnit *cUnit,
3363 unsigned int offset)
Ben Chengba4fc8b2009-06-01 13:00:29 -07003364{
Ben Cheng11d8f142010-03-24 15:24:19 -07003365 /*
3366 * Use raw instruction constructors to guarantee that the generated
3367 * instructions fit the predefined cell size.
3368 */
Bill Buzbeebd047242010-05-13 13:02:53 -07003369 insertChainingSwitch(cUnit);
Ben Cheng11d8f142010-03-24 15:24:19 -07003370 newLIR3(cUnit, kThumbLdrRRI5, r0, rGLUE,
3371 offsetof(InterpState,
3372 jitToInterpEntries.dvmJitToInterpTraceSelect) >> 2);
3373 newLIR1(cUnit, kThumbBlxR, r0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003374 addWordData(cUnit, (int) (cUnit->method->insns + offset), true);
3375}
3376
Bill Buzbee9c4b7c82009-09-10 10:10:38 -07003377#if defined(WITH_SELF_VERIFICATION) || defined(WITH_JIT_TUNING)
Jeff Hao97319a82009-08-12 16:57:15 -07003378/* Chaining cell for branches that branch back into the same basic block */
3379static void handleBackwardBranchChainingCell(CompilationUnit *cUnit,
3380 unsigned int offset)
3381{
Ben Cheng11d8f142010-03-24 15:24:19 -07003382 /*
3383 * Use raw instruction constructors to guarantee that the generated
3384 * instructions fit the predefined cell size.
3385 */
Bill Buzbeebd047242010-05-13 13:02:53 -07003386 insertChainingSwitch(cUnit);
Bill Buzbee9c4b7c82009-09-10 10:10:38 -07003387#if defined(WITH_SELF_VERIFICATION)
Bill Buzbee1465db52009-09-23 17:17:35 -07003388 newLIR3(cUnit, kThumbLdrRRI5, r0, rGLUE,
Ben Cheng40094c12010-02-24 20:58:44 -08003389 offsetof(InterpState,
3390 jitToInterpEntries.dvmJitToInterpBackwardBranch) >> 2);
Bill Buzbee9c4b7c82009-09-10 10:10:38 -07003391#else
Bill Buzbee1465db52009-09-23 17:17:35 -07003392 newLIR3(cUnit, kThumbLdrRRI5, r0, rGLUE,
Bill Buzbee9c4b7c82009-09-10 10:10:38 -07003393 offsetof(InterpState, jitToInterpEntries.dvmJitToInterpNormal) >> 2);
3394#endif
Bill Buzbee1465db52009-09-23 17:17:35 -07003395 newLIR1(cUnit, kThumbBlxR, r0);
Jeff Hao97319a82009-08-12 16:57:15 -07003396 addWordData(cUnit, (int) (cUnit->method->insns + offset), true);
3397}
3398
3399#endif
Ben Chengba4fc8b2009-06-01 13:00:29 -07003400/* Chaining cell for monomorphic method invocations. */
Ben Cheng38329f52009-07-07 14:19:20 -07003401static void handleInvokeSingletonChainingCell(CompilationUnit *cUnit,
3402 const Method *callee)
Ben Chengba4fc8b2009-06-01 13:00:29 -07003403{
Ben Cheng11d8f142010-03-24 15:24:19 -07003404 /*
3405 * Use raw instruction constructors to guarantee that the generated
3406 * instructions fit the predefined cell size.
3407 */
Bill Buzbeebd047242010-05-13 13:02:53 -07003408 insertChainingSwitch(cUnit);
Ben Cheng11d8f142010-03-24 15:24:19 -07003409 newLIR3(cUnit, kThumbLdrRRI5, r0, rGLUE,
3410 offsetof(InterpState,
3411 jitToInterpEntries.dvmJitToInterpTraceSelect) >> 2);
3412 newLIR1(cUnit, kThumbBlxR, r0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003413 addWordData(cUnit, (int) (callee->insns), true);
3414}
3415
Ben Cheng38329f52009-07-07 14:19:20 -07003416/* Chaining cell for monomorphic method invocations. */
3417static void handleInvokePredictedChainingCell(CompilationUnit *cUnit)
3418{
3419
3420 /* Should not be executed in the initial state */
3421 addWordData(cUnit, PREDICTED_CHAIN_BX_PAIR_INIT, true);
3422 /* To be filled: class */
3423 addWordData(cUnit, PREDICTED_CHAIN_CLAZZ_INIT, true);
3424 /* To be filled: method */
3425 addWordData(cUnit, PREDICTED_CHAIN_METHOD_INIT, true);
3426 /*
3427 * Rechain count. The initial value of 0 here will trigger chaining upon
3428 * the first invocation of this callsite.
3429 */
3430 addWordData(cUnit, PREDICTED_CHAIN_COUNTER_INIT, true);
3431}
3432
Ben Chengba4fc8b2009-06-01 13:00:29 -07003433/* Load the Dalvik PC into r0 and jump to the specified target */
3434static void handlePCReconstruction(CompilationUnit *cUnit,
Bill Buzbee89efc3d2009-07-28 11:22:22 -07003435 ArmLIR *targetLabel)
Ben Chengba4fc8b2009-06-01 13:00:29 -07003436{
Bill Buzbee89efc3d2009-07-28 11:22:22 -07003437 ArmLIR **pcrLabel =
3438 (ArmLIR **) cUnit->pcReconstructionList.elemList;
Ben Chengba4fc8b2009-06-01 13:00:29 -07003439 int numElems = cUnit->pcReconstructionList.numUsed;
3440 int i;
3441 for (i = 0; i < numElems; i++) {
3442 dvmCompilerAppendLIR(cUnit, (LIR *) pcrLabel[i]);
3443 /* r0 = dalvik PC */
3444 loadConstant(cUnit, r0, pcrLabel[i]->operands[0]);
3445 genUnconditionalBranch(cUnit, targetLabel);
3446 }
3447}
3448
Bill Buzbee1465db52009-09-23 17:17:35 -07003449static char *extendedMIROpNames[kMirOpLast - kMirOpFirst] = {
3450 "kMirOpPhi",
3451 "kMirOpNullNRangeUpCheck",
3452 "kMirOpNullNRangeDownCheck",
3453 "kMirOpLowerBound",
3454 "kMirOpPunt",
Ben Cheng4238ec22009-08-24 16:32:22 -07003455};
3456
3457/*
3458 * vA = arrayReg;
3459 * vB = idxReg;
3460 * vC = endConditionReg;
3461 * arg[0] = maxC
3462 * arg[1] = minC
3463 * arg[2] = loopBranchConditionCode
3464 */
3465static void genHoistedChecksForCountUpLoop(CompilationUnit *cUnit, MIR *mir)
3466{
Bill Buzbee1465db52009-09-23 17:17:35 -07003467 /*
3468 * NOTE: these synthesized blocks don't have ssa names assigned
3469 * for Dalvik registers. However, because they dominate the following
3470 * blocks we can simply use the Dalvik name w/ subscript 0 as the
3471 * ssa name.
3472 */
Ben Cheng4238ec22009-08-24 16:32:22 -07003473 DecodedInstruction *dInsn = &mir->dalvikInsn;
3474 const int lenOffset = offsetof(ArrayObject, length);
Ben Cheng4238ec22009-08-24 16:32:22 -07003475 const int maxC = dInsn->arg[0];
3476 const int minC = dInsn->arg[1];
Bill Buzbee1465db52009-09-23 17:17:35 -07003477 int regLength;
3478 RegLocation rlArray = cUnit->regLocation[mir->dalvikInsn.vA];
3479 RegLocation rlIdxEnd = cUnit->regLocation[mir->dalvikInsn.vC];
Ben Cheng4238ec22009-08-24 16:32:22 -07003480
3481 /* regArray <- arrayRef */
Bill Buzbee1465db52009-09-23 17:17:35 -07003482 rlArray = loadValue(cUnit, rlArray, kCoreReg);
3483 rlIdxEnd = loadValue(cUnit, rlIdxEnd, kCoreReg);
3484 genRegImmCheck(cUnit, kArmCondEq, rlArray.lowReg, 0, 0,
Ben Cheng4238ec22009-08-24 16:32:22 -07003485 (ArmLIR *) cUnit->loopAnalysis->branchToPCR);
3486
3487 /* regLength <- len(arrayRef) */
Bill Buzbeec6f10662010-02-09 11:16:15 -08003488 regLength = dvmCompilerAllocTemp(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07003489 loadWordDisp(cUnit, rlArray.lowReg, lenOffset, regLength);
Ben Cheng4238ec22009-08-24 16:32:22 -07003490
3491 int delta = maxC;
3492 /*
3493 * If the loop end condition is ">=" instead of ">", then the largest value
3494 * of the index is "endCondition - 1".
3495 */
3496 if (dInsn->arg[2] == OP_IF_GE) {
3497 delta--;
3498 }
3499
3500 if (delta) {
Bill Buzbeec6f10662010-02-09 11:16:15 -08003501 int tReg = dvmCompilerAllocTemp(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07003502 opRegRegImm(cUnit, kOpAdd, tReg, rlIdxEnd.lowReg, delta);
3503 rlIdxEnd.lowReg = tReg;
Bill Buzbeec6f10662010-02-09 11:16:15 -08003504 dvmCompilerFreeTemp(cUnit, tReg);
Ben Cheng4238ec22009-08-24 16:32:22 -07003505 }
3506 /* Punt if "regIdxEnd < len(Array)" is false */
Bill Buzbee1465db52009-09-23 17:17:35 -07003507 genRegRegCheck(cUnit, kArmCondGe, rlIdxEnd.lowReg, regLength, 0,
Ben Cheng0fd31e42009-09-03 14:40:16 -07003508 (ArmLIR *) cUnit->loopAnalysis->branchToPCR);
Ben Cheng4238ec22009-08-24 16:32:22 -07003509}
3510
3511/*
3512 * vA = arrayReg;
3513 * vB = idxReg;
3514 * vC = endConditionReg;
3515 * arg[0] = maxC
3516 * arg[1] = minC
3517 * arg[2] = loopBranchConditionCode
3518 */
3519static void genHoistedChecksForCountDownLoop(CompilationUnit *cUnit, MIR *mir)
3520{
3521 DecodedInstruction *dInsn = &mir->dalvikInsn;
3522 const int lenOffset = offsetof(ArrayObject, length);
Bill Buzbeec6f10662010-02-09 11:16:15 -08003523 const int regLength = dvmCompilerAllocTemp(cUnit);
Ben Cheng4238ec22009-08-24 16:32:22 -07003524 const int maxC = dInsn->arg[0];
3525 const int minC = dInsn->arg[1];
Bill Buzbee1465db52009-09-23 17:17:35 -07003526 RegLocation rlArray = cUnit->regLocation[mir->dalvikInsn.vA];
3527 RegLocation rlIdxInit = cUnit->regLocation[mir->dalvikInsn.vB];
Ben Cheng4238ec22009-08-24 16:32:22 -07003528
3529 /* regArray <- arrayRef */
Bill Buzbee1465db52009-09-23 17:17:35 -07003530 rlArray = loadValue(cUnit, rlArray, kCoreReg);
3531 rlIdxInit = loadValue(cUnit, rlIdxInit, kCoreReg);
3532 genRegImmCheck(cUnit, kArmCondEq, rlArray.lowReg, 0, 0,
Ben Cheng4238ec22009-08-24 16:32:22 -07003533 (ArmLIR *) cUnit->loopAnalysis->branchToPCR);
3534
3535 /* regLength <- len(arrayRef) */
Bill Buzbee1465db52009-09-23 17:17:35 -07003536 loadWordDisp(cUnit, rlArray.lowReg, lenOffset, regLength);
Ben Cheng4238ec22009-08-24 16:32:22 -07003537
3538 if (maxC) {
Bill Buzbeec6f10662010-02-09 11:16:15 -08003539 int tReg = dvmCompilerAllocTemp(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07003540 opRegRegImm(cUnit, kOpAdd, tReg, rlIdxInit.lowReg, maxC);
3541 rlIdxInit.lowReg = tReg;
Bill Buzbeec6f10662010-02-09 11:16:15 -08003542 dvmCompilerFreeTemp(cUnit, tReg);
Ben Cheng4238ec22009-08-24 16:32:22 -07003543 }
3544
3545 /* Punt if "regIdxInit < len(Array)" is false */
Bill Buzbee1465db52009-09-23 17:17:35 -07003546 genRegRegCheck(cUnit, kArmCondGe, rlIdxInit.lowReg, regLength, 0,
Ben Cheng0fd31e42009-09-03 14:40:16 -07003547 (ArmLIR *) cUnit->loopAnalysis->branchToPCR);
Ben Cheng4238ec22009-08-24 16:32:22 -07003548}
3549
3550/*
3551 * vA = idxReg;
3552 * vB = minC;
3553 */
3554static void genHoistedLowerBoundCheck(CompilationUnit *cUnit, MIR *mir)
3555{
3556 DecodedInstruction *dInsn = &mir->dalvikInsn;
Ben Cheng4238ec22009-08-24 16:32:22 -07003557 const int minC = dInsn->vB;
Bill Buzbee1465db52009-09-23 17:17:35 -07003558 RegLocation rlIdx = cUnit->regLocation[mir->dalvikInsn.vA];
Ben Cheng4238ec22009-08-24 16:32:22 -07003559
3560 /* regIdx <- initial index value */
Bill Buzbee1465db52009-09-23 17:17:35 -07003561 rlIdx = loadValue(cUnit, rlIdx, kCoreReg);
Ben Cheng4238ec22009-08-24 16:32:22 -07003562
3563 /* Punt if "regIdxInit + minC >= 0" is false */
Bill Buzbee1465db52009-09-23 17:17:35 -07003564 genRegImmCheck(cUnit, kArmCondLt, rlIdx.lowReg, -minC, 0,
Ben Cheng4238ec22009-08-24 16:32:22 -07003565 (ArmLIR *) cUnit->loopAnalysis->branchToPCR);
3566}
3567
3568/* Extended MIR instructions like PHI */
3569static void handleExtendedMIR(CompilationUnit *cUnit, MIR *mir)
3570{
Bill Buzbee1465db52009-09-23 17:17:35 -07003571 int opOffset = mir->dalvikInsn.opCode - kMirOpFirst;
Ben Cheng4238ec22009-08-24 16:32:22 -07003572 char *msg = dvmCompilerNew(strlen(extendedMIROpNames[opOffset]) + 1,
3573 false);
3574 strcpy(msg, extendedMIROpNames[opOffset]);
Bill Buzbee1465db52009-09-23 17:17:35 -07003575 newLIR1(cUnit, kArmPseudoExtended, (int) msg);
Ben Cheng4238ec22009-08-24 16:32:22 -07003576
3577 switch (mir->dalvikInsn.opCode) {
Bill Buzbee1465db52009-09-23 17:17:35 -07003578 case kMirOpPhi: {
Ben Cheng4238ec22009-08-24 16:32:22 -07003579 char *ssaString = dvmCompilerGetSSAString(cUnit, mir->ssaRep);
Bill Buzbee1465db52009-09-23 17:17:35 -07003580 newLIR1(cUnit, kArmPseudoSSARep, (int) ssaString);
Ben Cheng4238ec22009-08-24 16:32:22 -07003581 break;
3582 }
Bill Buzbee1465db52009-09-23 17:17:35 -07003583 case kMirOpNullNRangeUpCheck: {
Ben Cheng4238ec22009-08-24 16:32:22 -07003584 genHoistedChecksForCountUpLoop(cUnit, mir);
3585 break;
3586 }
Bill Buzbee1465db52009-09-23 17:17:35 -07003587 case kMirOpNullNRangeDownCheck: {
Ben Cheng4238ec22009-08-24 16:32:22 -07003588 genHoistedChecksForCountDownLoop(cUnit, mir);
3589 break;
3590 }
Bill Buzbee1465db52009-09-23 17:17:35 -07003591 case kMirOpLowerBound: {
Ben Cheng4238ec22009-08-24 16:32:22 -07003592 genHoistedLowerBoundCheck(cUnit, mir);
3593 break;
3594 }
Bill Buzbee1465db52009-09-23 17:17:35 -07003595 case kMirOpPunt: {
Ben Cheng4238ec22009-08-24 16:32:22 -07003596 genUnconditionalBranch(cUnit,
3597 (ArmLIR *) cUnit->loopAnalysis->branchToPCR);
3598 break;
3599 }
3600 default:
3601 break;
3602 }
3603}
3604
3605/*
3606 * Create a PC-reconstruction cell for the starting offset of this trace.
3607 * Since the PCR cell is placed near the end of the compiled code which is
3608 * usually out of range for a conditional branch, we put two branches (one
3609 * branch over to the loop body and one layover branch to the actual PCR) at the
3610 * end of the entry block.
3611 */
3612static void setupLoopEntryBlock(CompilationUnit *cUnit, BasicBlock *entry,
3613 ArmLIR *bodyLabel)
3614{
3615 /* Set up the place holder to reconstruct this Dalvik PC */
3616 ArmLIR *pcrLabel = dvmCompilerNew(sizeof(ArmLIR), true);
Ben Chenga4973592010-03-31 11:59:18 -07003617 pcrLabel->opCode = kArmPseudoPCReconstructionCell;
Ben Cheng4238ec22009-08-24 16:32:22 -07003618 pcrLabel->operands[0] =
3619 (int) (cUnit->method->insns + entry->startOffset);
3620 pcrLabel->operands[1] = entry->startOffset;
3621 /* Insert the place holder to the growable list */
3622 dvmInsertGrowableList(&cUnit->pcReconstructionList, pcrLabel);
3623
3624 /*
3625 * Next, create two branches - one branch over to the loop body and the
3626 * other branch to the PCR cell to punt.
3627 */
3628 ArmLIR *branchToBody = dvmCompilerNew(sizeof(ArmLIR), true);
Bill Buzbee1465db52009-09-23 17:17:35 -07003629 branchToBody->opCode = kThumbBUncond;
Ben Cheng4238ec22009-08-24 16:32:22 -07003630 branchToBody->generic.target = (LIR *) bodyLabel;
Ben Chengdcf3e5d2009-09-11 13:42:05 -07003631 setupResourceMasks(branchToBody);
Ben Cheng4238ec22009-08-24 16:32:22 -07003632 cUnit->loopAnalysis->branchToBody = (LIR *) branchToBody;
3633
3634 ArmLIR *branchToPCR = dvmCompilerNew(sizeof(ArmLIR), true);
Bill Buzbee1465db52009-09-23 17:17:35 -07003635 branchToPCR->opCode = kThumbBUncond;
Ben Cheng4238ec22009-08-24 16:32:22 -07003636 branchToPCR->generic.target = (LIR *) pcrLabel;
Ben Chengdcf3e5d2009-09-11 13:42:05 -07003637 setupResourceMasks(branchToPCR);
Ben Cheng4238ec22009-08-24 16:32:22 -07003638 cUnit->loopAnalysis->branchToPCR = (LIR *) branchToPCR;
3639}
3640
Ben Chengd5adae12010-03-26 17:45:28 -07003641#if defined(WITH_SELF_VERIFICATION)
3642static bool selfVerificationPuntOps(MIR *mir)
3643{
3644 DecodedInstruction *decInsn = &mir->dalvikInsn;
3645 OpCode op = decInsn->opCode;
3646 int flags = dexGetInstrFlags(gDvm.instrFlags, op);
3647 /*
3648 * All opcodes that can throw exceptions and use the
3649 * TEMPLATE_THROW_EXCEPTION_COMMON template should be excluded in the trace
3650 * under self-verification mode.
3651 */
3652 return (op == OP_MONITOR_ENTER || op == OP_MONITOR_EXIT ||
3653 op == OP_NEW_INSTANCE || op == OP_NEW_ARRAY ||
3654 op == OP_CHECK_CAST || op == OP_MOVE_EXCEPTION ||
3655 op == OP_FILL_ARRAY_DATA || op == OP_EXECUTE_INLINE ||
3656 op == OP_EXECUTE_INLINE_RANGE ||
3657 (flags & kInstrInvoke));
3658}
3659#endif
3660
Ben Chengba4fc8b2009-06-01 13:00:29 -07003661void dvmCompilerMIR2LIR(CompilationUnit *cUnit)
3662{
3663 /* Used to hold the labels of each block */
Bill Buzbee89efc3d2009-07-28 11:22:22 -07003664 ArmLIR *labelList =
3665 dvmCompilerNew(sizeof(ArmLIR) * cUnit->numBlocks, true);
Ben Chengcec26f62010-01-15 15:29:33 -08003666 GrowableList chainingListByType[kChainingCellGap];
Ben Chengba4fc8b2009-06-01 13:00:29 -07003667 int i;
3668
3669 /*
Ben Cheng38329f52009-07-07 14:19:20 -07003670 * Initialize various types chaining lists.
Ben Chengba4fc8b2009-06-01 13:00:29 -07003671 */
Ben Chengcec26f62010-01-15 15:29:33 -08003672 for (i = 0; i < kChainingCellGap; i++) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07003673 dvmInitGrowableList(&chainingListByType[i], 2);
3674 }
3675
3676 BasicBlock **blockList = cUnit->blockList;
3677
Bill Buzbee6e963e12009-06-17 16:56:19 -07003678 if (cUnit->executionCount) {
3679 /*
3680 * Reserve 6 bytes at the beginning of the trace
3681 * +----------------------------+
3682 * | execution count (4 bytes) |
3683 * +----------------------------+
3684 * | chain cell offset (2 bytes)|
3685 * +----------------------------+
3686 * ...and then code to increment the execution
3687 * count:
3688 * mov r0, pc @ move adr of "mov r0,pc" + 4 to r0
3689 * sub r0, #10 @ back up to addr of executionCount
3690 * ldr r1, [r0]
3691 * add r1, #1
3692 * str r1, [r0]
3693 */
Bill Buzbee1465db52009-09-23 17:17:35 -07003694 newLIR1(cUnit, kArm16BitData, 0);
3695 newLIR1(cUnit, kArm16BitData, 0);
Ben Chengcc6600c2009-06-22 14:45:16 -07003696 cUnit->chainCellOffsetLIR =
Bill Buzbee1465db52009-09-23 17:17:35 -07003697 (LIR *) newLIR1(cUnit, kArm16BitData, CHAIN_CELL_OFFSET_TAG);
Bill Buzbee6e963e12009-06-17 16:56:19 -07003698 cUnit->headerSize = 6;
Bill Buzbee270c1d62009-08-13 16:58:07 -07003699 /* Thumb instruction used directly here to ensure correct size */
Bill Buzbee1465db52009-09-23 17:17:35 -07003700 newLIR2(cUnit, kThumbMovRR_H2L, r0, rpc);
3701 newLIR2(cUnit, kThumbSubRI8, r0, 10);
3702 newLIR3(cUnit, kThumbLdrRRI5, r1, r0, 0);
3703 newLIR2(cUnit, kThumbAddRI8, r1, 1);
3704 newLIR3(cUnit, kThumbStrRRI5, r1, r0, 0);
Bill Buzbee6e963e12009-06-17 16:56:19 -07003705 } else {
3706 /* Just reserve 2 bytes for the chain cell offset */
Ben Chengcc6600c2009-06-22 14:45:16 -07003707 cUnit->chainCellOffsetLIR =
Bill Buzbee1465db52009-09-23 17:17:35 -07003708 (LIR *) newLIR1(cUnit, kArm16BitData, CHAIN_CELL_OFFSET_TAG);
Bill Buzbee6e963e12009-06-17 16:56:19 -07003709 cUnit->headerSize = 2;
3710 }
Ben Cheng1efc9c52009-06-08 18:25:27 -07003711
Ben Chengba4fc8b2009-06-01 13:00:29 -07003712 /* Handle the content in each basic block */
3713 for (i = 0; i < cUnit->numBlocks; i++) {
3714 blockList[i]->visited = true;
3715 MIR *mir;
3716
3717 labelList[i].operands[0] = blockList[i]->startOffset;
3718
Ben Chengcec26f62010-01-15 15:29:33 -08003719 if (blockList[i]->blockType >= kChainingCellGap) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07003720 /*
3721 * Append the label pseudo LIR first. Chaining cells will be handled
3722 * separately afterwards.
3723 */
3724 dvmCompilerAppendLIR(cUnit, (LIR *) &labelList[i]);
3725 }
3726
Bill Buzbee1465db52009-09-23 17:17:35 -07003727 if (blockList[i]->blockType == kEntryBlock) {
Ben Chenga4973592010-03-31 11:59:18 -07003728 labelList[i].opCode = kArmPseudoEntryBlock;
Ben Cheng4238ec22009-08-24 16:32:22 -07003729 if (blockList[i]->firstMIRInsn == NULL) {
3730 continue;
3731 } else {
3732 setupLoopEntryBlock(cUnit, blockList[i],
3733 &labelList[blockList[i]->fallThrough->id]);
3734 }
Bill Buzbee1465db52009-09-23 17:17:35 -07003735 } else if (blockList[i]->blockType == kExitBlock) {
Ben Chenga4973592010-03-31 11:59:18 -07003736 labelList[i].opCode = kArmPseudoExitBlock;
Ben Cheng4238ec22009-08-24 16:32:22 -07003737 goto gen_fallthrough;
Bill Buzbee1465db52009-09-23 17:17:35 -07003738 } else if (blockList[i]->blockType == kDalvikByteCode) {
3739 labelList[i].opCode = kArmPseudoNormalBlockLabel;
Ben Chenge9695e52009-06-16 16:11:47 -07003740 /* Reset the register state */
Bill Buzbeec6f10662010-02-09 11:16:15 -08003741 dvmCompilerResetRegPool(cUnit);
3742 dvmCompilerClobberAllRegs(cUnit);
3743 dvmCompilerResetNullCheck(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003744 } else {
3745 switch (blockList[i]->blockType) {
Bill Buzbee1465db52009-09-23 17:17:35 -07003746 case kChainingCellNormal:
Ben Chenga4973592010-03-31 11:59:18 -07003747 labelList[i].opCode = kArmPseudoChainingCellNormal;
Ben Chengba4fc8b2009-06-01 13:00:29 -07003748 /* handle the codegen later */
3749 dvmInsertGrowableList(
Bill Buzbee1465db52009-09-23 17:17:35 -07003750 &chainingListByType[kChainingCellNormal], (void *) i);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003751 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07003752 case kChainingCellInvokeSingleton:
Ben Cheng38329f52009-07-07 14:19:20 -07003753 labelList[i].opCode =
Ben Chenga4973592010-03-31 11:59:18 -07003754 kArmPseudoChainingCellInvokeSingleton;
Ben Chengba4fc8b2009-06-01 13:00:29 -07003755 labelList[i].operands[0] =
3756 (int) blockList[i]->containingMethod;
3757 /* handle the codegen later */
3758 dvmInsertGrowableList(
Bill Buzbee1465db52009-09-23 17:17:35 -07003759 &chainingListByType[kChainingCellInvokeSingleton],
Ben Cheng38329f52009-07-07 14:19:20 -07003760 (void *) i);
3761 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07003762 case kChainingCellInvokePredicted:
Ben Cheng38329f52009-07-07 14:19:20 -07003763 labelList[i].opCode =
Ben Chenga4973592010-03-31 11:59:18 -07003764 kArmPseudoChainingCellInvokePredicted;
Ben Cheng38329f52009-07-07 14:19:20 -07003765 /* handle the codegen later */
3766 dvmInsertGrowableList(
Bill Buzbee1465db52009-09-23 17:17:35 -07003767 &chainingListByType[kChainingCellInvokePredicted],
Ben Cheng38329f52009-07-07 14:19:20 -07003768 (void *) i);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003769 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07003770 case kChainingCellHot:
Ben Chengba4fc8b2009-06-01 13:00:29 -07003771 labelList[i].opCode =
Ben Chenga4973592010-03-31 11:59:18 -07003772 kArmPseudoChainingCellHot;
Ben Chengba4fc8b2009-06-01 13:00:29 -07003773 /* handle the codegen later */
3774 dvmInsertGrowableList(
Bill Buzbee1465db52009-09-23 17:17:35 -07003775 &chainingListByType[kChainingCellHot],
Ben Chengba4fc8b2009-06-01 13:00:29 -07003776 (void *) i);
3777 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07003778 case kPCReconstruction:
Ben Chengba4fc8b2009-06-01 13:00:29 -07003779 /* Make sure exception handling block is next */
3780 labelList[i].opCode =
Ben Chenga4973592010-03-31 11:59:18 -07003781 kArmPseudoPCReconstructionBlockLabel;
Ben Chengba4fc8b2009-06-01 13:00:29 -07003782 assert (i == cUnit->numBlocks - 2);
3783 handlePCReconstruction(cUnit, &labelList[i+1]);
3784 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07003785 case kExceptionHandling:
3786 labelList[i].opCode = kArmPseudoEHBlockLabel;
Ben Chengba4fc8b2009-06-01 13:00:29 -07003787 if (cUnit->pcReconstructionList.numUsed) {
Bill Buzbee270c1d62009-08-13 16:58:07 -07003788 loadWordDisp(cUnit, rGLUE, offsetof(InterpState,
3789 jitToInterpEntries.dvmJitToInterpPunt),
3790 r1);
Bill Buzbee1465db52009-09-23 17:17:35 -07003791 opReg(cUnit, kOpBlx, r1);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003792 }
3793 break;
Bill Buzbee9c4b7c82009-09-10 10:10:38 -07003794#if defined(WITH_SELF_VERIFICATION) || defined(WITH_JIT_TUNING)
Bill Buzbee1465db52009-09-23 17:17:35 -07003795 case kChainingCellBackwardBranch:
Jeff Hao97319a82009-08-12 16:57:15 -07003796 labelList[i].opCode =
Ben Chenga4973592010-03-31 11:59:18 -07003797 kArmPseudoChainingCellBackwardBranch;
Jeff Hao97319a82009-08-12 16:57:15 -07003798 /* handle the codegen later */
3799 dvmInsertGrowableList(
Bill Buzbee1465db52009-09-23 17:17:35 -07003800 &chainingListByType[kChainingCellBackwardBranch],
Jeff Hao97319a82009-08-12 16:57:15 -07003801 (void *) i);
3802 break;
3803#endif
Ben Chengba4fc8b2009-06-01 13:00:29 -07003804 default:
3805 break;
3806 }
3807 continue;
3808 }
Ben Chenge9695e52009-06-16 16:11:47 -07003809
Bill Buzbee89efc3d2009-07-28 11:22:22 -07003810 ArmLIR *headLIR = NULL;
Ben Chenge9695e52009-06-16 16:11:47 -07003811
Ben Chengba4fc8b2009-06-01 13:00:29 -07003812 for (mir = blockList[i]->firstMIRInsn; mir; mir = mir->next) {
Bill Buzbee1465db52009-09-23 17:17:35 -07003813
Bill Buzbeec6f10662010-02-09 11:16:15 -08003814 dvmCompilerResetRegPool(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07003815 if (gDvmJit.disableOpt & (1 << kTrackLiveTemps)) {
Bill Buzbeec6f10662010-02-09 11:16:15 -08003816 dvmCompilerClobberAllRegs(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07003817 }
3818
3819 if (gDvmJit.disableOpt & (1 << kSuppressLoads)) {
Bill Buzbeec6f10662010-02-09 11:16:15 -08003820 dvmCompilerResetDefTracking(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07003821 }
3822
3823 if (mir->dalvikInsn.opCode >= kMirOpFirst) {
Ben Cheng4238ec22009-08-24 16:32:22 -07003824 handleExtendedMIR(cUnit, mir);
3825 continue;
3826 }
3827
Bill Buzbee1465db52009-09-23 17:17:35 -07003828
Ben Chengba4fc8b2009-06-01 13:00:29 -07003829 OpCode dalvikOpCode = mir->dalvikInsn.opCode;
3830 InstructionFormat dalvikFormat =
3831 dexGetInstrFormat(gDvm.instrFormat, dalvikOpCode);
Bill Buzbee89efc3d2009-07-28 11:22:22 -07003832 ArmLIR *boundaryLIR =
Ben Chenga4973592010-03-31 11:59:18 -07003833 newLIR2(cUnit, kArmPseudoDalvikByteCodeBoundary,
Ben Chengccd6c012009-10-15 14:52:45 -07003834 mir->offset,
3835 (int) dvmCompilerGetDalvikDisassembly(&mir->dalvikInsn)
3836 );
Ben Cheng4238ec22009-08-24 16:32:22 -07003837 if (mir->ssaRep) {
3838 char *ssaString = dvmCompilerGetSSAString(cUnit, mir->ssaRep);
Bill Buzbee1465db52009-09-23 17:17:35 -07003839 newLIR1(cUnit, kArmPseudoSSARep, (int) ssaString);
Ben Cheng4238ec22009-08-24 16:32:22 -07003840 }
3841
Ben Chenge9695e52009-06-16 16:11:47 -07003842 /* Remember the first LIR for this block */
3843 if (headLIR == NULL) {
3844 headLIR = boundaryLIR;
Ben Chengd7d426a2009-09-22 11:23:36 -07003845 /* Set the first boundaryLIR as a scheduling barrier */
3846 headLIR->defMask = ENCODE_ALL;
Ben Chenge9695e52009-06-16 16:11:47 -07003847 }
Ben Cheng4238ec22009-08-24 16:32:22 -07003848
Ben Chengba4fc8b2009-06-01 13:00:29 -07003849 bool notHandled;
3850 /*
3851 * Debugging: screen the opcode first to see if it is in the
3852 * do[-not]-compile list
3853 */
3854 bool singleStepMe =
3855 gDvmJit.includeSelectedOp !=
3856 ((gDvmJit.opList[dalvikOpCode >> 3] &
3857 (1 << (dalvikOpCode & 0x7))) !=
3858 0);
Ben Chengd5adae12010-03-26 17:45:28 -07003859#if defined(WITH_SELF_VERIFICATION)
3860 if (singleStepMe == false) {
3861 singleStepMe = selfVerificationPuntOps(mir);
3862 }
3863#endif
Ben Chengba4fc8b2009-06-01 13:00:29 -07003864 if (singleStepMe || cUnit->allSingleStep) {
3865 notHandled = false;
3866 genInterpSingleStep(cUnit, mir);
3867 } else {
3868 opcodeCoverage[dalvikOpCode]++;
3869 switch (dalvikFormat) {
3870 case kFmt10t:
3871 case kFmt20t:
3872 case kFmt30t:
3873 notHandled = handleFmt10t_Fmt20t_Fmt30t(cUnit,
3874 mir, blockList[i], labelList);
3875 break;
3876 case kFmt10x:
3877 notHandled = handleFmt10x(cUnit, mir);
3878 break;
3879 case kFmt11n:
3880 case kFmt31i:
3881 notHandled = handleFmt11n_Fmt31i(cUnit, mir);
3882 break;
3883 case kFmt11x:
3884 notHandled = handleFmt11x(cUnit, mir);
3885 break;
3886 case kFmt12x:
3887 notHandled = handleFmt12x(cUnit, mir);
3888 break;
3889 case kFmt20bc:
3890 notHandled = handleFmt20bc(cUnit, mir);
3891 break;
3892 case kFmt21c:
3893 case kFmt31c:
3894 notHandled = handleFmt21c_Fmt31c(cUnit, mir);
3895 break;
3896 case kFmt21h:
3897 notHandled = handleFmt21h(cUnit, mir);
3898 break;
3899 case kFmt21s:
3900 notHandled = handleFmt21s(cUnit, mir);
3901 break;
3902 case kFmt21t:
3903 notHandled = handleFmt21t(cUnit, mir, blockList[i],
3904 labelList);
3905 break;
3906 case kFmt22b:
3907 case kFmt22s:
3908 notHandled = handleFmt22b_Fmt22s(cUnit, mir);
3909 break;
3910 case kFmt22c:
3911 notHandled = handleFmt22c(cUnit, mir);
3912 break;
3913 case kFmt22cs:
3914 notHandled = handleFmt22cs(cUnit, mir);
3915 break;
3916 case kFmt22t:
3917 notHandled = handleFmt22t(cUnit, mir, blockList[i],
3918 labelList);
3919 break;
3920 case kFmt22x:
3921 case kFmt32x:
3922 notHandled = handleFmt22x_Fmt32x(cUnit, mir);
3923 break;
3924 case kFmt23x:
3925 notHandled = handleFmt23x(cUnit, mir);
3926 break;
3927 case kFmt31t:
3928 notHandled = handleFmt31t(cUnit, mir);
3929 break;
3930 case kFmt3rc:
3931 case kFmt35c:
3932 notHandled = handleFmt35c_3rc(cUnit, mir, blockList[i],
3933 labelList);
3934 break;
3935 case kFmt3rms:
3936 case kFmt35ms:
3937 notHandled = handleFmt35ms_3rms(cUnit, mir,blockList[i],
3938 labelList);
3939 break;
3940 case kFmt3inline:
Andy McFaddenb0a05412009-11-19 10:23:41 -08003941 case kFmt3rinline:
Bill Buzbeece46c942009-11-20 15:41:34 -08003942 notHandled = handleExecuteInline(cUnit, mir);
Andy McFaddenb0a05412009-11-19 10:23:41 -08003943 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07003944 case kFmt51l:
3945 notHandled = handleFmt51l(cUnit, mir);
3946 break;
3947 default:
3948 notHandled = true;
3949 break;
3950 }
3951 }
3952 if (notHandled) {
3953 LOGE("%#06x: Opcode 0x%x (%s) / Fmt %d not handled\n",
3954 mir->offset,
3955 dalvikOpCode, getOpcodeName(dalvikOpCode),
3956 dalvikFormat);
Bill Buzbeefc519dc2010-03-06 23:30:57 -08003957 dvmCompilerAbort(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003958 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07003959 }
3960 }
Ben Cheng4238ec22009-08-24 16:32:22 -07003961
Bill Buzbee1465db52009-09-23 17:17:35 -07003962 if (blockList[i]->blockType == kEntryBlock) {
Ben Cheng4238ec22009-08-24 16:32:22 -07003963 dvmCompilerAppendLIR(cUnit,
3964 (LIR *) cUnit->loopAnalysis->branchToBody);
3965 dvmCompilerAppendLIR(cUnit,
3966 (LIR *) cUnit->loopAnalysis->branchToPCR);
3967 }
3968
3969 if (headLIR) {
3970 /*
3971 * Eliminate redundant loads/stores and delay stores into later
3972 * slots
3973 */
3974 dvmCompilerApplyLocalOptimizations(cUnit, (LIR *) headLIR,
3975 cUnit->lastLIRInsn);
3976 }
3977
3978gen_fallthrough:
Ben Cheng1efc9c52009-06-08 18:25:27 -07003979 /*
3980 * Check if the block is terminated due to trace length constraint -
3981 * insert an unconditional branch to the chaining cell.
3982 */
3983 if (blockList[i]->needFallThroughBranch) {
3984 genUnconditionalBranch(cUnit,
3985 &labelList[blockList[i]->fallThrough->id]);
3986 }
3987
Ben Chengba4fc8b2009-06-01 13:00:29 -07003988 }
3989
Ben Chenge9695e52009-06-16 16:11:47 -07003990 /* Handle the chaining cells in predefined order */
Ben Chengcec26f62010-01-15 15:29:33 -08003991 for (i = 0; i < kChainingCellGap; i++) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07003992 size_t j;
3993 int *blockIdList = (int *) chainingListByType[i].elemList;
3994
3995 cUnit->numChainingCells[i] = chainingListByType[i].numUsed;
3996
3997 /* No chaining cells of this type */
3998 if (cUnit->numChainingCells[i] == 0)
3999 continue;
4000
4001 /* Record the first LIR for a new type of chaining cell */
4002 cUnit->firstChainingLIR[i] = (LIR *) &labelList[blockIdList[0]];
4003
4004 for (j = 0; j < chainingListByType[i].numUsed; j++) {
4005 int blockId = blockIdList[j];
4006
4007 /* Align this chaining cell first */
Bill Buzbee1465db52009-09-23 17:17:35 -07004008 newLIR0(cUnit, kArmPseudoPseudoAlign4);
Ben Chengba4fc8b2009-06-01 13:00:29 -07004009
4010 /* Insert the pseudo chaining instruction */
4011 dvmCompilerAppendLIR(cUnit, (LIR *) &labelList[blockId]);
4012
4013
4014 switch (blockList[blockId]->blockType) {
Bill Buzbee1465db52009-09-23 17:17:35 -07004015 case kChainingCellNormal:
Ben Cheng1efc9c52009-06-08 18:25:27 -07004016 handleNormalChainingCell(cUnit,
Ben Chengba4fc8b2009-06-01 13:00:29 -07004017 blockList[blockId]->startOffset);
4018 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07004019 case kChainingCellInvokeSingleton:
Ben Cheng38329f52009-07-07 14:19:20 -07004020 handleInvokeSingletonChainingCell(cUnit,
Ben Chengba4fc8b2009-06-01 13:00:29 -07004021 blockList[blockId]->containingMethod);
4022 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07004023 case kChainingCellInvokePredicted:
Ben Cheng38329f52009-07-07 14:19:20 -07004024 handleInvokePredictedChainingCell(cUnit);
4025 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07004026 case kChainingCellHot:
Ben Cheng1efc9c52009-06-08 18:25:27 -07004027 handleHotChainingCell(cUnit,
Ben Chengba4fc8b2009-06-01 13:00:29 -07004028 blockList[blockId]->startOffset);
4029 break;
Bill Buzbee9c4b7c82009-09-10 10:10:38 -07004030#if defined(WITH_SELF_VERIFICATION) || defined(WITH_JIT_TUNING)
Bill Buzbee1465db52009-09-23 17:17:35 -07004031 case kChainingCellBackwardBranch:
Jeff Hao97319a82009-08-12 16:57:15 -07004032 handleBackwardBranchChainingCell(cUnit,
4033 blockList[blockId]->startOffset);
4034 break;
4035#endif
Ben Chengba4fc8b2009-06-01 13:00:29 -07004036 default:
Bill Buzbee1465db52009-09-23 17:17:35 -07004037 LOGE("Bad blocktype %d", blockList[blockId]->blockType);
Bill Buzbeefc519dc2010-03-06 23:30:57 -08004038 dvmCompilerAbort(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -07004039 }
4040 }
4041 }
Ben Chenge9695e52009-06-16 16:11:47 -07004042
Ben Chengcec26f62010-01-15 15:29:33 -08004043 /* Mark the bottom of chaining cells */
4044 cUnit->chainingCellBottom = (LIR *) newLIR0(cUnit, kArmChainingCellBottom);
4045
Ben Cheng6c10a972009-10-29 14:39:18 -07004046 /*
4047 * Generate the branch to the dvmJitToInterpNoChain entry point at the end
4048 * of all chaining cells for the overflow cases.
4049 */
4050 if (cUnit->switchOverflowPad) {
4051 loadConstant(cUnit, r0, (int) cUnit->switchOverflowPad);
4052 loadWordDisp(cUnit, rGLUE, offsetof(InterpState,
4053 jitToInterpEntries.dvmJitToInterpNoChain), r2);
4054 opRegReg(cUnit, kOpAdd, r1, r1);
4055 opRegRegReg(cUnit, kOpAdd, r4PC, r0, r1);
Ben Cheng978738d2010-05-13 13:45:57 -07004056#if defined(WITH_JIT_TUNING)
Ben Cheng6c10a972009-10-29 14:39:18 -07004057 loadConstant(cUnit, r0, kSwitchOverflow);
4058#endif
4059 opReg(cUnit, kOpBlx, r2);
4060 }
4061
Ben Chenge9695e52009-06-16 16:11:47 -07004062 dvmCompilerApplyGlobalOptimizations(cUnit);
jeffhao9e45c0b2010-02-03 10:24:05 -08004063
4064#if defined(WITH_SELF_VERIFICATION)
4065 selfVerificationBranchInsertPass(cUnit);
4066#endif
Ben Chengba4fc8b2009-06-01 13:00:29 -07004067}
4068
4069/* Accept the work and start compiling */
Bill Buzbee716f1202009-07-23 13:22:09 -07004070bool dvmCompilerDoWork(CompilerWorkOrder *work)
Ben Chengba4fc8b2009-06-01 13:00:29 -07004071{
Ben Chengccd6c012009-10-15 14:52:45 -07004072 bool res;
Ben Chengba4fc8b2009-06-01 13:00:29 -07004073
Ben Cheng6999d842010-01-26 16:46:15 -08004074 if (gDvmJit.codeCacheFull) {
Ben Chengccd6c012009-10-15 14:52:45 -07004075 return false;
4076 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07004077
Ben Chengccd6c012009-10-15 14:52:45 -07004078 switch (work->kind) {
4079 case kWorkOrderMethod:
4080 res = dvmCompileMethod(work->info, &work->result);
4081 break;
4082 case kWorkOrderTrace:
4083 /* Start compilation with maximally allowed trace length */
Bill Buzbeefc519dc2010-03-06 23:30:57 -08004084 res = dvmCompileTrace(work->info, JIT_MAX_TRACE_LEN, &work->result,
4085 work->bailPtr);
Ben Chengccd6c012009-10-15 14:52:45 -07004086 break;
4087 case kWorkOrderTraceDebug: {
4088 bool oldPrintMe = gDvmJit.printMe;
4089 gDvmJit.printMe = true;
4090 /* Start compilation with maximally allowed trace length */
Bill Buzbeefc519dc2010-03-06 23:30:57 -08004091 res = dvmCompileTrace(work->info, JIT_MAX_TRACE_LEN, &work->result,
4092 work->bailPtr);
Elliott Hughes672511b2010-04-26 17:40:13 -07004093 gDvmJit.printMe = oldPrintMe;
Ben Chengccd6c012009-10-15 14:52:45 -07004094 break;
4095 }
4096 default:
4097 res = false;
Bill Buzbeefc519dc2010-03-06 23:30:57 -08004098 LOGE("Jit: unknown work order type");
Elliott Hughes672511b2010-04-26 17:40:13 -07004099 assert(0); // Bail if debug build, discard otherwise
Ben Chengccd6c012009-10-15 14:52:45 -07004100 }
4101 return res;
Ben Chengba4fc8b2009-06-01 13:00:29 -07004102}
4103
Ben Chengba4fc8b2009-06-01 13:00:29 -07004104/* Architectural-specific debugging helpers go here */
4105void dvmCompilerArchDump(void)
4106{
4107 /* Print compiled opcode in this VM instance */
4108 int i, start, streak;
4109 char buf[1024];
4110
4111 streak = i = 0;
4112 buf[0] = 0;
4113 while (opcodeCoverage[i] == 0 && i < 256) {
4114 i++;
4115 }
4116 if (i == 256) {
4117 return;
4118 }
4119 for (start = i++, streak = 1; i < 256; i++) {
4120 if (opcodeCoverage[i]) {
4121 streak++;
4122 } else {
4123 if (streak == 1) {
4124 sprintf(buf+strlen(buf), "%x,", start);
4125 } else {
4126 sprintf(buf+strlen(buf), "%x-%x,", start, start + streak - 1);
4127 }
4128 streak = 0;
4129 while (opcodeCoverage[i] == 0 && i < 256) {
4130 i++;
4131 }
4132 if (i < 256) {
4133 streak = 1;
4134 start = i;
4135 }
4136 }
4137 }
4138 if (streak) {
4139 if (streak == 1) {
4140 sprintf(buf+strlen(buf), "%x", start);
4141 } else {
4142 sprintf(buf+strlen(buf), "%x-%x", start, start + streak - 1);
4143 }
4144 }
4145 if (strlen(buf)) {
Ben Cheng8b258bf2009-06-24 17:27:07 -07004146 LOGD("dalvik.vm.jit.op = %s", buf);
Ben Chengba4fc8b2009-06-01 13:00:29 -07004147 }
4148}
Ben Chengd7d426a2009-09-22 11:23:36 -07004149
4150/* Common initialization routine for an architecture family */
4151bool dvmCompilerArchInit()
4152{
4153 int i;
4154
Bill Buzbee1465db52009-09-23 17:17:35 -07004155 for (i = 0; i < kArmLast; i++) {
Ben Chengd7d426a2009-09-22 11:23:36 -07004156 if (EncodingMap[i].opCode != i) {
4157 LOGE("Encoding order for %s is wrong: expecting %d, seeing %d",
4158 EncodingMap[i].name, i, EncodingMap[i].opCode);
Bill Buzbeefc519dc2010-03-06 23:30:57 -08004159 dvmAbort(); // OK to dvmAbort - build error
Ben Chengd7d426a2009-09-22 11:23:36 -07004160 }
4161 }
4162
Ben Cheng5d90c202009-11-22 23:31:11 -08004163 return dvmCompilerArchVariantInit();
4164}
4165
4166void *dvmCompilerGetInterpretTemplate()
4167{
4168 return (void*) ((int)gDvmJit.codeCache +
4169 templateEntryOffsets[TEMPLATE_INTERPRET]);
4170}
4171
4172/* Needed by the ld/st optmizatons */
4173ArmLIR* dvmCompilerRegCopyNoInsert(CompilationUnit *cUnit, int rDest, int rSrc)
4174{
4175 return genRegCopyNoInsert(cUnit, rDest, rSrc);
4176}
4177
4178/* Needed by the register allocator */
4179ArmLIR* dvmCompilerRegCopy(CompilationUnit *cUnit, int rDest, int rSrc)
4180{
4181 return genRegCopy(cUnit, rDest, rSrc);
4182}
4183
4184/* Needed by the register allocator */
4185void dvmCompilerRegCopyWide(CompilationUnit *cUnit, int destLo, int destHi,
4186 int srcLo, int srcHi)
4187{
4188 genRegCopyWide(cUnit, destLo, destHi, srcLo, srcHi);
4189}
4190
4191void dvmCompilerFlushRegImpl(CompilationUnit *cUnit, int rBase,
4192 int displacement, int rSrc, OpSize size)
4193{
4194 storeBaseDisp(cUnit, rBase, displacement, rSrc, size);
4195}
4196
4197void dvmCompilerFlushRegWideImpl(CompilationUnit *cUnit, int rBase,
4198 int displacement, int rSrcLo, int rSrcHi)
4199{
4200 storeBaseDispWide(cUnit, rBase, displacement, rSrcLo, rSrcHi);
Ben Chengd7d426a2009-09-22 11:23:36 -07004201}