blob: 03fc2af0f81c1777baf1e18f8650657fd56404df [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 Buzbee749e8162010-07-07 06:55:56 -0700288 RegisterClass regClass = dvmCompilerRegClassBySize(size);
Bill Buzbeec6f10662010-02-09 11:16:15 -0800289 RegLocation rlObj = dvmCompilerGetSrc(cUnit, mir, 0);
290 RegLocation rlDest = dvmCompilerGetDest(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -0700291 rlObj = loadValue(cUnit, rlObj, kCoreReg);
Bill Buzbee749e8162010-07-07 06:55:56 -0700292 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, regClass, true);
Bill Buzbee1465db52009-09-23 17:17:35 -0700293 genNullCheck(cUnit, rlObj.sRegLow, rlObj.lowReg, mir->offset,
294 NULL);/* null object? */
Ben Cheng11d8f142010-03-24 15:24:19 -0700295
296 HEAP_ACCESS_SHADOW(true);
Ben Cheng5d90c202009-11-22 23:31:11 -0800297 loadBaseDisp(cUnit, mir, rlObj.lowReg, fieldOffset, rlResult.lowReg,
298 size, rlObj.sRegLow);
Ben Cheng11d8f142010-03-24 15:24:19 -0700299 HEAP_ACCESS_SHADOW(false);
300
Bill Buzbee1465db52009-09-23 17:17:35 -0700301 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700302}
303
304/*
305 * Store a field to an object instance
306 *
Ben Chengba4fc8b2009-06-01 13:00:29 -0700307 */
Bill Buzbee270c1d62009-08-13 16:58:07 -0700308static void genIPut(CompilationUnit *cUnit, MIR *mir, OpSize size,
Ben Chengba4fc8b2009-06-01 13:00:29 -0700309 int fieldOffset)
310{
Bill Buzbee749e8162010-07-07 06:55:56 -0700311 RegisterClass regClass = dvmCompilerRegClassBySize(size);
Bill Buzbeec6f10662010-02-09 11:16:15 -0800312 RegLocation rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
313 RegLocation rlObj = dvmCompilerGetSrc(cUnit, mir, 1);
Bill Buzbee1465db52009-09-23 17:17:35 -0700314 rlObj = loadValue(cUnit, rlObj, kCoreReg);
Bill Buzbee749e8162010-07-07 06:55:56 -0700315 rlSrc = loadValue(cUnit, rlSrc, regClass);
Bill Buzbee1465db52009-09-23 17:17:35 -0700316 genNullCheck(cUnit, rlObj.sRegLow, rlObj.lowReg, mir->offset,
317 NULL);/* null object? */
Ben Cheng11d8f142010-03-24 15:24:19 -0700318
319 HEAP_ACCESS_SHADOW(true);
Bill Buzbee1465db52009-09-23 17:17:35 -0700320 storeBaseDisp(cUnit, rlObj.lowReg, fieldOffset, rlSrc.lowReg, size);
Ben Cheng11d8f142010-03-24 15:24:19 -0700321 HEAP_ACCESS_SHADOW(false);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700322}
323
324
Ben Chengba4fc8b2009-06-01 13:00:29 -0700325/*
326 * Generate array load
Ben Chengba4fc8b2009-06-01 13:00:29 -0700327 */
Bill Buzbee270c1d62009-08-13 16:58:07 -0700328static void genArrayGet(CompilationUnit *cUnit, MIR *mir, OpSize size,
Bill Buzbee1465db52009-09-23 17:17:35 -0700329 RegLocation rlArray, RegLocation rlIndex,
330 RegLocation rlDest, int scale)
Ben Chengba4fc8b2009-06-01 13:00:29 -0700331{
Bill Buzbee749e8162010-07-07 06:55:56 -0700332 RegisterClass regClass = dvmCompilerRegClassBySize(size);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700333 int lenOffset = offsetof(ArrayObject, length);
334 int dataOffset = offsetof(ArrayObject, contents);
Bill Buzbee1465db52009-09-23 17:17:35 -0700335 RegLocation rlResult;
336 rlArray = loadValue(cUnit, rlArray, kCoreReg);
337 rlIndex = loadValue(cUnit, rlIndex, kCoreReg);
338 int regPtr;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700339
340 /* null object? */
Ben Cheng4238ec22009-08-24 16:32:22 -0700341 ArmLIR * pcrLabel = NULL;
342
343 if (!(mir->OptimizationFlags & MIR_IGNORE_NULL_CHECK)) {
Bill Buzbee1465db52009-09-23 17:17:35 -0700344 pcrLabel = genNullCheck(cUnit, rlArray.sRegLow,
345 rlArray.lowReg, mir->offset, NULL);
Ben Cheng4238ec22009-08-24 16:32:22 -0700346 }
347
Bill Buzbeec6f10662010-02-09 11:16:15 -0800348 regPtr = dvmCompilerAllocTemp(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -0700349
Ben Cheng4238ec22009-08-24 16:32:22 -0700350 if (!(mir->OptimizationFlags & MIR_IGNORE_RANGE_CHECK)) {
Bill Buzbeec6f10662010-02-09 11:16:15 -0800351 int regLen = dvmCompilerAllocTemp(cUnit);
Ben Cheng4238ec22009-08-24 16:32:22 -0700352 /* Get len */
Bill Buzbee1465db52009-09-23 17:17:35 -0700353 loadWordDisp(cUnit, rlArray.lowReg, lenOffset, regLen);
354 /* regPtr -> array data */
355 opRegRegImm(cUnit, kOpAdd, regPtr, rlArray.lowReg, dataOffset);
356 genBoundsCheck(cUnit, rlIndex.lowReg, regLen, mir->offset,
357 pcrLabel);
Bill Buzbeec6f10662010-02-09 11:16:15 -0800358 dvmCompilerFreeTemp(cUnit, regLen);
Ben Cheng4238ec22009-08-24 16:32:22 -0700359 } else {
Bill Buzbee1465db52009-09-23 17:17:35 -0700360 /* regPtr -> array data */
361 opRegRegImm(cUnit, kOpAdd, regPtr, rlArray.lowReg, dataOffset);
Ben Cheng4238ec22009-08-24 16:32:22 -0700362 }
Bill Buzbee1465db52009-09-23 17:17:35 -0700363 if ((size == kLong) || (size == kDouble)) {
364 if (scale) {
Bill Buzbeec6f10662010-02-09 11:16:15 -0800365 int rNewIndex = dvmCompilerAllocTemp(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -0700366 opRegRegImm(cUnit, kOpLsl, rNewIndex, rlIndex.lowReg, scale);
367 opRegReg(cUnit, kOpAdd, regPtr, rNewIndex);
Bill Buzbeec6f10662010-02-09 11:16:15 -0800368 dvmCompilerFreeTemp(cUnit, rNewIndex);
Bill Buzbee1465db52009-09-23 17:17:35 -0700369 } else {
370 opRegReg(cUnit, kOpAdd, regPtr, rlIndex.lowReg);
371 }
Bill Buzbee749e8162010-07-07 06:55:56 -0700372 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, regClass, true);
Ben Cheng11d8f142010-03-24 15:24:19 -0700373
374 HEAP_ACCESS_SHADOW(true);
Bill Buzbee1465db52009-09-23 17:17:35 -0700375 loadPair(cUnit, regPtr, rlResult.lowReg, rlResult.highReg);
Ben Cheng11d8f142010-03-24 15:24:19 -0700376 HEAP_ACCESS_SHADOW(false);
377
Bill Buzbeec6f10662010-02-09 11:16:15 -0800378 dvmCompilerFreeTemp(cUnit, regPtr);
Bill Buzbee1465db52009-09-23 17:17:35 -0700379 storeValueWide(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700380 } else {
Bill Buzbee749e8162010-07-07 06:55:56 -0700381 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, regClass, true);
Ben Cheng11d8f142010-03-24 15:24:19 -0700382
383 HEAP_ACCESS_SHADOW(true);
Bill Buzbee1465db52009-09-23 17:17:35 -0700384 loadBaseIndexed(cUnit, regPtr, rlIndex.lowReg, rlResult.lowReg,
385 scale, size);
Ben Cheng11d8f142010-03-24 15:24:19 -0700386 HEAP_ACCESS_SHADOW(false);
387
Bill Buzbeec6f10662010-02-09 11:16:15 -0800388 dvmCompilerFreeTemp(cUnit, regPtr);
Bill Buzbee1465db52009-09-23 17:17:35 -0700389 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700390 }
391}
392
Ben Chengba4fc8b2009-06-01 13:00:29 -0700393/*
394 * Generate array store
395 *
Ben Chengba4fc8b2009-06-01 13:00:29 -0700396 */
Bill Buzbee270c1d62009-08-13 16:58:07 -0700397static void genArrayPut(CompilationUnit *cUnit, MIR *mir, OpSize size,
Bill Buzbee1465db52009-09-23 17:17:35 -0700398 RegLocation rlArray, RegLocation rlIndex,
399 RegLocation rlSrc, int scale)
Ben Chengba4fc8b2009-06-01 13:00:29 -0700400{
Bill Buzbee749e8162010-07-07 06:55:56 -0700401 RegisterClass regClass = dvmCompilerRegClassBySize(size);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700402 int lenOffset = offsetof(ArrayObject, length);
403 int dataOffset = offsetof(ArrayObject, contents);
404
Bill Buzbee1465db52009-09-23 17:17:35 -0700405 int regPtr;
406 rlArray = loadValue(cUnit, rlArray, kCoreReg);
407 rlIndex = loadValue(cUnit, rlIndex, kCoreReg);
Ben Chenge9695e52009-06-16 16:11:47 -0700408
Bill Buzbeec6f10662010-02-09 11:16:15 -0800409 if (dvmCompilerIsTemp(cUnit, rlArray.lowReg)) {
410 dvmCompilerClobber(cUnit, rlArray.lowReg);
Bill Buzbee1465db52009-09-23 17:17:35 -0700411 regPtr = rlArray.lowReg;
412 } else {
Bill Buzbeec6f10662010-02-09 11:16:15 -0800413 regPtr = dvmCompilerAllocTemp(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -0700414 genRegCopy(cUnit, regPtr, rlArray.lowReg);
415 }
Ben Chenge9695e52009-06-16 16:11:47 -0700416
Ben Cheng1efc9c52009-06-08 18:25:27 -0700417 /* null object? */
Ben Cheng4238ec22009-08-24 16:32:22 -0700418 ArmLIR * pcrLabel = NULL;
419
420 if (!(mir->OptimizationFlags & MIR_IGNORE_NULL_CHECK)) {
Bill Buzbee1465db52009-09-23 17:17:35 -0700421 pcrLabel = genNullCheck(cUnit, rlArray.sRegLow, rlArray.lowReg,
422 mir->offset, NULL);
Ben Cheng4238ec22009-08-24 16:32:22 -0700423 }
424
425 if (!(mir->OptimizationFlags & MIR_IGNORE_RANGE_CHECK)) {
Bill Buzbeec6f10662010-02-09 11:16:15 -0800426 int regLen = dvmCompilerAllocTemp(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -0700427 //NOTE: max live temps(4) here.
Ben Cheng4238ec22009-08-24 16:32:22 -0700428 /* Get len */
Bill Buzbee1465db52009-09-23 17:17:35 -0700429 loadWordDisp(cUnit, rlArray.lowReg, lenOffset, regLen);
430 /* regPtr -> array data */
431 opRegImm(cUnit, kOpAdd, regPtr, dataOffset);
432 genBoundsCheck(cUnit, rlIndex.lowReg, regLen, mir->offset,
433 pcrLabel);
Bill Buzbeec6f10662010-02-09 11:16:15 -0800434 dvmCompilerFreeTemp(cUnit, regLen);
Ben Cheng4238ec22009-08-24 16:32:22 -0700435 } else {
Bill Buzbee1465db52009-09-23 17:17:35 -0700436 /* regPtr -> array data */
437 opRegImm(cUnit, kOpAdd, regPtr, dataOffset);
Ben Cheng4238ec22009-08-24 16:32:22 -0700438 }
Bill Buzbee1465db52009-09-23 17:17:35 -0700439 /* at this point, regPtr points to array, 2 live temps */
Bill Buzbee1465db52009-09-23 17:17:35 -0700440 if ((size == kLong) || (size == kDouble)) {
441 //TODO: need specific wide routine that can handle fp regs
442 if (scale) {
Bill Buzbeec6f10662010-02-09 11:16:15 -0800443 int rNewIndex = dvmCompilerAllocTemp(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -0700444 opRegRegImm(cUnit, kOpLsl, rNewIndex, rlIndex.lowReg, scale);
445 opRegReg(cUnit, kOpAdd, regPtr, rNewIndex);
Bill Buzbeec6f10662010-02-09 11:16:15 -0800446 dvmCompilerFreeTemp(cUnit, rNewIndex);
Bill Buzbee1465db52009-09-23 17:17:35 -0700447 } else {
448 opRegReg(cUnit, kOpAdd, regPtr, rlIndex.lowReg);
449 }
Bill Buzbee749e8162010-07-07 06:55:56 -0700450 rlSrc = loadValueWide(cUnit, rlSrc, regClass);
Ben Cheng11d8f142010-03-24 15:24:19 -0700451
452 HEAP_ACCESS_SHADOW(true);
Bill Buzbee1465db52009-09-23 17:17:35 -0700453 storePair(cUnit, regPtr, rlSrc.lowReg, rlSrc.highReg);
Ben Cheng11d8f142010-03-24 15:24:19 -0700454 HEAP_ACCESS_SHADOW(false);
455
Bill Buzbeec6f10662010-02-09 11:16:15 -0800456 dvmCompilerFreeTemp(cUnit, regPtr);
Bill Buzbee270c1d62009-08-13 16:58:07 -0700457 } else {
Bill Buzbee749e8162010-07-07 06:55:56 -0700458 rlSrc = loadValue(cUnit, rlSrc, regClass);
Ben Cheng11d8f142010-03-24 15:24:19 -0700459
460 HEAP_ACCESS_SHADOW(true);
Bill Buzbee1465db52009-09-23 17:17:35 -0700461 storeBaseIndexed(cUnit, regPtr, rlIndex.lowReg, rlSrc.lowReg,
462 scale, size);
Ben Cheng11d8f142010-03-24 15:24:19 -0700463 HEAP_ACCESS_SHADOW(false);
jeffhao9e45c0b2010-02-03 10:24:05 -0800464 }
Ben Chengba4fc8b2009-06-01 13:00:29 -0700465}
466
Bill Buzbeebe6534f2010-03-12 16:01:35 -0800467/*
468 * Generate array object store
469 * Must use explicit register allocation here because of
470 * call-out to dvmCanPutArrayElement
471 */
472static void genArrayObjectPut(CompilationUnit *cUnit, MIR *mir,
473 RegLocation rlArray, RegLocation rlIndex,
474 RegLocation rlSrc, int scale)
475{
476 int lenOffset = offsetof(ArrayObject, length);
477 int dataOffset = offsetof(ArrayObject, contents);
478
479 dvmCompilerFlushAllRegs(cUnit);
480
481 int regLen = r0;
482 int regPtr = r4PC; /* Preserved across call */
483 int regArray = r1;
484 int regIndex = r7; /* Preserved across call */
485
486 loadValueDirectFixed(cUnit, rlArray, regArray);
487 loadValueDirectFixed(cUnit, rlIndex, regIndex);
488
489 /* null object? */
490 ArmLIR * pcrLabel = NULL;
491
492 if (!(mir->OptimizationFlags & MIR_IGNORE_NULL_CHECK)) {
493 pcrLabel = genNullCheck(cUnit, rlArray.sRegLow, regArray,
494 mir->offset, NULL);
495 }
496
497 if (!(mir->OptimizationFlags & MIR_IGNORE_RANGE_CHECK)) {
498 /* Get len */
499 loadWordDisp(cUnit, regArray, lenOffset, regLen);
500 /* regPtr -> array data */
501 opRegRegImm(cUnit, kOpAdd, regPtr, regArray, dataOffset);
502 genBoundsCheck(cUnit, regIndex, regLen, mir->offset,
503 pcrLabel);
504 } else {
505 /* regPtr -> array data */
506 opRegRegImm(cUnit, kOpAdd, regPtr, regArray, dataOffset);
507 }
508
509 /* Get object to store */
510 loadValueDirectFixed(cUnit, rlSrc, r0);
Ben Chengbd1326d2010-04-02 15:04:53 -0700511 LOAD_FUNC_ADDR(cUnit, r2, (int)dvmCanPutArrayElement);
Bill Buzbeebe6534f2010-03-12 16:01:35 -0800512
513 /* Are we storing null? If so, avoid check */
514 opRegImm(cUnit, kOpCmp, r0, 0);
515 ArmLIR *branchOver = opCondBranch(cUnit, kArmCondEq);
516
517 /* Make sure the types are compatible */
518 loadWordDisp(cUnit, regArray, offsetof(Object, clazz), r1);
519 loadWordDisp(cUnit, r0, offsetof(Object, clazz), r0);
520 opReg(cUnit, kOpBlx, r2);
521 dvmCompilerClobberCallRegs(cUnit);
Bill Buzbee900a3af2010-03-16 12:41:43 -0700522
523 /*
524 * Using fixed registers here, and counting on r4 and r7 being
525 * preserved across the above call. Tell the register allocation
526 * utilities about the regs we are using directly
527 */
528 dvmCompilerLockTemp(cUnit, regPtr); // r4PC
529 dvmCompilerLockTemp(cUnit, regIndex); // r7
530 dvmCompilerLockTemp(cUnit, r0);
531
Bill Buzbeebe6534f2010-03-12 16:01:35 -0800532 /* Bad? - roll back and re-execute if so */
533 genRegImmCheck(cUnit, kArmCondEq, r0, 0, mir->offset, pcrLabel);
534
535 /* Resume here - must reload element, regPtr & index preserved */
536 loadValueDirectFixed(cUnit, rlSrc, r0);
537
538 ArmLIR *target = newLIR0(cUnit, kArmPseudoTargetLabel);
539 target->defMask = ENCODE_ALL;
540 branchOver->generic.target = (LIR *) target;
541
Ben Cheng11d8f142010-03-24 15:24:19 -0700542 HEAP_ACCESS_SHADOW(true);
Bill Buzbeebe6534f2010-03-12 16:01:35 -0800543 storeBaseIndexed(cUnit, regPtr, regIndex, r0,
544 scale, kWord);
Ben Cheng11d8f142010-03-24 15:24:19 -0700545 HEAP_ACCESS_SHADOW(false);
Bill Buzbeebe6534f2010-03-12 16:01:35 -0800546}
547
Ben Cheng5d90c202009-11-22 23:31:11 -0800548static bool genShiftOpLong(CompilationUnit *cUnit, MIR *mir,
549 RegLocation rlDest, RegLocation rlSrc1,
550 RegLocation rlShift)
Ben Chengba4fc8b2009-06-01 13:00:29 -0700551{
Ben Chenge9695e52009-06-16 16:11:47 -0700552 /*
553 * Don't mess with the regsiters here as there is a particular calling
554 * convention to the out-of-line handler.
555 */
Bill Buzbee1465db52009-09-23 17:17:35 -0700556 RegLocation rlResult;
557
558 loadValueDirectWideFixed(cUnit, rlSrc1, r0, r1);
559 loadValueDirect(cUnit, rlShift, r2);
Ben Chenge9695e52009-06-16 16:11:47 -0700560 switch( mir->dalvikInsn.opCode) {
561 case OP_SHL_LONG:
562 case OP_SHL_LONG_2ADDR:
563 genDispatchToHandler(cUnit, TEMPLATE_SHL_LONG);
564 break;
565 case OP_SHR_LONG:
566 case OP_SHR_LONG_2ADDR:
567 genDispatchToHandler(cUnit, TEMPLATE_SHR_LONG);
568 break;
569 case OP_USHR_LONG:
570 case OP_USHR_LONG_2ADDR:
571 genDispatchToHandler(cUnit, TEMPLATE_USHR_LONG);
572 break;
573 default:
574 return true;
575 }
Bill Buzbeec6f10662010-02-09 11:16:15 -0800576 rlResult = dvmCompilerGetReturnWide(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -0700577 storeValueWide(cUnit, rlDest, rlResult);
Ben Chenge9695e52009-06-16 16:11:47 -0700578 return false;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700579}
Ben Chenge9695e52009-06-16 16:11:47 -0700580
Ben Cheng5d90c202009-11-22 23:31:11 -0800581static bool genArithOpLong(CompilationUnit *cUnit, MIR *mir,
582 RegLocation rlDest, RegLocation rlSrc1,
583 RegLocation rlSrc2)
Ben Chengba4fc8b2009-06-01 13:00:29 -0700584{
Bill Buzbee1465db52009-09-23 17:17:35 -0700585 RegLocation rlResult;
586 OpKind firstOp = kOpBkpt;
587 OpKind secondOp = kOpBkpt;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700588 bool callOut = false;
589 void *callTgt;
590 int retReg = r0;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700591
592 switch (mir->dalvikInsn.opCode) {
593 case OP_NOT_LONG:
Bill Buzbee1465db52009-09-23 17:17:35 -0700594 rlSrc2 = loadValueWide(cUnit, rlSrc2, kCoreReg);
Bill Buzbeec6f10662010-02-09 11:16:15 -0800595 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Bill Buzbee1465db52009-09-23 17:17:35 -0700596 opRegReg(cUnit, kOpMvn, rlResult.lowReg, rlSrc2.lowReg);
597 opRegReg(cUnit, kOpMvn, rlResult.highReg, rlSrc2.highReg);
598 storeValueWide(cUnit, rlDest, rlResult);
599 return false;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700600 break;
601 case OP_ADD_LONG:
602 case OP_ADD_LONG_2ADDR:
Bill Buzbee1465db52009-09-23 17:17:35 -0700603 firstOp = kOpAdd;
604 secondOp = kOpAdc;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700605 break;
606 case OP_SUB_LONG:
607 case OP_SUB_LONG_2ADDR:
Bill Buzbee1465db52009-09-23 17:17:35 -0700608 firstOp = kOpSub;
609 secondOp = kOpSbc;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700610 break;
611 case OP_MUL_LONG:
612 case OP_MUL_LONG_2ADDR:
Bill Buzbee1465db52009-09-23 17:17:35 -0700613 genMulLong(cUnit, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700614 return false;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700615 case OP_DIV_LONG:
616 case OP_DIV_LONG_2ADDR:
617 callOut = true;
618 retReg = r0;
619 callTgt = (void*)__aeabi_ldivmod;
620 break;
621 /* NOTE - result is in r2/r3 instead of r0/r1 */
622 case OP_REM_LONG:
623 case OP_REM_LONG_2ADDR:
624 callOut = true;
625 callTgt = (void*)__aeabi_ldivmod;
626 retReg = r2;
627 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700628 case OP_AND_LONG_2ADDR:
Bill Buzbee1465db52009-09-23 17:17:35 -0700629 case OP_AND_LONG:
630 firstOp = kOpAnd;
631 secondOp = kOpAnd;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700632 break;
633 case OP_OR_LONG:
634 case OP_OR_LONG_2ADDR:
Bill Buzbee1465db52009-09-23 17:17:35 -0700635 firstOp = kOpOr;
636 secondOp = kOpOr;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700637 break;
638 case OP_XOR_LONG:
639 case OP_XOR_LONG_2ADDR:
Bill Buzbee1465db52009-09-23 17:17:35 -0700640 firstOp = kOpXor;
641 secondOp = kOpXor;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700642 break;
Ben Chenge9695e52009-06-16 16:11:47 -0700643 case OP_NEG_LONG: {
Bill Buzbee51ecf602010-01-14 14:27:52 -0800644 //TUNING: can improve this using Thumb2 code
Bill Buzbeec6f10662010-02-09 11:16:15 -0800645 int tReg = dvmCompilerAllocTemp(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -0700646 rlSrc2 = loadValueWide(cUnit, rlSrc2, kCoreReg);
Bill Buzbeec6f10662010-02-09 11:16:15 -0800647 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Ben Chengbd1326d2010-04-02 15:04:53 -0700648 loadConstantNoClobber(cUnit, tReg, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -0700649 opRegRegReg(cUnit, kOpSub, rlResult.lowReg,
Bill Buzbee51ecf602010-01-14 14:27:52 -0800650 tReg, rlSrc2.lowReg);
651 opRegReg(cUnit, kOpSbc, tReg, rlSrc2.highReg);
652 genRegCopy(cUnit, rlResult.highReg, tReg);
Bill Buzbee1465db52009-09-23 17:17:35 -0700653 storeValueWide(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700654 return false;
Ben Chenge9695e52009-06-16 16:11:47 -0700655 }
Ben Chengba4fc8b2009-06-01 13:00:29 -0700656 default:
657 LOGE("Invalid long arith op");
Bill Buzbeefc519dc2010-03-06 23:30:57 -0800658 dvmCompilerAbort(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700659 }
660 if (!callOut) {
Bill Buzbee80cef862010-03-25 10:38:34 -0700661 genLong3Addr(cUnit, mir, firstOp, secondOp, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700662 } else {
Bill Buzbee1465db52009-09-23 17:17:35 -0700663 // Adjust return regs in to handle case of rem returning r2/r3
Bill Buzbeec6f10662010-02-09 11:16:15 -0800664 dvmCompilerFlushAllRegs(cUnit); /* Send everything to home location */
Bill Buzbee1465db52009-09-23 17:17:35 -0700665 loadValueDirectWideFixed(cUnit, rlSrc1, r0, r1);
Ben Chengbd1326d2010-04-02 15:04:53 -0700666 LOAD_FUNC_ADDR(cUnit, rlr, (int) callTgt);
Bill Buzbee1465db52009-09-23 17:17:35 -0700667 loadValueDirectWideFixed(cUnit, rlSrc2, r2, r3);
668 opReg(cUnit, kOpBlx, rlr);
Elliott Hughes6a555132010-02-25 15:41:42 -0800669 dvmCompilerClobberCallRegs(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -0700670 if (retReg == r0)
Bill Buzbeec6f10662010-02-09 11:16:15 -0800671 rlResult = dvmCompilerGetReturnWide(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -0700672 else
Bill Buzbeec6f10662010-02-09 11:16:15 -0800673 rlResult = dvmCompilerGetReturnWideAlt(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -0700674 storeValueWide(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700675 }
676 return false;
677}
678
Ben Cheng5d90c202009-11-22 23:31:11 -0800679static bool genArithOpInt(CompilationUnit *cUnit, MIR *mir,
680 RegLocation rlDest, RegLocation rlSrc1,
681 RegLocation rlSrc2)
Ben Chengba4fc8b2009-06-01 13:00:29 -0700682{
Bill Buzbee1465db52009-09-23 17:17:35 -0700683 OpKind op = kOpBkpt;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700684 bool callOut = false;
685 bool checkZero = false;
Bill Buzbee1465db52009-09-23 17:17:35 -0700686 bool unary = false;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700687 int retReg = r0;
688 void *callTgt;
Bill Buzbee1465db52009-09-23 17:17:35 -0700689 RegLocation rlResult;
Bill Buzbee0e605272009-12-01 14:28:05 -0800690 bool shiftOp = false;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700691
Ben Chengba4fc8b2009-06-01 13:00:29 -0700692 switch (mir->dalvikInsn.opCode) {
693 case OP_NEG_INT:
Bill Buzbee1465db52009-09-23 17:17:35 -0700694 op = kOpNeg;
695 unary = true;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700696 break;
697 case OP_NOT_INT:
Bill Buzbee1465db52009-09-23 17:17:35 -0700698 op = kOpMvn;
699 unary = true;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700700 break;
701 case OP_ADD_INT:
702 case OP_ADD_INT_2ADDR:
Bill Buzbee1465db52009-09-23 17:17:35 -0700703 op = kOpAdd;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700704 break;
705 case OP_SUB_INT:
706 case OP_SUB_INT_2ADDR:
Bill Buzbee1465db52009-09-23 17:17:35 -0700707 op = kOpSub;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700708 break;
709 case OP_MUL_INT:
710 case OP_MUL_INT_2ADDR:
Bill Buzbee1465db52009-09-23 17:17:35 -0700711 op = kOpMul;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700712 break;
713 case OP_DIV_INT:
714 case OP_DIV_INT_2ADDR:
715 callOut = true;
716 checkZero = true;
717 callTgt = __aeabi_idiv;
718 retReg = r0;
719 break;
720 /* NOTE: returns in r1 */
721 case OP_REM_INT:
722 case OP_REM_INT_2ADDR:
723 callOut = true;
724 checkZero = true;
725 callTgt = __aeabi_idivmod;
726 retReg = r1;
727 break;
728 case OP_AND_INT:
729 case OP_AND_INT_2ADDR:
Bill Buzbee1465db52009-09-23 17:17:35 -0700730 op = kOpAnd;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700731 break;
732 case OP_OR_INT:
733 case OP_OR_INT_2ADDR:
Bill Buzbee1465db52009-09-23 17:17:35 -0700734 op = kOpOr;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700735 break;
736 case OP_XOR_INT:
737 case OP_XOR_INT_2ADDR:
Bill Buzbee1465db52009-09-23 17:17:35 -0700738 op = kOpXor;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700739 break;
740 case OP_SHL_INT:
741 case OP_SHL_INT_2ADDR:
Bill Buzbee0e605272009-12-01 14:28:05 -0800742 shiftOp = true;
Bill Buzbee1465db52009-09-23 17:17:35 -0700743 op = kOpLsl;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700744 break;
745 case OP_SHR_INT:
746 case OP_SHR_INT_2ADDR:
Bill Buzbee0e605272009-12-01 14:28:05 -0800747 shiftOp = true;
Bill Buzbee1465db52009-09-23 17:17:35 -0700748 op = kOpAsr;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700749 break;
750 case OP_USHR_INT:
751 case OP_USHR_INT_2ADDR:
Bill Buzbee0e605272009-12-01 14:28:05 -0800752 shiftOp = true;
Bill Buzbee1465db52009-09-23 17:17:35 -0700753 op = kOpLsr;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700754 break;
755 default:
756 LOGE("Invalid word arith op: 0x%x(%d)",
757 mir->dalvikInsn.opCode, mir->dalvikInsn.opCode);
Bill Buzbeefc519dc2010-03-06 23:30:57 -0800758 dvmCompilerAbort(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700759 }
760 if (!callOut) {
Bill Buzbee1465db52009-09-23 17:17:35 -0700761 rlSrc1 = loadValue(cUnit, rlSrc1, kCoreReg);
762 if (unary) {
Bill Buzbeec6f10662010-02-09 11:16:15 -0800763 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Bill Buzbee1465db52009-09-23 17:17:35 -0700764 opRegReg(cUnit, op, rlResult.lowReg,
765 rlSrc1.lowReg);
Ben Chenge9695e52009-06-16 16:11:47 -0700766 } else {
Bill Buzbee1465db52009-09-23 17:17:35 -0700767 rlSrc2 = loadValue(cUnit, rlSrc2, kCoreReg);
Bill Buzbee0e605272009-12-01 14:28:05 -0800768 if (shiftOp) {
Bill Buzbeec6f10662010-02-09 11:16:15 -0800769 int tReg = dvmCompilerAllocTemp(cUnit);
Bill Buzbee0e605272009-12-01 14:28:05 -0800770 opRegRegImm(cUnit, kOpAnd, tReg, rlSrc2.lowReg, 31);
Bill Buzbeec6f10662010-02-09 11:16:15 -0800771 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Bill Buzbee0e605272009-12-01 14:28:05 -0800772 opRegRegReg(cUnit, op, rlResult.lowReg,
773 rlSrc1.lowReg, tReg);
Bill Buzbeec6f10662010-02-09 11:16:15 -0800774 dvmCompilerFreeTemp(cUnit, tReg);
Bill Buzbee0e605272009-12-01 14:28:05 -0800775 } else {
Bill Buzbeec6f10662010-02-09 11:16:15 -0800776 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Bill Buzbee0e605272009-12-01 14:28:05 -0800777 opRegRegReg(cUnit, op, rlResult.lowReg,
778 rlSrc1.lowReg, rlSrc2.lowReg);
779 }
Ben Chenge9695e52009-06-16 16:11:47 -0700780 }
Bill Buzbee1465db52009-09-23 17:17:35 -0700781 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700782 } else {
Bill Buzbee1465db52009-09-23 17:17:35 -0700783 RegLocation rlResult;
Bill Buzbeec6f10662010-02-09 11:16:15 -0800784 dvmCompilerFlushAllRegs(cUnit); /* Send everything to home location */
Bill Buzbee1465db52009-09-23 17:17:35 -0700785 loadValueDirectFixed(cUnit, rlSrc2, r1);
Ben Chengbd1326d2010-04-02 15:04:53 -0700786 LOAD_FUNC_ADDR(cUnit, r2, (int) callTgt);
Bill Buzbee1465db52009-09-23 17:17:35 -0700787 loadValueDirectFixed(cUnit, rlSrc1, r0);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700788 if (checkZero) {
Bill Buzbee1465db52009-09-23 17:17:35 -0700789 genNullCheck(cUnit, rlSrc2.sRegLow, r1, mir->offset, NULL);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700790 }
Bill Buzbee1465db52009-09-23 17:17:35 -0700791 opReg(cUnit, kOpBlx, r2);
Elliott Hughes6a555132010-02-25 15:41:42 -0800792 dvmCompilerClobberCallRegs(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -0700793 if (retReg == r0)
Bill Buzbeec6f10662010-02-09 11:16:15 -0800794 rlResult = dvmCompilerGetReturn(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -0700795 else
Bill Buzbeec6f10662010-02-09 11:16:15 -0800796 rlResult = dvmCompilerGetReturnAlt(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -0700797 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700798 }
799 return false;
800}
801
Ben Cheng5d90c202009-11-22 23:31:11 -0800802static bool genArithOp(CompilationUnit *cUnit, MIR *mir)
Ben Chengba4fc8b2009-06-01 13:00:29 -0700803{
804 OpCode opCode = mir->dalvikInsn.opCode;
Bill Buzbee1465db52009-09-23 17:17:35 -0700805 RegLocation rlDest;
806 RegLocation rlSrc1;
807 RegLocation rlSrc2;
808 /* Deduce sizes of operands */
809 if (mir->ssaRep->numUses == 2) {
Bill Buzbeec6f10662010-02-09 11:16:15 -0800810 rlSrc1 = dvmCompilerGetSrc(cUnit, mir, 0);
811 rlSrc2 = dvmCompilerGetSrc(cUnit, mir, 1);
Bill Buzbee1465db52009-09-23 17:17:35 -0700812 } else if (mir->ssaRep->numUses == 3) {
Bill Buzbeec6f10662010-02-09 11:16:15 -0800813 rlSrc1 = dvmCompilerGetSrcWide(cUnit, mir, 0, 1);
814 rlSrc2 = dvmCompilerGetSrc(cUnit, mir, 2);
Bill Buzbee1465db52009-09-23 17:17:35 -0700815 } else {
Bill Buzbeec6f10662010-02-09 11:16:15 -0800816 rlSrc1 = dvmCompilerGetSrcWide(cUnit, mir, 0, 1);
817 rlSrc2 = dvmCompilerGetSrcWide(cUnit, mir, 2, 3);
Bill Buzbee1465db52009-09-23 17:17:35 -0700818 assert(mir->ssaRep->numUses == 4);
819 }
820 if (mir->ssaRep->numDefs == 1) {
Bill Buzbeec6f10662010-02-09 11:16:15 -0800821 rlDest = dvmCompilerGetDest(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -0700822 } else {
823 assert(mir->ssaRep->numDefs == 2);
Bill Buzbeec6f10662010-02-09 11:16:15 -0800824 rlDest = dvmCompilerGetDestWide(cUnit, mir, 0, 1);
Bill Buzbee1465db52009-09-23 17:17:35 -0700825 }
Ben Chengba4fc8b2009-06-01 13:00:29 -0700826
827 if ((opCode >= OP_ADD_LONG_2ADDR) && (opCode <= OP_XOR_LONG_2ADDR)) {
Ben Cheng5d90c202009-11-22 23:31:11 -0800828 return genArithOpLong(cUnit,mir, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700829 }
830 if ((opCode >= OP_ADD_LONG) && (opCode <= OP_XOR_LONG)) {
Ben Cheng5d90c202009-11-22 23:31:11 -0800831 return genArithOpLong(cUnit,mir, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700832 }
833 if ((opCode >= OP_SHL_LONG_2ADDR) && (opCode <= OP_USHR_LONG_2ADDR)) {
Ben Cheng5d90c202009-11-22 23:31:11 -0800834 return genShiftOpLong(cUnit,mir, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700835 }
836 if ((opCode >= OP_SHL_LONG) && (opCode <= OP_USHR_LONG)) {
Ben Cheng5d90c202009-11-22 23:31:11 -0800837 return genShiftOpLong(cUnit,mir, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700838 }
839 if ((opCode >= OP_ADD_INT_2ADDR) && (opCode <= OP_USHR_INT_2ADDR)) {
Ben Cheng5d90c202009-11-22 23:31:11 -0800840 return genArithOpInt(cUnit,mir, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700841 }
842 if ((opCode >= OP_ADD_INT) && (opCode <= OP_USHR_INT)) {
Ben Cheng5d90c202009-11-22 23:31:11 -0800843 return genArithOpInt(cUnit,mir, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700844 }
845 if ((opCode >= OP_ADD_FLOAT_2ADDR) && (opCode <= OP_REM_FLOAT_2ADDR)) {
Ben Cheng5d90c202009-11-22 23:31:11 -0800846 return genArithOpFloat(cUnit,mir, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700847 }
848 if ((opCode >= OP_ADD_FLOAT) && (opCode <= OP_REM_FLOAT)) {
Ben Cheng5d90c202009-11-22 23:31:11 -0800849 return genArithOpFloat(cUnit, mir, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700850 }
851 if ((opCode >= OP_ADD_DOUBLE_2ADDR) && (opCode <= OP_REM_DOUBLE_2ADDR)) {
Ben Cheng5d90c202009-11-22 23:31:11 -0800852 return genArithOpDouble(cUnit,mir, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700853 }
854 if ((opCode >= OP_ADD_DOUBLE) && (opCode <= OP_REM_DOUBLE)) {
Ben Cheng5d90c202009-11-22 23:31:11 -0800855 return genArithOpDouble(cUnit,mir, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700856 }
857 return true;
858}
859
Bill Buzbee1465db52009-09-23 17:17:35 -0700860/* Generate unconditional branch instructions */
861static ArmLIR *genUnconditionalBranch(CompilationUnit *cUnit, ArmLIR *target)
862{
863 ArmLIR *branch = opNone(cUnit, kOpUncondBr);
864 branch->generic.target = (LIR *) target;
865 return branch;
866}
867
Bill Buzbee1465db52009-09-23 17:17:35 -0700868/* Perform the actual operation for OP_RETURN_* */
869static void genReturnCommon(CompilationUnit *cUnit, MIR *mir)
870{
871 genDispatchToHandler(cUnit, TEMPLATE_RETURN);
Ben Cheng978738d2010-05-13 13:45:57 -0700872#if defined(WITH_JIT_TUNING)
Bill Buzbee1465db52009-09-23 17:17:35 -0700873 gDvmJit.returnOp++;
874#endif
875 int dPC = (int) (cUnit->method->insns + mir->offset);
876 /* Insert branch, but defer setting of target */
877 ArmLIR *branch = genUnconditionalBranch(cUnit, NULL);
878 /* Set up the place holder to reconstruct this Dalvik PC */
879 ArmLIR *pcrLabel = dvmCompilerNew(sizeof(ArmLIR), true);
Ben Chenga4973592010-03-31 11:59:18 -0700880 pcrLabel->opCode = kArmPseudoPCReconstructionCell;
Bill Buzbee1465db52009-09-23 17:17:35 -0700881 pcrLabel->operands[0] = dPC;
882 pcrLabel->operands[1] = mir->offset;
883 /* Insert the place holder to the growable list */
884 dvmInsertGrowableList(&cUnit->pcReconstructionList, pcrLabel);
885 /* Branch to the PC reconstruction code */
886 branch->generic.target = (LIR *) pcrLabel;
887}
888
Ben Chengba4fc8b2009-06-01 13:00:29 -0700889static void genProcessArgsNoRange(CompilationUnit *cUnit, MIR *mir,
890 DecodedInstruction *dInsn,
Bill Buzbee89efc3d2009-07-28 11:22:22 -0700891 ArmLIR **pcrLabel)
Ben Chengba4fc8b2009-06-01 13:00:29 -0700892{
893 unsigned int i;
894 unsigned int regMask = 0;
Bill Buzbee1465db52009-09-23 17:17:35 -0700895 RegLocation rlArg;
896 int numDone = 0;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700897
Bill Buzbee1465db52009-09-23 17:17:35 -0700898 /*
899 * Load arguments to r0..r4. Note that these registers may contain
900 * live values, so we clobber them immediately after loading to prevent
901 * them from being used as sources for subsequent loads.
902 */
Bill Buzbeec6f10662010-02-09 11:16:15 -0800903 dvmCompilerLockAllTemps(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700904 for (i = 0; i < dInsn->vA; i++) {
905 regMask |= 1 << i;
Bill Buzbeec6f10662010-02-09 11:16:15 -0800906 rlArg = dvmCompilerGetSrc(cUnit, mir, numDone++);
Bill Buzbee1465db52009-09-23 17:17:35 -0700907 loadValueDirectFixed(cUnit, rlArg, i);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700908 }
909 if (regMask) {
910 /* Up to 5 args are pushed on top of FP - sizeofStackSaveArea */
Bill Buzbee1465db52009-09-23 17:17:35 -0700911 opRegRegImm(cUnit, kOpSub, r7, rFP,
912 sizeof(StackSaveArea) + (dInsn->vA << 2));
Ben Chengba4fc8b2009-06-01 13:00:29 -0700913 /* generate null check */
914 if (pcrLabel) {
Bill Buzbeec6f10662010-02-09 11:16:15 -0800915 *pcrLabel = genNullCheck(cUnit, dvmCompilerSSASrc(mir, 0), r0,
Bill Buzbee1465db52009-09-23 17:17:35 -0700916 mir->offset, NULL);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700917 }
Bill Buzbee270c1d62009-08-13 16:58:07 -0700918 storeMultiple(cUnit, r7, regMask);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700919 }
920}
921
922static void genProcessArgsRange(CompilationUnit *cUnit, MIR *mir,
923 DecodedInstruction *dInsn,
Bill Buzbee89efc3d2009-07-28 11:22:22 -0700924 ArmLIR **pcrLabel)
Ben Chengba4fc8b2009-06-01 13:00:29 -0700925{
926 int srcOffset = dInsn->vC << 2;
927 int numArgs = dInsn->vA;
928 int regMask;
Bill Buzbee1465db52009-09-23 17:17:35 -0700929
930 /*
931 * Note: here, all promoted registers will have been flushed
932 * back to the Dalvik base locations, so register usage restrictins
933 * are lifted. All parms loaded from original Dalvik register
934 * region - even though some might conceivably have valid copies
935 * cached in a preserved register.
936 */
Bill Buzbeec6f10662010-02-09 11:16:15 -0800937 dvmCompilerLockAllTemps(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -0700938
Ben Chengba4fc8b2009-06-01 13:00:29 -0700939 /*
940 * r4PC : &rFP[vC]
941 * r7: &newFP[0]
942 */
Bill Buzbee1465db52009-09-23 17:17:35 -0700943 opRegRegImm(cUnit, kOpAdd, r4PC, rFP, srcOffset);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700944 /* load [r0 .. min(numArgs,4)] */
945 regMask = (1 << ((numArgs < 4) ? numArgs : 4)) - 1;
Ben Chengd7d426a2009-09-22 11:23:36 -0700946 /*
947 * Protect the loadMultiple instruction from being reordered with other
948 * Dalvik stack accesses.
949 */
Bill Buzbee270c1d62009-08-13 16:58:07 -0700950 loadMultiple(cUnit, r4PC, regMask);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700951
Bill Buzbee1465db52009-09-23 17:17:35 -0700952 opRegRegImm(cUnit, kOpSub, r7, rFP,
953 sizeof(StackSaveArea) + (numArgs << 2));
Ben Chengba4fc8b2009-06-01 13:00:29 -0700954 /* generate null check */
955 if (pcrLabel) {
Bill Buzbeec6f10662010-02-09 11:16:15 -0800956 *pcrLabel = genNullCheck(cUnit, dvmCompilerSSASrc(mir, 0), r0,
Bill Buzbee1465db52009-09-23 17:17:35 -0700957 mir->offset, NULL);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700958 }
959
960 /*
961 * Handle remaining 4n arguments:
962 * store previously loaded 4 values and load the next 4 values
963 */
964 if (numArgs >= 8) {
Bill Buzbee89efc3d2009-07-28 11:22:22 -0700965 ArmLIR *loopLabel = NULL;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700966 /*
967 * r0 contains "this" and it will be used later, so push it to the stack
Bill Buzbee270c1d62009-08-13 16:58:07 -0700968 * first. Pushing r5 (rFP) is just for stack alignment purposes.
Ben Chengba4fc8b2009-06-01 13:00:29 -0700969 */
Bill Buzbee1465db52009-09-23 17:17:35 -0700970 opImm(cUnit, kOpPush, (1 << r0 | 1 << rFP));
Ben Chengba4fc8b2009-06-01 13:00:29 -0700971 /* No need to generate the loop structure if numArgs <= 11 */
972 if (numArgs > 11) {
973 loadConstant(cUnit, 5, ((numArgs - 4) >> 2) << 2);
Bill Buzbee1465db52009-09-23 17:17:35 -0700974 loopLabel = newLIR0(cUnit, kArmPseudoTargetLabel);
Ben Chengd7d426a2009-09-22 11:23:36 -0700975 loopLabel->defMask = ENCODE_ALL;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700976 }
Bill Buzbee270c1d62009-08-13 16:58:07 -0700977 storeMultiple(cUnit, r7, regMask);
Ben Chengd7d426a2009-09-22 11:23:36 -0700978 /*
979 * Protect the loadMultiple instruction from being reordered with other
980 * Dalvik stack accesses.
981 */
Bill Buzbee270c1d62009-08-13 16:58:07 -0700982 loadMultiple(cUnit, r4PC, regMask);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700983 /* No need to generate the loop structure if numArgs <= 11 */
984 if (numArgs > 11) {
Bill Buzbee1465db52009-09-23 17:17:35 -0700985 opRegImm(cUnit, kOpSub, rFP, 4);
986 genConditionalBranch(cUnit, kArmCondNe, loopLabel);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700987 }
988 }
989
990 /* Save the last batch of loaded values */
Bill Buzbee270c1d62009-08-13 16:58:07 -0700991 storeMultiple(cUnit, r7, regMask);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700992
993 /* Generate the loop epilogue - don't use r0 */
994 if ((numArgs > 4) && (numArgs % 4)) {
995 regMask = ((1 << (numArgs & 0x3)) - 1) << 1;
Ben Chengd7d426a2009-09-22 11:23:36 -0700996 /*
997 * Protect the loadMultiple instruction from being reordered with other
998 * Dalvik stack accesses.
999 */
Bill Buzbee270c1d62009-08-13 16:58:07 -07001000 loadMultiple(cUnit, r4PC, regMask);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001001 }
1002 if (numArgs >= 8)
Bill Buzbee1465db52009-09-23 17:17:35 -07001003 opImm(cUnit, kOpPop, (1 << r0 | 1 << rFP));
Ben Chengba4fc8b2009-06-01 13:00:29 -07001004
1005 /* Save the modulo 4 arguments */
1006 if ((numArgs > 4) && (numArgs % 4)) {
Bill Buzbee270c1d62009-08-13 16:58:07 -07001007 storeMultiple(cUnit, r7, regMask);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001008 }
1009}
1010
Ben Cheng38329f52009-07-07 14:19:20 -07001011/*
1012 * Generate code to setup the call stack then jump to the chaining cell if it
1013 * is not a native method.
1014 */
1015static void genInvokeSingletonCommon(CompilationUnit *cUnit, MIR *mir,
Bill Buzbee89efc3d2009-07-28 11:22:22 -07001016 BasicBlock *bb, ArmLIR *labelList,
1017 ArmLIR *pcrLabel,
Ben Cheng38329f52009-07-07 14:19:20 -07001018 const Method *calleeMethod)
Ben Chengba4fc8b2009-06-01 13:00:29 -07001019{
Bill Buzbee1465db52009-09-23 17:17:35 -07001020 /*
1021 * Note: all Dalvik register state should be flushed to
1022 * memory by the point, so register usage restrictions no
1023 * longer apply. All temp & preserved registers may be used.
1024 */
Bill Buzbeec6f10662010-02-09 11:16:15 -08001025 dvmCompilerLockAllTemps(cUnit);
Bill Buzbee89efc3d2009-07-28 11:22:22 -07001026 ArmLIR *retChainingCell = &labelList[bb->fallThrough->id];
Ben Chengba4fc8b2009-06-01 13:00:29 -07001027
1028 /* r1 = &retChainingCell */
Bill Buzbeec6f10662010-02-09 11:16:15 -08001029 dvmCompilerLockTemp(cUnit, r1);
Bill Buzbee1465db52009-09-23 17:17:35 -07001030 ArmLIR *addrRetChain = opRegRegImm(cUnit, kOpAdd, r1, rpc, 0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001031 /* r4PC = dalvikCallsite */
1032 loadConstant(cUnit, r4PC,
1033 (int) (cUnit->method->insns + mir->offset));
1034 addrRetChain->generic.target = (LIR *) retChainingCell;
1035 /*
Ben Cheng38329f52009-07-07 14:19:20 -07001036 * r0 = calleeMethod (loaded upon calling genInvokeSingletonCommon)
Ben Chengba4fc8b2009-06-01 13:00:29 -07001037 * r1 = &ChainingCell
1038 * r4PC = callsiteDPC
1039 */
1040 if (dvmIsNativeMethod(calleeMethod)) {
Ben Cheng38329f52009-07-07 14:19:20 -07001041 genDispatchToHandler(cUnit, TEMPLATE_INVOKE_METHOD_NATIVE);
Ben Cheng978738d2010-05-13 13:45:57 -07001042#if defined(WITH_JIT_TUNING)
Ben Cheng38329f52009-07-07 14:19:20 -07001043 gDvmJit.invokeNative++;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001044#endif
1045 } else {
1046 genDispatchToHandler(cUnit, TEMPLATE_INVOKE_METHOD_CHAIN);
Ben Cheng978738d2010-05-13 13:45:57 -07001047#if defined(WITH_JIT_TUNING)
Ben Cheng86717f72010-03-05 15:27:21 -08001048 gDvmJit.invokeMonomorphic++;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001049#endif
Ben Cheng38329f52009-07-07 14:19:20 -07001050 /* Branch to the chaining cell */
Ben Chengba4fc8b2009-06-01 13:00:29 -07001051 genUnconditionalBranch(cUnit, &labelList[bb->taken->id]);
1052 }
1053 /* Handle exceptions using the interpreter */
1054 genTrap(cUnit, mir->offset, pcrLabel);
1055}
1056
Ben Cheng38329f52009-07-07 14:19:20 -07001057/*
1058 * Generate code to check the validity of a predicted chain and take actions
1059 * based on the result.
1060 *
1061 * 0x426a99aa : ldr r4, [pc, #72] --> r4 <- dalvikPC of this invoke
1062 * 0x426a99ac : add r1, pc, #32 --> r1 <- &retChainingCell
1063 * 0x426a99ae : add r2, pc, #40 --> r2 <- &predictedChainingCell
1064 * 0x426a99b0 : blx_1 0x426a918c --+ TEMPLATE_INVOKE_METHOD_PREDICTED_CHAIN
1065 * 0x426a99b2 : blx_2 see above --+
1066 * 0x426a99b4 : b 0x426a99d8 --> off to the predicted chain
1067 * 0x426a99b6 : b 0x426a99c8 --> punt to the interpreter
1068 * 0x426a99b8 : ldr r0, [r7, #44] --> r0 <- this->class->vtable[methodIdx]
1069 * 0x426a99ba : cmp r1, #0 --> compare r1 (rechain count) against 0
1070 * 0x426a99bc : bgt 0x426a99c2 --> >=0? don't rechain
1071 * 0x426a99be : ldr r7, [r6, #96] --+ dvmJitToPatchPredictedChain
1072 * 0x426a99c0 : blx r7 --+
1073 * 0x426a99c2 : add r1, pc, #12 --> r1 <- &retChainingCell
1074 * 0x426a99c4 : blx_1 0x426a9098 --+ TEMPLATE_INVOKE_METHOD_NO_OPT
1075 * 0x426a99c6 : blx_2 see above --+
1076 */
1077static void genInvokeVirtualCommon(CompilationUnit *cUnit, MIR *mir,
1078 int methodIndex,
Bill Buzbee89efc3d2009-07-28 11:22:22 -07001079 ArmLIR *retChainingCell,
1080 ArmLIR *predChainingCell,
1081 ArmLIR *pcrLabel)
Ben Cheng38329f52009-07-07 14:19:20 -07001082{
Bill Buzbee1465db52009-09-23 17:17:35 -07001083 /*
1084 * Note: all Dalvik register state should be flushed to
1085 * memory by the point, so register usage restrictions no
1086 * longer apply. Lock temps to prevent them from being
1087 * allocated by utility routines.
1088 */
Bill Buzbeec6f10662010-02-09 11:16:15 -08001089 dvmCompilerLockAllTemps(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07001090
Ben Cheng38329f52009-07-07 14:19:20 -07001091 /* "this" is already left in r0 by genProcessArgs* */
1092
1093 /* r4PC = dalvikCallsite */
1094 loadConstant(cUnit, r4PC,
1095 (int) (cUnit->method->insns + mir->offset));
1096
1097 /* r1 = &retChainingCell */
Bill Buzbee1465db52009-09-23 17:17:35 -07001098 ArmLIR *addrRetChain = opRegRegImm(cUnit, kOpAdd, r1, rpc, 0);
Ben Cheng38329f52009-07-07 14:19:20 -07001099 addrRetChain->generic.target = (LIR *) retChainingCell;
1100
1101 /* r2 = &predictedChainingCell */
Bill Buzbee1465db52009-09-23 17:17:35 -07001102 ArmLIR *predictedChainingCell = opRegRegImm(cUnit, kOpAdd, r2, rpc, 0);
Ben Cheng38329f52009-07-07 14:19:20 -07001103 predictedChainingCell->generic.target = (LIR *) predChainingCell;
1104
1105 genDispatchToHandler(cUnit, TEMPLATE_INVOKE_METHOD_PREDICTED_CHAIN);
1106
1107 /* return through lr - jump to the chaining cell */
1108 genUnconditionalBranch(cUnit, predChainingCell);
1109
1110 /*
1111 * null-check on "this" may have been eliminated, but we still need a PC-
1112 * reconstruction label for stack overflow bailout.
1113 */
1114 if (pcrLabel == NULL) {
1115 int dPC = (int) (cUnit->method->insns + mir->offset);
Bill Buzbee89efc3d2009-07-28 11:22:22 -07001116 pcrLabel = dvmCompilerNew(sizeof(ArmLIR), true);
Ben Chenga4973592010-03-31 11:59:18 -07001117 pcrLabel->opCode = kArmPseudoPCReconstructionCell;
Ben Cheng38329f52009-07-07 14:19:20 -07001118 pcrLabel->operands[0] = dPC;
1119 pcrLabel->operands[1] = mir->offset;
1120 /* Insert the place holder to the growable list */
1121 dvmInsertGrowableList(&cUnit->pcReconstructionList, pcrLabel);
1122 }
1123
1124 /* return through lr+2 - punt to the interpreter */
1125 genUnconditionalBranch(cUnit, pcrLabel);
1126
1127 /*
1128 * return through lr+4 - fully resolve the callee method.
1129 * r1 <- count
1130 * r2 <- &predictedChainCell
1131 * r3 <- this->class
1132 * r4 <- dPC
1133 * r7 <- this->class->vtable
1134 */
1135
1136 /* r0 <- calleeMethod */
Bill Buzbee270c1d62009-08-13 16:58:07 -07001137 loadWordDisp(cUnit, r7, methodIndex * 4, r0);
Ben Cheng38329f52009-07-07 14:19:20 -07001138
1139 /* Check if rechain limit is reached */
Bill Buzbee1465db52009-09-23 17:17:35 -07001140 opRegImm(cUnit, kOpCmp, r1, 0);
Ben Cheng38329f52009-07-07 14:19:20 -07001141
Bill Buzbee1465db52009-09-23 17:17:35 -07001142 ArmLIR *bypassRechaining = opCondBranch(cUnit, kArmCondGt);
Ben Cheng38329f52009-07-07 14:19:20 -07001143
Bill Buzbee270c1d62009-08-13 16:58:07 -07001144 loadWordDisp(cUnit, rGLUE, offsetof(InterpState,
1145 jitToInterpEntries.dvmJitToPatchPredictedChain), r7);
Ben Cheng38329f52009-07-07 14:19:20 -07001146
Ben Chengb88ec3c2010-05-17 12:50:33 -07001147 genRegCopy(cUnit, r1, rGLUE);
1148
Ben Cheng38329f52009-07-07 14:19:20 -07001149 /*
1150 * r0 = calleeMethod
1151 * r2 = &predictedChainingCell
1152 * r3 = class
1153 *
1154 * &returnChainingCell has been loaded into r1 but is not needed
1155 * when patching the chaining cell and will be clobbered upon
1156 * returning so it will be reconstructed again.
1157 */
Bill Buzbee1465db52009-09-23 17:17:35 -07001158 opReg(cUnit, kOpBlx, r7);
Ben Cheng38329f52009-07-07 14:19:20 -07001159
1160 /* r1 = &retChainingCell */
Bill Buzbee1465db52009-09-23 17:17:35 -07001161 addrRetChain = opRegRegImm(cUnit, kOpAdd, r1, rpc, 0);
Ben Cheng38329f52009-07-07 14:19:20 -07001162 addrRetChain->generic.target = (LIR *) retChainingCell;
1163
1164 bypassRechaining->generic.target = (LIR *) addrRetChain;
1165 /*
1166 * r0 = calleeMethod,
1167 * r1 = &ChainingCell,
1168 * r4PC = callsiteDPC,
1169 */
1170 genDispatchToHandler(cUnit, TEMPLATE_INVOKE_METHOD_NO_OPT);
Ben Cheng978738d2010-05-13 13:45:57 -07001171#if defined(WITH_JIT_TUNING)
Ben Cheng86717f72010-03-05 15:27:21 -08001172 gDvmJit.invokePolymorphic++;
Ben Cheng38329f52009-07-07 14:19:20 -07001173#endif
1174 /* Handle exceptions using the interpreter */
1175 genTrap(cUnit, mir->offset, pcrLabel);
1176}
1177
Ben Chengba4fc8b2009-06-01 13:00:29 -07001178/* Geneate a branch to go back to the interpreter */
1179static void genPuntToInterp(CompilationUnit *cUnit, unsigned int offset)
1180{
1181 /* r0 = dalvik pc */
Bill Buzbeec6f10662010-02-09 11:16:15 -08001182 dvmCompilerFlushAllRegs(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001183 loadConstant(cUnit, r0, (int) (cUnit->method->insns + offset));
Bill Buzbee270c1d62009-08-13 16:58:07 -07001184 loadWordDisp(cUnit, r0, offsetof(Object, clazz), r3);
1185 loadWordDisp(cUnit, rGLUE, offsetof(InterpState,
1186 jitToInterpEntries.dvmJitToInterpPunt), r1);
Bill Buzbee1465db52009-09-23 17:17:35 -07001187 opReg(cUnit, kOpBlx, r1);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001188}
1189
1190/*
1191 * Attempt to single step one instruction using the interpreter and return
1192 * to the compiled code for the next Dalvik instruction
1193 */
1194static void genInterpSingleStep(CompilationUnit *cUnit, MIR *mir)
1195{
1196 int flags = dexGetInstrFlags(gDvm.instrFlags, mir->dalvikInsn.opCode);
1197 int flagsToCheck = kInstrCanBranch | kInstrCanSwitch | kInstrCanReturn |
1198 kInstrCanThrow;
Bill Buzbee1465db52009-09-23 17:17:35 -07001199
Bill Buzbee45273872010-03-11 11:12:15 -08001200 //If already optimized out, just ignore
1201 if (mir->dalvikInsn.opCode == OP_NOP)
1202 return;
1203
Bill Buzbee1465db52009-09-23 17:17:35 -07001204 //Ugly, but necessary. Flush all Dalvik regs so Interp can find them
Bill Buzbeec6f10662010-02-09 11:16:15 -08001205 dvmCompilerFlushAllRegs(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07001206
Ben Chengba4fc8b2009-06-01 13:00:29 -07001207 if ((mir->next == NULL) || (flags & flagsToCheck)) {
1208 genPuntToInterp(cUnit, mir->offset);
1209 return;
1210 }
1211 int entryAddr = offsetof(InterpState,
1212 jitToInterpEntries.dvmJitToInterpSingleStep);
Bill Buzbee270c1d62009-08-13 16:58:07 -07001213 loadWordDisp(cUnit, rGLUE, entryAddr, r2);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001214 /* r0 = dalvik pc */
1215 loadConstant(cUnit, r0, (int) (cUnit->method->insns + mir->offset));
1216 /* r1 = dalvik pc of following instruction */
1217 loadConstant(cUnit, r1, (int) (cUnit->method->insns + mir->next->offset));
Bill Buzbee1465db52009-09-23 17:17:35 -07001218 opReg(cUnit, kOpBlx, r2);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001219}
1220
Ben Chengfc075c22010-05-28 15:20:08 -07001221#if defined(WITH_DEADLOCK_PREDICTION) || defined(WITH_MONITOR_TRACKING) || \
1222 defined(_ARMV5TE) || defined(_ARMV5TE_VFP)
Bill Buzbeec1d9ed42010-02-02 11:04:33 -08001223/*
1224 * To prevent a thread in a monitor wait from blocking the Jit from
1225 * resetting the code cache, heavyweight monitor lock will not
1226 * be allowed to return to an existing translation. Instead, we will
1227 * handle them by branching to a handler, which will in turn call the
1228 * runtime lock routine and then branch directly back to the
1229 * interpreter main loop. Given the high cost of the heavyweight
1230 * lock operation, this additional cost should be slight (especially when
1231 * considering that we expect the vast majority of lock operations to
1232 * use the fast-path thin lock bypass).
1233 */
Ben Cheng5d90c202009-11-22 23:31:11 -08001234static void genMonitorPortable(CompilationUnit *cUnit, MIR *mir)
Bill Buzbee270c1d62009-08-13 16:58:07 -07001235{
Bill Buzbeeefbd3c52009-11-04 22:18:40 -08001236 bool isEnter = (mir->dalvikInsn.opCode == OP_MONITOR_ENTER);
Bill Buzbee1465db52009-09-23 17:17:35 -07001237 genExportPC(cUnit, mir);
Bill Buzbeec6f10662010-02-09 11:16:15 -08001238 dvmCompilerFlushAllRegs(cUnit); /* Send everything to home location */
1239 RegLocation rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07001240 loadValueDirectFixed(cUnit, rlSrc, r1);
1241 loadWordDisp(cUnit, rGLUE, offsetof(InterpState, self), r0);
Bill Buzbeec1d9ed42010-02-02 11:04:33 -08001242 genNullCheck(cUnit, rlSrc.sRegLow, r1, mir->offset, NULL);
Bill Buzbeeefbd3c52009-11-04 22:18:40 -08001243 if (isEnter) {
Bill Buzbeec1d9ed42010-02-02 11:04:33 -08001244 /* Get dPC of next insn */
1245 loadConstant(cUnit, r4PC, (int)(cUnit->method->insns + mir->offset +
1246 dexGetInstrWidthAbs(gDvm.instrWidth, OP_MONITOR_ENTER)));
1247#if defined(WITH_DEADLOCK_PREDICTION)
1248 genDispatchToHandler(cUnit, TEMPLATE_MONITOR_ENTER_DEBUG);
1249#else
1250 genDispatchToHandler(cUnit, TEMPLATE_MONITOR_ENTER);
1251#endif
Bill Buzbee1465db52009-09-23 17:17:35 -07001252 } else {
Ben Chengbd1326d2010-04-02 15:04:53 -07001253 LOAD_FUNC_ADDR(cUnit, r2, (int)dvmUnlockObject);
Bill Buzbeec1d9ed42010-02-02 11:04:33 -08001254 /* Do the call */
1255 opReg(cUnit, kOpBlx, r2);
Bill Buzbee6bbdd6b2010-02-16 14:40:01 -08001256 opRegImm(cUnit, kOpCmp, r0, 0); /* Did we throw? */
1257 ArmLIR *branchOver = opCondBranch(cUnit, kArmCondNe);
1258 loadConstant(cUnit, r0,
1259 (int) (cUnit->method->insns + mir->offset +
1260 dexGetInstrWidthAbs(gDvm.instrWidth, OP_MONITOR_EXIT)));
1261 genDispatchToHandler(cUnit, TEMPLATE_THROW_EXCEPTION_COMMON);
1262 ArmLIR *target = newLIR0(cUnit, kArmPseudoTargetLabel);
1263 target->defMask = ENCODE_ALL;
1264 branchOver->generic.target = (LIR *) target;
Elliott Hughes6a555132010-02-25 15:41:42 -08001265 dvmCompilerClobberCallRegs(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07001266 }
Bill Buzbee270c1d62009-08-13 16:58:07 -07001267}
Ben Chengfc075c22010-05-28 15:20:08 -07001268#endif
Bill Buzbee270c1d62009-08-13 16:58:07 -07001269
Ben Chengba4fc8b2009-06-01 13:00:29 -07001270/*
1271 * The following are the first-level codegen routines that analyze the format
1272 * of each bytecode then either dispatch special purpose codegen routines
1273 * or produce corresponding Thumb instructions directly.
1274 */
1275
1276static bool handleFmt10t_Fmt20t_Fmt30t(CompilationUnit *cUnit, MIR *mir,
Bill Buzbee89efc3d2009-07-28 11:22:22 -07001277 BasicBlock *bb, ArmLIR *labelList)
Ben Chengba4fc8b2009-06-01 13:00:29 -07001278{
1279 /* For OP_GOTO, OP_GOTO_16, and OP_GOTO_32 */
1280 genUnconditionalBranch(cUnit, &labelList[bb->taken->id]);
1281 return false;
1282}
1283
1284static bool handleFmt10x(CompilationUnit *cUnit, MIR *mir)
1285{
1286 OpCode dalvikOpCode = mir->dalvikInsn.opCode;
Andy McFaddenc35a2ef2010-06-17 12:36:00 -07001287 if ((dalvikOpCode >= OP_UNUSED_3E) && (dalvikOpCode <= OP_UNUSED_43)) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07001288 LOGE("Codegen: got unused opcode 0x%x\n",dalvikOpCode);
1289 return true;
1290 }
1291 switch (dalvikOpCode) {
1292 case OP_RETURN_VOID:
1293 genReturnCommon(cUnit,mir);
1294 break;
1295 case OP_UNUSED_73:
1296 case OP_UNUSED_79:
1297 case OP_UNUSED_7A:
Andy McFaddenc35a2ef2010-06-17 12:36:00 -07001298 case OP_UNUSED_F1:
1299 case OP_UNUSED_FF:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001300 LOGE("Codegen: got unused opcode 0x%x\n",dalvikOpCode);
1301 return true;
1302 case OP_NOP:
1303 break;
1304 default:
1305 return true;
1306 }
1307 return false;
1308}
1309
1310static bool handleFmt11n_Fmt31i(CompilationUnit *cUnit, MIR *mir)
1311{
Bill Buzbee1465db52009-09-23 17:17:35 -07001312 RegLocation rlDest;
1313 RegLocation rlResult;
1314 if (mir->ssaRep->numDefs == 2) {
Bill Buzbeec6f10662010-02-09 11:16:15 -08001315 rlDest = dvmCompilerGetDestWide(cUnit, mir, 0, 1);
Bill Buzbee1465db52009-09-23 17:17:35 -07001316 } else {
Bill Buzbeec6f10662010-02-09 11:16:15 -08001317 rlDest = dvmCompilerGetDest(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07001318 }
Ben Chenge9695e52009-06-16 16:11:47 -07001319
Ben Chengba4fc8b2009-06-01 13:00:29 -07001320 switch (mir->dalvikInsn.opCode) {
1321 case OP_CONST:
Ben Chenge9695e52009-06-16 16:11:47 -07001322 case OP_CONST_4: {
Bill Buzbeec6f10662010-02-09 11:16:15 -08001323 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kAnyReg, true);
Ben Chengbd1326d2010-04-02 15:04:53 -07001324 loadConstantNoClobber(cUnit, rlResult.lowReg, mir->dalvikInsn.vB);
Bill Buzbee1465db52009-09-23 17:17:35 -07001325 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001326 break;
Ben Chenge9695e52009-06-16 16:11:47 -07001327 }
1328 case OP_CONST_WIDE_32: {
Bill Buzbee1465db52009-09-23 17:17:35 -07001329 //TUNING: single routine to load constant pair for support doubles
Bill Buzbee964a7b02010-01-28 12:54:19 -08001330 //TUNING: load 0/-1 separately to avoid load dependency
Bill Buzbeec6f10662010-02-09 11:16:15 -08001331 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Ben Chengbd1326d2010-04-02 15:04:53 -07001332 loadConstantNoClobber(cUnit, rlResult.lowReg, mir->dalvikInsn.vB);
Bill Buzbee1465db52009-09-23 17:17:35 -07001333 opRegRegImm(cUnit, kOpAsr, rlResult.highReg,
1334 rlResult.lowReg, 31);
1335 storeValueWide(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001336 break;
Ben Chenge9695e52009-06-16 16:11:47 -07001337 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07001338 default:
1339 return true;
1340 }
1341 return false;
1342}
1343
1344static bool handleFmt21h(CompilationUnit *cUnit, MIR *mir)
1345{
Bill Buzbee1465db52009-09-23 17:17:35 -07001346 RegLocation rlDest;
1347 RegLocation rlResult;
1348 if (mir->ssaRep->numDefs == 2) {
Bill Buzbeec6f10662010-02-09 11:16:15 -08001349 rlDest = dvmCompilerGetDestWide(cUnit, mir, 0, 1);
Bill Buzbee1465db52009-09-23 17:17:35 -07001350 } else {
Bill Buzbeec6f10662010-02-09 11:16:15 -08001351 rlDest = dvmCompilerGetDest(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07001352 }
Bill Buzbeec6f10662010-02-09 11:16:15 -08001353 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kAnyReg, true);
Ben Chenge9695e52009-06-16 16:11:47 -07001354
Ben Chengba4fc8b2009-06-01 13:00:29 -07001355 switch (mir->dalvikInsn.opCode) {
Ben Chenge9695e52009-06-16 16:11:47 -07001356 case OP_CONST_HIGH16: {
Ben Chengbd1326d2010-04-02 15:04:53 -07001357 loadConstantNoClobber(cUnit, rlResult.lowReg,
1358 mir->dalvikInsn.vB << 16);
Bill Buzbee1465db52009-09-23 17:17:35 -07001359 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001360 break;
Ben Chenge9695e52009-06-16 16:11:47 -07001361 }
1362 case OP_CONST_WIDE_HIGH16: {
Bill Buzbee1465db52009-09-23 17:17:35 -07001363 loadConstantValueWide(cUnit, rlResult.lowReg, rlResult.highReg,
1364 0, mir->dalvikInsn.vB << 16);
1365 storeValueWide(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001366 break;
Ben Chenge9695e52009-06-16 16:11:47 -07001367 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07001368 default:
1369 return true;
1370 }
1371 return false;
1372}
1373
1374static bool handleFmt20bc(CompilationUnit *cUnit, MIR *mir)
1375{
1376 /* For OP_THROW_VERIFICATION_ERROR */
1377 genInterpSingleStep(cUnit, mir);
1378 return false;
1379}
1380
1381static bool handleFmt21c_Fmt31c(CompilationUnit *cUnit, MIR *mir)
1382{
Bill Buzbee1465db52009-09-23 17:17:35 -07001383 RegLocation rlResult;
1384 RegLocation rlDest;
1385 RegLocation rlSrc;
Ben Chenge9695e52009-06-16 16:11:47 -07001386
Ben Chengba4fc8b2009-06-01 13:00:29 -07001387 switch (mir->dalvikInsn.opCode) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07001388 case OP_CONST_STRING_JUMBO:
1389 case OP_CONST_STRING: {
1390 void *strPtr = (void*)
1391 (cUnit->method->clazz->pDvmDex->pResStrings[mir->dalvikInsn.vB]);
Ben Chengdd6e8702010-05-07 13:05:47 -07001392
1393 if (strPtr == NULL) {
1394 LOGE("Unexpected null string");
1395 dvmAbort();
1396 }
1397
Bill Buzbeec6f10662010-02-09 11:16:15 -08001398 rlDest = dvmCompilerGetDest(cUnit, mir, 0);
1399 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Ben Chengbd1326d2010-04-02 15:04:53 -07001400 loadConstantNoClobber(cUnit, rlResult.lowReg, (int) strPtr );
Bill Buzbee1465db52009-09-23 17:17:35 -07001401 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001402 break;
1403 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07001404 case OP_CONST_CLASS: {
1405 void *classPtr = (void*)
1406 (cUnit->method->clazz->pDvmDex->pResClasses[mir->dalvikInsn.vB]);
Ben Chengdd6e8702010-05-07 13:05:47 -07001407
1408 if (classPtr == NULL) {
1409 LOGE("Unexpected null class");
1410 dvmAbort();
1411 }
1412
Bill Buzbeec6f10662010-02-09 11:16:15 -08001413 rlDest = dvmCompilerGetDest(cUnit, mir, 0);
1414 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Ben Chengbd1326d2010-04-02 15:04:53 -07001415 loadConstantNoClobber(cUnit, rlResult.lowReg, (int) classPtr );
Bill Buzbee1465db52009-09-23 17:17:35 -07001416 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001417 break;
1418 }
1419 case OP_SGET_OBJECT:
1420 case OP_SGET_BOOLEAN:
1421 case OP_SGET_CHAR:
1422 case OP_SGET_BYTE:
1423 case OP_SGET_SHORT:
1424 case OP_SGET: {
Bill Buzbee50a6bf22009-07-08 13:08:04 -07001425 int valOffset = offsetof(StaticField, value);
Bill Buzbeec6f10662010-02-09 11:16:15 -08001426 int tReg = dvmCompilerAllocTemp(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001427 void *fieldPtr = (void*)
1428 (cUnit->method->clazz->pDvmDex->pResFields[mir->dalvikInsn.vB]);
Ben Chengdd6e8702010-05-07 13:05:47 -07001429
1430 if (fieldPtr == NULL) {
1431 LOGE("Unexpected null static field");
1432 dvmAbort();
1433 }
1434
Bill Buzbeec6f10662010-02-09 11:16:15 -08001435 rlDest = dvmCompilerGetDest(cUnit, mir, 0);
1436 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kAnyReg, true);
Bill Buzbee1465db52009-09-23 17:17:35 -07001437 loadConstant(cUnit, tReg, (int) fieldPtr + valOffset);
Ben Cheng11d8f142010-03-24 15:24:19 -07001438
1439 HEAP_ACCESS_SHADOW(true);
Bill Buzbee1465db52009-09-23 17:17:35 -07001440 loadWordDisp(cUnit, tReg, 0, rlResult.lowReg);
Ben Cheng11d8f142010-03-24 15:24:19 -07001441 HEAP_ACCESS_SHADOW(false);
1442
Bill Buzbee1465db52009-09-23 17:17:35 -07001443 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001444 break;
1445 }
1446 case OP_SGET_WIDE: {
Bill Buzbee50a6bf22009-07-08 13:08:04 -07001447 int valOffset = offsetof(StaticField, value);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001448 void *fieldPtr = (void*)
1449 (cUnit->method->clazz->pDvmDex->pResFields[mir->dalvikInsn.vB]);
Ben Chengdd6e8702010-05-07 13:05:47 -07001450
1451 if (fieldPtr == NULL) {
1452 LOGE("Unexpected null static field");
1453 dvmAbort();
1454 }
1455
Bill Buzbeec6f10662010-02-09 11:16:15 -08001456 int tReg = dvmCompilerAllocTemp(cUnit);
Bill Buzbeec6f10662010-02-09 11:16:15 -08001457 rlDest = dvmCompilerGetDestWide(cUnit, mir, 0, 1);
1458 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kAnyReg, true);
Bill Buzbee1465db52009-09-23 17:17:35 -07001459 loadConstant(cUnit, tReg, (int) fieldPtr + valOffset);
Ben Cheng11d8f142010-03-24 15:24:19 -07001460
1461 HEAP_ACCESS_SHADOW(true);
Bill Buzbee1465db52009-09-23 17:17:35 -07001462 loadPair(cUnit, tReg, rlResult.lowReg, rlResult.highReg);
Ben Cheng11d8f142010-03-24 15:24:19 -07001463 HEAP_ACCESS_SHADOW(false);
1464
Bill Buzbee1465db52009-09-23 17:17:35 -07001465 storeValueWide(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001466 break;
1467 }
1468 case OP_SPUT_OBJECT:
1469 case OP_SPUT_BOOLEAN:
1470 case OP_SPUT_CHAR:
1471 case OP_SPUT_BYTE:
1472 case OP_SPUT_SHORT:
1473 case OP_SPUT: {
Bill Buzbee50a6bf22009-07-08 13:08:04 -07001474 int valOffset = offsetof(StaticField, value);
Bill Buzbeec6f10662010-02-09 11:16:15 -08001475 int tReg = dvmCompilerAllocTemp(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001476 void *fieldPtr = (void*)
1477 (cUnit->method->clazz->pDvmDex->pResFields[mir->dalvikInsn.vB]);
Ben Chenge9695e52009-06-16 16:11:47 -07001478
Ben Chengdd6e8702010-05-07 13:05:47 -07001479 if (fieldPtr == NULL) {
1480 LOGE("Unexpected null static field");
1481 dvmAbort();
1482 }
1483
Bill Buzbeec6f10662010-02-09 11:16:15 -08001484 rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07001485 rlSrc = loadValue(cUnit, rlSrc, kAnyReg);
1486 loadConstant(cUnit, tReg, (int) fieldPtr + valOffset);
Ben Cheng11d8f142010-03-24 15:24:19 -07001487
1488 HEAP_ACCESS_SHADOW(true);
Bill Buzbee1465db52009-09-23 17:17:35 -07001489 storeWordDisp(cUnit, tReg, 0 ,rlSrc.lowReg);
Ben Cheng11d8f142010-03-24 15:24:19 -07001490 HEAP_ACCESS_SHADOW(false);
1491
Ben Chengba4fc8b2009-06-01 13:00:29 -07001492 break;
1493 }
1494 case OP_SPUT_WIDE: {
Bill Buzbeec6f10662010-02-09 11:16:15 -08001495 int tReg = dvmCompilerAllocTemp(cUnit);
Bill Buzbee50a6bf22009-07-08 13:08:04 -07001496 int valOffset = offsetof(StaticField, value);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001497 void *fieldPtr = (void*)
1498 (cUnit->method->clazz->pDvmDex->pResFields[mir->dalvikInsn.vB]);
Ben Chenge9695e52009-06-16 16:11:47 -07001499
Ben Chengdd6e8702010-05-07 13:05:47 -07001500 if (fieldPtr == NULL) {
1501 LOGE("Unexpected null static field");
1502 dvmAbort();
1503 }
1504
Bill Buzbeec6f10662010-02-09 11:16:15 -08001505 rlSrc = dvmCompilerGetSrcWide(cUnit, mir, 0, 1);
Bill Buzbee1465db52009-09-23 17:17:35 -07001506 rlSrc = loadValueWide(cUnit, rlSrc, kAnyReg);
1507 loadConstant(cUnit, tReg, (int) fieldPtr + valOffset);
Ben Cheng11d8f142010-03-24 15:24:19 -07001508
1509 HEAP_ACCESS_SHADOW(true);
Bill Buzbee1465db52009-09-23 17:17:35 -07001510 storePair(cUnit, tReg, rlSrc.lowReg, rlSrc.highReg);
Ben Cheng11d8f142010-03-24 15:24:19 -07001511 HEAP_ACCESS_SHADOW(false);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001512 break;
1513 }
1514 case OP_NEW_INSTANCE: {
Ben Chenge9695e52009-06-16 16:11:47 -07001515 /*
1516 * Obey the calling convention and don't mess with the register
1517 * usage.
1518 */
Ben Chengba4fc8b2009-06-01 13:00:29 -07001519 ClassObject *classPtr = (void*)
1520 (cUnit->method->clazz->pDvmDex->pResClasses[mir->dalvikInsn.vB]);
Ben Chengdd6e8702010-05-07 13:05:47 -07001521
1522 if (classPtr == NULL) {
1523 LOGE("Unexpected null class");
1524 dvmAbort();
1525 }
1526
Ben Cheng79d173c2009-09-29 16:12:51 -07001527 /*
1528 * If it is going to throw, it should not make to the trace to begin
Bill Buzbee1465db52009-09-23 17:17:35 -07001529 * with. However, Alloc might throw, so we need to genExportPC()
Ben Cheng79d173c2009-09-29 16:12:51 -07001530 */
1531 assert((classPtr->accessFlags & (ACC_INTERFACE|ACC_ABSTRACT)) == 0);
Bill Buzbeec6f10662010-02-09 11:16:15 -08001532 dvmCompilerFlushAllRegs(cUnit); /* Everything to home location */
Bill Buzbee1465db52009-09-23 17:17:35 -07001533 genExportPC(cUnit, mir);
Ben Chengbd1326d2010-04-02 15:04:53 -07001534 LOAD_FUNC_ADDR(cUnit, r2, (int)dvmAllocObject);
Ben Chenge9695e52009-06-16 16:11:47 -07001535 loadConstant(cUnit, r0, (int) classPtr);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001536 loadConstant(cUnit, r1, ALLOC_DONT_TRACK);
Bill Buzbee1465db52009-09-23 17:17:35 -07001537 opReg(cUnit, kOpBlx, r2);
Elliott Hughes6a555132010-02-25 15:41:42 -08001538 dvmCompilerClobberCallRegs(cUnit);
Ben Cheng4f489172009-09-27 17:08:35 -07001539 /* generate a branch over if allocation is successful */
Bill Buzbee1465db52009-09-23 17:17:35 -07001540 opRegImm(cUnit, kOpCmp, r0, 0); /* NULL? */
1541 ArmLIR *branchOver = opCondBranch(cUnit, kArmCondNe);
Ben Cheng4f489172009-09-27 17:08:35 -07001542 /*
1543 * OOM exception needs to be thrown here and cannot re-execute
1544 */
1545 loadConstant(cUnit, r0,
1546 (int) (cUnit->method->insns + mir->offset));
1547 genDispatchToHandler(cUnit, TEMPLATE_THROW_EXCEPTION_COMMON);
1548 /* noreturn */
1549
Bill Buzbee1465db52009-09-23 17:17:35 -07001550 ArmLIR *target = newLIR0(cUnit, kArmPseudoTargetLabel);
Ben Cheng4f489172009-09-27 17:08:35 -07001551 target->defMask = ENCODE_ALL;
1552 branchOver->generic.target = (LIR *) target;
Bill Buzbeec6f10662010-02-09 11:16:15 -08001553 rlDest = dvmCompilerGetDest(cUnit, mir, 0);
1554 rlResult = dvmCompilerGetReturn(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07001555 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001556 break;
1557 }
1558 case OP_CHECK_CAST: {
Ben Chenge9695e52009-06-16 16:11:47 -07001559 /*
1560 * Obey the calling convention and don't mess with the register
1561 * usage.
1562 */
Ben Chengba4fc8b2009-06-01 13:00:29 -07001563 ClassObject *classPtr =
1564 (cUnit->method->clazz->pDvmDex->pResClasses[mir->dalvikInsn.vB]);
Bill Buzbee4df41a52009-11-12 17:07:16 -08001565 /*
1566 * Note: It is possible that classPtr is NULL at this point,
1567 * even though this instruction has been successfully interpreted.
1568 * If the previous interpretation had a null source, the
1569 * interpreter would not have bothered to resolve the clazz.
1570 * Bail out to the interpreter in this case, and log it
1571 * so that we can tell if it happens frequently.
1572 */
1573 if (classPtr == NULL) {
Ben Cheng11d8f142010-03-24 15:24:19 -07001574 LOGVV("null clazz in OP_CHECK_CAST, single-stepping");
Bill Buzbee4df41a52009-11-12 17:07:16 -08001575 genInterpSingleStep(cUnit, mir);
1576 return false;
1577 }
Bill Buzbeec6f10662010-02-09 11:16:15 -08001578 dvmCompilerFlushAllRegs(cUnit); /* Everything to home location */
Ben Chengba4fc8b2009-06-01 13:00:29 -07001579 loadConstant(cUnit, r1, (int) classPtr );
Bill Buzbeec6f10662010-02-09 11:16:15 -08001580 rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07001581 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
1582 opRegImm(cUnit, kOpCmp, rlSrc.lowReg, 0); /* Null? */
1583 ArmLIR *branch1 = opCondBranch(cUnit, kArmCondEq);
1584 /*
1585 * rlSrc.lowReg now contains object->clazz. Note that
1586 * it could have been allocated r0, but we're okay so long
1587 * as we don't do anything desctructive until r0 is loaded
1588 * with clazz.
1589 */
Ben Chengba4fc8b2009-06-01 13:00:29 -07001590 /* r0 now contains object->clazz */
Bill Buzbee1465db52009-09-23 17:17:35 -07001591 loadWordDisp(cUnit, rlSrc.lowReg, offsetof(Object, clazz), r0);
Ben Chengbd1326d2010-04-02 15:04:53 -07001592 LOAD_FUNC_ADDR(cUnit, r2, (int)dvmInstanceofNonTrivial);
Bill Buzbee1465db52009-09-23 17:17:35 -07001593 opRegReg(cUnit, kOpCmp, r0, r1);
1594 ArmLIR *branch2 = opCondBranch(cUnit, kArmCondEq);
1595 opReg(cUnit, kOpBlx, r2);
Elliott Hughes6a555132010-02-25 15:41:42 -08001596 dvmCompilerClobberCallRegs(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07001597 /*
1598 * If null, check cast failed - punt to the interpreter. Because
1599 * interpreter will be the one throwing, we don't need to
1600 * genExportPC() here.
1601 */
Bill Buzbee270c1d62009-08-13 16:58:07 -07001602 genZeroCheck(cUnit, r0, mir->offset, NULL);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001603 /* check cast passed - branch target here */
Bill Buzbee1465db52009-09-23 17:17:35 -07001604 ArmLIR *target = newLIR0(cUnit, kArmPseudoTargetLabel);
Ben Chengd7d426a2009-09-22 11:23:36 -07001605 target->defMask = ENCODE_ALL;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001606 branch1->generic.target = (LIR *)target;
1607 branch2->generic.target = (LIR *)target;
1608 break;
1609 }
1610 default:
1611 return true;
1612 }
1613 return false;
1614}
1615
1616static bool handleFmt11x(CompilationUnit *cUnit, MIR *mir)
1617{
1618 OpCode dalvikOpCode = mir->dalvikInsn.opCode;
Bill Buzbee1465db52009-09-23 17:17:35 -07001619 RegLocation rlResult;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001620 switch (dalvikOpCode) {
1621 case OP_MOVE_EXCEPTION: {
1622 int offset = offsetof(InterpState, self);
1623 int exOffset = offsetof(Thread, exception);
Bill Buzbeec6f10662010-02-09 11:16:15 -08001624 int selfReg = dvmCompilerAllocTemp(cUnit);
1625 int resetReg = dvmCompilerAllocTemp(cUnit);
1626 RegLocation rlDest = dvmCompilerGetDest(cUnit, mir, 0);
1627 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Bill Buzbee1465db52009-09-23 17:17:35 -07001628 loadWordDisp(cUnit, rGLUE, offset, selfReg);
Bill Buzbeef9f33282009-11-22 12:45:30 -08001629 loadConstant(cUnit, resetReg, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07001630 loadWordDisp(cUnit, selfReg, exOffset, rlResult.lowReg);
Bill Buzbeef9f33282009-11-22 12:45:30 -08001631 storeWordDisp(cUnit, selfReg, exOffset, resetReg);
Bill Buzbee1465db52009-09-23 17:17:35 -07001632 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001633 break;
1634 }
1635 case OP_MOVE_RESULT:
1636 case OP_MOVE_RESULT_OBJECT: {
Bill Buzbeec6f10662010-02-09 11:16:15 -08001637 RegLocation rlDest = dvmCompilerGetDest(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07001638 RegLocation rlSrc = LOC_DALVIK_RETURN_VAL;
1639 rlSrc.fp = rlDest.fp;
1640 storeValue(cUnit, rlDest, rlSrc);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001641 break;
1642 }
1643 case OP_MOVE_RESULT_WIDE: {
Bill Buzbeec6f10662010-02-09 11:16:15 -08001644 RegLocation rlDest = dvmCompilerGetDestWide(cUnit, mir, 0, 1);
Bill Buzbee1465db52009-09-23 17:17:35 -07001645 RegLocation rlSrc = LOC_DALVIK_RETURN_VAL_WIDE;
1646 rlSrc.fp = rlDest.fp;
1647 storeValueWide(cUnit, rlDest, rlSrc);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001648 break;
1649 }
1650 case OP_RETURN_WIDE: {
Bill Buzbeec6f10662010-02-09 11:16:15 -08001651 RegLocation rlSrc = dvmCompilerGetSrcWide(cUnit, mir, 0, 1);
Bill Buzbee1465db52009-09-23 17:17:35 -07001652 RegLocation rlDest = LOC_DALVIK_RETURN_VAL_WIDE;
1653 rlDest.fp = rlSrc.fp;
1654 storeValueWide(cUnit, rlDest, rlSrc);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001655 genReturnCommon(cUnit,mir);
1656 break;
1657 }
1658 case OP_RETURN:
1659 case OP_RETURN_OBJECT: {
Bill Buzbeec6f10662010-02-09 11:16:15 -08001660 RegLocation rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07001661 RegLocation rlDest = LOC_DALVIK_RETURN_VAL;
1662 rlDest.fp = rlSrc.fp;
1663 storeValue(cUnit, rlDest, rlSrc);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001664 genReturnCommon(cUnit,mir);
1665 break;
1666 }
Bill Buzbee1465db52009-09-23 17:17:35 -07001667 case OP_MONITOR_EXIT:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001668 case OP_MONITOR_ENTER:
Bill Buzbeed0937ef2009-12-22 16:15:39 -08001669#if defined(WITH_DEADLOCK_PREDICTION) || defined(WITH_MONITOR_TRACKING)
Ben Cheng5d90c202009-11-22 23:31:11 -08001670 genMonitorPortable(cUnit, mir);
Bill Buzbee1465db52009-09-23 17:17:35 -07001671#else
Ben Cheng5d90c202009-11-22 23:31:11 -08001672 genMonitor(cUnit, mir);
Bill Buzbee1465db52009-09-23 17:17:35 -07001673#endif
Ben Chengba4fc8b2009-06-01 13:00:29 -07001674 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001675 case OP_THROW: {
1676 genInterpSingleStep(cUnit, mir);
1677 break;
1678 }
1679 default:
1680 return true;
1681 }
1682 return false;
1683}
1684
Bill Buzbeed45ba372009-06-15 17:00:57 -07001685static bool handleFmt12x(CompilationUnit *cUnit, MIR *mir)
1686{
1687 OpCode opCode = mir->dalvikInsn.opCode;
Bill Buzbee1465db52009-09-23 17:17:35 -07001688 RegLocation rlDest;
1689 RegLocation rlSrc;
1690 RegLocation rlResult;
Bill Buzbeed45ba372009-06-15 17:00:57 -07001691
Ben Chengba4fc8b2009-06-01 13:00:29 -07001692 if ( (opCode >= OP_ADD_INT_2ADDR) && (opCode <= OP_REM_DOUBLE_2ADDR)) {
Ben Cheng5d90c202009-11-22 23:31:11 -08001693 return genArithOp( cUnit, mir );
Ben Chengba4fc8b2009-06-01 13:00:29 -07001694 }
1695
Bill Buzbee1465db52009-09-23 17:17:35 -07001696 if (mir->ssaRep->numUses == 2)
Bill Buzbeec6f10662010-02-09 11:16:15 -08001697 rlSrc = dvmCompilerGetSrcWide(cUnit, mir, 0, 1);
Bill Buzbee1465db52009-09-23 17:17:35 -07001698 else
Bill Buzbeec6f10662010-02-09 11:16:15 -08001699 rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07001700 if (mir->ssaRep->numDefs == 2)
Bill Buzbeec6f10662010-02-09 11:16:15 -08001701 rlDest = dvmCompilerGetDestWide(cUnit, mir, 0, 1);
Bill Buzbee1465db52009-09-23 17:17:35 -07001702 else
Bill Buzbeec6f10662010-02-09 11:16:15 -08001703 rlDest = dvmCompilerGetDest(cUnit, mir, 0);
Ben Chenge9695e52009-06-16 16:11:47 -07001704
Ben Chengba4fc8b2009-06-01 13:00:29 -07001705 switch (opCode) {
Bill Buzbee1465db52009-09-23 17:17:35 -07001706 case OP_DOUBLE_TO_INT:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001707 case OP_INT_TO_FLOAT:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001708 case OP_FLOAT_TO_INT:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001709 case OP_DOUBLE_TO_FLOAT:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001710 case OP_FLOAT_TO_DOUBLE:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001711 case OP_INT_TO_DOUBLE:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001712 case OP_FLOAT_TO_LONG:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001713 case OP_LONG_TO_FLOAT:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001714 case OP_DOUBLE_TO_LONG:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001715 case OP_LONG_TO_DOUBLE:
Ben Cheng5d90c202009-11-22 23:31:11 -08001716 return genConversion(cUnit, mir);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001717 case OP_NEG_INT:
1718 case OP_NOT_INT:
Ben Cheng5d90c202009-11-22 23:31:11 -08001719 return genArithOpInt(cUnit, mir, rlDest, rlSrc, rlSrc);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001720 case OP_NEG_LONG:
1721 case OP_NOT_LONG:
Ben Cheng5d90c202009-11-22 23:31:11 -08001722 return genArithOpLong(cUnit, mir, rlDest, rlSrc, rlSrc);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001723 case OP_NEG_FLOAT:
Ben Cheng5d90c202009-11-22 23:31:11 -08001724 return genArithOpFloat(cUnit, mir, rlDest, rlSrc, rlSrc);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001725 case OP_NEG_DOUBLE:
Ben Cheng5d90c202009-11-22 23:31:11 -08001726 return genArithOpDouble(cUnit, mir, rlDest, rlSrc, rlSrc);
Bill Buzbee1465db52009-09-23 17:17:35 -07001727 case OP_MOVE_WIDE:
1728 storeValueWide(cUnit, rlDest, rlSrc);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001729 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07001730 case OP_INT_TO_LONG:
Bill Buzbeec6f10662010-02-09 11:16:15 -08001731 rlSrc = dvmCompilerUpdateLoc(cUnit, rlSrc);
1732 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Bill Buzbee964a7b02010-01-28 12:54:19 -08001733 //TUNING: shouldn't loadValueDirect already check for phys reg?
Bill Buzbee1465db52009-09-23 17:17:35 -07001734 if (rlSrc.location == kLocPhysReg) {
1735 genRegCopy(cUnit, rlResult.lowReg, rlSrc.lowReg);
1736 } else {
1737 loadValueDirect(cUnit, rlSrc, rlResult.lowReg);
1738 }
1739 opRegRegImm(cUnit, kOpAsr, rlResult.highReg,
1740 rlResult.lowReg, 31);
1741 storeValueWide(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001742 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07001743 case OP_LONG_TO_INT:
Bill Buzbeec6f10662010-02-09 11:16:15 -08001744 rlSrc = dvmCompilerUpdateLocWide(cUnit, rlSrc);
1745 rlSrc = dvmCompilerWideToNarrow(cUnit, rlSrc);
Bill Buzbee1465db52009-09-23 17:17:35 -07001746 // Intentional fallthrough
Ben Chengba4fc8b2009-06-01 13:00:29 -07001747 case OP_MOVE:
1748 case OP_MOVE_OBJECT:
Bill Buzbee1465db52009-09-23 17:17:35 -07001749 storeValue(cUnit, rlDest, rlSrc);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001750 break;
1751 case OP_INT_TO_BYTE:
Bill Buzbee1465db52009-09-23 17:17:35 -07001752 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
Bill Buzbeec6f10662010-02-09 11:16:15 -08001753 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Bill Buzbee1465db52009-09-23 17:17:35 -07001754 opRegReg(cUnit, kOp2Byte, rlResult.lowReg, rlSrc.lowReg);
1755 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001756 break;
1757 case OP_INT_TO_SHORT:
Bill Buzbee1465db52009-09-23 17:17:35 -07001758 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
Bill Buzbeec6f10662010-02-09 11:16:15 -08001759 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Bill Buzbee1465db52009-09-23 17:17:35 -07001760 opRegReg(cUnit, kOp2Short, rlResult.lowReg, rlSrc.lowReg);
1761 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001762 break;
1763 case OP_INT_TO_CHAR:
Bill Buzbee1465db52009-09-23 17:17:35 -07001764 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
Bill Buzbeec6f10662010-02-09 11:16:15 -08001765 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Bill Buzbee1465db52009-09-23 17:17:35 -07001766 opRegReg(cUnit, kOp2Char, rlResult.lowReg, rlSrc.lowReg);
1767 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001768 break;
1769 case OP_ARRAY_LENGTH: {
1770 int lenOffset = offsetof(ArrayObject, length);
Bill Buzbee1465db52009-09-23 17:17:35 -07001771 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
1772 genNullCheck(cUnit, rlSrc.sRegLow, rlSrc.lowReg,
1773 mir->offset, NULL);
Bill Buzbeec6f10662010-02-09 11:16:15 -08001774 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Bill Buzbee1465db52009-09-23 17:17:35 -07001775 loadWordDisp(cUnit, rlSrc.lowReg, lenOffset,
1776 rlResult.lowReg);
1777 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001778 break;
1779 }
1780 default:
1781 return true;
1782 }
1783 return false;
1784}
1785
1786static bool handleFmt21s(CompilationUnit *cUnit, MIR *mir)
1787{
1788 OpCode dalvikOpCode = mir->dalvikInsn.opCode;
Bill Buzbee1465db52009-09-23 17:17:35 -07001789 RegLocation rlDest;
1790 RegLocation rlResult;
1791 int BBBB = mir->dalvikInsn.vB;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001792 if (dalvikOpCode == OP_CONST_WIDE_16) {
Bill Buzbeec6f10662010-02-09 11:16:15 -08001793 rlDest = dvmCompilerGetDestWide(cUnit, mir, 0, 1);
1794 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Ben Chengbd1326d2010-04-02 15:04:53 -07001795 loadConstantNoClobber(cUnit, rlResult.lowReg, BBBB);
Bill Buzbee964a7b02010-01-28 12:54:19 -08001796 //TUNING: do high separately to avoid load dependency
Bill Buzbee1465db52009-09-23 17:17:35 -07001797 opRegRegImm(cUnit, kOpAsr, rlResult.highReg, rlResult.lowReg, 31);
1798 storeValueWide(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001799 } else if (dalvikOpCode == OP_CONST_16) {
Bill Buzbeec6f10662010-02-09 11:16:15 -08001800 rlDest = dvmCompilerGetDest(cUnit, mir, 0);
1801 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kAnyReg, true);
Ben Chengbd1326d2010-04-02 15:04:53 -07001802 loadConstantNoClobber(cUnit, rlResult.lowReg, BBBB);
Bill Buzbee1465db52009-09-23 17:17:35 -07001803 storeValue(cUnit, rlDest, rlResult);
1804 } else
Ben Chengba4fc8b2009-06-01 13:00:29 -07001805 return true;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001806 return false;
1807}
1808
1809/* Compare agaist zero */
1810static bool handleFmt21t(CompilationUnit *cUnit, MIR *mir, BasicBlock *bb,
Bill Buzbee89efc3d2009-07-28 11:22:22 -07001811 ArmLIR *labelList)
Ben Chengba4fc8b2009-06-01 13:00:29 -07001812{
1813 OpCode dalvikOpCode = mir->dalvikInsn.opCode;
Bill Buzbee89efc3d2009-07-28 11:22:22 -07001814 ArmConditionCode cond;
Bill Buzbeec6f10662010-02-09 11:16:15 -08001815 RegLocation rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07001816 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
1817 opRegImm(cUnit, kOpCmp, rlSrc.lowReg, 0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001818
Bill Buzbee270c1d62009-08-13 16:58:07 -07001819//TUNING: break this out to allow use of Thumb2 CB[N]Z
Ben Chengba4fc8b2009-06-01 13:00:29 -07001820 switch (dalvikOpCode) {
1821 case OP_IF_EQZ:
Bill Buzbee1465db52009-09-23 17:17:35 -07001822 cond = kArmCondEq;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001823 break;
1824 case OP_IF_NEZ:
Bill Buzbee1465db52009-09-23 17:17:35 -07001825 cond = kArmCondNe;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001826 break;
1827 case OP_IF_LTZ:
Bill Buzbee1465db52009-09-23 17:17:35 -07001828 cond = kArmCondLt;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001829 break;
1830 case OP_IF_GEZ:
Bill Buzbee1465db52009-09-23 17:17:35 -07001831 cond = kArmCondGe;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001832 break;
1833 case OP_IF_GTZ:
Bill Buzbee1465db52009-09-23 17:17:35 -07001834 cond = kArmCondGt;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001835 break;
1836 case OP_IF_LEZ:
Bill Buzbee1465db52009-09-23 17:17:35 -07001837 cond = kArmCondLe;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001838 break;
1839 default:
1840 cond = 0;
1841 LOGE("Unexpected opcode (%d) for Fmt21t\n", dalvikOpCode);
Bill Buzbeefc519dc2010-03-06 23:30:57 -08001842 dvmCompilerAbort(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001843 }
1844 genConditionalBranch(cUnit, cond, &labelList[bb->taken->id]);
1845 /* This mostly likely will be optimized away in a later phase */
1846 genUnconditionalBranch(cUnit, &labelList[bb->fallThrough->id]);
1847 return false;
1848}
1849
Elliott Hughesb4c05972010-02-24 16:36:18 -08001850static bool isPowerOfTwo(int x)
1851{
1852 return (x & (x - 1)) == 0;
1853}
1854
1855// Returns true if no more than two bits are set in 'x'.
1856static bool isPopCountLE2(unsigned int x)
1857{
1858 x &= x - 1;
1859 return (x & (x - 1)) == 0;
1860}
1861
1862// Returns the index of the lowest set bit in 'x'.
1863static int lowestSetBit(unsigned int x) {
1864 int bit_posn = 0;
1865 while ((x & 0xf) == 0) {
1866 bit_posn += 4;
1867 x >>= 4;
Bill Buzbee78cb0e22010-02-11 14:04:53 -08001868 }
Elliott Hughesb4c05972010-02-24 16:36:18 -08001869 while ((x & 1) == 0) {
1870 bit_posn++;
1871 x >>= 1;
1872 }
1873 return bit_posn;
1874}
1875
Elliott Hughes672511b2010-04-26 17:40:13 -07001876// Returns true if it added instructions to 'cUnit' to divide 'rlSrc' by 'lit'
1877// and store the result in 'rlDest'.
Elliott Hughesc7ad9b22010-04-28 13:52:02 -07001878static bool handleEasyDivide(CompilationUnit *cUnit, OpCode dalvikOpCode,
Elliott Hughes672511b2010-04-26 17:40:13 -07001879 RegLocation rlSrc, RegLocation rlDest, int lit)
1880{
1881 if (lit < 2 || !isPowerOfTwo(lit)) {
1882 return false;
1883 }
1884 int k = lowestSetBit(lit);
1885 if (k >= 30) {
1886 // Avoid special cases.
1887 return false;
1888 }
Elliott Hughes9c457022010-04-28 16:15:38 -07001889 bool div = (dalvikOpCode == OP_DIV_INT_LIT8 || dalvikOpCode == OP_DIV_INT_LIT16);
Elliott Hughes672511b2010-04-26 17:40:13 -07001890 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
1891 RegLocation rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Elliott Hughes9c457022010-04-28 16:15:38 -07001892 if (div) {
1893 int tReg = dvmCompilerAllocTemp(cUnit);
1894 if (lit == 2) {
1895 // Division by 2 is by far the most common division by constant.
1896 opRegRegImm(cUnit, kOpLsr, tReg, rlSrc.lowReg, 32 - k);
1897 opRegRegReg(cUnit, kOpAdd, tReg, tReg, rlSrc.lowReg);
1898 opRegRegImm(cUnit, kOpAsr, rlResult.lowReg, tReg, k);
1899 } else {
1900 opRegRegImm(cUnit, kOpAsr, tReg, rlSrc.lowReg, 31);
1901 opRegRegImm(cUnit, kOpLsr, tReg, tReg, 32 - k);
1902 opRegRegReg(cUnit, kOpAdd, tReg, tReg, rlSrc.lowReg);
1903 opRegRegImm(cUnit, kOpAsr, rlResult.lowReg, tReg, k);
1904 }
Elliott Hughes672511b2010-04-26 17:40:13 -07001905 } else {
Elliott Hughes9c457022010-04-28 16:15:38 -07001906 int cReg = dvmCompilerAllocTemp(cUnit);
1907 loadConstant(cUnit, cReg, lit - 1);
1908 int tReg1 = dvmCompilerAllocTemp(cUnit);
1909 int tReg2 = dvmCompilerAllocTemp(cUnit);
1910 if (lit == 2) {
1911 opRegRegImm(cUnit, kOpLsr, tReg1, rlSrc.lowReg, 32 - k);
1912 opRegRegReg(cUnit, kOpAdd, tReg2, tReg1, rlSrc.lowReg);
1913 opRegRegReg(cUnit, kOpAnd, tReg2, tReg2, cReg);
1914 opRegRegReg(cUnit, kOpSub, rlResult.lowReg, tReg2, tReg1);
1915 } else {
1916 opRegRegImm(cUnit, kOpAsr, tReg1, rlSrc.lowReg, 31);
1917 opRegRegImm(cUnit, kOpLsr, tReg1, tReg1, 32 - k);
1918 opRegRegReg(cUnit, kOpAdd, tReg2, tReg1, rlSrc.lowReg);
1919 opRegRegReg(cUnit, kOpAnd, tReg2, tReg2, cReg);
1920 opRegRegReg(cUnit, kOpSub, rlResult.lowReg, tReg2, tReg1);
1921 }
Elliott Hughes672511b2010-04-26 17:40:13 -07001922 }
1923 storeValue(cUnit, rlDest, rlResult);
1924 return true;
1925}
1926
Elliott Hughesb4c05972010-02-24 16:36:18 -08001927// Returns true if it added instructions to 'cUnit' to multiply 'rlSrc' by 'lit'
1928// and store the result in 'rlDest'.
1929static bool handleEasyMultiply(CompilationUnit *cUnit,
1930 RegLocation rlSrc, RegLocation rlDest, int lit)
1931{
1932 // Can we simplify this multiplication?
1933 bool powerOfTwo = false;
1934 bool popCountLE2 = false;
1935 bool powerOfTwoMinusOne = false;
1936 if (lit < 2) {
1937 // Avoid special cases.
1938 return false;
1939 } else if (isPowerOfTwo(lit)) {
1940 powerOfTwo = true;
1941 } else if (isPopCountLE2(lit)) {
1942 popCountLE2 = true;
1943 } else if (isPowerOfTwo(lit + 1)) {
1944 powerOfTwoMinusOne = true;
1945 } else {
1946 return false;
1947 }
1948 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
1949 RegLocation rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
1950 if (powerOfTwo) {
1951 // Shift.
1952 opRegRegImm(cUnit, kOpLsl, rlResult.lowReg, rlSrc.lowReg,
1953 lowestSetBit(lit));
1954 } else if (popCountLE2) {
1955 // Shift and add and shift.
1956 int firstBit = lowestSetBit(lit);
1957 int secondBit = lowestSetBit(lit ^ (1 << firstBit));
1958 genMultiplyByTwoBitMultiplier(cUnit, rlSrc, rlResult, lit,
1959 firstBit, secondBit);
1960 } else {
1961 // Reverse subtract: (src << (shift + 1)) - src.
1962 assert(powerOfTwoMinusOne);
1963 // TODO: rsb dst, src, src lsl#lowestSetBit(lit + 1)
1964 int tReg = dvmCompilerAllocTemp(cUnit);
1965 opRegRegImm(cUnit, kOpLsl, tReg, rlSrc.lowReg, lowestSetBit(lit + 1));
1966 opRegRegReg(cUnit, kOpSub, rlResult.lowReg, tReg, rlSrc.lowReg);
1967 }
1968 storeValue(cUnit, rlDest, rlResult);
1969 return true;
Bill Buzbee78cb0e22010-02-11 14:04:53 -08001970}
1971
Ben Chengba4fc8b2009-06-01 13:00:29 -07001972static bool handleFmt22b_Fmt22s(CompilationUnit *cUnit, MIR *mir)
1973{
1974 OpCode dalvikOpCode = mir->dalvikInsn.opCode;
Bill Buzbeec6f10662010-02-09 11:16:15 -08001975 RegLocation rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
1976 RegLocation rlDest = dvmCompilerGetDest(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07001977 RegLocation rlResult;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001978 int lit = mir->dalvikInsn.vC;
Ben Cheng4f489172009-09-27 17:08:35 -07001979 OpKind op = 0; /* Make gcc happy */
Bill Buzbee1465db52009-09-23 17:17:35 -07001980 int shiftOp = false;
1981 bool isDiv = false;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001982
Ben Chengba4fc8b2009-06-01 13:00:29 -07001983 switch (dalvikOpCode) {
Bill Buzbee1465db52009-09-23 17:17:35 -07001984 case OP_RSUB_INT_LIT8:
1985 case OP_RSUB_INT: {
1986 int tReg;
1987 //TUNING: add support for use of Arm rsub op
1988 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
Bill Buzbeec6f10662010-02-09 11:16:15 -08001989 tReg = dvmCompilerAllocTemp(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07001990 loadConstant(cUnit, tReg, lit);
Bill Buzbeec6f10662010-02-09 11:16:15 -08001991 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Bill Buzbee1465db52009-09-23 17:17:35 -07001992 opRegRegReg(cUnit, kOpSub, rlResult.lowReg,
1993 tReg, rlSrc.lowReg);
1994 storeValue(cUnit, rlDest, rlResult);
1995 return false;
1996 break;
1997 }
1998
Ben Chengba4fc8b2009-06-01 13:00:29 -07001999 case OP_ADD_INT_LIT8:
2000 case OP_ADD_INT_LIT16:
Bill Buzbee1465db52009-09-23 17:17:35 -07002001 op = kOpAdd;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002002 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002003 case OP_MUL_INT_LIT8:
Bill Buzbee78cb0e22010-02-11 14:04:53 -08002004 case OP_MUL_INT_LIT16: {
Elliott Hughesb4c05972010-02-24 16:36:18 -08002005 if (handleEasyMultiply(cUnit, rlSrc, rlDest, lit)) {
2006 return false;
Bill Buzbee78cb0e22010-02-11 14:04:53 -08002007 }
Elliott Hughesb4c05972010-02-24 16:36:18 -08002008 op = kOpMul;
Bill Buzbee1465db52009-09-23 17:17:35 -07002009 break;
Bill Buzbee78cb0e22010-02-11 14:04:53 -08002010 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07002011 case OP_AND_INT_LIT8:
2012 case OP_AND_INT_LIT16:
Bill Buzbee1465db52009-09-23 17:17:35 -07002013 op = kOpAnd;
2014 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002015 case OP_OR_INT_LIT8:
2016 case OP_OR_INT_LIT16:
Bill Buzbee1465db52009-09-23 17:17:35 -07002017 op = kOpOr;
2018 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002019 case OP_XOR_INT_LIT8:
2020 case OP_XOR_INT_LIT16:
Bill Buzbee1465db52009-09-23 17:17:35 -07002021 op = kOpXor;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002022 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002023 case OP_SHL_INT_LIT8:
Bill Buzbee0e605272009-12-01 14:28:05 -08002024 lit &= 31;
Bill Buzbee1465db52009-09-23 17:17:35 -07002025 shiftOp = true;
2026 op = kOpLsl;
2027 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002028 case OP_SHR_INT_LIT8:
Bill Buzbee0e605272009-12-01 14:28:05 -08002029 lit &= 31;
Bill Buzbee1465db52009-09-23 17:17:35 -07002030 shiftOp = true;
2031 op = kOpAsr;
2032 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002033 case OP_USHR_INT_LIT8:
Bill Buzbee0e605272009-12-01 14:28:05 -08002034 lit &= 31;
Bill Buzbee1465db52009-09-23 17:17:35 -07002035 shiftOp = true;
2036 op = kOpLsr;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002037 break;
2038
2039 case OP_DIV_INT_LIT8:
2040 case OP_DIV_INT_LIT16:
Ben Chengba4fc8b2009-06-01 13:00:29 -07002041 case OP_REM_INT_LIT8:
2042 case OP_REM_INT_LIT16:
2043 if (lit == 0) {
2044 /* Let the interpreter deal with div by 0 */
2045 genInterpSingleStep(cUnit, mir);
2046 return false;
2047 }
Elliott Hughesc7ad9b22010-04-28 13:52:02 -07002048 if (handleEasyDivide(cUnit, dalvikOpCode, rlSrc, rlDest, lit)) {
Elliott Hughes672511b2010-04-26 17:40:13 -07002049 return false;
2050 }
Bill Buzbeec6f10662010-02-09 11:16:15 -08002051 dvmCompilerFlushAllRegs(cUnit); /* Everything to home location */
Bill Buzbee1465db52009-09-23 17:17:35 -07002052 loadValueDirectFixed(cUnit, rlSrc, r0);
Bill Buzbeec6f10662010-02-09 11:16:15 -08002053 dvmCompilerClobber(cUnit, r0);
Bill Buzbee1465db52009-09-23 17:17:35 -07002054 if ((dalvikOpCode == OP_DIV_INT_LIT8) ||
2055 (dalvikOpCode == OP_DIV_INT_LIT16)) {
Ben Chengbd1326d2010-04-02 15:04:53 -07002056 LOAD_FUNC_ADDR(cUnit, r2, (int)__aeabi_idiv);
Bill Buzbee1465db52009-09-23 17:17:35 -07002057 isDiv = true;
2058 } else {
Ben Chengbd1326d2010-04-02 15:04:53 -07002059 LOAD_FUNC_ADDR(cUnit, r2, (int)__aeabi_idivmod);
Bill Buzbee1465db52009-09-23 17:17:35 -07002060 isDiv = false;
2061 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07002062 loadConstant(cUnit, r1, lit);
Bill Buzbee1465db52009-09-23 17:17:35 -07002063 opReg(cUnit, kOpBlx, r2);
Elliott Hughes6a555132010-02-25 15:41:42 -08002064 dvmCompilerClobberCallRegs(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07002065 if (isDiv)
Bill Buzbeec6f10662010-02-09 11:16:15 -08002066 rlResult = dvmCompilerGetReturn(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07002067 else
Bill Buzbeec6f10662010-02-09 11:16:15 -08002068 rlResult = dvmCompilerGetReturnAlt(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07002069 storeValue(cUnit, rlDest, rlResult);
2070 return false;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002071 break;
2072 default:
2073 return true;
2074 }
Bill Buzbee1465db52009-09-23 17:17:35 -07002075 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
Bill Buzbeec6f10662010-02-09 11:16:15 -08002076 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Bill Buzbee1465db52009-09-23 17:17:35 -07002077 // Avoid shifts by literal 0 - no support in Thumb. Change to copy
2078 if (shiftOp && (lit == 0)) {
2079 genRegCopy(cUnit, rlResult.lowReg, rlSrc.lowReg);
2080 } else {
2081 opRegRegImm(cUnit, op, rlResult.lowReg, rlSrc.lowReg, lit);
2082 }
2083 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002084 return false;
2085}
2086
2087static bool handleFmt22c(CompilationUnit *cUnit, MIR *mir)
2088{
2089 OpCode dalvikOpCode = mir->dalvikInsn.opCode;
2090 int fieldOffset;
2091
2092 if (dalvikOpCode >= OP_IGET && dalvikOpCode <= OP_IPUT_SHORT) {
2093 InstField *pInstField = (InstField *)
2094 cUnit->method->clazz->pDvmDex->pResFields[mir->dalvikInsn.vC];
Ben Chengba4fc8b2009-06-01 13:00:29 -07002095
Ben Chengdd6e8702010-05-07 13:05:47 -07002096 if (pInstField == NULL) {
2097 LOGE("Unexpected null instance field");
2098 dvmAbort();
2099 }
2100
Ben Chengba4fc8b2009-06-01 13:00:29 -07002101 fieldOffset = pInstField->byteOffset;
2102 } else {
Ben Chenga0e7b602009-10-13 23:09:01 -07002103 /* Deliberately break the code while make the compiler happy */
2104 fieldOffset = -1;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002105 }
2106 switch (dalvikOpCode) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07002107 case OP_NEW_ARRAY: {
Bill Buzbee1465db52009-09-23 17:17:35 -07002108 // Generates a call - use explicit registers
Bill Buzbeec6f10662010-02-09 11:16:15 -08002109 RegLocation rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
2110 RegLocation rlDest = dvmCompilerGetDest(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07002111 RegLocation rlResult;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002112 void *classPtr = (void*)
2113 (cUnit->method->clazz->pDvmDex->pResClasses[mir->dalvikInsn.vC]);
Ben Chengdd6e8702010-05-07 13:05:47 -07002114
2115 if (classPtr == NULL) {
2116 LOGE("Unexpected null class");
2117 dvmAbort();
2118 }
2119
Bill Buzbeec6f10662010-02-09 11:16:15 -08002120 dvmCompilerFlushAllRegs(cUnit); /* Everything to home location */
Bill Buzbee1465db52009-09-23 17:17:35 -07002121 genExportPC(cUnit, mir);
2122 loadValueDirectFixed(cUnit, rlSrc, r1); /* Len */
Ben Chengba4fc8b2009-06-01 13:00:29 -07002123 loadConstant(cUnit, r0, (int) classPtr );
Ben Chengbd1326d2010-04-02 15:04:53 -07002124 LOAD_FUNC_ADDR(cUnit, r3, (int)dvmAllocArrayByClass);
Ben Cheng4f489172009-09-27 17:08:35 -07002125 /*
2126 * "len < 0": bail to the interpreter to re-execute the
2127 * instruction
2128 */
Carl Shapiroe3c01da2010-05-20 22:54:18 -07002129 genRegImmCheck(cUnit, kArmCondMi, r1, 0, mir->offset, NULL);
Bill Buzbee270c1d62009-08-13 16:58:07 -07002130 loadConstant(cUnit, r2, ALLOC_DONT_TRACK);
Bill Buzbee1465db52009-09-23 17:17:35 -07002131 opReg(cUnit, kOpBlx, r3);
Elliott Hughes6a555132010-02-25 15:41:42 -08002132 dvmCompilerClobberCallRegs(cUnit);
Ben Cheng4f489172009-09-27 17:08:35 -07002133 /* generate a branch over if allocation is successful */
Bill Buzbee1465db52009-09-23 17:17:35 -07002134 opRegImm(cUnit, kOpCmp, r0, 0); /* NULL? */
2135 ArmLIR *branchOver = opCondBranch(cUnit, kArmCondNe);
Ben Cheng4f489172009-09-27 17:08:35 -07002136 /*
2137 * OOM exception needs to be thrown here and cannot re-execute
2138 */
2139 loadConstant(cUnit, r0,
2140 (int) (cUnit->method->insns + mir->offset));
2141 genDispatchToHandler(cUnit, TEMPLATE_THROW_EXCEPTION_COMMON);
2142 /* noreturn */
2143
Bill Buzbee1465db52009-09-23 17:17:35 -07002144 ArmLIR *target = newLIR0(cUnit, kArmPseudoTargetLabel);
Ben Cheng4f489172009-09-27 17:08:35 -07002145 target->defMask = ENCODE_ALL;
2146 branchOver->generic.target = (LIR *) target;
Bill Buzbeec6f10662010-02-09 11:16:15 -08002147 rlResult = dvmCompilerGetReturn(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07002148 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002149 break;
2150 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07002151 case OP_INSTANCE_OF: {
Bill Buzbee1465db52009-09-23 17:17:35 -07002152 // May generate a call - use explicit registers
Bill Buzbeec6f10662010-02-09 11:16:15 -08002153 RegLocation rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
2154 RegLocation rlDest = dvmCompilerGetDest(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07002155 RegLocation rlResult;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002156 ClassObject *classPtr =
2157 (cUnit->method->clazz->pDvmDex->pResClasses[mir->dalvikInsn.vC]);
Bill Buzbee480e6782010-01-27 15:43:08 -08002158 /*
2159 * Note: It is possible that classPtr is NULL at this point,
2160 * even though this instruction has been successfully interpreted.
2161 * If the previous interpretation had a null source, the
2162 * interpreter would not have bothered to resolve the clazz.
2163 * Bail out to the interpreter in this case, and log it
2164 * so that we can tell if it happens frequently.
2165 */
2166 if (classPtr == NULL) {
2167 LOGD("null clazz in OP_INSTANCE_OF, single-stepping");
2168 genInterpSingleStep(cUnit, mir);
2169 break;
2170 }
Bill Buzbeec6f10662010-02-09 11:16:15 -08002171 dvmCompilerFlushAllRegs(cUnit); /* Everything to home location */
Bill Buzbee1465db52009-09-23 17:17:35 -07002172 loadValueDirectFixed(cUnit, rlSrc, r0); /* Ref */
Ben Chengba4fc8b2009-06-01 13:00:29 -07002173 loadConstant(cUnit, r2, (int) classPtr );
Bill Buzbee270c1d62009-08-13 16:58:07 -07002174//TUNING: compare to 0 primative to allow use of CB[N]Z
Bill Buzbee1465db52009-09-23 17:17:35 -07002175 opRegImm(cUnit, kOpCmp, r0, 0); /* NULL? */
Ben Cheng752c7942009-06-22 10:50:07 -07002176 /* When taken r0 has NULL which can be used for store directly */
Bill Buzbee1465db52009-09-23 17:17:35 -07002177 ArmLIR *branch1 = opCondBranch(cUnit, kArmCondEq);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002178 /* r1 now contains object->clazz */
Bill Buzbee270c1d62009-08-13 16:58:07 -07002179 loadWordDisp(cUnit, r0, offsetof(Object, clazz), r1);
Bill Buzbee1465db52009-09-23 17:17:35 -07002180 /* r1 now contains object->clazz */
Ben Chengbd1326d2010-04-02 15:04:53 -07002181 LOAD_FUNC_ADDR(cUnit, r3, (int)dvmInstanceofNonTrivial);
Ben Cheng752c7942009-06-22 10:50:07 -07002182 loadConstant(cUnit, r0, 1); /* Assume true */
Bill Buzbee1465db52009-09-23 17:17:35 -07002183 opRegReg(cUnit, kOpCmp, r1, r2);
2184 ArmLIR *branch2 = opCondBranch(cUnit, kArmCondEq);
2185 genRegCopy(cUnit, r0, r1);
2186 genRegCopy(cUnit, r1, r2);
2187 opReg(cUnit, kOpBlx, r3);
Elliott Hughes6a555132010-02-25 15:41:42 -08002188 dvmCompilerClobberCallRegs(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002189 /* branch target here */
Bill Buzbee1465db52009-09-23 17:17:35 -07002190 ArmLIR *target = newLIR0(cUnit, kArmPseudoTargetLabel);
Ben Chengd7d426a2009-09-22 11:23:36 -07002191 target->defMask = ENCODE_ALL;
Bill Buzbeec6f10662010-02-09 11:16:15 -08002192 rlResult = dvmCompilerGetReturn(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07002193 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002194 branch1->generic.target = (LIR *)target;
2195 branch2->generic.target = (LIR *)target;
2196 break;
2197 }
2198 case OP_IGET_WIDE:
2199 genIGetWide(cUnit, mir, fieldOffset);
2200 break;
2201 case OP_IGET:
2202 case OP_IGET_OBJECT:
Bill Buzbee1465db52009-09-23 17:17:35 -07002203 genIGet(cUnit, mir, kWord, fieldOffset);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002204 break;
2205 case OP_IGET_BOOLEAN:
Bill Buzbee1465db52009-09-23 17:17:35 -07002206 genIGet(cUnit, mir, kUnsignedByte, fieldOffset);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002207 break;
2208 case OP_IGET_BYTE:
Bill Buzbee1465db52009-09-23 17:17:35 -07002209 genIGet(cUnit, mir, kSignedByte, fieldOffset);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002210 break;
2211 case OP_IGET_CHAR:
Bill Buzbee1465db52009-09-23 17:17:35 -07002212 genIGet(cUnit, mir, kUnsignedHalf, fieldOffset);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002213 break;
2214 case OP_IGET_SHORT:
Bill Buzbee1465db52009-09-23 17:17:35 -07002215 genIGet(cUnit, mir, kSignedHalf, fieldOffset);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002216 break;
2217 case OP_IPUT_WIDE:
2218 genIPutWide(cUnit, mir, fieldOffset);
2219 break;
2220 case OP_IPUT:
2221 case OP_IPUT_OBJECT:
Bill Buzbee1465db52009-09-23 17:17:35 -07002222 genIPut(cUnit, mir, kWord, fieldOffset);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002223 break;
2224 case OP_IPUT_SHORT:
2225 case OP_IPUT_CHAR:
Bill Buzbee1465db52009-09-23 17:17:35 -07002226 genIPut(cUnit, mir, kUnsignedHalf, fieldOffset);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002227 break;
2228 case OP_IPUT_BYTE:
2229 case OP_IPUT_BOOLEAN:
Bill Buzbee1465db52009-09-23 17:17:35 -07002230 genIPut(cUnit, mir, kUnsignedByte, fieldOffset);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002231 break;
Bill Buzbeeb16344a2010-03-15 17:19:12 -07002232 case OP_IGET_WIDE_VOLATILE:
2233 case OP_IPUT_WIDE_VOLATILE:
2234 case OP_SGET_WIDE_VOLATILE:
2235 case OP_SPUT_WIDE_VOLATILE:
2236 genInterpSingleStep(cUnit, mir);
2237 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002238 default:
2239 return true;
2240 }
2241 return false;
2242}
2243
2244static bool handleFmt22cs(CompilationUnit *cUnit, MIR *mir)
2245{
2246 OpCode dalvikOpCode = mir->dalvikInsn.opCode;
2247 int fieldOffset = mir->dalvikInsn.vC;
2248 switch (dalvikOpCode) {
2249 case OP_IGET_QUICK:
2250 case OP_IGET_OBJECT_QUICK:
Bill Buzbee1465db52009-09-23 17:17:35 -07002251 genIGet(cUnit, mir, kWord, fieldOffset);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002252 break;
2253 case OP_IPUT_QUICK:
2254 case OP_IPUT_OBJECT_QUICK:
Bill Buzbee1465db52009-09-23 17:17:35 -07002255 genIPut(cUnit, mir, kWord, fieldOffset);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002256 break;
2257 case OP_IGET_WIDE_QUICK:
2258 genIGetWide(cUnit, mir, fieldOffset);
2259 break;
2260 case OP_IPUT_WIDE_QUICK:
2261 genIPutWide(cUnit, mir, fieldOffset);
2262 break;
2263 default:
2264 return true;
2265 }
2266 return false;
2267
2268}
2269
2270/* Compare agaist zero */
2271static bool handleFmt22t(CompilationUnit *cUnit, MIR *mir, BasicBlock *bb,
Bill Buzbee89efc3d2009-07-28 11:22:22 -07002272 ArmLIR *labelList)
Ben Chengba4fc8b2009-06-01 13:00:29 -07002273{
2274 OpCode dalvikOpCode = mir->dalvikInsn.opCode;
Bill Buzbee89efc3d2009-07-28 11:22:22 -07002275 ArmConditionCode cond;
Bill Buzbeec6f10662010-02-09 11:16:15 -08002276 RegLocation rlSrc1 = dvmCompilerGetSrc(cUnit, mir, 0);
2277 RegLocation rlSrc2 = dvmCompilerGetSrc(cUnit, mir, 1);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002278
Bill Buzbee1465db52009-09-23 17:17:35 -07002279 rlSrc1 = loadValue(cUnit, rlSrc1, kCoreReg);
2280 rlSrc2 = loadValue(cUnit, rlSrc2, kCoreReg);
2281 opRegReg(cUnit, kOpCmp, rlSrc1.lowReg, rlSrc2.lowReg);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002282
2283 switch (dalvikOpCode) {
2284 case OP_IF_EQ:
Bill Buzbee1465db52009-09-23 17:17:35 -07002285 cond = kArmCondEq;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002286 break;
2287 case OP_IF_NE:
Bill Buzbee1465db52009-09-23 17:17:35 -07002288 cond = kArmCondNe;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002289 break;
2290 case OP_IF_LT:
Bill Buzbee1465db52009-09-23 17:17:35 -07002291 cond = kArmCondLt;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002292 break;
2293 case OP_IF_GE:
Bill Buzbee1465db52009-09-23 17:17:35 -07002294 cond = kArmCondGe;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002295 break;
2296 case OP_IF_GT:
Bill Buzbee1465db52009-09-23 17:17:35 -07002297 cond = kArmCondGt;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002298 break;
2299 case OP_IF_LE:
Bill Buzbee1465db52009-09-23 17:17:35 -07002300 cond = kArmCondLe;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002301 break;
2302 default:
2303 cond = 0;
2304 LOGE("Unexpected opcode (%d) for Fmt22t\n", dalvikOpCode);
Bill Buzbeefc519dc2010-03-06 23:30:57 -08002305 dvmCompilerAbort(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002306 }
2307 genConditionalBranch(cUnit, cond, &labelList[bb->taken->id]);
2308 /* This mostly likely will be optimized away in a later phase */
2309 genUnconditionalBranch(cUnit, &labelList[bb->fallThrough->id]);
2310 return false;
2311}
2312
2313static bool handleFmt22x_Fmt32x(CompilationUnit *cUnit, MIR *mir)
2314{
2315 OpCode opCode = mir->dalvikInsn.opCode;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002316
2317 switch (opCode) {
2318 case OP_MOVE_16:
2319 case OP_MOVE_OBJECT_16:
2320 case OP_MOVE_FROM16:
Ben Chenge9695e52009-06-16 16:11:47 -07002321 case OP_MOVE_OBJECT_FROM16: {
Bill Buzbeec6f10662010-02-09 11:16:15 -08002322 storeValue(cUnit, dvmCompilerGetDest(cUnit, mir, 0),
2323 dvmCompilerGetSrc(cUnit, mir, 0));
Ben Chengba4fc8b2009-06-01 13:00:29 -07002324 break;
Ben Chenge9695e52009-06-16 16:11:47 -07002325 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07002326 case OP_MOVE_WIDE_16:
Ben Chenge9695e52009-06-16 16:11:47 -07002327 case OP_MOVE_WIDE_FROM16: {
Bill Buzbeec6f10662010-02-09 11:16:15 -08002328 storeValueWide(cUnit, dvmCompilerGetDestWide(cUnit, mir, 0, 1),
2329 dvmCompilerGetSrcWide(cUnit, mir, 0, 1));
Ben Chengba4fc8b2009-06-01 13:00:29 -07002330 break;
Ben Chenge9695e52009-06-16 16:11:47 -07002331 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07002332 default:
2333 return true;
2334 }
2335 return false;
2336}
2337
2338static bool handleFmt23x(CompilationUnit *cUnit, MIR *mir)
2339{
2340 OpCode opCode = mir->dalvikInsn.opCode;
Bill Buzbee1465db52009-09-23 17:17:35 -07002341 RegLocation rlSrc1;
2342 RegLocation rlSrc2;
2343 RegLocation rlDest;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002344
2345 if ( (opCode >= OP_ADD_INT) && (opCode <= OP_REM_DOUBLE)) {
Ben Cheng5d90c202009-11-22 23:31:11 -08002346 return genArithOp( cUnit, mir );
Ben Chengba4fc8b2009-06-01 13:00:29 -07002347 }
2348
Bill Buzbee1465db52009-09-23 17:17:35 -07002349 /* APUTs have 3 sources and no targets */
2350 if (mir->ssaRep->numDefs == 0) {
2351 if (mir->ssaRep->numUses == 3) {
Bill Buzbeec6f10662010-02-09 11:16:15 -08002352 rlDest = dvmCompilerGetSrc(cUnit, mir, 0);
2353 rlSrc1 = dvmCompilerGetSrc(cUnit, mir, 1);
2354 rlSrc2 = dvmCompilerGetSrc(cUnit, mir, 2);
Bill Buzbee1465db52009-09-23 17:17:35 -07002355 } else {
2356 assert(mir->ssaRep->numUses == 4);
Bill Buzbeec6f10662010-02-09 11:16:15 -08002357 rlDest = dvmCompilerGetSrcWide(cUnit, mir, 0, 1);
2358 rlSrc1 = dvmCompilerGetSrc(cUnit, mir, 2);
2359 rlSrc2 = dvmCompilerGetSrc(cUnit, mir, 3);
Bill Buzbee1465db52009-09-23 17:17:35 -07002360 }
2361 } else {
2362 /* Two sources and 1 dest. Deduce the operand sizes */
2363 if (mir->ssaRep->numUses == 4) {
Bill Buzbeec6f10662010-02-09 11:16:15 -08002364 rlSrc1 = dvmCompilerGetSrcWide(cUnit, mir, 0, 1);
2365 rlSrc2 = dvmCompilerGetSrcWide(cUnit, mir, 2, 3);
Bill Buzbee1465db52009-09-23 17:17:35 -07002366 } else {
2367 assert(mir->ssaRep->numUses == 2);
Bill Buzbeec6f10662010-02-09 11:16:15 -08002368 rlSrc1 = dvmCompilerGetSrc(cUnit, mir, 0);
2369 rlSrc2 = dvmCompilerGetSrc(cUnit, mir, 1);
Bill Buzbee1465db52009-09-23 17:17:35 -07002370 }
2371 if (mir->ssaRep->numDefs == 2) {
Bill Buzbeec6f10662010-02-09 11:16:15 -08002372 rlDest = dvmCompilerGetDestWide(cUnit, mir, 0, 1);
Bill Buzbee1465db52009-09-23 17:17:35 -07002373 } else {
2374 assert(mir->ssaRep->numDefs == 1);
Bill Buzbeec6f10662010-02-09 11:16:15 -08002375 rlDest = dvmCompilerGetDest(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07002376 }
2377 }
2378
2379
Ben Chengba4fc8b2009-06-01 13:00:29 -07002380 switch (opCode) {
Bill Buzbeed45ba372009-06-15 17:00:57 -07002381 case OP_CMPL_FLOAT:
2382 case OP_CMPG_FLOAT:
2383 case OP_CMPL_DOUBLE:
2384 case OP_CMPG_DOUBLE:
Ben Cheng5d90c202009-11-22 23:31:11 -08002385 return genCmpFP(cUnit, mir, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002386 case OP_CMP_LONG:
Bill Buzbee1465db52009-09-23 17:17:35 -07002387 genCmpLong(cUnit, mir, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002388 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002389 case OP_AGET_WIDE:
Bill Buzbee1465db52009-09-23 17:17:35 -07002390 genArrayGet(cUnit, mir, kLong, rlSrc1, rlSrc2, rlDest, 3);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002391 break;
2392 case OP_AGET:
2393 case OP_AGET_OBJECT:
Bill Buzbee1465db52009-09-23 17:17:35 -07002394 genArrayGet(cUnit, mir, kWord, rlSrc1, rlSrc2, rlDest, 2);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002395 break;
2396 case OP_AGET_BOOLEAN:
Bill Buzbee1465db52009-09-23 17:17:35 -07002397 genArrayGet(cUnit, mir, kUnsignedByte, rlSrc1, rlSrc2, rlDest, 0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002398 break;
2399 case OP_AGET_BYTE:
Bill Buzbee1465db52009-09-23 17:17:35 -07002400 genArrayGet(cUnit, mir, kSignedByte, rlSrc1, rlSrc2, rlDest, 0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002401 break;
2402 case OP_AGET_CHAR:
Bill Buzbee1465db52009-09-23 17:17:35 -07002403 genArrayGet(cUnit, mir, kUnsignedHalf, rlSrc1, rlSrc2, rlDest, 1);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002404 break;
2405 case OP_AGET_SHORT:
Bill Buzbee1465db52009-09-23 17:17:35 -07002406 genArrayGet(cUnit, mir, kSignedHalf, rlSrc1, rlSrc2, rlDest, 1);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002407 break;
2408 case OP_APUT_WIDE:
Bill Buzbee1465db52009-09-23 17:17:35 -07002409 genArrayPut(cUnit, mir, kLong, rlSrc1, rlSrc2, rlDest, 3);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002410 break;
2411 case OP_APUT:
Bill Buzbee1465db52009-09-23 17:17:35 -07002412 genArrayPut(cUnit, mir, kWord, rlSrc1, rlSrc2, rlDest, 2);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002413 break;
Bill Buzbeebe6534f2010-03-12 16:01:35 -08002414 case OP_APUT_OBJECT:
2415 genArrayObjectPut(cUnit, mir, rlSrc1, rlSrc2, rlDest, 2);
2416 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002417 case OP_APUT_SHORT:
2418 case OP_APUT_CHAR:
Bill Buzbee1465db52009-09-23 17:17:35 -07002419 genArrayPut(cUnit, mir, kUnsignedHalf, rlSrc1, rlSrc2, rlDest, 1);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002420 break;
2421 case OP_APUT_BYTE:
2422 case OP_APUT_BOOLEAN:
Bill Buzbee1465db52009-09-23 17:17:35 -07002423 genArrayPut(cUnit, mir, kUnsignedByte, rlSrc1, rlSrc2, rlDest, 0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002424 break;
2425 default:
2426 return true;
2427 }
2428 return false;
2429}
2430
Ben Cheng6c10a972009-10-29 14:39:18 -07002431/*
2432 * Find the matching case.
2433 *
2434 * return values:
2435 * r0 (low 32-bit): pc of the chaining cell corresponding to the resolved case,
2436 * including default which is placed at MIN(size, MAX_CHAINED_SWITCH_CASES).
2437 * r1 (high 32-bit): the branch offset of the matching case (only for indexes
2438 * above MAX_CHAINED_SWITCH_CASES).
2439 *
2440 * Instructions around the call are:
2441 *
2442 * mov r2, pc
2443 * blx &findPackedSwitchIndex
2444 * mov pc, r0
2445 * .align4
Bill Buzbeebd047242010-05-13 13:02:53 -07002446 * chaining cell for case 0 [12 bytes]
2447 * chaining cell for case 1 [12 bytes]
Ben Cheng6c10a972009-10-29 14:39:18 -07002448 * :
Bill Buzbeebd047242010-05-13 13:02:53 -07002449 * chaining cell for case MIN(size, MAX_CHAINED_SWITCH_CASES)-1 [12 bytes]
Ben Cheng6c10a972009-10-29 14:39:18 -07002450 * chaining cell for case default [8 bytes]
2451 * noChain exit
2452 */
Ben Chengbd1326d2010-04-02 15:04:53 -07002453static s8 findPackedSwitchIndex(const u2* switchData, int testVal, int pc)
Ben Cheng6c10a972009-10-29 14:39:18 -07002454{
2455 int size;
2456 int firstKey;
2457 const int *entries;
2458 int index;
2459 int jumpIndex;
2460 int caseDPCOffset = 0;
2461 /* In Thumb mode pc is 4 ahead of the "mov r2, pc" instruction */
2462 int chainingPC = (pc + 4) & ~3;
2463
2464 /*
2465 * Packed switch data format:
2466 * ushort ident = 0x0100 magic value
2467 * ushort size number of entries in the table
2468 * int first_key first (and lowest) switch case value
2469 * int targets[size] branch targets, relative to switch opcode
2470 *
2471 * Total size is (4+size*2) 16-bit code units.
2472 */
2473 size = switchData[1];
2474 assert(size > 0);
2475
2476 firstKey = switchData[2];
2477 firstKey |= switchData[3] << 16;
2478
2479
2480 /* The entries are guaranteed to be aligned on a 32-bit boundary;
2481 * we can treat them as a native int array.
2482 */
2483 entries = (const int*) &switchData[4];
2484 assert(((u4)entries & 0x3) == 0);
2485
2486 index = testVal - firstKey;
2487
2488 /* Jump to the default cell */
2489 if (index < 0 || index >= size) {
2490 jumpIndex = MIN(size, MAX_CHAINED_SWITCH_CASES);
2491 /* Jump to the non-chaining exit point */
2492 } else if (index >= MAX_CHAINED_SWITCH_CASES) {
2493 jumpIndex = MAX_CHAINED_SWITCH_CASES + 1;
2494 caseDPCOffset = entries[index];
2495 /* Jump to the inline chaining cell */
2496 } else {
2497 jumpIndex = index;
2498 }
2499
Bill Buzbeebd047242010-05-13 13:02:53 -07002500 chainingPC += jumpIndex * CHAIN_CELL_NORMAL_SIZE;
Ben Cheng6c10a972009-10-29 14:39:18 -07002501 return (((s8) caseDPCOffset) << 32) | (u8) chainingPC;
2502}
2503
2504/* See comments for findPackedSwitchIndex */
Ben Chengbd1326d2010-04-02 15:04:53 -07002505static s8 findSparseSwitchIndex(const u2* switchData, int testVal, int pc)
Ben Cheng6c10a972009-10-29 14:39:18 -07002506{
2507 int size;
2508 const int *keys;
2509 const int *entries;
2510 int chainingPC = (pc + 4) & ~3;
2511 int i;
2512
2513 /*
2514 * Sparse switch data format:
2515 * ushort ident = 0x0200 magic value
2516 * ushort size number of entries in the table; > 0
2517 * int keys[size] keys, sorted low-to-high; 32-bit aligned
2518 * int targets[size] branch targets, relative to switch opcode
2519 *
2520 * Total size is (2+size*4) 16-bit code units.
2521 */
2522
2523 size = switchData[1];
2524 assert(size > 0);
2525
2526 /* The keys are guaranteed to be aligned on a 32-bit boundary;
2527 * we can treat them as a native int array.
2528 */
2529 keys = (const int*) &switchData[2];
2530 assert(((u4)keys & 0x3) == 0);
2531
2532 /* The entries are guaranteed to be aligned on a 32-bit boundary;
2533 * we can treat them as a native int array.
2534 */
2535 entries = keys + size;
2536 assert(((u4)entries & 0x3) == 0);
2537
2538 /*
2539 * Run through the list of keys, which are guaranteed to
2540 * be sorted low-to-high.
2541 *
2542 * Most tables have 3-4 entries. Few have more than 10. A binary
2543 * search here is probably not useful.
2544 */
2545 for (i = 0; i < size; i++) {
2546 int k = keys[i];
2547 if (k == testVal) {
2548 /* MAX_CHAINED_SWITCH_CASES + 1 is the start of the overflow case */
2549 int jumpIndex = (i < MAX_CHAINED_SWITCH_CASES) ?
2550 i : MAX_CHAINED_SWITCH_CASES + 1;
Bill Buzbeebd047242010-05-13 13:02:53 -07002551 chainingPC += jumpIndex * CHAIN_CELL_NORMAL_SIZE;
Ben Cheng6c10a972009-10-29 14:39:18 -07002552 return (((s8) entries[i]) << 32) | (u8) chainingPC;
2553 } else if (k > testVal) {
2554 break;
2555 }
2556 }
Bill Buzbeebd047242010-05-13 13:02:53 -07002557 return chainingPC + MIN(size, MAX_CHAINED_SWITCH_CASES) *
2558 CHAIN_CELL_NORMAL_SIZE;
Ben Cheng6c10a972009-10-29 14:39:18 -07002559}
2560
Ben Chengba4fc8b2009-06-01 13:00:29 -07002561static bool handleFmt31t(CompilationUnit *cUnit, MIR *mir)
2562{
2563 OpCode dalvikOpCode = mir->dalvikInsn.opCode;
2564 switch (dalvikOpCode) {
2565 case OP_FILL_ARRAY_DATA: {
Bill Buzbeec6f10662010-02-09 11:16:15 -08002566 RegLocation rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07002567 // Making a call - use explicit registers
Bill Buzbeec6f10662010-02-09 11:16:15 -08002568 dvmCompilerFlushAllRegs(cUnit); /* Everything to home location */
Bill Buzbee1465db52009-09-23 17:17:35 -07002569 genExportPC(cUnit, mir);
2570 loadValueDirectFixed(cUnit, rlSrc, r0);
Ben Chengbd1326d2010-04-02 15:04:53 -07002571 LOAD_FUNC_ADDR(cUnit, r2, (int)dvmInterpHandleFillArrayData);
Ben Cheng6c10a972009-10-29 14:39:18 -07002572 loadConstant(cUnit, r1,
2573 (int) (cUnit->method->insns + mir->offset + mir->dalvikInsn.vB));
Bill Buzbee1465db52009-09-23 17:17:35 -07002574 opReg(cUnit, kOpBlx, r2);
Elliott Hughes6a555132010-02-25 15:41:42 -08002575 dvmCompilerClobberCallRegs(cUnit);
Bill Buzbeefd023aa2009-11-02 09:23:49 -08002576 /* generate a branch over if successful */
2577 opRegImm(cUnit, kOpCmp, r0, 0); /* NULL? */
2578 ArmLIR *branchOver = opCondBranch(cUnit, kArmCondNe);
2579 loadConstant(cUnit, r0,
2580 (int) (cUnit->method->insns + mir->offset));
2581 genDispatchToHandler(cUnit, TEMPLATE_THROW_EXCEPTION_COMMON);
2582 ArmLIR *target = newLIR0(cUnit, kArmPseudoTargetLabel);
2583 target->defMask = ENCODE_ALL;
2584 branchOver->generic.target = (LIR *) target;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002585 break;
2586 }
2587 /*
Ben Cheng6c10a972009-10-29 14:39:18 -07002588 * Compute the goto target of up to
2589 * MIN(switchSize, MAX_CHAINED_SWITCH_CASES) + 1 chaining cells.
2590 * See the comment before findPackedSwitchIndex for the code layout.
Ben Chengba4fc8b2009-06-01 13:00:29 -07002591 */
2592 case OP_PACKED_SWITCH:
2593 case OP_SPARSE_SWITCH: {
Bill Buzbeec6f10662010-02-09 11:16:15 -08002594 RegLocation rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
2595 dvmCompilerFlushAllRegs(cUnit); /* Everything to home location */
Bill Buzbee1465db52009-09-23 17:17:35 -07002596 loadValueDirectFixed(cUnit, rlSrc, r1);
Bill Buzbeec6f10662010-02-09 11:16:15 -08002597 dvmCompilerLockAllTemps(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002598 if (dalvikOpCode == OP_PACKED_SWITCH) {
Ben Chengbd1326d2010-04-02 15:04:53 -07002599 LOAD_FUNC_ADDR(cUnit, r4PC, (int)findPackedSwitchIndex);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002600 } else {
Ben Chengbd1326d2010-04-02 15:04:53 -07002601 LOAD_FUNC_ADDR(cUnit, r4PC, (int)findSparseSwitchIndex);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002602 }
Ben Cheng6c10a972009-10-29 14:39:18 -07002603 /* r0 <- Addr of the switch data */
2604 loadConstant(cUnit, r0,
2605 (int) (cUnit->method->insns + mir->offset + mir->dalvikInsn.vB));
2606 /* r2 <- pc of the instruction following the blx */
2607 opRegReg(cUnit, kOpMov, r2, rpc);
Bill Buzbee1465db52009-09-23 17:17:35 -07002608 opReg(cUnit, kOpBlx, r4PC);
Elliott Hughes6a555132010-02-25 15:41:42 -08002609 dvmCompilerClobberCallRegs(cUnit);
Ben Cheng6c10a972009-10-29 14:39:18 -07002610 /* pc <- computed goto target */
2611 opRegReg(cUnit, kOpMov, rpc, r0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002612 break;
2613 }
2614 default:
2615 return true;
2616 }
2617 return false;
2618}
2619
2620static bool handleFmt35c_3rc(CompilationUnit *cUnit, MIR *mir, BasicBlock *bb,
Bill Buzbee89efc3d2009-07-28 11:22:22 -07002621 ArmLIR *labelList)
Ben Chengba4fc8b2009-06-01 13:00:29 -07002622{
Bill Buzbee9bc3df32009-07-30 10:52:29 -07002623 ArmLIR *retChainingCell = NULL;
Bill Buzbee89efc3d2009-07-28 11:22:22 -07002624 ArmLIR *pcrLabel = NULL;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002625
Bill Buzbeef4ce16f2009-07-28 13:28:25 -07002626 if (bb->fallThrough != NULL)
2627 retChainingCell = &labelList[bb->fallThrough->id];
2628
Ben Chengba4fc8b2009-06-01 13:00:29 -07002629 DecodedInstruction *dInsn = &mir->dalvikInsn;
2630 switch (mir->dalvikInsn.opCode) {
2631 /*
2632 * calleeMethod = this->clazz->vtable[
2633 * method->clazz->pDvmDex->pResMethods[BBBB]->methodIndex
2634 * ]
2635 */
2636 case OP_INVOKE_VIRTUAL:
2637 case OP_INVOKE_VIRTUAL_RANGE: {
Bill Buzbee89efc3d2009-07-28 11:22:22 -07002638 ArmLIR *predChainingCell = &labelList[bb->taken->id];
Ben Chengba4fc8b2009-06-01 13:00:29 -07002639 int methodIndex =
2640 cUnit->method->clazz->pDvmDex->pResMethods[dInsn->vB]->
2641 methodIndex;
2642
2643 if (mir->dalvikInsn.opCode == OP_INVOKE_VIRTUAL)
2644 genProcessArgsNoRange(cUnit, mir, dInsn, &pcrLabel);
2645 else
2646 genProcessArgsRange(cUnit, mir, dInsn, &pcrLabel);
2647
Ben Cheng38329f52009-07-07 14:19:20 -07002648 genInvokeVirtualCommon(cUnit, mir, methodIndex,
2649 retChainingCell,
2650 predChainingCell,
2651 pcrLabel);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002652 break;
2653 }
2654 /*
2655 * calleeMethod = method->clazz->super->vtable[method->clazz->pDvmDex
2656 * ->pResMethods[BBBB]->methodIndex]
2657 */
Ben Chengba4fc8b2009-06-01 13:00:29 -07002658 case OP_INVOKE_SUPER:
2659 case OP_INVOKE_SUPER_RANGE: {
2660 int mIndex = cUnit->method->clazz->pDvmDex->
2661 pResMethods[dInsn->vB]->methodIndex;
2662 const Method *calleeMethod =
2663 cUnit->method->clazz->super->vtable[mIndex];
2664
2665 if (mir->dalvikInsn.opCode == OP_INVOKE_SUPER)
2666 genProcessArgsNoRange(cUnit, mir, dInsn, &pcrLabel);
2667 else
2668 genProcessArgsRange(cUnit, mir, dInsn, &pcrLabel);
2669
2670 /* r0 = calleeMethod */
2671 loadConstant(cUnit, r0, (int) calleeMethod);
2672
Ben Cheng38329f52009-07-07 14:19:20 -07002673 genInvokeSingletonCommon(cUnit, mir, bb, labelList, pcrLabel,
2674 calleeMethod);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002675 break;
2676 }
2677 /* calleeMethod = method->clazz->pDvmDex->pResMethods[BBBB] */
2678 case OP_INVOKE_DIRECT:
2679 case OP_INVOKE_DIRECT_RANGE: {
2680 const Method *calleeMethod =
2681 cUnit->method->clazz->pDvmDex->pResMethods[dInsn->vB];
2682
2683 if (mir->dalvikInsn.opCode == OP_INVOKE_DIRECT)
2684 genProcessArgsNoRange(cUnit, mir, dInsn, &pcrLabel);
2685 else
2686 genProcessArgsRange(cUnit, mir, dInsn, &pcrLabel);
2687
2688 /* r0 = calleeMethod */
2689 loadConstant(cUnit, r0, (int) calleeMethod);
2690
Ben Cheng38329f52009-07-07 14:19:20 -07002691 genInvokeSingletonCommon(cUnit, mir, bb, labelList, pcrLabel,
2692 calleeMethod);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002693 break;
2694 }
2695 /* calleeMethod = method->clazz->pDvmDex->pResMethods[BBBB] */
2696 case OP_INVOKE_STATIC:
2697 case OP_INVOKE_STATIC_RANGE: {
2698 const Method *calleeMethod =
2699 cUnit->method->clazz->pDvmDex->pResMethods[dInsn->vB];
2700
2701 if (mir->dalvikInsn.opCode == OP_INVOKE_STATIC)
2702 genProcessArgsNoRange(cUnit, mir, dInsn,
2703 NULL /* no null check */);
2704 else
2705 genProcessArgsRange(cUnit, mir, dInsn,
2706 NULL /* no null check */);
2707
2708 /* r0 = calleeMethod */
2709 loadConstant(cUnit, r0, (int) calleeMethod);
2710
Ben Cheng38329f52009-07-07 14:19:20 -07002711 genInvokeSingletonCommon(cUnit, mir, bb, labelList, pcrLabel,
2712 calleeMethod);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002713 break;
2714 }
Ben Cheng09e50c92010-05-02 10:45:32 -07002715 /*
Ben Chengba4fc8b2009-06-01 13:00:29 -07002716 * calleeMethod = dvmFindInterfaceMethodInCache(this->clazz,
2717 * BBBB, method, method->clazz->pDvmDex)
Ben Cheng38329f52009-07-07 14:19:20 -07002718 *
Ben Cheng09e50c92010-05-02 10:45:32 -07002719 * The following is an example of generated code for
2720 * "invoke-interface v0"
Ben Cheng38329f52009-07-07 14:19:20 -07002721 *
Ben Cheng09e50c92010-05-02 10:45:32 -07002722 * -------- dalvik offset: 0x0008 @ invoke-interface v0
2723 * 0x47357e36 : ldr r0, [r5, #0] --+
2724 * 0x47357e38 : sub r7,r5,#24 |
2725 * 0x47357e3c : cmp r0, #0 | genProcessArgsNoRange
2726 * 0x47357e3e : beq 0x47357e82 |
2727 * 0x47357e40 : stmia r7, <r0> --+
2728 * 0x47357e42 : ldr r4, [pc, #120] --> r4 <- dalvikPC of this invoke
2729 * 0x47357e44 : add r1, pc, #64 --> r1 <- &retChainingCell
2730 * 0x47357e46 : add r2, pc, #72 --> r2 <- &predictedChainingCell
2731 * 0x47357e48 : blx_1 0x47348190 --+ TEMPLATE_INVOKE_METHOD_
2732 * 0x47357e4a : blx_2 see above --+ PREDICTED_CHAIN
2733 * 0x47357e4c : b 0x47357e90 --> off to the predicted chain
2734 * 0x47357e4e : b 0x47357e82 --> punt to the interpreter
2735 * 0x47357e50 : mov r8, r1 --+
2736 * 0x47357e52 : mov r9, r2 |
2737 * 0x47357e54 : ldr r2, [pc, #96] |
2738 * 0x47357e56 : mov r10, r3 |
2739 * 0x47357e58 : movs r0, r3 | dvmFindInterfaceMethodInCache
2740 * 0x47357e5a : ldr r3, [pc, #88] |
2741 * 0x47357e5c : ldr r7, [pc, #80] |
2742 * 0x47357e5e : mov r1, #1452 |
2743 * 0x47357e62 : blx r7 --+
2744 * 0x47357e64 : cmp r0, #0 --> calleeMethod == NULL?
2745 * 0x47357e66 : bne 0x47357e6e --> branch over the throw if !r0
2746 * 0x47357e68 : ldr r0, [pc, #80] --> load Dalvik PC of the invoke
2747 * 0x47357e6a : blx_1 0x47348494 --+ TEMPLATE_THROW_EXCEPTION_
2748 * 0x47357e6c : blx_2 see above --+ COMMON
2749 * 0x47357e6e : mov r1, r8 --> r1 <- &retChainingCell
2750 * 0x47357e70 : cmp r1, #0 --> compare against 0
2751 * 0x47357e72 : bgt 0x47357e7c --> >=0? don't rechain
2752 * 0x47357e74 : ldr r7, [r6, #108] --+
2753 * 0x47357e76 : mov r2, r9 | dvmJitToPatchPredictedChain
2754 * 0x47357e78 : mov r3, r10 |
2755 * 0x47357e7a : blx r7 --+
2756 * 0x47357e7c : add r1, pc, #8 --> r1 <- &retChainingCell
2757 * 0x47357e7e : blx_1 0x4734809c --+ TEMPLATE_INVOKE_METHOD_NO_OPT
2758 * 0x47357e80 : blx_2 see above --+
2759 * -------- reconstruct dalvik PC : 0x425719dc @ +0x0008
2760 * 0x47357e82 : ldr r0, [pc, #56]
Ben Cheng38329f52009-07-07 14:19:20 -07002761 * Exception_Handling:
Ben Cheng09e50c92010-05-02 10:45:32 -07002762 * 0x47357e84 : ldr r1, [r6, #92]
2763 * 0x47357e86 : blx r1
2764 * 0x47357e88 : .align4
2765 * -------- chaining cell (hot): 0x000b
2766 * 0x47357e88 : ldr r0, [r6, #104]
2767 * 0x47357e8a : blx r0
2768 * 0x47357e8c : data 0x19e2(6626)
2769 * 0x47357e8e : data 0x4257(16983)
2770 * 0x47357e90 : .align4
Ben Cheng38329f52009-07-07 14:19:20 -07002771 * -------- chaining cell (predicted)
Ben Cheng09e50c92010-05-02 10:45:32 -07002772 * 0x47357e90 : data 0xe7fe(59390) --> will be patched into bx
2773 * 0x47357e92 : data 0x0000(0)
2774 * 0x47357e94 : data 0x0000(0) --> class
2775 * 0x47357e96 : data 0x0000(0)
2776 * 0x47357e98 : data 0x0000(0) --> method
2777 * 0x47357e9a : data 0x0000(0)
2778 * 0x47357e9c : data 0x0000(0) --> rechain count
2779 * 0x47357e9e : data 0x0000(0)
2780 * -------- end of chaining cells (0x006c)
2781 * 0x47357eb0 : .word (0xad03e369)
2782 * 0x47357eb4 : .word (0x28a90)
2783 * 0x47357eb8 : .word (0x41a63394)
2784 * 0x47357ebc : .word (0x425719dc)
Ben Chengba4fc8b2009-06-01 13:00:29 -07002785 */
2786 case OP_INVOKE_INTERFACE:
2787 case OP_INVOKE_INTERFACE_RANGE: {
Bill Buzbee89efc3d2009-07-28 11:22:22 -07002788 ArmLIR *predChainingCell = &labelList[bb->taken->id];
Ben Chengba4fc8b2009-06-01 13:00:29 -07002789
Bill Buzbee1465db52009-09-23 17:17:35 -07002790 /* Ensure that nothing is both live and dirty */
Bill Buzbeec6f10662010-02-09 11:16:15 -08002791 dvmCompilerFlushAllRegs(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07002792
Ben Chengba4fc8b2009-06-01 13:00:29 -07002793 if (mir->dalvikInsn.opCode == OP_INVOKE_INTERFACE)
2794 genProcessArgsNoRange(cUnit, mir, dInsn, &pcrLabel);
2795 else
2796 genProcessArgsRange(cUnit, mir, dInsn, &pcrLabel);
2797
Ben Cheng38329f52009-07-07 14:19:20 -07002798 /* "this" is already left in r0 by genProcessArgs* */
2799
2800 /* r4PC = dalvikCallsite */
2801 loadConstant(cUnit, r4PC,
2802 (int) (cUnit->method->insns + mir->offset));
2803
2804 /* r1 = &retChainingCell */
Bill Buzbee270c1d62009-08-13 16:58:07 -07002805 ArmLIR *addrRetChain =
Bill Buzbee1465db52009-09-23 17:17:35 -07002806 opRegRegImm(cUnit, kOpAdd, r1, rpc, 0);
Ben Cheng38329f52009-07-07 14:19:20 -07002807 addrRetChain->generic.target = (LIR *) retChainingCell;
2808
2809 /* r2 = &predictedChainingCell */
Bill Buzbee89efc3d2009-07-28 11:22:22 -07002810 ArmLIR *predictedChainingCell =
Bill Buzbee1465db52009-09-23 17:17:35 -07002811 opRegRegImm(cUnit, kOpAdd, r2, rpc, 0);
Ben Cheng38329f52009-07-07 14:19:20 -07002812 predictedChainingCell->generic.target = (LIR *) predChainingCell;
2813
2814 genDispatchToHandler(cUnit, TEMPLATE_INVOKE_METHOD_PREDICTED_CHAIN);
2815
2816 /* return through lr - jump to the chaining cell */
2817 genUnconditionalBranch(cUnit, predChainingCell);
2818
2819 /*
2820 * null-check on "this" may have been eliminated, but we still need
2821 * a PC-reconstruction label for stack overflow bailout.
2822 */
2823 if (pcrLabel == NULL) {
2824 int dPC = (int) (cUnit->method->insns + mir->offset);
Bill Buzbee89efc3d2009-07-28 11:22:22 -07002825 pcrLabel = dvmCompilerNew(sizeof(ArmLIR), true);
Ben Chenga4973592010-03-31 11:59:18 -07002826 pcrLabel->opCode = kArmPseudoPCReconstructionCell;
Ben Cheng38329f52009-07-07 14:19:20 -07002827 pcrLabel->operands[0] = dPC;
2828 pcrLabel->operands[1] = mir->offset;
2829 /* Insert the place holder to the growable list */
2830 dvmInsertGrowableList(&cUnit->pcReconstructionList, pcrLabel);
2831 }
2832
2833 /* return through lr+2 - punt to the interpreter */
2834 genUnconditionalBranch(cUnit, pcrLabel);
2835
2836 /*
2837 * return through lr+4 - fully resolve the callee method.
2838 * r1 <- count
2839 * r2 <- &predictedChainCell
2840 * r3 <- this->class
2841 * r4 <- dPC
2842 * r7 <- this->class->vtable
2843 */
2844
2845 /* Save count, &predictedChainCell, and class to high regs first */
Bill Buzbee1465db52009-09-23 17:17:35 -07002846 genRegCopy(cUnit, r8, r1);
2847 genRegCopy(cUnit, r9, r2);
2848 genRegCopy(cUnit, r10, r3);
Ben Cheng38329f52009-07-07 14:19:20 -07002849
Ben Chengba4fc8b2009-06-01 13:00:29 -07002850 /* r0 now contains this->clazz */
Bill Buzbee1465db52009-09-23 17:17:35 -07002851 genRegCopy(cUnit, r0, r3);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002852
2853 /* r1 = BBBB */
2854 loadConstant(cUnit, r1, dInsn->vB);
2855
2856 /* r2 = method (caller) */
2857 loadConstant(cUnit, r2, (int) cUnit->method);
2858
2859 /* r3 = pDvmDex */
2860 loadConstant(cUnit, r3, (int) cUnit->method->clazz->pDvmDex);
2861
Ben Chengbd1326d2010-04-02 15:04:53 -07002862 LOAD_FUNC_ADDR(cUnit, r7,
2863 (intptr_t) dvmFindInterfaceMethodInCache);
Bill Buzbee1465db52009-09-23 17:17:35 -07002864 opReg(cUnit, kOpBlx, r7);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002865 /* r0 = calleeMethod (returned from dvmFindInterfaceMethodInCache */
2866
Ben Cheng09e50c92010-05-02 10:45:32 -07002867 dvmCompilerClobberCallRegs(cUnit);
2868 /* generate a branch over if the interface method is resolved */
2869 opRegImm(cUnit, kOpCmp, r0, 0); /* NULL? */
2870 ArmLIR *branchOver = opCondBranch(cUnit, kArmCondNe);
2871 /*
2872 * calleeMethod == NULL -> throw
2873 */
2874 loadConstant(cUnit, r0,
2875 (int) (cUnit->method->insns + mir->offset));
2876 genDispatchToHandler(cUnit, TEMPLATE_THROW_EXCEPTION_COMMON);
2877 /* noreturn */
2878
2879 ArmLIR *target = newLIR0(cUnit, kArmPseudoTargetLabel);
2880 target->defMask = ENCODE_ALL;
2881 branchOver->generic.target = (LIR *) target;
2882
Bill Buzbee1465db52009-09-23 17:17:35 -07002883 genRegCopy(cUnit, r1, r8);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002884
Ben Cheng38329f52009-07-07 14:19:20 -07002885 /* Check if rechain limit is reached */
Bill Buzbee1465db52009-09-23 17:17:35 -07002886 opRegImm(cUnit, kOpCmp, r1, 0);
Ben Cheng38329f52009-07-07 14:19:20 -07002887
Bill Buzbee1465db52009-09-23 17:17:35 -07002888 ArmLIR *bypassRechaining = opCondBranch(cUnit, kArmCondGt);
Ben Cheng38329f52009-07-07 14:19:20 -07002889
Bill Buzbee270c1d62009-08-13 16:58:07 -07002890 loadWordDisp(cUnit, rGLUE, offsetof(InterpState,
2891 jitToInterpEntries.dvmJitToPatchPredictedChain), r7);
Ben Cheng38329f52009-07-07 14:19:20 -07002892
Ben Chengb88ec3c2010-05-17 12:50:33 -07002893 genRegCopy(cUnit, r1, rGLUE);
Bill Buzbee1465db52009-09-23 17:17:35 -07002894 genRegCopy(cUnit, r2, r9);
2895 genRegCopy(cUnit, r3, r10);
Ben Cheng38329f52009-07-07 14:19:20 -07002896
2897 /*
2898 * r0 = calleeMethod
2899 * r2 = &predictedChainingCell
2900 * r3 = class
2901 *
2902 * &returnChainingCell has been loaded into r1 but is not needed
2903 * when patching the chaining cell and will be clobbered upon
2904 * returning so it will be reconstructed again.
2905 */
Bill Buzbee1465db52009-09-23 17:17:35 -07002906 opReg(cUnit, kOpBlx, r7);
Ben Cheng38329f52009-07-07 14:19:20 -07002907
2908 /* r1 = &retChainingCell */
Bill Buzbee1465db52009-09-23 17:17:35 -07002909 addrRetChain = opRegRegImm(cUnit, kOpAdd, r1, rpc, 0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002910 addrRetChain->generic.target = (LIR *) retChainingCell;
Ben Cheng38329f52009-07-07 14:19:20 -07002911
2912 bypassRechaining->generic.target = (LIR *) addrRetChain;
2913
Ben Chengba4fc8b2009-06-01 13:00:29 -07002914 /*
2915 * r0 = this, r1 = calleeMethod,
2916 * r1 = &ChainingCell,
2917 * r4PC = callsiteDPC,
2918 */
2919 genDispatchToHandler(cUnit, TEMPLATE_INVOKE_METHOD_NO_OPT);
Ben Cheng978738d2010-05-13 13:45:57 -07002920#if defined(WITH_JIT_TUNING)
Ben Cheng86717f72010-03-05 15:27:21 -08002921 gDvmJit.invokePolymorphic++;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002922#endif
2923 /* Handle exceptions using the interpreter */
2924 genTrap(cUnit, mir->offset, pcrLabel);
2925 break;
2926 }
2927 /* NOP */
2928 case OP_INVOKE_DIRECT_EMPTY: {
2929 return false;
2930 }
2931 case OP_FILLED_NEW_ARRAY:
2932 case OP_FILLED_NEW_ARRAY_RANGE: {
2933 /* Just let the interpreter deal with these */
2934 genInterpSingleStep(cUnit, mir);
2935 break;
2936 }
2937 default:
2938 return true;
2939 }
2940 return false;
2941}
2942
2943static bool handleFmt35ms_3rms(CompilationUnit *cUnit, MIR *mir,
Bill Buzbee89efc3d2009-07-28 11:22:22 -07002944 BasicBlock *bb, ArmLIR *labelList)
Ben Chengba4fc8b2009-06-01 13:00:29 -07002945{
Bill Buzbee89efc3d2009-07-28 11:22:22 -07002946 ArmLIR *retChainingCell = &labelList[bb->fallThrough->id];
2947 ArmLIR *predChainingCell = &labelList[bb->taken->id];
2948 ArmLIR *pcrLabel = NULL;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002949
2950 DecodedInstruction *dInsn = &mir->dalvikInsn;
2951 switch (mir->dalvikInsn.opCode) {
2952 /* calleeMethod = this->clazz->vtable[BBBB] */
2953 case OP_INVOKE_VIRTUAL_QUICK_RANGE:
2954 case OP_INVOKE_VIRTUAL_QUICK: {
2955 int methodIndex = dInsn->vB;
2956 if (mir->dalvikInsn.opCode == OP_INVOKE_VIRTUAL_QUICK)
2957 genProcessArgsNoRange(cUnit, mir, dInsn, &pcrLabel);
2958 else
2959 genProcessArgsRange(cUnit, mir, dInsn, &pcrLabel);
2960
Ben Cheng38329f52009-07-07 14:19:20 -07002961 genInvokeVirtualCommon(cUnit, mir, methodIndex,
2962 retChainingCell,
2963 predChainingCell,
2964 pcrLabel);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002965 break;
2966 }
2967 /* calleeMethod = method->clazz->super->vtable[BBBB] */
2968 case OP_INVOKE_SUPER_QUICK:
2969 case OP_INVOKE_SUPER_QUICK_RANGE: {
2970 const Method *calleeMethod =
2971 cUnit->method->clazz->super->vtable[dInsn->vB];
2972
2973 if (mir->dalvikInsn.opCode == OP_INVOKE_SUPER_QUICK)
2974 genProcessArgsNoRange(cUnit, mir, dInsn, &pcrLabel);
2975 else
2976 genProcessArgsRange(cUnit, mir, dInsn, &pcrLabel);
2977
2978 /* r0 = calleeMethod */
2979 loadConstant(cUnit, r0, (int) calleeMethod);
2980
Ben Cheng38329f52009-07-07 14:19:20 -07002981 genInvokeSingletonCommon(cUnit, mir, bb, labelList, pcrLabel,
2982 calleeMethod);
2983 /* Handle exceptions using the interpreter */
2984 genTrap(cUnit, mir->offset, pcrLabel);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002985 break;
2986 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07002987 default:
2988 return true;
2989 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07002990 return false;
2991}
2992
2993/*
Bill Buzbeefd023aa2009-11-02 09:23:49 -08002994 * This operation is complex enough that we'll do it partly inline
2995 * and partly with a handler. NOTE: the handler uses hardcoded
2996 * values for string object offsets and must be revisitied if the
2997 * layout changes.
2998 */
2999static bool genInlinedCompareTo(CompilationUnit *cUnit, MIR *mir)
3000{
3001#if defined(USE_GLOBAL_STRING_DEFS)
3002 return false;
3003#else
3004 ArmLIR *rollback;
Bill Buzbeec6f10662010-02-09 11:16:15 -08003005 RegLocation rlThis = dvmCompilerGetSrc(cUnit, mir, 0);
3006 RegLocation rlComp = dvmCompilerGetSrc(cUnit, mir, 1);
Bill Buzbeefd023aa2009-11-02 09:23:49 -08003007
3008 loadValueDirectFixed(cUnit, rlThis, r0);
3009 loadValueDirectFixed(cUnit, rlComp, r1);
3010 /* Test objects for NULL */
3011 rollback = genNullCheck(cUnit, rlThis.sRegLow, r0, mir->offset, NULL);
3012 genNullCheck(cUnit, rlComp.sRegLow, r1, mir->offset, rollback);
3013 /*
3014 * TUNING: we could check for object pointer equality before invoking
3015 * handler. Unclear whether the gain would be worth the added code size
3016 * expansion.
3017 */
3018 genDispatchToHandler(cUnit, TEMPLATE_STRING_COMPARETO);
Bill Buzbeec6f10662010-02-09 11:16:15 -08003019 storeValue(cUnit, inlinedTarget(cUnit, mir, false),
3020 dvmCompilerGetReturn(cUnit));
Bill Buzbeefd023aa2009-11-02 09:23:49 -08003021 return true;
3022#endif
3023}
3024
Elliott Hughes2bdbcb62010-04-12 14:29:37 -07003025static bool genInlinedFastIndexOf(CompilationUnit *cUnit, MIR *mir)
Bill Buzbeefd023aa2009-11-02 09:23:49 -08003026{
3027#if defined(USE_GLOBAL_STRING_DEFS)
3028 return false;
3029#else
Bill Buzbeec6f10662010-02-09 11:16:15 -08003030 RegLocation rlThis = dvmCompilerGetSrc(cUnit, mir, 0);
3031 RegLocation rlChar = dvmCompilerGetSrc(cUnit, mir, 1);
Bill Buzbeefd023aa2009-11-02 09:23:49 -08003032
3033 loadValueDirectFixed(cUnit, rlThis, r0);
3034 loadValueDirectFixed(cUnit, rlChar, r1);
Elliott Hughes2bdbcb62010-04-12 14:29:37 -07003035 RegLocation rlStart = dvmCompilerGetSrc(cUnit, mir, 2);
3036 loadValueDirectFixed(cUnit, rlStart, r2);
Bill Buzbeefd023aa2009-11-02 09:23:49 -08003037 /* Test objects for NULL */
3038 genNullCheck(cUnit, rlThis.sRegLow, r0, mir->offset, NULL);
3039 genDispatchToHandler(cUnit, TEMPLATE_STRING_INDEXOF);
Bill Buzbeec6f10662010-02-09 11:16:15 -08003040 storeValue(cUnit, inlinedTarget(cUnit, mir, false),
3041 dvmCompilerGetReturn(cUnit));
Bill Buzbeefd023aa2009-11-02 09:23:49 -08003042 return true;
3043#endif
3044}
3045
Elliott Hughesee34f592010-04-05 18:13:52 -07003046// Generates an inlined String.isEmpty or String.length.
3047static bool genInlinedStringIsEmptyOrLength(CompilationUnit *cUnit, MIR *mir,
3048 bool isEmpty)
Bill Buzbee1f748632010-03-02 16:14:41 -08003049{
Elliott Hughesee34f592010-04-05 18:13:52 -07003050 // dst = src.length();
Bill Buzbee1f748632010-03-02 16:14:41 -08003051 RegLocation rlObj = dvmCompilerGetSrc(cUnit, mir, 0);
3052 RegLocation rlDest = inlinedTarget(cUnit, mir, false);
3053 rlObj = loadValue(cUnit, rlObj, kCoreReg);
3054 RegLocation rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
3055 genNullCheck(cUnit, rlObj.sRegLow, rlObj.lowReg, mir->offset, NULL);
3056 loadWordDisp(cUnit, rlObj.lowReg, gDvm.offJavaLangString_count,
3057 rlResult.lowReg);
Elliott Hughesee34f592010-04-05 18:13:52 -07003058 if (isEmpty) {
3059 // dst = (dst == 0);
3060 int tReg = dvmCompilerAllocTemp(cUnit);
3061 opRegReg(cUnit, kOpNeg, tReg, rlResult.lowReg);
3062 opRegRegReg(cUnit, kOpAdc, rlResult.lowReg, rlResult.lowReg, tReg);
3063 }
Bill Buzbee1f748632010-03-02 16:14:41 -08003064 storeValue(cUnit, rlDest, rlResult);
3065 return false;
3066}
3067
Elliott Hughesee34f592010-04-05 18:13:52 -07003068static bool genInlinedStringLength(CompilationUnit *cUnit, MIR *mir)
3069{
3070 return genInlinedStringIsEmptyOrLength(cUnit, mir, false);
3071}
3072
3073static bool genInlinedStringIsEmpty(CompilationUnit *cUnit, MIR *mir)
3074{
3075 return genInlinedStringIsEmptyOrLength(cUnit, mir, true);
3076}
3077
Bill Buzbee1f748632010-03-02 16:14:41 -08003078static bool genInlinedStringCharAt(CompilationUnit *cUnit, MIR *mir)
3079{
3080 int contents = offsetof(ArrayObject, contents);
3081 RegLocation rlObj = dvmCompilerGetSrc(cUnit, mir, 0);
3082 RegLocation rlIdx = dvmCompilerGetSrc(cUnit, mir, 1);
3083 RegLocation rlDest = inlinedTarget(cUnit, mir, false);
3084 RegLocation rlResult;
3085 rlObj = loadValue(cUnit, rlObj, kCoreReg);
3086 rlIdx = loadValue(cUnit, rlIdx, kCoreReg);
3087 int regMax = dvmCompilerAllocTemp(cUnit);
3088 int regOff = dvmCompilerAllocTemp(cUnit);
3089 int regPtr = dvmCompilerAllocTemp(cUnit);
3090 ArmLIR *pcrLabel = genNullCheck(cUnit, rlObj.sRegLow, rlObj.lowReg,
3091 mir->offset, NULL);
3092 loadWordDisp(cUnit, rlObj.lowReg, gDvm.offJavaLangString_count, regMax);
3093 loadWordDisp(cUnit, rlObj.lowReg, gDvm.offJavaLangString_offset, regOff);
3094 loadWordDisp(cUnit, rlObj.lowReg, gDvm.offJavaLangString_value, regPtr);
3095 genBoundsCheck(cUnit, rlIdx.lowReg, regMax, mir->offset, pcrLabel);
3096 dvmCompilerFreeTemp(cUnit, regMax);
3097 opRegImm(cUnit, kOpAdd, regPtr, contents);
3098 opRegReg(cUnit, kOpAdd, regOff, rlIdx.lowReg);
3099 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
3100 loadBaseIndexed(cUnit, regPtr, regOff, rlResult.lowReg, 1, kUnsignedHalf);
3101 storeValue(cUnit, rlDest, rlResult);
3102 return false;
3103}
3104
3105static bool genInlinedAbsInt(CompilationUnit *cUnit, MIR *mir)
3106{
3107 RegLocation rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
3108 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
3109 RegLocation rlDest = inlinedTarget(cUnit, mir, false);;
3110 RegLocation rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
3111 int signReg = dvmCompilerAllocTemp(cUnit);
3112 /*
3113 * abs(x) = y<=x>>31, (x+y)^y.
3114 * Thumb2's IT block also yields 3 instructions, but imposes
3115 * scheduling constraints.
3116 */
3117 opRegRegImm(cUnit, kOpAsr, signReg, rlSrc.lowReg, 31);
3118 opRegRegReg(cUnit, kOpAdd, rlResult.lowReg, rlSrc.lowReg, signReg);
3119 opRegReg(cUnit, kOpXor, rlResult.lowReg, signReg);
3120 storeValue(cUnit, rlDest, rlResult);
3121 return false;
3122}
3123
3124static bool genInlinedAbsLong(CompilationUnit *cUnit, MIR *mir)
3125{
3126 RegLocation rlSrc = dvmCompilerGetSrcWide(cUnit, mir, 0, 1);
3127 RegLocation rlDest = inlinedTargetWide(cUnit, mir, false);
3128 rlSrc = loadValueWide(cUnit, rlSrc, kCoreReg);
3129 RegLocation rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
3130 int signReg = dvmCompilerAllocTemp(cUnit);
3131 /*
3132 * abs(x) = y<=x>>31, (x+y)^y.
3133 * Thumb2 IT block allows slightly shorter sequence,
3134 * but introduces a scheduling barrier. Stick with this
3135 * mechanism for now.
3136 */
3137 opRegRegImm(cUnit, kOpAsr, signReg, rlSrc.highReg, 31);
3138 opRegRegReg(cUnit, kOpAdd, rlResult.lowReg, rlSrc.lowReg, signReg);
3139 opRegRegReg(cUnit, kOpAdc, rlResult.highReg, rlSrc.highReg, signReg);
3140 opRegReg(cUnit, kOpXor, rlResult.lowReg, signReg);
3141 opRegReg(cUnit, kOpXor, rlResult.highReg, signReg);
3142 storeValueWide(cUnit, rlDest, rlResult);
3143 return false;
3144}
Bill Buzbeefd023aa2009-11-02 09:23:49 -08003145
3146/*
Bill Buzbeece46c942009-11-20 15:41:34 -08003147 * NOTE: Handles both range and non-range versions (arguments
3148 * have already been normalized by this point).
Ben Chengba4fc8b2009-06-01 13:00:29 -07003149 */
Bill Buzbeece46c942009-11-20 15:41:34 -08003150static bool handleExecuteInline(CompilationUnit *cUnit, MIR *mir)
Ben Chengba4fc8b2009-06-01 13:00:29 -07003151{
3152 DecodedInstruction *dInsn = &mir->dalvikInsn;
3153 switch( mir->dalvikInsn.opCode) {
Bill Buzbeece46c942009-11-20 15:41:34 -08003154 case OP_EXECUTE_INLINE_RANGE:
Ben Chengba4fc8b2009-06-01 13:00:29 -07003155 case OP_EXECUTE_INLINE: {
3156 unsigned int i;
3157 const InlineOperation* inLineTable = dvmGetInlineOpsTable();
Bill Buzbee50a6bf22009-07-08 13:08:04 -07003158 int offset = offsetof(InterpState, retval);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003159 int operation = dInsn->vB;
Bill Buzbee50a6bf22009-07-08 13:08:04 -07003160 switch (operation) {
3161 case INLINE_EMPTYINLINEMETHOD:
3162 return false; /* Nop */
3163 case INLINE_STRING_LENGTH:
3164 return genInlinedStringLength(cUnit, mir);
Elliott Hughesee34f592010-04-05 18:13:52 -07003165 case INLINE_STRING_IS_EMPTY:
3166 return genInlinedStringIsEmpty(cUnit, mir);
Bill Buzbee50a6bf22009-07-08 13:08:04 -07003167 case INLINE_MATH_ABS_INT:
3168 return genInlinedAbsInt(cUnit, mir);
3169 case INLINE_MATH_ABS_LONG:
3170 return genInlinedAbsLong(cUnit, mir);
3171 case INLINE_MATH_MIN_INT:
3172 return genInlinedMinMaxInt(cUnit, mir, true);
3173 case INLINE_MATH_MAX_INT:
3174 return genInlinedMinMaxInt(cUnit, mir, false);
3175 case INLINE_STRING_CHARAT:
3176 return genInlinedStringCharAt(cUnit, mir);
3177 case INLINE_MATH_SQRT:
3178 if (genInlineSqrt(cUnit, mir))
Bill Buzbee9727c3d2009-08-01 11:32:36 -07003179 return false;
Bill Buzbee50a6bf22009-07-08 13:08:04 -07003180 else
3181 break; /* Handle with C routine */
Bill Buzbee50a6bf22009-07-08 13:08:04 -07003182 case INLINE_MATH_ABS_FLOAT:
Bill Buzbee1465db52009-09-23 17:17:35 -07003183 if (genInlinedAbsFloat(cUnit, mir))
3184 return false;
3185 else
3186 break;
Bill Buzbee50a6bf22009-07-08 13:08:04 -07003187 case INLINE_MATH_ABS_DOUBLE:
Bill Buzbee1465db52009-09-23 17:17:35 -07003188 if (genInlinedAbsDouble(cUnit, mir))
3189 return false;
3190 else
3191 break;
Bill Buzbee50a6bf22009-07-08 13:08:04 -07003192 case INLINE_STRING_COMPARETO:
Bill Buzbeefd023aa2009-11-02 09:23:49 -08003193 if (genInlinedCompareTo(cUnit, mir))
3194 return false;
3195 else
3196 break;
Elliott Hughes2bdbcb62010-04-12 14:29:37 -07003197 case INLINE_STRING_FASTINDEXOF_II:
3198 if (genInlinedFastIndexOf(cUnit, mir))
Bill Buzbeefd023aa2009-11-02 09:23:49 -08003199 return false;
3200 else
3201 break;
3202 case INLINE_STRING_EQUALS:
3203 case INLINE_MATH_COS:
3204 case INLINE_MATH_SIN:
3205 break; /* Handle with C routine */
Bill Buzbee50a6bf22009-07-08 13:08:04 -07003206 default:
Bill Buzbeefc519dc2010-03-06 23:30:57 -08003207 dvmCompilerAbort(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003208 }
Bill Buzbeec6f10662010-02-09 11:16:15 -08003209 dvmCompilerFlushAllRegs(cUnit); /* Everything to home location */
Elliott Hughes6a555132010-02-25 15:41:42 -08003210 dvmCompilerClobberCallRegs(cUnit);
Bill Buzbeec6f10662010-02-09 11:16:15 -08003211 dvmCompilerClobber(cUnit, r4PC);
3212 dvmCompilerClobber(cUnit, r7);
Bill Buzbee1465db52009-09-23 17:17:35 -07003213 opRegRegImm(cUnit, kOpAdd, r4PC, rGLUE, offset);
3214 opImm(cUnit, kOpPush, (1<<r4PC) | (1<<r7));
Ben Chengbd1326d2010-04-02 15:04:53 -07003215 LOAD_FUNC_ADDR(cUnit, r4PC, (int)inLineTable[operation].func);
Bill Buzbee1465db52009-09-23 17:17:35 -07003216 genExportPC(cUnit, mir);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003217 for (i=0; i < dInsn->vA; i++) {
Bill Buzbeec6f10662010-02-09 11:16:15 -08003218 loadValueDirect(cUnit, dvmCompilerGetSrc(cUnit, mir, i), i);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003219 }
Bill Buzbee1465db52009-09-23 17:17:35 -07003220 opReg(cUnit, kOpBlx, r4PC);
3221 opRegImm(cUnit, kOpAdd, r13, 8);
Bill Buzbeece46c942009-11-20 15:41:34 -08003222 opRegImm(cUnit, kOpCmp, r0, 0); /* NULL? */
3223 ArmLIR *branchOver = opCondBranch(cUnit, kArmCondNe);
3224 loadConstant(cUnit, r0,
3225 (int) (cUnit->method->insns + mir->offset));
3226 genDispatchToHandler(cUnit, TEMPLATE_THROW_EXCEPTION_COMMON);
3227 ArmLIR *target = newLIR0(cUnit, kArmPseudoTargetLabel);
3228 target->defMask = ENCODE_ALL;
3229 branchOver->generic.target = (LIR *) target;
Ben Chengba4fc8b2009-06-01 13:00:29 -07003230 break;
3231 }
3232 default:
3233 return true;
3234 }
3235 return false;
3236}
3237
3238static bool handleFmt51l(CompilationUnit *cUnit, MIR *mir)
3239{
Bill Buzbee1465db52009-09-23 17:17:35 -07003240 //TUNING: We're using core regs here - not optimal when target is a double
Bill Buzbeec6f10662010-02-09 11:16:15 -08003241 RegLocation rlDest = dvmCompilerGetDestWide(cUnit, mir, 0, 1);
3242 RegLocation rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Ben Chengbd1326d2010-04-02 15:04:53 -07003243 loadConstantNoClobber(cUnit, rlResult.lowReg,
3244 mir->dalvikInsn.vB_wide & 0xFFFFFFFFUL);
3245 loadConstantNoClobber(cUnit, rlResult.highReg,
3246 (mir->dalvikInsn.vB_wide>>32) & 0xFFFFFFFFUL);
Bill Buzbee1465db52009-09-23 17:17:35 -07003247 storeValueWide(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003248 return false;
3249}
3250
Ben Chengba4fc8b2009-06-01 13:00:29 -07003251/*
3252 * The following are special processing routines that handle transfer of
3253 * controls between compiled code and the interpreter. Certain VM states like
3254 * Dalvik PC and special-purpose registers are reconstructed here.
3255 */
3256
Bill Buzbeebd047242010-05-13 13:02:53 -07003257/*
3258 * Insert a
3259 * b .+4
3260 * nop
3261 * pair at the beginning of a chaining cell. This serves as the
3262 * switch branch that selects between reverting to the interpreter or
3263 * not. Once the cell is chained to a translation, the cell will
3264 * contain a 32-bit branch. Subsequent chain/unchain operations will
3265 * then only alter that first 16-bits - the "b .+4" for unchaining,
3266 * and the restoration of the first half of the 32-bit branch for
3267 * rechaining.
3268 */
3269static void insertChainingSwitch(CompilationUnit *cUnit)
3270{
3271 ArmLIR *branch = newLIR0(cUnit, kThumbBUncond);
3272 newLIR2(cUnit, kThumbOrr, r0, r0);
3273 ArmLIR *target = newLIR0(cUnit, kArmPseudoTargetLabel);
3274 target->defMask = ENCODE_ALL;
3275 branch->generic.target = (LIR *) target;
3276}
3277
Ben Cheng1efc9c52009-06-08 18:25:27 -07003278/* Chaining cell for code that may need warmup. */
3279static void handleNormalChainingCell(CompilationUnit *cUnit,
3280 unsigned int offset)
Ben Chengba4fc8b2009-06-01 13:00:29 -07003281{
Ben Cheng11d8f142010-03-24 15:24:19 -07003282 /*
3283 * Use raw instruction constructors to guarantee that the generated
3284 * instructions fit the predefined cell size.
3285 */
Bill Buzbeebd047242010-05-13 13:02:53 -07003286 insertChainingSwitch(cUnit);
Ben Cheng11d8f142010-03-24 15:24:19 -07003287 newLIR3(cUnit, kThumbLdrRRI5, r0, rGLUE,
3288 offsetof(InterpState,
3289 jitToInterpEntries.dvmJitToInterpNormal) >> 2);
3290 newLIR1(cUnit, kThumbBlxR, r0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003291 addWordData(cUnit, (int) (cUnit->method->insns + offset), true);
3292}
3293
3294/*
Ben Cheng1efc9c52009-06-08 18:25:27 -07003295 * Chaining cell for instructions that immediately following already translated
3296 * code.
Ben Chengba4fc8b2009-06-01 13:00:29 -07003297 */
Ben Cheng1efc9c52009-06-08 18:25:27 -07003298static void handleHotChainingCell(CompilationUnit *cUnit,
3299 unsigned int offset)
Ben Chengba4fc8b2009-06-01 13:00:29 -07003300{
Ben Cheng11d8f142010-03-24 15:24:19 -07003301 /*
3302 * Use raw instruction constructors to guarantee that the generated
3303 * instructions fit the predefined cell size.
3304 */
Bill Buzbeebd047242010-05-13 13:02:53 -07003305 insertChainingSwitch(cUnit);
Ben Cheng11d8f142010-03-24 15:24:19 -07003306 newLIR3(cUnit, kThumbLdrRRI5, r0, rGLUE,
3307 offsetof(InterpState,
3308 jitToInterpEntries.dvmJitToInterpTraceSelect) >> 2);
3309 newLIR1(cUnit, kThumbBlxR, r0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003310 addWordData(cUnit, (int) (cUnit->method->insns + offset), true);
3311}
3312
Bill Buzbee9c4b7c82009-09-10 10:10:38 -07003313#if defined(WITH_SELF_VERIFICATION) || defined(WITH_JIT_TUNING)
Jeff Hao97319a82009-08-12 16:57:15 -07003314/* Chaining cell for branches that branch back into the same basic block */
3315static void handleBackwardBranchChainingCell(CompilationUnit *cUnit,
3316 unsigned int offset)
3317{
Ben Cheng11d8f142010-03-24 15:24:19 -07003318 /*
3319 * Use raw instruction constructors to guarantee that the generated
3320 * instructions fit the predefined cell size.
3321 */
Bill Buzbeebd047242010-05-13 13:02:53 -07003322 insertChainingSwitch(cUnit);
Bill Buzbee9c4b7c82009-09-10 10:10:38 -07003323#if defined(WITH_SELF_VERIFICATION)
Bill Buzbee1465db52009-09-23 17:17:35 -07003324 newLIR3(cUnit, kThumbLdrRRI5, r0, rGLUE,
Ben Cheng40094c12010-02-24 20:58:44 -08003325 offsetof(InterpState,
3326 jitToInterpEntries.dvmJitToInterpBackwardBranch) >> 2);
Bill Buzbee9c4b7c82009-09-10 10:10:38 -07003327#else
Bill Buzbee1465db52009-09-23 17:17:35 -07003328 newLIR3(cUnit, kThumbLdrRRI5, r0, rGLUE,
Bill Buzbee9c4b7c82009-09-10 10:10:38 -07003329 offsetof(InterpState, jitToInterpEntries.dvmJitToInterpNormal) >> 2);
3330#endif
Bill Buzbee1465db52009-09-23 17:17:35 -07003331 newLIR1(cUnit, kThumbBlxR, r0);
Jeff Hao97319a82009-08-12 16:57:15 -07003332 addWordData(cUnit, (int) (cUnit->method->insns + offset), true);
3333}
3334
3335#endif
Ben Chengba4fc8b2009-06-01 13:00:29 -07003336/* Chaining cell for monomorphic method invocations. */
Ben Cheng38329f52009-07-07 14:19:20 -07003337static void handleInvokeSingletonChainingCell(CompilationUnit *cUnit,
3338 const Method *callee)
Ben Chengba4fc8b2009-06-01 13:00:29 -07003339{
Ben Cheng11d8f142010-03-24 15:24:19 -07003340 /*
3341 * Use raw instruction constructors to guarantee that the generated
3342 * instructions fit the predefined cell size.
3343 */
Bill Buzbeebd047242010-05-13 13:02:53 -07003344 insertChainingSwitch(cUnit);
Ben Cheng11d8f142010-03-24 15:24:19 -07003345 newLIR3(cUnit, kThumbLdrRRI5, r0, rGLUE,
3346 offsetof(InterpState,
3347 jitToInterpEntries.dvmJitToInterpTraceSelect) >> 2);
3348 newLIR1(cUnit, kThumbBlxR, r0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003349 addWordData(cUnit, (int) (callee->insns), true);
3350}
3351
Ben Cheng38329f52009-07-07 14:19:20 -07003352/* Chaining cell for monomorphic method invocations. */
3353static void handleInvokePredictedChainingCell(CompilationUnit *cUnit)
3354{
3355
3356 /* Should not be executed in the initial state */
3357 addWordData(cUnit, PREDICTED_CHAIN_BX_PAIR_INIT, true);
3358 /* To be filled: class */
3359 addWordData(cUnit, PREDICTED_CHAIN_CLAZZ_INIT, true);
3360 /* To be filled: method */
3361 addWordData(cUnit, PREDICTED_CHAIN_METHOD_INIT, true);
3362 /*
3363 * Rechain count. The initial value of 0 here will trigger chaining upon
3364 * the first invocation of this callsite.
3365 */
3366 addWordData(cUnit, PREDICTED_CHAIN_COUNTER_INIT, true);
3367}
3368
Ben Chengba4fc8b2009-06-01 13:00:29 -07003369/* Load the Dalvik PC into r0 and jump to the specified target */
3370static void handlePCReconstruction(CompilationUnit *cUnit,
Bill Buzbee89efc3d2009-07-28 11:22:22 -07003371 ArmLIR *targetLabel)
Ben Chengba4fc8b2009-06-01 13:00:29 -07003372{
Bill Buzbee89efc3d2009-07-28 11:22:22 -07003373 ArmLIR **pcrLabel =
3374 (ArmLIR **) cUnit->pcReconstructionList.elemList;
Ben Chengba4fc8b2009-06-01 13:00:29 -07003375 int numElems = cUnit->pcReconstructionList.numUsed;
3376 int i;
3377 for (i = 0; i < numElems; i++) {
3378 dvmCompilerAppendLIR(cUnit, (LIR *) pcrLabel[i]);
3379 /* r0 = dalvik PC */
3380 loadConstant(cUnit, r0, pcrLabel[i]->operands[0]);
3381 genUnconditionalBranch(cUnit, targetLabel);
3382 }
3383}
3384
Bill Buzbee1465db52009-09-23 17:17:35 -07003385static char *extendedMIROpNames[kMirOpLast - kMirOpFirst] = {
3386 "kMirOpPhi",
3387 "kMirOpNullNRangeUpCheck",
3388 "kMirOpNullNRangeDownCheck",
3389 "kMirOpLowerBound",
3390 "kMirOpPunt",
Ben Cheng4238ec22009-08-24 16:32:22 -07003391};
3392
3393/*
3394 * vA = arrayReg;
3395 * vB = idxReg;
3396 * vC = endConditionReg;
3397 * arg[0] = maxC
3398 * arg[1] = minC
3399 * arg[2] = loopBranchConditionCode
3400 */
3401static void genHoistedChecksForCountUpLoop(CompilationUnit *cUnit, MIR *mir)
3402{
Bill Buzbee1465db52009-09-23 17:17:35 -07003403 /*
3404 * NOTE: these synthesized blocks don't have ssa names assigned
3405 * for Dalvik registers. However, because they dominate the following
3406 * blocks we can simply use the Dalvik name w/ subscript 0 as the
3407 * ssa name.
3408 */
Ben Cheng4238ec22009-08-24 16:32:22 -07003409 DecodedInstruction *dInsn = &mir->dalvikInsn;
3410 const int lenOffset = offsetof(ArrayObject, length);
Ben Cheng4238ec22009-08-24 16:32:22 -07003411 const int maxC = dInsn->arg[0];
Bill Buzbee1465db52009-09-23 17:17:35 -07003412 int regLength;
3413 RegLocation rlArray = cUnit->regLocation[mir->dalvikInsn.vA];
3414 RegLocation rlIdxEnd = cUnit->regLocation[mir->dalvikInsn.vC];
Ben Cheng4238ec22009-08-24 16:32:22 -07003415
3416 /* regArray <- arrayRef */
Bill Buzbee1465db52009-09-23 17:17:35 -07003417 rlArray = loadValue(cUnit, rlArray, kCoreReg);
3418 rlIdxEnd = loadValue(cUnit, rlIdxEnd, kCoreReg);
3419 genRegImmCheck(cUnit, kArmCondEq, rlArray.lowReg, 0, 0,
Ben Cheng4238ec22009-08-24 16:32:22 -07003420 (ArmLIR *) cUnit->loopAnalysis->branchToPCR);
3421
3422 /* regLength <- len(arrayRef) */
Bill Buzbeec6f10662010-02-09 11:16:15 -08003423 regLength = dvmCompilerAllocTemp(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07003424 loadWordDisp(cUnit, rlArray.lowReg, lenOffset, regLength);
Ben Cheng4238ec22009-08-24 16:32:22 -07003425
3426 int delta = maxC;
3427 /*
3428 * If the loop end condition is ">=" instead of ">", then the largest value
3429 * of the index is "endCondition - 1".
3430 */
3431 if (dInsn->arg[2] == OP_IF_GE) {
3432 delta--;
3433 }
3434
3435 if (delta) {
Bill Buzbeec6f10662010-02-09 11:16:15 -08003436 int tReg = dvmCompilerAllocTemp(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07003437 opRegRegImm(cUnit, kOpAdd, tReg, rlIdxEnd.lowReg, delta);
3438 rlIdxEnd.lowReg = tReg;
Bill Buzbeec6f10662010-02-09 11:16:15 -08003439 dvmCompilerFreeTemp(cUnit, tReg);
Ben Cheng4238ec22009-08-24 16:32:22 -07003440 }
3441 /* Punt if "regIdxEnd < len(Array)" is false */
Bill Buzbee1465db52009-09-23 17:17:35 -07003442 genRegRegCheck(cUnit, kArmCondGe, rlIdxEnd.lowReg, regLength, 0,
Ben Cheng0fd31e42009-09-03 14:40:16 -07003443 (ArmLIR *) cUnit->loopAnalysis->branchToPCR);
Ben Cheng4238ec22009-08-24 16:32:22 -07003444}
3445
3446/*
3447 * vA = arrayReg;
3448 * vB = idxReg;
3449 * vC = endConditionReg;
3450 * arg[0] = maxC
3451 * arg[1] = minC
3452 * arg[2] = loopBranchConditionCode
3453 */
3454static void genHoistedChecksForCountDownLoop(CompilationUnit *cUnit, MIR *mir)
3455{
3456 DecodedInstruction *dInsn = &mir->dalvikInsn;
3457 const int lenOffset = offsetof(ArrayObject, length);
Bill Buzbeec6f10662010-02-09 11:16:15 -08003458 const int regLength = dvmCompilerAllocTemp(cUnit);
Ben Cheng4238ec22009-08-24 16:32:22 -07003459 const int maxC = dInsn->arg[0];
Bill Buzbee1465db52009-09-23 17:17:35 -07003460 RegLocation rlArray = cUnit->regLocation[mir->dalvikInsn.vA];
3461 RegLocation rlIdxInit = cUnit->regLocation[mir->dalvikInsn.vB];
Ben Cheng4238ec22009-08-24 16:32:22 -07003462
3463 /* regArray <- arrayRef */
Bill Buzbee1465db52009-09-23 17:17:35 -07003464 rlArray = loadValue(cUnit, rlArray, kCoreReg);
3465 rlIdxInit = loadValue(cUnit, rlIdxInit, kCoreReg);
3466 genRegImmCheck(cUnit, kArmCondEq, rlArray.lowReg, 0, 0,
Ben Cheng4238ec22009-08-24 16:32:22 -07003467 (ArmLIR *) cUnit->loopAnalysis->branchToPCR);
3468
3469 /* regLength <- len(arrayRef) */
Bill Buzbee1465db52009-09-23 17:17:35 -07003470 loadWordDisp(cUnit, rlArray.lowReg, lenOffset, regLength);
Ben Cheng4238ec22009-08-24 16:32:22 -07003471
3472 if (maxC) {
Bill Buzbeec6f10662010-02-09 11:16:15 -08003473 int tReg = dvmCompilerAllocTemp(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07003474 opRegRegImm(cUnit, kOpAdd, tReg, rlIdxInit.lowReg, maxC);
3475 rlIdxInit.lowReg = tReg;
Bill Buzbeec6f10662010-02-09 11:16:15 -08003476 dvmCompilerFreeTemp(cUnit, tReg);
Ben Cheng4238ec22009-08-24 16:32:22 -07003477 }
3478
3479 /* Punt if "regIdxInit < len(Array)" is false */
Bill Buzbee1465db52009-09-23 17:17:35 -07003480 genRegRegCheck(cUnit, kArmCondGe, rlIdxInit.lowReg, regLength, 0,
Ben Cheng0fd31e42009-09-03 14:40:16 -07003481 (ArmLIR *) cUnit->loopAnalysis->branchToPCR);
Ben Cheng4238ec22009-08-24 16:32:22 -07003482}
3483
3484/*
3485 * vA = idxReg;
3486 * vB = minC;
3487 */
3488static void genHoistedLowerBoundCheck(CompilationUnit *cUnit, MIR *mir)
3489{
3490 DecodedInstruction *dInsn = &mir->dalvikInsn;
Ben Cheng4238ec22009-08-24 16:32:22 -07003491 const int minC = dInsn->vB;
Bill Buzbee1465db52009-09-23 17:17:35 -07003492 RegLocation rlIdx = cUnit->regLocation[mir->dalvikInsn.vA];
Ben Cheng4238ec22009-08-24 16:32:22 -07003493
3494 /* regIdx <- initial index value */
Bill Buzbee1465db52009-09-23 17:17:35 -07003495 rlIdx = loadValue(cUnit, rlIdx, kCoreReg);
Ben Cheng4238ec22009-08-24 16:32:22 -07003496
3497 /* Punt if "regIdxInit + minC >= 0" is false */
Bill Buzbee1465db52009-09-23 17:17:35 -07003498 genRegImmCheck(cUnit, kArmCondLt, rlIdx.lowReg, -minC, 0,
Ben Cheng4238ec22009-08-24 16:32:22 -07003499 (ArmLIR *) cUnit->loopAnalysis->branchToPCR);
3500}
3501
3502/* Extended MIR instructions like PHI */
3503static void handleExtendedMIR(CompilationUnit *cUnit, MIR *mir)
3504{
Bill Buzbee1465db52009-09-23 17:17:35 -07003505 int opOffset = mir->dalvikInsn.opCode - kMirOpFirst;
Ben Cheng4238ec22009-08-24 16:32:22 -07003506 char *msg = dvmCompilerNew(strlen(extendedMIROpNames[opOffset]) + 1,
3507 false);
3508 strcpy(msg, extendedMIROpNames[opOffset]);
Bill Buzbee1465db52009-09-23 17:17:35 -07003509 newLIR1(cUnit, kArmPseudoExtended, (int) msg);
Ben Cheng4238ec22009-08-24 16:32:22 -07003510
3511 switch (mir->dalvikInsn.opCode) {
Bill Buzbee1465db52009-09-23 17:17:35 -07003512 case kMirOpPhi: {
Ben Cheng4238ec22009-08-24 16:32:22 -07003513 char *ssaString = dvmCompilerGetSSAString(cUnit, mir->ssaRep);
Bill Buzbee1465db52009-09-23 17:17:35 -07003514 newLIR1(cUnit, kArmPseudoSSARep, (int) ssaString);
Ben Cheng4238ec22009-08-24 16:32:22 -07003515 break;
3516 }
Bill Buzbee1465db52009-09-23 17:17:35 -07003517 case kMirOpNullNRangeUpCheck: {
Ben Cheng4238ec22009-08-24 16:32:22 -07003518 genHoistedChecksForCountUpLoop(cUnit, mir);
3519 break;
3520 }
Bill Buzbee1465db52009-09-23 17:17:35 -07003521 case kMirOpNullNRangeDownCheck: {
Ben Cheng4238ec22009-08-24 16:32:22 -07003522 genHoistedChecksForCountDownLoop(cUnit, mir);
3523 break;
3524 }
Bill Buzbee1465db52009-09-23 17:17:35 -07003525 case kMirOpLowerBound: {
Ben Cheng4238ec22009-08-24 16:32:22 -07003526 genHoistedLowerBoundCheck(cUnit, mir);
3527 break;
3528 }
Bill Buzbee1465db52009-09-23 17:17:35 -07003529 case kMirOpPunt: {
Ben Cheng4238ec22009-08-24 16:32:22 -07003530 genUnconditionalBranch(cUnit,
3531 (ArmLIR *) cUnit->loopAnalysis->branchToPCR);
3532 break;
3533 }
3534 default:
3535 break;
3536 }
3537}
3538
3539/*
3540 * Create a PC-reconstruction cell for the starting offset of this trace.
3541 * Since the PCR cell is placed near the end of the compiled code which is
3542 * usually out of range for a conditional branch, we put two branches (one
3543 * branch over to the loop body and one layover branch to the actual PCR) at the
3544 * end of the entry block.
3545 */
3546static void setupLoopEntryBlock(CompilationUnit *cUnit, BasicBlock *entry,
3547 ArmLIR *bodyLabel)
3548{
3549 /* Set up the place holder to reconstruct this Dalvik PC */
3550 ArmLIR *pcrLabel = dvmCompilerNew(sizeof(ArmLIR), true);
Ben Chenga4973592010-03-31 11:59:18 -07003551 pcrLabel->opCode = kArmPseudoPCReconstructionCell;
Ben Cheng4238ec22009-08-24 16:32:22 -07003552 pcrLabel->operands[0] =
3553 (int) (cUnit->method->insns + entry->startOffset);
3554 pcrLabel->operands[1] = entry->startOffset;
3555 /* Insert the place holder to the growable list */
3556 dvmInsertGrowableList(&cUnit->pcReconstructionList, pcrLabel);
3557
3558 /*
3559 * Next, create two branches - one branch over to the loop body and the
3560 * other branch to the PCR cell to punt.
3561 */
3562 ArmLIR *branchToBody = dvmCompilerNew(sizeof(ArmLIR), true);
Bill Buzbee1465db52009-09-23 17:17:35 -07003563 branchToBody->opCode = kThumbBUncond;
Ben Cheng4238ec22009-08-24 16:32:22 -07003564 branchToBody->generic.target = (LIR *) bodyLabel;
Ben Chengdcf3e5d2009-09-11 13:42:05 -07003565 setupResourceMasks(branchToBody);
Ben Cheng4238ec22009-08-24 16:32:22 -07003566 cUnit->loopAnalysis->branchToBody = (LIR *) branchToBody;
3567
3568 ArmLIR *branchToPCR = dvmCompilerNew(sizeof(ArmLIR), true);
Bill Buzbee1465db52009-09-23 17:17:35 -07003569 branchToPCR->opCode = kThumbBUncond;
Ben Cheng4238ec22009-08-24 16:32:22 -07003570 branchToPCR->generic.target = (LIR *) pcrLabel;
Ben Chengdcf3e5d2009-09-11 13:42:05 -07003571 setupResourceMasks(branchToPCR);
Ben Cheng4238ec22009-08-24 16:32:22 -07003572 cUnit->loopAnalysis->branchToPCR = (LIR *) branchToPCR;
3573}
3574
Ben Chengd5adae12010-03-26 17:45:28 -07003575#if defined(WITH_SELF_VERIFICATION)
3576static bool selfVerificationPuntOps(MIR *mir)
3577{
3578 DecodedInstruction *decInsn = &mir->dalvikInsn;
3579 OpCode op = decInsn->opCode;
3580 int flags = dexGetInstrFlags(gDvm.instrFlags, op);
3581 /*
3582 * All opcodes that can throw exceptions and use the
3583 * TEMPLATE_THROW_EXCEPTION_COMMON template should be excluded in the trace
3584 * under self-verification mode.
3585 */
3586 return (op == OP_MONITOR_ENTER || op == OP_MONITOR_EXIT ||
3587 op == OP_NEW_INSTANCE || op == OP_NEW_ARRAY ||
3588 op == OP_CHECK_CAST || op == OP_MOVE_EXCEPTION ||
3589 op == OP_FILL_ARRAY_DATA || op == OP_EXECUTE_INLINE ||
3590 op == OP_EXECUTE_INLINE_RANGE ||
3591 (flags & kInstrInvoke));
3592}
3593#endif
3594
Ben Chengba4fc8b2009-06-01 13:00:29 -07003595void dvmCompilerMIR2LIR(CompilationUnit *cUnit)
3596{
3597 /* Used to hold the labels of each block */
Bill Buzbee89efc3d2009-07-28 11:22:22 -07003598 ArmLIR *labelList =
3599 dvmCompilerNew(sizeof(ArmLIR) * cUnit->numBlocks, true);
Ben Chengcec26f62010-01-15 15:29:33 -08003600 GrowableList chainingListByType[kChainingCellGap];
Ben Chengba4fc8b2009-06-01 13:00:29 -07003601 int i;
3602
3603 /*
Ben Cheng38329f52009-07-07 14:19:20 -07003604 * Initialize various types chaining lists.
Ben Chengba4fc8b2009-06-01 13:00:29 -07003605 */
Ben Chengcec26f62010-01-15 15:29:33 -08003606 for (i = 0; i < kChainingCellGap; i++) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07003607 dvmInitGrowableList(&chainingListByType[i], 2);
3608 }
3609
3610 BasicBlock **blockList = cUnit->blockList;
3611
Bill Buzbee6e963e12009-06-17 16:56:19 -07003612 if (cUnit->executionCount) {
3613 /*
3614 * Reserve 6 bytes at the beginning of the trace
3615 * +----------------------------+
3616 * | execution count (4 bytes) |
3617 * +----------------------------+
3618 * | chain cell offset (2 bytes)|
3619 * +----------------------------+
3620 * ...and then code to increment the execution
3621 * count:
3622 * mov r0, pc @ move adr of "mov r0,pc" + 4 to r0
3623 * sub r0, #10 @ back up to addr of executionCount
3624 * ldr r1, [r0]
3625 * add r1, #1
3626 * str r1, [r0]
3627 */
Bill Buzbee1465db52009-09-23 17:17:35 -07003628 newLIR1(cUnit, kArm16BitData, 0);
3629 newLIR1(cUnit, kArm16BitData, 0);
Ben Chengcc6600c2009-06-22 14:45:16 -07003630 cUnit->chainCellOffsetLIR =
Bill Buzbee1465db52009-09-23 17:17:35 -07003631 (LIR *) newLIR1(cUnit, kArm16BitData, CHAIN_CELL_OFFSET_TAG);
Bill Buzbee6e963e12009-06-17 16:56:19 -07003632 cUnit->headerSize = 6;
Bill Buzbee270c1d62009-08-13 16:58:07 -07003633 /* Thumb instruction used directly here to ensure correct size */
Bill Buzbee1465db52009-09-23 17:17:35 -07003634 newLIR2(cUnit, kThumbMovRR_H2L, r0, rpc);
3635 newLIR2(cUnit, kThumbSubRI8, r0, 10);
3636 newLIR3(cUnit, kThumbLdrRRI5, r1, r0, 0);
3637 newLIR2(cUnit, kThumbAddRI8, r1, 1);
3638 newLIR3(cUnit, kThumbStrRRI5, r1, r0, 0);
Bill Buzbee6e963e12009-06-17 16:56:19 -07003639 } else {
3640 /* Just reserve 2 bytes for the chain cell offset */
Ben Chengcc6600c2009-06-22 14:45:16 -07003641 cUnit->chainCellOffsetLIR =
Bill Buzbee1465db52009-09-23 17:17:35 -07003642 (LIR *) newLIR1(cUnit, kArm16BitData, CHAIN_CELL_OFFSET_TAG);
Bill Buzbee6e963e12009-06-17 16:56:19 -07003643 cUnit->headerSize = 2;
3644 }
Ben Cheng1efc9c52009-06-08 18:25:27 -07003645
Ben Chengba4fc8b2009-06-01 13:00:29 -07003646 /* Handle the content in each basic block */
3647 for (i = 0; i < cUnit->numBlocks; i++) {
3648 blockList[i]->visited = true;
3649 MIR *mir;
3650
3651 labelList[i].operands[0] = blockList[i]->startOffset;
3652
Ben Chengcec26f62010-01-15 15:29:33 -08003653 if (blockList[i]->blockType >= kChainingCellGap) {
Ben Chengd44faf52010-06-02 15:33:51 -07003654 if (blockList[i]->firstMIRInsn != NULL &&
3655 ((blockList[i]->firstMIRInsn->dalvikInsn.opCode ==
3656 OP_MOVE_RESULT) ||
3657 (blockList[i]->firstMIRInsn->dalvikInsn.opCode ==
3658 OP_MOVE_RESULT_WIDE) ||
3659 (blockList[i]->firstMIRInsn->dalvikInsn.opCode ==
3660 OP_MOVE_RESULT_OBJECT))) {
3661 /* Align this block first since it is a return chaining cell */
3662 newLIR0(cUnit, kArmPseudoPseudoAlign4);
3663 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07003664 /*
3665 * Append the label pseudo LIR first. Chaining cells will be handled
3666 * separately afterwards.
3667 */
3668 dvmCompilerAppendLIR(cUnit, (LIR *) &labelList[i]);
3669 }
3670
Bill Buzbee1465db52009-09-23 17:17:35 -07003671 if (blockList[i]->blockType == kEntryBlock) {
Ben Chenga4973592010-03-31 11:59:18 -07003672 labelList[i].opCode = kArmPseudoEntryBlock;
Ben Cheng4238ec22009-08-24 16:32:22 -07003673 if (blockList[i]->firstMIRInsn == NULL) {
3674 continue;
3675 } else {
3676 setupLoopEntryBlock(cUnit, blockList[i],
3677 &labelList[blockList[i]->fallThrough->id]);
3678 }
Bill Buzbee1465db52009-09-23 17:17:35 -07003679 } else if (blockList[i]->blockType == kExitBlock) {
Ben Chenga4973592010-03-31 11:59:18 -07003680 labelList[i].opCode = kArmPseudoExitBlock;
Ben Cheng4238ec22009-08-24 16:32:22 -07003681 goto gen_fallthrough;
Bill Buzbee1465db52009-09-23 17:17:35 -07003682 } else if (blockList[i]->blockType == kDalvikByteCode) {
3683 labelList[i].opCode = kArmPseudoNormalBlockLabel;
Ben Chenge9695e52009-06-16 16:11:47 -07003684 /* Reset the register state */
Bill Buzbeec6f10662010-02-09 11:16:15 -08003685 dvmCompilerResetRegPool(cUnit);
3686 dvmCompilerClobberAllRegs(cUnit);
3687 dvmCompilerResetNullCheck(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003688 } else {
3689 switch (blockList[i]->blockType) {
Bill Buzbee1465db52009-09-23 17:17:35 -07003690 case kChainingCellNormal:
Ben Chenga4973592010-03-31 11:59:18 -07003691 labelList[i].opCode = kArmPseudoChainingCellNormal;
Ben Chengba4fc8b2009-06-01 13:00:29 -07003692 /* handle the codegen later */
3693 dvmInsertGrowableList(
Bill Buzbee1465db52009-09-23 17:17:35 -07003694 &chainingListByType[kChainingCellNormal], (void *) i);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003695 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07003696 case kChainingCellInvokeSingleton:
Ben Cheng38329f52009-07-07 14:19:20 -07003697 labelList[i].opCode =
Ben Chenga4973592010-03-31 11:59:18 -07003698 kArmPseudoChainingCellInvokeSingleton;
Ben Chengba4fc8b2009-06-01 13:00:29 -07003699 labelList[i].operands[0] =
3700 (int) blockList[i]->containingMethod;
3701 /* handle the codegen later */
3702 dvmInsertGrowableList(
Bill Buzbee1465db52009-09-23 17:17:35 -07003703 &chainingListByType[kChainingCellInvokeSingleton],
Ben Cheng38329f52009-07-07 14:19:20 -07003704 (void *) i);
3705 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07003706 case kChainingCellInvokePredicted:
Ben Cheng38329f52009-07-07 14:19:20 -07003707 labelList[i].opCode =
Ben Chenga4973592010-03-31 11:59:18 -07003708 kArmPseudoChainingCellInvokePredicted;
Ben Cheng38329f52009-07-07 14:19:20 -07003709 /* handle the codegen later */
3710 dvmInsertGrowableList(
Bill Buzbee1465db52009-09-23 17:17:35 -07003711 &chainingListByType[kChainingCellInvokePredicted],
Ben Cheng38329f52009-07-07 14:19:20 -07003712 (void *) i);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003713 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07003714 case kChainingCellHot:
Ben Chengba4fc8b2009-06-01 13:00:29 -07003715 labelList[i].opCode =
Ben Chenga4973592010-03-31 11:59:18 -07003716 kArmPseudoChainingCellHot;
Ben Chengba4fc8b2009-06-01 13:00:29 -07003717 /* handle the codegen later */
3718 dvmInsertGrowableList(
Bill Buzbee1465db52009-09-23 17:17:35 -07003719 &chainingListByType[kChainingCellHot],
Ben Chengba4fc8b2009-06-01 13:00:29 -07003720 (void *) i);
3721 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07003722 case kPCReconstruction:
Ben Chengba4fc8b2009-06-01 13:00:29 -07003723 /* Make sure exception handling block is next */
3724 labelList[i].opCode =
Ben Chenga4973592010-03-31 11:59:18 -07003725 kArmPseudoPCReconstructionBlockLabel;
Ben Chengba4fc8b2009-06-01 13:00:29 -07003726 assert (i == cUnit->numBlocks - 2);
3727 handlePCReconstruction(cUnit, &labelList[i+1]);
3728 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07003729 case kExceptionHandling:
3730 labelList[i].opCode = kArmPseudoEHBlockLabel;
Ben Chengba4fc8b2009-06-01 13:00:29 -07003731 if (cUnit->pcReconstructionList.numUsed) {
Bill Buzbee270c1d62009-08-13 16:58:07 -07003732 loadWordDisp(cUnit, rGLUE, offsetof(InterpState,
3733 jitToInterpEntries.dvmJitToInterpPunt),
3734 r1);
Bill Buzbee1465db52009-09-23 17:17:35 -07003735 opReg(cUnit, kOpBlx, r1);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003736 }
3737 break;
Bill Buzbee9c4b7c82009-09-10 10:10:38 -07003738#if defined(WITH_SELF_VERIFICATION) || defined(WITH_JIT_TUNING)
Bill Buzbee1465db52009-09-23 17:17:35 -07003739 case kChainingCellBackwardBranch:
Jeff Hao97319a82009-08-12 16:57:15 -07003740 labelList[i].opCode =
Ben Chenga4973592010-03-31 11:59:18 -07003741 kArmPseudoChainingCellBackwardBranch;
Jeff Hao97319a82009-08-12 16:57:15 -07003742 /* handle the codegen later */
3743 dvmInsertGrowableList(
Bill Buzbee1465db52009-09-23 17:17:35 -07003744 &chainingListByType[kChainingCellBackwardBranch],
Jeff Hao97319a82009-08-12 16:57:15 -07003745 (void *) i);
3746 break;
3747#endif
Ben Chengba4fc8b2009-06-01 13:00:29 -07003748 default:
3749 break;
3750 }
3751 continue;
3752 }
Ben Chenge9695e52009-06-16 16:11:47 -07003753
Bill Buzbee89efc3d2009-07-28 11:22:22 -07003754 ArmLIR *headLIR = NULL;
Ben Chenge9695e52009-06-16 16:11:47 -07003755
Ben Chengba4fc8b2009-06-01 13:00:29 -07003756 for (mir = blockList[i]->firstMIRInsn; mir; mir = mir->next) {
Bill Buzbee1465db52009-09-23 17:17:35 -07003757
Bill Buzbeec6f10662010-02-09 11:16:15 -08003758 dvmCompilerResetRegPool(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07003759 if (gDvmJit.disableOpt & (1 << kTrackLiveTemps)) {
Bill Buzbeec6f10662010-02-09 11:16:15 -08003760 dvmCompilerClobberAllRegs(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07003761 }
3762
3763 if (gDvmJit.disableOpt & (1 << kSuppressLoads)) {
Bill Buzbeec6f10662010-02-09 11:16:15 -08003764 dvmCompilerResetDefTracking(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07003765 }
3766
3767 if (mir->dalvikInsn.opCode >= kMirOpFirst) {
Ben Cheng4238ec22009-08-24 16:32:22 -07003768 handleExtendedMIR(cUnit, mir);
3769 continue;
3770 }
3771
Bill Buzbee1465db52009-09-23 17:17:35 -07003772
Ben Chengba4fc8b2009-06-01 13:00:29 -07003773 OpCode dalvikOpCode = mir->dalvikInsn.opCode;
3774 InstructionFormat dalvikFormat =
3775 dexGetInstrFormat(gDvm.instrFormat, dalvikOpCode);
Bill Buzbee89efc3d2009-07-28 11:22:22 -07003776 ArmLIR *boundaryLIR =
Ben Chenga4973592010-03-31 11:59:18 -07003777 newLIR2(cUnit, kArmPseudoDalvikByteCodeBoundary,
Ben Chengccd6c012009-10-15 14:52:45 -07003778 mir->offset,
3779 (int) dvmCompilerGetDalvikDisassembly(&mir->dalvikInsn)
3780 );
Ben Cheng4238ec22009-08-24 16:32:22 -07003781 if (mir->ssaRep) {
3782 char *ssaString = dvmCompilerGetSSAString(cUnit, mir->ssaRep);
Bill Buzbee1465db52009-09-23 17:17:35 -07003783 newLIR1(cUnit, kArmPseudoSSARep, (int) ssaString);
Ben Cheng4238ec22009-08-24 16:32:22 -07003784 }
3785
Ben Chenge9695e52009-06-16 16:11:47 -07003786 /* Remember the first LIR for this block */
3787 if (headLIR == NULL) {
3788 headLIR = boundaryLIR;
Ben Chengd7d426a2009-09-22 11:23:36 -07003789 /* Set the first boundaryLIR as a scheduling barrier */
3790 headLIR->defMask = ENCODE_ALL;
Ben Chenge9695e52009-06-16 16:11:47 -07003791 }
Ben Cheng4238ec22009-08-24 16:32:22 -07003792
Ben Chengba4fc8b2009-06-01 13:00:29 -07003793 bool notHandled;
3794 /*
3795 * Debugging: screen the opcode first to see if it is in the
3796 * do[-not]-compile list
3797 */
3798 bool singleStepMe =
3799 gDvmJit.includeSelectedOp !=
3800 ((gDvmJit.opList[dalvikOpCode >> 3] &
3801 (1 << (dalvikOpCode & 0x7))) !=
3802 0);
Ben Chengd5adae12010-03-26 17:45:28 -07003803#if defined(WITH_SELF_VERIFICATION)
3804 if (singleStepMe == false) {
3805 singleStepMe = selfVerificationPuntOps(mir);
3806 }
3807#endif
Ben Chengba4fc8b2009-06-01 13:00:29 -07003808 if (singleStepMe || cUnit->allSingleStep) {
3809 notHandled = false;
3810 genInterpSingleStep(cUnit, mir);
3811 } else {
3812 opcodeCoverage[dalvikOpCode]++;
3813 switch (dalvikFormat) {
3814 case kFmt10t:
3815 case kFmt20t:
3816 case kFmt30t:
3817 notHandled = handleFmt10t_Fmt20t_Fmt30t(cUnit,
3818 mir, blockList[i], labelList);
3819 break;
3820 case kFmt10x:
3821 notHandled = handleFmt10x(cUnit, mir);
3822 break;
3823 case kFmt11n:
3824 case kFmt31i:
3825 notHandled = handleFmt11n_Fmt31i(cUnit, mir);
3826 break;
3827 case kFmt11x:
3828 notHandled = handleFmt11x(cUnit, mir);
3829 break;
3830 case kFmt12x:
3831 notHandled = handleFmt12x(cUnit, mir);
3832 break;
3833 case kFmt20bc:
3834 notHandled = handleFmt20bc(cUnit, mir);
3835 break;
3836 case kFmt21c:
3837 case kFmt31c:
3838 notHandled = handleFmt21c_Fmt31c(cUnit, mir);
3839 break;
3840 case kFmt21h:
3841 notHandled = handleFmt21h(cUnit, mir);
3842 break;
3843 case kFmt21s:
3844 notHandled = handleFmt21s(cUnit, mir);
3845 break;
3846 case kFmt21t:
3847 notHandled = handleFmt21t(cUnit, mir, blockList[i],
3848 labelList);
3849 break;
3850 case kFmt22b:
3851 case kFmt22s:
3852 notHandled = handleFmt22b_Fmt22s(cUnit, mir);
3853 break;
3854 case kFmt22c:
3855 notHandled = handleFmt22c(cUnit, mir);
3856 break;
3857 case kFmt22cs:
3858 notHandled = handleFmt22cs(cUnit, mir);
3859 break;
3860 case kFmt22t:
3861 notHandled = handleFmt22t(cUnit, mir, blockList[i],
3862 labelList);
3863 break;
3864 case kFmt22x:
3865 case kFmt32x:
3866 notHandled = handleFmt22x_Fmt32x(cUnit, mir);
3867 break;
3868 case kFmt23x:
3869 notHandled = handleFmt23x(cUnit, mir);
3870 break;
3871 case kFmt31t:
3872 notHandled = handleFmt31t(cUnit, mir);
3873 break;
3874 case kFmt3rc:
3875 case kFmt35c:
3876 notHandled = handleFmt35c_3rc(cUnit, mir, blockList[i],
3877 labelList);
3878 break;
3879 case kFmt3rms:
3880 case kFmt35ms:
3881 notHandled = handleFmt35ms_3rms(cUnit, mir,blockList[i],
3882 labelList);
3883 break;
3884 case kFmt3inline:
Andy McFaddenb0a05412009-11-19 10:23:41 -08003885 case kFmt3rinline:
Bill Buzbeece46c942009-11-20 15:41:34 -08003886 notHandled = handleExecuteInline(cUnit, mir);
Andy McFaddenb0a05412009-11-19 10:23:41 -08003887 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07003888 case kFmt51l:
3889 notHandled = handleFmt51l(cUnit, mir);
3890 break;
3891 default:
3892 notHandled = true;
3893 break;
3894 }
3895 }
3896 if (notHandled) {
3897 LOGE("%#06x: Opcode 0x%x (%s) / Fmt %d not handled\n",
3898 mir->offset,
Andy McFaddenc6b25c72010-06-22 11:01:20 -07003899 dalvikOpCode, dexGetOpcodeName(dalvikOpCode),
Ben Chengba4fc8b2009-06-01 13:00:29 -07003900 dalvikFormat);
Bill Buzbeefc519dc2010-03-06 23:30:57 -08003901 dvmCompilerAbort(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003902 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07003903 }
3904 }
Ben Cheng4238ec22009-08-24 16:32:22 -07003905
Bill Buzbee1465db52009-09-23 17:17:35 -07003906 if (blockList[i]->blockType == kEntryBlock) {
Ben Cheng4238ec22009-08-24 16:32:22 -07003907 dvmCompilerAppendLIR(cUnit,
3908 (LIR *) cUnit->loopAnalysis->branchToBody);
3909 dvmCompilerAppendLIR(cUnit,
3910 (LIR *) cUnit->loopAnalysis->branchToPCR);
3911 }
3912
3913 if (headLIR) {
3914 /*
3915 * Eliminate redundant loads/stores and delay stores into later
3916 * slots
3917 */
3918 dvmCompilerApplyLocalOptimizations(cUnit, (LIR *) headLIR,
3919 cUnit->lastLIRInsn);
3920 }
3921
3922gen_fallthrough:
Ben Cheng1efc9c52009-06-08 18:25:27 -07003923 /*
3924 * Check if the block is terminated due to trace length constraint -
3925 * insert an unconditional branch to the chaining cell.
3926 */
3927 if (blockList[i]->needFallThroughBranch) {
3928 genUnconditionalBranch(cUnit,
3929 &labelList[blockList[i]->fallThrough->id]);
3930 }
3931
Ben Chengba4fc8b2009-06-01 13:00:29 -07003932 }
3933
Ben Chenge9695e52009-06-16 16:11:47 -07003934 /* Handle the chaining cells in predefined order */
Ben Chengcec26f62010-01-15 15:29:33 -08003935 for (i = 0; i < kChainingCellGap; i++) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07003936 size_t j;
3937 int *blockIdList = (int *) chainingListByType[i].elemList;
3938
3939 cUnit->numChainingCells[i] = chainingListByType[i].numUsed;
3940
3941 /* No chaining cells of this type */
3942 if (cUnit->numChainingCells[i] == 0)
3943 continue;
3944
3945 /* Record the first LIR for a new type of chaining cell */
3946 cUnit->firstChainingLIR[i] = (LIR *) &labelList[blockIdList[0]];
3947
3948 for (j = 0; j < chainingListByType[i].numUsed; j++) {
3949 int blockId = blockIdList[j];
3950
3951 /* Align this chaining cell first */
Bill Buzbee1465db52009-09-23 17:17:35 -07003952 newLIR0(cUnit, kArmPseudoPseudoAlign4);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003953
3954 /* Insert the pseudo chaining instruction */
3955 dvmCompilerAppendLIR(cUnit, (LIR *) &labelList[blockId]);
3956
3957
3958 switch (blockList[blockId]->blockType) {
Bill Buzbee1465db52009-09-23 17:17:35 -07003959 case kChainingCellNormal:
Ben Cheng1efc9c52009-06-08 18:25:27 -07003960 handleNormalChainingCell(cUnit,
Ben Chengba4fc8b2009-06-01 13:00:29 -07003961 blockList[blockId]->startOffset);
3962 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07003963 case kChainingCellInvokeSingleton:
Ben Cheng38329f52009-07-07 14:19:20 -07003964 handleInvokeSingletonChainingCell(cUnit,
Ben Chengba4fc8b2009-06-01 13:00:29 -07003965 blockList[blockId]->containingMethod);
3966 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07003967 case kChainingCellInvokePredicted:
Ben Cheng38329f52009-07-07 14:19:20 -07003968 handleInvokePredictedChainingCell(cUnit);
3969 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07003970 case kChainingCellHot:
Ben Cheng1efc9c52009-06-08 18:25:27 -07003971 handleHotChainingCell(cUnit,
Ben Chengba4fc8b2009-06-01 13:00:29 -07003972 blockList[blockId]->startOffset);
3973 break;
Bill Buzbee9c4b7c82009-09-10 10:10:38 -07003974#if defined(WITH_SELF_VERIFICATION) || defined(WITH_JIT_TUNING)
Bill Buzbee1465db52009-09-23 17:17:35 -07003975 case kChainingCellBackwardBranch:
Jeff Hao97319a82009-08-12 16:57:15 -07003976 handleBackwardBranchChainingCell(cUnit,
3977 blockList[blockId]->startOffset);
3978 break;
3979#endif
Ben Chengba4fc8b2009-06-01 13:00:29 -07003980 default:
Bill Buzbee1465db52009-09-23 17:17:35 -07003981 LOGE("Bad blocktype %d", blockList[blockId]->blockType);
Bill Buzbeefc519dc2010-03-06 23:30:57 -08003982 dvmCompilerAbort(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003983 }
3984 }
3985 }
Ben Chenge9695e52009-06-16 16:11:47 -07003986
Ben Chengcec26f62010-01-15 15:29:33 -08003987 /* Mark the bottom of chaining cells */
3988 cUnit->chainingCellBottom = (LIR *) newLIR0(cUnit, kArmChainingCellBottom);
3989
Ben Cheng6c10a972009-10-29 14:39:18 -07003990 /*
3991 * Generate the branch to the dvmJitToInterpNoChain entry point at the end
3992 * of all chaining cells for the overflow cases.
3993 */
3994 if (cUnit->switchOverflowPad) {
3995 loadConstant(cUnit, r0, (int) cUnit->switchOverflowPad);
3996 loadWordDisp(cUnit, rGLUE, offsetof(InterpState,
3997 jitToInterpEntries.dvmJitToInterpNoChain), r2);
3998 opRegReg(cUnit, kOpAdd, r1, r1);
3999 opRegRegReg(cUnit, kOpAdd, r4PC, r0, r1);
Ben Cheng978738d2010-05-13 13:45:57 -07004000#if defined(WITH_JIT_TUNING)
Ben Cheng6c10a972009-10-29 14:39:18 -07004001 loadConstant(cUnit, r0, kSwitchOverflow);
4002#endif
4003 opReg(cUnit, kOpBlx, r2);
4004 }
4005
Ben Chenge9695e52009-06-16 16:11:47 -07004006 dvmCompilerApplyGlobalOptimizations(cUnit);
jeffhao9e45c0b2010-02-03 10:24:05 -08004007
4008#if defined(WITH_SELF_VERIFICATION)
4009 selfVerificationBranchInsertPass(cUnit);
4010#endif
Ben Chengba4fc8b2009-06-01 13:00:29 -07004011}
4012
4013/* Accept the work and start compiling */
Bill Buzbee716f1202009-07-23 13:22:09 -07004014bool dvmCompilerDoWork(CompilerWorkOrder *work)
Ben Chengba4fc8b2009-06-01 13:00:29 -07004015{
Ben Chengccd6c012009-10-15 14:52:45 -07004016 bool res;
Ben Chengba4fc8b2009-06-01 13:00:29 -07004017
Ben Cheng6999d842010-01-26 16:46:15 -08004018 if (gDvmJit.codeCacheFull) {
Ben Chengccd6c012009-10-15 14:52:45 -07004019 return false;
4020 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07004021
Ben Chengccd6c012009-10-15 14:52:45 -07004022 switch (work->kind) {
4023 case kWorkOrderMethod:
4024 res = dvmCompileMethod(work->info, &work->result);
4025 break;
4026 case kWorkOrderTrace:
4027 /* Start compilation with maximally allowed trace length */
Bill Buzbeefc519dc2010-03-06 23:30:57 -08004028 res = dvmCompileTrace(work->info, JIT_MAX_TRACE_LEN, &work->result,
4029 work->bailPtr);
Ben Chengccd6c012009-10-15 14:52:45 -07004030 break;
4031 case kWorkOrderTraceDebug: {
4032 bool oldPrintMe = gDvmJit.printMe;
4033 gDvmJit.printMe = true;
4034 /* Start compilation with maximally allowed trace length */
Bill Buzbeefc519dc2010-03-06 23:30:57 -08004035 res = dvmCompileTrace(work->info, JIT_MAX_TRACE_LEN, &work->result,
4036 work->bailPtr);
Elliott Hughes672511b2010-04-26 17:40:13 -07004037 gDvmJit.printMe = oldPrintMe;
Ben Chengccd6c012009-10-15 14:52:45 -07004038 break;
4039 }
4040 default:
4041 res = false;
Bill Buzbeefc519dc2010-03-06 23:30:57 -08004042 LOGE("Jit: unknown work order type");
Elliott Hughes672511b2010-04-26 17:40:13 -07004043 assert(0); // Bail if debug build, discard otherwise
Ben Chengccd6c012009-10-15 14:52:45 -07004044 }
4045 return res;
Ben Chengba4fc8b2009-06-01 13:00:29 -07004046}
4047
Ben Chengba4fc8b2009-06-01 13:00:29 -07004048/* Architectural-specific debugging helpers go here */
4049void dvmCompilerArchDump(void)
4050{
4051 /* Print compiled opcode in this VM instance */
4052 int i, start, streak;
4053 char buf[1024];
4054
4055 streak = i = 0;
4056 buf[0] = 0;
4057 while (opcodeCoverage[i] == 0 && i < 256) {
4058 i++;
4059 }
4060 if (i == 256) {
4061 return;
4062 }
4063 for (start = i++, streak = 1; i < 256; i++) {
4064 if (opcodeCoverage[i]) {
4065 streak++;
4066 } else {
4067 if (streak == 1) {
4068 sprintf(buf+strlen(buf), "%x,", start);
4069 } else {
4070 sprintf(buf+strlen(buf), "%x-%x,", start, start + streak - 1);
4071 }
4072 streak = 0;
4073 while (opcodeCoverage[i] == 0 && i < 256) {
4074 i++;
4075 }
4076 if (i < 256) {
4077 streak = 1;
4078 start = i;
4079 }
4080 }
4081 }
4082 if (streak) {
4083 if (streak == 1) {
4084 sprintf(buf+strlen(buf), "%x", start);
4085 } else {
4086 sprintf(buf+strlen(buf), "%x-%x", start, start + streak - 1);
4087 }
4088 }
4089 if (strlen(buf)) {
Ben Cheng8b258bf2009-06-24 17:27:07 -07004090 LOGD("dalvik.vm.jit.op = %s", buf);
Ben Chengba4fc8b2009-06-01 13:00:29 -07004091 }
4092}
Ben Chengd7d426a2009-09-22 11:23:36 -07004093
4094/* Common initialization routine for an architecture family */
4095bool dvmCompilerArchInit()
4096{
4097 int i;
4098
Bill Buzbee1465db52009-09-23 17:17:35 -07004099 for (i = 0; i < kArmLast; i++) {
Ben Chengd7d426a2009-09-22 11:23:36 -07004100 if (EncodingMap[i].opCode != i) {
4101 LOGE("Encoding order for %s is wrong: expecting %d, seeing %d",
4102 EncodingMap[i].name, i, EncodingMap[i].opCode);
Bill Buzbeefc519dc2010-03-06 23:30:57 -08004103 dvmAbort(); // OK to dvmAbort - build error
Ben Chengd7d426a2009-09-22 11:23:36 -07004104 }
4105 }
4106
Ben Cheng5d90c202009-11-22 23:31:11 -08004107 return dvmCompilerArchVariantInit();
4108}
4109
4110void *dvmCompilerGetInterpretTemplate()
4111{
4112 return (void*) ((int)gDvmJit.codeCache +
4113 templateEntryOffsets[TEMPLATE_INTERPRET]);
4114}
4115
4116/* Needed by the ld/st optmizatons */
4117ArmLIR* dvmCompilerRegCopyNoInsert(CompilationUnit *cUnit, int rDest, int rSrc)
4118{
4119 return genRegCopyNoInsert(cUnit, rDest, rSrc);
4120}
4121
4122/* Needed by the register allocator */
4123ArmLIR* dvmCompilerRegCopy(CompilationUnit *cUnit, int rDest, int rSrc)
4124{
4125 return genRegCopy(cUnit, rDest, rSrc);
4126}
4127
4128/* Needed by the register allocator */
4129void dvmCompilerRegCopyWide(CompilationUnit *cUnit, int destLo, int destHi,
4130 int srcLo, int srcHi)
4131{
4132 genRegCopyWide(cUnit, destLo, destHi, srcLo, srcHi);
4133}
4134
4135void dvmCompilerFlushRegImpl(CompilationUnit *cUnit, int rBase,
4136 int displacement, int rSrc, OpSize size)
4137{
4138 storeBaseDisp(cUnit, rBase, displacement, rSrc, size);
4139}
4140
4141void dvmCompilerFlushRegWideImpl(CompilationUnit *cUnit, int rBase,
4142 int displacement, int rSrcLo, int rSrcHi)
4143{
4144 storeBaseDispWide(cUnit, rBase, displacement, rSrcLo, rSrcHi);
Ben Chengd7d426a2009-09-22 11:23:36 -07004145}