blob: 2cd16a1c003c6372a408c45e07691e9d6d12f946 [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 Cheng978738d2010-05-13 13:45:57 -0700875#if defined(WITH_JIT_TUNING)
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 Cheng978738d2010-05-13 13:45:57 -07001045#if defined(WITH_JIT_TUNING)
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 Cheng978738d2010-05-13 13:45:57 -07001050#if defined(WITH_JIT_TUNING)
Ben Cheng86717f72010-03-05 15:27:21 -08001051 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 Cheng978738d2010-05-13 13:45:57 -07001172#if defined(WITH_JIT_TUNING)
Ben Cheng86717f72010-03-05 15:27:21 -08001173 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]);
Ben Chengdd6e8702010-05-07 13:05:47 -07001449
1450 if (strPtr == NULL) {
1451 LOGE("Unexpected null string");
1452 dvmAbort();
1453 }
1454
Bill Buzbeec6f10662010-02-09 11:16:15 -08001455 rlDest = dvmCompilerGetDest(cUnit, mir, 0);
1456 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Ben Chengbd1326d2010-04-02 15:04:53 -07001457 loadConstantNoClobber(cUnit, rlResult.lowReg, (int) strPtr );
Bill Buzbee1465db52009-09-23 17:17:35 -07001458 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001459 break;
1460 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07001461 case OP_CONST_CLASS: {
1462 void *classPtr = (void*)
1463 (cUnit->method->clazz->pDvmDex->pResClasses[mir->dalvikInsn.vB]);
Ben Chengdd6e8702010-05-07 13:05:47 -07001464
1465 if (classPtr == NULL) {
1466 LOGE("Unexpected null class");
1467 dvmAbort();
1468 }
1469
Bill Buzbeec6f10662010-02-09 11:16:15 -08001470 rlDest = dvmCompilerGetDest(cUnit, mir, 0);
1471 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Ben Chengbd1326d2010-04-02 15:04:53 -07001472 loadConstantNoClobber(cUnit, rlResult.lowReg, (int) classPtr );
Bill Buzbee1465db52009-09-23 17:17:35 -07001473 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001474 break;
1475 }
1476 case OP_SGET_OBJECT:
1477 case OP_SGET_BOOLEAN:
1478 case OP_SGET_CHAR:
1479 case OP_SGET_BYTE:
1480 case OP_SGET_SHORT:
1481 case OP_SGET: {
Bill Buzbee50a6bf22009-07-08 13:08:04 -07001482 int valOffset = offsetof(StaticField, value);
Bill Buzbeec6f10662010-02-09 11:16:15 -08001483 int tReg = dvmCompilerAllocTemp(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001484 void *fieldPtr = (void*)
1485 (cUnit->method->clazz->pDvmDex->pResFields[mir->dalvikInsn.vB]);
Ben Chengdd6e8702010-05-07 13:05:47 -07001486
1487 if (fieldPtr == NULL) {
1488 LOGE("Unexpected null static field");
1489 dvmAbort();
1490 }
1491
Bill Buzbeec6f10662010-02-09 11:16:15 -08001492 rlDest = dvmCompilerGetDest(cUnit, mir, 0);
1493 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kAnyReg, true);
Bill Buzbee1465db52009-09-23 17:17:35 -07001494 loadConstant(cUnit, tReg, (int) fieldPtr + valOffset);
Ben Cheng11d8f142010-03-24 15:24:19 -07001495
1496 HEAP_ACCESS_SHADOW(true);
Bill Buzbee1465db52009-09-23 17:17:35 -07001497 loadWordDisp(cUnit, tReg, 0, rlResult.lowReg);
Ben Cheng11d8f142010-03-24 15:24:19 -07001498 HEAP_ACCESS_SHADOW(false);
1499
Bill Buzbee1465db52009-09-23 17:17:35 -07001500 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001501 break;
1502 }
1503 case OP_SGET_WIDE: {
Bill Buzbee50a6bf22009-07-08 13:08:04 -07001504 int valOffset = offsetof(StaticField, value);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001505 void *fieldPtr = (void*)
1506 (cUnit->method->clazz->pDvmDex->pResFields[mir->dalvikInsn.vB]);
Ben Chengdd6e8702010-05-07 13:05:47 -07001507
1508 if (fieldPtr == NULL) {
1509 LOGE("Unexpected null static field");
1510 dvmAbort();
1511 }
1512
Bill Buzbeec6f10662010-02-09 11:16:15 -08001513 int tReg = dvmCompilerAllocTemp(cUnit);
Bill Buzbeec6f10662010-02-09 11:16:15 -08001514 rlDest = dvmCompilerGetDestWide(cUnit, mir, 0, 1);
1515 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kAnyReg, true);
Bill Buzbee1465db52009-09-23 17:17:35 -07001516 loadConstant(cUnit, tReg, (int) fieldPtr + valOffset);
Ben Cheng11d8f142010-03-24 15:24:19 -07001517
1518 HEAP_ACCESS_SHADOW(true);
Bill Buzbee1465db52009-09-23 17:17:35 -07001519 loadPair(cUnit, tReg, rlResult.lowReg, rlResult.highReg);
Ben Cheng11d8f142010-03-24 15:24:19 -07001520 HEAP_ACCESS_SHADOW(false);
1521
Bill Buzbee1465db52009-09-23 17:17:35 -07001522 storeValueWide(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001523 break;
1524 }
1525 case OP_SPUT_OBJECT:
1526 case OP_SPUT_BOOLEAN:
1527 case OP_SPUT_CHAR:
1528 case OP_SPUT_BYTE:
1529 case OP_SPUT_SHORT:
1530 case OP_SPUT: {
Bill Buzbee50a6bf22009-07-08 13:08:04 -07001531 int valOffset = offsetof(StaticField, value);
Bill Buzbeec6f10662010-02-09 11:16:15 -08001532 int tReg = dvmCompilerAllocTemp(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001533 void *fieldPtr = (void*)
1534 (cUnit->method->clazz->pDvmDex->pResFields[mir->dalvikInsn.vB]);
Ben Chenge9695e52009-06-16 16:11:47 -07001535
Ben Chengdd6e8702010-05-07 13:05:47 -07001536 if (fieldPtr == NULL) {
1537 LOGE("Unexpected null static field");
1538 dvmAbort();
1539 }
1540
Bill Buzbeec6f10662010-02-09 11:16:15 -08001541 rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07001542 rlSrc = loadValue(cUnit, rlSrc, kAnyReg);
1543 loadConstant(cUnit, tReg, (int) fieldPtr + valOffset);
Ben Cheng11d8f142010-03-24 15:24:19 -07001544
1545 HEAP_ACCESS_SHADOW(true);
Bill Buzbee1465db52009-09-23 17:17:35 -07001546 storeWordDisp(cUnit, tReg, 0 ,rlSrc.lowReg);
Ben Cheng11d8f142010-03-24 15:24:19 -07001547 HEAP_ACCESS_SHADOW(false);
1548
Ben Chengba4fc8b2009-06-01 13:00:29 -07001549 break;
1550 }
1551 case OP_SPUT_WIDE: {
Bill Buzbeec6f10662010-02-09 11:16:15 -08001552 int tReg = dvmCompilerAllocTemp(cUnit);
Bill Buzbee50a6bf22009-07-08 13:08:04 -07001553 int valOffset = offsetof(StaticField, value);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001554 void *fieldPtr = (void*)
1555 (cUnit->method->clazz->pDvmDex->pResFields[mir->dalvikInsn.vB]);
Ben Chenge9695e52009-06-16 16:11:47 -07001556
Ben Chengdd6e8702010-05-07 13:05:47 -07001557 if (fieldPtr == NULL) {
1558 LOGE("Unexpected null static field");
1559 dvmAbort();
1560 }
1561
Bill Buzbeec6f10662010-02-09 11:16:15 -08001562 rlSrc = dvmCompilerGetSrcWide(cUnit, mir, 0, 1);
Bill Buzbee1465db52009-09-23 17:17:35 -07001563 rlSrc = loadValueWide(cUnit, rlSrc, kAnyReg);
1564 loadConstant(cUnit, tReg, (int) fieldPtr + valOffset);
Ben Cheng11d8f142010-03-24 15:24:19 -07001565
1566 HEAP_ACCESS_SHADOW(true);
Bill Buzbee1465db52009-09-23 17:17:35 -07001567 storePair(cUnit, tReg, rlSrc.lowReg, rlSrc.highReg);
Ben Cheng11d8f142010-03-24 15:24:19 -07001568 HEAP_ACCESS_SHADOW(false);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001569 break;
1570 }
1571 case OP_NEW_INSTANCE: {
Ben Chenge9695e52009-06-16 16:11:47 -07001572 /*
1573 * Obey the calling convention and don't mess with the register
1574 * usage.
1575 */
Ben Chengba4fc8b2009-06-01 13:00:29 -07001576 ClassObject *classPtr = (void*)
1577 (cUnit->method->clazz->pDvmDex->pResClasses[mir->dalvikInsn.vB]);
Ben Chengdd6e8702010-05-07 13:05:47 -07001578
1579 if (classPtr == NULL) {
1580 LOGE("Unexpected null class");
1581 dvmAbort();
1582 }
1583
Ben Cheng79d173c2009-09-29 16:12:51 -07001584 /*
1585 * If it is going to throw, it should not make to the trace to begin
Bill Buzbee1465db52009-09-23 17:17:35 -07001586 * with. However, Alloc might throw, so we need to genExportPC()
Ben Cheng79d173c2009-09-29 16:12:51 -07001587 */
1588 assert((classPtr->accessFlags & (ACC_INTERFACE|ACC_ABSTRACT)) == 0);
Bill Buzbeec6f10662010-02-09 11:16:15 -08001589 dvmCompilerFlushAllRegs(cUnit); /* Everything to home location */
Bill Buzbee1465db52009-09-23 17:17:35 -07001590 genExportPC(cUnit, mir);
Ben Chengbd1326d2010-04-02 15:04:53 -07001591 LOAD_FUNC_ADDR(cUnit, r2, (int)dvmAllocObject);
Ben Chenge9695e52009-06-16 16:11:47 -07001592 loadConstant(cUnit, r0, (int) classPtr);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001593 loadConstant(cUnit, r1, ALLOC_DONT_TRACK);
Bill Buzbee1465db52009-09-23 17:17:35 -07001594 opReg(cUnit, kOpBlx, r2);
Elliott Hughes6a555132010-02-25 15:41:42 -08001595 dvmCompilerClobberCallRegs(cUnit);
Ben Cheng4f489172009-09-27 17:08:35 -07001596 /* generate a branch over if allocation is successful */
Bill Buzbee1465db52009-09-23 17:17:35 -07001597 opRegImm(cUnit, kOpCmp, r0, 0); /* NULL? */
1598 ArmLIR *branchOver = opCondBranch(cUnit, kArmCondNe);
Ben Cheng4f489172009-09-27 17:08:35 -07001599 /*
1600 * OOM exception needs to be thrown here and cannot re-execute
1601 */
1602 loadConstant(cUnit, r0,
1603 (int) (cUnit->method->insns + mir->offset));
1604 genDispatchToHandler(cUnit, TEMPLATE_THROW_EXCEPTION_COMMON);
1605 /* noreturn */
1606
Bill Buzbee1465db52009-09-23 17:17:35 -07001607 ArmLIR *target = newLIR0(cUnit, kArmPseudoTargetLabel);
Ben Cheng4f489172009-09-27 17:08:35 -07001608 target->defMask = ENCODE_ALL;
1609 branchOver->generic.target = (LIR *) target;
Bill Buzbeec6f10662010-02-09 11:16:15 -08001610 rlDest = dvmCompilerGetDest(cUnit, mir, 0);
1611 rlResult = dvmCompilerGetReturn(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07001612 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001613 break;
1614 }
1615 case OP_CHECK_CAST: {
Ben Chenge9695e52009-06-16 16:11:47 -07001616 /*
1617 * Obey the calling convention and don't mess with the register
1618 * usage.
1619 */
Ben Chengba4fc8b2009-06-01 13:00:29 -07001620 ClassObject *classPtr =
1621 (cUnit->method->clazz->pDvmDex->pResClasses[mir->dalvikInsn.vB]);
Bill Buzbee4df41a52009-11-12 17:07:16 -08001622 /*
1623 * Note: It is possible that classPtr is NULL at this point,
1624 * even though this instruction has been successfully interpreted.
1625 * If the previous interpretation had a null source, the
1626 * interpreter would not have bothered to resolve the clazz.
1627 * Bail out to the interpreter in this case, and log it
1628 * so that we can tell if it happens frequently.
1629 */
1630 if (classPtr == NULL) {
Ben Cheng11d8f142010-03-24 15:24:19 -07001631 LOGVV("null clazz in OP_CHECK_CAST, single-stepping");
Bill Buzbee4df41a52009-11-12 17:07:16 -08001632 genInterpSingleStep(cUnit, mir);
1633 return false;
1634 }
Bill Buzbeec6f10662010-02-09 11:16:15 -08001635 dvmCompilerFlushAllRegs(cUnit); /* Everything to home location */
Ben Chengba4fc8b2009-06-01 13:00:29 -07001636 loadConstant(cUnit, r1, (int) classPtr );
Bill Buzbeec6f10662010-02-09 11:16:15 -08001637 rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07001638 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
1639 opRegImm(cUnit, kOpCmp, rlSrc.lowReg, 0); /* Null? */
1640 ArmLIR *branch1 = opCondBranch(cUnit, kArmCondEq);
1641 /*
1642 * rlSrc.lowReg now contains object->clazz. Note that
1643 * it could have been allocated r0, but we're okay so long
1644 * as we don't do anything desctructive until r0 is loaded
1645 * with clazz.
1646 */
Ben Chengba4fc8b2009-06-01 13:00:29 -07001647 /* r0 now contains object->clazz */
Bill Buzbee1465db52009-09-23 17:17:35 -07001648 loadWordDisp(cUnit, rlSrc.lowReg, offsetof(Object, clazz), r0);
Ben Chengbd1326d2010-04-02 15:04:53 -07001649 LOAD_FUNC_ADDR(cUnit, r2, (int)dvmInstanceofNonTrivial);
Bill Buzbee1465db52009-09-23 17:17:35 -07001650 opRegReg(cUnit, kOpCmp, r0, r1);
1651 ArmLIR *branch2 = opCondBranch(cUnit, kArmCondEq);
1652 opReg(cUnit, kOpBlx, r2);
Elliott Hughes6a555132010-02-25 15:41:42 -08001653 dvmCompilerClobberCallRegs(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07001654 /*
1655 * If null, check cast failed - punt to the interpreter. Because
1656 * interpreter will be the one throwing, we don't need to
1657 * genExportPC() here.
1658 */
Bill Buzbee270c1d62009-08-13 16:58:07 -07001659 genZeroCheck(cUnit, r0, mir->offset, NULL);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001660 /* check cast passed - branch target here */
Bill Buzbee1465db52009-09-23 17:17:35 -07001661 ArmLIR *target = newLIR0(cUnit, kArmPseudoTargetLabel);
Ben Chengd7d426a2009-09-22 11:23:36 -07001662 target->defMask = ENCODE_ALL;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001663 branch1->generic.target = (LIR *)target;
1664 branch2->generic.target = (LIR *)target;
1665 break;
1666 }
1667 default:
1668 return true;
1669 }
1670 return false;
1671}
1672
1673static bool handleFmt11x(CompilationUnit *cUnit, MIR *mir)
1674{
1675 OpCode dalvikOpCode = mir->dalvikInsn.opCode;
Bill Buzbee1465db52009-09-23 17:17:35 -07001676 RegLocation rlResult;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001677 switch (dalvikOpCode) {
1678 case OP_MOVE_EXCEPTION: {
1679 int offset = offsetof(InterpState, self);
1680 int exOffset = offsetof(Thread, exception);
Bill Buzbeec6f10662010-02-09 11:16:15 -08001681 int selfReg = dvmCompilerAllocTemp(cUnit);
1682 int resetReg = dvmCompilerAllocTemp(cUnit);
1683 RegLocation rlDest = dvmCompilerGetDest(cUnit, mir, 0);
1684 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Bill Buzbee1465db52009-09-23 17:17:35 -07001685 loadWordDisp(cUnit, rGLUE, offset, selfReg);
Bill Buzbeef9f33282009-11-22 12:45:30 -08001686 loadConstant(cUnit, resetReg, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07001687 loadWordDisp(cUnit, selfReg, exOffset, rlResult.lowReg);
Bill Buzbeef9f33282009-11-22 12:45:30 -08001688 storeWordDisp(cUnit, selfReg, exOffset, resetReg);
Bill Buzbee1465db52009-09-23 17:17:35 -07001689 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001690 break;
1691 }
1692 case OP_MOVE_RESULT:
1693 case OP_MOVE_RESULT_OBJECT: {
Bill Buzbeec6f10662010-02-09 11:16:15 -08001694 RegLocation rlDest = dvmCompilerGetDest(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07001695 RegLocation rlSrc = LOC_DALVIK_RETURN_VAL;
1696 rlSrc.fp = rlDest.fp;
1697 storeValue(cUnit, rlDest, rlSrc);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001698 break;
1699 }
1700 case OP_MOVE_RESULT_WIDE: {
Bill Buzbeec6f10662010-02-09 11:16:15 -08001701 RegLocation rlDest = dvmCompilerGetDestWide(cUnit, mir, 0, 1);
Bill Buzbee1465db52009-09-23 17:17:35 -07001702 RegLocation rlSrc = LOC_DALVIK_RETURN_VAL_WIDE;
1703 rlSrc.fp = rlDest.fp;
1704 storeValueWide(cUnit, rlDest, rlSrc);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001705 break;
1706 }
1707 case OP_RETURN_WIDE: {
Bill Buzbeec6f10662010-02-09 11:16:15 -08001708 RegLocation rlSrc = dvmCompilerGetSrcWide(cUnit, mir, 0, 1);
Bill Buzbee1465db52009-09-23 17:17:35 -07001709 RegLocation rlDest = LOC_DALVIK_RETURN_VAL_WIDE;
1710 rlDest.fp = rlSrc.fp;
1711 storeValueWide(cUnit, rlDest, rlSrc);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001712 genReturnCommon(cUnit,mir);
1713 break;
1714 }
1715 case OP_RETURN:
1716 case OP_RETURN_OBJECT: {
Bill Buzbeec6f10662010-02-09 11:16:15 -08001717 RegLocation rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07001718 RegLocation rlDest = LOC_DALVIK_RETURN_VAL;
1719 rlDest.fp = rlSrc.fp;
1720 storeValue(cUnit, rlDest, rlSrc);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001721 genReturnCommon(cUnit,mir);
1722 break;
1723 }
Bill Buzbee1465db52009-09-23 17:17:35 -07001724 case OP_MONITOR_EXIT:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001725 case OP_MONITOR_ENTER:
Bill Buzbeed0937ef2009-12-22 16:15:39 -08001726#if defined(WITH_DEADLOCK_PREDICTION) || defined(WITH_MONITOR_TRACKING)
Ben Cheng5d90c202009-11-22 23:31:11 -08001727 genMonitorPortable(cUnit, mir);
Bill Buzbee1465db52009-09-23 17:17:35 -07001728#else
Ben Cheng5d90c202009-11-22 23:31:11 -08001729 genMonitor(cUnit, mir);
Bill Buzbee1465db52009-09-23 17:17:35 -07001730#endif
Ben Chengba4fc8b2009-06-01 13:00:29 -07001731 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001732 case OP_THROW: {
1733 genInterpSingleStep(cUnit, mir);
1734 break;
1735 }
1736 default:
1737 return true;
1738 }
1739 return false;
1740}
1741
Bill Buzbeed45ba372009-06-15 17:00:57 -07001742static bool handleFmt12x(CompilationUnit *cUnit, MIR *mir)
1743{
1744 OpCode opCode = mir->dalvikInsn.opCode;
Bill Buzbee1465db52009-09-23 17:17:35 -07001745 RegLocation rlDest;
1746 RegLocation rlSrc;
1747 RegLocation rlResult;
Bill Buzbeed45ba372009-06-15 17:00:57 -07001748
Ben Chengba4fc8b2009-06-01 13:00:29 -07001749 if ( (opCode >= OP_ADD_INT_2ADDR) && (opCode <= OP_REM_DOUBLE_2ADDR)) {
Ben Cheng5d90c202009-11-22 23:31:11 -08001750 return genArithOp( cUnit, mir );
Ben Chengba4fc8b2009-06-01 13:00:29 -07001751 }
1752
Bill Buzbee1465db52009-09-23 17:17:35 -07001753 if (mir->ssaRep->numUses == 2)
Bill Buzbeec6f10662010-02-09 11:16:15 -08001754 rlSrc = dvmCompilerGetSrcWide(cUnit, mir, 0, 1);
Bill Buzbee1465db52009-09-23 17:17:35 -07001755 else
Bill Buzbeec6f10662010-02-09 11:16:15 -08001756 rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07001757 if (mir->ssaRep->numDefs == 2)
Bill Buzbeec6f10662010-02-09 11:16:15 -08001758 rlDest = dvmCompilerGetDestWide(cUnit, mir, 0, 1);
Bill Buzbee1465db52009-09-23 17:17:35 -07001759 else
Bill Buzbeec6f10662010-02-09 11:16:15 -08001760 rlDest = dvmCompilerGetDest(cUnit, mir, 0);
Ben Chenge9695e52009-06-16 16:11:47 -07001761
Ben Chengba4fc8b2009-06-01 13:00:29 -07001762 switch (opCode) {
Bill Buzbee1465db52009-09-23 17:17:35 -07001763 case OP_DOUBLE_TO_INT:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001764 case OP_INT_TO_FLOAT:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001765 case OP_FLOAT_TO_INT:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001766 case OP_DOUBLE_TO_FLOAT:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001767 case OP_FLOAT_TO_DOUBLE:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001768 case OP_INT_TO_DOUBLE:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001769 case OP_FLOAT_TO_LONG:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001770 case OP_LONG_TO_FLOAT:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001771 case OP_DOUBLE_TO_LONG:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001772 case OP_LONG_TO_DOUBLE:
Ben Cheng5d90c202009-11-22 23:31:11 -08001773 return genConversion(cUnit, mir);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001774 case OP_NEG_INT:
1775 case OP_NOT_INT:
Ben Cheng5d90c202009-11-22 23:31:11 -08001776 return genArithOpInt(cUnit, mir, rlDest, rlSrc, rlSrc);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001777 case OP_NEG_LONG:
1778 case OP_NOT_LONG:
Ben Cheng5d90c202009-11-22 23:31:11 -08001779 return genArithOpLong(cUnit, mir, rlDest, rlSrc, rlSrc);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001780 case OP_NEG_FLOAT:
Ben Cheng5d90c202009-11-22 23:31:11 -08001781 return genArithOpFloat(cUnit, mir, rlDest, rlSrc, rlSrc);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001782 case OP_NEG_DOUBLE:
Ben Cheng5d90c202009-11-22 23:31:11 -08001783 return genArithOpDouble(cUnit, mir, rlDest, rlSrc, rlSrc);
Bill Buzbee1465db52009-09-23 17:17:35 -07001784 case OP_MOVE_WIDE:
1785 storeValueWide(cUnit, rlDest, rlSrc);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001786 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07001787 case OP_INT_TO_LONG:
Bill Buzbeec6f10662010-02-09 11:16:15 -08001788 rlSrc = dvmCompilerUpdateLoc(cUnit, rlSrc);
1789 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Bill Buzbee964a7b02010-01-28 12:54:19 -08001790 //TUNING: shouldn't loadValueDirect already check for phys reg?
Bill Buzbee1465db52009-09-23 17:17:35 -07001791 if (rlSrc.location == kLocPhysReg) {
1792 genRegCopy(cUnit, rlResult.lowReg, rlSrc.lowReg);
1793 } else {
1794 loadValueDirect(cUnit, rlSrc, rlResult.lowReg);
1795 }
1796 opRegRegImm(cUnit, kOpAsr, rlResult.highReg,
1797 rlResult.lowReg, 31);
1798 storeValueWide(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001799 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07001800 case OP_LONG_TO_INT:
Bill Buzbeec6f10662010-02-09 11:16:15 -08001801 rlSrc = dvmCompilerUpdateLocWide(cUnit, rlSrc);
1802 rlSrc = dvmCompilerWideToNarrow(cUnit, rlSrc);
Bill Buzbee1465db52009-09-23 17:17:35 -07001803 // Intentional fallthrough
Ben Chengba4fc8b2009-06-01 13:00:29 -07001804 case OP_MOVE:
1805 case OP_MOVE_OBJECT:
Bill Buzbee1465db52009-09-23 17:17:35 -07001806 storeValue(cUnit, rlDest, rlSrc);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001807 break;
1808 case OP_INT_TO_BYTE:
Bill Buzbee1465db52009-09-23 17:17:35 -07001809 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
Bill Buzbeec6f10662010-02-09 11:16:15 -08001810 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Bill Buzbee1465db52009-09-23 17:17:35 -07001811 opRegReg(cUnit, kOp2Byte, rlResult.lowReg, rlSrc.lowReg);
1812 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001813 break;
1814 case OP_INT_TO_SHORT:
Bill Buzbee1465db52009-09-23 17:17:35 -07001815 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
Bill Buzbeec6f10662010-02-09 11:16:15 -08001816 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Bill Buzbee1465db52009-09-23 17:17:35 -07001817 opRegReg(cUnit, kOp2Short, rlResult.lowReg, rlSrc.lowReg);
1818 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001819 break;
1820 case OP_INT_TO_CHAR:
Bill Buzbee1465db52009-09-23 17:17:35 -07001821 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
Bill Buzbeec6f10662010-02-09 11:16:15 -08001822 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Bill Buzbee1465db52009-09-23 17:17:35 -07001823 opRegReg(cUnit, kOp2Char, rlResult.lowReg, rlSrc.lowReg);
1824 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001825 break;
1826 case OP_ARRAY_LENGTH: {
1827 int lenOffset = offsetof(ArrayObject, length);
Bill Buzbee1465db52009-09-23 17:17:35 -07001828 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
1829 genNullCheck(cUnit, rlSrc.sRegLow, rlSrc.lowReg,
1830 mir->offset, NULL);
Bill Buzbeec6f10662010-02-09 11:16:15 -08001831 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Bill Buzbee1465db52009-09-23 17:17:35 -07001832 loadWordDisp(cUnit, rlSrc.lowReg, lenOffset,
1833 rlResult.lowReg);
1834 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001835 break;
1836 }
1837 default:
1838 return true;
1839 }
1840 return false;
1841}
1842
1843static bool handleFmt21s(CompilationUnit *cUnit, MIR *mir)
1844{
1845 OpCode dalvikOpCode = mir->dalvikInsn.opCode;
Bill Buzbee1465db52009-09-23 17:17:35 -07001846 RegLocation rlDest;
1847 RegLocation rlResult;
1848 int BBBB = mir->dalvikInsn.vB;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001849 if (dalvikOpCode == OP_CONST_WIDE_16) {
Bill Buzbeec6f10662010-02-09 11:16:15 -08001850 rlDest = dvmCompilerGetDestWide(cUnit, mir, 0, 1);
1851 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Ben Chengbd1326d2010-04-02 15:04:53 -07001852 loadConstantNoClobber(cUnit, rlResult.lowReg, BBBB);
Bill Buzbee964a7b02010-01-28 12:54:19 -08001853 //TUNING: do high separately to avoid load dependency
Bill Buzbee1465db52009-09-23 17:17:35 -07001854 opRegRegImm(cUnit, kOpAsr, rlResult.highReg, rlResult.lowReg, 31);
1855 storeValueWide(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001856 } else if (dalvikOpCode == OP_CONST_16) {
Bill Buzbeec6f10662010-02-09 11:16:15 -08001857 rlDest = dvmCompilerGetDest(cUnit, mir, 0);
1858 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kAnyReg, true);
Ben Chengbd1326d2010-04-02 15:04:53 -07001859 loadConstantNoClobber(cUnit, rlResult.lowReg, BBBB);
Bill Buzbee1465db52009-09-23 17:17:35 -07001860 storeValue(cUnit, rlDest, rlResult);
1861 } else
Ben Chengba4fc8b2009-06-01 13:00:29 -07001862 return true;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001863 return false;
1864}
1865
1866/* Compare agaist zero */
1867static bool handleFmt21t(CompilationUnit *cUnit, MIR *mir, BasicBlock *bb,
Bill Buzbee89efc3d2009-07-28 11:22:22 -07001868 ArmLIR *labelList)
Ben Chengba4fc8b2009-06-01 13:00:29 -07001869{
1870 OpCode dalvikOpCode = mir->dalvikInsn.opCode;
Bill Buzbee89efc3d2009-07-28 11:22:22 -07001871 ArmConditionCode cond;
Bill Buzbeec6f10662010-02-09 11:16:15 -08001872 RegLocation rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07001873 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
1874 opRegImm(cUnit, kOpCmp, rlSrc.lowReg, 0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001875
Bill Buzbee270c1d62009-08-13 16:58:07 -07001876//TUNING: break this out to allow use of Thumb2 CB[N]Z
Ben Chengba4fc8b2009-06-01 13:00:29 -07001877 switch (dalvikOpCode) {
1878 case OP_IF_EQZ:
Bill Buzbee1465db52009-09-23 17:17:35 -07001879 cond = kArmCondEq;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001880 break;
1881 case OP_IF_NEZ:
Bill Buzbee1465db52009-09-23 17:17:35 -07001882 cond = kArmCondNe;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001883 break;
1884 case OP_IF_LTZ:
Bill Buzbee1465db52009-09-23 17:17:35 -07001885 cond = kArmCondLt;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001886 break;
1887 case OP_IF_GEZ:
Bill Buzbee1465db52009-09-23 17:17:35 -07001888 cond = kArmCondGe;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001889 break;
1890 case OP_IF_GTZ:
Bill Buzbee1465db52009-09-23 17:17:35 -07001891 cond = kArmCondGt;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001892 break;
1893 case OP_IF_LEZ:
Bill Buzbee1465db52009-09-23 17:17:35 -07001894 cond = kArmCondLe;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001895 break;
1896 default:
1897 cond = 0;
1898 LOGE("Unexpected opcode (%d) for Fmt21t\n", dalvikOpCode);
Bill Buzbeefc519dc2010-03-06 23:30:57 -08001899 dvmCompilerAbort(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001900 }
1901 genConditionalBranch(cUnit, cond, &labelList[bb->taken->id]);
1902 /* This mostly likely will be optimized away in a later phase */
1903 genUnconditionalBranch(cUnit, &labelList[bb->fallThrough->id]);
1904 return false;
1905}
1906
Elliott Hughesb4c05972010-02-24 16:36:18 -08001907static bool isPowerOfTwo(int x)
1908{
1909 return (x & (x - 1)) == 0;
1910}
1911
1912// Returns true if no more than two bits are set in 'x'.
1913static bool isPopCountLE2(unsigned int x)
1914{
1915 x &= x - 1;
1916 return (x & (x - 1)) == 0;
1917}
1918
1919// Returns the index of the lowest set bit in 'x'.
1920static int lowestSetBit(unsigned int x) {
1921 int bit_posn = 0;
1922 while ((x & 0xf) == 0) {
1923 bit_posn += 4;
1924 x >>= 4;
Bill Buzbee78cb0e22010-02-11 14:04:53 -08001925 }
Elliott Hughesb4c05972010-02-24 16:36:18 -08001926 while ((x & 1) == 0) {
1927 bit_posn++;
1928 x >>= 1;
1929 }
1930 return bit_posn;
1931}
1932
Elliott Hughes672511b2010-04-26 17:40:13 -07001933// Returns true if it added instructions to 'cUnit' to divide 'rlSrc' by 'lit'
1934// and store the result in 'rlDest'.
Elliott Hughesc7ad9b22010-04-28 13:52:02 -07001935static bool handleEasyDivide(CompilationUnit *cUnit, OpCode dalvikOpCode,
Elliott Hughes672511b2010-04-26 17:40:13 -07001936 RegLocation rlSrc, RegLocation rlDest, int lit)
1937{
1938 if (lit < 2 || !isPowerOfTwo(lit)) {
1939 return false;
1940 }
1941 int k = lowestSetBit(lit);
1942 if (k >= 30) {
1943 // Avoid special cases.
1944 return false;
1945 }
Elliott Hughes9c457022010-04-28 16:15:38 -07001946 bool div = (dalvikOpCode == OP_DIV_INT_LIT8 || dalvikOpCode == OP_DIV_INT_LIT16);
Elliott Hughes672511b2010-04-26 17:40:13 -07001947 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
1948 RegLocation rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Elliott Hughes9c457022010-04-28 16:15:38 -07001949 if (div) {
1950 int tReg = dvmCompilerAllocTemp(cUnit);
1951 if (lit == 2) {
1952 // Division by 2 is by far the most common division by constant.
1953 opRegRegImm(cUnit, kOpLsr, tReg, rlSrc.lowReg, 32 - k);
1954 opRegRegReg(cUnit, kOpAdd, tReg, tReg, rlSrc.lowReg);
1955 opRegRegImm(cUnit, kOpAsr, rlResult.lowReg, tReg, k);
1956 } else {
1957 opRegRegImm(cUnit, kOpAsr, tReg, rlSrc.lowReg, 31);
1958 opRegRegImm(cUnit, kOpLsr, tReg, tReg, 32 - k);
1959 opRegRegReg(cUnit, kOpAdd, tReg, tReg, rlSrc.lowReg);
1960 opRegRegImm(cUnit, kOpAsr, rlResult.lowReg, tReg, k);
1961 }
Elliott Hughes672511b2010-04-26 17:40:13 -07001962 } else {
Elliott Hughes9c457022010-04-28 16:15:38 -07001963 int cReg = dvmCompilerAllocTemp(cUnit);
1964 loadConstant(cUnit, cReg, lit - 1);
1965 int tReg1 = dvmCompilerAllocTemp(cUnit);
1966 int tReg2 = dvmCompilerAllocTemp(cUnit);
1967 if (lit == 2) {
1968 opRegRegImm(cUnit, kOpLsr, tReg1, rlSrc.lowReg, 32 - k);
1969 opRegRegReg(cUnit, kOpAdd, tReg2, tReg1, rlSrc.lowReg);
1970 opRegRegReg(cUnit, kOpAnd, tReg2, tReg2, cReg);
1971 opRegRegReg(cUnit, kOpSub, rlResult.lowReg, tReg2, tReg1);
1972 } else {
1973 opRegRegImm(cUnit, kOpAsr, tReg1, rlSrc.lowReg, 31);
1974 opRegRegImm(cUnit, kOpLsr, tReg1, tReg1, 32 - k);
1975 opRegRegReg(cUnit, kOpAdd, tReg2, tReg1, rlSrc.lowReg);
1976 opRegRegReg(cUnit, kOpAnd, tReg2, tReg2, cReg);
1977 opRegRegReg(cUnit, kOpSub, rlResult.lowReg, tReg2, tReg1);
1978 }
Elliott Hughes672511b2010-04-26 17:40:13 -07001979 }
1980 storeValue(cUnit, rlDest, rlResult);
1981 return true;
1982}
1983
Elliott Hughesb4c05972010-02-24 16:36:18 -08001984// Returns true if it added instructions to 'cUnit' to multiply 'rlSrc' by 'lit'
1985// and store the result in 'rlDest'.
1986static bool handleEasyMultiply(CompilationUnit *cUnit,
1987 RegLocation rlSrc, RegLocation rlDest, int lit)
1988{
1989 // Can we simplify this multiplication?
1990 bool powerOfTwo = false;
1991 bool popCountLE2 = false;
1992 bool powerOfTwoMinusOne = false;
1993 if (lit < 2) {
1994 // Avoid special cases.
1995 return false;
1996 } else if (isPowerOfTwo(lit)) {
1997 powerOfTwo = true;
1998 } else if (isPopCountLE2(lit)) {
1999 popCountLE2 = true;
2000 } else if (isPowerOfTwo(lit + 1)) {
2001 powerOfTwoMinusOne = true;
2002 } else {
2003 return false;
2004 }
2005 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
2006 RegLocation rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
2007 if (powerOfTwo) {
2008 // Shift.
2009 opRegRegImm(cUnit, kOpLsl, rlResult.lowReg, rlSrc.lowReg,
2010 lowestSetBit(lit));
2011 } else if (popCountLE2) {
2012 // Shift and add and shift.
2013 int firstBit = lowestSetBit(lit);
2014 int secondBit = lowestSetBit(lit ^ (1 << firstBit));
2015 genMultiplyByTwoBitMultiplier(cUnit, rlSrc, rlResult, lit,
2016 firstBit, secondBit);
2017 } else {
2018 // Reverse subtract: (src << (shift + 1)) - src.
2019 assert(powerOfTwoMinusOne);
2020 // TODO: rsb dst, src, src lsl#lowestSetBit(lit + 1)
2021 int tReg = dvmCompilerAllocTemp(cUnit);
2022 opRegRegImm(cUnit, kOpLsl, tReg, rlSrc.lowReg, lowestSetBit(lit + 1));
2023 opRegRegReg(cUnit, kOpSub, rlResult.lowReg, tReg, rlSrc.lowReg);
2024 }
2025 storeValue(cUnit, rlDest, rlResult);
2026 return true;
Bill Buzbee78cb0e22010-02-11 14:04:53 -08002027}
2028
Ben Chengba4fc8b2009-06-01 13:00:29 -07002029static bool handleFmt22b_Fmt22s(CompilationUnit *cUnit, MIR *mir)
2030{
2031 OpCode dalvikOpCode = mir->dalvikInsn.opCode;
Bill Buzbeec6f10662010-02-09 11:16:15 -08002032 RegLocation rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
2033 RegLocation rlDest = dvmCompilerGetDest(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07002034 RegLocation rlResult;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002035 int lit = mir->dalvikInsn.vC;
Ben Cheng4f489172009-09-27 17:08:35 -07002036 OpKind op = 0; /* Make gcc happy */
Bill Buzbee1465db52009-09-23 17:17:35 -07002037 int shiftOp = false;
2038 bool isDiv = false;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002039
Ben Chengba4fc8b2009-06-01 13:00:29 -07002040 switch (dalvikOpCode) {
Bill Buzbee1465db52009-09-23 17:17:35 -07002041 case OP_RSUB_INT_LIT8:
2042 case OP_RSUB_INT: {
2043 int tReg;
2044 //TUNING: add support for use of Arm rsub op
2045 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
Bill Buzbeec6f10662010-02-09 11:16:15 -08002046 tReg = dvmCompilerAllocTemp(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07002047 loadConstant(cUnit, tReg, lit);
Bill Buzbeec6f10662010-02-09 11:16:15 -08002048 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Bill Buzbee1465db52009-09-23 17:17:35 -07002049 opRegRegReg(cUnit, kOpSub, rlResult.lowReg,
2050 tReg, rlSrc.lowReg);
2051 storeValue(cUnit, rlDest, rlResult);
2052 return false;
2053 break;
2054 }
2055
Ben Chengba4fc8b2009-06-01 13:00:29 -07002056 case OP_ADD_INT_LIT8:
2057 case OP_ADD_INT_LIT16:
Bill Buzbee1465db52009-09-23 17:17:35 -07002058 op = kOpAdd;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002059 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002060 case OP_MUL_INT_LIT8:
Bill Buzbee78cb0e22010-02-11 14:04:53 -08002061 case OP_MUL_INT_LIT16: {
Elliott Hughesb4c05972010-02-24 16:36:18 -08002062 if (handleEasyMultiply(cUnit, rlSrc, rlDest, lit)) {
2063 return false;
Bill Buzbee78cb0e22010-02-11 14:04:53 -08002064 }
Elliott Hughesb4c05972010-02-24 16:36:18 -08002065 op = kOpMul;
Bill Buzbee1465db52009-09-23 17:17:35 -07002066 break;
Bill Buzbee78cb0e22010-02-11 14:04:53 -08002067 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07002068 case OP_AND_INT_LIT8:
2069 case OP_AND_INT_LIT16:
Bill Buzbee1465db52009-09-23 17:17:35 -07002070 op = kOpAnd;
2071 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002072 case OP_OR_INT_LIT8:
2073 case OP_OR_INT_LIT16:
Bill Buzbee1465db52009-09-23 17:17:35 -07002074 op = kOpOr;
2075 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002076 case OP_XOR_INT_LIT8:
2077 case OP_XOR_INT_LIT16:
Bill Buzbee1465db52009-09-23 17:17:35 -07002078 op = kOpXor;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002079 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002080 case OP_SHL_INT_LIT8:
Bill Buzbee0e605272009-12-01 14:28:05 -08002081 lit &= 31;
Bill Buzbee1465db52009-09-23 17:17:35 -07002082 shiftOp = true;
2083 op = kOpLsl;
2084 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002085 case OP_SHR_INT_LIT8:
Bill Buzbee0e605272009-12-01 14:28:05 -08002086 lit &= 31;
Bill Buzbee1465db52009-09-23 17:17:35 -07002087 shiftOp = true;
2088 op = kOpAsr;
2089 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002090 case OP_USHR_INT_LIT8:
Bill Buzbee0e605272009-12-01 14:28:05 -08002091 lit &= 31;
Bill Buzbee1465db52009-09-23 17:17:35 -07002092 shiftOp = true;
2093 op = kOpLsr;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002094 break;
2095
2096 case OP_DIV_INT_LIT8:
2097 case OP_DIV_INT_LIT16:
Ben Chengba4fc8b2009-06-01 13:00:29 -07002098 case OP_REM_INT_LIT8:
2099 case OP_REM_INT_LIT16:
2100 if (lit == 0) {
2101 /* Let the interpreter deal with div by 0 */
2102 genInterpSingleStep(cUnit, mir);
2103 return false;
2104 }
Elliott Hughesc7ad9b22010-04-28 13:52:02 -07002105 if (handleEasyDivide(cUnit, dalvikOpCode, rlSrc, rlDest, lit)) {
Elliott Hughes672511b2010-04-26 17:40:13 -07002106 return false;
2107 }
Bill Buzbeec6f10662010-02-09 11:16:15 -08002108 dvmCompilerFlushAllRegs(cUnit); /* Everything to home location */
Bill Buzbee1465db52009-09-23 17:17:35 -07002109 loadValueDirectFixed(cUnit, rlSrc, r0);
Bill Buzbeec6f10662010-02-09 11:16:15 -08002110 dvmCompilerClobber(cUnit, r0);
Bill Buzbee1465db52009-09-23 17:17:35 -07002111 if ((dalvikOpCode == OP_DIV_INT_LIT8) ||
2112 (dalvikOpCode == OP_DIV_INT_LIT16)) {
Ben Chengbd1326d2010-04-02 15:04:53 -07002113 LOAD_FUNC_ADDR(cUnit, r2, (int)__aeabi_idiv);
Bill Buzbee1465db52009-09-23 17:17:35 -07002114 isDiv = true;
2115 } else {
Ben Chengbd1326d2010-04-02 15:04:53 -07002116 LOAD_FUNC_ADDR(cUnit, r2, (int)__aeabi_idivmod);
Bill Buzbee1465db52009-09-23 17:17:35 -07002117 isDiv = false;
2118 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07002119 loadConstant(cUnit, r1, lit);
Bill Buzbee1465db52009-09-23 17:17:35 -07002120 opReg(cUnit, kOpBlx, r2);
Elliott Hughes6a555132010-02-25 15:41:42 -08002121 dvmCompilerClobberCallRegs(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07002122 if (isDiv)
Bill Buzbeec6f10662010-02-09 11:16:15 -08002123 rlResult = dvmCompilerGetReturn(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07002124 else
Bill Buzbeec6f10662010-02-09 11:16:15 -08002125 rlResult = dvmCompilerGetReturnAlt(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07002126 storeValue(cUnit, rlDest, rlResult);
2127 return false;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002128 break;
2129 default:
2130 return true;
2131 }
Bill Buzbee1465db52009-09-23 17:17:35 -07002132 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
Bill Buzbeec6f10662010-02-09 11:16:15 -08002133 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Bill Buzbee1465db52009-09-23 17:17:35 -07002134 // Avoid shifts by literal 0 - no support in Thumb. Change to copy
2135 if (shiftOp && (lit == 0)) {
2136 genRegCopy(cUnit, rlResult.lowReg, rlSrc.lowReg);
2137 } else {
2138 opRegRegImm(cUnit, op, rlResult.lowReg, rlSrc.lowReg, lit);
2139 }
2140 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002141 return false;
2142}
2143
2144static bool handleFmt22c(CompilationUnit *cUnit, MIR *mir)
2145{
2146 OpCode dalvikOpCode = mir->dalvikInsn.opCode;
2147 int fieldOffset;
2148
2149 if (dalvikOpCode >= OP_IGET && dalvikOpCode <= OP_IPUT_SHORT) {
2150 InstField *pInstField = (InstField *)
2151 cUnit->method->clazz->pDvmDex->pResFields[mir->dalvikInsn.vC];
Ben Chengba4fc8b2009-06-01 13:00:29 -07002152
Ben Chengdd6e8702010-05-07 13:05:47 -07002153 if (pInstField == NULL) {
2154 LOGE("Unexpected null instance field");
2155 dvmAbort();
2156 }
2157
Ben Chengba4fc8b2009-06-01 13:00:29 -07002158 fieldOffset = pInstField->byteOffset;
2159 } else {
Ben Chenga0e7b602009-10-13 23:09:01 -07002160 /* Deliberately break the code while make the compiler happy */
2161 fieldOffset = -1;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002162 }
2163 switch (dalvikOpCode) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07002164 case OP_NEW_ARRAY: {
Bill Buzbee1465db52009-09-23 17:17:35 -07002165 // Generates a call - use explicit registers
Bill Buzbeec6f10662010-02-09 11:16:15 -08002166 RegLocation rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
2167 RegLocation rlDest = dvmCompilerGetDest(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07002168 RegLocation rlResult;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002169 void *classPtr = (void*)
2170 (cUnit->method->clazz->pDvmDex->pResClasses[mir->dalvikInsn.vC]);
Ben Chengdd6e8702010-05-07 13:05:47 -07002171
2172 if (classPtr == NULL) {
2173 LOGE("Unexpected null class");
2174 dvmAbort();
2175 }
2176
Bill Buzbeec6f10662010-02-09 11:16:15 -08002177 dvmCompilerFlushAllRegs(cUnit); /* Everything to home location */
Bill Buzbee1465db52009-09-23 17:17:35 -07002178 genExportPC(cUnit, mir);
2179 loadValueDirectFixed(cUnit, rlSrc, r1); /* Len */
Ben Chengba4fc8b2009-06-01 13:00:29 -07002180 loadConstant(cUnit, r0, (int) classPtr );
Ben Chengbd1326d2010-04-02 15:04:53 -07002181 LOAD_FUNC_ADDR(cUnit, r3, (int)dvmAllocArrayByClass);
Ben Cheng4f489172009-09-27 17:08:35 -07002182 /*
2183 * "len < 0": bail to the interpreter to re-execute the
2184 * instruction
2185 */
Bill Buzbee89efc3d2009-07-28 11:22:22 -07002186 ArmLIR *pcrLabel =
Bill Buzbee1465db52009-09-23 17:17:35 -07002187 genRegImmCheck(cUnit, kArmCondMi, r1, 0, mir->offset, NULL);
Bill Buzbee270c1d62009-08-13 16:58:07 -07002188 loadConstant(cUnit, r2, ALLOC_DONT_TRACK);
Bill Buzbee1465db52009-09-23 17:17:35 -07002189 opReg(cUnit, kOpBlx, r3);
Elliott Hughes6a555132010-02-25 15:41:42 -08002190 dvmCompilerClobberCallRegs(cUnit);
Ben Cheng4f489172009-09-27 17:08:35 -07002191 /* generate a branch over if allocation is successful */
Bill Buzbee1465db52009-09-23 17:17:35 -07002192 opRegImm(cUnit, kOpCmp, r0, 0); /* NULL? */
2193 ArmLIR *branchOver = opCondBranch(cUnit, kArmCondNe);
Ben Cheng4f489172009-09-27 17:08:35 -07002194 /*
2195 * OOM exception needs to be thrown here and cannot re-execute
2196 */
2197 loadConstant(cUnit, r0,
2198 (int) (cUnit->method->insns + mir->offset));
2199 genDispatchToHandler(cUnit, TEMPLATE_THROW_EXCEPTION_COMMON);
2200 /* noreturn */
2201
Bill Buzbee1465db52009-09-23 17:17:35 -07002202 ArmLIR *target = newLIR0(cUnit, kArmPseudoTargetLabel);
Ben Cheng4f489172009-09-27 17:08:35 -07002203 target->defMask = ENCODE_ALL;
2204 branchOver->generic.target = (LIR *) target;
Bill Buzbeec6f10662010-02-09 11:16:15 -08002205 rlResult = dvmCompilerGetReturn(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07002206 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002207 break;
2208 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07002209 case OP_INSTANCE_OF: {
Bill Buzbee1465db52009-09-23 17:17:35 -07002210 // May generate a call - use explicit registers
Bill Buzbeec6f10662010-02-09 11:16:15 -08002211 RegLocation rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
2212 RegLocation rlDest = dvmCompilerGetDest(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07002213 RegLocation rlResult;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002214 ClassObject *classPtr =
2215 (cUnit->method->clazz->pDvmDex->pResClasses[mir->dalvikInsn.vC]);
Bill Buzbee480e6782010-01-27 15:43:08 -08002216 /*
2217 * Note: It is possible that classPtr is NULL at this point,
2218 * even though this instruction has been successfully interpreted.
2219 * If the previous interpretation had a null source, the
2220 * interpreter would not have bothered to resolve the clazz.
2221 * Bail out to the interpreter in this case, and log it
2222 * so that we can tell if it happens frequently.
2223 */
2224 if (classPtr == NULL) {
2225 LOGD("null clazz in OP_INSTANCE_OF, single-stepping");
2226 genInterpSingleStep(cUnit, mir);
2227 break;
2228 }
Bill Buzbeec6f10662010-02-09 11:16:15 -08002229 dvmCompilerFlushAllRegs(cUnit); /* Everything to home location */
Bill Buzbee1465db52009-09-23 17:17:35 -07002230 loadValueDirectFixed(cUnit, rlSrc, r0); /* Ref */
Ben Chengba4fc8b2009-06-01 13:00:29 -07002231 loadConstant(cUnit, r2, (int) classPtr );
Bill Buzbee270c1d62009-08-13 16:58:07 -07002232//TUNING: compare to 0 primative to allow use of CB[N]Z
Bill Buzbee1465db52009-09-23 17:17:35 -07002233 opRegImm(cUnit, kOpCmp, r0, 0); /* NULL? */
Ben Cheng752c7942009-06-22 10:50:07 -07002234 /* When taken r0 has NULL which can be used for store directly */
Bill Buzbee1465db52009-09-23 17:17:35 -07002235 ArmLIR *branch1 = opCondBranch(cUnit, kArmCondEq);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002236 /* r1 now contains object->clazz */
Bill Buzbee270c1d62009-08-13 16:58:07 -07002237 loadWordDisp(cUnit, r0, offsetof(Object, clazz), r1);
Bill Buzbee1465db52009-09-23 17:17:35 -07002238 /* r1 now contains object->clazz */
Ben Chengbd1326d2010-04-02 15:04:53 -07002239 LOAD_FUNC_ADDR(cUnit, r3, (int)dvmInstanceofNonTrivial);
Ben Cheng752c7942009-06-22 10:50:07 -07002240 loadConstant(cUnit, r0, 1); /* Assume true */
Bill Buzbee1465db52009-09-23 17:17:35 -07002241 opRegReg(cUnit, kOpCmp, r1, r2);
2242 ArmLIR *branch2 = opCondBranch(cUnit, kArmCondEq);
2243 genRegCopy(cUnit, r0, r1);
2244 genRegCopy(cUnit, r1, r2);
2245 opReg(cUnit, kOpBlx, r3);
Elliott Hughes6a555132010-02-25 15:41:42 -08002246 dvmCompilerClobberCallRegs(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002247 /* branch target here */
Bill Buzbee1465db52009-09-23 17:17:35 -07002248 ArmLIR *target = newLIR0(cUnit, kArmPseudoTargetLabel);
Ben Chengd7d426a2009-09-22 11:23:36 -07002249 target->defMask = ENCODE_ALL;
Bill Buzbeec6f10662010-02-09 11:16:15 -08002250 rlResult = dvmCompilerGetReturn(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07002251 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002252 branch1->generic.target = (LIR *)target;
2253 branch2->generic.target = (LIR *)target;
2254 break;
2255 }
2256 case OP_IGET_WIDE:
2257 genIGetWide(cUnit, mir, fieldOffset);
2258 break;
2259 case OP_IGET:
2260 case OP_IGET_OBJECT:
Bill Buzbee1465db52009-09-23 17:17:35 -07002261 genIGet(cUnit, mir, kWord, fieldOffset);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002262 break;
2263 case OP_IGET_BOOLEAN:
Bill Buzbee1465db52009-09-23 17:17:35 -07002264 genIGet(cUnit, mir, kUnsignedByte, fieldOffset);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002265 break;
2266 case OP_IGET_BYTE:
Bill Buzbee1465db52009-09-23 17:17:35 -07002267 genIGet(cUnit, mir, kSignedByte, fieldOffset);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002268 break;
2269 case OP_IGET_CHAR:
Bill Buzbee1465db52009-09-23 17:17:35 -07002270 genIGet(cUnit, mir, kUnsignedHalf, fieldOffset);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002271 break;
2272 case OP_IGET_SHORT:
Bill Buzbee1465db52009-09-23 17:17:35 -07002273 genIGet(cUnit, mir, kSignedHalf, fieldOffset);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002274 break;
2275 case OP_IPUT_WIDE:
2276 genIPutWide(cUnit, mir, fieldOffset);
2277 break;
2278 case OP_IPUT:
2279 case OP_IPUT_OBJECT:
Bill Buzbee1465db52009-09-23 17:17:35 -07002280 genIPut(cUnit, mir, kWord, fieldOffset);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002281 break;
2282 case OP_IPUT_SHORT:
2283 case OP_IPUT_CHAR:
Bill Buzbee1465db52009-09-23 17:17:35 -07002284 genIPut(cUnit, mir, kUnsignedHalf, fieldOffset);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002285 break;
2286 case OP_IPUT_BYTE:
2287 case OP_IPUT_BOOLEAN:
Bill Buzbee1465db52009-09-23 17:17:35 -07002288 genIPut(cUnit, mir, kUnsignedByte, fieldOffset);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002289 break;
Bill Buzbeeb16344a2010-03-15 17:19:12 -07002290 case OP_IGET_WIDE_VOLATILE:
2291 case OP_IPUT_WIDE_VOLATILE:
2292 case OP_SGET_WIDE_VOLATILE:
2293 case OP_SPUT_WIDE_VOLATILE:
2294 genInterpSingleStep(cUnit, mir);
2295 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002296 default:
2297 return true;
2298 }
2299 return false;
2300}
2301
2302static bool handleFmt22cs(CompilationUnit *cUnit, MIR *mir)
2303{
2304 OpCode dalvikOpCode = mir->dalvikInsn.opCode;
2305 int fieldOffset = mir->dalvikInsn.vC;
2306 switch (dalvikOpCode) {
2307 case OP_IGET_QUICK:
2308 case OP_IGET_OBJECT_QUICK:
Bill Buzbee1465db52009-09-23 17:17:35 -07002309 genIGet(cUnit, mir, kWord, fieldOffset);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002310 break;
2311 case OP_IPUT_QUICK:
2312 case OP_IPUT_OBJECT_QUICK:
Bill Buzbee1465db52009-09-23 17:17:35 -07002313 genIPut(cUnit, mir, kWord, fieldOffset);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002314 break;
2315 case OP_IGET_WIDE_QUICK:
2316 genIGetWide(cUnit, mir, fieldOffset);
2317 break;
2318 case OP_IPUT_WIDE_QUICK:
2319 genIPutWide(cUnit, mir, fieldOffset);
2320 break;
2321 default:
2322 return true;
2323 }
2324 return false;
2325
2326}
2327
2328/* Compare agaist zero */
2329static bool handleFmt22t(CompilationUnit *cUnit, MIR *mir, BasicBlock *bb,
Bill Buzbee89efc3d2009-07-28 11:22:22 -07002330 ArmLIR *labelList)
Ben Chengba4fc8b2009-06-01 13:00:29 -07002331{
2332 OpCode dalvikOpCode = mir->dalvikInsn.opCode;
Bill Buzbee89efc3d2009-07-28 11:22:22 -07002333 ArmConditionCode cond;
Bill Buzbeec6f10662010-02-09 11:16:15 -08002334 RegLocation rlSrc1 = dvmCompilerGetSrc(cUnit, mir, 0);
2335 RegLocation rlSrc2 = dvmCompilerGetSrc(cUnit, mir, 1);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002336
Bill Buzbee1465db52009-09-23 17:17:35 -07002337 rlSrc1 = loadValue(cUnit, rlSrc1, kCoreReg);
2338 rlSrc2 = loadValue(cUnit, rlSrc2, kCoreReg);
2339 opRegReg(cUnit, kOpCmp, rlSrc1.lowReg, rlSrc2.lowReg);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002340
2341 switch (dalvikOpCode) {
2342 case OP_IF_EQ:
Bill Buzbee1465db52009-09-23 17:17:35 -07002343 cond = kArmCondEq;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002344 break;
2345 case OP_IF_NE:
Bill Buzbee1465db52009-09-23 17:17:35 -07002346 cond = kArmCondNe;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002347 break;
2348 case OP_IF_LT:
Bill Buzbee1465db52009-09-23 17:17:35 -07002349 cond = kArmCondLt;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002350 break;
2351 case OP_IF_GE:
Bill Buzbee1465db52009-09-23 17:17:35 -07002352 cond = kArmCondGe;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002353 break;
2354 case OP_IF_GT:
Bill Buzbee1465db52009-09-23 17:17:35 -07002355 cond = kArmCondGt;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002356 break;
2357 case OP_IF_LE:
Bill Buzbee1465db52009-09-23 17:17:35 -07002358 cond = kArmCondLe;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002359 break;
2360 default:
2361 cond = 0;
2362 LOGE("Unexpected opcode (%d) for Fmt22t\n", dalvikOpCode);
Bill Buzbeefc519dc2010-03-06 23:30:57 -08002363 dvmCompilerAbort(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002364 }
2365 genConditionalBranch(cUnit, cond, &labelList[bb->taken->id]);
2366 /* This mostly likely will be optimized away in a later phase */
2367 genUnconditionalBranch(cUnit, &labelList[bb->fallThrough->id]);
2368 return false;
2369}
2370
2371static bool handleFmt22x_Fmt32x(CompilationUnit *cUnit, MIR *mir)
2372{
2373 OpCode opCode = mir->dalvikInsn.opCode;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002374
2375 switch (opCode) {
2376 case OP_MOVE_16:
2377 case OP_MOVE_OBJECT_16:
2378 case OP_MOVE_FROM16:
Ben Chenge9695e52009-06-16 16:11:47 -07002379 case OP_MOVE_OBJECT_FROM16: {
Bill Buzbeec6f10662010-02-09 11:16:15 -08002380 storeValue(cUnit, dvmCompilerGetDest(cUnit, mir, 0),
2381 dvmCompilerGetSrc(cUnit, mir, 0));
Ben Chengba4fc8b2009-06-01 13:00:29 -07002382 break;
Ben Chenge9695e52009-06-16 16:11:47 -07002383 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07002384 case OP_MOVE_WIDE_16:
Ben Chenge9695e52009-06-16 16:11:47 -07002385 case OP_MOVE_WIDE_FROM16: {
Bill Buzbeec6f10662010-02-09 11:16:15 -08002386 storeValueWide(cUnit, dvmCompilerGetDestWide(cUnit, mir, 0, 1),
2387 dvmCompilerGetSrcWide(cUnit, mir, 0, 1));
Ben Chengba4fc8b2009-06-01 13:00:29 -07002388 break;
Ben Chenge9695e52009-06-16 16:11:47 -07002389 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07002390 default:
2391 return true;
2392 }
2393 return false;
2394}
2395
2396static bool handleFmt23x(CompilationUnit *cUnit, MIR *mir)
2397{
2398 OpCode opCode = mir->dalvikInsn.opCode;
Bill Buzbee1465db52009-09-23 17:17:35 -07002399 RegLocation rlSrc1;
2400 RegLocation rlSrc2;
2401 RegLocation rlDest;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002402
2403 if ( (opCode >= OP_ADD_INT) && (opCode <= OP_REM_DOUBLE)) {
Ben Cheng5d90c202009-11-22 23:31:11 -08002404 return genArithOp( cUnit, mir );
Ben Chengba4fc8b2009-06-01 13:00:29 -07002405 }
2406
Bill Buzbee1465db52009-09-23 17:17:35 -07002407 /* APUTs have 3 sources and no targets */
2408 if (mir->ssaRep->numDefs == 0) {
2409 if (mir->ssaRep->numUses == 3) {
Bill Buzbeec6f10662010-02-09 11:16:15 -08002410 rlDest = dvmCompilerGetSrc(cUnit, mir, 0);
2411 rlSrc1 = dvmCompilerGetSrc(cUnit, mir, 1);
2412 rlSrc2 = dvmCompilerGetSrc(cUnit, mir, 2);
Bill Buzbee1465db52009-09-23 17:17:35 -07002413 } else {
2414 assert(mir->ssaRep->numUses == 4);
Bill Buzbeec6f10662010-02-09 11:16:15 -08002415 rlDest = dvmCompilerGetSrcWide(cUnit, mir, 0, 1);
2416 rlSrc1 = dvmCompilerGetSrc(cUnit, mir, 2);
2417 rlSrc2 = dvmCompilerGetSrc(cUnit, mir, 3);
Bill Buzbee1465db52009-09-23 17:17:35 -07002418 }
2419 } else {
2420 /* Two sources and 1 dest. Deduce the operand sizes */
2421 if (mir->ssaRep->numUses == 4) {
Bill Buzbeec6f10662010-02-09 11:16:15 -08002422 rlSrc1 = dvmCompilerGetSrcWide(cUnit, mir, 0, 1);
2423 rlSrc2 = dvmCompilerGetSrcWide(cUnit, mir, 2, 3);
Bill Buzbee1465db52009-09-23 17:17:35 -07002424 } else {
2425 assert(mir->ssaRep->numUses == 2);
Bill Buzbeec6f10662010-02-09 11:16:15 -08002426 rlSrc1 = dvmCompilerGetSrc(cUnit, mir, 0);
2427 rlSrc2 = dvmCompilerGetSrc(cUnit, mir, 1);
Bill Buzbee1465db52009-09-23 17:17:35 -07002428 }
2429 if (mir->ssaRep->numDefs == 2) {
Bill Buzbeec6f10662010-02-09 11:16:15 -08002430 rlDest = dvmCompilerGetDestWide(cUnit, mir, 0, 1);
Bill Buzbee1465db52009-09-23 17:17:35 -07002431 } else {
2432 assert(mir->ssaRep->numDefs == 1);
Bill Buzbeec6f10662010-02-09 11:16:15 -08002433 rlDest = dvmCompilerGetDest(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07002434 }
2435 }
2436
2437
Ben Chengba4fc8b2009-06-01 13:00:29 -07002438 switch (opCode) {
Bill Buzbeed45ba372009-06-15 17:00:57 -07002439 case OP_CMPL_FLOAT:
2440 case OP_CMPG_FLOAT:
2441 case OP_CMPL_DOUBLE:
2442 case OP_CMPG_DOUBLE:
Ben Cheng5d90c202009-11-22 23:31:11 -08002443 return genCmpFP(cUnit, mir, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002444 case OP_CMP_LONG:
Bill Buzbee1465db52009-09-23 17:17:35 -07002445 genCmpLong(cUnit, mir, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002446 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002447 case OP_AGET_WIDE:
Bill Buzbee1465db52009-09-23 17:17:35 -07002448 genArrayGet(cUnit, mir, kLong, rlSrc1, rlSrc2, rlDest, 3);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002449 break;
2450 case OP_AGET:
2451 case OP_AGET_OBJECT:
Bill Buzbee1465db52009-09-23 17:17:35 -07002452 genArrayGet(cUnit, mir, kWord, rlSrc1, rlSrc2, rlDest, 2);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002453 break;
2454 case OP_AGET_BOOLEAN:
Bill Buzbee1465db52009-09-23 17:17:35 -07002455 genArrayGet(cUnit, mir, kUnsignedByte, rlSrc1, rlSrc2, rlDest, 0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002456 break;
2457 case OP_AGET_BYTE:
Bill Buzbee1465db52009-09-23 17:17:35 -07002458 genArrayGet(cUnit, mir, kSignedByte, rlSrc1, rlSrc2, rlDest, 0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002459 break;
2460 case OP_AGET_CHAR:
Bill Buzbee1465db52009-09-23 17:17:35 -07002461 genArrayGet(cUnit, mir, kUnsignedHalf, rlSrc1, rlSrc2, rlDest, 1);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002462 break;
2463 case OP_AGET_SHORT:
Bill Buzbee1465db52009-09-23 17:17:35 -07002464 genArrayGet(cUnit, mir, kSignedHalf, rlSrc1, rlSrc2, rlDest, 1);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002465 break;
2466 case OP_APUT_WIDE:
Bill Buzbee1465db52009-09-23 17:17:35 -07002467 genArrayPut(cUnit, mir, kLong, rlSrc1, rlSrc2, rlDest, 3);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002468 break;
2469 case OP_APUT:
Bill Buzbee1465db52009-09-23 17:17:35 -07002470 genArrayPut(cUnit, mir, kWord, rlSrc1, rlSrc2, rlDest, 2);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002471 break;
Bill Buzbeebe6534f2010-03-12 16:01:35 -08002472 case OP_APUT_OBJECT:
2473 genArrayObjectPut(cUnit, mir, rlSrc1, rlSrc2, rlDest, 2);
2474 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002475 case OP_APUT_SHORT:
2476 case OP_APUT_CHAR:
Bill Buzbee1465db52009-09-23 17:17:35 -07002477 genArrayPut(cUnit, mir, kUnsignedHalf, rlSrc1, rlSrc2, rlDest, 1);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002478 break;
2479 case OP_APUT_BYTE:
2480 case OP_APUT_BOOLEAN:
Bill Buzbee1465db52009-09-23 17:17:35 -07002481 genArrayPut(cUnit, mir, kUnsignedByte, rlSrc1, rlSrc2, rlDest, 0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002482 break;
2483 default:
2484 return true;
2485 }
2486 return false;
2487}
2488
Ben Cheng6c10a972009-10-29 14:39:18 -07002489/*
2490 * Find the matching case.
2491 *
2492 * return values:
2493 * r0 (low 32-bit): pc of the chaining cell corresponding to the resolved case,
2494 * including default which is placed at MIN(size, MAX_CHAINED_SWITCH_CASES).
2495 * r1 (high 32-bit): the branch offset of the matching case (only for indexes
2496 * above MAX_CHAINED_SWITCH_CASES).
2497 *
2498 * Instructions around the call are:
2499 *
2500 * mov r2, pc
2501 * blx &findPackedSwitchIndex
2502 * mov pc, r0
2503 * .align4
2504 * chaining cell for case 0 [8 bytes]
2505 * chaining cell for case 1 [8 bytes]
2506 * :
2507 * chaining cell for case MIN(size, MAX_CHAINED_SWITCH_CASES)-1 [8 bytes]
2508 * chaining cell for case default [8 bytes]
2509 * noChain exit
2510 */
Ben Chengbd1326d2010-04-02 15:04:53 -07002511static s8 findPackedSwitchIndex(const u2* switchData, int testVal, int pc)
Ben Cheng6c10a972009-10-29 14:39:18 -07002512{
2513 int size;
2514 int firstKey;
2515 const int *entries;
2516 int index;
2517 int jumpIndex;
2518 int caseDPCOffset = 0;
2519 /* In Thumb mode pc is 4 ahead of the "mov r2, pc" instruction */
2520 int chainingPC = (pc + 4) & ~3;
2521
2522 /*
2523 * Packed switch data format:
2524 * ushort ident = 0x0100 magic value
2525 * ushort size number of entries in the table
2526 * int first_key first (and lowest) switch case value
2527 * int targets[size] branch targets, relative to switch opcode
2528 *
2529 * Total size is (4+size*2) 16-bit code units.
2530 */
2531 size = switchData[1];
2532 assert(size > 0);
2533
2534 firstKey = switchData[2];
2535 firstKey |= switchData[3] << 16;
2536
2537
2538 /* The entries are guaranteed to be aligned on a 32-bit boundary;
2539 * we can treat them as a native int array.
2540 */
2541 entries = (const int*) &switchData[4];
2542 assert(((u4)entries & 0x3) == 0);
2543
2544 index = testVal - firstKey;
2545
2546 /* Jump to the default cell */
2547 if (index < 0 || index >= size) {
2548 jumpIndex = MIN(size, MAX_CHAINED_SWITCH_CASES);
2549 /* Jump to the non-chaining exit point */
2550 } else if (index >= MAX_CHAINED_SWITCH_CASES) {
2551 jumpIndex = MAX_CHAINED_SWITCH_CASES + 1;
2552 caseDPCOffset = entries[index];
2553 /* Jump to the inline chaining cell */
2554 } else {
2555 jumpIndex = index;
2556 }
2557
2558 chainingPC += jumpIndex * 8;
2559 return (((s8) caseDPCOffset) << 32) | (u8) chainingPC;
2560}
2561
2562/* See comments for findPackedSwitchIndex */
Ben Chengbd1326d2010-04-02 15:04:53 -07002563static s8 findSparseSwitchIndex(const u2* switchData, int testVal, int pc)
Ben Cheng6c10a972009-10-29 14:39:18 -07002564{
2565 int size;
2566 const int *keys;
2567 const int *entries;
2568 int chainingPC = (pc + 4) & ~3;
2569 int i;
2570
2571 /*
2572 * Sparse switch data format:
2573 * ushort ident = 0x0200 magic value
2574 * ushort size number of entries in the table; > 0
2575 * int keys[size] keys, sorted low-to-high; 32-bit aligned
2576 * int targets[size] branch targets, relative to switch opcode
2577 *
2578 * Total size is (2+size*4) 16-bit code units.
2579 */
2580
2581 size = switchData[1];
2582 assert(size > 0);
2583
2584 /* The keys are guaranteed to be aligned on a 32-bit boundary;
2585 * we can treat them as a native int array.
2586 */
2587 keys = (const int*) &switchData[2];
2588 assert(((u4)keys & 0x3) == 0);
2589
2590 /* The entries are guaranteed to be aligned on a 32-bit boundary;
2591 * we can treat them as a native int array.
2592 */
2593 entries = keys + size;
2594 assert(((u4)entries & 0x3) == 0);
2595
2596 /*
2597 * Run through the list of keys, which are guaranteed to
2598 * be sorted low-to-high.
2599 *
2600 * Most tables have 3-4 entries. Few have more than 10. A binary
2601 * search here is probably not useful.
2602 */
2603 for (i = 0; i < size; i++) {
2604 int k = keys[i];
2605 if (k == testVal) {
2606 /* MAX_CHAINED_SWITCH_CASES + 1 is the start of the overflow case */
2607 int jumpIndex = (i < MAX_CHAINED_SWITCH_CASES) ?
2608 i : MAX_CHAINED_SWITCH_CASES + 1;
2609 chainingPC += jumpIndex * 8;
2610 return (((s8) entries[i]) << 32) | (u8) chainingPC;
2611 } else if (k > testVal) {
2612 break;
2613 }
2614 }
2615 return chainingPC + MIN(size, MAX_CHAINED_SWITCH_CASES) * 8;
2616}
2617
Ben Chengba4fc8b2009-06-01 13:00:29 -07002618static bool handleFmt31t(CompilationUnit *cUnit, MIR *mir)
2619{
2620 OpCode dalvikOpCode = mir->dalvikInsn.opCode;
2621 switch (dalvikOpCode) {
2622 case OP_FILL_ARRAY_DATA: {
Bill Buzbeec6f10662010-02-09 11:16:15 -08002623 RegLocation rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07002624 // Making a call - use explicit registers
Bill Buzbeec6f10662010-02-09 11:16:15 -08002625 dvmCompilerFlushAllRegs(cUnit); /* Everything to home location */
Bill Buzbee1465db52009-09-23 17:17:35 -07002626 genExportPC(cUnit, mir);
2627 loadValueDirectFixed(cUnit, rlSrc, r0);
Ben Chengbd1326d2010-04-02 15:04:53 -07002628 LOAD_FUNC_ADDR(cUnit, r2, (int)dvmInterpHandleFillArrayData);
Ben Cheng6c10a972009-10-29 14:39:18 -07002629 loadConstant(cUnit, r1,
2630 (int) (cUnit->method->insns + mir->offset + mir->dalvikInsn.vB));
Bill Buzbee1465db52009-09-23 17:17:35 -07002631 opReg(cUnit, kOpBlx, r2);
Elliott Hughes6a555132010-02-25 15:41:42 -08002632 dvmCompilerClobberCallRegs(cUnit);
Bill Buzbeefd023aa2009-11-02 09:23:49 -08002633 /* generate a branch over if successful */
2634 opRegImm(cUnit, kOpCmp, r0, 0); /* NULL? */
2635 ArmLIR *branchOver = opCondBranch(cUnit, kArmCondNe);
2636 loadConstant(cUnit, r0,
2637 (int) (cUnit->method->insns + mir->offset));
2638 genDispatchToHandler(cUnit, TEMPLATE_THROW_EXCEPTION_COMMON);
2639 ArmLIR *target = newLIR0(cUnit, kArmPseudoTargetLabel);
2640 target->defMask = ENCODE_ALL;
2641 branchOver->generic.target = (LIR *) target;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002642 break;
2643 }
2644 /*
Ben Cheng6c10a972009-10-29 14:39:18 -07002645 * Compute the goto target of up to
2646 * MIN(switchSize, MAX_CHAINED_SWITCH_CASES) + 1 chaining cells.
2647 * See the comment before findPackedSwitchIndex for the code layout.
Ben Chengba4fc8b2009-06-01 13:00:29 -07002648 */
2649 case OP_PACKED_SWITCH:
2650 case OP_SPARSE_SWITCH: {
Bill Buzbeec6f10662010-02-09 11:16:15 -08002651 RegLocation rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
2652 dvmCompilerFlushAllRegs(cUnit); /* Everything to home location */
Bill Buzbee1465db52009-09-23 17:17:35 -07002653 loadValueDirectFixed(cUnit, rlSrc, r1);
Bill Buzbeec6f10662010-02-09 11:16:15 -08002654 dvmCompilerLockAllTemps(cUnit);
Ben Cheng6c10a972009-10-29 14:39:18 -07002655 const u2 *switchData =
2656 cUnit->method->insns + mir->offset + mir->dalvikInsn.vB;
2657 u2 size = switchData[1];
2658
Ben Chengba4fc8b2009-06-01 13:00:29 -07002659 if (dalvikOpCode == OP_PACKED_SWITCH) {
Ben Chengbd1326d2010-04-02 15:04:53 -07002660 LOAD_FUNC_ADDR(cUnit, r4PC, (int)findPackedSwitchIndex);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002661 } else {
Ben Chengbd1326d2010-04-02 15:04:53 -07002662 LOAD_FUNC_ADDR(cUnit, r4PC, (int)findSparseSwitchIndex);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002663 }
Ben Cheng6c10a972009-10-29 14:39:18 -07002664 /* r0 <- Addr of the switch data */
2665 loadConstant(cUnit, r0,
2666 (int) (cUnit->method->insns + mir->offset + mir->dalvikInsn.vB));
2667 /* r2 <- pc of the instruction following the blx */
2668 opRegReg(cUnit, kOpMov, r2, rpc);
Bill Buzbee1465db52009-09-23 17:17:35 -07002669 opReg(cUnit, kOpBlx, r4PC);
Elliott Hughes6a555132010-02-25 15:41:42 -08002670 dvmCompilerClobberCallRegs(cUnit);
Ben Cheng6c10a972009-10-29 14:39:18 -07002671 /* pc <- computed goto target */
2672 opRegReg(cUnit, kOpMov, rpc, r0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002673 break;
2674 }
2675 default:
2676 return true;
2677 }
2678 return false;
2679}
2680
2681static bool handleFmt35c_3rc(CompilationUnit *cUnit, MIR *mir, BasicBlock *bb,
Bill Buzbee89efc3d2009-07-28 11:22:22 -07002682 ArmLIR *labelList)
Ben Chengba4fc8b2009-06-01 13:00:29 -07002683{
Bill Buzbee9bc3df32009-07-30 10:52:29 -07002684 ArmLIR *retChainingCell = NULL;
Bill Buzbee89efc3d2009-07-28 11:22:22 -07002685 ArmLIR *pcrLabel = NULL;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002686
Bill Buzbeef4ce16f2009-07-28 13:28:25 -07002687 if (bb->fallThrough != NULL)
2688 retChainingCell = &labelList[bb->fallThrough->id];
2689
Ben Chengba4fc8b2009-06-01 13:00:29 -07002690 DecodedInstruction *dInsn = &mir->dalvikInsn;
2691 switch (mir->dalvikInsn.opCode) {
2692 /*
2693 * calleeMethod = this->clazz->vtable[
2694 * method->clazz->pDvmDex->pResMethods[BBBB]->methodIndex
2695 * ]
2696 */
2697 case OP_INVOKE_VIRTUAL:
2698 case OP_INVOKE_VIRTUAL_RANGE: {
Bill Buzbee89efc3d2009-07-28 11:22:22 -07002699 ArmLIR *predChainingCell = &labelList[bb->taken->id];
Ben Chengba4fc8b2009-06-01 13:00:29 -07002700 int methodIndex =
2701 cUnit->method->clazz->pDvmDex->pResMethods[dInsn->vB]->
2702 methodIndex;
2703
2704 if (mir->dalvikInsn.opCode == OP_INVOKE_VIRTUAL)
2705 genProcessArgsNoRange(cUnit, mir, dInsn, &pcrLabel);
2706 else
2707 genProcessArgsRange(cUnit, mir, dInsn, &pcrLabel);
2708
Ben Cheng38329f52009-07-07 14:19:20 -07002709 genInvokeVirtualCommon(cUnit, mir, methodIndex,
2710 retChainingCell,
2711 predChainingCell,
2712 pcrLabel);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002713 break;
2714 }
2715 /*
2716 * calleeMethod = method->clazz->super->vtable[method->clazz->pDvmDex
2717 * ->pResMethods[BBBB]->methodIndex]
2718 */
Ben Chengba4fc8b2009-06-01 13:00:29 -07002719 case OP_INVOKE_SUPER:
2720 case OP_INVOKE_SUPER_RANGE: {
2721 int mIndex = cUnit->method->clazz->pDvmDex->
2722 pResMethods[dInsn->vB]->methodIndex;
2723 const Method *calleeMethod =
2724 cUnit->method->clazz->super->vtable[mIndex];
2725
2726 if (mir->dalvikInsn.opCode == OP_INVOKE_SUPER)
2727 genProcessArgsNoRange(cUnit, mir, dInsn, &pcrLabel);
2728 else
2729 genProcessArgsRange(cUnit, mir, dInsn, &pcrLabel);
2730
2731 /* r0 = calleeMethod */
2732 loadConstant(cUnit, r0, (int) calleeMethod);
2733
Ben Cheng38329f52009-07-07 14:19:20 -07002734 genInvokeSingletonCommon(cUnit, mir, bb, labelList, pcrLabel,
2735 calleeMethod);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002736 break;
2737 }
2738 /* calleeMethod = method->clazz->pDvmDex->pResMethods[BBBB] */
2739 case OP_INVOKE_DIRECT:
2740 case OP_INVOKE_DIRECT_RANGE: {
2741 const Method *calleeMethod =
2742 cUnit->method->clazz->pDvmDex->pResMethods[dInsn->vB];
2743
2744 if (mir->dalvikInsn.opCode == OP_INVOKE_DIRECT)
2745 genProcessArgsNoRange(cUnit, mir, dInsn, &pcrLabel);
2746 else
2747 genProcessArgsRange(cUnit, mir, dInsn, &pcrLabel);
2748
2749 /* r0 = calleeMethod */
2750 loadConstant(cUnit, r0, (int) calleeMethod);
2751
Ben Cheng38329f52009-07-07 14:19:20 -07002752 genInvokeSingletonCommon(cUnit, mir, bb, labelList, pcrLabel,
2753 calleeMethod);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002754 break;
2755 }
2756 /* calleeMethod = method->clazz->pDvmDex->pResMethods[BBBB] */
2757 case OP_INVOKE_STATIC:
2758 case OP_INVOKE_STATIC_RANGE: {
2759 const Method *calleeMethod =
2760 cUnit->method->clazz->pDvmDex->pResMethods[dInsn->vB];
2761
2762 if (mir->dalvikInsn.opCode == OP_INVOKE_STATIC)
2763 genProcessArgsNoRange(cUnit, mir, dInsn,
2764 NULL /* no null check */);
2765 else
2766 genProcessArgsRange(cUnit, mir, dInsn,
2767 NULL /* no null check */);
2768
2769 /* r0 = calleeMethod */
2770 loadConstant(cUnit, r0, (int) calleeMethod);
2771
Ben Cheng38329f52009-07-07 14:19:20 -07002772 genInvokeSingletonCommon(cUnit, mir, bb, labelList, pcrLabel,
2773 calleeMethod);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002774 break;
2775 }
Ben Cheng09e50c92010-05-02 10:45:32 -07002776 /*
Ben Chengba4fc8b2009-06-01 13:00:29 -07002777 * calleeMethod = dvmFindInterfaceMethodInCache(this->clazz,
2778 * BBBB, method, method->clazz->pDvmDex)
Ben Cheng38329f52009-07-07 14:19:20 -07002779 *
Ben Cheng09e50c92010-05-02 10:45:32 -07002780 * The following is an example of generated code for
2781 * "invoke-interface v0"
Ben Cheng38329f52009-07-07 14:19:20 -07002782 *
Ben Cheng09e50c92010-05-02 10:45:32 -07002783 * -------- dalvik offset: 0x0008 @ invoke-interface v0
2784 * 0x47357e36 : ldr r0, [r5, #0] --+
2785 * 0x47357e38 : sub r7,r5,#24 |
2786 * 0x47357e3c : cmp r0, #0 | genProcessArgsNoRange
2787 * 0x47357e3e : beq 0x47357e82 |
2788 * 0x47357e40 : stmia r7, <r0> --+
2789 * 0x47357e42 : ldr r4, [pc, #120] --> r4 <- dalvikPC of this invoke
2790 * 0x47357e44 : add r1, pc, #64 --> r1 <- &retChainingCell
2791 * 0x47357e46 : add r2, pc, #72 --> r2 <- &predictedChainingCell
2792 * 0x47357e48 : blx_1 0x47348190 --+ TEMPLATE_INVOKE_METHOD_
2793 * 0x47357e4a : blx_2 see above --+ PREDICTED_CHAIN
2794 * 0x47357e4c : b 0x47357e90 --> off to the predicted chain
2795 * 0x47357e4e : b 0x47357e82 --> punt to the interpreter
2796 * 0x47357e50 : mov r8, r1 --+
2797 * 0x47357e52 : mov r9, r2 |
2798 * 0x47357e54 : ldr r2, [pc, #96] |
2799 * 0x47357e56 : mov r10, r3 |
2800 * 0x47357e58 : movs r0, r3 | dvmFindInterfaceMethodInCache
2801 * 0x47357e5a : ldr r3, [pc, #88] |
2802 * 0x47357e5c : ldr r7, [pc, #80] |
2803 * 0x47357e5e : mov r1, #1452 |
2804 * 0x47357e62 : blx r7 --+
2805 * 0x47357e64 : cmp r0, #0 --> calleeMethod == NULL?
2806 * 0x47357e66 : bne 0x47357e6e --> branch over the throw if !r0
2807 * 0x47357e68 : ldr r0, [pc, #80] --> load Dalvik PC of the invoke
2808 * 0x47357e6a : blx_1 0x47348494 --+ TEMPLATE_THROW_EXCEPTION_
2809 * 0x47357e6c : blx_2 see above --+ COMMON
2810 * 0x47357e6e : mov r1, r8 --> r1 <- &retChainingCell
2811 * 0x47357e70 : cmp r1, #0 --> compare against 0
2812 * 0x47357e72 : bgt 0x47357e7c --> >=0? don't rechain
2813 * 0x47357e74 : ldr r7, [r6, #108] --+
2814 * 0x47357e76 : mov r2, r9 | dvmJitToPatchPredictedChain
2815 * 0x47357e78 : mov r3, r10 |
2816 * 0x47357e7a : blx r7 --+
2817 * 0x47357e7c : add r1, pc, #8 --> r1 <- &retChainingCell
2818 * 0x47357e7e : blx_1 0x4734809c --+ TEMPLATE_INVOKE_METHOD_NO_OPT
2819 * 0x47357e80 : blx_2 see above --+
2820 * -------- reconstruct dalvik PC : 0x425719dc @ +0x0008
2821 * 0x47357e82 : ldr r0, [pc, #56]
Ben Cheng38329f52009-07-07 14:19:20 -07002822 * Exception_Handling:
Ben Cheng09e50c92010-05-02 10:45:32 -07002823 * 0x47357e84 : ldr r1, [r6, #92]
2824 * 0x47357e86 : blx r1
2825 * 0x47357e88 : .align4
2826 * -------- chaining cell (hot): 0x000b
2827 * 0x47357e88 : ldr r0, [r6, #104]
2828 * 0x47357e8a : blx r0
2829 * 0x47357e8c : data 0x19e2(6626)
2830 * 0x47357e8e : data 0x4257(16983)
2831 * 0x47357e90 : .align4
Ben Cheng38329f52009-07-07 14:19:20 -07002832 * -------- chaining cell (predicted)
Ben Cheng09e50c92010-05-02 10:45:32 -07002833 * 0x47357e90 : data 0xe7fe(59390) --> will be patched into bx
2834 * 0x47357e92 : data 0x0000(0)
2835 * 0x47357e94 : data 0x0000(0) --> class
2836 * 0x47357e96 : data 0x0000(0)
2837 * 0x47357e98 : data 0x0000(0) --> method
2838 * 0x47357e9a : data 0x0000(0)
2839 * 0x47357e9c : data 0x0000(0) --> rechain count
2840 * 0x47357e9e : data 0x0000(0)
2841 * -------- end of chaining cells (0x006c)
2842 * 0x47357eb0 : .word (0xad03e369)
2843 * 0x47357eb4 : .word (0x28a90)
2844 * 0x47357eb8 : .word (0x41a63394)
2845 * 0x47357ebc : .word (0x425719dc)
Ben Chengba4fc8b2009-06-01 13:00:29 -07002846 */
2847 case OP_INVOKE_INTERFACE:
2848 case OP_INVOKE_INTERFACE_RANGE: {
Bill Buzbee89efc3d2009-07-28 11:22:22 -07002849 ArmLIR *predChainingCell = &labelList[bb->taken->id];
Ben Chengba4fc8b2009-06-01 13:00:29 -07002850 int methodIndex = dInsn->vB;
2851
Bill Buzbee1465db52009-09-23 17:17:35 -07002852 /* Ensure that nothing is both live and dirty */
Bill Buzbeec6f10662010-02-09 11:16:15 -08002853 dvmCompilerFlushAllRegs(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07002854
Ben Chengba4fc8b2009-06-01 13:00:29 -07002855 if (mir->dalvikInsn.opCode == OP_INVOKE_INTERFACE)
2856 genProcessArgsNoRange(cUnit, mir, dInsn, &pcrLabel);
2857 else
2858 genProcessArgsRange(cUnit, mir, dInsn, &pcrLabel);
2859
Ben Cheng38329f52009-07-07 14:19:20 -07002860 /* "this" is already left in r0 by genProcessArgs* */
2861
2862 /* r4PC = dalvikCallsite */
2863 loadConstant(cUnit, r4PC,
2864 (int) (cUnit->method->insns + mir->offset));
2865
2866 /* r1 = &retChainingCell */
Bill Buzbee270c1d62009-08-13 16:58:07 -07002867 ArmLIR *addrRetChain =
Bill Buzbee1465db52009-09-23 17:17:35 -07002868 opRegRegImm(cUnit, kOpAdd, r1, rpc, 0);
Ben Cheng38329f52009-07-07 14:19:20 -07002869 addrRetChain->generic.target = (LIR *) retChainingCell;
2870
2871 /* r2 = &predictedChainingCell */
Bill Buzbee89efc3d2009-07-28 11:22:22 -07002872 ArmLIR *predictedChainingCell =
Bill Buzbee1465db52009-09-23 17:17:35 -07002873 opRegRegImm(cUnit, kOpAdd, r2, rpc, 0);
Ben Cheng38329f52009-07-07 14:19:20 -07002874 predictedChainingCell->generic.target = (LIR *) predChainingCell;
2875
2876 genDispatchToHandler(cUnit, TEMPLATE_INVOKE_METHOD_PREDICTED_CHAIN);
2877
2878 /* return through lr - jump to the chaining cell */
2879 genUnconditionalBranch(cUnit, predChainingCell);
2880
2881 /*
2882 * null-check on "this" may have been eliminated, but we still need
2883 * a PC-reconstruction label for stack overflow bailout.
2884 */
2885 if (pcrLabel == NULL) {
2886 int dPC = (int) (cUnit->method->insns + mir->offset);
Bill Buzbee89efc3d2009-07-28 11:22:22 -07002887 pcrLabel = dvmCompilerNew(sizeof(ArmLIR), true);
Ben Chenga4973592010-03-31 11:59:18 -07002888 pcrLabel->opCode = kArmPseudoPCReconstructionCell;
Ben Cheng38329f52009-07-07 14:19:20 -07002889 pcrLabel->operands[0] = dPC;
2890 pcrLabel->operands[1] = mir->offset;
2891 /* Insert the place holder to the growable list */
2892 dvmInsertGrowableList(&cUnit->pcReconstructionList, pcrLabel);
2893 }
2894
2895 /* return through lr+2 - punt to the interpreter */
2896 genUnconditionalBranch(cUnit, pcrLabel);
2897
2898 /*
2899 * return through lr+4 - fully resolve the callee method.
2900 * r1 <- count
2901 * r2 <- &predictedChainCell
2902 * r3 <- this->class
2903 * r4 <- dPC
2904 * r7 <- this->class->vtable
2905 */
2906
2907 /* Save count, &predictedChainCell, and class to high regs first */
Bill Buzbee1465db52009-09-23 17:17:35 -07002908 genRegCopy(cUnit, r8, r1);
2909 genRegCopy(cUnit, r9, r2);
2910 genRegCopy(cUnit, r10, r3);
Ben Cheng38329f52009-07-07 14:19:20 -07002911
Ben Chengba4fc8b2009-06-01 13:00:29 -07002912 /* r0 now contains this->clazz */
Bill Buzbee1465db52009-09-23 17:17:35 -07002913 genRegCopy(cUnit, r0, r3);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002914
2915 /* r1 = BBBB */
2916 loadConstant(cUnit, r1, dInsn->vB);
2917
2918 /* r2 = method (caller) */
2919 loadConstant(cUnit, r2, (int) cUnit->method);
2920
2921 /* r3 = pDvmDex */
2922 loadConstant(cUnit, r3, (int) cUnit->method->clazz->pDvmDex);
2923
Ben Chengbd1326d2010-04-02 15:04:53 -07002924 LOAD_FUNC_ADDR(cUnit, r7,
2925 (intptr_t) dvmFindInterfaceMethodInCache);
Bill Buzbee1465db52009-09-23 17:17:35 -07002926 opReg(cUnit, kOpBlx, r7);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002927 /* r0 = calleeMethod (returned from dvmFindInterfaceMethodInCache */
2928
Ben Cheng09e50c92010-05-02 10:45:32 -07002929 dvmCompilerClobberCallRegs(cUnit);
2930 /* generate a branch over if the interface method is resolved */
2931 opRegImm(cUnit, kOpCmp, r0, 0); /* NULL? */
2932 ArmLIR *branchOver = opCondBranch(cUnit, kArmCondNe);
2933 /*
2934 * calleeMethod == NULL -> throw
2935 */
2936 loadConstant(cUnit, r0,
2937 (int) (cUnit->method->insns + mir->offset));
2938 genDispatchToHandler(cUnit, TEMPLATE_THROW_EXCEPTION_COMMON);
2939 /* noreturn */
2940
2941 ArmLIR *target = newLIR0(cUnit, kArmPseudoTargetLabel);
2942 target->defMask = ENCODE_ALL;
2943 branchOver->generic.target = (LIR *) target;
2944
Bill Buzbee1465db52009-09-23 17:17:35 -07002945 genRegCopy(cUnit, r1, r8);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002946
Ben Cheng38329f52009-07-07 14:19:20 -07002947 /* Check if rechain limit is reached */
Bill Buzbee1465db52009-09-23 17:17:35 -07002948 opRegImm(cUnit, kOpCmp, r1, 0);
Ben Cheng38329f52009-07-07 14:19:20 -07002949
Bill Buzbee1465db52009-09-23 17:17:35 -07002950 ArmLIR *bypassRechaining = opCondBranch(cUnit, kArmCondGt);
Ben Cheng38329f52009-07-07 14:19:20 -07002951
Bill Buzbee270c1d62009-08-13 16:58:07 -07002952 loadWordDisp(cUnit, rGLUE, offsetof(InterpState,
2953 jitToInterpEntries.dvmJitToPatchPredictedChain), r7);
Ben Cheng38329f52009-07-07 14:19:20 -07002954
Bill Buzbee1465db52009-09-23 17:17:35 -07002955 genRegCopy(cUnit, r2, r9);
2956 genRegCopy(cUnit, r3, r10);
Ben Cheng38329f52009-07-07 14:19:20 -07002957
2958 /*
2959 * r0 = calleeMethod
2960 * r2 = &predictedChainingCell
2961 * r3 = class
2962 *
2963 * &returnChainingCell has been loaded into r1 but is not needed
2964 * when patching the chaining cell and will be clobbered upon
2965 * returning so it will be reconstructed again.
2966 */
Bill Buzbee1465db52009-09-23 17:17:35 -07002967 opReg(cUnit, kOpBlx, r7);
Ben Cheng38329f52009-07-07 14:19:20 -07002968
2969 /* r1 = &retChainingCell */
Bill Buzbee1465db52009-09-23 17:17:35 -07002970 addrRetChain = opRegRegImm(cUnit, kOpAdd, r1, rpc, 0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002971 addrRetChain->generic.target = (LIR *) retChainingCell;
Ben Cheng38329f52009-07-07 14:19:20 -07002972
2973 bypassRechaining->generic.target = (LIR *) addrRetChain;
2974
Ben Chengba4fc8b2009-06-01 13:00:29 -07002975 /*
2976 * r0 = this, r1 = calleeMethod,
2977 * r1 = &ChainingCell,
2978 * r4PC = callsiteDPC,
2979 */
2980 genDispatchToHandler(cUnit, TEMPLATE_INVOKE_METHOD_NO_OPT);
Ben Cheng978738d2010-05-13 13:45:57 -07002981#if defined(WITH_JIT_TUNING)
Ben Cheng86717f72010-03-05 15:27:21 -08002982 gDvmJit.invokePolymorphic++;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002983#endif
2984 /* Handle exceptions using the interpreter */
2985 genTrap(cUnit, mir->offset, pcrLabel);
2986 break;
2987 }
2988 /* NOP */
2989 case OP_INVOKE_DIRECT_EMPTY: {
2990 return false;
2991 }
2992 case OP_FILLED_NEW_ARRAY:
2993 case OP_FILLED_NEW_ARRAY_RANGE: {
2994 /* Just let the interpreter deal with these */
2995 genInterpSingleStep(cUnit, mir);
2996 break;
2997 }
2998 default:
2999 return true;
3000 }
3001 return false;
3002}
3003
3004static bool handleFmt35ms_3rms(CompilationUnit *cUnit, MIR *mir,
Bill Buzbee89efc3d2009-07-28 11:22:22 -07003005 BasicBlock *bb, ArmLIR *labelList)
Ben Chengba4fc8b2009-06-01 13:00:29 -07003006{
Bill Buzbee89efc3d2009-07-28 11:22:22 -07003007 ArmLIR *retChainingCell = &labelList[bb->fallThrough->id];
3008 ArmLIR *predChainingCell = &labelList[bb->taken->id];
3009 ArmLIR *pcrLabel = NULL;
Ben Chengba4fc8b2009-06-01 13:00:29 -07003010
3011 DecodedInstruction *dInsn = &mir->dalvikInsn;
3012 switch (mir->dalvikInsn.opCode) {
3013 /* calleeMethod = this->clazz->vtable[BBBB] */
3014 case OP_INVOKE_VIRTUAL_QUICK_RANGE:
3015 case OP_INVOKE_VIRTUAL_QUICK: {
3016 int methodIndex = dInsn->vB;
3017 if (mir->dalvikInsn.opCode == OP_INVOKE_VIRTUAL_QUICK)
3018 genProcessArgsNoRange(cUnit, mir, dInsn, &pcrLabel);
3019 else
3020 genProcessArgsRange(cUnit, mir, dInsn, &pcrLabel);
3021
Ben Cheng38329f52009-07-07 14:19:20 -07003022 genInvokeVirtualCommon(cUnit, mir, methodIndex,
3023 retChainingCell,
3024 predChainingCell,
3025 pcrLabel);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003026 break;
3027 }
3028 /* calleeMethod = method->clazz->super->vtable[BBBB] */
3029 case OP_INVOKE_SUPER_QUICK:
3030 case OP_INVOKE_SUPER_QUICK_RANGE: {
3031 const Method *calleeMethod =
3032 cUnit->method->clazz->super->vtable[dInsn->vB];
3033
3034 if (mir->dalvikInsn.opCode == OP_INVOKE_SUPER_QUICK)
3035 genProcessArgsNoRange(cUnit, mir, dInsn, &pcrLabel);
3036 else
3037 genProcessArgsRange(cUnit, mir, dInsn, &pcrLabel);
3038
3039 /* r0 = calleeMethod */
3040 loadConstant(cUnit, r0, (int) calleeMethod);
3041
Ben Cheng38329f52009-07-07 14:19:20 -07003042 genInvokeSingletonCommon(cUnit, mir, bb, labelList, pcrLabel,
3043 calleeMethod);
3044 /* Handle exceptions using the interpreter */
3045 genTrap(cUnit, mir->offset, pcrLabel);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003046 break;
3047 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07003048 default:
3049 return true;
3050 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07003051 return false;
3052}
3053
3054/*
Bill Buzbeefd023aa2009-11-02 09:23:49 -08003055 * This operation is complex enough that we'll do it partly inline
3056 * and partly with a handler. NOTE: the handler uses hardcoded
3057 * values for string object offsets and must be revisitied if the
3058 * layout changes.
3059 */
3060static bool genInlinedCompareTo(CompilationUnit *cUnit, MIR *mir)
3061{
3062#if defined(USE_GLOBAL_STRING_DEFS)
3063 return false;
3064#else
3065 ArmLIR *rollback;
Bill Buzbeec6f10662010-02-09 11:16:15 -08003066 RegLocation rlThis = dvmCompilerGetSrc(cUnit, mir, 0);
3067 RegLocation rlComp = dvmCompilerGetSrc(cUnit, mir, 1);
Bill Buzbeefd023aa2009-11-02 09:23:49 -08003068
3069 loadValueDirectFixed(cUnit, rlThis, r0);
3070 loadValueDirectFixed(cUnit, rlComp, r1);
3071 /* Test objects for NULL */
3072 rollback = genNullCheck(cUnit, rlThis.sRegLow, r0, mir->offset, NULL);
3073 genNullCheck(cUnit, rlComp.sRegLow, r1, mir->offset, rollback);
3074 /*
3075 * TUNING: we could check for object pointer equality before invoking
3076 * handler. Unclear whether the gain would be worth the added code size
3077 * expansion.
3078 */
3079 genDispatchToHandler(cUnit, TEMPLATE_STRING_COMPARETO);
Bill Buzbeec6f10662010-02-09 11:16:15 -08003080 storeValue(cUnit, inlinedTarget(cUnit, mir, false),
3081 dvmCompilerGetReturn(cUnit));
Bill Buzbeefd023aa2009-11-02 09:23:49 -08003082 return true;
3083#endif
3084}
3085
Elliott Hughes2bdbcb62010-04-12 14:29:37 -07003086static bool genInlinedFastIndexOf(CompilationUnit *cUnit, MIR *mir)
Bill Buzbeefd023aa2009-11-02 09:23:49 -08003087{
3088#if defined(USE_GLOBAL_STRING_DEFS)
3089 return false;
3090#else
Bill Buzbeec6f10662010-02-09 11:16:15 -08003091 RegLocation rlThis = dvmCompilerGetSrc(cUnit, mir, 0);
3092 RegLocation rlChar = dvmCompilerGetSrc(cUnit, mir, 1);
Bill Buzbeefd023aa2009-11-02 09:23:49 -08003093
3094 loadValueDirectFixed(cUnit, rlThis, r0);
3095 loadValueDirectFixed(cUnit, rlChar, r1);
Elliott Hughes2bdbcb62010-04-12 14:29:37 -07003096 RegLocation rlStart = dvmCompilerGetSrc(cUnit, mir, 2);
3097 loadValueDirectFixed(cUnit, rlStart, r2);
Bill Buzbeefd023aa2009-11-02 09:23:49 -08003098 /* Test objects for NULL */
3099 genNullCheck(cUnit, rlThis.sRegLow, r0, mir->offset, NULL);
3100 genDispatchToHandler(cUnit, TEMPLATE_STRING_INDEXOF);
Bill Buzbeec6f10662010-02-09 11:16:15 -08003101 storeValue(cUnit, inlinedTarget(cUnit, mir, false),
3102 dvmCompilerGetReturn(cUnit));
Bill Buzbeefd023aa2009-11-02 09:23:49 -08003103 return true;
3104#endif
3105}
3106
Elliott Hughesee34f592010-04-05 18:13:52 -07003107// Generates an inlined String.isEmpty or String.length.
3108static bool genInlinedStringIsEmptyOrLength(CompilationUnit *cUnit, MIR *mir,
3109 bool isEmpty)
Bill Buzbee1f748632010-03-02 16:14:41 -08003110{
Elliott Hughesee34f592010-04-05 18:13:52 -07003111 // dst = src.length();
Bill Buzbee1f748632010-03-02 16:14:41 -08003112 RegLocation rlObj = dvmCompilerGetSrc(cUnit, mir, 0);
3113 RegLocation rlDest = inlinedTarget(cUnit, mir, false);
3114 rlObj = loadValue(cUnit, rlObj, kCoreReg);
3115 RegLocation rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
3116 genNullCheck(cUnit, rlObj.sRegLow, rlObj.lowReg, mir->offset, NULL);
3117 loadWordDisp(cUnit, rlObj.lowReg, gDvm.offJavaLangString_count,
3118 rlResult.lowReg);
Elliott Hughesee34f592010-04-05 18:13:52 -07003119 if (isEmpty) {
3120 // dst = (dst == 0);
3121 int tReg = dvmCompilerAllocTemp(cUnit);
3122 opRegReg(cUnit, kOpNeg, tReg, rlResult.lowReg);
3123 opRegRegReg(cUnit, kOpAdc, rlResult.lowReg, rlResult.lowReg, tReg);
3124 }
Bill Buzbee1f748632010-03-02 16:14:41 -08003125 storeValue(cUnit, rlDest, rlResult);
3126 return false;
3127}
3128
Elliott Hughesee34f592010-04-05 18:13:52 -07003129static bool genInlinedStringLength(CompilationUnit *cUnit, MIR *mir)
3130{
3131 return genInlinedStringIsEmptyOrLength(cUnit, mir, false);
3132}
3133
3134static bool genInlinedStringIsEmpty(CompilationUnit *cUnit, MIR *mir)
3135{
3136 return genInlinedStringIsEmptyOrLength(cUnit, mir, true);
3137}
3138
Bill Buzbee1f748632010-03-02 16:14:41 -08003139static bool genInlinedStringCharAt(CompilationUnit *cUnit, MIR *mir)
3140{
3141 int contents = offsetof(ArrayObject, contents);
3142 RegLocation rlObj = dvmCompilerGetSrc(cUnit, mir, 0);
3143 RegLocation rlIdx = dvmCompilerGetSrc(cUnit, mir, 1);
3144 RegLocation rlDest = inlinedTarget(cUnit, mir, false);
3145 RegLocation rlResult;
3146 rlObj = loadValue(cUnit, rlObj, kCoreReg);
3147 rlIdx = loadValue(cUnit, rlIdx, kCoreReg);
3148 int regMax = dvmCompilerAllocTemp(cUnit);
3149 int regOff = dvmCompilerAllocTemp(cUnit);
3150 int regPtr = dvmCompilerAllocTemp(cUnit);
3151 ArmLIR *pcrLabel = genNullCheck(cUnit, rlObj.sRegLow, rlObj.lowReg,
3152 mir->offset, NULL);
3153 loadWordDisp(cUnit, rlObj.lowReg, gDvm.offJavaLangString_count, regMax);
3154 loadWordDisp(cUnit, rlObj.lowReg, gDvm.offJavaLangString_offset, regOff);
3155 loadWordDisp(cUnit, rlObj.lowReg, gDvm.offJavaLangString_value, regPtr);
3156 genBoundsCheck(cUnit, rlIdx.lowReg, regMax, mir->offset, pcrLabel);
3157 dvmCompilerFreeTemp(cUnit, regMax);
3158 opRegImm(cUnit, kOpAdd, regPtr, contents);
3159 opRegReg(cUnit, kOpAdd, regOff, rlIdx.lowReg);
3160 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
3161 loadBaseIndexed(cUnit, regPtr, regOff, rlResult.lowReg, 1, kUnsignedHalf);
3162 storeValue(cUnit, rlDest, rlResult);
3163 return false;
3164}
3165
3166static bool genInlinedAbsInt(CompilationUnit *cUnit, MIR *mir)
3167{
3168 RegLocation rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
3169 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
3170 RegLocation rlDest = inlinedTarget(cUnit, mir, false);;
3171 RegLocation rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
3172 int signReg = dvmCompilerAllocTemp(cUnit);
3173 /*
3174 * abs(x) = y<=x>>31, (x+y)^y.
3175 * Thumb2's IT block also yields 3 instructions, but imposes
3176 * scheduling constraints.
3177 */
3178 opRegRegImm(cUnit, kOpAsr, signReg, rlSrc.lowReg, 31);
3179 opRegRegReg(cUnit, kOpAdd, rlResult.lowReg, rlSrc.lowReg, signReg);
3180 opRegReg(cUnit, kOpXor, rlResult.lowReg, signReg);
3181 storeValue(cUnit, rlDest, rlResult);
3182 return false;
3183}
3184
3185static bool genInlinedAbsLong(CompilationUnit *cUnit, MIR *mir)
3186{
3187 RegLocation rlSrc = dvmCompilerGetSrcWide(cUnit, mir, 0, 1);
3188 RegLocation rlDest = inlinedTargetWide(cUnit, mir, false);
3189 rlSrc = loadValueWide(cUnit, rlSrc, kCoreReg);
3190 RegLocation rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
3191 int signReg = dvmCompilerAllocTemp(cUnit);
3192 /*
3193 * abs(x) = y<=x>>31, (x+y)^y.
3194 * Thumb2 IT block allows slightly shorter sequence,
3195 * but introduces a scheduling barrier. Stick with this
3196 * mechanism for now.
3197 */
3198 opRegRegImm(cUnit, kOpAsr, signReg, rlSrc.highReg, 31);
3199 opRegRegReg(cUnit, kOpAdd, rlResult.lowReg, rlSrc.lowReg, signReg);
3200 opRegRegReg(cUnit, kOpAdc, rlResult.highReg, rlSrc.highReg, signReg);
3201 opRegReg(cUnit, kOpXor, rlResult.lowReg, signReg);
3202 opRegReg(cUnit, kOpXor, rlResult.highReg, signReg);
3203 storeValueWide(cUnit, rlDest, rlResult);
3204 return false;
3205}
Bill Buzbeefd023aa2009-11-02 09:23:49 -08003206
3207/*
Bill Buzbeece46c942009-11-20 15:41:34 -08003208 * NOTE: Handles both range and non-range versions (arguments
3209 * have already been normalized by this point).
Ben Chengba4fc8b2009-06-01 13:00:29 -07003210 */
Bill Buzbeece46c942009-11-20 15:41:34 -08003211static bool handleExecuteInline(CompilationUnit *cUnit, MIR *mir)
Ben Chengba4fc8b2009-06-01 13:00:29 -07003212{
3213 DecodedInstruction *dInsn = &mir->dalvikInsn;
3214 switch( mir->dalvikInsn.opCode) {
Bill Buzbeece46c942009-11-20 15:41:34 -08003215 case OP_EXECUTE_INLINE_RANGE:
Ben Chengba4fc8b2009-06-01 13:00:29 -07003216 case OP_EXECUTE_INLINE: {
3217 unsigned int i;
3218 const InlineOperation* inLineTable = dvmGetInlineOpsTable();
Bill Buzbee50a6bf22009-07-08 13:08:04 -07003219 int offset = offsetof(InterpState, retval);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003220 int operation = dInsn->vB;
Bill Buzbee1465db52009-09-23 17:17:35 -07003221 int tReg1;
3222 int tReg2;
Bill Buzbee50a6bf22009-07-08 13:08:04 -07003223 switch (operation) {
3224 case INLINE_EMPTYINLINEMETHOD:
3225 return false; /* Nop */
3226 case INLINE_STRING_LENGTH:
3227 return genInlinedStringLength(cUnit, mir);
Elliott Hughesee34f592010-04-05 18:13:52 -07003228 case INLINE_STRING_IS_EMPTY:
3229 return genInlinedStringIsEmpty(cUnit, mir);
Bill Buzbee50a6bf22009-07-08 13:08:04 -07003230 case INLINE_MATH_ABS_INT:
3231 return genInlinedAbsInt(cUnit, mir);
3232 case INLINE_MATH_ABS_LONG:
3233 return genInlinedAbsLong(cUnit, mir);
3234 case INLINE_MATH_MIN_INT:
3235 return genInlinedMinMaxInt(cUnit, mir, true);
3236 case INLINE_MATH_MAX_INT:
3237 return genInlinedMinMaxInt(cUnit, mir, false);
3238 case INLINE_STRING_CHARAT:
3239 return genInlinedStringCharAt(cUnit, mir);
3240 case INLINE_MATH_SQRT:
3241 if (genInlineSqrt(cUnit, mir))
Bill Buzbee9727c3d2009-08-01 11:32:36 -07003242 return false;
Bill Buzbee50a6bf22009-07-08 13:08:04 -07003243 else
3244 break; /* Handle with C routine */
Bill Buzbee50a6bf22009-07-08 13:08:04 -07003245 case INLINE_MATH_ABS_FLOAT:
Bill Buzbee1465db52009-09-23 17:17:35 -07003246 if (genInlinedAbsFloat(cUnit, mir))
3247 return false;
3248 else
3249 break;
Bill Buzbee50a6bf22009-07-08 13:08:04 -07003250 case INLINE_MATH_ABS_DOUBLE:
Bill Buzbee1465db52009-09-23 17:17:35 -07003251 if (genInlinedAbsDouble(cUnit, mir))
3252 return false;
3253 else
3254 break;
Bill Buzbee50a6bf22009-07-08 13:08:04 -07003255 case INLINE_STRING_COMPARETO:
Bill Buzbeefd023aa2009-11-02 09:23:49 -08003256 if (genInlinedCompareTo(cUnit, mir))
3257 return false;
3258 else
3259 break;
Elliott Hughes2bdbcb62010-04-12 14:29:37 -07003260 case INLINE_STRING_FASTINDEXOF_II:
3261 if (genInlinedFastIndexOf(cUnit, mir))
Bill Buzbeefd023aa2009-11-02 09:23:49 -08003262 return false;
3263 else
3264 break;
3265 case INLINE_STRING_EQUALS:
3266 case INLINE_MATH_COS:
3267 case INLINE_MATH_SIN:
3268 break; /* Handle with C routine */
Bill Buzbee50a6bf22009-07-08 13:08:04 -07003269 default:
Bill Buzbeefc519dc2010-03-06 23:30:57 -08003270 dvmCompilerAbort(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003271 }
Bill Buzbeec6f10662010-02-09 11:16:15 -08003272 dvmCompilerFlushAllRegs(cUnit); /* Everything to home location */
Elliott Hughes6a555132010-02-25 15:41:42 -08003273 dvmCompilerClobberCallRegs(cUnit);
Bill Buzbeec6f10662010-02-09 11:16:15 -08003274 dvmCompilerClobber(cUnit, r4PC);
3275 dvmCompilerClobber(cUnit, r7);
Bill Buzbee1465db52009-09-23 17:17:35 -07003276 opRegRegImm(cUnit, kOpAdd, r4PC, rGLUE, offset);
3277 opImm(cUnit, kOpPush, (1<<r4PC) | (1<<r7));
Ben Chengbd1326d2010-04-02 15:04:53 -07003278 LOAD_FUNC_ADDR(cUnit, r4PC, (int)inLineTable[operation].func);
Bill Buzbee1465db52009-09-23 17:17:35 -07003279 genExportPC(cUnit, mir);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003280 for (i=0; i < dInsn->vA; i++) {
Bill Buzbeec6f10662010-02-09 11:16:15 -08003281 loadValueDirect(cUnit, dvmCompilerGetSrc(cUnit, mir, i), i);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003282 }
Bill Buzbee1465db52009-09-23 17:17:35 -07003283 opReg(cUnit, kOpBlx, r4PC);
3284 opRegImm(cUnit, kOpAdd, r13, 8);
Bill Buzbeece46c942009-11-20 15:41:34 -08003285 opRegImm(cUnit, kOpCmp, r0, 0); /* NULL? */
3286 ArmLIR *branchOver = opCondBranch(cUnit, kArmCondNe);
3287 loadConstant(cUnit, r0,
3288 (int) (cUnit->method->insns + mir->offset));
3289 genDispatchToHandler(cUnit, TEMPLATE_THROW_EXCEPTION_COMMON);
3290 ArmLIR *target = newLIR0(cUnit, kArmPseudoTargetLabel);
3291 target->defMask = ENCODE_ALL;
3292 branchOver->generic.target = (LIR *) target;
Ben Chengba4fc8b2009-06-01 13:00:29 -07003293 break;
3294 }
3295 default:
3296 return true;
3297 }
3298 return false;
3299}
3300
3301static bool handleFmt51l(CompilationUnit *cUnit, MIR *mir)
3302{
Bill Buzbee1465db52009-09-23 17:17:35 -07003303 //TUNING: We're using core regs here - not optimal when target is a double
Bill Buzbeec6f10662010-02-09 11:16:15 -08003304 RegLocation rlDest = dvmCompilerGetDestWide(cUnit, mir, 0, 1);
3305 RegLocation rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Ben Chengbd1326d2010-04-02 15:04:53 -07003306 loadConstantNoClobber(cUnit, rlResult.lowReg,
3307 mir->dalvikInsn.vB_wide & 0xFFFFFFFFUL);
3308 loadConstantNoClobber(cUnit, rlResult.highReg,
3309 (mir->dalvikInsn.vB_wide>>32) & 0xFFFFFFFFUL);
Bill Buzbee1465db52009-09-23 17:17:35 -07003310 storeValueWide(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003311 return false;
3312}
3313
Ben Chengba4fc8b2009-06-01 13:00:29 -07003314/*
3315 * The following are special processing routines that handle transfer of
3316 * controls between compiled code and the interpreter. Certain VM states like
3317 * Dalvik PC and special-purpose registers are reconstructed here.
3318 */
3319
Ben Cheng1efc9c52009-06-08 18:25:27 -07003320/* Chaining cell for code that may need warmup. */
3321static void handleNormalChainingCell(CompilationUnit *cUnit,
3322 unsigned int offset)
Ben Chengba4fc8b2009-06-01 13:00:29 -07003323{
Ben Cheng11d8f142010-03-24 15:24:19 -07003324 /*
3325 * Use raw instruction constructors to guarantee that the generated
3326 * instructions fit the predefined cell size.
3327 */
3328 newLIR3(cUnit, kThumbLdrRRI5, r0, rGLUE,
3329 offsetof(InterpState,
3330 jitToInterpEntries.dvmJitToInterpNormal) >> 2);
3331 newLIR1(cUnit, kThumbBlxR, r0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003332 addWordData(cUnit, (int) (cUnit->method->insns + offset), true);
3333}
3334
3335/*
Ben Cheng1efc9c52009-06-08 18:25:27 -07003336 * Chaining cell for instructions that immediately following already translated
3337 * code.
Ben Chengba4fc8b2009-06-01 13:00:29 -07003338 */
Ben Cheng1efc9c52009-06-08 18:25:27 -07003339static void handleHotChainingCell(CompilationUnit *cUnit,
3340 unsigned int offset)
Ben Chengba4fc8b2009-06-01 13:00:29 -07003341{
Ben Cheng11d8f142010-03-24 15:24:19 -07003342 /*
3343 * Use raw instruction constructors to guarantee that the generated
3344 * instructions fit the predefined cell size.
3345 */
3346 newLIR3(cUnit, kThumbLdrRRI5, r0, rGLUE,
3347 offsetof(InterpState,
3348 jitToInterpEntries.dvmJitToInterpTraceSelect) >> 2);
3349 newLIR1(cUnit, kThumbBlxR, r0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003350 addWordData(cUnit, (int) (cUnit->method->insns + offset), true);
3351}
3352
Bill Buzbee9c4b7c82009-09-10 10:10:38 -07003353#if defined(WITH_SELF_VERIFICATION) || defined(WITH_JIT_TUNING)
Jeff Hao97319a82009-08-12 16:57:15 -07003354/* Chaining cell for branches that branch back into the same basic block */
3355static void handleBackwardBranchChainingCell(CompilationUnit *cUnit,
3356 unsigned int offset)
3357{
Ben Cheng11d8f142010-03-24 15:24:19 -07003358 /*
3359 * Use raw instruction constructors to guarantee that the generated
3360 * instructions fit the predefined cell size.
3361 */
Bill Buzbee9c4b7c82009-09-10 10:10:38 -07003362#if defined(WITH_SELF_VERIFICATION)
Bill Buzbee1465db52009-09-23 17:17:35 -07003363 newLIR3(cUnit, kThumbLdrRRI5, r0, rGLUE,
Ben Cheng40094c12010-02-24 20:58:44 -08003364 offsetof(InterpState,
3365 jitToInterpEntries.dvmJitToInterpBackwardBranch) >> 2);
Bill Buzbee9c4b7c82009-09-10 10:10:38 -07003366#else
Bill Buzbee1465db52009-09-23 17:17:35 -07003367 newLIR3(cUnit, kThumbLdrRRI5, r0, rGLUE,
Bill Buzbee9c4b7c82009-09-10 10:10:38 -07003368 offsetof(InterpState, jitToInterpEntries.dvmJitToInterpNormal) >> 2);
3369#endif
Bill Buzbee1465db52009-09-23 17:17:35 -07003370 newLIR1(cUnit, kThumbBlxR, r0);
Jeff Hao97319a82009-08-12 16:57:15 -07003371 addWordData(cUnit, (int) (cUnit->method->insns + offset), true);
3372}
3373
3374#endif
Ben Chengba4fc8b2009-06-01 13:00:29 -07003375/* Chaining cell for monomorphic method invocations. */
Ben Cheng38329f52009-07-07 14:19:20 -07003376static void handleInvokeSingletonChainingCell(CompilationUnit *cUnit,
3377 const Method *callee)
Ben Chengba4fc8b2009-06-01 13:00:29 -07003378{
Ben Cheng11d8f142010-03-24 15:24:19 -07003379 /*
3380 * Use raw instruction constructors to guarantee that the generated
3381 * instructions fit the predefined cell size.
3382 */
3383 newLIR3(cUnit, kThumbLdrRRI5, r0, rGLUE,
3384 offsetof(InterpState,
3385 jitToInterpEntries.dvmJitToInterpTraceSelect) >> 2);
3386 newLIR1(cUnit, kThumbBlxR, r0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003387 addWordData(cUnit, (int) (callee->insns), true);
3388}
3389
Ben Cheng38329f52009-07-07 14:19:20 -07003390/* Chaining cell for monomorphic method invocations. */
3391static void handleInvokePredictedChainingCell(CompilationUnit *cUnit)
3392{
3393
3394 /* Should not be executed in the initial state */
3395 addWordData(cUnit, PREDICTED_CHAIN_BX_PAIR_INIT, true);
3396 /* To be filled: class */
3397 addWordData(cUnit, PREDICTED_CHAIN_CLAZZ_INIT, true);
3398 /* To be filled: method */
3399 addWordData(cUnit, PREDICTED_CHAIN_METHOD_INIT, true);
3400 /*
3401 * Rechain count. The initial value of 0 here will trigger chaining upon
3402 * the first invocation of this callsite.
3403 */
3404 addWordData(cUnit, PREDICTED_CHAIN_COUNTER_INIT, true);
3405}
3406
Ben Chengba4fc8b2009-06-01 13:00:29 -07003407/* Load the Dalvik PC into r0 and jump to the specified target */
3408static void handlePCReconstruction(CompilationUnit *cUnit,
Bill Buzbee89efc3d2009-07-28 11:22:22 -07003409 ArmLIR *targetLabel)
Ben Chengba4fc8b2009-06-01 13:00:29 -07003410{
Bill Buzbee89efc3d2009-07-28 11:22:22 -07003411 ArmLIR **pcrLabel =
3412 (ArmLIR **) cUnit->pcReconstructionList.elemList;
Ben Chengba4fc8b2009-06-01 13:00:29 -07003413 int numElems = cUnit->pcReconstructionList.numUsed;
3414 int i;
3415 for (i = 0; i < numElems; i++) {
3416 dvmCompilerAppendLIR(cUnit, (LIR *) pcrLabel[i]);
3417 /* r0 = dalvik PC */
3418 loadConstant(cUnit, r0, pcrLabel[i]->operands[0]);
3419 genUnconditionalBranch(cUnit, targetLabel);
3420 }
3421}
3422
Bill Buzbee1465db52009-09-23 17:17:35 -07003423static char *extendedMIROpNames[kMirOpLast - kMirOpFirst] = {
3424 "kMirOpPhi",
3425 "kMirOpNullNRangeUpCheck",
3426 "kMirOpNullNRangeDownCheck",
3427 "kMirOpLowerBound",
3428 "kMirOpPunt",
Ben Cheng4238ec22009-08-24 16:32:22 -07003429};
3430
3431/*
3432 * vA = arrayReg;
3433 * vB = idxReg;
3434 * vC = endConditionReg;
3435 * arg[0] = maxC
3436 * arg[1] = minC
3437 * arg[2] = loopBranchConditionCode
3438 */
3439static void genHoistedChecksForCountUpLoop(CompilationUnit *cUnit, MIR *mir)
3440{
Bill Buzbee1465db52009-09-23 17:17:35 -07003441 /*
3442 * NOTE: these synthesized blocks don't have ssa names assigned
3443 * for Dalvik registers. However, because they dominate the following
3444 * blocks we can simply use the Dalvik name w/ subscript 0 as the
3445 * ssa name.
3446 */
Ben Cheng4238ec22009-08-24 16:32:22 -07003447 DecodedInstruction *dInsn = &mir->dalvikInsn;
3448 const int lenOffset = offsetof(ArrayObject, length);
Ben Cheng4238ec22009-08-24 16:32:22 -07003449 const int maxC = dInsn->arg[0];
3450 const int minC = dInsn->arg[1];
Bill Buzbee1465db52009-09-23 17:17:35 -07003451 int regLength;
3452 RegLocation rlArray = cUnit->regLocation[mir->dalvikInsn.vA];
3453 RegLocation rlIdxEnd = cUnit->regLocation[mir->dalvikInsn.vC];
Ben Cheng4238ec22009-08-24 16:32:22 -07003454
3455 /* regArray <- arrayRef */
Bill Buzbee1465db52009-09-23 17:17:35 -07003456 rlArray = loadValue(cUnit, rlArray, kCoreReg);
3457 rlIdxEnd = loadValue(cUnit, rlIdxEnd, kCoreReg);
3458 genRegImmCheck(cUnit, kArmCondEq, rlArray.lowReg, 0, 0,
Ben Cheng4238ec22009-08-24 16:32:22 -07003459 (ArmLIR *) cUnit->loopAnalysis->branchToPCR);
3460
3461 /* regLength <- len(arrayRef) */
Bill Buzbeec6f10662010-02-09 11:16:15 -08003462 regLength = dvmCompilerAllocTemp(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07003463 loadWordDisp(cUnit, rlArray.lowReg, lenOffset, regLength);
Ben Cheng4238ec22009-08-24 16:32:22 -07003464
3465 int delta = maxC;
3466 /*
3467 * If the loop end condition is ">=" instead of ">", then the largest value
3468 * of the index is "endCondition - 1".
3469 */
3470 if (dInsn->arg[2] == OP_IF_GE) {
3471 delta--;
3472 }
3473
3474 if (delta) {
Bill Buzbeec6f10662010-02-09 11:16:15 -08003475 int tReg = dvmCompilerAllocTemp(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07003476 opRegRegImm(cUnit, kOpAdd, tReg, rlIdxEnd.lowReg, delta);
3477 rlIdxEnd.lowReg = tReg;
Bill Buzbeec6f10662010-02-09 11:16:15 -08003478 dvmCompilerFreeTemp(cUnit, tReg);
Ben Cheng4238ec22009-08-24 16:32:22 -07003479 }
3480 /* Punt if "regIdxEnd < len(Array)" is false */
Bill Buzbee1465db52009-09-23 17:17:35 -07003481 genRegRegCheck(cUnit, kArmCondGe, rlIdxEnd.lowReg, regLength, 0,
Ben Cheng0fd31e42009-09-03 14:40:16 -07003482 (ArmLIR *) cUnit->loopAnalysis->branchToPCR);
Ben Cheng4238ec22009-08-24 16:32:22 -07003483}
3484
3485/*
3486 * vA = arrayReg;
3487 * vB = idxReg;
3488 * vC = endConditionReg;
3489 * arg[0] = maxC
3490 * arg[1] = minC
3491 * arg[2] = loopBranchConditionCode
3492 */
3493static void genHoistedChecksForCountDownLoop(CompilationUnit *cUnit, MIR *mir)
3494{
3495 DecodedInstruction *dInsn = &mir->dalvikInsn;
3496 const int lenOffset = offsetof(ArrayObject, length);
Bill Buzbeec6f10662010-02-09 11:16:15 -08003497 const int regLength = dvmCompilerAllocTemp(cUnit);
Ben Cheng4238ec22009-08-24 16:32:22 -07003498 const int maxC = dInsn->arg[0];
3499 const int minC = dInsn->arg[1];
Bill Buzbee1465db52009-09-23 17:17:35 -07003500 RegLocation rlArray = cUnit->regLocation[mir->dalvikInsn.vA];
3501 RegLocation rlIdxInit = cUnit->regLocation[mir->dalvikInsn.vB];
Ben Cheng4238ec22009-08-24 16:32:22 -07003502
3503 /* regArray <- arrayRef */
Bill Buzbee1465db52009-09-23 17:17:35 -07003504 rlArray = loadValue(cUnit, rlArray, kCoreReg);
3505 rlIdxInit = loadValue(cUnit, rlIdxInit, kCoreReg);
3506 genRegImmCheck(cUnit, kArmCondEq, rlArray.lowReg, 0, 0,
Ben Cheng4238ec22009-08-24 16:32:22 -07003507 (ArmLIR *) cUnit->loopAnalysis->branchToPCR);
3508
3509 /* regLength <- len(arrayRef) */
Bill Buzbee1465db52009-09-23 17:17:35 -07003510 loadWordDisp(cUnit, rlArray.lowReg, lenOffset, regLength);
Ben Cheng4238ec22009-08-24 16:32:22 -07003511
3512 if (maxC) {
Bill Buzbeec6f10662010-02-09 11:16:15 -08003513 int tReg = dvmCompilerAllocTemp(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07003514 opRegRegImm(cUnit, kOpAdd, tReg, rlIdxInit.lowReg, maxC);
3515 rlIdxInit.lowReg = tReg;
Bill Buzbeec6f10662010-02-09 11:16:15 -08003516 dvmCompilerFreeTemp(cUnit, tReg);
Ben Cheng4238ec22009-08-24 16:32:22 -07003517 }
3518
3519 /* Punt if "regIdxInit < len(Array)" is false */
Bill Buzbee1465db52009-09-23 17:17:35 -07003520 genRegRegCheck(cUnit, kArmCondGe, rlIdxInit.lowReg, regLength, 0,
Ben Cheng0fd31e42009-09-03 14:40:16 -07003521 (ArmLIR *) cUnit->loopAnalysis->branchToPCR);
Ben Cheng4238ec22009-08-24 16:32:22 -07003522}
3523
3524/*
3525 * vA = idxReg;
3526 * vB = minC;
3527 */
3528static void genHoistedLowerBoundCheck(CompilationUnit *cUnit, MIR *mir)
3529{
3530 DecodedInstruction *dInsn = &mir->dalvikInsn;
Ben Cheng4238ec22009-08-24 16:32:22 -07003531 const int minC = dInsn->vB;
Bill Buzbee1465db52009-09-23 17:17:35 -07003532 RegLocation rlIdx = cUnit->regLocation[mir->dalvikInsn.vA];
Ben Cheng4238ec22009-08-24 16:32:22 -07003533
3534 /* regIdx <- initial index value */
Bill Buzbee1465db52009-09-23 17:17:35 -07003535 rlIdx = loadValue(cUnit, rlIdx, kCoreReg);
Ben Cheng4238ec22009-08-24 16:32:22 -07003536
3537 /* Punt if "regIdxInit + minC >= 0" is false */
Bill Buzbee1465db52009-09-23 17:17:35 -07003538 genRegImmCheck(cUnit, kArmCondLt, rlIdx.lowReg, -minC, 0,
Ben Cheng4238ec22009-08-24 16:32:22 -07003539 (ArmLIR *) cUnit->loopAnalysis->branchToPCR);
3540}
3541
3542/* Extended MIR instructions like PHI */
3543static void handleExtendedMIR(CompilationUnit *cUnit, MIR *mir)
3544{
Bill Buzbee1465db52009-09-23 17:17:35 -07003545 int opOffset = mir->dalvikInsn.opCode - kMirOpFirst;
Ben Cheng4238ec22009-08-24 16:32:22 -07003546 char *msg = dvmCompilerNew(strlen(extendedMIROpNames[opOffset]) + 1,
3547 false);
3548 strcpy(msg, extendedMIROpNames[opOffset]);
Bill Buzbee1465db52009-09-23 17:17:35 -07003549 newLIR1(cUnit, kArmPseudoExtended, (int) msg);
Ben Cheng4238ec22009-08-24 16:32:22 -07003550
3551 switch (mir->dalvikInsn.opCode) {
Bill Buzbee1465db52009-09-23 17:17:35 -07003552 case kMirOpPhi: {
Ben Cheng4238ec22009-08-24 16:32:22 -07003553 char *ssaString = dvmCompilerGetSSAString(cUnit, mir->ssaRep);
Bill Buzbee1465db52009-09-23 17:17:35 -07003554 newLIR1(cUnit, kArmPseudoSSARep, (int) ssaString);
Ben Cheng4238ec22009-08-24 16:32:22 -07003555 break;
3556 }
Bill Buzbee1465db52009-09-23 17:17:35 -07003557 case kMirOpNullNRangeUpCheck: {
Ben Cheng4238ec22009-08-24 16:32:22 -07003558 genHoistedChecksForCountUpLoop(cUnit, mir);
3559 break;
3560 }
Bill Buzbee1465db52009-09-23 17:17:35 -07003561 case kMirOpNullNRangeDownCheck: {
Ben Cheng4238ec22009-08-24 16:32:22 -07003562 genHoistedChecksForCountDownLoop(cUnit, mir);
3563 break;
3564 }
Bill Buzbee1465db52009-09-23 17:17:35 -07003565 case kMirOpLowerBound: {
Ben Cheng4238ec22009-08-24 16:32:22 -07003566 genHoistedLowerBoundCheck(cUnit, mir);
3567 break;
3568 }
Bill Buzbee1465db52009-09-23 17:17:35 -07003569 case kMirOpPunt: {
Ben Cheng4238ec22009-08-24 16:32:22 -07003570 genUnconditionalBranch(cUnit,
3571 (ArmLIR *) cUnit->loopAnalysis->branchToPCR);
3572 break;
3573 }
3574 default:
3575 break;
3576 }
3577}
3578
3579/*
3580 * Create a PC-reconstruction cell for the starting offset of this trace.
3581 * Since the PCR cell is placed near the end of the compiled code which is
3582 * usually out of range for a conditional branch, we put two branches (one
3583 * branch over to the loop body and one layover branch to the actual PCR) at the
3584 * end of the entry block.
3585 */
3586static void setupLoopEntryBlock(CompilationUnit *cUnit, BasicBlock *entry,
3587 ArmLIR *bodyLabel)
3588{
3589 /* Set up the place holder to reconstruct this Dalvik PC */
3590 ArmLIR *pcrLabel = dvmCompilerNew(sizeof(ArmLIR), true);
Ben Chenga4973592010-03-31 11:59:18 -07003591 pcrLabel->opCode = kArmPseudoPCReconstructionCell;
Ben Cheng4238ec22009-08-24 16:32:22 -07003592 pcrLabel->operands[0] =
3593 (int) (cUnit->method->insns + entry->startOffset);
3594 pcrLabel->operands[1] = entry->startOffset;
3595 /* Insert the place holder to the growable list */
3596 dvmInsertGrowableList(&cUnit->pcReconstructionList, pcrLabel);
3597
3598 /*
3599 * Next, create two branches - one branch over to the loop body and the
3600 * other branch to the PCR cell to punt.
3601 */
3602 ArmLIR *branchToBody = dvmCompilerNew(sizeof(ArmLIR), true);
Bill Buzbee1465db52009-09-23 17:17:35 -07003603 branchToBody->opCode = kThumbBUncond;
Ben Cheng4238ec22009-08-24 16:32:22 -07003604 branchToBody->generic.target = (LIR *) bodyLabel;
Ben Chengdcf3e5d2009-09-11 13:42:05 -07003605 setupResourceMasks(branchToBody);
Ben Cheng4238ec22009-08-24 16:32:22 -07003606 cUnit->loopAnalysis->branchToBody = (LIR *) branchToBody;
3607
3608 ArmLIR *branchToPCR = dvmCompilerNew(sizeof(ArmLIR), true);
Bill Buzbee1465db52009-09-23 17:17:35 -07003609 branchToPCR->opCode = kThumbBUncond;
Ben Cheng4238ec22009-08-24 16:32:22 -07003610 branchToPCR->generic.target = (LIR *) pcrLabel;
Ben Chengdcf3e5d2009-09-11 13:42:05 -07003611 setupResourceMasks(branchToPCR);
Ben Cheng4238ec22009-08-24 16:32:22 -07003612 cUnit->loopAnalysis->branchToPCR = (LIR *) branchToPCR;
3613}
3614
Ben Chengd5adae12010-03-26 17:45:28 -07003615#if defined(WITH_SELF_VERIFICATION)
3616static bool selfVerificationPuntOps(MIR *mir)
3617{
3618 DecodedInstruction *decInsn = &mir->dalvikInsn;
3619 OpCode op = decInsn->opCode;
3620 int flags = dexGetInstrFlags(gDvm.instrFlags, op);
3621 /*
3622 * All opcodes that can throw exceptions and use the
3623 * TEMPLATE_THROW_EXCEPTION_COMMON template should be excluded in the trace
3624 * under self-verification mode.
3625 */
3626 return (op == OP_MONITOR_ENTER || op == OP_MONITOR_EXIT ||
3627 op == OP_NEW_INSTANCE || op == OP_NEW_ARRAY ||
3628 op == OP_CHECK_CAST || op == OP_MOVE_EXCEPTION ||
3629 op == OP_FILL_ARRAY_DATA || op == OP_EXECUTE_INLINE ||
3630 op == OP_EXECUTE_INLINE_RANGE ||
3631 (flags & kInstrInvoke));
3632}
3633#endif
3634
Ben Chengba4fc8b2009-06-01 13:00:29 -07003635void dvmCompilerMIR2LIR(CompilationUnit *cUnit)
3636{
3637 /* Used to hold the labels of each block */
Bill Buzbee89efc3d2009-07-28 11:22:22 -07003638 ArmLIR *labelList =
3639 dvmCompilerNew(sizeof(ArmLIR) * cUnit->numBlocks, true);
Ben Chengcec26f62010-01-15 15:29:33 -08003640 GrowableList chainingListByType[kChainingCellGap];
Ben Chengba4fc8b2009-06-01 13:00:29 -07003641 int i;
3642
3643 /*
Ben Cheng38329f52009-07-07 14:19:20 -07003644 * Initialize various types chaining lists.
Ben Chengba4fc8b2009-06-01 13:00:29 -07003645 */
Ben Chengcec26f62010-01-15 15:29:33 -08003646 for (i = 0; i < kChainingCellGap; i++) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07003647 dvmInitGrowableList(&chainingListByType[i], 2);
3648 }
3649
3650 BasicBlock **blockList = cUnit->blockList;
3651
Bill Buzbee6e963e12009-06-17 16:56:19 -07003652 if (cUnit->executionCount) {
3653 /*
3654 * Reserve 6 bytes at the beginning of the trace
3655 * +----------------------------+
3656 * | execution count (4 bytes) |
3657 * +----------------------------+
3658 * | chain cell offset (2 bytes)|
3659 * +----------------------------+
3660 * ...and then code to increment the execution
3661 * count:
3662 * mov r0, pc @ move adr of "mov r0,pc" + 4 to r0
3663 * sub r0, #10 @ back up to addr of executionCount
3664 * ldr r1, [r0]
3665 * add r1, #1
3666 * str r1, [r0]
3667 */
Bill Buzbee1465db52009-09-23 17:17:35 -07003668 newLIR1(cUnit, kArm16BitData, 0);
3669 newLIR1(cUnit, kArm16BitData, 0);
Ben Chengcc6600c2009-06-22 14:45:16 -07003670 cUnit->chainCellOffsetLIR =
Bill Buzbee1465db52009-09-23 17:17:35 -07003671 (LIR *) newLIR1(cUnit, kArm16BitData, CHAIN_CELL_OFFSET_TAG);
Bill Buzbee6e963e12009-06-17 16:56:19 -07003672 cUnit->headerSize = 6;
Bill Buzbee270c1d62009-08-13 16:58:07 -07003673 /* Thumb instruction used directly here to ensure correct size */
Bill Buzbee1465db52009-09-23 17:17:35 -07003674 newLIR2(cUnit, kThumbMovRR_H2L, r0, rpc);
3675 newLIR2(cUnit, kThumbSubRI8, r0, 10);
3676 newLIR3(cUnit, kThumbLdrRRI5, r1, r0, 0);
3677 newLIR2(cUnit, kThumbAddRI8, r1, 1);
3678 newLIR3(cUnit, kThumbStrRRI5, r1, r0, 0);
Bill Buzbee6e963e12009-06-17 16:56:19 -07003679 } else {
3680 /* Just reserve 2 bytes for the chain cell offset */
Ben Chengcc6600c2009-06-22 14:45:16 -07003681 cUnit->chainCellOffsetLIR =
Bill Buzbee1465db52009-09-23 17:17:35 -07003682 (LIR *) newLIR1(cUnit, kArm16BitData, CHAIN_CELL_OFFSET_TAG);
Bill Buzbee6e963e12009-06-17 16:56:19 -07003683 cUnit->headerSize = 2;
3684 }
Ben Cheng1efc9c52009-06-08 18:25:27 -07003685
Ben Chengba4fc8b2009-06-01 13:00:29 -07003686 /* Handle the content in each basic block */
3687 for (i = 0; i < cUnit->numBlocks; i++) {
3688 blockList[i]->visited = true;
3689 MIR *mir;
3690
3691 labelList[i].operands[0] = blockList[i]->startOffset;
3692
Ben Chengcec26f62010-01-15 15:29:33 -08003693 if (blockList[i]->blockType >= kChainingCellGap) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07003694 /*
3695 * Append the label pseudo LIR first. Chaining cells will be handled
3696 * separately afterwards.
3697 */
3698 dvmCompilerAppendLIR(cUnit, (LIR *) &labelList[i]);
3699 }
3700
Bill Buzbee1465db52009-09-23 17:17:35 -07003701 if (blockList[i]->blockType == kEntryBlock) {
Ben Chenga4973592010-03-31 11:59:18 -07003702 labelList[i].opCode = kArmPseudoEntryBlock;
Ben Cheng4238ec22009-08-24 16:32:22 -07003703 if (blockList[i]->firstMIRInsn == NULL) {
3704 continue;
3705 } else {
3706 setupLoopEntryBlock(cUnit, blockList[i],
3707 &labelList[blockList[i]->fallThrough->id]);
3708 }
Bill Buzbee1465db52009-09-23 17:17:35 -07003709 } else if (blockList[i]->blockType == kExitBlock) {
Ben Chenga4973592010-03-31 11:59:18 -07003710 labelList[i].opCode = kArmPseudoExitBlock;
Ben Cheng4238ec22009-08-24 16:32:22 -07003711 goto gen_fallthrough;
Bill Buzbee1465db52009-09-23 17:17:35 -07003712 } else if (blockList[i]->blockType == kDalvikByteCode) {
3713 labelList[i].opCode = kArmPseudoNormalBlockLabel;
Ben Chenge9695e52009-06-16 16:11:47 -07003714 /* Reset the register state */
Bill Buzbeec6f10662010-02-09 11:16:15 -08003715 dvmCompilerResetRegPool(cUnit);
3716 dvmCompilerClobberAllRegs(cUnit);
3717 dvmCompilerResetNullCheck(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003718 } else {
3719 switch (blockList[i]->blockType) {
Bill Buzbee1465db52009-09-23 17:17:35 -07003720 case kChainingCellNormal:
Ben Chenga4973592010-03-31 11:59:18 -07003721 labelList[i].opCode = kArmPseudoChainingCellNormal;
Ben Chengba4fc8b2009-06-01 13:00:29 -07003722 /* handle the codegen later */
3723 dvmInsertGrowableList(
Bill Buzbee1465db52009-09-23 17:17:35 -07003724 &chainingListByType[kChainingCellNormal], (void *) i);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003725 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07003726 case kChainingCellInvokeSingleton:
Ben Cheng38329f52009-07-07 14:19:20 -07003727 labelList[i].opCode =
Ben Chenga4973592010-03-31 11:59:18 -07003728 kArmPseudoChainingCellInvokeSingleton;
Ben Chengba4fc8b2009-06-01 13:00:29 -07003729 labelList[i].operands[0] =
3730 (int) blockList[i]->containingMethod;
3731 /* handle the codegen later */
3732 dvmInsertGrowableList(
Bill Buzbee1465db52009-09-23 17:17:35 -07003733 &chainingListByType[kChainingCellInvokeSingleton],
Ben Cheng38329f52009-07-07 14:19:20 -07003734 (void *) i);
3735 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07003736 case kChainingCellInvokePredicted:
Ben Cheng38329f52009-07-07 14:19:20 -07003737 labelList[i].opCode =
Ben Chenga4973592010-03-31 11:59:18 -07003738 kArmPseudoChainingCellInvokePredicted;
Ben Cheng38329f52009-07-07 14:19:20 -07003739 /* handle the codegen later */
3740 dvmInsertGrowableList(
Bill Buzbee1465db52009-09-23 17:17:35 -07003741 &chainingListByType[kChainingCellInvokePredicted],
Ben Cheng38329f52009-07-07 14:19:20 -07003742 (void *) i);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003743 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07003744 case kChainingCellHot:
Ben Chengba4fc8b2009-06-01 13:00:29 -07003745 labelList[i].opCode =
Ben Chenga4973592010-03-31 11:59:18 -07003746 kArmPseudoChainingCellHot;
Ben Chengba4fc8b2009-06-01 13:00:29 -07003747 /* handle the codegen later */
3748 dvmInsertGrowableList(
Bill Buzbee1465db52009-09-23 17:17:35 -07003749 &chainingListByType[kChainingCellHot],
Ben Chengba4fc8b2009-06-01 13:00:29 -07003750 (void *) i);
3751 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07003752 case kPCReconstruction:
Ben Chengba4fc8b2009-06-01 13:00:29 -07003753 /* Make sure exception handling block is next */
3754 labelList[i].opCode =
Ben Chenga4973592010-03-31 11:59:18 -07003755 kArmPseudoPCReconstructionBlockLabel;
Ben Chengba4fc8b2009-06-01 13:00:29 -07003756 assert (i == cUnit->numBlocks - 2);
3757 handlePCReconstruction(cUnit, &labelList[i+1]);
3758 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07003759 case kExceptionHandling:
3760 labelList[i].opCode = kArmPseudoEHBlockLabel;
Ben Chengba4fc8b2009-06-01 13:00:29 -07003761 if (cUnit->pcReconstructionList.numUsed) {
Bill Buzbee270c1d62009-08-13 16:58:07 -07003762 loadWordDisp(cUnit, rGLUE, offsetof(InterpState,
3763 jitToInterpEntries.dvmJitToInterpPunt),
3764 r1);
Bill Buzbee1465db52009-09-23 17:17:35 -07003765 opReg(cUnit, kOpBlx, r1);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003766 }
3767 break;
Bill Buzbee9c4b7c82009-09-10 10:10:38 -07003768#if defined(WITH_SELF_VERIFICATION) || defined(WITH_JIT_TUNING)
Bill Buzbee1465db52009-09-23 17:17:35 -07003769 case kChainingCellBackwardBranch:
Jeff Hao97319a82009-08-12 16:57:15 -07003770 labelList[i].opCode =
Ben Chenga4973592010-03-31 11:59:18 -07003771 kArmPseudoChainingCellBackwardBranch;
Jeff Hao97319a82009-08-12 16:57:15 -07003772 /* handle the codegen later */
3773 dvmInsertGrowableList(
Bill Buzbee1465db52009-09-23 17:17:35 -07003774 &chainingListByType[kChainingCellBackwardBranch],
Jeff Hao97319a82009-08-12 16:57:15 -07003775 (void *) i);
3776 break;
3777#endif
Ben Chengba4fc8b2009-06-01 13:00:29 -07003778 default:
3779 break;
3780 }
3781 continue;
3782 }
Ben Chenge9695e52009-06-16 16:11:47 -07003783
Bill Buzbee89efc3d2009-07-28 11:22:22 -07003784 ArmLIR *headLIR = NULL;
Ben Chenge9695e52009-06-16 16:11:47 -07003785
Ben Chengba4fc8b2009-06-01 13:00:29 -07003786 for (mir = blockList[i]->firstMIRInsn; mir; mir = mir->next) {
Bill Buzbee1465db52009-09-23 17:17:35 -07003787
Bill Buzbeec6f10662010-02-09 11:16:15 -08003788 dvmCompilerResetRegPool(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07003789 if (gDvmJit.disableOpt & (1 << kTrackLiveTemps)) {
Bill Buzbeec6f10662010-02-09 11:16:15 -08003790 dvmCompilerClobberAllRegs(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07003791 }
3792
3793 if (gDvmJit.disableOpt & (1 << kSuppressLoads)) {
Bill Buzbeec6f10662010-02-09 11:16:15 -08003794 dvmCompilerResetDefTracking(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07003795 }
3796
3797 if (mir->dalvikInsn.opCode >= kMirOpFirst) {
Ben Cheng4238ec22009-08-24 16:32:22 -07003798 handleExtendedMIR(cUnit, mir);
3799 continue;
3800 }
3801
Bill Buzbee1465db52009-09-23 17:17:35 -07003802
Ben Chengba4fc8b2009-06-01 13:00:29 -07003803 OpCode dalvikOpCode = mir->dalvikInsn.opCode;
3804 InstructionFormat dalvikFormat =
3805 dexGetInstrFormat(gDvm.instrFormat, dalvikOpCode);
Bill Buzbee89efc3d2009-07-28 11:22:22 -07003806 ArmLIR *boundaryLIR =
Ben Chenga4973592010-03-31 11:59:18 -07003807 newLIR2(cUnit, kArmPseudoDalvikByteCodeBoundary,
Ben Chengccd6c012009-10-15 14:52:45 -07003808 mir->offset,
3809 (int) dvmCompilerGetDalvikDisassembly(&mir->dalvikInsn)
3810 );
Ben Cheng4238ec22009-08-24 16:32:22 -07003811 if (mir->ssaRep) {
3812 char *ssaString = dvmCompilerGetSSAString(cUnit, mir->ssaRep);
Bill Buzbee1465db52009-09-23 17:17:35 -07003813 newLIR1(cUnit, kArmPseudoSSARep, (int) ssaString);
Ben Cheng4238ec22009-08-24 16:32:22 -07003814 }
3815
Ben Chenge9695e52009-06-16 16:11:47 -07003816 /* Remember the first LIR for this block */
3817 if (headLIR == NULL) {
3818 headLIR = boundaryLIR;
Ben Chengd7d426a2009-09-22 11:23:36 -07003819 /* Set the first boundaryLIR as a scheduling barrier */
3820 headLIR->defMask = ENCODE_ALL;
Ben Chenge9695e52009-06-16 16:11:47 -07003821 }
Ben Cheng4238ec22009-08-24 16:32:22 -07003822
Ben Chengba4fc8b2009-06-01 13:00:29 -07003823 bool notHandled;
3824 /*
3825 * Debugging: screen the opcode first to see if it is in the
3826 * do[-not]-compile list
3827 */
3828 bool singleStepMe =
3829 gDvmJit.includeSelectedOp !=
3830 ((gDvmJit.opList[dalvikOpCode >> 3] &
3831 (1 << (dalvikOpCode & 0x7))) !=
3832 0);
Ben Chengd5adae12010-03-26 17:45:28 -07003833#if defined(WITH_SELF_VERIFICATION)
3834 if (singleStepMe == false) {
3835 singleStepMe = selfVerificationPuntOps(mir);
3836 }
3837#endif
Ben Chengba4fc8b2009-06-01 13:00:29 -07003838 if (singleStepMe || cUnit->allSingleStep) {
3839 notHandled = false;
3840 genInterpSingleStep(cUnit, mir);
3841 } else {
3842 opcodeCoverage[dalvikOpCode]++;
3843 switch (dalvikFormat) {
3844 case kFmt10t:
3845 case kFmt20t:
3846 case kFmt30t:
3847 notHandled = handleFmt10t_Fmt20t_Fmt30t(cUnit,
3848 mir, blockList[i], labelList);
3849 break;
3850 case kFmt10x:
3851 notHandled = handleFmt10x(cUnit, mir);
3852 break;
3853 case kFmt11n:
3854 case kFmt31i:
3855 notHandled = handleFmt11n_Fmt31i(cUnit, mir);
3856 break;
3857 case kFmt11x:
3858 notHandled = handleFmt11x(cUnit, mir);
3859 break;
3860 case kFmt12x:
3861 notHandled = handleFmt12x(cUnit, mir);
3862 break;
3863 case kFmt20bc:
3864 notHandled = handleFmt20bc(cUnit, mir);
3865 break;
3866 case kFmt21c:
3867 case kFmt31c:
3868 notHandled = handleFmt21c_Fmt31c(cUnit, mir);
3869 break;
3870 case kFmt21h:
3871 notHandled = handleFmt21h(cUnit, mir);
3872 break;
3873 case kFmt21s:
3874 notHandled = handleFmt21s(cUnit, mir);
3875 break;
3876 case kFmt21t:
3877 notHandled = handleFmt21t(cUnit, mir, blockList[i],
3878 labelList);
3879 break;
3880 case kFmt22b:
3881 case kFmt22s:
3882 notHandled = handleFmt22b_Fmt22s(cUnit, mir);
3883 break;
3884 case kFmt22c:
3885 notHandled = handleFmt22c(cUnit, mir);
3886 break;
3887 case kFmt22cs:
3888 notHandled = handleFmt22cs(cUnit, mir);
3889 break;
3890 case kFmt22t:
3891 notHandled = handleFmt22t(cUnit, mir, blockList[i],
3892 labelList);
3893 break;
3894 case kFmt22x:
3895 case kFmt32x:
3896 notHandled = handleFmt22x_Fmt32x(cUnit, mir);
3897 break;
3898 case kFmt23x:
3899 notHandled = handleFmt23x(cUnit, mir);
3900 break;
3901 case kFmt31t:
3902 notHandled = handleFmt31t(cUnit, mir);
3903 break;
3904 case kFmt3rc:
3905 case kFmt35c:
3906 notHandled = handleFmt35c_3rc(cUnit, mir, blockList[i],
3907 labelList);
3908 break;
3909 case kFmt3rms:
3910 case kFmt35ms:
3911 notHandled = handleFmt35ms_3rms(cUnit, mir,blockList[i],
3912 labelList);
3913 break;
3914 case kFmt3inline:
Andy McFaddenb0a05412009-11-19 10:23:41 -08003915 case kFmt3rinline:
Bill Buzbeece46c942009-11-20 15:41:34 -08003916 notHandled = handleExecuteInline(cUnit, mir);
Andy McFaddenb0a05412009-11-19 10:23:41 -08003917 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07003918 case kFmt51l:
3919 notHandled = handleFmt51l(cUnit, mir);
3920 break;
3921 default:
3922 notHandled = true;
3923 break;
3924 }
3925 }
3926 if (notHandled) {
3927 LOGE("%#06x: Opcode 0x%x (%s) / Fmt %d not handled\n",
3928 mir->offset,
3929 dalvikOpCode, getOpcodeName(dalvikOpCode),
3930 dalvikFormat);
Bill Buzbeefc519dc2010-03-06 23:30:57 -08003931 dvmCompilerAbort(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003932 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07003933 }
3934 }
Ben Cheng4238ec22009-08-24 16:32:22 -07003935
Bill Buzbee1465db52009-09-23 17:17:35 -07003936 if (blockList[i]->blockType == kEntryBlock) {
Ben Cheng4238ec22009-08-24 16:32:22 -07003937 dvmCompilerAppendLIR(cUnit,
3938 (LIR *) cUnit->loopAnalysis->branchToBody);
3939 dvmCompilerAppendLIR(cUnit,
3940 (LIR *) cUnit->loopAnalysis->branchToPCR);
3941 }
3942
3943 if (headLIR) {
3944 /*
3945 * Eliminate redundant loads/stores and delay stores into later
3946 * slots
3947 */
3948 dvmCompilerApplyLocalOptimizations(cUnit, (LIR *) headLIR,
3949 cUnit->lastLIRInsn);
3950 }
3951
3952gen_fallthrough:
Ben Cheng1efc9c52009-06-08 18:25:27 -07003953 /*
3954 * Check if the block is terminated due to trace length constraint -
3955 * insert an unconditional branch to the chaining cell.
3956 */
3957 if (blockList[i]->needFallThroughBranch) {
3958 genUnconditionalBranch(cUnit,
3959 &labelList[blockList[i]->fallThrough->id]);
3960 }
3961
Ben Chengba4fc8b2009-06-01 13:00:29 -07003962 }
3963
Ben Chenge9695e52009-06-16 16:11:47 -07003964 /* Handle the chaining cells in predefined order */
Ben Chengcec26f62010-01-15 15:29:33 -08003965 for (i = 0; i < kChainingCellGap; i++) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07003966 size_t j;
3967 int *blockIdList = (int *) chainingListByType[i].elemList;
3968
3969 cUnit->numChainingCells[i] = chainingListByType[i].numUsed;
3970
3971 /* No chaining cells of this type */
3972 if (cUnit->numChainingCells[i] == 0)
3973 continue;
3974
3975 /* Record the first LIR for a new type of chaining cell */
3976 cUnit->firstChainingLIR[i] = (LIR *) &labelList[blockIdList[0]];
3977
3978 for (j = 0; j < chainingListByType[i].numUsed; j++) {
3979 int blockId = blockIdList[j];
3980
3981 /* Align this chaining cell first */
Bill Buzbee1465db52009-09-23 17:17:35 -07003982 newLIR0(cUnit, kArmPseudoPseudoAlign4);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003983
3984 /* Insert the pseudo chaining instruction */
3985 dvmCompilerAppendLIR(cUnit, (LIR *) &labelList[blockId]);
3986
3987
3988 switch (blockList[blockId]->blockType) {
Bill Buzbee1465db52009-09-23 17:17:35 -07003989 case kChainingCellNormal:
Ben Cheng1efc9c52009-06-08 18:25:27 -07003990 handleNormalChainingCell(cUnit,
Ben Chengba4fc8b2009-06-01 13:00:29 -07003991 blockList[blockId]->startOffset);
3992 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07003993 case kChainingCellInvokeSingleton:
Ben Cheng38329f52009-07-07 14:19:20 -07003994 handleInvokeSingletonChainingCell(cUnit,
Ben Chengba4fc8b2009-06-01 13:00:29 -07003995 blockList[blockId]->containingMethod);
3996 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07003997 case kChainingCellInvokePredicted:
Ben Cheng38329f52009-07-07 14:19:20 -07003998 handleInvokePredictedChainingCell(cUnit);
3999 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07004000 case kChainingCellHot:
Ben Cheng1efc9c52009-06-08 18:25:27 -07004001 handleHotChainingCell(cUnit,
Ben Chengba4fc8b2009-06-01 13:00:29 -07004002 blockList[blockId]->startOffset);
4003 break;
Bill Buzbee9c4b7c82009-09-10 10:10:38 -07004004#if defined(WITH_SELF_VERIFICATION) || defined(WITH_JIT_TUNING)
Bill Buzbee1465db52009-09-23 17:17:35 -07004005 case kChainingCellBackwardBranch:
Jeff Hao97319a82009-08-12 16:57:15 -07004006 handleBackwardBranchChainingCell(cUnit,
4007 blockList[blockId]->startOffset);
4008 break;
4009#endif
Ben Chengba4fc8b2009-06-01 13:00:29 -07004010 default:
Bill Buzbee1465db52009-09-23 17:17:35 -07004011 LOGE("Bad blocktype %d", blockList[blockId]->blockType);
Bill Buzbeefc519dc2010-03-06 23:30:57 -08004012 dvmCompilerAbort(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -07004013 }
4014 }
4015 }
Ben Chenge9695e52009-06-16 16:11:47 -07004016
Ben Chengcec26f62010-01-15 15:29:33 -08004017 /* Mark the bottom of chaining cells */
4018 cUnit->chainingCellBottom = (LIR *) newLIR0(cUnit, kArmChainingCellBottom);
4019
Ben Cheng6c10a972009-10-29 14:39:18 -07004020 /*
4021 * Generate the branch to the dvmJitToInterpNoChain entry point at the end
4022 * of all chaining cells for the overflow cases.
4023 */
4024 if (cUnit->switchOverflowPad) {
4025 loadConstant(cUnit, r0, (int) cUnit->switchOverflowPad);
4026 loadWordDisp(cUnit, rGLUE, offsetof(InterpState,
4027 jitToInterpEntries.dvmJitToInterpNoChain), r2);
4028 opRegReg(cUnit, kOpAdd, r1, r1);
4029 opRegRegReg(cUnit, kOpAdd, r4PC, r0, r1);
Ben Cheng978738d2010-05-13 13:45:57 -07004030#if defined(WITH_JIT_TUNING)
Ben Cheng6c10a972009-10-29 14:39:18 -07004031 loadConstant(cUnit, r0, kSwitchOverflow);
4032#endif
4033 opReg(cUnit, kOpBlx, r2);
4034 }
4035
Ben Chenge9695e52009-06-16 16:11:47 -07004036 dvmCompilerApplyGlobalOptimizations(cUnit);
jeffhao9e45c0b2010-02-03 10:24:05 -08004037
4038#if defined(WITH_SELF_VERIFICATION)
4039 selfVerificationBranchInsertPass(cUnit);
4040#endif
Ben Chengba4fc8b2009-06-01 13:00:29 -07004041}
4042
4043/* Accept the work and start compiling */
Bill Buzbee716f1202009-07-23 13:22:09 -07004044bool dvmCompilerDoWork(CompilerWorkOrder *work)
Ben Chengba4fc8b2009-06-01 13:00:29 -07004045{
Ben Chengccd6c012009-10-15 14:52:45 -07004046 bool res;
Ben Chengba4fc8b2009-06-01 13:00:29 -07004047
Ben Cheng6999d842010-01-26 16:46:15 -08004048 if (gDvmJit.codeCacheFull) {
Ben Chengccd6c012009-10-15 14:52:45 -07004049 return false;
4050 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07004051
Ben Chengccd6c012009-10-15 14:52:45 -07004052 switch (work->kind) {
4053 case kWorkOrderMethod:
4054 res = dvmCompileMethod(work->info, &work->result);
4055 break;
4056 case kWorkOrderTrace:
4057 /* Start compilation with maximally allowed trace length */
Bill Buzbeefc519dc2010-03-06 23:30:57 -08004058 res = dvmCompileTrace(work->info, JIT_MAX_TRACE_LEN, &work->result,
4059 work->bailPtr);
Ben Chengccd6c012009-10-15 14:52:45 -07004060 break;
4061 case kWorkOrderTraceDebug: {
4062 bool oldPrintMe = gDvmJit.printMe;
4063 gDvmJit.printMe = true;
4064 /* Start compilation with maximally allowed trace length */
Bill Buzbeefc519dc2010-03-06 23:30:57 -08004065 res = dvmCompileTrace(work->info, JIT_MAX_TRACE_LEN, &work->result,
4066 work->bailPtr);
Elliott Hughes672511b2010-04-26 17:40:13 -07004067 gDvmJit.printMe = oldPrintMe;
Ben Chengccd6c012009-10-15 14:52:45 -07004068 break;
4069 }
4070 default:
4071 res = false;
Bill Buzbeefc519dc2010-03-06 23:30:57 -08004072 LOGE("Jit: unknown work order type");
Elliott Hughes672511b2010-04-26 17:40:13 -07004073 assert(0); // Bail if debug build, discard otherwise
Ben Chengccd6c012009-10-15 14:52:45 -07004074 }
4075 return res;
Ben Chengba4fc8b2009-06-01 13:00:29 -07004076}
4077
Ben Chengba4fc8b2009-06-01 13:00:29 -07004078/* Architectural-specific debugging helpers go here */
4079void dvmCompilerArchDump(void)
4080{
4081 /* Print compiled opcode in this VM instance */
4082 int i, start, streak;
4083 char buf[1024];
4084
4085 streak = i = 0;
4086 buf[0] = 0;
4087 while (opcodeCoverage[i] == 0 && i < 256) {
4088 i++;
4089 }
4090 if (i == 256) {
4091 return;
4092 }
4093 for (start = i++, streak = 1; i < 256; i++) {
4094 if (opcodeCoverage[i]) {
4095 streak++;
4096 } else {
4097 if (streak == 1) {
4098 sprintf(buf+strlen(buf), "%x,", start);
4099 } else {
4100 sprintf(buf+strlen(buf), "%x-%x,", start, start + streak - 1);
4101 }
4102 streak = 0;
4103 while (opcodeCoverage[i] == 0 && i < 256) {
4104 i++;
4105 }
4106 if (i < 256) {
4107 streak = 1;
4108 start = i;
4109 }
4110 }
4111 }
4112 if (streak) {
4113 if (streak == 1) {
4114 sprintf(buf+strlen(buf), "%x", start);
4115 } else {
4116 sprintf(buf+strlen(buf), "%x-%x", start, start + streak - 1);
4117 }
4118 }
4119 if (strlen(buf)) {
Ben Cheng8b258bf2009-06-24 17:27:07 -07004120 LOGD("dalvik.vm.jit.op = %s", buf);
Ben Chengba4fc8b2009-06-01 13:00:29 -07004121 }
4122}
Ben Chengd7d426a2009-09-22 11:23:36 -07004123
4124/* Common initialization routine for an architecture family */
4125bool dvmCompilerArchInit()
4126{
4127 int i;
4128
Bill Buzbee1465db52009-09-23 17:17:35 -07004129 for (i = 0; i < kArmLast; i++) {
Ben Chengd7d426a2009-09-22 11:23:36 -07004130 if (EncodingMap[i].opCode != i) {
4131 LOGE("Encoding order for %s is wrong: expecting %d, seeing %d",
4132 EncodingMap[i].name, i, EncodingMap[i].opCode);
Bill Buzbeefc519dc2010-03-06 23:30:57 -08004133 dvmAbort(); // OK to dvmAbort - build error
Ben Chengd7d426a2009-09-22 11:23:36 -07004134 }
4135 }
4136
Ben Cheng5d90c202009-11-22 23:31:11 -08004137 return dvmCompilerArchVariantInit();
4138}
4139
4140void *dvmCompilerGetInterpretTemplate()
4141{
4142 return (void*) ((int)gDvmJit.codeCache +
4143 templateEntryOffsets[TEMPLATE_INTERPRET]);
4144}
4145
4146/* Needed by the ld/st optmizatons */
4147ArmLIR* dvmCompilerRegCopyNoInsert(CompilationUnit *cUnit, int rDest, int rSrc)
4148{
4149 return genRegCopyNoInsert(cUnit, rDest, rSrc);
4150}
4151
4152/* Needed by the register allocator */
4153ArmLIR* dvmCompilerRegCopy(CompilationUnit *cUnit, int rDest, int rSrc)
4154{
4155 return genRegCopy(cUnit, rDest, rSrc);
4156}
4157
4158/* Needed by the register allocator */
4159void dvmCompilerRegCopyWide(CompilationUnit *cUnit, int destLo, int destHi,
4160 int srcLo, int srcHi)
4161{
4162 genRegCopyWide(cUnit, destLo, destHi, srcLo, srcHi);
4163}
4164
4165void dvmCompilerFlushRegImpl(CompilationUnit *cUnit, int rBase,
4166 int displacement, int rSrc, OpSize size)
4167{
4168 storeBaseDisp(cUnit, rBase, displacement, rSrc, size);
4169}
4170
4171void dvmCompilerFlushRegWideImpl(CompilationUnit *cUnit, int rBase,
4172 int displacement, int rSrcLo, int rSrcHi)
4173{
4174 storeBaseDispWide(cUnit, rBase, displacement, rSrcLo, rSrcHi);
Ben Chengd7d426a2009-09-22 11:23:36 -07004175}