blob: 8d63c66236a9d4c4974e7ce99172722a45769b78 [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;
200 ArmLIR *branchLIR = dvmCompilerNew(sizeof(ArmLIR), true);
201 TemplateOpCode opCode = TEMPLATE_MEM_OP_DECODE;
Jeff Hao97319a82009-08-12 16:57:15 -0700202
jeffhao9e45c0b2010-02-03 10:24:05 -0800203 for (thisLIR = (ArmLIR *) cUnit->firstLIRInsn;
204 thisLIR != (ArmLIR *) cUnit->lastLIRInsn;
205 thisLIR = NEXT_LIR(thisLIR)) {
206 if (thisLIR->branchInsertSV) {
207 /* Branch to mem op decode template */
208 selfVerificationBranchInsert((LIR *) thisLIR, kThumbBlx1,
209 (int) gDvmJit.codeCache + templateEntryOffsets[opCode],
210 (int) gDvmJit.codeCache + templateEntryOffsets[opCode]);
211 selfVerificationBranchInsert((LIR *) thisLIR, kThumbBlx2,
212 (int) gDvmJit.codeCache + templateEntryOffsets[opCode],
213 (int) gDvmJit.codeCache + templateEntryOffsets[opCode]);
Jeff Hao97319a82009-08-12 16:57:15 -0700214 }
215 }
Jeff Hao97319a82009-08-12 16:57:15 -0700216}
Jeff Hao97319a82009-08-12 16:57:15 -0700217#endif
218
Bill Buzbeebe6534f2010-03-12 16:01:35 -0800219/* Generate conditional branch instructions */
220static ArmLIR *genConditionalBranch(CompilationUnit *cUnit,
221 ArmConditionCode cond,
222 ArmLIR *target)
223{
224 ArmLIR *branch = opCondBranch(cUnit, cond);
225 branch->generic.target = (LIR *) target;
226 return branch;
227}
228
Ben Chengba4fc8b2009-06-01 13:00:29 -0700229/* Generate a unconditional branch to go to the interpreter */
Bill Buzbee89efc3d2009-07-28 11:22:22 -0700230static inline ArmLIR *genTrap(CompilationUnit *cUnit, int dOffset,
231 ArmLIR *pcrLabel)
Ben Chengba4fc8b2009-06-01 13:00:29 -0700232{
Bill Buzbee1465db52009-09-23 17:17:35 -0700233 ArmLIR *branch = opNone(cUnit, kOpUncondBr);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700234 return genCheckCommon(cUnit, dOffset, branch, pcrLabel);
235}
236
237/* Load a wide field from an object instance */
238static void genIGetWide(CompilationUnit *cUnit, MIR *mir, int fieldOffset)
239{
240 DecodedInstruction *dInsn = &mir->dalvikInsn;
Bill Buzbeec6f10662010-02-09 11:16:15 -0800241 RegLocation rlObj = dvmCompilerGetSrc(cUnit, mir, 0);
242 RegLocation rlDest = dvmCompilerGetDestWide(cUnit, mir, 0, 1);
Bill Buzbee1465db52009-09-23 17:17:35 -0700243 RegLocation rlResult;
244 rlObj = loadValue(cUnit, rlObj, kCoreReg);
Bill Buzbeec6f10662010-02-09 11:16:15 -0800245 int regPtr = dvmCompilerAllocTemp(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700246
Bill Buzbee1465db52009-09-23 17:17:35 -0700247 assert(rlDest.wide);
Ben Chenge9695e52009-06-16 16:11:47 -0700248
Bill Buzbee1465db52009-09-23 17:17:35 -0700249 genNullCheck(cUnit, rlObj.sRegLow, rlObj.lowReg, mir->offset,
250 NULL);/* null object? */
251 opRegRegImm(cUnit, kOpAdd, regPtr, rlObj.lowReg, fieldOffset);
Bill Buzbeec6f10662010-02-09 11:16:15 -0800252 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kAnyReg, true);
Ben Cheng11d8f142010-03-24 15:24:19 -0700253
254 HEAP_ACCESS_SHADOW(true);
Bill Buzbee1465db52009-09-23 17:17:35 -0700255 loadPair(cUnit, regPtr, rlResult.lowReg, rlResult.highReg);
Ben Cheng11d8f142010-03-24 15:24:19 -0700256 HEAP_ACCESS_SHADOW(false);
257
Bill Buzbeec6f10662010-02-09 11:16:15 -0800258 dvmCompilerFreeTemp(cUnit, regPtr);
Bill Buzbee1465db52009-09-23 17:17:35 -0700259 storeValueWide(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700260}
261
262/* Store a wide field to an object instance */
263static void genIPutWide(CompilationUnit *cUnit, MIR *mir, int fieldOffset)
264{
265 DecodedInstruction *dInsn = &mir->dalvikInsn;
Bill Buzbeec6f10662010-02-09 11:16:15 -0800266 RegLocation rlSrc = dvmCompilerGetSrcWide(cUnit, mir, 0, 1);
267 RegLocation rlObj = dvmCompilerGetSrc(cUnit, mir, 2);
Bill Buzbee1465db52009-09-23 17:17:35 -0700268 rlObj = loadValue(cUnit, rlObj, kCoreReg);
269 int regPtr;
270 rlSrc = loadValueWide(cUnit, rlSrc, kAnyReg);
271 genNullCheck(cUnit, rlObj.sRegLow, rlObj.lowReg, mir->offset,
272 NULL);/* null object? */
Bill Buzbeec6f10662010-02-09 11:16:15 -0800273 regPtr = dvmCompilerAllocTemp(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -0700274 opRegRegImm(cUnit, kOpAdd, regPtr, rlObj.lowReg, fieldOffset);
Ben Cheng11d8f142010-03-24 15:24:19 -0700275
276 HEAP_ACCESS_SHADOW(true);
Bill Buzbee1465db52009-09-23 17:17:35 -0700277 storePair(cUnit, regPtr, rlSrc.lowReg, rlSrc.highReg);
Ben Cheng11d8f142010-03-24 15:24:19 -0700278 HEAP_ACCESS_SHADOW(false);
279
Bill Buzbeec6f10662010-02-09 11:16:15 -0800280 dvmCompilerFreeTemp(cUnit, regPtr);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700281}
282
283/*
284 * Load a field from an object instance
285 *
Ben Chengba4fc8b2009-06-01 13:00:29 -0700286 */
Bill Buzbee270c1d62009-08-13 16:58:07 -0700287static void genIGet(CompilationUnit *cUnit, MIR *mir, OpSize size,
Ben Chengba4fc8b2009-06-01 13:00:29 -0700288 int fieldOffset)
289{
Bill Buzbee1465db52009-09-23 17:17:35 -0700290 int regPtr;
291 RegLocation rlResult;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700292 DecodedInstruction *dInsn = &mir->dalvikInsn;
Bill Buzbeec6f10662010-02-09 11:16:15 -0800293 RegLocation rlObj = dvmCompilerGetSrc(cUnit, mir, 0);
294 RegLocation rlDest = dvmCompilerGetDest(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -0700295 rlObj = loadValue(cUnit, rlObj, kCoreReg);
Bill Buzbeec6f10662010-02-09 11:16:15 -0800296 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kAnyReg, true);
Bill Buzbee1465db52009-09-23 17:17:35 -0700297 genNullCheck(cUnit, rlObj.sRegLow, rlObj.lowReg, mir->offset,
298 NULL);/* null object? */
Ben Cheng11d8f142010-03-24 15:24:19 -0700299
300 HEAP_ACCESS_SHADOW(true);
Ben Cheng5d90c202009-11-22 23:31:11 -0800301 loadBaseDisp(cUnit, mir, rlObj.lowReg, fieldOffset, rlResult.lowReg,
302 size, rlObj.sRegLow);
Ben Cheng11d8f142010-03-24 15:24:19 -0700303 HEAP_ACCESS_SHADOW(false);
304
Bill Buzbee1465db52009-09-23 17:17:35 -0700305 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700306}
307
308/*
309 * Store a field to an object instance
310 *
Ben Chengba4fc8b2009-06-01 13:00:29 -0700311 */
Bill Buzbee270c1d62009-08-13 16:58:07 -0700312static void genIPut(CompilationUnit *cUnit, MIR *mir, OpSize size,
Ben Chengba4fc8b2009-06-01 13:00:29 -0700313 int fieldOffset)
314{
315 DecodedInstruction *dInsn = &mir->dalvikInsn;
Bill Buzbeec6f10662010-02-09 11:16:15 -0800316 RegLocation rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
317 RegLocation rlObj = dvmCompilerGetSrc(cUnit, mir, 1);
Bill Buzbee1465db52009-09-23 17:17:35 -0700318 rlObj = loadValue(cUnit, rlObj, kCoreReg);
319 rlSrc = loadValue(cUnit, rlSrc, kAnyReg);
320 int regPtr;
321 genNullCheck(cUnit, rlObj.sRegLow, rlObj.lowReg, mir->offset,
322 NULL);/* null object? */
Ben Cheng11d8f142010-03-24 15:24:19 -0700323
324 HEAP_ACCESS_SHADOW(true);
Bill Buzbee1465db52009-09-23 17:17:35 -0700325 storeBaseDisp(cUnit, rlObj.lowReg, fieldOffset, rlSrc.lowReg, size);
Ben Cheng11d8f142010-03-24 15:24:19 -0700326 HEAP_ACCESS_SHADOW(false);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700327}
328
329
Ben Chengba4fc8b2009-06-01 13:00:29 -0700330/*
331 * Generate array load
Ben Chengba4fc8b2009-06-01 13:00:29 -0700332 */
Bill Buzbee270c1d62009-08-13 16:58:07 -0700333static void genArrayGet(CompilationUnit *cUnit, MIR *mir, OpSize size,
Bill Buzbee1465db52009-09-23 17:17:35 -0700334 RegLocation rlArray, RegLocation rlIndex,
335 RegLocation rlDest, int scale)
Ben Chengba4fc8b2009-06-01 13:00:29 -0700336{
337 int lenOffset = offsetof(ArrayObject, length);
338 int dataOffset = offsetof(ArrayObject, contents);
Bill Buzbee1465db52009-09-23 17:17:35 -0700339 RegLocation rlResult;
340 rlArray = loadValue(cUnit, rlArray, kCoreReg);
341 rlIndex = loadValue(cUnit, rlIndex, kCoreReg);
342 int regPtr;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700343
344 /* null object? */
Ben Cheng4238ec22009-08-24 16:32:22 -0700345 ArmLIR * pcrLabel = NULL;
346
347 if (!(mir->OptimizationFlags & MIR_IGNORE_NULL_CHECK)) {
Bill Buzbee1465db52009-09-23 17:17:35 -0700348 pcrLabel = genNullCheck(cUnit, rlArray.sRegLow,
349 rlArray.lowReg, mir->offset, NULL);
Ben Cheng4238ec22009-08-24 16:32:22 -0700350 }
351
Bill Buzbeec6f10662010-02-09 11:16:15 -0800352 regPtr = dvmCompilerAllocTemp(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -0700353
Ben Cheng4238ec22009-08-24 16:32:22 -0700354 if (!(mir->OptimizationFlags & MIR_IGNORE_RANGE_CHECK)) {
Bill Buzbeec6f10662010-02-09 11:16:15 -0800355 int regLen = dvmCompilerAllocTemp(cUnit);
Ben Cheng4238ec22009-08-24 16:32:22 -0700356 /* Get len */
Bill Buzbee1465db52009-09-23 17:17:35 -0700357 loadWordDisp(cUnit, rlArray.lowReg, lenOffset, regLen);
358 /* regPtr -> array data */
359 opRegRegImm(cUnit, kOpAdd, regPtr, rlArray.lowReg, dataOffset);
360 genBoundsCheck(cUnit, rlIndex.lowReg, regLen, mir->offset,
361 pcrLabel);
Bill Buzbeec6f10662010-02-09 11:16:15 -0800362 dvmCompilerFreeTemp(cUnit, regLen);
Ben Cheng4238ec22009-08-24 16:32:22 -0700363 } else {
Bill Buzbee1465db52009-09-23 17:17:35 -0700364 /* regPtr -> array data */
365 opRegRegImm(cUnit, kOpAdd, regPtr, rlArray.lowReg, dataOffset);
Ben Cheng4238ec22009-08-24 16:32:22 -0700366 }
Bill Buzbee1465db52009-09-23 17:17:35 -0700367 if ((size == kLong) || (size == kDouble)) {
368 if (scale) {
Bill Buzbeec6f10662010-02-09 11:16:15 -0800369 int rNewIndex = dvmCompilerAllocTemp(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -0700370 opRegRegImm(cUnit, kOpLsl, rNewIndex, rlIndex.lowReg, scale);
371 opRegReg(cUnit, kOpAdd, regPtr, rNewIndex);
Bill Buzbeec6f10662010-02-09 11:16:15 -0800372 dvmCompilerFreeTemp(cUnit, rNewIndex);
Bill Buzbee1465db52009-09-23 17:17:35 -0700373 } else {
374 opRegReg(cUnit, kOpAdd, regPtr, rlIndex.lowReg);
375 }
Bill Buzbeec6f10662010-02-09 11:16:15 -0800376 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kAnyReg, true);
Ben Cheng11d8f142010-03-24 15:24:19 -0700377
378 HEAP_ACCESS_SHADOW(true);
Bill Buzbee1465db52009-09-23 17:17:35 -0700379 loadPair(cUnit, regPtr, rlResult.lowReg, rlResult.highReg);
Ben Cheng11d8f142010-03-24 15:24:19 -0700380 HEAP_ACCESS_SHADOW(false);
381
Bill Buzbeec6f10662010-02-09 11:16:15 -0800382 dvmCompilerFreeTemp(cUnit, regPtr);
Bill Buzbee1465db52009-09-23 17:17:35 -0700383 storeValueWide(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700384 } else {
Bill Buzbeec6f10662010-02-09 11:16:15 -0800385 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kAnyReg, true);
Ben Cheng11d8f142010-03-24 15:24:19 -0700386
387 HEAP_ACCESS_SHADOW(true);
Bill Buzbee1465db52009-09-23 17:17:35 -0700388 loadBaseIndexed(cUnit, regPtr, rlIndex.lowReg, rlResult.lowReg,
389 scale, size);
Ben Cheng11d8f142010-03-24 15:24:19 -0700390 HEAP_ACCESS_SHADOW(false);
391
Bill Buzbeec6f10662010-02-09 11:16:15 -0800392 dvmCompilerFreeTemp(cUnit, regPtr);
Bill Buzbee1465db52009-09-23 17:17:35 -0700393 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700394 }
395}
396
Ben Chengba4fc8b2009-06-01 13:00:29 -0700397/*
398 * Generate array store
399 *
Ben Chengba4fc8b2009-06-01 13:00:29 -0700400 */
Bill Buzbee270c1d62009-08-13 16:58:07 -0700401static void genArrayPut(CompilationUnit *cUnit, MIR *mir, OpSize size,
Bill Buzbee1465db52009-09-23 17:17:35 -0700402 RegLocation rlArray, RegLocation rlIndex,
403 RegLocation rlSrc, int scale)
Ben Chengba4fc8b2009-06-01 13:00:29 -0700404{
405 int lenOffset = offsetof(ArrayObject, length);
406 int dataOffset = offsetof(ArrayObject, contents);
407
Bill Buzbee1465db52009-09-23 17:17:35 -0700408 int regPtr;
409 rlArray = loadValue(cUnit, rlArray, kCoreReg);
410 rlIndex = loadValue(cUnit, rlIndex, kCoreReg);
Ben Chenge9695e52009-06-16 16:11:47 -0700411
Bill Buzbeec6f10662010-02-09 11:16:15 -0800412 if (dvmCompilerIsTemp(cUnit, rlArray.lowReg)) {
413 dvmCompilerClobber(cUnit, rlArray.lowReg);
Bill Buzbee1465db52009-09-23 17:17:35 -0700414 regPtr = rlArray.lowReg;
415 } else {
Bill Buzbeec6f10662010-02-09 11:16:15 -0800416 regPtr = dvmCompilerAllocTemp(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -0700417 genRegCopy(cUnit, regPtr, rlArray.lowReg);
418 }
Ben Chenge9695e52009-06-16 16:11:47 -0700419
Ben Cheng1efc9c52009-06-08 18:25:27 -0700420 /* null object? */
Ben Cheng4238ec22009-08-24 16:32:22 -0700421 ArmLIR * pcrLabel = NULL;
422
423 if (!(mir->OptimizationFlags & MIR_IGNORE_NULL_CHECK)) {
Bill Buzbee1465db52009-09-23 17:17:35 -0700424 pcrLabel = genNullCheck(cUnit, rlArray.sRegLow, rlArray.lowReg,
425 mir->offset, NULL);
Ben Cheng4238ec22009-08-24 16:32:22 -0700426 }
427
428 if (!(mir->OptimizationFlags & MIR_IGNORE_RANGE_CHECK)) {
Bill Buzbeec6f10662010-02-09 11:16:15 -0800429 int regLen = dvmCompilerAllocTemp(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -0700430 //NOTE: max live temps(4) here.
Ben Cheng4238ec22009-08-24 16:32:22 -0700431 /* Get len */
Bill Buzbee1465db52009-09-23 17:17:35 -0700432 loadWordDisp(cUnit, rlArray.lowReg, lenOffset, regLen);
433 /* regPtr -> array data */
434 opRegImm(cUnit, kOpAdd, regPtr, dataOffset);
435 genBoundsCheck(cUnit, rlIndex.lowReg, regLen, mir->offset,
436 pcrLabel);
Bill Buzbeec6f10662010-02-09 11:16:15 -0800437 dvmCompilerFreeTemp(cUnit, regLen);
Ben Cheng4238ec22009-08-24 16:32:22 -0700438 } else {
Bill Buzbee1465db52009-09-23 17:17:35 -0700439 /* regPtr -> array data */
440 opRegImm(cUnit, kOpAdd, regPtr, dataOffset);
Ben Cheng4238ec22009-08-24 16:32:22 -0700441 }
Bill Buzbee1465db52009-09-23 17:17:35 -0700442 /* at this point, regPtr points to array, 2 live temps */
Bill Buzbee1465db52009-09-23 17:17:35 -0700443 if ((size == kLong) || (size == kDouble)) {
444 //TODO: need specific wide routine that can handle fp regs
445 if (scale) {
Bill Buzbeec6f10662010-02-09 11:16:15 -0800446 int rNewIndex = dvmCompilerAllocTemp(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -0700447 opRegRegImm(cUnit, kOpLsl, rNewIndex, rlIndex.lowReg, scale);
448 opRegReg(cUnit, kOpAdd, regPtr, rNewIndex);
Bill Buzbeec6f10662010-02-09 11:16:15 -0800449 dvmCompilerFreeTemp(cUnit, rNewIndex);
Bill Buzbee1465db52009-09-23 17:17:35 -0700450 } else {
451 opRegReg(cUnit, kOpAdd, regPtr, rlIndex.lowReg);
452 }
453 rlSrc = loadValueWide(cUnit, rlSrc, kAnyReg);
Ben Cheng11d8f142010-03-24 15:24:19 -0700454
455 HEAP_ACCESS_SHADOW(true);
Bill Buzbee1465db52009-09-23 17:17:35 -0700456 storePair(cUnit, regPtr, rlSrc.lowReg, rlSrc.highReg);
Ben Cheng11d8f142010-03-24 15:24:19 -0700457 HEAP_ACCESS_SHADOW(false);
458
Bill Buzbeec6f10662010-02-09 11:16:15 -0800459 dvmCompilerFreeTemp(cUnit, regPtr);
Bill Buzbee270c1d62009-08-13 16:58:07 -0700460 } else {
Bill Buzbee1465db52009-09-23 17:17:35 -0700461 rlSrc = loadValue(cUnit, rlSrc, kAnyReg);
Ben Cheng11d8f142010-03-24 15:24:19 -0700462
463 HEAP_ACCESS_SHADOW(true);
Bill Buzbee1465db52009-09-23 17:17:35 -0700464 storeBaseIndexed(cUnit, regPtr, rlIndex.lowReg, rlSrc.lowReg,
465 scale, size);
Ben Cheng11d8f142010-03-24 15:24:19 -0700466 HEAP_ACCESS_SHADOW(false);
jeffhao9e45c0b2010-02-03 10:24:05 -0800467 }
Ben Chengba4fc8b2009-06-01 13:00:29 -0700468}
469
Bill Buzbeebe6534f2010-03-12 16:01:35 -0800470/*
471 * Generate array object store
472 * Must use explicit register allocation here because of
473 * call-out to dvmCanPutArrayElement
474 */
475static void genArrayObjectPut(CompilationUnit *cUnit, MIR *mir,
476 RegLocation rlArray, RegLocation rlIndex,
477 RegLocation rlSrc, int scale)
478{
479 int lenOffset = offsetof(ArrayObject, length);
480 int dataOffset = offsetof(ArrayObject, contents);
481
482 dvmCompilerFlushAllRegs(cUnit);
483
484 int regLen = r0;
485 int regPtr = r4PC; /* Preserved across call */
486 int regArray = r1;
487 int regIndex = r7; /* Preserved across call */
488
489 loadValueDirectFixed(cUnit, rlArray, regArray);
490 loadValueDirectFixed(cUnit, rlIndex, regIndex);
491
492 /* null object? */
493 ArmLIR * pcrLabel = NULL;
494
495 if (!(mir->OptimizationFlags & MIR_IGNORE_NULL_CHECK)) {
496 pcrLabel = genNullCheck(cUnit, rlArray.sRegLow, regArray,
497 mir->offset, NULL);
498 }
499
500 if (!(mir->OptimizationFlags & MIR_IGNORE_RANGE_CHECK)) {
501 /* Get len */
502 loadWordDisp(cUnit, regArray, lenOffset, regLen);
503 /* regPtr -> array data */
504 opRegRegImm(cUnit, kOpAdd, regPtr, regArray, dataOffset);
505 genBoundsCheck(cUnit, regIndex, regLen, mir->offset,
506 pcrLabel);
507 } else {
508 /* regPtr -> array data */
509 opRegRegImm(cUnit, kOpAdd, regPtr, regArray, dataOffset);
510 }
511
512 /* Get object to store */
513 loadValueDirectFixed(cUnit, rlSrc, r0);
Ben Chengbd1326d2010-04-02 15:04:53 -0700514 LOAD_FUNC_ADDR(cUnit, r2, (int)dvmCanPutArrayElement);
Bill Buzbeebe6534f2010-03-12 16:01:35 -0800515
516 /* Are we storing null? If so, avoid check */
517 opRegImm(cUnit, kOpCmp, r0, 0);
518 ArmLIR *branchOver = opCondBranch(cUnit, kArmCondEq);
519
520 /* Make sure the types are compatible */
521 loadWordDisp(cUnit, regArray, offsetof(Object, clazz), r1);
522 loadWordDisp(cUnit, r0, offsetof(Object, clazz), r0);
523 opReg(cUnit, kOpBlx, r2);
524 dvmCompilerClobberCallRegs(cUnit);
Bill Buzbee900a3af2010-03-16 12:41:43 -0700525
526 /*
527 * Using fixed registers here, and counting on r4 and r7 being
528 * preserved across the above call. Tell the register allocation
529 * utilities about the regs we are using directly
530 */
531 dvmCompilerLockTemp(cUnit, regPtr); // r4PC
532 dvmCompilerLockTemp(cUnit, regIndex); // r7
533 dvmCompilerLockTemp(cUnit, r0);
534
Bill Buzbeebe6534f2010-03-12 16:01:35 -0800535 /* Bad? - roll back and re-execute if so */
536 genRegImmCheck(cUnit, kArmCondEq, r0, 0, mir->offset, pcrLabel);
537
538 /* Resume here - must reload element, regPtr & index preserved */
539 loadValueDirectFixed(cUnit, rlSrc, r0);
540
541 ArmLIR *target = newLIR0(cUnit, kArmPseudoTargetLabel);
542 target->defMask = ENCODE_ALL;
543 branchOver->generic.target = (LIR *) target;
544
Ben Cheng11d8f142010-03-24 15:24:19 -0700545 HEAP_ACCESS_SHADOW(true);
Bill Buzbeebe6534f2010-03-12 16:01:35 -0800546 storeBaseIndexed(cUnit, regPtr, regIndex, r0,
547 scale, kWord);
Ben Cheng11d8f142010-03-24 15:24:19 -0700548 HEAP_ACCESS_SHADOW(false);
Bill Buzbeebe6534f2010-03-12 16:01:35 -0800549}
550
Ben Cheng5d90c202009-11-22 23:31:11 -0800551static bool genShiftOpLong(CompilationUnit *cUnit, MIR *mir,
552 RegLocation rlDest, RegLocation rlSrc1,
553 RegLocation rlShift)
Ben Chengba4fc8b2009-06-01 13:00:29 -0700554{
Ben Chenge9695e52009-06-16 16:11:47 -0700555 /*
556 * Don't mess with the regsiters here as there is a particular calling
557 * convention to the out-of-line handler.
558 */
Bill Buzbee1465db52009-09-23 17:17:35 -0700559 RegLocation rlResult;
560
561 loadValueDirectWideFixed(cUnit, rlSrc1, r0, r1);
562 loadValueDirect(cUnit, rlShift, r2);
Ben Chenge9695e52009-06-16 16:11:47 -0700563 switch( mir->dalvikInsn.opCode) {
564 case OP_SHL_LONG:
565 case OP_SHL_LONG_2ADDR:
566 genDispatchToHandler(cUnit, TEMPLATE_SHL_LONG);
567 break;
568 case OP_SHR_LONG:
569 case OP_SHR_LONG_2ADDR:
570 genDispatchToHandler(cUnit, TEMPLATE_SHR_LONG);
571 break;
572 case OP_USHR_LONG:
573 case OP_USHR_LONG_2ADDR:
574 genDispatchToHandler(cUnit, TEMPLATE_USHR_LONG);
575 break;
576 default:
577 return true;
578 }
Bill Buzbeec6f10662010-02-09 11:16:15 -0800579 rlResult = dvmCompilerGetReturnWide(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -0700580 storeValueWide(cUnit, rlDest, rlResult);
Ben Chenge9695e52009-06-16 16:11:47 -0700581 return false;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700582}
Ben Chenge9695e52009-06-16 16:11:47 -0700583
Ben Cheng5d90c202009-11-22 23:31:11 -0800584static bool genArithOpLong(CompilationUnit *cUnit, MIR *mir,
585 RegLocation rlDest, RegLocation rlSrc1,
586 RegLocation rlSrc2)
Ben Chengba4fc8b2009-06-01 13:00:29 -0700587{
Bill Buzbee1465db52009-09-23 17:17:35 -0700588 RegLocation rlResult;
589 OpKind firstOp = kOpBkpt;
590 OpKind secondOp = kOpBkpt;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700591 bool callOut = false;
592 void *callTgt;
593 int retReg = r0;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700594
595 switch (mir->dalvikInsn.opCode) {
596 case OP_NOT_LONG:
Bill Buzbee1465db52009-09-23 17:17:35 -0700597 rlSrc2 = loadValueWide(cUnit, rlSrc2, kCoreReg);
Bill Buzbeec6f10662010-02-09 11:16:15 -0800598 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Bill Buzbee1465db52009-09-23 17:17:35 -0700599 opRegReg(cUnit, kOpMvn, rlResult.lowReg, rlSrc2.lowReg);
600 opRegReg(cUnit, kOpMvn, rlResult.highReg, rlSrc2.highReg);
601 storeValueWide(cUnit, rlDest, rlResult);
602 return false;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700603 break;
604 case OP_ADD_LONG:
605 case OP_ADD_LONG_2ADDR:
Bill Buzbee1465db52009-09-23 17:17:35 -0700606 firstOp = kOpAdd;
607 secondOp = kOpAdc;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700608 break;
609 case OP_SUB_LONG:
610 case OP_SUB_LONG_2ADDR:
Bill Buzbee1465db52009-09-23 17:17:35 -0700611 firstOp = kOpSub;
612 secondOp = kOpSbc;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700613 break;
614 case OP_MUL_LONG:
615 case OP_MUL_LONG_2ADDR:
Bill Buzbee1465db52009-09-23 17:17:35 -0700616 genMulLong(cUnit, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700617 return false;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700618 case OP_DIV_LONG:
619 case OP_DIV_LONG_2ADDR:
620 callOut = true;
621 retReg = r0;
622 callTgt = (void*)__aeabi_ldivmod;
623 break;
624 /* NOTE - result is in r2/r3 instead of r0/r1 */
625 case OP_REM_LONG:
626 case OP_REM_LONG_2ADDR:
627 callOut = true;
628 callTgt = (void*)__aeabi_ldivmod;
629 retReg = r2;
630 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700631 case OP_AND_LONG_2ADDR:
Bill Buzbee1465db52009-09-23 17:17:35 -0700632 case OP_AND_LONG:
633 firstOp = kOpAnd;
634 secondOp = kOpAnd;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700635 break;
636 case OP_OR_LONG:
637 case OP_OR_LONG_2ADDR:
Bill Buzbee1465db52009-09-23 17:17:35 -0700638 firstOp = kOpOr;
639 secondOp = kOpOr;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700640 break;
641 case OP_XOR_LONG:
642 case OP_XOR_LONG_2ADDR:
Bill Buzbee1465db52009-09-23 17:17:35 -0700643 firstOp = kOpXor;
644 secondOp = kOpXor;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700645 break;
Ben Chenge9695e52009-06-16 16:11:47 -0700646 case OP_NEG_LONG: {
Bill Buzbee51ecf602010-01-14 14:27:52 -0800647 //TUNING: can improve this using Thumb2 code
Bill Buzbeec6f10662010-02-09 11:16:15 -0800648 int tReg = dvmCompilerAllocTemp(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -0700649 rlSrc2 = loadValueWide(cUnit, rlSrc2, kCoreReg);
Bill Buzbeec6f10662010-02-09 11:16:15 -0800650 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Ben Chengbd1326d2010-04-02 15:04:53 -0700651 loadConstantNoClobber(cUnit, tReg, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -0700652 opRegRegReg(cUnit, kOpSub, rlResult.lowReg,
Bill Buzbee51ecf602010-01-14 14:27:52 -0800653 tReg, rlSrc2.lowReg);
654 opRegReg(cUnit, kOpSbc, tReg, rlSrc2.highReg);
655 genRegCopy(cUnit, rlResult.highReg, tReg);
Bill Buzbee1465db52009-09-23 17:17:35 -0700656 storeValueWide(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700657 return false;
Ben Chenge9695e52009-06-16 16:11:47 -0700658 }
Ben Chengba4fc8b2009-06-01 13:00:29 -0700659 default:
660 LOGE("Invalid long arith op");
Bill Buzbeefc519dc2010-03-06 23:30:57 -0800661 dvmCompilerAbort(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700662 }
663 if (!callOut) {
Bill Buzbee80cef862010-03-25 10:38:34 -0700664 genLong3Addr(cUnit, mir, firstOp, secondOp, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700665 } else {
Bill Buzbee1465db52009-09-23 17:17:35 -0700666 // Adjust return regs in to handle case of rem returning r2/r3
Bill Buzbeec6f10662010-02-09 11:16:15 -0800667 dvmCompilerFlushAllRegs(cUnit); /* Send everything to home location */
Bill Buzbee1465db52009-09-23 17:17:35 -0700668 loadValueDirectWideFixed(cUnit, rlSrc1, r0, r1);
Ben Chengbd1326d2010-04-02 15:04:53 -0700669 LOAD_FUNC_ADDR(cUnit, rlr, (int) callTgt);
Bill Buzbee1465db52009-09-23 17:17:35 -0700670 loadValueDirectWideFixed(cUnit, rlSrc2, r2, r3);
671 opReg(cUnit, kOpBlx, rlr);
Elliott Hughes6a555132010-02-25 15:41:42 -0800672 dvmCompilerClobberCallRegs(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -0700673 if (retReg == r0)
Bill Buzbeec6f10662010-02-09 11:16:15 -0800674 rlResult = dvmCompilerGetReturnWide(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -0700675 else
Bill Buzbeec6f10662010-02-09 11:16:15 -0800676 rlResult = dvmCompilerGetReturnWideAlt(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -0700677 storeValueWide(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700678 }
679 return false;
680}
681
Ben Cheng5d90c202009-11-22 23:31:11 -0800682static bool genArithOpInt(CompilationUnit *cUnit, MIR *mir,
683 RegLocation rlDest, RegLocation rlSrc1,
684 RegLocation rlSrc2)
Ben Chengba4fc8b2009-06-01 13:00:29 -0700685{
Bill Buzbee1465db52009-09-23 17:17:35 -0700686 OpKind op = kOpBkpt;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700687 bool callOut = false;
688 bool checkZero = false;
Bill Buzbee1465db52009-09-23 17:17:35 -0700689 bool unary = false;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700690 int retReg = r0;
691 void *callTgt;
Bill Buzbee1465db52009-09-23 17:17:35 -0700692 RegLocation rlResult;
Bill Buzbee0e605272009-12-01 14:28:05 -0800693 bool shiftOp = false;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700694
Ben Chengba4fc8b2009-06-01 13:00:29 -0700695 switch (mir->dalvikInsn.opCode) {
696 case OP_NEG_INT:
Bill Buzbee1465db52009-09-23 17:17:35 -0700697 op = kOpNeg;
698 unary = true;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700699 break;
700 case OP_NOT_INT:
Bill Buzbee1465db52009-09-23 17:17:35 -0700701 op = kOpMvn;
702 unary = true;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700703 break;
704 case OP_ADD_INT:
705 case OP_ADD_INT_2ADDR:
Bill Buzbee1465db52009-09-23 17:17:35 -0700706 op = kOpAdd;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700707 break;
708 case OP_SUB_INT:
709 case OP_SUB_INT_2ADDR:
Bill Buzbee1465db52009-09-23 17:17:35 -0700710 op = kOpSub;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700711 break;
712 case OP_MUL_INT:
713 case OP_MUL_INT_2ADDR:
Bill Buzbee1465db52009-09-23 17:17:35 -0700714 op = kOpMul;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700715 break;
716 case OP_DIV_INT:
717 case OP_DIV_INT_2ADDR:
718 callOut = true;
719 checkZero = true;
720 callTgt = __aeabi_idiv;
721 retReg = r0;
722 break;
723 /* NOTE: returns in r1 */
724 case OP_REM_INT:
725 case OP_REM_INT_2ADDR:
726 callOut = true;
727 checkZero = true;
728 callTgt = __aeabi_idivmod;
729 retReg = r1;
730 break;
731 case OP_AND_INT:
732 case OP_AND_INT_2ADDR:
Bill Buzbee1465db52009-09-23 17:17:35 -0700733 op = kOpAnd;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700734 break;
735 case OP_OR_INT:
736 case OP_OR_INT_2ADDR:
Bill Buzbee1465db52009-09-23 17:17:35 -0700737 op = kOpOr;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700738 break;
739 case OP_XOR_INT:
740 case OP_XOR_INT_2ADDR:
Bill Buzbee1465db52009-09-23 17:17:35 -0700741 op = kOpXor;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700742 break;
743 case OP_SHL_INT:
744 case OP_SHL_INT_2ADDR:
Bill Buzbee0e605272009-12-01 14:28:05 -0800745 shiftOp = true;
Bill Buzbee1465db52009-09-23 17:17:35 -0700746 op = kOpLsl;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700747 break;
748 case OP_SHR_INT:
749 case OP_SHR_INT_2ADDR:
Bill Buzbee0e605272009-12-01 14:28:05 -0800750 shiftOp = true;
Bill Buzbee1465db52009-09-23 17:17:35 -0700751 op = kOpAsr;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700752 break;
753 case OP_USHR_INT:
754 case OP_USHR_INT_2ADDR:
Bill Buzbee0e605272009-12-01 14:28:05 -0800755 shiftOp = true;
Bill Buzbee1465db52009-09-23 17:17:35 -0700756 op = kOpLsr;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700757 break;
758 default:
759 LOGE("Invalid word arith op: 0x%x(%d)",
760 mir->dalvikInsn.opCode, mir->dalvikInsn.opCode);
Bill Buzbeefc519dc2010-03-06 23:30:57 -0800761 dvmCompilerAbort(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700762 }
763 if (!callOut) {
Bill Buzbee1465db52009-09-23 17:17:35 -0700764 rlSrc1 = loadValue(cUnit, rlSrc1, kCoreReg);
765 if (unary) {
Bill Buzbeec6f10662010-02-09 11:16:15 -0800766 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Bill Buzbee1465db52009-09-23 17:17:35 -0700767 opRegReg(cUnit, op, rlResult.lowReg,
768 rlSrc1.lowReg);
Ben Chenge9695e52009-06-16 16:11:47 -0700769 } else {
Bill Buzbee1465db52009-09-23 17:17:35 -0700770 rlSrc2 = loadValue(cUnit, rlSrc2, kCoreReg);
Bill Buzbee0e605272009-12-01 14:28:05 -0800771 if (shiftOp) {
Bill Buzbeec6f10662010-02-09 11:16:15 -0800772 int tReg = dvmCompilerAllocTemp(cUnit);
Bill Buzbee0e605272009-12-01 14:28:05 -0800773 opRegRegImm(cUnit, kOpAnd, tReg, rlSrc2.lowReg, 31);
Bill Buzbeec6f10662010-02-09 11:16:15 -0800774 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Bill Buzbee0e605272009-12-01 14:28:05 -0800775 opRegRegReg(cUnit, op, rlResult.lowReg,
776 rlSrc1.lowReg, tReg);
Bill Buzbeec6f10662010-02-09 11:16:15 -0800777 dvmCompilerFreeTemp(cUnit, tReg);
Bill Buzbee0e605272009-12-01 14:28:05 -0800778 } else {
Bill Buzbeec6f10662010-02-09 11:16:15 -0800779 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Bill Buzbee0e605272009-12-01 14:28:05 -0800780 opRegRegReg(cUnit, op, rlResult.lowReg,
781 rlSrc1.lowReg, rlSrc2.lowReg);
782 }
Ben Chenge9695e52009-06-16 16:11:47 -0700783 }
Bill Buzbee1465db52009-09-23 17:17:35 -0700784 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700785 } else {
Bill Buzbee1465db52009-09-23 17:17:35 -0700786 RegLocation rlResult;
Bill Buzbeec6f10662010-02-09 11:16:15 -0800787 dvmCompilerFlushAllRegs(cUnit); /* Send everything to home location */
Bill Buzbee1465db52009-09-23 17:17:35 -0700788 loadValueDirectFixed(cUnit, rlSrc2, r1);
Ben Chengbd1326d2010-04-02 15:04:53 -0700789 LOAD_FUNC_ADDR(cUnit, r2, (int) callTgt);
Bill Buzbee1465db52009-09-23 17:17:35 -0700790 loadValueDirectFixed(cUnit, rlSrc1, r0);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700791 if (checkZero) {
Bill Buzbee1465db52009-09-23 17:17:35 -0700792 genNullCheck(cUnit, rlSrc2.sRegLow, r1, mir->offset, NULL);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700793 }
Bill Buzbee1465db52009-09-23 17:17:35 -0700794 opReg(cUnit, kOpBlx, r2);
Elliott Hughes6a555132010-02-25 15:41:42 -0800795 dvmCompilerClobberCallRegs(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -0700796 if (retReg == r0)
Bill Buzbeec6f10662010-02-09 11:16:15 -0800797 rlResult = dvmCompilerGetReturn(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -0700798 else
Bill Buzbeec6f10662010-02-09 11:16:15 -0800799 rlResult = dvmCompilerGetReturnAlt(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -0700800 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700801 }
802 return false;
803}
804
Ben Cheng5d90c202009-11-22 23:31:11 -0800805static bool genArithOp(CompilationUnit *cUnit, MIR *mir)
Ben Chengba4fc8b2009-06-01 13:00:29 -0700806{
807 OpCode opCode = mir->dalvikInsn.opCode;
Bill Buzbee1465db52009-09-23 17:17:35 -0700808 RegLocation rlDest;
809 RegLocation rlSrc1;
810 RegLocation rlSrc2;
811 /* Deduce sizes of operands */
812 if (mir->ssaRep->numUses == 2) {
Bill Buzbeec6f10662010-02-09 11:16:15 -0800813 rlSrc1 = dvmCompilerGetSrc(cUnit, mir, 0);
814 rlSrc2 = dvmCompilerGetSrc(cUnit, mir, 1);
Bill Buzbee1465db52009-09-23 17:17:35 -0700815 } else if (mir->ssaRep->numUses == 3) {
Bill Buzbeec6f10662010-02-09 11:16:15 -0800816 rlSrc1 = dvmCompilerGetSrcWide(cUnit, mir, 0, 1);
817 rlSrc2 = dvmCompilerGetSrc(cUnit, mir, 2);
Bill Buzbee1465db52009-09-23 17:17:35 -0700818 } else {
Bill Buzbeec6f10662010-02-09 11:16:15 -0800819 rlSrc1 = dvmCompilerGetSrcWide(cUnit, mir, 0, 1);
820 rlSrc2 = dvmCompilerGetSrcWide(cUnit, mir, 2, 3);
Bill Buzbee1465db52009-09-23 17:17:35 -0700821 assert(mir->ssaRep->numUses == 4);
822 }
823 if (mir->ssaRep->numDefs == 1) {
Bill Buzbeec6f10662010-02-09 11:16:15 -0800824 rlDest = dvmCompilerGetDest(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -0700825 } else {
826 assert(mir->ssaRep->numDefs == 2);
Bill Buzbeec6f10662010-02-09 11:16:15 -0800827 rlDest = dvmCompilerGetDestWide(cUnit, mir, 0, 1);
Bill Buzbee1465db52009-09-23 17:17:35 -0700828 }
Ben Chengba4fc8b2009-06-01 13:00:29 -0700829
830 if ((opCode >= OP_ADD_LONG_2ADDR) && (opCode <= OP_XOR_LONG_2ADDR)) {
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_ADD_LONG) && (opCode <= OP_XOR_LONG)) {
Ben Cheng5d90c202009-11-22 23:31:11 -0800834 return genArithOpLong(cUnit,mir, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700835 }
836 if ((opCode >= OP_SHL_LONG_2ADDR) && (opCode <= OP_USHR_LONG_2ADDR)) {
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_SHL_LONG) && (opCode <= OP_USHR_LONG)) {
Ben Cheng5d90c202009-11-22 23:31:11 -0800840 return genShiftOpLong(cUnit,mir, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700841 }
842 if ((opCode >= OP_ADD_INT_2ADDR) && (opCode <= OP_USHR_INT_2ADDR)) {
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_INT) && (opCode <= OP_USHR_INT)) {
Ben Cheng5d90c202009-11-22 23:31:11 -0800846 return genArithOpInt(cUnit,mir, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700847 }
848 if ((opCode >= OP_ADD_FLOAT_2ADDR) && (opCode <= OP_REM_FLOAT_2ADDR)) {
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_FLOAT) && (opCode <= OP_REM_FLOAT)) {
Ben Cheng5d90c202009-11-22 23:31:11 -0800852 return genArithOpFloat(cUnit, mir, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700853 }
854 if ((opCode >= OP_ADD_DOUBLE_2ADDR) && (opCode <= OP_REM_DOUBLE_2ADDR)) {
Ben Cheng5d90c202009-11-22 23:31:11 -0800855 return genArithOpDouble(cUnit,mir, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700856 }
857 if ((opCode >= OP_ADD_DOUBLE) && (opCode <= OP_REM_DOUBLE)) {
Ben Cheng5d90c202009-11-22 23:31:11 -0800858 return genArithOpDouble(cUnit,mir, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700859 }
860 return true;
861}
862
Bill Buzbee1465db52009-09-23 17:17:35 -0700863/* Generate unconditional branch instructions */
864static ArmLIR *genUnconditionalBranch(CompilationUnit *cUnit, ArmLIR *target)
865{
866 ArmLIR *branch = opNone(cUnit, kOpUncondBr);
867 branch->generic.target = (LIR *) target;
868 return branch;
869}
870
Bill Buzbee1465db52009-09-23 17:17:35 -0700871/* Perform the actual operation for OP_RETURN_* */
872static void genReturnCommon(CompilationUnit *cUnit, MIR *mir)
873{
874 genDispatchToHandler(cUnit, TEMPLATE_RETURN);
Ben Cheng86717f72010-03-05 15:27:21 -0800875#if defined(JIT_STATS)
Bill Buzbee1465db52009-09-23 17:17:35 -0700876 gDvmJit.returnOp++;
877#endif
878 int dPC = (int) (cUnit->method->insns + mir->offset);
879 /* Insert branch, but defer setting of target */
880 ArmLIR *branch = genUnconditionalBranch(cUnit, NULL);
881 /* Set up the place holder to reconstruct this Dalvik PC */
882 ArmLIR *pcrLabel = dvmCompilerNew(sizeof(ArmLIR), true);
Ben Chenga4973592010-03-31 11:59:18 -0700883 pcrLabel->opCode = kArmPseudoPCReconstructionCell;
Bill Buzbee1465db52009-09-23 17:17:35 -0700884 pcrLabel->operands[0] = dPC;
885 pcrLabel->operands[1] = mir->offset;
886 /* Insert the place holder to the growable list */
887 dvmInsertGrowableList(&cUnit->pcReconstructionList, pcrLabel);
888 /* Branch to the PC reconstruction code */
889 branch->generic.target = (LIR *) pcrLabel;
890}
891
Ben Chengba4fc8b2009-06-01 13:00:29 -0700892static void genProcessArgsNoRange(CompilationUnit *cUnit, MIR *mir,
893 DecodedInstruction *dInsn,
Bill Buzbee89efc3d2009-07-28 11:22:22 -0700894 ArmLIR **pcrLabel)
Ben Chengba4fc8b2009-06-01 13:00:29 -0700895{
896 unsigned int i;
897 unsigned int regMask = 0;
Bill Buzbee1465db52009-09-23 17:17:35 -0700898 RegLocation rlArg;
899 int numDone = 0;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700900
Bill Buzbee1465db52009-09-23 17:17:35 -0700901 /*
902 * Load arguments to r0..r4. Note that these registers may contain
903 * live values, so we clobber them immediately after loading to prevent
904 * them from being used as sources for subsequent loads.
905 */
Bill Buzbeec6f10662010-02-09 11:16:15 -0800906 dvmCompilerLockAllTemps(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700907 for (i = 0; i < dInsn->vA; i++) {
908 regMask |= 1 << i;
Bill Buzbeec6f10662010-02-09 11:16:15 -0800909 rlArg = dvmCompilerGetSrc(cUnit, mir, numDone++);
Bill Buzbee1465db52009-09-23 17:17:35 -0700910 loadValueDirectFixed(cUnit, rlArg, i);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700911 }
912 if (regMask) {
913 /* Up to 5 args are pushed on top of FP - sizeofStackSaveArea */
Bill Buzbee1465db52009-09-23 17:17:35 -0700914 opRegRegImm(cUnit, kOpSub, r7, rFP,
915 sizeof(StackSaveArea) + (dInsn->vA << 2));
Ben Chengba4fc8b2009-06-01 13:00:29 -0700916 /* generate null check */
917 if (pcrLabel) {
Bill Buzbeec6f10662010-02-09 11:16:15 -0800918 *pcrLabel = genNullCheck(cUnit, dvmCompilerSSASrc(mir, 0), r0,
Bill Buzbee1465db52009-09-23 17:17:35 -0700919 mir->offset, NULL);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700920 }
Bill Buzbee270c1d62009-08-13 16:58:07 -0700921 storeMultiple(cUnit, r7, regMask);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700922 }
923}
924
925static void genProcessArgsRange(CompilationUnit *cUnit, MIR *mir,
926 DecodedInstruction *dInsn,
Bill Buzbee89efc3d2009-07-28 11:22:22 -0700927 ArmLIR **pcrLabel)
Ben Chengba4fc8b2009-06-01 13:00:29 -0700928{
929 int srcOffset = dInsn->vC << 2;
930 int numArgs = dInsn->vA;
931 int regMask;
Bill Buzbee1465db52009-09-23 17:17:35 -0700932
933 /*
934 * Note: here, all promoted registers will have been flushed
935 * back to the Dalvik base locations, so register usage restrictins
936 * are lifted. All parms loaded from original Dalvik register
937 * region - even though some might conceivably have valid copies
938 * cached in a preserved register.
939 */
Bill Buzbeec6f10662010-02-09 11:16:15 -0800940 dvmCompilerLockAllTemps(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -0700941
Ben Chengba4fc8b2009-06-01 13:00:29 -0700942 /*
943 * r4PC : &rFP[vC]
944 * r7: &newFP[0]
945 */
Bill Buzbee1465db52009-09-23 17:17:35 -0700946 opRegRegImm(cUnit, kOpAdd, r4PC, rFP, srcOffset);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700947 /* load [r0 .. min(numArgs,4)] */
948 regMask = (1 << ((numArgs < 4) ? numArgs : 4)) - 1;
Ben Chengd7d426a2009-09-22 11:23:36 -0700949 /*
950 * Protect the loadMultiple instruction from being reordered with other
951 * Dalvik stack accesses.
952 */
Bill Buzbee270c1d62009-08-13 16:58:07 -0700953 loadMultiple(cUnit, r4PC, regMask);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700954
Bill Buzbee1465db52009-09-23 17:17:35 -0700955 opRegRegImm(cUnit, kOpSub, r7, rFP,
956 sizeof(StackSaveArea) + (numArgs << 2));
Ben Chengba4fc8b2009-06-01 13:00:29 -0700957 /* generate null check */
958 if (pcrLabel) {
Bill Buzbeec6f10662010-02-09 11:16:15 -0800959 *pcrLabel = genNullCheck(cUnit, dvmCompilerSSASrc(mir, 0), r0,
Bill Buzbee1465db52009-09-23 17:17:35 -0700960 mir->offset, NULL);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700961 }
962
963 /*
964 * Handle remaining 4n arguments:
965 * store previously loaded 4 values and load the next 4 values
966 */
967 if (numArgs >= 8) {
Bill Buzbee89efc3d2009-07-28 11:22:22 -0700968 ArmLIR *loopLabel = NULL;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700969 /*
970 * r0 contains "this" and it will be used later, so push it to the stack
Bill Buzbee270c1d62009-08-13 16:58:07 -0700971 * first. Pushing r5 (rFP) is just for stack alignment purposes.
Ben Chengba4fc8b2009-06-01 13:00:29 -0700972 */
Bill Buzbee1465db52009-09-23 17:17:35 -0700973 opImm(cUnit, kOpPush, (1 << r0 | 1 << rFP));
Ben Chengba4fc8b2009-06-01 13:00:29 -0700974 /* No need to generate the loop structure if numArgs <= 11 */
975 if (numArgs > 11) {
976 loadConstant(cUnit, 5, ((numArgs - 4) >> 2) << 2);
Bill Buzbee1465db52009-09-23 17:17:35 -0700977 loopLabel = newLIR0(cUnit, kArmPseudoTargetLabel);
Ben Chengd7d426a2009-09-22 11:23:36 -0700978 loopLabel->defMask = ENCODE_ALL;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700979 }
Bill Buzbee270c1d62009-08-13 16:58:07 -0700980 storeMultiple(cUnit, r7, regMask);
Ben Chengd7d426a2009-09-22 11:23:36 -0700981 /*
982 * Protect the loadMultiple instruction from being reordered with other
983 * Dalvik stack accesses.
984 */
Bill Buzbee270c1d62009-08-13 16:58:07 -0700985 loadMultiple(cUnit, r4PC, regMask);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700986 /* No need to generate the loop structure if numArgs <= 11 */
987 if (numArgs > 11) {
Bill Buzbee1465db52009-09-23 17:17:35 -0700988 opRegImm(cUnit, kOpSub, rFP, 4);
989 genConditionalBranch(cUnit, kArmCondNe, loopLabel);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700990 }
991 }
992
993 /* Save the last batch of loaded values */
Bill Buzbee270c1d62009-08-13 16:58:07 -0700994 storeMultiple(cUnit, r7, regMask);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700995
996 /* Generate the loop epilogue - don't use r0 */
997 if ((numArgs > 4) && (numArgs % 4)) {
998 regMask = ((1 << (numArgs & 0x3)) - 1) << 1;
Ben Chengd7d426a2009-09-22 11:23:36 -0700999 /*
1000 * Protect the loadMultiple instruction from being reordered with other
1001 * Dalvik stack accesses.
1002 */
Bill Buzbee270c1d62009-08-13 16:58:07 -07001003 loadMultiple(cUnit, r4PC, regMask);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001004 }
1005 if (numArgs >= 8)
Bill Buzbee1465db52009-09-23 17:17:35 -07001006 opImm(cUnit, kOpPop, (1 << r0 | 1 << rFP));
Ben Chengba4fc8b2009-06-01 13:00:29 -07001007
1008 /* Save the modulo 4 arguments */
1009 if ((numArgs > 4) && (numArgs % 4)) {
Bill Buzbee270c1d62009-08-13 16:58:07 -07001010 storeMultiple(cUnit, r7, regMask);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001011 }
1012}
1013
Ben Cheng38329f52009-07-07 14:19:20 -07001014/*
1015 * Generate code to setup the call stack then jump to the chaining cell if it
1016 * is not a native method.
1017 */
1018static void genInvokeSingletonCommon(CompilationUnit *cUnit, MIR *mir,
Bill Buzbee89efc3d2009-07-28 11:22:22 -07001019 BasicBlock *bb, ArmLIR *labelList,
1020 ArmLIR *pcrLabel,
Ben Cheng38329f52009-07-07 14:19:20 -07001021 const Method *calleeMethod)
Ben Chengba4fc8b2009-06-01 13:00:29 -07001022{
Bill Buzbee1465db52009-09-23 17:17:35 -07001023 /*
1024 * Note: all Dalvik register state should be flushed to
1025 * memory by the point, so register usage restrictions no
1026 * longer apply. All temp & preserved registers may be used.
1027 */
Bill Buzbeec6f10662010-02-09 11:16:15 -08001028 dvmCompilerLockAllTemps(cUnit);
Bill Buzbee89efc3d2009-07-28 11:22:22 -07001029 ArmLIR *retChainingCell = &labelList[bb->fallThrough->id];
Ben Chengba4fc8b2009-06-01 13:00:29 -07001030
1031 /* r1 = &retChainingCell */
Bill Buzbeec6f10662010-02-09 11:16:15 -08001032 dvmCompilerLockTemp(cUnit, r1);
Bill Buzbee1465db52009-09-23 17:17:35 -07001033 ArmLIR *addrRetChain = opRegRegImm(cUnit, kOpAdd, r1, rpc, 0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001034 /* r4PC = dalvikCallsite */
1035 loadConstant(cUnit, r4PC,
1036 (int) (cUnit->method->insns + mir->offset));
1037 addrRetChain->generic.target = (LIR *) retChainingCell;
1038 /*
Ben Cheng38329f52009-07-07 14:19:20 -07001039 * r0 = calleeMethod (loaded upon calling genInvokeSingletonCommon)
Ben Chengba4fc8b2009-06-01 13:00:29 -07001040 * r1 = &ChainingCell
1041 * r4PC = callsiteDPC
1042 */
1043 if (dvmIsNativeMethod(calleeMethod)) {
Ben Cheng38329f52009-07-07 14:19:20 -07001044 genDispatchToHandler(cUnit, TEMPLATE_INVOKE_METHOD_NATIVE);
Ben Cheng86717f72010-03-05 15:27:21 -08001045#if defined(JIT_STATS)
Ben Cheng38329f52009-07-07 14:19:20 -07001046 gDvmJit.invokeNative++;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001047#endif
1048 } else {
1049 genDispatchToHandler(cUnit, TEMPLATE_INVOKE_METHOD_CHAIN);
Ben Cheng86717f72010-03-05 15:27:21 -08001050#if defined(JIT_STATS)
1051 gDvmJit.invokeMonomorphic++;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001052#endif
Ben Cheng38329f52009-07-07 14:19:20 -07001053 /* Branch to the chaining cell */
Ben Chengba4fc8b2009-06-01 13:00:29 -07001054 genUnconditionalBranch(cUnit, &labelList[bb->taken->id]);
1055 }
1056 /* Handle exceptions using the interpreter */
1057 genTrap(cUnit, mir->offset, pcrLabel);
1058}
1059
Ben Cheng38329f52009-07-07 14:19:20 -07001060/*
1061 * Generate code to check the validity of a predicted chain and take actions
1062 * based on the result.
1063 *
1064 * 0x426a99aa : ldr r4, [pc, #72] --> r4 <- dalvikPC of this invoke
1065 * 0x426a99ac : add r1, pc, #32 --> r1 <- &retChainingCell
1066 * 0x426a99ae : add r2, pc, #40 --> r2 <- &predictedChainingCell
1067 * 0x426a99b0 : blx_1 0x426a918c --+ TEMPLATE_INVOKE_METHOD_PREDICTED_CHAIN
1068 * 0x426a99b2 : blx_2 see above --+
1069 * 0x426a99b4 : b 0x426a99d8 --> off to the predicted chain
1070 * 0x426a99b6 : b 0x426a99c8 --> punt to the interpreter
1071 * 0x426a99b8 : ldr r0, [r7, #44] --> r0 <- this->class->vtable[methodIdx]
1072 * 0x426a99ba : cmp r1, #0 --> compare r1 (rechain count) against 0
1073 * 0x426a99bc : bgt 0x426a99c2 --> >=0? don't rechain
1074 * 0x426a99be : ldr r7, [r6, #96] --+ dvmJitToPatchPredictedChain
1075 * 0x426a99c0 : blx r7 --+
1076 * 0x426a99c2 : add r1, pc, #12 --> r1 <- &retChainingCell
1077 * 0x426a99c4 : blx_1 0x426a9098 --+ TEMPLATE_INVOKE_METHOD_NO_OPT
1078 * 0x426a99c6 : blx_2 see above --+
1079 */
1080static void genInvokeVirtualCommon(CompilationUnit *cUnit, MIR *mir,
1081 int methodIndex,
Bill Buzbee89efc3d2009-07-28 11:22:22 -07001082 ArmLIR *retChainingCell,
1083 ArmLIR *predChainingCell,
1084 ArmLIR *pcrLabel)
Ben Cheng38329f52009-07-07 14:19:20 -07001085{
Bill Buzbee1465db52009-09-23 17:17:35 -07001086 /*
1087 * Note: all Dalvik register state should be flushed to
1088 * memory by the point, so register usage restrictions no
1089 * longer apply. Lock temps to prevent them from being
1090 * allocated by utility routines.
1091 */
Bill Buzbeec6f10662010-02-09 11:16:15 -08001092 dvmCompilerLockAllTemps(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07001093
Ben Cheng38329f52009-07-07 14:19:20 -07001094 /* "this" is already left in r0 by genProcessArgs* */
1095
1096 /* r4PC = dalvikCallsite */
1097 loadConstant(cUnit, r4PC,
1098 (int) (cUnit->method->insns + mir->offset));
1099
1100 /* r1 = &retChainingCell */
Bill Buzbee1465db52009-09-23 17:17:35 -07001101 ArmLIR *addrRetChain = opRegRegImm(cUnit, kOpAdd, r1, rpc, 0);
Ben Cheng38329f52009-07-07 14:19:20 -07001102 addrRetChain->generic.target = (LIR *) retChainingCell;
1103
1104 /* r2 = &predictedChainingCell */
Bill Buzbee1465db52009-09-23 17:17:35 -07001105 ArmLIR *predictedChainingCell = opRegRegImm(cUnit, kOpAdd, r2, rpc, 0);
Ben Cheng38329f52009-07-07 14:19:20 -07001106 predictedChainingCell->generic.target = (LIR *) predChainingCell;
1107
1108 genDispatchToHandler(cUnit, TEMPLATE_INVOKE_METHOD_PREDICTED_CHAIN);
1109
1110 /* return through lr - jump to the chaining cell */
1111 genUnconditionalBranch(cUnit, predChainingCell);
1112
1113 /*
1114 * null-check on "this" may have been eliminated, but we still need a PC-
1115 * reconstruction label for stack overflow bailout.
1116 */
1117 if (pcrLabel == NULL) {
1118 int dPC = (int) (cUnit->method->insns + mir->offset);
Bill Buzbee89efc3d2009-07-28 11:22:22 -07001119 pcrLabel = dvmCompilerNew(sizeof(ArmLIR), true);
Ben Chenga4973592010-03-31 11:59:18 -07001120 pcrLabel->opCode = kArmPseudoPCReconstructionCell;
Ben Cheng38329f52009-07-07 14:19:20 -07001121 pcrLabel->operands[0] = dPC;
1122 pcrLabel->operands[1] = mir->offset;
1123 /* Insert the place holder to the growable list */
1124 dvmInsertGrowableList(&cUnit->pcReconstructionList, pcrLabel);
1125 }
1126
1127 /* return through lr+2 - punt to the interpreter */
1128 genUnconditionalBranch(cUnit, pcrLabel);
1129
1130 /*
1131 * return through lr+4 - fully resolve the callee method.
1132 * r1 <- count
1133 * r2 <- &predictedChainCell
1134 * r3 <- this->class
1135 * r4 <- dPC
1136 * r7 <- this->class->vtable
1137 */
1138
1139 /* r0 <- calleeMethod */
Bill Buzbee270c1d62009-08-13 16:58:07 -07001140 loadWordDisp(cUnit, r7, methodIndex * 4, r0);
Ben Cheng38329f52009-07-07 14:19:20 -07001141
1142 /* Check if rechain limit is reached */
Bill Buzbee1465db52009-09-23 17:17:35 -07001143 opRegImm(cUnit, kOpCmp, r1, 0);
Ben Cheng38329f52009-07-07 14:19:20 -07001144
Bill Buzbee1465db52009-09-23 17:17:35 -07001145 ArmLIR *bypassRechaining = opCondBranch(cUnit, kArmCondGt);
Ben Cheng38329f52009-07-07 14:19:20 -07001146
Bill Buzbee270c1d62009-08-13 16:58:07 -07001147 loadWordDisp(cUnit, rGLUE, offsetof(InterpState,
1148 jitToInterpEntries.dvmJitToPatchPredictedChain), r7);
Ben Cheng38329f52009-07-07 14:19:20 -07001149
1150 /*
1151 * r0 = calleeMethod
1152 * r2 = &predictedChainingCell
1153 * r3 = class
1154 *
1155 * &returnChainingCell has been loaded into r1 but is not needed
1156 * when patching the chaining cell and will be clobbered upon
1157 * returning so it will be reconstructed again.
1158 */
Bill Buzbee1465db52009-09-23 17:17:35 -07001159 opReg(cUnit, kOpBlx, r7);
Ben Cheng38329f52009-07-07 14:19:20 -07001160
1161 /* r1 = &retChainingCell */
Bill Buzbee1465db52009-09-23 17:17:35 -07001162 addrRetChain = opRegRegImm(cUnit, kOpAdd, r1, rpc, 0);
Ben Cheng38329f52009-07-07 14:19:20 -07001163 addrRetChain->generic.target = (LIR *) retChainingCell;
1164
1165 bypassRechaining->generic.target = (LIR *) addrRetChain;
1166 /*
1167 * r0 = calleeMethod,
1168 * r1 = &ChainingCell,
1169 * r4PC = callsiteDPC,
1170 */
1171 genDispatchToHandler(cUnit, TEMPLATE_INVOKE_METHOD_NO_OPT);
Ben Cheng86717f72010-03-05 15:27:21 -08001172#if defined(JIT_STATS)
1173 gDvmJit.invokePolymorphic++;
Ben Cheng38329f52009-07-07 14:19:20 -07001174#endif
1175 /* Handle exceptions using the interpreter */
1176 genTrap(cUnit, mir->offset, pcrLabel);
1177}
1178
1179/*
1180 * Up calling this function, "this" is stored in r0. The actual class will be
1181 * chased down off r0 and the predicted one will be retrieved through
1182 * predictedChainingCell then a comparison is performed to see whether the
1183 * previously established chaining is still valid.
1184 *
1185 * The return LIR is a branch based on the comparison result. The actual branch
1186 * target will be setup in the caller.
1187 */
Bill Buzbee89efc3d2009-07-28 11:22:22 -07001188static ArmLIR *genCheckPredictedChain(CompilationUnit *cUnit,
1189 ArmLIR *predChainingCell,
1190 ArmLIR *retChainingCell,
Ben Cheng38329f52009-07-07 14:19:20 -07001191 MIR *mir)
1192{
Bill Buzbee1465db52009-09-23 17:17:35 -07001193 /*
1194 * Note: all Dalvik register state should be flushed to
1195 * memory by the point, so register usage restrictions no
1196 * longer apply. All temp & preserved registers may be used.
1197 */
Bill Buzbeec6f10662010-02-09 11:16:15 -08001198 dvmCompilerLockAllTemps(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07001199
Ben Cheng38329f52009-07-07 14:19:20 -07001200 /* r3 now contains this->clazz */
Bill Buzbee270c1d62009-08-13 16:58:07 -07001201 loadWordDisp(cUnit, r0, offsetof(Object, clazz), r3);
Ben Cheng38329f52009-07-07 14:19:20 -07001202
1203 /*
1204 * r2 now contains predicted class. The starting offset of the
1205 * cached value is 4 bytes into the chaining cell.
1206 */
Bill Buzbee89efc3d2009-07-28 11:22:22 -07001207 ArmLIR *getPredictedClass =
Bill Buzbee270c1d62009-08-13 16:58:07 -07001208 loadWordDisp(cUnit, rpc, offsetof(PredictedChainingCell, clazz), r2);
Ben Cheng38329f52009-07-07 14:19:20 -07001209 getPredictedClass->generic.target = (LIR *) predChainingCell;
1210
1211 /*
1212 * r0 now contains predicted method. The starting offset of the
1213 * cached value is 8 bytes into the chaining cell.
1214 */
Bill Buzbee89efc3d2009-07-28 11:22:22 -07001215 ArmLIR *getPredictedMethod =
Bill Buzbee270c1d62009-08-13 16:58:07 -07001216 loadWordDisp(cUnit, rpc, offsetof(PredictedChainingCell, method), r0);
Ben Cheng38329f52009-07-07 14:19:20 -07001217 getPredictedMethod->generic.target = (LIR *) predChainingCell;
1218
1219 /* Load the stats counter to see if it is time to unchain and refresh */
Bill Buzbee89efc3d2009-07-28 11:22:22 -07001220 ArmLIR *getRechainingRequestCount =
Bill Buzbee270c1d62009-08-13 16:58:07 -07001221 loadWordDisp(cUnit, rpc, offsetof(PredictedChainingCell, counter), r7);
Ben Cheng38329f52009-07-07 14:19:20 -07001222 getRechainingRequestCount->generic.target =
1223 (LIR *) predChainingCell;
1224
1225 /* r4PC = dalvikCallsite */
1226 loadConstant(cUnit, r4PC,
1227 (int) (cUnit->method->insns + mir->offset));
1228
1229 /* r1 = &retChainingCell */
Bill Buzbee1465db52009-09-23 17:17:35 -07001230 ArmLIR *addrRetChain = opRegRegImm(cUnit, kOpAdd, r1, rpc, 0);
Ben Cheng38329f52009-07-07 14:19:20 -07001231 addrRetChain->generic.target = (LIR *) retChainingCell;
1232
1233 /* Check if r2 (predicted class) == r3 (actual class) */
Bill Buzbee1465db52009-09-23 17:17:35 -07001234 opRegReg(cUnit, kOpCmp, r2, r3);
Ben Cheng38329f52009-07-07 14:19:20 -07001235
Bill Buzbee1465db52009-09-23 17:17:35 -07001236 return opCondBranch(cUnit, kArmCondEq);
Ben Cheng38329f52009-07-07 14:19:20 -07001237}
1238
Ben Chengba4fc8b2009-06-01 13:00:29 -07001239/* Geneate a branch to go back to the interpreter */
1240static void genPuntToInterp(CompilationUnit *cUnit, unsigned int offset)
1241{
1242 /* r0 = dalvik pc */
Bill Buzbeec6f10662010-02-09 11:16:15 -08001243 dvmCompilerFlushAllRegs(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001244 loadConstant(cUnit, r0, (int) (cUnit->method->insns + offset));
Bill Buzbee270c1d62009-08-13 16:58:07 -07001245 loadWordDisp(cUnit, r0, offsetof(Object, clazz), r3);
1246 loadWordDisp(cUnit, rGLUE, offsetof(InterpState,
1247 jitToInterpEntries.dvmJitToInterpPunt), r1);
Bill Buzbee1465db52009-09-23 17:17:35 -07001248 opReg(cUnit, kOpBlx, r1);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001249}
1250
1251/*
1252 * Attempt to single step one instruction using the interpreter and return
1253 * to the compiled code for the next Dalvik instruction
1254 */
1255static void genInterpSingleStep(CompilationUnit *cUnit, MIR *mir)
1256{
1257 int flags = dexGetInstrFlags(gDvm.instrFlags, mir->dalvikInsn.opCode);
1258 int flagsToCheck = kInstrCanBranch | kInstrCanSwitch | kInstrCanReturn |
1259 kInstrCanThrow;
Bill Buzbee1465db52009-09-23 17:17:35 -07001260
Bill Buzbee45273872010-03-11 11:12:15 -08001261 //If already optimized out, just ignore
1262 if (mir->dalvikInsn.opCode == OP_NOP)
1263 return;
1264
Bill Buzbee1465db52009-09-23 17:17:35 -07001265 //Ugly, but necessary. Flush all Dalvik regs so Interp can find them
Bill Buzbeec6f10662010-02-09 11:16:15 -08001266 dvmCompilerFlushAllRegs(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07001267
Ben Chengba4fc8b2009-06-01 13:00:29 -07001268 if ((mir->next == NULL) || (flags & flagsToCheck)) {
1269 genPuntToInterp(cUnit, mir->offset);
1270 return;
1271 }
1272 int entryAddr = offsetof(InterpState,
1273 jitToInterpEntries.dvmJitToInterpSingleStep);
Bill Buzbee270c1d62009-08-13 16:58:07 -07001274 loadWordDisp(cUnit, rGLUE, entryAddr, r2);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001275 /* r0 = dalvik pc */
1276 loadConstant(cUnit, r0, (int) (cUnit->method->insns + mir->offset));
1277 /* r1 = dalvik pc of following instruction */
1278 loadConstant(cUnit, r1, (int) (cUnit->method->insns + mir->next->offset));
Bill Buzbee1465db52009-09-23 17:17:35 -07001279 opReg(cUnit, kOpBlx, r2);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001280}
1281
Bill Buzbeec1d9ed42010-02-02 11:04:33 -08001282/*
1283 * To prevent a thread in a monitor wait from blocking the Jit from
1284 * resetting the code cache, heavyweight monitor lock will not
1285 * be allowed to return to an existing translation. Instead, we will
1286 * handle them by branching to a handler, which will in turn call the
1287 * runtime lock routine and then branch directly back to the
1288 * interpreter main loop. Given the high cost of the heavyweight
1289 * lock operation, this additional cost should be slight (especially when
1290 * considering that we expect the vast majority of lock operations to
1291 * use the fast-path thin lock bypass).
1292 */
Ben Cheng5d90c202009-11-22 23:31:11 -08001293static void genMonitorPortable(CompilationUnit *cUnit, MIR *mir)
Bill Buzbee270c1d62009-08-13 16:58:07 -07001294{
Bill Buzbeeefbd3c52009-11-04 22:18:40 -08001295 bool isEnter = (mir->dalvikInsn.opCode == OP_MONITOR_ENTER);
Bill Buzbee1465db52009-09-23 17:17:35 -07001296 genExportPC(cUnit, mir);
Bill Buzbeec6f10662010-02-09 11:16:15 -08001297 dvmCompilerFlushAllRegs(cUnit); /* Send everything to home location */
1298 RegLocation rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07001299 loadValueDirectFixed(cUnit, rlSrc, r1);
1300 loadWordDisp(cUnit, rGLUE, offsetof(InterpState, self), r0);
Bill Buzbeec1d9ed42010-02-02 11:04:33 -08001301 genNullCheck(cUnit, rlSrc.sRegLow, r1, mir->offset, NULL);
Bill Buzbeeefbd3c52009-11-04 22:18:40 -08001302 if (isEnter) {
Bill Buzbeec1d9ed42010-02-02 11:04:33 -08001303 /* Get dPC of next insn */
1304 loadConstant(cUnit, r4PC, (int)(cUnit->method->insns + mir->offset +
1305 dexGetInstrWidthAbs(gDvm.instrWidth, OP_MONITOR_ENTER)));
1306#if defined(WITH_DEADLOCK_PREDICTION)
1307 genDispatchToHandler(cUnit, TEMPLATE_MONITOR_ENTER_DEBUG);
1308#else
1309 genDispatchToHandler(cUnit, TEMPLATE_MONITOR_ENTER);
1310#endif
Bill Buzbee1465db52009-09-23 17:17:35 -07001311 } else {
Ben Chengbd1326d2010-04-02 15:04:53 -07001312 LOAD_FUNC_ADDR(cUnit, r2, (int)dvmUnlockObject);
Bill Buzbeec1d9ed42010-02-02 11:04:33 -08001313 /* Do the call */
1314 opReg(cUnit, kOpBlx, r2);
Bill Buzbee6bbdd6b2010-02-16 14:40:01 -08001315 opRegImm(cUnit, kOpCmp, r0, 0); /* Did we throw? */
1316 ArmLIR *branchOver = opCondBranch(cUnit, kArmCondNe);
1317 loadConstant(cUnit, r0,
1318 (int) (cUnit->method->insns + mir->offset +
1319 dexGetInstrWidthAbs(gDvm.instrWidth, OP_MONITOR_EXIT)));
1320 genDispatchToHandler(cUnit, TEMPLATE_THROW_EXCEPTION_COMMON);
1321 ArmLIR *target = newLIR0(cUnit, kArmPseudoTargetLabel);
1322 target->defMask = ENCODE_ALL;
1323 branchOver->generic.target = (LIR *) target;
Elliott Hughes6a555132010-02-25 15:41:42 -08001324 dvmCompilerClobberCallRegs(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07001325 }
Bill Buzbee270c1d62009-08-13 16:58:07 -07001326}
1327
Ben Chengba4fc8b2009-06-01 13:00:29 -07001328/*
1329 * The following are the first-level codegen routines that analyze the format
1330 * of each bytecode then either dispatch special purpose codegen routines
1331 * or produce corresponding Thumb instructions directly.
1332 */
1333
1334static bool handleFmt10t_Fmt20t_Fmt30t(CompilationUnit *cUnit, MIR *mir,
Bill Buzbee89efc3d2009-07-28 11:22:22 -07001335 BasicBlock *bb, ArmLIR *labelList)
Ben Chengba4fc8b2009-06-01 13:00:29 -07001336{
1337 /* For OP_GOTO, OP_GOTO_16, and OP_GOTO_32 */
1338 genUnconditionalBranch(cUnit, &labelList[bb->taken->id]);
1339 return false;
1340}
1341
1342static bool handleFmt10x(CompilationUnit *cUnit, MIR *mir)
1343{
1344 OpCode dalvikOpCode = mir->dalvikInsn.opCode;
1345 if (((dalvikOpCode >= OP_UNUSED_3E) && (dalvikOpCode <= OP_UNUSED_43)) ||
Andy McFadden53878242010-03-05 07:24:27 -08001346 ((dalvikOpCode >= OP_UNUSED_E3) && (dalvikOpCode <= OP_UNUSED_E7))) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07001347 LOGE("Codegen: got unused opcode 0x%x\n",dalvikOpCode);
1348 return true;
1349 }
1350 switch (dalvikOpCode) {
1351 case OP_RETURN_VOID:
1352 genReturnCommon(cUnit,mir);
1353 break;
1354 case OP_UNUSED_73:
1355 case OP_UNUSED_79:
1356 case OP_UNUSED_7A:
1357 LOGE("Codegen: got unused opcode 0x%x\n",dalvikOpCode);
1358 return true;
1359 case OP_NOP:
1360 break;
1361 default:
1362 return true;
1363 }
1364 return false;
1365}
1366
1367static bool handleFmt11n_Fmt31i(CompilationUnit *cUnit, MIR *mir)
1368{
Bill Buzbee1465db52009-09-23 17:17:35 -07001369 RegLocation rlDest;
1370 RegLocation rlResult;
1371 if (mir->ssaRep->numDefs == 2) {
Bill Buzbeec6f10662010-02-09 11:16:15 -08001372 rlDest = dvmCompilerGetDestWide(cUnit, mir, 0, 1);
Bill Buzbee1465db52009-09-23 17:17:35 -07001373 } else {
Bill Buzbeec6f10662010-02-09 11:16:15 -08001374 rlDest = dvmCompilerGetDest(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07001375 }
Ben Chenge9695e52009-06-16 16:11:47 -07001376
Ben Chengba4fc8b2009-06-01 13:00:29 -07001377 switch (mir->dalvikInsn.opCode) {
1378 case OP_CONST:
Ben Chenge9695e52009-06-16 16:11:47 -07001379 case OP_CONST_4: {
Bill Buzbeec6f10662010-02-09 11:16:15 -08001380 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kAnyReg, true);
Ben Chengbd1326d2010-04-02 15:04:53 -07001381 loadConstantNoClobber(cUnit, rlResult.lowReg, mir->dalvikInsn.vB);
Bill Buzbee1465db52009-09-23 17:17:35 -07001382 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001383 break;
Ben Chenge9695e52009-06-16 16:11:47 -07001384 }
1385 case OP_CONST_WIDE_32: {
Bill Buzbee1465db52009-09-23 17:17:35 -07001386 //TUNING: single routine to load constant pair for support doubles
Bill Buzbee964a7b02010-01-28 12:54:19 -08001387 //TUNING: load 0/-1 separately to avoid load dependency
Bill Buzbeec6f10662010-02-09 11:16:15 -08001388 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Ben Chengbd1326d2010-04-02 15:04:53 -07001389 loadConstantNoClobber(cUnit, rlResult.lowReg, mir->dalvikInsn.vB);
Bill Buzbee1465db52009-09-23 17:17:35 -07001390 opRegRegImm(cUnit, kOpAsr, rlResult.highReg,
1391 rlResult.lowReg, 31);
1392 storeValueWide(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001393 break;
Ben Chenge9695e52009-06-16 16:11:47 -07001394 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07001395 default:
1396 return true;
1397 }
1398 return false;
1399}
1400
1401static bool handleFmt21h(CompilationUnit *cUnit, MIR *mir)
1402{
Bill Buzbee1465db52009-09-23 17:17:35 -07001403 RegLocation rlDest;
1404 RegLocation rlResult;
1405 if (mir->ssaRep->numDefs == 2) {
Bill Buzbeec6f10662010-02-09 11:16:15 -08001406 rlDest = dvmCompilerGetDestWide(cUnit, mir, 0, 1);
Bill Buzbee1465db52009-09-23 17:17:35 -07001407 } else {
Bill Buzbeec6f10662010-02-09 11:16:15 -08001408 rlDest = dvmCompilerGetDest(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07001409 }
Bill Buzbeec6f10662010-02-09 11:16:15 -08001410 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kAnyReg, true);
Ben Chenge9695e52009-06-16 16:11:47 -07001411
Ben Chengba4fc8b2009-06-01 13:00:29 -07001412 switch (mir->dalvikInsn.opCode) {
Ben Chenge9695e52009-06-16 16:11:47 -07001413 case OP_CONST_HIGH16: {
Ben Chengbd1326d2010-04-02 15:04:53 -07001414 loadConstantNoClobber(cUnit, rlResult.lowReg,
1415 mir->dalvikInsn.vB << 16);
Bill Buzbee1465db52009-09-23 17:17:35 -07001416 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001417 break;
Ben Chenge9695e52009-06-16 16:11:47 -07001418 }
1419 case OP_CONST_WIDE_HIGH16: {
Bill Buzbee1465db52009-09-23 17:17:35 -07001420 loadConstantValueWide(cUnit, rlResult.lowReg, rlResult.highReg,
1421 0, mir->dalvikInsn.vB << 16);
1422 storeValueWide(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001423 break;
Ben Chenge9695e52009-06-16 16:11:47 -07001424 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07001425 default:
1426 return true;
1427 }
1428 return false;
1429}
1430
1431static bool handleFmt20bc(CompilationUnit *cUnit, MIR *mir)
1432{
1433 /* For OP_THROW_VERIFICATION_ERROR */
1434 genInterpSingleStep(cUnit, mir);
1435 return false;
1436}
1437
1438static bool handleFmt21c_Fmt31c(CompilationUnit *cUnit, MIR *mir)
1439{
Bill Buzbee1465db52009-09-23 17:17:35 -07001440 RegLocation rlResult;
1441 RegLocation rlDest;
1442 RegLocation rlSrc;
Ben Chenge9695e52009-06-16 16:11:47 -07001443
Ben Chengba4fc8b2009-06-01 13:00:29 -07001444 switch (mir->dalvikInsn.opCode) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07001445 case OP_CONST_STRING_JUMBO:
1446 case OP_CONST_STRING: {
1447 void *strPtr = (void*)
1448 (cUnit->method->clazz->pDvmDex->pResStrings[mir->dalvikInsn.vB]);
1449 assert(strPtr != NULL);
Bill Buzbeec6f10662010-02-09 11:16:15 -08001450 rlDest = dvmCompilerGetDest(cUnit, mir, 0);
1451 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Ben Chengbd1326d2010-04-02 15:04:53 -07001452 loadConstantNoClobber(cUnit, rlResult.lowReg, (int) strPtr );
Bill Buzbee1465db52009-09-23 17:17:35 -07001453 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001454 break;
1455 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07001456 case OP_CONST_CLASS: {
1457 void *classPtr = (void*)
1458 (cUnit->method->clazz->pDvmDex->pResClasses[mir->dalvikInsn.vB]);
1459 assert(classPtr != NULL);
Bill Buzbeec6f10662010-02-09 11:16:15 -08001460 rlDest = dvmCompilerGetDest(cUnit, mir, 0);
1461 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Ben Chengbd1326d2010-04-02 15:04:53 -07001462 loadConstantNoClobber(cUnit, rlResult.lowReg, (int) classPtr );
Bill Buzbee1465db52009-09-23 17:17:35 -07001463 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001464 break;
1465 }
1466 case OP_SGET_OBJECT:
1467 case OP_SGET_BOOLEAN:
1468 case OP_SGET_CHAR:
1469 case OP_SGET_BYTE:
1470 case OP_SGET_SHORT:
1471 case OP_SGET: {
Bill Buzbee50a6bf22009-07-08 13:08:04 -07001472 int valOffset = offsetof(StaticField, value);
Bill Buzbeec6f10662010-02-09 11:16:15 -08001473 int tReg = dvmCompilerAllocTemp(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001474 void *fieldPtr = (void*)
1475 (cUnit->method->clazz->pDvmDex->pResFields[mir->dalvikInsn.vB]);
1476 assert(fieldPtr != NULL);
Bill Buzbeec6f10662010-02-09 11:16:15 -08001477 rlDest = dvmCompilerGetDest(cUnit, mir, 0);
1478 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kAnyReg, true);
Bill Buzbee1465db52009-09-23 17:17:35 -07001479 loadConstant(cUnit, tReg, (int) fieldPtr + valOffset);
Ben Cheng11d8f142010-03-24 15:24:19 -07001480
1481 HEAP_ACCESS_SHADOW(true);
Bill Buzbee1465db52009-09-23 17:17:35 -07001482 loadWordDisp(cUnit, tReg, 0, rlResult.lowReg);
Ben Cheng11d8f142010-03-24 15:24:19 -07001483 HEAP_ACCESS_SHADOW(false);
1484
Bill Buzbee1465db52009-09-23 17:17:35 -07001485 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001486 break;
1487 }
1488 case OP_SGET_WIDE: {
Bill Buzbee50a6bf22009-07-08 13:08:04 -07001489 int valOffset = offsetof(StaticField, value);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001490 void *fieldPtr = (void*)
1491 (cUnit->method->clazz->pDvmDex->pResFields[mir->dalvikInsn.vB]);
Bill Buzbeec6f10662010-02-09 11:16:15 -08001492 int tReg = dvmCompilerAllocTemp(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001493 assert(fieldPtr != NULL);
Bill Buzbeec6f10662010-02-09 11:16:15 -08001494 rlDest = dvmCompilerGetDestWide(cUnit, mir, 0, 1);
1495 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kAnyReg, true);
Bill Buzbee1465db52009-09-23 17:17:35 -07001496 loadConstant(cUnit, tReg, (int) fieldPtr + valOffset);
Ben Cheng11d8f142010-03-24 15:24:19 -07001497
1498 HEAP_ACCESS_SHADOW(true);
Bill Buzbee1465db52009-09-23 17:17:35 -07001499 loadPair(cUnit, tReg, rlResult.lowReg, rlResult.highReg);
Ben Cheng11d8f142010-03-24 15:24:19 -07001500 HEAP_ACCESS_SHADOW(false);
1501
Bill Buzbee1465db52009-09-23 17:17:35 -07001502 storeValueWide(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001503 break;
1504 }
1505 case OP_SPUT_OBJECT:
1506 case OP_SPUT_BOOLEAN:
1507 case OP_SPUT_CHAR:
1508 case OP_SPUT_BYTE:
1509 case OP_SPUT_SHORT:
1510 case OP_SPUT: {
Bill Buzbee50a6bf22009-07-08 13:08:04 -07001511 int valOffset = offsetof(StaticField, value);
Bill Buzbeec6f10662010-02-09 11:16:15 -08001512 int tReg = dvmCompilerAllocTemp(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001513 void *fieldPtr = (void*)
1514 (cUnit->method->clazz->pDvmDex->pResFields[mir->dalvikInsn.vB]);
Ben Chenge9695e52009-06-16 16:11:47 -07001515
Ben Chengba4fc8b2009-06-01 13:00:29 -07001516 assert(fieldPtr != NULL);
Bill Buzbeec6f10662010-02-09 11:16:15 -08001517 rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07001518 rlSrc = loadValue(cUnit, rlSrc, kAnyReg);
1519 loadConstant(cUnit, tReg, (int) fieldPtr + valOffset);
Ben Cheng11d8f142010-03-24 15:24:19 -07001520
1521 HEAP_ACCESS_SHADOW(true);
Bill Buzbee1465db52009-09-23 17:17:35 -07001522 storeWordDisp(cUnit, tReg, 0 ,rlSrc.lowReg);
Ben Cheng11d8f142010-03-24 15:24:19 -07001523 HEAP_ACCESS_SHADOW(false);
1524
Ben Chengba4fc8b2009-06-01 13:00:29 -07001525 break;
1526 }
1527 case OP_SPUT_WIDE: {
Bill Buzbeec6f10662010-02-09 11:16:15 -08001528 int tReg = dvmCompilerAllocTemp(cUnit);
Bill Buzbee50a6bf22009-07-08 13:08:04 -07001529 int valOffset = offsetof(StaticField, value);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001530 void *fieldPtr = (void*)
1531 (cUnit->method->clazz->pDvmDex->pResFields[mir->dalvikInsn.vB]);
Ben Chenge9695e52009-06-16 16:11:47 -07001532
Ben Chengba4fc8b2009-06-01 13:00:29 -07001533 assert(fieldPtr != NULL);
Bill Buzbeec6f10662010-02-09 11:16:15 -08001534 rlSrc = dvmCompilerGetSrcWide(cUnit, mir, 0, 1);
Bill Buzbee1465db52009-09-23 17:17:35 -07001535 rlSrc = loadValueWide(cUnit, rlSrc, kAnyReg);
1536 loadConstant(cUnit, tReg, (int) fieldPtr + valOffset);
Ben Cheng11d8f142010-03-24 15:24:19 -07001537
1538 HEAP_ACCESS_SHADOW(true);
Bill Buzbee1465db52009-09-23 17:17:35 -07001539 storePair(cUnit, tReg, rlSrc.lowReg, rlSrc.highReg);
Ben Cheng11d8f142010-03-24 15:24:19 -07001540 HEAP_ACCESS_SHADOW(false);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001541 break;
1542 }
1543 case OP_NEW_INSTANCE: {
Ben Chenge9695e52009-06-16 16:11:47 -07001544 /*
1545 * Obey the calling convention and don't mess with the register
1546 * usage.
1547 */
Ben Chengba4fc8b2009-06-01 13:00:29 -07001548 ClassObject *classPtr = (void*)
1549 (cUnit->method->clazz->pDvmDex->pResClasses[mir->dalvikInsn.vB]);
1550 assert(classPtr != NULL);
1551 assert(classPtr->status & CLASS_INITIALIZED);
Ben Cheng79d173c2009-09-29 16:12:51 -07001552 /*
1553 * If it is going to throw, it should not make to the trace to begin
Bill Buzbee1465db52009-09-23 17:17:35 -07001554 * with. However, Alloc might throw, so we need to genExportPC()
Ben Cheng79d173c2009-09-29 16:12:51 -07001555 */
1556 assert((classPtr->accessFlags & (ACC_INTERFACE|ACC_ABSTRACT)) == 0);
Bill Buzbeec6f10662010-02-09 11:16:15 -08001557 dvmCompilerFlushAllRegs(cUnit); /* Everything to home location */
Bill Buzbee1465db52009-09-23 17:17:35 -07001558 genExportPC(cUnit, mir);
Ben Chengbd1326d2010-04-02 15:04:53 -07001559 LOAD_FUNC_ADDR(cUnit, r2, (int)dvmAllocObject);
Ben Chenge9695e52009-06-16 16:11:47 -07001560 loadConstant(cUnit, r0, (int) classPtr);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001561 loadConstant(cUnit, r1, ALLOC_DONT_TRACK);
Bill Buzbee1465db52009-09-23 17:17:35 -07001562 opReg(cUnit, kOpBlx, r2);
Elliott Hughes6a555132010-02-25 15:41:42 -08001563 dvmCompilerClobberCallRegs(cUnit);
Ben Cheng4f489172009-09-27 17:08:35 -07001564 /* generate a branch over if allocation is successful */
Bill Buzbee1465db52009-09-23 17:17:35 -07001565 opRegImm(cUnit, kOpCmp, r0, 0); /* NULL? */
1566 ArmLIR *branchOver = opCondBranch(cUnit, kArmCondNe);
Ben Cheng4f489172009-09-27 17:08:35 -07001567 /*
1568 * OOM exception needs to be thrown here and cannot re-execute
1569 */
1570 loadConstant(cUnit, r0,
1571 (int) (cUnit->method->insns + mir->offset));
1572 genDispatchToHandler(cUnit, TEMPLATE_THROW_EXCEPTION_COMMON);
1573 /* noreturn */
1574
Bill Buzbee1465db52009-09-23 17:17:35 -07001575 ArmLIR *target = newLIR0(cUnit, kArmPseudoTargetLabel);
Ben Cheng4f489172009-09-27 17:08:35 -07001576 target->defMask = ENCODE_ALL;
1577 branchOver->generic.target = (LIR *) target;
Bill Buzbeec6f10662010-02-09 11:16:15 -08001578 rlDest = dvmCompilerGetDest(cUnit, mir, 0);
1579 rlResult = dvmCompilerGetReturn(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07001580 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001581 break;
1582 }
1583 case OP_CHECK_CAST: {
Ben Chenge9695e52009-06-16 16:11:47 -07001584 /*
1585 * Obey the calling convention and don't mess with the register
1586 * usage.
1587 */
Ben Chengba4fc8b2009-06-01 13:00:29 -07001588 ClassObject *classPtr =
1589 (cUnit->method->clazz->pDvmDex->pResClasses[mir->dalvikInsn.vB]);
Bill Buzbee4df41a52009-11-12 17:07:16 -08001590 /*
1591 * Note: It is possible that classPtr is NULL at this point,
1592 * even though this instruction has been successfully interpreted.
1593 * If the previous interpretation had a null source, the
1594 * interpreter would not have bothered to resolve the clazz.
1595 * Bail out to the interpreter in this case, and log it
1596 * so that we can tell if it happens frequently.
1597 */
1598 if (classPtr == NULL) {
Ben Cheng11d8f142010-03-24 15:24:19 -07001599 LOGVV("null clazz in OP_CHECK_CAST, single-stepping");
Bill Buzbee4df41a52009-11-12 17:07:16 -08001600 genInterpSingleStep(cUnit, mir);
1601 return false;
1602 }
Bill Buzbeec6f10662010-02-09 11:16:15 -08001603 dvmCompilerFlushAllRegs(cUnit); /* Everything to home location */
Ben Chengba4fc8b2009-06-01 13:00:29 -07001604 loadConstant(cUnit, r1, (int) classPtr );
Bill Buzbeec6f10662010-02-09 11:16:15 -08001605 rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07001606 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
1607 opRegImm(cUnit, kOpCmp, rlSrc.lowReg, 0); /* Null? */
1608 ArmLIR *branch1 = opCondBranch(cUnit, kArmCondEq);
1609 /*
1610 * rlSrc.lowReg now contains object->clazz. Note that
1611 * it could have been allocated r0, but we're okay so long
1612 * as we don't do anything desctructive until r0 is loaded
1613 * with clazz.
1614 */
Ben Chengba4fc8b2009-06-01 13:00:29 -07001615 /* r0 now contains object->clazz */
Bill Buzbee1465db52009-09-23 17:17:35 -07001616 loadWordDisp(cUnit, rlSrc.lowReg, offsetof(Object, clazz), r0);
Ben Chengbd1326d2010-04-02 15:04:53 -07001617 LOAD_FUNC_ADDR(cUnit, r2, (int)dvmInstanceofNonTrivial);
Bill Buzbee1465db52009-09-23 17:17:35 -07001618 opRegReg(cUnit, kOpCmp, r0, r1);
1619 ArmLIR *branch2 = opCondBranch(cUnit, kArmCondEq);
1620 opReg(cUnit, kOpBlx, r2);
Elliott Hughes6a555132010-02-25 15:41:42 -08001621 dvmCompilerClobberCallRegs(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07001622 /*
1623 * If null, check cast failed - punt to the interpreter. Because
1624 * interpreter will be the one throwing, we don't need to
1625 * genExportPC() here.
1626 */
Bill Buzbee270c1d62009-08-13 16:58:07 -07001627 genZeroCheck(cUnit, r0, mir->offset, NULL);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001628 /* check cast passed - branch target here */
Bill Buzbee1465db52009-09-23 17:17:35 -07001629 ArmLIR *target = newLIR0(cUnit, kArmPseudoTargetLabel);
Ben Chengd7d426a2009-09-22 11:23:36 -07001630 target->defMask = ENCODE_ALL;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001631 branch1->generic.target = (LIR *)target;
1632 branch2->generic.target = (LIR *)target;
1633 break;
1634 }
1635 default:
1636 return true;
1637 }
1638 return false;
1639}
1640
1641static bool handleFmt11x(CompilationUnit *cUnit, MIR *mir)
1642{
1643 OpCode dalvikOpCode = mir->dalvikInsn.opCode;
Bill Buzbee1465db52009-09-23 17:17:35 -07001644 RegLocation rlResult;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001645 switch (dalvikOpCode) {
1646 case OP_MOVE_EXCEPTION: {
1647 int offset = offsetof(InterpState, self);
1648 int exOffset = offsetof(Thread, exception);
Bill Buzbeec6f10662010-02-09 11:16:15 -08001649 int selfReg = dvmCompilerAllocTemp(cUnit);
1650 int resetReg = dvmCompilerAllocTemp(cUnit);
1651 RegLocation rlDest = dvmCompilerGetDest(cUnit, mir, 0);
1652 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Bill Buzbee1465db52009-09-23 17:17:35 -07001653 loadWordDisp(cUnit, rGLUE, offset, selfReg);
Bill Buzbeef9f33282009-11-22 12:45:30 -08001654 loadConstant(cUnit, resetReg, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07001655 loadWordDisp(cUnit, selfReg, exOffset, rlResult.lowReg);
Bill Buzbeef9f33282009-11-22 12:45:30 -08001656 storeWordDisp(cUnit, selfReg, exOffset, resetReg);
Bill Buzbee1465db52009-09-23 17:17:35 -07001657 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001658 break;
1659 }
1660 case OP_MOVE_RESULT:
1661 case OP_MOVE_RESULT_OBJECT: {
Bill Buzbeec6f10662010-02-09 11:16:15 -08001662 RegLocation rlDest = dvmCompilerGetDest(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07001663 RegLocation rlSrc = LOC_DALVIK_RETURN_VAL;
1664 rlSrc.fp = rlDest.fp;
1665 storeValue(cUnit, rlDest, rlSrc);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001666 break;
1667 }
1668 case OP_MOVE_RESULT_WIDE: {
Bill Buzbeec6f10662010-02-09 11:16:15 -08001669 RegLocation rlDest = dvmCompilerGetDestWide(cUnit, mir, 0, 1);
Bill Buzbee1465db52009-09-23 17:17:35 -07001670 RegLocation rlSrc = LOC_DALVIK_RETURN_VAL_WIDE;
1671 rlSrc.fp = rlDest.fp;
1672 storeValueWide(cUnit, rlDest, rlSrc);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001673 break;
1674 }
1675 case OP_RETURN_WIDE: {
Bill Buzbeec6f10662010-02-09 11:16:15 -08001676 RegLocation rlSrc = dvmCompilerGetSrcWide(cUnit, mir, 0, 1);
Bill Buzbee1465db52009-09-23 17:17:35 -07001677 RegLocation rlDest = LOC_DALVIK_RETURN_VAL_WIDE;
1678 rlDest.fp = rlSrc.fp;
1679 storeValueWide(cUnit, rlDest, rlSrc);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001680 genReturnCommon(cUnit,mir);
1681 break;
1682 }
1683 case OP_RETURN:
1684 case OP_RETURN_OBJECT: {
Bill Buzbeec6f10662010-02-09 11:16:15 -08001685 RegLocation rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07001686 RegLocation rlDest = LOC_DALVIK_RETURN_VAL;
1687 rlDest.fp = rlSrc.fp;
1688 storeValue(cUnit, rlDest, rlSrc);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001689 genReturnCommon(cUnit,mir);
1690 break;
1691 }
Bill Buzbee1465db52009-09-23 17:17:35 -07001692 case OP_MONITOR_EXIT:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001693 case OP_MONITOR_ENTER:
Bill Buzbeed0937ef2009-12-22 16:15:39 -08001694#if defined(WITH_DEADLOCK_PREDICTION) || defined(WITH_MONITOR_TRACKING)
Ben Cheng5d90c202009-11-22 23:31:11 -08001695 genMonitorPortable(cUnit, mir);
Bill Buzbee1465db52009-09-23 17:17:35 -07001696#else
Ben Cheng5d90c202009-11-22 23:31:11 -08001697 genMonitor(cUnit, mir);
Bill Buzbee1465db52009-09-23 17:17:35 -07001698#endif
Ben Chengba4fc8b2009-06-01 13:00:29 -07001699 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001700 case OP_THROW: {
1701 genInterpSingleStep(cUnit, mir);
1702 break;
1703 }
1704 default:
1705 return true;
1706 }
1707 return false;
1708}
1709
Bill Buzbeed45ba372009-06-15 17:00:57 -07001710static bool handleFmt12x(CompilationUnit *cUnit, MIR *mir)
1711{
1712 OpCode opCode = mir->dalvikInsn.opCode;
Bill Buzbee1465db52009-09-23 17:17:35 -07001713 RegLocation rlDest;
1714 RegLocation rlSrc;
1715 RegLocation rlResult;
Bill Buzbeed45ba372009-06-15 17:00:57 -07001716
Ben Chengba4fc8b2009-06-01 13:00:29 -07001717 if ( (opCode >= OP_ADD_INT_2ADDR) && (opCode <= OP_REM_DOUBLE_2ADDR)) {
Ben Cheng5d90c202009-11-22 23:31:11 -08001718 return genArithOp( cUnit, mir );
Ben Chengba4fc8b2009-06-01 13:00:29 -07001719 }
1720
Bill Buzbee1465db52009-09-23 17:17:35 -07001721 if (mir->ssaRep->numUses == 2)
Bill Buzbeec6f10662010-02-09 11:16:15 -08001722 rlSrc = dvmCompilerGetSrcWide(cUnit, mir, 0, 1);
Bill Buzbee1465db52009-09-23 17:17:35 -07001723 else
Bill Buzbeec6f10662010-02-09 11:16:15 -08001724 rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07001725 if (mir->ssaRep->numDefs == 2)
Bill Buzbeec6f10662010-02-09 11:16:15 -08001726 rlDest = dvmCompilerGetDestWide(cUnit, mir, 0, 1);
Bill Buzbee1465db52009-09-23 17:17:35 -07001727 else
Bill Buzbeec6f10662010-02-09 11:16:15 -08001728 rlDest = dvmCompilerGetDest(cUnit, mir, 0);
Ben Chenge9695e52009-06-16 16:11:47 -07001729
Ben Chengba4fc8b2009-06-01 13:00:29 -07001730 switch (opCode) {
Bill Buzbee1465db52009-09-23 17:17:35 -07001731 case OP_DOUBLE_TO_INT:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001732 case OP_INT_TO_FLOAT:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001733 case OP_FLOAT_TO_INT:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001734 case OP_DOUBLE_TO_FLOAT:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001735 case OP_FLOAT_TO_DOUBLE:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001736 case OP_INT_TO_DOUBLE:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001737 case OP_FLOAT_TO_LONG:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001738 case OP_LONG_TO_FLOAT:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001739 case OP_DOUBLE_TO_LONG:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001740 case OP_LONG_TO_DOUBLE:
Ben Cheng5d90c202009-11-22 23:31:11 -08001741 return genConversion(cUnit, mir);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001742 case OP_NEG_INT:
1743 case OP_NOT_INT:
Ben Cheng5d90c202009-11-22 23:31:11 -08001744 return genArithOpInt(cUnit, mir, rlDest, rlSrc, rlSrc);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001745 case OP_NEG_LONG:
1746 case OP_NOT_LONG:
Ben Cheng5d90c202009-11-22 23:31:11 -08001747 return genArithOpLong(cUnit, mir, rlDest, rlSrc, rlSrc);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001748 case OP_NEG_FLOAT:
Ben Cheng5d90c202009-11-22 23:31:11 -08001749 return genArithOpFloat(cUnit, mir, rlDest, rlSrc, rlSrc);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001750 case OP_NEG_DOUBLE:
Ben Cheng5d90c202009-11-22 23:31:11 -08001751 return genArithOpDouble(cUnit, mir, rlDest, rlSrc, rlSrc);
Bill Buzbee1465db52009-09-23 17:17:35 -07001752 case OP_MOVE_WIDE:
1753 storeValueWide(cUnit, rlDest, rlSrc);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001754 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07001755 case OP_INT_TO_LONG:
Bill Buzbeec6f10662010-02-09 11:16:15 -08001756 rlSrc = dvmCompilerUpdateLoc(cUnit, rlSrc);
1757 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Bill Buzbee964a7b02010-01-28 12:54:19 -08001758 //TUNING: shouldn't loadValueDirect already check for phys reg?
Bill Buzbee1465db52009-09-23 17:17:35 -07001759 if (rlSrc.location == kLocPhysReg) {
1760 genRegCopy(cUnit, rlResult.lowReg, rlSrc.lowReg);
1761 } else {
1762 loadValueDirect(cUnit, rlSrc, rlResult.lowReg);
1763 }
1764 opRegRegImm(cUnit, kOpAsr, rlResult.highReg,
1765 rlResult.lowReg, 31);
1766 storeValueWide(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001767 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07001768 case OP_LONG_TO_INT:
Bill Buzbeec6f10662010-02-09 11:16:15 -08001769 rlSrc = dvmCompilerUpdateLocWide(cUnit, rlSrc);
1770 rlSrc = dvmCompilerWideToNarrow(cUnit, rlSrc);
Bill Buzbee1465db52009-09-23 17:17:35 -07001771 // Intentional fallthrough
Ben Chengba4fc8b2009-06-01 13:00:29 -07001772 case OP_MOVE:
1773 case OP_MOVE_OBJECT:
Bill Buzbee1465db52009-09-23 17:17:35 -07001774 storeValue(cUnit, rlDest, rlSrc);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001775 break;
1776 case OP_INT_TO_BYTE:
Bill Buzbee1465db52009-09-23 17:17:35 -07001777 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
Bill Buzbeec6f10662010-02-09 11:16:15 -08001778 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Bill Buzbee1465db52009-09-23 17:17:35 -07001779 opRegReg(cUnit, kOp2Byte, rlResult.lowReg, rlSrc.lowReg);
1780 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001781 break;
1782 case OP_INT_TO_SHORT:
Bill Buzbee1465db52009-09-23 17:17:35 -07001783 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
Bill Buzbeec6f10662010-02-09 11:16:15 -08001784 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Bill Buzbee1465db52009-09-23 17:17:35 -07001785 opRegReg(cUnit, kOp2Short, rlResult.lowReg, rlSrc.lowReg);
1786 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001787 break;
1788 case OP_INT_TO_CHAR:
Bill Buzbee1465db52009-09-23 17:17:35 -07001789 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
Bill Buzbeec6f10662010-02-09 11:16:15 -08001790 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Bill Buzbee1465db52009-09-23 17:17:35 -07001791 opRegReg(cUnit, kOp2Char, rlResult.lowReg, rlSrc.lowReg);
1792 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001793 break;
1794 case OP_ARRAY_LENGTH: {
1795 int lenOffset = offsetof(ArrayObject, length);
Bill Buzbee1465db52009-09-23 17:17:35 -07001796 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
1797 genNullCheck(cUnit, rlSrc.sRegLow, rlSrc.lowReg,
1798 mir->offset, NULL);
Bill Buzbeec6f10662010-02-09 11:16:15 -08001799 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Bill Buzbee1465db52009-09-23 17:17:35 -07001800 loadWordDisp(cUnit, rlSrc.lowReg, lenOffset,
1801 rlResult.lowReg);
1802 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001803 break;
1804 }
1805 default:
1806 return true;
1807 }
1808 return false;
1809}
1810
1811static bool handleFmt21s(CompilationUnit *cUnit, MIR *mir)
1812{
1813 OpCode dalvikOpCode = mir->dalvikInsn.opCode;
Bill Buzbee1465db52009-09-23 17:17:35 -07001814 RegLocation rlDest;
1815 RegLocation rlResult;
1816 int BBBB = mir->dalvikInsn.vB;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001817 if (dalvikOpCode == OP_CONST_WIDE_16) {
Bill Buzbeec6f10662010-02-09 11:16:15 -08001818 rlDest = dvmCompilerGetDestWide(cUnit, mir, 0, 1);
1819 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Ben Chengbd1326d2010-04-02 15:04:53 -07001820 loadConstantNoClobber(cUnit, rlResult.lowReg, BBBB);
Bill Buzbee964a7b02010-01-28 12:54:19 -08001821 //TUNING: do high separately to avoid load dependency
Bill Buzbee1465db52009-09-23 17:17:35 -07001822 opRegRegImm(cUnit, kOpAsr, rlResult.highReg, rlResult.lowReg, 31);
1823 storeValueWide(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001824 } else if (dalvikOpCode == OP_CONST_16) {
Bill Buzbeec6f10662010-02-09 11:16:15 -08001825 rlDest = dvmCompilerGetDest(cUnit, mir, 0);
1826 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kAnyReg, true);
Ben Chengbd1326d2010-04-02 15:04:53 -07001827 loadConstantNoClobber(cUnit, rlResult.lowReg, BBBB);
Bill Buzbee1465db52009-09-23 17:17:35 -07001828 storeValue(cUnit, rlDest, rlResult);
1829 } else
Ben Chengba4fc8b2009-06-01 13:00:29 -07001830 return true;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001831 return false;
1832}
1833
1834/* Compare agaist zero */
1835static bool handleFmt21t(CompilationUnit *cUnit, MIR *mir, BasicBlock *bb,
Bill Buzbee89efc3d2009-07-28 11:22:22 -07001836 ArmLIR *labelList)
Ben Chengba4fc8b2009-06-01 13:00:29 -07001837{
1838 OpCode dalvikOpCode = mir->dalvikInsn.opCode;
Bill Buzbee89efc3d2009-07-28 11:22:22 -07001839 ArmConditionCode cond;
Bill Buzbeec6f10662010-02-09 11:16:15 -08001840 RegLocation rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07001841 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
1842 opRegImm(cUnit, kOpCmp, rlSrc.lowReg, 0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001843
Bill Buzbee270c1d62009-08-13 16:58:07 -07001844//TUNING: break this out to allow use of Thumb2 CB[N]Z
Ben Chengba4fc8b2009-06-01 13:00:29 -07001845 switch (dalvikOpCode) {
1846 case OP_IF_EQZ:
Bill Buzbee1465db52009-09-23 17:17:35 -07001847 cond = kArmCondEq;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001848 break;
1849 case OP_IF_NEZ:
Bill Buzbee1465db52009-09-23 17:17:35 -07001850 cond = kArmCondNe;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001851 break;
1852 case OP_IF_LTZ:
Bill Buzbee1465db52009-09-23 17:17:35 -07001853 cond = kArmCondLt;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001854 break;
1855 case OP_IF_GEZ:
Bill Buzbee1465db52009-09-23 17:17:35 -07001856 cond = kArmCondGe;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001857 break;
1858 case OP_IF_GTZ:
Bill Buzbee1465db52009-09-23 17:17:35 -07001859 cond = kArmCondGt;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001860 break;
1861 case OP_IF_LEZ:
Bill Buzbee1465db52009-09-23 17:17:35 -07001862 cond = kArmCondLe;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001863 break;
1864 default:
1865 cond = 0;
1866 LOGE("Unexpected opcode (%d) for Fmt21t\n", dalvikOpCode);
Bill Buzbeefc519dc2010-03-06 23:30:57 -08001867 dvmCompilerAbort(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001868 }
1869 genConditionalBranch(cUnit, cond, &labelList[bb->taken->id]);
1870 /* This mostly likely will be optimized away in a later phase */
1871 genUnconditionalBranch(cUnit, &labelList[bb->fallThrough->id]);
1872 return false;
1873}
1874
Elliott Hughesb4c05972010-02-24 16:36:18 -08001875static bool isPowerOfTwo(int x)
1876{
1877 return (x & (x - 1)) == 0;
1878}
1879
1880// Returns true if no more than two bits are set in 'x'.
1881static bool isPopCountLE2(unsigned int x)
1882{
1883 x &= x - 1;
1884 return (x & (x - 1)) == 0;
1885}
1886
1887// Returns the index of the lowest set bit in 'x'.
1888static int lowestSetBit(unsigned int x) {
1889 int bit_posn = 0;
1890 while ((x & 0xf) == 0) {
1891 bit_posn += 4;
1892 x >>= 4;
Bill Buzbee78cb0e22010-02-11 14:04:53 -08001893 }
Elliott Hughesb4c05972010-02-24 16:36:18 -08001894 while ((x & 1) == 0) {
1895 bit_posn++;
1896 x >>= 1;
1897 }
1898 return bit_posn;
1899}
1900
1901// Returns true if it added instructions to 'cUnit' to multiply 'rlSrc' by 'lit'
1902// and store the result in 'rlDest'.
1903static bool handleEasyMultiply(CompilationUnit *cUnit,
1904 RegLocation rlSrc, RegLocation rlDest, int lit)
1905{
1906 // Can we simplify this multiplication?
1907 bool powerOfTwo = false;
1908 bool popCountLE2 = false;
1909 bool powerOfTwoMinusOne = false;
1910 if (lit < 2) {
1911 // Avoid special cases.
1912 return false;
1913 } else if (isPowerOfTwo(lit)) {
1914 powerOfTwo = true;
1915 } else if (isPopCountLE2(lit)) {
1916 popCountLE2 = true;
1917 } else if (isPowerOfTwo(lit + 1)) {
1918 powerOfTwoMinusOne = true;
1919 } else {
1920 return false;
1921 }
1922 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
1923 RegLocation rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
1924 if (powerOfTwo) {
1925 // Shift.
1926 opRegRegImm(cUnit, kOpLsl, rlResult.lowReg, rlSrc.lowReg,
1927 lowestSetBit(lit));
1928 } else if (popCountLE2) {
1929 // Shift and add and shift.
1930 int firstBit = lowestSetBit(lit);
1931 int secondBit = lowestSetBit(lit ^ (1 << firstBit));
1932 genMultiplyByTwoBitMultiplier(cUnit, rlSrc, rlResult, lit,
1933 firstBit, secondBit);
1934 } else {
1935 // Reverse subtract: (src << (shift + 1)) - src.
1936 assert(powerOfTwoMinusOne);
1937 // TODO: rsb dst, src, src lsl#lowestSetBit(lit + 1)
1938 int tReg = dvmCompilerAllocTemp(cUnit);
1939 opRegRegImm(cUnit, kOpLsl, tReg, rlSrc.lowReg, lowestSetBit(lit + 1));
1940 opRegRegReg(cUnit, kOpSub, rlResult.lowReg, tReg, rlSrc.lowReg);
1941 }
1942 storeValue(cUnit, rlDest, rlResult);
1943 return true;
Bill Buzbee78cb0e22010-02-11 14:04:53 -08001944}
1945
Ben Chengba4fc8b2009-06-01 13:00:29 -07001946static bool handleFmt22b_Fmt22s(CompilationUnit *cUnit, MIR *mir)
1947{
1948 OpCode dalvikOpCode = mir->dalvikInsn.opCode;
Bill Buzbeec6f10662010-02-09 11:16:15 -08001949 RegLocation rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
1950 RegLocation rlDest = dvmCompilerGetDest(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07001951 RegLocation rlResult;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001952 int lit = mir->dalvikInsn.vC;
Ben Cheng4f489172009-09-27 17:08:35 -07001953 OpKind op = 0; /* Make gcc happy */
Bill Buzbee1465db52009-09-23 17:17:35 -07001954 int shiftOp = false;
1955 bool isDiv = false;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001956
Ben Chengba4fc8b2009-06-01 13:00:29 -07001957 switch (dalvikOpCode) {
Bill Buzbee1465db52009-09-23 17:17:35 -07001958 case OP_RSUB_INT_LIT8:
1959 case OP_RSUB_INT: {
1960 int tReg;
1961 //TUNING: add support for use of Arm rsub op
1962 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
Bill Buzbeec6f10662010-02-09 11:16:15 -08001963 tReg = dvmCompilerAllocTemp(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07001964 loadConstant(cUnit, tReg, lit);
Bill Buzbeec6f10662010-02-09 11:16:15 -08001965 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Bill Buzbee1465db52009-09-23 17:17:35 -07001966 opRegRegReg(cUnit, kOpSub, rlResult.lowReg,
1967 tReg, rlSrc.lowReg);
1968 storeValue(cUnit, rlDest, rlResult);
1969 return false;
1970 break;
1971 }
1972
Ben Chengba4fc8b2009-06-01 13:00:29 -07001973 case OP_ADD_INT_LIT8:
1974 case OP_ADD_INT_LIT16:
Bill Buzbee1465db52009-09-23 17:17:35 -07001975 op = kOpAdd;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001976 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001977 case OP_MUL_INT_LIT8:
Bill Buzbee78cb0e22010-02-11 14:04:53 -08001978 case OP_MUL_INT_LIT16: {
Elliott Hughesb4c05972010-02-24 16:36:18 -08001979 if (handleEasyMultiply(cUnit, rlSrc, rlDest, lit)) {
1980 return false;
Bill Buzbee78cb0e22010-02-11 14:04:53 -08001981 }
Elliott Hughesb4c05972010-02-24 16:36:18 -08001982 op = kOpMul;
Bill Buzbee1465db52009-09-23 17:17:35 -07001983 break;
Bill Buzbee78cb0e22010-02-11 14:04:53 -08001984 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07001985 case OP_AND_INT_LIT8:
1986 case OP_AND_INT_LIT16:
Bill Buzbee1465db52009-09-23 17:17:35 -07001987 op = kOpAnd;
1988 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001989 case OP_OR_INT_LIT8:
1990 case OP_OR_INT_LIT16:
Bill Buzbee1465db52009-09-23 17:17:35 -07001991 op = kOpOr;
1992 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001993 case OP_XOR_INT_LIT8:
1994 case OP_XOR_INT_LIT16:
Bill Buzbee1465db52009-09-23 17:17:35 -07001995 op = kOpXor;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001996 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001997 case OP_SHL_INT_LIT8:
Bill Buzbee0e605272009-12-01 14:28:05 -08001998 lit &= 31;
Bill Buzbee1465db52009-09-23 17:17:35 -07001999 shiftOp = true;
2000 op = kOpLsl;
2001 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002002 case OP_SHR_INT_LIT8:
Bill Buzbee0e605272009-12-01 14:28:05 -08002003 lit &= 31;
Bill Buzbee1465db52009-09-23 17:17:35 -07002004 shiftOp = true;
2005 op = kOpAsr;
2006 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002007 case OP_USHR_INT_LIT8:
Bill Buzbee0e605272009-12-01 14:28:05 -08002008 lit &= 31;
Bill Buzbee1465db52009-09-23 17:17:35 -07002009 shiftOp = true;
2010 op = kOpLsr;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002011 break;
2012
2013 case OP_DIV_INT_LIT8:
2014 case OP_DIV_INT_LIT16:
Ben Chengba4fc8b2009-06-01 13:00:29 -07002015 case OP_REM_INT_LIT8:
2016 case OP_REM_INT_LIT16:
2017 if (lit == 0) {
2018 /* Let the interpreter deal with div by 0 */
2019 genInterpSingleStep(cUnit, mir);
2020 return false;
2021 }
Bill Buzbeec6f10662010-02-09 11:16:15 -08002022 dvmCompilerFlushAllRegs(cUnit); /* Everything to home location */
Bill Buzbee1465db52009-09-23 17:17:35 -07002023 loadValueDirectFixed(cUnit, rlSrc, r0);
Bill Buzbeec6f10662010-02-09 11:16:15 -08002024 dvmCompilerClobber(cUnit, r0);
Bill Buzbee1465db52009-09-23 17:17:35 -07002025 if ((dalvikOpCode == OP_DIV_INT_LIT8) ||
2026 (dalvikOpCode == OP_DIV_INT_LIT16)) {
Ben Chengbd1326d2010-04-02 15:04:53 -07002027 LOAD_FUNC_ADDR(cUnit, r2, (int)__aeabi_idiv);
Bill Buzbee1465db52009-09-23 17:17:35 -07002028 isDiv = true;
2029 } else {
Ben Chengbd1326d2010-04-02 15:04:53 -07002030 LOAD_FUNC_ADDR(cUnit, r2, (int)__aeabi_idivmod);
Bill Buzbee1465db52009-09-23 17:17:35 -07002031 isDiv = false;
2032 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07002033 loadConstant(cUnit, r1, lit);
Bill Buzbee1465db52009-09-23 17:17:35 -07002034 opReg(cUnit, kOpBlx, r2);
Elliott Hughes6a555132010-02-25 15:41:42 -08002035 dvmCompilerClobberCallRegs(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07002036 if (isDiv)
Bill Buzbeec6f10662010-02-09 11:16:15 -08002037 rlResult = dvmCompilerGetReturn(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07002038 else
Bill Buzbeec6f10662010-02-09 11:16:15 -08002039 rlResult = dvmCompilerGetReturnAlt(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07002040 storeValue(cUnit, rlDest, rlResult);
2041 return false;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002042 break;
2043 default:
2044 return true;
2045 }
Bill Buzbee1465db52009-09-23 17:17:35 -07002046 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
Bill Buzbeec6f10662010-02-09 11:16:15 -08002047 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Bill Buzbee1465db52009-09-23 17:17:35 -07002048 // Avoid shifts by literal 0 - no support in Thumb. Change to copy
2049 if (shiftOp && (lit == 0)) {
2050 genRegCopy(cUnit, rlResult.lowReg, rlSrc.lowReg);
2051 } else {
2052 opRegRegImm(cUnit, op, rlResult.lowReg, rlSrc.lowReg, lit);
2053 }
2054 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002055 return false;
2056}
2057
2058static bool handleFmt22c(CompilationUnit *cUnit, MIR *mir)
2059{
2060 OpCode dalvikOpCode = mir->dalvikInsn.opCode;
2061 int fieldOffset;
2062
2063 if (dalvikOpCode >= OP_IGET && dalvikOpCode <= OP_IPUT_SHORT) {
2064 InstField *pInstField = (InstField *)
2065 cUnit->method->clazz->pDvmDex->pResFields[mir->dalvikInsn.vC];
Ben Chengba4fc8b2009-06-01 13:00:29 -07002066
2067 assert(pInstField != NULL);
2068 fieldOffset = pInstField->byteOffset;
2069 } else {
Ben Chenga0e7b602009-10-13 23:09:01 -07002070 /* Deliberately break the code while make the compiler happy */
2071 fieldOffset = -1;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002072 }
2073 switch (dalvikOpCode) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07002074 case OP_NEW_ARRAY: {
Bill Buzbee1465db52009-09-23 17:17:35 -07002075 // Generates a call - use explicit registers
Bill Buzbeec6f10662010-02-09 11:16:15 -08002076 RegLocation rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
2077 RegLocation rlDest = dvmCompilerGetDest(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07002078 RegLocation rlResult;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002079 void *classPtr = (void*)
2080 (cUnit->method->clazz->pDvmDex->pResClasses[mir->dalvikInsn.vC]);
2081 assert(classPtr != NULL);
Bill Buzbeec6f10662010-02-09 11:16:15 -08002082 dvmCompilerFlushAllRegs(cUnit); /* Everything to home location */
Bill Buzbee1465db52009-09-23 17:17:35 -07002083 genExportPC(cUnit, mir);
2084 loadValueDirectFixed(cUnit, rlSrc, r1); /* Len */
Ben Chengba4fc8b2009-06-01 13:00:29 -07002085 loadConstant(cUnit, r0, (int) classPtr );
Ben Chengbd1326d2010-04-02 15:04:53 -07002086 LOAD_FUNC_ADDR(cUnit, r3, (int)dvmAllocArrayByClass);
Ben Cheng4f489172009-09-27 17:08:35 -07002087 /*
2088 * "len < 0": bail to the interpreter to re-execute the
2089 * instruction
2090 */
Bill Buzbee89efc3d2009-07-28 11:22:22 -07002091 ArmLIR *pcrLabel =
Bill Buzbee1465db52009-09-23 17:17:35 -07002092 genRegImmCheck(cUnit, kArmCondMi, r1, 0, mir->offset, NULL);
Bill Buzbee270c1d62009-08-13 16:58:07 -07002093 loadConstant(cUnit, r2, ALLOC_DONT_TRACK);
Bill Buzbee1465db52009-09-23 17:17:35 -07002094 opReg(cUnit, kOpBlx, r3);
Elliott Hughes6a555132010-02-25 15:41:42 -08002095 dvmCompilerClobberCallRegs(cUnit);
Ben Cheng4f489172009-09-27 17:08:35 -07002096 /* generate a branch over if allocation is successful */
Bill Buzbee1465db52009-09-23 17:17:35 -07002097 opRegImm(cUnit, kOpCmp, r0, 0); /* NULL? */
2098 ArmLIR *branchOver = opCondBranch(cUnit, kArmCondNe);
Ben Cheng4f489172009-09-27 17:08:35 -07002099 /*
2100 * OOM exception needs to be thrown here and cannot re-execute
2101 */
2102 loadConstant(cUnit, r0,
2103 (int) (cUnit->method->insns + mir->offset));
2104 genDispatchToHandler(cUnit, TEMPLATE_THROW_EXCEPTION_COMMON);
2105 /* noreturn */
2106
Bill Buzbee1465db52009-09-23 17:17:35 -07002107 ArmLIR *target = newLIR0(cUnit, kArmPseudoTargetLabel);
Ben Cheng4f489172009-09-27 17:08:35 -07002108 target->defMask = ENCODE_ALL;
2109 branchOver->generic.target = (LIR *) target;
Bill Buzbeec6f10662010-02-09 11:16:15 -08002110 rlResult = dvmCompilerGetReturn(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07002111 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002112 break;
2113 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07002114 case OP_INSTANCE_OF: {
Bill Buzbee1465db52009-09-23 17:17:35 -07002115 // May generate a call - use explicit registers
Bill Buzbeec6f10662010-02-09 11:16:15 -08002116 RegLocation rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
2117 RegLocation rlDest = dvmCompilerGetDest(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07002118 RegLocation rlResult;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002119 ClassObject *classPtr =
2120 (cUnit->method->clazz->pDvmDex->pResClasses[mir->dalvikInsn.vC]);
Bill Buzbee480e6782010-01-27 15:43:08 -08002121 /*
2122 * Note: It is possible that classPtr is NULL at this point,
2123 * even though this instruction has been successfully interpreted.
2124 * If the previous interpretation had a null source, the
2125 * interpreter would not have bothered to resolve the clazz.
2126 * Bail out to the interpreter in this case, and log it
2127 * so that we can tell if it happens frequently.
2128 */
2129 if (classPtr == NULL) {
2130 LOGD("null clazz in OP_INSTANCE_OF, single-stepping");
2131 genInterpSingleStep(cUnit, mir);
2132 break;
2133 }
Bill Buzbeec6f10662010-02-09 11:16:15 -08002134 dvmCompilerFlushAllRegs(cUnit); /* Everything to home location */
Bill Buzbee1465db52009-09-23 17:17:35 -07002135 loadValueDirectFixed(cUnit, rlSrc, r0); /* Ref */
Ben Chengba4fc8b2009-06-01 13:00:29 -07002136 loadConstant(cUnit, r2, (int) classPtr );
Bill Buzbee270c1d62009-08-13 16:58:07 -07002137//TUNING: compare to 0 primative to allow use of CB[N]Z
Bill Buzbee1465db52009-09-23 17:17:35 -07002138 opRegImm(cUnit, kOpCmp, r0, 0); /* NULL? */
Ben Cheng752c7942009-06-22 10:50:07 -07002139 /* When taken r0 has NULL which can be used for store directly */
Bill Buzbee1465db52009-09-23 17:17:35 -07002140 ArmLIR *branch1 = opCondBranch(cUnit, kArmCondEq);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002141 /* r1 now contains object->clazz */
Bill Buzbee270c1d62009-08-13 16:58:07 -07002142 loadWordDisp(cUnit, r0, offsetof(Object, clazz), r1);
Bill Buzbee1465db52009-09-23 17:17:35 -07002143 /* r1 now contains object->clazz */
Ben Chengbd1326d2010-04-02 15:04:53 -07002144 LOAD_FUNC_ADDR(cUnit, r3, (int)dvmInstanceofNonTrivial);
Ben Cheng752c7942009-06-22 10:50:07 -07002145 loadConstant(cUnit, r0, 1); /* Assume true */
Bill Buzbee1465db52009-09-23 17:17:35 -07002146 opRegReg(cUnit, kOpCmp, r1, r2);
2147 ArmLIR *branch2 = opCondBranch(cUnit, kArmCondEq);
2148 genRegCopy(cUnit, r0, r1);
2149 genRegCopy(cUnit, r1, r2);
2150 opReg(cUnit, kOpBlx, r3);
Elliott Hughes6a555132010-02-25 15:41:42 -08002151 dvmCompilerClobberCallRegs(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002152 /* branch target here */
Bill Buzbee1465db52009-09-23 17:17:35 -07002153 ArmLIR *target = newLIR0(cUnit, kArmPseudoTargetLabel);
Ben Chengd7d426a2009-09-22 11:23:36 -07002154 target->defMask = ENCODE_ALL;
Bill Buzbeec6f10662010-02-09 11:16:15 -08002155 rlResult = dvmCompilerGetReturn(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07002156 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002157 branch1->generic.target = (LIR *)target;
2158 branch2->generic.target = (LIR *)target;
2159 break;
2160 }
2161 case OP_IGET_WIDE:
2162 genIGetWide(cUnit, mir, fieldOffset);
2163 break;
2164 case OP_IGET:
2165 case OP_IGET_OBJECT:
Bill Buzbee1465db52009-09-23 17:17:35 -07002166 genIGet(cUnit, mir, kWord, fieldOffset);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002167 break;
2168 case OP_IGET_BOOLEAN:
Bill Buzbee1465db52009-09-23 17:17:35 -07002169 genIGet(cUnit, mir, kUnsignedByte, fieldOffset);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002170 break;
2171 case OP_IGET_BYTE:
Bill Buzbee1465db52009-09-23 17:17:35 -07002172 genIGet(cUnit, mir, kSignedByte, fieldOffset);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002173 break;
2174 case OP_IGET_CHAR:
Bill Buzbee1465db52009-09-23 17:17:35 -07002175 genIGet(cUnit, mir, kUnsignedHalf, fieldOffset);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002176 break;
2177 case OP_IGET_SHORT:
Bill Buzbee1465db52009-09-23 17:17:35 -07002178 genIGet(cUnit, mir, kSignedHalf, fieldOffset);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002179 break;
2180 case OP_IPUT_WIDE:
2181 genIPutWide(cUnit, mir, fieldOffset);
2182 break;
2183 case OP_IPUT:
2184 case OP_IPUT_OBJECT:
Bill Buzbee1465db52009-09-23 17:17:35 -07002185 genIPut(cUnit, mir, kWord, fieldOffset);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002186 break;
2187 case OP_IPUT_SHORT:
2188 case OP_IPUT_CHAR:
Bill Buzbee1465db52009-09-23 17:17:35 -07002189 genIPut(cUnit, mir, kUnsignedHalf, fieldOffset);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002190 break;
2191 case OP_IPUT_BYTE:
2192 case OP_IPUT_BOOLEAN:
Bill Buzbee1465db52009-09-23 17:17:35 -07002193 genIPut(cUnit, mir, kUnsignedByte, fieldOffset);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002194 break;
Bill Buzbeeb16344a2010-03-15 17:19:12 -07002195 case OP_IGET_WIDE_VOLATILE:
2196 case OP_IPUT_WIDE_VOLATILE:
2197 case OP_SGET_WIDE_VOLATILE:
2198 case OP_SPUT_WIDE_VOLATILE:
2199 genInterpSingleStep(cUnit, mir);
2200 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002201 default:
2202 return true;
2203 }
2204 return false;
2205}
2206
2207static bool handleFmt22cs(CompilationUnit *cUnit, MIR *mir)
2208{
2209 OpCode dalvikOpCode = mir->dalvikInsn.opCode;
2210 int fieldOffset = mir->dalvikInsn.vC;
2211 switch (dalvikOpCode) {
2212 case OP_IGET_QUICK:
2213 case OP_IGET_OBJECT_QUICK:
Bill Buzbee1465db52009-09-23 17:17:35 -07002214 genIGet(cUnit, mir, kWord, fieldOffset);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002215 break;
2216 case OP_IPUT_QUICK:
2217 case OP_IPUT_OBJECT_QUICK:
Bill Buzbee1465db52009-09-23 17:17:35 -07002218 genIPut(cUnit, mir, kWord, fieldOffset);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002219 break;
2220 case OP_IGET_WIDE_QUICK:
2221 genIGetWide(cUnit, mir, fieldOffset);
2222 break;
2223 case OP_IPUT_WIDE_QUICK:
2224 genIPutWide(cUnit, mir, fieldOffset);
2225 break;
2226 default:
2227 return true;
2228 }
2229 return false;
2230
2231}
2232
2233/* Compare agaist zero */
2234static bool handleFmt22t(CompilationUnit *cUnit, MIR *mir, BasicBlock *bb,
Bill Buzbee89efc3d2009-07-28 11:22:22 -07002235 ArmLIR *labelList)
Ben Chengba4fc8b2009-06-01 13:00:29 -07002236{
2237 OpCode dalvikOpCode = mir->dalvikInsn.opCode;
Bill Buzbee89efc3d2009-07-28 11:22:22 -07002238 ArmConditionCode cond;
Bill Buzbeec6f10662010-02-09 11:16:15 -08002239 RegLocation rlSrc1 = dvmCompilerGetSrc(cUnit, mir, 0);
2240 RegLocation rlSrc2 = dvmCompilerGetSrc(cUnit, mir, 1);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002241
Bill Buzbee1465db52009-09-23 17:17:35 -07002242 rlSrc1 = loadValue(cUnit, rlSrc1, kCoreReg);
2243 rlSrc2 = loadValue(cUnit, rlSrc2, kCoreReg);
2244 opRegReg(cUnit, kOpCmp, rlSrc1.lowReg, rlSrc2.lowReg);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002245
2246 switch (dalvikOpCode) {
2247 case OP_IF_EQ:
Bill Buzbee1465db52009-09-23 17:17:35 -07002248 cond = kArmCondEq;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002249 break;
2250 case OP_IF_NE:
Bill Buzbee1465db52009-09-23 17:17:35 -07002251 cond = kArmCondNe;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002252 break;
2253 case OP_IF_LT:
Bill Buzbee1465db52009-09-23 17:17:35 -07002254 cond = kArmCondLt;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002255 break;
2256 case OP_IF_GE:
Bill Buzbee1465db52009-09-23 17:17:35 -07002257 cond = kArmCondGe;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002258 break;
2259 case OP_IF_GT:
Bill Buzbee1465db52009-09-23 17:17:35 -07002260 cond = kArmCondGt;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002261 break;
2262 case OP_IF_LE:
Bill Buzbee1465db52009-09-23 17:17:35 -07002263 cond = kArmCondLe;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002264 break;
2265 default:
2266 cond = 0;
2267 LOGE("Unexpected opcode (%d) for Fmt22t\n", dalvikOpCode);
Bill Buzbeefc519dc2010-03-06 23:30:57 -08002268 dvmCompilerAbort(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002269 }
2270 genConditionalBranch(cUnit, cond, &labelList[bb->taken->id]);
2271 /* This mostly likely will be optimized away in a later phase */
2272 genUnconditionalBranch(cUnit, &labelList[bb->fallThrough->id]);
2273 return false;
2274}
2275
2276static bool handleFmt22x_Fmt32x(CompilationUnit *cUnit, MIR *mir)
2277{
2278 OpCode opCode = mir->dalvikInsn.opCode;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002279
2280 switch (opCode) {
2281 case OP_MOVE_16:
2282 case OP_MOVE_OBJECT_16:
2283 case OP_MOVE_FROM16:
Ben Chenge9695e52009-06-16 16:11:47 -07002284 case OP_MOVE_OBJECT_FROM16: {
Bill Buzbeec6f10662010-02-09 11:16:15 -08002285 storeValue(cUnit, dvmCompilerGetDest(cUnit, mir, 0),
2286 dvmCompilerGetSrc(cUnit, mir, 0));
Ben Chengba4fc8b2009-06-01 13:00:29 -07002287 break;
Ben Chenge9695e52009-06-16 16:11:47 -07002288 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07002289 case OP_MOVE_WIDE_16:
Ben Chenge9695e52009-06-16 16:11:47 -07002290 case OP_MOVE_WIDE_FROM16: {
Bill Buzbeec6f10662010-02-09 11:16:15 -08002291 storeValueWide(cUnit, dvmCompilerGetDestWide(cUnit, mir, 0, 1),
2292 dvmCompilerGetSrcWide(cUnit, mir, 0, 1));
Ben Chengba4fc8b2009-06-01 13:00:29 -07002293 break;
Ben Chenge9695e52009-06-16 16:11:47 -07002294 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07002295 default:
2296 return true;
2297 }
2298 return false;
2299}
2300
2301static bool handleFmt23x(CompilationUnit *cUnit, MIR *mir)
2302{
2303 OpCode opCode = mir->dalvikInsn.opCode;
Bill Buzbee1465db52009-09-23 17:17:35 -07002304 RegLocation rlSrc1;
2305 RegLocation rlSrc2;
2306 RegLocation rlDest;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002307
2308 if ( (opCode >= OP_ADD_INT) && (opCode <= OP_REM_DOUBLE)) {
Ben Cheng5d90c202009-11-22 23:31:11 -08002309 return genArithOp( cUnit, mir );
Ben Chengba4fc8b2009-06-01 13:00:29 -07002310 }
2311
Bill Buzbee1465db52009-09-23 17:17:35 -07002312 /* APUTs have 3 sources and no targets */
2313 if (mir->ssaRep->numDefs == 0) {
2314 if (mir->ssaRep->numUses == 3) {
Bill Buzbeec6f10662010-02-09 11:16:15 -08002315 rlDest = dvmCompilerGetSrc(cUnit, mir, 0);
2316 rlSrc1 = dvmCompilerGetSrc(cUnit, mir, 1);
2317 rlSrc2 = dvmCompilerGetSrc(cUnit, mir, 2);
Bill Buzbee1465db52009-09-23 17:17:35 -07002318 } else {
2319 assert(mir->ssaRep->numUses == 4);
Bill Buzbeec6f10662010-02-09 11:16:15 -08002320 rlDest = dvmCompilerGetSrcWide(cUnit, mir, 0, 1);
2321 rlSrc1 = dvmCompilerGetSrc(cUnit, mir, 2);
2322 rlSrc2 = dvmCompilerGetSrc(cUnit, mir, 3);
Bill Buzbee1465db52009-09-23 17:17:35 -07002323 }
2324 } else {
2325 /* Two sources and 1 dest. Deduce the operand sizes */
2326 if (mir->ssaRep->numUses == 4) {
Bill Buzbeec6f10662010-02-09 11:16:15 -08002327 rlSrc1 = dvmCompilerGetSrcWide(cUnit, mir, 0, 1);
2328 rlSrc2 = dvmCompilerGetSrcWide(cUnit, mir, 2, 3);
Bill Buzbee1465db52009-09-23 17:17:35 -07002329 } else {
2330 assert(mir->ssaRep->numUses == 2);
Bill Buzbeec6f10662010-02-09 11:16:15 -08002331 rlSrc1 = dvmCompilerGetSrc(cUnit, mir, 0);
2332 rlSrc2 = dvmCompilerGetSrc(cUnit, mir, 1);
Bill Buzbee1465db52009-09-23 17:17:35 -07002333 }
2334 if (mir->ssaRep->numDefs == 2) {
Bill Buzbeec6f10662010-02-09 11:16:15 -08002335 rlDest = dvmCompilerGetDestWide(cUnit, mir, 0, 1);
Bill Buzbee1465db52009-09-23 17:17:35 -07002336 } else {
2337 assert(mir->ssaRep->numDefs == 1);
Bill Buzbeec6f10662010-02-09 11:16:15 -08002338 rlDest = dvmCompilerGetDest(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07002339 }
2340 }
2341
2342
Ben Chengba4fc8b2009-06-01 13:00:29 -07002343 switch (opCode) {
Bill Buzbeed45ba372009-06-15 17:00:57 -07002344 case OP_CMPL_FLOAT:
2345 case OP_CMPG_FLOAT:
2346 case OP_CMPL_DOUBLE:
2347 case OP_CMPG_DOUBLE:
Ben Cheng5d90c202009-11-22 23:31:11 -08002348 return genCmpFP(cUnit, mir, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002349 case OP_CMP_LONG:
Bill Buzbee1465db52009-09-23 17:17:35 -07002350 genCmpLong(cUnit, mir, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002351 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002352 case OP_AGET_WIDE:
Bill Buzbee1465db52009-09-23 17:17:35 -07002353 genArrayGet(cUnit, mir, kLong, rlSrc1, rlSrc2, rlDest, 3);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002354 break;
2355 case OP_AGET:
2356 case OP_AGET_OBJECT:
Bill Buzbee1465db52009-09-23 17:17:35 -07002357 genArrayGet(cUnit, mir, kWord, rlSrc1, rlSrc2, rlDest, 2);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002358 break;
2359 case OP_AGET_BOOLEAN:
Bill Buzbee1465db52009-09-23 17:17:35 -07002360 genArrayGet(cUnit, mir, kUnsignedByte, rlSrc1, rlSrc2, rlDest, 0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002361 break;
2362 case OP_AGET_BYTE:
Bill Buzbee1465db52009-09-23 17:17:35 -07002363 genArrayGet(cUnit, mir, kSignedByte, rlSrc1, rlSrc2, rlDest, 0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002364 break;
2365 case OP_AGET_CHAR:
Bill Buzbee1465db52009-09-23 17:17:35 -07002366 genArrayGet(cUnit, mir, kUnsignedHalf, rlSrc1, rlSrc2, rlDest, 1);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002367 break;
2368 case OP_AGET_SHORT:
Bill Buzbee1465db52009-09-23 17:17:35 -07002369 genArrayGet(cUnit, mir, kSignedHalf, rlSrc1, rlSrc2, rlDest, 1);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002370 break;
2371 case OP_APUT_WIDE:
Bill Buzbee1465db52009-09-23 17:17:35 -07002372 genArrayPut(cUnit, mir, kLong, rlSrc1, rlSrc2, rlDest, 3);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002373 break;
2374 case OP_APUT:
Bill Buzbee1465db52009-09-23 17:17:35 -07002375 genArrayPut(cUnit, mir, kWord, rlSrc1, rlSrc2, rlDest, 2);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002376 break;
Bill Buzbeebe6534f2010-03-12 16:01:35 -08002377 case OP_APUT_OBJECT:
2378 genArrayObjectPut(cUnit, mir, rlSrc1, rlSrc2, rlDest, 2);
2379 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002380 case OP_APUT_SHORT:
2381 case OP_APUT_CHAR:
Bill Buzbee1465db52009-09-23 17:17:35 -07002382 genArrayPut(cUnit, mir, kUnsignedHalf, rlSrc1, rlSrc2, rlDest, 1);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002383 break;
2384 case OP_APUT_BYTE:
2385 case OP_APUT_BOOLEAN:
Bill Buzbee1465db52009-09-23 17:17:35 -07002386 genArrayPut(cUnit, mir, kUnsignedByte, rlSrc1, rlSrc2, rlDest, 0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002387 break;
2388 default:
2389 return true;
2390 }
2391 return false;
2392}
2393
Ben Cheng6c10a972009-10-29 14:39:18 -07002394/*
2395 * Find the matching case.
2396 *
2397 * return values:
2398 * r0 (low 32-bit): pc of the chaining cell corresponding to the resolved case,
2399 * including default which is placed at MIN(size, MAX_CHAINED_SWITCH_CASES).
2400 * r1 (high 32-bit): the branch offset of the matching case (only for indexes
2401 * above MAX_CHAINED_SWITCH_CASES).
2402 *
2403 * Instructions around the call are:
2404 *
2405 * mov r2, pc
2406 * blx &findPackedSwitchIndex
2407 * mov pc, r0
2408 * .align4
2409 * chaining cell for case 0 [8 bytes]
2410 * chaining cell for case 1 [8 bytes]
2411 * :
2412 * chaining cell for case MIN(size, MAX_CHAINED_SWITCH_CASES)-1 [8 bytes]
2413 * chaining cell for case default [8 bytes]
2414 * noChain exit
2415 */
Ben Chengbd1326d2010-04-02 15:04:53 -07002416static s8 findPackedSwitchIndex(const u2* switchData, int testVal, int pc)
Ben Cheng6c10a972009-10-29 14:39:18 -07002417{
2418 int size;
2419 int firstKey;
2420 const int *entries;
2421 int index;
2422 int jumpIndex;
2423 int caseDPCOffset = 0;
2424 /* In Thumb mode pc is 4 ahead of the "mov r2, pc" instruction */
2425 int chainingPC = (pc + 4) & ~3;
2426
2427 /*
2428 * Packed switch data format:
2429 * ushort ident = 0x0100 magic value
2430 * ushort size number of entries in the table
2431 * int first_key first (and lowest) switch case value
2432 * int targets[size] branch targets, relative to switch opcode
2433 *
2434 * Total size is (4+size*2) 16-bit code units.
2435 */
2436 size = switchData[1];
2437 assert(size > 0);
2438
2439 firstKey = switchData[2];
2440 firstKey |= switchData[3] << 16;
2441
2442
2443 /* The entries are guaranteed to be aligned on a 32-bit boundary;
2444 * we can treat them as a native int array.
2445 */
2446 entries = (const int*) &switchData[4];
2447 assert(((u4)entries & 0x3) == 0);
2448
2449 index = testVal - firstKey;
2450
2451 /* Jump to the default cell */
2452 if (index < 0 || index >= size) {
2453 jumpIndex = MIN(size, MAX_CHAINED_SWITCH_CASES);
2454 /* Jump to the non-chaining exit point */
2455 } else if (index >= MAX_CHAINED_SWITCH_CASES) {
2456 jumpIndex = MAX_CHAINED_SWITCH_CASES + 1;
2457 caseDPCOffset = entries[index];
2458 /* Jump to the inline chaining cell */
2459 } else {
2460 jumpIndex = index;
2461 }
2462
2463 chainingPC += jumpIndex * 8;
2464 return (((s8) caseDPCOffset) << 32) | (u8) chainingPC;
2465}
2466
2467/* See comments for findPackedSwitchIndex */
Ben Chengbd1326d2010-04-02 15:04:53 -07002468static s8 findSparseSwitchIndex(const u2* switchData, int testVal, int pc)
Ben Cheng6c10a972009-10-29 14:39:18 -07002469{
2470 int size;
2471 const int *keys;
2472 const int *entries;
2473 int chainingPC = (pc + 4) & ~3;
2474 int i;
2475
2476 /*
2477 * Sparse switch data format:
2478 * ushort ident = 0x0200 magic value
2479 * ushort size number of entries in the table; > 0
2480 * int keys[size] keys, sorted low-to-high; 32-bit aligned
2481 * int targets[size] branch targets, relative to switch opcode
2482 *
2483 * Total size is (2+size*4) 16-bit code units.
2484 */
2485
2486 size = switchData[1];
2487 assert(size > 0);
2488
2489 /* The keys are guaranteed to be aligned on a 32-bit boundary;
2490 * we can treat them as a native int array.
2491 */
2492 keys = (const int*) &switchData[2];
2493 assert(((u4)keys & 0x3) == 0);
2494
2495 /* The entries are guaranteed to be aligned on a 32-bit boundary;
2496 * we can treat them as a native int array.
2497 */
2498 entries = keys + size;
2499 assert(((u4)entries & 0x3) == 0);
2500
2501 /*
2502 * Run through the list of keys, which are guaranteed to
2503 * be sorted low-to-high.
2504 *
2505 * Most tables have 3-4 entries. Few have more than 10. A binary
2506 * search here is probably not useful.
2507 */
2508 for (i = 0; i < size; i++) {
2509 int k = keys[i];
2510 if (k == testVal) {
2511 /* MAX_CHAINED_SWITCH_CASES + 1 is the start of the overflow case */
2512 int jumpIndex = (i < MAX_CHAINED_SWITCH_CASES) ?
2513 i : MAX_CHAINED_SWITCH_CASES + 1;
2514 chainingPC += jumpIndex * 8;
2515 return (((s8) entries[i]) << 32) | (u8) chainingPC;
2516 } else if (k > testVal) {
2517 break;
2518 }
2519 }
2520 return chainingPC + MIN(size, MAX_CHAINED_SWITCH_CASES) * 8;
2521}
2522
Ben Chengba4fc8b2009-06-01 13:00:29 -07002523static bool handleFmt31t(CompilationUnit *cUnit, MIR *mir)
2524{
2525 OpCode dalvikOpCode = mir->dalvikInsn.opCode;
2526 switch (dalvikOpCode) {
2527 case OP_FILL_ARRAY_DATA: {
Bill Buzbeec6f10662010-02-09 11:16:15 -08002528 RegLocation rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07002529 // Making a call - use explicit registers
Bill Buzbeec6f10662010-02-09 11:16:15 -08002530 dvmCompilerFlushAllRegs(cUnit); /* Everything to home location */
Bill Buzbee1465db52009-09-23 17:17:35 -07002531 genExportPC(cUnit, mir);
2532 loadValueDirectFixed(cUnit, rlSrc, r0);
Ben Chengbd1326d2010-04-02 15:04:53 -07002533 LOAD_FUNC_ADDR(cUnit, r2, (int)dvmInterpHandleFillArrayData);
Ben Cheng6c10a972009-10-29 14:39:18 -07002534 loadConstant(cUnit, r1,
2535 (int) (cUnit->method->insns + mir->offset + mir->dalvikInsn.vB));
Bill Buzbee1465db52009-09-23 17:17:35 -07002536 opReg(cUnit, kOpBlx, r2);
Elliott Hughes6a555132010-02-25 15:41:42 -08002537 dvmCompilerClobberCallRegs(cUnit);
Bill Buzbeefd023aa2009-11-02 09:23:49 -08002538 /* generate a branch over if successful */
2539 opRegImm(cUnit, kOpCmp, r0, 0); /* NULL? */
2540 ArmLIR *branchOver = opCondBranch(cUnit, kArmCondNe);
2541 loadConstant(cUnit, r0,
2542 (int) (cUnit->method->insns + mir->offset));
2543 genDispatchToHandler(cUnit, TEMPLATE_THROW_EXCEPTION_COMMON);
2544 ArmLIR *target = newLIR0(cUnit, kArmPseudoTargetLabel);
2545 target->defMask = ENCODE_ALL;
2546 branchOver->generic.target = (LIR *) target;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002547 break;
2548 }
2549 /*
Ben Cheng6c10a972009-10-29 14:39:18 -07002550 * Compute the goto target of up to
2551 * MIN(switchSize, MAX_CHAINED_SWITCH_CASES) + 1 chaining cells.
2552 * See the comment before findPackedSwitchIndex for the code layout.
Ben Chengba4fc8b2009-06-01 13:00:29 -07002553 */
2554 case OP_PACKED_SWITCH:
2555 case OP_SPARSE_SWITCH: {
Bill Buzbeec6f10662010-02-09 11:16:15 -08002556 RegLocation rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
2557 dvmCompilerFlushAllRegs(cUnit); /* Everything to home location */
Bill Buzbee1465db52009-09-23 17:17:35 -07002558 loadValueDirectFixed(cUnit, rlSrc, r1);
Bill Buzbeec6f10662010-02-09 11:16:15 -08002559 dvmCompilerLockAllTemps(cUnit);
Ben Cheng6c10a972009-10-29 14:39:18 -07002560 const u2 *switchData =
2561 cUnit->method->insns + mir->offset + mir->dalvikInsn.vB;
2562 u2 size = switchData[1];
2563
Ben Chengba4fc8b2009-06-01 13:00:29 -07002564 if (dalvikOpCode == OP_PACKED_SWITCH) {
Ben Chengbd1326d2010-04-02 15:04:53 -07002565 LOAD_FUNC_ADDR(cUnit, r4PC, (int)findPackedSwitchIndex);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002566 } else {
Ben Chengbd1326d2010-04-02 15:04:53 -07002567 LOAD_FUNC_ADDR(cUnit, r4PC, (int)findSparseSwitchIndex);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002568 }
Ben Cheng6c10a972009-10-29 14:39:18 -07002569 /* r0 <- Addr of the switch data */
2570 loadConstant(cUnit, r0,
2571 (int) (cUnit->method->insns + mir->offset + mir->dalvikInsn.vB));
2572 /* r2 <- pc of the instruction following the blx */
2573 opRegReg(cUnit, kOpMov, r2, rpc);
Bill Buzbee1465db52009-09-23 17:17:35 -07002574 opReg(cUnit, kOpBlx, r4PC);
Elliott Hughes6a555132010-02-25 15:41:42 -08002575 dvmCompilerClobberCallRegs(cUnit);
Ben Cheng6c10a972009-10-29 14:39:18 -07002576 /* pc <- computed goto target */
2577 opRegReg(cUnit, kOpMov, rpc, r0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002578 break;
2579 }
2580 default:
2581 return true;
2582 }
2583 return false;
2584}
2585
2586static bool handleFmt35c_3rc(CompilationUnit *cUnit, MIR *mir, BasicBlock *bb,
Bill Buzbee89efc3d2009-07-28 11:22:22 -07002587 ArmLIR *labelList)
Ben Chengba4fc8b2009-06-01 13:00:29 -07002588{
Bill Buzbee9bc3df32009-07-30 10:52:29 -07002589 ArmLIR *retChainingCell = NULL;
Bill Buzbee89efc3d2009-07-28 11:22:22 -07002590 ArmLIR *pcrLabel = NULL;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002591
Bill Buzbeef4ce16f2009-07-28 13:28:25 -07002592 if (bb->fallThrough != NULL)
2593 retChainingCell = &labelList[bb->fallThrough->id];
2594
Ben Chengba4fc8b2009-06-01 13:00:29 -07002595 DecodedInstruction *dInsn = &mir->dalvikInsn;
2596 switch (mir->dalvikInsn.opCode) {
2597 /*
2598 * calleeMethod = this->clazz->vtable[
2599 * method->clazz->pDvmDex->pResMethods[BBBB]->methodIndex
2600 * ]
2601 */
2602 case OP_INVOKE_VIRTUAL:
2603 case OP_INVOKE_VIRTUAL_RANGE: {
Bill Buzbee89efc3d2009-07-28 11:22:22 -07002604 ArmLIR *predChainingCell = &labelList[bb->taken->id];
Ben Chengba4fc8b2009-06-01 13:00:29 -07002605 int methodIndex =
2606 cUnit->method->clazz->pDvmDex->pResMethods[dInsn->vB]->
2607 methodIndex;
2608
2609 if (mir->dalvikInsn.opCode == OP_INVOKE_VIRTUAL)
2610 genProcessArgsNoRange(cUnit, mir, dInsn, &pcrLabel);
2611 else
2612 genProcessArgsRange(cUnit, mir, dInsn, &pcrLabel);
2613
Ben Cheng38329f52009-07-07 14:19:20 -07002614 genInvokeVirtualCommon(cUnit, mir, methodIndex,
2615 retChainingCell,
2616 predChainingCell,
2617 pcrLabel);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002618 break;
2619 }
2620 /*
2621 * calleeMethod = method->clazz->super->vtable[method->clazz->pDvmDex
2622 * ->pResMethods[BBBB]->methodIndex]
2623 */
Ben Chengba4fc8b2009-06-01 13:00:29 -07002624 case OP_INVOKE_SUPER:
2625 case OP_INVOKE_SUPER_RANGE: {
2626 int mIndex = cUnit->method->clazz->pDvmDex->
2627 pResMethods[dInsn->vB]->methodIndex;
2628 const Method *calleeMethod =
2629 cUnit->method->clazz->super->vtable[mIndex];
2630
2631 if (mir->dalvikInsn.opCode == OP_INVOKE_SUPER)
2632 genProcessArgsNoRange(cUnit, mir, dInsn, &pcrLabel);
2633 else
2634 genProcessArgsRange(cUnit, mir, dInsn, &pcrLabel);
2635
2636 /* r0 = calleeMethod */
2637 loadConstant(cUnit, r0, (int) calleeMethod);
2638
Ben Cheng38329f52009-07-07 14:19:20 -07002639 genInvokeSingletonCommon(cUnit, mir, bb, labelList, pcrLabel,
2640 calleeMethod);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002641 break;
2642 }
2643 /* calleeMethod = method->clazz->pDvmDex->pResMethods[BBBB] */
2644 case OP_INVOKE_DIRECT:
2645 case OP_INVOKE_DIRECT_RANGE: {
2646 const Method *calleeMethod =
2647 cUnit->method->clazz->pDvmDex->pResMethods[dInsn->vB];
2648
2649 if (mir->dalvikInsn.opCode == OP_INVOKE_DIRECT)
2650 genProcessArgsNoRange(cUnit, mir, dInsn, &pcrLabel);
2651 else
2652 genProcessArgsRange(cUnit, mir, dInsn, &pcrLabel);
2653
2654 /* r0 = calleeMethod */
2655 loadConstant(cUnit, r0, (int) calleeMethod);
2656
Ben Cheng38329f52009-07-07 14:19:20 -07002657 genInvokeSingletonCommon(cUnit, mir, bb, labelList, pcrLabel,
2658 calleeMethod);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002659 break;
2660 }
2661 /* calleeMethod = method->clazz->pDvmDex->pResMethods[BBBB] */
2662 case OP_INVOKE_STATIC:
2663 case OP_INVOKE_STATIC_RANGE: {
2664 const Method *calleeMethod =
2665 cUnit->method->clazz->pDvmDex->pResMethods[dInsn->vB];
2666
2667 if (mir->dalvikInsn.opCode == OP_INVOKE_STATIC)
2668 genProcessArgsNoRange(cUnit, mir, dInsn,
2669 NULL /* no null check */);
2670 else
2671 genProcessArgsRange(cUnit, mir, dInsn,
2672 NULL /* no null check */);
2673
2674 /* r0 = calleeMethod */
2675 loadConstant(cUnit, r0, (int) calleeMethod);
2676
Ben Cheng38329f52009-07-07 14:19:20 -07002677 genInvokeSingletonCommon(cUnit, mir, bb, labelList, pcrLabel,
2678 calleeMethod);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002679 break;
2680 }
Bill Buzbee1465db52009-09-23 17:17:35 -07002681 /*
Ben Chengba4fc8b2009-06-01 13:00:29 -07002682 * calleeMethod = dvmFindInterfaceMethodInCache(this->clazz,
2683 * BBBB, method, method->clazz->pDvmDex)
Ben Cheng38329f52009-07-07 14:19:20 -07002684 *
2685 * Given "invoke-interface {v0}", the following is the generated code:
2686 *
2687 * 0x426a9abe : ldr r0, [r5, #0] --+
2688 * 0x426a9ac0 : mov r7, r5 |
2689 * 0x426a9ac2 : sub r7, #24 |
2690 * 0x426a9ac4 : cmp r0, #0 | genProcessArgsNoRange
2691 * 0x426a9ac6 : beq 0x426a9afe |
2692 * 0x426a9ac8 : stmia r7, <r0> --+
2693 * 0x426a9aca : ldr r4, [pc, #104] --> r4 <- dalvikPC of this invoke
2694 * 0x426a9acc : add r1, pc, #52 --> r1 <- &retChainingCell
2695 * 0x426a9ace : add r2, pc, #60 --> r2 <- &predictedChainingCell
2696 * 0x426a9ad0 : blx_1 0x426a918c --+ TEMPLATE_INVOKE_METHOD_
2697 * 0x426a9ad2 : blx_2 see above --+ PREDICTED_CHAIN
2698 * 0x426a9ad4 : b 0x426a9b0c --> off to the predicted chain
2699 * 0x426a9ad6 : b 0x426a9afe --> punt to the interpreter
Ben Chenga8e64a72009-10-20 13:01:36 -07002700 * 0x426a9ad8 : mov r8, r1 --+
2701 * 0x426a9ada : mov r9, r2 |
2702 * 0x426a9adc : mov r10, r3 |
Ben Cheng38329f52009-07-07 14:19:20 -07002703 * 0x426a9ade : mov r0, r3 |
2704 * 0x426a9ae0 : mov r1, #74 | dvmFindInterfaceMethodInCache
2705 * 0x426a9ae2 : ldr r2, [pc, #76] |
2706 * 0x426a9ae4 : ldr r3, [pc, #68] |
2707 * 0x426a9ae6 : ldr r7, [pc, #64] |
2708 * 0x426a9ae8 : blx r7 --+
Ben Chenga8e64a72009-10-20 13:01:36 -07002709 * 0x426a9aea : mov r1, r8 --> r1 <- rechain count
Ben Cheng38329f52009-07-07 14:19:20 -07002710 * 0x426a9aec : cmp r1, #0 --> compare against 0
2711 * 0x426a9aee : bgt 0x426a9af8 --> >=0? don't rechain
2712 * 0x426a9af0 : ldr r7, [r6, #96] --+
Ben Chenga8e64a72009-10-20 13:01:36 -07002713 * 0x426a9af2 : mov r2, r9 | dvmJitToPatchPredictedChain
2714 * 0x426a9af4 : mov r3, r10 |
Ben Cheng38329f52009-07-07 14:19:20 -07002715 * 0x426a9af6 : blx r7 --+
2716 * 0x426a9af8 : add r1, pc, #8 --> r1 <- &retChainingCell
2717 * 0x426a9afa : blx_1 0x426a9098 --+ TEMPLATE_INVOKE_METHOD_NO_OPT
2718 * 0x426a9afc : blx_2 see above --+
2719 * -------- reconstruct dalvik PC : 0x428b786c @ +0x001e
2720 * 0x426a9afe (0042): ldr r0, [pc, #52]
2721 * Exception_Handling:
2722 * 0x426a9b00 (0044): ldr r1, [r6, #84]
2723 * 0x426a9b02 (0046): blx r1
2724 * 0x426a9b04 (0048): .align4
2725 * -------- chaining cell (hot): 0x0021
2726 * 0x426a9b04 (0048): ldr r0, [r6, #92]
2727 * 0x426a9b06 (004a): blx r0
2728 * 0x426a9b08 (004c): data 0x7872(30834)
2729 * 0x426a9b0a (004e): data 0x428b(17035)
2730 * 0x426a9b0c (0050): .align4
2731 * -------- chaining cell (predicted)
2732 * 0x426a9b0c (0050): data 0x0000(0) --> will be patched into bx
2733 * 0x426a9b0e (0052): data 0x0000(0)
2734 * 0x426a9b10 (0054): data 0x0000(0) --> class
2735 * 0x426a9b12 (0056): data 0x0000(0)
2736 * 0x426a9b14 (0058): data 0x0000(0) --> method
2737 * 0x426a9b16 (005a): data 0x0000(0)
2738 * 0x426a9b18 (005c): data 0x0000(0) --> reset count
2739 * 0x426a9b1a (005e): data 0x0000(0)
2740 * 0x426a9b28 (006c): .word (0xad0392a5)
2741 * 0x426a9b2c (0070): .word (0x6e750)
2742 * 0x426a9b30 (0074): .word (0x4109a618)
2743 * 0x426a9b34 (0078): .word (0x428b786c)
Ben Chengba4fc8b2009-06-01 13:00:29 -07002744 */
2745 case OP_INVOKE_INTERFACE:
2746 case OP_INVOKE_INTERFACE_RANGE: {
Bill Buzbee89efc3d2009-07-28 11:22:22 -07002747 ArmLIR *predChainingCell = &labelList[bb->taken->id];
Ben Chengba4fc8b2009-06-01 13:00:29 -07002748 int methodIndex = dInsn->vB;
2749
Bill Buzbee1465db52009-09-23 17:17:35 -07002750 /* Ensure that nothing is both live and dirty */
Bill Buzbeec6f10662010-02-09 11:16:15 -08002751 dvmCompilerFlushAllRegs(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07002752
Ben Chengba4fc8b2009-06-01 13:00:29 -07002753 if (mir->dalvikInsn.opCode == OP_INVOKE_INTERFACE)
2754 genProcessArgsNoRange(cUnit, mir, dInsn, &pcrLabel);
2755 else
2756 genProcessArgsRange(cUnit, mir, dInsn, &pcrLabel);
2757
Ben Cheng38329f52009-07-07 14:19:20 -07002758 /* "this" is already left in r0 by genProcessArgs* */
2759
2760 /* r4PC = dalvikCallsite */
2761 loadConstant(cUnit, r4PC,
2762 (int) (cUnit->method->insns + mir->offset));
2763
2764 /* r1 = &retChainingCell */
Bill Buzbee270c1d62009-08-13 16:58:07 -07002765 ArmLIR *addrRetChain =
Bill Buzbee1465db52009-09-23 17:17:35 -07002766 opRegRegImm(cUnit, kOpAdd, r1, rpc, 0);
Ben Cheng38329f52009-07-07 14:19:20 -07002767 addrRetChain->generic.target = (LIR *) retChainingCell;
2768
2769 /* r2 = &predictedChainingCell */
Bill Buzbee89efc3d2009-07-28 11:22:22 -07002770 ArmLIR *predictedChainingCell =
Bill Buzbee1465db52009-09-23 17:17:35 -07002771 opRegRegImm(cUnit, kOpAdd, r2, rpc, 0);
Ben Cheng38329f52009-07-07 14:19:20 -07002772 predictedChainingCell->generic.target = (LIR *) predChainingCell;
2773
2774 genDispatchToHandler(cUnit, TEMPLATE_INVOKE_METHOD_PREDICTED_CHAIN);
2775
2776 /* return through lr - jump to the chaining cell */
2777 genUnconditionalBranch(cUnit, predChainingCell);
2778
2779 /*
2780 * null-check on "this" may have been eliminated, but we still need
2781 * a PC-reconstruction label for stack overflow bailout.
2782 */
2783 if (pcrLabel == NULL) {
2784 int dPC = (int) (cUnit->method->insns + mir->offset);
Bill Buzbee89efc3d2009-07-28 11:22:22 -07002785 pcrLabel = dvmCompilerNew(sizeof(ArmLIR), true);
Ben Chenga4973592010-03-31 11:59:18 -07002786 pcrLabel->opCode = kArmPseudoPCReconstructionCell;
Ben Cheng38329f52009-07-07 14:19:20 -07002787 pcrLabel->operands[0] = dPC;
2788 pcrLabel->operands[1] = mir->offset;
2789 /* Insert the place holder to the growable list */
2790 dvmInsertGrowableList(&cUnit->pcReconstructionList, pcrLabel);
2791 }
2792
2793 /* return through lr+2 - punt to the interpreter */
2794 genUnconditionalBranch(cUnit, pcrLabel);
2795
2796 /*
2797 * return through lr+4 - fully resolve the callee method.
2798 * r1 <- count
2799 * r2 <- &predictedChainCell
2800 * r3 <- this->class
2801 * r4 <- dPC
2802 * r7 <- this->class->vtable
2803 */
2804
2805 /* Save count, &predictedChainCell, and class to high regs first */
Bill Buzbee1465db52009-09-23 17:17:35 -07002806 genRegCopy(cUnit, r8, r1);
2807 genRegCopy(cUnit, r9, r2);
2808 genRegCopy(cUnit, r10, r3);
Ben Cheng38329f52009-07-07 14:19:20 -07002809
Ben Chengba4fc8b2009-06-01 13:00:29 -07002810 /* r0 now contains this->clazz */
Bill Buzbee1465db52009-09-23 17:17:35 -07002811 genRegCopy(cUnit, r0, r3);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002812
2813 /* r1 = BBBB */
2814 loadConstant(cUnit, r1, dInsn->vB);
2815
2816 /* r2 = method (caller) */
2817 loadConstant(cUnit, r2, (int) cUnit->method);
2818
2819 /* r3 = pDvmDex */
2820 loadConstant(cUnit, r3, (int) cUnit->method->clazz->pDvmDex);
2821
Ben Chengbd1326d2010-04-02 15:04:53 -07002822 LOAD_FUNC_ADDR(cUnit, r7,
2823 (intptr_t) dvmFindInterfaceMethodInCache);
Bill Buzbee1465db52009-09-23 17:17:35 -07002824 opReg(cUnit, kOpBlx, r7);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002825
2826 /* r0 = calleeMethod (returned from dvmFindInterfaceMethodInCache */
2827
Bill Buzbee1465db52009-09-23 17:17:35 -07002828 genRegCopy(cUnit, r1, r8);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002829
Ben Cheng38329f52009-07-07 14:19:20 -07002830 /* Check if rechain limit is reached */
Bill Buzbee1465db52009-09-23 17:17:35 -07002831 opRegImm(cUnit, kOpCmp, r1, 0);
Ben Cheng38329f52009-07-07 14:19:20 -07002832
Bill Buzbee1465db52009-09-23 17:17:35 -07002833 ArmLIR *bypassRechaining = opCondBranch(cUnit, kArmCondGt);
Ben Cheng38329f52009-07-07 14:19:20 -07002834
Bill Buzbee270c1d62009-08-13 16:58:07 -07002835 loadWordDisp(cUnit, rGLUE, offsetof(InterpState,
2836 jitToInterpEntries.dvmJitToPatchPredictedChain), r7);
Ben Cheng38329f52009-07-07 14:19:20 -07002837
Bill Buzbee1465db52009-09-23 17:17:35 -07002838 genRegCopy(cUnit, r2, r9);
2839 genRegCopy(cUnit, r3, r10);
Ben Cheng38329f52009-07-07 14:19:20 -07002840
2841 /*
2842 * r0 = calleeMethod
2843 * r2 = &predictedChainingCell
2844 * r3 = class
2845 *
2846 * &returnChainingCell has been loaded into r1 but is not needed
2847 * when patching the chaining cell and will be clobbered upon
2848 * returning so it will be reconstructed again.
2849 */
Bill Buzbee1465db52009-09-23 17:17:35 -07002850 opReg(cUnit, kOpBlx, r7);
Ben Cheng38329f52009-07-07 14:19:20 -07002851
2852 /* r1 = &retChainingCell */
Bill Buzbee1465db52009-09-23 17:17:35 -07002853 addrRetChain = opRegRegImm(cUnit, kOpAdd, r1, rpc, 0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002854 addrRetChain->generic.target = (LIR *) retChainingCell;
Ben Cheng38329f52009-07-07 14:19:20 -07002855
2856 bypassRechaining->generic.target = (LIR *) addrRetChain;
2857
Ben Chengba4fc8b2009-06-01 13:00:29 -07002858 /*
2859 * r0 = this, r1 = calleeMethod,
2860 * r1 = &ChainingCell,
2861 * r4PC = callsiteDPC,
2862 */
2863 genDispatchToHandler(cUnit, TEMPLATE_INVOKE_METHOD_NO_OPT);
Ben Cheng86717f72010-03-05 15:27:21 -08002864#if defined(JIT_STATS)
2865 gDvmJit.invokePolymorphic++;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002866#endif
2867 /* Handle exceptions using the interpreter */
2868 genTrap(cUnit, mir->offset, pcrLabel);
2869 break;
2870 }
2871 /* NOP */
2872 case OP_INVOKE_DIRECT_EMPTY: {
2873 return false;
2874 }
2875 case OP_FILLED_NEW_ARRAY:
2876 case OP_FILLED_NEW_ARRAY_RANGE: {
2877 /* Just let the interpreter deal with these */
2878 genInterpSingleStep(cUnit, mir);
2879 break;
2880 }
2881 default:
2882 return true;
2883 }
2884 return false;
2885}
2886
2887static bool handleFmt35ms_3rms(CompilationUnit *cUnit, MIR *mir,
Bill Buzbee89efc3d2009-07-28 11:22:22 -07002888 BasicBlock *bb, ArmLIR *labelList)
Ben Chengba4fc8b2009-06-01 13:00:29 -07002889{
Bill Buzbee89efc3d2009-07-28 11:22:22 -07002890 ArmLIR *retChainingCell = &labelList[bb->fallThrough->id];
2891 ArmLIR *predChainingCell = &labelList[bb->taken->id];
2892 ArmLIR *pcrLabel = NULL;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002893
2894 DecodedInstruction *dInsn = &mir->dalvikInsn;
2895 switch (mir->dalvikInsn.opCode) {
2896 /* calleeMethod = this->clazz->vtable[BBBB] */
2897 case OP_INVOKE_VIRTUAL_QUICK_RANGE:
2898 case OP_INVOKE_VIRTUAL_QUICK: {
2899 int methodIndex = dInsn->vB;
2900 if (mir->dalvikInsn.opCode == OP_INVOKE_VIRTUAL_QUICK)
2901 genProcessArgsNoRange(cUnit, mir, dInsn, &pcrLabel);
2902 else
2903 genProcessArgsRange(cUnit, mir, dInsn, &pcrLabel);
2904
Ben Cheng38329f52009-07-07 14:19:20 -07002905 genInvokeVirtualCommon(cUnit, mir, methodIndex,
2906 retChainingCell,
2907 predChainingCell,
2908 pcrLabel);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002909 break;
2910 }
2911 /* calleeMethod = method->clazz->super->vtable[BBBB] */
2912 case OP_INVOKE_SUPER_QUICK:
2913 case OP_INVOKE_SUPER_QUICK_RANGE: {
2914 const Method *calleeMethod =
2915 cUnit->method->clazz->super->vtable[dInsn->vB];
2916
2917 if (mir->dalvikInsn.opCode == OP_INVOKE_SUPER_QUICK)
2918 genProcessArgsNoRange(cUnit, mir, dInsn, &pcrLabel);
2919 else
2920 genProcessArgsRange(cUnit, mir, dInsn, &pcrLabel);
2921
2922 /* r0 = calleeMethod */
2923 loadConstant(cUnit, r0, (int) calleeMethod);
2924
Ben Cheng38329f52009-07-07 14:19:20 -07002925 genInvokeSingletonCommon(cUnit, mir, bb, labelList, pcrLabel,
2926 calleeMethod);
2927 /* Handle exceptions using the interpreter */
2928 genTrap(cUnit, mir->offset, pcrLabel);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002929 break;
2930 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07002931 default:
2932 return true;
2933 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07002934 return false;
2935}
2936
2937/*
Bill Buzbeefd023aa2009-11-02 09:23:49 -08002938 * This operation is complex enough that we'll do it partly inline
2939 * and partly with a handler. NOTE: the handler uses hardcoded
2940 * values for string object offsets and must be revisitied if the
2941 * layout changes.
2942 */
2943static bool genInlinedCompareTo(CompilationUnit *cUnit, MIR *mir)
2944{
2945#if defined(USE_GLOBAL_STRING_DEFS)
2946 return false;
2947#else
2948 ArmLIR *rollback;
Bill Buzbeec6f10662010-02-09 11:16:15 -08002949 RegLocation rlThis = dvmCompilerGetSrc(cUnit, mir, 0);
2950 RegLocation rlComp = dvmCompilerGetSrc(cUnit, mir, 1);
Bill Buzbeefd023aa2009-11-02 09:23:49 -08002951
2952 loadValueDirectFixed(cUnit, rlThis, r0);
2953 loadValueDirectFixed(cUnit, rlComp, r1);
2954 /* Test objects for NULL */
2955 rollback = genNullCheck(cUnit, rlThis.sRegLow, r0, mir->offset, NULL);
2956 genNullCheck(cUnit, rlComp.sRegLow, r1, mir->offset, rollback);
2957 /*
2958 * TUNING: we could check for object pointer equality before invoking
2959 * handler. Unclear whether the gain would be worth the added code size
2960 * expansion.
2961 */
2962 genDispatchToHandler(cUnit, TEMPLATE_STRING_COMPARETO);
Bill Buzbeec6f10662010-02-09 11:16:15 -08002963 storeValue(cUnit, inlinedTarget(cUnit, mir, false),
2964 dvmCompilerGetReturn(cUnit));
Bill Buzbeefd023aa2009-11-02 09:23:49 -08002965 return true;
2966#endif
2967}
2968
Elliott Hughes2bdbcb62010-04-12 14:29:37 -07002969static bool genInlinedFastIndexOf(CompilationUnit *cUnit, MIR *mir)
Bill Buzbeefd023aa2009-11-02 09:23:49 -08002970{
2971#if defined(USE_GLOBAL_STRING_DEFS)
2972 return false;
2973#else
Bill Buzbeec6f10662010-02-09 11:16:15 -08002974 RegLocation rlThis = dvmCompilerGetSrc(cUnit, mir, 0);
2975 RegLocation rlChar = dvmCompilerGetSrc(cUnit, mir, 1);
Bill Buzbeefd023aa2009-11-02 09:23:49 -08002976
2977 loadValueDirectFixed(cUnit, rlThis, r0);
2978 loadValueDirectFixed(cUnit, rlChar, r1);
Elliott Hughes2bdbcb62010-04-12 14:29:37 -07002979 RegLocation rlStart = dvmCompilerGetSrc(cUnit, mir, 2);
2980 loadValueDirectFixed(cUnit, rlStart, r2);
Bill Buzbeefd023aa2009-11-02 09:23:49 -08002981 /* Test objects for NULL */
2982 genNullCheck(cUnit, rlThis.sRegLow, r0, mir->offset, NULL);
2983 genDispatchToHandler(cUnit, TEMPLATE_STRING_INDEXOF);
Bill Buzbeec6f10662010-02-09 11:16:15 -08002984 storeValue(cUnit, inlinedTarget(cUnit, mir, false),
2985 dvmCompilerGetReturn(cUnit));
Bill Buzbeefd023aa2009-11-02 09:23:49 -08002986 return true;
2987#endif
2988}
2989
Elliott Hughesee34f592010-04-05 18:13:52 -07002990// Generates an inlined String.isEmpty or String.length.
2991static bool genInlinedStringIsEmptyOrLength(CompilationUnit *cUnit, MIR *mir,
2992 bool isEmpty)
Bill Buzbee1f748632010-03-02 16:14:41 -08002993{
Elliott Hughesee34f592010-04-05 18:13:52 -07002994 // dst = src.length();
Bill Buzbee1f748632010-03-02 16:14:41 -08002995 RegLocation rlObj = dvmCompilerGetSrc(cUnit, mir, 0);
2996 RegLocation rlDest = inlinedTarget(cUnit, mir, false);
2997 rlObj = loadValue(cUnit, rlObj, kCoreReg);
2998 RegLocation rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
2999 genNullCheck(cUnit, rlObj.sRegLow, rlObj.lowReg, mir->offset, NULL);
3000 loadWordDisp(cUnit, rlObj.lowReg, gDvm.offJavaLangString_count,
3001 rlResult.lowReg);
Elliott Hughesee34f592010-04-05 18:13:52 -07003002 if (isEmpty) {
3003 // dst = (dst == 0);
3004 int tReg = dvmCompilerAllocTemp(cUnit);
3005 opRegReg(cUnit, kOpNeg, tReg, rlResult.lowReg);
3006 opRegRegReg(cUnit, kOpAdc, rlResult.lowReg, rlResult.lowReg, tReg);
3007 }
Bill Buzbee1f748632010-03-02 16:14:41 -08003008 storeValue(cUnit, rlDest, rlResult);
3009 return false;
3010}
3011
Elliott Hughesee34f592010-04-05 18:13:52 -07003012static bool genInlinedStringLength(CompilationUnit *cUnit, MIR *mir)
3013{
3014 return genInlinedStringIsEmptyOrLength(cUnit, mir, false);
3015}
3016
3017static bool genInlinedStringIsEmpty(CompilationUnit *cUnit, MIR *mir)
3018{
3019 return genInlinedStringIsEmptyOrLength(cUnit, mir, true);
3020}
3021
Bill Buzbee1f748632010-03-02 16:14:41 -08003022static bool genInlinedStringCharAt(CompilationUnit *cUnit, MIR *mir)
3023{
3024 int contents = offsetof(ArrayObject, contents);
3025 RegLocation rlObj = dvmCompilerGetSrc(cUnit, mir, 0);
3026 RegLocation rlIdx = dvmCompilerGetSrc(cUnit, mir, 1);
3027 RegLocation rlDest = inlinedTarget(cUnit, mir, false);
3028 RegLocation rlResult;
3029 rlObj = loadValue(cUnit, rlObj, kCoreReg);
3030 rlIdx = loadValue(cUnit, rlIdx, kCoreReg);
3031 int regMax = dvmCompilerAllocTemp(cUnit);
3032 int regOff = dvmCompilerAllocTemp(cUnit);
3033 int regPtr = dvmCompilerAllocTemp(cUnit);
3034 ArmLIR *pcrLabel = genNullCheck(cUnit, rlObj.sRegLow, rlObj.lowReg,
3035 mir->offset, NULL);
3036 loadWordDisp(cUnit, rlObj.lowReg, gDvm.offJavaLangString_count, regMax);
3037 loadWordDisp(cUnit, rlObj.lowReg, gDvm.offJavaLangString_offset, regOff);
3038 loadWordDisp(cUnit, rlObj.lowReg, gDvm.offJavaLangString_value, regPtr);
3039 genBoundsCheck(cUnit, rlIdx.lowReg, regMax, mir->offset, pcrLabel);
3040 dvmCompilerFreeTemp(cUnit, regMax);
3041 opRegImm(cUnit, kOpAdd, regPtr, contents);
3042 opRegReg(cUnit, kOpAdd, regOff, rlIdx.lowReg);
3043 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
3044 loadBaseIndexed(cUnit, regPtr, regOff, rlResult.lowReg, 1, kUnsignedHalf);
3045 storeValue(cUnit, rlDest, rlResult);
3046 return false;
3047}
3048
3049static bool genInlinedAbsInt(CompilationUnit *cUnit, MIR *mir)
3050{
3051 RegLocation rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
3052 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
3053 RegLocation rlDest = inlinedTarget(cUnit, mir, false);;
3054 RegLocation rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
3055 int signReg = dvmCompilerAllocTemp(cUnit);
3056 /*
3057 * abs(x) = y<=x>>31, (x+y)^y.
3058 * Thumb2's IT block also yields 3 instructions, but imposes
3059 * scheduling constraints.
3060 */
3061 opRegRegImm(cUnit, kOpAsr, signReg, rlSrc.lowReg, 31);
3062 opRegRegReg(cUnit, kOpAdd, rlResult.lowReg, rlSrc.lowReg, signReg);
3063 opRegReg(cUnit, kOpXor, rlResult.lowReg, signReg);
3064 storeValue(cUnit, rlDest, rlResult);
3065 return false;
3066}
3067
3068static bool genInlinedAbsLong(CompilationUnit *cUnit, MIR *mir)
3069{
3070 RegLocation rlSrc = dvmCompilerGetSrcWide(cUnit, mir, 0, 1);
3071 RegLocation rlDest = inlinedTargetWide(cUnit, mir, false);
3072 rlSrc = loadValueWide(cUnit, rlSrc, kCoreReg);
3073 RegLocation rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
3074 int signReg = dvmCompilerAllocTemp(cUnit);
3075 /*
3076 * abs(x) = y<=x>>31, (x+y)^y.
3077 * Thumb2 IT block allows slightly shorter sequence,
3078 * but introduces a scheduling barrier. Stick with this
3079 * mechanism for now.
3080 */
3081 opRegRegImm(cUnit, kOpAsr, signReg, rlSrc.highReg, 31);
3082 opRegRegReg(cUnit, kOpAdd, rlResult.lowReg, rlSrc.lowReg, signReg);
3083 opRegRegReg(cUnit, kOpAdc, rlResult.highReg, rlSrc.highReg, signReg);
3084 opRegReg(cUnit, kOpXor, rlResult.lowReg, signReg);
3085 opRegReg(cUnit, kOpXor, rlResult.highReg, signReg);
3086 storeValueWide(cUnit, rlDest, rlResult);
3087 return false;
3088}
Bill Buzbeefd023aa2009-11-02 09:23:49 -08003089
3090/*
Bill Buzbeece46c942009-11-20 15:41:34 -08003091 * NOTE: Handles both range and non-range versions (arguments
3092 * have already been normalized by this point).
Ben Chengba4fc8b2009-06-01 13:00:29 -07003093 */
Bill Buzbeece46c942009-11-20 15:41:34 -08003094static bool handleExecuteInline(CompilationUnit *cUnit, MIR *mir)
Ben Chengba4fc8b2009-06-01 13:00:29 -07003095{
3096 DecodedInstruction *dInsn = &mir->dalvikInsn;
3097 switch( mir->dalvikInsn.opCode) {
Bill Buzbeece46c942009-11-20 15:41:34 -08003098 case OP_EXECUTE_INLINE_RANGE:
Ben Chengba4fc8b2009-06-01 13:00:29 -07003099 case OP_EXECUTE_INLINE: {
3100 unsigned int i;
3101 const InlineOperation* inLineTable = dvmGetInlineOpsTable();
Bill Buzbee50a6bf22009-07-08 13:08:04 -07003102 int offset = offsetof(InterpState, retval);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003103 int operation = dInsn->vB;
Bill Buzbee1465db52009-09-23 17:17:35 -07003104 int tReg1;
3105 int tReg2;
Bill Buzbee50a6bf22009-07-08 13:08:04 -07003106 switch (operation) {
3107 case INLINE_EMPTYINLINEMETHOD:
3108 return false; /* Nop */
3109 case INLINE_STRING_LENGTH:
3110 return genInlinedStringLength(cUnit, mir);
Elliott Hughesee34f592010-04-05 18:13:52 -07003111 case INLINE_STRING_IS_EMPTY:
3112 return genInlinedStringIsEmpty(cUnit, mir);
Bill Buzbee50a6bf22009-07-08 13:08:04 -07003113 case INLINE_MATH_ABS_INT:
3114 return genInlinedAbsInt(cUnit, mir);
3115 case INLINE_MATH_ABS_LONG:
3116 return genInlinedAbsLong(cUnit, mir);
3117 case INLINE_MATH_MIN_INT:
3118 return genInlinedMinMaxInt(cUnit, mir, true);
3119 case INLINE_MATH_MAX_INT:
3120 return genInlinedMinMaxInt(cUnit, mir, false);
3121 case INLINE_STRING_CHARAT:
3122 return genInlinedStringCharAt(cUnit, mir);
3123 case INLINE_MATH_SQRT:
3124 if (genInlineSqrt(cUnit, mir))
Bill Buzbee9727c3d2009-08-01 11:32:36 -07003125 return false;
Bill Buzbee50a6bf22009-07-08 13:08:04 -07003126 else
3127 break; /* Handle with C routine */
Bill Buzbee50a6bf22009-07-08 13:08:04 -07003128 case INLINE_MATH_ABS_FLOAT:
Bill Buzbee1465db52009-09-23 17:17:35 -07003129 if (genInlinedAbsFloat(cUnit, mir))
3130 return false;
3131 else
3132 break;
Bill Buzbee50a6bf22009-07-08 13:08:04 -07003133 case INLINE_MATH_ABS_DOUBLE:
Bill Buzbee1465db52009-09-23 17:17:35 -07003134 if (genInlinedAbsDouble(cUnit, mir))
3135 return false;
3136 else
3137 break;
Bill Buzbee50a6bf22009-07-08 13:08:04 -07003138 case INLINE_STRING_COMPARETO:
Bill Buzbeefd023aa2009-11-02 09:23:49 -08003139 if (genInlinedCompareTo(cUnit, mir))
3140 return false;
3141 else
3142 break;
Elliott Hughes2bdbcb62010-04-12 14:29:37 -07003143 case INLINE_STRING_FASTINDEXOF_II:
3144 if (genInlinedFastIndexOf(cUnit, mir))
Bill Buzbeefd023aa2009-11-02 09:23:49 -08003145 return false;
3146 else
3147 break;
3148 case INLINE_STRING_EQUALS:
3149 case INLINE_MATH_COS:
3150 case INLINE_MATH_SIN:
3151 break; /* Handle with C routine */
Bill Buzbee50a6bf22009-07-08 13:08:04 -07003152 default:
Bill Buzbeefc519dc2010-03-06 23:30:57 -08003153 dvmCompilerAbort(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003154 }
Bill Buzbeec6f10662010-02-09 11:16:15 -08003155 dvmCompilerFlushAllRegs(cUnit); /* Everything to home location */
Elliott Hughes6a555132010-02-25 15:41:42 -08003156 dvmCompilerClobberCallRegs(cUnit);
Bill Buzbeec6f10662010-02-09 11:16:15 -08003157 dvmCompilerClobber(cUnit, r4PC);
3158 dvmCompilerClobber(cUnit, r7);
Bill Buzbee1465db52009-09-23 17:17:35 -07003159 opRegRegImm(cUnit, kOpAdd, r4PC, rGLUE, offset);
3160 opImm(cUnit, kOpPush, (1<<r4PC) | (1<<r7));
Ben Chengbd1326d2010-04-02 15:04:53 -07003161 LOAD_FUNC_ADDR(cUnit, r4PC, (int)inLineTable[operation].func);
Bill Buzbee1465db52009-09-23 17:17:35 -07003162 genExportPC(cUnit, mir);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003163 for (i=0; i < dInsn->vA; i++) {
Bill Buzbeec6f10662010-02-09 11:16:15 -08003164 loadValueDirect(cUnit, dvmCompilerGetSrc(cUnit, mir, i), i);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003165 }
Bill Buzbee1465db52009-09-23 17:17:35 -07003166 opReg(cUnit, kOpBlx, r4PC);
3167 opRegImm(cUnit, kOpAdd, r13, 8);
Bill Buzbeece46c942009-11-20 15:41:34 -08003168 opRegImm(cUnit, kOpCmp, r0, 0); /* NULL? */
3169 ArmLIR *branchOver = opCondBranch(cUnit, kArmCondNe);
3170 loadConstant(cUnit, r0,
3171 (int) (cUnit->method->insns + mir->offset));
3172 genDispatchToHandler(cUnit, TEMPLATE_THROW_EXCEPTION_COMMON);
3173 ArmLIR *target = newLIR0(cUnit, kArmPseudoTargetLabel);
3174 target->defMask = ENCODE_ALL;
3175 branchOver->generic.target = (LIR *) target;
Ben Chengba4fc8b2009-06-01 13:00:29 -07003176 break;
3177 }
3178 default:
3179 return true;
3180 }
3181 return false;
3182}
3183
3184static bool handleFmt51l(CompilationUnit *cUnit, MIR *mir)
3185{
Bill Buzbee1465db52009-09-23 17:17:35 -07003186 //TUNING: We're using core regs here - not optimal when target is a double
Bill Buzbeec6f10662010-02-09 11:16:15 -08003187 RegLocation rlDest = dvmCompilerGetDestWide(cUnit, mir, 0, 1);
3188 RegLocation rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Ben Chengbd1326d2010-04-02 15:04:53 -07003189 loadConstantNoClobber(cUnit, rlResult.lowReg,
3190 mir->dalvikInsn.vB_wide & 0xFFFFFFFFUL);
3191 loadConstantNoClobber(cUnit, rlResult.highReg,
3192 (mir->dalvikInsn.vB_wide>>32) & 0xFFFFFFFFUL);
Bill Buzbee1465db52009-09-23 17:17:35 -07003193 storeValueWide(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003194 return false;
3195}
3196
Ben Chengba4fc8b2009-06-01 13:00:29 -07003197/*
3198 * The following are special processing routines that handle transfer of
3199 * controls between compiled code and the interpreter. Certain VM states like
3200 * Dalvik PC and special-purpose registers are reconstructed here.
3201 */
3202
Ben Cheng1efc9c52009-06-08 18:25:27 -07003203/* Chaining cell for code that may need warmup. */
3204static void handleNormalChainingCell(CompilationUnit *cUnit,
3205 unsigned int offset)
Ben Chengba4fc8b2009-06-01 13:00:29 -07003206{
Ben Cheng11d8f142010-03-24 15:24:19 -07003207 /*
3208 * Use raw instruction constructors to guarantee that the generated
3209 * instructions fit the predefined cell size.
3210 */
3211 newLIR3(cUnit, kThumbLdrRRI5, r0, rGLUE,
3212 offsetof(InterpState,
3213 jitToInterpEntries.dvmJitToInterpNormal) >> 2);
3214 newLIR1(cUnit, kThumbBlxR, r0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003215 addWordData(cUnit, (int) (cUnit->method->insns + offset), true);
3216}
3217
3218/*
Ben Cheng1efc9c52009-06-08 18:25:27 -07003219 * Chaining cell for instructions that immediately following already translated
3220 * code.
Ben Chengba4fc8b2009-06-01 13:00:29 -07003221 */
Ben Cheng1efc9c52009-06-08 18:25:27 -07003222static void handleHotChainingCell(CompilationUnit *cUnit,
3223 unsigned int offset)
Ben Chengba4fc8b2009-06-01 13:00:29 -07003224{
Ben Cheng11d8f142010-03-24 15:24:19 -07003225 /*
3226 * Use raw instruction constructors to guarantee that the generated
3227 * instructions fit the predefined cell size.
3228 */
3229 newLIR3(cUnit, kThumbLdrRRI5, r0, rGLUE,
3230 offsetof(InterpState,
3231 jitToInterpEntries.dvmJitToInterpTraceSelect) >> 2);
3232 newLIR1(cUnit, kThumbBlxR, r0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003233 addWordData(cUnit, (int) (cUnit->method->insns + offset), true);
3234}
3235
Bill Buzbee9c4b7c82009-09-10 10:10:38 -07003236#if defined(WITH_SELF_VERIFICATION) || defined(WITH_JIT_TUNING)
Jeff Hao97319a82009-08-12 16:57:15 -07003237/* Chaining cell for branches that branch back into the same basic block */
3238static void handleBackwardBranchChainingCell(CompilationUnit *cUnit,
3239 unsigned int offset)
3240{
Ben Cheng11d8f142010-03-24 15:24:19 -07003241 /*
3242 * Use raw instruction constructors to guarantee that the generated
3243 * instructions fit the predefined cell size.
3244 */
Bill Buzbee9c4b7c82009-09-10 10:10:38 -07003245#if defined(WITH_SELF_VERIFICATION)
Bill Buzbee1465db52009-09-23 17:17:35 -07003246 newLIR3(cUnit, kThumbLdrRRI5, r0, rGLUE,
Ben Cheng40094c12010-02-24 20:58:44 -08003247 offsetof(InterpState,
3248 jitToInterpEntries.dvmJitToInterpBackwardBranch) >> 2);
Bill Buzbee9c4b7c82009-09-10 10:10:38 -07003249#else
Bill Buzbee1465db52009-09-23 17:17:35 -07003250 newLIR3(cUnit, kThumbLdrRRI5, r0, rGLUE,
Bill Buzbee9c4b7c82009-09-10 10:10:38 -07003251 offsetof(InterpState, jitToInterpEntries.dvmJitToInterpNormal) >> 2);
3252#endif
Bill Buzbee1465db52009-09-23 17:17:35 -07003253 newLIR1(cUnit, kThumbBlxR, r0);
Jeff Hao97319a82009-08-12 16:57:15 -07003254 addWordData(cUnit, (int) (cUnit->method->insns + offset), true);
3255}
3256
3257#endif
Ben Chengba4fc8b2009-06-01 13:00:29 -07003258/* Chaining cell for monomorphic method invocations. */
Ben Cheng38329f52009-07-07 14:19:20 -07003259static void handleInvokeSingletonChainingCell(CompilationUnit *cUnit,
3260 const Method *callee)
Ben Chengba4fc8b2009-06-01 13:00:29 -07003261{
Ben Cheng11d8f142010-03-24 15:24:19 -07003262 /*
3263 * Use raw instruction constructors to guarantee that the generated
3264 * instructions fit the predefined cell size.
3265 */
3266 newLIR3(cUnit, kThumbLdrRRI5, r0, rGLUE,
3267 offsetof(InterpState,
3268 jitToInterpEntries.dvmJitToInterpTraceSelect) >> 2);
3269 newLIR1(cUnit, kThumbBlxR, r0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003270 addWordData(cUnit, (int) (callee->insns), true);
3271}
3272
Ben Cheng38329f52009-07-07 14:19:20 -07003273/* Chaining cell for monomorphic method invocations. */
3274static void handleInvokePredictedChainingCell(CompilationUnit *cUnit)
3275{
3276
3277 /* Should not be executed in the initial state */
3278 addWordData(cUnit, PREDICTED_CHAIN_BX_PAIR_INIT, true);
3279 /* To be filled: class */
3280 addWordData(cUnit, PREDICTED_CHAIN_CLAZZ_INIT, true);
3281 /* To be filled: method */
3282 addWordData(cUnit, PREDICTED_CHAIN_METHOD_INIT, true);
3283 /*
3284 * Rechain count. The initial value of 0 here will trigger chaining upon
3285 * the first invocation of this callsite.
3286 */
3287 addWordData(cUnit, PREDICTED_CHAIN_COUNTER_INIT, true);
3288}
3289
Ben Chengba4fc8b2009-06-01 13:00:29 -07003290/* Load the Dalvik PC into r0 and jump to the specified target */
3291static void handlePCReconstruction(CompilationUnit *cUnit,
Bill Buzbee89efc3d2009-07-28 11:22:22 -07003292 ArmLIR *targetLabel)
Ben Chengba4fc8b2009-06-01 13:00:29 -07003293{
Bill Buzbee89efc3d2009-07-28 11:22:22 -07003294 ArmLIR **pcrLabel =
3295 (ArmLIR **) cUnit->pcReconstructionList.elemList;
Ben Chengba4fc8b2009-06-01 13:00:29 -07003296 int numElems = cUnit->pcReconstructionList.numUsed;
3297 int i;
3298 for (i = 0; i < numElems; i++) {
3299 dvmCompilerAppendLIR(cUnit, (LIR *) pcrLabel[i]);
3300 /* r0 = dalvik PC */
3301 loadConstant(cUnit, r0, pcrLabel[i]->operands[0]);
3302 genUnconditionalBranch(cUnit, targetLabel);
3303 }
3304}
3305
Bill Buzbee1465db52009-09-23 17:17:35 -07003306static char *extendedMIROpNames[kMirOpLast - kMirOpFirst] = {
3307 "kMirOpPhi",
3308 "kMirOpNullNRangeUpCheck",
3309 "kMirOpNullNRangeDownCheck",
3310 "kMirOpLowerBound",
3311 "kMirOpPunt",
Ben Cheng4238ec22009-08-24 16:32:22 -07003312};
3313
3314/*
3315 * vA = arrayReg;
3316 * vB = idxReg;
3317 * vC = endConditionReg;
3318 * arg[0] = maxC
3319 * arg[1] = minC
3320 * arg[2] = loopBranchConditionCode
3321 */
3322static void genHoistedChecksForCountUpLoop(CompilationUnit *cUnit, MIR *mir)
3323{
Bill Buzbee1465db52009-09-23 17:17:35 -07003324 /*
3325 * NOTE: these synthesized blocks don't have ssa names assigned
3326 * for Dalvik registers. However, because they dominate the following
3327 * blocks we can simply use the Dalvik name w/ subscript 0 as the
3328 * ssa name.
3329 */
Ben Cheng4238ec22009-08-24 16:32:22 -07003330 DecodedInstruction *dInsn = &mir->dalvikInsn;
3331 const int lenOffset = offsetof(ArrayObject, length);
Ben Cheng4238ec22009-08-24 16:32:22 -07003332 const int maxC = dInsn->arg[0];
3333 const int minC = dInsn->arg[1];
Bill Buzbee1465db52009-09-23 17:17:35 -07003334 int regLength;
3335 RegLocation rlArray = cUnit->regLocation[mir->dalvikInsn.vA];
3336 RegLocation rlIdxEnd = cUnit->regLocation[mir->dalvikInsn.vC];
Ben Cheng4238ec22009-08-24 16:32:22 -07003337
3338 /* regArray <- arrayRef */
Bill Buzbee1465db52009-09-23 17:17:35 -07003339 rlArray = loadValue(cUnit, rlArray, kCoreReg);
3340 rlIdxEnd = loadValue(cUnit, rlIdxEnd, kCoreReg);
3341 genRegImmCheck(cUnit, kArmCondEq, rlArray.lowReg, 0, 0,
Ben Cheng4238ec22009-08-24 16:32:22 -07003342 (ArmLIR *) cUnit->loopAnalysis->branchToPCR);
3343
3344 /* regLength <- len(arrayRef) */
Bill Buzbeec6f10662010-02-09 11:16:15 -08003345 regLength = dvmCompilerAllocTemp(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07003346 loadWordDisp(cUnit, rlArray.lowReg, lenOffset, regLength);
Ben Cheng4238ec22009-08-24 16:32:22 -07003347
3348 int delta = maxC;
3349 /*
3350 * If the loop end condition is ">=" instead of ">", then the largest value
3351 * of the index is "endCondition - 1".
3352 */
3353 if (dInsn->arg[2] == OP_IF_GE) {
3354 delta--;
3355 }
3356
3357 if (delta) {
Bill Buzbeec6f10662010-02-09 11:16:15 -08003358 int tReg = dvmCompilerAllocTemp(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07003359 opRegRegImm(cUnit, kOpAdd, tReg, rlIdxEnd.lowReg, delta);
3360 rlIdxEnd.lowReg = tReg;
Bill Buzbeec6f10662010-02-09 11:16:15 -08003361 dvmCompilerFreeTemp(cUnit, tReg);
Ben Cheng4238ec22009-08-24 16:32:22 -07003362 }
3363 /* Punt if "regIdxEnd < len(Array)" is false */
Bill Buzbee1465db52009-09-23 17:17:35 -07003364 genRegRegCheck(cUnit, kArmCondGe, rlIdxEnd.lowReg, regLength, 0,
Ben Cheng0fd31e42009-09-03 14:40:16 -07003365 (ArmLIR *) cUnit->loopAnalysis->branchToPCR);
Ben Cheng4238ec22009-08-24 16:32:22 -07003366}
3367
3368/*
3369 * vA = arrayReg;
3370 * vB = idxReg;
3371 * vC = endConditionReg;
3372 * arg[0] = maxC
3373 * arg[1] = minC
3374 * arg[2] = loopBranchConditionCode
3375 */
3376static void genHoistedChecksForCountDownLoop(CompilationUnit *cUnit, MIR *mir)
3377{
3378 DecodedInstruction *dInsn = &mir->dalvikInsn;
3379 const int lenOffset = offsetof(ArrayObject, length);
Bill Buzbeec6f10662010-02-09 11:16:15 -08003380 const int regLength = dvmCompilerAllocTemp(cUnit);
Ben Cheng4238ec22009-08-24 16:32:22 -07003381 const int maxC = dInsn->arg[0];
3382 const int minC = dInsn->arg[1];
Bill Buzbee1465db52009-09-23 17:17:35 -07003383 RegLocation rlArray = cUnit->regLocation[mir->dalvikInsn.vA];
3384 RegLocation rlIdxInit = cUnit->regLocation[mir->dalvikInsn.vB];
Ben Cheng4238ec22009-08-24 16:32:22 -07003385
3386 /* regArray <- arrayRef */
Bill Buzbee1465db52009-09-23 17:17:35 -07003387 rlArray = loadValue(cUnit, rlArray, kCoreReg);
3388 rlIdxInit = loadValue(cUnit, rlIdxInit, kCoreReg);
3389 genRegImmCheck(cUnit, kArmCondEq, rlArray.lowReg, 0, 0,
Ben Cheng4238ec22009-08-24 16:32:22 -07003390 (ArmLIR *) cUnit->loopAnalysis->branchToPCR);
3391
3392 /* regLength <- len(arrayRef) */
Bill Buzbee1465db52009-09-23 17:17:35 -07003393 loadWordDisp(cUnit, rlArray.lowReg, lenOffset, regLength);
Ben Cheng4238ec22009-08-24 16:32:22 -07003394
3395 if (maxC) {
Bill Buzbeec6f10662010-02-09 11:16:15 -08003396 int tReg = dvmCompilerAllocTemp(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07003397 opRegRegImm(cUnit, kOpAdd, tReg, rlIdxInit.lowReg, maxC);
3398 rlIdxInit.lowReg = tReg;
Bill Buzbeec6f10662010-02-09 11:16:15 -08003399 dvmCompilerFreeTemp(cUnit, tReg);
Ben Cheng4238ec22009-08-24 16:32:22 -07003400 }
3401
3402 /* Punt if "regIdxInit < len(Array)" is false */
Bill Buzbee1465db52009-09-23 17:17:35 -07003403 genRegRegCheck(cUnit, kArmCondGe, rlIdxInit.lowReg, regLength, 0,
Ben Cheng0fd31e42009-09-03 14:40:16 -07003404 (ArmLIR *) cUnit->loopAnalysis->branchToPCR);
Ben Cheng4238ec22009-08-24 16:32:22 -07003405}
3406
3407/*
3408 * vA = idxReg;
3409 * vB = minC;
3410 */
3411static void genHoistedLowerBoundCheck(CompilationUnit *cUnit, MIR *mir)
3412{
3413 DecodedInstruction *dInsn = &mir->dalvikInsn;
Ben Cheng4238ec22009-08-24 16:32:22 -07003414 const int minC = dInsn->vB;
Bill Buzbee1465db52009-09-23 17:17:35 -07003415 RegLocation rlIdx = cUnit->regLocation[mir->dalvikInsn.vA];
Ben Cheng4238ec22009-08-24 16:32:22 -07003416
3417 /* regIdx <- initial index value */
Bill Buzbee1465db52009-09-23 17:17:35 -07003418 rlIdx = loadValue(cUnit, rlIdx, kCoreReg);
Ben Cheng4238ec22009-08-24 16:32:22 -07003419
3420 /* Punt if "regIdxInit + minC >= 0" is false */
Bill Buzbee1465db52009-09-23 17:17:35 -07003421 genRegImmCheck(cUnit, kArmCondLt, rlIdx.lowReg, -minC, 0,
Ben Cheng4238ec22009-08-24 16:32:22 -07003422 (ArmLIR *) cUnit->loopAnalysis->branchToPCR);
3423}
3424
3425/* Extended MIR instructions like PHI */
3426static void handleExtendedMIR(CompilationUnit *cUnit, MIR *mir)
3427{
Bill Buzbee1465db52009-09-23 17:17:35 -07003428 int opOffset = mir->dalvikInsn.opCode - kMirOpFirst;
Ben Cheng4238ec22009-08-24 16:32:22 -07003429 char *msg = dvmCompilerNew(strlen(extendedMIROpNames[opOffset]) + 1,
3430 false);
3431 strcpy(msg, extendedMIROpNames[opOffset]);
Bill Buzbee1465db52009-09-23 17:17:35 -07003432 newLIR1(cUnit, kArmPseudoExtended, (int) msg);
Ben Cheng4238ec22009-08-24 16:32:22 -07003433
3434 switch (mir->dalvikInsn.opCode) {
Bill Buzbee1465db52009-09-23 17:17:35 -07003435 case kMirOpPhi: {
Ben Cheng4238ec22009-08-24 16:32:22 -07003436 char *ssaString = dvmCompilerGetSSAString(cUnit, mir->ssaRep);
Bill Buzbee1465db52009-09-23 17:17:35 -07003437 newLIR1(cUnit, kArmPseudoSSARep, (int) ssaString);
Ben Cheng4238ec22009-08-24 16:32:22 -07003438 break;
3439 }
Bill Buzbee1465db52009-09-23 17:17:35 -07003440 case kMirOpNullNRangeUpCheck: {
Ben Cheng4238ec22009-08-24 16:32:22 -07003441 genHoistedChecksForCountUpLoop(cUnit, mir);
3442 break;
3443 }
Bill Buzbee1465db52009-09-23 17:17:35 -07003444 case kMirOpNullNRangeDownCheck: {
Ben Cheng4238ec22009-08-24 16:32:22 -07003445 genHoistedChecksForCountDownLoop(cUnit, mir);
3446 break;
3447 }
Bill Buzbee1465db52009-09-23 17:17:35 -07003448 case kMirOpLowerBound: {
Ben Cheng4238ec22009-08-24 16:32:22 -07003449 genHoistedLowerBoundCheck(cUnit, mir);
3450 break;
3451 }
Bill Buzbee1465db52009-09-23 17:17:35 -07003452 case kMirOpPunt: {
Ben Cheng4238ec22009-08-24 16:32:22 -07003453 genUnconditionalBranch(cUnit,
3454 (ArmLIR *) cUnit->loopAnalysis->branchToPCR);
3455 break;
3456 }
3457 default:
3458 break;
3459 }
3460}
3461
3462/*
3463 * Create a PC-reconstruction cell for the starting offset of this trace.
3464 * Since the PCR cell is placed near the end of the compiled code which is
3465 * usually out of range for a conditional branch, we put two branches (one
3466 * branch over to the loop body and one layover branch to the actual PCR) at the
3467 * end of the entry block.
3468 */
3469static void setupLoopEntryBlock(CompilationUnit *cUnit, BasicBlock *entry,
3470 ArmLIR *bodyLabel)
3471{
3472 /* Set up the place holder to reconstruct this Dalvik PC */
3473 ArmLIR *pcrLabel = dvmCompilerNew(sizeof(ArmLIR), true);
Ben Chenga4973592010-03-31 11:59:18 -07003474 pcrLabel->opCode = kArmPseudoPCReconstructionCell;
Ben Cheng4238ec22009-08-24 16:32:22 -07003475 pcrLabel->operands[0] =
3476 (int) (cUnit->method->insns + entry->startOffset);
3477 pcrLabel->operands[1] = entry->startOffset;
3478 /* Insert the place holder to the growable list */
3479 dvmInsertGrowableList(&cUnit->pcReconstructionList, pcrLabel);
3480
3481 /*
3482 * Next, create two branches - one branch over to the loop body and the
3483 * other branch to the PCR cell to punt.
3484 */
3485 ArmLIR *branchToBody = dvmCompilerNew(sizeof(ArmLIR), true);
Bill Buzbee1465db52009-09-23 17:17:35 -07003486 branchToBody->opCode = kThumbBUncond;
Ben Cheng4238ec22009-08-24 16:32:22 -07003487 branchToBody->generic.target = (LIR *) bodyLabel;
Ben Chengdcf3e5d2009-09-11 13:42:05 -07003488 setupResourceMasks(branchToBody);
Ben Cheng4238ec22009-08-24 16:32:22 -07003489 cUnit->loopAnalysis->branchToBody = (LIR *) branchToBody;
3490
3491 ArmLIR *branchToPCR = dvmCompilerNew(sizeof(ArmLIR), true);
Bill Buzbee1465db52009-09-23 17:17:35 -07003492 branchToPCR->opCode = kThumbBUncond;
Ben Cheng4238ec22009-08-24 16:32:22 -07003493 branchToPCR->generic.target = (LIR *) pcrLabel;
Ben Chengdcf3e5d2009-09-11 13:42:05 -07003494 setupResourceMasks(branchToPCR);
Ben Cheng4238ec22009-08-24 16:32:22 -07003495 cUnit->loopAnalysis->branchToPCR = (LIR *) branchToPCR;
3496}
3497
Ben Chengd5adae12010-03-26 17:45:28 -07003498#if defined(WITH_SELF_VERIFICATION)
3499static bool selfVerificationPuntOps(MIR *mir)
3500{
3501 DecodedInstruction *decInsn = &mir->dalvikInsn;
3502 OpCode op = decInsn->opCode;
3503 int flags = dexGetInstrFlags(gDvm.instrFlags, op);
3504 /*
3505 * All opcodes that can throw exceptions and use the
3506 * TEMPLATE_THROW_EXCEPTION_COMMON template should be excluded in the trace
3507 * under self-verification mode.
3508 */
3509 return (op == OP_MONITOR_ENTER || op == OP_MONITOR_EXIT ||
3510 op == OP_NEW_INSTANCE || op == OP_NEW_ARRAY ||
3511 op == OP_CHECK_CAST || op == OP_MOVE_EXCEPTION ||
3512 op == OP_FILL_ARRAY_DATA || op == OP_EXECUTE_INLINE ||
3513 op == OP_EXECUTE_INLINE_RANGE ||
3514 (flags & kInstrInvoke));
3515}
3516#endif
3517
Ben Chengba4fc8b2009-06-01 13:00:29 -07003518void dvmCompilerMIR2LIR(CompilationUnit *cUnit)
3519{
3520 /* Used to hold the labels of each block */
Bill Buzbee89efc3d2009-07-28 11:22:22 -07003521 ArmLIR *labelList =
3522 dvmCompilerNew(sizeof(ArmLIR) * cUnit->numBlocks, true);
Ben Chengcec26f62010-01-15 15:29:33 -08003523 GrowableList chainingListByType[kChainingCellGap];
Ben Chengba4fc8b2009-06-01 13:00:29 -07003524 int i;
3525
3526 /*
Ben Cheng38329f52009-07-07 14:19:20 -07003527 * Initialize various types chaining lists.
Ben Chengba4fc8b2009-06-01 13:00:29 -07003528 */
Ben Chengcec26f62010-01-15 15:29:33 -08003529 for (i = 0; i < kChainingCellGap; i++) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07003530 dvmInitGrowableList(&chainingListByType[i], 2);
3531 }
3532
3533 BasicBlock **blockList = cUnit->blockList;
3534
Bill Buzbee6e963e12009-06-17 16:56:19 -07003535 if (cUnit->executionCount) {
3536 /*
3537 * Reserve 6 bytes at the beginning of the trace
3538 * +----------------------------+
3539 * | execution count (4 bytes) |
3540 * +----------------------------+
3541 * | chain cell offset (2 bytes)|
3542 * +----------------------------+
3543 * ...and then code to increment the execution
3544 * count:
3545 * mov r0, pc @ move adr of "mov r0,pc" + 4 to r0
3546 * sub r0, #10 @ back up to addr of executionCount
3547 * ldr r1, [r0]
3548 * add r1, #1
3549 * str r1, [r0]
3550 */
Bill Buzbee1465db52009-09-23 17:17:35 -07003551 newLIR1(cUnit, kArm16BitData, 0);
3552 newLIR1(cUnit, kArm16BitData, 0);
Ben Chengcc6600c2009-06-22 14:45:16 -07003553 cUnit->chainCellOffsetLIR =
Bill Buzbee1465db52009-09-23 17:17:35 -07003554 (LIR *) newLIR1(cUnit, kArm16BitData, CHAIN_CELL_OFFSET_TAG);
Bill Buzbee6e963e12009-06-17 16:56:19 -07003555 cUnit->headerSize = 6;
Bill Buzbee270c1d62009-08-13 16:58:07 -07003556 /* Thumb instruction used directly here to ensure correct size */
Bill Buzbee1465db52009-09-23 17:17:35 -07003557 newLIR2(cUnit, kThumbMovRR_H2L, r0, rpc);
3558 newLIR2(cUnit, kThumbSubRI8, r0, 10);
3559 newLIR3(cUnit, kThumbLdrRRI5, r1, r0, 0);
3560 newLIR2(cUnit, kThumbAddRI8, r1, 1);
3561 newLIR3(cUnit, kThumbStrRRI5, r1, r0, 0);
Bill Buzbee6e963e12009-06-17 16:56:19 -07003562 } else {
3563 /* Just reserve 2 bytes for the chain cell offset */
Ben Chengcc6600c2009-06-22 14:45:16 -07003564 cUnit->chainCellOffsetLIR =
Bill Buzbee1465db52009-09-23 17:17:35 -07003565 (LIR *) newLIR1(cUnit, kArm16BitData, CHAIN_CELL_OFFSET_TAG);
Bill Buzbee6e963e12009-06-17 16:56:19 -07003566 cUnit->headerSize = 2;
3567 }
Ben Cheng1efc9c52009-06-08 18:25:27 -07003568
Ben Chengba4fc8b2009-06-01 13:00:29 -07003569 /* Handle the content in each basic block */
3570 for (i = 0; i < cUnit->numBlocks; i++) {
3571 blockList[i]->visited = true;
3572 MIR *mir;
3573
3574 labelList[i].operands[0] = blockList[i]->startOffset;
3575
Ben Chengcec26f62010-01-15 15:29:33 -08003576 if (blockList[i]->blockType >= kChainingCellGap) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07003577 /*
3578 * Append the label pseudo LIR first. Chaining cells will be handled
3579 * separately afterwards.
3580 */
3581 dvmCompilerAppendLIR(cUnit, (LIR *) &labelList[i]);
3582 }
3583
Bill Buzbee1465db52009-09-23 17:17:35 -07003584 if (blockList[i]->blockType == kEntryBlock) {
Ben Chenga4973592010-03-31 11:59:18 -07003585 labelList[i].opCode = kArmPseudoEntryBlock;
Ben Cheng4238ec22009-08-24 16:32:22 -07003586 if (blockList[i]->firstMIRInsn == NULL) {
3587 continue;
3588 } else {
3589 setupLoopEntryBlock(cUnit, blockList[i],
3590 &labelList[blockList[i]->fallThrough->id]);
3591 }
Bill Buzbee1465db52009-09-23 17:17:35 -07003592 } else if (blockList[i]->blockType == kExitBlock) {
Ben Chenga4973592010-03-31 11:59:18 -07003593 labelList[i].opCode = kArmPseudoExitBlock;
Ben Cheng4238ec22009-08-24 16:32:22 -07003594 goto gen_fallthrough;
Bill Buzbee1465db52009-09-23 17:17:35 -07003595 } else if (blockList[i]->blockType == kDalvikByteCode) {
3596 labelList[i].opCode = kArmPseudoNormalBlockLabel;
Ben Chenge9695e52009-06-16 16:11:47 -07003597 /* Reset the register state */
Bill Buzbeec6f10662010-02-09 11:16:15 -08003598 dvmCompilerResetRegPool(cUnit);
3599 dvmCompilerClobberAllRegs(cUnit);
3600 dvmCompilerResetNullCheck(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003601 } else {
3602 switch (blockList[i]->blockType) {
Bill Buzbee1465db52009-09-23 17:17:35 -07003603 case kChainingCellNormal:
Ben Chenga4973592010-03-31 11:59:18 -07003604 labelList[i].opCode = kArmPseudoChainingCellNormal;
Ben Chengba4fc8b2009-06-01 13:00:29 -07003605 /* handle the codegen later */
3606 dvmInsertGrowableList(
Bill Buzbee1465db52009-09-23 17:17:35 -07003607 &chainingListByType[kChainingCellNormal], (void *) i);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003608 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07003609 case kChainingCellInvokeSingleton:
Ben Cheng38329f52009-07-07 14:19:20 -07003610 labelList[i].opCode =
Ben Chenga4973592010-03-31 11:59:18 -07003611 kArmPseudoChainingCellInvokeSingleton;
Ben Chengba4fc8b2009-06-01 13:00:29 -07003612 labelList[i].operands[0] =
3613 (int) blockList[i]->containingMethod;
3614 /* handle the codegen later */
3615 dvmInsertGrowableList(
Bill Buzbee1465db52009-09-23 17:17:35 -07003616 &chainingListByType[kChainingCellInvokeSingleton],
Ben Cheng38329f52009-07-07 14:19:20 -07003617 (void *) i);
3618 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07003619 case kChainingCellInvokePredicted:
Ben Cheng38329f52009-07-07 14:19:20 -07003620 labelList[i].opCode =
Ben Chenga4973592010-03-31 11:59:18 -07003621 kArmPseudoChainingCellInvokePredicted;
Ben Cheng38329f52009-07-07 14:19:20 -07003622 /* handle the codegen later */
3623 dvmInsertGrowableList(
Bill Buzbee1465db52009-09-23 17:17:35 -07003624 &chainingListByType[kChainingCellInvokePredicted],
Ben Cheng38329f52009-07-07 14:19:20 -07003625 (void *) i);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003626 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07003627 case kChainingCellHot:
Ben Chengba4fc8b2009-06-01 13:00:29 -07003628 labelList[i].opCode =
Ben Chenga4973592010-03-31 11:59:18 -07003629 kArmPseudoChainingCellHot;
Ben Chengba4fc8b2009-06-01 13:00:29 -07003630 /* handle the codegen later */
3631 dvmInsertGrowableList(
Bill Buzbee1465db52009-09-23 17:17:35 -07003632 &chainingListByType[kChainingCellHot],
Ben Chengba4fc8b2009-06-01 13:00:29 -07003633 (void *) i);
3634 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07003635 case kPCReconstruction:
Ben Chengba4fc8b2009-06-01 13:00:29 -07003636 /* Make sure exception handling block is next */
3637 labelList[i].opCode =
Ben Chenga4973592010-03-31 11:59:18 -07003638 kArmPseudoPCReconstructionBlockLabel;
Ben Chengba4fc8b2009-06-01 13:00:29 -07003639 assert (i == cUnit->numBlocks - 2);
3640 handlePCReconstruction(cUnit, &labelList[i+1]);
3641 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07003642 case kExceptionHandling:
3643 labelList[i].opCode = kArmPseudoEHBlockLabel;
Ben Chengba4fc8b2009-06-01 13:00:29 -07003644 if (cUnit->pcReconstructionList.numUsed) {
Bill Buzbee270c1d62009-08-13 16:58:07 -07003645 loadWordDisp(cUnit, rGLUE, offsetof(InterpState,
3646 jitToInterpEntries.dvmJitToInterpPunt),
3647 r1);
Bill Buzbee1465db52009-09-23 17:17:35 -07003648 opReg(cUnit, kOpBlx, r1);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003649 }
3650 break;
Bill Buzbee9c4b7c82009-09-10 10:10:38 -07003651#if defined(WITH_SELF_VERIFICATION) || defined(WITH_JIT_TUNING)
Bill Buzbee1465db52009-09-23 17:17:35 -07003652 case kChainingCellBackwardBranch:
Jeff Hao97319a82009-08-12 16:57:15 -07003653 labelList[i].opCode =
Ben Chenga4973592010-03-31 11:59:18 -07003654 kArmPseudoChainingCellBackwardBranch;
Jeff Hao97319a82009-08-12 16:57:15 -07003655 /* handle the codegen later */
3656 dvmInsertGrowableList(
Bill Buzbee1465db52009-09-23 17:17:35 -07003657 &chainingListByType[kChainingCellBackwardBranch],
Jeff Hao97319a82009-08-12 16:57:15 -07003658 (void *) i);
3659 break;
3660#endif
Ben Chengba4fc8b2009-06-01 13:00:29 -07003661 default:
3662 break;
3663 }
3664 continue;
3665 }
Ben Chenge9695e52009-06-16 16:11:47 -07003666
Bill Buzbee89efc3d2009-07-28 11:22:22 -07003667 ArmLIR *headLIR = NULL;
Ben Chenge9695e52009-06-16 16:11:47 -07003668
Ben Chengba4fc8b2009-06-01 13:00:29 -07003669 for (mir = blockList[i]->firstMIRInsn; mir; mir = mir->next) {
Bill Buzbee1465db52009-09-23 17:17:35 -07003670
Bill Buzbeec6f10662010-02-09 11:16:15 -08003671 dvmCompilerResetRegPool(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07003672 if (gDvmJit.disableOpt & (1 << kTrackLiveTemps)) {
Bill Buzbeec6f10662010-02-09 11:16:15 -08003673 dvmCompilerClobberAllRegs(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07003674 }
3675
3676 if (gDvmJit.disableOpt & (1 << kSuppressLoads)) {
Bill Buzbeec6f10662010-02-09 11:16:15 -08003677 dvmCompilerResetDefTracking(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07003678 }
3679
3680 if (mir->dalvikInsn.opCode >= kMirOpFirst) {
Ben Cheng4238ec22009-08-24 16:32:22 -07003681 handleExtendedMIR(cUnit, mir);
3682 continue;
3683 }
3684
Bill Buzbee1465db52009-09-23 17:17:35 -07003685
Ben Chengba4fc8b2009-06-01 13:00:29 -07003686 OpCode dalvikOpCode = mir->dalvikInsn.opCode;
3687 InstructionFormat dalvikFormat =
3688 dexGetInstrFormat(gDvm.instrFormat, dalvikOpCode);
Bill Buzbee89efc3d2009-07-28 11:22:22 -07003689 ArmLIR *boundaryLIR =
Ben Chenga4973592010-03-31 11:59:18 -07003690 newLIR2(cUnit, kArmPseudoDalvikByteCodeBoundary,
Ben Chengccd6c012009-10-15 14:52:45 -07003691 mir->offset,
3692 (int) dvmCompilerGetDalvikDisassembly(&mir->dalvikInsn)
3693 );
Ben Cheng4238ec22009-08-24 16:32:22 -07003694 if (mir->ssaRep) {
3695 char *ssaString = dvmCompilerGetSSAString(cUnit, mir->ssaRep);
Bill Buzbee1465db52009-09-23 17:17:35 -07003696 newLIR1(cUnit, kArmPseudoSSARep, (int) ssaString);
Ben Cheng4238ec22009-08-24 16:32:22 -07003697 }
3698
Ben Chenge9695e52009-06-16 16:11:47 -07003699 /* Remember the first LIR for this block */
3700 if (headLIR == NULL) {
3701 headLIR = boundaryLIR;
Ben Chengd7d426a2009-09-22 11:23:36 -07003702 /* Set the first boundaryLIR as a scheduling barrier */
3703 headLIR->defMask = ENCODE_ALL;
Ben Chenge9695e52009-06-16 16:11:47 -07003704 }
Ben Cheng4238ec22009-08-24 16:32:22 -07003705
Ben Chengba4fc8b2009-06-01 13:00:29 -07003706 bool notHandled;
3707 /*
3708 * Debugging: screen the opcode first to see if it is in the
3709 * do[-not]-compile list
3710 */
3711 bool singleStepMe =
3712 gDvmJit.includeSelectedOp !=
3713 ((gDvmJit.opList[dalvikOpCode >> 3] &
3714 (1 << (dalvikOpCode & 0x7))) !=
3715 0);
Ben Chengd5adae12010-03-26 17:45:28 -07003716#if defined(WITH_SELF_VERIFICATION)
3717 if (singleStepMe == false) {
3718 singleStepMe = selfVerificationPuntOps(mir);
3719 }
3720#endif
Ben Chengba4fc8b2009-06-01 13:00:29 -07003721 if (singleStepMe || cUnit->allSingleStep) {
3722 notHandled = false;
3723 genInterpSingleStep(cUnit, mir);
3724 } else {
3725 opcodeCoverage[dalvikOpCode]++;
3726 switch (dalvikFormat) {
3727 case kFmt10t:
3728 case kFmt20t:
3729 case kFmt30t:
3730 notHandled = handleFmt10t_Fmt20t_Fmt30t(cUnit,
3731 mir, blockList[i], labelList);
3732 break;
3733 case kFmt10x:
3734 notHandled = handleFmt10x(cUnit, mir);
3735 break;
3736 case kFmt11n:
3737 case kFmt31i:
3738 notHandled = handleFmt11n_Fmt31i(cUnit, mir);
3739 break;
3740 case kFmt11x:
3741 notHandled = handleFmt11x(cUnit, mir);
3742 break;
3743 case kFmt12x:
3744 notHandled = handleFmt12x(cUnit, mir);
3745 break;
3746 case kFmt20bc:
3747 notHandled = handleFmt20bc(cUnit, mir);
3748 break;
3749 case kFmt21c:
3750 case kFmt31c:
3751 notHandled = handleFmt21c_Fmt31c(cUnit, mir);
3752 break;
3753 case kFmt21h:
3754 notHandled = handleFmt21h(cUnit, mir);
3755 break;
3756 case kFmt21s:
3757 notHandled = handleFmt21s(cUnit, mir);
3758 break;
3759 case kFmt21t:
3760 notHandled = handleFmt21t(cUnit, mir, blockList[i],
3761 labelList);
3762 break;
3763 case kFmt22b:
3764 case kFmt22s:
3765 notHandled = handleFmt22b_Fmt22s(cUnit, mir);
3766 break;
3767 case kFmt22c:
3768 notHandled = handleFmt22c(cUnit, mir);
3769 break;
3770 case kFmt22cs:
3771 notHandled = handleFmt22cs(cUnit, mir);
3772 break;
3773 case kFmt22t:
3774 notHandled = handleFmt22t(cUnit, mir, blockList[i],
3775 labelList);
3776 break;
3777 case kFmt22x:
3778 case kFmt32x:
3779 notHandled = handleFmt22x_Fmt32x(cUnit, mir);
3780 break;
3781 case kFmt23x:
3782 notHandled = handleFmt23x(cUnit, mir);
3783 break;
3784 case kFmt31t:
3785 notHandled = handleFmt31t(cUnit, mir);
3786 break;
3787 case kFmt3rc:
3788 case kFmt35c:
3789 notHandled = handleFmt35c_3rc(cUnit, mir, blockList[i],
3790 labelList);
3791 break;
3792 case kFmt3rms:
3793 case kFmt35ms:
3794 notHandled = handleFmt35ms_3rms(cUnit, mir,blockList[i],
3795 labelList);
3796 break;
3797 case kFmt3inline:
Andy McFaddenb0a05412009-11-19 10:23:41 -08003798 case kFmt3rinline:
Bill Buzbeece46c942009-11-20 15:41:34 -08003799 notHandled = handleExecuteInline(cUnit, mir);
Andy McFaddenb0a05412009-11-19 10:23:41 -08003800 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07003801 case kFmt51l:
3802 notHandled = handleFmt51l(cUnit, mir);
3803 break;
3804 default:
3805 notHandled = true;
3806 break;
3807 }
3808 }
3809 if (notHandled) {
3810 LOGE("%#06x: Opcode 0x%x (%s) / Fmt %d not handled\n",
3811 mir->offset,
3812 dalvikOpCode, getOpcodeName(dalvikOpCode),
3813 dalvikFormat);
Bill Buzbeefc519dc2010-03-06 23:30:57 -08003814 dvmCompilerAbort(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003815 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07003816 }
3817 }
Ben Cheng4238ec22009-08-24 16:32:22 -07003818
Bill Buzbee1465db52009-09-23 17:17:35 -07003819 if (blockList[i]->blockType == kEntryBlock) {
Ben Cheng4238ec22009-08-24 16:32:22 -07003820 dvmCompilerAppendLIR(cUnit,
3821 (LIR *) cUnit->loopAnalysis->branchToBody);
3822 dvmCompilerAppendLIR(cUnit,
3823 (LIR *) cUnit->loopAnalysis->branchToPCR);
3824 }
3825
3826 if (headLIR) {
3827 /*
3828 * Eliminate redundant loads/stores and delay stores into later
3829 * slots
3830 */
3831 dvmCompilerApplyLocalOptimizations(cUnit, (LIR *) headLIR,
3832 cUnit->lastLIRInsn);
3833 }
3834
3835gen_fallthrough:
Ben Cheng1efc9c52009-06-08 18:25:27 -07003836 /*
3837 * Check if the block is terminated due to trace length constraint -
3838 * insert an unconditional branch to the chaining cell.
3839 */
3840 if (blockList[i]->needFallThroughBranch) {
3841 genUnconditionalBranch(cUnit,
3842 &labelList[blockList[i]->fallThrough->id]);
3843 }
3844
Ben Chengba4fc8b2009-06-01 13:00:29 -07003845 }
3846
Ben Chenge9695e52009-06-16 16:11:47 -07003847 /* Handle the chaining cells in predefined order */
Ben Chengcec26f62010-01-15 15:29:33 -08003848 for (i = 0; i < kChainingCellGap; i++) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07003849 size_t j;
3850 int *blockIdList = (int *) chainingListByType[i].elemList;
3851
3852 cUnit->numChainingCells[i] = chainingListByType[i].numUsed;
3853
3854 /* No chaining cells of this type */
3855 if (cUnit->numChainingCells[i] == 0)
3856 continue;
3857
3858 /* Record the first LIR for a new type of chaining cell */
3859 cUnit->firstChainingLIR[i] = (LIR *) &labelList[blockIdList[0]];
3860
3861 for (j = 0; j < chainingListByType[i].numUsed; j++) {
3862 int blockId = blockIdList[j];
3863
3864 /* Align this chaining cell first */
Bill Buzbee1465db52009-09-23 17:17:35 -07003865 newLIR0(cUnit, kArmPseudoPseudoAlign4);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003866
3867 /* Insert the pseudo chaining instruction */
3868 dvmCompilerAppendLIR(cUnit, (LIR *) &labelList[blockId]);
3869
3870
3871 switch (blockList[blockId]->blockType) {
Bill Buzbee1465db52009-09-23 17:17:35 -07003872 case kChainingCellNormal:
Ben Cheng1efc9c52009-06-08 18:25:27 -07003873 handleNormalChainingCell(cUnit,
Ben Chengba4fc8b2009-06-01 13:00:29 -07003874 blockList[blockId]->startOffset);
3875 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07003876 case kChainingCellInvokeSingleton:
Ben Cheng38329f52009-07-07 14:19:20 -07003877 handleInvokeSingletonChainingCell(cUnit,
Ben Chengba4fc8b2009-06-01 13:00:29 -07003878 blockList[blockId]->containingMethod);
3879 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07003880 case kChainingCellInvokePredicted:
Ben Cheng38329f52009-07-07 14:19:20 -07003881 handleInvokePredictedChainingCell(cUnit);
3882 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07003883 case kChainingCellHot:
Ben Cheng1efc9c52009-06-08 18:25:27 -07003884 handleHotChainingCell(cUnit,
Ben Chengba4fc8b2009-06-01 13:00:29 -07003885 blockList[blockId]->startOffset);
3886 break;
Bill Buzbee9c4b7c82009-09-10 10:10:38 -07003887#if defined(WITH_SELF_VERIFICATION) || defined(WITH_JIT_TUNING)
Bill Buzbee1465db52009-09-23 17:17:35 -07003888 case kChainingCellBackwardBranch:
Jeff Hao97319a82009-08-12 16:57:15 -07003889 handleBackwardBranchChainingCell(cUnit,
3890 blockList[blockId]->startOffset);
3891 break;
3892#endif
Ben Chengba4fc8b2009-06-01 13:00:29 -07003893 default:
Bill Buzbee1465db52009-09-23 17:17:35 -07003894 LOGE("Bad blocktype %d", blockList[blockId]->blockType);
Bill Buzbeefc519dc2010-03-06 23:30:57 -08003895 dvmCompilerAbort(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003896 }
3897 }
3898 }
Ben Chenge9695e52009-06-16 16:11:47 -07003899
Ben Chengcec26f62010-01-15 15:29:33 -08003900 /* Mark the bottom of chaining cells */
3901 cUnit->chainingCellBottom = (LIR *) newLIR0(cUnit, kArmChainingCellBottom);
3902
Ben Cheng6c10a972009-10-29 14:39:18 -07003903 /*
3904 * Generate the branch to the dvmJitToInterpNoChain entry point at the end
3905 * of all chaining cells for the overflow cases.
3906 */
3907 if (cUnit->switchOverflowPad) {
3908 loadConstant(cUnit, r0, (int) cUnit->switchOverflowPad);
3909 loadWordDisp(cUnit, rGLUE, offsetof(InterpState,
3910 jitToInterpEntries.dvmJitToInterpNoChain), r2);
3911 opRegReg(cUnit, kOpAdd, r1, r1);
3912 opRegRegReg(cUnit, kOpAdd, r4PC, r0, r1);
Ben Cheng86717f72010-03-05 15:27:21 -08003913#if defined(JIT_STATS)
Ben Cheng6c10a972009-10-29 14:39:18 -07003914 loadConstant(cUnit, r0, kSwitchOverflow);
3915#endif
3916 opReg(cUnit, kOpBlx, r2);
3917 }
3918
Ben Chenge9695e52009-06-16 16:11:47 -07003919 dvmCompilerApplyGlobalOptimizations(cUnit);
jeffhao9e45c0b2010-02-03 10:24:05 -08003920
3921#if defined(WITH_SELF_VERIFICATION)
3922 selfVerificationBranchInsertPass(cUnit);
3923#endif
Ben Chengba4fc8b2009-06-01 13:00:29 -07003924}
3925
3926/* Accept the work and start compiling */
Bill Buzbee716f1202009-07-23 13:22:09 -07003927bool dvmCompilerDoWork(CompilerWorkOrder *work)
Ben Chengba4fc8b2009-06-01 13:00:29 -07003928{
Ben Chengccd6c012009-10-15 14:52:45 -07003929 bool res;
Ben Chengba4fc8b2009-06-01 13:00:29 -07003930
Ben Cheng6999d842010-01-26 16:46:15 -08003931 if (gDvmJit.codeCacheFull) {
Ben Chengccd6c012009-10-15 14:52:45 -07003932 return false;
3933 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07003934
Ben Chengccd6c012009-10-15 14:52:45 -07003935 switch (work->kind) {
3936 case kWorkOrderMethod:
3937 res = dvmCompileMethod(work->info, &work->result);
3938 break;
3939 case kWorkOrderTrace:
3940 /* Start compilation with maximally allowed trace length */
Bill Buzbeefc519dc2010-03-06 23:30:57 -08003941 res = dvmCompileTrace(work->info, JIT_MAX_TRACE_LEN, &work->result,
3942 work->bailPtr);
Ben Chengccd6c012009-10-15 14:52:45 -07003943 break;
3944 case kWorkOrderTraceDebug: {
3945 bool oldPrintMe = gDvmJit.printMe;
3946 gDvmJit.printMe = true;
3947 /* Start compilation with maximally allowed trace length */
Bill Buzbeefc519dc2010-03-06 23:30:57 -08003948 res = dvmCompileTrace(work->info, JIT_MAX_TRACE_LEN, &work->result,
3949 work->bailPtr);
Ben Chengccd6c012009-10-15 14:52:45 -07003950 gDvmJit.printMe = oldPrintMe;;
3951 break;
3952 }
3953 default:
3954 res = false;
Bill Buzbeefc519dc2010-03-06 23:30:57 -08003955 LOGE("Jit: unknown work order type");
3956 assert(0); // Bail if debug build, discard oteherwise
Ben Chengccd6c012009-10-15 14:52:45 -07003957 }
3958 return res;
Ben Chengba4fc8b2009-06-01 13:00:29 -07003959}
3960
Ben Chengba4fc8b2009-06-01 13:00:29 -07003961/* Architectural-specific debugging helpers go here */
3962void dvmCompilerArchDump(void)
3963{
3964 /* Print compiled opcode in this VM instance */
3965 int i, start, streak;
3966 char buf[1024];
3967
3968 streak = i = 0;
3969 buf[0] = 0;
3970 while (opcodeCoverage[i] == 0 && i < 256) {
3971 i++;
3972 }
3973 if (i == 256) {
3974 return;
3975 }
3976 for (start = i++, streak = 1; i < 256; i++) {
3977 if (opcodeCoverage[i]) {
3978 streak++;
3979 } else {
3980 if (streak == 1) {
3981 sprintf(buf+strlen(buf), "%x,", start);
3982 } else {
3983 sprintf(buf+strlen(buf), "%x-%x,", start, start + streak - 1);
3984 }
3985 streak = 0;
3986 while (opcodeCoverage[i] == 0 && i < 256) {
3987 i++;
3988 }
3989 if (i < 256) {
3990 streak = 1;
3991 start = i;
3992 }
3993 }
3994 }
3995 if (streak) {
3996 if (streak == 1) {
3997 sprintf(buf+strlen(buf), "%x", start);
3998 } else {
3999 sprintf(buf+strlen(buf), "%x-%x", start, start + streak - 1);
4000 }
4001 }
4002 if (strlen(buf)) {
Ben Cheng8b258bf2009-06-24 17:27:07 -07004003 LOGD("dalvik.vm.jit.op = %s", buf);
Ben Chengba4fc8b2009-06-01 13:00:29 -07004004 }
4005}
Ben Chengd7d426a2009-09-22 11:23:36 -07004006
4007/* Common initialization routine for an architecture family */
4008bool dvmCompilerArchInit()
4009{
4010 int i;
4011
Bill Buzbee1465db52009-09-23 17:17:35 -07004012 for (i = 0; i < kArmLast; i++) {
Ben Chengd7d426a2009-09-22 11:23:36 -07004013 if (EncodingMap[i].opCode != i) {
4014 LOGE("Encoding order for %s is wrong: expecting %d, seeing %d",
4015 EncodingMap[i].name, i, EncodingMap[i].opCode);
Bill Buzbeefc519dc2010-03-06 23:30:57 -08004016 dvmAbort(); // OK to dvmAbort - build error
Ben Chengd7d426a2009-09-22 11:23:36 -07004017 }
4018 }
4019
Ben Cheng5d90c202009-11-22 23:31:11 -08004020 return dvmCompilerArchVariantInit();
4021}
4022
4023void *dvmCompilerGetInterpretTemplate()
4024{
4025 return (void*) ((int)gDvmJit.codeCache +
4026 templateEntryOffsets[TEMPLATE_INTERPRET]);
4027}
4028
4029/* Needed by the ld/st optmizatons */
4030ArmLIR* dvmCompilerRegCopyNoInsert(CompilationUnit *cUnit, int rDest, int rSrc)
4031{
4032 return genRegCopyNoInsert(cUnit, rDest, rSrc);
4033}
4034
4035/* Needed by the register allocator */
4036ArmLIR* dvmCompilerRegCopy(CompilationUnit *cUnit, int rDest, int rSrc)
4037{
4038 return genRegCopy(cUnit, rDest, rSrc);
4039}
4040
4041/* Needed by the register allocator */
4042void dvmCompilerRegCopyWide(CompilationUnit *cUnit, int destLo, int destHi,
4043 int srcLo, int srcHi)
4044{
4045 genRegCopyWide(cUnit, destLo, destHi, srcLo, srcHi);
4046}
4047
4048void dvmCompilerFlushRegImpl(CompilationUnit *cUnit, int rBase,
4049 int displacement, int rSrc, OpSize size)
4050{
4051 storeBaseDisp(cUnit, rBase, displacement, rSrc, size);
4052}
4053
4054void dvmCompilerFlushRegWideImpl(CompilationUnit *cUnit, int rBase,
4055 int displacement, int rSrcLo, int rSrcHi)
4056{
4057 storeBaseDispWide(cUnit, rBase, displacement, rSrcLo, rSrcHi);
Ben Chengd7d426a2009-09-22 11:23:36 -07004058}