blob: 0a59ea42a92bea4904e2b03fd95f8037c2da0bfe [file] [log] [blame]
Ben Chengba4fc8b2009-06-01 13:00:29 -07001/*
2 * Copyright (C) 2009 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Bill Buzbee50a6bf22009-07-08 13:08:04 -070017/*
18 * This file contains codegen and support common to all supported
19 * ARM variants. It is included by:
20 *
21 * Codegen-$(TARGET_ARCH_VARIANT).c
22 *
23 * which combines this common code with specific support found in the
24 * applicable directory below this one.
25 */
26
Ben Cheng5d90c202009-11-22 23:31:11 -080027static bool genConversionCall(CompilationUnit *cUnit, MIR *mir, void *funct,
28 int srcSize, int tgtSize)
29{
30 /*
31 * Don't optimize the register usage since it calls out to template
32 * functions
33 */
34 RegLocation rlSrc;
35 RegLocation rlDest;
Bill Buzbeec6f10662010-02-09 11:16:15 -080036 dvmCompilerFlushAllRegs(cUnit); /* Send everything to home location */
Ben Cheng5d90c202009-11-22 23:31:11 -080037 if (srcSize == 1) {
Bill Buzbeec6f10662010-02-09 11:16:15 -080038 rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
Ben Cheng5d90c202009-11-22 23:31:11 -080039 loadValueDirectFixed(cUnit, rlSrc, r0);
40 } else {
Bill Buzbeec6f10662010-02-09 11:16:15 -080041 rlSrc = dvmCompilerGetSrcWide(cUnit, mir, 0, 1);
Ben Cheng5d90c202009-11-22 23:31:11 -080042 loadValueDirectWideFixed(cUnit, rlSrc, r0, r1);
43 }
Ben Chengbd1326d2010-04-02 15:04:53 -070044 LOAD_FUNC_ADDR(cUnit, r2, (int)funct);
Ben Cheng5d90c202009-11-22 23:31:11 -080045 opReg(cUnit, kOpBlx, r2);
Elliott Hughes6a555132010-02-25 15:41:42 -080046 dvmCompilerClobberCallRegs(cUnit);
Ben Cheng5d90c202009-11-22 23:31:11 -080047 if (tgtSize == 1) {
48 RegLocation rlResult;
Bill Buzbeec6f10662010-02-09 11:16:15 -080049 rlDest = dvmCompilerGetDest(cUnit, mir, 0);
50 rlResult = dvmCompilerGetReturn(cUnit);
Ben Cheng5d90c202009-11-22 23:31:11 -080051 storeValue(cUnit, rlDest, rlResult);
52 } else {
53 RegLocation rlResult;
Bill Buzbeec6f10662010-02-09 11:16:15 -080054 rlDest = dvmCompilerGetDestWide(cUnit, mir, 0, 1);
55 rlResult = dvmCompilerGetReturnWide(cUnit);
Ben Cheng5d90c202009-11-22 23:31:11 -080056 storeValueWide(cUnit, rlDest, rlResult);
57 }
58 return false;
59}
Ben Chengba4fc8b2009-06-01 13:00:29 -070060
Ben Chengba4fc8b2009-06-01 13:00:29 -070061
Ben Cheng5d90c202009-11-22 23:31:11 -080062static bool genArithOpFloatPortable(CompilationUnit *cUnit, MIR *mir,
63 RegLocation rlDest, RegLocation rlSrc1,
64 RegLocation rlSrc2)
65{
66 RegLocation rlResult;
67 void* funct;
68
Ben Cheng5d90c202009-11-22 23:31:11 -080069 switch (mir->dalvikInsn.opCode) {
70 case OP_ADD_FLOAT_2ADDR:
71 case OP_ADD_FLOAT:
72 funct = (void*) __aeabi_fadd;
73 break;
74 case OP_SUB_FLOAT_2ADDR:
75 case OP_SUB_FLOAT:
76 funct = (void*) __aeabi_fsub;
77 break;
78 case OP_DIV_FLOAT_2ADDR:
79 case OP_DIV_FLOAT:
80 funct = (void*) __aeabi_fdiv;
81 break;
82 case OP_MUL_FLOAT_2ADDR:
83 case OP_MUL_FLOAT:
84 funct = (void*) __aeabi_fmul;
85 break;
86 case OP_REM_FLOAT_2ADDR:
87 case OP_REM_FLOAT:
88 funct = (void*) fmodf;
89 break;
90 case OP_NEG_FLOAT: {
91 genNegFloat(cUnit, rlDest, rlSrc1);
92 return false;
93 }
94 default:
95 return true;
96 }
Bill Buzbeec6f10662010-02-09 11:16:15 -080097 dvmCompilerFlushAllRegs(cUnit); /* Send everything to home location */
Ben Cheng5d90c202009-11-22 23:31:11 -080098 loadValueDirectFixed(cUnit, rlSrc1, r0);
99 loadValueDirectFixed(cUnit, rlSrc2, r1);
Ben Chengbd1326d2010-04-02 15:04:53 -0700100 LOAD_FUNC_ADDR(cUnit, r2, (int)funct);
Ben Cheng5d90c202009-11-22 23:31:11 -0800101 opReg(cUnit, kOpBlx, r2);
Elliott Hughes6a555132010-02-25 15:41:42 -0800102 dvmCompilerClobberCallRegs(cUnit);
Bill Buzbeec6f10662010-02-09 11:16:15 -0800103 rlResult = dvmCompilerGetReturn(cUnit);
Ben Cheng5d90c202009-11-22 23:31:11 -0800104 storeValue(cUnit, rlDest, rlResult);
105 return false;
106}
107
108static bool genArithOpDoublePortable(CompilationUnit *cUnit, MIR *mir,
109 RegLocation rlDest, RegLocation rlSrc1,
110 RegLocation rlSrc2)
111{
112 RegLocation rlResult;
113 void* funct;
114
Ben Cheng5d90c202009-11-22 23:31:11 -0800115 switch (mir->dalvikInsn.opCode) {
116 case OP_ADD_DOUBLE_2ADDR:
117 case OP_ADD_DOUBLE:
118 funct = (void*) __aeabi_dadd;
119 break;
120 case OP_SUB_DOUBLE_2ADDR:
121 case OP_SUB_DOUBLE:
122 funct = (void*) __aeabi_dsub;
123 break;
124 case OP_DIV_DOUBLE_2ADDR:
125 case OP_DIV_DOUBLE:
126 funct = (void*) __aeabi_ddiv;
127 break;
128 case OP_MUL_DOUBLE_2ADDR:
129 case OP_MUL_DOUBLE:
130 funct = (void*) __aeabi_dmul;
131 break;
132 case OP_REM_DOUBLE_2ADDR:
133 case OP_REM_DOUBLE:
134 funct = (void*) fmod;
135 break;
136 case OP_NEG_DOUBLE: {
137 genNegDouble(cUnit, rlDest, rlSrc1);
138 return false;
139 }
140 default:
141 return true;
142 }
Bill Buzbeec6f10662010-02-09 11:16:15 -0800143 dvmCompilerFlushAllRegs(cUnit); /* Send everything to home location */
Ben Chengbd1326d2010-04-02 15:04:53 -0700144 LOAD_FUNC_ADDR(cUnit, rlr, (int)funct);
Ben Cheng5d90c202009-11-22 23:31:11 -0800145 loadValueDirectWideFixed(cUnit, rlSrc1, r0, r1);
146 loadValueDirectWideFixed(cUnit, rlSrc2, r2, r3);
147 opReg(cUnit, kOpBlx, rlr);
Elliott Hughes6a555132010-02-25 15:41:42 -0800148 dvmCompilerClobberCallRegs(cUnit);
Bill Buzbeec6f10662010-02-09 11:16:15 -0800149 rlResult = dvmCompilerGetReturnWide(cUnit);
Ben Cheng5d90c202009-11-22 23:31:11 -0800150 storeValueWide(cUnit, rlDest, rlResult);
151 return false;
152}
153
154static bool genConversionPortable(CompilationUnit *cUnit, MIR *mir)
155{
156 OpCode opCode = mir->dalvikInsn.opCode;
157
Ben Cheng5d90c202009-11-22 23:31:11 -0800158 switch (opCode) {
159 case OP_INT_TO_FLOAT:
160 return genConversionCall(cUnit, mir, (void*)__aeabi_i2f, 1, 1);
161 case OP_FLOAT_TO_INT:
162 return genConversionCall(cUnit, mir, (void*)__aeabi_f2iz, 1, 1);
163 case OP_DOUBLE_TO_FLOAT:
164 return genConversionCall(cUnit, mir, (void*)__aeabi_d2f, 2, 1);
165 case OP_FLOAT_TO_DOUBLE:
166 return genConversionCall(cUnit, mir, (void*)__aeabi_f2d, 1, 2);
167 case OP_INT_TO_DOUBLE:
168 return genConversionCall(cUnit, mir, (void*)__aeabi_i2d, 1, 2);
169 case OP_DOUBLE_TO_INT:
170 return genConversionCall(cUnit, mir, (void*)__aeabi_d2iz, 2, 1);
171 case OP_FLOAT_TO_LONG:
172 return genConversionCall(cUnit, mir, (void*)dvmJitf2l, 1, 2);
173 case OP_LONG_TO_FLOAT:
174 return genConversionCall(cUnit, mir, (void*)__aeabi_l2f, 2, 1);
175 case OP_DOUBLE_TO_LONG:
176 return genConversionCall(cUnit, mir, (void*)dvmJitd2l, 2, 2);
177 case OP_LONG_TO_DOUBLE:
178 return genConversionCall(cUnit, mir, (void*)__aeabi_l2d, 2, 2);
179 default:
180 return true;
181 }
182 return false;
183}
Ben Chengba4fc8b2009-06-01 13:00:29 -0700184
Jeff Hao97319a82009-08-12 16:57:15 -0700185#if defined(WITH_SELF_VERIFICATION)
jeffhao9e45c0b2010-02-03 10:24:05 -0800186static void selfVerificationBranchInsert(LIR *currentLIR, ArmOpCode opCode,
187 int dest, int src1)
Jeff Hao97319a82009-08-12 16:57:15 -0700188{
jeffhao9e45c0b2010-02-03 10:24:05 -0800189 ArmLIR *insn = dvmCompilerNew(sizeof(ArmLIR), true);
190 insn->opCode = opCode;
191 insn->operands[0] = dest;
192 insn->operands[1] = src1;
193 setupResourceMasks(insn);
194 dvmCompilerInsertLIRBefore(currentLIR, (LIR *) insn);
Jeff Hao97319a82009-08-12 16:57:15 -0700195}
196
jeffhao9e45c0b2010-02-03 10:24:05 -0800197static void selfVerificationBranchInsertPass(CompilationUnit *cUnit)
Jeff Hao97319a82009-08-12 16:57:15 -0700198{
jeffhao9e45c0b2010-02-03 10:24:05 -0800199 ArmLIR *thisLIR;
200 ArmLIR *branchLIR = dvmCompilerNew(sizeof(ArmLIR), true);
201 TemplateOpCode opCode = TEMPLATE_MEM_OP_DECODE;
Jeff Hao97319a82009-08-12 16:57:15 -0700202
jeffhao9e45c0b2010-02-03 10:24:05 -0800203 for (thisLIR = (ArmLIR *) cUnit->firstLIRInsn;
204 thisLIR != (ArmLIR *) cUnit->lastLIRInsn;
205 thisLIR = NEXT_LIR(thisLIR)) {
206 if (thisLIR->branchInsertSV) {
207 /* Branch to mem op decode template */
208 selfVerificationBranchInsert((LIR *) thisLIR, kThumbBlx1,
209 (int) gDvmJit.codeCache + templateEntryOffsets[opCode],
210 (int) gDvmJit.codeCache + templateEntryOffsets[opCode]);
211 selfVerificationBranchInsert((LIR *) thisLIR, kThumbBlx2,
212 (int) gDvmJit.codeCache + templateEntryOffsets[opCode],
213 (int) gDvmJit.codeCache + templateEntryOffsets[opCode]);
Jeff Hao97319a82009-08-12 16:57:15 -0700214 }
215 }
Jeff Hao97319a82009-08-12 16:57:15 -0700216}
Jeff Hao97319a82009-08-12 16:57:15 -0700217#endif
218
Bill Buzbeebe6534f2010-03-12 16:01:35 -0800219/* Generate conditional branch instructions */
220static ArmLIR *genConditionalBranch(CompilationUnit *cUnit,
221 ArmConditionCode cond,
222 ArmLIR *target)
223{
224 ArmLIR *branch = opCondBranch(cUnit, cond);
225 branch->generic.target = (LIR *) target;
226 return branch;
227}
228
Ben Chengba4fc8b2009-06-01 13:00:29 -0700229/* Generate a unconditional branch to go to the interpreter */
Bill Buzbee89efc3d2009-07-28 11:22:22 -0700230static inline ArmLIR *genTrap(CompilationUnit *cUnit, int dOffset,
231 ArmLIR *pcrLabel)
Ben Chengba4fc8b2009-06-01 13:00:29 -0700232{
Bill Buzbee1465db52009-09-23 17:17:35 -0700233 ArmLIR *branch = opNone(cUnit, kOpUncondBr);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700234 return genCheckCommon(cUnit, dOffset, branch, pcrLabel);
235}
236
237/* Load a wide field from an object instance */
238static void genIGetWide(CompilationUnit *cUnit, MIR *mir, int fieldOffset)
239{
240 DecodedInstruction *dInsn = &mir->dalvikInsn;
Bill Buzbeec6f10662010-02-09 11:16:15 -0800241 RegLocation rlObj = dvmCompilerGetSrc(cUnit, mir, 0);
242 RegLocation rlDest = dvmCompilerGetDestWide(cUnit, mir, 0, 1);
Bill Buzbee1465db52009-09-23 17:17:35 -0700243 RegLocation rlResult;
244 rlObj = loadValue(cUnit, rlObj, kCoreReg);
Bill Buzbeec6f10662010-02-09 11:16:15 -0800245 int regPtr = dvmCompilerAllocTemp(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700246
Bill Buzbee1465db52009-09-23 17:17:35 -0700247 assert(rlDest.wide);
Ben Chenge9695e52009-06-16 16:11:47 -0700248
Bill Buzbee1465db52009-09-23 17:17:35 -0700249 genNullCheck(cUnit, rlObj.sRegLow, rlObj.lowReg, mir->offset,
250 NULL);/* null object? */
251 opRegRegImm(cUnit, kOpAdd, regPtr, rlObj.lowReg, fieldOffset);
Bill Buzbeec6f10662010-02-09 11:16:15 -0800252 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kAnyReg, true);
Ben Cheng11d8f142010-03-24 15:24:19 -0700253
254 HEAP_ACCESS_SHADOW(true);
Bill Buzbee1465db52009-09-23 17:17:35 -0700255 loadPair(cUnit, regPtr, rlResult.lowReg, rlResult.highReg);
Ben Cheng11d8f142010-03-24 15:24:19 -0700256 HEAP_ACCESS_SHADOW(false);
257
Bill Buzbeec6f10662010-02-09 11:16:15 -0800258 dvmCompilerFreeTemp(cUnit, regPtr);
Bill Buzbee1465db52009-09-23 17:17:35 -0700259 storeValueWide(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700260}
261
262/* Store a wide field to an object instance */
263static void genIPutWide(CompilationUnit *cUnit, MIR *mir, int fieldOffset)
264{
265 DecodedInstruction *dInsn = &mir->dalvikInsn;
Bill Buzbeec6f10662010-02-09 11:16:15 -0800266 RegLocation rlSrc = dvmCompilerGetSrcWide(cUnit, mir, 0, 1);
267 RegLocation rlObj = dvmCompilerGetSrc(cUnit, mir, 2);
Bill Buzbee1465db52009-09-23 17:17:35 -0700268 rlObj = loadValue(cUnit, rlObj, kCoreReg);
269 int regPtr;
270 rlSrc = loadValueWide(cUnit, rlSrc, kAnyReg);
271 genNullCheck(cUnit, rlObj.sRegLow, rlObj.lowReg, mir->offset,
272 NULL);/* null object? */
Bill Buzbeec6f10662010-02-09 11:16:15 -0800273 regPtr = dvmCompilerAllocTemp(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -0700274 opRegRegImm(cUnit, kOpAdd, regPtr, rlObj.lowReg, fieldOffset);
Ben Cheng11d8f142010-03-24 15:24:19 -0700275
276 HEAP_ACCESS_SHADOW(true);
Bill Buzbee1465db52009-09-23 17:17:35 -0700277 storePair(cUnit, regPtr, rlSrc.lowReg, rlSrc.highReg);
Ben Cheng11d8f142010-03-24 15:24:19 -0700278 HEAP_ACCESS_SHADOW(false);
279
Bill Buzbeec6f10662010-02-09 11:16:15 -0800280 dvmCompilerFreeTemp(cUnit, regPtr);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700281}
282
283/*
284 * Load a field from an object instance
285 *
Ben Chengba4fc8b2009-06-01 13:00:29 -0700286 */
Bill Buzbee270c1d62009-08-13 16:58:07 -0700287static void genIGet(CompilationUnit *cUnit, MIR *mir, OpSize size,
Ben Chengba4fc8b2009-06-01 13:00:29 -0700288 int fieldOffset)
289{
Bill Buzbee1465db52009-09-23 17:17:35 -0700290 int regPtr;
291 RegLocation rlResult;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700292 DecodedInstruction *dInsn = &mir->dalvikInsn;
Bill Buzbeec6f10662010-02-09 11:16:15 -0800293 RegLocation rlObj = dvmCompilerGetSrc(cUnit, mir, 0);
294 RegLocation rlDest = dvmCompilerGetDest(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -0700295 rlObj = loadValue(cUnit, rlObj, kCoreReg);
Bill Buzbeec6f10662010-02-09 11:16:15 -0800296 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kAnyReg, true);
Bill Buzbee1465db52009-09-23 17:17:35 -0700297 genNullCheck(cUnit, rlObj.sRegLow, rlObj.lowReg, mir->offset,
298 NULL);/* null object? */
Ben Cheng11d8f142010-03-24 15:24:19 -0700299
300 HEAP_ACCESS_SHADOW(true);
Ben Cheng5d90c202009-11-22 23:31:11 -0800301 loadBaseDisp(cUnit, mir, rlObj.lowReg, fieldOffset, rlResult.lowReg,
302 size, rlObj.sRegLow);
Ben Cheng11d8f142010-03-24 15:24:19 -0700303 HEAP_ACCESS_SHADOW(false);
304
Bill Buzbee1465db52009-09-23 17:17:35 -0700305 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700306}
307
308/*
309 * Store a field to an object instance
310 *
Ben Chengba4fc8b2009-06-01 13:00:29 -0700311 */
Bill Buzbee270c1d62009-08-13 16:58:07 -0700312static void genIPut(CompilationUnit *cUnit, MIR *mir, OpSize size,
Ben Chengba4fc8b2009-06-01 13:00:29 -0700313 int fieldOffset)
314{
315 DecodedInstruction *dInsn = &mir->dalvikInsn;
Bill Buzbeec6f10662010-02-09 11:16:15 -0800316 RegLocation rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
317 RegLocation rlObj = dvmCompilerGetSrc(cUnit, mir, 1);
Bill Buzbee1465db52009-09-23 17:17:35 -0700318 rlObj = loadValue(cUnit, rlObj, kCoreReg);
319 rlSrc = loadValue(cUnit, rlSrc, kAnyReg);
320 int regPtr;
321 genNullCheck(cUnit, rlObj.sRegLow, rlObj.lowReg, mir->offset,
322 NULL);/* null object? */
Ben Cheng11d8f142010-03-24 15:24:19 -0700323
324 HEAP_ACCESS_SHADOW(true);
Bill Buzbee1465db52009-09-23 17:17:35 -0700325 storeBaseDisp(cUnit, rlObj.lowReg, fieldOffset, rlSrc.lowReg, size);
Ben Cheng11d8f142010-03-24 15:24:19 -0700326 HEAP_ACCESS_SHADOW(false);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700327}
328
329
Ben Chengba4fc8b2009-06-01 13:00:29 -0700330/*
331 * Generate array load
Ben Chengba4fc8b2009-06-01 13:00:29 -0700332 */
Bill Buzbee270c1d62009-08-13 16:58:07 -0700333static void genArrayGet(CompilationUnit *cUnit, MIR *mir, OpSize size,
Bill Buzbee1465db52009-09-23 17:17:35 -0700334 RegLocation rlArray, RegLocation rlIndex,
335 RegLocation rlDest, int scale)
Ben Chengba4fc8b2009-06-01 13:00:29 -0700336{
337 int lenOffset = offsetof(ArrayObject, length);
338 int dataOffset = offsetof(ArrayObject, contents);
Bill Buzbee1465db52009-09-23 17:17:35 -0700339 RegLocation rlResult;
340 rlArray = loadValue(cUnit, rlArray, kCoreReg);
341 rlIndex = loadValue(cUnit, rlIndex, kCoreReg);
342 int regPtr;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700343
344 /* null object? */
Ben Cheng4238ec22009-08-24 16:32:22 -0700345 ArmLIR * pcrLabel = NULL;
346
347 if (!(mir->OptimizationFlags & MIR_IGNORE_NULL_CHECK)) {
Bill Buzbee1465db52009-09-23 17:17:35 -0700348 pcrLabel = genNullCheck(cUnit, rlArray.sRegLow,
349 rlArray.lowReg, mir->offset, NULL);
Ben Cheng4238ec22009-08-24 16:32:22 -0700350 }
351
Bill Buzbeec6f10662010-02-09 11:16:15 -0800352 regPtr = dvmCompilerAllocTemp(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -0700353
Ben Cheng4238ec22009-08-24 16:32:22 -0700354 if (!(mir->OptimizationFlags & MIR_IGNORE_RANGE_CHECK)) {
Bill Buzbeec6f10662010-02-09 11:16:15 -0800355 int regLen = dvmCompilerAllocTemp(cUnit);
Ben Cheng4238ec22009-08-24 16:32:22 -0700356 /* Get len */
Bill Buzbee1465db52009-09-23 17:17:35 -0700357 loadWordDisp(cUnit, rlArray.lowReg, lenOffset, regLen);
358 /* regPtr -> array data */
359 opRegRegImm(cUnit, kOpAdd, regPtr, rlArray.lowReg, dataOffset);
360 genBoundsCheck(cUnit, rlIndex.lowReg, regLen, mir->offset,
361 pcrLabel);
Bill Buzbeec6f10662010-02-09 11:16:15 -0800362 dvmCompilerFreeTemp(cUnit, regLen);
Ben Cheng4238ec22009-08-24 16:32:22 -0700363 } else {
Bill Buzbee1465db52009-09-23 17:17:35 -0700364 /* regPtr -> array data */
365 opRegRegImm(cUnit, kOpAdd, regPtr, rlArray.lowReg, dataOffset);
Ben Cheng4238ec22009-08-24 16:32:22 -0700366 }
Bill Buzbee1465db52009-09-23 17:17:35 -0700367 if ((size == kLong) || (size == kDouble)) {
368 if (scale) {
Bill Buzbeec6f10662010-02-09 11:16:15 -0800369 int rNewIndex = dvmCompilerAllocTemp(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -0700370 opRegRegImm(cUnit, kOpLsl, rNewIndex, rlIndex.lowReg, scale);
371 opRegReg(cUnit, kOpAdd, regPtr, rNewIndex);
Bill Buzbeec6f10662010-02-09 11:16:15 -0800372 dvmCompilerFreeTemp(cUnit, rNewIndex);
Bill Buzbee1465db52009-09-23 17:17:35 -0700373 } else {
374 opRegReg(cUnit, kOpAdd, regPtr, rlIndex.lowReg);
375 }
Bill Buzbeec6f10662010-02-09 11:16:15 -0800376 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kAnyReg, true);
Ben Cheng11d8f142010-03-24 15:24:19 -0700377
378 HEAP_ACCESS_SHADOW(true);
Bill Buzbee1465db52009-09-23 17:17:35 -0700379 loadPair(cUnit, regPtr, rlResult.lowReg, rlResult.highReg);
Ben Cheng11d8f142010-03-24 15:24:19 -0700380 HEAP_ACCESS_SHADOW(false);
381
Bill Buzbeec6f10662010-02-09 11:16:15 -0800382 dvmCompilerFreeTemp(cUnit, regPtr);
Bill Buzbee1465db52009-09-23 17:17:35 -0700383 storeValueWide(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700384 } else {
Bill Buzbeec6f10662010-02-09 11:16:15 -0800385 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kAnyReg, true);
Ben Cheng11d8f142010-03-24 15:24:19 -0700386
387 HEAP_ACCESS_SHADOW(true);
Bill Buzbee1465db52009-09-23 17:17:35 -0700388 loadBaseIndexed(cUnit, regPtr, rlIndex.lowReg, rlResult.lowReg,
389 scale, size);
Ben Cheng11d8f142010-03-24 15:24:19 -0700390 HEAP_ACCESS_SHADOW(false);
391
Bill Buzbeec6f10662010-02-09 11:16:15 -0800392 dvmCompilerFreeTemp(cUnit, regPtr);
Bill Buzbee1465db52009-09-23 17:17:35 -0700393 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700394 }
395}
396
Ben Chengba4fc8b2009-06-01 13:00:29 -0700397/*
398 * Generate array store
399 *
Ben Chengba4fc8b2009-06-01 13:00:29 -0700400 */
Bill Buzbee270c1d62009-08-13 16:58:07 -0700401static void genArrayPut(CompilationUnit *cUnit, MIR *mir, OpSize size,
Bill Buzbee1465db52009-09-23 17:17:35 -0700402 RegLocation rlArray, RegLocation rlIndex,
403 RegLocation rlSrc, int scale)
Ben Chengba4fc8b2009-06-01 13:00:29 -0700404{
405 int lenOffset = offsetof(ArrayObject, length);
406 int dataOffset = offsetof(ArrayObject, contents);
407
Bill Buzbee1465db52009-09-23 17:17:35 -0700408 int regPtr;
409 rlArray = loadValue(cUnit, rlArray, kCoreReg);
410 rlIndex = loadValue(cUnit, rlIndex, kCoreReg);
Ben Chenge9695e52009-06-16 16:11:47 -0700411
Bill Buzbeec6f10662010-02-09 11:16:15 -0800412 if (dvmCompilerIsTemp(cUnit, rlArray.lowReg)) {
413 dvmCompilerClobber(cUnit, rlArray.lowReg);
Bill Buzbee1465db52009-09-23 17:17:35 -0700414 regPtr = rlArray.lowReg;
415 } else {
Bill Buzbeec6f10662010-02-09 11:16:15 -0800416 regPtr = dvmCompilerAllocTemp(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -0700417 genRegCopy(cUnit, regPtr, rlArray.lowReg);
418 }
Ben Chenge9695e52009-06-16 16:11:47 -0700419
Ben Cheng1efc9c52009-06-08 18:25:27 -0700420 /* null object? */
Ben Cheng4238ec22009-08-24 16:32:22 -0700421 ArmLIR * pcrLabel = NULL;
422
423 if (!(mir->OptimizationFlags & MIR_IGNORE_NULL_CHECK)) {
Bill Buzbee1465db52009-09-23 17:17:35 -0700424 pcrLabel = genNullCheck(cUnit, rlArray.sRegLow, rlArray.lowReg,
425 mir->offset, NULL);
Ben Cheng4238ec22009-08-24 16:32:22 -0700426 }
427
428 if (!(mir->OptimizationFlags & MIR_IGNORE_RANGE_CHECK)) {
Bill Buzbeec6f10662010-02-09 11:16:15 -0800429 int regLen = dvmCompilerAllocTemp(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -0700430 //NOTE: max live temps(4) here.
Ben Cheng4238ec22009-08-24 16:32:22 -0700431 /* Get len */
Bill Buzbee1465db52009-09-23 17:17:35 -0700432 loadWordDisp(cUnit, rlArray.lowReg, lenOffset, regLen);
433 /* regPtr -> array data */
434 opRegImm(cUnit, kOpAdd, regPtr, dataOffset);
435 genBoundsCheck(cUnit, rlIndex.lowReg, regLen, mir->offset,
436 pcrLabel);
Bill Buzbeec6f10662010-02-09 11:16:15 -0800437 dvmCompilerFreeTemp(cUnit, regLen);
Ben Cheng4238ec22009-08-24 16:32:22 -0700438 } else {
Bill Buzbee1465db52009-09-23 17:17:35 -0700439 /* regPtr -> array data */
440 opRegImm(cUnit, kOpAdd, regPtr, dataOffset);
Ben Cheng4238ec22009-08-24 16:32:22 -0700441 }
Bill Buzbee1465db52009-09-23 17:17:35 -0700442 /* at this point, regPtr points to array, 2 live temps */
Bill Buzbee1465db52009-09-23 17:17:35 -0700443 if ((size == kLong) || (size == kDouble)) {
444 //TODO: need specific wide routine that can handle fp regs
445 if (scale) {
Bill Buzbeec6f10662010-02-09 11:16:15 -0800446 int rNewIndex = dvmCompilerAllocTemp(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -0700447 opRegRegImm(cUnit, kOpLsl, rNewIndex, rlIndex.lowReg, scale);
448 opRegReg(cUnit, kOpAdd, regPtr, rNewIndex);
Bill Buzbeec6f10662010-02-09 11:16:15 -0800449 dvmCompilerFreeTemp(cUnit, rNewIndex);
Bill Buzbee1465db52009-09-23 17:17:35 -0700450 } else {
451 opRegReg(cUnit, kOpAdd, regPtr, rlIndex.lowReg);
452 }
453 rlSrc = loadValueWide(cUnit, rlSrc, kAnyReg);
Ben Cheng11d8f142010-03-24 15:24:19 -0700454
455 HEAP_ACCESS_SHADOW(true);
Bill Buzbee1465db52009-09-23 17:17:35 -0700456 storePair(cUnit, regPtr, rlSrc.lowReg, rlSrc.highReg);
Ben Cheng11d8f142010-03-24 15:24:19 -0700457 HEAP_ACCESS_SHADOW(false);
458
Bill Buzbeec6f10662010-02-09 11:16:15 -0800459 dvmCompilerFreeTemp(cUnit, regPtr);
Bill Buzbee270c1d62009-08-13 16:58:07 -0700460 } else {
Bill Buzbee1465db52009-09-23 17:17:35 -0700461 rlSrc = loadValue(cUnit, rlSrc, kAnyReg);
Ben Cheng11d8f142010-03-24 15:24:19 -0700462
463 HEAP_ACCESS_SHADOW(true);
Bill Buzbee1465db52009-09-23 17:17:35 -0700464 storeBaseIndexed(cUnit, regPtr, rlIndex.lowReg, rlSrc.lowReg,
465 scale, size);
Ben Cheng11d8f142010-03-24 15:24:19 -0700466 HEAP_ACCESS_SHADOW(false);
jeffhao9e45c0b2010-02-03 10:24:05 -0800467 }
Ben Chengba4fc8b2009-06-01 13:00:29 -0700468}
469
Bill Buzbeebe6534f2010-03-12 16:01:35 -0800470/*
471 * Generate array object store
472 * Must use explicit register allocation here because of
473 * call-out to dvmCanPutArrayElement
474 */
475static void genArrayObjectPut(CompilationUnit *cUnit, MIR *mir,
476 RegLocation rlArray, RegLocation rlIndex,
477 RegLocation rlSrc, int scale)
478{
479 int lenOffset = offsetof(ArrayObject, length);
480 int dataOffset = offsetof(ArrayObject, contents);
481
482 dvmCompilerFlushAllRegs(cUnit);
483
484 int regLen = r0;
485 int regPtr = r4PC; /* Preserved across call */
486 int regArray = r1;
487 int regIndex = r7; /* Preserved across call */
488
489 loadValueDirectFixed(cUnit, rlArray, regArray);
490 loadValueDirectFixed(cUnit, rlIndex, regIndex);
491
492 /* null object? */
493 ArmLIR * pcrLabel = NULL;
494
495 if (!(mir->OptimizationFlags & MIR_IGNORE_NULL_CHECK)) {
496 pcrLabel = genNullCheck(cUnit, rlArray.sRegLow, regArray,
497 mir->offset, NULL);
498 }
499
500 if (!(mir->OptimizationFlags & MIR_IGNORE_RANGE_CHECK)) {
501 /* Get len */
502 loadWordDisp(cUnit, regArray, lenOffset, regLen);
503 /* regPtr -> array data */
504 opRegRegImm(cUnit, kOpAdd, regPtr, regArray, dataOffset);
505 genBoundsCheck(cUnit, regIndex, regLen, mir->offset,
506 pcrLabel);
507 } else {
508 /* regPtr -> array data */
509 opRegRegImm(cUnit, kOpAdd, regPtr, regArray, dataOffset);
510 }
511
512 /* Get object to store */
513 loadValueDirectFixed(cUnit, rlSrc, r0);
Ben Chengbd1326d2010-04-02 15:04:53 -0700514 LOAD_FUNC_ADDR(cUnit, r2, (int)dvmCanPutArrayElement);
Bill Buzbeebe6534f2010-03-12 16:01:35 -0800515
516 /* Are we storing null? If so, avoid check */
517 opRegImm(cUnit, kOpCmp, r0, 0);
518 ArmLIR *branchOver = opCondBranch(cUnit, kArmCondEq);
519
520 /* Make sure the types are compatible */
521 loadWordDisp(cUnit, regArray, offsetof(Object, clazz), r1);
522 loadWordDisp(cUnit, r0, offsetof(Object, clazz), r0);
523 opReg(cUnit, kOpBlx, r2);
524 dvmCompilerClobberCallRegs(cUnit);
Bill Buzbee900a3af2010-03-16 12:41:43 -0700525
526 /*
527 * Using fixed registers here, and counting on r4 and r7 being
528 * preserved across the above call. Tell the register allocation
529 * utilities about the regs we are using directly
530 */
531 dvmCompilerLockTemp(cUnit, regPtr); // r4PC
532 dvmCompilerLockTemp(cUnit, regIndex); // r7
533 dvmCompilerLockTemp(cUnit, r0);
534
Bill Buzbeebe6534f2010-03-12 16:01:35 -0800535 /* Bad? - roll back and re-execute if so */
536 genRegImmCheck(cUnit, kArmCondEq, r0, 0, mir->offset, pcrLabel);
537
538 /* Resume here - must reload element, regPtr & index preserved */
539 loadValueDirectFixed(cUnit, rlSrc, r0);
540
541 ArmLIR *target = newLIR0(cUnit, kArmPseudoTargetLabel);
542 target->defMask = ENCODE_ALL;
543 branchOver->generic.target = (LIR *) target;
544
Ben Cheng11d8f142010-03-24 15:24:19 -0700545 HEAP_ACCESS_SHADOW(true);
Bill Buzbeebe6534f2010-03-12 16:01:35 -0800546 storeBaseIndexed(cUnit, regPtr, regIndex, r0,
547 scale, kWord);
Ben Cheng11d8f142010-03-24 15:24:19 -0700548 HEAP_ACCESS_SHADOW(false);
Bill Buzbeebe6534f2010-03-12 16:01:35 -0800549}
550
Ben Cheng5d90c202009-11-22 23:31:11 -0800551static bool genShiftOpLong(CompilationUnit *cUnit, MIR *mir,
552 RegLocation rlDest, RegLocation rlSrc1,
553 RegLocation rlShift)
Ben Chengba4fc8b2009-06-01 13:00:29 -0700554{
Ben Chenge9695e52009-06-16 16:11:47 -0700555 /*
556 * Don't mess with the regsiters here as there is a particular calling
557 * convention to the out-of-line handler.
558 */
Bill Buzbee1465db52009-09-23 17:17:35 -0700559 RegLocation rlResult;
560
561 loadValueDirectWideFixed(cUnit, rlSrc1, r0, r1);
562 loadValueDirect(cUnit, rlShift, r2);
Ben Chenge9695e52009-06-16 16:11:47 -0700563 switch( mir->dalvikInsn.opCode) {
564 case OP_SHL_LONG:
565 case OP_SHL_LONG_2ADDR:
566 genDispatchToHandler(cUnit, TEMPLATE_SHL_LONG);
567 break;
568 case OP_SHR_LONG:
569 case OP_SHR_LONG_2ADDR:
570 genDispatchToHandler(cUnit, TEMPLATE_SHR_LONG);
571 break;
572 case OP_USHR_LONG:
573 case OP_USHR_LONG_2ADDR:
574 genDispatchToHandler(cUnit, TEMPLATE_USHR_LONG);
575 break;
576 default:
577 return true;
578 }
Bill Buzbeec6f10662010-02-09 11:16:15 -0800579 rlResult = dvmCompilerGetReturnWide(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -0700580 storeValueWide(cUnit, rlDest, rlResult);
Ben Chenge9695e52009-06-16 16:11:47 -0700581 return false;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700582}
Ben Chenge9695e52009-06-16 16:11:47 -0700583
Ben Cheng5d90c202009-11-22 23:31:11 -0800584static bool genArithOpLong(CompilationUnit *cUnit, MIR *mir,
585 RegLocation rlDest, RegLocation rlSrc1,
586 RegLocation rlSrc2)
Ben Chengba4fc8b2009-06-01 13:00:29 -0700587{
Bill Buzbee1465db52009-09-23 17:17:35 -0700588 RegLocation rlResult;
589 OpKind firstOp = kOpBkpt;
590 OpKind secondOp = kOpBkpt;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700591 bool callOut = false;
592 void *callTgt;
593 int retReg = r0;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700594
595 switch (mir->dalvikInsn.opCode) {
596 case OP_NOT_LONG:
Bill Buzbee1465db52009-09-23 17:17:35 -0700597 rlSrc2 = loadValueWide(cUnit, rlSrc2, kCoreReg);
Bill Buzbeec6f10662010-02-09 11:16:15 -0800598 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Bill Buzbee1465db52009-09-23 17:17:35 -0700599 opRegReg(cUnit, kOpMvn, rlResult.lowReg, rlSrc2.lowReg);
600 opRegReg(cUnit, kOpMvn, rlResult.highReg, rlSrc2.highReg);
601 storeValueWide(cUnit, rlDest, rlResult);
602 return false;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700603 break;
604 case OP_ADD_LONG:
605 case OP_ADD_LONG_2ADDR:
Bill Buzbee1465db52009-09-23 17:17:35 -0700606 firstOp = kOpAdd;
607 secondOp = kOpAdc;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700608 break;
609 case OP_SUB_LONG:
610 case OP_SUB_LONG_2ADDR:
Bill Buzbee1465db52009-09-23 17:17:35 -0700611 firstOp = kOpSub;
612 secondOp = kOpSbc;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700613 break;
614 case OP_MUL_LONG:
615 case OP_MUL_LONG_2ADDR:
Bill Buzbee1465db52009-09-23 17:17:35 -0700616 genMulLong(cUnit, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700617 return false;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700618 case OP_DIV_LONG:
619 case OP_DIV_LONG_2ADDR:
620 callOut = true;
621 retReg = r0;
622 callTgt = (void*)__aeabi_ldivmod;
623 break;
624 /* NOTE - result is in r2/r3 instead of r0/r1 */
625 case OP_REM_LONG:
626 case OP_REM_LONG_2ADDR:
627 callOut = true;
628 callTgt = (void*)__aeabi_ldivmod;
629 retReg = r2;
630 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700631 case OP_AND_LONG_2ADDR:
Bill Buzbee1465db52009-09-23 17:17:35 -0700632 case OP_AND_LONG:
633 firstOp = kOpAnd;
634 secondOp = kOpAnd;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700635 break;
636 case OP_OR_LONG:
637 case OP_OR_LONG_2ADDR:
Bill Buzbee1465db52009-09-23 17:17:35 -0700638 firstOp = kOpOr;
639 secondOp = kOpOr;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700640 break;
641 case OP_XOR_LONG:
642 case OP_XOR_LONG_2ADDR:
Bill Buzbee1465db52009-09-23 17:17:35 -0700643 firstOp = kOpXor;
644 secondOp = kOpXor;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700645 break;
Ben Chenge9695e52009-06-16 16:11:47 -0700646 case OP_NEG_LONG: {
Bill Buzbee51ecf602010-01-14 14:27:52 -0800647 //TUNING: can improve this using Thumb2 code
Bill Buzbeec6f10662010-02-09 11:16:15 -0800648 int tReg = dvmCompilerAllocTemp(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -0700649 rlSrc2 = loadValueWide(cUnit, rlSrc2, kCoreReg);
Bill Buzbeec6f10662010-02-09 11:16:15 -0800650 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Ben Chengbd1326d2010-04-02 15:04:53 -0700651 loadConstantNoClobber(cUnit, tReg, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -0700652 opRegRegReg(cUnit, kOpSub, rlResult.lowReg,
Bill Buzbee51ecf602010-01-14 14:27:52 -0800653 tReg, rlSrc2.lowReg);
654 opRegReg(cUnit, kOpSbc, tReg, rlSrc2.highReg);
655 genRegCopy(cUnit, rlResult.highReg, tReg);
Bill Buzbee1465db52009-09-23 17:17:35 -0700656 storeValueWide(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700657 return false;
Ben Chenge9695e52009-06-16 16:11:47 -0700658 }
Ben Chengba4fc8b2009-06-01 13:00:29 -0700659 default:
660 LOGE("Invalid long arith op");
Bill Buzbeefc519dc2010-03-06 23:30:57 -0800661 dvmCompilerAbort(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700662 }
663 if (!callOut) {
Bill Buzbee80cef862010-03-25 10:38:34 -0700664 genLong3Addr(cUnit, mir, firstOp, secondOp, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700665 } else {
Bill Buzbee1465db52009-09-23 17:17:35 -0700666 // Adjust return regs in to handle case of rem returning r2/r3
Bill Buzbeec6f10662010-02-09 11:16:15 -0800667 dvmCompilerFlushAllRegs(cUnit); /* Send everything to home location */
Bill Buzbee1465db52009-09-23 17:17:35 -0700668 loadValueDirectWideFixed(cUnit, rlSrc1, r0, r1);
Ben Chengbd1326d2010-04-02 15:04:53 -0700669 LOAD_FUNC_ADDR(cUnit, rlr, (int) callTgt);
Bill Buzbee1465db52009-09-23 17:17:35 -0700670 loadValueDirectWideFixed(cUnit, rlSrc2, r2, r3);
671 opReg(cUnit, kOpBlx, rlr);
Elliott Hughes6a555132010-02-25 15:41:42 -0800672 dvmCompilerClobberCallRegs(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -0700673 if (retReg == r0)
Bill Buzbeec6f10662010-02-09 11:16:15 -0800674 rlResult = dvmCompilerGetReturnWide(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -0700675 else
Bill Buzbeec6f10662010-02-09 11:16:15 -0800676 rlResult = dvmCompilerGetReturnWideAlt(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -0700677 storeValueWide(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700678 }
679 return false;
680}
681
Ben Cheng5d90c202009-11-22 23:31:11 -0800682static bool genArithOpInt(CompilationUnit *cUnit, MIR *mir,
683 RegLocation rlDest, RegLocation rlSrc1,
684 RegLocation rlSrc2)
Ben Chengba4fc8b2009-06-01 13:00:29 -0700685{
Bill Buzbee1465db52009-09-23 17:17:35 -0700686 OpKind op = kOpBkpt;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700687 bool callOut = false;
688 bool checkZero = false;
Bill Buzbee1465db52009-09-23 17:17:35 -0700689 bool unary = false;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700690 int retReg = r0;
691 void *callTgt;
Bill Buzbee1465db52009-09-23 17:17:35 -0700692 RegLocation rlResult;
Bill Buzbee0e605272009-12-01 14:28:05 -0800693 bool shiftOp = false;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700694
Ben Chengba4fc8b2009-06-01 13:00:29 -0700695 switch (mir->dalvikInsn.opCode) {
696 case OP_NEG_INT:
Bill Buzbee1465db52009-09-23 17:17:35 -0700697 op = kOpNeg;
698 unary = true;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700699 break;
700 case OP_NOT_INT:
Bill Buzbee1465db52009-09-23 17:17:35 -0700701 op = kOpMvn;
702 unary = true;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700703 break;
704 case OP_ADD_INT:
705 case OP_ADD_INT_2ADDR:
Bill Buzbee1465db52009-09-23 17:17:35 -0700706 op = kOpAdd;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700707 break;
708 case OP_SUB_INT:
709 case OP_SUB_INT_2ADDR:
Bill Buzbee1465db52009-09-23 17:17:35 -0700710 op = kOpSub;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700711 break;
712 case OP_MUL_INT:
713 case OP_MUL_INT_2ADDR:
Bill Buzbee1465db52009-09-23 17:17:35 -0700714 op = kOpMul;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700715 break;
716 case OP_DIV_INT:
717 case OP_DIV_INT_2ADDR:
718 callOut = true;
719 checkZero = true;
720 callTgt = __aeabi_idiv;
721 retReg = r0;
722 break;
723 /* NOTE: returns in r1 */
724 case OP_REM_INT:
725 case OP_REM_INT_2ADDR:
726 callOut = true;
727 checkZero = true;
728 callTgt = __aeabi_idivmod;
729 retReg = r1;
730 break;
731 case OP_AND_INT:
732 case OP_AND_INT_2ADDR:
Bill Buzbee1465db52009-09-23 17:17:35 -0700733 op = kOpAnd;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700734 break;
735 case OP_OR_INT:
736 case OP_OR_INT_2ADDR:
Bill Buzbee1465db52009-09-23 17:17:35 -0700737 op = kOpOr;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700738 break;
739 case OP_XOR_INT:
740 case OP_XOR_INT_2ADDR:
Bill Buzbee1465db52009-09-23 17:17:35 -0700741 op = kOpXor;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700742 break;
743 case OP_SHL_INT:
744 case OP_SHL_INT_2ADDR:
Bill Buzbee0e605272009-12-01 14:28:05 -0800745 shiftOp = true;
Bill Buzbee1465db52009-09-23 17:17:35 -0700746 op = kOpLsl;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700747 break;
748 case OP_SHR_INT:
749 case OP_SHR_INT_2ADDR:
Bill Buzbee0e605272009-12-01 14:28:05 -0800750 shiftOp = true;
Bill Buzbee1465db52009-09-23 17:17:35 -0700751 op = kOpAsr;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700752 break;
753 case OP_USHR_INT:
754 case OP_USHR_INT_2ADDR:
Bill Buzbee0e605272009-12-01 14:28:05 -0800755 shiftOp = true;
Bill Buzbee1465db52009-09-23 17:17:35 -0700756 op = kOpLsr;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700757 break;
758 default:
759 LOGE("Invalid word arith op: 0x%x(%d)",
760 mir->dalvikInsn.opCode, mir->dalvikInsn.opCode);
Bill Buzbeefc519dc2010-03-06 23:30:57 -0800761 dvmCompilerAbort(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700762 }
763 if (!callOut) {
Bill Buzbee1465db52009-09-23 17:17:35 -0700764 rlSrc1 = loadValue(cUnit, rlSrc1, kCoreReg);
765 if (unary) {
Bill Buzbeec6f10662010-02-09 11:16:15 -0800766 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Bill Buzbee1465db52009-09-23 17:17:35 -0700767 opRegReg(cUnit, op, rlResult.lowReg,
768 rlSrc1.lowReg);
Ben Chenge9695e52009-06-16 16:11:47 -0700769 } else {
Bill Buzbee1465db52009-09-23 17:17:35 -0700770 rlSrc2 = loadValue(cUnit, rlSrc2, kCoreReg);
Bill Buzbee0e605272009-12-01 14:28:05 -0800771 if (shiftOp) {
Bill Buzbeec6f10662010-02-09 11:16:15 -0800772 int tReg = dvmCompilerAllocTemp(cUnit);
Bill Buzbee0e605272009-12-01 14:28:05 -0800773 opRegRegImm(cUnit, kOpAnd, tReg, rlSrc2.lowReg, 31);
Bill Buzbeec6f10662010-02-09 11:16:15 -0800774 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Bill Buzbee0e605272009-12-01 14:28:05 -0800775 opRegRegReg(cUnit, op, rlResult.lowReg,
776 rlSrc1.lowReg, tReg);
Bill Buzbeec6f10662010-02-09 11:16:15 -0800777 dvmCompilerFreeTemp(cUnit, tReg);
Bill Buzbee0e605272009-12-01 14:28:05 -0800778 } else {
Bill Buzbeec6f10662010-02-09 11:16:15 -0800779 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Bill Buzbee0e605272009-12-01 14:28:05 -0800780 opRegRegReg(cUnit, op, rlResult.lowReg,
781 rlSrc1.lowReg, rlSrc2.lowReg);
782 }
Ben Chenge9695e52009-06-16 16:11:47 -0700783 }
Bill Buzbee1465db52009-09-23 17:17:35 -0700784 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700785 } else {
Bill Buzbee1465db52009-09-23 17:17:35 -0700786 RegLocation rlResult;
Bill Buzbeec6f10662010-02-09 11:16:15 -0800787 dvmCompilerFlushAllRegs(cUnit); /* Send everything to home location */
Bill Buzbee1465db52009-09-23 17:17:35 -0700788 loadValueDirectFixed(cUnit, rlSrc2, r1);
Ben Chengbd1326d2010-04-02 15:04:53 -0700789 LOAD_FUNC_ADDR(cUnit, r2, (int) callTgt);
Bill Buzbee1465db52009-09-23 17:17:35 -0700790 loadValueDirectFixed(cUnit, rlSrc1, r0);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700791 if (checkZero) {
Bill Buzbee1465db52009-09-23 17:17:35 -0700792 genNullCheck(cUnit, rlSrc2.sRegLow, r1, mir->offset, NULL);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700793 }
Bill Buzbee1465db52009-09-23 17:17:35 -0700794 opReg(cUnit, kOpBlx, r2);
Elliott Hughes6a555132010-02-25 15:41:42 -0800795 dvmCompilerClobberCallRegs(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -0700796 if (retReg == r0)
Bill Buzbeec6f10662010-02-09 11:16:15 -0800797 rlResult = dvmCompilerGetReturn(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -0700798 else
Bill Buzbeec6f10662010-02-09 11:16:15 -0800799 rlResult = dvmCompilerGetReturnAlt(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -0700800 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700801 }
802 return false;
803}
804
Ben Cheng5d90c202009-11-22 23:31:11 -0800805static bool genArithOp(CompilationUnit *cUnit, MIR *mir)
Ben Chengba4fc8b2009-06-01 13:00:29 -0700806{
807 OpCode opCode = mir->dalvikInsn.opCode;
Bill Buzbee1465db52009-09-23 17:17:35 -0700808 RegLocation rlDest;
809 RegLocation rlSrc1;
810 RegLocation rlSrc2;
811 /* Deduce sizes of operands */
812 if (mir->ssaRep->numUses == 2) {
Bill Buzbeec6f10662010-02-09 11:16:15 -0800813 rlSrc1 = dvmCompilerGetSrc(cUnit, mir, 0);
814 rlSrc2 = dvmCompilerGetSrc(cUnit, mir, 1);
Bill Buzbee1465db52009-09-23 17:17:35 -0700815 } else if (mir->ssaRep->numUses == 3) {
Bill Buzbeec6f10662010-02-09 11:16:15 -0800816 rlSrc1 = dvmCompilerGetSrcWide(cUnit, mir, 0, 1);
817 rlSrc2 = dvmCompilerGetSrc(cUnit, mir, 2);
Bill Buzbee1465db52009-09-23 17:17:35 -0700818 } else {
Bill Buzbeec6f10662010-02-09 11:16:15 -0800819 rlSrc1 = dvmCompilerGetSrcWide(cUnit, mir, 0, 1);
820 rlSrc2 = dvmCompilerGetSrcWide(cUnit, mir, 2, 3);
Bill Buzbee1465db52009-09-23 17:17:35 -0700821 assert(mir->ssaRep->numUses == 4);
822 }
823 if (mir->ssaRep->numDefs == 1) {
Bill Buzbeec6f10662010-02-09 11:16:15 -0800824 rlDest = dvmCompilerGetDest(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -0700825 } else {
826 assert(mir->ssaRep->numDefs == 2);
Bill Buzbeec6f10662010-02-09 11:16:15 -0800827 rlDest = dvmCompilerGetDestWide(cUnit, mir, 0, 1);
Bill Buzbee1465db52009-09-23 17:17:35 -0700828 }
Ben Chengba4fc8b2009-06-01 13:00:29 -0700829
830 if ((opCode >= OP_ADD_LONG_2ADDR) && (opCode <= OP_XOR_LONG_2ADDR)) {
Ben Cheng5d90c202009-11-22 23:31:11 -0800831 return genArithOpLong(cUnit,mir, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700832 }
833 if ((opCode >= OP_ADD_LONG) && (opCode <= OP_XOR_LONG)) {
Ben Cheng5d90c202009-11-22 23:31:11 -0800834 return genArithOpLong(cUnit,mir, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700835 }
836 if ((opCode >= OP_SHL_LONG_2ADDR) && (opCode <= OP_USHR_LONG_2ADDR)) {
Ben Cheng5d90c202009-11-22 23:31:11 -0800837 return genShiftOpLong(cUnit,mir, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700838 }
839 if ((opCode >= OP_SHL_LONG) && (opCode <= OP_USHR_LONG)) {
Ben Cheng5d90c202009-11-22 23:31:11 -0800840 return genShiftOpLong(cUnit,mir, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700841 }
842 if ((opCode >= OP_ADD_INT_2ADDR) && (opCode <= OP_USHR_INT_2ADDR)) {
Ben Cheng5d90c202009-11-22 23:31:11 -0800843 return genArithOpInt(cUnit,mir, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700844 }
845 if ((opCode >= OP_ADD_INT) && (opCode <= OP_USHR_INT)) {
Ben Cheng5d90c202009-11-22 23:31:11 -0800846 return genArithOpInt(cUnit,mir, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700847 }
848 if ((opCode >= OP_ADD_FLOAT_2ADDR) && (opCode <= OP_REM_FLOAT_2ADDR)) {
Ben Cheng5d90c202009-11-22 23:31:11 -0800849 return genArithOpFloat(cUnit,mir, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700850 }
851 if ((opCode >= OP_ADD_FLOAT) && (opCode <= OP_REM_FLOAT)) {
Ben Cheng5d90c202009-11-22 23:31:11 -0800852 return genArithOpFloat(cUnit, mir, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700853 }
854 if ((opCode >= OP_ADD_DOUBLE_2ADDR) && (opCode <= OP_REM_DOUBLE_2ADDR)) {
Ben Cheng5d90c202009-11-22 23:31:11 -0800855 return genArithOpDouble(cUnit,mir, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700856 }
857 if ((opCode >= OP_ADD_DOUBLE) && (opCode <= OP_REM_DOUBLE)) {
Ben Cheng5d90c202009-11-22 23:31:11 -0800858 return genArithOpDouble(cUnit,mir, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700859 }
860 return true;
861}
862
Bill Buzbee1465db52009-09-23 17:17:35 -0700863/* Generate unconditional branch instructions */
864static ArmLIR *genUnconditionalBranch(CompilationUnit *cUnit, ArmLIR *target)
865{
866 ArmLIR *branch = opNone(cUnit, kOpUncondBr);
867 branch->generic.target = (LIR *) target;
868 return branch;
869}
870
Bill Buzbee1465db52009-09-23 17:17:35 -0700871/* Perform the actual operation for OP_RETURN_* */
872static void genReturnCommon(CompilationUnit *cUnit, MIR *mir)
873{
874 genDispatchToHandler(cUnit, TEMPLATE_RETURN);
Ben Cheng86717f72010-03-05 15:27:21 -0800875#if defined(JIT_STATS)
Bill Buzbee1465db52009-09-23 17:17:35 -0700876 gDvmJit.returnOp++;
877#endif
878 int dPC = (int) (cUnit->method->insns + mir->offset);
879 /* Insert branch, but defer setting of target */
880 ArmLIR *branch = genUnconditionalBranch(cUnit, NULL);
881 /* Set up the place holder to reconstruct this Dalvik PC */
882 ArmLIR *pcrLabel = dvmCompilerNew(sizeof(ArmLIR), true);
Ben Chenga4973592010-03-31 11:59:18 -0700883 pcrLabel->opCode = kArmPseudoPCReconstructionCell;
Bill Buzbee1465db52009-09-23 17:17:35 -0700884 pcrLabel->operands[0] = dPC;
885 pcrLabel->operands[1] = mir->offset;
886 /* Insert the place holder to the growable list */
887 dvmInsertGrowableList(&cUnit->pcReconstructionList, pcrLabel);
888 /* Branch to the PC reconstruction code */
889 branch->generic.target = (LIR *) pcrLabel;
890}
891
Ben Chengba4fc8b2009-06-01 13:00:29 -0700892static void genProcessArgsNoRange(CompilationUnit *cUnit, MIR *mir,
893 DecodedInstruction *dInsn,
Bill Buzbee89efc3d2009-07-28 11:22:22 -0700894 ArmLIR **pcrLabel)
Ben Chengba4fc8b2009-06-01 13:00:29 -0700895{
896 unsigned int i;
897 unsigned int regMask = 0;
Bill Buzbee1465db52009-09-23 17:17:35 -0700898 RegLocation rlArg;
899 int numDone = 0;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700900
Bill Buzbee1465db52009-09-23 17:17:35 -0700901 /*
902 * Load arguments to r0..r4. Note that these registers may contain
903 * live values, so we clobber them immediately after loading to prevent
904 * them from being used as sources for subsequent loads.
905 */
Bill Buzbeec6f10662010-02-09 11:16:15 -0800906 dvmCompilerLockAllTemps(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700907 for (i = 0; i < dInsn->vA; i++) {
908 regMask |= 1 << i;
Bill Buzbeec6f10662010-02-09 11:16:15 -0800909 rlArg = dvmCompilerGetSrc(cUnit, mir, numDone++);
Bill Buzbee1465db52009-09-23 17:17:35 -0700910 loadValueDirectFixed(cUnit, rlArg, i);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700911 }
912 if (regMask) {
913 /* Up to 5 args are pushed on top of FP - sizeofStackSaveArea */
Bill Buzbee1465db52009-09-23 17:17:35 -0700914 opRegRegImm(cUnit, kOpSub, r7, rFP,
915 sizeof(StackSaveArea) + (dInsn->vA << 2));
Ben Chengba4fc8b2009-06-01 13:00:29 -0700916 /* generate null check */
917 if (pcrLabel) {
Bill Buzbeec6f10662010-02-09 11:16:15 -0800918 *pcrLabel = genNullCheck(cUnit, dvmCompilerSSASrc(mir, 0), r0,
Bill Buzbee1465db52009-09-23 17:17:35 -0700919 mir->offset, NULL);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700920 }
Bill Buzbee270c1d62009-08-13 16:58:07 -0700921 storeMultiple(cUnit, r7, regMask);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700922 }
923}
924
925static void genProcessArgsRange(CompilationUnit *cUnit, MIR *mir,
926 DecodedInstruction *dInsn,
Bill Buzbee89efc3d2009-07-28 11:22:22 -0700927 ArmLIR **pcrLabel)
Ben Chengba4fc8b2009-06-01 13:00:29 -0700928{
929 int srcOffset = dInsn->vC << 2;
930 int numArgs = dInsn->vA;
931 int regMask;
Bill Buzbee1465db52009-09-23 17:17:35 -0700932
933 /*
934 * Note: here, all promoted registers will have been flushed
935 * back to the Dalvik base locations, so register usage restrictins
936 * are lifted. All parms loaded from original Dalvik register
937 * region - even though some might conceivably have valid copies
938 * cached in a preserved register.
939 */
Bill Buzbeec6f10662010-02-09 11:16:15 -0800940 dvmCompilerLockAllTemps(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -0700941
Ben Chengba4fc8b2009-06-01 13:00:29 -0700942 /*
943 * r4PC : &rFP[vC]
944 * r7: &newFP[0]
945 */
Bill Buzbee1465db52009-09-23 17:17:35 -0700946 opRegRegImm(cUnit, kOpAdd, r4PC, rFP, srcOffset);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700947 /* load [r0 .. min(numArgs,4)] */
948 regMask = (1 << ((numArgs < 4) ? numArgs : 4)) - 1;
Ben Chengd7d426a2009-09-22 11:23:36 -0700949 /*
950 * Protect the loadMultiple instruction from being reordered with other
951 * Dalvik stack accesses.
952 */
Bill Buzbee270c1d62009-08-13 16:58:07 -0700953 loadMultiple(cUnit, r4PC, regMask);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700954
Bill Buzbee1465db52009-09-23 17:17:35 -0700955 opRegRegImm(cUnit, kOpSub, r7, rFP,
956 sizeof(StackSaveArea) + (numArgs << 2));
Ben Chengba4fc8b2009-06-01 13:00:29 -0700957 /* generate null check */
958 if (pcrLabel) {
Bill Buzbeec6f10662010-02-09 11:16:15 -0800959 *pcrLabel = genNullCheck(cUnit, dvmCompilerSSASrc(mir, 0), r0,
Bill Buzbee1465db52009-09-23 17:17:35 -0700960 mir->offset, NULL);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700961 }
962
963 /*
964 * Handle remaining 4n arguments:
965 * store previously loaded 4 values and load the next 4 values
966 */
967 if (numArgs >= 8) {
Bill Buzbee89efc3d2009-07-28 11:22:22 -0700968 ArmLIR *loopLabel = NULL;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700969 /*
970 * r0 contains "this" and it will be used later, so push it to the stack
Bill Buzbee270c1d62009-08-13 16:58:07 -0700971 * first. Pushing r5 (rFP) is just for stack alignment purposes.
Ben Chengba4fc8b2009-06-01 13:00:29 -0700972 */
Bill Buzbee1465db52009-09-23 17:17:35 -0700973 opImm(cUnit, kOpPush, (1 << r0 | 1 << rFP));
Ben Chengba4fc8b2009-06-01 13:00:29 -0700974 /* No need to generate the loop structure if numArgs <= 11 */
975 if (numArgs > 11) {
976 loadConstant(cUnit, 5, ((numArgs - 4) >> 2) << 2);
Bill Buzbee1465db52009-09-23 17:17:35 -0700977 loopLabel = newLIR0(cUnit, kArmPseudoTargetLabel);
Ben Chengd7d426a2009-09-22 11:23:36 -0700978 loopLabel->defMask = ENCODE_ALL;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700979 }
Bill Buzbee270c1d62009-08-13 16:58:07 -0700980 storeMultiple(cUnit, r7, regMask);
Ben Chengd7d426a2009-09-22 11:23:36 -0700981 /*
982 * Protect the loadMultiple instruction from being reordered with other
983 * Dalvik stack accesses.
984 */
Bill Buzbee270c1d62009-08-13 16:58:07 -0700985 loadMultiple(cUnit, r4PC, regMask);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700986 /* No need to generate the loop structure if numArgs <= 11 */
987 if (numArgs > 11) {
Bill Buzbee1465db52009-09-23 17:17:35 -0700988 opRegImm(cUnit, kOpSub, rFP, 4);
989 genConditionalBranch(cUnit, kArmCondNe, loopLabel);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700990 }
991 }
992
993 /* Save the last batch of loaded values */
Bill Buzbee270c1d62009-08-13 16:58:07 -0700994 storeMultiple(cUnit, r7, regMask);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700995
996 /* Generate the loop epilogue - don't use r0 */
997 if ((numArgs > 4) && (numArgs % 4)) {
998 regMask = ((1 << (numArgs & 0x3)) - 1) << 1;
Ben Chengd7d426a2009-09-22 11:23:36 -0700999 /*
1000 * Protect the loadMultiple instruction from being reordered with other
1001 * Dalvik stack accesses.
1002 */
Bill Buzbee270c1d62009-08-13 16:58:07 -07001003 loadMultiple(cUnit, r4PC, regMask);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001004 }
1005 if (numArgs >= 8)
Bill Buzbee1465db52009-09-23 17:17:35 -07001006 opImm(cUnit, kOpPop, (1 << r0 | 1 << rFP));
Ben Chengba4fc8b2009-06-01 13:00:29 -07001007
1008 /* Save the modulo 4 arguments */
1009 if ((numArgs > 4) && (numArgs % 4)) {
Bill Buzbee270c1d62009-08-13 16:58:07 -07001010 storeMultiple(cUnit, r7, regMask);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001011 }
1012}
1013
Ben Cheng38329f52009-07-07 14:19:20 -07001014/*
1015 * Generate code to setup the call stack then jump to the chaining cell if it
1016 * is not a native method.
1017 */
1018static void genInvokeSingletonCommon(CompilationUnit *cUnit, MIR *mir,
Bill Buzbee89efc3d2009-07-28 11:22:22 -07001019 BasicBlock *bb, ArmLIR *labelList,
1020 ArmLIR *pcrLabel,
Ben Cheng38329f52009-07-07 14:19:20 -07001021 const Method *calleeMethod)
Ben Chengba4fc8b2009-06-01 13:00:29 -07001022{
Bill Buzbee1465db52009-09-23 17:17:35 -07001023 /*
1024 * Note: all Dalvik register state should be flushed to
1025 * memory by the point, so register usage restrictions no
1026 * longer apply. All temp & preserved registers may be used.
1027 */
Bill Buzbeec6f10662010-02-09 11:16:15 -08001028 dvmCompilerLockAllTemps(cUnit);
Bill Buzbee89efc3d2009-07-28 11:22:22 -07001029 ArmLIR *retChainingCell = &labelList[bb->fallThrough->id];
Ben Chengba4fc8b2009-06-01 13:00:29 -07001030
1031 /* r1 = &retChainingCell */
Bill Buzbeec6f10662010-02-09 11:16:15 -08001032 dvmCompilerLockTemp(cUnit, r1);
Bill Buzbee1465db52009-09-23 17:17:35 -07001033 ArmLIR *addrRetChain = opRegRegImm(cUnit, kOpAdd, r1, rpc, 0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001034 /* r4PC = dalvikCallsite */
1035 loadConstant(cUnit, r4PC,
1036 (int) (cUnit->method->insns + mir->offset));
1037 addrRetChain->generic.target = (LIR *) retChainingCell;
1038 /*
Ben Cheng38329f52009-07-07 14:19:20 -07001039 * r0 = calleeMethod (loaded upon calling genInvokeSingletonCommon)
Ben Chengba4fc8b2009-06-01 13:00:29 -07001040 * r1 = &ChainingCell
1041 * r4PC = callsiteDPC
1042 */
1043 if (dvmIsNativeMethod(calleeMethod)) {
Ben Cheng38329f52009-07-07 14:19:20 -07001044 genDispatchToHandler(cUnit, TEMPLATE_INVOKE_METHOD_NATIVE);
Ben Cheng86717f72010-03-05 15:27:21 -08001045#if defined(JIT_STATS)
Ben Cheng38329f52009-07-07 14:19:20 -07001046 gDvmJit.invokeNative++;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001047#endif
1048 } else {
1049 genDispatchToHandler(cUnit, TEMPLATE_INVOKE_METHOD_CHAIN);
Ben Cheng86717f72010-03-05 15:27:21 -08001050#if defined(JIT_STATS)
1051 gDvmJit.invokeMonomorphic++;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001052#endif
Ben Cheng38329f52009-07-07 14:19:20 -07001053 /* Branch to the chaining cell */
Ben Chengba4fc8b2009-06-01 13:00:29 -07001054 genUnconditionalBranch(cUnit, &labelList[bb->taken->id]);
1055 }
1056 /* Handle exceptions using the interpreter */
1057 genTrap(cUnit, mir->offset, pcrLabel);
1058}
1059
Ben Cheng38329f52009-07-07 14:19:20 -07001060/*
1061 * Generate code to check the validity of a predicted chain and take actions
1062 * based on the result.
1063 *
1064 * 0x426a99aa : ldr r4, [pc, #72] --> r4 <- dalvikPC of this invoke
1065 * 0x426a99ac : add r1, pc, #32 --> r1 <- &retChainingCell
1066 * 0x426a99ae : add r2, pc, #40 --> r2 <- &predictedChainingCell
1067 * 0x426a99b0 : blx_1 0x426a918c --+ TEMPLATE_INVOKE_METHOD_PREDICTED_CHAIN
1068 * 0x426a99b2 : blx_2 see above --+
1069 * 0x426a99b4 : b 0x426a99d8 --> off to the predicted chain
1070 * 0x426a99b6 : b 0x426a99c8 --> punt to the interpreter
1071 * 0x426a99b8 : ldr r0, [r7, #44] --> r0 <- this->class->vtable[methodIdx]
1072 * 0x426a99ba : cmp r1, #0 --> compare r1 (rechain count) against 0
1073 * 0x426a99bc : bgt 0x426a99c2 --> >=0? don't rechain
1074 * 0x426a99be : ldr r7, [r6, #96] --+ dvmJitToPatchPredictedChain
1075 * 0x426a99c0 : blx r7 --+
1076 * 0x426a99c2 : add r1, pc, #12 --> r1 <- &retChainingCell
1077 * 0x426a99c4 : blx_1 0x426a9098 --+ TEMPLATE_INVOKE_METHOD_NO_OPT
1078 * 0x426a99c6 : blx_2 see above --+
1079 */
1080static void genInvokeVirtualCommon(CompilationUnit *cUnit, MIR *mir,
1081 int methodIndex,
Bill Buzbee89efc3d2009-07-28 11:22:22 -07001082 ArmLIR *retChainingCell,
1083 ArmLIR *predChainingCell,
1084 ArmLIR *pcrLabel)
Ben Cheng38329f52009-07-07 14:19:20 -07001085{
Bill Buzbee1465db52009-09-23 17:17:35 -07001086 /*
1087 * Note: all Dalvik register state should be flushed to
1088 * memory by the point, so register usage restrictions no
1089 * longer apply. Lock temps to prevent them from being
1090 * allocated by utility routines.
1091 */
Bill Buzbeec6f10662010-02-09 11:16:15 -08001092 dvmCompilerLockAllTemps(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07001093
Ben Cheng38329f52009-07-07 14:19:20 -07001094 /* "this" is already left in r0 by genProcessArgs* */
1095
1096 /* r4PC = dalvikCallsite */
1097 loadConstant(cUnit, r4PC,
1098 (int) (cUnit->method->insns + mir->offset));
1099
1100 /* r1 = &retChainingCell */
Bill Buzbee1465db52009-09-23 17:17:35 -07001101 ArmLIR *addrRetChain = opRegRegImm(cUnit, kOpAdd, r1, rpc, 0);
Ben Cheng38329f52009-07-07 14:19:20 -07001102 addrRetChain->generic.target = (LIR *) retChainingCell;
1103
1104 /* r2 = &predictedChainingCell */
Bill Buzbee1465db52009-09-23 17:17:35 -07001105 ArmLIR *predictedChainingCell = opRegRegImm(cUnit, kOpAdd, r2, rpc, 0);
Ben Cheng38329f52009-07-07 14:19:20 -07001106 predictedChainingCell->generic.target = (LIR *) predChainingCell;
1107
1108 genDispatchToHandler(cUnit, TEMPLATE_INVOKE_METHOD_PREDICTED_CHAIN);
1109
1110 /* return through lr - jump to the chaining cell */
1111 genUnconditionalBranch(cUnit, predChainingCell);
1112
1113 /*
1114 * null-check on "this" may have been eliminated, but we still need a PC-
1115 * reconstruction label for stack overflow bailout.
1116 */
1117 if (pcrLabel == NULL) {
1118 int dPC = (int) (cUnit->method->insns + mir->offset);
Bill Buzbee89efc3d2009-07-28 11:22:22 -07001119 pcrLabel = dvmCompilerNew(sizeof(ArmLIR), true);
Ben Chenga4973592010-03-31 11:59:18 -07001120 pcrLabel->opCode = kArmPseudoPCReconstructionCell;
Ben Cheng38329f52009-07-07 14:19:20 -07001121 pcrLabel->operands[0] = dPC;
1122 pcrLabel->operands[1] = mir->offset;
1123 /* Insert the place holder to the growable list */
1124 dvmInsertGrowableList(&cUnit->pcReconstructionList, pcrLabel);
1125 }
1126
1127 /* return through lr+2 - punt to the interpreter */
1128 genUnconditionalBranch(cUnit, pcrLabel);
1129
1130 /*
1131 * return through lr+4 - fully resolve the callee method.
1132 * r1 <- count
1133 * r2 <- &predictedChainCell
1134 * r3 <- this->class
1135 * r4 <- dPC
1136 * r7 <- this->class->vtable
1137 */
1138
1139 /* r0 <- calleeMethod */
Bill Buzbee270c1d62009-08-13 16:58:07 -07001140 loadWordDisp(cUnit, r7, methodIndex * 4, r0);
Ben Cheng38329f52009-07-07 14:19:20 -07001141
1142 /* Check if rechain limit is reached */
Bill Buzbee1465db52009-09-23 17:17:35 -07001143 opRegImm(cUnit, kOpCmp, r1, 0);
Ben Cheng38329f52009-07-07 14:19:20 -07001144
Bill Buzbee1465db52009-09-23 17:17:35 -07001145 ArmLIR *bypassRechaining = opCondBranch(cUnit, kArmCondGt);
Ben Cheng38329f52009-07-07 14:19:20 -07001146
Bill Buzbee270c1d62009-08-13 16:58:07 -07001147 loadWordDisp(cUnit, rGLUE, offsetof(InterpState,
1148 jitToInterpEntries.dvmJitToPatchPredictedChain), r7);
Ben Cheng38329f52009-07-07 14:19:20 -07001149
1150 /*
1151 * r0 = calleeMethod
1152 * r2 = &predictedChainingCell
1153 * r3 = class
1154 *
1155 * &returnChainingCell has been loaded into r1 but is not needed
1156 * when patching the chaining cell and will be clobbered upon
1157 * returning so it will be reconstructed again.
1158 */
Bill Buzbee1465db52009-09-23 17:17:35 -07001159 opReg(cUnit, kOpBlx, r7);
Ben Cheng38329f52009-07-07 14:19:20 -07001160
1161 /* r1 = &retChainingCell */
Bill Buzbee1465db52009-09-23 17:17:35 -07001162 addrRetChain = opRegRegImm(cUnit, kOpAdd, r1, rpc, 0);
Ben Cheng38329f52009-07-07 14:19:20 -07001163 addrRetChain->generic.target = (LIR *) retChainingCell;
1164
1165 bypassRechaining->generic.target = (LIR *) addrRetChain;
1166 /*
1167 * r0 = calleeMethod,
1168 * r1 = &ChainingCell,
1169 * r4PC = callsiteDPC,
1170 */
1171 genDispatchToHandler(cUnit, TEMPLATE_INVOKE_METHOD_NO_OPT);
Ben Cheng86717f72010-03-05 15:27:21 -08001172#if defined(JIT_STATS)
1173 gDvmJit.invokePolymorphic++;
Ben Cheng38329f52009-07-07 14:19:20 -07001174#endif
1175 /* Handle exceptions using the interpreter */
1176 genTrap(cUnit, mir->offset, pcrLabel);
1177}
1178
1179/*
1180 * Up calling this function, "this" is stored in r0. The actual class will be
1181 * chased down off r0 and the predicted one will be retrieved through
1182 * predictedChainingCell then a comparison is performed to see whether the
1183 * previously established chaining is still valid.
1184 *
1185 * The return LIR is a branch based on the comparison result. The actual branch
1186 * target will be setup in the caller.
1187 */
Bill Buzbee89efc3d2009-07-28 11:22:22 -07001188static ArmLIR *genCheckPredictedChain(CompilationUnit *cUnit,
1189 ArmLIR *predChainingCell,
1190 ArmLIR *retChainingCell,
Ben Cheng38329f52009-07-07 14:19:20 -07001191 MIR *mir)
1192{
Bill Buzbee1465db52009-09-23 17:17:35 -07001193 /*
1194 * Note: all Dalvik register state should be flushed to
1195 * memory by the point, so register usage restrictions no
1196 * longer apply. All temp & preserved registers may be used.
1197 */
Bill Buzbeec6f10662010-02-09 11:16:15 -08001198 dvmCompilerLockAllTemps(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07001199
Ben Cheng38329f52009-07-07 14:19:20 -07001200 /* r3 now contains this->clazz */
Bill Buzbee270c1d62009-08-13 16:58:07 -07001201 loadWordDisp(cUnit, r0, offsetof(Object, clazz), r3);
Ben Cheng38329f52009-07-07 14:19:20 -07001202
1203 /*
1204 * r2 now contains predicted class. The starting offset of the
1205 * cached value is 4 bytes into the chaining cell.
1206 */
Bill Buzbee89efc3d2009-07-28 11:22:22 -07001207 ArmLIR *getPredictedClass =
Bill Buzbee270c1d62009-08-13 16:58:07 -07001208 loadWordDisp(cUnit, rpc, offsetof(PredictedChainingCell, clazz), r2);
Ben Cheng38329f52009-07-07 14:19:20 -07001209 getPredictedClass->generic.target = (LIR *) predChainingCell;
1210
1211 /*
1212 * r0 now contains predicted method. The starting offset of the
1213 * cached value is 8 bytes into the chaining cell.
1214 */
Bill Buzbee89efc3d2009-07-28 11:22:22 -07001215 ArmLIR *getPredictedMethod =
Bill Buzbee270c1d62009-08-13 16:58:07 -07001216 loadWordDisp(cUnit, rpc, offsetof(PredictedChainingCell, method), r0);
Ben Cheng38329f52009-07-07 14:19:20 -07001217 getPredictedMethod->generic.target = (LIR *) predChainingCell;
1218
1219 /* Load the stats counter to see if it is time to unchain and refresh */
Bill Buzbee89efc3d2009-07-28 11:22:22 -07001220 ArmLIR *getRechainingRequestCount =
Bill Buzbee270c1d62009-08-13 16:58:07 -07001221 loadWordDisp(cUnit, rpc, offsetof(PredictedChainingCell, counter), r7);
Ben Cheng38329f52009-07-07 14:19:20 -07001222 getRechainingRequestCount->generic.target =
1223 (LIR *) predChainingCell;
1224
1225 /* r4PC = dalvikCallsite */
1226 loadConstant(cUnit, r4PC,
1227 (int) (cUnit->method->insns + mir->offset));
1228
1229 /* r1 = &retChainingCell */
Bill Buzbee1465db52009-09-23 17:17:35 -07001230 ArmLIR *addrRetChain = opRegRegImm(cUnit, kOpAdd, r1, rpc, 0);
Ben Cheng38329f52009-07-07 14:19:20 -07001231 addrRetChain->generic.target = (LIR *) retChainingCell;
1232
1233 /* Check if r2 (predicted class) == r3 (actual class) */
Bill Buzbee1465db52009-09-23 17:17:35 -07001234 opRegReg(cUnit, kOpCmp, r2, r3);
Ben Cheng38329f52009-07-07 14:19:20 -07001235
Bill Buzbee1465db52009-09-23 17:17:35 -07001236 return opCondBranch(cUnit, kArmCondEq);
Ben Cheng38329f52009-07-07 14:19:20 -07001237}
1238
Ben Chengba4fc8b2009-06-01 13:00:29 -07001239/* Geneate a branch to go back to the interpreter */
1240static void genPuntToInterp(CompilationUnit *cUnit, unsigned int offset)
1241{
1242 /* r0 = dalvik pc */
Bill Buzbeec6f10662010-02-09 11:16:15 -08001243 dvmCompilerFlushAllRegs(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001244 loadConstant(cUnit, r0, (int) (cUnit->method->insns + offset));
Bill Buzbee270c1d62009-08-13 16:58:07 -07001245 loadWordDisp(cUnit, r0, offsetof(Object, clazz), r3);
1246 loadWordDisp(cUnit, rGLUE, offsetof(InterpState,
1247 jitToInterpEntries.dvmJitToInterpPunt), r1);
Bill Buzbee1465db52009-09-23 17:17:35 -07001248 opReg(cUnit, kOpBlx, r1);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001249}
1250
1251/*
1252 * Attempt to single step one instruction using the interpreter and return
1253 * to the compiled code for the next Dalvik instruction
1254 */
1255static void genInterpSingleStep(CompilationUnit *cUnit, MIR *mir)
1256{
1257 int flags = dexGetInstrFlags(gDvm.instrFlags, mir->dalvikInsn.opCode);
1258 int flagsToCheck = kInstrCanBranch | kInstrCanSwitch | kInstrCanReturn |
1259 kInstrCanThrow;
Bill Buzbee1465db52009-09-23 17:17:35 -07001260
Bill Buzbee45273872010-03-11 11:12:15 -08001261 //If already optimized out, just ignore
1262 if (mir->dalvikInsn.opCode == OP_NOP)
1263 return;
1264
Bill Buzbee1465db52009-09-23 17:17:35 -07001265 //Ugly, but necessary. Flush all Dalvik regs so Interp can find them
Bill Buzbeec6f10662010-02-09 11:16:15 -08001266 dvmCompilerFlushAllRegs(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07001267
Ben Chengba4fc8b2009-06-01 13:00:29 -07001268 if ((mir->next == NULL) || (flags & flagsToCheck)) {
1269 genPuntToInterp(cUnit, mir->offset);
1270 return;
1271 }
1272 int entryAddr = offsetof(InterpState,
1273 jitToInterpEntries.dvmJitToInterpSingleStep);
Bill Buzbee270c1d62009-08-13 16:58:07 -07001274 loadWordDisp(cUnit, rGLUE, entryAddr, r2);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001275 /* r0 = dalvik pc */
1276 loadConstant(cUnit, r0, (int) (cUnit->method->insns + mir->offset));
1277 /* r1 = dalvik pc of following instruction */
1278 loadConstant(cUnit, r1, (int) (cUnit->method->insns + mir->next->offset));
Bill Buzbee1465db52009-09-23 17:17:35 -07001279 opReg(cUnit, kOpBlx, r2);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001280}
1281
Bill Buzbeec1d9ed42010-02-02 11:04:33 -08001282/*
1283 * To prevent a thread in a monitor wait from blocking the Jit from
1284 * resetting the code cache, heavyweight monitor lock will not
1285 * be allowed to return to an existing translation. Instead, we will
1286 * handle them by branching to a handler, which will in turn call the
1287 * runtime lock routine and then branch directly back to the
1288 * interpreter main loop. Given the high cost of the heavyweight
1289 * lock operation, this additional cost should be slight (especially when
1290 * considering that we expect the vast majority of lock operations to
1291 * use the fast-path thin lock bypass).
1292 */
Ben Cheng5d90c202009-11-22 23:31:11 -08001293static void genMonitorPortable(CompilationUnit *cUnit, MIR *mir)
Bill Buzbee270c1d62009-08-13 16:58:07 -07001294{
Bill Buzbeeefbd3c52009-11-04 22:18:40 -08001295 bool isEnter = (mir->dalvikInsn.opCode == OP_MONITOR_ENTER);
Bill Buzbee1465db52009-09-23 17:17:35 -07001296 genExportPC(cUnit, mir);
Bill Buzbeec6f10662010-02-09 11:16:15 -08001297 dvmCompilerFlushAllRegs(cUnit); /* Send everything to home location */
1298 RegLocation rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07001299 loadValueDirectFixed(cUnit, rlSrc, r1);
1300 loadWordDisp(cUnit, rGLUE, offsetof(InterpState, self), r0);
Bill Buzbeec1d9ed42010-02-02 11:04:33 -08001301 genNullCheck(cUnit, rlSrc.sRegLow, r1, mir->offset, NULL);
Bill Buzbeeefbd3c52009-11-04 22:18:40 -08001302 if (isEnter) {
Bill Buzbeec1d9ed42010-02-02 11:04:33 -08001303 /* Get dPC of next insn */
1304 loadConstant(cUnit, r4PC, (int)(cUnit->method->insns + mir->offset +
1305 dexGetInstrWidthAbs(gDvm.instrWidth, OP_MONITOR_ENTER)));
1306#if defined(WITH_DEADLOCK_PREDICTION)
1307 genDispatchToHandler(cUnit, TEMPLATE_MONITOR_ENTER_DEBUG);
1308#else
1309 genDispatchToHandler(cUnit, TEMPLATE_MONITOR_ENTER);
1310#endif
Bill Buzbee1465db52009-09-23 17:17:35 -07001311 } else {
Ben Chengbd1326d2010-04-02 15:04:53 -07001312 LOAD_FUNC_ADDR(cUnit, r2, (int)dvmUnlockObject);
Bill Buzbeec1d9ed42010-02-02 11:04:33 -08001313 /* Do the call */
1314 opReg(cUnit, kOpBlx, r2);
Bill Buzbee6bbdd6b2010-02-16 14:40:01 -08001315 opRegImm(cUnit, kOpCmp, r0, 0); /* Did we throw? */
1316 ArmLIR *branchOver = opCondBranch(cUnit, kArmCondNe);
1317 loadConstant(cUnit, r0,
1318 (int) (cUnit->method->insns + mir->offset +
1319 dexGetInstrWidthAbs(gDvm.instrWidth, OP_MONITOR_EXIT)));
1320 genDispatchToHandler(cUnit, TEMPLATE_THROW_EXCEPTION_COMMON);
1321 ArmLIR *target = newLIR0(cUnit, kArmPseudoTargetLabel);
1322 target->defMask = ENCODE_ALL;
1323 branchOver->generic.target = (LIR *) target;
Elliott Hughes6a555132010-02-25 15:41:42 -08001324 dvmCompilerClobberCallRegs(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07001325 }
Bill Buzbee270c1d62009-08-13 16:58:07 -07001326}
1327
Ben Chengba4fc8b2009-06-01 13:00:29 -07001328/*
1329 * The following are the first-level codegen routines that analyze the format
1330 * of each bytecode then either dispatch special purpose codegen routines
1331 * or produce corresponding Thumb instructions directly.
1332 */
1333
1334static bool handleFmt10t_Fmt20t_Fmt30t(CompilationUnit *cUnit, MIR *mir,
Bill Buzbee89efc3d2009-07-28 11:22:22 -07001335 BasicBlock *bb, ArmLIR *labelList)
Ben Chengba4fc8b2009-06-01 13:00:29 -07001336{
1337 /* For OP_GOTO, OP_GOTO_16, and OP_GOTO_32 */
1338 genUnconditionalBranch(cUnit, &labelList[bb->taken->id]);
1339 return false;
1340}
1341
1342static bool handleFmt10x(CompilationUnit *cUnit, MIR *mir)
1343{
1344 OpCode dalvikOpCode = mir->dalvikInsn.opCode;
1345 if (((dalvikOpCode >= OP_UNUSED_3E) && (dalvikOpCode <= OP_UNUSED_43)) ||
Andy McFadden96516932009-10-28 17:39:02 -07001346 ((dalvikOpCode >= OP_UNUSED_E3) && (dalvikOpCode <= OP_UNUSED_EB))) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07001347 LOGE("Codegen: got unused opcode 0x%x\n",dalvikOpCode);
1348 return true;
1349 }
1350 switch (dalvikOpCode) {
1351 case OP_RETURN_VOID:
1352 genReturnCommon(cUnit,mir);
1353 break;
1354 case OP_UNUSED_73:
1355 case OP_UNUSED_79:
1356 case OP_UNUSED_7A:
1357 LOGE("Codegen: got unused opcode 0x%x\n",dalvikOpCode);
1358 return true;
1359 case OP_NOP:
1360 break;
1361 default:
1362 return true;
1363 }
1364 return false;
1365}
1366
1367static bool handleFmt11n_Fmt31i(CompilationUnit *cUnit, MIR *mir)
1368{
Bill Buzbee1465db52009-09-23 17:17:35 -07001369 RegLocation rlDest;
1370 RegLocation rlResult;
1371 if (mir->ssaRep->numDefs == 2) {
Bill Buzbeec6f10662010-02-09 11:16:15 -08001372 rlDest = dvmCompilerGetDestWide(cUnit, mir, 0, 1);
Bill Buzbee1465db52009-09-23 17:17:35 -07001373 } else {
Bill Buzbeec6f10662010-02-09 11:16:15 -08001374 rlDest = dvmCompilerGetDest(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07001375 }
Ben Chenge9695e52009-06-16 16:11:47 -07001376
Ben Chengba4fc8b2009-06-01 13:00:29 -07001377 switch (mir->dalvikInsn.opCode) {
1378 case OP_CONST:
Ben Chenge9695e52009-06-16 16:11:47 -07001379 case OP_CONST_4: {
Bill Buzbeec6f10662010-02-09 11:16:15 -08001380 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kAnyReg, true);
Ben Chengbd1326d2010-04-02 15:04:53 -07001381 loadConstantNoClobber(cUnit, rlResult.lowReg, mir->dalvikInsn.vB);
Bill Buzbee1465db52009-09-23 17:17:35 -07001382 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001383 break;
Ben Chenge9695e52009-06-16 16:11:47 -07001384 }
1385 case OP_CONST_WIDE_32: {
Bill Buzbee1465db52009-09-23 17:17:35 -07001386 //TUNING: single routine to load constant pair for support doubles
Bill Buzbee964a7b02010-01-28 12:54:19 -08001387 //TUNING: load 0/-1 separately to avoid load dependency
Bill Buzbeec6f10662010-02-09 11:16:15 -08001388 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Ben Chengbd1326d2010-04-02 15:04:53 -07001389 loadConstantNoClobber(cUnit, rlResult.lowReg, mir->dalvikInsn.vB);
Bill Buzbee1465db52009-09-23 17:17:35 -07001390 opRegRegImm(cUnit, kOpAsr, rlResult.highReg,
1391 rlResult.lowReg, 31);
1392 storeValueWide(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001393 break;
Ben Chenge9695e52009-06-16 16:11:47 -07001394 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07001395 default:
1396 return true;
1397 }
1398 return false;
1399}
1400
1401static bool handleFmt21h(CompilationUnit *cUnit, MIR *mir)
1402{
Bill Buzbee1465db52009-09-23 17:17:35 -07001403 RegLocation rlDest;
1404 RegLocation rlResult;
1405 if (mir->ssaRep->numDefs == 2) {
Bill Buzbeec6f10662010-02-09 11:16:15 -08001406 rlDest = dvmCompilerGetDestWide(cUnit, mir, 0, 1);
Bill Buzbee1465db52009-09-23 17:17:35 -07001407 } else {
Bill Buzbeec6f10662010-02-09 11:16:15 -08001408 rlDest = dvmCompilerGetDest(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07001409 }
Bill Buzbeec6f10662010-02-09 11:16:15 -08001410 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kAnyReg, true);
Ben Chenge9695e52009-06-16 16:11:47 -07001411
Ben Chengba4fc8b2009-06-01 13:00:29 -07001412 switch (mir->dalvikInsn.opCode) {
Ben Chenge9695e52009-06-16 16:11:47 -07001413 case OP_CONST_HIGH16: {
Ben Chengbd1326d2010-04-02 15:04:53 -07001414 loadConstantNoClobber(cUnit, rlResult.lowReg,
1415 mir->dalvikInsn.vB << 16);
Bill Buzbee1465db52009-09-23 17:17:35 -07001416 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001417 break;
Ben Chenge9695e52009-06-16 16:11:47 -07001418 }
1419 case OP_CONST_WIDE_HIGH16: {
Bill Buzbee1465db52009-09-23 17:17:35 -07001420 loadConstantValueWide(cUnit, rlResult.lowReg, rlResult.highReg,
1421 0, mir->dalvikInsn.vB << 16);
1422 storeValueWide(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001423 break;
Ben Chenge9695e52009-06-16 16:11:47 -07001424 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07001425 default:
1426 return true;
1427 }
1428 return false;
1429}
1430
1431static bool handleFmt20bc(CompilationUnit *cUnit, MIR *mir)
1432{
1433 /* For OP_THROW_VERIFICATION_ERROR */
1434 genInterpSingleStep(cUnit, mir);
1435 return false;
1436}
1437
1438static bool handleFmt21c_Fmt31c(CompilationUnit *cUnit, MIR *mir)
1439{
Bill Buzbee1465db52009-09-23 17:17:35 -07001440 RegLocation rlResult;
1441 RegLocation rlDest;
1442 RegLocation rlSrc;
Ben Chenge9695e52009-06-16 16:11:47 -07001443
Ben Chengba4fc8b2009-06-01 13:00:29 -07001444 switch (mir->dalvikInsn.opCode) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07001445 case OP_CONST_STRING_JUMBO:
1446 case OP_CONST_STRING: {
1447 void *strPtr = (void*)
1448 (cUnit->method->clazz->pDvmDex->pResStrings[mir->dalvikInsn.vB]);
1449 assert(strPtr != NULL);
Bill Buzbeec6f10662010-02-09 11:16:15 -08001450 rlDest = dvmCompilerGetDest(cUnit, mir, 0);
1451 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Ben Chengbd1326d2010-04-02 15:04:53 -07001452 loadConstantNoClobber(cUnit, rlResult.lowReg, (int) strPtr );
Bill Buzbee1465db52009-09-23 17:17:35 -07001453 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001454 break;
1455 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07001456 case OP_CONST_CLASS: {
1457 void *classPtr = (void*)
1458 (cUnit->method->clazz->pDvmDex->pResClasses[mir->dalvikInsn.vB]);
1459 assert(classPtr != NULL);
Bill Buzbeec6f10662010-02-09 11:16:15 -08001460 rlDest = dvmCompilerGetDest(cUnit, mir, 0);
1461 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Ben Chengbd1326d2010-04-02 15:04:53 -07001462 loadConstantNoClobber(cUnit, rlResult.lowReg, (int) classPtr );
Bill Buzbee1465db52009-09-23 17:17:35 -07001463 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001464 break;
1465 }
1466 case OP_SGET_OBJECT:
1467 case OP_SGET_BOOLEAN:
1468 case OP_SGET_CHAR:
1469 case OP_SGET_BYTE:
1470 case OP_SGET_SHORT:
1471 case OP_SGET: {
Bill Buzbee50a6bf22009-07-08 13:08:04 -07001472 int valOffset = offsetof(StaticField, value);
Bill Buzbeec6f10662010-02-09 11:16:15 -08001473 int tReg = dvmCompilerAllocTemp(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001474 void *fieldPtr = (void*)
1475 (cUnit->method->clazz->pDvmDex->pResFields[mir->dalvikInsn.vB]);
1476 assert(fieldPtr != NULL);
Bill Buzbeec6f10662010-02-09 11:16:15 -08001477 rlDest = dvmCompilerGetDest(cUnit, mir, 0);
1478 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kAnyReg, true);
Bill Buzbee1465db52009-09-23 17:17:35 -07001479 loadConstant(cUnit, tReg, (int) fieldPtr + valOffset);
Ben Cheng11d8f142010-03-24 15:24:19 -07001480
1481 HEAP_ACCESS_SHADOW(true);
Bill Buzbee1465db52009-09-23 17:17:35 -07001482 loadWordDisp(cUnit, tReg, 0, rlResult.lowReg);
Ben Cheng11d8f142010-03-24 15:24:19 -07001483 HEAP_ACCESS_SHADOW(false);
1484
Bill Buzbee1465db52009-09-23 17:17:35 -07001485 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001486 break;
1487 }
1488 case OP_SGET_WIDE: {
Bill Buzbee50a6bf22009-07-08 13:08:04 -07001489 int valOffset = offsetof(StaticField, value);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001490 void *fieldPtr = (void*)
1491 (cUnit->method->clazz->pDvmDex->pResFields[mir->dalvikInsn.vB]);
Bill Buzbeec6f10662010-02-09 11:16:15 -08001492 int tReg = dvmCompilerAllocTemp(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001493 assert(fieldPtr != NULL);
Bill Buzbeec6f10662010-02-09 11:16:15 -08001494 rlDest = dvmCompilerGetDestWide(cUnit, mir, 0, 1);
1495 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kAnyReg, true);
Bill Buzbee1465db52009-09-23 17:17:35 -07001496 loadConstant(cUnit, tReg, (int) fieldPtr + valOffset);
Ben Cheng11d8f142010-03-24 15:24:19 -07001497
1498 HEAP_ACCESS_SHADOW(true);
Bill Buzbee1465db52009-09-23 17:17:35 -07001499 loadPair(cUnit, tReg, rlResult.lowReg, rlResult.highReg);
Ben Cheng11d8f142010-03-24 15:24:19 -07001500 HEAP_ACCESS_SHADOW(false);
1501
Bill Buzbee1465db52009-09-23 17:17:35 -07001502 storeValueWide(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001503 break;
1504 }
1505 case OP_SPUT_OBJECT:
1506 case OP_SPUT_BOOLEAN:
1507 case OP_SPUT_CHAR:
1508 case OP_SPUT_BYTE:
1509 case OP_SPUT_SHORT:
1510 case OP_SPUT: {
Bill Buzbee50a6bf22009-07-08 13:08:04 -07001511 int valOffset = offsetof(StaticField, value);
Bill Buzbeec6f10662010-02-09 11:16:15 -08001512 int tReg = dvmCompilerAllocTemp(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001513 void *fieldPtr = (void*)
1514 (cUnit->method->clazz->pDvmDex->pResFields[mir->dalvikInsn.vB]);
Ben Chenge9695e52009-06-16 16:11:47 -07001515
Ben Chengba4fc8b2009-06-01 13:00:29 -07001516 assert(fieldPtr != NULL);
Bill Buzbeec6f10662010-02-09 11:16:15 -08001517 rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07001518 rlSrc = loadValue(cUnit, rlSrc, kAnyReg);
1519 loadConstant(cUnit, tReg, (int) fieldPtr + valOffset);
Ben Cheng11d8f142010-03-24 15:24:19 -07001520
1521 HEAP_ACCESS_SHADOW(true);
Bill Buzbee1465db52009-09-23 17:17:35 -07001522 storeWordDisp(cUnit, tReg, 0 ,rlSrc.lowReg);
Ben Cheng11d8f142010-03-24 15:24:19 -07001523 HEAP_ACCESS_SHADOW(false);
1524
Ben Chengba4fc8b2009-06-01 13:00:29 -07001525 break;
1526 }
1527 case OP_SPUT_WIDE: {
Bill Buzbeec6f10662010-02-09 11:16:15 -08001528 int tReg = dvmCompilerAllocTemp(cUnit);
Bill Buzbee50a6bf22009-07-08 13:08:04 -07001529 int valOffset = offsetof(StaticField, value);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001530 void *fieldPtr = (void*)
1531 (cUnit->method->clazz->pDvmDex->pResFields[mir->dalvikInsn.vB]);
Ben Chenge9695e52009-06-16 16:11:47 -07001532
Ben Chengba4fc8b2009-06-01 13:00:29 -07001533 assert(fieldPtr != NULL);
Bill Buzbeec6f10662010-02-09 11:16:15 -08001534 rlSrc = dvmCompilerGetSrcWide(cUnit, mir, 0, 1);
Bill Buzbee1465db52009-09-23 17:17:35 -07001535 rlSrc = loadValueWide(cUnit, rlSrc, kAnyReg);
1536 loadConstant(cUnit, tReg, (int) fieldPtr + valOffset);
Ben Cheng11d8f142010-03-24 15:24:19 -07001537
1538 HEAP_ACCESS_SHADOW(true);
Bill Buzbee1465db52009-09-23 17:17:35 -07001539 storePair(cUnit, tReg, rlSrc.lowReg, rlSrc.highReg);
Ben Cheng11d8f142010-03-24 15:24:19 -07001540 HEAP_ACCESS_SHADOW(false);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001541 break;
1542 }
1543 case OP_NEW_INSTANCE: {
Ben Chenge9695e52009-06-16 16:11:47 -07001544 /*
1545 * Obey the calling convention and don't mess with the register
1546 * usage.
1547 */
Ben Chengba4fc8b2009-06-01 13:00:29 -07001548 ClassObject *classPtr = (void*)
1549 (cUnit->method->clazz->pDvmDex->pResClasses[mir->dalvikInsn.vB]);
1550 assert(classPtr != NULL);
1551 assert(classPtr->status & CLASS_INITIALIZED);
Ben Cheng79d173c2009-09-29 16:12:51 -07001552 /*
1553 * If it is going to throw, it should not make to the trace to begin
Bill Buzbee1465db52009-09-23 17:17:35 -07001554 * with. However, Alloc might throw, so we need to genExportPC()
Ben Cheng79d173c2009-09-29 16:12:51 -07001555 */
1556 assert((classPtr->accessFlags & (ACC_INTERFACE|ACC_ABSTRACT)) == 0);
Bill Buzbeec6f10662010-02-09 11:16:15 -08001557 dvmCompilerFlushAllRegs(cUnit); /* Everything to home location */
Bill Buzbee1465db52009-09-23 17:17:35 -07001558 genExportPC(cUnit, mir);
Ben Chengbd1326d2010-04-02 15:04:53 -07001559 LOAD_FUNC_ADDR(cUnit, r2, (int)dvmAllocObject);
Ben Chenge9695e52009-06-16 16:11:47 -07001560 loadConstant(cUnit, r0, (int) classPtr);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001561 loadConstant(cUnit, r1, ALLOC_DONT_TRACK);
Bill Buzbee1465db52009-09-23 17:17:35 -07001562 opReg(cUnit, kOpBlx, r2);
Elliott Hughes6a555132010-02-25 15:41:42 -08001563 dvmCompilerClobberCallRegs(cUnit);
Ben Cheng4f489172009-09-27 17:08:35 -07001564 /* generate a branch over if allocation is successful */
Bill Buzbee1465db52009-09-23 17:17:35 -07001565 opRegImm(cUnit, kOpCmp, r0, 0); /* NULL? */
1566 ArmLIR *branchOver = opCondBranch(cUnit, kArmCondNe);
Ben Cheng4f489172009-09-27 17:08:35 -07001567 /*
1568 * OOM exception needs to be thrown here and cannot re-execute
1569 */
1570 loadConstant(cUnit, r0,
1571 (int) (cUnit->method->insns + mir->offset));
1572 genDispatchToHandler(cUnit, TEMPLATE_THROW_EXCEPTION_COMMON);
1573 /* noreturn */
1574
Bill Buzbee1465db52009-09-23 17:17:35 -07001575 ArmLIR *target = newLIR0(cUnit, kArmPseudoTargetLabel);
Ben Cheng4f489172009-09-27 17:08:35 -07001576 target->defMask = ENCODE_ALL;
1577 branchOver->generic.target = (LIR *) target;
Bill Buzbeec6f10662010-02-09 11:16:15 -08001578 rlDest = dvmCompilerGetDest(cUnit, mir, 0);
1579 rlResult = dvmCompilerGetReturn(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07001580 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001581 break;
1582 }
1583 case OP_CHECK_CAST: {
Ben Chenge9695e52009-06-16 16:11:47 -07001584 /*
1585 * Obey the calling convention and don't mess with the register
1586 * usage.
1587 */
Ben Chengba4fc8b2009-06-01 13:00:29 -07001588 ClassObject *classPtr =
1589 (cUnit->method->clazz->pDvmDex->pResClasses[mir->dalvikInsn.vB]);
Bill Buzbee4df41a52009-11-12 17:07:16 -08001590 /*
1591 * Note: It is possible that classPtr is NULL at this point,
1592 * even though this instruction has been successfully interpreted.
1593 * If the previous interpretation had a null source, the
1594 * interpreter would not have bothered to resolve the clazz.
1595 * Bail out to the interpreter in this case, and log it
1596 * so that we can tell if it happens frequently.
1597 */
1598 if (classPtr == NULL) {
Ben Cheng11d8f142010-03-24 15:24:19 -07001599 LOGVV("null clazz in OP_CHECK_CAST, single-stepping");
Bill Buzbee4df41a52009-11-12 17:07:16 -08001600 genInterpSingleStep(cUnit, mir);
1601 return false;
1602 }
Bill Buzbeec6f10662010-02-09 11:16:15 -08001603 dvmCompilerFlushAllRegs(cUnit); /* Everything to home location */
Ben Chengba4fc8b2009-06-01 13:00:29 -07001604 loadConstant(cUnit, r1, (int) classPtr );
Bill Buzbeec6f10662010-02-09 11:16:15 -08001605 rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07001606 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
1607 opRegImm(cUnit, kOpCmp, rlSrc.lowReg, 0); /* Null? */
1608 ArmLIR *branch1 = opCondBranch(cUnit, kArmCondEq);
1609 /*
1610 * rlSrc.lowReg now contains object->clazz. Note that
1611 * it could have been allocated r0, but we're okay so long
1612 * as we don't do anything desctructive until r0 is loaded
1613 * with clazz.
1614 */
Ben Chengba4fc8b2009-06-01 13:00:29 -07001615 /* r0 now contains object->clazz */
Bill Buzbee1465db52009-09-23 17:17:35 -07001616 loadWordDisp(cUnit, rlSrc.lowReg, offsetof(Object, clazz), r0);
Ben Chengbd1326d2010-04-02 15:04:53 -07001617 LOAD_FUNC_ADDR(cUnit, r2, (int)dvmInstanceofNonTrivial);
Bill Buzbee1465db52009-09-23 17:17:35 -07001618 opRegReg(cUnit, kOpCmp, r0, r1);
1619 ArmLIR *branch2 = opCondBranch(cUnit, kArmCondEq);
1620 opReg(cUnit, kOpBlx, r2);
Elliott Hughes6a555132010-02-25 15:41:42 -08001621 dvmCompilerClobberCallRegs(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07001622 /*
1623 * If null, check cast failed - punt to the interpreter. Because
1624 * interpreter will be the one throwing, we don't need to
1625 * genExportPC() here.
1626 */
Bill Buzbee270c1d62009-08-13 16:58:07 -07001627 genZeroCheck(cUnit, r0, mir->offset, NULL);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001628 /* check cast passed - branch target here */
Bill Buzbee1465db52009-09-23 17:17:35 -07001629 ArmLIR *target = newLIR0(cUnit, kArmPseudoTargetLabel);
Ben Chengd7d426a2009-09-22 11:23:36 -07001630 target->defMask = ENCODE_ALL;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001631 branch1->generic.target = (LIR *)target;
1632 branch2->generic.target = (LIR *)target;
1633 break;
1634 }
1635 default:
1636 return true;
1637 }
1638 return false;
1639}
1640
1641static bool handleFmt11x(CompilationUnit *cUnit, MIR *mir)
1642{
1643 OpCode dalvikOpCode = mir->dalvikInsn.opCode;
Bill Buzbee1465db52009-09-23 17:17:35 -07001644 RegLocation rlResult;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001645 switch (dalvikOpCode) {
1646 case OP_MOVE_EXCEPTION: {
1647 int offset = offsetof(InterpState, self);
1648 int exOffset = offsetof(Thread, exception);
Bill Buzbeec6f10662010-02-09 11:16:15 -08001649 int selfReg = dvmCompilerAllocTemp(cUnit);
1650 int resetReg = dvmCompilerAllocTemp(cUnit);
1651 RegLocation rlDest = dvmCompilerGetDest(cUnit, mir, 0);
1652 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Bill Buzbee1465db52009-09-23 17:17:35 -07001653 loadWordDisp(cUnit, rGLUE, offset, selfReg);
Bill Buzbeef9f33282009-11-22 12:45:30 -08001654 loadConstant(cUnit, resetReg, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07001655 loadWordDisp(cUnit, selfReg, exOffset, rlResult.lowReg);
Bill Buzbeef9f33282009-11-22 12:45:30 -08001656 storeWordDisp(cUnit, selfReg, exOffset, resetReg);
Bill Buzbee1465db52009-09-23 17:17:35 -07001657 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001658 break;
1659 }
1660 case OP_MOVE_RESULT:
1661 case OP_MOVE_RESULT_OBJECT: {
Bill Buzbeec6f10662010-02-09 11:16:15 -08001662 RegLocation rlDest = dvmCompilerGetDest(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07001663 RegLocation rlSrc = LOC_DALVIK_RETURN_VAL;
1664 rlSrc.fp = rlDest.fp;
1665 storeValue(cUnit, rlDest, rlSrc);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001666 break;
1667 }
1668 case OP_MOVE_RESULT_WIDE: {
Bill Buzbeec6f10662010-02-09 11:16:15 -08001669 RegLocation rlDest = dvmCompilerGetDestWide(cUnit, mir, 0, 1);
Bill Buzbee1465db52009-09-23 17:17:35 -07001670 RegLocation rlSrc = LOC_DALVIK_RETURN_VAL_WIDE;
1671 rlSrc.fp = rlDest.fp;
1672 storeValueWide(cUnit, rlDest, rlSrc);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001673 break;
1674 }
1675 case OP_RETURN_WIDE: {
Bill Buzbeec6f10662010-02-09 11:16:15 -08001676 RegLocation rlSrc = dvmCompilerGetSrcWide(cUnit, mir, 0, 1);
Bill Buzbee1465db52009-09-23 17:17:35 -07001677 RegLocation rlDest = LOC_DALVIK_RETURN_VAL_WIDE;
1678 rlDest.fp = rlSrc.fp;
1679 storeValueWide(cUnit, rlDest, rlSrc);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001680 genReturnCommon(cUnit,mir);
1681 break;
1682 }
1683 case OP_RETURN:
1684 case OP_RETURN_OBJECT: {
Bill Buzbeec6f10662010-02-09 11:16:15 -08001685 RegLocation rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07001686 RegLocation rlDest = LOC_DALVIK_RETURN_VAL;
1687 rlDest.fp = rlSrc.fp;
1688 storeValue(cUnit, rlDest, rlSrc);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001689 genReturnCommon(cUnit,mir);
1690 break;
1691 }
Bill Buzbee1465db52009-09-23 17:17:35 -07001692 case OP_MONITOR_EXIT:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001693 case OP_MONITOR_ENTER:
Bill Buzbeed0937ef2009-12-22 16:15:39 -08001694#if defined(WITH_DEADLOCK_PREDICTION) || defined(WITH_MONITOR_TRACKING)
Ben Cheng5d90c202009-11-22 23:31:11 -08001695 genMonitorPortable(cUnit, mir);
Bill Buzbee1465db52009-09-23 17:17:35 -07001696#else
Ben Cheng5d90c202009-11-22 23:31:11 -08001697 genMonitor(cUnit, mir);
Bill Buzbee1465db52009-09-23 17:17:35 -07001698#endif
Ben Chengba4fc8b2009-06-01 13:00:29 -07001699 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001700 case OP_THROW: {
1701 genInterpSingleStep(cUnit, mir);
1702 break;
1703 }
1704 default:
1705 return true;
1706 }
1707 return false;
1708}
1709
Bill Buzbeed45ba372009-06-15 17:00:57 -07001710static bool handleFmt12x(CompilationUnit *cUnit, MIR *mir)
1711{
1712 OpCode opCode = mir->dalvikInsn.opCode;
Bill Buzbee1465db52009-09-23 17:17:35 -07001713 RegLocation rlDest;
1714 RegLocation rlSrc;
1715 RegLocation rlResult;
Bill Buzbeed45ba372009-06-15 17:00:57 -07001716
Ben Chengba4fc8b2009-06-01 13:00:29 -07001717 if ( (opCode >= OP_ADD_INT_2ADDR) && (opCode <= OP_REM_DOUBLE_2ADDR)) {
Ben Cheng5d90c202009-11-22 23:31:11 -08001718 return genArithOp( cUnit, mir );
Ben Chengba4fc8b2009-06-01 13:00:29 -07001719 }
1720
Bill Buzbee1465db52009-09-23 17:17:35 -07001721 if (mir->ssaRep->numUses == 2)
Bill Buzbeec6f10662010-02-09 11:16:15 -08001722 rlSrc = dvmCompilerGetSrcWide(cUnit, mir, 0, 1);
Bill Buzbee1465db52009-09-23 17:17:35 -07001723 else
Bill Buzbeec6f10662010-02-09 11:16:15 -08001724 rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07001725 if (mir->ssaRep->numDefs == 2)
Bill Buzbeec6f10662010-02-09 11:16:15 -08001726 rlDest = dvmCompilerGetDestWide(cUnit, mir, 0, 1);
Bill Buzbee1465db52009-09-23 17:17:35 -07001727 else
Bill Buzbeec6f10662010-02-09 11:16:15 -08001728 rlDest = dvmCompilerGetDest(cUnit, mir, 0);
Ben Chenge9695e52009-06-16 16:11:47 -07001729
Ben Chengba4fc8b2009-06-01 13:00:29 -07001730 switch (opCode) {
Bill Buzbee1465db52009-09-23 17:17:35 -07001731 case OP_DOUBLE_TO_INT:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001732 case OP_INT_TO_FLOAT:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001733 case OP_FLOAT_TO_INT:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001734 case OP_DOUBLE_TO_FLOAT:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001735 case OP_FLOAT_TO_DOUBLE:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001736 case OP_INT_TO_DOUBLE:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001737 case OP_FLOAT_TO_LONG:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001738 case OP_LONG_TO_FLOAT:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001739 case OP_DOUBLE_TO_LONG:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001740 case OP_LONG_TO_DOUBLE:
Ben Cheng5d90c202009-11-22 23:31:11 -08001741 return genConversion(cUnit, mir);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001742 case OP_NEG_INT:
1743 case OP_NOT_INT:
Ben Cheng5d90c202009-11-22 23:31:11 -08001744 return genArithOpInt(cUnit, mir, rlDest, rlSrc, rlSrc);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001745 case OP_NEG_LONG:
1746 case OP_NOT_LONG:
Ben Cheng5d90c202009-11-22 23:31:11 -08001747 return genArithOpLong(cUnit, mir, rlDest, rlSrc, rlSrc);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001748 case OP_NEG_FLOAT:
Ben Cheng5d90c202009-11-22 23:31:11 -08001749 return genArithOpFloat(cUnit, mir, rlDest, rlSrc, rlSrc);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001750 case OP_NEG_DOUBLE:
Ben Cheng5d90c202009-11-22 23:31:11 -08001751 return genArithOpDouble(cUnit, mir, rlDest, rlSrc, rlSrc);
Bill Buzbee1465db52009-09-23 17:17:35 -07001752 case OP_MOVE_WIDE:
1753 storeValueWide(cUnit, rlDest, rlSrc);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001754 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07001755 case OP_INT_TO_LONG:
Bill Buzbeec6f10662010-02-09 11:16:15 -08001756 rlSrc = dvmCompilerUpdateLoc(cUnit, rlSrc);
1757 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Bill Buzbee964a7b02010-01-28 12:54:19 -08001758 //TUNING: shouldn't loadValueDirect already check for phys reg?
Bill Buzbee1465db52009-09-23 17:17:35 -07001759 if (rlSrc.location == kLocPhysReg) {
1760 genRegCopy(cUnit, rlResult.lowReg, rlSrc.lowReg);
1761 } else {
1762 loadValueDirect(cUnit, rlSrc, rlResult.lowReg);
1763 }
1764 opRegRegImm(cUnit, kOpAsr, rlResult.highReg,
1765 rlResult.lowReg, 31);
1766 storeValueWide(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001767 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07001768 case OP_LONG_TO_INT:
Bill Buzbeec6f10662010-02-09 11:16:15 -08001769 rlSrc = dvmCompilerUpdateLocWide(cUnit, rlSrc);
1770 rlSrc = dvmCompilerWideToNarrow(cUnit, rlSrc);
Bill Buzbee1465db52009-09-23 17:17:35 -07001771 // Intentional fallthrough
Ben Chengba4fc8b2009-06-01 13:00:29 -07001772 case OP_MOVE:
1773 case OP_MOVE_OBJECT:
Bill Buzbee1465db52009-09-23 17:17:35 -07001774 storeValue(cUnit, rlDest, rlSrc);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001775 break;
1776 case OP_INT_TO_BYTE:
Bill Buzbee1465db52009-09-23 17:17:35 -07001777 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
Bill Buzbeec6f10662010-02-09 11:16:15 -08001778 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Bill Buzbee1465db52009-09-23 17:17:35 -07001779 opRegReg(cUnit, kOp2Byte, rlResult.lowReg, rlSrc.lowReg);
1780 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001781 break;
1782 case OP_INT_TO_SHORT:
Bill Buzbee1465db52009-09-23 17:17:35 -07001783 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
Bill Buzbeec6f10662010-02-09 11:16:15 -08001784 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Bill Buzbee1465db52009-09-23 17:17:35 -07001785 opRegReg(cUnit, kOp2Short, rlResult.lowReg, rlSrc.lowReg);
1786 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001787 break;
1788 case OP_INT_TO_CHAR:
Bill Buzbee1465db52009-09-23 17:17:35 -07001789 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
Bill Buzbeec6f10662010-02-09 11:16:15 -08001790 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Bill Buzbee1465db52009-09-23 17:17:35 -07001791 opRegReg(cUnit, kOp2Char, rlResult.lowReg, rlSrc.lowReg);
1792 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001793 break;
1794 case OP_ARRAY_LENGTH: {
1795 int lenOffset = offsetof(ArrayObject, length);
Bill Buzbee1465db52009-09-23 17:17:35 -07001796 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
1797 genNullCheck(cUnit, rlSrc.sRegLow, rlSrc.lowReg,
1798 mir->offset, NULL);
Bill Buzbeec6f10662010-02-09 11:16:15 -08001799 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Bill Buzbee1465db52009-09-23 17:17:35 -07001800 loadWordDisp(cUnit, rlSrc.lowReg, lenOffset,
1801 rlResult.lowReg);
1802 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001803 break;
1804 }
1805 default:
1806 return true;
1807 }
1808 return false;
1809}
1810
1811static bool handleFmt21s(CompilationUnit *cUnit, MIR *mir)
1812{
1813 OpCode dalvikOpCode = mir->dalvikInsn.opCode;
Bill Buzbee1465db52009-09-23 17:17:35 -07001814 RegLocation rlDest;
1815 RegLocation rlResult;
1816 int BBBB = mir->dalvikInsn.vB;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001817 if (dalvikOpCode == OP_CONST_WIDE_16) {
Bill Buzbeec6f10662010-02-09 11:16:15 -08001818 rlDest = dvmCompilerGetDestWide(cUnit, mir, 0, 1);
1819 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Ben Chengbd1326d2010-04-02 15:04:53 -07001820 loadConstantNoClobber(cUnit, rlResult.lowReg, BBBB);
Bill Buzbee964a7b02010-01-28 12:54:19 -08001821 //TUNING: do high separately to avoid load dependency
Bill Buzbee1465db52009-09-23 17:17:35 -07001822 opRegRegImm(cUnit, kOpAsr, rlResult.highReg, rlResult.lowReg, 31);
1823 storeValueWide(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001824 } else if (dalvikOpCode == OP_CONST_16) {
Bill Buzbeec6f10662010-02-09 11:16:15 -08001825 rlDest = dvmCompilerGetDest(cUnit, mir, 0);
1826 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kAnyReg, true);
Ben Chengbd1326d2010-04-02 15:04:53 -07001827 loadConstantNoClobber(cUnit, rlResult.lowReg, BBBB);
Bill Buzbee1465db52009-09-23 17:17:35 -07001828 storeValue(cUnit, rlDest, rlResult);
1829 } else
Ben Chengba4fc8b2009-06-01 13:00:29 -07001830 return true;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001831 return false;
1832}
1833
1834/* Compare agaist zero */
1835static bool handleFmt21t(CompilationUnit *cUnit, MIR *mir, BasicBlock *bb,
Bill Buzbee89efc3d2009-07-28 11:22:22 -07001836 ArmLIR *labelList)
Ben Chengba4fc8b2009-06-01 13:00:29 -07001837{
1838 OpCode dalvikOpCode = mir->dalvikInsn.opCode;
Bill Buzbee89efc3d2009-07-28 11:22:22 -07001839 ArmConditionCode cond;
Bill Buzbeec6f10662010-02-09 11:16:15 -08001840 RegLocation rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07001841 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
1842 opRegImm(cUnit, kOpCmp, rlSrc.lowReg, 0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001843
Bill Buzbee270c1d62009-08-13 16:58:07 -07001844//TUNING: break this out to allow use of Thumb2 CB[N]Z
Ben Chengba4fc8b2009-06-01 13:00:29 -07001845 switch (dalvikOpCode) {
1846 case OP_IF_EQZ:
Bill Buzbee1465db52009-09-23 17:17:35 -07001847 cond = kArmCondEq;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001848 break;
1849 case OP_IF_NEZ:
Bill Buzbee1465db52009-09-23 17:17:35 -07001850 cond = kArmCondNe;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001851 break;
1852 case OP_IF_LTZ:
Bill Buzbee1465db52009-09-23 17:17:35 -07001853 cond = kArmCondLt;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001854 break;
1855 case OP_IF_GEZ:
Bill Buzbee1465db52009-09-23 17:17:35 -07001856 cond = kArmCondGe;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001857 break;
1858 case OP_IF_GTZ:
Bill Buzbee1465db52009-09-23 17:17:35 -07001859 cond = kArmCondGt;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001860 break;
1861 case OP_IF_LEZ:
Bill Buzbee1465db52009-09-23 17:17:35 -07001862 cond = kArmCondLe;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001863 break;
1864 default:
1865 cond = 0;
1866 LOGE("Unexpected opcode (%d) for Fmt21t\n", dalvikOpCode);
Bill Buzbeefc519dc2010-03-06 23:30:57 -08001867 dvmCompilerAbort(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001868 }
1869 genConditionalBranch(cUnit, cond, &labelList[bb->taken->id]);
1870 /* This mostly likely will be optimized away in a later phase */
1871 genUnconditionalBranch(cUnit, &labelList[bb->fallThrough->id]);
1872 return false;
1873}
1874
Elliott Hughesb4c05972010-02-24 16:36:18 -08001875static bool isPowerOfTwo(int x)
1876{
1877 return (x & (x - 1)) == 0;
1878}
1879
1880// Returns true if no more than two bits are set in 'x'.
1881static bool isPopCountLE2(unsigned int x)
1882{
1883 x &= x - 1;
1884 return (x & (x - 1)) == 0;
1885}
1886
1887// Returns the index of the lowest set bit in 'x'.
1888static int lowestSetBit(unsigned int x) {
1889 int bit_posn = 0;
1890 while ((x & 0xf) == 0) {
1891 bit_posn += 4;
1892 x >>= 4;
Bill Buzbee78cb0e22010-02-11 14:04:53 -08001893 }
Elliott Hughesb4c05972010-02-24 16:36:18 -08001894 while ((x & 1) == 0) {
1895 bit_posn++;
1896 x >>= 1;
1897 }
1898 return bit_posn;
1899}
1900
1901// Returns true if it added instructions to 'cUnit' to multiply 'rlSrc' by 'lit'
1902// and store the result in 'rlDest'.
1903static bool handleEasyMultiply(CompilationUnit *cUnit,
1904 RegLocation rlSrc, RegLocation rlDest, int lit)
1905{
1906 // Can we simplify this multiplication?
1907 bool powerOfTwo = false;
1908 bool popCountLE2 = false;
1909 bool powerOfTwoMinusOne = false;
1910 if (lit < 2) {
1911 // Avoid special cases.
1912 return false;
1913 } else if (isPowerOfTwo(lit)) {
1914 powerOfTwo = true;
1915 } else if (isPopCountLE2(lit)) {
1916 popCountLE2 = true;
1917 } else if (isPowerOfTwo(lit + 1)) {
1918 powerOfTwoMinusOne = true;
1919 } else {
1920 return false;
1921 }
1922 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
1923 RegLocation rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
1924 if (powerOfTwo) {
1925 // Shift.
1926 opRegRegImm(cUnit, kOpLsl, rlResult.lowReg, rlSrc.lowReg,
1927 lowestSetBit(lit));
1928 } else if (popCountLE2) {
1929 // Shift and add and shift.
1930 int firstBit = lowestSetBit(lit);
1931 int secondBit = lowestSetBit(lit ^ (1 << firstBit));
1932 genMultiplyByTwoBitMultiplier(cUnit, rlSrc, rlResult, lit,
1933 firstBit, secondBit);
1934 } else {
1935 // Reverse subtract: (src << (shift + 1)) - src.
1936 assert(powerOfTwoMinusOne);
1937 // TODO: rsb dst, src, src lsl#lowestSetBit(lit + 1)
1938 int tReg = dvmCompilerAllocTemp(cUnit);
1939 opRegRegImm(cUnit, kOpLsl, tReg, rlSrc.lowReg, lowestSetBit(lit + 1));
1940 opRegRegReg(cUnit, kOpSub, rlResult.lowReg, tReg, rlSrc.lowReg);
1941 }
1942 storeValue(cUnit, rlDest, rlResult);
1943 return true;
Bill Buzbee78cb0e22010-02-11 14:04:53 -08001944}
1945
Ben Chengba4fc8b2009-06-01 13:00:29 -07001946static bool handleFmt22b_Fmt22s(CompilationUnit *cUnit, MIR *mir)
1947{
1948 OpCode dalvikOpCode = mir->dalvikInsn.opCode;
Bill Buzbeec6f10662010-02-09 11:16:15 -08001949 RegLocation rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
1950 RegLocation rlDest = dvmCompilerGetDest(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07001951 RegLocation rlResult;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001952 int lit = mir->dalvikInsn.vC;
Ben Cheng4f489172009-09-27 17:08:35 -07001953 OpKind op = 0; /* Make gcc happy */
Bill Buzbee1465db52009-09-23 17:17:35 -07001954 int shiftOp = false;
1955 bool isDiv = false;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001956
Ben Chengba4fc8b2009-06-01 13:00:29 -07001957 switch (dalvikOpCode) {
Bill Buzbee1465db52009-09-23 17:17:35 -07001958 case OP_RSUB_INT_LIT8:
1959 case OP_RSUB_INT: {
1960 int tReg;
1961 //TUNING: add support for use of Arm rsub op
1962 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
Bill Buzbeec6f10662010-02-09 11:16:15 -08001963 tReg = dvmCompilerAllocTemp(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07001964 loadConstant(cUnit, tReg, lit);
Bill Buzbeec6f10662010-02-09 11:16:15 -08001965 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Bill Buzbee1465db52009-09-23 17:17:35 -07001966 opRegRegReg(cUnit, kOpSub, rlResult.lowReg,
1967 tReg, rlSrc.lowReg);
1968 storeValue(cUnit, rlDest, rlResult);
1969 return false;
1970 break;
1971 }
1972
Ben Chengba4fc8b2009-06-01 13:00:29 -07001973 case OP_ADD_INT_LIT8:
1974 case OP_ADD_INT_LIT16:
Bill Buzbee1465db52009-09-23 17:17:35 -07001975 op = kOpAdd;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001976 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001977 case OP_MUL_INT_LIT8:
Bill Buzbee78cb0e22010-02-11 14:04:53 -08001978 case OP_MUL_INT_LIT16: {
Elliott Hughesb4c05972010-02-24 16:36:18 -08001979 if (handleEasyMultiply(cUnit, rlSrc, rlDest, lit)) {
1980 return false;
Bill Buzbee78cb0e22010-02-11 14:04:53 -08001981 }
Elliott Hughesb4c05972010-02-24 16:36:18 -08001982 op = kOpMul;
Bill Buzbee1465db52009-09-23 17:17:35 -07001983 break;
Bill Buzbee78cb0e22010-02-11 14:04:53 -08001984 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07001985 case OP_AND_INT_LIT8:
1986 case OP_AND_INT_LIT16:
Bill Buzbee1465db52009-09-23 17:17:35 -07001987 op = kOpAnd;
1988 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001989 case OP_OR_INT_LIT8:
1990 case OP_OR_INT_LIT16:
Bill Buzbee1465db52009-09-23 17:17:35 -07001991 op = kOpOr;
1992 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001993 case OP_XOR_INT_LIT8:
1994 case OP_XOR_INT_LIT16:
Bill Buzbee1465db52009-09-23 17:17:35 -07001995 op = kOpXor;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001996 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001997 case OP_SHL_INT_LIT8:
Bill Buzbee0e605272009-12-01 14:28:05 -08001998 lit &= 31;
Bill Buzbee1465db52009-09-23 17:17:35 -07001999 shiftOp = true;
2000 op = kOpLsl;
2001 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002002 case OP_SHR_INT_LIT8:
Bill Buzbee0e605272009-12-01 14:28:05 -08002003 lit &= 31;
Bill Buzbee1465db52009-09-23 17:17:35 -07002004 shiftOp = true;
2005 op = kOpAsr;
2006 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002007 case OP_USHR_INT_LIT8:
Bill Buzbee0e605272009-12-01 14:28:05 -08002008 lit &= 31;
Bill Buzbee1465db52009-09-23 17:17:35 -07002009 shiftOp = true;
2010 op = kOpLsr;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002011 break;
2012
2013 case OP_DIV_INT_LIT8:
2014 case OP_DIV_INT_LIT16:
Ben Chengba4fc8b2009-06-01 13:00:29 -07002015 case OP_REM_INT_LIT8:
2016 case OP_REM_INT_LIT16:
2017 if (lit == 0) {
2018 /* Let the interpreter deal with div by 0 */
2019 genInterpSingleStep(cUnit, mir);
2020 return false;
2021 }
Bill Buzbeec6f10662010-02-09 11:16:15 -08002022 dvmCompilerFlushAllRegs(cUnit); /* Everything to home location */
Bill Buzbee1465db52009-09-23 17:17:35 -07002023 loadValueDirectFixed(cUnit, rlSrc, r0);
Bill Buzbeec6f10662010-02-09 11:16:15 -08002024 dvmCompilerClobber(cUnit, r0);
Bill Buzbee1465db52009-09-23 17:17:35 -07002025 if ((dalvikOpCode == OP_DIV_INT_LIT8) ||
2026 (dalvikOpCode == OP_DIV_INT_LIT16)) {
Ben Chengbd1326d2010-04-02 15:04:53 -07002027 LOAD_FUNC_ADDR(cUnit, r2, (int)__aeabi_idiv);
Bill Buzbee1465db52009-09-23 17:17:35 -07002028 isDiv = true;
2029 } else {
Ben Chengbd1326d2010-04-02 15:04:53 -07002030 LOAD_FUNC_ADDR(cUnit, r2, (int)__aeabi_idivmod);
Bill Buzbee1465db52009-09-23 17:17:35 -07002031 isDiv = false;
2032 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07002033 loadConstant(cUnit, r1, lit);
Bill Buzbee1465db52009-09-23 17:17:35 -07002034 opReg(cUnit, kOpBlx, r2);
Elliott Hughes6a555132010-02-25 15:41:42 -08002035 dvmCompilerClobberCallRegs(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07002036 if (isDiv)
Bill Buzbeec6f10662010-02-09 11:16:15 -08002037 rlResult = dvmCompilerGetReturn(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07002038 else
Bill Buzbeec6f10662010-02-09 11:16:15 -08002039 rlResult = dvmCompilerGetReturnAlt(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07002040 storeValue(cUnit, rlDest, rlResult);
2041 return false;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002042 break;
2043 default:
2044 return true;
2045 }
Bill Buzbee1465db52009-09-23 17:17:35 -07002046 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
Bill Buzbeec6f10662010-02-09 11:16:15 -08002047 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Bill Buzbee1465db52009-09-23 17:17:35 -07002048 // Avoid shifts by literal 0 - no support in Thumb. Change to copy
2049 if (shiftOp && (lit == 0)) {
2050 genRegCopy(cUnit, rlResult.lowReg, rlSrc.lowReg);
2051 } else {
2052 opRegRegImm(cUnit, op, rlResult.lowReg, rlSrc.lowReg, lit);
2053 }
2054 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002055 return false;
2056}
2057
2058static bool handleFmt22c(CompilationUnit *cUnit, MIR *mir)
2059{
2060 OpCode dalvikOpCode = mir->dalvikInsn.opCode;
2061 int fieldOffset;
2062
2063 if (dalvikOpCode >= OP_IGET && dalvikOpCode <= OP_IPUT_SHORT) {
2064 InstField *pInstField = (InstField *)
2065 cUnit->method->clazz->pDvmDex->pResFields[mir->dalvikInsn.vC];
Ben Chengba4fc8b2009-06-01 13:00:29 -07002066
2067 assert(pInstField != NULL);
2068 fieldOffset = pInstField->byteOffset;
2069 } else {
Ben Chenga0e7b602009-10-13 23:09:01 -07002070 /* Deliberately break the code while make the compiler happy */
2071 fieldOffset = -1;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002072 }
2073 switch (dalvikOpCode) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07002074 case OP_NEW_ARRAY: {
Bill Buzbee1465db52009-09-23 17:17:35 -07002075 // Generates a call - use explicit registers
Bill Buzbeec6f10662010-02-09 11:16:15 -08002076 RegLocation rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
2077 RegLocation rlDest = dvmCompilerGetDest(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07002078 RegLocation rlResult;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002079 void *classPtr = (void*)
2080 (cUnit->method->clazz->pDvmDex->pResClasses[mir->dalvikInsn.vC]);
2081 assert(classPtr != NULL);
Bill Buzbeec6f10662010-02-09 11:16:15 -08002082 dvmCompilerFlushAllRegs(cUnit); /* Everything to home location */
Bill Buzbee1465db52009-09-23 17:17:35 -07002083 genExportPC(cUnit, mir);
2084 loadValueDirectFixed(cUnit, rlSrc, r1); /* Len */
Ben Chengba4fc8b2009-06-01 13:00:29 -07002085 loadConstant(cUnit, r0, (int) classPtr );
Ben Chengbd1326d2010-04-02 15:04:53 -07002086 LOAD_FUNC_ADDR(cUnit, r3, (int)dvmAllocArrayByClass);
Ben Cheng4f489172009-09-27 17:08:35 -07002087 /*
2088 * "len < 0": bail to the interpreter to re-execute the
2089 * instruction
2090 */
Bill Buzbee89efc3d2009-07-28 11:22:22 -07002091 ArmLIR *pcrLabel =
Bill Buzbee1465db52009-09-23 17:17:35 -07002092 genRegImmCheck(cUnit, kArmCondMi, r1, 0, mir->offset, NULL);
Bill Buzbee270c1d62009-08-13 16:58:07 -07002093 loadConstant(cUnit, r2, ALLOC_DONT_TRACK);
Bill Buzbee1465db52009-09-23 17:17:35 -07002094 opReg(cUnit, kOpBlx, r3);
Elliott Hughes6a555132010-02-25 15:41:42 -08002095 dvmCompilerClobberCallRegs(cUnit);
Ben Cheng4f489172009-09-27 17:08:35 -07002096 /* generate a branch over if allocation is successful */
Bill Buzbee1465db52009-09-23 17:17:35 -07002097 opRegImm(cUnit, kOpCmp, r0, 0); /* NULL? */
2098 ArmLIR *branchOver = opCondBranch(cUnit, kArmCondNe);
Ben Cheng4f489172009-09-27 17:08:35 -07002099 /*
2100 * OOM exception needs to be thrown here and cannot re-execute
2101 */
2102 loadConstant(cUnit, r0,
2103 (int) (cUnit->method->insns + mir->offset));
2104 genDispatchToHandler(cUnit, TEMPLATE_THROW_EXCEPTION_COMMON);
2105 /* noreturn */
2106
Bill Buzbee1465db52009-09-23 17:17:35 -07002107 ArmLIR *target = newLIR0(cUnit, kArmPseudoTargetLabel);
Ben Cheng4f489172009-09-27 17:08:35 -07002108 target->defMask = ENCODE_ALL;
2109 branchOver->generic.target = (LIR *) target;
Bill Buzbeec6f10662010-02-09 11:16:15 -08002110 rlResult = dvmCompilerGetReturn(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07002111 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002112 break;
2113 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07002114 case OP_INSTANCE_OF: {
Bill Buzbee1465db52009-09-23 17:17:35 -07002115 // May generate a call - use explicit registers
Bill Buzbeec6f10662010-02-09 11:16:15 -08002116 RegLocation rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
2117 RegLocation rlDest = dvmCompilerGetDest(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07002118 RegLocation rlResult;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002119 ClassObject *classPtr =
2120 (cUnit->method->clazz->pDvmDex->pResClasses[mir->dalvikInsn.vC]);
Bill Buzbee480e6782010-01-27 15:43:08 -08002121 /*
2122 * Note: It is possible that classPtr is NULL at this point,
2123 * even though this instruction has been successfully interpreted.
2124 * If the previous interpretation had a null source, the
2125 * interpreter would not have bothered to resolve the clazz.
2126 * Bail out to the interpreter in this case, and log it
2127 * so that we can tell if it happens frequently.
2128 */
2129 if (classPtr == NULL) {
2130 LOGD("null clazz in OP_INSTANCE_OF, single-stepping");
2131 genInterpSingleStep(cUnit, mir);
2132 break;
2133 }
Bill Buzbeec6f10662010-02-09 11:16:15 -08002134 dvmCompilerFlushAllRegs(cUnit); /* Everything to home location */
Bill Buzbee1465db52009-09-23 17:17:35 -07002135 loadValueDirectFixed(cUnit, rlSrc, r0); /* Ref */
Ben Chengba4fc8b2009-06-01 13:00:29 -07002136 loadConstant(cUnit, r2, (int) classPtr );
Bill Buzbee270c1d62009-08-13 16:58:07 -07002137//TUNING: compare to 0 primative to allow use of CB[N]Z
Bill Buzbee1465db52009-09-23 17:17:35 -07002138 opRegImm(cUnit, kOpCmp, r0, 0); /* NULL? */
Ben Cheng752c7942009-06-22 10:50:07 -07002139 /* When taken r0 has NULL which can be used for store directly */
Bill Buzbee1465db52009-09-23 17:17:35 -07002140 ArmLIR *branch1 = opCondBranch(cUnit, kArmCondEq);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002141 /* r1 now contains object->clazz */
Bill Buzbee270c1d62009-08-13 16:58:07 -07002142 loadWordDisp(cUnit, r0, offsetof(Object, clazz), r1);
Bill Buzbee1465db52009-09-23 17:17:35 -07002143 /* r1 now contains object->clazz */
Ben Chengbd1326d2010-04-02 15:04:53 -07002144 LOAD_FUNC_ADDR(cUnit, r3, (int)dvmInstanceofNonTrivial);
Ben Cheng752c7942009-06-22 10:50:07 -07002145 loadConstant(cUnit, r0, 1); /* Assume true */
Bill Buzbee1465db52009-09-23 17:17:35 -07002146 opRegReg(cUnit, kOpCmp, r1, r2);
2147 ArmLIR *branch2 = opCondBranch(cUnit, kArmCondEq);
2148 genRegCopy(cUnit, r0, r1);
2149 genRegCopy(cUnit, r1, r2);
2150 opReg(cUnit, kOpBlx, r3);
Elliott Hughes6a555132010-02-25 15:41:42 -08002151 dvmCompilerClobberCallRegs(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002152 /* branch target here */
Bill Buzbee1465db52009-09-23 17:17:35 -07002153 ArmLIR *target = newLIR0(cUnit, kArmPseudoTargetLabel);
Ben Chengd7d426a2009-09-22 11:23:36 -07002154 target->defMask = ENCODE_ALL;
Bill Buzbeec6f10662010-02-09 11:16:15 -08002155 rlResult = dvmCompilerGetReturn(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07002156 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002157 branch1->generic.target = (LIR *)target;
2158 branch2->generic.target = (LIR *)target;
2159 break;
2160 }
2161 case OP_IGET_WIDE:
2162 genIGetWide(cUnit, mir, fieldOffset);
2163 break;
2164 case OP_IGET:
2165 case OP_IGET_OBJECT:
Bill Buzbee1465db52009-09-23 17:17:35 -07002166 genIGet(cUnit, mir, kWord, fieldOffset);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002167 break;
2168 case OP_IGET_BOOLEAN:
Bill Buzbee1465db52009-09-23 17:17:35 -07002169 genIGet(cUnit, mir, kUnsignedByte, fieldOffset);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002170 break;
2171 case OP_IGET_BYTE:
Bill Buzbee1465db52009-09-23 17:17:35 -07002172 genIGet(cUnit, mir, kSignedByte, fieldOffset);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002173 break;
2174 case OP_IGET_CHAR:
Bill Buzbee1465db52009-09-23 17:17:35 -07002175 genIGet(cUnit, mir, kUnsignedHalf, fieldOffset);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002176 break;
2177 case OP_IGET_SHORT:
Bill Buzbee1465db52009-09-23 17:17:35 -07002178 genIGet(cUnit, mir, kSignedHalf, fieldOffset);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002179 break;
2180 case OP_IPUT_WIDE:
2181 genIPutWide(cUnit, mir, fieldOffset);
2182 break;
2183 case OP_IPUT:
2184 case OP_IPUT_OBJECT:
Bill Buzbee1465db52009-09-23 17:17:35 -07002185 genIPut(cUnit, mir, kWord, fieldOffset);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002186 break;
2187 case OP_IPUT_SHORT:
2188 case OP_IPUT_CHAR:
Bill Buzbee1465db52009-09-23 17:17:35 -07002189 genIPut(cUnit, mir, kUnsignedHalf, fieldOffset);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002190 break;
2191 case OP_IPUT_BYTE:
2192 case OP_IPUT_BOOLEAN:
Bill Buzbee1465db52009-09-23 17:17:35 -07002193 genIPut(cUnit, mir, kUnsignedByte, fieldOffset);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002194 break;
2195 default:
2196 return true;
2197 }
2198 return false;
2199}
2200
2201static bool handleFmt22cs(CompilationUnit *cUnit, MIR *mir)
2202{
2203 OpCode dalvikOpCode = mir->dalvikInsn.opCode;
2204 int fieldOffset = mir->dalvikInsn.vC;
2205 switch (dalvikOpCode) {
2206 case OP_IGET_QUICK:
2207 case OP_IGET_OBJECT_QUICK:
Bill Buzbee1465db52009-09-23 17:17:35 -07002208 genIGet(cUnit, mir, kWord, fieldOffset);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002209 break;
2210 case OP_IPUT_QUICK:
2211 case OP_IPUT_OBJECT_QUICK:
Bill Buzbee1465db52009-09-23 17:17:35 -07002212 genIPut(cUnit, mir, kWord, fieldOffset);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002213 break;
2214 case OP_IGET_WIDE_QUICK:
2215 genIGetWide(cUnit, mir, fieldOffset);
2216 break;
2217 case OP_IPUT_WIDE_QUICK:
2218 genIPutWide(cUnit, mir, fieldOffset);
2219 break;
2220 default:
2221 return true;
2222 }
2223 return false;
2224
2225}
2226
2227/* Compare agaist zero */
2228static bool handleFmt22t(CompilationUnit *cUnit, MIR *mir, BasicBlock *bb,
Bill Buzbee89efc3d2009-07-28 11:22:22 -07002229 ArmLIR *labelList)
Ben Chengba4fc8b2009-06-01 13:00:29 -07002230{
2231 OpCode dalvikOpCode = mir->dalvikInsn.opCode;
Bill Buzbee89efc3d2009-07-28 11:22:22 -07002232 ArmConditionCode cond;
Bill Buzbeec6f10662010-02-09 11:16:15 -08002233 RegLocation rlSrc1 = dvmCompilerGetSrc(cUnit, mir, 0);
2234 RegLocation rlSrc2 = dvmCompilerGetSrc(cUnit, mir, 1);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002235
Bill Buzbee1465db52009-09-23 17:17:35 -07002236 rlSrc1 = loadValue(cUnit, rlSrc1, kCoreReg);
2237 rlSrc2 = loadValue(cUnit, rlSrc2, kCoreReg);
2238 opRegReg(cUnit, kOpCmp, rlSrc1.lowReg, rlSrc2.lowReg);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002239
2240 switch (dalvikOpCode) {
2241 case OP_IF_EQ:
Bill Buzbee1465db52009-09-23 17:17:35 -07002242 cond = kArmCondEq;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002243 break;
2244 case OP_IF_NE:
Bill Buzbee1465db52009-09-23 17:17:35 -07002245 cond = kArmCondNe;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002246 break;
2247 case OP_IF_LT:
Bill Buzbee1465db52009-09-23 17:17:35 -07002248 cond = kArmCondLt;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002249 break;
2250 case OP_IF_GE:
Bill Buzbee1465db52009-09-23 17:17:35 -07002251 cond = kArmCondGe;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002252 break;
2253 case OP_IF_GT:
Bill Buzbee1465db52009-09-23 17:17:35 -07002254 cond = kArmCondGt;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002255 break;
2256 case OP_IF_LE:
Bill Buzbee1465db52009-09-23 17:17:35 -07002257 cond = kArmCondLe;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002258 break;
2259 default:
2260 cond = 0;
2261 LOGE("Unexpected opcode (%d) for Fmt22t\n", dalvikOpCode);
Bill Buzbeefc519dc2010-03-06 23:30:57 -08002262 dvmCompilerAbort(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002263 }
2264 genConditionalBranch(cUnit, cond, &labelList[bb->taken->id]);
2265 /* This mostly likely will be optimized away in a later phase */
2266 genUnconditionalBranch(cUnit, &labelList[bb->fallThrough->id]);
2267 return false;
2268}
2269
2270static bool handleFmt22x_Fmt32x(CompilationUnit *cUnit, MIR *mir)
2271{
2272 OpCode opCode = mir->dalvikInsn.opCode;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002273
2274 switch (opCode) {
2275 case OP_MOVE_16:
2276 case OP_MOVE_OBJECT_16:
2277 case OP_MOVE_FROM16:
Ben Chenge9695e52009-06-16 16:11:47 -07002278 case OP_MOVE_OBJECT_FROM16: {
Bill Buzbeec6f10662010-02-09 11:16:15 -08002279 storeValue(cUnit, dvmCompilerGetDest(cUnit, mir, 0),
2280 dvmCompilerGetSrc(cUnit, mir, 0));
Ben Chengba4fc8b2009-06-01 13:00:29 -07002281 break;
Ben Chenge9695e52009-06-16 16:11:47 -07002282 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07002283 case OP_MOVE_WIDE_16:
Ben Chenge9695e52009-06-16 16:11:47 -07002284 case OP_MOVE_WIDE_FROM16: {
Bill Buzbeec6f10662010-02-09 11:16:15 -08002285 storeValueWide(cUnit, dvmCompilerGetDestWide(cUnit, mir, 0, 1),
2286 dvmCompilerGetSrcWide(cUnit, mir, 0, 1));
Ben Chengba4fc8b2009-06-01 13:00:29 -07002287 break;
Ben Chenge9695e52009-06-16 16:11:47 -07002288 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07002289 default:
2290 return true;
2291 }
2292 return false;
2293}
2294
2295static bool handleFmt23x(CompilationUnit *cUnit, MIR *mir)
2296{
2297 OpCode opCode = mir->dalvikInsn.opCode;
Bill Buzbee1465db52009-09-23 17:17:35 -07002298 RegLocation rlSrc1;
2299 RegLocation rlSrc2;
2300 RegLocation rlDest;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002301
2302 if ( (opCode >= OP_ADD_INT) && (opCode <= OP_REM_DOUBLE)) {
Ben Cheng5d90c202009-11-22 23:31:11 -08002303 return genArithOp( cUnit, mir );
Ben Chengba4fc8b2009-06-01 13:00:29 -07002304 }
2305
Bill Buzbee1465db52009-09-23 17:17:35 -07002306 /* APUTs have 3 sources and no targets */
2307 if (mir->ssaRep->numDefs == 0) {
2308 if (mir->ssaRep->numUses == 3) {
Bill Buzbeec6f10662010-02-09 11:16:15 -08002309 rlDest = dvmCompilerGetSrc(cUnit, mir, 0);
2310 rlSrc1 = dvmCompilerGetSrc(cUnit, mir, 1);
2311 rlSrc2 = dvmCompilerGetSrc(cUnit, mir, 2);
Bill Buzbee1465db52009-09-23 17:17:35 -07002312 } else {
2313 assert(mir->ssaRep->numUses == 4);
Bill Buzbeec6f10662010-02-09 11:16:15 -08002314 rlDest = dvmCompilerGetSrcWide(cUnit, mir, 0, 1);
2315 rlSrc1 = dvmCompilerGetSrc(cUnit, mir, 2);
2316 rlSrc2 = dvmCompilerGetSrc(cUnit, mir, 3);
Bill Buzbee1465db52009-09-23 17:17:35 -07002317 }
2318 } else {
2319 /* Two sources and 1 dest. Deduce the operand sizes */
2320 if (mir->ssaRep->numUses == 4) {
Bill Buzbeec6f10662010-02-09 11:16:15 -08002321 rlSrc1 = dvmCompilerGetSrcWide(cUnit, mir, 0, 1);
2322 rlSrc2 = dvmCompilerGetSrcWide(cUnit, mir, 2, 3);
Bill Buzbee1465db52009-09-23 17:17:35 -07002323 } else {
2324 assert(mir->ssaRep->numUses == 2);
Bill Buzbeec6f10662010-02-09 11:16:15 -08002325 rlSrc1 = dvmCompilerGetSrc(cUnit, mir, 0);
2326 rlSrc2 = dvmCompilerGetSrc(cUnit, mir, 1);
Bill Buzbee1465db52009-09-23 17:17:35 -07002327 }
2328 if (mir->ssaRep->numDefs == 2) {
Bill Buzbeec6f10662010-02-09 11:16:15 -08002329 rlDest = dvmCompilerGetDestWide(cUnit, mir, 0, 1);
Bill Buzbee1465db52009-09-23 17:17:35 -07002330 } else {
2331 assert(mir->ssaRep->numDefs == 1);
Bill Buzbeec6f10662010-02-09 11:16:15 -08002332 rlDest = dvmCompilerGetDest(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07002333 }
2334 }
2335
2336
Ben Chengba4fc8b2009-06-01 13:00:29 -07002337 switch (opCode) {
Bill Buzbeed45ba372009-06-15 17:00:57 -07002338 case OP_CMPL_FLOAT:
2339 case OP_CMPG_FLOAT:
2340 case OP_CMPL_DOUBLE:
2341 case OP_CMPG_DOUBLE:
Ben Cheng5d90c202009-11-22 23:31:11 -08002342 return genCmpFP(cUnit, mir, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002343 case OP_CMP_LONG:
Bill Buzbee1465db52009-09-23 17:17:35 -07002344 genCmpLong(cUnit, mir, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002345 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002346 case OP_AGET_WIDE:
Bill Buzbee1465db52009-09-23 17:17:35 -07002347 genArrayGet(cUnit, mir, kLong, rlSrc1, rlSrc2, rlDest, 3);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002348 break;
2349 case OP_AGET:
2350 case OP_AGET_OBJECT:
Bill Buzbee1465db52009-09-23 17:17:35 -07002351 genArrayGet(cUnit, mir, kWord, rlSrc1, rlSrc2, rlDest, 2);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002352 break;
2353 case OP_AGET_BOOLEAN:
Bill Buzbee1465db52009-09-23 17:17:35 -07002354 genArrayGet(cUnit, mir, kUnsignedByte, rlSrc1, rlSrc2, rlDest, 0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002355 break;
2356 case OP_AGET_BYTE:
Bill Buzbee1465db52009-09-23 17:17:35 -07002357 genArrayGet(cUnit, mir, kSignedByte, rlSrc1, rlSrc2, rlDest, 0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002358 break;
2359 case OP_AGET_CHAR:
Bill Buzbee1465db52009-09-23 17:17:35 -07002360 genArrayGet(cUnit, mir, kUnsignedHalf, rlSrc1, rlSrc2, rlDest, 1);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002361 break;
2362 case OP_AGET_SHORT:
Bill Buzbee1465db52009-09-23 17:17:35 -07002363 genArrayGet(cUnit, mir, kSignedHalf, rlSrc1, rlSrc2, rlDest, 1);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002364 break;
2365 case OP_APUT_WIDE:
Bill Buzbee1465db52009-09-23 17:17:35 -07002366 genArrayPut(cUnit, mir, kLong, rlSrc1, rlSrc2, rlDest, 3);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002367 break;
2368 case OP_APUT:
Bill Buzbee1465db52009-09-23 17:17:35 -07002369 genArrayPut(cUnit, mir, kWord, rlSrc1, rlSrc2, rlDest, 2);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002370 break;
Bill Buzbeebe6534f2010-03-12 16:01:35 -08002371 case OP_APUT_OBJECT:
2372 genArrayObjectPut(cUnit, mir, rlSrc1, rlSrc2, rlDest, 2);
2373 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002374 case OP_APUT_SHORT:
2375 case OP_APUT_CHAR:
Bill Buzbee1465db52009-09-23 17:17:35 -07002376 genArrayPut(cUnit, mir, kUnsignedHalf, rlSrc1, rlSrc2, rlDest, 1);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002377 break;
2378 case OP_APUT_BYTE:
2379 case OP_APUT_BOOLEAN:
Bill Buzbee1465db52009-09-23 17:17:35 -07002380 genArrayPut(cUnit, mir, kUnsignedByte, rlSrc1, rlSrc2, rlDest, 0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002381 break;
2382 default:
2383 return true;
2384 }
2385 return false;
2386}
2387
Ben Cheng6c10a972009-10-29 14:39:18 -07002388/*
2389 * Find the matching case.
2390 *
2391 * return values:
2392 * r0 (low 32-bit): pc of the chaining cell corresponding to the resolved case,
2393 * including default which is placed at MIN(size, MAX_CHAINED_SWITCH_CASES).
2394 * r1 (high 32-bit): the branch offset of the matching case (only for indexes
2395 * above MAX_CHAINED_SWITCH_CASES).
2396 *
2397 * Instructions around the call are:
2398 *
2399 * mov r2, pc
2400 * blx &findPackedSwitchIndex
2401 * mov pc, r0
2402 * .align4
2403 * chaining cell for case 0 [8 bytes]
2404 * chaining cell for case 1 [8 bytes]
2405 * :
2406 * chaining cell for case MIN(size, MAX_CHAINED_SWITCH_CASES)-1 [8 bytes]
2407 * chaining cell for case default [8 bytes]
2408 * noChain exit
2409 */
Ben Chengbd1326d2010-04-02 15:04:53 -07002410static s8 findPackedSwitchIndex(const u2* switchData, int testVal, int pc)
Ben Cheng6c10a972009-10-29 14:39:18 -07002411{
2412 int size;
2413 int firstKey;
2414 const int *entries;
2415 int index;
2416 int jumpIndex;
2417 int caseDPCOffset = 0;
2418 /* In Thumb mode pc is 4 ahead of the "mov r2, pc" instruction */
2419 int chainingPC = (pc + 4) & ~3;
2420
2421 /*
2422 * Packed switch data format:
2423 * ushort ident = 0x0100 magic value
2424 * ushort size number of entries in the table
2425 * int first_key first (and lowest) switch case value
2426 * int targets[size] branch targets, relative to switch opcode
2427 *
2428 * Total size is (4+size*2) 16-bit code units.
2429 */
2430 size = switchData[1];
2431 assert(size > 0);
2432
2433 firstKey = switchData[2];
2434 firstKey |= switchData[3] << 16;
2435
2436
2437 /* The entries are guaranteed to be aligned on a 32-bit boundary;
2438 * we can treat them as a native int array.
2439 */
2440 entries = (const int*) &switchData[4];
2441 assert(((u4)entries & 0x3) == 0);
2442
2443 index = testVal - firstKey;
2444
2445 /* Jump to the default cell */
2446 if (index < 0 || index >= size) {
2447 jumpIndex = MIN(size, MAX_CHAINED_SWITCH_CASES);
2448 /* Jump to the non-chaining exit point */
2449 } else if (index >= MAX_CHAINED_SWITCH_CASES) {
2450 jumpIndex = MAX_CHAINED_SWITCH_CASES + 1;
2451 caseDPCOffset = entries[index];
2452 /* Jump to the inline chaining cell */
2453 } else {
2454 jumpIndex = index;
2455 }
2456
2457 chainingPC += jumpIndex * 8;
2458 return (((s8) caseDPCOffset) << 32) | (u8) chainingPC;
2459}
2460
2461/* See comments for findPackedSwitchIndex */
Ben Chengbd1326d2010-04-02 15:04:53 -07002462static s8 findSparseSwitchIndex(const u2* switchData, int testVal, int pc)
Ben Cheng6c10a972009-10-29 14:39:18 -07002463{
2464 int size;
2465 const int *keys;
2466 const int *entries;
2467 int chainingPC = (pc + 4) & ~3;
2468 int i;
2469
2470 /*
2471 * Sparse switch data format:
2472 * ushort ident = 0x0200 magic value
2473 * ushort size number of entries in the table; > 0
2474 * int keys[size] keys, sorted low-to-high; 32-bit aligned
2475 * int targets[size] branch targets, relative to switch opcode
2476 *
2477 * Total size is (2+size*4) 16-bit code units.
2478 */
2479
2480 size = switchData[1];
2481 assert(size > 0);
2482
2483 /* The keys are guaranteed to be aligned on a 32-bit boundary;
2484 * we can treat them as a native int array.
2485 */
2486 keys = (const int*) &switchData[2];
2487 assert(((u4)keys & 0x3) == 0);
2488
2489 /* The entries are guaranteed to be aligned on a 32-bit boundary;
2490 * we can treat them as a native int array.
2491 */
2492 entries = keys + size;
2493 assert(((u4)entries & 0x3) == 0);
2494
2495 /*
2496 * Run through the list of keys, which are guaranteed to
2497 * be sorted low-to-high.
2498 *
2499 * Most tables have 3-4 entries. Few have more than 10. A binary
2500 * search here is probably not useful.
2501 */
2502 for (i = 0; i < size; i++) {
2503 int k = keys[i];
2504 if (k == testVal) {
2505 /* MAX_CHAINED_SWITCH_CASES + 1 is the start of the overflow case */
2506 int jumpIndex = (i < MAX_CHAINED_SWITCH_CASES) ?
2507 i : MAX_CHAINED_SWITCH_CASES + 1;
2508 chainingPC += jumpIndex * 8;
2509 return (((s8) entries[i]) << 32) | (u8) chainingPC;
2510 } else if (k > testVal) {
2511 break;
2512 }
2513 }
2514 return chainingPC + MIN(size, MAX_CHAINED_SWITCH_CASES) * 8;
2515}
2516
Ben Chengba4fc8b2009-06-01 13:00:29 -07002517static bool handleFmt31t(CompilationUnit *cUnit, MIR *mir)
2518{
2519 OpCode dalvikOpCode = mir->dalvikInsn.opCode;
2520 switch (dalvikOpCode) {
2521 case OP_FILL_ARRAY_DATA: {
Bill Buzbeec6f10662010-02-09 11:16:15 -08002522 RegLocation rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07002523 // Making a call - use explicit registers
Bill Buzbeec6f10662010-02-09 11:16:15 -08002524 dvmCompilerFlushAllRegs(cUnit); /* Everything to home location */
Bill Buzbee1465db52009-09-23 17:17:35 -07002525 genExportPC(cUnit, mir);
2526 loadValueDirectFixed(cUnit, rlSrc, r0);
Ben Chengbd1326d2010-04-02 15:04:53 -07002527 LOAD_FUNC_ADDR(cUnit, r2, (int)dvmInterpHandleFillArrayData);
Ben Cheng6c10a972009-10-29 14:39:18 -07002528 loadConstant(cUnit, r1,
2529 (int) (cUnit->method->insns + mir->offset + mir->dalvikInsn.vB));
Bill Buzbee1465db52009-09-23 17:17:35 -07002530 opReg(cUnit, kOpBlx, r2);
Elliott Hughes6a555132010-02-25 15:41:42 -08002531 dvmCompilerClobberCallRegs(cUnit);
Bill Buzbeefd023aa2009-11-02 09:23:49 -08002532 /* generate a branch over if successful */
2533 opRegImm(cUnit, kOpCmp, r0, 0); /* NULL? */
2534 ArmLIR *branchOver = opCondBranch(cUnit, kArmCondNe);
2535 loadConstant(cUnit, r0,
2536 (int) (cUnit->method->insns + mir->offset));
2537 genDispatchToHandler(cUnit, TEMPLATE_THROW_EXCEPTION_COMMON);
2538 ArmLIR *target = newLIR0(cUnit, kArmPseudoTargetLabel);
2539 target->defMask = ENCODE_ALL;
2540 branchOver->generic.target = (LIR *) target;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002541 break;
2542 }
2543 /*
Ben Cheng6c10a972009-10-29 14:39:18 -07002544 * Compute the goto target of up to
2545 * MIN(switchSize, MAX_CHAINED_SWITCH_CASES) + 1 chaining cells.
2546 * See the comment before findPackedSwitchIndex for the code layout.
Ben Chengba4fc8b2009-06-01 13:00:29 -07002547 */
2548 case OP_PACKED_SWITCH:
2549 case OP_SPARSE_SWITCH: {
Bill Buzbeec6f10662010-02-09 11:16:15 -08002550 RegLocation rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
2551 dvmCompilerFlushAllRegs(cUnit); /* Everything to home location */
Bill Buzbee1465db52009-09-23 17:17:35 -07002552 loadValueDirectFixed(cUnit, rlSrc, r1);
Bill Buzbeec6f10662010-02-09 11:16:15 -08002553 dvmCompilerLockAllTemps(cUnit);
Ben Cheng6c10a972009-10-29 14:39:18 -07002554 const u2 *switchData =
2555 cUnit->method->insns + mir->offset + mir->dalvikInsn.vB;
2556 u2 size = switchData[1];
2557
Ben Chengba4fc8b2009-06-01 13:00:29 -07002558 if (dalvikOpCode == OP_PACKED_SWITCH) {
Ben Chengbd1326d2010-04-02 15:04:53 -07002559 LOAD_FUNC_ADDR(cUnit, r4PC, (int)findPackedSwitchIndex);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002560 } else {
Ben Chengbd1326d2010-04-02 15:04:53 -07002561 LOAD_FUNC_ADDR(cUnit, r4PC, (int)findSparseSwitchIndex);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002562 }
Ben Cheng6c10a972009-10-29 14:39:18 -07002563 /* r0 <- Addr of the switch data */
2564 loadConstant(cUnit, r0,
2565 (int) (cUnit->method->insns + mir->offset + mir->dalvikInsn.vB));
2566 /* r2 <- pc of the instruction following the blx */
2567 opRegReg(cUnit, kOpMov, r2, rpc);
Bill Buzbee1465db52009-09-23 17:17:35 -07002568 opReg(cUnit, kOpBlx, r4PC);
Elliott Hughes6a555132010-02-25 15:41:42 -08002569 dvmCompilerClobberCallRegs(cUnit);
Ben Cheng6c10a972009-10-29 14:39:18 -07002570 /* pc <- computed goto target */
2571 opRegReg(cUnit, kOpMov, rpc, r0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002572 break;
2573 }
2574 default:
2575 return true;
2576 }
2577 return false;
2578}
2579
2580static bool handleFmt35c_3rc(CompilationUnit *cUnit, MIR *mir, BasicBlock *bb,
Bill Buzbee89efc3d2009-07-28 11:22:22 -07002581 ArmLIR *labelList)
Ben Chengba4fc8b2009-06-01 13:00:29 -07002582{
Bill Buzbee9bc3df32009-07-30 10:52:29 -07002583 ArmLIR *retChainingCell = NULL;
Bill Buzbee89efc3d2009-07-28 11:22:22 -07002584 ArmLIR *pcrLabel = NULL;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002585
Bill Buzbeef4ce16f2009-07-28 13:28:25 -07002586 if (bb->fallThrough != NULL)
2587 retChainingCell = &labelList[bb->fallThrough->id];
2588
Ben Chengba4fc8b2009-06-01 13:00:29 -07002589 DecodedInstruction *dInsn = &mir->dalvikInsn;
2590 switch (mir->dalvikInsn.opCode) {
2591 /*
2592 * calleeMethod = this->clazz->vtable[
2593 * method->clazz->pDvmDex->pResMethods[BBBB]->methodIndex
2594 * ]
2595 */
2596 case OP_INVOKE_VIRTUAL:
2597 case OP_INVOKE_VIRTUAL_RANGE: {
Bill Buzbee89efc3d2009-07-28 11:22:22 -07002598 ArmLIR *predChainingCell = &labelList[bb->taken->id];
Ben Chengba4fc8b2009-06-01 13:00:29 -07002599 int methodIndex =
2600 cUnit->method->clazz->pDvmDex->pResMethods[dInsn->vB]->
2601 methodIndex;
2602
2603 if (mir->dalvikInsn.opCode == OP_INVOKE_VIRTUAL)
2604 genProcessArgsNoRange(cUnit, mir, dInsn, &pcrLabel);
2605 else
2606 genProcessArgsRange(cUnit, mir, dInsn, &pcrLabel);
2607
Ben Cheng38329f52009-07-07 14:19:20 -07002608 genInvokeVirtualCommon(cUnit, mir, methodIndex,
2609 retChainingCell,
2610 predChainingCell,
2611 pcrLabel);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002612 break;
2613 }
2614 /*
2615 * calleeMethod = method->clazz->super->vtable[method->clazz->pDvmDex
2616 * ->pResMethods[BBBB]->methodIndex]
2617 */
Ben Chengba4fc8b2009-06-01 13:00:29 -07002618 case OP_INVOKE_SUPER:
2619 case OP_INVOKE_SUPER_RANGE: {
2620 int mIndex = cUnit->method->clazz->pDvmDex->
2621 pResMethods[dInsn->vB]->methodIndex;
2622 const Method *calleeMethod =
2623 cUnit->method->clazz->super->vtable[mIndex];
2624
2625 if (mir->dalvikInsn.opCode == OP_INVOKE_SUPER)
2626 genProcessArgsNoRange(cUnit, mir, dInsn, &pcrLabel);
2627 else
2628 genProcessArgsRange(cUnit, mir, dInsn, &pcrLabel);
2629
2630 /* r0 = calleeMethod */
2631 loadConstant(cUnit, r0, (int) calleeMethod);
2632
Ben Cheng38329f52009-07-07 14:19:20 -07002633 genInvokeSingletonCommon(cUnit, mir, bb, labelList, pcrLabel,
2634 calleeMethod);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002635 break;
2636 }
2637 /* calleeMethod = method->clazz->pDvmDex->pResMethods[BBBB] */
2638 case OP_INVOKE_DIRECT:
2639 case OP_INVOKE_DIRECT_RANGE: {
2640 const Method *calleeMethod =
2641 cUnit->method->clazz->pDvmDex->pResMethods[dInsn->vB];
2642
2643 if (mir->dalvikInsn.opCode == OP_INVOKE_DIRECT)
2644 genProcessArgsNoRange(cUnit, mir, dInsn, &pcrLabel);
2645 else
2646 genProcessArgsRange(cUnit, mir, dInsn, &pcrLabel);
2647
2648 /* r0 = calleeMethod */
2649 loadConstant(cUnit, r0, (int) calleeMethod);
2650
Ben Cheng38329f52009-07-07 14:19:20 -07002651 genInvokeSingletonCommon(cUnit, mir, bb, labelList, pcrLabel,
2652 calleeMethod);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002653 break;
2654 }
2655 /* calleeMethod = method->clazz->pDvmDex->pResMethods[BBBB] */
2656 case OP_INVOKE_STATIC:
2657 case OP_INVOKE_STATIC_RANGE: {
2658 const Method *calleeMethod =
2659 cUnit->method->clazz->pDvmDex->pResMethods[dInsn->vB];
2660
2661 if (mir->dalvikInsn.opCode == OP_INVOKE_STATIC)
2662 genProcessArgsNoRange(cUnit, mir, dInsn,
2663 NULL /* no null check */);
2664 else
2665 genProcessArgsRange(cUnit, mir, dInsn,
2666 NULL /* no null check */);
2667
2668 /* r0 = calleeMethod */
2669 loadConstant(cUnit, r0, (int) calleeMethod);
2670
Ben Cheng38329f52009-07-07 14:19:20 -07002671 genInvokeSingletonCommon(cUnit, mir, bb, labelList, pcrLabel,
2672 calleeMethod);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002673 break;
2674 }
Bill Buzbee1465db52009-09-23 17:17:35 -07002675 /*
Ben Chengba4fc8b2009-06-01 13:00:29 -07002676 * calleeMethod = dvmFindInterfaceMethodInCache(this->clazz,
2677 * BBBB, method, method->clazz->pDvmDex)
Ben Cheng38329f52009-07-07 14:19:20 -07002678 *
2679 * Given "invoke-interface {v0}", the following is the generated code:
2680 *
2681 * 0x426a9abe : ldr r0, [r5, #0] --+
2682 * 0x426a9ac0 : mov r7, r5 |
2683 * 0x426a9ac2 : sub r7, #24 |
2684 * 0x426a9ac4 : cmp r0, #0 | genProcessArgsNoRange
2685 * 0x426a9ac6 : beq 0x426a9afe |
2686 * 0x426a9ac8 : stmia r7, <r0> --+
2687 * 0x426a9aca : ldr r4, [pc, #104] --> r4 <- dalvikPC of this invoke
2688 * 0x426a9acc : add r1, pc, #52 --> r1 <- &retChainingCell
2689 * 0x426a9ace : add r2, pc, #60 --> r2 <- &predictedChainingCell
2690 * 0x426a9ad0 : blx_1 0x426a918c --+ TEMPLATE_INVOKE_METHOD_
2691 * 0x426a9ad2 : blx_2 see above --+ PREDICTED_CHAIN
2692 * 0x426a9ad4 : b 0x426a9b0c --> off to the predicted chain
2693 * 0x426a9ad6 : b 0x426a9afe --> punt to the interpreter
Ben Chenga8e64a72009-10-20 13:01:36 -07002694 * 0x426a9ad8 : mov r8, r1 --+
2695 * 0x426a9ada : mov r9, r2 |
2696 * 0x426a9adc : mov r10, r3 |
Ben Cheng38329f52009-07-07 14:19:20 -07002697 * 0x426a9ade : mov r0, r3 |
2698 * 0x426a9ae0 : mov r1, #74 | dvmFindInterfaceMethodInCache
2699 * 0x426a9ae2 : ldr r2, [pc, #76] |
2700 * 0x426a9ae4 : ldr r3, [pc, #68] |
2701 * 0x426a9ae6 : ldr r7, [pc, #64] |
2702 * 0x426a9ae8 : blx r7 --+
Ben Chenga8e64a72009-10-20 13:01:36 -07002703 * 0x426a9aea : mov r1, r8 --> r1 <- rechain count
Ben Cheng38329f52009-07-07 14:19:20 -07002704 * 0x426a9aec : cmp r1, #0 --> compare against 0
2705 * 0x426a9aee : bgt 0x426a9af8 --> >=0? don't rechain
2706 * 0x426a9af0 : ldr r7, [r6, #96] --+
Ben Chenga8e64a72009-10-20 13:01:36 -07002707 * 0x426a9af2 : mov r2, r9 | dvmJitToPatchPredictedChain
2708 * 0x426a9af4 : mov r3, r10 |
Ben Cheng38329f52009-07-07 14:19:20 -07002709 * 0x426a9af6 : blx r7 --+
2710 * 0x426a9af8 : add r1, pc, #8 --> r1 <- &retChainingCell
2711 * 0x426a9afa : blx_1 0x426a9098 --+ TEMPLATE_INVOKE_METHOD_NO_OPT
2712 * 0x426a9afc : blx_2 see above --+
2713 * -------- reconstruct dalvik PC : 0x428b786c @ +0x001e
2714 * 0x426a9afe (0042): ldr r0, [pc, #52]
2715 * Exception_Handling:
2716 * 0x426a9b00 (0044): ldr r1, [r6, #84]
2717 * 0x426a9b02 (0046): blx r1
2718 * 0x426a9b04 (0048): .align4
2719 * -------- chaining cell (hot): 0x0021
2720 * 0x426a9b04 (0048): ldr r0, [r6, #92]
2721 * 0x426a9b06 (004a): blx r0
2722 * 0x426a9b08 (004c): data 0x7872(30834)
2723 * 0x426a9b0a (004e): data 0x428b(17035)
2724 * 0x426a9b0c (0050): .align4
2725 * -------- chaining cell (predicted)
2726 * 0x426a9b0c (0050): data 0x0000(0) --> will be patched into bx
2727 * 0x426a9b0e (0052): data 0x0000(0)
2728 * 0x426a9b10 (0054): data 0x0000(0) --> class
2729 * 0x426a9b12 (0056): data 0x0000(0)
2730 * 0x426a9b14 (0058): data 0x0000(0) --> method
2731 * 0x426a9b16 (005a): data 0x0000(0)
2732 * 0x426a9b18 (005c): data 0x0000(0) --> reset count
2733 * 0x426a9b1a (005e): data 0x0000(0)
2734 * 0x426a9b28 (006c): .word (0xad0392a5)
2735 * 0x426a9b2c (0070): .word (0x6e750)
2736 * 0x426a9b30 (0074): .word (0x4109a618)
2737 * 0x426a9b34 (0078): .word (0x428b786c)
Ben Chengba4fc8b2009-06-01 13:00:29 -07002738 */
2739 case OP_INVOKE_INTERFACE:
2740 case OP_INVOKE_INTERFACE_RANGE: {
Bill Buzbee89efc3d2009-07-28 11:22:22 -07002741 ArmLIR *predChainingCell = &labelList[bb->taken->id];
Ben Chengba4fc8b2009-06-01 13:00:29 -07002742 int methodIndex = dInsn->vB;
2743
Bill Buzbee1465db52009-09-23 17:17:35 -07002744 /* Ensure that nothing is both live and dirty */
Bill Buzbeec6f10662010-02-09 11:16:15 -08002745 dvmCompilerFlushAllRegs(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07002746
Ben Chengba4fc8b2009-06-01 13:00:29 -07002747 if (mir->dalvikInsn.opCode == OP_INVOKE_INTERFACE)
2748 genProcessArgsNoRange(cUnit, mir, dInsn, &pcrLabel);
2749 else
2750 genProcessArgsRange(cUnit, mir, dInsn, &pcrLabel);
2751
Ben Cheng38329f52009-07-07 14:19:20 -07002752 /* "this" is already left in r0 by genProcessArgs* */
2753
2754 /* r4PC = dalvikCallsite */
2755 loadConstant(cUnit, r4PC,
2756 (int) (cUnit->method->insns + mir->offset));
2757
2758 /* r1 = &retChainingCell */
Bill Buzbee270c1d62009-08-13 16:58:07 -07002759 ArmLIR *addrRetChain =
Bill Buzbee1465db52009-09-23 17:17:35 -07002760 opRegRegImm(cUnit, kOpAdd, r1, rpc, 0);
Ben Cheng38329f52009-07-07 14:19:20 -07002761 addrRetChain->generic.target = (LIR *) retChainingCell;
2762
2763 /* r2 = &predictedChainingCell */
Bill Buzbee89efc3d2009-07-28 11:22:22 -07002764 ArmLIR *predictedChainingCell =
Bill Buzbee1465db52009-09-23 17:17:35 -07002765 opRegRegImm(cUnit, kOpAdd, r2, rpc, 0);
Ben Cheng38329f52009-07-07 14:19:20 -07002766 predictedChainingCell->generic.target = (LIR *) predChainingCell;
2767
2768 genDispatchToHandler(cUnit, TEMPLATE_INVOKE_METHOD_PREDICTED_CHAIN);
2769
2770 /* return through lr - jump to the chaining cell */
2771 genUnconditionalBranch(cUnit, predChainingCell);
2772
2773 /*
2774 * null-check on "this" may have been eliminated, but we still need
2775 * a PC-reconstruction label for stack overflow bailout.
2776 */
2777 if (pcrLabel == NULL) {
2778 int dPC = (int) (cUnit->method->insns + mir->offset);
Bill Buzbee89efc3d2009-07-28 11:22:22 -07002779 pcrLabel = dvmCompilerNew(sizeof(ArmLIR), true);
Ben Chenga4973592010-03-31 11:59:18 -07002780 pcrLabel->opCode = kArmPseudoPCReconstructionCell;
Ben Cheng38329f52009-07-07 14:19:20 -07002781 pcrLabel->operands[0] = dPC;
2782 pcrLabel->operands[1] = mir->offset;
2783 /* Insert the place holder to the growable list */
2784 dvmInsertGrowableList(&cUnit->pcReconstructionList, pcrLabel);
2785 }
2786
2787 /* return through lr+2 - punt to the interpreter */
2788 genUnconditionalBranch(cUnit, pcrLabel);
2789
2790 /*
2791 * return through lr+4 - fully resolve the callee method.
2792 * r1 <- count
2793 * r2 <- &predictedChainCell
2794 * r3 <- this->class
2795 * r4 <- dPC
2796 * r7 <- this->class->vtable
2797 */
2798
2799 /* Save count, &predictedChainCell, and class to high regs first */
Bill Buzbee1465db52009-09-23 17:17:35 -07002800 genRegCopy(cUnit, r8, r1);
2801 genRegCopy(cUnit, r9, r2);
2802 genRegCopy(cUnit, r10, r3);
Ben Cheng38329f52009-07-07 14:19:20 -07002803
Ben Chengba4fc8b2009-06-01 13:00:29 -07002804 /* r0 now contains this->clazz */
Bill Buzbee1465db52009-09-23 17:17:35 -07002805 genRegCopy(cUnit, r0, r3);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002806
2807 /* r1 = BBBB */
2808 loadConstant(cUnit, r1, dInsn->vB);
2809
2810 /* r2 = method (caller) */
2811 loadConstant(cUnit, r2, (int) cUnit->method);
2812
2813 /* r3 = pDvmDex */
2814 loadConstant(cUnit, r3, (int) cUnit->method->clazz->pDvmDex);
2815
Ben Chengbd1326d2010-04-02 15:04:53 -07002816 LOAD_FUNC_ADDR(cUnit, r7,
2817 (intptr_t) dvmFindInterfaceMethodInCache);
Bill Buzbee1465db52009-09-23 17:17:35 -07002818 opReg(cUnit, kOpBlx, r7);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002819
2820 /* r0 = calleeMethod (returned from dvmFindInterfaceMethodInCache */
2821
Bill Buzbee1465db52009-09-23 17:17:35 -07002822 genRegCopy(cUnit, r1, r8);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002823
Ben Cheng38329f52009-07-07 14:19:20 -07002824 /* Check if rechain limit is reached */
Bill Buzbee1465db52009-09-23 17:17:35 -07002825 opRegImm(cUnit, kOpCmp, r1, 0);
Ben Cheng38329f52009-07-07 14:19:20 -07002826
Bill Buzbee1465db52009-09-23 17:17:35 -07002827 ArmLIR *bypassRechaining = opCondBranch(cUnit, kArmCondGt);
Ben Cheng38329f52009-07-07 14:19:20 -07002828
Bill Buzbee270c1d62009-08-13 16:58:07 -07002829 loadWordDisp(cUnit, rGLUE, offsetof(InterpState,
2830 jitToInterpEntries.dvmJitToPatchPredictedChain), r7);
Ben Cheng38329f52009-07-07 14:19:20 -07002831
Bill Buzbee1465db52009-09-23 17:17:35 -07002832 genRegCopy(cUnit, r2, r9);
2833 genRegCopy(cUnit, r3, r10);
Ben Cheng38329f52009-07-07 14:19:20 -07002834
2835 /*
2836 * r0 = calleeMethod
2837 * r2 = &predictedChainingCell
2838 * r3 = class
2839 *
2840 * &returnChainingCell has been loaded into r1 but is not needed
2841 * when patching the chaining cell and will be clobbered upon
2842 * returning so it will be reconstructed again.
2843 */
Bill Buzbee1465db52009-09-23 17:17:35 -07002844 opReg(cUnit, kOpBlx, r7);
Ben Cheng38329f52009-07-07 14:19:20 -07002845
2846 /* r1 = &retChainingCell */
Bill Buzbee1465db52009-09-23 17:17:35 -07002847 addrRetChain = opRegRegImm(cUnit, kOpAdd, r1, rpc, 0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002848 addrRetChain->generic.target = (LIR *) retChainingCell;
Ben Cheng38329f52009-07-07 14:19:20 -07002849
2850 bypassRechaining->generic.target = (LIR *) addrRetChain;
2851
Ben Chengba4fc8b2009-06-01 13:00:29 -07002852 /*
2853 * r0 = this, r1 = calleeMethod,
2854 * r1 = &ChainingCell,
2855 * r4PC = callsiteDPC,
2856 */
2857 genDispatchToHandler(cUnit, TEMPLATE_INVOKE_METHOD_NO_OPT);
Ben Cheng86717f72010-03-05 15:27:21 -08002858#if defined(JIT_STATS)
2859 gDvmJit.invokePolymorphic++;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002860#endif
2861 /* Handle exceptions using the interpreter */
2862 genTrap(cUnit, mir->offset, pcrLabel);
2863 break;
2864 }
2865 /* NOP */
2866 case OP_INVOKE_DIRECT_EMPTY: {
2867 return false;
2868 }
2869 case OP_FILLED_NEW_ARRAY:
2870 case OP_FILLED_NEW_ARRAY_RANGE: {
2871 /* Just let the interpreter deal with these */
2872 genInterpSingleStep(cUnit, mir);
2873 break;
2874 }
2875 default:
2876 return true;
2877 }
2878 return false;
2879}
2880
2881static bool handleFmt35ms_3rms(CompilationUnit *cUnit, MIR *mir,
Bill Buzbee89efc3d2009-07-28 11:22:22 -07002882 BasicBlock *bb, ArmLIR *labelList)
Ben Chengba4fc8b2009-06-01 13:00:29 -07002883{
Bill Buzbee89efc3d2009-07-28 11:22:22 -07002884 ArmLIR *retChainingCell = &labelList[bb->fallThrough->id];
2885 ArmLIR *predChainingCell = &labelList[bb->taken->id];
2886 ArmLIR *pcrLabel = NULL;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002887
2888 DecodedInstruction *dInsn = &mir->dalvikInsn;
2889 switch (mir->dalvikInsn.opCode) {
2890 /* calleeMethod = this->clazz->vtable[BBBB] */
2891 case OP_INVOKE_VIRTUAL_QUICK_RANGE:
2892 case OP_INVOKE_VIRTUAL_QUICK: {
2893 int methodIndex = dInsn->vB;
2894 if (mir->dalvikInsn.opCode == OP_INVOKE_VIRTUAL_QUICK)
2895 genProcessArgsNoRange(cUnit, mir, dInsn, &pcrLabel);
2896 else
2897 genProcessArgsRange(cUnit, mir, dInsn, &pcrLabel);
2898
Ben Cheng38329f52009-07-07 14:19:20 -07002899 genInvokeVirtualCommon(cUnit, mir, methodIndex,
2900 retChainingCell,
2901 predChainingCell,
2902 pcrLabel);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002903 break;
2904 }
2905 /* calleeMethod = method->clazz->super->vtable[BBBB] */
2906 case OP_INVOKE_SUPER_QUICK:
2907 case OP_INVOKE_SUPER_QUICK_RANGE: {
2908 const Method *calleeMethod =
2909 cUnit->method->clazz->super->vtable[dInsn->vB];
2910
2911 if (mir->dalvikInsn.opCode == OP_INVOKE_SUPER_QUICK)
2912 genProcessArgsNoRange(cUnit, mir, dInsn, &pcrLabel);
2913 else
2914 genProcessArgsRange(cUnit, mir, dInsn, &pcrLabel);
2915
2916 /* r0 = calleeMethod */
2917 loadConstant(cUnit, r0, (int) calleeMethod);
2918
Ben Cheng38329f52009-07-07 14:19:20 -07002919 genInvokeSingletonCommon(cUnit, mir, bb, labelList, pcrLabel,
2920 calleeMethod);
2921 /* Handle exceptions using the interpreter */
2922 genTrap(cUnit, mir->offset, pcrLabel);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002923 break;
2924 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07002925 default:
2926 return true;
2927 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07002928 return false;
2929}
2930
2931/*
Bill Buzbeefd023aa2009-11-02 09:23:49 -08002932 * This operation is complex enough that we'll do it partly inline
2933 * and partly with a handler. NOTE: the handler uses hardcoded
2934 * values for string object offsets and must be revisitied if the
2935 * layout changes.
2936 */
2937static bool genInlinedCompareTo(CompilationUnit *cUnit, MIR *mir)
2938{
2939#if defined(USE_GLOBAL_STRING_DEFS)
2940 return false;
2941#else
2942 ArmLIR *rollback;
Bill Buzbeec6f10662010-02-09 11:16:15 -08002943 RegLocation rlThis = dvmCompilerGetSrc(cUnit, mir, 0);
2944 RegLocation rlComp = dvmCompilerGetSrc(cUnit, mir, 1);
Bill Buzbeefd023aa2009-11-02 09:23:49 -08002945
2946 loadValueDirectFixed(cUnit, rlThis, r0);
2947 loadValueDirectFixed(cUnit, rlComp, r1);
2948 /* Test objects for NULL */
2949 rollback = genNullCheck(cUnit, rlThis.sRegLow, r0, mir->offset, NULL);
2950 genNullCheck(cUnit, rlComp.sRegLow, r1, mir->offset, rollback);
2951 /*
2952 * TUNING: we could check for object pointer equality before invoking
2953 * handler. Unclear whether the gain would be worth the added code size
2954 * expansion.
2955 */
2956 genDispatchToHandler(cUnit, TEMPLATE_STRING_COMPARETO);
Bill Buzbeec6f10662010-02-09 11:16:15 -08002957 storeValue(cUnit, inlinedTarget(cUnit, mir, false),
2958 dvmCompilerGetReturn(cUnit));
Bill Buzbeefd023aa2009-11-02 09:23:49 -08002959 return true;
2960#endif
2961}
2962
2963static bool genInlinedIndexOf(CompilationUnit *cUnit, MIR *mir, bool singleI)
2964{
2965#if defined(USE_GLOBAL_STRING_DEFS)
2966 return false;
2967#else
Bill Buzbeec6f10662010-02-09 11:16:15 -08002968 RegLocation rlThis = dvmCompilerGetSrc(cUnit, mir, 0);
2969 RegLocation rlChar = dvmCompilerGetSrc(cUnit, mir, 1);
Bill Buzbeefd023aa2009-11-02 09:23:49 -08002970
2971 loadValueDirectFixed(cUnit, rlThis, r0);
2972 loadValueDirectFixed(cUnit, rlChar, r1);
2973 if (!singleI) {
Bill Buzbeec6f10662010-02-09 11:16:15 -08002974 RegLocation rlStart = dvmCompilerGetSrc(cUnit, mir, 2);
Bill Buzbeefd023aa2009-11-02 09:23:49 -08002975 loadValueDirectFixed(cUnit, rlStart, r2);
2976 } else {
2977 loadConstant(cUnit, r2, 0);
2978 }
2979 /* Test objects for NULL */
2980 genNullCheck(cUnit, rlThis.sRegLow, r0, mir->offset, NULL);
2981 genDispatchToHandler(cUnit, TEMPLATE_STRING_INDEXOF);
Bill Buzbeec6f10662010-02-09 11:16:15 -08002982 storeValue(cUnit, inlinedTarget(cUnit, mir, false),
2983 dvmCompilerGetReturn(cUnit));
Bill Buzbeefd023aa2009-11-02 09:23:49 -08002984 return true;
2985#endif
2986}
2987
Bill Buzbee1f748632010-03-02 16:14:41 -08002988static bool genInlinedStringLength(CompilationUnit *cUnit, MIR *mir)
2989{
2990 RegLocation rlObj = dvmCompilerGetSrc(cUnit, mir, 0);
2991 RegLocation rlDest = inlinedTarget(cUnit, mir, false);
2992 rlObj = loadValue(cUnit, rlObj, kCoreReg);
2993 RegLocation rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
2994 genNullCheck(cUnit, rlObj.sRegLow, rlObj.lowReg, mir->offset, NULL);
2995 loadWordDisp(cUnit, rlObj.lowReg, gDvm.offJavaLangString_count,
2996 rlResult.lowReg);
2997 storeValue(cUnit, rlDest, rlResult);
2998 return false;
2999}
3000
3001static bool genInlinedStringCharAt(CompilationUnit *cUnit, MIR *mir)
3002{
3003 int contents = offsetof(ArrayObject, contents);
3004 RegLocation rlObj = dvmCompilerGetSrc(cUnit, mir, 0);
3005 RegLocation rlIdx = dvmCompilerGetSrc(cUnit, mir, 1);
3006 RegLocation rlDest = inlinedTarget(cUnit, mir, false);
3007 RegLocation rlResult;
3008 rlObj = loadValue(cUnit, rlObj, kCoreReg);
3009 rlIdx = loadValue(cUnit, rlIdx, kCoreReg);
3010 int regMax = dvmCompilerAllocTemp(cUnit);
3011 int regOff = dvmCompilerAllocTemp(cUnit);
3012 int regPtr = dvmCompilerAllocTemp(cUnit);
3013 ArmLIR *pcrLabel = genNullCheck(cUnit, rlObj.sRegLow, rlObj.lowReg,
3014 mir->offset, NULL);
3015 loadWordDisp(cUnit, rlObj.lowReg, gDvm.offJavaLangString_count, regMax);
3016 loadWordDisp(cUnit, rlObj.lowReg, gDvm.offJavaLangString_offset, regOff);
3017 loadWordDisp(cUnit, rlObj.lowReg, gDvm.offJavaLangString_value, regPtr);
3018 genBoundsCheck(cUnit, rlIdx.lowReg, regMax, mir->offset, pcrLabel);
3019 dvmCompilerFreeTemp(cUnit, regMax);
3020 opRegImm(cUnit, kOpAdd, regPtr, contents);
3021 opRegReg(cUnit, kOpAdd, regOff, rlIdx.lowReg);
3022 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
3023 loadBaseIndexed(cUnit, regPtr, regOff, rlResult.lowReg, 1, kUnsignedHalf);
3024 storeValue(cUnit, rlDest, rlResult);
3025 return false;
3026}
3027
3028static bool genInlinedAbsInt(CompilationUnit *cUnit, MIR *mir)
3029{
3030 RegLocation rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
3031 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
3032 RegLocation rlDest = inlinedTarget(cUnit, mir, false);;
3033 RegLocation rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
3034 int signReg = dvmCompilerAllocTemp(cUnit);
3035 /*
3036 * abs(x) = y<=x>>31, (x+y)^y.
3037 * Thumb2's IT block also yields 3 instructions, but imposes
3038 * scheduling constraints.
3039 */
3040 opRegRegImm(cUnit, kOpAsr, signReg, rlSrc.lowReg, 31);
3041 opRegRegReg(cUnit, kOpAdd, rlResult.lowReg, rlSrc.lowReg, signReg);
3042 opRegReg(cUnit, kOpXor, rlResult.lowReg, signReg);
3043 storeValue(cUnit, rlDest, rlResult);
3044 return false;
3045}
3046
3047static bool genInlinedAbsLong(CompilationUnit *cUnit, MIR *mir)
3048{
3049 RegLocation rlSrc = dvmCompilerGetSrcWide(cUnit, mir, 0, 1);
3050 RegLocation rlDest = inlinedTargetWide(cUnit, mir, false);
3051 rlSrc = loadValueWide(cUnit, rlSrc, kCoreReg);
3052 RegLocation rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
3053 int signReg = dvmCompilerAllocTemp(cUnit);
3054 /*
3055 * abs(x) = y<=x>>31, (x+y)^y.
3056 * Thumb2 IT block allows slightly shorter sequence,
3057 * but introduces a scheduling barrier. Stick with this
3058 * mechanism for now.
3059 */
3060 opRegRegImm(cUnit, kOpAsr, signReg, rlSrc.highReg, 31);
3061 opRegRegReg(cUnit, kOpAdd, rlResult.lowReg, rlSrc.lowReg, signReg);
3062 opRegRegReg(cUnit, kOpAdc, rlResult.highReg, rlSrc.highReg, signReg);
3063 opRegReg(cUnit, kOpXor, rlResult.lowReg, signReg);
3064 opRegReg(cUnit, kOpXor, rlResult.highReg, signReg);
3065 storeValueWide(cUnit, rlDest, rlResult);
3066 return false;
3067}
Bill Buzbeefd023aa2009-11-02 09:23:49 -08003068
3069/*
Bill Buzbeece46c942009-11-20 15:41:34 -08003070 * NOTE: Handles both range and non-range versions (arguments
3071 * have already been normalized by this point).
Ben Chengba4fc8b2009-06-01 13:00:29 -07003072 */
Bill Buzbeece46c942009-11-20 15:41:34 -08003073static bool handleExecuteInline(CompilationUnit *cUnit, MIR *mir)
Ben Chengba4fc8b2009-06-01 13:00:29 -07003074{
3075 DecodedInstruction *dInsn = &mir->dalvikInsn;
3076 switch( mir->dalvikInsn.opCode) {
Bill Buzbeece46c942009-11-20 15:41:34 -08003077 case OP_EXECUTE_INLINE_RANGE:
Ben Chengba4fc8b2009-06-01 13:00:29 -07003078 case OP_EXECUTE_INLINE: {
3079 unsigned int i;
3080 const InlineOperation* inLineTable = dvmGetInlineOpsTable();
Bill Buzbee50a6bf22009-07-08 13:08:04 -07003081 int offset = offsetof(InterpState, retval);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003082 int operation = dInsn->vB;
Bill Buzbee1465db52009-09-23 17:17:35 -07003083 int tReg1;
3084 int tReg2;
Bill Buzbee50a6bf22009-07-08 13:08:04 -07003085 switch (operation) {
3086 case INLINE_EMPTYINLINEMETHOD:
3087 return false; /* Nop */
3088 case INLINE_STRING_LENGTH:
3089 return genInlinedStringLength(cUnit, mir);
3090 case INLINE_MATH_ABS_INT:
3091 return genInlinedAbsInt(cUnit, mir);
3092 case INLINE_MATH_ABS_LONG:
3093 return genInlinedAbsLong(cUnit, mir);
3094 case INLINE_MATH_MIN_INT:
3095 return genInlinedMinMaxInt(cUnit, mir, true);
3096 case INLINE_MATH_MAX_INT:
3097 return genInlinedMinMaxInt(cUnit, mir, false);
3098 case INLINE_STRING_CHARAT:
3099 return genInlinedStringCharAt(cUnit, mir);
3100 case INLINE_MATH_SQRT:
3101 if (genInlineSqrt(cUnit, mir))
Bill Buzbee9727c3d2009-08-01 11:32:36 -07003102 return false;
Bill Buzbee50a6bf22009-07-08 13:08:04 -07003103 else
3104 break; /* Handle with C routine */
Bill Buzbee50a6bf22009-07-08 13:08:04 -07003105 case INLINE_MATH_ABS_FLOAT:
Bill Buzbee1465db52009-09-23 17:17:35 -07003106 if (genInlinedAbsFloat(cUnit, mir))
3107 return false;
3108 else
3109 break;
Bill Buzbee50a6bf22009-07-08 13:08:04 -07003110 case INLINE_MATH_ABS_DOUBLE:
Bill Buzbee1465db52009-09-23 17:17:35 -07003111 if (genInlinedAbsDouble(cUnit, mir))
3112 return false;
3113 else
3114 break;
Bill Buzbee50a6bf22009-07-08 13:08:04 -07003115 case INLINE_STRING_COMPARETO:
Bill Buzbeefd023aa2009-11-02 09:23:49 -08003116 if (genInlinedCompareTo(cUnit, mir))
3117 return false;
3118 else
3119 break;
Bill Buzbee12ba0152009-09-03 14:03:09 -07003120 case INLINE_STRING_INDEXOF_I:
Bill Buzbeefd023aa2009-11-02 09:23:49 -08003121 if (genInlinedIndexOf(cUnit, mir, true /* I */))
3122 return false;
3123 else
3124 break;
Bill Buzbee12ba0152009-09-03 14:03:09 -07003125 case INLINE_STRING_INDEXOF_II:
Bill Buzbeefd023aa2009-11-02 09:23:49 -08003126 if (genInlinedIndexOf(cUnit, mir, false /* I */))
3127 return false;
3128 else
3129 break;
3130 case INLINE_STRING_EQUALS:
3131 case INLINE_MATH_COS:
3132 case INLINE_MATH_SIN:
3133 break; /* Handle with C routine */
Bill Buzbee50a6bf22009-07-08 13:08:04 -07003134 default:
Bill Buzbeefc519dc2010-03-06 23:30:57 -08003135 dvmCompilerAbort(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003136 }
Bill Buzbeec6f10662010-02-09 11:16:15 -08003137 dvmCompilerFlushAllRegs(cUnit); /* Everything to home location */
Elliott Hughes6a555132010-02-25 15:41:42 -08003138 dvmCompilerClobberCallRegs(cUnit);
Bill Buzbeec6f10662010-02-09 11:16:15 -08003139 dvmCompilerClobber(cUnit, r4PC);
3140 dvmCompilerClobber(cUnit, r7);
Bill Buzbee1465db52009-09-23 17:17:35 -07003141 opRegRegImm(cUnit, kOpAdd, r4PC, rGLUE, offset);
3142 opImm(cUnit, kOpPush, (1<<r4PC) | (1<<r7));
Ben Chengbd1326d2010-04-02 15:04:53 -07003143 LOAD_FUNC_ADDR(cUnit, r4PC, (int)inLineTable[operation].func);
Bill Buzbee1465db52009-09-23 17:17:35 -07003144 genExportPC(cUnit, mir);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003145 for (i=0; i < dInsn->vA; i++) {
Bill Buzbeec6f10662010-02-09 11:16:15 -08003146 loadValueDirect(cUnit, dvmCompilerGetSrc(cUnit, mir, i), i);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003147 }
Bill Buzbee1465db52009-09-23 17:17:35 -07003148 opReg(cUnit, kOpBlx, r4PC);
3149 opRegImm(cUnit, kOpAdd, r13, 8);
Bill Buzbeece46c942009-11-20 15:41:34 -08003150 opRegImm(cUnit, kOpCmp, r0, 0); /* NULL? */
3151 ArmLIR *branchOver = opCondBranch(cUnit, kArmCondNe);
3152 loadConstant(cUnit, r0,
3153 (int) (cUnit->method->insns + mir->offset));
3154 genDispatchToHandler(cUnit, TEMPLATE_THROW_EXCEPTION_COMMON);
3155 ArmLIR *target = newLIR0(cUnit, kArmPseudoTargetLabel);
3156 target->defMask = ENCODE_ALL;
3157 branchOver->generic.target = (LIR *) target;
Ben Chengba4fc8b2009-06-01 13:00:29 -07003158 break;
3159 }
3160 default:
3161 return true;
3162 }
3163 return false;
3164}
3165
3166static bool handleFmt51l(CompilationUnit *cUnit, MIR *mir)
3167{
Bill Buzbee1465db52009-09-23 17:17:35 -07003168 //TUNING: We're using core regs here - not optimal when target is a double
Bill Buzbeec6f10662010-02-09 11:16:15 -08003169 RegLocation rlDest = dvmCompilerGetDestWide(cUnit, mir, 0, 1);
3170 RegLocation rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Ben Chengbd1326d2010-04-02 15:04:53 -07003171 loadConstantNoClobber(cUnit, rlResult.lowReg,
3172 mir->dalvikInsn.vB_wide & 0xFFFFFFFFUL);
3173 loadConstantNoClobber(cUnit, rlResult.highReg,
3174 (mir->dalvikInsn.vB_wide>>32) & 0xFFFFFFFFUL);
Bill Buzbee1465db52009-09-23 17:17:35 -07003175 storeValueWide(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003176 return false;
3177}
3178
Ben Chengba4fc8b2009-06-01 13:00:29 -07003179/*
3180 * The following are special processing routines that handle transfer of
3181 * controls between compiled code and the interpreter. Certain VM states like
3182 * Dalvik PC and special-purpose registers are reconstructed here.
3183 */
3184
Ben Cheng1efc9c52009-06-08 18:25:27 -07003185/* Chaining cell for code that may need warmup. */
3186static void handleNormalChainingCell(CompilationUnit *cUnit,
3187 unsigned int offset)
Ben Chengba4fc8b2009-06-01 13:00:29 -07003188{
Ben Cheng11d8f142010-03-24 15:24:19 -07003189 /*
3190 * Use raw instruction constructors to guarantee that the generated
3191 * instructions fit the predefined cell size.
3192 */
3193 newLIR3(cUnit, kThumbLdrRRI5, r0, rGLUE,
3194 offsetof(InterpState,
3195 jitToInterpEntries.dvmJitToInterpNormal) >> 2);
3196 newLIR1(cUnit, kThumbBlxR, r0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003197 addWordData(cUnit, (int) (cUnit->method->insns + offset), true);
3198}
3199
3200/*
Ben Cheng1efc9c52009-06-08 18:25:27 -07003201 * Chaining cell for instructions that immediately following already translated
3202 * code.
Ben Chengba4fc8b2009-06-01 13:00:29 -07003203 */
Ben Cheng1efc9c52009-06-08 18:25:27 -07003204static void handleHotChainingCell(CompilationUnit *cUnit,
3205 unsigned int offset)
Ben Chengba4fc8b2009-06-01 13:00:29 -07003206{
Ben Cheng11d8f142010-03-24 15:24:19 -07003207 /*
3208 * Use raw instruction constructors to guarantee that the generated
3209 * instructions fit the predefined cell size.
3210 */
3211 newLIR3(cUnit, kThumbLdrRRI5, r0, rGLUE,
3212 offsetof(InterpState,
3213 jitToInterpEntries.dvmJitToInterpTraceSelect) >> 2);
3214 newLIR1(cUnit, kThumbBlxR, r0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003215 addWordData(cUnit, (int) (cUnit->method->insns + offset), true);
3216}
3217
Bill Buzbee9c4b7c82009-09-10 10:10:38 -07003218#if defined(WITH_SELF_VERIFICATION) || defined(WITH_JIT_TUNING)
Jeff Hao97319a82009-08-12 16:57:15 -07003219/* Chaining cell for branches that branch back into the same basic block */
3220static void handleBackwardBranchChainingCell(CompilationUnit *cUnit,
3221 unsigned int offset)
3222{
Ben Cheng11d8f142010-03-24 15:24:19 -07003223 /*
3224 * Use raw instruction constructors to guarantee that the generated
3225 * instructions fit the predefined cell size.
3226 */
Bill Buzbee9c4b7c82009-09-10 10:10:38 -07003227#if defined(WITH_SELF_VERIFICATION)
Bill Buzbee1465db52009-09-23 17:17:35 -07003228 newLIR3(cUnit, kThumbLdrRRI5, r0, rGLUE,
Ben Cheng40094c12010-02-24 20:58:44 -08003229 offsetof(InterpState,
3230 jitToInterpEntries.dvmJitToInterpBackwardBranch) >> 2);
Bill Buzbee9c4b7c82009-09-10 10:10:38 -07003231#else
Bill Buzbee1465db52009-09-23 17:17:35 -07003232 newLIR3(cUnit, kThumbLdrRRI5, r0, rGLUE,
Bill Buzbee9c4b7c82009-09-10 10:10:38 -07003233 offsetof(InterpState, jitToInterpEntries.dvmJitToInterpNormal) >> 2);
3234#endif
Bill Buzbee1465db52009-09-23 17:17:35 -07003235 newLIR1(cUnit, kThumbBlxR, r0);
Jeff Hao97319a82009-08-12 16:57:15 -07003236 addWordData(cUnit, (int) (cUnit->method->insns + offset), true);
3237}
3238
3239#endif
Ben Chengba4fc8b2009-06-01 13:00:29 -07003240/* Chaining cell for monomorphic method invocations. */
Ben Cheng38329f52009-07-07 14:19:20 -07003241static void handleInvokeSingletonChainingCell(CompilationUnit *cUnit,
3242 const Method *callee)
Ben Chengba4fc8b2009-06-01 13:00:29 -07003243{
Ben Cheng11d8f142010-03-24 15:24:19 -07003244 /*
3245 * Use raw instruction constructors to guarantee that the generated
3246 * instructions fit the predefined cell size.
3247 */
3248 newLIR3(cUnit, kThumbLdrRRI5, r0, rGLUE,
3249 offsetof(InterpState,
3250 jitToInterpEntries.dvmJitToInterpTraceSelect) >> 2);
3251 newLIR1(cUnit, kThumbBlxR, r0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003252 addWordData(cUnit, (int) (callee->insns), true);
3253}
3254
Ben Cheng38329f52009-07-07 14:19:20 -07003255/* Chaining cell for monomorphic method invocations. */
3256static void handleInvokePredictedChainingCell(CompilationUnit *cUnit)
3257{
3258
3259 /* Should not be executed in the initial state */
3260 addWordData(cUnit, PREDICTED_CHAIN_BX_PAIR_INIT, true);
3261 /* To be filled: class */
3262 addWordData(cUnit, PREDICTED_CHAIN_CLAZZ_INIT, true);
3263 /* To be filled: method */
3264 addWordData(cUnit, PREDICTED_CHAIN_METHOD_INIT, true);
3265 /*
3266 * Rechain count. The initial value of 0 here will trigger chaining upon
3267 * the first invocation of this callsite.
3268 */
3269 addWordData(cUnit, PREDICTED_CHAIN_COUNTER_INIT, true);
3270}
3271
Ben Chengba4fc8b2009-06-01 13:00:29 -07003272/* Load the Dalvik PC into r0 and jump to the specified target */
3273static void handlePCReconstruction(CompilationUnit *cUnit,
Bill Buzbee89efc3d2009-07-28 11:22:22 -07003274 ArmLIR *targetLabel)
Ben Chengba4fc8b2009-06-01 13:00:29 -07003275{
Bill Buzbee89efc3d2009-07-28 11:22:22 -07003276 ArmLIR **pcrLabel =
3277 (ArmLIR **) cUnit->pcReconstructionList.elemList;
Ben Chengba4fc8b2009-06-01 13:00:29 -07003278 int numElems = cUnit->pcReconstructionList.numUsed;
3279 int i;
3280 for (i = 0; i < numElems; i++) {
3281 dvmCompilerAppendLIR(cUnit, (LIR *) pcrLabel[i]);
3282 /* r0 = dalvik PC */
3283 loadConstant(cUnit, r0, pcrLabel[i]->operands[0]);
3284 genUnconditionalBranch(cUnit, targetLabel);
3285 }
3286}
3287
Bill Buzbee1465db52009-09-23 17:17:35 -07003288static char *extendedMIROpNames[kMirOpLast - kMirOpFirst] = {
3289 "kMirOpPhi",
3290 "kMirOpNullNRangeUpCheck",
3291 "kMirOpNullNRangeDownCheck",
3292 "kMirOpLowerBound",
3293 "kMirOpPunt",
Ben Cheng4238ec22009-08-24 16:32:22 -07003294};
3295
3296/*
3297 * vA = arrayReg;
3298 * vB = idxReg;
3299 * vC = endConditionReg;
3300 * arg[0] = maxC
3301 * arg[1] = minC
3302 * arg[2] = loopBranchConditionCode
3303 */
3304static void genHoistedChecksForCountUpLoop(CompilationUnit *cUnit, MIR *mir)
3305{
Bill Buzbee1465db52009-09-23 17:17:35 -07003306 /*
3307 * NOTE: these synthesized blocks don't have ssa names assigned
3308 * for Dalvik registers. However, because they dominate the following
3309 * blocks we can simply use the Dalvik name w/ subscript 0 as the
3310 * ssa name.
3311 */
Ben Cheng4238ec22009-08-24 16:32:22 -07003312 DecodedInstruction *dInsn = &mir->dalvikInsn;
3313 const int lenOffset = offsetof(ArrayObject, length);
Ben Cheng4238ec22009-08-24 16:32:22 -07003314 const int maxC = dInsn->arg[0];
3315 const int minC = dInsn->arg[1];
Bill Buzbee1465db52009-09-23 17:17:35 -07003316 int regLength;
3317 RegLocation rlArray = cUnit->regLocation[mir->dalvikInsn.vA];
3318 RegLocation rlIdxEnd = cUnit->regLocation[mir->dalvikInsn.vC];
Ben Cheng4238ec22009-08-24 16:32:22 -07003319
3320 /* regArray <- arrayRef */
Bill Buzbee1465db52009-09-23 17:17:35 -07003321 rlArray = loadValue(cUnit, rlArray, kCoreReg);
3322 rlIdxEnd = loadValue(cUnit, rlIdxEnd, kCoreReg);
3323 genRegImmCheck(cUnit, kArmCondEq, rlArray.lowReg, 0, 0,
Ben Cheng4238ec22009-08-24 16:32:22 -07003324 (ArmLIR *) cUnit->loopAnalysis->branchToPCR);
3325
3326 /* regLength <- len(arrayRef) */
Bill Buzbeec6f10662010-02-09 11:16:15 -08003327 regLength = dvmCompilerAllocTemp(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07003328 loadWordDisp(cUnit, rlArray.lowReg, lenOffset, regLength);
Ben Cheng4238ec22009-08-24 16:32:22 -07003329
3330 int delta = maxC;
3331 /*
3332 * If the loop end condition is ">=" instead of ">", then the largest value
3333 * of the index is "endCondition - 1".
3334 */
3335 if (dInsn->arg[2] == OP_IF_GE) {
3336 delta--;
3337 }
3338
3339 if (delta) {
Bill Buzbeec6f10662010-02-09 11:16:15 -08003340 int tReg = dvmCompilerAllocTemp(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07003341 opRegRegImm(cUnit, kOpAdd, tReg, rlIdxEnd.lowReg, delta);
3342 rlIdxEnd.lowReg = tReg;
Bill Buzbeec6f10662010-02-09 11:16:15 -08003343 dvmCompilerFreeTemp(cUnit, tReg);
Ben Cheng4238ec22009-08-24 16:32:22 -07003344 }
3345 /* Punt if "regIdxEnd < len(Array)" is false */
Bill Buzbee1465db52009-09-23 17:17:35 -07003346 genRegRegCheck(cUnit, kArmCondGe, rlIdxEnd.lowReg, regLength, 0,
Ben Cheng0fd31e42009-09-03 14:40:16 -07003347 (ArmLIR *) cUnit->loopAnalysis->branchToPCR);
Ben Cheng4238ec22009-08-24 16:32:22 -07003348}
3349
3350/*
3351 * vA = arrayReg;
3352 * vB = idxReg;
3353 * vC = endConditionReg;
3354 * arg[0] = maxC
3355 * arg[1] = minC
3356 * arg[2] = loopBranchConditionCode
3357 */
3358static void genHoistedChecksForCountDownLoop(CompilationUnit *cUnit, MIR *mir)
3359{
3360 DecodedInstruction *dInsn = &mir->dalvikInsn;
3361 const int lenOffset = offsetof(ArrayObject, length);
Bill Buzbeec6f10662010-02-09 11:16:15 -08003362 const int regLength = dvmCompilerAllocTemp(cUnit);
Ben Cheng4238ec22009-08-24 16:32:22 -07003363 const int maxC = dInsn->arg[0];
3364 const int minC = dInsn->arg[1];
Bill Buzbee1465db52009-09-23 17:17:35 -07003365 RegLocation rlArray = cUnit->regLocation[mir->dalvikInsn.vA];
3366 RegLocation rlIdxInit = cUnit->regLocation[mir->dalvikInsn.vB];
Ben Cheng4238ec22009-08-24 16:32:22 -07003367
3368 /* regArray <- arrayRef */
Bill Buzbee1465db52009-09-23 17:17:35 -07003369 rlArray = loadValue(cUnit, rlArray, kCoreReg);
3370 rlIdxInit = loadValue(cUnit, rlIdxInit, kCoreReg);
3371 genRegImmCheck(cUnit, kArmCondEq, rlArray.lowReg, 0, 0,
Ben Cheng4238ec22009-08-24 16:32:22 -07003372 (ArmLIR *) cUnit->loopAnalysis->branchToPCR);
3373
3374 /* regLength <- len(arrayRef) */
Bill Buzbee1465db52009-09-23 17:17:35 -07003375 loadWordDisp(cUnit, rlArray.lowReg, lenOffset, regLength);
Ben Cheng4238ec22009-08-24 16:32:22 -07003376
3377 if (maxC) {
Bill Buzbeec6f10662010-02-09 11:16:15 -08003378 int tReg = dvmCompilerAllocTemp(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07003379 opRegRegImm(cUnit, kOpAdd, tReg, rlIdxInit.lowReg, maxC);
3380 rlIdxInit.lowReg = tReg;
Bill Buzbeec6f10662010-02-09 11:16:15 -08003381 dvmCompilerFreeTemp(cUnit, tReg);
Ben Cheng4238ec22009-08-24 16:32:22 -07003382 }
3383
3384 /* Punt if "regIdxInit < len(Array)" is false */
Bill Buzbee1465db52009-09-23 17:17:35 -07003385 genRegRegCheck(cUnit, kArmCondGe, rlIdxInit.lowReg, regLength, 0,
Ben Cheng0fd31e42009-09-03 14:40:16 -07003386 (ArmLIR *) cUnit->loopAnalysis->branchToPCR);
Ben Cheng4238ec22009-08-24 16:32:22 -07003387}
3388
3389/*
3390 * vA = idxReg;
3391 * vB = minC;
3392 */
3393static void genHoistedLowerBoundCheck(CompilationUnit *cUnit, MIR *mir)
3394{
3395 DecodedInstruction *dInsn = &mir->dalvikInsn;
Ben Cheng4238ec22009-08-24 16:32:22 -07003396 const int minC = dInsn->vB;
Bill Buzbee1465db52009-09-23 17:17:35 -07003397 RegLocation rlIdx = cUnit->regLocation[mir->dalvikInsn.vA];
Ben Cheng4238ec22009-08-24 16:32:22 -07003398
3399 /* regIdx <- initial index value */
Bill Buzbee1465db52009-09-23 17:17:35 -07003400 rlIdx = loadValue(cUnit, rlIdx, kCoreReg);
Ben Cheng4238ec22009-08-24 16:32:22 -07003401
3402 /* Punt if "regIdxInit + minC >= 0" is false */
Bill Buzbee1465db52009-09-23 17:17:35 -07003403 genRegImmCheck(cUnit, kArmCondLt, rlIdx.lowReg, -minC, 0,
Ben Cheng4238ec22009-08-24 16:32:22 -07003404 (ArmLIR *) cUnit->loopAnalysis->branchToPCR);
3405}
3406
3407/* Extended MIR instructions like PHI */
3408static void handleExtendedMIR(CompilationUnit *cUnit, MIR *mir)
3409{
Bill Buzbee1465db52009-09-23 17:17:35 -07003410 int opOffset = mir->dalvikInsn.opCode - kMirOpFirst;
Ben Cheng4238ec22009-08-24 16:32:22 -07003411 char *msg = dvmCompilerNew(strlen(extendedMIROpNames[opOffset]) + 1,
3412 false);
3413 strcpy(msg, extendedMIROpNames[opOffset]);
Bill Buzbee1465db52009-09-23 17:17:35 -07003414 newLIR1(cUnit, kArmPseudoExtended, (int) msg);
Ben Cheng4238ec22009-08-24 16:32:22 -07003415
3416 switch (mir->dalvikInsn.opCode) {
Bill Buzbee1465db52009-09-23 17:17:35 -07003417 case kMirOpPhi: {
Ben Cheng4238ec22009-08-24 16:32:22 -07003418 char *ssaString = dvmCompilerGetSSAString(cUnit, mir->ssaRep);
Bill Buzbee1465db52009-09-23 17:17:35 -07003419 newLIR1(cUnit, kArmPseudoSSARep, (int) ssaString);
Ben Cheng4238ec22009-08-24 16:32:22 -07003420 break;
3421 }
Bill Buzbee1465db52009-09-23 17:17:35 -07003422 case kMirOpNullNRangeUpCheck: {
Ben Cheng4238ec22009-08-24 16:32:22 -07003423 genHoistedChecksForCountUpLoop(cUnit, mir);
3424 break;
3425 }
Bill Buzbee1465db52009-09-23 17:17:35 -07003426 case kMirOpNullNRangeDownCheck: {
Ben Cheng4238ec22009-08-24 16:32:22 -07003427 genHoistedChecksForCountDownLoop(cUnit, mir);
3428 break;
3429 }
Bill Buzbee1465db52009-09-23 17:17:35 -07003430 case kMirOpLowerBound: {
Ben Cheng4238ec22009-08-24 16:32:22 -07003431 genHoistedLowerBoundCheck(cUnit, mir);
3432 break;
3433 }
Bill Buzbee1465db52009-09-23 17:17:35 -07003434 case kMirOpPunt: {
Ben Cheng4238ec22009-08-24 16:32:22 -07003435 genUnconditionalBranch(cUnit,
3436 (ArmLIR *) cUnit->loopAnalysis->branchToPCR);
3437 break;
3438 }
3439 default:
3440 break;
3441 }
3442}
3443
3444/*
3445 * Create a PC-reconstruction cell for the starting offset of this trace.
3446 * Since the PCR cell is placed near the end of the compiled code which is
3447 * usually out of range for a conditional branch, we put two branches (one
3448 * branch over to the loop body and one layover branch to the actual PCR) at the
3449 * end of the entry block.
3450 */
3451static void setupLoopEntryBlock(CompilationUnit *cUnit, BasicBlock *entry,
3452 ArmLIR *bodyLabel)
3453{
3454 /* Set up the place holder to reconstruct this Dalvik PC */
3455 ArmLIR *pcrLabel = dvmCompilerNew(sizeof(ArmLIR), true);
Ben Chenga4973592010-03-31 11:59:18 -07003456 pcrLabel->opCode = kArmPseudoPCReconstructionCell;
Ben Cheng4238ec22009-08-24 16:32:22 -07003457 pcrLabel->operands[0] =
3458 (int) (cUnit->method->insns + entry->startOffset);
3459 pcrLabel->operands[1] = entry->startOffset;
3460 /* Insert the place holder to the growable list */
3461 dvmInsertGrowableList(&cUnit->pcReconstructionList, pcrLabel);
3462
3463 /*
3464 * Next, create two branches - one branch over to the loop body and the
3465 * other branch to the PCR cell to punt.
3466 */
3467 ArmLIR *branchToBody = dvmCompilerNew(sizeof(ArmLIR), true);
Bill Buzbee1465db52009-09-23 17:17:35 -07003468 branchToBody->opCode = kThumbBUncond;
Ben Cheng4238ec22009-08-24 16:32:22 -07003469 branchToBody->generic.target = (LIR *) bodyLabel;
Ben Chengdcf3e5d2009-09-11 13:42:05 -07003470 setupResourceMasks(branchToBody);
Ben Cheng4238ec22009-08-24 16:32:22 -07003471 cUnit->loopAnalysis->branchToBody = (LIR *) branchToBody;
3472
3473 ArmLIR *branchToPCR = dvmCompilerNew(sizeof(ArmLIR), true);
Bill Buzbee1465db52009-09-23 17:17:35 -07003474 branchToPCR->opCode = kThumbBUncond;
Ben Cheng4238ec22009-08-24 16:32:22 -07003475 branchToPCR->generic.target = (LIR *) pcrLabel;
Ben Chengdcf3e5d2009-09-11 13:42:05 -07003476 setupResourceMasks(branchToPCR);
Ben Cheng4238ec22009-08-24 16:32:22 -07003477 cUnit->loopAnalysis->branchToPCR = (LIR *) branchToPCR;
3478}
3479
Ben Chengd5adae12010-03-26 17:45:28 -07003480#if defined(WITH_SELF_VERIFICATION)
3481static bool selfVerificationPuntOps(MIR *mir)
3482{
3483 DecodedInstruction *decInsn = &mir->dalvikInsn;
3484 OpCode op = decInsn->opCode;
3485 int flags = dexGetInstrFlags(gDvm.instrFlags, op);
3486 /*
3487 * All opcodes that can throw exceptions and use the
3488 * TEMPLATE_THROW_EXCEPTION_COMMON template should be excluded in the trace
3489 * under self-verification mode.
3490 */
3491 return (op == OP_MONITOR_ENTER || op == OP_MONITOR_EXIT ||
3492 op == OP_NEW_INSTANCE || op == OP_NEW_ARRAY ||
3493 op == OP_CHECK_CAST || op == OP_MOVE_EXCEPTION ||
3494 op == OP_FILL_ARRAY_DATA || op == OP_EXECUTE_INLINE ||
3495 op == OP_EXECUTE_INLINE_RANGE ||
3496 (flags & kInstrInvoke));
3497}
3498#endif
3499
Ben Chengba4fc8b2009-06-01 13:00:29 -07003500void dvmCompilerMIR2LIR(CompilationUnit *cUnit)
3501{
3502 /* Used to hold the labels of each block */
Bill Buzbee89efc3d2009-07-28 11:22:22 -07003503 ArmLIR *labelList =
3504 dvmCompilerNew(sizeof(ArmLIR) * cUnit->numBlocks, true);
Ben Chengcec26f62010-01-15 15:29:33 -08003505 GrowableList chainingListByType[kChainingCellGap];
Ben Chengba4fc8b2009-06-01 13:00:29 -07003506 int i;
3507
3508 /*
Ben Cheng38329f52009-07-07 14:19:20 -07003509 * Initialize various types chaining lists.
Ben Chengba4fc8b2009-06-01 13:00:29 -07003510 */
Ben Chengcec26f62010-01-15 15:29:33 -08003511 for (i = 0; i < kChainingCellGap; i++) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07003512 dvmInitGrowableList(&chainingListByType[i], 2);
3513 }
3514
3515 BasicBlock **blockList = cUnit->blockList;
3516
Bill Buzbee6e963e12009-06-17 16:56:19 -07003517 if (cUnit->executionCount) {
3518 /*
3519 * Reserve 6 bytes at the beginning of the trace
3520 * +----------------------------+
3521 * | execution count (4 bytes) |
3522 * +----------------------------+
3523 * | chain cell offset (2 bytes)|
3524 * +----------------------------+
3525 * ...and then code to increment the execution
3526 * count:
3527 * mov r0, pc @ move adr of "mov r0,pc" + 4 to r0
3528 * sub r0, #10 @ back up to addr of executionCount
3529 * ldr r1, [r0]
3530 * add r1, #1
3531 * str r1, [r0]
3532 */
Bill Buzbee1465db52009-09-23 17:17:35 -07003533 newLIR1(cUnit, kArm16BitData, 0);
3534 newLIR1(cUnit, kArm16BitData, 0);
Ben Chengcc6600c2009-06-22 14:45:16 -07003535 cUnit->chainCellOffsetLIR =
Bill Buzbee1465db52009-09-23 17:17:35 -07003536 (LIR *) newLIR1(cUnit, kArm16BitData, CHAIN_CELL_OFFSET_TAG);
Bill Buzbee6e963e12009-06-17 16:56:19 -07003537 cUnit->headerSize = 6;
Bill Buzbee270c1d62009-08-13 16:58:07 -07003538 /* Thumb instruction used directly here to ensure correct size */
Bill Buzbee1465db52009-09-23 17:17:35 -07003539 newLIR2(cUnit, kThumbMovRR_H2L, r0, rpc);
3540 newLIR2(cUnit, kThumbSubRI8, r0, 10);
3541 newLIR3(cUnit, kThumbLdrRRI5, r1, r0, 0);
3542 newLIR2(cUnit, kThumbAddRI8, r1, 1);
3543 newLIR3(cUnit, kThumbStrRRI5, r1, r0, 0);
Bill Buzbee6e963e12009-06-17 16:56:19 -07003544 } else {
3545 /* Just reserve 2 bytes for the chain cell offset */
Ben Chengcc6600c2009-06-22 14:45:16 -07003546 cUnit->chainCellOffsetLIR =
Bill Buzbee1465db52009-09-23 17:17:35 -07003547 (LIR *) newLIR1(cUnit, kArm16BitData, CHAIN_CELL_OFFSET_TAG);
Bill Buzbee6e963e12009-06-17 16:56:19 -07003548 cUnit->headerSize = 2;
3549 }
Ben Cheng1efc9c52009-06-08 18:25:27 -07003550
Ben Chengba4fc8b2009-06-01 13:00:29 -07003551 /* Handle the content in each basic block */
3552 for (i = 0; i < cUnit->numBlocks; i++) {
3553 blockList[i]->visited = true;
3554 MIR *mir;
3555
3556 labelList[i].operands[0] = blockList[i]->startOffset;
3557
Ben Chengcec26f62010-01-15 15:29:33 -08003558 if (blockList[i]->blockType >= kChainingCellGap) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07003559 /*
3560 * Append the label pseudo LIR first. Chaining cells will be handled
3561 * separately afterwards.
3562 */
3563 dvmCompilerAppendLIR(cUnit, (LIR *) &labelList[i]);
3564 }
3565
Bill Buzbee1465db52009-09-23 17:17:35 -07003566 if (blockList[i]->blockType == kEntryBlock) {
Ben Chenga4973592010-03-31 11:59:18 -07003567 labelList[i].opCode = kArmPseudoEntryBlock;
Ben Cheng4238ec22009-08-24 16:32:22 -07003568 if (blockList[i]->firstMIRInsn == NULL) {
3569 continue;
3570 } else {
3571 setupLoopEntryBlock(cUnit, blockList[i],
3572 &labelList[blockList[i]->fallThrough->id]);
3573 }
Bill Buzbee1465db52009-09-23 17:17:35 -07003574 } else if (blockList[i]->blockType == kExitBlock) {
Ben Chenga4973592010-03-31 11:59:18 -07003575 labelList[i].opCode = kArmPseudoExitBlock;
Ben Cheng4238ec22009-08-24 16:32:22 -07003576 goto gen_fallthrough;
Bill Buzbee1465db52009-09-23 17:17:35 -07003577 } else if (blockList[i]->blockType == kDalvikByteCode) {
3578 labelList[i].opCode = kArmPseudoNormalBlockLabel;
Ben Chenge9695e52009-06-16 16:11:47 -07003579 /* Reset the register state */
Bill Buzbeec6f10662010-02-09 11:16:15 -08003580 dvmCompilerResetRegPool(cUnit);
3581 dvmCompilerClobberAllRegs(cUnit);
3582 dvmCompilerResetNullCheck(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003583 } else {
3584 switch (blockList[i]->blockType) {
Bill Buzbee1465db52009-09-23 17:17:35 -07003585 case kChainingCellNormal:
Ben Chenga4973592010-03-31 11:59:18 -07003586 labelList[i].opCode = kArmPseudoChainingCellNormal;
Ben Chengba4fc8b2009-06-01 13:00:29 -07003587 /* handle the codegen later */
3588 dvmInsertGrowableList(
Bill Buzbee1465db52009-09-23 17:17:35 -07003589 &chainingListByType[kChainingCellNormal], (void *) i);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003590 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07003591 case kChainingCellInvokeSingleton:
Ben Cheng38329f52009-07-07 14:19:20 -07003592 labelList[i].opCode =
Ben Chenga4973592010-03-31 11:59:18 -07003593 kArmPseudoChainingCellInvokeSingleton;
Ben Chengba4fc8b2009-06-01 13:00:29 -07003594 labelList[i].operands[0] =
3595 (int) blockList[i]->containingMethod;
3596 /* handle the codegen later */
3597 dvmInsertGrowableList(
Bill Buzbee1465db52009-09-23 17:17:35 -07003598 &chainingListByType[kChainingCellInvokeSingleton],
Ben Cheng38329f52009-07-07 14:19:20 -07003599 (void *) i);
3600 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07003601 case kChainingCellInvokePredicted:
Ben Cheng38329f52009-07-07 14:19:20 -07003602 labelList[i].opCode =
Ben Chenga4973592010-03-31 11:59:18 -07003603 kArmPseudoChainingCellInvokePredicted;
Ben Cheng38329f52009-07-07 14:19:20 -07003604 /* handle the codegen later */
3605 dvmInsertGrowableList(
Bill Buzbee1465db52009-09-23 17:17:35 -07003606 &chainingListByType[kChainingCellInvokePredicted],
Ben Cheng38329f52009-07-07 14:19:20 -07003607 (void *) i);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003608 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07003609 case kChainingCellHot:
Ben Chengba4fc8b2009-06-01 13:00:29 -07003610 labelList[i].opCode =
Ben Chenga4973592010-03-31 11:59:18 -07003611 kArmPseudoChainingCellHot;
Ben Chengba4fc8b2009-06-01 13:00:29 -07003612 /* handle the codegen later */
3613 dvmInsertGrowableList(
Bill Buzbee1465db52009-09-23 17:17:35 -07003614 &chainingListByType[kChainingCellHot],
Ben Chengba4fc8b2009-06-01 13:00:29 -07003615 (void *) i);
3616 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07003617 case kPCReconstruction:
Ben Chengba4fc8b2009-06-01 13:00:29 -07003618 /* Make sure exception handling block is next */
3619 labelList[i].opCode =
Ben Chenga4973592010-03-31 11:59:18 -07003620 kArmPseudoPCReconstructionBlockLabel;
Ben Chengba4fc8b2009-06-01 13:00:29 -07003621 assert (i == cUnit->numBlocks - 2);
3622 handlePCReconstruction(cUnit, &labelList[i+1]);
3623 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07003624 case kExceptionHandling:
3625 labelList[i].opCode = kArmPseudoEHBlockLabel;
Ben Chengba4fc8b2009-06-01 13:00:29 -07003626 if (cUnit->pcReconstructionList.numUsed) {
Bill Buzbee270c1d62009-08-13 16:58:07 -07003627 loadWordDisp(cUnit, rGLUE, offsetof(InterpState,
3628 jitToInterpEntries.dvmJitToInterpPunt),
3629 r1);
Bill Buzbee1465db52009-09-23 17:17:35 -07003630 opReg(cUnit, kOpBlx, r1);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003631 }
3632 break;
Bill Buzbee9c4b7c82009-09-10 10:10:38 -07003633#if defined(WITH_SELF_VERIFICATION) || defined(WITH_JIT_TUNING)
Bill Buzbee1465db52009-09-23 17:17:35 -07003634 case kChainingCellBackwardBranch:
Jeff Hao97319a82009-08-12 16:57:15 -07003635 labelList[i].opCode =
Ben Chenga4973592010-03-31 11:59:18 -07003636 kArmPseudoChainingCellBackwardBranch;
Jeff Hao97319a82009-08-12 16:57:15 -07003637 /* handle the codegen later */
3638 dvmInsertGrowableList(
Bill Buzbee1465db52009-09-23 17:17:35 -07003639 &chainingListByType[kChainingCellBackwardBranch],
Jeff Hao97319a82009-08-12 16:57:15 -07003640 (void *) i);
3641 break;
3642#endif
Ben Chengba4fc8b2009-06-01 13:00:29 -07003643 default:
3644 break;
3645 }
3646 continue;
3647 }
Ben Chenge9695e52009-06-16 16:11:47 -07003648
Bill Buzbee89efc3d2009-07-28 11:22:22 -07003649 ArmLIR *headLIR = NULL;
Ben Chenge9695e52009-06-16 16:11:47 -07003650
Ben Chengba4fc8b2009-06-01 13:00:29 -07003651 for (mir = blockList[i]->firstMIRInsn; mir; mir = mir->next) {
Bill Buzbee1465db52009-09-23 17:17:35 -07003652
Bill Buzbeec6f10662010-02-09 11:16:15 -08003653 dvmCompilerResetRegPool(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07003654 if (gDvmJit.disableOpt & (1 << kTrackLiveTemps)) {
Bill Buzbeec6f10662010-02-09 11:16:15 -08003655 dvmCompilerClobberAllRegs(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07003656 }
3657
3658 if (gDvmJit.disableOpt & (1 << kSuppressLoads)) {
Bill Buzbeec6f10662010-02-09 11:16:15 -08003659 dvmCompilerResetDefTracking(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07003660 }
3661
3662 if (mir->dalvikInsn.opCode >= kMirOpFirst) {
Ben Cheng4238ec22009-08-24 16:32:22 -07003663 handleExtendedMIR(cUnit, mir);
3664 continue;
3665 }
3666
Bill Buzbee1465db52009-09-23 17:17:35 -07003667
Ben Chengba4fc8b2009-06-01 13:00:29 -07003668 OpCode dalvikOpCode = mir->dalvikInsn.opCode;
3669 InstructionFormat dalvikFormat =
3670 dexGetInstrFormat(gDvm.instrFormat, dalvikOpCode);
Bill Buzbee89efc3d2009-07-28 11:22:22 -07003671 ArmLIR *boundaryLIR =
Ben Chenga4973592010-03-31 11:59:18 -07003672 newLIR2(cUnit, kArmPseudoDalvikByteCodeBoundary,
Ben Chengccd6c012009-10-15 14:52:45 -07003673 mir->offset,
3674 (int) dvmCompilerGetDalvikDisassembly(&mir->dalvikInsn)
3675 );
Ben Cheng4238ec22009-08-24 16:32:22 -07003676 if (mir->ssaRep) {
3677 char *ssaString = dvmCompilerGetSSAString(cUnit, mir->ssaRep);
Bill Buzbee1465db52009-09-23 17:17:35 -07003678 newLIR1(cUnit, kArmPseudoSSARep, (int) ssaString);
Ben Cheng4238ec22009-08-24 16:32:22 -07003679 }
3680
Ben Chenge9695e52009-06-16 16:11:47 -07003681 /* Remember the first LIR for this block */
3682 if (headLIR == NULL) {
3683 headLIR = boundaryLIR;
Ben Chengd7d426a2009-09-22 11:23:36 -07003684 /* Set the first boundaryLIR as a scheduling barrier */
3685 headLIR->defMask = ENCODE_ALL;
Ben Chenge9695e52009-06-16 16:11:47 -07003686 }
Ben Cheng4238ec22009-08-24 16:32:22 -07003687
Ben Chengba4fc8b2009-06-01 13:00:29 -07003688 bool notHandled;
3689 /*
3690 * Debugging: screen the opcode first to see if it is in the
3691 * do[-not]-compile list
3692 */
3693 bool singleStepMe =
3694 gDvmJit.includeSelectedOp !=
3695 ((gDvmJit.opList[dalvikOpCode >> 3] &
3696 (1 << (dalvikOpCode & 0x7))) !=
3697 0);
Ben Chengd5adae12010-03-26 17:45:28 -07003698#if defined(WITH_SELF_VERIFICATION)
3699 if (singleStepMe == false) {
3700 singleStepMe = selfVerificationPuntOps(mir);
3701 }
3702#endif
Ben Chengba4fc8b2009-06-01 13:00:29 -07003703 if (singleStepMe || cUnit->allSingleStep) {
3704 notHandled = false;
3705 genInterpSingleStep(cUnit, mir);
3706 } else {
3707 opcodeCoverage[dalvikOpCode]++;
3708 switch (dalvikFormat) {
3709 case kFmt10t:
3710 case kFmt20t:
3711 case kFmt30t:
3712 notHandled = handleFmt10t_Fmt20t_Fmt30t(cUnit,
3713 mir, blockList[i], labelList);
3714 break;
3715 case kFmt10x:
3716 notHandled = handleFmt10x(cUnit, mir);
3717 break;
3718 case kFmt11n:
3719 case kFmt31i:
3720 notHandled = handleFmt11n_Fmt31i(cUnit, mir);
3721 break;
3722 case kFmt11x:
3723 notHandled = handleFmt11x(cUnit, mir);
3724 break;
3725 case kFmt12x:
3726 notHandled = handleFmt12x(cUnit, mir);
3727 break;
3728 case kFmt20bc:
3729 notHandled = handleFmt20bc(cUnit, mir);
3730 break;
3731 case kFmt21c:
3732 case kFmt31c:
3733 notHandled = handleFmt21c_Fmt31c(cUnit, mir);
3734 break;
3735 case kFmt21h:
3736 notHandled = handleFmt21h(cUnit, mir);
3737 break;
3738 case kFmt21s:
3739 notHandled = handleFmt21s(cUnit, mir);
3740 break;
3741 case kFmt21t:
3742 notHandled = handleFmt21t(cUnit, mir, blockList[i],
3743 labelList);
3744 break;
3745 case kFmt22b:
3746 case kFmt22s:
3747 notHandled = handleFmt22b_Fmt22s(cUnit, mir);
3748 break;
3749 case kFmt22c:
3750 notHandled = handleFmt22c(cUnit, mir);
3751 break;
3752 case kFmt22cs:
3753 notHandled = handleFmt22cs(cUnit, mir);
3754 break;
3755 case kFmt22t:
3756 notHandled = handleFmt22t(cUnit, mir, blockList[i],
3757 labelList);
3758 break;
3759 case kFmt22x:
3760 case kFmt32x:
3761 notHandled = handleFmt22x_Fmt32x(cUnit, mir);
3762 break;
3763 case kFmt23x:
3764 notHandled = handleFmt23x(cUnit, mir);
3765 break;
3766 case kFmt31t:
3767 notHandled = handleFmt31t(cUnit, mir);
3768 break;
3769 case kFmt3rc:
3770 case kFmt35c:
3771 notHandled = handleFmt35c_3rc(cUnit, mir, blockList[i],
3772 labelList);
3773 break;
3774 case kFmt3rms:
3775 case kFmt35ms:
3776 notHandled = handleFmt35ms_3rms(cUnit, mir,blockList[i],
3777 labelList);
3778 break;
3779 case kFmt3inline:
Andy McFaddenb0a05412009-11-19 10:23:41 -08003780 case kFmt3rinline:
Bill Buzbeece46c942009-11-20 15:41:34 -08003781 notHandled = handleExecuteInline(cUnit, mir);
Andy McFaddenb0a05412009-11-19 10:23:41 -08003782 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07003783 case kFmt51l:
3784 notHandled = handleFmt51l(cUnit, mir);
3785 break;
3786 default:
3787 notHandled = true;
3788 break;
3789 }
3790 }
3791 if (notHandled) {
3792 LOGE("%#06x: Opcode 0x%x (%s) / Fmt %d not handled\n",
3793 mir->offset,
3794 dalvikOpCode, getOpcodeName(dalvikOpCode),
3795 dalvikFormat);
Bill Buzbeefc519dc2010-03-06 23:30:57 -08003796 dvmCompilerAbort(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003797 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07003798 }
3799 }
Ben Cheng4238ec22009-08-24 16:32:22 -07003800
Bill Buzbee1465db52009-09-23 17:17:35 -07003801 if (blockList[i]->blockType == kEntryBlock) {
Ben Cheng4238ec22009-08-24 16:32:22 -07003802 dvmCompilerAppendLIR(cUnit,
3803 (LIR *) cUnit->loopAnalysis->branchToBody);
3804 dvmCompilerAppendLIR(cUnit,
3805 (LIR *) cUnit->loopAnalysis->branchToPCR);
3806 }
3807
3808 if (headLIR) {
3809 /*
3810 * Eliminate redundant loads/stores and delay stores into later
3811 * slots
3812 */
3813 dvmCompilerApplyLocalOptimizations(cUnit, (LIR *) headLIR,
3814 cUnit->lastLIRInsn);
3815 }
3816
3817gen_fallthrough:
Ben Cheng1efc9c52009-06-08 18:25:27 -07003818 /*
3819 * Check if the block is terminated due to trace length constraint -
3820 * insert an unconditional branch to the chaining cell.
3821 */
3822 if (blockList[i]->needFallThroughBranch) {
3823 genUnconditionalBranch(cUnit,
3824 &labelList[blockList[i]->fallThrough->id]);
3825 }
3826
Ben Chengba4fc8b2009-06-01 13:00:29 -07003827 }
3828
Ben Chenge9695e52009-06-16 16:11:47 -07003829 /* Handle the chaining cells in predefined order */
Ben Chengcec26f62010-01-15 15:29:33 -08003830 for (i = 0; i < kChainingCellGap; i++) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07003831 size_t j;
3832 int *blockIdList = (int *) chainingListByType[i].elemList;
3833
3834 cUnit->numChainingCells[i] = chainingListByType[i].numUsed;
3835
3836 /* No chaining cells of this type */
3837 if (cUnit->numChainingCells[i] == 0)
3838 continue;
3839
3840 /* Record the first LIR for a new type of chaining cell */
3841 cUnit->firstChainingLIR[i] = (LIR *) &labelList[blockIdList[0]];
3842
3843 for (j = 0; j < chainingListByType[i].numUsed; j++) {
3844 int blockId = blockIdList[j];
3845
3846 /* Align this chaining cell first */
Bill Buzbee1465db52009-09-23 17:17:35 -07003847 newLIR0(cUnit, kArmPseudoPseudoAlign4);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003848
3849 /* Insert the pseudo chaining instruction */
3850 dvmCompilerAppendLIR(cUnit, (LIR *) &labelList[blockId]);
3851
3852
3853 switch (blockList[blockId]->blockType) {
Bill Buzbee1465db52009-09-23 17:17:35 -07003854 case kChainingCellNormal:
Ben Cheng1efc9c52009-06-08 18:25:27 -07003855 handleNormalChainingCell(cUnit,
Ben Chengba4fc8b2009-06-01 13:00:29 -07003856 blockList[blockId]->startOffset);
3857 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07003858 case kChainingCellInvokeSingleton:
Ben Cheng38329f52009-07-07 14:19:20 -07003859 handleInvokeSingletonChainingCell(cUnit,
Ben Chengba4fc8b2009-06-01 13:00:29 -07003860 blockList[blockId]->containingMethod);
3861 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07003862 case kChainingCellInvokePredicted:
Ben Cheng38329f52009-07-07 14:19:20 -07003863 handleInvokePredictedChainingCell(cUnit);
3864 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07003865 case kChainingCellHot:
Ben Cheng1efc9c52009-06-08 18:25:27 -07003866 handleHotChainingCell(cUnit,
Ben Chengba4fc8b2009-06-01 13:00:29 -07003867 blockList[blockId]->startOffset);
3868 break;
Bill Buzbee9c4b7c82009-09-10 10:10:38 -07003869#if defined(WITH_SELF_VERIFICATION) || defined(WITH_JIT_TUNING)
Bill Buzbee1465db52009-09-23 17:17:35 -07003870 case kChainingCellBackwardBranch:
Jeff Hao97319a82009-08-12 16:57:15 -07003871 handleBackwardBranchChainingCell(cUnit,
3872 blockList[blockId]->startOffset);
3873 break;
3874#endif
Ben Chengba4fc8b2009-06-01 13:00:29 -07003875 default:
Bill Buzbee1465db52009-09-23 17:17:35 -07003876 LOGE("Bad blocktype %d", blockList[blockId]->blockType);
Bill Buzbeefc519dc2010-03-06 23:30:57 -08003877 dvmCompilerAbort(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003878 }
3879 }
3880 }
Ben Chenge9695e52009-06-16 16:11:47 -07003881
Ben Chengcec26f62010-01-15 15:29:33 -08003882 /* Mark the bottom of chaining cells */
3883 cUnit->chainingCellBottom = (LIR *) newLIR0(cUnit, kArmChainingCellBottom);
3884
Ben Cheng6c10a972009-10-29 14:39:18 -07003885 /*
3886 * Generate the branch to the dvmJitToInterpNoChain entry point at the end
3887 * of all chaining cells for the overflow cases.
3888 */
3889 if (cUnit->switchOverflowPad) {
3890 loadConstant(cUnit, r0, (int) cUnit->switchOverflowPad);
3891 loadWordDisp(cUnit, rGLUE, offsetof(InterpState,
3892 jitToInterpEntries.dvmJitToInterpNoChain), r2);
3893 opRegReg(cUnit, kOpAdd, r1, r1);
3894 opRegRegReg(cUnit, kOpAdd, r4PC, r0, r1);
Ben Cheng86717f72010-03-05 15:27:21 -08003895#if defined(JIT_STATS)
Ben Cheng6c10a972009-10-29 14:39:18 -07003896 loadConstant(cUnit, r0, kSwitchOverflow);
3897#endif
3898 opReg(cUnit, kOpBlx, r2);
3899 }
3900
Ben Chenge9695e52009-06-16 16:11:47 -07003901 dvmCompilerApplyGlobalOptimizations(cUnit);
jeffhao9e45c0b2010-02-03 10:24:05 -08003902
3903#if defined(WITH_SELF_VERIFICATION)
3904 selfVerificationBranchInsertPass(cUnit);
3905#endif
Ben Chengba4fc8b2009-06-01 13:00:29 -07003906}
3907
3908/* Accept the work and start compiling */
Bill Buzbee716f1202009-07-23 13:22:09 -07003909bool dvmCompilerDoWork(CompilerWorkOrder *work)
Ben Chengba4fc8b2009-06-01 13:00:29 -07003910{
Ben Chengccd6c012009-10-15 14:52:45 -07003911 bool res;
Ben Chengba4fc8b2009-06-01 13:00:29 -07003912
Ben Cheng6999d842010-01-26 16:46:15 -08003913 if (gDvmJit.codeCacheFull) {
Ben Chengccd6c012009-10-15 14:52:45 -07003914 return false;
3915 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07003916
Ben Chengccd6c012009-10-15 14:52:45 -07003917 switch (work->kind) {
3918 case kWorkOrderMethod:
3919 res = dvmCompileMethod(work->info, &work->result);
3920 break;
3921 case kWorkOrderTrace:
3922 /* Start compilation with maximally allowed trace length */
Bill Buzbeefc519dc2010-03-06 23:30:57 -08003923 res = dvmCompileTrace(work->info, JIT_MAX_TRACE_LEN, &work->result,
3924 work->bailPtr);
Ben Chengccd6c012009-10-15 14:52:45 -07003925 break;
3926 case kWorkOrderTraceDebug: {
3927 bool oldPrintMe = gDvmJit.printMe;
3928 gDvmJit.printMe = true;
3929 /* Start compilation with maximally allowed trace length */
Bill Buzbeefc519dc2010-03-06 23:30:57 -08003930 res = dvmCompileTrace(work->info, JIT_MAX_TRACE_LEN, &work->result,
3931 work->bailPtr);
Ben Chengccd6c012009-10-15 14:52:45 -07003932 gDvmJit.printMe = oldPrintMe;;
3933 break;
3934 }
3935 default:
3936 res = false;
Bill Buzbeefc519dc2010-03-06 23:30:57 -08003937 LOGE("Jit: unknown work order type");
3938 assert(0); // Bail if debug build, discard oteherwise
Ben Chengccd6c012009-10-15 14:52:45 -07003939 }
3940 return res;
Ben Chengba4fc8b2009-06-01 13:00:29 -07003941}
3942
Ben Chengba4fc8b2009-06-01 13:00:29 -07003943/* Architectural-specific debugging helpers go here */
3944void dvmCompilerArchDump(void)
3945{
3946 /* Print compiled opcode in this VM instance */
3947 int i, start, streak;
3948 char buf[1024];
3949
3950 streak = i = 0;
3951 buf[0] = 0;
3952 while (opcodeCoverage[i] == 0 && i < 256) {
3953 i++;
3954 }
3955 if (i == 256) {
3956 return;
3957 }
3958 for (start = i++, streak = 1; i < 256; i++) {
3959 if (opcodeCoverage[i]) {
3960 streak++;
3961 } else {
3962 if (streak == 1) {
3963 sprintf(buf+strlen(buf), "%x,", start);
3964 } else {
3965 sprintf(buf+strlen(buf), "%x-%x,", start, start + streak - 1);
3966 }
3967 streak = 0;
3968 while (opcodeCoverage[i] == 0 && i < 256) {
3969 i++;
3970 }
3971 if (i < 256) {
3972 streak = 1;
3973 start = i;
3974 }
3975 }
3976 }
3977 if (streak) {
3978 if (streak == 1) {
3979 sprintf(buf+strlen(buf), "%x", start);
3980 } else {
3981 sprintf(buf+strlen(buf), "%x-%x", start, start + streak - 1);
3982 }
3983 }
3984 if (strlen(buf)) {
Ben Cheng8b258bf2009-06-24 17:27:07 -07003985 LOGD("dalvik.vm.jit.op = %s", buf);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003986 }
3987}
Ben Chengd7d426a2009-09-22 11:23:36 -07003988
3989/* Common initialization routine for an architecture family */
3990bool dvmCompilerArchInit()
3991{
3992 int i;
3993
Bill Buzbee1465db52009-09-23 17:17:35 -07003994 for (i = 0; i < kArmLast; i++) {
Ben Chengd7d426a2009-09-22 11:23:36 -07003995 if (EncodingMap[i].opCode != i) {
3996 LOGE("Encoding order for %s is wrong: expecting %d, seeing %d",
3997 EncodingMap[i].name, i, EncodingMap[i].opCode);
Bill Buzbeefc519dc2010-03-06 23:30:57 -08003998 dvmAbort(); // OK to dvmAbort - build error
Ben Chengd7d426a2009-09-22 11:23:36 -07003999 }
4000 }
4001
Ben Cheng5d90c202009-11-22 23:31:11 -08004002 return dvmCompilerArchVariantInit();
4003}
4004
4005void *dvmCompilerGetInterpretTemplate()
4006{
4007 return (void*) ((int)gDvmJit.codeCache +
4008 templateEntryOffsets[TEMPLATE_INTERPRET]);
4009}
4010
4011/* Needed by the ld/st optmizatons */
4012ArmLIR* dvmCompilerRegCopyNoInsert(CompilationUnit *cUnit, int rDest, int rSrc)
4013{
4014 return genRegCopyNoInsert(cUnit, rDest, rSrc);
4015}
4016
4017/* Needed by the register allocator */
4018ArmLIR* dvmCompilerRegCopy(CompilationUnit *cUnit, int rDest, int rSrc)
4019{
4020 return genRegCopy(cUnit, rDest, rSrc);
4021}
4022
4023/* Needed by the register allocator */
4024void dvmCompilerRegCopyWide(CompilationUnit *cUnit, int destLo, int destHi,
4025 int srcLo, int srcHi)
4026{
4027 genRegCopyWide(cUnit, destLo, destHi, srcLo, srcHi);
4028}
4029
4030void dvmCompilerFlushRegImpl(CompilationUnit *cUnit, int rBase,
4031 int displacement, int rSrc, OpSize size)
4032{
4033 storeBaseDisp(cUnit, rBase, displacement, rSrc, size);
4034}
4035
4036void dvmCompilerFlushRegWideImpl(CompilationUnit *cUnit, int rBase,
4037 int displacement, int rSrcLo, int rSrcHi)
4038{
4039 storeBaseDispWide(cUnit, rBase, displacement, rSrcLo, rSrcHi);
Ben Chengd7d426a2009-09-22 11:23:36 -07004040}