blob: 7f601575c9a31d3ebae5000b448cf1c55a48209c [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
Ben Chengb88ec3c2010-05-17 12:50:33 -07001143 genRegCopy(cUnit, r1, rGLUE);
1144
Ben Cheng38329f52009-07-07 14:19:20 -07001145 /*
1146 * r0 = calleeMethod
1147 * r2 = &predictedChainingCell
1148 * r3 = class
1149 *
1150 * &returnChainingCell has been loaded into r1 but is not needed
1151 * when patching the chaining cell and will be clobbered upon
1152 * returning so it will be reconstructed again.
1153 */
Bill Buzbee1465db52009-09-23 17:17:35 -07001154 opReg(cUnit, kOpBlx, r7);
Ben Cheng38329f52009-07-07 14:19:20 -07001155
1156 /* r1 = &retChainingCell */
Bill Buzbee1465db52009-09-23 17:17:35 -07001157 addrRetChain = opRegRegImm(cUnit, kOpAdd, r1, rpc, 0);
Ben Cheng38329f52009-07-07 14:19:20 -07001158 addrRetChain->generic.target = (LIR *) retChainingCell;
1159
1160 bypassRechaining->generic.target = (LIR *) addrRetChain;
1161 /*
1162 * r0 = calleeMethod,
1163 * r1 = &ChainingCell,
1164 * r4PC = callsiteDPC,
1165 */
1166 genDispatchToHandler(cUnit, TEMPLATE_INVOKE_METHOD_NO_OPT);
Ben Cheng978738d2010-05-13 13:45:57 -07001167#if defined(WITH_JIT_TUNING)
Ben Cheng86717f72010-03-05 15:27:21 -08001168 gDvmJit.invokePolymorphic++;
Ben Cheng38329f52009-07-07 14:19:20 -07001169#endif
1170 /* Handle exceptions using the interpreter */
1171 genTrap(cUnit, mir->offset, pcrLabel);
1172}
1173
Ben Chengba4fc8b2009-06-01 13:00:29 -07001174/* Geneate a branch to go back to the interpreter */
1175static void genPuntToInterp(CompilationUnit *cUnit, unsigned int offset)
1176{
1177 /* r0 = dalvik pc */
Bill Buzbeec6f10662010-02-09 11:16:15 -08001178 dvmCompilerFlushAllRegs(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001179 loadConstant(cUnit, r0, (int) (cUnit->method->insns + offset));
Bill Buzbee270c1d62009-08-13 16:58:07 -07001180 loadWordDisp(cUnit, r0, offsetof(Object, clazz), r3);
1181 loadWordDisp(cUnit, rGLUE, offsetof(InterpState,
1182 jitToInterpEntries.dvmJitToInterpPunt), r1);
Bill Buzbee1465db52009-09-23 17:17:35 -07001183 opReg(cUnit, kOpBlx, r1);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001184}
1185
1186/*
1187 * Attempt to single step one instruction using the interpreter and return
1188 * to the compiled code for the next Dalvik instruction
1189 */
1190static void genInterpSingleStep(CompilationUnit *cUnit, MIR *mir)
1191{
1192 int flags = dexGetInstrFlags(gDvm.instrFlags, mir->dalvikInsn.opCode);
1193 int flagsToCheck = kInstrCanBranch | kInstrCanSwitch | kInstrCanReturn |
1194 kInstrCanThrow;
Bill Buzbee1465db52009-09-23 17:17:35 -07001195
Bill Buzbee45273872010-03-11 11:12:15 -08001196 //If already optimized out, just ignore
1197 if (mir->dalvikInsn.opCode == OP_NOP)
1198 return;
1199
Bill Buzbee1465db52009-09-23 17:17:35 -07001200 //Ugly, but necessary. Flush all Dalvik regs so Interp can find them
Bill Buzbeec6f10662010-02-09 11:16:15 -08001201 dvmCompilerFlushAllRegs(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07001202
Ben Chengba4fc8b2009-06-01 13:00:29 -07001203 if ((mir->next == NULL) || (flags & flagsToCheck)) {
1204 genPuntToInterp(cUnit, mir->offset);
1205 return;
1206 }
1207 int entryAddr = offsetof(InterpState,
1208 jitToInterpEntries.dvmJitToInterpSingleStep);
Bill Buzbee270c1d62009-08-13 16:58:07 -07001209 loadWordDisp(cUnit, rGLUE, entryAddr, r2);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001210 /* r0 = dalvik pc */
1211 loadConstant(cUnit, r0, (int) (cUnit->method->insns + mir->offset));
1212 /* r1 = dalvik pc of following instruction */
1213 loadConstant(cUnit, r1, (int) (cUnit->method->insns + mir->next->offset));
Bill Buzbee1465db52009-09-23 17:17:35 -07001214 opReg(cUnit, kOpBlx, r2);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001215}
1216
Ben Chengfc075c22010-05-28 15:20:08 -07001217#if defined(WITH_DEADLOCK_PREDICTION) || defined(WITH_MONITOR_TRACKING) || \
1218 defined(_ARMV5TE) || defined(_ARMV5TE_VFP)
Bill Buzbeec1d9ed42010-02-02 11:04:33 -08001219/*
1220 * To prevent a thread in a monitor wait from blocking the Jit from
1221 * resetting the code cache, heavyweight monitor lock will not
1222 * be allowed to return to an existing translation. Instead, we will
1223 * handle them by branching to a handler, which will in turn call the
1224 * runtime lock routine and then branch directly back to the
1225 * interpreter main loop. Given the high cost of the heavyweight
1226 * lock operation, this additional cost should be slight (especially when
1227 * considering that we expect the vast majority of lock operations to
1228 * use the fast-path thin lock bypass).
1229 */
Ben Cheng5d90c202009-11-22 23:31:11 -08001230static void genMonitorPortable(CompilationUnit *cUnit, MIR *mir)
Bill Buzbee270c1d62009-08-13 16:58:07 -07001231{
Bill Buzbeeefbd3c52009-11-04 22:18:40 -08001232 bool isEnter = (mir->dalvikInsn.opCode == OP_MONITOR_ENTER);
Bill Buzbee1465db52009-09-23 17:17:35 -07001233 genExportPC(cUnit, mir);
Bill Buzbeec6f10662010-02-09 11:16:15 -08001234 dvmCompilerFlushAllRegs(cUnit); /* Send everything to home location */
1235 RegLocation rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07001236 loadValueDirectFixed(cUnit, rlSrc, r1);
1237 loadWordDisp(cUnit, rGLUE, offsetof(InterpState, self), r0);
Bill Buzbeec1d9ed42010-02-02 11:04:33 -08001238 genNullCheck(cUnit, rlSrc.sRegLow, r1, mir->offset, NULL);
Bill Buzbeeefbd3c52009-11-04 22:18:40 -08001239 if (isEnter) {
Bill Buzbeec1d9ed42010-02-02 11:04:33 -08001240 /* Get dPC of next insn */
1241 loadConstant(cUnit, r4PC, (int)(cUnit->method->insns + mir->offset +
1242 dexGetInstrWidthAbs(gDvm.instrWidth, OP_MONITOR_ENTER)));
1243#if defined(WITH_DEADLOCK_PREDICTION)
1244 genDispatchToHandler(cUnit, TEMPLATE_MONITOR_ENTER_DEBUG);
1245#else
1246 genDispatchToHandler(cUnit, TEMPLATE_MONITOR_ENTER);
1247#endif
Bill Buzbee1465db52009-09-23 17:17:35 -07001248 } else {
Ben Chengbd1326d2010-04-02 15:04:53 -07001249 LOAD_FUNC_ADDR(cUnit, r2, (int)dvmUnlockObject);
Bill Buzbeec1d9ed42010-02-02 11:04:33 -08001250 /* Do the call */
1251 opReg(cUnit, kOpBlx, r2);
Bill Buzbee6bbdd6b2010-02-16 14:40:01 -08001252 opRegImm(cUnit, kOpCmp, r0, 0); /* Did we throw? */
1253 ArmLIR *branchOver = opCondBranch(cUnit, kArmCondNe);
1254 loadConstant(cUnit, r0,
1255 (int) (cUnit->method->insns + mir->offset +
1256 dexGetInstrWidthAbs(gDvm.instrWidth, OP_MONITOR_EXIT)));
1257 genDispatchToHandler(cUnit, TEMPLATE_THROW_EXCEPTION_COMMON);
1258 ArmLIR *target = newLIR0(cUnit, kArmPseudoTargetLabel);
1259 target->defMask = ENCODE_ALL;
1260 branchOver->generic.target = (LIR *) target;
Elliott Hughes6a555132010-02-25 15:41:42 -08001261 dvmCompilerClobberCallRegs(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07001262 }
Bill Buzbee270c1d62009-08-13 16:58:07 -07001263}
Ben Chengfc075c22010-05-28 15:20:08 -07001264#endif
Bill Buzbee270c1d62009-08-13 16:58:07 -07001265
Ben Chengba4fc8b2009-06-01 13:00:29 -07001266/*
1267 * The following are the first-level codegen routines that analyze the format
1268 * of each bytecode then either dispatch special purpose codegen routines
1269 * or produce corresponding Thumb instructions directly.
1270 */
1271
1272static bool handleFmt10t_Fmt20t_Fmt30t(CompilationUnit *cUnit, MIR *mir,
Bill Buzbee89efc3d2009-07-28 11:22:22 -07001273 BasicBlock *bb, ArmLIR *labelList)
Ben Chengba4fc8b2009-06-01 13:00:29 -07001274{
1275 /* For OP_GOTO, OP_GOTO_16, and OP_GOTO_32 */
1276 genUnconditionalBranch(cUnit, &labelList[bb->taken->id]);
1277 return false;
1278}
1279
1280static bool handleFmt10x(CompilationUnit *cUnit, MIR *mir)
1281{
1282 OpCode dalvikOpCode = mir->dalvikInsn.opCode;
Andy McFaddenc35a2ef2010-06-17 12:36:00 -07001283 if ((dalvikOpCode >= OP_UNUSED_3E) && (dalvikOpCode <= OP_UNUSED_43)) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07001284 LOGE("Codegen: got unused opcode 0x%x\n",dalvikOpCode);
1285 return true;
1286 }
1287 switch (dalvikOpCode) {
1288 case OP_RETURN_VOID:
1289 genReturnCommon(cUnit,mir);
1290 break;
1291 case OP_UNUSED_73:
1292 case OP_UNUSED_79:
1293 case OP_UNUSED_7A:
Andy McFaddenc35a2ef2010-06-17 12:36:00 -07001294 case OP_UNUSED_F1:
1295 case OP_UNUSED_FF:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001296 LOGE("Codegen: got unused opcode 0x%x\n",dalvikOpCode);
1297 return true;
1298 case OP_NOP:
1299 break;
1300 default:
1301 return true;
1302 }
1303 return false;
1304}
1305
1306static bool handleFmt11n_Fmt31i(CompilationUnit *cUnit, MIR *mir)
1307{
Bill Buzbee1465db52009-09-23 17:17:35 -07001308 RegLocation rlDest;
1309 RegLocation rlResult;
1310 if (mir->ssaRep->numDefs == 2) {
Bill Buzbeec6f10662010-02-09 11:16:15 -08001311 rlDest = dvmCompilerGetDestWide(cUnit, mir, 0, 1);
Bill Buzbee1465db52009-09-23 17:17:35 -07001312 } else {
Bill Buzbeec6f10662010-02-09 11:16:15 -08001313 rlDest = dvmCompilerGetDest(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07001314 }
Ben Chenge9695e52009-06-16 16:11:47 -07001315
Ben Chengba4fc8b2009-06-01 13:00:29 -07001316 switch (mir->dalvikInsn.opCode) {
1317 case OP_CONST:
Ben Chenge9695e52009-06-16 16:11:47 -07001318 case OP_CONST_4: {
Bill Buzbeec6f10662010-02-09 11:16:15 -08001319 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kAnyReg, true);
Ben Chengbd1326d2010-04-02 15:04:53 -07001320 loadConstantNoClobber(cUnit, rlResult.lowReg, mir->dalvikInsn.vB);
Bill Buzbee1465db52009-09-23 17:17:35 -07001321 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001322 break;
Ben Chenge9695e52009-06-16 16:11:47 -07001323 }
1324 case OP_CONST_WIDE_32: {
Bill Buzbee1465db52009-09-23 17:17:35 -07001325 //TUNING: single routine to load constant pair for support doubles
Bill Buzbee964a7b02010-01-28 12:54:19 -08001326 //TUNING: load 0/-1 separately to avoid load dependency
Bill Buzbeec6f10662010-02-09 11:16:15 -08001327 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Ben Chengbd1326d2010-04-02 15:04:53 -07001328 loadConstantNoClobber(cUnit, rlResult.lowReg, mir->dalvikInsn.vB);
Bill Buzbee1465db52009-09-23 17:17:35 -07001329 opRegRegImm(cUnit, kOpAsr, rlResult.highReg,
1330 rlResult.lowReg, 31);
1331 storeValueWide(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001332 break;
Ben Chenge9695e52009-06-16 16:11:47 -07001333 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07001334 default:
1335 return true;
1336 }
1337 return false;
1338}
1339
1340static bool handleFmt21h(CompilationUnit *cUnit, MIR *mir)
1341{
Bill Buzbee1465db52009-09-23 17:17:35 -07001342 RegLocation rlDest;
1343 RegLocation rlResult;
1344 if (mir->ssaRep->numDefs == 2) {
Bill Buzbeec6f10662010-02-09 11:16:15 -08001345 rlDest = dvmCompilerGetDestWide(cUnit, mir, 0, 1);
Bill Buzbee1465db52009-09-23 17:17:35 -07001346 } else {
Bill Buzbeec6f10662010-02-09 11:16:15 -08001347 rlDest = dvmCompilerGetDest(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07001348 }
Bill Buzbeec6f10662010-02-09 11:16:15 -08001349 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kAnyReg, true);
Ben Chenge9695e52009-06-16 16:11:47 -07001350
Ben Chengba4fc8b2009-06-01 13:00:29 -07001351 switch (mir->dalvikInsn.opCode) {
Ben Chenge9695e52009-06-16 16:11:47 -07001352 case OP_CONST_HIGH16: {
Ben Chengbd1326d2010-04-02 15:04:53 -07001353 loadConstantNoClobber(cUnit, rlResult.lowReg,
1354 mir->dalvikInsn.vB << 16);
Bill Buzbee1465db52009-09-23 17:17:35 -07001355 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001356 break;
Ben Chenge9695e52009-06-16 16:11:47 -07001357 }
1358 case OP_CONST_WIDE_HIGH16: {
Bill Buzbee1465db52009-09-23 17:17:35 -07001359 loadConstantValueWide(cUnit, rlResult.lowReg, rlResult.highReg,
1360 0, mir->dalvikInsn.vB << 16);
1361 storeValueWide(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001362 break;
Ben Chenge9695e52009-06-16 16:11:47 -07001363 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07001364 default:
1365 return true;
1366 }
1367 return false;
1368}
1369
1370static bool handleFmt20bc(CompilationUnit *cUnit, MIR *mir)
1371{
1372 /* For OP_THROW_VERIFICATION_ERROR */
1373 genInterpSingleStep(cUnit, mir);
1374 return false;
1375}
1376
1377static bool handleFmt21c_Fmt31c(CompilationUnit *cUnit, MIR *mir)
1378{
Bill Buzbee1465db52009-09-23 17:17:35 -07001379 RegLocation rlResult;
1380 RegLocation rlDest;
1381 RegLocation rlSrc;
Ben Chenge9695e52009-06-16 16:11:47 -07001382
Ben Chengba4fc8b2009-06-01 13:00:29 -07001383 switch (mir->dalvikInsn.opCode) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07001384 case OP_CONST_STRING_JUMBO:
1385 case OP_CONST_STRING: {
1386 void *strPtr = (void*)
1387 (cUnit->method->clazz->pDvmDex->pResStrings[mir->dalvikInsn.vB]);
Ben Chengdd6e8702010-05-07 13:05:47 -07001388
1389 if (strPtr == NULL) {
1390 LOGE("Unexpected null string");
1391 dvmAbort();
1392 }
1393
Bill Buzbeec6f10662010-02-09 11:16:15 -08001394 rlDest = dvmCompilerGetDest(cUnit, mir, 0);
1395 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Ben Chengbd1326d2010-04-02 15:04:53 -07001396 loadConstantNoClobber(cUnit, rlResult.lowReg, (int) strPtr );
Bill Buzbee1465db52009-09-23 17:17:35 -07001397 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001398 break;
1399 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07001400 case OP_CONST_CLASS: {
1401 void *classPtr = (void*)
1402 (cUnit->method->clazz->pDvmDex->pResClasses[mir->dalvikInsn.vB]);
Ben Chengdd6e8702010-05-07 13:05:47 -07001403
1404 if (classPtr == NULL) {
1405 LOGE("Unexpected null class");
1406 dvmAbort();
1407 }
1408
Bill Buzbeec6f10662010-02-09 11:16:15 -08001409 rlDest = dvmCompilerGetDest(cUnit, mir, 0);
1410 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Ben Chengbd1326d2010-04-02 15:04:53 -07001411 loadConstantNoClobber(cUnit, rlResult.lowReg, (int) classPtr );
Bill Buzbee1465db52009-09-23 17:17:35 -07001412 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001413 break;
1414 }
1415 case OP_SGET_OBJECT:
1416 case OP_SGET_BOOLEAN:
1417 case OP_SGET_CHAR:
1418 case OP_SGET_BYTE:
1419 case OP_SGET_SHORT:
1420 case OP_SGET: {
Bill Buzbee50a6bf22009-07-08 13:08:04 -07001421 int valOffset = offsetof(StaticField, value);
Bill Buzbeec6f10662010-02-09 11:16:15 -08001422 int tReg = dvmCompilerAllocTemp(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001423 void *fieldPtr = (void*)
1424 (cUnit->method->clazz->pDvmDex->pResFields[mir->dalvikInsn.vB]);
Ben Chengdd6e8702010-05-07 13:05:47 -07001425
1426 if (fieldPtr == NULL) {
1427 LOGE("Unexpected null static field");
1428 dvmAbort();
1429 }
1430
Bill Buzbeec6f10662010-02-09 11:16:15 -08001431 rlDest = dvmCompilerGetDest(cUnit, mir, 0);
1432 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kAnyReg, true);
Bill Buzbee1465db52009-09-23 17:17:35 -07001433 loadConstant(cUnit, tReg, (int) fieldPtr + valOffset);
Ben Cheng11d8f142010-03-24 15:24:19 -07001434
1435 HEAP_ACCESS_SHADOW(true);
Bill Buzbee1465db52009-09-23 17:17:35 -07001436 loadWordDisp(cUnit, tReg, 0, rlResult.lowReg);
Ben Cheng11d8f142010-03-24 15:24:19 -07001437 HEAP_ACCESS_SHADOW(false);
1438
Bill Buzbee1465db52009-09-23 17:17:35 -07001439 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001440 break;
1441 }
1442 case OP_SGET_WIDE: {
Bill Buzbee50a6bf22009-07-08 13:08:04 -07001443 int valOffset = offsetof(StaticField, value);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001444 void *fieldPtr = (void*)
1445 (cUnit->method->clazz->pDvmDex->pResFields[mir->dalvikInsn.vB]);
Ben Chengdd6e8702010-05-07 13:05:47 -07001446
1447 if (fieldPtr == NULL) {
1448 LOGE("Unexpected null static field");
1449 dvmAbort();
1450 }
1451
Bill Buzbeec6f10662010-02-09 11:16:15 -08001452 int tReg = dvmCompilerAllocTemp(cUnit);
Bill Buzbeec6f10662010-02-09 11:16:15 -08001453 rlDest = dvmCompilerGetDestWide(cUnit, mir, 0, 1);
1454 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kAnyReg, true);
Bill Buzbee1465db52009-09-23 17:17:35 -07001455 loadConstant(cUnit, tReg, (int) fieldPtr + valOffset);
Ben Cheng11d8f142010-03-24 15:24:19 -07001456
1457 HEAP_ACCESS_SHADOW(true);
Bill Buzbee1465db52009-09-23 17:17:35 -07001458 loadPair(cUnit, tReg, rlResult.lowReg, rlResult.highReg);
Ben Cheng11d8f142010-03-24 15:24:19 -07001459 HEAP_ACCESS_SHADOW(false);
1460
Bill Buzbee1465db52009-09-23 17:17:35 -07001461 storeValueWide(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001462 break;
1463 }
1464 case OP_SPUT_OBJECT:
1465 case OP_SPUT_BOOLEAN:
1466 case OP_SPUT_CHAR:
1467 case OP_SPUT_BYTE:
1468 case OP_SPUT_SHORT:
1469 case OP_SPUT: {
Bill Buzbee50a6bf22009-07-08 13:08:04 -07001470 int valOffset = offsetof(StaticField, value);
Bill Buzbeec6f10662010-02-09 11:16:15 -08001471 int tReg = dvmCompilerAllocTemp(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001472 void *fieldPtr = (void*)
1473 (cUnit->method->clazz->pDvmDex->pResFields[mir->dalvikInsn.vB]);
Ben Chenge9695e52009-06-16 16:11:47 -07001474
Ben Chengdd6e8702010-05-07 13:05:47 -07001475 if (fieldPtr == NULL) {
1476 LOGE("Unexpected null static field");
1477 dvmAbort();
1478 }
1479
Bill Buzbeec6f10662010-02-09 11:16:15 -08001480 rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07001481 rlSrc = loadValue(cUnit, rlSrc, kAnyReg);
1482 loadConstant(cUnit, tReg, (int) fieldPtr + valOffset);
Ben Cheng11d8f142010-03-24 15:24:19 -07001483
1484 HEAP_ACCESS_SHADOW(true);
Bill Buzbee1465db52009-09-23 17:17:35 -07001485 storeWordDisp(cUnit, tReg, 0 ,rlSrc.lowReg);
Ben Cheng11d8f142010-03-24 15:24:19 -07001486 HEAP_ACCESS_SHADOW(false);
1487
Ben Chengba4fc8b2009-06-01 13:00:29 -07001488 break;
1489 }
1490 case OP_SPUT_WIDE: {
Bill Buzbeec6f10662010-02-09 11:16:15 -08001491 int tReg = dvmCompilerAllocTemp(cUnit);
Bill Buzbee50a6bf22009-07-08 13:08:04 -07001492 int valOffset = offsetof(StaticField, value);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001493 void *fieldPtr = (void*)
1494 (cUnit->method->clazz->pDvmDex->pResFields[mir->dalvikInsn.vB]);
Ben Chenge9695e52009-06-16 16:11:47 -07001495
Ben Chengdd6e8702010-05-07 13:05:47 -07001496 if (fieldPtr == NULL) {
1497 LOGE("Unexpected null static field");
1498 dvmAbort();
1499 }
1500
Bill Buzbeec6f10662010-02-09 11:16:15 -08001501 rlSrc = dvmCompilerGetSrcWide(cUnit, mir, 0, 1);
Bill Buzbee1465db52009-09-23 17:17:35 -07001502 rlSrc = loadValueWide(cUnit, rlSrc, kAnyReg);
1503 loadConstant(cUnit, tReg, (int) fieldPtr + valOffset);
Ben Cheng11d8f142010-03-24 15:24:19 -07001504
1505 HEAP_ACCESS_SHADOW(true);
Bill Buzbee1465db52009-09-23 17:17:35 -07001506 storePair(cUnit, tReg, rlSrc.lowReg, rlSrc.highReg);
Ben Cheng11d8f142010-03-24 15:24:19 -07001507 HEAP_ACCESS_SHADOW(false);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001508 break;
1509 }
1510 case OP_NEW_INSTANCE: {
Ben Chenge9695e52009-06-16 16:11:47 -07001511 /*
1512 * Obey the calling convention and don't mess with the register
1513 * usage.
1514 */
Ben Chengba4fc8b2009-06-01 13:00:29 -07001515 ClassObject *classPtr = (void*)
1516 (cUnit->method->clazz->pDvmDex->pResClasses[mir->dalvikInsn.vB]);
Ben Chengdd6e8702010-05-07 13:05:47 -07001517
1518 if (classPtr == NULL) {
1519 LOGE("Unexpected null class");
1520 dvmAbort();
1521 }
1522
Ben Cheng79d173c2009-09-29 16:12:51 -07001523 /*
1524 * If it is going to throw, it should not make to the trace to begin
Bill Buzbee1465db52009-09-23 17:17:35 -07001525 * with. However, Alloc might throw, so we need to genExportPC()
Ben Cheng79d173c2009-09-29 16:12:51 -07001526 */
1527 assert((classPtr->accessFlags & (ACC_INTERFACE|ACC_ABSTRACT)) == 0);
Bill Buzbeec6f10662010-02-09 11:16:15 -08001528 dvmCompilerFlushAllRegs(cUnit); /* Everything to home location */
Bill Buzbee1465db52009-09-23 17:17:35 -07001529 genExportPC(cUnit, mir);
Ben Chengbd1326d2010-04-02 15:04:53 -07001530 LOAD_FUNC_ADDR(cUnit, r2, (int)dvmAllocObject);
Ben Chenge9695e52009-06-16 16:11:47 -07001531 loadConstant(cUnit, r0, (int) classPtr);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001532 loadConstant(cUnit, r1, ALLOC_DONT_TRACK);
Bill Buzbee1465db52009-09-23 17:17:35 -07001533 opReg(cUnit, kOpBlx, r2);
Elliott Hughes6a555132010-02-25 15:41:42 -08001534 dvmCompilerClobberCallRegs(cUnit);
Ben Cheng4f489172009-09-27 17:08:35 -07001535 /* generate a branch over if allocation is successful */
Bill Buzbee1465db52009-09-23 17:17:35 -07001536 opRegImm(cUnit, kOpCmp, r0, 0); /* NULL? */
1537 ArmLIR *branchOver = opCondBranch(cUnit, kArmCondNe);
Ben Cheng4f489172009-09-27 17:08:35 -07001538 /*
1539 * OOM exception needs to be thrown here and cannot re-execute
1540 */
1541 loadConstant(cUnit, r0,
1542 (int) (cUnit->method->insns + mir->offset));
1543 genDispatchToHandler(cUnit, TEMPLATE_THROW_EXCEPTION_COMMON);
1544 /* noreturn */
1545
Bill Buzbee1465db52009-09-23 17:17:35 -07001546 ArmLIR *target = newLIR0(cUnit, kArmPseudoTargetLabel);
Ben Cheng4f489172009-09-27 17:08:35 -07001547 target->defMask = ENCODE_ALL;
1548 branchOver->generic.target = (LIR *) target;
Bill Buzbeec6f10662010-02-09 11:16:15 -08001549 rlDest = dvmCompilerGetDest(cUnit, mir, 0);
1550 rlResult = dvmCompilerGetReturn(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07001551 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001552 break;
1553 }
1554 case OP_CHECK_CAST: {
Ben Chenge9695e52009-06-16 16:11:47 -07001555 /*
1556 * Obey the calling convention and don't mess with the register
1557 * usage.
1558 */
Ben Chengba4fc8b2009-06-01 13:00:29 -07001559 ClassObject *classPtr =
1560 (cUnit->method->clazz->pDvmDex->pResClasses[mir->dalvikInsn.vB]);
Bill Buzbee4df41a52009-11-12 17:07:16 -08001561 /*
1562 * Note: It is possible that classPtr is NULL at this point,
1563 * even though this instruction has been successfully interpreted.
1564 * If the previous interpretation had a null source, the
1565 * interpreter would not have bothered to resolve the clazz.
1566 * Bail out to the interpreter in this case, and log it
1567 * so that we can tell if it happens frequently.
1568 */
1569 if (classPtr == NULL) {
Ben Cheng11d8f142010-03-24 15:24:19 -07001570 LOGVV("null clazz in OP_CHECK_CAST, single-stepping");
Bill Buzbee4df41a52009-11-12 17:07:16 -08001571 genInterpSingleStep(cUnit, mir);
1572 return false;
1573 }
Bill Buzbeec6f10662010-02-09 11:16:15 -08001574 dvmCompilerFlushAllRegs(cUnit); /* Everything to home location */
Ben Chengba4fc8b2009-06-01 13:00:29 -07001575 loadConstant(cUnit, r1, (int) classPtr );
Bill Buzbeec6f10662010-02-09 11:16:15 -08001576 rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07001577 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
1578 opRegImm(cUnit, kOpCmp, rlSrc.lowReg, 0); /* Null? */
1579 ArmLIR *branch1 = opCondBranch(cUnit, kArmCondEq);
1580 /*
1581 * rlSrc.lowReg now contains object->clazz. Note that
1582 * it could have been allocated r0, but we're okay so long
1583 * as we don't do anything desctructive until r0 is loaded
1584 * with clazz.
1585 */
Ben Chengba4fc8b2009-06-01 13:00:29 -07001586 /* r0 now contains object->clazz */
Bill Buzbee1465db52009-09-23 17:17:35 -07001587 loadWordDisp(cUnit, rlSrc.lowReg, offsetof(Object, clazz), r0);
Ben Chengbd1326d2010-04-02 15:04:53 -07001588 LOAD_FUNC_ADDR(cUnit, r2, (int)dvmInstanceofNonTrivial);
Bill Buzbee1465db52009-09-23 17:17:35 -07001589 opRegReg(cUnit, kOpCmp, r0, r1);
1590 ArmLIR *branch2 = opCondBranch(cUnit, kArmCondEq);
1591 opReg(cUnit, kOpBlx, r2);
Elliott Hughes6a555132010-02-25 15:41:42 -08001592 dvmCompilerClobberCallRegs(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07001593 /*
1594 * If null, check cast failed - punt to the interpreter. Because
1595 * interpreter will be the one throwing, we don't need to
1596 * genExportPC() here.
1597 */
Bill Buzbee270c1d62009-08-13 16:58:07 -07001598 genZeroCheck(cUnit, r0, mir->offset, NULL);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001599 /* check cast passed - branch target here */
Bill Buzbee1465db52009-09-23 17:17:35 -07001600 ArmLIR *target = newLIR0(cUnit, kArmPseudoTargetLabel);
Ben Chengd7d426a2009-09-22 11:23:36 -07001601 target->defMask = ENCODE_ALL;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001602 branch1->generic.target = (LIR *)target;
1603 branch2->generic.target = (LIR *)target;
1604 break;
1605 }
1606 default:
1607 return true;
1608 }
1609 return false;
1610}
1611
1612static bool handleFmt11x(CompilationUnit *cUnit, MIR *mir)
1613{
1614 OpCode dalvikOpCode = mir->dalvikInsn.opCode;
Bill Buzbee1465db52009-09-23 17:17:35 -07001615 RegLocation rlResult;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001616 switch (dalvikOpCode) {
1617 case OP_MOVE_EXCEPTION: {
1618 int offset = offsetof(InterpState, self);
1619 int exOffset = offsetof(Thread, exception);
Bill Buzbeec6f10662010-02-09 11:16:15 -08001620 int selfReg = dvmCompilerAllocTemp(cUnit);
1621 int resetReg = dvmCompilerAllocTemp(cUnit);
1622 RegLocation rlDest = dvmCompilerGetDest(cUnit, mir, 0);
1623 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Bill Buzbee1465db52009-09-23 17:17:35 -07001624 loadWordDisp(cUnit, rGLUE, offset, selfReg);
Bill Buzbeef9f33282009-11-22 12:45:30 -08001625 loadConstant(cUnit, resetReg, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07001626 loadWordDisp(cUnit, selfReg, exOffset, rlResult.lowReg);
Bill Buzbeef9f33282009-11-22 12:45:30 -08001627 storeWordDisp(cUnit, selfReg, exOffset, resetReg);
Bill Buzbee1465db52009-09-23 17:17:35 -07001628 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001629 break;
1630 }
1631 case OP_MOVE_RESULT:
1632 case OP_MOVE_RESULT_OBJECT: {
Bill Buzbeec6f10662010-02-09 11:16:15 -08001633 RegLocation rlDest = dvmCompilerGetDest(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07001634 RegLocation rlSrc = LOC_DALVIK_RETURN_VAL;
1635 rlSrc.fp = rlDest.fp;
1636 storeValue(cUnit, rlDest, rlSrc);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001637 break;
1638 }
1639 case OP_MOVE_RESULT_WIDE: {
Bill Buzbeec6f10662010-02-09 11:16:15 -08001640 RegLocation rlDest = dvmCompilerGetDestWide(cUnit, mir, 0, 1);
Bill Buzbee1465db52009-09-23 17:17:35 -07001641 RegLocation rlSrc = LOC_DALVIK_RETURN_VAL_WIDE;
1642 rlSrc.fp = rlDest.fp;
1643 storeValueWide(cUnit, rlDest, rlSrc);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001644 break;
1645 }
1646 case OP_RETURN_WIDE: {
Bill Buzbeec6f10662010-02-09 11:16:15 -08001647 RegLocation rlSrc = dvmCompilerGetSrcWide(cUnit, mir, 0, 1);
Bill Buzbee1465db52009-09-23 17:17:35 -07001648 RegLocation rlDest = LOC_DALVIK_RETURN_VAL_WIDE;
1649 rlDest.fp = rlSrc.fp;
1650 storeValueWide(cUnit, rlDest, rlSrc);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001651 genReturnCommon(cUnit,mir);
1652 break;
1653 }
1654 case OP_RETURN:
1655 case OP_RETURN_OBJECT: {
Bill Buzbeec6f10662010-02-09 11:16:15 -08001656 RegLocation rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07001657 RegLocation rlDest = LOC_DALVIK_RETURN_VAL;
1658 rlDest.fp = rlSrc.fp;
1659 storeValue(cUnit, rlDest, rlSrc);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001660 genReturnCommon(cUnit,mir);
1661 break;
1662 }
Bill Buzbee1465db52009-09-23 17:17:35 -07001663 case OP_MONITOR_EXIT:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001664 case OP_MONITOR_ENTER:
Bill Buzbeed0937ef2009-12-22 16:15:39 -08001665#if defined(WITH_DEADLOCK_PREDICTION) || defined(WITH_MONITOR_TRACKING)
Ben Cheng5d90c202009-11-22 23:31:11 -08001666 genMonitorPortable(cUnit, mir);
Bill Buzbee1465db52009-09-23 17:17:35 -07001667#else
Ben Cheng5d90c202009-11-22 23:31:11 -08001668 genMonitor(cUnit, mir);
Bill Buzbee1465db52009-09-23 17:17:35 -07001669#endif
Ben Chengba4fc8b2009-06-01 13:00:29 -07001670 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001671 case OP_THROW: {
1672 genInterpSingleStep(cUnit, mir);
1673 break;
1674 }
1675 default:
1676 return true;
1677 }
1678 return false;
1679}
1680
Bill Buzbeed45ba372009-06-15 17:00:57 -07001681static bool handleFmt12x(CompilationUnit *cUnit, MIR *mir)
1682{
1683 OpCode opCode = mir->dalvikInsn.opCode;
Bill Buzbee1465db52009-09-23 17:17:35 -07001684 RegLocation rlDest;
1685 RegLocation rlSrc;
1686 RegLocation rlResult;
Bill Buzbeed45ba372009-06-15 17:00:57 -07001687
Ben Chengba4fc8b2009-06-01 13:00:29 -07001688 if ( (opCode >= OP_ADD_INT_2ADDR) && (opCode <= OP_REM_DOUBLE_2ADDR)) {
Ben Cheng5d90c202009-11-22 23:31:11 -08001689 return genArithOp( cUnit, mir );
Ben Chengba4fc8b2009-06-01 13:00:29 -07001690 }
1691
Bill Buzbee1465db52009-09-23 17:17:35 -07001692 if (mir->ssaRep->numUses == 2)
Bill Buzbeec6f10662010-02-09 11:16:15 -08001693 rlSrc = dvmCompilerGetSrcWide(cUnit, mir, 0, 1);
Bill Buzbee1465db52009-09-23 17:17:35 -07001694 else
Bill Buzbeec6f10662010-02-09 11:16:15 -08001695 rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07001696 if (mir->ssaRep->numDefs == 2)
Bill Buzbeec6f10662010-02-09 11:16:15 -08001697 rlDest = dvmCompilerGetDestWide(cUnit, mir, 0, 1);
Bill Buzbee1465db52009-09-23 17:17:35 -07001698 else
Bill Buzbeec6f10662010-02-09 11:16:15 -08001699 rlDest = dvmCompilerGetDest(cUnit, mir, 0);
Ben Chenge9695e52009-06-16 16:11:47 -07001700
Ben Chengba4fc8b2009-06-01 13:00:29 -07001701 switch (opCode) {
Bill Buzbee1465db52009-09-23 17:17:35 -07001702 case OP_DOUBLE_TO_INT:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001703 case OP_INT_TO_FLOAT:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001704 case OP_FLOAT_TO_INT:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001705 case OP_DOUBLE_TO_FLOAT:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001706 case OP_FLOAT_TO_DOUBLE:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001707 case OP_INT_TO_DOUBLE:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001708 case OP_FLOAT_TO_LONG:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001709 case OP_LONG_TO_FLOAT:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001710 case OP_DOUBLE_TO_LONG:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001711 case OP_LONG_TO_DOUBLE:
Ben Cheng5d90c202009-11-22 23:31:11 -08001712 return genConversion(cUnit, mir);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001713 case OP_NEG_INT:
1714 case OP_NOT_INT:
Ben Cheng5d90c202009-11-22 23:31:11 -08001715 return genArithOpInt(cUnit, mir, rlDest, rlSrc, rlSrc);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001716 case OP_NEG_LONG:
1717 case OP_NOT_LONG:
Ben Cheng5d90c202009-11-22 23:31:11 -08001718 return genArithOpLong(cUnit, mir, rlDest, rlSrc, rlSrc);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001719 case OP_NEG_FLOAT:
Ben Cheng5d90c202009-11-22 23:31:11 -08001720 return genArithOpFloat(cUnit, mir, rlDest, rlSrc, rlSrc);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001721 case OP_NEG_DOUBLE:
Ben Cheng5d90c202009-11-22 23:31:11 -08001722 return genArithOpDouble(cUnit, mir, rlDest, rlSrc, rlSrc);
Bill Buzbee1465db52009-09-23 17:17:35 -07001723 case OP_MOVE_WIDE:
1724 storeValueWide(cUnit, rlDest, rlSrc);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001725 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07001726 case OP_INT_TO_LONG:
Bill Buzbeec6f10662010-02-09 11:16:15 -08001727 rlSrc = dvmCompilerUpdateLoc(cUnit, rlSrc);
1728 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Bill Buzbee964a7b02010-01-28 12:54:19 -08001729 //TUNING: shouldn't loadValueDirect already check for phys reg?
Bill Buzbee1465db52009-09-23 17:17:35 -07001730 if (rlSrc.location == kLocPhysReg) {
1731 genRegCopy(cUnit, rlResult.lowReg, rlSrc.lowReg);
1732 } else {
1733 loadValueDirect(cUnit, rlSrc, rlResult.lowReg);
1734 }
1735 opRegRegImm(cUnit, kOpAsr, rlResult.highReg,
1736 rlResult.lowReg, 31);
1737 storeValueWide(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001738 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07001739 case OP_LONG_TO_INT:
Bill Buzbeec6f10662010-02-09 11:16:15 -08001740 rlSrc = dvmCompilerUpdateLocWide(cUnit, rlSrc);
1741 rlSrc = dvmCompilerWideToNarrow(cUnit, rlSrc);
Bill Buzbee1465db52009-09-23 17:17:35 -07001742 // Intentional fallthrough
Ben Chengba4fc8b2009-06-01 13:00:29 -07001743 case OP_MOVE:
1744 case OP_MOVE_OBJECT:
Bill Buzbee1465db52009-09-23 17:17:35 -07001745 storeValue(cUnit, rlDest, rlSrc);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001746 break;
1747 case OP_INT_TO_BYTE:
Bill Buzbee1465db52009-09-23 17:17:35 -07001748 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
Bill Buzbeec6f10662010-02-09 11:16:15 -08001749 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Bill Buzbee1465db52009-09-23 17:17:35 -07001750 opRegReg(cUnit, kOp2Byte, rlResult.lowReg, rlSrc.lowReg);
1751 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001752 break;
1753 case OP_INT_TO_SHORT:
Bill Buzbee1465db52009-09-23 17:17:35 -07001754 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
Bill Buzbeec6f10662010-02-09 11:16:15 -08001755 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Bill Buzbee1465db52009-09-23 17:17:35 -07001756 opRegReg(cUnit, kOp2Short, rlResult.lowReg, rlSrc.lowReg);
1757 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001758 break;
1759 case OP_INT_TO_CHAR:
Bill Buzbee1465db52009-09-23 17:17:35 -07001760 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
Bill Buzbeec6f10662010-02-09 11:16:15 -08001761 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Bill Buzbee1465db52009-09-23 17:17:35 -07001762 opRegReg(cUnit, kOp2Char, rlResult.lowReg, rlSrc.lowReg);
1763 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001764 break;
1765 case OP_ARRAY_LENGTH: {
1766 int lenOffset = offsetof(ArrayObject, length);
Bill Buzbee1465db52009-09-23 17:17:35 -07001767 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
1768 genNullCheck(cUnit, rlSrc.sRegLow, rlSrc.lowReg,
1769 mir->offset, NULL);
Bill Buzbeec6f10662010-02-09 11:16:15 -08001770 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Bill Buzbee1465db52009-09-23 17:17:35 -07001771 loadWordDisp(cUnit, rlSrc.lowReg, lenOffset,
1772 rlResult.lowReg);
1773 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001774 break;
1775 }
1776 default:
1777 return true;
1778 }
1779 return false;
1780}
1781
1782static bool handleFmt21s(CompilationUnit *cUnit, MIR *mir)
1783{
1784 OpCode dalvikOpCode = mir->dalvikInsn.opCode;
Bill Buzbee1465db52009-09-23 17:17:35 -07001785 RegLocation rlDest;
1786 RegLocation rlResult;
1787 int BBBB = mir->dalvikInsn.vB;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001788 if (dalvikOpCode == OP_CONST_WIDE_16) {
Bill Buzbeec6f10662010-02-09 11:16:15 -08001789 rlDest = dvmCompilerGetDestWide(cUnit, mir, 0, 1);
1790 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Ben Chengbd1326d2010-04-02 15:04:53 -07001791 loadConstantNoClobber(cUnit, rlResult.lowReg, BBBB);
Bill Buzbee964a7b02010-01-28 12:54:19 -08001792 //TUNING: do high separately to avoid load dependency
Bill Buzbee1465db52009-09-23 17:17:35 -07001793 opRegRegImm(cUnit, kOpAsr, rlResult.highReg, rlResult.lowReg, 31);
1794 storeValueWide(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001795 } else if (dalvikOpCode == OP_CONST_16) {
Bill Buzbeec6f10662010-02-09 11:16:15 -08001796 rlDest = dvmCompilerGetDest(cUnit, mir, 0);
1797 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kAnyReg, true);
Ben Chengbd1326d2010-04-02 15:04:53 -07001798 loadConstantNoClobber(cUnit, rlResult.lowReg, BBBB);
Bill Buzbee1465db52009-09-23 17:17:35 -07001799 storeValue(cUnit, rlDest, rlResult);
1800 } else
Ben Chengba4fc8b2009-06-01 13:00:29 -07001801 return true;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001802 return false;
1803}
1804
1805/* Compare agaist zero */
1806static bool handleFmt21t(CompilationUnit *cUnit, MIR *mir, BasicBlock *bb,
Bill Buzbee89efc3d2009-07-28 11:22:22 -07001807 ArmLIR *labelList)
Ben Chengba4fc8b2009-06-01 13:00:29 -07001808{
1809 OpCode dalvikOpCode = mir->dalvikInsn.opCode;
Bill Buzbee89efc3d2009-07-28 11:22:22 -07001810 ArmConditionCode cond;
Bill Buzbeec6f10662010-02-09 11:16:15 -08001811 RegLocation rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07001812 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
1813 opRegImm(cUnit, kOpCmp, rlSrc.lowReg, 0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001814
Bill Buzbee270c1d62009-08-13 16:58:07 -07001815//TUNING: break this out to allow use of Thumb2 CB[N]Z
Ben Chengba4fc8b2009-06-01 13:00:29 -07001816 switch (dalvikOpCode) {
1817 case OP_IF_EQZ:
Bill Buzbee1465db52009-09-23 17:17:35 -07001818 cond = kArmCondEq;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001819 break;
1820 case OP_IF_NEZ:
Bill Buzbee1465db52009-09-23 17:17:35 -07001821 cond = kArmCondNe;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001822 break;
1823 case OP_IF_LTZ:
Bill Buzbee1465db52009-09-23 17:17:35 -07001824 cond = kArmCondLt;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001825 break;
1826 case OP_IF_GEZ:
Bill Buzbee1465db52009-09-23 17:17:35 -07001827 cond = kArmCondGe;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001828 break;
1829 case OP_IF_GTZ:
Bill Buzbee1465db52009-09-23 17:17:35 -07001830 cond = kArmCondGt;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001831 break;
1832 case OP_IF_LEZ:
Bill Buzbee1465db52009-09-23 17:17:35 -07001833 cond = kArmCondLe;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001834 break;
1835 default:
1836 cond = 0;
1837 LOGE("Unexpected opcode (%d) for Fmt21t\n", dalvikOpCode);
Bill Buzbeefc519dc2010-03-06 23:30:57 -08001838 dvmCompilerAbort(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001839 }
1840 genConditionalBranch(cUnit, cond, &labelList[bb->taken->id]);
1841 /* This mostly likely will be optimized away in a later phase */
1842 genUnconditionalBranch(cUnit, &labelList[bb->fallThrough->id]);
1843 return false;
1844}
1845
Elliott Hughesb4c05972010-02-24 16:36:18 -08001846static bool isPowerOfTwo(int x)
1847{
1848 return (x & (x - 1)) == 0;
1849}
1850
1851// Returns true if no more than two bits are set in 'x'.
1852static bool isPopCountLE2(unsigned int x)
1853{
1854 x &= x - 1;
1855 return (x & (x - 1)) == 0;
1856}
1857
1858// Returns the index of the lowest set bit in 'x'.
1859static int lowestSetBit(unsigned int x) {
1860 int bit_posn = 0;
1861 while ((x & 0xf) == 0) {
1862 bit_posn += 4;
1863 x >>= 4;
Bill Buzbee78cb0e22010-02-11 14:04:53 -08001864 }
Elliott Hughesb4c05972010-02-24 16:36:18 -08001865 while ((x & 1) == 0) {
1866 bit_posn++;
1867 x >>= 1;
1868 }
1869 return bit_posn;
1870}
1871
Elliott Hughes672511b2010-04-26 17:40:13 -07001872// Returns true if it added instructions to 'cUnit' to divide 'rlSrc' by 'lit'
1873// and store the result in 'rlDest'.
Elliott Hughesc7ad9b22010-04-28 13:52:02 -07001874static bool handleEasyDivide(CompilationUnit *cUnit, OpCode dalvikOpCode,
Elliott Hughes672511b2010-04-26 17:40:13 -07001875 RegLocation rlSrc, RegLocation rlDest, int lit)
1876{
1877 if (lit < 2 || !isPowerOfTwo(lit)) {
1878 return false;
1879 }
1880 int k = lowestSetBit(lit);
1881 if (k >= 30) {
1882 // Avoid special cases.
1883 return false;
1884 }
Elliott Hughes9c457022010-04-28 16:15:38 -07001885 bool div = (dalvikOpCode == OP_DIV_INT_LIT8 || dalvikOpCode == OP_DIV_INT_LIT16);
Elliott Hughes672511b2010-04-26 17:40:13 -07001886 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
1887 RegLocation rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Elliott Hughes9c457022010-04-28 16:15:38 -07001888 if (div) {
1889 int tReg = dvmCompilerAllocTemp(cUnit);
1890 if (lit == 2) {
1891 // Division by 2 is by far the most common division by constant.
1892 opRegRegImm(cUnit, kOpLsr, tReg, rlSrc.lowReg, 32 - k);
1893 opRegRegReg(cUnit, kOpAdd, tReg, tReg, rlSrc.lowReg);
1894 opRegRegImm(cUnit, kOpAsr, rlResult.lowReg, tReg, k);
1895 } else {
1896 opRegRegImm(cUnit, kOpAsr, tReg, rlSrc.lowReg, 31);
1897 opRegRegImm(cUnit, kOpLsr, tReg, tReg, 32 - k);
1898 opRegRegReg(cUnit, kOpAdd, tReg, tReg, rlSrc.lowReg);
1899 opRegRegImm(cUnit, kOpAsr, rlResult.lowReg, tReg, k);
1900 }
Elliott Hughes672511b2010-04-26 17:40:13 -07001901 } else {
Elliott Hughes9c457022010-04-28 16:15:38 -07001902 int cReg = dvmCompilerAllocTemp(cUnit);
1903 loadConstant(cUnit, cReg, lit - 1);
1904 int tReg1 = dvmCompilerAllocTemp(cUnit);
1905 int tReg2 = dvmCompilerAllocTemp(cUnit);
1906 if (lit == 2) {
1907 opRegRegImm(cUnit, kOpLsr, tReg1, rlSrc.lowReg, 32 - k);
1908 opRegRegReg(cUnit, kOpAdd, tReg2, tReg1, rlSrc.lowReg);
1909 opRegRegReg(cUnit, kOpAnd, tReg2, tReg2, cReg);
1910 opRegRegReg(cUnit, kOpSub, rlResult.lowReg, tReg2, tReg1);
1911 } else {
1912 opRegRegImm(cUnit, kOpAsr, tReg1, rlSrc.lowReg, 31);
1913 opRegRegImm(cUnit, kOpLsr, tReg1, tReg1, 32 - k);
1914 opRegRegReg(cUnit, kOpAdd, tReg2, tReg1, rlSrc.lowReg);
1915 opRegRegReg(cUnit, kOpAnd, tReg2, tReg2, cReg);
1916 opRegRegReg(cUnit, kOpSub, rlResult.lowReg, tReg2, tReg1);
1917 }
Elliott Hughes672511b2010-04-26 17:40:13 -07001918 }
1919 storeValue(cUnit, rlDest, rlResult);
1920 return true;
1921}
1922
Elliott Hughesb4c05972010-02-24 16:36:18 -08001923// Returns true if it added instructions to 'cUnit' to multiply 'rlSrc' by 'lit'
1924// and store the result in 'rlDest'.
1925static bool handleEasyMultiply(CompilationUnit *cUnit,
1926 RegLocation rlSrc, RegLocation rlDest, int lit)
1927{
1928 // Can we simplify this multiplication?
1929 bool powerOfTwo = false;
1930 bool popCountLE2 = false;
1931 bool powerOfTwoMinusOne = false;
1932 if (lit < 2) {
1933 // Avoid special cases.
1934 return false;
1935 } else if (isPowerOfTwo(lit)) {
1936 powerOfTwo = true;
1937 } else if (isPopCountLE2(lit)) {
1938 popCountLE2 = true;
1939 } else if (isPowerOfTwo(lit + 1)) {
1940 powerOfTwoMinusOne = true;
1941 } else {
1942 return false;
1943 }
1944 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
1945 RegLocation rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
1946 if (powerOfTwo) {
1947 // Shift.
1948 opRegRegImm(cUnit, kOpLsl, rlResult.lowReg, rlSrc.lowReg,
1949 lowestSetBit(lit));
1950 } else if (popCountLE2) {
1951 // Shift and add and shift.
1952 int firstBit = lowestSetBit(lit);
1953 int secondBit = lowestSetBit(lit ^ (1 << firstBit));
1954 genMultiplyByTwoBitMultiplier(cUnit, rlSrc, rlResult, lit,
1955 firstBit, secondBit);
1956 } else {
1957 // Reverse subtract: (src << (shift + 1)) - src.
1958 assert(powerOfTwoMinusOne);
1959 // TODO: rsb dst, src, src lsl#lowestSetBit(lit + 1)
1960 int tReg = dvmCompilerAllocTemp(cUnit);
1961 opRegRegImm(cUnit, kOpLsl, tReg, rlSrc.lowReg, lowestSetBit(lit + 1));
1962 opRegRegReg(cUnit, kOpSub, rlResult.lowReg, tReg, rlSrc.lowReg);
1963 }
1964 storeValue(cUnit, rlDest, rlResult);
1965 return true;
Bill Buzbee78cb0e22010-02-11 14:04:53 -08001966}
1967
Ben Chengba4fc8b2009-06-01 13:00:29 -07001968static bool handleFmt22b_Fmt22s(CompilationUnit *cUnit, MIR *mir)
1969{
1970 OpCode dalvikOpCode = mir->dalvikInsn.opCode;
Bill Buzbeec6f10662010-02-09 11:16:15 -08001971 RegLocation rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
1972 RegLocation rlDest = dvmCompilerGetDest(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07001973 RegLocation rlResult;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001974 int lit = mir->dalvikInsn.vC;
Ben Cheng4f489172009-09-27 17:08:35 -07001975 OpKind op = 0; /* Make gcc happy */
Bill Buzbee1465db52009-09-23 17:17:35 -07001976 int shiftOp = false;
1977 bool isDiv = false;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001978
Ben Chengba4fc8b2009-06-01 13:00:29 -07001979 switch (dalvikOpCode) {
Bill Buzbee1465db52009-09-23 17:17:35 -07001980 case OP_RSUB_INT_LIT8:
1981 case OP_RSUB_INT: {
1982 int tReg;
1983 //TUNING: add support for use of Arm rsub op
1984 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
Bill Buzbeec6f10662010-02-09 11:16:15 -08001985 tReg = dvmCompilerAllocTemp(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07001986 loadConstant(cUnit, tReg, lit);
Bill Buzbeec6f10662010-02-09 11:16:15 -08001987 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Bill Buzbee1465db52009-09-23 17:17:35 -07001988 opRegRegReg(cUnit, kOpSub, rlResult.lowReg,
1989 tReg, rlSrc.lowReg);
1990 storeValue(cUnit, rlDest, rlResult);
1991 return false;
1992 break;
1993 }
1994
Ben Chengba4fc8b2009-06-01 13:00:29 -07001995 case OP_ADD_INT_LIT8:
1996 case OP_ADD_INT_LIT16:
Bill Buzbee1465db52009-09-23 17:17:35 -07001997 op = kOpAdd;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001998 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001999 case OP_MUL_INT_LIT8:
Bill Buzbee78cb0e22010-02-11 14:04:53 -08002000 case OP_MUL_INT_LIT16: {
Elliott Hughesb4c05972010-02-24 16:36:18 -08002001 if (handleEasyMultiply(cUnit, rlSrc, rlDest, lit)) {
2002 return false;
Bill Buzbee78cb0e22010-02-11 14:04:53 -08002003 }
Elliott Hughesb4c05972010-02-24 16:36:18 -08002004 op = kOpMul;
Bill Buzbee1465db52009-09-23 17:17:35 -07002005 break;
Bill Buzbee78cb0e22010-02-11 14:04:53 -08002006 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07002007 case OP_AND_INT_LIT8:
2008 case OP_AND_INT_LIT16:
Bill Buzbee1465db52009-09-23 17:17:35 -07002009 op = kOpAnd;
2010 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002011 case OP_OR_INT_LIT8:
2012 case OP_OR_INT_LIT16:
Bill Buzbee1465db52009-09-23 17:17:35 -07002013 op = kOpOr;
2014 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002015 case OP_XOR_INT_LIT8:
2016 case OP_XOR_INT_LIT16:
Bill Buzbee1465db52009-09-23 17:17:35 -07002017 op = kOpXor;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002018 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002019 case OP_SHL_INT_LIT8:
Bill Buzbee0e605272009-12-01 14:28:05 -08002020 lit &= 31;
Bill Buzbee1465db52009-09-23 17:17:35 -07002021 shiftOp = true;
2022 op = kOpLsl;
2023 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002024 case OP_SHR_INT_LIT8:
Bill Buzbee0e605272009-12-01 14:28:05 -08002025 lit &= 31;
Bill Buzbee1465db52009-09-23 17:17:35 -07002026 shiftOp = true;
2027 op = kOpAsr;
2028 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002029 case OP_USHR_INT_LIT8:
Bill Buzbee0e605272009-12-01 14:28:05 -08002030 lit &= 31;
Bill Buzbee1465db52009-09-23 17:17:35 -07002031 shiftOp = true;
2032 op = kOpLsr;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002033 break;
2034
2035 case OP_DIV_INT_LIT8:
2036 case OP_DIV_INT_LIT16:
Ben Chengba4fc8b2009-06-01 13:00:29 -07002037 case OP_REM_INT_LIT8:
2038 case OP_REM_INT_LIT16:
2039 if (lit == 0) {
2040 /* Let the interpreter deal with div by 0 */
2041 genInterpSingleStep(cUnit, mir);
2042 return false;
2043 }
Elliott Hughesc7ad9b22010-04-28 13:52:02 -07002044 if (handleEasyDivide(cUnit, dalvikOpCode, rlSrc, rlDest, lit)) {
Elliott Hughes672511b2010-04-26 17:40:13 -07002045 return false;
2046 }
Bill Buzbeec6f10662010-02-09 11:16:15 -08002047 dvmCompilerFlushAllRegs(cUnit); /* Everything to home location */
Bill Buzbee1465db52009-09-23 17:17:35 -07002048 loadValueDirectFixed(cUnit, rlSrc, r0);
Bill Buzbeec6f10662010-02-09 11:16:15 -08002049 dvmCompilerClobber(cUnit, r0);
Bill Buzbee1465db52009-09-23 17:17:35 -07002050 if ((dalvikOpCode == OP_DIV_INT_LIT8) ||
2051 (dalvikOpCode == OP_DIV_INT_LIT16)) {
Ben Chengbd1326d2010-04-02 15:04:53 -07002052 LOAD_FUNC_ADDR(cUnit, r2, (int)__aeabi_idiv);
Bill Buzbee1465db52009-09-23 17:17:35 -07002053 isDiv = true;
2054 } else {
Ben Chengbd1326d2010-04-02 15:04:53 -07002055 LOAD_FUNC_ADDR(cUnit, r2, (int)__aeabi_idivmod);
Bill Buzbee1465db52009-09-23 17:17:35 -07002056 isDiv = false;
2057 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07002058 loadConstant(cUnit, r1, lit);
Bill Buzbee1465db52009-09-23 17:17:35 -07002059 opReg(cUnit, kOpBlx, r2);
Elliott Hughes6a555132010-02-25 15:41:42 -08002060 dvmCompilerClobberCallRegs(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07002061 if (isDiv)
Bill Buzbeec6f10662010-02-09 11:16:15 -08002062 rlResult = dvmCompilerGetReturn(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07002063 else
Bill Buzbeec6f10662010-02-09 11:16:15 -08002064 rlResult = dvmCompilerGetReturnAlt(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07002065 storeValue(cUnit, rlDest, rlResult);
2066 return false;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002067 break;
2068 default:
2069 return true;
2070 }
Bill Buzbee1465db52009-09-23 17:17:35 -07002071 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
Bill Buzbeec6f10662010-02-09 11:16:15 -08002072 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Bill Buzbee1465db52009-09-23 17:17:35 -07002073 // Avoid shifts by literal 0 - no support in Thumb. Change to copy
2074 if (shiftOp && (lit == 0)) {
2075 genRegCopy(cUnit, rlResult.lowReg, rlSrc.lowReg);
2076 } else {
2077 opRegRegImm(cUnit, op, rlResult.lowReg, rlSrc.lowReg, lit);
2078 }
2079 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002080 return false;
2081}
2082
2083static bool handleFmt22c(CompilationUnit *cUnit, MIR *mir)
2084{
2085 OpCode dalvikOpCode = mir->dalvikInsn.opCode;
2086 int fieldOffset;
2087
2088 if (dalvikOpCode >= OP_IGET && dalvikOpCode <= OP_IPUT_SHORT) {
2089 InstField *pInstField = (InstField *)
2090 cUnit->method->clazz->pDvmDex->pResFields[mir->dalvikInsn.vC];
Ben Chengba4fc8b2009-06-01 13:00:29 -07002091
Ben Chengdd6e8702010-05-07 13:05:47 -07002092 if (pInstField == NULL) {
2093 LOGE("Unexpected null instance field");
2094 dvmAbort();
2095 }
2096
Ben Chengba4fc8b2009-06-01 13:00:29 -07002097 fieldOffset = pInstField->byteOffset;
2098 } else {
Ben Chenga0e7b602009-10-13 23:09:01 -07002099 /* Deliberately break the code while make the compiler happy */
2100 fieldOffset = -1;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002101 }
2102 switch (dalvikOpCode) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07002103 case OP_NEW_ARRAY: {
Bill Buzbee1465db52009-09-23 17:17:35 -07002104 // Generates a call - use explicit registers
Bill Buzbeec6f10662010-02-09 11:16:15 -08002105 RegLocation rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
2106 RegLocation rlDest = dvmCompilerGetDest(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07002107 RegLocation rlResult;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002108 void *classPtr = (void*)
2109 (cUnit->method->clazz->pDvmDex->pResClasses[mir->dalvikInsn.vC]);
Ben Chengdd6e8702010-05-07 13:05:47 -07002110
2111 if (classPtr == NULL) {
2112 LOGE("Unexpected null class");
2113 dvmAbort();
2114 }
2115
Bill Buzbeec6f10662010-02-09 11:16:15 -08002116 dvmCompilerFlushAllRegs(cUnit); /* Everything to home location */
Bill Buzbee1465db52009-09-23 17:17:35 -07002117 genExportPC(cUnit, mir);
2118 loadValueDirectFixed(cUnit, rlSrc, r1); /* Len */
Ben Chengba4fc8b2009-06-01 13:00:29 -07002119 loadConstant(cUnit, r0, (int) classPtr );
Ben Chengbd1326d2010-04-02 15:04:53 -07002120 LOAD_FUNC_ADDR(cUnit, r3, (int)dvmAllocArrayByClass);
Ben Cheng4f489172009-09-27 17:08:35 -07002121 /*
2122 * "len < 0": bail to the interpreter to re-execute the
2123 * instruction
2124 */
Carl Shapiroe3c01da2010-05-20 22:54:18 -07002125 genRegImmCheck(cUnit, kArmCondMi, r1, 0, mir->offset, NULL);
Bill Buzbee270c1d62009-08-13 16:58:07 -07002126 loadConstant(cUnit, r2, ALLOC_DONT_TRACK);
Bill Buzbee1465db52009-09-23 17:17:35 -07002127 opReg(cUnit, kOpBlx, r3);
Elliott Hughes6a555132010-02-25 15:41:42 -08002128 dvmCompilerClobberCallRegs(cUnit);
Ben Cheng4f489172009-09-27 17:08:35 -07002129 /* generate a branch over if allocation is successful */
Bill Buzbee1465db52009-09-23 17:17:35 -07002130 opRegImm(cUnit, kOpCmp, r0, 0); /* NULL? */
2131 ArmLIR *branchOver = opCondBranch(cUnit, kArmCondNe);
Ben Cheng4f489172009-09-27 17:08:35 -07002132 /*
2133 * OOM exception needs to be thrown here and cannot re-execute
2134 */
2135 loadConstant(cUnit, r0,
2136 (int) (cUnit->method->insns + mir->offset));
2137 genDispatchToHandler(cUnit, TEMPLATE_THROW_EXCEPTION_COMMON);
2138 /* noreturn */
2139
Bill Buzbee1465db52009-09-23 17:17:35 -07002140 ArmLIR *target = newLIR0(cUnit, kArmPseudoTargetLabel);
Ben Cheng4f489172009-09-27 17:08:35 -07002141 target->defMask = ENCODE_ALL;
2142 branchOver->generic.target = (LIR *) target;
Bill Buzbeec6f10662010-02-09 11:16:15 -08002143 rlResult = dvmCompilerGetReturn(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07002144 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002145 break;
2146 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07002147 case OP_INSTANCE_OF: {
Bill Buzbee1465db52009-09-23 17:17:35 -07002148 // May generate a call - use explicit registers
Bill Buzbeec6f10662010-02-09 11:16:15 -08002149 RegLocation rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
2150 RegLocation rlDest = dvmCompilerGetDest(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07002151 RegLocation rlResult;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002152 ClassObject *classPtr =
2153 (cUnit->method->clazz->pDvmDex->pResClasses[mir->dalvikInsn.vC]);
Bill Buzbee480e6782010-01-27 15:43:08 -08002154 /*
2155 * Note: It is possible that classPtr is NULL at this point,
2156 * even though this instruction has been successfully interpreted.
2157 * If the previous interpretation had a null source, the
2158 * interpreter would not have bothered to resolve the clazz.
2159 * Bail out to the interpreter in this case, and log it
2160 * so that we can tell if it happens frequently.
2161 */
2162 if (classPtr == NULL) {
2163 LOGD("null clazz in OP_INSTANCE_OF, single-stepping");
2164 genInterpSingleStep(cUnit, mir);
2165 break;
2166 }
Bill Buzbeec6f10662010-02-09 11:16:15 -08002167 dvmCompilerFlushAllRegs(cUnit); /* Everything to home location */
Bill Buzbee1465db52009-09-23 17:17:35 -07002168 loadValueDirectFixed(cUnit, rlSrc, r0); /* Ref */
Ben Chengba4fc8b2009-06-01 13:00:29 -07002169 loadConstant(cUnit, r2, (int) classPtr );
Bill Buzbee270c1d62009-08-13 16:58:07 -07002170//TUNING: compare to 0 primative to allow use of CB[N]Z
Bill Buzbee1465db52009-09-23 17:17:35 -07002171 opRegImm(cUnit, kOpCmp, r0, 0); /* NULL? */
Ben Cheng752c7942009-06-22 10:50:07 -07002172 /* When taken r0 has NULL which can be used for store directly */
Bill Buzbee1465db52009-09-23 17:17:35 -07002173 ArmLIR *branch1 = opCondBranch(cUnit, kArmCondEq);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002174 /* r1 now contains object->clazz */
Bill Buzbee270c1d62009-08-13 16:58:07 -07002175 loadWordDisp(cUnit, r0, offsetof(Object, clazz), r1);
Bill Buzbee1465db52009-09-23 17:17:35 -07002176 /* r1 now contains object->clazz */
Ben Chengbd1326d2010-04-02 15:04:53 -07002177 LOAD_FUNC_ADDR(cUnit, r3, (int)dvmInstanceofNonTrivial);
Ben Cheng752c7942009-06-22 10:50:07 -07002178 loadConstant(cUnit, r0, 1); /* Assume true */
Bill Buzbee1465db52009-09-23 17:17:35 -07002179 opRegReg(cUnit, kOpCmp, r1, r2);
2180 ArmLIR *branch2 = opCondBranch(cUnit, kArmCondEq);
2181 genRegCopy(cUnit, r0, r1);
2182 genRegCopy(cUnit, r1, r2);
2183 opReg(cUnit, kOpBlx, r3);
Elliott Hughes6a555132010-02-25 15:41:42 -08002184 dvmCompilerClobberCallRegs(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002185 /* branch target here */
Bill Buzbee1465db52009-09-23 17:17:35 -07002186 ArmLIR *target = newLIR0(cUnit, kArmPseudoTargetLabel);
Ben Chengd7d426a2009-09-22 11:23:36 -07002187 target->defMask = ENCODE_ALL;
Bill Buzbeec6f10662010-02-09 11:16:15 -08002188 rlResult = dvmCompilerGetReturn(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07002189 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002190 branch1->generic.target = (LIR *)target;
2191 branch2->generic.target = (LIR *)target;
2192 break;
2193 }
2194 case OP_IGET_WIDE:
2195 genIGetWide(cUnit, mir, fieldOffset);
2196 break;
2197 case OP_IGET:
2198 case OP_IGET_OBJECT:
Bill Buzbee1465db52009-09-23 17:17:35 -07002199 genIGet(cUnit, mir, kWord, fieldOffset);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002200 break;
2201 case OP_IGET_BOOLEAN:
Bill Buzbee1465db52009-09-23 17:17:35 -07002202 genIGet(cUnit, mir, kUnsignedByte, fieldOffset);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002203 break;
2204 case OP_IGET_BYTE:
Bill Buzbee1465db52009-09-23 17:17:35 -07002205 genIGet(cUnit, mir, kSignedByte, fieldOffset);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002206 break;
2207 case OP_IGET_CHAR:
Bill Buzbee1465db52009-09-23 17:17:35 -07002208 genIGet(cUnit, mir, kUnsignedHalf, fieldOffset);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002209 break;
2210 case OP_IGET_SHORT:
Bill Buzbee1465db52009-09-23 17:17:35 -07002211 genIGet(cUnit, mir, kSignedHalf, fieldOffset);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002212 break;
2213 case OP_IPUT_WIDE:
2214 genIPutWide(cUnit, mir, fieldOffset);
2215 break;
2216 case OP_IPUT:
2217 case OP_IPUT_OBJECT:
Bill Buzbee1465db52009-09-23 17:17:35 -07002218 genIPut(cUnit, mir, kWord, fieldOffset);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002219 break;
2220 case OP_IPUT_SHORT:
2221 case OP_IPUT_CHAR:
Bill Buzbee1465db52009-09-23 17:17:35 -07002222 genIPut(cUnit, mir, kUnsignedHalf, fieldOffset);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002223 break;
2224 case OP_IPUT_BYTE:
2225 case OP_IPUT_BOOLEAN:
Bill Buzbee1465db52009-09-23 17:17:35 -07002226 genIPut(cUnit, mir, kUnsignedByte, fieldOffset);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002227 break;
Bill Buzbeeb16344a2010-03-15 17:19:12 -07002228 case OP_IGET_WIDE_VOLATILE:
2229 case OP_IPUT_WIDE_VOLATILE:
2230 case OP_SGET_WIDE_VOLATILE:
2231 case OP_SPUT_WIDE_VOLATILE:
2232 genInterpSingleStep(cUnit, mir);
2233 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002234 default:
2235 return true;
2236 }
2237 return false;
2238}
2239
2240static bool handleFmt22cs(CompilationUnit *cUnit, MIR *mir)
2241{
2242 OpCode dalvikOpCode = mir->dalvikInsn.opCode;
2243 int fieldOffset = mir->dalvikInsn.vC;
2244 switch (dalvikOpCode) {
2245 case OP_IGET_QUICK:
2246 case OP_IGET_OBJECT_QUICK:
Bill Buzbee1465db52009-09-23 17:17:35 -07002247 genIGet(cUnit, mir, kWord, fieldOffset);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002248 break;
2249 case OP_IPUT_QUICK:
2250 case OP_IPUT_OBJECT_QUICK:
Bill Buzbee1465db52009-09-23 17:17:35 -07002251 genIPut(cUnit, mir, kWord, fieldOffset);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002252 break;
2253 case OP_IGET_WIDE_QUICK:
2254 genIGetWide(cUnit, mir, fieldOffset);
2255 break;
2256 case OP_IPUT_WIDE_QUICK:
2257 genIPutWide(cUnit, mir, fieldOffset);
2258 break;
2259 default:
2260 return true;
2261 }
2262 return false;
2263
2264}
2265
2266/* Compare agaist zero */
2267static bool handleFmt22t(CompilationUnit *cUnit, MIR *mir, BasicBlock *bb,
Bill Buzbee89efc3d2009-07-28 11:22:22 -07002268 ArmLIR *labelList)
Ben Chengba4fc8b2009-06-01 13:00:29 -07002269{
2270 OpCode dalvikOpCode = mir->dalvikInsn.opCode;
Bill Buzbee89efc3d2009-07-28 11:22:22 -07002271 ArmConditionCode cond;
Bill Buzbeec6f10662010-02-09 11:16:15 -08002272 RegLocation rlSrc1 = dvmCompilerGetSrc(cUnit, mir, 0);
2273 RegLocation rlSrc2 = dvmCompilerGetSrc(cUnit, mir, 1);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002274
Bill Buzbee1465db52009-09-23 17:17:35 -07002275 rlSrc1 = loadValue(cUnit, rlSrc1, kCoreReg);
2276 rlSrc2 = loadValue(cUnit, rlSrc2, kCoreReg);
2277 opRegReg(cUnit, kOpCmp, rlSrc1.lowReg, rlSrc2.lowReg);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002278
2279 switch (dalvikOpCode) {
2280 case OP_IF_EQ:
Bill Buzbee1465db52009-09-23 17:17:35 -07002281 cond = kArmCondEq;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002282 break;
2283 case OP_IF_NE:
Bill Buzbee1465db52009-09-23 17:17:35 -07002284 cond = kArmCondNe;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002285 break;
2286 case OP_IF_LT:
Bill Buzbee1465db52009-09-23 17:17:35 -07002287 cond = kArmCondLt;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002288 break;
2289 case OP_IF_GE:
Bill Buzbee1465db52009-09-23 17:17:35 -07002290 cond = kArmCondGe;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002291 break;
2292 case OP_IF_GT:
Bill Buzbee1465db52009-09-23 17:17:35 -07002293 cond = kArmCondGt;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002294 break;
2295 case OP_IF_LE:
Bill Buzbee1465db52009-09-23 17:17:35 -07002296 cond = kArmCondLe;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002297 break;
2298 default:
2299 cond = 0;
2300 LOGE("Unexpected opcode (%d) for Fmt22t\n", dalvikOpCode);
Bill Buzbeefc519dc2010-03-06 23:30:57 -08002301 dvmCompilerAbort(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002302 }
2303 genConditionalBranch(cUnit, cond, &labelList[bb->taken->id]);
2304 /* This mostly likely will be optimized away in a later phase */
2305 genUnconditionalBranch(cUnit, &labelList[bb->fallThrough->id]);
2306 return false;
2307}
2308
2309static bool handleFmt22x_Fmt32x(CompilationUnit *cUnit, MIR *mir)
2310{
2311 OpCode opCode = mir->dalvikInsn.opCode;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002312
2313 switch (opCode) {
2314 case OP_MOVE_16:
2315 case OP_MOVE_OBJECT_16:
2316 case OP_MOVE_FROM16:
Ben Chenge9695e52009-06-16 16:11:47 -07002317 case OP_MOVE_OBJECT_FROM16: {
Bill Buzbeec6f10662010-02-09 11:16:15 -08002318 storeValue(cUnit, dvmCompilerGetDest(cUnit, mir, 0),
2319 dvmCompilerGetSrc(cUnit, mir, 0));
Ben Chengba4fc8b2009-06-01 13:00:29 -07002320 break;
Ben Chenge9695e52009-06-16 16:11:47 -07002321 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07002322 case OP_MOVE_WIDE_16:
Ben Chenge9695e52009-06-16 16:11:47 -07002323 case OP_MOVE_WIDE_FROM16: {
Bill Buzbeec6f10662010-02-09 11:16:15 -08002324 storeValueWide(cUnit, dvmCompilerGetDestWide(cUnit, mir, 0, 1),
2325 dvmCompilerGetSrcWide(cUnit, mir, 0, 1));
Ben Chengba4fc8b2009-06-01 13:00:29 -07002326 break;
Ben Chenge9695e52009-06-16 16:11:47 -07002327 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07002328 default:
2329 return true;
2330 }
2331 return false;
2332}
2333
2334static bool handleFmt23x(CompilationUnit *cUnit, MIR *mir)
2335{
2336 OpCode opCode = mir->dalvikInsn.opCode;
Bill Buzbee1465db52009-09-23 17:17:35 -07002337 RegLocation rlSrc1;
2338 RegLocation rlSrc2;
2339 RegLocation rlDest;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002340
2341 if ( (opCode >= OP_ADD_INT) && (opCode <= OP_REM_DOUBLE)) {
Ben Cheng5d90c202009-11-22 23:31:11 -08002342 return genArithOp( cUnit, mir );
Ben Chengba4fc8b2009-06-01 13:00:29 -07002343 }
2344
Bill Buzbee1465db52009-09-23 17:17:35 -07002345 /* APUTs have 3 sources and no targets */
2346 if (mir->ssaRep->numDefs == 0) {
2347 if (mir->ssaRep->numUses == 3) {
Bill Buzbeec6f10662010-02-09 11:16:15 -08002348 rlDest = dvmCompilerGetSrc(cUnit, mir, 0);
2349 rlSrc1 = dvmCompilerGetSrc(cUnit, mir, 1);
2350 rlSrc2 = dvmCompilerGetSrc(cUnit, mir, 2);
Bill Buzbee1465db52009-09-23 17:17:35 -07002351 } else {
2352 assert(mir->ssaRep->numUses == 4);
Bill Buzbeec6f10662010-02-09 11:16:15 -08002353 rlDest = dvmCompilerGetSrcWide(cUnit, mir, 0, 1);
2354 rlSrc1 = dvmCompilerGetSrc(cUnit, mir, 2);
2355 rlSrc2 = dvmCompilerGetSrc(cUnit, mir, 3);
Bill Buzbee1465db52009-09-23 17:17:35 -07002356 }
2357 } else {
2358 /* Two sources and 1 dest. Deduce the operand sizes */
2359 if (mir->ssaRep->numUses == 4) {
Bill Buzbeec6f10662010-02-09 11:16:15 -08002360 rlSrc1 = dvmCompilerGetSrcWide(cUnit, mir, 0, 1);
2361 rlSrc2 = dvmCompilerGetSrcWide(cUnit, mir, 2, 3);
Bill Buzbee1465db52009-09-23 17:17:35 -07002362 } else {
2363 assert(mir->ssaRep->numUses == 2);
Bill Buzbeec6f10662010-02-09 11:16:15 -08002364 rlSrc1 = dvmCompilerGetSrc(cUnit, mir, 0);
2365 rlSrc2 = dvmCompilerGetSrc(cUnit, mir, 1);
Bill Buzbee1465db52009-09-23 17:17:35 -07002366 }
2367 if (mir->ssaRep->numDefs == 2) {
Bill Buzbeec6f10662010-02-09 11:16:15 -08002368 rlDest = dvmCompilerGetDestWide(cUnit, mir, 0, 1);
Bill Buzbee1465db52009-09-23 17:17:35 -07002369 } else {
2370 assert(mir->ssaRep->numDefs == 1);
Bill Buzbeec6f10662010-02-09 11:16:15 -08002371 rlDest = dvmCompilerGetDest(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07002372 }
2373 }
2374
2375
Ben Chengba4fc8b2009-06-01 13:00:29 -07002376 switch (opCode) {
Bill Buzbeed45ba372009-06-15 17:00:57 -07002377 case OP_CMPL_FLOAT:
2378 case OP_CMPG_FLOAT:
2379 case OP_CMPL_DOUBLE:
2380 case OP_CMPG_DOUBLE:
Ben Cheng5d90c202009-11-22 23:31:11 -08002381 return genCmpFP(cUnit, mir, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002382 case OP_CMP_LONG:
Bill Buzbee1465db52009-09-23 17:17:35 -07002383 genCmpLong(cUnit, mir, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002384 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002385 case OP_AGET_WIDE:
Bill Buzbee1465db52009-09-23 17:17:35 -07002386 genArrayGet(cUnit, mir, kLong, rlSrc1, rlSrc2, rlDest, 3);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002387 break;
2388 case OP_AGET:
2389 case OP_AGET_OBJECT:
Bill Buzbee1465db52009-09-23 17:17:35 -07002390 genArrayGet(cUnit, mir, kWord, rlSrc1, rlSrc2, rlDest, 2);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002391 break;
2392 case OP_AGET_BOOLEAN:
Bill Buzbee1465db52009-09-23 17:17:35 -07002393 genArrayGet(cUnit, mir, kUnsignedByte, rlSrc1, rlSrc2, rlDest, 0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002394 break;
2395 case OP_AGET_BYTE:
Bill Buzbee1465db52009-09-23 17:17:35 -07002396 genArrayGet(cUnit, mir, kSignedByte, rlSrc1, rlSrc2, rlDest, 0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002397 break;
2398 case OP_AGET_CHAR:
Bill Buzbee1465db52009-09-23 17:17:35 -07002399 genArrayGet(cUnit, mir, kUnsignedHalf, rlSrc1, rlSrc2, rlDest, 1);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002400 break;
2401 case OP_AGET_SHORT:
Bill Buzbee1465db52009-09-23 17:17:35 -07002402 genArrayGet(cUnit, mir, kSignedHalf, rlSrc1, rlSrc2, rlDest, 1);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002403 break;
2404 case OP_APUT_WIDE:
Bill Buzbee1465db52009-09-23 17:17:35 -07002405 genArrayPut(cUnit, mir, kLong, rlSrc1, rlSrc2, rlDest, 3);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002406 break;
2407 case OP_APUT:
Bill Buzbee1465db52009-09-23 17:17:35 -07002408 genArrayPut(cUnit, mir, kWord, rlSrc1, rlSrc2, rlDest, 2);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002409 break;
Bill Buzbeebe6534f2010-03-12 16:01:35 -08002410 case OP_APUT_OBJECT:
2411 genArrayObjectPut(cUnit, mir, rlSrc1, rlSrc2, rlDest, 2);
2412 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002413 case OP_APUT_SHORT:
2414 case OP_APUT_CHAR:
Bill Buzbee1465db52009-09-23 17:17:35 -07002415 genArrayPut(cUnit, mir, kUnsignedHalf, rlSrc1, rlSrc2, rlDest, 1);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002416 break;
2417 case OP_APUT_BYTE:
2418 case OP_APUT_BOOLEAN:
Bill Buzbee1465db52009-09-23 17:17:35 -07002419 genArrayPut(cUnit, mir, kUnsignedByte, rlSrc1, rlSrc2, rlDest, 0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002420 break;
2421 default:
2422 return true;
2423 }
2424 return false;
2425}
2426
Ben Cheng6c10a972009-10-29 14:39:18 -07002427/*
2428 * Find the matching case.
2429 *
2430 * return values:
2431 * r0 (low 32-bit): pc of the chaining cell corresponding to the resolved case,
2432 * including default which is placed at MIN(size, MAX_CHAINED_SWITCH_CASES).
2433 * r1 (high 32-bit): the branch offset of the matching case (only for indexes
2434 * above MAX_CHAINED_SWITCH_CASES).
2435 *
2436 * Instructions around the call are:
2437 *
2438 * mov r2, pc
2439 * blx &findPackedSwitchIndex
2440 * mov pc, r0
2441 * .align4
Bill Buzbeebd047242010-05-13 13:02:53 -07002442 * chaining cell for case 0 [12 bytes]
2443 * chaining cell for case 1 [12 bytes]
Ben Cheng6c10a972009-10-29 14:39:18 -07002444 * :
Bill Buzbeebd047242010-05-13 13:02:53 -07002445 * chaining cell for case MIN(size, MAX_CHAINED_SWITCH_CASES)-1 [12 bytes]
Ben Cheng6c10a972009-10-29 14:39:18 -07002446 * chaining cell for case default [8 bytes]
2447 * noChain exit
2448 */
Ben Chengbd1326d2010-04-02 15:04:53 -07002449static s8 findPackedSwitchIndex(const u2* switchData, int testVal, int pc)
Ben Cheng6c10a972009-10-29 14:39:18 -07002450{
2451 int size;
2452 int firstKey;
2453 const int *entries;
2454 int index;
2455 int jumpIndex;
2456 int caseDPCOffset = 0;
2457 /* In Thumb mode pc is 4 ahead of the "mov r2, pc" instruction */
2458 int chainingPC = (pc + 4) & ~3;
2459
2460 /*
2461 * Packed switch data format:
2462 * ushort ident = 0x0100 magic value
2463 * ushort size number of entries in the table
2464 * int first_key first (and lowest) switch case value
2465 * int targets[size] branch targets, relative to switch opcode
2466 *
2467 * Total size is (4+size*2) 16-bit code units.
2468 */
2469 size = switchData[1];
2470 assert(size > 0);
2471
2472 firstKey = switchData[2];
2473 firstKey |= switchData[3] << 16;
2474
2475
2476 /* The entries are guaranteed to be aligned on a 32-bit boundary;
2477 * we can treat them as a native int array.
2478 */
2479 entries = (const int*) &switchData[4];
2480 assert(((u4)entries & 0x3) == 0);
2481
2482 index = testVal - firstKey;
2483
2484 /* Jump to the default cell */
2485 if (index < 0 || index >= size) {
2486 jumpIndex = MIN(size, MAX_CHAINED_SWITCH_CASES);
2487 /* Jump to the non-chaining exit point */
2488 } else if (index >= MAX_CHAINED_SWITCH_CASES) {
2489 jumpIndex = MAX_CHAINED_SWITCH_CASES + 1;
2490 caseDPCOffset = entries[index];
2491 /* Jump to the inline chaining cell */
2492 } else {
2493 jumpIndex = index;
2494 }
2495
Bill Buzbeebd047242010-05-13 13:02:53 -07002496 chainingPC += jumpIndex * CHAIN_CELL_NORMAL_SIZE;
Ben Cheng6c10a972009-10-29 14:39:18 -07002497 return (((s8) caseDPCOffset) << 32) | (u8) chainingPC;
2498}
2499
2500/* See comments for findPackedSwitchIndex */
Ben Chengbd1326d2010-04-02 15:04:53 -07002501static s8 findSparseSwitchIndex(const u2* switchData, int testVal, int pc)
Ben Cheng6c10a972009-10-29 14:39:18 -07002502{
2503 int size;
2504 const int *keys;
2505 const int *entries;
2506 int chainingPC = (pc + 4) & ~3;
2507 int i;
2508
2509 /*
2510 * Sparse switch data format:
2511 * ushort ident = 0x0200 magic value
2512 * ushort size number of entries in the table; > 0
2513 * int keys[size] keys, sorted low-to-high; 32-bit aligned
2514 * int targets[size] branch targets, relative to switch opcode
2515 *
2516 * Total size is (2+size*4) 16-bit code units.
2517 */
2518
2519 size = switchData[1];
2520 assert(size > 0);
2521
2522 /* The keys are guaranteed to be aligned on a 32-bit boundary;
2523 * we can treat them as a native int array.
2524 */
2525 keys = (const int*) &switchData[2];
2526 assert(((u4)keys & 0x3) == 0);
2527
2528 /* The entries are guaranteed to be aligned on a 32-bit boundary;
2529 * we can treat them as a native int array.
2530 */
2531 entries = keys + size;
2532 assert(((u4)entries & 0x3) == 0);
2533
2534 /*
2535 * Run through the list of keys, which are guaranteed to
2536 * be sorted low-to-high.
2537 *
2538 * Most tables have 3-4 entries. Few have more than 10. A binary
2539 * search here is probably not useful.
2540 */
2541 for (i = 0; i < size; i++) {
2542 int k = keys[i];
2543 if (k == testVal) {
2544 /* MAX_CHAINED_SWITCH_CASES + 1 is the start of the overflow case */
2545 int jumpIndex = (i < MAX_CHAINED_SWITCH_CASES) ?
2546 i : MAX_CHAINED_SWITCH_CASES + 1;
Bill Buzbeebd047242010-05-13 13:02:53 -07002547 chainingPC += jumpIndex * CHAIN_CELL_NORMAL_SIZE;
Ben Cheng6c10a972009-10-29 14:39:18 -07002548 return (((s8) entries[i]) << 32) | (u8) chainingPC;
2549 } else if (k > testVal) {
2550 break;
2551 }
2552 }
Bill Buzbeebd047242010-05-13 13:02:53 -07002553 return chainingPC + MIN(size, MAX_CHAINED_SWITCH_CASES) *
2554 CHAIN_CELL_NORMAL_SIZE;
Ben Cheng6c10a972009-10-29 14:39:18 -07002555}
2556
Ben Chengba4fc8b2009-06-01 13:00:29 -07002557static bool handleFmt31t(CompilationUnit *cUnit, MIR *mir)
2558{
2559 OpCode dalvikOpCode = mir->dalvikInsn.opCode;
2560 switch (dalvikOpCode) {
2561 case OP_FILL_ARRAY_DATA: {
Bill Buzbeec6f10662010-02-09 11:16:15 -08002562 RegLocation rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07002563 // Making a call - use explicit registers
Bill Buzbeec6f10662010-02-09 11:16:15 -08002564 dvmCompilerFlushAllRegs(cUnit); /* Everything to home location */
Bill Buzbee1465db52009-09-23 17:17:35 -07002565 genExportPC(cUnit, mir);
2566 loadValueDirectFixed(cUnit, rlSrc, r0);
Ben Chengbd1326d2010-04-02 15:04:53 -07002567 LOAD_FUNC_ADDR(cUnit, r2, (int)dvmInterpHandleFillArrayData);
Ben Cheng6c10a972009-10-29 14:39:18 -07002568 loadConstant(cUnit, r1,
2569 (int) (cUnit->method->insns + mir->offset + mir->dalvikInsn.vB));
Bill Buzbee1465db52009-09-23 17:17:35 -07002570 opReg(cUnit, kOpBlx, r2);
Elliott Hughes6a555132010-02-25 15:41:42 -08002571 dvmCompilerClobberCallRegs(cUnit);
Bill Buzbeefd023aa2009-11-02 09:23:49 -08002572 /* generate a branch over if successful */
2573 opRegImm(cUnit, kOpCmp, r0, 0); /* NULL? */
2574 ArmLIR *branchOver = opCondBranch(cUnit, kArmCondNe);
2575 loadConstant(cUnit, r0,
2576 (int) (cUnit->method->insns + mir->offset));
2577 genDispatchToHandler(cUnit, TEMPLATE_THROW_EXCEPTION_COMMON);
2578 ArmLIR *target = newLIR0(cUnit, kArmPseudoTargetLabel);
2579 target->defMask = ENCODE_ALL;
2580 branchOver->generic.target = (LIR *) target;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002581 break;
2582 }
2583 /*
Ben Cheng6c10a972009-10-29 14:39:18 -07002584 * Compute the goto target of up to
2585 * MIN(switchSize, MAX_CHAINED_SWITCH_CASES) + 1 chaining cells.
2586 * See the comment before findPackedSwitchIndex for the code layout.
Ben Chengba4fc8b2009-06-01 13:00:29 -07002587 */
2588 case OP_PACKED_SWITCH:
2589 case OP_SPARSE_SWITCH: {
Bill Buzbeec6f10662010-02-09 11:16:15 -08002590 RegLocation rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
2591 dvmCompilerFlushAllRegs(cUnit); /* Everything to home location */
Bill Buzbee1465db52009-09-23 17:17:35 -07002592 loadValueDirectFixed(cUnit, rlSrc, r1);
Bill Buzbeec6f10662010-02-09 11:16:15 -08002593 dvmCompilerLockAllTemps(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002594 if (dalvikOpCode == OP_PACKED_SWITCH) {
Ben Chengbd1326d2010-04-02 15:04:53 -07002595 LOAD_FUNC_ADDR(cUnit, r4PC, (int)findPackedSwitchIndex);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002596 } else {
Ben Chengbd1326d2010-04-02 15:04:53 -07002597 LOAD_FUNC_ADDR(cUnit, r4PC, (int)findSparseSwitchIndex);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002598 }
Ben Cheng6c10a972009-10-29 14:39:18 -07002599 /* r0 <- Addr of the switch data */
2600 loadConstant(cUnit, r0,
2601 (int) (cUnit->method->insns + mir->offset + mir->dalvikInsn.vB));
2602 /* r2 <- pc of the instruction following the blx */
2603 opRegReg(cUnit, kOpMov, r2, rpc);
Bill Buzbee1465db52009-09-23 17:17:35 -07002604 opReg(cUnit, kOpBlx, r4PC);
Elliott Hughes6a555132010-02-25 15:41:42 -08002605 dvmCompilerClobberCallRegs(cUnit);
Ben Cheng6c10a972009-10-29 14:39:18 -07002606 /* pc <- computed goto target */
2607 opRegReg(cUnit, kOpMov, rpc, r0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002608 break;
2609 }
2610 default:
2611 return true;
2612 }
2613 return false;
2614}
2615
2616static bool handleFmt35c_3rc(CompilationUnit *cUnit, MIR *mir, BasicBlock *bb,
Bill Buzbee89efc3d2009-07-28 11:22:22 -07002617 ArmLIR *labelList)
Ben Chengba4fc8b2009-06-01 13:00:29 -07002618{
Bill Buzbee9bc3df32009-07-30 10:52:29 -07002619 ArmLIR *retChainingCell = NULL;
Bill Buzbee89efc3d2009-07-28 11:22:22 -07002620 ArmLIR *pcrLabel = NULL;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002621
Bill Buzbeef4ce16f2009-07-28 13:28:25 -07002622 if (bb->fallThrough != NULL)
2623 retChainingCell = &labelList[bb->fallThrough->id];
2624
Ben Chengba4fc8b2009-06-01 13:00:29 -07002625 DecodedInstruction *dInsn = &mir->dalvikInsn;
2626 switch (mir->dalvikInsn.opCode) {
2627 /*
2628 * calleeMethod = this->clazz->vtable[
2629 * method->clazz->pDvmDex->pResMethods[BBBB]->methodIndex
2630 * ]
2631 */
2632 case OP_INVOKE_VIRTUAL:
2633 case OP_INVOKE_VIRTUAL_RANGE: {
Bill Buzbee89efc3d2009-07-28 11:22:22 -07002634 ArmLIR *predChainingCell = &labelList[bb->taken->id];
Ben Chengba4fc8b2009-06-01 13:00:29 -07002635 int methodIndex =
2636 cUnit->method->clazz->pDvmDex->pResMethods[dInsn->vB]->
2637 methodIndex;
2638
2639 if (mir->dalvikInsn.opCode == OP_INVOKE_VIRTUAL)
2640 genProcessArgsNoRange(cUnit, mir, dInsn, &pcrLabel);
2641 else
2642 genProcessArgsRange(cUnit, mir, dInsn, &pcrLabel);
2643
Ben Cheng38329f52009-07-07 14:19:20 -07002644 genInvokeVirtualCommon(cUnit, mir, methodIndex,
2645 retChainingCell,
2646 predChainingCell,
2647 pcrLabel);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002648 break;
2649 }
2650 /*
2651 * calleeMethod = method->clazz->super->vtable[method->clazz->pDvmDex
2652 * ->pResMethods[BBBB]->methodIndex]
2653 */
Ben Chengba4fc8b2009-06-01 13:00:29 -07002654 case OP_INVOKE_SUPER:
2655 case OP_INVOKE_SUPER_RANGE: {
2656 int mIndex = cUnit->method->clazz->pDvmDex->
2657 pResMethods[dInsn->vB]->methodIndex;
2658 const Method *calleeMethod =
2659 cUnit->method->clazz->super->vtable[mIndex];
2660
2661 if (mir->dalvikInsn.opCode == OP_INVOKE_SUPER)
2662 genProcessArgsNoRange(cUnit, mir, dInsn, &pcrLabel);
2663 else
2664 genProcessArgsRange(cUnit, mir, dInsn, &pcrLabel);
2665
2666 /* r0 = calleeMethod */
2667 loadConstant(cUnit, r0, (int) calleeMethod);
2668
Ben Cheng38329f52009-07-07 14:19:20 -07002669 genInvokeSingletonCommon(cUnit, mir, bb, labelList, pcrLabel,
2670 calleeMethod);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002671 break;
2672 }
2673 /* calleeMethod = method->clazz->pDvmDex->pResMethods[BBBB] */
2674 case OP_INVOKE_DIRECT:
2675 case OP_INVOKE_DIRECT_RANGE: {
2676 const Method *calleeMethod =
2677 cUnit->method->clazz->pDvmDex->pResMethods[dInsn->vB];
2678
2679 if (mir->dalvikInsn.opCode == OP_INVOKE_DIRECT)
2680 genProcessArgsNoRange(cUnit, mir, dInsn, &pcrLabel);
2681 else
2682 genProcessArgsRange(cUnit, mir, dInsn, &pcrLabel);
2683
2684 /* r0 = calleeMethod */
2685 loadConstant(cUnit, r0, (int) calleeMethod);
2686
Ben Cheng38329f52009-07-07 14:19:20 -07002687 genInvokeSingletonCommon(cUnit, mir, bb, labelList, pcrLabel,
2688 calleeMethod);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002689 break;
2690 }
2691 /* calleeMethod = method->clazz->pDvmDex->pResMethods[BBBB] */
2692 case OP_INVOKE_STATIC:
2693 case OP_INVOKE_STATIC_RANGE: {
2694 const Method *calleeMethod =
2695 cUnit->method->clazz->pDvmDex->pResMethods[dInsn->vB];
2696
2697 if (mir->dalvikInsn.opCode == OP_INVOKE_STATIC)
2698 genProcessArgsNoRange(cUnit, mir, dInsn,
2699 NULL /* no null check */);
2700 else
2701 genProcessArgsRange(cUnit, mir, dInsn,
2702 NULL /* no null check */);
2703
2704 /* r0 = calleeMethod */
2705 loadConstant(cUnit, r0, (int) calleeMethod);
2706
Ben Cheng38329f52009-07-07 14:19:20 -07002707 genInvokeSingletonCommon(cUnit, mir, bb, labelList, pcrLabel,
2708 calleeMethod);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002709 break;
2710 }
Ben Cheng09e50c92010-05-02 10:45:32 -07002711 /*
Ben Chengba4fc8b2009-06-01 13:00:29 -07002712 * calleeMethod = dvmFindInterfaceMethodInCache(this->clazz,
2713 * BBBB, method, method->clazz->pDvmDex)
Ben Cheng38329f52009-07-07 14:19:20 -07002714 *
Ben Cheng09e50c92010-05-02 10:45:32 -07002715 * The following is an example of generated code for
2716 * "invoke-interface v0"
Ben Cheng38329f52009-07-07 14:19:20 -07002717 *
Ben Cheng09e50c92010-05-02 10:45:32 -07002718 * -------- dalvik offset: 0x0008 @ invoke-interface v0
2719 * 0x47357e36 : ldr r0, [r5, #0] --+
2720 * 0x47357e38 : sub r7,r5,#24 |
2721 * 0x47357e3c : cmp r0, #0 | genProcessArgsNoRange
2722 * 0x47357e3e : beq 0x47357e82 |
2723 * 0x47357e40 : stmia r7, <r0> --+
2724 * 0x47357e42 : ldr r4, [pc, #120] --> r4 <- dalvikPC of this invoke
2725 * 0x47357e44 : add r1, pc, #64 --> r1 <- &retChainingCell
2726 * 0x47357e46 : add r2, pc, #72 --> r2 <- &predictedChainingCell
2727 * 0x47357e48 : blx_1 0x47348190 --+ TEMPLATE_INVOKE_METHOD_
2728 * 0x47357e4a : blx_2 see above --+ PREDICTED_CHAIN
2729 * 0x47357e4c : b 0x47357e90 --> off to the predicted chain
2730 * 0x47357e4e : b 0x47357e82 --> punt to the interpreter
2731 * 0x47357e50 : mov r8, r1 --+
2732 * 0x47357e52 : mov r9, r2 |
2733 * 0x47357e54 : ldr r2, [pc, #96] |
2734 * 0x47357e56 : mov r10, r3 |
2735 * 0x47357e58 : movs r0, r3 | dvmFindInterfaceMethodInCache
2736 * 0x47357e5a : ldr r3, [pc, #88] |
2737 * 0x47357e5c : ldr r7, [pc, #80] |
2738 * 0x47357e5e : mov r1, #1452 |
2739 * 0x47357e62 : blx r7 --+
2740 * 0x47357e64 : cmp r0, #0 --> calleeMethod == NULL?
2741 * 0x47357e66 : bne 0x47357e6e --> branch over the throw if !r0
2742 * 0x47357e68 : ldr r0, [pc, #80] --> load Dalvik PC of the invoke
2743 * 0x47357e6a : blx_1 0x47348494 --+ TEMPLATE_THROW_EXCEPTION_
2744 * 0x47357e6c : blx_2 see above --+ COMMON
2745 * 0x47357e6e : mov r1, r8 --> r1 <- &retChainingCell
2746 * 0x47357e70 : cmp r1, #0 --> compare against 0
2747 * 0x47357e72 : bgt 0x47357e7c --> >=0? don't rechain
2748 * 0x47357e74 : ldr r7, [r6, #108] --+
2749 * 0x47357e76 : mov r2, r9 | dvmJitToPatchPredictedChain
2750 * 0x47357e78 : mov r3, r10 |
2751 * 0x47357e7a : blx r7 --+
2752 * 0x47357e7c : add r1, pc, #8 --> r1 <- &retChainingCell
2753 * 0x47357e7e : blx_1 0x4734809c --+ TEMPLATE_INVOKE_METHOD_NO_OPT
2754 * 0x47357e80 : blx_2 see above --+
2755 * -------- reconstruct dalvik PC : 0x425719dc @ +0x0008
2756 * 0x47357e82 : ldr r0, [pc, #56]
Ben Cheng38329f52009-07-07 14:19:20 -07002757 * Exception_Handling:
Ben Cheng09e50c92010-05-02 10:45:32 -07002758 * 0x47357e84 : ldr r1, [r6, #92]
2759 * 0x47357e86 : blx r1
2760 * 0x47357e88 : .align4
2761 * -------- chaining cell (hot): 0x000b
2762 * 0x47357e88 : ldr r0, [r6, #104]
2763 * 0x47357e8a : blx r0
2764 * 0x47357e8c : data 0x19e2(6626)
2765 * 0x47357e8e : data 0x4257(16983)
2766 * 0x47357e90 : .align4
Ben Cheng38329f52009-07-07 14:19:20 -07002767 * -------- chaining cell (predicted)
Ben Cheng09e50c92010-05-02 10:45:32 -07002768 * 0x47357e90 : data 0xe7fe(59390) --> will be patched into bx
2769 * 0x47357e92 : data 0x0000(0)
2770 * 0x47357e94 : data 0x0000(0) --> class
2771 * 0x47357e96 : data 0x0000(0)
2772 * 0x47357e98 : data 0x0000(0) --> method
2773 * 0x47357e9a : data 0x0000(0)
2774 * 0x47357e9c : data 0x0000(0) --> rechain count
2775 * 0x47357e9e : data 0x0000(0)
2776 * -------- end of chaining cells (0x006c)
2777 * 0x47357eb0 : .word (0xad03e369)
2778 * 0x47357eb4 : .word (0x28a90)
2779 * 0x47357eb8 : .word (0x41a63394)
2780 * 0x47357ebc : .word (0x425719dc)
Ben Chengba4fc8b2009-06-01 13:00:29 -07002781 */
2782 case OP_INVOKE_INTERFACE:
2783 case OP_INVOKE_INTERFACE_RANGE: {
Bill Buzbee89efc3d2009-07-28 11:22:22 -07002784 ArmLIR *predChainingCell = &labelList[bb->taken->id];
Ben Chengba4fc8b2009-06-01 13:00:29 -07002785
Bill Buzbee1465db52009-09-23 17:17:35 -07002786 /* Ensure that nothing is both live and dirty */
Bill Buzbeec6f10662010-02-09 11:16:15 -08002787 dvmCompilerFlushAllRegs(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07002788
Ben Chengba4fc8b2009-06-01 13:00:29 -07002789 if (mir->dalvikInsn.opCode == OP_INVOKE_INTERFACE)
2790 genProcessArgsNoRange(cUnit, mir, dInsn, &pcrLabel);
2791 else
2792 genProcessArgsRange(cUnit, mir, dInsn, &pcrLabel);
2793
Ben Cheng38329f52009-07-07 14:19:20 -07002794 /* "this" is already left in r0 by genProcessArgs* */
2795
2796 /* r4PC = dalvikCallsite */
2797 loadConstant(cUnit, r4PC,
2798 (int) (cUnit->method->insns + mir->offset));
2799
2800 /* r1 = &retChainingCell */
Bill Buzbee270c1d62009-08-13 16:58:07 -07002801 ArmLIR *addrRetChain =
Bill Buzbee1465db52009-09-23 17:17:35 -07002802 opRegRegImm(cUnit, kOpAdd, r1, rpc, 0);
Ben Cheng38329f52009-07-07 14:19:20 -07002803 addrRetChain->generic.target = (LIR *) retChainingCell;
2804
2805 /* r2 = &predictedChainingCell */
Bill Buzbee89efc3d2009-07-28 11:22:22 -07002806 ArmLIR *predictedChainingCell =
Bill Buzbee1465db52009-09-23 17:17:35 -07002807 opRegRegImm(cUnit, kOpAdd, r2, rpc, 0);
Ben Cheng38329f52009-07-07 14:19:20 -07002808 predictedChainingCell->generic.target = (LIR *) predChainingCell;
2809
2810 genDispatchToHandler(cUnit, TEMPLATE_INVOKE_METHOD_PREDICTED_CHAIN);
2811
2812 /* return through lr - jump to the chaining cell */
2813 genUnconditionalBranch(cUnit, predChainingCell);
2814
2815 /*
2816 * null-check on "this" may have been eliminated, but we still need
2817 * a PC-reconstruction label for stack overflow bailout.
2818 */
2819 if (pcrLabel == NULL) {
2820 int dPC = (int) (cUnit->method->insns + mir->offset);
Bill Buzbee89efc3d2009-07-28 11:22:22 -07002821 pcrLabel = dvmCompilerNew(sizeof(ArmLIR), true);
Ben Chenga4973592010-03-31 11:59:18 -07002822 pcrLabel->opCode = kArmPseudoPCReconstructionCell;
Ben Cheng38329f52009-07-07 14:19:20 -07002823 pcrLabel->operands[0] = dPC;
2824 pcrLabel->operands[1] = mir->offset;
2825 /* Insert the place holder to the growable list */
2826 dvmInsertGrowableList(&cUnit->pcReconstructionList, pcrLabel);
2827 }
2828
2829 /* return through lr+2 - punt to the interpreter */
2830 genUnconditionalBranch(cUnit, pcrLabel);
2831
2832 /*
2833 * return through lr+4 - fully resolve the callee method.
2834 * r1 <- count
2835 * r2 <- &predictedChainCell
2836 * r3 <- this->class
2837 * r4 <- dPC
2838 * r7 <- this->class->vtable
2839 */
2840
2841 /* Save count, &predictedChainCell, and class to high regs first */
Bill Buzbee1465db52009-09-23 17:17:35 -07002842 genRegCopy(cUnit, r8, r1);
2843 genRegCopy(cUnit, r9, r2);
2844 genRegCopy(cUnit, r10, r3);
Ben Cheng38329f52009-07-07 14:19:20 -07002845
Ben Chengba4fc8b2009-06-01 13:00:29 -07002846 /* r0 now contains this->clazz */
Bill Buzbee1465db52009-09-23 17:17:35 -07002847 genRegCopy(cUnit, r0, r3);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002848
2849 /* r1 = BBBB */
2850 loadConstant(cUnit, r1, dInsn->vB);
2851
2852 /* r2 = method (caller) */
2853 loadConstant(cUnit, r2, (int) cUnit->method);
2854
2855 /* r3 = pDvmDex */
2856 loadConstant(cUnit, r3, (int) cUnit->method->clazz->pDvmDex);
2857
Ben Chengbd1326d2010-04-02 15:04:53 -07002858 LOAD_FUNC_ADDR(cUnit, r7,
2859 (intptr_t) dvmFindInterfaceMethodInCache);
Bill Buzbee1465db52009-09-23 17:17:35 -07002860 opReg(cUnit, kOpBlx, r7);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002861 /* r0 = calleeMethod (returned from dvmFindInterfaceMethodInCache */
2862
Ben Cheng09e50c92010-05-02 10:45:32 -07002863 dvmCompilerClobberCallRegs(cUnit);
2864 /* generate a branch over if the interface method is resolved */
2865 opRegImm(cUnit, kOpCmp, r0, 0); /* NULL? */
2866 ArmLIR *branchOver = opCondBranch(cUnit, kArmCondNe);
2867 /*
2868 * calleeMethod == NULL -> throw
2869 */
2870 loadConstant(cUnit, r0,
2871 (int) (cUnit->method->insns + mir->offset));
2872 genDispatchToHandler(cUnit, TEMPLATE_THROW_EXCEPTION_COMMON);
2873 /* noreturn */
2874
2875 ArmLIR *target = newLIR0(cUnit, kArmPseudoTargetLabel);
2876 target->defMask = ENCODE_ALL;
2877 branchOver->generic.target = (LIR *) target;
2878
Bill Buzbee1465db52009-09-23 17:17:35 -07002879 genRegCopy(cUnit, r1, r8);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002880
Ben Cheng38329f52009-07-07 14:19:20 -07002881 /* Check if rechain limit is reached */
Bill Buzbee1465db52009-09-23 17:17:35 -07002882 opRegImm(cUnit, kOpCmp, r1, 0);
Ben Cheng38329f52009-07-07 14:19:20 -07002883
Bill Buzbee1465db52009-09-23 17:17:35 -07002884 ArmLIR *bypassRechaining = opCondBranch(cUnit, kArmCondGt);
Ben Cheng38329f52009-07-07 14:19:20 -07002885
Bill Buzbee270c1d62009-08-13 16:58:07 -07002886 loadWordDisp(cUnit, rGLUE, offsetof(InterpState,
2887 jitToInterpEntries.dvmJitToPatchPredictedChain), r7);
Ben Cheng38329f52009-07-07 14:19:20 -07002888
Ben Chengb88ec3c2010-05-17 12:50:33 -07002889 genRegCopy(cUnit, r1, rGLUE);
Bill Buzbee1465db52009-09-23 17:17:35 -07002890 genRegCopy(cUnit, r2, r9);
2891 genRegCopy(cUnit, r3, r10);
Ben Cheng38329f52009-07-07 14:19:20 -07002892
2893 /*
2894 * r0 = calleeMethod
2895 * r2 = &predictedChainingCell
2896 * r3 = class
2897 *
2898 * &returnChainingCell has been loaded into r1 but is not needed
2899 * when patching the chaining cell and will be clobbered upon
2900 * returning so it will be reconstructed again.
2901 */
Bill Buzbee1465db52009-09-23 17:17:35 -07002902 opReg(cUnit, kOpBlx, r7);
Ben Cheng38329f52009-07-07 14:19:20 -07002903
2904 /* r1 = &retChainingCell */
Bill Buzbee1465db52009-09-23 17:17:35 -07002905 addrRetChain = opRegRegImm(cUnit, kOpAdd, r1, rpc, 0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002906 addrRetChain->generic.target = (LIR *) retChainingCell;
Ben Cheng38329f52009-07-07 14:19:20 -07002907
2908 bypassRechaining->generic.target = (LIR *) addrRetChain;
2909
Ben Chengba4fc8b2009-06-01 13:00:29 -07002910 /*
2911 * r0 = this, r1 = calleeMethod,
2912 * r1 = &ChainingCell,
2913 * r4PC = callsiteDPC,
2914 */
2915 genDispatchToHandler(cUnit, TEMPLATE_INVOKE_METHOD_NO_OPT);
Ben Cheng978738d2010-05-13 13:45:57 -07002916#if defined(WITH_JIT_TUNING)
Ben Cheng86717f72010-03-05 15:27:21 -08002917 gDvmJit.invokePolymorphic++;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002918#endif
2919 /* Handle exceptions using the interpreter */
2920 genTrap(cUnit, mir->offset, pcrLabel);
2921 break;
2922 }
2923 /* NOP */
2924 case OP_INVOKE_DIRECT_EMPTY: {
2925 return false;
2926 }
2927 case OP_FILLED_NEW_ARRAY:
2928 case OP_FILLED_NEW_ARRAY_RANGE: {
2929 /* Just let the interpreter deal with these */
2930 genInterpSingleStep(cUnit, mir);
2931 break;
2932 }
2933 default:
2934 return true;
2935 }
2936 return false;
2937}
2938
2939static bool handleFmt35ms_3rms(CompilationUnit *cUnit, MIR *mir,
Bill Buzbee89efc3d2009-07-28 11:22:22 -07002940 BasicBlock *bb, ArmLIR *labelList)
Ben Chengba4fc8b2009-06-01 13:00:29 -07002941{
Bill Buzbee89efc3d2009-07-28 11:22:22 -07002942 ArmLIR *retChainingCell = &labelList[bb->fallThrough->id];
2943 ArmLIR *predChainingCell = &labelList[bb->taken->id];
2944 ArmLIR *pcrLabel = NULL;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002945
2946 DecodedInstruction *dInsn = &mir->dalvikInsn;
2947 switch (mir->dalvikInsn.opCode) {
2948 /* calleeMethod = this->clazz->vtable[BBBB] */
2949 case OP_INVOKE_VIRTUAL_QUICK_RANGE:
2950 case OP_INVOKE_VIRTUAL_QUICK: {
2951 int methodIndex = dInsn->vB;
2952 if (mir->dalvikInsn.opCode == OP_INVOKE_VIRTUAL_QUICK)
2953 genProcessArgsNoRange(cUnit, mir, dInsn, &pcrLabel);
2954 else
2955 genProcessArgsRange(cUnit, mir, dInsn, &pcrLabel);
2956
Ben Cheng38329f52009-07-07 14:19:20 -07002957 genInvokeVirtualCommon(cUnit, mir, methodIndex,
2958 retChainingCell,
2959 predChainingCell,
2960 pcrLabel);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002961 break;
2962 }
2963 /* calleeMethod = method->clazz->super->vtable[BBBB] */
2964 case OP_INVOKE_SUPER_QUICK:
2965 case OP_INVOKE_SUPER_QUICK_RANGE: {
2966 const Method *calleeMethod =
2967 cUnit->method->clazz->super->vtable[dInsn->vB];
2968
2969 if (mir->dalvikInsn.opCode == OP_INVOKE_SUPER_QUICK)
2970 genProcessArgsNoRange(cUnit, mir, dInsn, &pcrLabel);
2971 else
2972 genProcessArgsRange(cUnit, mir, dInsn, &pcrLabel);
2973
2974 /* r0 = calleeMethod */
2975 loadConstant(cUnit, r0, (int) calleeMethod);
2976
Ben Cheng38329f52009-07-07 14:19:20 -07002977 genInvokeSingletonCommon(cUnit, mir, bb, labelList, pcrLabel,
2978 calleeMethod);
2979 /* Handle exceptions using the interpreter */
2980 genTrap(cUnit, mir->offset, pcrLabel);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002981 break;
2982 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07002983 default:
2984 return true;
2985 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07002986 return false;
2987}
2988
2989/*
Bill Buzbeefd023aa2009-11-02 09:23:49 -08002990 * This operation is complex enough that we'll do it partly inline
2991 * and partly with a handler. NOTE: the handler uses hardcoded
2992 * values for string object offsets and must be revisitied if the
2993 * layout changes.
2994 */
2995static bool genInlinedCompareTo(CompilationUnit *cUnit, MIR *mir)
2996{
2997#if defined(USE_GLOBAL_STRING_DEFS)
2998 return false;
2999#else
3000 ArmLIR *rollback;
Bill Buzbeec6f10662010-02-09 11:16:15 -08003001 RegLocation rlThis = dvmCompilerGetSrc(cUnit, mir, 0);
3002 RegLocation rlComp = dvmCompilerGetSrc(cUnit, mir, 1);
Bill Buzbeefd023aa2009-11-02 09:23:49 -08003003
3004 loadValueDirectFixed(cUnit, rlThis, r0);
3005 loadValueDirectFixed(cUnit, rlComp, r1);
3006 /* Test objects for NULL */
3007 rollback = genNullCheck(cUnit, rlThis.sRegLow, r0, mir->offset, NULL);
3008 genNullCheck(cUnit, rlComp.sRegLow, r1, mir->offset, rollback);
3009 /*
3010 * TUNING: we could check for object pointer equality before invoking
3011 * handler. Unclear whether the gain would be worth the added code size
3012 * expansion.
3013 */
3014 genDispatchToHandler(cUnit, TEMPLATE_STRING_COMPARETO);
Bill Buzbeec6f10662010-02-09 11:16:15 -08003015 storeValue(cUnit, inlinedTarget(cUnit, mir, false),
3016 dvmCompilerGetReturn(cUnit));
Bill Buzbeefd023aa2009-11-02 09:23:49 -08003017 return true;
3018#endif
3019}
3020
Elliott Hughes2bdbcb62010-04-12 14:29:37 -07003021static bool genInlinedFastIndexOf(CompilationUnit *cUnit, MIR *mir)
Bill Buzbeefd023aa2009-11-02 09:23:49 -08003022{
3023#if defined(USE_GLOBAL_STRING_DEFS)
3024 return false;
3025#else
Bill Buzbeec6f10662010-02-09 11:16:15 -08003026 RegLocation rlThis = dvmCompilerGetSrc(cUnit, mir, 0);
3027 RegLocation rlChar = dvmCompilerGetSrc(cUnit, mir, 1);
Bill Buzbeefd023aa2009-11-02 09:23:49 -08003028
3029 loadValueDirectFixed(cUnit, rlThis, r0);
3030 loadValueDirectFixed(cUnit, rlChar, r1);
Elliott Hughes2bdbcb62010-04-12 14:29:37 -07003031 RegLocation rlStart = dvmCompilerGetSrc(cUnit, mir, 2);
3032 loadValueDirectFixed(cUnit, rlStart, r2);
Bill Buzbeefd023aa2009-11-02 09:23:49 -08003033 /* Test objects for NULL */
3034 genNullCheck(cUnit, rlThis.sRegLow, r0, mir->offset, NULL);
3035 genDispatchToHandler(cUnit, TEMPLATE_STRING_INDEXOF);
Bill Buzbeec6f10662010-02-09 11:16:15 -08003036 storeValue(cUnit, inlinedTarget(cUnit, mir, false),
3037 dvmCompilerGetReturn(cUnit));
Bill Buzbeefd023aa2009-11-02 09:23:49 -08003038 return true;
3039#endif
3040}
3041
Elliott Hughesee34f592010-04-05 18:13:52 -07003042// Generates an inlined String.isEmpty or String.length.
3043static bool genInlinedStringIsEmptyOrLength(CompilationUnit *cUnit, MIR *mir,
3044 bool isEmpty)
Bill Buzbee1f748632010-03-02 16:14:41 -08003045{
Elliott Hughesee34f592010-04-05 18:13:52 -07003046 // dst = src.length();
Bill Buzbee1f748632010-03-02 16:14:41 -08003047 RegLocation rlObj = dvmCompilerGetSrc(cUnit, mir, 0);
3048 RegLocation rlDest = inlinedTarget(cUnit, mir, false);
3049 rlObj = loadValue(cUnit, rlObj, kCoreReg);
3050 RegLocation rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
3051 genNullCheck(cUnit, rlObj.sRegLow, rlObj.lowReg, mir->offset, NULL);
3052 loadWordDisp(cUnit, rlObj.lowReg, gDvm.offJavaLangString_count,
3053 rlResult.lowReg);
Elliott Hughesee34f592010-04-05 18:13:52 -07003054 if (isEmpty) {
3055 // dst = (dst == 0);
3056 int tReg = dvmCompilerAllocTemp(cUnit);
3057 opRegReg(cUnit, kOpNeg, tReg, rlResult.lowReg);
3058 opRegRegReg(cUnit, kOpAdc, rlResult.lowReg, rlResult.lowReg, tReg);
3059 }
Bill Buzbee1f748632010-03-02 16:14:41 -08003060 storeValue(cUnit, rlDest, rlResult);
3061 return false;
3062}
3063
Elliott Hughesee34f592010-04-05 18:13:52 -07003064static bool genInlinedStringLength(CompilationUnit *cUnit, MIR *mir)
3065{
3066 return genInlinedStringIsEmptyOrLength(cUnit, mir, false);
3067}
3068
3069static bool genInlinedStringIsEmpty(CompilationUnit *cUnit, MIR *mir)
3070{
3071 return genInlinedStringIsEmptyOrLength(cUnit, mir, true);
3072}
3073
Bill Buzbee1f748632010-03-02 16:14:41 -08003074static bool genInlinedStringCharAt(CompilationUnit *cUnit, MIR *mir)
3075{
3076 int contents = offsetof(ArrayObject, contents);
3077 RegLocation rlObj = dvmCompilerGetSrc(cUnit, mir, 0);
3078 RegLocation rlIdx = dvmCompilerGetSrc(cUnit, mir, 1);
3079 RegLocation rlDest = inlinedTarget(cUnit, mir, false);
3080 RegLocation rlResult;
3081 rlObj = loadValue(cUnit, rlObj, kCoreReg);
3082 rlIdx = loadValue(cUnit, rlIdx, kCoreReg);
3083 int regMax = dvmCompilerAllocTemp(cUnit);
3084 int regOff = dvmCompilerAllocTemp(cUnit);
3085 int regPtr = dvmCompilerAllocTemp(cUnit);
3086 ArmLIR *pcrLabel = genNullCheck(cUnit, rlObj.sRegLow, rlObj.lowReg,
3087 mir->offset, NULL);
3088 loadWordDisp(cUnit, rlObj.lowReg, gDvm.offJavaLangString_count, regMax);
3089 loadWordDisp(cUnit, rlObj.lowReg, gDvm.offJavaLangString_offset, regOff);
3090 loadWordDisp(cUnit, rlObj.lowReg, gDvm.offJavaLangString_value, regPtr);
3091 genBoundsCheck(cUnit, rlIdx.lowReg, regMax, mir->offset, pcrLabel);
3092 dvmCompilerFreeTemp(cUnit, regMax);
3093 opRegImm(cUnit, kOpAdd, regPtr, contents);
3094 opRegReg(cUnit, kOpAdd, regOff, rlIdx.lowReg);
3095 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
3096 loadBaseIndexed(cUnit, regPtr, regOff, rlResult.lowReg, 1, kUnsignedHalf);
3097 storeValue(cUnit, rlDest, rlResult);
3098 return false;
3099}
3100
3101static bool genInlinedAbsInt(CompilationUnit *cUnit, MIR *mir)
3102{
3103 RegLocation rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
3104 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
3105 RegLocation rlDest = inlinedTarget(cUnit, mir, false);;
3106 RegLocation rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
3107 int signReg = dvmCompilerAllocTemp(cUnit);
3108 /*
3109 * abs(x) = y<=x>>31, (x+y)^y.
3110 * Thumb2's IT block also yields 3 instructions, but imposes
3111 * scheduling constraints.
3112 */
3113 opRegRegImm(cUnit, kOpAsr, signReg, rlSrc.lowReg, 31);
3114 opRegRegReg(cUnit, kOpAdd, rlResult.lowReg, rlSrc.lowReg, signReg);
3115 opRegReg(cUnit, kOpXor, rlResult.lowReg, signReg);
3116 storeValue(cUnit, rlDest, rlResult);
3117 return false;
3118}
3119
3120static bool genInlinedAbsLong(CompilationUnit *cUnit, MIR *mir)
3121{
3122 RegLocation rlSrc = dvmCompilerGetSrcWide(cUnit, mir, 0, 1);
3123 RegLocation rlDest = inlinedTargetWide(cUnit, mir, false);
3124 rlSrc = loadValueWide(cUnit, rlSrc, kCoreReg);
3125 RegLocation rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
3126 int signReg = dvmCompilerAllocTemp(cUnit);
3127 /*
3128 * abs(x) = y<=x>>31, (x+y)^y.
3129 * Thumb2 IT block allows slightly shorter sequence,
3130 * but introduces a scheduling barrier. Stick with this
3131 * mechanism for now.
3132 */
3133 opRegRegImm(cUnit, kOpAsr, signReg, rlSrc.highReg, 31);
3134 opRegRegReg(cUnit, kOpAdd, rlResult.lowReg, rlSrc.lowReg, signReg);
3135 opRegRegReg(cUnit, kOpAdc, rlResult.highReg, rlSrc.highReg, signReg);
3136 opRegReg(cUnit, kOpXor, rlResult.lowReg, signReg);
3137 opRegReg(cUnit, kOpXor, rlResult.highReg, signReg);
3138 storeValueWide(cUnit, rlDest, rlResult);
3139 return false;
3140}
Bill Buzbeefd023aa2009-11-02 09:23:49 -08003141
3142/*
Bill Buzbeece46c942009-11-20 15:41:34 -08003143 * NOTE: Handles both range and non-range versions (arguments
3144 * have already been normalized by this point).
Ben Chengba4fc8b2009-06-01 13:00:29 -07003145 */
Bill Buzbeece46c942009-11-20 15:41:34 -08003146static bool handleExecuteInline(CompilationUnit *cUnit, MIR *mir)
Ben Chengba4fc8b2009-06-01 13:00:29 -07003147{
3148 DecodedInstruction *dInsn = &mir->dalvikInsn;
3149 switch( mir->dalvikInsn.opCode) {
Bill Buzbeece46c942009-11-20 15:41:34 -08003150 case OP_EXECUTE_INLINE_RANGE:
Ben Chengba4fc8b2009-06-01 13:00:29 -07003151 case OP_EXECUTE_INLINE: {
3152 unsigned int i;
3153 const InlineOperation* inLineTable = dvmGetInlineOpsTable();
Bill Buzbee50a6bf22009-07-08 13:08:04 -07003154 int offset = offsetof(InterpState, retval);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003155 int operation = dInsn->vB;
Bill Buzbee50a6bf22009-07-08 13:08:04 -07003156 switch (operation) {
3157 case INLINE_EMPTYINLINEMETHOD:
3158 return false; /* Nop */
3159 case INLINE_STRING_LENGTH:
3160 return genInlinedStringLength(cUnit, mir);
Elliott Hughesee34f592010-04-05 18:13:52 -07003161 case INLINE_STRING_IS_EMPTY:
3162 return genInlinedStringIsEmpty(cUnit, mir);
Bill Buzbee50a6bf22009-07-08 13:08:04 -07003163 case INLINE_MATH_ABS_INT:
3164 return genInlinedAbsInt(cUnit, mir);
3165 case INLINE_MATH_ABS_LONG:
3166 return genInlinedAbsLong(cUnit, mir);
3167 case INLINE_MATH_MIN_INT:
3168 return genInlinedMinMaxInt(cUnit, mir, true);
3169 case INLINE_MATH_MAX_INT:
3170 return genInlinedMinMaxInt(cUnit, mir, false);
3171 case INLINE_STRING_CHARAT:
3172 return genInlinedStringCharAt(cUnit, mir);
3173 case INLINE_MATH_SQRT:
3174 if (genInlineSqrt(cUnit, mir))
Bill Buzbee9727c3d2009-08-01 11:32:36 -07003175 return false;
Bill Buzbee50a6bf22009-07-08 13:08:04 -07003176 else
3177 break; /* Handle with C routine */
Bill Buzbee50a6bf22009-07-08 13:08:04 -07003178 case INLINE_MATH_ABS_FLOAT:
Bill Buzbee1465db52009-09-23 17:17:35 -07003179 if (genInlinedAbsFloat(cUnit, mir))
3180 return false;
3181 else
3182 break;
Bill Buzbee50a6bf22009-07-08 13:08:04 -07003183 case INLINE_MATH_ABS_DOUBLE:
Bill Buzbee1465db52009-09-23 17:17:35 -07003184 if (genInlinedAbsDouble(cUnit, mir))
3185 return false;
3186 else
3187 break;
Bill Buzbee50a6bf22009-07-08 13:08:04 -07003188 case INLINE_STRING_COMPARETO:
Bill Buzbeefd023aa2009-11-02 09:23:49 -08003189 if (genInlinedCompareTo(cUnit, mir))
3190 return false;
3191 else
3192 break;
Elliott Hughes2bdbcb62010-04-12 14:29:37 -07003193 case INLINE_STRING_FASTINDEXOF_II:
3194 if (genInlinedFastIndexOf(cUnit, mir))
Bill Buzbeefd023aa2009-11-02 09:23:49 -08003195 return false;
3196 else
3197 break;
3198 case INLINE_STRING_EQUALS:
3199 case INLINE_MATH_COS:
3200 case INLINE_MATH_SIN:
3201 break; /* Handle with C routine */
Bill Buzbee50a6bf22009-07-08 13:08:04 -07003202 default:
Bill Buzbeefc519dc2010-03-06 23:30:57 -08003203 dvmCompilerAbort(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003204 }
Bill Buzbeec6f10662010-02-09 11:16:15 -08003205 dvmCompilerFlushAllRegs(cUnit); /* Everything to home location */
Elliott Hughes6a555132010-02-25 15:41:42 -08003206 dvmCompilerClobberCallRegs(cUnit);
Bill Buzbeec6f10662010-02-09 11:16:15 -08003207 dvmCompilerClobber(cUnit, r4PC);
3208 dvmCompilerClobber(cUnit, r7);
Bill Buzbee1465db52009-09-23 17:17:35 -07003209 opRegRegImm(cUnit, kOpAdd, r4PC, rGLUE, offset);
3210 opImm(cUnit, kOpPush, (1<<r4PC) | (1<<r7));
Ben Chengbd1326d2010-04-02 15:04:53 -07003211 LOAD_FUNC_ADDR(cUnit, r4PC, (int)inLineTable[operation].func);
Bill Buzbee1465db52009-09-23 17:17:35 -07003212 genExportPC(cUnit, mir);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003213 for (i=0; i < dInsn->vA; i++) {
Bill Buzbeec6f10662010-02-09 11:16:15 -08003214 loadValueDirect(cUnit, dvmCompilerGetSrc(cUnit, mir, i), i);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003215 }
Bill Buzbee1465db52009-09-23 17:17:35 -07003216 opReg(cUnit, kOpBlx, r4PC);
3217 opRegImm(cUnit, kOpAdd, r13, 8);
Bill Buzbeece46c942009-11-20 15:41:34 -08003218 opRegImm(cUnit, kOpCmp, r0, 0); /* NULL? */
3219 ArmLIR *branchOver = opCondBranch(cUnit, kArmCondNe);
3220 loadConstant(cUnit, r0,
3221 (int) (cUnit->method->insns + mir->offset));
3222 genDispatchToHandler(cUnit, TEMPLATE_THROW_EXCEPTION_COMMON);
3223 ArmLIR *target = newLIR0(cUnit, kArmPseudoTargetLabel);
3224 target->defMask = ENCODE_ALL;
3225 branchOver->generic.target = (LIR *) target;
Ben Chengba4fc8b2009-06-01 13:00:29 -07003226 break;
3227 }
3228 default:
3229 return true;
3230 }
3231 return false;
3232}
3233
3234static bool handleFmt51l(CompilationUnit *cUnit, MIR *mir)
3235{
Bill Buzbee1465db52009-09-23 17:17:35 -07003236 //TUNING: We're using core regs here - not optimal when target is a double
Bill Buzbeec6f10662010-02-09 11:16:15 -08003237 RegLocation rlDest = dvmCompilerGetDestWide(cUnit, mir, 0, 1);
3238 RegLocation rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Ben Chengbd1326d2010-04-02 15:04:53 -07003239 loadConstantNoClobber(cUnit, rlResult.lowReg,
3240 mir->dalvikInsn.vB_wide & 0xFFFFFFFFUL);
3241 loadConstantNoClobber(cUnit, rlResult.highReg,
3242 (mir->dalvikInsn.vB_wide>>32) & 0xFFFFFFFFUL);
Bill Buzbee1465db52009-09-23 17:17:35 -07003243 storeValueWide(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003244 return false;
3245}
3246
Ben Chengba4fc8b2009-06-01 13:00:29 -07003247/*
3248 * The following are special processing routines that handle transfer of
3249 * controls between compiled code and the interpreter. Certain VM states like
3250 * Dalvik PC and special-purpose registers are reconstructed here.
3251 */
3252
Bill Buzbeebd047242010-05-13 13:02:53 -07003253/*
3254 * Insert a
3255 * b .+4
3256 * nop
3257 * pair at the beginning of a chaining cell. This serves as the
3258 * switch branch that selects between reverting to the interpreter or
3259 * not. Once the cell is chained to a translation, the cell will
3260 * contain a 32-bit branch. Subsequent chain/unchain operations will
3261 * then only alter that first 16-bits - the "b .+4" for unchaining,
3262 * and the restoration of the first half of the 32-bit branch for
3263 * rechaining.
3264 */
3265static void insertChainingSwitch(CompilationUnit *cUnit)
3266{
3267 ArmLIR *branch = newLIR0(cUnit, kThumbBUncond);
3268 newLIR2(cUnit, kThumbOrr, r0, r0);
3269 ArmLIR *target = newLIR0(cUnit, kArmPseudoTargetLabel);
3270 target->defMask = ENCODE_ALL;
3271 branch->generic.target = (LIR *) target;
3272}
3273
Ben Cheng1efc9c52009-06-08 18:25:27 -07003274/* Chaining cell for code that may need warmup. */
3275static void handleNormalChainingCell(CompilationUnit *cUnit,
3276 unsigned int offset)
Ben Chengba4fc8b2009-06-01 13:00:29 -07003277{
Ben Cheng11d8f142010-03-24 15:24:19 -07003278 /*
3279 * Use raw instruction constructors to guarantee that the generated
3280 * instructions fit the predefined cell size.
3281 */
Bill Buzbeebd047242010-05-13 13:02:53 -07003282 insertChainingSwitch(cUnit);
Ben Cheng11d8f142010-03-24 15:24:19 -07003283 newLIR3(cUnit, kThumbLdrRRI5, r0, rGLUE,
3284 offsetof(InterpState,
3285 jitToInterpEntries.dvmJitToInterpNormal) >> 2);
3286 newLIR1(cUnit, kThumbBlxR, r0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003287 addWordData(cUnit, (int) (cUnit->method->insns + offset), true);
3288}
3289
3290/*
Ben Cheng1efc9c52009-06-08 18:25:27 -07003291 * Chaining cell for instructions that immediately following already translated
3292 * code.
Ben Chengba4fc8b2009-06-01 13:00:29 -07003293 */
Ben Cheng1efc9c52009-06-08 18:25:27 -07003294static void handleHotChainingCell(CompilationUnit *cUnit,
3295 unsigned int offset)
Ben Chengba4fc8b2009-06-01 13:00:29 -07003296{
Ben Cheng11d8f142010-03-24 15:24:19 -07003297 /*
3298 * Use raw instruction constructors to guarantee that the generated
3299 * instructions fit the predefined cell size.
3300 */
Bill Buzbeebd047242010-05-13 13:02:53 -07003301 insertChainingSwitch(cUnit);
Ben Cheng11d8f142010-03-24 15:24:19 -07003302 newLIR3(cUnit, kThumbLdrRRI5, r0, rGLUE,
3303 offsetof(InterpState,
3304 jitToInterpEntries.dvmJitToInterpTraceSelect) >> 2);
3305 newLIR1(cUnit, kThumbBlxR, r0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003306 addWordData(cUnit, (int) (cUnit->method->insns + offset), true);
3307}
3308
Bill Buzbee9c4b7c82009-09-10 10:10:38 -07003309#if defined(WITH_SELF_VERIFICATION) || defined(WITH_JIT_TUNING)
Jeff Hao97319a82009-08-12 16:57:15 -07003310/* Chaining cell for branches that branch back into the same basic block */
3311static void handleBackwardBranchChainingCell(CompilationUnit *cUnit,
3312 unsigned int offset)
3313{
Ben Cheng11d8f142010-03-24 15:24:19 -07003314 /*
3315 * Use raw instruction constructors to guarantee that the generated
3316 * instructions fit the predefined cell size.
3317 */
Bill Buzbeebd047242010-05-13 13:02:53 -07003318 insertChainingSwitch(cUnit);
Bill Buzbee9c4b7c82009-09-10 10:10:38 -07003319#if defined(WITH_SELF_VERIFICATION)
Bill Buzbee1465db52009-09-23 17:17:35 -07003320 newLIR3(cUnit, kThumbLdrRRI5, r0, rGLUE,
Ben Cheng40094c12010-02-24 20:58:44 -08003321 offsetof(InterpState,
3322 jitToInterpEntries.dvmJitToInterpBackwardBranch) >> 2);
Bill Buzbee9c4b7c82009-09-10 10:10:38 -07003323#else
Bill Buzbee1465db52009-09-23 17:17:35 -07003324 newLIR3(cUnit, kThumbLdrRRI5, r0, rGLUE,
Bill Buzbee9c4b7c82009-09-10 10:10:38 -07003325 offsetof(InterpState, jitToInterpEntries.dvmJitToInterpNormal) >> 2);
3326#endif
Bill Buzbee1465db52009-09-23 17:17:35 -07003327 newLIR1(cUnit, kThumbBlxR, r0);
Jeff Hao97319a82009-08-12 16:57:15 -07003328 addWordData(cUnit, (int) (cUnit->method->insns + offset), true);
3329}
3330
3331#endif
Ben Chengba4fc8b2009-06-01 13:00:29 -07003332/* Chaining cell for monomorphic method invocations. */
Ben Cheng38329f52009-07-07 14:19:20 -07003333static void handleInvokeSingletonChainingCell(CompilationUnit *cUnit,
3334 const Method *callee)
Ben Chengba4fc8b2009-06-01 13:00:29 -07003335{
Ben Cheng11d8f142010-03-24 15:24:19 -07003336 /*
3337 * Use raw instruction constructors to guarantee that the generated
3338 * instructions fit the predefined cell size.
3339 */
Bill Buzbeebd047242010-05-13 13:02:53 -07003340 insertChainingSwitch(cUnit);
Ben Cheng11d8f142010-03-24 15:24:19 -07003341 newLIR3(cUnit, kThumbLdrRRI5, r0, rGLUE,
3342 offsetof(InterpState,
3343 jitToInterpEntries.dvmJitToInterpTraceSelect) >> 2);
3344 newLIR1(cUnit, kThumbBlxR, r0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003345 addWordData(cUnit, (int) (callee->insns), true);
3346}
3347
Ben Cheng38329f52009-07-07 14:19:20 -07003348/* Chaining cell for monomorphic method invocations. */
3349static void handleInvokePredictedChainingCell(CompilationUnit *cUnit)
3350{
3351
3352 /* Should not be executed in the initial state */
3353 addWordData(cUnit, PREDICTED_CHAIN_BX_PAIR_INIT, true);
3354 /* To be filled: class */
3355 addWordData(cUnit, PREDICTED_CHAIN_CLAZZ_INIT, true);
3356 /* To be filled: method */
3357 addWordData(cUnit, PREDICTED_CHAIN_METHOD_INIT, true);
3358 /*
3359 * Rechain count. The initial value of 0 here will trigger chaining upon
3360 * the first invocation of this callsite.
3361 */
3362 addWordData(cUnit, PREDICTED_CHAIN_COUNTER_INIT, true);
3363}
3364
Ben Chengba4fc8b2009-06-01 13:00:29 -07003365/* Load the Dalvik PC into r0 and jump to the specified target */
3366static void handlePCReconstruction(CompilationUnit *cUnit,
Bill Buzbee89efc3d2009-07-28 11:22:22 -07003367 ArmLIR *targetLabel)
Ben Chengba4fc8b2009-06-01 13:00:29 -07003368{
Bill Buzbee89efc3d2009-07-28 11:22:22 -07003369 ArmLIR **pcrLabel =
3370 (ArmLIR **) cUnit->pcReconstructionList.elemList;
Ben Chengba4fc8b2009-06-01 13:00:29 -07003371 int numElems = cUnit->pcReconstructionList.numUsed;
3372 int i;
3373 for (i = 0; i < numElems; i++) {
3374 dvmCompilerAppendLIR(cUnit, (LIR *) pcrLabel[i]);
3375 /* r0 = dalvik PC */
3376 loadConstant(cUnit, r0, pcrLabel[i]->operands[0]);
3377 genUnconditionalBranch(cUnit, targetLabel);
3378 }
3379}
3380
Bill Buzbee1465db52009-09-23 17:17:35 -07003381static char *extendedMIROpNames[kMirOpLast - kMirOpFirst] = {
3382 "kMirOpPhi",
3383 "kMirOpNullNRangeUpCheck",
3384 "kMirOpNullNRangeDownCheck",
3385 "kMirOpLowerBound",
3386 "kMirOpPunt",
Ben Cheng4238ec22009-08-24 16:32:22 -07003387};
3388
3389/*
3390 * vA = arrayReg;
3391 * vB = idxReg;
3392 * vC = endConditionReg;
3393 * arg[0] = maxC
3394 * arg[1] = minC
3395 * arg[2] = loopBranchConditionCode
3396 */
3397static void genHoistedChecksForCountUpLoop(CompilationUnit *cUnit, MIR *mir)
3398{
Bill Buzbee1465db52009-09-23 17:17:35 -07003399 /*
3400 * NOTE: these synthesized blocks don't have ssa names assigned
3401 * for Dalvik registers. However, because they dominate the following
3402 * blocks we can simply use the Dalvik name w/ subscript 0 as the
3403 * ssa name.
3404 */
Ben Cheng4238ec22009-08-24 16:32:22 -07003405 DecodedInstruction *dInsn = &mir->dalvikInsn;
3406 const int lenOffset = offsetof(ArrayObject, length);
Ben Cheng4238ec22009-08-24 16:32:22 -07003407 const int maxC = dInsn->arg[0];
Bill Buzbee1465db52009-09-23 17:17:35 -07003408 int regLength;
3409 RegLocation rlArray = cUnit->regLocation[mir->dalvikInsn.vA];
3410 RegLocation rlIdxEnd = cUnit->regLocation[mir->dalvikInsn.vC];
Ben Cheng4238ec22009-08-24 16:32:22 -07003411
3412 /* regArray <- arrayRef */
Bill Buzbee1465db52009-09-23 17:17:35 -07003413 rlArray = loadValue(cUnit, rlArray, kCoreReg);
3414 rlIdxEnd = loadValue(cUnit, rlIdxEnd, kCoreReg);
3415 genRegImmCheck(cUnit, kArmCondEq, rlArray.lowReg, 0, 0,
Ben Cheng4238ec22009-08-24 16:32:22 -07003416 (ArmLIR *) cUnit->loopAnalysis->branchToPCR);
3417
3418 /* regLength <- len(arrayRef) */
Bill Buzbeec6f10662010-02-09 11:16:15 -08003419 regLength = dvmCompilerAllocTemp(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07003420 loadWordDisp(cUnit, rlArray.lowReg, lenOffset, regLength);
Ben Cheng4238ec22009-08-24 16:32:22 -07003421
3422 int delta = maxC;
3423 /*
3424 * If the loop end condition is ">=" instead of ">", then the largest value
3425 * of the index is "endCondition - 1".
3426 */
3427 if (dInsn->arg[2] == OP_IF_GE) {
3428 delta--;
3429 }
3430
3431 if (delta) {
Bill Buzbeec6f10662010-02-09 11:16:15 -08003432 int tReg = dvmCompilerAllocTemp(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07003433 opRegRegImm(cUnit, kOpAdd, tReg, rlIdxEnd.lowReg, delta);
3434 rlIdxEnd.lowReg = tReg;
Bill Buzbeec6f10662010-02-09 11:16:15 -08003435 dvmCompilerFreeTemp(cUnit, tReg);
Ben Cheng4238ec22009-08-24 16:32:22 -07003436 }
3437 /* Punt if "regIdxEnd < len(Array)" is false */
Bill Buzbee1465db52009-09-23 17:17:35 -07003438 genRegRegCheck(cUnit, kArmCondGe, rlIdxEnd.lowReg, regLength, 0,
Ben Cheng0fd31e42009-09-03 14:40:16 -07003439 (ArmLIR *) cUnit->loopAnalysis->branchToPCR);
Ben Cheng4238ec22009-08-24 16:32:22 -07003440}
3441
3442/*
3443 * vA = arrayReg;
3444 * vB = idxReg;
3445 * vC = endConditionReg;
3446 * arg[0] = maxC
3447 * arg[1] = minC
3448 * arg[2] = loopBranchConditionCode
3449 */
3450static void genHoistedChecksForCountDownLoop(CompilationUnit *cUnit, MIR *mir)
3451{
3452 DecodedInstruction *dInsn = &mir->dalvikInsn;
3453 const int lenOffset = offsetof(ArrayObject, length);
Bill Buzbeec6f10662010-02-09 11:16:15 -08003454 const int regLength = dvmCompilerAllocTemp(cUnit);
Ben Cheng4238ec22009-08-24 16:32:22 -07003455 const int maxC = dInsn->arg[0];
Bill Buzbee1465db52009-09-23 17:17:35 -07003456 RegLocation rlArray = cUnit->regLocation[mir->dalvikInsn.vA];
3457 RegLocation rlIdxInit = cUnit->regLocation[mir->dalvikInsn.vB];
Ben Cheng4238ec22009-08-24 16:32:22 -07003458
3459 /* regArray <- arrayRef */
Bill Buzbee1465db52009-09-23 17:17:35 -07003460 rlArray = loadValue(cUnit, rlArray, kCoreReg);
3461 rlIdxInit = loadValue(cUnit, rlIdxInit, kCoreReg);
3462 genRegImmCheck(cUnit, kArmCondEq, rlArray.lowReg, 0, 0,
Ben Cheng4238ec22009-08-24 16:32:22 -07003463 (ArmLIR *) cUnit->loopAnalysis->branchToPCR);
3464
3465 /* regLength <- len(arrayRef) */
Bill Buzbee1465db52009-09-23 17:17:35 -07003466 loadWordDisp(cUnit, rlArray.lowReg, lenOffset, regLength);
Ben Cheng4238ec22009-08-24 16:32:22 -07003467
3468 if (maxC) {
Bill Buzbeec6f10662010-02-09 11:16:15 -08003469 int tReg = dvmCompilerAllocTemp(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07003470 opRegRegImm(cUnit, kOpAdd, tReg, rlIdxInit.lowReg, maxC);
3471 rlIdxInit.lowReg = tReg;
Bill Buzbeec6f10662010-02-09 11:16:15 -08003472 dvmCompilerFreeTemp(cUnit, tReg);
Ben Cheng4238ec22009-08-24 16:32:22 -07003473 }
3474
3475 /* Punt if "regIdxInit < len(Array)" is false */
Bill Buzbee1465db52009-09-23 17:17:35 -07003476 genRegRegCheck(cUnit, kArmCondGe, rlIdxInit.lowReg, regLength, 0,
Ben Cheng0fd31e42009-09-03 14:40:16 -07003477 (ArmLIR *) cUnit->loopAnalysis->branchToPCR);
Ben Cheng4238ec22009-08-24 16:32:22 -07003478}
3479
3480/*
3481 * vA = idxReg;
3482 * vB = minC;
3483 */
3484static void genHoistedLowerBoundCheck(CompilationUnit *cUnit, MIR *mir)
3485{
3486 DecodedInstruction *dInsn = &mir->dalvikInsn;
Ben Cheng4238ec22009-08-24 16:32:22 -07003487 const int minC = dInsn->vB;
Bill Buzbee1465db52009-09-23 17:17:35 -07003488 RegLocation rlIdx = cUnit->regLocation[mir->dalvikInsn.vA];
Ben Cheng4238ec22009-08-24 16:32:22 -07003489
3490 /* regIdx <- initial index value */
Bill Buzbee1465db52009-09-23 17:17:35 -07003491 rlIdx = loadValue(cUnit, rlIdx, kCoreReg);
Ben Cheng4238ec22009-08-24 16:32:22 -07003492
3493 /* Punt if "regIdxInit + minC >= 0" is false */
Bill Buzbee1465db52009-09-23 17:17:35 -07003494 genRegImmCheck(cUnit, kArmCondLt, rlIdx.lowReg, -minC, 0,
Ben Cheng4238ec22009-08-24 16:32:22 -07003495 (ArmLIR *) cUnit->loopAnalysis->branchToPCR);
3496}
3497
3498/* Extended MIR instructions like PHI */
3499static void handleExtendedMIR(CompilationUnit *cUnit, MIR *mir)
3500{
Bill Buzbee1465db52009-09-23 17:17:35 -07003501 int opOffset = mir->dalvikInsn.opCode - kMirOpFirst;
Ben Cheng4238ec22009-08-24 16:32:22 -07003502 char *msg = dvmCompilerNew(strlen(extendedMIROpNames[opOffset]) + 1,
3503 false);
3504 strcpy(msg, extendedMIROpNames[opOffset]);
Bill Buzbee1465db52009-09-23 17:17:35 -07003505 newLIR1(cUnit, kArmPseudoExtended, (int) msg);
Ben Cheng4238ec22009-08-24 16:32:22 -07003506
3507 switch (mir->dalvikInsn.opCode) {
Bill Buzbee1465db52009-09-23 17:17:35 -07003508 case kMirOpPhi: {
Ben Cheng4238ec22009-08-24 16:32:22 -07003509 char *ssaString = dvmCompilerGetSSAString(cUnit, mir->ssaRep);
Bill Buzbee1465db52009-09-23 17:17:35 -07003510 newLIR1(cUnit, kArmPseudoSSARep, (int) ssaString);
Ben Cheng4238ec22009-08-24 16:32:22 -07003511 break;
3512 }
Bill Buzbee1465db52009-09-23 17:17:35 -07003513 case kMirOpNullNRangeUpCheck: {
Ben Cheng4238ec22009-08-24 16:32:22 -07003514 genHoistedChecksForCountUpLoop(cUnit, mir);
3515 break;
3516 }
Bill Buzbee1465db52009-09-23 17:17:35 -07003517 case kMirOpNullNRangeDownCheck: {
Ben Cheng4238ec22009-08-24 16:32:22 -07003518 genHoistedChecksForCountDownLoop(cUnit, mir);
3519 break;
3520 }
Bill Buzbee1465db52009-09-23 17:17:35 -07003521 case kMirOpLowerBound: {
Ben Cheng4238ec22009-08-24 16:32:22 -07003522 genHoistedLowerBoundCheck(cUnit, mir);
3523 break;
3524 }
Bill Buzbee1465db52009-09-23 17:17:35 -07003525 case kMirOpPunt: {
Ben Cheng4238ec22009-08-24 16:32:22 -07003526 genUnconditionalBranch(cUnit,
3527 (ArmLIR *) cUnit->loopAnalysis->branchToPCR);
3528 break;
3529 }
3530 default:
3531 break;
3532 }
3533}
3534
3535/*
3536 * Create a PC-reconstruction cell for the starting offset of this trace.
3537 * Since the PCR cell is placed near the end of the compiled code which is
3538 * usually out of range for a conditional branch, we put two branches (one
3539 * branch over to the loop body and one layover branch to the actual PCR) at the
3540 * end of the entry block.
3541 */
3542static void setupLoopEntryBlock(CompilationUnit *cUnit, BasicBlock *entry,
3543 ArmLIR *bodyLabel)
3544{
3545 /* Set up the place holder to reconstruct this Dalvik PC */
3546 ArmLIR *pcrLabel = dvmCompilerNew(sizeof(ArmLIR), true);
Ben Chenga4973592010-03-31 11:59:18 -07003547 pcrLabel->opCode = kArmPseudoPCReconstructionCell;
Ben Cheng4238ec22009-08-24 16:32:22 -07003548 pcrLabel->operands[0] =
3549 (int) (cUnit->method->insns + entry->startOffset);
3550 pcrLabel->operands[1] = entry->startOffset;
3551 /* Insert the place holder to the growable list */
3552 dvmInsertGrowableList(&cUnit->pcReconstructionList, pcrLabel);
3553
3554 /*
3555 * Next, create two branches - one branch over to the loop body and the
3556 * other branch to the PCR cell to punt.
3557 */
3558 ArmLIR *branchToBody = dvmCompilerNew(sizeof(ArmLIR), true);
Bill Buzbee1465db52009-09-23 17:17:35 -07003559 branchToBody->opCode = kThumbBUncond;
Ben Cheng4238ec22009-08-24 16:32:22 -07003560 branchToBody->generic.target = (LIR *) bodyLabel;
Ben Chengdcf3e5d2009-09-11 13:42:05 -07003561 setupResourceMasks(branchToBody);
Ben Cheng4238ec22009-08-24 16:32:22 -07003562 cUnit->loopAnalysis->branchToBody = (LIR *) branchToBody;
3563
3564 ArmLIR *branchToPCR = dvmCompilerNew(sizeof(ArmLIR), true);
Bill Buzbee1465db52009-09-23 17:17:35 -07003565 branchToPCR->opCode = kThumbBUncond;
Ben Cheng4238ec22009-08-24 16:32:22 -07003566 branchToPCR->generic.target = (LIR *) pcrLabel;
Ben Chengdcf3e5d2009-09-11 13:42:05 -07003567 setupResourceMasks(branchToPCR);
Ben Cheng4238ec22009-08-24 16:32:22 -07003568 cUnit->loopAnalysis->branchToPCR = (LIR *) branchToPCR;
3569}
3570
Ben Chengd5adae12010-03-26 17:45:28 -07003571#if defined(WITH_SELF_VERIFICATION)
3572static bool selfVerificationPuntOps(MIR *mir)
3573{
3574 DecodedInstruction *decInsn = &mir->dalvikInsn;
3575 OpCode op = decInsn->opCode;
3576 int flags = dexGetInstrFlags(gDvm.instrFlags, op);
3577 /*
3578 * All opcodes that can throw exceptions and use the
3579 * TEMPLATE_THROW_EXCEPTION_COMMON template should be excluded in the trace
3580 * under self-verification mode.
3581 */
3582 return (op == OP_MONITOR_ENTER || op == OP_MONITOR_EXIT ||
3583 op == OP_NEW_INSTANCE || op == OP_NEW_ARRAY ||
3584 op == OP_CHECK_CAST || op == OP_MOVE_EXCEPTION ||
3585 op == OP_FILL_ARRAY_DATA || op == OP_EXECUTE_INLINE ||
3586 op == OP_EXECUTE_INLINE_RANGE ||
3587 (flags & kInstrInvoke));
3588}
3589#endif
3590
Ben Chengba4fc8b2009-06-01 13:00:29 -07003591void dvmCompilerMIR2LIR(CompilationUnit *cUnit)
3592{
3593 /* Used to hold the labels of each block */
Bill Buzbee89efc3d2009-07-28 11:22:22 -07003594 ArmLIR *labelList =
3595 dvmCompilerNew(sizeof(ArmLIR) * cUnit->numBlocks, true);
Ben Chengcec26f62010-01-15 15:29:33 -08003596 GrowableList chainingListByType[kChainingCellGap];
Ben Chengba4fc8b2009-06-01 13:00:29 -07003597 int i;
3598
3599 /*
Ben Cheng38329f52009-07-07 14:19:20 -07003600 * Initialize various types chaining lists.
Ben Chengba4fc8b2009-06-01 13:00:29 -07003601 */
Ben Chengcec26f62010-01-15 15:29:33 -08003602 for (i = 0; i < kChainingCellGap; i++) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07003603 dvmInitGrowableList(&chainingListByType[i], 2);
3604 }
3605
3606 BasicBlock **blockList = cUnit->blockList;
3607
Bill Buzbee6e963e12009-06-17 16:56:19 -07003608 if (cUnit->executionCount) {
3609 /*
3610 * Reserve 6 bytes at the beginning of the trace
3611 * +----------------------------+
3612 * | execution count (4 bytes) |
3613 * +----------------------------+
3614 * | chain cell offset (2 bytes)|
3615 * +----------------------------+
3616 * ...and then code to increment the execution
3617 * count:
3618 * mov r0, pc @ move adr of "mov r0,pc" + 4 to r0
3619 * sub r0, #10 @ back up to addr of executionCount
3620 * ldr r1, [r0]
3621 * add r1, #1
3622 * str r1, [r0]
3623 */
Bill Buzbee1465db52009-09-23 17:17:35 -07003624 newLIR1(cUnit, kArm16BitData, 0);
3625 newLIR1(cUnit, kArm16BitData, 0);
Ben Chengcc6600c2009-06-22 14:45:16 -07003626 cUnit->chainCellOffsetLIR =
Bill Buzbee1465db52009-09-23 17:17:35 -07003627 (LIR *) newLIR1(cUnit, kArm16BitData, CHAIN_CELL_OFFSET_TAG);
Bill Buzbee6e963e12009-06-17 16:56:19 -07003628 cUnit->headerSize = 6;
Bill Buzbee270c1d62009-08-13 16:58:07 -07003629 /* Thumb instruction used directly here to ensure correct size */
Bill Buzbee1465db52009-09-23 17:17:35 -07003630 newLIR2(cUnit, kThumbMovRR_H2L, r0, rpc);
3631 newLIR2(cUnit, kThumbSubRI8, r0, 10);
3632 newLIR3(cUnit, kThumbLdrRRI5, r1, r0, 0);
3633 newLIR2(cUnit, kThumbAddRI8, r1, 1);
3634 newLIR3(cUnit, kThumbStrRRI5, r1, r0, 0);
Bill Buzbee6e963e12009-06-17 16:56:19 -07003635 } else {
3636 /* Just reserve 2 bytes for the chain cell offset */
Ben Chengcc6600c2009-06-22 14:45:16 -07003637 cUnit->chainCellOffsetLIR =
Bill Buzbee1465db52009-09-23 17:17:35 -07003638 (LIR *) newLIR1(cUnit, kArm16BitData, CHAIN_CELL_OFFSET_TAG);
Bill Buzbee6e963e12009-06-17 16:56:19 -07003639 cUnit->headerSize = 2;
3640 }
Ben Cheng1efc9c52009-06-08 18:25:27 -07003641
Ben Chengba4fc8b2009-06-01 13:00:29 -07003642 /* Handle the content in each basic block */
3643 for (i = 0; i < cUnit->numBlocks; i++) {
3644 blockList[i]->visited = true;
3645 MIR *mir;
3646
3647 labelList[i].operands[0] = blockList[i]->startOffset;
3648
Ben Chengcec26f62010-01-15 15:29:33 -08003649 if (blockList[i]->blockType >= kChainingCellGap) {
Ben Chengd44faf52010-06-02 15:33:51 -07003650 if (blockList[i]->firstMIRInsn != NULL &&
3651 ((blockList[i]->firstMIRInsn->dalvikInsn.opCode ==
3652 OP_MOVE_RESULT) ||
3653 (blockList[i]->firstMIRInsn->dalvikInsn.opCode ==
3654 OP_MOVE_RESULT_WIDE) ||
3655 (blockList[i]->firstMIRInsn->dalvikInsn.opCode ==
3656 OP_MOVE_RESULT_OBJECT))) {
3657 /* Align this block first since it is a return chaining cell */
3658 newLIR0(cUnit, kArmPseudoPseudoAlign4);
3659 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07003660 /*
3661 * Append the label pseudo LIR first. Chaining cells will be handled
3662 * separately afterwards.
3663 */
3664 dvmCompilerAppendLIR(cUnit, (LIR *) &labelList[i]);
3665 }
3666
Bill Buzbee1465db52009-09-23 17:17:35 -07003667 if (blockList[i]->blockType == kEntryBlock) {
Ben Chenga4973592010-03-31 11:59:18 -07003668 labelList[i].opCode = kArmPseudoEntryBlock;
Ben Cheng4238ec22009-08-24 16:32:22 -07003669 if (blockList[i]->firstMIRInsn == NULL) {
3670 continue;
3671 } else {
3672 setupLoopEntryBlock(cUnit, blockList[i],
3673 &labelList[blockList[i]->fallThrough->id]);
3674 }
Bill Buzbee1465db52009-09-23 17:17:35 -07003675 } else if (blockList[i]->blockType == kExitBlock) {
Ben Chenga4973592010-03-31 11:59:18 -07003676 labelList[i].opCode = kArmPseudoExitBlock;
Ben Cheng4238ec22009-08-24 16:32:22 -07003677 goto gen_fallthrough;
Bill Buzbee1465db52009-09-23 17:17:35 -07003678 } else if (blockList[i]->blockType == kDalvikByteCode) {
3679 labelList[i].opCode = kArmPseudoNormalBlockLabel;
Ben Chenge9695e52009-06-16 16:11:47 -07003680 /* Reset the register state */
Bill Buzbeec6f10662010-02-09 11:16:15 -08003681 dvmCompilerResetRegPool(cUnit);
3682 dvmCompilerClobberAllRegs(cUnit);
3683 dvmCompilerResetNullCheck(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003684 } else {
3685 switch (blockList[i]->blockType) {
Bill Buzbee1465db52009-09-23 17:17:35 -07003686 case kChainingCellNormal:
Ben Chenga4973592010-03-31 11:59:18 -07003687 labelList[i].opCode = kArmPseudoChainingCellNormal;
Ben Chengba4fc8b2009-06-01 13:00:29 -07003688 /* handle the codegen later */
3689 dvmInsertGrowableList(
Bill Buzbee1465db52009-09-23 17:17:35 -07003690 &chainingListByType[kChainingCellNormal], (void *) i);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003691 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07003692 case kChainingCellInvokeSingleton:
Ben Cheng38329f52009-07-07 14:19:20 -07003693 labelList[i].opCode =
Ben Chenga4973592010-03-31 11:59:18 -07003694 kArmPseudoChainingCellInvokeSingleton;
Ben Chengba4fc8b2009-06-01 13:00:29 -07003695 labelList[i].operands[0] =
3696 (int) blockList[i]->containingMethod;
3697 /* handle the codegen later */
3698 dvmInsertGrowableList(
Bill Buzbee1465db52009-09-23 17:17:35 -07003699 &chainingListByType[kChainingCellInvokeSingleton],
Ben Cheng38329f52009-07-07 14:19:20 -07003700 (void *) i);
3701 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07003702 case kChainingCellInvokePredicted:
Ben Cheng38329f52009-07-07 14:19:20 -07003703 labelList[i].opCode =
Ben Chenga4973592010-03-31 11:59:18 -07003704 kArmPseudoChainingCellInvokePredicted;
Ben Cheng38329f52009-07-07 14:19:20 -07003705 /* handle the codegen later */
3706 dvmInsertGrowableList(
Bill Buzbee1465db52009-09-23 17:17:35 -07003707 &chainingListByType[kChainingCellInvokePredicted],
Ben Cheng38329f52009-07-07 14:19:20 -07003708 (void *) i);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003709 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07003710 case kChainingCellHot:
Ben Chengba4fc8b2009-06-01 13:00:29 -07003711 labelList[i].opCode =
Ben Chenga4973592010-03-31 11:59:18 -07003712 kArmPseudoChainingCellHot;
Ben Chengba4fc8b2009-06-01 13:00:29 -07003713 /* handle the codegen later */
3714 dvmInsertGrowableList(
Bill Buzbee1465db52009-09-23 17:17:35 -07003715 &chainingListByType[kChainingCellHot],
Ben Chengba4fc8b2009-06-01 13:00:29 -07003716 (void *) i);
3717 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07003718 case kPCReconstruction:
Ben Chengba4fc8b2009-06-01 13:00:29 -07003719 /* Make sure exception handling block is next */
3720 labelList[i].opCode =
Ben Chenga4973592010-03-31 11:59:18 -07003721 kArmPseudoPCReconstructionBlockLabel;
Ben Chengba4fc8b2009-06-01 13:00:29 -07003722 assert (i == cUnit->numBlocks - 2);
3723 handlePCReconstruction(cUnit, &labelList[i+1]);
3724 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07003725 case kExceptionHandling:
3726 labelList[i].opCode = kArmPseudoEHBlockLabel;
Ben Chengba4fc8b2009-06-01 13:00:29 -07003727 if (cUnit->pcReconstructionList.numUsed) {
Bill Buzbee270c1d62009-08-13 16:58:07 -07003728 loadWordDisp(cUnit, rGLUE, offsetof(InterpState,
3729 jitToInterpEntries.dvmJitToInterpPunt),
3730 r1);
Bill Buzbee1465db52009-09-23 17:17:35 -07003731 opReg(cUnit, kOpBlx, r1);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003732 }
3733 break;
Bill Buzbee9c4b7c82009-09-10 10:10:38 -07003734#if defined(WITH_SELF_VERIFICATION) || defined(WITH_JIT_TUNING)
Bill Buzbee1465db52009-09-23 17:17:35 -07003735 case kChainingCellBackwardBranch:
Jeff Hao97319a82009-08-12 16:57:15 -07003736 labelList[i].opCode =
Ben Chenga4973592010-03-31 11:59:18 -07003737 kArmPseudoChainingCellBackwardBranch;
Jeff Hao97319a82009-08-12 16:57:15 -07003738 /* handle the codegen later */
3739 dvmInsertGrowableList(
Bill Buzbee1465db52009-09-23 17:17:35 -07003740 &chainingListByType[kChainingCellBackwardBranch],
Jeff Hao97319a82009-08-12 16:57:15 -07003741 (void *) i);
3742 break;
3743#endif
Ben Chengba4fc8b2009-06-01 13:00:29 -07003744 default:
3745 break;
3746 }
3747 continue;
3748 }
Ben Chenge9695e52009-06-16 16:11:47 -07003749
Bill Buzbee89efc3d2009-07-28 11:22:22 -07003750 ArmLIR *headLIR = NULL;
Ben Chenge9695e52009-06-16 16:11:47 -07003751
Ben Chengba4fc8b2009-06-01 13:00:29 -07003752 for (mir = blockList[i]->firstMIRInsn; mir; mir = mir->next) {
Bill Buzbee1465db52009-09-23 17:17:35 -07003753
Bill Buzbeec6f10662010-02-09 11:16:15 -08003754 dvmCompilerResetRegPool(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07003755 if (gDvmJit.disableOpt & (1 << kTrackLiveTemps)) {
Bill Buzbeec6f10662010-02-09 11:16:15 -08003756 dvmCompilerClobberAllRegs(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07003757 }
3758
3759 if (gDvmJit.disableOpt & (1 << kSuppressLoads)) {
Bill Buzbeec6f10662010-02-09 11:16:15 -08003760 dvmCompilerResetDefTracking(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07003761 }
3762
3763 if (mir->dalvikInsn.opCode >= kMirOpFirst) {
Ben Cheng4238ec22009-08-24 16:32:22 -07003764 handleExtendedMIR(cUnit, mir);
3765 continue;
3766 }
3767
Bill Buzbee1465db52009-09-23 17:17:35 -07003768
Ben Chengba4fc8b2009-06-01 13:00:29 -07003769 OpCode dalvikOpCode = mir->dalvikInsn.opCode;
3770 InstructionFormat dalvikFormat =
3771 dexGetInstrFormat(gDvm.instrFormat, dalvikOpCode);
Bill Buzbee89efc3d2009-07-28 11:22:22 -07003772 ArmLIR *boundaryLIR =
Ben Chenga4973592010-03-31 11:59:18 -07003773 newLIR2(cUnit, kArmPseudoDalvikByteCodeBoundary,
Ben Chengccd6c012009-10-15 14:52:45 -07003774 mir->offset,
3775 (int) dvmCompilerGetDalvikDisassembly(&mir->dalvikInsn)
3776 );
Ben Cheng4238ec22009-08-24 16:32:22 -07003777 if (mir->ssaRep) {
3778 char *ssaString = dvmCompilerGetSSAString(cUnit, mir->ssaRep);
Bill Buzbee1465db52009-09-23 17:17:35 -07003779 newLIR1(cUnit, kArmPseudoSSARep, (int) ssaString);
Ben Cheng4238ec22009-08-24 16:32:22 -07003780 }
3781
Ben Chenge9695e52009-06-16 16:11:47 -07003782 /* Remember the first LIR for this block */
3783 if (headLIR == NULL) {
3784 headLIR = boundaryLIR;
Ben Chengd7d426a2009-09-22 11:23:36 -07003785 /* Set the first boundaryLIR as a scheduling barrier */
3786 headLIR->defMask = ENCODE_ALL;
Ben Chenge9695e52009-06-16 16:11:47 -07003787 }
Ben Cheng4238ec22009-08-24 16:32:22 -07003788
Ben Chengba4fc8b2009-06-01 13:00:29 -07003789 bool notHandled;
3790 /*
3791 * Debugging: screen the opcode first to see if it is in the
3792 * do[-not]-compile list
3793 */
3794 bool singleStepMe =
3795 gDvmJit.includeSelectedOp !=
3796 ((gDvmJit.opList[dalvikOpCode >> 3] &
3797 (1 << (dalvikOpCode & 0x7))) !=
3798 0);
Ben Chengd5adae12010-03-26 17:45:28 -07003799#if defined(WITH_SELF_VERIFICATION)
3800 if (singleStepMe == false) {
3801 singleStepMe = selfVerificationPuntOps(mir);
3802 }
3803#endif
Ben Chengba4fc8b2009-06-01 13:00:29 -07003804 if (singleStepMe || cUnit->allSingleStep) {
3805 notHandled = false;
3806 genInterpSingleStep(cUnit, mir);
3807 } else {
3808 opcodeCoverage[dalvikOpCode]++;
3809 switch (dalvikFormat) {
3810 case kFmt10t:
3811 case kFmt20t:
3812 case kFmt30t:
3813 notHandled = handleFmt10t_Fmt20t_Fmt30t(cUnit,
3814 mir, blockList[i], labelList);
3815 break;
3816 case kFmt10x:
3817 notHandled = handleFmt10x(cUnit, mir);
3818 break;
3819 case kFmt11n:
3820 case kFmt31i:
3821 notHandled = handleFmt11n_Fmt31i(cUnit, mir);
3822 break;
3823 case kFmt11x:
3824 notHandled = handleFmt11x(cUnit, mir);
3825 break;
3826 case kFmt12x:
3827 notHandled = handleFmt12x(cUnit, mir);
3828 break;
3829 case kFmt20bc:
3830 notHandled = handleFmt20bc(cUnit, mir);
3831 break;
3832 case kFmt21c:
3833 case kFmt31c:
3834 notHandled = handleFmt21c_Fmt31c(cUnit, mir);
3835 break;
3836 case kFmt21h:
3837 notHandled = handleFmt21h(cUnit, mir);
3838 break;
3839 case kFmt21s:
3840 notHandled = handleFmt21s(cUnit, mir);
3841 break;
3842 case kFmt21t:
3843 notHandled = handleFmt21t(cUnit, mir, blockList[i],
3844 labelList);
3845 break;
3846 case kFmt22b:
3847 case kFmt22s:
3848 notHandled = handleFmt22b_Fmt22s(cUnit, mir);
3849 break;
3850 case kFmt22c:
3851 notHandled = handleFmt22c(cUnit, mir);
3852 break;
3853 case kFmt22cs:
3854 notHandled = handleFmt22cs(cUnit, mir);
3855 break;
3856 case kFmt22t:
3857 notHandled = handleFmt22t(cUnit, mir, blockList[i],
3858 labelList);
3859 break;
3860 case kFmt22x:
3861 case kFmt32x:
3862 notHandled = handleFmt22x_Fmt32x(cUnit, mir);
3863 break;
3864 case kFmt23x:
3865 notHandled = handleFmt23x(cUnit, mir);
3866 break;
3867 case kFmt31t:
3868 notHandled = handleFmt31t(cUnit, mir);
3869 break;
3870 case kFmt3rc:
3871 case kFmt35c:
3872 notHandled = handleFmt35c_3rc(cUnit, mir, blockList[i],
3873 labelList);
3874 break;
3875 case kFmt3rms:
3876 case kFmt35ms:
3877 notHandled = handleFmt35ms_3rms(cUnit, mir,blockList[i],
3878 labelList);
3879 break;
3880 case kFmt3inline:
Andy McFaddenb0a05412009-11-19 10:23:41 -08003881 case kFmt3rinline:
Bill Buzbeece46c942009-11-20 15:41:34 -08003882 notHandled = handleExecuteInline(cUnit, mir);
Andy McFaddenb0a05412009-11-19 10:23:41 -08003883 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07003884 case kFmt51l:
3885 notHandled = handleFmt51l(cUnit, mir);
3886 break;
3887 default:
3888 notHandled = true;
3889 break;
3890 }
3891 }
3892 if (notHandled) {
3893 LOGE("%#06x: Opcode 0x%x (%s) / Fmt %d not handled\n",
3894 mir->offset,
3895 dalvikOpCode, getOpcodeName(dalvikOpCode),
3896 dalvikFormat);
Bill Buzbeefc519dc2010-03-06 23:30:57 -08003897 dvmCompilerAbort(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003898 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07003899 }
3900 }
Ben Cheng4238ec22009-08-24 16:32:22 -07003901
Bill Buzbee1465db52009-09-23 17:17:35 -07003902 if (blockList[i]->blockType == kEntryBlock) {
Ben Cheng4238ec22009-08-24 16:32:22 -07003903 dvmCompilerAppendLIR(cUnit,
3904 (LIR *) cUnit->loopAnalysis->branchToBody);
3905 dvmCompilerAppendLIR(cUnit,
3906 (LIR *) cUnit->loopAnalysis->branchToPCR);
3907 }
3908
3909 if (headLIR) {
3910 /*
3911 * Eliminate redundant loads/stores and delay stores into later
3912 * slots
3913 */
3914 dvmCompilerApplyLocalOptimizations(cUnit, (LIR *) headLIR,
3915 cUnit->lastLIRInsn);
3916 }
3917
3918gen_fallthrough:
Ben Cheng1efc9c52009-06-08 18:25:27 -07003919 /*
3920 * Check if the block is terminated due to trace length constraint -
3921 * insert an unconditional branch to the chaining cell.
3922 */
3923 if (blockList[i]->needFallThroughBranch) {
3924 genUnconditionalBranch(cUnit,
3925 &labelList[blockList[i]->fallThrough->id]);
3926 }
3927
Ben Chengba4fc8b2009-06-01 13:00:29 -07003928 }
3929
Ben Chenge9695e52009-06-16 16:11:47 -07003930 /* Handle the chaining cells in predefined order */
Ben Chengcec26f62010-01-15 15:29:33 -08003931 for (i = 0; i < kChainingCellGap; i++) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07003932 size_t j;
3933 int *blockIdList = (int *) chainingListByType[i].elemList;
3934
3935 cUnit->numChainingCells[i] = chainingListByType[i].numUsed;
3936
3937 /* No chaining cells of this type */
3938 if (cUnit->numChainingCells[i] == 0)
3939 continue;
3940
3941 /* Record the first LIR for a new type of chaining cell */
3942 cUnit->firstChainingLIR[i] = (LIR *) &labelList[blockIdList[0]];
3943
3944 for (j = 0; j < chainingListByType[i].numUsed; j++) {
3945 int blockId = blockIdList[j];
3946
3947 /* Align this chaining cell first */
Bill Buzbee1465db52009-09-23 17:17:35 -07003948 newLIR0(cUnit, kArmPseudoPseudoAlign4);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003949
3950 /* Insert the pseudo chaining instruction */
3951 dvmCompilerAppendLIR(cUnit, (LIR *) &labelList[blockId]);
3952
3953
3954 switch (blockList[blockId]->blockType) {
Bill Buzbee1465db52009-09-23 17:17:35 -07003955 case kChainingCellNormal:
Ben Cheng1efc9c52009-06-08 18:25:27 -07003956 handleNormalChainingCell(cUnit,
Ben Chengba4fc8b2009-06-01 13:00:29 -07003957 blockList[blockId]->startOffset);
3958 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07003959 case kChainingCellInvokeSingleton:
Ben Cheng38329f52009-07-07 14:19:20 -07003960 handleInvokeSingletonChainingCell(cUnit,
Ben Chengba4fc8b2009-06-01 13:00:29 -07003961 blockList[blockId]->containingMethod);
3962 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07003963 case kChainingCellInvokePredicted:
Ben Cheng38329f52009-07-07 14:19:20 -07003964 handleInvokePredictedChainingCell(cUnit);
3965 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07003966 case kChainingCellHot:
Ben Cheng1efc9c52009-06-08 18:25:27 -07003967 handleHotChainingCell(cUnit,
Ben Chengba4fc8b2009-06-01 13:00:29 -07003968 blockList[blockId]->startOffset);
3969 break;
Bill Buzbee9c4b7c82009-09-10 10:10:38 -07003970#if defined(WITH_SELF_VERIFICATION) || defined(WITH_JIT_TUNING)
Bill Buzbee1465db52009-09-23 17:17:35 -07003971 case kChainingCellBackwardBranch:
Jeff Hao97319a82009-08-12 16:57:15 -07003972 handleBackwardBranchChainingCell(cUnit,
3973 blockList[blockId]->startOffset);
3974 break;
3975#endif
Ben Chengba4fc8b2009-06-01 13:00:29 -07003976 default:
Bill Buzbee1465db52009-09-23 17:17:35 -07003977 LOGE("Bad blocktype %d", blockList[blockId]->blockType);
Bill Buzbeefc519dc2010-03-06 23:30:57 -08003978 dvmCompilerAbort(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003979 }
3980 }
3981 }
Ben Chenge9695e52009-06-16 16:11:47 -07003982
Ben Chengcec26f62010-01-15 15:29:33 -08003983 /* Mark the bottom of chaining cells */
3984 cUnit->chainingCellBottom = (LIR *) newLIR0(cUnit, kArmChainingCellBottom);
3985
Ben Cheng6c10a972009-10-29 14:39:18 -07003986 /*
3987 * Generate the branch to the dvmJitToInterpNoChain entry point at the end
3988 * of all chaining cells for the overflow cases.
3989 */
3990 if (cUnit->switchOverflowPad) {
3991 loadConstant(cUnit, r0, (int) cUnit->switchOverflowPad);
3992 loadWordDisp(cUnit, rGLUE, offsetof(InterpState,
3993 jitToInterpEntries.dvmJitToInterpNoChain), r2);
3994 opRegReg(cUnit, kOpAdd, r1, r1);
3995 opRegRegReg(cUnit, kOpAdd, r4PC, r0, r1);
Ben Cheng978738d2010-05-13 13:45:57 -07003996#if defined(WITH_JIT_TUNING)
Ben Cheng6c10a972009-10-29 14:39:18 -07003997 loadConstant(cUnit, r0, kSwitchOverflow);
3998#endif
3999 opReg(cUnit, kOpBlx, r2);
4000 }
4001
Ben Chenge9695e52009-06-16 16:11:47 -07004002 dvmCompilerApplyGlobalOptimizations(cUnit);
jeffhao9e45c0b2010-02-03 10:24:05 -08004003
4004#if defined(WITH_SELF_VERIFICATION)
4005 selfVerificationBranchInsertPass(cUnit);
4006#endif
Ben Chengba4fc8b2009-06-01 13:00:29 -07004007}
4008
4009/* Accept the work and start compiling */
Bill Buzbee716f1202009-07-23 13:22:09 -07004010bool dvmCompilerDoWork(CompilerWorkOrder *work)
Ben Chengba4fc8b2009-06-01 13:00:29 -07004011{
Ben Chengccd6c012009-10-15 14:52:45 -07004012 bool res;
Ben Chengba4fc8b2009-06-01 13:00:29 -07004013
Ben Cheng6999d842010-01-26 16:46:15 -08004014 if (gDvmJit.codeCacheFull) {
Ben Chengccd6c012009-10-15 14:52:45 -07004015 return false;
4016 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07004017
Ben Chengccd6c012009-10-15 14:52:45 -07004018 switch (work->kind) {
4019 case kWorkOrderMethod:
4020 res = dvmCompileMethod(work->info, &work->result);
4021 break;
4022 case kWorkOrderTrace:
4023 /* Start compilation with maximally allowed trace length */
Bill Buzbeefc519dc2010-03-06 23:30:57 -08004024 res = dvmCompileTrace(work->info, JIT_MAX_TRACE_LEN, &work->result,
4025 work->bailPtr);
Ben Chengccd6c012009-10-15 14:52:45 -07004026 break;
4027 case kWorkOrderTraceDebug: {
4028 bool oldPrintMe = gDvmJit.printMe;
4029 gDvmJit.printMe = true;
4030 /* Start compilation with maximally allowed trace length */
Bill Buzbeefc519dc2010-03-06 23:30:57 -08004031 res = dvmCompileTrace(work->info, JIT_MAX_TRACE_LEN, &work->result,
4032 work->bailPtr);
Elliott Hughes672511b2010-04-26 17:40:13 -07004033 gDvmJit.printMe = oldPrintMe;
Ben Chengccd6c012009-10-15 14:52:45 -07004034 break;
4035 }
4036 default:
4037 res = false;
Bill Buzbeefc519dc2010-03-06 23:30:57 -08004038 LOGE("Jit: unknown work order type");
Elliott Hughes672511b2010-04-26 17:40:13 -07004039 assert(0); // Bail if debug build, discard otherwise
Ben Chengccd6c012009-10-15 14:52:45 -07004040 }
4041 return res;
Ben Chengba4fc8b2009-06-01 13:00:29 -07004042}
4043
Ben Chengba4fc8b2009-06-01 13:00:29 -07004044/* Architectural-specific debugging helpers go here */
4045void dvmCompilerArchDump(void)
4046{
4047 /* Print compiled opcode in this VM instance */
4048 int i, start, streak;
4049 char buf[1024];
4050
4051 streak = i = 0;
4052 buf[0] = 0;
4053 while (opcodeCoverage[i] == 0 && i < 256) {
4054 i++;
4055 }
4056 if (i == 256) {
4057 return;
4058 }
4059 for (start = i++, streak = 1; i < 256; i++) {
4060 if (opcodeCoverage[i]) {
4061 streak++;
4062 } else {
4063 if (streak == 1) {
4064 sprintf(buf+strlen(buf), "%x,", start);
4065 } else {
4066 sprintf(buf+strlen(buf), "%x-%x,", start, start + streak - 1);
4067 }
4068 streak = 0;
4069 while (opcodeCoverage[i] == 0 && i < 256) {
4070 i++;
4071 }
4072 if (i < 256) {
4073 streak = 1;
4074 start = i;
4075 }
4076 }
4077 }
4078 if (streak) {
4079 if (streak == 1) {
4080 sprintf(buf+strlen(buf), "%x", start);
4081 } else {
4082 sprintf(buf+strlen(buf), "%x-%x", start, start + streak - 1);
4083 }
4084 }
4085 if (strlen(buf)) {
Ben Cheng8b258bf2009-06-24 17:27:07 -07004086 LOGD("dalvik.vm.jit.op = %s", buf);
Ben Chengba4fc8b2009-06-01 13:00:29 -07004087 }
4088}
Ben Chengd7d426a2009-09-22 11:23:36 -07004089
4090/* Common initialization routine for an architecture family */
4091bool dvmCompilerArchInit()
4092{
4093 int i;
4094
Bill Buzbee1465db52009-09-23 17:17:35 -07004095 for (i = 0; i < kArmLast; i++) {
Ben Chengd7d426a2009-09-22 11:23:36 -07004096 if (EncodingMap[i].opCode != i) {
4097 LOGE("Encoding order for %s is wrong: expecting %d, seeing %d",
4098 EncodingMap[i].name, i, EncodingMap[i].opCode);
Bill Buzbeefc519dc2010-03-06 23:30:57 -08004099 dvmAbort(); // OK to dvmAbort - build error
Ben Chengd7d426a2009-09-22 11:23:36 -07004100 }
4101 }
4102
Ben Cheng5d90c202009-11-22 23:31:11 -08004103 return dvmCompilerArchVariantInit();
4104}
4105
4106void *dvmCompilerGetInterpretTemplate()
4107{
4108 return (void*) ((int)gDvmJit.codeCache +
4109 templateEntryOffsets[TEMPLATE_INTERPRET]);
4110}
4111
4112/* Needed by the ld/st optmizatons */
4113ArmLIR* dvmCompilerRegCopyNoInsert(CompilationUnit *cUnit, int rDest, int rSrc)
4114{
4115 return genRegCopyNoInsert(cUnit, rDest, rSrc);
4116}
4117
4118/* Needed by the register allocator */
4119ArmLIR* dvmCompilerRegCopy(CompilationUnit *cUnit, int rDest, int rSrc)
4120{
4121 return genRegCopy(cUnit, rDest, rSrc);
4122}
4123
4124/* Needed by the register allocator */
4125void dvmCompilerRegCopyWide(CompilationUnit *cUnit, int destLo, int destHi,
4126 int srcLo, int srcHi)
4127{
4128 genRegCopyWide(cUnit, destLo, destHi, srcLo, srcHi);
4129}
4130
4131void dvmCompilerFlushRegImpl(CompilationUnit *cUnit, int rBase,
4132 int displacement, int rSrc, OpSize size)
4133{
4134 storeBaseDisp(cUnit, rBase, displacement, rSrc, size);
4135}
4136
4137void dvmCompilerFlushRegWideImpl(CompilationUnit *cUnit, int rBase,
4138 int displacement, int rSrcLo, int rSrcHi)
4139{
4140 storeBaseDispWide(cUnit, rBase, displacement, rSrcLo, rSrcHi);
Ben Chengd7d426a2009-09-22 11:23:36 -07004141}