blob: 8522563b36df4bd30fe45d4a808da635ac5dacf0 [file] [log] [blame]
Ben Chengba4fc8b2009-06-01 13:00:29 -07001/*
2 * Copyright (C) 2009 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Bill Buzbee50a6bf22009-07-08 13:08:04 -070017/*
18 * This file contains codegen and support common to all supported
19 * ARM variants. It is included by:
20 *
21 * Codegen-$(TARGET_ARCH_VARIANT).c
22 *
23 * which combines this common code with specific support found in the
24 * applicable directory below this one.
25 */
26
Ben Cheng5d90c202009-11-22 23:31:11 -080027static bool genConversionCall(CompilationUnit *cUnit, MIR *mir, void *funct,
28 int srcSize, int tgtSize)
29{
30 /*
31 * Don't optimize the register usage since it calls out to template
32 * functions
33 */
34 RegLocation rlSrc;
35 RegLocation rlDest;
Bill Buzbeec6f10662010-02-09 11:16:15 -080036 dvmCompilerFlushAllRegs(cUnit); /* Send everything to home location */
Ben Cheng5d90c202009-11-22 23:31:11 -080037 if (srcSize == 1) {
Bill Buzbeec6f10662010-02-09 11:16:15 -080038 rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
Ben Cheng5d90c202009-11-22 23:31:11 -080039 loadValueDirectFixed(cUnit, rlSrc, r0);
40 } else {
Bill Buzbeec6f10662010-02-09 11:16:15 -080041 rlSrc = dvmCompilerGetSrcWide(cUnit, mir, 0, 1);
Ben Cheng5d90c202009-11-22 23:31:11 -080042 loadValueDirectWideFixed(cUnit, rlSrc, r0, r1);
43 }
Ben Chengbd1326d2010-04-02 15:04:53 -070044 LOAD_FUNC_ADDR(cUnit, r2, (int)funct);
Ben Cheng5d90c202009-11-22 23:31:11 -080045 opReg(cUnit, kOpBlx, r2);
Elliott Hughes6a555132010-02-25 15:41:42 -080046 dvmCompilerClobberCallRegs(cUnit);
Ben Cheng5d90c202009-11-22 23:31:11 -080047 if (tgtSize == 1) {
48 RegLocation rlResult;
Bill Buzbeec6f10662010-02-09 11:16:15 -080049 rlDest = dvmCompilerGetDest(cUnit, mir, 0);
50 rlResult = dvmCompilerGetReturn(cUnit);
Ben Cheng5d90c202009-11-22 23:31:11 -080051 storeValue(cUnit, rlDest, rlResult);
52 } else {
53 RegLocation rlResult;
Bill Buzbeec6f10662010-02-09 11:16:15 -080054 rlDest = dvmCompilerGetDestWide(cUnit, mir, 0, 1);
55 rlResult = dvmCompilerGetReturnWide(cUnit);
Ben Cheng5d90c202009-11-22 23:31:11 -080056 storeValueWide(cUnit, rlDest, rlResult);
57 }
58 return false;
59}
Ben Chengba4fc8b2009-06-01 13:00:29 -070060
Ben Chengba4fc8b2009-06-01 13:00:29 -070061
Ben Cheng5d90c202009-11-22 23:31:11 -080062static bool genArithOpFloatPortable(CompilationUnit *cUnit, MIR *mir,
63 RegLocation rlDest, RegLocation rlSrc1,
64 RegLocation rlSrc2)
65{
66 RegLocation rlResult;
67 void* funct;
68
Ben Cheng5d90c202009-11-22 23:31:11 -080069 switch (mir->dalvikInsn.opCode) {
70 case OP_ADD_FLOAT_2ADDR:
71 case OP_ADD_FLOAT:
72 funct = (void*) __aeabi_fadd;
73 break;
74 case OP_SUB_FLOAT_2ADDR:
75 case OP_SUB_FLOAT:
76 funct = (void*) __aeabi_fsub;
77 break;
78 case OP_DIV_FLOAT_2ADDR:
79 case OP_DIV_FLOAT:
80 funct = (void*) __aeabi_fdiv;
81 break;
82 case OP_MUL_FLOAT_2ADDR:
83 case OP_MUL_FLOAT:
84 funct = (void*) __aeabi_fmul;
85 break;
86 case OP_REM_FLOAT_2ADDR:
87 case OP_REM_FLOAT:
88 funct = (void*) fmodf;
89 break;
90 case OP_NEG_FLOAT: {
91 genNegFloat(cUnit, rlDest, rlSrc1);
92 return false;
93 }
94 default:
95 return true;
96 }
Bill Buzbeec6f10662010-02-09 11:16:15 -080097 dvmCompilerFlushAllRegs(cUnit); /* Send everything to home location */
Ben Cheng5d90c202009-11-22 23:31:11 -080098 loadValueDirectFixed(cUnit, rlSrc1, r0);
99 loadValueDirectFixed(cUnit, rlSrc2, r1);
Ben Chengbd1326d2010-04-02 15:04:53 -0700100 LOAD_FUNC_ADDR(cUnit, r2, (int)funct);
Ben Cheng5d90c202009-11-22 23:31:11 -0800101 opReg(cUnit, kOpBlx, r2);
Elliott Hughes6a555132010-02-25 15:41:42 -0800102 dvmCompilerClobberCallRegs(cUnit);
Bill Buzbeec6f10662010-02-09 11:16:15 -0800103 rlResult = dvmCompilerGetReturn(cUnit);
Ben Cheng5d90c202009-11-22 23:31:11 -0800104 storeValue(cUnit, rlDest, rlResult);
105 return false;
106}
107
108static bool genArithOpDoublePortable(CompilationUnit *cUnit, MIR *mir,
109 RegLocation rlDest, RegLocation rlSrc1,
110 RegLocation rlSrc2)
111{
112 RegLocation rlResult;
113 void* funct;
114
Ben Cheng5d90c202009-11-22 23:31:11 -0800115 switch (mir->dalvikInsn.opCode) {
116 case OP_ADD_DOUBLE_2ADDR:
117 case OP_ADD_DOUBLE:
118 funct = (void*) __aeabi_dadd;
119 break;
120 case OP_SUB_DOUBLE_2ADDR:
121 case OP_SUB_DOUBLE:
122 funct = (void*) __aeabi_dsub;
123 break;
124 case OP_DIV_DOUBLE_2ADDR:
125 case OP_DIV_DOUBLE:
126 funct = (void*) __aeabi_ddiv;
127 break;
128 case OP_MUL_DOUBLE_2ADDR:
129 case OP_MUL_DOUBLE:
130 funct = (void*) __aeabi_dmul;
131 break;
132 case OP_REM_DOUBLE_2ADDR:
133 case OP_REM_DOUBLE:
134 funct = (void*) fmod;
135 break;
136 case OP_NEG_DOUBLE: {
137 genNegDouble(cUnit, rlDest, rlSrc1);
138 return false;
139 }
140 default:
141 return true;
142 }
Bill Buzbeec6f10662010-02-09 11:16:15 -0800143 dvmCompilerFlushAllRegs(cUnit); /* Send everything to home location */
Ben Chengbd1326d2010-04-02 15:04:53 -0700144 LOAD_FUNC_ADDR(cUnit, rlr, (int)funct);
Ben Cheng5d90c202009-11-22 23:31:11 -0800145 loadValueDirectWideFixed(cUnit, rlSrc1, r0, r1);
146 loadValueDirectWideFixed(cUnit, rlSrc2, r2, r3);
147 opReg(cUnit, kOpBlx, rlr);
Elliott Hughes6a555132010-02-25 15:41:42 -0800148 dvmCompilerClobberCallRegs(cUnit);
Bill Buzbeec6f10662010-02-09 11:16:15 -0800149 rlResult = dvmCompilerGetReturnWide(cUnit);
Ben Cheng5d90c202009-11-22 23:31:11 -0800150 storeValueWide(cUnit, rlDest, rlResult);
151 return false;
152}
153
154static bool genConversionPortable(CompilationUnit *cUnit, MIR *mir)
155{
156 OpCode opCode = mir->dalvikInsn.opCode;
157
Ben Cheng5d90c202009-11-22 23:31:11 -0800158 switch (opCode) {
159 case OP_INT_TO_FLOAT:
160 return genConversionCall(cUnit, mir, (void*)__aeabi_i2f, 1, 1);
161 case OP_FLOAT_TO_INT:
162 return genConversionCall(cUnit, mir, (void*)__aeabi_f2iz, 1, 1);
163 case OP_DOUBLE_TO_FLOAT:
164 return genConversionCall(cUnit, mir, (void*)__aeabi_d2f, 2, 1);
165 case OP_FLOAT_TO_DOUBLE:
166 return genConversionCall(cUnit, mir, (void*)__aeabi_f2d, 1, 2);
167 case OP_INT_TO_DOUBLE:
168 return genConversionCall(cUnit, mir, (void*)__aeabi_i2d, 1, 2);
169 case OP_DOUBLE_TO_INT:
170 return genConversionCall(cUnit, mir, (void*)__aeabi_d2iz, 2, 1);
171 case OP_FLOAT_TO_LONG:
172 return genConversionCall(cUnit, mir, (void*)dvmJitf2l, 1, 2);
173 case OP_LONG_TO_FLOAT:
174 return genConversionCall(cUnit, mir, (void*)__aeabi_l2f, 2, 1);
175 case OP_DOUBLE_TO_LONG:
176 return genConversionCall(cUnit, mir, (void*)dvmJitd2l, 2, 2);
177 case OP_LONG_TO_DOUBLE:
178 return genConversionCall(cUnit, mir, (void*)__aeabi_l2d, 2, 2);
179 default:
180 return true;
181 }
182 return false;
183}
Ben Chengba4fc8b2009-06-01 13:00:29 -0700184
Jeff Hao97319a82009-08-12 16:57:15 -0700185#if defined(WITH_SELF_VERIFICATION)
jeffhao9e45c0b2010-02-03 10:24:05 -0800186static void selfVerificationBranchInsert(LIR *currentLIR, ArmOpCode opCode,
187 int dest, int src1)
Jeff Hao97319a82009-08-12 16:57:15 -0700188{
jeffhao9e45c0b2010-02-03 10:24:05 -0800189 ArmLIR *insn = dvmCompilerNew(sizeof(ArmLIR), true);
190 insn->opCode = opCode;
191 insn->operands[0] = dest;
192 insn->operands[1] = src1;
193 setupResourceMasks(insn);
194 dvmCompilerInsertLIRBefore(currentLIR, (LIR *) insn);
Jeff Hao97319a82009-08-12 16:57:15 -0700195}
196
jeffhao9e45c0b2010-02-03 10:24:05 -0800197static void selfVerificationBranchInsertPass(CompilationUnit *cUnit)
Jeff Hao97319a82009-08-12 16:57:15 -0700198{
jeffhao9e45c0b2010-02-03 10:24:05 -0800199 ArmLIR *thisLIR;
200 ArmLIR *branchLIR = dvmCompilerNew(sizeof(ArmLIR), true);
201 TemplateOpCode opCode = TEMPLATE_MEM_OP_DECODE;
Jeff Hao97319a82009-08-12 16:57:15 -0700202
jeffhao9e45c0b2010-02-03 10:24:05 -0800203 for (thisLIR = (ArmLIR *) cUnit->firstLIRInsn;
204 thisLIR != (ArmLIR *) cUnit->lastLIRInsn;
205 thisLIR = NEXT_LIR(thisLIR)) {
206 if (thisLIR->branchInsertSV) {
207 /* Branch to mem op decode template */
208 selfVerificationBranchInsert((LIR *) thisLIR, kThumbBlx1,
209 (int) gDvmJit.codeCache + templateEntryOffsets[opCode],
210 (int) gDvmJit.codeCache + templateEntryOffsets[opCode]);
211 selfVerificationBranchInsert((LIR *) thisLIR, kThumbBlx2,
212 (int) gDvmJit.codeCache + templateEntryOffsets[opCode],
213 (int) gDvmJit.codeCache + templateEntryOffsets[opCode]);
Jeff Hao97319a82009-08-12 16:57:15 -0700214 }
215 }
Jeff Hao97319a82009-08-12 16:57:15 -0700216}
Jeff Hao97319a82009-08-12 16:57:15 -0700217#endif
218
Bill Buzbeebe6534f2010-03-12 16:01:35 -0800219/* Generate conditional branch instructions */
220static ArmLIR *genConditionalBranch(CompilationUnit *cUnit,
221 ArmConditionCode cond,
222 ArmLIR *target)
223{
224 ArmLIR *branch = opCondBranch(cUnit, cond);
225 branch->generic.target = (LIR *) target;
226 return branch;
227}
228
Ben Chengba4fc8b2009-06-01 13:00:29 -0700229/* Generate a unconditional branch to go to the interpreter */
Bill Buzbee89efc3d2009-07-28 11:22:22 -0700230static inline ArmLIR *genTrap(CompilationUnit *cUnit, int dOffset,
231 ArmLIR *pcrLabel)
Ben Chengba4fc8b2009-06-01 13:00:29 -0700232{
Bill Buzbee1465db52009-09-23 17:17:35 -0700233 ArmLIR *branch = opNone(cUnit, kOpUncondBr);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700234 return genCheckCommon(cUnit, dOffset, branch, pcrLabel);
235}
236
237/* Load a wide field from an object instance */
238static void genIGetWide(CompilationUnit *cUnit, MIR *mir, int fieldOffset)
239{
240 DecodedInstruction *dInsn = &mir->dalvikInsn;
Bill Buzbeec6f10662010-02-09 11:16:15 -0800241 RegLocation rlObj = dvmCompilerGetSrc(cUnit, mir, 0);
242 RegLocation rlDest = dvmCompilerGetDestWide(cUnit, mir, 0, 1);
Bill Buzbee1465db52009-09-23 17:17:35 -0700243 RegLocation rlResult;
244 rlObj = loadValue(cUnit, rlObj, kCoreReg);
Bill Buzbeec6f10662010-02-09 11:16:15 -0800245 int regPtr = dvmCompilerAllocTemp(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700246
Bill Buzbee1465db52009-09-23 17:17:35 -0700247 assert(rlDest.wide);
Ben Chenge9695e52009-06-16 16:11:47 -0700248
Bill Buzbee1465db52009-09-23 17:17:35 -0700249 genNullCheck(cUnit, rlObj.sRegLow, rlObj.lowReg, mir->offset,
250 NULL);/* null object? */
251 opRegRegImm(cUnit, kOpAdd, regPtr, rlObj.lowReg, fieldOffset);
Bill Buzbeec6f10662010-02-09 11:16:15 -0800252 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kAnyReg, true);
Ben Cheng11d8f142010-03-24 15:24:19 -0700253
254 HEAP_ACCESS_SHADOW(true);
Bill Buzbee1465db52009-09-23 17:17:35 -0700255 loadPair(cUnit, regPtr, rlResult.lowReg, rlResult.highReg);
Ben Cheng11d8f142010-03-24 15:24:19 -0700256 HEAP_ACCESS_SHADOW(false);
257
Bill Buzbeec6f10662010-02-09 11:16:15 -0800258 dvmCompilerFreeTemp(cUnit, regPtr);
Bill Buzbee1465db52009-09-23 17:17:35 -0700259 storeValueWide(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700260}
261
262/* Store a wide field to an object instance */
263static void genIPutWide(CompilationUnit *cUnit, MIR *mir, int fieldOffset)
264{
265 DecodedInstruction *dInsn = &mir->dalvikInsn;
Bill Buzbeec6f10662010-02-09 11:16:15 -0800266 RegLocation rlSrc = dvmCompilerGetSrcWide(cUnit, mir, 0, 1);
267 RegLocation rlObj = dvmCompilerGetSrc(cUnit, mir, 2);
Bill Buzbee1465db52009-09-23 17:17:35 -0700268 rlObj = loadValue(cUnit, rlObj, kCoreReg);
269 int regPtr;
270 rlSrc = loadValueWide(cUnit, rlSrc, kAnyReg);
271 genNullCheck(cUnit, rlObj.sRegLow, rlObj.lowReg, mir->offset,
272 NULL);/* null object? */
Bill Buzbeec6f10662010-02-09 11:16:15 -0800273 regPtr = dvmCompilerAllocTemp(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -0700274 opRegRegImm(cUnit, kOpAdd, regPtr, rlObj.lowReg, fieldOffset);
Ben Cheng11d8f142010-03-24 15:24:19 -0700275
276 HEAP_ACCESS_SHADOW(true);
Bill Buzbee1465db52009-09-23 17:17:35 -0700277 storePair(cUnit, regPtr, rlSrc.lowReg, rlSrc.highReg);
Ben Cheng11d8f142010-03-24 15:24:19 -0700278 HEAP_ACCESS_SHADOW(false);
279
Bill Buzbeec6f10662010-02-09 11:16:15 -0800280 dvmCompilerFreeTemp(cUnit, regPtr);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700281}
282
283/*
284 * Load a field from an object instance
285 *
Ben Chengba4fc8b2009-06-01 13:00:29 -0700286 */
Bill Buzbee270c1d62009-08-13 16:58:07 -0700287static void genIGet(CompilationUnit *cUnit, MIR *mir, OpSize size,
Ben Chengba4fc8b2009-06-01 13:00:29 -0700288 int fieldOffset)
289{
Bill Buzbee1465db52009-09-23 17:17:35 -0700290 int regPtr;
291 RegLocation rlResult;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700292 DecodedInstruction *dInsn = &mir->dalvikInsn;
Bill Buzbeec6f10662010-02-09 11:16:15 -0800293 RegLocation rlObj = dvmCompilerGetSrc(cUnit, mir, 0);
294 RegLocation rlDest = dvmCompilerGetDest(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -0700295 rlObj = loadValue(cUnit, rlObj, kCoreReg);
Bill Buzbeec6f10662010-02-09 11:16:15 -0800296 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kAnyReg, true);
Bill Buzbee1465db52009-09-23 17:17:35 -0700297 genNullCheck(cUnit, rlObj.sRegLow, rlObj.lowReg, mir->offset,
298 NULL);/* null object? */
Ben Cheng11d8f142010-03-24 15:24:19 -0700299
300 HEAP_ACCESS_SHADOW(true);
Ben Cheng5d90c202009-11-22 23:31:11 -0800301 loadBaseDisp(cUnit, mir, rlObj.lowReg, fieldOffset, rlResult.lowReg,
302 size, rlObj.sRegLow);
Ben Cheng11d8f142010-03-24 15:24:19 -0700303 HEAP_ACCESS_SHADOW(false);
304
Bill Buzbee1465db52009-09-23 17:17:35 -0700305 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700306}
307
308/*
309 * Store a field to an object instance
310 *
Ben Chengba4fc8b2009-06-01 13:00:29 -0700311 */
Bill Buzbee270c1d62009-08-13 16:58:07 -0700312static void genIPut(CompilationUnit *cUnit, MIR *mir, OpSize size,
Ben Chengba4fc8b2009-06-01 13:00:29 -0700313 int fieldOffset)
314{
315 DecodedInstruction *dInsn = &mir->dalvikInsn;
Bill Buzbeec6f10662010-02-09 11:16:15 -0800316 RegLocation rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
317 RegLocation rlObj = dvmCompilerGetSrc(cUnit, mir, 1);
Bill Buzbee1465db52009-09-23 17:17:35 -0700318 rlObj = loadValue(cUnit, rlObj, kCoreReg);
319 rlSrc = loadValue(cUnit, rlSrc, kAnyReg);
320 int regPtr;
321 genNullCheck(cUnit, rlObj.sRegLow, rlObj.lowReg, mir->offset,
322 NULL);/* null object? */
Ben Cheng11d8f142010-03-24 15:24:19 -0700323
324 HEAP_ACCESS_SHADOW(true);
Bill Buzbee1465db52009-09-23 17:17:35 -0700325 storeBaseDisp(cUnit, rlObj.lowReg, fieldOffset, rlSrc.lowReg, size);
Ben Cheng11d8f142010-03-24 15:24:19 -0700326 HEAP_ACCESS_SHADOW(false);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700327}
328
329
Ben Chengba4fc8b2009-06-01 13:00:29 -0700330/*
331 * Generate array load
Ben Chengba4fc8b2009-06-01 13:00:29 -0700332 */
Bill Buzbee270c1d62009-08-13 16:58:07 -0700333static void genArrayGet(CompilationUnit *cUnit, MIR *mir, OpSize size,
Bill Buzbee1465db52009-09-23 17:17:35 -0700334 RegLocation rlArray, RegLocation rlIndex,
335 RegLocation rlDest, int scale)
Ben Chengba4fc8b2009-06-01 13:00:29 -0700336{
337 int lenOffset = offsetof(ArrayObject, length);
338 int dataOffset = offsetof(ArrayObject, contents);
Bill Buzbee1465db52009-09-23 17:17:35 -0700339 RegLocation rlResult;
340 rlArray = loadValue(cUnit, rlArray, kCoreReg);
341 rlIndex = loadValue(cUnit, rlIndex, kCoreReg);
342 int regPtr;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700343
344 /* null object? */
Ben Cheng4238ec22009-08-24 16:32:22 -0700345 ArmLIR * pcrLabel = NULL;
346
347 if (!(mir->OptimizationFlags & MIR_IGNORE_NULL_CHECK)) {
Bill Buzbee1465db52009-09-23 17:17:35 -0700348 pcrLabel = genNullCheck(cUnit, rlArray.sRegLow,
349 rlArray.lowReg, mir->offset, NULL);
Ben Cheng4238ec22009-08-24 16:32:22 -0700350 }
351
Bill Buzbeec6f10662010-02-09 11:16:15 -0800352 regPtr = dvmCompilerAllocTemp(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -0700353
Ben Cheng4238ec22009-08-24 16:32:22 -0700354 if (!(mir->OptimizationFlags & MIR_IGNORE_RANGE_CHECK)) {
Bill Buzbeec6f10662010-02-09 11:16:15 -0800355 int regLen = dvmCompilerAllocTemp(cUnit);
Ben Cheng4238ec22009-08-24 16:32:22 -0700356 /* Get len */
Bill Buzbee1465db52009-09-23 17:17:35 -0700357 loadWordDisp(cUnit, rlArray.lowReg, lenOffset, regLen);
358 /* regPtr -> array data */
359 opRegRegImm(cUnit, kOpAdd, regPtr, rlArray.lowReg, dataOffset);
360 genBoundsCheck(cUnit, rlIndex.lowReg, regLen, mir->offset,
361 pcrLabel);
Bill Buzbeec6f10662010-02-09 11:16:15 -0800362 dvmCompilerFreeTemp(cUnit, regLen);
Ben Cheng4238ec22009-08-24 16:32:22 -0700363 } else {
Bill Buzbee1465db52009-09-23 17:17:35 -0700364 /* regPtr -> array data */
365 opRegRegImm(cUnit, kOpAdd, regPtr, rlArray.lowReg, dataOffset);
Ben Cheng4238ec22009-08-24 16:32:22 -0700366 }
Bill Buzbee1465db52009-09-23 17:17:35 -0700367 if ((size == kLong) || (size == kDouble)) {
368 if (scale) {
Bill Buzbeec6f10662010-02-09 11:16:15 -0800369 int rNewIndex = dvmCompilerAllocTemp(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -0700370 opRegRegImm(cUnit, kOpLsl, rNewIndex, rlIndex.lowReg, scale);
371 opRegReg(cUnit, kOpAdd, regPtr, rNewIndex);
Bill Buzbeec6f10662010-02-09 11:16:15 -0800372 dvmCompilerFreeTemp(cUnit, rNewIndex);
Bill Buzbee1465db52009-09-23 17:17:35 -0700373 } else {
374 opRegReg(cUnit, kOpAdd, regPtr, rlIndex.lowReg);
375 }
Bill Buzbeec6f10662010-02-09 11:16:15 -0800376 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kAnyReg, true);
Ben Cheng11d8f142010-03-24 15:24:19 -0700377
378 HEAP_ACCESS_SHADOW(true);
Bill Buzbee1465db52009-09-23 17:17:35 -0700379 loadPair(cUnit, regPtr, rlResult.lowReg, rlResult.highReg);
Ben Cheng11d8f142010-03-24 15:24:19 -0700380 HEAP_ACCESS_SHADOW(false);
381
Bill Buzbeec6f10662010-02-09 11:16:15 -0800382 dvmCompilerFreeTemp(cUnit, regPtr);
Bill Buzbee1465db52009-09-23 17:17:35 -0700383 storeValueWide(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700384 } else {
Bill Buzbeec6f10662010-02-09 11:16:15 -0800385 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kAnyReg, true);
Ben Cheng11d8f142010-03-24 15:24:19 -0700386
387 HEAP_ACCESS_SHADOW(true);
Bill Buzbee1465db52009-09-23 17:17:35 -0700388 loadBaseIndexed(cUnit, regPtr, rlIndex.lowReg, rlResult.lowReg,
389 scale, size);
Ben Cheng11d8f142010-03-24 15:24:19 -0700390 HEAP_ACCESS_SHADOW(false);
391
Bill Buzbeec6f10662010-02-09 11:16:15 -0800392 dvmCompilerFreeTemp(cUnit, regPtr);
Bill Buzbee1465db52009-09-23 17:17:35 -0700393 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700394 }
395}
396
Ben Chengba4fc8b2009-06-01 13:00:29 -0700397/*
398 * Generate array store
399 *
Ben Chengba4fc8b2009-06-01 13:00:29 -0700400 */
Bill Buzbee270c1d62009-08-13 16:58:07 -0700401static void genArrayPut(CompilationUnit *cUnit, MIR *mir, OpSize size,
Bill Buzbee1465db52009-09-23 17:17:35 -0700402 RegLocation rlArray, RegLocation rlIndex,
403 RegLocation rlSrc, int scale)
Ben Chengba4fc8b2009-06-01 13:00:29 -0700404{
405 int lenOffset = offsetof(ArrayObject, length);
406 int dataOffset = offsetof(ArrayObject, contents);
407
Bill Buzbee1465db52009-09-23 17:17:35 -0700408 int regPtr;
409 rlArray = loadValue(cUnit, rlArray, kCoreReg);
410 rlIndex = loadValue(cUnit, rlIndex, kCoreReg);
Ben Chenge9695e52009-06-16 16:11:47 -0700411
Bill Buzbeec6f10662010-02-09 11:16:15 -0800412 if (dvmCompilerIsTemp(cUnit, rlArray.lowReg)) {
413 dvmCompilerClobber(cUnit, rlArray.lowReg);
Bill Buzbee1465db52009-09-23 17:17:35 -0700414 regPtr = rlArray.lowReg;
415 } else {
Bill Buzbeec6f10662010-02-09 11:16:15 -0800416 regPtr = dvmCompilerAllocTemp(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -0700417 genRegCopy(cUnit, regPtr, rlArray.lowReg);
418 }
Ben Chenge9695e52009-06-16 16:11:47 -0700419
Ben Cheng1efc9c52009-06-08 18:25:27 -0700420 /* null object? */
Ben Cheng4238ec22009-08-24 16:32:22 -0700421 ArmLIR * pcrLabel = NULL;
422
423 if (!(mir->OptimizationFlags & MIR_IGNORE_NULL_CHECK)) {
Bill Buzbee1465db52009-09-23 17:17:35 -0700424 pcrLabel = genNullCheck(cUnit, rlArray.sRegLow, rlArray.lowReg,
425 mir->offset, NULL);
Ben Cheng4238ec22009-08-24 16:32:22 -0700426 }
427
428 if (!(mir->OptimizationFlags & MIR_IGNORE_RANGE_CHECK)) {
Bill Buzbeec6f10662010-02-09 11:16:15 -0800429 int regLen = dvmCompilerAllocTemp(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -0700430 //NOTE: max live temps(4) here.
Ben Cheng4238ec22009-08-24 16:32:22 -0700431 /* Get len */
Bill Buzbee1465db52009-09-23 17:17:35 -0700432 loadWordDisp(cUnit, rlArray.lowReg, lenOffset, regLen);
433 /* regPtr -> array data */
434 opRegImm(cUnit, kOpAdd, regPtr, dataOffset);
435 genBoundsCheck(cUnit, rlIndex.lowReg, regLen, mir->offset,
436 pcrLabel);
Bill Buzbeec6f10662010-02-09 11:16:15 -0800437 dvmCompilerFreeTemp(cUnit, regLen);
Ben Cheng4238ec22009-08-24 16:32:22 -0700438 } else {
Bill Buzbee1465db52009-09-23 17:17:35 -0700439 /* regPtr -> array data */
440 opRegImm(cUnit, kOpAdd, regPtr, dataOffset);
Ben Cheng4238ec22009-08-24 16:32:22 -0700441 }
Bill Buzbee1465db52009-09-23 17:17:35 -0700442 /* at this point, regPtr points to array, 2 live temps */
Bill Buzbee1465db52009-09-23 17:17:35 -0700443 if ((size == kLong) || (size == kDouble)) {
444 //TODO: need specific wide routine that can handle fp regs
445 if (scale) {
Bill Buzbeec6f10662010-02-09 11:16:15 -0800446 int rNewIndex = dvmCompilerAllocTemp(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -0700447 opRegRegImm(cUnit, kOpLsl, rNewIndex, rlIndex.lowReg, scale);
448 opRegReg(cUnit, kOpAdd, regPtr, rNewIndex);
Bill Buzbeec6f10662010-02-09 11:16:15 -0800449 dvmCompilerFreeTemp(cUnit, rNewIndex);
Bill Buzbee1465db52009-09-23 17:17:35 -0700450 } else {
451 opRegReg(cUnit, kOpAdd, regPtr, rlIndex.lowReg);
452 }
453 rlSrc = loadValueWide(cUnit, rlSrc, kAnyReg);
Ben Cheng11d8f142010-03-24 15:24:19 -0700454
455 HEAP_ACCESS_SHADOW(true);
Bill Buzbee1465db52009-09-23 17:17:35 -0700456 storePair(cUnit, regPtr, rlSrc.lowReg, rlSrc.highReg);
Ben Cheng11d8f142010-03-24 15:24:19 -0700457 HEAP_ACCESS_SHADOW(false);
458
Bill Buzbeec6f10662010-02-09 11:16:15 -0800459 dvmCompilerFreeTemp(cUnit, regPtr);
Bill Buzbee270c1d62009-08-13 16:58:07 -0700460 } else {
Bill Buzbee1465db52009-09-23 17:17:35 -0700461 rlSrc = loadValue(cUnit, rlSrc, kAnyReg);
Ben Cheng11d8f142010-03-24 15:24:19 -0700462
463 HEAP_ACCESS_SHADOW(true);
Bill Buzbee1465db52009-09-23 17:17:35 -0700464 storeBaseIndexed(cUnit, regPtr, rlIndex.lowReg, rlSrc.lowReg,
465 scale, size);
Ben Cheng11d8f142010-03-24 15:24:19 -0700466 HEAP_ACCESS_SHADOW(false);
jeffhao9e45c0b2010-02-03 10:24:05 -0800467 }
Ben Chengba4fc8b2009-06-01 13:00:29 -0700468}
469
Bill Buzbeebe6534f2010-03-12 16:01:35 -0800470/*
471 * Generate array object store
472 * Must use explicit register allocation here because of
473 * call-out to dvmCanPutArrayElement
474 */
475static void genArrayObjectPut(CompilationUnit *cUnit, MIR *mir,
476 RegLocation rlArray, RegLocation rlIndex,
477 RegLocation rlSrc, int scale)
478{
479 int lenOffset = offsetof(ArrayObject, length);
480 int dataOffset = offsetof(ArrayObject, contents);
481
482 dvmCompilerFlushAllRegs(cUnit);
483
484 int regLen = r0;
485 int regPtr = r4PC; /* Preserved across call */
486 int regArray = r1;
487 int regIndex = r7; /* Preserved across call */
488
489 loadValueDirectFixed(cUnit, rlArray, regArray);
490 loadValueDirectFixed(cUnit, rlIndex, regIndex);
491
492 /* null object? */
493 ArmLIR * pcrLabel = NULL;
494
495 if (!(mir->OptimizationFlags & MIR_IGNORE_NULL_CHECK)) {
496 pcrLabel = genNullCheck(cUnit, rlArray.sRegLow, regArray,
497 mir->offset, NULL);
498 }
499
500 if (!(mir->OptimizationFlags & MIR_IGNORE_RANGE_CHECK)) {
501 /* Get len */
502 loadWordDisp(cUnit, regArray, lenOffset, regLen);
503 /* regPtr -> array data */
504 opRegRegImm(cUnit, kOpAdd, regPtr, regArray, dataOffset);
505 genBoundsCheck(cUnit, regIndex, regLen, mir->offset,
506 pcrLabel);
507 } else {
508 /* regPtr -> array data */
509 opRegRegImm(cUnit, kOpAdd, regPtr, regArray, dataOffset);
510 }
511
512 /* Get object to store */
513 loadValueDirectFixed(cUnit, rlSrc, r0);
Ben Chengbd1326d2010-04-02 15:04:53 -0700514 LOAD_FUNC_ADDR(cUnit, r2, (int)dvmCanPutArrayElement);
Bill Buzbeebe6534f2010-03-12 16:01:35 -0800515
516 /* Are we storing null? If so, avoid check */
517 opRegImm(cUnit, kOpCmp, r0, 0);
518 ArmLIR *branchOver = opCondBranch(cUnit, kArmCondEq);
519
520 /* Make sure the types are compatible */
521 loadWordDisp(cUnit, regArray, offsetof(Object, clazz), r1);
522 loadWordDisp(cUnit, r0, offsetof(Object, clazz), r0);
523 opReg(cUnit, kOpBlx, r2);
524 dvmCompilerClobberCallRegs(cUnit);
Bill Buzbee900a3af2010-03-16 12:41:43 -0700525
526 /*
527 * Using fixed registers here, and counting on r4 and r7 being
528 * preserved across the above call. Tell the register allocation
529 * utilities about the regs we are using directly
530 */
531 dvmCompilerLockTemp(cUnit, regPtr); // r4PC
532 dvmCompilerLockTemp(cUnit, regIndex); // r7
533 dvmCompilerLockTemp(cUnit, r0);
534
Bill Buzbeebe6534f2010-03-12 16:01:35 -0800535 /* Bad? - roll back and re-execute if so */
536 genRegImmCheck(cUnit, kArmCondEq, r0, 0, mir->offset, pcrLabel);
537
538 /* Resume here - must reload element, regPtr & index preserved */
539 loadValueDirectFixed(cUnit, rlSrc, r0);
540
541 ArmLIR *target = newLIR0(cUnit, kArmPseudoTargetLabel);
542 target->defMask = ENCODE_ALL;
543 branchOver->generic.target = (LIR *) target;
544
Ben Cheng11d8f142010-03-24 15:24:19 -0700545 HEAP_ACCESS_SHADOW(true);
Bill Buzbeebe6534f2010-03-12 16:01:35 -0800546 storeBaseIndexed(cUnit, regPtr, regIndex, r0,
547 scale, kWord);
Ben Cheng11d8f142010-03-24 15:24:19 -0700548 HEAP_ACCESS_SHADOW(false);
Bill Buzbeebe6534f2010-03-12 16:01:35 -0800549}
550
Ben Cheng5d90c202009-11-22 23:31:11 -0800551static bool genShiftOpLong(CompilationUnit *cUnit, MIR *mir,
552 RegLocation rlDest, RegLocation rlSrc1,
553 RegLocation rlShift)
Ben Chengba4fc8b2009-06-01 13:00:29 -0700554{
Ben Chenge9695e52009-06-16 16:11:47 -0700555 /*
556 * Don't mess with the regsiters here as there is a particular calling
557 * convention to the out-of-line handler.
558 */
Bill Buzbee1465db52009-09-23 17:17:35 -0700559 RegLocation rlResult;
560
561 loadValueDirectWideFixed(cUnit, rlSrc1, r0, r1);
562 loadValueDirect(cUnit, rlShift, r2);
Ben Chenge9695e52009-06-16 16:11:47 -0700563 switch( mir->dalvikInsn.opCode) {
564 case OP_SHL_LONG:
565 case OP_SHL_LONG_2ADDR:
566 genDispatchToHandler(cUnit, TEMPLATE_SHL_LONG);
567 break;
568 case OP_SHR_LONG:
569 case OP_SHR_LONG_2ADDR:
570 genDispatchToHandler(cUnit, TEMPLATE_SHR_LONG);
571 break;
572 case OP_USHR_LONG:
573 case OP_USHR_LONG_2ADDR:
574 genDispatchToHandler(cUnit, TEMPLATE_USHR_LONG);
575 break;
576 default:
577 return true;
578 }
Bill Buzbeec6f10662010-02-09 11:16:15 -0800579 rlResult = dvmCompilerGetReturnWide(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -0700580 storeValueWide(cUnit, rlDest, rlResult);
Ben Chenge9695e52009-06-16 16:11:47 -0700581 return false;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700582}
Ben Chenge9695e52009-06-16 16:11:47 -0700583
Ben Cheng5d90c202009-11-22 23:31:11 -0800584static bool genArithOpLong(CompilationUnit *cUnit, MIR *mir,
585 RegLocation rlDest, RegLocation rlSrc1,
586 RegLocation rlSrc2)
Ben Chengba4fc8b2009-06-01 13:00:29 -0700587{
Bill Buzbee1465db52009-09-23 17:17:35 -0700588 RegLocation rlResult;
589 OpKind firstOp = kOpBkpt;
590 OpKind secondOp = kOpBkpt;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700591 bool callOut = false;
592 void *callTgt;
593 int retReg = r0;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700594
595 switch (mir->dalvikInsn.opCode) {
596 case OP_NOT_LONG:
Bill Buzbee1465db52009-09-23 17:17:35 -0700597 rlSrc2 = loadValueWide(cUnit, rlSrc2, kCoreReg);
Bill Buzbeec6f10662010-02-09 11:16:15 -0800598 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Bill Buzbee1465db52009-09-23 17:17:35 -0700599 opRegReg(cUnit, kOpMvn, rlResult.lowReg, rlSrc2.lowReg);
600 opRegReg(cUnit, kOpMvn, rlResult.highReg, rlSrc2.highReg);
601 storeValueWide(cUnit, rlDest, rlResult);
602 return false;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700603 break;
604 case OP_ADD_LONG:
605 case OP_ADD_LONG_2ADDR:
Bill Buzbee1465db52009-09-23 17:17:35 -0700606 firstOp = kOpAdd;
607 secondOp = kOpAdc;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700608 break;
609 case OP_SUB_LONG:
610 case OP_SUB_LONG_2ADDR:
Bill Buzbee1465db52009-09-23 17:17:35 -0700611 firstOp = kOpSub;
612 secondOp = kOpSbc;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700613 break;
614 case OP_MUL_LONG:
615 case OP_MUL_LONG_2ADDR:
Bill Buzbee1465db52009-09-23 17:17:35 -0700616 genMulLong(cUnit, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700617 return false;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700618 case OP_DIV_LONG:
619 case OP_DIV_LONG_2ADDR:
620 callOut = true;
621 retReg = r0;
622 callTgt = (void*)__aeabi_ldivmod;
623 break;
624 /* NOTE - result is in r2/r3 instead of r0/r1 */
625 case OP_REM_LONG:
626 case OP_REM_LONG_2ADDR:
627 callOut = true;
628 callTgt = (void*)__aeabi_ldivmod;
629 retReg = r2;
630 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700631 case OP_AND_LONG_2ADDR:
Bill Buzbee1465db52009-09-23 17:17:35 -0700632 case OP_AND_LONG:
633 firstOp = kOpAnd;
634 secondOp = kOpAnd;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700635 break;
636 case OP_OR_LONG:
637 case OP_OR_LONG_2ADDR:
Bill Buzbee1465db52009-09-23 17:17:35 -0700638 firstOp = kOpOr;
639 secondOp = kOpOr;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700640 break;
641 case OP_XOR_LONG:
642 case OP_XOR_LONG_2ADDR:
Bill Buzbee1465db52009-09-23 17:17:35 -0700643 firstOp = kOpXor;
644 secondOp = kOpXor;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700645 break;
Ben Chenge9695e52009-06-16 16:11:47 -0700646 case OP_NEG_LONG: {
Bill Buzbee51ecf602010-01-14 14:27:52 -0800647 //TUNING: can improve this using Thumb2 code
Bill Buzbeec6f10662010-02-09 11:16:15 -0800648 int tReg = dvmCompilerAllocTemp(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -0700649 rlSrc2 = loadValueWide(cUnit, rlSrc2, kCoreReg);
Bill Buzbeec6f10662010-02-09 11:16:15 -0800650 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Ben Chengbd1326d2010-04-02 15:04:53 -0700651 loadConstantNoClobber(cUnit, tReg, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -0700652 opRegRegReg(cUnit, kOpSub, rlResult.lowReg,
Bill Buzbee51ecf602010-01-14 14:27:52 -0800653 tReg, rlSrc2.lowReg);
654 opRegReg(cUnit, kOpSbc, tReg, rlSrc2.highReg);
655 genRegCopy(cUnit, rlResult.highReg, tReg);
Bill Buzbee1465db52009-09-23 17:17:35 -0700656 storeValueWide(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700657 return false;
Ben Chenge9695e52009-06-16 16:11:47 -0700658 }
Ben Chengba4fc8b2009-06-01 13:00:29 -0700659 default:
660 LOGE("Invalid long arith op");
Bill Buzbeefc519dc2010-03-06 23:30:57 -0800661 dvmCompilerAbort(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700662 }
663 if (!callOut) {
Bill Buzbee80cef862010-03-25 10:38:34 -0700664 genLong3Addr(cUnit, mir, firstOp, secondOp, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700665 } else {
Bill Buzbee1465db52009-09-23 17:17:35 -0700666 // Adjust return regs in to handle case of rem returning r2/r3
Bill Buzbeec6f10662010-02-09 11:16:15 -0800667 dvmCompilerFlushAllRegs(cUnit); /* Send everything to home location */
Bill Buzbee1465db52009-09-23 17:17:35 -0700668 loadValueDirectWideFixed(cUnit, rlSrc1, r0, r1);
Ben Chengbd1326d2010-04-02 15:04:53 -0700669 LOAD_FUNC_ADDR(cUnit, rlr, (int) callTgt);
Bill Buzbee1465db52009-09-23 17:17:35 -0700670 loadValueDirectWideFixed(cUnit, rlSrc2, r2, r3);
671 opReg(cUnit, kOpBlx, rlr);
Elliott Hughes6a555132010-02-25 15:41:42 -0800672 dvmCompilerClobberCallRegs(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -0700673 if (retReg == r0)
Bill Buzbeec6f10662010-02-09 11:16:15 -0800674 rlResult = dvmCompilerGetReturnWide(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -0700675 else
Bill Buzbeec6f10662010-02-09 11:16:15 -0800676 rlResult = dvmCompilerGetReturnWideAlt(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -0700677 storeValueWide(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700678 }
679 return false;
680}
681
Ben Cheng5d90c202009-11-22 23:31:11 -0800682static bool genArithOpInt(CompilationUnit *cUnit, MIR *mir,
683 RegLocation rlDest, RegLocation rlSrc1,
684 RegLocation rlSrc2)
Ben Chengba4fc8b2009-06-01 13:00:29 -0700685{
Bill Buzbee1465db52009-09-23 17:17:35 -0700686 OpKind op = kOpBkpt;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700687 bool callOut = false;
688 bool checkZero = false;
Bill Buzbee1465db52009-09-23 17:17:35 -0700689 bool unary = false;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700690 int retReg = r0;
691 void *callTgt;
Bill Buzbee1465db52009-09-23 17:17:35 -0700692 RegLocation rlResult;
Bill Buzbee0e605272009-12-01 14:28:05 -0800693 bool shiftOp = false;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700694
Ben Chengba4fc8b2009-06-01 13:00:29 -0700695 switch (mir->dalvikInsn.opCode) {
696 case OP_NEG_INT:
Bill Buzbee1465db52009-09-23 17:17:35 -0700697 op = kOpNeg;
698 unary = true;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700699 break;
700 case OP_NOT_INT:
Bill Buzbee1465db52009-09-23 17:17:35 -0700701 op = kOpMvn;
702 unary = true;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700703 break;
704 case OP_ADD_INT:
705 case OP_ADD_INT_2ADDR:
Bill Buzbee1465db52009-09-23 17:17:35 -0700706 op = kOpAdd;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700707 break;
708 case OP_SUB_INT:
709 case OP_SUB_INT_2ADDR:
Bill Buzbee1465db52009-09-23 17:17:35 -0700710 op = kOpSub;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700711 break;
712 case OP_MUL_INT:
713 case OP_MUL_INT_2ADDR:
Bill Buzbee1465db52009-09-23 17:17:35 -0700714 op = kOpMul;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700715 break;
716 case OP_DIV_INT:
717 case OP_DIV_INT_2ADDR:
718 callOut = true;
719 checkZero = true;
720 callTgt = __aeabi_idiv;
721 retReg = r0;
722 break;
723 /* NOTE: returns in r1 */
724 case OP_REM_INT:
725 case OP_REM_INT_2ADDR:
726 callOut = true;
727 checkZero = true;
728 callTgt = __aeabi_idivmod;
729 retReg = r1;
730 break;
731 case OP_AND_INT:
732 case OP_AND_INT_2ADDR:
Bill Buzbee1465db52009-09-23 17:17:35 -0700733 op = kOpAnd;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700734 break;
735 case OP_OR_INT:
736 case OP_OR_INT_2ADDR:
Bill Buzbee1465db52009-09-23 17:17:35 -0700737 op = kOpOr;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700738 break;
739 case OP_XOR_INT:
740 case OP_XOR_INT_2ADDR:
Bill Buzbee1465db52009-09-23 17:17:35 -0700741 op = kOpXor;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700742 break;
743 case OP_SHL_INT:
744 case OP_SHL_INT_2ADDR:
Bill Buzbee0e605272009-12-01 14:28:05 -0800745 shiftOp = true;
Bill Buzbee1465db52009-09-23 17:17:35 -0700746 op = kOpLsl;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700747 break;
748 case OP_SHR_INT:
749 case OP_SHR_INT_2ADDR:
Bill Buzbee0e605272009-12-01 14:28:05 -0800750 shiftOp = true;
Bill Buzbee1465db52009-09-23 17:17:35 -0700751 op = kOpAsr;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700752 break;
753 case OP_USHR_INT:
754 case OP_USHR_INT_2ADDR:
Bill Buzbee0e605272009-12-01 14:28:05 -0800755 shiftOp = true;
Bill Buzbee1465db52009-09-23 17:17:35 -0700756 op = kOpLsr;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700757 break;
758 default:
759 LOGE("Invalid word arith op: 0x%x(%d)",
760 mir->dalvikInsn.opCode, mir->dalvikInsn.opCode);
Bill Buzbeefc519dc2010-03-06 23:30:57 -0800761 dvmCompilerAbort(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700762 }
763 if (!callOut) {
Bill Buzbee1465db52009-09-23 17:17:35 -0700764 rlSrc1 = loadValue(cUnit, rlSrc1, kCoreReg);
765 if (unary) {
Bill Buzbeec6f10662010-02-09 11:16:15 -0800766 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Bill Buzbee1465db52009-09-23 17:17:35 -0700767 opRegReg(cUnit, op, rlResult.lowReg,
768 rlSrc1.lowReg);
Ben Chenge9695e52009-06-16 16:11:47 -0700769 } else {
Bill Buzbee1465db52009-09-23 17:17:35 -0700770 rlSrc2 = loadValue(cUnit, rlSrc2, kCoreReg);
Bill Buzbee0e605272009-12-01 14:28:05 -0800771 if (shiftOp) {
Bill Buzbeec6f10662010-02-09 11:16:15 -0800772 int tReg = dvmCompilerAllocTemp(cUnit);
Bill Buzbee0e605272009-12-01 14:28:05 -0800773 opRegRegImm(cUnit, kOpAnd, tReg, rlSrc2.lowReg, 31);
Bill Buzbeec6f10662010-02-09 11:16:15 -0800774 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Bill Buzbee0e605272009-12-01 14:28:05 -0800775 opRegRegReg(cUnit, op, rlResult.lowReg,
776 rlSrc1.lowReg, tReg);
Bill Buzbeec6f10662010-02-09 11:16:15 -0800777 dvmCompilerFreeTemp(cUnit, tReg);
Bill Buzbee0e605272009-12-01 14:28:05 -0800778 } else {
Bill Buzbeec6f10662010-02-09 11:16:15 -0800779 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Bill Buzbee0e605272009-12-01 14:28:05 -0800780 opRegRegReg(cUnit, op, rlResult.lowReg,
781 rlSrc1.lowReg, rlSrc2.lowReg);
782 }
Ben Chenge9695e52009-06-16 16:11:47 -0700783 }
Bill Buzbee1465db52009-09-23 17:17:35 -0700784 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700785 } else {
Bill Buzbee1465db52009-09-23 17:17:35 -0700786 RegLocation rlResult;
Bill Buzbeec6f10662010-02-09 11:16:15 -0800787 dvmCompilerFlushAllRegs(cUnit); /* Send everything to home location */
Bill Buzbee1465db52009-09-23 17:17:35 -0700788 loadValueDirectFixed(cUnit, rlSrc2, r1);
Ben Chengbd1326d2010-04-02 15:04:53 -0700789 LOAD_FUNC_ADDR(cUnit, r2, (int) callTgt);
Bill Buzbee1465db52009-09-23 17:17:35 -0700790 loadValueDirectFixed(cUnit, rlSrc1, r0);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700791 if (checkZero) {
Bill Buzbee1465db52009-09-23 17:17:35 -0700792 genNullCheck(cUnit, rlSrc2.sRegLow, r1, mir->offset, NULL);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700793 }
Bill Buzbee1465db52009-09-23 17:17:35 -0700794 opReg(cUnit, kOpBlx, r2);
Elliott Hughes6a555132010-02-25 15:41:42 -0800795 dvmCompilerClobberCallRegs(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -0700796 if (retReg == r0)
Bill Buzbeec6f10662010-02-09 11:16:15 -0800797 rlResult = dvmCompilerGetReturn(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -0700798 else
Bill Buzbeec6f10662010-02-09 11:16:15 -0800799 rlResult = dvmCompilerGetReturnAlt(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -0700800 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700801 }
802 return false;
803}
804
Ben Cheng5d90c202009-11-22 23:31:11 -0800805static bool genArithOp(CompilationUnit *cUnit, MIR *mir)
Ben Chengba4fc8b2009-06-01 13:00:29 -0700806{
807 OpCode opCode = mir->dalvikInsn.opCode;
Bill Buzbee1465db52009-09-23 17:17:35 -0700808 RegLocation rlDest;
809 RegLocation rlSrc1;
810 RegLocation rlSrc2;
811 /* Deduce sizes of operands */
812 if (mir->ssaRep->numUses == 2) {
Bill Buzbeec6f10662010-02-09 11:16:15 -0800813 rlSrc1 = dvmCompilerGetSrc(cUnit, mir, 0);
814 rlSrc2 = dvmCompilerGetSrc(cUnit, mir, 1);
Bill Buzbee1465db52009-09-23 17:17:35 -0700815 } else if (mir->ssaRep->numUses == 3) {
Bill Buzbeec6f10662010-02-09 11:16:15 -0800816 rlSrc1 = dvmCompilerGetSrcWide(cUnit, mir, 0, 1);
817 rlSrc2 = dvmCompilerGetSrc(cUnit, mir, 2);
Bill Buzbee1465db52009-09-23 17:17:35 -0700818 } else {
Bill Buzbeec6f10662010-02-09 11:16:15 -0800819 rlSrc1 = dvmCompilerGetSrcWide(cUnit, mir, 0, 1);
820 rlSrc2 = dvmCompilerGetSrcWide(cUnit, mir, 2, 3);
Bill Buzbee1465db52009-09-23 17:17:35 -0700821 assert(mir->ssaRep->numUses == 4);
822 }
823 if (mir->ssaRep->numDefs == 1) {
Bill Buzbeec6f10662010-02-09 11:16:15 -0800824 rlDest = dvmCompilerGetDest(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -0700825 } else {
826 assert(mir->ssaRep->numDefs == 2);
Bill Buzbeec6f10662010-02-09 11:16:15 -0800827 rlDest = dvmCompilerGetDestWide(cUnit, mir, 0, 1);
Bill Buzbee1465db52009-09-23 17:17:35 -0700828 }
Ben Chengba4fc8b2009-06-01 13:00:29 -0700829
830 if ((opCode >= OP_ADD_LONG_2ADDR) && (opCode <= OP_XOR_LONG_2ADDR)) {
Ben Cheng5d90c202009-11-22 23:31:11 -0800831 return genArithOpLong(cUnit,mir, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700832 }
833 if ((opCode >= OP_ADD_LONG) && (opCode <= OP_XOR_LONG)) {
Ben Cheng5d90c202009-11-22 23:31:11 -0800834 return genArithOpLong(cUnit,mir, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700835 }
836 if ((opCode >= OP_SHL_LONG_2ADDR) && (opCode <= OP_USHR_LONG_2ADDR)) {
Ben Cheng5d90c202009-11-22 23:31:11 -0800837 return genShiftOpLong(cUnit,mir, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700838 }
839 if ((opCode >= OP_SHL_LONG) && (opCode <= OP_USHR_LONG)) {
Ben Cheng5d90c202009-11-22 23:31:11 -0800840 return genShiftOpLong(cUnit,mir, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700841 }
842 if ((opCode >= OP_ADD_INT_2ADDR) && (opCode <= OP_USHR_INT_2ADDR)) {
Ben Cheng5d90c202009-11-22 23:31:11 -0800843 return genArithOpInt(cUnit,mir, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700844 }
845 if ((opCode >= OP_ADD_INT) && (opCode <= OP_USHR_INT)) {
Ben Cheng5d90c202009-11-22 23:31:11 -0800846 return genArithOpInt(cUnit,mir, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700847 }
848 if ((opCode >= OP_ADD_FLOAT_2ADDR) && (opCode <= OP_REM_FLOAT_2ADDR)) {
Ben Cheng5d90c202009-11-22 23:31:11 -0800849 return genArithOpFloat(cUnit,mir, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700850 }
851 if ((opCode >= OP_ADD_FLOAT) && (opCode <= OP_REM_FLOAT)) {
Ben Cheng5d90c202009-11-22 23:31:11 -0800852 return genArithOpFloat(cUnit, mir, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700853 }
854 if ((opCode >= OP_ADD_DOUBLE_2ADDR) && (opCode <= OP_REM_DOUBLE_2ADDR)) {
Ben Cheng5d90c202009-11-22 23:31:11 -0800855 return genArithOpDouble(cUnit,mir, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700856 }
857 if ((opCode >= OP_ADD_DOUBLE) && (opCode <= OP_REM_DOUBLE)) {
Ben Cheng5d90c202009-11-22 23:31:11 -0800858 return genArithOpDouble(cUnit,mir, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700859 }
860 return true;
861}
862
Bill Buzbee1465db52009-09-23 17:17:35 -0700863/* Generate unconditional branch instructions */
864static ArmLIR *genUnconditionalBranch(CompilationUnit *cUnit, ArmLIR *target)
865{
866 ArmLIR *branch = opNone(cUnit, kOpUncondBr);
867 branch->generic.target = (LIR *) target;
868 return branch;
869}
870
Bill Buzbee1465db52009-09-23 17:17:35 -0700871/* Perform the actual operation for OP_RETURN_* */
872static void genReturnCommon(CompilationUnit *cUnit, MIR *mir)
873{
874 genDispatchToHandler(cUnit, TEMPLATE_RETURN);
Ben Cheng86717f72010-03-05 15:27:21 -0800875#if defined(JIT_STATS)
Bill Buzbee1465db52009-09-23 17:17:35 -0700876 gDvmJit.returnOp++;
877#endif
878 int dPC = (int) (cUnit->method->insns + mir->offset);
879 /* Insert branch, but defer setting of target */
880 ArmLIR *branch = genUnconditionalBranch(cUnit, NULL);
881 /* Set up the place holder to reconstruct this Dalvik PC */
882 ArmLIR *pcrLabel = dvmCompilerNew(sizeof(ArmLIR), true);
Ben Chenga4973592010-03-31 11:59:18 -0700883 pcrLabel->opCode = kArmPseudoPCReconstructionCell;
Bill Buzbee1465db52009-09-23 17:17:35 -0700884 pcrLabel->operands[0] = dPC;
885 pcrLabel->operands[1] = mir->offset;
886 /* Insert the place holder to the growable list */
887 dvmInsertGrowableList(&cUnit->pcReconstructionList, pcrLabel);
888 /* Branch to the PC reconstruction code */
889 branch->generic.target = (LIR *) pcrLabel;
890}
891
Ben Chengba4fc8b2009-06-01 13:00:29 -0700892static void genProcessArgsNoRange(CompilationUnit *cUnit, MIR *mir,
893 DecodedInstruction *dInsn,
Bill Buzbee89efc3d2009-07-28 11:22:22 -0700894 ArmLIR **pcrLabel)
Ben Chengba4fc8b2009-06-01 13:00:29 -0700895{
896 unsigned int i;
897 unsigned int regMask = 0;
Bill Buzbee1465db52009-09-23 17:17:35 -0700898 RegLocation rlArg;
899 int numDone = 0;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700900
Bill Buzbee1465db52009-09-23 17:17:35 -0700901 /*
902 * Load arguments to r0..r4. Note that these registers may contain
903 * live values, so we clobber them immediately after loading to prevent
904 * them from being used as sources for subsequent loads.
905 */
Bill Buzbeec6f10662010-02-09 11:16:15 -0800906 dvmCompilerLockAllTemps(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700907 for (i = 0; i < dInsn->vA; i++) {
908 regMask |= 1 << i;
Bill Buzbeec6f10662010-02-09 11:16:15 -0800909 rlArg = dvmCompilerGetSrc(cUnit, mir, numDone++);
Bill Buzbee1465db52009-09-23 17:17:35 -0700910 loadValueDirectFixed(cUnit, rlArg, i);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700911 }
912 if (regMask) {
913 /* Up to 5 args are pushed on top of FP - sizeofStackSaveArea */
Bill Buzbee1465db52009-09-23 17:17:35 -0700914 opRegRegImm(cUnit, kOpSub, r7, rFP,
915 sizeof(StackSaveArea) + (dInsn->vA << 2));
Ben Chengba4fc8b2009-06-01 13:00:29 -0700916 /* generate null check */
917 if (pcrLabel) {
Bill Buzbeec6f10662010-02-09 11:16:15 -0800918 *pcrLabel = genNullCheck(cUnit, dvmCompilerSSASrc(mir, 0), r0,
Bill Buzbee1465db52009-09-23 17:17:35 -0700919 mir->offset, NULL);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700920 }
Bill Buzbee270c1d62009-08-13 16:58:07 -0700921 storeMultiple(cUnit, r7, regMask);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700922 }
923}
924
925static void genProcessArgsRange(CompilationUnit *cUnit, MIR *mir,
926 DecodedInstruction *dInsn,
Bill Buzbee89efc3d2009-07-28 11:22:22 -0700927 ArmLIR **pcrLabel)
Ben Chengba4fc8b2009-06-01 13:00:29 -0700928{
929 int srcOffset = dInsn->vC << 2;
930 int numArgs = dInsn->vA;
931 int regMask;
Bill Buzbee1465db52009-09-23 17:17:35 -0700932
933 /*
934 * Note: here, all promoted registers will have been flushed
935 * back to the Dalvik base locations, so register usage restrictins
936 * are lifted. All parms loaded from original Dalvik register
937 * region - even though some might conceivably have valid copies
938 * cached in a preserved register.
939 */
Bill Buzbeec6f10662010-02-09 11:16:15 -0800940 dvmCompilerLockAllTemps(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -0700941
Ben Chengba4fc8b2009-06-01 13:00:29 -0700942 /*
943 * r4PC : &rFP[vC]
944 * r7: &newFP[0]
945 */
Bill Buzbee1465db52009-09-23 17:17:35 -0700946 opRegRegImm(cUnit, kOpAdd, r4PC, rFP, srcOffset);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700947 /* load [r0 .. min(numArgs,4)] */
948 regMask = (1 << ((numArgs < 4) ? numArgs : 4)) - 1;
Ben Chengd7d426a2009-09-22 11:23:36 -0700949 /*
950 * Protect the loadMultiple instruction from being reordered with other
951 * Dalvik stack accesses.
952 */
Bill Buzbee270c1d62009-08-13 16:58:07 -0700953 loadMultiple(cUnit, r4PC, regMask);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700954
Bill Buzbee1465db52009-09-23 17:17:35 -0700955 opRegRegImm(cUnit, kOpSub, r7, rFP,
956 sizeof(StackSaveArea) + (numArgs << 2));
Ben Chengba4fc8b2009-06-01 13:00:29 -0700957 /* generate null check */
958 if (pcrLabel) {
Bill Buzbeec6f10662010-02-09 11:16:15 -0800959 *pcrLabel = genNullCheck(cUnit, dvmCompilerSSASrc(mir, 0), r0,
Bill Buzbee1465db52009-09-23 17:17:35 -0700960 mir->offset, NULL);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700961 }
962
963 /*
964 * Handle remaining 4n arguments:
965 * store previously loaded 4 values and load the next 4 values
966 */
967 if (numArgs >= 8) {
Bill Buzbee89efc3d2009-07-28 11:22:22 -0700968 ArmLIR *loopLabel = NULL;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700969 /*
970 * r0 contains "this" and it will be used later, so push it to the stack
Bill Buzbee270c1d62009-08-13 16:58:07 -0700971 * first. Pushing r5 (rFP) is just for stack alignment purposes.
Ben Chengba4fc8b2009-06-01 13:00:29 -0700972 */
Bill Buzbee1465db52009-09-23 17:17:35 -0700973 opImm(cUnit, kOpPush, (1 << r0 | 1 << rFP));
Ben Chengba4fc8b2009-06-01 13:00:29 -0700974 /* No need to generate the loop structure if numArgs <= 11 */
975 if (numArgs > 11) {
976 loadConstant(cUnit, 5, ((numArgs - 4) >> 2) << 2);
Bill Buzbee1465db52009-09-23 17:17:35 -0700977 loopLabel = newLIR0(cUnit, kArmPseudoTargetLabel);
Ben Chengd7d426a2009-09-22 11:23:36 -0700978 loopLabel->defMask = ENCODE_ALL;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700979 }
Bill Buzbee270c1d62009-08-13 16:58:07 -0700980 storeMultiple(cUnit, r7, regMask);
Ben Chengd7d426a2009-09-22 11:23:36 -0700981 /*
982 * Protect the loadMultiple instruction from being reordered with other
983 * Dalvik stack accesses.
984 */
Bill Buzbee270c1d62009-08-13 16:58:07 -0700985 loadMultiple(cUnit, r4PC, regMask);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700986 /* No need to generate the loop structure if numArgs <= 11 */
987 if (numArgs > 11) {
Bill Buzbee1465db52009-09-23 17:17:35 -0700988 opRegImm(cUnit, kOpSub, rFP, 4);
989 genConditionalBranch(cUnit, kArmCondNe, loopLabel);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700990 }
991 }
992
993 /* Save the last batch of loaded values */
Bill Buzbee270c1d62009-08-13 16:58:07 -0700994 storeMultiple(cUnit, r7, regMask);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700995
996 /* Generate the loop epilogue - don't use r0 */
997 if ((numArgs > 4) && (numArgs % 4)) {
998 regMask = ((1 << (numArgs & 0x3)) - 1) << 1;
Ben Chengd7d426a2009-09-22 11:23:36 -0700999 /*
1000 * Protect the loadMultiple instruction from being reordered with other
1001 * Dalvik stack accesses.
1002 */
Bill Buzbee270c1d62009-08-13 16:58:07 -07001003 loadMultiple(cUnit, r4PC, regMask);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001004 }
1005 if (numArgs >= 8)
Bill Buzbee1465db52009-09-23 17:17:35 -07001006 opImm(cUnit, kOpPop, (1 << r0 | 1 << rFP));
Ben Chengba4fc8b2009-06-01 13:00:29 -07001007
1008 /* Save the modulo 4 arguments */
1009 if ((numArgs > 4) && (numArgs % 4)) {
Bill Buzbee270c1d62009-08-13 16:58:07 -07001010 storeMultiple(cUnit, r7, regMask);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001011 }
1012}
1013
Ben Cheng38329f52009-07-07 14:19:20 -07001014/*
1015 * Generate code to setup the call stack then jump to the chaining cell if it
1016 * is not a native method.
1017 */
1018static void genInvokeSingletonCommon(CompilationUnit *cUnit, MIR *mir,
Bill Buzbee89efc3d2009-07-28 11:22:22 -07001019 BasicBlock *bb, ArmLIR *labelList,
1020 ArmLIR *pcrLabel,
Ben Cheng38329f52009-07-07 14:19:20 -07001021 const Method *calleeMethod)
Ben Chengba4fc8b2009-06-01 13:00:29 -07001022{
Bill Buzbee1465db52009-09-23 17:17:35 -07001023 /*
1024 * Note: all Dalvik register state should be flushed to
1025 * memory by the point, so register usage restrictions no
1026 * longer apply. All temp & preserved registers may be used.
1027 */
Bill Buzbeec6f10662010-02-09 11:16:15 -08001028 dvmCompilerLockAllTemps(cUnit);
Bill Buzbee89efc3d2009-07-28 11:22:22 -07001029 ArmLIR *retChainingCell = &labelList[bb->fallThrough->id];
Ben Chengba4fc8b2009-06-01 13:00:29 -07001030
1031 /* r1 = &retChainingCell */
Bill Buzbeec6f10662010-02-09 11:16:15 -08001032 dvmCompilerLockTemp(cUnit, r1);
Bill Buzbee1465db52009-09-23 17:17:35 -07001033 ArmLIR *addrRetChain = opRegRegImm(cUnit, kOpAdd, r1, rpc, 0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001034 /* r4PC = dalvikCallsite */
1035 loadConstant(cUnit, r4PC,
1036 (int) (cUnit->method->insns + mir->offset));
1037 addrRetChain->generic.target = (LIR *) retChainingCell;
1038 /*
Ben Cheng38329f52009-07-07 14:19:20 -07001039 * r0 = calleeMethod (loaded upon calling genInvokeSingletonCommon)
Ben Chengba4fc8b2009-06-01 13:00:29 -07001040 * r1 = &ChainingCell
1041 * r4PC = callsiteDPC
1042 */
1043 if (dvmIsNativeMethod(calleeMethod)) {
Ben Cheng38329f52009-07-07 14:19:20 -07001044 genDispatchToHandler(cUnit, TEMPLATE_INVOKE_METHOD_NATIVE);
Ben Cheng86717f72010-03-05 15:27:21 -08001045#if defined(JIT_STATS)
Ben Cheng38329f52009-07-07 14:19:20 -07001046 gDvmJit.invokeNative++;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001047#endif
1048 } else {
1049 genDispatchToHandler(cUnit, TEMPLATE_INVOKE_METHOD_CHAIN);
Ben Cheng86717f72010-03-05 15:27:21 -08001050#if defined(JIT_STATS)
1051 gDvmJit.invokeMonomorphic++;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001052#endif
Ben Cheng38329f52009-07-07 14:19:20 -07001053 /* Branch to the chaining cell */
Ben Chengba4fc8b2009-06-01 13:00:29 -07001054 genUnconditionalBranch(cUnit, &labelList[bb->taken->id]);
1055 }
1056 /* Handle exceptions using the interpreter */
1057 genTrap(cUnit, mir->offset, pcrLabel);
1058}
1059
Ben Cheng38329f52009-07-07 14:19:20 -07001060/*
1061 * Generate code to check the validity of a predicted chain and take actions
1062 * based on the result.
1063 *
1064 * 0x426a99aa : ldr r4, [pc, #72] --> r4 <- dalvikPC of this invoke
1065 * 0x426a99ac : add r1, pc, #32 --> r1 <- &retChainingCell
1066 * 0x426a99ae : add r2, pc, #40 --> r2 <- &predictedChainingCell
1067 * 0x426a99b0 : blx_1 0x426a918c --+ TEMPLATE_INVOKE_METHOD_PREDICTED_CHAIN
1068 * 0x426a99b2 : blx_2 see above --+
1069 * 0x426a99b4 : b 0x426a99d8 --> off to the predicted chain
1070 * 0x426a99b6 : b 0x426a99c8 --> punt to the interpreter
1071 * 0x426a99b8 : ldr r0, [r7, #44] --> r0 <- this->class->vtable[methodIdx]
1072 * 0x426a99ba : cmp r1, #0 --> compare r1 (rechain count) against 0
1073 * 0x426a99bc : bgt 0x426a99c2 --> >=0? don't rechain
1074 * 0x426a99be : ldr r7, [r6, #96] --+ dvmJitToPatchPredictedChain
1075 * 0x426a99c0 : blx r7 --+
1076 * 0x426a99c2 : add r1, pc, #12 --> r1 <- &retChainingCell
1077 * 0x426a99c4 : blx_1 0x426a9098 --+ TEMPLATE_INVOKE_METHOD_NO_OPT
1078 * 0x426a99c6 : blx_2 see above --+
1079 */
1080static void genInvokeVirtualCommon(CompilationUnit *cUnit, MIR *mir,
1081 int methodIndex,
Bill Buzbee89efc3d2009-07-28 11:22:22 -07001082 ArmLIR *retChainingCell,
1083 ArmLIR *predChainingCell,
1084 ArmLIR *pcrLabel)
Ben Cheng38329f52009-07-07 14:19:20 -07001085{
Bill Buzbee1465db52009-09-23 17:17:35 -07001086 /*
1087 * Note: all Dalvik register state should be flushed to
1088 * memory by the point, so register usage restrictions no
1089 * longer apply. Lock temps to prevent them from being
1090 * allocated by utility routines.
1091 */
Bill Buzbeec6f10662010-02-09 11:16:15 -08001092 dvmCompilerLockAllTemps(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07001093
Ben Cheng38329f52009-07-07 14:19:20 -07001094 /* "this" is already left in r0 by genProcessArgs* */
1095
1096 /* r4PC = dalvikCallsite */
1097 loadConstant(cUnit, r4PC,
1098 (int) (cUnit->method->insns + mir->offset));
1099
1100 /* r1 = &retChainingCell */
Bill Buzbee1465db52009-09-23 17:17:35 -07001101 ArmLIR *addrRetChain = opRegRegImm(cUnit, kOpAdd, r1, rpc, 0);
Ben Cheng38329f52009-07-07 14:19:20 -07001102 addrRetChain->generic.target = (LIR *) retChainingCell;
1103
1104 /* r2 = &predictedChainingCell */
Bill Buzbee1465db52009-09-23 17:17:35 -07001105 ArmLIR *predictedChainingCell = opRegRegImm(cUnit, kOpAdd, r2, rpc, 0);
Ben Cheng38329f52009-07-07 14:19:20 -07001106 predictedChainingCell->generic.target = (LIR *) predChainingCell;
1107
1108 genDispatchToHandler(cUnit, TEMPLATE_INVOKE_METHOD_PREDICTED_CHAIN);
1109
1110 /* return through lr - jump to the chaining cell */
1111 genUnconditionalBranch(cUnit, predChainingCell);
1112
1113 /*
1114 * null-check on "this" may have been eliminated, but we still need a PC-
1115 * reconstruction label for stack overflow bailout.
1116 */
1117 if (pcrLabel == NULL) {
1118 int dPC = (int) (cUnit->method->insns + mir->offset);
Bill Buzbee89efc3d2009-07-28 11:22:22 -07001119 pcrLabel = dvmCompilerNew(sizeof(ArmLIR), true);
Ben Chenga4973592010-03-31 11:59:18 -07001120 pcrLabel->opCode = kArmPseudoPCReconstructionCell;
Ben Cheng38329f52009-07-07 14:19:20 -07001121 pcrLabel->operands[0] = dPC;
1122 pcrLabel->operands[1] = mir->offset;
1123 /* Insert the place holder to the growable list */
1124 dvmInsertGrowableList(&cUnit->pcReconstructionList, pcrLabel);
1125 }
1126
1127 /* return through lr+2 - punt to the interpreter */
1128 genUnconditionalBranch(cUnit, pcrLabel);
1129
1130 /*
1131 * return through lr+4 - fully resolve the callee method.
1132 * r1 <- count
1133 * r2 <- &predictedChainCell
1134 * r3 <- this->class
1135 * r4 <- dPC
1136 * r7 <- this->class->vtable
1137 */
1138
1139 /* r0 <- calleeMethod */
Bill Buzbee270c1d62009-08-13 16:58:07 -07001140 loadWordDisp(cUnit, r7, methodIndex * 4, r0);
Ben Cheng38329f52009-07-07 14:19:20 -07001141
1142 /* Check if rechain limit is reached */
Bill Buzbee1465db52009-09-23 17:17:35 -07001143 opRegImm(cUnit, kOpCmp, r1, 0);
Ben Cheng38329f52009-07-07 14:19:20 -07001144
Bill Buzbee1465db52009-09-23 17:17:35 -07001145 ArmLIR *bypassRechaining = opCondBranch(cUnit, kArmCondGt);
Ben Cheng38329f52009-07-07 14:19:20 -07001146
Bill Buzbee270c1d62009-08-13 16:58:07 -07001147 loadWordDisp(cUnit, rGLUE, offsetof(InterpState,
1148 jitToInterpEntries.dvmJitToPatchPredictedChain), r7);
Ben Cheng38329f52009-07-07 14:19:20 -07001149
1150 /*
1151 * r0 = calleeMethod
1152 * r2 = &predictedChainingCell
1153 * r3 = class
1154 *
1155 * &returnChainingCell has been loaded into r1 but is not needed
1156 * when patching the chaining cell and will be clobbered upon
1157 * returning so it will be reconstructed again.
1158 */
Bill Buzbee1465db52009-09-23 17:17:35 -07001159 opReg(cUnit, kOpBlx, r7);
Ben Cheng38329f52009-07-07 14:19:20 -07001160
1161 /* r1 = &retChainingCell */
Bill Buzbee1465db52009-09-23 17:17:35 -07001162 addrRetChain = opRegRegImm(cUnit, kOpAdd, r1, rpc, 0);
Ben Cheng38329f52009-07-07 14:19:20 -07001163 addrRetChain->generic.target = (LIR *) retChainingCell;
1164
1165 bypassRechaining->generic.target = (LIR *) addrRetChain;
1166 /*
1167 * r0 = calleeMethod,
1168 * r1 = &ChainingCell,
1169 * r4PC = callsiteDPC,
1170 */
1171 genDispatchToHandler(cUnit, TEMPLATE_INVOKE_METHOD_NO_OPT);
Ben Cheng86717f72010-03-05 15:27:21 -08001172#if defined(JIT_STATS)
1173 gDvmJit.invokePolymorphic++;
Ben Cheng38329f52009-07-07 14:19:20 -07001174#endif
1175 /* Handle exceptions using the interpreter */
1176 genTrap(cUnit, mir->offset, pcrLabel);
1177}
1178
1179/*
1180 * Up calling this function, "this" is stored in r0. The actual class will be
1181 * chased down off r0 and the predicted one will be retrieved through
1182 * predictedChainingCell then a comparison is performed to see whether the
1183 * previously established chaining is still valid.
1184 *
1185 * The return LIR is a branch based on the comparison result. The actual branch
1186 * target will be setup in the caller.
1187 */
Bill Buzbee89efc3d2009-07-28 11:22:22 -07001188static ArmLIR *genCheckPredictedChain(CompilationUnit *cUnit,
1189 ArmLIR *predChainingCell,
1190 ArmLIR *retChainingCell,
Ben Cheng38329f52009-07-07 14:19:20 -07001191 MIR *mir)
1192{
Bill Buzbee1465db52009-09-23 17:17:35 -07001193 /*
1194 * Note: all Dalvik register state should be flushed to
1195 * memory by the point, so register usage restrictions no
1196 * longer apply. All temp & preserved registers may be used.
1197 */
Bill Buzbeec6f10662010-02-09 11:16:15 -08001198 dvmCompilerLockAllTemps(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07001199
Ben Cheng38329f52009-07-07 14:19:20 -07001200 /* r3 now contains this->clazz */
Bill Buzbee270c1d62009-08-13 16:58:07 -07001201 loadWordDisp(cUnit, r0, offsetof(Object, clazz), r3);
Ben Cheng38329f52009-07-07 14:19:20 -07001202
1203 /*
1204 * r2 now contains predicted class. The starting offset of the
1205 * cached value is 4 bytes into the chaining cell.
1206 */
Bill Buzbee89efc3d2009-07-28 11:22:22 -07001207 ArmLIR *getPredictedClass =
Bill Buzbee270c1d62009-08-13 16:58:07 -07001208 loadWordDisp(cUnit, rpc, offsetof(PredictedChainingCell, clazz), r2);
Ben Cheng38329f52009-07-07 14:19:20 -07001209 getPredictedClass->generic.target = (LIR *) predChainingCell;
1210
1211 /*
1212 * r0 now contains predicted method. The starting offset of the
1213 * cached value is 8 bytes into the chaining cell.
1214 */
Bill Buzbee89efc3d2009-07-28 11:22:22 -07001215 ArmLIR *getPredictedMethod =
Bill Buzbee270c1d62009-08-13 16:58:07 -07001216 loadWordDisp(cUnit, rpc, offsetof(PredictedChainingCell, method), r0);
Ben Cheng38329f52009-07-07 14:19:20 -07001217 getPredictedMethod->generic.target = (LIR *) predChainingCell;
1218
1219 /* Load the stats counter to see if it is time to unchain and refresh */
Bill Buzbee89efc3d2009-07-28 11:22:22 -07001220 ArmLIR *getRechainingRequestCount =
Bill Buzbee270c1d62009-08-13 16:58:07 -07001221 loadWordDisp(cUnit, rpc, offsetof(PredictedChainingCell, counter), r7);
Ben Cheng38329f52009-07-07 14:19:20 -07001222 getRechainingRequestCount->generic.target =
1223 (LIR *) predChainingCell;
1224
1225 /* r4PC = dalvikCallsite */
1226 loadConstant(cUnit, r4PC,
1227 (int) (cUnit->method->insns + mir->offset));
1228
1229 /* r1 = &retChainingCell */
Bill Buzbee1465db52009-09-23 17:17:35 -07001230 ArmLIR *addrRetChain = opRegRegImm(cUnit, kOpAdd, r1, rpc, 0);
Ben Cheng38329f52009-07-07 14:19:20 -07001231 addrRetChain->generic.target = (LIR *) retChainingCell;
1232
1233 /* Check if r2 (predicted class) == r3 (actual class) */
Bill Buzbee1465db52009-09-23 17:17:35 -07001234 opRegReg(cUnit, kOpCmp, r2, r3);
Ben Cheng38329f52009-07-07 14:19:20 -07001235
Bill Buzbee1465db52009-09-23 17:17:35 -07001236 return opCondBranch(cUnit, kArmCondEq);
Ben Cheng38329f52009-07-07 14:19:20 -07001237}
1238
Ben Chengba4fc8b2009-06-01 13:00:29 -07001239/* Geneate a branch to go back to the interpreter */
1240static void genPuntToInterp(CompilationUnit *cUnit, unsigned int offset)
1241{
1242 /* r0 = dalvik pc */
Bill Buzbeec6f10662010-02-09 11:16:15 -08001243 dvmCompilerFlushAllRegs(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001244 loadConstant(cUnit, r0, (int) (cUnit->method->insns + offset));
Bill Buzbee270c1d62009-08-13 16:58:07 -07001245 loadWordDisp(cUnit, r0, offsetof(Object, clazz), r3);
1246 loadWordDisp(cUnit, rGLUE, offsetof(InterpState,
1247 jitToInterpEntries.dvmJitToInterpPunt), r1);
Bill Buzbee1465db52009-09-23 17:17:35 -07001248 opReg(cUnit, kOpBlx, r1);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001249}
1250
1251/*
1252 * Attempt to single step one instruction using the interpreter and return
1253 * to the compiled code for the next Dalvik instruction
1254 */
1255static void genInterpSingleStep(CompilationUnit *cUnit, MIR *mir)
1256{
1257 int flags = dexGetInstrFlags(gDvm.instrFlags, mir->dalvikInsn.opCode);
1258 int flagsToCheck = kInstrCanBranch | kInstrCanSwitch | kInstrCanReturn |
1259 kInstrCanThrow;
Bill Buzbee1465db52009-09-23 17:17:35 -07001260
Bill Buzbee45273872010-03-11 11:12:15 -08001261 //If already optimized out, just ignore
1262 if (mir->dalvikInsn.opCode == OP_NOP)
1263 return;
1264
Bill Buzbee1465db52009-09-23 17:17:35 -07001265 //Ugly, but necessary. Flush all Dalvik regs so Interp can find them
Bill Buzbeec6f10662010-02-09 11:16:15 -08001266 dvmCompilerFlushAllRegs(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07001267
Ben Chengba4fc8b2009-06-01 13:00:29 -07001268 if ((mir->next == NULL) || (flags & flagsToCheck)) {
1269 genPuntToInterp(cUnit, mir->offset);
1270 return;
1271 }
1272 int entryAddr = offsetof(InterpState,
1273 jitToInterpEntries.dvmJitToInterpSingleStep);
Bill Buzbee270c1d62009-08-13 16:58:07 -07001274 loadWordDisp(cUnit, rGLUE, entryAddr, r2);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001275 /* r0 = dalvik pc */
1276 loadConstant(cUnit, r0, (int) (cUnit->method->insns + mir->offset));
1277 /* r1 = dalvik pc of following instruction */
1278 loadConstant(cUnit, r1, (int) (cUnit->method->insns + mir->next->offset));
Bill Buzbee1465db52009-09-23 17:17:35 -07001279 opReg(cUnit, kOpBlx, r2);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001280}
1281
Bill Buzbeec1d9ed42010-02-02 11:04:33 -08001282/*
1283 * To prevent a thread in a monitor wait from blocking the Jit from
1284 * resetting the code cache, heavyweight monitor lock will not
1285 * be allowed to return to an existing translation. Instead, we will
1286 * handle them by branching to a handler, which will in turn call the
1287 * runtime lock routine and then branch directly back to the
1288 * interpreter main loop. Given the high cost of the heavyweight
1289 * lock operation, this additional cost should be slight (especially when
1290 * considering that we expect the vast majority of lock operations to
1291 * use the fast-path thin lock bypass).
1292 */
Ben Cheng5d90c202009-11-22 23:31:11 -08001293static void genMonitorPortable(CompilationUnit *cUnit, MIR *mir)
Bill Buzbee270c1d62009-08-13 16:58:07 -07001294{
Bill Buzbeeefbd3c52009-11-04 22:18:40 -08001295 bool isEnter = (mir->dalvikInsn.opCode == OP_MONITOR_ENTER);
Bill Buzbee1465db52009-09-23 17:17:35 -07001296 genExportPC(cUnit, mir);
Bill Buzbeec6f10662010-02-09 11:16:15 -08001297 dvmCompilerFlushAllRegs(cUnit); /* Send everything to home location */
1298 RegLocation rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07001299 loadValueDirectFixed(cUnit, rlSrc, r1);
1300 loadWordDisp(cUnit, rGLUE, offsetof(InterpState, self), r0);
Bill Buzbeec1d9ed42010-02-02 11:04:33 -08001301 genNullCheck(cUnit, rlSrc.sRegLow, r1, mir->offset, NULL);
Bill Buzbeeefbd3c52009-11-04 22:18:40 -08001302 if (isEnter) {
Bill Buzbeec1d9ed42010-02-02 11:04:33 -08001303 /* Get dPC of next insn */
1304 loadConstant(cUnit, r4PC, (int)(cUnit->method->insns + mir->offset +
1305 dexGetInstrWidthAbs(gDvm.instrWidth, OP_MONITOR_ENTER)));
1306#if defined(WITH_DEADLOCK_PREDICTION)
1307 genDispatchToHandler(cUnit, TEMPLATE_MONITOR_ENTER_DEBUG);
1308#else
1309 genDispatchToHandler(cUnit, TEMPLATE_MONITOR_ENTER);
1310#endif
Bill Buzbee1465db52009-09-23 17:17:35 -07001311 } else {
Ben Chengbd1326d2010-04-02 15:04:53 -07001312 LOAD_FUNC_ADDR(cUnit, r2, (int)dvmUnlockObject);
Bill Buzbeec1d9ed42010-02-02 11:04:33 -08001313 /* Do the call */
1314 opReg(cUnit, kOpBlx, r2);
Bill Buzbee6bbdd6b2010-02-16 14:40:01 -08001315 opRegImm(cUnit, kOpCmp, r0, 0); /* Did we throw? */
1316 ArmLIR *branchOver = opCondBranch(cUnit, kArmCondNe);
1317 loadConstant(cUnit, r0,
1318 (int) (cUnit->method->insns + mir->offset +
1319 dexGetInstrWidthAbs(gDvm.instrWidth, OP_MONITOR_EXIT)));
1320 genDispatchToHandler(cUnit, TEMPLATE_THROW_EXCEPTION_COMMON);
1321 ArmLIR *target = newLIR0(cUnit, kArmPseudoTargetLabel);
1322 target->defMask = ENCODE_ALL;
1323 branchOver->generic.target = (LIR *) target;
Elliott Hughes6a555132010-02-25 15:41:42 -08001324 dvmCompilerClobberCallRegs(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07001325 }
Bill Buzbee270c1d62009-08-13 16:58:07 -07001326}
1327
Ben Chengba4fc8b2009-06-01 13:00:29 -07001328/*
1329 * The following are the first-level codegen routines that analyze the format
1330 * of each bytecode then either dispatch special purpose codegen routines
1331 * or produce corresponding Thumb instructions directly.
1332 */
1333
1334static bool handleFmt10t_Fmt20t_Fmt30t(CompilationUnit *cUnit, MIR *mir,
Bill Buzbee89efc3d2009-07-28 11:22:22 -07001335 BasicBlock *bb, ArmLIR *labelList)
Ben Chengba4fc8b2009-06-01 13:00:29 -07001336{
1337 /* For OP_GOTO, OP_GOTO_16, and OP_GOTO_32 */
1338 genUnconditionalBranch(cUnit, &labelList[bb->taken->id]);
1339 return false;
1340}
1341
1342static bool handleFmt10x(CompilationUnit *cUnit, MIR *mir)
1343{
1344 OpCode dalvikOpCode = mir->dalvikInsn.opCode;
1345 if (((dalvikOpCode >= OP_UNUSED_3E) && (dalvikOpCode <= OP_UNUSED_43)) ||
Andy McFadden53878242010-03-05 07:24:27 -08001346 ((dalvikOpCode >= OP_UNUSED_E3) && (dalvikOpCode <= OP_UNUSED_E7))) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07001347 LOGE("Codegen: got unused opcode 0x%x\n",dalvikOpCode);
1348 return true;
1349 }
1350 switch (dalvikOpCode) {
1351 case OP_RETURN_VOID:
1352 genReturnCommon(cUnit,mir);
1353 break;
1354 case OP_UNUSED_73:
1355 case OP_UNUSED_79:
1356 case OP_UNUSED_7A:
1357 LOGE("Codegen: got unused opcode 0x%x\n",dalvikOpCode);
1358 return true;
1359 case OP_NOP:
1360 break;
1361 default:
1362 return true;
1363 }
1364 return false;
1365}
1366
1367static bool handleFmt11n_Fmt31i(CompilationUnit *cUnit, MIR *mir)
1368{
Bill Buzbee1465db52009-09-23 17:17:35 -07001369 RegLocation rlDest;
1370 RegLocation rlResult;
1371 if (mir->ssaRep->numDefs == 2) {
Bill Buzbeec6f10662010-02-09 11:16:15 -08001372 rlDest = dvmCompilerGetDestWide(cUnit, mir, 0, 1);
Bill Buzbee1465db52009-09-23 17:17:35 -07001373 } else {
Bill Buzbeec6f10662010-02-09 11:16:15 -08001374 rlDest = dvmCompilerGetDest(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07001375 }
Ben Chenge9695e52009-06-16 16:11:47 -07001376
Ben Chengba4fc8b2009-06-01 13:00:29 -07001377 switch (mir->dalvikInsn.opCode) {
1378 case OP_CONST:
Ben Chenge9695e52009-06-16 16:11:47 -07001379 case OP_CONST_4: {
Bill Buzbeec6f10662010-02-09 11:16:15 -08001380 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kAnyReg, true);
Ben Chengbd1326d2010-04-02 15:04:53 -07001381 loadConstantNoClobber(cUnit, rlResult.lowReg, mir->dalvikInsn.vB);
Bill Buzbee1465db52009-09-23 17:17:35 -07001382 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001383 break;
Ben Chenge9695e52009-06-16 16:11:47 -07001384 }
1385 case OP_CONST_WIDE_32: {
Bill Buzbee1465db52009-09-23 17:17:35 -07001386 //TUNING: single routine to load constant pair for support doubles
Bill Buzbee964a7b02010-01-28 12:54:19 -08001387 //TUNING: load 0/-1 separately to avoid load dependency
Bill Buzbeec6f10662010-02-09 11:16:15 -08001388 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Ben Chengbd1326d2010-04-02 15:04:53 -07001389 loadConstantNoClobber(cUnit, rlResult.lowReg, mir->dalvikInsn.vB);
Bill Buzbee1465db52009-09-23 17:17:35 -07001390 opRegRegImm(cUnit, kOpAsr, rlResult.highReg,
1391 rlResult.lowReg, 31);
1392 storeValueWide(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001393 break;
Ben Chenge9695e52009-06-16 16:11:47 -07001394 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07001395 default:
1396 return true;
1397 }
1398 return false;
1399}
1400
1401static bool handleFmt21h(CompilationUnit *cUnit, MIR *mir)
1402{
Bill Buzbee1465db52009-09-23 17:17:35 -07001403 RegLocation rlDest;
1404 RegLocation rlResult;
1405 if (mir->ssaRep->numDefs == 2) {
Bill Buzbeec6f10662010-02-09 11:16:15 -08001406 rlDest = dvmCompilerGetDestWide(cUnit, mir, 0, 1);
Bill Buzbee1465db52009-09-23 17:17:35 -07001407 } else {
Bill Buzbeec6f10662010-02-09 11:16:15 -08001408 rlDest = dvmCompilerGetDest(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07001409 }
Bill Buzbeec6f10662010-02-09 11:16:15 -08001410 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kAnyReg, true);
Ben Chenge9695e52009-06-16 16:11:47 -07001411
Ben Chengba4fc8b2009-06-01 13:00:29 -07001412 switch (mir->dalvikInsn.opCode) {
Ben Chenge9695e52009-06-16 16:11:47 -07001413 case OP_CONST_HIGH16: {
Ben Chengbd1326d2010-04-02 15:04:53 -07001414 loadConstantNoClobber(cUnit, rlResult.lowReg,
1415 mir->dalvikInsn.vB << 16);
Bill Buzbee1465db52009-09-23 17:17:35 -07001416 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001417 break;
Ben Chenge9695e52009-06-16 16:11:47 -07001418 }
1419 case OP_CONST_WIDE_HIGH16: {
Bill Buzbee1465db52009-09-23 17:17:35 -07001420 loadConstantValueWide(cUnit, rlResult.lowReg, rlResult.highReg,
1421 0, mir->dalvikInsn.vB << 16);
1422 storeValueWide(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001423 break;
Ben Chenge9695e52009-06-16 16:11:47 -07001424 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07001425 default:
1426 return true;
1427 }
1428 return false;
1429}
1430
1431static bool handleFmt20bc(CompilationUnit *cUnit, MIR *mir)
1432{
1433 /* For OP_THROW_VERIFICATION_ERROR */
1434 genInterpSingleStep(cUnit, mir);
1435 return false;
1436}
1437
1438static bool handleFmt21c_Fmt31c(CompilationUnit *cUnit, MIR *mir)
1439{
Bill Buzbee1465db52009-09-23 17:17:35 -07001440 RegLocation rlResult;
1441 RegLocation rlDest;
1442 RegLocation rlSrc;
Ben Chenge9695e52009-06-16 16:11:47 -07001443
Ben Chengba4fc8b2009-06-01 13:00:29 -07001444 switch (mir->dalvikInsn.opCode) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07001445 case OP_CONST_STRING_JUMBO:
1446 case OP_CONST_STRING: {
1447 void *strPtr = (void*)
1448 (cUnit->method->clazz->pDvmDex->pResStrings[mir->dalvikInsn.vB]);
1449 assert(strPtr != NULL);
Bill Buzbeec6f10662010-02-09 11:16:15 -08001450 rlDest = dvmCompilerGetDest(cUnit, mir, 0);
1451 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Ben Chengbd1326d2010-04-02 15:04:53 -07001452 loadConstantNoClobber(cUnit, rlResult.lowReg, (int) strPtr );
Bill Buzbee1465db52009-09-23 17:17:35 -07001453 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001454 break;
1455 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07001456 case OP_CONST_CLASS: {
1457 void *classPtr = (void*)
1458 (cUnit->method->clazz->pDvmDex->pResClasses[mir->dalvikInsn.vB]);
1459 assert(classPtr != NULL);
Bill Buzbeec6f10662010-02-09 11:16:15 -08001460 rlDest = dvmCompilerGetDest(cUnit, mir, 0);
1461 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Ben Chengbd1326d2010-04-02 15:04:53 -07001462 loadConstantNoClobber(cUnit, rlResult.lowReg, (int) classPtr );
Bill Buzbee1465db52009-09-23 17:17:35 -07001463 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001464 break;
1465 }
1466 case OP_SGET_OBJECT:
1467 case OP_SGET_BOOLEAN:
1468 case OP_SGET_CHAR:
1469 case OP_SGET_BYTE:
1470 case OP_SGET_SHORT:
1471 case OP_SGET: {
Bill Buzbee50a6bf22009-07-08 13:08:04 -07001472 int valOffset = offsetof(StaticField, value);
Bill Buzbeec6f10662010-02-09 11:16:15 -08001473 int tReg = dvmCompilerAllocTemp(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001474 void *fieldPtr = (void*)
1475 (cUnit->method->clazz->pDvmDex->pResFields[mir->dalvikInsn.vB]);
1476 assert(fieldPtr != NULL);
Bill Buzbeec6f10662010-02-09 11:16:15 -08001477 rlDest = dvmCompilerGetDest(cUnit, mir, 0);
1478 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kAnyReg, true);
Bill Buzbee1465db52009-09-23 17:17:35 -07001479 loadConstant(cUnit, tReg, (int) fieldPtr + valOffset);
Ben Cheng11d8f142010-03-24 15:24:19 -07001480
1481 HEAP_ACCESS_SHADOW(true);
Bill Buzbee1465db52009-09-23 17:17:35 -07001482 loadWordDisp(cUnit, tReg, 0, rlResult.lowReg);
Ben Cheng11d8f142010-03-24 15:24:19 -07001483 HEAP_ACCESS_SHADOW(false);
1484
Bill Buzbee1465db52009-09-23 17:17:35 -07001485 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001486 break;
1487 }
1488 case OP_SGET_WIDE: {
Bill Buzbee50a6bf22009-07-08 13:08:04 -07001489 int valOffset = offsetof(StaticField, value);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001490 void *fieldPtr = (void*)
1491 (cUnit->method->clazz->pDvmDex->pResFields[mir->dalvikInsn.vB]);
Bill Buzbeec6f10662010-02-09 11:16:15 -08001492 int tReg = dvmCompilerAllocTemp(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001493 assert(fieldPtr != NULL);
Bill Buzbeec6f10662010-02-09 11:16:15 -08001494 rlDest = dvmCompilerGetDestWide(cUnit, mir, 0, 1);
1495 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kAnyReg, true);
Bill Buzbee1465db52009-09-23 17:17:35 -07001496 loadConstant(cUnit, tReg, (int) fieldPtr + valOffset);
Ben Cheng11d8f142010-03-24 15:24:19 -07001497
1498 HEAP_ACCESS_SHADOW(true);
Bill Buzbee1465db52009-09-23 17:17:35 -07001499 loadPair(cUnit, tReg, rlResult.lowReg, rlResult.highReg);
Ben Cheng11d8f142010-03-24 15:24:19 -07001500 HEAP_ACCESS_SHADOW(false);
1501
Bill Buzbee1465db52009-09-23 17:17:35 -07001502 storeValueWide(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001503 break;
1504 }
1505 case OP_SPUT_OBJECT:
1506 case OP_SPUT_BOOLEAN:
1507 case OP_SPUT_CHAR:
1508 case OP_SPUT_BYTE:
1509 case OP_SPUT_SHORT:
1510 case OP_SPUT: {
Bill Buzbee50a6bf22009-07-08 13:08:04 -07001511 int valOffset = offsetof(StaticField, value);
Bill Buzbeec6f10662010-02-09 11:16:15 -08001512 int tReg = dvmCompilerAllocTemp(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001513 void *fieldPtr = (void*)
1514 (cUnit->method->clazz->pDvmDex->pResFields[mir->dalvikInsn.vB]);
Ben Chenge9695e52009-06-16 16:11:47 -07001515
Ben Chengba4fc8b2009-06-01 13:00:29 -07001516 assert(fieldPtr != NULL);
Bill Buzbeec6f10662010-02-09 11:16:15 -08001517 rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07001518 rlSrc = loadValue(cUnit, rlSrc, kAnyReg);
1519 loadConstant(cUnit, tReg, (int) fieldPtr + valOffset);
Ben Cheng11d8f142010-03-24 15:24:19 -07001520
1521 HEAP_ACCESS_SHADOW(true);
Bill Buzbee1465db52009-09-23 17:17:35 -07001522 storeWordDisp(cUnit, tReg, 0 ,rlSrc.lowReg);
Ben Cheng11d8f142010-03-24 15:24:19 -07001523 HEAP_ACCESS_SHADOW(false);
1524
Ben Chengba4fc8b2009-06-01 13:00:29 -07001525 break;
1526 }
1527 case OP_SPUT_WIDE: {
Bill Buzbeec6f10662010-02-09 11:16:15 -08001528 int tReg = dvmCompilerAllocTemp(cUnit);
Bill Buzbee50a6bf22009-07-08 13:08:04 -07001529 int valOffset = offsetof(StaticField, value);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001530 void *fieldPtr = (void*)
1531 (cUnit->method->clazz->pDvmDex->pResFields[mir->dalvikInsn.vB]);
Ben Chenge9695e52009-06-16 16:11:47 -07001532
Ben Chengba4fc8b2009-06-01 13:00:29 -07001533 assert(fieldPtr != NULL);
Bill Buzbeec6f10662010-02-09 11:16:15 -08001534 rlSrc = dvmCompilerGetSrcWide(cUnit, mir, 0, 1);
Bill Buzbee1465db52009-09-23 17:17:35 -07001535 rlSrc = loadValueWide(cUnit, rlSrc, kAnyReg);
1536 loadConstant(cUnit, tReg, (int) fieldPtr + valOffset);
Ben Cheng11d8f142010-03-24 15:24:19 -07001537
1538 HEAP_ACCESS_SHADOW(true);
Bill Buzbee1465db52009-09-23 17:17:35 -07001539 storePair(cUnit, tReg, rlSrc.lowReg, rlSrc.highReg);
Ben Cheng11d8f142010-03-24 15:24:19 -07001540 HEAP_ACCESS_SHADOW(false);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001541 break;
1542 }
1543 case OP_NEW_INSTANCE: {
Ben Chenge9695e52009-06-16 16:11:47 -07001544 /*
1545 * Obey the calling convention and don't mess with the register
1546 * usage.
1547 */
Ben Chengba4fc8b2009-06-01 13:00:29 -07001548 ClassObject *classPtr = (void*)
1549 (cUnit->method->clazz->pDvmDex->pResClasses[mir->dalvikInsn.vB]);
1550 assert(classPtr != NULL);
1551 assert(classPtr->status & CLASS_INITIALIZED);
Ben Cheng79d173c2009-09-29 16:12:51 -07001552 /*
1553 * If it is going to throw, it should not make to the trace to begin
Bill Buzbee1465db52009-09-23 17:17:35 -07001554 * with. However, Alloc might throw, so we need to genExportPC()
Ben Cheng79d173c2009-09-29 16:12:51 -07001555 */
1556 assert((classPtr->accessFlags & (ACC_INTERFACE|ACC_ABSTRACT)) == 0);
Bill Buzbeec6f10662010-02-09 11:16:15 -08001557 dvmCompilerFlushAllRegs(cUnit); /* Everything to home location */
Bill Buzbee1465db52009-09-23 17:17:35 -07001558 genExportPC(cUnit, mir);
Ben Chengbd1326d2010-04-02 15:04:53 -07001559 LOAD_FUNC_ADDR(cUnit, r2, (int)dvmAllocObject);
Ben Chenge9695e52009-06-16 16:11:47 -07001560 loadConstant(cUnit, r0, (int) classPtr);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001561 loadConstant(cUnit, r1, ALLOC_DONT_TRACK);
Bill Buzbee1465db52009-09-23 17:17:35 -07001562 opReg(cUnit, kOpBlx, r2);
Elliott Hughes6a555132010-02-25 15:41:42 -08001563 dvmCompilerClobberCallRegs(cUnit);
Ben Cheng4f489172009-09-27 17:08:35 -07001564 /* generate a branch over if allocation is successful */
Bill Buzbee1465db52009-09-23 17:17:35 -07001565 opRegImm(cUnit, kOpCmp, r0, 0); /* NULL? */
1566 ArmLIR *branchOver = opCondBranch(cUnit, kArmCondNe);
Ben Cheng4f489172009-09-27 17:08:35 -07001567 /*
1568 * OOM exception needs to be thrown here and cannot re-execute
1569 */
1570 loadConstant(cUnit, r0,
1571 (int) (cUnit->method->insns + mir->offset));
1572 genDispatchToHandler(cUnit, TEMPLATE_THROW_EXCEPTION_COMMON);
1573 /* noreturn */
1574
Bill Buzbee1465db52009-09-23 17:17:35 -07001575 ArmLIR *target = newLIR0(cUnit, kArmPseudoTargetLabel);
Ben Cheng4f489172009-09-27 17:08:35 -07001576 target->defMask = ENCODE_ALL;
1577 branchOver->generic.target = (LIR *) target;
Bill Buzbeec6f10662010-02-09 11:16:15 -08001578 rlDest = dvmCompilerGetDest(cUnit, mir, 0);
1579 rlResult = dvmCompilerGetReturn(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07001580 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001581 break;
1582 }
1583 case OP_CHECK_CAST: {
Ben Chenge9695e52009-06-16 16:11:47 -07001584 /*
1585 * Obey the calling convention and don't mess with the register
1586 * usage.
1587 */
Ben Chengba4fc8b2009-06-01 13:00:29 -07001588 ClassObject *classPtr =
1589 (cUnit->method->clazz->pDvmDex->pResClasses[mir->dalvikInsn.vB]);
Bill Buzbee4df41a52009-11-12 17:07:16 -08001590 /*
1591 * Note: It is possible that classPtr is NULL at this point,
1592 * even though this instruction has been successfully interpreted.
1593 * If the previous interpretation had a null source, the
1594 * interpreter would not have bothered to resolve the clazz.
1595 * Bail out to the interpreter in this case, and log it
1596 * so that we can tell if it happens frequently.
1597 */
1598 if (classPtr == NULL) {
Ben Cheng11d8f142010-03-24 15:24:19 -07001599 LOGVV("null clazz in OP_CHECK_CAST, single-stepping");
Bill Buzbee4df41a52009-11-12 17:07:16 -08001600 genInterpSingleStep(cUnit, mir);
1601 return false;
1602 }
Bill Buzbeec6f10662010-02-09 11:16:15 -08001603 dvmCompilerFlushAllRegs(cUnit); /* Everything to home location */
Ben Chengba4fc8b2009-06-01 13:00:29 -07001604 loadConstant(cUnit, r1, (int) classPtr );
Bill Buzbeec6f10662010-02-09 11:16:15 -08001605 rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07001606 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
1607 opRegImm(cUnit, kOpCmp, rlSrc.lowReg, 0); /* Null? */
1608 ArmLIR *branch1 = opCondBranch(cUnit, kArmCondEq);
1609 /*
1610 * rlSrc.lowReg now contains object->clazz. Note that
1611 * it could have been allocated r0, but we're okay so long
1612 * as we don't do anything desctructive until r0 is loaded
1613 * with clazz.
1614 */
Ben Chengba4fc8b2009-06-01 13:00:29 -07001615 /* r0 now contains object->clazz */
Bill Buzbee1465db52009-09-23 17:17:35 -07001616 loadWordDisp(cUnit, rlSrc.lowReg, offsetof(Object, clazz), r0);
Ben Chengbd1326d2010-04-02 15:04:53 -07001617 LOAD_FUNC_ADDR(cUnit, r2, (int)dvmInstanceofNonTrivial);
Bill Buzbee1465db52009-09-23 17:17:35 -07001618 opRegReg(cUnit, kOpCmp, r0, r1);
1619 ArmLIR *branch2 = opCondBranch(cUnit, kArmCondEq);
1620 opReg(cUnit, kOpBlx, r2);
Elliott Hughes6a555132010-02-25 15:41:42 -08001621 dvmCompilerClobberCallRegs(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07001622 /*
1623 * If null, check cast failed - punt to the interpreter. Because
1624 * interpreter will be the one throwing, we don't need to
1625 * genExportPC() here.
1626 */
Bill Buzbee270c1d62009-08-13 16:58:07 -07001627 genZeroCheck(cUnit, r0, mir->offset, NULL);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001628 /* check cast passed - branch target here */
Bill Buzbee1465db52009-09-23 17:17:35 -07001629 ArmLIR *target = newLIR0(cUnit, kArmPseudoTargetLabel);
Ben Chengd7d426a2009-09-22 11:23:36 -07001630 target->defMask = ENCODE_ALL;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001631 branch1->generic.target = (LIR *)target;
1632 branch2->generic.target = (LIR *)target;
1633 break;
1634 }
1635 default:
1636 return true;
1637 }
1638 return false;
1639}
1640
1641static bool handleFmt11x(CompilationUnit *cUnit, MIR *mir)
1642{
1643 OpCode dalvikOpCode = mir->dalvikInsn.opCode;
Bill Buzbee1465db52009-09-23 17:17:35 -07001644 RegLocation rlResult;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001645 switch (dalvikOpCode) {
1646 case OP_MOVE_EXCEPTION: {
1647 int offset = offsetof(InterpState, self);
1648 int exOffset = offsetof(Thread, exception);
Bill Buzbeec6f10662010-02-09 11:16:15 -08001649 int selfReg = dvmCompilerAllocTemp(cUnit);
1650 int resetReg = dvmCompilerAllocTemp(cUnit);
1651 RegLocation rlDest = dvmCompilerGetDest(cUnit, mir, 0);
1652 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Bill Buzbee1465db52009-09-23 17:17:35 -07001653 loadWordDisp(cUnit, rGLUE, offset, selfReg);
Bill Buzbeef9f33282009-11-22 12:45:30 -08001654 loadConstant(cUnit, resetReg, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07001655 loadWordDisp(cUnit, selfReg, exOffset, rlResult.lowReg);
Bill Buzbeef9f33282009-11-22 12:45:30 -08001656 storeWordDisp(cUnit, selfReg, exOffset, resetReg);
Bill Buzbee1465db52009-09-23 17:17:35 -07001657 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001658 break;
1659 }
1660 case OP_MOVE_RESULT:
1661 case OP_MOVE_RESULT_OBJECT: {
Bill Buzbeec6f10662010-02-09 11:16:15 -08001662 RegLocation rlDest = dvmCompilerGetDest(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07001663 RegLocation rlSrc = LOC_DALVIK_RETURN_VAL;
1664 rlSrc.fp = rlDest.fp;
1665 storeValue(cUnit, rlDest, rlSrc);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001666 break;
1667 }
1668 case OP_MOVE_RESULT_WIDE: {
Bill Buzbeec6f10662010-02-09 11:16:15 -08001669 RegLocation rlDest = dvmCompilerGetDestWide(cUnit, mir, 0, 1);
Bill Buzbee1465db52009-09-23 17:17:35 -07001670 RegLocation rlSrc = LOC_DALVIK_RETURN_VAL_WIDE;
1671 rlSrc.fp = rlDest.fp;
1672 storeValueWide(cUnit, rlDest, rlSrc);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001673 break;
1674 }
1675 case OP_RETURN_WIDE: {
Bill Buzbeec6f10662010-02-09 11:16:15 -08001676 RegLocation rlSrc = dvmCompilerGetSrcWide(cUnit, mir, 0, 1);
Bill Buzbee1465db52009-09-23 17:17:35 -07001677 RegLocation rlDest = LOC_DALVIK_RETURN_VAL_WIDE;
1678 rlDest.fp = rlSrc.fp;
1679 storeValueWide(cUnit, rlDest, rlSrc);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001680 genReturnCommon(cUnit,mir);
1681 break;
1682 }
1683 case OP_RETURN:
1684 case OP_RETURN_OBJECT: {
Bill Buzbeec6f10662010-02-09 11:16:15 -08001685 RegLocation rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07001686 RegLocation rlDest = LOC_DALVIK_RETURN_VAL;
1687 rlDest.fp = rlSrc.fp;
1688 storeValue(cUnit, rlDest, rlSrc);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001689 genReturnCommon(cUnit,mir);
1690 break;
1691 }
Bill Buzbee1465db52009-09-23 17:17:35 -07001692 case OP_MONITOR_EXIT:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001693 case OP_MONITOR_ENTER:
Bill Buzbeed0937ef2009-12-22 16:15:39 -08001694#if defined(WITH_DEADLOCK_PREDICTION) || defined(WITH_MONITOR_TRACKING)
Ben Cheng5d90c202009-11-22 23:31:11 -08001695 genMonitorPortable(cUnit, mir);
Bill Buzbee1465db52009-09-23 17:17:35 -07001696#else
Ben Cheng5d90c202009-11-22 23:31:11 -08001697 genMonitor(cUnit, mir);
Bill Buzbee1465db52009-09-23 17:17:35 -07001698#endif
Ben Chengba4fc8b2009-06-01 13:00:29 -07001699 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001700 case OP_THROW: {
1701 genInterpSingleStep(cUnit, mir);
1702 break;
1703 }
1704 default:
1705 return true;
1706 }
1707 return false;
1708}
1709
Bill Buzbeed45ba372009-06-15 17:00:57 -07001710static bool handleFmt12x(CompilationUnit *cUnit, MIR *mir)
1711{
1712 OpCode opCode = mir->dalvikInsn.opCode;
Bill Buzbee1465db52009-09-23 17:17:35 -07001713 RegLocation rlDest;
1714 RegLocation rlSrc;
1715 RegLocation rlResult;
Bill Buzbeed45ba372009-06-15 17:00:57 -07001716
Ben Chengba4fc8b2009-06-01 13:00:29 -07001717 if ( (opCode >= OP_ADD_INT_2ADDR) && (opCode <= OP_REM_DOUBLE_2ADDR)) {
Ben Cheng5d90c202009-11-22 23:31:11 -08001718 return genArithOp( cUnit, mir );
Ben Chengba4fc8b2009-06-01 13:00:29 -07001719 }
1720
Bill Buzbee1465db52009-09-23 17:17:35 -07001721 if (mir->ssaRep->numUses == 2)
Bill Buzbeec6f10662010-02-09 11:16:15 -08001722 rlSrc = dvmCompilerGetSrcWide(cUnit, mir, 0, 1);
Bill Buzbee1465db52009-09-23 17:17:35 -07001723 else
Bill Buzbeec6f10662010-02-09 11:16:15 -08001724 rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07001725 if (mir->ssaRep->numDefs == 2)
Bill Buzbeec6f10662010-02-09 11:16:15 -08001726 rlDest = dvmCompilerGetDestWide(cUnit, mir, 0, 1);
Bill Buzbee1465db52009-09-23 17:17:35 -07001727 else
Bill Buzbeec6f10662010-02-09 11:16:15 -08001728 rlDest = dvmCompilerGetDest(cUnit, mir, 0);
Ben Chenge9695e52009-06-16 16:11:47 -07001729
Ben Chengba4fc8b2009-06-01 13:00:29 -07001730 switch (opCode) {
Bill Buzbee1465db52009-09-23 17:17:35 -07001731 case OP_DOUBLE_TO_INT:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001732 case OP_INT_TO_FLOAT:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001733 case OP_FLOAT_TO_INT:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001734 case OP_DOUBLE_TO_FLOAT:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001735 case OP_FLOAT_TO_DOUBLE:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001736 case OP_INT_TO_DOUBLE:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001737 case OP_FLOAT_TO_LONG:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001738 case OP_LONG_TO_FLOAT:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001739 case OP_DOUBLE_TO_LONG:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001740 case OP_LONG_TO_DOUBLE:
Ben Cheng5d90c202009-11-22 23:31:11 -08001741 return genConversion(cUnit, mir);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001742 case OP_NEG_INT:
1743 case OP_NOT_INT:
Ben Cheng5d90c202009-11-22 23:31:11 -08001744 return genArithOpInt(cUnit, mir, rlDest, rlSrc, rlSrc);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001745 case OP_NEG_LONG:
1746 case OP_NOT_LONG:
Ben Cheng5d90c202009-11-22 23:31:11 -08001747 return genArithOpLong(cUnit, mir, rlDest, rlSrc, rlSrc);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001748 case OP_NEG_FLOAT:
Ben Cheng5d90c202009-11-22 23:31:11 -08001749 return genArithOpFloat(cUnit, mir, rlDest, rlSrc, rlSrc);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001750 case OP_NEG_DOUBLE:
Ben Cheng5d90c202009-11-22 23:31:11 -08001751 return genArithOpDouble(cUnit, mir, rlDest, rlSrc, rlSrc);
Bill Buzbee1465db52009-09-23 17:17:35 -07001752 case OP_MOVE_WIDE:
1753 storeValueWide(cUnit, rlDest, rlSrc);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001754 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07001755 case OP_INT_TO_LONG:
Bill Buzbeec6f10662010-02-09 11:16:15 -08001756 rlSrc = dvmCompilerUpdateLoc(cUnit, rlSrc);
1757 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Bill Buzbee964a7b02010-01-28 12:54:19 -08001758 //TUNING: shouldn't loadValueDirect already check for phys reg?
Bill Buzbee1465db52009-09-23 17:17:35 -07001759 if (rlSrc.location == kLocPhysReg) {
1760 genRegCopy(cUnit, rlResult.lowReg, rlSrc.lowReg);
1761 } else {
1762 loadValueDirect(cUnit, rlSrc, rlResult.lowReg);
1763 }
1764 opRegRegImm(cUnit, kOpAsr, rlResult.highReg,
1765 rlResult.lowReg, 31);
1766 storeValueWide(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001767 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07001768 case OP_LONG_TO_INT:
Bill Buzbeec6f10662010-02-09 11:16:15 -08001769 rlSrc = dvmCompilerUpdateLocWide(cUnit, rlSrc);
1770 rlSrc = dvmCompilerWideToNarrow(cUnit, rlSrc);
Bill Buzbee1465db52009-09-23 17:17:35 -07001771 // Intentional fallthrough
Ben Chengba4fc8b2009-06-01 13:00:29 -07001772 case OP_MOVE:
1773 case OP_MOVE_OBJECT:
Bill Buzbee1465db52009-09-23 17:17:35 -07001774 storeValue(cUnit, rlDest, rlSrc);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001775 break;
1776 case OP_INT_TO_BYTE:
Bill Buzbee1465db52009-09-23 17:17:35 -07001777 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
Bill Buzbeec6f10662010-02-09 11:16:15 -08001778 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Bill Buzbee1465db52009-09-23 17:17:35 -07001779 opRegReg(cUnit, kOp2Byte, rlResult.lowReg, rlSrc.lowReg);
1780 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001781 break;
1782 case OP_INT_TO_SHORT:
Bill Buzbee1465db52009-09-23 17:17:35 -07001783 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
Bill Buzbeec6f10662010-02-09 11:16:15 -08001784 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Bill Buzbee1465db52009-09-23 17:17:35 -07001785 opRegReg(cUnit, kOp2Short, rlResult.lowReg, rlSrc.lowReg);
1786 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001787 break;
1788 case OP_INT_TO_CHAR:
Bill Buzbee1465db52009-09-23 17:17:35 -07001789 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
Bill Buzbeec6f10662010-02-09 11:16:15 -08001790 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Bill Buzbee1465db52009-09-23 17:17:35 -07001791 opRegReg(cUnit, kOp2Char, rlResult.lowReg, rlSrc.lowReg);
1792 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001793 break;
1794 case OP_ARRAY_LENGTH: {
1795 int lenOffset = offsetof(ArrayObject, length);
Bill Buzbee1465db52009-09-23 17:17:35 -07001796 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
1797 genNullCheck(cUnit, rlSrc.sRegLow, rlSrc.lowReg,
1798 mir->offset, NULL);
Bill Buzbeec6f10662010-02-09 11:16:15 -08001799 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Bill Buzbee1465db52009-09-23 17:17:35 -07001800 loadWordDisp(cUnit, rlSrc.lowReg, lenOffset,
1801 rlResult.lowReg);
1802 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001803 break;
1804 }
1805 default:
1806 return true;
1807 }
1808 return false;
1809}
1810
1811static bool handleFmt21s(CompilationUnit *cUnit, MIR *mir)
1812{
1813 OpCode dalvikOpCode = mir->dalvikInsn.opCode;
Bill Buzbee1465db52009-09-23 17:17:35 -07001814 RegLocation rlDest;
1815 RegLocation rlResult;
1816 int BBBB = mir->dalvikInsn.vB;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001817 if (dalvikOpCode == OP_CONST_WIDE_16) {
Bill Buzbeec6f10662010-02-09 11:16:15 -08001818 rlDest = dvmCompilerGetDestWide(cUnit, mir, 0, 1);
1819 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Ben Chengbd1326d2010-04-02 15:04:53 -07001820 loadConstantNoClobber(cUnit, rlResult.lowReg, BBBB);
Bill Buzbee964a7b02010-01-28 12:54:19 -08001821 //TUNING: do high separately to avoid load dependency
Bill Buzbee1465db52009-09-23 17:17:35 -07001822 opRegRegImm(cUnit, kOpAsr, rlResult.highReg, rlResult.lowReg, 31);
1823 storeValueWide(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001824 } else if (dalvikOpCode == OP_CONST_16) {
Bill Buzbeec6f10662010-02-09 11:16:15 -08001825 rlDest = dvmCompilerGetDest(cUnit, mir, 0);
1826 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kAnyReg, true);
Ben Chengbd1326d2010-04-02 15:04:53 -07001827 loadConstantNoClobber(cUnit, rlResult.lowReg, BBBB);
Bill Buzbee1465db52009-09-23 17:17:35 -07001828 storeValue(cUnit, rlDest, rlResult);
1829 } else
Ben Chengba4fc8b2009-06-01 13:00:29 -07001830 return true;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001831 return false;
1832}
1833
1834/* Compare agaist zero */
1835static bool handleFmt21t(CompilationUnit *cUnit, MIR *mir, BasicBlock *bb,
Bill Buzbee89efc3d2009-07-28 11:22:22 -07001836 ArmLIR *labelList)
Ben Chengba4fc8b2009-06-01 13:00:29 -07001837{
1838 OpCode dalvikOpCode = mir->dalvikInsn.opCode;
Bill Buzbee89efc3d2009-07-28 11:22:22 -07001839 ArmConditionCode cond;
Bill Buzbeec6f10662010-02-09 11:16:15 -08001840 RegLocation rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07001841 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
1842 opRegImm(cUnit, kOpCmp, rlSrc.lowReg, 0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001843
Bill Buzbee270c1d62009-08-13 16:58:07 -07001844//TUNING: break this out to allow use of Thumb2 CB[N]Z
Ben Chengba4fc8b2009-06-01 13:00:29 -07001845 switch (dalvikOpCode) {
1846 case OP_IF_EQZ:
Bill Buzbee1465db52009-09-23 17:17:35 -07001847 cond = kArmCondEq;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001848 break;
1849 case OP_IF_NEZ:
Bill Buzbee1465db52009-09-23 17:17:35 -07001850 cond = kArmCondNe;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001851 break;
1852 case OP_IF_LTZ:
Bill Buzbee1465db52009-09-23 17:17:35 -07001853 cond = kArmCondLt;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001854 break;
1855 case OP_IF_GEZ:
Bill Buzbee1465db52009-09-23 17:17:35 -07001856 cond = kArmCondGe;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001857 break;
1858 case OP_IF_GTZ:
Bill Buzbee1465db52009-09-23 17:17:35 -07001859 cond = kArmCondGt;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001860 break;
1861 case OP_IF_LEZ:
Bill Buzbee1465db52009-09-23 17:17:35 -07001862 cond = kArmCondLe;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001863 break;
1864 default:
1865 cond = 0;
1866 LOGE("Unexpected opcode (%d) for Fmt21t\n", dalvikOpCode);
Bill Buzbeefc519dc2010-03-06 23:30:57 -08001867 dvmCompilerAbort(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001868 }
1869 genConditionalBranch(cUnit, cond, &labelList[bb->taken->id]);
1870 /* This mostly likely will be optimized away in a later phase */
1871 genUnconditionalBranch(cUnit, &labelList[bb->fallThrough->id]);
1872 return false;
1873}
1874
Elliott Hughesb4c05972010-02-24 16:36:18 -08001875static bool isPowerOfTwo(int x)
1876{
1877 return (x & (x - 1)) == 0;
1878}
1879
1880// Returns true if no more than two bits are set in 'x'.
1881static bool isPopCountLE2(unsigned int x)
1882{
1883 x &= x - 1;
1884 return (x & (x - 1)) == 0;
1885}
1886
1887// Returns the index of the lowest set bit in 'x'.
1888static int lowestSetBit(unsigned int x) {
1889 int bit_posn = 0;
1890 while ((x & 0xf) == 0) {
1891 bit_posn += 4;
1892 x >>= 4;
Bill Buzbee78cb0e22010-02-11 14:04:53 -08001893 }
Elliott Hughesb4c05972010-02-24 16:36:18 -08001894 while ((x & 1) == 0) {
1895 bit_posn++;
1896 x >>= 1;
1897 }
1898 return bit_posn;
1899}
1900
Elliott Hughes672511b2010-04-26 17:40:13 -07001901// Returns true if it added instructions to 'cUnit' to divide 'rlSrc' by 'lit'
1902// and store the result in 'rlDest'.
Elliott Hughesc7ad9b22010-04-28 13:52:02 -07001903static bool handleEasyDivide(CompilationUnit *cUnit, OpCode dalvikOpCode,
Elliott Hughes672511b2010-04-26 17:40:13 -07001904 RegLocation rlSrc, RegLocation rlDest, int lit)
1905{
1906 if (lit < 2 || !isPowerOfTwo(lit)) {
1907 return false;
1908 }
1909 int k = lowestSetBit(lit);
1910 if (k >= 30) {
1911 // Avoid special cases.
1912 return false;
1913 }
Elliott Hughes9c457022010-04-28 16:15:38 -07001914 bool div = (dalvikOpCode == OP_DIV_INT_LIT8 || dalvikOpCode == OP_DIV_INT_LIT16);
Elliott Hughes672511b2010-04-26 17:40:13 -07001915 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
1916 RegLocation rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Elliott Hughes9c457022010-04-28 16:15:38 -07001917 if (div) {
1918 int tReg = dvmCompilerAllocTemp(cUnit);
1919 if (lit == 2) {
1920 // Division by 2 is by far the most common division by constant.
1921 opRegRegImm(cUnit, kOpLsr, tReg, rlSrc.lowReg, 32 - k);
1922 opRegRegReg(cUnit, kOpAdd, tReg, tReg, rlSrc.lowReg);
1923 opRegRegImm(cUnit, kOpAsr, rlResult.lowReg, tReg, k);
1924 } else {
1925 opRegRegImm(cUnit, kOpAsr, tReg, rlSrc.lowReg, 31);
1926 opRegRegImm(cUnit, kOpLsr, tReg, tReg, 32 - k);
1927 opRegRegReg(cUnit, kOpAdd, tReg, tReg, rlSrc.lowReg);
1928 opRegRegImm(cUnit, kOpAsr, rlResult.lowReg, tReg, k);
1929 }
Elliott Hughes672511b2010-04-26 17:40:13 -07001930 } else {
Elliott Hughes9c457022010-04-28 16:15:38 -07001931 int cReg = dvmCompilerAllocTemp(cUnit);
1932 loadConstant(cUnit, cReg, lit - 1);
1933 int tReg1 = dvmCompilerAllocTemp(cUnit);
1934 int tReg2 = dvmCompilerAllocTemp(cUnit);
1935 if (lit == 2) {
1936 opRegRegImm(cUnit, kOpLsr, tReg1, rlSrc.lowReg, 32 - k);
1937 opRegRegReg(cUnit, kOpAdd, tReg2, tReg1, rlSrc.lowReg);
1938 opRegRegReg(cUnit, kOpAnd, tReg2, tReg2, cReg);
1939 opRegRegReg(cUnit, kOpSub, rlResult.lowReg, tReg2, tReg1);
1940 } else {
1941 opRegRegImm(cUnit, kOpAsr, tReg1, rlSrc.lowReg, 31);
1942 opRegRegImm(cUnit, kOpLsr, tReg1, tReg1, 32 - k);
1943 opRegRegReg(cUnit, kOpAdd, tReg2, tReg1, rlSrc.lowReg);
1944 opRegRegReg(cUnit, kOpAnd, tReg2, tReg2, cReg);
1945 opRegRegReg(cUnit, kOpSub, rlResult.lowReg, tReg2, tReg1);
1946 }
Elliott Hughes672511b2010-04-26 17:40:13 -07001947 }
1948 storeValue(cUnit, rlDest, rlResult);
1949 return true;
1950}
1951
Elliott Hughesb4c05972010-02-24 16:36:18 -08001952// Returns true if it added instructions to 'cUnit' to multiply 'rlSrc' by 'lit'
1953// and store the result in 'rlDest'.
1954static bool handleEasyMultiply(CompilationUnit *cUnit,
1955 RegLocation rlSrc, RegLocation rlDest, int lit)
1956{
1957 // Can we simplify this multiplication?
1958 bool powerOfTwo = false;
1959 bool popCountLE2 = false;
1960 bool powerOfTwoMinusOne = false;
1961 if (lit < 2) {
1962 // Avoid special cases.
1963 return false;
1964 } else if (isPowerOfTwo(lit)) {
1965 powerOfTwo = true;
1966 } else if (isPopCountLE2(lit)) {
1967 popCountLE2 = true;
1968 } else if (isPowerOfTwo(lit + 1)) {
1969 powerOfTwoMinusOne = true;
1970 } else {
1971 return false;
1972 }
1973 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
1974 RegLocation rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
1975 if (powerOfTwo) {
1976 // Shift.
1977 opRegRegImm(cUnit, kOpLsl, rlResult.lowReg, rlSrc.lowReg,
1978 lowestSetBit(lit));
1979 } else if (popCountLE2) {
1980 // Shift and add and shift.
1981 int firstBit = lowestSetBit(lit);
1982 int secondBit = lowestSetBit(lit ^ (1 << firstBit));
1983 genMultiplyByTwoBitMultiplier(cUnit, rlSrc, rlResult, lit,
1984 firstBit, secondBit);
1985 } else {
1986 // Reverse subtract: (src << (shift + 1)) - src.
1987 assert(powerOfTwoMinusOne);
1988 // TODO: rsb dst, src, src lsl#lowestSetBit(lit + 1)
1989 int tReg = dvmCompilerAllocTemp(cUnit);
1990 opRegRegImm(cUnit, kOpLsl, tReg, rlSrc.lowReg, lowestSetBit(lit + 1));
1991 opRegRegReg(cUnit, kOpSub, rlResult.lowReg, tReg, rlSrc.lowReg);
1992 }
1993 storeValue(cUnit, rlDest, rlResult);
1994 return true;
Bill Buzbee78cb0e22010-02-11 14:04:53 -08001995}
1996
Ben Chengba4fc8b2009-06-01 13:00:29 -07001997static bool handleFmt22b_Fmt22s(CompilationUnit *cUnit, MIR *mir)
1998{
1999 OpCode dalvikOpCode = mir->dalvikInsn.opCode;
Bill Buzbeec6f10662010-02-09 11:16:15 -08002000 RegLocation rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
2001 RegLocation rlDest = dvmCompilerGetDest(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07002002 RegLocation rlResult;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002003 int lit = mir->dalvikInsn.vC;
Ben Cheng4f489172009-09-27 17:08:35 -07002004 OpKind op = 0; /* Make gcc happy */
Bill Buzbee1465db52009-09-23 17:17:35 -07002005 int shiftOp = false;
2006 bool isDiv = false;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002007
Ben Chengba4fc8b2009-06-01 13:00:29 -07002008 switch (dalvikOpCode) {
Bill Buzbee1465db52009-09-23 17:17:35 -07002009 case OP_RSUB_INT_LIT8:
2010 case OP_RSUB_INT: {
2011 int tReg;
2012 //TUNING: add support for use of Arm rsub op
2013 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
Bill Buzbeec6f10662010-02-09 11:16:15 -08002014 tReg = dvmCompilerAllocTemp(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07002015 loadConstant(cUnit, tReg, lit);
Bill Buzbeec6f10662010-02-09 11:16:15 -08002016 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Bill Buzbee1465db52009-09-23 17:17:35 -07002017 opRegRegReg(cUnit, kOpSub, rlResult.lowReg,
2018 tReg, rlSrc.lowReg);
2019 storeValue(cUnit, rlDest, rlResult);
2020 return false;
2021 break;
2022 }
2023
Ben Chengba4fc8b2009-06-01 13:00:29 -07002024 case OP_ADD_INT_LIT8:
2025 case OP_ADD_INT_LIT16:
Bill Buzbee1465db52009-09-23 17:17:35 -07002026 op = kOpAdd;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002027 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002028 case OP_MUL_INT_LIT8:
Bill Buzbee78cb0e22010-02-11 14:04:53 -08002029 case OP_MUL_INT_LIT16: {
Elliott Hughesb4c05972010-02-24 16:36:18 -08002030 if (handleEasyMultiply(cUnit, rlSrc, rlDest, lit)) {
2031 return false;
Bill Buzbee78cb0e22010-02-11 14:04:53 -08002032 }
Elliott Hughesb4c05972010-02-24 16:36:18 -08002033 op = kOpMul;
Bill Buzbee1465db52009-09-23 17:17:35 -07002034 break;
Bill Buzbee78cb0e22010-02-11 14:04:53 -08002035 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07002036 case OP_AND_INT_LIT8:
2037 case OP_AND_INT_LIT16:
Bill Buzbee1465db52009-09-23 17:17:35 -07002038 op = kOpAnd;
2039 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002040 case OP_OR_INT_LIT8:
2041 case OP_OR_INT_LIT16:
Bill Buzbee1465db52009-09-23 17:17:35 -07002042 op = kOpOr;
2043 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002044 case OP_XOR_INT_LIT8:
2045 case OP_XOR_INT_LIT16:
Bill Buzbee1465db52009-09-23 17:17:35 -07002046 op = kOpXor;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002047 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002048 case OP_SHL_INT_LIT8:
Bill Buzbee0e605272009-12-01 14:28:05 -08002049 lit &= 31;
Bill Buzbee1465db52009-09-23 17:17:35 -07002050 shiftOp = true;
2051 op = kOpLsl;
2052 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002053 case OP_SHR_INT_LIT8:
Bill Buzbee0e605272009-12-01 14:28:05 -08002054 lit &= 31;
Bill Buzbee1465db52009-09-23 17:17:35 -07002055 shiftOp = true;
2056 op = kOpAsr;
2057 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002058 case OP_USHR_INT_LIT8:
Bill Buzbee0e605272009-12-01 14:28:05 -08002059 lit &= 31;
Bill Buzbee1465db52009-09-23 17:17:35 -07002060 shiftOp = true;
2061 op = kOpLsr;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002062 break;
2063
2064 case OP_DIV_INT_LIT8:
2065 case OP_DIV_INT_LIT16:
Ben Chengba4fc8b2009-06-01 13:00:29 -07002066 case OP_REM_INT_LIT8:
2067 case OP_REM_INT_LIT16:
2068 if (lit == 0) {
2069 /* Let the interpreter deal with div by 0 */
2070 genInterpSingleStep(cUnit, mir);
2071 return false;
2072 }
Elliott Hughesc7ad9b22010-04-28 13:52:02 -07002073 if (handleEasyDivide(cUnit, dalvikOpCode, rlSrc, rlDest, lit)) {
Elliott Hughes672511b2010-04-26 17:40:13 -07002074 return false;
2075 }
Bill Buzbeec6f10662010-02-09 11:16:15 -08002076 dvmCompilerFlushAllRegs(cUnit); /* Everything to home location */
Bill Buzbee1465db52009-09-23 17:17:35 -07002077 loadValueDirectFixed(cUnit, rlSrc, r0);
Bill Buzbeec6f10662010-02-09 11:16:15 -08002078 dvmCompilerClobber(cUnit, r0);
Bill Buzbee1465db52009-09-23 17:17:35 -07002079 if ((dalvikOpCode == OP_DIV_INT_LIT8) ||
2080 (dalvikOpCode == OP_DIV_INT_LIT16)) {
Ben Chengbd1326d2010-04-02 15:04:53 -07002081 LOAD_FUNC_ADDR(cUnit, r2, (int)__aeabi_idiv);
Bill Buzbee1465db52009-09-23 17:17:35 -07002082 isDiv = true;
2083 } else {
Ben Chengbd1326d2010-04-02 15:04:53 -07002084 LOAD_FUNC_ADDR(cUnit, r2, (int)__aeabi_idivmod);
Bill Buzbee1465db52009-09-23 17:17:35 -07002085 isDiv = false;
2086 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07002087 loadConstant(cUnit, r1, lit);
Bill Buzbee1465db52009-09-23 17:17:35 -07002088 opReg(cUnit, kOpBlx, r2);
Elliott Hughes6a555132010-02-25 15:41:42 -08002089 dvmCompilerClobberCallRegs(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07002090 if (isDiv)
Bill Buzbeec6f10662010-02-09 11:16:15 -08002091 rlResult = dvmCompilerGetReturn(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07002092 else
Bill Buzbeec6f10662010-02-09 11:16:15 -08002093 rlResult = dvmCompilerGetReturnAlt(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07002094 storeValue(cUnit, rlDest, rlResult);
2095 return false;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002096 break;
2097 default:
2098 return true;
2099 }
Bill Buzbee1465db52009-09-23 17:17:35 -07002100 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
Bill Buzbeec6f10662010-02-09 11:16:15 -08002101 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Bill Buzbee1465db52009-09-23 17:17:35 -07002102 // Avoid shifts by literal 0 - no support in Thumb. Change to copy
2103 if (shiftOp && (lit == 0)) {
2104 genRegCopy(cUnit, rlResult.lowReg, rlSrc.lowReg);
2105 } else {
2106 opRegRegImm(cUnit, op, rlResult.lowReg, rlSrc.lowReg, lit);
2107 }
2108 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002109 return false;
2110}
2111
2112static bool handleFmt22c(CompilationUnit *cUnit, MIR *mir)
2113{
2114 OpCode dalvikOpCode = mir->dalvikInsn.opCode;
2115 int fieldOffset;
2116
2117 if (dalvikOpCode >= OP_IGET && dalvikOpCode <= OP_IPUT_SHORT) {
2118 InstField *pInstField = (InstField *)
2119 cUnit->method->clazz->pDvmDex->pResFields[mir->dalvikInsn.vC];
Ben Chengba4fc8b2009-06-01 13:00:29 -07002120
2121 assert(pInstField != NULL);
2122 fieldOffset = pInstField->byteOffset;
2123 } else {
Ben Chenga0e7b602009-10-13 23:09:01 -07002124 /* Deliberately break the code while make the compiler happy */
2125 fieldOffset = -1;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002126 }
2127 switch (dalvikOpCode) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07002128 case OP_NEW_ARRAY: {
Bill Buzbee1465db52009-09-23 17:17:35 -07002129 // Generates a call - use explicit registers
Bill Buzbeec6f10662010-02-09 11:16:15 -08002130 RegLocation rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
2131 RegLocation rlDest = dvmCompilerGetDest(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07002132 RegLocation rlResult;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002133 void *classPtr = (void*)
2134 (cUnit->method->clazz->pDvmDex->pResClasses[mir->dalvikInsn.vC]);
2135 assert(classPtr != NULL);
Bill Buzbeec6f10662010-02-09 11:16:15 -08002136 dvmCompilerFlushAllRegs(cUnit); /* Everything to home location */
Bill Buzbee1465db52009-09-23 17:17:35 -07002137 genExportPC(cUnit, mir);
2138 loadValueDirectFixed(cUnit, rlSrc, r1); /* Len */
Ben Chengba4fc8b2009-06-01 13:00:29 -07002139 loadConstant(cUnit, r0, (int) classPtr );
Ben Chengbd1326d2010-04-02 15:04:53 -07002140 LOAD_FUNC_ADDR(cUnit, r3, (int)dvmAllocArrayByClass);
Ben Cheng4f489172009-09-27 17:08:35 -07002141 /*
2142 * "len < 0": bail to the interpreter to re-execute the
2143 * instruction
2144 */
Bill Buzbee89efc3d2009-07-28 11:22:22 -07002145 ArmLIR *pcrLabel =
Bill Buzbee1465db52009-09-23 17:17:35 -07002146 genRegImmCheck(cUnit, kArmCondMi, r1, 0, mir->offset, NULL);
Bill Buzbee270c1d62009-08-13 16:58:07 -07002147 loadConstant(cUnit, r2, ALLOC_DONT_TRACK);
Bill Buzbee1465db52009-09-23 17:17:35 -07002148 opReg(cUnit, kOpBlx, r3);
Elliott Hughes6a555132010-02-25 15:41:42 -08002149 dvmCompilerClobberCallRegs(cUnit);
Ben Cheng4f489172009-09-27 17:08:35 -07002150 /* generate a branch over if allocation is successful */
Bill Buzbee1465db52009-09-23 17:17:35 -07002151 opRegImm(cUnit, kOpCmp, r0, 0); /* NULL? */
2152 ArmLIR *branchOver = opCondBranch(cUnit, kArmCondNe);
Ben Cheng4f489172009-09-27 17:08:35 -07002153 /*
2154 * OOM exception needs to be thrown here and cannot re-execute
2155 */
2156 loadConstant(cUnit, r0,
2157 (int) (cUnit->method->insns + mir->offset));
2158 genDispatchToHandler(cUnit, TEMPLATE_THROW_EXCEPTION_COMMON);
2159 /* noreturn */
2160
Bill Buzbee1465db52009-09-23 17:17:35 -07002161 ArmLIR *target = newLIR0(cUnit, kArmPseudoTargetLabel);
Ben Cheng4f489172009-09-27 17:08:35 -07002162 target->defMask = ENCODE_ALL;
2163 branchOver->generic.target = (LIR *) target;
Bill Buzbeec6f10662010-02-09 11:16:15 -08002164 rlResult = dvmCompilerGetReturn(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07002165 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002166 break;
2167 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07002168 case OP_INSTANCE_OF: {
Bill Buzbee1465db52009-09-23 17:17:35 -07002169 // May generate a call - use explicit registers
Bill Buzbeec6f10662010-02-09 11:16:15 -08002170 RegLocation rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
2171 RegLocation rlDest = dvmCompilerGetDest(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07002172 RegLocation rlResult;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002173 ClassObject *classPtr =
2174 (cUnit->method->clazz->pDvmDex->pResClasses[mir->dalvikInsn.vC]);
Bill Buzbee480e6782010-01-27 15:43:08 -08002175 /*
2176 * Note: It is possible that classPtr is NULL at this point,
2177 * even though this instruction has been successfully interpreted.
2178 * If the previous interpretation had a null source, the
2179 * interpreter would not have bothered to resolve the clazz.
2180 * Bail out to the interpreter in this case, and log it
2181 * so that we can tell if it happens frequently.
2182 */
2183 if (classPtr == NULL) {
2184 LOGD("null clazz in OP_INSTANCE_OF, single-stepping");
2185 genInterpSingleStep(cUnit, mir);
2186 break;
2187 }
Bill Buzbeec6f10662010-02-09 11:16:15 -08002188 dvmCompilerFlushAllRegs(cUnit); /* Everything to home location */
Bill Buzbee1465db52009-09-23 17:17:35 -07002189 loadValueDirectFixed(cUnit, rlSrc, r0); /* Ref */
Ben Chengba4fc8b2009-06-01 13:00:29 -07002190 loadConstant(cUnit, r2, (int) classPtr );
Bill Buzbee270c1d62009-08-13 16:58:07 -07002191//TUNING: compare to 0 primative to allow use of CB[N]Z
Bill Buzbee1465db52009-09-23 17:17:35 -07002192 opRegImm(cUnit, kOpCmp, r0, 0); /* NULL? */
Ben Cheng752c7942009-06-22 10:50:07 -07002193 /* When taken r0 has NULL which can be used for store directly */
Bill Buzbee1465db52009-09-23 17:17:35 -07002194 ArmLIR *branch1 = opCondBranch(cUnit, kArmCondEq);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002195 /* r1 now contains object->clazz */
Bill Buzbee270c1d62009-08-13 16:58:07 -07002196 loadWordDisp(cUnit, r0, offsetof(Object, clazz), r1);
Bill Buzbee1465db52009-09-23 17:17:35 -07002197 /* r1 now contains object->clazz */
Ben Chengbd1326d2010-04-02 15:04:53 -07002198 LOAD_FUNC_ADDR(cUnit, r3, (int)dvmInstanceofNonTrivial);
Ben Cheng752c7942009-06-22 10:50:07 -07002199 loadConstant(cUnit, r0, 1); /* Assume true */
Bill Buzbee1465db52009-09-23 17:17:35 -07002200 opRegReg(cUnit, kOpCmp, r1, r2);
2201 ArmLIR *branch2 = opCondBranch(cUnit, kArmCondEq);
2202 genRegCopy(cUnit, r0, r1);
2203 genRegCopy(cUnit, r1, r2);
2204 opReg(cUnit, kOpBlx, r3);
Elliott Hughes6a555132010-02-25 15:41:42 -08002205 dvmCompilerClobberCallRegs(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002206 /* branch target here */
Bill Buzbee1465db52009-09-23 17:17:35 -07002207 ArmLIR *target = newLIR0(cUnit, kArmPseudoTargetLabel);
Ben Chengd7d426a2009-09-22 11:23:36 -07002208 target->defMask = ENCODE_ALL;
Bill Buzbeec6f10662010-02-09 11:16:15 -08002209 rlResult = dvmCompilerGetReturn(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07002210 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002211 branch1->generic.target = (LIR *)target;
2212 branch2->generic.target = (LIR *)target;
2213 break;
2214 }
2215 case OP_IGET_WIDE:
2216 genIGetWide(cUnit, mir, fieldOffset);
2217 break;
2218 case OP_IGET:
2219 case OP_IGET_OBJECT:
Bill Buzbee1465db52009-09-23 17:17:35 -07002220 genIGet(cUnit, mir, kWord, fieldOffset);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002221 break;
2222 case OP_IGET_BOOLEAN:
Bill Buzbee1465db52009-09-23 17:17:35 -07002223 genIGet(cUnit, mir, kUnsignedByte, fieldOffset);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002224 break;
2225 case OP_IGET_BYTE:
Bill Buzbee1465db52009-09-23 17:17:35 -07002226 genIGet(cUnit, mir, kSignedByte, fieldOffset);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002227 break;
2228 case OP_IGET_CHAR:
Bill Buzbee1465db52009-09-23 17:17:35 -07002229 genIGet(cUnit, mir, kUnsignedHalf, fieldOffset);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002230 break;
2231 case OP_IGET_SHORT:
Bill Buzbee1465db52009-09-23 17:17:35 -07002232 genIGet(cUnit, mir, kSignedHalf, fieldOffset);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002233 break;
2234 case OP_IPUT_WIDE:
2235 genIPutWide(cUnit, mir, fieldOffset);
2236 break;
2237 case OP_IPUT:
2238 case OP_IPUT_OBJECT:
Bill Buzbee1465db52009-09-23 17:17:35 -07002239 genIPut(cUnit, mir, kWord, fieldOffset);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002240 break;
2241 case OP_IPUT_SHORT:
2242 case OP_IPUT_CHAR:
Bill Buzbee1465db52009-09-23 17:17:35 -07002243 genIPut(cUnit, mir, kUnsignedHalf, fieldOffset);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002244 break;
2245 case OP_IPUT_BYTE:
2246 case OP_IPUT_BOOLEAN:
Bill Buzbee1465db52009-09-23 17:17:35 -07002247 genIPut(cUnit, mir, kUnsignedByte, fieldOffset);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002248 break;
Bill Buzbeeb16344a2010-03-15 17:19:12 -07002249 case OP_IGET_WIDE_VOLATILE:
2250 case OP_IPUT_WIDE_VOLATILE:
2251 case OP_SGET_WIDE_VOLATILE:
2252 case OP_SPUT_WIDE_VOLATILE:
2253 genInterpSingleStep(cUnit, mir);
2254 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002255 default:
2256 return true;
2257 }
2258 return false;
2259}
2260
2261static bool handleFmt22cs(CompilationUnit *cUnit, MIR *mir)
2262{
2263 OpCode dalvikOpCode = mir->dalvikInsn.opCode;
2264 int fieldOffset = mir->dalvikInsn.vC;
2265 switch (dalvikOpCode) {
2266 case OP_IGET_QUICK:
2267 case OP_IGET_OBJECT_QUICK:
Bill Buzbee1465db52009-09-23 17:17:35 -07002268 genIGet(cUnit, mir, kWord, fieldOffset);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002269 break;
2270 case OP_IPUT_QUICK:
2271 case OP_IPUT_OBJECT_QUICK:
Bill Buzbee1465db52009-09-23 17:17:35 -07002272 genIPut(cUnit, mir, kWord, fieldOffset);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002273 break;
2274 case OP_IGET_WIDE_QUICK:
2275 genIGetWide(cUnit, mir, fieldOffset);
2276 break;
2277 case OP_IPUT_WIDE_QUICK:
2278 genIPutWide(cUnit, mir, fieldOffset);
2279 break;
2280 default:
2281 return true;
2282 }
2283 return false;
2284
2285}
2286
2287/* Compare agaist zero */
2288static bool handleFmt22t(CompilationUnit *cUnit, MIR *mir, BasicBlock *bb,
Bill Buzbee89efc3d2009-07-28 11:22:22 -07002289 ArmLIR *labelList)
Ben Chengba4fc8b2009-06-01 13:00:29 -07002290{
2291 OpCode dalvikOpCode = mir->dalvikInsn.opCode;
Bill Buzbee89efc3d2009-07-28 11:22:22 -07002292 ArmConditionCode cond;
Bill Buzbeec6f10662010-02-09 11:16:15 -08002293 RegLocation rlSrc1 = dvmCompilerGetSrc(cUnit, mir, 0);
2294 RegLocation rlSrc2 = dvmCompilerGetSrc(cUnit, mir, 1);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002295
Bill Buzbee1465db52009-09-23 17:17:35 -07002296 rlSrc1 = loadValue(cUnit, rlSrc1, kCoreReg);
2297 rlSrc2 = loadValue(cUnit, rlSrc2, kCoreReg);
2298 opRegReg(cUnit, kOpCmp, rlSrc1.lowReg, rlSrc2.lowReg);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002299
2300 switch (dalvikOpCode) {
2301 case OP_IF_EQ:
Bill Buzbee1465db52009-09-23 17:17:35 -07002302 cond = kArmCondEq;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002303 break;
2304 case OP_IF_NE:
Bill Buzbee1465db52009-09-23 17:17:35 -07002305 cond = kArmCondNe;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002306 break;
2307 case OP_IF_LT:
Bill Buzbee1465db52009-09-23 17:17:35 -07002308 cond = kArmCondLt;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002309 break;
2310 case OP_IF_GE:
Bill Buzbee1465db52009-09-23 17:17:35 -07002311 cond = kArmCondGe;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002312 break;
2313 case OP_IF_GT:
Bill Buzbee1465db52009-09-23 17:17:35 -07002314 cond = kArmCondGt;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002315 break;
2316 case OP_IF_LE:
Bill Buzbee1465db52009-09-23 17:17:35 -07002317 cond = kArmCondLe;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002318 break;
2319 default:
2320 cond = 0;
2321 LOGE("Unexpected opcode (%d) for Fmt22t\n", dalvikOpCode);
Bill Buzbeefc519dc2010-03-06 23:30:57 -08002322 dvmCompilerAbort(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002323 }
2324 genConditionalBranch(cUnit, cond, &labelList[bb->taken->id]);
2325 /* This mostly likely will be optimized away in a later phase */
2326 genUnconditionalBranch(cUnit, &labelList[bb->fallThrough->id]);
2327 return false;
2328}
2329
2330static bool handleFmt22x_Fmt32x(CompilationUnit *cUnit, MIR *mir)
2331{
2332 OpCode opCode = mir->dalvikInsn.opCode;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002333
2334 switch (opCode) {
2335 case OP_MOVE_16:
2336 case OP_MOVE_OBJECT_16:
2337 case OP_MOVE_FROM16:
Ben Chenge9695e52009-06-16 16:11:47 -07002338 case OP_MOVE_OBJECT_FROM16: {
Bill Buzbeec6f10662010-02-09 11:16:15 -08002339 storeValue(cUnit, dvmCompilerGetDest(cUnit, mir, 0),
2340 dvmCompilerGetSrc(cUnit, mir, 0));
Ben Chengba4fc8b2009-06-01 13:00:29 -07002341 break;
Ben Chenge9695e52009-06-16 16:11:47 -07002342 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07002343 case OP_MOVE_WIDE_16:
Ben Chenge9695e52009-06-16 16:11:47 -07002344 case OP_MOVE_WIDE_FROM16: {
Bill Buzbeec6f10662010-02-09 11:16:15 -08002345 storeValueWide(cUnit, dvmCompilerGetDestWide(cUnit, mir, 0, 1),
2346 dvmCompilerGetSrcWide(cUnit, mir, 0, 1));
Ben Chengba4fc8b2009-06-01 13:00:29 -07002347 break;
Ben Chenge9695e52009-06-16 16:11:47 -07002348 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07002349 default:
2350 return true;
2351 }
2352 return false;
2353}
2354
2355static bool handleFmt23x(CompilationUnit *cUnit, MIR *mir)
2356{
2357 OpCode opCode = mir->dalvikInsn.opCode;
Bill Buzbee1465db52009-09-23 17:17:35 -07002358 RegLocation rlSrc1;
2359 RegLocation rlSrc2;
2360 RegLocation rlDest;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002361
2362 if ( (opCode >= OP_ADD_INT) && (opCode <= OP_REM_DOUBLE)) {
Ben Cheng5d90c202009-11-22 23:31:11 -08002363 return genArithOp( cUnit, mir );
Ben Chengba4fc8b2009-06-01 13:00:29 -07002364 }
2365
Bill Buzbee1465db52009-09-23 17:17:35 -07002366 /* APUTs have 3 sources and no targets */
2367 if (mir->ssaRep->numDefs == 0) {
2368 if (mir->ssaRep->numUses == 3) {
Bill Buzbeec6f10662010-02-09 11:16:15 -08002369 rlDest = dvmCompilerGetSrc(cUnit, mir, 0);
2370 rlSrc1 = dvmCompilerGetSrc(cUnit, mir, 1);
2371 rlSrc2 = dvmCompilerGetSrc(cUnit, mir, 2);
Bill Buzbee1465db52009-09-23 17:17:35 -07002372 } else {
2373 assert(mir->ssaRep->numUses == 4);
Bill Buzbeec6f10662010-02-09 11:16:15 -08002374 rlDest = dvmCompilerGetSrcWide(cUnit, mir, 0, 1);
2375 rlSrc1 = dvmCompilerGetSrc(cUnit, mir, 2);
2376 rlSrc2 = dvmCompilerGetSrc(cUnit, mir, 3);
Bill Buzbee1465db52009-09-23 17:17:35 -07002377 }
2378 } else {
2379 /* Two sources and 1 dest. Deduce the operand sizes */
2380 if (mir->ssaRep->numUses == 4) {
Bill Buzbeec6f10662010-02-09 11:16:15 -08002381 rlSrc1 = dvmCompilerGetSrcWide(cUnit, mir, 0, 1);
2382 rlSrc2 = dvmCompilerGetSrcWide(cUnit, mir, 2, 3);
Bill Buzbee1465db52009-09-23 17:17:35 -07002383 } else {
2384 assert(mir->ssaRep->numUses == 2);
Bill Buzbeec6f10662010-02-09 11:16:15 -08002385 rlSrc1 = dvmCompilerGetSrc(cUnit, mir, 0);
2386 rlSrc2 = dvmCompilerGetSrc(cUnit, mir, 1);
Bill Buzbee1465db52009-09-23 17:17:35 -07002387 }
2388 if (mir->ssaRep->numDefs == 2) {
Bill Buzbeec6f10662010-02-09 11:16:15 -08002389 rlDest = dvmCompilerGetDestWide(cUnit, mir, 0, 1);
Bill Buzbee1465db52009-09-23 17:17:35 -07002390 } else {
2391 assert(mir->ssaRep->numDefs == 1);
Bill Buzbeec6f10662010-02-09 11:16:15 -08002392 rlDest = dvmCompilerGetDest(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07002393 }
2394 }
2395
2396
Ben Chengba4fc8b2009-06-01 13:00:29 -07002397 switch (opCode) {
Bill Buzbeed45ba372009-06-15 17:00:57 -07002398 case OP_CMPL_FLOAT:
2399 case OP_CMPG_FLOAT:
2400 case OP_CMPL_DOUBLE:
2401 case OP_CMPG_DOUBLE:
Ben Cheng5d90c202009-11-22 23:31:11 -08002402 return genCmpFP(cUnit, mir, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002403 case OP_CMP_LONG:
Bill Buzbee1465db52009-09-23 17:17:35 -07002404 genCmpLong(cUnit, mir, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002405 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002406 case OP_AGET_WIDE:
Bill Buzbee1465db52009-09-23 17:17:35 -07002407 genArrayGet(cUnit, mir, kLong, rlSrc1, rlSrc2, rlDest, 3);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002408 break;
2409 case OP_AGET:
2410 case OP_AGET_OBJECT:
Bill Buzbee1465db52009-09-23 17:17:35 -07002411 genArrayGet(cUnit, mir, kWord, rlSrc1, rlSrc2, rlDest, 2);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002412 break;
2413 case OP_AGET_BOOLEAN:
Bill Buzbee1465db52009-09-23 17:17:35 -07002414 genArrayGet(cUnit, mir, kUnsignedByte, rlSrc1, rlSrc2, rlDest, 0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002415 break;
2416 case OP_AGET_BYTE:
Bill Buzbee1465db52009-09-23 17:17:35 -07002417 genArrayGet(cUnit, mir, kSignedByte, rlSrc1, rlSrc2, rlDest, 0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002418 break;
2419 case OP_AGET_CHAR:
Bill Buzbee1465db52009-09-23 17:17:35 -07002420 genArrayGet(cUnit, mir, kUnsignedHalf, rlSrc1, rlSrc2, rlDest, 1);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002421 break;
2422 case OP_AGET_SHORT:
Bill Buzbee1465db52009-09-23 17:17:35 -07002423 genArrayGet(cUnit, mir, kSignedHalf, rlSrc1, rlSrc2, rlDest, 1);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002424 break;
2425 case OP_APUT_WIDE:
Bill Buzbee1465db52009-09-23 17:17:35 -07002426 genArrayPut(cUnit, mir, kLong, rlSrc1, rlSrc2, rlDest, 3);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002427 break;
2428 case OP_APUT:
Bill Buzbee1465db52009-09-23 17:17:35 -07002429 genArrayPut(cUnit, mir, kWord, rlSrc1, rlSrc2, rlDest, 2);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002430 break;
Bill Buzbeebe6534f2010-03-12 16:01:35 -08002431 case OP_APUT_OBJECT:
2432 genArrayObjectPut(cUnit, mir, rlSrc1, rlSrc2, rlDest, 2);
2433 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002434 case OP_APUT_SHORT:
2435 case OP_APUT_CHAR:
Bill Buzbee1465db52009-09-23 17:17:35 -07002436 genArrayPut(cUnit, mir, kUnsignedHalf, rlSrc1, rlSrc2, rlDest, 1);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002437 break;
2438 case OP_APUT_BYTE:
2439 case OP_APUT_BOOLEAN:
Bill Buzbee1465db52009-09-23 17:17:35 -07002440 genArrayPut(cUnit, mir, kUnsignedByte, rlSrc1, rlSrc2, rlDest, 0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002441 break;
2442 default:
2443 return true;
2444 }
2445 return false;
2446}
2447
Ben Cheng6c10a972009-10-29 14:39:18 -07002448/*
2449 * Find the matching case.
2450 *
2451 * return values:
2452 * r0 (low 32-bit): pc of the chaining cell corresponding to the resolved case,
2453 * including default which is placed at MIN(size, MAX_CHAINED_SWITCH_CASES).
2454 * r1 (high 32-bit): the branch offset of the matching case (only for indexes
2455 * above MAX_CHAINED_SWITCH_CASES).
2456 *
2457 * Instructions around the call are:
2458 *
2459 * mov r2, pc
2460 * blx &findPackedSwitchIndex
2461 * mov pc, r0
2462 * .align4
2463 * chaining cell for case 0 [8 bytes]
2464 * chaining cell for case 1 [8 bytes]
2465 * :
2466 * chaining cell for case MIN(size, MAX_CHAINED_SWITCH_CASES)-1 [8 bytes]
2467 * chaining cell for case default [8 bytes]
2468 * noChain exit
2469 */
Ben Chengbd1326d2010-04-02 15:04:53 -07002470static s8 findPackedSwitchIndex(const u2* switchData, int testVal, int pc)
Ben Cheng6c10a972009-10-29 14:39:18 -07002471{
2472 int size;
2473 int firstKey;
2474 const int *entries;
2475 int index;
2476 int jumpIndex;
2477 int caseDPCOffset = 0;
2478 /* In Thumb mode pc is 4 ahead of the "mov r2, pc" instruction */
2479 int chainingPC = (pc + 4) & ~3;
2480
2481 /*
2482 * Packed switch data format:
2483 * ushort ident = 0x0100 magic value
2484 * ushort size number of entries in the table
2485 * int first_key first (and lowest) switch case value
2486 * int targets[size] branch targets, relative to switch opcode
2487 *
2488 * Total size is (4+size*2) 16-bit code units.
2489 */
2490 size = switchData[1];
2491 assert(size > 0);
2492
2493 firstKey = switchData[2];
2494 firstKey |= switchData[3] << 16;
2495
2496
2497 /* The entries are guaranteed to be aligned on a 32-bit boundary;
2498 * we can treat them as a native int array.
2499 */
2500 entries = (const int*) &switchData[4];
2501 assert(((u4)entries & 0x3) == 0);
2502
2503 index = testVal - firstKey;
2504
2505 /* Jump to the default cell */
2506 if (index < 0 || index >= size) {
2507 jumpIndex = MIN(size, MAX_CHAINED_SWITCH_CASES);
2508 /* Jump to the non-chaining exit point */
2509 } else if (index >= MAX_CHAINED_SWITCH_CASES) {
2510 jumpIndex = MAX_CHAINED_SWITCH_CASES + 1;
2511 caseDPCOffset = entries[index];
2512 /* Jump to the inline chaining cell */
2513 } else {
2514 jumpIndex = index;
2515 }
2516
2517 chainingPC += jumpIndex * 8;
2518 return (((s8) caseDPCOffset) << 32) | (u8) chainingPC;
2519}
2520
2521/* See comments for findPackedSwitchIndex */
Ben Chengbd1326d2010-04-02 15:04:53 -07002522static s8 findSparseSwitchIndex(const u2* switchData, int testVal, int pc)
Ben Cheng6c10a972009-10-29 14:39:18 -07002523{
2524 int size;
2525 const int *keys;
2526 const int *entries;
2527 int chainingPC = (pc + 4) & ~3;
2528 int i;
2529
2530 /*
2531 * Sparse switch data format:
2532 * ushort ident = 0x0200 magic value
2533 * ushort size number of entries in the table; > 0
2534 * int keys[size] keys, sorted low-to-high; 32-bit aligned
2535 * int targets[size] branch targets, relative to switch opcode
2536 *
2537 * Total size is (2+size*4) 16-bit code units.
2538 */
2539
2540 size = switchData[1];
2541 assert(size > 0);
2542
2543 /* The keys are guaranteed to be aligned on a 32-bit boundary;
2544 * we can treat them as a native int array.
2545 */
2546 keys = (const int*) &switchData[2];
2547 assert(((u4)keys & 0x3) == 0);
2548
2549 /* The entries are guaranteed to be aligned on a 32-bit boundary;
2550 * we can treat them as a native int array.
2551 */
2552 entries = keys + size;
2553 assert(((u4)entries & 0x3) == 0);
2554
2555 /*
2556 * Run through the list of keys, which are guaranteed to
2557 * be sorted low-to-high.
2558 *
2559 * Most tables have 3-4 entries. Few have more than 10. A binary
2560 * search here is probably not useful.
2561 */
2562 for (i = 0; i < size; i++) {
2563 int k = keys[i];
2564 if (k == testVal) {
2565 /* MAX_CHAINED_SWITCH_CASES + 1 is the start of the overflow case */
2566 int jumpIndex = (i < MAX_CHAINED_SWITCH_CASES) ?
2567 i : MAX_CHAINED_SWITCH_CASES + 1;
2568 chainingPC += jumpIndex * 8;
2569 return (((s8) entries[i]) << 32) | (u8) chainingPC;
2570 } else if (k > testVal) {
2571 break;
2572 }
2573 }
2574 return chainingPC + MIN(size, MAX_CHAINED_SWITCH_CASES) * 8;
2575}
2576
Ben Chengba4fc8b2009-06-01 13:00:29 -07002577static bool handleFmt31t(CompilationUnit *cUnit, MIR *mir)
2578{
2579 OpCode dalvikOpCode = mir->dalvikInsn.opCode;
2580 switch (dalvikOpCode) {
2581 case OP_FILL_ARRAY_DATA: {
Bill Buzbeec6f10662010-02-09 11:16:15 -08002582 RegLocation rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07002583 // Making a call - use explicit registers
Bill Buzbeec6f10662010-02-09 11:16:15 -08002584 dvmCompilerFlushAllRegs(cUnit); /* Everything to home location */
Bill Buzbee1465db52009-09-23 17:17:35 -07002585 genExportPC(cUnit, mir);
2586 loadValueDirectFixed(cUnit, rlSrc, r0);
Ben Chengbd1326d2010-04-02 15:04:53 -07002587 LOAD_FUNC_ADDR(cUnit, r2, (int)dvmInterpHandleFillArrayData);
Ben Cheng6c10a972009-10-29 14:39:18 -07002588 loadConstant(cUnit, r1,
2589 (int) (cUnit->method->insns + mir->offset + mir->dalvikInsn.vB));
Bill Buzbee1465db52009-09-23 17:17:35 -07002590 opReg(cUnit, kOpBlx, r2);
Elliott Hughes6a555132010-02-25 15:41:42 -08002591 dvmCompilerClobberCallRegs(cUnit);
Bill Buzbeefd023aa2009-11-02 09:23:49 -08002592 /* generate a branch over if successful */
2593 opRegImm(cUnit, kOpCmp, r0, 0); /* NULL? */
2594 ArmLIR *branchOver = opCondBranch(cUnit, kArmCondNe);
2595 loadConstant(cUnit, r0,
2596 (int) (cUnit->method->insns + mir->offset));
2597 genDispatchToHandler(cUnit, TEMPLATE_THROW_EXCEPTION_COMMON);
2598 ArmLIR *target = newLIR0(cUnit, kArmPseudoTargetLabel);
2599 target->defMask = ENCODE_ALL;
2600 branchOver->generic.target = (LIR *) target;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002601 break;
2602 }
2603 /*
Ben Cheng6c10a972009-10-29 14:39:18 -07002604 * Compute the goto target of up to
2605 * MIN(switchSize, MAX_CHAINED_SWITCH_CASES) + 1 chaining cells.
2606 * See the comment before findPackedSwitchIndex for the code layout.
Ben Chengba4fc8b2009-06-01 13:00:29 -07002607 */
2608 case OP_PACKED_SWITCH:
2609 case OP_SPARSE_SWITCH: {
Bill Buzbeec6f10662010-02-09 11:16:15 -08002610 RegLocation rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
2611 dvmCompilerFlushAllRegs(cUnit); /* Everything to home location */
Bill Buzbee1465db52009-09-23 17:17:35 -07002612 loadValueDirectFixed(cUnit, rlSrc, r1);
Bill Buzbeec6f10662010-02-09 11:16:15 -08002613 dvmCompilerLockAllTemps(cUnit);
Ben Cheng6c10a972009-10-29 14:39:18 -07002614 const u2 *switchData =
2615 cUnit->method->insns + mir->offset + mir->dalvikInsn.vB;
2616 u2 size = switchData[1];
2617
Ben Chengba4fc8b2009-06-01 13:00:29 -07002618 if (dalvikOpCode == OP_PACKED_SWITCH) {
Ben Chengbd1326d2010-04-02 15:04:53 -07002619 LOAD_FUNC_ADDR(cUnit, r4PC, (int)findPackedSwitchIndex);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002620 } else {
Ben Chengbd1326d2010-04-02 15:04:53 -07002621 LOAD_FUNC_ADDR(cUnit, r4PC, (int)findSparseSwitchIndex);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002622 }
Ben Cheng6c10a972009-10-29 14:39:18 -07002623 /* r0 <- Addr of the switch data */
2624 loadConstant(cUnit, r0,
2625 (int) (cUnit->method->insns + mir->offset + mir->dalvikInsn.vB));
2626 /* r2 <- pc of the instruction following the blx */
2627 opRegReg(cUnit, kOpMov, r2, rpc);
Bill Buzbee1465db52009-09-23 17:17:35 -07002628 opReg(cUnit, kOpBlx, r4PC);
Elliott Hughes6a555132010-02-25 15:41:42 -08002629 dvmCompilerClobberCallRegs(cUnit);
Ben Cheng6c10a972009-10-29 14:39:18 -07002630 /* pc <- computed goto target */
2631 opRegReg(cUnit, kOpMov, rpc, r0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002632 break;
2633 }
2634 default:
2635 return true;
2636 }
2637 return false;
2638}
2639
2640static bool handleFmt35c_3rc(CompilationUnit *cUnit, MIR *mir, BasicBlock *bb,
Bill Buzbee89efc3d2009-07-28 11:22:22 -07002641 ArmLIR *labelList)
Ben Chengba4fc8b2009-06-01 13:00:29 -07002642{
Bill Buzbee9bc3df32009-07-30 10:52:29 -07002643 ArmLIR *retChainingCell = NULL;
Bill Buzbee89efc3d2009-07-28 11:22:22 -07002644 ArmLIR *pcrLabel = NULL;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002645
Bill Buzbeef4ce16f2009-07-28 13:28:25 -07002646 if (bb->fallThrough != NULL)
2647 retChainingCell = &labelList[bb->fallThrough->id];
2648
Ben Chengba4fc8b2009-06-01 13:00:29 -07002649 DecodedInstruction *dInsn = &mir->dalvikInsn;
2650 switch (mir->dalvikInsn.opCode) {
2651 /*
2652 * calleeMethod = this->clazz->vtable[
2653 * method->clazz->pDvmDex->pResMethods[BBBB]->methodIndex
2654 * ]
2655 */
2656 case OP_INVOKE_VIRTUAL:
2657 case OP_INVOKE_VIRTUAL_RANGE: {
Bill Buzbee89efc3d2009-07-28 11:22:22 -07002658 ArmLIR *predChainingCell = &labelList[bb->taken->id];
Ben Chengba4fc8b2009-06-01 13:00:29 -07002659 int methodIndex =
2660 cUnit->method->clazz->pDvmDex->pResMethods[dInsn->vB]->
2661 methodIndex;
2662
2663 if (mir->dalvikInsn.opCode == OP_INVOKE_VIRTUAL)
2664 genProcessArgsNoRange(cUnit, mir, dInsn, &pcrLabel);
2665 else
2666 genProcessArgsRange(cUnit, mir, dInsn, &pcrLabel);
2667
Ben Cheng38329f52009-07-07 14:19:20 -07002668 genInvokeVirtualCommon(cUnit, mir, methodIndex,
2669 retChainingCell,
2670 predChainingCell,
2671 pcrLabel);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002672 break;
2673 }
2674 /*
2675 * calleeMethod = method->clazz->super->vtable[method->clazz->pDvmDex
2676 * ->pResMethods[BBBB]->methodIndex]
2677 */
Ben Chengba4fc8b2009-06-01 13:00:29 -07002678 case OP_INVOKE_SUPER:
2679 case OP_INVOKE_SUPER_RANGE: {
2680 int mIndex = cUnit->method->clazz->pDvmDex->
2681 pResMethods[dInsn->vB]->methodIndex;
2682 const Method *calleeMethod =
2683 cUnit->method->clazz->super->vtable[mIndex];
2684
2685 if (mir->dalvikInsn.opCode == OP_INVOKE_SUPER)
2686 genProcessArgsNoRange(cUnit, mir, dInsn, &pcrLabel);
2687 else
2688 genProcessArgsRange(cUnit, mir, dInsn, &pcrLabel);
2689
2690 /* r0 = calleeMethod */
2691 loadConstant(cUnit, r0, (int) calleeMethod);
2692
Ben Cheng38329f52009-07-07 14:19:20 -07002693 genInvokeSingletonCommon(cUnit, mir, bb, labelList, pcrLabel,
2694 calleeMethod);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002695 break;
2696 }
2697 /* calleeMethod = method->clazz->pDvmDex->pResMethods[BBBB] */
2698 case OP_INVOKE_DIRECT:
2699 case OP_INVOKE_DIRECT_RANGE: {
2700 const Method *calleeMethod =
2701 cUnit->method->clazz->pDvmDex->pResMethods[dInsn->vB];
2702
2703 if (mir->dalvikInsn.opCode == OP_INVOKE_DIRECT)
2704 genProcessArgsNoRange(cUnit, mir, dInsn, &pcrLabel);
2705 else
2706 genProcessArgsRange(cUnit, mir, dInsn, &pcrLabel);
2707
2708 /* r0 = calleeMethod */
2709 loadConstant(cUnit, r0, (int) calleeMethod);
2710
Ben Cheng38329f52009-07-07 14:19:20 -07002711 genInvokeSingletonCommon(cUnit, mir, bb, labelList, pcrLabel,
2712 calleeMethod);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002713 break;
2714 }
2715 /* calleeMethod = method->clazz->pDvmDex->pResMethods[BBBB] */
2716 case OP_INVOKE_STATIC:
2717 case OP_INVOKE_STATIC_RANGE: {
2718 const Method *calleeMethod =
2719 cUnit->method->clazz->pDvmDex->pResMethods[dInsn->vB];
2720
2721 if (mir->dalvikInsn.opCode == OP_INVOKE_STATIC)
2722 genProcessArgsNoRange(cUnit, mir, dInsn,
2723 NULL /* no null check */);
2724 else
2725 genProcessArgsRange(cUnit, mir, dInsn,
2726 NULL /* no null check */);
2727
2728 /* r0 = calleeMethod */
2729 loadConstant(cUnit, r0, (int) calleeMethod);
2730
Ben Cheng38329f52009-07-07 14:19:20 -07002731 genInvokeSingletonCommon(cUnit, mir, bb, labelList, pcrLabel,
2732 calleeMethod);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002733 break;
2734 }
Ben Cheng09e50c92010-05-02 10:45:32 -07002735 /*
Ben Chengba4fc8b2009-06-01 13:00:29 -07002736 * calleeMethod = dvmFindInterfaceMethodInCache(this->clazz,
2737 * BBBB, method, method->clazz->pDvmDex)
Ben Cheng38329f52009-07-07 14:19:20 -07002738 *
Ben Cheng09e50c92010-05-02 10:45:32 -07002739 * The following is an example of generated code for
2740 * "invoke-interface v0"
Ben Cheng38329f52009-07-07 14:19:20 -07002741 *
Ben Cheng09e50c92010-05-02 10:45:32 -07002742 * -------- dalvik offset: 0x0008 @ invoke-interface v0
2743 * 0x47357e36 : ldr r0, [r5, #0] --+
2744 * 0x47357e38 : sub r7,r5,#24 |
2745 * 0x47357e3c : cmp r0, #0 | genProcessArgsNoRange
2746 * 0x47357e3e : beq 0x47357e82 |
2747 * 0x47357e40 : stmia r7, <r0> --+
2748 * 0x47357e42 : ldr r4, [pc, #120] --> r4 <- dalvikPC of this invoke
2749 * 0x47357e44 : add r1, pc, #64 --> r1 <- &retChainingCell
2750 * 0x47357e46 : add r2, pc, #72 --> r2 <- &predictedChainingCell
2751 * 0x47357e48 : blx_1 0x47348190 --+ TEMPLATE_INVOKE_METHOD_
2752 * 0x47357e4a : blx_2 see above --+ PREDICTED_CHAIN
2753 * 0x47357e4c : b 0x47357e90 --> off to the predicted chain
2754 * 0x47357e4e : b 0x47357e82 --> punt to the interpreter
2755 * 0x47357e50 : mov r8, r1 --+
2756 * 0x47357e52 : mov r9, r2 |
2757 * 0x47357e54 : ldr r2, [pc, #96] |
2758 * 0x47357e56 : mov r10, r3 |
2759 * 0x47357e58 : movs r0, r3 | dvmFindInterfaceMethodInCache
2760 * 0x47357e5a : ldr r3, [pc, #88] |
2761 * 0x47357e5c : ldr r7, [pc, #80] |
2762 * 0x47357e5e : mov r1, #1452 |
2763 * 0x47357e62 : blx r7 --+
2764 * 0x47357e64 : cmp r0, #0 --> calleeMethod == NULL?
2765 * 0x47357e66 : bne 0x47357e6e --> branch over the throw if !r0
2766 * 0x47357e68 : ldr r0, [pc, #80] --> load Dalvik PC of the invoke
2767 * 0x47357e6a : blx_1 0x47348494 --+ TEMPLATE_THROW_EXCEPTION_
2768 * 0x47357e6c : blx_2 see above --+ COMMON
2769 * 0x47357e6e : mov r1, r8 --> r1 <- &retChainingCell
2770 * 0x47357e70 : cmp r1, #0 --> compare against 0
2771 * 0x47357e72 : bgt 0x47357e7c --> >=0? don't rechain
2772 * 0x47357e74 : ldr r7, [r6, #108] --+
2773 * 0x47357e76 : mov r2, r9 | dvmJitToPatchPredictedChain
2774 * 0x47357e78 : mov r3, r10 |
2775 * 0x47357e7a : blx r7 --+
2776 * 0x47357e7c : add r1, pc, #8 --> r1 <- &retChainingCell
2777 * 0x47357e7e : blx_1 0x4734809c --+ TEMPLATE_INVOKE_METHOD_NO_OPT
2778 * 0x47357e80 : blx_2 see above --+
2779 * -------- reconstruct dalvik PC : 0x425719dc @ +0x0008
2780 * 0x47357e82 : ldr r0, [pc, #56]
Ben Cheng38329f52009-07-07 14:19:20 -07002781 * Exception_Handling:
Ben Cheng09e50c92010-05-02 10:45:32 -07002782 * 0x47357e84 : ldr r1, [r6, #92]
2783 * 0x47357e86 : blx r1
2784 * 0x47357e88 : .align4
2785 * -------- chaining cell (hot): 0x000b
2786 * 0x47357e88 : ldr r0, [r6, #104]
2787 * 0x47357e8a : blx r0
2788 * 0x47357e8c : data 0x19e2(6626)
2789 * 0x47357e8e : data 0x4257(16983)
2790 * 0x47357e90 : .align4
Ben Cheng38329f52009-07-07 14:19:20 -07002791 * -------- chaining cell (predicted)
Ben Cheng09e50c92010-05-02 10:45:32 -07002792 * 0x47357e90 : data 0xe7fe(59390) --> will be patched into bx
2793 * 0x47357e92 : data 0x0000(0)
2794 * 0x47357e94 : data 0x0000(0) --> class
2795 * 0x47357e96 : data 0x0000(0)
2796 * 0x47357e98 : data 0x0000(0) --> method
2797 * 0x47357e9a : data 0x0000(0)
2798 * 0x47357e9c : data 0x0000(0) --> rechain count
2799 * 0x47357e9e : data 0x0000(0)
2800 * -------- end of chaining cells (0x006c)
2801 * 0x47357eb0 : .word (0xad03e369)
2802 * 0x47357eb4 : .word (0x28a90)
2803 * 0x47357eb8 : .word (0x41a63394)
2804 * 0x47357ebc : .word (0x425719dc)
Ben Chengba4fc8b2009-06-01 13:00:29 -07002805 */
2806 case OP_INVOKE_INTERFACE:
2807 case OP_INVOKE_INTERFACE_RANGE: {
Bill Buzbee89efc3d2009-07-28 11:22:22 -07002808 ArmLIR *predChainingCell = &labelList[bb->taken->id];
Ben Chengba4fc8b2009-06-01 13:00:29 -07002809 int methodIndex = dInsn->vB;
2810
Bill Buzbee1465db52009-09-23 17:17:35 -07002811 /* Ensure that nothing is both live and dirty */
Bill Buzbeec6f10662010-02-09 11:16:15 -08002812 dvmCompilerFlushAllRegs(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07002813
Ben Chengba4fc8b2009-06-01 13:00:29 -07002814 if (mir->dalvikInsn.opCode == OP_INVOKE_INTERFACE)
2815 genProcessArgsNoRange(cUnit, mir, dInsn, &pcrLabel);
2816 else
2817 genProcessArgsRange(cUnit, mir, dInsn, &pcrLabel);
2818
Ben Cheng38329f52009-07-07 14:19:20 -07002819 /* "this" is already left in r0 by genProcessArgs* */
2820
2821 /* r4PC = dalvikCallsite */
2822 loadConstant(cUnit, r4PC,
2823 (int) (cUnit->method->insns + mir->offset));
2824
2825 /* r1 = &retChainingCell */
Bill Buzbee270c1d62009-08-13 16:58:07 -07002826 ArmLIR *addrRetChain =
Bill Buzbee1465db52009-09-23 17:17:35 -07002827 opRegRegImm(cUnit, kOpAdd, r1, rpc, 0);
Ben Cheng38329f52009-07-07 14:19:20 -07002828 addrRetChain->generic.target = (LIR *) retChainingCell;
2829
2830 /* r2 = &predictedChainingCell */
Bill Buzbee89efc3d2009-07-28 11:22:22 -07002831 ArmLIR *predictedChainingCell =
Bill Buzbee1465db52009-09-23 17:17:35 -07002832 opRegRegImm(cUnit, kOpAdd, r2, rpc, 0);
Ben Cheng38329f52009-07-07 14:19:20 -07002833 predictedChainingCell->generic.target = (LIR *) predChainingCell;
2834
2835 genDispatchToHandler(cUnit, TEMPLATE_INVOKE_METHOD_PREDICTED_CHAIN);
2836
2837 /* return through lr - jump to the chaining cell */
2838 genUnconditionalBranch(cUnit, predChainingCell);
2839
2840 /*
2841 * null-check on "this" may have been eliminated, but we still need
2842 * a PC-reconstruction label for stack overflow bailout.
2843 */
2844 if (pcrLabel == NULL) {
2845 int dPC = (int) (cUnit->method->insns + mir->offset);
Bill Buzbee89efc3d2009-07-28 11:22:22 -07002846 pcrLabel = dvmCompilerNew(sizeof(ArmLIR), true);
Ben Chenga4973592010-03-31 11:59:18 -07002847 pcrLabel->opCode = kArmPseudoPCReconstructionCell;
Ben Cheng38329f52009-07-07 14:19:20 -07002848 pcrLabel->operands[0] = dPC;
2849 pcrLabel->operands[1] = mir->offset;
2850 /* Insert the place holder to the growable list */
2851 dvmInsertGrowableList(&cUnit->pcReconstructionList, pcrLabel);
2852 }
2853
2854 /* return through lr+2 - punt to the interpreter */
2855 genUnconditionalBranch(cUnit, pcrLabel);
2856
2857 /*
2858 * return through lr+4 - fully resolve the callee method.
2859 * r1 <- count
2860 * r2 <- &predictedChainCell
2861 * r3 <- this->class
2862 * r4 <- dPC
2863 * r7 <- this->class->vtable
2864 */
2865
2866 /* Save count, &predictedChainCell, and class to high regs first */
Bill Buzbee1465db52009-09-23 17:17:35 -07002867 genRegCopy(cUnit, r8, r1);
2868 genRegCopy(cUnit, r9, r2);
2869 genRegCopy(cUnit, r10, r3);
Ben Cheng38329f52009-07-07 14:19:20 -07002870
Ben Chengba4fc8b2009-06-01 13:00:29 -07002871 /* r0 now contains this->clazz */
Bill Buzbee1465db52009-09-23 17:17:35 -07002872 genRegCopy(cUnit, r0, r3);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002873
2874 /* r1 = BBBB */
2875 loadConstant(cUnit, r1, dInsn->vB);
2876
2877 /* r2 = method (caller) */
2878 loadConstant(cUnit, r2, (int) cUnit->method);
2879
2880 /* r3 = pDvmDex */
2881 loadConstant(cUnit, r3, (int) cUnit->method->clazz->pDvmDex);
2882
Ben Chengbd1326d2010-04-02 15:04:53 -07002883 LOAD_FUNC_ADDR(cUnit, r7,
2884 (intptr_t) dvmFindInterfaceMethodInCache);
Bill Buzbee1465db52009-09-23 17:17:35 -07002885 opReg(cUnit, kOpBlx, r7);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002886 /* r0 = calleeMethod (returned from dvmFindInterfaceMethodInCache */
2887
Ben Cheng09e50c92010-05-02 10:45:32 -07002888 dvmCompilerClobberCallRegs(cUnit);
2889 /* generate a branch over if the interface method is resolved */
2890 opRegImm(cUnit, kOpCmp, r0, 0); /* NULL? */
2891 ArmLIR *branchOver = opCondBranch(cUnit, kArmCondNe);
2892 /*
2893 * calleeMethod == NULL -> throw
2894 */
2895 loadConstant(cUnit, r0,
2896 (int) (cUnit->method->insns + mir->offset));
2897 genDispatchToHandler(cUnit, TEMPLATE_THROW_EXCEPTION_COMMON);
2898 /* noreturn */
2899
2900 ArmLIR *target = newLIR0(cUnit, kArmPseudoTargetLabel);
2901 target->defMask = ENCODE_ALL;
2902 branchOver->generic.target = (LIR *) target;
2903
Bill Buzbee1465db52009-09-23 17:17:35 -07002904 genRegCopy(cUnit, r1, r8);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002905
Ben Cheng38329f52009-07-07 14:19:20 -07002906 /* Check if rechain limit is reached */
Bill Buzbee1465db52009-09-23 17:17:35 -07002907 opRegImm(cUnit, kOpCmp, r1, 0);
Ben Cheng38329f52009-07-07 14:19:20 -07002908
Bill Buzbee1465db52009-09-23 17:17:35 -07002909 ArmLIR *bypassRechaining = opCondBranch(cUnit, kArmCondGt);
Ben Cheng38329f52009-07-07 14:19:20 -07002910
Bill Buzbee270c1d62009-08-13 16:58:07 -07002911 loadWordDisp(cUnit, rGLUE, offsetof(InterpState,
2912 jitToInterpEntries.dvmJitToPatchPredictedChain), r7);
Ben Cheng38329f52009-07-07 14:19:20 -07002913
Bill Buzbee1465db52009-09-23 17:17:35 -07002914 genRegCopy(cUnit, r2, r9);
2915 genRegCopy(cUnit, r3, r10);
Ben Cheng38329f52009-07-07 14:19:20 -07002916
2917 /*
2918 * r0 = calleeMethod
2919 * r2 = &predictedChainingCell
2920 * r3 = class
2921 *
2922 * &returnChainingCell has been loaded into r1 but is not needed
2923 * when patching the chaining cell and will be clobbered upon
2924 * returning so it will be reconstructed again.
2925 */
Bill Buzbee1465db52009-09-23 17:17:35 -07002926 opReg(cUnit, kOpBlx, r7);
Ben Cheng38329f52009-07-07 14:19:20 -07002927
2928 /* r1 = &retChainingCell */
Bill Buzbee1465db52009-09-23 17:17:35 -07002929 addrRetChain = opRegRegImm(cUnit, kOpAdd, r1, rpc, 0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002930 addrRetChain->generic.target = (LIR *) retChainingCell;
Ben Cheng38329f52009-07-07 14:19:20 -07002931
2932 bypassRechaining->generic.target = (LIR *) addrRetChain;
2933
Ben Chengba4fc8b2009-06-01 13:00:29 -07002934 /*
2935 * r0 = this, r1 = calleeMethod,
2936 * r1 = &ChainingCell,
2937 * r4PC = callsiteDPC,
2938 */
2939 genDispatchToHandler(cUnit, TEMPLATE_INVOKE_METHOD_NO_OPT);
Ben Cheng86717f72010-03-05 15:27:21 -08002940#if defined(JIT_STATS)
2941 gDvmJit.invokePolymorphic++;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002942#endif
2943 /* Handle exceptions using the interpreter */
2944 genTrap(cUnit, mir->offset, pcrLabel);
2945 break;
2946 }
2947 /* NOP */
2948 case OP_INVOKE_DIRECT_EMPTY: {
2949 return false;
2950 }
2951 case OP_FILLED_NEW_ARRAY:
2952 case OP_FILLED_NEW_ARRAY_RANGE: {
2953 /* Just let the interpreter deal with these */
2954 genInterpSingleStep(cUnit, mir);
2955 break;
2956 }
2957 default:
2958 return true;
2959 }
2960 return false;
2961}
2962
2963static bool handleFmt35ms_3rms(CompilationUnit *cUnit, MIR *mir,
Bill Buzbee89efc3d2009-07-28 11:22:22 -07002964 BasicBlock *bb, ArmLIR *labelList)
Ben Chengba4fc8b2009-06-01 13:00:29 -07002965{
Bill Buzbee89efc3d2009-07-28 11:22:22 -07002966 ArmLIR *retChainingCell = &labelList[bb->fallThrough->id];
2967 ArmLIR *predChainingCell = &labelList[bb->taken->id];
2968 ArmLIR *pcrLabel = NULL;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002969
2970 DecodedInstruction *dInsn = &mir->dalvikInsn;
2971 switch (mir->dalvikInsn.opCode) {
2972 /* calleeMethod = this->clazz->vtable[BBBB] */
2973 case OP_INVOKE_VIRTUAL_QUICK_RANGE:
2974 case OP_INVOKE_VIRTUAL_QUICK: {
2975 int methodIndex = dInsn->vB;
2976 if (mir->dalvikInsn.opCode == OP_INVOKE_VIRTUAL_QUICK)
2977 genProcessArgsNoRange(cUnit, mir, dInsn, &pcrLabel);
2978 else
2979 genProcessArgsRange(cUnit, mir, dInsn, &pcrLabel);
2980
Ben Cheng38329f52009-07-07 14:19:20 -07002981 genInvokeVirtualCommon(cUnit, mir, methodIndex,
2982 retChainingCell,
2983 predChainingCell,
2984 pcrLabel);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002985 break;
2986 }
2987 /* calleeMethod = method->clazz->super->vtable[BBBB] */
2988 case OP_INVOKE_SUPER_QUICK:
2989 case OP_INVOKE_SUPER_QUICK_RANGE: {
2990 const Method *calleeMethod =
2991 cUnit->method->clazz->super->vtable[dInsn->vB];
2992
2993 if (mir->dalvikInsn.opCode == OP_INVOKE_SUPER_QUICK)
2994 genProcessArgsNoRange(cUnit, mir, dInsn, &pcrLabel);
2995 else
2996 genProcessArgsRange(cUnit, mir, dInsn, &pcrLabel);
2997
2998 /* r0 = calleeMethod */
2999 loadConstant(cUnit, r0, (int) calleeMethod);
3000
Ben Cheng38329f52009-07-07 14:19:20 -07003001 genInvokeSingletonCommon(cUnit, mir, bb, labelList, pcrLabel,
3002 calleeMethod);
3003 /* Handle exceptions using the interpreter */
3004 genTrap(cUnit, mir->offset, pcrLabel);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003005 break;
3006 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07003007 default:
3008 return true;
3009 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07003010 return false;
3011}
3012
3013/*
Bill Buzbeefd023aa2009-11-02 09:23:49 -08003014 * This operation is complex enough that we'll do it partly inline
3015 * and partly with a handler. NOTE: the handler uses hardcoded
3016 * values for string object offsets and must be revisitied if the
3017 * layout changes.
3018 */
3019static bool genInlinedCompareTo(CompilationUnit *cUnit, MIR *mir)
3020{
3021#if defined(USE_GLOBAL_STRING_DEFS)
3022 return false;
3023#else
3024 ArmLIR *rollback;
Bill Buzbeec6f10662010-02-09 11:16:15 -08003025 RegLocation rlThis = dvmCompilerGetSrc(cUnit, mir, 0);
3026 RegLocation rlComp = dvmCompilerGetSrc(cUnit, mir, 1);
Bill Buzbeefd023aa2009-11-02 09:23:49 -08003027
3028 loadValueDirectFixed(cUnit, rlThis, r0);
3029 loadValueDirectFixed(cUnit, rlComp, r1);
3030 /* Test objects for NULL */
3031 rollback = genNullCheck(cUnit, rlThis.sRegLow, r0, mir->offset, NULL);
3032 genNullCheck(cUnit, rlComp.sRegLow, r1, mir->offset, rollback);
3033 /*
3034 * TUNING: we could check for object pointer equality before invoking
3035 * handler. Unclear whether the gain would be worth the added code size
3036 * expansion.
3037 */
3038 genDispatchToHandler(cUnit, TEMPLATE_STRING_COMPARETO);
Bill Buzbeec6f10662010-02-09 11:16:15 -08003039 storeValue(cUnit, inlinedTarget(cUnit, mir, false),
3040 dvmCompilerGetReturn(cUnit));
Bill Buzbeefd023aa2009-11-02 09:23:49 -08003041 return true;
3042#endif
3043}
3044
Elliott Hughes2bdbcb62010-04-12 14:29:37 -07003045static bool genInlinedFastIndexOf(CompilationUnit *cUnit, MIR *mir)
Bill Buzbeefd023aa2009-11-02 09:23:49 -08003046{
3047#if defined(USE_GLOBAL_STRING_DEFS)
3048 return false;
3049#else
Bill Buzbeec6f10662010-02-09 11:16:15 -08003050 RegLocation rlThis = dvmCompilerGetSrc(cUnit, mir, 0);
3051 RegLocation rlChar = dvmCompilerGetSrc(cUnit, mir, 1);
Bill Buzbeefd023aa2009-11-02 09:23:49 -08003052
3053 loadValueDirectFixed(cUnit, rlThis, r0);
3054 loadValueDirectFixed(cUnit, rlChar, r1);
Elliott Hughes2bdbcb62010-04-12 14:29:37 -07003055 RegLocation rlStart = dvmCompilerGetSrc(cUnit, mir, 2);
3056 loadValueDirectFixed(cUnit, rlStart, r2);
Bill Buzbeefd023aa2009-11-02 09:23:49 -08003057 /* Test objects for NULL */
3058 genNullCheck(cUnit, rlThis.sRegLow, r0, mir->offset, NULL);
3059 genDispatchToHandler(cUnit, TEMPLATE_STRING_INDEXOF);
Bill Buzbeec6f10662010-02-09 11:16:15 -08003060 storeValue(cUnit, inlinedTarget(cUnit, mir, false),
3061 dvmCompilerGetReturn(cUnit));
Bill Buzbeefd023aa2009-11-02 09:23:49 -08003062 return true;
3063#endif
3064}
3065
Elliott Hughesee34f592010-04-05 18:13:52 -07003066// Generates an inlined String.isEmpty or String.length.
3067static bool genInlinedStringIsEmptyOrLength(CompilationUnit *cUnit, MIR *mir,
3068 bool isEmpty)
Bill Buzbee1f748632010-03-02 16:14:41 -08003069{
Elliott Hughesee34f592010-04-05 18:13:52 -07003070 // dst = src.length();
Bill Buzbee1f748632010-03-02 16:14:41 -08003071 RegLocation rlObj = dvmCompilerGetSrc(cUnit, mir, 0);
3072 RegLocation rlDest = inlinedTarget(cUnit, mir, false);
3073 rlObj = loadValue(cUnit, rlObj, kCoreReg);
3074 RegLocation rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
3075 genNullCheck(cUnit, rlObj.sRegLow, rlObj.lowReg, mir->offset, NULL);
3076 loadWordDisp(cUnit, rlObj.lowReg, gDvm.offJavaLangString_count,
3077 rlResult.lowReg);
Elliott Hughesee34f592010-04-05 18:13:52 -07003078 if (isEmpty) {
3079 // dst = (dst == 0);
3080 int tReg = dvmCompilerAllocTemp(cUnit);
3081 opRegReg(cUnit, kOpNeg, tReg, rlResult.lowReg);
3082 opRegRegReg(cUnit, kOpAdc, rlResult.lowReg, rlResult.lowReg, tReg);
3083 }
Bill Buzbee1f748632010-03-02 16:14:41 -08003084 storeValue(cUnit, rlDest, rlResult);
3085 return false;
3086}
3087
Elliott Hughesee34f592010-04-05 18:13:52 -07003088static bool genInlinedStringLength(CompilationUnit *cUnit, MIR *mir)
3089{
3090 return genInlinedStringIsEmptyOrLength(cUnit, mir, false);
3091}
3092
3093static bool genInlinedStringIsEmpty(CompilationUnit *cUnit, MIR *mir)
3094{
3095 return genInlinedStringIsEmptyOrLength(cUnit, mir, true);
3096}
3097
Bill Buzbee1f748632010-03-02 16:14:41 -08003098static bool genInlinedStringCharAt(CompilationUnit *cUnit, MIR *mir)
3099{
3100 int contents = offsetof(ArrayObject, contents);
3101 RegLocation rlObj = dvmCompilerGetSrc(cUnit, mir, 0);
3102 RegLocation rlIdx = dvmCompilerGetSrc(cUnit, mir, 1);
3103 RegLocation rlDest = inlinedTarget(cUnit, mir, false);
3104 RegLocation rlResult;
3105 rlObj = loadValue(cUnit, rlObj, kCoreReg);
3106 rlIdx = loadValue(cUnit, rlIdx, kCoreReg);
3107 int regMax = dvmCompilerAllocTemp(cUnit);
3108 int regOff = dvmCompilerAllocTemp(cUnit);
3109 int regPtr = dvmCompilerAllocTemp(cUnit);
3110 ArmLIR *pcrLabel = genNullCheck(cUnit, rlObj.sRegLow, rlObj.lowReg,
3111 mir->offset, NULL);
3112 loadWordDisp(cUnit, rlObj.lowReg, gDvm.offJavaLangString_count, regMax);
3113 loadWordDisp(cUnit, rlObj.lowReg, gDvm.offJavaLangString_offset, regOff);
3114 loadWordDisp(cUnit, rlObj.lowReg, gDvm.offJavaLangString_value, regPtr);
3115 genBoundsCheck(cUnit, rlIdx.lowReg, regMax, mir->offset, pcrLabel);
3116 dvmCompilerFreeTemp(cUnit, regMax);
3117 opRegImm(cUnit, kOpAdd, regPtr, contents);
3118 opRegReg(cUnit, kOpAdd, regOff, rlIdx.lowReg);
3119 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
3120 loadBaseIndexed(cUnit, regPtr, regOff, rlResult.lowReg, 1, kUnsignedHalf);
3121 storeValue(cUnit, rlDest, rlResult);
3122 return false;
3123}
3124
3125static bool genInlinedAbsInt(CompilationUnit *cUnit, MIR *mir)
3126{
3127 RegLocation rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
3128 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
3129 RegLocation rlDest = inlinedTarget(cUnit, mir, false);;
3130 RegLocation rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
3131 int signReg = dvmCompilerAllocTemp(cUnit);
3132 /*
3133 * abs(x) = y<=x>>31, (x+y)^y.
3134 * Thumb2's IT block also yields 3 instructions, but imposes
3135 * scheduling constraints.
3136 */
3137 opRegRegImm(cUnit, kOpAsr, signReg, rlSrc.lowReg, 31);
3138 opRegRegReg(cUnit, kOpAdd, rlResult.lowReg, rlSrc.lowReg, signReg);
3139 opRegReg(cUnit, kOpXor, rlResult.lowReg, signReg);
3140 storeValue(cUnit, rlDest, rlResult);
3141 return false;
3142}
3143
3144static bool genInlinedAbsLong(CompilationUnit *cUnit, MIR *mir)
3145{
3146 RegLocation rlSrc = dvmCompilerGetSrcWide(cUnit, mir, 0, 1);
3147 RegLocation rlDest = inlinedTargetWide(cUnit, mir, false);
3148 rlSrc = loadValueWide(cUnit, rlSrc, kCoreReg);
3149 RegLocation rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
3150 int signReg = dvmCompilerAllocTemp(cUnit);
3151 /*
3152 * abs(x) = y<=x>>31, (x+y)^y.
3153 * Thumb2 IT block allows slightly shorter sequence,
3154 * but introduces a scheduling barrier. Stick with this
3155 * mechanism for now.
3156 */
3157 opRegRegImm(cUnit, kOpAsr, signReg, rlSrc.highReg, 31);
3158 opRegRegReg(cUnit, kOpAdd, rlResult.lowReg, rlSrc.lowReg, signReg);
3159 opRegRegReg(cUnit, kOpAdc, rlResult.highReg, rlSrc.highReg, signReg);
3160 opRegReg(cUnit, kOpXor, rlResult.lowReg, signReg);
3161 opRegReg(cUnit, kOpXor, rlResult.highReg, signReg);
3162 storeValueWide(cUnit, rlDest, rlResult);
3163 return false;
3164}
Bill Buzbeefd023aa2009-11-02 09:23:49 -08003165
3166/*
Bill Buzbeece46c942009-11-20 15:41:34 -08003167 * NOTE: Handles both range and non-range versions (arguments
3168 * have already been normalized by this point).
Ben Chengba4fc8b2009-06-01 13:00:29 -07003169 */
Bill Buzbeece46c942009-11-20 15:41:34 -08003170static bool handleExecuteInline(CompilationUnit *cUnit, MIR *mir)
Ben Chengba4fc8b2009-06-01 13:00:29 -07003171{
3172 DecodedInstruction *dInsn = &mir->dalvikInsn;
3173 switch( mir->dalvikInsn.opCode) {
Bill Buzbeece46c942009-11-20 15:41:34 -08003174 case OP_EXECUTE_INLINE_RANGE:
Ben Chengba4fc8b2009-06-01 13:00:29 -07003175 case OP_EXECUTE_INLINE: {
3176 unsigned int i;
3177 const InlineOperation* inLineTable = dvmGetInlineOpsTable();
Bill Buzbee50a6bf22009-07-08 13:08:04 -07003178 int offset = offsetof(InterpState, retval);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003179 int operation = dInsn->vB;
Bill Buzbee1465db52009-09-23 17:17:35 -07003180 int tReg1;
3181 int tReg2;
Bill Buzbee50a6bf22009-07-08 13:08:04 -07003182 switch (operation) {
3183 case INLINE_EMPTYINLINEMETHOD:
3184 return false; /* Nop */
3185 case INLINE_STRING_LENGTH:
3186 return genInlinedStringLength(cUnit, mir);
Elliott Hughesee34f592010-04-05 18:13:52 -07003187 case INLINE_STRING_IS_EMPTY:
3188 return genInlinedStringIsEmpty(cUnit, mir);
Bill Buzbee50a6bf22009-07-08 13:08:04 -07003189 case INLINE_MATH_ABS_INT:
3190 return genInlinedAbsInt(cUnit, mir);
3191 case INLINE_MATH_ABS_LONG:
3192 return genInlinedAbsLong(cUnit, mir);
3193 case INLINE_MATH_MIN_INT:
3194 return genInlinedMinMaxInt(cUnit, mir, true);
3195 case INLINE_MATH_MAX_INT:
3196 return genInlinedMinMaxInt(cUnit, mir, false);
3197 case INLINE_STRING_CHARAT:
3198 return genInlinedStringCharAt(cUnit, mir);
3199 case INLINE_MATH_SQRT:
3200 if (genInlineSqrt(cUnit, mir))
Bill Buzbee9727c3d2009-08-01 11:32:36 -07003201 return false;
Bill Buzbee50a6bf22009-07-08 13:08:04 -07003202 else
3203 break; /* Handle with C routine */
Bill Buzbee50a6bf22009-07-08 13:08:04 -07003204 case INLINE_MATH_ABS_FLOAT:
Bill Buzbee1465db52009-09-23 17:17:35 -07003205 if (genInlinedAbsFloat(cUnit, mir))
3206 return false;
3207 else
3208 break;
Bill Buzbee50a6bf22009-07-08 13:08:04 -07003209 case INLINE_MATH_ABS_DOUBLE:
Bill Buzbee1465db52009-09-23 17:17:35 -07003210 if (genInlinedAbsDouble(cUnit, mir))
3211 return false;
3212 else
3213 break;
Bill Buzbee50a6bf22009-07-08 13:08:04 -07003214 case INLINE_STRING_COMPARETO:
Bill Buzbeefd023aa2009-11-02 09:23:49 -08003215 if (genInlinedCompareTo(cUnit, mir))
3216 return false;
3217 else
3218 break;
Elliott Hughes2bdbcb62010-04-12 14:29:37 -07003219 case INLINE_STRING_FASTINDEXOF_II:
3220 if (genInlinedFastIndexOf(cUnit, mir))
Bill Buzbeefd023aa2009-11-02 09:23:49 -08003221 return false;
3222 else
3223 break;
3224 case INLINE_STRING_EQUALS:
3225 case INLINE_MATH_COS:
3226 case INLINE_MATH_SIN:
3227 break; /* Handle with C routine */
Bill Buzbee50a6bf22009-07-08 13:08:04 -07003228 default:
Bill Buzbeefc519dc2010-03-06 23:30:57 -08003229 dvmCompilerAbort(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003230 }
Bill Buzbeec6f10662010-02-09 11:16:15 -08003231 dvmCompilerFlushAllRegs(cUnit); /* Everything to home location */
Elliott Hughes6a555132010-02-25 15:41:42 -08003232 dvmCompilerClobberCallRegs(cUnit);
Bill Buzbeec6f10662010-02-09 11:16:15 -08003233 dvmCompilerClobber(cUnit, r4PC);
3234 dvmCompilerClobber(cUnit, r7);
Bill Buzbee1465db52009-09-23 17:17:35 -07003235 opRegRegImm(cUnit, kOpAdd, r4PC, rGLUE, offset);
3236 opImm(cUnit, kOpPush, (1<<r4PC) | (1<<r7));
Ben Chengbd1326d2010-04-02 15:04:53 -07003237 LOAD_FUNC_ADDR(cUnit, r4PC, (int)inLineTable[operation].func);
Bill Buzbee1465db52009-09-23 17:17:35 -07003238 genExportPC(cUnit, mir);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003239 for (i=0; i < dInsn->vA; i++) {
Bill Buzbeec6f10662010-02-09 11:16:15 -08003240 loadValueDirect(cUnit, dvmCompilerGetSrc(cUnit, mir, i), i);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003241 }
Bill Buzbee1465db52009-09-23 17:17:35 -07003242 opReg(cUnit, kOpBlx, r4PC);
3243 opRegImm(cUnit, kOpAdd, r13, 8);
Bill Buzbeece46c942009-11-20 15:41:34 -08003244 opRegImm(cUnit, kOpCmp, r0, 0); /* NULL? */
3245 ArmLIR *branchOver = opCondBranch(cUnit, kArmCondNe);
3246 loadConstant(cUnit, r0,
3247 (int) (cUnit->method->insns + mir->offset));
3248 genDispatchToHandler(cUnit, TEMPLATE_THROW_EXCEPTION_COMMON);
3249 ArmLIR *target = newLIR0(cUnit, kArmPseudoTargetLabel);
3250 target->defMask = ENCODE_ALL;
3251 branchOver->generic.target = (LIR *) target;
Ben Chengba4fc8b2009-06-01 13:00:29 -07003252 break;
3253 }
3254 default:
3255 return true;
3256 }
3257 return false;
3258}
3259
3260static bool handleFmt51l(CompilationUnit *cUnit, MIR *mir)
3261{
Bill Buzbee1465db52009-09-23 17:17:35 -07003262 //TUNING: We're using core regs here - not optimal when target is a double
Bill Buzbeec6f10662010-02-09 11:16:15 -08003263 RegLocation rlDest = dvmCompilerGetDestWide(cUnit, mir, 0, 1);
3264 RegLocation rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Ben Chengbd1326d2010-04-02 15:04:53 -07003265 loadConstantNoClobber(cUnit, rlResult.lowReg,
3266 mir->dalvikInsn.vB_wide & 0xFFFFFFFFUL);
3267 loadConstantNoClobber(cUnit, rlResult.highReg,
3268 (mir->dalvikInsn.vB_wide>>32) & 0xFFFFFFFFUL);
Bill Buzbee1465db52009-09-23 17:17:35 -07003269 storeValueWide(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003270 return false;
3271}
3272
Ben Chengba4fc8b2009-06-01 13:00:29 -07003273/*
3274 * The following are special processing routines that handle transfer of
3275 * controls between compiled code and the interpreter. Certain VM states like
3276 * Dalvik PC and special-purpose registers are reconstructed here.
3277 */
3278
Ben Cheng1efc9c52009-06-08 18:25:27 -07003279/* Chaining cell for code that may need warmup. */
3280static void handleNormalChainingCell(CompilationUnit *cUnit,
3281 unsigned int offset)
Ben Chengba4fc8b2009-06-01 13:00:29 -07003282{
Ben Cheng11d8f142010-03-24 15:24:19 -07003283 /*
3284 * Use raw instruction constructors to guarantee that the generated
3285 * instructions fit the predefined cell size.
3286 */
3287 newLIR3(cUnit, kThumbLdrRRI5, r0, rGLUE,
3288 offsetof(InterpState,
3289 jitToInterpEntries.dvmJitToInterpNormal) >> 2);
3290 newLIR1(cUnit, kThumbBlxR, r0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003291 addWordData(cUnit, (int) (cUnit->method->insns + offset), true);
3292}
3293
3294/*
Ben Cheng1efc9c52009-06-08 18:25:27 -07003295 * Chaining cell for instructions that immediately following already translated
3296 * code.
Ben Chengba4fc8b2009-06-01 13:00:29 -07003297 */
Ben Cheng1efc9c52009-06-08 18:25:27 -07003298static void handleHotChainingCell(CompilationUnit *cUnit,
3299 unsigned int offset)
Ben Chengba4fc8b2009-06-01 13:00:29 -07003300{
Ben Cheng11d8f142010-03-24 15:24:19 -07003301 /*
3302 * Use raw instruction constructors to guarantee that the generated
3303 * instructions fit the predefined cell size.
3304 */
3305 newLIR3(cUnit, kThumbLdrRRI5, r0, rGLUE,
3306 offsetof(InterpState,
3307 jitToInterpEntries.dvmJitToInterpTraceSelect) >> 2);
3308 newLIR1(cUnit, kThumbBlxR, r0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003309 addWordData(cUnit, (int) (cUnit->method->insns + offset), true);
3310}
3311
Bill Buzbee9c4b7c82009-09-10 10:10:38 -07003312#if defined(WITH_SELF_VERIFICATION) || defined(WITH_JIT_TUNING)
Jeff Hao97319a82009-08-12 16:57:15 -07003313/* Chaining cell for branches that branch back into the same basic block */
3314static void handleBackwardBranchChainingCell(CompilationUnit *cUnit,
3315 unsigned int offset)
3316{
Ben Cheng11d8f142010-03-24 15:24:19 -07003317 /*
3318 * Use raw instruction constructors to guarantee that the generated
3319 * instructions fit the predefined cell size.
3320 */
Bill Buzbee9c4b7c82009-09-10 10:10:38 -07003321#if defined(WITH_SELF_VERIFICATION)
Bill Buzbee1465db52009-09-23 17:17:35 -07003322 newLIR3(cUnit, kThumbLdrRRI5, r0, rGLUE,
Ben Cheng40094c12010-02-24 20:58:44 -08003323 offsetof(InterpState,
3324 jitToInterpEntries.dvmJitToInterpBackwardBranch) >> 2);
Bill Buzbee9c4b7c82009-09-10 10:10:38 -07003325#else
Bill Buzbee1465db52009-09-23 17:17:35 -07003326 newLIR3(cUnit, kThumbLdrRRI5, r0, rGLUE,
Bill Buzbee9c4b7c82009-09-10 10:10:38 -07003327 offsetof(InterpState, jitToInterpEntries.dvmJitToInterpNormal) >> 2);
3328#endif
Bill Buzbee1465db52009-09-23 17:17:35 -07003329 newLIR1(cUnit, kThumbBlxR, r0);
Jeff Hao97319a82009-08-12 16:57:15 -07003330 addWordData(cUnit, (int) (cUnit->method->insns + offset), true);
3331}
3332
3333#endif
Ben Chengba4fc8b2009-06-01 13:00:29 -07003334/* Chaining cell for monomorphic method invocations. */
Ben Cheng38329f52009-07-07 14:19:20 -07003335static void handleInvokeSingletonChainingCell(CompilationUnit *cUnit,
3336 const Method *callee)
Ben Chengba4fc8b2009-06-01 13:00:29 -07003337{
Ben Cheng11d8f142010-03-24 15:24:19 -07003338 /*
3339 * Use raw instruction constructors to guarantee that the generated
3340 * instructions fit the predefined cell size.
3341 */
3342 newLIR3(cUnit, kThumbLdrRRI5, r0, rGLUE,
3343 offsetof(InterpState,
3344 jitToInterpEntries.dvmJitToInterpTraceSelect) >> 2);
3345 newLIR1(cUnit, kThumbBlxR, r0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003346 addWordData(cUnit, (int) (callee->insns), true);
3347}
3348
Ben Cheng38329f52009-07-07 14:19:20 -07003349/* Chaining cell for monomorphic method invocations. */
3350static void handleInvokePredictedChainingCell(CompilationUnit *cUnit)
3351{
3352
3353 /* Should not be executed in the initial state */
3354 addWordData(cUnit, PREDICTED_CHAIN_BX_PAIR_INIT, true);
3355 /* To be filled: class */
3356 addWordData(cUnit, PREDICTED_CHAIN_CLAZZ_INIT, true);
3357 /* To be filled: method */
3358 addWordData(cUnit, PREDICTED_CHAIN_METHOD_INIT, true);
3359 /*
3360 * Rechain count. The initial value of 0 here will trigger chaining upon
3361 * the first invocation of this callsite.
3362 */
3363 addWordData(cUnit, PREDICTED_CHAIN_COUNTER_INIT, true);
3364}
3365
Ben Chengba4fc8b2009-06-01 13:00:29 -07003366/* Load the Dalvik PC into r0 and jump to the specified target */
3367static void handlePCReconstruction(CompilationUnit *cUnit,
Bill Buzbee89efc3d2009-07-28 11:22:22 -07003368 ArmLIR *targetLabel)
Ben Chengba4fc8b2009-06-01 13:00:29 -07003369{
Bill Buzbee89efc3d2009-07-28 11:22:22 -07003370 ArmLIR **pcrLabel =
3371 (ArmLIR **) cUnit->pcReconstructionList.elemList;
Ben Chengba4fc8b2009-06-01 13:00:29 -07003372 int numElems = cUnit->pcReconstructionList.numUsed;
3373 int i;
3374 for (i = 0; i < numElems; i++) {
3375 dvmCompilerAppendLIR(cUnit, (LIR *) pcrLabel[i]);
3376 /* r0 = dalvik PC */
3377 loadConstant(cUnit, r0, pcrLabel[i]->operands[0]);
3378 genUnconditionalBranch(cUnit, targetLabel);
3379 }
3380}
3381
Bill Buzbee1465db52009-09-23 17:17:35 -07003382static char *extendedMIROpNames[kMirOpLast - kMirOpFirst] = {
3383 "kMirOpPhi",
3384 "kMirOpNullNRangeUpCheck",
3385 "kMirOpNullNRangeDownCheck",
3386 "kMirOpLowerBound",
3387 "kMirOpPunt",
Ben Cheng4238ec22009-08-24 16:32:22 -07003388};
3389
3390/*
3391 * vA = arrayReg;
3392 * vB = idxReg;
3393 * vC = endConditionReg;
3394 * arg[0] = maxC
3395 * arg[1] = minC
3396 * arg[2] = loopBranchConditionCode
3397 */
3398static void genHoistedChecksForCountUpLoop(CompilationUnit *cUnit, MIR *mir)
3399{
Bill Buzbee1465db52009-09-23 17:17:35 -07003400 /*
3401 * NOTE: these synthesized blocks don't have ssa names assigned
3402 * for Dalvik registers. However, because they dominate the following
3403 * blocks we can simply use the Dalvik name w/ subscript 0 as the
3404 * ssa name.
3405 */
Ben Cheng4238ec22009-08-24 16:32:22 -07003406 DecodedInstruction *dInsn = &mir->dalvikInsn;
3407 const int lenOffset = offsetof(ArrayObject, length);
Ben Cheng4238ec22009-08-24 16:32:22 -07003408 const int maxC = dInsn->arg[0];
3409 const int minC = dInsn->arg[1];
Bill Buzbee1465db52009-09-23 17:17:35 -07003410 int regLength;
3411 RegLocation rlArray = cUnit->regLocation[mir->dalvikInsn.vA];
3412 RegLocation rlIdxEnd = cUnit->regLocation[mir->dalvikInsn.vC];
Ben Cheng4238ec22009-08-24 16:32:22 -07003413
3414 /* regArray <- arrayRef */
Bill Buzbee1465db52009-09-23 17:17:35 -07003415 rlArray = loadValue(cUnit, rlArray, kCoreReg);
3416 rlIdxEnd = loadValue(cUnit, rlIdxEnd, kCoreReg);
3417 genRegImmCheck(cUnit, kArmCondEq, rlArray.lowReg, 0, 0,
Ben Cheng4238ec22009-08-24 16:32:22 -07003418 (ArmLIR *) cUnit->loopAnalysis->branchToPCR);
3419
3420 /* regLength <- len(arrayRef) */
Bill Buzbeec6f10662010-02-09 11:16:15 -08003421 regLength = dvmCompilerAllocTemp(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07003422 loadWordDisp(cUnit, rlArray.lowReg, lenOffset, regLength);
Ben Cheng4238ec22009-08-24 16:32:22 -07003423
3424 int delta = maxC;
3425 /*
3426 * If the loop end condition is ">=" instead of ">", then the largest value
3427 * of the index is "endCondition - 1".
3428 */
3429 if (dInsn->arg[2] == OP_IF_GE) {
3430 delta--;
3431 }
3432
3433 if (delta) {
Bill Buzbeec6f10662010-02-09 11:16:15 -08003434 int tReg = dvmCompilerAllocTemp(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07003435 opRegRegImm(cUnit, kOpAdd, tReg, rlIdxEnd.lowReg, delta);
3436 rlIdxEnd.lowReg = tReg;
Bill Buzbeec6f10662010-02-09 11:16:15 -08003437 dvmCompilerFreeTemp(cUnit, tReg);
Ben Cheng4238ec22009-08-24 16:32:22 -07003438 }
3439 /* Punt if "regIdxEnd < len(Array)" is false */
Bill Buzbee1465db52009-09-23 17:17:35 -07003440 genRegRegCheck(cUnit, kArmCondGe, rlIdxEnd.lowReg, regLength, 0,
Ben Cheng0fd31e42009-09-03 14:40:16 -07003441 (ArmLIR *) cUnit->loopAnalysis->branchToPCR);
Ben Cheng4238ec22009-08-24 16:32:22 -07003442}
3443
3444/*
3445 * vA = arrayReg;
3446 * vB = idxReg;
3447 * vC = endConditionReg;
3448 * arg[0] = maxC
3449 * arg[1] = minC
3450 * arg[2] = loopBranchConditionCode
3451 */
3452static void genHoistedChecksForCountDownLoop(CompilationUnit *cUnit, MIR *mir)
3453{
3454 DecodedInstruction *dInsn = &mir->dalvikInsn;
3455 const int lenOffset = offsetof(ArrayObject, length);
Bill Buzbeec6f10662010-02-09 11:16:15 -08003456 const int regLength = dvmCompilerAllocTemp(cUnit);
Ben Cheng4238ec22009-08-24 16:32:22 -07003457 const int maxC = dInsn->arg[0];
3458 const int minC = dInsn->arg[1];
Bill Buzbee1465db52009-09-23 17:17:35 -07003459 RegLocation rlArray = cUnit->regLocation[mir->dalvikInsn.vA];
3460 RegLocation rlIdxInit = cUnit->regLocation[mir->dalvikInsn.vB];
Ben Cheng4238ec22009-08-24 16:32:22 -07003461
3462 /* regArray <- arrayRef */
Bill Buzbee1465db52009-09-23 17:17:35 -07003463 rlArray = loadValue(cUnit, rlArray, kCoreReg);
3464 rlIdxInit = loadValue(cUnit, rlIdxInit, kCoreReg);
3465 genRegImmCheck(cUnit, kArmCondEq, rlArray.lowReg, 0, 0,
Ben Cheng4238ec22009-08-24 16:32:22 -07003466 (ArmLIR *) cUnit->loopAnalysis->branchToPCR);
3467
3468 /* regLength <- len(arrayRef) */
Bill Buzbee1465db52009-09-23 17:17:35 -07003469 loadWordDisp(cUnit, rlArray.lowReg, lenOffset, regLength);
Ben Cheng4238ec22009-08-24 16:32:22 -07003470
3471 if (maxC) {
Bill Buzbeec6f10662010-02-09 11:16:15 -08003472 int tReg = dvmCompilerAllocTemp(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07003473 opRegRegImm(cUnit, kOpAdd, tReg, rlIdxInit.lowReg, maxC);
3474 rlIdxInit.lowReg = tReg;
Bill Buzbeec6f10662010-02-09 11:16:15 -08003475 dvmCompilerFreeTemp(cUnit, tReg);
Ben Cheng4238ec22009-08-24 16:32:22 -07003476 }
3477
3478 /* Punt if "regIdxInit < len(Array)" is false */
Bill Buzbee1465db52009-09-23 17:17:35 -07003479 genRegRegCheck(cUnit, kArmCondGe, rlIdxInit.lowReg, regLength, 0,
Ben Cheng0fd31e42009-09-03 14:40:16 -07003480 (ArmLIR *) cUnit->loopAnalysis->branchToPCR);
Ben Cheng4238ec22009-08-24 16:32:22 -07003481}
3482
3483/*
3484 * vA = idxReg;
3485 * vB = minC;
3486 */
3487static void genHoistedLowerBoundCheck(CompilationUnit *cUnit, MIR *mir)
3488{
3489 DecodedInstruction *dInsn = &mir->dalvikInsn;
Ben Cheng4238ec22009-08-24 16:32:22 -07003490 const int minC = dInsn->vB;
Bill Buzbee1465db52009-09-23 17:17:35 -07003491 RegLocation rlIdx = cUnit->regLocation[mir->dalvikInsn.vA];
Ben Cheng4238ec22009-08-24 16:32:22 -07003492
3493 /* regIdx <- initial index value */
Bill Buzbee1465db52009-09-23 17:17:35 -07003494 rlIdx = loadValue(cUnit, rlIdx, kCoreReg);
Ben Cheng4238ec22009-08-24 16:32:22 -07003495
3496 /* Punt if "regIdxInit + minC >= 0" is false */
Bill Buzbee1465db52009-09-23 17:17:35 -07003497 genRegImmCheck(cUnit, kArmCondLt, rlIdx.lowReg, -minC, 0,
Ben Cheng4238ec22009-08-24 16:32:22 -07003498 (ArmLIR *) cUnit->loopAnalysis->branchToPCR);
3499}
3500
3501/* Extended MIR instructions like PHI */
3502static void handleExtendedMIR(CompilationUnit *cUnit, MIR *mir)
3503{
Bill Buzbee1465db52009-09-23 17:17:35 -07003504 int opOffset = mir->dalvikInsn.opCode - kMirOpFirst;
Ben Cheng4238ec22009-08-24 16:32:22 -07003505 char *msg = dvmCompilerNew(strlen(extendedMIROpNames[opOffset]) + 1,
3506 false);
3507 strcpy(msg, extendedMIROpNames[opOffset]);
Bill Buzbee1465db52009-09-23 17:17:35 -07003508 newLIR1(cUnit, kArmPseudoExtended, (int) msg);
Ben Cheng4238ec22009-08-24 16:32:22 -07003509
3510 switch (mir->dalvikInsn.opCode) {
Bill Buzbee1465db52009-09-23 17:17:35 -07003511 case kMirOpPhi: {
Ben Cheng4238ec22009-08-24 16:32:22 -07003512 char *ssaString = dvmCompilerGetSSAString(cUnit, mir->ssaRep);
Bill Buzbee1465db52009-09-23 17:17:35 -07003513 newLIR1(cUnit, kArmPseudoSSARep, (int) ssaString);
Ben Cheng4238ec22009-08-24 16:32:22 -07003514 break;
3515 }
Bill Buzbee1465db52009-09-23 17:17:35 -07003516 case kMirOpNullNRangeUpCheck: {
Ben Cheng4238ec22009-08-24 16:32:22 -07003517 genHoistedChecksForCountUpLoop(cUnit, mir);
3518 break;
3519 }
Bill Buzbee1465db52009-09-23 17:17:35 -07003520 case kMirOpNullNRangeDownCheck: {
Ben Cheng4238ec22009-08-24 16:32:22 -07003521 genHoistedChecksForCountDownLoop(cUnit, mir);
3522 break;
3523 }
Bill Buzbee1465db52009-09-23 17:17:35 -07003524 case kMirOpLowerBound: {
Ben Cheng4238ec22009-08-24 16:32:22 -07003525 genHoistedLowerBoundCheck(cUnit, mir);
3526 break;
3527 }
Bill Buzbee1465db52009-09-23 17:17:35 -07003528 case kMirOpPunt: {
Ben Cheng4238ec22009-08-24 16:32:22 -07003529 genUnconditionalBranch(cUnit,
3530 (ArmLIR *) cUnit->loopAnalysis->branchToPCR);
3531 break;
3532 }
3533 default:
3534 break;
3535 }
3536}
3537
3538/*
3539 * Create a PC-reconstruction cell for the starting offset of this trace.
3540 * Since the PCR cell is placed near the end of the compiled code which is
3541 * usually out of range for a conditional branch, we put two branches (one
3542 * branch over to the loop body and one layover branch to the actual PCR) at the
3543 * end of the entry block.
3544 */
3545static void setupLoopEntryBlock(CompilationUnit *cUnit, BasicBlock *entry,
3546 ArmLIR *bodyLabel)
3547{
3548 /* Set up the place holder to reconstruct this Dalvik PC */
3549 ArmLIR *pcrLabel = dvmCompilerNew(sizeof(ArmLIR), true);
Ben Chenga4973592010-03-31 11:59:18 -07003550 pcrLabel->opCode = kArmPseudoPCReconstructionCell;
Ben Cheng4238ec22009-08-24 16:32:22 -07003551 pcrLabel->operands[0] =
3552 (int) (cUnit->method->insns + entry->startOffset);
3553 pcrLabel->operands[1] = entry->startOffset;
3554 /* Insert the place holder to the growable list */
3555 dvmInsertGrowableList(&cUnit->pcReconstructionList, pcrLabel);
3556
3557 /*
3558 * Next, create two branches - one branch over to the loop body and the
3559 * other branch to the PCR cell to punt.
3560 */
3561 ArmLIR *branchToBody = dvmCompilerNew(sizeof(ArmLIR), true);
Bill Buzbee1465db52009-09-23 17:17:35 -07003562 branchToBody->opCode = kThumbBUncond;
Ben Cheng4238ec22009-08-24 16:32:22 -07003563 branchToBody->generic.target = (LIR *) bodyLabel;
Ben Chengdcf3e5d2009-09-11 13:42:05 -07003564 setupResourceMasks(branchToBody);
Ben Cheng4238ec22009-08-24 16:32:22 -07003565 cUnit->loopAnalysis->branchToBody = (LIR *) branchToBody;
3566
3567 ArmLIR *branchToPCR = dvmCompilerNew(sizeof(ArmLIR), true);
Bill Buzbee1465db52009-09-23 17:17:35 -07003568 branchToPCR->opCode = kThumbBUncond;
Ben Cheng4238ec22009-08-24 16:32:22 -07003569 branchToPCR->generic.target = (LIR *) pcrLabel;
Ben Chengdcf3e5d2009-09-11 13:42:05 -07003570 setupResourceMasks(branchToPCR);
Ben Cheng4238ec22009-08-24 16:32:22 -07003571 cUnit->loopAnalysis->branchToPCR = (LIR *) branchToPCR;
3572}
3573
Ben Chengd5adae12010-03-26 17:45:28 -07003574#if defined(WITH_SELF_VERIFICATION)
3575static bool selfVerificationPuntOps(MIR *mir)
3576{
3577 DecodedInstruction *decInsn = &mir->dalvikInsn;
3578 OpCode op = decInsn->opCode;
3579 int flags = dexGetInstrFlags(gDvm.instrFlags, op);
3580 /*
3581 * All opcodes that can throw exceptions and use the
3582 * TEMPLATE_THROW_EXCEPTION_COMMON template should be excluded in the trace
3583 * under self-verification mode.
3584 */
3585 return (op == OP_MONITOR_ENTER || op == OP_MONITOR_EXIT ||
3586 op == OP_NEW_INSTANCE || op == OP_NEW_ARRAY ||
3587 op == OP_CHECK_CAST || op == OP_MOVE_EXCEPTION ||
3588 op == OP_FILL_ARRAY_DATA || op == OP_EXECUTE_INLINE ||
3589 op == OP_EXECUTE_INLINE_RANGE ||
3590 (flags & kInstrInvoke));
3591}
3592#endif
3593
Ben Chengba4fc8b2009-06-01 13:00:29 -07003594void dvmCompilerMIR2LIR(CompilationUnit *cUnit)
3595{
3596 /* Used to hold the labels of each block */
Bill Buzbee89efc3d2009-07-28 11:22:22 -07003597 ArmLIR *labelList =
3598 dvmCompilerNew(sizeof(ArmLIR) * cUnit->numBlocks, true);
Ben Chengcec26f62010-01-15 15:29:33 -08003599 GrowableList chainingListByType[kChainingCellGap];
Ben Chengba4fc8b2009-06-01 13:00:29 -07003600 int i;
3601
3602 /*
Ben Cheng38329f52009-07-07 14:19:20 -07003603 * Initialize various types chaining lists.
Ben Chengba4fc8b2009-06-01 13:00:29 -07003604 */
Ben Chengcec26f62010-01-15 15:29:33 -08003605 for (i = 0; i < kChainingCellGap; i++) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07003606 dvmInitGrowableList(&chainingListByType[i], 2);
3607 }
3608
3609 BasicBlock **blockList = cUnit->blockList;
3610
Bill Buzbee6e963e12009-06-17 16:56:19 -07003611 if (cUnit->executionCount) {
3612 /*
3613 * Reserve 6 bytes at the beginning of the trace
3614 * +----------------------------+
3615 * | execution count (4 bytes) |
3616 * +----------------------------+
3617 * | chain cell offset (2 bytes)|
3618 * +----------------------------+
3619 * ...and then code to increment the execution
3620 * count:
3621 * mov r0, pc @ move adr of "mov r0,pc" + 4 to r0
3622 * sub r0, #10 @ back up to addr of executionCount
3623 * ldr r1, [r0]
3624 * add r1, #1
3625 * str r1, [r0]
3626 */
Bill Buzbee1465db52009-09-23 17:17:35 -07003627 newLIR1(cUnit, kArm16BitData, 0);
3628 newLIR1(cUnit, kArm16BitData, 0);
Ben Chengcc6600c2009-06-22 14:45:16 -07003629 cUnit->chainCellOffsetLIR =
Bill Buzbee1465db52009-09-23 17:17:35 -07003630 (LIR *) newLIR1(cUnit, kArm16BitData, CHAIN_CELL_OFFSET_TAG);
Bill Buzbee6e963e12009-06-17 16:56:19 -07003631 cUnit->headerSize = 6;
Bill Buzbee270c1d62009-08-13 16:58:07 -07003632 /* Thumb instruction used directly here to ensure correct size */
Bill Buzbee1465db52009-09-23 17:17:35 -07003633 newLIR2(cUnit, kThumbMovRR_H2L, r0, rpc);
3634 newLIR2(cUnit, kThumbSubRI8, r0, 10);
3635 newLIR3(cUnit, kThumbLdrRRI5, r1, r0, 0);
3636 newLIR2(cUnit, kThumbAddRI8, r1, 1);
3637 newLIR3(cUnit, kThumbStrRRI5, r1, r0, 0);
Bill Buzbee6e963e12009-06-17 16:56:19 -07003638 } else {
3639 /* Just reserve 2 bytes for the chain cell offset */
Ben Chengcc6600c2009-06-22 14:45:16 -07003640 cUnit->chainCellOffsetLIR =
Bill Buzbee1465db52009-09-23 17:17:35 -07003641 (LIR *) newLIR1(cUnit, kArm16BitData, CHAIN_CELL_OFFSET_TAG);
Bill Buzbee6e963e12009-06-17 16:56:19 -07003642 cUnit->headerSize = 2;
3643 }
Ben Cheng1efc9c52009-06-08 18:25:27 -07003644
Ben Chengba4fc8b2009-06-01 13:00:29 -07003645 /* Handle the content in each basic block */
3646 for (i = 0; i < cUnit->numBlocks; i++) {
3647 blockList[i]->visited = true;
3648 MIR *mir;
3649
3650 labelList[i].operands[0] = blockList[i]->startOffset;
3651
Ben Chengcec26f62010-01-15 15:29:33 -08003652 if (blockList[i]->blockType >= kChainingCellGap) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07003653 /*
3654 * Append the label pseudo LIR first. Chaining cells will be handled
3655 * separately afterwards.
3656 */
3657 dvmCompilerAppendLIR(cUnit, (LIR *) &labelList[i]);
3658 }
3659
Bill Buzbee1465db52009-09-23 17:17:35 -07003660 if (blockList[i]->blockType == kEntryBlock) {
Ben Chenga4973592010-03-31 11:59:18 -07003661 labelList[i].opCode = kArmPseudoEntryBlock;
Ben Cheng4238ec22009-08-24 16:32:22 -07003662 if (blockList[i]->firstMIRInsn == NULL) {
3663 continue;
3664 } else {
3665 setupLoopEntryBlock(cUnit, blockList[i],
3666 &labelList[blockList[i]->fallThrough->id]);
3667 }
Bill Buzbee1465db52009-09-23 17:17:35 -07003668 } else if (blockList[i]->blockType == kExitBlock) {
Ben Chenga4973592010-03-31 11:59:18 -07003669 labelList[i].opCode = kArmPseudoExitBlock;
Ben Cheng4238ec22009-08-24 16:32:22 -07003670 goto gen_fallthrough;
Bill Buzbee1465db52009-09-23 17:17:35 -07003671 } else if (blockList[i]->blockType == kDalvikByteCode) {
3672 labelList[i].opCode = kArmPseudoNormalBlockLabel;
Ben Chenge9695e52009-06-16 16:11:47 -07003673 /* Reset the register state */
Bill Buzbeec6f10662010-02-09 11:16:15 -08003674 dvmCompilerResetRegPool(cUnit);
3675 dvmCompilerClobberAllRegs(cUnit);
3676 dvmCompilerResetNullCheck(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003677 } else {
3678 switch (blockList[i]->blockType) {
Bill Buzbee1465db52009-09-23 17:17:35 -07003679 case kChainingCellNormal:
Ben Chenga4973592010-03-31 11:59:18 -07003680 labelList[i].opCode = kArmPseudoChainingCellNormal;
Ben Chengba4fc8b2009-06-01 13:00:29 -07003681 /* handle the codegen later */
3682 dvmInsertGrowableList(
Bill Buzbee1465db52009-09-23 17:17:35 -07003683 &chainingListByType[kChainingCellNormal], (void *) i);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003684 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07003685 case kChainingCellInvokeSingleton:
Ben Cheng38329f52009-07-07 14:19:20 -07003686 labelList[i].opCode =
Ben Chenga4973592010-03-31 11:59:18 -07003687 kArmPseudoChainingCellInvokeSingleton;
Ben Chengba4fc8b2009-06-01 13:00:29 -07003688 labelList[i].operands[0] =
3689 (int) blockList[i]->containingMethod;
3690 /* handle the codegen later */
3691 dvmInsertGrowableList(
Bill Buzbee1465db52009-09-23 17:17:35 -07003692 &chainingListByType[kChainingCellInvokeSingleton],
Ben Cheng38329f52009-07-07 14:19:20 -07003693 (void *) i);
3694 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07003695 case kChainingCellInvokePredicted:
Ben Cheng38329f52009-07-07 14:19:20 -07003696 labelList[i].opCode =
Ben Chenga4973592010-03-31 11:59:18 -07003697 kArmPseudoChainingCellInvokePredicted;
Ben Cheng38329f52009-07-07 14:19:20 -07003698 /* handle the codegen later */
3699 dvmInsertGrowableList(
Bill Buzbee1465db52009-09-23 17:17:35 -07003700 &chainingListByType[kChainingCellInvokePredicted],
Ben Cheng38329f52009-07-07 14:19:20 -07003701 (void *) i);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003702 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07003703 case kChainingCellHot:
Ben Chengba4fc8b2009-06-01 13:00:29 -07003704 labelList[i].opCode =
Ben Chenga4973592010-03-31 11:59:18 -07003705 kArmPseudoChainingCellHot;
Ben Chengba4fc8b2009-06-01 13:00:29 -07003706 /* handle the codegen later */
3707 dvmInsertGrowableList(
Bill Buzbee1465db52009-09-23 17:17:35 -07003708 &chainingListByType[kChainingCellHot],
Ben Chengba4fc8b2009-06-01 13:00:29 -07003709 (void *) i);
3710 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07003711 case kPCReconstruction:
Ben Chengba4fc8b2009-06-01 13:00:29 -07003712 /* Make sure exception handling block is next */
3713 labelList[i].opCode =
Ben Chenga4973592010-03-31 11:59:18 -07003714 kArmPseudoPCReconstructionBlockLabel;
Ben Chengba4fc8b2009-06-01 13:00:29 -07003715 assert (i == cUnit->numBlocks - 2);
3716 handlePCReconstruction(cUnit, &labelList[i+1]);
3717 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07003718 case kExceptionHandling:
3719 labelList[i].opCode = kArmPseudoEHBlockLabel;
Ben Chengba4fc8b2009-06-01 13:00:29 -07003720 if (cUnit->pcReconstructionList.numUsed) {
Bill Buzbee270c1d62009-08-13 16:58:07 -07003721 loadWordDisp(cUnit, rGLUE, offsetof(InterpState,
3722 jitToInterpEntries.dvmJitToInterpPunt),
3723 r1);
Bill Buzbee1465db52009-09-23 17:17:35 -07003724 opReg(cUnit, kOpBlx, r1);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003725 }
3726 break;
Bill Buzbee9c4b7c82009-09-10 10:10:38 -07003727#if defined(WITH_SELF_VERIFICATION) || defined(WITH_JIT_TUNING)
Bill Buzbee1465db52009-09-23 17:17:35 -07003728 case kChainingCellBackwardBranch:
Jeff Hao97319a82009-08-12 16:57:15 -07003729 labelList[i].opCode =
Ben Chenga4973592010-03-31 11:59:18 -07003730 kArmPseudoChainingCellBackwardBranch;
Jeff Hao97319a82009-08-12 16:57:15 -07003731 /* handle the codegen later */
3732 dvmInsertGrowableList(
Bill Buzbee1465db52009-09-23 17:17:35 -07003733 &chainingListByType[kChainingCellBackwardBranch],
Jeff Hao97319a82009-08-12 16:57:15 -07003734 (void *) i);
3735 break;
3736#endif
Ben Chengba4fc8b2009-06-01 13:00:29 -07003737 default:
3738 break;
3739 }
3740 continue;
3741 }
Ben Chenge9695e52009-06-16 16:11:47 -07003742
Bill Buzbee89efc3d2009-07-28 11:22:22 -07003743 ArmLIR *headLIR = NULL;
Ben Chenge9695e52009-06-16 16:11:47 -07003744
Ben Chengba4fc8b2009-06-01 13:00:29 -07003745 for (mir = blockList[i]->firstMIRInsn; mir; mir = mir->next) {
Bill Buzbee1465db52009-09-23 17:17:35 -07003746
Bill Buzbeec6f10662010-02-09 11:16:15 -08003747 dvmCompilerResetRegPool(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07003748 if (gDvmJit.disableOpt & (1 << kTrackLiveTemps)) {
Bill Buzbeec6f10662010-02-09 11:16:15 -08003749 dvmCompilerClobberAllRegs(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07003750 }
3751
3752 if (gDvmJit.disableOpt & (1 << kSuppressLoads)) {
Bill Buzbeec6f10662010-02-09 11:16:15 -08003753 dvmCompilerResetDefTracking(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07003754 }
3755
3756 if (mir->dalvikInsn.opCode >= kMirOpFirst) {
Ben Cheng4238ec22009-08-24 16:32:22 -07003757 handleExtendedMIR(cUnit, mir);
3758 continue;
3759 }
3760
Bill Buzbee1465db52009-09-23 17:17:35 -07003761
Ben Chengba4fc8b2009-06-01 13:00:29 -07003762 OpCode dalvikOpCode = mir->dalvikInsn.opCode;
3763 InstructionFormat dalvikFormat =
3764 dexGetInstrFormat(gDvm.instrFormat, dalvikOpCode);
Bill Buzbee89efc3d2009-07-28 11:22:22 -07003765 ArmLIR *boundaryLIR =
Ben Chenga4973592010-03-31 11:59:18 -07003766 newLIR2(cUnit, kArmPseudoDalvikByteCodeBoundary,
Ben Chengccd6c012009-10-15 14:52:45 -07003767 mir->offset,
3768 (int) dvmCompilerGetDalvikDisassembly(&mir->dalvikInsn)
3769 );
Ben Cheng4238ec22009-08-24 16:32:22 -07003770 if (mir->ssaRep) {
3771 char *ssaString = dvmCompilerGetSSAString(cUnit, mir->ssaRep);
Bill Buzbee1465db52009-09-23 17:17:35 -07003772 newLIR1(cUnit, kArmPseudoSSARep, (int) ssaString);
Ben Cheng4238ec22009-08-24 16:32:22 -07003773 }
3774
Ben Chenge9695e52009-06-16 16:11:47 -07003775 /* Remember the first LIR for this block */
3776 if (headLIR == NULL) {
3777 headLIR = boundaryLIR;
Ben Chengd7d426a2009-09-22 11:23:36 -07003778 /* Set the first boundaryLIR as a scheduling barrier */
3779 headLIR->defMask = ENCODE_ALL;
Ben Chenge9695e52009-06-16 16:11:47 -07003780 }
Ben Cheng4238ec22009-08-24 16:32:22 -07003781
Ben Chengba4fc8b2009-06-01 13:00:29 -07003782 bool notHandled;
3783 /*
3784 * Debugging: screen the opcode first to see if it is in the
3785 * do[-not]-compile list
3786 */
3787 bool singleStepMe =
3788 gDvmJit.includeSelectedOp !=
3789 ((gDvmJit.opList[dalvikOpCode >> 3] &
3790 (1 << (dalvikOpCode & 0x7))) !=
3791 0);
Ben Chengd5adae12010-03-26 17:45:28 -07003792#if defined(WITH_SELF_VERIFICATION)
3793 if (singleStepMe == false) {
3794 singleStepMe = selfVerificationPuntOps(mir);
3795 }
3796#endif
Ben Chengba4fc8b2009-06-01 13:00:29 -07003797 if (singleStepMe || cUnit->allSingleStep) {
3798 notHandled = false;
3799 genInterpSingleStep(cUnit, mir);
3800 } else {
3801 opcodeCoverage[dalvikOpCode]++;
3802 switch (dalvikFormat) {
3803 case kFmt10t:
3804 case kFmt20t:
3805 case kFmt30t:
3806 notHandled = handleFmt10t_Fmt20t_Fmt30t(cUnit,
3807 mir, blockList[i], labelList);
3808 break;
3809 case kFmt10x:
3810 notHandled = handleFmt10x(cUnit, mir);
3811 break;
3812 case kFmt11n:
3813 case kFmt31i:
3814 notHandled = handleFmt11n_Fmt31i(cUnit, mir);
3815 break;
3816 case kFmt11x:
3817 notHandled = handleFmt11x(cUnit, mir);
3818 break;
3819 case kFmt12x:
3820 notHandled = handleFmt12x(cUnit, mir);
3821 break;
3822 case kFmt20bc:
3823 notHandled = handleFmt20bc(cUnit, mir);
3824 break;
3825 case kFmt21c:
3826 case kFmt31c:
3827 notHandled = handleFmt21c_Fmt31c(cUnit, mir);
3828 break;
3829 case kFmt21h:
3830 notHandled = handleFmt21h(cUnit, mir);
3831 break;
3832 case kFmt21s:
3833 notHandled = handleFmt21s(cUnit, mir);
3834 break;
3835 case kFmt21t:
3836 notHandled = handleFmt21t(cUnit, mir, blockList[i],
3837 labelList);
3838 break;
3839 case kFmt22b:
3840 case kFmt22s:
3841 notHandled = handleFmt22b_Fmt22s(cUnit, mir);
3842 break;
3843 case kFmt22c:
3844 notHandled = handleFmt22c(cUnit, mir);
3845 break;
3846 case kFmt22cs:
3847 notHandled = handleFmt22cs(cUnit, mir);
3848 break;
3849 case kFmt22t:
3850 notHandled = handleFmt22t(cUnit, mir, blockList[i],
3851 labelList);
3852 break;
3853 case kFmt22x:
3854 case kFmt32x:
3855 notHandled = handleFmt22x_Fmt32x(cUnit, mir);
3856 break;
3857 case kFmt23x:
3858 notHandled = handleFmt23x(cUnit, mir);
3859 break;
3860 case kFmt31t:
3861 notHandled = handleFmt31t(cUnit, mir);
3862 break;
3863 case kFmt3rc:
3864 case kFmt35c:
3865 notHandled = handleFmt35c_3rc(cUnit, mir, blockList[i],
3866 labelList);
3867 break;
3868 case kFmt3rms:
3869 case kFmt35ms:
3870 notHandled = handleFmt35ms_3rms(cUnit, mir,blockList[i],
3871 labelList);
3872 break;
3873 case kFmt3inline:
Andy McFaddenb0a05412009-11-19 10:23:41 -08003874 case kFmt3rinline:
Bill Buzbeece46c942009-11-20 15:41:34 -08003875 notHandled = handleExecuteInline(cUnit, mir);
Andy McFaddenb0a05412009-11-19 10:23:41 -08003876 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07003877 case kFmt51l:
3878 notHandled = handleFmt51l(cUnit, mir);
3879 break;
3880 default:
3881 notHandled = true;
3882 break;
3883 }
3884 }
3885 if (notHandled) {
3886 LOGE("%#06x: Opcode 0x%x (%s) / Fmt %d not handled\n",
3887 mir->offset,
3888 dalvikOpCode, getOpcodeName(dalvikOpCode),
3889 dalvikFormat);
Bill Buzbeefc519dc2010-03-06 23:30:57 -08003890 dvmCompilerAbort(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003891 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07003892 }
3893 }
Ben Cheng4238ec22009-08-24 16:32:22 -07003894
Bill Buzbee1465db52009-09-23 17:17:35 -07003895 if (blockList[i]->blockType == kEntryBlock) {
Ben Cheng4238ec22009-08-24 16:32:22 -07003896 dvmCompilerAppendLIR(cUnit,
3897 (LIR *) cUnit->loopAnalysis->branchToBody);
3898 dvmCompilerAppendLIR(cUnit,
3899 (LIR *) cUnit->loopAnalysis->branchToPCR);
3900 }
3901
3902 if (headLIR) {
3903 /*
3904 * Eliminate redundant loads/stores and delay stores into later
3905 * slots
3906 */
3907 dvmCompilerApplyLocalOptimizations(cUnit, (LIR *) headLIR,
3908 cUnit->lastLIRInsn);
3909 }
3910
3911gen_fallthrough:
Ben Cheng1efc9c52009-06-08 18:25:27 -07003912 /*
3913 * Check if the block is terminated due to trace length constraint -
3914 * insert an unconditional branch to the chaining cell.
3915 */
3916 if (blockList[i]->needFallThroughBranch) {
3917 genUnconditionalBranch(cUnit,
3918 &labelList[blockList[i]->fallThrough->id]);
3919 }
3920
Ben Chengba4fc8b2009-06-01 13:00:29 -07003921 }
3922
Ben Chenge9695e52009-06-16 16:11:47 -07003923 /* Handle the chaining cells in predefined order */
Ben Chengcec26f62010-01-15 15:29:33 -08003924 for (i = 0; i < kChainingCellGap; i++) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07003925 size_t j;
3926 int *blockIdList = (int *) chainingListByType[i].elemList;
3927
3928 cUnit->numChainingCells[i] = chainingListByType[i].numUsed;
3929
3930 /* No chaining cells of this type */
3931 if (cUnit->numChainingCells[i] == 0)
3932 continue;
3933
3934 /* Record the first LIR for a new type of chaining cell */
3935 cUnit->firstChainingLIR[i] = (LIR *) &labelList[blockIdList[0]];
3936
3937 for (j = 0; j < chainingListByType[i].numUsed; j++) {
3938 int blockId = blockIdList[j];
3939
3940 /* Align this chaining cell first */
Bill Buzbee1465db52009-09-23 17:17:35 -07003941 newLIR0(cUnit, kArmPseudoPseudoAlign4);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003942
3943 /* Insert the pseudo chaining instruction */
3944 dvmCompilerAppendLIR(cUnit, (LIR *) &labelList[blockId]);
3945
3946
3947 switch (blockList[blockId]->blockType) {
Bill Buzbee1465db52009-09-23 17:17:35 -07003948 case kChainingCellNormal:
Ben Cheng1efc9c52009-06-08 18:25:27 -07003949 handleNormalChainingCell(cUnit,
Ben Chengba4fc8b2009-06-01 13:00:29 -07003950 blockList[blockId]->startOffset);
3951 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07003952 case kChainingCellInvokeSingleton:
Ben Cheng38329f52009-07-07 14:19:20 -07003953 handleInvokeSingletonChainingCell(cUnit,
Ben Chengba4fc8b2009-06-01 13:00:29 -07003954 blockList[blockId]->containingMethod);
3955 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07003956 case kChainingCellInvokePredicted:
Ben Cheng38329f52009-07-07 14:19:20 -07003957 handleInvokePredictedChainingCell(cUnit);
3958 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07003959 case kChainingCellHot:
Ben Cheng1efc9c52009-06-08 18:25:27 -07003960 handleHotChainingCell(cUnit,
Ben Chengba4fc8b2009-06-01 13:00:29 -07003961 blockList[blockId]->startOffset);
3962 break;
Bill Buzbee9c4b7c82009-09-10 10:10:38 -07003963#if defined(WITH_SELF_VERIFICATION) || defined(WITH_JIT_TUNING)
Bill Buzbee1465db52009-09-23 17:17:35 -07003964 case kChainingCellBackwardBranch:
Jeff Hao97319a82009-08-12 16:57:15 -07003965 handleBackwardBranchChainingCell(cUnit,
3966 blockList[blockId]->startOffset);
3967 break;
3968#endif
Ben Chengba4fc8b2009-06-01 13:00:29 -07003969 default:
Bill Buzbee1465db52009-09-23 17:17:35 -07003970 LOGE("Bad blocktype %d", blockList[blockId]->blockType);
Bill Buzbeefc519dc2010-03-06 23:30:57 -08003971 dvmCompilerAbort(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003972 }
3973 }
3974 }
Ben Chenge9695e52009-06-16 16:11:47 -07003975
Ben Chengcec26f62010-01-15 15:29:33 -08003976 /* Mark the bottom of chaining cells */
3977 cUnit->chainingCellBottom = (LIR *) newLIR0(cUnit, kArmChainingCellBottom);
3978
Ben Cheng6c10a972009-10-29 14:39:18 -07003979 /*
3980 * Generate the branch to the dvmJitToInterpNoChain entry point at the end
3981 * of all chaining cells for the overflow cases.
3982 */
3983 if (cUnit->switchOverflowPad) {
3984 loadConstant(cUnit, r0, (int) cUnit->switchOverflowPad);
3985 loadWordDisp(cUnit, rGLUE, offsetof(InterpState,
3986 jitToInterpEntries.dvmJitToInterpNoChain), r2);
3987 opRegReg(cUnit, kOpAdd, r1, r1);
3988 opRegRegReg(cUnit, kOpAdd, r4PC, r0, r1);
Ben Cheng86717f72010-03-05 15:27:21 -08003989#if defined(JIT_STATS)
Ben Cheng6c10a972009-10-29 14:39:18 -07003990 loadConstant(cUnit, r0, kSwitchOverflow);
3991#endif
3992 opReg(cUnit, kOpBlx, r2);
3993 }
3994
Ben Chenge9695e52009-06-16 16:11:47 -07003995 dvmCompilerApplyGlobalOptimizations(cUnit);
jeffhao9e45c0b2010-02-03 10:24:05 -08003996
3997#if defined(WITH_SELF_VERIFICATION)
3998 selfVerificationBranchInsertPass(cUnit);
3999#endif
Ben Chengba4fc8b2009-06-01 13:00:29 -07004000}
4001
4002/* Accept the work and start compiling */
Bill Buzbee716f1202009-07-23 13:22:09 -07004003bool dvmCompilerDoWork(CompilerWorkOrder *work)
Ben Chengba4fc8b2009-06-01 13:00:29 -07004004{
Ben Chengccd6c012009-10-15 14:52:45 -07004005 bool res;
Ben Chengba4fc8b2009-06-01 13:00:29 -07004006
Ben Cheng6999d842010-01-26 16:46:15 -08004007 if (gDvmJit.codeCacheFull) {
Ben Chengccd6c012009-10-15 14:52:45 -07004008 return false;
4009 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07004010
Ben Chengccd6c012009-10-15 14:52:45 -07004011 switch (work->kind) {
4012 case kWorkOrderMethod:
4013 res = dvmCompileMethod(work->info, &work->result);
4014 break;
4015 case kWorkOrderTrace:
4016 /* Start compilation with maximally allowed trace length */
Bill Buzbeefc519dc2010-03-06 23:30:57 -08004017 res = dvmCompileTrace(work->info, JIT_MAX_TRACE_LEN, &work->result,
4018 work->bailPtr);
Ben Chengccd6c012009-10-15 14:52:45 -07004019 break;
4020 case kWorkOrderTraceDebug: {
4021 bool oldPrintMe = gDvmJit.printMe;
4022 gDvmJit.printMe = true;
4023 /* Start compilation with maximally allowed trace length */
Bill Buzbeefc519dc2010-03-06 23:30:57 -08004024 res = dvmCompileTrace(work->info, JIT_MAX_TRACE_LEN, &work->result,
4025 work->bailPtr);
Elliott Hughes672511b2010-04-26 17:40:13 -07004026 gDvmJit.printMe = oldPrintMe;
Ben Chengccd6c012009-10-15 14:52:45 -07004027 break;
4028 }
4029 default:
4030 res = false;
Bill Buzbeefc519dc2010-03-06 23:30:57 -08004031 LOGE("Jit: unknown work order type");
Elliott Hughes672511b2010-04-26 17:40:13 -07004032 assert(0); // Bail if debug build, discard otherwise
Ben Chengccd6c012009-10-15 14:52:45 -07004033 }
4034 return res;
Ben Chengba4fc8b2009-06-01 13:00:29 -07004035}
4036
Ben Chengba4fc8b2009-06-01 13:00:29 -07004037/* Architectural-specific debugging helpers go here */
4038void dvmCompilerArchDump(void)
4039{
4040 /* Print compiled opcode in this VM instance */
4041 int i, start, streak;
4042 char buf[1024];
4043
4044 streak = i = 0;
4045 buf[0] = 0;
4046 while (opcodeCoverage[i] == 0 && i < 256) {
4047 i++;
4048 }
4049 if (i == 256) {
4050 return;
4051 }
4052 for (start = i++, streak = 1; i < 256; i++) {
4053 if (opcodeCoverage[i]) {
4054 streak++;
4055 } else {
4056 if (streak == 1) {
4057 sprintf(buf+strlen(buf), "%x,", start);
4058 } else {
4059 sprintf(buf+strlen(buf), "%x-%x,", start, start + streak - 1);
4060 }
4061 streak = 0;
4062 while (opcodeCoverage[i] == 0 && i < 256) {
4063 i++;
4064 }
4065 if (i < 256) {
4066 streak = 1;
4067 start = i;
4068 }
4069 }
4070 }
4071 if (streak) {
4072 if (streak == 1) {
4073 sprintf(buf+strlen(buf), "%x", start);
4074 } else {
4075 sprintf(buf+strlen(buf), "%x-%x", start, start + streak - 1);
4076 }
4077 }
4078 if (strlen(buf)) {
Ben Cheng8b258bf2009-06-24 17:27:07 -07004079 LOGD("dalvik.vm.jit.op = %s", buf);
Ben Chengba4fc8b2009-06-01 13:00:29 -07004080 }
4081}
Ben Chengd7d426a2009-09-22 11:23:36 -07004082
4083/* Common initialization routine for an architecture family */
4084bool dvmCompilerArchInit()
4085{
4086 int i;
4087
Bill Buzbee1465db52009-09-23 17:17:35 -07004088 for (i = 0; i < kArmLast; i++) {
Ben Chengd7d426a2009-09-22 11:23:36 -07004089 if (EncodingMap[i].opCode != i) {
4090 LOGE("Encoding order for %s is wrong: expecting %d, seeing %d",
4091 EncodingMap[i].name, i, EncodingMap[i].opCode);
Bill Buzbeefc519dc2010-03-06 23:30:57 -08004092 dvmAbort(); // OK to dvmAbort - build error
Ben Chengd7d426a2009-09-22 11:23:36 -07004093 }
4094 }
4095
Ben Cheng5d90c202009-11-22 23:31:11 -08004096 return dvmCompilerArchVariantInit();
4097}
4098
4099void *dvmCompilerGetInterpretTemplate()
4100{
4101 return (void*) ((int)gDvmJit.codeCache +
4102 templateEntryOffsets[TEMPLATE_INTERPRET]);
4103}
4104
4105/* Needed by the ld/st optmizatons */
4106ArmLIR* dvmCompilerRegCopyNoInsert(CompilationUnit *cUnit, int rDest, int rSrc)
4107{
4108 return genRegCopyNoInsert(cUnit, rDest, rSrc);
4109}
4110
4111/* Needed by the register allocator */
4112ArmLIR* dvmCompilerRegCopy(CompilationUnit *cUnit, int rDest, int rSrc)
4113{
4114 return genRegCopy(cUnit, rDest, rSrc);
4115}
4116
4117/* Needed by the register allocator */
4118void dvmCompilerRegCopyWide(CompilationUnit *cUnit, int destLo, int destHi,
4119 int srcLo, int srcHi)
4120{
4121 genRegCopyWide(cUnit, destLo, destHi, srcLo, srcHi);
4122}
4123
4124void dvmCompilerFlushRegImpl(CompilationUnit *cUnit, int rBase,
4125 int displacement, int rSrc, OpSize size)
4126{
4127 storeBaseDisp(cUnit, rBase, displacement, rSrc, size);
4128}
4129
4130void dvmCompilerFlushRegWideImpl(CompilationUnit *cUnit, int rBase,
4131 int displacement, int rSrcLo, int rSrcHi)
4132{
4133 storeBaseDispWide(cUnit, rBase, displacement, rSrcLo, rSrcHi);
Ben Chengd7d426a2009-09-22 11:23:36 -07004134}