blob: 711431e7b94146d74c576dbe34dcaddae36931cd [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;
jeffhao9e45c0b2010-02-03 10:24:05 -0800200 TemplateOpCode opCode = TEMPLATE_MEM_OP_DECODE;
Jeff Hao97319a82009-08-12 16:57:15 -0700201
jeffhao9e45c0b2010-02-03 10:24:05 -0800202 for (thisLIR = (ArmLIR *) cUnit->firstLIRInsn;
203 thisLIR != (ArmLIR *) cUnit->lastLIRInsn;
204 thisLIR = NEXT_LIR(thisLIR)) {
205 if (thisLIR->branchInsertSV) {
206 /* Branch to mem op decode template */
207 selfVerificationBranchInsert((LIR *) thisLIR, kThumbBlx1,
208 (int) gDvmJit.codeCache + templateEntryOffsets[opCode],
209 (int) gDvmJit.codeCache + templateEntryOffsets[opCode]);
210 selfVerificationBranchInsert((LIR *) thisLIR, kThumbBlx2,
211 (int) gDvmJit.codeCache + templateEntryOffsets[opCode],
212 (int) gDvmJit.codeCache + templateEntryOffsets[opCode]);
Jeff Hao97319a82009-08-12 16:57:15 -0700213 }
214 }
Jeff Hao97319a82009-08-12 16:57:15 -0700215}
Jeff Hao97319a82009-08-12 16:57:15 -0700216#endif
217
Bill Buzbeebe6534f2010-03-12 16:01:35 -0800218/* Generate conditional branch instructions */
219static ArmLIR *genConditionalBranch(CompilationUnit *cUnit,
220 ArmConditionCode cond,
221 ArmLIR *target)
222{
223 ArmLIR *branch = opCondBranch(cUnit, cond);
224 branch->generic.target = (LIR *) target;
225 return branch;
226}
227
Ben Chengba4fc8b2009-06-01 13:00:29 -0700228/* Generate a unconditional branch to go to the interpreter */
Bill Buzbee89efc3d2009-07-28 11:22:22 -0700229static inline ArmLIR *genTrap(CompilationUnit *cUnit, int dOffset,
230 ArmLIR *pcrLabel)
Ben Chengba4fc8b2009-06-01 13:00:29 -0700231{
Bill Buzbee1465db52009-09-23 17:17:35 -0700232 ArmLIR *branch = opNone(cUnit, kOpUncondBr);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700233 return genCheckCommon(cUnit, dOffset, branch, pcrLabel);
234}
235
236/* Load a wide field from an object instance */
237static void genIGetWide(CompilationUnit *cUnit, MIR *mir, int fieldOffset)
238{
Bill Buzbeec6f10662010-02-09 11:16:15 -0800239 RegLocation rlObj = dvmCompilerGetSrc(cUnit, mir, 0);
240 RegLocation rlDest = dvmCompilerGetDestWide(cUnit, mir, 0, 1);
Bill Buzbee1465db52009-09-23 17:17:35 -0700241 RegLocation rlResult;
242 rlObj = loadValue(cUnit, rlObj, kCoreReg);
Bill Buzbeec6f10662010-02-09 11:16:15 -0800243 int regPtr = dvmCompilerAllocTemp(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700244
Bill Buzbee1465db52009-09-23 17:17:35 -0700245 assert(rlDest.wide);
Ben Chenge9695e52009-06-16 16:11:47 -0700246
Bill Buzbee1465db52009-09-23 17:17:35 -0700247 genNullCheck(cUnit, rlObj.sRegLow, rlObj.lowReg, mir->offset,
248 NULL);/* null object? */
249 opRegRegImm(cUnit, kOpAdd, regPtr, rlObj.lowReg, fieldOffset);
Bill Buzbeec6f10662010-02-09 11:16:15 -0800250 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kAnyReg, true);
Ben Cheng11d8f142010-03-24 15:24:19 -0700251
252 HEAP_ACCESS_SHADOW(true);
Bill Buzbee1465db52009-09-23 17:17:35 -0700253 loadPair(cUnit, regPtr, rlResult.lowReg, rlResult.highReg);
Ben Cheng11d8f142010-03-24 15:24:19 -0700254 HEAP_ACCESS_SHADOW(false);
255
Bill Buzbeec6f10662010-02-09 11:16:15 -0800256 dvmCompilerFreeTemp(cUnit, regPtr);
Bill Buzbee1465db52009-09-23 17:17:35 -0700257 storeValueWide(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700258}
259
260/* Store a wide field to an object instance */
261static void genIPutWide(CompilationUnit *cUnit, MIR *mir, int fieldOffset)
262{
Bill Buzbeec6f10662010-02-09 11:16:15 -0800263 RegLocation rlSrc = dvmCompilerGetSrcWide(cUnit, mir, 0, 1);
264 RegLocation rlObj = dvmCompilerGetSrc(cUnit, mir, 2);
Bill Buzbee1465db52009-09-23 17:17:35 -0700265 rlObj = loadValue(cUnit, rlObj, kCoreReg);
266 int regPtr;
267 rlSrc = loadValueWide(cUnit, rlSrc, kAnyReg);
268 genNullCheck(cUnit, rlObj.sRegLow, rlObj.lowReg, mir->offset,
269 NULL);/* null object? */
Bill Buzbeec6f10662010-02-09 11:16:15 -0800270 regPtr = dvmCompilerAllocTemp(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -0700271 opRegRegImm(cUnit, kOpAdd, regPtr, rlObj.lowReg, fieldOffset);
Ben Cheng11d8f142010-03-24 15:24:19 -0700272
273 HEAP_ACCESS_SHADOW(true);
Bill Buzbee1465db52009-09-23 17:17:35 -0700274 storePair(cUnit, regPtr, rlSrc.lowReg, rlSrc.highReg);
Ben Cheng11d8f142010-03-24 15:24:19 -0700275 HEAP_ACCESS_SHADOW(false);
276
Bill Buzbeec6f10662010-02-09 11:16:15 -0800277 dvmCompilerFreeTemp(cUnit, regPtr);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700278}
279
280/*
281 * Load a field from an object instance
282 *
Ben Chengba4fc8b2009-06-01 13:00:29 -0700283 */
Bill Buzbee270c1d62009-08-13 16:58:07 -0700284static void genIGet(CompilationUnit *cUnit, MIR *mir, OpSize size,
Ben Chengba4fc8b2009-06-01 13:00:29 -0700285 int fieldOffset)
286{
Bill Buzbee1465db52009-09-23 17:17:35 -0700287 RegLocation rlResult;
Bill Buzbeec6f10662010-02-09 11:16:15 -0800288 RegLocation rlObj = dvmCompilerGetSrc(cUnit, mir, 0);
289 RegLocation rlDest = dvmCompilerGetDest(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -0700290 rlObj = loadValue(cUnit, rlObj, kCoreReg);
Bill Buzbeec6f10662010-02-09 11:16:15 -0800291 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kAnyReg, true);
Bill Buzbee1465db52009-09-23 17:17:35 -0700292 genNullCheck(cUnit, rlObj.sRegLow, rlObj.lowReg, mir->offset,
293 NULL);/* null object? */
Ben Cheng11d8f142010-03-24 15:24:19 -0700294
295 HEAP_ACCESS_SHADOW(true);
Ben Cheng5d90c202009-11-22 23:31:11 -0800296 loadBaseDisp(cUnit, mir, rlObj.lowReg, fieldOffset, rlResult.lowReg,
297 size, rlObj.sRegLow);
Ben Cheng11d8f142010-03-24 15:24:19 -0700298 HEAP_ACCESS_SHADOW(false);
299
Bill Buzbee1465db52009-09-23 17:17:35 -0700300 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700301}
302
303/*
304 * Store a field to an object instance
305 *
Ben Chengba4fc8b2009-06-01 13:00:29 -0700306 */
Bill Buzbee270c1d62009-08-13 16:58:07 -0700307static void genIPut(CompilationUnit *cUnit, MIR *mir, OpSize size,
Ben Chengba4fc8b2009-06-01 13:00:29 -0700308 int fieldOffset)
309{
Bill Buzbeec6f10662010-02-09 11:16:15 -0800310 RegLocation rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
311 RegLocation rlObj = dvmCompilerGetSrc(cUnit, mir, 1);
Bill Buzbee1465db52009-09-23 17:17:35 -0700312 rlObj = loadValue(cUnit, rlObj, kCoreReg);
313 rlSrc = loadValue(cUnit, rlSrc, kAnyReg);
Bill Buzbee1465db52009-09-23 17:17:35 -0700314 genNullCheck(cUnit, rlObj.sRegLow, rlObj.lowReg, mir->offset,
315 NULL);/* null object? */
Ben Cheng11d8f142010-03-24 15:24:19 -0700316
317 HEAP_ACCESS_SHADOW(true);
Bill Buzbee1465db52009-09-23 17:17:35 -0700318 storeBaseDisp(cUnit, rlObj.lowReg, fieldOffset, rlSrc.lowReg, size);
Ben Cheng11d8f142010-03-24 15:24:19 -0700319 HEAP_ACCESS_SHADOW(false);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700320}
321
322
Ben Chengba4fc8b2009-06-01 13:00:29 -0700323/*
324 * Generate array load
Ben Chengba4fc8b2009-06-01 13:00:29 -0700325 */
Bill Buzbee270c1d62009-08-13 16:58:07 -0700326static void genArrayGet(CompilationUnit *cUnit, MIR *mir, OpSize size,
Bill Buzbee1465db52009-09-23 17:17:35 -0700327 RegLocation rlArray, RegLocation rlIndex,
328 RegLocation rlDest, int scale)
Ben Chengba4fc8b2009-06-01 13:00:29 -0700329{
330 int lenOffset = offsetof(ArrayObject, length);
331 int dataOffset = offsetof(ArrayObject, contents);
Bill Buzbee1465db52009-09-23 17:17:35 -0700332 RegLocation rlResult;
333 rlArray = loadValue(cUnit, rlArray, kCoreReg);
334 rlIndex = loadValue(cUnit, rlIndex, kCoreReg);
335 int regPtr;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700336
337 /* null object? */
Ben Cheng4238ec22009-08-24 16:32:22 -0700338 ArmLIR * pcrLabel = NULL;
339
340 if (!(mir->OptimizationFlags & MIR_IGNORE_NULL_CHECK)) {
Bill Buzbee1465db52009-09-23 17:17:35 -0700341 pcrLabel = genNullCheck(cUnit, rlArray.sRegLow,
342 rlArray.lowReg, mir->offset, NULL);
Ben Cheng4238ec22009-08-24 16:32:22 -0700343 }
344
Bill Buzbeec6f10662010-02-09 11:16:15 -0800345 regPtr = dvmCompilerAllocTemp(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -0700346
Ben Cheng4238ec22009-08-24 16:32:22 -0700347 if (!(mir->OptimizationFlags & MIR_IGNORE_RANGE_CHECK)) {
Bill Buzbeec6f10662010-02-09 11:16:15 -0800348 int regLen = dvmCompilerAllocTemp(cUnit);
Ben Cheng4238ec22009-08-24 16:32:22 -0700349 /* Get len */
Bill Buzbee1465db52009-09-23 17:17:35 -0700350 loadWordDisp(cUnit, rlArray.lowReg, lenOffset, regLen);
351 /* regPtr -> array data */
352 opRegRegImm(cUnit, kOpAdd, regPtr, rlArray.lowReg, dataOffset);
353 genBoundsCheck(cUnit, rlIndex.lowReg, regLen, mir->offset,
354 pcrLabel);
Bill Buzbeec6f10662010-02-09 11:16:15 -0800355 dvmCompilerFreeTemp(cUnit, regLen);
Ben Cheng4238ec22009-08-24 16:32:22 -0700356 } else {
Bill Buzbee1465db52009-09-23 17:17:35 -0700357 /* regPtr -> array data */
358 opRegRegImm(cUnit, kOpAdd, regPtr, rlArray.lowReg, dataOffset);
Ben Cheng4238ec22009-08-24 16:32:22 -0700359 }
Bill Buzbee1465db52009-09-23 17:17:35 -0700360 if ((size == kLong) || (size == kDouble)) {
361 if (scale) {
Bill Buzbeec6f10662010-02-09 11:16:15 -0800362 int rNewIndex = dvmCompilerAllocTemp(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -0700363 opRegRegImm(cUnit, kOpLsl, rNewIndex, rlIndex.lowReg, scale);
364 opRegReg(cUnit, kOpAdd, regPtr, rNewIndex);
Bill Buzbeec6f10662010-02-09 11:16:15 -0800365 dvmCompilerFreeTemp(cUnit, rNewIndex);
Bill Buzbee1465db52009-09-23 17:17:35 -0700366 } else {
367 opRegReg(cUnit, kOpAdd, regPtr, rlIndex.lowReg);
368 }
Bill Buzbeec6f10662010-02-09 11:16:15 -0800369 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kAnyReg, true);
Ben Cheng11d8f142010-03-24 15:24:19 -0700370
371 HEAP_ACCESS_SHADOW(true);
Bill Buzbee1465db52009-09-23 17:17:35 -0700372 loadPair(cUnit, regPtr, rlResult.lowReg, rlResult.highReg);
Ben Cheng11d8f142010-03-24 15:24:19 -0700373 HEAP_ACCESS_SHADOW(false);
374
Bill Buzbeec6f10662010-02-09 11:16:15 -0800375 dvmCompilerFreeTemp(cUnit, regPtr);
Bill Buzbee1465db52009-09-23 17:17:35 -0700376 storeValueWide(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700377 } else {
Bill Buzbeec6f10662010-02-09 11:16:15 -0800378 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kAnyReg, true);
Ben Cheng11d8f142010-03-24 15:24:19 -0700379
380 HEAP_ACCESS_SHADOW(true);
Bill Buzbee1465db52009-09-23 17:17:35 -0700381 loadBaseIndexed(cUnit, regPtr, rlIndex.lowReg, rlResult.lowReg,
382 scale, size);
Ben Cheng11d8f142010-03-24 15:24:19 -0700383 HEAP_ACCESS_SHADOW(false);
384
Bill Buzbeec6f10662010-02-09 11:16:15 -0800385 dvmCompilerFreeTemp(cUnit, regPtr);
Bill Buzbee1465db52009-09-23 17:17:35 -0700386 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700387 }
388}
389
Ben Chengba4fc8b2009-06-01 13:00:29 -0700390/*
391 * Generate array store
392 *
Ben Chengba4fc8b2009-06-01 13:00:29 -0700393 */
Bill Buzbee270c1d62009-08-13 16:58:07 -0700394static void genArrayPut(CompilationUnit *cUnit, MIR *mir, OpSize size,
Bill Buzbee1465db52009-09-23 17:17:35 -0700395 RegLocation rlArray, RegLocation rlIndex,
396 RegLocation rlSrc, int scale)
Ben Chengba4fc8b2009-06-01 13:00:29 -0700397{
398 int lenOffset = offsetof(ArrayObject, length);
399 int dataOffset = offsetof(ArrayObject, contents);
400
Bill Buzbee1465db52009-09-23 17:17:35 -0700401 int regPtr;
402 rlArray = loadValue(cUnit, rlArray, kCoreReg);
403 rlIndex = loadValue(cUnit, rlIndex, kCoreReg);
Ben Chenge9695e52009-06-16 16:11:47 -0700404
Bill Buzbeec6f10662010-02-09 11:16:15 -0800405 if (dvmCompilerIsTemp(cUnit, rlArray.lowReg)) {
406 dvmCompilerClobber(cUnit, rlArray.lowReg);
Bill Buzbee1465db52009-09-23 17:17:35 -0700407 regPtr = rlArray.lowReg;
408 } else {
Bill Buzbeec6f10662010-02-09 11:16:15 -0800409 regPtr = dvmCompilerAllocTemp(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -0700410 genRegCopy(cUnit, regPtr, rlArray.lowReg);
411 }
Ben Chenge9695e52009-06-16 16:11:47 -0700412
Ben Cheng1efc9c52009-06-08 18:25:27 -0700413 /* null object? */
Ben Cheng4238ec22009-08-24 16:32:22 -0700414 ArmLIR * pcrLabel = NULL;
415
416 if (!(mir->OptimizationFlags & MIR_IGNORE_NULL_CHECK)) {
Bill Buzbee1465db52009-09-23 17:17:35 -0700417 pcrLabel = genNullCheck(cUnit, rlArray.sRegLow, rlArray.lowReg,
418 mir->offset, NULL);
Ben Cheng4238ec22009-08-24 16:32:22 -0700419 }
420
421 if (!(mir->OptimizationFlags & MIR_IGNORE_RANGE_CHECK)) {
Bill Buzbeec6f10662010-02-09 11:16:15 -0800422 int regLen = dvmCompilerAllocTemp(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -0700423 //NOTE: max live temps(4) here.
Ben Cheng4238ec22009-08-24 16:32:22 -0700424 /* Get len */
Bill Buzbee1465db52009-09-23 17:17:35 -0700425 loadWordDisp(cUnit, rlArray.lowReg, lenOffset, regLen);
426 /* regPtr -> array data */
427 opRegImm(cUnit, kOpAdd, regPtr, dataOffset);
428 genBoundsCheck(cUnit, rlIndex.lowReg, regLen, mir->offset,
429 pcrLabel);
Bill Buzbeec6f10662010-02-09 11:16:15 -0800430 dvmCompilerFreeTemp(cUnit, regLen);
Ben Cheng4238ec22009-08-24 16:32:22 -0700431 } else {
Bill Buzbee1465db52009-09-23 17:17:35 -0700432 /* regPtr -> array data */
433 opRegImm(cUnit, kOpAdd, regPtr, dataOffset);
Ben Cheng4238ec22009-08-24 16:32:22 -0700434 }
Bill Buzbee1465db52009-09-23 17:17:35 -0700435 /* at this point, regPtr points to array, 2 live temps */
Bill Buzbee1465db52009-09-23 17:17:35 -0700436 if ((size == kLong) || (size == kDouble)) {
437 //TODO: need specific wide routine that can handle fp regs
438 if (scale) {
Bill Buzbeec6f10662010-02-09 11:16:15 -0800439 int rNewIndex = dvmCompilerAllocTemp(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -0700440 opRegRegImm(cUnit, kOpLsl, rNewIndex, rlIndex.lowReg, scale);
441 opRegReg(cUnit, kOpAdd, regPtr, rNewIndex);
Bill Buzbeec6f10662010-02-09 11:16:15 -0800442 dvmCompilerFreeTemp(cUnit, rNewIndex);
Bill Buzbee1465db52009-09-23 17:17:35 -0700443 } else {
444 opRegReg(cUnit, kOpAdd, regPtr, rlIndex.lowReg);
445 }
446 rlSrc = loadValueWide(cUnit, rlSrc, kAnyReg);
Ben Cheng11d8f142010-03-24 15:24:19 -0700447
448 HEAP_ACCESS_SHADOW(true);
Bill Buzbee1465db52009-09-23 17:17:35 -0700449 storePair(cUnit, regPtr, rlSrc.lowReg, rlSrc.highReg);
Ben Cheng11d8f142010-03-24 15:24:19 -0700450 HEAP_ACCESS_SHADOW(false);
451
Bill Buzbeec6f10662010-02-09 11:16:15 -0800452 dvmCompilerFreeTemp(cUnit, regPtr);
Bill Buzbee270c1d62009-08-13 16:58:07 -0700453 } else {
Bill Buzbee1465db52009-09-23 17:17:35 -0700454 rlSrc = loadValue(cUnit, rlSrc, kAnyReg);
Ben Cheng11d8f142010-03-24 15:24:19 -0700455
456 HEAP_ACCESS_SHADOW(true);
Bill Buzbee1465db52009-09-23 17:17:35 -0700457 storeBaseIndexed(cUnit, regPtr, rlIndex.lowReg, rlSrc.lowReg,
458 scale, size);
Ben Cheng11d8f142010-03-24 15:24:19 -0700459 HEAP_ACCESS_SHADOW(false);
jeffhao9e45c0b2010-02-03 10:24:05 -0800460 }
Ben Chengba4fc8b2009-06-01 13:00:29 -0700461}
462
Bill Buzbeebe6534f2010-03-12 16:01:35 -0800463/*
464 * Generate array object store
465 * Must use explicit register allocation here because of
466 * call-out to dvmCanPutArrayElement
467 */
468static void genArrayObjectPut(CompilationUnit *cUnit, MIR *mir,
469 RegLocation rlArray, RegLocation rlIndex,
470 RegLocation rlSrc, int scale)
471{
472 int lenOffset = offsetof(ArrayObject, length);
473 int dataOffset = offsetof(ArrayObject, contents);
474
475 dvmCompilerFlushAllRegs(cUnit);
476
477 int regLen = r0;
478 int regPtr = r4PC; /* Preserved across call */
479 int regArray = r1;
480 int regIndex = r7; /* Preserved across call */
481
482 loadValueDirectFixed(cUnit, rlArray, regArray);
483 loadValueDirectFixed(cUnit, rlIndex, regIndex);
484
485 /* null object? */
486 ArmLIR * pcrLabel = NULL;
487
488 if (!(mir->OptimizationFlags & MIR_IGNORE_NULL_CHECK)) {
489 pcrLabel = genNullCheck(cUnit, rlArray.sRegLow, regArray,
490 mir->offset, NULL);
491 }
492
493 if (!(mir->OptimizationFlags & MIR_IGNORE_RANGE_CHECK)) {
494 /* Get len */
495 loadWordDisp(cUnit, regArray, lenOffset, regLen);
496 /* regPtr -> array data */
497 opRegRegImm(cUnit, kOpAdd, regPtr, regArray, dataOffset);
498 genBoundsCheck(cUnit, regIndex, regLen, mir->offset,
499 pcrLabel);
500 } else {
501 /* regPtr -> array data */
502 opRegRegImm(cUnit, kOpAdd, regPtr, regArray, dataOffset);
503 }
504
505 /* Get object to store */
506 loadValueDirectFixed(cUnit, rlSrc, r0);
Ben Chengbd1326d2010-04-02 15:04:53 -0700507 LOAD_FUNC_ADDR(cUnit, r2, (int)dvmCanPutArrayElement);
Bill Buzbeebe6534f2010-03-12 16:01:35 -0800508
509 /* Are we storing null? If so, avoid check */
510 opRegImm(cUnit, kOpCmp, r0, 0);
511 ArmLIR *branchOver = opCondBranch(cUnit, kArmCondEq);
512
513 /* Make sure the types are compatible */
514 loadWordDisp(cUnit, regArray, offsetof(Object, clazz), r1);
515 loadWordDisp(cUnit, r0, offsetof(Object, clazz), r0);
516 opReg(cUnit, kOpBlx, r2);
517 dvmCompilerClobberCallRegs(cUnit);
Bill Buzbee900a3af2010-03-16 12:41:43 -0700518
519 /*
520 * Using fixed registers here, and counting on r4 and r7 being
521 * preserved across the above call. Tell the register allocation
522 * utilities about the regs we are using directly
523 */
524 dvmCompilerLockTemp(cUnit, regPtr); // r4PC
525 dvmCompilerLockTemp(cUnit, regIndex); // r7
526 dvmCompilerLockTemp(cUnit, r0);
527
Bill Buzbeebe6534f2010-03-12 16:01:35 -0800528 /* Bad? - roll back and re-execute if so */
529 genRegImmCheck(cUnit, kArmCondEq, r0, 0, mir->offset, pcrLabel);
530
531 /* Resume here - must reload element, regPtr & index preserved */
532 loadValueDirectFixed(cUnit, rlSrc, r0);
533
534 ArmLIR *target = newLIR0(cUnit, kArmPseudoTargetLabel);
535 target->defMask = ENCODE_ALL;
536 branchOver->generic.target = (LIR *) target;
537
Ben Cheng11d8f142010-03-24 15:24:19 -0700538 HEAP_ACCESS_SHADOW(true);
Bill Buzbeebe6534f2010-03-12 16:01:35 -0800539 storeBaseIndexed(cUnit, regPtr, regIndex, r0,
540 scale, kWord);
Ben Cheng11d8f142010-03-24 15:24:19 -0700541 HEAP_ACCESS_SHADOW(false);
Bill Buzbeebe6534f2010-03-12 16:01:35 -0800542}
543
Ben Cheng5d90c202009-11-22 23:31:11 -0800544static bool genShiftOpLong(CompilationUnit *cUnit, MIR *mir,
545 RegLocation rlDest, RegLocation rlSrc1,
546 RegLocation rlShift)
Ben Chengba4fc8b2009-06-01 13:00:29 -0700547{
Ben Chenge9695e52009-06-16 16:11:47 -0700548 /*
549 * Don't mess with the regsiters here as there is a particular calling
550 * convention to the out-of-line handler.
551 */
Bill Buzbee1465db52009-09-23 17:17:35 -0700552 RegLocation rlResult;
553
554 loadValueDirectWideFixed(cUnit, rlSrc1, r0, r1);
555 loadValueDirect(cUnit, rlShift, r2);
Ben Chenge9695e52009-06-16 16:11:47 -0700556 switch( mir->dalvikInsn.opCode) {
557 case OP_SHL_LONG:
558 case OP_SHL_LONG_2ADDR:
559 genDispatchToHandler(cUnit, TEMPLATE_SHL_LONG);
560 break;
561 case OP_SHR_LONG:
562 case OP_SHR_LONG_2ADDR:
563 genDispatchToHandler(cUnit, TEMPLATE_SHR_LONG);
564 break;
565 case OP_USHR_LONG:
566 case OP_USHR_LONG_2ADDR:
567 genDispatchToHandler(cUnit, TEMPLATE_USHR_LONG);
568 break;
569 default:
570 return true;
571 }
Bill Buzbeec6f10662010-02-09 11:16:15 -0800572 rlResult = dvmCompilerGetReturnWide(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -0700573 storeValueWide(cUnit, rlDest, rlResult);
Ben Chenge9695e52009-06-16 16:11:47 -0700574 return false;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700575}
Ben Chenge9695e52009-06-16 16:11:47 -0700576
Ben Cheng5d90c202009-11-22 23:31:11 -0800577static bool genArithOpLong(CompilationUnit *cUnit, MIR *mir,
578 RegLocation rlDest, RegLocation rlSrc1,
579 RegLocation rlSrc2)
Ben Chengba4fc8b2009-06-01 13:00:29 -0700580{
Bill Buzbee1465db52009-09-23 17:17:35 -0700581 RegLocation rlResult;
582 OpKind firstOp = kOpBkpt;
583 OpKind secondOp = kOpBkpt;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700584 bool callOut = false;
585 void *callTgt;
586 int retReg = r0;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700587
588 switch (mir->dalvikInsn.opCode) {
589 case OP_NOT_LONG:
Bill Buzbee1465db52009-09-23 17:17:35 -0700590 rlSrc2 = loadValueWide(cUnit, rlSrc2, kCoreReg);
Bill Buzbeec6f10662010-02-09 11:16:15 -0800591 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Bill Buzbee1465db52009-09-23 17:17:35 -0700592 opRegReg(cUnit, kOpMvn, rlResult.lowReg, rlSrc2.lowReg);
593 opRegReg(cUnit, kOpMvn, rlResult.highReg, rlSrc2.highReg);
594 storeValueWide(cUnit, rlDest, rlResult);
595 return false;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700596 break;
597 case OP_ADD_LONG:
598 case OP_ADD_LONG_2ADDR:
Bill Buzbee1465db52009-09-23 17:17:35 -0700599 firstOp = kOpAdd;
600 secondOp = kOpAdc;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700601 break;
602 case OP_SUB_LONG:
603 case OP_SUB_LONG_2ADDR:
Bill Buzbee1465db52009-09-23 17:17:35 -0700604 firstOp = kOpSub;
605 secondOp = kOpSbc;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700606 break;
607 case OP_MUL_LONG:
608 case OP_MUL_LONG_2ADDR:
Bill Buzbee1465db52009-09-23 17:17:35 -0700609 genMulLong(cUnit, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700610 return false;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700611 case OP_DIV_LONG:
612 case OP_DIV_LONG_2ADDR:
613 callOut = true;
614 retReg = r0;
615 callTgt = (void*)__aeabi_ldivmod;
616 break;
617 /* NOTE - result is in r2/r3 instead of r0/r1 */
618 case OP_REM_LONG:
619 case OP_REM_LONG_2ADDR:
620 callOut = true;
621 callTgt = (void*)__aeabi_ldivmod;
622 retReg = r2;
623 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700624 case OP_AND_LONG_2ADDR:
Bill Buzbee1465db52009-09-23 17:17:35 -0700625 case OP_AND_LONG:
626 firstOp = kOpAnd;
627 secondOp = kOpAnd;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700628 break;
629 case OP_OR_LONG:
630 case OP_OR_LONG_2ADDR:
Bill Buzbee1465db52009-09-23 17:17:35 -0700631 firstOp = kOpOr;
632 secondOp = kOpOr;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700633 break;
634 case OP_XOR_LONG:
635 case OP_XOR_LONG_2ADDR:
Bill Buzbee1465db52009-09-23 17:17:35 -0700636 firstOp = kOpXor;
637 secondOp = kOpXor;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700638 break;
Ben Chenge9695e52009-06-16 16:11:47 -0700639 case OP_NEG_LONG: {
Bill Buzbee51ecf602010-01-14 14:27:52 -0800640 //TUNING: can improve this using Thumb2 code
Bill Buzbeec6f10662010-02-09 11:16:15 -0800641 int tReg = dvmCompilerAllocTemp(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -0700642 rlSrc2 = loadValueWide(cUnit, rlSrc2, kCoreReg);
Bill Buzbeec6f10662010-02-09 11:16:15 -0800643 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Ben Chengbd1326d2010-04-02 15:04:53 -0700644 loadConstantNoClobber(cUnit, tReg, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -0700645 opRegRegReg(cUnit, kOpSub, rlResult.lowReg,
Bill Buzbee51ecf602010-01-14 14:27:52 -0800646 tReg, rlSrc2.lowReg);
647 opRegReg(cUnit, kOpSbc, tReg, rlSrc2.highReg);
648 genRegCopy(cUnit, rlResult.highReg, tReg);
Bill Buzbee1465db52009-09-23 17:17:35 -0700649 storeValueWide(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700650 return false;
Ben Chenge9695e52009-06-16 16:11:47 -0700651 }
Ben Chengba4fc8b2009-06-01 13:00:29 -0700652 default:
653 LOGE("Invalid long arith op");
Bill Buzbeefc519dc2010-03-06 23:30:57 -0800654 dvmCompilerAbort(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700655 }
656 if (!callOut) {
Bill Buzbee80cef862010-03-25 10:38:34 -0700657 genLong3Addr(cUnit, mir, firstOp, secondOp, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700658 } else {
Bill Buzbee1465db52009-09-23 17:17:35 -0700659 // Adjust return regs in to handle case of rem returning r2/r3
Bill Buzbeec6f10662010-02-09 11:16:15 -0800660 dvmCompilerFlushAllRegs(cUnit); /* Send everything to home location */
Bill Buzbee1465db52009-09-23 17:17:35 -0700661 loadValueDirectWideFixed(cUnit, rlSrc1, r0, r1);
Ben Chengbd1326d2010-04-02 15:04:53 -0700662 LOAD_FUNC_ADDR(cUnit, rlr, (int) callTgt);
Bill Buzbee1465db52009-09-23 17:17:35 -0700663 loadValueDirectWideFixed(cUnit, rlSrc2, r2, r3);
664 opReg(cUnit, kOpBlx, rlr);
Elliott Hughes6a555132010-02-25 15:41:42 -0800665 dvmCompilerClobberCallRegs(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -0700666 if (retReg == r0)
Bill Buzbeec6f10662010-02-09 11:16:15 -0800667 rlResult = dvmCompilerGetReturnWide(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -0700668 else
Bill Buzbeec6f10662010-02-09 11:16:15 -0800669 rlResult = dvmCompilerGetReturnWideAlt(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -0700670 storeValueWide(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700671 }
672 return false;
673}
674
Ben Cheng5d90c202009-11-22 23:31:11 -0800675static bool genArithOpInt(CompilationUnit *cUnit, MIR *mir,
676 RegLocation rlDest, RegLocation rlSrc1,
677 RegLocation rlSrc2)
Ben Chengba4fc8b2009-06-01 13:00:29 -0700678{
Bill Buzbee1465db52009-09-23 17:17:35 -0700679 OpKind op = kOpBkpt;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700680 bool callOut = false;
681 bool checkZero = false;
Bill Buzbee1465db52009-09-23 17:17:35 -0700682 bool unary = false;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700683 int retReg = r0;
684 void *callTgt;
Bill Buzbee1465db52009-09-23 17:17:35 -0700685 RegLocation rlResult;
Bill Buzbee0e605272009-12-01 14:28:05 -0800686 bool shiftOp = false;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700687
Ben Chengba4fc8b2009-06-01 13:00:29 -0700688 switch (mir->dalvikInsn.opCode) {
689 case OP_NEG_INT:
Bill Buzbee1465db52009-09-23 17:17:35 -0700690 op = kOpNeg;
691 unary = true;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700692 break;
693 case OP_NOT_INT:
Bill Buzbee1465db52009-09-23 17:17:35 -0700694 op = kOpMvn;
695 unary = true;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700696 break;
697 case OP_ADD_INT:
698 case OP_ADD_INT_2ADDR:
Bill Buzbee1465db52009-09-23 17:17:35 -0700699 op = kOpAdd;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700700 break;
701 case OP_SUB_INT:
702 case OP_SUB_INT_2ADDR:
Bill Buzbee1465db52009-09-23 17:17:35 -0700703 op = kOpSub;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700704 break;
705 case OP_MUL_INT:
706 case OP_MUL_INT_2ADDR:
Bill Buzbee1465db52009-09-23 17:17:35 -0700707 op = kOpMul;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700708 break;
709 case OP_DIV_INT:
710 case OP_DIV_INT_2ADDR:
711 callOut = true;
712 checkZero = true;
713 callTgt = __aeabi_idiv;
714 retReg = r0;
715 break;
716 /* NOTE: returns in r1 */
717 case OP_REM_INT:
718 case OP_REM_INT_2ADDR:
719 callOut = true;
720 checkZero = true;
721 callTgt = __aeabi_idivmod;
722 retReg = r1;
723 break;
724 case OP_AND_INT:
725 case OP_AND_INT_2ADDR:
Bill Buzbee1465db52009-09-23 17:17:35 -0700726 op = kOpAnd;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700727 break;
728 case OP_OR_INT:
729 case OP_OR_INT_2ADDR:
Bill Buzbee1465db52009-09-23 17:17:35 -0700730 op = kOpOr;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700731 break;
732 case OP_XOR_INT:
733 case OP_XOR_INT_2ADDR:
Bill Buzbee1465db52009-09-23 17:17:35 -0700734 op = kOpXor;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700735 break;
736 case OP_SHL_INT:
737 case OP_SHL_INT_2ADDR:
Bill Buzbee0e605272009-12-01 14:28:05 -0800738 shiftOp = true;
Bill Buzbee1465db52009-09-23 17:17:35 -0700739 op = kOpLsl;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700740 break;
741 case OP_SHR_INT:
742 case OP_SHR_INT_2ADDR:
Bill Buzbee0e605272009-12-01 14:28:05 -0800743 shiftOp = true;
Bill Buzbee1465db52009-09-23 17:17:35 -0700744 op = kOpAsr;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700745 break;
746 case OP_USHR_INT:
747 case OP_USHR_INT_2ADDR:
Bill Buzbee0e605272009-12-01 14:28:05 -0800748 shiftOp = true;
Bill Buzbee1465db52009-09-23 17:17:35 -0700749 op = kOpLsr;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700750 break;
751 default:
752 LOGE("Invalid word arith op: 0x%x(%d)",
753 mir->dalvikInsn.opCode, mir->dalvikInsn.opCode);
Bill Buzbeefc519dc2010-03-06 23:30:57 -0800754 dvmCompilerAbort(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700755 }
756 if (!callOut) {
Bill Buzbee1465db52009-09-23 17:17:35 -0700757 rlSrc1 = loadValue(cUnit, rlSrc1, kCoreReg);
758 if (unary) {
Bill Buzbeec6f10662010-02-09 11:16:15 -0800759 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Bill Buzbee1465db52009-09-23 17:17:35 -0700760 opRegReg(cUnit, op, rlResult.lowReg,
761 rlSrc1.lowReg);
Ben Chenge9695e52009-06-16 16:11:47 -0700762 } else {
Bill Buzbee1465db52009-09-23 17:17:35 -0700763 rlSrc2 = loadValue(cUnit, rlSrc2, kCoreReg);
Bill Buzbee0e605272009-12-01 14:28:05 -0800764 if (shiftOp) {
Bill Buzbeec6f10662010-02-09 11:16:15 -0800765 int tReg = dvmCompilerAllocTemp(cUnit);
Bill Buzbee0e605272009-12-01 14:28:05 -0800766 opRegRegImm(cUnit, kOpAnd, tReg, rlSrc2.lowReg, 31);
Bill Buzbeec6f10662010-02-09 11:16:15 -0800767 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Bill Buzbee0e605272009-12-01 14:28:05 -0800768 opRegRegReg(cUnit, op, rlResult.lowReg,
769 rlSrc1.lowReg, tReg);
Bill Buzbeec6f10662010-02-09 11:16:15 -0800770 dvmCompilerFreeTemp(cUnit, tReg);
Bill Buzbee0e605272009-12-01 14:28:05 -0800771 } else {
Bill Buzbeec6f10662010-02-09 11:16:15 -0800772 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Bill Buzbee0e605272009-12-01 14:28:05 -0800773 opRegRegReg(cUnit, op, rlResult.lowReg,
774 rlSrc1.lowReg, rlSrc2.lowReg);
775 }
Ben Chenge9695e52009-06-16 16:11:47 -0700776 }
Bill Buzbee1465db52009-09-23 17:17:35 -0700777 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700778 } else {
Bill Buzbee1465db52009-09-23 17:17:35 -0700779 RegLocation rlResult;
Bill Buzbeec6f10662010-02-09 11:16:15 -0800780 dvmCompilerFlushAllRegs(cUnit); /* Send everything to home location */
Bill Buzbee1465db52009-09-23 17:17:35 -0700781 loadValueDirectFixed(cUnit, rlSrc2, r1);
Ben Chengbd1326d2010-04-02 15:04:53 -0700782 LOAD_FUNC_ADDR(cUnit, r2, (int) callTgt);
Bill Buzbee1465db52009-09-23 17:17:35 -0700783 loadValueDirectFixed(cUnit, rlSrc1, r0);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700784 if (checkZero) {
Bill Buzbee1465db52009-09-23 17:17:35 -0700785 genNullCheck(cUnit, rlSrc2.sRegLow, r1, mir->offset, NULL);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700786 }
Bill Buzbee1465db52009-09-23 17:17:35 -0700787 opReg(cUnit, kOpBlx, r2);
Elliott Hughes6a555132010-02-25 15:41:42 -0800788 dvmCompilerClobberCallRegs(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -0700789 if (retReg == r0)
Bill Buzbeec6f10662010-02-09 11:16:15 -0800790 rlResult = dvmCompilerGetReturn(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -0700791 else
Bill Buzbeec6f10662010-02-09 11:16:15 -0800792 rlResult = dvmCompilerGetReturnAlt(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -0700793 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700794 }
795 return false;
796}
797
Ben Cheng5d90c202009-11-22 23:31:11 -0800798static bool genArithOp(CompilationUnit *cUnit, MIR *mir)
Ben Chengba4fc8b2009-06-01 13:00:29 -0700799{
800 OpCode opCode = mir->dalvikInsn.opCode;
Bill Buzbee1465db52009-09-23 17:17:35 -0700801 RegLocation rlDest;
802 RegLocation rlSrc1;
803 RegLocation rlSrc2;
804 /* Deduce sizes of operands */
805 if (mir->ssaRep->numUses == 2) {
Bill Buzbeec6f10662010-02-09 11:16:15 -0800806 rlSrc1 = dvmCompilerGetSrc(cUnit, mir, 0);
807 rlSrc2 = dvmCompilerGetSrc(cUnit, mir, 1);
Bill Buzbee1465db52009-09-23 17:17:35 -0700808 } else if (mir->ssaRep->numUses == 3) {
Bill Buzbeec6f10662010-02-09 11:16:15 -0800809 rlSrc1 = dvmCompilerGetSrcWide(cUnit, mir, 0, 1);
810 rlSrc2 = dvmCompilerGetSrc(cUnit, mir, 2);
Bill Buzbee1465db52009-09-23 17:17:35 -0700811 } else {
Bill Buzbeec6f10662010-02-09 11:16:15 -0800812 rlSrc1 = dvmCompilerGetSrcWide(cUnit, mir, 0, 1);
813 rlSrc2 = dvmCompilerGetSrcWide(cUnit, mir, 2, 3);
Bill Buzbee1465db52009-09-23 17:17:35 -0700814 assert(mir->ssaRep->numUses == 4);
815 }
816 if (mir->ssaRep->numDefs == 1) {
Bill Buzbeec6f10662010-02-09 11:16:15 -0800817 rlDest = dvmCompilerGetDest(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -0700818 } else {
819 assert(mir->ssaRep->numDefs == 2);
Bill Buzbeec6f10662010-02-09 11:16:15 -0800820 rlDest = dvmCompilerGetDestWide(cUnit, mir, 0, 1);
Bill Buzbee1465db52009-09-23 17:17:35 -0700821 }
Ben Chengba4fc8b2009-06-01 13:00:29 -0700822
823 if ((opCode >= OP_ADD_LONG_2ADDR) && (opCode <= OP_XOR_LONG_2ADDR)) {
Ben Cheng5d90c202009-11-22 23:31:11 -0800824 return genArithOpLong(cUnit,mir, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700825 }
826 if ((opCode >= OP_ADD_LONG) && (opCode <= OP_XOR_LONG)) {
Ben Cheng5d90c202009-11-22 23:31:11 -0800827 return genArithOpLong(cUnit,mir, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700828 }
829 if ((opCode >= OP_SHL_LONG_2ADDR) && (opCode <= OP_USHR_LONG_2ADDR)) {
Ben Cheng5d90c202009-11-22 23:31:11 -0800830 return genShiftOpLong(cUnit,mir, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700831 }
832 if ((opCode >= OP_SHL_LONG) && (opCode <= OP_USHR_LONG)) {
Ben Cheng5d90c202009-11-22 23:31:11 -0800833 return genShiftOpLong(cUnit,mir, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700834 }
835 if ((opCode >= OP_ADD_INT_2ADDR) && (opCode <= OP_USHR_INT_2ADDR)) {
Ben Cheng5d90c202009-11-22 23:31:11 -0800836 return genArithOpInt(cUnit,mir, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700837 }
838 if ((opCode >= OP_ADD_INT) && (opCode <= OP_USHR_INT)) {
Ben Cheng5d90c202009-11-22 23:31:11 -0800839 return genArithOpInt(cUnit,mir, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700840 }
841 if ((opCode >= OP_ADD_FLOAT_2ADDR) && (opCode <= OP_REM_FLOAT_2ADDR)) {
Ben Cheng5d90c202009-11-22 23:31:11 -0800842 return genArithOpFloat(cUnit,mir, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700843 }
844 if ((opCode >= OP_ADD_FLOAT) && (opCode <= OP_REM_FLOAT)) {
Ben Cheng5d90c202009-11-22 23:31:11 -0800845 return genArithOpFloat(cUnit, mir, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700846 }
847 if ((opCode >= OP_ADD_DOUBLE_2ADDR) && (opCode <= OP_REM_DOUBLE_2ADDR)) {
Ben Cheng5d90c202009-11-22 23:31:11 -0800848 return genArithOpDouble(cUnit,mir, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700849 }
850 if ((opCode >= OP_ADD_DOUBLE) && (opCode <= OP_REM_DOUBLE)) {
Ben Cheng5d90c202009-11-22 23:31:11 -0800851 return genArithOpDouble(cUnit,mir, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700852 }
853 return true;
854}
855
Bill Buzbee1465db52009-09-23 17:17:35 -0700856/* Generate unconditional branch instructions */
857static ArmLIR *genUnconditionalBranch(CompilationUnit *cUnit, ArmLIR *target)
858{
859 ArmLIR *branch = opNone(cUnit, kOpUncondBr);
860 branch->generic.target = (LIR *) target;
861 return branch;
862}
863
Bill Buzbee1465db52009-09-23 17:17:35 -0700864/* Perform the actual operation for OP_RETURN_* */
865static void genReturnCommon(CompilationUnit *cUnit, MIR *mir)
866{
867 genDispatchToHandler(cUnit, TEMPLATE_RETURN);
Ben Cheng978738d2010-05-13 13:45:57 -0700868#if defined(WITH_JIT_TUNING)
Bill Buzbee1465db52009-09-23 17:17:35 -0700869 gDvmJit.returnOp++;
870#endif
871 int dPC = (int) (cUnit->method->insns + mir->offset);
872 /* Insert branch, but defer setting of target */
873 ArmLIR *branch = genUnconditionalBranch(cUnit, NULL);
874 /* Set up the place holder to reconstruct this Dalvik PC */
875 ArmLIR *pcrLabel = dvmCompilerNew(sizeof(ArmLIR), true);
Ben Chenga4973592010-03-31 11:59:18 -0700876 pcrLabel->opCode = kArmPseudoPCReconstructionCell;
Bill Buzbee1465db52009-09-23 17:17:35 -0700877 pcrLabel->operands[0] = dPC;
878 pcrLabel->operands[1] = mir->offset;
879 /* Insert the place holder to the growable list */
880 dvmInsertGrowableList(&cUnit->pcReconstructionList, pcrLabel);
881 /* Branch to the PC reconstruction code */
882 branch->generic.target = (LIR *) pcrLabel;
883}
884
Ben Chengba4fc8b2009-06-01 13:00:29 -0700885static void genProcessArgsNoRange(CompilationUnit *cUnit, MIR *mir,
886 DecodedInstruction *dInsn,
Bill Buzbee89efc3d2009-07-28 11:22:22 -0700887 ArmLIR **pcrLabel)
Ben Chengba4fc8b2009-06-01 13:00:29 -0700888{
889 unsigned int i;
890 unsigned int regMask = 0;
Bill Buzbee1465db52009-09-23 17:17:35 -0700891 RegLocation rlArg;
892 int numDone = 0;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700893
Bill Buzbee1465db52009-09-23 17:17:35 -0700894 /*
895 * Load arguments to r0..r4. Note that these registers may contain
896 * live values, so we clobber them immediately after loading to prevent
897 * them from being used as sources for subsequent loads.
898 */
Bill Buzbeec6f10662010-02-09 11:16:15 -0800899 dvmCompilerLockAllTemps(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700900 for (i = 0; i < dInsn->vA; i++) {
901 regMask |= 1 << i;
Bill Buzbeec6f10662010-02-09 11:16:15 -0800902 rlArg = dvmCompilerGetSrc(cUnit, mir, numDone++);
Bill Buzbee1465db52009-09-23 17:17:35 -0700903 loadValueDirectFixed(cUnit, rlArg, i);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700904 }
905 if (regMask) {
906 /* Up to 5 args are pushed on top of FP - sizeofStackSaveArea */
Bill Buzbee1465db52009-09-23 17:17:35 -0700907 opRegRegImm(cUnit, kOpSub, r7, rFP,
908 sizeof(StackSaveArea) + (dInsn->vA << 2));
Ben Chengba4fc8b2009-06-01 13:00:29 -0700909 /* generate null check */
910 if (pcrLabel) {
Bill Buzbeec6f10662010-02-09 11:16:15 -0800911 *pcrLabel = genNullCheck(cUnit, dvmCompilerSSASrc(mir, 0), r0,
Bill Buzbee1465db52009-09-23 17:17:35 -0700912 mir->offset, NULL);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700913 }
Bill Buzbee270c1d62009-08-13 16:58:07 -0700914 storeMultiple(cUnit, r7, regMask);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700915 }
916}
917
918static void genProcessArgsRange(CompilationUnit *cUnit, MIR *mir,
919 DecodedInstruction *dInsn,
Bill Buzbee89efc3d2009-07-28 11:22:22 -0700920 ArmLIR **pcrLabel)
Ben Chengba4fc8b2009-06-01 13:00:29 -0700921{
922 int srcOffset = dInsn->vC << 2;
923 int numArgs = dInsn->vA;
924 int regMask;
Bill Buzbee1465db52009-09-23 17:17:35 -0700925
926 /*
927 * Note: here, all promoted registers will have been flushed
928 * back to the Dalvik base locations, so register usage restrictins
929 * are lifted. All parms loaded from original Dalvik register
930 * region - even though some might conceivably have valid copies
931 * cached in a preserved register.
932 */
Bill Buzbeec6f10662010-02-09 11:16:15 -0800933 dvmCompilerLockAllTemps(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -0700934
Ben Chengba4fc8b2009-06-01 13:00:29 -0700935 /*
936 * r4PC : &rFP[vC]
937 * r7: &newFP[0]
938 */
Bill Buzbee1465db52009-09-23 17:17:35 -0700939 opRegRegImm(cUnit, kOpAdd, r4PC, rFP, srcOffset);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700940 /* load [r0 .. min(numArgs,4)] */
941 regMask = (1 << ((numArgs < 4) ? numArgs : 4)) - 1;
Ben Chengd7d426a2009-09-22 11:23:36 -0700942 /*
943 * Protect the loadMultiple instruction from being reordered with other
944 * Dalvik stack accesses.
945 */
Bill Buzbee270c1d62009-08-13 16:58:07 -0700946 loadMultiple(cUnit, r4PC, regMask);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700947
Bill Buzbee1465db52009-09-23 17:17:35 -0700948 opRegRegImm(cUnit, kOpSub, r7, rFP,
949 sizeof(StackSaveArea) + (numArgs << 2));
Ben Chengba4fc8b2009-06-01 13:00:29 -0700950 /* generate null check */
951 if (pcrLabel) {
Bill Buzbeec6f10662010-02-09 11:16:15 -0800952 *pcrLabel = genNullCheck(cUnit, dvmCompilerSSASrc(mir, 0), r0,
Bill Buzbee1465db52009-09-23 17:17:35 -0700953 mir->offset, NULL);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700954 }
955
956 /*
957 * Handle remaining 4n arguments:
958 * store previously loaded 4 values and load the next 4 values
959 */
960 if (numArgs >= 8) {
Bill Buzbee89efc3d2009-07-28 11:22:22 -0700961 ArmLIR *loopLabel = NULL;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700962 /*
963 * r0 contains "this" and it will be used later, so push it to the stack
Bill Buzbee270c1d62009-08-13 16:58:07 -0700964 * first. Pushing r5 (rFP) is just for stack alignment purposes.
Ben Chengba4fc8b2009-06-01 13:00:29 -0700965 */
Bill Buzbee1465db52009-09-23 17:17:35 -0700966 opImm(cUnit, kOpPush, (1 << r0 | 1 << rFP));
Ben Chengba4fc8b2009-06-01 13:00:29 -0700967 /* No need to generate the loop structure if numArgs <= 11 */
968 if (numArgs > 11) {
969 loadConstant(cUnit, 5, ((numArgs - 4) >> 2) << 2);
Bill Buzbee1465db52009-09-23 17:17:35 -0700970 loopLabel = newLIR0(cUnit, kArmPseudoTargetLabel);
Ben Chengd7d426a2009-09-22 11:23:36 -0700971 loopLabel->defMask = ENCODE_ALL;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700972 }
Bill Buzbee270c1d62009-08-13 16:58:07 -0700973 storeMultiple(cUnit, r7, regMask);
Ben Chengd7d426a2009-09-22 11:23:36 -0700974 /*
975 * Protect the loadMultiple instruction from being reordered with other
976 * Dalvik stack accesses.
977 */
Bill Buzbee270c1d62009-08-13 16:58:07 -0700978 loadMultiple(cUnit, r4PC, regMask);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700979 /* No need to generate the loop structure if numArgs <= 11 */
980 if (numArgs > 11) {
Bill Buzbee1465db52009-09-23 17:17:35 -0700981 opRegImm(cUnit, kOpSub, rFP, 4);
982 genConditionalBranch(cUnit, kArmCondNe, loopLabel);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700983 }
984 }
985
986 /* Save the last batch of loaded values */
Bill Buzbee270c1d62009-08-13 16:58:07 -0700987 storeMultiple(cUnit, r7, regMask);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700988
989 /* Generate the loop epilogue - don't use r0 */
990 if ((numArgs > 4) && (numArgs % 4)) {
991 regMask = ((1 << (numArgs & 0x3)) - 1) << 1;
Ben Chengd7d426a2009-09-22 11:23:36 -0700992 /*
993 * Protect the loadMultiple instruction from being reordered with other
994 * Dalvik stack accesses.
995 */
Bill Buzbee270c1d62009-08-13 16:58:07 -0700996 loadMultiple(cUnit, r4PC, regMask);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700997 }
998 if (numArgs >= 8)
Bill Buzbee1465db52009-09-23 17:17:35 -0700999 opImm(cUnit, kOpPop, (1 << r0 | 1 << rFP));
Ben Chengba4fc8b2009-06-01 13:00:29 -07001000
1001 /* Save the modulo 4 arguments */
1002 if ((numArgs > 4) && (numArgs % 4)) {
Bill Buzbee270c1d62009-08-13 16:58:07 -07001003 storeMultiple(cUnit, r7, regMask);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001004 }
1005}
1006
Ben Cheng38329f52009-07-07 14:19:20 -07001007/*
1008 * Generate code to setup the call stack then jump to the chaining cell if it
1009 * is not a native method.
1010 */
1011static void genInvokeSingletonCommon(CompilationUnit *cUnit, MIR *mir,
Bill Buzbee89efc3d2009-07-28 11:22:22 -07001012 BasicBlock *bb, ArmLIR *labelList,
1013 ArmLIR *pcrLabel,
Ben Cheng38329f52009-07-07 14:19:20 -07001014 const Method *calleeMethod)
Ben Chengba4fc8b2009-06-01 13:00:29 -07001015{
Bill Buzbee1465db52009-09-23 17:17:35 -07001016 /*
1017 * Note: all Dalvik register state should be flushed to
1018 * memory by the point, so register usage restrictions no
1019 * longer apply. All temp & preserved registers may be used.
1020 */
Bill Buzbeec6f10662010-02-09 11:16:15 -08001021 dvmCompilerLockAllTemps(cUnit);
Bill Buzbee89efc3d2009-07-28 11:22:22 -07001022 ArmLIR *retChainingCell = &labelList[bb->fallThrough->id];
Ben Chengba4fc8b2009-06-01 13:00:29 -07001023
1024 /* r1 = &retChainingCell */
Bill Buzbeec6f10662010-02-09 11:16:15 -08001025 dvmCompilerLockTemp(cUnit, r1);
Bill Buzbee1465db52009-09-23 17:17:35 -07001026 ArmLIR *addrRetChain = opRegRegImm(cUnit, kOpAdd, r1, rpc, 0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001027 /* r4PC = dalvikCallsite */
1028 loadConstant(cUnit, r4PC,
1029 (int) (cUnit->method->insns + mir->offset));
1030 addrRetChain->generic.target = (LIR *) retChainingCell;
1031 /*
Ben Cheng38329f52009-07-07 14:19:20 -07001032 * r0 = calleeMethod (loaded upon calling genInvokeSingletonCommon)
Ben Chengba4fc8b2009-06-01 13:00:29 -07001033 * r1 = &ChainingCell
1034 * r4PC = callsiteDPC
1035 */
1036 if (dvmIsNativeMethod(calleeMethod)) {
Ben Cheng38329f52009-07-07 14:19:20 -07001037 genDispatchToHandler(cUnit, TEMPLATE_INVOKE_METHOD_NATIVE);
Ben Cheng978738d2010-05-13 13:45:57 -07001038#if defined(WITH_JIT_TUNING)
Ben Cheng38329f52009-07-07 14:19:20 -07001039 gDvmJit.invokeNative++;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001040#endif
1041 } else {
1042 genDispatchToHandler(cUnit, TEMPLATE_INVOKE_METHOD_CHAIN);
Ben Cheng978738d2010-05-13 13:45:57 -07001043#if defined(WITH_JIT_TUNING)
Ben Cheng86717f72010-03-05 15:27:21 -08001044 gDvmJit.invokeMonomorphic++;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001045#endif
Ben Cheng38329f52009-07-07 14:19:20 -07001046 /* Branch to the chaining cell */
Ben Chengba4fc8b2009-06-01 13:00:29 -07001047 genUnconditionalBranch(cUnit, &labelList[bb->taken->id]);
1048 }
1049 /* Handle exceptions using the interpreter */
1050 genTrap(cUnit, mir->offset, pcrLabel);
1051}
1052
Ben Cheng38329f52009-07-07 14:19:20 -07001053/*
1054 * Generate code to check the validity of a predicted chain and take actions
1055 * based on the result.
1056 *
1057 * 0x426a99aa : ldr r4, [pc, #72] --> r4 <- dalvikPC of this invoke
1058 * 0x426a99ac : add r1, pc, #32 --> r1 <- &retChainingCell
1059 * 0x426a99ae : add r2, pc, #40 --> r2 <- &predictedChainingCell
1060 * 0x426a99b0 : blx_1 0x426a918c --+ TEMPLATE_INVOKE_METHOD_PREDICTED_CHAIN
1061 * 0x426a99b2 : blx_2 see above --+
1062 * 0x426a99b4 : b 0x426a99d8 --> off to the predicted chain
1063 * 0x426a99b6 : b 0x426a99c8 --> punt to the interpreter
1064 * 0x426a99b8 : ldr r0, [r7, #44] --> r0 <- this->class->vtable[methodIdx]
1065 * 0x426a99ba : cmp r1, #0 --> compare r1 (rechain count) against 0
1066 * 0x426a99bc : bgt 0x426a99c2 --> >=0? don't rechain
1067 * 0x426a99be : ldr r7, [r6, #96] --+ dvmJitToPatchPredictedChain
1068 * 0x426a99c0 : blx r7 --+
1069 * 0x426a99c2 : add r1, pc, #12 --> r1 <- &retChainingCell
1070 * 0x426a99c4 : blx_1 0x426a9098 --+ TEMPLATE_INVOKE_METHOD_NO_OPT
1071 * 0x426a99c6 : blx_2 see above --+
1072 */
1073static void genInvokeVirtualCommon(CompilationUnit *cUnit, MIR *mir,
1074 int methodIndex,
Bill Buzbee89efc3d2009-07-28 11:22:22 -07001075 ArmLIR *retChainingCell,
1076 ArmLIR *predChainingCell,
1077 ArmLIR *pcrLabel)
Ben Cheng38329f52009-07-07 14:19:20 -07001078{
Bill Buzbee1465db52009-09-23 17:17:35 -07001079 /*
1080 * Note: all Dalvik register state should be flushed to
1081 * memory by the point, so register usage restrictions no
1082 * longer apply. Lock temps to prevent them from being
1083 * allocated by utility routines.
1084 */
Bill Buzbeec6f10662010-02-09 11:16:15 -08001085 dvmCompilerLockAllTemps(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07001086
Ben Cheng38329f52009-07-07 14:19:20 -07001087 /* "this" is already left in r0 by genProcessArgs* */
1088
1089 /* r4PC = dalvikCallsite */
1090 loadConstant(cUnit, r4PC,
1091 (int) (cUnit->method->insns + mir->offset));
1092
1093 /* r1 = &retChainingCell */
Bill Buzbee1465db52009-09-23 17:17:35 -07001094 ArmLIR *addrRetChain = opRegRegImm(cUnit, kOpAdd, r1, rpc, 0);
Ben Cheng38329f52009-07-07 14:19:20 -07001095 addrRetChain->generic.target = (LIR *) retChainingCell;
1096
1097 /* r2 = &predictedChainingCell */
Bill Buzbee1465db52009-09-23 17:17:35 -07001098 ArmLIR *predictedChainingCell = opRegRegImm(cUnit, kOpAdd, r2, rpc, 0);
Ben Cheng38329f52009-07-07 14:19:20 -07001099 predictedChainingCell->generic.target = (LIR *) predChainingCell;
1100
1101 genDispatchToHandler(cUnit, TEMPLATE_INVOKE_METHOD_PREDICTED_CHAIN);
1102
1103 /* return through lr - jump to the chaining cell */
1104 genUnconditionalBranch(cUnit, predChainingCell);
1105
1106 /*
1107 * null-check on "this" may have been eliminated, but we still need a PC-
1108 * reconstruction label for stack overflow bailout.
1109 */
1110 if (pcrLabel == NULL) {
1111 int dPC = (int) (cUnit->method->insns + mir->offset);
Bill Buzbee89efc3d2009-07-28 11:22:22 -07001112 pcrLabel = dvmCompilerNew(sizeof(ArmLIR), true);
Ben Chenga4973592010-03-31 11:59:18 -07001113 pcrLabel->opCode = kArmPseudoPCReconstructionCell;
Ben Cheng38329f52009-07-07 14:19:20 -07001114 pcrLabel->operands[0] = dPC;
1115 pcrLabel->operands[1] = mir->offset;
1116 /* Insert the place holder to the growable list */
1117 dvmInsertGrowableList(&cUnit->pcReconstructionList, pcrLabel);
1118 }
1119
1120 /* return through lr+2 - punt to the interpreter */
1121 genUnconditionalBranch(cUnit, pcrLabel);
1122
1123 /*
1124 * return through lr+4 - fully resolve the callee method.
1125 * r1 <- count
1126 * r2 <- &predictedChainCell
1127 * r3 <- this->class
1128 * r4 <- dPC
1129 * r7 <- this->class->vtable
1130 */
1131
1132 /* r0 <- calleeMethod */
Bill Buzbee270c1d62009-08-13 16:58:07 -07001133 loadWordDisp(cUnit, r7, methodIndex * 4, r0);
Ben Cheng38329f52009-07-07 14:19:20 -07001134
1135 /* Check if rechain limit is reached */
Bill Buzbee1465db52009-09-23 17:17:35 -07001136 opRegImm(cUnit, kOpCmp, r1, 0);
Ben Cheng38329f52009-07-07 14:19:20 -07001137
Bill Buzbee1465db52009-09-23 17:17:35 -07001138 ArmLIR *bypassRechaining = opCondBranch(cUnit, kArmCondGt);
Ben Cheng38329f52009-07-07 14:19:20 -07001139
Bill Buzbee270c1d62009-08-13 16:58:07 -07001140 loadWordDisp(cUnit, rGLUE, offsetof(InterpState,
1141 jitToInterpEntries.dvmJitToPatchPredictedChain), r7);
Ben Cheng38329f52009-07-07 14:19:20 -07001142
1143 /*
1144 * r0 = calleeMethod
1145 * r2 = &predictedChainingCell
1146 * r3 = class
1147 *
1148 * &returnChainingCell has been loaded into r1 but is not needed
1149 * when patching the chaining cell and will be clobbered upon
1150 * returning so it will be reconstructed again.
1151 */
Bill Buzbee1465db52009-09-23 17:17:35 -07001152 opReg(cUnit, kOpBlx, r7);
Ben Cheng38329f52009-07-07 14:19:20 -07001153
1154 /* r1 = &retChainingCell */
Bill Buzbee1465db52009-09-23 17:17:35 -07001155 addrRetChain = opRegRegImm(cUnit, kOpAdd, r1, rpc, 0);
Ben Cheng38329f52009-07-07 14:19:20 -07001156 addrRetChain->generic.target = (LIR *) retChainingCell;
1157
1158 bypassRechaining->generic.target = (LIR *) addrRetChain;
1159 /*
1160 * r0 = calleeMethod,
1161 * r1 = &ChainingCell,
1162 * r4PC = callsiteDPC,
1163 */
1164 genDispatchToHandler(cUnit, TEMPLATE_INVOKE_METHOD_NO_OPT);
Ben Cheng978738d2010-05-13 13:45:57 -07001165#if defined(WITH_JIT_TUNING)
Ben Cheng86717f72010-03-05 15:27:21 -08001166 gDvmJit.invokePolymorphic++;
Ben Cheng38329f52009-07-07 14:19:20 -07001167#endif
1168 /* Handle exceptions using the interpreter */
1169 genTrap(cUnit, mir->offset, pcrLabel);
1170}
1171
1172/*
1173 * Up calling this function, "this" is stored in r0. The actual class will be
1174 * chased down off r0 and the predicted one will be retrieved through
1175 * predictedChainingCell then a comparison is performed to see whether the
1176 * previously established chaining is still valid.
1177 *
1178 * The return LIR is a branch based on the comparison result. The actual branch
1179 * target will be setup in the caller.
1180 */
Carl Shapiroe3c01da2010-05-20 22:54:18 -07001181#if 0
Bill Buzbee89efc3d2009-07-28 11:22:22 -07001182static ArmLIR *genCheckPredictedChain(CompilationUnit *cUnit,
1183 ArmLIR *predChainingCell,
1184 ArmLIR *retChainingCell,
Ben Cheng38329f52009-07-07 14:19:20 -07001185 MIR *mir)
1186{
Bill Buzbee1465db52009-09-23 17:17:35 -07001187 /*
1188 * Note: all Dalvik register state should be flushed to
1189 * memory by the point, so register usage restrictions no
1190 * longer apply. All temp & preserved registers may be used.
1191 */
Bill Buzbeec6f10662010-02-09 11:16:15 -08001192 dvmCompilerLockAllTemps(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07001193
Ben Cheng38329f52009-07-07 14:19:20 -07001194 /* r3 now contains this->clazz */
Bill Buzbee270c1d62009-08-13 16:58:07 -07001195 loadWordDisp(cUnit, r0, offsetof(Object, clazz), r3);
Ben Cheng38329f52009-07-07 14:19:20 -07001196
1197 /*
1198 * r2 now contains predicted class. The starting offset of the
1199 * cached value is 4 bytes into the chaining cell.
1200 */
Bill Buzbee89efc3d2009-07-28 11:22:22 -07001201 ArmLIR *getPredictedClass =
Bill Buzbee270c1d62009-08-13 16:58:07 -07001202 loadWordDisp(cUnit, rpc, offsetof(PredictedChainingCell, clazz), r2);
Ben Cheng38329f52009-07-07 14:19:20 -07001203 getPredictedClass->generic.target = (LIR *) predChainingCell;
1204
1205 /*
1206 * r0 now contains predicted method. The starting offset of the
1207 * cached value is 8 bytes into the chaining cell.
1208 */
Bill Buzbee89efc3d2009-07-28 11:22:22 -07001209 ArmLIR *getPredictedMethod =
Bill Buzbee270c1d62009-08-13 16:58:07 -07001210 loadWordDisp(cUnit, rpc, offsetof(PredictedChainingCell, method), r0);
Ben Cheng38329f52009-07-07 14:19:20 -07001211 getPredictedMethod->generic.target = (LIR *) predChainingCell;
1212
1213 /* Load the stats counter to see if it is time to unchain and refresh */
Bill Buzbee89efc3d2009-07-28 11:22:22 -07001214 ArmLIR *getRechainingRequestCount =
Bill Buzbee270c1d62009-08-13 16:58:07 -07001215 loadWordDisp(cUnit, rpc, offsetof(PredictedChainingCell, counter), r7);
Ben Cheng38329f52009-07-07 14:19:20 -07001216 getRechainingRequestCount->generic.target =
1217 (LIR *) predChainingCell;
1218
1219 /* r4PC = dalvikCallsite */
1220 loadConstant(cUnit, r4PC,
1221 (int) (cUnit->method->insns + mir->offset));
1222
1223 /* r1 = &retChainingCell */
Bill Buzbee1465db52009-09-23 17:17:35 -07001224 ArmLIR *addrRetChain = opRegRegImm(cUnit, kOpAdd, r1, rpc, 0);
Ben Cheng38329f52009-07-07 14:19:20 -07001225 addrRetChain->generic.target = (LIR *) retChainingCell;
1226
1227 /* Check if r2 (predicted class) == r3 (actual class) */
Bill Buzbee1465db52009-09-23 17:17:35 -07001228 opRegReg(cUnit, kOpCmp, r2, r3);
Ben Cheng38329f52009-07-07 14:19:20 -07001229
Bill Buzbee1465db52009-09-23 17:17:35 -07001230 return opCondBranch(cUnit, kArmCondEq);
Ben Cheng38329f52009-07-07 14:19:20 -07001231}
Carl Shapiroe3c01da2010-05-20 22:54:18 -07001232#endif
Ben Cheng38329f52009-07-07 14:19:20 -07001233
Ben Chengba4fc8b2009-06-01 13:00:29 -07001234/* Geneate a branch to go back to the interpreter */
1235static void genPuntToInterp(CompilationUnit *cUnit, unsigned int offset)
1236{
1237 /* r0 = dalvik pc */
Bill Buzbeec6f10662010-02-09 11:16:15 -08001238 dvmCompilerFlushAllRegs(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001239 loadConstant(cUnit, r0, (int) (cUnit->method->insns + offset));
Bill Buzbee270c1d62009-08-13 16:58:07 -07001240 loadWordDisp(cUnit, r0, offsetof(Object, clazz), r3);
1241 loadWordDisp(cUnit, rGLUE, offsetof(InterpState,
1242 jitToInterpEntries.dvmJitToInterpPunt), r1);
Bill Buzbee1465db52009-09-23 17:17:35 -07001243 opReg(cUnit, kOpBlx, r1);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001244}
1245
1246/*
1247 * Attempt to single step one instruction using the interpreter and return
1248 * to the compiled code for the next Dalvik instruction
1249 */
1250static void genInterpSingleStep(CompilationUnit *cUnit, MIR *mir)
1251{
1252 int flags = dexGetInstrFlags(gDvm.instrFlags, mir->dalvikInsn.opCode);
1253 int flagsToCheck = kInstrCanBranch | kInstrCanSwitch | kInstrCanReturn |
1254 kInstrCanThrow;
Bill Buzbee1465db52009-09-23 17:17:35 -07001255
Bill Buzbee45273872010-03-11 11:12:15 -08001256 //If already optimized out, just ignore
1257 if (mir->dalvikInsn.opCode == OP_NOP)
1258 return;
1259
Bill Buzbee1465db52009-09-23 17:17:35 -07001260 //Ugly, but necessary. Flush all Dalvik regs so Interp can find them
Bill Buzbeec6f10662010-02-09 11:16:15 -08001261 dvmCompilerFlushAllRegs(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07001262
Ben Chengba4fc8b2009-06-01 13:00:29 -07001263 if ((mir->next == NULL) || (flags & flagsToCheck)) {
1264 genPuntToInterp(cUnit, mir->offset);
1265 return;
1266 }
1267 int entryAddr = offsetof(InterpState,
1268 jitToInterpEntries.dvmJitToInterpSingleStep);
Bill Buzbee270c1d62009-08-13 16:58:07 -07001269 loadWordDisp(cUnit, rGLUE, entryAddr, r2);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001270 /* r0 = dalvik pc */
1271 loadConstant(cUnit, r0, (int) (cUnit->method->insns + mir->offset));
1272 /* r1 = dalvik pc of following instruction */
1273 loadConstant(cUnit, r1, (int) (cUnit->method->insns + mir->next->offset));
Bill Buzbee1465db52009-09-23 17:17:35 -07001274 opReg(cUnit, kOpBlx, r2);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001275}
1276
Bill Buzbeec1d9ed42010-02-02 11:04:33 -08001277/*
1278 * To prevent a thread in a monitor wait from blocking the Jit from
1279 * resetting the code cache, heavyweight monitor lock will not
1280 * be allowed to return to an existing translation. Instead, we will
1281 * handle them by branching to a handler, which will in turn call the
1282 * runtime lock routine and then branch directly back to the
1283 * interpreter main loop. Given the high cost of the heavyweight
1284 * lock operation, this additional cost should be slight (especially when
1285 * considering that we expect the vast majority of lock operations to
1286 * use the fast-path thin lock bypass).
1287 */
Ben Cheng5d90c202009-11-22 23:31:11 -08001288static void genMonitorPortable(CompilationUnit *cUnit, MIR *mir)
Bill Buzbee270c1d62009-08-13 16:58:07 -07001289{
Bill Buzbeeefbd3c52009-11-04 22:18:40 -08001290 bool isEnter = (mir->dalvikInsn.opCode == OP_MONITOR_ENTER);
Bill Buzbee1465db52009-09-23 17:17:35 -07001291 genExportPC(cUnit, mir);
Bill Buzbeec6f10662010-02-09 11:16:15 -08001292 dvmCompilerFlushAllRegs(cUnit); /* Send everything to home location */
1293 RegLocation rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07001294 loadValueDirectFixed(cUnit, rlSrc, r1);
1295 loadWordDisp(cUnit, rGLUE, offsetof(InterpState, self), r0);
Bill Buzbeec1d9ed42010-02-02 11:04:33 -08001296 genNullCheck(cUnit, rlSrc.sRegLow, r1, mir->offset, NULL);
Bill Buzbeeefbd3c52009-11-04 22:18:40 -08001297 if (isEnter) {
Bill Buzbeec1d9ed42010-02-02 11:04:33 -08001298 /* Get dPC of next insn */
1299 loadConstant(cUnit, r4PC, (int)(cUnit->method->insns + mir->offset +
1300 dexGetInstrWidthAbs(gDvm.instrWidth, OP_MONITOR_ENTER)));
1301#if defined(WITH_DEADLOCK_PREDICTION)
1302 genDispatchToHandler(cUnit, TEMPLATE_MONITOR_ENTER_DEBUG);
1303#else
1304 genDispatchToHandler(cUnit, TEMPLATE_MONITOR_ENTER);
1305#endif
Bill Buzbee1465db52009-09-23 17:17:35 -07001306 } else {
Ben Chengbd1326d2010-04-02 15:04:53 -07001307 LOAD_FUNC_ADDR(cUnit, r2, (int)dvmUnlockObject);
Bill Buzbeec1d9ed42010-02-02 11:04:33 -08001308 /* Do the call */
1309 opReg(cUnit, kOpBlx, r2);
Bill Buzbee6bbdd6b2010-02-16 14:40:01 -08001310 opRegImm(cUnit, kOpCmp, r0, 0); /* Did we throw? */
1311 ArmLIR *branchOver = opCondBranch(cUnit, kArmCondNe);
1312 loadConstant(cUnit, r0,
1313 (int) (cUnit->method->insns + mir->offset +
1314 dexGetInstrWidthAbs(gDvm.instrWidth, OP_MONITOR_EXIT)));
1315 genDispatchToHandler(cUnit, TEMPLATE_THROW_EXCEPTION_COMMON);
1316 ArmLIR *target = newLIR0(cUnit, kArmPseudoTargetLabel);
1317 target->defMask = ENCODE_ALL;
1318 branchOver->generic.target = (LIR *) target;
Elliott Hughes6a555132010-02-25 15:41:42 -08001319 dvmCompilerClobberCallRegs(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07001320 }
Bill Buzbee270c1d62009-08-13 16:58:07 -07001321}
1322
Ben Chengba4fc8b2009-06-01 13:00:29 -07001323/*
1324 * The following are the first-level codegen routines that analyze the format
1325 * of each bytecode then either dispatch special purpose codegen routines
1326 * or produce corresponding Thumb instructions directly.
1327 */
1328
1329static bool handleFmt10t_Fmt20t_Fmt30t(CompilationUnit *cUnit, MIR *mir,
Bill Buzbee89efc3d2009-07-28 11:22:22 -07001330 BasicBlock *bb, ArmLIR *labelList)
Ben Chengba4fc8b2009-06-01 13:00:29 -07001331{
1332 /* For OP_GOTO, OP_GOTO_16, and OP_GOTO_32 */
1333 genUnconditionalBranch(cUnit, &labelList[bb->taken->id]);
1334 return false;
1335}
1336
1337static bool handleFmt10x(CompilationUnit *cUnit, MIR *mir)
1338{
1339 OpCode dalvikOpCode = mir->dalvikInsn.opCode;
1340 if (((dalvikOpCode >= OP_UNUSED_3E) && (dalvikOpCode <= OP_UNUSED_43)) ||
Andy McFadden53878242010-03-05 07:24:27 -08001341 ((dalvikOpCode >= OP_UNUSED_E3) && (dalvikOpCode <= OP_UNUSED_E7))) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07001342 LOGE("Codegen: got unused opcode 0x%x\n",dalvikOpCode);
1343 return true;
1344 }
1345 switch (dalvikOpCode) {
1346 case OP_RETURN_VOID:
1347 genReturnCommon(cUnit,mir);
1348 break;
1349 case OP_UNUSED_73:
1350 case OP_UNUSED_79:
1351 case OP_UNUSED_7A:
1352 LOGE("Codegen: got unused opcode 0x%x\n",dalvikOpCode);
1353 return true;
1354 case OP_NOP:
1355 break;
1356 default:
1357 return true;
1358 }
1359 return false;
1360}
1361
1362static bool handleFmt11n_Fmt31i(CompilationUnit *cUnit, MIR *mir)
1363{
Bill Buzbee1465db52009-09-23 17:17:35 -07001364 RegLocation rlDest;
1365 RegLocation rlResult;
1366 if (mir->ssaRep->numDefs == 2) {
Bill Buzbeec6f10662010-02-09 11:16:15 -08001367 rlDest = dvmCompilerGetDestWide(cUnit, mir, 0, 1);
Bill Buzbee1465db52009-09-23 17:17:35 -07001368 } else {
Bill Buzbeec6f10662010-02-09 11:16:15 -08001369 rlDest = dvmCompilerGetDest(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07001370 }
Ben Chenge9695e52009-06-16 16:11:47 -07001371
Ben Chengba4fc8b2009-06-01 13:00:29 -07001372 switch (mir->dalvikInsn.opCode) {
1373 case OP_CONST:
Ben Chenge9695e52009-06-16 16:11:47 -07001374 case OP_CONST_4: {
Bill Buzbeec6f10662010-02-09 11:16:15 -08001375 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kAnyReg, true);
Ben Chengbd1326d2010-04-02 15:04:53 -07001376 loadConstantNoClobber(cUnit, rlResult.lowReg, mir->dalvikInsn.vB);
Bill Buzbee1465db52009-09-23 17:17:35 -07001377 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001378 break;
Ben Chenge9695e52009-06-16 16:11:47 -07001379 }
1380 case OP_CONST_WIDE_32: {
Bill Buzbee1465db52009-09-23 17:17:35 -07001381 //TUNING: single routine to load constant pair for support doubles
Bill Buzbee964a7b02010-01-28 12:54:19 -08001382 //TUNING: load 0/-1 separately to avoid load dependency
Bill Buzbeec6f10662010-02-09 11:16:15 -08001383 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Ben Chengbd1326d2010-04-02 15:04:53 -07001384 loadConstantNoClobber(cUnit, rlResult.lowReg, mir->dalvikInsn.vB);
Bill Buzbee1465db52009-09-23 17:17:35 -07001385 opRegRegImm(cUnit, kOpAsr, rlResult.highReg,
1386 rlResult.lowReg, 31);
1387 storeValueWide(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001388 break;
Ben Chenge9695e52009-06-16 16:11:47 -07001389 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07001390 default:
1391 return true;
1392 }
1393 return false;
1394}
1395
1396static bool handleFmt21h(CompilationUnit *cUnit, MIR *mir)
1397{
Bill Buzbee1465db52009-09-23 17:17:35 -07001398 RegLocation rlDest;
1399 RegLocation rlResult;
1400 if (mir->ssaRep->numDefs == 2) {
Bill Buzbeec6f10662010-02-09 11:16:15 -08001401 rlDest = dvmCompilerGetDestWide(cUnit, mir, 0, 1);
Bill Buzbee1465db52009-09-23 17:17:35 -07001402 } else {
Bill Buzbeec6f10662010-02-09 11:16:15 -08001403 rlDest = dvmCompilerGetDest(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07001404 }
Bill Buzbeec6f10662010-02-09 11:16:15 -08001405 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kAnyReg, true);
Ben Chenge9695e52009-06-16 16:11:47 -07001406
Ben Chengba4fc8b2009-06-01 13:00:29 -07001407 switch (mir->dalvikInsn.opCode) {
Ben Chenge9695e52009-06-16 16:11:47 -07001408 case OP_CONST_HIGH16: {
Ben Chengbd1326d2010-04-02 15:04:53 -07001409 loadConstantNoClobber(cUnit, rlResult.lowReg,
1410 mir->dalvikInsn.vB << 16);
Bill Buzbee1465db52009-09-23 17:17:35 -07001411 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001412 break;
Ben Chenge9695e52009-06-16 16:11:47 -07001413 }
1414 case OP_CONST_WIDE_HIGH16: {
Bill Buzbee1465db52009-09-23 17:17:35 -07001415 loadConstantValueWide(cUnit, rlResult.lowReg, rlResult.highReg,
1416 0, mir->dalvikInsn.vB << 16);
1417 storeValueWide(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001418 break;
Ben Chenge9695e52009-06-16 16:11:47 -07001419 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07001420 default:
1421 return true;
1422 }
1423 return false;
1424}
1425
1426static bool handleFmt20bc(CompilationUnit *cUnit, MIR *mir)
1427{
1428 /* For OP_THROW_VERIFICATION_ERROR */
1429 genInterpSingleStep(cUnit, mir);
1430 return false;
1431}
1432
1433static bool handleFmt21c_Fmt31c(CompilationUnit *cUnit, MIR *mir)
1434{
Bill Buzbee1465db52009-09-23 17:17:35 -07001435 RegLocation rlResult;
1436 RegLocation rlDest;
1437 RegLocation rlSrc;
Ben Chenge9695e52009-06-16 16:11:47 -07001438
Ben Chengba4fc8b2009-06-01 13:00:29 -07001439 switch (mir->dalvikInsn.opCode) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07001440 case OP_CONST_STRING_JUMBO:
1441 case OP_CONST_STRING: {
1442 void *strPtr = (void*)
1443 (cUnit->method->clazz->pDvmDex->pResStrings[mir->dalvikInsn.vB]);
Ben Chengdd6e8702010-05-07 13:05:47 -07001444
1445 if (strPtr == NULL) {
1446 LOGE("Unexpected null string");
1447 dvmAbort();
1448 }
1449
Bill Buzbeec6f10662010-02-09 11:16:15 -08001450 rlDest = dvmCompilerGetDest(cUnit, mir, 0);
1451 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Ben Chengbd1326d2010-04-02 15:04:53 -07001452 loadConstantNoClobber(cUnit, rlResult.lowReg, (int) strPtr );
Bill Buzbee1465db52009-09-23 17:17:35 -07001453 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001454 break;
1455 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07001456 case OP_CONST_CLASS: {
1457 void *classPtr = (void*)
1458 (cUnit->method->clazz->pDvmDex->pResClasses[mir->dalvikInsn.vB]);
Ben Chengdd6e8702010-05-07 13:05:47 -07001459
1460 if (classPtr == NULL) {
1461 LOGE("Unexpected null class");
1462 dvmAbort();
1463 }
1464
Bill Buzbeec6f10662010-02-09 11:16:15 -08001465 rlDest = dvmCompilerGetDest(cUnit, mir, 0);
1466 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Ben Chengbd1326d2010-04-02 15:04:53 -07001467 loadConstantNoClobber(cUnit, rlResult.lowReg, (int) classPtr );
Bill Buzbee1465db52009-09-23 17:17:35 -07001468 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001469 break;
1470 }
1471 case OP_SGET_OBJECT:
1472 case OP_SGET_BOOLEAN:
1473 case OP_SGET_CHAR:
1474 case OP_SGET_BYTE:
1475 case OP_SGET_SHORT:
1476 case OP_SGET: {
Bill Buzbee50a6bf22009-07-08 13:08:04 -07001477 int valOffset = offsetof(StaticField, value);
Bill Buzbeec6f10662010-02-09 11:16:15 -08001478 int tReg = dvmCompilerAllocTemp(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001479 void *fieldPtr = (void*)
1480 (cUnit->method->clazz->pDvmDex->pResFields[mir->dalvikInsn.vB]);
Ben Chengdd6e8702010-05-07 13:05:47 -07001481
1482 if (fieldPtr == NULL) {
1483 LOGE("Unexpected null static field");
1484 dvmAbort();
1485 }
1486
Bill Buzbeec6f10662010-02-09 11:16:15 -08001487 rlDest = dvmCompilerGetDest(cUnit, mir, 0);
1488 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kAnyReg, true);
Bill Buzbee1465db52009-09-23 17:17:35 -07001489 loadConstant(cUnit, tReg, (int) fieldPtr + valOffset);
Ben Cheng11d8f142010-03-24 15:24:19 -07001490
1491 HEAP_ACCESS_SHADOW(true);
Bill Buzbee1465db52009-09-23 17:17:35 -07001492 loadWordDisp(cUnit, tReg, 0, rlResult.lowReg);
Ben Cheng11d8f142010-03-24 15:24:19 -07001493 HEAP_ACCESS_SHADOW(false);
1494
Bill Buzbee1465db52009-09-23 17:17:35 -07001495 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001496 break;
1497 }
1498 case OP_SGET_WIDE: {
Bill Buzbee50a6bf22009-07-08 13:08:04 -07001499 int valOffset = offsetof(StaticField, value);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001500 void *fieldPtr = (void*)
1501 (cUnit->method->clazz->pDvmDex->pResFields[mir->dalvikInsn.vB]);
Ben Chengdd6e8702010-05-07 13:05:47 -07001502
1503 if (fieldPtr == NULL) {
1504 LOGE("Unexpected null static field");
1505 dvmAbort();
1506 }
1507
Bill Buzbeec6f10662010-02-09 11:16:15 -08001508 int tReg = dvmCompilerAllocTemp(cUnit);
Bill Buzbeec6f10662010-02-09 11:16:15 -08001509 rlDest = dvmCompilerGetDestWide(cUnit, mir, 0, 1);
1510 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kAnyReg, true);
Bill Buzbee1465db52009-09-23 17:17:35 -07001511 loadConstant(cUnit, tReg, (int) fieldPtr + valOffset);
Ben Cheng11d8f142010-03-24 15:24:19 -07001512
1513 HEAP_ACCESS_SHADOW(true);
Bill Buzbee1465db52009-09-23 17:17:35 -07001514 loadPair(cUnit, tReg, rlResult.lowReg, rlResult.highReg);
Ben Cheng11d8f142010-03-24 15:24:19 -07001515 HEAP_ACCESS_SHADOW(false);
1516
Bill Buzbee1465db52009-09-23 17:17:35 -07001517 storeValueWide(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001518 break;
1519 }
1520 case OP_SPUT_OBJECT:
1521 case OP_SPUT_BOOLEAN:
1522 case OP_SPUT_CHAR:
1523 case OP_SPUT_BYTE:
1524 case OP_SPUT_SHORT:
1525 case OP_SPUT: {
Bill Buzbee50a6bf22009-07-08 13:08:04 -07001526 int valOffset = offsetof(StaticField, value);
Bill Buzbeec6f10662010-02-09 11:16:15 -08001527 int tReg = dvmCompilerAllocTemp(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001528 void *fieldPtr = (void*)
1529 (cUnit->method->clazz->pDvmDex->pResFields[mir->dalvikInsn.vB]);
Ben Chenge9695e52009-06-16 16:11:47 -07001530
Ben Chengdd6e8702010-05-07 13:05:47 -07001531 if (fieldPtr == NULL) {
1532 LOGE("Unexpected null static field");
1533 dvmAbort();
1534 }
1535
Bill Buzbeec6f10662010-02-09 11:16:15 -08001536 rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07001537 rlSrc = loadValue(cUnit, rlSrc, kAnyReg);
1538 loadConstant(cUnit, tReg, (int) fieldPtr + valOffset);
Ben Cheng11d8f142010-03-24 15:24:19 -07001539
1540 HEAP_ACCESS_SHADOW(true);
Bill Buzbee1465db52009-09-23 17:17:35 -07001541 storeWordDisp(cUnit, tReg, 0 ,rlSrc.lowReg);
Ben Cheng11d8f142010-03-24 15:24:19 -07001542 HEAP_ACCESS_SHADOW(false);
1543
Ben Chengba4fc8b2009-06-01 13:00:29 -07001544 break;
1545 }
1546 case OP_SPUT_WIDE: {
Bill Buzbeec6f10662010-02-09 11:16:15 -08001547 int tReg = dvmCompilerAllocTemp(cUnit);
Bill Buzbee50a6bf22009-07-08 13:08:04 -07001548 int valOffset = offsetof(StaticField, value);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001549 void *fieldPtr = (void*)
1550 (cUnit->method->clazz->pDvmDex->pResFields[mir->dalvikInsn.vB]);
Ben Chenge9695e52009-06-16 16:11:47 -07001551
Ben Chengdd6e8702010-05-07 13:05:47 -07001552 if (fieldPtr == NULL) {
1553 LOGE("Unexpected null static field");
1554 dvmAbort();
1555 }
1556
Bill Buzbeec6f10662010-02-09 11:16:15 -08001557 rlSrc = dvmCompilerGetSrcWide(cUnit, mir, 0, 1);
Bill Buzbee1465db52009-09-23 17:17:35 -07001558 rlSrc = loadValueWide(cUnit, rlSrc, kAnyReg);
1559 loadConstant(cUnit, tReg, (int) fieldPtr + valOffset);
Ben Cheng11d8f142010-03-24 15:24:19 -07001560
1561 HEAP_ACCESS_SHADOW(true);
Bill Buzbee1465db52009-09-23 17:17:35 -07001562 storePair(cUnit, tReg, rlSrc.lowReg, rlSrc.highReg);
Ben Cheng11d8f142010-03-24 15:24:19 -07001563 HEAP_ACCESS_SHADOW(false);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001564 break;
1565 }
1566 case OP_NEW_INSTANCE: {
Ben Chenge9695e52009-06-16 16:11:47 -07001567 /*
1568 * Obey the calling convention and don't mess with the register
1569 * usage.
1570 */
Ben Chengba4fc8b2009-06-01 13:00:29 -07001571 ClassObject *classPtr = (void*)
1572 (cUnit->method->clazz->pDvmDex->pResClasses[mir->dalvikInsn.vB]);
Ben Chengdd6e8702010-05-07 13:05:47 -07001573
1574 if (classPtr == NULL) {
1575 LOGE("Unexpected null class");
1576 dvmAbort();
1577 }
1578
Ben Cheng79d173c2009-09-29 16:12:51 -07001579 /*
1580 * If it is going to throw, it should not make to the trace to begin
Bill Buzbee1465db52009-09-23 17:17:35 -07001581 * with. However, Alloc might throw, so we need to genExportPC()
Ben Cheng79d173c2009-09-29 16:12:51 -07001582 */
1583 assert((classPtr->accessFlags & (ACC_INTERFACE|ACC_ABSTRACT)) == 0);
Bill Buzbeec6f10662010-02-09 11:16:15 -08001584 dvmCompilerFlushAllRegs(cUnit); /* Everything to home location */
Bill Buzbee1465db52009-09-23 17:17:35 -07001585 genExportPC(cUnit, mir);
Ben Chengbd1326d2010-04-02 15:04:53 -07001586 LOAD_FUNC_ADDR(cUnit, r2, (int)dvmAllocObject);
Ben Chenge9695e52009-06-16 16:11:47 -07001587 loadConstant(cUnit, r0, (int) classPtr);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001588 loadConstant(cUnit, r1, ALLOC_DONT_TRACK);
Bill Buzbee1465db52009-09-23 17:17:35 -07001589 opReg(cUnit, kOpBlx, r2);
Elliott Hughes6a555132010-02-25 15:41:42 -08001590 dvmCompilerClobberCallRegs(cUnit);
Ben Cheng4f489172009-09-27 17:08:35 -07001591 /* generate a branch over if allocation is successful */
Bill Buzbee1465db52009-09-23 17:17:35 -07001592 opRegImm(cUnit, kOpCmp, r0, 0); /* NULL? */
1593 ArmLIR *branchOver = opCondBranch(cUnit, kArmCondNe);
Ben Cheng4f489172009-09-27 17:08:35 -07001594 /*
1595 * OOM exception needs to be thrown here and cannot re-execute
1596 */
1597 loadConstant(cUnit, r0,
1598 (int) (cUnit->method->insns + mir->offset));
1599 genDispatchToHandler(cUnit, TEMPLATE_THROW_EXCEPTION_COMMON);
1600 /* noreturn */
1601
Bill Buzbee1465db52009-09-23 17:17:35 -07001602 ArmLIR *target = newLIR0(cUnit, kArmPseudoTargetLabel);
Ben Cheng4f489172009-09-27 17:08:35 -07001603 target->defMask = ENCODE_ALL;
1604 branchOver->generic.target = (LIR *) target;
Bill Buzbeec6f10662010-02-09 11:16:15 -08001605 rlDest = dvmCompilerGetDest(cUnit, mir, 0);
1606 rlResult = dvmCompilerGetReturn(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07001607 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001608 break;
1609 }
1610 case OP_CHECK_CAST: {
Ben Chenge9695e52009-06-16 16:11:47 -07001611 /*
1612 * Obey the calling convention and don't mess with the register
1613 * usage.
1614 */
Ben Chengba4fc8b2009-06-01 13:00:29 -07001615 ClassObject *classPtr =
1616 (cUnit->method->clazz->pDvmDex->pResClasses[mir->dalvikInsn.vB]);
Bill Buzbee4df41a52009-11-12 17:07:16 -08001617 /*
1618 * Note: It is possible that classPtr is NULL at this point,
1619 * even though this instruction has been successfully interpreted.
1620 * If the previous interpretation had a null source, the
1621 * interpreter would not have bothered to resolve the clazz.
1622 * Bail out to the interpreter in this case, and log it
1623 * so that we can tell if it happens frequently.
1624 */
1625 if (classPtr == NULL) {
Ben Cheng11d8f142010-03-24 15:24:19 -07001626 LOGVV("null clazz in OP_CHECK_CAST, single-stepping");
Bill Buzbee4df41a52009-11-12 17:07:16 -08001627 genInterpSingleStep(cUnit, mir);
1628 return false;
1629 }
Bill Buzbeec6f10662010-02-09 11:16:15 -08001630 dvmCompilerFlushAllRegs(cUnit); /* Everything to home location */
Ben Chengba4fc8b2009-06-01 13:00:29 -07001631 loadConstant(cUnit, r1, (int) classPtr );
Bill Buzbeec6f10662010-02-09 11:16:15 -08001632 rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07001633 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
1634 opRegImm(cUnit, kOpCmp, rlSrc.lowReg, 0); /* Null? */
1635 ArmLIR *branch1 = opCondBranch(cUnit, kArmCondEq);
1636 /*
1637 * rlSrc.lowReg now contains object->clazz. Note that
1638 * it could have been allocated r0, but we're okay so long
1639 * as we don't do anything desctructive until r0 is loaded
1640 * with clazz.
1641 */
Ben Chengba4fc8b2009-06-01 13:00:29 -07001642 /* r0 now contains object->clazz */
Bill Buzbee1465db52009-09-23 17:17:35 -07001643 loadWordDisp(cUnit, rlSrc.lowReg, offsetof(Object, clazz), r0);
Ben Chengbd1326d2010-04-02 15:04:53 -07001644 LOAD_FUNC_ADDR(cUnit, r2, (int)dvmInstanceofNonTrivial);
Bill Buzbee1465db52009-09-23 17:17:35 -07001645 opRegReg(cUnit, kOpCmp, r0, r1);
1646 ArmLIR *branch2 = opCondBranch(cUnit, kArmCondEq);
1647 opReg(cUnit, kOpBlx, r2);
Elliott Hughes6a555132010-02-25 15:41:42 -08001648 dvmCompilerClobberCallRegs(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07001649 /*
1650 * If null, check cast failed - punt to the interpreter. Because
1651 * interpreter will be the one throwing, we don't need to
1652 * genExportPC() here.
1653 */
Bill Buzbee270c1d62009-08-13 16:58:07 -07001654 genZeroCheck(cUnit, r0, mir->offset, NULL);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001655 /* check cast passed - branch target here */
Bill Buzbee1465db52009-09-23 17:17:35 -07001656 ArmLIR *target = newLIR0(cUnit, kArmPseudoTargetLabel);
Ben Chengd7d426a2009-09-22 11:23:36 -07001657 target->defMask = ENCODE_ALL;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001658 branch1->generic.target = (LIR *)target;
1659 branch2->generic.target = (LIR *)target;
1660 break;
1661 }
1662 default:
1663 return true;
1664 }
1665 return false;
1666}
1667
1668static bool handleFmt11x(CompilationUnit *cUnit, MIR *mir)
1669{
1670 OpCode dalvikOpCode = mir->dalvikInsn.opCode;
Bill Buzbee1465db52009-09-23 17:17:35 -07001671 RegLocation rlResult;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001672 switch (dalvikOpCode) {
1673 case OP_MOVE_EXCEPTION: {
1674 int offset = offsetof(InterpState, self);
1675 int exOffset = offsetof(Thread, exception);
Bill Buzbeec6f10662010-02-09 11:16:15 -08001676 int selfReg = dvmCompilerAllocTemp(cUnit);
1677 int resetReg = dvmCompilerAllocTemp(cUnit);
1678 RegLocation rlDest = dvmCompilerGetDest(cUnit, mir, 0);
1679 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Bill Buzbee1465db52009-09-23 17:17:35 -07001680 loadWordDisp(cUnit, rGLUE, offset, selfReg);
Bill Buzbeef9f33282009-11-22 12:45:30 -08001681 loadConstant(cUnit, resetReg, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07001682 loadWordDisp(cUnit, selfReg, exOffset, rlResult.lowReg);
Bill Buzbeef9f33282009-11-22 12:45:30 -08001683 storeWordDisp(cUnit, selfReg, exOffset, resetReg);
Bill Buzbee1465db52009-09-23 17:17:35 -07001684 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001685 break;
1686 }
1687 case OP_MOVE_RESULT:
1688 case OP_MOVE_RESULT_OBJECT: {
Bill Buzbeec6f10662010-02-09 11:16:15 -08001689 RegLocation rlDest = dvmCompilerGetDest(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07001690 RegLocation rlSrc = LOC_DALVIK_RETURN_VAL;
1691 rlSrc.fp = rlDest.fp;
1692 storeValue(cUnit, rlDest, rlSrc);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001693 break;
1694 }
1695 case OP_MOVE_RESULT_WIDE: {
Bill Buzbeec6f10662010-02-09 11:16:15 -08001696 RegLocation rlDest = dvmCompilerGetDestWide(cUnit, mir, 0, 1);
Bill Buzbee1465db52009-09-23 17:17:35 -07001697 RegLocation rlSrc = LOC_DALVIK_RETURN_VAL_WIDE;
1698 rlSrc.fp = rlDest.fp;
1699 storeValueWide(cUnit, rlDest, rlSrc);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001700 break;
1701 }
1702 case OP_RETURN_WIDE: {
Bill Buzbeec6f10662010-02-09 11:16:15 -08001703 RegLocation rlSrc = dvmCompilerGetSrcWide(cUnit, mir, 0, 1);
Bill Buzbee1465db52009-09-23 17:17:35 -07001704 RegLocation rlDest = LOC_DALVIK_RETURN_VAL_WIDE;
1705 rlDest.fp = rlSrc.fp;
1706 storeValueWide(cUnit, rlDest, rlSrc);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001707 genReturnCommon(cUnit,mir);
1708 break;
1709 }
1710 case OP_RETURN:
1711 case OP_RETURN_OBJECT: {
Bill Buzbeec6f10662010-02-09 11:16:15 -08001712 RegLocation rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07001713 RegLocation rlDest = LOC_DALVIK_RETURN_VAL;
1714 rlDest.fp = rlSrc.fp;
1715 storeValue(cUnit, rlDest, rlSrc);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001716 genReturnCommon(cUnit,mir);
1717 break;
1718 }
Bill Buzbee1465db52009-09-23 17:17:35 -07001719 case OP_MONITOR_EXIT:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001720 case OP_MONITOR_ENTER:
Bill Buzbeed0937ef2009-12-22 16:15:39 -08001721#if defined(WITH_DEADLOCK_PREDICTION) || defined(WITH_MONITOR_TRACKING)
Ben Cheng5d90c202009-11-22 23:31:11 -08001722 genMonitorPortable(cUnit, mir);
Bill Buzbee1465db52009-09-23 17:17:35 -07001723#else
Ben Cheng5d90c202009-11-22 23:31:11 -08001724 genMonitor(cUnit, mir);
Bill Buzbee1465db52009-09-23 17:17:35 -07001725#endif
Ben Chengba4fc8b2009-06-01 13:00:29 -07001726 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001727 case OP_THROW: {
1728 genInterpSingleStep(cUnit, mir);
1729 break;
1730 }
1731 default:
1732 return true;
1733 }
1734 return false;
1735}
1736
Bill Buzbeed45ba372009-06-15 17:00:57 -07001737static bool handleFmt12x(CompilationUnit *cUnit, MIR *mir)
1738{
1739 OpCode opCode = mir->dalvikInsn.opCode;
Bill Buzbee1465db52009-09-23 17:17:35 -07001740 RegLocation rlDest;
1741 RegLocation rlSrc;
1742 RegLocation rlResult;
Bill Buzbeed45ba372009-06-15 17:00:57 -07001743
Ben Chengba4fc8b2009-06-01 13:00:29 -07001744 if ( (opCode >= OP_ADD_INT_2ADDR) && (opCode <= OP_REM_DOUBLE_2ADDR)) {
Ben Cheng5d90c202009-11-22 23:31:11 -08001745 return genArithOp( cUnit, mir );
Ben Chengba4fc8b2009-06-01 13:00:29 -07001746 }
1747
Bill Buzbee1465db52009-09-23 17:17:35 -07001748 if (mir->ssaRep->numUses == 2)
Bill Buzbeec6f10662010-02-09 11:16:15 -08001749 rlSrc = dvmCompilerGetSrcWide(cUnit, mir, 0, 1);
Bill Buzbee1465db52009-09-23 17:17:35 -07001750 else
Bill Buzbeec6f10662010-02-09 11:16:15 -08001751 rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07001752 if (mir->ssaRep->numDefs == 2)
Bill Buzbeec6f10662010-02-09 11:16:15 -08001753 rlDest = dvmCompilerGetDestWide(cUnit, mir, 0, 1);
Bill Buzbee1465db52009-09-23 17:17:35 -07001754 else
Bill Buzbeec6f10662010-02-09 11:16:15 -08001755 rlDest = dvmCompilerGetDest(cUnit, mir, 0);
Ben Chenge9695e52009-06-16 16:11:47 -07001756
Ben Chengba4fc8b2009-06-01 13:00:29 -07001757 switch (opCode) {
Bill Buzbee1465db52009-09-23 17:17:35 -07001758 case OP_DOUBLE_TO_INT:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001759 case OP_INT_TO_FLOAT:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001760 case OP_FLOAT_TO_INT:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001761 case OP_DOUBLE_TO_FLOAT:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001762 case OP_FLOAT_TO_DOUBLE:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001763 case OP_INT_TO_DOUBLE:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001764 case OP_FLOAT_TO_LONG:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001765 case OP_LONG_TO_FLOAT:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001766 case OP_DOUBLE_TO_LONG:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001767 case OP_LONG_TO_DOUBLE:
Ben Cheng5d90c202009-11-22 23:31:11 -08001768 return genConversion(cUnit, mir);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001769 case OP_NEG_INT:
1770 case OP_NOT_INT:
Ben Cheng5d90c202009-11-22 23:31:11 -08001771 return genArithOpInt(cUnit, mir, rlDest, rlSrc, rlSrc);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001772 case OP_NEG_LONG:
1773 case OP_NOT_LONG:
Ben Cheng5d90c202009-11-22 23:31:11 -08001774 return genArithOpLong(cUnit, mir, rlDest, rlSrc, rlSrc);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001775 case OP_NEG_FLOAT:
Ben Cheng5d90c202009-11-22 23:31:11 -08001776 return genArithOpFloat(cUnit, mir, rlDest, rlSrc, rlSrc);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001777 case OP_NEG_DOUBLE:
Ben Cheng5d90c202009-11-22 23:31:11 -08001778 return genArithOpDouble(cUnit, mir, rlDest, rlSrc, rlSrc);
Bill Buzbee1465db52009-09-23 17:17:35 -07001779 case OP_MOVE_WIDE:
1780 storeValueWide(cUnit, rlDest, rlSrc);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001781 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07001782 case OP_INT_TO_LONG:
Bill Buzbeec6f10662010-02-09 11:16:15 -08001783 rlSrc = dvmCompilerUpdateLoc(cUnit, rlSrc);
1784 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Bill Buzbee964a7b02010-01-28 12:54:19 -08001785 //TUNING: shouldn't loadValueDirect already check for phys reg?
Bill Buzbee1465db52009-09-23 17:17:35 -07001786 if (rlSrc.location == kLocPhysReg) {
1787 genRegCopy(cUnit, rlResult.lowReg, rlSrc.lowReg);
1788 } else {
1789 loadValueDirect(cUnit, rlSrc, rlResult.lowReg);
1790 }
1791 opRegRegImm(cUnit, kOpAsr, rlResult.highReg,
1792 rlResult.lowReg, 31);
1793 storeValueWide(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001794 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07001795 case OP_LONG_TO_INT:
Bill Buzbeec6f10662010-02-09 11:16:15 -08001796 rlSrc = dvmCompilerUpdateLocWide(cUnit, rlSrc);
1797 rlSrc = dvmCompilerWideToNarrow(cUnit, rlSrc);
Bill Buzbee1465db52009-09-23 17:17:35 -07001798 // Intentional fallthrough
Ben Chengba4fc8b2009-06-01 13:00:29 -07001799 case OP_MOVE:
1800 case OP_MOVE_OBJECT:
Bill Buzbee1465db52009-09-23 17:17:35 -07001801 storeValue(cUnit, rlDest, rlSrc);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001802 break;
1803 case OP_INT_TO_BYTE:
Bill Buzbee1465db52009-09-23 17:17:35 -07001804 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
Bill Buzbeec6f10662010-02-09 11:16:15 -08001805 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Bill Buzbee1465db52009-09-23 17:17:35 -07001806 opRegReg(cUnit, kOp2Byte, rlResult.lowReg, rlSrc.lowReg);
1807 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001808 break;
1809 case OP_INT_TO_SHORT:
Bill Buzbee1465db52009-09-23 17:17:35 -07001810 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
Bill Buzbeec6f10662010-02-09 11:16:15 -08001811 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Bill Buzbee1465db52009-09-23 17:17:35 -07001812 opRegReg(cUnit, kOp2Short, rlResult.lowReg, rlSrc.lowReg);
1813 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001814 break;
1815 case OP_INT_TO_CHAR:
Bill Buzbee1465db52009-09-23 17:17:35 -07001816 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
Bill Buzbeec6f10662010-02-09 11:16:15 -08001817 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Bill Buzbee1465db52009-09-23 17:17:35 -07001818 opRegReg(cUnit, kOp2Char, rlResult.lowReg, rlSrc.lowReg);
1819 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001820 break;
1821 case OP_ARRAY_LENGTH: {
1822 int lenOffset = offsetof(ArrayObject, length);
Bill Buzbee1465db52009-09-23 17:17:35 -07001823 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
1824 genNullCheck(cUnit, rlSrc.sRegLow, rlSrc.lowReg,
1825 mir->offset, NULL);
Bill Buzbeec6f10662010-02-09 11:16:15 -08001826 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Bill Buzbee1465db52009-09-23 17:17:35 -07001827 loadWordDisp(cUnit, rlSrc.lowReg, lenOffset,
1828 rlResult.lowReg);
1829 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001830 break;
1831 }
1832 default:
1833 return true;
1834 }
1835 return false;
1836}
1837
1838static bool handleFmt21s(CompilationUnit *cUnit, MIR *mir)
1839{
1840 OpCode dalvikOpCode = mir->dalvikInsn.opCode;
Bill Buzbee1465db52009-09-23 17:17:35 -07001841 RegLocation rlDest;
1842 RegLocation rlResult;
1843 int BBBB = mir->dalvikInsn.vB;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001844 if (dalvikOpCode == OP_CONST_WIDE_16) {
Bill Buzbeec6f10662010-02-09 11:16:15 -08001845 rlDest = dvmCompilerGetDestWide(cUnit, mir, 0, 1);
1846 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Ben Chengbd1326d2010-04-02 15:04:53 -07001847 loadConstantNoClobber(cUnit, rlResult.lowReg, BBBB);
Bill Buzbee964a7b02010-01-28 12:54:19 -08001848 //TUNING: do high separately to avoid load dependency
Bill Buzbee1465db52009-09-23 17:17:35 -07001849 opRegRegImm(cUnit, kOpAsr, rlResult.highReg, rlResult.lowReg, 31);
1850 storeValueWide(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001851 } else if (dalvikOpCode == OP_CONST_16) {
Bill Buzbeec6f10662010-02-09 11:16:15 -08001852 rlDest = dvmCompilerGetDest(cUnit, mir, 0);
1853 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kAnyReg, true);
Ben Chengbd1326d2010-04-02 15:04:53 -07001854 loadConstantNoClobber(cUnit, rlResult.lowReg, BBBB);
Bill Buzbee1465db52009-09-23 17:17:35 -07001855 storeValue(cUnit, rlDest, rlResult);
1856 } else
Ben Chengba4fc8b2009-06-01 13:00:29 -07001857 return true;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001858 return false;
1859}
1860
1861/* Compare agaist zero */
1862static bool handleFmt21t(CompilationUnit *cUnit, MIR *mir, BasicBlock *bb,
Bill Buzbee89efc3d2009-07-28 11:22:22 -07001863 ArmLIR *labelList)
Ben Chengba4fc8b2009-06-01 13:00:29 -07001864{
1865 OpCode dalvikOpCode = mir->dalvikInsn.opCode;
Bill Buzbee89efc3d2009-07-28 11:22:22 -07001866 ArmConditionCode cond;
Bill Buzbeec6f10662010-02-09 11:16:15 -08001867 RegLocation rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07001868 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
1869 opRegImm(cUnit, kOpCmp, rlSrc.lowReg, 0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001870
Bill Buzbee270c1d62009-08-13 16:58:07 -07001871//TUNING: break this out to allow use of Thumb2 CB[N]Z
Ben Chengba4fc8b2009-06-01 13:00:29 -07001872 switch (dalvikOpCode) {
1873 case OP_IF_EQZ:
Bill Buzbee1465db52009-09-23 17:17:35 -07001874 cond = kArmCondEq;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001875 break;
1876 case OP_IF_NEZ:
Bill Buzbee1465db52009-09-23 17:17:35 -07001877 cond = kArmCondNe;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001878 break;
1879 case OP_IF_LTZ:
Bill Buzbee1465db52009-09-23 17:17:35 -07001880 cond = kArmCondLt;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001881 break;
1882 case OP_IF_GEZ:
Bill Buzbee1465db52009-09-23 17:17:35 -07001883 cond = kArmCondGe;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001884 break;
1885 case OP_IF_GTZ:
Bill Buzbee1465db52009-09-23 17:17:35 -07001886 cond = kArmCondGt;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001887 break;
1888 case OP_IF_LEZ:
Bill Buzbee1465db52009-09-23 17:17:35 -07001889 cond = kArmCondLe;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001890 break;
1891 default:
1892 cond = 0;
1893 LOGE("Unexpected opcode (%d) for Fmt21t\n", dalvikOpCode);
Bill Buzbeefc519dc2010-03-06 23:30:57 -08001894 dvmCompilerAbort(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001895 }
1896 genConditionalBranch(cUnit, cond, &labelList[bb->taken->id]);
1897 /* This mostly likely will be optimized away in a later phase */
1898 genUnconditionalBranch(cUnit, &labelList[bb->fallThrough->id]);
1899 return false;
1900}
1901
Elliott Hughesb4c05972010-02-24 16:36:18 -08001902static bool isPowerOfTwo(int x)
1903{
1904 return (x & (x - 1)) == 0;
1905}
1906
1907// Returns true if no more than two bits are set in 'x'.
1908static bool isPopCountLE2(unsigned int x)
1909{
1910 x &= x - 1;
1911 return (x & (x - 1)) == 0;
1912}
1913
1914// Returns the index of the lowest set bit in 'x'.
1915static int lowestSetBit(unsigned int x) {
1916 int bit_posn = 0;
1917 while ((x & 0xf) == 0) {
1918 bit_posn += 4;
1919 x >>= 4;
Bill Buzbee78cb0e22010-02-11 14:04:53 -08001920 }
Elliott Hughesb4c05972010-02-24 16:36:18 -08001921 while ((x & 1) == 0) {
1922 bit_posn++;
1923 x >>= 1;
1924 }
1925 return bit_posn;
1926}
1927
Elliott Hughes672511b2010-04-26 17:40:13 -07001928// Returns true if it added instructions to 'cUnit' to divide 'rlSrc' by 'lit'
1929// and store the result in 'rlDest'.
Elliott Hughesc7ad9b22010-04-28 13:52:02 -07001930static bool handleEasyDivide(CompilationUnit *cUnit, OpCode dalvikOpCode,
Elliott Hughes672511b2010-04-26 17:40:13 -07001931 RegLocation rlSrc, RegLocation rlDest, int lit)
1932{
1933 if (lit < 2 || !isPowerOfTwo(lit)) {
1934 return false;
1935 }
1936 int k = lowestSetBit(lit);
1937 if (k >= 30) {
1938 // Avoid special cases.
1939 return false;
1940 }
Elliott Hughes9c457022010-04-28 16:15:38 -07001941 bool div = (dalvikOpCode == OP_DIV_INT_LIT8 || dalvikOpCode == OP_DIV_INT_LIT16);
Elliott Hughes672511b2010-04-26 17:40:13 -07001942 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
1943 RegLocation rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Elliott Hughes9c457022010-04-28 16:15:38 -07001944 if (div) {
1945 int tReg = dvmCompilerAllocTemp(cUnit);
1946 if (lit == 2) {
1947 // Division by 2 is by far the most common division by constant.
1948 opRegRegImm(cUnit, kOpLsr, tReg, rlSrc.lowReg, 32 - k);
1949 opRegRegReg(cUnit, kOpAdd, tReg, tReg, rlSrc.lowReg);
1950 opRegRegImm(cUnit, kOpAsr, rlResult.lowReg, tReg, k);
1951 } else {
1952 opRegRegImm(cUnit, kOpAsr, tReg, rlSrc.lowReg, 31);
1953 opRegRegImm(cUnit, kOpLsr, tReg, tReg, 32 - k);
1954 opRegRegReg(cUnit, kOpAdd, tReg, tReg, rlSrc.lowReg);
1955 opRegRegImm(cUnit, kOpAsr, rlResult.lowReg, tReg, k);
1956 }
Elliott Hughes672511b2010-04-26 17:40:13 -07001957 } else {
Elliott Hughes9c457022010-04-28 16:15:38 -07001958 int cReg = dvmCompilerAllocTemp(cUnit);
1959 loadConstant(cUnit, cReg, lit - 1);
1960 int tReg1 = dvmCompilerAllocTemp(cUnit);
1961 int tReg2 = dvmCompilerAllocTemp(cUnit);
1962 if (lit == 2) {
1963 opRegRegImm(cUnit, kOpLsr, tReg1, rlSrc.lowReg, 32 - k);
1964 opRegRegReg(cUnit, kOpAdd, tReg2, tReg1, rlSrc.lowReg);
1965 opRegRegReg(cUnit, kOpAnd, tReg2, tReg2, cReg);
1966 opRegRegReg(cUnit, kOpSub, rlResult.lowReg, tReg2, tReg1);
1967 } else {
1968 opRegRegImm(cUnit, kOpAsr, tReg1, rlSrc.lowReg, 31);
1969 opRegRegImm(cUnit, kOpLsr, tReg1, tReg1, 32 - k);
1970 opRegRegReg(cUnit, kOpAdd, tReg2, tReg1, rlSrc.lowReg);
1971 opRegRegReg(cUnit, kOpAnd, tReg2, tReg2, cReg);
1972 opRegRegReg(cUnit, kOpSub, rlResult.lowReg, tReg2, tReg1);
1973 }
Elliott Hughes672511b2010-04-26 17:40:13 -07001974 }
1975 storeValue(cUnit, rlDest, rlResult);
1976 return true;
1977}
1978
Elliott Hughesb4c05972010-02-24 16:36:18 -08001979// Returns true if it added instructions to 'cUnit' to multiply 'rlSrc' by 'lit'
1980// and store the result in 'rlDest'.
1981static bool handleEasyMultiply(CompilationUnit *cUnit,
1982 RegLocation rlSrc, RegLocation rlDest, int lit)
1983{
1984 // Can we simplify this multiplication?
1985 bool powerOfTwo = false;
1986 bool popCountLE2 = false;
1987 bool powerOfTwoMinusOne = false;
1988 if (lit < 2) {
1989 // Avoid special cases.
1990 return false;
1991 } else if (isPowerOfTwo(lit)) {
1992 powerOfTwo = true;
1993 } else if (isPopCountLE2(lit)) {
1994 popCountLE2 = true;
1995 } else if (isPowerOfTwo(lit + 1)) {
1996 powerOfTwoMinusOne = true;
1997 } else {
1998 return false;
1999 }
2000 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
2001 RegLocation rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
2002 if (powerOfTwo) {
2003 // Shift.
2004 opRegRegImm(cUnit, kOpLsl, rlResult.lowReg, rlSrc.lowReg,
2005 lowestSetBit(lit));
2006 } else if (popCountLE2) {
2007 // Shift and add and shift.
2008 int firstBit = lowestSetBit(lit);
2009 int secondBit = lowestSetBit(lit ^ (1 << firstBit));
2010 genMultiplyByTwoBitMultiplier(cUnit, rlSrc, rlResult, lit,
2011 firstBit, secondBit);
2012 } else {
2013 // Reverse subtract: (src << (shift + 1)) - src.
2014 assert(powerOfTwoMinusOne);
2015 // TODO: rsb dst, src, src lsl#lowestSetBit(lit + 1)
2016 int tReg = dvmCompilerAllocTemp(cUnit);
2017 opRegRegImm(cUnit, kOpLsl, tReg, rlSrc.lowReg, lowestSetBit(lit + 1));
2018 opRegRegReg(cUnit, kOpSub, rlResult.lowReg, tReg, rlSrc.lowReg);
2019 }
2020 storeValue(cUnit, rlDest, rlResult);
2021 return true;
Bill Buzbee78cb0e22010-02-11 14:04:53 -08002022}
2023
Ben Chengba4fc8b2009-06-01 13:00:29 -07002024static bool handleFmt22b_Fmt22s(CompilationUnit *cUnit, MIR *mir)
2025{
2026 OpCode dalvikOpCode = mir->dalvikInsn.opCode;
Bill Buzbeec6f10662010-02-09 11:16:15 -08002027 RegLocation rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
2028 RegLocation rlDest = dvmCompilerGetDest(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07002029 RegLocation rlResult;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002030 int lit = mir->dalvikInsn.vC;
Ben Cheng4f489172009-09-27 17:08:35 -07002031 OpKind op = 0; /* Make gcc happy */
Bill Buzbee1465db52009-09-23 17:17:35 -07002032 int shiftOp = false;
2033 bool isDiv = false;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002034
Ben Chengba4fc8b2009-06-01 13:00:29 -07002035 switch (dalvikOpCode) {
Bill Buzbee1465db52009-09-23 17:17:35 -07002036 case OP_RSUB_INT_LIT8:
2037 case OP_RSUB_INT: {
2038 int tReg;
2039 //TUNING: add support for use of Arm rsub op
2040 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
Bill Buzbeec6f10662010-02-09 11:16:15 -08002041 tReg = dvmCompilerAllocTemp(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07002042 loadConstant(cUnit, tReg, lit);
Bill Buzbeec6f10662010-02-09 11:16:15 -08002043 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Bill Buzbee1465db52009-09-23 17:17:35 -07002044 opRegRegReg(cUnit, kOpSub, rlResult.lowReg,
2045 tReg, rlSrc.lowReg);
2046 storeValue(cUnit, rlDest, rlResult);
2047 return false;
2048 break;
2049 }
2050
Ben Chengba4fc8b2009-06-01 13:00:29 -07002051 case OP_ADD_INT_LIT8:
2052 case OP_ADD_INT_LIT16:
Bill Buzbee1465db52009-09-23 17:17:35 -07002053 op = kOpAdd;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002054 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002055 case OP_MUL_INT_LIT8:
Bill Buzbee78cb0e22010-02-11 14:04:53 -08002056 case OP_MUL_INT_LIT16: {
Elliott Hughesb4c05972010-02-24 16:36:18 -08002057 if (handleEasyMultiply(cUnit, rlSrc, rlDest, lit)) {
2058 return false;
Bill Buzbee78cb0e22010-02-11 14:04:53 -08002059 }
Elliott Hughesb4c05972010-02-24 16:36:18 -08002060 op = kOpMul;
Bill Buzbee1465db52009-09-23 17:17:35 -07002061 break;
Bill Buzbee78cb0e22010-02-11 14:04:53 -08002062 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07002063 case OP_AND_INT_LIT8:
2064 case OP_AND_INT_LIT16:
Bill Buzbee1465db52009-09-23 17:17:35 -07002065 op = kOpAnd;
2066 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002067 case OP_OR_INT_LIT8:
2068 case OP_OR_INT_LIT16:
Bill Buzbee1465db52009-09-23 17:17:35 -07002069 op = kOpOr;
2070 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002071 case OP_XOR_INT_LIT8:
2072 case OP_XOR_INT_LIT16:
Bill Buzbee1465db52009-09-23 17:17:35 -07002073 op = kOpXor;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002074 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002075 case OP_SHL_INT_LIT8:
Bill Buzbee0e605272009-12-01 14:28:05 -08002076 lit &= 31;
Bill Buzbee1465db52009-09-23 17:17:35 -07002077 shiftOp = true;
2078 op = kOpLsl;
2079 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002080 case OP_SHR_INT_LIT8:
Bill Buzbee0e605272009-12-01 14:28:05 -08002081 lit &= 31;
Bill Buzbee1465db52009-09-23 17:17:35 -07002082 shiftOp = true;
2083 op = kOpAsr;
2084 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002085 case OP_USHR_INT_LIT8:
Bill Buzbee0e605272009-12-01 14:28:05 -08002086 lit &= 31;
Bill Buzbee1465db52009-09-23 17:17:35 -07002087 shiftOp = true;
2088 op = kOpLsr;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002089 break;
2090
2091 case OP_DIV_INT_LIT8:
2092 case OP_DIV_INT_LIT16:
Ben Chengba4fc8b2009-06-01 13:00:29 -07002093 case OP_REM_INT_LIT8:
2094 case OP_REM_INT_LIT16:
2095 if (lit == 0) {
2096 /* Let the interpreter deal with div by 0 */
2097 genInterpSingleStep(cUnit, mir);
2098 return false;
2099 }
Elliott Hughesc7ad9b22010-04-28 13:52:02 -07002100 if (handleEasyDivide(cUnit, dalvikOpCode, rlSrc, rlDest, lit)) {
Elliott Hughes672511b2010-04-26 17:40:13 -07002101 return false;
2102 }
Bill Buzbeec6f10662010-02-09 11:16:15 -08002103 dvmCompilerFlushAllRegs(cUnit); /* Everything to home location */
Bill Buzbee1465db52009-09-23 17:17:35 -07002104 loadValueDirectFixed(cUnit, rlSrc, r0);
Bill Buzbeec6f10662010-02-09 11:16:15 -08002105 dvmCompilerClobber(cUnit, r0);
Bill Buzbee1465db52009-09-23 17:17:35 -07002106 if ((dalvikOpCode == OP_DIV_INT_LIT8) ||
2107 (dalvikOpCode == OP_DIV_INT_LIT16)) {
Ben Chengbd1326d2010-04-02 15:04:53 -07002108 LOAD_FUNC_ADDR(cUnit, r2, (int)__aeabi_idiv);
Bill Buzbee1465db52009-09-23 17:17:35 -07002109 isDiv = true;
2110 } else {
Ben Chengbd1326d2010-04-02 15:04:53 -07002111 LOAD_FUNC_ADDR(cUnit, r2, (int)__aeabi_idivmod);
Bill Buzbee1465db52009-09-23 17:17:35 -07002112 isDiv = false;
2113 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07002114 loadConstant(cUnit, r1, lit);
Bill Buzbee1465db52009-09-23 17:17:35 -07002115 opReg(cUnit, kOpBlx, r2);
Elliott Hughes6a555132010-02-25 15:41:42 -08002116 dvmCompilerClobberCallRegs(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07002117 if (isDiv)
Bill Buzbeec6f10662010-02-09 11:16:15 -08002118 rlResult = dvmCompilerGetReturn(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07002119 else
Bill Buzbeec6f10662010-02-09 11:16:15 -08002120 rlResult = dvmCompilerGetReturnAlt(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07002121 storeValue(cUnit, rlDest, rlResult);
2122 return false;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002123 break;
2124 default:
2125 return true;
2126 }
Bill Buzbee1465db52009-09-23 17:17:35 -07002127 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
Bill Buzbeec6f10662010-02-09 11:16:15 -08002128 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Bill Buzbee1465db52009-09-23 17:17:35 -07002129 // Avoid shifts by literal 0 - no support in Thumb. Change to copy
2130 if (shiftOp && (lit == 0)) {
2131 genRegCopy(cUnit, rlResult.lowReg, rlSrc.lowReg);
2132 } else {
2133 opRegRegImm(cUnit, op, rlResult.lowReg, rlSrc.lowReg, lit);
2134 }
2135 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002136 return false;
2137}
2138
2139static bool handleFmt22c(CompilationUnit *cUnit, MIR *mir)
2140{
2141 OpCode dalvikOpCode = mir->dalvikInsn.opCode;
2142 int fieldOffset;
2143
2144 if (dalvikOpCode >= OP_IGET && dalvikOpCode <= OP_IPUT_SHORT) {
2145 InstField *pInstField = (InstField *)
2146 cUnit->method->clazz->pDvmDex->pResFields[mir->dalvikInsn.vC];
Ben Chengba4fc8b2009-06-01 13:00:29 -07002147
Ben Chengdd6e8702010-05-07 13:05:47 -07002148 if (pInstField == NULL) {
2149 LOGE("Unexpected null instance field");
2150 dvmAbort();
2151 }
2152
Ben Chengba4fc8b2009-06-01 13:00:29 -07002153 fieldOffset = pInstField->byteOffset;
2154 } else {
Ben Chenga0e7b602009-10-13 23:09:01 -07002155 /* Deliberately break the code while make the compiler happy */
2156 fieldOffset = -1;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002157 }
2158 switch (dalvikOpCode) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07002159 case OP_NEW_ARRAY: {
Bill Buzbee1465db52009-09-23 17:17:35 -07002160 // Generates a call - use explicit registers
Bill Buzbeec6f10662010-02-09 11:16:15 -08002161 RegLocation rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
2162 RegLocation rlDest = dvmCompilerGetDest(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07002163 RegLocation rlResult;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002164 void *classPtr = (void*)
2165 (cUnit->method->clazz->pDvmDex->pResClasses[mir->dalvikInsn.vC]);
Ben Chengdd6e8702010-05-07 13:05:47 -07002166
2167 if (classPtr == NULL) {
2168 LOGE("Unexpected null class");
2169 dvmAbort();
2170 }
2171
Bill Buzbeec6f10662010-02-09 11:16:15 -08002172 dvmCompilerFlushAllRegs(cUnit); /* Everything to home location */
Bill Buzbee1465db52009-09-23 17:17:35 -07002173 genExportPC(cUnit, mir);
2174 loadValueDirectFixed(cUnit, rlSrc, r1); /* Len */
Ben Chengba4fc8b2009-06-01 13:00:29 -07002175 loadConstant(cUnit, r0, (int) classPtr );
Ben Chengbd1326d2010-04-02 15:04:53 -07002176 LOAD_FUNC_ADDR(cUnit, r3, (int)dvmAllocArrayByClass);
Ben Cheng4f489172009-09-27 17:08:35 -07002177 /*
2178 * "len < 0": bail to the interpreter to re-execute the
2179 * instruction
2180 */
Carl Shapiroe3c01da2010-05-20 22:54:18 -07002181 genRegImmCheck(cUnit, kArmCondMi, r1, 0, mir->offset, NULL);
Bill Buzbee270c1d62009-08-13 16:58:07 -07002182 loadConstant(cUnit, r2, ALLOC_DONT_TRACK);
Bill Buzbee1465db52009-09-23 17:17:35 -07002183 opReg(cUnit, kOpBlx, r3);
Elliott Hughes6a555132010-02-25 15:41:42 -08002184 dvmCompilerClobberCallRegs(cUnit);
Ben Cheng4f489172009-09-27 17:08:35 -07002185 /* generate a branch over if allocation is successful */
Bill Buzbee1465db52009-09-23 17:17:35 -07002186 opRegImm(cUnit, kOpCmp, r0, 0); /* NULL? */
2187 ArmLIR *branchOver = opCondBranch(cUnit, kArmCondNe);
Ben Cheng4f489172009-09-27 17:08:35 -07002188 /*
2189 * OOM exception needs to be thrown here and cannot re-execute
2190 */
2191 loadConstant(cUnit, r0,
2192 (int) (cUnit->method->insns + mir->offset));
2193 genDispatchToHandler(cUnit, TEMPLATE_THROW_EXCEPTION_COMMON);
2194 /* noreturn */
2195
Bill Buzbee1465db52009-09-23 17:17:35 -07002196 ArmLIR *target = newLIR0(cUnit, kArmPseudoTargetLabel);
Ben Cheng4f489172009-09-27 17:08:35 -07002197 target->defMask = ENCODE_ALL;
2198 branchOver->generic.target = (LIR *) target;
Bill Buzbeec6f10662010-02-09 11:16:15 -08002199 rlResult = dvmCompilerGetReturn(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07002200 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002201 break;
2202 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07002203 case OP_INSTANCE_OF: {
Bill Buzbee1465db52009-09-23 17:17:35 -07002204 // May generate a call - use explicit registers
Bill Buzbeec6f10662010-02-09 11:16:15 -08002205 RegLocation rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
2206 RegLocation rlDest = dvmCompilerGetDest(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07002207 RegLocation rlResult;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002208 ClassObject *classPtr =
2209 (cUnit->method->clazz->pDvmDex->pResClasses[mir->dalvikInsn.vC]);
Bill Buzbee480e6782010-01-27 15:43:08 -08002210 /*
2211 * Note: It is possible that classPtr is NULL at this point,
2212 * even though this instruction has been successfully interpreted.
2213 * If the previous interpretation had a null source, the
2214 * interpreter would not have bothered to resolve the clazz.
2215 * Bail out to the interpreter in this case, and log it
2216 * so that we can tell if it happens frequently.
2217 */
2218 if (classPtr == NULL) {
2219 LOGD("null clazz in OP_INSTANCE_OF, single-stepping");
2220 genInterpSingleStep(cUnit, mir);
2221 break;
2222 }
Bill Buzbeec6f10662010-02-09 11:16:15 -08002223 dvmCompilerFlushAllRegs(cUnit); /* Everything to home location */
Bill Buzbee1465db52009-09-23 17:17:35 -07002224 loadValueDirectFixed(cUnit, rlSrc, r0); /* Ref */
Ben Chengba4fc8b2009-06-01 13:00:29 -07002225 loadConstant(cUnit, r2, (int) classPtr );
Bill Buzbee270c1d62009-08-13 16:58:07 -07002226//TUNING: compare to 0 primative to allow use of CB[N]Z
Bill Buzbee1465db52009-09-23 17:17:35 -07002227 opRegImm(cUnit, kOpCmp, r0, 0); /* NULL? */
Ben Cheng752c7942009-06-22 10:50:07 -07002228 /* When taken r0 has NULL which can be used for store directly */
Bill Buzbee1465db52009-09-23 17:17:35 -07002229 ArmLIR *branch1 = opCondBranch(cUnit, kArmCondEq);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002230 /* r1 now contains object->clazz */
Bill Buzbee270c1d62009-08-13 16:58:07 -07002231 loadWordDisp(cUnit, r0, offsetof(Object, clazz), r1);
Bill Buzbee1465db52009-09-23 17:17:35 -07002232 /* r1 now contains object->clazz */
Ben Chengbd1326d2010-04-02 15:04:53 -07002233 LOAD_FUNC_ADDR(cUnit, r3, (int)dvmInstanceofNonTrivial);
Ben Cheng752c7942009-06-22 10:50:07 -07002234 loadConstant(cUnit, r0, 1); /* Assume true */
Bill Buzbee1465db52009-09-23 17:17:35 -07002235 opRegReg(cUnit, kOpCmp, r1, r2);
2236 ArmLIR *branch2 = opCondBranch(cUnit, kArmCondEq);
2237 genRegCopy(cUnit, r0, r1);
2238 genRegCopy(cUnit, r1, r2);
2239 opReg(cUnit, kOpBlx, r3);
Elliott Hughes6a555132010-02-25 15:41:42 -08002240 dvmCompilerClobberCallRegs(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002241 /* branch target here */
Bill Buzbee1465db52009-09-23 17:17:35 -07002242 ArmLIR *target = newLIR0(cUnit, kArmPseudoTargetLabel);
Ben Chengd7d426a2009-09-22 11:23:36 -07002243 target->defMask = ENCODE_ALL;
Bill Buzbeec6f10662010-02-09 11:16:15 -08002244 rlResult = dvmCompilerGetReturn(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07002245 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002246 branch1->generic.target = (LIR *)target;
2247 branch2->generic.target = (LIR *)target;
2248 break;
2249 }
2250 case OP_IGET_WIDE:
2251 genIGetWide(cUnit, mir, fieldOffset);
2252 break;
2253 case OP_IGET:
2254 case OP_IGET_OBJECT:
Bill Buzbee1465db52009-09-23 17:17:35 -07002255 genIGet(cUnit, mir, kWord, fieldOffset);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002256 break;
2257 case OP_IGET_BOOLEAN:
Bill Buzbee1465db52009-09-23 17:17:35 -07002258 genIGet(cUnit, mir, kUnsignedByte, fieldOffset);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002259 break;
2260 case OP_IGET_BYTE:
Bill Buzbee1465db52009-09-23 17:17:35 -07002261 genIGet(cUnit, mir, kSignedByte, fieldOffset);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002262 break;
2263 case OP_IGET_CHAR:
Bill Buzbee1465db52009-09-23 17:17:35 -07002264 genIGet(cUnit, mir, kUnsignedHalf, fieldOffset);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002265 break;
2266 case OP_IGET_SHORT:
Bill Buzbee1465db52009-09-23 17:17:35 -07002267 genIGet(cUnit, mir, kSignedHalf, fieldOffset);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002268 break;
2269 case OP_IPUT_WIDE:
2270 genIPutWide(cUnit, mir, fieldOffset);
2271 break;
2272 case OP_IPUT:
2273 case OP_IPUT_OBJECT:
Bill Buzbee1465db52009-09-23 17:17:35 -07002274 genIPut(cUnit, mir, kWord, fieldOffset);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002275 break;
2276 case OP_IPUT_SHORT:
2277 case OP_IPUT_CHAR:
Bill Buzbee1465db52009-09-23 17:17:35 -07002278 genIPut(cUnit, mir, kUnsignedHalf, fieldOffset);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002279 break;
2280 case OP_IPUT_BYTE:
2281 case OP_IPUT_BOOLEAN:
Bill Buzbee1465db52009-09-23 17:17:35 -07002282 genIPut(cUnit, mir, kUnsignedByte, fieldOffset);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002283 break;
Bill Buzbeeb16344a2010-03-15 17:19:12 -07002284 case OP_IGET_WIDE_VOLATILE:
2285 case OP_IPUT_WIDE_VOLATILE:
2286 case OP_SGET_WIDE_VOLATILE:
2287 case OP_SPUT_WIDE_VOLATILE:
2288 genInterpSingleStep(cUnit, mir);
2289 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002290 default:
2291 return true;
2292 }
2293 return false;
2294}
2295
2296static bool handleFmt22cs(CompilationUnit *cUnit, MIR *mir)
2297{
2298 OpCode dalvikOpCode = mir->dalvikInsn.opCode;
2299 int fieldOffset = mir->dalvikInsn.vC;
2300 switch (dalvikOpCode) {
2301 case OP_IGET_QUICK:
2302 case OP_IGET_OBJECT_QUICK:
Bill Buzbee1465db52009-09-23 17:17:35 -07002303 genIGet(cUnit, mir, kWord, fieldOffset);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002304 break;
2305 case OP_IPUT_QUICK:
2306 case OP_IPUT_OBJECT_QUICK:
Bill Buzbee1465db52009-09-23 17:17:35 -07002307 genIPut(cUnit, mir, kWord, fieldOffset);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002308 break;
2309 case OP_IGET_WIDE_QUICK:
2310 genIGetWide(cUnit, mir, fieldOffset);
2311 break;
2312 case OP_IPUT_WIDE_QUICK:
2313 genIPutWide(cUnit, mir, fieldOffset);
2314 break;
2315 default:
2316 return true;
2317 }
2318 return false;
2319
2320}
2321
2322/* Compare agaist zero */
2323static bool handleFmt22t(CompilationUnit *cUnit, MIR *mir, BasicBlock *bb,
Bill Buzbee89efc3d2009-07-28 11:22:22 -07002324 ArmLIR *labelList)
Ben Chengba4fc8b2009-06-01 13:00:29 -07002325{
2326 OpCode dalvikOpCode = mir->dalvikInsn.opCode;
Bill Buzbee89efc3d2009-07-28 11:22:22 -07002327 ArmConditionCode cond;
Bill Buzbeec6f10662010-02-09 11:16:15 -08002328 RegLocation rlSrc1 = dvmCompilerGetSrc(cUnit, mir, 0);
2329 RegLocation rlSrc2 = dvmCompilerGetSrc(cUnit, mir, 1);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002330
Bill Buzbee1465db52009-09-23 17:17:35 -07002331 rlSrc1 = loadValue(cUnit, rlSrc1, kCoreReg);
2332 rlSrc2 = loadValue(cUnit, rlSrc2, kCoreReg);
2333 opRegReg(cUnit, kOpCmp, rlSrc1.lowReg, rlSrc2.lowReg);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002334
2335 switch (dalvikOpCode) {
2336 case OP_IF_EQ:
Bill Buzbee1465db52009-09-23 17:17:35 -07002337 cond = kArmCondEq;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002338 break;
2339 case OP_IF_NE:
Bill Buzbee1465db52009-09-23 17:17:35 -07002340 cond = kArmCondNe;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002341 break;
2342 case OP_IF_LT:
Bill Buzbee1465db52009-09-23 17:17:35 -07002343 cond = kArmCondLt;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002344 break;
2345 case OP_IF_GE:
Bill Buzbee1465db52009-09-23 17:17:35 -07002346 cond = kArmCondGe;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002347 break;
2348 case OP_IF_GT:
Bill Buzbee1465db52009-09-23 17:17:35 -07002349 cond = kArmCondGt;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002350 break;
2351 case OP_IF_LE:
Bill Buzbee1465db52009-09-23 17:17:35 -07002352 cond = kArmCondLe;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002353 break;
2354 default:
2355 cond = 0;
2356 LOGE("Unexpected opcode (%d) for Fmt22t\n", dalvikOpCode);
Bill Buzbeefc519dc2010-03-06 23:30:57 -08002357 dvmCompilerAbort(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002358 }
2359 genConditionalBranch(cUnit, cond, &labelList[bb->taken->id]);
2360 /* This mostly likely will be optimized away in a later phase */
2361 genUnconditionalBranch(cUnit, &labelList[bb->fallThrough->id]);
2362 return false;
2363}
2364
2365static bool handleFmt22x_Fmt32x(CompilationUnit *cUnit, MIR *mir)
2366{
2367 OpCode opCode = mir->dalvikInsn.opCode;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002368
2369 switch (opCode) {
2370 case OP_MOVE_16:
2371 case OP_MOVE_OBJECT_16:
2372 case OP_MOVE_FROM16:
Ben Chenge9695e52009-06-16 16:11:47 -07002373 case OP_MOVE_OBJECT_FROM16: {
Bill Buzbeec6f10662010-02-09 11:16:15 -08002374 storeValue(cUnit, dvmCompilerGetDest(cUnit, mir, 0),
2375 dvmCompilerGetSrc(cUnit, mir, 0));
Ben Chengba4fc8b2009-06-01 13:00:29 -07002376 break;
Ben Chenge9695e52009-06-16 16:11:47 -07002377 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07002378 case OP_MOVE_WIDE_16:
Ben Chenge9695e52009-06-16 16:11:47 -07002379 case OP_MOVE_WIDE_FROM16: {
Bill Buzbeec6f10662010-02-09 11:16:15 -08002380 storeValueWide(cUnit, dvmCompilerGetDestWide(cUnit, mir, 0, 1),
2381 dvmCompilerGetSrcWide(cUnit, mir, 0, 1));
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 default:
2385 return true;
2386 }
2387 return false;
2388}
2389
2390static bool handleFmt23x(CompilationUnit *cUnit, MIR *mir)
2391{
2392 OpCode opCode = mir->dalvikInsn.opCode;
Bill Buzbee1465db52009-09-23 17:17:35 -07002393 RegLocation rlSrc1;
2394 RegLocation rlSrc2;
2395 RegLocation rlDest;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002396
2397 if ( (opCode >= OP_ADD_INT) && (opCode <= OP_REM_DOUBLE)) {
Ben Cheng5d90c202009-11-22 23:31:11 -08002398 return genArithOp( cUnit, mir );
Ben Chengba4fc8b2009-06-01 13:00:29 -07002399 }
2400
Bill Buzbee1465db52009-09-23 17:17:35 -07002401 /* APUTs have 3 sources and no targets */
2402 if (mir->ssaRep->numDefs == 0) {
2403 if (mir->ssaRep->numUses == 3) {
Bill Buzbeec6f10662010-02-09 11:16:15 -08002404 rlDest = dvmCompilerGetSrc(cUnit, mir, 0);
2405 rlSrc1 = dvmCompilerGetSrc(cUnit, mir, 1);
2406 rlSrc2 = dvmCompilerGetSrc(cUnit, mir, 2);
Bill Buzbee1465db52009-09-23 17:17:35 -07002407 } else {
2408 assert(mir->ssaRep->numUses == 4);
Bill Buzbeec6f10662010-02-09 11:16:15 -08002409 rlDest = dvmCompilerGetSrcWide(cUnit, mir, 0, 1);
2410 rlSrc1 = dvmCompilerGetSrc(cUnit, mir, 2);
2411 rlSrc2 = dvmCompilerGetSrc(cUnit, mir, 3);
Bill Buzbee1465db52009-09-23 17:17:35 -07002412 }
2413 } else {
2414 /* Two sources and 1 dest. Deduce the operand sizes */
2415 if (mir->ssaRep->numUses == 4) {
Bill Buzbeec6f10662010-02-09 11:16:15 -08002416 rlSrc1 = dvmCompilerGetSrcWide(cUnit, mir, 0, 1);
2417 rlSrc2 = dvmCompilerGetSrcWide(cUnit, mir, 2, 3);
Bill Buzbee1465db52009-09-23 17:17:35 -07002418 } else {
2419 assert(mir->ssaRep->numUses == 2);
Bill Buzbeec6f10662010-02-09 11:16:15 -08002420 rlSrc1 = dvmCompilerGetSrc(cUnit, mir, 0);
2421 rlSrc2 = dvmCompilerGetSrc(cUnit, mir, 1);
Bill Buzbee1465db52009-09-23 17:17:35 -07002422 }
2423 if (mir->ssaRep->numDefs == 2) {
Bill Buzbeec6f10662010-02-09 11:16:15 -08002424 rlDest = dvmCompilerGetDestWide(cUnit, mir, 0, 1);
Bill Buzbee1465db52009-09-23 17:17:35 -07002425 } else {
2426 assert(mir->ssaRep->numDefs == 1);
Bill Buzbeec6f10662010-02-09 11:16:15 -08002427 rlDest = dvmCompilerGetDest(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07002428 }
2429 }
2430
2431
Ben Chengba4fc8b2009-06-01 13:00:29 -07002432 switch (opCode) {
Bill Buzbeed45ba372009-06-15 17:00:57 -07002433 case OP_CMPL_FLOAT:
2434 case OP_CMPG_FLOAT:
2435 case OP_CMPL_DOUBLE:
2436 case OP_CMPG_DOUBLE:
Ben Cheng5d90c202009-11-22 23:31:11 -08002437 return genCmpFP(cUnit, mir, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002438 case OP_CMP_LONG:
Bill Buzbee1465db52009-09-23 17:17:35 -07002439 genCmpLong(cUnit, mir, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002440 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002441 case OP_AGET_WIDE:
Bill Buzbee1465db52009-09-23 17:17:35 -07002442 genArrayGet(cUnit, mir, kLong, rlSrc1, rlSrc2, rlDest, 3);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002443 break;
2444 case OP_AGET:
2445 case OP_AGET_OBJECT:
Bill Buzbee1465db52009-09-23 17:17:35 -07002446 genArrayGet(cUnit, mir, kWord, rlSrc1, rlSrc2, rlDest, 2);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002447 break;
2448 case OP_AGET_BOOLEAN:
Bill Buzbee1465db52009-09-23 17:17:35 -07002449 genArrayGet(cUnit, mir, kUnsignedByte, rlSrc1, rlSrc2, rlDest, 0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002450 break;
2451 case OP_AGET_BYTE:
Bill Buzbee1465db52009-09-23 17:17:35 -07002452 genArrayGet(cUnit, mir, kSignedByte, rlSrc1, rlSrc2, rlDest, 0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002453 break;
2454 case OP_AGET_CHAR:
Bill Buzbee1465db52009-09-23 17:17:35 -07002455 genArrayGet(cUnit, mir, kUnsignedHalf, rlSrc1, rlSrc2, rlDest, 1);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002456 break;
2457 case OP_AGET_SHORT:
Bill Buzbee1465db52009-09-23 17:17:35 -07002458 genArrayGet(cUnit, mir, kSignedHalf, rlSrc1, rlSrc2, rlDest, 1);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002459 break;
2460 case OP_APUT_WIDE:
Bill Buzbee1465db52009-09-23 17:17:35 -07002461 genArrayPut(cUnit, mir, kLong, rlSrc1, rlSrc2, rlDest, 3);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002462 break;
2463 case OP_APUT:
Bill Buzbee1465db52009-09-23 17:17:35 -07002464 genArrayPut(cUnit, mir, kWord, rlSrc1, rlSrc2, rlDest, 2);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002465 break;
Bill Buzbeebe6534f2010-03-12 16:01:35 -08002466 case OP_APUT_OBJECT:
2467 genArrayObjectPut(cUnit, mir, rlSrc1, rlSrc2, rlDest, 2);
2468 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002469 case OP_APUT_SHORT:
2470 case OP_APUT_CHAR:
Bill Buzbee1465db52009-09-23 17:17:35 -07002471 genArrayPut(cUnit, mir, kUnsignedHalf, rlSrc1, rlSrc2, rlDest, 1);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002472 break;
2473 case OP_APUT_BYTE:
2474 case OP_APUT_BOOLEAN:
Bill Buzbee1465db52009-09-23 17:17:35 -07002475 genArrayPut(cUnit, mir, kUnsignedByte, rlSrc1, rlSrc2, rlDest, 0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002476 break;
2477 default:
2478 return true;
2479 }
2480 return false;
2481}
2482
Ben Cheng6c10a972009-10-29 14:39:18 -07002483/*
2484 * Find the matching case.
2485 *
2486 * return values:
2487 * r0 (low 32-bit): pc of the chaining cell corresponding to the resolved case,
2488 * including default which is placed at MIN(size, MAX_CHAINED_SWITCH_CASES).
2489 * r1 (high 32-bit): the branch offset of the matching case (only for indexes
2490 * above MAX_CHAINED_SWITCH_CASES).
2491 *
2492 * Instructions around the call are:
2493 *
2494 * mov r2, pc
2495 * blx &findPackedSwitchIndex
2496 * mov pc, r0
2497 * .align4
Bill Buzbeebd047242010-05-13 13:02:53 -07002498 * chaining cell for case 0 [12 bytes]
2499 * chaining cell for case 1 [12 bytes]
Ben Cheng6c10a972009-10-29 14:39:18 -07002500 * :
Bill Buzbeebd047242010-05-13 13:02:53 -07002501 * chaining cell for case MIN(size, MAX_CHAINED_SWITCH_CASES)-1 [12 bytes]
Ben Cheng6c10a972009-10-29 14:39:18 -07002502 * chaining cell for case default [8 bytes]
2503 * noChain exit
2504 */
Ben Chengbd1326d2010-04-02 15:04:53 -07002505static s8 findPackedSwitchIndex(const u2* switchData, int testVal, int pc)
Ben Cheng6c10a972009-10-29 14:39:18 -07002506{
2507 int size;
2508 int firstKey;
2509 const int *entries;
2510 int index;
2511 int jumpIndex;
2512 int caseDPCOffset = 0;
2513 /* In Thumb mode pc is 4 ahead of the "mov r2, pc" instruction */
2514 int chainingPC = (pc + 4) & ~3;
2515
2516 /*
2517 * Packed switch data format:
2518 * ushort ident = 0x0100 magic value
2519 * ushort size number of entries in the table
2520 * int first_key first (and lowest) switch case value
2521 * int targets[size] branch targets, relative to switch opcode
2522 *
2523 * Total size is (4+size*2) 16-bit code units.
2524 */
2525 size = switchData[1];
2526 assert(size > 0);
2527
2528 firstKey = switchData[2];
2529 firstKey |= switchData[3] << 16;
2530
2531
2532 /* The entries are guaranteed to be aligned on a 32-bit boundary;
2533 * we can treat them as a native int array.
2534 */
2535 entries = (const int*) &switchData[4];
2536 assert(((u4)entries & 0x3) == 0);
2537
2538 index = testVal - firstKey;
2539
2540 /* Jump to the default cell */
2541 if (index < 0 || index >= size) {
2542 jumpIndex = MIN(size, MAX_CHAINED_SWITCH_CASES);
2543 /* Jump to the non-chaining exit point */
2544 } else if (index >= MAX_CHAINED_SWITCH_CASES) {
2545 jumpIndex = MAX_CHAINED_SWITCH_CASES + 1;
2546 caseDPCOffset = entries[index];
2547 /* Jump to the inline chaining cell */
2548 } else {
2549 jumpIndex = index;
2550 }
2551
Bill Buzbeebd047242010-05-13 13:02:53 -07002552 chainingPC += jumpIndex * CHAIN_CELL_NORMAL_SIZE;
Ben Cheng6c10a972009-10-29 14:39:18 -07002553 return (((s8) caseDPCOffset) << 32) | (u8) chainingPC;
2554}
2555
2556/* See comments for findPackedSwitchIndex */
Ben Chengbd1326d2010-04-02 15:04:53 -07002557static s8 findSparseSwitchIndex(const u2* switchData, int testVal, int pc)
Ben Cheng6c10a972009-10-29 14:39:18 -07002558{
2559 int size;
2560 const int *keys;
2561 const int *entries;
2562 int chainingPC = (pc + 4) & ~3;
2563 int i;
2564
2565 /*
2566 * Sparse switch data format:
2567 * ushort ident = 0x0200 magic value
2568 * ushort size number of entries in the table; > 0
2569 * int keys[size] keys, sorted low-to-high; 32-bit aligned
2570 * int targets[size] branch targets, relative to switch opcode
2571 *
2572 * Total size is (2+size*4) 16-bit code units.
2573 */
2574
2575 size = switchData[1];
2576 assert(size > 0);
2577
2578 /* The keys are guaranteed to be aligned on a 32-bit boundary;
2579 * we can treat them as a native int array.
2580 */
2581 keys = (const int*) &switchData[2];
2582 assert(((u4)keys & 0x3) == 0);
2583
2584 /* The entries are guaranteed to be aligned on a 32-bit boundary;
2585 * we can treat them as a native int array.
2586 */
2587 entries = keys + size;
2588 assert(((u4)entries & 0x3) == 0);
2589
2590 /*
2591 * Run through the list of keys, which are guaranteed to
2592 * be sorted low-to-high.
2593 *
2594 * Most tables have 3-4 entries. Few have more than 10. A binary
2595 * search here is probably not useful.
2596 */
2597 for (i = 0; i < size; i++) {
2598 int k = keys[i];
2599 if (k == testVal) {
2600 /* MAX_CHAINED_SWITCH_CASES + 1 is the start of the overflow case */
2601 int jumpIndex = (i < MAX_CHAINED_SWITCH_CASES) ?
2602 i : MAX_CHAINED_SWITCH_CASES + 1;
Bill Buzbeebd047242010-05-13 13:02:53 -07002603 chainingPC += jumpIndex * CHAIN_CELL_NORMAL_SIZE;
Ben Cheng6c10a972009-10-29 14:39:18 -07002604 return (((s8) entries[i]) << 32) | (u8) chainingPC;
2605 } else if (k > testVal) {
2606 break;
2607 }
2608 }
Bill Buzbeebd047242010-05-13 13:02:53 -07002609 return chainingPC + MIN(size, MAX_CHAINED_SWITCH_CASES) *
2610 CHAIN_CELL_NORMAL_SIZE;
Ben Cheng6c10a972009-10-29 14:39:18 -07002611}
2612
Ben Chengba4fc8b2009-06-01 13:00:29 -07002613static bool handleFmt31t(CompilationUnit *cUnit, MIR *mir)
2614{
2615 OpCode dalvikOpCode = mir->dalvikInsn.opCode;
2616 switch (dalvikOpCode) {
2617 case OP_FILL_ARRAY_DATA: {
Bill Buzbeec6f10662010-02-09 11:16:15 -08002618 RegLocation rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07002619 // Making a call - use explicit registers
Bill Buzbeec6f10662010-02-09 11:16:15 -08002620 dvmCompilerFlushAllRegs(cUnit); /* Everything to home location */
Bill Buzbee1465db52009-09-23 17:17:35 -07002621 genExportPC(cUnit, mir);
2622 loadValueDirectFixed(cUnit, rlSrc, r0);
Ben Chengbd1326d2010-04-02 15:04:53 -07002623 LOAD_FUNC_ADDR(cUnit, r2, (int)dvmInterpHandleFillArrayData);
Ben Cheng6c10a972009-10-29 14:39:18 -07002624 loadConstant(cUnit, r1,
2625 (int) (cUnit->method->insns + mir->offset + mir->dalvikInsn.vB));
Bill Buzbee1465db52009-09-23 17:17:35 -07002626 opReg(cUnit, kOpBlx, r2);
Elliott Hughes6a555132010-02-25 15:41:42 -08002627 dvmCompilerClobberCallRegs(cUnit);
Bill Buzbeefd023aa2009-11-02 09:23:49 -08002628 /* generate a branch over if successful */
2629 opRegImm(cUnit, kOpCmp, r0, 0); /* NULL? */
2630 ArmLIR *branchOver = opCondBranch(cUnit, kArmCondNe);
2631 loadConstant(cUnit, r0,
2632 (int) (cUnit->method->insns + mir->offset));
2633 genDispatchToHandler(cUnit, TEMPLATE_THROW_EXCEPTION_COMMON);
2634 ArmLIR *target = newLIR0(cUnit, kArmPseudoTargetLabel);
2635 target->defMask = ENCODE_ALL;
2636 branchOver->generic.target = (LIR *) target;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002637 break;
2638 }
2639 /*
Ben Cheng6c10a972009-10-29 14:39:18 -07002640 * Compute the goto target of up to
2641 * MIN(switchSize, MAX_CHAINED_SWITCH_CASES) + 1 chaining cells.
2642 * See the comment before findPackedSwitchIndex for the code layout.
Ben Chengba4fc8b2009-06-01 13:00:29 -07002643 */
2644 case OP_PACKED_SWITCH:
2645 case OP_SPARSE_SWITCH: {
Bill Buzbeec6f10662010-02-09 11:16:15 -08002646 RegLocation rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
2647 dvmCompilerFlushAllRegs(cUnit); /* Everything to home location */
Bill Buzbee1465db52009-09-23 17:17:35 -07002648 loadValueDirectFixed(cUnit, rlSrc, r1);
Bill Buzbeec6f10662010-02-09 11:16:15 -08002649 dvmCompilerLockAllTemps(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002650 if (dalvikOpCode == OP_PACKED_SWITCH) {
Ben Chengbd1326d2010-04-02 15:04:53 -07002651 LOAD_FUNC_ADDR(cUnit, r4PC, (int)findPackedSwitchIndex);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002652 } else {
Ben Chengbd1326d2010-04-02 15:04:53 -07002653 LOAD_FUNC_ADDR(cUnit, r4PC, (int)findSparseSwitchIndex);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002654 }
Ben Cheng6c10a972009-10-29 14:39:18 -07002655 /* r0 <- Addr of the switch data */
2656 loadConstant(cUnit, r0,
2657 (int) (cUnit->method->insns + mir->offset + mir->dalvikInsn.vB));
2658 /* r2 <- pc of the instruction following the blx */
2659 opRegReg(cUnit, kOpMov, r2, rpc);
Bill Buzbee1465db52009-09-23 17:17:35 -07002660 opReg(cUnit, kOpBlx, r4PC);
Elliott Hughes6a555132010-02-25 15:41:42 -08002661 dvmCompilerClobberCallRegs(cUnit);
Ben Cheng6c10a972009-10-29 14:39:18 -07002662 /* pc <- computed goto target */
2663 opRegReg(cUnit, kOpMov, rpc, r0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002664 break;
2665 }
2666 default:
2667 return true;
2668 }
2669 return false;
2670}
2671
2672static bool handleFmt35c_3rc(CompilationUnit *cUnit, MIR *mir, BasicBlock *bb,
Bill Buzbee89efc3d2009-07-28 11:22:22 -07002673 ArmLIR *labelList)
Ben Chengba4fc8b2009-06-01 13:00:29 -07002674{
Bill Buzbee9bc3df32009-07-30 10:52:29 -07002675 ArmLIR *retChainingCell = NULL;
Bill Buzbee89efc3d2009-07-28 11:22:22 -07002676 ArmLIR *pcrLabel = NULL;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002677
Bill Buzbeef4ce16f2009-07-28 13:28:25 -07002678 if (bb->fallThrough != NULL)
2679 retChainingCell = &labelList[bb->fallThrough->id];
2680
Ben Chengba4fc8b2009-06-01 13:00:29 -07002681 DecodedInstruction *dInsn = &mir->dalvikInsn;
2682 switch (mir->dalvikInsn.opCode) {
2683 /*
2684 * calleeMethod = this->clazz->vtable[
2685 * method->clazz->pDvmDex->pResMethods[BBBB]->methodIndex
2686 * ]
2687 */
2688 case OP_INVOKE_VIRTUAL:
2689 case OP_INVOKE_VIRTUAL_RANGE: {
Bill Buzbee89efc3d2009-07-28 11:22:22 -07002690 ArmLIR *predChainingCell = &labelList[bb->taken->id];
Ben Chengba4fc8b2009-06-01 13:00:29 -07002691 int methodIndex =
2692 cUnit->method->clazz->pDvmDex->pResMethods[dInsn->vB]->
2693 methodIndex;
2694
2695 if (mir->dalvikInsn.opCode == OP_INVOKE_VIRTUAL)
2696 genProcessArgsNoRange(cUnit, mir, dInsn, &pcrLabel);
2697 else
2698 genProcessArgsRange(cUnit, mir, dInsn, &pcrLabel);
2699
Ben Cheng38329f52009-07-07 14:19:20 -07002700 genInvokeVirtualCommon(cUnit, mir, methodIndex,
2701 retChainingCell,
2702 predChainingCell,
2703 pcrLabel);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002704 break;
2705 }
2706 /*
2707 * calleeMethod = method->clazz->super->vtable[method->clazz->pDvmDex
2708 * ->pResMethods[BBBB]->methodIndex]
2709 */
Ben Chengba4fc8b2009-06-01 13:00:29 -07002710 case OP_INVOKE_SUPER:
2711 case OP_INVOKE_SUPER_RANGE: {
2712 int mIndex = cUnit->method->clazz->pDvmDex->
2713 pResMethods[dInsn->vB]->methodIndex;
2714 const Method *calleeMethod =
2715 cUnit->method->clazz->super->vtable[mIndex];
2716
2717 if (mir->dalvikInsn.opCode == OP_INVOKE_SUPER)
2718 genProcessArgsNoRange(cUnit, mir, dInsn, &pcrLabel);
2719 else
2720 genProcessArgsRange(cUnit, mir, dInsn, &pcrLabel);
2721
2722 /* r0 = calleeMethod */
2723 loadConstant(cUnit, r0, (int) calleeMethod);
2724
Ben Cheng38329f52009-07-07 14:19:20 -07002725 genInvokeSingletonCommon(cUnit, mir, bb, labelList, pcrLabel,
2726 calleeMethod);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002727 break;
2728 }
2729 /* calleeMethod = method->clazz->pDvmDex->pResMethods[BBBB] */
2730 case OP_INVOKE_DIRECT:
2731 case OP_INVOKE_DIRECT_RANGE: {
2732 const Method *calleeMethod =
2733 cUnit->method->clazz->pDvmDex->pResMethods[dInsn->vB];
2734
2735 if (mir->dalvikInsn.opCode == OP_INVOKE_DIRECT)
2736 genProcessArgsNoRange(cUnit, mir, dInsn, &pcrLabel);
2737 else
2738 genProcessArgsRange(cUnit, mir, dInsn, &pcrLabel);
2739
2740 /* r0 = calleeMethod */
2741 loadConstant(cUnit, r0, (int) calleeMethod);
2742
Ben Cheng38329f52009-07-07 14:19:20 -07002743 genInvokeSingletonCommon(cUnit, mir, bb, labelList, pcrLabel,
2744 calleeMethod);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002745 break;
2746 }
2747 /* calleeMethod = method->clazz->pDvmDex->pResMethods[BBBB] */
2748 case OP_INVOKE_STATIC:
2749 case OP_INVOKE_STATIC_RANGE: {
2750 const Method *calleeMethod =
2751 cUnit->method->clazz->pDvmDex->pResMethods[dInsn->vB];
2752
2753 if (mir->dalvikInsn.opCode == OP_INVOKE_STATIC)
2754 genProcessArgsNoRange(cUnit, mir, dInsn,
2755 NULL /* no null check */);
2756 else
2757 genProcessArgsRange(cUnit, mir, dInsn,
2758 NULL /* no null check */);
2759
2760 /* r0 = calleeMethod */
2761 loadConstant(cUnit, r0, (int) calleeMethod);
2762
Ben Cheng38329f52009-07-07 14:19:20 -07002763 genInvokeSingletonCommon(cUnit, mir, bb, labelList, pcrLabel,
2764 calleeMethod);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002765 break;
2766 }
Ben Cheng09e50c92010-05-02 10:45:32 -07002767 /*
Ben Chengba4fc8b2009-06-01 13:00:29 -07002768 * calleeMethod = dvmFindInterfaceMethodInCache(this->clazz,
2769 * BBBB, method, method->clazz->pDvmDex)
Ben Cheng38329f52009-07-07 14:19:20 -07002770 *
Ben Cheng09e50c92010-05-02 10:45:32 -07002771 * The following is an example of generated code for
2772 * "invoke-interface v0"
Ben Cheng38329f52009-07-07 14:19:20 -07002773 *
Ben Cheng09e50c92010-05-02 10:45:32 -07002774 * -------- dalvik offset: 0x0008 @ invoke-interface v0
2775 * 0x47357e36 : ldr r0, [r5, #0] --+
2776 * 0x47357e38 : sub r7,r5,#24 |
2777 * 0x47357e3c : cmp r0, #0 | genProcessArgsNoRange
2778 * 0x47357e3e : beq 0x47357e82 |
2779 * 0x47357e40 : stmia r7, <r0> --+
2780 * 0x47357e42 : ldr r4, [pc, #120] --> r4 <- dalvikPC of this invoke
2781 * 0x47357e44 : add r1, pc, #64 --> r1 <- &retChainingCell
2782 * 0x47357e46 : add r2, pc, #72 --> r2 <- &predictedChainingCell
2783 * 0x47357e48 : blx_1 0x47348190 --+ TEMPLATE_INVOKE_METHOD_
2784 * 0x47357e4a : blx_2 see above --+ PREDICTED_CHAIN
2785 * 0x47357e4c : b 0x47357e90 --> off to the predicted chain
2786 * 0x47357e4e : b 0x47357e82 --> punt to the interpreter
2787 * 0x47357e50 : mov r8, r1 --+
2788 * 0x47357e52 : mov r9, r2 |
2789 * 0x47357e54 : ldr r2, [pc, #96] |
2790 * 0x47357e56 : mov r10, r3 |
2791 * 0x47357e58 : movs r0, r3 | dvmFindInterfaceMethodInCache
2792 * 0x47357e5a : ldr r3, [pc, #88] |
2793 * 0x47357e5c : ldr r7, [pc, #80] |
2794 * 0x47357e5e : mov r1, #1452 |
2795 * 0x47357e62 : blx r7 --+
2796 * 0x47357e64 : cmp r0, #0 --> calleeMethod == NULL?
2797 * 0x47357e66 : bne 0x47357e6e --> branch over the throw if !r0
2798 * 0x47357e68 : ldr r0, [pc, #80] --> load Dalvik PC of the invoke
2799 * 0x47357e6a : blx_1 0x47348494 --+ TEMPLATE_THROW_EXCEPTION_
2800 * 0x47357e6c : blx_2 see above --+ COMMON
2801 * 0x47357e6e : mov r1, r8 --> r1 <- &retChainingCell
2802 * 0x47357e70 : cmp r1, #0 --> compare against 0
2803 * 0x47357e72 : bgt 0x47357e7c --> >=0? don't rechain
2804 * 0x47357e74 : ldr r7, [r6, #108] --+
2805 * 0x47357e76 : mov r2, r9 | dvmJitToPatchPredictedChain
2806 * 0x47357e78 : mov r3, r10 |
2807 * 0x47357e7a : blx r7 --+
2808 * 0x47357e7c : add r1, pc, #8 --> r1 <- &retChainingCell
2809 * 0x47357e7e : blx_1 0x4734809c --+ TEMPLATE_INVOKE_METHOD_NO_OPT
2810 * 0x47357e80 : blx_2 see above --+
2811 * -------- reconstruct dalvik PC : 0x425719dc @ +0x0008
2812 * 0x47357e82 : ldr r0, [pc, #56]
Ben Cheng38329f52009-07-07 14:19:20 -07002813 * Exception_Handling:
Ben Cheng09e50c92010-05-02 10:45:32 -07002814 * 0x47357e84 : ldr r1, [r6, #92]
2815 * 0x47357e86 : blx r1
2816 * 0x47357e88 : .align4
2817 * -------- chaining cell (hot): 0x000b
2818 * 0x47357e88 : ldr r0, [r6, #104]
2819 * 0x47357e8a : blx r0
2820 * 0x47357e8c : data 0x19e2(6626)
2821 * 0x47357e8e : data 0x4257(16983)
2822 * 0x47357e90 : .align4
Ben Cheng38329f52009-07-07 14:19:20 -07002823 * -------- chaining cell (predicted)
Ben Cheng09e50c92010-05-02 10:45:32 -07002824 * 0x47357e90 : data 0xe7fe(59390) --> will be patched into bx
2825 * 0x47357e92 : data 0x0000(0)
2826 * 0x47357e94 : data 0x0000(0) --> class
2827 * 0x47357e96 : data 0x0000(0)
2828 * 0x47357e98 : data 0x0000(0) --> method
2829 * 0x47357e9a : data 0x0000(0)
2830 * 0x47357e9c : data 0x0000(0) --> rechain count
2831 * 0x47357e9e : data 0x0000(0)
2832 * -------- end of chaining cells (0x006c)
2833 * 0x47357eb0 : .word (0xad03e369)
2834 * 0x47357eb4 : .word (0x28a90)
2835 * 0x47357eb8 : .word (0x41a63394)
2836 * 0x47357ebc : .word (0x425719dc)
Ben Chengba4fc8b2009-06-01 13:00:29 -07002837 */
2838 case OP_INVOKE_INTERFACE:
2839 case OP_INVOKE_INTERFACE_RANGE: {
Bill Buzbee89efc3d2009-07-28 11:22:22 -07002840 ArmLIR *predChainingCell = &labelList[bb->taken->id];
Ben Chengba4fc8b2009-06-01 13:00:29 -07002841
Bill Buzbee1465db52009-09-23 17:17:35 -07002842 /* Ensure that nothing is both live and dirty */
Bill Buzbeec6f10662010-02-09 11:16:15 -08002843 dvmCompilerFlushAllRegs(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07002844
Ben Chengba4fc8b2009-06-01 13:00:29 -07002845 if (mir->dalvikInsn.opCode == OP_INVOKE_INTERFACE)
2846 genProcessArgsNoRange(cUnit, mir, dInsn, &pcrLabel);
2847 else
2848 genProcessArgsRange(cUnit, mir, dInsn, &pcrLabel);
2849
Ben Cheng38329f52009-07-07 14:19:20 -07002850 /* "this" is already left in r0 by genProcessArgs* */
2851
2852 /* r4PC = dalvikCallsite */
2853 loadConstant(cUnit, r4PC,
2854 (int) (cUnit->method->insns + mir->offset));
2855
2856 /* r1 = &retChainingCell */
Bill Buzbee270c1d62009-08-13 16:58:07 -07002857 ArmLIR *addrRetChain =
Bill Buzbee1465db52009-09-23 17:17:35 -07002858 opRegRegImm(cUnit, kOpAdd, r1, rpc, 0);
Ben Cheng38329f52009-07-07 14:19:20 -07002859 addrRetChain->generic.target = (LIR *) retChainingCell;
2860
2861 /* r2 = &predictedChainingCell */
Bill Buzbee89efc3d2009-07-28 11:22:22 -07002862 ArmLIR *predictedChainingCell =
Bill Buzbee1465db52009-09-23 17:17:35 -07002863 opRegRegImm(cUnit, kOpAdd, r2, rpc, 0);
Ben Cheng38329f52009-07-07 14:19:20 -07002864 predictedChainingCell->generic.target = (LIR *) predChainingCell;
2865
2866 genDispatchToHandler(cUnit, TEMPLATE_INVOKE_METHOD_PREDICTED_CHAIN);
2867
2868 /* return through lr - jump to the chaining cell */
2869 genUnconditionalBranch(cUnit, predChainingCell);
2870
2871 /*
2872 * null-check on "this" may have been eliminated, but we still need
2873 * a PC-reconstruction label for stack overflow bailout.
2874 */
2875 if (pcrLabel == NULL) {
2876 int dPC = (int) (cUnit->method->insns + mir->offset);
Bill Buzbee89efc3d2009-07-28 11:22:22 -07002877 pcrLabel = dvmCompilerNew(sizeof(ArmLIR), true);
Ben Chenga4973592010-03-31 11:59:18 -07002878 pcrLabel->opCode = kArmPseudoPCReconstructionCell;
Ben Cheng38329f52009-07-07 14:19:20 -07002879 pcrLabel->operands[0] = dPC;
2880 pcrLabel->operands[1] = mir->offset;
2881 /* Insert the place holder to the growable list */
2882 dvmInsertGrowableList(&cUnit->pcReconstructionList, pcrLabel);
2883 }
2884
2885 /* return through lr+2 - punt to the interpreter */
2886 genUnconditionalBranch(cUnit, pcrLabel);
2887
2888 /*
2889 * return through lr+4 - fully resolve the callee method.
2890 * r1 <- count
2891 * r2 <- &predictedChainCell
2892 * r3 <- this->class
2893 * r4 <- dPC
2894 * r7 <- this->class->vtable
2895 */
2896
2897 /* Save count, &predictedChainCell, and class to high regs first */
Bill Buzbee1465db52009-09-23 17:17:35 -07002898 genRegCopy(cUnit, r8, r1);
2899 genRegCopy(cUnit, r9, r2);
2900 genRegCopy(cUnit, r10, r3);
Ben Cheng38329f52009-07-07 14:19:20 -07002901
Ben Chengba4fc8b2009-06-01 13:00:29 -07002902 /* r0 now contains this->clazz */
Bill Buzbee1465db52009-09-23 17:17:35 -07002903 genRegCopy(cUnit, r0, r3);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002904
2905 /* r1 = BBBB */
2906 loadConstant(cUnit, r1, dInsn->vB);
2907
2908 /* r2 = method (caller) */
2909 loadConstant(cUnit, r2, (int) cUnit->method);
2910
2911 /* r3 = pDvmDex */
2912 loadConstant(cUnit, r3, (int) cUnit->method->clazz->pDvmDex);
2913
Ben Chengbd1326d2010-04-02 15:04:53 -07002914 LOAD_FUNC_ADDR(cUnit, r7,
2915 (intptr_t) dvmFindInterfaceMethodInCache);
Bill Buzbee1465db52009-09-23 17:17:35 -07002916 opReg(cUnit, kOpBlx, r7);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002917 /* r0 = calleeMethod (returned from dvmFindInterfaceMethodInCache */
2918
Ben Cheng09e50c92010-05-02 10:45:32 -07002919 dvmCompilerClobberCallRegs(cUnit);
2920 /* generate a branch over if the interface method is resolved */
2921 opRegImm(cUnit, kOpCmp, r0, 0); /* NULL? */
2922 ArmLIR *branchOver = opCondBranch(cUnit, kArmCondNe);
2923 /*
2924 * calleeMethod == NULL -> throw
2925 */
2926 loadConstant(cUnit, r0,
2927 (int) (cUnit->method->insns + mir->offset));
2928 genDispatchToHandler(cUnit, TEMPLATE_THROW_EXCEPTION_COMMON);
2929 /* noreturn */
2930
2931 ArmLIR *target = newLIR0(cUnit, kArmPseudoTargetLabel);
2932 target->defMask = ENCODE_ALL;
2933 branchOver->generic.target = (LIR *) target;
2934
Bill Buzbee1465db52009-09-23 17:17:35 -07002935 genRegCopy(cUnit, r1, r8);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002936
Ben Cheng38329f52009-07-07 14:19:20 -07002937 /* Check if rechain limit is reached */
Bill Buzbee1465db52009-09-23 17:17:35 -07002938 opRegImm(cUnit, kOpCmp, r1, 0);
Ben Cheng38329f52009-07-07 14:19:20 -07002939
Bill Buzbee1465db52009-09-23 17:17:35 -07002940 ArmLIR *bypassRechaining = opCondBranch(cUnit, kArmCondGt);
Ben Cheng38329f52009-07-07 14:19:20 -07002941
Bill Buzbee270c1d62009-08-13 16:58:07 -07002942 loadWordDisp(cUnit, rGLUE, offsetof(InterpState,
2943 jitToInterpEntries.dvmJitToPatchPredictedChain), r7);
Ben Cheng38329f52009-07-07 14:19:20 -07002944
Bill Buzbee1465db52009-09-23 17:17:35 -07002945 genRegCopy(cUnit, r2, r9);
2946 genRegCopy(cUnit, r3, r10);
Ben Cheng38329f52009-07-07 14:19:20 -07002947
2948 /*
2949 * r0 = calleeMethod
2950 * r2 = &predictedChainingCell
2951 * r3 = class
2952 *
2953 * &returnChainingCell has been loaded into r1 but is not needed
2954 * when patching the chaining cell and will be clobbered upon
2955 * returning so it will be reconstructed again.
2956 */
Bill Buzbee1465db52009-09-23 17:17:35 -07002957 opReg(cUnit, kOpBlx, r7);
Ben Cheng38329f52009-07-07 14:19:20 -07002958
2959 /* r1 = &retChainingCell */
Bill Buzbee1465db52009-09-23 17:17:35 -07002960 addrRetChain = opRegRegImm(cUnit, kOpAdd, r1, rpc, 0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002961 addrRetChain->generic.target = (LIR *) retChainingCell;
Ben Cheng38329f52009-07-07 14:19:20 -07002962
2963 bypassRechaining->generic.target = (LIR *) addrRetChain;
2964
Ben Chengba4fc8b2009-06-01 13:00:29 -07002965 /*
2966 * r0 = this, r1 = calleeMethod,
2967 * r1 = &ChainingCell,
2968 * r4PC = callsiteDPC,
2969 */
2970 genDispatchToHandler(cUnit, TEMPLATE_INVOKE_METHOD_NO_OPT);
Ben Cheng978738d2010-05-13 13:45:57 -07002971#if defined(WITH_JIT_TUNING)
Ben Cheng86717f72010-03-05 15:27:21 -08002972 gDvmJit.invokePolymorphic++;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002973#endif
2974 /* Handle exceptions using the interpreter */
2975 genTrap(cUnit, mir->offset, pcrLabel);
2976 break;
2977 }
2978 /* NOP */
2979 case OP_INVOKE_DIRECT_EMPTY: {
2980 return false;
2981 }
2982 case OP_FILLED_NEW_ARRAY:
2983 case OP_FILLED_NEW_ARRAY_RANGE: {
2984 /* Just let the interpreter deal with these */
2985 genInterpSingleStep(cUnit, mir);
2986 break;
2987 }
2988 default:
2989 return true;
2990 }
2991 return false;
2992}
2993
2994static bool handleFmt35ms_3rms(CompilationUnit *cUnit, MIR *mir,
Bill Buzbee89efc3d2009-07-28 11:22:22 -07002995 BasicBlock *bb, ArmLIR *labelList)
Ben Chengba4fc8b2009-06-01 13:00:29 -07002996{
Bill Buzbee89efc3d2009-07-28 11:22:22 -07002997 ArmLIR *retChainingCell = &labelList[bb->fallThrough->id];
2998 ArmLIR *predChainingCell = &labelList[bb->taken->id];
2999 ArmLIR *pcrLabel = NULL;
Ben Chengba4fc8b2009-06-01 13:00:29 -07003000
3001 DecodedInstruction *dInsn = &mir->dalvikInsn;
3002 switch (mir->dalvikInsn.opCode) {
3003 /* calleeMethod = this->clazz->vtable[BBBB] */
3004 case OP_INVOKE_VIRTUAL_QUICK_RANGE:
3005 case OP_INVOKE_VIRTUAL_QUICK: {
3006 int methodIndex = dInsn->vB;
3007 if (mir->dalvikInsn.opCode == OP_INVOKE_VIRTUAL_QUICK)
3008 genProcessArgsNoRange(cUnit, mir, dInsn, &pcrLabel);
3009 else
3010 genProcessArgsRange(cUnit, mir, dInsn, &pcrLabel);
3011
Ben Cheng38329f52009-07-07 14:19:20 -07003012 genInvokeVirtualCommon(cUnit, mir, methodIndex,
3013 retChainingCell,
3014 predChainingCell,
3015 pcrLabel);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003016 break;
3017 }
3018 /* calleeMethod = method->clazz->super->vtable[BBBB] */
3019 case OP_INVOKE_SUPER_QUICK:
3020 case OP_INVOKE_SUPER_QUICK_RANGE: {
3021 const Method *calleeMethod =
3022 cUnit->method->clazz->super->vtable[dInsn->vB];
3023
3024 if (mir->dalvikInsn.opCode == OP_INVOKE_SUPER_QUICK)
3025 genProcessArgsNoRange(cUnit, mir, dInsn, &pcrLabel);
3026 else
3027 genProcessArgsRange(cUnit, mir, dInsn, &pcrLabel);
3028
3029 /* r0 = calleeMethod */
3030 loadConstant(cUnit, r0, (int) calleeMethod);
3031
Ben Cheng38329f52009-07-07 14:19:20 -07003032 genInvokeSingletonCommon(cUnit, mir, bb, labelList, pcrLabel,
3033 calleeMethod);
3034 /* Handle exceptions using the interpreter */
3035 genTrap(cUnit, mir->offset, pcrLabel);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003036 break;
3037 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07003038 default:
3039 return true;
3040 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07003041 return false;
3042}
3043
3044/*
Bill Buzbeefd023aa2009-11-02 09:23:49 -08003045 * This operation is complex enough that we'll do it partly inline
3046 * and partly with a handler. NOTE: the handler uses hardcoded
3047 * values for string object offsets and must be revisitied if the
3048 * layout changes.
3049 */
3050static bool genInlinedCompareTo(CompilationUnit *cUnit, MIR *mir)
3051{
3052#if defined(USE_GLOBAL_STRING_DEFS)
3053 return false;
3054#else
3055 ArmLIR *rollback;
Bill Buzbeec6f10662010-02-09 11:16:15 -08003056 RegLocation rlThis = dvmCompilerGetSrc(cUnit, mir, 0);
3057 RegLocation rlComp = dvmCompilerGetSrc(cUnit, mir, 1);
Bill Buzbeefd023aa2009-11-02 09:23:49 -08003058
3059 loadValueDirectFixed(cUnit, rlThis, r0);
3060 loadValueDirectFixed(cUnit, rlComp, r1);
3061 /* Test objects for NULL */
3062 rollback = genNullCheck(cUnit, rlThis.sRegLow, r0, mir->offset, NULL);
3063 genNullCheck(cUnit, rlComp.sRegLow, r1, mir->offset, rollback);
3064 /*
3065 * TUNING: we could check for object pointer equality before invoking
3066 * handler. Unclear whether the gain would be worth the added code size
3067 * expansion.
3068 */
3069 genDispatchToHandler(cUnit, TEMPLATE_STRING_COMPARETO);
Bill Buzbeec6f10662010-02-09 11:16:15 -08003070 storeValue(cUnit, inlinedTarget(cUnit, mir, false),
3071 dvmCompilerGetReturn(cUnit));
Bill Buzbeefd023aa2009-11-02 09:23:49 -08003072 return true;
3073#endif
3074}
3075
Elliott Hughes2bdbcb62010-04-12 14:29:37 -07003076static bool genInlinedFastIndexOf(CompilationUnit *cUnit, MIR *mir)
Bill Buzbeefd023aa2009-11-02 09:23:49 -08003077{
3078#if defined(USE_GLOBAL_STRING_DEFS)
3079 return false;
3080#else
Bill Buzbeec6f10662010-02-09 11:16:15 -08003081 RegLocation rlThis = dvmCompilerGetSrc(cUnit, mir, 0);
3082 RegLocation rlChar = dvmCompilerGetSrc(cUnit, mir, 1);
Bill Buzbeefd023aa2009-11-02 09:23:49 -08003083
3084 loadValueDirectFixed(cUnit, rlThis, r0);
3085 loadValueDirectFixed(cUnit, rlChar, r1);
Elliott Hughes2bdbcb62010-04-12 14:29:37 -07003086 RegLocation rlStart = dvmCompilerGetSrc(cUnit, mir, 2);
3087 loadValueDirectFixed(cUnit, rlStart, r2);
Bill Buzbeefd023aa2009-11-02 09:23:49 -08003088 /* Test objects for NULL */
3089 genNullCheck(cUnit, rlThis.sRegLow, r0, mir->offset, NULL);
3090 genDispatchToHandler(cUnit, TEMPLATE_STRING_INDEXOF);
Bill Buzbeec6f10662010-02-09 11:16:15 -08003091 storeValue(cUnit, inlinedTarget(cUnit, mir, false),
3092 dvmCompilerGetReturn(cUnit));
Bill Buzbeefd023aa2009-11-02 09:23:49 -08003093 return true;
3094#endif
3095}
3096
Elliott Hughesee34f592010-04-05 18:13:52 -07003097// Generates an inlined String.isEmpty or String.length.
3098static bool genInlinedStringIsEmptyOrLength(CompilationUnit *cUnit, MIR *mir,
3099 bool isEmpty)
Bill Buzbee1f748632010-03-02 16:14:41 -08003100{
Elliott Hughesee34f592010-04-05 18:13:52 -07003101 // dst = src.length();
Bill Buzbee1f748632010-03-02 16:14:41 -08003102 RegLocation rlObj = dvmCompilerGetSrc(cUnit, mir, 0);
3103 RegLocation rlDest = inlinedTarget(cUnit, mir, false);
3104 rlObj = loadValue(cUnit, rlObj, kCoreReg);
3105 RegLocation rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
3106 genNullCheck(cUnit, rlObj.sRegLow, rlObj.lowReg, mir->offset, NULL);
3107 loadWordDisp(cUnit, rlObj.lowReg, gDvm.offJavaLangString_count,
3108 rlResult.lowReg);
Elliott Hughesee34f592010-04-05 18:13:52 -07003109 if (isEmpty) {
3110 // dst = (dst == 0);
3111 int tReg = dvmCompilerAllocTemp(cUnit);
3112 opRegReg(cUnit, kOpNeg, tReg, rlResult.lowReg);
3113 opRegRegReg(cUnit, kOpAdc, rlResult.lowReg, rlResult.lowReg, tReg);
3114 }
Bill Buzbee1f748632010-03-02 16:14:41 -08003115 storeValue(cUnit, rlDest, rlResult);
3116 return false;
3117}
3118
Elliott Hughesee34f592010-04-05 18:13:52 -07003119static bool genInlinedStringLength(CompilationUnit *cUnit, MIR *mir)
3120{
3121 return genInlinedStringIsEmptyOrLength(cUnit, mir, false);
3122}
3123
3124static bool genInlinedStringIsEmpty(CompilationUnit *cUnit, MIR *mir)
3125{
3126 return genInlinedStringIsEmptyOrLength(cUnit, mir, true);
3127}
3128
Bill Buzbee1f748632010-03-02 16:14:41 -08003129static bool genInlinedStringCharAt(CompilationUnit *cUnit, MIR *mir)
3130{
3131 int contents = offsetof(ArrayObject, contents);
3132 RegLocation rlObj = dvmCompilerGetSrc(cUnit, mir, 0);
3133 RegLocation rlIdx = dvmCompilerGetSrc(cUnit, mir, 1);
3134 RegLocation rlDest = inlinedTarget(cUnit, mir, false);
3135 RegLocation rlResult;
3136 rlObj = loadValue(cUnit, rlObj, kCoreReg);
3137 rlIdx = loadValue(cUnit, rlIdx, kCoreReg);
3138 int regMax = dvmCompilerAllocTemp(cUnit);
3139 int regOff = dvmCompilerAllocTemp(cUnit);
3140 int regPtr = dvmCompilerAllocTemp(cUnit);
3141 ArmLIR *pcrLabel = genNullCheck(cUnit, rlObj.sRegLow, rlObj.lowReg,
3142 mir->offset, NULL);
3143 loadWordDisp(cUnit, rlObj.lowReg, gDvm.offJavaLangString_count, regMax);
3144 loadWordDisp(cUnit, rlObj.lowReg, gDvm.offJavaLangString_offset, regOff);
3145 loadWordDisp(cUnit, rlObj.lowReg, gDvm.offJavaLangString_value, regPtr);
3146 genBoundsCheck(cUnit, rlIdx.lowReg, regMax, mir->offset, pcrLabel);
3147 dvmCompilerFreeTemp(cUnit, regMax);
3148 opRegImm(cUnit, kOpAdd, regPtr, contents);
3149 opRegReg(cUnit, kOpAdd, regOff, rlIdx.lowReg);
3150 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
3151 loadBaseIndexed(cUnit, regPtr, regOff, rlResult.lowReg, 1, kUnsignedHalf);
3152 storeValue(cUnit, rlDest, rlResult);
3153 return false;
3154}
3155
3156static bool genInlinedAbsInt(CompilationUnit *cUnit, MIR *mir)
3157{
3158 RegLocation rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
3159 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
3160 RegLocation rlDest = inlinedTarget(cUnit, mir, false);;
3161 RegLocation rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
3162 int signReg = dvmCompilerAllocTemp(cUnit);
3163 /*
3164 * abs(x) = y<=x>>31, (x+y)^y.
3165 * Thumb2's IT block also yields 3 instructions, but imposes
3166 * scheduling constraints.
3167 */
3168 opRegRegImm(cUnit, kOpAsr, signReg, rlSrc.lowReg, 31);
3169 opRegRegReg(cUnit, kOpAdd, rlResult.lowReg, rlSrc.lowReg, signReg);
3170 opRegReg(cUnit, kOpXor, rlResult.lowReg, signReg);
3171 storeValue(cUnit, rlDest, rlResult);
3172 return false;
3173}
3174
3175static bool genInlinedAbsLong(CompilationUnit *cUnit, MIR *mir)
3176{
3177 RegLocation rlSrc = dvmCompilerGetSrcWide(cUnit, mir, 0, 1);
3178 RegLocation rlDest = inlinedTargetWide(cUnit, mir, false);
3179 rlSrc = loadValueWide(cUnit, rlSrc, kCoreReg);
3180 RegLocation rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
3181 int signReg = dvmCompilerAllocTemp(cUnit);
3182 /*
3183 * abs(x) = y<=x>>31, (x+y)^y.
3184 * Thumb2 IT block allows slightly shorter sequence,
3185 * but introduces a scheduling barrier. Stick with this
3186 * mechanism for now.
3187 */
3188 opRegRegImm(cUnit, kOpAsr, signReg, rlSrc.highReg, 31);
3189 opRegRegReg(cUnit, kOpAdd, rlResult.lowReg, rlSrc.lowReg, signReg);
3190 opRegRegReg(cUnit, kOpAdc, rlResult.highReg, rlSrc.highReg, signReg);
3191 opRegReg(cUnit, kOpXor, rlResult.lowReg, signReg);
3192 opRegReg(cUnit, kOpXor, rlResult.highReg, signReg);
3193 storeValueWide(cUnit, rlDest, rlResult);
3194 return false;
3195}
Bill Buzbeefd023aa2009-11-02 09:23:49 -08003196
3197/*
Bill Buzbeece46c942009-11-20 15:41:34 -08003198 * NOTE: Handles both range and non-range versions (arguments
3199 * have already been normalized by this point).
Ben Chengba4fc8b2009-06-01 13:00:29 -07003200 */
Bill Buzbeece46c942009-11-20 15:41:34 -08003201static bool handleExecuteInline(CompilationUnit *cUnit, MIR *mir)
Ben Chengba4fc8b2009-06-01 13:00:29 -07003202{
3203 DecodedInstruction *dInsn = &mir->dalvikInsn;
3204 switch( mir->dalvikInsn.opCode) {
Bill Buzbeece46c942009-11-20 15:41:34 -08003205 case OP_EXECUTE_INLINE_RANGE:
Ben Chengba4fc8b2009-06-01 13:00:29 -07003206 case OP_EXECUTE_INLINE: {
3207 unsigned int i;
3208 const InlineOperation* inLineTable = dvmGetInlineOpsTable();
Bill Buzbee50a6bf22009-07-08 13:08:04 -07003209 int offset = offsetof(InterpState, retval);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003210 int operation = dInsn->vB;
Bill Buzbee50a6bf22009-07-08 13:08:04 -07003211 switch (operation) {
3212 case INLINE_EMPTYINLINEMETHOD:
3213 return false; /* Nop */
3214 case INLINE_STRING_LENGTH:
3215 return genInlinedStringLength(cUnit, mir);
Elliott Hughesee34f592010-04-05 18:13:52 -07003216 case INLINE_STRING_IS_EMPTY:
3217 return genInlinedStringIsEmpty(cUnit, mir);
Bill Buzbee50a6bf22009-07-08 13:08:04 -07003218 case INLINE_MATH_ABS_INT:
3219 return genInlinedAbsInt(cUnit, mir);
3220 case INLINE_MATH_ABS_LONG:
3221 return genInlinedAbsLong(cUnit, mir);
3222 case INLINE_MATH_MIN_INT:
3223 return genInlinedMinMaxInt(cUnit, mir, true);
3224 case INLINE_MATH_MAX_INT:
3225 return genInlinedMinMaxInt(cUnit, mir, false);
3226 case INLINE_STRING_CHARAT:
3227 return genInlinedStringCharAt(cUnit, mir);
3228 case INLINE_MATH_SQRT:
3229 if (genInlineSqrt(cUnit, mir))
Bill Buzbee9727c3d2009-08-01 11:32:36 -07003230 return false;
Bill Buzbee50a6bf22009-07-08 13:08:04 -07003231 else
3232 break; /* Handle with C routine */
Bill Buzbee50a6bf22009-07-08 13:08:04 -07003233 case INLINE_MATH_ABS_FLOAT:
Bill Buzbee1465db52009-09-23 17:17:35 -07003234 if (genInlinedAbsFloat(cUnit, mir))
3235 return false;
3236 else
3237 break;
Bill Buzbee50a6bf22009-07-08 13:08:04 -07003238 case INLINE_MATH_ABS_DOUBLE:
Bill Buzbee1465db52009-09-23 17:17:35 -07003239 if (genInlinedAbsDouble(cUnit, mir))
3240 return false;
3241 else
3242 break;
Bill Buzbee50a6bf22009-07-08 13:08:04 -07003243 case INLINE_STRING_COMPARETO:
Bill Buzbeefd023aa2009-11-02 09:23:49 -08003244 if (genInlinedCompareTo(cUnit, mir))
3245 return false;
3246 else
3247 break;
Elliott Hughes2bdbcb62010-04-12 14:29:37 -07003248 case INLINE_STRING_FASTINDEXOF_II:
3249 if (genInlinedFastIndexOf(cUnit, mir))
Bill Buzbeefd023aa2009-11-02 09:23:49 -08003250 return false;
3251 else
3252 break;
3253 case INLINE_STRING_EQUALS:
3254 case INLINE_MATH_COS:
3255 case INLINE_MATH_SIN:
3256 break; /* Handle with C routine */
Bill Buzbee50a6bf22009-07-08 13:08:04 -07003257 default:
Bill Buzbeefc519dc2010-03-06 23:30:57 -08003258 dvmCompilerAbort(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003259 }
Bill Buzbeec6f10662010-02-09 11:16:15 -08003260 dvmCompilerFlushAllRegs(cUnit); /* Everything to home location */
Elliott Hughes6a555132010-02-25 15:41:42 -08003261 dvmCompilerClobberCallRegs(cUnit);
Bill Buzbeec6f10662010-02-09 11:16:15 -08003262 dvmCompilerClobber(cUnit, r4PC);
3263 dvmCompilerClobber(cUnit, r7);
Bill Buzbee1465db52009-09-23 17:17:35 -07003264 opRegRegImm(cUnit, kOpAdd, r4PC, rGLUE, offset);
3265 opImm(cUnit, kOpPush, (1<<r4PC) | (1<<r7));
Ben Chengbd1326d2010-04-02 15:04:53 -07003266 LOAD_FUNC_ADDR(cUnit, r4PC, (int)inLineTable[operation].func);
Bill Buzbee1465db52009-09-23 17:17:35 -07003267 genExportPC(cUnit, mir);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003268 for (i=0; i < dInsn->vA; i++) {
Bill Buzbeec6f10662010-02-09 11:16:15 -08003269 loadValueDirect(cUnit, dvmCompilerGetSrc(cUnit, mir, i), i);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003270 }
Bill Buzbee1465db52009-09-23 17:17:35 -07003271 opReg(cUnit, kOpBlx, r4PC);
3272 opRegImm(cUnit, kOpAdd, r13, 8);
Bill Buzbeece46c942009-11-20 15:41:34 -08003273 opRegImm(cUnit, kOpCmp, r0, 0); /* NULL? */
3274 ArmLIR *branchOver = opCondBranch(cUnit, kArmCondNe);
3275 loadConstant(cUnit, r0,
3276 (int) (cUnit->method->insns + mir->offset));
3277 genDispatchToHandler(cUnit, TEMPLATE_THROW_EXCEPTION_COMMON);
3278 ArmLIR *target = newLIR0(cUnit, kArmPseudoTargetLabel);
3279 target->defMask = ENCODE_ALL;
3280 branchOver->generic.target = (LIR *) target;
Ben Chengba4fc8b2009-06-01 13:00:29 -07003281 break;
3282 }
3283 default:
3284 return true;
3285 }
3286 return false;
3287}
3288
3289static bool handleFmt51l(CompilationUnit *cUnit, MIR *mir)
3290{
Bill Buzbee1465db52009-09-23 17:17:35 -07003291 //TUNING: We're using core regs here - not optimal when target is a double
Bill Buzbeec6f10662010-02-09 11:16:15 -08003292 RegLocation rlDest = dvmCompilerGetDestWide(cUnit, mir, 0, 1);
3293 RegLocation rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Ben Chengbd1326d2010-04-02 15:04:53 -07003294 loadConstantNoClobber(cUnit, rlResult.lowReg,
3295 mir->dalvikInsn.vB_wide & 0xFFFFFFFFUL);
3296 loadConstantNoClobber(cUnit, rlResult.highReg,
3297 (mir->dalvikInsn.vB_wide>>32) & 0xFFFFFFFFUL);
Bill Buzbee1465db52009-09-23 17:17:35 -07003298 storeValueWide(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003299 return false;
3300}
3301
Ben Chengba4fc8b2009-06-01 13:00:29 -07003302/*
3303 * The following are special processing routines that handle transfer of
3304 * controls between compiled code and the interpreter. Certain VM states like
3305 * Dalvik PC and special-purpose registers are reconstructed here.
3306 */
3307
Bill Buzbeebd047242010-05-13 13:02:53 -07003308/*
3309 * Insert a
3310 * b .+4
3311 * nop
3312 * pair at the beginning of a chaining cell. This serves as the
3313 * switch branch that selects between reverting to the interpreter or
3314 * not. Once the cell is chained to a translation, the cell will
3315 * contain a 32-bit branch. Subsequent chain/unchain operations will
3316 * then only alter that first 16-bits - the "b .+4" for unchaining,
3317 * and the restoration of the first half of the 32-bit branch for
3318 * rechaining.
3319 */
3320static void insertChainingSwitch(CompilationUnit *cUnit)
3321{
3322 ArmLIR *branch = newLIR0(cUnit, kThumbBUncond);
3323 newLIR2(cUnit, kThumbOrr, r0, r0);
3324 ArmLIR *target = newLIR0(cUnit, kArmPseudoTargetLabel);
3325 target->defMask = ENCODE_ALL;
3326 branch->generic.target = (LIR *) target;
3327}
3328
Ben Cheng1efc9c52009-06-08 18:25:27 -07003329/* Chaining cell for code that may need warmup. */
3330static void handleNormalChainingCell(CompilationUnit *cUnit,
3331 unsigned int offset)
Ben Chengba4fc8b2009-06-01 13:00:29 -07003332{
Ben Cheng11d8f142010-03-24 15:24:19 -07003333 /*
3334 * Use raw instruction constructors to guarantee that the generated
3335 * instructions fit the predefined cell size.
3336 */
Bill Buzbeebd047242010-05-13 13:02:53 -07003337 insertChainingSwitch(cUnit);
Ben Cheng11d8f142010-03-24 15:24:19 -07003338 newLIR3(cUnit, kThumbLdrRRI5, r0, rGLUE,
3339 offsetof(InterpState,
3340 jitToInterpEntries.dvmJitToInterpNormal) >> 2);
3341 newLIR1(cUnit, kThumbBlxR, r0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003342 addWordData(cUnit, (int) (cUnit->method->insns + offset), true);
3343}
3344
3345/*
Ben Cheng1efc9c52009-06-08 18:25:27 -07003346 * Chaining cell for instructions that immediately following already translated
3347 * code.
Ben Chengba4fc8b2009-06-01 13:00:29 -07003348 */
Ben Cheng1efc9c52009-06-08 18:25:27 -07003349static void handleHotChainingCell(CompilationUnit *cUnit,
3350 unsigned int offset)
Ben Chengba4fc8b2009-06-01 13:00:29 -07003351{
Ben Cheng11d8f142010-03-24 15:24:19 -07003352 /*
3353 * Use raw instruction constructors to guarantee that the generated
3354 * instructions fit the predefined cell size.
3355 */
Bill Buzbeebd047242010-05-13 13:02:53 -07003356 insertChainingSwitch(cUnit);
Ben Cheng11d8f142010-03-24 15:24:19 -07003357 newLIR3(cUnit, kThumbLdrRRI5, r0, rGLUE,
3358 offsetof(InterpState,
3359 jitToInterpEntries.dvmJitToInterpTraceSelect) >> 2);
3360 newLIR1(cUnit, kThumbBlxR, r0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003361 addWordData(cUnit, (int) (cUnit->method->insns + offset), true);
3362}
3363
Bill Buzbee9c4b7c82009-09-10 10:10:38 -07003364#if defined(WITH_SELF_VERIFICATION) || defined(WITH_JIT_TUNING)
Jeff Hao97319a82009-08-12 16:57:15 -07003365/* Chaining cell for branches that branch back into the same basic block */
3366static void handleBackwardBranchChainingCell(CompilationUnit *cUnit,
3367 unsigned int offset)
3368{
Ben Cheng11d8f142010-03-24 15:24:19 -07003369 /*
3370 * Use raw instruction constructors to guarantee that the generated
3371 * instructions fit the predefined cell size.
3372 */
Bill Buzbeebd047242010-05-13 13:02:53 -07003373 insertChainingSwitch(cUnit);
Bill Buzbee9c4b7c82009-09-10 10:10:38 -07003374#if defined(WITH_SELF_VERIFICATION)
Bill Buzbee1465db52009-09-23 17:17:35 -07003375 newLIR3(cUnit, kThumbLdrRRI5, r0, rGLUE,
Ben Cheng40094c12010-02-24 20:58:44 -08003376 offsetof(InterpState,
3377 jitToInterpEntries.dvmJitToInterpBackwardBranch) >> 2);
Bill Buzbee9c4b7c82009-09-10 10:10:38 -07003378#else
Bill Buzbee1465db52009-09-23 17:17:35 -07003379 newLIR3(cUnit, kThumbLdrRRI5, r0, rGLUE,
Bill Buzbee9c4b7c82009-09-10 10:10:38 -07003380 offsetof(InterpState, jitToInterpEntries.dvmJitToInterpNormal) >> 2);
3381#endif
Bill Buzbee1465db52009-09-23 17:17:35 -07003382 newLIR1(cUnit, kThumbBlxR, r0);
Jeff Hao97319a82009-08-12 16:57:15 -07003383 addWordData(cUnit, (int) (cUnit->method->insns + offset), true);
3384}
3385
3386#endif
Ben Chengba4fc8b2009-06-01 13:00:29 -07003387/* Chaining cell for monomorphic method invocations. */
Ben Cheng38329f52009-07-07 14:19:20 -07003388static void handleInvokeSingletonChainingCell(CompilationUnit *cUnit,
3389 const Method *callee)
Ben Chengba4fc8b2009-06-01 13:00:29 -07003390{
Ben Cheng11d8f142010-03-24 15:24:19 -07003391 /*
3392 * Use raw instruction constructors to guarantee that the generated
3393 * instructions fit the predefined cell size.
3394 */
Bill Buzbeebd047242010-05-13 13:02:53 -07003395 insertChainingSwitch(cUnit);
Ben Cheng11d8f142010-03-24 15:24:19 -07003396 newLIR3(cUnit, kThumbLdrRRI5, r0, rGLUE,
3397 offsetof(InterpState,
3398 jitToInterpEntries.dvmJitToInterpTraceSelect) >> 2);
3399 newLIR1(cUnit, kThumbBlxR, r0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003400 addWordData(cUnit, (int) (callee->insns), true);
3401}
3402
Ben Cheng38329f52009-07-07 14:19:20 -07003403/* Chaining cell for monomorphic method invocations. */
3404static void handleInvokePredictedChainingCell(CompilationUnit *cUnit)
3405{
3406
3407 /* Should not be executed in the initial state */
3408 addWordData(cUnit, PREDICTED_CHAIN_BX_PAIR_INIT, true);
3409 /* To be filled: class */
3410 addWordData(cUnit, PREDICTED_CHAIN_CLAZZ_INIT, true);
3411 /* To be filled: method */
3412 addWordData(cUnit, PREDICTED_CHAIN_METHOD_INIT, true);
3413 /*
3414 * Rechain count. The initial value of 0 here will trigger chaining upon
3415 * the first invocation of this callsite.
3416 */
3417 addWordData(cUnit, PREDICTED_CHAIN_COUNTER_INIT, true);
3418}
3419
Ben Chengba4fc8b2009-06-01 13:00:29 -07003420/* Load the Dalvik PC into r0 and jump to the specified target */
3421static void handlePCReconstruction(CompilationUnit *cUnit,
Bill Buzbee89efc3d2009-07-28 11:22:22 -07003422 ArmLIR *targetLabel)
Ben Chengba4fc8b2009-06-01 13:00:29 -07003423{
Bill Buzbee89efc3d2009-07-28 11:22:22 -07003424 ArmLIR **pcrLabel =
3425 (ArmLIR **) cUnit->pcReconstructionList.elemList;
Ben Chengba4fc8b2009-06-01 13:00:29 -07003426 int numElems = cUnit->pcReconstructionList.numUsed;
3427 int i;
3428 for (i = 0; i < numElems; i++) {
3429 dvmCompilerAppendLIR(cUnit, (LIR *) pcrLabel[i]);
3430 /* r0 = dalvik PC */
3431 loadConstant(cUnit, r0, pcrLabel[i]->operands[0]);
3432 genUnconditionalBranch(cUnit, targetLabel);
3433 }
3434}
3435
Bill Buzbee1465db52009-09-23 17:17:35 -07003436static char *extendedMIROpNames[kMirOpLast - kMirOpFirst] = {
3437 "kMirOpPhi",
3438 "kMirOpNullNRangeUpCheck",
3439 "kMirOpNullNRangeDownCheck",
3440 "kMirOpLowerBound",
3441 "kMirOpPunt",
Ben Cheng4238ec22009-08-24 16:32:22 -07003442};
3443
3444/*
3445 * vA = arrayReg;
3446 * vB = idxReg;
3447 * vC = endConditionReg;
3448 * arg[0] = maxC
3449 * arg[1] = minC
3450 * arg[2] = loopBranchConditionCode
3451 */
3452static void genHoistedChecksForCountUpLoop(CompilationUnit *cUnit, MIR *mir)
3453{
Bill Buzbee1465db52009-09-23 17:17:35 -07003454 /*
3455 * NOTE: these synthesized blocks don't have ssa names assigned
3456 * for Dalvik registers. However, because they dominate the following
3457 * blocks we can simply use the Dalvik name w/ subscript 0 as the
3458 * ssa name.
3459 */
Ben Cheng4238ec22009-08-24 16:32:22 -07003460 DecodedInstruction *dInsn = &mir->dalvikInsn;
3461 const int lenOffset = offsetof(ArrayObject, length);
Ben Cheng4238ec22009-08-24 16:32:22 -07003462 const int maxC = dInsn->arg[0];
Bill Buzbee1465db52009-09-23 17:17:35 -07003463 int regLength;
3464 RegLocation rlArray = cUnit->regLocation[mir->dalvikInsn.vA];
3465 RegLocation rlIdxEnd = cUnit->regLocation[mir->dalvikInsn.vC];
Ben Cheng4238ec22009-08-24 16:32:22 -07003466
3467 /* regArray <- arrayRef */
Bill Buzbee1465db52009-09-23 17:17:35 -07003468 rlArray = loadValue(cUnit, rlArray, kCoreReg);
3469 rlIdxEnd = loadValue(cUnit, rlIdxEnd, kCoreReg);
3470 genRegImmCheck(cUnit, kArmCondEq, rlArray.lowReg, 0, 0,
Ben Cheng4238ec22009-08-24 16:32:22 -07003471 (ArmLIR *) cUnit->loopAnalysis->branchToPCR);
3472
3473 /* regLength <- len(arrayRef) */
Bill Buzbeec6f10662010-02-09 11:16:15 -08003474 regLength = dvmCompilerAllocTemp(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07003475 loadWordDisp(cUnit, rlArray.lowReg, lenOffset, regLength);
Ben Cheng4238ec22009-08-24 16:32:22 -07003476
3477 int delta = maxC;
3478 /*
3479 * If the loop end condition is ">=" instead of ">", then the largest value
3480 * of the index is "endCondition - 1".
3481 */
3482 if (dInsn->arg[2] == OP_IF_GE) {
3483 delta--;
3484 }
3485
3486 if (delta) {
Bill Buzbeec6f10662010-02-09 11:16:15 -08003487 int tReg = dvmCompilerAllocTemp(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07003488 opRegRegImm(cUnit, kOpAdd, tReg, rlIdxEnd.lowReg, delta);
3489 rlIdxEnd.lowReg = tReg;
Bill Buzbeec6f10662010-02-09 11:16:15 -08003490 dvmCompilerFreeTemp(cUnit, tReg);
Ben Cheng4238ec22009-08-24 16:32:22 -07003491 }
3492 /* Punt if "regIdxEnd < len(Array)" is false */
Bill Buzbee1465db52009-09-23 17:17:35 -07003493 genRegRegCheck(cUnit, kArmCondGe, rlIdxEnd.lowReg, regLength, 0,
Ben Cheng0fd31e42009-09-03 14:40:16 -07003494 (ArmLIR *) cUnit->loopAnalysis->branchToPCR);
Ben Cheng4238ec22009-08-24 16:32:22 -07003495}
3496
3497/*
3498 * vA = arrayReg;
3499 * vB = idxReg;
3500 * vC = endConditionReg;
3501 * arg[0] = maxC
3502 * arg[1] = minC
3503 * arg[2] = loopBranchConditionCode
3504 */
3505static void genHoistedChecksForCountDownLoop(CompilationUnit *cUnit, MIR *mir)
3506{
3507 DecodedInstruction *dInsn = &mir->dalvikInsn;
3508 const int lenOffset = offsetof(ArrayObject, length);
Bill Buzbeec6f10662010-02-09 11:16:15 -08003509 const int regLength = dvmCompilerAllocTemp(cUnit);
Ben Cheng4238ec22009-08-24 16:32:22 -07003510 const int maxC = dInsn->arg[0];
Bill Buzbee1465db52009-09-23 17:17:35 -07003511 RegLocation rlArray = cUnit->regLocation[mir->dalvikInsn.vA];
3512 RegLocation rlIdxInit = cUnit->regLocation[mir->dalvikInsn.vB];
Ben Cheng4238ec22009-08-24 16:32:22 -07003513
3514 /* regArray <- arrayRef */
Bill Buzbee1465db52009-09-23 17:17:35 -07003515 rlArray = loadValue(cUnit, rlArray, kCoreReg);
3516 rlIdxInit = loadValue(cUnit, rlIdxInit, kCoreReg);
3517 genRegImmCheck(cUnit, kArmCondEq, rlArray.lowReg, 0, 0,
Ben Cheng4238ec22009-08-24 16:32:22 -07003518 (ArmLIR *) cUnit->loopAnalysis->branchToPCR);
3519
3520 /* regLength <- len(arrayRef) */
Bill Buzbee1465db52009-09-23 17:17:35 -07003521 loadWordDisp(cUnit, rlArray.lowReg, lenOffset, regLength);
Ben Cheng4238ec22009-08-24 16:32:22 -07003522
3523 if (maxC) {
Bill Buzbeec6f10662010-02-09 11:16:15 -08003524 int tReg = dvmCompilerAllocTemp(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07003525 opRegRegImm(cUnit, kOpAdd, tReg, rlIdxInit.lowReg, maxC);
3526 rlIdxInit.lowReg = tReg;
Bill Buzbeec6f10662010-02-09 11:16:15 -08003527 dvmCompilerFreeTemp(cUnit, tReg);
Ben Cheng4238ec22009-08-24 16:32:22 -07003528 }
3529
3530 /* Punt if "regIdxInit < len(Array)" is false */
Bill Buzbee1465db52009-09-23 17:17:35 -07003531 genRegRegCheck(cUnit, kArmCondGe, rlIdxInit.lowReg, regLength, 0,
Ben Cheng0fd31e42009-09-03 14:40:16 -07003532 (ArmLIR *) cUnit->loopAnalysis->branchToPCR);
Ben Cheng4238ec22009-08-24 16:32:22 -07003533}
3534
3535/*
3536 * vA = idxReg;
3537 * vB = minC;
3538 */
3539static void genHoistedLowerBoundCheck(CompilationUnit *cUnit, MIR *mir)
3540{
3541 DecodedInstruction *dInsn = &mir->dalvikInsn;
Ben Cheng4238ec22009-08-24 16:32:22 -07003542 const int minC = dInsn->vB;
Bill Buzbee1465db52009-09-23 17:17:35 -07003543 RegLocation rlIdx = cUnit->regLocation[mir->dalvikInsn.vA];
Ben Cheng4238ec22009-08-24 16:32:22 -07003544
3545 /* regIdx <- initial index value */
Bill Buzbee1465db52009-09-23 17:17:35 -07003546 rlIdx = loadValue(cUnit, rlIdx, kCoreReg);
Ben Cheng4238ec22009-08-24 16:32:22 -07003547
3548 /* Punt if "regIdxInit + minC >= 0" is false */
Bill Buzbee1465db52009-09-23 17:17:35 -07003549 genRegImmCheck(cUnit, kArmCondLt, rlIdx.lowReg, -minC, 0,
Ben Cheng4238ec22009-08-24 16:32:22 -07003550 (ArmLIR *) cUnit->loopAnalysis->branchToPCR);
3551}
3552
3553/* Extended MIR instructions like PHI */
3554static void handleExtendedMIR(CompilationUnit *cUnit, MIR *mir)
3555{
Bill Buzbee1465db52009-09-23 17:17:35 -07003556 int opOffset = mir->dalvikInsn.opCode - kMirOpFirst;
Ben Cheng4238ec22009-08-24 16:32:22 -07003557 char *msg = dvmCompilerNew(strlen(extendedMIROpNames[opOffset]) + 1,
3558 false);
3559 strcpy(msg, extendedMIROpNames[opOffset]);
Bill Buzbee1465db52009-09-23 17:17:35 -07003560 newLIR1(cUnit, kArmPseudoExtended, (int) msg);
Ben Cheng4238ec22009-08-24 16:32:22 -07003561
3562 switch (mir->dalvikInsn.opCode) {
Bill Buzbee1465db52009-09-23 17:17:35 -07003563 case kMirOpPhi: {
Ben Cheng4238ec22009-08-24 16:32:22 -07003564 char *ssaString = dvmCompilerGetSSAString(cUnit, mir->ssaRep);
Bill Buzbee1465db52009-09-23 17:17:35 -07003565 newLIR1(cUnit, kArmPseudoSSARep, (int) ssaString);
Ben Cheng4238ec22009-08-24 16:32:22 -07003566 break;
3567 }
Bill Buzbee1465db52009-09-23 17:17:35 -07003568 case kMirOpNullNRangeUpCheck: {
Ben Cheng4238ec22009-08-24 16:32:22 -07003569 genHoistedChecksForCountUpLoop(cUnit, mir);
3570 break;
3571 }
Bill Buzbee1465db52009-09-23 17:17:35 -07003572 case kMirOpNullNRangeDownCheck: {
Ben Cheng4238ec22009-08-24 16:32:22 -07003573 genHoistedChecksForCountDownLoop(cUnit, mir);
3574 break;
3575 }
Bill Buzbee1465db52009-09-23 17:17:35 -07003576 case kMirOpLowerBound: {
Ben Cheng4238ec22009-08-24 16:32:22 -07003577 genHoistedLowerBoundCheck(cUnit, mir);
3578 break;
3579 }
Bill Buzbee1465db52009-09-23 17:17:35 -07003580 case kMirOpPunt: {
Ben Cheng4238ec22009-08-24 16:32:22 -07003581 genUnconditionalBranch(cUnit,
3582 (ArmLIR *) cUnit->loopAnalysis->branchToPCR);
3583 break;
3584 }
3585 default:
3586 break;
3587 }
3588}
3589
3590/*
3591 * Create a PC-reconstruction cell for the starting offset of this trace.
3592 * Since the PCR cell is placed near the end of the compiled code which is
3593 * usually out of range for a conditional branch, we put two branches (one
3594 * branch over to the loop body and one layover branch to the actual PCR) at the
3595 * end of the entry block.
3596 */
3597static void setupLoopEntryBlock(CompilationUnit *cUnit, BasicBlock *entry,
3598 ArmLIR *bodyLabel)
3599{
3600 /* Set up the place holder to reconstruct this Dalvik PC */
3601 ArmLIR *pcrLabel = dvmCompilerNew(sizeof(ArmLIR), true);
Ben Chenga4973592010-03-31 11:59:18 -07003602 pcrLabel->opCode = kArmPseudoPCReconstructionCell;
Ben Cheng4238ec22009-08-24 16:32:22 -07003603 pcrLabel->operands[0] =
3604 (int) (cUnit->method->insns + entry->startOffset);
3605 pcrLabel->operands[1] = entry->startOffset;
3606 /* Insert the place holder to the growable list */
3607 dvmInsertGrowableList(&cUnit->pcReconstructionList, pcrLabel);
3608
3609 /*
3610 * Next, create two branches - one branch over to the loop body and the
3611 * other branch to the PCR cell to punt.
3612 */
3613 ArmLIR *branchToBody = dvmCompilerNew(sizeof(ArmLIR), true);
Bill Buzbee1465db52009-09-23 17:17:35 -07003614 branchToBody->opCode = kThumbBUncond;
Ben Cheng4238ec22009-08-24 16:32:22 -07003615 branchToBody->generic.target = (LIR *) bodyLabel;
Ben Chengdcf3e5d2009-09-11 13:42:05 -07003616 setupResourceMasks(branchToBody);
Ben Cheng4238ec22009-08-24 16:32:22 -07003617 cUnit->loopAnalysis->branchToBody = (LIR *) branchToBody;
3618
3619 ArmLIR *branchToPCR = dvmCompilerNew(sizeof(ArmLIR), true);
Bill Buzbee1465db52009-09-23 17:17:35 -07003620 branchToPCR->opCode = kThumbBUncond;
Ben Cheng4238ec22009-08-24 16:32:22 -07003621 branchToPCR->generic.target = (LIR *) pcrLabel;
Ben Chengdcf3e5d2009-09-11 13:42:05 -07003622 setupResourceMasks(branchToPCR);
Ben Cheng4238ec22009-08-24 16:32:22 -07003623 cUnit->loopAnalysis->branchToPCR = (LIR *) branchToPCR;
3624}
3625
Ben Chengd5adae12010-03-26 17:45:28 -07003626#if defined(WITH_SELF_VERIFICATION)
3627static bool selfVerificationPuntOps(MIR *mir)
3628{
3629 DecodedInstruction *decInsn = &mir->dalvikInsn;
3630 OpCode op = decInsn->opCode;
3631 int flags = dexGetInstrFlags(gDvm.instrFlags, op);
3632 /*
3633 * All opcodes that can throw exceptions and use the
3634 * TEMPLATE_THROW_EXCEPTION_COMMON template should be excluded in the trace
3635 * under self-verification mode.
3636 */
3637 return (op == OP_MONITOR_ENTER || op == OP_MONITOR_EXIT ||
3638 op == OP_NEW_INSTANCE || op == OP_NEW_ARRAY ||
3639 op == OP_CHECK_CAST || op == OP_MOVE_EXCEPTION ||
3640 op == OP_FILL_ARRAY_DATA || op == OP_EXECUTE_INLINE ||
3641 op == OP_EXECUTE_INLINE_RANGE ||
3642 (flags & kInstrInvoke));
3643}
3644#endif
3645
Ben Chengba4fc8b2009-06-01 13:00:29 -07003646void dvmCompilerMIR2LIR(CompilationUnit *cUnit)
3647{
3648 /* Used to hold the labels of each block */
Bill Buzbee89efc3d2009-07-28 11:22:22 -07003649 ArmLIR *labelList =
3650 dvmCompilerNew(sizeof(ArmLIR) * cUnit->numBlocks, true);
Ben Chengcec26f62010-01-15 15:29:33 -08003651 GrowableList chainingListByType[kChainingCellGap];
Ben Chengba4fc8b2009-06-01 13:00:29 -07003652 int i;
3653
3654 /*
Ben Cheng38329f52009-07-07 14:19:20 -07003655 * Initialize various types chaining lists.
Ben Chengba4fc8b2009-06-01 13:00:29 -07003656 */
Ben Chengcec26f62010-01-15 15:29:33 -08003657 for (i = 0; i < kChainingCellGap; i++) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07003658 dvmInitGrowableList(&chainingListByType[i], 2);
3659 }
3660
3661 BasicBlock **blockList = cUnit->blockList;
3662
Bill Buzbee6e963e12009-06-17 16:56:19 -07003663 if (cUnit->executionCount) {
3664 /*
3665 * Reserve 6 bytes at the beginning of the trace
3666 * +----------------------------+
3667 * | execution count (4 bytes) |
3668 * +----------------------------+
3669 * | chain cell offset (2 bytes)|
3670 * +----------------------------+
3671 * ...and then code to increment the execution
3672 * count:
3673 * mov r0, pc @ move adr of "mov r0,pc" + 4 to r0
3674 * sub r0, #10 @ back up to addr of executionCount
3675 * ldr r1, [r0]
3676 * add r1, #1
3677 * str r1, [r0]
3678 */
Bill Buzbee1465db52009-09-23 17:17:35 -07003679 newLIR1(cUnit, kArm16BitData, 0);
3680 newLIR1(cUnit, kArm16BitData, 0);
Ben Chengcc6600c2009-06-22 14:45:16 -07003681 cUnit->chainCellOffsetLIR =
Bill Buzbee1465db52009-09-23 17:17:35 -07003682 (LIR *) newLIR1(cUnit, kArm16BitData, CHAIN_CELL_OFFSET_TAG);
Bill Buzbee6e963e12009-06-17 16:56:19 -07003683 cUnit->headerSize = 6;
Bill Buzbee270c1d62009-08-13 16:58:07 -07003684 /* Thumb instruction used directly here to ensure correct size */
Bill Buzbee1465db52009-09-23 17:17:35 -07003685 newLIR2(cUnit, kThumbMovRR_H2L, r0, rpc);
3686 newLIR2(cUnit, kThumbSubRI8, r0, 10);
3687 newLIR3(cUnit, kThumbLdrRRI5, r1, r0, 0);
3688 newLIR2(cUnit, kThumbAddRI8, r1, 1);
3689 newLIR3(cUnit, kThumbStrRRI5, r1, r0, 0);
Bill Buzbee6e963e12009-06-17 16:56:19 -07003690 } else {
3691 /* Just reserve 2 bytes for the chain cell offset */
Ben Chengcc6600c2009-06-22 14:45:16 -07003692 cUnit->chainCellOffsetLIR =
Bill Buzbee1465db52009-09-23 17:17:35 -07003693 (LIR *) newLIR1(cUnit, kArm16BitData, CHAIN_CELL_OFFSET_TAG);
Bill Buzbee6e963e12009-06-17 16:56:19 -07003694 cUnit->headerSize = 2;
3695 }
Ben Cheng1efc9c52009-06-08 18:25:27 -07003696
Ben Chengba4fc8b2009-06-01 13:00:29 -07003697 /* Handle the content in each basic block */
3698 for (i = 0; i < cUnit->numBlocks; i++) {
3699 blockList[i]->visited = true;
3700 MIR *mir;
3701
3702 labelList[i].operands[0] = blockList[i]->startOffset;
3703
Ben Chengcec26f62010-01-15 15:29:33 -08003704 if (blockList[i]->blockType >= kChainingCellGap) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07003705 /*
3706 * Append the label pseudo LIR first. Chaining cells will be handled
3707 * separately afterwards.
3708 */
3709 dvmCompilerAppendLIR(cUnit, (LIR *) &labelList[i]);
3710 }
3711
Bill Buzbee1465db52009-09-23 17:17:35 -07003712 if (blockList[i]->blockType == kEntryBlock) {
Ben Chenga4973592010-03-31 11:59:18 -07003713 labelList[i].opCode = kArmPseudoEntryBlock;
Ben Cheng4238ec22009-08-24 16:32:22 -07003714 if (blockList[i]->firstMIRInsn == NULL) {
3715 continue;
3716 } else {
3717 setupLoopEntryBlock(cUnit, blockList[i],
3718 &labelList[blockList[i]->fallThrough->id]);
3719 }
Bill Buzbee1465db52009-09-23 17:17:35 -07003720 } else if (blockList[i]->blockType == kExitBlock) {
Ben Chenga4973592010-03-31 11:59:18 -07003721 labelList[i].opCode = kArmPseudoExitBlock;
Ben Cheng4238ec22009-08-24 16:32:22 -07003722 goto gen_fallthrough;
Bill Buzbee1465db52009-09-23 17:17:35 -07003723 } else if (blockList[i]->blockType == kDalvikByteCode) {
3724 labelList[i].opCode = kArmPseudoNormalBlockLabel;
Ben Chenge9695e52009-06-16 16:11:47 -07003725 /* Reset the register state */
Bill Buzbeec6f10662010-02-09 11:16:15 -08003726 dvmCompilerResetRegPool(cUnit);
3727 dvmCompilerClobberAllRegs(cUnit);
3728 dvmCompilerResetNullCheck(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003729 } else {
3730 switch (blockList[i]->blockType) {
Bill Buzbee1465db52009-09-23 17:17:35 -07003731 case kChainingCellNormal:
Ben Chenga4973592010-03-31 11:59:18 -07003732 labelList[i].opCode = kArmPseudoChainingCellNormal;
Ben Chengba4fc8b2009-06-01 13:00:29 -07003733 /* handle the codegen later */
3734 dvmInsertGrowableList(
Bill Buzbee1465db52009-09-23 17:17:35 -07003735 &chainingListByType[kChainingCellNormal], (void *) i);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003736 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07003737 case kChainingCellInvokeSingleton:
Ben Cheng38329f52009-07-07 14:19:20 -07003738 labelList[i].opCode =
Ben Chenga4973592010-03-31 11:59:18 -07003739 kArmPseudoChainingCellInvokeSingleton;
Ben Chengba4fc8b2009-06-01 13:00:29 -07003740 labelList[i].operands[0] =
3741 (int) blockList[i]->containingMethod;
3742 /* handle the codegen later */
3743 dvmInsertGrowableList(
Bill Buzbee1465db52009-09-23 17:17:35 -07003744 &chainingListByType[kChainingCellInvokeSingleton],
Ben Cheng38329f52009-07-07 14:19:20 -07003745 (void *) i);
3746 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07003747 case kChainingCellInvokePredicted:
Ben Cheng38329f52009-07-07 14:19:20 -07003748 labelList[i].opCode =
Ben Chenga4973592010-03-31 11:59:18 -07003749 kArmPseudoChainingCellInvokePredicted;
Ben Cheng38329f52009-07-07 14:19:20 -07003750 /* handle the codegen later */
3751 dvmInsertGrowableList(
Bill Buzbee1465db52009-09-23 17:17:35 -07003752 &chainingListByType[kChainingCellInvokePredicted],
Ben Cheng38329f52009-07-07 14:19:20 -07003753 (void *) i);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003754 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07003755 case kChainingCellHot:
Ben Chengba4fc8b2009-06-01 13:00:29 -07003756 labelList[i].opCode =
Ben Chenga4973592010-03-31 11:59:18 -07003757 kArmPseudoChainingCellHot;
Ben Chengba4fc8b2009-06-01 13:00:29 -07003758 /* handle the codegen later */
3759 dvmInsertGrowableList(
Bill Buzbee1465db52009-09-23 17:17:35 -07003760 &chainingListByType[kChainingCellHot],
Ben Chengba4fc8b2009-06-01 13:00:29 -07003761 (void *) i);
3762 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07003763 case kPCReconstruction:
Ben Chengba4fc8b2009-06-01 13:00:29 -07003764 /* Make sure exception handling block is next */
3765 labelList[i].opCode =
Ben Chenga4973592010-03-31 11:59:18 -07003766 kArmPseudoPCReconstructionBlockLabel;
Ben Chengba4fc8b2009-06-01 13:00:29 -07003767 assert (i == cUnit->numBlocks - 2);
3768 handlePCReconstruction(cUnit, &labelList[i+1]);
3769 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07003770 case kExceptionHandling:
3771 labelList[i].opCode = kArmPseudoEHBlockLabel;
Ben Chengba4fc8b2009-06-01 13:00:29 -07003772 if (cUnit->pcReconstructionList.numUsed) {
Bill Buzbee270c1d62009-08-13 16:58:07 -07003773 loadWordDisp(cUnit, rGLUE, offsetof(InterpState,
3774 jitToInterpEntries.dvmJitToInterpPunt),
3775 r1);
Bill Buzbee1465db52009-09-23 17:17:35 -07003776 opReg(cUnit, kOpBlx, r1);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003777 }
3778 break;
Bill Buzbee9c4b7c82009-09-10 10:10:38 -07003779#if defined(WITH_SELF_VERIFICATION) || defined(WITH_JIT_TUNING)
Bill Buzbee1465db52009-09-23 17:17:35 -07003780 case kChainingCellBackwardBranch:
Jeff Hao97319a82009-08-12 16:57:15 -07003781 labelList[i].opCode =
Ben Chenga4973592010-03-31 11:59:18 -07003782 kArmPseudoChainingCellBackwardBranch;
Jeff Hao97319a82009-08-12 16:57:15 -07003783 /* handle the codegen later */
3784 dvmInsertGrowableList(
Bill Buzbee1465db52009-09-23 17:17:35 -07003785 &chainingListByType[kChainingCellBackwardBranch],
Jeff Hao97319a82009-08-12 16:57:15 -07003786 (void *) i);
3787 break;
3788#endif
Ben Chengba4fc8b2009-06-01 13:00:29 -07003789 default:
3790 break;
3791 }
3792 continue;
3793 }
Ben Chenge9695e52009-06-16 16:11:47 -07003794
Bill Buzbee89efc3d2009-07-28 11:22:22 -07003795 ArmLIR *headLIR = NULL;
Ben Chenge9695e52009-06-16 16:11:47 -07003796
Ben Chengba4fc8b2009-06-01 13:00:29 -07003797 for (mir = blockList[i]->firstMIRInsn; mir; mir = mir->next) {
Bill Buzbee1465db52009-09-23 17:17:35 -07003798
Bill Buzbeec6f10662010-02-09 11:16:15 -08003799 dvmCompilerResetRegPool(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07003800 if (gDvmJit.disableOpt & (1 << kTrackLiveTemps)) {
Bill Buzbeec6f10662010-02-09 11:16:15 -08003801 dvmCompilerClobberAllRegs(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07003802 }
3803
3804 if (gDvmJit.disableOpt & (1 << kSuppressLoads)) {
Bill Buzbeec6f10662010-02-09 11:16:15 -08003805 dvmCompilerResetDefTracking(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07003806 }
3807
3808 if (mir->dalvikInsn.opCode >= kMirOpFirst) {
Ben Cheng4238ec22009-08-24 16:32:22 -07003809 handleExtendedMIR(cUnit, mir);
3810 continue;
3811 }
3812
Bill Buzbee1465db52009-09-23 17:17:35 -07003813
Ben Chengba4fc8b2009-06-01 13:00:29 -07003814 OpCode dalvikOpCode = mir->dalvikInsn.opCode;
3815 InstructionFormat dalvikFormat =
3816 dexGetInstrFormat(gDvm.instrFormat, dalvikOpCode);
Bill Buzbee89efc3d2009-07-28 11:22:22 -07003817 ArmLIR *boundaryLIR =
Ben Chenga4973592010-03-31 11:59:18 -07003818 newLIR2(cUnit, kArmPseudoDalvikByteCodeBoundary,
Ben Chengccd6c012009-10-15 14:52:45 -07003819 mir->offset,
3820 (int) dvmCompilerGetDalvikDisassembly(&mir->dalvikInsn)
3821 );
Ben Cheng4238ec22009-08-24 16:32:22 -07003822 if (mir->ssaRep) {
3823 char *ssaString = dvmCompilerGetSSAString(cUnit, mir->ssaRep);
Bill Buzbee1465db52009-09-23 17:17:35 -07003824 newLIR1(cUnit, kArmPseudoSSARep, (int) ssaString);
Ben Cheng4238ec22009-08-24 16:32:22 -07003825 }
3826
Ben Chenge9695e52009-06-16 16:11:47 -07003827 /* Remember the first LIR for this block */
3828 if (headLIR == NULL) {
3829 headLIR = boundaryLIR;
Ben Chengd7d426a2009-09-22 11:23:36 -07003830 /* Set the first boundaryLIR as a scheduling barrier */
3831 headLIR->defMask = ENCODE_ALL;
Ben Chenge9695e52009-06-16 16:11:47 -07003832 }
Ben Cheng4238ec22009-08-24 16:32:22 -07003833
Ben Chengba4fc8b2009-06-01 13:00:29 -07003834 bool notHandled;
3835 /*
3836 * Debugging: screen the opcode first to see if it is in the
3837 * do[-not]-compile list
3838 */
3839 bool singleStepMe =
3840 gDvmJit.includeSelectedOp !=
3841 ((gDvmJit.opList[dalvikOpCode >> 3] &
3842 (1 << (dalvikOpCode & 0x7))) !=
3843 0);
Ben Chengd5adae12010-03-26 17:45:28 -07003844#if defined(WITH_SELF_VERIFICATION)
3845 if (singleStepMe == false) {
3846 singleStepMe = selfVerificationPuntOps(mir);
3847 }
3848#endif
Ben Chengba4fc8b2009-06-01 13:00:29 -07003849 if (singleStepMe || cUnit->allSingleStep) {
3850 notHandled = false;
3851 genInterpSingleStep(cUnit, mir);
3852 } else {
3853 opcodeCoverage[dalvikOpCode]++;
3854 switch (dalvikFormat) {
3855 case kFmt10t:
3856 case kFmt20t:
3857 case kFmt30t:
3858 notHandled = handleFmt10t_Fmt20t_Fmt30t(cUnit,
3859 mir, blockList[i], labelList);
3860 break;
3861 case kFmt10x:
3862 notHandled = handleFmt10x(cUnit, mir);
3863 break;
3864 case kFmt11n:
3865 case kFmt31i:
3866 notHandled = handleFmt11n_Fmt31i(cUnit, mir);
3867 break;
3868 case kFmt11x:
3869 notHandled = handleFmt11x(cUnit, mir);
3870 break;
3871 case kFmt12x:
3872 notHandled = handleFmt12x(cUnit, mir);
3873 break;
3874 case kFmt20bc:
3875 notHandled = handleFmt20bc(cUnit, mir);
3876 break;
3877 case kFmt21c:
3878 case kFmt31c:
3879 notHandled = handleFmt21c_Fmt31c(cUnit, mir);
3880 break;
3881 case kFmt21h:
3882 notHandled = handleFmt21h(cUnit, mir);
3883 break;
3884 case kFmt21s:
3885 notHandled = handleFmt21s(cUnit, mir);
3886 break;
3887 case kFmt21t:
3888 notHandled = handleFmt21t(cUnit, mir, blockList[i],
3889 labelList);
3890 break;
3891 case kFmt22b:
3892 case kFmt22s:
3893 notHandled = handleFmt22b_Fmt22s(cUnit, mir);
3894 break;
3895 case kFmt22c:
3896 notHandled = handleFmt22c(cUnit, mir);
3897 break;
3898 case kFmt22cs:
3899 notHandled = handleFmt22cs(cUnit, mir);
3900 break;
3901 case kFmt22t:
3902 notHandled = handleFmt22t(cUnit, mir, blockList[i],
3903 labelList);
3904 break;
3905 case kFmt22x:
3906 case kFmt32x:
3907 notHandled = handleFmt22x_Fmt32x(cUnit, mir);
3908 break;
3909 case kFmt23x:
3910 notHandled = handleFmt23x(cUnit, mir);
3911 break;
3912 case kFmt31t:
3913 notHandled = handleFmt31t(cUnit, mir);
3914 break;
3915 case kFmt3rc:
3916 case kFmt35c:
3917 notHandled = handleFmt35c_3rc(cUnit, mir, blockList[i],
3918 labelList);
3919 break;
3920 case kFmt3rms:
3921 case kFmt35ms:
3922 notHandled = handleFmt35ms_3rms(cUnit, mir,blockList[i],
3923 labelList);
3924 break;
3925 case kFmt3inline:
Andy McFaddenb0a05412009-11-19 10:23:41 -08003926 case kFmt3rinline:
Bill Buzbeece46c942009-11-20 15:41:34 -08003927 notHandled = handleExecuteInline(cUnit, mir);
Andy McFaddenb0a05412009-11-19 10:23:41 -08003928 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07003929 case kFmt51l:
3930 notHandled = handleFmt51l(cUnit, mir);
3931 break;
3932 default:
3933 notHandled = true;
3934 break;
3935 }
3936 }
3937 if (notHandled) {
3938 LOGE("%#06x: Opcode 0x%x (%s) / Fmt %d not handled\n",
3939 mir->offset,
3940 dalvikOpCode, getOpcodeName(dalvikOpCode),
3941 dalvikFormat);
Bill Buzbeefc519dc2010-03-06 23:30:57 -08003942 dvmCompilerAbort(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003943 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07003944 }
3945 }
Ben Cheng4238ec22009-08-24 16:32:22 -07003946
Bill Buzbee1465db52009-09-23 17:17:35 -07003947 if (blockList[i]->blockType == kEntryBlock) {
Ben Cheng4238ec22009-08-24 16:32:22 -07003948 dvmCompilerAppendLIR(cUnit,
3949 (LIR *) cUnit->loopAnalysis->branchToBody);
3950 dvmCompilerAppendLIR(cUnit,
3951 (LIR *) cUnit->loopAnalysis->branchToPCR);
3952 }
3953
3954 if (headLIR) {
3955 /*
3956 * Eliminate redundant loads/stores and delay stores into later
3957 * slots
3958 */
3959 dvmCompilerApplyLocalOptimizations(cUnit, (LIR *) headLIR,
3960 cUnit->lastLIRInsn);
3961 }
3962
3963gen_fallthrough:
Ben Cheng1efc9c52009-06-08 18:25:27 -07003964 /*
3965 * Check if the block is terminated due to trace length constraint -
3966 * insert an unconditional branch to the chaining cell.
3967 */
3968 if (blockList[i]->needFallThroughBranch) {
3969 genUnconditionalBranch(cUnit,
3970 &labelList[blockList[i]->fallThrough->id]);
3971 }
3972
Ben Chengba4fc8b2009-06-01 13:00:29 -07003973 }
3974
Ben Chenge9695e52009-06-16 16:11:47 -07003975 /* Handle the chaining cells in predefined order */
Ben Chengcec26f62010-01-15 15:29:33 -08003976 for (i = 0; i < kChainingCellGap; i++) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07003977 size_t j;
3978 int *blockIdList = (int *) chainingListByType[i].elemList;
3979
3980 cUnit->numChainingCells[i] = chainingListByType[i].numUsed;
3981
3982 /* No chaining cells of this type */
3983 if (cUnit->numChainingCells[i] == 0)
3984 continue;
3985
3986 /* Record the first LIR for a new type of chaining cell */
3987 cUnit->firstChainingLIR[i] = (LIR *) &labelList[blockIdList[0]];
3988
3989 for (j = 0; j < chainingListByType[i].numUsed; j++) {
3990 int blockId = blockIdList[j];
3991
3992 /* Align this chaining cell first */
Bill Buzbee1465db52009-09-23 17:17:35 -07003993 newLIR0(cUnit, kArmPseudoPseudoAlign4);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003994
3995 /* Insert the pseudo chaining instruction */
3996 dvmCompilerAppendLIR(cUnit, (LIR *) &labelList[blockId]);
3997
3998
3999 switch (blockList[blockId]->blockType) {
Bill Buzbee1465db52009-09-23 17:17:35 -07004000 case kChainingCellNormal:
Ben Cheng1efc9c52009-06-08 18:25:27 -07004001 handleNormalChainingCell(cUnit,
Ben Chengba4fc8b2009-06-01 13:00:29 -07004002 blockList[blockId]->startOffset);
4003 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07004004 case kChainingCellInvokeSingleton:
Ben Cheng38329f52009-07-07 14:19:20 -07004005 handleInvokeSingletonChainingCell(cUnit,
Ben Chengba4fc8b2009-06-01 13:00:29 -07004006 blockList[blockId]->containingMethod);
4007 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07004008 case kChainingCellInvokePredicted:
Ben Cheng38329f52009-07-07 14:19:20 -07004009 handleInvokePredictedChainingCell(cUnit);
4010 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07004011 case kChainingCellHot:
Ben Cheng1efc9c52009-06-08 18:25:27 -07004012 handleHotChainingCell(cUnit,
Ben Chengba4fc8b2009-06-01 13:00:29 -07004013 blockList[blockId]->startOffset);
4014 break;
Bill Buzbee9c4b7c82009-09-10 10:10:38 -07004015#if defined(WITH_SELF_VERIFICATION) || defined(WITH_JIT_TUNING)
Bill Buzbee1465db52009-09-23 17:17:35 -07004016 case kChainingCellBackwardBranch:
Jeff Hao97319a82009-08-12 16:57:15 -07004017 handleBackwardBranchChainingCell(cUnit,
4018 blockList[blockId]->startOffset);
4019 break;
4020#endif
Ben Chengba4fc8b2009-06-01 13:00:29 -07004021 default:
Bill Buzbee1465db52009-09-23 17:17:35 -07004022 LOGE("Bad blocktype %d", blockList[blockId]->blockType);
Bill Buzbeefc519dc2010-03-06 23:30:57 -08004023 dvmCompilerAbort(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -07004024 }
4025 }
4026 }
Ben Chenge9695e52009-06-16 16:11:47 -07004027
Ben Chengcec26f62010-01-15 15:29:33 -08004028 /* Mark the bottom of chaining cells */
4029 cUnit->chainingCellBottom = (LIR *) newLIR0(cUnit, kArmChainingCellBottom);
4030
Ben Cheng6c10a972009-10-29 14:39:18 -07004031 /*
4032 * Generate the branch to the dvmJitToInterpNoChain entry point at the end
4033 * of all chaining cells for the overflow cases.
4034 */
4035 if (cUnit->switchOverflowPad) {
4036 loadConstant(cUnit, r0, (int) cUnit->switchOverflowPad);
4037 loadWordDisp(cUnit, rGLUE, offsetof(InterpState,
4038 jitToInterpEntries.dvmJitToInterpNoChain), r2);
4039 opRegReg(cUnit, kOpAdd, r1, r1);
4040 opRegRegReg(cUnit, kOpAdd, r4PC, r0, r1);
Ben Cheng978738d2010-05-13 13:45:57 -07004041#if defined(WITH_JIT_TUNING)
Ben Cheng6c10a972009-10-29 14:39:18 -07004042 loadConstant(cUnit, r0, kSwitchOverflow);
4043#endif
4044 opReg(cUnit, kOpBlx, r2);
4045 }
4046
Ben Chenge9695e52009-06-16 16:11:47 -07004047 dvmCompilerApplyGlobalOptimizations(cUnit);
jeffhao9e45c0b2010-02-03 10:24:05 -08004048
4049#if defined(WITH_SELF_VERIFICATION)
4050 selfVerificationBranchInsertPass(cUnit);
4051#endif
Ben Chengba4fc8b2009-06-01 13:00:29 -07004052}
4053
4054/* Accept the work and start compiling */
Bill Buzbee716f1202009-07-23 13:22:09 -07004055bool dvmCompilerDoWork(CompilerWorkOrder *work)
Ben Chengba4fc8b2009-06-01 13:00:29 -07004056{
Ben Chengccd6c012009-10-15 14:52:45 -07004057 bool res;
Ben Chengba4fc8b2009-06-01 13:00:29 -07004058
Ben Cheng6999d842010-01-26 16:46:15 -08004059 if (gDvmJit.codeCacheFull) {
Ben Chengccd6c012009-10-15 14:52:45 -07004060 return false;
4061 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07004062
Ben Chengccd6c012009-10-15 14:52:45 -07004063 switch (work->kind) {
4064 case kWorkOrderMethod:
4065 res = dvmCompileMethod(work->info, &work->result);
4066 break;
4067 case kWorkOrderTrace:
4068 /* Start compilation with maximally allowed trace length */
Bill Buzbeefc519dc2010-03-06 23:30:57 -08004069 res = dvmCompileTrace(work->info, JIT_MAX_TRACE_LEN, &work->result,
4070 work->bailPtr);
Ben Chengccd6c012009-10-15 14:52:45 -07004071 break;
4072 case kWorkOrderTraceDebug: {
4073 bool oldPrintMe = gDvmJit.printMe;
4074 gDvmJit.printMe = true;
4075 /* Start compilation with maximally allowed trace length */
Bill Buzbeefc519dc2010-03-06 23:30:57 -08004076 res = dvmCompileTrace(work->info, JIT_MAX_TRACE_LEN, &work->result,
4077 work->bailPtr);
Elliott Hughes672511b2010-04-26 17:40:13 -07004078 gDvmJit.printMe = oldPrintMe;
Ben Chengccd6c012009-10-15 14:52:45 -07004079 break;
4080 }
4081 default:
4082 res = false;
Bill Buzbeefc519dc2010-03-06 23:30:57 -08004083 LOGE("Jit: unknown work order type");
Elliott Hughes672511b2010-04-26 17:40:13 -07004084 assert(0); // Bail if debug build, discard otherwise
Ben Chengccd6c012009-10-15 14:52:45 -07004085 }
4086 return res;
Ben Chengba4fc8b2009-06-01 13:00:29 -07004087}
4088
Ben Chengba4fc8b2009-06-01 13:00:29 -07004089/* Architectural-specific debugging helpers go here */
4090void dvmCompilerArchDump(void)
4091{
4092 /* Print compiled opcode in this VM instance */
4093 int i, start, streak;
4094 char buf[1024];
4095
4096 streak = i = 0;
4097 buf[0] = 0;
4098 while (opcodeCoverage[i] == 0 && i < 256) {
4099 i++;
4100 }
4101 if (i == 256) {
4102 return;
4103 }
4104 for (start = i++, streak = 1; i < 256; i++) {
4105 if (opcodeCoverage[i]) {
4106 streak++;
4107 } else {
4108 if (streak == 1) {
4109 sprintf(buf+strlen(buf), "%x,", start);
4110 } else {
4111 sprintf(buf+strlen(buf), "%x-%x,", start, start + streak - 1);
4112 }
4113 streak = 0;
4114 while (opcodeCoverage[i] == 0 && i < 256) {
4115 i++;
4116 }
4117 if (i < 256) {
4118 streak = 1;
4119 start = i;
4120 }
4121 }
4122 }
4123 if (streak) {
4124 if (streak == 1) {
4125 sprintf(buf+strlen(buf), "%x", start);
4126 } else {
4127 sprintf(buf+strlen(buf), "%x-%x", start, start + streak - 1);
4128 }
4129 }
4130 if (strlen(buf)) {
Ben Cheng8b258bf2009-06-24 17:27:07 -07004131 LOGD("dalvik.vm.jit.op = %s", buf);
Ben Chengba4fc8b2009-06-01 13:00:29 -07004132 }
4133}
Ben Chengd7d426a2009-09-22 11:23:36 -07004134
4135/* Common initialization routine for an architecture family */
4136bool dvmCompilerArchInit()
4137{
4138 int i;
4139
Bill Buzbee1465db52009-09-23 17:17:35 -07004140 for (i = 0; i < kArmLast; i++) {
Ben Chengd7d426a2009-09-22 11:23:36 -07004141 if (EncodingMap[i].opCode != i) {
4142 LOGE("Encoding order for %s is wrong: expecting %d, seeing %d",
4143 EncodingMap[i].name, i, EncodingMap[i].opCode);
Bill Buzbeefc519dc2010-03-06 23:30:57 -08004144 dvmAbort(); // OK to dvmAbort - build error
Ben Chengd7d426a2009-09-22 11:23:36 -07004145 }
4146 }
4147
Ben Cheng5d90c202009-11-22 23:31:11 -08004148 return dvmCompilerArchVariantInit();
4149}
4150
4151void *dvmCompilerGetInterpretTemplate()
4152{
4153 return (void*) ((int)gDvmJit.codeCache +
4154 templateEntryOffsets[TEMPLATE_INTERPRET]);
4155}
4156
4157/* Needed by the ld/st optmizatons */
4158ArmLIR* dvmCompilerRegCopyNoInsert(CompilationUnit *cUnit, int rDest, int rSrc)
4159{
4160 return genRegCopyNoInsert(cUnit, rDest, rSrc);
4161}
4162
4163/* Needed by the register allocator */
4164ArmLIR* dvmCompilerRegCopy(CompilationUnit *cUnit, int rDest, int rSrc)
4165{
4166 return genRegCopy(cUnit, rDest, rSrc);
4167}
4168
4169/* Needed by the register allocator */
4170void dvmCompilerRegCopyWide(CompilationUnit *cUnit, int destLo, int destHi,
4171 int srcLo, int srcHi)
4172{
4173 genRegCopyWide(cUnit, destLo, destHi, srcLo, srcHi);
4174}
4175
4176void dvmCompilerFlushRegImpl(CompilationUnit *cUnit, int rBase,
4177 int displacement, int rSrc, OpSize size)
4178{
4179 storeBaseDisp(cUnit, rBase, displacement, rSrc, size);
4180}
4181
4182void dvmCompilerFlushRegWideImpl(CompilationUnit *cUnit, int rBase,
4183 int displacement, int rSrcLo, int rSrcHi)
4184{
4185 storeBaseDispWide(cUnit, rBase, displacement, rSrcLo, rSrcHi);
Ben Chengd7d426a2009-09-22 11:23:36 -07004186}