blob: 0bcdd0e963f066c547f573e8a12e0ce1e369cae6 [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 }
1665 default:
1666 return true;
1667 }
1668 return false;
1669}
1670
1671static bool handleFmt11x(CompilationUnit *cUnit, MIR *mir)
1672{
1673 OpCode dalvikOpCode = mir->dalvikInsn.opCode;
Bill Buzbee1465db52009-09-23 17:17:35 -07001674 RegLocation rlResult;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001675 switch (dalvikOpCode) {
1676 case OP_MOVE_EXCEPTION: {
1677 int offset = offsetof(InterpState, self);
1678 int exOffset = offsetof(Thread, exception);
Bill Buzbeec6f10662010-02-09 11:16:15 -08001679 int selfReg = dvmCompilerAllocTemp(cUnit);
1680 int resetReg = dvmCompilerAllocTemp(cUnit);
1681 RegLocation rlDest = dvmCompilerGetDest(cUnit, mir, 0);
1682 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Bill Buzbee1465db52009-09-23 17:17:35 -07001683 loadWordDisp(cUnit, rGLUE, offset, selfReg);
Bill Buzbeef9f33282009-11-22 12:45:30 -08001684 loadConstant(cUnit, resetReg, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07001685 loadWordDisp(cUnit, selfReg, exOffset, rlResult.lowReg);
Bill Buzbeef9f33282009-11-22 12:45:30 -08001686 storeWordDisp(cUnit, selfReg, exOffset, resetReg);
Bill Buzbee1465db52009-09-23 17:17:35 -07001687 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001688 break;
1689 }
1690 case OP_MOVE_RESULT:
1691 case OP_MOVE_RESULT_OBJECT: {
Bill Buzbeec6f10662010-02-09 11:16:15 -08001692 RegLocation rlDest = dvmCompilerGetDest(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07001693 RegLocation rlSrc = LOC_DALVIK_RETURN_VAL;
1694 rlSrc.fp = rlDest.fp;
1695 storeValue(cUnit, rlDest, rlSrc);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001696 break;
1697 }
1698 case OP_MOVE_RESULT_WIDE: {
Bill Buzbeec6f10662010-02-09 11:16:15 -08001699 RegLocation rlDest = dvmCompilerGetDestWide(cUnit, mir, 0, 1);
Bill Buzbee1465db52009-09-23 17:17:35 -07001700 RegLocation rlSrc = LOC_DALVIK_RETURN_VAL_WIDE;
1701 rlSrc.fp = rlDest.fp;
1702 storeValueWide(cUnit, rlDest, rlSrc);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001703 break;
1704 }
1705 case OP_RETURN_WIDE: {
Bill Buzbeec6f10662010-02-09 11:16:15 -08001706 RegLocation rlSrc = dvmCompilerGetSrcWide(cUnit, mir, 0, 1);
Bill Buzbee1465db52009-09-23 17:17:35 -07001707 RegLocation rlDest = LOC_DALVIK_RETURN_VAL_WIDE;
1708 rlDest.fp = rlSrc.fp;
1709 storeValueWide(cUnit, rlDest, rlSrc);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001710 genReturnCommon(cUnit,mir);
1711 break;
1712 }
1713 case OP_RETURN:
1714 case OP_RETURN_OBJECT: {
Bill Buzbeec6f10662010-02-09 11:16:15 -08001715 RegLocation rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07001716 RegLocation rlDest = LOC_DALVIK_RETURN_VAL;
1717 rlDest.fp = rlSrc.fp;
1718 storeValue(cUnit, rlDest, rlSrc);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001719 genReturnCommon(cUnit,mir);
1720 break;
1721 }
Bill Buzbee1465db52009-09-23 17:17:35 -07001722 case OP_MONITOR_EXIT:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001723 case OP_MONITOR_ENTER:
Bill Buzbeed0937ef2009-12-22 16:15:39 -08001724#if defined(WITH_DEADLOCK_PREDICTION) || defined(WITH_MONITOR_TRACKING)
Ben Cheng5d90c202009-11-22 23:31:11 -08001725 genMonitorPortable(cUnit, mir);
Bill Buzbee1465db52009-09-23 17:17:35 -07001726#else
Ben Cheng5d90c202009-11-22 23:31:11 -08001727 genMonitor(cUnit, mir);
Bill Buzbee1465db52009-09-23 17:17:35 -07001728#endif
Ben Chengba4fc8b2009-06-01 13:00:29 -07001729 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001730 case OP_THROW: {
1731 genInterpSingleStep(cUnit, mir);
1732 break;
1733 }
1734 default:
1735 return true;
1736 }
1737 return false;
1738}
1739
Bill Buzbeed45ba372009-06-15 17:00:57 -07001740static bool handleFmt12x(CompilationUnit *cUnit, MIR *mir)
1741{
1742 OpCode opCode = mir->dalvikInsn.opCode;
Bill Buzbee1465db52009-09-23 17:17:35 -07001743 RegLocation rlDest;
1744 RegLocation rlSrc;
1745 RegLocation rlResult;
Bill Buzbeed45ba372009-06-15 17:00:57 -07001746
Ben Chengba4fc8b2009-06-01 13:00:29 -07001747 if ( (opCode >= OP_ADD_INT_2ADDR) && (opCode <= OP_REM_DOUBLE_2ADDR)) {
Ben Cheng5d90c202009-11-22 23:31:11 -08001748 return genArithOp( cUnit, mir );
Ben Chengba4fc8b2009-06-01 13:00:29 -07001749 }
1750
Bill Buzbee1465db52009-09-23 17:17:35 -07001751 if (mir->ssaRep->numUses == 2)
Bill Buzbeec6f10662010-02-09 11:16:15 -08001752 rlSrc = dvmCompilerGetSrcWide(cUnit, mir, 0, 1);
Bill Buzbee1465db52009-09-23 17:17:35 -07001753 else
Bill Buzbeec6f10662010-02-09 11:16:15 -08001754 rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07001755 if (mir->ssaRep->numDefs == 2)
Bill Buzbeec6f10662010-02-09 11:16:15 -08001756 rlDest = dvmCompilerGetDestWide(cUnit, mir, 0, 1);
Bill Buzbee1465db52009-09-23 17:17:35 -07001757 else
Bill Buzbeec6f10662010-02-09 11:16:15 -08001758 rlDest = dvmCompilerGetDest(cUnit, mir, 0);
Ben Chenge9695e52009-06-16 16:11:47 -07001759
Ben Chengba4fc8b2009-06-01 13:00:29 -07001760 switch (opCode) {
Bill Buzbee1465db52009-09-23 17:17:35 -07001761 case OP_DOUBLE_TO_INT:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001762 case OP_INT_TO_FLOAT:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001763 case OP_FLOAT_TO_INT:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001764 case OP_DOUBLE_TO_FLOAT:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001765 case OP_FLOAT_TO_DOUBLE:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001766 case OP_INT_TO_DOUBLE:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001767 case OP_FLOAT_TO_LONG:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001768 case OP_LONG_TO_FLOAT:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001769 case OP_DOUBLE_TO_LONG:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001770 case OP_LONG_TO_DOUBLE:
Ben Cheng5d90c202009-11-22 23:31:11 -08001771 return genConversion(cUnit, mir);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001772 case OP_NEG_INT:
1773 case OP_NOT_INT:
Ben Cheng5d90c202009-11-22 23:31:11 -08001774 return genArithOpInt(cUnit, mir, rlDest, rlSrc, rlSrc);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001775 case OP_NEG_LONG:
1776 case OP_NOT_LONG:
Ben Cheng5d90c202009-11-22 23:31:11 -08001777 return genArithOpLong(cUnit, mir, rlDest, rlSrc, rlSrc);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001778 case OP_NEG_FLOAT:
Ben Cheng5d90c202009-11-22 23:31:11 -08001779 return genArithOpFloat(cUnit, mir, rlDest, rlSrc, rlSrc);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001780 case OP_NEG_DOUBLE:
Ben Cheng5d90c202009-11-22 23:31:11 -08001781 return genArithOpDouble(cUnit, mir, rlDest, rlSrc, rlSrc);
Bill Buzbee1465db52009-09-23 17:17:35 -07001782 case OP_MOVE_WIDE:
1783 storeValueWide(cUnit, rlDest, rlSrc);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001784 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07001785 case OP_INT_TO_LONG:
Bill Buzbeec6f10662010-02-09 11:16:15 -08001786 rlSrc = dvmCompilerUpdateLoc(cUnit, rlSrc);
1787 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Bill Buzbee964a7b02010-01-28 12:54:19 -08001788 //TUNING: shouldn't loadValueDirect already check for phys reg?
Bill Buzbee1465db52009-09-23 17:17:35 -07001789 if (rlSrc.location == kLocPhysReg) {
1790 genRegCopy(cUnit, rlResult.lowReg, rlSrc.lowReg);
1791 } else {
1792 loadValueDirect(cUnit, rlSrc, rlResult.lowReg);
1793 }
1794 opRegRegImm(cUnit, kOpAsr, rlResult.highReg,
1795 rlResult.lowReg, 31);
1796 storeValueWide(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001797 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07001798 case OP_LONG_TO_INT:
Bill Buzbeec6f10662010-02-09 11:16:15 -08001799 rlSrc = dvmCompilerUpdateLocWide(cUnit, rlSrc);
1800 rlSrc = dvmCompilerWideToNarrow(cUnit, rlSrc);
Bill Buzbee1465db52009-09-23 17:17:35 -07001801 // Intentional fallthrough
Ben Chengba4fc8b2009-06-01 13:00:29 -07001802 case OP_MOVE:
1803 case OP_MOVE_OBJECT:
Bill Buzbee1465db52009-09-23 17:17:35 -07001804 storeValue(cUnit, rlDest, rlSrc);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001805 break;
1806 case OP_INT_TO_BYTE:
Bill Buzbee1465db52009-09-23 17:17:35 -07001807 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
Bill Buzbeec6f10662010-02-09 11:16:15 -08001808 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Bill Buzbee1465db52009-09-23 17:17:35 -07001809 opRegReg(cUnit, kOp2Byte, rlResult.lowReg, rlSrc.lowReg);
1810 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001811 break;
1812 case OP_INT_TO_SHORT:
Bill Buzbee1465db52009-09-23 17:17:35 -07001813 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
Bill Buzbeec6f10662010-02-09 11:16:15 -08001814 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Bill Buzbee1465db52009-09-23 17:17:35 -07001815 opRegReg(cUnit, kOp2Short, rlResult.lowReg, rlSrc.lowReg);
1816 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001817 break;
1818 case OP_INT_TO_CHAR:
Bill Buzbee1465db52009-09-23 17:17:35 -07001819 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
Bill Buzbeec6f10662010-02-09 11:16:15 -08001820 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Bill Buzbee1465db52009-09-23 17:17:35 -07001821 opRegReg(cUnit, kOp2Char, rlResult.lowReg, rlSrc.lowReg);
1822 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001823 break;
1824 case OP_ARRAY_LENGTH: {
1825 int lenOffset = offsetof(ArrayObject, length);
Bill Buzbee1465db52009-09-23 17:17:35 -07001826 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
1827 genNullCheck(cUnit, rlSrc.sRegLow, rlSrc.lowReg,
1828 mir->offset, NULL);
Bill Buzbeec6f10662010-02-09 11:16:15 -08001829 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Bill Buzbee1465db52009-09-23 17:17:35 -07001830 loadWordDisp(cUnit, rlSrc.lowReg, lenOffset,
1831 rlResult.lowReg);
1832 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001833 break;
1834 }
1835 default:
1836 return true;
1837 }
1838 return false;
1839}
1840
1841static bool handleFmt21s(CompilationUnit *cUnit, MIR *mir)
1842{
1843 OpCode dalvikOpCode = mir->dalvikInsn.opCode;
Bill Buzbee1465db52009-09-23 17:17:35 -07001844 RegLocation rlDest;
1845 RegLocation rlResult;
1846 int BBBB = mir->dalvikInsn.vB;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001847 if (dalvikOpCode == OP_CONST_WIDE_16) {
Bill Buzbeec6f10662010-02-09 11:16:15 -08001848 rlDest = dvmCompilerGetDestWide(cUnit, mir, 0, 1);
1849 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Ben Chengbd1326d2010-04-02 15:04:53 -07001850 loadConstantNoClobber(cUnit, rlResult.lowReg, BBBB);
Bill Buzbee964a7b02010-01-28 12:54:19 -08001851 //TUNING: do high separately to avoid load dependency
Bill Buzbee1465db52009-09-23 17:17:35 -07001852 opRegRegImm(cUnit, kOpAsr, rlResult.highReg, rlResult.lowReg, 31);
1853 storeValueWide(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001854 } else if (dalvikOpCode == OP_CONST_16) {
Bill Buzbeec6f10662010-02-09 11:16:15 -08001855 rlDest = dvmCompilerGetDest(cUnit, mir, 0);
1856 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kAnyReg, true);
Ben Chengbd1326d2010-04-02 15:04:53 -07001857 loadConstantNoClobber(cUnit, rlResult.lowReg, BBBB);
Bill Buzbee1465db52009-09-23 17:17:35 -07001858 storeValue(cUnit, rlDest, rlResult);
1859 } else
Ben Chengba4fc8b2009-06-01 13:00:29 -07001860 return true;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001861 return false;
1862}
1863
1864/* Compare agaist zero */
1865static bool handleFmt21t(CompilationUnit *cUnit, MIR *mir, BasicBlock *bb,
Bill Buzbee89efc3d2009-07-28 11:22:22 -07001866 ArmLIR *labelList)
Ben Chengba4fc8b2009-06-01 13:00:29 -07001867{
1868 OpCode dalvikOpCode = mir->dalvikInsn.opCode;
Bill Buzbee89efc3d2009-07-28 11:22:22 -07001869 ArmConditionCode cond;
Bill Buzbeec6f10662010-02-09 11:16:15 -08001870 RegLocation rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07001871 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
1872 opRegImm(cUnit, kOpCmp, rlSrc.lowReg, 0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001873
Bill Buzbee270c1d62009-08-13 16:58:07 -07001874//TUNING: break this out to allow use of Thumb2 CB[N]Z
Ben Chengba4fc8b2009-06-01 13:00:29 -07001875 switch (dalvikOpCode) {
1876 case OP_IF_EQZ:
Bill Buzbee1465db52009-09-23 17:17:35 -07001877 cond = kArmCondEq;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001878 break;
1879 case OP_IF_NEZ:
Bill Buzbee1465db52009-09-23 17:17:35 -07001880 cond = kArmCondNe;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001881 break;
1882 case OP_IF_LTZ:
Bill Buzbee1465db52009-09-23 17:17:35 -07001883 cond = kArmCondLt;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001884 break;
1885 case OP_IF_GEZ:
Bill Buzbee1465db52009-09-23 17:17:35 -07001886 cond = kArmCondGe;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001887 break;
1888 case OP_IF_GTZ:
Bill Buzbee1465db52009-09-23 17:17:35 -07001889 cond = kArmCondGt;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001890 break;
1891 case OP_IF_LEZ:
Bill Buzbee1465db52009-09-23 17:17:35 -07001892 cond = kArmCondLe;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001893 break;
1894 default:
1895 cond = 0;
1896 LOGE("Unexpected opcode (%d) for Fmt21t\n", dalvikOpCode);
Bill Buzbeefc519dc2010-03-06 23:30:57 -08001897 dvmCompilerAbort(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001898 }
1899 genConditionalBranch(cUnit, cond, &labelList[bb->taken->id]);
1900 /* This mostly likely will be optimized away in a later phase */
1901 genUnconditionalBranch(cUnit, &labelList[bb->fallThrough->id]);
1902 return false;
1903}
1904
Elliott Hughesb4c05972010-02-24 16:36:18 -08001905static bool isPowerOfTwo(int x)
1906{
1907 return (x & (x - 1)) == 0;
1908}
1909
1910// Returns true if no more than two bits are set in 'x'.
1911static bool isPopCountLE2(unsigned int x)
1912{
1913 x &= x - 1;
1914 return (x & (x - 1)) == 0;
1915}
1916
1917// Returns the index of the lowest set bit in 'x'.
1918static int lowestSetBit(unsigned int x) {
1919 int bit_posn = 0;
1920 while ((x & 0xf) == 0) {
1921 bit_posn += 4;
1922 x >>= 4;
Bill Buzbee78cb0e22010-02-11 14:04:53 -08001923 }
Elliott Hughesb4c05972010-02-24 16:36:18 -08001924 while ((x & 1) == 0) {
1925 bit_posn++;
1926 x >>= 1;
1927 }
1928 return bit_posn;
1929}
1930
Elliott Hughes672511b2010-04-26 17:40:13 -07001931// Returns true if it added instructions to 'cUnit' to divide 'rlSrc' by 'lit'
1932// and store the result in 'rlDest'.
Elliott Hughesc7ad9b22010-04-28 13:52:02 -07001933static bool handleEasyDivide(CompilationUnit *cUnit, OpCode dalvikOpCode,
Elliott Hughes672511b2010-04-26 17:40:13 -07001934 RegLocation rlSrc, RegLocation rlDest, int lit)
1935{
1936 if (lit < 2 || !isPowerOfTwo(lit)) {
1937 return false;
1938 }
1939 int k = lowestSetBit(lit);
1940 if (k >= 30) {
1941 // Avoid special cases.
1942 return false;
1943 }
Elliott Hughes9c457022010-04-28 16:15:38 -07001944 bool div = (dalvikOpCode == OP_DIV_INT_LIT8 || dalvikOpCode == OP_DIV_INT_LIT16);
Elliott Hughes672511b2010-04-26 17:40:13 -07001945 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
1946 RegLocation rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Elliott Hughes9c457022010-04-28 16:15:38 -07001947 if (div) {
1948 int tReg = dvmCompilerAllocTemp(cUnit);
1949 if (lit == 2) {
1950 // Division by 2 is by far the most common division by constant.
1951 opRegRegImm(cUnit, kOpLsr, tReg, rlSrc.lowReg, 32 - k);
1952 opRegRegReg(cUnit, kOpAdd, tReg, tReg, rlSrc.lowReg);
1953 opRegRegImm(cUnit, kOpAsr, rlResult.lowReg, tReg, k);
1954 } else {
1955 opRegRegImm(cUnit, kOpAsr, tReg, rlSrc.lowReg, 31);
1956 opRegRegImm(cUnit, kOpLsr, tReg, tReg, 32 - k);
1957 opRegRegReg(cUnit, kOpAdd, tReg, tReg, rlSrc.lowReg);
1958 opRegRegImm(cUnit, kOpAsr, rlResult.lowReg, tReg, k);
1959 }
Elliott Hughes672511b2010-04-26 17:40:13 -07001960 } else {
Elliott Hughes9c457022010-04-28 16:15:38 -07001961 int cReg = dvmCompilerAllocTemp(cUnit);
1962 loadConstant(cUnit, cReg, lit - 1);
1963 int tReg1 = dvmCompilerAllocTemp(cUnit);
1964 int tReg2 = dvmCompilerAllocTemp(cUnit);
1965 if (lit == 2) {
1966 opRegRegImm(cUnit, kOpLsr, tReg1, rlSrc.lowReg, 32 - k);
1967 opRegRegReg(cUnit, kOpAdd, tReg2, tReg1, rlSrc.lowReg);
1968 opRegRegReg(cUnit, kOpAnd, tReg2, tReg2, cReg);
1969 opRegRegReg(cUnit, kOpSub, rlResult.lowReg, tReg2, tReg1);
1970 } else {
1971 opRegRegImm(cUnit, kOpAsr, tReg1, rlSrc.lowReg, 31);
1972 opRegRegImm(cUnit, kOpLsr, tReg1, tReg1, 32 - k);
1973 opRegRegReg(cUnit, kOpAdd, tReg2, tReg1, rlSrc.lowReg);
1974 opRegRegReg(cUnit, kOpAnd, tReg2, tReg2, cReg);
1975 opRegRegReg(cUnit, kOpSub, rlResult.lowReg, tReg2, tReg1);
1976 }
Elliott Hughes672511b2010-04-26 17:40:13 -07001977 }
1978 storeValue(cUnit, rlDest, rlResult);
1979 return true;
1980}
1981
Elliott Hughesb4c05972010-02-24 16:36:18 -08001982// Returns true if it added instructions to 'cUnit' to multiply 'rlSrc' by 'lit'
1983// and store the result in 'rlDest'.
1984static bool handleEasyMultiply(CompilationUnit *cUnit,
1985 RegLocation rlSrc, RegLocation rlDest, int lit)
1986{
1987 // Can we simplify this multiplication?
1988 bool powerOfTwo = false;
1989 bool popCountLE2 = false;
1990 bool powerOfTwoMinusOne = false;
1991 if (lit < 2) {
1992 // Avoid special cases.
1993 return false;
1994 } else if (isPowerOfTwo(lit)) {
1995 powerOfTwo = true;
1996 } else if (isPopCountLE2(lit)) {
1997 popCountLE2 = true;
1998 } else if (isPowerOfTwo(lit + 1)) {
1999 powerOfTwoMinusOne = true;
2000 } else {
2001 return false;
2002 }
2003 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
2004 RegLocation rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
2005 if (powerOfTwo) {
2006 // Shift.
2007 opRegRegImm(cUnit, kOpLsl, rlResult.lowReg, rlSrc.lowReg,
2008 lowestSetBit(lit));
2009 } else if (popCountLE2) {
2010 // Shift and add and shift.
2011 int firstBit = lowestSetBit(lit);
2012 int secondBit = lowestSetBit(lit ^ (1 << firstBit));
2013 genMultiplyByTwoBitMultiplier(cUnit, rlSrc, rlResult, lit,
2014 firstBit, secondBit);
2015 } else {
2016 // Reverse subtract: (src << (shift + 1)) - src.
2017 assert(powerOfTwoMinusOne);
2018 // TODO: rsb dst, src, src lsl#lowestSetBit(lit + 1)
2019 int tReg = dvmCompilerAllocTemp(cUnit);
2020 opRegRegImm(cUnit, kOpLsl, tReg, rlSrc.lowReg, lowestSetBit(lit + 1));
2021 opRegRegReg(cUnit, kOpSub, rlResult.lowReg, tReg, rlSrc.lowReg);
2022 }
2023 storeValue(cUnit, rlDest, rlResult);
2024 return true;
Bill Buzbee78cb0e22010-02-11 14:04:53 -08002025}
2026
Ben Chengba4fc8b2009-06-01 13:00:29 -07002027static bool handleFmt22b_Fmt22s(CompilationUnit *cUnit, MIR *mir)
2028{
2029 OpCode dalvikOpCode = mir->dalvikInsn.opCode;
Bill Buzbeec6f10662010-02-09 11:16:15 -08002030 RegLocation rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
2031 RegLocation rlDest = dvmCompilerGetDest(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07002032 RegLocation rlResult;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002033 int lit = mir->dalvikInsn.vC;
Ben Cheng4f489172009-09-27 17:08:35 -07002034 OpKind op = 0; /* Make gcc happy */
Bill Buzbee1465db52009-09-23 17:17:35 -07002035 int shiftOp = false;
2036 bool isDiv = false;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002037
Ben Chengba4fc8b2009-06-01 13:00:29 -07002038 switch (dalvikOpCode) {
Bill Buzbee1465db52009-09-23 17:17:35 -07002039 case OP_RSUB_INT_LIT8:
2040 case OP_RSUB_INT: {
2041 int tReg;
2042 //TUNING: add support for use of Arm rsub op
2043 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
Bill Buzbeec6f10662010-02-09 11:16:15 -08002044 tReg = dvmCompilerAllocTemp(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07002045 loadConstant(cUnit, tReg, lit);
Bill Buzbeec6f10662010-02-09 11:16:15 -08002046 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Bill Buzbee1465db52009-09-23 17:17:35 -07002047 opRegRegReg(cUnit, kOpSub, rlResult.lowReg,
2048 tReg, rlSrc.lowReg);
2049 storeValue(cUnit, rlDest, rlResult);
2050 return false;
2051 break;
2052 }
2053
Ben Chengba4fc8b2009-06-01 13:00:29 -07002054 case OP_ADD_INT_LIT8:
2055 case OP_ADD_INT_LIT16:
Bill Buzbee1465db52009-09-23 17:17:35 -07002056 op = kOpAdd;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002057 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002058 case OP_MUL_INT_LIT8:
Bill Buzbee78cb0e22010-02-11 14:04:53 -08002059 case OP_MUL_INT_LIT16: {
Elliott Hughesb4c05972010-02-24 16:36:18 -08002060 if (handleEasyMultiply(cUnit, rlSrc, rlDest, lit)) {
2061 return false;
Bill Buzbee78cb0e22010-02-11 14:04:53 -08002062 }
Elliott Hughesb4c05972010-02-24 16:36:18 -08002063 op = kOpMul;
Bill Buzbee1465db52009-09-23 17:17:35 -07002064 break;
Bill Buzbee78cb0e22010-02-11 14:04:53 -08002065 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07002066 case OP_AND_INT_LIT8:
2067 case OP_AND_INT_LIT16:
Bill Buzbee1465db52009-09-23 17:17:35 -07002068 op = kOpAnd;
2069 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002070 case OP_OR_INT_LIT8:
2071 case OP_OR_INT_LIT16:
Bill Buzbee1465db52009-09-23 17:17:35 -07002072 op = kOpOr;
2073 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002074 case OP_XOR_INT_LIT8:
2075 case OP_XOR_INT_LIT16:
Bill Buzbee1465db52009-09-23 17:17:35 -07002076 op = kOpXor;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002077 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002078 case OP_SHL_INT_LIT8:
Bill Buzbee0e605272009-12-01 14:28:05 -08002079 lit &= 31;
Bill Buzbee1465db52009-09-23 17:17:35 -07002080 shiftOp = true;
2081 op = kOpLsl;
2082 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002083 case OP_SHR_INT_LIT8:
Bill Buzbee0e605272009-12-01 14:28:05 -08002084 lit &= 31;
Bill Buzbee1465db52009-09-23 17:17:35 -07002085 shiftOp = true;
2086 op = kOpAsr;
2087 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002088 case OP_USHR_INT_LIT8:
Bill Buzbee0e605272009-12-01 14:28:05 -08002089 lit &= 31;
Bill Buzbee1465db52009-09-23 17:17:35 -07002090 shiftOp = true;
2091 op = kOpLsr;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002092 break;
2093
2094 case OP_DIV_INT_LIT8:
2095 case OP_DIV_INT_LIT16:
Ben Chengba4fc8b2009-06-01 13:00:29 -07002096 case OP_REM_INT_LIT8:
2097 case OP_REM_INT_LIT16:
2098 if (lit == 0) {
2099 /* Let the interpreter deal with div by 0 */
2100 genInterpSingleStep(cUnit, mir);
2101 return false;
2102 }
Elliott Hughesc7ad9b22010-04-28 13:52:02 -07002103 if (handleEasyDivide(cUnit, dalvikOpCode, rlSrc, rlDest, lit)) {
Elliott Hughes672511b2010-04-26 17:40:13 -07002104 return false;
2105 }
Bill Buzbeec6f10662010-02-09 11:16:15 -08002106 dvmCompilerFlushAllRegs(cUnit); /* Everything to home location */
Bill Buzbee1465db52009-09-23 17:17:35 -07002107 loadValueDirectFixed(cUnit, rlSrc, r0);
Bill Buzbeec6f10662010-02-09 11:16:15 -08002108 dvmCompilerClobber(cUnit, r0);
Bill Buzbee1465db52009-09-23 17:17:35 -07002109 if ((dalvikOpCode == OP_DIV_INT_LIT8) ||
2110 (dalvikOpCode == OP_DIV_INT_LIT16)) {
Ben Chengbd1326d2010-04-02 15:04:53 -07002111 LOAD_FUNC_ADDR(cUnit, r2, (int)__aeabi_idiv);
Bill Buzbee1465db52009-09-23 17:17:35 -07002112 isDiv = true;
2113 } else {
Ben Chengbd1326d2010-04-02 15:04:53 -07002114 LOAD_FUNC_ADDR(cUnit, r2, (int)__aeabi_idivmod);
Bill Buzbee1465db52009-09-23 17:17:35 -07002115 isDiv = false;
2116 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07002117 loadConstant(cUnit, r1, lit);
Bill Buzbee1465db52009-09-23 17:17:35 -07002118 opReg(cUnit, kOpBlx, r2);
Elliott Hughes6a555132010-02-25 15:41:42 -08002119 dvmCompilerClobberCallRegs(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07002120 if (isDiv)
Bill Buzbeec6f10662010-02-09 11:16:15 -08002121 rlResult = dvmCompilerGetReturn(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07002122 else
Bill Buzbeec6f10662010-02-09 11:16:15 -08002123 rlResult = dvmCompilerGetReturnAlt(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07002124 storeValue(cUnit, rlDest, rlResult);
2125 return false;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002126 break;
2127 default:
2128 return true;
2129 }
Bill Buzbee1465db52009-09-23 17:17:35 -07002130 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
Bill Buzbeec6f10662010-02-09 11:16:15 -08002131 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Bill Buzbee1465db52009-09-23 17:17:35 -07002132 // Avoid shifts by literal 0 - no support in Thumb. Change to copy
2133 if (shiftOp && (lit == 0)) {
2134 genRegCopy(cUnit, rlResult.lowReg, rlSrc.lowReg);
2135 } else {
2136 opRegRegImm(cUnit, op, rlResult.lowReg, rlSrc.lowReg, lit);
2137 }
2138 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002139 return false;
2140}
2141
2142static bool handleFmt22c(CompilationUnit *cUnit, MIR *mir)
2143{
2144 OpCode dalvikOpCode = mir->dalvikInsn.opCode;
2145 int fieldOffset;
buzbeeecf8f6e2010-07-20 14:53:42 -07002146 bool isVolatile = false;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002147
2148 if (dalvikOpCode >= OP_IGET && dalvikOpCode <= OP_IPUT_SHORT) {
buzbeeecf8f6e2010-07-20 14:53:42 -07002149 Field *fieldPtr =
Ben Chengba4fc8b2009-06-01 13:00:29 -07002150 cUnit->method->clazz->pDvmDex->pResFields[mir->dalvikInsn.vC];
Ben Chengba4fc8b2009-06-01 13:00:29 -07002151
buzbeeecf8f6e2010-07-20 14:53:42 -07002152 if (fieldPtr == NULL) {
Ben Chengdd6e8702010-05-07 13:05:47 -07002153 LOGE("Unexpected null instance field");
2154 dvmAbort();
2155 }
buzbeeecf8f6e2010-07-20 14:53:42 -07002156 isVolatile = dvmIsVolatileField(fieldPtr);
2157 fieldOffset = ((InstField *)fieldPtr)->byteOffset;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002158 } else {
Ben Chenga0e7b602009-10-13 23:09:01 -07002159 /* Deliberately break the code while make the compiler happy */
2160 fieldOffset = -1;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002161 }
2162 switch (dalvikOpCode) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07002163 case OP_NEW_ARRAY: {
Bill Buzbee1465db52009-09-23 17:17:35 -07002164 // Generates a call - use explicit registers
Bill Buzbeec6f10662010-02-09 11:16:15 -08002165 RegLocation rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
2166 RegLocation rlDest = dvmCompilerGetDest(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07002167 RegLocation rlResult;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002168 void *classPtr = (void*)
2169 (cUnit->method->clazz->pDvmDex->pResClasses[mir->dalvikInsn.vC]);
Ben Chengdd6e8702010-05-07 13:05:47 -07002170
2171 if (classPtr == NULL) {
2172 LOGE("Unexpected null class");
2173 dvmAbort();
2174 }
2175
Bill Buzbeec6f10662010-02-09 11:16:15 -08002176 dvmCompilerFlushAllRegs(cUnit); /* Everything to home location */
Bill Buzbee1465db52009-09-23 17:17:35 -07002177 genExportPC(cUnit, mir);
2178 loadValueDirectFixed(cUnit, rlSrc, r1); /* Len */
Ben Chengba4fc8b2009-06-01 13:00:29 -07002179 loadConstant(cUnit, r0, (int) classPtr );
Ben Chengbd1326d2010-04-02 15:04:53 -07002180 LOAD_FUNC_ADDR(cUnit, r3, (int)dvmAllocArrayByClass);
Ben Cheng4f489172009-09-27 17:08:35 -07002181 /*
2182 * "len < 0": bail to the interpreter to re-execute the
2183 * instruction
2184 */
Carl Shapiroe3c01da2010-05-20 22:54:18 -07002185 genRegImmCheck(cUnit, kArmCondMi, r1, 0, mir->offset, NULL);
Bill Buzbee270c1d62009-08-13 16:58:07 -07002186 loadConstant(cUnit, r2, ALLOC_DONT_TRACK);
Bill Buzbee1465db52009-09-23 17:17:35 -07002187 opReg(cUnit, kOpBlx, r3);
Elliott Hughes6a555132010-02-25 15:41:42 -08002188 dvmCompilerClobberCallRegs(cUnit);
Ben Cheng4f489172009-09-27 17:08:35 -07002189 /* generate a branch over if allocation is successful */
Bill Buzbee1465db52009-09-23 17:17:35 -07002190 opRegImm(cUnit, kOpCmp, r0, 0); /* NULL? */
2191 ArmLIR *branchOver = opCondBranch(cUnit, kArmCondNe);
Ben Cheng4f489172009-09-27 17:08:35 -07002192 /*
2193 * OOM exception needs to be thrown here and cannot re-execute
2194 */
2195 loadConstant(cUnit, r0,
2196 (int) (cUnit->method->insns + mir->offset));
2197 genDispatchToHandler(cUnit, TEMPLATE_THROW_EXCEPTION_COMMON);
2198 /* noreturn */
2199
Bill Buzbee1465db52009-09-23 17:17:35 -07002200 ArmLIR *target = newLIR0(cUnit, kArmPseudoTargetLabel);
Ben Cheng4f489172009-09-27 17:08:35 -07002201 target->defMask = ENCODE_ALL;
2202 branchOver->generic.target = (LIR *) target;
Bill Buzbeec6f10662010-02-09 11:16:15 -08002203 rlResult = dvmCompilerGetReturn(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07002204 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002205 break;
2206 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07002207 case OP_INSTANCE_OF: {
Bill Buzbee1465db52009-09-23 17:17:35 -07002208 // May generate a call - use explicit registers
Bill Buzbeec6f10662010-02-09 11:16:15 -08002209 RegLocation rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
2210 RegLocation rlDest = dvmCompilerGetDest(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07002211 RegLocation rlResult;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002212 ClassObject *classPtr =
2213 (cUnit->method->clazz->pDvmDex->pResClasses[mir->dalvikInsn.vC]);
Bill Buzbee480e6782010-01-27 15:43:08 -08002214 /*
2215 * Note: It is possible that classPtr is NULL at this point,
2216 * even though this instruction has been successfully interpreted.
2217 * If the previous interpretation had a null source, the
2218 * interpreter would not have bothered to resolve the clazz.
2219 * Bail out to the interpreter in this case, and log it
2220 * so that we can tell if it happens frequently.
2221 */
2222 if (classPtr == NULL) {
2223 LOGD("null clazz in OP_INSTANCE_OF, single-stepping");
2224 genInterpSingleStep(cUnit, mir);
2225 break;
2226 }
Bill Buzbeec6f10662010-02-09 11:16:15 -08002227 dvmCompilerFlushAllRegs(cUnit); /* Everything to home location */
Bill Buzbee1465db52009-09-23 17:17:35 -07002228 loadValueDirectFixed(cUnit, rlSrc, r0); /* Ref */
Ben Chengba4fc8b2009-06-01 13:00:29 -07002229 loadConstant(cUnit, r2, (int) classPtr );
Bill Buzbee270c1d62009-08-13 16:58:07 -07002230//TUNING: compare to 0 primative to allow use of CB[N]Z
Bill Buzbee1465db52009-09-23 17:17:35 -07002231 opRegImm(cUnit, kOpCmp, r0, 0); /* NULL? */
Ben Cheng752c7942009-06-22 10:50:07 -07002232 /* When taken r0 has NULL which can be used for store directly */
Bill Buzbee1465db52009-09-23 17:17:35 -07002233 ArmLIR *branch1 = opCondBranch(cUnit, kArmCondEq);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002234 /* r1 now contains object->clazz */
Bill Buzbee270c1d62009-08-13 16:58:07 -07002235 loadWordDisp(cUnit, r0, offsetof(Object, clazz), r1);
Bill Buzbee1465db52009-09-23 17:17:35 -07002236 /* r1 now contains object->clazz */
Ben Chengbd1326d2010-04-02 15:04:53 -07002237 LOAD_FUNC_ADDR(cUnit, r3, (int)dvmInstanceofNonTrivial);
Ben Cheng752c7942009-06-22 10:50:07 -07002238 loadConstant(cUnit, r0, 1); /* Assume true */
Bill Buzbee1465db52009-09-23 17:17:35 -07002239 opRegReg(cUnit, kOpCmp, r1, r2);
2240 ArmLIR *branch2 = opCondBranch(cUnit, kArmCondEq);
2241 genRegCopy(cUnit, r0, r1);
2242 genRegCopy(cUnit, r1, r2);
2243 opReg(cUnit, kOpBlx, r3);
Elliott Hughes6a555132010-02-25 15:41:42 -08002244 dvmCompilerClobberCallRegs(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002245 /* branch target here */
Bill Buzbee1465db52009-09-23 17:17:35 -07002246 ArmLIR *target = newLIR0(cUnit, kArmPseudoTargetLabel);
Ben Chengd7d426a2009-09-22 11:23:36 -07002247 target->defMask = ENCODE_ALL;
Bill Buzbeec6f10662010-02-09 11:16:15 -08002248 rlResult = dvmCompilerGetReturn(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07002249 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002250 branch1->generic.target = (LIR *)target;
2251 branch2->generic.target = (LIR *)target;
2252 break;
2253 }
2254 case OP_IGET_WIDE:
2255 genIGetWide(cUnit, mir, fieldOffset);
2256 break;
buzbeeecf8f6e2010-07-20 14:53:42 -07002257 case OP_IGET_VOLATILE:
2258 case OP_IGET_OBJECT_VOLATILE:
2259 isVolatile = true;
2260 // NOTE: intentional fallthrough
Ben Chengba4fc8b2009-06-01 13:00:29 -07002261 case OP_IGET:
2262 case OP_IGET_OBJECT:
buzbeeecf8f6e2010-07-20 14:53:42 -07002263 genIGet(cUnit, mir, kWord, fieldOffset, isVolatile);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002264 break;
2265 case OP_IGET_BOOLEAN:
buzbeeecf8f6e2010-07-20 14:53:42 -07002266 genIGet(cUnit, mir, kUnsignedByte, fieldOffset, isVolatile);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002267 break;
2268 case OP_IGET_BYTE:
buzbeeecf8f6e2010-07-20 14:53:42 -07002269 genIGet(cUnit, mir, kSignedByte, fieldOffset, isVolatile);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002270 break;
2271 case OP_IGET_CHAR:
buzbeeecf8f6e2010-07-20 14:53:42 -07002272 genIGet(cUnit, mir, kUnsignedHalf, fieldOffset, isVolatile);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002273 break;
2274 case OP_IGET_SHORT:
buzbeeecf8f6e2010-07-20 14:53:42 -07002275 genIGet(cUnit, mir, kSignedHalf, fieldOffset, isVolatile);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002276 break;
2277 case OP_IPUT_WIDE:
2278 genIPutWide(cUnit, mir, fieldOffset);
2279 break;
2280 case OP_IPUT:
buzbeeecf8f6e2010-07-20 14:53:42 -07002281 genIPut(cUnit, mir, kWord, fieldOffset, false, isVolatile);
buzbee919eb062010-07-12 12:59:22 -07002282 break;
buzbeeecf8f6e2010-07-20 14:53:42 -07002283 case OP_IPUT_OBJECT_VOLATILE:
2284 isVolatile = true;
2285 // NOTE: intentional fallthrough
Ben Chengba4fc8b2009-06-01 13:00:29 -07002286 case OP_IPUT_OBJECT:
buzbeeecf8f6e2010-07-20 14:53:42 -07002287 genIPut(cUnit, mir, kWord, fieldOffset, true, isVolatile);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002288 break;
2289 case OP_IPUT_SHORT:
2290 case OP_IPUT_CHAR:
buzbeeecf8f6e2010-07-20 14:53:42 -07002291 genIPut(cUnit, mir, kUnsignedHalf, fieldOffset, false, isVolatile);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002292 break;
2293 case OP_IPUT_BYTE:
buzbeeecf8f6e2010-07-20 14:53:42 -07002294 genIPut(cUnit, mir, kSignedByte, fieldOffset, false, isVolatile);
2295 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002296 case OP_IPUT_BOOLEAN:
buzbeeecf8f6e2010-07-20 14:53:42 -07002297 genIPut(cUnit, mir, kUnsignedByte, fieldOffset, false, isVolatile);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002298 break;
Bill Buzbeeb16344a2010-03-15 17:19:12 -07002299 case OP_IGET_WIDE_VOLATILE:
2300 case OP_IPUT_WIDE_VOLATILE:
2301 case OP_SGET_WIDE_VOLATILE:
2302 case OP_SPUT_WIDE_VOLATILE:
2303 genInterpSingleStep(cUnit, mir);
2304 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002305 default:
2306 return true;
2307 }
2308 return false;
2309}
2310
2311static bool handleFmt22cs(CompilationUnit *cUnit, MIR *mir)
2312{
2313 OpCode dalvikOpCode = mir->dalvikInsn.opCode;
2314 int fieldOffset = mir->dalvikInsn.vC;
2315 switch (dalvikOpCode) {
2316 case OP_IGET_QUICK:
2317 case OP_IGET_OBJECT_QUICK:
buzbeeecf8f6e2010-07-20 14:53:42 -07002318 genIGet(cUnit, mir, kWord, fieldOffset, false);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002319 break;
2320 case OP_IPUT_QUICK:
buzbeeecf8f6e2010-07-20 14:53:42 -07002321 genIPut(cUnit, mir, kWord, fieldOffset, false, false);
buzbee919eb062010-07-12 12:59:22 -07002322 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002323 case OP_IPUT_OBJECT_QUICK:
buzbeeecf8f6e2010-07-20 14:53:42 -07002324 genIPut(cUnit, mir, kWord, fieldOffset, true, false);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002325 break;
2326 case OP_IGET_WIDE_QUICK:
2327 genIGetWide(cUnit, mir, fieldOffset);
2328 break;
2329 case OP_IPUT_WIDE_QUICK:
2330 genIPutWide(cUnit, mir, fieldOffset);
2331 break;
2332 default:
2333 return true;
2334 }
2335 return false;
2336
2337}
2338
2339/* Compare agaist zero */
2340static bool handleFmt22t(CompilationUnit *cUnit, MIR *mir, BasicBlock *bb,
Bill Buzbee89efc3d2009-07-28 11:22:22 -07002341 ArmLIR *labelList)
Ben Chengba4fc8b2009-06-01 13:00:29 -07002342{
2343 OpCode dalvikOpCode = mir->dalvikInsn.opCode;
Bill Buzbee89efc3d2009-07-28 11:22:22 -07002344 ArmConditionCode cond;
Bill Buzbeec6f10662010-02-09 11:16:15 -08002345 RegLocation rlSrc1 = dvmCompilerGetSrc(cUnit, mir, 0);
2346 RegLocation rlSrc2 = dvmCompilerGetSrc(cUnit, mir, 1);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002347
Bill Buzbee1465db52009-09-23 17:17:35 -07002348 rlSrc1 = loadValue(cUnit, rlSrc1, kCoreReg);
2349 rlSrc2 = loadValue(cUnit, rlSrc2, kCoreReg);
2350 opRegReg(cUnit, kOpCmp, rlSrc1.lowReg, rlSrc2.lowReg);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002351
2352 switch (dalvikOpCode) {
2353 case OP_IF_EQ:
Bill Buzbee1465db52009-09-23 17:17:35 -07002354 cond = kArmCondEq;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002355 break;
2356 case OP_IF_NE:
Bill Buzbee1465db52009-09-23 17:17:35 -07002357 cond = kArmCondNe;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002358 break;
2359 case OP_IF_LT:
Bill Buzbee1465db52009-09-23 17:17:35 -07002360 cond = kArmCondLt;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002361 break;
2362 case OP_IF_GE:
Bill Buzbee1465db52009-09-23 17:17:35 -07002363 cond = kArmCondGe;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002364 break;
2365 case OP_IF_GT:
Bill Buzbee1465db52009-09-23 17:17:35 -07002366 cond = kArmCondGt;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002367 break;
2368 case OP_IF_LE:
Bill Buzbee1465db52009-09-23 17:17:35 -07002369 cond = kArmCondLe;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002370 break;
2371 default:
2372 cond = 0;
2373 LOGE("Unexpected opcode (%d) for Fmt22t\n", dalvikOpCode);
Bill Buzbeefc519dc2010-03-06 23:30:57 -08002374 dvmCompilerAbort(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002375 }
2376 genConditionalBranch(cUnit, cond, &labelList[bb->taken->id]);
2377 /* This mostly likely will be optimized away in a later phase */
2378 genUnconditionalBranch(cUnit, &labelList[bb->fallThrough->id]);
2379 return false;
2380}
2381
2382static bool handleFmt22x_Fmt32x(CompilationUnit *cUnit, MIR *mir)
2383{
2384 OpCode opCode = mir->dalvikInsn.opCode;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002385
2386 switch (opCode) {
2387 case OP_MOVE_16:
2388 case OP_MOVE_OBJECT_16:
2389 case OP_MOVE_FROM16:
Ben Chenge9695e52009-06-16 16:11:47 -07002390 case OP_MOVE_OBJECT_FROM16: {
Bill Buzbeec6f10662010-02-09 11:16:15 -08002391 storeValue(cUnit, dvmCompilerGetDest(cUnit, mir, 0),
2392 dvmCompilerGetSrc(cUnit, mir, 0));
Ben Chengba4fc8b2009-06-01 13:00:29 -07002393 break;
Ben Chenge9695e52009-06-16 16:11:47 -07002394 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07002395 case OP_MOVE_WIDE_16:
Ben Chenge9695e52009-06-16 16:11:47 -07002396 case OP_MOVE_WIDE_FROM16: {
Bill Buzbeec6f10662010-02-09 11:16:15 -08002397 storeValueWide(cUnit, dvmCompilerGetDestWide(cUnit, mir, 0, 1),
2398 dvmCompilerGetSrcWide(cUnit, mir, 0, 1));
Ben Chengba4fc8b2009-06-01 13:00:29 -07002399 break;
Ben Chenge9695e52009-06-16 16:11:47 -07002400 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07002401 default:
2402 return true;
2403 }
2404 return false;
2405}
2406
2407static bool handleFmt23x(CompilationUnit *cUnit, MIR *mir)
2408{
2409 OpCode opCode = mir->dalvikInsn.opCode;
Bill Buzbee1465db52009-09-23 17:17:35 -07002410 RegLocation rlSrc1;
2411 RegLocation rlSrc2;
2412 RegLocation rlDest;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002413
2414 if ( (opCode >= OP_ADD_INT) && (opCode <= OP_REM_DOUBLE)) {
Ben Cheng5d90c202009-11-22 23:31:11 -08002415 return genArithOp( cUnit, mir );
Ben Chengba4fc8b2009-06-01 13:00:29 -07002416 }
2417
Bill Buzbee1465db52009-09-23 17:17:35 -07002418 /* APUTs have 3 sources and no targets */
2419 if (mir->ssaRep->numDefs == 0) {
2420 if (mir->ssaRep->numUses == 3) {
Bill Buzbeec6f10662010-02-09 11:16:15 -08002421 rlDest = dvmCompilerGetSrc(cUnit, mir, 0);
2422 rlSrc1 = dvmCompilerGetSrc(cUnit, mir, 1);
2423 rlSrc2 = dvmCompilerGetSrc(cUnit, mir, 2);
Bill Buzbee1465db52009-09-23 17:17:35 -07002424 } else {
2425 assert(mir->ssaRep->numUses == 4);
Bill Buzbeec6f10662010-02-09 11:16:15 -08002426 rlDest = dvmCompilerGetSrcWide(cUnit, mir, 0, 1);
2427 rlSrc1 = dvmCompilerGetSrc(cUnit, mir, 2);
2428 rlSrc2 = dvmCompilerGetSrc(cUnit, mir, 3);
Bill Buzbee1465db52009-09-23 17:17:35 -07002429 }
2430 } else {
2431 /* Two sources and 1 dest. Deduce the operand sizes */
2432 if (mir->ssaRep->numUses == 4) {
Bill Buzbeec6f10662010-02-09 11:16:15 -08002433 rlSrc1 = dvmCompilerGetSrcWide(cUnit, mir, 0, 1);
2434 rlSrc2 = dvmCompilerGetSrcWide(cUnit, mir, 2, 3);
Bill Buzbee1465db52009-09-23 17:17:35 -07002435 } else {
2436 assert(mir->ssaRep->numUses == 2);
Bill Buzbeec6f10662010-02-09 11:16:15 -08002437 rlSrc1 = dvmCompilerGetSrc(cUnit, mir, 0);
2438 rlSrc2 = dvmCompilerGetSrc(cUnit, mir, 1);
Bill Buzbee1465db52009-09-23 17:17:35 -07002439 }
2440 if (mir->ssaRep->numDefs == 2) {
Bill Buzbeec6f10662010-02-09 11:16:15 -08002441 rlDest = dvmCompilerGetDestWide(cUnit, mir, 0, 1);
Bill Buzbee1465db52009-09-23 17:17:35 -07002442 } else {
2443 assert(mir->ssaRep->numDefs == 1);
Bill Buzbeec6f10662010-02-09 11:16:15 -08002444 rlDest = dvmCompilerGetDest(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07002445 }
2446 }
2447
2448
Ben Chengba4fc8b2009-06-01 13:00:29 -07002449 switch (opCode) {
Bill Buzbeed45ba372009-06-15 17:00:57 -07002450 case OP_CMPL_FLOAT:
2451 case OP_CMPG_FLOAT:
2452 case OP_CMPL_DOUBLE:
2453 case OP_CMPG_DOUBLE:
Ben Cheng5d90c202009-11-22 23:31:11 -08002454 return genCmpFP(cUnit, mir, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002455 case OP_CMP_LONG:
Bill Buzbee1465db52009-09-23 17:17:35 -07002456 genCmpLong(cUnit, mir, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002457 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002458 case OP_AGET_WIDE:
Bill Buzbee1465db52009-09-23 17:17:35 -07002459 genArrayGet(cUnit, mir, kLong, rlSrc1, rlSrc2, rlDest, 3);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002460 break;
2461 case OP_AGET:
2462 case OP_AGET_OBJECT:
Bill Buzbee1465db52009-09-23 17:17:35 -07002463 genArrayGet(cUnit, mir, kWord, rlSrc1, rlSrc2, rlDest, 2);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002464 break;
2465 case OP_AGET_BOOLEAN:
Bill Buzbee1465db52009-09-23 17:17:35 -07002466 genArrayGet(cUnit, mir, kUnsignedByte, rlSrc1, rlSrc2, rlDest, 0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002467 break;
2468 case OP_AGET_BYTE:
Bill Buzbee1465db52009-09-23 17:17:35 -07002469 genArrayGet(cUnit, mir, kSignedByte, rlSrc1, rlSrc2, rlDest, 0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002470 break;
2471 case OP_AGET_CHAR:
Bill Buzbee1465db52009-09-23 17:17:35 -07002472 genArrayGet(cUnit, mir, kUnsignedHalf, rlSrc1, rlSrc2, rlDest, 1);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002473 break;
2474 case OP_AGET_SHORT:
Bill Buzbee1465db52009-09-23 17:17:35 -07002475 genArrayGet(cUnit, mir, kSignedHalf, rlSrc1, rlSrc2, rlDest, 1);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002476 break;
2477 case OP_APUT_WIDE:
Bill Buzbee1465db52009-09-23 17:17:35 -07002478 genArrayPut(cUnit, mir, kLong, rlSrc1, rlSrc2, rlDest, 3);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002479 break;
2480 case OP_APUT:
Bill Buzbee1465db52009-09-23 17:17:35 -07002481 genArrayPut(cUnit, mir, kWord, rlSrc1, rlSrc2, rlDest, 2);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002482 break;
Bill Buzbeebe6534f2010-03-12 16:01:35 -08002483 case OP_APUT_OBJECT:
2484 genArrayObjectPut(cUnit, mir, rlSrc1, rlSrc2, rlDest, 2);
2485 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002486 case OP_APUT_SHORT:
2487 case OP_APUT_CHAR:
Bill Buzbee1465db52009-09-23 17:17:35 -07002488 genArrayPut(cUnit, mir, kUnsignedHalf, rlSrc1, rlSrc2, rlDest, 1);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002489 break;
2490 case OP_APUT_BYTE:
2491 case OP_APUT_BOOLEAN:
Bill Buzbee1465db52009-09-23 17:17:35 -07002492 genArrayPut(cUnit, mir, kUnsignedByte, rlSrc1, rlSrc2, rlDest, 0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002493 break;
2494 default:
2495 return true;
2496 }
2497 return false;
2498}
2499
Ben Cheng6c10a972009-10-29 14:39:18 -07002500/*
2501 * Find the matching case.
2502 *
2503 * return values:
2504 * r0 (low 32-bit): pc of the chaining cell corresponding to the resolved case,
2505 * including default which is placed at MIN(size, MAX_CHAINED_SWITCH_CASES).
2506 * r1 (high 32-bit): the branch offset of the matching case (only for indexes
2507 * above MAX_CHAINED_SWITCH_CASES).
2508 *
2509 * Instructions around the call are:
2510 *
2511 * mov r2, pc
2512 * blx &findPackedSwitchIndex
2513 * mov pc, r0
2514 * .align4
Bill Buzbeebd047242010-05-13 13:02:53 -07002515 * chaining cell for case 0 [12 bytes]
2516 * chaining cell for case 1 [12 bytes]
Ben Cheng6c10a972009-10-29 14:39:18 -07002517 * :
Bill Buzbeebd047242010-05-13 13:02:53 -07002518 * chaining cell for case MIN(size, MAX_CHAINED_SWITCH_CASES)-1 [12 bytes]
Ben Cheng6c10a972009-10-29 14:39:18 -07002519 * chaining cell for case default [8 bytes]
2520 * noChain exit
2521 */
Ben Chengbd1326d2010-04-02 15:04:53 -07002522static s8 findPackedSwitchIndex(const u2* switchData, int testVal, int pc)
Ben Cheng6c10a972009-10-29 14:39:18 -07002523{
2524 int size;
2525 int firstKey;
2526 const int *entries;
2527 int index;
2528 int jumpIndex;
2529 int caseDPCOffset = 0;
2530 /* In Thumb mode pc is 4 ahead of the "mov r2, pc" instruction */
2531 int chainingPC = (pc + 4) & ~3;
2532
2533 /*
2534 * Packed switch data format:
2535 * ushort ident = 0x0100 magic value
2536 * ushort size number of entries in the table
2537 * int first_key first (and lowest) switch case value
2538 * int targets[size] branch targets, relative to switch opcode
2539 *
2540 * Total size is (4+size*2) 16-bit code units.
2541 */
2542 size = switchData[1];
2543 assert(size > 0);
2544
2545 firstKey = switchData[2];
2546 firstKey |= switchData[3] << 16;
2547
2548
2549 /* The entries are guaranteed to be aligned on a 32-bit boundary;
2550 * we can treat them as a native int array.
2551 */
2552 entries = (const int*) &switchData[4];
2553 assert(((u4)entries & 0x3) == 0);
2554
2555 index = testVal - firstKey;
2556
2557 /* Jump to the default cell */
2558 if (index < 0 || index >= size) {
2559 jumpIndex = MIN(size, MAX_CHAINED_SWITCH_CASES);
2560 /* Jump to the non-chaining exit point */
2561 } else if (index >= MAX_CHAINED_SWITCH_CASES) {
2562 jumpIndex = MAX_CHAINED_SWITCH_CASES + 1;
2563 caseDPCOffset = entries[index];
2564 /* Jump to the inline chaining cell */
2565 } else {
2566 jumpIndex = index;
2567 }
2568
Bill Buzbeebd047242010-05-13 13:02:53 -07002569 chainingPC += jumpIndex * CHAIN_CELL_NORMAL_SIZE;
Ben Cheng6c10a972009-10-29 14:39:18 -07002570 return (((s8) caseDPCOffset) << 32) | (u8) chainingPC;
2571}
2572
2573/* See comments for findPackedSwitchIndex */
Ben Chengbd1326d2010-04-02 15:04:53 -07002574static s8 findSparseSwitchIndex(const u2* switchData, int testVal, int pc)
Ben Cheng6c10a972009-10-29 14:39:18 -07002575{
2576 int size;
2577 const int *keys;
2578 const int *entries;
2579 int chainingPC = (pc + 4) & ~3;
2580 int i;
2581
2582 /*
2583 * Sparse switch data format:
2584 * ushort ident = 0x0200 magic value
2585 * ushort size number of entries in the table; > 0
2586 * int keys[size] keys, sorted low-to-high; 32-bit aligned
2587 * int targets[size] branch targets, relative to switch opcode
2588 *
2589 * Total size is (2+size*4) 16-bit code units.
2590 */
2591
2592 size = switchData[1];
2593 assert(size > 0);
2594
2595 /* The keys are guaranteed to be aligned on a 32-bit boundary;
2596 * we can treat them as a native int array.
2597 */
2598 keys = (const int*) &switchData[2];
2599 assert(((u4)keys & 0x3) == 0);
2600
2601 /* The entries are guaranteed to be aligned on a 32-bit boundary;
2602 * we can treat them as a native int array.
2603 */
2604 entries = keys + size;
2605 assert(((u4)entries & 0x3) == 0);
2606
2607 /*
2608 * Run through the list of keys, which are guaranteed to
2609 * be sorted low-to-high.
2610 *
2611 * Most tables have 3-4 entries. Few have more than 10. A binary
2612 * search here is probably not useful.
2613 */
2614 for (i = 0; i < size; i++) {
2615 int k = keys[i];
2616 if (k == testVal) {
2617 /* MAX_CHAINED_SWITCH_CASES + 1 is the start of the overflow case */
2618 int jumpIndex = (i < MAX_CHAINED_SWITCH_CASES) ?
2619 i : MAX_CHAINED_SWITCH_CASES + 1;
Bill Buzbeebd047242010-05-13 13:02:53 -07002620 chainingPC += jumpIndex * CHAIN_CELL_NORMAL_SIZE;
Ben Cheng6c10a972009-10-29 14:39:18 -07002621 return (((s8) entries[i]) << 32) | (u8) chainingPC;
2622 } else if (k > testVal) {
2623 break;
2624 }
2625 }
Bill Buzbeebd047242010-05-13 13:02:53 -07002626 return chainingPC + MIN(size, MAX_CHAINED_SWITCH_CASES) *
2627 CHAIN_CELL_NORMAL_SIZE;
Ben Cheng6c10a972009-10-29 14:39:18 -07002628}
2629
Ben Chengba4fc8b2009-06-01 13:00:29 -07002630static bool handleFmt31t(CompilationUnit *cUnit, MIR *mir)
2631{
2632 OpCode dalvikOpCode = mir->dalvikInsn.opCode;
2633 switch (dalvikOpCode) {
2634 case OP_FILL_ARRAY_DATA: {
Bill Buzbeec6f10662010-02-09 11:16:15 -08002635 RegLocation rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07002636 // Making a call - use explicit registers
Bill Buzbeec6f10662010-02-09 11:16:15 -08002637 dvmCompilerFlushAllRegs(cUnit); /* Everything to home location */
Bill Buzbee1465db52009-09-23 17:17:35 -07002638 genExportPC(cUnit, mir);
2639 loadValueDirectFixed(cUnit, rlSrc, r0);
Ben Chengbd1326d2010-04-02 15:04:53 -07002640 LOAD_FUNC_ADDR(cUnit, r2, (int)dvmInterpHandleFillArrayData);
Ben Cheng6c10a972009-10-29 14:39:18 -07002641 loadConstant(cUnit, r1,
2642 (int) (cUnit->method->insns + mir->offset + mir->dalvikInsn.vB));
Bill Buzbee1465db52009-09-23 17:17:35 -07002643 opReg(cUnit, kOpBlx, r2);
Elliott Hughes6a555132010-02-25 15:41:42 -08002644 dvmCompilerClobberCallRegs(cUnit);
Bill Buzbeefd023aa2009-11-02 09:23:49 -08002645 /* generate a branch over if successful */
2646 opRegImm(cUnit, kOpCmp, r0, 0); /* NULL? */
2647 ArmLIR *branchOver = opCondBranch(cUnit, kArmCondNe);
2648 loadConstant(cUnit, r0,
2649 (int) (cUnit->method->insns + mir->offset));
2650 genDispatchToHandler(cUnit, TEMPLATE_THROW_EXCEPTION_COMMON);
2651 ArmLIR *target = newLIR0(cUnit, kArmPseudoTargetLabel);
2652 target->defMask = ENCODE_ALL;
2653 branchOver->generic.target = (LIR *) target;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002654 break;
2655 }
2656 /*
Ben Cheng6c10a972009-10-29 14:39:18 -07002657 * Compute the goto target of up to
2658 * MIN(switchSize, MAX_CHAINED_SWITCH_CASES) + 1 chaining cells.
2659 * See the comment before findPackedSwitchIndex for the code layout.
Ben Chengba4fc8b2009-06-01 13:00:29 -07002660 */
2661 case OP_PACKED_SWITCH:
2662 case OP_SPARSE_SWITCH: {
Bill Buzbeec6f10662010-02-09 11:16:15 -08002663 RegLocation rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
2664 dvmCompilerFlushAllRegs(cUnit); /* Everything to home location */
Bill Buzbee1465db52009-09-23 17:17:35 -07002665 loadValueDirectFixed(cUnit, rlSrc, r1);
Bill Buzbeec6f10662010-02-09 11:16:15 -08002666 dvmCompilerLockAllTemps(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002667 if (dalvikOpCode == OP_PACKED_SWITCH) {
Ben Chengbd1326d2010-04-02 15:04:53 -07002668 LOAD_FUNC_ADDR(cUnit, r4PC, (int)findPackedSwitchIndex);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002669 } else {
Ben Chengbd1326d2010-04-02 15:04:53 -07002670 LOAD_FUNC_ADDR(cUnit, r4PC, (int)findSparseSwitchIndex);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002671 }
Ben Cheng6c10a972009-10-29 14:39:18 -07002672 /* r0 <- Addr of the switch data */
2673 loadConstant(cUnit, r0,
2674 (int) (cUnit->method->insns + mir->offset + mir->dalvikInsn.vB));
2675 /* r2 <- pc of the instruction following the blx */
2676 opRegReg(cUnit, kOpMov, r2, rpc);
Bill Buzbee1465db52009-09-23 17:17:35 -07002677 opReg(cUnit, kOpBlx, r4PC);
Elliott Hughes6a555132010-02-25 15:41:42 -08002678 dvmCompilerClobberCallRegs(cUnit);
Ben Cheng6c10a972009-10-29 14:39:18 -07002679 /* pc <- computed goto target */
2680 opRegReg(cUnit, kOpMov, rpc, r0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002681 break;
2682 }
2683 default:
2684 return true;
2685 }
2686 return false;
2687}
2688
2689static bool handleFmt35c_3rc(CompilationUnit *cUnit, MIR *mir, BasicBlock *bb,
Bill Buzbee89efc3d2009-07-28 11:22:22 -07002690 ArmLIR *labelList)
Ben Chengba4fc8b2009-06-01 13:00:29 -07002691{
Bill Buzbee9bc3df32009-07-30 10:52:29 -07002692 ArmLIR *retChainingCell = NULL;
Bill Buzbee89efc3d2009-07-28 11:22:22 -07002693 ArmLIR *pcrLabel = NULL;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002694
Bill Buzbeef4ce16f2009-07-28 13:28:25 -07002695 if (bb->fallThrough != NULL)
2696 retChainingCell = &labelList[bb->fallThrough->id];
2697
Ben Chengba4fc8b2009-06-01 13:00:29 -07002698 DecodedInstruction *dInsn = &mir->dalvikInsn;
2699 switch (mir->dalvikInsn.opCode) {
2700 /*
2701 * calleeMethod = this->clazz->vtable[
2702 * method->clazz->pDvmDex->pResMethods[BBBB]->methodIndex
2703 * ]
2704 */
2705 case OP_INVOKE_VIRTUAL:
2706 case OP_INVOKE_VIRTUAL_RANGE: {
Bill Buzbee89efc3d2009-07-28 11:22:22 -07002707 ArmLIR *predChainingCell = &labelList[bb->taken->id];
Ben Chengba4fc8b2009-06-01 13:00:29 -07002708 int methodIndex =
2709 cUnit->method->clazz->pDvmDex->pResMethods[dInsn->vB]->
2710 methodIndex;
2711
2712 if (mir->dalvikInsn.opCode == OP_INVOKE_VIRTUAL)
2713 genProcessArgsNoRange(cUnit, mir, dInsn, &pcrLabel);
2714 else
2715 genProcessArgsRange(cUnit, mir, dInsn, &pcrLabel);
2716
Ben Cheng38329f52009-07-07 14:19:20 -07002717 genInvokeVirtualCommon(cUnit, mir, methodIndex,
2718 retChainingCell,
2719 predChainingCell,
2720 pcrLabel);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002721 break;
2722 }
2723 /*
2724 * calleeMethod = method->clazz->super->vtable[method->clazz->pDvmDex
2725 * ->pResMethods[BBBB]->methodIndex]
2726 */
Ben Chengba4fc8b2009-06-01 13:00:29 -07002727 case OP_INVOKE_SUPER:
2728 case OP_INVOKE_SUPER_RANGE: {
2729 int mIndex = cUnit->method->clazz->pDvmDex->
2730 pResMethods[dInsn->vB]->methodIndex;
2731 const Method *calleeMethod =
2732 cUnit->method->clazz->super->vtable[mIndex];
2733
2734 if (mir->dalvikInsn.opCode == OP_INVOKE_SUPER)
2735 genProcessArgsNoRange(cUnit, mir, dInsn, &pcrLabel);
2736 else
2737 genProcessArgsRange(cUnit, mir, dInsn, &pcrLabel);
2738
2739 /* r0 = calleeMethod */
2740 loadConstant(cUnit, r0, (int) calleeMethod);
2741
Ben Cheng38329f52009-07-07 14:19:20 -07002742 genInvokeSingletonCommon(cUnit, mir, bb, labelList, pcrLabel,
2743 calleeMethod);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002744 break;
2745 }
2746 /* calleeMethod = method->clazz->pDvmDex->pResMethods[BBBB] */
2747 case OP_INVOKE_DIRECT:
2748 case OP_INVOKE_DIRECT_RANGE: {
2749 const Method *calleeMethod =
2750 cUnit->method->clazz->pDvmDex->pResMethods[dInsn->vB];
2751
2752 if (mir->dalvikInsn.opCode == OP_INVOKE_DIRECT)
2753 genProcessArgsNoRange(cUnit, mir, dInsn, &pcrLabel);
2754 else
2755 genProcessArgsRange(cUnit, mir, dInsn, &pcrLabel);
2756
2757 /* r0 = calleeMethod */
2758 loadConstant(cUnit, r0, (int) calleeMethod);
2759
Ben Cheng38329f52009-07-07 14:19:20 -07002760 genInvokeSingletonCommon(cUnit, mir, bb, labelList, pcrLabel,
2761 calleeMethod);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002762 break;
2763 }
2764 /* calleeMethod = method->clazz->pDvmDex->pResMethods[BBBB] */
2765 case OP_INVOKE_STATIC:
2766 case OP_INVOKE_STATIC_RANGE: {
2767 const Method *calleeMethod =
2768 cUnit->method->clazz->pDvmDex->pResMethods[dInsn->vB];
2769
2770 if (mir->dalvikInsn.opCode == OP_INVOKE_STATIC)
2771 genProcessArgsNoRange(cUnit, mir, dInsn,
2772 NULL /* no null check */);
2773 else
2774 genProcessArgsRange(cUnit, mir, dInsn,
2775 NULL /* no null check */);
2776
2777 /* r0 = calleeMethod */
2778 loadConstant(cUnit, r0, (int) calleeMethod);
2779
Ben Cheng38329f52009-07-07 14:19:20 -07002780 genInvokeSingletonCommon(cUnit, mir, bb, labelList, pcrLabel,
2781 calleeMethod);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002782 break;
2783 }
Ben Cheng09e50c92010-05-02 10:45:32 -07002784 /*
Ben Chengba4fc8b2009-06-01 13:00:29 -07002785 * calleeMethod = dvmFindInterfaceMethodInCache(this->clazz,
2786 * BBBB, method, method->clazz->pDvmDex)
Ben Cheng38329f52009-07-07 14:19:20 -07002787 *
Ben Cheng09e50c92010-05-02 10:45:32 -07002788 * The following is an example of generated code for
2789 * "invoke-interface v0"
Ben Cheng38329f52009-07-07 14:19:20 -07002790 *
Ben Cheng09e50c92010-05-02 10:45:32 -07002791 * -------- dalvik offset: 0x0008 @ invoke-interface v0
2792 * 0x47357e36 : ldr r0, [r5, #0] --+
2793 * 0x47357e38 : sub r7,r5,#24 |
2794 * 0x47357e3c : cmp r0, #0 | genProcessArgsNoRange
2795 * 0x47357e3e : beq 0x47357e82 |
2796 * 0x47357e40 : stmia r7, <r0> --+
2797 * 0x47357e42 : ldr r4, [pc, #120] --> r4 <- dalvikPC of this invoke
2798 * 0x47357e44 : add r1, pc, #64 --> r1 <- &retChainingCell
2799 * 0x47357e46 : add r2, pc, #72 --> r2 <- &predictedChainingCell
2800 * 0x47357e48 : blx_1 0x47348190 --+ TEMPLATE_INVOKE_METHOD_
2801 * 0x47357e4a : blx_2 see above --+ PREDICTED_CHAIN
2802 * 0x47357e4c : b 0x47357e90 --> off to the predicted chain
2803 * 0x47357e4e : b 0x47357e82 --> punt to the interpreter
2804 * 0x47357e50 : mov r8, r1 --+
2805 * 0x47357e52 : mov r9, r2 |
2806 * 0x47357e54 : ldr r2, [pc, #96] |
2807 * 0x47357e56 : mov r10, r3 |
2808 * 0x47357e58 : movs r0, r3 | dvmFindInterfaceMethodInCache
2809 * 0x47357e5a : ldr r3, [pc, #88] |
2810 * 0x47357e5c : ldr r7, [pc, #80] |
2811 * 0x47357e5e : mov r1, #1452 |
2812 * 0x47357e62 : blx r7 --+
2813 * 0x47357e64 : cmp r0, #0 --> calleeMethod == NULL?
2814 * 0x47357e66 : bne 0x47357e6e --> branch over the throw if !r0
2815 * 0x47357e68 : ldr r0, [pc, #80] --> load Dalvik PC of the invoke
2816 * 0x47357e6a : blx_1 0x47348494 --+ TEMPLATE_THROW_EXCEPTION_
2817 * 0x47357e6c : blx_2 see above --+ COMMON
2818 * 0x47357e6e : mov r1, r8 --> r1 <- &retChainingCell
2819 * 0x47357e70 : cmp r1, #0 --> compare against 0
2820 * 0x47357e72 : bgt 0x47357e7c --> >=0? don't rechain
2821 * 0x47357e74 : ldr r7, [r6, #108] --+
2822 * 0x47357e76 : mov r2, r9 | dvmJitToPatchPredictedChain
2823 * 0x47357e78 : mov r3, r10 |
2824 * 0x47357e7a : blx r7 --+
2825 * 0x47357e7c : add r1, pc, #8 --> r1 <- &retChainingCell
2826 * 0x47357e7e : blx_1 0x4734809c --+ TEMPLATE_INVOKE_METHOD_NO_OPT
2827 * 0x47357e80 : blx_2 see above --+
2828 * -------- reconstruct dalvik PC : 0x425719dc @ +0x0008
2829 * 0x47357e82 : ldr r0, [pc, #56]
Ben Cheng38329f52009-07-07 14:19:20 -07002830 * Exception_Handling:
Ben Cheng09e50c92010-05-02 10:45:32 -07002831 * 0x47357e84 : ldr r1, [r6, #92]
2832 * 0x47357e86 : blx r1
2833 * 0x47357e88 : .align4
2834 * -------- chaining cell (hot): 0x000b
2835 * 0x47357e88 : ldr r0, [r6, #104]
2836 * 0x47357e8a : blx r0
2837 * 0x47357e8c : data 0x19e2(6626)
2838 * 0x47357e8e : data 0x4257(16983)
2839 * 0x47357e90 : .align4
Ben Cheng38329f52009-07-07 14:19:20 -07002840 * -------- chaining cell (predicted)
Ben Cheng09e50c92010-05-02 10:45:32 -07002841 * 0x47357e90 : data 0xe7fe(59390) --> will be patched into bx
2842 * 0x47357e92 : data 0x0000(0)
2843 * 0x47357e94 : data 0x0000(0) --> class
2844 * 0x47357e96 : data 0x0000(0)
2845 * 0x47357e98 : data 0x0000(0) --> method
2846 * 0x47357e9a : data 0x0000(0)
2847 * 0x47357e9c : data 0x0000(0) --> rechain count
2848 * 0x47357e9e : data 0x0000(0)
2849 * -------- end of chaining cells (0x006c)
2850 * 0x47357eb0 : .word (0xad03e369)
2851 * 0x47357eb4 : .word (0x28a90)
2852 * 0x47357eb8 : .word (0x41a63394)
2853 * 0x47357ebc : .word (0x425719dc)
Ben Chengba4fc8b2009-06-01 13:00:29 -07002854 */
2855 case OP_INVOKE_INTERFACE:
2856 case OP_INVOKE_INTERFACE_RANGE: {
Bill Buzbee89efc3d2009-07-28 11:22:22 -07002857 ArmLIR *predChainingCell = &labelList[bb->taken->id];
Ben Chengba4fc8b2009-06-01 13:00:29 -07002858
Bill Buzbee1465db52009-09-23 17:17:35 -07002859 /* Ensure that nothing is both live and dirty */
Bill Buzbeec6f10662010-02-09 11:16:15 -08002860 dvmCompilerFlushAllRegs(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07002861
Ben Chengba4fc8b2009-06-01 13:00:29 -07002862 if (mir->dalvikInsn.opCode == OP_INVOKE_INTERFACE)
2863 genProcessArgsNoRange(cUnit, mir, dInsn, &pcrLabel);
2864 else
2865 genProcessArgsRange(cUnit, mir, dInsn, &pcrLabel);
2866
Ben Cheng38329f52009-07-07 14:19:20 -07002867 /* "this" is already left in r0 by genProcessArgs* */
2868
2869 /* r4PC = dalvikCallsite */
2870 loadConstant(cUnit, r4PC,
2871 (int) (cUnit->method->insns + mir->offset));
2872
2873 /* r1 = &retChainingCell */
Bill Buzbee270c1d62009-08-13 16:58:07 -07002874 ArmLIR *addrRetChain =
Bill Buzbee1465db52009-09-23 17:17:35 -07002875 opRegRegImm(cUnit, kOpAdd, r1, rpc, 0);
Ben Cheng38329f52009-07-07 14:19:20 -07002876 addrRetChain->generic.target = (LIR *) retChainingCell;
2877
2878 /* r2 = &predictedChainingCell */
Bill Buzbee89efc3d2009-07-28 11:22:22 -07002879 ArmLIR *predictedChainingCell =
Bill Buzbee1465db52009-09-23 17:17:35 -07002880 opRegRegImm(cUnit, kOpAdd, r2, rpc, 0);
Ben Cheng38329f52009-07-07 14:19:20 -07002881 predictedChainingCell->generic.target = (LIR *) predChainingCell;
2882
2883 genDispatchToHandler(cUnit, TEMPLATE_INVOKE_METHOD_PREDICTED_CHAIN);
2884
2885 /* return through lr - jump to the chaining cell */
2886 genUnconditionalBranch(cUnit, predChainingCell);
2887
2888 /*
2889 * null-check on "this" may have been eliminated, but we still need
2890 * a PC-reconstruction label for stack overflow bailout.
2891 */
2892 if (pcrLabel == NULL) {
2893 int dPC = (int) (cUnit->method->insns + mir->offset);
Bill Buzbee89efc3d2009-07-28 11:22:22 -07002894 pcrLabel = dvmCompilerNew(sizeof(ArmLIR), true);
Ben Chenga4973592010-03-31 11:59:18 -07002895 pcrLabel->opCode = kArmPseudoPCReconstructionCell;
Ben Cheng38329f52009-07-07 14:19:20 -07002896 pcrLabel->operands[0] = dPC;
2897 pcrLabel->operands[1] = mir->offset;
2898 /* Insert the place holder to the growable list */
2899 dvmInsertGrowableList(&cUnit->pcReconstructionList, pcrLabel);
2900 }
2901
2902 /* return through lr+2 - punt to the interpreter */
2903 genUnconditionalBranch(cUnit, pcrLabel);
2904
2905 /*
2906 * return through lr+4 - fully resolve the callee method.
2907 * r1 <- count
2908 * r2 <- &predictedChainCell
2909 * r3 <- this->class
2910 * r4 <- dPC
2911 * r7 <- this->class->vtable
2912 */
2913
2914 /* Save count, &predictedChainCell, and class to high regs first */
Bill Buzbee1465db52009-09-23 17:17:35 -07002915 genRegCopy(cUnit, r8, r1);
2916 genRegCopy(cUnit, r9, r2);
2917 genRegCopy(cUnit, r10, r3);
Ben Cheng38329f52009-07-07 14:19:20 -07002918
Ben Chengba4fc8b2009-06-01 13:00:29 -07002919 /* r0 now contains this->clazz */
Bill Buzbee1465db52009-09-23 17:17:35 -07002920 genRegCopy(cUnit, r0, r3);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002921
2922 /* r1 = BBBB */
2923 loadConstant(cUnit, r1, dInsn->vB);
2924
2925 /* r2 = method (caller) */
2926 loadConstant(cUnit, r2, (int) cUnit->method);
2927
2928 /* r3 = pDvmDex */
2929 loadConstant(cUnit, r3, (int) cUnit->method->clazz->pDvmDex);
2930
Ben Chengbd1326d2010-04-02 15:04:53 -07002931 LOAD_FUNC_ADDR(cUnit, r7,
2932 (intptr_t) dvmFindInterfaceMethodInCache);
Bill Buzbee1465db52009-09-23 17:17:35 -07002933 opReg(cUnit, kOpBlx, r7);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002934 /* r0 = calleeMethod (returned from dvmFindInterfaceMethodInCache */
2935
Ben Cheng09e50c92010-05-02 10:45:32 -07002936 dvmCompilerClobberCallRegs(cUnit);
2937 /* generate a branch over if the interface method is resolved */
2938 opRegImm(cUnit, kOpCmp, r0, 0); /* NULL? */
2939 ArmLIR *branchOver = opCondBranch(cUnit, kArmCondNe);
2940 /*
2941 * calleeMethod == NULL -> throw
2942 */
2943 loadConstant(cUnit, r0,
2944 (int) (cUnit->method->insns + mir->offset));
2945 genDispatchToHandler(cUnit, TEMPLATE_THROW_EXCEPTION_COMMON);
2946 /* noreturn */
2947
2948 ArmLIR *target = newLIR0(cUnit, kArmPseudoTargetLabel);
2949 target->defMask = ENCODE_ALL;
2950 branchOver->generic.target = (LIR *) target;
2951
Bill Buzbee1465db52009-09-23 17:17:35 -07002952 genRegCopy(cUnit, r1, r8);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002953
Ben Cheng38329f52009-07-07 14:19:20 -07002954 /* Check if rechain limit is reached */
Bill Buzbee1465db52009-09-23 17:17:35 -07002955 opRegImm(cUnit, kOpCmp, r1, 0);
Ben Cheng38329f52009-07-07 14:19:20 -07002956
Bill Buzbee1465db52009-09-23 17:17:35 -07002957 ArmLIR *bypassRechaining = opCondBranch(cUnit, kArmCondGt);
Ben Cheng38329f52009-07-07 14:19:20 -07002958
Bill Buzbee270c1d62009-08-13 16:58:07 -07002959 loadWordDisp(cUnit, rGLUE, offsetof(InterpState,
2960 jitToInterpEntries.dvmJitToPatchPredictedChain), r7);
Ben Cheng38329f52009-07-07 14:19:20 -07002961
Ben Chengb88ec3c2010-05-17 12:50:33 -07002962 genRegCopy(cUnit, r1, rGLUE);
Bill Buzbee1465db52009-09-23 17:17:35 -07002963 genRegCopy(cUnit, r2, r9);
2964 genRegCopy(cUnit, r3, r10);
Ben Cheng38329f52009-07-07 14:19:20 -07002965
2966 /*
2967 * r0 = calleeMethod
2968 * r2 = &predictedChainingCell
2969 * r3 = class
2970 *
2971 * &returnChainingCell has been loaded into r1 but is not needed
2972 * when patching the chaining cell and will be clobbered upon
2973 * returning so it will be reconstructed again.
2974 */
Bill Buzbee1465db52009-09-23 17:17:35 -07002975 opReg(cUnit, kOpBlx, r7);
Ben Cheng38329f52009-07-07 14:19:20 -07002976
2977 /* r1 = &retChainingCell */
Bill Buzbee1465db52009-09-23 17:17:35 -07002978 addrRetChain = opRegRegImm(cUnit, kOpAdd, r1, rpc, 0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002979 addrRetChain->generic.target = (LIR *) retChainingCell;
Ben Cheng38329f52009-07-07 14:19:20 -07002980
2981 bypassRechaining->generic.target = (LIR *) addrRetChain;
2982
Ben Chengba4fc8b2009-06-01 13:00:29 -07002983 /*
2984 * r0 = this, r1 = calleeMethod,
2985 * r1 = &ChainingCell,
2986 * r4PC = callsiteDPC,
2987 */
2988 genDispatchToHandler(cUnit, TEMPLATE_INVOKE_METHOD_NO_OPT);
Ben Cheng978738d2010-05-13 13:45:57 -07002989#if defined(WITH_JIT_TUNING)
Ben Cheng86717f72010-03-05 15:27:21 -08002990 gDvmJit.invokePolymorphic++;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002991#endif
2992 /* Handle exceptions using the interpreter */
2993 genTrap(cUnit, mir->offset, pcrLabel);
2994 break;
2995 }
2996 /* NOP */
2997 case OP_INVOKE_DIRECT_EMPTY: {
2998 return false;
2999 }
3000 case OP_FILLED_NEW_ARRAY:
3001 case OP_FILLED_NEW_ARRAY_RANGE: {
3002 /* Just let the interpreter deal with these */
3003 genInterpSingleStep(cUnit, mir);
3004 break;
3005 }
3006 default:
3007 return true;
3008 }
3009 return false;
3010}
3011
3012static bool handleFmt35ms_3rms(CompilationUnit *cUnit, MIR *mir,
Bill Buzbee89efc3d2009-07-28 11:22:22 -07003013 BasicBlock *bb, ArmLIR *labelList)
Ben Chengba4fc8b2009-06-01 13:00:29 -07003014{
Bill Buzbee89efc3d2009-07-28 11:22:22 -07003015 ArmLIR *retChainingCell = &labelList[bb->fallThrough->id];
3016 ArmLIR *predChainingCell = &labelList[bb->taken->id];
3017 ArmLIR *pcrLabel = NULL;
Ben Chengba4fc8b2009-06-01 13:00:29 -07003018
3019 DecodedInstruction *dInsn = &mir->dalvikInsn;
3020 switch (mir->dalvikInsn.opCode) {
3021 /* calleeMethod = this->clazz->vtable[BBBB] */
3022 case OP_INVOKE_VIRTUAL_QUICK_RANGE:
3023 case OP_INVOKE_VIRTUAL_QUICK: {
3024 int methodIndex = dInsn->vB;
3025 if (mir->dalvikInsn.opCode == OP_INVOKE_VIRTUAL_QUICK)
3026 genProcessArgsNoRange(cUnit, mir, dInsn, &pcrLabel);
3027 else
3028 genProcessArgsRange(cUnit, mir, dInsn, &pcrLabel);
3029
Ben Cheng38329f52009-07-07 14:19:20 -07003030 genInvokeVirtualCommon(cUnit, mir, methodIndex,
3031 retChainingCell,
3032 predChainingCell,
3033 pcrLabel);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003034 break;
3035 }
3036 /* calleeMethod = method->clazz->super->vtable[BBBB] */
3037 case OP_INVOKE_SUPER_QUICK:
3038 case OP_INVOKE_SUPER_QUICK_RANGE: {
3039 const Method *calleeMethod =
3040 cUnit->method->clazz->super->vtable[dInsn->vB];
3041
3042 if (mir->dalvikInsn.opCode == OP_INVOKE_SUPER_QUICK)
3043 genProcessArgsNoRange(cUnit, mir, dInsn, &pcrLabel);
3044 else
3045 genProcessArgsRange(cUnit, mir, dInsn, &pcrLabel);
3046
3047 /* r0 = calleeMethod */
3048 loadConstant(cUnit, r0, (int) calleeMethod);
3049
Ben Cheng38329f52009-07-07 14:19:20 -07003050 genInvokeSingletonCommon(cUnit, mir, bb, labelList, pcrLabel,
3051 calleeMethod);
3052 /* Handle exceptions using the interpreter */
3053 genTrap(cUnit, mir->offset, pcrLabel);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003054 break;
3055 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07003056 default:
3057 return true;
3058 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07003059 return false;
3060}
3061
3062/*
Bill Buzbeefd023aa2009-11-02 09:23:49 -08003063 * This operation is complex enough that we'll do it partly inline
3064 * and partly with a handler. NOTE: the handler uses hardcoded
3065 * values for string object offsets and must be revisitied if the
3066 * layout changes.
3067 */
3068static bool genInlinedCompareTo(CompilationUnit *cUnit, MIR *mir)
3069{
3070#if defined(USE_GLOBAL_STRING_DEFS)
3071 return false;
3072#else
3073 ArmLIR *rollback;
Bill Buzbeec6f10662010-02-09 11:16:15 -08003074 RegLocation rlThis = dvmCompilerGetSrc(cUnit, mir, 0);
3075 RegLocation rlComp = dvmCompilerGetSrc(cUnit, mir, 1);
Bill Buzbeefd023aa2009-11-02 09:23:49 -08003076
3077 loadValueDirectFixed(cUnit, rlThis, r0);
3078 loadValueDirectFixed(cUnit, rlComp, r1);
3079 /* Test objects for NULL */
3080 rollback = genNullCheck(cUnit, rlThis.sRegLow, r0, mir->offset, NULL);
3081 genNullCheck(cUnit, rlComp.sRegLow, r1, mir->offset, rollback);
3082 /*
3083 * TUNING: we could check for object pointer equality before invoking
3084 * handler. Unclear whether the gain would be worth the added code size
3085 * expansion.
3086 */
3087 genDispatchToHandler(cUnit, TEMPLATE_STRING_COMPARETO);
Bill Buzbeec6f10662010-02-09 11:16:15 -08003088 storeValue(cUnit, inlinedTarget(cUnit, mir, false),
3089 dvmCompilerGetReturn(cUnit));
Bill Buzbeefd023aa2009-11-02 09:23:49 -08003090 return true;
3091#endif
3092}
3093
Elliott Hughes2bdbcb62010-04-12 14:29:37 -07003094static bool genInlinedFastIndexOf(CompilationUnit *cUnit, MIR *mir)
Bill Buzbeefd023aa2009-11-02 09:23:49 -08003095{
3096#if defined(USE_GLOBAL_STRING_DEFS)
3097 return false;
3098#else
Bill Buzbeec6f10662010-02-09 11:16:15 -08003099 RegLocation rlThis = dvmCompilerGetSrc(cUnit, mir, 0);
3100 RegLocation rlChar = dvmCompilerGetSrc(cUnit, mir, 1);
Bill Buzbeefd023aa2009-11-02 09:23:49 -08003101
3102 loadValueDirectFixed(cUnit, rlThis, r0);
3103 loadValueDirectFixed(cUnit, rlChar, r1);
Elliott Hughes2bdbcb62010-04-12 14:29:37 -07003104 RegLocation rlStart = dvmCompilerGetSrc(cUnit, mir, 2);
3105 loadValueDirectFixed(cUnit, rlStart, r2);
Bill Buzbeefd023aa2009-11-02 09:23:49 -08003106 /* Test objects for NULL */
3107 genNullCheck(cUnit, rlThis.sRegLow, r0, mir->offset, NULL);
3108 genDispatchToHandler(cUnit, TEMPLATE_STRING_INDEXOF);
Bill Buzbeec6f10662010-02-09 11:16:15 -08003109 storeValue(cUnit, inlinedTarget(cUnit, mir, false),
3110 dvmCompilerGetReturn(cUnit));
Bill Buzbeefd023aa2009-11-02 09:23:49 -08003111 return true;
3112#endif
3113}
3114
Elliott Hughesee34f592010-04-05 18:13:52 -07003115// Generates an inlined String.isEmpty or String.length.
3116static bool genInlinedStringIsEmptyOrLength(CompilationUnit *cUnit, MIR *mir,
3117 bool isEmpty)
Bill Buzbee1f748632010-03-02 16:14:41 -08003118{
Elliott Hughesee34f592010-04-05 18:13:52 -07003119 // dst = src.length();
Bill Buzbee1f748632010-03-02 16:14:41 -08003120 RegLocation rlObj = dvmCompilerGetSrc(cUnit, mir, 0);
3121 RegLocation rlDest = inlinedTarget(cUnit, mir, false);
3122 rlObj = loadValue(cUnit, rlObj, kCoreReg);
3123 RegLocation rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
3124 genNullCheck(cUnit, rlObj.sRegLow, rlObj.lowReg, mir->offset, NULL);
3125 loadWordDisp(cUnit, rlObj.lowReg, gDvm.offJavaLangString_count,
3126 rlResult.lowReg);
Elliott Hughesee34f592010-04-05 18:13:52 -07003127 if (isEmpty) {
3128 // dst = (dst == 0);
3129 int tReg = dvmCompilerAllocTemp(cUnit);
3130 opRegReg(cUnit, kOpNeg, tReg, rlResult.lowReg);
3131 opRegRegReg(cUnit, kOpAdc, rlResult.lowReg, rlResult.lowReg, tReg);
3132 }
Bill Buzbee1f748632010-03-02 16:14:41 -08003133 storeValue(cUnit, rlDest, rlResult);
3134 return false;
3135}
3136
Elliott Hughesee34f592010-04-05 18:13:52 -07003137static bool genInlinedStringLength(CompilationUnit *cUnit, MIR *mir)
3138{
3139 return genInlinedStringIsEmptyOrLength(cUnit, mir, false);
3140}
3141
3142static bool genInlinedStringIsEmpty(CompilationUnit *cUnit, MIR *mir)
3143{
3144 return genInlinedStringIsEmptyOrLength(cUnit, mir, true);
3145}
3146
Bill Buzbee1f748632010-03-02 16:14:41 -08003147static bool genInlinedStringCharAt(CompilationUnit *cUnit, MIR *mir)
3148{
3149 int contents = offsetof(ArrayObject, contents);
3150 RegLocation rlObj = dvmCompilerGetSrc(cUnit, mir, 0);
3151 RegLocation rlIdx = dvmCompilerGetSrc(cUnit, mir, 1);
3152 RegLocation rlDest = inlinedTarget(cUnit, mir, false);
3153 RegLocation rlResult;
3154 rlObj = loadValue(cUnit, rlObj, kCoreReg);
3155 rlIdx = loadValue(cUnit, rlIdx, kCoreReg);
3156 int regMax = dvmCompilerAllocTemp(cUnit);
3157 int regOff = dvmCompilerAllocTemp(cUnit);
3158 int regPtr = dvmCompilerAllocTemp(cUnit);
3159 ArmLIR *pcrLabel = genNullCheck(cUnit, rlObj.sRegLow, rlObj.lowReg,
3160 mir->offset, NULL);
3161 loadWordDisp(cUnit, rlObj.lowReg, gDvm.offJavaLangString_count, regMax);
3162 loadWordDisp(cUnit, rlObj.lowReg, gDvm.offJavaLangString_offset, regOff);
3163 loadWordDisp(cUnit, rlObj.lowReg, gDvm.offJavaLangString_value, regPtr);
3164 genBoundsCheck(cUnit, rlIdx.lowReg, regMax, mir->offset, pcrLabel);
3165 dvmCompilerFreeTemp(cUnit, regMax);
3166 opRegImm(cUnit, kOpAdd, regPtr, contents);
3167 opRegReg(cUnit, kOpAdd, regOff, rlIdx.lowReg);
3168 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
3169 loadBaseIndexed(cUnit, regPtr, regOff, rlResult.lowReg, 1, kUnsignedHalf);
3170 storeValue(cUnit, rlDest, rlResult);
3171 return false;
3172}
3173
3174static bool genInlinedAbsInt(CompilationUnit *cUnit, MIR *mir)
3175{
3176 RegLocation rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
3177 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
3178 RegLocation rlDest = inlinedTarget(cUnit, mir, false);;
3179 RegLocation rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
3180 int signReg = dvmCompilerAllocTemp(cUnit);
3181 /*
3182 * abs(x) = y<=x>>31, (x+y)^y.
3183 * Thumb2's IT block also yields 3 instructions, but imposes
3184 * scheduling constraints.
3185 */
3186 opRegRegImm(cUnit, kOpAsr, signReg, rlSrc.lowReg, 31);
3187 opRegRegReg(cUnit, kOpAdd, rlResult.lowReg, rlSrc.lowReg, signReg);
3188 opRegReg(cUnit, kOpXor, rlResult.lowReg, signReg);
3189 storeValue(cUnit, rlDest, rlResult);
3190 return false;
3191}
3192
3193static bool genInlinedAbsLong(CompilationUnit *cUnit, MIR *mir)
3194{
3195 RegLocation rlSrc = dvmCompilerGetSrcWide(cUnit, mir, 0, 1);
3196 RegLocation rlDest = inlinedTargetWide(cUnit, mir, false);
3197 rlSrc = loadValueWide(cUnit, rlSrc, kCoreReg);
3198 RegLocation rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
3199 int signReg = dvmCompilerAllocTemp(cUnit);
3200 /*
3201 * abs(x) = y<=x>>31, (x+y)^y.
3202 * Thumb2 IT block allows slightly shorter sequence,
3203 * but introduces a scheduling barrier. Stick with this
3204 * mechanism for now.
3205 */
3206 opRegRegImm(cUnit, kOpAsr, signReg, rlSrc.highReg, 31);
3207 opRegRegReg(cUnit, kOpAdd, rlResult.lowReg, rlSrc.lowReg, signReg);
3208 opRegRegReg(cUnit, kOpAdc, rlResult.highReg, rlSrc.highReg, signReg);
3209 opRegReg(cUnit, kOpXor, rlResult.lowReg, signReg);
3210 opRegReg(cUnit, kOpXor, rlResult.highReg, signReg);
3211 storeValueWide(cUnit, rlDest, rlResult);
3212 return false;
3213}
Bill Buzbeefd023aa2009-11-02 09:23:49 -08003214
3215/*
Bill Buzbeece46c942009-11-20 15:41:34 -08003216 * NOTE: Handles both range and non-range versions (arguments
3217 * have already been normalized by this point).
Ben Chengba4fc8b2009-06-01 13:00:29 -07003218 */
Bill Buzbeece46c942009-11-20 15:41:34 -08003219static bool handleExecuteInline(CompilationUnit *cUnit, MIR *mir)
Ben Chengba4fc8b2009-06-01 13:00:29 -07003220{
3221 DecodedInstruction *dInsn = &mir->dalvikInsn;
3222 switch( mir->dalvikInsn.opCode) {
Bill Buzbeece46c942009-11-20 15:41:34 -08003223 case OP_EXECUTE_INLINE_RANGE:
Ben Chengba4fc8b2009-06-01 13:00:29 -07003224 case OP_EXECUTE_INLINE: {
3225 unsigned int i;
3226 const InlineOperation* inLineTable = dvmGetInlineOpsTable();
Bill Buzbee50a6bf22009-07-08 13:08:04 -07003227 int offset = offsetof(InterpState, retval);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003228 int operation = dInsn->vB;
Bill Buzbee50a6bf22009-07-08 13:08:04 -07003229 switch (operation) {
3230 case INLINE_EMPTYINLINEMETHOD:
3231 return false; /* Nop */
3232 case INLINE_STRING_LENGTH:
3233 return genInlinedStringLength(cUnit, mir);
Elliott Hughesee34f592010-04-05 18:13:52 -07003234 case INLINE_STRING_IS_EMPTY:
3235 return genInlinedStringIsEmpty(cUnit, mir);
Bill Buzbee50a6bf22009-07-08 13:08:04 -07003236 case INLINE_MATH_ABS_INT:
3237 return genInlinedAbsInt(cUnit, mir);
3238 case INLINE_MATH_ABS_LONG:
3239 return genInlinedAbsLong(cUnit, mir);
3240 case INLINE_MATH_MIN_INT:
3241 return genInlinedMinMaxInt(cUnit, mir, true);
3242 case INLINE_MATH_MAX_INT:
3243 return genInlinedMinMaxInt(cUnit, mir, false);
3244 case INLINE_STRING_CHARAT:
3245 return genInlinedStringCharAt(cUnit, mir);
3246 case INLINE_MATH_SQRT:
3247 if (genInlineSqrt(cUnit, mir))
Bill Buzbee9727c3d2009-08-01 11:32:36 -07003248 return false;
Bill Buzbee50a6bf22009-07-08 13:08:04 -07003249 else
3250 break; /* Handle with C routine */
Bill Buzbee50a6bf22009-07-08 13:08:04 -07003251 case INLINE_MATH_ABS_FLOAT:
Bill Buzbee1465db52009-09-23 17:17:35 -07003252 if (genInlinedAbsFloat(cUnit, mir))
3253 return false;
3254 else
3255 break;
Bill Buzbee50a6bf22009-07-08 13:08:04 -07003256 case INLINE_MATH_ABS_DOUBLE:
Bill Buzbee1465db52009-09-23 17:17:35 -07003257 if (genInlinedAbsDouble(cUnit, mir))
3258 return false;
3259 else
3260 break;
Bill Buzbee50a6bf22009-07-08 13:08:04 -07003261 case INLINE_STRING_COMPARETO:
Bill Buzbeefd023aa2009-11-02 09:23:49 -08003262 if (genInlinedCompareTo(cUnit, mir))
3263 return false;
3264 else
3265 break;
Elliott Hughes2bdbcb62010-04-12 14:29:37 -07003266 case INLINE_STRING_FASTINDEXOF_II:
3267 if (genInlinedFastIndexOf(cUnit, mir))
Bill Buzbeefd023aa2009-11-02 09:23:49 -08003268 return false;
3269 else
3270 break;
3271 case INLINE_STRING_EQUALS:
3272 case INLINE_MATH_COS:
3273 case INLINE_MATH_SIN:
3274 break; /* Handle with C routine */
Bill Buzbee50a6bf22009-07-08 13:08:04 -07003275 default:
Bill Buzbeefc519dc2010-03-06 23:30:57 -08003276 dvmCompilerAbort(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003277 }
Bill Buzbeec6f10662010-02-09 11:16:15 -08003278 dvmCompilerFlushAllRegs(cUnit); /* Everything to home location */
Elliott Hughes6a555132010-02-25 15:41:42 -08003279 dvmCompilerClobberCallRegs(cUnit);
Bill Buzbeec6f10662010-02-09 11:16:15 -08003280 dvmCompilerClobber(cUnit, r4PC);
3281 dvmCompilerClobber(cUnit, r7);
Bill Buzbee1465db52009-09-23 17:17:35 -07003282 opRegRegImm(cUnit, kOpAdd, r4PC, rGLUE, offset);
3283 opImm(cUnit, kOpPush, (1<<r4PC) | (1<<r7));
Ben Chengbd1326d2010-04-02 15:04:53 -07003284 LOAD_FUNC_ADDR(cUnit, r4PC, (int)inLineTable[operation].func);
Bill Buzbee1465db52009-09-23 17:17:35 -07003285 genExportPC(cUnit, mir);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003286 for (i=0; i < dInsn->vA; i++) {
Bill Buzbeec6f10662010-02-09 11:16:15 -08003287 loadValueDirect(cUnit, dvmCompilerGetSrc(cUnit, mir, i), i);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003288 }
Bill Buzbee1465db52009-09-23 17:17:35 -07003289 opReg(cUnit, kOpBlx, r4PC);
3290 opRegImm(cUnit, kOpAdd, r13, 8);
Bill Buzbeece46c942009-11-20 15:41:34 -08003291 opRegImm(cUnit, kOpCmp, r0, 0); /* NULL? */
3292 ArmLIR *branchOver = opCondBranch(cUnit, kArmCondNe);
3293 loadConstant(cUnit, r0,
3294 (int) (cUnit->method->insns + mir->offset));
3295 genDispatchToHandler(cUnit, TEMPLATE_THROW_EXCEPTION_COMMON);
3296 ArmLIR *target = newLIR0(cUnit, kArmPseudoTargetLabel);
3297 target->defMask = ENCODE_ALL;
3298 branchOver->generic.target = (LIR *) target;
Ben Chengba4fc8b2009-06-01 13:00:29 -07003299 break;
3300 }
3301 default:
3302 return true;
3303 }
3304 return false;
3305}
3306
3307static bool handleFmt51l(CompilationUnit *cUnit, MIR *mir)
3308{
Bill Buzbee1465db52009-09-23 17:17:35 -07003309 //TUNING: We're using core regs here - not optimal when target is a double
Bill Buzbeec6f10662010-02-09 11:16:15 -08003310 RegLocation rlDest = dvmCompilerGetDestWide(cUnit, mir, 0, 1);
3311 RegLocation rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Ben Chengbd1326d2010-04-02 15:04:53 -07003312 loadConstantNoClobber(cUnit, rlResult.lowReg,
3313 mir->dalvikInsn.vB_wide & 0xFFFFFFFFUL);
3314 loadConstantNoClobber(cUnit, rlResult.highReg,
3315 (mir->dalvikInsn.vB_wide>>32) & 0xFFFFFFFFUL);
Bill Buzbee1465db52009-09-23 17:17:35 -07003316 storeValueWide(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003317 return false;
3318}
3319
Ben Chengba4fc8b2009-06-01 13:00:29 -07003320/*
3321 * The following are special processing routines that handle transfer of
3322 * controls between compiled code and the interpreter. Certain VM states like
3323 * Dalvik PC and special-purpose registers are reconstructed here.
3324 */
3325
Bill Buzbeebd047242010-05-13 13:02:53 -07003326/*
3327 * Insert a
3328 * b .+4
3329 * nop
3330 * pair at the beginning of a chaining cell. This serves as the
3331 * switch branch that selects between reverting to the interpreter or
3332 * not. Once the cell is chained to a translation, the cell will
3333 * contain a 32-bit branch. Subsequent chain/unchain operations will
3334 * then only alter that first 16-bits - the "b .+4" for unchaining,
3335 * and the restoration of the first half of the 32-bit branch for
3336 * rechaining.
3337 */
3338static void insertChainingSwitch(CompilationUnit *cUnit)
3339{
3340 ArmLIR *branch = newLIR0(cUnit, kThumbBUncond);
3341 newLIR2(cUnit, kThumbOrr, r0, r0);
3342 ArmLIR *target = newLIR0(cUnit, kArmPseudoTargetLabel);
3343 target->defMask = ENCODE_ALL;
3344 branch->generic.target = (LIR *) target;
3345}
3346
Ben Cheng1efc9c52009-06-08 18:25:27 -07003347/* Chaining cell for code that may need warmup. */
3348static void handleNormalChainingCell(CompilationUnit *cUnit,
3349 unsigned int offset)
Ben Chengba4fc8b2009-06-01 13:00:29 -07003350{
Ben Cheng11d8f142010-03-24 15:24:19 -07003351 /*
3352 * Use raw instruction constructors to guarantee that the generated
3353 * instructions fit the predefined cell size.
3354 */
Bill Buzbeebd047242010-05-13 13:02:53 -07003355 insertChainingSwitch(cUnit);
Ben Cheng11d8f142010-03-24 15:24:19 -07003356 newLIR3(cUnit, kThumbLdrRRI5, r0, rGLUE,
3357 offsetof(InterpState,
3358 jitToInterpEntries.dvmJitToInterpNormal) >> 2);
3359 newLIR1(cUnit, kThumbBlxR, r0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003360 addWordData(cUnit, (int) (cUnit->method->insns + offset), true);
3361}
3362
3363/*
Ben Cheng1efc9c52009-06-08 18:25:27 -07003364 * Chaining cell for instructions that immediately following already translated
3365 * code.
Ben Chengba4fc8b2009-06-01 13:00:29 -07003366 */
Ben Cheng1efc9c52009-06-08 18:25:27 -07003367static void handleHotChainingCell(CompilationUnit *cUnit,
3368 unsigned int offset)
Ben Chengba4fc8b2009-06-01 13:00:29 -07003369{
Ben Cheng11d8f142010-03-24 15:24:19 -07003370 /*
3371 * Use raw instruction constructors to guarantee that the generated
3372 * instructions fit the predefined cell size.
3373 */
Bill Buzbeebd047242010-05-13 13:02:53 -07003374 insertChainingSwitch(cUnit);
Ben Cheng11d8f142010-03-24 15:24:19 -07003375 newLIR3(cUnit, kThumbLdrRRI5, r0, rGLUE,
3376 offsetof(InterpState,
3377 jitToInterpEntries.dvmJitToInterpTraceSelect) >> 2);
3378 newLIR1(cUnit, kThumbBlxR, r0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003379 addWordData(cUnit, (int) (cUnit->method->insns + offset), true);
3380}
3381
Bill Buzbee9c4b7c82009-09-10 10:10:38 -07003382#if defined(WITH_SELF_VERIFICATION) || defined(WITH_JIT_TUNING)
Jeff Hao97319a82009-08-12 16:57:15 -07003383/* Chaining cell for branches that branch back into the same basic block */
3384static void handleBackwardBranchChainingCell(CompilationUnit *cUnit,
3385 unsigned int offset)
3386{
Ben Cheng11d8f142010-03-24 15:24:19 -07003387 /*
3388 * Use raw instruction constructors to guarantee that the generated
3389 * instructions fit the predefined cell size.
3390 */
Bill Buzbeebd047242010-05-13 13:02:53 -07003391 insertChainingSwitch(cUnit);
Bill Buzbee9c4b7c82009-09-10 10:10:38 -07003392#if defined(WITH_SELF_VERIFICATION)
Bill Buzbee1465db52009-09-23 17:17:35 -07003393 newLIR3(cUnit, kThumbLdrRRI5, r0, rGLUE,
Ben Cheng40094c12010-02-24 20:58:44 -08003394 offsetof(InterpState,
3395 jitToInterpEntries.dvmJitToInterpBackwardBranch) >> 2);
Bill Buzbee9c4b7c82009-09-10 10:10:38 -07003396#else
Bill Buzbee1465db52009-09-23 17:17:35 -07003397 newLIR3(cUnit, kThumbLdrRRI5, r0, rGLUE,
Bill Buzbee9c4b7c82009-09-10 10:10:38 -07003398 offsetof(InterpState, jitToInterpEntries.dvmJitToInterpNormal) >> 2);
3399#endif
Bill Buzbee1465db52009-09-23 17:17:35 -07003400 newLIR1(cUnit, kThumbBlxR, r0);
Jeff Hao97319a82009-08-12 16:57:15 -07003401 addWordData(cUnit, (int) (cUnit->method->insns + offset), true);
3402}
3403
3404#endif
Ben Chengba4fc8b2009-06-01 13:00:29 -07003405/* Chaining cell for monomorphic method invocations. */
Ben Cheng38329f52009-07-07 14:19:20 -07003406static void handleInvokeSingletonChainingCell(CompilationUnit *cUnit,
3407 const Method *callee)
Ben Chengba4fc8b2009-06-01 13:00:29 -07003408{
Ben Cheng11d8f142010-03-24 15:24:19 -07003409 /*
3410 * Use raw instruction constructors to guarantee that the generated
3411 * instructions fit the predefined cell size.
3412 */
Bill Buzbeebd047242010-05-13 13:02:53 -07003413 insertChainingSwitch(cUnit);
Ben Cheng11d8f142010-03-24 15:24:19 -07003414 newLIR3(cUnit, kThumbLdrRRI5, r0, rGLUE,
3415 offsetof(InterpState,
3416 jitToInterpEntries.dvmJitToInterpTraceSelect) >> 2);
3417 newLIR1(cUnit, kThumbBlxR, r0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003418 addWordData(cUnit, (int) (callee->insns), true);
3419}
3420
Ben Cheng38329f52009-07-07 14:19:20 -07003421/* Chaining cell for monomorphic method invocations. */
3422static void handleInvokePredictedChainingCell(CompilationUnit *cUnit)
3423{
3424
3425 /* Should not be executed in the initial state */
3426 addWordData(cUnit, PREDICTED_CHAIN_BX_PAIR_INIT, true);
3427 /* To be filled: class */
3428 addWordData(cUnit, PREDICTED_CHAIN_CLAZZ_INIT, true);
3429 /* To be filled: method */
3430 addWordData(cUnit, PREDICTED_CHAIN_METHOD_INIT, true);
3431 /*
3432 * Rechain count. The initial value of 0 here will trigger chaining upon
3433 * the first invocation of this callsite.
3434 */
3435 addWordData(cUnit, PREDICTED_CHAIN_COUNTER_INIT, true);
3436}
3437
Ben Chengba4fc8b2009-06-01 13:00:29 -07003438/* Load the Dalvik PC into r0 and jump to the specified target */
3439static void handlePCReconstruction(CompilationUnit *cUnit,
Bill Buzbee89efc3d2009-07-28 11:22:22 -07003440 ArmLIR *targetLabel)
Ben Chengba4fc8b2009-06-01 13:00:29 -07003441{
Bill Buzbee89efc3d2009-07-28 11:22:22 -07003442 ArmLIR **pcrLabel =
3443 (ArmLIR **) cUnit->pcReconstructionList.elemList;
Ben Chengba4fc8b2009-06-01 13:00:29 -07003444 int numElems = cUnit->pcReconstructionList.numUsed;
3445 int i;
3446 for (i = 0; i < numElems; i++) {
3447 dvmCompilerAppendLIR(cUnit, (LIR *) pcrLabel[i]);
3448 /* r0 = dalvik PC */
3449 loadConstant(cUnit, r0, pcrLabel[i]->operands[0]);
3450 genUnconditionalBranch(cUnit, targetLabel);
3451 }
3452}
3453
Bill Buzbee1465db52009-09-23 17:17:35 -07003454static char *extendedMIROpNames[kMirOpLast - kMirOpFirst] = {
3455 "kMirOpPhi",
3456 "kMirOpNullNRangeUpCheck",
3457 "kMirOpNullNRangeDownCheck",
3458 "kMirOpLowerBound",
3459 "kMirOpPunt",
Ben Cheng4238ec22009-08-24 16:32:22 -07003460};
3461
3462/*
3463 * vA = arrayReg;
3464 * vB = idxReg;
3465 * vC = endConditionReg;
3466 * arg[0] = maxC
3467 * arg[1] = minC
3468 * arg[2] = loopBranchConditionCode
3469 */
3470static void genHoistedChecksForCountUpLoop(CompilationUnit *cUnit, MIR *mir)
3471{
Bill Buzbee1465db52009-09-23 17:17:35 -07003472 /*
3473 * NOTE: these synthesized blocks don't have ssa names assigned
3474 * for Dalvik registers. However, because they dominate the following
3475 * blocks we can simply use the Dalvik name w/ subscript 0 as the
3476 * ssa name.
3477 */
Ben Cheng4238ec22009-08-24 16:32:22 -07003478 DecodedInstruction *dInsn = &mir->dalvikInsn;
3479 const int lenOffset = offsetof(ArrayObject, length);
Ben Cheng4238ec22009-08-24 16:32:22 -07003480 const int maxC = dInsn->arg[0];
Bill Buzbee1465db52009-09-23 17:17:35 -07003481 int regLength;
3482 RegLocation rlArray = cUnit->regLocation[mir->dalvikInsn.vA];
3483 RegLocation rlIdxEnd = cUnit->regLocation[mir->dalvikInsn.vC];
Ben Cheng4238ec22009-08-24 16:32:22 -07003484
3485 /* regArray <- arrayRef */
Bill Buzbee1465db52009-09-23 17:17:35 -07003486 rlArray = loadValue(cUnit, rlArray, kCoreReg);
3487 rlIdxEnd = loadValue(cUnit, rlIdxEnd, kCoreReg);
3488 genRegImmCheck(cUnit, kArmCondEq, rlArray.lowReg, 0, 0,
Ben Cheng4238ec22009-08-24 16:32:22 -07003489 (ArmLIR *) cUnit->loopAnalysis->branchToPCR);
3490
3491 /* regLength <- len(arrayRef) */
Bill Buzbeec6f10662010-02-09 11:16:15 -08003492 regLength = dvmCompilerAllocTemp(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07003493 loadWordDisp(cUnit, rlArray.lowReg, lenOffset, regLength);
Ben Cheng4238ec22009-08-24 16:32:22 -07003494
3495 int delta = maxC;
3496 /*
3497 * If the loop end condition is ">=" instead of ">", then the largest value
3498 * of the index is "endCondition - 1".
3499 */
3500 if (dInsn->arg[2] == OP_IF_GE) {
3501 delta--;
3502 }
3503
3504 if (delta) {
Bill Buzbeec6f10662010-02-09 11:16:15 -08003505 int tReg = dvmCompilerAllocTemp(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07003506 opRegRegImm(cUnit, kOpAdd, tReg, rlIdxEnd.lowReg, delta);
3507 rlIdxEnd.lowReg = tReg;
Bill Buzbeec6f10662010-02-09 11:16:15 -08003508 dvmCompilerFreeTemp(cUnit, tReg);
Ben Cheng4238ec22009-08-24 16:32:22 -07003509 }
3510 /* Punt if "regIdxEnd < len(Array)" is false */
Bill Buzbee1465db52009-09-23 17:17:35 -07003511 genRegRegCheck(cUnit, kArmCondGe, rlIdxEnd.lowReg, regLength, 0,
Ben Cheng0fd31e42009-09-03 14:40:16 -07003512 (ArmLIR *) cUnit->loopAnalysis->branchToPCR);
Ben Cheng4238ec22009-08-24 16:32:22 -07003513}
3514
3515/*
3516 * vA = arrayReg;
3517 * vB = idxReg;
3518 * vC = endConditionReg;
3519 * arg[0] = maxC
3520 * arg[1] = minC
3521 * arg[2] = loopBranchConditionCode
3522 */
3523static void genHoistedChecksForCountDownLoop(CompilationUnit *cUnit, MIR *mir)
3524{
3525 DecodedInstruction *dInsn = &mir->dalvikInsn;
3526 const int lenOffset = offsetof(ArrayObject, length);
Bill Buzbeec6f10662010-02-09 11:16:15 -08003527 const int regLength = dvmCompilerAllocTemp(cUnit);
Ben Cheng4238ec22009-08-24 16:32:22 -07003528 const int maxC = dInsn->arg[0];
Bill Buzbee1465db52009-09-23 17:17:35 -07003529 RegLocation rlArray = cUnit->regLocation[mir->dalvikInsn.vA];
3530 RegLocation rlIdxInit = cUnit->regLocation[mir->dalvikInsn.vB];
Ben Cheng4238ec22009-08-24 16:32:22 -07003531
3532 /* regArray <- arrayRef */
Bill Buzbee1465db52009-09-23 17:17:35 -07003533 rlArray = loadValue(cUnit, rlArray, kCoreReg);
3534 rlIdxInit = loadValue(cUnit, rlIdxInit, kCoreReg);
3535 genRegImmCheck(cUnit, kArmCondEq, rlArray.lowReg, 0, 0,
Ben Cheng4238ec22009-08-24 16:32:22 -07003536 (ArmLIR *) cUnit->loopAnalysis->branchToPCR);
3537
3538 /* regLength <- len(arrayRef) */
Bill Buzbee1465db52009-09-23 17:17:35 -07003539 loadWordDisp(cUnit, rlArray.lowReg, lenOffset, regLength);
Ben Cheng4238ec22009-08-24 16:32:22 -07003540
3541 if (maxC) {
Bill Buzbeec6f10662010-02-09 11:16:15 -08003542 int tReg = dvmCompilerAllocTemp(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07003543 opRegRegImm(cUnit, kOpAdd, tReg, rlIdxInit.lowReg, maxC);
3544 rlIdxInit.lowReg = tReg;
Bill Buzbeec6f10662010-02-09 11:16:15 -08003545 dvmCompilerFreeTemp(cUnit, tReg);
Ben Cheng4238ec22009-08-24 16:32:22 -07003546 }
3547
3548 /* Punt if "regIdxInit < len(Array)" is false */
Bill Buzbee1465db52009-09-23 17:17:35 -07003549 genRegRegCheck(cUnit, kArmCondGe, rlIdxInit.lowReg, regLength, 0,
Ben Cheng0fd31e42009-09-03 14:40:16 -07003550 (ArmLIR *) cUnit->loopAnalysis->branchToPCR);
Ben Cheng4238ec22009-08-24 16:32:22 -07003551}
3552
3553/*
3554 * vA = idxReg;
3555 * vB = minC;
3556 */
3557static void genHoistedLowerBoundCheck(CompilationUnit *cUnit, MIR *mir)
3558{
3559 DecodedInstruction *dInsn = &mir->dalvikInsn;
Ben Cheng4238ec22009-08-24 16:32:22 -07003560 const int minC = dInsn->vB;
Bill Buzbee1465db52009-09-23 17:17:35 -07003561 RegLocation rlIdx = cUnit->regLocation[mir->dalvikInsn.vA];
Ben Cheng4238ec22009-08-24 16:32:22 -07003562
3563 /* regIdx <- initial index value */
Bill Buzbee1465db52009-09-23 17:17:35 -07003564 rlIdx = loadValue(cUnit, rlIdx, kCoreReg);
Ben Cheng4238ec22009-08-24 16:32:22 -07003565
3566 /* Punt if "regIdxInit + minC >= 0" is false */
Bill Buzbee1465db52009-09-23 17:17:35 -07003567 genRegImmCheck(cUnit, kArmCondLt, rlIdx.lowReg, -minC, 0,
Ben Cheng4238ec22009-08-24 16:32:22 -07003568 (ArmLIR *) cUnit->loopAnalysis->branchToPCR);
3569}
3570
3571/* Extended MIR instructions like PHI */
3572static void handleExtendedMIR(CompilationUnit *cUnit, MIR *mir)
3573{
Bill Buzbee1465db52009-09-23 17:17:35 -07003574 int opOffset = mir->dalvikInsn.opCode - kMirOpFirst;
Ben Cheng4238ec22009-08-24 16:32:22 -07003575 char *msg = dvmCompilerNew(strlen(extendedMIROpNames[opOffset]) + 1,
3576 false);
3577 strcpy(msg, extendedMIROpNames[opOffset]);
Bill Buzbee1465db52009-09-23 17:17:35 -07003578 newLIR1(cUnit, kArmPseudoExtended, (int) msg);
Ben Cheng4238ec22009-08-24 16:32:22 -07003579
3580 switch (mir->dalvikInsn.opCode) {
Bill Buzbee1465db52009-09-23 17:17:35 -07003581 case kMirOpPhi: {
Ben Cheng4238ec22009-08-24 16:32:22 -07003582 char *ssaString = dvmCompilerGetSSAString(cUnit, mir->ssaRep);
Bill Buzbee1465db52009-09-23 17:17:35 -07003583 newLIR1(cUnit, kArmPseudoSSARep, (int) ssaString);
Ben Cheng4238ec22009-08-24 16:32:22 -07003584 break;
3585 }
Bill Buzbee1465db52009-09-23 17:17:35 -07003586 case kMirOpNullNRangeUpCheck: {
Ben Cheng4238ec22009-08-24 16:32:22 -07003587 genHoistedChecksForCountUpLoop(cUnit, mir);
3588 break;
3589 }
Bill Buzbee1465db52009-09-23 17:17:35 -07003590 case kMirOpNullNRangeDownCheck: {
Ben Cheng4238ec22009-08-24 16:32:22 -07003591 genHoistedChecksForCountDownLoop(cUnit, mir);
3592 break;
3593 }
Bill Buzbee1465db52009-09-23 17:17:35 -07003594 case kMirOpLowerBound: {
Ben Cheng4238ec22009-08-24 16:32:22 -07003595 genHoistedLowerBoundCheck(cUnit, mir);
3596 break;
3597 }
Bill Buzbee1465db52009-09-23 17:17:35 -07003598 case kMirOpPunt: {
Ben Cheng4238ec22009-08-24 16:32:22 -07003599 genUnconditionalBranch(cUnit,
3600 (ArmLIR *) cUnit->loopAnalysis->branchToPCR);
3601 break;
3602 }
3603 default:
3604 break;
3605 }
3606}
3607
3608/*
3609 * Create a PC-reconstruction cell for the starting offset of this trace.
3610 * Since the PCR cell is placed near the end of the compiled code which is
3611 * usually out of range for a conditional branch, we put two branches (one
3612 * branch over to the loop body and one layover branch to the actual PCR) at the
3613 * end of the entry block.
3614 */
3615static void setupLoopEntryBlock(CompilationUnit *cUnit, BasicBlock *entry,
3616 ArmLIR *bodyLabel)
3617{
3618 /* Set up the place holder to reconstruct this Dalvik PC */
3619 ArmLIR *pcrLabel = dvmCompilerNew(sizeof(ArmLIR), true);
Ben Chenga4973592010-03-31 11:59:18 -07003620 pcrLabel->opCode = kArmPseudoPCReconstructionCell;
Ben Cheng4238ec22009-08-24 16:32:22 -07003621 pcrLabel->operands[0] =
3622 (int) (cUnit->method->insns + entry->startOffset);
3623 pcrLabel->operands[1] = entry->startOffset;
3624 /* Insert the place holder to the growable list */
3625 dvmInsertGrowableList(&cUnit->pcReconstructionList, pcrLabel);
3626
3627 /*
3628 * Next, create two branches - one branch over to the loop body and the
3629 * other branch to the PCR cell to punt.
3630 */
3631 ArmLIR *branchToBody = dvmCompilerNew(sizeof(ArmLIR), true);
Bill Buzbee1465db52009-09-23 17:17:35 -07003632 branchToBody->opCode = kThumbBUncond;
Ben Cheng4238ec22009-08-24 16:32:22 -07003633 branchToBody->generic.target = (LIR *) bodyLabel;
Ben Chengdcf3e5d2009-09-11 13:42:05 -07003634 setupResourceMasks(branchToBody);
Ben Cheng4238ec22009-08-24 16:32:22 -07003635 cUnit->loopAnalysis->branchToBody = (LIR *) branchToBody;
3636
3637 ArmLIR *branchToPCR = dvmCompilerNew(sizeof(ArmLIR), true);
Bill Buzbee1465db52009-09-23 17:17:35 -07003638 branchToPCR->opCode = kThumbBUncond;
Ben Cheng4238ec22009-08-24 16:32:22 -07003639 branchToPCR->generic.target = (LIR *) pcrLabel;
Ben Chengdcf3e5d2009-09-11 13:42:05 -07003640 setupResourceMasks(branchToPCR);
Ben Cheng4238ec22009-08-24 16:32:22 -07003641 cUnit->loopAnalysis->branchToPCR = (LIR *) branchToPCR;
3642}
3643
Ben Chengd5adae12010-03-26 17:45:28 -07003644#if defined(WITH_SELF_VERIFICATION)
3645static bool selfVerificationPuntOps(MIR *mir)
3646{
3647 DecodedInstruction *decInsn = &mir->dalvikInsn;
3648 OpCode op = decInsn->opCode;
3649 int flags = dexGetInstrFlags(gDvm.instrFlags, op);
3650 /*
3651 * All opcodes that can throw exceptions and use the
3652 * TEMPLATE_THROW_EXCEPTION_COMMON template should be excluded in the trace
3653 * under self-verification mode.
3654 */
3655 return (op == OP_MONITOR_ENTER || op == OP_MONITOR_EXIT ||
3656 op == OP_NEW_INSTANCE || op == OP_NEW_ARRAY ||
3657 op == OP_CHECK_CAST || op == OP_MOVE_EXCEPTION ||
3658 op == OP_FILL_ARRAY_DATA || op == OP_EXECUTE_INLINE ||
3659 op == OP_EXECUTE_INLINE_RANGE ||
3660 (flags & kInstrInvoke));
3661}
3662#endif
3663
Ben Chengba4fc8b2009-06-01 13:00:29 -07003664void dvmCompilerMIR2LIR(CompilationUnit *cUnit)
3665{
3666 /* Used to hold the labels of each block */
Bill Buzbee89efc3d2009-07-28 11:22:22 -07003667 ArmLIR *labelList =
3668 dvmCompilerNew(sizeof(ArmLIR) * cUnit->numBlocks, true);
Ben Chengcec26f62010-01-15 15:29:33 -08003669 GrowableList chainingListByType[kChainingCellGap];
Ben Chengba4fc8b2009-06-01 13:00:29 -07003670 int i;
3671
3672 /*
Ben Cheng38329f52009-07-07 14:19:20 -07003673 * Initialize various types chaining lists.
Ben Chengba4fc8b2009-06-01 13:00:29 -07003674 */
Ben Chengcec26f62010-01-15 15:29:33 -08003675 for (i = 0; i < kChainingCellGap; i++) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07003676 dvmInitGrowableList(&chainingListByType[i], 2);
3677 }
3678
3679 BasicBlock **blockList = cUnit->blockList;
3680
Bill Buzbee6e963e12009-06-17 16:56:19 -07003681 if (cUnit->executionCount) {
3682 /*
3683 * Reserve 6 bytes at the beginning of the trace
3684 * +----------------------------+
3685 * | execution count (4 bytes) |
3686 * +----------------------------+
3687 * | chain cell offset (2 bytes)|
3688 * +----------------------------+
3689 * ...and then code to increment the execution
3690 * count:
3691 * mov r0, pc @ move adr of "mov r0,pc" + 4 to r0
3692 * sub r0, #10 @ back up to addr of executionCount
3693 * ldr r1, [r0]
3694 * add r1, #1
3695 * str r1, [r0]
3696 */
Bill Buzbee1465db52009-09-23 17:17:35 -07003697 newLIR1(cUnit, kArm16BitData, 0);
3698 newLIR1(cUnit, kArm16BitData, 0);
Ben Chengcc6600c2009-06-22 14:45:16 -07003699 cUnit->chainCellOffsetLIR =
Bill Buzbee1465db52009-09-23 17:17:35 -07003700 (LIR *) newLIR1(cUnit, kArm16BitData, CHAIN_CELL_OFFSET_TAG);
Bill Buzbee6e963e12009-06-17 16:56:19 -07003701 cUnit->headerSize = 6;
Bill Buzbee270c1d62009-08-13 16:58:07 -07003702 /* Thumb instruction used directly here to ensure correct size */
Bill Buzbee1465db52009-09-23 17:17:35 -07003703 newLIR2(cUnit, kThumbMovRR_H2L, r0, rpc);
3704 newLIR2(cUnit, kThumbSubRI8, r0, 10);
3705 newLIR3(cUnit, kThumbLdrRRI5, r1, r0, 0);
3706 newLIR2(cUnit, kThumbAddRI8, r1, 1);
3707 newLIR3(cUnit, kThumbStrRRI5, r1, r0, 0);
Bill Buzbee6e963e12009-06-17 16:56:19 -07003708 } else {
3709 /* Just reserve 2 bytes for the chain cell offset */
Ben Chengcc6600c2009-06-22 14:45:16 -07003710 cUnit->chainCellOffsetLIR =
Bill Buzbee1465db52009-09-23 17:17:35 -07003711 (LIR *) newLIR1(cUnit, kArm16BitData, CHAIN_CELL_OFFSET_TAG);
Bill Buzbee6e963e12009-06-17 16:56:19 -07003712 cUnit->headerSize = 2;
3713 }
Ben Cheng1efc9c52009-06-08 18:25:27 -07003714
Ben Chengba4fc8b2009-06-01 13:00:29 -07003715 /* Handle the content in each basic block */
3716 for (i = 0; i < cUnit->numBlocks; i++) {
3717 blockList[i]->visited = true;
3718 MIR *mir;
3719
3720 labelList[i].operands[0] = blockList[i]->startOffset;
3721
Ben Chengcec26f62010-01-15 15:29:33 -08003722 if (blockList[i]->blockType >= kChainingCellGap) {
Ben Chengd44faf52010-06-02 15:33:51 -07003723 if (blockList[i]->firstMIRInsn != NULL &&
3724 ((blockList[i]->firstMIRInsn->dalvikInsn.opCode ==
3725 OP_MOVE_RESULT) ||
3726 (blockList[i]->firstMIRInsn->dalvikInsn.opCode ==
3727 OP_MOVE_RESULT_WIDE) ||
3728 (blockList[i]->firstMIRInsn->dalvikInsn.opCode ==
3729 OP_MOVE_RESULT_OBJECT))) {
3730 /* Align this block first since it is a return chaining cell */
3731 newLIR0(cUnit, kArmPseudoPseudoAlign4);
3732 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07003733 /*
3734 * Append the label pseudo LIR first. Chaining cells will be handled
3735 * separately afterwards.
3736 */
3737 dvmCompilerAppendLIR(cUnit, (LIR *) &labelList[i]);
3738 }
3739
Bill Buzbee1465db52009-09-23 17:17:35 -07003740 if (blockList[i]->blockType == kEntryBlock) {
Ben Chenga4973592010-03-31 11:59:18 -07003741 labelList[i].opCode = kArmPseudoEntryBlock;
Ben Cheng4238ec22009-08-24 16:32:22 -07003742 if (blockList[i]->firstMIRInsn == NULL) {
3743 continue;
3744 } else {
3745 setupLoopEntryBlock(cUnit, blockList[i],
3746 &labelList[blockList[i]->fallThrough->id]);
3747 }
Bill Buzbee1465db52009-09-23 17:17:35 -07003748 } else if (blockList[i]->blockType == kExitBlock) {
Ben Chenga4973592010-03-31 11:59:18 -07003749 labelList[i].opCode = kArmPseudoExitBlock;
Ben Cheng4238ec22009-08-24 16:32:22 -07003750 goto gen_fallthrough;
Bill Buzbee1465db52009-09-23 17:17:35 -07003751 } else if (blockList[i]->blockType == kDalvikByteCode) {
3752 labelList[i].opCode = kArmPseudoNormalBlockLabel;
Ben Chenge9695e52009-06-16 16:11:47 -07003753 /* Reset the register state */
Bill Buzbeec6f10662010-02-09 11:16:15 -08003754 dvmCompilerResetRegPool(cUnit);
3755 dvmCompilerClobberAllRegs(cUnit);
3756 dvmCompilerResetNullCheck(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003757 } else {
3758 switch (blockList[i]->blockType) {
Bill Buzbee1465db52009-09-23 17:17:35 -07003759 case kChainingCellNormal:
Ben Chenga4973592010-03-31 11:59:18 -07003760 labelList[i].opCode = kArmPseudoChainingCellNormal;
Ben Chengba4fc8b2009-06-01 13:00:29 -07003761 /* handle the codegen later */
3762 dvmInsertGrowableList(
Bill Buzbee1465db52009-09-23 17:17:35 -07003763 &chainingListByType[kChainingCellNormal], (void *) i);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003764 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07003765 case kChainingCellInvokeSingleton:
Ben Cheng38329f52009-07-07 14:19:20 -07003766 labelList[i].opCode =
Ben Chenga4973592010-03-31 11:59:18 -07003767 kArmPseudoChainingCellInvokeSingleton;
Ben Chengba4fc8b2009-06-01 13:00:29 -07003768 labelList[i].operands[0] =
3769 (int) blockList[i]->containingMethod;
3770 /* handle the codegen later */
3771 dvmInsertGrowableList(
Bill Buzbee1465db52009-09-23 17:17:35 -07003772 &chainingListByType[kChainingCellInvokeSingleton],
Ben Cheng38329f52009-07-07 14:19:20 -07003773 (void *) i);
3774 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07003775 case kChainingCellInvokePredicted:
Ben Cheng38329f52009-07-07 14:19:20 -07003776 labelList[i].opCode =
Ben Chenga4973592010-03-31 11:59:18 -07003777 kArmPseudoChainingCellInvokePredicted;
Ben Cheng38329f52009-07-07 14:19:20 -07003778 /* handle the codegen later */
3779 dvmInsertGrowableList(
Bill Buzbee1465db52009-09-23 17:17:35 -07003780 &chainingListByType[kChainingCellInvokePredicted],
Ben Cheng38329f52009-07-07 14:19:20 -07003781 (void *) i);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003782 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07003783 case kChainingCellHot:
Ben Chengba4fc8b2009-06-01 13:00:29 -07003784 labelList[i].opCode =
Ben Chenga4973592010-03-31 11:59:18 -07003785 kArmPseudoChainingCellHot;
Ben Chengba4fc8b2009-06-01 13:00:29 -07003786 /* handle the codegen later */
3787 dvmInsertGrowableList(
Bill Buzbee1465db52009-09-23 17:17:35 -07003788 &chainingListByType[kChainingCellHot],
Ben Chengba4fc8b2009-06-01 13:00:29 -07003789 (void *) i);
3790 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07003791 case kPCReconstruction:
Ben Chengba4fc8b2009-06-01 13:00:29 -07003792 /* Make sure exception handling block is next */
3793 labelList[i].opCode =
Ben Chenga4973592010-03-31 11:59:18 -07003794 kArmPseudoPCReconstructionBlockLabel;
Ben Chengba4fc8b2009-06-01 13:00:29 -07003795 assert (i == cUnit->numBlocks - 2);
3796 handlePCReconstruction(cUnit, &labelList[i+1]);
3797 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07003798 case kExceptionHandling:
3799 labelList[i].opCode = kArmPseudoEHBlockLabel;
Ben Chengba4fc8b2009-06-01 13:00:29 -07003800 if (cUnit->pcReconstructionList.numUsed) {
Bill Buzbee270c1d62009-08-13 16:58:07 -07003801 loadWordDisp(cUnit, rGLUE, offsetof(InterpState,
3802 jitToInterpEntries.dvmJitToInterpPunt),
3803 r1);
Bill Buzbee1465db52009-09-23 17:17:35 -07003804 opReg(cUnit, kOpBlx, r1);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003805 }
3806 break;
Bill Buzbee9c4b7c82009-09-10 10:10:38 -07003807#if defined(WITH_SELF_VERIFICATION) || defined(WITH_JIT_TUNING)
Bill Buzbee1465db52009-09-23 17:17:35 -07003808 case kChainingCellBackwardBranch:
Jeff Hao97319a82009-08-12 16:57:15 -07003809 labelList[i].opCode =
Ben Chenga4973592010-03-31 11:59:18 -07003810 kArmPseudoChainingCellBackwardBranch;
Jeff Hao97319a82009-08-12 16:57:15 -07003811 /* handle the codegen later */
3812 dvmInsertGrowableList(
Bill Buzbee1465db52009-09-23 17:17:35 -07003813 &chainingListByType[kChainingCellBackwardBranch],
Jeff Hao97319a82009-08-12 16:57:15 -07003814 (void *) i);
3815 break;
3816#endif
Ben Chengba4fc8b2009-06-01 13:00:29 -07003817 default:
3818 break;
3819 }
3820 continue;
3821 }
Ben Chenge9695e52009-06-16 16:11:47 -07003822
Bill Buzbee89efc3d2009-07-28 11:22:22 -07003823 ArmLIR *headLIR = NULL;
Ben Chenge9695e52009-06-16 16:11:47 -07003824
Ben Chengba4fc8b2009-06-01 13:00:29 -07003825 for (mir = blockList[i]->firstMIRInsn; mir; mir = mir->next) {
Bill Buzbee1465db52009-09-23 17:17:35 -07003826
Bill Buzbeec6f10662010-02-09 11:16:15 -08003827 dvmCompilerResetRegPool(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07003828 if (gDvmJit.disableOpt & (1 << kTrackLiveTemps)) {
Bill Buzbeec6f10662010-02-09 11:16:15 -08003829 dvmCompilerClobberAllRegs(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07003830 }
3831
3832 if (gDvmJit.disableOpt & (1 << kSuppressLoads)) {
Bill Buzbeec6f10662010-02-09 11:16:15 -08003833 dvmCompilerResetDefTracking(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07003834 }
3835
3836 if (mir->dalvikInsn.opCode >= kMirOpFirst) {
Ben Cheng4238ec22009-08-24 16:32:22 -07003837 handleExtendedMIR(cUnit, mir);
3838 continue;
3839 }
3840
Bill Buzbee1465db52009-09-23 17:17:35 -07003841
Ben Chengba4fc8b2009-06-01 13:00:29 -07003842 OpCode dalvikOpCode = mir->dalvikInsn.opCode;
3843 InstructionFormat dalvikFormat =
3844 dexGetInstrFormat(gDvm.instrFormat, dalvikOpCode);
Bill Buzbee89efc3d2009-07-28 11:22:22 -07003845 ArmLIR *boundaryLIR =
Ben Chenga4973592010-03-31 11:59:18 -07003846 newLIR2(cUnit, kArmPseudoDalvikByteCodeBoundary,
Ben Chengccd6c012009-10-15 14:52:45 -07003847 mir->offset,
3848 (int) dvmCompilerGetDalvikDisassembly(&mir->dalvikInsn)
3849 );
Ben Cheng4238ec22009-08-24 16:32:22 -07003850 if (mir->ssaRep) {
3851 char *ssaString = dvmCompilerGetSSAString(cUnit, mir->ssaRep);
Bill Buzbee1465db52009-09-23 17:17:35 -07003852 newLIR1(cUnit, kArmPseudoSSARep, (int) ssaString);
Ben Cheng4238ec22009-08-24 16:32:22 -07003853 }
3854
Ben Chenge9695e52009-06-16 16:11:47 -07003855 /* Remember the first LIR for this block */
3856 if (headLIR == NULL) {
3857 headLIR = boundaryLIR;
Ben Chengd7d426a2009-09-22 11:23:36 -07003858 /* Set the first boundaryLIR as a scheduling barrier */
3859 headLIR->defMask = ENCODE_ALL;
Ben Chenge9695e52009-06-16 16:11:47 -07003860 }
Ben Cheng4238ec22009-08-24 16:32:22 -07003861
Ben Chengba4fc8b2009-06-01 13:00:29 -07003862 bool notHandled;
3863 /*
3864 * Debugging: screen the opcode first to see if it is in the
3865 * do[-not]-compile list
3866 */
3867 bool singleStepMe =
3868 gDvmJit.includeSelectedOp !=
3869 ((gDvmJit.opList[dalvikOpCode >> 3] &
3870 (1 << (dalvikOpCode & 0x7))) !=
3871 0);
Ben Chengd5adae12010-03-26 17:45:28 -07003872#if defined(WITH_SELF_VERIFICATION)
3873 if (singleStepMe == false) {
3874 singleStepMe = selfVerificationPuntOps(mir);
3875 }
3876#endif
Ben Chengba4fc8b2009-06-01 13:00:29 -07003877 if (singleStepMe || cUnit->allSingleStep) {
3878 notHandled = false;
3879 genInterpSingleStep(cUnit, mir);
3880 } else {
3881 opcodeCoverage[dalvikOpCode]++;
3882 switch (dalvikFormat) {
3883 case kFmt10t:
3884 case kFmt20t:
3885 case kFmt30t:
3886 notHandled = handleFmt10t_Fmt20t_Fmt30t(cUnit,
3887 mir, blockList[i], labelList);
3888 break;
3889 case kFmt10x:
3890 notHandled = handleFmt10x(cUnit, mir);
3891 break;
3892 case kFmt11n:
3893 case kFmt31i:
3894 notHandled = handleFmt11n_Fmt31i(cUnit, mir);
3895 break;
3896 case kFmt11x:
3897 notHandled = handleFmt11x(cUnit, mir);
3898 break;
3899 case kFmt12x:
3900 notHandled = handleFmt12x(cUnit, mir);
3901 break;
3902 case kFmt20bc:
3903 notHandled = handleFmt20bc(cUnit, mir);
3904 break;
3905 case kFmt21c:
3906 case kFmt31c:
3907 notHandled = handleFmt21c_Fmt31c(cUnit, mir);
3908 break;
3909 case kFmt21h:
3910 notHandled = handleFmt21h(cUnit, mir);
3911 break;
3912 case kFmt21s:
3913 notHandled = handleFmt21s(cUnit, mir);
3914 break;
3915 case kFmt21t:
3916 notHandled = handleFmt21t(cUnit, mir, blockList[i],
3917 labelList);
3918 break;
3919 case kFmt22b:
3920 case kFmt22s:
3921 notHandled = handleFmt22b_Fmt22s(cUnit, mir);
3922 break;
3923 case kFmt22c:
3924 notHandled = handleFmt22c(cUnit, mir);
3925 break;
3926 case kFmt22cs:
3927 notHandled = handleFmt22cs(cUnit, mir);
3928 break;
3929 case kFmt22t:
3930 notHandled = handleFmt22t(cUnit, mir, blockList[i],
3931 labelList);
3932 break;
3933 case kFmt22x:
3934 case kFmt32x:
3935 notHandled = handleFmt22x_Fmt32x(cUnit, mir);
3936 break;
3937 case kFmt23x:
3938 notHandled = handleFmt23x(cUnit, mir);
3939 break;
3940 case kFmt31t:
3941 notHandled = handleFmt31t(cUnit, mir);
3942 break;
3943 case kFmt3rc:
3944 case kFmt35c:
3945 notHandled = handleFmt35c_3rc(cUnit, mir, blockList[i],
3946 labelList);
3947 break;
3948 case kFmt3rms:
3949 case kFmt35ms:
3950 notHandled = handleFmt35ms_3rms(cUnit, mir,blockList[i],
3951 labelList);
3952 break;
3953 case kFmt3inline:
Andy McFaddenb0a05412009-11-19 10:23:41 -08003954 case kFmt3rinline:
Bill Buzbeece46c942009-11-20 15:41:34 -08003955 notHandled = handleExecuteInline(cUnit, mir);
Andy McFaddenb0a05412009-11-19 10:23:41 -08003956 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07003957 case kFmt51l:
3958 notHandled = handleFmt51l(cUnit, mir);
3959 break;
3960 default:
3961 notHandled = true;
3962 break;
3963 }
3964 }
3965 if (notHandled) {
3966 LOGE("%#06x: Opcode 0x%x (%s) / Fmt %d not handled\n",
3967 mir->offset,
Andy McFaddenc6b25c72010-06-22 11:01:20 -07003968 dalvikOpCode, dexGetOpcodeName(dalvikOpCode),
Ben Chengba4fc8b2009-06-01 13:00:29 -07003969 dalvikFormat);
Bill Buzbeefc519dc2010-03-06 23:30:57 -08003970 dvmCompilerAbort(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003971 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07003972 }
3973 }
Ben Cheng4238ec22009-08-24 16:32:22 -07003974
Bill Buzbee1465db52009-09-23 17:17:35 -07003975 if (blockList[i]->blockType == kEntryBlock) {
Ben Cheng4238ec22009-08-24 16:32:22 -07003976 dvmCompilerAppendLIR(cUnit,
3977 (LIR *) cUnit->loopAnalysis->branchToBody);
3978 dvmCompilerAppendLIR(cUnit,
3979 (LIR *) cUnit->loopAnalysis->branchToPCR);
3980 }
3981
3982 if (headLIR) {
3983 /*
3984 * Eliminate redundant loads/stores and delay stores into later
3985 * slots
3986 */
3987 dvmCompilerApplyLocalOptimizations(cUnit, (LIR *) headLIR,
3988 cUnit->lastLIRInsn);
3989 }
3990
3991gen_fallthrough:
Ben Cheng1efc9c52009-06-08 18:25:27 -07003992 /*
3993 * Check if the block is terminated due to trace length constraint -
3994 * insert an unconditional branch to the chaining cell.
3995 */
3996 if (blockList[i]->needFallThroughBranch) {
3997 genUnconditionalBranch(cUnit,
3998 &labelList[blockList[i]->fallThrough->id]);
3999 }
4000
Ben Chengba4fc8b2009-06-01 13:00:29 -07004001 }
4002
Ben Chenge9695e52009-06-16 16:11:47 -07004003 /* Handle the chaining cells in predefined order */
Ben Chengcec26f62010-01-15 15:29:33 -08004004 for (i = 0; i < kChainingCellGap; i++) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07004005 size_t j;
4006 int *blockIdList = (int *) chainingListByType[i].elemList;
4007
4008 cUnit->numChainingCells[i] = chainingListByType[i].numUsed;
4009
4010 /* No chaining cells of this type */
4011 if (cUnit->numChainingCells[i] == 0)
4012 continue;
4013
4014 /* Record the first LIR for a new type of chaining cell */
4015 cUnit->firstChainingLIR[i] = (LIR *) &labelList[blockIdList[0]];
4016
4017 for (j = 0; j < chainingListByType[i].numUsed; j++) {
4018 int blockId = blockIdList[j];
4019
4020 /* Align this chaining cell first */
Bill Buzbee1465db52009-09-23 17:17:35 -07004021 newLIR0(cUnit, kArmPseudoPseudoAlign4);
Ben Chengba4fc8b2009-06-01 13:00:29 -07004022
4023 /* Insert the pseudo chaining instruction */
4024 dvmCompilerAppendLIR(cUnit, (LIR *) &labelList[blockId]);
4025
4026
4027 switch (blockList[blockId]->blockType) {
Bill Buzbee1465db52009-09-23 17:17:35 -07004028 case kChainingCellNormal:
Ben Cheng1efc9c52009-06-08 18:25:27 -07004029 handleNormalChainingCell(cUnit,
Ben Chengba4fc8b2009-06-01 13:00:29 -07004030 blockList[blockId]->startOffset);
4031 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07004032 case kChainingCellInvokeSingleton:
Ben Cheng38329f52009-07-07 14:19:20 -07004033 handleInvokeSingletonChainingCell(cUnit,
Ben Chengba4fc8b2009-06-01 13:00:29 -07004034 blockList[blockId]->containingMethod);
4035 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07004036 case kChainingCellInvokePredicted:
Ben Cheng38329f52009-07-07 14:19:20 -07004037 handleInvokePredictedChainingCell(cUnit);
4038 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07004039 case kChainingCellHot:
Ben Cheng1efc9c52009-06-08 18:25:27 -07004040 handleHotChainingCell(cUnit,
Ben Chengba4fc8b2009-06-01 13:00:29 -07004041 blockList[blockId]->startOffset);
4042 break;
Bill Buzbee9c4b7c82009-09-10 10:10:38 -07004043#if defined(WITH_SELF_VERIFICATION) || defined(WITH_JIT_TUNING)
Bill Buzbee1465db52009-09-23 17:17:35 -07004044 case kChainingCellBackwardBranch:
Jeff Hao97319a82009-08-12 16:57:15 -07004045 handleBackwardBranchChainingCell(cUnit,
4046 blockList[blockId]->startOffset);
4047 break;
4048#endif
Ben Chengba4fc8b2009-06-01 13:00:29 -07004049 default:
Bill Buzbee1465db52009-09-23 17:17:35 -07004050 LOGE("Bad blocktype %d", blockList[blockId]->blockType);
Bill Buzbeefc519dc2010-03-06 23:30:57 -08004051 dvmCompilerAbort(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -07004052 }
4053 }
4054 }
Ben Chenge9695e52009-06-16 16:11:47 -07004055
Ben Chengcec26f62010-01-15 15:29:33 -08004056 /* Mark the bottom of chaining cells */
4057 cUnit->chainingCellBottom = (LIR *) newLIR0(cUnit, kArmChainingCellBottom);
4058
Ben Cheng6c10a972009-10-29 14:39:18 -07004059 /*
4060 * Generate the branch to the dvmJitToInterpNoChain entry point at the end
4061 * of all chaining cells for the overflow cases.
4062 */
4063 if (cUnit->switchOverflowPad) {
4064 loadConstant(cUnit, r0, (int) cUnit->switchOverflowPad);
4065 loadWordDisp(cUnit, rGLUE, offsetof(InterpState,
4066 jitToInterpEntries.dvmJitToInterpNoChain), r2);
4067 opRegReg(cUnit, kOpAdd, r1, r1);
4068 opRegRegReg(cUnit, kOpAdd, r4PC, r0, r1);
Ben Cheng978738d2010-05-13 13:45:57 -07004069#if defined(WITH_JIT_TUNING)
Ben Cheng6c10a972009-10-29 14:39:18 -07004070 loadConstant(cUnit, r0, kSwitchOverflow);
4071#endif
4072 opReg(cUnit, kOpBlx, r2);
4073 }
4074
Ben Chenge9695e52009-06-16 16:11:47 -07004075 dvmCompilerApplyGlobalOptimizations(cUnit);
jeffhao9e45c0b2010-02-03 10:24:05 -08004076
4077#if defined(WITH_SELF_VERIFICATION)
4078 selfVerificationBranchInsertPass(cUnit);
4079#endif
Ben Chengba4fc8b2009-06-01 13:00:29 -07004080}
4081
4082/* Accept the work and start compiling */
Bill Buzbee716f1202009-07-23 13:22:09 -07004083bool dvmCompilerDoWork(CompilerWorkOrder *work)
Ben Chengba4fc8b2009-06-01 13:00:29 -07004084{
Ben Chengccd6c012009-10-15 14:52:45 -07004085 bool res;
Ben Chengba4fc8b2009-06-01 13:00:29 -07004086
Ben Cheng6999d842010-01-26 16:46:15 -08004087 if (gDvmJit.codeCacheFull) {
Ben Chengccd6c012009-10-15 14:52:45 -07004088 return false;
4089 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07004090
Ben Chengccd6c012009-10-15 14:52:45 -07004091 switch (work->kind) {
4092 case kWorkOrderMethod:
4093 res = dvmCompileMethod(work->info, &work->result);
4094 break;
4095 case kWorkOrderTrace:
4096 /* Start compilation with maximally allowed trace length */
Bill Buzbeefc519dc2010-03-06 23:30:57 -08004097 res = dvmCompileTrace(work->info, JIT_MAX_TRACE_LEN, &work->result,
4098 work->bailPtr);
Ben Chengccd6c012009-10-15 14:52:45 -07004099 break;
4100 case kWorkOrderTraceDebug: {
4101 bool oldPrintMe = gDvmJit.printMe;
4102 gDvmJit.printMe = true;
4103 /* Start compilation with maximally allowed trace length */
Bill Buzbeefc519dc2010-03-06 23:30:57 -08004104 res = dvmCompileTrace(work->info, JIT_MAX_TRACE_LEN, &work->result,
4105 work->bailPtr);
Elliott Hughes672511b2010-04-26 17:40:13 -07004106 gDvmJit.printMe = oldPrintMe;
Ben Chengccd6c012009-10-15 14:52:45 -07004107 break;
4108 }
4109 default:
4110 res = false;
Bill Buzbeefc519dc2010-03-06 23:30:57 -08004111 LOGE("Jit: unknown work order type");
Elliott Hughes672511b2010-04-26 17:40:13 -07004112 assert(0); // Bail if debug build, discard otherwise
Ben Chengccd6c012009-10-15 14:52:45 -07004113 }
4114 return res;
Ben Chengba4fc8b2009-06-01 13:00:29 -07004115}
4116
Ben Chengba4fc8b2009-06-01 13:00:29 -07004117/* Architectural-specific debugging helpers go here */
4118void dvmCompilerArchDump(void)
4119{
4120 /* Print compiled opcode in this VM instance */
4121 int i, start, streak;
4122 char buf[1024];
4123
4124 streak = i = 0;
4125 buf[0] = 0;
4126 while (opcodeCoverage[i] == 0 && i < 256) {
4127 i++;
4128 }
4129 if (i == 256) {
4130 return;
4131 }
4132 for (start = i++, streak = 1; i < 256; i++) {
4133 if (opcodeCoverage[i]) {
4134 streak++;
4135 } else {
4136 if (streak == 1) {
4137 sprintf(buf+strlen(buf), "%x,", start);
4138 } else {
4139 sprintf(buf+strlen(buf), "%x-%x,", start, start + streak - 1);
4140 }
4141 streak = 0;
4142 while (opcodeCoverage[i] == 0 && i < 256) {
4143 i++;
4144 }
4145 if (i < 256) {
4146 streak = 1;
4147 start = i;
4148 }
4149 }
4150 }
4151 if (streak) {
4152 if (streak == 1) {
4153 sprintf(buf+strlen(buf), "%x", start);
4154 } else {
4155 sprintf(buf+strlen(buf), "%x-%x", start, start + streak - 1);
4156 }
4157 }
4158 if (strlen(buf)) {
Ben Cheng8b258bf2009-06-24 17:27:07 -07004159 LOGD("dalvik.vm.jit.op = %s", buf);
Ben Chengba4fc8b2009-06-01 13:00:29 -07004160 }
4161}
Ben Chengd7d426a2009-09-22 11:23:36 -07004162
4163/* Common initialization routine for an architecture family */
4164bool dvmCompilerArchInit()
4165{
4166 int i;
4167
Bill Buzbee1465db52009-09-23 17:17:35 -07004168 for (i = 0; i < kArmLast; i++) {
Ben Chengd7d426a2009-09-22 11:23:36 -07004169 if (EncodingMap[i].opCode != i) {
4170 LOGE("Encoding order for %s is wrong: expecting %d, seeing %d",
4171 EncodingMap[i].name, i, EncodingMap[i].opCode);
Bill Buzbeefc519dc2010-03-06 23:30:57 -08004172 dvmAbort(); // OK to dvmAbort - build error
Ben Chengd7d426a2009-09-22 11:23:36 -07004173 }
4174 }
4175
Ben Cheng5d90c202009-11-22 23:31:11 -08004176 return dvmCompilerArchVariantInit();
4177}
4178
4179void *dvmCompilerGetInterpretTemplate()
4180{
4181 return (void*) ((int)gDvmJit.codeCache +
4182 templateEntryOffsets[TEMPLATE_INTERPRET]);
4183}
4184
4185/* Needed by the ld/st optmizatons */
4186ArmLIR* dvmCompilerRegCopyNoInsert(CompilationUnit *cUnit, int rDest, int rSrc)
4187{
4188 return genRegCopyNoInsert(cUnit, rDest, rSrc);
4189}
4190
4191/* Needed by the register allocator */
4192ArmLIR* dvmCompilerRegCopy(CompilationUnit *cUnit, int rDest, int rSrc)
4193{
4194 return genRegCopy(cUnit, rDest, rSrc);
4195}
4196
4197/* Needed by the register allocator */
4198void dvmCompilerRegCopyWide(CompilationUnit *cUnit, int destLo, int destHi,
4199 int srcLo, int srcHi)
4200{
4201 genRegCopyWide(cUnit, destLo, destHi, srcLo, srcHi);
4202}
4203
4204void dvmCompilerFlushRegImpl(CompilationUnit *cUnit, int rBase,
4205 int displacement, int rSrc, OpSize size)
4206{
4207 storeBaseDisp(cUnit, rBase, displacement, rSrc, size);
4208}
4209
4210void dvmCompilerFlushRegWideImpl(CompilationUnit *cUnit, int rBase,
4211 int displacement, int rSrcLo, int rSrcHi)
4212{
4213 storeBaseDispWide(cUnit, rBase, displacement, rSrcLo, rSrcHi);
Ben Chengd7d426a2009-09-22 11:23:36 -07004214}