blob: f625771ec13e2ae14df702495dc27778898f5d7e [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
Bill Buzbeec1d9ed42010-02-02 11:04:33 -08001217/*
1218 * To prevent a thread in a monitor wait from blocking the Jit from
1219 * resetting the code cache, heavyweight monitor lock will not
1220 * be allowed to return to an existing translation. Instead, we will
1221 * handle them by branching to a handler, which will in turn call the
1222 * runtime lock routine and then branch directly back to the
1223 * interpreter main loop. Given the high cost of the heavyweight
1224 * lock operation, this additional cost should be slight (especially when
1225 * considering that we expect the vast majority of lock operations to
1226 * use the fast-path thin lock bypass).
1227 */
Ben Cheng5d90c202009-11-22 23:31:11 -08001228static void genMonitorPortable(CompilationUnit *cUnit, MIR *mir)
Bill Buzbee270c1d62009-08-13 16:58:07 -07001229{
Bill Buzbeeefbd3c52009-11-04 22:18:40 -08001230 bool isEnter = (mir->dalvikInsn.opCode == OP_MONITOR_ENTER);
Bill Buzbee1465db52009-09-23 17:17:35 -07001231 genExportPC(cUnit, mir);
Bill Buzbeec6f10662010-02-09 11:16:15 -08001232 dvmCompilerFlushAllRegs(cUnit); /* Send everything to home location */
1233 RegLocation rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07001234 loadValueDirectFixed(cUnit, rlSrc, r1);
1235 loadWordDisp(cUnit, rGLUE, offsetof(InterpState, self), r0);
Bill Buzbeec1d9ed42010-02-02 11:04:33 -08001236 genNullCheck(cUnit, rlSrc.sRegLow, r1, mir->offset, NULL);
Bill Buzbeeefbd3c52009-11-04 22:18:40 -08001237 if (isEnter) {
Bill Buzbeec1d9ed42010-02-02 11:04:33 -08001238 /* Get dPC of next insn */
1239 loadConstant(cUnit, r4PC, (int)(cUnit->method->insns + mir->offset +
1240 dexGetInstrWidthAbs(gDvm.instrWidth, OP_MONITOR_ENTER)));
1241#if defined(WITH_DEADLOCK_PREDICTION)
1242 genDispatchToHandler(cUnit, TEMPLATE_MONITOR_ENTER_DEBUG);
1243#else
1244 genDispatchToHandler(cUnit, TEMPLATE_MONITOR_ENTER);
1245#endif
Bill Buzbee1465db52009-09-23 17:17:35 -07001246 } else {
Ben Chengbd1326d2010-04-02 15:04:53 -07001247 LOAD_FUNC_ADDR(cUnit, r2, (int)dvmUnlockObject);
Bill Buzbeec1d9ed42010-02-02 11:04:33 -08001248 /* Do the call */
1249 opReg(cUnit, kOpBlx, r2);
Bill Buzbee6bbdd6b2010-02-16 14:40:01 -08001250 opRegImm(cUnit, kOpCmp, r0, 0); /* Did we throw? */
1251 ArmLIR *branchOver = opCondBranch(cUnit, kArmCondNe);
1252 loadConstant(cUnit, r0,
1253 (int) (cUnit->method->insns + mir->offset +
1254 dexGetInstrWidthAbs(gDvm.instrWidth, OP_MONITOR_EXIT)));
1255 genDispatchToHandler(cUnit, TEMPLATE_THROW_EXCEPTION_COMMON);
1256 ArmLIR *target = newLIR0(cUnit, kArmPseudoTargetLabel);
1257 target->defMask = ENCODE_ALL;
1258 branchOver->generic.target = (LIR *) target;
Elliott Hughes6a555132010-02-25 15:41:42 -08001259 dvmCompilerClobberCallRegs(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07001260 }
Bill Buzbee270c1d62009-08-13 16:58:07 -07001261}
1262
Ben Chengba4fc8b2009-06-01 13:00:29 -07001263/*
1264 * The following are the first-level codegen routines that analyze the format
1265 * of each bytecode then either dispatch special purpose codegen routines
1266 * or produce corresponding Thumb instructions directly.
1267 */
1268
1269static bool handleFmt10t_Fmt20t_Fmt30t(CompilationUnit *cUnit, MIR *mir,
Bill Buzbee89efc3d2009-07-28 11:22:22 -07001270 BasicBlock *bb, ArmLIR *labelList)
Ben Chengba4fc8b2009-06-01 13:00:29 -07001271{
1272 /* For OP_GOTO, OP_GOTO_16, and OP_GOTO_32 */
1273 genUnconditionalBranch(cUnit, &labelList[bb->taken->id]);
1274 return false;
1275}
1276
1277static bool handleFmt10x(CompilationUnit *cUnit, MIR *mir)
1278{
1279 OpCode dalvikOpCode = mir->dalvikInsn.opCode;
1280 if (((dalvikOpCode >= OP_UNUSED_3E) && (dalvikOpCode <= OP_UNUSED_43)) ||
Andy McFadden53878242010-03-05 07:24:27 -08001281 ((dalvikOpCode >= OP_UNUSED_E3) && (dalvikOpCode <= OP_UNUSED_E7))) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07001282 LOGE("Codegen: got unused opcode 0x%x\n",dalvikOpCode);
1283 return true;
1284 }
1285 switch (dalvikOpCode) {
1286 case OP_RETURN_VOID:
1287 genReturnCommon(cUnit,mir);
1288 break;
1289 case OP_UNUSED_73:
1290 case OP_UNUSED_79:
1291 case OP_UNUSED_7A:
1292 LOGE("Codegen: got unused opcode 0x%x\n",dalvikOpCode);
1293 return true;
1294 case OP_NOP:
1295 break;
1296 default:
1297 return true;
1298 }
1299 return false;
1300}
1301
1302static bool handleFmt11n_Fmt31i(CompilationUnit *cUnit, MIR *mir)
1303{
Bill Buzbee1465db52009-09-23 17:17:35 -07001304 RegLocation rlDest;
1305 RegLocation rlResult;
1306 if (mir->ssaRep->numDefs == 2) {
Bill Buzbeec6f10662010-02-09 11:16:15 -08001307 rlDest = dvmCompilerGetDestWide(cUnit, mir, 0, 1);
Bill Buzbee1465db52009-09-23 17:17:35 -07001308 } else {
Bill Buzbeec6f10662010-02-09 11:16:15 -08001309 rlDest = dvmCompilerGetDest(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07001310 }
Ben Chenge9695e52009-06-16 16:11:47 -07001311
Ben Chengba4fc8b2009-06-01 13:00:29 -07001312 switch (mir->dalvikInsn.opCode) {
1313 case OP_CONST:
Ben Chenge9695e52009-06-16 16:11:47 -07001314 case OP_CONST_4: {
Bill Buzbeec6f10662010-02-09 11:16:15 -08001315 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kAnyReg, true);
Ben Chengbd1326d2010-04-02 15:04:53 -07001316 loadConstantNoClobber(cUnit, rlResult.lowReg, mir->dalvikInsn.vB);
Bill Buzbee1465db52009-09-23 17:17:35 -07001317 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001318 break;
Ben Chenge9695e52009-06-16 16:11:47 -07001319 }
1320 case OP_CONST_WIDE_32: {
Bill Buzbee1465db52009-09-23 17:17:35 -07001321 //TUNING: single routine to load constant pair for support doubles
Bill Buzbee964a7b02010-01-28 12:54:19 -08001322 //TUNING: load 0/-1 separately to avoid load dependency
Bill Buzbeec6f10662010-02-09 11:16:15 -08001323 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Ben Chengbd1326d2010-04-02 15:04:53 -07001324 loadConstantNoClobber(cUnit, rlResult.lowReg, mir->dalvikInsn.vB);
Bill Buzbee1465db52009-09-23 17:17:35 -07001325 opRegRegImm(cUnit, kOpAsr, rlResult.highReg,
1326 rlResult.lowReg, 31);
1327 storeValueWide(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001328 break;
Ben Chenge9695e52009-06-16 16:11:47 -07001329 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07001330 default:
1331 return true;
1332 }
1333 return false;
1334}
1335
1336static bool handleFmt21h(CompilationUnit *cUnit, MIR *mir)
1337{
Bill Buzbee1465db52009-09-23 17:17:35 -07001338 RegLocation rlDest;
1339 RegLocation rlResult;
1340 if (mir->ssaRep->numDefs == 2) {
Bill Buzbeec6f10662010-02-09 11:16:15 -08001341 rlDest = dvmCompilerGetDestWide(cUnit, mir, 0, 1);
Bill Buzbee1465db52009-09-23 17:17:35 -07001342 } else {
Bill Buzbeec6f10662010-02-09 11:16:15 -08001343 rlDest = dvmCompilerGetDest(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07001344 }
Bill Buzbeec6f10662010-02-09 11:16:15 -08001345 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kAnyReg, true);
Ben Chenge9695e52009-06-16 16:11:47 -07001346
Ben Chengba4fc8b2009-06-01 13:00:29 -07001347 switch (mir->dalvikInsn.opCode) {
Ben Chenge9695e52009-06-16 16:11:47 -07001348 case OP_CONST_HIGH16: {
Ben Chengbd1326d2010-04-02 15:04:53 -07001349 loadConstantNoClobber(cUnit, rlResult.lowReg,
1350 mir->dalvikInsn.vB << 16);
Bill Buzbee1465db52009-09-23 17:17:35 -07001351 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001352 break;
Ben Chenge9695e52009-06-16 16:11:47 -07001353 }
1354 case OP_CONST_WIDE_HIGH16: {
Bill Buzbee1465db52009-09-23 17:17:35 -07001355 loadConstantValueWide(cUnit, rlResult.lowReg, rlResult.highReg,
1356 0, mir->dalvikInsn.vB << 16);
1357 storeValueWide(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001358 break;
Ben Chenge9695e52009-06-16 16:11:47 -07001359 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07001360 default:
1361 return true;
1362 }
1363 return false;
1364}
1365
1366static bool handleFmt20bc(CompilationUnit *cUnit, MIR *mir)
1367{
1368 /* For OP_THROW_VERIFICATION_ERROR */
1369 genInterpSingleStep(cUnit, mir);
1370 return false;
1371}
1372
1373static bool handleFmt21c_Fmt31c(CompilationUnit *cUnit, MIR *mir)
1374{
Bill Buzbee1465db52009-09-23 17:17:35 -07001375 RegLocation rlResult;
1376 RegLocation rlDest;
1377 RegLocation rlSrc;
Ben Chenge9695e52009-06-16 16:11:47 -07001378
Ben Chengba4fc8b2009-06-01 13:00:29 -07001379 switch (mir->dalvikInsn.opCode) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07001380 case OP_CONST_STRING_JUMBO:
1381 case OP_CONST_STRING: {
1382 void *strPtr = (void*)
1383 (cUnit->method->clazz->pDvmDex->pResStrings[mir->dalvikInsn.vB]);
Ben Chengdd6e8702010-05-07 13:05:47 -07001384
1385 if (strPtr == NULL) {
1386 LOGE("Unexpected null string");
1387 dvmAbort();
1388 }
1389
Bill Buzbeec6f10662010-02-09 11:16:15 -08001390 rlDest = dvmCompilerGetDest(cUnit, mir, 0);
1391 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Ben Chengbd1326d2010-04-02 15:04:53 -07001392 loadConstantNoClobber(cUnit, rlResult.lowReg, (int) strPtr );
Bill Buzbee1465db52009-09-23 17:17:35 -07001393 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001394 break;
1395 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07001396 case OP_CONST_CLASS: {
1397 void *classPtr = (void*)
1398 (cUnit->method->clazz->pDvmDex->pResClasses[mir->dalvikInsn.vB]);
Ben Chengdd6e8702010-05-07 13:05:47 -07001399
1400 if (classPtr == NULL) {
1401 LOGE("Unexpected null class");
1402 dvmAbort();
1403 }
1404
Bill Buzbeec6f10662010-02-09 11:16:15 -08001405 rlDest = dvmCompilerGetDest(cUnit, mir, 0);
1406 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Ben Chengbd1326d2010-04-02 15:04:53 -07001407 loadConstantNoClobber(cUnit, rlResult.lowReg, (int) classPtr );
Bill Buzbee1465db52009-09-23 17:17:35 -07001408 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001409 break;
1410 }
1411 case OP_SGET_OBJECT:
1412 case OP_SGET_BOOLEAN:
1413 case OP_SGET_CHAR:
1414 case OP_SGET_BYTE:
1415 case OP_SGET_SHORT:
1416 case OP_SGET: {
Bill Buzbee50a6bf22009-07-08 13:08:04 -07001417 int valOffset = offsetof(StaticField, value);
Bill Buzbeec6f10662010-02-09 11:16:15 -08001418 int tReg = dvmCompilerAllocTemp(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001419 void *fieldPtr = (void*)
1420 (cUnit->method->clazz->pDvmDex->pResFields[mir->dalvikInsn.vB]);
Ben Chengdd6e8702010-05-07 13:05:47 -07001421
1422 if (fieldPtr == NULL) {
1423 LOGE("Unexpected null static field");
1424 dvmAbort();
1425 }
1426
Bill Buzbeec6f10662010-02-09 11:16:15 -08001427 rlDest = dvmCompilerGetDest(cUnit, mir, 0);
1428 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kAnyReg, true);
Bill Buzbee1465db52009-09-23 17:17:35 -07001429 loadConstant(cUnit, tReg, (int) fieldPtr + valOffset);
Ben Cheng11d8f142010-03-24 15:24:19 -07001430
1431 HEAP_ACCESS_SHADOW(true);
Bill Buzbee1465db52009-09-23 17:17:35 -07001432 loadWordDisp(cUnit, tReg, 0, rlResult.lowReg);
Ben Cheng11d8f142010-03-24 15:24:19 -07001433 HEAP_ACCESS_SHADOW(false);
1434
Bill Buzbee1465db52009-09-23 17:17:35 -07001435 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001436 break;
1437 }
1438 case OP_SGET_WIDE: {
Bill Buzbee50a6bf22009-07-08 13:08:04 -07001439 int valOffset = offsetof(StaticField, value);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001440 void *fieldPtr = (void*)
1441 (cUnit->method->clazz->pDvmDex->pResFields[mir->dalvikInsn.vB]);
Ben Chengdd6e8702010-05-07 13:05:47 -07001442
1443 if (fieldPtr == NULL) {
1444 LOGE("Unexpected null static field");
1445 dvmAbort();
1446 }
1447
Bill Buzbeec6f10662010-02-09 11:16:15 -08001448 int tReg = dvmCompilerAllocTemp(cUnit);
Bill Buzbeec6f10662010-02-09 11:16:15 -08001449 rlDest = dvmCompilerGetDestWide(cUnit, mir, 0, 1);
1450 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kAnyReg, true);
Bill Buzbee1465db52009-09-23 17:17:35 -07001451 loadConstant(cUnit, tReg, (int) fieldPtr + valOffset);
Ben Cheng11d8f142010-03-24 15:24:19 -07001452
1453 HEAP_ACCESS_SHADOW(true);
Bill Buzbee1465db52009-09-23 17:17:35 -07001454 loadPair(cUnit, tReg, rlResult.lowReg, rlResult.highReg);
Ben Cheng11d8f142010-03-24 15:24:19 -07001455 HEAP_ACCESS_SHADOW(false);
1456
Bill Buzbee1465db52009-09-23 17:17:35 -07001457 storeValueWide(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001458 break;
1459 }
1460 case OP_SPUT_OBJECT:
1461 case OP_SPUT_BOOLEAN:
1462 case OP_SPUT_CHAR:
1463 case OP_SPUT_BYTE:
1464 case OP_SPUT_SHORT:
1465 case OP_SPUT: {
Bill Buzbee50a6bf22009-07-08 13:08:04 -07001466 int valOffset = offsetof(StaticField, value);
Bill Buzbeec6f10662010-02-09 11:16:15 -08001467 int tReg = dvmCompilerAllocTemp(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001468 void *fieldPtr = (void*)
1469 (cUnit->method->clazz->pDvmDex->pResFields[mir->dalvikInsn.vB]);
Ben Chenge9695e52009-06-16 16:11:47 -07001470
Ben Chengdd6e8702010-05-07 13:05:47 -07001471 if (fieldPtr == NULL) {
1472 LOGE("Unexpected null static field");
1473 dvmAbort();
1474 }
1475
Bill Buzbeec6f10662010-02-09 11:16:15 -08001476 rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07001477 rlSrc = loadValue(cUnit, rlSrc, kAnyReg);
1478 loadConstant(cUnit, tReg, (int) fieldPtr + valOffset);
Ben Cheng11d8f142010-03-24 15:24:19 -07001479
1480 HEAP_ACCESS_SHADOW(true);
Bill Buzbee1465db52009-09-23 17:17:35 -07001481 storeWordDisp(cUnit, tReg, 0 ,rlSrc.lowReg);
Ben Cheng11d8f142010-03-24 15:24:19 -07001482 HEAP_ACCESS_SHADOW(false);
1483
Ben Chengba4fc8b2009-06-01 13:00:29 -07001484 break;
1485 }
1486 case OP_SPUT_WIDE: {
Bill Buzbeec6f10662010-02-09 11:16:15 -08001487 int tReg = dvmCompilerAllocTemp(cUnit);
Bill Buzbee50a6bf22009-07-08 13:08:04 -07001488 int valOffset = offsetof(StaticField, value);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001489 void *fieldPtr = (void*)
1490 (cUnit->method->clazz->pDvmDex->pResFields[mir->dalvikInsn.vB]);
Ben Chenge9695e52009-06-16 16:11:47 -07001491
Ben Chengdd6e8702010-05-07 13:05:47 -07001492 if (fieldPtr == NULL) {
1493 LOGE("Unexpected null static field");
1494 dvmAbort();
1495 }
1496
Bill Buzbeec6f10662010-02-09 11:16:15 -08001497 rlSrc = dvmCompilerGetSrcWide(cUnit, mir, 0, 1);
Bill Buzbee1465db52009-09-23 17:17:35 -07001498 rlSrc = loadValueWide(cUnit, rlSrc, kAnyReg);
1499 loadConstant(cUnit, tReg, (int) fieldPtr + valOffset);
Ben Cheng11d8f142010-03-24 15:24:19 -07001500
1501 HEAP_ACCESS_SHADOW(true);
Bill Buzbee1465db52009-09-23 17:17:35 -07001502 storePair(cUnit, tReg, rlSrc.lowReg, rlSrc.highReg);
Ben Cheng11d8f142010-03-24 15:24:19 -07001503 HEAP_ACCESS_SHADOW(false);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001504 break;
1505 }
1506 case OP_NEW_INSTANCE: {
Ben Chenge9695e52009-06-16 16:11:47 -07001507 /*
1508 * Obey the calling convention and don't mess with the register
1509 * usage.
1510 */
Ben Chengba4fc8b2009-06-01 13:00:29 -07001511 ClassObject *classPtr = (void*)
1512 (cUnit->method->clazz->pDvmDex->pResClasses[mir->dalvikInsn.vB]);
Ben Chengdd6e8702010-05-07 13:05:47 -07001513
1514 if (classPtr == NULL) {
1515 LOGE("Unexpected null class");
1516 dvmAbort();
1517 }
1518
Ben Cheng79d173c2009-09-29 16:12:51 -07001519 /*
1520 * If it is going to throw, it should not make to the trace to begin
Bill Buzbee1465db52009-09-23 17:17:35 -07001521 * with. However, Alloc might throw, so we need to genExportPC()
Ben Cheng79d173c2009-09-29 16:12:51 -07001522 */
1523 assert((classPtr->accessFlags & (ACC_INTERFACE|ACC_ABSTRACT)) == 0);
Bill Buzbeec6f10662010-02-09 11:16:15 -08001524 dvmCompilerFlushAllRegs(cUnit); /* Everything to home location */
Bill Buzbee1465db52009-09-23 17:17:35 -07001525 genExportPC(cUnit, mir);
Ben Chengbd1326d2010-04-02 15:04:53 -07001526 LOAD_FUNC_ADDR(cUnit, r2, (int)dvmAllocObject);
Ben Chenge9695e52009-06-16 16:11:47 -07001527 loadConstant(cUnit, r0, (int) classPtr);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001528 loadConstant(cUnit, r1, ALLOC_DONT_TRACK);
Bill Buzbee1465db52009-09-23 17:17:35 -07001529 opReg(cUnit, kOpBlx, r2);
Elliott Hughes6a555132010-02-25 15:41:42 -08001530 dvmCompilerClobberCallRegs(cUnit);
Ben Cheng4f489172009-09-27 17:08:35 -07001531 /* generate a branch over if allocation is successful */
Bill Buzbee1465db52009-09-23 17:17:35 -07001532 opRegImm(cUnit, kOpCmp, r0, 0); /* NULL? */
1533 ArmLIR *branchOver = opCondBranch(cUnit, kArmCondNe);
Ben Cheng4f489172009-09-27 17:08:35 -07001534 /*
1535 * OOM exception needs to be thrown here and cannot re-execute
1536 */
1537 loadConstant(cUnit, r0,
1538 (int) (cUnit->method->insns + mir->offset));
1539 genDispatchToHandler(cUnit, TEMPLATE_THROW_EXCEPTION_COMMON);
1540 /* noreturn */
1541
Bill Buzbee1465db52009-09-23 17:17:35 -07001542 ArmLIR *target = newLIR0(cUnit, kArmPseudoTargetLabel);
Ben Cheng4f489172009-09-27 17:08:35 -07001543 target->defMask = ENCODE_ALL;
1544 branchOver->generic.target = (LIR *) target;
Bill Buzbeec6f10662010-02-09 11:16:15 -08001545 rlDest = dvmCompilerGetDest(cUnit, mir, 0);
1546 rlResult = dvmCompilerGetReturn(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07001547 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001548 break;
1549 }
1550 case OP_CHECK_CAST: {
Ben Chenge9695e52009-06-16 16:11:47 -07001551 /*
1552 * Obey the calling convention and don't mess with the register
1553 * usage.
1554 */
Ben Chengba4fc8b2009-06-01 13:00:29 -07001555 ClassObject *classPtr =
1556 (cUnit->method->clazz->pDvmDex->pResClasses[mir->dalvikInsn.vB]);
Bill Buzbee4df41a52009-11-12 17:07:16 -08001557 /*
1558 * Note: It is possible that classPtr is NULL at this point,
1559 * even though this instruction has been successfully interpreted.
1560 * If the previous interpretation had a null source, the
1561 * interpreter would not have bothered to resolve the clazz.
1562 * Bail out to the interpreter in this case, and log it
1563 * so that we can tell if it happens frequently.
1564 */
1565 if (classPtr == NULL) {
Ben Cheng11d8f142010-03-24 15:24:19 -07001566 LOGVV("null clazz in OP_CHECK_CAST, single-stepping");
Bill Buzbee4df41a52009-11-12 17:07:16 -08001567 genInterpSingleStep(cUnit, mir);
1568 return false;
1569 }
Bill Buzbeec6f10662010-02-09 11:16:15 -08001570 dvmCompilerFlushAllRegs(cUnit); /* Everything to home location */
Ben Chengba4fc8b2009-06-01 13:00:29 -07001571 loadConstant(cUnit, r1, (int) classPtr );
Bill Buzbeec6f10662010-02-09 11:16:15 -08001572 rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07001573 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
1574 opRegImm(cUnit, kOpCmp, rlSrc.lowReg, 0); /* Null? */
1575 ArmLIR *branch1 = opCondBranch(cUnit, kArmCondEq);
1576 /*
1577 * rlSrc.lowReg now contains object->clazz. Note that
1578 * it could have been allocated r0, but we're okay so long
1579 * as we don't do anything desctructive until r0 is loaded
1580 * with clazz.
1581 */
Ben Chengba4fc8b2009-06-01 13:00:29 -07001582 /* r0 now contains object->clazz */
Bill Buzbee1465db52009-09-23 17:17:35 -07001583 loadWordDisp(cUnit, rlSrc.lowReg, offsetof(Object, clazz), r0);
Ben Chengbd1326d2010-04-02 15:04:53 -07001584 LOAD_FUNC_ADDR(cUnit, r2, (int)dvmInstanceofNonTrivial);
Bill Buzbee1465db52009-09-23 17:17:35 -07001585 opRegReg(cUnit, kOpCmp, r0, r1);
1586 ArmLIR *branch2 = opCondBranch(cUnit, kArmCondEq);
1587 opReg(cUnit, kOpBlx, r2);
Elliott Hughes6a555132010-02-25 15:41:42 -08001588 dvmCompilerClobberCallRegs(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07001589 /*
1590 * If null, check cast failed - punt to the interpreter. Because
1591 * interpreter will be the one throwing, we don't need to
1592 * genExportPC() here.
1593 */
Bill Buzbee270c1d62009-08-13 16:58:07 -07001594 genZeroCheck(cUnit, r0, mir->offset, NULL);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001595 /* check cast passed - branch target here */
Bill Buzbee1465db52009-09-23 17:17:35 -07001596 ArmLIR *target = newLIR0(cUnit, kArmPseudoTargetLabel);
Ben Chengd7d426a2009-09-22 11:23:36 -07001597 target->defMask = ENCODE_ALL;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001598 branch1->generic.target = (LIR *)target;
1599 branch2->generic.target = (LIR *)target;
1600 break;
1601 }
1602 default:
1603 return true;
1604 }
1605 return false;
1606}
1607
1608static bool handleFmt11x(CompilationUnit *cUnit, MIR *mir)
1609{
1610 OpCode dalvikOpCode = mir->dalvikInsn.opCode;
Bill Buzbee1465db52009-09-23 17:17:35 -07001611 RegLocation rlResult;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001612 switch (dalvikOpCode) {
1613 case OP_MOVE_EXCEPTION: {
1614 int offset = offsetof(InterpState, self);
1615 int exOffset = offsetof(Thread, exception);
Bill Buzbeec6f10662010-02-09 11:16:15 -08001616 int selfReg = dvmCompilerAllocTemp(cUnit);
1617 int resetReg = dvmCompilerAllocTemp(cUnit);
1618 RegLocation rlDest = dvmCompilerGetDest(cUnit, mir, 0);
1619 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Bill Buzbee1465db52009-09-23 17:17:35 -07001620 loadWordDisp(cUnit, rGLUE, offset, selfReg);
Bill Buzbeef9f33282009-11-22 12:45:30 -08001621 loadConstant(cUnit, resetReg, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07001622 loadWordDisp(cUnit, selfReg, exOffset, rlResult.lowReg);
Bill Buzbeef9f33282009-11-22 12:45:30 -08001623 storeWordDisp(cUnit, selfReg, exOffset, resetReg);
Bill Buzbee1465db52009-09-23 17:17:35 -07001624 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001625 break;
1626 }
1627 case OP_MOVE_RESULT:
1628 case OP_MOVE_RESULT_OBJECT: {
Bill Buzbeec6f10662010-02-09 11:16:15 -08001629 RegLocation rlDest = dvmCompilerGetDest(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07001630 RegLocation rlSrc = LOC_DALVIK_RETURN_VAL;
1631 rlSrc.fp = rlDest.fp;
1632 storeValue(cUnit, rlDest, rlSrc);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001633 break;
1634 }
1635 case OP_MOVE_RESULT_WIDE: {
Bill Buzbeec6f10662010-02-09 11:16:15 -08001636 RegLocation rlDest = dvmCompilerGetDestWide(cUnit, mir, 0, 1);
Bill Buzbee1465db52009-09-23 17:17:35 -07001637 RegLocation rlSrc = LOC_DALVIK_RETURN_VAL_WIDE;
1638 rlSrc.fp = rlDest.fp;
1639 storeValueWide(cUnit, rlDest, rlSrc);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001640 break;
1641 }
1642 case OP_RETURN_WIDE: {
Bill Buzbeec6f10662010-02-09 11:16:15 -08001643 RegLocation rlSrc = dvmCompilerGetSrcWide(cUnit, mir, 0, 1);
Bill Buzbee1465db52009-09-23 17:17:35 -07001644 RegLocation rlDest = LOC_DALVIK_RETURN_VAL_WIDE;
1645 rlDest.fp = rlSrc.fp;
1646 storeValueWide(cUnit, rlDest, rlSrc);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001647 genReturnCommon(cUnit,mir);
1648 break;
1649 }
1650 case OP_RETURN:
1651 case OP_RETURN_OBJECT: {
Bill Buzbeec6f10662010-02-09 11:16:15 -08001652 RegLocation rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07001653 RegLocation rlDest = LOC_DALVIK_RETURN_VAL;
1654 rlDest.fp = rlSrc.fp;
1655 storeValue(cUnit, rlDest, rlSrc);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001656 genReturnCommon(cUnit,mir);
1657 break;
1658 }
Bill Buzbee1465db52009-09-23 17:17:35 -07001659 case OP_MONITOR_EXIT:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001660 case OP_MONITOR_ENTER:
Bill Buzbeed0937ef2009-12-22 16:15:39 -08001661#if defined(WITH_DEADLOCK_PREDICTION) || defined(WITH_MONITOR_TRACKING)
Ben Cheng5d90c202009-11-22 23:31:11 -08001662 genMonitorPortable(cUnit, mir);
Bill Buzbee1465db52009-09-23 17:17:35 -07001663#else
Ben Cheng5d90c202009-11-22 23:31:11 -08001664 genMonitor(cUnit, mir);
Bill Buzbee1465db52009-09-23 17:17:35 -07001665#endif
Ben Chengba4fc8b2009-06-01 13:00:29 -07001666 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001667 case OP_THROW: {
1668 genInterpSingleStep(cUnit, mir);
1669 break;
1670 }
1671 default:
1672 return true;
1673 }
1674 return false;
1675}
1676
Bill Buzbeed45ba372009-06-15 17:00:57 -07001677static bool handleFmt12x(CompilationUnit *cUnit, MIR *mir)
1678{
1679 OpCode opCode = mir->dalvikInsn.opCode;
Bill Buzbee1465db52009-09-23 17:17:35 -07001680 RegLocation rlDest;
1681 RegLocation rlSrc;
1682 RegLocation rlResult;
Bill Buzbeed45ba372009-06-15 17:00:57 -07001683
Ben Chengba4fc8b2009-06-01 13:00:29 -07001684 if ( (opCode >= OP_ADD_INT_2ADDR) && (opCode <= OP_REM_DOUBLE_2ADDR)) {
Ben Cheng5d90c202009-11-22 23:31:11 -08001685 return genArithOp( cUnit, mir );
Ben Chengba4fc8b2009-06-01 13:00:29 -07001686 }
1687
Bill Buzbee1465db52009-09-23 17:17:35 -07001688 if (mir->ssaRep->numUses == 2)
Bill Buzbeec6f10662010-02-09 11:16:15 -08001689 rlSrc = dvmCompilerGetSrcWide(cUnit, mir, 0, 1);
Bill Buzbee1465db52009-09-23 17:17:35 -07001690 else
Bill Buzbeec6f10662010-02-09 11:16:15 -08001691 rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07001692 if (mir->ssaRep->numDefs == 2)
Bill Buzbeec6f10662010-02-09 11:16:15 -08001693 rlDest = dvmCompilerGetDestWide(cUnit, mir, 0, 1);
Bill Buzbee1465db52009-09-23 17:17:35 -07001694 else
Bill Buzbeec6f10662010-02-09 11:16:15 -08001695 rlDest = dvmCompilerGetDest(cUnit, mir, 0);
Ben Chenge9695e52009-06-16 16:11:47 -07001696
Ben Chengba4fc8b2009-06-01 13:00:29 -07001697 switch (opCode) {
Bill Buzbee1465db52009-09-23 17:17:35 -07001698 case OP_DOUBLE_TO_INT:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001699 case OP_INT_TO_FLOAT:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001700 case OP_FLOAT_TO_INT:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001701 case OP_DOUBLE_TO_FLOAT:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001702 case OP_FLOAT_TO_DOUBLE:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001703 case OP_INT_TO_DOUBLE:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001704 case OP_FLOAT_TO_LONG:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001705 case OP_LONG_TO_FLOAT:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001706 case OP_DOUBLE_TO_LONG:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001707 case OP_LONG_TO_DOUBLE:
Ben Cheng5d90c202009-11-22 23:31:11 -08001708 return genConversion(cUnit, mir);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001709 case OP_NEG_INT:
1710 case OP_NOT_INT:
Ben Cheng5d90c202009-11-22 23:31:11 -08001711 return genArithOpInt(cUnit, mir, rlDest, rlSrc, rlSrc);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001712 case OP_NEG_LONG:
1713 case OP_NOT_LONG:
Ben Cheng5d90c202009-11-22 23:31:11 -08001714 return genArithOpLong(cUnit, mir, rlDest, rlSrc, rlSrc);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001715 case OP_NEG_FLOAT:
Ben Cheng5d90c202009-11-22 23:31:11 -08001716 return genArithOpFloat(cUnit, mir, rlDest, rlSrc, rlSrc);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001717 case OP_NEG_DOUBLE:
Ben Cheng5d90c202009-11-22 23:31:11 -08001718 return genArithOpDouble(cUnit, mir, rlDest, rlSrc, rlSrc);
Bill Buzbee1465db52009-09-23 17:17:35 -07001719 case OP_MOVE_WIDE:
1720 storeValueWide(cUnit, rlDest, rlSrc);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001721 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07001722 case OP_INT_TO_LONG:
Bill Buzbeec6f10662010-02-09 11:16:15 -08001723 rlSrc = dvmCompilerUpdateLoc(cUnit, rlSrc);
1724 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Bill Buzbee964a7b02010-01-28 12:54:19 -08001725 //TUNING: shouldn't loadValueDirect already check for phys reg?
Bill Buzbee1465db52009-09-23 17:17:35 -07001726 if (rlSrc.location == kLocPhysReg) {
1727 genRegCopy(cUnit, rlResult.lowReg, rlSrc.lowReg);
1728 } else {
1729 loadValueDirect(cUnit, rlSrc, rlResult.lowReg);
1730 }
1731 opRegRegImm(cUnit, kOpAsr, rlResult.highReg,
1732 rlResult.lowReg, 31);
1733 storeValueWide(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001734 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07001735 case OP_LONG_TO_INT:
Bill Buzbeec6f10662010-02-09 11:16:15 -08001736 rlSrc = dvmCompilerUpdateLocWide(cUnit, rlSrc);
1737 rlSrc = dvmCompilerWideToNarrow(cUnit, rlSrc);
Bill Buzbee1465db52009-09-23 17:17:35 -07001738 // Intentional fallthrough
Ben Chengba4fc8b2009-06-01 13:00:29 -07001739 case OP_MOVE:
1740 case OP_MOVE_OBJECT:
Bill Buzbee1465db52009-09-23 17:17:35 -07001741 storeValue(cUnit, rlDest, rlSrc);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001742 break;
1743 case OP_INT_TO_BYTE:
Bill Buzbee1465db52009-09-23 17:17:35 -07001744 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
Bill Buzbeec6f10662010-02-09 11:16:15 -08001745 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Bill Buzbee1465db52009-09-23 17:17:35 -07001746 opRegReg(cUnit, kOp2Byte, rlResult.lowReg, rlSrc.lowReg);
1747 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001748 break;
1749 case OP_INT_TO_SHORT:
Bill Buzbee1465db52009-09-23 17:17:35 -07001750 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
Bill Buzbeec6f10662010-02-09 11:16:15 -08001751 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Bill Buzbee1465db52009-09-23 17:17:35 -07001752 opRegReg(cUnit, kOp2Short, rlResult.lowReg, rlSrc.lowReg);
1753 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001754 break;
1755 case OP_INT_TO_CHAR:
Bill Buzbee1465db52009-09-23 17:17:35 -07001756 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
Bill Buzbeec6f10662010-02-09 11:16:15 -08001757 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Bill Buzbee1465db52009-09-23 17:17:35 -07001758 opRegReg(cUnit, kOp2Char, rlResult.lowReg, rlSrc.lowReg);
1759 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001760 break;
1761 case OP_ARRAY_LENGTH: {
1762 int lenOffset = offsetof(ArrayObject, length);
Bill Buzbee1465db52009-09-23 17:17:35 -07001763 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
1764 genNullCheck(cUnit, rlSrc.sRegLow, rlSrc.lowReg,
1765 mir->offset, NULL);
Bill Buzbeec6f10662010-02-09 11:16:15 -08001766 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Bill Buzbee1465db52009-09-23 17:17:35 -07001767 loadWordDisp(cUnit, rlSrc.lowReg, lenOffset,
1768 rlResult.lowReg);
1769 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001770 break;
1771 }
1772 default:
1773 return true;
1774 }
1775 return false;
1776}
1777
1778static bool handleFmt21s(CompilationUnit *cUnit, MIR *mir)
1779{
1780 OpCode dalvikOpCode = mir->dalvikInsn.opCode;
Bill Buzbee1465db52009-09-23 17:17:35 -07001781 RegLocation rlDest;
1782 RegLocation rlResult;
1783 int BBBB = mir->dalvikInsn.vB;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001784 if (dalvikOpCode == OP_CONST_WIDE_16) {
Bill Buzbeec6f10662010-02-09 11:16:15 -08001785 rlDest = dvmCompilerGetDestWide(cUnit, mir, 0, 1);
1786 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Ben Chengbd1326d2010-04-02 15:04:53 -07001787 loadConstantNoClobber(cUnit, rlResult.lowReg, BBBB);
Bill Buzbee964a7b02010-01-28 12:54:19 -08001788 //TUNING: do high separately to avoid load dependency
Bill Buzbee1465db52009-09-23 17:17:35 -07001789 opRegRegImm(cUnit, kOpAsr, rlResult.highReg, rlResult.lowReg, 31);
1790 storeValueWide(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001791 } else if (dalvikOpCode == OP_CONST_16) {
Bill Buzbeec6f10662010-02-09 11:16:15 -08001792 rlDest = dvmCompilerGetDest(cUnit, mir, 0);
1793 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kAnyReg, true);
Ben Chengbd1326d2010-04-02 15:04:53 -07001794 loadConstantNoClobber(cUnit, rlResult.lowReg, BBBB);
Bill Buzbee1465db52009-09-23 17:17:35 -07001795 storeValue(cUnit, rlDest, rlResult);
1796 } else
Ben Chengba4fc8b2009-06-01 13:00:29 -07001797 return true;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001798 return false;
1799}
1800
1801/* Compare agaist zero */
1802static bool handleFmt21t(CompilationUnit *cUnit, MIR *mir, BasicBlock *bb,
Bill Buzbee89efc3d2009-07-28 11:22:22 -07001803 ArmLIR *labelList)
Ben Chengba4fc8b2009-06-01 13:00:29 -07001804{
1805 OpCode dalvikOpCode = mir->dalvikInsn.opCode;
Bill Buzbee89efc3d2009-07-28 11:22:22 -07001806 ArmConditionCode cond;
Bill Buzbeec6f10662010-02-09 11:16:15 -08001807 RegLocation rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07001808 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
1809 opRegImm(cUnit, kOpCmp, rlSrc.lowReg, 0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001810
Bill Buzbee270c1d62009-08-13 16:58:07 -07001811//TUNING: break this out to allow use of Thumb2 CB[N]Z
Ben Chengba4fc8b2009-06-01 13:00:29 -07001812 switch (dalvikOpCode) {
1813 case OP_IF_EQZ:
Bill Buzbee1465db52009-09-23 17:17:35 -07001814 cond = kArmCondEq;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001815 break;
1816 case OP_IF_NEZ:
Bill Buzbee1465db52009-09-23 17:17:35 -07001817 cond = kArmCondNe;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001818 break;
1819 case OP_IF_LTZ:
Bill Buzbee1465db52009-09-23 17:17:35 -07001820 cond = kArmCondLt;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001821 break;
1822 case OP_IF_GEZ:
Bill Buzbee1465db52009-09-23 17:17:35 -07001823 cond = kArmCondGe;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001824 break;
1825 case OP_IF_GTZ:
Bill Buzbee1465db52009-09-23 17:17:35 -07001826 cond = kArmCondGt;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001827 break;
1828 case OP_IF_LEZ:
Bill Buzbee1465db52009-09-23 17:17:35 -07001829 cond = kArmCondLe;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001830 break;
1831 default:
1832 cond = 0;
1833 LOGE("Unexpected opcode (%d) for Fmt21t\n", dalvikOpCode);
Bill Buzbeefc519dc2010-03-06 23:30:57 -08001834 dvmCompilerAbort(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001835 }
1836 genConditionalBranch(cUnit, cond, &labelList[bb->taken->id]);
1837 /* This mostly likely will be optimized away in a later phase */
1838 genUnconditionalBranch(cUnit, &labelList[bb->fallThrough->id]);
1839 return false;
1840}
1841
Elliott Hughesb4c05972010-02-24 16:36:18 -08001842static bool isPowerOfTwo(int x)
1843{
1844 return (x & (x - 1)) == 0;
1845}
1846
1847// Returns true if no more than two bits are set in 'x'.
1848static bool isPopCountLE2(unsigned int x)
1849{
1850 x &= x - 1;
1851 return (x & (x - 1)) == 0;
1852}
1853
1854// Returns the index of the lowest set bit in 'x'.
1855static int lowestSetBit(unsigned int x) {
1856 int bit_posn = 0;
1857 while ((x & 0xf) == 0) {
1858 bit_posn += 4;
1859 x >>= 4;
Bill Buzbee78cb0e22010-02-11 14:04:53 -08001860 }
Elliott Hughesb4c05972010-02-24 16:36:18 -08001861 while ((x & 1) == 0) {
1862 bit_posn++;
1863 x >>= 1;
1864 }
1865 return bit_posn;
1866}
1867
Elliott Hughes672511b2010-04-26 17:40:13 -07001868// Returns true if it added instructions to 'cUnit' to divide 'rlSrc' by 'lit'
1869// and store the result in 'rlDest'.
Elliott Hughesc7ad9b22010-04-28 13:52:02 -07001870static bool handleEasyDivide(CompilationUnit *cUnit, OpCode dalvikOpCode,
Elliott Hughes672511b2010-04-26 17:40:13 -07001871 RegLocation rlSrc, RegLocation rlDest, int lit)
1872{
1873 if (lit < 2 || !isPowerOfTwo(lit)) {
1874 return false;
1875 }
1876 int k = lowestSetBit(lit);
1877 if (k >= 30) {
1878 // Avoid special cases.
1879 return false;
1880 }
Elliott Hughes9c457022010-04-28 16:15:38 -07001881 bool div = (dalvikOpCode == OP_DIV_INT_LIT8 || dalvikOpCode == OP_DIV_INT_LIT16);
Elliott Hughes672511b2010-04-26 17:40:13 -07001882 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
1883 RegLocation rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Elliott Hughes9c457022010-04-28 16:15:38 -07001884 if (div) {
1885 int tReg = dvmCompilerAllocTemp(cUnit);
1886 if (lit == 2) {
1887 // Division by 2 is by far the most common division by constant.
1888 opRegRegImm(cUnit, kOpLsr, tReg, rlSrc.lowReg, 32 - k);
1889 opRegRegReg(cUnit, kOpAdd, tReg, tReg, rlSrc.lowReg);
1890 opRegRegImm(cUnit, kOpAsr, rlResult.lowReg, tReg, k);
1891 } else {
1892 opRegRegImm(cUnit, kOpAsr, tReg, rlSrc.lowReg, 31);
1893 opRegRegImm(cUnit, kOpLsr, tReg, tReg, 32 - k);
1894 opRegRegReg(cUnit, kOpAdd, tReg, tReg, rlSrc.lowReg);
1895 opRegRegImm(cUnit, kOpAsr, rlResult.lowReg, tReg, k);
1896 }
Elliott Hughes672511b2010-04-26 17:40:13 -07001897 } else {
Elliott Hughes9c457022010-04-28 16:15:38 -07001898 int cReg = dvmCompilerAllocTemp(cUnit);
1899 loadConstant(cUnit, cReg, lit - 1);
1900 int tReg1 = dvmCompilerAllocTemp(cUnit);
1901 int tReg2 = dvmCompilerAllocTemp(cUnit);
1902 if (lit == 2) {
1903 opRegRegImm(cUnit, kOpLsr, tReg1, rlSrc.lowReg, 32 - k);
1904 opRegRegReg(cUnit, kOpAdd, tReg2, tReg1, rlSrc.lowReg);
1905 opRegRegReg(cUnit, kOpAnd, tReg2, tReg2, cReg);
1906 opRegRegReg(cUnit, kOpSub, rlResult.lowReg, tReg2, tReg1);
1907 } else {
1908 opRegRegImm(cUnit, kOpAsr, tReg1, rlSrc.lowReg, 31);
1909 opRegRegImm(cUnit, kOpLsr, tReg1, tReg1, 32 - k);
1910 opRegRegReg(cUnit, kOpAdd, tReg2, tReg1, rlSrc.lowReg);
1911 opRegRegReg(cUnit, kOpAnd, tReg2, tReg2, cReg);
1912 opRegRegReg(cUnit, kOpSub, rlResult.lowReg, tReg2, tReg1);
1913 }
Elliott Hughes672511b2010-04-26 17:40:13 -07001914 }
1915 storeValue(cUnit, rlDest, rlResult);
1916 return true;
1917}
1918
Elliott Hughesb4c05972010-02-24 16:36:18 -08001919// Returns true if it added instructions to 'cUnit' to multiply 'rlSrc' by 'lit'
1920// and store the result in 'rlDest'.
1921static bool handleEasyMultiply(CompilationUnit *cUnit,
1922 RegLocation rlSrc, RegLocation rlDest, int lit)
1923{
1924 // Can we simplify this multiplication?
1925 bool powerOfTwo = false;
1926 bool popCountLE2 = false;
1927 bool powerOfTwoMinusOne = false;
1928 if (lit < 2) {
1929 // Avoid special cases.
1930 return false;
1931 } else if (isPowerOfTwo(lit)) {
1932 powerOfTwo = true;
1933 } else if (isPopCountLE2(lit)) {
1934 popCountLE2 = true;
1935 } else if (isPowerOfTwo(lit + 1)) {
1936 powerOfTwoMinusOne = true;
1937 } else {
1938 return false;
1939 }
1940 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
1941 RegLocation rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
1942 if (powerOfTwo) {
1943 // Shift.
1944 opRegRegImm(cUnit, kOpLsl, rlResult.lowReg, rlSrc.lowReg,
1945 lowestSetBit(lit));
1946 } else if (popCountLE2) {
1947 // Shift and add and shift.
1948 int firstBit = lowestSetBit(lit);
1949 int secondBit = lowestSetBit(lit ^ (1 << firstBit));
1950 genMultiplyByTwoBitMultiplier(cUnit, rlSrc, rlResult, lit,
1951 firstBit, secondBit);
1952 } else {
1953 // Reverse subtract: (src << (shift + 1)) - src.
1954 assert(powerOfTwoMinusOne);
1955 // TODO: rsb dst, src, src lsl#lowestSetBit(lit + 1)
1956 int tReg = dvmCompilerAllocTemp(cUnit);
1957 opRegRegImm(cUnit, kOpLsl, tReg, rlSrc.lowReg, lowestSetBit(lit + 1));
1958 opRegRegReg(cUnit, kOpSub, rlResult.lowReg, tReg, rlSrc.lowReg);
1959 }
1960 storeValue(cUnit, rlDest, rlResult);
1961 return true;
Bill Buzbee78cb0e22010-02-11 14:04:53 -08001962}
1963
Ben Chengba4fc8b2009-06-01 13:00:29 -07001964static bool handleFmt22b_Fmt22s(CompilationUnit *cUnit, MIR *mir)
1965{
1966 OpCode dalvikOpCode = mir->dalvikInsn.opCode;
Bill Buzbeec6f10662010-02-09 11:16:15 -08001967 RegLocation rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
1968 RegLocation rlDest = dvmCompilerGetDest(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07001969 RegLocation rlResult;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001970 int lit = mir->dalvikInsn.vC;
Ben Cheng4f489172009-09-27 17:08:35 -07001971 OpKind op = 0; /* Make gcc happy */
Bill Buzbee1465db52009-09-23 17:17:35 -07001972 int shiftOp = false;
1973 bool isDiv = false;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001974
Ben Chengba4fc8b2009-06-01 13:00:29 -07001975 switch (dalvikOpCode) {
Bill Buzbee1465db52009-09-23 17:17:35 -07001976 case OP_RSUB_INT_LIT8:
1977 case OP_RSUB_INT: {
1978 int tReg;
1979 //TUNING: add support for use of Arm rsub op
1980 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
Bill Buzbeec6f10662010-02-09 11:16:15 -08001981 tReg = dvmCompilerAllocTemp(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07001982 loadConstant(cUnit, tReg, lit);
Bill Buzbeec6f10662010-02-09 11:16:15 -08001983 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Bill Buzbee1465db52009-09-23 17:17:35 -07001984 opRegRegReg(cUnit, kOpSub, rlResult.lowReg,
1985 tReg, rlSrc.lowReg);
1986 storeValue(cUnit, rlDest, rlResult);
1987 return false;
1988 break;
1989 }
1990
Ben Chengba4fc8b2009-06-01 13:00:29 -07001991 case OP_ADD_INT_LIT8:
1992 case OP_ADD_INT_LIT16:
Bill Buzbee1465db52009-09-23 17:17:35 -07001993 op = kOpAdd;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001994 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001995 case OP_MUL_INT_LIT8:
Bill Buzbee78cb0e22010-02-11 14:04:53 -08001996 case OP_MUL_INT_LIT16: {
Elliott Hughesb4c05972010-02-24 16:36:18 -08001997 if (handleEasyMultiply(cUnit, rlSrc, rlDest, lit)) {
1998 return false;
Bill Buzbee78cb0e22010-02-11 14:04:53 -08001999 }
Elliott Hughesb4c05972010-02-24 16:36:18 -08002000 op = kOpMul;
Bill Buzbee1465db52009-09-23 17:17:35 -07002001 break;
Bill Buzbee78cb0e22010-02-11 14:04:53 -08002002 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07002003 case OP_AND_INT_LIT8:
2004 case OP_AND_INT_LIT16:
Bill Buzbee1465db52009-09-23 17:17:35 -07002005 op = kOpAnd;
2006 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002007 case OP_OR_INT_LIT8:
2008 case OP_OR_INT_LIT16:
Bill Buzbee1465db52009-09-23 17:17:35 -07002009 op = kOpOr;
2010 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002011 case OP_XOR_INT_LIT8:
2012 case OP_XOR_INT_LIT16:
Bill Buzbee1465db52009-09-23 17:17:35 -07002013 op = kOpXor;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002014 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002015 case OP_SHL_INT_LIT8:
Bill Buzbee0e605272009-12-01 14:28:05 -08002016 lit &= 31;
Bill Buzbee1465db52009-09-23 17:17:35 -07002017 shiftOp = true;
2018 op = kOpLsl;
2019 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002020 case OP_SHR_INT_LIT8:
Bill Buzbee0e605272009-12-01 14:28:05 -08002021 lit &= 31;
Bill Buzbee1465db52009-09-23 17:17:35 -07002022 shiftOp = true;
2023 op = kOpAsr;
2024 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002025 case OP_USHR_INT_LIT8:
Bill Buzbee0e605272009-12-01 14:28:05 -08002026 lit &= 31;
Bill Buzbee1465db52009-09-23 17:17:35 -07002027 shiftOp = true;
2028 op = kOpLsr;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002029 break;
2030
2031 case OP_DIV_INT_LIT8:
2032 case OP_DIV_INT_LIT16:
Ben Chengba4fc8b2009-06-01 13:00:29 -07002033 case OP_REM_INT_LIT8:
2034 case OP_REM_INT_LIT16:
2035 if (lit == 0) {
2036 /* Let the interpreter deal with div by 0 */
2037 genInterpSingleStep(cUnit, mir);
2038 return false;
2039 }
Elliott Hughesc7ad9b22010-04-28 13:52:02 -07002040 if (handleEasyDivide(cUnit, dalvikOpCode, rlSrc, rlDest, lit)) {
Elliott Hughes672511b2010-04-26 17:40:13 -07002041 return false;
2042 }
Bill Buzbeec6f10662010-02-09 11:16:15 -08002043 dvmCompilerFlushAllRegs(cUnit); /* Everything to home location */
Bill Buzbee1465db52009-09-23 17:17:35 -07002044 loadValueDirectFixed(cUnit, rlSrc, r0);
Bill Buzbeec6f10662010-02-09 11:16:15 -08002045 dvmCompilerClobber(cUnit, r0);
Bill Buzbee1465db52009-09-23 17:17:35 -07002046 if ((dalvikOpCode == OP_DIV_INT_LIT8) ||
2047 (dalvikOpCode == OP_DIV_INT_LIT16)) {
Ben Chengbd1326d2010-04-02 15:04:53 -07002048 LOAD_FUNC_ADDR(cUnit, r2, (int)__aeabi_idiv);
Bill Buzbee1465db52009-09-23 17:17:35 -07002049 isDiv = true;
2050 } else {
Ben Chengbd1326d2010-04-02 15:04:53 -07002051 LOAD_FUNC_ADDR(cUnit, r2, (int)__aeabi_idivmod);
Bill Buzbee1465db52009-09-23 17:17:35 -07002052 isDiv = false;
2053 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07002054 loadConstant(cUnit, r1, lit);
Bill Buzbee1465db52009-09-23 17:17:35 -07002055 opReg(cUnit, kOpBlx, r2);
Elliott Hughes6a555132010-02-25 15:41:42 -08002056 dvmCompilerClobberCallRegs(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07002057 if (isDiv)
Bill Buzbeec6f10662010-02-09 11:16:15 -08002058 rlResult = dvmCompilerGetReturn(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07002059 else
Bill Buzbeec6f10662010-02-09 11:16:15 -08002060 rlResult = dvmCompilerGetReturnAlt(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07002061 storeValue(cUnit, rlDest, rlResult);
2062 return false;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002063 break;
2064 default:
2065 return true;
2066 }
Bill Buzbee1465db52009-09-23 17:17:35 -07002067 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
Bill Buzbeec6f10662010-02-09 11:16:15 -08002068 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Bill Buzbee1465db52009-09-23 17:17:35 -07002069 // Avoid shifts by literal 0 - no support in Thumb. Change to copy
2070 if (shiftOp && (lit == 0)) {
2071 genRegCopy(cUnit, rlResult.lowReg, rlSrc.lowReg);
2072 } else {
2073 opRegRegImm(cUnit, op, rlResult.lowReg, rlSrc.lowReg, lit);
2074 }
2075 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002076 return false;
2077}
2078
2079static bool handleFmt22c(CompilationUnit *cUnit, MIR *mir)
2080{
2081 OpCode dalvikOpCode = mir->dalvikInsn.opCode;
2082 int fieldOffset;
2083
2084 if (dalvikOpCode >= OP_IGET && dalvikOpCode <= OP_IPUT_SHORT) {
2085 InstField *pInstField = (InstField *)
2086 cUnit->method->clazz->pDvmDex->pResFields[mir->dalvikInsn.vC];
Ben Chengba4fc8b2009-06-01 13:00:29 -07002087
Ben Chengdd6e8702010-05-07 13:05:47 -07002088 if (pInstField == NULL) {
2089 LOGE("Unexpected null instance field");
2090 dvmAbort();
2091 }
2092
Ben Chengba4fc8b2009-06-01 13:00:29 -07002093 fieldOffset = pInstField->byteOffset;
2094 } else {
Ben Chenga0e7b602009-10-13 23:09:01 -07002095 /* Deliberately break the code while make the compiler happy */
2096 fieldOffset = -1;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002097 }
2098 switch (dalvikOpCode) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07002099 case OP_NEW_ARRAY: {
Bill Buzbee1465db52009-09-23 17:17:35 -07002100 // Generates a call - use explicit registers
Bill Buzbeec6f10662010-02-09 11:16:15 -08002101 RegLocation rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
2102 RegLocation rlDest = dvmCompilerGetDest(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07002103 RegLocation rlResult;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002104 void *classPtr = (void*)
2105 (cUnit->method->clazz->pDvmDex->pResClasses[mir->dalvikInsn.vC]);
Ben Chengdd6e8702010-05-07 13:05:47 -07002106
2107 if (classPtr == NULL) {
2108 LOGE("Unexpected null class");
2109 dvmAbort();
2110 }
2111
Bill Buzbeec6f10662010-02-09 11:16:15 -08002112 dvmCompilerFlushAllRegs(cUnit); /* Everything to home location */
Bill Buzbee1465db52009-09-23 17:17:35 -07002113 genExportPC(cUnit, mir);
2114 loadValueDirectFixed(cUnit, rlSrc, r1); /* Len */
Ben Chengba4fc8b2009-06-01 13:00:29 -07002115 loadConstant(cUnit, r0, (int) classPtr );
Ben Chengbd1326d2010-04-02 15:04:53 -07002116 LOAD_FUNC_ADDR(cUnit, r3, (int)dvmAllocArrayByClass);
Ben Cheng4f489172009-09-27 17:08:35 -07002117 /*
2118 * "len < 0": bail to the interpreter to re-execute the
2119 * instruction
2120 */
Carl Shapiroe3c01da2010-05-20 22:54:18 -07002121 genRegImmCheck(cUnit, kArmCondMi, r1, 0, mir->offset, NULL);
Bill Buzbee270c1d62009-08-13 16:58:07 -07002122 loadConstant(cUnit, r2, ALLOC_DONT_TRACK);
Bill Buzbee1465db52009-09-23 17:17:35 -07002123 opReg(cUnit, kOpBlx, r3);
Elliott Hughes6a555132010-02-25 15:41:42 -08002124 dvmCompilerClobberCallRegs(cUnit);
Ben Cheng4f489172009-09-27 17:08:35 -07002125 /* generate a branch over if allocation is successful */
Bill Buzbee1465db52009-09-23 17:17:35 -07002126 opRegImm(cUnit, kOpCmp, r0, 0); /* NULL? */
2127 ArmLIR *branchOver = opCondBranch(cUnit, kArmCondNe);
Ben Cheng4f489172009-09-27 17:08:35 -07002128 /*
2129 * OOM exception needs to be thrown here and cannot re-execute
2130 */
2131 loadConstant(cUnit, r0,
2132 (int) (cUnit->method->insns + mir->offset));
2133 genDispatchToHandler(cUnit, TEMPLATE_THROW_EXCEPTION_COMMON);
2134 /* noreturn */
2135
Bill Buzbee1465db52009-09-23 17:17:35 -07002136 ArmLIR *target = newLIR0(cUnit, kArmPseudoTargetLabel);
Ben Cheng4f489172009-09-27 17:08:35 -07002137 target->defMask = ENCODE_ALL;
2138 branchOver->generic.target = (LIR *) target;
Bill Buzbeec6f10662010-02-09 11:16:15 -08002139 rlResult = dvmCompilerGetReturn(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07002140 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002141 break;
2142 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07002143 case OP_INSTANCE_OF: {
Bill Buzbee1465db52009-09-23 17:17:35 -07002144 // May generate a call - use explicit registers
Bill Buzbeec6f10662010-02-09 11:16:15 -08002145 RegLocation rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
2146 RegLocation rlDest = dvmCompilerGetDest(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07002147 RegLocation rlResult;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002148 ClassObject *classPtr =
2149 (cUnit->method->clazz->pDvmDex->pResClasses[mir->dalvikInsn.vC]);
Bill Buzbee480e6782010-01-27 15:43:08 -08002150 /*
2151 * Note: It is possible that classPtr is NULL at this point,
2152 * even though this instruction has been successfully interpreted.
2153 * If the previous interpretation had a null source, the
2154 * interpreter would not have bothered to resolve the clazz.
2155 * Bail out to the interpreter in this case, and log it
2156 * so that we can tell if it happens frequently.
2157 */
2158 if (classPtr == NULL) {
2159 LOGD("null clazz in OP_INSTANCE_OF, single-stepping");
2160 genInterpSingleStep(cUnit, mir);
2161 break;
2162 }
Bill Buzbeec6f10662010-02-09 11:16:15 -08002163 dvmCompilerFlushAllRegs(cUnit); /* Everything to home location */
Bill Buzbee1465db52009-09-23 17:17:35 -07002164 loadValueDirectFixed(cUnit, rlSrc, r0); /* Ref */
Ben Chengba4fc8b2009-06-01 13:00:29 -07002165 loadConstant(cUnit, r2, (int) classPtr );
Bill Buzbee270c1d62009-08-13 16:58:07 -07002166//TUNING: compare to 0 primative to allow use of CB[N]Z
Bill Buzbee1465db52009-09-23 17:17:35 -07002167 opRegImm(cUnit, kOpCmp, r0, 0); /* NULL? */
Ben Cheng752c7942009-06-22 10:50:07 -07002168 /* When taken r0 has NULL which can be used for store directly */
Bill Buzbee1465db52009-09-23 17:17:35 -07002169 ArmLIR *branch1 = opCondBranch(cUnit, kArmCondEq);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002170 /* r1 now contains object->clazz */
Bill Buzbee270c1d62009-08-13 16:58:07 -07002171 loadWordDisp(cUnit, r0, offsetof(Object, clazz), r1);
Bill Buzbee1465db52009-09-23 17:17:35 -07002172 /* r1 now contains object->clazz */
Ben Chengbd1326d2010-04-02 15:04:53 -07002173 LOAD_FUNC_ADDR(cUnit, r3, (int)dvmInstanceofNonTrivial);
Ben Cheng752c7942009-06-22 10:50:07 -07002174 loadConstant(cUnit, r0, 1); /* Assume true */
Bill Buzbee1465db52009-09-23 17:17:35 -07002175 opRegReg(cUnit, kOpCmp, r1, r2);
2176 ArmLIR *branch2 = opCondBranch(cUnit, kArmCondEq);
2177 genRegCopy(cUnit, r0, r1);
2178 genRegCopy(cUnit, r1, r2);
2179 opReg(cUnit, kOpBlx, r3);
Elliott Hughes6a555132010-02-25 15:41:42 -08002180 dvmCompilerClobberCallRegs(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002181 /* branch target here */
Bill Buzbee1465db52009-09-23 17:17:35 -07002182 ArmLIR *target = newLIR0(cUnit, kArmPseudoTargetLabel);
Ben Chengd7d426a2009-09-22 11:23:36 -07002183 target->defMask = ENCODE_ALL;
Bill Buzbeec6f10662010-02-09 11:16:15 -08002184 rlResult = dvmCompilerGetReturn(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07002185 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002186 branch1->generic.target = (LIR *)target;
2187 branch2->generic.target = (LIR *)target;
2188 break;
2189 }
2190 case OP_IGET_WIDE:
2191 genIGetWide(cUnit, mir, fieldOffset);
2192 break;
2193 case OP_IGET:
2194 case OP_IGET_OBJECT:
Bill Buzbee1465db52009-09-23 17:17:35 -07002195 genIGet(cUnit, mir, kWord, fieldOffset);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002196 break;
2197 case OP_IGET_BOOLEAN:
Bill Buzbee1465db52009-09-23 17:17:35 -07002198 genIGet(cUnit, mir, kUnsignedByte, fieldOffset);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002199 break;
2200 case OP_IGET_BYTE:
Bill Buzbee1465db52009-09-23 17:17:35 -07002201 genIGet(cUnit, mir, kSignedByte, fieldOffset);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002202 break;
2203 case OP_IGET_CHAR:
Bill Buzbee1465db52009-09-23 17:17:35 -07002204 genIGet(cUnit, mir, kUnsignedHalf, fieldOffset);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002205 break;
2206 case OP_IGET_SHORT:
Bill Buzbee1465db52009-09-23 17:17:35 -07002207 genIGet(cUnit, mir, kSignedHalf, fieldOffset);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002208 break;
2209 case OP_IPUT_WIDE:
2210 genIPutWide(cUnit, mir, fieldOffset);
2211 break;
2212 case OP_IPUT:
2213 case OP_IPUT_OBJECT:
Bill Buzbee1465db52009-09-23 17:17:35 -07002214 genIPut(cUnit, mir, kWord, fieldOffset);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002215 break;
2216 case OP_IPUT_SHORT:
2217 case OP_IPUT_CHAR:
Bill Buzbee1465db52009-09-23 17:17:35 -07002218 genIPut(cUnit, mir, kUnsignedHalf, fieldOffset);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002219 break;
2220 case OP_IPUT_BYTE:
2221 case OP_IPUT_BOOLEAN:
Bill Buzbee1465db52009-09-23 17:17:35 -07002222 genIPut(cUnit, mir, kUnsignedByte, fieldOffset);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002223 break;
Bill Buzbeeb16344a2010-03-15 17:19:12 -07002224 case OP_IGET_WIDE_VOLATILE:
2225 case OP_IPUT_WIDE_VOLATILE:
2226 case OP_SGET_WIDE_VOLATILE:
2227 case OP_SPUT_WIDE_VOLATILE:
2228 genInterpSingleStep(cUnit, mir);
2229 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002230 default:
2231 return true;
2232 }
2233 return false;
2234}
2235
2236static bool handleFmt22cs(CompilationUnit *cUnit, MIR *mir)
2237{
2238 OpCode dalvikOpCode = mir->dalvikInsn.opCode;
2239 int fieldOffset = mir->dalvikInsn.vC;
2240 switch (dalvikOpCode) {
2241 case OP_IGET_QUICK:
2242 case OP_IGET_OBJECT_QUICK:
Bill Buzbee1465db52009-09-23 17:17:35 -07002243 genIGet(cUnit, mir, kWord, fieldOffset);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002244 break;
2245 case OP_IPUT_QUICK:
2246 case OP_IPUT_OBJECT_QUICK:
Bill Buzbee1465db52009-09-23 17:17:35 -07002247 genIPut(cUnit, mir, kWord, fieldOffset);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002248 break;
2249 case OP_IGET_WIDE_QUICK:
2250 genIGetWide(cUnit, mir, fieldOffset);
2251 break;
2252 case OP_IPUT_WIDE_QUICK:
2253 genIPutWide(cUnit, mir, fieldOffset);
2254 break;
2255 default:
2256 return true;
2257 }
2258 return false;
2259
2260}
2261
2262/* Compare agaist zero */
2263static bool handleFmt22t(CompilationUnit *cUnit, MIR *mir, BasicBlock *bb,
Bill Buzbee89efc3d2009-07-28 11:22:22 -07002264 ArmLIR *labelList)
Ben Chengba4fc8b2009-06-01 13:00:29 -07002265{
2266 OpCode dalvikOpCode = mir->dalvikInsn.opCode;
Bill Buzbee89efc3d2009-07-28 11:22:22 -07002267 ArmConditionCode cond;
Bill Buzbeec6f10662010-02-09 11:16:15 -08002268 RegLocation rlSrc1 = dvmCompilerGetSrc(cUnit, mir, 0);
2269 RegLocation rlSrc2 = dvmCompilerGetSrc(cUnit, mir, 1);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002270
Bill Buzbee1465db52009-09-23 17:17:35 -07002271 rlSrc1 = loadValue(cUnit, rlSrc1, kCoreReg);
2272 rlSrc2 = loadValue(cUnit, rlSrc2, kCoreReg);
2273 opRegReg(cUnit, kOpCmp, rlSrc1.lowReg, rlSrc2.lowReg);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002274
2275 switch (dalvikOpCode) {
2276 case OP_IF_EQ:
Bill Buzbee1465db52009-09-23 17:17:35 -07002277 cond = kArmCondEq;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002278 break;
2279 case OP_IF_NE:
Bill Buzbee1465db52009-09-23 17:17:35 -07002280 cond = kArmCondNe;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002281 break;
2282 case OP_IF_LT:
Bill Buzbee1465db52009-09-23 17:17:35 -07002283 cond = kArmCondLt;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002284 break;
2285 case OP_IF_GE:
Bill Buzbee1465db52009-09-23 17:17:35 -07002286 cond = kArmCondGe;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002287 break;
2288 case OP_IF_GT:
Bill Buzbee1465db52009-09-23 17:17:35 -07002289 cond = kArmCondGt;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002290 break;
2291 case OP_IF_LE:
Bill Buzbee1465db52009-09-23 17:17:35 -07002292 cond = kArmCondLe;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002293 break;
2294 default:
2295 cond = 0;
2296 LOGE("Unexpected opcode (%d) for Fmt22t\n", dalvikOpCode);
Bill Buzbeefc519dc2010-03-06 23:30:57 -08002297 dvmCompilerAbort(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002298 }
2299 genConditionalBranch(cUnit, cond, &labelList[bb->taken->id]);
2300 /* This mostly likely will be optimized away in a later phase */
2301 genUnconditionalBranch(cUnit, &labelList[bb->fallThrough->id]);
2302 return false;
2303}
2304
2305static bool handleFmt22x_Fmt32x(CompilationUnit *cUnit, MIR *mir)
2306{
2307 OpCode opCode = mir->dalvikInsn.opCode;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002308
2309 switch (opCode) {
2310 case OP_MOVE_16:
2311 case OP_MOVE_OBJECT_16:
2312 case OP_MOVE_FROM16:
Ben Chenge9695e52009-06-16 16:11:47 -07002313 case OP_MOVE_OBJECT_FROM16: {
Bill Buzbeec6f10662010-02-09 11:16:15 -08002314 storeValue(cUnit, dvmCompilerGetDest(cUnit, mir, 0),
2315 dvmCompilerGetSrc(cUnit, mir, 0));
Ben Chengba4fc8b2009-06-01 13:00:29 -07002316 break;
Ben Chenge9695e52009-06-16 16:11:47 -07002317 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07002318 case OP_MOVE_WIDE_16:
Ben Chenge9695e52009-06-16 16:11:47 -07002319 case OP_MOVE_WIDE_FROM16: {
Bill Buzbeec6f10662010-02-09 11:16:15 -08002320 storeValueWide(cUnit, dvmCompilerGetDestWide(cUnit, mir, 0, 1),
2321 dvmCompilerGetSrcWide(cUnit, mir, 0, 1));
Ben Chengba4fc8b2009-06-01 13:00:29 -07002322 break;
Ben Chenge9695e52009-06-16 16:11:47 -07002323 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07002324 default:
2325 return true;
2326 }
2327 return false;
2328}
2329
2330static bool handleFmt23x(CompilationUnit *cUnit, MIR *mir)
2331{
2332 OpCode opCode = mir->dalvikInsn.opCode;
Bill Buzbee1465db52009-09-23 17:17:35 -07002333 RegLocation rlSrc1;
2334 RegLocation rlSrc2;
2335 RegLocation rlDest;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002336
2337 if ( (opCode >= OP_ADD_INT) && (opCode <= OP_REM_DOUBLE)) {
Ben Cheng5d90c202009-11-22 23:31:11 -08002338 return genArithOp( cUnit, mir );
Ben Chengba4fc8b2009-06-01 13:00:29 -07002339 }
2340
Bill Buzbee1465db52009-09-23 17:17:35 -07002341 /* APUTs have 3 sources and no targets */
2342 if (mir->ssaRep->numDefs == 0) {
2343 if (mir->ssaRep->numUses == 3) {
Bill Buzbeec6f10662010-02-09 11:16:15 -08002344 rlDest = dvmCompilerGetSrc(cUnit, mir, 0);
2345 rlSrc1 = dvmCompilerGetSrc(cUnit, mir, 1);
2346 rlSrc2 = dvmCompilerGetSrc(cUnit, mir, 2);
Bill Buzbee1465db52009-09-23 17:17:35 -07002347 } else {
2348 assert(mir->ssaRep->numUses == 4);
Bill Buzbeec6f10662010-02-09 11:16:15 -08002349 rlDest = dvmCompilerGetSrcWide(cUnit, mir, 0, 1);
2350 rlSrc1 = dvmCompilerGetSrc(cUnit, mir, 2);
2351 rlSrc2 = dvmCompilerGetSrc(cUnit, mir, 3);
Bill Buzbee1465db52009-09-23 17:17:35 -07002352 }
2353 } else {
2354 /* Two sources and 1 dest. Deduce the operand sizes */
2355 if (mir->ssaRep->numUses == 4) {
Bill Buzbeec6f10662010-02-09 11:16:15 -08002356 rlSrc1 = dvmCompilerGetSrcWide(cUnit, mir, 0, 1);
2357 rlSrc2 = dvmCompilerGetSrcWide(cUnit, mir, 2, 3);
Bill Buzbee1465db52009-09-23 17:17:35 -07002358 } else {
2359 assert(mir->ssaRep->numUses == 2);
Bill Buzbeec6f10662010-02-09 11:16:15 -08002360 rlSrc1 = dvmCompilerGetSrc(cUnit, mir, 0);
2361 rlSrc2 = dvmCompilerGetSrc(cUnit, mir, 1);
Bill Buzbee1465db52009-09-23 17:17:35 -07002362 }
2363 if (mir->ssaRep->numDefs == 2) {
Bill Buzbeec6f10662010-02-09 11:16:15 -08002364 rlDest = dvmCompilerGetDestWide(cUnit, mir, 0, 1);
Bill Buzbee1465db52009-09-23 17:17:35 -07002365 } else {
2366 assert(mir->ssaRep->numDefs == 1);
Bill Buzbeec6f10662010-02-09 11:16:15 -08002367 rlDest = dvmCompilerGetDest(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07002368 }
2369 }
2370
2371
Ben Chengba4fc8b2009-06-01 13:00:29 -07002372 switch (opCode) {
Bill Buzbeed45ba372009-06-15 17:00:57 -07002373 case OP_CMPL_FLOAT:
2374 case OP_CMPG_FLOAT:
2375 case OP_CMPL_DOUBLE:
2376 case OP_CMPG_DOUBLE:
Ben Cheng5d90c202009-11-22 23:31:11 -08002377 return genCmpFP(cUnit, mir, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002378 case OP_CMP_LONG:
Bill Buzbee1465db52009-09-23 17:17:35 -07002379 genCmpLong(cUnit, mir, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002380 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002381 case OP_AGET_WIDE:
Bill Buzbee1465db52009-09-23 17:17:35 -07002382 genArrayGet(cUnit, mir, kLong, rlSrc1, rlSrc2, rlDest, 3);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002383 break;
2384 case OP_AGET:
2385 case OP_AGET_OBJECT:
Bill Buzbee1465db52009-09-23 17:17:35 -07002386 genArrayGet(cUnit, mir, kWord, rlSrc1, rlSrc2, rlDest, 2);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002387 break;
2388 case OP_AGET_BOOLEAN:
Bill Buzbee1465db52009-09-23 17:17:35 -07002389 genArrayGet(cUnit, mir, kUnsignedByte, rlSrc1, rlSrc2, rlDest, 0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002390 break;
2391 case OP_AGET_BYTE:
Bill Buzbee1465db52009-09-23 17:17:35 -07002392 genArrayGet(cUnit, mir, kSignedByte, rlSrc1, rlSrc2, rlDest, 0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002393 break;
2394 case OP_AGET_CHAR:
Bill Buzbee1465db52009-09-23 17:17:35 -07002395 genArrayGet(cUnit, mir, kUnsignedHalf, rlSrc1, rlSrc2, rlDest, 1);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002396 break;
2397 case OP_AGET_SHORT:
Bill Buzbee1465db52009-09-23 17:17:35 -07002398 genArrayGet(cUnit, mir, kSignedHalf, rlSrc1, rlSrc2, rlDest, 1);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002399 break;
2400 case OP_APUT_WIDE:
Bill Buzbee1465db52009-09-23 17:17:35 -07002401 genArrayPut(cUnit, mir, kLong, rlSrc1, rlSrc2, rlDest, 3);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002402 break;
2403 case OP_APUT:
Bill Buzbee1465db52009-09-23 17:17:35 -07002404 genArrayPut(cUnit, mir, kWord, rlSrc1, rlSrc2, rlDest, 2);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002405 break;
Bill Buzbeebe6534f2010-03-12 16:01:35 -08002406 case OP_APUT_OBJECT:
2407 genArrayObjectPut(cUnit, mir, rlSrc1, rlSrc2, rlDest, 2);
2408 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002409 case OP_APUT_SHORT:
2410 case OP_APUT_CHAR:
Bill Buzbee1465db52009-09-23 17:17:35 -07002411 genArrayPut(cUnit, mir, kUnsignedHalf, rlSrc1, rlSrc2, rlDest, 1);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002412 break;
2413 case OP_APUT_BYTE:
2414 case OP_APUT_BOOLEAN:
Bill Buzbee1465db52009-09-23 17:17:35 -07002415 genArrayPut(cUnit, mir, kUnsignedByte, rlSrc1, rlSrc2, rlDest, 0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002416 break;
2417 default:
2418 return true;
2419 }
2420 return false;
2421}
2422
Ben Cheng6c10a972009-10-29 14:39:18 -07002423/*
2424 * Find the matching case.
2425 *
2426 * return values:
2427 * r0 (low 32-bit): pc of the chaining cell corresponding to the resolved case,
2428 * including default which is placed at MIN(size, MAX_CHAINED_SWITCH_CASES).
2429 * r1 (high 32-bit): the branch offset of the matching case (only for indexes
2430 * above MAX_CHAINED_SWITCH_CASES).
2431 *
2432 * Instructions around the call are:
2433 *
2434 * mov r2, pc
2435 * blx &findPackedSwitchIndex
2436 * mov pc, r0
2437 * .align4
Bill Buzbeebd047242010-05-13 13:02:53 -07002438 * chaining cell for case 0 [12 bytes]
2439 * chaining cell for case 1 [12 bytes]
Ben Cheng6c10a972009-10-29 14:39:18 -07002440 * :
Bill Buzbeebd047242010-05-13 13:02:53 -07002441 * chaining cell for case MIN(size, MAX_CHAINED_SWITCH_CASES)-1 [12 bytes]
Ben Cheng6c10a972009-10-29 14:39:18 -07002442 * chaining cell for case default [8 bytes]
2443 * noChain exit
2444 */
Ben Chengbd1326d2010-04-02 15:04:53 -07002445static s8 findPackedSwitchIndex(const u2* switchData, int testVal, int pc)
Ben Cheng6c10a972009-10-29 14:39:18 -07002446{
2447 int size;
2448 int firstKey;
2449 const int *entries;
2450 int index;
2451 int jumpIndex;
2452 int caseDPCOffset = 0;
2453 /* In Thumb mode pc is 4 ahead of the "mov r2, pc" instruction */
2454 int chainingPC = (pc + 4) & ~3;
2455
2456 /*
2457 * Packed switch data format:
2458 * ushort ident = 0x0100 magic value
2459 * ushort size number of entries in the table
2460 * int first_key first (and lowest) switch case value
2461 * int targets[size] branch targets, relative to switch opcode
2462 *
2463 * Total size is (4+size*2) 16-bit code units.
2464 */
2465 size = switchData[1];
2466 assert(size > 0);
2467
2468 firstKey = switchData[2];
2469 firstKey |= switchData[3] << 16;
2470
2471
2472 /* The entries are guaranteed to be aligned on a 32-bit boundary;
2473 * we can treat them as a native int array.
2474 */
2475 entries = (const int*) &switchData[4];
2476 assert(((u4)entries & 0x3) == 0);
2477
2478 index = testVal - firstKey;
2479
2480 /* Jump to the default cell */
2481 if (index < 0 || index >= size) {
2482 jumpIndex = MIN(size, MAX_CHAINED_SWITCH_CASES);
2483 /* Jump to the non-chaining exit point */
2484 } else if (index >= MAX_CHAINED_SWITCH_CASES) {
2485 jumpIndex = MAX_CHAINED_SWITCH_CASES + 1;
2486 caseDPCOffset = entries[index];
2487 /* Jump to the inline chaining cell */
2488 } else {
2489 jumpIndex = index;
2490 }
2491
Bill Buzbeebd047242010-05-13 13:02:53 -07002492 chainingPC += jumpIndex * CHAIN_CELL_NORMAL_SIZE;
Ben Cheng6c10a972009-10-29 14:39:18 -07002493 return (((s8) caseDPCOffset) << 32) | (u8) chainingPC;
2494}
2495
2496/* See comments for findPackedSwitchIndex */
Ben Chengbd1326d2010-04-02 15:04:53 -07002497static s8 findSparseSwitchIndex(const u2* switchData, int testVal, int pc)
Ben Cheng6c10a972009-10-29 14:39:18 -07002498{
2499 int size;
2500 const int *keys;
2501 const int *entries;
2502 int chainingPC = (pc + 4) & ~3;
2503 int i;
2504
2505 /*
2506 * Sparse switch data format:
2507 * ushort ident = 0x0200 magic value
2508 * ushort size number of entries in the table; > 0
2509 * int keys[size] keys, sorted low-to-high; 32-bit aligned
2510 * int targets[size] branch targets, relative to switch opcode
2511 *
2512 * Total size is (2+size*4) 16-bit code units.
2513 */
2514
2515 size = switchData[1];
2516 assert(size > 0);
2517
2518 /* The keys are guaranteed to be aligned on a 32-bit boundary;
2519 * we can treat them as a native int array.
2520 */
2521 keys = (const int*) &switchData[2];
2522 assert(((u4)keys & 0x3) == 0);
2523
2524 /* The entries are guaranteed to be aligned on a 32-bit boundary;
2525 * we can treat them as a native int array.
2526 */
2527 entries = keys + size;
2528 assert(((u4)entries & 0x3) == 0);
2529
2530 /*
2531 * Run through the list of keys, which are guaranteed to
2532 * be sorted low-to-high.
2533 *
2534 * Most tables have 3-4 entries. Few have more than 10. A binary
2535 * search here is probably not useful.
2536 */
2537 for (i = 0; i < size; i++) {
2538 int k = keys[i];
2539 if (k == testVal) {
2540 /* MAX_CHAINED_SWITCH_CASES + 1 is the start of the overflow case */
2541 int jumpIndex = (i < MAX_CHAINED_SWITCH_CASES) ?
2542 i : MAX_CHAINED_SWITCH_CASES + 1;
Bill Buzbeebd047242010-05-13 13:02:53 -07002543 chainingPC += jumpIndex * CHAIN_CELL_NORMAL_SIZE;
Ben Cheng6c10a972009-10-29 14:39:18 -07002544 return (((s8) entries[i]) << 32) | (u8) chainingPC;
2545 } else if (k > testVal) {
2546 break;
2547 }
2548 }
Bill Buzbeebd047242010-05-13 13:02:53 -07002549 return chainingPC + MIN(size, MAX_CHAINED_SWITCH_CASES) *
2550 CHAIN_CELL_NORMAL_SIZE;
Ben Cheng6c10a972009-10-29 14:39:18 -07002551}
2552
Ben Chengba4fc8b2009-06-01 13:00:29 -07002553static bool handleFmt31t(CompilationUnit *cUnit, MIR *mir)
2554{
2555 OpCode dalvikOpCode = mir->dalvikInsn.opCode;
2556 switch (dalvikOpCode) {
2557 case OP_FILL_ARRAY_DATA: {
Bill Buzbeec6f10662010-02-09 11:16:15 -08002558 RegLocation rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07002559 // Making a call - use explicit registers
Bill Buzbeec6f10662010-02-09 11:16:15 -08002560 dvmCompilerFlushAllRegs(cUnit); /* Everything to home location */
Bill Buzbee1465db52009-09-23 17:17:35 -07002561 genExportPC(cUnit, mir);
2562 loadValueDirectFixed(cUnit, rlSrc, r0);
Ben Chengbd1326d2010-04-02 15:04:53 -07002563 LOAD_FUNC_ADDR(cUnit, r2, (int)dvmInterpHandleFillArrayData);
Ben Cheng6c10a972009-10-29 14:39:18 -07002564 loadConstant(cUnit, r1,
2565 (int) (cUnit->method->insns + mir->offset + mir->dalvikInsn.vB));
Bill Buzbee1465db52009-09-23 17:17:35 -07002566 opReg(cUnit, kOpBlx, r2);
Elliott Hughes6a555132010-02-25 15:41:42 -08002567 dvmCompilerClobberCallRegs(cUnit);
Bill Buzbeefd023aa2009-11-02 09:23:49 -08002568 /* generate a branch over if successful */
2569 opRegImm(cUnit, kOpCmp, r0, 0); /* NULL? */
2570 ArmLIR *branchOver = opCondBranch(cUnit, kArmCondNe);
2571 loadConstant(cUnit, r0,
2572 (int) (cUnit->method->insns + mir->offset));
2573 genDispatchToHandler(cUnit, TEMPLATE_THROW_EXCEPTION_COMMON);
2574 ArmLIR *target = newLIR0(cUnit, kArmPseudoTargetLabel);
2575 target->defMask = ENCODE_ALL;
2576 branchOver->generic.target = (LIR *) target;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002577 break;
2578 }
2579 /*
Ben Cheng6c10a972009-10-29 14:39:18 -07002580 * Compute the goto target of up to
2581 * MIN(switchSize, MAX_CHAINED_SWITCH_CASES) + 1 chaining cells.
2582 * See the comment before findPackedSwitchIndex for the code layout.
Ben Chengba4fc8b2009-06-01 13:00:29 -07002583 */
2584 case OP_PACKED_SWITCH:
2585 case OP_SPARSE_SWITCH: {
Bill Buzbeec6f10662010-02-09 11:16:15 -08002586 RegLocation rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
2587 dvmCompilerFlushAllRegs(cUnit); /* Everything to home location */
Bill Buzbee1465db52009-09-23 17:17:35 -07002588 loadValueDirectFixed(cUnit, rlSrc, r1);
Bill Buzbeec6f10662010-02-09 11:16:15 -08002589 dvmCompilerLockAllTemps(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002590 if (dalvikOpCode == OP_PACKED_SWITCH) {
Ben Chengbd1326d2010-04-02 15:04:53 -07002591 LOAD_FUNC_ADDR(cUnit, r4PC, (int)findPackedSwitchIndex);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002592 } else {
Ben Chengbd1326d2010-04-02 15:04:53 -07002593 LOAD_FUNC_ADDR(cUnit, r4PC, (int)findSparseSwitchIndex);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002594 }
Ben Cheng6c10a972009-10-29 14:39:18 -07002595 /* r0 <- Addr of the switch data */
2596 loadConstant(cUnit, r0,
2597 (int) (cUnit->method->insns + mir->offset + mir->dalvikInsn.vB));
2598 /* r2 <- pc of the instruction following the blx */
2599 opRegReg(cUnit, kOpMov, r2, rpc);
Bill Buzbee1465db52009-09-23 17:17:35 -07002600 opReg(cUnit, kOpBlx, r4PC);
Elliott Hughes6a555132010-02-25 15:41:42 -08002601 dvmCompilerClobberCallRegs(cUnit);
Ben Cheng6c10a972009-10-29 14:39:18 -07002602 /* pc <- computed goto target */
2603 opRegReg(cUnit, kOpMov, rpc, r0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002604 break;
2605 }
2606 default:
2607 return true;
2608 }
2609 return false;
2610}
2611
2612static bool handleFmt35c_3rc(CompilationUnit *cUnit, MIR *mir, BasicBlock *bb,
Bill Buzbee89efc3d2009-07-28 11:22:22 -07002613 ArmLIR *labelList)
Ben Chengba4fc8b2009-06-01 13:00:29 -07002614{
Bill Buzbee9bc3df32009-07-30 10:52:29 -07002615 ArmLIR *retChainingCell = NULL;
Bill Buzbee89efc3d2009-07-28 11:22:22 -07002616 ArmLIR *pcrLabel = NULL;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002617
Bill Buzbeef4ce16f2009-07-28 13:28:25 -07002618 if (bb->fallThrough != NULL)
2619 retChainingCell = &labelList[bb->fallThrough->id];
2620
Ben Chengba4fc8b2009-06-01 13:00:29 -07002621 DecodedInstruction *dInsn = &mir->dalvikInsn;
2622 switch (mir->dalvikInsn.opCode) {
2623 /*
2624 * calleeMethod = this->clazz->vtable[
2625 * method->clazz->pDvmDex->pResMethods[BBBB]->methodIndex
2626 * ]
2627 */
2628 case OP_INVOKE_VIRTUAL:
2629 case OP_INVOKE_VIRTUAL_RANGE: {
Bill Buzbee89efc3d2009-07-28 11:22:22 -07002630 ArmLIR *predChainingCell = &labelList[bb->taken->id];
Ben Chengba4fc8b2009-06-01 13:00:29 -07002631 int methodIndex =
2632 cUnit->method->clazz->pDvmDex->pResMethods[dInsn->vB]->
2633 methodIndex;
2634
2635 if (mir->dalvikInsn.opCode == OP_INVOKE_VIRTUAL)
2636 genProcessArgsNoRange(cUnit, mir, dInsn, &pcrLabel);
2637 else
2638 genProcessArgsRange(cUnit, mir, dInsn, &pcrLabel);
2639
Ben Cheng38329f52009-07-07 14:19:20 -07002640 genInvokeVirtualCommon(cUnit, mir, methodIndex,
2641 retChainingCell,
2642 predChainingCell,
2643 pcrLabel);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002644 break;
2645 }
2646 /*
2647 * calleeMethod = method->clazz->super->vtable[method->clazz->pDvmDex
2648 * ->pResMethods[BBBB]->methodIndex]
2649 */
Ben Chengba4fc8b2009-06-01 13:00:29 -07002650 case OP_INVOKE_SUPER:
2651 case OP_INVOKE_SUPER_RANGE: {
2652 int mIndex = cUnit->method->clazz->pDvmDex->
2653 pResMethods[dInsn->vB]->methodIndex;
2654 const Method *calleeMethod =
2655 cUnit->method->clazz->super->vtable[mIndex];
2656
2657 if (mir->dalvikInsn.opCode == OP_INVOKE_SUPER)
2658 genProcessArgsNoRange(cUnit, mir, dInsn, &pcrLabel);
2659 else
2660 genProcessArgsRange(cUnit, mir, dInsn, &pcrLabel);
2661
2662 /* r0 = calleeMethod */
2663 loadConstant(cUnit, r0, (int) calleeMethod);
2664
Ben Cheng38329f52009-07-07 14:19:20 -07002665 genInvokeSingletonCommon(cUnit, mir, bb, labelList, pcrLabel,
2666 calleeMethod);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002667 break;
2668 }
2669 /* calleeMethod = method->clazz->pDvmDex->pResMethods[BBBB] */
2670 case OP_INVOKE_DIRECT:
2671 case OP_INVOKE_DIRECT_RANGE: {
2672 const Method *calleeMethod =
2673 cUnit->method->clazz->pDvmDex->pResMethods[dInsn->vB];
2674
2675 if (mir->dalvikInsn.opCode == OP_INVOKE_DIRECT)
2676 genProcessArgsNoRange(cUnit, mir, dInsn, &pcrLabel);
2677 else
2678 genProcessArgsRange(cUnit, mir, dInsn, &pcrLabel);
2679
2680 /* r0 = calleeMethod */
2681 loadConstant(cUnit, r0, (int) calleeMethod);
2682
Ben Cheng38329f52009-07-07 14:19:20 -07002683 genInvokeSingletonCommon(cUnit, mir, bb, labelList, pcrLabel,
2684 calleeMethod);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002685 break;
2686 }
2687 /* calleeMethod = method->clazz->pDvmDex->pResMethods[BBBB] */
2688 case OP_INVOKE_STATIC:
2689 case OP_INVOKE_STATIC_RANGE: {
2690 const Method *calleeMethod =
2691 cUnit->method->clazz->pDvmDex->pResMethods[dInsn->vB];
2692
2693 if (mir->dalvikInsn.opCode == OP_INVOKE_STATIC)
2694 genProcessArgsNoRange(cUnit, mir, dInsn,
2695 NULL /* no null check */);
2696 else
2697 genProcessArgsRange(cUnit, mir, dInsn,
2698 NULL /* no null check */);
2699
2700 /* r0 = calleeMethod */
2701 loadConstant(cUnit, r0, (int) calleeMethod);
2702
Ben Cheng38329f52009-07-07 14:19:20 -07002703 genInvokeSingletonCommon(cUnit, mir, bb, labelList, pcrLabel,
2704 calleeMethod);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002705 break;
2706 }
Ben Cheng09e50c92010-05-02 10:45:32 -07002707 /*
Ben Chengba4fc8b2009-06-01 13:00:29 -07002708 * calleeMethod = dvmFindInterfaceMethodInCache(this->clazz,
2709 * BBBB, method, method->clazz->pDvmDex)
Ben Cheng38329f52009-07-07 14:19:20 -07002710 *
Ben Cheng09e50c92010-05-02 10:45:32 -07002711 * The following is an example of generated code for
2712 * "invoke-interface v0"
Ben Cheng38329f52009-07-07 14:19:20 -07002713 *
Ben Cheng09e50c92010-05-02 10:45:32 -07002714 * -------- dalvik offset: 0x0008 @ invoke-interface v0
2715 * 0x47357e36 : ldr r0, [r5, #0] --+
2716 * 0x47357e38 : sub r7,r5,#24 |
2717 * 0x47357e3c : cmp r0, #0 | genProcessArgsNoRange
2718 * 0x47357e3e : beq 0x47357e82 |
2719 * 0x47357e40 : stmia r7, <r0> --+
2720 * 0x47357e42 : ldr r4, [pc, #120] --> r4 <- dalvikPC of this invoke
2721 * 0x47357e44 : add r1, pc, #64 --> r1 <- &retChainingCell
2722 * 0x47357e46 : add r2, pc, #72 --> r2 <- &predictedChainingCell
2723 * 0x47357e48 : blx_1 0x47348190 --+ TEMPLATE_INVOKE_METHOD_
2724 * 0x47357e4a : blx_2 see above --+ PREDICTED_CHAIN
2725 * 0x47357e4c : b 0x47357e90 --> off to the predicted chain
2726 * 0x47357e4e : b 0x47357e82 --> punt to the interpreter
2727 * 0x47357e50 : mov r8, r1 --+
2728 * 0x47357e52 : mov r9, r2 |
2729 * 0x47357e54 : ldr r2, [pc, #96] |
2730 * 0x47357e56 : mov r10, r3 |
2731 * 0x47357e58 : movs r0, r3 | dvmFindInterfaceMethodInCache
2732 * 0x47357e5a : ldr r3, [pc, #88] |
2733 * 0x47357e5c : ldr r7, [pc, #80] |
2734 * 0x47357e5e : mov r1, #1452 |
2735 * 0x47357e62 : blx r7 --+
2736 * 0x47357e64 : cmp r0, #0 --> calleeMethod == NULL?
2737 * 0x47357e66 : bne 0x47357e6e --> branch over the throw if !r0
2738 * 0x47357e68 : ldr r0, [pc, #80] --> load Dalvik PC of the invoke
2739 * 0x47357e6a : blx_1 0x47348494 --+ TEMPLATE_THROW_EXCEPTION_
2740 * 0x47357e6c : blx_2 see above --+ COMMON
2741 * 0x47357e6e : mov r1, r8 --> r1 <- &retChainingCell
2742 * 0x47357e70 : cmp r1, #0 --> compare against 0
2743 * 0x47357e72 : bgt 0x47357e7c --> >=0? don't rechain
2744 * 0x47357e74 : ldr r7, [r6, #108] --+
2745 * 0x47357e76 : mov r2, r9 | dvmJitToPatchPredictedChain
2746 * 0x47357e78 : mov r3, r10 |
2747 * 0x47357e7a : blx r7 --+
2748 * 0x47357e7c : add r1, pc, #8 --> r1 <- &retChainingCell
2749 * 0x47357e7e : blx_1 0x4734809c --+ TEMPLATE_INVOKE_METHOD_NO_OPT
2750 * 0x47357e80 : blx_2 see above --+
2751 * -------- reconstruct dalvik PC : 0x425719dc @ +0x0008
2752 * 0x47357e82 : ldr r0, [pc, #56]
Ben Cheng38329f52009-07-07 14:19:20 -07002753 * Exception_Handling:
Ben Cheng09e50c92010-05-02 10:45:32 -07002754 * 0x47357e84 : ldr r1, [r6, #92]
2755 * 0x47357e86 : blx r1
2756 * 0x47357e88 : .align4
2757 * -------- chaining cell (hot): 0x000b
2758 * 0x47357e88 : ldr r0, [r6, #104]
2759 * 0x47357e8a : blx r0
2760 * 0x47357e8c : data 0x19e2(6626)
2761 * 0x47357e8e : data 0x4257(16983)
2762 * 0x47357e90 : .align4
Ben Cheng38329f52009-07-07 14:19:20 -07002763 * -------- chaining cell (predicted)
Ben Cheng09e50c92010-05-02 10:45:32 -07002764 * 0x47357e90 : data 0xe7fe(59390) --> will be patched into bx
2765 * 0x47357e92 : data 0x0000(0)
2766 * 0x47357e94 : data 0x0000(0) --> class
2767 * 0x47357e96 : data 0x0000(0)
2768 * 0x47357e98 : data 0x0000(0) --> method
2769 * 0x47357e9a : data 0x0000(0)
2770 * 0x47357e9c : data 0x0000(0) --> rechain count
2771 * 0x47357e9e : data 0x0000(0)
2772 * -------- end of chaining cells (0x006c)
2773 * 0x47357eb0 : .word (0xad03e369)
2774 * 0x47357eb4 : .word (0x28a90)
2775 * 0x47357eb8 : .word (0x41a63394)
2776 * 0x47357ebc : .word (0x425719dc)
Ben Chengba4fc8b2009-06-01 13:00:29 -07002777 */
2778 case OP_INVOKE_INTERFACE:
2779 case OP_INVOKE_INTERFACE_RANGE: {
Bill Buzbee89efc3d2009-07-28 11:22:22 -07002780 ArmLIR *predChainingCell = &labelList[bb->taken->id];
Ben Chengba4fc8b2009-06-01 13:00:29 -07002781
Bill Buzbee1465db52009-09-23 17:17:35 -07002782 /* Ensure that nothing is both live and dirty */
Bill Buzbeec6f10662010-02-09 11:16:15 -08002783 dvmCompilerFlushAllRegs(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07002784
Ben Chengba4fc8b2009-06-01 13:00:29 -07002785 if (mir->dalvikInsn.opCode == OP_INVOKE_INTERFACE)
2786 genProcessArgsNoRange(cUnit, mir, dInsn, &pcrLabel);
2787 else
2788 genProcessArgsRange(cUnit, mir, dInsn, &pcrLabel);
2789
Ben Cheng38329f52009-07-07 14:19:20 -07002790 /* "this" is already left in r0 by genProcessArgs* */
2791
2792 /* r4PC = dalvikCallsite */
2793 loadConstant(cUnit, r4PC,
2794 (int) (cUnit->method->insns + mir->offset));
2795
2796 /* r1 = &retChainingCell */
Bill Buzbee270c1d62009-08-13 16:58:07 -07002797 ArmLIR *addrRetChain =
Bill Buzbee1465db52009-09-23 17:17:35 -07002798 opRegRegImm(cUnit, kOpAdd, r1, rpc, 0);
Ben Cheng38329f52009-07-07 14:19:20 -07002799 addrRetChain->generic.target = (LIR *) retChainingCell;
2800
2801 /* r2 = &predictedChainingCell */
Bill Buzbee89efc3d2009-07-28 11:22:22 -07002802 ArmLIR *predictedChainingCell =
Bill Buzbee1465db52009-09-23 17:17:35 -07002803 opRegRegImm(cUnit, kOpAdd, r2, rpc, 0);
Ben Cheng38329f52009-07-07 14:19:20 -07002804 predictedChainingCell->generic.target = (LIR *) predChainingCell;
2805
2806 genDispatchToHandler(cUnit, TEMPLATE_INVOKE_METHOD_PREDICTED_CHAIN);
2807
2808 /* return through lr - jump to the chaining cell */
2809 genUnconditionalBranch(cUnit, predChainingCell);
2810
2811 /*
2812 * null-check on "this" may have been eliminated, but we still need
2813 * a PC-reconstruction label for stack overflow bailout.
2814 */
2815 if (pcrLabel == NULL) {
2816 int dPC = (int) (cUnit->method->insns + mir->offset);
Bill Buzbee89efc3d2009-07-28 11:22:22 -07002817 pcrLabel = dvmCompilerNew(sizeof(ArmLIR), true);
Ben Chenga4973592010-03-31 11:59:18 -07002818 pcrLabel->opCode = kArmPseudoPCReconstructionCell;
Ben Cheng38329f52009-07-07 14:19:20 -07002819 pcrLabel->operands[0] = dPC;
2820 pcrLabel->operands[1] = mir->offset;
2821 /* Insert the place holder to the growable list */
2822 dvmInsertGrowableList(&cUnit->pcReconstructionList, pcrLabel);
2823 }
2824
2825 /* return through lr+2 - punt to the interpreter */
2826 genUnconditionalBranch(cUnit, pcrLabel);
2827
2828 /*
2829 * return through lr+4 - fully resolve the callee method.
2830 * r1 <- count
2831 * r2 <- &predictedChainCell
2832 * r3 <- this->class
2833 * r4 <- dPC
2834 * r7 <- this->class->vtable
2835 */
2836
2837 /* Save count, &predictedChainCell, and class to high regs first */
Bill Buzbee1465db52009-09-23 17:17:35 -07002838 genRegCopy(cUnit, r8, r1);
2839 genRegCopy(cUnit, r9, r2);
2840 genRegCopy(cUnit, r10, r3);
Ben Cheng38329f52009-07-07 14:19:20 -07002841
Ben Chengba4fc8b2009-06-01 13:00:29 -07002842 /* r0 now contains this->clazz */
Bill Buzbee1465db52009-09-23 17:17:35 -07002843 genRegCopy(cUnit, r0, r3);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002844
2845 /* r1 = BBBB */
2846 loadConstant(cUnit, r1, dInsn->vB);
2847
2848 /* r2 = method (caller) */
2849 loadConstant(cUnit, r2, (int) cUnit->method);
2850
2851 /* r3 = pDvmDex */
2852 loadConstant(cUnit, r3, (int) cUnit->method->clazz->pDvmDex);
2853
Ben Chengbd1326d2010-04-02 15:04:53 -07002854 LOAD_FUNC_ADDR(cUnit, r7,
2855 (intptr_t) dvmFindInterfaceMethodInCache);
Bill Buzbee1465db52009-09-23 17:17:35 -07002856 opReg(cUnit, kOpBlx, r7);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002857 /* r0 = calleeMethod (returned from dvmFindInterfaceMethodInCache */
2858
Ben Cheng09e50c92010-05-02 10:45:32 -07002859 dvmCompilerClobberCallRegs(cUnit);
2860 /* generate a branch over if the interface method is resolved */
2861 opRegImm(cUnit, kOpCmp, r0, 0); /* NULL? */
2862 ArmLIR *branchOver = opCondBranch(cUnit, kArmCondNe);
2863 /*
2864 * calleeMethod == NULL -> throw
2865 */
2866 loadConstant(cUnit, r0,
2867 (int) (cUnit->method->insns + mir->offset));
2868 genDispatchToHandler(cUnit, TEMPLATE_THROW_EXCEPTION_COMMON);
2869 /* noreturn */
2870
2871 ArmLIR *target = newLIR0(cUnit, kArmPseudoTargetLabel);
2872 target->defMask = ENCODE_ALL;
2873 branchOver->generic.target = (LIR *) target;
2874
Bill Buzbee1465db52009-09-23 17:17:35 -07002875 genRegCopy(cUnit, r1, r8);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002876
Ben Cheng38329f52009-07-07 14:19:20 -07002877 /* Check if rechain limit is reached */
Bill Buzbee1465db52009-09-23 17:17:35 -07002878 opRegImm(cUnit, kOpCmp, r1, 0);
Ben Cheng38329f52009-07-07 14:19:20 -07002879
Bill Buzbee1465db52009-09-23 17:17:35 -07002880 ArmLIR *bypassRechaining = opCondBranch(cUnit, kArmCondGt);
Ben Cheng38329f52009-07-07 14:19:20 -07002881
Bill Buzbee270c1d62009-08-13 16:58:07 -07002882 loadWordDisp(cUnit, rGLUE, offsetof(InterpState,
2883 jitToInterpEntries.dvmJitToPatchPredictedChain), r7);
Ben Cheng38329f52009-07-07 14:19:20 -07002884
Ben Chengb88ec3c2010-05-17 12:50:33 -07002885 genRegCopy(cUnit, r1, rGLUE);
Bill Buzbee1465db52009-09-23 17:17:35 -07002886 genRegCopy(cUnit, r2, r9);
2887 genRegCopy(cUnit, r3, r10);
Ben Cheng38329f52009-07-07 14:19:20 -07002888
2889 /*
2890 * r0 = calleeMethod
2891 * r2 = &predictedChainingCell
2892 * r3 = class
2893 *
2894 * &returnChainingCell has been loaded into r1 but is not needed
2895 * when patching the chaining cell and will be clobbered upon
2896 * returning so it will be reconstructed again.
2897 */
Bill Buzbee1465db52009-09-23 17:17:35 -07002898 opReg(cUnit, kOpBlx, r7);
Ben Cheng38329f52009-07-07 14:19:20 -07002899
2900 /* r1 = &retChainingCell */
Bill Buzbee1465db52009-09-23 17:17:35 -07002901 addrRetChain = opRegRegImm(cUnit, kOpAdd, r1, rpc, 0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002902 addrRetChain->generic.target = (LIR *) retChainingCell;
Ben Cheng38329f52009-07-07 14:19:20 -07002903
2904 bypassRechaining->generic.target = (LIR *) addrRetChain;
2905
Ben Chengba4fc8b2009-06-01 13:00:29 -07002906 /*
2907 * r0 = this, r1 = calleeMethod,
2908 * r1 = &ChainingCell,
2909 * r4PC = callsiteDPC,
2910 */
2911 genDispatchToHandler(cUnit, TEMPLATE_INVOKE_METHOD_NO_OPT);
Ben Cheng978738d2010-05-13 13:45:57 -07002912#if defined(WITH_JIT_TUNING)
Ben Cheng86717f72010-03-05 15:27:21 -08002913 gDvmJit.invokePolymorphic++;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002914#endif
2915 /* Handle exceptions using the interpreter */
2916 genTrap(cUnit, mir->offset, pcrLabel);
2917 break;
2918 }
2919 /* NOP */
2920 case OP_INVOKE_DIRECT_EMPTY: {
2921 return false;
2922 }
2923 case OP_FILLED_NEW_ARRAY:
2924 case OP_FILLED_NEW_ARRAY_RANGE: {
2925 /* Just let the interpreter deal with these */
2926 genInterpSingleStep(cUnit, mir);
2927 break;
2928 }
2929 default:
2930 return true;
2931 }
2932 return false;
2933}
2934
2935static bool handleFmt35ms_3rms(CompilationUnit *cUnit, MIR *mir,
Bill Buzbee89efc3d2009-07-28 11:22:22 -07002936 BasicBlock *bb, ArmLIR *labelList)
Ben Chengba4fc8b2009-06-01 13:00:29 -07002937{
Bill Buzbee89efc3d2009-07-28 11:22:22 -07002938 ArmLIR *retChainingCell = &labelList[bb->fallThrough->id];
2939 ArmLIR *predChainingCell = &labelList[bb->taken->id];
2940 ArmLIR *pcrLabel = NULL;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002941
2942 DecodedInstruction *dInsn = &mir->dalvikInsn;
2943 switch (mir->dalvikInsn.opCode) {
2944 /* calleeMethod = this->clazz->vtable[BBBB] */
2945 case OP_INVOKE_VIRTUAL_QUICK_RANGE:
2946 case OP_INVOKE_VIRTUAL_QUICK: {
2947 int methodIndex = dInsn->vB;
2948 if (mir->dalvikInsn.opCode == OP_INVOKE_VIRTUAL_QUICK)
2949 genProcessArgsNoRange(cUnit, mir, dInsn, &pcrLabel);
2950 else
2951 genProcessArgsRange(cUnit, mir, dInsn, &pcrLabel);
2952
Ben Cheng38329f52009-07-07 14:19:20 -07002953 genInvokeVirtualCommon(cUnit, mir, methodIndex,
2954 retChainingCell,
2955 predChainingCell,
2956 pcrLabel);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002957 break;
2958 }
2959 /* calleeMethod = method->clazz->super->vtable[BBBB] */
2960 case OP_INVOKE_SUPER_QUICK:
2961 case OP_INVOKE_SUPER_QUICK_RANGE: {
2962 const Method *calleeMethod =
2963 cUnit->method->clazz->super->vtable[dInsn->vB];
2964
2965 if (mir->dalvikInsn.opCode == OP_INVOKE_SUPER_QUICK)
2966 genProcessArgsNoRange(cUnit, mir, dInsn, &pcrLabel);
2967 else
2968 genProcessArgsRange(cUnit, mir, dInsn, &pcrLabel);
2969
2970 /* r0 = calleeMethod */
2971 loadConstant(cUnit, r0, (int) calleeMethod);
2972
Ben Cheng38329f52009-07-07 14:19:20 -07002973 genInvokeSingletonCommon(cUnit, mir, bb, labelList, pcrLabel,
2974 calleeMethod);
2975 /* Handle exceptions using the interpreter */
2976 genTrap(cUnit, mir->offset, pcrLabel);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002977 break;
2978 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07002979 default:
2980 return true;
2981 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07002982 return false;
2983}
2984
2985/*
Bill Buzbeefd023aa2009-11-02 09:23:49 -08002986 * This operation is complex enough that we'll do it partly inline
2987 * and partly with a handler. NOTE: the handler uses hardcoded
2988 * values for string object offsets and must be revisitied if the
2989 * layout changes.
2990 */
2991static bool genInlinedCompareTo(CompilationUnit *cUnit, MIR *mir)
2992{
2993#if defined(USE_GLOBAL_STRING_DEFS)
2994 return false;
2995#else
2996 ArmLIR *rollback;
Bill Buzbeec6f10662010-02-09 11:16:15 -08002997 RegLocation rlThis = dvmCompilerGetSrc(cUnit, mir, 0);
2998 RegLocation rlComp = dvmCompilerGetSrc(cUnit, mir, 1);
Bill Buzbeefd023aa2009-11-02 09:23:49 -08002999
3000 loadValueDirectFixed(cUnit, rlThis, r0);
3001 loadValueDirectFixed(cUnit, rlComp, r1);
3002 /* Test objects for NULL */
3003 rollback = genNullCheck(cUnit, rlThis.sRegLow, r0, mir->offset, NULL);
3004 genNullCheck(cUnit, rlComp.sRegLow, r1, mir->offset, rollback);
3005 /*
3006 * TUNING: we could check for object pointer equality before invoking
3007 * handler. Unclear whether the gain would be worth the added code size
3008 * expansion.
3009 */
3010 genDispatchToHandler(cUnit, TEMPLATE_STRING_COMPARETO);
Bill Buzbeec6f10662010-02-09 11:16:15 -08003011 storeValue(cUnit, inlinedTarget(cUnit, mir, false),
3012 dvmCompilerGetReturn(cUnit));
Bill Buzbeefd023aa2009-11-02 09:23:49 -08003013 return true;
3014#endif
3015}
3016
Elliott Hughes2bdbcb62010-04-12 14:29:37 -07003017static bool genInlinedFastIndexOf(CompilationUnit *cUnit, MIR *mir)
Bill Buzbeefd023aa2009-11-02 09:23:49 -08003018{
3019#if defined(USE_GLOBAL_STRING_DEFS)
3020 return false;
3021#else
Bill Buzbeec6f10662010-02-09 11:16:15 -08003022 RegLocation rlThis = dvmCompilerGetSrc(cUnit, mir, 0);
3023 RegLocation rlChar = dvmCompilerGetSrc(cUnit, mir, 1);
Bill Buzbeefd023aa2009-11-02 09:23:49 -08003024
3025 loadValueDirectFixed(cUnit, rlThis, r0);
3026 loadValueDirectFixed(cUnit, rlChar, r1);
Elliott Hughes2bdbcb62010-04-12 14:29:37 -07003027 RegLocation rlStart = dvmCompilerGetSrc(cUnit, mir, 2);
3028 loadValueDirectFixed(cUnit, rlStart, r2);
Bill Buzbeefd023aa2009-11-02 09:23:49 -08003029 /* Test objects for NULL */
3030 genNullCheck(cUnit, rlThis.sRegLow, r0, mir->offset, NULL);
3031 genDispatchToHandler(cUnit, TEMPLATE_STRING_INDEXOF);
Bill Buzbeec6f10662010-02-09 11:16:15 -08003032 storeValue(cUnit, inlinedTarget(cUnit, mir, false),
3033 dvmCompilerGetReturn(cUnit));
Bill Buzbeefd023aa2009-11-02 09:23:49 -08003034 return true;
3035#endif
3036}
3037
Elliott Hughesee34f592010-04-05 18:13:52 -07003038// Generates an inlined String.isEmpty or String.length.
3039static bool genInlinedStringIsEmptyOrLength(CompilationUnit *cUnit, MIR *mir,
3040 bool isEmpty)
Bill Buzbee1f748632010-03-02 16:14:41 -08003041{
Elliott Hughesee34f592010-04-05 18:13:52 -07003042 // dst = src.length();
Bill Buzbee1f748632010-03-02 16:14:41 -08003043 RegLocation rlObj = dvmCompilerGetSrc(cUnit, mir, 0);
3044 RegLocation rlDest = inlinedTarget(cUnit, mir, false);
3045 rlObj = loadValue(cUnit, rlObj, kCoreReg);
3046 RegLocation rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
3047 genNullCheck(cUnit, rlObj.sRegLow, rlObj.lowReg, mir->offset, NULL);
3048 loadWordDisp(cUnit, rlObj.lowReg, gDvm.offJavaLangString_count,
3049 rlResult.lowReg);
Elliott Hughesee34f592010-04-05 18:13:52 -07003050 if (isEmpty) {
3051 // dst = (dst == 0);
3052 int tReg = dvmCompilerAllocTemp(cUnit);
3053 opRegReg(cUnit, kOpNeg, tReg, rlResult.lowReg);
3054 opRegRegReg(cUnit, kOpAdc, rlResult.lowReg, rlResult.lowReg, tReg);
3055 }
Bill Buzbee1f748632010-03-02 16:14:41 -08003056 storeValue(cUnit, rlDest, rlResult);
3057 return false;
3058}
3059
Elliott Hughesee34f592010-04-05 18:13:52 -07003060static bool genInlinedStringLength(CompilationUnit *cUnit, MIR *mir)
3061{
3062 return genInlinedStringIsEmptyOrLength(cUnit, mir, false);
3063}
3064
3065static bool genInlinedStringIsEmpty(CompilationUnit *cUnit, MIR *mir)
3066{
3067 return genInlinedStringIsEmptyOrLength(cUnit, mir, true);
3068}
3069
Bill Buzbee1f748632010-03-02 16:14:41 -08003070static bool genInlinedStringCharAt(CompilationUnit *cUnit, MIR *mir)
3071{
3072 int contents = offsetof(ArrayObject, contents);
3073 RegLocation rlObj = dvmCompilerGetSrc(cUnit, mir, 0);
3074 RegLocation rlIdx = dvmCompilerGetSrc(cUnit, mir, 1);
3075 RegLocation rlDest = inlinedTarget(cUnit, mir, false);
3076 RegLocation rlResult;
3077 rlObj = loadValue(cUnit, rlObj, kCoreReg);
3078 rlIdx = loadValue(cUnit, rlIdx, kCoreReg);
3079 int regMax = dvmCompilerAllocTemp(cUnit);
3080 int regOff = dvmCompilerAllocTemp(cUnit);
3081 int regPtr = dvmCompilerAllocTemp(cUnit);
3082 ArmLIR *pcrLabel = genNullCheck(cUnit, rlObj.sRegLow, rlObj.lowReg,
3083 mir->offset, NULL);
3084 loadWordDisp(cUnit, rlObj.lowReg, gDvm.offJavaLangString_count, regMax);
3085 loadWordDisp(cUnit, rlObj.lowReg, gDvm.offJavaLangString_offset, regOff);
3086 loadWordDisp(cUnit, rlObj.lowReg, gDvm.offJavaLangString_value, regPtr);
3087 genBoundsCheck(cUnit, rlIdx.lowReg, regMax, mir->offset, pcrLabel);
3088 dvmCompilerFreeTemp(cUnit, regMax);
3089 opRegImm(cUnit, kOpAdd, regPtr, contents);
3090 opRegReg(cUnit, kOpAdd, regOff, rlIdx.lowReg);
3091 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
3092 loadBaseIndexed(cUnit, regPtr, regOff, rlResult.lowReg, 1, kUnsignedHalf);
3093 storeValue(cUnit, rlDest, rlResult);
3094 return false;
3095}
3096
3097static bool genInlinedAbsInt(CompilationUnit *cUnit, MIR *mir)
3098{
3099 RegLocation rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
3100 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
3101 RegLocation rlDest = inlinedTarget(cUnit, mir, false);;
3102 RegLocation rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
3103 int signReg = dvmCompilerAllocTemp(cUnit);
3104 /*
3105 * abs(x) = y<=x>>31, (x+y)^y.
3106 * Thumb2's IT block also yields 3 instructions, but imposes
3107 * scheduling constraints.
3108 */
3109 opRegRegImm(cUnit, kOpAsr, signReg, rlSrc.lowReg, 31);
3110 opRegRegReg(cUnit, kOpAdd, rlResult.lowReg, rlSrc.lowReg, signReg);
3111 opRegReg(cUnit, kOpXor, rlResult.lowReg, signReg);
3112 storeValue(cUnit, rlDest, rlResult);
3113 return false;
3114}
3115
3116static bool genInlinedAbsLong(CompilationUnit *cUnit, MIR *mir)
3117{
3118 RegLocation rlSrc = dvmCompilerGetSrcWide(cUnit, mir, 0, 1);
3119 RegLocation rlDest = inlinedTargetWide(cUnit, mir, false);
3120 rlSrc = loadValueWide(cUnit, rlSrc, kCoreReg);
3121 RegLocation rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
3122 int signReg = dvmCompilerAllocTemp(cUnit);
3123 /*
3124 * abs(x) = y<=x>>31, (x+y)^y.
3125 * Thumb2 IT block allows slightly shorter sequence,
3126 * but introduces a scheduling barrier. Stick with this
3127 * mechanism for now.
3128 */
3129 opRegRegImm(cUnit, kOpAsr, signReg, rlSrc.highReg, 31);
3130 opRegRegReg(cUnit, kOpAdd, rlResult.lowReg, rlSrc.lowReg, signReg);
3131 opRegRegReg(cUnit, kOpAdc, rlResult.highReg, rlSrc.highReg, signReg);
3132 opRegReg(cUnit, kOpXor, rlResult.lowReg, signReg);
3133 opRegReg(cUnit, kOpXor, rlResult.highReg, signReg);
3134 storeValueWide(cUnit, rlDest, rlResult);
3135 return false;
3136}
Bill Buzbeefd023aa2009-11-02 09:23:49 -08003137
3138/*
Bill Buzbeece46c942009-11-20 15:41:34 -08003139 * NOTE: Handles both range and non-range versions (arguments
3140 * have already been normalized by this point).
Ben Chengba4fc8b2009-06-01 13:00:29 -07003141 */
Bill Buzbeece46c942009-11-20 15:41:34 -08003142static bool handleExecuteInline(CompilationUnit *cUnit, MIR *mir)
Ben Chengba4fc8b2009-06-01 13:00:29 -07003143{
3144 DecodedInstruction *dInsn = &mir->dalvikInsn;
3145 switch( mir->dalvikInsn.opCode) {
Bill Buzbeece46c942009-11-20 15:41:34 -08003146 case OP_EXECUTE_INLINE_RANGE:
Ben Chengba4fc8b2009-06-01 13:00:29 -07003147 case OP_EXECUTE_INLINE: {
3148 unsigned int i;
3149 const InlineOperation* inLineTable = dvmGetInlineOpsTable();
Bill Buzbee50a6bf22009-07-08 13:08:04 -07003150 int offset = offsetof(InterpState, retval);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003151 int operation = dInsn->vB;
Bill Buzbee50a6bf22009-07-08 13:08:04 -07003152 switch (operation) {
3153 case INLINE_EMPTYINLINEMETHOD:
3154 return false; /* Nop */
3155 case INLINE_STRING_LENGTH:
3156 return genInlinedStringLength(cUnit, mir);
Elliott Hughesee34f592010-04-05 18:13:52 -07003157 case INLINE_STRING_IS_EMPTY:
3158 return genInlinedStringIsEmpty(cUnit, mir);
Bill Buzbee50a6bf22009-07-08 13:08:04 -07003159 case INLINE_MATH_ABS_INT:
3160 return genInlinedAbsInt(cUnit, mir);
3161 case INLINE_MATH_ABS_LONG:
3162 return genInlinedAbsLong(cUnit, mir);
3163 case INLINE_MATH_MIN_INT:
3164 return genInlinedMinMaxInt(cUnit, mir, true);
3165 case INLINE_MATH_MAX_INT:
3166 return genInlinedMinMaxInt(cUnit, mir, false);
3167 case INLINE_STRING_CHARAT:
3168 return genInlinedStringCharAt(cUnit, mir);
3169 case INLINE_MATH_SQRT:
3170 if (genInlineSqrt(cUnit, mir))
Bill Buzbee9727c3d2009-08-01 11:32:36 -07003171 return false;
Bill Buzbee50a6bf22009-07-08 13:08:04 -07003172 else
3173 break; /* Handle with C routine */
Bill Buzbee50a6bf22009-07-08 13:08:04 -07003174 case INLINE_MATH_ABS_FLOAT:
Bill Buzbee1465db52009-09-23 17:17:35 -07003175 if (genInlinedAbsFloat(cUnit, mir))
3176 return false;
3177 else
3178 break;
Bill Buzbee50a6bf22009-07-08 13:08:04 -07003179 case INLINE_MATH_ABS_DOUBLE:
Bill Buzbee1465db52009-09-23 17:17:35 -07003180 if (genInlinedAbsDouble(cUnit, mir))
3181 return false;
3182 else
3183 break;
Bill Buzbee50a6bf22009-07-08 13:08:04 -07003184 case INLINE_STRING_COMPARETO:
Bill Buzbeefd023aa2009-11-02 09:23:49 -08003185 if (genInlinedCompareTo(cUnit, mir))
3186 return false;
3187 else
3188 break;
Elliott Hughes2bdbcb62010-04-12 14:29:37 -07003189 case INLINE_STRING_FASTINDEXOF_II:
3190 if (genInlinedFastIndexOf(cUnit, mir))
Bill Buzbeefd023aa2009-11-02 09:23:49 -08003191 return false;
3192 else
3193 break;
3194 case INLINE_STRING_EQUALS:
3195 case INLINE_MATH_COS:
3196 case INLINE_MATH_SIN:
3197 break; /* Handle with C routine */
Bill Buzbee50a6bf22009-07-08 13:08:04 -07003198 default:
Bill Buzbeefc519dc2010-03-06 23:30:57 -08003199 dvmCompilerAbort(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003200 }
Bill Buzbeec6f10662010-02-09 11:16:15 -08003201 dvmCompilerFlushAllRegs(cUnit); /* Everything to home location */
Elliott Hughes6a555132010-02-25 15:41:42 -08003202 dvmCompilerClobberCallRegs(cUnit);
Bill Buzbeec6f10662010-02-09 11:16:15 -08003203 dvmCompilerClobber(cUnit, r4PC);
3204 dvmCompilerClobber(cUnit, r7);
Bill Buzbee1465db52009-09-23 17:17:35 -07003205 opRegRegImm(cUnit, kOpAdd, r4PC, rGLUE, offset);
3206 opImm(cUnit, kOpPush, (1<<r4PC) | (1<<r7));
Ben Chengbd1326d2010-04-02 15:04:53 -07003207 LOAD_FUNC_ADDR(cUnit, r4PC, (int)inLineTable[operation].func);
Bill Buzbee1465db52009-09-23 17:17:35 -07003208 genExportPC(cUnit, mir);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003209 for (i=0; i < dInsn->vA; i++) {
Bill Buzbeec6f10662010-02-09 11:16:15 -08003210 loadValueDirect(cUnit, dvmCompilerGetSrc(cUnit, mir, i), i);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003211 }
Bill Buzbee1465db52009-09-23 17:17:35 -07003212 opReg(cUnit, kOpBlx, r4PC);
3213 opRegImm(cUnit, kOpAdd, r13, 8);
Bill Buzbeece46c942009-11-20 15:41:34 -08003214 opRegImm(cUnit, kOpCmp, r0, 0); /* NULL? */
3215 ArmLIR *branchOver = opCondBranch(cUnit, kArmCondNe);
3216 loadConstant(cUnit, r0,
3217 (int) (cUnit->method->insns + mir->offset));
3218 genDispatchToHandler(cUnit, TEMPLATE_THROW_EXCEPTION_COMMON);
3219 ArmLIR *target = newLIR0(cUnit, kArmPseudoTargetLabel);
3220 target->defMask = ENCODE_ALL;
3221 branchOver->generic.target = (LIR *) target;
Ben Chengba4fc8b2009-06-01 13:00:29 -07003222 break;
3223 }
3224 default:
3225 return true;
3226 }
3227 return false;
3228}
3229
3230static bool handleFmt51l(CompilationUnit *cUnit, MIR *mir)
3231{
Bill Buzbee1465db52009-09-23 17:17:35 -07003232 //TUNING: We're using core regs here - not optimal when target is a double
Bill Buzbeec6f10662010-02-09 11:16:15 -08003233 RegLocation rlDest = dvmCompilerGetDestWide(cUnit, mir, 0, 1);
3234 RegLocation rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Ben Chengbd1326d2010-04-02 15:04:53 -07003235 loadConstantNoClobber(cUnit, rlResult.lowReg,
3236 mir->dalvikInsn.vB_wide & 0xFFFFFFFFUL);
3237 loadConstantNoClobber(cUnit, rlResult.highReg,
3238 (mir->dalvikInsn.vB_wide>>32) & 0xFFFFFFFFUL);
Bill Buzbee1465db52009-09-23 17:17:35 -07003239 storeValueWide(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003240 return false;
3241}
3242
Ben Chengba4fc8b2009-06-01 13:00:29 -07003243/*
3244 * The following are special processing routines that handle transfer of
3245 * controls between compiled code and the interpreter. Certain VM states like
3246 * Dalvik PC and special-purpose registers are reconstructed here.
3247 */
3248
Bill Buzbeebd047242010-05-13 13:02:53 -07003249/*
3250 * Insert a
3251 * b .+4
3252 * nop
3253 * pair at the beginning of a chaining cell. This serves as the
3254 * switch branch that selects between reverting to the interpreter or
3255 * not. Once the cell is chained to a translation, the cell will
3256 * contain a 32-bit branch. Subsequent chain/unchain operations will
3257 * then only alter that first 16-bits - the "b .+4" for unchaining,
3258 * and the restoration of the first half of the 32-bit branch for
3259 * rechaining.
3260 */
3261static void insertChainingSwitch(CompilationUnit *cUnit)
3262{
3263 ArmLIR *branch = newLIR0(cUnit, kThumbBUncond);
3264 newLIR2(cUnit, kThumbOrr, r0, r0);
3265 ArmLIR *target = newLIR0(cUnit, kArmPseudoTargetLabel);
3266 target->defMask = ENCODE_ALL;
3267 branch->generic.target = (LIR *) target;
3268}
3269
Ben Cheng1efc9c52009-06-08 18:25:27 -07003270/* Chaining cell for code that may need warmup. */
3271static void handleNormalChainingCell(CompilationUnit *cUnit,
3272 unsigned int offset)
Ben Chengba4fc8b2009-06-01 13:00:29 -07003273{
Ben Cheng11d8f142010-03-24 15:24:19 -07003274 /*
3275 * Use raw instruction constructors to guarantee that the generated
3276 * instructions fit the predefined cell size.
3277 */
Bill Buzbeebd047242010-05-13 13:02:53 -07003278 insertChainingSwitch(cUnit);
Ben Cheng11d8f142010-03-24 15:24:19 -07003279 newLIR3(cUnit, kThumbLdrRRI5, r0, rGLUE,
3280 offsetof(InterpState,
3281 jitToInterpEntries.dvmJitToInterpNormal) >> 2);
3282 newLIR1(cUnit, kThumbBlxR, r0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003283 addWordData(cUnit, (int) (cUnit->method->insns + offset), true);
3284}
3285
3286/*
Ben Cheng1efc9c52009-06-08 18:25:27 -07003287 * Chaining cell for instructions that immediately following already translated
3288 * code.
Ben Chengba4fc8b2009-06-01 13:00:29 -07003289 */
Ben Cheng1efc9c52009-06-08 18:25:27 -07003290static void handleHotChainingCell(CompilationUnit *cUnit,
3291 unsigned int offset)
Ben Chengba4fc8b2009-06-01 13:00:29 -07003292{
Ben Cheng11d8f142010-03-24 15:24:19 -07003293 /*
3294 * Use raw instruction constructors to guarantee that the generated
3295 * instructions fit the predefined cell size.
3296 */
Bill Buzbeebd047242010-05-13 13:02:53 -07003297 insertChainingSwitch(cUnit);
Ben Cheng11d8f142010-03-24 15:24:19 -07003298 newLIR3(cUnit, kThumbLdrRRI5, r0, rGLUE,
3299 offsetof(InterpState,
3300 jitToInterpEntries.dvmJitToInterpTraceSelect) >> 2);
3301 newLIR1(cUnit, kThumbBlxR, r0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003302 addWordData(cUnit, (int) (cUnit->method->insns + offset), true);
3303}
3304
Bill Buzbee9c4b7c82009-09-10 10:10:38 -07003305#if defined(WITH_SELF_VERIFICATION) || defined(WITH_JIT_TUNING)
Jeff Hao97319a82009-08-12 16:57:15 -07003306/* Chaining cell for branches that branch back into the same basic block */
3307static void handleBackwardBranchChainingCell(CompilationUnit *cUnit,
3308 unsigned int offset)
3309{
Ben Cheng11d8f142010-03-24 15:24:19 -07003310 /*
3311 * Use raw instruction constructors to guarantee that the generated
3312 * instructions fit the predefined cell size.
3313 */
Bill Buzbeebd047242010-05-13 13:02:53 -07003314 insertChainingSwitch(cUnit);
Bill Buzbee9c4b7c82009-09-10 10:10:38 -07003315#if defined(WITH_SELF_VERIFICATION)
Bill Buzbee1465db52009-09-23 17:17:35 -07003316 newLIR3(cUnit, kThumbLdrRRI5, r0, rGLUE,
Ben Cheng40094c12010-02-24 20:58:44 -08003317 offsetof(InterpState,
3318 jitToInterpEntries.dvmJitToInterpBackwardBranch) >> 2);
Bill Buzbee9c4b7c82009-09-10 10:10:38 -07003319#else
Bill Buzbee1465db52009-09-23 17:17:35 -07003320 newLIR3(cUnit, kThumbLdrRRI5, r0, rGLUE,
Bill Buzbee9c4b7c82009-09-10 10:10:38 -07003321 offsetof(InterpState, jitToInterpEntries.dvmJitToInterpNormal) >> 2);
3322#endif
Bill Buzbee1465db52009-09-23 17:17:35 -07003323 newLIR1(cUnit, kThumbBlxR, r0);
Jeff Hao97319a82009-08-12 16:57:15 -07003324 addWordData(cUnit, (int) (cUnit->method->insns + offset), true);
3325}
3326
3327#endif
Ben Chengba4fc8b2009-06-01 13:00:29 -07003328/* Chaining cell for monomorphic method invocations. */
Ben Cheng38329f52009-07-07 14:19:20 -07003329static void handleInvokeSingletonChainingCell(CompilationUnit *cUnit,
3330 const Method *callee)
Ben Chengba4fc8b2009-06-01 13:00:29 -07003331{
Ben Cheng11d8f142010-03-24 15:24:19 -07003332 /*
3333 * Use raw instruction constructors to guarantee that the generated
3334 * instructions fit the predefined cell size.
3335 */
Bill Buzbeebd047242010-05-13 13:02:53 -07003336 insertChainingSwitch(cUnit);
Ben Cheng11d8f142010-03-24 15:24:19 -07003337 newLIR3(cUnit, kThumbLdrRRI5, r0, rGLUE,
3338 offsetof(InterpState,
3339 jitToInterpEntries.dvmJitToInterpTraceSelect) >> 2);
3340 newLIR1(cUnit, kThumbBlxR, r0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003341 addWordData(cUnit, (int) (callee->insns), true);
3342}
3343
Ben Cheng38329f52009-07-07 14:19:20 -07003344/* Chaining cell for monomorphic method invocations. */
3345static void handleInvokePredictedChainingCell(CompilationUnit *cUnit)
3346{
3347
3348 /* Should not be executed in the initial state */
3349 addWordData(cUnit, PREDICTED_CHAIN_BX_PAIR_INIT, true);
3350 /* To be filled: class */
3351 addWordData(cUnit, PREDICTED_CHAIN_CLAZZ_INIT, true);
3352 /* To be filled: method */
3353 addWordData(cUnit, PREDICTED_CHAIN_METHOD_INIT, true);
3354 /*
3355 * Rechain count. The initial value of 0 here will trigger chaining upon
3356 * the first invocation of this callsite.
3357 */
3358 addWordData(cUnit, PREDICTED_CHAIN_COUNTER_INIT, true);
3359}
3360
Ben Chengba4fc8b2009-06-01 13:00:29 -07003361/* Load the Dalvik PC into r0 and jump to the specified target */
3362static void handlePCReconstruction(CompilationUnit *cUnit,
Bill Buzbee89efc3d2009-07-28 11:22:22 -07003363 ArmLIR *targetLabel)
Ben Chengba4fc8b2009-06-01 13:00:29 -07003364{
Bill Buzbee89efc3d2009-07-28 11:22:22 -07003365 ArmLIR **pcrLabel =
3366 (ArmLIR **) cUnit->pcReconstructionList.elemList;
Ben Chengba4fc8b2009-06-01 13:00:29 -07003367 int numElems = cUnit->pcReconstructionList.numUsed;
3368 int i;
3369 for (i = 0; i < numElems; i++) {
3370 dvmCompilerAppendLIR(cUnit, (LIR *) pcrLabel[i]);
3371 /* r0 = dalvik PC */
3372 loadConstant(cUnit, r0, pcrLabel[i]->operands[0]);
3373 genUnconditionalBranch(cUnit, targetLabel);
3374 }
3375}
3376
Bill Buzbee1465db52009-09-23 17:17:35 -07003377static char *extendedMIROpNames[kMirOpLast - kMirOpFirst] = {
3378 "kMirOpPhi",
3379 "kMirOpNullNRangeUpCheck",
3380 "kMirOpNullNRangeDownCheck",
3381 "kMirOpLowerBound",
3382 "kMirOpPunt",
Ben Cheng4238ec22009-08-24 16:32:22 -07003383};
3384
3385/*
3386 * vA = arrayReg;
3387 * vB = idxReg;
3388 * vC = endConditionReg;
3389 * arg[0] = maxC
3390 * arg[1] = minC
3391 * arg[2] = loopBranchConditionCode
3392 */
3393static void genHoistedChecksForCountUpLoop(CompilationUnit *cUnit, MIR *mir)
3394{
Bill Buzbee1465db52009-09-23 17:17:35 -07003395 /*
3396 * NOTE: these synthesized blocks don't have ssa names assigned
3397 * for Dalvik registers. However, because they dominate the following
3398 * blocks we can simply use the Dalvik name w/ subscript 0 as the
3399 * ssa name.
3400 */
Ben Cheng4238ec22009-08-24 16:32:22 -07003401 DecodedInstruction *dInsn = &mir->dalvikInsn;
3402 const int lenOffset = offsetof(ArrayObject, length);
Ben Cheng4238ec22009-08-24 16:32:22 -07003403 const int maxC = dInsn->arg[0];
Bill Buzbee1465db52009-09-23 17:17:35 -07003404 int regLength;
3405 RegLocation rlArray = cUnit->regLocation[mir->dalvikInsn.vA];
3406 RegLocation rlIdxEnd = cUnit->regLocation[mir->dalvikInsn.vC];
Ben Cheng4238ec22009-08-24 16:32:22 -07003407
3408 /* regArray <- arrayRef */
Bill Buzbee1465db52009-09-23 17:17:35 -07003409 rlArray = loadValue(cUnit, rlArray, kCoreReg);
3410 rlIdxEnd = loadValue(cUnit, rlIdxEnd, kCoreReg);
3411 genRegImmCheck(cUnit, kArmCondEq, rlArray.lowReg, 0, 0,
Ben Cheng4238ec22009-08-24 16:32:22 -07003412 (ArmLIR *) cUnit->loopAnalysis->branchToPCR);
3413
3414 /* regLength <- len(arrayRef) */
Bill Buzbeec6f10662010-02-09 11:16:15 -08003415 regLength = dvmCompilerAllocTemp(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07003416 loadWordDisp(cUnit, rlArray.lowReg, lenOffset, regLength);
Ben Cheng4238ec22009-08-24 16:32:22 -07003417
3418 int delta = maxC;
3419 /*
3420 * If the loop end condition is ">=" instead of ">", then the largest value
3421 * of the index is "endCondition - 1".
3422 */
3423 if (dInsn->arg[2] == OP_IF_GE) {
3424 delta--;
3425 }
3426
3427 if (delta) {
Bill Buzbeec6f10662010-02-09 11:16:15 -08003428 int tReg = dvmCompilerAllocTemp(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07003429 opRegRegImm(cUnit, kOpAdd, tReg, rlIdxEnd.lowReg, delta);
3430 rlIdxEnd.lowReg = tReg;
Bill Buzbeec6f10662010-02-09 11:16:15 -08003431 dvmCompilerFreeTemp(cUnit, tReg);
Ben Cheng4238ec22009-08-24 16:32:22 -07003432 }
3433 /* Punt if "regIdxEnd < len(Array)" is false */
Bill Buzbee1465db52009-09-23 17:17:35 -07003434 genRegRegCheck(cUnit, kArmCondGe, rlIdxEnd.lowReg, regLength, 0,
Ben Cheng0fd31e42009-09-03 14:40:16 -07003435 (ArmLIR *) cUnit->loopAnalysis->branchToPCR);
Ben Cheng4238ec22009-08-24 16:32:22 -07003436}
3437
3438/*
3439 * vA = arrayReg;
3440 * vB = idxReg;
3441 * vC = endConditionReg;
3442 * arg[0] = maxC
3443 * arg[1] = minC
3444 * arg[2] = loopBranchConditionCode
3445 */
3446static void genHoistedChecksForCountDownLoop(CompilationUnit *cUnit, MIR *mir)
3447{
3448 DecodedInstruction *dInsn = &mir->dalvikInsn;
3449 const int lenOffset = offsetof(ArrayObject, length);
Bill Buzbeec6f10662010-02-09 11:16:15 -08003450 const int regLength = dvmCompilerAllocTemp(cUnit);
Ben Cheng4238ec22009-08-24 16:32:22 -07003451 const int maxC = dInsn->arg[0];
Bill Buzbee1465db52009-09-23 17:17:35 -07003452 RegLocation rlArray = cUnit->regLocation[mir->dalvikInsn.vA];
3453 RegLocation rlIdxInit = cUnit->regLocation[mir->dalvikInsn.vB];
Ben Cheng4238ec22009-08-24 16:32:22 -07003454
3455 /* regArray <- arrayRef */
Bill Buzbee1465db52009-09-23 17:17:35 -07003456 rlArray = loadValue(cUnit, rlArray, kCoreReg);
3457 rlIdxInit = loadValue(cUnit, rlIdxInit, kCoreReg);
3458 genRegImmCheck(cUnit, kArmCondEq, rlArray.lowReg, 0, 0,
Ben Cheng4238ec22009-08-24 16:32:22 -07003459 (ArmLIR *) cUnit->loopAnalysis->branchToPCR);
3460
3461 /* regLength <- len(arrayRef) */
Bill Buzbee1465db52009-09-23 17:17:35 -07003462 loadWordDisp(cUnit, rlArray.lowReg, lenOffset, regLength);
Ben Cheng4238ec22009-08-24 16:32:22 -07003463
3464 if (maxC) {
Bill Buzbeec6f10662010-02-09 11:16:15 -08003465 int tReg = dvmCompilerAllocTemp(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07003466 opRegRegImm(cUnit, kOpAdd, tReg, rlIdxInit.lowReg, maxC);
3467 rlIdxInit.lowReg = tReg;
Bill Buzbeec6f10662010-02-09 11:16:15 -08003468 dvmCompilerFreeTemp(cUnit, tReg);
Ben Cheng4238ec22009-08-24 16:32:22 -07003469 }
3470
3471 /* Punt if "regIdxInit < len(Array)" is false */
Bill Buzbee1465db52009-09-23 17:17:35 -07003472 genRegRegCheck(cUnit, kArmCondGe, rlIdxInit.lowReg, regLength, 0,
Ben Cheng0fd31e42009-09-03 14:40:16 -07003473 (ArmLIR *) cUnit->loopAnalysis->branchToPCR);
Ben Cheng4238ec22009-08-24 16:32:22 -07003474}
3475
3476/*
3477 * vA = idxReg;
3478 * vB = minC;
3479 */
3480static void genHoistedLowerBoundCheck(CompilationUnit *cUnit, MIR *mir)
3481{
3482 DecodedInstruction *dInsn = &mir->dalvikInsn;
Ben Cheng4238ec22009-08-24 16:32:22 -07003483 const int minC = dInsn->vB;
Bill Buzbee1465db52009-09-23 17:17:35 -07003484 RegLocation rlIdx = cUnit->regLocation[mir->dalvikInsn.vA];
Ben Cheng4238ec22009-08-24 16:32:22 -07003485
3486 /* regIdx <- initial index value */
Bill Buzbee1465db52009-09-23 17:17:35 -07003487 rlIdx = loadValue(cUnit, rlIdx, kCoreReg);
Ben Cheng4238ec22009-08-24 16:32:22 -07003488
3489 /* Punt if "regIdxInit + minC >= 0" is false */
Bill Buzbee1465db52009-09-23 17:17:35 -07003490 genRegImmCheck(cUnit, kArmCondLt, rlIdx.lowReg, -minC, 0,
Ben Cheng4238ec22009-08-24 16:32:22 -07003491 (ArmLIR *) cUnit->loopAnalysis->branchToPCR);
3492}
3493
3494/* Extended MIR instructions like PHI */
3495static void handleExtendedMIR(CompilationUnit *cUnit, MIR *mir)
3496{
Bill Buzbee1465db52009-09-23 17:17:35 -07003497 int opOffset = mir->dalvikInsn.opCode - kMirOpFirst;
Ben Cheng4238ec22009-08-24 16:32:22 -07003498 char *msg = dvmCompilerNew(strlen(extendedMIROpNames[opOffset]) + 1,
3499 false);
3500 strcpy(msg, extendedMIROpNames[opOffset]);
Bill Buzbee1465db52009-09-23 17:17:35 -07003501 newLIR1(cUnit, kArmPseudoExtended, (int) msg);
Ben Cheng4238ec22009-08-24 16:32:22 -07003502
3503 switch (mir->dalvikInsn.opCode) {
Bill Buzbee1465db52009-09-23 17:17:35 -07003504 case kMirOpPhi: {
Ben Cheng4238ec22009-08-24 16:32:22 -07003505 char *ssaString = dvmCompilerGetSSAString(cUnit, mir->ssaRep);
Bill Buzbee1465db52009-09-23 17:17:35 -07003506 newLIR1(cUnit, kArmPseudoSSARep, (int) ssaString);
Ben Cheng4238ec22009-08-24 16:32:22 -07003507 break;
3508 }
Bill Buzbee1465db52009-09-23 17:17:35 -07003509 case kMirOpNullNRangeUpCheck: {
Ben Cheng4238ec22009-08-24 16:32:22 -07003510 genHoistedChecksForCountUpLoop(cUnit, mir);
3511 break;
3512 }
Bill Buzbee1465db52009-09-23 17:17:35 -07003513 case kMirOpNullNRangeDownCheck: {
Ben Cheng4238ec22009-08-24 16:32:22 -07003514 genHoistedChecksForCountDownLoop(cUnit, mir);
3515 break;
3516 }
Bill Buzbee1465db52009-09-23 17:17:35 -07003517 case kMirOpLowerBound: {
Ben Cheng4238ec22009-08-24 16:32:22 -07003518 genHoistedLowerBoundCheck(cUnit, mir);
3519 break;
3520 }
Bill Buzbee1465db52009-09-23 17:17:35 -07003521 case kMirOpPunt: {
Ben Cheng4238ec22009-08-24 16:32:22 -07003522 genUnconditionalBranch(cUnit,
3523 (ArmLIR *) cUnit->loopAnalysis->branchToPCR);
3524 break;
3525 }
3526 default:
3527 break;
3528 }
3529}
3530
3531/*
3532 * Create a PC-reconstruction cell for the starting offset of this trace.
3533 * Since the PCR cell is placed near the end of the compiled code which is
3534 * usually out of range for a conditional branch, we put two branches (one
3535 * branch over to the loop body and one layover branch to the actual PCR) at the
3536 * end of the entry block.
3537 */
3538static void setupLoopEntryBlock(CompilationUnit *cUnit, BasicBlock *entry,
3539 ArmLIR *bodyLabel)
3540{
3541 /* Set up the place holder to reconstruct this Dalvik PC */
3542 ArmLIR *pcrLabel = dvmCompilerNew(sizeof(ArmLIR), true);
Ben Chenga4973592010-03-31 11:59:18 -07003543 pcrLabel->opCode = kArmPseudoPCReconstructionCell;
Ben Cheng4238ec22009-08-24 16:32:22 -07003544 pcrLabel->operands[0] =
3545 (int) (cUnit->method->insns + entry->startOffset);
3546 pcrLabel->operands[1] = entry->startOffset;
3547 /* Insert the place holder to the growable list */
3548 dvmInsertGrowableList(&cUnit->pcReconstructionList, pcrLabel);
3549
3550 /*
3551 * Next, create two branches - one branch over to the loop body and the
3552 * other branch to the PCR cell to punt.
3553 */
3554 ArmLIR *branchToBody = dvmCompilerNew(sizeof(ArmLIR), true);
Bill Buzbee1465db52009-09-23 17:17:35 -07003555 branchToBody->opCode = kThumbBUncond;
Ben Cheng4238ec22009-08-24 16:32:22 -07003556 branchToBody->generic.target = (LIR *) bodyLabel;
Ben Chengdcf3e5d2009-09-11 13:42:05 -07003557 setupResourceMasks(branchToBody);
Ben Cheng4238ec22009-08-24 16:32:22 -07003558 cUnit->loopAnalysis->branchToBody = (LIR *) branchToBody;
3559
3560 ArmLIR *branchToPCR = dvmCompilerNew(sizeof(ArmLIR), true);
Bill Buzbee1465db52009-09-23 17:17:35 -07003561 branchToPCR->opCode = kThumbBUncond;
Ben Cheng4238ec22009-08-24 16:32:22 -07003562 branchToPCR->generic.target = (LIR *) pcrLabel;
Ben Chengdcf3e5d2009-09-11 13:42:05 -07003563 setupResourceMasks(branchToPCR);
Ben Cheng4238ec22009-08-24 16:32:22 -07003564 cUnit->loopAnalysis->branchToPCR = (LIR *) branchToPCR;
3565}
3566
Ben Chengd5adae12010-03-26 17:45:28 -07003567#if defined(WITH_SELF_VERIFICATION)
3568static bool selfVerificationPuntOps(MIR *mir)
3569{
3570 DecodedInstruction *decInsn = &mir->dalvikInsn;
3571 OpCode op = decInsn->opCode;
3572 int flags = dexGetInstrFlags(gDvm.instrFlags, op);
3573 /*
3574 * All opcodes that can throw exceptions and use the
3575 * TEMPLATE_THROW_EXCEPTION_COMMON template should be excluded in the trace
3576 * under self-verification mode.
3577 */
3578 return (op == OP_MONITOR_ENTER || op == OP_MONITOR_EXIT ||
3579 op == OP_NEW_INSTANCE || op == OP_NEW_ARRAY ||
3580 op == OP_CHECK_CAST || op == OP_MOVE_EXCEPTION ||
3581 op == OP_FILL_ARRAY_DATA || op == OP_EXECUTE_INLINE ||
3582 op == OP_EXECUTE_INLINE_RANGE ||
3583 (flags & kInstrInvoke));
3584}
3585#endif
3586
Ben Chengba4fc8b2009-06-01 13:00:29 -07003587void dvmCompilerMIR2LIR(CompilationUnit *cUnit)
3588{
3589 /* Used to hold the labels of each block */
Bill Buzbee89efc3d2009-07-28 11:22:22 -07003590 ArmLIR *labelList =
3591 dvmCompilerNew(sizeof(ArmLIR) * cUnit->numBlocks, true);
Ben Chengcec26f62010-01-15 15:29:33 -08003592 GrowableList chainingListByType[kChainingCellGap];
Ben Chengba4fc8b2009-06-01 13:00:29 -07003593 int i;
3594
3595 /*
Ben Cheng38329f52009-07-07 14:19:20 -07003596 * Initialize various types chaining lists.
Ben Chengba4fc8b2009-06-01 13:00:29 -07003597 */
Ben Chengcec26f62010-01-15 15:29:33 -08003598 for (i = 0; i < kChainingCellGap; i++) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07003599 dvmInitGrowableList(&chainingListByType[i], 2);
3600 }
3601
3602 BasicBlock **blockList = cUnit->blockList;
3603
Bill Buzbee6e963e12009-06-17 16:56:19 -07003604 if (cUnit->executionCount) {
3605 /*
3606 * Reserve 6 bytes at the beginning of the trace
3607 * +----------------------------+
3608 * | execution count (4 bytes) |
3609 * +----------------------------+
3610 * | chain cell offset (2 bytes)|
3611 * +----------------------------+
3612 * ...and then code to increment the execution
3613 * count:
3614 * mov r0, pc @ move adr of "mov r0,pc" + 4 to r0
3615 * sub r0, #10 @ back up to addr of executionCount
3616 * ldr r1, [r0]
3617 * add r1, #1
3618 * str r1, [r0]
3619 */
Bill Buzbee1465db52009-09-23 17:17:35 -07003620 newLIR1(cUnit, kArm16BitData, 0);
3621 newLIR1(cUnit, kArm16BitData, 0);
Ben Chengcc6600c2009-06-22 14:45:16 -07003622 cUnit->chainCellOffsetLIR =
Bill Buzbee1465db52009-09-23 17:17:35 -07003623 (LIR *) newLIR1(cUnit, kArm16BitData, CHAIN_CELL_OFFSET_TAG);
Bill Buzbee6e963e12009-06-17 16:56:19 -07003624 cUnit->headerSize = 6;
Bill Buzbee270c1d62009-08-13 16:58:07 -07003625 /* Thumb instruction used directly here to ensure correct size */
Bill Buzbee1465db52009-09-23 17:17:35 -07003626 newLIR2(cUnit, kThumbMovRR_H2L, r0, rpc);
3627 newLIR2(cUnit, kThumbSubRI8, r0, 10);
3628 newLIR3(cUnit, kThumbLdrRRI5, r1, r0, 0);
3629 newLIR2(cUnit, kThumbAddRI8, r1, 1);
3630 newLIR3(cUnit, kThumbStrRRI5, r1, r0, 0);
Bill Buzbee6e963e12009-06-17 16:56:19 -07003631 } else {
3632 /* Just reserve 2 bytes for the chain cell offset */
Ben Chengcc6600c2009-06-22 14:45:16 -07003633 cUnit->chainCellOffsetLIR =
Bill Buzbee1465db52009-09-23 17:17:35 -07003634 (LIR *) newLIR1(cUnit, kArm16BitData, CHAIN_CELL_OFFSET_TAG);
Bill Buzbee6e963e12009-06-17 16:56:19 -07003635 cUnit->headerSize = 2;
3636 }
Ben Cheng1efc9c52009-06-08 18:25:27 -07003637
Ben Chengba4fc8b2009-06-01 13:00:29 -07003638 /* Handle the content in each basic block */
3639 for (i = 0; i < cUnit->numBlocks; i++) {
3640 blockList[i]->visited = true;
3641 MIR *mir;
3642
3643 labelList[i].operands[0] = blockList[i]->startOffset;
3644
Ben Chengcec26f62010-01-15 15:29:33 -08003645 if (blockList[i]->blockType >= kChainingCellGap) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07003646 /*
3647 * Append the label pseudo LIR first. Chaining cells will be handled
3648 * separately afterwards.
3649 */
3650 dvmCompilerAppendLIR(cUnit, (LIR *) &labelList[i]);
3651 }
3652
Bill Buzbee1465db52009-09-23 17:17:35 -07003653 if (blockList[i]->blockType == kEntryBlock) {
Ben Chenga4973592010-03-31 11:59:18 -07003654 labelList[i].opCode = kArmPseudoEntryBlock;
Ben Cheng4238ec22009-08-24 16:32:22 -07003655 if (blockList[i]->firstMIRInsn == NULL) {
3656 continue;
3657 } else {
3658 setupLoopEntryBlock(cUnit, blockList[i],
3659 &labelList[blockList[i]->fallThrough->id]);
3660 }
Bill Buzbee1465db52009-09-23 17:17:35 -07003661 } else if (blockList[i]->blockType == kExitBlock) {
Ben Chenga4973592010-03-31 11:59:18 -07003662 labelList[i].opCode = kArmPseudoExitBlock;
Ben Cheng4238ec22009-08-24 16:32:22 -07003663 goto gen_fallthrough;
Bill Buzbee1465db52009-09-23 17:17:35 -07003664 } else if (blockList[i]->blockType == kDalvikByteCode) {
3665 labelList[i].opCode = kArmPseudoNormalBlockLabel;
Ben Chenge9695e52009-06-16 16:11:47 -07003666 /* Reset the register state */
Bill Buzbeec6f10662010-02-09 11:16:15 -08003667 dvmCompilerResetRegPool(cUnit);
3668 dvmCompilerClobberAllRegs(cUnit);
3669 dvmCompilerResetNullCheck(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003670 } else {
3671 switch (blockList[i]->blockType) {
Bill Buzbee1465db52009-09-23 17:17:35 -07003672 case kChainingCellNormal:
Ben Chenga4973592010-03-31 11:59:18 -07003673 labelList[i].opCode = kArmPseudoChainingCellNormal;
Ben Chengba4fc8b2009-06-01 13:00:29 -07003674 /* handle the codegen later */
3675 dvmInsertGrowableList(
Bill Buzbee1465db52009-09-23 17:17:35 -07003676 &chainingListByType[kChainingCellNormal], (void *) i);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003677 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07003678 case kChainingCellInvokeSingleton:
Ben Cheng38329f52009-07-07 14:19:20 -07003679 labelList[i].opCode =
Ben Chenga4973592010-03-31 11:59:18 -07003680 kArmPseudoChainingCellInvokeSingleton;
Ben Chengba4fc8b2009-06-01 13:00:29 -07003681 labelList[i].operands[0] =
3682 (int) blockList[i]->containingMethod;
3683 /* handle the codegen later */
3684 dvmInsertGrowableList(
Bill Buzbee1465db52009-09-23 17:17:35 -07003685 &chainingListByType[kChainingCellInvokeSingleton],
Ben Cheng38329f52009-07-07 14:19:20 -07003686 (void *) i);
3687 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07003688 case kChainingCellInvokePredicted:
Ben Cheng38329f52009-07-07 14:19:20 -07003689 labelList[i].opCode =
Ben Chenga4973592010-03-31 11:59:18 -07003690 kArmPseudoChainingCellInvokePredicted;
Ben Cheng38329f52009-07-07 14:19:20 -07003691 /* handle the codegen later */
3692 dvmInsertGrowableList(
Bill Buzbee1465db52009-09-23 17:17:35 -07003693 &chainingListByType[kChainingCellInvokePredicted],
Ben Cheng38329f52009-07-07 14:19:20 -07003694 (void *) i);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003695 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07003696 case kChainingCellHot:
Ben Chengba4fc8b2009-06-01 13:00:29 -07003697 labelList[i].opCode =
Ben Chenga4973592010-03-31 11:59:18 -07003698 kArmPseudoChainingCellHot;
Ben Chengba4fc8b2009-06-01 13:00:29 -07003699 /* handle the codegen later */
3700 dvmInsertGrowableList(
Bill Buzbee1465db52009-09-23 17:17:35 -07003701 &chainingListByType[kChainingCellHot],
Ben Chengba4fc8b2009-06-01 13:00:29 -07003702 (void *) i);
3703 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07003704 case kPCReconstruction:
Ben Chengba4fc8b2009-06-01 13:00:29 -07003705 /* Make sure exception handling block is next */
3706 labelList[i].opCode =
Ben Chenga4973592010-03-31 11:59:18 -07003707 kArmPseudoPCReconstructionBlockLabel;
Ben Chengba4fc8b2009-06-01 13:00:29 -07003708 assert (i == cUnit->numBlocks - 2);
3709 handlePCReconstruction(cUnit, &labelList[i+1]);
3710 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07003711 case kExceptionHandling:
3712 labelList[i].opCode = kArmPseudoEHBlockLabel;
Ben Chengba4fc8b2009-06-01 13:00:29 -07003713 if (cUnit->pcReconstructionList.numUsed) {
Bill Buzbee270c1d62009-08-13 16:58:07 -07003714 loadWordDisp(cUnit, rGLUE, offsetof(InterpState,
3715 jitToInterpEntries.dvmJitToInterpPunt),
3716 r1);
Bill Buzbee1465db52009-09-23 17:17:35 -07003717 opReg(cUnit, kOpBlx, r1);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003718 }
3719 break;
Bill Buzbee9c4b7c82009-09-10 10:10:38 -07003720#if defined(WITH_SELF_VERIFICATION) || defined(WITH_JIT_TUNING)
Bill Buzbee1465db52009-09-23 17:17:35 -07003721 case kChainingCellBackwardBranch:
Jeff Hao97319a82009-08-12 16:57:15 -07003722 labelList[i].opCode =
Ben Chenga4973592010-03-31 11:59:18 -07003723 kArmPseudoChainingCellBackwardBranch;
Jeff Hao97319a82009-08-12 16:57:15 -07003724 /* handle the codegen later */
3725 dvmInsertGrowableList(
Bill Buzbee1465db52009-09-23 17:17:35 -07003726 &chainingListByType[kChainingCellBackwardBranch],
Jeff Hao97319a82009-08-12 16:57:15 -07003727 (void *) i);
3728 break;
3729#endif
Ben Chengba4fc8b2009-06-01 13:00:29 -07003730 default:
3731 break;
3732 }
3733 continue;
3734 }
Ben Chenge9695e52009-06-16 16:11:47 -07003735
Bill Buzbee89efc3d2009-07-28 11:22:22 -07003736 ArmLIR *headLIR = NULL;
Ben Chenge9695e52009-06-16 16:11:47 -07003737
Ben Chengba4fc8b2009-06-01 13:00:29 -07003738 for (mir = blockList[i]->firstMIRInsn; mir; mir = mir->next) {
Bill Buzbee1465db52009-09-23 17:17:35 -07003739
Bill Buzbeec6f10662010-02-09 11:16:15 -08003740 dvmCompilerResetRegPool(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07003741 if (gDvmJit.disableOpt & (1 << kTrackLiveTemps)) {
Bill Buzbeec6f10662010-02-09 11:16:15 -08003742 dvmCompilerClobberAllRegs(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07003743 }
3744
3745 if (gDvmJit.disableOpt & (1 << kSuppressLoads)) {
Bill Buzbeec6f10662010-02-09 11:16:15 -08003746 dvmCompilerResetDefTracking(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07003747 }
3748
3749 if (mir->dalvikInsn.opCode >= kMirOpFirst) {
Ben Cheng4238ec22009-08-24 16:32:22 -07003750 handleExtendedMIR(cUnit, mir);
3751 continue;
3752 }
3753
Bill Buzbee1465db52009-09-23 17:17:35 -07003754
Ben Chengba4fc8b2009-06-01 13:00:29 -07003755 OpCode dalvikOpCode = mir->dalvikInsn.opCode;
3756 InstructionFormat dalvikFormat =
3757 dexGetInstrFormat(gDvm.instrFormat, dalvikOpCode);
Bill Buzbee89efc3d2009-07-28 11:22:22 -07003758 ArmLIR *boundaryLIR =
Ben Chenga4973592010-03-31 11:59:18 -07003759 newLIR2(cUnit, kArmPseudoDalvikByteCodeBoundary,
Ben Chengccd6c012009-10-15 14:52:45 -07003760 mir->offset,
3761 (int) dvmCompilerGetDalvikDisassembly(&mir->dalvikInsn)
3762 );
Ben Cheng4238ec22009-08-24 16:32:22 -07003763 if (mir->ssaRep) {
3764 char *ssaString = dvmCompilerGetSSAString(cUnit, mir->ssaRep);
Bill Buzbee1465db52009-09-23 17:17:35 -07003765 newLIR1(cUnit, kArmPseudoSSARep, (int) ssaString);
Ben Cheng4238ec22009-08-24 16:32:22 -07003766 }
3767
Ben Chenge9695e52009-06-16 16:11:47 -07003768 /* Remember the first LIR for this block */
3769 if (headLIR == NULL) {
3770 headLIR = boundaryLIR;
Ben Chengd7d426a2009-09-22 11:23:36 -07003771 /* Set the first boundaryLIR as a scheduling barrier */
3772 headLIR->defMask = ENCODE_ALL;
Ben Chenge9695e52009-06-16 16:11:47 -07003773 }
Ben Cheng4238ec22009-08-24 16:32:22 -07003774
Ben Chengba4fc8b2009-06-01 13:00:29 -07003775 bool notHandled;
3776 /*
3777 * Debugging: screen the opcode first to see if it is in the
3778 * do[-not]-compile list
3779 */
3780 bool singleStepMe =
3781 gDvmJit.includeSelectedOp !=
3782 ((gDvmJit.opList[dalvikOpCode >> 3] &
3783 (1 << (dalvikOpCode & 0x7))) !=
3784 0);
Ben Chengd5adae12010-03-26 17:45:28 -07003785#if defined(WITH_SELF_VERIFICATION)
3786 if (singleStepMe == false) {
3787 singleStepMe = selfVerificationPuntOps(mir);
3788 }
3789#endif
Ben Chengba4fc8b2009-06-01 13:00:29 -07003790 if (singleStepMe || cUnit->allSingleStep) {
3791 notHandled = false;
3792 genInterpSingleStep(cUnit, mir);
3793 } else {
3794 opcodeCoverage[dalvikOpCode]++;
3795 switch (dalvikFormat) {
3796 case kFmt10t:
3797 case kFmt20t:
3798 case kFmt30t:
3799 notHandled = handleFmt10t_Fmt20t_Fmt30t(cUnit,
3800 mir, blockList[i], labelList);
3801 break;
3802 case kFmt10x:
3803 notHandled = handleFmt10x(cUnit, mir);
3804 break;
3805 case kFmt11n:
3806 case kFmt31i:
3807 notHandled = handleFmt11n_Fmt31i(cUnit, mir);
3808 break;
3809 case kFmt11x:
3810 notHandled = handleFmt11x(cUnit, mir);
3811 break;
3812 case kFmt12x:
3813 notHandled = handleFmt12x(cUnit, mir);
3814 break;
3815 case kFmt20bc:
3816 notHandled = handleFmt20bc(cUnit, mir);
3817 break;
3818 case kFmt21c:
3819 case kFmt31c:
3820 notHandled = handleFmt21c_Fmt31c(cUnit, mir);
3821 break;
3822 case kFmt21h:
3823 notHandled = handleFmt21h(cUnit, mir);
3824 break;
3825 case kFmt21s:
3826 notHandled = handleFmt21s(cUnit, mir);
3827 break;
3828 case kFmt21t:
3829 notHandled = handleFmt21t(cUnit, mir, blockList[i],
3830 labelList);
3831 break;
3832 case kFmt22b:
3833 case kFmt22s:
3834 notHandled = handleFmt22b_Fmt22s(cUnit, mir);
3835 break;
3836 case kFmt22c:
3837 notHandled = handleFmt22c(cUnit, mir);
3838 break;
3839 case kFmt22cs:
3840 notHandled = handleFmt22cs(cUnit, mir);
3841 break;
3842 case kFmt22t:
3843 notHandled = handleFmt22t(cUnit, mir, blockList[i],
3844 labelList);
3845 break;
3846 case kFmt22x:
3847 case kFmt32x:
3848 notHandled = handleFmt22x_Fmt32x(cUnit, mir);
3849 break;
3850 case kFmt23x:
3851 notHandled = handleFmt23x(cUnit, mir);
3852 break;
3853 case kFmt31t:
3854 notHandled = handleFmt31t(cUnit, mir);
3855 break;
3856 case kFmt3rc:
3857 case kFmt35c:
3858 notHandled = handleFmt35c_3rc(cUnit, mir, blockList[i],
3859 labelList);
3860 break;
3861 case kFmt3rms:
3862 case kFmt35ms:
3863 notHandled = handleFmt35ms_3rms(cUnit, mir,blockList[i],
3864 labelList);
3865 break;
3866 case kFmt3inline:
Andy McFaddenb0a05412009-11-19 10:23:41 -08003867 case kFmt3rinline:
Bill Buzbeece46c942009-11-20 15:41:34 -08003868 notHandled = handleExecuteInline(cUnit, mir);
Andy McFaddenb0a05412009-11-19 10:23:41 -08003869 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07003870 case kFmt51l:
3871 notHandled = handleFmt51l(cUnit, mir);
3872 break;
3873 default:
3874 notHandled = true;
3875 break;
3876 }
3877 }
3878 if (notHandled) {
3879 LOGE("%#06x: Opcode 0x%x (%s) / Fmt %d not handled\n",
3880 mir->offset,
3881 dalvikOpCode, getOpcodeName(dalvikOpCode),
3882 dalvikFormat);
Bill Buzbeefc519dc2010-03-06 23:30:57 -08003883 dvmCompilerAbort(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003884 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07003885 }
3886 }
Ben Cheng4238ec22009-08-24 16:32:22 -07003887
Bill Buzbee1465db52009-09-23 17:17:35 -07003888 if (blockList[i]->blockType == kEntryBlock) {
Ben Cheng4238ec22009-08-24 16:32:22 -07003889 dvmCompilerAppendLIR(cUnit,
3890 (LIR *) cUnit->loopAnalysis->branchToBody);
3891 dvmCompilerAppendLIR(cUnit,
3892 (LIR *) cUnit->loopAnalysis->branchToPCR);
3893 }
3894
3895 if (headLIR) {
3896 /*
3897 * Eliminate redundant loads/stores and delay stores into later
3898 * slots
3899 */
3900 dvmCompilerApplyLocalOptimizations(cUnit, (LIR *) headLIR,
3901 cUnit->lastLIRInsn);
3902 }
3903
3904gen_fallthrough:
Ben Cheng1efc9c52009-06-08 18:25:27 -07003905 /*
3906 * Check if the block is terminated due to trace length constraint -
3907 * insert an unconditional branch to the chaining cell.
3908 */
3909 if (blockList[i]->needFallThroughBranch) {
3910 genUnconditionalBranch(cUnit,
3911 &labelList[blockList[i]->fallThrough->id]);
3912 }
3913
Ben Chengba4fc8b2009-06-01 13:00:29 -07003914 }
3915
Ben Chenge9695e52009-06-16 16:11:47 -07003916 /* Handle the chaining cells in predefined order */
Ben Chengcec26f62010-01-15 15:29:33 -08003917 for (i = 0; i < kChainingCellGap; i++) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07003918 size_t j;
3919 int *blockIdList = (int *) chainingListByType[i].elemList;
3920
3921 cUnit->numChainingCells[i] = chainingListByType[i].numUsed;
3922
3923 /* No chaining cells of this type */
3924 if (cUnit->numChainingCells[i] == 0)
3925 continue;
3926
3927 /* Record the first LIR for a new type of chaining cell */
3928 cUnit->firstChainingLIR[i] = (LIR *) &labelList[blockIdList[0]];
3929
3930 for (j = 0; j < chainingListByType[i].numUsed; j++) {
3931 int blockId = blockIdList[j];
3932
3933 /* Align this chaining cell first */
Bill Buzbee1465db52009-09-23 17:17:35 -07003934 newLIR0(cUnit, kArmPseudoPseudoAlign4);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003935
3936 /* Insert the pseudo chaining instruction */
3937 dvmCompilerAppendLIR(cUnit, (LIR *) &labelList[blockId]);
3938
3939
3940 switch (blockList[blockId]->blockType) {
Bill Buzbee1465db52009-09-23 17:17:35 -07003941 case kChainingCellNormal:
Ben Cheng1efc9c52009-06-08 18:25:27 -07003942 handleNormalChainingCell(cUnit,
Ben Chengba4fc8b2009-06-01 13:00:29 -07003943 blockList[blockId]->startOffset);
3944 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07003945 case kChainingCellInvokeSingleton:
Ben Cheng38329f52009-07-07 14:19:20 -07003946 handleInvokeSingletonChainingCell(cUnit,
Ben Chengba4fc8b2009-06-01 13:00:29 -07003947 blockList[blockId]->containingMethod);
3948 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07003949 case kChainingCellInvokePredicted:
Ben Cheng38329f52009-07-07 14:19:20 -07003950 handleInvokePredictedChainingCell(cUnit);
3951 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07003952 case kChainingCellHot:
Ben Cheng1efc9c52009-06-08 18:25:27 -07003953 handleHotChainingCell(cUnit,
Ben Chengba4fc8b2009-06-01 13:00:29 -07003954 blockList[blockId]->startOffset);
3955 break;
Bill Buzbee9c4b7c82009-09-10 10:10:38 -07003956#if defined(WITH_SELF_VERIFICATION) || defined(WITH_JIT_TUNING)
Bill Buzbee1465db52009-09-23 17:17:35 -07003957 case kChainingCellBackwardBranch:
Jeff Hao97319a82009-08-12 16:57:15 -07003958 handleBackwardBranchChainingCell(cUnit,
3959 blockList[blockId]->startOffset);
3960 break;
3961#endif
Ben Chengba4fc8b2009-06-01 13:00:29 -07003962 default:
Bill Buzbee1465db52009-09-23 17:17:35 -07003963 LOGE("Bad blocktype %d", blockList[blockId]->blockType);
Bill Buzbeefc519dc2010-03-06 23:30:57 -08003964 dvmCompilerAbort(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003965 }
3966 }
3967 }
Ben Chenge9695e52009-06-16 16:11:47 -07003968
Ben Chengcec26f62010-01-15 15:29:33 -08003969 /* Mark the bottom of chaining cells */
3970 cUnit->chainingCellBottom = (LIR *) newLIR0(cUnit, kArmChainingCellBottom);
3971
Ben Cheng6c10a972009-10-29 14:39:18 -07003972 /*
3973 * Generate the branch to the dvmJitToInterpNoChain entry point at the end
3974 * of all chaining cells for the overflow cases.
3975 */
3976 if (cUnit->switchOverflowPad) {
3977 loadConstant(cUnit, r0, (int) cUnit->switchOverflowPad);
3978 loadWordDisp(cUnit, rGLUE, offsetof(InterpState,
3979 jitToInterpEntries.dvmJitToInterpNoChain), r2);
3980 opRegReg(cUnit, kOpAdd, r1, r1);
3981 opRegRegReg(cUnit, kOpAdd, r4PC, r0, r1);
Ben Cheng978738d2010-05-13 13:45:57 -07003982#if defined(WITH_JIT_TUNING)
Ben Cheng6c10a972009-10-29 14:39:18 -07003983 loadConstant(cUnit, r0, kSwitchOverflow);
3984#endif
3985 opReg(cUnit, kOpBlx, r2);
3986 }
3987
Ben Chenge9695e52009-06-16 16:11:47 -07003988 dvmCompilerApplyGlobalOptimizations(cUnit);
jeffhao9e45c0b2010-02-03 10:24:05 -08003989
3990#if defined(WITH_SELF_VERIFICATION)
3991 selfVerificationBranchInsertPass(cUnit);
3992#endif
Ben Chengba4fc8b2009-06-01 13:00:29 -07003993}
3994
3995/* Accept the work and start compiling */
Bill Buzbee716f1202009-07-23 13:22:09 -07003996bool dvmCompilerDoWork(CompilerWorkOrder *work)
Ben Chengba4fc8b2009-06-01 13:00:29 -07003997{
Ben Chengccd6c012009-10-15 14:52:45 -07003998 bool res;
Ben Chengba4fc8b2009-06-01 13:00:29 -07003999
Ben Cheng6999d842010-01-26 16:46:15 -08004000 if (gDvmJit.codeCacheFull) {
Ben Chengccd6c012009-10-15 14:52:45 -07004001 return false;
4002 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07004003
Ben Chengccd6c012009-10-15 14:52:45 -07004004 switch (work->kind) {
4005 case kWorkOrderMethod:
4006 res = dvmCompileMethod(work->info, &work->result);
4007 break;
4008 case kWorkOrderTrace:
4009 /* Start compilation with maximally allowed trace length */
Bill Buzbeefc519dc2010-03-06 23:30:57 -08004010 res = dvmCompileTrace(work->info, JIT_MAX_TRACE_LEN, &work->result,
4011 work->bailPtr);
Ben Chengccd6c012009-10-15 14:52:45 -07004012 break;
4013 case kWorkOrderTraceDebug: {
4014 bool oldPrintMe = gDvmJit.printMe;
4015 gDvmJit.printMe = true;
4016 /* Start compilation with maximally allowed trace length */
Bill Buzbeefc519dc2010-03-06 23:30:57 -08004017 res = dvmCompileTrace(work->info, JIT_MAX_TRACE_LEN, &work->result,
4018 work->bailPtr);
Elliott Hughes672511b2010-04-26 17:40:13 -07004019 gDvmJit.printMe = oldPrintMe;
Ben Chengccd6c012009-10-15 14:52:45 -07004020 break;
4021 }
4022 default:
4023 res = false;
Bill Buzbeefc519dc2010-03-06 23:30:57 -08004024 LOGE("Jit: unknown work order type");
Elliott Hughes672511b2010-04-26 17:40:13 -07004025 assert(0); // Bail if debug build, discard otherwise
Ben Chengccd6c012009-10-15 14:52:45 -07004026 }
4027 return res;
Ben Chengba4fc8b2009-06-01 13:00:29 -07004028}
4029
Ben Chengba4fc8b2009-06-01 13:00:29 -07004030/* Architectural-specific debugging helpers go here */
4031void dvmCompilerArchDump(void)
4032{
4033 /* Print compiled opcode in this VM instance */
4034 int i, start, streak;
4035 char buf[1024];
4036
4037 streak = i = 0;
4038 buf[0] = 0;
4039 while (opcodeCoverage[i] == 0 && i < 256) {
4040 i++;
4041 }
4042 if (i == 256) {
4043 return;
4044 }
4045 for (start = i++, streak = 1; i < 256; i++) {
4046 if (opcodeCoverage[i]) {
4047 streak++;
4048 } else {
4049 if (streak == 1) {
4050 sprintf(buf+strlen(buf), "%x,", start);
4051 } else {
4052 sprintf(buf+strlen(buf), "%x-%x,", start, start + streak - 1);
4053 }
4054 streak = 0;
4055 while (opcodeCoverage[i] == 0 && i < 256) {
4056 i++;
4057 }
4058 if (i < 256) {
4059 streak = 1;
4060 start = i;
4061 }
4062 }
4063 }
4064 if (streak) {
4065 if (streak == 1) {
4066 sprintf(buf+strlen(buf), "%x", start);
4067 } else {
4068 sprintf(buf+strlen(buf), "%x-%x", start, start + streak - 1);
4069 }
4070 }
4071 if (strlen(buf)) {
Ben Cheng8b258bf2009-06-24 17:27:07 -07004072 LOGD("dalvik.vm.jit.op = %s", buf);
Ben Chengba4fc8b2009-06-01 13:00:29 -07004073 }
4074}
Ben Chengd7d426a2009-09-22 11:23:36 -07004075
4076/* Common initialization routine for an architecture family */
4077bool dvmCompilerArchInit()
4078{
4079 int i;
4080
Bill Buzbee1465db52009-09-23 17:17:35 -07004081 for (i = 0; i < kArmLast; i++) {
Ben Chengd7d426a2009-09-22 11:23:36 -07004082 if (EncodingMap[i].opCode != i) {
4083 LOGE("Encoding order for %s is wrong: expecting %d, seeing %d",
4084 EncodingMap[i].name, i, EncodingMap[i].opCode);
Bill Buzbeefc519dc2010-03-06 23:30:57 -08004085 dvmAbort(); // OK to dvmAbort - build error
Ben Chengd7d426a2009-09-22 11:23:36 -07004086 }
4087 }
4088
Ben Cheng5d90c202009-11-22 23:31:11 -08004089 return dvmCompilerArchVariantInit();
4090}
4091
4092void *dvmCompilerGetInterpretTemplate()
4093{
4094 return (void*) ((int)gDvmJit.codeCache +
4095 templateEntryOffsets[TEMPLATE_INTERPRET]);
4096}
4097
4098/* Needed by the ld/st optmizatons */
4099ArmLIR* dvmCompilerRegCopyNoInsert(CompilationUnit *cUnit, int rDest, int rSrc)
4100{
4101 return genRegCopyNoInsert(cUnit, rDest, rSrc);
4102}
4103
4104/* Needed by the register allocator */
4105ArmLIR* dvmCompilerRegCopy(CompilationUnit *cUnit, int rDest, int rSrc)
4106{
4107 return genRegCopy(cUnit, rDest, rSrc);
4108}
4109
4110/* Needed by the register allocator */
4111void dvmCompilerRegCopyWide(CompilationUnit *cUnit, int destLo, int destHi,
4112 int srcLo, int srcHi)
4113{
4114 genRegCopyWide(cUnit, destLo, destHi, srcLo, srcHi);
4115}
4116
4117void dvmCompilerFlushRegImpl(CompilationUnit *cUnit, int rBase,
4118 int displacement, int rSrc, OpSize size)
4119{
4120 storeBaseDisp(cUnit, rBase, displacement, rSrc, size);
4121}
4122
4123void dvmCompilerFlushRegWideImpl(CompilationUnit *cUnit, int rBase,
4124 int displacement, int rSrcLo, int rSrcHi)
4125{
4126 storeBaseDispWide(cUnit, rBase, displacement, rSrcLo, rSrcHi);
Ben Chengd7d426a2009-09-22 11:23:36 -07004127}