blob: ef7de282264f0c87cc58833aba6eddac2fbd1c60 [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
Elliott Hughes672511b2010-04-26 17:40:13 -07001901// Returns true if it added instructions to 'cUnit' to divide 'rlSrc' by 'lit'
1902// and store the result in 'rlDest'.
1903static bool handleEasyDivide(CompilationUnit *cUnit,
1904 RegLocation rlSrc, RegLocation rlDest, int lit)
1905{
1906 if (lit < 2 || !isPowerOfTwo(lit)) {
1907 return false;
1908 }
1909 int k = lowestSetBit(lit);
1910 if (k >= 30) {
1911 // Avoid special cases.
1912 return false;
1913 }
1914 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
1915 RegLocation rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
1916 int tReg = dvmCompilerAllocTemp(cUnit);
1917 if (lit == 2) {
1918 // Division by 2 is by far the most common division by constant.
1919 opRegRegImm(cUnit, kOpLsr, tReg, rlSrc.lowReg, 32 - k);
1920 opRegRegReg(cUnit, kOpAdd, tReg, tReg, rlSrc.lowReg);
1921 opRegRegImm(cUnit, kOpAsr, rlResult.lowReg, tReg, k);
1922 } else {
1923 opRegRegImm(cUnit, kOpAsr, tReg, rlSrc.lowReg, 31);
1924 opRegRegImm(cUnit, kOpLsr, tReg, tReg, 32 - k);
1925 opRegRegReg(cUnit, kOpAdd, tReg, tReg, rlSrc.lowReg);
1926 opRegRegImm(cUnit, kOpAsr, rlResult.lowReg, tReg, k);
1927 }
1928 storeValue(cUnit, rlDest, rlResult);
1929 return true;
1930}
1931
Elliott Hughesb4c05972010-02-24 16:36:18 -08001932// Returns true if it added instructions to 'cUnit' to multiply 'rlSrc' by 'lit'
1933// and store the result in 'rlDest'.
1934static bool handleEasyMultiply(CompilationUnit *cUnit,
1935 RegLocation rlSrc, RegLocation rlDest, int lit)
1936{
1937 // Can we simplify this multiplication?
1938 bool powerOfTwo = false;
1939 bool popCountLE2 = false;
1940 bool powerOfTwoMinusOne = false;
1941 if (lit < 2) {
1942 // Avoid special cases.
1943 return false;
1944 } else if (isPowerOfTwo(lit)) {
1945 powerOfTwo = true;
1946 } else if (isPopCountLE2(lit)) {
1947 popCountLE2 = true;
1948 } else if (isPowerOfTwo(lit + 1)) {
1949 powerOfTwoMinusOne = true;
1950 } else {
1951 return false;
1952 }
1953 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
1954 RegLocation rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
1955 if (powerOfTwo) {
1956 // Shift.
1957 opRegRegImm(cUnit, kOpLsl, rlResult.lowReg, rlSrc.lowReg,
1958 lowestSetBit(lit));
1959 } else if (popCountLE2) {
1960 // Shift and add and shift.
1961 int firstBit = lowestSetBit(lit);
1962 int secondBit = lowestSetBit(lit ^ (1 << firstBit));
1963 genMultiplyByTwoBitMultiplier(cUnit, rlSrc, rlResult, lit,
1964 firstBit, secondBit);
1965 } else {
1966 // Reverse subtract: (src << (shift + 1)) - src.
1967 assert(powerOfTwoMinusOne);
1968 // TODO: rsb dst, src, src lsl#lowestSetBit(lit + 1)
1969 int tReg = dvmCompilerAllocTemp(cUnit);
1970 opRegRegImm(cUnit, kOpLsl, tReg, rlSrc.lowReg, lowestSetBit(lit + 1));
1971 opRegRegReg(cUnit, kOpSub, rlResult.lowReg, tReg, rlSrc.lowReg);
1972 }
1973 storeValue(cUnit, rlDest, rlResult);
1974 return true;
Bill Buzbee78cb0e22010-02-11 14:04:53 -08001975}
1976
Ben Chengba4fc8b2009-06-01 13:00:29 -07001977static bool handleFmt22b_Fmt22s(CompilationUnit *cUnit, MIR *mir)
1978{
1979 OpCode dalvikOpCode = mir->dalvikInsn.opCode;
Bill Buzbeec6f10662010-02-09 11:16:15 -08001980 RegLocation rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
1981 RegLocation rlDest = dvmCompilerGetDest(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07001982 RegLocation rlResult;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001983 int lit = mir->dalvikInsn.vC;
Ben Cheng4f489172009-09-27 17:08:35 -07001984 OpKind op = 0; /* Make gcc happy */
Bill Buzbee1465db52009-09-23 17:17:35 -07001985 int shiftOp = false;
1986 bool isDiv = false;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001987
Ben Chengba4fc8b2009-06-01 13:00:29 -07001988 switch (dalvikOpCode) {
Bill Buzbee1465db52009-09-23 17:17:35 -07001989 case OP_RSUB_INT_LIT8:
1990 case OP_RSUB_INT: {
1991 int tReg;
1992 //TUNING: add support for use of Arm rsub op
1993 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
Bill Buzbeec6f10662010-02-09 11:16:15 -08001994 tReg = dvmCompilerAllocTemp(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07001995 loadConstant(cUnit, tReg, lit);
Bill Buzbeec6f10662010-02-09 11:16:15 -08001996 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Bill Buzbee1465db52009-09-23 17:17:35 -07001997 opRegRegReg(cUnit, kOpSub, rlResult.lowReg,
1998 tReg, rlSrc.lowReg);
1999 storeValue(cUnit, rlDest, rlResult);
2000 return false;
2001 break;
2002 }
2003
Ben Chengba4fc8b2009-06-01 13:00:29 -07002004 case OP_ADD_INT_LIT8:
2005 case OP_ADD_INT_LIT16:
Bill Buzbee1465db52009-09-23 17:17:35 -07002006 op = kOpAdd;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002007 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002008 case OP_MUL_INT_LIT8:
Bill Buzbee78cb0e22010-02-11 14:04:53 -08002009 case OP_MUL_INT_LIT16: {
Elliott Hughesb4c05972010-02-24 16:36:18 -08002010 if (handleEasyMultiply(cUnit, rlSrc, rlDest, lit)) {
2011 return false;
Bill Buzbee78cb0e22010-02-11 14:04:53 -08002012 }
Elliott Hughesb4c05972010-02-24 16:36:18 -08002013 op = kOpMul;
Bill Buzbee1465db52009-09-23 17:17:35 -07002014 break;
Bill Buzbee78cb0e22010-02-11 14:04:53 -08002015 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07002016 case OP_AND_INT_LIT8:
2017 case OP_AND_INT_LIT16:
Bill Buzbee1465db52009-09-23 17:17:35 -07002018 op = kOpAnd;
2019 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002020 case OP_OR_INT_LIT8:
2021 case OP_OR_INT_LIT16:
Bill Buzbee1465db52009-09-23 17:17:35 -07002022 op = kOpOr;
2023 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002024 case OP_XOR_INT_LIT8:
2025 case OP_XOR_INT_LIT16:
Bill Buzbee1465db52009-09-23 17:17:35 -07002026 op = kOpXor;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002027 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002028 case OP_SHL_INT_LIT8:
Bill Buzbee0e605272009-12-01 14:28:05 -08002029 lit &= 31;
Bill Buzbee1465db52009-09-23 17:17:35 -07002030 shiftOp = true;
2031 op = kOpLsl;
2032 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002033 case OP_SHR_INT_LIT8:
Bill Buzbee0e605272009-12-01 14:28:05 -08002034 lit &= 31;
Bill Buzbee1465db52009-09-23 17:17:35 -07002035 shiftOp = true;
2036 op = kOpAsr;
2037 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002038 case OP_USHR_INT_LIT8:
Bill Buzbee0e605272009-12-01 14:28:05 -08002039 lit &= 31;
Bill Buzbee1465db52009-09-23 17:17:35 -07002040 shiftOp = true;
2041 op = kOpLsr;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002042 break;
2043
2044 case OP_DIV_INT_LIT8:
2045 case OP_DIV_INT_LIT16:
Ben Chengba4fc8b2009-06-01 13:00:29 -07002046 case OP_REM_INT_LIT8:
2047 case OP_REM_INT_LIT16:
2048 if (lit == 0) {
2049 /* Let the interpreter deal with div by 0 */
2050 genInterpSingleStep(cUnit, mir);
2051 return false;
2052 }
Elliott Hughes672511b2010-04-26 17:40:13 -07002053 if (handleEasyDivide(cUnit, rlSrc, rlDest, lit)) {
2054 return false;
2055 }
Bill Buzbeec6f10662010-02-09 11:16:15 -08002056 dvmCompilerFlushAllRegs(cUnit); /* Everything to home location */
Bill Buzbee1465db52009-09-23 17:17:35 -07002057 loadValueDirectFixed(cUnit, rlSrc, r0);
Bill Buzbeec6f10662010-02-09 11:16:15 -08002058 dvmCompilerClobber(cUnit, r0);
Bill Buzbee1465db52009-09-23 17:17:35 -07002059 if ((dalvikOpCode == OP_DIV_INT_LIT8) ||
2060 (dalvikOpCode == OP_DIV_INT_LIT16)) {
Ben Chengbd1326d2010-04-02 15:04:53 -07002061 LOAD_FUNC_ADDR(cUnit, r2, (int)__aeabi_idiv);
Bill Buzbee1465db52009-09-23 17:17:35 -07002062 isDiv = true;
2063 } else {
Ben Chengbd1326d2010-04-02 15:04:53 -07002064 LOAD_FUNC_ADDR(cUnit, r2, (int)__aeabi_idivmod);
Bill Buzbee1465db52009-09-23 17:17:35 -07002065 isDiv = false;
2066 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07002067 loadConstant(cUnit, r1, lit);
Bill Buzbee1465db52009-09-23 17:17:35 -07002068 opReg(cUnit, kOpBlx, r2);
Elliott Hughes6a555132010-02-25 15:41:42 -08002069 dvmCompilerClobberCallRegs(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07002070 if (isDiv)
Bill Buzbeec6f10662010-02-09 11:16:15 -08002071 rlResult = dvmCompilerGetReturn(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07002072 else
Bill Buzbeec6f10662010-02-09 11:16:15 -08002073 rlResult = dvmCompilerGetReturnAlt(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07002074 storeValue(cUnit, rlDest, rlResult);
2075 return false;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002076 break;
2077 default:
2078 return true;
2079 }
Bill Buzbee1465db52009-09-23 17:17:35 -07002080 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
Bill Buzbeec6f10662010-02-09 11:16:15 -08002081 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Bill Buzbee1465db52009-09-23 17:17:35 -07002082 // Avoid shifts by literal 0 - no support in Thumb. Change to copy
2083 if (shiftOp && (lit == 0)) {
2084 genRegCopy(cUnit, rlResult.lowReg, rlSrc.lowReg);
2085 } else {
2086 opRegRegImm(cUnit, op, rlResult.lowReg, rlSrc.lowReg, lit);
2087 }
2088 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002089 return false;
2090}
2091
2092static bool handleFmt22c(CompilationUnit *cUnit, MIR *mir)
2093{
2094 OpCode dalvikOpCode = mir->dalvikInsn.opCode;
2095 int fieldOffset;
2096
2097 if (dalvikOpCode >= OP_IGET && dalvikOpCode <= OP_IPUT_SHORT) {
2098 InstField *pInstField = (InstField *)
2099 cUnit->method->clazz->pDvmDex->pResFields[mir->dalvikInsn.vC];
Ben Chengba4fc8b2009-06-01 13:00:29 -07002100
2101 assert(pInstField != NULL);
2102 fieldOffset = pInstField->byteOffset;
2103 } else {
Ben Chenga0e7b602009-10-13 23:09:01 -07002104 /* Deliberately break the code while make the compiler happy */
2105 fieldOffset = -1;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002106 }
2107 switch (dalvikOpCode) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07002108 case OP_NEW_ARRAY: {
Bill Buzbee1465db52009-09-23 17:17:35 -07002109 // Generates a call - use explicit registers
Bill Buzbeec6f10662010-02-09 11:16:15 -08002110 RegLocation rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
2111 RegLocation rlDest = dvmCompilerGetDest(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07002112 RegLocation rlResult;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002113 void *classPtr = (void*)
2114 (cUnit->method->clazz->pDvmDex->pResClasses[mir->dalvikInsn.vC]);
2115 assert(classPtr != NULL);
Bill Buzbeec6f10662010-02-09 11:16:15 -08002116 dvmCompilerFlushAllRegs(cUnit); /* Everything to home location */
Bill Buzbee1465db52009-09-23 17:17:35 -07002117 genExportPC(cUnit, mir);
2118 loadValueDirectFixed(cUnit, rlSrc, r1); /* Len */
Ben Chengba4fc8b2009-06-01 13:00:29 -07002119 loadConstant(cUnit, r0, (int) classPtr );
Ben Chengbd1326d2010-04-02 15:04:53 -07002120 LOAD_FUNC_ADDR(cUnit, r3, (int)dvmAllocArrayByClass);
Ben Cheng4f489172009-09-27 17:08:35 -07002121 /*
2122 * "len < 0": bail to the interpreter to re-execute the
2123 * instruction
2124 */
Bill Buzbee89efc3d2009-07-28 11:22:22 -07002125 ArmLIR *pcrLabel =
Bill Buzbee1465db52009-09-23 17:17:35 -07002126 genRegImmCheck(cUnit, kArmCondMi, r1, 0, mir->offset, NULL);
Bill Buzbee270c1d62009-08-13 16:58:07 -07002127 loadConstant(cUnit, r2, ALLOC_DONT_TRACK);
Bill Buzbee1465db52009-09-23 17:17:35 -07002128 opReg(cUnit, kOpBlx, r3);
Elliott Hughes6a555132010-02-25 15:41:42 -08002129 dvmCompilerClobberCallRegs(cUnit);
Ben Cheng4f489172009-09-27 17:08:35 -07002130 /* generate a branch over if allocation is successful */
Bill Buzbee1465db52009-09-23 17:17:35 -07002131 opRegImm(cUnit, kOpCmp, r0, 0); /* NULL? */
2132 ArmLIR *branchOver = opCondBranch(cUnit, kArmCondNe);
Ben Cheng4f489172009-09-27 17:08:35 -07002133 /*
2134 * OOM exception needs to be thrown here and cannot re-execute
2135 */
2136 loadConstant(cUnit, r0,
2137 (int) (cUnit->method->insns + mir->offset));
2138 genDispatchToHandler(cUnit, TEMPLATE_THROW_EXCEPTION_COMMON);
2139 /* noreturn */
2140
Bill Buzbee1465db52009-09-23 17:17:35 -07002141 ArmLIR *target = newLIR0(cUnit, kArmPseudoTargetLabel);
Ben Cheng4f489172009-09-27 17:08:35 -07002142 target->defMask = ENCODE_ALL;
2143 branchOver->generic.target = (LIR *) target;
Bill Buzbeec6f10662010-02-09 11:16:15 -08002144 rlResult = dvmCompilerGetReturn(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07002145 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002146 break;
2147 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07002148 case OP_INSTANCE_OF: {
Bill Buzbee1465db52009-09-23 17:17:35 -07002149 // May generate a call - use explicit registers
Bill Buzbeec6f10662010-02-09 11:16:15 -08002150 RegLocation rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
2151 RegLocation rlDest = dvmCompilerGetDest(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07002152 RegLocation rlResult;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002153 ClassObject *classPtr =
2154 (cUnit->method->clazz->pDvmDex->pResClasses[mir->dalvikInsn.vC]);
Bill Buzbee480e6782010-01-27 15:43:08 -08002155 /*
2156 * Note: It is possible that classPtr is NULL at this point,
2157 * even though this instruction has been successfully interpreted.
2158 * If the previous interpretation had a null source, the
2159 * interpreter would not have bothered to resolve the clazz.
2160 * Bail out to the interpreter in this case, and log it
2161 * so that we can tell if it happens frequently.
2162 */
2163 if (classPtr == NULL) {
2164 LOGD("null clazz in OP_INSTANCE_OF, single-stepping");
2165 genInterpSingleStep(cUnit, mir);
2166 break;
2167 }
Bill Buzbeec6f10662010-02-09 11:16:15 -08002168 dvmCompilerFlushAllRegs(cUnit); /* Everything to home location */
Bill Buzbee1465db52009-09-23 17:17:35 -07002169 loadValueDirectFixed(cUnit, rlSrc, r0); /* Ref */
Ben Chengba4fc8b2009-06-01 13:00:29 -07002170 loadConstant(cUnit, r2, (int) classPtr );
Bill Buzbee270c1d62009-08-13 16:58:07 -07002171//TUNING: compare to 0 primative to allow use of CB[N]Z
Bill Buzbee1465db52009-09-23 17:17:35 -07002172 opRegImm(cUnit, kOpCmp, r0, 0); /* NULL? */
Ben Cheng752c7942009-06-22 10:50:07 -07002173 /* When taken r0 has NULL which can be used for store directly */
Bill Buzbee1465db52009-09-23 17:17:35 -07002174 ArmLIR *branch1 = opCondBranch(cUnit, kArmCondEq);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002175 /* r1 now contains object->clazz */
Bill Buzbee270c1d62009-08-13 16:58:07 -07002176 loadWordDisp(cUnit, r0, offsetof(Object, clazz), r1);
Bill Buzbee1465db52009-09-23 17:17:35 -07002177 /* r1 now contains object->clazz */
Ben Chengbd1326d2010-04-02 15:04:53 -07002178 LOAD_FUNC_ADDR(cUnit, r3, (int)dvmInstanceofNonTrivial);
Ben Cheng752c7942009-06-22 10:50:07 -07002179 loadConstant(cUnit, r0, 1); /* Assume true */
Bill Buzbee1465db52009-09-23 17:17:35 -07002180 opRegReg(cUnit, kOpCmp, r1, r2);
2181 ArmLIR *branch2 = opCondBranch(cUnit, kArmCondEq);
2182 genRegCopy(cUnit, r0, r1);
2183 genRegCopy(cUnit, r1, r2);
2184 opReg(cUnit, kOpBlx, r3);
Elliott Hughes6a555132010-02-25 15:41:42 -08002185 dvmCompilerClobberCallRegs(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002186 /* branch target here */
Bill Buzbee1465db52009-09-23 17:17:35 -07002187 ArmLIR *target = newLIR0(cUnit, kArmPseudoTargetLabel);
Ben Chengd7d426a2009-09-22 11:23:36 -07002188 target->defMask = ENCODE_ALL;
Bill Buzbeec6f10662010-02-09 11:16:15 -08002189 rlResult = dvmCompilerGetReturn(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07002190 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002191 branch1->generic.target = (LIR *)target;
2192 branch2->generic.target = (LIR *)target;
2193 break;
2194 }
2195 case OP_IGET_WIDE:
2196 genIGetWide(cUnit, mir, fieldOffset);
2197 break;
2198 case OP_IGET:
2199 case OP_IGET_OBJECT:
Bill Buzbee1465db52009-09-23 17:17:35 -07002200 genIGet(cUnit, mir, kWord, fieldOffset);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002201 break;
2202 case OP_IGET_BOOLEAN:
Bill Buzbee1465db52009-09-23 17:17:35 -07002203 genIGet(cUnit, mir, kUnsignedByte, fieldOffset);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002204 break;
2205 case OP_IGET_BYTE:
Bill Buzbee1465db52009-09-23 17:17:35 -07002206 genIGet(cUnit, mir, kSignedByte, fieldOffset);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002207 break;
2208 case OP_IGET_CHAR:
Bill Buzbee1465db52009-09-23 17:17:35 -07002209 genIGet(cUnit, mir, kUnsignedHalf, fieldOffset);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002210 break;
2211 case OP_IGET_SHORT:
Bill Buzbee1465db52009-09-23 17:17:35 -07002212 genIGet(cUnit, mir, kSignedHalf, fieldOffset);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002213 break;
2214 case OP_IPUT_WIDE:
2215 genIPutWide(cUnit, mir, fieldOffset);
2216 break;
2217 case OP_IPUT:
2218 case OP_IPUT_OBJECT:
Bill Buzbee1465db52009-09-23 17:17:35 -07002219 genIPut(cUnit, mir, kWord, fieldOffset);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002220 break;
2221 case OP_IPUT_SHORT:
2222 case OP_IPUT_CHAR:
Bill Buzbee1465db52009-09-23 17:17:35 -07002223 genIPut(cUnit, mir, kUnsignedHalf, fieldOffset);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002224 break;
2225 case OP_IPUT_BYTE:
2226 case OP_IPUT_BOOLEAN:
Bill Buzbee1465db52009-09-23 17:17:35 -07002227 genIPut(cUnit, mir, kUnsignedByte, fieldOffset);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002228 break;
Bill Buzbeeb16344a2010-03-15 17:19:12 -07002229 case OP_IGET_WIDE_VOLATILE:
2230 case OP_IPUT_WIDE_VOLATILE:
2231 case OP_SGET_WIDE_VOLATILE:
2232 case OP_SPUT_WIDE_VOLATILE:
2233 genInterpSingleStep(cUnit, mir);
2234 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002235 default:
2236 return true;
2237 }
2238 return false;
2239}
2240
2241static bool handleFmt22cs(CompilationUnit *cUnit, MIR *mir)
2242{
2243 OpCode dalvikOpCode = mir->dalvikInsn.opCode;
2244 int fieldOffset = mir->dalvikInsn.vC;
2245 switch (dalvikOpCode) {
2246 case OP_IGET_QUICK:
2247 case OP_IGET_OBJECT_QUICK:
Bill Buzbee1465db52009-09-23 17:17:35 -07002248 genIGet(cUnit, mir, kWord, fieldOffset);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002249 break;
2250 case OP_IPUT_QUICK:
2251 case OP_IPUT_OBJECT_QUICK:
Bill Buzbee1465db52009-09-23 17:17:35 -07002252 genIPut(cUnit, mir, kWord, fieldOffset);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002253 break;
2254 case OP_IGET_WIDE_QUICK:
2255 genIGetWide(cUnit, mir, fieldOffset);
2256 break;
2257 case OP_IPUT_WIDE_QUICK:
2258 genIPutWide(cUnit, mir, fieldOffset);
2259 break;
2260 default:
2261 return true;
2262 }
2263 return false;
2264
2265}
2266
2267/* Compare agaist zero */
2268static bool handleFmt22t(CompilationUnit *cUnit, MIR *mir, BasicBlock *bb,
Bill Buzbee89efc3d2009-07-28 11:22:22 -07002269 ArmLIR *labelList)
Ben Chengba4fc8b2009-06-01 13:00:29 -07002270{
2271 OpCode dalvikOpCode = mir->dalvikInsn.opCode;
Bill Buzbee89efc3d2009-07-28 11:22:22 -07002272 ArmConditionCode cond;
Bill Buzbeec6f10662010-02-09 11:16:15 -08002273 RegLocation rlSrc1 = dvmCompilerGetSrc(cUnit, mir, 0);
2274 RegLocation rlSrc2 = dvmCompilerGetSrc(cUnit, mir, 1);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002275
Bill Buzbee1465db52009-09-23 17:17:35 -07002276 rlSrc1 = loadValue(cUnit, rlSrc1, kCoreReg);
2277 rlSrc2 = loadValue(cUnit, rlSrc2, kCoreReg);
2278 opRegReg(cUnit, kOpCmp, rlSrc1.lowReg, rlSrc2.lowReg);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002279
2280 switch (dalvikOpCode) {
2281 case OP_IF_EQ:
Bill Buzbee1465db52009-09-23 17:17:35 -07002282 cond = kArmCondEq;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002283 break;
2284 case OP_IF_NE:
Bill Buzbee1465db52009-09-23 17:17:35 -07002285 cond = kArmCondNe;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002286 break;
2287 case OP_IF_LT:
Bill Buzbee1465db52009-09-23 17:17:35 -07002288 cond = kArmCondLt;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002289 break;
2290 case OP_IF_GE:
Bill Buzbee1465db52009-09-23 17:17:35 -07002291 cond = kArmCondGe;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002292 break;
2293 case OP_IF_GT:
Bill Buzbee1465db52009-09-23 17:17:35 -07002294 cond = kArmCondGt;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002295 break;
2296 case OP_IF_LE:
Bill Buzbee1465db52009-09-23 17:17:35 -07002297 cond = kArmCondLe;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002298 break;
2299 default:
2300 cond = 0;
2301 LOGE("Unexpected opcode (%d) for Fmt22t\n", dalvikOpCode);
Bill Buzbeefc519dc2010-03-06 23:30:57 -08002302 dvmCompilerAbort(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002303 }
2304 genConditionalBranch(cUnit, cond, &labelList[bb->taken->id]);
2305 /* This mostly likely will be optimized away in a later phase */
2306 genUnconditionalBranch(cUnit, &labelList[bb->fallThrough->id]);
2307 return false;
2308}
2309
2310static bool handleFmt22x_Fmt32x(CompilationUnit *cUnit, MIR *mir)
2311{
2312 OpCode opCode = mir->dalvikInsn.opCode;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002313
2314 switch (opCode) {
2315 case OP_MOVE_16:
2316 case OP_MOVE_OBJECT_16:
2317 case OP_MOVE_FROM16:
Ben Chenge9695e52009-06-16 16:11:47 -07002318 case OP_MOVE_OBJECT_FROM16: {
Bill Buzbeec6f10662010-02-09 11:16:15 -08002319 storeValue(cUnit, dvmCompilerGetDest(cUnit, mir, 0),
2320 dvmCompilerGetSrc(cUnit, mir, 0));
Ben Chengba4fc8b2009-06-01 13:00:29 -07002321 break;
Ben Chenge9695e52009-06-16 16:11:47 -07002322 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07002323 case OP_MOVE_WIDE_16:
Ben Chenge9695e52009-06-16 16:11:47 -07002324 case OP_MOVE_WIDE_FROM16: {
Bill Buzbeec6f10662010-02-09 11:16:15 -08002325 storeValueWide(cUnit, dvmCompilerGetDestWide(cUnit, mir, 0, 1),
2326 dvmCompilerGetSrcWide(cUnit, mir, 0, 1));
Ben Chengba4fc8b2009-06-01 13:00:29 -07002327 break;
Ben Chenge9695e52009-06-16 16:11:47 -07002328 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07002329 default:
2330 return true;
2331 }
2332 return false;
2333}
2334
2335static bool handleFmt23x(CompilationUnit *cUnit, MIR *mir)
2336{
2337 OpCode opCode = mir->dalvikInsn.opCode;
Bill Buzbee1465db52009-09-23 17:17:35 -07002338 RegLocation rlSrc1;
2339 RegLocation rlSrc2;
2340 RegLocation rlDest;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002341
2342 if ( (opCode >= OP_ADD_INT) && (opCode <= OP_REM_DOUBLE)) {
Ben Cheng5d90c202009-11-22 23:31:11 -08002343 return genArithOp( cUnit, mir );
Ben Chengba4fc8b2009-06-01 13:00:29 -07002344 }
2345
Bill Buzbee1465db52009-09-23 17:17:35 -07002346 /* APUTs have 3 sources and no targets */
2347 if (mir->ssaRep->numDefs == 0) {
2348 if (mir->ssaRep->numUses == 3) {
Bill Buzbeec6f10662010-02-09 11:16:15 -08002349 rlDest = dvmCompilerGetSrc(cUnit, mir, 0);
2350 rlSrc1 = dvmCompilerGetSrc(cUnit, mir, 1);
2351 rlSrc2 = dvmCompilerGetSrc(cUnit, mir, 2);
Bill Buzbee1465db52009-09-23 17:17:35 -07002352 } else {
2353 assert(mir->ssaRep->numUses == 4);
Bill Buzbeec6f10662010-02-09 11:16:15 -08002354 rlDest = dvmCompilerGetSrcWide(cUnit, mir, 0, 1);
2355 rlSrc1 = dvmCompilerGetSrc(cUnit, mir, 2);
2356 rlSrc2 = dvmCompilerGetSrc(cUnit, mir, 3);
Bill Buzbee1465db52009-09-23 17:17:35 -07002357 }
2358 } else {
2359 /* Two sources and 1 dest. Deduce the operand sizes */
2360 if (mir->ssaRep->numUses == 4) {
Bill Buzbeec6f10662010-02-09 11:16:15 -08002361 rlSrc1 = dvmCompilerGetSrcWide(cUnit, mir, 0, 1);
2362 rlSrc2 = dvmCompilerGetSrcWide(cUnit, mir, 2, 3);
Bill Buzbee1465db52009-09-23 17:17:35 -07002363 } else {
2364 assert(mir->ssaRep->numUses == 2);
Bill Buzbeec6f10662010-02-09 11:16:15 -08002365 rlSrc1 = dvmCompilerGetSrc(cUnit, mir, 0);
2366 rlSrc2 = dvmCompilerGetSrc(cUnit, mir, 1);
Bill Buzbee1465db52009-09-23 17:17:35 -07002367 }
2368 if (mir->ssaRep->numDefs == 2) {
Bill Buzbeec6f10662010-02-09 11:16:15 -08002369 rlDest = dvmCompilerGetDestWide(cUnit, mir, 0, 1);
Bill Buzbee1465db52009-09-23 17:17:35 -07002370 } else {
2371 assert(mir->ssaRep->numDefs == 1);
Bill Buzbeec6f10662010-02-09 11:16:15 -08002372 rlDest = dvmCompilerGetDest(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07002373 }
2374 }
2375
2376
Ben Chengba4fc8b2009-06-01 13:00:29 -07002377 switch (opCode) {
Bill Buzbeed45ba372009-06-15 17:00:57 -07002378 case OP_CMPL_FLOAT:
2379 case OP_CMPG_FLOAT:
2380 case OP_CMPL_DOUBLE:
2381 case OP_CMPG_DOUBLE:
Ben Cheng5d90c202009-11-22 23:31:11 -08002382 return genCmpFP(cUnit, mir, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002383 case OP_CMP_LONG:
Bill Buzbee1465db52009-09-23 17:17:35 -07002384 genCmpLong(cUnit, mir, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002385 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002386 case OP_AGET_WIDE:
Bill Buzbee1465db52009-09-23 17:17:35 -07002387 genArrayGet(cUnit, mir, kLong, rlSrc1, rlSrc2, rlDest, 3);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002388 break;
2389 case OP_AGET:
2390 case OP_AGET_OBJECT:
Bill Buzbee1465db52009-09-23 17:17:35 -07002391 genArrayGet(cUnit, mir, kWord, rlSrc1, rlSrc2, rlDest, 2);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002392 break;
2393 case OP_AGET_BOOLEAN:
Bill Buzbee1465db52009-09-23 17:17:35 -07002394 genArrayGet(cUnit, mir, kUnsignedByte, rlSrc1, rlSrc2, rlDest, 0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002395 break;
2396 case OP_AGET_BYTE:
Bill Buzbee1465db52009-09-23 17:17:35 -07002397 genArrayGet(cUnit, mir, kSignedByte, rlSrc1, rlSrc2, rlDest, 0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002398 break;
2399 case OP_AGET_CHAR:
Bill Buzbee1465db52009-09-23 17:17:35 -07002400 genArrayGet(cUnit, mir, kUnsignedHalf, rlSrc1, rlSrc2, rlDest, 1);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002401 break;
2402 case OP_AGET_SHORT:
Bill Buzbee1465db52009-09-23 17:17:35 -07002403 genArrayGet(cUnit, mir, kSignedHalf, rlSrc1, rlSrc2, rlDest, 1);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002404 break;
2405 case OP_APUT_WIDE:
Bill Buzbee1465db52009-09-23 17:17:35 -07002406 genArrayPut(cUnit, mir, kLong, rlSrc1, rlSrc2, rlDest, 3);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002407 break;
2408 case OP_APUT:
Bill Buzbee1465db52009-09-23 17:17:35 -07002409 genArrayPut(cUnit, mir, kWord, rlSrc1, rlSrc2, rlDest, 2);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002410 break;
Bill Buzbeebe6534f2010-03-12 16:01:35 -08002411 case OP_APUT_OBJECT:
2412 genArrayObjectPut(cUnit, mir, rlSrc1, rlSrc2, rlDest, 2);
2413 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002414 case OP_APUT_SHORT:
2415 case OP_APUT_CHAR:
Bill Buzbee1465db52009-09-23 17:17:35 -07002416 genArrayPut(cUnit, mir, kUnsignedHalf, rlSrc1, rlSrc2, rlDest, 1);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002417 break;
2418 case OP_APUT_BYTE:
2419 case OP_APUT_BOOLEAN:
Bill Buzbee1465db52009-09-23 17:17:35 -07002420 genArrayPut(cUnit, mir, kUnsignedByte, rlSrc1, rlSrc2, rlDest, 0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002421 break;
2422 default:
2423 return true;
2424 }
2425 return false;
2426}
2427
Ben Cheng6c10a972009-10-29 14:39:18 -07002428/*
2429 * Find the matching case.
2430 *
2431 * return values:
2432 * r0 (low 32-bit): pc of the chaining cell corresponding to the resolved case,
2433 * including default which is placed at MIN(size, MAX_CHAINED_SWITCH_CASES).
2434 * r1 (high 32-bit): the branch offset of the matching case (only for indexes
2435 * above MAX_CHAINED_SWITCH_CASES).
2436 *
2437 * Instructions around the call are:
2438 *
2439 * mov r2, pc
2440 * blx &findPackedSwitchIndex
2441 * mov pc, r0
2442 * .align4
2443 * chaining cell for case 0 [8 bytes]
2444 * chaining cell for case 1 [8 bytes]
2445 * :
2446 * chaining cell for case MIN(size, MAX_CHAINED_SWITCH_CASES)-1 [8 bytes]
2447 * chaining cell for case default [8 bytes]
2448 * noChain exit
2449 */
Ben Chengbd1326d2010-04-02 15:04:53 -07002450static s8 findPackedSwitchIndex(const u2* switchData, int testVal, int pc)
Ben Cheng6c10a972009-10-29 14:39:18 -07002451{
2452 int size;
2453 int firstKey;
2454 const int *entries;
2455 int index;
2456 int jumpIndex;
2457 int caseDPCOffset = 0;
2458 /* In Thumb mode pc is 4 ahead of the "mov r2, pc" instruction */
2459 int chainingPC = (pc + 4) & ~3;
2460
2461 /*
2462 * Packed switch data format:
2463 * ushort ident = 0x0100 magic value
2464 * ushort size number of entries in the table
2465 * int first_key first (and lowest) switch case value
2466 * int targets[size] branch targets, relative to switch opcode
2467 *
2468 * Total size is (4+size*2) 16-bit code units.
2469 */
2470 size = switchData[1];
2471 assert(size > 0);
2472
2473 firstKey = switchData[2];
2474 firstKey |= switchData[3] << 16;
2475
2476
2477 /* The entries are guaranteed to be aligned on a 32-bit boundary;
2478 * we can treat them as a native int array.
2479 */
2480 entries = (const int*) &switchData[4];
2481 assert(((u4)entries & 0x3) == 0);
2482
2483 index = testVal - firstKey;
2484
2485 /* Jump to the default cell */
2486 if (index < 0 || index >= size) {
2487 jumpIndex = MIN(size, MAX_CHAINED_SWITCH_CASES);
2488 /* Jump to the non-chaining exit point */
2489 } else if (index >= MAX_CHAINED_SWITCH_CASES) {
2490 jumpIndex = MAX_CHAINED_SWITCH_CASES + 1;
2491 caseDPCOffset = entries[index];
2492 /* Jump to the inline chaining cell */
2493 } else {
2494 jumpIndex = index;
2495 }
2496
2497 chainingPC += jumpIndex * 8;
2498 return (((s8) caseDPCOffset) << 32) | (u8) chainingPC;
2499}
2500
2501/* See comments for findPackedSwitchIndex */
Ben Chengbd1326d2010-04-02 15:04:53 -07002502static s8 findSparseSwitchIndex(const u2* switchData, int testVal, int pc)
Ben Cheng6c10a972009-10-29 14:39:18 -07002503{
2504 int size;
2505 const int *keys;
2506 const int *entries;
2507 int chainingPC = (pc + 4) & ~3;
2508 int i;
2509
2510 /*
2511 * Sparse switch data format:
2512 * ushort ident = 0x0200 magic value
2513 * ushort size number of entries in the table; > 0
2514 * int keys[size] keys, sorted low-to-high; 32-bit aligned
2515 * int targets[size] branch targets, relative to switch opcode
2516 *
2517 * Total size is (2+size*4) 16-bit code units.
2518 */
2519
2520 size = switchData[1];
2521 assert(size > 0);
2522
2523 /* The keys are guaranteed to be aligned on a 32-bit boundary;
2524 * we can treat them as a native int array.
2525 */
2526 keys = (const int*) &switchData[2];
2527 assert(((u4)keys & 0x3) == 0);
2528
2529 /* The entries are guaranteed to be aligned on a 32-bit boundary;
2530 * we can treat them as a native int array.
2531 */
2532 entries = keys + size;
2533 assert(((u4)entries & 0x3) == 0);
2534
2535 /*
2536 * Run through the list of keys, which are guaranteed to
2537 * be sorted low-to-high.
2538 *
2539 * Most tables have 3-4 entries. Few have more than 10. A binary
2540 * search here is probably not useful.
2541 */
2542 for (i = 0; i < size; i++) {
2543 int k = keys[i];
2544 if (k == testVal) {
2545 /* MAX_CHAINED_SWITCH_CASES + 1 is the start of the overflow case */
2546 int jumpIndex = (i < MAX_CHAINED_SWITCH_CASES) ?
2547 i : MAX_CHAINED_SWITCH_CASES + 1;
2548 chainingPC += jumpIndex * 8;
2549 return (((s8) entries[i]) << 32) | (u8) chainingPC;
2550 } else if (k > testVal) {
2551 break;
2552 }
2553 }
2554 return chainingPC + MIN(size, MAX_CHAINED_SWITCH_CASES) * 8;
2555}
2556
Ben Chengba4fc8b2009-06-01 13:00:29 -07002557static bool handleFmt31t(CompilationUnit *cUnit, MIR *mir)
2558{
2559 OpCode dalvikOpCode = mir->dalvikInsn.opCode;
2560 switch (dalvikOpCode) {
2561 case OP_FILL_ARRAY_DATA: {
Bill Buzbeec6f10662010-02-09 11:16:15 -08002562 RegLocation rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07002563 // Making a call - use explicit registers
Bill Buzbeec6f10662010-02-09 11:16:15 -08002564 dvmCompilerFlushAllRegs(cUnit); /* Everything to home location */
Bill Buzbee1465db52009-09-23 17:17:35 -07002565 genExportPC(cUnit, mir);
2566 loadValueDirectFixed(cUnit, rlSrc, r0);
Ben Chengbd1326d2010-04-02 15:04:53 -07002567 LOAD_FUNC_ADDR(cUnit, r2, (int)dvmInterpHandleFillArrayData);
Ben Cheng6c10a972009-10-29 14:39:18 -07002568 loadConstant(cUnit, r1,
2569 (int) (cUnit->method->insns + mir->offset + mir->dalvikInsn.vB));
Bill Buzbee1465db52009-09-23 17:17:35 -07002570 opReg(cUnit, kOpBlx, r2);
Elliott Hughes6a555132010-02-25 15:41:42 -08002571 dvmCompilerClobberCallRegs(cUnit);
Bill Buzbeefd023aa2009-11-02 09:23:49 -08002572 /* generate a branch over if successful */
2573 opRegImm(cUnit, kOpCmp, r0, 0); /* NULL? */
2574 ArmLIR *branchOver = opCondBranch(cUnit, kArmCondNe);
2575 loadConstant(cUnit, r0,
2576 (int) (cUnit->method->insns + mir->offset));
2577 genDispatchToHandler(cUnit, TEMPLATE_THROW_EXCEPTION_COMMON);
2578 ArmLIR *target = newLIR0(cUnit, kArmPseudoTargetLabel);
2579 target->defMask = ENCODE_ALL;
2580 branchOver->generic.target = (LIR *) target;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002581 break;
2582 }
2583 /*
Ben Cheng6c10a972009-10-29 14:39:18 -07002584 * Compute the goto target of up to
2585 * MIN(switchSize, MAX_CHAINED_SWITCH_CASES) + 1 chaining cells.
2586 * See the comment before findPackedSwitchIndex for the code layout.
Ben Chengba4fc8b2009-06-01 13:00:29 -07002587 */
2588 case OP_PACKED_SWITCH:
2589 case OP_SPARSE_SWITCH: {
Bill Buzbeec6f10662010-02-09 11:16:15 -08002590 RegLocation rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
2591 dvmCompilerFlushAllRegs(cUnit); /* Everything to home location */
Bill Buzbee1465db52009-09-23 17:17:35 -07002592 loadValueDirectFixed(cUnit, rlSrc, r1);
Bill Buzbeec6f10662010-02-09 11:16:15 -08002593 dvmCompilerLockAllTemps(cUnit);
Ben Cheng6c10a972009-10-29 14:39:18 -07002594 const u2 *switchData =
2595 cUnit->method->insns + mir->offset + mir->dalvikInsn.vB;
2596 u2 size = switchData[1];
2597
Ben Chengba4fc8b2009-06-01 13:00:29 -07002598 if (dalvikOpCode == OP_PACKED_SWITCH) {
Ben Chengbd1326d2010-04-02 15:04:53 -07002599 LOAD_FUNC_ADDR(cUnit, r4PC, (int)findPackedSwitchIndex);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002600 } else {
Ben Chengbd1326d2010-04-02 15:04:53 -07002601 LOAD_FUNC_ADDR(cUnit, r4PC, (int)findSparseSwitchIndex);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002602 }
Ben Cheng6c10a972009-10-29 14:39:18 -07002603 /* r0 <- Addr of the switch data */
2604 loadConstant(cUnit, r0,
2605 (int) (cUnit->method->insns + mir->offset + mir->dalvikInsn.vB));
2606 /* r2 <- pc of the instruction following the blx */
2607 opRegReg(cUnit, kOpMov, r2, rpc);
Bill Buzbee1465db52009-09-23 17:17:35 -07002608 opReg(cUnit, kOpBlx, r4PC);
Elliott Hughes6a555132010-02-25 15:41:42 -08002609 dvmCompilerClobberCallRegs(cUnit);
Ben Cheng6c10a972009-10-29 14:39:18 -07002610 /* pc <- computed goto target */
2611 opRegReg(cUnit, kOpMov, rpc, r0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002612 break;
2613 }
2614 default:
2615 return true;
2616 }
2617 return false;
2618}
2619
2620static bool handleFmt35c_3rc(CompilationUnit *cUnit, MIR *mir, BasicBlock *bb,
Bill Buzbee89efc3d2009-07-28 11:22:22 -07002621 ArmLIR *labelList)
Ben Chengba4fc8b2009-06-01 13:00:29 -07002622{
Bill Buzbee9bc3df32009-07-30 10:52:29 -07002623 ArmLIR *retChainingCell = NULL;
Bill Buzbee89efc3d2009-07-28 11:22:22 -07002624 ArmLIR *pcrLabel = NULL;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002625
Bill Buzbeef4ce16f2009-07-28 13:28:25 -07002626 if (bb->fallThrough != NULL)
2627 retChainingCell = &labelList[bb->fallThrough->id];
2628
Ben Chengba4fc8b2009-06-01 13:00:29 -07002629 DecodedInstruction *dInsn = &mir->dalvikInsn;
2630 switch (mir->dalvikInsn.opCode) {
2631 /*
2632 * calleeMethod = this->clazz->vtable[
2633 * method->clazz->pDvmDex->pResMethods[BBBB]->methodIndex
2634 * ]
2635 */
2636 case OP_INVOKE_VIRTUAL:
2637 case OP_INVOKE_VIRTUAL_RANGE: {
Bill Buzbee89efc3d2009-07-28 11:22:22 -07002638 ArmLIR *predChainingCell = &labelList[bb->taken->id];
Ben Chengba4fc8b2009-06-01 13:00:29 -07002639 int methodIndex =
2640 cUnit->method->clazz->pDvmDex->pResMethods[dInsn->vB]->
2641 methodIndex;
2642
2643 if (mir->dalvikInsn.opCode == OP_INVOKE_VIRTUAL)
2644 genProcessArgsNoRange(cUnit, mir, dInsn, &pcrLabel);
2645 else
2646 genProcessArgsRange(cUnit, mir, dInsn, &pcrLabel);
2647
Ben Cheng38329f52009-07-07 14:19:20 -07002648 genInvokeVirtualCommon(cUnit, mir, methodIndex,
2649 retChainingCell,
2650 predChainingCell,
2651 pcrLabel);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002652 break;
2653 }
2654 /*
2655 * calleeMethod = method->clazz->super->vtable[method->clazz->pDvmDex
2656 * ->pResMethods[BBBB]->methodIndex]
2657 */
Ben Chengba4fc8b2009-06-01 13:00:29 -07002658 case OP_INVOKE_SUPER:
2659 case OP_INVOKE_SUPER_RANGE: {
2660 int mIndex = cUnit->method->clazz->pDvmDex->
2661 pResMethods[dInsn->vB]->methodIndex;
2662 const Method *calleeMethod =
2663 cUnit->method->clazz->super->vtable[mIndex];
2664
2665 if (mir->dalvikInsn.opCode == OP_INVOKE_SUPER)
2666 genProcessArgsNoRange(cUnit, mir, dInsn, &pcrLabel);
2667 else
2668 genProcessArgsRange(cUnit, mir, dInsn, &pcrLabel);
2669
2670 /* r0 = calleeMethod */
2671 loadConstant(cUnit, r0, (int) calleeMethod);
2672
Ben Cheng38329f52009-07-07 14:19:20 -07002673 genInvokeSingletonCommon(cUnit, mir, bb, labelList, pcrLabel,
2674 calleeMethod);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002675 break;
2676 }
2677 /* calleeMethod = method->clazz->pDvmDex->pResMethods[BBBB] */
2678 case OP_INVOKE_DIRECT:
2679 case OP_INVOKE_DIRECT_RANGE: {
2680 const Method *calleeMethod =
2681 cUnit->method->clazz->pDvmDex->pResMethods[dInsn->vB];
2682
2683 if (mir->dalvikInsn.opCode == OP_INVOKE_DIRECT)
2684 genProcessArgsNoRange(cUnit, mir, dInsn, &pcrLabel);
2685 else
2686 genProcessArgsRange(cUnit, mir, dInsn, &pcrLabel);
2687
2688 /* r0 = calleeMethod */
2689 loadConstant(cUnit, r0, (int) calleeMethod);
2690
Ben Cheng38329f52009-07-07 14:19:20 -07002691 genInvokeSingletonCommon(cUnit, mir, bb, labelList, pcrLabel,
2692 calleeMethod);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002693 break;
2694 }
2695 /* calleeMethod = method->clazz->pDvmDex->pResMethods[BBBB] */
2696 case OP_INVOKE_STATIC:
2697 case OP_INVOKE_STATIC_RANGE: {
2698 const Method *calleeMethod =
2699 cUnit->method->clazz->pDvmDex->pResMethods[dInsn->vB];
2700
2701 if (mir->dalvikInsn.opCode == OP_INVOKE_STATIC)
2702 genProcessArgsNoRange(cUnit, mir, dInsn,
2703 NULL /* no null check */);
2704 else
2705 genProcessArgsRange(cUnit, mir, dInsn,
2706 NULL /* no null check */);
2707
2708 /* r0 = calleeMethod */
2709 loadConstant(cUnit, r0, (int) calleeMethod);
2710
Ben Cheng38329f52009-07-07 14:19:20 -07002711 genInvokeSingletonCommon(cUnit, mir, bb, labelList, pcrLabel,
2712 calleeMethod);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002713 break;
2714 }
Bill Buzbee1465db52009-09-23 17:17:35 -07002715 /*
Ben Chengba4fc8b2009-06-01 13:00:29 -07002716 * calleeMethod = dvmFindInterfaceMethodInCache(this->clazz,
2717 * BBBB, method, method->clazz->pDvmDex)
Ben Cheng38329f52009-07-07 14:19:20 -07002718 *
2719 * Given "invoke-interface {v0}", the following is the generated code:
2720 *
2721 * 0x426a9abe : ldr r0, [r5, #0] --+
2722 * 0x426a9ac0 : mov r7, r5 |
2723 * 0x426a9ac2 : sub r7, #24 |
2724 * 0x426a9ac4 : cmp r0, #0 | genProcessArgsNoRange
2725 * 0x426a9ac6 : beq 0x426a9afe |
2726 * 0x426a9ac8 : stmia r7, <r0> --+
2727 * 0x426a9aca : ldr r4, [pc, #104] --> r4 <- dalvikPC of this invoke
2728 * 0x426a9acc : add r1, pc, #52 --> r1 <- &retChainingCell
2729 * 0x426a9ace : add r2, pc, #60 --> r2 <- &predictedChainingCell
2730 * 0x426a9ad0 : blx_1 0x426a918c --+ TEMPLATE_INVOKE_METHOD_
2731 * 0x426a9ad2 : blx_2 see above --+ PREDICTED_CHAIN
2732 * 0x426a9ad4 : b 0x426a9b0c --> off to the predicted chain
2733 * 0x426a9ad6 : b 0x426a9afe --> punt to the interpreter
Ben Chenga8e64a72009-10-20 13:01:36 -07002734 * 0x426a9ad8 : mov r8, r1 --+
2735 * 0x426a9ada : mov r9, r2 |
2736 * 0x426a9adc : mov r10, r3 |
Ben Cheng38329f52009-07-07 14:19:20 -07002737 * 0x426a9ade : mov r0, r3 |
2738 * 0x426a9ae0 : mov r1, #74 | dvmFindInterfaceMethodInCache
2739 * 0x426a9ae2 : ldr r2, [pc, #76] |
2740 * 0x426a9ae4 : ldr r3, [pc, #68] |
2741 * 0x426a9ae6 : ldr r7, [pc, #64] |
2742 * 0x426a9ae8 : blx r7 --+
Ben Chenga8e64a72009-10-20 13:01:36 -07002743 * 0x426a9aea : mov r1, r8 --> r1 <- rechain count
Ben Cheng38329f52009-07-07 14:19:20 -07002744 * 0x426a9aec : cmp r1, #0 --> compare against 0
2745 * 0x426a9aee : bgt 0x426a9af8 --> >=0? don't rechain
2746 * 0x426a9af0 : ldr r7, [r6, #96] --+
Ben Chenga8e64a72009-10-20 13:01:36 -07002747 * 0x426a9af2 : mov r2, r9 | dvmJitToPatchPredictedChain
2748 * 0x426a9af4 : mov r3, r10 |
Ben Cheng38329f52009-07-07 14:19:20 -07002749 * 0x426a9af6 : blx r7 --+
2750 * 0x426a9af8 : add r1, pc, #8 --> r1 <- &retChainingCell
2751 * 0x426a9afa : blx_1 0x426a9098 --+ TEMPLATE_INVOKE_METHOD_NO_OPT
2752 * 0x426a9afc : blx_2 see above --+
2753 * -------- reconstruct dalvik PC : 0x428b786c @ +0x001e
2754 * 0x426a9afe (0042): ldr r0, [pc, #52]
2755 * Exception_Handling:
2756 * 0x426a9b00 (0044): ldr r1, [r6, #84]
2757 * 0x426a9b02 (0046): blx r1
2758 * 0x426a9b04 (0048): .align4
2759 * -------- chaining cell (hot): 0x0021
2760 * 0x426a9b04 (0048): ldr r0, [r6, #92]
2761 * 0x426a9b06 (004a): blx r0
2762 * 0x426a9b08 (004c): data 0x7872(30834)
2763 * 0x426a9b0a (004e): data 0x428b(17035)
2764 * 0x426a9b0c (0050): .align4
2765 * -------- chaining cell (predicted)
2766 * 0x426a9b0c (0050): data 0x0000(0) --> will be patched into bx
2767 * 0x426a9b0e (0052): data 0x0000(0)
2768 * 0x426a9b10 (0054): data 0x0000(0) --> class
2769 * 0x426a9b12 (0056): data 0x0000(0)
2770 * 0x426a9b14 (0058): data 0x0000(0) --> method
2771 * 0x426a9b16 (005a): data 0x0000(0)
2772 * 0x426a9b18 (005c): data 0x0000(0) --> reset count
2773 * 0x426a9b1a (005e): data 0x0000(0)
2774 * 0x426a9b28 (006c): .word (0xad0392a5)
2775 * 0x426a9b2c (0070): .word (0x6e750)
2776 * 0x426a9b30 (0074): .word (0x4109a618)
2777 * 0x426a9b34 (0078): .word (0x428b786c)
Ben Chengba4fc8b2009-06-01 13:00:29 -07002778 */
2779 case OP_INVOKE_INTERFACE:
2780 case OP_INVOKE_INTERFACE_RANGE: {
Bill Buzbee89efc3d2009-07-28 11:22:22 -07002781 ArmLIR *predChainingCell = &labelList[bb->taken->id];
Ben Chengba4fc8b2009-06-01 13:00:29 -07002782 int methodIndex = dInsn->vB;
2783
Bill Buzbee1465db52009-09-23 17:17:35 -07002784 /* Ensure that nothing is both live and dirty */
Bill Buzbeec6f10662010-02-09 11:16:15 -08002785 dvmCompilerFlushAllRegs(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07002786
Ben Chengba4fc8b2009-06-01 13:00:29 -07002787 if (mir->dalvikInsn.opCode == OP_INVOKE_INTERFACE)
2788 genProcessArgsNoRange(cUnit, mir, dInsn, &pcrLabel);
2789 else
2790 genProcessArgsRange(cUnit, mir, dInsn, &pcrLabel);
2791
Ben Cheng38329f52009-07-07 14:19:20 -07002792 /* "this" is already left in r0 by genProcessArgs* */
2793
2794 /* r4PC = dalvikCallsite */
2795 loadConstant(cUnit, r4PC,
2796 (int) (cUnit->method->insns + mir->offset));
2797
2798 /* r1 = &retChainingCell */
Bill Buzbee270c1d62009-08-13 16:58:07 -07002799 ArmLIR *addrRetChain =
Bill Buzbee1465db52009-09-23 17:17:35 -07002800 opRegRegImm(cUnit, kOpAdd, r1, rpc, 0);
Ben Cheng38329f52009-07-07 14:19:20 -07002801 addrRetChain->generic.target = (LIR *) retChainingCell;
2802
2803 /* r2 = &predictedChainingCell */
Bill Buzbee89efc3d2009-07-28 11:22:22 -07002804 ArmLIR *predictedChainingCell =
Bill Buzbee1465db52009-09-23 17:17:35 -07002805 opRegRegImm(cUnit, kOpAdd, r2, rpc, 0);
Ben Cheng38329f52009-07-07 14:19:20 -07002806 predictedChainingCell->generic.target = (LIR *) predChainingCell;
2807
2808 genDispatchToHandler(cUnit, TEMPLATE_INVOKE_METHOD_PREDICTED_CHAIN);
2809
2810 /* return through lr - jump to the chaining cell */
2811 genUnconditionalBranch(cUnit, predChainingCell);
2812
2813 /*
2814 * null-check on "this" may have been eliminated, but we still need
2815 * a PC-reconstruction label for stack overflow bailout.
2816 */
2817 if (pcrLabel == NULL) {
2818 int dPC = (int) (cUnit->method->insns + mir->offset);
Bill Buzbee89efc3d2009-07-28 11:22:22 -07002819 pcrLabel = dvmCompilerNew(sizeof(ArmLIR), true);
Ben Chenga4973592010-03-31 11:59:18 -07002820 pcrLabel->opCode = kArmPseudoPCReconstructionCell;
Ben Cheng38329f52009-07-07 14:19:20 -07002821 pcrLabel->operands[0] = dPC;
2822 pcrLabel->operands[1] = mir->offset;
2823 /* Insert the place holder to the growable list */
2824 dvmInsertGrowableList(&cUnit->pcReconstructionList, pcrLabel);
2825 }
2826
2827 /* return through lr+2 - punt to the interpreter */
2828 genUnconditionalBranch(cUnit, pcrLabel);
2829
2830 /*
2831 * return through lr+4 - fully resolve the callee method.
2832 * r1 <- count
2833 * r2 <- &predictedChainCell
2834 * r3 <- this->class
2835 * r4 <- dPC
2836 * r7 <- this->class->vtable
2837 */
2838
2839 /* Save count, &predictedChainCell, and class to high regs first */
Bill Buzbee1465db52009-09-23 17:17:35 -07002840 genRegCopy(cUnit, r8, r1);
2841 genRegCopy(cUnit, r9, r2);
2842 genRegCopy(cUnit, r10, r3);
Ben Cheng38329f52009-07-07 14:19:20 -07002843
Ben Chengba4fc8b2009-06-01 13:00:29 -07002844 /* r0 now contains this->clazz */
Bill Buzbee1465db52009-09-23 17:17:35 -07002845 genRegCopy(cUnit, r0, r3);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002846
2847 /* r1 = BBBB */
2848 loadConstant(cUnit, r1, dInsn->vB);
2849
2850 /* r2 = method (caller) */
2851 loadConstant(cUnit, r2, (int) cUnit->method);
2852
2853 /* r3 = pDvmDex */
2854 loadConstant(cUnit, r3, (int) cUnit->method->clazz->pDvmDex);
2855
Ben Chengbd1326d2010-04-02 15:04:53 -07002856 LOAD_FUNC_ADDR(cUnit, r7,
2857 (intptr_t) dvmFindInterfaceMethodInCache);
Bill Buzbee1465db52009-09-23 17:17:35 -07002858 opReg(cUnit, kOpBlx, r7);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002859
2860 /* r0 = calleeMethod (returned from dvmFindInterfaceMethodInCache */
2861
Bill Buzbee1465db52009-09-23 17:17:35 -07002862 genRegCopy(cUnit, r1, r8);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002863
Ben Cheng38329f52009-07-07 14:19:20 -07002864 /* Check if rechain limit is reached */
Bill Buzbee1465db52009-09-23 17:17:35 -07002865 opRegImm(cUnit, kOpCmp, r1, 0);
Ben Cheng38329f52009-07-07 14:19:20 -07002866
Bill Buzbee1465db52009-09-23 17:17:35 -07002867 ArmLIR *bypassRechaining = opCondBranch(cUnit, kArmCondGt);
Ben Cheng38329f52009-07-07 14:19:20 -07002868
Bill Buzbee270c1d62009-08-13 16:58:07 -07002869 loadWordDisp(cUnit, rGLUE, offsetof(InterpState,
2870 jitToInterpEntries.dvmJitToPatchPredictedChain), r7);
Ben Cheng38329f52009-07-07 14:19:20 -07002871
Bill Buzbee1465db52009-09-23 17:17:35 -07002872 genRegCopy(cUnit, r2, r9);
2873 genRegCopy(cUnit, r3, r10);
Ben Cheng38329f52009-07-07 14:19:20 -07002874
2875 /*
2876 * r0 = calleeMethod
2877 * r2 = &predictedChainingCell
2878 * r3 = class
2879 *
2880 * &returnChainingCell has been loaded into r1 but is not needed
2881 * when patching the chaining cell and will be clobbered upon
2882 * returning so it will be reconstructed again.
2883 */
Bill Buzbee1465db52009-09-23 17:17:35 -07002884 opReg(cUnit, kOpBlx, r7);
Ben Cheng38329f52009-07-07 14:19:20 -07002885
2886 /* r1 = &retChainingCell */
Bill Buzbee1465db52009-09-23 17:17:35 -07002887 addrRetChain = opRegRegImm(cUnit, kOpAdd, r1, rpc, 0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002888 addrRetChain->generic.target = (LIR *) retChainingCell;
Ben Cheng38329f52009-07-07 14:19:20 -07002889
2890 bypassRechaining->generic.target = (LIR *) addrRetChain;
2891
Ben Chengba4fc8b2009-06-01 13:00:29 -07002892 /*
2893 * r0 = this, r1 = calleeMethod,
2894 * r1 = &ChainingCell,
2895 * r4PC = callsiteDPC,
2896 */
2897 genDispatchToHandler(cUnit, TEMPLATE_INVOKE_METHOD_NO_OPT);
Ben Cheng86717f72010-03-05 15:27:21 -08002898#if defined(JIT_STATS)
2899 gDvmJit.invokePolymorphic++;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002900#endif
2901 /* Handle exceptions using the interpreter */
2902 genTrap(cUnit, mir->offset, pcrLabel);
2903 break;
2904 }
2905 /* NOP */
2906 case OP_INVOKE_DIRECT_EMPTY: {
2907 return false;
2908 }
2909 case OP_FILLED_NEW_ARRAY:
2910 case OP_FILLED_NEW_ARRAY_RANGE: {
2911 /* Just let the interpreter deal with these */
2912 genInterpSingleStep(cUnit, mir);
2913 break;
2914 }
2915 default:
2916 return true;
2917 }
2918 return false;
2919}
2920
2921static bool handleFmt35ms_3rms(CompilationUnit *cUnit, MIR *mir,
Bill Buzbee89efc3d2009-07-28 11:22:22 -07002922 BasicBlock *bb, ArmLIR *labelList)
Ben Chengba4fc8b2009-06-01 13:00:29 -07002923{
Bill Buzbee89efc3d2009-07-28 11:22:22 -07002924 ArmLIR *retChainingCell = &labelList[bb->fallThrough->id];
2925 ArmLIR *predChainingCell = &labelList[bb->taken->id];
2926 ArmLIR *pcrLabel = NULL;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002927
2928 DecodedInstruction *dInsn = &mir->dalvikInsn;
2929 switch (mir->dalvikInsn.opCode) {
2930 /* calleeMethod = this->clazz->vtable[BBBB] */
2931 case OP_INVOKE_VIRTUAL_QUICK_RANGE:
2932 case OP_INVOKE_VIRTUAL_QUICK: {
2933 int methodIndex = dInsn->vB;
2934 if (mir->dalvikInsn.opCode == OP_INVOKE_VIRTUAL_QUICK)
2935 genProcessArgsNoRange(cUnit, mir, dInsn, &pcrLabel);
2936 else
2937 genProcessArgsRange(cUnit, mir, dInsn, &pcrLabel);
2938
Ben Cheng38329f52009-07-07 14:19:20 -07002939 genInvokeVirtualCommon(cUnit, mir, methodIndex,
2940 retChainingCell,
2941 predChainingCell,
2942 pcrLabel);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002943 break;
2944 }
2945 /* calleeMethod = method->clazz->super->vtable[BBBB] */
2946 case OP_INVOKE_SUPER_QUICK:
2947 case OP_INVOKE_SUPER_QUICK_RANGE: {
2948 const Method *calleeMethod =
2949 cUnit->method->clazz->super->vtable[dInsn->vB];
2950
2951 if (mir->dalvikInsn.opCode == OP_INVOKE_SUPER_QUICK)
2952 genProcessArgsNoRange(cUnit, mir, dInsn, &pcrLabel);
2953 else
2954 genProcessArgsRange(cUnit, mir, dInsn, &pcrLabel);
2955
2956 /* r0 = calleeMethod */
2957 loadConstant(cUnit, r0, (int) calleeMethod);
2958
Ben Cheng38329f52009-07-07 14:19:20 -07002959 genInvokeSingletonCommon(cUnit, mir, bb, labelList, pcrLabel,
2960 calleeMethod);
2961 /* Handle exceptions using the interpreter */
2962 genTrap(cUnit, mir->offset, pcrLabel);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002963 break;
2964 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07002965 default:
2966 return true;
2967 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07002968 return false;
2969}
2970
2971/*
Bill Buzbeefd023aa2009-11-02 09:23:49 -08002972 * This operation is complex enough that we'll do it partly inline
2973 * and partly with a handler. NOTE: the handler uses hardcoded
2974 * values for string object offsets and must be revisitied if the
2975 * layout changes.
2976 */
2977static bool genInlinedCompareTo(CompilationUnit *cUnit, MIR *mir)
2978{
2979#if defined(USE_GLOBAL_STRING_DEFS)
2980 return false;
2981#else
2982 ArmLIR *rollback;
Bill Buzbeec6f10662010-02-09 11:16:15 -08002983 RegLocation rlThis = dvmCompilerGetSrc(cUnit, mir, 0);
2984 RegLocation rlComp = dvmCompilerGetSrc(cUnit, mir, 1);
Bill Buzbeefd023aa2009-11-02 09:23:49 -08002985
2986 loadValueDirectFixed(cUnit, rlThis, r0);
2987 loadValueDirectFixed(cUnit, rlComp, r1);
2988 /* Test objects for NULL */
2989 rollback = genNullCheck(cUnit, rlThis.sRegLow, r0, mir->offset, NULL);
2990 genNullCheck(cUnit, rlComp.sRegLow, r1, mir->offset, rollback);
2991 /*
2992 * TUNING: we could check for object pointer equality before invoking
2993 * handler. Unclear whether the gain would be worth the added code size
2994 * expansion.
2995 */
2996 genDispatchToHandler(cUnit, TEMPLATE_STRING_COMPARETO);
Bill Buzbeec6f10662010-02-09 11:16:15 -08002997 storeValue(cUnit, inlinedTarget(cUnit, mir, false),
2998 dvmCompilerGetReturn(cUnit));
Bill Buzbeefd023aa2009-11-02 09:23:49 -08002999 return true;
3000#endif
3001}
3002
Elliott Hughes2bdbcb62010-04-12 14:29:37 -07003003static bool genInlinedFastIndexOf(CompilationUnit *cUnit, MIR *mir)
Bill Buzbeefd023aa2009-11-02 09:23:49 -08003004{
3005#if defined(USE_GLOBAL_STRING_DEFS)
3006 return false;
3007#else
Bill Buzbeec6f10662010-02-09 11:16:15 -08003008 RegLocation rlThis = dvmCompilerGetSrc(cUnit, mir, 0);
3009 RegLocation rlChar = dvmCompilerGetSrc(cUnit, mir, 1);
Bill Buzbeefd023aa2009-11-02 09:23:49 -08003010
3011 loadValueDirectFixed(cUnit, rlThis, r0);
3012 loadValueDirectFixed(cUnit, rlChar, r1);
Elliott Hughes2bdbcb62010-04-12 14:29:37 -07003013 RegLocation rlStart = dvmCompilerGetSrc(cUnit, mir, 2);
3014 loadValueDirectFixed(cUnit, rlStart, r2);
Bill Buzbeefd023aa2009-11-02 09:23:49 -08003015 /* Test objects for NULL */
3016 genNullCheck(cUnit, rlThis.sRegLow, r0, mir->offset, NULL);
3017 genDispatchToHandler(cUnit, TEMPLATE_STRING_INDEXOF);
Bill Buzbeec6f10662010-02-09 11:16:15 -08003018 storeValue(cUnit, inlinedTarget(cUnit, mir, false),
3019 dvmCompilerGetReturn(cUnit));
Bill Buzbeefd023aa2009-11-02 09:23:49 -08003020 return true;
3021#endif
3022}
3023
Elliott Hughesee34f592010-04-05 18:13:52 -07003024// Generates an inlined String.isEmpty or String.length.
3025static bool genInlinedStringIsEmptyOrLength(CompilationUnit *cUnit, MIR *mir,
3026 bool isEmpty)
Bill Buzbee1f748632010-03-02 16:14:41 -08003027{
Elliott Hughesee34f592010-04-05 18:13:52 -07003028 // dst = src.length();
Bill Buzbee1f748632010-03-02 16:14:41 -08003029 RegLocation rlObj = dvmCompilerGetSrc(cUnit, mir, 0);
3030 RegLocation rlDest = inlinedTarget(cUnit, mir, false);
3031 rlObj = loadValue(cUnit, rlObj, kCoreReg);
3032 RegLocation rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
3033 genNullCheck(cUnit, rlObj.sRegLow, rlObj.lowReg, mir->offset, NULL);
3034 loadWordDisp(cUnit, rlObj.lowReg, gDvm.offJavaLangString_count,
3035 rlResult.lowReg);
Elliott Hughesee34f592010-04-05 18:13:52 -07003036 if (isEmpty) {
3037 // dst = (dst == 0);
3038 int tReg = dvmCompilerAllocTemp(cUnit);
3039 opRegReg(cUnit, kOpNeg, tReg, rlResult.lowReg);
3040 opRegRegReg(cUnit, kOpAdc, rlResult.lowReg, rlResult.lowReg, tReg);
3041 }
Bill Buzbee1f748632010-03-02 16:14:41 -08003042 storeValue(cUnit, rlDest, rlResult);
3043 return false;
3044}
3045
Elliott Hughesee34f592010-04-05 18:13:52 -07003046static bool genInlinedStringLength(CompilationUnit *cUnit, MIR *mir)
3047{
3048 return genInlinedStringIsEmptyOrLength(cUnit, mir, false);
3049}
3050
3051static bool genInlinedStringIsEmpty(CompilationUnit *cUnit, MIR *mir)
3052{
3053 return genInlinedStringIsEmptyOrLength(cUnit, mir, true);
3054}
3055
Bill Buzbee1f748632010-03-02 16:14:41 -08003056static bool genInlinedStringCharAt(CompilationUnit *cUnit, MIR *mir)
3057{
3058 int contents = offsetof(ArrayObject, contents);
3059 RegLocation rlObj = dvmCompilerGetSrc(cUnit, mir, 0);
3060 RegLocation rlIdx = dvmCompilerGetSrc(cUnit, mir, 1);
3061 RegLocation rlDest = inlinedTarget(cUnit, mir, false);
3062 RegLocation rlResult;
3063 rlObj = loadValue(cUnit, rlObj, kCoreReg);
3064 rlIdx = loadValue(cUnit, rlIdx, kCoreReg);
3065 int regMax = dvmCompilerAllocTemp(cUnit);
3066 int regOff = dvmCompilerAllocTemp(cUnit);
3067 int regPtr = dvmCompilerAllocTemp(cUnit);
3068 ArmLIR *pcrLabel = genNullCheck(cUnit, rlObj.sRegLow, rlObj.lowReg,
3069 mir->offset, NULL);
3070 loadWordDisp(cUnit, rlObj.lowReg, gDvm.offJavaLangString_count, regMax);
3071 loadWordDisp(cUnit, rlObj.lowReg, gDvm.offJavaLangString_offset, regOff);
3072 loadWordDisp(cUnit, rlObj.lowReg, gDvm.offJavaLangString_value, regPtr);
3073 genBoundsCheck(cUnit, rlIdx.lowReg, regMax, mir->offset, pcrLabel);
3074 dvmCompilerFreeTemp(cUnit, regMax);
3075 opRegImm(cUnit, kOpAdd, regPtr, contents);
3076 opRegReg(cUnit, kOpAdd, regOff, rlIdx.lowReg);
3077 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
3078 loadBaseIndexed(cUnit, regPtr, regOff, rlResult.lowReg, 1, kUnsignedHalf);
3079 storeValue(cUnit, rlDest, rlResult);
3080 return false;
3081}
3082
3083static bool genInlinedAbsInt(CompilationUnit *cUnit, MIR *mir)
3084{
3085 RegLocation rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
3086 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
3087 RegLocation rlDest = inlinedTarget(cUnit, mir, false);;
3088 RegLocation rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
3089 int signReg = dvmCompilerAllocTemp(cUnit);
3090 /*
3091 * abs(x) = y<=x>>31, (x+y)^y.
3092 * Thumb2's IT block also yields 3 instructions, but imposes
3093 * scheduling constraints.
3094 */
3095 opRegRegImm(cUnit, kOpAsr, signReg, rlSrc.lowReg, 31);
3096 opRegRegReg(cUnit, kOpAdd, rlResult.lowReg, rlSrc.lowReg, signReg);
3097 opRegReg(cUnit, kOpXor, rlResult.lowReg, signReg);
3098 storeValue(cUnit, rlDest, rlResult);
3099 return false;
3100}
3101
3102static bool genInlinedAbsLong(CompilationUnit *cUnit, MIR *mir)
3103{
3104 RegLocation rlSrc = dvmCompilerGetSrcWide(cUnit, mir, 0, 1);
3105 RegLocation rlDest = inlinedTargetWide(cUnit, mir, false);
3106 rlSrc = loadValueWide(cUnit, rlSrc, kCoreReg);
3107 RegLocation rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
3108 int signReg = dvmCompilerAllocTemp(cUnit);
3109 /*
3110 * abs(x) = y<=x>>31, (x+y)^y.
3111 * Thumb2 IT block allows slightly shorter sequence,
3112 * but introduces a scheduling barrier. Stick with this
3113 * mechanism for now.
3114 */
3115 opRegRegImm(cUnit, kOpAsr, signReg, rlSrc.highReg, 31);
3116 opRegRegReg(cUnit, kOpAdd, rlResult.lowReg, rlSrc.lowReg, signReg);
3117 opRegRegReg(cUnit, kOpAdc, rlResult.highReg, rlSrc.highReg, signReg);
3118 opRegReg(cUnit, kOpXor, rlResult.lowReg, signReg);
3119 opRegReg(cUnit, kOpXor, rlResult.highReg, signReg);
3120 storeValueWide(cUnit, rlDest, rlResult);
3121 return false;
3122}
Bill Buzbeefd023aa2009-11-02 09:23:49 -08003123
3124/*
Bill Buzbeece46c942009-11-20 15:41:34 -08003125 * NOTE: Handles both range and non-range versions (arguments
3126 * have already been normalized by this point).
Ben Chengba4fc8b2009-06-01 13:00:29 -07003127 */
Bill Buzbeece46c942009-11-20 15:41:34 -08003128static bool handleExecuteInline(CompilationUnit *cUnit, MIR *mir)
Ben Chengba4fc8b2009-06-01 13:00:29 -07003129{
3130 DecodedInstruction *dInsn = &mir->dalvikInsn;
3131 switch( mir->dalvikInsn.opCode) {
Bill Buzbeece46c942009-11-20 15:41:34 -08003132 case OP_EXECUTE_INLINE_RANGE:
Ben Chengba4fc8b2009-06-01 13:00:29 -07003133 case OP_EXECUTE_INLINE: {
3134 unsigned int i;
3135 const InlineOperation* inLineTable = dvmGetInlineOpsTable();
Bill Buzbee50a6bf22009-07-08 13:08:04 -07003136 int offset = offsetof(InterpState, retval);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003137 int operation = dInsn->vB;
Bill Buzbee1465db52009-09-23 17:17:35 -07003138 int tReg1;
3139 int tReg2;
Bill Buzbee50a6bf22009-07-08 13:08:04 -07003140 switch (operation) {
3141 case INLINE_EMPTYINLINEMETHOD:
3142 return false; /* Nop */
3143 case INLINE_STRING_LENGTH:
3144 return genInlinedStringLength(cUnit, mir);
Elliott Hughesee34f592010-04-05 18:13:52 -07003145 case INLINE_STRING_IS_EMPTY:
3146 return genInlinedStringIsEmpty(cUnit, mir);
Bill Buzbee50a6bf22009-07-08 13:08:04 -07003147 case INLINE_MATH_ABS_INT:
3148 return genInlinedAbsInt(cUnit, mir);
3149 case INLINE_MATH_ABS_LONG:
3150 return genInlinedAbsLong(cUnit, mir);
3151 case INLINE_MATH_MIN_INT:
3152 return genInlinedMinMaxInt(cUnit, mir, true);
3153 case INLINE_MATH_MAX_INT:
3154 return genInlinedMinMaxInt(cUnit, mir, false);
3155 case INLINE_STRING_CHARAT:
3156 return genInlinedStringCharAt(cUnit, mir);
3157 case INLINE_MATH_SQRT:
3158 if (genInlineSqrt(cUnit, mir))
Bill Buzbee9727c3d2009-08-01 11:32:36 -07003159 return false;
Bill Buzbee50a6bf22009-07-08 13:08:04 -07003160 else
3161 break; /* Handle with C routine */
Bill Buzbee50a6bf22009-07-08 13:08:04 -07003162 case INLINE_MATH_ABS_FLOAT:
Bill Buzbee1465db52009-09-23 17:17:35 -07003163 if (genInlinedAbsFloat(cUnit, mir))
3164 return false;
3165 else
3166 break;
Bill Buzbee50a6bf22009-07-08 13:08:04 -07003167 case INLINE_MATH_ABS_DOUBLE:
Bill Buzbee1465db52009-09-23 17:17:35 -07003168 if (genInlinedAbsDouble(cUnit, mir))
3169 return false;
3170 else
3171 break;
Bill Buzbee50a6bf22009-07-08 13:08:04 -07003172 case INLINE_STRING_COMPARETO:
Bill Buzbeefd023aa2009-11-02 09:23:49 -08003173 if (genInlinedCompareTo(cUnit, mir))
3174 return false;
3175 else
3176 break;
Elliott Hughes2bdbcb62010-04-12 14:29:37 -07003177 case INLINE_STRING_FASTINDEXOF_II:
3178 if (genInlinedFastIndexOf(cUnit, mir))
Bill Buzbeefd023aa2009-11-02 09:23:49 -08003179 return false;
3180 else
3181 break;
3182 case INLINE_STRING_EQUALS:
3183 case INLINE_MATH_COS:
3184 case INLINE_MATH_SIN:
3185 break; /* Handle with C routine */
Bill Buzbee50a6bf22009-07-08 13:08:04 -07003186 default:
Bill Buzbeefc519dc2010-03-06 23:30:57 -08003187 dvmCompilerAbort(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003188 }
Bill Buzbeec6f10662010-02-09 11:16:15 -08003189 dvmCompilerFlushAllRegs(cUnit); /* Everything to home location */
Elliott Hughes6a555132010-02-25 15:41:42 -08003190 dvmCompilerClobberCallRegs(cUnit);
Bill Buzbeec6f10662010-02-09 11:16:15 -08003191 dvmCompilerClobber(cUnit, r4PC);
3192 dvmCompilerClobber(cUnit, r7);
Bill Buzbee1465db52009-09-23 17:17:35 -07003193 opRegRegImm(cUnit, kOpAdd, r4PC, rGLUE, offset);
3194 opImm(cUnit, kOpPush, (1<<r4PC) | (1<<r7));
Ben Chengbd1326d2010-04-02 15:04:53 -07003195 LOAD_FUNC_ADDR(cUnit, r4PC, (int)inLineTable[operation].func);
Bill Buzbee1465db52009-09-23 17:17:35 -07003196 genExportPC(cUnit, mir);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003197 for (i=0; i < dInsn->vA; i++) {
Bill Buzbeec6f10662010-02-09 11:16:15 -08003198 loadValueDirect(cUnit, dvmCompilerGetSrc(cUnit, mir, i), i);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003199 }
Bill Buzbee1465db52009-09-23 17:17:35 -07003200 opReg(cUnit, kOpBlx, r4PC);
3201 opRegImm(cUnit, kOpAdd, r13, 8);
Bill Buzbeece46c942009-11-20 15:41:34 -08003202 opRegImm(cUnit, kOpCmp, r0, 0); /* NULL? */
3203 ArmLIR *branchOver = opCondBranch(cUnit, kArmCondNe);
3204 loadConstant(cUnit, r0,
3205 (int) (cUnit->method->insns + mir->offset));
3206 genDispatchToHandler(cUnit, TEMPLATE_THROW_EXCEPTION_COMMON);
3207 ArmLIR *target = newLIR0(cUnit, kArmPseudoTargetLabel);
3208 target->defMask = ENCODE_ALL;
3209 branchOver->generic.target = (LIR *) target;
Ben Chengba4fc8b2009-06-01 13:00:29 -07003210 break;
3211 }
3212 default:
3213 return true;
3214 }
3215 return false;
3216}
3217
3218static bool handleFmt51l(CompilationUnit *cUnit, MIR *mir)
3219{
Bill Buzbee1465db52009-09-23 17:17:35 -07003220 //TUNING: We're using core regs here - not optimal when target is a double
Bill Buzbeec6f10662010-02-09 11:16:15 -08003221 RegLocation rlDest = dvmCompilerGetDestWide(cUnit, mir, 0, 1);
3222 RegLocation rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Ben Chengbd1326d2010-04-02 15:04:53 -07003223 loadConstantNoClobber(cUnit, rlResult.lowReg,
3224 mir->dalvikInsn.vB_wide & 0xFFFFFFFFUL);
3225 loadConstantNoClobber(cUnit, rlResult.highReg,
3226 (mir->dalvikInsn.vB_wide>>32) & 0xFFFFFFFFUL);
Bill Buzbee1465db52009-09-23 17:17:35 -07003227 storeValueWide(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003228 return false;
3229}
3230
Ben Chengba4fc8b2009-06-01 13:00:29 -07003231/*
3232 * The following are special processing routines that handle transfer of
3233 * controls between compiled code and the interpreter. Certain VM states like
3234 * Dalvik PC and special-purpose registers are reconstructed here.
3235 */
3236
Ben Cheng1efc9c52009-06-08 18:25:27 -07003237/* Chaining cell for code that may need warmup. */
3238static void handleNormalChainingCell(CompilationUnit *cUnit,
3239 unsigned int offset)
Ben Chengba4fc8b2009-06-01 13:00:29 -07003240{
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 */
3245 newLIR3(cUnit, kThumbLdrRRI5, r0, rGLUE,
3246 offsetof(InterpState,
3247 jitToInterpEntries.dvmJitToInterpNormal) >> 2);
3248 newLIR1(cUnit, kThumbBlxR, r0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003249 addWordData(cUnit, (int) (cUnit->method->insns + offset), true);
3250}
3251
3252/*
Ben Cheng1efc9c52009-06-08 18:25:27 -07003253 * Chaining cell for instructions that immediately following already translated
3254 * code.
Ben Chengba4fc8b2009-06-01 13:00:29 -07003255 */
Ben Cheng1efc9c52009-06-08 18:25:27 -07003256static void handleHotChainingCell(CompilationUnit *cUnit,
3257 unsigned int offset)
Ben Chengba4fc8b2009-06-01 13:00:29 -07003258{
Ben Cheng11d8f142010-03-24 15:24:19 -07003259 /*
3260 * Use raw instruction constructors to guarantee that the generated
3261 * instructions fit the predefined cell size.
3262 */
3263 newLIR3(cUnit, kThumbLdrRRI5, r0, rGLUE,
3264 offsetof(InterpState,
3265 jitToInterpEntries.dvmJitToInterpTraceSelect) >> 2);
3266 newLIR1(cUnit, kThumbBlxR, r0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003267 addWordData(cUnit, (int) (cUnit->method->insns + offset), true);
3268}
3269
Bill Buzbee9c4b7c82009-09-10 10:10:38 -07003270#if defined(WITH_SELF_VERIFICATION) || defined(WITH_JIT_TUNING)
Jeff Hao97319a82009-08-12 16:57:15 -07003271/* Chaining cell for branches that branch back into the same basic block */
3272static void handleBackwardBranchChainingCell(CompilationUnit *cUnit,
3273 unsigned int offset)
3274{
Ben Cheng11d8f142010-03-24 15:24:19 -07003275 /*
3276 * Use raw instruction constructors to guarantee that the generated
3277 * instructions fit the predefined cell size.
3278 */
Bill Buzbee9c4b7c82009-09-10 10:10:38 -07003279#if defined(WITH_SELF_VERIFICATION)
Bill Buzbee1465db52009-09-23 17:17:35 -07003280 newLIR3(cUnit, kThumbLdrRRI5, r0, rGLUE,
Ben Cheng40094c12010-02-24 20:58:44 -08003281 offsetof(InterpState,
3282 jitToInterpEntries.dvmJitToInterpBackwardBranch) >> 2);
Bill Buzbee9c4b7c82009-09-10 10:10:38 -07003283#else
Bill Buzbee1465db52009-09-23 17:17:35 -07003284 newLIR3(cUnit, kThumbLdrRRI5, r0, rGLUE,
Bill Buzbee9c4b7c82009-09-10 10:10:38 -07003285 offsetof(InterpState, jitToInterpEntries.dvmJitToInterpNormal) >> 2);
3286#endif
Bill Buzbee1465db52009-09-23 17:17:35 -07003287 newLIR1(cUnit, kThumbBlxR, r0);
Jeff Hao97319a82009-08-12 16:57:15 -07003288 addWordData(cUnit, (int) (cUnit->method->insns + offset), true);
3289}
3290
3291#endif
Ben Chengba4fc8b2009-06-01 13:00:29 -07003292/* Chaining cell for monomorphic method invocations. */
Ben Cheng38329f52009-07-07 14:19:20 -07003293static void handleInvokeSingletonChainingCell(CompilationUnit *cUnit,
3294 const Method *callee)
Ben Chengba4fc8b2009-06-01 13:00:29 -07003295{
Ben Cheng11d8f142010-03-24 15:24:19 -07003296 /*
3297 * Use raw instruction constructors to guarantee that the generated
3298 * instructions fit the predefined cell size.
3299 */
3300 newLIR3(cUnit, kThumbLdrRRI5, r0, rGLUE,
3301 offsetof(InterpState,
3302 jitToInterpEntries.dvmJitToInterpTraceSelect) >> 2);
3303 newLIR1(cUnit, kThumbBlxR, r0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003304 addWordData(cUnit, (int) (callee->insns), true);
3305}
3306
Ben Cheng38329f52009-07-07 14:19:20 -07003307/* Chaining cell for monomorphic method invocations. */
3308static void handleInvokePredictedChainingCell(CompilationUnit *cUnit)
3309{
3310
3311 /* Should not be executed in the initial state */
3312 addWordData(cUnit, PREDICTED_CHAIN_BX_PAIR_INIT, true);
3313 /* To be filled: class */
3314 addWordData(cUnit, PREDICTED_CHAIN_CLAZZ_INIT, true);
3315 /* To be filled: method */
3316 addWordData(cUnit, PREDICTED_CHAIN_METHOD_INIT, true);
3317 /*
3318 * Rechain count. The initial value of 0 here will trigger chaining upon
3319 * the first invocation of this callsite.
3320 */
3321 addWordData(cUnit, PREDICTED_CHAIN_COUNTER_INIT, true);
3322}
3323
Ben Chengba4fc8b2009-06-01 13:00:29 -07003324/* Load the Dalvik PC into r0 and jump to the specified target */
3325static void handlePCReconstruction(CompilationUnit *cUnit,
Bill Buzbee89efc3d2009-07-28 11:22:22 -07003326 ArmLIR *targetLabel)
Ben Chengba4fc8b2009-06-01 13:00:29 -07003327{
Bill Buzbee89efc3d2009-07-28 11:22:22 -07003328 ArmLIR **pcrLabel =
3329 (ArmLIR **) cUnit->pcReconstructionList.elemList;
Ben Chengba4fc8b2009-06-01 13:00:29 -07003330 int numElems = cUnit->pcReconstructionList.numUsed;
3331 int i;
3332 for (i = 0; i < numElems; i++) {
3333 dvmCompilerAppendLIR(cUnit, (LIR *) pcrLabel[i]);
3334 /* r0 = dalvik PC */
3335 loadConstant(cUnit, r0, pcrLabel[i]->operands[0]);
3336 genUnconditionalBranch(cUnit, targetLabel);
3337 }
3338}
3339
Bill Buzbee1465db52009-09-23 17:17:35 -07003340static char *extendedMIROpNames[kMirOpLast - kMirOpFirst] = {
3341 "kMirOpPhi",
3342 "kMirOpNullNRangeUpCheck",
3343 "kMirOpNullNRangeDownCheck",
3344 "kMirOpLowerBound",
3345 "kMirOpPunt",
Ben Cheng4238ec22009-08-24 16:32:22 -07003346};
3347
3348/*
3349 * vA = arrayReg;
3350 * vB = idxReg;
3351 * vC = endConditionReg;
3352 * arg[0] = maxC
3353 * arg[1] = minC
3354 * arg[2] = loopBranchConditionCode
3355 */
3356static void genHoistedChecksForCountUpLoop(CompilationUnit *cUnit, MIR *mir)
3357{
Bill Buzbee1465db52009-09-23 17:17:35 -07003358 /*
3359 * NOTE: these synthesized blocks don't have ssa names assigned
3360 * for Dalvik registers. However, because they dominate the following
3361 * blocks we can simply use the Dalvik name w/ subscript 0 as the
3362 * ssa name.
3363 */
Ben Cheng4238ec22009-08-24 16:32:22 -07003364 DecodedInstruction *dInsn = &mir->dalvikInsn;
3365 const int lenOffset = offsetof(ArrayObject, length);
Ben Cheng4238ec22009-08-24 16:32:22 -07003366 const int maxC = dInsn->arg[0];
3367 const int minC = dInsn->arg[1];
Bill Buzbee1465db52009-09-23 17:17:35 -07003368 int regLength;
3369 RegLocation rlArray = cUnit->regLocation[mir->dalvikInsn.vA];
3370 RegLocation rlIdxEnd = cUnit->regLocation[mir->dalvikInsn.vC];
Ben Cheng4238ec22009-08-24 16:32:22 -07003371
3372 /* regArray <- arrayRef */
Bill Buzbee1465db52009-09-23 17:17:35 -07003373 rlArray = loadValue(cUnit, rlArray, kCoreReg);
3374 rlIdxEnd = loadValue(cUnit, rlIdxEnd, kCoreReg);
3375 genRegImmCheck(cUnit, kArmCondEq, rlArray.lowReg, 0, 0,
Ben Cheng4238ec22009-08-24 16:32:22 -07003376 (ArmLIR *) cUnit->loopAnalysis->branchToPCR);
3377
3378 /* regLength <- len(arrayRef) */
Bill Buzbeec6f10662010-02-09 11:16:15 -08003379 regLength = dvmCompilerAllocTemp(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07003380 loadWordDisp(cUnit, rlArray.lowReg, lenOffset, regLength);
Ben Cheng4238ec22009-08-24 16:32:22 -07003381
3382 int delta = maxC;
3383 /*
3384 * If the loop end condition is ">=" instead of ">", then the largest value
3385 * of the index is "endCondition - 1".
3386 */
3387 if (dInsn->arg[2] == OP_IF_GE) {
3388 delta--;
3389 }
3390
3391 if (delta) {
Bill Buzbeec6f10662010-02-09 11:16:15 -08003392 int tReg = dvmCompilerAllocTemp(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07003393 opRegRegImm(cUnit, kOpAdd, tReg, rlIdxEnd.lowReg, delta);
3394 rlIdxEnd.lowReg = tReg;
Bill Buzbeec6f10662010-02-09 11:16:15 -08003395 dvmCompilerFreeTemp(cUnit, tReg);
Ben Cheng4238ec22009-08-24 16:32:22 -07003396 }
3397 /* Punt if "regIdxEnd < len(Array)" is false */
Bill Buzbee1465db52009-09-23 17:17:35 -07003398 genRegRegCheck(cUnit, kArmCondGe, rlIdxEnd.lowReg, regLength, 0,
Ben Cheng0fd31e42009-09-03 14:40:16 -07003399 (ArmLIR *) cUnit->loopAnalysis->branchToPCR);
Ben Cheng4238ec22009-08-24 16:32:22 -07003400}
3401
3402/*
3403 * vA = arrayReg;
3404 * vB = idxReg;
3405 * vC = endConditionReg;
3406 * arg[0] = maxC
3407 * arg[1] = minC
3408 * arg[2] = loopBranchConditionCode
3409 */
3410static void genHoistedChecksForCountDownLoop(CompilationUnit *cUnit, MIR *mir)
3411{
3412 DecodedInstruction *dInsn = &mir->dalvikInsn;
3413 const int lenOffset = offsetof(ArrayObject, length);
Bill Buzbeec6f10662010-02-09 11:16:15 -08003414 const int regLength = dvmCompilerAllocTemp(cUnit);
Ben Cheng4238ec22009-08-24 16:32:22 -07003415 const int maxC = dInsn->arg[0];
3416 const int minC = dInsn->arg[1];
Bill Buzbee1465db52009-09-23 17:17:35 -07003417 RegLocation rlArray = cUnit->regLocation[mir->dalvikInsn.vA];
3418 RegLocation rlIdxInit = cUnit->regLocation[mir->dalvikInsn.vB];
Ben Cheng4238ec22009-08-24 16:32:22 -07003419
3420 /* regArray <- arrayRef */
Bill Buzbee1465db52009-09-23 17:17:35 -07003421 rlArray = loadValue(cUnit, rlArray, kCoreReg);
3422 rlIdxInit = loadValue(cUnit, rlIdxInit, kCoreReg);
3423 genRegImmCheck(cUnit, kArmCondEq, rlArray.lowReg, 0, 0,
Ben Cheng4238ec22009-08-24 16:32:22 -07003424 (ArmLIR *) cUnit->loopAnalysis->branchToPCR);
3425
3426 /* regLength <- len(arrayRef) */
Bill Buzbee1465db52009-09-23 17:17:35 -07003427 loadWordDisp(cUnit, rlArray.lowReg, lenOffset, regLength);
Ben Cheng4238ec22009-08-24 16:32:22 -07003428
3429 if (maxC) {
Bill Buzbeec6f10662010-02-09 11:16:15 -08003430 int tReg = dvmCompilerAllocTemp(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07003431 opRegRegImm(cUnit, kOpAdd, tReg, rlIdxInit.lowReg, maxC);
3432 rlIdxInit.lowReg = tReg;
Bill Buzbeec6f10662010-02-09 11:16:15 -08003433 dvmCompilerFreeTemp(cUnit, tReg);
Ben Cheng4238ec22009-08-24 16:32:22 -07003434 }
3435
3436 /* Punt if "regIdxInit < len(Array)" is false */
Bill Buzbee1465db52009-09-23 17:17:35 -07003437 genRegRegCheck(cUnit, kArmCondGe, rlIdxInit.lowReg, regLength, 0,
Ben Cheng0fd31e42009-09-03 14:40:16 -07003438 (ArmLIR *) cUnit->loopAnalysis->branchToPCR);
Ben Cheng4238ec22009-08-24 16:32:22 -07003439}
3440
3441/*
3442 * vA = idxReg;
3443 * vB = minC;
3444 */
3445static void genHoistedLowerBoundCheck(CompilationUnit *cUnit, MIR *mir)
3446{
3447 DecodedInstruction *dInsn = &mir->dalvikInsn;
Ben Cheng4238ec22009-08-24 16:32:22 -07003448 const int minC = dInsn->vB;
Bill Buzbee1465db52009-09-23 17:17:35 -07003449 RegLocation rlIdx = cUnit->regLocation[mir->dalvikInsn.vA];
Ben Cheng4238ec22009-08-24 16:32:22 -07003450
3451 /* regIdx <- initial index value */
Bill Buzbee1465db52009-09-23 17:17:35 -07003452 rlIdx = loadValue(cUnit, rlIdx, kCoreReg);
Ben Cheng4238ec22009-08-24 16:32:22 -07003453
3454 /* Punt if "regIdxInit + minC >= 0" is false */
Bill Buzbee1465db52009-09-23 17:17:35 -07003455 genRegImmCheck(cUnit, kArmCondLt, rlIdx.lowReg, -minC, 0,
Ben Cheng4238ec22009-08-24 16:32:22 -07003456 (ArmLIR *) cUnit->loopAnalysis->branchToPCR);
3457}
3458
3459/* Extended MIR instructions like PHI */
3460static void handleExtendedMIR(CompilationUnit *cUnit, MIR *mir)
3461{
Bill Buzbee1465db52009-09-23 17:17:35 -07003462 int opOffset = mir->dalvikInsn.opCode - kMirOpFirst;
Ben Cheng4238ec22009-08-24 16:32:22 -07003463 char *msg = dvmCompilerNew(strlen(extendedMIROpNames[opOffset]) + 1,
3464 false);
3465 strcpy(msg, extendedMIROpNames[opOffset]);
Bill Buzbee1465db52009-09-23 17:17:35 -07003466 newLIR1(cUnit, kArmPseudoExtended, (int) msg);
Ben Cheng4238ec22009-08-24 16:32:22 -07003467
3468 switch (mir->dalvikInsn.opCode) {
Bill Buzbee1465db52009-09-23 17:17:35 -07003469 case kMirOpPhi: {
Ben Cheng4238ec22009-08-24 16:32:22 -07003470 char *ssaString = dvmCompilerGetSSAString(cUnit, mir->ssaRep);
Bill Buzbee1465db52009-09-23 17:17:35 -07003471 newLIR1(cUnit, kArmPseudoSSARep, (int) ssaString);
Ben Cheng4238ec22009-08-24 16:32:22 -07003472 break;
3473 }
Bill Buzbee1465db52009-09-23 17:17:35 -07003474 case kMirOpNullNRangeUpCheck: {
Ben Cheng4238ec22009-08-24 16:32:22 -07003475 genHoistedChecksForCountUpLoop(cUnit, mir);
3476 break;
3477 }
Bill Buzbee1465db52009-09-23 17:17:35 -07003478 case kMirOpNullNRangeDownCheck: {
Ben Cheng4238ec22009-08-24 16:32:22 -07003479 genHoistedChecksForCountDownLoop(cUnit, mir);
3480 break;
3481 }
Bill Buzbee1465db52009-09-23 17:17:35 -07003482 case kMirOpLowerBound: {
Ben Cheng4238ec22009-08-24 16:32:22 -07003483 genHoistedLowerBoundCheck(cUnit, mir);
3484 break;
3485 }
Bill Buzbee1465db52009-09-23 17:17:35 -07003486 case kMirOpPunt: {
Ben Cheng4238ec22009-08-24 16:32:22 -07003487 genUnconditionalBranch(cUnit,
3488 (ArmLIR *) cUnit->loopAnalysis->branchToPCR);
3489 break;
3490 }
3491 default:
3492 break;
3493 }
3494}
3495
3496/*
3497 * Create a PC-reconstruction cell for the starting offset of this trace.
3498 * Since the PCR cell is placed near the end of the compiled code which is
3499 * usually out of range for a conditional branch, we put two branches (one
3500 * branch over to the loop body and one layover branch to the actual PCR) at the
3501 * end of the entry block.
3502 */
3503static void setupLoopEntryBlock(CompilationUnit *cUnit, BasicBlock *entry,
3504 ArmLIR *bodyLabel)
3505{
3506 /* Set up the place holder to reconstruct this Dalvik PC */
3507 ArmLIR *pcrLabel = dvmCompilerNew(sizeof(ArmLIR), true);
Ben Chenga4973592010-03-31 11:59:18 -07003508 pcrLabel->opCode = kArmPseudoPCReconstructionCell;
Ben Cheng4238ec22009-08-24 16:32:22 -07003509 pcrLabel->operands[0] =
3510 (int) (cUnit->method->insns + entry->startOffset);
3511 pcrLabel->operands[1] = entry->startOffset;
3512 /* Insert the place holder to the growable list */
3513 dvmInsertGrowableList(&cUnit->pcReconstructionList, pcrLabel);
3514
3515 /*
3516 * Next, create two branches - one branch over to the loop body and the
3517 * other branch to the PCR cell to punt.
3518 */
3519 ArmLIR *branchToBody = dvmCompilerNew(sizeof(ArmLIR), true);
Bill Buzbee1465db52009-09-23 17:17:35 -07003520 branchToBody->opCode = kThumbBUncond;
Ben Cheng4238ec22009-08-24 16:32:22 -07003521 branchToBody->generic.target = (LIR *) bodyLabel;
Ben Chengdcf3e5d2009-09-11 13:42:05 -07003522 setupResourceMasks(branchToBody);
Ben Cheng4238ec22009-08-24 16:32:22 -07003523 cUnit->loopAnalysis->branchToBody = (LIR *) branchToBody;
3524
3525 ArmLIR *branchToPCR = dvmCompilerNew(sizeof(ArmLIR), true);
Bill Buzbee1465db52009-09-23 17:17:35 -07003526 branchToPCR->opCode = kThumbBUncond;
Ben Cheng4238ec22009-08-24 16:32:22 -07003527 branchToPCR->generic.target = (LIR *) pcrLabel;
Ben Chengdcf3e5d2009-09-11 13:42:05 -07003528 setupResourceMasks(branchToPCR);
Ben Cheng4238ec22009-08-24 16:32:22 -07003529 cUnit->loopAnalysis->branchToPCR = (LIR *) branchToPCR;
3530}
3531
Ben Chengd5adae12010-03-26 17:45:28 -07003532#if defined(WITH_SELF_VERIFICATION)
3533static bool selfVerificationPuntOps(MIR *mir)
3534{
3535 DecodedInstruction *decInsn = &mir->dalvikInsn;
3536 OpCode op = decInsn->opCode;
3537 int flags = dexGetInstrFlags(gDvm.instrFlags, op);
3538 /*
3539 * All opcodes that can throw exceptions and use the
3540 * TEMPLATE_THROW_EXCEPTION_COMMON template should be excluded in the trace
3541 * under self-verification mode.
3542 */
3543 return (op == OP_MONITOR_ENTER || op == OP_MONITOR_EXIT ||
3544 op == OP_NEW_INSTANCE || op == OP_NEW_ARRAY ||
3545 op == OP_CHECK_CAST || op == OP_MOVE_EXCEPTION ||
3546 op == OP_FILL_ARRAY_DATA || op == OP_EXECUTE_INLINE ||
3547 op == OP_EXECUTE_INLINE_RANGE ||
3548 (flags & kInstrInvoke));
3549}
3550#endif
3551
Ben Chengba4fc8b2009-06-01 13:00:29 -07003552void dvmCompilerMIR2LIR(CompilationUnit *cUnit)
3553{
3554 /* Used to hold the labels of each block */
Bill Buzbee89efc3d2009-07-28 11:22:22 -07003555 ArmLIR *labelList =
3556 dvmCompilerNew(sizeof(ArmLIR) * cUnit->numBlocks, true);
Ben Chengcec26f62010-01-15 15:29:33 -08003557 GrowableList chainingListByType[kChainingCellGap];
Ben Chengba4fc8b2009-06-01 13:00:29 -07003558 int i;
3559
3560 /*
Ben Cheng38329f52009-07-07 14:19:20 -07003561 * Initialize various types chaining lists.
Ben Chengba4fc8b2009-06-01 13:00:29 -07003562 */
Ben Chengcec26f62010-01-15 15:29:33 -08003563 for (i = 0; i < kChainingCellGap; i++) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07003564 dvmInitGrowableList(&chainingListByType[i], 2);
3565 }
3566
3567 BasicBlock **blockList = cUnit->blockList;
3568
Bill Buzbee6e963e12009-06-17 16:56:19 -07003569 if (cUnit->executionCount) {
3570 /*
3571 * Reserve 6 bytes at the beginning of the trace
3572 * +----------------------------+
3573 * | execution count (4 bytes) |
3574 * +----------------------------+
3575 * | chain cell offset (2 bytes)|
3576 * +----------------------------+
3577 * ...and then code to increment the execution
3578 * count:
3579 * mov r0, pc @ move adr of "mov r0,pc" + 4 to r0
3580 * sub r0, #10 @ back up to addr of executionCount
3581 * ldr r1, [r0]
3582 * add r1, #1
3583 * str r1, [r0]
3584 */
Bill Buzbee1465db52009-09-23 17:17:35 -07003585 newLIR1(cUnit, kArm16BitData, 0);
3586 newLIR1(cUnit, kArm16BitData, 0);
Ben Chengcc6600c2009-06-22 14:45:16 -07003587 cUnit->chainCellOffsetLIR =
Bill Buzbee1465db52009-09-23 17:17:35 -07003588 (LIR *) newLIR1(cUnit, kArm16BitData, CHAIN_CELL_OFFSET_TAG);
Bill Buzbee6e963e12009-06-17 16:56:19 -07003589 cUnit->headerSize = 6;
Bill Buzbee270c1d62009-08-13 16:58:07 -07003590 /* Thumb instruction used directly here to ensure correct size */
Bill Buzbee1465db52009-09-23 17:17:35 -07003591 newLIR2(cUnit, kThumbMovRR_H2L, r0, rpc);
3592 newLIR2(cUnit, kThumbSubRI8, r0, 10);
3593 newLIR3(cUnit, kThumbLdrRRI5, r1, r0, 0);
3594 newLIR2(cUnit, kThumbAddRI8, r1, 1);
3595 newLIR3(cUnit, kThumbStrRRI5, r1, r0, 0);
Bill Buzbee6e963e12009-06-17 16:56:19 -07003596 } else {
3597 /* Just reserve 2 bytes for the chain cell offset */
Ben Chengcc6600c2009-06-22 14:45:16 -07003598 cUnit->chainCellOffsetLIR =
Bill Buzbee1465db52009-09-23 17:17:35 -07003599 (LIR *) newLIR1(cUnit, kArm16BitData, CHAIN_CELL_OFFSET_TAG);
Bill Buzbee6e963e12009-06-17 16:56:19 -07003600 cUnit->headerSize = 2;
3601 }
Ben Cheng1efc9c52009-06-08 18:25:27 -07003602
Ben Chengba4fc8b2009-06-01 13:00:29 -07003603 /* Handle the content in each basic block */
3604 for (i = 0; i < cUnit->numBlocks; i++) {
3605 blockList[i]->visited = true;
3606 MIR *mir;
3607
3608 labelList[i].operands[0] = blockList[i]->startOffset;
3609
Ben Chengcec26f62010-01-15 15:29:33 -08003610 if (blockList[i]->blockType >= kChainingCellGap) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07003611 /*
3612 * Append the label pseudo LIR first. Chaining cells will be handled
3613 * separately afterwards.
3614 */
3615 dvmCompilerAppendLIR(cUnit, (LIR *) &labelList[i]);
3616 }
3617
Bill Buzbee1465db52009-09-23 17:17:35 -07003618 if (blockList[i]->blockType == kEntryBlock) {
Ben Chenga4973592010-03-31 11:59:18 -07003619 labelList[i].opCode = kArmPseudoEntryBlock;
Ben Cheng4238ec22009-08-24 16:32:22 -07003620 if (blockList[i]->firstMIRInsn == NULL) {
3621 continue;
3622 } else {
3623 setupLoopEntryBlock(cUnit, blockList[i],
3624 &labelList[blockList[i]->fallThrough->id]);
3625 }
Bill Buzbee1465db52009-09-23 17:17:35 -07003626 } else if (blockList[i]->blockType == kExitBlock) {
Ben Chenga4973592010-03-31 11:59:18 -07003627 labelList[i].opCode = kArmPseudoExitBlock;
Ben Cheng4238ec22009-08-24 16:32:22 -07003628 goto gen_fallthrough;
Bill Buzbee1465db52009-09-23 17:17:35 -07003629 } else if (blockList[i]->blockType == kDalvikByteCode) {
3630 labelList[i].opCode = kArmPseudoNormalBlockLabel;
Ben Chenge9695e52009-06-16 16:11:47 -07003631 /* Reset the register state */
Bill Buzbeec6f10662010-02-09 11:16:15 -08003632 dvmCompilerResetRegPool(cUnit);
3633 dvmCompilerClobberAllRegs(cUnit);
3634 dvmCompilerResetNullCheck(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003635 } else {
3636 switch (blockList[i]->blockType) {
Bill Buzbee1465db52009-09-23 17:17:35 -07003637 case kChainingCellNormal:
Ben Chenga4973592010-03-31 11:59:18 -07003638 labelList[i].opCode = kArmPseudoChainingCellNormal;
Ben Chengba4fc8b2009-06-01 13:00:29 -07003639 /* handle the codegen later */
3640 dvmInsertGrowableList(
Bill Buzbee1465db52009-09-23 17:17:35 -07003641 &chainingListByType[kChainingCellNormal], (void *) i);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003642 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07003643 case kChainingCellInvokeSingleton:
Ben Cheng38329f52009-07-07 14:19:20 -07003644 labelList[i].opCode =
Ben Chenga4973592010-03-31 11:59:18 -07003645 kArmPseudoChainingCellInvokeSingleton;
Ben Chengba4fc8b2009-06-01 13:00:29 -07003646 labelList[i].operands[0] =
3647 (int) blockList[i]->containingMethod;
3648 /* handle the codegen later */
3649 dvmInsertGrowableList(
Bill Buzbee1465db52009-09-23 17:17:35 -07003650 &chainingListByType[kChainingCellInvokeSingleton],
Ben Cheng38329f52009-07-07 14:19:20 -07003651 (void *) i);
3652 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07003653 case kChainingCellInvokePredicted:
Ben Cheng38329f52009-07-07 14:19:20 -07003654 labelList[i].opCode =
Ben Chenga4973592010-03-31 11:59:18 -07003655 kArmPseudoChainingCellInvokePredicted;
Ben Cheng38329f52009-07-07 14:19:20 -07003656 /* handle the codegen later */
3657 dvmInsertGrowableList(
Bill Buzbee1465db52009-09-23 17:17:35 -07003658 &chainingListByType[kChainingCellInvokePredicted],
Ben Cheng38329f52009-07-07 14:19:20 -07003659 (void *) i);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003660 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07003661 case kChainingCellHot:
Ben Chengba4fc8b2009-06-01 13:00:29 -07003662 labelList[i].opCode =
Ben Chenga4973592010-03-31 11:59:18 -07003663 kArmPseudoChainingCellHot;
Ben Chengba4fc8b2009-06-01 13:00:29 -07003664 /* handle the codegen later */
3665 dvmInsertGrowableList(
Bill Buzbee1465db52009-09-23 17:17:35 -07003666 &chainingListByType[kChainingCellHot],
Ben Chengba4fc8b2009-06-01 13:00:29 -07003667 (void *) i);
3668 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07003669 case kPCReconstruction:
Ben Chengba4fc8b2009-06-01 13:00:29 -07003670 /* Make sure exception handling block is next */
3671 labelList[i].opCode =
Ben Chenga4973592010-03-31 11:59:18 -07003672 kArmPseudoPCReconstructionBlockLabel;
Ben Chengba4fc8b2009-06-01 13:00:29 -07003673 assert (i == cUnit->numBlocks - 2);
3674 handlePCReconstruction(cUnit, &labelList[i+1]);
3675 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07003676 case kExceptionHandling:
3677 labelList[i].opCode = kArmPseudoEHBlockLabel;
Ben Chengba4fc8b2009-06-01 13:00:29 -07003678 if (cUnit->pcReconstructionList.numUsed) {
Bill Buzbee270c1d62009-08-13 16:58:07 -07003679 loadWordDisp(cUnit, rGLUE, offsetof(InterpState,
3680 jitToInterpEntries.dvmJitToInterpPunt),
3681 r1);
Bill Buzbee1465db52009-09-23 17:17:35 -07003682 opReg(cUnit, kOpBlx, r1);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003683 }
3684 break;
Bill Buzbee9c4b7c82009-09-10 10:10:38 -07003685#if defined(WITH_SELF_VERIFICATION) || defined(WITH_JIT_TUNING)
Bill Buzbee1465db52009-09-23 17:17:35 -07003686 case kChainingCellBackwardBranch:
Jeff Hao97319a82009-08-12 16:57:15 -07003687 labelList[i].opCode =
Ben Chenga4973592010-03-31 11:59:18 -07003688 kArmPseudoChainingCellBackwardBranch;
Jeff Hao97319a82009-08-12 16:57:15 -07003689 /* handle the codegen later */
3690 dvmInsertGrowableList(
Bill Buzbee1465db52009-09-23 17:17:35 -07003691 &chainingListByType[kChainingCellBackwardBranch],
Jeff Hao97319a82009-08-12 16:57:15 -07003692 (void *) i);
3693 break;
3694#endif
Ben Chengba4fc8b2009-06-01 13:00:29 -07003695 default:
3696 break;
3697 }
3698 continue;
3699 }
Ben Chenge9695e52009-06-16 16:11:47 -07003700
Bill Buzbee89efc3d2009-07-28 11:22:22 -07003701 ArmLIR *headLIR = NULL;
Ben Chenge9695e52009-06-16 16:11:47 -07003702
Ben Chengba4fc8b2009-06-01 13:00:29 -07003703 for (mir = blockList[i]->firstMIRInsn; mir; mir = mir->next) {
Bill Buzbee1465db52009-09-23 17:17:35 -07003704
Bill Buzbeec6f10662010-02-09 11:16:15 -08003705 dvmCompilerResetRegPool(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07003706 if (gDvmJit.disableOpt & (1 << kTrackLiveTemps)) {
Bill Buzbeec6f10662010-02-09 11:16:15 -08003707 dvmCompilerClobberAllRegs(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07003708 }
3709
3710 if (gDvmJit.disableOpt & (1 << kSuppressLoads)) {
Bill Buzbeec6f10662010-02-09 11:16:15 -08003711 dvmCompilerResetDefTracking(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07003712 }
3713
3714 if (mir->dalvikInsn.opCode >= kMirOpFirst) {
Ben Cheng4238ec22009-08-24 16:32:22 -07003715 handleExtendedMIR(cUnit, mir);
3716 continue;
3717 }
3718
Bill Buzbee1465db52009-09-23 17:17:35 -07003719
Ben Chengba4fc8b2009-06-01 13:00:29 -07003720 OpCode dalvikOpCode = mir->dalvikInsn.opCode;
3721 InstructionFormat dalvikFormat =
3722 dexGetInstrFormat(gDvm.instrFormat, dalvikOpCode);
Bill Buzbee89efc3d2009-07-28 11:22:22 -07003723 ArmLIR *boundaryLIR =
Ben Chenga4973592010-03-31 11:59:18 -07003724 newLIR2(cUnit, kArmPseudoDalvikByteCodeBoundary,
Ben Chengccd6c012009-10-15 14:52:45 -07003725 mir->offset,
3726 (int) dvmCompilerGetDalvikDisassembly(&mir->dalvikInsn)
3727 );
Ben Cheng4238ec22009-08-24 16:32:22 -07003728 if (mir->ssaRep) {
3729 char *ssaString = dvmCompilerGetSSAString(cUnit, mir->ssaRep);
Bill Buzbee1465db52009-09-23 17:17:35 -07003730 newLIR1(cUnit, kArmPseudoSSARep, (int) ssaString);
Ben Cheng4238ec22009-08-24 16:32:22 -07003731 }
3732
Ben Chenge9695e52009-06-16 16:11:47 -07003733 /* Remember the first LIR for this block */
3734 if (headLIR == NULL) {
3735 headLIR = boundaryLIR;
Ben Chengd7d426a2009-09-22 11:23:36 -07003736 /* Set the first boundaryLIR as a scheduling barrier */
3737 headLIR->defMask = ENCODE_ALL;
Ben Chenge9695e52009-06-16 16:11:47 -07003738 }
Ben Cheng4238ec22009-08-24 16:32:22 -07003739
Ben Chengba4fc8b2009-06-01 13:00:29 -07003740 bool notHandled;
3741 /*
3742 * Debugging: screen the opcode first to see if it is in the
3743 * do[-not]-compile list
3744 */
3745 bool singleStepMe =
3746 gDvmJit.includeSelectedOp !=
3747 ((gDvmJit.opList[dalvikOpCode >> 3] &
3748 (1 << (dalvikOpCode & 0x7))) !=
3749 0);
Ben Chengd5adae12010-03-26 17:45:28 -07003750#if defined(WITH_SELF_VERIFICATION)
3751 if (singleStepMe == false) {
3752 singleStepMe = selfVerificationPuntOps(mir);
3753 }
3754#endif
Ben Chengba4fc8b2009-06-01 13:00:29 -07003755 if (singleStepMe || cUnit->allSingleStep) {
3756 notHandled = false;
3757 genInterpSingleStep(cUnit, mir);
3758 } else {
3759 opcodeCoverage[dalvikOpCode]++;
3760 switch (dalvikFormat) {
3761 case kFmt10t:
3762 case kFmt20t:
3763 case kFmt30t:
3764 notHandled = handleFmt10t_Fmt20t_Fmt30t(cUnit,
3765 mir, blockList[i], labelList);
3766 break;
3767 case kFmt10x:
3768 notHandled = handleFmt10x(cUnit, mir);
3769 break;
3770 case kFmt11n:
3771 case kFmt31i:
3772 notHandled = handleFmt11n_Fmt31i(cUnit, mir);
3773 break;
3774 case kFmt11x:
3775 notHandled = handleFmt11x(cUnit, mir);
3776 break;
3777 case kFmt12x:
3778 notHandled = handleFmt12x(cUnit, mir);
3779 break;
3780 case kFmt20bc:
3781 notHandled = handleFmt20bc(cUnit, mir);
3782 break;
3783 case kFmt21c:
3784 case kFmt31c:
3785 notHandled = handleFmt21c_Fmt31c(cUnit, mir);
3786 break;
3787 case kFmt21h:
3788 notHandled = handleFmt21h(cUnit, mir);
3789 break;
3790 case kFmt21s:
3791 notHandled = handleFmt21s(cUnit, mir);
3792 break;
3793 case kFmt21t:
3794 notHandled = handleFmt21t(cUnit, mir, blockList[i],
3795 labelList);
3796 break;
3797 case kFmt22b:
3798 case kFmt22s:
3799 notHandled = handleFmt22b_Fmt22s(cUnit, mir);
3800 break;
3801 case kFmt22c:
3802 notHandled = handleFmt22c(cUnit, mir);
3803 break;
3804 case kFmt22cs:
3805 notHandled = handleFmt22cs(cUnit, mir);
3806 break;
3807 case kFmt22t:
3808 notHandled = handleFmt22t(cUnit, mir, blockList[i],
3809 labelList);
3810 break;
3811 case kFmt22x:
3812 case kFmt32x:
3813 notHandled = handleFmt22x_Fmt32x(cUnit, mir);
3814 break;
3815 case kFmt23x:
3816 notHandled = handleFmt23x(cUnit, mir);
3817 break;
3818 case kFmt31t:
3819 notHandled = handleFmt31t(cUnit, mir);
3820 break;
3821 case kFmt3rc:
3822 case kFmt35c:
3823 notHandled = handleFmt35c_3rc(cUnit, mir, blockList[i],
3824 labelList);
3825 break;
3826 case kFmt3rms:
3827 case kFmt35ms:
3828 notHandled = handleFmt35ms_3rms(cUnit, mir,blockList[i],
3829 labelList);
3830 break;
3831 case kFmt3inline:
Andy McFaddenb0a05412009-11-19 10:23:41 -08003832 case kFmt3rinline:
Bill Buzbeece46c942009-11-20 15:41:34 -08003833 notHandled = handleExecuteInline(cUnit, mir);
Andy McFaddenb0a05412009-11-19 10:23:41 -08003834 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07003835 case kFmt51l:
3836 notHandled = handleFmt51l(cUnit, mir);
3837 break;
3838 default:
3839 notHandled = true;
3840 break;
3841 }
3842 }
3843 if (notHandled) {
3844 LOGE("%#06x: Opcode 0x%x (%s) / Fmt %d not handled\n",
3845 mir->offset,
3846 dalvikOpCode, getOpcodeName(dalvikOpCode),
3847 dalvikFormat);
Bill Buzbeefc519dc2010-03-06 23:30:57 -08003848 dvmCompilerAbort(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003849 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07003850 }
3851 }
Ben Cheng4238ec22009-08-24 16:32:22 -07003852
Bill Buzbee1465db52009-09-23 17:17:35 -07003853 if (blockList[i]->blockType == kEntryBlock) {
Ben Cheng4238ec22009-08-24 16:32:22 -07003854 dvmCompilerAppendLIR(cUnit,
3855 (LIR *) cUnit->loopAnalysis->branchToBody);
3856 dvmCompilerAppendLIR(cUnit,
3857 (LIR *) cUnit->loopAnalysis->branchToPCR);
3858 }
3859
3860 if (headLIR) {
3861 /*
3862 * Eliminate redundant loads/stores and delay stores into later
3863 * slots
3864 */
3865 dvmCompilerApplyLocalOptimizations(cUnit, (LIR *) headLIR,
3866 cUnit->lastLIRInsn);
3867 }
3868
3869gen_fallthrough:
Ben Cheng1efc9c52009-06-08 18:25:27 -07003870 /*
3871 * Check if the block is terminated due to trace length constraint -
3872 * insert an unconditional branch to the chaining cell.
3873 */
3874 if (blockList[i]->needFallThroughBranch) {
3875 genUnconditionalBranch(cUnit,
3876 &labelList[blockList[i]->fallThrough->id]);
3877 }
3878
Ben Chengba4fc8b2009-06-01 13:00:29 -07003879 }
3880
Ben Chenge9695e52009-06-16 16:11:47 -07003881 /* Handle the chaining cells in predefined order */
Ben Chengcec26f62010-01-15 15:29:33 -08003882 for (i = 0; i < kChainingCellGap; i++) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07003883 size_t j;
3884 int *blockIdList = (int *) chainingListByType[i].elemList;
3885
3886 cUnit->numChainingCells[i] = chainingListByType[i].numUsed;
3887
3888 /* No chaining cells of this type */
3889 if (cUnit->numChainingCells[i] == 0)
3890 continue;
3891
3892 /* Record the first LIR for a new type of chaining cell */
3893 cUnit->firstChainingLIR[i] = (LIR *) &labelList[blockIdList[0]];
3894
3895 for (j = 0; j < chainingListByType[i].numUsed; j++) {
3896 int blockId = blockIdList[j];
3897
3898 /* Align this chaining cell first */
Bill Buzbee1465db52009-09-23 17:17:35 -07003899 newLIR0(cUnit, kArmPseudoPseudoAlign4);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003900
3901 /* Insert the pseudo chaining instruction */
3902 dvmCompilerAppendLIR(cUnit, (LIR *) &labelList[blockId]);
3903
3904
3905 switch (blockList[blockId]->blockType) {
Bill Buzbee1465db52009-09-23 17:17:35 -07003906 case kChainingCellNormal:
Ben Cheng1efc9c52009-06-08 18:25:27 -07003907 handleNormalChainingCell(cUnit,
Ben Chengba4fc8b2009-06-01 13:00:29 -07003908 blockList[blockId]->startOffset);
3909 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07003910 case kChainingCellInvokeSingleton:
Ben Cheng38329f52009-07-07 14:19:20 -07003911 handleInvokeSingletonChainingCell(cUnit,
Ben Chengba4fc8b2009-06-01 13:00:29 -07003912 blockList[blockId]->containingMethod);
3913 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07003914 case kChainingCellInvokePredicted:
Ben Cheng38329f52009-07-07 14:19:20 -07003915 handleInvokePredictedChainingCell(cUnit);
3916 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07003917 case kChainingCellHot:
Ben Cheng1efc9c52009-06-08 18:25:27 -07003918 handleHotChainingCell(cUnit,
Ben Chengba4fc8b2009-06-01 13:00:29 -07003919 blockList[blockId]->startOffset);
3920 break;
Bill Buzbee9c4b7c82009-09-10 10:10:38 -07003921#if defined(WITH_SELF_VERIFICATION) || defined(WITH_JIT_TUNING)
Bill Buzbee1465db52009-09-23 17:17:35 -07003922 case kChainingCellBackwardBranch:
Jeff Hao97319a82009-08-12 16:57:15 -07003923 handleBackwardBranchChainingCell(cUnit,
3924 blockList[blockId]->startOffset);
3925 break;
3926#endif
Ben Chengba4fc8b2009-06-01 13:00:29 -07003927 default:
Bill Buzbee1465db52009-09-23 17:17:35 -07003928 LOGE("Bad blocktype %d", blockList[blockId]->blockType);
Bill Buzbeefc519dc2010-03-06 23:30:57 -08003929 dvmCompilerAbort(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003930 }
3931 }
3932 }
Ben Chenge9695e52009-06-16 16:11:47 -07003933
Ben Chengcec26f62010-01-15 15:29:33 -08003934 /* Mark the bottom of chaining cells */
3935 cUnit->chainingCellBottom = (LIR *) newLIR0(cUnit, kArmChainingCellBottom);
3936
Ben Cheng6c10a972009-10-29 14:39:18 -07003937 /*
3938 * Generate the branch to the dvmJitToInterpNoChain entry point at the end
3939 * of all chaining cells for the overflow cases.
3940 */
3941 if (cUnit->switchOverflowPad) {
3942 loadConstant(cUnit, r0, (int) cUnit->switchOverflowPad);
3943 loadWordDisp(cUnit, rGLUE, offsetof(InterpState,
3944 jitToInterpEntries.dvmJitToInterpNoChain), r2);
3945 opRegReg(cUnit, kOpAdd, r1, r1);
3946 opRegRegReg(cUnit, kOpAdd, r4PC, r0, r1);
Ben Cheng86717f72010-03-05 15:27:21 -08003947#if defined(JIT_STATS)
Ben Cheng6c10a972009-10-29 14:39:18 -07003948 loadConstant(cUnit, r0, kSwitchOverflow);
3949#endif
3950 opReg(cUnit, kOpBlx, r2);
3951 }
3952
Ben Chenge9695e52009-06-16 16:11:47 -07003953 dvmCompilerApplyGlobalOptimizations(cUnit);
jeffhao9e45c0b2010-02-03 10:24:05 -08003954
3955#if defined(WITH_SELF_VERIFICATION)
3956 selfVerificationBranchInsertPass(cUnit);
3957#endif
Ben Chengba4fc8b2009-06-01 13:00:29 -07003958}
3959
3960/* Accept the work and start compiling */
Bill Buzbee716f1202009-07-23 13:22:09 -07003961bool dvmCompilerDoWork(CompilerWorkOrder *work)
Ben Chengba4fc8b2009-06-01 13:00:29 -07003962{
Ben Chengccd6c012009-10-15 14:52:45 -07003963 bool res;
Ben Chengba4fc8b2009-06-01 13:00:29 -07003964
Ben Cheng6999d842010-01-26 16:46:15 -08003965 if (gDvmJit.codeCacheFull) {
Ben Chengccd6c012009-10-15 14:52:45 -07003966 return false;
3967 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07003968
Ben Chengccd6c012009-10-15 14:52:45 -07003969 switch (work->kind) {
3970 case kWorkOrderMethod:
3971 res = dvmCompileMethod(work->info, &work->result);
3972 break;
3973 case kWorkOrderTrace:
3974 /* Start compilation with maximally allowed trace length */
Bill Buzbeefc519dc2010-03-06 23:30:57 -08003975 res = dvmCompileTrace(work->info, JIT_MAX_TRACE_LEN, &work->result,
3976 work->bailPtr);
Ben Chengccd6c012009-10-15 14:52:45 -07003977 break;
3978 case kWorkOrderTraceDebug: {
3979 bool oldPrintMe = gDvmJit.printMe;
3980 gDvmJit.printMe = true;
3981 /* Start compilation with maximally allowed trace length */
Bill Buzbeefc519dc2010-03-06 23:30:57 -08003982 res = dvmCompileTrace(work->info, JIT_MAX_TRACE_LEN, &work->result,
3983 work->bailPtr);
Elliott Hughes672511b2010-04-26 17:40:13 -07003984 gDvmJit.printMe = oldPrintMe;
Ben Chengccd6c012009-10-15 14:52:45 -07003985 break;
3986 }
3987 default:
3988 res = false;
Bill Buzbeefc519dc2010-03-06 23:30:57 -08003989 LOGE("Jit: unknown work order type");
Elliott Hughes672511b2010-04-26 17:40:13 -07003990 assert(0); // Bail if debug build, discard otherwise
Ben Chengccd6c012009-10-15 14:52:45 -07003991 }
3992 return res;
Ben Chengba4fc8b2009-06-01 13:00:29 -07003993}
3994
Ben Chengba4fc8b2009-06-01 13:00:29 -07003995/* Architectural-specific debugging helpers go here */
3996void dvmCompilerArchDump(void)
3997{
3998 /* Print compiled opcode in this VM instance */
3999 int i, start, streak;
4000 char buf[1024];
4001
4002 streak = i = 0;
4003 buf[0] = 0;
4004 while (opcodeCoverage[i] == 0 && i < 256) {
4005 i++;
4006 }
4007 if (i == 256) {
4008 return;
4009 }
4010 for (start = i++, streak = 1; i < 256; i++) {
4011 if (opcodeCoverage[i]) {
4012 streak++;
4013 } else {
4014 if (streak == 1) {
4015 sprintf(buf+strlen(buf), "%x,", start);
4016 } else {
4017 sprintf(buf+strlen(buf), "%x-%x,", start, start + streak - 1);
4018 }
4019 streak = 0;
4020 while (opcodeCoverage[i] == 0 && i < 256) {
4021 i++;
4022 }
4023 if (i < 256) {
4024 streak = 1;
4025 start = i;
4026 }
4027 }
4028 }
4029 if (streak) {
4030 if (streak == 1) {
4031 sprintf(buf+strlen(buf), "%x", start);
4032 } else {
4033 sprintf(buf+strlen(buf), "%x-%x", start, start + streak - 1);
4034 }
4035 }
4036 if (strlen(buf)) {
Ben Cheng8b258bf2009-06-24 17:27:07 -07004037 LOGD("dalvik.vm.jit.op = %s", buf);
Ben Chengba4fc8b2009-06-01 13:00:29 -07004038 }
4039}
Ben Chengd7d426a2009-09-22 11:23:36 -07004040
4041/* Common initialization routine for an architecture family */
4042bool dvmCompilerArchInit()
4043{
4044 int i;
4045
Bill Buzbee1465db52009-09-23 17:17:35 -07004046 for (i = 0; i < kArmLast; i++) {
Ben Chengd7d426a2009-09-22 11:23:36 -07004047 if (EncodingMap[i].opCode != i) {
4048 LOGE("Encoding order for %s is wrong: expecting %d, seeing %d",
4049 EncodingMap[i].name, i, EncodingMap[i].opCode);
Bill Buzbeefc519dc2010-03-06 23:30:57 -08004050 dvmAbort(); // OK to dvmAbort - build error
Ben Chengd7d426a2009-09-22 11:23:36 -07004051 }
4052 }
4053
Ben Cheng5d90c202009-11-22 23:31:11 -08004054 return dvmCompilerArchVariantInit();
4055}
4056
4057void *dvmCompilerGetInterpretTemplate()
4058{
4059 return (void*) ((int)gDvmJit.codeCache +
4060 templateEntryOffsets[TEMPLATE_INTERPRET]);
4061}
4062
4063/* Needed by the ld/st optmizatons */
4064ArmLIR* dvmCompilerRegCopyNoInsert(CompilationUnit *cUnit, int rDest, int rSrc)
4065{
4066 return genRegCopyNoInsert(cUnit, rDest, rSrc);
4067}
4068
4069/* Needed by the register allocator */
4070ArmLIR* dvmCompilerRegCopy(CompilationUnit *cUnit, int rDest, int rSrc)
4071{
4072 return genRegCopy(cUnit, rDest, rSrc);
4073}
4074
4075/* Needed by the register allocator */
4076void dvmCompilerRegCopyWide(CompilationUnit *cUnit, int destLo, int destHi,
4077 int srcLo, int srcHi)
4078{
4079 genRegCopyWide(cUnit, destLo, destHi, srcLo, srcHi);
4080}
4081
4082void dvmCompilerFlushRegImpl(CompilationUnit *cUnit, int rBase,
4083 int displacement, int rSrc, OpSize size)
4084{
4085 storeBaseDisp(cUnit, rBase, displacement, rSrc, size);
4086}
4087
4088void dvmCompilerFlushRegWideImpl(CompilationUnit *cUnit, int rBase,
4089 int displacement, int rSrcLo, int rSrcHi)
4090{
4091 storeBaseDispWide(cUnit, rBase, displacement, rSrcLo, rSrcHi);
Ben Chengd7d426a2009-09-22 11:23:36 -07004092}