blob: a28e4114dce4d430d533e2f70abc525d059a35a5 [file] [log] [blame]
Ben Chengba4fc8b2009-06-01 13:00:29 -07001/*
2 * Copyright (C) 2009 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Bill Buzbee50a6bf22009-07-08 13:08:04 -070017/*
18 * This file contains codegen and support common to all supported
19 * ARM variants. It is included by:
20 *
21 * Codegen-$(TARGET_ARCH_VARIANT).c
22 *
23 * which combines this common code with specific support found in the
24 * applicable directory below this one.
25 */
26
Ben Cheng5d90c202009-11-22 23:31:11 -080027static bool genConversionCall(CompilationUnit *cUnit, MIR *mir, void *funct,
28 int srcSize, int tgtSize)
29{
30 /*
31 * Don't optimize the register usage since it calls out to template
32 * functions
33 */
34 RegLocation rlSrc;
35 RegLocation rlDest;
Bill Buzbeec6f10662010-02-09 11:16:15 -080036 dvmCompilerFlushAllRegs(cUnit); /* Send everything to home location */
Ben Cheng5d90c202009-11-22 23:31:11 -080037 if (srcSize == 1) {
Bill Buzbeec6f10662010-02-09 11:16:15 -080038 rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
Ben Cheng5d90c202009-11-22 23:31:11 -080039 loadValueDirectFixed(cUnit, rlSrc, r0);
40 } else {
Bill Buzbeec6f10662010-02-09 11:16:15 -080041 rlSrc = dvmCompilerGetSrcWide(cUnit, mir, 0, 1);
Ben Cheng5d90c202009-11-22 23:31:11 -080042 loadValueDirectWideFixed(cUnit, rlSrc, r0, r1);
43 }
Ben Chengbd1326d2010-04-02 15:04:53 -070044 LOAD_FUNC_ADDR(cUnit, r2, (int)funct);
Ben Cheng5d90c202009-11-22 23:31:11 -080045 opReg(cUnit, kOpBlx, r2);
Elliott Hughes6a555132010-02-25 15:41:42 -080046 dvmCompilerClobberCallRegs(cUnit);
Ben Cheng5d90c202009-11-22 23:31:11 -080047 if (tgtSize == 1) {
48 RegLocation rlResult;
Bill Buzbeec6f10662010-02-09 11:16:15 -080049 rlDest = dvmCompilerGetDest(cUnit, mir, 0);
50 rlResult = dvmCompilerGetReturn(cUnit);
Ben Cheng5d90c202009-11-22 23:31:11 -080051 storeValue(cUnit, rlDest, rlResult);
52 } else {
53 RegLocation rlResult;
Bill Buzbeec6f10662010-02-09 11:16:15 -080054 rlDest = dvmCompilerGetDestWide(cUnit, mir, 0, 1);
55 rlResult = dvmCompilerGetReturnWide(cUnit);
Ben Cheng5d90c202009-11-22 23:31:11 -080056 storeValueWide(cUnit, rlDest, rlResult);
57 }
58 return false;
59}
Ben Chengba4fc8b2009-06-01 13:00:29 -070060
Ben Chengba4fc8b2009-06-01 13:00:29 -070061
Ben Cheng5d90c202009-11-22 23:31:11 -080062static bool genArithOpFloatPortable(CompilationUnit *cUnit, MIR *mir,
63 RegLocation rlDest, RegLocation rlSrc1,
64 RegLocation rlSrc2)
65{
66 RegLocation rlResult;
67 void* funct;
68
Ben Cheng5d90c202009-11-22 23:31:11 -080069 switch (mir->dalvikInsn.opCode) {
70 case OP_ADD_FLOAT_2ADDR:
71 case OP_ADD_FLOAT:
72 funct = (void*) __aeabi_fadd;
73 break;
74 case OP_SUB_FLOAT_2ADDR:
75 case OP_SUB_FLOAT:
76 funct = (void*) __aeabi_fsub;
77 break;
78 case OP_DIV_FLOAT_2ADDR:
79 case OP_DIV_FLOAT:
80 funct = (void*) __aeabi_fdiv;
81 break;
82 case OP_MUL_FLOAT_2ADDR:
83 case OP_MUL_FLOAT:
84 funct = (void*) __aeabi_fmul;
85 break;
86 case OP_REM_FLOAT_2ADDR:
87 case OP_REM_FLOAT:
88 funct = (void*) fmodf;
89 break;
90 case OP_NEG_FLOAT: {
91 genNegFloat(cUnit, rlDest, rlSrc1);
92 return false;
93 }
94 default:
95 return true;
96 }
Bill Buzbeec6f10662010-02-09 11:16:15 -080097 dvmCompilerFlushAllRegs(cUnit); /* Send everything to home location */
Ben Cheng5d90c202009-11-22 23:31:11 -080098 loadValueDirectFixed(cUnit, rlSrc1, r0);
99 loadValueDirectFixed(cUnit, rlSrc2, r1);
Ben Chengbd1326d2010-04-02 15:04:53 -0700100 LOAD_FUNC_ADDR(cUnit, r2, (int)funct);
Ben Cheng5d90c202009-11-22 23:31:11 -0800101 opReg(cUnit, kOpBlx, r2);
Elliott Hughes6a555132010-02-25 15:41:42 -0800102 dvmCompilerClobberCallRegs(cUnit);
Bill Buzbeec6f10662010-02-09 11:16:15 -0800103 rlResult = dvmCompilerGetReturn(cUnit);
Ben Cheng5d90c202009-11-22 23:31:11 -0800104 storeValue(cUnit, rlDest, rlResult);
105 return false;
106}
107
108static bool genArithOpDoublePortable(CompilationUnit *cUnit, MIR *mir,
109 RegLocation rlDest, RegLocation rlSrc1,
110 RegLocation rlSrc2)
111{
112 RegLocation rlResult;
113 void* funct;
114
Ben Cheng5d90c202009-11-22 23:31:11 -0800115 switch (mir->dalvikInsn.opCode) {
116 case OP_ADD_DOUBLE_2ADDR:
117 case OP_ADD_DOUBLE:
118 funct = (void*) __aeabi_dadd;
119 break;
120 case OP_SUB_DOUBLE_2ADDR:
121 case OP_SUB_DOUBLE:
122 funct = (void*) __aeabi_dsub;
123 break;
124 case OP_DIV_DOUBLE_2ADDR:
125 case OP_DIV_DOUBLE:
126 funct = (void*) __aeabi_ddiv;
127 break;
128 case OP_MUL_DOUBLE_2ADDR:
129 case OP_MUL_DOUBLE:
130 funct = (void*) __aeabi_dmul;
131 break;
132 case OP_REM_DOUBLE_2ADDR:
133 case OP_REM_DOUBLE:
134 funct = (void*) fmod;
135 break;
136 case OP_NEG_DOUBLE: {
137 genNegDouble(cUnit, rlDest, rlSrc1);
138 return false;
139 }
140 default:
141 return true;
142 }
Bill Buzbeec6f10662010-02-09 11:16:15 -0800143 dvmCompilerFlushAllRegs(cUnit); /* Send everything to home location */
Ben Chengbd1326d2010-04-02 15:04:53 -0700144 LOAD_FUNC_ADDR(cUnit, rlr, (int)funct);
Ben Cheng5d90c202009-11-22 23:31:11 -0800145 loadValueDirectWideFixed(cUnit, rlSrc1, r0, r1);
146 loadValueDirectWideFixed(cUnit, rlSrc2, r2, r3);
147 opReg(cUnit, kOpBlx, rlr);
Elliott Hughes6a555132010-02-25 15:41:42 -0800148 dvmCompilerClobberCallRegs(cUnit);
Bill Buzbeec6f10662010-02-09 11:16:15 -0800149 rlResult = dvmCompilerGetReturnWide(cUnit);
Ben Cheng5d90c202009-11-22 23:31:11 -0800150 storeValueWide(cUnit, rlDest, rlResult);
151 return false;
152}
153
154static bool genConversionPortable(CompilationUnit *cUnit, MIR *mir)
155{
156 OpCode opCode = mir->dalvikInsn.opCode;
157
Ben Cheng5d90c202009-11-22 23:31:11 -0800158 switch (opCode) {
159 case OP_INT_TO_FLOAT:
160 return genConversionCall(cUnit, mir, (void*)__aeabi_i2f, 1, 1);
161 case OP_FLOAT_TO_INT:
162 return genConversionCall(cUnit, mir, (void*)__aeabi_f2iz, 1, 1);
163 case OP_DOUBLE_TO_FLOAT:
164 return genConversionCall(cUnit, mir, (void*)__aeabi_d2f, 2, 1);
165 case OP_FLOAT_TO_DOUBLE:
166 return genConversionCall(cUnit, mir, (void*)__aeabi_f2d, 1, 2);
167 case OP_INT_TO_DOUBLE:
168 return genConversionCall(cUnit, mir, (void*)__aeabi_i2d, 1, 2);
169 case OP_DOUBLE_TO_INT:
170 return genConversionCall(cUnit, mir, (void*)__aeabi_d2iz, 2, 1);
171 case OP_FLOAT_TO_LONG:
172 return genConversionCall(cUnit, mir, (void*)dvmJitf2l, 1, 2);
173 case OP_LONG_TO_FLOAT:
174 return genConversionCall(cUnit, mir, (void*)__aeabi_l2f, 2, 1);
175 case OP_DOUBLE_TO_LONG:
176 return genConversionCall(cUnit, mir, (void*)dvmJitd2l, 2, 2);
177 case OP_LONG_TO_DOUBLE:
178 return genConversionCall(cUnit, mir, (void*)__aeabi_l2d, 2, 2);
179 default:
180 return true;
181 }
182 return false;
183}
Ben Chengba4fc8b2009-06-01 13:00:29 -0700184
Jeff Hao97319a82009-08-12 16:57:15 -0700185#if defined(WITH_SELF_VERIFICATION)
jeffhao9e45c0b2010-02-03 10:24:05 -0800186static void selfVerificationBranchInsert(LIR *currentLIR, ArmOpCode opCode,
187 int dest, int src1)
Jeff Hao97319a82009-08-12 16:57:15 -0700188{
jeffhao9e45c0b2010-02-03 10:24:05 -0800189 ArmLIR *insn = dvmCompilerNew(sizeof(ArmLIR), true);
190 insn->opCode = opCode;
191 insn->operands[0] = dest;
192 insn->operands[1] = src1;
193 setupResourceMasks(insn);
194 dvmCompilerInsertLIRBefore(currentLIR, (LIR *) insn);
Jeff Hao97319a82009-08-12 16:57:15 -0700195}
196
jeffhao9e45c0b2010-02-03 10:24:05 -0800197static void selfVerificationBranchInsertPass(CompilationUnit *cUnit)
Jeff Hao97319a82009-08-12 16:57:15 -0700198{
jeffhao9e45c0b2010-02-03 10:24:05 -0800199 ArmLIR *thisLIR;
jeffhao9e45c0b2010-02-03 10:24:05 -0800200 TemplateOpCode opCode = TEMPLATE_MEM_OP_DECODE;
Jeff Hao97319a82009-08-12 16:57:15 -0700201
jeffhao9e45c0b2010-02-03 10:24:05 -0800202 for (thisLIR = (ArmLIR *) cUnit->firstLIRInsn;
203 thisLIR != (ArmLIR *) cUnit->lastLIRInsn;
204 thisLIR = NEXT_LIR(thisLIR)) {
205 if (thisLIR->branchInsertSV) {
206 /* Branch to mem op decode template */
207 selfVerificationBranchInsert((LIR *) thisLIR, kThumbBlx1,
208 (int) gDvmJit.codeCache + templateEntryOffsets[opCode],
209 (int) gDvmJit.codeCache + templateEntryOffsets[opCode]);
210 selfVerificationBranchInsert((LIR *) thisLIR, kThumbBlx2,
211 (int) gDvmJit.codeCache + templateEntryOffsets[opCode],
212 (int) gDvmJit.codeCache + templateEntryOffsets[opCode]);
Jeff Hao97319a82009-08-12 16:57:15 -0700213 }
214 }
Jeff Hao97319a82009-08-12 16:57:15 -0700215}
Jeff Hao97319a82009-08-12 16:57:15 -0700216#endif
217
Bill Buzbeebe6534f2010-03-12 16:01:35 -0800218/* Generate conditional branch instructions */
219static ArmLIR *genConditionalBranch(CompilationUnit *cUnit,
220 ArmConditionCode cond,
221 ArmLIR *target)
222{
223 ArmLIR *branch = opCondBranch(cUnit, cond);
224 branch->generic.target = (LIR *) target;
225 return branch;
226}
227
Ben Chengba4fc8b2009-06-01 13:00:29 -0700228/* Generate a unconditional branch to go to the interpreter */
Bill Buzbee89efc3d2009-07-28 11:22:22 -0700229static inline ArmLIR *genTrap(CompilationUnit *cUnit, int dOffset,
230 ArmLIR *pcrLabel)
Ben Chengba4fc8b2009-06-01 13:00:29 -0700231{
Bill Buzbee1465db52009-09-23 17:17:35 -0700232 ArmLIR *branch = opNone(cUnit, kOpUncondBr);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700233 return genCheckCommon(cUnit, dOffset, branch, pcrLabel);
234}
235
236/* Load a wide field from an object instance */
237static void genIGetWide(CompilationUnit *cUnit, MIR *mir, int fieldOffset)
238{
Bill Buzbeec6f10662010-02-09 11:16:15 -0800239 RegLocation rlObj = dvmCompilerGetSrc(cUnit, mir, 0);
240 RegLocation rlDest = dvmCompilerGetDestWide(cUnit, mir, 0, 1);
Bill Buzbee1465db52009-09-23 17:17:35 -0700241 RegLocation rlResult;
242 rlObj = loadValue(cUnit, rlObj, kCoreReg);
Bill Buzbeec6f10662010-02-09 11:16:15 -0800243 int regPtr = dvmCompilerAllocTemp(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700244
Bill Buzbee1465db52009-09-23 17:17:35 -0700245 assert(rlDest.wide);
Ben Chenge9695e52009-06-16 16:11:47 -0700246
Bill Buzbee1465db52009-09-23 17:17:35 -0700247 genNullCheck(cUnit, rlObj.sRegLow, rlObj.lowReg, mir->offset,
248 NULL);/* null object? */
249 opRegRegImm(cUnit, kOpAdd, regPtr, rlObj.lowReg, fieldOffset);
Bill Buzbeec6f10662010-02-09 11:16:15 -0800250 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kAnyReg, true);
Ben Cheng11d8f142010-03-24 15:24:19 -0700251
252 HEAP_ACCESS_SHADOW(true);
Bill Buzbee1465db52009-09-23 17:17:35 -0700253 loadPair(cUnit, regPtr, rlResult.lowReg, rlResult.highReg);
Ben Cheng11d8f142010-03-24 15:24:19 -0700254 HEAP_ACCESS_SHADOW(false);
255
Bill Buzbeec6f10662010-02-09 11:16:15 -0800256 dvmCompilerFreeTemp(cUnit, regPtr);
Bill Buzbee1465db52009-09-23 17:17:35 -0700257 storeValueWide(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700258}
259
260/* Store a wide field to an object instance */
261static void genIPutWide(CompilationUnit *cUnit, MIR *mir, int fieldOffset)
262{
Bill Buzbeec6f10662010-02-09 11:16:15 -0800263 RegLocation rlSrc = dvmCompilerGetSrcWide(cUnit, mir, 0, 1);
264 RegLocation rlObj = dvmCompilerGetSrc(cUnit, mir, 2);
Bill Buzbee1465db52009-09-23 17:17:35 -0700265 rlObj = loadValue(cUnit, rlObj, kCoreReg);
266 int regPtr;
267 rlSrc = loadValueWide(cUnit, rlSrc, kAnyReg);
268 genNullCheck(cUnit, rlObj.sRegLow, rlObj.lowReg, mir->offset,
269 NULL);/* null object? */
Bill Buzbeec6f10662010-02-09 11:16:15 -0800270 regPtr = dvmCompilerAllocTemp(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -0700271 opRegRegImm(cUnit, kOpAdd, regPtr, rlObj.lowReg, fieldOffset);
Ben Cheng11d8f142010-03-24 15:24:19 -0700272
273 HEAP_ACCESS_SHADOW(true);
Bill Buzbee1465db52009-09-23 17:17:35 -0700274 storePair(cUnit, regPtr, rlSrc.lowReg, rlSrc.highReg);
Ben Cheng11d8f142010-03-24 15:24:19 -0700275 HEAP_ACCESS_SHADOW(false);
276
Bill Buzbeec6f10662010-02-09 11:16:15 -0800277 dvmCompilerFreeTemp(cUnit, regPtr);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700278}
279
280/*
281 * Load a field from an object instance
282 *
Ben Chengba4fc8b2009-06-01 13:00:29 -0700283 */
Bill Buzbee270c1d62009-08-13 16:58:07 -0700284static void genIGet(CompilationUnit *cUnit, MIR *mir, OpSize size,
Ben Chengba4fc8b2009-06-01 13:00:29 -0700285 int fieldOffset)
286{
Bill Buzbee1465db52009-09-23 17:17:35 -0700287 RegLocation rlResult;
Bill Buzbeec6f10662010-02-09 11:16:15 -0800288 RegLocation rlObj = dvmCompilerGetSrc(cUnit, mir, 0);
289 RegLocation rlDest = dvmCompilerGetDest(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -0700290 rlObj = loadValue(cUnit, rlObj, kCoreReg);
Bill Buzbeec6f10662010-02-09 11:16:15 -0800291 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kAnyReg, true);
Bill Buzbee1465db52009-09-23 17:17:35 -0700292 genNullCheck(cUnit, rlObj.sRegLow, rlObj.lowReg, mir->offset,
293 NULL);/* null object? */
Ben Cheng11d8f142010-03-24 15:24:19 -0700294
295 HEAP_ACCESS_SHADOW(true);
Ben Cheng5d90c202009-11-22 23:31:11 -0800296 loadBaseDisp(cUnit, mir, rlObj.lowReg, fieldOffset, rlResult.lowReg,
297 size, rlObj.sRegLow);
Ben Cheng11d8f142010-03-24 15:24:19 -0700298 HEAP_ACCESS_SHADOW(false);
299
Bill Buzbee1465db52009-09-23 17:17:35 -0700300 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700301}
302
303/*
304 * Store a field to an object instance
305 *
Ben Chengba4fc8b2009-06-01 13:00:29 -0700306 */
Bill Buzbee270c1d62009-08-13 16:58:07 -0700307static void genIPut(CompilationUnit *cUnit, MIR *mir, OpSize size,
Ben Chengba4fc8b2009-06-01 13:00:29 -0700308 int fieldOffset)
309{
Bill Buzbeec6f10662010-02-09 11:16:15 -0800310 RegLocation rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
311 RegLocation rlObj = dvmCompilerGetSrc(cUnit, mir, 1);
Bill Buzbee1465db52009-09-23 17:17:35 -0700312 rlObj = loadValue(cUnit, rlObj, kCoreReg);
313 rlSrc = loadValue(cUnit, rlSrc, kAnyReg);
Bill Buzbee1465db52009-09-23 17:17:35 -0700314 genNullCheck(cUnit, rlObj.sRegLow, rlObj.lowReg, mir->offset,
315 NULL);/* null object? */
Ben Cheng11d8f142010-03-24 15:24:19 -0700316
317 HEAP_ACCESS_SHADOW(true);
Bill Buzbee1465db52009-09-23 17:17:35 -0700318 storeBaseDisp(cUnit, rlObj.lowReg, fieldOffset, rlSrc.lowReg, size);
Ben Cheng11d8f142010-03-24 15:24:19 -0700319 HEAP_ACCESS_SHADOW(false);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700320}
321
322
Ben Chengba4fc8b2009-06-01 13:00:29 -0700323/*
324 * Generate array load
Ben Chengba4fc8b2009-06-01 13:00:29 -0700325 */
Bill Buzbee270c1d62009-08-13 16:58:07 -0700326static void genArrayGet(CompilationUnit *cUnit, MIR *mir, OpSize size,
Bill Buzbee1465db52009-09-23 17:17:35 -0700327 RegLocation rlArray, RegLocation rlIndex,
328 RegLocation rlDest, int scale)
Ben Chengba4fc8b2009-06-01 13:00:29 -0700329{
330 int lenOffset = offsetof(ArrayObject, length);
331 int dataOffset = offsetof(ArrayObject, contents);
Bill Buzbee1465db52009-09-23 17:17:35 -0700332 RegLocation rlResult;
333 rlArray = loadValue(cUnit, rlArray, kCoreReg);
334 rlIndex = loadValue(cUnit, rlIndex, kCoreReg);
335 int regPtr;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700336
337 /* null object? */
Ben Cheng4238ec22009-08-24 16:32:22 -0700338 ArmLIR * pcrLabel = NULL;
339
340 if (!(mir->OptimizationFlags & MIR_IGNORE_NULL_CHECK)) {
Bill Buzbee1465db52009-09-23 17:17:35 -0700341 pcrLabel = genNullCheck(cUnit, rlArray.sRegLow,
342 rlArray.lowReg, mir->offset, NULL);
Ben Cheng4238ec22009-08-24 16:32:22 -0700343 }
344
Bill Buzbeec6f10662010-02-09 11:16:15 -0800345 regPtr = dvmCompilerAllocTemp(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -0700346
Ben Cheng4238ec22009-08-24 16:32:22 -0700347 if (!(mir->OptimizationFlags & MIR_IGNORE_RANGE_CHECK)) {
Bill Buzbeec6f10662010-02-09 11:16:15 -0800348 int regLen = dvmCompilerAllocTemp(cUnit);
Ben Cheng4238ec22009-08-24 16:32:22 -0700349 /* Get len */
Bill Buzbee1465db52009-09-23 17:17:35 -0700350 loadWordDisp(cUnit, rlArray.lowReg, lenOffset, regLen);
351 /* regPtr -> array data */
352 opRegRegImm(cUnit, kOpAdd, regPtr, rlArray.lowReg, dataOffset);
353 genBoundsCheck(cUnit, rlIndex.lowReg, regLen, mir->offset,
354 pcrLabel);
Bill Buzbeec6f10662010-02-09 11:16:15 -0800355 dvmCompilerFreeTemp(cUnit, regLen);
Ben Cheng4238ec22009-08-24 16:32:22 -0700356 } else {
Bill Buzbee1465db52009-09-23 17:17:35 -0700357 /* regPtr -> array data */
358 opRegRegImm(cUnit, kOpAdd, regPtr, rlArray.lowReg, dataOffset);
Ben Cheng4238ec22009-08-24 16:32:22 -0700359 }
Bill Buzbee1465db52009-09-23 17:17:35 -0700360 if ((size == kLong) || (size == kDouble)) {
361 if (scale) {
Bill Buzbeec6f10662010-02-09 11:16:15 -0800362 int rNewIndex = dvmCompilerAllocTemp(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -0700363 opRegRegImm(cUnit, kOpLsl, rNewIndex, rlIndex.lowReg, scale);
364 opRegReg(cUnit, kOpAdd, regPtr, rNewIndex);
Bill Buzbeec6f10662010-02-09 11:16:15 -0800365 dvmCompilerFreeTemp(cUnit, rNewIndex);
Bill Buzbee1465db52009-09-23 17:17:35 -0700366 } else {
367 opRegReg(cUnit, kOpAdd, regPtr, rlIndex.lowReg);
368 }
Bill Buzbeec6f10662010-02-09 11:16:15 -0800369 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kAnyReg, true);
Ben Cheng11d8f142010-03-24 15:24:19 -0700370
371 HEAP_ACCESS_SHADOW(true);
Bill Buzbee1465db52009-09-23 17:17:35 -0700372 loadPair(cUnit, regPtr, rlResult.lowReg, rlResult.highReg);
Ben Cheng11d8f142010-03-24 15:24:19 -0700373 HEAP_ACCESS_SHADOW(false);
374
Bill Buzbeec6f10662010-02-09 11:16:15 -0800375 dvmCompilerFreeTemp(cUnit, regPtr);
Bill Buzbee1465db52009-09-23 17:17:35 -0700376 storeValueWide(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700377 } else {
Bill Buzbeec6f10662010-02-09 11:16:15 -0800378 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kAnyReg, true);
Ben Cheng11d8f142010-03-24 15:24:19 -0700379
380 HEAP_ACCESS_SHADOW(true);
Bill Buzbee1465db52009-09-23 17:17:35 -0700381 loadBaseIndexed(cUnit, regPtr, rlIndex.lowReg, rlResult.lowReg,
382 scale, size);
Ben Cheng11d8f142010-03-24 15:24:19 -0700383 HEAP_ACCESS_SHADOW(false);
384
Bill Buzbeec6f10662010-02-09 11:16:15 -0800385 dvmCompilerFreeTemp(cUnit, regPtr);
Bill Buzbee1465db52009-09-23 17:17:35 -0700386 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700387 }
388}
389
Ben Chengba4fc8b2009-06-01 13:00:29 -0700390/*
391 * Generate array store
392 *
Ben Chengba4fc8b2009-06-01 13:00:29 -0700393 */
Bill Buzbee270c1d62009-08-13 16:58:07 -0700394static void genArrayPut(CompilationUnit *cUnit, MIR *mir, OpSize size,
Bill Buzbee1465db52009-09-23 17:17:35 -0700395 RegLocation rlArray, RegLocation rlIndex,
396 RegLocation rlSrc, int scale)
Ben Chengba4fc8b2009-06-01 13:00:29 -0700397{
398 int lenOffset = offsetof(ArrayObject, length);
399 int dataOffset = offsetof(ArrayObject, contents);
400
Bill Buzbee1465db52009-09-23 17:17:35 -0700401 int regPtr;
402 rlArray = loadValue(cUnit, rlArray, kCoreReg);
403 rlIndex = loadValue(cUnit, rlIndex, kCoreReg);
Ben Chenge9695e52009-06-16 16:11:47 -0700404
Bill Buzbeec6f10662010-02-09 11:16:15 -0800405 if (dvmCompilerIsTemp(cUnit, rlArray.lowReg)) {
406 dvmCompilerClobber(cUnit, rlArray.lowReg);
Bill Buzbee1465db52009-09-23 17:17:35 -0700407 regPtr = rlArray.lowReg;
408 } else {
Bill Buzbeec6f10662010-02-09 11:16:15 -0800409 regPtr = dvmCompilerAllocTemp(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -0700410 genRegCopy(cUnit, regPtr, rlArray.lowReg);
411 }
Ben Chenge9695e52009-06-16 16:11:47 -0700412
Ben Cheng1efc9c52009-06-08 18:25:27 -0700413 /* null object? */
Ben Cheng4238ec22009-08-24 16:32:22 -0700414 ArmLIR * pcrLabel = NULL;
415
416 if (!(mir->OptimizationFlags & MIR_IGNORE_NULL_CHECK)) {
Bill Buzbee1465db52009-09-23 17:17:35 -0700417 pcrLabel = genNullCheck(cUnit, rlArray.sRegLow, rlArray.lowReg,
418 mir->offset, NULL);
Ben Cheng4238ec22009-08-24 16:32:22 -0700419 }
420
421 if (!(mir->OptimizationFlags & MIR_IGNORE_RANGE_CHECK)) {
Bill Buzbeec6f10662010-02-09 11:16:15 -0800422 int regLen = dvmCompilerAllocTemp(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -0700423 //NOTE: max live temps(4) here.
Ben Cheng4238ec22009-08-24 16:32:22 -0700424 /* Get len */
Bill Buzbee1465db52009-09-23 17:17:35 -0700425 loadWordDisp(cUnit, rlArray.lowReg, lenOffset, regLen);
426 /* regPtr -> array data */
427 opRegImm(cUnit, kOpAdd, regPtr, dataOffset);
428 genBoundsCheck(cUnit, rlIndex.lowReg, regLen, mir->offset,
429 pcrLabel);
Bill Buzbeec6f10662010-02-09 11:16:15 -0800430 dvmCompilerFreeTemp(cUnit, regLen);
Ben Cheng4238ec22009-08-24 16:32:22 -0700431 } else {
Bill Buzbee1465db52009-09-23 17:17:35 -0700432 /* regPtr -> array data */
433 opRegImm(cUnit, kOpAdd, regPtr, dataOffset);
Ben Cheng4238ec22009-08-24 16:32:22 -0700434 }
Bill Buzbee1465db52009-09-23 17:17:35 -0700435 /* at this point, regPtr points to array, 2 live temps */
Bill Buzbee1465db52009-09-23 17:17:35 -0700436 if ((size == kLong) || (size == kDouble)) {
437 //TODO: need specific wide routine that can handle fp regs
438 if (scale) {
Bill Buzbeec6f10662010-02-09 11:16:15 -0800439 int rNewIndex = dvmCompilerAllocTemp(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -0700440 opRegRegImm(cUnit, kOpLsl, rNewIndex, rlIndex.lowReg, scale);
441 opRegReg(cUnit, kOpAdd, regPtr, rNewIndex);
Bill Buzbeec6f10662010-02-09 11:16:15 -0800442 dvmCompilerFreeTemp(cUnit, rNewIndex);
Bill Buzbee1465db52009-09-23 17:17:35 -0700443 } else {
444 opRegReg(cUnit, kOpAdd, regPtr, rlIndex.lowReg);
445 }
446 rlSrc = loadValueWide(cUnit, rlSrc, kAnyReg);
Ben Cheng11d8f142010-03-24 15:24:19 -0700447
448 HEAP_ACCESS_SHADOW(true);
Bill Buzbee1465db52009-09-23 17:17:35 -0700449 storePair(cUnit, regPtr, rlSrc.lowReg, rlSrc.highReg);
Ben Cheng11d8f142010-03-24 15:24:19 -0700450 HEAP_ACCESS_SHADOW(false);
451
Bill Buzbeec6f10662010-02-09 11:16:15 -0800452 dvmCompilerFreeTemp(cUnit, regPtr);
Bill Buzbee270c1d62009-08-13 16:58:07 -0700453 } else {
Bill Buzbee1465db52009-09-23 17:17:35 -0700454 rlSrc = loadValue(cUnit, rlSrc, kAnyReg);
Ben Cheng11d8f142010-03-24 15:24:19 -0700455
456 HEAP_ACCESS_SHADOW(true);
Bill Buzbee1465db52009-09-23 17:17:35 -0700457 storeBaseIndexed(cUnit, regPtr, rlIndex.lowReg, rlSrc.lowReg,
458 scale, size);
Ben Cheng11d8f142010-03-24 15:24:19 -0700459 HEAP_ACCESS_SHADOW(false);
jeffhao9e45c0b2010-02-03 10:24:05 -0800460 }
Ben Chengba4fc8b2009-06-01 13:00:29 -0700461}
462
Bill Buzbeebe6534f2010-03-12 16:01:35 -0800463/*
464 * Generate array object store
465 * Must use explicit register allocation here because of
466 * call-out to dvmCanPutArrayElement
467 */
468static void genArrayObjectPut(CompilationUnit *cUnit, MIR *mir,
469 RegLocation rlArray, RegLocation rlIndex,
470 RegLocation rlSrc, int scale)
471{
472 int lenOffset = offsetof(ArrayObject, length);
473 int dataOffset = offsetof(ArrayObject, contents);
474
475 dvmCompilerFlushAllRegs(cUnit);
476
477 int regLen = r0;
478 int regPtr = r4PC; /* Preserved across call */
479 int regArray = r1;
480 int regIndex = r7; /* Preserved across call */
481
482 loadValueDirectFixed(cUnit, rlArray, regArray);
483 loadValueDirectFixed(cUnit, rlIndex, regIndex);
484
485 /* null object? */
486 ArmLIR * pcrLabel = NULL;
487
488 if (!(mir->OptimizationFlags & MIR_IGNORE_NULL_CHECK)) {
489 pcrLabel = genNullCheck(cUnit, rlArray.sRegLow, regArray,
490 mir->offset, NULL);
491 }
492
493 if (!(mir->OptimizationFlags & MIR_IGNORE_RANGE_CHECK)) {
494 /* Get len */
495 loadWordDisp(cUnit, regArray, lenOffset, regLen);
496 /* regPtr -> array data */
497 opRegRegImm(cUnit, kOpAdd, regPtr, regArray, dataOffset);
498 genBoundsCheck(cUnit, regIndex, regLen, mir->offset,
499 pcrLabel);
500 } else {
501 /* regPtr -> array data */
502 opRegRegImm(cUnit, kOpAdd, regPtr, regArray, dataOffset);
503 }
504
505 /* Get object to store */
506 loadValueDirectFixed(cUnit, rlSrc, r0);
Ben Chengbd1326d2010-04-02 15:04:53 -0700507 LOAD_FUNC_ADDR(cUnit, r2, (int)dvmCanPutArrayElement);
Bill Buzbeebe6534f2010-03-12 16:01:35 -0800508
509 /* Are we storing null? If so, avoid check */
510 opRegImm(cUnit, kOpCmp, r0, 0);
511 ArmLIR *branchOver = opCondBranch(cUnit, kArmCondEq);
512
513 /* Make sure the types are compatible */
514 loadWordDisp(cUnit, regArray, offsetof(Object, clazz), r1);
515 loadWordDisp(cUnit, r0, offsetof(Object, clazz), r0);
516 opReg(cUnit, kOpBlx, r2);
517 dvmCompilerClobberCallRegs(cUnit);
Bill Buzbee900a3af2010-03-16 12:41:43 -0700518
519 /*
520 * Using fixed registers here, and counting on r4 and r7 being
521 * preserved across the above call. Tell the register allocation
522 * utilities about the regs we are using directly
523 */
524 dvmCompilerLockTemp(cUnit, regPtr); // r4PC
525 dvmCompilerLockTemp(cUnit, regIndex); // r7
526 dvmCompilerLockTemp(cUnit, r0);
527
Bill Buzbeebe6534f2010-03-12 16:01:35 -0800528 /* Bad? - roll back and re-execute if so */
529 genRegImmCheck(cUnit, kArmCondEq, r0, 0, mir->offset, pcrLabel);
530
531 /* Resume here - must reload element, regPtr & index preserved */
532 loadValueDirectFixed(cUnit, rlSrc, r0);
533
534 ArmLIR *target = newLIR0(cUnit, kArmPseudoTargetLabel);
535 target->defMask = ENCODE_ALL;
536 branchOver->generic.target = (LIR *) target;
537
Ben Cheng11d8f142010-03-24 15:24:19 -0700538 HEAP_ACCESS_SHADOW(true);
Bill Buzbeebe6534f2010-03-12 16:01:35 -0800539 storeBaseIndexed(cUnit, regPtr, regIndex, r0,
540 scale, kWord);
Ben Cheng11d8f142010-03-24 15:24:19 -0700541 HEAP_ACCESS_SHADOW(false);
Bill Buzbeebe6534f2010-03-12 16:01:35 -0800542}
543
Ben Cheng5d90c202009-11-22 23:31:11 -0800544static bool genShiftOpLong(CompilationUnit *cUnit, MIR *mir,
545 RegLocation rlDest, RegLocation rlSrc1,
546 RegLocation rlShift)
Ben Chengba4fc8b2009-06-01 13:00:29 -0700547{
Ben Chenge9695e52009-06-16 16:11:47 -0700548 /*
549 * Don't mess with the regsiters here as there is a particular calling
550 * convention to the out-of-line handler.
551 */
Bill Buzbee1465db52009-09-23 17:17:35 -0700552 RegLocation rlResult;
553
554 loadValueDirectWideFixed(cUnit, rlSrc1, r0, r1);
555 loadValueDirect(cUnit, rlShift, r2);
Ben Chenge9695e52009-06-16 16:11:47 -0700556 switch( mir->dalvikInsn.opCode) {
557 case OP_SHL_LONG:
558 case OP_SHL_LONG_2ADDR:
559 genDispatchToHandler(cUnit, TEMPLATE_SHL_LONG);
560 break;
561 case OP_SHR_LONG:
562 case OP_SHR_LONG_2ADDR:
563 genDispatchToHandler(cUnit, TEMPLATE_SHR_LONG);
564 break;
565 case OP_USHR_LONG:
566 case OP_USHR_LONG_2ADDR:
567 genDispatchToHandler(cUnit, TEMPLATE_USHR_LONG);
568 break;
569 default:
570 return true;
571 }
Bill Buzbeec6f10662010-02-09 11:16:15 -0800572 rlResult = dvmCompilerGetReturnWide(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -0700573 storeValueWide(cUnit, rlDest, rlResult);
Ben Chenge9695e52009-06-16 16:11:47 -0700574 return false;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700575}
Ben Chenge9695e52009-06-16 16:11:47 -0700576
Ben Cheng5d90c202009-11-22 23:31:11 -0800577static bool genArithOpLong(CompilationUnit *cUnit, MIR *mir,
578 RegLocation rlDest, RegLocation rlSrc1,
579 RegLocation rlSrc2)
Ben Chengba4fc8b2009-06-01 13:00:29 -0700580{
Bill Buzbee1465db52009-09-23 17:17:35 -0700581 RegLocation rlResult;
582 OpKind firstOp = kOpBkpt;
583 OpKind secondOp = kOpBkpt;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700584 bool callOut = false;
585 void *callTgt;
586 int retReg = r0;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700587
588 switch (mir->dalvikInsn.opCode) {
589 case OP_NOT_LONG:
Bill Buzbee1465db52009-09-23 17:17:35 -0700590 rlSrc2 = loadValueWide(cUnit, rlSrc2, kCoreReg);
Bill Buzbeec6f10662010-02-09 11:16:15 -0800591 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Bill Buzbee1465db52009-09-23 17:17:35 -0700592 opRegReg(cUnit, kOpMvn, rlResult.lowReg, rlSrc2.lowReg);
593 opRegReg(cUnit, kOpMvn, rlResult.highReg, rlSrc2.highReg);
594 storeValueWide(cUnit, rlDest, rlResult);
595 return false;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700596 break;
597 case OP_ADD_LONG:
598 case OP_ADD_LONG_2ADDR:
Bill Buzbee1465db52009-09-23 17:17:35 -0700599 firstOp = kOpAdd;
600 secondOp = kOpAdc;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700601 break;
602 case OP_SUB_LONG:
603 case OP_SUB_LONG_2ADDR:
Bill Buzbee1465db52009-09-23 17:17:35 -0700604 firstOp = kOpSub;
605 secondOp = kOpSbc;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700606 break;
607 case OP_MUL_LONG:
608 case OP_MUL_LONG_2ADDR:
Bill Buzbee1465db52009-09-23 17:17:35 -0700609 genMulLong(cUnit, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700610 return false;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700611 case OP_DIV_LONG:
612 case OP_DIV_LONG_2ADDR:
613 callOut = true;
614 retReg = r0;
615 callTgt = (void*)__aeabi_ldivmod;
616 break;
617 /* NOTE - result is in r2/r3 instead of r0/r1 */
618 case OP_REM_LONG:
619 case OP_REM_LONG_2ADDR:
620 callOut = true;
621 callTgt = (void*)__aeabi_ldivmod;
622 retReg = r2;
623 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700624 case OP_AND_LONG_2ADDR:
Bill Buzbee1465db52009-09-23 17:17:35 -0700625 case OP_AND_LONG:
626 firstOp = kOpAnd;
627 secondOp = kOpAnd;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700628 break;
629 case OP_OR_LONG:
630 case OP_OR_LONG_2ADDR:
Bill Buzbee1465db52009-09-23 17:17:35 -0700631 firstOp = kOpOr;
632 secondOp = kOpOr;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700633 break;
634 case OP_XOR_LONG:
635 case OP_XOR_LONG_2ADDR:
Bill Buzbee1465db52009-09-23 17:17:35 -0700636 firstOp = kOpXor;
637 secondOp = kOpXor;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700638 break;
Ben Chenge9695e52009-06-16 16:11:47 -0700639 case OP_NEG_LONG: {
Bill Buzbee51ecf602010-01-14 14:27:52 -0800640 //TUNING: can improve this using Thumb2 code
Bill Buzbeec6f10662010-02-09 11:16:15 -0800641 int tReg = dvmCompilerAllocTemp(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -0700642 rlSrc2 = loadValueWide(cUnit, rlSrc2, kCoreReg);
Bill Buzbeec6f10662010-02-09 11:16:15 -0800643 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Ben Chengbd1326d2010-04-02 15:04:53 -0700644 loadConstantNoClobber(cUnit, tReg, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -0700645 opRegRegReg(cUnit, kOpSub, rlResult.lowReg,
Bill Buzbee51ecf602010-01-14 14:27:52 -0800646 tReg, rlSrc2.lowReg);
647 opRegReg(cUnit, kOpSbc, tReg, rlSrc2.highReg);
648 genRegCopy(cUnit, rlResult.highReg, tReg);
Bill Buzbee1465db52009-09-23 17:17:35 -0700649 storeValueWide(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700650 return false;
Ben Chenge9695e52009-06-16 16:11:47 -0700651 }
Ben Chengba4fc8b2009-06-01 13:00:29 -0700652 default:
653 LOGE("Invalid long arith op");
Bill Buzbeefc519dc2010-03-06 23:30:57 -0800654 dvmCompilerAbort(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700655 }
656 if (!callOut) {
Bill Buzbee80cef862010-03-25 10:38:34 -0700657 genLong3Addr(cUnit, mir, firstOp, secondOp, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700658 } else {
Bill Buzbee1465db52009-09-23 17:17:35 -0700659 // Adjust return regs in to handle case of rem returning r2/r3
Bill Buzbeec6f10662010-02-09 11:16:15 -0800660 dvmCompilerFlushAllRegs(cUnit); /* Send everything to home location */
Bill Buzbee1465db52009-09-23 17:17:35 -0700661 loadValueDirectWideFixed(cUnit, rlSrc1, r0, r1);
Ben Chengbd1326d2010-04-02 15:04:53 -0700662 LOAD_FUNC_ADDR(cUnit, rlr, (int) callTgt);
Bill Buzbee1465db52009-09-23 17:17:35 -0700663 loadValueDirectWideFixed(cUnit, rlSrc2, r2, r3);
664 opReg(cUnit, kOpBlx, rlr);
Elliott Hughes6a555132010-02-25 15:41:42 -0800665 dvmCompilerClobberCallRegs(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -0700666 if (retReg == r0)
Bill Buzbeec6f10662010-02-09 11:16:15 -0800667 rlResult = dvmCompilerGetReturnWide(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -0700668 else
Bill Buzbeec6f10662010-02-09 11:16:15 -0800669 rlResult = dvmCompilerGetReturnWideAlt(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -0700670 storeValueWide(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700671 }
672 return false;
673}
674
Ben Cheng5d90c202009-11-22 23:31:11 -0800675static bool genArithOpInt(CompilationUnit *cUnit, MIR *mir,
676 RegLocation rlDest, RegLocation rlSrc1,
677 RegLocation rlSrc2)
Ben Chengba4fc8b2009-06-01 13:00:29 -0700678{
Bill Buzbee1465db52009-09-23 17:17:35 -0700679 OpKind op = kOpBkpt;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700680 bool callOut = false;
681 bool checkZero = false;
Bill Buzbee1465db52009-09-23 17:17:35 -0700682 bool unary = false;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700683 int retReg = r0;
684 void *callTgt;
Bill Buzbee1465db52009-09-23 17:17:35 -0700685 RegLocation rlResult;
Bill Buzbee0e605272009-12-01 14:28:05 -0800686 bool shiftOp = false;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700687
Ben Chengba4fc8b2009-06-01 13:00:29 -0700688 switch (mir->dalvikInsn.opCode) {
689 case OP_NEG_INT:
Bill Buzbee1465db52009-09-23 17:17:35 -0700690 op = kOpNeg;
691 unary = true;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700692 break;
693 case OP_NOT_INT:
Bill Buzbee1465db52009-09-23 17:17:35 -0700694 op = kOpMvn;
695 unary = true;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700696 break;
697 case OP_ADD_INT:
698 case OP_ADD_INT_2ADDR:
Bill Buzbee1465db52009-09-23 17:17:35 -0700699 op = kOpAdd;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700700 break;
701 case OP_SUB_INT:
702 case OP_SUB_INT_2ADDR:
Bill Buzbee1465db52009-09-23 17:17:35 -0700703 op = kOpSub;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700704 break;
705 case OP_MUL_INT:
706 case OP_MUL_INT_2ADDR:
Bill Buzbee1465db52009-09-23 17:17:35 -0700707 op = kOpMul;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700708 break;
709 case OP_DIV_INT:
710 case OP_DIV_INT_2ADDR:
711 callOut = true;
712 checkZero = true;
713 callTgt = __aeabi_idiv;
714 retReg = r0;
715 break;
716 /* NOTE: returns in r1 */
717 case OP_REM_INT:
718 case OP_REM_INT_2ADDR:
719 callOut = true;
720 checkZero = true;
721 callTgt = __aeabi_idivmod;
722 retReg = r1;
723 break;
724 case OP_AND_INT:
725 case OP_AND_INT_2ADDR:
Bill Buzbee1465db52009-09-23 17:17:35 -0700726 op = kOpAnd;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700727 break;
728 case OP_OR_INT:
729 case OP_OR_INT_2ADDR:
Bill Buzbee1465db52009-09-23 17:17:35 -0700730 op = kOpOr;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700731 break;
732 case OP_XOR_INT:
733 case OP_XOR_INT_2ADDR:
Bill Buzbee1465db52009-09-23 17:17:35 -0700734 op = kOpXor;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700735 break;
736 case OP_SHL_INT:
737 case OP_SHL_INT_2ADDR:
Bill Buzbee0e605272009-12-01 14:28:05 -0800738 shiftOp = true;
Bill Buzbee1465db52009-09-23 17:17:35 -0700739 op = kOpLsl;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700740 break;
741 case OP_SHR_INT:
742 case OP_SHR_INT_2ADDR:
Bill Buzbee0e605272009-12-01 14:28:05 -0800743 shiftOp = true;
Bill Buzbee1465db52009-09-23 17:17:35 -0700744 op = kOpAsr;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700745 break;
746 case OP_USHR_INT:
747 case OP_USHR_INT_2ADDR:
Bill Buzbee0e605272009-12-01 14:28:05 -0800748 shiftOp = true;
Bill Buzbee1465db52009-09-23 17:17:35 -0700749 op = kOpLsr;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700750 break;
751 default:
752 LOGE("Invalid word arith op: 0x%x(%d)",
753 mir->dalvikInsn.opCode, mir->dalvikInsn.opCode);
Bill Buzbeefc519dc2010-03-06 23:30:57 -0800754 dvmCompilerAbort(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700755 }
756 if (!callOut) {
Bill Buzbee1465db52009-09-23 17:17:35 -0700757 rlSrc1 = loadValue(cUnit, rlSrc1, kCoreReg);
758 if (unary) {
Bill Buzbeec6f10662010-02-09 11:16:15 -0800759 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Bill Buzbee1465db52009-09-23 17:17:35 -0700760 opRegReg(cUnit, op, rlResult.lowReg,
761 rlSrc1.lowReg);
Ben Chenge9695e52009-06-16 16:11:47 -0700762 } else {
Bill Buzbee1465db52009-09-23 17:17:35 -0700763 rlSrc2 = loadValue(cUnit, rlSrc2, kCoreReg);
Bill Buzbee0e605272009-12-01 14:28:05 -0800764 if (shiftOp) {
Bill Buzbeec6f10662010-02-09 11:16:15 -0800765 int tReg = dvmCompilerAllocTemp(cUnit);
Bill Buzbee0e605272009-12-01 14:28:05 -0800766 opRegRegImm(cUnit, kOpAnd, tReg, rlSrc2.lowReg, 31);
Bill Buzbeec6f10662010-02-09 11:16:15 -0800767 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Bill Buzbee0e605272009-12-01 14:28:05 -0800768 opRegRegReg(cUnit, op, rlResult.lowReg,
769 rlSrc1.lowReg, tReg);
Bill Buzbeec6f10662010-02-09 11:16:15 -0800770 dvmCompilerFreeTemp(cUnit, tReg);
Bill Buzbee0e605272009-12-01 14:28:05 -0800771 } else {
Bill Buzbeec6f10662010-02-09 11:16:15 -0800772 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Bill Buzbee0e605272009-12-01 14:28:05 -0800773 opRegRegReg(cUnit, op, rlResult.lowReg,
774 rlSrc1.lowReg, rlSrc2.lowReg);
775 }
Ben Chenge9695e52009-06-16 16:11:47 -0700776 }
Bill Buzbee1465db52009-09-23 17:17:35 -0700777 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700778 } else {
Bill Buzbee1465db52009-09-23 17:17:35 -0700779 RegLocation rlResult;
Bill Buzbeec6f10662010-02-09 11:16:15 -0800780 dvmCompilerFlushAllRegs(cUnit); /* Send everything to home location */
Bill Buzbee1465db52009-09-23 17:17:35 -0700781 loadValueDirectFixed(cUnit, rlSrc2, r1);
Ben Chengbd1326d2010-04-02 15:04:53 -0700782 LOAD_FUNC_ADDR(cUnit, r2, (int) callTgt);
Bill Buzbee1465db52009-09-23 17:17:35 -0700783 loadValueDirectFixed(cUnit, rlSrc1, r0);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700784 if (checkZero) {
Bill Buzbee1465db52009-09-23 17:17:35 -0700785 genNullCheck(cUnit, rlSrc2.sRegLow, r1, mir->offset, NULL);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700786 }
Bill Buzbee1465db52009-09-23 17:17:35 -0700787 opReg(cUnit, kOpBlx, r2);
Elliott Hughes6a555132010-02-25 15:41:42 -0800788 dvmCompilerClobberCallRegs(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -0700789 if (retReg == r0)
Bill Buzbeec6f10662010-02-09 11:16:15 -0800790 rlResult = dvmCompilerGetReturn(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -0700791 else
Bill Buzbeec6f10662010-02-09 11:16:15 -0800792 rlResult = dvmCompilerGetReturnAlt(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -0700793 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700794 }
795 return false;
796}
797
Ben Cheng5d90c202009-11-22 23:31:11 -0800798static bool genArithOp(CompilationUnit *cUnit, MIR *mir)
Ben Chengba4fc8b2009-06-01 13:00:29 -0700799{
800 OpCode opCode = mir->dalvikInsn.opCode;
Bill Buzbee1465db52009-09-23 17:17:35 -0700801 RegLocation rlDest;
802 RegLocation rlSrc1;
803 RegLocation rlSrc2;
804 /* Deduce sizes of operands */
805 if (mir->ssaRep->numUses == 2) {
Bill Buzbeec6f10662010-02-09 11:16:15 -0800806 rlSrc1 = dvmCompilerGetSrc(cUnit, mir, 0);
807 rlSrc2 = dvmCompilerGetSrc(cUnit, mir, 1);
Bill Buzbee1465db52009-09-23 17:17:35 -0700808 } else if (mir->ssaRep->numUses == 3) {
Bill Buzbeec6f10662010-02-09 11:16:15 -0800809 rlSrc1 = dvmCompilerGetSrcWide(cUnit, mir, 0, 1);
810 rlSrc2 = dvmCompilerGetSrc(cUnit, mir, 2);
Bill Buzbee1465db52009-09-23 17:17:35 -0700811 } else {
Bill Buzbeec6f10662010-02-09 11:16:15 -0800812 rlSrc1 = dvmCompilerGetSrcWide(cUnit, mir, 0, 1);
813 rlSrc2 = dvmCompilerGetSrcWide(cUnit, mir, 2, 3);
Bill Buzbee1465db52009-09-23 17:17:35 -0700814 assert(mir->ssaRep->numUses == 4);
815 }
816 if (mir->ssaRep->numDefs == 1) {
Bill Buzbeec6f10662010-02-09 11:16:15 -0800817 rlDest = dvmCompilerGetDest(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -0700818 } else {
819 assert(mir->ssaRep->numDefs == 2);
Bill Buzbeec6f10662010-02-09 11:16:15 -0800820 rlDest = dvmCompilerGetDestWide(cUnit, mir, 0, 1);
Bill Buzbee1465db52009-09-23 17:17:35 -0700821 }
Ben Chengba4fc8b2009-06-01 13:00:29 -0700822
823 if ((opCode >= OP_ADD_LONG_2ADDR) && (opCode <= OP_XOR_LONG_2ADDR)) {
Ben Cheng5d90c202009-11-22 23:31:11 -0800824 return genArithOpLong(cUnit,mir, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700825 }
826 if ((opCode >= OP_ADD_LONG) && (opCode <= OP_XOR_LONG)) {
Ben Cheng5d90c202009-11-22 23:31:11 -0800827 return genArithOpLong(cUnit,mir, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700828 }
829 if ((opCode >= OP_SHL_LONG_2ADDR) && (opCode <= OP_USHR_LONG_2ADDR)) {
Ben Cheng5d90c202009-11-22 23:31:11 -0800830 return genShiftOpLong(cUnit,mir, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700831 }
832 if ((opCode >= OP_SHL_LONG) && (opCode <= OP_USHR_LONG)) {
Ben Cheng5d90c202009-11-22 23:31:11 -0800833 return genShiftOpLong(cUnit,mir, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700834 }
835 if ((opCode >= OP_ADD_INT_2ADDR) && (opCode <= OP_USHR_INT_2ADDR)) {
Ben Cheng5d90c202009-11-22 23:31:11 -0800836 return genArithOpInt(cUnit,mir, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700837 }
838 if ((opCode >= OP_ADD_INT) && (opCode <= OP_USHR_INT)) {
Ben Cheng5d90c202009-11-22 23:31:11 -0800839 return genArithOpInt(cUnit,mir, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700840 }
841 if ((opCode >= OP_ADD_FLOAT_2ADDR) && (opCode <= OP_REM_FLOAT_2ADDR)) {
Ben Cheng5d90c202009-11-22 23:31:11 -0800842 return genArithOpFloat(cUnit,mir, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700843 }
844 if ((opCode >= OP_ADD_FLOAT) && (opCode <= OP_REM_FLOAT)) {
Ben Cheng5d90c202009-11-22 23:31:11 -0800845 return genArithOpFloat(cUnit, mir, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700846 }
847 if ((opCode >= OP_ADD_DOUBLE_2ADDR) && (opCode <= OP_REM_DOUBLE_2ADDR)) {
Ben Cheng5d90c202009-11-22 23:31:11 -0800848 return genArithOpDouble(cUnit,mir, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700849 }
850 if ((opCode >= OP_ADD_DOUBLE) && (opCode <= OP_REM_DOUBLE)) {
Ben Cheng5d90c202009-11-22 23:31:11 -0800851 return genArithOpDouble(cUnit,mir, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700852 }
853 return true;
854}
855
Bill Buzbee1465db52009-09-23 17:17:35 -0700856/* Generate unconditional branch instructions */
857static ArmLIR *genUnconditionalBranch(CompilationUnit *cUnit, ArmLIR *target)
858{
859 ArmLIR *branch = opNone(cUnit, kOpUncondBr);
860 branch->generic.target = (LIR *) target;
861 return branch;
862}
863
Bill Buzbee1465db52009-09-23 17:17:35 -0700864/* Perform the actual operation for OP_RETURN_* */
865static void genReturnCommon(CompilationUnit *cUnit, MIR *mir)
866{
867 genDispatchToHandler(cUnit, TEMPLATE_RETURN);
Ben Cheng978738d2010-05-13 13:45:57 -0700868#if defined(WITH_JIT_TUNING)
Bill Buzbee1465db52009-09-23 17:17:35 -0700869 gDvmJit.returnOp++;
870#endif
871 int dPC = (int) (cUnit->method->insns + mir->offset);
872 /* Insert branch, but defer setting of target */
873 ArmLIR *branch = genUnconditionalBranch(cUnit, NULL);
874 /* Set up the place holder to reconstruct this Dalvik PC */
875 ArmLIR *pcrLabel = dvmCompilerNew(sizeof(ArmLIR), true);
Ben Chenga4973592010-03-31 11:59:18 -0700876 pcrLabel->opCode = kArmPseudoPCReconstructionCell;
Bill Buzbee1465db52009-09-23 17:17:35 -0700877 pcrLabel->operands[0] = dPC;
878 pcrLabel->operands[1] = mir->offset;
879 /* Insert the place holder to the growable list */
880 dvmInsertGrowableList(&cUnit->pcReconstructionList, pcrLabel);
881 /* Branch to the PC reconstruction code */
882 branch->generic.target = (LIR *) pcrLabel;
883}
884
Ben Chengba4fc8b2009-06-01 13:00:29 -0700885static void genProcessArgsNoRange(CompilationUnit *cUnit, MIR *mir,
886 DecodedInstruction *dInsn,
Bill Buzbee89efc3d2009-07-28 11:22:22 -0700887 ArmLIR **pcrLabel)
Ben Chengba4fc8b2009-06-01 13:00:29 -0700888{
889 unsigned int i;
890 unsigned int regMask = 0;
Bill Buzbee1465db52009-09-23 17:17:35 -0700891 RegLocation rlArg;
892 int numDone = 0;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700893
Bill Buzbee1465db52009-09-23 17:17:35 -0700894 /*
895 * Load arguments to r0..r4. Note that these registers may contain
896 * live values, so we clobber them immediately after loading to prevent
897 * them from being used as sources for subsequent loads.
898 */
Bill Buzbeec6f10662010-02-09 11:16:15 -0800899 dvmCompilerLockAllTemps(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700900 for (i = 0; i < dInsn->vA; i++) {
901 regMask |= 1 << i;
Bill Buzbeec6f10662010-02-09 11:16:15 -0800902 rlArg = dvmCompilerGetSrc(cUnit, mir, numDone++);
Bill Buzbee1465db52009-09-23 17:17:35 -0700903 loadValueDirectFixed(cUnit, rlArg, i);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700904 }
905 if (regMask) {
906 /* Up to 5 args are pushed on top of FP - sizeofStackSaveArea */
Bill Buzbee1465db52009-09-23 17:17:35 -0700907 opRegRegImm(cUnit, kOpSub, r7, rFP,
908 sizeof(StackSaveArea) + (dInsn->vA << 2));
Ben Chengba4fc8b2009-06-01 13:00:29 -0700909 /* generate null check */
910 if (pcrLabel) {
Bill Buzbeec6f10662010-02-09 11:16:15 -0800911 *pcrLabel = genNullCheck(cUnit, dvmCompilerSSASrc(mir, 0), r0,
Bill Buzbee1465db52009-09-23 17:17:35 -0700912 mir->offset, NULL);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700913 }
Bill Buzbee270c1d62009-08-13 16:58:07 -0700914 storeMultiple(cUnit, r7, regMask);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700915 }
916}
917
918static void genProcessArgsRange(CompilationUnit *cUnit, MIR *mir,
919 DecodedInstruction *dInsn,
Bill Buzbee89efc3d2009-07-28 11:22:22 -0700920 ArmLIR **pcrLabel)
Ben Chengba4fc8b2009-06-01 13:00:29 -0700921{
922 int srcOffset = dInsn->vC << 2;
923 int numArgs = dInsn->vA;
924 int regMask;
Bill Buzbee1465db52009-09-23 17:17:35 -0700925
926 /*
927 * Note: here, all promoted registers will have been flushed
928 * back to the Dalvik base locations, so register usage restrictins
929 * are lifted. All parms loaded from original Dalvik register
930 * region - even though some might conceivably have valid copies
931 * cached in a preserved register.
932 */
Bill Buzbeec6f10662010-02-09 11:16:15 -0800933 dvmCompilerLockAllTemps(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -0700934
Ben Chengba4fc8b2009-06-01 13:00:29 -0700935 /*
936 * r4PC : &rFP[vC]
937 * r7: &newFP[0]
938 */
Bill Buzbee1465db52009-09-23 17:17:35 -0700939 opRegRegImm(cUnit, kOpAdd, r4PC, rFP, srcOffset);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700940 /* load [r0 .. min(numArgs,4)] */
941 regMask = (1 << ((numArgs < 4) ? numArgs : 4)) - 1;
Ben Chengd7d426a2009-09-22 11:23:36 -0700942 /*
943 * Protect the loadMultiple instruction from being reordered with other
944 * Dalvik stack accesses.
945 */
Bill Buzbee270c1d62009-08-13 16:58:07 -0700946 loadMultiple(cUnit, r4PC, regMask);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700947
Bill Buzbee1465db52009-09-23 17:17:35 -0700948 opRegRegImm(cUnit, kOpSub, r7, rFP,
949 sizeof(StackSaveArea) + (numArgs << 2));
Ben Chengba4fc8b2009-06-01 13:00:29 -0700950 /* generate null check */
951 if (pcrLabel) {
Bill Buzbeec6f10662010-02-09 11:16:15 -0800952 *pcrLabel = genNullCheck(cUnit, dvmCompilerSSASrc(mir, 0), r0,
Bill Buzbee1465db52009-09-23 17:17:35 -0700953 mir->offset, NULL);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700954 }
955
956 /*
957 * Handle remaining 4n arguments:
958 * store previously loaded 4 values and load the next 4 values
959 */
960 if (numArgs >= 8) {
Bill Buzbee89efc3d2009-07-28 11:22:22 -0700961 ArmLIR *loopLabel = NULL;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700962 /*
963 * r0 contains "this" and it will be used later, so push it to the stack
Bill Buzbee270c1d62009-08-13 16:58:07 -0700964 * first. Pushing r5 (rFP) is just for stack alignment purposes.
Ben Chengba4fc8b2009-06-01 13:00:29 -0700965 */
Bill Buzbee1465db52009-09-23 17:17:35 -0700966 opImm(cUnit, kOpPush, (1 << r0 | 1 << rFP));
Ben Chengba4fc8b2009-06-01 13:00:29 -0700967 /* No need to generate the loop structure if numArgs <= 11 */
968 if (numArgs > 11) {
969 loadConstant(cUnit, 5, ((numArgs - 4) >> 2) << 2);
Bill Buzbee1465db52009-09-23 17:17:35 -0700970 loopLabel = newLIR0(cUnit, kArmPseudoTargetLabel);
Ben Chengd7d426a2009-09-22 11:23:36 -0700971 loopLabel->defMask = ENCODE_ALL;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700972 }
Bill Buzbee270c1d62009-08-13 16:58:07 -0700973 storeMultiple(cUnit, r7, regMask);
Ben Chengd7d426a2009-09-22 11:23:36 -0700974 /*
975 * Protect the loadMultiple instruction from being reordered with other
976 * Dalvik stack accesses.
977 */
Bill Buzbee270c1d62009-08-13 16:58:07 -0700978 loadMultiple(cUnit, r4PC, regMask);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700979 /* No need to generate the loop structure if numArgs <= 11 */
980 if (numArgs > 11) {
Bill Buzbee1465db52009-09-23 17:17:35 -0700981 opRegImm(cUnit, kOpSub, rFP, 4);
982 genConditionalBranch(cUnit, kArmCondNe, loopLabel);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700983 }
984 }
985
986 /* Save the last batch of loaded values */
Bill Buzbee270c1d62009-08-13 16:58:07 -0700987 storeMultiple(cUnit, r7, regMask);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700988
989 /* Generate the loop epilogue - don't use r0 */
990 if ((numArgs > 4) && (numArgs % 4)) {
991 regMask = ((1 << (numArgs & 0x3)) - 1) << 1;
Ben Chengd7d426a2009-09-22 11:23:36 -0700992 /*
993 * Protect the loadMultiple instruction from being reordered with other
994 * Dalvik stack accesses.
995 */
Bill Buzbee270c1d62009-08-13 16:58:07 -0700996 loadMultiple(cUnit, r4PC, regMask);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700997 }
998 if (numArgs >= 8)
Bill Buzbee1465db52009-09-23 17:17:35 -0700999 opImm(cUnit, kOpPop, (1 << r0 | 1 << rFP));
Ben Chengba4fc8b2009-06-01 13:00:29 -07001000
1001 /* Save the modulo 4 arguments */
1002 if ((numArgs > 4) && (numArgs % 4)) {
Bill Buzbee270c1d62009-08-13 16:58:07 -07001003 storeMultiple(cUnit, r7, regMask);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001004 }
1005}
1006
Ben Cheng38329f52009-07-07 14:19:20 -07001007/*
1008 * Generate code to setup the call stack then jump to the chaining cell if it
1009 * is not a native method.
1010 */
1011static void genInvokeSingletonCommon(CompilationUnit *cUnit, MIR *mir,
Bill Buzbee89efc3d2009-07-28 11:22:22 -07001012 BasicBlock *bb, ArmLIR *labelList,
1013 ArmLIR *pcrLabel,
Ben Cheng38329f52009-07-07 14:19:20 -07001014 const Method *calleeMethod)
Ben Chengba4fc8b2009-06-01 13:00:29 -07001015{
Bill Buzbee1465db52009-09-23 17:17:35 -07001016 /*
1017 * Note: all Dalvik register state should be flushed to
1018 * memory by the point, so register usage restrictions no
1019 * longer apply. All temp & preserved registers may be used.
1020 */
Bill Buzbeec6f10662010-02-09 11:16:15 -08001021 dvmCompilerLockAllTemps(cUnit);
Bill Buzbee89efc3d2009-07-28 11:22:22 -07001022 ArmLIR *retChainingCell = &labelList[bb->fallThrough->id];
Ben Chengba4fc8b2009-06-01 13:00:29 -07001023
1024 /* r1 = &retChainingCell */
Bill Buzbeec6f10662010-02-09 11:16:15 -08001025 dvmCompilerLockTemp(cUnit, r1);
Bill Buzbee1465db52009-09-23 17:17:35 -07001026 ArmLIR *addrRetChain = opRegRegImm(cUnit, kOpAdd, r1, rpc, 0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001027 /* r4PC = dalvikCallsite */
1028 loadConstant(cUnit, r4PC,
1029 (int) (cUnit->method->insns + mir->offset));
1030 addrRetChain->generic.target = (LIR *) retChainingCell;
1031 /*
Ben Cheng38329f52009-07-07 14:19:20 -07001032 * r0 = calleeMethod (loaded upon calling genInvokeSingletonCommon)
Ben Chengba4fc8b2009-06-01 13:00:29 -07001033 * r1 = &ChainingCell
1034 * r4PC = callsiteDPC
1035 */
1036 if (dvmIsNativeMethod(calleeMethod)) {
Ben Cheng38329f52009-07-07 14:19:20 -07001037 genDispatchToHandler(cUnit, TEMPLATE_INVOKE_METHOD_NATIVE);
Ben Cheng978738d2010-05-13 13:45:57 -07001038#if defined(WITH_JIT_TUNING)
Ben Cheng38329f52009-07-07 14:19:20 -07001039 gDvmJit.invokeNative++;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001040#endif
1041 } else {
1042 genDispatchToHandler(cUnit, TEMPLATE_INVOKE_METHOD_CHAIN);
Ben Cheng978738d2010-05-13 13:45:57 -07001043#if defined(WITH_JIT_TUNING)
Ben Cheng86717f72010-03-05 15:27:21 -08001044 gDvmJit.invokeMonomorphic++;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001045#endif
Ben Cheng38329f52009-07-07 14:19:20 -07001046 /* Branch to the chaining cell */
Ben Chengba4fc8b2009-06-01 13:00:29 -07001047 genUnconditionalBranch(cUnit, &labelList[bb->taken->id]);
1048 }
1049 /* Handle exceptions using the interpreter */
1050 genTrap(cUnit, mir->offset, pcrLabel);
1051}
1052
Ben Cheng38329f52009-07-07 14:19:20 -07001053/*
1054 * Generate code to check the validity of a predicted chain and take actions
1055 * based on the result.
1056 *
1057 * 0x426a99aa : ldr r4, [pc, #72] --> r4 <- dalvikPC of this invoke
1058 * 0x426a99ac : add r1, pc, #32 --> r1 <- &retChainingCell
1059 * 0x426a99ae : add r2, pc, #40 --> r2 <- &predictedChainingCell
1060 * 0x426a99b0 : blx_1 0x426a918c --+ TEMPLATE_INVOKE_METHOD_PREDICTED_CHAIN
1061 * 0x426a99b2 : blx_2 see above --+
1062 * 0x426a99b4 : b 0x426a99d8 --> off to the predicted chain
1063 * 0x426a99b6 : b 0x426a99c8 --> punt to the interpreter
1064 * 0x426a99b8 : ldr r0, [r7, #44] --> r0 <- this->class->vtable[methodIdx]
1065 * 0x426a99ba : cmp r1, #0 --> compare r1 (rechain count) against 0
1066 * 0x426a99bc : bgt 0x426a99c2 --> >=0? don't rechain
1067 * 0x426a99be : ldr r7, [r6, #96] --+ dvmJitToPatchPredictedChain
1068 * 0x426a99c0 : blx r7 --+
1069 * 0x426a99c2 : add r1, pc, #12 --> r1 <- &retChainingCell
1070 * 0x426a99c4 : blx_1 0x426a9098 --+ TEMPLATE_INVOKE_METHOD_NO_OPT
1071 * 0x426a99c6 : blx_2 see above --+
1072 */
1073static void genInvokeVirtualCommon(CompilationUnit *cUnit, MIR *mir,
1074 int methodIndex,
Bill Buzbee89efc3d2009-07-28 11:22:22 -07001075 ArmLIR *retChainingCell,
1076 ArmLIR *predChainingCell,
1077 ArmLIR *pcrLabel)
Ben Cheng38329f52009-07-07 14:19:20 -07001078{
Bill Buzbee1465db52009-09-23 17:17:35 -07001079 /*
1080 * Note: all Dalvik register state should be flushed to
1081 * memory by the point, so register usage restrictions no
1082 * longer apply. Lock temps to prevent them from being
1083 * allocated by utility routines.
1084 */
Bill Buzbeec6f10662010-02-09 11:16:15 -08001085 dvmCompilerLockAllTemps(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07001086
Ben Cheng38329f52009-07-07 14:19:20 -07001087 /* "this" is already left in r0 by genProcessArgs* */
1088
1089 /* r4PC = dalvikCallsite */
1090 loadConstant(cUnit, r4PC,
1091 (int) (cUnit->method->insns + mir->offset));
1092
1093 /* r1 = &retChainingCell */
Bill Buzbee1465db52009-09-23 17:17:35 -07001094 ArmLIR *addrRetChain = opRegRegImm(cUnit, kOpAdd, r1, rpc, 0);
Ben Cheng38329f52009-07-07 14:19:20 -07001095 addrRetChain->generic.target = (LIR *) retChainingCell;
1096
1097 /* r2 = &predictedChainingCell */
Bill Buzbee1465db52009-09-23 17:17:35 -07001098 ArmLIR *predictedChainingCell = opRegRegImm(cUnit, kOpAdd, r2, rpc, 0);
Ben Cheng38329f52009-07-07 14:19:20 -07001099 predictedChainingCell->generic.target = (LIR *) predChainingCell;
1100
1101 genDispatchToHandler(cUnit, TEMPLATE_INVOKE_METHOD_PREDICTED_CHAIN);
1102
1103 /* return through lr - jump to the chaining cell */
1104 genUnconditionalBranch(cUnit, predChainingCell);
1105
1106 /*
1107 * null-check on "this" may have been eliminated, but we still need a PC-
1108 * reconstruction label for stack overflow bailout.
1109 */
1110 if (pcrLabel == NULL) {
1111 int dPC = (int) (cUnit->method->insns + mir->offset);
Bill Buzbee89efc3d2009-07-28 11:22:22 -07001112 pcrLabel = dvmCompilerNew(sizeof(ArmLIR), true);
Ben Chenga4973592010-03-31 11:59:18 -07001113 pcrLabel->opCode = kArmPseudoPCReconstructionCell;
Ben Cheng38329f52009-07-07 14:19:20 -07001114 pcrLabel->operands[0] = dPC;
1115 pcrLabel->operands[1] = mir->offset;
1116 /* Insert the place holder to the growable list */
1117 dvmInsertGrowableList(&cUnit->pcReconstructionList, pcrLabel);
1118 }
1119
1120 /* return through lr+2 - punt to the interpreter */
1121 genUnconditionalBranch(cUnit, pcrLabel);
1122
1123 /*
1124 * return through lr+4 - fully resolve the callee method.
1125 * r1 <- count
1126 * r2 <- &predictedChainCell
1127 * r3 <- this->class
1128 * r4 <- dPC
1129 * r7 <- this->class->vtable
1130 */
1131
1132 /* r0 <- calleeMethod */
Bill Buzbee270c1d62009-08-13 16:58:07 -07001133 loadWordDisp(cUnit, r7, methodIndex * 4, r0);
Ben Cheng38329f52009-07-07 14:19:20 -07001134
1135 /* Check if rechain limit is reached */
Bill Buzbee1465db52009-09-23 17:17:35 -07001136 opRegImm(cUnit, kOpCmp, r1, 0);
Ben Cheng38329f52009-07-07 14:19:20 -07001137
Bill Buzbee1465db52009-09-23 17:17:35 -07001138 ArmLIR *bypassRechaining = opCondBranch(cUnit, kArmCondGt);
Ben Cheng38329f52009-07-07 14:19:20 -07001139
Bill Buzbee270c1d62009-08-13 16:58:07 -07001140 loadWordDisp(cUnit, rGLUE, offsetof(InterpState,
1141 jitToInterpEntries.dvmJitToPatchPredictedChain), r7);
Ben Cheng38329f52009-07-07 14:19:20 -07001142
Ben Chengb88ec3c2010-05-17 12:50:33 -07001143 genRegCopy(cUnit, r1, rGLUE);
1144
Ben Cheng38329f52009-07-07 14:19:20 -07001145 /*
1146 * r0 = calleeMethod
1147 * r2 = &predictedChainingCell
1148 * r3 = class
1149 *
1150 * &returnChainingCell has been loaded into r1 but is not needed
1151 * when patching the chaining cell and will be clobbered upon
1152 * returning so it will be reconstructed again.
1153 */
Bill Buzbee1465db52009-09-23 17:17:35 -07001154 opReg(cUnit, kOpBlx, r7);
Ben Cheng38329f52009-07-07 14:19:20 -07001155
1156 /* r1 = &retChainingCell */
Bill Buzbee1465db52009-09-23 17:17:35 -07001157 addrRetChain = opRegRegImm(cUnit, kOpAdd, r1, rpc, 0);
Ben Cheng38329f52009-07-07 14:19:20 -07001158 addrRetChain->generic.target = (LIR *) retChainingCell;
1159
1160 bypassRechaining->generic.target = (LIR *) addrRetChain;
1161 /*
1162 * r0 = calleeMethod,
1163 * r1 = &ChainingCell,
1164 * r4PC = callsiteDPC,
1165 */
1166 genDispatchToHandler(cUnit, TEMPLATE_INVOKE_METHOD_NO_OPT);
Ben Cheng978738d2010-05-13 13:45:57 -07001167#if defined(WITH_JIT_TUNING)
Ben Cheng86717f72010-03-05 15:27:21 -08001168 gDvmJit.invokePolymorphic++;
Ben Cheng38329f52009-07-07 14:19:20 -07001169#endif
1170 /* Handle exceptions using the interpreter */
1171 genTrap(cUnit, mir->offset, pcrLabel);
1172}
1173
Ben Chengba4fc8b2009-06-01 13:00:29 -07001174/* Geneate a branch to go back to the interpreter */
1175static void genPuntToInterp(CompilationUnit *cUnit, unsigned int offset)
1176{
1177 /* r0 = dalvik pc */
Bill Buzbeec6f10662010-02-09 11:16:15 -08001178 dvmCompilerFlushAllRegs(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001179 loadConstant(cUnit, r0, (int) (cUnit->method->insns + offset));
Bill Buzbee270c1d62009-08-13 16:58:07 -07001180 loadWordDisp(cUnit, r0, offsetof(Object, clazz), r3);
1181 loadWordDisp(cUnit, rGLUE, offsetof(InterpState,
1182 jitToInterpEntries.dvmJitToInterpPunt), r1);
Bill Buzbee1465db52009-09-23 17:17:35 -07001183 opReg(cUnit, kOpBlx, r1);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001184}
1185
1186/*
1187 * Attempt to single step one instruction using the interpreter and return
1188 * to the compiled code for the next Dalvik instruction
1189 */
1190static void genInterpSingleStep(CompilationUnit *cUnit, MIR *mir)
1191{
1192 int flags = dexGetInstrFlags(gDvm.instrFlags, mir->dalvikInsn.opCode);
1193 int flagsToCheck = kInstrCanBranch | kInstrCanSwitch | kInstrCanReturn |
1194 kInstrCanThrow;
Bill Buzbee1465db52009-09-23 17:17:35 -07001195
Bill Buzbee45273872010-03-11 11:12:15 -08001196 //If already optimized out, just ignore
1197 if (mir->dalvikInsn.opCode == OP_NOP)
1198 return;
1199
Bill Buzbee1465db52009-09-23 17:17:35 -07001200 //Ugly, but necessary. Flush all Dalvik regs so Interp can find them
Bill Buzbeec6f10662010-02-09 11:16:15 -08001201 dvmCompilerFlushAllRegs(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07001202
Ben Chengba4fc8b2009-06-01 13:00:29 -07001203 if ((mir->next == NULL) || (flags & flagsToCheck)) {
1204 genPuntToInterp(cUnit, mir->offset);
1205 return;
1206 }
1207 int entryAddr = offsetof(InterpState,
1208 jitToInterpEntries.dvmJitToInterpSingleStep);
Bill Buzbee270c1d62009-08-13 16:58:07 -07001209 loadWordDisp(cUnit, rGLUE, entryAddr, r2);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001210 /* r0 = dalvik pc */
1211 loadConstant(cUnit, r0, (int) (cUnit->method->insns + mir->offset));
1212 /* r1 = dalvik pc of following instruction */
1213 loadConstant(cUnit, r1, (int) (cUnit->method->insns + mir->next->offset));
Bill Buzbee1465db52009-09-23 17:17:35 -07001214 opReg(cUnit, kOpBlx, r2);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001215}
1216
Ben Chengfc075c22010-05-28 15:20:08 -07001217#if defined(WITH_DEADLOCK_PREDICTION) || defined(WITH_MONITOR_TRACKING) || \
1218 defined(_ARMV5TE) || defined(_ARMV5TE_VFP)
Bill Buzbeec1d9ed42010-02-02 11:04:33 -08001219/*
1220 * To prevent a thread in a monitor wait from blocking the Jit from
1221 * resetting the code cache, heavyweight monitor lock will not
1222 * be allowed to return to an existing translation. Instead, we will
1223 * handle them by branching to a handler, which will in turn call the
1224 * runtime lock routine and then branch directly back to the
1225 * interpreter main loop. Given the high cost of the heavyweight
1226 * lock operation, this additional cost should be slight (especially when
1227 * considering that we expect the vast majority of lock operations to
1228 * use the fast-path thin lock bypass).
1229 */
Ben Cheng5d90c202009-11-22 23:31:11 -08001230static void genMonitorPortable(CompilationUnit *cUnit, MIR *mir)
Bill Buzbee270c1d62009-08-13 16:58:07 -07001231{
Bill Buzbeeefbd3c52009-11-04 22:18:40 -08001232 bool isEnter = (mir->dalvikInsn.opCode == OP_MONITOR_ENTER);
Bill Buzbee1465db52009-09-23 17:17:35 -07001233 genExportPC(cUnit, mir);
Bill Buzbeec6f10662010-02-09 11:16:15 -08001234 dvmCompilerFlushAllRegs(cUnit); /* Send everything to home location */
1235 RegLocation rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07001236 loadValueDirectFixed(cUnit, rlSrc, r1);
1237 loadWordDisp(cUnit, rGLUE, offsetof(InterpState, self), r0);
Bill Buzbeec1d9ed42010-02-02 11:04:33 -08001238 genNullCheck(cUnit, rlSrc.sRegLow, r1, mir->offset, NULL);
Bill Buzbeeefbd3c52009-11-04 22:18:40 -08001239 if (isEnter) {
Bill Buzbeec1d9ed42010-02-02 11:04:33 -08001240 /* Get dPC of next insn */
1241 loadConstant(cUnit, r4PC, (int)(cUnit->method->insns + mir->offset +
1242 dexGetInstrWidthAbs(gDvm.instrWidth, OP_MONITOR_ENTER)));
1243#if defined(WITH_DEADLOCK_PREDICTION)
1244 genDispatchToHandler(cUnit, TEMPLATE_MONITOR_ENTER_DEBUG);
1245#else
1246 genDispatchToHandler(cUnit, TEMPLATE_MONITOR_ENTER);
1247#endif
Bill Buzbee1465db52009-09-23 17:17:35 -07001248 } else {
Ben Chengbd1326d2010-04-02 15:04:53 -07001249 LOAD_FUNC_ADDR(cUnit, r2, (int)dvmUnlockObject);
Bill Buzbeec1d9ed42010-02-02 11:04:33 -08001250 /* Do the call */
1251 opReg(cUnit, kOpBlx, r2);
Bill Buzbee6bbdd6b2010-02-16 14:40:01 -08001252 opRegImm(cUnit, kOpCmp, r0, 0); /* Did we throw? */
1253 ArmLIR *branchOver = opCondBranch(cUnit, kArmCondNe);
1254 loadConstant(cUnit, r0,
1255 (int) (cUnit->method->insns + mir->offset +
1256 dexGetInstrWidthAbs(gDvm.instrWidth, OP_MONITOR_EXIT)));
1257 genDispatchToHandler(cUnit, TEMPLATE_THROW_EXCEPTION_COMMON);
1258 ArmLIR *target = newLIR0(cUnit, kArmPseudoTargetLabel);
1259 target->defMask = ENCODE_ALL;
1260 branchOver->generic.target = (LIR *) target;
Elliott Hughes6a555132010-02-25 15:41:42 -08001261 dvmCompilerClobberCallRegs(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07001262 }
Bill Buzbee270c1d62009-08-13 16:58:07 -07001263}
Ben Chengfc075c22010-05-28 15:20:08 -07001264#endif
Bill Buzbee270c1d62009-08-13 16:58:07 -07001265
Ben Chengba4fc8b2009-06-01 13:00:29 -07001266/*
1267 * The following are the first-level codegen routines that analyze the format
1268 * of each bytecode then either dispatch special purpose codegen routines
1269 * or produce corresponding Thumb instructions directly.
1270 */
1271
1272static bool handleFmt10t_Fmt20t_Fmt30t(CompilationUnit *cUnit, MIR *mir,
Bill Buzbee89efc3d2009-07-28 11:22:22 -07001273 BasicBlock *bb, ArmLIR *labelList)
Ben Chengba4fc8b2009-06-01 13:00:29 -07001274{
1275 /* For OP_GOTO, OP_GOTO_16, and OP_GOTO_32 */
1276 genUnconditionalBranch(cUnit, &labelList[bb->taken->id]);
1277 return false;
1278}
1279
1280static bool handleFmt10x(CompilationUnit *cUnit, MIR *mir)
1281{
1282 OpCode dalvikOpCode = mir->dalvikInsn.opCode;
1283 if (((dalvikOpCode >= OP_UNUSED_3E) && (dalvikOpCode <= OP_UNUSED_43)) ||
Andy McFadden53878242010-03-05 07:24:27 -08001284 ((dalvikOpCode >= OP_UNUSED_E3) && (dalvikOpCode <= OP_UNUSED_E7))) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07001285 LOGE("Codegen: got unused opcode 0x%x\n",dalvikOpCode);
1286 return true;
1287 }
1288 switch (dalvikOpCode) {
1289 case OP_RETURN_VOID:
1290 genReturnCommon(cUnit,mir);
1291 break;
1292 case OP_UNUSED_73:
1293 case OP_UNUSED_79:
1294 case OP_UNUSED_7A:
1295 LOGE("Codegen: got unused opcode 0x%x\n",dalvikOpCode);
1296 return true;
1297 case OP_NOP:
1298 break;
1299 default:
1300 return true;
1301 }
1302 return false;
1303}
1304
1305static bool handleFmt11n_Fmt31i(CompilationUnit *cUnit, MIR *mir)
1306{
Bill Buzbee1465db52009-09-23 17:17:35 -07001307 RegLocation rlDest;
1308 RegLocation rlResult;
1309 if (mir->ssaRep->numDefs == 2) {
Bill Buzbeec6f10662010-02-09 11:16:15 -08001310 rlDest = dvmCompilerGetDestWide(cUnit, mir, 0, 1);
Bill Buzbee1465db52009-09-23 17:17:35 -07001311 } else {
Bill Buzbeec6f10662010-02-09 11:16:15 -08001312 rlDest = dvmCompilerGetDest(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07001313 }
Ben Chenge9695e52009-06-16 16:11:47 -07001314
Ben Chengba4fc8b2009-06-01 13:00:29 -07001315 switch (mir->dalvikInsn.opCode) {
1316 case OP_CONST:
Ben Chenge9695e52009-06-16 16:11:47 -07001317 case OP_CONST_4: {
Bill Buzbeec6f10662010-02-09 11:16:15 -08001318 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kAnyReg, true);
Ben Chengbd1326d2010-04-02 15:04:53 -07001319 loadConstantNoClobber(cUnit, rlResult.lowReg, mir->dalvikInsn.vB);
Bill Buzbee1465db52009-09-23 17:17:35 -07001320 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001321 break;
Ben Chenge9695e52009-06-16 16:11:47 -07001322 }
1323 case OP_CONST_WIDE_32: {
Bill Buzbee1465db52009-09-23 17:17:35 -07001324 //TUNING: single routine to load constant pair for support doubles
Bill Buzbee964a7b02010-01-28 12:54:19 -08001325 //TUNING: load 0/-1 separately to avoid load dependency
Bill Buzbeec6f10662010-02-09 11:16:15 -08001326 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Ben Chengbd1326d2010-04-02 15:04:53 -07001327 loadConstantNoClobber(cUnit, rlResult.lowReg, mir->dalvikInsn.vB);
Bill Buzbee1465db52009-09-23 17:17:35 -07001328 opRegRegImm(cUnit, kOpAsr, rlResult.highReg,
1329 rlResult.lowReg, 31);
1330 storeValueWide(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001331 break;
Ben Chenge9695e52009-06-16 16:11:47 -07001332 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07001333 default:
1334 return true;
1335 }
1336 return false;
1337}
1338
1339static bool handleFmt21h(CompilationUnit *cUnit, MIR *mir)
1340{
Bill Buzbee1465db52009-09-23 17:17:35 -07001341 RegLocation rlDest;
1342 RegLocation rlResult;
1343 if (mir->ssaRep->numDefs == 2) {
Bill Buzbeec6f10662010-02-09 11:16:15 -08001344 rlDest = dvmCompilerGetDestWide(cUnit, mir, 0, 1);
Bill Buzbee1465db52009-09-23 17:17:35 -07001345 } else {
Bill Buzbeec6f10662010-02-09 11:16:15 -08001346 rlDest = dvmCompilerGetDest(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07001347 }
Bill Buzbeec6f10662010-02-09 11:16:15 -08001348 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kAnyReg, true);
Ben Chenge9695e52009-06-16 16:11:47 -07001349
Ben Chengba4fc8b2009-06-01 13:00:29 -07001350 switch (mir->dalvikInsn.opCode) {
Ben Chenge9695e52009-06-16 16:11:47 -07001351 case OP_CONST_HIGH16: {
Ben Chengbd1326d2010-04-02 15:04:53 -07001352 loadConstantNoClobber(cUnit, rlResult.lowReg,
1353 mir->dalvikInsn.vB << 16);
Bill Buzbee1465db52009-09-23 17:17:35 -07001354 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001355 break;
Ben Chenge9695e52009-06-16 16:11:47 -07001356 }
1357 case OP_CONST_WIDE_HIGH16: {
Bill Buzbee1465db52009-09-23 17:17:35 -07001358 loadConstantValueWide(cUnit, rlResult.lowReg, rlResult.highReg,
1359 0, mir->dalvikInsn.vB << 16);
1360 storeValueWide(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001361 break;
Ben Chenge9695e52009-06-16 16:11:47 -07001362 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07001363 default:
1364 return true;
1365 }
1366 return false;
1367}
1368
1369static bool handleFmt20bc(CompilationUnit *cUnit, MIR *mir)
1370{
1371 /* For OP_THROW_VERIFICATION_ERROR */
1372 genInterpSingleStep(cUnit, mir);
1373 return false;
1374}
1375
1376static bool handleFmt21c_Fmt31c(CompilationUnit *cUnit, MIR *mir)
1377{
Bill Buzbee1465db52009-09-23 17:17:35 -07001378 RegLocation rlResult;
1379 RegLocation rlDest;
1380 RegLocation rlSrc;
Ben Chenge9695e52009-06-16 16:11:47 -07001381
Ben Chengba4fc8b2009-06-01 13:00:29 -07001382 switch (mir->dalvikInsn.opCode) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07001383 case OP_CONST_STRING_JUMBO:
1384 case OP_CONST_STRING: {
1385 void *strPtr = (void*)
1386 (cUnit->method->clazz->pDvmDex->pResStrings[mir->dalvikInsn.vB]);
Ben Chengdd6e8702010-05-07 13:05:47 -07001387
1388 if (strPtr == NULL) {
1389 LOGE("Unexpected null string");
1390 dvmAbort();
1391 }
1392
Bill Buzbeec6f10662010-02-09 11:16:15 -08001393 rlDest = dvmCompilerGetDest(cUnit, mir, 0);
1394 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Ben Chengbd1326d2010-04-02 15:04:53 -07001395 loadConstantNoClobber(cUnit, rlResult.lowReg, (int) strPtr );
Bill Buzbee1465db52009-09-23 17:17:35 -07001396 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001397 break;
1398 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07001399 case OP_CONST_CLASS: {
1400 void *classPtr = (void*)
1401 (cUnit->method->clazz->pDvmDex->pResClasses[mir->dalvikInsn.vB]);
Ben Chengdd6e8702010-05-07 13:05:47 -07001402
1403 if (classPtr == NULL) {
1404 LOGE("Unexpected null class");
1405 dvmAbort();
1406 }
1407
Bill Buzbeec6f10662010-02-09 11:16:15 -08001408 rlDest = dvmCompilerGetDest(cUnit, mir, 0);
1409 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Ben Chengbd1326d2010-04-02 15:04:53 -07001410 loadConstantNoClobber(cUnit, rlResult.lowReg, (int) classPtr );
Bill Buzbee1465db52009-09-23 17:17:35 -07001411 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001412 break;
1413 }
1414 case OP_SGET_OBJECT:
1415 case OP_SGET_BOOLEAN:
1416 case OP_SGET_CHAR:
1417 case OP_SGET_BYTE:
1418 case OP_SGET_SHORT:
1419 case OP_SGET: {
Bill Buzbee50a6bf22009-07-08 13:08:04 -07001420 int valOffset = offsetof(StaticField, value);
Bill Buzbeec6f10662010-02-09 11:16:15 -08001421 int tReg = dvmCompilerAllocTemp(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001422 void *fieldPtr = (void*)
1423 (cUnit->method->clazz->pDvmDex->pResFields[mir->dalvikInsn.vB]);
Ben Chengdd6e8702010-05-07 13:05:47 -07001424
1425 if (fieldPtr == NULL) {
1426 LOGE("Unexpected null static field");
1427 dvmAbort();
1428 }
1429
Bill Buzbeec6f10662010-02-09 11:16:15 -08001430 rlDest = dvmCompilerGetDest(cUnit, mir, 0);
1431 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kAnyReg, true);
Bill Buzbee1465db52009-09-23 17:17:35 -07001432 loadConstant(cUnit, tReg, (int) fieldPtr + valOffset);
Ben Cheng11d8f142010-03-24 15:24:19 -07001433
1434 HEAP_ACCESS_SHADOW(true);
Bill Buzbee1465db52009-09-23 17:17:35 -07001435 loadWordDisp(cUnit, tReg, 0, rlResult.lowReg);
Ben Cheng11d8f142010-03-24 15:24:19 -07001436 HEAP_ACCESS_SHADOW(false);
1437
Bill Buzbee1465db52009-09-23 17:17:35 -07001438 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001439 break;
1440 }
1441 case OP_SGET_WIDE: {
Bill Buzbee50a6bf22009-07-08 13:08:04 -07001442 int valOffset = offsetof(StaticField, value);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001443 void *fieldPtr = (void*)
1444 (cUnit->method->clazz->pDvmDex->pResFields[mir->dalvikInsn.vB]);
Ben Chengdd6e8702010-05-07 13:05:47 -07001445
1446 if (fieldPtr == NULL) {
1447 LOGE("Unexpected null static field");
1448 dvmAbort();
1449 }
1450
Bill Buzbeec6f10662010-02-09 11:16:15 -08001451 int tReg = dvmCompilerAllocTemp(cUnit);
Bill Buzbeec6f10662010-02-09 11:16:15 -08001452 rlDest = dvmCompilerGetDestWide(cUnit, mir, 0, 1);
1453 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kAnyReg, true);
Bill Buzbee1465db52009-09-23 17:17:35 -07001454 loadConstant(cUnit, tReg, (int) fieldPtr + valOffset);
Ben Cheng11d8f142010-03-24 15:24:19 -07001455
1456 HEAP_ACCESS_SHADOW(true);
Bill Buzbee1465db52009-09-23 17:17:35 -07001457 loadPair(cUnit, tReg, rlResult.lowReg, rlResult.highReg);
Ben Cheng11d8f142010-03-24 15:24:19 -07001458 HEAP_ACCESS_SHADOW(false);
1459
Bill Buzbee1465db52009-09-23 17:17:35 -07001460 storeValueWide(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001461 break;
1462 }
1463 case OP_SPUT_OBJECT:
1464 case OP_SPUT_BOOLEAN:
1465 case OP_SPUT_CHAR:
1466 case OP_SPUT_BYTE:
1467 case OP_SPUT_SHORT:
1468 case OP_SPUT: {
Bill Buzbee50a6bf22009-07-08 13:08:04 -07001469 int valOffset = offsetof(StaticField, value);
Bill Buzbeec6f10662010-02-09 11:16:15 -08001470 int tReg = dvmCompilerAllocTemp(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001471 void *fieldPtr = (void*)
1472 (cUnit->method->clazz->pDvmDex->pResFields[mir->dalvikInsn.vB]);
Ben Chenge9695e52009-06-16 16:11:47 -07001473
Ben Chengdd6e8702010-05-07 13:05:47 -07001474 if (fieldPtr == NULL) {
1475 LOGE("Unexpected null static field");
1476 dvmAbort();
1477 }
1478
Bill Buzbeec6f10662010-02-09 11:16:15 -08001479 rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07001480 rlSrc = loadValue(cUnit, rlSrc, kAnyReg);
1481 loadConstant(cUnit, tReg, (int) fieldPtr + valOffset);
Ben Cheng11d8f142010-03-24 15:24:19 -07001482
1483 HEAP_ACCESS_SHADOW(true);
Bill Buzbee1465db52009-09-23 17:17:35 -07001484 storeWordDisp(cUnit, tReg, 0 ,rlSrc.lowReg);
Ben Cheng11d8f142010-03-24 15:24:19 -07001485 HEAP_ACCESS_SHADOW(false);
1486
Ben Chengba4fc8b2009-06-01 13:00:29 -07001487 break;
1488 }
1489 case OP_SPUT_WIDE: {
Bill Buzbeec6f10662010-02-09 11:16:15 -08001490 int tReg = dvmCompilerAllocTemp(cUnit);
Bill Buzbee50a6bf22009-07-08 13:08:04 -07001491 int valOffset = offsetof(StaticField, value);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001492 void *fieldPtr = (void*)
1493 (cUnit->method->clazz->pDvmDex->pResFields[mir->dalvikInsn.vB]);
Ben Chenge9695e52009-06-16 16:11:47 -07001494
Ben Chengdd6e8702010-05-07 13:05:47 -07001495 if (fieldPtr == NULL) {
1496 LOGE("Unexpected null static field");
1497 dvmAbort();
1498 }
1499
Bill Buzbeec6f10662010-02-09 11:16:15 -08001500 rlSrc = dvmCompilerGetSrcWide(cUnit, mir, 0, 1);
Bill Buzbee1465db52009-09-23 17:17:35 -07001501 rlSrc = loadValueWide(cUnit, rlSrc, kAnyReg);
1502 loadConstant(cUnit, tReg, (int) fieldPtr + valOffset);
Ben Cheng11d8f142010-03-24 15:24:19 -07001503
1504 HEAP_ACCESS_SHADOW(true);
Bill Buzbee1465db52009-09-23 17:17:35 -07001505 storePair(cUnit, tReg, rlSrc.lowReg, rlSrc.highReg);
Ben Cheng11d8f142010-03-24 15:24:19 -07001506 HEAP_ACCESS_SHADOW(false);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001507 break;
1508 }
1509 case OP_NEW_INSTANCE: {
Ben Chenge9695e52009-06-16 16:11:47 -07001510 /*
1511 * Obey the calling convention and don't mess with the register
1512 * usage.
1513 */
Ben Chengba4fc8b2009-06-01 13:00:29 -07001514 ClassObject *classPtr = (void*)
1515 (cUnit->method->clazz->pDvmDex->pResClasses[mir->dalvikInsn.vB]);
Ben Chengdd6e8702010-05-07 13:05:47 -07001516
1517 if (classPtr == NULL) {
1518 LOGE("Unexpected null class");
1519 dvmAbort();
1520 }
1521
Ben Cheng79d173c2009-09-29 16:12:51 -07001522 /*
1523 * If it is going to throw, it should not make to the trace to begin
Bill Buzbee1465db52009-09-23 17:17:35 -07001524 * with. However, Alloc might throw, so we need to genExportPC()
Ben Cheng79d173c2009-09-29 16:12:51 -07001525 */
1526 assert((classPtr->accessFlags & (ACC_INTERFACE|ACC_ABSTRACT)) == 0);
Bill Buzbeec6f10662010-02-09 11:16:15 -08001527 dvmCompilerFlushAllRegs(cUnit); /* Everything to home location */
Bill Buzbee1465db52009-09-23 17:17:35 -07001528 genExportPC(cUnit, mir);
Ben Chengbd1326d2010-04-02 15:04:53 -07001529 LOAD_FUNC_ADDR(cUnit, r2, (int)dvmAllocObject);
Ben Chenge9695e52009-06-16 16:11:47 -07001530 loadConstant(cUnit, r0, (int) classPtr);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001531 loadConstant(cUnit, r1, ALLOC_DONT_TRACK);
Bill Buzbee1465db52009-09-23 17:17:35 -07001532 opReg(cUnit, kOpBlx, r2);
Elliott Hughes6a555132010-02-25 15:41:42 -08001533 dvmCompilerClobberCallRegs(cUnit);
Ben Cheng4f489172009-09-27 17:08:35 -07001534 /* generate a branch over if allocation is successful */
Bill Buzbee1465db52009-09-23 17:17:35 -07001535 opRegImm(cUnit, kOpCmp, r0, 0); /* NULL? */
1536 ArmLIR *branchOver = opCondBranch(cUnit, kArmCondNe);
Ben Cheng4f489172009-09-27 17:08:35 -07001537 /*
1538 * OOM exception needs to be thrown here and cannot re-execute
1539 */
1540 loadConstant(cUnit, r0,
1541 (int) (cUnit->method->insns + mir->offset));
1542 genDispatchToHandler(cUnit, TEMPLATE_THROW_EXCEPTION_COMMON);
1543 /* noreturn */
1544
Bill Buzbee1465db52009-09-23 17:17:35 -07001545 ArmLIR *target = newLIR0(cUnit, kArmPseudoTargetLabel);
Ben Cheng4f489172009-09-27 17:08:35 -07001546 target->defMask = ENCODE_ALL;
1547 branchOver->generic.target = (LIR *) target;
Bill Buzbeec6f10662010-02-09 11:16:15 -08001548 rlDest = dvmCompilerGetDest(cUnit, mir, 0);
1549 rlResult = dvmCompilerGetReturn(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07001550 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001551 break;
1552 }
1553 case OP_CHECK_CAST: {
Ben Chenge9695e52009-06-16 16:11:47 -07001554 /*
1555 * Obey the calling convention and don't mess with the register
1556 * usage.
1557 */
Ben Chengba4fc8b2009-06-01 13:00:29 -07001558 ClassObject *classPtr =
1559 (cUnit->method->clazz->pDvmDex->pResClasses[mir->dalvikInsn.vB]);
Bill Buzbee4df41a52009-11-12 17:07:16 -08001560 /*
1561 * Note: It is possible that classPtr is NULL at this point,
1562 * even though this instruction has been successfully interpreted.
1563 * If the previous interpretation had a null source, the
1564 * interpreter would not have bothered to resolve the clazz.
1565 * Bail out to the interpreter in this case, and log it
1566 * so that we can tell if it happens frequently.
1567 */
1568 if (classPtr == NULL) {
Ben Cheng11d8f142010-03-24 15:24:19 -07001569 LOGVV("null clazz in OP_CHECK_CAST, single-stepping");
Bill Buzbee4df41a52009-11-12 17:07:16 -08001570 genInterpSingleStep(cUnit, mir);
1571 return false;
1572 }
Bill Buzbeec6f10662010-02-09 11:16:15 -08001573 dvmCompilerFlushAllRegs(cUnit); /* Everything to home location */
Ben Chengba4fc8b2009-06-01 13:00:29 -07001574 loadConstant(cUnit, r1, (int) classPtr );
Bill Buzbeec6f10662010-02-09 11:16:15 -08001575 rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07001576 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
1577 opRegImm(cUnit, kOpCmp, rlSrc.lowReg, 0); /* Null? */
1578 ArmLIR *branch1 = opCondBranch(cUnit, kArmCondEq);
1579 /*
1580 * rlSrc.lowReg now contains object->clazz. Note that
1581 * it could have been allocated r0, but we're okay so long
1582 * as we don't do anything desctructive until r0 is loaded
1583 * with clazz.
1584 */
Ben Chengba4fc8b2009-06-01 13:00:29 -07001585 /* r0 now contains object->clazz */
Bill Buzbee1465db52009-09-23 17:17:35 -07001586 loadWordDisp(cUnit, rlSrc.lowReg, offsetof(Object, clazz), r0);
Ben Chengbd1326d2010-04-02 15:04:53 -07001587 LOAD_FUNC_ADDR(cUnit, r2, (int)dvmInstanceofNonTrivial);
Bill Buzbee1465db52009-09-23 17:17:35 -07001588 opRegReg(cUnit, kOpCmp, r0, r1);
1589 ArmLIR *branch2 = opCondBranch(cUnit, kArmCondEq);
1590 opReg(cUnit, kOpBlx, r2);
Elliott Hughes6a555132010-02-25 15:41:42 -08001591 dvmCompilerClobberCallRegs(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07001592 /*
1593 * If null, check cast failed - punt to the interpreter. Because
1594 * interpreter will be the one throwing, we don't need to
1595 * genExportPC() here.
1596 */
Bill Buzbee270c1d62009-08-13 16:58:07 -07001597 genZeroCheck(cUnit, r0, mir->offset, NULL);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001598 /* check cast passed - branch target here */
Bill Buzbee1465db52009-09-23 17:17:35 -07001599 ArmLIR *target = newLIR0(cUnit, kArmPseudoTargetLabel);
Ben Chengd7d426a2009-09-22 11:23:36 -07001600 target->defMask = ENCODE_ALL;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001601 branch1->generic.target = (LIR *)target;
1602 branch2->generic.target = (LIR *)target;
1603 break;
1604 }
1605 default:
1606 return true;
1607 }
1608 return false;
1609}
1610
1611static bool handleFmt11x(CompilationUnit *cUnit, MIR *mir)
1612{
1613 OpCode dalvikOpCode = mir->dalvikInsn.opCode;
Bill Buzbee1465db52009-09-23 17:17:35 -07001614 RegLocation rlResult;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001615 switch (dalvikOpCode) {
1616 case OP_MOVE_EXCEPTION: {
1617 int offset = offsetof(InterpState, self);
1618 int exOffset = offsetof(Thread, exception);
Bill Buzbeec6f10662010-02-09 11:16:15 -08001619 int selfReg = dvmCompilerAllocTemp(cUnit);
1620 int resetReg = dvmCompilerAllocTemp(cUnit);
1621 RegLocation rlDest = dvmCompilerGetDest(cUnit, mir, 0);
1622 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Bill Buzbee1465db52009-09-23 17:17:35 -07001623 loadWordDisp(cUnit, rGLUE, offset, selfReg);
Bill Buzbeef9f33282009-11-22 12:45:30 -08001624 loadConstant(cUnit, resetReg, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07001625 loadWordDisp(cUnit, selfReg, exOffset, rlResult.lowReg);
Bill Buzbeef9f33282009-11-22 12:45:30 -08001626 storeWordDisp(cUnit, selfReg, exOffset, resetReg);
Bill Buzbee1465db52009-09-23 17:17:35 -07001627 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001628 break;
1629 }
1630 case OP_MOVE_RESULT:
1631 case OP_MOVE_RESULT_OBJECT: {
Bill Buzbeec6f10662010-02-09 11:16:15 -08001632 RegLocation rlDest = dvmCompilerGetDest(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07001633 RegLocation rlSrc = LOC_DALVIK_RETURN_VAL;
1634 rlSrc.fp = rlDest.fp;
1635 storeValue(cUnit, rlDest, rlSrc);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001636 break;
1637 }
1638 case OP_MOVE_RESULT_WIDE: {
Bill Buzbeec6f10662010-02-09 11:16:15 -08001639 RegLocation rlDest = dvmCompilerGetDestWide(cUnit, mir, 0, 1);
Bill Buzbee1465db52009-09-23 17:17:35 -07001640 RegLocation rlSrc = LOC_DALVIK_RETURN_VAL_WIDE;
1641 rlSrc.fp = rlDest.fp;
1642 storeValueWide(cUnit, rlDest, rlSrc);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001643 break;
1644 }
1645 case OP_RETURN_WIDE: {
Bill Buzbeec6f10662010-02-09 11:16:15 -08001646 RegLocation rlSrc = dvmCompilerGetSrcWide(cUnit, mir, 0, 1);
Bill Buzbee1465db52009-09-23 17:17:35 -07001647 RegLocation rlDest = LOC_DALVIK_RETURN_VAL_WIDE;
1648 rlDest.fp = rlSrc.fp;
1649 storeValueWide(cUnit, rlDest, rlSrc);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001650 genReturnCommon(cUnit,mir);
1651 break;
1652 }
1653 case OP_RETURN:
1654 case OP_RETURN_OBJECT: {
Bill Buzbeec6f10662010-02-09 11:16:15 -08001655 RegLocation rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07001656 RegLocation rlDest = LOC_DALVIK_RETURN_VAL;
1657 rlDest.fp = rlSrc.fp;
1658 storeValue(cUnit, rlDest, rlSrc);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001659 genReturnCommon(cUnit,mir);
1660 break;
1661 }
Bill Buzbee1465db52009-09-23 17:17:35 -07001662 case OP_MONITOR_EXIT:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001663 case OP_MONITOR_ENTER:
Bill Buzbeed0937ef2009-12-22 16:15:39 -08001664#if defined(WITH_DEADLOCK_PREDICTION) || defined(WITH_MONITOR_TRACKING)
Ben Cheng5d90c202009-11-22 23:31:11 -08001665 genMonitorPortable(cUnit, mir);
Bill Buzbee1465db52009-09-23 17:17:35 -07001666#else
Ben Cheng5d90c202009-11-22 23:31:11 -08001667 genMonitor(cUnit, mir);
Bill Buzbee1465db52009-09-23 17:17:35 -07001668#endif
Ben Chengba4fc8b2009-06-01 13:00:29 -07001669 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001670 case OP_THROW: {
1671 genInterpSingleStep(cUnit, mir);
1672 break;
1673 }
1674 default:
1675 return true;
1676 }
1677 return false;
1678}
1679
Bill Buzbeed45ba372009-06-15 17:00:57 -07001680static bool handleFmt12x(CompilationUnit *cUnit, MIR *mir)
1681{
1682 OpCode opCode = mir->dalvikInsn.opCode;
Bill Buzbee1465db52009-09-23 17:17:35 -07001683 RegLocation rlDest;
1684 RegLocation rlSrc;
1685 RegLocation rlResult;
Bill Buzbeed45ba372009-06-15 17:00:57 -07001686
Ben Chengba4fc8b2009-06-01 13:00:29 -07001687 if ( (opCode >= OP_ADD_INT_2ADDR) && (opCode <= OP_REM_DOUBLE_2ADDR)) {
Ben Cheng5d90c202009-11-22 23:31:11 -08001688 return genArithOp( cUnit, mir );
Ben Chengba4fc8b2009-06-01 13:00:29 -07001689 }
1690
Bill Buzbee1465db52009-09-23 17:17:35 -07001691 if (mir->ssaRep->numUses == 2)
Bill Buzbeec6f10662010-02-09 11:16:15 -08001692 rlSrc = dvmCompilerGetSrcWide(cUnit, mir, 0, 1);
Bill Buzbee1465db52009-09-23 17:17:35 -07001693 else
Bill Buzbeec6f10662010-02-09 11:16:15 -08001694 rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07001695 if (mir->ssaRep->numDefs == 2)
Bill Buzbeec6f10662010-02-09 11:16:15 -08001696 rlDest = dvmCompilerGetDestWide(cUnit, mir, 0, 1);
Bill Buzbee1465db52009-09-23 17:17:35 -07001697 else
Bill Buzbeec6f10662010-02-09 11:16:15 -08001698 rlDest = dvmCompilerGetDest(cUnit, mir, 0);
Ben Chenge9695e52009-06-16 16:11:47 -07001699
Ben Chengba4fc8b2009-06-01 13:00:29 -07001700 switch (opCode) {
Bill Buzbee1465db52009-09-23 17:17:35 -07001701 case OP_DOUBLE_TO_INT:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001702 case OP_INT_TO_FLOAT:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001703 case OP_FLOAT_TO_INT:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001704 case OP_DOUBLE_TO_FLOAT:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001705 case OP_FLOAT_TO_DOUBLE:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001706 case OP_INT_TO_DOUBLE:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001707 case OP_FLOAT_TO_LONG:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001708 case OP_LONG_TO_FLOAT:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001709 case OP_DOUBLE_TO_LONG:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001710 case OP_LONG_TO_DOUBLE:
Ben Cheng5d90c202009-11-22 23:31:11 -08001711 return genConversion(cUnit, mir);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001712 case OP_NEG_INT:
1713 case OP_NOT_INT:
Ben Cheng5d90c202009-11-22 23:31:11 -08001714 return genArithOpInt(cUnit, mir, rlDest, rlSrc, rlSrc);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001715 case OP_NEG_LONG:
1716 case OP_NOT_LONG:
Ben Cheng5d90c202009-11-22 23:31:11 -08001717 return genArithOpLong(cUnit, mir, rlDest, rlSrc, rlSrc);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001718 case OP_NEG_FLOAT:
Ben Cheng5d90c202009-11-22 23:31:11 -08001719 return genArithOpFloat(cUnit, mir, rlDest, rlSrc, rlSrc);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001720 case OP_NEG_DOUBLE:
Ben Cheng5d90c202009-11-22 23:31:11 -08001721 return genArithOpDouble(cUnit, mir, rlDest, rlSrc, rlSrc);
Bill Buzbee1465db52009-09-23 17:17:35 -07001722 case OP_MOVE_WIDE:
1723 storeValueWide(cUnit, rlDest, rlSrc);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001724 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07001725 case OP_INT_TO_LONG:
Bill Buzbeec6f10662010-02-09 11:16:15 -08001726 rlSrc = dvmCompilerUpdateLoc(cUnit, rlSrc);
1727 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Bill Buzbee964a7b02010-01-28 12:54:19 -08001728 //TUNING: shouldn't loadValueDirect already check for phys reg?
Bill Buzbee1465db52009-09-23 17:17:35 -07001729 if (rlSrc.location == kLocPhysReg) {
1730 genRegCopy(cUnit, rlResult.lowReg, rlSrc.lowReg);
1731 } else {
1732 loadValueDirect(cUnit, rlSrc, rlResult.lowReg);
1733 }
1734 opRegRegImm(cUnit, kOpAsr, rlResult.highReg,
1735 rlResult.lowReg, 31);
1736 storeValueWide(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001737 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07001738 case OP_LONG_TO_INT:
Bill Buzbeec6f10662010-02-09 11:16:15 -08001739 rlSrc = dvmCompilerUpdateLocWide(cUnit, rlSrc);
1740 rlSrc = dvmCompilerWideToNarrow(cUnit, rlSrc);
Bill Buzbee1465db52009-09-23 17:17:35 -07001741 // Intentional fallthrough
Ben Chengba4fc8b2009-06-01 13:00:29 -07001742 case OP_MOVE:
1743 case OP_MOVE_OBJECT:
Bill Buzbee1465db52009-09-23 17:17:35 -07001744 storeValue(cUnit, rlDest, rlSrc);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001745 break;
1746 case OP_INT_TO_BYTE:
Bill Buzbee1465db52009-09-23 17:17:35 -07001747 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
Bill Buzbeec6f10662010-02-09 11:16:15 -08001748 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Bill Buzbee1465db52009-09-23 17:17:35 -07001749 opRegReg(cUnit, kOp2Byte, rlResult.lowReg, rlSrc.lowReg);
1750 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001751 break;
1752 case OP_INT_TO_SHORT:
Bill Buzbee1465db52009-09-23 17:17:35 -07001753 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
Bill Buzbeec6f10662010-02-09 11:16:15 -08001754 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Bill Buzbee1465db52009-09-23 17:17:35 -07001755 opRegReg(cUnit, kOp2Short, rlResult.lowReg, rlSrc.lowReg);
1756 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001757 break;
1758 case OP_INT_TO_CHAR:
Bill Buzbee1465db52009-09-23 17:17:35 -07001759 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
Bill Buzbeec6f10662010-02-09 11:16:15 -08001760 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Bill Buzbee1465db52009-09-23 17:17:35 -07001761 opRegReg(cUnit, kOp2Char, rlResult.lowReg, rlSrc.lowReg);
1762 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001763 break;
1764 case OP_ARRAY_LENGTH: {
1765 int lenOffset = offsetof(ArrayObject, length);
Bill Buzbee1465db52009-09-23 17:17:35 -07001766 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
1767 genNullCheck(cUnit, rlSrc.sRegLow, rlSrc.lowReg,
1768 mir->offset, NULL);
Bill Buzbeec6f10662010-02-09 11:16:15 -08001769 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Bill Buzbee1465db52009-09-23 17:17:35 -07001770 loadWordDisp(cUnit, rlSrc.lowReg, lenOffset,
1771 rlResult.lowReg);
1772 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001773 break;
1774 }
1775 default:
1776 return true;
1777 }
1778 return false;
1779}
1780
1781static bool handleFmt21s(CompilationUnit *cUnit, MIR *mir)
1782{
1783 OpCode dalvikOpCode = mir->dalvikInsn.opCode;
Bill Buzbee1465db52009-09-23 17:17:35 -07001784 RegLocation rlDest;
1785 RegLocation rlResult;
1786 int BBBB = mir->dalvikInsn.vB;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001787 if (dalvikOpCode == OP_CONST_WIDE_16) {
Bill Buzbeec6f10662010-02-09 11:16:15 -08001788 rlDest = dvmCompilerGetDestWide(cUnit, mir, 0, 1);
1789 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Ben Chengbd1326d2010-04-02 15:04:53 -07001790 loadConstantNoClobber(cUnit, rlResult.lowReg, BBBB);
Bill Buzbee964a7b02010-01-28 12:54:19 -08001791 //TUNING: do high separately to avoid load dependency
Bill Buzbee1465db52009-09-23 17:17:35 -07001792 opRegRegImm(cUnit, kOpAsr, rlResult.highReg, rlResult.lowReg, 31);
1793 storeValueWide(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001794 } else if (dalvikOpCode == OP_CONST_16) {
Bill Buzbeec6f10662010-02-09 11:16:15 -08001795 rlDest = dvmCompilerGetDest(cUnit, mir, 0);
1796 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kAnyReg, true);
Ben Chengbd1326d2010-04-02 15:04:53 -07001797 loadConstantNoClobber(cUnit, rlResult.lowReg, BBBB);
Bill Buzbee1465db52009-09-23 17:17:35 -07001798 storeValue(cUnit, rlDest, rlResult);
1799 } else
Ben Chengba4fc8b2009-06-01 13:00:29 -07001800 return true;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001801 return false;
1802}
1803
1804/* Compare agaist zero */
1805static bool handleFmt21t(CompilationUnit *cUnit, MIR *mir, BasicBlock *bb,
Bill Buzbee89efc3d2009-07-28 11:22:22 -07001806 ArmLIR *labelList)
Ben Chengba4fc8b2009-06-01 13:00:29 -07001807{
1808 OpCode dalvikOpCode = mir->dalvikInsn.opCode;
Bill Buzbee89efc3d2009-07-28 11:22:22 -07001809 ArmConditionCode cond;
Bill Buzbeec6f10662010-02-09 11:16:15 -08001810 RegLocation rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07001811 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
1812 opRegImm(cUnit, kOpCmp, rlSrc.lowReg, 0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001813
Bill Buzbee270c1d62009-08-13 16:58:07 -07001814//TUNING: break this out to allow use of Thumb2 CB[N]Z
Ben Chengba4fc8b2009-06-01 13:00:29 -07001815 switch (dalvikOpCode) {
1816 case OP_IF_EQZ:
Bill Buzbee1465db52009-09-23 17:17:35 -07001817 cond = kArmCondEq;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001818 break;
1819 case OP_IF_NEZ:
Bill Buzbee1465db52009-09-23 17:17:35 -07001820 cond = kArmCondNe;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001821 break;
1822 case OP_IF_LTZ:
Bill Buzbee1465db52009-09-23 17:17:35 -07001823 cond = kArmCondLt;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001824 break;
1825 case OP_IF_GEZ:
Bill Buzbee1465db52009-09-23 17:17:35 -07001826 cond = kArmCondGe;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001827 break;
1828 case OP_IF_GTZ:
Bill Buzbee1465db52009-09-23 17:17:35 -07001829 cond = kArmCondGt;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001830 break;
1831 case OP_IF_LEZ:
Bill Buzbee1465db52009-09-23 17:17:35 -07001832 cond = kArmCondLe;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001833 break;
1834 default:
1835 cond = 0;
1836 LOGE("Unexpected opcode (%d) for Fmt21t\n", dalvikOpCode);
Bill Buzbeefc519dc2010-03-06 23:30:57 -08001837 dvmCompilerAbort(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001838 }
1839 genConditionalBranch(cUnit, cond, &labelList[bb->taken->id]);
1840 /* This mostly likely will be optimized away in a later phase */
1841 genUnconditionalBranch(cUnit, &labelList[bb->fallThrough->id]);
1842 return false;
1843}
1844
Elliott Hughesb4c05972010-02-24 16:36:18 -08001845static bool isPowerOfTwo(int x)
1846{
1847 return (x & (x - 1)) == 0;
1848}
1849
1850// Returns true if no more than two bits are set in 'x'.
1851static bool isPopCountLE2(unsigned int x)
1852{
1853 x &= x - 1;
1854 return (x & (x - 1)) == 0;
1855}
1856
1857// Returns the index of the lowest set bit in 'x'.
1858static int lowestSetBit(unsigned int x) {
1859 int bit_posn = 0;
1860 while ((x & 0xf) == 0) {
1861 bit_posn += 4;
1862 x >>= 4;
Bill Buzbee78cb0e22010-02-11 14:04:53 -08001863 }
Elliott Hughesb4c05972010-02-24 16:36:18 -08001864 while ((x & 1) == 0) {
1865 bit_posn++;
1866 x >>= 1;
1867 }
1868 return bit_posn;
1869}
1870
Elliott Hughes672511b2010-04-26 17:40:13 -07001871// Returns true if it added instructions to 'cUnit' to divide 'rlSrc' by 'lit'
1872// and store the result in 'rlDest'.
Elliott Hughesc7ad9b22010-04-28 13:52:02 -07001873static bool handleEasyDivide(CompilationUnit *cUnit, OpCode dalvikOpCode,
Elliott Hughes672511b2010-04-26 17:40:13 -07001874 RegLocation rlSrc, RegLocation rlDest, int lit)
1875{
1876 if (lit < 2 || !isPowerOfTwo(lit)) {
1877 return false;
1878 }
1879 int k = lowestSetBit(lit);
1880 if (k >= 30) {
1881 // Avoid special cases.
1882 return false;
1883 }
Elliott Hughes9c457022010-04-28 16:15:38 -07001884 bool div = (dalvikOpCode == OP_DIV_INT_LIT8 || dalvikOpCode == OP_DIV_INT_LIT16);
Elliott Hughes672511b2010-04-26 17:40:13 -07001885 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
1886 RegLocation rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Elliott Hughes9c457022010-04-28 16:15:38 -07001887 if (div) {
1888 int tReg = dvmCompilerAllocTemp(cUnit);
1889 if (lit == 2) {
1890 // Division by 2 is by far the most common division by constant.
1891 opRegRegImm(cUnit, kOpLsr, tReg, rlSrc.lowReg, 32 - k);
1892 opRegRegReg(cUnit, kOpAdd, tReg, tReg, rlSrc.lowReg);
1893 opRegRegImm(cUnit, kOpAsr, rlResult.lowReg, tReg, k);
1894 } else {
1895 opRegRegImm(cUnit, kOpAsr, tReg, rlSrc.lowReg, 31);
1896 opRegRegImm(cUnit, kOpLsr, tReg, tReg, 32 - k);
1897 opRegRegReg(cUnit, kOpAdd, tReg, tReg, rlSrc.lowReg);
1898 opRegRegImm(cUnit, kOpAsr, rlResult.lowReg, tReg, k);
1899 }
Elliott Hughes672511b2010-04-26 17:40:13 -07001900 } else {
Elliott Hughes9c457022010-04-28 16:15:38 -07001901 int cReg = dvmCompilerAllocTemp(cUnit);
1902 loadConstant(cUnit, cReg, lit - 1);
1903 int tReg1 = dvmCompilerAllocTemp(cUnit);
1904 int tReg2 = dvmCompilerAllocTemp(cUnit);
1905 if (lit == 2) {
1906 opRegRegImm(cUnit, kOpLsr, tReg1, rlSrc.lowReg, 32 - k);
1907 opRegRegReg(cUnit, kOpAdd, tReg2, tReg1, rlSrc.lowReg);
1908 opRegRegReg(cUnit, kOpAnd, tReg2, tReg2, cReg);
1909 opRegRegReg(cUnit, kOpSub, rlResult.lowReg, tReg2, tReg1);
1910 } else {
1911 opRegRegImm(cUnit, kOpAsr, tReg1, rlSrc.lowReg, 31);
1912 opRegRegImm(cUnit, kOpLsr, tReg1, tReg1, 32 - k);
1913 opRegRegReg(cUnit, kOpAdd, tReg2, tReg1, rlSrc.lowReg);
1914 opRegRegReg(cUnit, kOpAnd, tReg2, tReg2, cReg);
1915 opRegRegReg(cUnit, kOpSub, rlResult.lowReg, tReg2, tReg1);
1916 }
Elliott Hughes672511b2010-04-26 17:40:13 -07001917 }
1918 storeValue(cUnit, rlDest, rlResult);
1919 return true;
1920}
1921
Elliott Hughesb4c05972010-02-24 16:36:18 -08001922// Returns true if it added instructions to 'cUnit' to multiply 'rlSrc' by 'lit'
1923// and store the result in 'rlDest'.
1924static bool handleEasyMultiply(CompilationUnit *cUnit,
1925 RegLocation rlSrc, RegLocation rlDest, int lit)
1926{
1927 // Can we simplify this multiplication?
1928 bool powerOfTwo = false;
1929 bool popCountLE2 = false;
1930 bool powerOfTwoMinusOne = false;
1931 if (lit < 2) {
1932 // Avoid special cases.
1933 return false;
1934 } else if (isPowerOfTwo(lit)) {
1935 powerOfTwo = true;
1936 } else if (isPopCountLE2(lit)) {
1937 popCountLE2 = true;
1938 } else if (isPowerOfTwo(lit + 1)) {
1939 powerOfTwoMinusOne = true;
1940 } else {
1941 return false;
1942 }
1943 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
1944 RegLocation rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
1945 if (powerOfTwo) {
1946 // Shift.
1947 opRegRegImm(cUnit, kOpLsl, rlResult.lowReg, rlSrc.lowReg,
1948 lowestSetBit(lit));
1949 } else if (popCountLE2) {
1950 // Shift and add and shift.
1951 int firstBit = lowestSetBit(lit);
1952 int secondBit = lowestSetBit(lit ^ (1 << firstBit));
1953 genMultiplyByTwoBitMultiplier(cUnit, rlSrc, rlResult, lit,
1954 firstBit, secondBit);
1955 } else {
1956 // Reverse subtract: (src << (shift + 1)) - src.
1957 assert(powerOfTwoMinusOne);
1958 // TODO: rsb dst, src, src lsl#lowestSetBit(lit + 1)
1959 int tReg = dvmCompilerAllocTemp(cUnit);
1960 opRegRegImm(cUnit, kOpLsl, tReg, rlSrc.lowReg, lowestSetBit(lit + 1));
1961 opRegRegReg(cUnit, kOpSub, rlResult.lowReg, tReg, rlSrc.lowReg);
1962 }
1963 storeValue(cUnit, rlDest, rlResult);
1964 return true;
Bill Buzbee78cb0e22010-02-11 14:04:53 -08001965}
1966
Ben Chengba4fc8b2009-06-01 13:00:29 -07001967static bool handleFmt22b_Fmt22s(CompilationUnit *cUnit, MIR *mir)
1968{
1969 OpCode dalvikOpCode = mir->dalvikInsn.opCode;
Bill Buzbeec6f10662010-02-09 11:16:15 -08001970 RegLocation rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
1971 RegLocation rlDest = dvmCompilerGetDest(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07001972 RegLocation rlResult;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001973 int lit = mir->dalvikInsn.vC;
Ben Cheng4f489172009-09-27 17:08:35 -07001974 OpKind op = 0; /* Make gcc happy */
Bill Buzbee1465db52009-09-23 17:17:35 -07001975 int shiftOp = false;
1976 bool isDiv = false;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001977
Ben Chengba4fc8b2009-06-01 13:00:29 -07001978 switch (dalvikOpCode) {
Bill Buzbee1465db52009-09-23 17:17:35 -07001979 case OP_RSUB_INT_LIT8:
1980 case OP_RSUB_INT: {
1981 int tReg;
1982 //TUNING: add support for use of Arm rsub op
1983 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
Bill Buzbeec6f10662010-02-09 11:16:15 -08001984 tReg = dvmCompilerAllocTemp(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07001985 loadConstant(cUnit, tReg, lit);
Bill Buzbeec6f10662010-02-09 11:16:15 -08001986 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Bill Buzbee1465db52009-09-23 17:17:35 -07001987 opRegRegReg(cUnit, kOpSub, rlResult.lowReg,
1988 tReg, rlSrc.lowReg);
1989 storeValue(cUnit, rlDest, rlResult);
1990 return false;
1991 break;
1992 }
1993
Ben Chengba4fc8b2009-06-01 13:00:29 -07001994 case OP_ADD_INT_LIT8:
1995 case OP_ADD_INT_LIT16:
Bill Buzbee1465db52009-09-23 17:17:35 -07001996 op = kOpAdd;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001997 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001998 case OP_MUL_INT_LIT8:
Bill Buzbee78cb0e22010-02-11 14:04:53 -08001999 case OP_MUL_INT_LIT16: {
Elliott Hughesb4c05972010-02-24 16:36:18 -08002000 if (handleEasyMultiply(cUnit, rlSrc, rlDest, lit)) {
2001 return false;
Bill Buzbee78cb0e22010-02-11 14:04:53 -08002002 }
Elliott Hughesb4c05972010-02-24 16:36:18 -08002003 op = kOpMul;
Bill Buzbee1465db52009-09-23 17:17:35 -07002004 break;
Bill Buzbee78cb0e22010-02-11 14:04:53 -08002005 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07002006 case OP_AND_INT_LIT8:
2007 case OP_AND_INT_LIT16:
Bill Buzbee1465db52009-09-23 17:17:35 -07002008 op = kOpAnd;
2009 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002010 case OP_OR_INT_LIT8:
2011 case OP_OR_INT_LIT16:
Bill Buzbee1465db52009-09-23 17:17:35 -07002012 op = kOpOr;
2013 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002014 case OP_XOR_INT_LIT8:
2015 case OP_XOR_INT_LIT16:
Bill Buzbee1465db52009-09-23 17:17:35 -07002016 op = kOpXor;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002017 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002018 case OP_SHL_INT_LIT8:
Bill Buzbee0e605272009-12-01 14:28:05 -08002019 lit &= 31;
Bill Buzbee1465db52009-09-23 17:17:35 -07002020 shiftOp = true;
2021 op = kOpLsl;
2022 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002023 case OP_SHR_INT_LIT8:
Bill Buzbee0e605272009-12-01 14:28:05 -08002024 lit &= 31;
Bill Buzbee1465db52009-09-23 17:17:35 -07002025 shiftOp = true;
2026 op = kOpAsr;
2027 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002028 case OP_USHR_INT_LIT8:
Bill Buzbee0e605272009-12-01 14:28:05 -08002029 lit &= 31;
Bill Buzbee1465db52009-09-23 17:17:35 -07002030 shiftOp = true;
2031 op = kOpLsr;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002032 break;
2033
2034 case OP_DIV_INT_LIT8:
2035 case OP_DIV_INT_LIT16:
Ben Chengba4fc8b2009-06-01 13:00:29 -07002036 case OP_REM_INT_LIT8:
2037 case OP_REM_INT_LIT16:
2038 if (lit == 0) {
2039 /* Let the interpreter deal with div by 0 */
2040 genInterpSingleStep(cUnit, mir);
2041 return false;
2042 }
Elliott Hughesc7ad9b22010-04-28 13:52:02 -07002043 if (handleEasyDivide(cUnit, dalvikOpCode, rlSrc, rlDest, lit)) {
Elliott Hughes672511b2010-04-26 17:40:13 -07002044 return false;
2045 }
Bill Buzbeec6f10662010-02-09 11:16:15 -08002046 dvmCompilerFlushAllRegs(cUnit); /* Everything to home location */
Bill Buzbee1465db52009-09-23 17:17:35 -07002047 loadValueDirectFixed(cUnit, rlSrc, r0);
Bill Buzbeec6f10662010-02-09 11:16:15 -08002048 dvmCompilerClobber(cUnit, r0);
Bill Buzbee1465db52009-09-23 17:17:35 -07002049 if ((dalvikOpCode == OP_DIV_INT_LIT8) ||
2050 (dalvikOpCode == OP_DIV_INT_LIT16)) {
Ben Chengbd1326d2010-04-02 15:04:53 -07002051 LOAD_FUNC_ADDR(cUnit, r2, (int)__aeabi_idiv);
Bill Buzbee1465db52009-09-23 17:17:35 -07002052 isDiv = true;
2053 } else {
Ben Chengbd1326d2010-04-02 15:04:53 -07002054 LOAD_FUNC_ADDR(cUnit, r2, (int)__aeabi_idivmod);
Bill Buzbee1465db52009-09-23 17:17:35 -07002055 isDiv = false;
2056 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07002057 loadConstant(cUnit, r1, lit);
Bill Buzbee1465db52009-09-23 17:17:35 -07002058 opReg(cUnit, kOpBlx, r2);
Elliott Hughes6a555132010-02-25 15:41:42 -08002059 dvmCompilerClobberCallRegs(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07002060 if (isDiv)
Bill Buzbeec6f10662010-02-09 11:16:15 -08002061 rlResult = dvmCompilerGetReturn(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07002062 else
Bill Buzbeec6f10662010-02-09 11:16:15 -08002063 rlResult = dvmCompilerGetReturnAlt(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07002064 storeValue(cUnit, rlDest, rlResult);
2065 return false;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002066 break;
2067 default:
2068 return true;
2069 }
Bill Buzbee1465db52009-09-23 17:17:35 -07002070 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
Bill Buzbeec6f10662010-02-09 11:16:15 -08002071 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Bill Buzbee1465db52009-09-23 17:17:35 -07002072 // Avoid shifts by literal 0 - no support in Thumb. Change to copy
2073 if (shiftOp && (lit == 0)) {
2074 genRegCopy(cUnit, rlResult.lowReg, rlSrc.lowReg);
2075 } else {
2076 opRegRegImm(cUnit, op, rlResult.lowReg, rlSrc.lowReg, lit);
2077 }
2078 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002079 return false;
2080}
2081
2082static bool handleFmt22c(CompilationUnit *cUnit, MIR *mir)
2083{
2084 OpCode dalvikOpCode = mir->dalvikInsn.opCode;
2085 int fieldOffset;
2086
2087 if (dalvikOpCode >= OP_IGET && dalvikOpCode <= OP_IPUT_SHORT) {
2088 InstField *pInstField = (InstField *)
2089 cUnit->method->clazz->pDvmDex->pResFields[mir->dalvikInsn.vC];
Ben Chengba4fc8b2009-06-01 13:00:29 -07002090
Ben Chengdd6e8702010-05-07 13:05:47 -07002091 if (pInstField == NULL) {
2092 LOGE("Unexpected null instance field");
2093 dvmAbort();
2094 }
2095
Ben Chengba4fc8b2009-06-01 13:00:29 -07002096 fieldOffset = pInstField->byteOffset;
2097 } else {
Ben Chenga0e7b602009-10-13 23:09:01 -07002098 /* Deliberately break the code while make the compiler happy */
2099 fieldOffset = -1;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002100 }
2101 switch (dalvikOpCode) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07002102 case OP_NEW_ARRAY: {
Bill Buzbee1465db52009-09-23 17:17:35 -07002103 // Generates a call - use explicit registers
Bill Buzbeec6f10662010-02-09 11:16:15 -08002104 RegLocation rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
2105 RegLocation rlDest = dvmCompilerGetDest(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07002106 RegLocation rlResult;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002107 void *classPtr = (void*)
2108 (cUnit->method->clazz->pDvmDex->pResClasses[mir->dalvikInsn.vC]);
Ben Chengdd6e8702010-05-07 13:05:47 -07002109
2110 if (classPtr == NULL) {
2111 LOGE("Unexpected null class");
2112 dvmAbort();
2113 }
2114
Bill Buzbeec6f10662010-02-09 11:16:15 -08002115 dvmCompilerFlushAllRegs(cUnit); /* Everything to home location */
Bill Buzbee1465db52009-09-23 17:17:35 -07002116 genExportPC(cUnit, mir);
2117 loadValueDirectFixed(cUnit, rlSrc, r1); /* Len */
Ben Chengba4fc8b2009-06-01 13:00:29 -07002118 loadConstant(cUnit, r0, (int) classPtr );
Ben Chengbd1326d2010-04-02 15:04:53 -07002119 LOAD_FUNC_ADDR(cUnit, r3, (int)dvmAllocArrayByClass);
Ben Cheng4f489172009-09-27 17:08:35 -07002120 /*
2121 * "len < 0": bail to the interpreter to re-execute the
2122 * instruction
2123 */
Carl Shapiroe3c01da2010-05-20 22:54:18 -07002124 genRegImmCheck(cUnit, kArmCondMi, r1, 0, mir->offset, NULL);
Bill Buzbee270c1d62009-08-13 16:58:07 -07002125 loadConstant(cUnit, r2, ALLOC_DONT_TRACK);
Bill Buzbee1465db52009-09-23 17:17:35 -07002126 opReg(cUnit, kOpBlx, r3);
Elliott Hughes6a555132010-02-25 15:41:42 -08002127 dvmCompilerClobberCallRegs(cUnit);
Ben Cheng4f489172009-09-27 17:08:35 -07002128 /* generate a branch over if allocation is successful */
Bill Buzbee1465db52009-09-23 17:17:35 -07002129 opRegImm(cUnit, kOpCmp, r0, 0); /* NULL? */
2130 ArmLIR *branchOver = opCondBranch(cUnit, kArmCondNe);
Ben Cheng4f489172009-09-27 17:08:35 -07002131 /*
2132 * OOM exception needs to be thrown here and cannot re-execute
2133 */
2134 loadConstant(cUnit, r0,
2135 (int) (cUnit->method->insns + mir->offset));
2136 genDispatchToHandler(cUnit, TEMPLATE_THROW_EXCEPTION_COMMON);
2137 /* noreturn */
2138
Bill Buzbee1465db52009-09-23 17:17:35 -07002139 ArmLIR *target = newLIR0(cUnit, kArmPseudoTargetLabel);
Ben Cheng4f489172009-09-27 17:08:35 -07002140 target->defMask = ENCODE_ALL;
2141 branchOver->generic.target = (LIR *) target;
Bill Buzbeec6f10662010-02-09 11:16:15 -08002142 rlResult = dvmCompilerGetReturn(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07002143 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002144 break;
2145 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07002146 case OP_INSTANCE_OF: {
Bill Buzbee1465db52009-09-23 17:17:35 -07002147 // May generate a call - use explicit registers
Bill Buzbeec6f10662010-02-09 11:16:15 -08002148 RegLocation rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
2149 RegLocation rlDest = dvmCompilerGetDest(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07002150 RegLocation rlResult;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002151 ClassObject *classPtr =
2152 (cUnit->method->clazz->pDvmDex->pResClasses[mir->dalvikInsn.vC]);
Bill Buzbee480e6782010-01-27 15:43:08 -08002153 /*
2154 * Note: It is possible that classPtr is NULL at this point,
2155 * even though this instruction has been successfully interpreted.
2156 * If the previous interpretation had a null source, the
2157 * interpreter would not have bothered to resolve the clazz.
2158 * Bail out to the interpreter in this case, and log it
2159 * so that we can tell if it happens frequently.
2160 */
2161 if (classPtr == NULL) {
2162 LOGD("null clazz in OP_INSTANCE_OF, single-stepping");
2163 genInterpSingleStep(cUnit, mir);
2164 break;
2165 }
Bill Buzbeec6f10662010-02-09 11:16:15 -08002166 dvmCompilerFlushAllRegs(cUnit); /* Everything to home location */
Bill Buzbee1465db52009-09-23 17:17:35 -07002167 loadValueDirectFixed(cUnit, rlSrc, r0); /* Ref */
Ben Chengba4fc8b2009-06-01 13:00:29 -07002168 loadConstant(cUnit, r2, (int) classPtr );
Bill Buzbee270c1d62009-08-13 16:58:07 -07002169//TUNING: compare to 0 primative to allow use of CB[N]Z
Bill Buzbee1465db52009-09-23 17:17:35 -07002170 opRegImm(cUnit, kOpCmp, r0, 0); /* NULL? */
Ben Cheng752c7942009-06-22 10:50:07 -07002171 /* When taken r0 has NULL which can be used for store directly */
Bill Buzbee1465db52009-09-23 17:17:35 -07002172 ArmLIR *branch1 = opCondBranch(cUnit, kArmCondEq);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002173 /* r1 now contains object->clazz */
Bill Buzbee270c1d62009-08-13 16:58:07 -07002174 loadWordDisp(cUnit, r0, offsetof(Object, clazz), r1);
Bill Buzbee1465db52009-09-23 17:17:35 -07002175 /* r1 now contains object->clazz */
Ben Chengbd1326d2010-04-02 15:04:53 -07002176 LOAD_FUNC_ADDR(cUnit, r3, (int)dvmInstanceofNonTrivial);
Ben Cheng752c7942009-06-22 10:50:07 -07002177 loadConstant(cUnit, r0, 1); /* Assume true */
Bill Buzbee1465db52009-09-23 17:17:35 -07002178 opRegReg(cUnit, kOpCmp, r1, r2);
2179 ArmLIR *branch2 = opCondBranch(cUnit, kArmCondEq);
2180 genRegCopy(cUnit, r0, r1);
2181 genRegCopy(cUnit, r1, r2);
2182 opReg(cUnit, kOpBlx, r3);
Elliott Hughes6a555132010-02-25 15:41:42 -08002183 dvmCompilerClobberCallRegs(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002184 /* branch target here */
Bill Buzbee1465db52009-09-23 17:17:35 -07002185 ArmLIR *target = newLIR0(cUnit, kArmPseudoTargetLabel);
Ben Chengd7d426a2009-09-22 11:23:36 -07002186 target->defMask = ENCODE_ALL;
Bill Buzbeec6f10662010-02-09 11:16:15 -08002187 rlResult = dvmCompilerGetReturn(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07002188 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002189 branch1->generic.target = (LIR *)target;
2190 branch2->generic.target = (LIR *)target;
2191 break;
2192 }
2193 case OP_IGET_WIDE:
2194 genIGetWide(cUnit, mir, fieldOffset);
2195 break;
2196 case OP_IGET:
2197 case OP_IGET_OBJECT:
Bill Buzbee1465db52009-09-23 17:17:35 -07002198 genIGet(cUnit, mir, kWord, fieldOffset);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002199 break;
2200 case OP_IGET_BOOLEAN:
Bill Buzbee1465db52009-09-23 17:17:35 -07002201 genIGet(cUnit, mir, kUnsignedByte, fieldOffset);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002202 break;
2203 case OP_IGET_BYTE:
Bill Buzbee1465db52009-09-23 17:17:35 -07002204 genIGet(cUnit, mir, kSignedByte, fieldOffset);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002205 break;
2206 case OP_IGET_CHAR:
Bill Buzbee1465db52009-09-23 17:17:35 -07002207 genIGet(cUnit, mir, kUnsignedHalf, fieldOffset);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002208 break;
2209 case OP_IGET_SHORT:
Bill Buzbee1465db52009-09-23 17:17:35 -07002210 genIGet(cUnit, mir, kSignedHalf, fieldOffset);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002211 break;
2212 case OP_IPUT_WIDE:
2213 genIPutWide(cUnit, mir, fieldOffset);
2214 break;
2215 case OP_IPUT:
2216 case OP_IPUT_OBJECT:
Bill Buzbee1465db52009-09-23 17:17:35 -07002217 genIPut(cUnit, mir, kWord, fieldOffset);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002218 break;
2219 case OP_IPUT_SHORT:
2220 case OP_IPUT_CHAR:
Bill Buzbee1465db52009-09-23 17:17:35 -07002221 genIPut(cUnit, mir, kUnsignedHalf, fieldOffset);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002222 break;
2223 case OP_IPUT_BYTE:
2224 case OP_IPUT_BOOLEAN:
Bill Buzbee1465db52009-09-23 17:17:35 -07002225 genIPut(cUnit, mir, kUnsignedByte, fieldOffset);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002226 break;
Bill Buzbeeb16344a2010-03-15 17:19:12 -07002227 case OP_IGET_WIDE_VOLATILE:
2228 case OP_IPUT_WIDE_VOLATILE:
2229 case OP_SGET_WIDE_VOLATILE:
2230 case OP_SPUT_WIDE_VOLATILE:
2231 genInterpSingleStep(cUnit, mir);
2232 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002233 default:
2234 return true;
2235 }
2236 return false;
2237}
2238
2239static bool handleFmt22cs(CompilationUnit *cUnit, MIR *mir)
2240{
2241 OpCode dalvikOpCode = mir->dalvikInsn.opCode;
2242 int fieldOffset = mir->dalvikInsn.vC;
2243 switch (dalvikOpCode) {
2244 case OP_IGET_QUICK:
2245 case OP_IGET_OBJECT_QUICK:
Bill Buzbee1465db52009-09-23 17:17:35 -07002246 genIGet(cUnit, mir, kWord, fieldOffset);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002247 break;
2248 case OP_IPUT_QUICK:
2249 case OP_IPUT_OBJECT_QUICK:
Bill Buzbee1465db52009-09-23 17:17:35 -07002250 genIPut(cUnit, mir, kWord, fieldOffset);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002251 break;
2252 case OP_IGET_WIDE_QUICK:
2253 genIGetWide(cUnit, mir, fieldOffset);
2254 break;
2255 case OP_IPUT_WIDE_QUICK:
2256 genIPutWide(cUnit, mir, fieldOffset);
2257 break;
2258 default:
2259 return true;
2260 }
2261 return false;
2262
2263}
2264
2265/* Compare agaist zero */
2266static bool handleFmt22t(CompilationUnit *cUnit, MIR *mir, BasicBlock *bb,
Bill Buzbee89efc3d2009-07-28 11:22:22 -07002267 ArmLIR *labelList)
Ben Chengba4fc8b2009-06-01 13:00:29 -07002268{
2269 OpCode dalvikOpCode = mir->dalvikInsn.opCode;
Bill Buzbee89efc3d2009-07-28 11:22:22 -07002270 ArmConditionCode cond;
Bill Buzbeec6f10662010-02-09 11:16:15 -08002271 RegLocation rlSrc1 = dvmCompilerGetSrc(cUnit, mir, 0);
2272 RegLocation rlSrc2 = dvmCompilerGetSrc(cUnit, mir, 1);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002273
Bill Buzbee1465db52009-09-23 17:17:35 -07002274 rlSrc1 = loadValue(cUnit, rlSrc1, kCoreReg);
2275 rlSrc2 = loadValue(cUnit, rlSrc2, kCoreReg);
2276 opRegReg(cUnit, kOpCmp, rlSrc1.lowReg, rlSrc2.lowReg);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002277
2278 switch (dalvikOpCode) {
2279 case OP_IF_EQ:
Bill Buzbee1465db52009-09-23 17:17:35 -07002280 cond = kArmCondEq;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002281 break;
2282 case OP_IF_NE:
Bill Buzbee1465db52009-09-23 17:17:35 -07002283 cond = kArmCondNe;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002284 break;
2285 case OP_IF_LT:
Bill Buzbee1465db52009-09-23 17:17:35 -07002286 cond = kArmCondLt;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002287 break;
2288 case OP_IF_GE:
Bill Buzbee1465db52009-09-23 17:17:35 -07002289 cond = kArmCondGe;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002290 break;
2291 case OP_IF_GT:
Bill Buzbee1465db52009-09-23 17:17:35 -07002292 cond = kArmCondGt;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002293 break;
2294 case OP_IF_LE:
Bill Buzbee1465db52009-09-23 17:17:35 -07002295 cond = kArmCondLe;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002296 break;
2297 default:
2298 cond = 0;
2299 LOGE("Unexpected opcode (%d) for Fmt22t\n", dalvikOpCode);
Bill Buzbeefc519dc2010-03-06 23:30:57 -08002300 dvmCompilerAbort(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002301 }
2302 genConditionalBranch(cUnit, cond, &labelList[bb->taken->id]);
2303 /* This mostly likely will be optimized away in a later phase */
2304 genUnconditionalBranch(cUnit, &labelList[bb->fallThrough->id]);
2305 return false;
2306}
2307
2308static bool handleFmt22x_Fmt32x(CompilationUnit *cUnit, MIR *mir)
2309{
2310 OpCode opCode = mir->dalvikInsn.opCode;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002311
2312 switch (opCode) {
2313 case OP_MOVE_16:
2314 case OP_MOVE_OBJECT_16:
2315 case OP_MOVE_FROM16:
Ben Chenge9695e52009-06-16 16:11:47 -07002316 case OP_MOVE_OBJECT_FROM16: {
Bill Buzbeec6f10662010-02-09 11:16:15 -08002317 storeValue(cUnit, dvmCompilerGetDest(cUnit, mir, 0),
2318 dvmCompilerGetSrc(cUnit, mir, 0));
Ben Chengba4fc8b2009-06-01 13:00:29 -07002319 break;
Ben Chenge9695e52009-06-16 16:11:47 -07002320 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07002321 case OP_MOVE_WIDE_16:
Ben Chenge9695e52009-06-16 16:11:47 -07002322 case OP_MOVE_WIDE_FROM16: {
Bill Buzbeec6f10662010-02-09 11:16:15 -08002323 storeValueWide(cUnit, dvmCompilerGetDestWide(cUnit, mir, 0, 1),
2324 dvmCompilerGetSrcWide(cUnit, mir, 0, 1));
Ben Chengba4fc8b2009-06-01 13:00:29 -07002325 break;
Ben Chenge9695e52009-06-16 16:11:47 -07002326 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07002327 default:
2328 return true;
2329 }
2330 return false;
2331}
2332
2333static bool handleFmt23x(CompilationUnit *cUnit, MIR *mir)
2334{
2335 OpCode opCode = mir->dalvikInsn.opCode;
Bill Buzbee1465db52009-09-23 17:17:35 -07002336 RegLocation rlSrc1;
2337 RegLocation rlSrc2;
2338 RegLocation rlDest;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002339
2340 if ( (opCode >= OP_ADD_INT) && (opCode <= OP_REM_DOUBLE)) {
Ben Cheng5d90c202009-11-22 23:31:11 -08002341 return genArithOp( cUnit, mir );
Ben Chengba4fc8b2009-06-01 13:00:29 -07002342 }
2343
Bill Buzbee1465db52009-09-23 17:17:35 -07002344 /* APUTs have 3 sources and no targets */
2345 if (mir->ssaRep->numDefs == 0) {
2346 if (mir->ssaRep->numUses == 3) {
Bill Buzbeec6f10662010-02-09 11:16:15 -08002347 rlDest = dvmCompilerGetSrc(cUnit, mir, 0);
2348 rlSrc1 = dvmCompilerGetSrc(cUnit, mir, 1);
2349 rlSrc2 = dvmCompilerGetSrc(cUnit, mir, 2);
Bill Buzbee1465db52009-09-23 17:17:35 -07002350 } else {
2351 assert(mir->ssaRep->numUses == 4);
Bill Buzbeec6f10662010-02-09 11:16:15 -08002352 rlDest = dvmCompilerGetSrcWide(cUnit, mir, 0, 1);
2353 rlSrc1 = dvmCompilerGetSrc(cUnit, mir, 2);
2354 rlSrc2 = dvmCompilerGetSrc(cUnit, mir, 3);
Bill Buzbee1465db52009-09-23 17:17:35 -07002355 }
2356 } else {
2357 /* Two sources and 1 dest. Deduce the operand sizes */
2358 if (mir->ssaRep->numUses == 4) {
Bill Buzbeec6f10662010-02-09 11:16:15 -08002359 rlSrc1 = dvmCompilerGetSrcWide(cUnit, mir, 0, 1);
2360 rlSrc2 = dvmCompilerGetSrcWide(cUnit, mir, 2, 3);
Bill Buzbee1465db52009-09-23 17:17:35 -07002361 } else {
2362 assert(mir->ssaRep->numUses == 2);
Bill Buzbeec6f10662010-02-09 11:16:15 -08002363 rlSrc1 = dvmCompilerGetSrc(cUnit, mir, 0);
2364 rlSrc2 = dvmCompilerGetSrc(cUnit, mir, 1);
Bill Buzbee1465db52009-09-23 17:17:35 -07002365 }
2366 if (mir->ssaRep->numDefs == 2) {
Bill Buzbeec6f10662010-02-09 11:16:15 -08002367 rlDest = dvmCompilerGetDestWide(cUnit, mir, 0, 1);
Bill Buzbee1465db52009-09-23 17:17:35 -07002368 } else {
2369 assert(mir->ssaRep->numDefs == 1);
Bill Buzbeec6f10662010-02-09 11:16:15 -08002370 rlDest = dvmCompilerGetDest(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07002371 }
2372 }
2373
2374
Ben Chengba4fc8b2009-06-01 13:00:29 -07002375 switch (opCode) {
Bill Buzbeed45ba372009-06-15 17:00:57 -07002376 case OP_CMPL_FLOAT:
2377 case OP_CMPG_FLOAT:
2378 case OP_CMPL_DOUBLE:
2379 case OP_CMPG_DOUBLE:
Ben Cheng5d90c202009-11-22 23:31:11 -08002380 return genCmpFP(cUnit, mir, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002381 case OP_CMP_LONG:
Bill Buzbee1465db52009-09-23 17:17:35 -07002382 genCmpLong(cUnit, mir, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002383 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002384 case OP_AGET_WIDE:
Bill Buzbee1465db52009-09-23 17:17:35 -07002385 genArrayGet(cUnit, mir, kLong, rlSrc1, rlSrc2, rlDest, 3);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002386 break;
2387 case OP_AGET:
2388 case OP_AGET_OBJECT:
Bill Buzbee1465db52009-09-23 17:17:35 -07002389 genArrayGet(cUnit, mir, kWord, rlSrc1, rlSrc2, rlDest, 2);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002390 break;
2391 case OP_AGET_BOOLEAN:
Bill Buzbee1465db52009-09-23 17:17:35 -07002392 genArrayGet(cUnit, mir, kUnsignedByte, rlSrc1, rlSrc2, rlDest, 0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002393 break;
2394 case OP_AGET_BYTE:
Bill Buzbee1465db52009-09-23 17:17:35 -07002395 genArrayGet(cUnit, mir, kSignedByte, rlSrc1, rlSrc2, rlDest, 0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002396 break;
2397 case OP_AGET_CHAR:
Bill Buzbee1465db52009-09-23 17:17:35 -07002398 genArrayGet(cUnit, mir, kUnsignedHalf, rlSrc1, rlSrc2, rlDest, 1);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002399 break;
2400 case OP_AGET_SHORT:
Bill Buzbee1465db52009-09-23 17:17:35 -07002401 genArrayGet(cUnit, mir, kSignedHalf, rlSrc1, rlSrc2, rlDest, 1);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002402 break;
2403 case OP_APUT_WIDE:
Bill Buzbee1465db52009-09-23 17:17:35 -07002404 genArrayPut(cUnit, mir, kLong, rlSrc1, rlSrc2, rlDest, 3);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002405 break;
2406 case OP_APUT:
Bill Buzbee1465db52009-09-23 17:17:35 -07002407 genArrayPut(cUnit, mir, kWord, rlSrc1, rlSrc2, rlDest, 2);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002408 break;
Bill Buzbeebe6534f2010-03-12 16:01:35 -08002409 case OP_APUT_OBJECT:
2410 genArrayObjectPut(cUnit, mir, rlSrc1, rlSrc2, rlDest, 2);
2411 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002412 case OP_APUT_SHORT:
2413 case OP_APUT_CHAR:
Bill Buzbee1465db52009-09-23 17:17:35 -07002414 genArrayPut(cUnit, mir, kUnsignedHalf, rlSrc1, rlSrc2, rlDest, 1);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002415 break;
2416 case OP_APUT_BYTE:
2417 case OP_APUT_BOOLEAN:
Bill Buzbee1465db52009-09-23 17:17:35 -07002418 genArrayPut(cUnit, mir, kUnsignedByte, rlSrc1, rlSrc2, rlDest, 0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002419 break;
2420 default:
2421 return true;
2422 }
2423 return false;
2424}
2425
Ben Cheng6c10a972009-10-29 14:39:18 -07002426/*
2427 * Find the matching case.
2428 *
2429 * return values:
2430 * r0 (low 32-bit): pc of the chaining cell corresponding to the resolved case,
2431 * including default which is placed at MIN(size, MAX_CHAINED_SWITCH_CASES).
2432 * r1 (high 32-bit): the branch offset of the matching case (only for indexes
2433 * above MAX_CHAINED_SWITCH_CASES).
2434 *
2435 * Instructions around the call are:
2436 *
2437 * mov r2, pc
2438 * blx &findPackedSwitchIndex
2439 * mov pc, r0
2440 * .align4
Bill Buzbeebd047242010-05-13 13:02:53 -07002441 * chaining cell for case 0 [12 bytes]
2442 * chaining cell for case 1 [12 bytes]
Ben Cheng6c10a972009-10-29 14:39:18 -07002443 * :
Bill Buzbeebd047242010-05-13 13:02:53 -07002444 * chaining cell for case MIN(size, MAX_CHAINED_SWITCH_CASES)-1 [12 bytes]
Ben Cheng6c10a972009-10-29 14:39:18 -07002445 * chaining cell for case default [8 bytes]
2446 * noChain exit
2447 */
Ben Chengbd1326d2010-04-02 15:04:53 -07002448static s8 findPackedSwitchIndex(const u2* switchData, int testVal, int pc)
Ben Cheng6c10a972009-10-29 14:39:18 -07002449{
2450 int size;
2451 int firstKey;
2452 const int *entries;
2453 int index;
2454 int jumpIndex;
2455 int caseDPCOffset = 0;
2456 /* In Thumb mode pc is 4 ahead of the "mov r2, pc" instruction */
2457 int chainingPC = (pc + 4) & ~3;
2458
2459 /*
2460 * Packed switch data format:
2461 * ushort ident = 0x0100 magic value
2462 * ushort size number of entries in the table
2463 * int first_key first (and lowest) switch case value
2464 * int targets[size] branch targets, relative to switch opcode
2465 *
2466 * Total size is (4+size*2) 16-bit code units.
2467 */
2468 size = switchData[1];
2469 assert(size > 0);
2470
2471 firstKey = switchData[2];
2472 firstKey |= switchData[3] << 16;
2473
2474
2475 /* The entries are guaranteed to be aligned on a 32-bit boundary;
2476 * we can treat them as a native int array.
2477 */
2478 entries = (const int*) &switchData[4];
2479 assert(((u4)entries & 0x3) == 0);
2480
2481 index = testVal - firstKey;
2482
2483 /* Jump to the default cell */
2484 if (index < 0 || index >= size) {
2485 jumpIndex = MIN(size, MAX_CHAINED_SWITCH_CASES);
2486 /* Jump to the non-chaining exit point */
2487 } else if (index >= MAX_CHAINED_SWITCH_CASES) {
2488 jumpIndex = MAX_CHAINED_SWITCH_CASES + 1;
2489 caseDPCOffset = entries[index];
2490 /* Jump to the inline chaining cell */
2491 } else {
2492 jumpIndex = index;
2493 }
2494
Bill Buzbeebd047242010-05-13 13:02:53 -07002495 chainingPC += jumpIndex * CHAIN_CELL_NORMAL_SIZE;
Ben Cheng6c10a972009-10-29 14:39:18 -07002496 return (((s8) caseDPCOffset) << 32) | (u8) chainingPC;
2497}
2498
2499/* See comments for findPackedSwitchIndex */
Ben Chengbd1326d2010-04-02 15:04:53 -07002500static s8 findSparseSwitchIndex(const u2* switchData, int testVal, int pc)
Ben Cheng6c10a972009-10-29 14:39:18 -07002501{
2502 int size;
2503 const int *keys;
2504 const int *entries;
2505 int chainingPC = (pc + 4) & ~3;
2506 int i;
2507
2508 /*
2509 * Sparse switch data format:
2510 * ushort ident = 0x0200 magic value
2511 * ushort size number of entries in the table; > 0
2512 * int keys[size] keys, sorted low-to-high; 32-bit aligned
2513 * int targets[size] branch targets, relative to switch opcode
2514 *
2515 * Total size is (2+size*4) 16-bit code units.
2516 */
2517
2518 size = switchData[1];
2519 assert(size > 0);
2520
2521 /* The keys are guaranteed to be aligned on a 32-bit boundary;
2522 * we can treat them as a native int array.
2523 */
2524 keys = (const int*) &switchData[2];
2525 assert(((u4)keys & 0x3) == 0);
2526
2527 /* The entries are guaranteed to be aligned on a 32-bit boundary;
2528 * we can treat them as a native int array.
2529 */
2530 entries = keys + size;
2531 assert(((u4)entries & 0x3) == 0);
2532
2533 /*
2534 * Run through the list of keys, which are guaranteed to
2535 * be sorted low-to-high.
2536 *
2537 * Most tables have 3-4 entries. Few have more than 10. A binary
2538 * search here is probably not useful.
2539 */
2540 for (i = 0; i < size; i++) {
2541 int k = keys[i];
2542 if (k == testVal) {
2543 /* MAX_CHAINED_SWITCH_CASES + 1 is the start of the overflow case */
2544 int jumpIndex = (i < MAX_CHAINED_SWITCH_CASES) ?
2545 i : MAX_CHAINED_SWITCH_CASES + 1;
Bill Buzbeebd047242010-05-13 13:02:53 -07002546 chainingPC += jumpIndex * CHAIN_CELL_NORMAL_SIZE;
Ben Cheng6c10a972009-10-29 14:39:18 -07002547 return (((s8) entries[i]) << 32) | (u8) chainingPC;
2548 } else if (k > testVal) {
2549 break;
2550 }
2551 }
Bill Buzbeebd047242010-05-13 13:02:53 -07002552 return chainingPC + MIN(size, MAX_CHAINED_SWITCH_CASES) *
2553 CHAIN_CELL_NORMAL_SIZE;
Ben Cheng6c10a972009-10-29 14:39:18 -07002554}
2555
Ben Chengba4fc8b2009-06-01 13:00:29 -07002556static bool handleFmt31t(CompilationUnit *cUnit, MIR *mir)
2557{
2558 OpCode dalvikOpCode = mir->dalvikInsn.opCode;
2559 switch (dalvikOpCode) {
2560 case OP_FILL_ARRAY_DATA: {
Bill Buzbeec6f10662010-02-09 11:16:15 -08002561 RegLocation rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07002562 // Making a call - use explicit registers
Bill Buzbeec6f10662010-02-09 11:16:15 -08002563 dvmCompilerFlushAllRegs(cUnit); /* Everything to home location */
Bill Buzbee1465db52009-09-23 17:17:35 -07002564 genExportPC(cUnit, mir);
2565 loadValueDirectFixed(cUnit, rlSrc, r0);
Ben Chengbd1326d2010-04-02 15:04:53 -07002566 LOAD_FUNC_ADDR(cUnit, r2, (int)dvmInterpHandleFillArrayData);
Ben Cheng6c10a972009-10-29 14:39:18 -07002567 loadConstant(cUnit, r1,
2568 (int) (cUnit->method->insns + mir->offset + mir->dalvikInsn.vB));
Bill Buzbee1465db52009-09-23 17:17:35 -07002569 opReg(cUnit, kOpBlx, r2);
Elliott Hughes6a555132010-02-25 15:41:42 -08002570 dvmCompilerClobberCallRegs(cUnit);
Bill Buzbeefd023aa2009-11-02 09:23:49 -08002571 /* generate a branch over if successful */
2572 opRegImm(cUnit, kOpCmp, r0, 0); /* NULL? */
2573 ArmLIR *branchOver = opCondBranch(cUnit, kArmCondNe);
2574 loadConstant(cUnit, r0,
2575 (int) (cUnit->method->insns + mir->offset));
2576 genDispatchToHandler(cUnit, TEMPLATE_THROW_EXCEPTION_COMMON);
2577 ArmLIR *target = newLIR0(cUnit, kArmPseudoTargetLabel);
2578 target->defMask = ENCODE_ALL;
2579 branchOver->generic.target = (LIR *) target;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002580 break;
2581 }
2582 /*
Ben Cheng6c10a972009-10-29 14:39:18 -07002583 * Compute the goto target of up to
2584 * MIN(switchSize, MAX_CHAINED_SWITCH_CASES) + 1 chaining cells.
2585 * See the comment before findPackedSwitchIndex for the code layout.
Ben Chengba4fc8b2009-06-01 13:00:29 -07002586 */
2587 case OP_PACKED_SWITCH:
2588 case OP_SPARSE_SWITCH: {
Bill Buzbeec6f10662010-02-09 11:16:15 -08002589 RegLocation rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
2590 dvmCompilerFlushAllRegs(cUnit); /* Everything to home location */
Bill Buzbee1465db52009-09-23 17:17:35 -07002591 loadValueDirectFixed(cUnit, rlSrc, r1);
Bill Buzbeec6f10662010-02-09 11:16:15 -08002592 dvmCompilerLockAllTemps(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002593 if (dalvikOpCode == OP_PACKED_SWITCH) {
Ben Chengbd1326d2010-04-02 15:04:53 -07002594 LOAD_FUNC_ADDR(cUnit, r4PC, (int)findPackedSwitchIndex);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002595 } else {
Ben Chengbd1326d2010-04-02 15:04:53 -07002596 LOAD_FUNC_ADDR(cUnit, r4PC, (int)findSparseSwitchIndex);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002597 }
Ben Cheng6c10a972009-10-29 14:39:18 -07002598 /* r0 <- Addr of the switch data */
2599 loadConstant(cUnit, r0,
2600 (int) (cUnit->method->insns + mir->offset + mir->dalvikInsn.vB));
2601 /* r2 <- pc of the instruction following the blx */
2602 opRegReg(cUnit, kOpMov, r2, rpc);
Bill Buzbee1465db52009-09-23 17:17:35 -07002603 opReg(cUnit, kOpBlx, r4PC);
Elliott Hughes6a555132010-02-25 15:41:42 -08002604 dvmCompilerClobberCallRegs(cUnit);
Ben Cheng6c10a972009-10-29 14:39:18 -07002605 /* pc <- computed goto target */
2606 opRegReg(cUnit, kOpMov, rpc, r0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002607 break;
2608 }
2609 default:
2610 return true;
2611 }
2612 return false;
2613}
2614
2615static bool handleFmt35c_3rc(CompilationUnit *cUnit, MIR *mir, BasicBlock *bb,
Bill Buzbee89efc3d2009-07-28 11:22:22 -07002616 ArmLIR *labelList)
Ben Chengba4fc8b2009-06-01 13:00:29 -07002617{
Bill Buzbee9bc3df32009-07-30 10:52:29 -07002618 ArmLIR *retChainingCell = NULL;
Bill Buzbee89efc3d2009-07-28 11:22:22 -07002619 ArmLIR *pcrLabel = NULL;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002620
Bill Buzbeef4ce16f2009-07-28 13:28:25 -07002621 if (bb->fallThrough != NULL)
2622 retChainingCell = &labelList[bb->fallThrough->id];
2623
Ben Chengba4fc8b2009-06-01 13:00:29 -07002624 DecodedInstruction *dInsn = &mir->dalvikInsn;
2625 switch (mir->dalvikInsn.opCode) {
2626 /*
2627 * calleeMethod = this->clazz->vtable[
2628 * method->clazz->pDvmDex->pResMethods[BBBB]->methodIndex
2629 * ]
2630 */
2631 case OP_INVOKE_VIRTUAL:
2632 case OP_INVOKE_VIRTUAL_RANGE: {
Bill Buzbee89efc3d2009-07-28 11:22:22 -07002633 ArmLIR *predChainingCell = &labelList[bb->taken->id];
Ben Chengba4fc8b2009-06-01 13:00:29 -07002634 int methodIndex =
2635 cUnit->method->clazz->pDvmDex->pResMethods[dInsn->vB]->
2636 methodIndex;
2637
2638 if (mir->dalvikInsn.opCode == OP_INVOKE_VIRTUAL)
2639 genProcessArgsNoRange(cUnit, mir, dInsn, &pcrLabel);
2640 else
2641 genProcessArgsRange(cUnit, mir, dInsn, &pcrLabel);
2642
Ben Cheng38329f52009-07-07 14:19:20 -07002643 genInvokeVirtualCommon(cUnit, mir, methodIndex,
2644 retChainingCell,
2645 predChainingCell,
2646 pcrLabel);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002647 break;
2648 }
2649 /*
2650 * calleeMethod = method->clazz->super->vtable[method->clazz->pDvmDex
2651 * ->pResMethods[BBBB]->methodIndex]
2652 */
Ben Chengba4fc8b2009-06-01 13:00:29 -07002653 case OP_INVOKE_SUPER:
2654 case OP_INVOKE_SUPER_RANGE: {
2655 int mIndex = cUnit->method->clazz->pDvmDex->
2656 pResMethods[dInsn->vB]->methodIndex;
2657 const Method *calleeMethod =
2658 cUnit->method->clazz->super->vtable[mIndex];
2659
2660 if (mir->dalvikInsn.opCode == OP_INVOKE_SUPER)
2661 genProcessArgsNoRange(cUnit, mir, dInsn, &pcrLabel);
2662 else
2663 genProcessArgsRange(cUnit, mir, dInsn, &pcrLabel);
2664
2665 /* r0 = calleeMethod */
2666 loadConstant(cUnit, r0, (int) calleeMethod);
2667
Ben Cheng38329f52009-07-07 14:19:20 -07002668 genInvokeSingletonCommon(cUnit, mir, bb, labelList, pcrLabel,
2669 calleeMethod);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002670 break;
2671 }
2672 /* calleeMethod = method->clazz->pDvmDex->pResMethods[BBBB] */
2673 case OP_INVOKE_DIRECT:
2674 case OP_INVOKE_DIRECT_RANGE: {
2675 const Method *calleeMethod =
2676 cUnit->method->clazz->pDvmDex->pResMethods[dInsn->vB];
2677
2678 if (mir->dalvikInsn.opCode == OP_INVOKE_DIRECT)
2679 genProcessArgsNoRange(cUnit, mir, dInsn, &pcrLabel);
2680 else
2681 genProcessArgsRange(cUnit, mir, dInsn, &pcrLabel);
2682
2683 /* r0 = calleeMethod */
2684 loadConstant(cUnit, r0, (int) calleeMethod);
2685
Ben Cheng38329f52009-07-07 14:19:20 -07002686 genInvokeSingletonCommon(cUnit, mir, bb, labelList, pcrLabel,
2687 calleeMethod);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002688 break;
2689 }
2690 /* calleeMethod = method->clazz->pDvmDex->pResMethods[BBBB] */
2691 case OP_INVOKE_STATIC:
2692 case OP_INVOKE_STATIC_RANGE: {
2693 const Method *calleeMethod =
2694 cUnit->method->clazz->pDvmDex->pResMethods[dInsn->vB];
2695
2696 if (mir->dalvikInsn.opCode == OP_INVOKE_STATIC)
2697 genProcessArgsNoRange(cUnit, mir, dInsn,
2698 NULL /* no null check */);
2699 else
2700 genProcessArgsRange(cUnit, mir, dInsn,
2701 NULL /* no null check */);
2702
2703 /* r0 = calleeMethod */
2704 loadConstant(cUnit, r0, (int) calleeMethod);
2705
Ben Cheng38329f52009-07-07 14:19:20 -07002706 genInvokeSingletonCommon(cUnit, mir, bb, labelList, pcrLabel,
2707 calleeMethod);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002708 break;
2709 }
Ben Cheng09e50c92010-05-02 10:45:32 -07002710 /*
Ben Chengba4fc8b2009-06-01 13:00:29 -07002711 * calleeMethod = dvmFindInterfaceMethodInCache(this->clazz,
2712 * BBBB, method, method->clazz->pDvmDex)
Ben Cheng38329f52009-07-07 14:19:20 -07002713 *
Ben Cheng09e50c92010-05-02 10:45:32 -07002714 * The following is an example of generated code for
2715 * "invoke-interface v0"
Ben Cheng38329f52009-07-07 14:19:20 -07002716 *
Ben Cheng09e50c92010-05-02 10:45:32 -07002717 * -------- dalvik offset: 0x0008 @ invoke-interface v0
2718 * 0x47357e36 : ldr r0, [r5, #0] --+
2719 * 0x47357e38 : sub r7,r5,#24 |
2720 * 0x47357e3c : cmp r0, #0 | genProcessArgsNoRange
2721 * 0x47357e3e : beq 0x47357e82 |
2722 * 0x47357e40 : stmia r7, <r0> --+
2723 * 0x47357e42 : ldr r4, [pc, #120] --> r4 <- dalvikPC of this invoke
2724 * 0x47357e44 : add r1, pc, #64 --> r1 <- &retChainingCell
2725 * 0x47357e46 : add r2, pc, #72 --> r2 <- &predictedChainingCell
2726 * 0x47357e48 : blx_1 0x47348190 --+ TEMPLATE_INVOKE_METHOD_
2727 * 0x47357e4a : blx_2 see above --+ PREDICTED_CHAIN
2728 * 0x47357e4c : b 0x47357e90 --> off to the predicted chain
2729 * 0x47357e4e : b 0x47357e82 --> punt to the interpreter
2730 * 0x47357e50 : mov r8, r1 --+
2731 * 0x47357e52 : mov r9, r2 |
2732 * 0x47357e54 : ldr r2, [pc, #96] |
2733 * 0x47357e56 : mov r10, r3 |
2734 * 0x47357e58 : movs r0, r3 | dvmFindInterfaceMethodInCache
2735 * 0x47357e5a : ldr r3, [pc, #88] |
2736 * 0x47357e5c : ldr r7, [pc, #80] |
2737 * 0x47357e5e : mov r1, #1452 |
2738 * 0x47357e62 : blx r7 --+
2739 * 0x47357e64 : cmp r0, #0 --> calleeMethod == NULL?
2740 * 0x47357e66 : bne 0x47357e6e --> branch over the throw if !r0
2741 * 0x47357e68 : ldr r0, [pc, #80] --> load Dalvik PC of the invoke
2742 * 0x47357e6a : blx_1 0x47348494 --+ TEMPLATE_THROW_EXCEPTION_
2743 * 0x47357e6c : blx_2 see above --+ COMMON
2744 * 0x47357e6e : mov r1, r8 --> r1 <- &retChainingCell
2745 * 0x47357e70 : cmp r1, #0 --> compare against 0
2746 * 0x47357e72 : bgt 0x47357e7c --> >=0? don't rechain
2747 * 0x47357e74 : ldr r7, [r6, #108] --+
2748 * 0x47357e76 : mov r2, r9 | dvmJitToPatchPredictedChain
2749 * 0x47357e78 : mov r3, r10 |
2750 * 0x47357e7a : blx r7 --+
2751 * 0x47357e7c : add r1, pc, #8 --> r1 <- &retChainingCell
2752 * 0x47357e7e : blx_1 0x4734809c --+ TEMPLATE_INVOKE_METHOD_NO_OPT
2753 * 0x47357e80 : blx_2 see above --+
2754 * -------- reconstruct dalvik PC : 0x425719dc @ +0x0008
2755 * 0x47357e82 : ldr r0, [pc, #56]
Ben Cheng38329f52009-07-07 14:19:20 -07002756 * Exception_Handling:
Ben Cheng09e50c92010-05-02 10:45:32 -07002757 * 0x47357e84 : ldr r1, [r6, #92]
2758 * 0x47357e86 : blx r1
2759 * 0x47357e88 : .align4
2760 * -------- chaining cell (hot): 0x000b
2761 * 0x47357e88 : ldr r0, [r6, #104]
2762 * 0x47357e8a : blx r0
2763 * 0x47357e8c : data 0x19e2(6626)
2764 * 0x47357e8e : data 0x4257(16983)
2765 * 0x47357e90 : .align4
Ben Cheng38329f52009-07-07 14:19:20 -07002766 * -------- chaining cell (predicted)
Ben Cheng09e50c92010-05-02 10:45:32 -07002767 * 0x47357e90 : data 0xe7fe(59390) --> will be patched into bx
2768 * 0x47357e92 : data 0x0000(0)
2769 * 0x47357e94 : data 0x0000(0) --> class
2770 * 0x47357e96 : data 0x0000(0)
2771 * 0x47357e98 : data 0x0000(0) --> method
2772 * 0x47357e9a : data 0x0000(0)
2773 * 0x47357e9c : data 0x0000(0) --> rechain count
2774 * 0x47357e9e : data 0x0000(0)
2775 * -------- end of chaining cells (0x006c)
2776 * 0x47357eb0 : .word (0xad03e369)
2777 * 0x47357eb4 : .word (0x28a90)
2778 * 0x47357eb8 : .word (0x41a63394)
2779 * 0x47357ebc : .word (0x425719dc)
Ben Chengba4fc8b2009-06-01 13:00:29 -07002780 */
2781 case OP_INVOKE_INTERFACE:
2782 case OP_INVOKE_INTERFACE_RANGE: {
Bill Buzbee89efc3d2009-07-28 11:22:22 -07002783 ArmLIR *predChainingCell = &labelList[bb->taken->id];
Ben Chengba4fc8b2009-06-01 13:00:29 -07002784
Bill Buzbee1465db52009-09-23 17:17:35 -07002785 /* Ensure that nothing is both live and dirty */
Bill Buzbeec6f10662010-02-09 11:16:15 -08002786 dvmCompilerFlushAllRegs(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07002787
Ben Chengba4fc8b2009-06-01 13:00:29 -07002788 if (mir->dalvikInsn.opCode == OP_INVOKE_INTERFACE)
2789 genProcessArgsNoRange(cUnit, mir, dInsn, &pcrLabel);
2790 else
2791 genProcessArgsRange(cUnit, mir, dInsn, &pcrLabel);
2792
Ben Cheng38329f52009-07-07 14:19:20 -07002793 /* "this" is already left in r0 by genProcessArgs* */
2794
2795 /* r4PC = dalvikCallsite */
2796 loadConstant(cUnit, r4PC,
2797 (int) (cUnit->method->insns + mir->offset));
2798
2799 /* r1 = &retChainingCell */
Bill Buzbee270c1d62009-08-13 16:58:07 -07002800 ArmLIR *addrRetChain =
Bill Buzbee1465db52009-09-23 17:17:35 -07002801 opRegRegImm(cUnit, kOpAdd, r1, rpc, 0);
Ben Cheng38329f52009-07-07 14:19:20 -07002802 addrRetChain->generic.target = (LIR *) retChainingCell;
2803
2804 /* r2 = &predictedChainingCell */
Bill Buzbee89efc3d2009-07-28 11:22:22 -07002805 ArmLIR *predictedChainingCell =
Bill Buzbee1465db52009-09-23 17:17:35 -07002806 opRegRegImm(cUnit, kOpAdd, r2, rpc, 0);
Ben Cheng38329f52009-07-07 14:19:20 -07002807 predictedChainingCell->generic.target = (LIR *) predChainingCell;
2808
2809 genDispatchToHandler(cUnit, TEMPLATE_INVOKE_METHOD_PREDICTED_CHAIN);
2810
2811 /* return through lr - jump to the chaining cell */
2812 genUnconditionalBranch(cUnit, predChainingCell);
2813
2814 /*
2815 * null-check on "this" may have been eliminated, but we still need
2816 * a PC-reconstruction label for stack overflow bailout.
2817 */
2818 if (pcrLabel == NULL) {
2819 int dPC = (int) (cUnit->method->insns + mir->offset);
Bill Buzbee89efc3d2009-07-28 11:22:22 -07002820 pcrLabel = dvmCompilerNew(sizeof(ArmLIR), true);
Ben Chenga4973592010-03-31 11:59:18 -07002821 pcrLabel->opCode = kArmPseudoPCReconstructionCell;
Ben Cheng38329f52009-07-07 14:19:20 -07002822 pcrLabel->operands[0] = dPC;
2823 pcrLabel->operands[1] = mir->offset;
2824 /* Insert the place holder to the growable list */
2825 dvmInsertGrowableList(&cUnit->pcReconstructionList, pcrLabel);
2826 }
2827
2828 /* return through lr+2 - punt to the interpreter */
2829 genUnconditionalBranch(cUnit, pcrLabel);
2830
2831 /*
2832 * return through lr+4 - fully resolve the callee method.
2833 * r1 <- count
2834 * r2 <- &predictedChainCell
2835 * r3 <- this->class
2836 * r4 <- dPC
2837 * r7 <- this->class->vtable
2838 */
2839
2840 /* Save count, &predictedChainCell, and class to high regs first */
Bill Buzbee1465db52009-09-23 17:17:35 -07002841 genRegCopy(cUnit, r8, r1);
2842 genRegCopy(cUnit, r9, r2);
2843 genRegCopy(cUnit, r10, r3);
Ben Cheng38329f52009-07-07 14:19:20 -07002844
Ben Chengba4fc8b2009-06-01 13:00:29 -07002845 /* r0 now contains this->clazz */
Bill Buzbee1465db52009-09-23 17:17:35 -07002846 genRegCopy(cUnit, r0, r3);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002847
2848 /* r1 = BBBB */
2849 loadConstant(cUnit, r1, dInsn->vB);
2850
2851 /* r2 = method (caller) */
2852 loadConstant(cUnit, r2, (int) cUnit->method);
2853
2854 /* r3 = pDvmDex */
2855 loadConstant(cUnit, r3, (int) cUnit->method->clazz->pDvmDex);
2856
Ben Chengbd1326d2010-04-02 15:04:53 -07002857 LOAD_FUNC_ADDR(cUnit, r7,
2858 (intptr_t) dvmFindInterfaceMethodInCache);
Bill Buzbee1465db52009-09-23 17:17:35 -07002859 opReg(cUnit, kOpBlx, r7);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002860 /* r0 = calleeMethod (returned from dvmFindInterfaceMethodInCache */
2861
Ben Cheng09e50c92010-05-02 10:45:32 -07002862 dvmCompilerClobberCallRegs(cUnit);
2863 /* generate a branch over if the interface method is resolved */
2864 opRegImm(cUnit, kOpCmp, r0, 0); /* NULL? */
2865 ArmLIR *branchOver = opCondBranch(cUnit, kArmCondNe);
2866 /*
2867 * calleeMethod == NULL -> throw
2868 */
2869 loadConstant(cUnit, r0,
2870 (int) (cUnit->method->insns + mir->offset));
2871 genDispatchToHandler(cUnit, TEMPLATE_THROW_EXCEPTION_COMMON);
2872 /* noreturn */
2873
2874 ArmLIR *target = newLIR0(cUnit, kArmPseudoTargetLabel);
2875 target->defMask = ENCODE_ALL;
2876 branchOver->generic.target = (LIR *) target;
2877
Bill Buzbee1465db52009-09-23 17:17:35 -07002878 genRegCopy(cUnit, r1, r8);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002879
Ben Cheng38329f52009-07-07 14:19:20 -07002880 /* Check if rechain limit is reached */
Bill Buzbee1465db52009-09-23 17:17:35 -07002881 opRegImm(cUnit, kOpCmp, r1, 0);
Ben Cheng38329f52009-07-07 14:19:20 -07002882
Bill Buzbee1465db52009-09-23 17:17:35 -07002883 ArmLIR *bypassRechaining = opCondBranch(cUnit, kArmCondGt);
Ben Cheng38329f52009-07-07 14:19:20 -07002884
Bill Buzbee270c1d62009-08-13 16:58:07 -07002885 loadWordDisp(cUnit, rGLUE, offsetof(InterpState,
2886 jitToInterpEntries.dvmJitToPatchPredictedChain), r7);
Ben Cheng38329f52009-07-07 14:19:20 -07002887
Ben Chengb88ec3c2010-05-17 12:50:33 -07002888 genRegCopy(cUnit, r1, rGLUE);
Bill Buzbee1465db52009-09-23 17:17:35 -07002889 genRegCopy(cUnit, r2, r9);
2890 genRegCopy(cUnit, r3, r10);
Ben Cheng38329f52009-07-07 14:19:20 -07002891
2892 /*
2893 * r0 = calleeMethod
2894 * r2 = &predictedChainingCell
2895 * r3 = class
2896 *
2897 * &returnChainingCell has been loaded into r1 but is not needed
2898 * when patching the chaining cell and will be clobbered upon
2899 * returning so it will be reconstructed again.
2900 */
Bill Buzbee1465db52009-09-23 17:17:35 -07002901 opReg(cUnit, kOpBlx, r7);
Ben Cheng38329f52009-07-07 14:19:20 -07002902
2903 /* r1 = &retChainingCell */
Bill Buzbee1465db52009-09-23 17:17:35 -07002904 addrRetChain = opRegRegImm(cUnit, kOpAdd, r1, rpc, 0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002905 addrRetChain->generic.target = (LIR *) retChainingCell;
Ben Cheng38329f52009-07-07 14:19:20 -07002906
2907 bypassRechaining->generic.target = (LIR *) addrRetChain;
2908
Ben Chengba4fc8b2009-06-01 13:00:29 -07002909 /*
2910 * r0 = this, r1 = calleeMethod,
2911 * r1 = &ChainingCell,
2912 * r4PC = callsiteDPC,
2913 */
2914 genDispatchToHandler(cUnit, TEMPLATE_INVOKE_METHOD_NO_OPT);
Ben Cheng978738d2010-05-13 13:45:57 -07002915#if defined(WITH_JIT_TUNING)
Ben Cheng86717f72010-03-05 15:27:21 -08002916 gDvmJit.invokePolymorphic++;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002917#endif
2918 /* Handle exceptions using the interpreter */
2919 genTrap(cUnit, mir->offset, pcrLabel);
2920 break;
2921 }
2922 /* NOP */
2923 case OP_INVOKE_DIRECT_EMPTY: {
2924 return false;
2925 }
2926 case OP_FILLED_NEW_ARRAY:
2927 case OP_FILLED_NEW_ARRAY_RANGE: {
2928 /* Just let the interpreter deal with these */
2929 genInterpSingleStep(cUnit, mir);
2930 break;
2931 }
2932 default:
2933 return true;
2934 }
2935 return false;
2936}
2937
2938static bool handleFmt35ms_3rms(CompilationUnit *cUnit, MIR *mir,
Bill Buzbee89efc3d2009-07-28 11:22:22 -07002939 BasicBlock *bb, ArmLIR *labelList)
Ben Chengba4fc8b2009-06-01 13:00:29 -07002940{
Bill Buzbee89efc3d2009-07-28 11:22:22 -07002941 ArmLIR *retChainingCell = &labelList[bb->fallThrough->id];
2942 ArmLIR *predChainingCell = &labelList[bb->taken->id];
2943 ArmLIR *pcrLabel = NULL;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002944
2945 DecodedInstruction *dInsn = &mir->dalvikInsn;
2946 switch (mir->dalvikInsn.opCode) {
2947 /* calleeMethod = this->clazz->vtable[BBBB] */
2948 case OP_INVOKE_VIRTUAL_QUICK_RANGE:
2949 case OP_INVOKE_VIRTUAL_QUICK: {
2950 int methodIndex = dInsn->vB;
2951 if (mir->dalvikInsn.opCode == OP_INVOKE_VIRTUAL_QUICK)
2952 genProcessArgsNoRange(cUnit, mir, dInsn, &pcrLabel);
2953 else
2954 genProcessArgsRange(cUnit, mir, dInsn, &pcrLabel);
2955
Ben Cheng38329f52009-07-07 14:19:20 -07002956 genInvokeVirtualCommon(cUnit, mir, methodIndex,
2957 retChainingCell,
2958 predChainingCell,
2959 pcrLabel);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002960 break;
2961 }
2962 /* calleeMethod = method->clazz->super->vtable[BBBB] */
2963 case OP_INVOKE_SUPER_QUICK:
2964 case OP_INVOKE_SUPER_QUICK_RANGE: {
2965 const Method *calleeMethod =
2966 cUnit->method->clazz->super->vtable[dInsn->vB];
2967
2968 if (mir->dalvikInsn.opCode == OP_INVOKE_SUPER_QUICK)
2969 genProcessArgsNoRange(cUnit, mir, dInsn, &pcrLabel);
2970 else
2971 genProcessArgsRange(cUnit, mir, dInsn, &pcrLabel);
2972
2973 /* r0 = calleeMethod */
2974 loadConstant(cUnit, r0, (int) calleeMethod);
2975
Ben Cheng38329f52009-07-07 14:19:20 -07002976 genInvokeSingletonCommon(cUnit, mir, bb, labelList, pcrLabel,
2977 calleeMethod);
2978 /* Handle exceptions using the interpreter */
2979 genTrap(cUnit, mir->offset, pcrLabel);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002980 break;
2981 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07002982 default:
2983 return true;
2984 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07002985 return false;
2986}
2987
2988/*
Bill Buzbeefd023aa2009-11-02 09:23:49 -08002989 * This operation is complex enough that we'll do it partly inline
2990 * and partly with a handler. NOTE: the handler uses hardcoded
2991 * values for string object offsets and must be revisitied if the
2992 * layout changes.
2993 */
2994static bool genInlinedCompareTo(CompilationUnit *cUnit, MIR *mir)
2995{
2996#if defined(USE_GLOBAL_STRING_DEFS)
2997 return false;
2998#else
2999 ArmLIR *rollback;
Bill Buzbeec6f10662010-02-09 11:16:15 -08003000 RegLocation rlThis = dvmCompilerGetSrc(cUnit, mir, 0);
3001 RegLocation rlComp = dvmCompilerGetSrc(cUnit, mir, 1);
Bill Buzbeefd023aa2009-11-02 09:23:49 -08003002
3003 loadValueDirectFixed(cUnit, rlThis, r0);
3004 loadValueDirectFixed(cUnit, rlComp, r1);
3005 /* Test objects for NULL */
3006 rollback = genNullCheck(cUnit, rlThis.sRegLow, r0, mir->offset, NULL);
3007 genNullCheck(cUnit, rlComp.sRegLow, r1, mir->offset, rollback);
3008 /*
3009 * TUNING: we could check for object pointer equality before invoking
3010 * handler. Unclear whether the gain would be worth the added code size
3011 * expansion.
3012 */
3013 genDispatchToHandler(cUnit, TEMPLATE_STRING_COMPARETO);
Bill Buzbeec6f10662010-02-09 11:16:15 -08003014 storeValue(cUnit, inlinedTarget(cUnit, mir, false),
3015 dvmCompilerGetReturn(cUnit));
Bill Buzbeefd023aa2009-11-02 09:23:49 -08003016 return true;
3017#endif
3018}
3019
Elliott Hughes2bdbcb62010-04-12 14:29:37 -07003020static bool genInlinedFastIndexOf(CompilationUnit *cUnit, MIR *mir)
Bill Buzbeefd023aa2009-11-02 09:23:49 -08003021{
3022#if defined(USE_GLOBAL_STRING_DEFS)
3023 return false;
3024#else
Bill Buzbeec6f10662010-02-09 11:16:15 -08003025 RegLocation rlThis = dvmCompilerGetSrc(cUnit, mir, 0);
3026 RegLocation rlChar = dvmCompilerGetSrc(cUnit, mir, 1);
Bill Buzbeefd023aa2009-11-02 09:23:49 -08003027
3028 loadValueDirectFixed(cUnit, rlThis, r0);
3029 loadValueDirectFixed(cUnit, rlChar, r1);
Elliott Hughes2bdbcb62010-04-12 14:29:37 -07003030 RegLocation rlStart = dvmCompilerGetSrc(cUnit, mir, 2);
3031 loadValueDirectFixed(cUnit, rlStart, r2);
Bill Buzbeefd023aa2009-11-02 09:23:49 -08003032 /* Test objects for NULL */
3033 genNullCheck(cUnit, rlThis.sRegLow, r0, mir->offset, NULL);
3034 genDispatchToHandler(cUnit, TEMPLATE_STRING_INDEXOF);
Bill Buzbeec6f10662010-02-09 11:16:15 -08003035 storeValue(cUnit, inlinedTarget(cUnit, mir, false),
3036 dvmCompilerGetReturn(cUnit));
Bill Buzbeefd023aa2009-11-02 09:23:49 -08003037 return true;
3038#endif
3039}
3040
Elliott Hughesee34f592010-04-05 18:13:52 -07003041// Generates an inlined String.isEmpty or String.length.
3042static bool genInlinedStringIsEmptyOrLength(CompilationUnit *cUnit, MIR *mir,
3043 bool isEmpty)
Bill Buzbee1f748632010-03-02 16:14:41 -08003044{
Elliott Hughesee34f592010-04-05 18:13:52 -07003045 // dst = src.length();
Bill Buzbee1f748632010-03-02 16:14:41 -08003046 RegLocation rlObj = dvmCompilerGetSrc(cUnit, mir, 0);
3047 RegLocation rlDest = inlinedTarget(cUnit, mir, false);
3048 rlObj = loadValue(cUnit, rlObj, kCoreReg);
3049 RegLocation rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
3050 genNullCheck(cUnit, rlObj.sRegLow, rlObj.lowReg, mir->offset, NULL);
3051 loadWordDisp(cUnit, rlObj.lowReg, gDvm.offJavaLangString_count,
3052 rlResult.lowReg);
Elliott Hughesee34f592010-04-05 18:13:52 -07003053 if (isEmpty) {
3054 // dst = (dst == 0);
3055 int tReg = dvmCompilerAllocTemp(cUnit);
3056 opRegReg(cUnit, kOpNeg, tReg, rlResult.lowReg);
3057 opRegRegReg(cUnit, kOpAdc, rlResult.lowReg, rlResult.lowReg, tReg);
3058 }
Bill Buzbee1f748632010-03-02 16:14:41 -08003059 storeValue(cUnit, rlDest, rlResult);
3060 return false;
3061}
3062
Elliott Hughesee34f592010-04-05 18:13:52 -07003063static bool genInlinedStringLength(CompilationUnit *cUnit, MIR *mir)
3064{
3065 return genInlinedStringIsEmptyOrLength(cUnit, mir, false);
3066}
3067
3068static bool genInlinedStringIsEmpty(CompilationUnit *cUnit, MIR *mir)
3069{
3070 return genInlinedStringIsEmptyOrLength(cUnit, mir, true);
3071}
3072
Bill Buzbee1f748632010-03-02 16:14:41 -08003073static bool genInlinedStringCharAt(CompilationUnit *cUnit, MIR *mir)
3074{
3075 int contents = offsetof(ArrayObject, contents);
3076 RegLocation rlObj = dvmCompilerGetSrc(cUnit, mir, 0);
3077 RegLocation rlIdx = dvmCompilerGetSrc(cUnit, mir, 1);
3078 RegLocation rlDest = inlinedTarget(cUnit, mir, false);
3079 RegLocation rlResult;
3080 rlObj = loadValue(cUnit, rlObj, kCoreReg);
3081 rlIdx = loadValue(cUnit, rlIdx, kCoreReg);
3082 int regMax = dvmCompilerAllocTemp(cUnit);
3083 int regOff = dvmCompilerAllocTemp(cUnit);
3084 int regPtr = dvmCompilerAllocTemp(cUnit);
3085 ArmLIR *pcrLabel = genNullCheck(cUnit, rlObj.sRegLow, rlObj.lowReg,
3086 mir->offset, NULL);
3087 loadWordDisp(cUnit, rlObj.lowReg, gDvm.offJavaLangString_count, regMax);
3088 loadWordDisp(cUnit, rlObj.lowReg, gDvm.offJavaLangString_offset, regOff);
3089 loadWordDisp(cUnit, rlObj.lowReg, gDvm.offJavaLangString_value, regPtr);
3090 genBoundsCheck(cUnit, rlIdx.lowReg, regMax, mir->offset, pcrLabel);
3091 dvmCompilerFreeTemp(cUnit, regMax);
3092 opRegImm(cUnit, kOpAdd, regPtr, contents);
3093 opRegReg(cUnit, kOpAdd, regOff, rlIdx.lowReg);
3094 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
3095 loadBaseIndexed(cUnit, regPtr, regOff, rlResult.lowReg, 1, kUnsignedHalf);
3096 storeValue(cUnit, rlDest, rlResult);
3097 return false;
3098}
3099
3100static bool genInlinedAbsInt(CompilationUnit *cUnit, MIR *mir)
3101{
3102 RegLocation rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
3103 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
3104 RegLocation rlDest = inlinedTarget(cUnit, mir, false);;
3105 RegLocation rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
3106 int signReg = dvmCompilerAllocTemp(cUnit);
3107 /*
3108 * abs(x) = y<=x>>31, (x+y)^y.
3109 * Thumb2's IT block also yields 3 instructions, but imposes
3110 * scheduling constraints.
3111 */
3112 opRegRegImm(cUnit, kOpAsr, signReg, rlSrc.lowReg, 31);
3113 opRegRegReg(cUnit, kOpAdd, rlResult.lowReg, rlSrc.lowReg, signReg);
3114 opRegReg(cUnit, kOpXor, rlResult.lowReg, signReg);
3115 storeValue(cUnit, rlDest, rlResult);
3116 return false;
3117}
3118
3119static bool genInlinedAbsLong(CompilationUnit *cUnit, MIR *mir)
3120{
3121 RegLocation rlSrc = dvmCompilerGetSrcWide(cUnit, mir, 0, 1);
3122 RegLocation rlDest = inlinedTargetWide(cUnit, mir, false);
3123 rlSrc = loadValueWide(cUnit, rlSrc, kCoreReg);
3124 RegLocation rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
3125 int signReg = dvmCompilerAllocTemp(cUnit);
3126 /*
3127 * abs(x) = y<=x>>31, (x+y)^y.
3128 * Thumb2 IT block allows slightly shorter sequence,
3129 * but introduces a scheduling barrier. Stick with this
3130 * mechanism for now.
3131 */
3132 opRegRegImm(cUnit, kOpAsr, signReg, rlSrc.highReg, 31);
3133 opRegRegReg(cUnit, kOpAdd, rlResult.lowReg, rlSrc.lowReg, signReg);
3134 opRegRegReg(cUnit, kOpAdc, rlResult.highReg, rlSrc.highReg, signReg);
3135 opRegReg(cUnit, kOpXor, rlResult.lowReg, signReg);
3136 opRegReg(cUnit, kOpXor, rlResult.highReg, signReg);
3137 storeValueWide(cUnit, rlDest, rlResult);
3138 return false;
3139}
Bill Buzbeefd023aa2009-11-02 09:23:49 -08003140
3141/*
Bill Buzbeece46c942009-11-20 15:41:34 -08003142 * NOTE: Handles both range and non-range versions (arguments
3143 * have already been normalized by this point).
Ben Chengba4fc8b2009-06-01 13:00:29 -07003144 */
Bill Buzbeece46c942009-11-20 15:41:34 -08003145static bool handleExecuteInline(CompilationUnit *cUnit, MIR *mir)
Ben Chengba4fc8b2009-06-01 13:00:29 -07003146{
3147 DecodedInstruction *dInsn = &mir->dalvikInsn;
3148 switch( mir->dalvikInsn.opCode) {
Bill Buzbeece46c942009-11-20 15:41:34 -08003149 case OP_EXECUTE_INLINE_RANGE:
Ben Chengba4fc8b2009-06-01 13:00:29 -07003150 case OP_EXECUTE_INLINE: {
3151 unsigned int i;
3152 const InlineOperation* inLineTable = dvmGetInlineOpsTable();
Bill Buzbee50a6bf22009-07-08 13:08:04 -07003153 int offset = offsetof(InterpState, retval);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003154 int operation = dInsn->vB;
Bill Buzbee50a6bf22009-07-08 13:08:04 -07003155 switch (operation) {
3156 case INLINE_EMPTYINLINEMETHOD:
3157 return false; /* Nop */
3158 case INLINE_STRING_LENGTH:
3159 return genInlinedStringLength(cUnit, mir);
Elliott Hughesee34f592010-04-05 18:13:52 -07003160 case INLINE_STRING_IS_EMPTY:
3161 return genInlinedStringIsEmpty(cUnit, mir);
Bill Buzbee50a6bf22009-07-08 13:08:04 -07003162 case INLINE_MATH_ABS_INT:
3163 return genInlinedAbsInt(cUnit, mir);
3164 case INLINE_MATH_ABS_LONG:
3165 return genInlinedAbsLong(cUnit, mir);
3166 case INLINE_MATH_MIN_INT:
3167 return genInlinedMinMaxInt(cUnit, mir, true);
3168 case INLINE_MATH_MAX_INT:
3169 return genInlinedMinMaxInt(cUnit, mir, false);
3170 case INLINE_STRING_CHARAT:
3171 return genInlinedStringCharAt(cUnit, mir);
3172 case INLINE_MATH_SQRT:
3173 if (genInlineSqrt(cUnit, mir))
Bill Buzbee9727c3d2009-08-01 11:32:36 -07003174 return false;
Bill Buzbee50a6bf22009-07-08 13:08:04 -07003175 else
3176 break; /* Handle with C routine */
Bill Buzbee50a6bf22009-07-08 13:08:04 -07003177 case INLINE_MATH_ABS_FLOAT:
Bill Buzbee1465db52009-09-23 17:17:35 -07003178 if (genInlinedAbsFloat(cUnit, mir))
3179 return false;
3180 else
3181 break;
Bill Buzbee50a6bf22009-07-08 13:08:04 -07003182 case INLINE_MATH_ABS_DOUBLE:
Bill Buzbee1465db52009-09-23 17:17:35 -07003183 if (genInlinedAbsDouble(cUnit, mir))
3184 return false;
3185 else
3186 break;
Bill Buzbee50a6bf22009-07-08 13:08:04 -07003187 case INLINE_STRING_COMPARETO:
Bill Buzbeefd023aa2009-11-02 09:23:49 -08003188 if (genInlinedCompareTo(cUnit, mir))
3189 return false;
3190 else
3191 break;
Elliott Hughes2bdbcb62010-04-12 14:29:37 -07003192 case INLINE_STRING_FASTINDEXOF_II:
3193 if (genInlinedFastIndexOf(cUnit, mir))
Bill Buzbeefd023aa2009-11-02 09:23:49 -08003194 return false;
3195 else
3196 break;
3197 case INLINE_STRING_EQUALS:
3198 case INLINE_MATH_COS:
3199 case INLINE_MATH_SIN:
3200 break; /* Handle with C routine */
Bill Buzbee50a6bf22009-07-08 13:08:04 -07003201 default:
Bill Buzbeefc519dc2010-03-06 23:30:57 -08003202 dvmCompilerAbort(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003203 }
Bill Buzbeec6f10662010-02-09 11:16:15 -08003204 dvmCompilerFlushAllRegs(cUnit); /* Everything to home location */
Elliott Hughes6a555132010-02-25 15:41:42 -08003205 dvmCompilerClobberCallRegs(cUnit);
Bill Buzbeec6f10662010-02-09 11:16:15 -08003206 dvmCompilerClobber(cUnit, r4PC);
3207 dvmCompilerClobber(cUnit, r7);
Bill Buzbee1465db52009-09-23 17:17:35 -07003208 opRegRegImm(cUnit, kOpAdd, r4PC, rGLUE, offset);
3209 opImm(cUnit, kOpPush, (1<<r4PC) | (1<<r7));
Ben Chengbd1326d2010-04-02 15:04:53 -07003210 LOAD_FUNC_ADDR(cUnit, r4PC, (int)inLineTable[operation].func);
Bill Buzbee1465db52009-09-23 17:17:35 -07003211 genExportPC(cUnit, mir);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003212 for (i=0; i < dInsn->vA; i++) {
Bill Buzbeec6f10662010-02-09 11:16:15 -08003213 loadValueDirect(cUnit, dvmCompilerGetSrc(cUnit, mir, i), i);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003214 }
Bill Buzbee1465db52009-09-23 17:17:35 -07003215 opReg(cUnit, kOpBlx, r4PC);
3216 opRegImm(cUnit, kOpAdd, r13, 8);
Bill Buzbeece46c942009-11-20 15:41:34 -08003217 opRegImm(cUnit, kOpCmp, r0, 0); /* NULL? */
3218 ArmLIR *branchOver = opCondBranch(cUnit, kArmCondNe);
3219 loadConstant(cUnit, r0,
3220 (int) (cUnit->method->insns + mir->offset));
3221 genDispatchToHandler(cUnit, TEMPLATE_THROW_EXCEPTION_COMMON);
3222 ArmLIR *target = newLIR0(cUnit, kArmPseudoTargetLabel);
3223 target->defMask = ENCODE_ALL;
3224 branchOver->generic.target = (LIR *) target;
Ben Chengba4fc8b2009-06-01 13:00:29 -07003225 break;
3226 }
3227 default:
3228 return true;
3229 }
3230 return false;
3231}
3232
3233static bool handleFmt51l(CompilationUnit *cUnit, MIR *mir)
3234{
Bill Buzbee1465db52009-09-23 17:17:35 -07003235 //TUNING: We're using core regs here - not optimal when target is a double
Bill Buzbeec6f10662010-02-09 11:16:15 -08003236 RegLocation rlDest = dvmCompilerGetDestWide(cUnit, mir, 0, 1);
3237 RegLocation rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Ben Chengbd1326d2010-04-02 15:04:53 -07003238 loadConstantNoClobber(cUnit, rlResult.lowReg,
3239 mir->dalvikInsn.vB_wide & 0xFFFFFFFFUL);
3240 loadConstantNoClobber(cUnit, rlResult.highReg,
3241 (mir->dalvikInsn.vB_wide>>32) & 0xFFFFFFFFUL);
Bill Buzbee1465db52009-09-23 17:17:35 -07003242 storeValueWide(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003243 return false;
3244}
3245
Ben Chengba4fc8b2009-06-01 13:00:29 -07003246/*
3247 * The following are special processing routines that handle transfer of
3248 * controls between compiled code and the interpreter. Certain VM states like
3249 * Dalvik PC and special-purpose registers are reconstructed here.
3250 */
3251
Bill Buzbeebd047242010-05-13 13:02:53 -07003252/*
3253 * Insert a
3254 * b .+4
3255 * nop
3256 * pair at the beginning of a chaining cell. This serves as the
3257 * switch branch that selects between reverting to the interpreter or
3258 * not. Once the cell is chained to a translation, the cell will
3259 * contain a 32-bit branch. Subsequent chain/unchain operations will
3260 * then only alter that first 16-bits - the "b .+4" for unchaining,
3261 * and the restoration of the first half of the 32-bit branch for
3262 * rechaining.
3263 */
3264static void insertChainingSwitch(CompilationUnit *cUnit)
3265{
3266 ArmLIR *branch = newLIR0(cUnit, kThumbBUncond);
3267 newLIR2(cUnit, kThumbOrr, r0, r0);
3268 ArmLIR *target = newLIR0(cUnit, kArmPseudoTargetLabel);
3269 target->defMask = ENCODE_ALL;
3270 branch->generic.target = (LIR *) target;
3271}
3272
Ben Cheng1efc9c52009-06-08 18:25:27 -07003273/* Chaining cell for code that may need warmup. */
3274static void handleNormalChainingCell(CompilationUnit *cUnit,
3275 unsigned int offset)
Ben Chengba4fc8b2009-06-01 13:00:29 -07003276{
Ben Cheng11d8f142010-03-24 15:24:19 -07003277 /*
3278 * Use raw instruction constructors to guarantee that the generated
3279 * instructions fit the predefined cell size.
3280 */
Bill Buzbeebd047242010-05-13 13:02:53 -07003281 insertChainingSwitch(cUnit);
Ben Cheng11d8f142010-03-24 15:24:19 -07003282 newLIR3(cUnit, kThumbLdrRRI5, r0, rGLUE,
3283 offsetof(InterpState,
3284 jitToInterpEntries.dvmJitToInterpNormal) >> 2);
3285 newLIR1(cUnit, kThumbBlxR, r0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003286 addWordData(cUnit, (int) (cUnit->method->insns + offset), true);
3287}
3288
3289/*
Ben Cheng1efc9c52009-06-08 18:25:27 -07003290 * Chaining cell for instructions that immediately following already translated
3291 * code.
Ben Chengba4fc8b2009-06-01 13:00:29 -07003292 */
Ben Cheng1efc9c52009-06-08 18:25:27 -07003293static void handleHotChainingCell(CompilationUnit *cUnit,
3294 unsigned int offset)
Ben Chengba4fc8b2009-06-01 13:00:29 -07003295{
Ben Cheng11d8f142010-03-24 15:24:19 -07003296 /*
3297 * Use raw instruction constructors to guarantee that the generated
3298 * instructions fit the predefined cell size.
3299 */
Bill Buzbeebd047242010-05-13 13:02:53 -07003300 insertChainingSwitch(cUnit);
Ben Cheng11d8f142010-03-24 15:24:19 -07003301 newLIR3(cUnit, kThumbLdrRRI5, r0, rGLUE,
3302 offsetof(InterpState,
3303 jitToInterpEntries.dvmJitToInterpTraceSelect) >> 2);
3304 newLIR1(cUnit, kThumbBlxR, r0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003305 addWordData(cUnit, (int) (cUnit->method->insns + offset), true);
3306}
3307
Bill Buzbee9c4b7c82009-09-10 10:10:38 -07003308#if defined(WITH_SELF_VERIFICATION) || defined(WITH_JIT_TUNING)
Jeff Hao97319a82009-08-12 16:57:15 -07003309/* Chaining cell for branches that branch back into the same basic block */
3310static void handleBackwardBranchChainingCell(CompilationUnit *cUnit,
3311 unsigned int offset)
3312{
Ben Cheng11d8f142010-03-24 15:24:19 -07003313 /*
3314 * Use raw instruction constructors to guarantee that the generated
3315 * instructions fit the predefined cell size.
3316 */
Bill Buzbeebd047242010-05-13 13:02:53 -07003317 insertChainingSwitch(cUnit);
Bill Buzbee9c4b7c82009-09-10 10:10:38 -07003318#if defined(WITH_SELF_VERIFICATION)
Bill Buzbee1465db52009-09-23 17:17:35 -07003319 newLIR3(cUnit, kThumbLdrRRI5, r0, rGLUE,
Ben Cheng40094c12010-02-24 20:58:44 -08003320 offsetof(InterpState,
3321 jitToInterpEntries.dvmJitToInterpBackwardBranch) >> 2);
Bill Buzbee9c4b7c82009-09-10 10:10:38 -07003322#else
Bill Buzbee1465db52009-09-23 17:17:35 -07003323 newLIR3(cUnit, kThumbLdrRRI5, r0, rGLUE,
Bill Buzbee9c4b7c82009-09-10 10:10:38 -07003324 offsetof(InterpState, jitToInterpEntries.dvmJitToInterpNormal) >> 2);
3325#endif
Bill Buzbee1465db52009-09-23 17:17:35 -07003326 newLIR1(cUnit, kThumbBlxR, r0);
Jeff Hao97319a82009-08-12 16:57:15 -07003327 addWordData(cUnit, (int) (cUnit->method->insns + offset), true);
3328}
3329
3330#endif
Ben Chengba4fc8b2009-06-01 13:00:29 -07003331/* Chaining cell for monomorphic method invocations. */
Ben Cheng38329f52009-07-07 14:19:20 -07003332static void handleInvokeSingletonChainingCell(CompilationUnit *cUnit,
3333 const Method *callee)
Ben Chengba4fc8b2009-06-01 13:00:29 -07003334{
Ben Cheng11d8f142010-03-24 15:24:19 -07003335 /*
3336 * Use raw instruction constructors to guarantee that the generated
3337 * instructions fit the predefined cell size.
3338 */
Bill Buzbeebd047242010-05-13 13:02:53 -07003339 insertChainingSwitch(cUnit);
Ben Cheng11d8f142010-03-24 15:24:19 -07003340 newLIR3(cUnit, kThumbLdrRRI5, r0, rGLUE,
3341 offsetof(InterpState,
3342 jitToInterpEntries.dvmJitToInterpTraceSelect) >> 2);
3343 newLIR1(cUnit, kThumbBlxR, r0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003344 addWordData(cUnit, (int) (callee->insns), true);
3345}
3346
Ben Cheng38329f52009-07-07 14:19:20 -07003347/* Chaining cell for monomorphic method invocations. */
3348static void handleInvokePredictedChainingCell(CompilationUnit *cUnit)
3349{
3350
3351 /* Should not be executed in the initial state */
3352 addWordData(cUnit, PREDICTED_CHAIN_BX_PAIR_INIT, true);
3353 /* To be filled: class */
3354 addWordData(cUnit, PREDICTED_CHAIN_CLAZZ_INIT, true);
3355 /* To be filled: method */
3356 addWordData(cUnit, PREDICTED_CHAIN_METHOD_INIT, true);
3357 /*
3358 * Rechain count. The initial value of 0 here will trigger chaining upon
3359 * the first invocation of this callsite.
3360 */
3361 addWordData(cUnit, PREDICTED_CHAIN_COUNTER_INIT, true);
3362}
3363
Ben Chengba4fc8b2009-06-01 13:00:29 -07003364/* Load the Dalvik PC into r0 and jump to the specified target */
3365static void handlePCReconstruction(CompilationUnit *cUnit,
Bill Buzbee89efc3d2009-07-28 11:22:22 -07003366 ArmLIR *targetLabel)
Ben Chengba4fc8b2009-06-01 13:00:29 -07003367{
Bill Buzbee89efc3d2009-07-28 11:22:22 -07003368 ArmLIR **pcrLabel =
3369 (ArmLIR **) cUnit->pcReconstructionList.elemList;
Ben Chengba4fc8b2009-06-01 13:00:29 -07003370 int numElems = cUnit->pcReconstructionList.numUsed;
3371 int i;
3372 for (i = 0; i < numElems; i++) {
3373 dvmCompilerAppendLIR(cUnit, (LIR *) pcrLabel[i]);
3374 /* r0 = dalvik PC */
3375 loadConstant(cUnit, r0, pcrLabel[i]->operands[0]);
3376 genUnconditionalBranch(cUnit, targetLabel);
3377 }
3378}
3379
Bill Buzbee1465db52009-09-23 17:17:35 -07003380static char *extendedMIROpNames[kMirOpLast - kMirOpFirst] = {
3381 "kMirOpPhi",
3382 "kMirOpNullNRangeUpCheck",
3383 "kMirOpNullNRangeDownCheck",
3384 "kMirOpLowerBound",
3385 "kMirOpPunt",
Ben Cheng4238ec22009-08-24 16:32:22 -07003386};
3387
3388/*
3389 * vA = arrayReg;
3390 * vB = idxReg;
3391 * vC = endConditionReg;
3392 * arg[0] = maxC
3393 * arg[1] = minC
3394 * arg[2] = loopBranchConditionCode
3395 */
3396static void genHoistedChecksForCountUpLoop(CompilationUnit *cUnit, MIR *mir)
3397{
Bill Buzbee1465db52009-09-23 17:17:35 -07003398 /*
3399 * NOTE: these synthesized blocks don't have ssa names assigned
3400 * for Dalvik registers. However, because they dominate the following
3401 * blocks we can simply use the Dalvik name w/ subscript 0 as the
3402 * ssa name.
3403 */
Ben Cheng4238ec22009-08-24 16:32:22 -07003404 DecodedInstruction *dInsn = &mir->dalvikInsn;
3405 const int lenOffset = offsetof(ArrayObject, length);
Ben Cheng4238ec22009-08-24 16:32:22 -07003406 const int maxC = dInsn->arg[0];
Bill Buzbee1465db52009-09-23 17:17:35 -07003407 int regLength;
3408 RegLocation rlArray = cUnit->regLocation[mir->dalvikInsn.vA];
3409 RegLocation rlIdxEnd = cUnit->regLocation[mir->dalvikInsn.vC];
Ben Cheng4238ec22009-08-24 16:32:22 -07003410
3411 /* regArray <- arrayRef */
Bill Buzbee1465db52009-09-23 17:17:35 -07003412 rlArray = loadValue(cUnit, rlArray, kCoreReg);
3413 rlIdxEnd = loadValue(cUnit, rlIdxEnd, kCoreReg);
3414 genRegImmCheck(cUnit, kArmCondEq, rlArray.lowReg, 0, 0,
Ben Cheng4238ec22009-08-24 16:32:22 -07003415 (ArmLIR *) cUnit->loopAnalysis->branchToPCR);
3416
3417 /* regLength <- len(arrayRef) */
Bill Buzbeec6f10662010-02-09 11:16:15 -08003418 regLength = dvmCompilerAllocTemp(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07003419 loadWordDisp(cUnit, rlArray.lowReg, lenOffset, regLength);
Ben Cheng4238ec22009-08-24 16:32:22 -07003420
3421 int delta = maxC;
3422 /*
3423 * If the loop end condition is ">=" instead of ">", then the largest value
3424 * of the index is "endCondition - 1".
3425 */
3426 if (dInsn->arg[2] == OP_IF_GE) {
3427 delta--;
3428 }
3429
3430 if (delta) {
Bill Buzbeec6f10662010-02-09 11:16:15 -08003431 int tReg = dvmCompilerAllocTemp(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07003432 opRegRegImm(cUnit, kOpAdd, tReg, rlIdxEnd.lowReg, delta);
3433 rlIdxEnd.lowReg = tReg;
Bill Buzbeec6f10662010-02-09 11:16:15 -08003434 dvmCompilerFreeTemp(cUnit, tReg);
Ben Cheng4238ec22009-08-24 16:32:22 -07003435 }
3436 /* Punt if "regIdxEnd < len(Array)" is false */
Bill Buzbee1465db52009-09-23 17:17:35 -07003437 genRegRegCheck(cUnit, kArmCondGe, rlIdxEnd.lowReg, regLength, 0,
Ben Cheng0fd31e42009-09-03 14:40:16 -07003438 (ArmLIR *) cUnit->loopAnalysis->branchToPCR);
Ben Cheng4238ec22009-08-24 16:32:22 -07003439}
3440
3441/*
3442 * vA = arrayReg;
3443 * vB = idxReg;
3444 * vC = endConditionReg;
3445 * arg[0] = maxC
3446 * arg[1] = minC
3447 * arg[2] = loopBranchConditionCode
3448 */
3449static void genHoistedChecksForCountDownLoop(CompilationUnit *cUnit, MIR *mir)
3450{
3451 DecodedInstruction *dInsn = &mir->dalvikInsn;
3452 const int lenOffset = offsetof(ArrayObject, length);
Bill Buzbeec6f10662010-02-09 11:16:15 -08003453 const int regLength = dvmCompilerAllocTemp(cUnit);
Ben Cheng4238ec22009-08-24 16:32:22 -07003454 const int maxC = dInsn->arg[0];
Bill Buzbee1465db52009-09-23 17:17:35 -07003455 RegLocation rlArray = cUnit->regLocation[mir->dalvikInsn.vA];
3456 RegLocation rlIdxInit = cUnit->regLocation[mir->dalvikInsn.vB];
Ben Cheng4238ec22009-08-24 16:32:22 -07003457
3458 /* regArray <- arrayRef */
Bill Buzbee1465db52009-09-23 17:17:35 -07003459 rlArray = loadValue(cUnit, rlArray, kCoreReg);
3460 rlIdxInit = loadValue(cUnit, rlIdxInit, kCoreReg);
3461 genRegImmCheck(cUnit, kArmCondEq, rlArray.lowReg, 0, 0,
Ben Cheng4238ec22009-08-24 16:32:22 -07003462 (ArmLIR *) cUnit->loopAnalysis->branchToPCR);
3463
3464 /* regLength <- len(arrayRef) */
Bill Buzbee1465db52009-09-23 17:17:35 -07003465 loadWordDisp(cUnit, rlArray.lowReg, lenOffset, regLength);
Ben Cheng4238ec22009-08-24 16:32:22 -07003466
3467 if (maxC) {
Bill Buzbeec6f10662010-02-09 11:16:15 -08003468 int tReg = dvmCompilerAllocTemp(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07003469 opRegRegImm(cUnit, kOpAdd, tReg, rlIdxInit.lowReg, maxC);
3470 rlIdxInit.lowReg = tReg;
Bill Buzbeec6f10662010-02-09 11:16:15 -08003471 dvmCompilerFreeTemp(cUnit, tReg);
Ben Cheng4238ec22009-08-24 16:32:22 -07003472 }
3473
3474 /* Punt if "regIdxInit < len(Array)" is false */
Bill Buzbee1465db52009-09-23 17:17:35 -07003475 genRegRegCheck(cUnit, kArmCondGe, rlIdxInit.lowReg, regLength, 0,
Ben Cheng0fd31e42009-09-03 14:40:16 -07003476 (ArmLIR *) cUnit->loopAnalysis->branchToPCR);
Ben Cheng4238ec22009-08-24 16:32:22 -07003477}
3478
3479/*
3480 * vA = idxReg;
3481 * vB = minC;
3482 */
3483static void genHoistedLowerBoundCheck(CompilationUnit *cUnit, MIR *mir)
3484{
3485 DecodedInstruction *dInsn = &mir->dalvikInsn;
Ben Cheng4238ec22009-08-24 16:32:22 -07003486 const int minC = dInsn->vB;
Bill Buzbee1465db52009-09-23 17:17:35 -07003487 RegLocation rlIdx = cUnit->regLocation[mir->dalvikInsn.vA];
Ben Cheng4238ec22009-08-24 16:32:22 -07003488
3489 /* regIdx <- initial index value */
Bill Buzbee1465db52009-09-23 17:17:35 -07003490 rlIdx = loadValue(cUnit, rlIdx, kCoreReg);
Ben Cheng4238ec22009-08-24 16:32:22 -07003491
3492 /* Punt if "regIdxInit + minC >= 0" is false */
Bill Buzbee1465db52009-09-23 17:17:35 -07003493 genRegImmCheck(cUnit, kArmCondLt, rlIdx.lowReg, -minC, 0,
Ben Cheng4238ec22009-08-24 16:32:22 -07003494 (ArmLIR *) cUnit->loopAnalysis->branchToPCR);
3495}
3496
3497/* Extended MIR instructions like PHI */
3498static void handleExtendedMIR(CompilationUnit *cUnit, MIR *mir)
3499{
Bill Buzbee1465db52009-09-23 17:17:35 -07003500 int opOffset = mir->dalvikInsn.opCode - kMirOpFirst;
Ben Cheng4238ec22009-08-24 16:32:22 -07003501 char *msg = dvmCompilerNew(strlen(extendedMIROpNames[opOffset]) + 1,
3502 false);
3503 strcpy(msg, extendedMIROpNames[opOffset]);
Bill Buzbee1465db52009-09-23 17:17:35 -07003504 newLIR1(cUnit, kArmPseudoExtended, (int) msg);
Ben Cheng4238ec22009-08-24 16:32:22 -07003505
3506 switch (mir->dalvikInsn.opCode) {
Bill Buzbee1465db52009-09-23 17:17:35 -07003507 case kMirOpPhi: {
Ben Cheng4238ec22009-08-24 16:32:22 -07003508 char *ssaString = dvmCompilerGetSSAString(cUnit, mir->ssaRep);
Bill Buzbee1465db52009-09-23 17:17:35 -07003509 newLIR1(cUnit, kArmPseudoSSARep, (int) ssaString);
Ben Cheng4238ec22009-08-24 16:32:22 -07003510 break;
3511 }
Bill Buzbee1465db52009-09-23 17:17:35 -07003512 case kMirOpNullNRangeUpCheck: {
Ben Cheng4238ec22009-08-24 16:32:22 -07003513 genHoistedChecksForCountUpLoop(cUnit, mir);
3514 break;
3515 }
Bill Buzbee1465db52009-09-23 17:17:35 -07003516 case kMirOpNullNRangeDownCheck: {
Ben Cheng4238ec22009-08-24 16:32:22 -07003517 genHoistedChecksForCountDownLoop(cUnit, mir);
3518 break;
3519 }
Bill Buzbee1465db52009-09-23 17:17:35 -07003520 case kMirOpLowerBound: {
Ben Cheng4238ec22009-08-24 16:32:22 -07003521 genHoistedLowerBoundCheck(cUnit, mir);
3522 break;
3523 }
Bill Buzbee1465db52009-09-23 17:17:35 -07003524 case kMirOpPunt: {
Ben Cheng4238ec22009-08-24 16:32:22 -07003525 genUnconditionalBranch(cUnit,
3526 (ArmLIR *) cUnit->loopAnalysis->branchToPCR);
3527 break;
3528 }
3529 default:
3530 break;
3531 }
3532}
3533
3534/*
3535 * Create a PC-reconstruction cell for the starting offset of this trace.
3536 * Since the PCR cell is placed near the end of the compiled code which is
3537 * usually out of range for a conditional branch, we put two branches (one
3538 * branch over to the loop body and one layover branch to the actual PCR) at the
3539 * end of the entry block.
3540 */
3541static void setupLoopEntryBlock(CompilationUnit *cUnit, BasicBlock *entry,
3542 ArmLIR *bodyLabel)
3543{
3544 /* Set up the place holder to reconstruct this Dalvik PC */
3545 ArmLIR *pcrLabel = dvmCompilerNew(sizeof(ArmLIR), true);
Ben Chenga4973592010-03-31 11:59:18 -07003546 pcrLabel->opCode = kArmPseudoPCReconstructionCell;
Ben Cheng4238ec22009-08-24 16:32:22 -07003547 pcrLabel->operands[0] =
3548 (int) (cUnit->method->insns + entry->startOffset);
3549 pcrLabel->operands[1] = entry->startOffset;
3550 /* Insert the place holder to the growable list */
3551 dvmInsertGrowableList(&cUnit->pcReconstructionList, pcrLabel);
3552
3553 /*
3554 * Next, create two branches - one branch over to the loop body and the
3555 * other branch to the PCR cell to punt.
3556 */
3557 ArmLIR *branchToBody = dvmCompilerNew(sizeof(ArmLIR), true);
Bill Buzbee1465db52009-09-23 17:17:35 -07003558 branchToBody->opCode = kThumbBUncond;
Ben Cheng4238ec22009-08-24 16:32:22 -07003559 branchToBody->generic.target = (LIR *) bodyLabel;
Ben Chengdcf3e5d2009-09-11 13:42:05 -07003560 setupResourceMasks(branchToBody);
Ben Cheng4238ec22009-08-24 16:32:22 -07003561 cUnit->loopAnalysis->branchToBody = (LIR *) branchToBody;
3562
3563 ArmLIR *branchToPCR = dvmCompilerNew(sizeof(ArmLIR), true);
Bill Buzbee1465db52009-09-23 17:17:35 -07003564 branchToPCR->opCode = kThumbBUncond;
Ben Cheng4238ec22009-08-24 16:32:22 -07003565 branchToPCR->generic.target = (LIR *) pcrLabel;
Ben Chengdcf3e5d2009-09-11 13:42:05 -07003566 setupResourceMasks(branchToPCR);
Ben Cheng4238ec22009-08-24 16:32:22 -07003567 cUnit->loopAnalysis->branchToPCR = (LIR *) branchToPCR;
3568}
3569
Ben Chengd5adae12010-03-26 17:45:28 -07003570#if defined(WITH_SELF_VERIFICATION)
3571static bool selfVerificationPuntOps(MIR *mir)
3572{
3573 DecodedInstruction *decInsn = &mir->dalvikInsn;
3574 OpCode op = decInsn->opCode;
3575 int flags = dexGetInstrFlags(gDvm.instrFlags, op);
3576 /*
3577 * All opcodes that can throw exceptions and use the
3578 * TEMPLATE_THROW_EXCEPTION_COMMON template should be excluded in the trace
3579 * under self-verification mode.
3580 */
3581 return (op == OP_MONITOR_ENTER || op == OP_MONITOR_EXIT ||
3582 op == OP_NEW_INSTANCE || op == OP_NEW_ARRAY ||
3583 op == OP_CHECK_CAST || op == OP_MOVE_EXCEPTION ||
3584 op == OP_FILL_ARRAY_DATA || op == OP_EXECUTE_INLINE ||
3585 op == OP_EXECUTE_INLINE_RANGE ||
3586 (flags & kInstrInvoke));
3587}
3588#endif
3589
Ben Chengba4fc8b2009-06-01 13:00:29 -07003590void dvmCompilerMIR2LIR(CompilationUnit *cUnit)
3591{
3592 /* Used to hold the labels of each block */
Bill Buzbee89efc3d2009-07-28 11:22:22 -07003593 ArmLIR *labelList =
3594 dvmCompilerNew(sizeof(ArmLIR) * cUnit->numBlocks, true);
Ben Chengcec26f62010-01-15 15:29:33 -08003595 GrowableList chainingListByType[kChainingCellGap];
Ben Chengba4fc8b2009-06-01 13:00:29 -07003596 int i;
3597
3598 /*
Ben Cheng38329f52009-07-07 14:19:20 -07003599 * Initialize various types chaining lists.
Ben Chengba4fc8b2009-06-01 13:00:29 -07003600 */
Ben Chengcec26f62010-01-15 15:29:33 -08003601 for (i = 0; i < kChainingCellGap; i++) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07003602 dvmInitGrowableList(&chainingListByType[i], 2);
3603 }
3604
3605 BasicBlock **blockList = cUnit->blockList;
3606
Bill Buzbee6e963e12009-06-17 16:56:19 -07003607 if (cUnit->executionCount) {
3608 /*
3609 * Reserve 6 bytes at the beginning of the trace
3610 * +----------------------------+
3611 * | execution count (4 bytes) |
3612 * +----------------------------+
3613 * | chain cell offset (2 bytes)|
3614 * +----------------------------+
3615 * ...and then code to increment the execution
3616 * count:
3617 * mov r0, pc @ move adr of "mov r0,pc" + 4 to r0
3618 * sub r0, #10 @ back up to addr of executionCount
3619 * ldr r1, [r0]
3620 * add r1, #1
3621 * str r1, [r0]
3622 */
Bill Buzbee1465db52009-09-23 17:17:35 -07003623 newLIR1(cUnit, kArm16BitData, 0);
3624 newLIR1(cUnit, kArm16BitData, 0);
Ben Chengcc6600c2009-06-22 14:45:16 -07003625 cUnit->chainCellOffsetLIR =
Bill Buzbee1465db52009-09-23 17:17:35 -07003626 (LIR *) newLIR1(cUnit, kArm16BitData, CHAIN_CELL_OFFSET_TAG);
Bill Buzbee6e963e12009-06-17 16:56:19 -07003627 cUnit->headerSize = 6;
Bill Buzbee270c1d62009-08-13 16:58:07 -07003628 /* Thumb instruction used directly here to ensure correct size */
Bill Buzbee1465db52009-09-23 17:17:35 -07003629 newLIR2(cUnit, kThumbMovRR_H2L, r0, rpc);
3630 newLIR2(cUnit, kThumbSubRI8, r0, 10);
3631 newLIR3(cUnit, kThumbLdrRRI5, r1, r0, 0);
3632 newLIR2(cUnit, kThumbAddRI8, r1, 1);
3633 newLIR3(cUnit, kThumbStrRRI5, r1, r0, 0);
Bill Buzbee6e963e12009-06-17 16:56:19 -07003634 } else {
3635 /* Just reserve 2 bytes for the chain cell offset */
Ben Chengcc6600c2009-06-22 14:45:16 -07003636 cUnit->chainCellOffsetLIR =
Bill Buzbee1465db52009-09-23 17:17:35 -07003637 (LIR *) newLIR1(cUnit, kArm16BitData, CHAIN_CELL_OFFSET_TAG);
Bill Buzbee6e963e12009-06-17 16:56:19 -07003638 cUnit->headerSize = 2;
3639 }
Ben Cheng1efc9c52009-06-08 18:25:27 -07003640
Ben Chengba4fc8b2009-06-01 13:00:29 -07003641 /* Handle the content in each basic block */
3642 for (i = 0; i < cUnit->numBlocks; i++) {
3643 blockList[i]->visited = true;
3644 MIR *mir;
3645
3646 labelList[i].operands[0] = blockList[i]->startOffset;
3647
Ben Chengcec26f62010-01-15 15:29:33 -08003648 if (blockList[i]->blockType >= kChainingCellGap) {
Ben Chengd44faf52010-06-02 15:33:51 -07003649 if (blockList[i]->firstMIRInsn != NULL &&
3650 ((blockList[i]->firstMIRInsn->dalvikInsn.opCode ==
3651 OP_MOVE_RESULT) ||
3652 (blockList[i]->firstMIRInsn->dalvikInsn.opCode ==
3653 OP_MOVE_RESULT_WIDE) ||
3654 (blockList[i]->firstMIRInsn->dalvikInsn.opCode ==
3655 OP_MOVE_RESULT_OBJECT))) {
3656 /* Align this block first since it is a return chaining cell */
3657 newLIR0(cUnit, kArmPseudoPseudoAlign4);
3658 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07003659 /*
3660 * Append the label pseudo LIR first. Chaining cells will be handled
3661 * separately afterwards.
3662 */
3663 dvmCompilerAppendLIR(cUnit, (LIR *) &labelList[i]);
3664 }
3665
Bill Buzbee1465db52009-09-23 17:17:35 -07003666 if (blockList[i]->blockType == kEntryBlock) {
Ben Chenga4973592010-03-31 11:59:18 -07003667 labelList[i].opCode = kArmPseudoEntryBlock;
Ben Cheng4238ec22009-08-24 16:32:22 -07003668 if (blockList[i]->firstMIRInsn == NULL) {
3669 continue;
3670 } else {
3671 setupLoopEntryBlock(cUnit, blockList[i],
3672 &labelList[blockList[i]->fallThrough->id]);
3673 }
Bill Buzbee1465db52009-09-23 17:17:35 -07003674 } else if (blockList[i]->blockType == kExitBlock) {
Ben Chenga4973592010-03-31 11:59:18 -07003675 labelList[i].opCode = kArmPseudoExitBlock;
Ben Cheng4238ec22009-08-24 16:32:22 -07003676 goto gen_fallthrough;
Bill Buzbee1465db52009-09-23 17:17:35 -07003677 } else if (blockList[i]->blockType == kDalvikByteCode) {
3678 labelList[i].opCode = kArmPseudoNormalBlockLabel;
Ben Chenge9695e52009-06-16 16:11:47 -07003679 /* Reset the register state */
Bill Buzbeec6f10662010-02-09 11:16:15 -08003680 dvmCompilerResetRegPool(cUnit);
3681 dvmCompilerClobberAllRegs(cUnit);
3682 dvmCompilerResetNullCheck(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003683 } else {
3684 switch (blockList[i]->blockType) {
Bill Buzbee1465db52009-09-23 17:17:35 -07003685 case kChainingCellNormal:
Ben Chenga4973592010-03-31 11:59:18 -07003686 labelList[i].opCode = kArmPseudoChainingCellNormal;
Ben Chengba4fc8b2009-06-01 13:00:29 -07003687 /* handle the codegen later */
3688 dvmInsertGrowableList(
Bill Buzbee1465db52009-09-23 17:17:35 -07003689 &chainingListByType[kChainingCellNormal], (void *) i);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003690 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07003691 case kChainingCellInvokeSingleton:
Ben Cheng38329f52009-07-07 14:19:20 -07003692 labelList[i].opCode =
Ben Chenga4973592010-03-31 11:59:18 -07003693 kArmPseudoChainingCellInvokeSingleton;
Ben Chengba4fc8b2009-06-01 13:00:29 -07003694 labelList[i].operands[0] =
3695 (int) blockList[i]->containingMethod;
3696 /* handle the codegen later */
3697 dvmInsertGrowableList(
Bill Buzbee1465db52009-09-23 17:17:35 -07003698 &chainingListByType[kChainingCellInvokeSingleton],
Ben Cheng38329f52009-07-07 14:19:20 -07003699 (void *) i);
3700 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07003701 case kChainingCellInvokePredicted:
Ben Cheng38329f52009-07-07 14:19:20 -07003702 labelList[i].opCode =
Ben Chenga4973592010-03-31 11:59:18 -07003703 kArmPseudoChainingCellInvokePredicted;
Ben Cheng38329f52009-07-07 14:19:20 -07003704 /* handle the codegen later */
3705 dvmInsertGrowableList(
Bill Buzbee1465db52009-09-23 17:17:35 -07003706 &chainingListByType[kChainingCellInvokePredicted],
Ben Cheng38329f52009-07-07 14:19:20 -07003707 (void *) i);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003708 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07003709 case kChainingCellHot:
Ben Chengba4fc8b2009-06-01 13:00:29 -07003710 labelList[i].opCode =
Ben Chenga4973592010-03-31 11:59:18 -07003711 kArmPseudoChainingCellHot;
Ben Chengba4fc8b2009-06-01 13:00:29 -07003712 /* handle the codegen later */
3713 dvmInsertGrowableList(
Bill Buzbee1465db52009-09-23 17:17:35 -07003714 &chainingListByType[kChainingCellHot],
Ben Chengba4fc8b2009-06-01 13:00:29 -07003715 (void *) i);
3716 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07003717 case kPCReconstruction:
Ben Chengba4fc8b2009-06-01 13:00:29 -07003718 /* Make sure exception handling block is next */
3719 labelList[i].opCode =
Ben Chenga4973592010-03-31 11:59:18 -07003720 kArmPseudoPCReconstructionBlockLabel;
Ben Chengba4fc8b2009-06-01 13:00:29 -07003721 assert (i == cUnit->numBlocks - 2);
3722 handlePCReconstruction(cUnit, &labelList[i+1]);
3723 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07003724 case kExceptionHandling:
3725 labelList[i].opCode = kArmPseudoEHBlockLabel;
Ben Chengba4fc8b2009-06-01 13:00:29 -07003726 if (cUnit->pcReconstructionList.numUsed) {
Bill Buzbee270c1d62009-08-13 16:58:07 -07003727 loadWordDisp(cUnit, rGLUE, offsetof(InterpState,
3728 jitToInterpEntries.dvmJitToInterpPunt),
3729 r1);
Bill Buzbee1465db52009-09-23 17:17:35 -07003730 opReg(cUnit, kOpBlx, r1);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003731 }
3732 break;
Bill Buzbee9c4b7c82009-09-10 10:10:38 -07003733#if defined(WITH_SELF_VERIFICATION) || defined(WITH_JIT_TUNING)
Bill Buzbee1465db52009-09-23 17:17:35 -07003734 case kChainingCellBackwardBranch:
Jeff Hao97319a82009-08-12 16:57:15 -07003735 labelList[i].opCode =
Ben Chenga4973592010-03-31 11:59:18 -07003736 kArmPseudoChainingCellBackwardBranch;
Jeff Hao97319a82009-08-12 16:57:15 -07003737 /* handle the codegen later */
3738 dvmInsertGrowableList(
Bill Buzbee1465db52009-09-23 17:17:35 -07003739 &chainingListByType[kChainingCellBackwardBranch],
Jeff Hao97319a82009-08-12 16:57:15 -07003740 (void *) i);
3741 break;
3742#endif
Ben Chengba4fc8b2009-06-01 13:00:29 -07003743 default:
3744 break;
3745 }
3746 continue;
3747 }
Ben Chenge9695e52009-06-16 16:11:47 -07003748
Bill Buzbee89efc3d2009-07-28 11:22:22 -07003749 ArmLIR *headLIR = NULL;
Ben Chenge9695e52009-06-16 16:11:47 -07003750
Ben Chengba4fc8b2009-06-01 13:00:29 -07003751 for (mir = blockList[i]->firstMIRInsn; mir; mir = mir->next) {
Bill Buzbee1465db52009-09-23 17:17:35 -07003752
Bill Buzbeec6f10662010-02-09 11:16:15 -08003753 dvmCompilerResetRegPool(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07003754 if (gDvmJit.disableOpt & (1 << kTrackLiveTemps)) {
Bill Buzbeec6f10662010-02-09 11:16:15 -08003755 dvmCompilerClobberAllRegs(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07003756 }
3757
3758 if (gDvmJit.disableOpt & (1 << kSuppressLoads)) {
Bill Buzbeec6f10662010-02-09 11:16:15 -08003759 dvmCompilerResetDefTracking(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07003760 }
3761
3762 if (mir->dalvikInsn.opCode >= kMirOpFirst) {
Ben Cheng4238ec22009-08-24 16:32:22 -07003763 handleExtendedMIR(cUnit, mir);
3764 continue;
3765 }
3766
Bill Buzbee1465db52009-09-23 17:17:35 -07003767
Ben Chengba4fc8b2009-06-01 13:00:29 -07003768 OpCode dalvikOpCode = mir->dalvikInsn.opCode;
3769 InstructionFormat dalvikFormat =
3770 dexGetInstrFormat(gDvm.instrFormat, dalvikOpCode);
Bill Buzbee89efc3d2009-07-28 11:22:22 -07003771 ArmLIR *boundaryLIR =
Ben Chenga4973592010-03-31 11:59:18 -07003772 newLIR2(cUnit, kArmPseudoDalvikByteCodeBoundary,
Ben Chengccd6c012009-10-15 14:52:45 -07003773 mir->offset,
3774 (int) dvmCompilerGetDalvikDisassembly(&mir->dalvikInsn)
3775 );
Ben Cheng4238ec22009-08-24 16:32:22 -07003776 if (mir->ssaRep) {
3777 char *ssaString = dvmCompilerGetSSAString(cUnit, mir->ssaRep);
Bill Buzbee1465db52009-09-23 17:17:35 -07003778 newLIR1(cUnit, kArmPseudoSSARep, (int) ssaString);
Ben Cheng4238ec22009-08-24 16:32:22 -07003779 }
3780
Ben Chenge9695e52009-06-16 16:11:47 -07003781 /* Remember the first LIR for this block */
3782 if (headLIR == NULL) {
3783 headLIR = boundaryLIR;
Ben Chengd7d426a2009-09-22 11:23:36 -07003784 /* Set the first boundaryLIR as a scheduling barrier */
3785 headLIR->defMask = ENCODE_ALL;
Ben Chenge9695e52009-06-16 16:11:47 -07003786 }
Ben Cheng4238ec22009-08-24 16:32:22 -07003787
Ben Chengba4fc8b2009-06-01 13:00:29 -07003788 bool notHandled;
3789 /*
3790 * Debugging: screen the opcode first to see if it is in the
3791 * do[-not]-compile list
3792 */
3793 bool singleStepMe =
3794 gDvmJit.includeSelectedOp !=
3795 ((gDvmJit.opList[dalvikOpCode >> 3] &
3796 (1 << (dalvikOpCode & 0x7))) !=
3797 0);
Ben Chengd5adae12010-03-26 17:45:28 -07003798#if defined(WITH_SELF_VERIFICATION)
3799 if (singleStepMe == false) {
3800 singleStepMe = selfVerificationPuntOps(mir);
3801 }
3802#endif
Ben Chengba4fc8b2009-06-01 13:00:29 -07003803 if (singleStepMe || cUnit->allSingleStep) {
3804 notHandled = false;
3805 genInterpSingleStep(cUnit, mir);
3806 } else {
3807 opcodeCoverage[dalvikOpCode]++;
3808 switch (dalvikFormat) {
3809 case kFmt10t:
3810 case kFmt20t:
3811 case kFmt30t:
3812 notHandled = handleFmt10t_Fmt20t_Fmt30t(cUnit,
3813 mir, blockList[i], labelList);
3814 break;
3815 case kFmt10x:
3816 notHandled = handleFmt10x(cUnit, mir);
3817 break;
3818 case kFmt11n:
3819 case kFmt31i:
3820 notHandled = handleFmt11n_Fmt31i(cUnit, mir);
3821 break;
3822 case kFmt11x:
3823 notHandled = handleFmt11x(cUnit, mir);
3824 break;
3825 case kFmt12x:
3826 notHandled = handleFmt12x(cUnit, mir);
3827 break;
3828 case kFmt20bc:
3829 notHandled = handleFmt20bc(cUnit, mir);
3830 break;
3831 case kFmt21c:
3832 case kFmt31c:
3833 notHandled = handleFmt21c_Fmt31c(cUnit, mir);
3834 break;
3835 case kFmt21h:
3836 notHandled = handleFmt21h(cUnit, mir);
3837 break;
3838 case kFmt21s:
3839 notHandled = handleFmt21s(cUnit, mir);
3840 break;
3841 case kFmt21t:
3842 notHandled = handleFmt21t(cUnit, mir, blockList[i],
3843 labelList);
3844 break;
3845 case kFmt22b:
3846 case kFmt22s:
3847 notHandled = handleFmt22b_Fmt22s(cUnit, mir);
3848 break;
3849 case kFmt22c:
3850 notHandled = handleFmt22c(cUnit, mir);
3851 break;
3852 case kFmt22cs:
3853 notHandled = handleFmt22cs(cUnit, mir);
3854 break;
3855 case kFmt22t:
3856 notHandled = handleFmt22t(cUnit, mir, blockList[i],
3857 labelList);
3858 break;
3859 case kFmt22x:
3860 case kFmt32x:
3861 notHandled = handleFmt22x_Fmt32x(cUnit, mir);
3862 break;
3863 case kFmt23x:
3864 notHandled = handleFmt23x(cUnit, mir);
3865 break;
3866 case kFmt31t:
3867 notHandled = handleFmt31t(cUnit, mir);
3868 break;
3869 case kFmt3rc:
3870 case kFmt35c:
3871 notHandled = handleFmt35c_3rc(cUnit, mir, blockList[i],
3872 labelList);
3873 break;
3874 case kFmt3rms:
3875 case kFmt35ms:
3876 notHandled = handleFmt35ms_3rms(cUnit, mir,blockList[i],
3877 labelList);
3878 break;
3879 case kFmt3inline:
Andy McFaddenb0a05412009-11-19 10:23:41 -08003880 case kFmt3rinline:
Bill Buzbeece46c942009-11-20 15:41:34 -08003881 notHandled = handleExecuteInline(cUnit, mir);
Andy McFaddenb0a05412009-11-19 10:23:41 -08003882 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07003883 case kFmt51l:
3884 notHandled = handleFmt51l(cUnit, mir);
3885 break;
3886 default:
3887 notHandled = true;
3888 break;
3889 }
3890 }
3891 if (notHandled) {
3892 LOGE("%#06x: Opcode 0x%x (%s) / Fmt %d not handled\n",
3893 mir->offset,
3894 dalvikOpCode, getOpcodeName(dalvikOpCode),
3895 dalvikFormat);
Bill Buzbeefc519dc2010-03-06 23:30:57 -08003896 dvmCompilerAbort(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003897 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07003898 }
3899 }
Ben Cheng4238ec22009-08-24 16:32:22 -07003900
Bill Buzbee1465db52009-09-23 17:17:35 -07003901 if (blockList[i]->blockType == kEntryBlock) {
Ben Cheng4238ec22009-08-24 16:32:22 -07003902 dvmCompilerAppendLIR(cUnit,
3903 (LIR *) cUnit->loopAnalysis->branchToBody);
3904 dvmCompilerAppendLIR(cUnit,
3905 (LIR *) cUnit->loopAnalysis->branchToPCR);
3906 }
3907
3908 if (headLIR) {
3909 /*
3910 * Eliminate redundant loads/stores and delay stores into later
3911 * slots
3912 */
3913 dvmCompilerApplyLocalOptimizations(cUnit, (LIR *) headLIR,
3914 cUnit->lastLIRInsn);
3915 }
3916
3917gen_fallthrough:
Ben Cheng1efc9c52009-06-08 18:25:27 -07003918 /*
3919 * Check if the block is terminated due to trace length constraint -
3920 * insert an unconditional branch to the chaining cell.
3921 */
3922 if (blockList[i]->needFallThroughBranch) {
3923 genUnconditionalBranch(cUnit,
3924 &labelList[blockList[i]->fallThrough->id]);
3925 }
3926
Ben Chengba4fc8b2009-06-01 13:00:29 -07003927 }
3928
Ben Chenge9695e52009-06-16 16:11:47 -07003929 /* Handle the chaining cells in predefined order */
Ben Chengcec26f62010-01-15 15:29:33 -08003930 for (i = 0; i < kChainingCellGap; i++) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07003931 size_t j;
3932 int *blockIdList = (int *) chainingListByType[i].elemList;
3933
3934 cUnit->numChainingCells[i] = chainingListByType[i].numUsed;
3935
3936 /* No chaining cells of this type */
3937 if (cUnit->numChainingCells[i] == 0)
3938 continue;
3939
3940 /* Record the first LIR for a new type of chaining cell */
3941 cUnit->firstChainingLIR[i] = (LIR *) &labelList[blockIdList[0]];
3942
3943 for (j = 0; j < chainingListByType[i].numUsed; j++) {
3944 int blockId = blockIdList[j];
3945
3946 /* Align this chaining cell first */
Bill Buzbee1465db52009-09-23 17:17:35 -07003947 newLIR0(cUnit, kArmPseudoPseudoAlign4);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003948
3949 /* Insert the pseudo chaining instruction */
3950 dvmCompilerAppendLIR(cUnit, (LIR *) &labelList[blockId]);
3951
3952
3953 switch (blockList[blockId]->blockType) {
Bill Buzbee1465db52009-09-23 17:17:35 -07003954 case kChainingCellNormal:
Ben Cheng1efc9c52009-06-08 18:25:27 -07003955 handleNormalChainingCell(cUnit,
Ben Chengba4fc8b2009-06-01 13:00:29 -07003956 blockList[blockId]->startOffset);
3957 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07003958 case kChainingCellInvokeSingleton:
Ben Cheng38329f52009-07-07 14:19:20 -07003959 handleInvokeSingletonChainingCell(cUnit,
Ben Chengba4fc8b2009-06-01 13:00:29 -07003960 blockList[blockId]->containingMethod);
3961 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07003962 case kChainingCellInvokePredicted:
Ben Cheng38329f52009-07-07 14:19:20 -07003963 handleInvokePredictedChainingCell(cUnit);
3964 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07003965 case kChainingCellHot:
Ben Cheng1efc9c52009-06-08 18:25:27 -07003966 handleHotChainingCell(cUnit,
Ben Chengba4fc8b2009-06-01 13:00:29 -07003967 blockList[blockId]->startOffset);
3968 break;
Bill Buzbee9c4b7c82009-09-10 10:10:38 -07003969#if defined(WITH_SELF_VERIFICATION) || defined(WITH_JIT_TUNING)
Bill Buzbee1465db52009-09-23 17:17:35 -07003970 case kChainingCellBackwardBranch:
Jeff Hao97319a82009-08-12 16:57:15 -07003971 handleBackwardBranchChainingCell(cUnit,
3972 blockList[blockId]->startOffset);
3973 break;
3974#endif
Ben Chengba4fc8b2009-06-01 13:00:29 -07003975 default:
Bill Buzbee1465db52009-09-23 17:17:35 -07003976 LOGE("Bad blocktype %d", blockList[blockId]->blockType);
Bill Buzbeefc519dc2010-03-06 23:30:57 -08003977 dvmCompilerAbort(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003978 }
3979 }
3980 }
Ben Chenge9695e52009-06-16 16:11:47 -07003981
Ben Chengcec26f62010-01-15 15:29:33 -08003982 /* Mark the bottom of chaining cells */
3983 cUnit->chainingCellBottom = (LIR *) newLIR0(cUnit, kArmChainingCellBottom);
3984
Ben Cheng6c10a972009-10-29 14:39:18 -07003985 /*
3986 * Generate the branch to the dvmJitToInterpNoChain entry point at the end
3987 * of all chaining cells for the overflow cases.
3988 */
3989 if (cUnit->switchOverflowPad) {
3990 loadConstant(cUnit, r0, (int) cUnit->switchOverflowPad);
3991 loadWordDisp(cUnit, rGLUE, offsetof(InterpState,
3992 jitToInterpEntries.dvmJitToInterpNoChain), r2);
3993 opRegReg(cUnit, kOpAdd, r1, r1);
3994 opRegRegReg(cUnit, kOpAdd, r4PC, r0, r1);
Ben Cheng978738d2010-05-13 13:45:57 -07003995#if defined(WITH_JIT_TUNING)
Ben Cheng6c10a972009-10-29 14:39:18 -07003996 loadConstant(cUnit, r0, kSwitchOverflow);
3997#endif
3998 opReg(cUnit, kOpBlx, r2);
3999 }
4000
Ben Chenge9695e52009-06-16 16:11:47 -07004001 dvmCompilerApplyGlobalOptimizations(cUnit);
jeffhao9e45c0b2010-02-03 10:24:05 -08004002
4003#if defined(WITH_SELF_VERIFICATION)
4004 selfVerificationBranchInsertPass(cUnit);
4005#endif
Ben Chengba4fc8b2009-06-01 13:00:29 -07004006}
4007
4008/* Accept the work and start compiling */
Bill Buzbee716f1202009-07-23 13:22:09 -07004009bool dvmCompilerDoWork(CompilerWorkOrder *work)
Ben Chengba4fc8b2009-06-01 13:00:29 -07004010{
Ben Chengccd6c012009-10-15 14:52:45 -07004011 bool res;
Ben Chengba4fc8b2009-06-01 13:00:29 -07004012
Ben Cheng6999d842010-01-26 16:46:15 -08004013 if (gDvmJit.codeCacheFull) {
Ben Chengccd6c012009-10-15 14:52:45 -07004014 return false;
4015 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07004016
Ben Chengccd6c012009-10-15 14:52:45 -07004017 switch (work->kind) {
4018 case kWorkOrderMethod:
4019 res = dvmCompileMethod(work->info, &work->result);
4020 break;
4021 case kWorkOrderTrace:
4022 /* Start compilation with maximally allowed trace length */
Bill Buzbeefc519dc2010-03-06 23:30:57 -08004023 res = dvmCompileTrace(work->info, JIT_MAX_TRACE_LEN, &work->result,
4024 work->bailPtr);
Ben Chengccd6c012009-10-15 14:52:45 -07004025 break;
4026 case kWorkOrderTraceDebug: {
4027 bool oldPrintMe = gDvmJit.printMe;
4028 gDvmJit.printMe = true;
4029 /* Start compilation with maximally allowed trace length */
Bill Buzbeefc519dc2010-03-06 23:30:57 -08004030 res = dvmCompileTrace(work->info, JIT_MAX_TRACE_LEN, &work->result,
4031 work->bailPtr);
Elliott Hughes672511b2010-04-26 17:40:13 -07004032 gDvmJit.printMe = oldPrintMe;
Ben Chengccd6c012009-10-15 14:52:45 -07004033 break;
4034 }
4035 default:
4036 res = false;
Bill Buzbeefc519dc2010-03-06 23:30:57 -08004037 LOGE("Jit: unknown work order type");
Elliott Hughes672511b2010-04-26 17:40:13 -07004038 assert(0); // Bail if debug build, discard otherwise
Ben Chengccd6c012009-10-15 14:52:45 -07004039 }
4040 return res;
Ben Chengba4fc8b2009-06-01 13:00:29 -07004041}
4042
Ben Chengba4fc8b2009-06-01 13:00:29 -07004043/* Architectural-specific debugging helpers go here */
4044void dvmCompilerArchDump(void)
4045{
4046 /* Print compiled opcode in this VM instance */
4047 int i, start, streak;
4048 char buf[1024];
4049
4050 streak = i = 0;
4051 buf[0] = 0;
4052 while (opcodeCoverage[i] == 0 && i < 256) {
4053 i++;
4054 }
4055 if (i == 256) {
4056 return;
4057 }
4058 for (start = i++, streak = 1; i < 256; i++) {
4059 if (opcodeCoverage[i]) {
4060 streak++;
4061 } else {
4062 if (streak == 1) {
4063 sprintf(buf+strlen(buf), "%x,", start);
4064 } else {
4065 sprintf(buf+strlen(buf), "%x-%x,", start, start + streak - 1);
4066 }
4067 streak = 0;
4068 while (opcodeCoverage[i] == 0 && i < 256) {
4069 i++;
4070 }
4071 if (i < 256) {
4072 streak = 1;
4073 start = i;
4074 }
4075 }
4076 }
4077 if (streak) {
4078 if (streak == 1) {
4079 sprintf(buf+strlen(buf), "%x", start);
4080 } else {
4081 sprintf(buf+strlen(buf), "%x-%x", start, start + streak - 1);
4082 }
4083 }
4084 if (strlen(buf)) {
Ben Cheng8b258bf2009-06-24 17:27:07 -07004085 LOGD("dalvik.vm.jit.op = %s", buf);
Ben Chengba4fc8b2009-06-01 13:00:29 -07004086 }
4087}
Ben Chengd7d426a2009-09-22 11:23:36 -07004088
4089/* Common initialization routine for an architecture family */
4090bool dvmCompilerArchInit()
4091{
4092 int i;
4093
Bill Buzbee1465db52009-09-23 17:17:35 -07004094 for (i = 0; i < kArmLast; i++) {
Ben Chengd7d426a2009-09-22 11:23:36 -07004095 if (EncodingMap[i].opCode != i) {
4096 LOGE("Encoding order for %s is wrong: expecting %d, seeing %d",
4097 EncodingMap[i].name, i, EncodingMap[i].opCode);
Bill Buzbeefc519dc2010-03-06 23:30:57 -08004098 dvmAbort(); // OK to dvmAbort - build error
Ben Chengd7d426a2009-09-22 11:23:36 -07004099 }
4100 }
4101
Ben Cheng5d90c202009-11-22 23:31:11 -08004102 return dvmCompilerArchVariantInit();
4103}
4104
4105void *dvmCompilerGetInterpretTemplate()
4106{
4107 return (void*) ((int)gDvmJit.codeCache +
4108 templateEntryOffsets[TEMPLATE_INTERPRET]);
4109}
4110
4111/* Needed by the ld/st optmizatons */
4112ArmLIR* dvmCompilerRegCopyNoInsert(CompilationUnit *cUnit, int rDest, int rSrc)
4113{
4114 return genRegCopyNoInsert(cUnit, rDest, rSrc);
4115}
4116
4117/* Needed by the register allocator */
4118ArmLIR* dvmCompilerRegCopy(CompilationUnit *cUnit, int rDest, int rSrc)
4119{
4120 return genRegCopy(cUnit, rDest, rSrc);
4121}
4122
4123/* Needed by the register allocator */
4124void dvmCompilerRegCopyWide(CompilationUnit *cUnit, int destLo, int destHi,
4125 int srcLo, int srcHi)
4126{
4127 genRegCopyWide(cUnit, destLo, destHi, srcLo, srcHi);
4128}
4129
4130void dvmCompilerFlushRegImpl(CompilationUnit *cUnit, int rBase,
4131 int displacement, int rSrc, OpSize size)
4132{
4133 storeBaseDisp(cUnit, rBase, displacement, rSrc, size);
4134}
4135
4136void dvmCompilerFlushRegWideImpl(CompilationUnit *cUnit, int rBase,
4137 int displacement, int rSrcLo, int rSrcHi)
4138{
4139 storeBaseDispWide(cUnit, rBase, displacement, rSrcLo, rSrcHi);
Ben Chengd7d426a2009-09-22 11:23:36 -07004140}