blob: cd0f18d999be628225675a4e1b76b15a90274d5c [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
buzbee919eb062010-07-12 12:59:22 -070027/*
28 * Mark garbage collection card. Skip if the value we're storing is null.
29 */
30static void markCard(CompilationUnit *cUnit, int valReg, int tgtAddrReg)
31{
32 int regCardBase = dvmCompilerAllocTemp(cUnit);
33 int regCardNo = dvmCompilerAllocTemp(cUnit);
34 opRegImm(cUnit, kOpCmp, valReg, 0); /* storing null? */
35 ArmLIR *branchOver = opCondBranch(cUnit, kArmCondEq);
36 loadWordDisp(cUnit, rGLUE, offsetof(InterpState, cardTable),
37 regCardBase);
38 opRegRegImm(cUnit, kOpLsr, regCardNo, tgtAddrReg, GC_CARD_SHIFT);
39 storeBaseIndexed(cUnit, regCardBase, regCardNo, regCardBase, 0,
40 kUnsignedByte);
41 ArmLIR *target = newLIR0(cUnit, kArmPseudoTargetLabel);
42 target->defMask = ENCODE_ALL;
43 branchOver->generic.target = (LIR *)target;
44}
45
Ben Cheng5d90c202009-11-22 23:31:11 -080046static bool genConversionCall(CompilationUnit *cUnit, MIR *mir, void *funct,
47 int srcSize, int tgtSize)
48{
49 /*
50 * Don't optimize the register usage since it calls out to template
51 * functions
52 */
53 RegLocation rlSrc;
54 RegLocation rlDest;
Bill Buzbeec6f10662010-02-09 11:16:15 -080055 dvmCompilerFlushAllRegs(cUnit); /* Send everything to home location */
Ben Cheng5d90c202009-11-22 23:31:11 -080056 if (srcSize == 1) {
Bill Buzbeec6f10662010-02-09 11:16:15 -080057 rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
Ben Cheng5d90c202009-11-22 23:31:11 -080058 loadValueDirectFixed(cUnit, rlSrc, r0);
59 } else {
Bill Buzbeec6f10662010-02-09 11:16:15 -080060 rlSrc = dvmCompilerGetSrcWide(cUnit, mir, 0, 1);
Ben Cheng5d90c202009-11-22 23:31:11 -080061 loadValueDirectWideFixed(cUnit, rlSrc, r0, r1);
62 }
Ben Chengbd1326d2010-04-02 15:04:53 -070063 LOAD_FUNC_ADDR(cUnit, r2, (int)funct);
Ben Cheng5d90c202009-11-22 23:31:11 -080064 opReg(cUnit, kOpBlx, r2);
Elliott Hughes6a555132010-02-25 15:41:42 -080065 dvmCompilerClobberCallRegs(cUnit);
Ben Cheng5d90c202009-11-22 23:31:11 -080066 if (tgtSize == 1) {
67 RegLocation rlResult;
Bill Buzbeec6f10662010-02-09 11:16:15 -080068 rlDest = dvmCompilerGetDest(cUnit, mir, 0);
69 rlResult = dvmCompilerGetReturn(cUnit);
Ben Cheng5d90c202009-11-22 23:31:11 -080070 storeValue(cUnit, rlDest, rlResult);
71 } else {
72 RegLocation rlResult;
Bill Buzbeec6f10662010-02-09 11:16:15 -080073 rlDest = dvmCompilerGetDestWide(cUnit, mir, 0, 1);
74 rlResult = dvmCompilerGetReturnWide(cUnit);
Ben Cheng5d90c202009-11-22 23:31:11 -080075 storeValueWide(cUnit, rlDest, rlResult);
76 }
77 return false;
78}
Ben Chengba4fc8b2009-06-01 13:00:29 -070079
Ben Cheng5d90c202009-11-22 23:31:11 -080080static bool genArithOpFloatPortable(CompilationUnit *cUnit, MIR *mir,
81 RegLocation rlDest, RegLocation rlSrc1,
82 RegLocation rlSrc2)
83{
84 RegLocation rlResult;
85 void* funct;
86
Ben Cheng5d90c202009-11-22 23:31:11 -080087 switch (mir->dalvikInsn.opCode) {
88 case OP_ADD_FLOAT_2ADDR:
89 case OP_ADD_FLOAT:
90 funct = (void*) __aeabi_fadd;
91 break;
92 case OP_SUB_FLOAT_2ADDR:
93 case OP_SUB_FLOAT:
94 funct = (void*) __aeabi_fsub;
95 break;
96 case OP_DIV_FLOAT_2ADDR:
97 case OP_DIV_FLOAT:
98 funct = (void*) __aeabi_fdiv;
99 break;
100 case OP_MUL_FLOAT_2ADDR:
101 case OP_MUL_FLOAT:
102 funct = (void*) __aeabi_fmul;
103 break;
104 case OP_REM_FLOAT_2ADDR:
105 case OP_REM_FLOAT:
106 funct = (void*) fmodf;
107 break;
108 case OP_NEG_FLOAT: {
109 genNegFloat(cUnit, rlDest, rlSrc1);
110 return false;
111 }
112 default:
113 return true;
114 }
Bill Buzbeec6f10662010-02-09 11:16:15 -0800115 dvmCompilerFlushAllRegs(cUnit); /* Send everything to home location */
Ben Cheng5d90c202009-11-22 23:31:11 -0800116 loadValueDirectFixed(cUnit, rlSrc1, r0);
117 loadValueDirectFixed(cUnit, rlSrc2, r1);
Ben Chengbd1326d2010-04-02 15:04:53 -0700118 LOAD_FUNC_ADDR(cUnit, r2, (int)funct);
Ben Cheng5d90c202009-11-22 23:31:11 -0800119 opReg(cUnit, kOpBlx, r2);
Elliott Hughes6a555132010-02-25 15:41:42 -0800120 dvmCompilerClobberCallRegs(cUnit);
Bill Buzbeec6f10662010-02-09 11:16:15 -0800121 rlResult = dvmCompilerGetReturn(cUnit);
Ben Cheng5d90c202009-11-22 23:31:11 -0800122 storeValue(cUnit, rlDest, rlResult);
123 return false;
124}
125
126static bool genArithOpDoublePortable(CompilationUnit *cUnit, MIR *mir,
127 RegLocation rlDest, RegLocation rlSrc1,
128 RegLocation rlSrc2)
129{
130 RegLocation rlResult;
131 void* funct;
132
Ben Cheng5d90c202009-11-22 23:31:11 -0800133 switch (mir->dalvikInsn.opCode) {
134 case OP_ADD_DOUBLE_2ADDR:
135 case OP_ADD_DOUBLE:
136 funct = (void*) __aeabi_dadd;
137 break;
138 case OP_SUB_DOUBLE_2ADDR:
139 case OP_SUB_DOUBLE:
140 funct = (void*) __aeabi_dsub;
141 break;
142 case OP_DIV_DOUBLE_2ADDR:
143 case OP_DIV_DOUBLE:
144 funct = (void*) __aeabi_ddiv;
145 break;
146 case OP_MUL_DOUBLE_2ADDR:
147 case OP_MUL_DOUBLE:
148 funct = (void*) __aeabi_dmul;
149 break;
150 case OP_REM_DOUBLE_2ADDR:
151 case OP_REM_DOUBLE:
152 funct = (void*) fmod;
153 break;
154 case OP_NEG_DOUBLE: {
155 genNegDouble(cUnit, rlDest, rlSrc1);
156 return false;
157 }
158 default:
159 return true;
160 }
Bill Buzbeec6f10662010-02-09 11:16:15 -0800161 dvmCompilerFlushAllRegs(cUnit); /* Send everything to home location */
Ben Chengbd1326d2010-04-02 15:04:53 -0700162 LOAD_FUNC_ADDR(cUnit, rlr, (int)funct);
Ben Cheng5d90c202009-11-22 23:31:11 -0800163 loadValueDirectWideFixed(cUnit, rlSrc1, r0, r1);
164 loadValueDirectWideFixed(cUnit, rlSrc2, r2, r3);
165 opReg(cUnit, kOpBlx, rlr);
Elliott Hughes6a555132010-02-25 15:41:42 -0800166 dvmCompilerClobberCallRegs(cUnit);
Bill Buzbeec6f10662010-02-09 11:16:15 -0800167 rlResult = dvmCompilerGetReturnWide(cUnit);
Ben Cheng5d90c202009-11-22 23:31:11 -0800168 storeValueWide(cUnit, rlDest, rlResult);
169 return false;
170}
171
172static bool genConversionPortable(CompilationUnit *cUnit, MIR *mir)
173{
174 OpCode opCode = mir->dalvikInsn.opCode;
175
Ben Cheng5d90c202009-11-22 23:31:11 -0800176 switch (opCode) {
177 case OP_INT_TO_FLOAT:
178 return genConversionCall(cUnit, mir, (void*)__aeabi_i2f, 1, 1);
179 case OP_FLOAT_TO_INT:
180 return genConversionCall(cUnit, mir, (void*)__aeabi_f2iz, 1, 1);
181 case OP_DOUBLE_TO_FLOAT:
182 return genConversionCall(cUnit, mir, (void*)__aeabi_d2f, 2, 1);
183 case OP_FLOAT_TO_DOUBLE:
184 return genConversionCall(cUnit, mir, (void*)__aeabi_f2d, 1, 2);
185 case OP_INT_TO_DOUBLE:
186 return genConversionCall(cUnit, mir, (void*)__aeabi_i2d, 1, 2);
187 case OP_DOUBLE_TO_INT:
188 return genConversionCall(cUnit, mir, (void*)__aeabi_d2iz, 2, 1);
189 case OP_FLOAT_TO_LONG:
190 return genConversionCall(cUnit, mir, (void*)dvmJitf2l, 1, 2);
191 case OP_LONG_TO_FLOAT:
192 return genConversionCall(cUnit, mir, (void*)__aeabi_l2f, 2, 1);
193 case OP_DOUBLE_TO_LONG:
194 return genConversionCall(cUnit, mir, (void*)dvmJitd2l, 2, 2);
195 case OP_LONG_TO_DOUBLE:
196 return genConversionCall(cUnit, mir, (void*)__aeabi_l2d, 2, 2);
197 default:
198 return true;
199 }
200 return false;
201}
Ben Chengba4fc8b2009-06-01 13:00:29 -0700202
Jeff Hao97319a82009-08-12 16:57:15 -0700203#if defined(WITH_SELF_VERIFICATION)
jeffhao9e45c0b2010-02-03 10:24:05 -0800204static void selfVerificationBranchInsert(LIR *currentLIR, ArmOpCode opCode,
205 int dest, int src1)
Jeff Hao97319a82009-08-12 16:57:15 -0700206{
jeffhao9e45c0b2010-02-03 10:24:05 -0800207 ArmLIR *insn = dvmCompilerNew(sizeof(ArmLIR), true);
208 insn->opCode = opCode;
209 insn->operands[0] = dest;
210 insn->operands[1] = src1;
211 setupResourceMasks(insn);
212 dvmCompilerInsertLIRBefore(currentLIR, (LIR *) insn);
Jeff Hao97319a82009-08-12 16:57:15 -0700213}
214
jeffhao9e45c0b2010-02-03 10:24:05 -0800215static void selfVerificationBranchInsertPass(CompilationUnit *cUnit)
Jeff Hao97319a82009-08-12 16:57:15 -0700216{
jeffhao9e45c0b2010-02-03 10:24:05 -0800217 ArmLIR *thisLIR;
jeffhao9e45c0b2010-02-03 10:24:05 -0800218 TemplateOpCode opCode = TEMPLATE_MEM_OP_DECODE;
Jeff Hao97319a82009-08-12 16:57:15 -0700219
jeffhao9e45c0b2010-02-03 10:24:05 -0800220 for (thisLIR = (ArmLIR *) cUnit->firstLIRInsn;
221 thisLIR != (ArmLIR *) cUnit->lastLIRInsn;
222 thisLIR = NEXT_LIR(thisLIR)) {
223 if (thisLIR->branchInsertSV) {
224 /* Branch to mem op decode template */
225 selfVerificationBranchInsert((LIR *) thisLIR, kThumbBlx1,
226 (int) gDvmJit.codeCache + templateEntryOffsets[opCode],
227 (int) gDvmJit.codeCache + templateEntryOffsets[opCode]);
228 selfVerificationBranchInsert((LIR *) thisLIR, kThumbBlx2,
229 (int) gDvmJit.codeCache + templateEntryOffsets[opCode],
230 (int) gDvmJit.codeCache + templateEntryOffsets[opCode]);
Jeff Hao97319a82009-08-12 16:57:15 -0700231 }
232 }
Jeff Hao97319a82009-08-12 16:57:15 -0700233}
Jeff Hao97319a82009-08-12 16:57:15 -0700234#endif
235
Bill Buzbeebe6534f2010-03-12 16:01:35 -0800236/* Generate conditional branch instructions */
237static ArmLIR *genConditionalBranch(CompilationUnit *cUnit,
238 ArmConditionCode cond,
239 ArmLIR *target)
240{
241 ArmLIR *branch = opCondBranch(cUnit, cond);
242 branch->generic.target = (LIR *) target;
243 return branch;
244}
245
Ben Chengba4fc8b2009-06-01 13:00:29 -0700246/* Generate a unconditional branch to go to the interpreter */
Bill Buzbee89efc3d2009-07-28 11:22:22 -0700247static inline ArmLIR *genTrap(CompilationUnit *cUnit, int dOffset,
248 ArmLIR *pcrLabel)
Ben Chengba4fc8b2009-06-01 13:00:29 -0700249{
Bill Buzbee1465db52009-09-23 17:17:35 -0700250 ArmLIR *branch = opNone(cUnit, kOpUncondBr);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700251 return genCheckCommon(cUnit, dOffset, branch, pcrLabel);
252}
253
254/* Load a wide field from an object instance */
255static void genIGetWide(CompilationUnit *cUnit, MIR *mir, int fieldOffset)
256{
Bill Buzbeec6f10662010-02-09 11:16:15 -0800257 RegLocation rlObj = dvmCompilerGetSrc(cUnit, mir, 0);
258 RegLocation rlDest = dvmCompilerGetDestWide(cUnit, mir, 0, 1);
Bill Buzbee1465db52009-09-23 17:17:35 -0700259 RegLocation rlResult;
260 rlObj = loadValue(cUnit, rlObj, kCoreReg);
Bill Buzbeec6f10662010-02-09 11:16:15 -0800261 int regPtr = dvmCompilerAllocTemp(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700262
Bill Buzbee1465db52009-09-23 17:17:35 -0700263 assert(rlDest.wide);
Ben Chenge9695e52009-06-16 16:11:47 -0700264
Bill Buzbee1465db52009-09-23 17:17:35 -0700265 genNullCheck(cUnit, rlObj.sRegLow, rlObj.lowReg, mir->offset,
266 NULL);/* null object? */
267 opRegRegImm(cUnit, kOpAdd, regPtr, rlObj.lowReg, fieldOffset);
Bill Buzbeec6f10662010-02-09 11:16:15 -0800268 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kAnyReg, true);
Ben Cheng11d8f142010-03-24 15:24:19 -0700269
270 HEAP_ACCESS_SHADOW(true);
Bill Buzbee1465db52009-09-23 17:17:35 -0700271 loadPair(cUnit, regPtr, rlResult.lowReg, rlResult.highReg);
Ben Cheng11d8f142010-03-24 15:24:19 -0700272 HEAP_ACCESS_SHADOW(false);
273
Bill Buzbeec6f10662010-02-09 11:16:15 -0800274 dvmCompilerFreeTemp(cUnit, regPtr);
Bill Buzbee1465db52009-09-23 17:17:35 -0700275 storeValueWide(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700276}
277
278/* Store a wide field to an object instance */
279static void genIPutWide(CompilationUnit *cUnit, MIR *mir, int fieldOffset)
280{
Bill Buzbeec6f10662010-02-09 11:16:15 -0800281 RegLocation rlSrc = dvmCompilerGetSrcWide(cUnit, mir, 0, 1);
282 RegLocation rlObj = dvmCompilerGetSrc(cUnit, mir, 2);
Bill Buzbee1465db52009-09-23 17:17:35 -0700283 rlObj = loadValue(cUnit, rlObj, kCoreReg);
284 int regPtr;
285 rlSrc = loadValueWide(cUnit, rlSrc, kAnyReg);
286 genNullCheck(cUnit, rlObj.sRegLow, rlObj.lowReg, mir->offset,
287 NULL);/* null object? */
Bill Buzbeec6f10662010-02-09 11:16:15 -0800288 regPtr = dvmCompilerAllocTemp(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -0700289 opRegRegImm(cUnit, kOpAdd, regPtr, rlObj.lowReg, fieldOffset);
Ben Cheng11d8f142010-03-24 15:24:19 -0700290
291 HEAP_ACCESS_SHADOW(true);
Bill Buzbee1465db52009-09-23 17:17:35 -0700292 storePair(cUnit, regPtr, rlSrc.lowReg, rlSrc.highReg);
Ben Cheng11d8f142010-03-24 15:24:19 -0700293 HEAP_ACCESS_SHADOW(false);
294
Bill Buzbeec6f10662010-02-09 11:16:15 -0800295 dvmCompilerFreeTemp(cUnit, regPtr);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700296}
297
298/*
299 * Load a field from an object instance
300 *
Ben Chengba4fc8b2009-06-01 13:00:29 -0700301 */
Bill Buzbee270c1d62009-08-13 16:58:07 -0700302static void genIGet(CompilationUnit *cUnit, MIR *mir, OpSize size,
buzbeeecf8f6e2010-07-20 14:53:42 -0700303 int fieldOffset, bool isVolatile)
Ben Chengba4fc8b2009-06-01 13:00:29 -0700304{
Bill Buzbee1465db52009-09-23 17:17:35 -0700305 RegLocation rlResult;
Bill Buzbee749e8162010-07-07 06:55:56 -0700306 RegisterClass regClass = dvmCompilerRegClassBySize(size);
Bill Buzbeec6f10662010-02-09 11:16:15 -0800307 RegLocation rlObj = dvmCompilerGetSrc(cUnit, mir, 0);
308 RegLocation rlDest = dvmCompilerGetDest(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -0700309 rlObj = loadValue(cUnit, rlObj, kCoreReg);
Bill Buzbee749e8162010-07-07 06:55:56 -0700310 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, regClass, true);
Bill Buzbee1465db52009-09-23 17:17:35 -0700311 genNullCheck(cUnit, rlObj.sRegLow, rlObj.lowReg, mir->offset,
312 NULL);/* null object? */
Ben Cheng11d8f142010-03-24 15:24:19 -0700313
314 HEAP_ACCESS_SHADOW(true);
Ben Cheng5d90c202009-11-22 23:31:11 -0800315 loadBaseDisp(cUnit, mir, rlObj.lowReg, fieldOffset, rlResult.lowReg,
316 size, rlObj.sRegLow);
Ben Cheng11d8f142010-03-24 15:24:19 -0700317 HEAP_ACCESS_SHADOW(false);
buzbeeecf8f6e2010-07-20 14:53:42 -0700318 if (isVolatile) {
319 dvmCompilerGenMemBarrier(cUnit);
320 }
Ben Cheng11d8f142010-03-24 15:24:19 -0700321
Bill Buzbee1465db52009-09-23 17:17:35 -0700322 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700323}
324
325/*
326 * Store a field to an object instance
327 *
Ben Chengba4fc8b2009-06-01 13:00:29 -0700328 */
Bill Buzbee270c1d62009-08-13 16:58:07 -0700329static void genIPut(CompilationUnit *cUnit, MIR *mir, OpSize size,
buzbeeecf8f6e2010-07-20 14:53:42 -0700330 int fieldOffset, bool isObject, bool isVolatile)
Ben Chengba4fc8b2009-06-01 13:00:29 -0700331{
Bill Buzbee749e8162010-07-07 06:55:56 -0700332 RegisterClass regClass = dvmCompilerRegClassBySize(size);
Bill Buzbeec6f10662010-02-09 11:16:15 -0800333 RegLocation rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
334 RegLocation rlObj = dvmCompilerGetSrc(cUnit, mir, 1);
Bill Buzbee1465db52009-09-23 17:17:35 -0700335 rlObj = loadValue(cUnit, rlObj, kCoreReg);
Bill Buzbee749e8162010-07-07 06:55:56 -0700336 rlSrc = loadValue(cUnit, rlSrc, regClass);
Bill Buzbee1465db52009-09-23 17:17:35 -0700337 genNullCheck(cUnit, rlObj.sRegLow, rlObj.lowReg, mir->offset,
338 NULL);/* null object? */
Ben Cheng11d8f142010-03-24 15:24:19 -0700339
buzbeeecf8f6e2010-07-20 14:53:42 -0700340 if (isVolatile) {
341 dvmCompilerGenMemBarrier(cUnit);
342 }
Ben Cheng11d8f142010-03-24 15:24:19 -0700343 HEAP_ACCESS_SHADOW(true);
Bill Buzbee1465db52009-09-23 17:17:35 -0700344 storeBaseDisp(cUnit, rlObj.lowReg, fieldOffset, rlSrc.lowReg, size);
Ben Cheng11d8f142010-03-24 15:24:19 -0700345 HEAP_ACCESS_SHADOW(false);
buzbee919eb062010-07-12 12:59:22 -0700346 if (isObject) {
347 /* NOTE: marking card based on object head */
348 markCard(cUnit, rlSrc.lowReg, rlObj.lowReg);
349 }
Ben Chengba4fc8b2009-06-01 13:00:29 -0700350}
351
352
Ben Chengba4fc8b2009-06-01 13:00:29 -0700353/*
354 * Generate array load
Ben Chengba4fc8b2009-06-01 13:00:29 -0700355 */
Bill Buzbee270c1d62009-08-13 16:58:07 -0700356static void genArrayGet(CompilationUnit *cUnit, MIR *mir, OpSize size,
Bill Buzbee1465db52009-09-23 17:17:35 -0700357 RegLocation rlArray, RegLocation rlIndex,
358 RegLocation rlDest, int scale)
Ben Chengba4fc8b2009-06-01 13:00:29 -0700359{
Bill Buzbee749e8162010-07-07 06:55:56 -0700360 RegisterClass regClass = dvmCompilerRegClassBySize(size);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700361 int lenOffset = offsetof(ArrayObject, length);
362 int dataOffset = offsetof(ArrayObject, contents);
Bill Buzbee1465db52009-09-23 17:17:35 -0700363 RegLocation rlResult;
364 rlArray = loadValue(cUnit, rlArray, kCoreReg);
365 rlIndex = loadValue(cUnit, rlIndex, kCoreReg);
366 int regPtr;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700367
368 /* null object? */
Ben Cheng4238ec22009-08-24 16:32:22 -0700369 ArmLIR * pcrLabel = NULL;
370
371 if (!(mir->OptimizationFlags & MIR_IGNORE_NULL_CHECK)) {
Bill Buzbee1465db52009-09-23 17:17:35 -0700372 pcrLabel = genNullCheck(cUnit, rlArray.sRegLow,
373 rlArray.lowReg, mir->offset, NULL);
Ben Cheng4238ec22009-08-24 16:32:22 -0700374 }
375
Bill Buzbeec6f10662010-02-09 11:16:15 -0800376 regPtr = dvmCompilerAllocTemp(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -0700377
Ben Cheng4238ec22009-08-24 16:32:22 -0700378 if (!(mir->OptimizationFlags & MIR_IGNORE_RANGE_CHECK)) {
Bill Buzbeec6f10662010-02-09 11:16:15 -0800379 int regLen = dvmCompilerAllocTemp(cUnit);
Ben Cheng4238ec22009-08-24 16:32:22 -0700380 /* Get len */
Bill Buzbee1465db52009-09-23 17:17:35 -0700381 loadWordDisp(cUnit, rlArray.lowReg, lenOffset, regLen);
382 /* regPtr -> array data */
383 opRegRegImm(cUnit, kOpAdd, regPtr, rlArray.lowReg, dataOffset);
384 genBoundsCheck(cUnit, rlIndex.lowReg, regLen, mir->offset,
385 pcrLabel);
Bill Buzbeec6f10662010-02-09 11:16:15 -0800386 dvmCompilerFreeTemp(cUnit, regLen);
Ben Cheng4238ec22009-08-24 16:32:22 -0700387 } else {
Bill Buzbee1465db52009-09-23 17:17:35 -0700388 /* regPtr -> array data */
389 opRegRegImm(cUnit, kOpAdd, regPtr, rlArray.lowReg, dataOffset);
Ben Cheng4238ec22009-08-24 16:32:22 -0700390 }
Bill Buzbee1465db52009-09-23 17:17:35 -0700391 if ((size == kLong) || (size == kDouble)) {
392 if (scale) {
Bill Buzbeec6f10662010-02-09 11:16:15 -0800393 int rNewIndex = dvmCompilerAllocTemp(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -0700394 opRegRegImm(cUnit, kOpLsl, rNewIndex, rlIndex.lowReg, scale);
395 opRegReg(cUnit, kOpAdd, regPtr, rNewIndex);
Bill Buzbeec6f10662010-02-09 11:16:15 -0800396 dvmCompilerFreeTemp(cUnit, rNewIndex);
Bill Buzbee1465db52009-09-23 17:17:35 -0700397 } else {
398 opRegReg(cUnit, kOpAdd, regPtr, rlIndex.lowReg);
399 }
Bill Buzbee749e8162010-07-07 06:55:56 -0700400 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, regClass, true);
Ben Cheng11d8f142010-03-24 15:24:19 -0700401
402 HEAP_ACCESS_SHADOW(true);
Bill Buzbee1465db52009-09-23 17:17:35 -0700403 loadPair(cUnit, regPtr, rlResult.lowReg, rlResult.highReg);
Ben Cheng11d8f142010-03-24 15:24:19 -0700404 HEAP_ACCESS_SHADOW(false);
405
Bill Buzbeec6f10662010-02-09 11:16:15 -0800406 dvmCompilerFreeTemp(cUnit, regPtr);
Bill Buzbee1465db52009-09-23 17:17:35 -0700407 storeValueWide(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700408 } else {
Bill Buzbee749e8162010-07-07 06:55:56 -0700409 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, regClass, true);
Ben Cheng11d8f142010-03-24 15:24:19 -0700410
411 HEAP_ACCESS_SHADOW(true);
Bill Buzbee1465db52009-09-23 17:17:35 -0700412 loadBaseIndexed(cUnit, regPtr, rlIndex.lowReg, rlResult.lowReg,
413 scale, size);
Ben Cheng11d8f142010-03-24 15:24:19 -0700414 HEAP_ACCESS_SHADOW(false);
415
Bill Buzbeec6f10662010-02-09 11:16:15 -0800416 dvmCompilerFreeTemp(cUnit, regPtr);
Bill Buzbee1465db52009-09-23 17:17:35 -0700417 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700418 }
419}
420
Ben Chengba4fc8b2009-06-01 13:00:29 -0700421/*
422 * Generate array store
423 *
Ben Chengba4fc8b2009-06-01 13:00:29 -0700424 */
Bill Buzbee270c1d62009-08-13 16:58:07 -0700425static void genArrayPut(CompilationUnit *cUnit, MIR *mir, OpSize size,
Bill Buzbee1465db52009-09-23 17:17:35 -0700426 RegLocation rlArray, RegLocation rlIndex,
427 RegLocation rlSrc, int scale)
Ben Chengba4fc8b2009-06-01 13:00:29 -0700428{
Bill Buzbee749e8162010-07-07 06:55:56 -0700429 RegisterClass regClass = dvmCompilerRegClassBySize(size);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700430 int lenOffset = offsetof(ArrayObject, length);
431 int dataOffset = offsetof(ArrayObject, contents);
432
Bill Buzbee1465db52009-09-23 17:17:35 -0700433 int regPtr;
434 rlArray = loadValue(cUnit, rlArray, kCoreReg);
435 rlIndex = loadValue(cUnit, rlIndex, kCoreReg);
Ben Chenge9695e52009-06-16 16:11:47 -0700436
Bill Buzbeec6f10662010-02-09 11:16:15 -0800437 if (dvmCompilerIsTemp(cUnit, rlArray.lowReg)) {
438 dvmCompilerClobber(cUnit, rlArray.lowReg);
Bill Buzbee1465db52009-09-23 17:17:35 -0700439 regPtr = rlArray.lowReg;
440 } else {
Bill Buzbeec6f10662010-02-09 11:16:15 -0800441 regPtr = dvmCompilerAllocTemp(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -0700442 genRegCopy(cUnit, regPtr, rlArray.lowReg);
443 }
Ben Chenge9695e52009-06-16 16:11:47 -0700444
Ben Cheng1efc9c52009-06-08 18:25:27 -0700445 /* null object? */
Ben Cheng4238ec22009-08-24 16:32:22 -0700446 ArmLIR * pcrLabel = NULL;
447
448 if (!(mir->OptimizationFlags & MIR_IGNORE_NULL_CHECK)) {
Bill Buzbee1465db52009-09-23 17:17:35 -0700449 pcrLabel = genNullCheck(cUnit, rlArray.sRegLow, rlArray.lowReg,
450 mir->offset, NULL);
Ben Cheng4238ec22009-08-24 16:32:22 -0700451 }
452
453 if (!(mir->OptimizationFlags & MIR_IGNORE_RANGE_CHECK)) {
Bill Buzbeec6f10662010-02-09 11:16:15 -0800454 int regLen = dvmCompilerAllocTemp(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -0700455 //NOTE: max live temps(4) here.
Ben Cheng4238ec22009-08-24 16:32:22 -0700456 /* Get len */
Bill Buzbee1465db52009-09-23 17:17:35 -0700457 loadWordDisp(cUnit, rlArray.lowReg, lenOffset, regLen);
458 /* regPtr -> array data */
459 opRegImm(cUnit, kOpAdd, regPtr, dataOffset);
460 genBoundsCheck(cUnit, rlIndex.lowReg, regLen, mir->offset,
461 pcrLabel);
Bill Buzbeec6f10662010-02-09 11:16:15 -0800462 dvmCompilerFreeTemp(cUnit, regLen);
Ben Cheng4238ec22009-08-24 16:32:22 -0700463 } else {
Bill Buzbee1465db52009-09-23 17:17:35 -0700464 /* regPtr -> array data */
465 opRegImm(cUnit, kOpAdd, regPtr, dataOffset);
Ben Cheng4238ec22009-08-24 16:32:22 -0700466 }
Bill Buzbee1465db52009-09-23 17:17:35 -0700467 /* at this point, regPtr points to array, 2 live temps */
Bill Buzbee1465db52009-09-23 17:17:35 -0700468 if ((size == kLong) || (size == kDouble)) {
469 //TODO: need specific wide routine that can handle fp regs
470 if (scale) {
Bill Buzbeec6f10662010-02-09 11:16:15 -0800471 int rNewIndex = dvmCompilerAllocTemp(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -0700472 opRegRegImm(cUnit, kOpLsl, rNewIndex, rlIndex.lowReg, scale);
473 opRegReg(cUnit, kOpAdd, regPtr, rNewIndex);
Bill Buzbeec6f10662010-02-09 11:16:15 -0800474 dvmCompilerFreeTemp(cUnit, rNewIndex);
Bill Buzbee1465db52009-09-23 17:17:35 -0700475 } else {
476 opRegReg(cUnit, kOpAdd, regPtr, rlIndex.lowReg);
477 }
Bill Buzbee749e8162010-07-07 06:55:56 -0700478 rlSrc = loadValueWide(cUnit, rlSrc, regClass);
Ben Cheng11d8f142010-03-24 15:24:19 -0700479
480 HEAP_ACCESS_SHADOW(true);
Bill Buzbee1465db52009-09-23 17:17:35 -0700481 storePair(cUnit, regPtr, rlSrc.lowReg, rlSrc.highReg);
Ben Cheng11d8f142010-03-24 15:24:19 -0700482 HEAP_ACCESS_SHADOW(false);
483
Bill Buzbeec6f10662010-02-09 11:16:15 -0800484 dvmCompilerFreeTemp(cUnit, regPtr);
Bill Buzbee270c1d62009-08-13 16:58:07 -0700485 } else {
Bill Buzbee749e8162010-07-07 06:55:56 -0700486 rlSrc = loadValue(cUnit, rlSrc, regClass);
Ben Cheng11d8f142010-03-24 15:24:19 -0700487
488 HEAP_ACCESS_SHADOW(true);
Bill Buzbee1465db52009-09-23 17:17:35 -0700489 storeBaseIndexed(cUnit, regPtr, rlIndex.lowReg, rlSrc.lowReg,
490 scale, size);
Ben Cheng11d8f142010-03-24 15:24:19 -0700491 HEAP_ACCESS_SHADOW(false);
jeffhao9e45c0b2010-02-03 10:24:05 -0800492 }
Ben Chengba4fc8b2009-06-01 13:00:29 -0700493}
494
Bill Buzbeebe6534f2010-03-12 16:01:35 -0800495/*
496 * Generate array object store
497 * Must use explicit register allocation here because of
498 * call-out to dvmCanPutArrayElement
499 */
500static void genArrayObjectPut(CompilationUnit *cUnit, MIR *mir,
501 RegLocation rlArray, RegLocation rlIndex,
502 RegLocation rlSrc, int scale)
503{
504 int lenOffset = offsetof(ArrayObject, length);
505 int dataOffset = offsetof(ArrayObject, contents);
506
507 dvmCompilerFlushAllRegs(cUnit);
508
509 int regLen = r0;
510 int regPtr = r4PC; /* Preserved across call */
511 int regArray = r1;
512 int regIndex = r7; /* Preserved across call */
513
514 loadValueDirectFixed(cUnit, rlArray, regArray);
515 loadValueDirectFixed(cUnit, rlIndex, regIndex);
516
517 /* null object? */
518 ArmLIR * pcrLabel = NULL;
519
520 if (!(mir->OptimizationFlags & MIR_IGNORE_NULL_CHECK)) {
521 pcrLabel = genNullCheck(cUnit, rlArray.sRegLow, regArray,
522 mir->offset, NULL);
523 }
524
525 if (!(mir->OptimizationFlags & MIR_IGNORE_RANGE_CHECK)) {
526 /* Get len */
527 loadWordDisp(cUnit, regArray, lenOffset, regLen);
528 /* regPtr -> array data */
529 opRegRegImm(cUnit, kOpAdd, regPtr, regArray, dataOffset);
530 genBoundsCheck(cUnit, regIndex, regLen, mir->offset,
531 pcrLabel);
532 } else {
533 /* regPtr -> array data */
534 opRegRegImm(cUnit, kOpAdd, regPtr, regArray, dataOffset);
535 }
536
537 /* Get object to store */
538 loadValueDirectFixed(cUnit, rlSrc, r0);
Ben Chengbd1326d2010-04-02 15:04:53 -0700539 LOAD_FUNC_ADDR(cUnit, r2, (int)dvmCanPutArrayElement);
Bill Buzbeebe6534f2010-03-12 16:01:35 -0800540
541 /* Are we storing null? If so, avoid check */
542 opRegImm(cUnit, kOpCmp, r0, 0);
543 ArmLIR *branchOver = opCondBranch(cUnit, kArmCondEq);
544
545 /* Make sure the types are compatible */
546 loadWordDisp(cUnit, regArray, offsetof(Object, clazz), r1);
547 loadWordDisp(cUnit, r0, offsetof(Object, clazz), r0);
548 opReg(cUnit, kOpBlx, r2);
549 dvmCompilerClobberCallRegs(cUnit);
Bill Buzbee900a3af2010-03-16 12:41:43 -0700550
551 /*
552 * Using fixed registers here, and counting on r4 and r7 being
553 * preserved across the above call. Tell the register allocation
554 * utilities about the regs we are using directly
555 */
556 dvmCompilerLockTemp(cUnit, regPtr); // r4PC
557 dvmCompilerLockTemp(cUnit, regIndex); // r7
558 dvmCompilerLockTemp(cUnit, r0);
buzbee919eb062010-07-12 12:59:22 -0700559 dvmCompilerLockTemp(cUnit, r1);
Bill Buzbee900a3af2010-03-16 12:41:43 -0700560
Bill Buzbeebe6534f2010-03-12 16:01:35 -0800561 /* Bad? - roll back and re-execute if so */
562 genRegImmCheck(cUnit, kArmCondEq, r0, 0, mir->offset, pcrLabel);
563
buzbee919eb062010-07-12 12:59:22 -0700564 /* Resume here - must reload element & array, regPtr & index preserved */
Bill Buzbeebe6534f2010-03-12 16:01:35 -0800565 loadValueDirectFixed(cUnit, rlSrc, r0);
buzbee919eb062010-07-12 12:59:22 -0700566 loadValueDirectFixed(cUnit, rlArray, r1);
Bill Buzbeebe6534f2010-03-12 16:01:35 -0800567
568 ArmLIR *target = newLIR0(cUnit, kArmPseudoTargetLabel);
569 target->defMask = ENCODE_ALL;
570 branchOver->generic.target = (LIR *) target;
571
Ben Cheng11d8f142010-03-24 15:24:19 -0700572 HEAP_ACCESS_SHADOW(true);
Bill Buzbeebe6534f2010-03-12 16:01:35 -0800573 storeBaseIndexed(cUnit, regPtr, regIndex, r0,
574 scale, kWord);
Ben Cheng11d8f142010-03-24 15:24:19 -0700575 HEAP_ACCESS_SHADOW(false);
buzbee919eb062010-07-12 12:59:22 -0700576
577 /* NOTE: marking card here based on object head */
578 markCard(cUnit, r0, r1);
Bill Buzbeebe6534f2010-03-12 16:01:35 -0800579}
580
Ben Cheng5d90c202009-11-22 23:31:11 -0800581static bool genShiftOpLong(CompilationUnit *cUnit, MIR *mir,
582 RegLocation rlDest, RegLocation rlSrc1,
583 RegLocation rlShift)
Ben Chengba4fc8b2009-06-01 13:00:29 -0700584{
Ben Chenge9695e52009-06-16 16:11:47 -0700585 /*
586 * Don't mess with the regsiters here as there is a particular calling
587 * convention to the out-of-line handler.
588 */
Bill Buzbee1465db52009-09-23 17:17:35 -0700589 RegLocation rlResult;
590
591 loadValueDirectWideFixed(cUnit, rlSrc1, r0, r1);
592 loadValueDirect(cUnit, rlShift, r2);
Ben Chenge9695e52009-06-16 16:11:47 -0700593 switch( mir->dalvikInsn.opCode) {
594 case OP_SHL_LONG:
595 case OP_SHL_LONG_2ADDR:
596 genDispatchToHandler(cUnit, TEMPLATE_SHL_LONG);
597 break;
598 case OP_SHR_LONG:
599 case OP_SHR_LONG_2ADDR:
600 genDispatchToHandler(cUnit, TEMPLATE_SHR_LONG);
601 break;
602 case OP_USHR_LONG:
603 case OP_USHR_LONG_2ADDR:
604 genDispatchToHandler(cUnit, TEMPLATE_USHR_LONG);
605 break;
606 default:
607 return true;
608 }
Bill Buzbeec6f10662010-02-09 11:16:15 -0800609 rlResult = dvmCompilerGetReturnWide(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -0700610 storeValueWide(cUnit, rlDest, rlResult);
Ben Chenge9695e52009-06-16 16:11:47 -0700611 return false;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700612}
Ben Chenge9695e52009-06-16 16:11:47 -0700613
Ben Cheng5d90c202009-11-22 23:31:11 -0800614static bool genArithOpLong(CompilationUnit *cUnit, MIR *mir,
615 RegLocation rlDest, RegLocation rlSrc1,
616 RegLocation rlSrc2)
Ben Chengba4fc8b2009-06-01 13:00:29 -0700617{
Bill Buzbee1465db52009-09-23 17:17:35 -0700618 RegLocation rlResult;
619 OpKind firstOp = kOpBkpt;
620 OpKind secondOp = kOpBkpt;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700621 bool callOut = false;
622 void *callTgt;
623 int retReg = r0;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700624
625 switch (mir->dalvikInsn.opCode) {
626 case OP_NOT_LONG:
Bill Buzbee1465db52009-09-23 17:17:35 -0700627 rlSrc2 = loadValueWide(cUnit, rlSrc2, kCoreReg);
Bill Buzbeec6f10662010-02-09 11:16:15 -0800628 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Bill Buzbee1465db52009-09-23 17:17:35 -0700629 opRegReg(cUnit, kOpMvn, rlResult.lowReg, rlSrc2.lowReg);
630 opRegReg(cUnit, kOpMvn, rlResult.highReg, rlSrc2.highReg);
631 storeValueWide(cUnit, rlDest, rlResult);
632 return false;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700633 break;
634 case OP_ADD_LONG:
635 case OP_ADD_LONG_2ADDR:
Bill Buzbee1465db52009-09-23 17:17:35 -0700636 firstOp = kOpAdd;
637 secondOp = kOpAdc;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700638 break;
639 case OP_SUB_LONG:
640 case OP_SUB_LONG_2ADDR:
Bill Buzbee1465db52009-09-23 17:17:35 -0700641 firstOp = kOpSub;
642 secondOp = kOpSbc;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700643 break;
644 case OP_MUL_LONG:
645 case OP_MUL_LONG_2ADDR:
Bill Buzbee1465db52009-09-23 17:17:35 -0700646 genMulLong(cUnit, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700647 return false;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700648 case OP_DIV_LONG:
649 case OP_DIV_LONG_2ADDR:
650 callOut = true;
651 retReg = r0;
652 callTgt = (void*)__aeabi_ldivmod;
653 break;
654 /* NOTE - result is in r2/r3 instead of r0/r1 */
655 case OP_REM_LONG:
656 case OP_REM_LONG_2ADDR:
657 callOut = true;
658 callTgt = (void*)__aeabi_ldivmod;
659 retReg = r2;
660 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700661 case OP_AND_LONG_2ADDR:
Bill Buzbee1465db52009-09-23 17:17:35 -0700662 case OP_AND_LONG:
663 firstOp = kOpAnd;
664 secondOp = kOpAnd;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700665 break;
666 case OP_OR_LONG:
667 case OP_OR_LONG_2ADDR:
Bill Buzbee1465db52009-09-23 17:17:35 -0700668 firstOp = kOpOr;
669 secondOp = kOpOr;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700670 break;
671 case OP_XOR_LONG:
672 case OP_XOR_LONG_2ADDR:
Bill Buzbee1465db52009-09-23 17:17:35 -0700673 firstOp = kOpXor;
674 secondOp = kOpXor;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700675 break;
Ben Chenge9695e52009-06-16 16:11:47 -0700676 case OP_NEG_LONG: {
Bill Buzbee51ecf602010-01-14 14:27:52 -0800677 //TUNING: can improve this using Thumb2 code
Bill Buzbeec6f10662010-02-09 11:16:15 -0800678 int tReg = dvmCompilerAllocTemp(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -0700679 rlSrc2 = loadValueWide(cUnit, rlSrc2, kCoreReg);
Bill Buzbeec6f10662010-02-09 11:16:15 -0800680 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Ben Chengbd1326d2010-04-02 15:04:53 -0700681 loadConstantNoClobber(cUnit, tReg, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -0700682 opRegRegReg(cUnit, kOpSub, rlResult.lowReg,
Bill Buzbee51ecf602010-01-14 14:27:52 -0800683 tReg, rlSrc2.lowReg);
684 opRegReg(cUnit, kOpSbc, tReg, rlSrc2.highReg);
685 genRegCopy(cUnit, rlResult.highReg, tReg);
Bill Buzbee1465db52009-09-23 17:17:35 -0700686 storeValueWide(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700687 return false;
Ben Chenge9695e52009-06-16 16:11:47 -0700688 }
Ben Chengba4fc8b2009-06-01 13:00:29 -0700689 default:
690 LOGE("Invalid long arith op");
Bill Buzbeefc519dc2010-03-06 23:30:57 -0800691 dvmCompilerAbort(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700692 }
693 if (!callOut) {
Bill Buzbee80cef862010-03-25 10:38:34 -0700694 genLong3Addr(cUnit, mir, firstOp, secondOp, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700695 } else {
Bill Buzbee1465db52009-09-23 17:17:35 -0700696 // Adjust return regs in to handle case of rem returning r2/r3
Bill Buzbeec6f10662010-02-09 11:16:15 -0800697 dvmCompilerFlushAllRegs(cUnit); /* Send everything to home location */
Bill Buzbee1465db52009-09-23 17:17:35 -0700698 loadValueDirectWideFixed(cUnit, rlSrc1, r0, r1);
Ben Chengbd1326d2010-04-02 15:04:53 -0700699 LOAD_FUNC_ADDR(cUnit, rlr, (int) callTgt);
Bill Buzbee1465db52009-09-23 17:17:35 -0700700 loadValueDirectWideFixed(cUnit, rlSrc2, r2, r3);
701 opReg(cUnit, kOpBlx, rlr);
Elliott Hughes6a555132010-02-25 15:41:42 -0800702 dvmCompilerClobberCallRegs(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -0700703 if (retReg == r0)
Bill Buzbeec6f10662010-02-09 11:16:15 -0800704 rlResult = dvmCompilerGetReturnWide(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -0700705 else
Bill Buzbeec6f10662010-02-09 11:16:15 -0800706 rlResult = dvmCompilerGetReturnWideAlt(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -0700707 storeValueWide(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700708 }
709 return false;
710}
711
Ben Cheng5d90c202009-11-22 23:31:11 -0800712static bool genArithOpInt(CompilationUnit *cUnit, MIR *mir,
713 RegLocation rlDest, RegLocation rlSrc1,
714 RegLocation rlSrc2)
Ben Chengba4fc8b2009-06-01 13:00:29 -0700715{
Bill Buzbee1465db52009-09-23 17:17:35 -0700716 OpKind op = kOpBkpt;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700717 bool callOut = false;
718 bool checkZero = false;
Bill Buzbee1465db52009-09-23 17:17:35 -0700719 bool unary = false;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700720 int retReg = r0;
721 void *callTgt;
Bill Buzbee1465db52009-09-23 17:17:35 -0700722 RegLocation rlResult;
Bill Buzbee0e605272009-12-01 14:28:05 -0800723 bool shiftOp = false;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700724
Ben Chengba4fc8b2009-06-01 13:00:29 -0700725 switch (mir->dalvikInsn.opCode) {
726 case OP_NEG_INT:
Bill Buzbee1465db52009-09-23 17:17:35 -0700727 op = kOpNeg;
728 unary = true;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700729 break;
730 case OP_NOT_INT:
Bill Buzbee1465db52009-09-23 17:17:35 -0700731 op = kOpMvn;
732 unary = true;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700733 break;
734 case OP_ADD_INT:
735 case OP_ADD_INT_2ADDR:
Bill Buzbee1465db52009-09-23 17:17:35 -0700736 op = kOpAdd;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700737 break;
738 case OP_SUB_INT:
739 case OP_SUB_INT_2ADDR:
Bill Buzbee1465db52009-09-23 17:17:35 -0700740 op = kOpSub;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700741 break;
742 case OP_MUL_INT:
743 case OP_MUL_INT_2ADDR:
Bill Buzbee1465db52009-09-23 17:17:35 -0700744 op = kOpMul;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700745 break;
746 case OP_DIV_INT:
747 case OP_DIV_INT_2ADDR:
748 callOut = true;
749 checkZero = true;
750 callTgt = __aeabi_idiv;
751 retReg = r0;
752 break;
753 /* NOTE: returns in r1 */
754 case OP_REM_INT:
755 case OP_REM_INT_2ADDR:
756 callOut = true;
757 checkZero = true;
758 callTgt = __aeabi_idivmod;
759 retReg = r1;
760 break;
761 case OP_AND_INT:
762 case OP_AND_INT_2ADDR:
Bill Buzbee1465db52009-09-23 17:17:35 -0700763 op = kOpAnd;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700764 break;
765 case OP_OR_INT:
766 case OP_OR_INT_2ADDR:
Bill Buzbee1465db52009-09-23 17:17:35 -0700767 op = kOpOr;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700768 break;
769 case OP_XOR_INT:
770 case OP_XOR_INT_2ADDR:
Bill Buzbee1465db52009-09-23 17:17:35 -0700771 op = kOpXor;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700772 break;
773 case OP_SHL_INT:
774 case OP_SHL_INT_2ADDR:
Bill Buzbee0e605272009-12-01 14:28:05 -0800775 shiftOp = true;
Bill Buzbee1465db52009-09-23 17:17:35 -0700776 op = kOpLsl;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700777 break;
778 case OP_SHR_INT:
779 case OP_SHR_INT_2ADDR:
Bill Buzbee0e605272009-12-01 14:28:05 -0800780 shiftOp = true;
Bill Buzbee1465db52009-09-23 17:17:35 -0700781 op = kOpAsr;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700782 break;
783 case OP_USHR_INT:
784 case OP_USHR_INT_2ADDR:
Bill Buzbee0e605272009-12-01 14:28:05 -0800785 shiftOp = true;
Bill Buzbee1465db52009-09-23 17:17:35 -0700786 op = kOpLsr;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700787 break;
788 default:
789 LOGE("Invalid word arith op: 0x%x(%d)",
790 mir->dalvikInsn.opCode, mir->dalvikInsn.opCode);
Bill Buzbeefc519dc2010-03-06 23:30:57 -0800791 dvmCompilerAbort(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700792 }
793 if (!callOut) {
Bill Buzbee1465db52009-09-23 17:17:35 -0700794 rlSrc1 = loadValue(cUnit, rlSrc1, kCoreReg);
795 if (unary) {
Bill Buzbeec6f10662010-02-09 11:16:15 -0800796 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Bill Buzbee1465db52009-09-23 17:17:35 -0700797 opRegReg(cUnit, op, rlResult.lowReg,
798 rlSrc1.lowReg);
Ben Chenge9695e52009-06-16 16:11:47 -0700799 } else {
Bill Buzbee1465db52009-09-23 17:17:35 -0700800 rlSrc2 = loadValue(cUnit, rlSrc2, kCoreReg);
Bill Buzbee0e605272009-12-01 14:28:05 -0800801 if (shiftOp) {
Bill Buzbeec6f10662010-02-09 11:16:15 -0800802 int tReg = dvmCompilerAllocTemp(cUnit);
Bill Buzbee0e605272009-12-01 14:28:05 -0800803 opRegRegImm(cUnit, kOpAnd, tReg, rlSrc2.lowReg, 31);
Bill Buzbeec6f10662010-02-09 11:16:15 -0800804 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Bill Buzbee0e605272009-12-01 14:28:05 -0800805 opRegRegReg(cUnit, op, rlResult.lowReg,
806 rlSrc1.lowReg, tReg);
Bill Buzbeec6f10662010-02-09 11:16:15 -0800807 dvmCompilerFreeTemp(cUnit, tReg);
Bill Buzbee0e605272009-12-01 14:28:05 -0800808 } else {
Bill Buzbeec6f10662010-02-09 11:16:15 -0800809 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Bill Buzbee0e605272009-12-01 14:28:05 -0800810 opRegRegReg(cUnit, op, rlResult.lowReg,
811 rlSrc1.lowReg, rlSrc2.lowReg);
812 }
Ben Chenge9695e52009-06-16 16:11:47 -0700813 }
Bill Buzbee1465db52009-09-23 17:17:35 -0700814 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700815 } else {
Bill Buzbee1465db52009-09-23 17:17:35 -0700816 RegLocation rlResult;
Bill Buzbeec6f10662010-02-09 11:16:15 -0800817 dvmCompilerFlushAllRegs(cUnit); /* Send everything to home location */
Bill Buzbee1465db52009-09-23 17:17:35 -0700818 loadValueDirectFixed(cUnit, rlSrc2, r1);
Ben Chengbd1326d2010-04-02 15:04:53 -0700819 LOAD_FUNC_ADDR(cUnit, r2, (int) callTgt);
Bill Buzbee1465db52009-09-23 17:17:35 -0700820 loadValueDirectFixed(cUnit, rlSrc1, r0);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700821 if (checkZero) {
Bill Buzbee1465db52009-09-23 17:17:35 -0700822 genNullCheck(cUnit, rlSrc2.sRegLow, r1, mir->offset, NULL);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700823 }
Bill Buzbee1465db52009-09-23 17:17:35 -0700824 opReg(cUnit, kOpBlx, r2);
Elliott Hughes6a555132010-02-25 15:41:42 -0800825 dvmCompilerClobberCallRegs(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -0700826 if (retReg == r0)
Bill Buzbeec6f10662010-02-09 11:16:15 -0800827 rlResult = dvmCompilerGetReturn(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -0700828 else
Bill Buzbeec6f10662010-02-09 11:16:15 -0800829 rlResult = dvmCompilerGetReturnAlt(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -0700830 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700831 }
832 return false;
833}
834
Ben Cheng5d90c202009-11-22 23:31:11 -0800835static bool genArithOp(CompilationUnit *cUnit, MIR *mir)
Ben Chengba4fc8b2009-06-01 13:00:29 -0700836{
837 OpCode opCode = mir->dalvikInsn.opCode;
Bill Buzbee1465db52009-09-23 17:17:35 -0700838 RegLocation rlDest;
839 RegLocation rlSrc1;
840 RegLocation rlSrc2;
841 /* Deduce sizes of operands */
842 if (mir->ssaRep->numUses == 2) {
Bill Buzbeec6f10662010-02-09 11:16:15 -0800843 rlSrc1 = dvmCompilerGetSrc(cUnit, mir, 0);
844 rlSrc2 = dvmCompilerGetSrc(cUnit, mir, 1);
Bill Buzbee1465db52009-09-23 17:17:35 -0700845 } else if (mir->ssaRep->numUses == 3) {
Bill Buzbeec6f10662010-02-09 11:16:15 -0800846 rlSrc1 = dvmCompilerGetSrcWide(cUnit, mir, 0, 1);
847 rlSrc2 = dvmCompilerGetSrc(cUnit, mir, 2);
Bill Buzbee1465db52009-09-23 17:17:35 -0700848 } else {
Bill Buzbeec6f10662010-02-09 11:16:15 -0800849 rlSrc1 = dvmCompilerGetSrcWide(cUnit, mir, 0, 1);
850 rlSrc2 = dvmCompilerGetSrcWide(cUnit, mir, 2, 3);
Bill Buzbee1465db52009-09-23 17:17:35 -0700851 assert(mir->ssaRep->numUses == 4);
852 }
853 if (mir->ssaRep->numDefs == 1) {
Bill Buzbeec6f10662010-02-09 11:16:15 -0800854 rlDest = dvmCompilerGetDest(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -0700855 } else {
856 assert(mir->ssaRep->numDefs == 2);
Bill Buzbeec6f10662010-02-09 11:16:15 -0800857 rlDest = dvmCompilerGetDestWide(cUnit, mir, 0, 1);
Bill Buzbee1465db52009-09-23 17:17:35 -0700858 }
Ben Chengba4fc8b2009-06-01 13:00:29 -0700859
860 if ((opCode >= OP_ADD_LONG_2ADDR) && (opCode <= OP_XOR_LONG_2ADDR)) {
Ben Cheng5d90c202009-11-22 23:31:11 -0800861 return genArithOpLong(cUnit,mir, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700862 }
863 if ((opCode >= OP_ADD_LONG) && (opCode <= OP_XOR_LONG)) {
Ben Cheng5d90c202009-11-22 23:31:11 -0800864 return genArithOpLong(cUnit,mir, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700865 }
866 if ((opCode >= OP_SHL_LONG_2ADDR) && (opCode <= OP_USHR_LONG_2ADDR)) {
Ben Cheng5d90c202009-11-22 23:31:11 -0800867 return genShiftOpLong(cUnit,mir, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700868 }
869 if ((opCode >= OP_SHL_LONG) && (opCode <= OP_USHR_LONG)) {
Ben Cheng5d90c202009-11-22 23:31:11 -0800870 return genShiftOpLong(cUnit,mir, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700871 }
872 if ((opCode >= OP_ADD_INT_2ADDR) && (opCode <= OP_USHR_INT_2ADDR)) {
Ben Cheng5d90c202009-11-22 23:31:11 -0800873 return genArithOpInt(cUnit,mir, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700874 }
875 if ((opCode >= OP_ADD_INT) && (opCode <= OP_USHR_INT)) {
Ben Cheng5d90c202009-11-22 23:31:11 -0800876 return genArithOpInt(cUnit,mir, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700877 }
878 if ((opCode >= OP_ADD_FLOAT_2ADDR) && (opCode <= OP_REM_FLOAT_2ADDR)) {
Ben Cheng5d90c202009-11-22 23:31:11 -0800879 return genArithOpFloat(cUnit,mir, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700880 }
881 if ((opCode >= OP_ADD_FLOAT) && (opCode <= OP_REM_FLOAT)) {
Ben Cheng5d90c202009-11-22 23:31:11 -0800882 return genArithOpFloat(cUnit, mir, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700883 }
884 if ((opCode >= OP_ADD_DOUBLE_2ADDR) && (opCode <= OP_REM_DOUBLE_2ADDR)) {
Ben Cheng5d90c202009-11-22 23:31:11 -0800885 return genArithOpDouble(cUnit,mir, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700886 }
887 if ((opCode >= OP_ADD_DOUBLE) && (opCode <= OP_REM_DOUBLE)) {
Ben Cheng5d90c202009-11-22 23:31:11 -0800888 return genArithOpDouble(cUnit,mir, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700889 }
890 return true;
891}
892
Bill Buzbee1465db52009-09-23 17:17:35 -0700893/* Generate unconditional branch instructions */
894static ArmLIR *genUnconditionalBranch(CompilationUnit *cUnit, ArmLIR *target)
895{
896 ArmLIR *branch = opNone(cUnit, kOpUncondBr);
897 branch->generic.target = (LIR *) target;
898 return branch;
899}
900
Bill Buzbee1465db52009-09-23 17:17:35 -0700901/* Perform the actual operation for OP_RETURN_* */
902static void genReturnCommon(CompilationUnit *cUnit, MIR *mir)
903{
904 genDispatchToHandler(cUnit, TEMPLATE_RETURN);
Ben Cheng978738d2010-05-13 13:45:57 -0700905#if defined(WITH_JIT_TUNING)
Bill Buzbee1465db52009-09-23 17:17:35 -0700906 gDvmJit.returnOp++;
907#endif
908 int dPC = (int) (cUnit->method->insns + mir->offset);
909 /* Insert branch, but defer setting of target */
910 ArmLIR *branch = genUnconditionalBranch(cUnit, NULL);
911 /* Set up the place holder to reconstruct this Dalvik PC */
912 ArmLIR *pcrLabel = dvmCompilerNew(sizeof(ArmLIR), true);
Ben Chenga4973592010-03-31 11:59:18 -0700913 pcrLabel->opCode = kArmPseudoPCReconstructionCell;
Bill Buzbee1465db52009-09-23 17:17:35 -0700914 pcrLabel->operands[0] = dPC;
915 pcrLabel->operands[1] = mir->offset;
916 /* Insert the place holder to the growable list */
917 dvmInsertGrowableList(&cUnit->pcReconstructionList, pcrLabel);
918 /* Branch to the PC reconstruction code */
919 branch->generic.target = (LIR *) pcrLabel;
920}
921
Ben Chengba4fc8b2009-06-01 13:00:29 -0700922static void genProcessArgsNoRange(CompilationUnit *cUnit, MIR *mir,
923 DecodedInstruction *dInsn,
Bill Buzbee89efc3d2009-07-28 11:22:22 -0700924 ArmLIR **pcrLabel)
Ben Chengba4fc8b2009-06-01 13:00:29 -0700925{
926 unsigned int i;
927 unsigned int regMask = 0;
Bill Buzbee1465db52009-09-23 17:17:35 -0700928 RegLocation rlArg;
929 int numDone = 0;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700930
Bill Buzbee1465db52009-09-23 17:17:35 -0700931 /*
932 * Load arguments to r0..r4. Note that these registers may contain
933 * live values, so we clobber them immediately after loading to prevent
934 * them from being used as sources for subsequent loads.
935 */
Bill Buzbeec6f10662010-02-09 11:16:15 -0800936 dvmCompilerLockAllTemps(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700937 for (i = 0; i < dInsn->vA; i++) {
938 regMask |= 1 << i;
Bill Buzbeec6f10662010-02-09 11:16:15 -0800939 rlArg = dvmCompilerGetSrc(cUnit, mir, numDone++);
Bill Buzbee1465db52009-09-23 17:17:35 -0700940 loadValueDirectFixed(cUnit, rlArg, i);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700941 }
942 if (regMask) {
943 /* Up to 5 args are pushed on top of FP - sizeofStackSaveArea */
Bill Buzbee1465db52009-09-23 17:17:35 -0700944 opRegRegImm(cUnit, kOpSub, r7, rFP,
945 sizeof(StackSaveArea) + (dInsn->vA << 2));
Ben Chengba4fc8b2009-06-01 13:00:29 -0700946 /* generate null check */
947 if (pcrLabel) {
Bill Buzbeec6f10662010-02-09 11:16:15 -0800948 *pcrLabel = genNullCheck(cUnit, dvmCompilerSSASrc(mir, 0), r0,
Bill Buzbee1465db52009-09-23 17:17:35 -0700949 mir->offset, NULL);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700950 }
Bill Buzbee270c1d62009-08-13 16:58:07 -0700951 storeMultiple(cUnit, r7, regMask);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700952 }
953}
954
955static void genProcessArgsRange(CompilationUnit *cUnit, MIR *mir,
956 DecodedInstruction *dInsn,
Bill Buzbee89efc3d2009-07-28 11:22:22 -0700957 ArmLIR **pcrLabel)
Ben Chengba4fc8b2009-06-01 13:00:29 -0700958{
959 int srcOffset = dInsn->vC << 2;
960 int numArgs = dInsn->vA;
961 int regMask;
Bill Buzbee1465db52009-09-23 17:17:35 -0700962
963 /*
964 * Note: here, all promoted registers will have been flushed
965 * back to the Dalvik base locations, so register usage restrictins
966 * are lifted. All parms loaded from original Dalvik register
967 * region - even though some might conceivably have valid copies
968 * cached in a preserved register.
969 */
Bill Buzbeec6f10662010-02-09 11:16:15 -0800970 dvmCompilerLockAllTemps(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -0700971
Ben Chengba4fc8b2009-06-01 13:00:29 -0700972 /*
973 * r4PC : &rFP[vC]
974 * r7: &newFP[0]
975 */
Bill Buzbee1465db52009-09-23 17:17:35 -0700976 opRegRegImm(cUnit, kOpAdd, r4PC, rFP, srcOffset);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700977 /* load [r0 .. min(numArgs,4)] */
978 regMask = (1 << ((numArgs < 4) ? numArgs : 4)) - 1;
Ben Chengd7d426a2009-09-22 11:23:36 -0700979 /*
980 * Protect the loadMultiple instruction from being reordered with other
981 * Dalvik stack accesses.
982 */
Bill Buzbee270c1d62009-08-13 16:58:07 -0700983 loadMultiple(cUnit, r4PC, regMask);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700984
Bill Buzbee1465db52009-09-23 17:17:35 -0700985 opRegRegImm(cUnit, kOpSub, r7, rFP,
986 sizeof(StackSaveArea) + (numArgs << 2));
Ben Chengba4fc8b2009-06-01 13:00:29 -0700987 /* generate null check */
988 if (pcrLabel) {
Bill Buzbeec6f10662010-02-09 11:16:15 -0800989 *pcrLabel = genNullCheck(cUnit, dvmCompilerSSASrc(mir, 0), r0,
Bill Buzbee1465db52009-09-23 17:17:35 -0700990 mir->offset, NULL);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700991 }
992
993 /*
994 * Handle remaining 4n arguments:
995 * store previously loaded 4 values and load the next 4 values
996 */
997 if (numArgs >= 8) {
Bill Buzbee89efc3d2009-07-28 11:22:22 -0700998 ArmLIR *loopLabel = NULL;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700999 /*
1000 * r0 contains "this" and it will be used later, so push it to the stack
Bill Buzbee270c1d62009-08-13 16:58:07 -07001001 * first. Pushing r5 (rFP) is just for stack alignment purposes.
Ben Chengba4fc8b2009-06-01 13:00:29 -07001002 */
Bill Buzbee1465db52009-09-23 17:17:35 -07001003 opImm(cUnit, kOpPush, (1 << r0 | 1 << rFP));
Ben Chengba4fc8b2009-06-01 13:00:29 -07001004 /* No need to generate the loop structure if numArgs <= 11 */
1005 if (numArgs > 11) {
1006 loadConstant(cUnit, 5, ((numArgs - 4) >> 2) << 2);
Bill Buzbee1465db52009-09-23 17:17:35 -07001007 loopLabel = newLIR0(cUnit, kArmPseudoTargetLabel);
Ben Chengd7d426a2009-09-22 11:23:36 -07001008 loopLabel->defMask = ENCODE_ALL;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001009 }
Bill Buzbee270c1d62009-08-13 16:58:07 -07001010 storeMultiple(cUnit, r7, regMask);
Ben Chengd7d426a2009-09-22 11:23:36 -07001011 /*
1012 * Protect the loadMultiple instruction from being reordered with other
1013 * Dalvik stack accesses.
1014 */
Bill Buzbee270c1d62009-08-13 16:58:07 -07001015 loadMultiple(cUnit, r4PC, regMask);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001016 /* No need to generate the loop structure if numArgs <= 11 */
1017 if (numArgs > 11) {
Bill Buzbee1465db52009-09-23 17:17:35 -07001018 opRegImm(cUnit, kOpSub, rFP, 4);
1019 genConditionalBranch(cUnit, kArmCondNe, loopLabel);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001020 }
1021 }
1022
1023 /* Save the last batch of loaded values */
Bill Buzbee270c1d62009-08-13 16:58:07 -07001024 storeMultiple(cUnit, r7, regMask);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001025
1026 /* Generate the loop epilogue - don't use r0 */
1027 if ((numArgs > 4) && (numArgs % 4)) {
1028 regMask = ((1 << (numArgs & 0x3)) - 1) << 1;
Ben Chengd7d426a2009-09-22 11:23:36 -07001029 /*
1030 * Protect the loadMultiple instruction from being reordered with other
1031 * Dalvik stack accesses.
1032 */
Bill Buzbee270c1d62009-08-13 16:58:07 -07001033 loadMultiple(cUnit, r4PC, regMask);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001034 }
1035 if (numArgs >= 8)
Bill Buzbee1465db52009-09-23 17:17:35 -07001036 opImm(cUnit, kOpPop, (1 << r0 | 1 << rFP));
Ben Chengba4fc8b2009-06-01 13:00:29 -07001037
1038 /* Save the modulo 4 arguments */
1039 if ((numArgs > 4) && (numArgs % 4)) {
Bill Buzbee270c1d62009-08-13 16:58:07 -07001040 storeMultiple(cUnit, r7, regMask);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001041 }
1042}
1043
Ben Cheng38329f52009-07-07 14:19:20 -07001044/*
1045 * Generate code to setup the call stack then jump to the chaining cell if it
1046 * is not a native method.
1047 */
1048static void genInvokeSingletonCommon(CompilationUnit *cUnit, MIR *mir,
Bill Buzbee89efc3d2009-07-28 11:22:22 -07001049 BasicBlock *bb, ArmLIR *labelList,
1050 ArmLIR *pcrLabel,
Ben Cheng38329f52009-07-07 14:19:20 -07001051 const Method *calleeMethod)
Ben Chengba4fc8b2009-06-01 13:00:29 -07001052{
Bill Buzbee1465db52009-09-23 17:17:35 -07001053 /*
1054 * Note: all Dalvik register state should be flushed to
1055 * memory by the point, so register usage restrictions no
1056 * longer apply. All temp & preserved registers may be used.
1057 */
Bill Buzbeec6f10662010-02-09 11:16:15 -08001058 dvmCompilerLockAllTemps(cUnit);
Bill Buzbee89efc3d2009-07-28 11:22:22 -07001059 ArmLIR *retChainingCell = &labelList[bb->fallThrough->id];
Ben Chengba4fc8b2009-06-01 13:00:29 -07001060
1061 /* r1 = &retChainingCell */
Bill Buzbeec6f10662010-02-09 11:16:15 -08001062 dvmCompilerLockTemp(cUnit, r1);
Bill Buzbee1465db52009-09-23 17:17:35 -07001063 ArmLIR *addrRetChain = opRegRegImm(cUnit, kOpAdd, r1, rpc, 0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001064 /* r4PC = dalvikCallsite */
1065 loadConstant(cUnit, r4PC,
1066 (int) (cUnit->method->insns + mir->offset));
1067 addrRetChain->generic.target = (LIR *) retChainingCell;
1068 /*
Ben Cheng38329f52009-07-07 14:19:20 -07001069 * r0 = calleeMethod (loaded upon calling genInvokeSingletonCommon)
Ben Chengba4fc8b2009-06-01 13:00:29 -07001070 * r1 = &ChainingCell
1071 * r4PC = callsiteDPC
1072 */
1073 if (dvmIsNativeMethod(calleeMethod)) {
Ben Cheng38329f52009-07-07 14:19:20 -07001074 genDispatchToHandler(cUnit, TEMPLATE_INVOKE_METHOD_NATIVE);
Ben Cheng978738d2010-05-13 13:45:57 -07001075#if defined(WITH_JIT_TUNING)
Ben Cheng38329f52009-07-07 14:19:20 -07001076 gDvmJit.invokeNative++;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001077#endif
1078 } else {
1079 genDispatchToHandler(cUnit, TEMPLATE_INVOKE_METHOD_CHAIN);
Ben Cheng978738d2010-05-13 13:45:57 -07001080#if defined(WITH_JIT_TUNING)
Ben Cheng86717f72010-03-05 15:27:21 -08001081 gDvmJit.invokeMonomorphic++;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001082#endif
Ben Cheng38329f52009-07-07 14:19:20 -07001083 /* Branch to the chaining cell */
Ben Chengba4fc8b2009-06-01 13:00:29 -07001084 genUnconditionalBranch(cUnit, &labelList[bb->taken->id]);
1085 }
1086 /* Handle exceptions using the interpreter */
1087 genTrap(cUnit, mir->offset, pcrLabel);
1088}
1089
Ben Cheng38329f52009-07-07 14:19:20 -07001090/*
1091 * Generate code to check the validity of a predicted chain and take actions
1092 * based on the result.
1093 *
1094 * 0x426a99aa : ldr r4, [pc, #72] --> r4 <- dalvikPC of this invoke
1095 * 0x426a99ac : add r1, pc, #32 --> r1 <- &retChainingCell
1096 * 0x426a99ae : add r2, pc, #40 --> r2 <- &predictedChainingCell
1097 * 0x426a99b0 : blx_1 0x426a918c --+ TEMPLATE_INVOKE_METHOD_PREDICTED_CHAIN
1098 * 0x426a99b2 : blx_2 see above --+
1099 * 0x426a99b4 : b 0x426a99d8 --> off to the predicted chain
1100 * 0x426a99b6 : b 0x426a99c8 --> punt to the interpreter
1101 * 0x426a99b8 : ldr r0, [r7, #44] --> r0 <- this->class->vtable[methodIdx]
1102 * 0x426a99ba : cmp r1, #0 --> compare r1 (rechain count) against 0
1103 * 0x426a99bc : bgt 0x426a99c2 --> >=0? don't rechain
1104 * 0x426a99be : ldr r7, [r6, #96] --+ dvmJitToPatchPredictedChain
1105 * 0x426a99c0 : blx r7 --+
1106 * 0x426a99c2 : add r1, pc, #12 --> r1 <- &retChainingCell
1107 * 0x426a99c4 : blx_1 0x426a9098 --+ TEMPLATE_INVOKE_METHOD_NO_OPT
1108 * 0x426a99c6 : blx_2 see above --+
1109 */
1110static void genInvokeVirtualCommon(CompilationUnit *cUnit, MIR *mir,
1111 int methodIndex,
Bill Buzbee89efc3d2009-07-28 11:22:22 -07001112 ArmLIR *retChainingCell,
1113 ArmLIR *predChainingCell,
1114 ArmLIR *pcrLabel)
Ben Cheng38329f52009-07-07 14:19:20 -07001115{
Bill Buzbee1465db52009-09-23 17:17:35 -07001116 /*
1117 * Note: all Dalvik register state should be flushed to
1118 * memory by the point, so register usage restrictions no
1119 * longer apply. Lock temps to prevent them from being
1120 * allocated by utility routines.
1121 */
Bill Buzbeec6f10662010-02-09 11:16:15 -08001122 dvmCompilerLockAllTemps(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07001123
Ben Cheng38329f52009-07-07 14:19:20 -07001124 /* "this" is already left in r0 by genProcessArgs* */
1125
1126 /* r4PC = dalvikCallsite */
1127 loadConstant(cUnit, r4PC,
1128 (int) (cUnit->method->insns + mir->offset));
1129
1130 /* r1 = &retChainingCell */
Bill Buzbee1465db52009-09-23 17:17:35 -07001131 ArmLIR *addrRetChain = opRegRegImm(cUnit, kOpAdd, r1, rpc, 0);
Ben Cheng38329f52009-07-07 14:19:20 -07001132 addrRetChain->generic.target = (LIR *) retChainingCell;
1133
1134 /* r2 = &predictedChainingCell */
Bill Buzbee1465db52009-09-23 17:17:35 -07001135 ArmLIR *predictedChainingCell = opRegRegImm(cUnit, kOpAdd, r2, rpc, 0);
Ben Cheng38329f52009-07-07 14:19:20 -07001136 predictedChainingCell->generic.target = (LIR *) predChainingCell;
1137
1138 genDispatchToHandler(cUnit, TEMPLATE_INVOKE_METHOD_PREDICTED_CHAIN);
1139
1140 /* return through lr - jump to the chaining cell */
1141 genUnconditionalBranch(cUnit, predChainingCell);
1142
1143 /*
1144 * null-check on "this" may have been eliminated, but we still need a PC-
1145 * reconstruction label for stack overflow bailout.
1146 */
1147 if (pcrLabel == NULL) {
1148 int dPC = (int) (cUnit->method->insns + mir->offset);
Bill Buzbee89efc3d2009-07-28 11:22:22 -07001149 pcrLabel = dvmCompilerNew(sizeof(ArmLIR), true);
Ben Chenga4973592010-03-31 11:59:18 -07001150 pcrLabel->opCode = kArmPseudoPCReconstructionCell;
Ben Cheng38329f52009-07-07 14:19:20 -07001151 pcrLabel->operands[0] = dPC;
1152 pcrLabel->operands[1] = mir->offset;
1153 /* Insert the place holder to the growable list */
1154 dvmInsertGrowableList(&cUnit->pcReconstructionList, pcrLabel);
1155 }
1156
1157 /* return through lr+2 - punt to the interpreter */
1158 genUnconditionalBranch(cUnit, pcrLabel);
1159
1160 /*
1161 * return through lr+4 - fully resolve the callee method.
1162 * r1 <- count
1163 * r2 <- &predictedChainCell
1164 * r3 <- this->class
1165 * r4 <- dPC
1166 * r7 <- this->class->vtable
1167 */
1168
1169 /* r0 <- calleeMethod */
Bill Buzbee270c1d62009-08-13 16:58:07 -07001170 loadWordDisp(cUnit, r7, methodIndex * 4, r0);
Ben Cheng38329f52009-07-07 14:19:20 -07001171
1172 /* Check if rechain limit is reached */
Bill Buzbee1465db52009-09-23 17:17:35 -07001173 opRegImm(cUnit, kOpCmp, r1, 0);
Ben Cheng38329f52009-07-07 14:19:20 -07001174
Bill Buzbee1465db52009-09-23 17:17:35 -07001175 ArmLIR *bypassRechaining = opCondBranch(cUnit, kArmCondGt);
Ben Cheng38329f52009-07-07 14:19:20 -07001176
Bill Buzbee270c1d62009-08-13 16:58:07 -07001177 loadWordDisp(cUnit, rGLUE, offsetof(InterpState,
1178 jitToInterpEntries.dvmJitToPatchPredictedChain), r7);
Ben Cheng38329f52009-07-07 14:19:20 -07001179
Ben Chengb88ec3c2010-05-17 12:50:33 -07001180 genRegCopy(cUnit, r1, rGLUE);
1181
Ben Cheng38329f52009-07-07 14:19:20 -07001182 /*
1183 * r0 = calleeMethod
1184 * r2 = &predictedChainingCell
1185 * r3 = class
1186 *
1187 * &returnChainingCell has been loaded into r1 but is not needed
1188 * when patching the chaining cell and will be clobbered upon
1189 * returning so it will be reconstructed again.
1190 */
Bill Buzbee1465db52009-09-23 17:17:35 -07001191 opReg(cUnit, kOpBlx, r7);
Ben Cheng38329f52009-07-07 14:19:20 -07001192
1193 /* r1 = &retChainingCell */
Bill Buzbee1465db52009-09-23 17:17:35 -07001194 addrRetChain = opRegRegImm(cUnit, kOpAdd, r1, rpc, 0);
Ben Cheng38329f52009-07-07 14:19:20 -07001195 addrRetChain->generic.target = (LIR *) retChainingCell;
1196
1197 bypassRechaining->generic.target = (LIR *) addrRetChain;
1198 /*
1199 * r0 = calleeMethod,
1200 * r1 = &ChainingCell,
1201 * r4PC = callsiteDPC,
1202 */
1203 genDispatchToHandler(cUnit, TEMPLATE_INVOKE_METHOD_NO_OPT);
Ben Cheng978738d2010-05-13 13:45:57 -07001204#if defined(WITH_JIT_TUNING)
Ben Cheng86717f72010-03-05 15:27:21 -08001205 gDvmJit.invokePolymorphic++;
Ben Cheng38329f52009-07-07 14:19:20 -07001206#endif
1207 /* Handle exceptions using the interpreter */
1208 genTrap(cUnit, mir->offset, pcrLabel);
1209}
1210
Ben Chengba4fc8b2009-06-01 13:00:29 -07001211/* Geneate a branch to go back to the interpreter */
1212static void genPuntToInterp(CompilationUnit *cUnit, unsigned int offset)
1213{
1214 /* r0 = dalvik pc */
Bill Buzbeec6f10662010-02-09 11:16:15 -08001215 dvmCompilerFlushAllRegs(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001216 loadConstant(cUnit, r0, (int) (cUnit->method->insns + offset));
Bill Buzbee270c1d62009-08-13 16:58:07 -07001217 loadWordDisp(cUnit, r0, offsetof(Object, clazz), r3);
1218 loadWordDisp(cUnit, rGLUE, offsetof(InterpState,
1219 jitToInterpEntries.dvmJitToInterpPunt), r1);
Bill Buzbee1465db52009-09-23 17:17:35 -07001220 opReg(cUnit, kOpBlx, r1);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001221}
1222
1223/*
1224 * Attempt to single step one instruction using the interpreter and return
1225 * to the compiled code for the next Dalvik instruction
1226 */
1227static void genInterpSingleStep(CompilationUnit *cUnit, MIR *mir)
1228{
1229 int flags = dexGetInstrFlags(gDvm.instrFlags, mir->dalvikInsn.opCode);
1230 int flagsToCheck = kInstrCanBranch | kInstrCanSwitch | kInstrCanReturn |
1231 kInstrCanThrow;
Bill Buzbee1465db52009-09-23 17:17:35 -07001232
Bill Buzbee45273872010-03-11 11:12:15 -08001233 //If already optimized out, just ignore
1234 if (mir->dalvikInsn.opCode == OP_NOP)
1235 return;
1236
Bill Buzbee1465db52009-09-23 17:17:35 -07001237 //Ugly, but necessary. Flush all Dalvik regs so Interp can find them
Bill Buzbeec6f10662010-02-09 11:16:15 -08001238 dvmCompilerFlushAllRegs(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07001239
Ben Chengba4fc8b2009-06-01 13:00:29 -07001240 if ((mir->next == NULL) || (flags & flagsToCheck)) {
1241 genPuntToInterp(cUnit, mir->offset);
1242 return;
1243 }
1244 int entryAddr = offsetof(InterpState,
1245 jitToInterpEntries.dvmJitToInterpSingleStep);
Bill Buzbee270c1d62009-08-13 16:58:07 -07001246 loadWordDisp(cUnit, rGLUE, entryAddr, r2);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001247 /* r0 = dalvik pc */
1248 loadConstant(cUnit, r0, (int) (cUnit->method->insns + mir->offset));
1249 /* r1 = dalvik pc of following instruction */
1250 loadConstant(cUnit, r1, (int) (cUnit->method->insns + mir->next->offset));
Bill Buzbee1465db52009-09-23 17:17:35 -07001251 opReg(cUnit, kOpBlx, r2);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001252}
1253
Ben Chengfc075c22010-05-28 15:20:08 -07001254#if defined(WITH_DEADLOCK_PREDICTION) || defined(WITH_MONITOR_TRACKING) || \
1255 defined(_ARMV5TE) || defined(_ARMV5TE_VFP)
Bill Buzbeec1d9ed42010-02-02 11:04:33 -08001256/*
1257 * To prevent a thread in a monitor wait from blocking the Jit from
1258 * resetting the code cache, heavyweight monitor lock will not
1259 * be allowed to return to an existing translation. Instead, we will
1260 * handle them by branching to a handler, which will in turn call the
1261 * runtime lock routine and then branch directly back to the
1262 * interpreter main loop. Given the high cost of the heavyweight
1263 * lock operation, this additional cost should be slight (especially when
1264 * considering that we expect the vast majority of lock operations to
1265 * use the fast-path thin lock bypass).
1266 */
Ben Cheng5d90c202009-11-22 23:31:11 -08001267static void genMonitorPortable(CompilationUnit *cUnit, MIR *mir)
Bill Buzbee270c1d62009-08-13 16:58:07 -07001268{
Bill Buzbeeefbd3c52009-11-04 22:18:40 -08001269 bool isEnter = (mir->dalvikInsn.opCode == OP_MONITOR_ENTER);
Bill Buzbee1465db52009-09-23 17:17:35 -07001270 genExportPC(cUnit, mir);
Bill Buzbeec6f10662010-02-09 11:16:15 -08001271 dvmCompilerFlushAllRegs(cUnit); /* Send everything to home location */
1272 RegLocation rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07001273 loadValueDirectFixed(cUnit, rlSrc, r1);
1274 loadWordDisp(cUnit, rGLUE, offsetof(InterpState, self), r0);
Bill Buzbeec1d9ed42010-02-02 11:04:33 -08001275 genNullCheck(cUnit, rlSrc.sRegLow, r1, mir->offset, NULL);
Bill Buzbeeefbd3c52009-11-04 22:18:40 -08001276 if (isEnter) {
Bill Buzbeec1d9ed42010-02-02 11:04:33 -08001277 /* Get dPC of next insn */
1278 loadConstant(cUnit, r4PC, (int)(cUnit->method->insns + mir->offset +
1279 dexGetInstrWidthAbs(gDvm.instrWidth, OP_MONITOR_ENTER)));
1280#if defined(WITH_DEADLOCK_PREDICTION)
1281 genDispatchToHandler(cUnit, TEMPLATE_MONITOR_ENTER_DEBUG);
1282#else
1283 genDispatchToHandler(cUnit, TEMPLATE_MONITOR_ENTER);
1284#endif
Bill Buzbee1465db52009-09-23 17:17:35 -07001285 } else {
Ben Chengbd1326d2010-04-02 15:04:53 -07001286 LOAD_FUNC_ADDR(cUnit, r2, (int)dvmUnlockObject);
Bill Buzbeec1d9ed42010-02-02 11:04:33 -08001287 /* Do the call */
1288 opReg(cUnit, kOpBlx, r2);
Bill Buzbee6bbdd6b2010-02-16 14:40:01 -08001289 opRegImm(cUnit, kOpCmp, r0, 0); /* Did we throw? */
1290 ArmLIR *branchOver = opCondBranch(cUnit, kArmCondNe);
1291 loadConstant(cUnit, r0,
1292 (int) (cUnit->method->insns + mir->offset +
1293 dexGetInstrWidthAbs(gDvm.instrWidth, OP_MONITOR_EXIT)));
1294 genDispatchToHandler(cUnit, TEMPLATE_THROW_EXCEPTION_COMMON);
1295 ArmLIR *target = newLIR0(cUnit, kArmPseudoTargetLabel);
1296 target->defMask = ENCODE_ALL;
1297 branchOver->generic.target = (LIR *) target;
Elliott Hughes6a555132010-02-25 15:41:42 -08001298 dvmCompilerClobberCallRegs(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07001299 }
Bill Buzbee270c1d62009-08-13 16:58:07 -07001300}
Ben Chengfc075c22010-05-28 15:20:08 -07001301#endif
Bill Buzbee270c1d62009-08-13 16:58:07 -07001302
Ben Chengba4fc8b2009-06-01 13:00:29 -07001303/*
1304 * The following are the first-level codegen routines that analyze the format
1305 * of each bytecode then either dispatch special purpose codegen routines
1306 * or produce corresponding Thumb instructions directly.
1307 */
1308
1309static bool handleFmt10t_Fmt20t_Fmt30t(CompilationUnit *cUnit, MIR *mir,
Bill Buzbee89efc3d2009-07-28 11:22:22 -07001310 BasicBlock *bb, ArmLIR *labelList)
Ben Chengba4fc8b2009-06-01 13:00:29 -07001311{
1312 /* For OP_GOTO, OP_GOTO_16, and OP_GOTO_32 */
1313 genUnconditionalBranch(cUnit, &labelList[bb->taken->id]);
1314 return false;
1315}
1316
1317static bool handleFmt10x(CompilationUnit *cUnit, MIR *mir)
1318{
1319 OpCode dalvikOpCode = mir->dalvikInsn.opCode;
Andy McFaddenc35a2ef2010-06-17 12:36:00 -07001320 if ((dalvikOpCode >= OP_UNUSED_3E) && (dalvikOpCode <= OP_UNUSED_43)) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07001321 LOGE("Codegen: got unused opcode 0x%x\n",dalvikOpCode);
1322 return true;
1323 }
1324 switch (dalvikOpCode) {
1325 case OP_RETURN_VOID:
1326 genReturnCommon(cUnit,mir);
1327 break;
1328 case OP_UNUSED_73:
1329 case OP_UNUSED_79:
1330 case OP_UNUSED_7A:
Andy McFaddenc35a2ef2010-06-17 12:36:00 -07001331 case OP_UNUSED_F1:
1332 case OP_UNUSED_FF:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001333 LOGE("Codegen: got unused opcode 0x%x\n",dalvikOpCode);
1334 return true;
1335 case OP_NOP:
1336 break;
1337 default:
1338 return true;
1339 }
1340 return false;
1341}
1342
1343static bool handleFmt11n_Fmt31i(CompilationUnit *cUnit, MIR *mir)
1344{
Bill Buzbee1465db52009-09-23 17:17:35 -07001345 RegLocation rlDest;
1346 RegLocation rlResult;
1347 if (mir->ssaRep->numDefs == 2) {
Bill Buzbeec6f10662010-02-09 11:16:15 -08001348 rlDest = dvmCompilerGetDestWide(cUnit, mir, 0, 1);
Bill Buzbee1465db52009-09-23 17:17:35 -07001349 } else {
Bill Buzbeec6f10662010-02-09 11:16:15 -08001350 rlDest = dvmCompilerGetDest(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07001351 }
Ben Chenge9695e52009-06-16 16:11:47 -07001352
Ben Chengba4fc8b2009-06-01 13:00:29 -07001353 switch (mir->dalvikInsn.opCode) {
1354 case OP_CONST:
Ben Chenge9695e52009-06-16 16:11:47 -07001355 case OP_CONST_4: {
Bill Buzbeec6f10662010-02-09 11:16:15 -08001356 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kAnyReg, true);
Ben Chengbd1326d2010-04-02 15:04:53 -07001357 loadConstantNoClobber(cUnit, rlResult.lowReg, mir->dalvikInsn.vB);
Bill Buzbee1465db52009-09-23 17:17:35 -07001358 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001359 break;
Ben Chenge9695e52009-06-16 16:11:47 -07001360 }
1361 case OP_CONST_WIDE_32: {
Bill Buzbee1465db52009-09-23 17:17:35 -07001362 //TUNING: single routine to load constant pair for support doubles
Bill Buzbee964a7b02010-01-28 12:54:19 -08001363 //TUNING: load 0/-1 separately to avoid load dependency
Bill Buzbeec6f10662010-02-09 11:16:15 -08001364 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Ben Chengbd1326d2010-04-02 15:04:53 -07001365 loadConstantNoClobber(cUnit, rlResult.lowReg, mir->dalvikInsn.vB);
Bill Buzbee1465db52009-09-23 17:17:35 -07001366 opRegRegImm(cUnit, kOpAsr, rlResult.highReg,
1367 rlResult.lowReg, 31);
1368 storeValueWide(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001369 break;
Ben Chenge9695e52009-06-16 16:11:47 -07001370 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07001371 default:
1372 return true;
1373 }
1374 return false;
1375}
1376
1377static bool handleFmt21h(CompilationUnit *cUnit, MIR *mir)
1378{
Bill Buzbee1465db52009-09-23 17:17:35 -07001379 RegLocation rlDest;
1380 RegLocation rlResult;
1381 if (mir->ssaRep->numDefs == 2) {
Bill Buzbeec6f10662010-02-09 11:16:15 -08001382 rlDest = dvmCompilerGetDestWide(cUnit, mir, 0, 1);
Bill Buzbee1465db52009-09-23 17:17:35 -07001383 } else {
Bill Buzbeec6f10662010-02-09 11:16:15 -08001384 rlDest = dvmCompilerGetDest(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07001385 }
Bill Buzbeec6f10662010-02-09 11:16:15 -08001386 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kAnyReg, true);
Ben Chenge9695e52009-06-16 16:11:47 -07001387
Ben Chengba4fc8b2009-06-01 13:00:29 -07001388 switch (mir->dalvikInsn.opCode) {
Ben Chenge9695e52009-06-16 16:11:47 -07001389 case OP_CONST_HIGH16: {
Ben Chengbd1326d2010-04-02 15:04:53 -07001390 loadConstantNoClobber(cUnit, rlResult.lowReg,
1391 mir->dalvikInsn.vB << 16);
Bill Buzbee1465db52009-09-23 17:17:35 -07001392 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001393 break;
Ben Chenge9695e52009-06-16 16:11:47 -07001394 }
1395 case OP_CONST_WIDE_HIGH16: {
Bill Buzbee1465db52009-09-23 17:17:35 -07001396 loadConstantValueWide(cUnit, rlResult.lowReg, rlResult.highReg,
1397 0, mir->dalvikInsn.vB << 16);
1398 storeValueWide(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001399 break;
Ben Chenge9695e52009-06-16 16:11:47 -07001400 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07001401 default:
1402 return true;
1403 }
1404 return false;
1405}
1406
1407static bool handleFmt20bc(CompilationUnit *cUnit, MIR *mir)
1408{
1409 /* For OP_THROW_VERIFICATION_ERROR */
1410 genInterpSingleStep(cUnit, mir);
1411 return false;
1412}
1413
1414static bool handleFmt21c_Fmt31c(CompilationUnit *cUnit, MIR *mir)
1415{
Bill Buzbee1465db52009-09-23 17:17:35 -07001416 RegLocation rlResult;
1417 RegLocation rlDest;
1418 RegLocation rlSrc;
Ben Chenge9695e52009-06-16 16:11:47 -07001419
Ben Chengba4fc8b2009-06-01 13:00:29 -07001420 switch (mir->dalvikInsn.opCode) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07001421 case OP_CONST_STRING_JUMBO:
1422 case OP_CONST_STRING: {
1423 void *strPtr = (void*)
1424 (cUnit->method->clazz->pDvmDex->pResStrings[mir->dalvikInsn.vB]);
Ben Chengdd6e8702010-05-07 13:05:47 -07001425
1426 if (strPtr == NULL) {
1427 LOGE("Unexpected null string");
1428 dvmAbort();
1429 }
1430
Bill Buzbeec6f10662010-02-09 11:16:15 -08001431 rlDest = dvmCompilerGetDest(cUnit, mir, 0);
1432 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Ben Chengbd1326d2010-04-02 15:04:53 -07001433 loadConstantNoClobber(cUnit, rlResult.lowReg, (int) strPtr );
Bill Buzbee1465db52009-09-23 17:17:35 -07001434 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001435 break;
1436 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07001437 case OP_CONST_CLASS: {
1438 void *classPtr = (void*)
1439 (cUnit->method->clazz->pDvmDex->pResClasses[mir->dalvikInsn.vB]);
Ben Chengdd6e8702010-05-07 13:05:47 -07001440
1441 if (classPtr == NULL) {
1442 LOGE("Unexpected null class");
1443 dvmAbort();
1444 }
1445
Bill Buzbeec6f10662010-02-09 11:16:15 -08001446 rlDest = dvmCompilerGetDest(cUnit, mir, 0);
1447 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Ben Chengbd1326d2010-04-02 15:04:53 -07001448 loadConstantNoClobber(cUnit, rlResult.lowReg, (int) classPtr );
Bill Buzbee1465db52009-09-23 17:17:35 -07001449 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001450 break;
1451 }
buzbeeecf8f6e2010-07-20 14:53:42 -07001452 case OP_SGET_VOLATILE:
1453 case OP_SGET_OBJECT_VOLATILE:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001454 case OP_SGET_OBJECT:
1455 case OP_SGET_BOOLEAN:
1456 case OP_SGET_CHAR:
1457 case OP_SGET_BYTE:
1458 case OP_SGET_SHORT:
1459 case OP_SGET: {
Bill Buzbee50a6bf22009-07-08 13:08:04 -07001460 int valOffset = offsetof(StaticField, value);
Bill Buzbeec6f10662010-02-09 11:16:15 -08001461 int tReg = dvmCompilerAllocTemp(cUnit);
buzbeeecf8f6e2010-07-20 14:53:42 -07001462 bool isVolatile;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001463 void *fieldPtr = (void*)
1464 (cUnit->method->clazz->pDvmDex->pResFields[mir->dalvikInsn.vB]);
Ben Chengdd6e8702010-05-07 13:05:47 -07001465
1466 if (fieldPtr == NULL) {
1467 LOGE("Unexpected null static field");
1468 dvmAbort();
1469 }
1470
buzbeeecf8f6e2010-07-20 14:53:42 -07001471 isVolatile = (mir->dalvikInsn.opCode == OP_SGET_VOLATILE) ||
1472 (mir->dalvikInsn.opCode == OP_SGET_OBJECT_VOLATILE) ||
1473 dvmIsVolatileField(fieldPtr);
1474
Bill Buzbeec6f10662010-02-09 11:16:15 -08001475 rlDest = dvmCompilerGetDest(cUnit, mir, 0);
1476 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kAnyReg, true);
Bill Buzbee1465db52009-09-23 17:17:35 -07001477 loadConstant(cUnit, tReg, (int) fieldPtr + valOffset);
Ben Cheng11d8f142010-03-24 15:24:19 -07001478
buzbeeecf8f6e2010-07-20 14:53:42 -07001479 if (isVolatile) {
1480 dvmCompilerGenMemBarrier(cUnit);
1481 }
Ben Cheng11d8f142010-03-24 15:24:19 -07001482 HEAP_ACCESS_SHADOW(true);
Bill Buzbee1465db52009-09-23 17:17:35 -07001483 loadWordDisp(cUnit, tReg, 0, rlResult.lowReg);
Ben Cheng11d8f142010-03-24 15:24:19 -07001484 HEAP_ACCESS_SHADOW(false);
1485
Bill Buzbee1465db52009-09-23 17:17:35 -07001486 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001487 break;
1488 }
1489 case OP_SGET_WIDE: {
Bill Buzbee50a6bf22009-07-08 13:08:04 -07001490 int valOffset = offsetof(StaticField, value);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001491 void *fieldPtr = (void*)
1492 (cUnit->method->clazz->pDvmDex->pResFields[mir->dalvikInsn.vB]);
Ben Chengdd6e8702010-05-07 13:05:47 -07001493
1494 if (fieldPtr == NULL) {
1495 LOGE("Unexpected null static field");
1496 dvmAbort();
1497 }
1498
Bill Buzbeec6f10662010-02-09 11:16:15 -08001499 int tReg = dvmCompilerAllocTemp(cUnit);
Bill Buzbeec6f10662010-02-09 11:16:15 -08001500 rlDest = dvmCompilerGetDestWide(cUnit, mir, 0, 1);
1501 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kAnyReg, true);
Bill Buzbee1465db52009-09-23 17:17:35 -07001502 loadConstant(cUnit, tReg, (int) fieldPtr + valOffset);
Ben Cheng11d8f142010-03-24 15:24:19 -07001503
1504 HEAP_ACCESS_SHADOW(true);
Bill Buzbee1465db52009-09-23 17:17:35 -07001505 loadPair(cUnit, tReg, rlResult.lowReg, rlResult.highReg);
Ben Cheng11d8f142010-03-24 15:24:19 -07001506 HEAP_ACCESS_SHADOW(false);
1507
Bill Buzbee1465db52009-09-23 17:17:35 -07001508 storeValueWide(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001509 break;
1510 }
1511 case OP_SPUT_OBJECT:
1512 case OP_SPUT_BOOLEAN:
1513 case OP_SPUT_CHAR:
1514 case OP_SPUT_BYTE:
1515 case OP_SPUT_SHORT:
1516 case OP_SPUT: {
Bill Buzbee50a6bf22009-07-08 13:08:04 -07001517 int valOffset = offsetof(StaticField, value);
Bill Buzbeec6f10662010-02-09 11:16:15 -08001518 int tReg = dvmCompilerAllocTemp(cUnit);
buzbeeecf8f6e2010-07-20 14:53:42 -07001519 bool isVolatile;
1520 Field *fieldPtr =
Ben Chengba4fc8b2009-06-01 13:00:29 -07001521 (cUnit->method->clazz->pDvmDex->pResFields[mir->dalvikInsn.vB]);
Ben Chenge9695e52009-06-16 16:11:47 -07001522
buzbeeecf8f6e2010-07-20 14:53:42 -07001523 isVolatile = (mir->dalvikInsn.opCode == OP_SPUT_VOLATILE) ||
1524 (mir->dalvikInsn.opCode == OP_SPUT_OBJECT_VOLATILE) ||
1525 dvmIsVolatileField(fieldPtr);
1526
Ben Chengdd6e8702010-05-07 13:05:47 -07001527 if (fieldPtr == NULL) {
1528 LOGE("Unexpected null static field");
1529 dvmAbort();
1530 }
1531
Bill Buzbeec6f10662010-02-09 11:16:15 -08001532 rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07001533 rlSrc = loadValue(cUnit, rlSrc, kAnyReg);
1534 loadConstant(cUnit, tReg, (int) fieldPtr + valOffset);
Ben Cheng11d8f142010-03-24 15:24:19 -07001535
1536 HEAP_ACCESS_SHADOW(true);
Bill Buzbee1465db52009-09-23 17:17:35 -07001537 storeWordDisp(cUnit, tReg, 0 ,rlSrc.lowReg);
Ben Cheng11d8f142010-03-24 15:24:19 -07001538 HEAP_ACCESS_SHADOW(false);
buzbeeecf8f6e2010-07-20 14:53:42 -07001539 if (isVolatile) {
1540 dvmCompilerGenMemBarrier(cUnit);
1541 }
buzbee919eb062010-07-12 12:59:22 -07001542 if (mir->dalvikInsn.opCode == OP_SPUT_OBJECT) {
1543 /* NOTE: marking card based on field address */
1544 markCard(cUnit, rlSrc.lowReg, tReg);
1545 }
Ben Cheng11d8f142010-03-24 15:24:19 -07001546
Ben Chengba4fc8b2009-06-01 13:00:29 -07001547 break;
1548 }
1549 case OP_SPUT_WIDE: {
Bill Buzbeec6f10662010-02-09 11:16:15 -08001550 int tReg = dvmCompilerAllocTemp(cUnit);
Bill Buzbee50a6bf22009-07-08 13:08:04 -07001551 int valOffset = offsetof(StaticField, value);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001552 void *fieldPtr = (void*)
1553 (cUnit->method->clazz->pDvmDex->pResFields[mir->dalvikInsn.vB]);
Ben Chenge9695e52009-06-16 16:11:47 -07001554
Ben Chengdd6e8702010-05-07 13:05:47 -07001555 if (fieldPtr == NULL) {
1556 LOGE("Unexpected null static field");
1557 dvmAbort();
1558 }
1559
Bill Buzbeec6f10662010-02-09 11:16:15 -08001560 rlSrc = dvmCompilerGetSrcWide(cUnit, mir, 0, 1);
Bill Buzbee1465db52009-09-23 17:17:35 -07001561 rlSrc = loadValueWide(cUnit, rlSrc, kAnyReg);
1562 loadConstant(cUnit, tReg, (int) fieldPtr + valOffset);
Ben Cheng11d8f142010-03-24 15:24:19 -07001563
1564 HEAP_ACCESS_SHADOW(true);
Bill Buzbee1465db52009-09-23 17:17:35 -07001565 storePair(cUnit, tReg, rlSrc.lowReg, rlSrc.highReg);
Ben Cheng11d8f142010-03-24 15:24:19 -07001566 HEAP_ACCESS_SHADOW(false);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001567 break;
1568 }
1569 case OP_NEW_INSTANCE: {
Ben Chenge9695e52009-06-16 16:11:47 -07001570 /*
1571 * Obey the calling convention and don't mess with the register
1572 * usage.
1573 */
Ben Chengba4fc8b2009-06-01 13:00:29 -07001574 ClassObject *classPtr = (void*)
1575 (cUnit->method->clazz->pDvmDex->pResClasses[mir->dalvikInsn.vB]);
Ben Chengdd6e8702010-05-07 13:05:47 -07001576
1577 if (classPtr == NULL) {
1578 LOGE("Unexpected null class");
1579 dvmAbort();
1580 }
1581
Ben Cheng79d173c2009-09-29 16:12:51 -07001582 /*
1583 * If it is going to throw, it should not make to the trace to begin
Bill Buzbee1465db52009-09-23 17:17:35 -07001584 * with. However, Alloc might throw, so we need to genExportPC()
Ben Cheng79d173c2009-09-29 16:12:51 -07001585 */
1586 assert((classPtr->accessFlags & (ACC_INTERFACE|ACC_ABSTRACT)) == 0);
Bill Buzbeec6f10662010-02-09 11:16:15 -08001587 dvmCompilerFlushAllRegs(cUnit); /* Everything to home location */
Bill Buzbee1465db52009-09-23 17:17:35 -07001588 genExportPC(cUnit, mir);
Ben Chengbd1326d2010-04-02 15:04:53 -07001589 LOAD_FUNC_ADDR(cUnit, r2, (int)dvmAllocObject);
Ben Chenge9695e52009-06-16 16:11:47 -07001590 loadConstant(cUnit, r0, (int) classPtr);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001591 loadConstant(cUnit, r1, ALLOC_DONT_TRACK);
Bill Buzbee1465db52009-09-23 17:17:35 -07001592 opReg(cUnit, kOpBlx, r2);
Elliott Hughes6a555132010-02-25 15:41:42 -08001593 dvmCompilerClobberCallRegs(cUnit);
Ben Cheng4f489172009-09-27 17:08:35 -07001594 /* generate a branch over if allocation is successful */
Bill Buzbee1465db52009-09-23 17:17:35 -07001595 opRegImm(cUnit, kOpCmp, r0, 0); /* NULL? */
1596 ArmLIR *branchOver = opCondBranch(cUnit, kArmCondNe);
Ben Cheng4f489172009-09-27 17:08:35 -07001597 /*
1598 * OOM exception needs to be thrown here and cannot re-execute
1599 */
1600 loadConstant(cUnit, r0,
1601 (int) (cUnit->method->insns + mir->offset));
1602 genDispatchToHandler(cUnit, TEMPLATE_THROW_EXCEPTION_COMMON);
1603 /* noreturn */
1604
Bill Buzbee1465db52009-09-23 17:17:35 -07001605 ArmLIR *target = newLIR0(cUnit, kArmPseudoTargetLabel);
Ben Cheng4f489172009-09-27 17:08:35 -07001606 target->defMask = ENCODE_ALL;
1607 branchOver->generic.target = (LIR *) target;
Bill Buzbeec6f10662010-02-09 11:16:15 -08001608 rlDest = dvmCompilerGetDest(cUnit, mir, 0);
1609 rlResult = dvmCompilerGetReturn(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07001610 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001611 break;
1612 }
1613 case OP_CHECK_CAST: {
Ben Chenge9695e52009-06-16 16:11:47 -07001614 /*
1615 * Obey the calling convention and don't mess with the register
1616 * usage.
1617 */
Ben Chengba4fc8b2009-06-01 13:00:29 -07001618 ClassObject *classPtr =
1619 (cUnit->method->clazz->pDvmDex->pResClasses[mir->dalvikInsn.vB]);
Bill Buzbee4df41a52009-11-12 17:07:16 -08001620 /*
1621 * Note: It is possible that classPtr is NULL at this point,
1622 * even though this instruction has been successfully interpreted.
1623 * If the previous interpretation had a null source, the
1624 * interpreter would not have bothered to resolve the clazz.
1625 * Bail out to the interpreter in this case, and log it
1626 * so that we can tell if it happens frequently.
1627 */
1628 if (classPtr == NULL) {
Ben Cheng11d8f142010-03-24 15:24:19 -07001629 LOGVV("null clazz in OP_CHECK_CAST, single-stepping");
Bill Buzbee4df41a52009-11-12 17:07:16 -08001630 genInterpSingleStep(cUnit, mir);
1631 return false;
1632 }
Bill Buzbeec6f10662010-02-09 11:16:15 -08001633 dvmCompilerFlushAllRegs(cUnit); /* Everything to home location */
Ben Chengba4fc8b2009-06-01 13:00:29 -07001634 loadConstant(cUnit, r1, (int) classPtr );
Bill Buzbeec6f10662010-02-09 11:16:15 -08001635 rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07001636 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
1637 opRegImm(cUnit, kOpCmp, rlSrc.lowReg, 0); /* Null? */
1638 ArmLIR *branch1 = opCondBranch(cUnit, kArmCondEq);
1639 /*
1640 * rlSrc.lowReg now contains object->clazz. Note that
1641 * it could have been allocated r0, but we're okay so long
1642 * as we don't do anything desctructive until r0 is loaded
1643 * with clazz.
1644 */
Ben Chengba4fc8b2009-06-01 13:00:29 -07001645 /* r0 now contains object->clazz */
Bill Buzbee1465db52009-09-23 17:17:35 -07001646 loadWordDisp(cUnit, rlSrc.lowReg, offsetof(Object, clazz), r0);
Ben Chengbd1326d2010-04-02 15:04:53 -07001647 LOAD_FUNC_ADDR(cUnit, r2, (int)dvmInstanceofNonTrivial);
Bill Buzbee1465db52009-09-23 17:17:35 -07001648 opRegReg(cUnit, kOpCmp, r0, r1);
1649 ArmLIR *branch2 = opCondBranch(cUnit, kArmCondEq);
1650 opReg(cUnit, kOpBlx, r2);
Elliott Hughes6a555132010-02-25 15:41:42 -08001651 dvmCompilerClobberCallRegs(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07001652 /*
1653 * If null, check cast failed - punt to the interpreter. Because
1654 * interpreter will be the one throwing, we don't need to
1655 * genExportPC() here.
1656 */
Bill Buzbee270c1d62009-08-13 16:58:07 -07001657 genZeroCheck(cUnit, r0, mir->offset, NULL);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001658 /* check cast passed - branch target here */
Bill Buzbee1465db52009-09-23 17:17:35 -07001659 ArmLIR *target = newLIR0(cUnit, kArmPseudoTargetLabel);
Ben Chengd7d426a2009-09-22 11:23:36 -07001660 target->defMask = ENCODE_ALL;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001661 branch1->generic.target = (LIR *)target;
1662 branch2->generic.target = (LIR *)target;
1663 break;
1664 }
buzbee4d92e682010-07-29 15:24:14 -07001665 case OP_SGET_WIDE_VOLATILE:
1666 case OP_SPUT_WIDE_VOLATILE:
1667 genInterpSingleStep(cUnit, mir);
1668 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001669 default:
1670 return true;
1671 }
1672 return false;
1673}
1674
1675static bool handleFmt11x(CompilationUnit *cUnit, MIR *mir)
1676{
1677 OpCode dalvikOpCode = mir->dalvikInsn.opCode;
Bill Buzbee1465db52009-09-23 17:17:35 -07001678 RegLocation rlResult;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001679 switch (dalvikOpCode) {
1680 case OP_MOVE_EXCEPTION: {
1681 int offset = offsetof(InterpState, self);
1682 int exOffset = offsetof(Thread, exception);
Bill Buzbeec6f10662010-02-09 11:16:15 -08001683 int selfReg = dvmCompilerAllocTemp(cUnit);
1684 int resetReg = dvmCompilerAllocTemp(cUnit);
1685 RegLocation rlDest = dvmCompilerGetDest(cUnit, mir, 0);
1686 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Bill Buzbee1465db52009-09-23 17:17:35 -07001687 loadWordDisp(cUnit, rGLUE, offset, selfReg);
Bill Buzbeef9f33282009-11-22 12:45:30 -08001688 loadConstant(cUnit, resetReg, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07001689 loadWordDisp(cUnit, selfReg, exOffset, rlResult.lowReg);
Bill Buzbeef9f33282009-11-22 12:45:30 -08001690 storeWordDisp(cUnit, selfReg, exOffset, resetReg);
Bill Buzbee1465db52009-09-23 17:17:35 -07001691 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001692 break;
1693 }
1694 case OP_MOVE_RESULT:
1695 case OP_MOVE_RESULT_OBJECT: {
Bill Buzbeec6f10662010-02-09 11:16:15 -08001696 RegLocation rlDest = dvmCompilerGetDest(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07001697 RegLocation rlSrc = LOC_DALVIK_RETURN_VAL;
1698 rlSrc.fp = rlDest.fp;
1699 storeValue(cUnit, rlDest, rlSrc);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001700 break;
1701 }
1702 case OP_MOVE_RESULT_WIDE: {
Bill Buzbeec6f10662010-02-09 11:16:15 -08001703 RegLocation rlDest = dvmCompilerGetDestWide(cUnit, mir, 0, 1);
Bill Buzbee1465db52009-09-23 17:17:35 -07001704 RegLocation rlSrc = LOC_DALVIK_RETURN_VAL_WIDE;
1705 rlSrc.fp = rlDest.fp;
1706 storeValueWide(cUnit, rlDest, rlSrc);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001707 break;
1708 }
1709 case OP_RETURN_WIDE: {
Bill Buzbeec6f10662010-02-09 11:16:15 -08001710 RegLocation rlSrc = dvmCompilerGetSrcWide(cUnit, mir, 0, 1);
Bill Buzbee1465db52009-09-23 17:17:35 -07001711 RegLocation rlDest = LOC_DALVIK_RETURN_VAL_WIDE;
1712 rlDest.fp = rlSrc.fp;
1713 storeValueWide(cUnit, rlDest, rlSrc);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001714 genReturnCommon(cUnit,mir);
1715 break;
1716 }
1717 case OP_RETURN:
1718 case OP_RETURN_OBJECT: {
Bill Buzbeec6f10662010-02-09 11:16:15 -08001719 RegLocation rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07001720 RegLocation rlDest = LOC_DALVIK_RETURN_VAL;
1721 rlDest.fp = rlSrc.fp;
1722 storeValue(cUnit, rlDest, rlSrc);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001723 genReturnCommon(cUnit,mir);
1724 break;
1725 }
Bill Buzbee1465db52009-09-23 17:17:35 -07001726 case OP_MONITOR_EXIT:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001727 case OP_MONITOR_ENTER:
Bill Buzbeed0937ef2009-12-22 16:15:39 -08001728#if defined(WITH_DEADLOCK_PREDICTION) || defined(WITH_MONITOR_TRACKING)
Ben Cheng5d90c202009-11-22 23:31:11 -08001729 genMonitorPortable(cUnit, mir);
Bill Buzbee1465db52009-09-23 17:17:35 -07001730#else
Ben Cheng5d90c202009-11-22 23:31:11 -08001731 genMonitor(cUnit, mir);
Bill Buzbee1465db52009-09-23 17:17:35 -07001732#endif
Ben Chengba4fc8b2009-06-01 13:00:29 -07001733 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001734 case OP_THROW: {
1735 genInterpSingleStep(cUnit, mir);
1736 break;
1737 }
1738 default:
1739 return true;
1740 }
1741 return false;
1742}
1743
Bill Buzbeed45ba372009-06-15 17:00:57 -07001744static bool handleFmt12x(CompilationUnit *cUnit, MIR *mir)
1745{
1746 OpCode opCode = mir->dalvikInsn.opCode;
Bill Buzbee1465db52009-09-23 17:17:35 -07001747 RegLocation rlDest;
1748 RegLocation rlSrc;
1749 RegLocation rlResult;
Bill Buzbeed45ba372009-06-15 17:00:57 -07001750
Ben Chengba4fc8b2009-06-01 13:00:29 -07001751 if ( (opCode >= OP_ADD_INT_2ADDR) && (opCode <= OP_REM_DOUBLE_2ADDR)) {
Ben Cheng5d90c202009-11-22 23:31:11 -08001752 return genArithOp( cUnit, mir );
Ben Chengba4fc8b2009-06-01 13:00:29 -07001753 }
1754
Bill Buzbee1465db52009-09-23 17:17:35 -07001755 if (mir->ssaRep->numUses == 2)
Bill Buzbeec6f10662010-02-09 11:16:15 -08001756 rlSrc = dvmCompilerGetSrcWide(cUnit, mir, 0, 1);
Bill Buzbee1465db52009-09-23 17:17:35 -07001757 else
Bill Buzbeec6f10662010-02-09 11:16:15 -08001758 rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07001759 if (mir->ssaRep->numDefs == 2)
Bill Buzbeec6f10662010-02-09 11:16:15 -08001760 rlDest = dvmCompilerGetDestWide(cUnit, mir, 0, 1);
Bill Buzbee1465db52009-09-23 17:17:35 -07001761 else
Bill Buzbeec6f10662010-02-09 11:16:15 -08001762 rlDest = dvmCompilerGetDest(cUnit, mir, 0);
Ben Chenge9695e52009-06-16 16:11:47 -07001763
Ben Chengba4fc8b2009-06-01 13:00:29 -07001764 switch (opCode) {
Bill Buzbee1465db52009-09-23 17:17:35 -07001765 case OP_DOUBLE_TO_INT:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001766 case OP_INT_TO_FLOAT:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001767 case OP_FLOAT_TO_INT:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001768 case OP_DOUBLE_TO_FLOAT:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001769 case OP_FLOAT_TO_DOUBLE:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001770 case OP_INT_TO_DOUBLE:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001771 case OP_FLOAT_TO_LONG:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001772 case OP_LONG_TO_FLOAT:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001773 case OP_DOUBLE_TO_LONG:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001774 case OP_LONG_TO_DOUBLE:
Ben Cheng5d90c202009-11-22 23:31:11 -08001775 return genConversion(cUnit, mir);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001776 case OP_NEG_INT:
1777 case OP_NOT_INT:
Ben Cheng5d90c202009-11-22 23:31:11 -08001778 return genArithOpInt(cUnit, mir, rlDest, rlSrc, rlSrc);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001779 case OP_NEG_LONG:
1780 case OP_NOT_LONG:
Ben Cheng5d90c202009-11-22 23:31:11 -08001781 return genArithOpLong(cUnit, mir, rlDest, rlSrc, rlSrc);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001782 case OP_NEG_FLOAT:
Ben Cheng5d90c202009-11-22 23:31:11 -08001783 return genArithOpFloat(cUnit, mir, rlDest, rlSrc, rlSrc);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001784 case OP_NEG_DOUBLE:
Ben Cheng5d90c202009-11-22 23:31:11 -08001785 return genArithOpDouble(cUnit, mir, rlDest, rlSrc, rlSrc);
Bill Buzbee1465db52009-09-23 17:17:35 -07001786 case OP_MOVE_WIDE:
1787 storeValueWide(cUnit, rlDest, rlSrc);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001788 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07001789 case OP_INT_TO_LONG:
Bill Buzbeec6f10662010-02-09 11:16:15 -08001790 rlSrc = dvmCompilerUpdateLoc(cUnit, rlSrc);
1791 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Bill Buzbee964a7b02010-01-28 12:54:19 -08001792 //TUNING: shouldn't loadValueDirect already check for phys reg?
Bill Buzbee1465db52009-09-23 17:17:35 -07001793 if (rlSrc.location == kLocPhysReg) {
1794 genRegCopy(cUnit, rlResult.lowReg, rlSrc.lowReg);
1795 } else {
1796 loadValueDirect(cUnit, rlSrc, rlResult.lowReg);
1797 }
1798 opRegRegImm(cUnit, kOpAsr, rlResult.highReg,
1799 rlResult.lowReg, 31);
1800 storeValueWide(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001801 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07001802 case OP_LONG_TO_INT:
Bill Buzbeec6f10662010-02-09 11:16:15 -08001803 rlSrc = dvmCompilerUpdateLocWide(cUnit, rlSrc);
1804 rlSrc = dvmCompilerWideToNarrow(cUnit, rlSrc);
Bill Buzbee1465db52009-09-23 17:17:35 -07001805 // Intentional fallthrough
Ben Chengba4fc8b2009-06-01 13:00:29 -07001806 case OP_MOVE:
1807 case OP_MOVE_OBJECT:
Bill Buzbee1465db52009-09-23 17:17:35 -07001808 storeValue(cUnit, rlDest, rlSrc);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001809 break;
1810 case OP_INT_TO_BYTE:
Bill Buzbee1465db52009-09-23 17:17:35 -07001811 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
Bill Buzbeec6f10662010-02-09 11:16:15 -08001812 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Bill Buzbee1465db52009-09-23 17:17:35 -07001813 opRegReg(cUnit, kOp2Byte, rlResult.lowReg, rlSrc.lowReg);
1814 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001815 break;
1816 case OP_INT_TO_SHORT:
Bill Buzbee1465db52009-09-23 17:17:35 -07001817 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
Bill Buzbeec6f10662010-02-09 11:16:15 -08001818 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Bill Buzbee1465db52009-09-23 17:17:35 -07001819 opRegReg(cUnit, kOp2Short, rlResult.lowReg, rlSrc.lowReg);
1820 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001821 break;
1822 case OP_INT_TO_CHAR:
Bill Buzbee1465db52009-09-23 17:17:35 -07001823 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
Bill Buzbeec6f10662010-02-09 11:16:15 -08001824 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Bill Buzbee1465db52009-09-23 17:17:35 -07001825 opRegReg(cUnit, kOp2Char, rlResult.lowReg, rlSrc.lowReg);
1826 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001827 break;
1828 case OP_ARRAY_LENGTH: {
1829 int lenOffset = offsetof(ArrayObject, length);
Bill Buzbee1465db52009-09-23 17:17:35 -07001830 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
1831 genNullCheck(cUnit, rlSrc.sRegLow, rlSrc.lowReg,
1832 mir->offset, NULL);
Bill Buzbeec6f10662010-02-09 11:16:15 -08001833 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Bill Buzbee1465db52009-09-23 17:17:35 -07001834 loadWordDisp(cUnit, rlSrc.lowReg, lenOffset,
1835 rlResult.lowReg);
1836 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001837 break;
1838 }
1839 default:
1840 return true;
1841 }
1842 return false;
1843}
1844
1845static bool handleFmt21s(CompilationUnit *cUnit, MIR *mir)
1846{
1847 OpCode dalvikOpCode = mir->dalvikInsn.opCode;
Bill Buzbee1465db52009-09-23 17:17:35 -07001848 RegLocation rlDest;
1849 RegLocation rlResult;
1850 int BBBB = mir->dalvikInsn.vB;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001851 if (dalvikOpCode == OP_CONST_WIDE_16) {
Bill Buzbeec6f10662010-02-09 11:16:15 -08001852 rlDest = dvmCompilerGetDestWide(cUnit, mir, 0, 1);
1853 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Ben Chengbd1326d2010-04-02 15:04:53 -07001854 loadConstantNoClobber(cUnit, rlResult.lowReg, BBBB);
Bill Buzbee964a7b02010-01-28 12:54:19 -08001855 //TUNING: do high separately to avoid load dependency
Bill Buzbee1465db52009-09-23 17:17:35 -07001856 opRegRegImm(cUnit, kOpAsr, rlResult.highReg, rlResult.lowReg, 31);
1857 storeValueWide(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001858 } else if (dalvikOpCode == OP_CONST_16) {
Bill Buzbeec6f10662010-02-09 11:16:15 -08001859 rlDest = dvmCompilerGetDest(cUnit, mir, 0);
1860 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kAnyReg, true);
Ben Chengbd1326d2010-04-02 15:04:53 -07001861 loadConstantNoClobber(cUnit, rlResult.lowReg, BBBB);
Bill Buzbee1465db52009-09-23 17:17:35 -07001862 storeValue(cUnit, rlDest, rlResult);
1863 } else
Ben Chengba4fc8b2009-06-01 13:00:29 -07001864 return true;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001865 return false;
1866}
1867
1868/* Compare agaist zero */
1869static bool handleFmt21t(CompilationUnit *cUnit, MIR *mir, BasicBlock *bb,
Bill Buzbee89efc3d2009-07-28 11:22:22 -07001870 ArmLIR *labelList)
Ben Chengba4fc8b2009-06-01 13:00:29 -07001871{
1872 OpCode dalvikOpCode = mir->dalvikInsn.opCode;
Bill Buzbee89efc3d2009-07-28 11:22:22 -07001873 ArmConditionCode cond;
Bill Buzbeec6f10662010-02-09 11:16:15 -08001874 RegLocation rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07001875 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
1876 opRegImm(cUnit, kOpCmp, rlSrc.lowReg, 0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001877
Bill Buzbee270c1d62009-08-13 16:58:07 -07001878//TUNING: break this out to allow use of Thumb2 CB[N]Z
Ben Chengba4fc8b2009-06-01 13:00:29 -07001879 switch (dalvikOpCode) {
1880 case OP_IF_EQZ:
Bill Buzbee1465db52009-09-23 17:17:35 -07001881 cond = kArmCondEq;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001882 break;
1883 case OP_IF_NEZ:
Bill Buzbee1465db52009-09-23 17:17:35 -07001884 cond = kArmCondNe;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001885 break;
1886 case OP_IF_LTZ:
Bill Buzbee1465db52009-09-23 17:17:35 -07001887 cond = kArmCondLt;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001888 break;
1889 case OP_IF_GEZ:
Bill Buzbee1465db52009-09-23 17:17:35 -07001890 cond = kArmCondGe;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001891 break;
1892 case OP_IF_GTZ:
Bill Buzbee1465db52009-09-23 17:17:35 -07001893 cond = kArmCondGt;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001894 break;
1895 case OP_IF_LEZ:
Bill Buzbee1465db52009-09-23 17:17:35 -07001896 cond = kArmCondLe;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001897 break;
1898 default:
1899 cond = 0;
1900 LOGE("Unexpected opcode (%d) for Fmt21t\n", dalvikOpCode);
Bill Buzbeefc519dc2010-03-06 23:30:57 -08001901 dvmCompilerAbort(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001902 }
1903 genConditionalBranch(cUnit, cond, &labelList[bb->taken->id]);
1904 /* This mostly likely will be optimized away in a later phase */
1905 genUnconditionalBranch(cUnit, &labelList[bb->fallThrough->id]);
1906 return false;
1907}
1908
Elliott Hughesb4c05972010-02-24 16:36:18 -08001909static bool isPowerOfTwo(int x)
1910{
1911 return (x & (x - 1)) == 0;
1912}
1913
1914// Returns true if no more than two bits are set in 'x'.
1915static bool isPopCountLE2(unsigned int x)
1916{
1917 x &= x - 1;
1918 return (x & (x - 1)) == 0;
1919}
1920
1921// Returns the index of the lowest set bit in 'x'.
1922static int lowestSetBit(unsigned int x) {
1923 int bit_posn = 0;
1924 while ((x & 0xf) == 0) {
1925 bit_posn += 4;
1926 x >>= 4;
Bill Buzbee78cb0e22010-02-11 14:04:53 -08001927 }
Elliott Hughesb4c05972010-02-24 16:36:18 -08001928 while ((x & 1) == 0) {
1929 bit_posn++;
1930 x >>= 1;
1931 }
1932 return bit_posn;
1933}
1934
Elliott Hughes672511b2010-04-26 17:40:13 -07001935// Returns true if it added instructions to 'cUnit' to divide 'rlSrc' by 'lit'
1936// and store the result in 'rlDest'.
Elliott Hughesc7ad9b22010-04-28 13:52:02 -07001937static bool handleEasyDivide(CompilationUnit *cUnit, OpCode dalvikOpCode,
Elliott Hughes672511b2010-04-26 17:40:13 -07001938 RegLocation rlSrc, RegLocation rlDest, int lit)
1939{
1940 if (lit < 2 || !isPowerOfTwo(lit)) {
1941 return false;
1942 }
1943 int k = lowestSetBit(lit);
1944 if (k >= 30) {
1945 // Avoid special cases.
1946 return false;
1947 }
Elliott Hughes9c457022010-04-28 16:15:38 -07001948 bool div = (dalvikOpCode == OP_DIV_INT_LIT8 || dalvikOpCode == OP_DIV_INT_LIT16);
Elliott Hughes672511b2010-04-26 17:40:13 -07001949 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
1950 RegLocation rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Elliott Hughes9c457022010-04-28 16:15:38 -07001951 if (div) {
1952 int tReg = dvmCompilerAllocTemp(cUnit);
1953 if (lit == 2) {
1954 // Division by 2 is by far the most common division by constant.
1955 opRegRegImm(cUnit, kOpLsr, tReg, rlSrc.lowReg, 32 - k);
1956 opRegRegReg(cUnit, kOpAdd, tReg, tReg, rlSrc.lowReg);
1957 opRegRegImm(cUnit, kOpAsr, rlResult.lowReg, tReg, k);
1958 } else {
1959 opRegRegImm(cUnit, kOpAsr, tReg, rlSrc.lowReg, 31);
1960 opRegRegImm(cUnit, kOpLsr, tReg, tReg, 32 - k);
1961 opRegRegReg(cUnit, kOpAdd, tReg, tReg, rlSrc.lowReg);
1962 opRegRegImm(cUnit, kOpAsr, rlResult.lowReg, tReg, k);
1963 }
Elliott Hughes672511b2010-04-26 17:40:13 -07001964 } else {
Elliott Hughes9c457022010-04-28 16:15:38 -07001965 int cReg = dvmCompilerAllocTemp(cUnit);
1966 loadConstant(cUnit, cReg, lit - 1);
1967 int tReg1 = dvmCompilerAllocTemp(cUnit);
1968 int tReg2 = dvmCompilerAllocTemp(cUnit);
1969 if (lit == 2) {
1970 opRegRegImm(cUnit, kOpLsr, tReg1, rlSrc.lowReg, 32 - k);
1971 opRegRegReg(cUnit, kOpAdd, tReg2, tReg1, rlSrc.lowReg);
1972 opRegRegReg(cUnit, kOpAnd, tReg2, tReg2, cReg);
1973 opRegRegReg(cUnit, kOpSub, rlResult.lowReg, tReg2, tReg1);
1974 } else {
1975 opRegRegImm(cUnit, kOpAsr, tReg1, rlSrc.lowReg, 31);
1976 opRegRegImm(cUnit, kOpLsr, tReg1, tReg1, 32 - k);
1977 opRegRegReg(cUnit, kOpAdd, tReg2, tReg1, rlSrc.lowReg);
1978 opRegRegReg(cUnit, kOpAnd, tReg2, tReg2, cReg);
1979 opRegRegReg(cUnit, kOpSub, rlResult.lowReg, tReg2, tReg1);
1980 }
Elliott Hughes672511b2010-04-26 17:40:13 -07001981 }
1982 storeValue(cUnit, rlDest, rlResult);
1983 return true;
1984}
1985
Elliott Hughesb4c05972010-02-24 16:36:18 -08001986// Returns true if it added instructions to 'cUnit' to multiply 'rlSrc' by 'lit'
1987// and store the result in 'rlDest'.
1988static bool handleEasyMultiply(CompilationUnit *cUnit,
1989 RegLocation rlSrc, RegLocation rlDest, int lit)
1990{
1991 // Can we simplify this multiplication?
1992 bool powerOfTwo = false;
1993 bool popCountLE2 = false;
1994 bool powerOfTwoMinusOne = false;
1995 if (lit < 2) {
1996 // Avoid special cases.
1997 return false;
1998 } else if (isPowerOfTwo(lit)) {
1999 powerOfTwo = true;
2000 } else if (isPopCountLE2(lit)) {
2001 popCountLE2 = true;
2002 } else if (isPowerOfTwo(lit + 1)) {
2003 powerOfTwoMinusOne = true;
2004 } else {
2005 return false;
2006 }
2007 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
2008 RegLocation rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
2009 if (powerOfTwo) {
2010 // Shift.
2011 opRegRegImm(cUnit, kOpLsl, rlResult.lowReg, rlSrc.lowReg,
2012 lowestSetBit(lit));
2013 } else if (popCountLE2) {
2014 // Shift and add and shift.
2015 int firstBit = lowestSetBit(lit);
2016 int secondBit = lowestSetBit(lit ^ (1 << firstBit));
2017 genMultiplyByTwoBitMultiplier(cUnit, rlSrc, rlResult, lit,
2018 firstBit, secondBit);
2019 } else {
2020 // Reverse subtract: (src << (shift + 1)) - src.
2021 assert(powerOfTwoMinusOne);
2022 // TODO: rsb dst, src, src lsl#lowestSetBit(lit + 1)
2023 int tReg = dvmCompilerAllocTemp(cUnit);
2024 opRegRegImm(cUnit, kOpLsl, tReg, rlSrc.lowReg, lowestSetBit(lit + 1));
2025 opRegRegReg(cUnit, kOpSub, rlResult.lowReg, tReg, rlSrc.lowReg);
2026 }
2027 storeValue(cUnit, rlDest, rlResult);
2028 return true;
Bill Buzbee78cb0e22010-02-11 14:04:53 -08002029}
2030
Ben Chengba4fc8b2009-06-01 13:00:29 -07002031static bool handleFmt22b_Fmt22s(CompilationUnit *cUnit, MIR *mir)
2032{
2033 OpCode dalvikOpCode = mir->dalvikInsn.opCode;
Bill Buzbeec6f10662010-02-09 11:16:15 -08002034 RegLocation rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
2035 RegLocation rlDest = dvmCompilerGetDest(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07002036 RegLocation rlResult;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002037 int lit = mir->dalvikInsn.vC;
Ben Cheng4f489172009-09-27 17:08:35 -07002038 OpKind op = 0; /* Make gcc happy */
Bill Buzbee1465db52009-09-23 17:17:35 -07002039 int shiftOp = false;
2040 bool isDiv = false;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002041
Ben Chengba4fc8b2009-06-01 13:00:29 -07002042 switch (dalvikOpCode) {
Bill Buzbee1465db52009-09-23 17:17:35 -07002043 case OP_RSUB_INT_LIT8:
2044 case OP_RSUB_INT: {
2045 int tReg;
2046 //TUNING: add support for use of Arm rsub op
2047 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
Bill Buzbeec6f10662010-02-09 11:16:15 -08002048 tReg = dvmCompilerAllocTemp(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07002049 loadConstant(cUnit, tReg, lit);
Bill Buzbeec6f10662010-02-09 11:16:15 -08002050 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Bill Buzbee1465db52009-09-23 17:17:35 -07002051 opRegRegReg(cUnit, kOpSub, rlResult.lowReg,
2052 tReg, rlSrc.lowReg);
2053 storeValue(cUnit, rlDest, rlResult);
2054 return false;
2055 break;
2056 }
2057
Ben Chengba4fc8b2009-06-01 13:00:29 -07002058 case OP_ADD_INT_LIT8:
2059 case OP_ADD_INT_LIT16:
Bill Buzbee1465db52009-09-23 17:17:35 -07002060 op = kOpAdd;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002061 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002062 case OP_MUL_INT_LIT8:
Bill Buzbee78cb0e22010-02-11 14:04:53 -08002063 case OP_MUL_INT_LIT16: {
Elliott Hughesb4c05972010-02-24 16:36:18 -08002064 if (handleEasyMultiply(cUnit, rlSrc, rlDest, lit)) {
2065 return false;
Bill Buzbee78cb0e22010-02-11 14:04:53 -08002066 }
Elliott Hughesb4c05972010-02-24 16:36:18 -08002067 op = kOpMul;
Bill Buzbee1465db52009-09-23 17:17:35 -07002068 break;
Bill Buzbee78cb0e22010-02-11 14:04:53 -08002069 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07002070 case OP_AND_INT_LIT8:
2071 case OP_AND_INT_LIT16:
Bill Buzbee1465db52009-09-23 17:17:35 -07002072 op = kOpAnd;
2073 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002074 case OP_OR_INT_LIT8:
2075 case OP_OR_INT_LIT16:
Bill Buzbee1465db52009-09-23 17:17:35 -07002076 op = kOpOr;
2077 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002078 case OP_XOR_INT_LIT8:
2079 case OP_XOR_INT_LIT16:
Bill Buzbee1465db52009-09-23 17:17:35 -07002080 op = kOpXor;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002081 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002082 case OP_SHL_INT_LIT8:
Bill Buzbee0e605272009-12-01 14:28:05 -08002083 lit &= 31;
Bill Buzbee1465db52009-09-23 17:17:35 -07002084 shiftOp = true;
2085 op = kOpLsl;
2086 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002087 case OP_SHR_INT_LIT8:
Bill Buzbee0e605272009-12-01 14:28:05 -08002088 lit &= 31;
Bill Buzbee1465db52009-09-23 17:17:35 -07002089 shiftOp = true;
2090 op = kOpAsr;
2091 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002092 case OP_USHR_INT_LIT8:
Bill Buzbee0e605272009-12-01 14:28:05 -08002093 lit &= 31;
Bill Buzbee1465db52009-09-23 17:17:35 -07002094 shiftOp = true;
2095 op = kOpLsr;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002096 break;
2097
2098 case OP_DIV_INT_LIT8:
2099 case OP_DIV_INT_LIT16:
Ben Chengba4fc8b2009-06-01 13:00:29 -07002100 case OP_REM_INT_LIT8:
2101 case OP_REM_INT_LIT16:
2102 if (lit == 0) {
2103 /* Let the interpreter deal with div by 0 */
2104 genInterpSingleStep(cUnit, mir);
2105 return false;
2106 }
Elliott Hughesc7ad9b22010-04-28 13:52:02 -07002107 if (handleEasyDivide(cUnit, dalvikOpCode, rlSrc, rlDest, lit)) {
Elliott Hughes672511b2010-04-26 17:40:13 -07002108 return false;
2109 }
Bill Buzbeec6f10662010-02-09 11:16:15 -08002110 dvmCompilerFlushAllRegs(cUnit); /* Everything to home location */
Bill Buzbee1465db52009-09-23 17:17:35 -07002111 loadValueDirectFixed(cUnit, rlSrc, r0);
Bill Buzbeec6f10662010-02-09 11:16:15 -08002112 dvmCompilerClobber(cUnit, r0);
Bill Buzbee1465db52009-09-23 17:17:35 -07002113 if ((dalvikOpCode == OP_DIV_INT_LIT8) ||
2114 (dalvikOpCode == OP_DIV_INT_LIT16)) {
Ben Chengbd1326d2010-04-02 15:04:53 -07002115 LOAD_FUNC_ADDR(cUnit, r2, (int)__aeabi_idiv);
Bill Buzbee1465db52009-09-23 17:17:35 -07002116 isDiv = true;
2117 } else {
Ben Chengbd1326d2010-04-02 15:04:53 -07002118 LOAD_FUNC_ADDR(cUnit, r2, (int)__aeabi_idivmod);
Bill Buzbee1465db52009-09-23 17:17:35 -07002119 isDiv = false;
2120 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07002121 loadConstant(cUnit, r1, lit);
Bill Buzbee1465db52009-09-23 17:17:35 -07002122 opReg(cUnit, kOpBlx, r2);
Elliott Hughes6a555132010-02-25 15:41:42 -08002123 dvmCompilerClobberCallRegs(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07002124 if (isDiv)
Bill Buzbeec6f10662010-02-09 11:16:15 -08002125 rlResult = dvmCompilerGetReturn(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07002126 else
Bill Buzbeec6f10662010-02-09 11:16:15 -08002127 rlResult = dvmCompilerGetReturnAlt(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07002128 storeValue(cUnit, rlDest, rlResult);
2129 return false;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002130 break;
2131 default:
2132 return true;
2133 }
Bill Buzbee1465db52009-09-23 17:17:35 -07002134 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
Bill Buzbeec6f10662010-02-09 11:16:15 -08002135 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Bill Buzbee1465db52009-09-23 17:17:35 -07002136 // Avoid shifts by literal 0 - no support in Thumb. Change to copy
2137 if (shiftOp && (lit == 0)) {
2138 genRegCopy(cUnit, rlResult.lowReg, rlSrc.lowReg);
2139 } else {
2140 opRegRegImm(cUnit, op, rlResult.lowReg, rlSrc.lowReg, lit);
2141 }
2142 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002143 return false;
2144}
2145
2146static bool handleFmt22c(CompilationUnit *cUnit, MIR *mir)
2147{
2148 OpCode dalvikOpCode = mir->dalvikInsn.opCode;
buzbee4d92e682010-07-29 15:24:14 -07002149 int fieldOffset = -1;
buzbeeecf8f6e2010-07-20 14:53:42 -07002150 bool isVolatile = false;
buzbee4d92e682010-07-29 15:24:14 -07002151 switch (dalvikOpCode) {
2152 /*
2153 * Wide volatiles currently handled via single step.
2154 * Add them here if generating in-line code.
2155 * case OP_IGET_WIDE_VOLATILE:
2156 * case OP_IPUT_WIDE_VOLATILE:
2157 */
2158 case OP_IGET:
2159 case OP_IGET_VOLATILE:
2160 case OP_IGET_WIDE:
2161 case OP_IGET_OBJECT:
2162 case OP_IGET_OBJECT_VOLATILE:
2163 case OP_IGET_BOOLEAN:
2164 case OP_IGET_BYTE:
2165 case OP_IGET_CHAR:
2166 case OP_IGET_SHORT:
2167 case OP_IPUT:
2168 case OP_IPUT_VOLATILE:
2169 case OP_IPUT_WIDE:
2170 case OP_IPUT_OBJECT:
2171 case OP_IPUT_OBJECT_VOLATILE:
2172 case OP_IPUT_BOOLEAN:
2173 case OP_IPUT_BYTE:
2174 case OP_IPUT_CHAR:
2175 case OP_IPUT_SHORT: {
2176 Field *fieldPtr =
2177 cUnit->method->clazz->pDvmDex->pResFields[mir->dalvikInsn.vC];
Ben Chengba4fc8b2009-06-01 13:00:29 -07002178
buzbee4d92e682010-07-29 15:24:14 -07002179 if (fieldPtr == NULL) {
2180 LOGE("Unexpected null instance field");
2181 dvmAbort();
2182 }
2183 isVolatile = dvmIsVolatileField(fieldPtr);
2184 fieldOffset = ((InstField *)fieldPtr)->byteOffset;
2185 break;
Ben Chengdd6e8702010-05-07 13:05:47 -07002186 }
buzbee4d92e682010-07-29 15:24:14 -07002187 default:
2188 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002189 }
buzbee4d92e682010-07-29 15:24:14 -07002190
Ben Chengba4fc8b2009-06-01 13:00:29 -07002191 switch (dalvikOpCode) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07002192 case OP_NEW_ARRAY: {
Bill Buzbee1465db52009-09-23 17:17:35 -07002193 // Generates a call - use explicit registers
Bill Buzbeec6f10662010-02-09 11:16:15 -08002194 RegLocation rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
2195 RegLocation rlDest = dvmCompilerGetDest(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07002196 RegLocation rlResult;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002197 void *classPtr = (void*)
2198 (cUnit->method->clazz->pDvmDex->pResClasses[mir->dalvikInsn.vC]);
Ben Chengdd6e8702010-05-07 13:05:47 -07002199
2200 if (classPtr == NULL) {
2201 LOGE("Unexpected null class");
2202 dvmAbort();
2203 }
2204
Bill Buzbeec6f10662010-02-09 11:16:15 -08002205 dvmCompilerFlushAllRegs(cUnit); /* Everything to home location */
Bill Buzbee1465db52009-09-23 17:17:35 -07002206 genExportPC(cUnit, mir);
2207 loadValueDirectFixed(cUnit, rlSrc, r1); /* Len */
Ben Chengba4fc8b2009-06-01 13:00:29 -07002208 loadConstant(cUnit, r0, (int) classPtr );
Ben Chengbd1326d2010-04-02 15:04:53 -07002209 LOAD_FUNC_ADDR(cUnit, r3, (int)dvmAllocArrayByClass);
Ben Cheng4f489172009-09-27 17:08:35 -07002210 /*
2211 * "len < 0": bail to the interpreter to re-execute the
2212 * instruction
2213 */
Carl Shapiroe3c01da2010-05-20 22:54:18 -07002214 genRegImmCheck(cUnit, kArmCondMi, r1, 0, mir->offset, NULL);
Bill Buzbee270c1d62009-08-13 16:58:07 -07002215 loadConstant(cUnit, r2, ALLOC_DONT_TRACK);
Bill Buzbee1465db52009-09-23 17:17:35 -07002216 opReg(cUnit, kOpBlx, r3);
Elliott Hughes6a555132010-02-25 15:41:42 -08002217 dvmCompilerClobberCallRegs(cUnit);
Ben Cheng4f489172009-09-27 17:08:35 -07002218 /* generate a branch over if allocation is successful */
Bill Buzbee1465db52009-09-23 17:17:35 -07002219 opRegImm(cUnit, kOpCmp, r0, 0); /* NULL? */
2220 ArmLIR *branchOver = opCondBranch(cUnit, kArmCondNe);
Ben Cheng4f489172009-09-27 17:08:35 -07002221 /*
2222 * OOM exception needs to be thrown here and cannot re-execute
2223 */
2224 loadConstant(cUnit, r0,
2225 (int) (cUnit->method->insns + mir->offset));
2226 genDispatchToHandler(cUnit, TEMPLATE_THROW_EXCEPTION_COMMON);
2227 /* noreturn */
2228
Bill Buzbee1465db52009-09-23 17:17:35 -07002229 ArmLIR *target = newLIR0(cUnit, kArmPseudoTargetLabel);
Ben Cheng4f489172009-09-27 17:08:35 -07002230 target->defMask = ENCODE_ALL;
2231 branchOver->generic.target = (LIR *) target;
Bill Buzbeec6f10662010-02-09 11:16:15 -08002232 rlResult = dvmCompilerGetReturn(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07002233 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002234 break;
2235 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07002236 case OP_INSTANCE_OF: {
Bill Buzbee1465db52009-09-23 17:17:35 -07002237 // May generate a call - use explicit registers
Bill Buzbeec6f10662010-02-09 11:16:15 -08002238 RegLocation rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
2239 RegLocation rlDest = dvmCompilerGetDest(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07002240 RegLocation rlResult;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002241 ClassObject *classPtr =
2242 (cUnit->method->clazz->pDvmDex->pResClasses[mir->dalvikInsn.vC]);
Bill Buzbee480e6782010-01-27 15:43:08 -08002243 /*
2244 * Note: It is possible that classPtr is NULL at this point,
2245 * even though this instruction has been successfully interpreted.
2246 * If the previous interpretation had a null source, the
2247 * interpreter would not have bothered to resolve the clazz.
2248 * Bail out to the interpreter in this case, and log it
2249 * so that we can tell if it happens frequently.
2250 */
2251 if (classPtr == NULL) {
2252 LOGD("null clazz in OP_INSTANCE_OF, single-stepping");
2253 genInterpSingleStep(cUnit, mir);
2254 break;
2255 }
Bill Buzbeec6f10662010-02-09 11:16:15 -08002256 dvmCompilerFlushAllRegs(cUnit); /* Everything to home location */
Bill Buzbee1465db52009-09-23 17:17:35 -07002257 loadValueDirectFixed(cUnit, rlSrc, r0); /* Ref */
Ben Chengba4fc8b2009-06-01 13:00:29 -07002258 loadConstant(cUnit, r2, (int) classPtr );
Bill Buzbee270c1d62009-08-13 16:58:07 -07002259//TUNING: compare to 0 primative to allow use of CB[N]Z
Bill Buzbee1465db52009-09-23 17:17:35 -07002260 opRegImm(cUnit, kOpCmp, r0, 0); /* NULL? */
Ben Cheng752c7942009-06-22 10:50:07 -07002261 /* When taken r0 has NULL which can be used for store directly */
Bill Buzbee1465db52009-09-23 17:17:35 -07002262 ArmLIR *branch1 = opCondBranch(cUnit, kArmCondEq);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002263 /* r1 now contains object->clazz */
Bill Buzbee270c1d62009-08-13 16:58:07 -07002264 loadWordDisp(cUnit, r0, offsetof(Object, clazz), r1);
Bill Buzbee1465db52009-09-23 17:17:35 -07002265 /* r1 now contains object->clazz */
Ben Chengbd1326d2010-04-02 15:04:53 -07002266 LOAD_FUNC_ADDR(cUnit, r3, (int)dvmInstanceofNonTrivial);
Ben Cheng752c7942009-06-22 10:50:07 -07002267 loadConstant(cUnit, r0, 1); /* Assume true */
Bill Buzbee1465db52009-09-23 17:17:35 -07002268 opRegReg(cUnit, kOpCmp, r1, r2);
2269 ArmLIR *branch2 = opCondBranch(cUnit, kArmCondEq);
2270 genRegCopy(cUnit, r0, r1);
2271 genRegCopy(cUnit, r1, r2);
2272 opReg(cUnit, kOpBlx, r3);
Elliott Hughes6a555132010-02-25 15:41:42 -08002273 dvmCompilerClobberCallRegs(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002274 /* branch target here */
Bill Buzbee1465db52009-09-23 17:17:35 -07002275 ArmLIR *target = newLIR0(cUnit, kArmPseudoTargetLabel);
Ben Chengd7d426a2009-09-22 11:23:36 -07002276 target->defMask = ENCODE_ALL;
Bill Buzbeec6f10662010-02-09 11:16:15 -08002277 rlResult = dvmCompilerGetReturn(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07002278 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002279 branch1->generic.target = (LIR *)target;
2280 branch2->generic.target = (LIR *)target;
2281 break;
2282 }
2283 case OP_IGET_WIDE:
2284 genIGetWide(cUnit, mir, fieldOffset);
2285 break;
buzbeeecf8f6e2010-07-20 14:53:42 -07002286 case OP_IGET_VOLATILE:
2287 case OP_IGET_OBJECT_VOLATILE:
2288 isVolatile = true;
2289 // NOTE: intentional fallthrough
Ben Chengba4fc8b2009-06-01 13:00:29 -07002290 case OP_IGET:
2291 case OP_IGET_OBJECT:
buzbeeecf8f6e2010-07-20 14:53:42 -07002292 genIGet(cUnit, mir, kWord, fieldOffset, isVolatile);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002293 break;
2294 case OP_IGET_BOOLEAN:
buzbeeecf8f6e2010-07-20 14:53:42 -07002295 genIGet(cUnit, mir, kUnsignedByte, fieldOffset, isVolatile);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002296 break;
2297 case OP_IGET_BYTE:
buzbeeecf8f6e2010-07-20 14:53:42 -07002298 genIGet(cUnit, mir, kSignedByte, fieldOffset, isVolatile);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002299 break;
2300 case OP_IGET_CHAR:
buzbeeecf8f6e2010-07-20 14:53:42 -07002301 genIGet(cUnit, mir, kUnsignedHalf, fieldOffset, isVolatile);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002302 break;
2303 case OP_IGET_SHORT:
buzbeeecf8f6e2010-07-20 14:53:42 -07002304 genIGet(cUnit, mir, kSignedHalf, fieldOffset, isVolatile);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002305 break;
2306 case OP_IPUT_WIDE:
2307 genIPutWide(cUnit, mir, fieldOffset);
2308 break;
2309 case OP_IPUT:
buzbeeecf8f6e2010-07-20 14:53:42 -07002310 genIPut(cUnit, mir, kWord, fieldOffset, false, isVolatile);
buzbee919eb062010-07-12 12:59:22 -07002311 break;
buzbee4d92e682010-07-29 15:24:14 -07002312 case OP_IPUT_VOLATILE:
buzbeeecf8f6e2010-07-20 14:53:42 -07002313 case OP_IPUT_OBJECT_VOLATILE:
2314 isVolatile = true;
2315 // NOTE: intentional fallthrough
Ben Chengba4fc8b2009-06-01 13:00:29 -07002316 case OP_IPUT_OBJECT:
buzbeeecf8f6e2010-07-20 14:53:42 -07002317 genIPut(cUnit, mir, kWord, fieldOffset, true, isVolatile);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002318 break;
2319 case OP_IPUT_SHORT:
2320 case OP_IPUT_CHAR:
buzbeeecf8f6e2010-07-20 14:53:42 -07002321 genIPut(cUnit, mir, kUnsignedHalf, fieldOffset, false, isVolatile);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002322 break;
2323 case OP_IPUT_BYTE:
buzbeeecf8f6e2010-07-20 14:53:42 -07002324 genIPut(cUnit, mir, kSignedByte, fieldOffset, false, isVolatile);
2325 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002326 case OP_IPUT_BOOLEAN:
buzbeeecf8f6e2010-07-20 14:53:42 -07002327 genIPut(cUnit, mir, kUnsignedByte, fieldOffset, false, isVolatile);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002328 break;
Bill Buzbeeb16344a2010-03-15 17:19:12 -07002329 case OP_IGET_WIDE_VOLATILE:
2330 case OP_IPUT_WIDE_VOLATILE:
Bill Buzbeeb16344a2010-03-15 17:19:12 -07002331 genInterpSingleStep(cUnit, mir);
2332 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002333 default:
2334 return true;
2335 }
2336 return false;
2337}
2338
2339static bool handleFmt22cs(CompilationUnit *cUnit, MIR *mir)
2340{
2341 OpCode dalvikOpCode = mir->dalvikInsn.opCode;
2342 int fieldOffset = mir->dalvikInsn.vC;
2343 switch (dalvikOpCode) {
2344 case OP_IGET_QUICK:
2345 case OP_IGET_OBJECT_QUICK:
buzbeeecf8f6e2010-07-20 14:53:42 -07002346 genIGet(cUnit, mir, kWord, fieldOffset, false);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002347 break;
2348 case OP_IPUT_QUICK:
buzbeeecf8f6e2010-07-20 14:53:42 -07002349 genIPut(cUnit, mir, kWord, fieldOffset, false, false);
buzbee919eb062010-07-12 12:59:22 -07002350 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002351 case OP_IPUT_OBJECT_QUICK:
buzbeeecf8f6e2010-07-20 14:53:42 -07002352 genIPut(cUnit, mir, kWord, fieldOffset, true, false);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002353 break;
2354 case OP_IGET_WIDE_QUICK:
2355 genIGetWide(cUnit, mir, fieldOffset);
2356 break;
2357 case OP_IPUT_WIDE_QUICK:
2358 genIPutWide(cUnit, mir, fieldOffset);
2359 break;
2360 default:
2361 return true;
2362 }
2363 return false;
2364
2365}
2366
2367/* Compare agaist zero */
2368static bool handleFmt22t(CompilationUnit *cUnit, MIR *mir, BasicBlock *bb,
Bill Buzbee89efc3d2009-07-28 11:22:22 -07002369 ArmLIR *labelList)
Ben Chengba4fc8b2009-06-01 13:00:29 -07002370{
2371 OpCode dalvikOpCode = mir->dalvikInsn.opCode;
Bill Buzbee89efc3d2009-07-28 11:22:22 -07002372 ArmConditionCode cond;
Bill Buzbeec6f10662010-02-09 11:16:15 -08002373 RegLocation rlSrc1 = dvmCompilerGetSrc(cUnit, mir, 0);
2374 RegLocation rlSrc2 = dvmCompilerGetSrc(cUnit, mir, 1);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002375
Bill Buzbee1465db52009-09-23 17:17:35 -07002376 rlSrc1 = loadValue(cUnit, rlSrc1, kCoreReg);
2377 rlSrc2 = loadValue(cUnit, rlSrc2, kCoreReg);
2378 opRegReg(cUnit, kOpCmp, rlSrc1.lowReg, rlSrc2.lowReg);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002379
2380 switch (dalvikOpCode) {
2381 case OP_IF_EQ:
Bill Buzbee1465db52009-09-23 17:17:35 -07002382 cond = kArmCondEq;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002383 break;
2384 case OP_IF_NE:
Bill Buzbee1465db52009-09-23 17:17:35 -07002385 cond = kArmCondNe;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002386 break;
2387 case OP_IF_LT:
Bill Buzbee1465db52009-09-23 17:17:35 -07002388 cond = kArmCondLt;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002389 break;
2390 case OP_IF_GE:
Bill Buzbee1465db52009-09-23 17:17:35 -07002391 cond = kArmCondGe;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002392 break;
2393 case OP_IF_GT:
Bill Buzbee1465db52009-09-23 17:17:35 -07002394 cond = kArmCondGt;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002395 break;
2396 case OP_IF_LE:
Bill Buzbee1465db52009-09-23 17:17:35 -07002397 cond = kArmCondLe;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002398 break;
2399 default:
2400 cond = 0;
2401 LOGE("Unexpected opcode (%d) for Fmt22t\n", dalvikOpCode);
Bill Buzbeefc519dc2010-03-06 23:30:57 -08002402 dvmCompilerAbort(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002403 }
2404 genConditionalBranch(cUnit, cond, &labelList[bb->taken->id]);
2405 /* This mostly likely will be optimized away in a later phase */
2406 genUnconditionalBranch(cUnit, &labelList[bb->fallThrough->id]);
2407 return false;
2408}
2409
2410static bool handleFmt22x_Fmt32x(CompilationUnit *cUnit, MIR *mir)
2411{
2412 OpCode opCode = mir->dalvikInsn.opCode;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002413
2414 switch (opCode) {
2415 case OP_MOVE_16:
2416 case OP_MOVE_OBJECT_16:
2417 case OP_MOVE_FROM16:
Ben Chenge9695e52009-06-16 16:11:47 -07002418 case OP_MOVE_OBJECT_FROM16: {
Bill Buzbeec6f10662010-02-09 11:16:15 -08002419 storeValue(cUnit, dvmCompilerGetDest(cUnit, mir, 0),
2420 dvmCompilerGetSrc(cUnit, mir, 0));
Ben Chengba4fc8b2009-06-01 13:00:29 -07002421 break;
Ben Chenge9695e52009-06-16 16:11:47 -07002422 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07002423 case OP_MOVE_WIDE_16:
Ben Chenge9695e52009-06-16 16:11:47 -07002424 case OP_MOVE_WIDE_FROM16: {
Bill Buzbeec6f10662010-02-09 11:16:15 -08002425 storeValueWide(cUnit, dvmCompilerGetDestWide(cUnit, mir, 0, 1),
2426 dvmCompilerGetSrcWide(cUnit, mir, 0, 1));
Ben Chengba4fc8b2009-06-01 13:00:29 -07002427 break;
Ben Chenge9695e52009-06-16 16:11:47 -07002428 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07002429 default:
2430 return true;
2431 }
2432 return false;
2433}
2434
2435static bool handleFmt23x(CompilationUnit *cUnit, MIR *mir)
2436{
2437 OpCode opCode = mir->dalvikInsn.opCode;
Bill Buzbee1465db52009-09-23 17:17:35 -07002438 RegLocation rlSrc1;
2439 RegLocation rlSrc2;
2440 RegLocation rlDest;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002441
2442 if ( (opCode >= OP_ADD_INT) && (opCode <= OP_REM_DOUBLE)) {
Ben Cheng5d90c202009-11-22 23:31:11 -08002443 return genArithOp( cUnit, mir );
Ben Chengba4fc8b2009-06-01 13:00:29 -07002444 }
2445
Bill Buzbee1465db52009-09-23 17:17:35 -07002446 /* APUTs have 3 sources and no targets */
2447 if (mir->ssaRep->numDefs == 0) {
2448 if (mir->ssaRep->numUses == 3) {
Bill Buzbeec6f10662010-02-09 11:16:15 -08002449 rlDest = dvmCompilerGetSrc(cUnit, mir, 0);
2450 rlSrc1 = dvmCompilerGetSrc(cUnit, mir, 1);
2451 rlSrc2 = dvmCompilerGetSrc(cUnit, mir, 2);
Bill Buzbee1465db52009-09-23 17:17:35 -07002452 } else {
2453 assert(mir->ssaRep->numUses == 4);
Bill Buzbeec6f10662010-02-09 11:16:15 -08002454 rlDest = dvmCompilerGetSrcWide(cUnit, mir, 0, 1);
2455 rlSrc1 = dvmCompilerGetSrc(cUnit, mir, 2);
2456 rlSrc2 = dvmCompilerGetSrc(cUnit, mir, 3);
Bill Buzbee1465db52009-09-23 17:17:35 -07002457 }
2458 } else {
2459 /* Two sources and 1 dest. Deduce the operand sizes */
2460 if (mir->ssaRep->numUses == 4) {
Bill Buzbeec6f10662010-02-09 11:16:15 -08002461 rlSrc1 = dvmCompilerGetSrcWide(cUnit, mir, 0, 1);
2462 rlSrc2 = dvmCompilerGetSrcWide(cUnit, mir, 2, 3);
Bill Buzbee1465db52009-09-23 17:17:35 -07002463 } else {
2464 assert(mir->ssaRep->numUses == 2);
Bill Buzbeec6f10662010-02-09 11:16:15 -08002465 rlSrc1 = dvmCompilerGetSrc(cUnit, mir, 0);
2466 rlSrc2 = dvmCompilerGetSrc(cUnit, mir, 1);
Bill Buzbee1465db52009-09-23 17:17:35 -07002467 }
2468 if (mir->ssaRep->numDefs == 2) {
Bill Buzbeec6f10662010-02-09 11:16:15 -08002469 rlDest = dvmCompilerGetDestWide(cUnit, mir, 0, 1);
Bill Buzbee1465db52009-09-23 17:17:35 -07002470 } else {
2471 assert(mir->ssaRep->numDefs == 1);
Bill Buzbeec6f10662010-02-09 11:16:15 -08002472 rlDest = dvmCompilerGetDest(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07002473 }
2474 }
2475
2476
Ben Chengba4fc8b2009-06-01 13:00:29 -07002477 switch (opCode) {
Bill Buzbeed45ba372009-06-15 17:00:57 -07002478 case OP_CMPL_FLOAT:
2479 case OP_CMPG_FLOAT:
2480 case OP_CMPL_DOUBLE:
2481 case OP_CMPG_DOUBLE:
Ben Cheng5d90c202009-11-22 23:31:11 -08002482 return genCmpFP(cUnit, mir, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002483 case OP_CMP_LONG:
Bill Buzbee1465db52009-09-23 17:17:35 -07002484 genCmpLong(cUnit, mir, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002485 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002486 case OP_AGET_WIDE:
Bill Buzbee1465db52009-09-23 17:17:35 -07002487 genArrayGet(cUnit, mir, kLong, rlSrc1, rlSrc2, rlDest, 3);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002488 break;
2489 case OP_AGET:
2490 case OP_AGET_OBJECT:
Bill Buzbee1465db52009-09-23 17:17:35 -07002491 genArrayGet(cUnit, mir, kWord, rlSrc1, rlSrc2, rlDest, 2);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002492 break;
2493 case OP_AGET_BOOLEAN:
Bill Buzbee1465db52009-09-23 17:17:35 -07002494 genArrayGet(cUnit, mir, kUnsignedByte, rlSrc1, rlSrc2, rlDest, 0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002495 break;
2496 case OP_AGET_BYTE:
Bill Buzbee1465db52009-09-23 17:17:35 -07002497 genArrayGet(cUnit, mir, kSignedByte, rlSrc1, rlSrc2, rlDest, 0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002498 break;
2499 case OP_AGET_CHAR:
Bill Buzbee1465db52009-09-23 17:17:35 -07002500 genArrayGet(cUnit, mir, kUnsignedHalf, rlSrc1, rlSrc2, rlDest, 1);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002501 break;
2502 case OP_AGET_SHORT:
Bill Buzbee1465db52009-09-23 17:17:35 -07002503 genArrayGet(cUnit, mir, kSignedHalf, rlSrc1, rlSrc2, rlDest, 1);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002504 break;
2505 case OP_APUT_WIDE:
Bill Buzbee1465db52009-09-23 17:17:35 -07002506 genArrayPut(cUnit, mir, kLong, rlSrc1, rlSrc2, rlDest, 3);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002507 break;
2508 case OP_APUT:
Bill Buzbee1465db52009-09-23 17:17:35 -07002509 genArrayPut(cUnit, mir, kWord, rlSrc1, rlSrc2, rlDest, 2);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002510 break;
Bill Buzbeebe6534f2010-03-12 16:01:35 -08002511 case OP_APUT_OBJECT:
2512 genArrayObjectPut(cUnit, mir, rlSrc1, rlSrc2, rlDest, 2);
2513 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002514 case OP_APUT_SHORT:
2515 case OP_APUT_CHAR:
Bill Buzbee1465db52009-09-23 17:17:35 -07002516 genArrayPut(cUnit, mir, kUnsignedHalf, rlSrc1, rlSrc2, rlDest, 1);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002517 break;
2518 case OP_APUT_BYTE:
2519 case OP_APUT_BOOLEAN:
Bill Buzbee1465db52009-09-23 17:17:35 -07002520 genArrayPut(cUnit, mir, kUnsignedByte, rlSrc1, rlSrc2, rlDest, 0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002521 break;
2522 default:
2523 return true;
2524 }
2525 return false;
2526}
2527
Ben Cheng6c10a972009-10-29 14:39:18 -07002528/*
2529 * Find the matching case.
2530 *
2531 * return values:
2532 * r0 (low 32-bit): pc of the chaining cell corresponding to the resolved case,
2533 * including default which is placed at MIN(size, MAX_CHAINED_SWITCH_CASES).
2534 * r1 (high 32-bit): the branch offset of the matching case (only for indexes
2535 * above MAX_CHAINED_SWITCH_CASES).
2536 *
2537 * Instructions around the call are:
2538 *
2539 * mov r2, pc
2540 * blx &findPackedSwitchIndex
2541 * mov pc, r0
2542 * .align4
Bill Buzbeebd047242010-05-13 13:02:53 -07002543 * chaining cell for case 0 [12 bytes]
2544 * chaining cell for case 1 [12 bytes]
Ben Cheng6c10a972009-10-29 14:39:18 -07002545 * :
Bill Buzbeebd047242010-05-13 13:02:53 -07002546 * chaining cell for case MIN(size, MAX_CHAINED_SWITCH_CASES)-1 [12 bytes]
Ben Cheng6c10a972009-10-29 14:39:18 -07002547 * chaining cell for case default [8 bytes]
2548 * noChain exit
2549 */
Ben Chengbd1326d2010-04-02 15:04:53 -07002550static s8 findPackedSwitchIndex(const u2* switchData, int testVal, int pc)
Ben Cheng6c10a972009-10-29 14:39:18 -07002551{
2552 int size;
2553 int firstKey;
2554 const int *entries;
2555 int index;
2556 int jumpIndex;
2557 int caseDPCOffset = 0;
2558 /* In Thumb mode pc is 4 ahead of the "mov r2, pc" instruction */
2559 int chainingPC = (pc + 4) & ~3;
2560
2561 /*
2562 * Packed switch data format:
2563 * ushort ident = 0x0100 magic value
2564 * ushort size number of entries in the table
2565 * int first_key first (and lowest) switch case value
2566 * int targets[size] branch targets, relative to switch opcode
2567 *
2568 * Total size is (4+size*2) 16-bit code units.
2569 */
2570 size = switchData[1];
2571 assert(size > 0);
2572
2573 firstKey = switchData[2];
2574 firstKey |= switchData[3] << 16;
2575
2576
2577 /* The entries are guaranteed to be aligned on a 32-bit boundary;
2578 * we can treat them as a native int array.
2579 */
2580 entries = (const int*) &switchData[4];
2581 assert(((u4)entries & 0x3) == 0);
2582
2583 index = testVal - firstKey;
2584
2585 /* Jump to the default cell */
2586 if (index < 0 || index >= size) {
2587 jumpIndex = MIN(size, MAX_CHAINED_SWITCH_CASES);
2588 /* Jump to the non-chaining exit point */
2589 } else if (index >= MAX_CHAINED_SWITCH_CASES) {
2590 jumpIndex = MAX_CHAINED_SWITCH_CASES + 1;
2591 caseDPCOffset = entries[index];
2592 /* Jump to the inline chaining cell */
2593 } else {
2594 jumpIndex = index;
2595 }
2596
Bill Buzbeebd047242010-05-13 13:02:53 -07002597 chainingPC += jumpIndex * CHAIN_CELL_NORMAL_SIZE;
Ben Cheng6c10a972009-10-29 14:39:18 -07002598 return (((s8) caseDPCOffset) << 32) | (u8) chainingPC;
2599}
2600
2601/* See comments for findPackedSwitchIndex */
Ben Chengbd1326d2010-04-02 15:04:53 -07002602static s8 findSparseSwitchIndex(const u2* switchData, int testVal, int pc)
Ben Cheng6c10a972009-10-29 14:39:18 -07002603{
2604 int size;
2605 const int *keys;
2606 const int *entries;
2607 int chainingPC = (pc + 4) & ~3;
2608 int i;
2609
2610 /*
2611 * Sparse switch data format:
2612 * ushort ident = 0x0200 magic value
2613 * ushort size number of entries in the table; > 0
2614 * int keys[size] keys, sorted low-to-high; 32-bit aligned
2615 * int targets[size] branch targets, relative to switch opcode
2616 *
2617 * Total size is (2+size*4) 16-bit code units.
2618 */
2619
2620 size = switchData[1];
2621 assert(size > 0);
2622
2623 /* The keys are guaranteed to be aligned on a 32-bit boundary;
2624 * we can treat them as a native int array.
2625 */
2626 keys = (const int*) &switchData[2];
2627 assert(((u4)keys & 0x3) == 0);
2628
2629 /* The entries are guaranteed to be aligned on a 32-bit boundary;
2630 * we can treat them as a native int array.
2631 */
2632 entries = keys + size;
2633 assert(((u4)entries & 0x3) == 0);
2634
2635 /*
2636 * Run through the list of keys, which are guaranteed to
2637 * be sorted low-to-high.
2638 *
2639 * Most tables have 3-4 entries. Few have more than 10. A binary
2640 * search here is probably not useful.
2641 */
2642 for (i = 0; i < size; i++) {
2643 int k = keys[i];
2644 if (k == testVal) {
2645 /* MAX_CHAINED_SWITCH_CASES + 1 is the start of the overflow case */
2646 int jumpIndex = (i < MAX_CHAINED_SWITCH_CASES) ?
2647 i : MAX_CHAINED_SWITCH_CASES + 1;
Bill Buzbeebd047242010-05-13 13:02:53 -07002648 chainingPC += jumpIndex * CHAIN_CELL_NORMAL_SIZE;
Ben Cheng6c10a972009-10-29 14:39:18 -07002649 return (((s8) entries[i]) << 32) | (u8) chainingPC;
2650 } else if (k > testVal) {
2651 break;
2652 }
2653 }
Bill Buzbeebd047242010-05-13 13:02:53 -07002654 return chainingPC + MIN(size, MAX_CHAINED_SWITCH_CASES) *
2655 CHAIN_CELL_NORMAL_SIZE;
Ben Cheng6c10a972009-10-29 14:39:18 -07002656}
2657
Ben Chengba4fc8b2009-06-01 13:00:29 -07002658static bool handleFmt31t(CompilationUnit *cUnit, MIR *mir)
2659{
2660 OpCode dalvikOpCode = mir->dalvikInsn.opCode;
2661 switch (dalvikOpCode) {
2662 case OP_FILL_ARRAY_DATA: {
Bill Buzbeec6f10662010-02-09 11:16:15 -08002663 RegLocation rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07002664 // Making a call - use explicit registers
Bill Buzbeec6f10662010-02-09 11:16:15 -08002665 dvmCompilerFlushAllRegs(cUnit); /* Everything to home location */
Bill Buzbee1465db52009-09-23 17:17:35 -07002666 genExportPC(cUnit, mir);
2667 loadValueDirectFixed(cUnit, rlSrc, r0);
Ben Chengbd1326d2010-04-02 15:04:53 -07002668 LOAD_FUNC_ADDR(cUnit, r2, (int)dvmInterpHandleFillArrayData);
Ben Cheng6c10a972009-10-29 14:39:18 -07002669 loadConstant(cUnit, r1,
2670 (int) (cUnit->method->insns + mir->offset + mir->dalvikInsn.vB));
Bill Buzbee1465db52009-09-23 17:17:35 -07002671 opReg(cUnit, kOpBlx, r2);
Elliott Hughes6a555132010-02-25 15:41:42 -08002672 dvmCompilerClobberCallRegs(cUnit);
Bill Buzbeefd023aa2009-11-02 09:23:49 -08002673 /* generate a branch over if successful */
2674 opRegImm(cUnit, kOpCmp, r0, 0); /* NULL? */
2675 ArmLIR *branchOver = opCondBranch(cUnit, kArmCondNe);
2676 loadConstant(cUnit, r0,
2677 (int) (cUnit->method->insns + mir->offset));
2678 genDispatchToHandler(cUnit, TEMPLATE_THROW_EXCEPTION_COMMON);
2679 ArmLIR *target = newLIR0(cUnit, kArmPseudoTargetLabel);
2680 target->defMask = ENCODE_ALL;
2681 branchOver->generic.target = (LIR *) target;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002682 break;
2683 }
2684 /*
Ben Cheng6c10a972009-10-29 14:39:18 -07002685 * Compute the goto target of up to
2686 * MIN(switchSize, MAX_CHAINED_SWITCH_CASES) + 1 chaining cells.
2687 * See the comment before findPackedSwitchIndex for the code layout.
Ben Chengba4fc8b2009-06-01 13:00:29 -07002688 */
2689 case OP_PACKED_SWITCH:
2690 case OP_SPARSE_SWITCH: {
Bill Buzbeec6f10662010-02-09 11:16:15 -08002691 RegLocation rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
2692 dvmCompilerFlushAllRegs(cUnit); /* Everything to home location */
Bill Buzbee1465db52009-09-23 17:17:35 -07002693 loadValueDirectFixed(cUnit, rlSrc, r1);
Bill Buzbeec6f10662010-02-09 11:16:15 -08002694 dvmCompilerLockAllTemps(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002695 if (dalvikOpCode == OP_PACKED_SWITCH) {
Ben Chengbd1326d2010-04-02 15:04:53 -07002696 LOAD_FUNC_ADDR(cUnit, r4PC, (int)findPackedSwitchIndex);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002697 } else {
Ben Chengbd1326d2010-04-02 15:04:53 -07002698 LOAD_FUNC_ADDR(cUnit, r4PC, (int)findSparseSwitchIndex);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002699 }
Ben Cheng6c10a972009-10-29 14:39:18 -07002700 /* r0 <- Addr of the switch data */
2701 loadConstant(cUnit, r0,
2702 (int) (cUnit->method->insns + mir->offset + mir->dalvikInsn.vB));
2703 /* r2 <- pc of the instruction following the blx */
2704 opRegReg(cUnit, kOpMov, r2, rpc);
Bill Buzbee1465db52009-09-23 17:17:35 -07002705 opReg(cUnit, kOpBlx, r4PC);
Elliott Hughes6a555132010-02-25 15:41:42 -08002706 dvmCompilerClobberCallRegs(cUnit);
Ben Cheng6c10a972009-10-29 14:39:18 -07002707 /* pc <- computed goto target */
2708 opRegReg(cUnit, kOpMov, rpc, r0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002709 break;
2710 }
2711 default:
2712 return true;
2713 }
2714 return false;
2715}
2716
2717static bool handleFmt35c_3rc(CompilationUnit *cUnit, MIR *mir, BasicBlock *bb,
Bill Buzbee89efc3d2009-07-28 11:22:22 -07002718 ArmLIR *labelList)
Ben Chengba4fc8b2009-06-01 13:00:29 -07002719{
Bill Buzbee9bc3df32009-07-30 10:52:29 -07002720 ArmLIR *retChainingCell = NULL;
Bill Buzbee89efc3d2009-07-28 11:22:22 -07002721 ArmLIR *pcrLabel = NULL;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002722
Bill Buzbeef4ce16f2009-07-28 13:28:25 -07002723 if (bb->fallThrough != NULL)
2724 retChainingCell = &labelList[bb->fallThrough->id];
2725
Ben Chengba4fc8b2009-06-01 13:00:29 -07002726 DecodedInstruction *dInsn = &mir->dalvikInsn;
2727 switch (mir->dalvikInsn.opCode) {
2728 /*
2729 * calleeMethod = this->clazz->vtable[
2730 * method->clazz->pDvmDex->pResMethods[BBBB]->methodIndex
2731 * ]
2732 */
2733 case OP_INVOKE_VIRTUAL:
2734 case OP_INVOKE_VIRTUAL_RANGE: {
Bill Buzbee89efc3d2009-07-28 11:22:22 -07002735 ArmLIR *predChainingCell = &labelList[bb->taken->id];
Ben Chengba4fc8b2009-06-01 13:00:29 -07002736 int methodIndex =
2737 cUnit->method->clazz->pDvmDex->pResMethods[dInsn->vB]->
2738 methodIndex;
2739
2740 if (mir->dalvikInsn.opCode == OP_INVOKE_VIRTUAL)
2741 genProcessArgsNoRange(cUnit, mir, dInsn, &pcrLabel);
2742 else
2743 genProcessArgsRange(cUnit, mir, dInsn, &pcrLabel);
2744
Ben Cheng38329f52009-07-07 14:19:20 -07002745 genInvokeVirtualCommon(cUnit, mir, methodIndex,
2746 retChainingCell,
2747 predChainingCell,
2748 pcrLabel);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002749 break;
2750 }
2751 /*
2752 * calleeMethod = method->clazz->super->vtable[method->clazz->pDvmDex
2753 * ->pResMethods[BBBB]->methodIndex]
2754 */
Ben Chengba4fc8b2009-06-01 13:00:29 -07002755 case OP_INVOKE_SUPER:
2756 case OP_INVOKE_SUPER_RANGE: {
2757 int mIndex = cUnit->method->clazz->pDvmDex->
2758 pResMethods[dInsn->vB]->methodIndex;
2759 const Method *calleeMethod =
2760 cUnit->method->clazz->super->vtable[mIndex];
2761
2762 if (mir->dalvikInsn.opCode == OP_INVOKE_SUPER)
2763 genProcessArgsNoRange(cUnit, mir, dInsn, &pcrLabel);
2764 else
2765 genProcessArgsRange(cUnit, mir, dInsn, &pcrLabel);
2766
2767 /* r0 = calleeMethod */
2768 loadConstant(cUnit, r0, (int) calleeMethod);
2769
Ben Cheng38329f52009-07-07 14:19:20 -07002770 genInvokeSingletonCommon(cUnit, mir, bb, labelList, pcrLabel,
2771 calleeMethod);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002772 break;
2773 }
2774 /* calleeMethod = method->clazz->pDvmDex->pResMethods[BBBB] */
2775 case OP_INVOKE_DIRECT:
2776 case OP_INVOKE_DIRECT_RANGE: {
2777 const Method *calleeMethod =
2778 cUnit->method->clazz->pDvmDex->pResMethods[dInsn->vB];
2779
2780 if (mir->dalvikInsn.opCode == OP_INVOKE_DIRECT)
2781 genProcessArgsNoRange(cUnit, mir, dInsn, &pcrLabel);
2782 else
2783 genProcessArgsRange(cUnit, mir, dInsn, &pcrLabel);
2784
2785 /* r0 = calleeMethod */
2786 loadConstant(cUnit, r0, (int) calleeMethod);
2787
Ben Cheng38329f52009-07-07 14:19:20 -07002788 genInvokeSingletonCommon(cUnit, mir, bb, labelList, pcrLabel,
2789 calleeMethod);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002790 break;
2791 }
2792 /* calleeMethod = method->clazz->pDvmDex->pResMethods[BBBB] */
2793 case OP_INVOKE_STATIC:
2794 case OP_INVOKE_STATIC_RANGE: {
2795 const Method *calleeMethod =
2796 cUnit->method->clazz->pDvmDex->pResMethods[dInsn->vB];
2797
2798 if (mir->dalvikInsn.opCode == OP_INVOKE_STATIC)
2799 genProcessArgsNoRange(cUnit, mir, dInsn,
2800 NULL /* no null check */);
2801 else
2802 genProcessArgsRange(cUnit, mir, dInsn,
2803 NULL /* no null check */);
2804
2805 /* r0 = calleeMethod */
2806 loadConstant(cUnit, r0, (int) calleeMethod);
2807
Ben Cheng38329f52009-07-07 14:19:20 -07002808 genInvokeSingletonCommon(cUnit, mir, bb, labelList, pcrLabel,
2809 calleeMethod);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002810 break;
2811 }
Ben Cheng09e50c92010-05-02 10:45:32 -07002812 /*
Ben Chengba4fc8b2009-06-01 13:00:29 -07002813 * calleeMethod = dvmFindInterfaceMethodInCache(this->clazz,
2814 * BBBB, method, method->clazz->pDvmDex)
Ben Cheng38329f52009-07-07 14:19:20 -07002815 *
Ben Cheng09e50c92010-05-02 10:45:32 -07002816 * The following is an example of generated code for
2817 * "invoke-interface v0"
Ben Cheng38329f52009-07-07 14:19:20 -07002818 *
Ben Cheng09e50c92010-05-02 10:45:32 -07002819 * -------- dalvik offset: 0x0008 @ invoke-interface v0
2820 * 0x47357e36 : ldr r0, [r5, #0] --+
2821 * 0x47357e38 : sub r7,r5,#24 |
2822 * 0x47357e3c : cmp r0, #0 | genProcessArgsNoRange
2823 * 0x47357e3e : beq 0x47357e82 |
2824 * 0x47357e40 : stmia r7, <r0> --+
2825 * 0x47357e42 : ldr r4, [pc, #120] --> r4 <- dalvikPC of this invoke
2826 * 0x47357e44 : add r1, pc, #64 --> r1 <- &retChainingCell
2827 * 0x47357e46 : add r2, pc, #72 --> r2 <- &predictedChainingCell
2828 * 0x47357e48 : blx_1 0x47348190 --+ TEMPLATE_INVOKE_METHOD_
2829 * 0x47357e4a : blx_2 see above --+ PREDICTED_CHAIN
2830 * 0x47357e4c : b 0x47357e90 --> off to the predicted chain
2831 * 0x47357e4e : b 0x47357e82 --> punt to the interpreter
2832 * 0x47357e50 : mov r8, r1 --+
2833 * 0x47357e52 : mov r9, r2 |
2834 * 0x47357e54 : ldr r2, [pc, #96] |
2835 * 0x47357e56 : mov r10, r3 |
2836 * 0x47357e58 : movs r0, r3 | dvmFindInterfaceMethodInCache
2837 * 0x47357e5a : ldr r3, [pc, #88] |
2838 * 0x47357e5c : ldr r7, [pc, #80] |
2839 * 0x47357e5e : mov r1, #1452 |
2840 * 0x47357e62 : blx r7 --+
2841 * 0x47357e64 : cmp r0, #0 --> calleeMethod == NULL?
2842 * 0x47357e66 : bne 0x47357e6e --> branch over the throw if !r0
2843 * 0x47357e68 : ldr r0, [pc, #80] --> load Dalvik PC of the invoke
2844 * 0x47357e6a : blx_1 0x47348494 --+ TEMPLATE_THROW_EXCEPTION_
2845 * 0x47357e6c : blx_2 see above --+ COMMON
2846 * 0x47357e6e : mov r1, r8 --> r1 <- &retChainingCell
2847 * 0x47357e70 : cmp r1, #0 --> compare against 0
2848 * 0x47357e72 : bgt 0x47357e7c --> >=0? don't rechain
2849 * 0x47357e74 : ldr r7, [r6, #108] --+
2850 * 0x47357e76 : mov r2, r9 | dvmJitToPatchPredictedChain
2851 * 0x47357e78 : mov r3, r10 |
2852 * 0x47357e7a : blx r7 --+
2853 * 0x47357e7c : add r1, pc, #8 --> r1 <- &retChainingCell
2854 * 0x47357e7e : blx_1 0x4734809c --+ TEMPLATE_INVOKE_METHOD_NO_OPT
2855 * 0x47357e80 : blx_2 see above --+
2856 * -------- reconstruct dalvik PC : 0x425719dc @ +0x0008
2857 * 0x47357e82 : ldr r0, [pc, #56]
Ben Cheng38329f52009-07-07 14:19:20 -07002858 * Exception_Handling:
Ben Cheng09e50c92010-05-02 10:45:32 -07002859 * 0x47357e84 : ldr r1, [r6, #92]
2860 * 0x47357e86 : blx r1
2861 * 0x47357e88 : .align4
2862 * -------- chaining cell (hot): 0x000b
2863 * 0x47357e88 : ldr r0, [r6, #104]
2864 * 0x47357e8a : blx r0
2865 * 0x47357e8c : data 0x19e2(6626)
2866 * 0x47357e8e : data 0x4257(16983)
2867 * 0x47357e90 : .align4
Ben Cheng38329f52009-07-07 14:19:20 -07002868 * -------- chaining cell (predicted)
Ben Cheng09e50c92010-05-02 10:45:32 -07002869 * 0x47357e90 : data 0xe7fe(59390) --> will be patched into bx
2870 * 0x47357e92 : data 0x0000(0)
2871 * 0x47357e94 : data 0x0000(0) --> class
2872 * 0x47357e96 : data 0x0000(0)
2873 * 0x47357e98 : data 0x0000(0) --> method
2874 * 0x47357e9a : data 0x0000(0)
2875 * 0x47357e9c : data 0x0000(0) --> rechain count
2876 * 0x47357e9e : data 0x0000(0)
2877 * -------- end of chaining cells (0x006c)
2878 * 0x47357eb0 : .word (0xad03e369)
2879 * 0x47357eb4 : .word (0x28a90)
2880 * 0x47357eb8 : .word (0x41a63394)
2881 * 0x47357ebc : .word (0x425719dc)
Ben Chengba4fc8b2009-06-01 13:00:29 -07002882 */
2883 case OP_INVOKE_INTERFACE:
2884 case OP_INVOKE_INTERFACE_RANGE: {
Bill Buzbee89efc3d2009-07-28 11:22:22 -07002885 ArmLIR *predChainingCell = &labelList[bb->taken->id];
Ben Chengba4fc8b2009-06-01 13:00:29 -07002886
Bill Buzbee1465db52009-09-23 17:17:35 -07002887 /* Ensure that nothing is both live and dirty */
Bill Buzbeec6f10662010-02-09 11:16:15 -08002888 dvmCompilerFlushAllRegs(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07002889
Ben Chengba4fc8b2009-06-01 13:00:29 -07002890 if (mir->dalvikInsn.opCode == OP_INVOKE_INTERFACE)
2891 genProcessArgsNoRange(cUnit, mir, dInsn, &pcrLabel);
2892 else
2893 genProcessArgsRange(cUnit, mir, dInsn, &pcrLabel);
2894
Ben Cheng38329f52009-07-07 14:19:20 -07002895 /* "this" is already left in r0 by genProcessArgs* */
2896
2897 /* r4PC = dalvikCallsite */
2898 loadConstant(cUnit, r4PC,
2899 (int) (cUnit->method->insns + mir->offset));
2900
2901 /* r1 = &retChainingCell */
Bill Buzbee270c1d62009-08-13 16:58:07 -07002902 ArmLIR *addrRetChain =
Bill Buzbee1465db52009-09-23 17:17:35 -07002903 opRegRegImm(cUnit, kOpAdd, r1, rpc, 0);
Ben Cheng38329f52009-07-07 14:19:20 -07002904 addrRetChain->generic.target = (LIR *) retChainingCell;
2905
2906 /* r2 = &predictedChainingCell */
Bill Buzbee89efc3d2009-07-28 11:22:22 -07002907 ArmLIR *predictedChainingCell =
Bill Buzbee1465db52009-09-23 17:17:35 -07002908 opRegRegImm(cUnit, kOpAdd, r2, rpc, 0);
Ben Cheng38329f52009-07-07 14:19:20 -07002909 predictedChainingCell->generic.target = (LIR *) predChainingCell;
2910
2911 genDispatchToHandler(cUnit, TEMPLATE_INVOKE_METHOD_PREDICTED_CHAIN);
2912
2913 /* return through lr - jump to the chaining cell */
2914 genUnconditionalBranch(cUnit, predChainingCell);
2915
2916 /*
2917 * null-check on "this" may have been eliminated, but we still need
2918 * a PC-reconstruction label for stack overflow bailout.
2919 */
2920 if (pcrLabel == NULL) {
2921 int dPC = (int) (cUnit->method->insns + mir->offset);
Bill Buzbee89efc3d2009-07-28 11:22:22 -07002922 pcrLabel = dvmCompilerNew(sizeof(ArmLIR), true);
Ben Chenga4973592010-03-31 11:59:18 -07002923 pcrLabel->opCode = kArmPseudoPCReconstructionCell;
Ben Cheng38329f52009-07-07 14:19:20 -07002924 pcrLabel->operands[0] = dPC;
2925 pcrLabel->operands[1] = mir->offset;
2926 /* Insert the place holder to the growable list */
2927 dvmInsertGrowableList(&cUnit->pcReconstructionList, pcrLabel);
2928 }
2929
2930 /* return through lr+2 - punt to the interpreter */
2931 genUnconditionalBranch(cUnit, pcrLabel);
2932
2933 /*
2934 * return through lr+4 - fully resolve the callee method.
2935 * r1 <- count
2936 * r2 <- &predictedChainCell
2937 * r3 <- this->class
2938 * r4 <- dPC
2939 * r7 <- this->class->vtable
2940 */
2941
2942 /* Save count, &predictedChainCell, and class to high regs first */
Bill Buzbee1465db52009-09-23 17:17:35 -07002943 genRegCopy(cUnit, r8, r1);
2944 genRegCopy(cUnit, r9, r2);
2945 genRegCopy(cUnit, r10, r3);
Ben Cheng38329f52009-07-07 14:19:20 -07002946
Ben Chengba4fc8b2009-06-01 13:00:29 -07002947 /* r0 now contains this->clazz */
Bill Buzbee1465db52009-09-23 17:17:35 -07002948 genRegCopy(cUnit, r0, r3);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002949
2950 /* r1 = BBBB */
2951 loadConstant(cUnit, r1, dInsn->vB);
2952
2953 /* r2 = method (caller) */
2954 loadConstant(cUnit, r2, (int) cUnit->method);
2955
2956 /* r3 = pDvmDex */
2957 loadConstant(cUnit, r3, (int) cUnit->method->clazz->pDvmDex);
2958
Ben Chengbd1326d2010-04-02 15:04:53 -07002959 LOAD_FUNC_ADDR(cUnit, r7,
2960 (intptr_t) dvmFindInterfaceMethodInCache);
Bill Buzbee1465db52009-09-23 17:17:35 -07002961 opReg(cUnit, kOpBlx, r7);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002962 /* r0 = calleeMethod (returned from dvmFindInterfaceMethodInCache */
2963
Ben Cheng09e50c92010-05-02 10:45:32 -07002964 dvmCompilerClobberCallRegs(cUnit);
2965 /* generate a branch over if the interface method is resolved */
2966 opRegImm(cUnit, kOpCmp, r0, 0); /* NULL? */
2967 ArmLIR *branchOver = opCondBranch(cUnit, kArmCondNe);
2968 /*
2969 * calleeMethod == NULL -> throw
2970 */
2971 loadConstant(cUnit, r0,
2972 (int) (cUnit->method->insns + mir->offset));
2973 genDispatchToHandler(cUnit, TEMPLATE_THROW_EXCEPTION_COMMON);
2974 /* noreturn */
2975
2976 ArmLIR *target = newLIR0(cUnit, kArmPseudoTargetLabel);
2977 target->defMask = ENCODE_ALL;
2978 branchOver->generic.target = (LIR *) target;
2979
Bill Buzbee1465db52009-09-23 17:17:35 -07002980 genRegCopy(cUnit, r1, r8);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002981
Ben Cheng38329f52009-07-07 14:19:20 -07002982 /* Check if rechain limit is reached */
Bill Buzbee1465db52009-09-23 17:17:35 -07002983 opRegImm(cUnit, kOpCmp, r1, 0);
Ben Cheng38329f52009-07-07 14:19:20 -07002984
Bill Buzbee1465db52009-09-23 17:17:35 -07002985 ArmLIR *bypassRechaining = opCondBranch(cUnit, kArmCondGt);
Ben Cheng38329f52009-07-07 14:19:20 -07002986
Bill Buzbee270c1d62009-08-13 16:58:07 -07002987 loadWordDisp(cUnit, rGLUE, offsetof(InterpState,
2988 jitToInterpEntries.dvmJitToPatchPredictedChain), r7);
Ben Cheng38329f52009-07-07 14:19:20 -07002989
Ben Chengb88ec3c2010-05-17 12:50:33 -07002990 genRegCopy(cUnit, r1, rGLUE);
Bill Buzbee1465db52009-09-23 17:17:35 -07002991 genRegCopy(cUnit, r2, r9);
2992 genRegCopy(cUnit, r3, r10);
Ben Cheng38329f52009-07-07 14:19:20 -07002993
2994 /*
2995 * r0 = calleeMethod
2996 * r2 = &predictedChainingCell
2997 * r3 = class
2998 *
2999 * &returnChainingCell has been loaded into r1 but is not needed
3000 * when patching the chaining cell and will be clobbered upon
3001 * returning so it will be reconstructed again.
3002 */
Bill Buzbee1465db52009-09-23 17:17:35 -07003003 opReg(cUnit, kOpBlx, r7);
Ben Cheng38329f52009-07-07 14:19:20 -07003004
3005 /* r1 = &retChainingCell */
Bill Buzbee1465db52009-09-23 17:17:35 -07003006 addrRetChain = opRegRegImm(cUnit, kOpAdd, r1, rpc, 0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003007 addrRetChain->generic.target = (LIR *) retChainingCell;
Ben Cheng38329f52009-07-07 14:19:20 -07003008
3009 bypassRechaining->generic.target = (LIR *) addrRetChain;
3010
Ben Chengba4fc8b2009-06-01 13:00:29 -07003011 /*
3012 * r0 = this, r1 = calleeMethod,
3013 * r1 = &ChainingCell,
3014 * r4PC = callsiteDPC,
3015 */
3016 genDispatchToHandler(cUnit, TEMPLATE_INVOKE_METHOD_NO_OPT);
Ben Cheng978738d2010-05-13 13:45:57 -07003017#if defined(WITH_JIT_TUNING)
Ben Cheng86717f72010-03-05 15:27:21 -08003018 gDvmJit.invokePolymorphic++;
Ben Chengba4fc8b2009-06-01 13:00:29 -07003019#endif
3020 /* Handle exceptions using the interpreter */
3021 genTrap(cUnit, mir->offset, pcrLabel);
3022 break;
3023 }
3024 /* NOP */
3025 case OP_INVOKE_DIRECT_EMPTY: {
3026 return false;
3027 }
3028 case OP_FILLED_NEW_ARRAY:
3029 case OP_FILLED_NEW_ARRAY_RANGE: {
3030 /* Just let the interpreter deal with these */
3031 genInterpSingleStep(cUnit, mir);
3032 break;
3033 }
3034 default:
3035 return true;
3036 }
3037 return false;
3038}
3039
3040static bool handleFmt35ms_3rms(CompilationUnit *cUnit, MIR *mir,
Bill Buzbee89efc3d2009-07-28 11:22:22 -07003041 BasicBlock *bb, ArmLIR *labelList)
Ben Chengba4fc8b2009-06-01 13:00:29 -07003042{
Bill Buzbee89efc3d2009-07-28 11:22:22 -07003043 ArmLIR *retChainingCell = &labelList[bb->fallThrough->id];
3044 ArmLIR *predChainingCell = &labelList[bb->taken->id];
3045 ArmLIR *pcrLabel = NULL;
Ben Chengba4fc8b2009-06-01 13:00:29 -07003046
3047 DecodedInstruction *dInsn = &mir->dalvikInsn;
3048 switch (mir->dalvikInsn.opCode) {
3049 /* calleeMethod = this->clazz->vtable[BBBB] */
3050 case OP_INVOKE_VIRTUAL_QUICK_RANGE:
3051 case OP_INVOKE_VIRTUAL_QUICK: {
3052 int methodIndex = dInsn->vB;
3053 if (mir->dalvikInsn.opCode == OP_INVOKE_VIRTUAL_QUICK)
3054 genProcessArgsNoRange(cUnit, mir, dInsn, &pcrLabel);
3055 else
3056 genProcessArgsRange(cUnit, mir, dInsn, &pcrLabel);
3057
Ben Cheng38329f52009-07-07 14:19:20 -07003058 genInvokeVirtualCommon(cUnit, mir, methodIndex,
3059 retChainingCell,
3060 predChainingCell,
3061 pcrLabel);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003062 break;
3063 }
3064 /* calleeMethod = method->clazz->super->vtable[BBBB] */
3065 case OP_INVOKE_SUPER_QUICK:
3066 case OP_INVOKE_SUPER_QUICK_RANGE: {
3067 const Method *calleeMethod =
3068 cUnit->method->clazz->super->vtable[dInsn->vB];
3069
3070 if (mir->dalvikInsn.opCode == OP_INVOKE_SUPER_QUICK)
3071 genProcessArgsNoRange(cUnit, mir, dInsn, &pcrLabel);
3072 else
3073 genProcessArgsRange(cUnit, mir, dInsn, &pcrLabel);
3074
3075 /* r0 = calleeMethod */
3076 loadConstant(cUnit, r0, (int) calleeMethod);
3077
Ben Cheng38329f52009-07-07 14:19:20 -07003078 genInvokeSingletonCommon(cUnit, mir, bb, labelList, pcrLabel,
3079 calleeMethod);
3080 /* Handle exceptions using the interpreter */
3081 genTrap(cUnit, mir->offset, pcrLabel);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003082 break;
3083 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07003084 default:
3085 return true;
3086 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07003087 return false;
3088}
3089
3090/*
Bill Buzbeefd023aa2009-11-02 09:23:49 -08003091 * This operation is complex enough that we'll do it partly inline
3092 * and partly with a handler. NOTE: the handler uses hardcoded
3093 * values for string object offsets and must be revisitied if the
3094 * layout changes.
3095 */
3096static bool genInlinedCompareTo(CompilationUnit *cUnit, MIR *mir)
3097{
3098#if defined(USE_GLOBAL_STRING_DEFS)
3099 return false;
3100#else
3101 ArmLIR *rollback;
Bill Buzbeec6f10662010-02-09 11:16:15 -08003102 RegLocation rlThis = dvmCompilerGetSrc(cUnit, mir, 0);
3103 RegLocation rlComp = dvmCompilerGetSrc(cUnit, mir, 1);
Bill Buzbeefd023aa2009-11-02 09:23:49 -08003104
3105 loadValueDirectFixed(cUnit, rlThis, r0);
3106 loadValueDirectFixed(cUnit, rlComp, r1);
3107 /* Test objects for NULL */
3108 rollback = genNullCheck(cUnit, rlThis.sRegLow, r0, mir->offset, NULL);
3109 genNullCheck(cUnit, rlComp.sRegLow, r1, mir->offset, rollback);
3110 /*
3111 * TUNING: we could check for object pointer equality before invoking
3112 * handler. Unclear whether the gain would be worth the added code size
3113 * expansion.
3114 */
3115 genDispatchToHandler(cUnit, TEMPLATE_STRING_COMPARETO);
Bill Buzbeec6f10662010-02-09 11:16:15 -08003116 storeValue(cUnit, inlinedTarget(cUnit, mir, false),
3117 dvmCompilerGetReturn(cUnit));
Bill Buzbeefd023aa2009-11-02 09:23:49 -08003118 return true;
3119#endif
3120}
3121
Elliott Hughes2bdbcb62010-04-12 14:29:37 -07003122static bool genInlinedFastIndexOf(CompilationUnit *cUnit, MIR *mir)
Bill Buzbeefd023aa2009-11-02 09:23:49 -08003123{
3124#if defined(USE_GLOBAL_STRING_DEFS)
3125 return false;
3126#else
Bill Buzbeec6f10662010-02-09 11:16:15 -08003127 RegLocation rlThis = dvmCompilerGetSrc(cUnit, mir, 0);
3128 RegLocation rlChar = dvmCompilerGetSrc(cUnit, mir, 1);
Bill Buzbeefd023aa2009-11-02 09:23:49 -08003129
3130 loadValueDirectFixed(cUnit, rlThis, r0);
3131 loadValueDirectFixed(cUnit, rlChar, r1);
Elliott Hughes2bdbcb62010-04-12 14:29:37 -07003132 RegLocation rlStart = dvmCompilerGetSrc(cUnit, mir, 2);
3133 loadValueDirectFixed(cUnit, rlStart, r2);
Bill Buzbeefd023aa2009-11-02 09:23:49 -08003134 /* Test objects for NULL */
3135 genNullCheck(cUnit, rlThis.sRegLow, r0, mir->offset, NULL);
3136 genDispatchToHandler(cUnit, TEMPLATE_STRING_INDEXOF);
Bill Buzbeec6f10662010-02-09 11:16:15 -08003137 storeValue(cUnit, inlinedTarget(cUnit, mir, false),
3138 dvmCompilerGetReturn(cUnit));
Bill Buzbeefd023aa2009-11-02 09:23:49 -08003139 return true;
3140#endif
3141}
3142
Elliott Hughesee34f592010-04-05 18:13:52 -07003143// Generates an inlined String.isEmpty or String.length.
3144static bool genInlinedStringIsEmptyOrLength(CompilationUnit *cUnit, MIR *mir,
3145 bool isEmpty)
Bill Buzbee1f748632010-03-02 16:14:41 -08003146{
Elliott Hughesee34f592010-04-05 18:13:52 -07003147 // dst = src.length();
Bill Buzbee1f748632010-03-02 16:14:41 -08003148 RegLocation rlObj = dvmCompilerGetSrc(cUnit, mir, 0);
3149 RegLocation rlDest = inlinedTarget(cUnit, mir, false);
3150 rlObj = loadValue(cUnit, rlObj, kCoreReg);
3151 RegLocation rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
3152 genNullCheck(cUnit, rlObj.sRegLow, rlObj.lowReg, mir->offset, NULL);
3153 loadWordDisp(cUnit, rlObj.lowReg, gDvm.offJavaLangString_count,
3154 rlResult.lowReg);
Elliott Hughesee34f592010-04-05 18:13:52 -07003155 if (isEmpty) {
3156 // dst = (dst == 0);
3157 int tReg = dvmCompilerAllocTemp(cUnit);
3158 opRegReg(cUnit, kOpNeg, tReg, rlResult.lowReg);
3159 opRegRegReg(cUnit, kOpAdc, rlResult.lowReg, rlResult.lowReg, tReg);
3160 }
Bill Buzbee1f748632010-03-02 16:14:41 -08003161 storeValue(cUnit, rlDest, rlResult);
3162 return false;
3163}
3164
Elliott Hughesee34f592010-04-05 18:13:52 -07003165static bool genInlinedStringLength(CompilationUnit *cUnit, MIR *mir)
3166{
3167 return genInlinedStringIsEmptyOrLength(cUnit, mir, false);
3168}
3169
3170static bool genInlinedStringIsEmpty(CompilationUnit *cUnit, MIR *mir)
3171{
3172 return genInlinedStringIsEmptyOrLength(cUnit, mir, true);
3173}
3174
Bill Buzbee1f748632010-03-02 16:14:41 -08003175static bool genInlinedStringCharAt(CompilationUnit *cUnit, MIR *mir)
3176{
3177 int contents = offsetof(ArrayObject, contents);
3178 RegLocation rlObj = dvmCompilerGetSrc(cUnit, mir, 0);
3179 RegLocation rlIdx = dvmCompilerGetSrc(cUnit, mir, 1);
3180 RegLocation rlDest = inlinedTarget(cUnit, mir, false);
3181 RegLocation rlResult;
3182 rlObj = loadValue(cUnit, rlObj, kCoreReg);
3183 rlIdx = loadValue(cUnit, rlIdx, kCoreReg);
3184 int regMax = dvmCompilerAllocTemp(cUnit);
3185 int regOff = dvmCompilerAllocTemp(cUnit);
3186 int regPtr = dvmCompilerAllocTemp(cUnit);
3187 ArmLIR *pcrLabel = genNullCheck(cUnit, rlObj.sRegLow, rlObj.lowReg,
3188 mir->offset, NULL);
3189 loadWordDisp(cUnit, rlObj.lowReg, gDvm.offJavaLangString_count, regMax);
3190 loadWordDisp(cUnit, rlObj.lowReg, gDvm.offJavaLangString_offset, regOff);
3191 loadWordDisp(cUnit, rlObj.lowReg, gDvm.offJavaLangString_value, regPtr);
3192 genBoundsCheck(cUnit, rlIdx.lowReg, regMax, mir->offset, pcrLabel);
3193 dvmCompilerFreeTemp(cUnit, regMax);
3194 opRegImm(cUnit, kOpAdd, regPtr, contents);
3195 opRegReg(cUnit, kOpAdd, regOff, rlIdx.lowReg);
3196 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
3197 loadBaseIndexed(cUnit, regPtr, regOff, rlResult.lowReg, 1, kUnsignedHalf);
3198 storeValue(cUnit, rlDest, rlResult);
3199 return false;
3200}
3201
3202static bool genInlinedAbsInt(CompilationUnit *cUnit, MIR *mir)
3203{
3204 RegLocation rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
3205 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
3206 RegLocation rlDest = inlinedTarget(cUnit, mir, false);;
3207 RegLocation rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
3208 int signReg = dvmCompilerAllocTemp(cUnit);
3209 /*
3210 * abs(x) = y<=x>>31, (x+y)^y.
3211 * Thumb2's IT block also yields 3 instructions, but imposes
3212 * scheduling constraints.
3213 */
3214 opRegRegImm(cUnit, kOpAsr, signReg, rlSrc.lowReg, 31);
3215 opRegRegReg(cUnit, kOpAdd, rlResult.lowReg, rlSrc.lowReg, signReg);
3216 opRegReg(cUnit, kOpXor, rlResult.lowReg, signReg);
3217 storeValue(cUnit, rlDest, rlResult);
3218 return false;
3219}
3220
3221static bool genInlinedAbsLong(CompilationUnit *cUnit, MIR *mir)
3222{
3223 RegLocation rlSrc = dvmCompilerGetSrcWide(cUnit, mir, 0, 1);
3224 RegLocation rlDest = inlinedTargetWide(cUnit, mir, false);
3225 rlSrc = loadValueWide(cUnit, rlSrc, kCoreReg);
3226 RegLocation rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
3227 int signReg = dvmCompilerAllocTemp(cUnit);
3228 /*
3229 * abs(x) = y<=x>>31, (x+y)^y.
3230 * Thumb2 IT block allows slightly shorter sequence,
3231 * but introduces a scheduling barrier. Stick with this
3232 * mechanism for now.
3233 */
3234 opRegRegImm(cUnit, kOpAsr, signReg, rlSrc.highReg, 31);
3235 opRegRegReg(cUnit, kOpAdd, rlResult.lowReg, rlSrc.lowReg, signReg);
3236 opRegRegReg(cUnit, kOpAdc, rlResult.highReg, rlSrc.highReg, signReg);
3237 opRegReg(cUnit, kOpXor, rlResult.lowReg, signReg);
3238 opRegReg(cUnit, kOpXor, rlResult.highReg, signReg);
3239 storeValueWide(cUnit, rlDest, rlResult);
3240 return false;
3241}
Bill Buzbeefd023aa2009-11-02 09:23:49 -08003242
3243/*
Bill Buzbeece46c942009-11-20 15:41:34 -08003244 * NOTE: Handles both range and non-range versions (arguments
3245 * have already been normalized by this point).
Ben Chengba4fc8b2009-06-01 13:00:29 -07003246 */
Bill Buzbeece46c942009-11-20 15:41:34 -08003247static bool handleExecuteInline(CompilationUnit *cUnit, MIR *mir)
Ben Chengba4fc8b2009-06-01 13:00:29 -07003248{
3249 DecodedInstruction *dInsn = &mir->dalvikInsn;
3250 switch( mir->dalvikInsn.opCode) {
Bill Buzbeece46c942009-11-20 15:41:34 -08003251 case OP_EXECUTE_INLINE_RANGE:
Ben Chengba4fc8b2009-06-01 13:00:29 -07003252 case OP_EXECUTE_INLINE: {
3253 unsigned int i;
3254 const InlineOperation* inLineTable = dvmGetInlineOpsTable();
Bill Buzbee50a6bf22009-07-08 13:08:04 -07003255 int offset = offsetof(InterpState, retval);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003256 int operation = dInsn->vB;
Bill Buzbee50a6bf22009-07-08 13:08:04 -07003257 switch (operation) {
3258 case INLINE_EMPTYINLINEMETHOD:
3259 return false; /* Nop */
3260 case INLINE_STRING_LENGTH:
3261 return genInlinedStringLength(cUnit, mir);
Elliott Hughesee34f592010-04-05 18:13:52 -07003262 case INLINE_STRING_IS_EMPTY:
3263 return genInlinedStringIsEmpty(cUnit, mir);
Bill Buzbee50a6bf22009-07-08 13:08:04 -07003264 case INLINE_MATH_ABS_INT:
3265 return genInlinedAbsInt(cUnit, mir);
3266 case INLINE_MATH_ABS_LONG:
3267 return genInlinedAbsLong(cUnit, mir);
3268 case INLINE_MATH_MIN_INT:
3269 return genInlinedMinMaxInt(cUnit, mir, true);
3270 case INLINE_MATH_MAX_INT:
3271 return genInlinedMinMaxInt(cUnit, mir, false);
3272 case INLINE_STRING_CHARAT:
3273 return genInlinedStringCharAt(cUnit, mir);
3274 case INLINE_MATH_SQRT:
3275 if (genInlineSqrt(cUnit, mir))
Bill Buzbee9727c3d2009-08-01 11:32:36 -07003276 return false;
Bill Buzbee50a6bf22009-07-08 13:08:04 -07003277 else
3278 break; /* Handle with C routine */
Bill Buzbee50a6bf22009-07-08 13:08:04 -07003279 case INLINE_MATH_ABS_FLOAT:
Bill Buzbee1465db52009-09-23 17:17:35 -07003280 if (genInlinedAbsFloat(cUnit, mir))
3281 return false;
3282 else
3283 break;
Bill Buzbee50a6bf22009-07-08 13:08:04 -07003284 case INLINE_MATH_ABS_DOUBLE:
Bill Buzbee1465db52009-09-23 17:17:35 -07003285 if (genInlinedAbsDouble(cUnit, mir))
3286 return false;
3287 else
3288 break;
Bill Buzbee50a6bf22009-07-08 13:08:04 -07003289 case INLINE_STRING_COMPARETO:
Bill Buzbeefd023aa2009-11-02 09:23:49 -08003290 if (genInlinedCompareTo(cUnit, mir))
3291 return false;
3292 else
3293 break;
Elliott Hughes2bdbcb62010-04-12 14:29:37 -07003294 case INLINE_STRING_FASTINDEXOF_II:
3295 if (genInlinedFastIndexOf(cUnit, mir))
Bill Buzbeefd023aa2009-11-02 09:23:49 -08003296 return false;
3297 else
3298 break;
3299 case INLINE_STRING_EQUALS:
3300 case INLINE_MATH_COS:
3301 case INLINE_MATH_SIN:
3302 break; /* Handle with C routine */
Bill Buzbee50a6bf22009-07-08 13:08:04 -07003303 default:
Bill Buzbeefc519dc2010-03-06 23:30:57 -08003304 dvmCompilerAbort(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003305 }
Bill Buzbeec6f10662010-02-09 11:16:15 -08003306 dvmCompilerFlushAllRegs(cUnit); /* Everything to home location */
Elliott Hughes6a555132010-02-25 15:41:42 -08003307 dvmCompilerClobberCallRegs(cUnit);
Bill Buzbeec6f10662010-02-09 11:16:15 -08003308 dvmCompilerClobber(cUnit, r4PC);
3309 dvmCompilerClobber(cUnit, r7);
Bill Buzbee1465db52009-09-23 17:17:35 -07003310 opRegRegImm(cUnit, kOpAdd, r4PC, rGLUE, offset);
3311 opImm(cUnit, kOpPush, (1<<r4PC) | (1<<r7));
Ben Chengbd1326d2010-04-02 15:04:53 -07003312 LOAD_FUNC_ADDR(cUnit, r4PC, (int)inLineTable[operation].func);
Bill Buzbee1465db52009-09-23 17:17:35 -07003313 genExportPC(cUnit, mir);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003314 for (i=0; i < dInsn->vA; i++) {
Bill Buzbeec6f10662010-02-09 11:16:15 -08003315 loadValueDirect(cUnit, dvmCompilerGetSrc(cUnit, mir, i), i);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003316 }
Bill Buzbee1465db52009-09-23 17:17:35 -07003317 opReg(cUnit, kOpBlx, r4PC);
3318 opRegImm(cUnit, kOpAdd, r13, 8);
Bill Buzbeece46c942009-11-20 15:41:34 -08003319 opRegImm(cUnit, kOpCmp, r0, 0); /* NULL? */
3320 ArmLIR *branchOver = opCondBranch(cUnit, kArmCondNe);
3321 loadConstant(cUnit, r0,
3322 (int) (cUnit->method->insns + mir->offset));
3323 genDispatchToHandler(cUnit, TEMPLATE_THROW_EXCEPTION_COMMON);
3324 ArmLIR *target = newLIR0(cUnit, kArmPseudoTargetLabel);
3325 target->defMask = ENCODE_ALL;
3326 branchOver->generic.target = (LIR *) target;
Ben Chengba4fc8b2009-06-01 13:00:29 -07003327 break;
3328 }
3329 default:
3330 return true;
3331 }
3332 return false;
3333}
3334
3335static bool handleFmt51l(CompilationUnit *cUnit, MIR *mir)
3336{
Bill Buzbee1465db52009-09-23 17:17:35 -07003337 //TUNING: We're using core regs here - not optimal when target is a double
Bill Buzbeec6f10662010-02-09 11:16:15 -08003338 RegLocation rlDest = dvmCompilerGetDestWide(cUnit, mir, 0, 1);
3339 RegLocation rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Ben Chengbd1326d2010-04-02 15:04:53 -07003340 loadConstantNoClobber(cUnit, rlResult.lowReg,
3341 mir->dalvikInsn.vB_wide & 0xFFFFFFFFUL);
3342 loadConstantNoClobber(cUnit, rlResult.highReg,
3343 (mir->dalvikInsn.vB_wide>>32) & 0xFFFFFFFFUL);
Bill Buzbee1465db52009-09-23 17:17:35 -07003344 storeValueWide(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003345 return false;
3346}
3347
Ben Chengba4fc8b2009-06-01 13:00:29 -07003348/*
3349 * The following are special processing routines that handle transfer of
3350 * controls between compiled code and the interpreter. Certain VM states like
3351 * Dalvik PC and special-purpose registers are reconstructed here.
3352 */
3353
Bill Buzbeebd047242010-05-13 13:02:53 -07003354/*
3355 * Insert a
3356 * b .+4
3357 * nop
3358 * pair at the beginning of a chaining cell. This serves as the
3359 * switch branch that selects between reverting to the interpreter or
3360 * not. Once the cell is chained to a translation, the cell will
3361 * contain a 32-bit branch. Subsequent chain/unchain operations will
3362 * then only alter that first 16-bits - the "b .+4" for unchaining,
3363 * and the restoration of the first half of the 32-bit branch for
3364 * rechaining.
3365 */
3366static void insertChainingSwitch(CompilationUnit *cUnit)
3367{
3368 ArmLIR *branch = newLIR0(cUnit, kThumbBUncond);
3369 newLIR2(cUnit, kThumbOrr, r0, r0);
3370 ArmLIR *target = newLIR0(cUnit, kArmPseudoTargetLabel);
3371 target->defMask = ENCODE_ALL;
3372 branch->generic.target = (LIR *) target;
3373}
3374
Ben Cheng1efc9c52009-06-08 18:25:27 -07003375/* Chaining cell for code that may need warmup. */
3376static void handleNormalChainingCell(CompilationUnit *cUnit,
3377 unsigned int offset)
Ben Chengba4fc8b2009-06-01 13:00:29 -07003378{
Ben Cheng11d8f142010-03-24 15:24:19 -07003379 /*
3380 * Use raw instruction constructors to guarantee that the generated
3381 * instructions fit the predefined cell size.
3382 */
Bill Buzbeebd047242010-05-13 13:02:53 -07003383 insertChainingSwitch(cUnit);
Ben Cheng11d8f142010-03-24 15:24:19 -07003384 newLIR3(cUnit, kThumbLdrRRI5, r0, rGLUE,
3385 offsetof(InterpState,
3386 jitToInterpEntries.dvmJitToInterpNormal) >> 2);
3387 newLIR1(cUnit, kThumbBlxR, r0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003388 addWordData(cUnit, (int) (cUnit->method->insns + offset), true);
3389}
3390
3391/*
Ben Cheng1efc9c52009-06-08 18:25:27 -07003392 * Chaining cell for instructions that immediately following already translated
3393 * code.
Ben Chengba4fc8b2009-06-01 13:00:29 -07003394 */
Ben Cheng1efc9c52009-06-08 18:25:27 -07003395static void handleHotChainingCell(CompilationUnit *cUnit,
3396 unsigned int offset)
Ben Chengba4fc8b2009-06-01 13:00:29 -07003397{
Ben Cheng11d8f142010-03-24 15:24:19 -07003398 /*
3399 * Use raw instruction constructors to guarantee that the generated
3400 * instructions fit the predefined cell size.
3401 */
Bill Buzbeebd047242010-05-13 13:02:53 -07003402 insertChainingSwitch(cUnit);
Ben Cheng11d8f142010-03-24 15:24:19 -07003403 newLIR3(cUnit, kThumbLdrRRI5, r0, rGLUE,
3404 offsetof(InterpState,
3405 jitToInterpEntries.dvmJitToInterpTraceSelect) >> 2);
3406 newLIR1(cUnit, kThumbBlxR, r0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003407 addWordData(cUnit, (int) (cUnit->method->insns + offset), true);
3408}
3409
Bill Buzbee9c4b7c82009-09-10 10:10:38 -07003410#if defined(WITH_SELF_VERIFICATION) || defined(WITH_JIT_TUNING)
Jeff Hao97319a82009-08-12 16:57:15 -07003411/* Chaining cell for branches that branch back into the same basic block */
3412static void handleBackwardBranchChainingCell(CompilationUnit *cUnit,
3413 unsigned int offset)
3414{
Ben Cheng11d8f142010-03-24 15:24:19 -07003415 /*
3416 * Use raw instruction constructors to guarantee that the generated
3417 * instructions fit the predefined cell size.
3418 */
Bill Buzbeebd047242010-05-13 13:02:53 -07003419 insertChainingSwitch(cUnit);
Bill Buzbee9c4b7c82009-09-10 10:10:38 -07003420#if defined(WITH_SELF_VERIFICATION)
Bill Buzbee1465db52009-09-23 17:17:35 -07003421 newLIR3(cUnit, kThumbLdrRRI5, r0, rGLUE,
Ben Cheng40094c12010-02-24 20:58:44 -08003422 offsetof(InterpState,
3423 jitToInterpEntries.dvmJitToInterpBackwardBranch) >> 2);
Bill Buzbee9c4b7c82009-09-10 10:10:38 -07003424#else
Bill Buzbee1465db52009-09-23 17:17:35 -07003425 newLIR3(cUnit, kThumbLdrRRI5, r0, rGLUE,
Bill Buzbee9c4b7c82009-09-10 10:10:38 -07003426 offsetof(InterpState, jitToInterpEntries.dvmJitToInterpNormal) >> 2);
3427#endif
Bill Buzbee1465db52009-09-23 17:17:35 -07003428 newLIR1(cUnit, kThumbBlxR, r0);
Jeff Hao97319a82009-08-12 16:57:15 -07003429 addWordData(cUnit, (int) (cUnit->method->insns + offset), true);
3430}
3431
3432#endif
Ben Chengba4fc8b2009-06-01 13:00:29 -07003433/* Chaining cell for monomorphic method invocations. */
Ben Cheng38329f52009-07-07 14:19:20 -07003434static void handleInvokeSingletonChainingCell(CompilationUnit *cUnit,
3435 const Method *callee)
Ben Chengba4fc8b2009-06-01 13:00:29 -07003436{
Ben Cheng11d8f142010-03-24 15:24:19 -07003437 /*
3438 * Use raw instruction constructors to guarantee that the generated
3439 * instructions fit the predefined cell size.
3440 */
Bill Buzbeebd047242010-05-13 13:02:53 -07003441 insertChainingSwitch(cUnit);
Ben Cheng11d8f142010-03-24 15:24:19 -07003442 newLIR3(cUnit, kThumbLdrRRI5, r0, rGLUE,
3443 offsetof(InterpState,
3444 jitToInterpEntries.dvmJitToInterpTraceSelect) >> 2);
3445 newLIR1(cUnit, kThumbBlxR, r0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003446 addWordData(cUnit, (int) (callee->insns), true);
3447}
3448
Ben Cheng38329f52009-07-07 14:19:20 -07003449/* Chaining cell for monomorphic method invocations. */
3450static void handleInvokePredictedChainingCell(CompilationUnit *cUnit)
3451{
3452
3453 /* Should not be executed in the initial state */
3454 addWordData(cUnit, PREDICTED_CHAIN_BX_PAIR_INIT, true);
3455 /* To be filled: class */
3456 addWordData(cUnit, PREDICTED_CHAIN_CLAZZ_INIT, true);
3457 /* To be filled: method */
3458 addWordData(cUnit, PREDICTED_CHAIN_METHOD_INIT, true);
3459 /*
3460 * Rechain count. The initial value of 0 here will trigger chaining upon
3461 * the first invocation of this callsite.
3462 */
3463 addWordData(cUnit, PREDICTED_CHAIN_COUNTER_INIT, true);
3464}
3465
Ben Chengba4fc8b2009-06-01 13:00:29 -07003466/* Load the Dalvik PC into r0 and jump to the specified target */
3467static void handlePCReconstruction(CompilationUnit *cUnit,
Bill Buzbee89efc3d2009-07-28 11:22:22 -07003468 ArmLIR *targetLabel)
Ben Chengba4fc8b2009-06-01 13:00:29 -07003469{
Bill Buzbee89efc3d2009-07-28 11:22:22 -07003470 ArmLIR **pcrLabel =
3471 (ArmLIR **) cUnit->pcReconstructionList.elemList;
Ben Chengba4fc8b2009-06-01 13:00:29 -07003472 int numElems = cUnit->pcReconstructionList.numUsed;
3473 int i;
3474 for (i = 0; i < numElems; i++) {
3475 dvmCompilerAppendLIR(cUnit, (LIR *) pcrLabel[i]);
3476 /* r0 = dalvik PC */
3477 loadConstant(cUnit, r0, pcrLabel[i]->operands[0]);
3478 genUnconditionalBranch(cUnit, targetLabel);
3479 }
3480}
3481
Bill Buzbee1465db52009-09-23 17:17:35 -07003482static char *extendedMIROpNames[kMirOpLast - kMirOpFirst] = {
3483 "kMirOpPhi",
3484 "kMirOpNullNRangeUpCheck",
3485 "kMirOpNullNRangeDownCheck",
3486 "kMirOpLowerBound",
3487 "kMirOpPunt",
Ben Cheng4238ec22009-08-24 16:32:22 -07003488};
3489
3490/*
3491 * vA = arrayReg;
3492 * vB = idxReg;
3493 * vC = endConditionReg;
3494 * arg[0] = maxC
3495 * arg[1] = minC
3496 * arg[2] = loopBranchConditionCode
3497 */
3498static void genHoistedChecksForCountUpLoop(CompilationUnit *cUnit, MIR *mir)
3499{
Bill Buzbee1465db52009-09-23 17:17:35 -07003500 /*
3501 * NOTE: these synthesized blocks don't have ssa names assigned
3502 * for Dalvik registers. However, because they dominate the following
3503 * blocks we can simply use the Dalvik name w/ subscript 0 as the
3504 * ssa name.
3505 */
Ben Cheng4238ec22009-08-24 16:32:22 -07003506 DecodedInstruction *dInsn = &mir->dalvikInsn;
3507 const int lenOffset = offsetof(ArrayObject, length);
Ben Cheng4238ec22009-08-24 16:32:22 -07003508 const int maxC = dInsn->arg[0];
Bill Buzbee1465db52009-09-23 17:17:35 -07003509 int regLength;
3510 RegLocation rlArray = cUnit->regLocation[mir->dalvikInsn.vA];
3511 RegLocation rlIdxEnd = cUnit->regLocation[mir->dalvikInsn.vC];
Ben Cheng4238ec22009-08-24 16:32:22 -07003512
3513 /* regArray <- arrayRef */
Bill Buzbee1465db52009-09-23 17:17:35 -07003514 rlArray = loadValue(cUnit, rlArray, kCoreReg);
3515 rlIdxEnd = loadValue(cUnit, rlIdxEnd, kCoreReg);
3516 genRegImmCheck(cUnit, kArmCondEq, rlArray.lowReg, 0, 0,
Ben Cheng4238ec22009-08-24 16:32:22 -07003517 (ArmLIR *) cUnit->loopAnalysis->branchToPCR);
3518
3519 /* regLength <- len(arrayRef) */
Bill Buzbeec6f10662010-02-09 11:16:15 -08003520 regLength = dvmCompilerAllocTemp(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07003521 loadWordDisp(cUnit, rlArray.lowReg, lenOffset, regLength);
Ben Cheng4238ec22009-08-24 16:32:22 -07003522
3523 int delta = maxC;
3524 /*
3525 * If the loop end condition is ">=" instead of ">", then the largest value
3526 * of the index is "endCondition - 1".
3527 */
3528 if (dInsn->arg[2] == OP_IF_GE) {
3529 delta--;
3530 }
3531
3532 if (delta) {
Bill Buzbeec6f10662010-02-09 11:16:15 -08003533 int tReg = dvmCompilerAllocTemp(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07003534 opRegRegImm(cUnit, kOpAdd, tReg, rlIdxEnd.lowReg, delta);
3535 rlIdxEnd.lowReg = tReg;
Bill Buzbeec6f10662010-02-09 11:16:15 -08003536 dvmCompilerFreeTemp(cUnit, tReg);
Ben Cheng4238ec22009-08-24 16:32:22 -07003537 }
3538 /* Punt if "regIdxEnd < len(Array)" is false */
Bill Buzbee1465db52009-09-23 17:17:35 -07003539 genRegRegCheck(cUnit, kArmCondGe, rlIdxEnd.lowReg, regLength, 0,
Ben Cheng0fd31e42009-09-03 14:40:16 -07003540 (ArmLIR *) cUnit->loopAnalysis->branchToPCR);
Ben Cheng4238ec22009-08-24 16:32:22 -07003541}
3542
3543/*
3544 * vA = arrayReg;
3545 * vB = idxReg;
3546 * vC = endConditionReg;
3547 * arg[0] = maxC
3548 * arg[1] = minC
3549 * arg[2] = loopBranchConditionCode
3550 */
3551static void genHoistedChecksForCountDownLoop(CompilationUnit *cUnit, MIR *mir)
3552{
3553 DecodedInstruction *dInsn = &mir->dalvikInsn;
3554 const int lenOffset = offsetof(ArrayObject, length);
Bill Buzbeec6f10662010-02-09 11:16:15 -08003555 const int regLength = dvmCompilerAllocTemp(cUnit);
Ben Cheng4238ec22009-08-24 16:32:22 -07003556 const int maxC = dInsn->arg[0];
Bill Buzbee1465db52009-09-23 17:17:35 -07003557 RegLocation rlArray = cUnit->regLocation[mir->dalvikInsn.vA];
3558 RegLocation rlIdxInit = cUnit->regLocation[mir->dalvikInsn.vB];
Ben Cheng4238ec22009-08-24 16:32:22 -07003559
3560 /* regArray <- arrayRef */
Bill Buzbee1465db52009-09-23 17:17:35 -07003561 rlArray = loadValue(cUnit, rlArray, kCoreReg);
3562 rlIdxInit = loadValue(cUnit, rlIdxInit, kCoreReg);
3563 genRegImmCheck(cUnit, kArmCondEq, rlArray.lowReg, 0, 0,
Ben Cheng4238ec22009-08-24 16:32:22 -07003564 (ArmLIR *) cUnit->loopAnalysis->branchToPCR);
3565
3566 /* regLength <- len(arrayRef) */
Bill Buzbee1465db52009-09-23 17:17:35 -07003567 loadWordDisp(cUnit, rlArray.lowReg, lenOffset, regLength);
Ben Cheng4238ec22009-08-24 16:32:22 -07003568
3569 if (maxC) {
Bill Buzbeec6f10662010-02-09 11:16:15 -08003570 int tReg = dvmCompilerAllocTemp(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07003571 opRegRegImm(cUnit, kOpAdd, tReg, rlIdxInit.lowReg, maxC);
3572 rlIdxInit.lowReg = tReg;
Bill Buzbeec6f10662010-02-09 11:16:15 -08003573 dvmCompilerFreeTemp(cUnit, tReg);
Ben Cheng4238ec22009-08-24 16:32:22 -07003574 }
3575
3576 /* Punt if "regIdxInit < len(Array)" is false */
Bill Buzbee1465db52009-09-23 17:17:35 -07003577 genRegRegCheck(cUnit, kArmCondGe, rlIdxInit.lowReg, regLength, 0,
Ben Cheng0fd31e42009-09-03 14:40:16 -07003578 (ArmLIR *) cUnit->loopAnalysis->branchToPCR);
Ben Cheng4238ec22009-08-24 16:32:22 -07003579}
3580
3581/*
3582 * vA = idxReg;
3583 * vB = minC;
3584 */
3585static void genHoistedLowerBoundCheck(CompilationUnit *cUnit, MIR *mir)
3586{
3587 DecodedInstruction *dInsn = &mir->dalvikInsn;
Ben Cheng4238ec22009-08-24 16:32:22 -07003588 const int minC = dInsn->vB;
Bill Buzbee1465db52009-09-23 17:17:35 -07003589 RegLocation rlIdx = cUnit->regLocation[mir->dalvikInsn.vA];
Ben Cheng4238ec22009-08-24 16:32:22 -07003590
3591 /* regIdx <- initial index value */
Bill Buzbee1465db52009-09-23 17:17:35 -07003592 rlIdx = loadValue(cUnit, rlIdx, kCoreReg);
Ben Cheng4238ec22009-08-24 16:32:22 -07003593
3594 /* Punt if "regIdxInit + minC >= 0" is false */
Bill Buzbee1465db52009-09-23 17:17:35 -07003595 genRegImmCheck(cUnit, kArmCondLt, rlIdx.lowReg, -minC, 0,
Ben Cheng4238ec22009-08-24 16:32:22 -07003596 (ArmLIR *) cUnit->loopAnalysis->branchToPCR);
3597}
3598
3599/* Extended MIR instructions like PHI */
3600static void handleExtendedMIR(CompilationUnit *cUnit, MIR *mir)
3601{
Bill Buzbee1465db52009-09-23 17:17:35 -07003602 int opOffset = mir->dalvikInsn.opCode - kMirOpFirst;
Ben Cheng4238ec22009-08-24 16:32:22 -07003603 char *msg = dvmCompilerNew(strlen(extendedMIROpNames[opOffset]) + 1,
3604 false);
3605 strcpy(msg, extendedMIROpNames[opOffset]);
Bill Buzbee1465db52009-09-23 17:17:35 -07003606 newLIR1(cUnit, kArmPseudoExtended, (int) msg);
Ben Cheng4238ec22009-08-24 16:32:22 -07003607
3608 switch (mir->dalvikInsn.opCode) {
Bill Buzbee1465db52009-09-23 17:17:35 -07003609 case kMirOpPhi: {
Ben Cheng4238ec22009-08-24 16:32:22 -07003610 char *ssaString = dvmCompilerGetSSAString(cUnit, mir->ssaRep);
Bill Buzbee1465db52009-09-23 17:17:35 -07003611 newLIR1(cUnit, kArmPseudoSSARep, (int) ssaString);
Ben Cheng4238ec22009-08-24 16:32:22 -07003612 break;
3613 }
Bill Buzbee1465db52009-09-23 17:17:35 -07003614 case kMirOpNullNRangeUpCheck: {
Ben Cheng4238ec22009-08-24 16:32:22 -07003615 genHoistedChecksForCountUpLoop(cUnit, mir);
3616 break;
3617 }
Bill Buzbee1465db52009-09-23 17:17:35 -07003618 case kMirOpNullNRangeDownCheck: {
Ben Cheng4238ec22009-08-24 16:32:22 -07003619 genHoistedChecksForCountDownLoop(cUnit, mir);
3620 break;
3621 }
Bill Buzbee1465db52009-09-23 17:17:35 -07003622 case kMirOpLowerBound: {
Ben Cheng4238ec22009-08-24 16:32:22 -07003623 genHoistedLowerBoundCheck(cUnit, mir);
3624 break;
3625 }
Bill Buzbee1465db52009-09-23 17:17:35 -07003626 case kMirOpPunt: {
Ben Cheng4238ec22009-08-24 16:32:22 -07003627 genUnconditionalBranch(cUnit,
3628 (ArmLIR *) cUnit->loopAnalysis->branchToPCR);
3629 break;
3630 }
3631 default:
3632 break;
3633 }
3634}
3635
3636/*
3637 * Create a PC-reconstruction cell for the starting offset of this trace.
3638 * Since the PCR cell is placed near the end of the compiled code which is
3639 * usually out of range for a conditional branch, we put two branches (one
3640 * branch over to the loop body and one layover branch to the actual PCR) at the
3641 * end of the entry block.
3642 */
3643static void setupLoopEntryBlock(CompilationUnit *cUnit, BasicBlock *entry,
3644 ArmLIR *bodyLabel)
3645{
3646 /* Set up the place holder to reconstruct this Dalvik PC */
3647 ArmLIR *pcrLabel = dvmCompilerNew(sizeof(ArmLIR), true);
Ben Chenga4973592010-03-31 11:59:18 -07003648 pcrLabel->opCode = kArmPseudoPCReconstructionCell;
Ben Cheng4238ec22009-08-24 16:32:22 -07003649 pcrLabel->operands[0] =
3650 (int) (cUnit->method->insns + entry->startOffset);
3651 pcrLabel->operands[1] = entry->startOffset;
3652 /* Insert the place holder to the growable list */
3653 dvmInsertGrowableList(&cUnit->pcReconstructionList, pcrLabel);
3654
3655 /*
3656 * Next, create two branches - one branch over to the loop body and the
3657 * other branch to the PCR cell to punt.
3658 */
3659 ArmLIR *branchToBody = dvmCompilerNew(sizeof(ArmLIR), true);
Bill Buzbee1465db52009-09-23 17:17:35 -07003660 branchToBody->opCode = kThumbBUncond;
Ben Cheng4238ec22009-08-24 16:32:22 -07003661 branchToBody->generic.target = (LIR *) bodyLabel;
Ben Chengdcf3e5d2009-09-11 13:42:05 -07003662 setupResourceMasks(branchToBody);
Ben Cheng4238ec22009-08-24 16:32:22 -07003663 cUnit->loopAnalysis->branchToBody = (LIR *) branchToBody;
3664
3665 ArmLIR *branchToPCR = dvmCompilerNew(sizeof(ArmLIR), true);
Bill Buzbee1465db52009-09-23 17:17:35 -07003666 branchToPCR->opCode = kThumbBUncond;
Ben Cheng4238ec22009-08-24 16:32:22 -07003667 branchToPCR->generic.target = (LIR *) pcrLabel;
Ben Chengdcf3e5d2009-09-11 13:42:05 -07003668 setupResourceMasks(branchToPCR);
Ben Cheng4238ec22009-08-24 16:32:22 -07003669 cUnit->loopAnalysis->branchToPCR = (LIR *) branchToPCR;
3670}
3671
Ben Chengd5adae12010-03-26 17:45:28 -07003672#if defined(WITH_SELF_VERIFICATION)
3673static bool selfVerificationPuntOps(MIR *mir)
3674{
3675 DecodedInstruction *decInsn = &mir->dalvikInsn;
3676 OpCode op = decInsn->opCode;
3677 int flags = dexGetInstrFlags(gDvm.instrFlags, op);
3678 /*
3679 * All opcodes that can throw exceptions and use the
3680 * TEMPLATE_THROW_EXCEPTION_COMMON template should be excluded in the trace
3681 * under self-verification mode.
3682 */
3683 return (op == OP_MONITOR_ENTER || op == OP_MONITOR_EXIT ||
3684 op == OP_NEW_INSTANCE || op == OP_NEW_ARRAY ||
3685 op == OP_CHECK_CAST || op == OP_MOVE_EXCEPTION ||
3686 op == OP_FILL_ARRAY_DATA || op == OP_EXECUTE_INLINE ||
3687 op == OP_EXECUTE_INLINE_RANGE ||
3688 (flags & kInstrInvoke));
3689}
3690#endif
3691
Ben Chengba4fc8b2009-06-01 13:00:29 -07003692void dvmCompilerMIR2LIR(CompilationUnit *cUnit)
3693{
3694 /* Used to hold the labels of each block */
Bill Buzbee89efc3d2009-07-28 11:22:22 -07003695 ArmLIR *labelList =
3696 dvmCompilerNew(sizeof(ArmLIR) * cUnit->numBlocks, true);
Ben Chengcec26f62010-01-15 15:29:33 -08003697 GrowableList chainingListByType[kChainingCellGap];
Ben Chengba4fc8b2009-06-01 13:00:29 -07003698 int i;
3699
3700 /*
Ben Cheng38329f52009-07-07 14:19:20 -07003701 * Initialize various types chaining lists.
Ben Chengba4fc8b2009-06-01 13:00:29 -07003702 */
Ben Chengcec26f62010-01-15 15:29:33 -08003703 for (i = 0; i < kChainingCellGap; i++) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07003704 dvmInitGrowableList(&chainingListByType[i], 2);
3705 }
3706
3707 BasicBlock **blockList = cUnit->blockList;
3708
Bill Buzbee6e963e12009-06-17 16:56:19 -07003709 if (cUnit->executionCount) {
3710 /*
3711 * Reserve 6 bytes at the beginning of the trace
3712 * +----------------------------+
3713 * | execution count (4 bytes) |
3714 * +----------------------------+
3715 * | chain cell offset (2 bytes)|
3716 * +----------------------------+
3717 * ...and then code to increment the execution
3718 * count:
3719 * mov r0, pc @ move adr of "mov r0,pc" + 4 to r0
3720 * sub r0, #10 @ back up to addr of executionCount
3721 * ldr r1, [r0]
3722 * add r1, #1
3723 * str r1, [r0]
3724 */
Bill Buzbee1465db52009-09-23 17:17:35 -07003725 newLIR1(cUnit, kArm16BitData, 0);
3726 newLIR1(cUnit, kArm16BitData, 0);
Ben Chengcc6600c2009-06-22 14:45:16 -07003727 cUnit->chainCellOffsetLIR =
Bill Buzbee1465db52009-09-23 17:17:35 -07003728 (LIR *) newLIR1(cUnit, kArm16BitData, CHAIN_CELL_OFFSET_TAG);
Bill Buzbee6e963e12009-06-17 16:56:19 -07003729 cUnit->headerSize = 6;
Bill Buzbee270c1d62009-08-13 16:58:07 -07003730 /* Thumb instruction used directly here to ensure correct size */
Bill Buzbee1465db52009-09-23 17:17:35 -07003731 newLIR2(cUnit, kThumbMovRR_H2L, r0, rpc);
3732 newLIR2(cUnit, kThumbSubRI8, r0, 10);
3733 newLIR3(cUnit, kThumbLdrRRI5, r1, r0, 0);
3734 newLIR2(cUnit, kThumbAddRI8, r1, 1);
3735 newLIR3(cUnit, kThumbStrRRI5, r1, r0, 0);
Bill Buzbee6e963e12009-06-17 16:56:19 -07003736 } else {
3737 /* Just reserve 2 bytes for the chain cell offset */
Ben Chengcc6600c2009-06-22 14:45:16 -07003738 cUnit->chainCellOffsetLIR =
Bill Buzbee1465db52009-09-23 17:17:35 -07003739 (LIR *) newLIR1(cUnit, kArm16BitData, CHAIN_CELL_OFFSET_TAG);
Bill Buzbee6e963e12009-06-17 16:56:19 -07003740 cUnit->headerSize = 2;
3741 }
Ben Cheng1efc9c52009-06-08 18:25:27 -07003742
Ben Chengba4fc8b2009-06-01 13:00:29 -07003743 /* Handle the content in each basic block */
3744 for (i = 0; i < cUnit->numBlocks; i++) {
3745 blockList[i]->visited = true;
3746 MIR *mir;
3747
3748 labelList[i].operands[0] = blockList[i]->startOffset;
3749
Ben Chengcec26f62010-01-15 15:29:33 -08003750 if (blockList[i]->blockType >= kChainingCellGap) {
Ben Chengd44faf52010-06-02 15:33:51 -07003751 if (blockList[i]->firstMIRInsn != NULL &&
3752 ((blockList[i]->firstMIRInsn->dalvikInsn.opCode ==
3753 OP_MOVE_RESULT) ||
3754 (blockList[i]->firstMIRInsn->dalvikInsn.opCode ==
3755 OP_MOVE_RESULT_WIDE) ||
3756 (blockList[i]->firstMIRInsn->dalvikInsn.opCode ==
3757 OP_MOVE_RESULT_OBJECT))) {
3758 /* Align this block first since it is a return chaining cell */
3759 newLIR0(cUnit, kArmPseudoPseudoAlign4);
3760 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07003761 /*
3762 * Append the label pseudo LIR first. Chaining cells will be handled
3763 * separately afterwards.
3764 */
3765 dvmCompilerAppendLIR(cUnit, (LIR *) &labelList[i]);
3766 }
3767
Bill Buzbee1465db52009-09-23 17:17:35 -07003768 if (blockList[i]->blockType == kEntryBlock) {
Ben Chenga4973592010-03-31 11:59:18 -07003769 labelList[i].opCode = kArmPseudoEntryBlock;
Ben Cheng4238ec22009-08-24 16:32:22 -07003770 if (blockList[i]->firstMIRInsn == NULL) {
3771 continue;
3772 } else {
3773 setupLoopEntryBlock(cUnit, blockList[i],
3774 &labelList[blockList[i]->fallThrough->id]);
3775 }
Bill Buzbee1465db52009-09-23 17:17:35 -07003776 } else if (blockList[i]->blockType == kExitBlock) {
Ben Chenga4973592010-03-31 11:59:18 -07003777 labelList[i].opCode = kArmPseudoExitBlock;
Ben Cheng4238ec22009-08-24 16:32:22 -07003778 goto gen_fallthrough;
Bill Buzbee1465db52009-09-23 17:17:35 -07003779 } else if (blockList[i]->blockType == kDalvikByteCode) {
3780 labelList[i].opCode = kArmPseudoNormalBlockLabel;
Ben Chenge9695e52009-06-16 16:11:47 -07003781 /* Reset the register state */
Bill Buzbeec6f10662010-02-09 11:16:15 -08003782 dvmCompilerResetRegPool(cUnit);
3783 dvmCompilerClobberAllRegs(cUnit);
3784 dvmCompilerResetNullCheck(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003785 } else {
3786 switch (blockList[i]->blockType) {
Bill Buzbee1465db52009-09-23 17:17:35 -07003787 case kChainingCellNormal:
Ben Chenga4973592010-03-31 11:59:18 -07003788 labelList[i].opCode = kArmPseudoChainingCellNormal;
Ben Chengba4fc8b2009-06-01 13:00:29 -07003789 /* handle the codegen later */
3790 dvmInsertGrowableList(
Bill Buzbee1465db52009-09-23 17:17:35 -07003791 &chainingListByType[kChainingCellNormal], (void *) i);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003792 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07003793 case kChainingCellInvokeSingleton:
Ben Cheng38329f52009-07-07 14:19:20 -07003794 labelList[i].opCode =
Ben Chenga4973592010-03-31 11:59:18 -07003795 kArmPseudoChainingCellInvokeSingleton;
Ben Chengba4fc8b2009-06-01 13:00:29 -07003796 labelList[i].operands[0] =
3797 (int) blockList[i]->containingMethod;
3798 /* handle the codegen later */
3799 dvmInsertGrowableList(
Bill Buzbee1465db52009-09-23 17:17:35 -07003800 &chainingListByType[kChainingCellInvokeSingleton],
Ben Cheng38329f52009-07-07 14:19:20 -07003801 (void *) i);
3802 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07003803 case kChainingCellInvokePredicted:
Ben Cheng38329f52009-07-07 14:19:20 -07003804 labelList[i].opCode =
Ben Chenga4973592010-03-31 11:59:18 -07003805 kArmPseudoChainingCellInvokePredicted;
Ben Cheng38329f52009-07-07 14:19:20 -07003806 /* handle the codegen later */
3807 dvmInsertGrowableList(
Bill Buzbee1465db52009-09-23 17:17:35 -07003808 &chainingListByType[kChainingCellInvokePredicted],
Ben Cheng38329f52009-07-07 14:19:20 -07003809 (void *) i);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003810 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07003811 case kChainingCellHot:
Ben Chengba4fc8b2009-06-01 13:00:29 -07003812 labelList[i].opCode =
Ben Chenga4973592010-03-31 11:59:18 -07003813 kArmPseudoChainingCellHot;
Ben Chengba4fc8b2009-06-01 13:00:29 -07003814 /* handle the codegen later */
3815 dvmInsertGrowableList(
Bill Buzbee1465db52009-09-23 17:17:35 -07003816 &chainingListByType[kChainingCellHot],
Ben Chengba4fc8b2009-06-01 13:00:29 -07003817 (void *) i);
3818 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07003819 case kPCReconstruction:
Ben Chengba4fc8b2009-06-01 13:00:29 -07003820 /* Make sure exception handling block is next */
3821 labelList[i].opCode =
Ben Chenga4973592010-03-31 11:59:18 -07003822 kArmPseudoPCReconstructionBlockLabel;
Ben Chengba4fc8b2009-06-01 13:00:29 -07003823 assert (i == cUnit->numBlocks - 2);
3824 handlePCReconstruction(cUnit, &labelList[i+1]);
3825 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07003826 case kExceptionHandling:
3827 labelList[i].opCode = kArmPseudoEHBlockLabel;
Ben Chengba4fc8b2009-06-01 13:00:29 -07003828 if (cUnit->pcReconstructionList.numUsed) {
Bill Buzbee270c1d62009-08-13 16:58:07 -07003829 loadWordDisp(cUnit, rGLUE, offsetof(InterpState,
3830 jitToInterpEntries.dvmJitToInterpPunt),
3831 r1);
Bill Buzbee1465db52009-09-23 17:17:35 -07003832 opReg(cUnit, kOpBlx, r1);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003833 }
3834 break;
Bill Buzbee9c4b7c82009-09-10 10:10:38 -07003835#if defined(WITH_SELF_VERIFICATION) || defined(WITH_JIT_TUNING)
Bill Buzbee1465db52009-09-23 17:17:35 -07003836 case kChainingCellBackwardBranch:
Jeff Hao97319a82009-08-12 16:57:15 -07003837 labelList[i].opCode =
Ben Chenga4973592010-03-31 11:59:18 -07003838 kArmPseudoChainingCellBackwardBranch;
Jeff Hao97319a82009-08-12 16:57:15 -07003839 /* handle the codegen later */
3840 dvmInsertGrowableList(
Bill Buzbee1465db52009-09-23 17:17:35 -07003841 &chainingListByType[kChainingCellBackwardBranch],
Jeff Hao97319a82009-08-12 16:57:15 -07003842 (void *) i);
3843 break;
3844#endif
Ben Chengba4fc8b2009-06-01 13:00:29 -07003845 default:
3846 break;
3847 }
3848 continue;
3849 }
Ben Chenge9695e52009-06-16 16:11:47 -07003850
Bill Buzbee89efc3d2009-07-28 11:22:22 -07003851 ArmLIR *headLIR = NULL;
Ben Chenge9695e52009-06-16 16:11:47 -07003852
Ben Chengba4fc8b2009-06-01 13:00:29 -07003853 for (mir = blockList[i]->firstMIRInsn; mir; mir = mir->next) {
Bill Buzbee1465db52009-09-23 17:17:35 -07003854
Bill Buzbeec6f10662010-02-09 11:16:15 -08003855 dvmCompilerResetRegPool(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07003856 if (gDvmJit.disableOpt & (1 << kTrackLiveTemps)) {
Bill Buzbeec6f10662010-02-09 11:16:15 -08003857 dvmCompilerClobberAllRegs(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07003858 }
3859
3860 if (gDvmJit.disableOpt & (1 << kSuppressLoads)) {
Bill Buzbeec6f10662010-02-09 11:16:15 -08003861 dvmCompilerResetDefTracking(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07003862 }
3863
3864 if (mir->dalvikInsn.opCode >= kMirOpFirst) {
Ben Cheng4238ec22009-08-24 16:32:22 -07003865 handleExtendedMIR(cUnit, mir);
3866 continue;
3867 }
3868
Bill Buzbee1465db52009-09-23 17:17:35 -07003869
Ben Chengba4fc8b2009-06-01 13:00:29 -07003870 OpCode dalvikOpCode = mir->dalvikInsn.opCode;
3871 InstructionFormat dalvikFormat =
3872 dexGetInstrFormat(gDvm.instrFormat, dalvikOpCode);
Bill Buzbee89efc3d2009-07-28 11:22:22 -07003873 ArmLIR *boundaryLIR =
Ben Chenga4973592010-03-31 11:59:18 -07003874 newLIR2(cUnit, kArmPseudoDalvikByteCodeBoundary,
Ben Chengccd6c012009-10-15 14:52:45 -07003875 mir->offset,
3876 (int) dvmCompilerGetDalvikDisassembly(&mir->dalvikInsn)
3877 );
Ben Cheng4238ec22009-08-24 16:32:22 -07003878 if (mir->ssaRep) {
3879 char *ssaString = dvmCompilerGetSSAString(cUnit, mir->ssaRep);
Bill Buzbee1465db52009-09-23 17:17:35 -07003880 newLIR1(cUnit, kArmPseudoSSARep, (int) ssaString);
Ben Cheng4238ec22009-08-24 16:32:22 -07003881 }
3882
Ben Chenge9695e52009-06-16 16:11:47 -07003883 /* Remember the first LIR for this block */
3884 if (headLIR == NULL) {
3885 headLIR = boundaryLIR;
Ben Chengd7d426a2009-09-22 11:23:36 -07003886 /* Set the first boundaryLIR as a scheduling barrier */
3887 headLIR->defMask = ENCODE_ALL;
Ben Chenge9695e52009-06-16 16:11:47 -07003888 }
Ben Cheng4238ec22009-08-24 16:32:22 -07003889
Ben Chengba4fc8b2009-06-01 13:00:29 -07003890 bool notHandled;
3891 /*
3892 * Debugging: screen the opcode first to see if it is in the
3893 * do[-not]-compile list
3894 */
3895 bool singleStepMe =
3896 gDvmJit.includeSelectedOp !=
3897 ((gDvmJit.opList[dalvikOpCode >> 3] &
3898 (1 << (dalvikOpCode & 0x7))) !=
3899 0);
Ben Chengd5adae12010-03-26 17:45:28 -07003900#if defined(WITH_SELF_VERIFICATION)
3901 if (singleStepMe == false) {
3902 singleStepMe = selfVerificationPuntOps(mir);
3903 }
3904#endif
Ben Chengba4fc8b2009-06-01 13:00:29 -07003905 if (singleStepMe || cUnit->allSingleStep) {
3906 notHandled = false;
3907 genInterpSingleStep(cUnit, mir);
3908 } else {
3909 opcodeCoverage[dalvikOpCode]++;
3910 switch (dalvikFormat) {
3911 case kFmt10t:
3912 case kFmt20t:
3913 case kFmt30t:
3914 notHandled = handleFmt10t_Fmt20t_Fmt30t(cUnit,
3915 mir, blockList[i], labelList);
3916 break;
3917 case kFmt10x:
3918 notHandled = handleFmt10x(cUnit, mir);
3919 break;
3920 case kFmt11n:
3921 case kFmt31i:
3922 notHandled = handleFmt11n_Fmt31i(cUnit, mir);
3923 break;
3924 case kFmt11x:
3925 notHandled = handleFmt11x(cUnit, mir);
3926 break;
3927 case kFmt12x:
3928 notHandled = handleFmt12x(cUnit, mir);
3929 break;
3930 case kFmt20bc:
3931 notHandled = handleFmt20bc(cUnit, mir);
3932 break;
3933 case kFmt21c:
3934 case kFmt31c:
3935 notHandled = handleFmt21c_Fmt31c(cUnit, mir);
3936 break;
3937 case kFmt21h:
3938 notHandled = handleFmt21h(cUnit, mir);
3939 break;
3940 case kFmt21s:
3941 notHandled = handleFmt21s(cUnit, mir);
3942 break;
3943 case kFmt21t:
3944 notHandled = handleFmt21t(cUnit, mir, blockList[i],
3945 labelList);
3946 break;
3947 case kFmt22b:
3948 case kFmt22s:
3949 notHandled = handleFmt22b_Fmt22s(cUnit, mir);
3950 break;
3951 case kFmt22c:
3952 notHandled = handleFmt22c(cUnit, mir);
3953 break;
3954 case kFmt22cs:
3955 notHandled = handleFmt22cs(cUnit, mir);
3956 break;
3957 case kFmt22t:
3958 notHandled = handleFmt22t(cUnit, mir, blockList[i],
3959 labelList);
3960 break;
3961 case kFmt22x:
3962 case kFmt32x:
3963 notHandled = handleFmt22x_Fmt32x(cUnit, mir);
3964 break;
3965 case kFmt23x:
3966 notHandled = handleFmt23x(cUnit, mir);
3967 break;
3968 case kFmt31t:
3969 notHandled = handleFmt31t(cUnit, mir);
3970 break;
3971 case kFmt3rc:
3972 case kFmt35c:
3973 notHandled = handleFmt35c_3rc(cUnit, mir, blockList[i],
3974 labelList);
3975 break;
3976 case kFmt3rms:
3977 case kFmt35ms:
3978 notHandled = handleFmt35ms_3rms(cUnit, mir,blockList[i],
3979 labelList);
3980 break;
3981 case kFmt3inline:
Andy McFaddenb0a05412009-11-19 10:23:41 -08003982 case kFmt3rinline:
Bill Buzbeece46c942009-11-20 15:41:34 -08003983 notHandled = handleExecuteInline(cUnit, mir);
Andy McFaddenb0a05412009-11-19 10:23:41 -08003984 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07003985 case kFmt51l:
3986 notHandled = handleFmt51l(cUnit, mir);
3987 break;
3988 default:
3989 notHandled = true;
3990 break;
3991 }
3992 }
3993 if (notHandled) {
3994 LOGE("%#06x: Opcode 0x%x (%s) / Fmt %d not handled\n",
3995 mir->offset,
Andy McFaddenc6b25c72010-06-22 11:01:20 -07003996 dalvikOpCode, dexGetOpcodeName(dalvikOpCode),
Ben Chengba4fc8b2009-06-01 13:00:29 -07003997 dalvikFormat);
Bill Buzbeefc519dc2010-03-06 23:30:57 -08003998 dvmCompilerAbort(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003999 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07004000 }
4001 }
Ben Cheng4238ec22009-08-24 16:32:22 -07004002
Bill Buzbee1465db52009-09-23 17:17:35 -07004003 if (blockList[i]->blockType == kEntryBlock) {
Ben Cheng4238ec22009-08-24 16:32:22 -07004004 dvmCompilerAppendLIR(cUnit,
4005 (LIR *) cUnit->loopAnalysis->branchToBody);
4006 dvmCompilerAppendLIR(cUnit,
4007 (LIR *) cUnit->loopAnalysis->branchToPCR);
4008 }
4009
4010 if (headLIR) {
4011 /*
4012 * Eliminate redundant loads/stores and delay stores into later
4013 * slots
4014 */
4015 dvmCompilerApplyLocalOptimizations(cUnit, (LIR *) headLIR,
4016 cUnit->lastLIRInsn);
4017 }
4018
4019gen_fallthrough:
Ben Cheng1efc9c52009-06-08 18:25:27 -07004020 /*
4021 * Check if the block is terminated due to trace length constraint -
4022 * insert an unconditional branch to the chaining cell.
4023 */
4024 if (blockList[i]->needFallThroughBranch) {
4025 genUnconditionalBranch(cUnit,
4026 &labelList[blockList[i]->fallThrough->id]);
4027 }
4028
Ben Chengba4fc8b2009-06-01 13:00:29 -07004029 }
4030
Ben Chenge9695e52009-06-16 16:11:47 -07004031 /* Handle the chaining cells in predefined order */
Ben Chengcec26f62010-01-15 15:29:33 -08004032 for (i = 0; i < kChainingCellGap; i++) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07004033 size_t j;
4034 int *blockIdList = (int *) chainingListByType[i].elemList;
4035
4036 cUnit->numChainingCells[i] = chainingListByType[i].numUsed;
4037
4038 /* No chaining cells of this type */
4039 if (cUnit->numChainingCells[i] == 0)
4040 continue;
4041
4042 /* Record the first LIR for a new type of chaining cell */
4043 cUnit->firstChainingLIR[i] = (LIR *) &labelList[blockIdList[0]];
4044
4045 for (j = 0; j < chainingListByType[i].numUsed; j++) {
4046 int blockId = blockIdList[j];
4047
4048 /* Align this chaining cell first */
Bill Buzbee1465db52009-09-23 17:17:35 -07004049 newLIR0(cUnit, kArmPseudoPseudoAlign4);
Ben Chengba4fc8b2009-06-01 13:00:29 -07004050
4051 /* Insert the pseudo chaining instruction */
4052 dvmCompilerAppendLIR(cUnit, (LIR *) &labelList[blockId]);
4053
4054
4055 switch (blockList[blockId]->blockType) {
Bill Buzbee1465db52009-09-23 17:17:35 -07004056 case kChainingCellNormal:
Ben Cheng1efc9c52009-06-08 18:25:27 -07004057 handleNormalChainingCell(cUnit,
Ben Chengba4fc8b2009-06-01 13:00:29 -07004058 blockList[blockId]->startOffset);
4059 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07004060 case kChainingCellInvokeSingleton:
Ben Cheng38329f52009-07-07 14:19:20 -07004061 handleInvokeSingletonChainingCell(cUnit,
Ben Chengba4fc8b2009-06-01 13:00:29 -07004062 blockList[blockId]->containingMethod);
4063 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07004064 case kChainingCellInvokePredicted:
Ben Cheng38329f52009-07-07 14:19:20 -07004065 handleInvokePredictedChainingCell(cUnit);
4066 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07004067 case kChainingCellHot:
Ben Cheng1efc9c52009-06-08 18:25:27 -07004068 handleHotChainingCell(cUnit,
Ben Chengba4fc8b2009-06-01 13:00:29 -07004069 blockList[blockId]->startOffset);
4070 break;
Bill Buzbee9c4b7c82009-09-10 10:10:38 -07004071#if defined(WITH_SELF_VERIFICATION) || defined(WITH_JIT_TUNING)
Bill Buzbee1465db52009-09-23 17:17:35 -07004072 case kChainingCellBackwardBranch:
Jeff Hao97319a82009-08-12 16:57:15 -07004073 handleBackwardBranchChainingCell(cUnit,
4074 blockList[blockId]->startOffset);
4075 break;
4076#endif
Ben Chengba4fc8b2009-06-01 13:00:29 -07004077 default:
Bill Buzbee1465db52009-09-23 17:17:35 -07004078 LOGE("Bad blocktype %d", blockList[blockId]->blockType);
Bill Buzbeefc519dc2010-03-06 23:30:57 -08004079 dvmCompilerAbort(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -07004080 }
4081 }
4082 }
Ben Chenge9695e52009-06-16 16:11:47 -07004083
Ben Chengcec26f62010-01-15 15:29:33 -08004084 /* Mark the bottom of chaining cells */
4085 cUnit->chainingCellBottom = (LIR *) newLIR0(cUnit, kArmChainingCellBottom);
4086
Ben Cheng6c10a972009-10-29 14:39:18 -07004087 /*
4088 * Generate the branch to the dvmJitToInterpNoChain entry point at the end
4089 * of all chaining cells for the overflow cases.
4090 */
4091 if (cUnit->switchOverflowPad) {
4092 loadConstant(cUnit, r0, (int) cUnit->switchOverflowPad);
4093 loadWordDisp(cUnit, rGLUE, offsetof(InterpState,
4094 jitToInterpEntries.dvmJitToInterpNoChain), r2);
4095 opRegReg(cUnit, kOpAdd, r1, r1);
4096 opRegRegReg(cUnit, kOpAdd, r4PC, r0, r1);
Ben Cheng978738d2010-05-13 13:45:57 -07004097#if defined(WITH_JIT_TUNING)
Ben Cheng6c10a972009-10-29 14:39:18 -07004098 loadConstant(cUnit, r0, kSwitchOverflow);
4099#endif
4100 opReg(cUnit, kOpBlx, r2);
4101 }
4102
Ben Chenge9695e52009-06-16 16:11:47 -07004103 dvmCompilerApplyGlobalOptimizations(cUnit);
jeffhao9e45c0b2010-02-03 10:24:05 -08004104
4105#if defined(WITH_SELF_VERIFICATION)
4106 selfVerificationBranchInsertPass(cUnit);
4107#endif
Ben Chengba4fc8b2009-06-01 13:00:29 -07004108}
4109
4110/* Accept the work and start compiling */
Bill Buzbee716f1202009-07-23 13:22:09 -07004111bool dvmCompilerDoWork(CompilerWorkOrder *work)
Ben Chengba4fc8b2009-06-01 13:00:29 -07004112{
Ben Chengccd6c012009-10-15 14:52:45 -07004113 bool res;
Ben Chengba4fc8b2009-06-01 13:00:29 -07004114
Ben Cheng6999d842010-01-26 16:46:15 -08004115 if (gDvmJit.codeCacheFull) {
Ben Chengccd6c012009-10-15 14:52:45 -07004116 return false;
4117 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07004118
Ben Chengccd6c012009-10-15 14:52:45 -07004119 switch (work->kind) {
4120 case kWorkOrderMethod:
4121 res = dvmCompileMethod(work->info, &work->result);
4122 break;
4123 case kWorkOrderTrace:
4124 /* Start compilation with maximally allowed trace length */
Bill Buzbeefc519dc2010-03-06 23:30:57 -08004125 res = dvmCompileTrace(work->info, JIT_MAX_TRACE_LEN, &work->result,
4126 work->bailPtr);
Ben Chengccd6c012009-10-15 14:52:45 -07004127 break;
4128 case kWorkOrderTraceDebug: {
4129 bool oldPrintMe = gDvmJit.printMe;
4130 gDvmJit.printMe = true;
4131 /* Start compilation with maximally allowed trace length */
Bill Buzbeefc519dc2010-03-06 23:30:57 -08004132 res = dvmCompileTrace(work->info, JIT_MAX_TRACE_LEN, &work->result,
4133 work->bailPtr);
Elliott Hughes672511b2010-04-26 17:40:13 -07004134 gDvmJit.printMe = oldPrintMe;
Ben Chengccd6c012009-10-15 14:52:45 -07004135 break;
4136 }
4137 default:
4138 res = false;
Bill Buzbeefc519dc2010-03-06 23:30:57 -08004139 LOGE("Jit: unknown work order type");
Elliott Hughes672511b2010-04-26 17:40:13 -07004140 assert(0); // Bail if debug build, discard otherwise
Ben Chengccd6c012009-10-15 14:52:45 -07004141 }
4142 return res;
Ben Chengba4fc8b2009-06-01 13:00:29 -07004143}
4144
Ben Chengba4fc8b2009-06-01 13:00:29 -07004145/* Architectural-specific debugging helpers go here */
4146void dvmCompilerArchDump(void)
4147{
4148 /* Print compiled opcode in this VM instance */
4149 int i, start, streak;
4150 char buf[1024];
4151
4152 streak = i = 0;
4153 buf[0] = 0;
4154 while (opcodeCoverage[i] == 0 && i < 256) {
4155 i++;
4156 }
4157 if (i == 256) {
4158 return;
4159 }
4160 for (start = i++, streak = 1; i < 256; i++) {
4161 if (opcodeCoverage[i]) {
4162 streak++;
4163 } else {
4164 if (streak == 1) {
4165 sprintf(buf+strlen(buf), "%x,", start);
4166 } else {
4167 sprintf(buf+strlen(buf), "%x-%x,", start, start + streak - 1);
4168 }
4169 streak = 0;
4170 while (opcodeCoverage[i] == 0 && i < 256) {
4171 i++;
4172 }
4173 if (i < 256) {
4174 streak = 1;
4175 start = i;
4176 }
4177 }
4178 }
4179 if (streak) {
4180 if (streak == 1) {
4181 sprintf(buf+strlen(buf), "%x", start);
4182 } else {
4183 sprintf(buf+strlen(buf), "%x-%x", start, start + streak - 1);
4184 }
4185 }
4186 if (strlen(buf)) {
Ben Cheng8b258bf2009-06-24 17:27:07 -07004187 LOGD("dalvik.vm.jit.op = %s", buf);
Ben Chengba4fc8b2009-06-01 13:00:29 -07004188 }
4189}
Ben Chengd7d426a2009-09-22 11:23:36 -07004190
4191/* Common initialization routine for an architecture family */
4192bool dvmCompilerArchInit()
4193{
4194 int i;
4195
Bill Buzbee1465db52009-09-23 17:17:35 -07004196 for (i = 0; i < kArmLast; i++) {
Ben Chengd7d426a2009-09-22 11:23:36 -07004197 if (EncodingMap[i].opCode != i) {
4198 LOGE("Encoding order for %s is wrong: expecting %d, seeing %d",
4199 EncodingMap[i].name, i, EncodingMap[i].opCode);
Bill Buzbeefc519dc2010-03-06 23:30:57 -08004200 dvmAbort(); // OK to dvmAbort - build error
Ben Chengd7d426a2009-09-22 11:23:36 -07004201 }
4202 }
4203
Ben Cheng5d90c202009-11-22 23:31:11 -08004204 return dvmCompilerArchVariantInit();
4205}
4206
4207void *dvmCompilerGetInterpretTemplate()
4208{
4209 return (void*) ((int)gDvmJit.codeCache +
4210 templateEntryOffsets[TEMPLATE_INTERPRET]);
4211}
4212
4213/* Needed by the ld/st optmizatons */
4214ArmLIR* dvmCompilerRegCopyNoInsert(CompilationUnit *cUnit, int rDest, int rSrc)
4215{
4216 return genRegCopyNoInsert(cUnit, rDest, rSrc);
4217}
4218
4219/* Needed by the register allocator */
4220ArmLIR* dvmCompilerRegCopy(CompilationUnit *cUnit, int rDest, int rSrc)
4221{
4222 return genRegCopy(cUnit, rDest, rSrc);
4223}
4224
4225/* Needed by the register allocator */
4226void dvmCompilerRegCopyWide(CompilationUnit *cUnit, int destLo, int destHi,
4227 int srcLo, int srcHi)
4228{
4229 genRegCopyWide(cUnit, destLo, destHi, srcLo, srcHi);
4230}
4231
4232void dvmCompilerFlushRegImpl(CompilationUnit *cUnit, int rBase,
4233 int displacement, int rSrc, OpSize size)
4234{
4235 storeBaseDisp(cUnit, rBase, displacement, rSrc, size);
4236}
4237
4238void dvmCompilerFlushRegWideImpl(CompilationUnit *cUnit, int rBase,
4239 int displacement, int rSrcLo, int rSrcHi)
4240{
4241 storeBaseDispWide(cUnit, rBase, displacement, rSrcLo, rSrcHi);
Ben Chengd7d426a2009-09-22 11:23:36 -07004242}