blob: d69991d5d056b6772ba19fe231791d22235809bd [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 Chengba4fc8b2009-06-01 13:00:29 -070080
Ben Cheng5d90c202009-11-22 23:31:11 -080081static bool genArithOpFloatPortable(CompilationUnit *cUnit, MIR *mir,
82 RegLocation rlDest, RegLocation rlSrc1,
83 RegLocation rlSrc2)
84{
85 RegLocation rlResult;
86 void* funct;
87
Ben Cheng5d90c202009-11-22 23:31:11 -080088 switch (mir->dalvikInsn.opCode) {
89 case OP_ADD_FLOAT_2ADDR:
90 case OP_ADD_FLOAT:
91 funct = (void*) __aeabi_fadd;
92 break;
93 case OP_SUB_FLOAT_2ADDR:
94 case OP_SUB_FLOAT:
95 funct = (void*) __aeabi_fsub;
96 break;
97 case OP_DIV_FLOAT_2ADDR:
98 case OP_DIV_FLOAT:
99 funct = (void*) __aeabi_fdiv;
100 break;
101 case OP_MUL_FLOAT_2ADDR:
102 case OP_MUL_FLOAT:
103 funct = (void*) __aeabi_fmul;
104 break;
105 case OP_REM_FLOAT_2ADDR:
106 case OP_REM_FLOAT:
107 funct = (void*) fmodf;
108 break;
109 case OP_NEG_FLOAT: {
110 genNegFloat(cUnit, rlDest, rlSrc1);
111 return false;
112 }
113 default:
114 return true;
115 }
Bill Buzbeec6f10662010-02-09 11:16:15 -0800116 dvmCompilerFlushAllRegs(cUnit); /* Send everything to home location */
Ben Cheng5d90c202009-11-22 23:31:11 -0800117 loadValueDirectFixed(cUnit, rlSrc1, r0);
118 loadValueDirectFixed(cUnit, rlSrc2, r1);
Ben Chengbd1326d2010-04-02 15:04:53 -0700119 LOAD_FUNC_ADDR(cUnit, r2, (int)funct);
Ben Cheng5d90c202009-11-22 23:31:11 -0800120 opReg(cUnit, kOpBlx, r2);
Elliott Hughes6a555132010-02-25 15:41:42 -0800121 dvmCompilerClobberCallRegs(cUnit);
Bill Buzbeec6f10662010-02-09 11:16:15 -0800122 rlResult = dvmCompilerGetReturn(cUnit);
Ben Cheng5d90c202009-11-22 23:31:11 -0800123 storeValue(cUnit, rlDest, rlResult);
124 return false;
125}
126
127static bool genArithOpDoublePortable(CompilationUnit *cUnit, MIR *mir,
128 RegLocation rlDest, RegLocation rlSrc1,
129 RegLocation rlSrc2)
130{
131 RegLocation rlResult;
132 void* funct;
133
Ben Cheng5d90c202009-11-22 23:31:11 -0800134 switch (mir->dalvikInsn.opCode) {
135 case OP_ADD_DOUBLE_2ADDR:
136 case OP_ADD_DOUBLE:
137 funct = (void*) __aeabi_dadd;
138 break;
139 case OP_SUB_DOUBLE_2ADDR:
140 case OP_SUB_DOUBLE:
141 funct = (void*) __aeabi_dsub;
142 break;
143 case OP_DIV_DOUBLE_2ADDR:
144 case OP_DIV_DOUBLE:
145 funct = (void*) __aeabi_ddiv;
146 break;
147 case OP_MUL_DOUBLE_2ADDR:
148 case OP_MUL_DOUBLE:
149 funct = (void*) __aeabi_dmul;
150 break;
151 case OP_REM_DOUBLE_2ADDR:
152 case OP_REM_DOUBLE:
153 funct = (void*) fmod;
154 break;
155 case OP_NEG_DOUBLE: {
156 genNegDouble(cUnit, rlDest, rlSrc1);
157 return false;
158 }
159 default:
160 return true;
161 }
Bill Buzbeec6f10662010-02-09 11:16:15 -0800162 dvmCompilerFlushAllRegs(cUnit); /* Send everything to home location */
Ben Chengbd1326d2010-04-02 15:04:53 -0700163 LOAD_FUNC_ADDR(cUnit, rlr, (int)funct);
Ben Cheng5d90c202009-11-22 23:31:11 -0800164 loadValueDirectWideFixed(cUnit, rlSrc1, r0, r1);
165 loadValueDirectWideFixed(cUnit, rlSrc2, r2, r3);
166 opReg(cUnit, kOpBlx, rlr);
Elliott Hughes6a555132010-02-25 15:41:42 -0800167 dvmCompilerClobberCallRegs(cUnit);
Bill Buzbeec6f10662010-02-09 11:16:15 -0800168 rlResult = dvmCompilerGetReturnWide(cUnit);
Ben Cheng5d90c202009-11-22 23:31:11 -0800169 storeValueWide(cUnit, rlDest, rlResult);
170 return false;
171}
172
173static bool genConversionPortable(CompilationUnit *cUnit, MIR *mir)
174{
175 OpCode opCode = mir->dalvikInsn.opCode;
176
Ben Cheng5d90c202009-11-22 23:31:11 -0800177 switch (opCode) {
178 case OP_INT_TO_FLOAT:
179 return genConversionCall(cUnit, mir, (void*)__aeabi_i2f, 1, 1);
180 case OP_FLOAT_TO_INT:
181 return genConversionCall(cUnit, mir, (void*)__aeabi_f2iz, 1, 1);
182 case OP_DOUBLE_TO_FLOAT:
183 return genConversionCall(cUnit, mir, (void*)__aeabi_d2f, 2, 1);
184 case OP_FLOAT_TO_DOUBLE:
185 return genConversionCall(cUnit, mir, (void*)__aeabi_f2d, 1, 2);
186 case OP_INT_TO_DOUBLE:
187 return genConversionCall(cUnit, mir, (void*)__aeabi_i2d, 1, 2);
188 case OP_DOUBLE_TO_INT:
189 return genConversionCall(cUnit, mir, (void*)__aeabi_d2iz, 2, 1);
190 case OP_FLOAT_TO_LONG:
191 return genConversionCall(cUnit, mir, (void*)dvmJitf2l, 1, 2);
192 case OP_LONG_TO_FLOAT:
193 return genConversionCall(cUnit, mir, (void*)__aeabi_l2f, 2, 1);
194 case OP_DOUBLE_TO_LONG:
195 return genConversionCall(cUnit, mir, (void*)dvmJitd2l, 2, 2);
196 case OP_LONG_TO_DOUBLE:
197 return genConversionCall(cUnit, mir, (void*)__aeabi_l2d, 2, 2);
198 default:
199 return true;
200 }
201 return false;
202}
Ben Chengba4fc8b2009-06-01 13:00:29 -0700203
Jeff Hao97319a82009-08-12 16:57:15 -0700204#if defined(WITH_SELF_VERIFICATION)
jeffhao9e45c0b2010-02-03 10:24:05 -0800205static void selfVerificationBranchInsert(LIR *currentLIR, ArmOpCode opCode,
206 int dest, int src1)
Jeff Hao97319a82009-08-12 16:57:15 -0700207{
jeffhao9e45c0b2010-02-03 10:24:05 -0800208 ArmLIR *insn = dvmCompilerNew(sizeof(ArmLIR), true);
209 insn->opCode = opCode;
210 insn->operands[0] = dest;
211 insn->operands[1] = src1;
212 setupResourceMasks(insn);
213 dvmCompilerInsertLIRBefore(currentLIR, (LIR *) insn);
Jeff Hao97319a82009-08-12 16:57:15 -0700214}
215
jeffhao9e45c0b2010-02-03 10:24:05 -0800216static void selfVerificationBranchInsertPass(CompilationUnit *cUnit)
Jeff Hao97319a82009-08-12 16:57:15 -0700217{
jeffhao9e45c0b2010-02-03 10:24:05 -0800218 ArmLIR *thisLIR;
jeffhao9e45c0b2010-02-03 10:24:05 -0800219 TemplateOpCode opCode = TEMPLATE_MEM_OP_DECODE;
Jeff Hao97319a82009-08-12 16:57:15 -0700220
jeffhao9e45c0b2010-02-03 10:24:05 -0800221 for (thisLIR = (ArmLIR *) cUnit->firstLIRInsn;
222 thisLIR != (ArmLIR *) cUnit->lastLIRInsn;
223 thisLIR = NEXT_LIR(thisLIR)) {
224 if (thisLIR->branchInsertSV) {
225 /* Branch to mem op decode template */
226 selfVerificationBranchInsert((LIR *) thisLIR, kThumbBlx1,
227 (int) gDvmJit.codeCache + templateEntryOffsets[opCode],
228 (int) gDvmJit.codeCache + templateEntryOffsets[opCode]);
229 selfVerificationBranchInsert((LIR *) thisLIR, kThumbBlx2,
230 (int) gDvmJit.codeCache + templateEntryOffsets[opCode],
231 (int) gDvmJit.codeCache + templateEntryOffsets[opCode]);
Jeff Hao97319a82009-08-12 16:57:15 -0700232 }
233 }
Jeff Hao97319a82009-08-12 16:57:15 -0700234}
Jeff Hao97319a82009-08-12 16:57:15 -0700235#endif
236
Bill Buzbeebe6534f2010-03-12 16:01:35 -0800237/* Generate conditional branch instructions */
238static ArmLIR *genConditionalBranch(CompilationUnit *cUnit,
239 ArmConditionCode cond,
240 ArmLIR *target)
241{
242 ArmLIR *branch = opCondBranch(cUnit, cond);
243 branch->generic.target = (LIR *) target;
244 return branch;
245}
246
Ben Chengba4fc8b2009-06-01 13:00:29 -0700247/* Generate a unconditional branch to go to the interpreter */
Bill Buzbee89efc3d2009-07-28 11:22:22 -0700248static inline ArmLIR *genTrap(CompilationUnit *cUnit, int dOffset,
249 ArmLIR *pcrLabel)
Ben Chengba4fc8b2009-06-01 13:00:29 -0700250{
Bill Buzbee1465db52009-09-23 17:17:35 -0700251 ArmLIR *branch = opNone(cUnit, kOpUncondBr);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700252 return genCheckCommon(cUnit, dOffset, branch, pcrLabel);
253}
254
255/* Load a wide field from an object instance */
256static void genIGetWide(CompilationUnit *cUnit, MIR *mir, int fieldOffset)
257{
Bill Buzbeec6f10662010-02-09 11:16:15 -0800258 RegLocation rlObj = dvmCompilerGetSrc(cUnit, mir, 0);
259 RegLocation rlDest = dvmCompilerGetDestWide(cUnit, mir, 0, 1);
Bill Buzbee1465db52009-09-23 17:17:35 -0700260 RegLocation rlResult;
261 rlObj = loadValue(cUnit, rlObj, kCoreReg);
Bill Buzbeec6f10662010-02-09 11:16:15 -0800262 int regPtr = dvmCompilerAllocTemp(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700263
Bill Buzbee1465db52009-09-23 17:17:35 -0700264 assert(rlDest.wide);
Ben Chenge9695e52009-06-16 16:11:47 -0700265
Bill Buzbee1465db52009-09-23 17:17:35 -0700266 genNullCheck(cUnit, rlObj.sRegLow, rlObj.lowReg, mir->offset,
267 NULL);/* null object? */
268 opRegRegImm(cUnit, kOpAdd, regPtr, rlObj.lowReg, fieldOffset);
Bill Buzbeec6f10662010-02-09 11:16:15 -0800269 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kAnyReg, true);
Ben Cheng11d8f142010-03-24 15:24:19 -0700270
271 HEAP_ACCESS_SHADOW(true);
Bill Buzbee1465db52009-09-23 17:17:35 -0700272 loadPair(cUnit, regPtr, rlResult.lowReg, rlResult.highReg);
Ben Cheng11d8f142010-03-24 15:24:19 -0700273 HEAP_ACCESS_SHADOW(false);
274
Bill Buzbeec6f10662010-02-09 11:16:15 -0800275 dvmCompilerFreeTemp(cUnit, regPtr);
Bill Buzbee1465db52009-09-23 17:17:35 -0700276 storeValueWide(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700277}
278
279/* Store a wide field to an object instance */
280static void genIPutWide(CompilationUnit *cUnit, MIR *mir, int fieldOffset)
281{
Bill Buzbeec6f10662010-02-09 11:16:15 -0800282 RegLocation rlSrc = dvmCompilerGetSrcWide(cUnit, mir, 0, 1);
283 RegLocation rlObj = dvmCompilerGetSrc(cUnit, mir, 2);
Bill Buzbee1465db52009-09-23 17:17:35 -0700284 rlObj = loadValue(cUnit, rlObj, kCoreReg);
285 int regPtr;
286 rlSrc = loadValueWide(cUnit, rlSrc, kAnyReg);
287 genNullCheck(cUnit, rlObj.sRegLow, rlObj.lowReg, mir->offset,
288 NULL);/* null object? */
Bill Buzbeec6f10662010-02-09 11:16:15 -0800289 regPtr = dvmCompilerAllocTemp(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -0700290 opRegRegImm(cUnit, kOpAdd, regPtr, rlObj.lowReg, fieldOffset);
Ben Cheng11d8f142010-03-24 15:24:19 -0700291
292 HEAP_ACCESS_SHADOW(true);
Bill Buzbee1465db52009-09-23 17:17:35 -0700293 storePair(cUnit, regPtr, rlSrc.lowReg, rlSrc.highReg);
Ben Cheng11d8f142010-03-24 15:24:19 -0700294 HEAP_ACCESS_SHADOW(false);
295
Bill Buzbeec6f10662010-02-09 11:16:15 -0800296 dvmCompilerFreeTemp(cUnit, regPtr);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700297}
298
299/*
300 * Load a field from an object instance
301 *
Ben Chengba4fc8b2009-06-01 13:00:29 -0700302 */
Bill Buzbee270c1d62009-08-13 16:58:07 -0700303static void genIGet(CompilationUnit *cUnit, MIR *mir, OpSize size,
Ben Chengba4fc8b2009-06-01 13:00:29 -0700304 int fieldOffset)
305{
Bill Buzbee1465db52009-09-23 17:17:35 -0700306 RegLocation rlResult;
Bill Buzbee749e8162010-07-07 06:55:56 -0700307 RegisterClass regClass = dvmCompilerRegClassBySize(size);
Bill Buzbeec6f10662010-02-09 11:16:15 -0800308 RegLocation rlObj = dvmCompilerGetSrc(cUnit, mir, 0);
309 RegLocation rlDest = dvmCompilerGetDest(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -0700310 rlObj = loadValue(cUnit, rlObj, kCoreReg);
Bill Buzbee749e8162010-07-07 06:55:56 -0700311 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, regClass, true);
Bill Buzbee1465db52009-09-23 17:17:35 -0700312 genNullCheck(cUnit, rlObj.sRegLow, rlObj.lowReg, mir->offset,
313 NULL);/* null object? */
Ben Cheng11d8f142010-03-24 15:24:19 -0700314
315 HEAP_ACCESS_SHADOW(true);
Ben Cheng5d90c202009-11-22 23:31:11 -0800316 loadBaseDisp(cUnit, mir, rlObj.lowReg, fieldOffset, rlResult.lowReg,
317 size, rlObj.sRegLow);
Ben Cheng11d8f142010-03-24 15:24:19 -0700318 HEAP_ACCESS_SHADOW(false);
319
Bill Buzbee1465db52009-09-23 17:17:35 -0700320 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700321}
322
323/*
324 * Store a field to an object instance
325 *
Ben Chengba4fc8b2009-06-01 13:00:29 -0700326 */
Bill Buzbee270c1d62009-08-13 16:58:07 -0700327static void genIPut(CompilationUnit *cUnit, MIR *mir, OpSize size,
buzbee919eb062010-07-12 12:59:22 -0700328 int fieldOffset, bool isObject)
Ben Chengba4fc8b2009-06-01 13:00:29 -0700329{
Bill Buzbee749e8162010-07-07 06:55:56 -0700330 RegisterClass regClass = dvmCompilerRegClassBySize(size);
Bill Buzbeec6f10662010-02-09 11:16:15 -0800331 RegLocation rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
332 RegLocation rlObj = dvmCompilerGetSrc(cUnit, mir, 1);
Bill Buzbee1465db52009-09-23 17:17:35 -0700333 rlObj = loadValue(cUnit, rlObj, kCoreReg);
Bill Buzbee749e8162010-07-07 06:55:56 -0700334 rlSrc = loadValue(cUnit, rlSrc, regClass);
Bill Buzbee1465db52009-09-23 17:17:35 -0700335 genNullCheck(cUnit, rlObj.sRegLow, rlObj.lowReg, mir->offset,
336 NULL);/* null object? */
Ben Cheng11d8f142010-03-24 15:24:19 -0700337
338 HEAP_ACCESS_SHADOW(true);
Bill Buzbee1465db52009-09-23 17:17:35 -0700339 storeBaseDisp(cUnit, rlObj.lowReg, fieldOffset, rlSrc.lowReg, size);
Ben Cheng11d8f142010-03-24 15:24:19 -0700340 HEAP_ACCESS_SHADOW(false);
buzbee919eb062010-07-12 12:59:22 -0700341 if (isObject) {
342 /* NOTE: marking card based on object head */
343 markCard(cUnit, rlSrc.lowReg, rlObj.lowReg);
344 }
Ben Chengba4fc8b2009-06-01 13:00:29 -0700345}
346
347
Ben Chengba4fc8b2009-06-01 13:00:29 -0700348/*
349 * Generate array load
Ben Chengba4fc8b2009-06-01 13:00:29 -0700350 */
Bill Buzbee270c1d62009-08-13 16:58:07 -0700351static void genArrayGet(CompilationUnit *cUnit, MIR *mir, OpSize size,
Bill Buzbee1465db52009-09-23 17:17:35 -0700352 RegLocation rlArray, RegLocation rlIndex,
353 RegLocation rlDest, int scale)
Ben Chengba4fc8b2009-06-01 13:00:29 -0700354{
Bill Buzbee749e8162010-07-07 06:55:56 -0700355 RegisterClass regClass = dvmCompilerRegClassBySize(size);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700356 int lenOffset = offsetof(ArrayObject, length);
357 int dataOffset = offsetof(ArrayObject, contents);
Bill Buzbee1465db52009-09-23 17:17:35 -0700358 RegLocation rlResult;
359 rlArray = loadValue(cUnit, rlArray, kCoreReg);
360 rlIndex = loadValue(cUnit, rlIndex, kCoreReg);
361 int regPtr;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700362
363 /* null object? */
Ben Cheng4238ec22009-08-24 16:32:22 -0700364 ArmLIR * pcrLabel = NULL;
365
366 if (!(mir->OptimizationFlags & MIR_IGNORE_NULL_CHECK)) {
Bill Buzbee1465db52009-09-23 17:17:35 -0700367 pcrLabel = genNullCheck(cUnit, rlArray.sRegLow,
368 rlArray.lowReg, mir->offset, NULL);
Ben Cheng4238ec22009-08-24 16:32:22 -0700369 }
370
Bill Buzbeec6f10662010-02-09 11:16:15 -0800371 regPtr = dvmCompilerAllocTemp(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -0700372
Ben Cheng4238ec22009-08-24 16:32:22 -0700373 if (!(mir->OptimizationFlags & MIR_IGNORE_RANGE_CHECK)) {
Bill Buzbeec6f10662010-02-09 11:16:15 -0800374 int regLen = dvmCompilerAllocTemp(cUnit);
Ben Cheng4238ec22009-08-24 16:32:22 -0700375 /* Get len */
Bill Buzbee1465db52009-09-23 17:17:35 -0700376 loadWordDisp(cUnit, rlArray.lowReg, lenOffset, regLen);
377 /* regPtr -> array data */
378 opRegRegImm(cUnit, kOpAdd, regPtr, rlArray.lowReg, dataOffset);
379 genBoundsCheck(cUnit, rlIndex.lowReg, regLen, mir->offset,
380 pcrLabel);
Bill Buzbeec6f10662010-02-09 11:16:15 -0800381 dvmCompilerFreeTemp(cUnit, regLen);
Ben Cheng4238ec22009-08-24 16:32:22 -0700382 } else {
Bill Buzbee1465db52009-09-23 17:17:35 -0700383 /* regPtr -> array data */
384 opRegRegImm(cUnit, kOpAdd, regPtr, rlArray.lowReg, dataOffset);
Ben Cheng4238ec22009-08-24 16:32:22 -0700385 }
Bill Buzbee1465db52009-09-23 17:17:35 -0700386 if ((size == kLong) || (size == kDouble)) {
387 if (scale) {
Bill Buzbeec6f10662010-02-09 11:16:15 -0800388 int rNewIndex = dvmCompilerAllocTemp(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -0700389 opRegRegImm(cUnit, kOpLsl, rNewIndex, rlIndex.lowReg, scale);
390 opRegReg(cUnit, kOpAdd, regPtr, rNewIndex);
Bill Buzbeec6f10662010-02-09 11:16:15 -0800391 dvmCompilerFreeTemp(cUnit, rNewIndex);
Bill Buzbee1465db52009-09-23 17:17:35 -0700392 } else {
393 opRegReg(cUnit, kOpAdd, regPtr, rlIndex.lowReg);
394 }
Bill Buzbee749e8162010-07-07 06:55:56 -0700395 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, regClass, true);
Ben Cheng11d8f142010-03-24 15:24:19 -0700396
397 HEAP_ACCESS_SHADOW(true);
Bill Buzbee1465db52009-09-23 17:17:35 -0700398 loadPair(cUnit, regPtr, rlResult.lowReg, rlResult.highReg);
Ben Cheng11d8f142010-03-24 15:24:19 -0700399 HEAP_ACCESS_SHADOW(false);
400
Bill Buzbeec6f10662010-02-09 11:16:15 -0800401 dvmCompilerFreeTemp(cUnit, regPtr);
Bill Buzbee1465db52009-09-23 17:17:35 -0700402 storeValueWide(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700403 } else {
Bill Buzbee749e8162010-07-07 06:55:56 -0700404 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, regClass, true);
Ben Cheng11d8f142010-03-24 15:24:19 -0700405
406 HEAP_ACCESS_SHADOW(true);
Bill Buzbee1465db52009-09-23 17:17:35 -0700407 loadBaseIndexed(cUnit, regPtr, rlIndex.lowReg, rlResult.lowReg,
408 scale, size);
Ben Cheng11d8f142010-03-24 15:24:19 -0700409 HEAP_ACCESS_SHADOW(false);
410
Bill Buzbeec6f10662010-02-09 11:16:15 -0800411 dvmCompilerFreeTemp(cUnit, regPtr);
Bill Buzbee1465db52009-09-23 17:17:35 -0700412 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700413 }
414}
415
Ben Chengba4fc8b2009-06-01 13:00:29 -0700416/*
417 * Generate array store
418 *
Ben Chengba4fc8b2009-06-01 13:00:29 -0700419 */
Bill Buzbee270c1d62009-08-13 16:58:07 -0700420static void genArrayPut(CompilationUnit *cUnit, MIR *mir, OpSize size,
Bill Buzbee1465db52009-09-23 17:17:35 -0700421 RegLocation rlArray, RegLocation rlIndex,
422 RegLocation rlSrc, int scale)
Ben Chengba4fc8b2009-06-01 13:00:29 -0700423{
Bill Buzbee749e8162010-07-07 06:55:56 -0700424 RegisterClass regClass = dvmCompilerRegClassBySize(size);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700425 int lenOffset = offsetof(ArrayObject, length);
426 int dataOffset = offsetof(ArrayObject, contents);
427
Bill Buzbee1465db52009-09-23 17:17:35 -0700428 int regPtr;
429 rlArray = loadValue(cUnit, rlArray, kCoreReg);
430 rlIndex = loadValue(cUnit, rlIndex, kCoreReg);
Ben Chenge9695e52009-06-16 16:11:47 -0700431
Bill Buzbeec6f10662010-02-09 11:16:15 -0800432 if (dvmCompilerIsTemp(cUnit, rlArray.lowReg)) {
433 dvmCompilerClobber(cUnit, rlArray.lowReg);
Bill Buzbee1465db52009-09-23 17:17:35 -0700434 regPtr = rlArray.lowReg;
435 } else {
Bill Buzbeec6f10662010-02-09 11:16:15 -0800436 regPtr = dvmCompilerAllocTemp(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -0700437 genRegCopy(cUnit, regPtr, rlArray.lowReg);
438 }
Ben Chenge9695e52009-06-16 16:11:47 -0700439
Ben Cheng1efc9c52009-06-08 18:25:27 -0700440 /* null object? */
Ben Cheng4238ec22009-08-24 16:32:22 -0700441 ArmLIR * pcrLabel = NULL;
442
443 if (!(mir->OptimizationFlags & MIR_IGNORE_NULL_CHECK)) {
Bill Buzbee1465db52009-09-23 17:17:35 -0700444 pcrLabel = genNullCheck(cUnit, rlArray.sRegLow, rlArray.lowReg,
445 mir->offset, NULL);
Ben Cheng4238ec22009-08-24 16:32:22 -0700446 }
447
448 if (!(mir->OptimizationFlags & MIR_IGNORE_RANGE_CHECK)) {
Bill Buzbeec6f10662010-02-09 11:16:15 -0800449 int regLen = dvmCompilerAllocTemp(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -0700450 //NOTE: max live temps(4) here.
Ben Cheng4238ec22009-08-24 16:32:22 -0700451 /* Get len */
Bill Buzbee1465db52009-09-23 17:17:35 -0700452 loadWordDisp(cUnit, rlArray.lowReg, lenOffset, regLen);
453 /* regPtr -> array data */
454 opRegImm(cUnit, kOpAdd, regPtr, dataOffset);
455 genBoundsCheck(cUnit, rlIndex.lowReg, regLen, mir->offset,
456 pcrLabel);
Bill Buzbeec6f10662010-02-09 11:16:15 -0800457 dvmCompilerFreeTemp(cUnit, regLen);
Ben Cheng4238ec22009-08-24 16:32:22 -0700458 } else {
Bill Buzbee1465db52009-09-23 17:17:35 -0700459 /* regPtr -> array data */
460 opRegImm(cUnit, kOpAdd, regPtr, dataOffset);
Ben Cheng4238ec22009-08-24 16:32:22 -0700461 }
Bill Buzbee1465db52009-09-23 17:17:35 -0700462 /* at this point, regPtr points to array, 2 live temps */
Bill Buzbee1465db52009-09-23 17:17:35 -0700463 if ((size == kLong) || (size == kDouble)) {
464 //TODO: need specific wide routine that can handle fp regs
465 if (scale) {
Bill Buzbeec6f10662010-02-09 11:16:15 -0800466 int rNewIndex = dvmCompilerAllocTemp(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -0700467 opRegRegImm(cUnit, kOpLsl, rNewIndex, rlIndex.lowReg, scale);
468 opRegReg(cUnit, kOpAdd, regPtr, rNewIndex);
Bill Buzbeec6f10662010-02-09 11:16:15 -0800469 dvmCompilerFreeTemp(cUnit, rNewIndex);
Bill Buzbee1465db52009-09-23 17:17:35 -0700470 } else {
471 opRegReg(cUnit, kOpAdd, regPtr, rlIndex.lowReg);
472 }
Bill Buzbee749e8162010-07-07 06:55:56 -0700473 rlSrc = loadValueWide(cUnit, rlSrc, regClass);
Ben Cheng11d8f142010-03-24 15:24:19 -0700474
475 HEAP_ACCESS_SHADOW(true);
Bill Buzbee1465db52009-09-23 17:17:35 -0700476 storePair(cUnit, regPtr, rlSrc.lowReg, rlSrc.highReg);
Ben Cheng11d8f142010-03-24 15:24:19 -0700477 HEAP_ACCESS_SHADOW(false);
478
Bill Buzbeec6f10662010-02-09 11:16:15 -0800479 dvmCompilerFreeTemp(cUnit, regPtr);
Bill Buzbee270c1d62009-08-13 16:58:07 -0700480 } else {
Bill Buzbee749e8162010-07-07 06:55:56 -0700481 rlSrc = loadValue(cUnit, rlSrc, regClass);
Ben Cheng11d8f142010-03-24 15:24:19 -0700482
483 HEAP_ACCESS_SHADOW(true);
Bill Buzbee1465db52009-09-23 17:17:35 -0700484 storeBaseIndexed(cUnit, regPtr, rlIndex.lowReg, rlSrc.lowReg,
485 scale, size);
Ben Cheng11d8f142010-03-24 15:24:19 -0700486 HEAP_ACCESS_SHADOW(false);
jeffhao9e45c0b2010-02-03 10:24:05 -0800487 }
Ben Chengba4fc8b2009-06-01 13:00:29 -0700488}
489
Bill Buzbeebe6534f2010-03-12 16:01:35 -0800490/*
491 * Generate array object store
492 * Must use explicit register allocation here because of
493 * call-out to dvmCanPutArrayElement
494 */
495static void genArrayObjectPut(CompilationUnit *cUnit, MIR *mir,
496 RegLocation rlArray, RegLocation rlIndex,
497 RegLocation rlSrc, int scale)
498{
499 int lenOffset = offsetof(ArrayObject, length);
500 int dataOffset = offsetof(ArrayObject, contents);
501
502 dvmCompilerFlushAllRegs(cUnit);
503
504 int regLen = r0;
505 int regPtr = r4PC; /* Preserved across call */
506 int regArray = r1;
507 int regIndex = r7; /* Preserved across call */
508
509 loadValueDirectFixed(cUnit, rlArray, regArray);
510 loadValueDirectFixed(cUnit, rlIndex, regIndex);
511
512 /* null object? */
513 ArmLIR * pcrLabel = NULL;
514
515 if (!(mir->OptimizationFlags & MIR_IGNORE_NULL_CHECK)) {
516 pcrLabel = genNullCheck(cUnit, rlArray.sRegLow, regArray,
517 mir->offset, NULL);
518 }
519
520 if (!(mir->OptimizationFlags & MIR_IGNORE_RANGE_CHECK)) {
521 /* Get len */
522 loadWordDisp(cUnit, regArray, lenOffset, regLen);
523 /* regPtr -> array data */
524 opRegRegImm(cUnit, kOpAdd, regPtr, regArray, dataOffset);
525 genBoundsCheck(cUnit, regIndex, regLen, mir->offset,
526 pcrLabel);
527 } else {
528 /* regPtr -> array data */
529 opRegRegImm(cUnit, kOpAdd, regPtr, regArray, dataOffset);
530 }
531
532 /* Get object to store */
533 loadValueDirectFixed(cUnit, rlSrc, r0);
Ben Chengbd1326d2010-04-02 15:04:53 -0700534 LOAD_FUNC_ADDR(cUnit, r2, (int)dvmCanPutArrayElement);
Bill Buzbeebe6534f2010-03-12 16:01:35 -0800535
536 /* Are we storing null? If so, avoid check */
537 opRegImm(cUnit, kOpCmp, r0, 0);
538 ArmLIR *branchOver = opCondBranch(cUnit, kArmCondEq);
539
540 /* Make sure the types are compatible */
541 loadWordDisp(cUnit, regArray, offsetof(Object, clazz), r1);
542 loadWordDisp(cUnit, r0, offsetof(Object, clazz), r0);
543 opReg(cUnit, kOpBlx, r2);
544 dvmCompilerClobberCallRegs(cUnit);
Bill Buzbee900a3af2010-03-16 12:41:43 -0700545
546 /*
547 * Using fixed registers here, and counting on r4 and r7 being
548 * preserved across the above call. Tell the register allocation
549 * utilities about the regs we are using directly
550 */
551 dvmCompilerLockTemp(cUnit, regPtr); // r4PC
552 dvmCompilerLockTemp(cUnit, regIndex); // r7
553 dvmCompilerLockTemp(cUnit, r0);
buzbee919eb062010-07-12 12:59:22 -0700554 dvmCompilerLockTemp(cUnit, r1);
Bill Buzbee900a3af2010-03-16 12:41:43 -0700555
Bill Buzbeebe6534f2010-03-12 16:01:35 -0800556 /* Bad? - roll back and re-execute if so */
557 genRegImmCheck(cUnit, kArmCondEq, r0, 0, mir->offset, pcrLabel);
558
buzbee919eb062010-07-12 12:59:22 -0700559 /* Resume here - must reload element & array, regPtr & index preserved */
Bill Buzbeebe6534f2010-03-12 16:01:35 -0800560 loadValueDirectFixed(cUnit, rlSrc, r0);
buzbee919eb062010-07-12 12:59:22 -0700561 loadValueDirectFixed(cUnit, rlArray, r1);
Bill Buzbeebe6534f2010-03-12 16:01:35 -0800562
563 ArmLIR *target = newLIR0(cUnit, kArmPseudoTargetLabel);
564 target->defMask = ENCODE_ALL;
565 branchOver->generic.target = (LIR *) target;
566
Ben Cheng11d8f142010-03-24 15:24:19 -0700567 HEAP_ACCESS_SHADOW(true);
Bill Buzbeebe6534f2010-03-12 16:01:35 -0800568 storeBaseIndexed(cUnit, regPtr, regIndex, r0,
569 scale, kWord);
Ben Cheng11d8f142010-03-24 15:24:19 -0700570 HEAP_ACCESS_SHADOW(false);
buzbee919eb062010-07-12 12:59:22 -0700571
572 /* NOTE: marking card here based on object head */
573 markCard(cUnit, r0, r1);
Bill Buzbeebe6534f2010-03-12 16:01:35 -0800574}
575
Ben Cheng5d90c202009-11-22 23:31:11 -0800576static bool genShiftOpLong(CompilationUnit *cUnit, MIR *mir,
577 RegLocation rlDest, RegLocation rlSrc1,
578 RegLocation rlShift)
Ben Chengba4fc8b2009-06-01 13:00:29 -0700579{
Ben Chenge9695e52009-06-16 16:11:47 -0700580 /*
581 * Don't mess with the regsiters here as there is a particular calling
582 * convention to the out-of-line handler.
583 */
Bill Buzbee1465db52009-09-23 17:17:35 -0700584 RegLocation rlResult;
585
586 loadValueDirectWideFixed(cUnit, rlSrc1, r0, r1);
587 loadValueDirect(cUnit, rlShift, r2);
Ben Chenge9695e52009-06-16 16:11:47 -0700588 switch( mir->dalvikInsn.opCode) {
589 case OP_SHL_LONG:
590 case OP_SHL_LONG_2ADDR:
591 genDispatchToHandler(cUnit, TEMPLATE_SHL_LONG);
592 break;
593 case OP_SHR_LONG:
594 case OP_SHR_LONG_2ADDR:
595 genDispatchToHandler(cUnit, TEMPLATE_SHR_LONG);
596 break;
597 case OP_USHR_LONG:
598 case OP_USHR_LONG_2ADDR:
599 genDispatchToHandler(cUnit, TEMPLATE_USHR_LONG);
600 break;
601 default:
602 return true;
603 }
Bill Buzbeec6f10662010-02-09 11:16:15 -0800604 rlResult = dvmCompilerGetReturnWide(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -0700605 storeValueWide(cUnit, rlDest, rlResult);
Ben Chenge9695e52009-06-16 16:11:47 -0700606 return false;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700607}
Ben Chenge9695e52009-06-16 16:11:47 -0700608
Ben Cheng5d90c202009-11-22 23:31:11 -0800609static bool genArithOpLong(CompilationUnit *cUnit, MIR *mir,
610 RegLocation rlDest, RegLocation rlSrc1,
611 RegLocation rlSrc2)
Ben Chengba4fc8b2009-06-01 13:00:29 -0700612{
Bill Buzbee1465db52009-09-23 17:17:35 -0700613 RegLocation rlResult;
614 OpKind firstOp = kOpBkpt;
615 OpKind secondOp = kOpBkpt;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700616 bool callOut = false;
617 void *callTgt;
618 int retReg = r0;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700619
620 switch (mir->dalvikInsn.opCode) {
621 case OP_NOT_LONG:
Bill Buzbee1465db52009-09-23 17:17:35 -0700622 rlSrc2 = loadValueWide(cUnit, rlSrc2, kCoreReg);
Bill Buzbeec6f10662010-02-09 11:16:15 -0800623 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Bill Buzbee1465db52009-09-23 17:17:35 -0700624 opRegReg(cUnit, kOpMvn, rlResult.lowReg, rlSrc2.lowReg);
625 opRegReg(cUnit, kOpMvn, rlResult.highReg, rlSrc2.highReg);
626 storeValueWide(cUnit, rlDest, rlResult);
627 return false;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700628 break;
629 case OP_ADD_LONG:
630 case OP_ADD_LONG_2ADDR:
Bill Buzbee1465db52009-09-23 17:17:35 -0700631 firstOp = kOpAdd;
632 secondOp = kOpAdc;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700633 break;
634 case OP_SUB_LONG:
635 case OP_SUB_LONG_2ADDR:
Bill Buzbee1465db52009-09-23 17:17:35 -0700636 firstOp = kOpSub;
637 secondOp = kOpSbc;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700638 break;
639 case OP_MUL_LONG:
640 case OP_MUL_LONG_2ADDR:
Bill Buzbee1465db52009-09-23 17:17:35 -0700641 genMulLong(cUnit, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700642 return false;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700643 case OP_DIV_LONG:
644 case OP_DIV_LONG_2ADDR:
645 callOut = true;
646 retReg = r0;
647 callTgt = (void*)__aeabi_ldivmod;
648 break;
649 /* NOTE - result is in r2/r3 instead of r0/r1 */
650 case OP_REM_LONG:
651 case OP_REM_LONG_2ADDR:
652 callOut = true;
653 callTgt = (void*)__aeabi_ldivmod;
654 retReg = r2;
655 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700656 case OP_AND_LONG_2ADDR:
Bill Buzbee1465db52009-09-23 17:17:35 -0700657 case OP_AND_LONG:
658 firstOp = kOpAnd;
659 secondOp = kOpAnd;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700660 break;
661 case OP_OR_LONG:
662 case OP_OR_LONG_2ADDR:
Bill Buzbee1465db52009-09-23 17:17:35 -0700663 firstOp = kOpOr;
664 secondOp = kOpOr;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700665 break;
666 case OP_XOR_LONG:
667 case OP_XOR_LONG_2ADDR:
Bill Buzbee1465db52009-09-23 17:17:35 -0700668 firstOp = kOpXor;
669 secondOp = kOpXor;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700670 break;
Ben Chenge9695e52009-06-16 16:11:47 -0700671 case OP_NEG_LONG: {
Bill Buzbee51ecf602010-01-14 14:27:52 -0800672 //TUNING: can improve this using Thumb2 code
Bill Buzbeec6f10662010-02-09 11:16:15 -0800673 int tReg = dvmCompilerAllocTemp(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -0700674 rlSrc2 = loadValueWide(cUnit, rlSrc2, kCoreReg);
Bill Buzbeec6f10662010-02-09 11:16:15 -0800675 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Ben Chengbd1326d2010-04-02 15:04:53 -0700676 loadConstantNoClobber(cUnit, tReg, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -0700677 opRegRegReg(cUnit, kOpSub, rlResult.lowReg,
Bill Buzbee51ecf602010-01-14 14:27:52 -0800678 tReg, rlSrc2.lowReg);
679 opRegReg(cUnit, kOpSbc, tReg, rlSrc2.highReg);
680 genRegCopy(cUnit, rlResult.highReg, tReg);
Bill Buzbee1465db52009-09-23 17:17:35 -0700681 storeValueWide(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700682 return false;
Ben Chenge9695e52009-06-16 16:11:47 -0700683 }
Ben Chengba4fc8b2009-06-01 13:00:29 -0700684 default:
685 LOGE("Invalid long arith op");
Bill Buzbeefc519dc2010-03-06 23:30:57 -0800686 dvmCompilerAbort(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700687 }
688 if (!callOut) {
Bill Buzbee80cef862010-03-25 10:38:34 -0700689 genLong3Addr(cUnit, mir, firstOp, secondOp, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700690 } else {
Bill Buzbee1465db52009-09-23 17:17:35 -0700691 // Adjust return regs in to handle case of rem returning r2/r3
Bill Buzbeec6f10662010-02-09 11:16:15 -0800692 dvmCompilerFlushAllRegs(cUnit); /* Send everything to home location */
Bill Buzbee1465db52009-09-23 17:17:35 -0700693 loadValueDirectWideFixed(cUnit, rlSrc1, r0, r1);
Ben Chengbd1326d2010-04-02 15:04:53 -0700694 LOAD_FUNC_ADDR(cUnit, rlr, (int) callTgt);
Bill Buzbee1465db52009-09-23 17:17:35 -0700695 loadValueDirectWideFixed(cUnit, rlSrc2, r2, r3);
696 opReg(cUnit, kOpBlx, rlr);
Elliott Hughes6a555132010-02-25 15:41:42 -0800697 dvmCompilerClobberCallRegs(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -0700698 if (retReg == r0)
Bill Buzbeec6f10662010-02-09 11:16:15 -0800699 rlResult = dvmCompilerGetReturnWide(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -0700700 else
Bill Buzbeec6f10662010-02-09 11:16:15 -0800701 rlResult = dvmCompilerGetReturnWideAlt(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -0700702 storeValueWide(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700703 }
704 return false;
705}
706
Ben Cheng5d90c202009-11-22 23:31:11 -0800707static bool genArithOpInt(CompilationUnit *cUnit, MIR *mir,
708 RegLocation rlDest, RegLocation rlSrc1,
709 RegLocation rlSrc2)
Ben Chengba4fc8b2009-06-01 13:00:29 -0700710{
Bill Buzbee1465db52009-09-23 17:17:35 -0700711 OpKind op = kOpBkpt;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700712 bool callOut = false;
713 bool checkZero = false;
Bill Buzbee1465db52009-09-23 17:17:35 -0700714 bool unary = false;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700715 int retReg = r0;
716 void *callTgt;
Bill Buzbee1465db52009-09-23 17:17:35 -0700717 RegLocation rlResult;
Bill Buzbee0e605272009-12-01 14:28:05 -0800718 bool shiftOp = false;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700719
Ben Chengba4fc8b2009-06-01 13:00:29 -0700720 switch (mir->dalvikInsn.opCode) {
721 case OP_NEG_INT:
Bill Buzbee1465db52009-09-23 17:17:35 -0700722 op = kOpNeg;
723 unary = true;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700724 break;
725 case OP_NOT_INT:
Bill Buzbee1465db52009-09-23 17:17:35 -0700726 op = kOpMvn;
727 unary = true;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700728 break;
729 case OP_ADD_INT:
730 case OP_ADD_INT_2ADDR:
Bill Buzbee1465db52009-09-23 17:17:35 -0700731 op = kOpAdd;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700732 break;
733 case OP_SUB_INT:
734 case OP_SUB_INT_2ADDR:
Bill Buzbee1465db52009-09-23 17:17:35 -0700735 op = kOpSub;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700736 break;
737 case OP_MUL_INT:
738 case OP_MUL_INT_2ADDR:
Bill Buzbee1465db52009-09-23 17:17:35 -0700739 op = kOpMul;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700740 break;
741 case OP_DIV_INT:
742 case OP_DIV_INT_2ADDR:
743 callOut = true;
744 checkZero = true;
745 callTgt = __aeabi_idiv;
746 retReg = r0;
747 break;
748 /* NOTE: returns in r1 */
749 case OP_REM_INT:
750 case OP_REM_INT_2ADDR:
751 callOut = true;
752 checkZero = true;
753 callTgt = __aeabi_idivmod;
754 retReg = r1;
755 break;
756 case OP_AND_INT:
757 case OP_AND_INT_2ADDR:
Bill Buzbee1465db52009-09-23 17:17:35 -0700758 op = kOpAnd;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700759 break;
760 case OP_OR_INT:
761 case OP_OR_INT_2ADDR:
Bill Buzbee1465db52009-09-23 17:17:35 -0700762 op = kOpOr;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700763 break;
764 case OP_XOR_INT:
765 case OP_XOR_INT_2ADDR:
Bill Buzbee1465db52009-09-23 17:17:35 -0700766 op = kOpXor;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700767 break;
768 case OP_SHL_INT:
769 case OP_SHL_INT_2ADDR:
Bill Buzbee0e605272009-12-01 14:28:05 -0800770 shiftOp = true;
Bill Buzbee1465db52009-09-23 17:17:35 -0700771 op = kOpLsl;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700772 break;
773 case OP_SHR_INT:
774 case OP_SHR_INT_2ADDR:
Bill Buzbee0e605272009-12-01 14:28:05 -0800775 shiftOp = true;
Bill Buzbee1465db52009-09-23 17:17:35 -0700776 op = kOpAsr;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700777 break;
778 case OP_USHR_INT:
779 case OP_USHR_INT_2ADDR:
Bill Buzbee0e605272009-12-01 14:28:05 -0800780 shiftOp = true;
Bill Buzbee1465db52009-09-23 17:17:35 -0700781 op = kOpLsr;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700782 break;
783 default:
784 LOGE("Invalid word arith op: 0x%x(%d)",
785 mir->dalvikInsn.opCode, mir->dalvikInsn.opCode);
Bill Buzbeefc519dc2010-03-06 23:30:57 -0800786 dvmCompilerAbort(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700787 }
788 if (!callOut) {
Bill Buzbee1465db52009-09-23 17:17:35 -0700789 rlSrc1 = loadValue(cUnit, rlSrc1, kCoreReg);
790 if (unary) {
Bill Buzbeec6f10662010-02-09 11:16:15 -0800791 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Bill Buzbee1465db52009-09-23 17:17:35 -0700792 opRegReg(cUnit, op, rlResult.lowReg,
793 rlSrc1.lowReg);
Ben Chenge9695e52009-06-16 16:11:47 -0700794 } else {
Bill Buzbee1465db52009-09-23 17:17:35 -0700795 rlSrc2 = loadValue(cUnit, rlSrc2, kCoreReg);
Bill Buzbee0e605272009-12-01 14:28:05 -0800796 if (shiftOp) {
Bill Buzbeec6f10662010-02-09 11:16:15 -0800797 int tReg = dvmCompilerAllocTemp(cUnit);
Bill Buzbee0e605272009-12-01 14:28:05 -0800798 opRegRegImm(cUnit, kOpAnd, tReg, rlSrc2.lowReg, 31);
Bill Buzbeec6f10662010-02-09 11:16:15 -0800799 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Bill Buzbee0e605272009-12-01 14:28:05 -0800800 opRegRegReg(cUnit, op, rlResult.lowReg,
801 rlSrc1.lowReg, tReg);
Bill Buzbeec6f10662010-02-09 11:16:15 -0800802 dvmCompilerFreeTemp(cUnit, tReg);
Bill Buzbee0e605272009-12-01 14:28:05 -0800803 } else {
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, rlSrc2.lowReg);
807 }
Ben Chenge9695e52009-06-16 16:11:47 -0700808 }
Bill Buzbee1465db52009-09-23 17:17:35 -0700809 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700810 } else {
Bill Buzbee1465db52009-09-23 17:17:35 -0700811 RegLocation rlResult;
Bill Buzbeec6f10662010-02-09 11:16:15 -0800812 dvmCompilerFlushAllRegs(cUnit); /* Send everything to home location */
Bill Buzbee1465db52009-09-23 17:17:35 -0700813 loadValueDirectFixed(cUnit, rlSrc2, r1);
Ben Chengbd1326d2010-04-02 15:04:53 -0700814 LOAD_FUNC_ADDR(cUnit, r2, (int) callTgt);
Bill Buzbee1465db52009-09-23 17:17:35 -0700815 loadValueDirectFixed(cUnit, rlSrc1, r0);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700816 if (checkZero) {
Bill Buzbee1465db52009-09-23 17:17:35 -0700817 genNullCheck(cUnit, rlSrc2.sRegLow, r1, mir->offset, NULL);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700818 }
Bill Buzbee1465db52009-09-23 17:17:35 -0700819 opReg(cUnit, kOpBlx, r2);
Elliott Hughes6a555132010-02-25 15:41:42 -0800820 dvmCompilerClobberCallRegs(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -0700821 if (retReg == r0)
Bill Buzbeec6f10662010-02-09 11:16:15 -0800822 rlResult = dvmCompilerGetReturn(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -0700823 else
Bill Buzbeec6f10662010-02-09 11:16:15 -0800824 rlResult = dvmCompilerGetReturnAlt(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -0700825 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700826 }
827 return false;
828}
829
Ben Cheng5d90c202009-11-22 23:31:11 -0800830static bool genArithOp(CompilationUnit *cUnit, MIR *mir)
Ben Chengba4fc8b2009-06-01 13:00:29 -0700831{
832 OpCode opCode = mir->dalvikInsn.opCode;
Bill Buzbee1465db52009-09-23 17:17:35 -0700833 RegLocation rlDest;
834 RegLocation rlSrc1;
835 RegLocation rlSrc2;
836 /* Deduce sizes of operands */
837 if (mir->ssaRep->numUses == 2) {
Bill Buzbeec6f10662010-02-09 11:16:15 -0800838 rlSrc1 = dvmCompilerGetSrc(cUnit, mir, 0);
839 rlSrc2 = dvmCompilerGetSrc(cUnit, mir, 1);
Bill Buzbee1465db52009-09-23 17:17:35 -0700840 } else if (mir->ssaRep->numUses == 3) {
Bill Buzbeec6f10662010-02-09 11:16:15 -0800841 rlSrc1 = dvmCompilerGetSrcWide(cUnit, mir, 0, 1);
842 rlSrc2 = dvmCompilerGetSrc(cUnit, mir, 2);
Bill Buzbee1465db52009-09-23 17:17:35 -0700843 } else {
Bill Buzbeec6f10662010-02-09 11:16:15 -0800844 rlSrc1 = dvmCompilerGetSrcWide(cUnit, mir, 0, 1);
845 rlSrc2 = dvmCompilerGetSrcWide(cUnit, mir, 2, 3);
Bill Buzbee1465db52009-09-23 17:17:35 -0700846 assert(mir->ssaRep->numUses == 4);
847 }
848 if (mir->ssaRep->numDefs == 1) {
Bill Buzbeec6f10662010-02-09 11:16:15 -0800849 rlDest = dvmCompilerGetDest(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -0700850 } else {
851 assert(mir->ssaRep->numDefs == 2);
Bill Buzbeec6f10662010-02-09 11:16:15 -0800852 rlDest = dvmCompilerGetDestWide(cUnit, mir, 0, 1);
Bill Buzbee1465db52009-09-23 17:17:35 -0700853 }
Ben Chengba4fc8b2009-06-01 13:00:29 -0700854
855 if ((opCode >= OP_ADD_LONG_2ADDR) && (opCode <= OP_XOR_LONG_2ADDR)) {
Ben Cheng5d90c202009-11-22 23:31:11 -0800856 return genArithOpLong(cUnit,mir, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700857 }
858 if ((opCode >= OP_ADD_LONG) && (opCode <= OP_XOR_LONG)) {
Ben Cheng5d90c202009-11-22 23:31:11 -0800859 return genArithOpLong(cUnit,mir, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700860 }
861 if ((opCode >= OP_SHL_LONG_2ADDR) && (opCode <= OP_USHR_LONG_2ADDR)) {
Ben Cheng5d90c202009-11-22 23:31:11 -0800862 return genShiftOpLong(cUnit,mir, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700863 }
864 if ((opCode >= OP_SHL_LONG) && (opCode <= OP_USHR_LONG)) {
Ben Cheng5d90c202009-11-22 23:31:11 -0800865 return genShiftOpLong(cUnit,mir, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700866 }
867 if ((opCode >= OP_ADD_INT_2ADDR) && (opCode <= OP_USHR_INT_2ADDR)) {
Ben Cheng5d90c202009-11-22 23:31:11 -0800868 return genArithOpInt(cUnit,mir, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700869 }
870 if ((opCode >= OP_ADD_INT) && (opCode <= OP_USHR_INT)) {
Ben Cheng5d90c202009-11-22 23:31:11 -0800871 return genArithOpInt(cUnit,mir, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700872 }
873 if ((opCode >= OP_ADD_FLOAT_2ADDR) && (opCode <= OP_REM_FLOAT_2ADDR)) {
Ben Cheng5d90c202009-11-22 23:31:11 -0800874 return genArithOpFloat(cUnit,mir, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700875 }
876 if ((opCode >= OP_ADD_FLOAT) && (opCode <= OP_REM_FLOAT)) {
Ben Cheng5d90c202009-11-22 23:31:11 -0800877 return genArithOpFloat(cUnit, mir, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700878 }
879 if ((opCode >= OP_ADD_DOUBLE_2ADDR) && (opCode <= OP_REM_DOUBLE_2ADDR)) {
Ben Cheng5d90c202009-11-22 23:31:11 -0800880 return genArithOpDouble(cUnit,mir, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700881 }
882 if ((opCode >= OP_ADD_DOUBLE) && (opCode <= OP_REM_DOUBLE)) {
Ben Cheng5d90c202009-11-22 23:31:11 -0800883 return genArithOpDouble(cUnit,mir, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700884 }
885 return true;
886}
887
Bill Buzbee1465db52009-09-23 17:17:35 -0700888/* Generate unconditional branch instructions */
889static ArmLIR *genUnconditionalBranch(CompilationUnit *cUnit, ArmLIR *target)
890{
891 ArmLIR *branch = opNone(cUnit, kOpUncondBr);
892 branch->generic.target = (LIR *) target;
893 return branch;
894}
895
Bill Buzbee1465db52009-09-23 17:17:35 -0700896/* Perform the actual operation for OP_RETURN_* */
897static void genReturnCommon(CompilationUnit *cUnit, MIR *mir)
898{
899 genDispatchToHandler(cUnit, TEMPLATE_RETURN);
Ben Cheng978738d2010-05-13 13:45:57 -0700900#if defined(WITH_JIT_TUNING)
Bill Buzbee1465db52009-09-23 17:17:35 -0700901 gDvmJit.returnOp++;
902#endif
903 int dPC = (int) (cUnit->method->insns + mir->offset);
904 /* Insert branch, but defer setting of target */
905 ArmLIR *branch = genUnconditionalBranch(cUnit, NULL);
906 /* Set up the place holder to reconstruct this Dalvik PC */
907 ArmLIR *pcrLabel = dvmCompilerNew(sizeof(ArmLIR), true);
Ben Chenga4973592010-03-31 11:59:18 -0700908 pcrLabel->opCode = kArmPseudoPCReconstructionCell;
Bill Buzbee1465db52009-09-23 17:17:35 -0700909 pcrLabel->operands[0] = dPC;
910 pcrLabel->operands[1] = mir->offset;
911 /* Insert the place holder to the growable list */
912 dvmInsertGrowableList(&cUnit->pcReconstructionList, pcrLabel);
913 /* Branch to the PC reconstruction code */
914 branch->generic.target = (LIR *) pcrLabel;
915}
916
Ben Chengba4fc8b2009-06-01 13:00:29 -0700917static void genProcessArgsNoRange(CompilationUnit *cUnit, MIR *mir,
918 DecodedInstruction *dInsn,
Bill Buzbee89efc3d2009-07-28 11:22:22 -0700919 ArmLIR **pcrLabel)
Ben Chengba4fc8b2009-06-01 13:00:29 -0700920{
921 unsigned int i;
922 unsigned int regMask = 0;
Bill Buzbee1465db52009-09-23 17:17:35 -0700923 RegLocation rlArg;
924 int numDone = 0;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700925
Bill Buzbee1465db52009-09-23 17:17:35 -0700926 /*
927 * Load arguments to r0..r4. Note that these registers may contain
928 * live values, so we clobber them immediately after loading to prevent
929 * them from being used as sources for subsequent loads.
930 */
Bill Buzbeec6f10662010-02-09 11:16:15 -0800931 dvmCompilerLockAllTemps(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700932 for (i = 0; i < dInsn->vA; i++) {
933 regMask |= 1 << i;
Bill Buzbeec6f10662010-02-09 11:16:15 -0800934 rlArg = dvmCompilerGetSrc(cUnit, mir, numDone++);
Bill Buzbee1465db52009-09-23 17:17:35 -0700935 loadValueDirectFixed(cUnit, rlArg, i);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700936 }
937 if (regMask) {
938 /* Up to 5 args are pushed on top of FP - sizeofStackSaveArea */
Bill Buzbee1465db52009-09-23 17:17:35 -0700939 opRegRegImm(cUnit, kOpSub, r7, rFP,
940 sizeof(StackSaveArea) + (dInsn->vA << 2));
Ben Chengba4fc8b2009-06-01 13:00:29 -0700941 /* generate null check */
942 if (pcrLabel) {
Bill Buzbeec6f10662010-02-09 11:16:15 -0800943 *pcrLabel = genNullCheck(cUnit, dvmCompilerSSASrc(mir, 0), r0,
Bill Buzbee1465db52009-09-23 17:17:35 -0700944 mir->offset, NULL);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700945 }
Bill Buzbee270c1d62009-08-13 16:58:07 -0700946 storeMultiple(cUnit, r7, regMask);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700947 }
948}
949
950static void genProcessArgsRange(CompilationUnit *cUnit, MIR *mir,
951 DecodedInstruction *dInsn,
Bill Buzbee89efc3d2009-07-28 11:22:22 -0700952 ArmLIR **pcrLabel)
Ben Chengba4fc8b2009-06-01 13:00:29 -0700953{
954 int srcOffset = dInsn->vC << 2;
955 int numArgs = dInsn->vA;
956 int regMask;
Bill Buzbee1465db52009-09-23 17:17:35 -0700957
958 /*
959 * Note: here, all promoted registers will have been flushed
960 * back to the Dalvik base locations, so register usage restrictins
961 * are lifted. All parms loaded from original Dalvik register
962 * region - even though some might conceivably have valid copies
963 * cached in a preserved register.
964 */
Bill Buzbeec6f10662010-02-09 11:16:15 -0800965 dvmCompilerLockAllTemps(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -0700966
Ben Chengba4fc8b2009-06-01 13:00:29 -0700967 /*
968 * r4PC : &rFP[vC]
969 * r7: &newFP[0]
970 */
Bill Buzbee1465db52009-09-23 17:17:35 -0700971 opRegRegImm(cUnit, kOpAdd, r4PC, rFP, srcOffset);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700972 /* load [r0 .. min(numArgs,4)] */
973 regMask = (1 << ((numArgs < 4) ? numArgs : 4)) - 1;
Ben Chengd7d426a2009-09-22 11:23:36 -0700974 /*
975 * Protect the loadMultiple instruction from being reordered with other
976 * Dalvik stack accesses.
977 */
Bill Buzbee270c1d62009-08-13 16:58:07 -0700978 loadMultiple(cUnit, r4PC, regMask);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700979
Bill Buzbee1465db52009-09-23 17:17:35 -0700980 opRegRegImm(cUnit, kOpSub, r7, rFP,
981 sizeof(StackSaveArea) + (numArgs << 2));
Ben Chengba4fc8b2009-06-01 13:00:29 -0700982 /* generate null check */
983 if (pcrLabel) {
Bill Buzbeec6f10662010-02-09 11:16:15 -0800984 *pcrLabel = genNullCheck(cUnit, dvmCompilerSSASrc(mir, 0), r0,
Bill Buzbee1465db52009-09-23 17:17:35 -0700985 mir->offset, NULL);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700986 }
987
988 /*
989 * Handle remaining 4n arguments:
990 * store previously loaded 4 values and load the next 4 values
991 */
992 if (numArgs >= 8) {
Bill Buzbee89efc3d2009-07-28 11:22:22 -0700993 ArmLIR *loopLabel = NULL;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700994 /*
995 * r0 contains "this" and it will be used later, so push it to the stack
Bill Buzbee270c1d62009-08-13 16:58:07 -0700996 * first. Pushing r5 (rFP) is just for stack alignment purposes.
Ben Chengba4fc8b2009-06-01 13:00:29 -0700997 */
Bill Buzbee1465db52009-09-23 17:17:35 -0700998 opImm(cUnit, kOpPush, (1 << r0 | 1 << rFP));
Ben Chengba4fc8b2009-06-01 13:00:29 -0700999 /* No need to generate the loop structure if numArgs <= 11 */
1000 if (numArgs > 11) {
1001 loadConstant(cUnit, 5, ((numArgs - 4) >> 2) << 2);
Bill Buzbee1465db52009-09-23 17:17:35 -07001002 loopLabel = newLIR0(cUnit, kArmPseudoTargetLabel);
Ben Chengd7d426a2009-09-22 11:23:36 -07001003 loopLabel->defMask = ENCODE_ALL;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001004 }
Bill Buzbee270c1d62009-08-13 16:58:07 -07001005 storeMultiple(cUnit, r7, regMask);
Ben Chengd7d426a2009-09-22 11:23:36 -07001006 /*
1007 * Protect the loadMultiple instruction from being reordered with other
1008 * Dalvik stack accesses.
1009 */
Bill Buzbee270c1d62009-08-13 16:58:07 -07001010 loadMultiple(cUnit, r4PC, regMask);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001011 /* No need to generate the loop structure if numArgs <= 11 */
1012 if (numArgs > 11) {
Bill Buzbee1465db52009-09-23 17:17:35 -07001013 opRegImm(cUnit, kOpSub, rFP, 4);
1014 genConditionalBranch(cUnit, kArmCondNe, loopLabel);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001015 }
1016 }
1017
1018 /* Save the last batch of loaded values */
Bill Buzbee270c1d62009-08-13 16:58:07 -07001019 storeMultiple(cUnit, r7, regMask);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001020
1021 /* Generate the loop epilogue - don't use r0 */
1022 if ((numArgs > 4) && (numArgs % 4)) {
1023 regMask = ((1 << (numArgs & 0x3)) - 1) << 1;
Ben Chengd7d426a2009-09-22 11:23:36 -07001024 /*
1025 * Protect the loadMultiple instruction from being reordered with other
1026 * Dalvik stack accesses.
1027 */
Bill Buzbee270c1d62009-08-13 16:58:07 -07001028 loadMultiple(cUnit, r4PC, regMask);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001029 }
1030 if (numArgs >= 8)
Bill Buzbee1465db52009-09-23 17:17:35 -07001031 opImm(cUnit, kOpPop, (1 << r0 | 1 << rFP));
Ben Chengba4fc8b2009-06-01 13:00:29 -07001032
1033 /* Save the modulo 4 arguments */
1034 if ((numArgs > 4) && (numArgs % 4)) {
Bill Buzbee270c1d62009-08-13 16:58:07 -07001035 storeMultiple(cUnit, r7, regMask);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001036 }
1037}
1038
Ben Cheng38329f52009-07-07 14:19:20 -07001039/*
1040 * Generate code to setup the call stack then jump to the chaining cell if it
1041 * is not a native method.
1042 */
1043static void genInvokeSingletonCommon(CompilationUnit *cUnit, MIR *mir,
Bill Buzbee89efc3d2009-07-28 11:22:22 -07001044 BasicBlock *bb, ArmLIR *labelList,
1045 ArmLIR *pcrLabel,
Ben Cheng38329f52009-07-07 14:19:20 -07001046 const Method *calleeMethod)
Ben Chengba4fc8b2009-06-01 13:00:29 -07001047{
Bill Buzbee1465db52009-09-23 17:17:35 -07001048 /*
1049 * Note: all Dalvik register state should be flushed to
1050 * memory by the point, so register usage restrictions no
1051 * longer apply. All temp & preserved registers may be used.
1052 */
Bill Buzbeec6f10662010-02-09 11:16:15 -08001053 dvmCompilerLockAllTemps(cUnit);
Bill Buzbee89efc3d2009-07-28 11:22:22 -07001054 ArmLIR *retChainingCell = &labelList[bb->fallThrough->id];
Ben Chengba4fc8b2009-06-01 13:00:29 -07001055
1056 /* r1 = &retChainingCell */
Bill Buzbeec6f10662010-02-09 11:16:15 -08001057 dvmCompilerLockTemp(cUnit, r1);
Bill Buzbee1465db52009-09-23 17:17:35 -07001058 ArmLIR *addrRetChain = opRegRegImm(cUnit, kOpAdd, r1, rpc, 0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001059 /* r4PC = dalvikCallsite */
1060 loadConstant(cUnit, r4PC,
1061 (int) (cUnit->method->insns + mir->offset));
1062 addrRetChain->generic.target = (LIR *) retChainingCell;
1063 /*
Ben Cheng38329f52009-07-07 14:19:20 -07001064 * r0 = calleeMethod (loaded upon calling genInvokeSingletonCommon)
Ben Chengba4fc8b2009-06-01 13:00:29 -07001065 * r1 = &ChainingCell
1066 * r4PC = callsiteDPC
1067 */
1068 if (dvmIsNativeMethod(calleeMethod)) {
Ben Cheng38329f52009-07-07 14:19:20 -07001069 genDispatchToHandler(cUnit, TEMPLATE_INVOKE_METHOD_NATIVE);
Ben Cheng978738d2010-05-13 13:45:57 -07001070#if defined(WITH_JIT_TUNING)
Ben Cheng38329f52009-07-07 14:19:20 -07001071 gDvmJit.invokeNative++;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001072#endif
1073 } else {
1074 genDispatchToHandler(cUnit, TEMPLATE_INVOKE_METHOD_CHAIN);
Ben Cheng978738d2010-05-13 13:45:57 -07001075#if defined(WITH_JIT_TUNING)
Ben Cheng86717f72010-03-05 15:27:21 -08001076 gDvmJit.invokeMonomorphic++;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001077#endif
Ben Cheng38329f52009-07-07 14:19:20 -07001078 /* Branch to the chaining cell */
Ben Chengba4fc8b2009-06-01 13:00:29 -07001079 genUnconditionalBranch(cUnit, &labelList[bb->taken->id]);
1080 }
1081 /* Handle exceptions using the interpreter */
1082 genTrap(cUnit, mir->offset, pcrLabel);
1083}
1084
Ben Cheng38329f52009-07-07 14:19:20 -07001085/*
1086 * Generate code to check the validity of a predicted chain and take actions
1087 * based on the result.
1088 *
1089 * 0x426a99aa : ldr r4, [pc, #72] --> r4 <- dalvikPC of this invoke
1090 * 0x426a99ac : add r1, pc, #32 --> r1 <- &retChainingCell
1091 * 0x426a99ae : add r2, pc, #40 --> r2 <- &predictedChainingCell
1092 * 0x426a99b0 : blx_1 0x426a918c --+ TEMPLATE_INVOKE_METHOD_PREDICTED_CHAIN
1093 * 0x426a99b2 : blx_2 see above --+
1094 * 0x426a99b4 : b 0x426a99d8 --> off to the predicted chain
1095 * 0x426a99b6 : b 0x426a99c8 --> punt to the interpreter
1096 * 0x426a99b8 : ldr r0, [r7, #44] --> r0 <- this->class->vtable[methodIdx]
1097 * 0x426a99ba : cmp r1, #0 --> compare r1 (rechain count) against 0
1098 * 0x426a99bc : bgt 0x426a99c2 --> >=0? don't rechain
1099 * 0x426a99be : ldr r7, [r6, #96] --+ dvmJitToPatchPredictedChain
1100 * 0x426a99c0 : blx r7 --+
1101 * 0x426a99c2 : add r1, pc, #12 --> r1 <- &retChainingCell
1102 * 0x426a99c4 : blx_1 0x426a9098 --+ TEMPLATE_INVOKE_METHOD_NO_OPT
1103 * 0x426a99c6 : blx_2 see above --+
1104 */
1105static void genInvokeVirtualCommon(CompilationUnit *cUnit, MIR *mir,
1106 int methodIndex,
Bill Buzbee89efc3d2009-07-28 11:22:22 -07001107 ArmLIR *retChainingCell,
1108 ArmLIR *predChainingCell,
1109 ArmLIR *pcrLabel)
Ben Cheng38329f52009-07-07 14:19:20 -07001110{
Bill Buzbee1465db52009-09-23 17:17:35 -07001111 /*
1112 * Note: all Dalvik register state should be flushed to
1113 * memory by the point, so register usage restrictions no
1114 * longer apply. Lock temps to prevent them from being
1115 * allocated by utility routines.
1116 */
Bill Buzbeec6f10662010-02-09 11:16:15 -08001117 dvmCompilerLockAllTemps(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07001118
Ben Cheng38329f52009-07-07 14:19:20 -07001119 /* "this" is already left in r0 by genProcessArgs* */
1120
1121 /* r4PC = dalvikCallsite */
1122 loadConstant(cUnit, r4PC,
1123 (int) (cUnit->method->insns + mir->offset));
1124
1125 /* r1 = &retChainingCell */
Bill Buzbee1465db52009-09-23 17:17:35 -07001126 ArmLIR *addrRetChain = opRegRegImm(cUnit, kOpAdd, r1, rpc, 0);
Ben Cheng38329f52009-07-07 14:19:20 -07001127 addrRetChain->generic.target = (LIR *) retChainingCell;
1128
1129 /* r2 = &predictedChainingCell */
Bill Buzbee1465db52009-09-23 17:17:35 -07001130 ArmLIR *predictedChainingCell = opRegRegImm(cUnit, kOpAdd, r2, rpc, 0);
Ben Cheng38329f52009-07-07 14:19:20 -07001131 predictedChainingCell->generic.target = (LIR *) predChainingCell;
1132
1133 genDispatchToHandler(cUnit, TEMPLATE_INVOKE_METHOD_PREDICTED_CHAIN);
1134
1135 /* return through lr - jump to the chaining cell */
1136 genUnconditionalBranch(cUnit, predChainingCell);
1137
1138 /*
1139 * null-check on "this" may have been eliminated, but we still need a PC-
1140 * reconstruction label for stack overflow bailout.
1141 */
1142 if (pcrLabel == NULL) {
1143 int dPC = (int) (cUnit->method->insns + mir->offset);
Bill Buzbee89efc3d2009-07-28 11:22:22 -07001144 pcrLabel = dvmCompilerNew(sizeof(ArmLIR), true);
Ben Chenga4973592010-03-31 11:59:18 -07001145 pcrLabel->opCode = kArmPseudoPCReconstructionCell;
Ben Cheng38329f52009-07-07 14:19:20 -07001146 pcrLabel->operands[0] = dPC;
1147 pcrLabel->operands[1] = mir->offset;
1148 /* Insert the place holder to the growable list */
1149 dvmInsertGrowableList(&cUnit->pcReconstructionList, pcrLabel);
1150 }
1151
1152 /* return through lr+2 - punt to the interpreter */
1153 genUnconditionalBranch(cUnit, pcrLabel);
1154
1155 /*
1156 * return through lr+4 - fully resolve the callee method.
1157 * r1 <- count
1158 * r2 <- &predictedChainCell
1159 * r3 <- this->class
1160 * r4 <- dPC
1161 * r7 <- this->class->vtable
1162 */
1163
1164 /* r0 <- calleeMethod */
Bill Buzbee270c1d62009-08-13 16:58:07 -07001165 loadWordDisp(cUnit, r7, methodIndex * 4, r0);
Ben Cheng38329f52009-07-07 14:19:20 -07001166
1167 /* Check if rechain limit is reached */
Bill Buzbee1465db52009-09-23 17:17:35 -07001168 opRegImm(cUnit, kOpCmp, r1, 0);
Ben Cheng38329f52009-07-07 14:19:20 -07001169
Bill Buzbee1465db52009-09-23 17:17:35 -07001170 ArmLIR *bypassRechaining = opCondBranch(cUnit, kArmCondGt);
Ben Cheng38329f52009-07-07 14:19:20 -07001171
Bill Buzbee270c1d62009-08-13 16:58:07 -07001172 loadWordDisp(cUnit, rGLUE, offsetof(InterpState,
1173 jitToInterpEntries.dvmJitToPatchPredictedChain), r7);
Ben Cheng38329f52009-07-07 14:19:20 -07001174
Ben Chengb88ec3c2010-05-17 12:50:33 -07001175 genRegCopy(cUnit, r1, rGLUE);
1176
Ben Cheng38329f52009-07-07 14:19:20 -07001177 /*
1178 * r0 = calleeMethod
1179 * r2 = &predictedChainingCell
1180 * r3 = class
1181 *
1182 * &returnChainingCell has been loaded into r1 but is not needed
1183 * when patching the chaining cell and will be clobbered upon
1184 * returning so it will be reconstructed again.
1185 */
Bill Buzbee1465db52009-09-23 17:17:35 -07001186 opReg(cUnit, kOpBlx, r7);
Ben Cheng38329f52009-07-07 14:19:20 -07001187
1188 /* r1 = &retChainingCell */
Bill Buzbee1465db52009-09-23 17:17:35 -07001189 addrRetChain = opRegRegImm(cUnit, kOpAdd, r1, rpc, 0);
Ben Cheng38329f52009-07-07 14:19:20 -07001190 addrRetChain->generic.target = (LIR *) retChainingCell;
1191
1192 bypassRechaining->generic.target = (LIR *) addrRetChain;
1193 /*
1194 * r0 = calleeMethod,
1195 * r1 = &ChainingCell,
1196 * r4PC = callsiteDPC,
1197 */
1198 genDispatchToHandler(cUnit, TEMPLATE_INVOKE_METHOD_NO_OPT);
Ben Cheng978738d2010-05-13 13:45:57 -07001199#if defined(WITH_JIT_TUNING)
Ben Cheng86717f72010-03-05 15:27:21 -08001200 gDvmJit.invokePolymorphic++;
Ben Cheng38329f52009-07-07 14:19:20 -07001201#endif
1202 /* Handle exceptions using the interpreter */
1203 genTrap(cUnit, mir->offset, pcrLabel);
1204}
1205
Ben Chengba4fc8b2009-06-01 13:00:29 -07001206/* Geneate a branch to go back to the interpreter */
1207static void genPuntToInterp(CompilationUnit *cUnit, unsigned int offset)
1208{
1209 /* r0 = dalvik pc */
Bill Buzbeec6f10662010-02-09 11:16:15 -08001210 dvmCompilerFlushAllRegs(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001211 loadConstant(cUnit, r0, (int) (cUnit->method->insns + offset));
Bill Buzbee270c1d62009-08-13 16:58:07 -07001212 loadWordDisp(cUnit, r0, offsetof(Object, clazz), r3);
1213 loadWordDisp(cUnit, rGLUE, offsetof(InterpState,
1214 jitToInterpEntries.dvmJitToInterpPunt), r1);
Bill Buzbee1465db52009-09-23 17:17:35 -07001215 opReg(cUnit, kOpBlx, r1);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001216}
1217
1218/*
1219 * Attempt to single step one instruction using the interpreter and return
1220 * to the compiled code for the next Dalvik instruction
1221 */
1222static void genInterpSingleStep(CompilationUnit *cUnit, MIR *mir)
1223{
1224 int flags = dexGetInstrFlags(gDvm.instrFlags, mir->dalvikInsn.opCode);
1225 int flagsToCheck = kInstrCanBranch | kInstrCanSwitch | kInstrCanReturn |
1226 kInstrCanThrow;
Bill Buzbee1465db52009-09-23 17:17:35 -07001227
Bill Buzbee45273872010-03-11 11:12:15 -08001228 //If already optimized out, just ignore
1229 if (mir->dalvikInsn.opCode == OP_NOP)
1230 return;
1231
Bill Buzbee1465db52009-09-23 17:17:35 -07001232 //Ugly, but necessary. Flush all Dalvik regs so Interp can find them
Bill Buzbeec6f10662010-02-09 11:16:15 -08001233 dvmCompilerFlushAllRegs(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07001234
Ben Chengba4fc8b2009-06-01 13:00:29 -07001235 if ((mir->next == NULL) || (flags & flagsToCheck)) {
1236 genPuntToInterp(cUnit, mir->offset);
1237 return;
1238 }
1239 int entryAddr = offsetof(InterpState,
1240 jitToInterpEntries.dvmJitToInterpSingleStep);
Bill Buzbee270c1d62009-08-13 16:58:07 -07001241 loadWordDisp(cUnit, rGLUE, entryAddr, r2);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001242 /* r0 = dalvik pc */
1243 loadConstant(cUnit, r0, (int) (cUnit->method->insns + mir->offset));
1244 /* r1 = dalvik pc of following instruction */
1245 loadConstant(cUnit, r1, (int) (cUnit->method->insns + mir->next->offset));
Bill Buzbee1465db52009-09-23 17:17:35 -07001246 opReg(cUnit, kOpBlx, r2);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001247}
1248
Ben Chengfc075c22010-05-28 15:20:08 -07001249#if defined(WITH_DEADLOCK_PREDICTION) || defined(WITH_MONITOR_TRACKING) || \
1250 defined(_ARMV5TE) || defined(_ARMV5TE_VFP)
Bill Buzbeec1d9ed42010-02-02 11:04:33 -08001251/*
1252 * To prevent a thread in a monitor wait from blocking the Jit from
1253 * resetting the code cache, heavyweight monitor lock will not
1254 * be allowed to return to an existing translation. Instead, we will
1255 * handle them by branching to a handler, which will in turn call the
1256 * runtime lock routine and then branch directly back to the
1257 * interpreter main loop. Given the high cost of the heavyweight
1258 * lock operation, this additional cost should be slight (especially when
1259 * considering that we expect the vast majority of lock operations to
1260 * use the fast-path thin lock bypass).
1261 */
Ben Cheng5d90c202009-11-22 23:31:11 -08001262static void genMonitorPortable(CompilationUnit *cUnit, MIR *mir)
Bill Buzbee270c1d62009-08-13 16:58:07 -07001263{
Bill Buzbeeefbd3c52009-11-04 22:18:40 -08001264 bool isEnter = (mir->dalvikInsn.opCode == OP_MONITOR_ENTER);
Bill Buzbee1465db52009-09-23 17:17:35 -07001265 genExportPC(cUnit, mir);
Bill Buzbeec6f10662010-02-09 11:16:15 -08001266 dvmCompilerFlushAllRegs(cUnit); /* Send everything to home location */
1267 RegLocation rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07001268 loadValueDirectFixed(cUnit, rlSrc, r1);
1269 loadWordDisp(cUnit, rGLUE, offsetof(InterpState, self), r0);
Bill Buzbeec1d9ed42010-02-02 11:04:33 -08001270 genNullCheck(cUnit, rlSrc.sRegLow, r1, mir->offset, NULL);
Bill Buzbeeefbd3c52009-11-04 22:18:40 -08001271 if (isEnter) {
Bill Buzbeec1d9ed42010-02-02 11:04:33 -08001272 /* Get dPC of next insn */
1273 loadConstant(cUnit, r4PC, (int)(cUnit->method->insns + mir->offset +
1274 dexGetInstrWidthAbs(gDvm.instrWidth, OP_MONITOR_ENTER)));
1275#if defined(WITH_DEADLOCK_PREDICTION)
1276 genDispatchToHandler(cUnit, TEMPLATE_MONITOR_ENTER_DEBUG);
1277#else
1278 genDispatchToHandler(cUnit, TEMPLATE_MONITOR_ENTER);
1279#endif
Bill Buzbee1465db52009-09-23 17:17:35 -07001280 } else {
Ben Chengbd1326d2010-04-02 15:04:53 -07001281 LOAD_FUNC_ADDR(cUnit, r2, (int)dvmUnlockObject);
Bill Buzbeec1d9ed42010-02-02 11:04:33 -08001282 /* Do the call */
1283 opReg(cUnit, kOpBlx, r2);
Bill Buzbee6bbdd6b2010-02-16 14:40:01 -08001284 opRegImm(cUnit, kOpCmp, r0, 0); /* Did we throw? */
1285 ArmLIR *branchOver = opCondBranch(cUnit, kArmCondNe);
1286 loadConstant(cUnit, r0,
1287 (int) (cUnit->method->insns + mir->offset +
1288 dexGetInstrWidthAbs(gDvm.instrWidth, OP_MONITOR_EXIT)));
1289 genDispatchToHandler(cUnit, TEMPLATE_THROW_EXCEPTION_COMMON);
1290 ArmLIR *target = newLIR0(cUnit, kArmPseudoTargetLabel);
1291 target->defMask = ENCODE_ALL;
1292 branchOver->generic.target = (LIR *) target;
Elliott Hughes6a555132010-02-25 15:41:42 -08001293 dvmCompilerClobberCallRegs(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07001294 }
Bill Buzbee270c1d62009-08-13 16:58:07 -07001295}
Ben Chengfc075c22010-05-28 15:20:08 -07001296#endif
Bill Buzbee270c1d62009-08-13 16:58:07 -07001297
Ben Chengba4fc8b2009-06-01 13:00:29 -07001298/*
1299 * The following are the first-level codegen routines that analyze the format
1300 * of each bytecode then either dispatch special purpose codegen routines
1301 * or produce corresponding Thumb instructions directly.
1302 */
1303
1304static bool handleFmt10t_Fmt20t_Fmt30t(CompilationUnit *cUnit, MIR *mir,
Bill Buzbee89efc3d2009-07-28 11:22:22 -07001305 BasicBlock *bb, ArmLIR *labelList)
Ben Chengba4fc8b2009-06-01 13:00:29 -07001306{
1307 /* For OP_GOTO, OP_GOTO_16, and OP_GOTO_32 */
1308 genUnconditionalBranch(cUnit, &labelList[bb->taken->id]);
1309 return false;
1310}
1311
1312static bool handleFmt10x(CompilationUnit *cUnit, MIR *mir)
1313{
1314 OpCode dalvikOpCode = mir->dalvikInsn.opCode;
Andy McFaddenc35a2ef2010-06-17 12:36:00 -07001315 if ((dalvikOpCode >= OP_UNUSED_3E) && (dalvikOpCode <= OP_UNUSED_43)) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07001316 LOGE("Codegen: got unused opcode 0x%x\n",dalvikOpCode);
1317 return true;
1318 }
1319 switch (dalvikOpCode) {
1320 case OP_RETURN_VOID:
1321 genReturnCommon(cUnit,mir);
1322 break;
1323 case OP_UNUSED_73:
1324 case OP_UNUSED_79:
1325 case OP_UNUSED_7A:
Andy McFaddenc35a2ef2010-06-17 12:36:00 -07001326 case OP_UNUSED_F1:
1327 case OP_UNUSED_FF:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001328 LOGE("Codegen: got unused opcode 0x%x\n",dalvikOpCode);
1329 return true;
1330 case OP_NOP:
1331 break;
1332 default:
1333 return true;
1334 }
1335 return false;
1336}
1337
1338static bool handleFmt11n_Fmt31i(CompilationUnit *cUnit, MIR *mir)
1339{
Bill Buzbee1465db52009-09-23 17:17:35 -07001340 RegLocation rlDest;
1341 RegLocation rlResult;
1342 if (mir->ssaRep->numDefs == 2) {
Bill Buzbeec6f10662010-02-09 11:16:15 -08001343 rlDest = dvmCompilerGetDestWide(cUnit, mir, 0, 1);
Bill Buzbee1465db52009-09-23 17:17:35 -07001344 } else {
Bill Buzbeec6f10662010-02-09 11:16:15 -08001345 rlDest = dvmCompilerGetDest(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07001346 }
Ben Chenge9695e52009-06-16 16:11:47 -07001347
Ben Chengba4fc8b2009-06-01 13:00:29 -07001348 switch (mir->dalvikInsn.opCode) {
1349 case OP_CONST:
Ben Chenge9695e52009-06-16 16:11:47 -07001350 case OP_CONST_4: {
Bill Buzbeec6f10662010-02-09 11:16:15 -08001351 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kAnyReg, true);
Ben Chengbd1326d2010-04-02 15:04:53 -07001352 loadConstantNoClobber(cUnit, rlResult.lowReg, mir->dalvikInsn.vB);
Bill Buzbee1465db52009-09-23 17:17:35 -07001353 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001354 break;
Ben Chenge9695e52009-06-16 16:11:47 -07001355 }
1356 case OP_CONST_WIDE_32: {
Bill Buzbee1465db52009-09-23 17:17:35 -07001357 //TUNING: single routine to load constant pair for support doubles
Bill Buzbee964a7b02010-01-28 12:54:19 -08001358 //TUNING: load 0/-1 separately to avoid load dependency
Bill Buzbeec6f10662010-02-09 11:16:15 -08001359 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Ben Chengbd1326d2010-04-02 15:04:53 -07001360 loadConstantNoClobber(cUnit, rlResult.lowReg, mir->dalvikInsn.vB);
Bill Buzbee1465db52009-09-23 17:17:35 -07001361 opRegRegImm(cUnit, kOpAsr, rlResult.highReg,
1362 rlResult.lowReg, 31);
1363 storeValueWide(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001364 break;
Ben Chenge9695e52009-06-16 16:11:47 -07001365 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07001366 default:
1367 return true;
1368 }
1369 return false;
1370}
1371
1372static bool handleFmt21h(CompilationUnit *cUnit, MIR *mir)
1373{
Bill Buzbee1465db52009-09-23 17:17:35 -07001374 RegLocation rlDest;
1375 RegLocation rlResult;
1376 if (mir->ssaRep->numDefs == 2) {
Bill Buzbeec6f10662010-02-09 11:16:15 -08001377 rlDest = dvmCompilerGetDestWide(cUnit, mir, 0, 1);
Bill Buzbee1465db52009-09-23 17:17:35 -07001378 } else {
Bill Buzbeec6f10662010-02-09 11:16:15 -08001379 rlDest = dvmCompilerGetDest(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07001380 }
Bill Buzbeec6f10662010-02-09 11:16:15 -08001381 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kAnyReg, true);
Ben Chenge9695e52009-06-16 16:11:47 -07001382
Ben Chengba4fc8b2009-06-01 13:00:29 -07001383 switch (mir->dalvikInsn.opCode) {
Ben Chenge9695e52009-06-16 16:11:47 -07001384 case OP_CONST_HIGH16: {
Ben Chengbd1326d2010-04-02 15:04:53 -07001385 loadConstantNoClobber(cUnit, rlResult.lowReg,
1386 mir->dalvikInsn.vB << 16);
Bill Buzbee1465db52009-09-23 17:17:35 -07001387 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001388 break;
Ben Chenge9695e52009-06-16 16:11:47 -07001389 }
1390 case OP_CONST_WIDE_HIGH16: {
Bill Buzbee1465db52009-09-23 17:17:35 -07001391 loadConstantValueWide(cUnit, rlResult.lowReg, rlResult.highReg,
1392 0, mir->dalvikInsn.vB << 16);
1393 storeValueWide(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001394 break;
Ben Chenge9695e52009-06-16 16:11:47 -07001395 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07001396 default:
1397 return true;
1398 }
1399 return false;
1400}
1401
1402static bool handleFmt20bc(CompilationUnit *cUnit, MIR *mir)
1403{
1404 /* For OP_THROW_VERIFICATION_ERROR */
1405 genInterpSingleStep(cUnit, mir);
1406 return false;
1407}
1408
1409static bool handleFmt21c_Fmt31c(CompilationUnit *cUnit, MIR *mir)
1410{
Bill Buzbee1465db52009-09-23 17:17:35 -07001411 RegLocation rlResult;
1412 RegLocation rlDest;
1413 RegLocation rlSrc;
Ben Chenge9695e52009-06-16 16:11:47 -07001414
Ben Chengba4fc8b2009-06-01 13:00:29 -07001415 switch (mir->dalvikInsn.opCode) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07001416 case OP_CONST_STRING_JUMBO:
1417 case OP_CONST_STRING: {
1418 void *strPtr = (void*)
1419 (cUnit->method->clazz->pDvmDex->pResStrings[mir->dalvikInsn.vB]);
Ben Chengdd6e8702010-05-07 13:05:47 -07001420
1421 if (strPtr == NULL) {
1422 LOGE("Unexpected null string");
1423 dvmAbort();
1424 }
1425
Bill Buzbeec6f10662010-02-09 11:16:15 -08001426 rlDest = dvmCompilerGetDest(cUnit, mir, 0);
1427 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Ben Chengbd1326d2010-04-02 15:04:53 -07001428 loadConstantNoClobber(cUnit, rlResult.lowReg, (int) strPtr );
Bill Buzbee1465db52009-09-23 17:17:35 -07001429 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001430 break;
1431 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07001432 case OP_CONST_CLASS: {
1433 void *classPtr = (void*)
1434 (cUnit->method->clazz->pDvmDex->pResClasses[mir->dalvikInsn.vB]);
Ben Chengdd6e8702010-05-07 13:05:47 -07001435
1436 if (classPtr == NULL) {
1437 LOGE("Unexpected null class");
1438 dvmAbort();
1439 }
1440
Bill Buzbeec6f10662010-02-09 11:16:15 -08001441 rlDest = dvmCompilerGetDest(cUnit, mir, 0);
1442 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Ben Chengbd1326d2010-04-02 15:04:53 -07001443 loadConstantNoClobber(cUnit, rlResult.lowReg, (int) classPtr );
Bill Buzbee1465db52009-09-23 17:17:35 -07001444 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001445 break;
1446 }
1447 case OP_SGET_OBJECT:
1448 case OP_SGET_BOOLEAN:
1449 case OP_SGET_CHAR:
1450 case OP_SGET_BYTE:
1451 case OP_SGET_SHORT:
1452 case OP_SGET: {
Bill Buzbee50a6bf22009-07-08 13:08:04 -07001453 int valOffset = offsetof(StaticField, value);
Bill Buzbeec6f10662010-02-09 11:16:15 -08001454 int tReg = dvmCompilerAllocTemp(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001455 void *fieldPtr = (void*)
1456 (cUnit->method->clazz->pDvmDex->pResFields[mir->dalvikInsn.vB]);
Ben Chengdd6e8702010-05-07 13:05:47 -07001457
1458 if (fieldPtr == NULL) {
1459 LOGE("Unexpected null static field");
1460 dvmAbort();
1461 }
1462
Bill Buzbeec6f10662010-02-09 11:16:15 -08001463 rlDest = dvmCompilerGetDest(cUnit, mir, 0);
1464 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kAnyReg, true);
Bill Buzbee1465db52009-09-23 17:17:35 -07001465 loadConstant(cUnit, tReg, (int) fieldPtr + valOffset);
Ben Cheng11d8f142010-03-24 15:24:19 -07001466
1467 HEAP_ACCESS_SHADOW(true);
Bill Buzbee1465db52009-09-23 17:17:35 -07001468 loadWordDisp(cUnit, tReg, 0, rlResult.lowReg);
Ben Cheng11d8f142010-03-24 15:24:19 -07001469 HEAP_ACCESS_SHADOW(false);
1470
Bill Buzbee1465db52009-09-23 17:17:35 -07001471 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001472 break;
1473 }
1474 case OP_SGET_WIDE: {
Bill Buzbee50a6bf22009-07-08 13:08:04 -07001475 int valOffset = offsetof(StaticField, value);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001476 void *fieldPtr = (void*)
1477 (cUnit->method->clazz->pDvmDex->pResFields[mir->dalvikInsn.vB]);
Ben Chengdd6e8702010-05-07 13:05:47 -07001478
1479 if (fieldPtr == NULL) {
1480 LOGE("Unexpected null static field");
1481 dvmAbort();
1482 }
1483
Bill Buzbeec6f10662010-02-09 11:16:15 -08001484 int tReg = dvmCompilerAllocTemp(cUnit);
Bill Buzbeec6f10662010-02-09 11:16:15 -08001485 rlDest = dvmCompilerGetDestWide(cUnit, mir, 0, 1);
1486 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kAnyReg, true);
Bill Buzbee1465db52009-09-23 17:17:35 -07001487 loadConstant(cUnit, tReg, (int) fieldPtr + valOffset);
Ben Cheng11d8f142010-03-24 15:24:19 -07001488
1489 HEAP_ACCESS_SHADOW(true);
Bill Buzbee1465db52009-09-23 17:17:35 -07001490 loadPair(cUnit, tReg, rlResult.lowReg, rlResult.highReg);
Ben Cheng11d8f142010-03-24 15:24:19 -07001491 HEAP_ACCESS_SHADOW(false);
1492
Bill Buzbee1465db52009-09-23 17:17:35 -07001493 storeValueWide(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001494 break;
1495 }
1496 case OP_SPUT_OBJECT:
1497 case OP_SPUT_BOOLEAN:
1498 case OP_SPUT_CHAR:
1499 case OP_SPUT_BYTE:
1500 case OP_SPUT_SHORT:
1501 case OP_SPUT: {
Bill Buzbee50a6bf22009-07-08 13:08:04 -07001502 int valOffset = offsetof(StaticField, value);
Bill Buzbeec6f10662010-02-09 11:16:15 -08001503 int tReg = dvmCompilerAllocTemp(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001504 void *fieldPtr = (void*)
1505 (cUnit->method->clazz->pDvmDex->pResFields[mir->dalvikInsn.vB]);
Ben Chenge9695e52009-06-16 16:11:47 -07001506
Ben Chengdd6e8702010-05-07 13:05:47 -07001507 if (fieldPtr == NULL) {
1508 LOGE("Unexpected null static field");
1509 dvmAbort();
1510 }
1511
Bill Buzbeec6f10662010-02-09 11:16:15 -08001512 rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07001513 rlSrc = loadValue(cUnit, rlSrc, kAnyReg);
1514 loadConstant(cUnit, tReg, (int) fieldPtr + valOffset);
Ben Cheng11d8f142010-03-24 15:24:19 -07001515
1516 HEAP_ACCESS_SHADOW(true);
Bill Buzbee1465db52009-09-23 17:17:35 -07001517 storeWordDisp(cUnit, tReg, 0 ,rlSrc.lowReg);
Ben Cheng11d8f142010-03-24 15:24:19 -07001518 HEAP_ACCESS_SHADOW(false);
buzbee919eb062010-07-12 12:59:22 -07001519 if (mir->dalvikInsn.opCode == OP_SPUT_OBJECT) {
1520 /* NOTE: marking card based on field address */
1521 markCard(cUnit, rlSrc.lowReg, tReg);
1522 }
Ben Cheng11d8f142010-03-24 15:24:19 -07001523
Ben Chengba4fc8b2009-06-01 13:00:29 -07001524 break;
1525 }
1526 case OP_SPUT_WIDE: {
Bill Buzbeec6f10662010-02-09 11:16:15 -08001527 int tReg = dvmCompilerAllocTemp(cUnit);
Bill Buzbee50a6bf22009-07-08 13:08:04 -07001528 int valOffset = offsetof(StaticField, value);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001529 void *fieldPtr = (void*)
1530 (cUnit->method->clazz->pDvmDex->pResFields[mir->dalvikInsn.vB]);
Ben Chenge9695e52009-06-16 16:11:47 -07001531
Ben Chengdd6e8702010-05-07 13:05:47 -07001532 if (fieldPtr == NULL) {
1533 LOGE("Unexpected null static field");
1534 dvmAbort();
1535 }
1536
Bill Buzbeec6f10662010-02-09 11:16:15 -08001537 rlSrc = dvmCompilerGetSrcWide(cUnit, mir, 0, 1);
Bill Buzbee1465db52009-09-23 17:17:35 -07001538 rlSrc = loadValueWide(cUnit, rlSrc, kAnyReg);
1539 loadConstant(cUnit, tReg, (int) fieldPtr + valOffset);
Ben Cheng11d8f142010-03-24 15:24:19 -07001540
1541 HEAP_ACCESS_SHADOW(true);
Bill Buzbee1465db52009-09-23 17:17:35 -07001542 storePair(cUnit, tReg, rlSrc.lowReg, rlSrc.highReg);
Ben Cheng11d8f142010-03-24 15:24:19 -07001543 HEAP_ACCESS_SHADOW(false);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001544 break;
1545 }
1546 case OP_NEW_INSTANCE: {
Ben Chenge9695e52009-06-16 16:11:47 -07001547 /*
1548 * Obey the calling convention and don't mess with the register
1549 * usage.
1550 */
Ben Chengba4fc8b2009-06-01 13:00:29 -07001551 ClassObject *classPtr = (void*)
1552 (cUnit->method->clazz->pDvmDex->pResClasses[mir->dalvikInsn.vB]);
Ben Chengdd6e8702010-05-07 13:05:47 -07001553
1554 if (classPtr == NULL) {
1555 LOGE("Unexpected null class");
1556 dvmAbort();
1557 }
1558
Ben Cheng79d173c2009-09-29 16:12:51 -07001559 /*
1560 * If it is going to throw, it should not make to the trace to begin
Bill Buzbee1465db52009-09-23 17:17:35 -07001561 * with. However, Alloc might throw, so we need to genExportPC()
Ben Cheng79d173c2009-09-29 16:12:51 -07001562 */
1563 assert((classPtr->accessFlags & (ACC_INTERFACE|ACC_ABSTRACT)) == 0);
Bill Buzbeec6f10662010-02-09 11:16:15 -08001564 dvmCompilerFlushAllRegs(cUnit); /* Everything to home location */
Bill Buzbee1465db52009-09-23 17:17:35 -07001565 genExportPC(cUnit, mir);
Ben Chengbd1326d2010-04-02 15:04:53 -07001566 LOAD_FUNC_ADDR(cUnit, r2, (int)dvmAllocObject);
Ben Chenge9695e52009-06-16 16:11:47 -07001567 loadConstant(cUnit, r0, (int) classPtr);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001568 loadConstant(cUnit, r1, ALLOC_DONT_TRACK);
Bill Buzbee1465db52009-09-23 17:17:35 -07001569 opReg(cUnit, kOpBlx, r2);
Elliott Hughes6a555132010-02-25 15:41:42 -08001570 dvmCompilerClobberCallRegs(cUnit);
Ben Cheng4f489172009-09-27 17:08:35 -07001571 /* generate a branch over if allocation is successful */
Bill Buzbee1465db52009-09-23 17:17:35 -07001572 opRegImm(cUnit, kOpCmp, r0, 0); /* NULL? */
1573 ArmLIR *branchOver = opCondBranch(cUnit, kArmCondNe);
Ben Cheng4f489172009-09-27 17:08:35 -07001574 /*
1575 * OOM exception needs to be thrown here and cannot re-execute
1576 */
1577 loadConstant(cUnit, r0,
1578 (int) (cUnit->method->insns + mir->offset));
1579 genDispatchToHandler(cUnit, TEMPLATE_THROW_EXCEPTION_COMMON);
1580 /* noreturn */
1581
Bill Buzbee1465db52009-09-23 17:17:35 -07001582 ArmLIR *target = newLIR0(cUnit, kArmPseudoTargetLabel);
Ben Cheng4f489172009-09-27 17:08:35 -07001583 target->defMask = ENCODE_ALL;
1584 branchOver->generic.target = (LIR *) target;
Bill Buzbeec6f10662010-02-09 11:16:15 -08001585 rlDest = dvmCompilerGetDest(cUnit, mir, 0);
1586 rlResult = dvmCompilerGetReturn(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07001587 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001588 break;
1589 }
1590 case OP_CHECK_CAST: {
Ben Chenge9695e52009-06-16 16:11:47 -07001591 /*
1592 * Obey the calling convention and don't mess with the register
1593 * usage.
1594 */
Ben Chengba4fc8b2009-06-01 13:00:29 -07001595 ClassObject *classPtr =
1596 (cUnit->method->clazz->pDvmDex->pResClasses[mir->dalvikInsn.vB]);
Bill Buzbee4df41a52009-11-12 17:07:16 -08001597 /*
1598 * Note: It is possible that classPtr is NULL at this point,
1599 * even though this instruction has been successfully interpreted.
1600 * If the previous interpretation had a null source, the
1601 * interpreter would not have bothered to resolve the clazz.
1602 * Bail out to the interpreter in this case, and log it
1603 * so that we can tell if it happens frequently.
1604 */
1605 if (classPtr == NULL) {
Ben Cheng11d8f142010-03-24 15:24:19 -07001606 LOGVV("null clazz in OP_CHECK_CAST, single-stepping");
Bill Buzbee4df41a52009-11-12 17:07:16 -08001607 genInterpSingleStep(cUnit, mir);
1608 return false;
1609 }
Bill Buzbeec6f10662010-02-09 11:16:15 -08001610 dvmCompilerFlushAllRegs(cUnit); /* Everything to home location */
Ben Chengba4fc8b2009-06-01 13:00:29 -07001611 loadConstant(cUnit, r1, (int) classPtr );
Bill Buzbeec6f10662010-02-09 11:16:15 -08001612 rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07001613 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
1614 opRegImm(cUnit, kOpCmp, rlSrc.lowReg, 0); /* Null? */
1615 ArmLIR *branch1 = opCondBranch(cUnit, kArmCondEq);
1616 /*
1617 * rlSrc.lowReg now contains object->clazz. Note that
1618 * it could have been allocated r0, but we're okay so long
1619 * as we don't do anything desctructive until r0 is loaded
1620 * with clazz.
1621 */
Ben Chengba4fc8b2009-06-01 13:00:29 -07001622 /* r0 now contains object->clazz */
Bill Buzbee1465db52009-09-23 17:17:35 -07001623 loadWordDisp(cUnit, rlSrc.lowReg, offsetof(Object, clazz), r0);
Ben Chengbd1326d2010-04-02 15:04:53 -07001624 LOAD_FUNC_ADDR(cUnit, r2, (int)dvmInstanceofNonTrivial);
Bill Buzbee1465db52009-09-23 17:17:35 -07001625 opRegReg(cUnit, kOpCmp, r0, r1);
1626 ArmLIR *branch2 = opCondBranch(cUnit, kArmCondEq);
1627 opReg(cUnit, kOpBlx, r2);
Elliott Hughes6a555132010-02-25 15:41:42 -08001628 dvmCompilerClobberCallRegs(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07001629 /*
1630 * If null, check cast failed - punt to the interpreter. Because
1631 * interpreter will be the one throwing, we don't need to
1632 * genExportPC() here.
1633 */
Bill Buzbee270c1d62009-08-13 16:58:07 -07001634 genZeroCheck(cUnit, r0, mir->offset, NULL);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001635 /* check cast passed - branch target here */
Bill Buzbee1465db52009-09-23 17:17:35 -07001636 ArmLIR *target = newLIR0(cUnit, kArmPseudoTargetLabel);
Ben Chengd7d426a2009-09-22 11:23:36 -07001637 target->defMask = ENCODE_ALL;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001638 branch1->generic.target = (LIR *)target;
1639 branch2->generic.target = (LIR *)target;
1640 break;
1641 }
1642 default:
1643 return true;
1644 }
1645 return false;
1646}
1647
1648static bool handleFmt11x(CompilationUnit *cUnit, MIR *mir)
1649{
1650 OpCode dalvikOpCode = mir->dalvikInsn.opCode;
Bill Buzbee1465db52009-09-23 17:17:35 -07001651 RegLocation rlResult;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001652 switch (dalvikOpCode) {
1653 case OP_MOVE_EXCEPTION: {
1654 int offset = offsetof(InterpState, self);
1655 int exOffset = offsetof(Thread, exception);
Bill Buzbeec6f10662010-02-09 11:16:15 -08001656 int selfReg = dvmCompilerAllocTemp(cUnit);
1657 int resetReg = dvmCompilerAllocTemp(cUnit);
1658 RegLocation rlDest = dvmCompilerGetDest(cUnit, mir, 0);
1659 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Bill Buzbee1465db52009-09-23 17:17:35 -07001660 loadWordDisp(cUnit, rGLUE, offset, selfReg);
Bill Buzbeef9f33282009-11-22 12:45:30 -08001661 loadConstant(cUnit, resetReg, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07001662 loadWordDisp(cUnit, selfReg, exOffset, rlResult.lowReg);
Bill Buzbeef9f33282009-11-22 12:45:30 -08001663 storeWordDisp(cUnit, selfReg, exOffset, resetReg);
Bill Buzbee1465db52009-09-23 17:17:35 -07001664 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001665 break;
1666 }
1667 case OP_MOVE_RESULT:
1668 case OP_MOVE_RESULT_OBJECT: {
Bill Buzbeec6f10662010-02-09 11:16:15 -08001669 RegLocation rlDest = dvmCompilerGetDest(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07001670 RegLocation rlSrc = LOC_DALVIK_RETURN_VAL;
1671 rlSrc.fp = rlDest.fp;
1672 storeValue(cUnit, rlDest, rlSrc);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001673 break;
1674 }
1675 case OP_MOVE_RESULT_WIDE: {
Bill Buzbeec6f10662010-02-09 11:16:15 -08001676 RegLocation rlDest = dvmCompilerGetDestWide(cUnit, mir, 0, 1);
Bill Buzbee1465db52009-09-23 17:17:35 -07001677 RegLocation rlSrc = LOC_DALVIK_RETURN_VAL_WIDE;
1678 rlSrc.fp = rlDest.fp;
1679 storeValueWide(cUnit, rlDest, rlSrc);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001680 break;
1681 }
1682 case OP_RETURN_WIDE: {
Bill Buzbeec6f10662010-02-09 11:16:15 -08001683 RegLocation rlSrc = dvmCompilerGetSrcWide(cUnit, mir, 0, 1);
Bill Buzbee1465db52009-09-23 17:17:35 -07001684 RegLocation rlDest = LOC_DALVIK_RETURN_VAL_WIDE;
1685 rlDest.fp = rlSrc.fp;
1686 storeValueWide(cUnit, rlDest, rlSrc);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001687 genReturnCommon(cUnit,mir);
1688 break;
1689 }
1690 case OP_RETURN:
1691 case OP_RETURN_OBJECT: {
Bill Buzbeec6f10662010-02-09 11:16:15 -08001692 RegLocation rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07001693 RegLocation rlDest = LOC_DALVIK_RETURN_VAL;
1694 rlDest.fp = rlSrc.fp;
1695 storeValue(cUnit, rlDest, rlSrc);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001696 genReturnCommon(cUnit,mir);
1697 break;
1698 }
Bill Buzbee1465db52009-09-23 17:17:35 -07001699 case OP_MONITOR_EXIT:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001700 case OP_MONITOR_ENTER:
Bill Buzbeed0937ef2009-12-22 16:15:39 -08001701#if defined(WITH_DEADLOCK_PREDICTION) || defined(WITH_MONITOR_TRACKING)
Ben Cheng5d90c202009-11-22 23:31:11 -08001702 genMonitorPortable(cUnit, mir);
Bill Buzbee1465db52009-09-23 17:17:35 -07001703#else
Ben Cheng5d90c202009-11-22 23:31:11 -08001704 genMonitor(cUnit, mir);
Bill Buzbee1465db52009-09-23 17:17:35 -07001705#endif
Ben Chengba4fc8b2009-06-01 13:00:29 -07001706 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001707 case OP_THROW: {
1708 genInterpSingleStep(cUnit, mir);
1709 break;
1710 }
1711 default:
1712 return true;
1713 }
1714 return false;
1715}
1716
Bill Buzbeed45ba372009-06-15 17:00:57 -07001717static bool handleFmt12x(CompilationUnit *cUnit, MIR *mir)
1718{
1719 OpCode opCode = mir->dalvikInsn.opCode;
Bill Buzbee1465db52009-09-23 17:17:35 -07001720 RegLocation rlDest;
1721 RegLocation rlSrc;
1722 RegLocation rlResult;
Bill Buzbeed45ba372009-06-15 17:00:57 -07001723
Ben Chengba4fc8b2009-06-01 13:00:29 -07001724 if ( (opCode >= OP_ADD_INT_2ADDR) && (opCode <= OP_REM_DOUBLE_2ADDR)) {
Ben Cheng5d90c202009-11-22 23:31:11 -08001725 return genArithOp( cUnit, mir );
Ben Chengba4fc8b2009-06-01 13:00:29 -07001726 }
1727
Bill Buzbee1465db52009-09-23 17:17:35 -07001728 if (mir->ssaRep->numUses == 2)
Bill Buzbeec6f10662010-02-09 11:16:15 -08001729 rlSrc = dvmCompilerGetSrcWide(cUnit, mir, 0, 1);
Bill Buzbee1465db52009-09-23 17:17:35 -07001730 else
Bill Buzbeec6f10662010-02-09 11:16:15 -08001731 rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07001732 if (mir->ssaRep->numDefs == 2)
Bill Buzbeec6f10662010-02-09 11:16:15 -08001733 rlDest = dvmCompilerGetDestWide(cUnit, mir, 0, 1);
Bill Buzbee1465db52009-09-23 17:17:35 -07001734 else
Bill Buzbeec6f10662010-02-09 11:16:15 -08001735 rlDest = dvmCompilerGetDest(cUnit, mir, 0);
Ben Chenge9695e52009-06-16 16:11:47 -07001736
Ben Chengba4fc8b2009-06-01 13:00:29 -07001737 switch (opCode) {
Bill Buzbee1465db52009-09-23 17:17:35 -07001738 case OP_DOUBLE_TO_INT:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001739 case OP_INT_TO_FLOAT:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001740 case OP_FLOAT_TO_INT:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001741 case OP_DOUBLE_TO_FLOAT:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001742 case OP_FLOAT_TO_DOUBLE:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001743 case OP_INT_TO_DOUBLE:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001744 case OP_FLOAT_TO_LONG:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001745 case OP_LONG_TO_FLOAT:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001746 case OP_DOUBLE_TO_LONG:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001747 case OP_LONG_TO_DOUBLE:
Ben Cheng5d90c202009-11-22 23:31:11 -08001748 return genConversion(cUnit, mir);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001749 case OP_NEG_INT:
1750 case OP_NOT_INT:
Ben Cheng5d90c202009-11-22 23:31:11 -08001751 return genArithOpInt(cUnit, mir, rlDest, rlSrc, rlSrc);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001752 case OP_NEG_LONG:
1753 case OP_NOT_LONG:
Ben Cheng5d90c202009-11-22 23:31:11 -08001754 return genArithOpLong(cUnit, mir, rlDest, rlSrc, rlSrc);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001755 case OP_NEG_FLOAT:
Ben Cheng5d90c202009-11-22 23:31:11 -08001756 return genArithOpFloat(cUnit, mir, rlDest, rlSrc, rlSrc);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001757 case OP_NEG_DOUBLE:
Ben Cheng5d90c202009-11-22 23:31:11 -08001758 return genArithOpDouble(cUnit, mir, rlDest, rlSrc, rlSrc);
Bill Buzbee1465db52009-09-23 17:17:35 -07001759 case OP_MOVE_WIDE:
1760 storeValueWide(cUnit, rlDest, rlSrc);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001761 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07001762 case OP_INT_TO_LONG:
Bill Buzbeec6f10662010-02-09 11:16:15 -08001763 rlSrc = dvmCompilerUpdateLoc(cUnit, rlSrc);
1764 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Bill Buzbee964a7b02010-01-28 12:54:19 -08001765 //TUNING: shouldn't loadValueDirect already check for phys reg?
Bill Buzbee1465db52009-09-23 17:17:35 -07001766 if (rlSrc.location == kLocPhysReg) {
1767 genRegCopy(cUnit, rlResult.lowReg, rlSrc.lowReg);
1768 } else {
1769 loadValueDirect(cUnit, rlSrc, rlResult.lowReg);
1770 }
1771 opRegRegImm(cUnit, kOpAsr, rlResult.highReg,
1772 rlResult.lowReg, 31);
1773 storeValueWide(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001774 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07001775 case OP_LONG_TO_INT:
Bill Buzbeec6f10662010-02-09 11:16:15 -08001776 rlSrc = dvmCompilerUpdateLocWide(cUnit, rlSrc);
1777 rlSrc = dvmCompilerWideToNarrow(cUnit, rlSrc);
Bill Buzbee1465db52009-09-23 17:17:35 -07001778 // Intentional fallthrough
Ben Chengba4fc8b2009-06-01 13:00:29 -07001779 case OP_MOVE:
1780 case OP_MOVE_OBJECT:
Bill Buzbee1465db52009-09-23 17:17:35 -07001781 storeValue(cUnit, rlDest, rlSrc);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001782 break;
1783 case OP_INT_TO_BYTE:
Bill Buzbee1465db52009-09-23 17:17:35 -07001784 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
Bill Buzbeec6f10662010-02-09 11:16:15 -08001785 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Bill Buzbee1465db52009-09-23 17:17:35 -07001786 opRegReg(cUnit, kOp2Byte, rlResult.lowReg, rlSrc.lowReg);
1787 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001788 break;
1789 case OP_INT_TO_SHORT:
Bill Buzbee1465db52009-09-23 17:17:35 -07001790 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
Bill Buzbeec6f10662010-02-09 11:16:15 -08001791 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Bill Buzbee1465db52009-09-23 17:17:35 -07001792 opRegReg(cUnit, kOp2Short, rlResult.lowReg, rlSrc.lowReg);
1793 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001794 break;
1795 case OP_INT_TO_CHAR:
Bill Buzbee1465db52009-09-23 17:17:35 -07001796 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
Bill Buzbeec6f10662010-02-09 11:16:15 -08001797 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Bill Buzbee1465db52009-09-23 17:17:35 -07001798 opRegReg(cUnit, kOp2Char, rlResult.lowReg, rlSrc.lowReg);
1799 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001800 break;
1801 case OP_ARRAY_LENGTH: {
1802 int lenOffset = offsetof(ArrayObject, length);
Bill Buzbee1465db52009-09-23 17:17:35 -07001803 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
1804 genNullCheck(cUnit, rlSrc.sRegLow, rlSrc.lowReg,
1805 mir->offset, NULL);
Bill Buzbeec6f10662010-02-09 11:16:15 -08001806 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Bill Buzbee1465db52009-09-23 17:17:35 -07001807 loadWordDisp(cUnit, rlSrc.lowReg, lenOffset,
1808 rlResult.lowReg);
1809 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001810 break;
1811 }
1812 default:
1813 return true;
1814 }
1815 return false;
1816}
1817
1818static bool handleFmt21s(CompilationUnit *cUnit, MIR *mir)
1819{
1820 OpCode dalvikOpCode = mir->dalvikInsn.opCode;
Bill Buzbee1465db52009-09-23 17:17:35 -07001821 RegLocation rlDest;
1822 RegLocation rlResult;
1823 int BBBB = mir->dalvikInsn.vB;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001824 if (dalvikOpCode == OP_CONST_WIDE_16) {
Bill Buzbeec6f10662010-02-09 11:16:15 -08001825 rlDest = dvmCompilerGetDestWide(cUnit, mir, 0, 1);
1826 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Ben Chengbd1326d2010-04-02 15:04:53 -07001827 loadConstantNoClobber(cUnit, rlResult.lowReg, BBBB);
Bill Buzbee964a7b02010-01-28 12:54:19 -08001828 //TUNING: do high separately to avoid load dependency
Bill Buzbee1465db52009-09-23 17:17:35 -07001829 opRegRegImm(cUnit, kOpAsr, rlResult.highReg, rlResult.lowReg, 31);
1830 storeValueWide(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001831 } else if (dalvikOpCode == OP_CONST_16) {
Bill Buzbeec6f10662010-02-09 11:16:15 -08001832 rlDest = dvmCompilerGetDest(cUnit, mir, 0);
1833 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kAnyReg, true);
Ben Chengbd1326d2010-04-02 15:04:53 -07001834 loadConstantNoClobber(cUnit, rlResult.lowReg, BBBB);
Bill Buzbee1465db52009-09-23 17:17:35 -07001835 storeValue(cUnit, rlDest, rlResult);
1836 } else
Ben Chengba4fc8b2009-06-01 13:00:29 -07001837 return true;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001838 return false;
1839}
1840
1841/* Compare agaist zero */
1842static bool handleFmt21t(CompilationUnit *cUnit, MIR *mir, BasicBlock *bb,
Bill Buzbee89efc3d2009-07-28 11:22:22 -07001843 ArmLIR *labelList)
Ben Chengba4fc8b2009-06-01 13:00:29 -07001844{
1845 OpCode dalvikOpCode = mir->dalvikInsn.opCode;
Bill Buzbee89efc3d2009-07-28 11:22:22 -07001846 ArmConditionCode cond;
Bill Buzbeec6f10662010-02-09 11:16:15 -08001847 RegLocation rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07001848 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
1849 opRegImm(cUnit, kOpCmp, rlSrc.lowReg, 0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001850
Bill Buzbee270c1d62009-08-13 16:58:07 -07001851//TUNING: break this out to allow use of Thumb2 CB[N]Z
Ben Chengba4fc8b2009-06-01 13:00:29 -07001852 switch (dalvikOpCode) {
1853 case OP_IF_EQZ:
Bill Buzbee1465db52009-09-23 17:17:35 -07001854 cond = kArmCondEq;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001855 break;
1856 case OP_IF_NEZ:
Bill Buzbee1465db52009-09-23 17:17:35 -07001857 cond = kArmCondNe;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001858 break;
1859 case OP_IF_LTZ:
Bill Buzbee1465db52009-09-23 17:17:35 -07001860 cond = kArmCondLt;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001861 break;
1862 case OP_IF_GEZ:
Bill Buzbee1465db52009-09-23 17:17:35 -07001863 cond = kArmCondGe;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001864 break;
1865 case OP_IF_GTZ:
Bill Buzbee1465db52009-09-23 17:17:35 -07001866 cond = kArmCondGt;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001867 break;
1868 case OP_IF_LEZ:
Bill Buzbee1465db52009-09-23 17:17:35 -07001869 cond = kArmCondLe;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001870 break;
1871 default:
1872 cond = 0;
1873 LOGE("Unexpected opcode (%d) for Fmt21t\n", dalvikOpCode);
Bill Buzbeefc519dc2010-03-06 23:30:57 -08001874 dvmCompilerAbort(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001875 }
1876 genConditionalBranch(cUnit, cond, &labelList[bb->taken->id]);
1877 /* This mostly likely will be optimized away in a later phase */
1878 genUnconditionalBranch(cUnit, &labelList[bb->fallThrough->id]);
1879 return false;
1880}
1881
Elliott Hughesb4c05972010-02-24 16:36:18 -08001882static bool isPowerOfTwo(int x)
1883{
1884 return (x & (x - 1)) == 0;
1885}
1886
1887// Returns true if no more than two bits are set in 'x'.
1888static bool isPopCountLE2(unsigned int x)
1889{
1890 x &= x - 1;
1891 return (x & (x - 1)) == 0;
1892}
1893
1894// Returns the index of the lowest set bit in 'x'.
1895static int lowestSetBit(unsigned int x) {
1896 int bit_posn = 0;
1897 while ((x & 0xf) == 0) {
1898 bit_posn += 4;
1899 x >>= 4;
Bill Buzbee78cb0e22010-02-11 14:04:53 -08001900 }
Elliott Hughesb4c05972010-02-24 16:36:18 -08001901 while ((x & 1) == 0) {
1902 bit_posn++;
1903 x >>= 1;
1904 }
1905 return bit_posn;
1906}
1907
Elliott Hughes672511b2010-04-26 17:40:13 -07001908// Returns true if it added instructions to 'cUnit' to divide 'rlSrc' by 'lit'
1909// and store the result in 'rlDest'.
Elliott Hughesc7ad9b22010-04-28 13:52:02 -07001910static bool handleEasyDivide(CompilationUnit *cUnit, OpCode dalvikOpCode,
Elliott Hughes672511b2010-04-26 17:40:13 -07001911 RegLocation rlSrc, RegLocation rlDest, int lit)
1912{
1913 if (lit < 2 || !isPowerOfTwo(lit)) {
1914 return false;
1915 }
1916 int k = lowestSetBit(lit);
1917 if (k >= 30) {
1918 // Avoid special cases.
1919 return false;
1920 }
Elliott Hughes9c457022010-04-28 16:15:38 -07001921 bool div = (dalvikOpCode == OP_DIV_INT_LIT8 || dalvikOpCode == OP_DIV_INT_LIT16);
Elliott Hughes672511b2010-04-26 17:40:13 -07001922 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
1923 RegLocation rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Elliott Hughes9c457022010-04-28 16:15:38 -07001924 if (div) {
1925 int tReg = dvmCompilerAllocTemp(cUnit);
1926 if (lit == 2) {
1927 // Division by 2 is by far the most common division by constant.
1928 opRegRegImm(cUnit, kOpLsr, tReg, rlSrc.lowReg, 32 - k);
1929 opRegRegReg(cUnit, kOpAdd, tReg, tReg, rlSrc.lowReg);
1930 opRegRegImm(cUnit, kOpAsr, rlResult.lowReg, tReg, k);
1931 } else {
1932 opRegRegImm(cUnit, kOpAsr, tReg, rlSrc.lowReg, 31);
1933 opRegRegImm(cUnit, kOpLsr, tReg, tReg, 32 - k);
1934 opRegRegReg(cUnit, kOpAdd, tReg, tReg, rlSrc.lowReg);
1935 opRegRegImm(cUnit, kOpAsr, rlResult.lowReg, tReg, k);
1936 }
Elliott Hughes672511b2010-04-26 17:40:13 -07001937 } else {
Elliott Hughes9c457022010-04-28 16:15:38 -07001938 int cReg = dvmCompilerAllocTemp(cUnit);
1939 loadConstant(cUnit, cReg, lit - 1);
1940 int tReg1 = dvmCompilerAllocTemp(cUnit);
1941 int tReg2 = dvmCompilerAllocTemp(cUnit);
1942 if (lit == 2) {
1943 opRegRegImm(cUnit, kOpLsr, tReg1, rlSrc.lowReg, 32 - k);
1944 opRegRegReg(cUnit, kOpAdd, tReg2, tReg1, rlSrc.lowReg);
1945 opRegRegReg(cUnit, kOpAnd, tReg2, tReg2, cReg);
1946 opRegRegReg(cUnit, kOpSub, rlResult.lowReg, tReg2, tReg1);
1947 } else {
1948 opRegRegImm(cUnit, kOpAsr, tReg1, rlSrc.lowReg, 31);
1949 opRegRegImm(cUnit, kOpLsr, tReg1, tReg1, 32 - k);
1950 opRegRegReg(cUnit, kOpAdd, tReg2, tReg1, rlSrc.lowReg);
1951 opRegRegReg(cUnit, kOpAnd, tReg2, tReg2, cReg);
1952 opRegRegReg(cUnit, kOpSub, rlResult.lowReg, tReg2, tReg1);
1953 }
Elliott Hughes672511b2010-04-26 17:40:13 -07001954 }
1955 storeValue(cUnit, rlDest, rlResult);
1956 return true;
1957}
1958
Elliott Hughesb4c05972010-02-24 16:36:18 -08001959// Returns true if it added instructions to 'cUnit' to multiply 'rlSrc' by 'lit'
1960// and store the result in 'rlDest'.
1961static bool handleEasyMultiply(CompilationUnit *cUnit,
1962 RegLocation rlSrc, RegLocation rlDest, int lit)
1963{
1964 // Can we simplify this multiplication?
1965 bool powerOfTwo = false;
1966 bool popCountLE2 = false;
1967 bool powerOfTwoMinusOne = false;
1968 if (lit < 2) {
1969 // Avoid special cases.
1970 return false;
1971 } else if (isPowerOfTwo(lit)) {
1972 powerOfTwo = true;
1973 } else if (isPopCountLE2(lit)) {
1974 popCountLE2 = true;
1975 } else if (isPowerOfTwo(lit + 1)) {
1976 powerOfTwoMinusOne = true;
1977 } else {
1978 return false;
1979 }
1980 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
1981 RegLocation rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
1982 if (powerOfTwo) {
1983 // Shift.
1984 opRegRegImm(cUnit, kOpLsl, rlResult.lowReg, rlSrc.lowReg,
1985 lowestSetBit(lit));
1986 } else if (popCountLE2) {
1987 // Shift and add and shift.
1988 int firstBit = lowestSetBit(lit);
1989 int secondBit = lowestSetBit(lit ^ (1 << firstBit));
1990 genMultiplyByTwoBitMultiplier(cUnit, rlSrc, rlResult, lit,
1991 firstBit, secondBit);
1992 } else {
1993 // Reverse subtract: (src << (shift + 1)) - src.
1994 assert(powerOfTwoMinusOne);
1995 // TODO: rsb dst, src, src lsl#lowestSetBit(lit + 1)
1996 int tReg = dvmCompilerAllocTemp(cUnit);
1997 opRegRegImm(cUnit, kOpLsl, tReg, rlSrc.lowReg, lowestSetBit(lit + 1));
1998 opRegRegReg(cUnit, kOpSub, rlResult.lowReg, tReg, rlSrc.lowReg);
1999 }
2000 storeValue(cUnit, rlDest, rlResult);
2001 return true;
Bill Buzbee78cb0e22010-02-11 14:04:53 -08002002}
2003
Ben Chengba4fc8b2009-06-01 13:00:29 -07002004static bool handleFmt22b_Fmt22s(CompilationUnit *cUnit, MIR *mir)
2005{
2006 OpCode dalvikOpCode = mir->dalvikInsn.opCode;
Bill Buzbeec6f10662010-02-09 11:16:15 -08002007 RegLocation rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
2008 RegLocation rlDest = dvmCompilerGetDest(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07002009 RegLocation rlResult;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002010 int lit = mir->dalvikInsn.vC;
Ben Cheng4f489172009-09-27 17:08:35 -07002011 OpKind op = 0; /* Make gcc happy */
Bill Buzbee1465db52009-09-23 17:17:35 -07002012 int shiftOp = false;
2013 bool isDiv = false;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002014
Ben Chengba4fc8b2009-06-01 13:00:29 -07002015 switch (dalvikOpCode) {
Bill Buzbee1465db52009-09-23 17:17:35 -07002016 case OP_RSUB_INT_LIT8:
2017 case OP_RSUB_INT: {
2018 int tReg;
2019 //TUNING: add support for use of Arm rsub op
2020 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
Bill Buzbeec6f10662010-02-09 11:16:15 -08002021 tReg = dvmCompilerAllocTemp(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07002022 loadConstant(cUnit, tReg, lit);
Bill Buzbeec6f10662010-02-09 11:16:15 -08002023 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Bill Buzbee1465db52009-09-23 17:17:35 -07002024 opRegRegReg(cUnit, kOpSub, rlResult.lowReg,
2025 tReg, rlSrc.lowReg);
2026 storeValue(cUnit, rlDest, rlResult);
2027 return false;
2028 break;
2029 }
2030
Ben Chengba4fc8b2009-06-01 13:00:29 -07002031 case OP_ADD_INT_LIT8:
2032 case OP_ADD_INT_LIT16:
Bill Buzbee1465db52009-09-23 17:17:35 -07002033 op = kOpAdd;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002034 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002035 case OP_MUL_INT_LIT8:
Bill Buzbee78cb0e22010-02-11 14:04:53 -08002036 case OP_MUL_INT_LIT16: {
Elliott Hughesb4c05972010-02-24 16:36:18 -08002037 if (handleEasyMultiply(cUnit, rlSrc, rlDest, lit)) {
2038 return false;
Bill Buzbee78cb0e22010-02-11 14:04:53 -08002039 }
Elliott Hughesb4c05972010-02-24 16:36:18 -08002040 op = kOpMul;
Bill Buzbee1465db52009-09-23 17:17:35 -07002041 break;
Bill Buzbee78cb0e22010-02-11 14:04:53 -08002042 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07002043 case OP_AND_INT_LIT8:
2044 case OP_AND_INT_LIT16:
Bill Buzbee1465db52009-09-23 17:17:35 -07002045 op = kOpAnd;
2046 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002047 case OP_OR_INT_LIT8:
2048 case OP_OR_INT_LIT16:
Bill Buzbee1465db52009-09-23 17:17:35 -07002049 op = kOpOr;
2050 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002051 case OP_XOR_INT_LIT8:
2052 case OP_XOR_INT_LIT16:
Bill Buzbee1465db52009-09-23 17:17:35 -07002053 op = kOpXor;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002054 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002055 case OP_SHL_INT_LIT8:
Bill Buzbee0e605272009-12-01 14:28:05 -08002056 lit &= 31;
Bill Buzbee1465db52009-09-23 17:17:35 -07002057 shiftOp = true;
2058 op = kOpLsl;
2059 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002060 case OP_SHR_INT_LIT8:
Bill Buzbee0e605272009-12-01 14:28:05 -08002061 lit &= 31;
Bill Buzbee1465db52009-09-23 17:17:35 -07002062 shiftOp = true;
2063 op = kOpAsr;
2064 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002065 case OP_USHR_INT_LIT8:
Bill Buzbee0e605272009-12-01 14:28:05 -08002066 lit &= 31;
Bill Buzbee1465db52009-09-23 17:17:35 -07002067 shiftOp = true;
2068 op = kOpLsr;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002069 break;
2070
2071 case OP_DIV_INT_LIT8:
2072 case OP_DIV_INT_LIT16:
Ben Chengba4fc8b2009-06-01 13:00:29 -07002073 case OP_REM_INT_LIT8:
2074 case OP_REM_INT_LIT16:
2075 if (lit == 0) {
2076 /* Let the interpreter deal with div by 0 */
2077 genInterpSingleStep(cUnit, mir);
2078 return false;
2079 }
Elliott Hughesc7ad9b22010-04-28 13:52:02 -07002080 if (handleEasyDivide(cUnit, dalvikOpCode, rlSrc, rlDest, lit)) {
Elliott Hughes672511b2010-04-26 17:40:13 -07002081 return false;
2082 }
Bill Buzbeec6f10662010-02-09 11:16:15 -08002083 dvmCompilerFlushAllRegs(cUnit); /* Everything to home location */
Bill Buzbee1465db52009-09-23 17:17:35 -07002084 loadValueDirectFixed(cUnit, rlSrc, r0);
Bill Buzbeec6f10662010-02-09 11:16:15 -08002085 dvmCompilerClobber(cUnit, r0);
Bill Buzbee1465db52009-09-23 17:17:35 -07002086 if ((dalvikOpCode == OP_DIV_INT_LIT8) ||
2087 (dalvikOpCode == OP_DIV_INT_LIT16)) {
Ben Chengbd1326d2010-04-02 15:04:53 -07002088 LOAD_FUNC_ADDR(cUnit, r2, (int)__aeabi_idiv);
Bill Buzbee1465db52009-09-23 17:17:35 -07002089 isDiv = true;
2090 } else {
Ben Chengbd1326d2010-04-02 15:04:53 -07002091 LOAD_FUNC_ADDR(cUnit, r2, (int)__aeabi_idivmod);
Bill Buzbee1465db52009-09-23 17:17:35 -07002092 isDiv = false;
2093 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07002094 loadConstant(cUnit, r1, lit);
Bill Buzbee1465db52009-09-23 17:17:35 -07002095 opReg(cUnit, kOpBlx, r2);
Elliott Hughes6a555132010-02-25 15:41:42 -08002096 dvmCompilerClobberCallRegs(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07002097 if (isDiv)
Bill Buzbeec6f10662010-02-09 11:16:15 -08002098 rlResult = dvmCompilerGetReturn(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07002099 else
Bill Buzbeec6f10662010-02-09 11:16:15 -08002100 rlResult = dvmCompilerGetReturnAlt(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07002101 storeValue(cUnit, rlDest, rlResult);
2102 return false;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002103 break;
2104 default:
2105 return true;
2106 }
Bill Buzbee1465db52009-09-23 17:17:35 -07002107 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
Bill Buzbeec6f10662010-02-09 11:16:15 -08002108 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Bill Buzbee1465db52009-09-23 17:17:35 -07002109 // Avoid shifts by literal 0 - no support in Thumb. Change to copy
2110 if (shiftOp && (lit == 0)) {
2111 genRegCopy(cUnit, rlResult.lowReg, rlSrc.lowReg);
2112 } else {
2113 opRegRegImm(cUnit, op, rlResult.lowReg, rlSrc.lowReg, lit);
2114 }
2115 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002116 return false;
2117}
2118
2119static bool handleFmt22c(CompilationUnit *cUnit, MIR *mir)
2120{
2121 OpCode dalvikOpCode = mir->dalvikInsn.opCode;
2122 int fieldOffset;
2123
2124 if (dalvikOpCode >= OP_IGET && dalvikOpCode <= OP_IPUT_SHORT) {
2125 InstField *pInstField = (InstField *)
2126 cUnit->method->clazz->pDvmDex->pResFields[mir->dalvikInsn.vC];
Ben Chengba4fc8b2009-06-01 13:00:29 -07002127
Ben Chengdd6e8702010-05-07 13:05:47 -07002128 if (pInstField == NULL) {
2129 LOGE("Unexpected null instance field");
2130 dvmAbort();
2131 }
2132
Ben Chengba4fc8b2009-06-01 13:00:29 -07002133 fieldOffset = pInstField->byteOffset;
2134 } else {
Ben Chenga0e7b602009-10-13 23:09:01 -07002135 /* Deliberately break the code while make the compiler happy */
2136 fieldOffset = -1;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002137 }
2138 switch (dalvikOpCode) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07002139 case OP_NEW_ARRAY: {
Bill Buzbee1465db52009-09-23 17:17:35 -07002140 // Generates a call - use explicit registers
Bill Buzbeec6f10662010-02-09 11:16:15 -08002141 RegLocation rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
2142 RegLocation rlDest = dvmCompilerGetDest(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07002143 RegLocation rlResult;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002144 void *classPtr = (void*)
2145 (cUnit->method->clazz->pDvmDex->pResClasses[mir->dalvikInsn.vC]);
Ben Chengdd6e8702010-05-07 13:05:47 -07002146
2147 if (classPtr == NULL) {
2148 LOGE("Unexpected null class");
2149 dvmAbort();
2150 }
2151
Bill Buzbeec6f10662010-02-09 11:16:15 -08002152 dvmCompilerFlushAllRegs(cUnit); /* Everything to home location */
Bill Buzbee1465db52009-09-23 17:17:35 -07002153 genExportPC(cUnit, mir);
2154 loadValueDirectFixed(cUnit, rlSrc, r1); /* Len */
Ben Chengba4fc8b2009-06-01 13:00:29 -07002155 loadConstant(cUnit, r0, (int) classPtr );
Ben Chengbd1326d2010-04-02 15:04:53 -07002156 LOAD_FUNC_ADDR(cUnit, r3, (int)dvmAllocArrayByClass);
Ben Cheng4f489172009-09-27 17:08:35 -07002157 /*
2158 * "len < 0": bail to the interpreter to re-execute the
2159 * instruction
2160 */
Carl Shapiroe3c01da2010-05-20 22:54:18 -07002161 genRegImmCheck(cUnit, kArmCondMi, r1, 0, mir->offset, NULL);
Bill Buzbee270c1d62009-08-13 16:58:07 -07002162 loadConstant(cUnit, r2, ALLOC_DONT_TRACK);
Bill Buzbee1465db52009-09-23 17:17:35 -07002163 opReg(cUnit, kOpBlx, r3);
Elliott Hughes6a555132010-02-25 15:41:42 -08002164 dvmCompilerClobberCallRegs(cUnit);
Ben Cheng4f489172009-09-27 17:08:35 -07002165 /* generate a branch over if allocation is successful */
Bill Buzbee1465db52009-09-23 17:17:35 -07002166 opRegImm(cUnit, kOpCmp, r0, 0); /* NULL? */
2167 ArmLIR *branchOver = opCondBranch(cUnit, kArmCondNe);
Ben Cheng4f489172009-09-27 17:08:35 -07002168 /*
2169 * OOM exception needs to be thrown here and cannot re-execute
2170 */
2171 loadConstant(cUnit, r0,
2172 (int) (cUnit->method->insns + mir->offset));
2173 genDispatchToHandler(cUnit, TEMPLATE_THROW_EXCEPTION_COMMON);
2174 /* noreturn */
2175
Bill Buzbee1465db52009-09-23 17:17:35 -07002176 ArmLIR *target = newLIR0(cUnit, kArmPseudoTargetLabel);
Ben Cheng4f489172009-09-27 17:08:35 -07002177 target->defMask = ENCODE_ALL;
2178 branchOver->generic.target = (LIR *) target;
Bill Buzbeec6f10662010-02-09 11:16:15 -08002179 rlResult = dvmCompilerGetReturn(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07002180 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002181 break;
2182 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07002183 case OP_INSTANCE_OF: {
Bill Buzbee1465db52009-09-23 17:17:35 -07002184 // May generate a call - use explicit registers
Bill Buzbeec6f10662010-02-09 11:16:15 -08002185 RegLocation rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
2186 RegLocation rlDest = dvmCompilerGetDest(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07002187 RegLocation rlResult;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002188 ClassObject *classPtr =
2189 (cUnit->method->clazz->pDvmDex->pResClasses[mir->dalvikInsn.vC]);
Bill Buzbee480e6782010-01-27 15:43:08 -08002190 /*
2191 * Note: It is possible that classPtr is NULL at this point,
2192 * even though this instruction has been successfully interpreted.
2193 * If the previous interpretation had a null source, the
2194 * interpreter would not have bothered to resolve the clazz.
2195 * Bail out to the interpreter in this case, and log it
2196 * so that we can tell if it happens frequently.
2197 */
2198 if (classPtr == NULL) {
2199 LOGD("null clazz in OP_INSTANCE_OF, single-stepping");
2200 genInterpSingleStep(cUnit, mir);
2201 break;
2202 }
Bill Buzbeec6f10662010-02-09 11:16:15 -08002203 dvmCompilerFlushAllRegs(cUnit); /* Everything to home location */
Bill Buzbee1465db52009-09-23 17:17:35 -07002204 loadValueDirectFixed(cUnit, rlSrc, r0); /* Ref */
Ben Chengba4fc8b2009-06-01 13:00:29 -07002205 loadConstant(cUnit, r2, (int) classPtr );
Bill Buzbee270c1d62009-08-13 16:58:07 -07002206//TUNING: compare to 0 primative to allow use of CB[N]Z
Bill Buzbee1465db52009-09-23 17:17:35 -07002207 opRegImm(cUnit, kOpCmp, r0, 0); /* NULL? */
Ben Cheng752c7942009-06-22 10:50:07 -07002208 /* When taken r0 has NULL which can be used for store directly */
Bill Buzbee1465db52009-09-23 17:17:35 -07002209 ArmLIR *branch1 = opCondBranch(cUnit, kArmCondEq);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002210 /* r1 now contains object->clazz */
Bill Buzbee270c1d62009-08-13 16:58:07 -07002211 loadWordDisp(cUnit, r0, offsetof(Object, clazz), r1);
Bill Buzbee1465db52009-09-23 17:17:35 -07002212 /* r1 now contains object->clazz */
Ben Chengbd1326d2010-04-02 15:04:53 -07002213 LOAD_FUNC_ADDR(cUnit, r3, (int)dvmInstanceofNonTrivial);
Ben Cheng752c7942009-06-22 10:50:07 -07002214 loadConstant(cUnit, r0, 1); /* Assume true */
Bill Buzbee1465db52009-09-23 17:17:35 -07002215 opRegReg(cUnit, kOpCmp, r1, r2);
2216 ArmLIR *branch2 = opCondBranch(cUnit, kArmCondEq);
2217 genRegCopy(cUnit, r0, r1);
2218 genRegCopy(cUnit, r1, r2);
2219 opReg(cUnit, kOpBlx, r3);
Elliott Hughes6a555132010-02-25 15:41:42 -08002220 dvmCompilerClobberCallRegs(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002221 /* branch target here */
Bill Buzbee1465db52009-09-23 17:17:35 -07002222 ArmLIR *target = newLIR0(cUnit, kArmPseudoTargetLabel);
Ben Chengd7d426a2009-09-22 11:23:36 -07002223 target->defMask = ENCODE_ALL;
Bill Buzbeec6f10662010-02-09 11:16:15 -08002224 rlResult = dvmCompilerGetReturn(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07002225 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002226 branch1->generic.target = (LIR *)target;
2227 branch2->generic.target = (LIR *)target;
2228 break;
2229 }
2230 case OP_IGET_WIDE:
2231 genIGetWide(cUnit, mir, fieldOffset);
2232 break;
2233 case OP_IGET:
2234 case OP_IGET_OBJECT:
Bill Buzbee1465db52009-09-23 17:17:35 -07002235 genIGet(cUnit, mir, kWord, fieldOffset);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002236 break;
2237 case OP_IGET_BOOLEAN:
Bill Buzbee1465db52009-09-23 17:17:35 -07002238 genIGet(cUnit, mir, kUnsignedByte, fieldOffset);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002239 break;
2240 case OP_IGET_BYTE:
Bill Buzbee1465db52009-09-23 17:17:35 -07002241 genIGet(cUnit, mir, kSignedByte, fieldOffset);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002242 break;
2243 case OP_IGET_CHAR:
Bill Buzbee1465db52009-09-23 17:17:35 -07002244 genIGet(cUnit, mir, kUnsignedHalf, fieldOffset);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002245 break;
2246 case OP_IGET_SHORT:
Bill Buzbee1465db52009-09-23 17:17:35 -07002247 genIGet(cUnit, mir, kSignedHalf, fieldOffset);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002248 break;
2249 case OP_IPUT_WIDE:
2250 genIPutWide(cUnit, mir, fieldOffset);
2251 break;
2252 case OP_IPUT:
buzbee919eb062010-07-12 12:59:22 -07002253 genIPut(cUnit, mir, kWord, fieldOffset, false);
2254 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002255 case OP_IPUT_OBJECT:
buzbee919eb062010-07-12 12:59:22 -07002256 genIPut(cUnit, mir, kWord, fieldOffset, true);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002257 break;
2258 case OP_IPUT_SHORT:
2259 case OP_IPUT_CHAR:
buzbee919eb062010-07-12 12:59:22 -07002260 genIPut(cUnit, mir, kUnsignedHalf, fieldOffset, false);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002261 break;
2262 case OP_IPUT_BYTE:
2263 case OP_IPUT_BOOLEAN:
buzbee919eb062010-07-12 12:59:22 -07002264 genIPut(cUnit, mir, kUnsignedByte, fieldOffset, false);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002265 break;
Bill Buzbeeb16344a2010-03-15 17:19:12 -07002266 case OP_IGET_WIDE_VOLATILE:
2267 case OP_IPUT_WIDE_VOLATILE:
2268 case OP_SGET_WIDE_VOLATILE:
2269 case OP_SPUT_WIDE_VOLATILE:
2270 genInterpSingleStep(cUnit, mir);
2271 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002272 default:
2273 return true;
2274 }
2275 return false;
2276}
2277
2278static bool handleFmt22cs(CompilationUnit *cUnit, MIR *mir)
2279{
2280 OpCode dalvikOpCode = mir->dalvikInsn.opCode;
2281 int fieldOffset = mir->dalvikInsn.vC;
2282 switch (dalvikOpCode) {
2283 case OP_IGET_QUICK:
2284 case OP_IGET_OBJECT_QUICK:
Bill Buzbee1465db52009-09-23 17:17:35 -07002285 genIGet(cUnit, mir, kWord, fieldOffset);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002286 break;
2287 case OP_IPUT_QUICK:
buzbee919eb062010-07-12 12:59:22 -07002288 genIPut(cUnit, mir, kWord, fieldOffset, false);
2289 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002290 case OP_IPUT_OBJECT_QUICK:
buzbee919eb062010-07-12 12:59:22 -07002291 genIPut(cUnit, mir, kWord, fieldOffset, true);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002292 break;
2293 case OP_IGET_WIDE_QUICK:
2294 genIGetWide(cUnit, mir, fieldOffset);
2295 break;
2296 case OP_IPUT_WIDE_QUICK:
2297 genIPutWide(cUnit, mir, fieldOffset);
2298 break;
2299 default:
2300 return true;
2301 }
2302 return false;
2303
2304}
2305
2306/* Compare agaist zero */
2307static bool handleFmt22t(CompilationUnit *cUnit, MIR *mir, BasicBlock *bb,
Bill Buzbee89efc3d2009-07-28 11:22:22 -07002308 ArmLIR *labelList)
Ben Chengba4fc8b2009-06-01 13:00:29 -07002309{
2310 OpCode dalvikOpCode = mir->dalvikInsn.opCode;
Bill Buzbee89efc3d2009-07-28 11:22:22 -07002311 ArmConditionCode cond;
Bill Buzbeec6f10662010-02-09 11:16:15 -08002312 RegLocation rlSrc1 = dvmCompilerGetSrc(cUnit, mir, 0);
2313 RegLocation rlSrc2 = dvmCompilerGetSrc(cUnit, mir, 1);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002314
Bill Buzbee1465db52009-09-23 17:17:35 -07002315 rlSrc1 = loadValue(cUnit, rlSrc1, kCoreReg);
2316 rlSrc2 = loadValue(cUnit, rlSrc2, kCoreReg);
2317 opRegReg(cUnit, kOpCmp, rlSrc1.lowReg, rlSrc2.lowReg);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002318
2319 switch (dalvikOpCode) {
2320 case OP_IF_EQ:
Bill Buzbee1465db52009-09-23 17:17:35 -07002321 cond = kArmCondEq;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002322 break;
2323 case OP_IF_NE:
Bill Buzbee1465db52009-09-23 17:17:35 -07002324 cond = kArmCondNe;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002325 break;
2326 case OP_IF_LT:
Bill Buzbee1465db52009-09-23 17:17:35 -07002327 cond = kArmCondLt;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002328 break;
2329 case OP_IF_GE:
Bill Buzbee1465db52009-09-23 17:17:35 -07002330 cond = kArmCondGe;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002331 break;
2332 case OP_IF_GT:
Bill Buzbee1465db52009-09-23 17:17:35 -07002333 cond = kArmCondGt;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002334 break;
2335 case OP_IF_LE:
Bill Buzbee1465db52009-09-23 17:17:35 -07002336 cond = kArmCondLe;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002337 break;
2338 default:
2339 cond = 0;
2340 LOGE("Unexpected opcode (%d) for Fmt22t\n", dalvikOpCode);
Bill Buzbeefc519dc2010-03-06 23:30:57 -08002341 dvmCompilerAbort(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002342 }
2343 genConditionalBranch(cUnit, cond, &labelList[bb->taken->id]);
2344 /* This mostly likely will be optimized away in a later phase */
2345 genUnconditionalBranch(cUnit, &labelList[bb->fallThrough->id]);
2346 return false;
2347}
2348
2349static bool handleFmt22x_Fmt32x(CompilationUnit *cUnit, MIR *mir)
2350{
2351 OpCode opCode = mir->dalvikInsn.opCode;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002352
2353 switch (opCode) {
2354 case OP_MOVE_16:
2355 case OP_MOVE_OBJECT_16:
2356 case OP_MOVE_FROM16:
Ben Chenge9695e52009-06-16 16:11:47 -07002357 case OP_MOVE_OBJECT_FROM16: {
Bill Buzbeec6f10662010-02-09 11:16:15 -08002358 storeValue(cUnit, dvmCompilerGetDest(cUnit, mir, 0),
2359 dvmCompilerGetSrc(cUnit, mir, 0));
Ben Chengba4fc8b2009-06-01 13:00:29 -07002360 break;
Ben Chenge9695e52009-06-16 16:11:47 -07002361 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07002362 case OP_MOVE_WIDE_16:
Ben Chenge9695e52009-06-16 16:11:47 -07002363 case OP_MOVE_WIDE_FROM16: {
Bill Buzbeec6f10662010-02-09 11:16:15 -08002364 storeValueWide(cUnit, dvmCompilerGetDestWide(cUnit, mir, 0, 1),
2365 dvmCompilerGetSrcWide(cUnit, mir, 0, 1));
Ben Chengba4fc8b2009-06-01 13:00:29 -07002366 break;
Ben Chenge9695e52009-06-16 16:11:47 -07002367 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07002368 default:
2369 return true;
2370 }
2371 return false;
2372}
2373
2374static bool handleFmt23x(CompilationUnit *cUnit, MIR *mir)
2375{
2376 OpCode opCode = mir->dalvikInsn.opCode;
Bill Buzbee1465db52009-09-23 17:17:35 -07002377 RegLocation rlSrc1;
2378 RegLocation rlSrc2;
2379 RegLocation rlDest;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002380
2381 if ( (opCode >= OP_ADD_INT) && (opCode <= OP_REM_DOUBLE)) {
Ben Cheng5d90c202009-11-22 23:31:11 -08002382 return genArithOp( cUnit, mir );
Ben Chengba4fc8b2009-06-01 13:00:29 -07002383 }
2384
Bill Buzbee1465db52009-09-23 17:17:35 -07002385 /* APUTs have 3 sources and no targets */
2386 if (mir->ssaRep->numDefs == 0) {
2387 if (mir->ssaRep->numUses == 3) {
Bill Buzbeec6f10662010-02-09 11:16:15 -08002388 rlDest = dvmCompilerGetSrc(cUnit, mir, 0);
2389 rlSrc1 = dvmCompilerGetSrc(cUnit, mir, 1);
2390 rlSrc2 = dvmCompilerGetSrc(cUnit, mir, 2);
Bill Buzbee1465db52009-09-23 17:17:35 -07002391 } else {
2392 assert(mir->ssaRep->numUses == 4);
Bill Buzbeec6f10662010-02-09 11:16:15 -08002393 rlDest = dvmCompilerGetSrcWide(cUnit, mir, 0, 1);
2394 rlSrc1 = dvmCompilerGetSrc(cUnit, mir, 2);
2395 rlSrc2 = dvmCompilerGetSrc(cUnit, mir, 3);
Bill Buzbee1465db52009-09-23 17:17:35 -07002396 }
2397 } else {
2398 /* Two sources and 1 dest. Deduce the operand sizes */
2399 if (mir->ssaRep->numUses == 4) {
Bill Buzbeec6f10662010-02-09 11:16:15 -08002400 rlSrc1 = dvmCompilerGetSrcWide(cUnit, mir, 0, 1);
2401 rlSrc2 = dvmCompilerGetSrcWide(cUnit, mir, 2, 3);
Bill Buzbee1465db52009-09-23 17:17:35 -07002402 } else {
2403 assert(mir->ssaRep->numUses == 2);
Bill Buzbeec6f10662010-02-09 11:16:15 -08002404 rlSrc1 = dvmCompilerGetSrc(cUnit, mir, 0);
2405 rlSrc2 = dvmCompilerGetSrc(cUnit, mir, 1);
Bill Buzbee1465db52009-09-23 17:17:35 -07002406 }
2407 if (mir->ssaRep->numDefs == 2) {
Bill Buzbeec6f10662010-02-09 11:16:15 -08002408 rlDest = dvmCompilerGetDestWide(cUnit, mir, 0, 1);
Bill Buzbee1465db52009-09-23 17:17:35 -07002409 } else {
2410 assert(mir->ssaRep->numDefs == 1);
Bill Buzbeec6f10662010-02-09 11:16:15 -08002411 rlDest = dvmCompilerGetDest(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07002412 }
2413 }
2414
2415
Ben Chengba4fc8b2009-06-01 13:00:29 -07002416 switch (opCode) {
Bill Buzbeed45ba372009-06-15 17:00:57 -07002417 case OP_CMPL_FLOAT:
2418 case OP_CMPG_FLOAT:
2419 case OP_CMPL_DOUBLE:
2420 case OP_CMPG_DOUBLE:
Ben Cheng5d90c202009-11-22 23:31:11 -08002421 return genCmpFP(cUnit, mir, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002422 case OP_CMP_LONG:
Bill Buzbee1465db52009-09-23 17:17:35 -07002423 genCmpLong(cUnit, mir, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002424 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002425 case OP_AGET_WIDE:
Bill Buzbee1465db52009-09-23 17:17:35 -07002426 genArrayGet(cUnit, mir, kLong, rlSrc1, rlSrc2, rlDest, 3);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002427 break;
2428 case OP_AGET:
2429 case OP_AGET_OBJECT:
Bill Buzbee1465db52009-09-23 17:17:35 -07002430 genArrayGet(cUnit, mir, kWord, rlSrc1, rlSrc2, rlDest, 2);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002431 break;
2432 case OP_AGET_BOOLEAN:
Bill Buzbee1465db52009-09-23 17:17:35 -07002433 genArrayGet(cUnit, mir, kUnsignedByte, rlSrc1, rlSrc2, rlDest, 0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002434 break;
2435 case OP_AGET_BYTE:
Bill Buzbee1465db52009-09-23 17:17:35 -07002436 genArrayGet(cUnit, mir, kSignedByte, rlSrc1, rlSrc2, rlDest, 0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002437 break;
2438 case OP_AGET_CHAR:
Bill Buzbee1465db52009-09-23 17:17:35 -07002439 genArrayGet(cUnit, mir, kUnsignedHalf, rlSrc1, rlSrc2, rlDest, 1);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002440 break;
2441 case OP_AGET_SHORT:
Bill Buzbee1465db52009-09-23 17:17:35 -07002442 genArrayGet(cUnit, mir, kSignedHalf, rlSrc1, rlSrc2, rlDest, 1);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002443 break;
2444 case OP_APUT_WIDE:
Bill Buzbee1465db52009-09-23 17:17:35 -07002445 genArrayPut(cUnit, mir, kLong, rlSrc1, rlSrc2, rlDest, 3);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002446 break;
2447 case OP_APUT:
Bill Buzbee1465db52009-09-23 17:17:35 -07002448 genArrayPut(cUnit, mir, kWord, rlSrc1, rlSrc2, rlDest, 2);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002449 break;
Bill Buzbeebe6534f2010-03-12 16:01:35 -08002450 case OP_APUT_OBJECT:
2451 genArrayObjectPut(cUnit, mir, rlSrc1, rlSrc2, rlDest, 2);
2452 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002453 case OP_APUT_SHORT:
2454 case OP_APUT_CHAR:
Bill Buzbee1465db52009-09-23 17:17:35 -07002455 genArrayPut(cUnit, mir, kUnsignedHalf, rlSrc1, rlSrc2, rlDest, 1);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002456 break;
2457 case OP_APUT_BYTE:
2458 case OP_APUT_BOOLEAN:
Bill Buzbee1465db52009-09-23 17:17:35 -07002459 genArrayPut(cUnit, mir, kUnsignedByte, rlSrc1, rlSrc2, rlDest, 0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002460 break;
2461 default:
2462 return true;
2463 }
2464 return false;
2465}
2466
Ben Cheng6c10a972009-10-29 14:39:18 -07002467/*
2468 * Find the matching case.
2469 *
2470 * return values:
2471 * r0 (low 32-bit): pc of the chaining cell corresponding to the resolved case,
2472 * including default which is placed at MIN(size, MAX_CHAINED_SWITCH_CASES).
2473 * r1 (high 32-bit): the branch offset of the matching case (only for indexes
2474 * above MAX_CHAINED_SWITCH_CASES).
2475 *
2476 * Instructions around the call are:
2477 *
2478 * mov r2, pc
2479 * blx &findPackedSwitchIndex
2480 * mov pc, r0
2481 * .align4
Bill Buzbeebd047242010-05-13 13:02:53 -07002482 * chaining cell for case 0 [12 bytes]
2483 * chaining cell for case 1 [12 bytes]
Ben Cheng6c10a972009-10-29 14:39:18 -07002484 * :
Bill Buzbeebd047242010-05-13 13:02:53 -07002485 * chaining cell for case MIN(size, MAX_CHAINED_SWITCH_CASES)-1 [12 bytes]
Ben Cheng6c10a972009-10-29 14:39:18 -07002486 * chaining cell for case default [8 bytes]
2487 * noChain exit
2488 */
Ben Chengbd1326d2010-04-02 15:04:53 -07002489static s8 findPackedSwitchIndex(const u2* switchData, int testVal, int pc)
Ben Cheng6c10a972009-10-29 14:39:18 -07002490{
2491 int size;
2492 int firstKey;
2493 const int *entries;
2494 int index;
2495 int jumpIndex;
2496 int caseDPCOffset = 0;
2497 /* In Thumb mode pc is 4 ahead of the "mov r2, pc" instruction */
2498 int chainingPC = (pc + 4) & ~3;
2499
2500 /*
2501 * Packed switch data format:
2502 * ushort ident = 0x0100 magic value
2503 * ushort size number of entries in the table
2504 * int first_key first (and lowest) switch case value
2505 * int targets[size] branch targets, relative to switch opcode
2506 *
2507 * Total size is (4+size*2) 16-bit code units.
2508 */
2509 size = switchData[1];
2510 assert(size > 0);
2511
2512 firstKey = switchData[2];
2513 firstKey |= switchData[3] << 16;
2514
2515
2516 /* The entries are guaranteed to be aligned on a 32-bit boundary;
2517 * we can treat them as a native int array.
2518 */
2519 entries = (const int*) &switchData[4];
2520 assert(((u4)entries & 0x3) == 0);
2521
2522 index = testVal - firstKey;
2523
2524 /* Jump to the default cell */
2525 if (index < 0 || index >= size) {
2526 jumpIndex = MIN(size, MAX_CHAINED_SWITCH_CASES);
2527 /* Jump to the non-chaining exit point */
2528 } else if (index >= MAX_CHAINED_SWITCH_CASES) {
2529 jumpIndex = MAX_CHAINED_SWITCH_CASES + 1;
2530 caseDPCOffset = entries[index];
2531 /* Jump to the inline chaining cell */
2532 } else {
2533 jumpIndex = index;
2534 }
2535
Bill Buzbeebd047242010-05-13 13:02:53 -07002536 chainingPC += jumpIndex * CHAIN_CELL_NORMAL_SIZE;
Ben Cheng6c10a972009-10-29 14:39:18 -07002537 return (((s8) caseDPCOffset) << 32) | (u8) chainingPC;
2538}
2539
2540/* See comments for findPackedSwitchIndex */
Ben Chengbd1326d2010-04-02 15:04:53 -07002541static s8 findSparseSwitchIndex(const u2* switchData, int testVal, int pc)
Ben Cheng6c10a972009-10-29 14:39:18 -07002542{
2543 int size;
2544 const int *keys;
2545 const int *entries;
2546 int chainingPC = (pc + 4) & ~3;
2547 int i;
2548
2549 /*
2550 * Sparse switch data format:
2551 * ushort ident = 0x0200 magic value
2552 * ushort size number of entries in the table; > 0
2553 * int keys[size] keys, sorted low-to-high; 32-bit aligned
2554 * int targets[size] branch targets, relative to switch opcode
2555 *
2556 * Total size is (2+size*4) 16-bit code units.
2557 */
2558
2559 size = switchData[1];
2560 assert(size > 0);
2561
2562 /* The keys are guaranteed to be aligned on a 32-bit boundary;
2563 * we can treat them as a native int array.
2564 */
2565 keys = (const int*) &switchData[2];
2566 assert(((u4)keys & 0x3) == 0);
2567
2568 /* The entries are guaranteed to be aligned on a 32-bit boundary;
2569 * we can treat them as a native int array.
2570 */
2571 entries = keys + size;
2572 assert(((u4)entries & 0x3) == 0);
2573
2574 /*
2575 * Run through the list of keys, which are guaranteed to
2576 * be sorted low-to-high.
2577 *
2578 * Most tables have 3-4 entries. Few have more than 10. A binary
2579 * search here is probably not useful.
2580 */
2581 for (i = 0; i < size; i++) {
2582 int k = keys[i];
2583 if (k == testVal) {
2584 /* MAX_CHAINED_SWITCH_CASES + 1 is the start of the overflow case */
2585 int jumpIndex = (i < MAX_CHAINED_SWITCH_CASES) ?
2586 i : MAX_CHAINED_SWITCH_CASES + 1;
Bill Buzbeebd047242010-05-13 13:02:53 -07002587 chainingPC += jumpIndex * CHAIN_CELL_NORMAL_SIZE;
Ben Cheng6c10a972009-10-29 14:39:18 -07002588 return (((s8) entries[i]) << 32) | (u8) chainingPC;
2589 } else if (k > testVal) {
2590 break;
2591 }
2592 }
Bill Buzbeebd047242010-05-13 13:02:53 -07002593 return chainingPC + MIN(size, MAX_CHAINED_SWITCH_CASES) *
2594 CHAIN_CELL_NORMAL_SIZE;
Ben Cheng6c10a972009-10-29 14:39:18 -07002595}
2596
Ben Chengba4fc8b2009-06-01 13:00:29 -07002597static bool handleFmt31t(CompilationUnit *cUnit, MIR *mir)
2598{
2599 OpCode dalvikOpCode = mir->dalvikInsn.opCode;
2600 switch (dalvikOpCode) {
2601 case OP_FILL_ARRAY_DATA: {
Bill Buzbeec6f10662010-02-09 11:16:15 -08002602 RegLocation rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07002603 // Making a call - use explicit registers
Bill Buzbeec6f10662010-02-09 11:16:15 -08002604 dvmCompilerFlushAllRegs(cUnit); /* Everything to home location */
Bill Buzbee1465db52009-09-23 17:17:35 -07002605 genExportPC(cUnit, mir);
2606 loadValueDirectFixed(cUnit, rlSrc, r0);
Ben Chengbd1326d2010-04-02 15:04:53 -07002607 LOAD_FUNC_ADDR(cUnit, r2, (int)dvmInterpHandleFillArrayData);
Ben Cheng6c10a972009-10-29 14:39:18 -07002608 loadConstant(cUnit, r1,
2609 (int) (cUnit->method->insns + mir->offset + mir->dalvikInsn.vB));
Bill Buzbee1465db52009-09-23 17:17:35 -07002610 opReg(cUnit, kOpBlx, r2);
Elliott Hughes6a555132010-02-25 15:41:42 -08002611 dvmCompilerClobberCallRegs(cUnit);
Bill Buzbeefd023aa2009-11-02 09:23:49 -08002612 /* generate a branch over if successful */
2613 opRegImm(cUnit, kOpCmp, r0, 0); /* NULL? */
2614 ArmLIR *branchOver = opCondBranch(cUnit, kArmCondNe);
2615 loadConstant(cUnit, r0,
2616 (int) (cUnit->method->insns + mir->offset));
2617 genDispatchToHandler(cUnit, TEMPLATE_THROW_EXCEPTION_COMMON);
2618 ArmLIR *target = newLIR0(cUnit, kArmPseudoTargetLabel);
2619 target->defMask = ENCODE_ALL;
2620 branchOver->generic.target = (LIR *) target;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002621 break;
2622 }
2623 /*
Ben Cheng6c10a972009-10-29 14:39:18 -07002624 * Compute the goto target of up to
2625 * MIN(switchSize, MAX_CHAINED_SWITCH_CASES) + 1 chaining cells.
2626 * See the comment before findPackedSwitchIndex for the code layout.
Ben Chengba4fc8b2009-06-01 13:00:29 -07002627 */
2628 case OP_PACKED_SWITCH:
2629 case OP_SPARSE_SWITCH: {
Bill Buzbeec6f10662010-02-09 11:16:15 -08002630 RegLocation rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
2631 dvmCompilerFlushAllRegs(cUnit); /* Everything to home location */
Bill Buzbee1465db52009-09-23 17:17:35 -07002632 loadValueDirectFixed(cUnit, rlSrc, r1);
Bill Buzbeec6f10662010-02-09 11:16:15 -08002633 dvmCompilerLockAllTemps(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002634 if (dalvikOpCode == OP_PACKED_SWITCH) {
Ben Chengbd1326d2010-04-02 15:04:53 -07002635 LOAD_FUNC_ADDR(cUnit, r4PC, (int)findPackedSwitchIndex);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002636 } else {
Ben Chengbd1326d2010-04-02 15:04:53 -07002637 LOAD_FUNC_ADDR(cUnit, r4PC, (int)findSparseSwitchIndex);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002638 }
Ben Cheng6c10a972009-10-29 14:39:18 -07002639 /* r0 <- Addr of the switch data */
2640 loadConstant(cUnit, r0,
2641 (int) (cUnit->method->insns + mir->offset + mir->dalvikInsn.vB));
2642 /* r2 <- pc of the instruction following the blx */
2643 opRegReg(cUnit, kOpMov, r2, rpc);
Bill Buzbee1465db52009-09-23 17:17:35 -07002644 opReg(cUnit, kOpBlx, r4PC);
Elliott Hughes6a555132010-02-25 15:41:42 -08002645 dvmCompilerClobberCallRegs(cUnit);
Ben Cheng6c10a972009-10-29 14:39:18 -07002646 /* pc <- computed goto target */
2647 opRegReg(cUnit, kOpMov, rpc, r0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002648 break;
2649 }
2650 default:
2651 return true;
2652 }
2653 return false;
2654}
2655
2656static bool handleFmt35c_3rc(CompilationUnit *cUnit, MIR *mir, BasicBlock *bb,
Bill Buzbee89efc3d2009-07-28 11:22:22 -07002657 ArmLIR *labelList)
Ben Chengba4fc8b2009-06-01 13:00:29 -07002658{
Bill Buzbee9bc3df32009-07-30 10:52:29 -07002659 ArmLIR *retChainingCell = NULL;
Bill Buzbee89efc3d2009-07-28 11:22:22 -07002660 ArmLIR *pcrLabel = NULL;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002661
Bill Buzbeef4ce16f2009-07-28 13:28:25 -07002662 if (bb->fallThrough != NULL)
2663 retChainingCell = &labelList[bb->fallThrough->id];
2664
Ben Chengba4fc8b2009-06-01 13:00:29 -07002665 DecodedInstruction *dInsn = &mir->dalvikInsn;
2666 switch (mir->dalvikInsn.opCode) {
2667 /*
2668 * calleeMethod = this->clazz->vtable[
2669 * method->clazz->pDvmDex->pResMethods[BBBB]->methodIndex
2670 * ]
2671 */
2672 case OP_INVOKE_VIRTUAL:
2673 case OP_INVOKE_VIRTUAL_RANGE: {
Bill Buzbee89efc3d2009-07-28 11:22:22 -07002674 ArmLIR *predChainingCell = &labelList[bb->taken->id];
Ben Chengba4fc8b2009-06-01 13:00:29 -07002675 int methodIndex =
2676 cUnit->method->clazz->pDvmDex->pResMethods[dInsn->vB]->
2677 methodIndex;
2678
2679 if (mir->dalvikInsn.opCode == OP_INVOKE_VIRTUAL)
2680 genProcessArgsNoRange(cUnit, mir, dInsn, &pcrLabel);
2681 else
2682 genProcessArgsRange(cUnit, mir, dInsn, &pcrLabel);
2683
Ben Cheng38329f52009-07-07 14:19:20 -07002684 genInvokeVirtualCommon(cUnit, mir, methodIndex,
2685 retChainingCell,
2686 predChainingCell,
2687 pcrLabel);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002688 break;
2689 }
2690 /*
2691 * calleeMethod = method->clazz->super->vtable[method->clazz->pDvmDex
2692 * ->pResMethods[BBBB]->methodIndex]
2693 */
Ben Chengba4fc8b2009-06-01 13:00:29 -07002694 case OP_INVOKE_SUPER:
2695 case OP_INVOKE_SUPER_RANGE: {
2696 int mIndex = cUnit->method->clazz->pDvmDex->
2697 pResMethods[dInsn->vB]->methodIndex;
2698 const Method *calleeMethod =
2699 cUnit->method->clazz->super->vtable[mIndex];
2700
2701 if (mir->dalvikInsn.opCode == OP_INVOKE_SUPER)
2702 genProcessArgsNoRange(cUnit, mir, dInsn, &pcrLabel);
2703 else
2704 genProcessArgsRange(cUnit, mir, dInsn, &pcrLabel);
2705
2706 /* r0 = calleeMethod */
2707 loadConstant(cUnit, r0, (int) calleeMethod);
2708
Ben Cheng38329f52009-07-07 14:19:20 -07002709 genInvokeSingletonCommon(cUnit, mir, bb, labelList, pcrLabel,
2710 calleeMethod);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002711 break;
2712 }
2713 /* calleeMethod = method->clazz->pDvmDex->pResMethods[BBBB] */
2714 case OP_INVOKE_DIRECT:
2715 case OP_INVOKE_DIRECT_RANGE: {
2716 const Method *calleeMethod =
2717 cUnit->method->clazz->pDvmDex->pResMethods[dInsn->vB];
2718
2719 if (mir->dalvikInsn.opCode == OP_INVOKE_DIRECT)
2720 genProcessArgsNoRange(cUnit, mir, dInsn, &pcrLabel);
2721 else
2722 genProcessArgsRange(cUnit, mir, dInsn, &pcrLabel);
2723
2724 /* r0 = calleeMethod */
2725 loadConstant(cUnit, r0, (int) calleeMethod);
2726
Ben Cheng38329f52009-07-07 14:19:20 -07002727 genInvokeSingletonCommon(cUnit, mir, bb, labelList, pcrLabel,
2728 calleeMethod);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002729 break;
2730 }
2731 /* calleeMethod = method->clazz->pDvmDex->pResMethods[BBBB] */
2732 case OP_INVOKE_STATIC:
2733 case OP_INVOKE_STATIC_RANGE: {
2734 const Method *calleeMethod =
2735 cUnit->method->clazz->pDvmDex->pResMethods[dInsn->vB];
2736
2737 if (mir->dalvikInsn.opCode == OP_INVOKE_STATIC)
2738 genProcessArgsNoRange(cUnit, mir, dInsn,
2739 NULL /* no null check */);
2740 else
2741 genProcessArgsRange(cUnit, mir, dInsn,
2742 NULL /* no null check */);
2743
2744 /* r0 = calleeMethod */
2745 loadConstant(cUnit, r0, (int) calleeMethod);
2746
Ben Cheng38329f52009-07-07 14:19:20 -07002747 genInvokeSingletonCommon(cUnit, mir, bb, labelList, pcrLabel,
2748 calleeMethod);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002749 break;
2750 }
Ben Cheng09e50c92010-05-02 10:45:32 -07002751 /*
Ben Chengba4fc8b2009-06-01 13:00:29 -07002752 * calleeMethod = dvmFindInterfaceMethodInCache(this->clazz,
2753 * BBBB, method, method->clazz->pDvmDex)
Ben Cheng38329f52009-07-07 14:19:20 -07002754 *
Ben Cheng09e50c92010-05-02 10:45:32 -07002755 * The following is an example of generated code for
2756 * "invoke-interface v0"
Ben Cheng38329f52009-07-07 14:19:20 -07002757 *
Ben Cheng09e50c92010-05-02 10:45:32 -07002758 * -------- dalvik offset: 0x0008 @ invoke-interface v0
2759 * 0x47357e36 : ldr r0, [r5, #0] --+
2760 * 0x47357e38 : sub r7,r5,#24 |
2761 * 0x47357e3c : cmp r0, #0 | genProcessArgsNoRange
2762 * 0x47357e3e : beq 0x47357e82 |
2763 * 0x47357e40 : stmia r7, <r0> --+
2764 * 0x47357e42 : ldr r4, [pc, #120] --> r4 <- dalvikPC of this invoke
2765 * 0x47357e44 : add r1, pc, #64 --> r1 <- &retChainingCell
2766 * 0x47357e46 : add r2, pc, #72 --> r2 <- &predictedChainingCell
2767 * 0x47357e48 : blx_1 0x47348190 --+ TEMPLATE_INVOKE_METHOD_
2768 * 0x47357e4a : blx_2 see above --+ PREDICTED_CHAIN
2769 * 0x47357e4c : b 0x47357e90 --> off to the predicted chain
2770 * 0x47357e4e : b 0x47357e82 --> punt to the interpreter
2771 * 0x47357e50 : mov r8, r1 --+
2772 * 0x47357e52 : mov r9, r2 |
2773 * 0x47357e54 : ldr r2, [pc, #96] |
2774 * 0x47357e56 : mov r10, r3 |
2775 * 0x47357e58 : movs r0, r3 | dvmFindInterfaceMethodInCache
2776 * 0x47357e5a : ldr r3, [pc, #88] |
2777 * 0x47357e5c : ldr r7, [pc, #80] |
2778 * 0x47357e5e : mov r1, #1452 |
2779 * 0x47357e62 : blx r7 --+
2780 * 0x47357e64 : cmp r0, #0 --> calleeMethod == NULL?
2781 * 0x47357e66 : bne 0x47357e6e --> branch over the throw if !r0
2782 * 0x47357e68 : ldr r0, [pc, #80] --> load Dalvik PC of the invoke
2783 * 0x47357e6a : blx_1 0x47348494 --+ TEMPLATE_THROW_EXCEPTION_
2784 * 0x47357e6c : blx_2 see above --+ COMMON
2785 * 0x47357e6e : mov r1, r8 --> r1 <- &retChainingCell
2786 * 0x47357e70 : cmp r1, #0 --> compare against 0
2787 * 0x47357e72 : bgt 0x47357e7c --> >=0? don't rechain
2788 * 0x47357e74 : ldr r7, [r6, #108] --+
2789 * 0x47357e76 : mov r2, r9 | dvmJitToPatchPredictedChain
2790 * 0x47357e78 : mov r3, r10 |
2791 * 0x47357e7a : blx r7 --+
2792 * 0x47357e7c : add r1, pc, #8 --> r1 <- &retChainingCell
2793 * 0x47357e7e : blx_1 0x4734809c --+ TEMPLATE_INVOKE_METHOD_NO_OPT
2794 * 0x47357e80 : blx_2 see above --+
2795 * -------- reconstruct dalvik PC : 0x425719dc @ +0x0008
2796 * 0x47357e82 : ldr r0, [pc, #56]
Ben Cheng38329f52009-07-07 14:19:20 -07002797 * Exception_Handling:
Ben Cheng09e50c92010-05-02 10:45:32 -07002798 * 0x47357e84 : ldr r1, [r6, #92]
2799 * 0x47357e86 : blx r1
2800 * 0x47357e88 : .align4
2801 * -------- chaining cell (hot): 0x000b
2802 * 0x47357e88 : ldr r0, [r6, #104]
2803 * 0x47357e8a : blx r0
2804 * 0x47357e8c : data 0x19e2(6626)
2805 * 0x47357e8e : data 0x4257(16983)
2806 * 0x47357e90 : .align4
Ben Cheng38329f52009-07-07 14:19:20 -07002807 * -------- chaining cell (predicted)
Ben Cheng09e50c92010-05-02 10:45:32 -07002808 * 0x47357e90 : data 0xe7fe(59390) --> will be patched into bx
2809 * 0x47357e92 : data 0x0000(0)
2810 * 0x47357e94 : data 0x0000(0) --> class
2811 * 0x47357e96 : data 0x0000(0)
2812 * 0x47357e98 : data 0x0000(0) --> method
2813 * 0x47357e9a : data 0x0000(0)
2814 * 0x47357e9c : data 0x0000(0) --> rechain count
2815 * 0x47357e9e : data 0x0000(0)
2816 * -------- end of chaining cells (0x006c)
2817 * 0x47357eb0 : .word (0xad03e369)
2818 * 0x47357eb4 : .word (0x28a90)
2819 * 0x47357eb8 : .word (0x41a63394)
2820 * 0x47357ebc : .word (0x425719dc)
Ben Chengba4fc8b2009-06-01 13:00:29 -07002821 */
2822 case OP_INVOKE_INTERFACE:
2823 case OP_INVOKE_INTERFACE_RANGE: {
Bill Buzbee89efc3d2009-07-28 11:22:22 -07002824 ArmLIR *predChainingCell = &labelList[bb->taken->id];
Ben Chengba4fc8b2009-06-01 13:00:29 -07002825
Bill Buzbee1465db52009-09-23 17:17:35 -07002826 /* Ensure that nothing is both live and dirty */
Bill Buzbeec6f10662010-02-09 11:16:15 -08002827 dvmCompilerFlushAllRegs(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07002828
Ben Chengba4fc8b2009-06-01 13:00:29 -07002829 if (mir->dalvikInsn.opCode == OP_INVOKE_INTERFACE)
2830 genProcessArgsNoRange(cUnit, mir, dInsn, &pcrLabel);
2831 else
2832 genProcessArgsRange(cUnit, mir, dInsn, &pcrLabel);
2833
Ben Cheng38329f52009-07-07 14:19:20 -07002834 /* "this" is already left in r0 by genProcessArgs* */
2835
2836 /* r4PC = dalvikCallsite */
2837 loadConstant(cUnit, r4PC,
2838 (int) (cUnit->method->insns + mir->offset));
2839
2840 /* r1 = &retChainingCell */
Bill Buzbee270c1d62009-08-13 16:58:07 -07002841 ArmLIR *addrRetChain =
Bill Buzbee1465db52009-09-23 17:17:35 -07002842 opRegRegImm(cUnit, kOpAdd, r1, rpc, 0);
Ben Cheng38329f52009-07-07 14:19:20 -07002843 addrRetChain->generic.target = (LIR *) retChainingCell;
2844
2845 /* r2 = &predictedChainingCell */
Bill Buzbee89efc3d2009-07-28 11:22:22 -07002846 ArmLIR *predictedChainingCell =
Bill Buzbee1465db52009-09-23 17:17:35 -07002847 opRegRegImm(cUnit, kOpAdd, r2, rpc, 0);
Ben Cheng38329f52009-07-07 14:19:20 -07002848 predictedChainingCell->generic.target = (LIR *) predChainingCell;
2849
2850 genDispatchToHandler(cUnit, TEMPLATE_INVOKE_METHOD_PREDICTED_CHAIN);
2851
2852 /* return through lr - jump to the chaining cell */
2853 genUnconditionalBranch(cUnit, predChainingCell);
2854
2855 /*
2856 * null-check on "this" may have been eliminated, but we still need
2857 * a PC-reconstruction label for stack overflow bailout.
2858 */
2859 if (pcrLabel == NULL) {
2860 int dPC = (int) (cUnit->method->insns + mir->offset);
Bill Buzbee89efc3d2009-07-28 11:22:22 -07002861 pcrLabel = dvmCompilerNew(sizeof(ArmLIR), true);
Ben Chenga4973592010-03-31 11:59:18 -07002862 pcrLabel->opCode = kArmPseudoPCReconstructionCell;
Ben Cheng38329f52009-07-07 14:19:20 -07002863 pcrLabel->operands[0] = dPC;
2864 pcrLabel->operands[1] = mir->offset;
2865 /* Insert the place holder to the growable list */
2866 dvmInsertGrowableList(&cUnit->pcReconstructionList, pcrLabel);
2867 }
2868
2869 /* return through lr+2 - punt to the interpreter */
2870 genUnconditionalBranch(cUnit, pcrLabel);
2871
2872 /*
2873 * return through lr+4 - fully resolve the callee method.
2874 * r1 <- count
2875 * r2 <- &predictedChainCell
2876 * r3 <- this->class
2877 * r4 <- dPC
2878 * r7 <- this->class->vtable
2879 */
2880
2881 /* Save count, &predictedChainCell, and class to high regs first */
Bill Buzbee1465db52009-09-23 17:17:35 -07002882 genRegCopy(cUnit, r8, r1);
2883 genRegCopy(cUnit, r9, r2);
2884 genRegCopy(cUnit, r10, r3);
Ben Cheng38329f52009-07-07 14:19:20 -07002885
Ben Chengba4fc8b2009-06-01 13:00:29 -07002886 /* r0 now contains this->clazz */
Bill Buzbee1465db52009-09-23 17:17:35 -07002887 genRegCopy(cUnit, r0, r3);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002888
2889 /* r1 = BBBB */
2890 loadConstant(cUnit, r1, dInsn->vB);
2891
2892 /* r2 = method (caller) */
2893 loadConstant(cUnit, r2, (int) cUnit->method);
2894
2895 /* r3 = pDvmDex */
2896 loadConstant(cUnit, r3, (int) cUnit->method->clazz->pDvmDex);
2897
Ben Chengbd1326d2010-04-02 15:04:53 -07002898 LOAD_FUNC_ADDR(cUnit, r7,
2899 (intptr_t) dvmFindInterfaceMethodInCache);
Bill Buzbee1465db52009-09-23 17:17:35 -07002900 opReg(cUnit, kOpBlx, r7);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002901 /* r0 = calleeMethod (returned from dvmFindInterfaceMethodInCache */
2902
Ben Cheng09e50c92010-05-02 10:45:32 -07002903 dvmCompilerClobberCallRegs(cUnit);
2904 /* generate a branch over if the interface method is resolved */
2905 opRegImm(cUnit, kOpCmp, r0, 0); /* NULL? */
2906 ArmLIR *branchOver = opCondBranch(cUnit, kArmCondNe);
2907 /*
2908 * calleeMethod == NULL -> throw
2909 */
2910 loadConstant(cUnit, r0,
2911 (int) (cUnit->method->insns + mir->offset));
2912 genDispatchToHandler(cUnit, TEMPLATE_THROW_EXCEPTION_COMMON);
2913 /* noreturn */
2914
2915 ArmLIR *target = newLIR0(cUnit, kArmPseudoTargetLabel);
2916 target->defMask = ENCODE_ALL;
2917 branchOver->generic.target = (LIR *) target;
2918
Bill Buzbee1465db52009-09-23 17:17:35 -07002919 genRegCopy(cUnit, r1, r8);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002920
Ben Cheng38329f52009-07-07 14:19:20 -07002921 /* Check if rechain limit is reached */
Bill Buzbee1465db52009-09-23 17:17:35 -07002922 opRegImm(cUnit, kOpCmp, r1, 0);
Ben Cheng38329f52009-07-07 14:19:20 -07002923
Bill Buzbee1465db52009-09-23 17:17:35 -07002924 ArmLIR *bypassRechaining = opCondBranch(cUnit, kArmCondGt);
Ben Cheng38329f52009-07-07 14:19:20 -07002925
Bill Buzbee270c1d62009-08-13 16:58:07 -07002926 loadWordDisp(cUnit, rGLUE, offsetof(InterpState,
2927 jitToInterpEntries.dvmJitToPatchPredictedChain), r7);
Ben Cheng38329f52009-07-07 14:19:20 -07002928
Ben Chengb88ec3c2010-05-17 12:50:33 -07002929 genRegCopy(cUnit, r1, rGLUE);
Bill Buzbee1465db52009-09-23 17:17:35 -07002930 genRegCopy(cUnit, r2, r9);
2931 genRegCopy(cUnit, r3, r10);
Ben Cheng38329f52009-07-07 14:19:20 -07002932
2933 /*
2934 * r0 = calleeMethod
2935 * r2 = &predictedChainingCell
2936 * r3 = class
2937 *
2938 * &returnChainingCell has been loaded into r1 but is not needed
2939 * when patching the chaining cell and will be clobbered upon
2940 * returning so it will be reconstructed again.
2941 */
Bill Buzbee1465db52009-09-23 17:17:35 -07002942 opReg(cUnit, kOpBlx, r7);
Ben Cheng38329f52009-07-07 14:19:20 -07002943
2944 /* r1 = &retChainingCell */
Bill Buzbee1465db52009-09-23 17:17:35 -07002945 addrRetChain = opRegRegImm(cUnit, kOpAdd, r1, rpc, 0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002946 addrRetChain->generic.target = (LIR *) retChainingCell;
Ben Cheng38329f52009-07-07 14:19:20 -07002947
2948 bypassRechaining->generic.target = (LIR *) addrRetChain;
2949
Ben Chengba4fc8b2009-06-01 13:00:29 -07002950 /*
2951 * r0 = this, r1 = calleeMethod,
2952 * r1 = &ChainingCell,
2953 * r4PC = callsiteDPC,
2954 */
2955 genDispatchToHandler(cUnit, TEMPLATE_INVOKE_METHOD_NO_OPT);
Ben Cheng978738d2010-05-13 13:45:57 -07002956#if defined(WITH_JIT_TUNING)
Ben Cheng86717f72010-03-05 15:27:21 -08002957 gDvmJit.invokePolymorphic++;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002958#endif
2959 /* Handle exceptions using the interpreter */
2960 genTrap(cUnit, mir->offset, pcrLabel);
2961 break;
2962 }
2963 /* NOP */
2964 case OP_INVOKE_DIRECT_EMPTY: {
2965 return false;
2966 }
2967 case OP_FILLED_NEW_ARRAY:
2968 case OP_FILLED_NEW_ARRAY_RANGE: {
2969 /* Just let the interpreter deal with these */
2970 genInterpSingleStep(cUnit, mir);
2971 break;
2972 }
2973 default:
2974 return true;
2975 }
2976 return false;
2977}
2978
2979static bool handleFmt35ms_3rms(CompilationUnit *cUnit, MIR *mir,
Bill Buzbee89efc3d2009-07-28 11:22:22 -07002980 BasicBlock *bb, ArmLIR *labelList)
Ben Chengba4fc8b2009-06-01 13:00:29 -07002981{
Bill Buzbee89efc3d2009-07-28 11:22:22 -07002982 ArmLIR *retChainingCell = &labelList[bb->fallThrough->id];
2983 ArmLIR *predChainingCell = &labelList[bb->taken->id];
2984 ArmLIR *pcrLabel = NULL;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002985
2986 DecodedInstruction *dInsn = &mir->dalvikInsn;
2987 switch (mir->dalvikInsn.opCode) {
2988 /* calleeMethod = this->clazz->vtable[BBBB] */
2989 case OP_INVOKE_VIRTUAL_QUICK_RANGE:
2990 case OP_INVOKE_VIRTUAL_QUICK: {
2991 int methodIndex = dInsn->vB;
2992 if (mir->dalvikInsn.opCode == OP_INVOKE_VIRTUAL_QUICK)
2993 genProcessArgsNoRange(cUnit, mir, dInsn, &pcrLabel);
2994 else
2995 genProcessArgsRange(cUnit, mir, dInsn, &pcrLabel);
2996
Ben Cheng38329f52009-07-07 14:19:20 -07002997 genInvokeVirtualCommon(cUnit, mir, methodIndex,
2998 retChainingCell,
2999 predChainingCell,
3000 pcrLabel);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003001 break;
3002 }
3003 /* calleeMethod = method->clazz->super->vtable[BBBB] */
3004 case OP_INVOKE_SUPER_QUICK:
3005 case OP_INVOKE_SUPER_QUICK_RANGE: {
3006 const Method *calleeMethod =
3007 cUnit->method->clazz->super->vtable[dInsn->vB];
3008
3009 if (mir->dalvikInsn.opCode == OP_INVOKE_SUPER_QUICK)
3010 genProcessArgsNoRange(cUnit, mir, dInsn, &pcrLabel);
3011 else
3012 genProcessArgsRange(cUnit, mir, dInsn, &pcrLabel);
3013
3014 /* r0 = calleeMethod */
3015 loadConstant(cUnit, r0, (int) calleeMethod);
3016
Ben Cheng38329f52009-07-07 14:19:20 -07003017 genInvokeSingletonCommon(cUnit, mir, bb, labelList, pcrLabel,
3018 calleeMethod);
3019 /* Handle exceptions using the interpreter */
3020 genTrap(cUnit, mir->offset, pcrLabel);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003021 break;
3022 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07003023 default:
3024 return true;
3025 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07003026 return false;
3027}
3028
3029/*
Bill Buzbeefd023aa2009-11-02 09:23:49 -08003030 * This operation is complex enough that we'll do it partly inline
3031 * and partly with a handler. NOTE: the handler uses hardcoded
3032 * values for string object offsets and must be revisitied if the
3033 * layout changes.
3034 */
3035static bool genInlinedCompareTo(CompilationUnit *cUnit, MIR *mir)
3036{
3037#if defined(USE_GLOBAL_STRING_DEFS)
3038 return false;
3039#else
3040 ArmLIR *rollback;
Bill Buzbeec6f10662010-02-09 11:16:15 -08003041 RegLocation rlThis = dvmCompilerGetSrc(cUnit, mir, 0);
3042 RegLocation rlComp = dvmCompilerGetSrc(cUnit, mir, 1);
Bill Buzbeefd023aa2009-11-02 09:23:49 -08003043
3044 loadValueDirectFixed(cUnit, rlThis, r0);
3045 loadValueDirectFixed(cUnit, rlComp, r1);
3046 /* Test objects for NULL */
3047 rollback = genNullCheck(cUnit, rlThis.sRegLow, r0, mir->offset, NULL);
3048 genNullCheck(cUnit, rlComp.sRegLow, r1, mir->offset, rollback);
3049 /*
3050 * TUNING: we could check for object pointer equality before invoking
3051 * handler. Unclear whether the gain would be worth the added code size
3052 * expansion.
3053 */
3054 genDispatchToHandler(cUnit, TEMPLATE_STRING_COMPARETO);
Bill Buzbeec6f10662010-02-09 11:16:15 -08003055 storeValue(cUnit, inlinedTarget(cUnit, mir, false),
3056 dvmCompilerGetReturn(cUnit));
Bill Buzbeefd023aa2009-11-02 09:23:49 -08003057 return true;
3058#endif
3059}
3060
Elliott Hughes2bdbcb62010-04-12 14:29:37 -07003061static bool genInlinedFastIndexOf(CompilationUnit *cUnit, MIR *mir)
Bill Buzbeefd023aa2009-11-02 09:23:49 -08003062{
3063#if defined(USE_GLOBAL_STRING_DEFS)
3064 return false;
3065#else
Bill Buzbeec6f10662010-02-09 11:16:15 -08003066 RegLocation rlThis = dvmCompilerGetSrc(cUnit, mir, 0);
3067 RegLocation rlChar = dvmCompilerGetSrc(cUnit, mir, 1);
Bill Buzbeefd023aa2009-11-02 09:23:49 -08003068
3069 loadValueDirectFixed(cUnit, rlThis, r0);
3070 loadValueDirectFixed(cUnit, rlChar, r1);
Elliott Hughes2bdbcb62010-04-12 14:29:37 -07003071 RegLocation rlStart = dvmCompilerGetSrc(cUnit, mir, 2);
3072 loadValueDirectFixed(cUnit, rlStart, r2);
Bill Buzbeefd023aa2009-11-02 09:23:49 -08003073 /* Test objects for NULL */
3074 genNullCheck(cUnit, rlThis.sRegLow, r0, mir->offset, NULL);
3075 genDispatchToHandler(cUnit, TEMPLATE_STRING_INDEXOF);
Bill Buzbeec6f10662010-02-09 11:16:15 -08003076 storeValue(cUnit, inlinedTarget(cUnit, mir, false),
3077 dvmCompilerGetReturn(cUnit));
Bill Buzbeefd023aa2009-11-02 09:23:49 -08003078 return true;
3079#endif
3080}
3081
Elliott Hughesee34f592010-04-05 18:13:52 -07003082// Generates an inlined String.isEmpty or String.length.
3083static bool genInlinedStringIsEmptyOrLength(CompilationUnit *cUnit, MIR *mir,
3084 bool isEmpty)
Bill Buzbee1f748632010-03-02 16:14:41 -08003085{
Elliott Hughesee34f592010-04-05 18:13:52 -07003086 // dst = src.length();
Bill Buzbee1f748632010-03-02 16:14:41 -08003087 RegLocation rlObj = dvmCompilerGetSrc(cUnit, mir, 0);
3088 RegLocation rlDest = inlinedTarget(cUnit, mir, false);
3089 rlObj = loadValue(cUnit, rlObj, kCoreReg);
3090 RegLocation rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
3091 genNullCheck(cUnit, rlObj.sRegLow, rlObj.lowReg, mir->offset, NULL);
3092 loadWordDisp(cUnit, rlObj.lowReg, gDvm.offJavaLangString_count,
3093 rlResult.lowReg);
Elliott Hughesee34f592010-04-05 18:13:52 -07003094 if (isEmpty) {
3095 // dst = (dst == 0);
3096 int tReg = dvmCompilerAllocTemp(cUnit);
3097 opRegReg(cUnit, kOpNeg, tReg, rlResult.lowReg);
3098 opRegRegReg(cUnit, kOpAdc, rlResult.lowReg, rlResult.lowReg, tReg);
3099 }
Bill Buzbee1f748632010-03-02 16:14:41 -08003100 storeValue(cUnit, rlDest, rlResult);
3101 return false;
3102}
3103
Elliott Hughesee34f592010-04-05 18:13:52 -07003104static bool genInlinedStringLength(CompilationUnit *cUnit, MIR *mir)
3105{
3106 return genInlinedStringIsEmptyOrLength(cUnit, mir, false);
3107}
3108
3109static bool genInlinedStringIsEmpty(CompilationUnit *cUnit, MIR *mir)
3110{
3111 return genInlinedStringIsEmptyOrLength(cUnit, mir, true);
3112}
3113
Bill Buzbee1f748632010-03-02 16:14:41 -08003114static bool genInlinedStringCharAt(CompilationUnit *cUnit, MIR *mir)
3115{
3116 int contents = offsetof(ArrayObject, contents);
3117 RegLocation rlObj = dvmCompilerGetSrc(cUnit, mir, 0);
3118 RegLocation rlIdx = dvmCompilerGetSrc(cUnit, mir, 1);
3119 RegLocation rlDest = inlinedTarget(cUnit, mir, false);
3120 RegLocation rlResult;
3121 rlObj = loadValue(cUnit, rlObj, kCoreReg);
3122 rlIdx = loadValue(cUnit, rlIdx, kCoreReg);
3123 int regMax = dvmCompilerAllocTemp(cUnit);
3124 int regOff = dvmCompilerAllocTemp(cUnit);
3125 int regPtr = dvmCompilerAllocTemp(cUnit);
3126 ArmLIR *pcrLabel = genNullCheck(cUnit, rlObj.sRegLow, rlObj.lowReg,
3127 mir->offset, NULL);
3128 loadWordDisp(cUnit, rlObj.lowReg, gDvm.offJavaLangString_count, regMax);
3129 loadWordDisp(cUnit, rlObj.lowReg, gDvm.offJavaLangString_offset, regOff);
3130 loadWordDisp(cUnit, rlObj.lowReg, gDvm.offJavaLangString_value, regPtr);
3131 genBoundsCheck(cUnit, rlIdx.lowReg, regMax, mir->offset, pcrLabel);
3132 dvmCompilerFreeTemp(cUnit, regMax);
3133 opRegImm(cUnit, kOpAdd, regPtr, contents);
3134 opRegReg(cUnit, kOpAdd, regOff, rlIdx.lowReg);
3135 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
3136 loadBaseIndexed(cUnit, regPtr, regOff, rlResult.lowReg, 1, kUnsignedHalf);
3137 storeValue(cUnit, rlDest, rlResult);
3138 return false;
3139}
3140
3141static bool genInlinedAbsInt(CompilationUnit *cUnit, MIR *mir)
3142{
3143 RegLocation rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
3144 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
3145 RegLocation rlDest = inlinedTarget(cUnit, mir, false);;
3146 RegLocation rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
3147 int signReg = dvmCompilerAllocTemp(cUnit);
3148 /*
3149 * abs(x) = y<=x>>31, (x+y)^y.
3150 * Thumb2's IT block also yields 3 instructions, but imposes
3151 * scheduling constraints.
3152 */
3153 opRegRegImm(cUnit, kOpAsr, signReg, rlSrc.lowReg, 31);
3154 opRegRegReg(cUnit, kOpAdd, rlResult.lowReg, rlSrc.lowReg, signReg);
3155 opRegReg(cUnit, kOpXor, rlResult.lowReg, signReg);
3156 storeValue(cUnit, rlDest, rlResult);
3157 return false;
3158}
3159
3160static bool genInlinedAbsLong(CompilationUnit *cUnit, MIR *mir)
3161{
3162 RegLocation rlSrc = dvmCompilerGetSrcWide(cUnit, mir, 0, 1);
3163 RegLocation rlDest = inlinedTargetWide(cUnit, mir, false);
3164 rlSrc = loadValueWide(cUnit, rlSrc, kCoreReg);
3165 RegLocation rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
3166 int signReg = dvmCompilerAllocTemp(cUnit);
3167 /*
3168 * abs(x) = y<=x>>31, (x+y)^y.
3169 * Thumb2 IT block allows slightly shorter sequence,
3170 * but introduces a scheduling barrier. Stick with this
3171 * mechanism for now.
3172 */
3173 opRegRegImm(cUnit, kOpAsr, signReg, rlSrc.highReg, 31);
3174 opRegRegReg(cUnit, kOpAdd, rlResult.lowReg, rlSrc.lowReg, signReg);
3175 opRegRegReg(cUnit, kOpAdc, rlResult.highReg, rlSrc.highReg, signReg);
3176 opRegReg(cUnit, kOpXor, rlResult.lowReg, signReg);
3177 opRegReg(cUnit, kOpXor, rlResult.highReg, signReg);
3178 storeValueWide(cUnit, rlDest, rlResult);
3179 return false;
3180}
Bill Buzbeefd023aa2009-11-02 09:23:49 -08003181
3182/*
Bill Buzbeece46c942009-11-20 15:41:34 -08003183 * NOTE: Handles both range and non-range versions (arguments
3184 * have already been normalized by this point).
Ben Chengba4fc8b2009-06-01 13:00:29 -07003185 */
Bill Buzbeece46c942009-11-20 15:41:34 -08003186static bool handleExecuteInline(CompilationUnit *cUnit, MIR *mir)
Ben Chengba4fc8b2009-06-01 13:00:29 -07003187{
3188 DecodedInstruction *dInsn = &mir->dalvikInsn;
3189 switch( mir->dalvikInsn.opCode) {
Bill Buzbeece46c942009-11-20 15:41:34 -08003190 case OP_EXECUTE_INLINE_RANGE:
Ben Chengba4fc8b2009-06-01 13:00:29 -07003191 case OP_EXECUTE_INLINE: {
3192 unsigned int i;
3193 const InlineOperation* inLineTable = dvmGetInlineOpsTable();
Bill Buzbee50a6bf22009-07-08 13:08:04 -07003194 int offset = offsetof(InterpState, retval);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003195 int operation = dInsn->vB;
Bill Buzbee50a6bf22009-07-08 13:08:04 -07003196 switch (operation) {
3197 case INLINE_EMPTYINLINEMETHOD:
3198 return false; /* Nop */
3199 case INLINE_STRING_LENGTH:
3200 return genInlinedStringLength(cUnit, mir);
Elliott Hughesee34f592010-04-05 18:13:52 -07003201 case INLINE_STRING_IS_EMPTY:
3202 return genInlinedStringIsEmpty(cUnit, mir);
Bill Buzbee50a6bf22009-07-08 13:08:04 -07003203 case INLINE_MATH_ABS_INT:
3204 return genInlinedAbsInt(cUnit, mir);
3205 case INLINE_MATH_ABS_LONG:
3206 return genInlinedAbsLong(cUnit, mir);
3207 case INLINE_MATH_MIN_INT:
3208 return genInlinedMinMaxInt(cUnit, mir, true);
3209 case INLINE_MATH_MAX_INT:
3210 return genInlinedMinMaxInt(cUnit, mir, false);
3211 case INLINE_STRING_CHARAT:
3212 return genInlinedStringCharAt(cUnit, mir);
3213 case INLINE_MATH_SQRT:
3214 if (genInlineSqrt(cUnit, mir))
Bill Buzbee9727c3d2009-08-01 11:32:36 -07003215 return false;
Bill Buzbee50a6bf22009-07-08 13:08:04 -07003216 else
3217 break; /* Handle with C routine */
Bill Buzbee50a6bf22009-07-08 13:08:04 -07003218 case INLINE_MATH_ABS_FLOAT:
Bill Buzbee1465db52009-09-23 17:17:35 -07003219 if (genInlinedAbsFloat(cUnit, mir))
3220 return false;
3221 else
3222 break;
Bill Buzbee50a6bf22009-07-08 13:08:04 -07003223 case INLINE_MATH_ABS_DOUBLE:
Bill Buzbee1465db52009-09-23 17:17:35 -07003224 if (genInlinedAbsDouble(cUnit, mir))
3225 return false;
3226 else
3227 break;
Bill Buzbee50a6bf22009-07-08 13:08:04 -07003228 case INLINE_STRING_COMPARETO:
Bill Buzbeefd023aa2009-11-02 09:23:49 -08003229 if (genInlinedCompareTo(cUnit, mir))
3230 return false;
3231 else
3232 break;
Elliott Hughes2bdbcb62010-04-12 14:29:37 -07003233 case INLINE_STRING_FASTINDEXOF_II:
3234 if (genInlinedFastIndexOf(cUnit, mir))
Bill Buzbeefd023aa2009-11-02 09:23:49 -08003235 return false;
3236 else
3237 break;
3238 case INLINE_STRING_EQUALS:
3239 case INLINE_MATH_COS:
3240 case INLINE_MATH_SIN:
3241 break; /* Handle with C routine */
Bill Buzbee50a6bf22009-07-08 13:08:04 -07003242 default:
Bill Buzbeefc519dc2010-03-06 23:30:57 -08003243 dvmCompilerAbort(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003244 }
Bill Buzbeec6f10662010-02-09 11:16:15 -08003245 dvmCompilerFlushAllRegs(cUnit); /* Everything to home location */
Elliott Hughes6a555132010-02-25 15:41:42 -08003246 dvmCompilerClobberCallRegs(cUnit);
Bill Buzbeec6f10662010-02-09 11:16:15 -08003247 dvmCompilerClobber(cUnit, r4PC);
3248 dvmCompilerClobber(cUnit, r7);
Bill Buzbee1465db52009-09-23 17:17:35 -07003249 opRegRegImm(cUnit, kOpAdd, r4PC, rGLUE, offset);
3250 opImm(cUnit, kOpPush, (1<<r4PC) | (1<<r7));
Ben Chengbd1326d2010-04-02 15:04:53 -07003251 LOAD_FUNC_ADDR(cUnit, r4PC, (int)inLineTable[operation].func);
Bill Buzbee1465db52009-09-23 17:17:35 -07003252 genExportPC(cUnit, mir);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003253 for (i=0; i < dInsn->vA; i++) {
Bill Buzbeec6f10662010-02-09 11:16:15 -08003254 loadValueDirect(cUnit, dvmCompilerGetSrc(cUnit, mir, i), i);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003255 }
Bill Buzbee1465db52009-09-23 17:17:35 -07003256 opReg(cUnit, kOpBlx, r4PC);
3257 opRegImm(cUnit, kOpAdd, r13, 8);
Bill Buzbeece46c942009-11-20 15:41:34 -08003258 opRegImm(cUnit, kOpCmp, r0, 0); /* NULL? */
3259 ArmLIR *branchOver = opCondBranch(cUnit, kArmCondNe);
3260 loadConstant(cUnit, r0,
3261 (int) (cUnit->method->insns + mir->offset));
3262 genDispatchToHandler(cUnit, TEMPLATE_THROW_EXCEPTION_COMMON);
3263 ArmLIR *target = newLIR0(cUnit, kArmPseudoTargetLabel);
3264 target->defMask = ENCODE_ALL;
3265 branchOver->generic.target = (LIR *) target;
Ben Chengba4fc8b2009-06-01 13:00:29 -07003266 break;
3267 }
3268 default:
3269 return true;
3270 }
3271 return false;
3272}
3273
3274static bool handleFmt51l(CompilationUnit *cUnit, MIR *mir)
3275{
Bill Buzbee1465db52009-09-23 17:17:35 -07003276 //TUNING: We're using core regs here - not optimal when target is a double
Bill Buzbeec6f10662010-02-09 11:16:15 -08003277 RegLocation rlDest = dvmCompilerGetDestWide(cUnit, mir, 0, 1);
3278 RegLocation rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Ben Chengbd1326d2010-04-02 15:04:53 -07003279 loadConstantNoClobber(cUnit, rlResult.lowReg,
3280 mir->dalvikInsn.vB_wide & 0xFFFFFFFFUL);
3281 loadConstantNoClobber(cUnit, rlResult.highReg,
3282 (mir->dalvikInsn.vB_wide>>32) & 0xFFFFFFFFUL);
Bill Buzbee1465db52009-09-23 17:17:35 -07003283 storeValueWide(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003284 return false;
3285}
3286
Ben Chengba4fc8b2009-06-01 13:00:29 -07003287/*
3288 * The following are special processing routines that handle transfer of
3289 * controls between compiled code and the interpreter. Certain VM states like
3290 * Dalvik PC and special-purpose registers are reconstructed here.
3291 */
3292
Bill Buzbeebd047242010-05-13 13:02:53 -07003293/*
3294 * Insert a
3295 * b .+4
3296 * nop
3297 * pair at the beginning of a chaining cell. This serves as the
3298 * switch branch that selects between reverting to the interpreter or
3299 * not. Once the cell is chained to a translation, the cell will
3300 * contain a 32-bit branch. Subsequent chain/unchain operations will
3301 * then only alter that first 16-bits - the "b .+4" for unchaining,
3302 * and the restoration of the first half of the 32-bit branch for
3303 * rechaining.
3304 */
3305static void insertChainingSwitch(CompilationUnit *cUnit)
3306{
3307 ArmLIR *branch = newLIR0(cUnit, kThumbBUncond);
3308 newLIR2(cUnit, kThumbOrr, r0, r0);
3309 ArmLIR *target = newLIR0(cUnit, kArmPseudoTargetLabel);
3310 target->defMask = ENCODE_ALL;
3311 branch->generic.target = (LIR *) target;
3312}
3313
Ben Cheng1efc9c52009-06-08 18:25:27 -07003314/* Chaining cell for code that may need warmup. */
3315static void handleNormalChainingCell(CompilationUnit *cUnit,
3316 unsigned int offset)
Ben Chengba4fc8b2009-06-01 13:00:29 -07003317{
Ben Cheng11d8f142010-03-24 15:24:19 -07003318 /*
3319 * Use raw instruction constructors to guarantee that the generated
3320 * instructions fit the predefined cell size.
3321 */
Bill Buzbeebd047242010-05-13 13:02:53 -07003322 insertChainingSwitch(cUnit);
Ben Cheng11d8f142010-03-24 15:24:19 -07003323 newLIR3(cUnit, kThumbLdrRRI5, r0, rGLUE,
3324 offsetof(InterpState,
3325 jitToInterpEntries.dvmJitToInterpNormal) >> 2);
3326 newLIR1(cUnit, kThumbBlxR, r0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003327 addWordData(cUnit, (int) (cUnit->method->insns + offset), true);
3328}
3329
3330/*
Ben Cheng1efc9c52009-06-08 18:25:27 -07003331 * Chaining cell for instructions that immediately following already translated
3332 * code.
Ben Chengba4fc8b2009-06-01 13:00:29 -07003333 */
Ben Cheng1efc9c52009-06-08 18:25:27 -07003334static void handleHotChainingCell(CompilationUnit *cUnit,
3335 unsigned int offset)
Ben Chengba4fc8b2009-06-01 13:00:29 -07003336{
Ben Cheng11d8f142010-03-24 15:24:19 -07003337 /*
3338 * Use raw instruction constructors to guarantee that the generated
3339 * instructions fit the predefined cell size.
3340 */
Bill Buzbeebd047242010-05-13 13:02:53 -07003341 insertChainingSwitch(cUnit);
Ben Cheng11d8f142010-03-24 15:24:19 -07003342 newLIR3(cUnit, kThumbLdrRRI5, r0, rGLUE,
3343 offsetof(InterpState,
3344 jitToInterpEntries.dvmJitToInterpTraceSelect) >> 2);
3345 newLIR1(cUnit, kThumbBlxR, r0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003346 addWordData(cUnit, (int) (cUnit->method->insns + offset), true);
3347}
3348
Bill Buzbee9c4b7c82009-09-10 10:10:38 -07003349#if defined(WITH_SELF_VERIFICATION) || defined(WITH_JIT_TUNING)
Jeff Hao97319a82009-08-12 16:57:15 -07003350/* Chaining cell for branches that branch back into the same basic block */
3351static void handleBackwardBranchChainingCell(CompilationUnit *cUnit,
3352 unsigned int offset)
3353{
Ben Cheng11d8f142010-03-24 15:24:19 -07003354 /*
3355 * Use raw instruction constructors to guarantee that the generated
3356 * instructions fit the predefined cell size.
3357 */
Bill Buzbeebd047242010-05-13 13:02:53 -07003358 insertChainingSwitch(cUnit);
Bill Buzbee9c4b7c82009-09-10 10:10:38 -07003359#if defined(WITH_SELF_VERIFICATION)
Bill Buzbee1465db52009-09-23 17:17:35 -07003360 newLIR3(cUnit, kThumbLdrRRI5, r0, rGLUE,
Ben Cheng40094c12010-02-24 20:58:44 -08003361 offsetof(InterpState,
3362 jitToInterpEntries.dvmJitToInterpBackwardBranch) >> 2);
Bill Buzbee9c4b7c82009-09-10 10:10:38 -07003363#else
Bill Buzbee1465db52009-09-23 17:17:35 -07003364 newLIR3(cUnit, kThumbLdrRRI5, r0, rGLUE,
Bill Buzbee9c4b7c82009-09-10 10:10:38 -07003365 offsetof(InterpState, jitToInterpEntries.dvmJitToInterpNormal) >> 2);
3366#endif
Bill Buzbee1465db52009-09-23 17:17:35 -07003367 newLIR1(cUnit, kThumbBlxR, r0);
Jeff Hao97319a82009-08-12 16:57:15 -07003368 addWordData(cUnit, (int) (cUnit->method->insns + offset), true);
3369}
3370
3371#endif
Ben Chengba4fc8b2009-06-01 13:00:29 -07003372/* Chaining cell for monomorphic method invocations. */
Ben Cheng38329f52009-07-07 14:19:20 -07003373static void handleInvokeSingletonChainingCell(CompilationUnit *cUnit,
3374 const Method *callee)
Ben Chengba4fc8b2009-06-01 13:00:29 -07003375{
Ben Cheng11d8f142010-03-24 15:24:19 -07003376 /*
3377 * Use raw instruction constructors to guarantee that the generated
3378 * instructions fit the predefined cell size.
3379 */
Bill Buzbeebd047242010-05-13 13:02:53 -07003380 insertChainingSwitch(cUnit);
Ben Cheng11d8f142010-03-24 15:24:19 -07003381 newLIR3(cUnit, kThumbLdrRRI5, r0, rGLUE,
3382 offsetof(InterpState,
3383 jitToInterpEntries.dvmJitToInterpTraceSelect) >> 2);
3384 newLIR1(cUnit, kThumbBlxR, r0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003385 addWordData(cUnit, (int) (callee->insns), true);
3386}
3387
Ben Cheng38329f52009-07-07 14:19:20 -07003388/* Chaining cell for monomorphic method invocations. */
3389static void handleInvokePredictedChainingCell(CompilationUnit *cUnit)
3390{
3391
3392 /* Should not be executed in the initial state */
3393 addWordData(cUnit, PREDICTED_CHAIN_BX_PAIR_INIT, true);
3394 /* To be filled: class */
3395 addWordData(cUnit, PREDICTED_CHAIN_CLAZZ_INIT, true);
3396 /* To be filled: method */
3397 addWordData(cUnit, PREDICTED_CHAIN_METHOD_INIT, true);
3398 /*
3399 * Rechain count. The initial value of 0 here will trigger chaining upon
3400 * the first invocation of this callsite.
3401 */
3402 addWordData(cUnit, PREDICTED_CHAIN_COUNTER_INIT, true);
3403}
3404
Ben Chengba4fc8b2009-06-01 13:00:29 -07003405/* Load the Dalvik PC into r0 and jump to the specified target */
3406static void handlePCReconstruction(CompilationUnit *cUnit,
Bill Buzbee89efc3d2009-07-28 11:22:22 -07003407 ArmLIR *targetLabel)
Ben Chengba4fc8b2009-06-01 13:00:29 -07003408{
Bill Buzbee89efc3d2009-07-28 11:22:22 -07003409 ArmLIR **pcrLabel =
3410 (ArmLIR **) cUnit->pcReconstructionList.elemList;
Ben Chengba4fc8b2009-06-01 13:00:29 -07003411 int numElems = cUnit->pcReconstructionList.numUsed;
3412 int i;
3413 for (i = 0; i < numElems; i++) {
3414 dvmCompilerAppendLIR(cUnit, (LIR *) pcrLabel[i]);
3415 /* r0 = dalvik PC */
3416 loadConstant(cUnit, r0, pcrLabel[i]->operands[0]);
3417 genUnconditionalBranch(cUnit, targetLabel);
3418 }
3419}
3420
Bill Buzbee1465db52009-09-23 17:17:35 -07003421static char *extendedMIROpNames[kMirOpLast - kMirOpFirst] = {
3422 "kMirOpPhi",
3423 "kMirOpNullNRangeUpCheck",
3424 "kMirOpNullNRangeDownCheck",
3425 "kMirOpLowerBound",
3426 "kMirOpPunt",
Ben Cheng4238ec22009-08-24 16:32:22 -07003427};
3428
3429/*
3430 * vA = arrayReg;
3431 * vB = idxReg;
3432 * vC = endConditionReg;
3433 * arg[0] = maxC
3434 * arg[1] = minC
3435 * arg[2] = loopBranchConditionCode
3436 */
3437static void genHoistedChecksForCountUpLoop(CompilationUnit *cUnit, MIR *mir)
3438{
Bill Buzbee1465db52009-09-23 17:17:35 -07003439 /*
3440 * NOTE: these synthesized blocks don't have ssa names assigned
3441 * for Dalvik registers. However, because they dominate the following
3442 * blocks we can simply use the Dalvik name w/ subscript 0 as the
3443 * ssa name.
3444 */
Ben Cheng4238ec22009-08-24 16:32:22 -07003445 DecodedInstruction *dInsn = &mir->dalvikInsn;
3446 const int lenOffset = offsetof(ArrayObject, length);
Ben Cheng4238ec22009-08-24 16:32:22 -07003447 const int maxC = dInsn->arg[0];
Bill Buzbee1465db52009-09-23 17:17:35 -07003448 int regLength;
3449 RegLocation rlArray = cUnit->regLocation[mir->dalvikInsn.vA];
3450 RegLocation rlIdxEnd = cUnit->regLocation[mir->dalvikInsn.vC];
Ben Cheng4238ec22009-08-24 16:32:22 -07003451
3452 /* regArray <- arrayRef */
Bill Buzbee1465db52009-09-23 17:17:35 -07003453 rlArray = loadValue(cUnit, rlArray, kCoreReg);
3454 rlIdxEnd = loadValue(cUnit, rlIdxEnd, kCoreReg);
3455 genRegImmCheck(cUnit, kArmCondEq, rlArray.lowReg, 0, 0,
Ben Cheng4238ec22009-08-24 16:32:22 -07003456 (ArmLIR *) cUnit->loopAnalysis->branchToPCR);
3457
3458 /* regLength <- len(arrayRef) */
Bill Buzbeec6f10662010-02-09 11:16:15 -08003459 regLength = dvmCompilerAllocTemp(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07003460 loadWordDisp(cUnit, rlArray.lowReg, lenOffset, regLength);
Ben Cheng4238ec22009-08-24 16:32:22 -07003461
3462 int delta = maxC;
3463 /*
3464 * If the loop end condition is ">=" instead of ">", then the largest value
3465 * of the index is "endCondition - 1".
3466 */
3467 if (dInsn->arg[2] == OP_IF_GE) {
3468 delta--;
3469 }
3470
3471 if (delta) {
Bill Buzbeec6f10662010-02-09 11:16:15 -08003472 int tReg = dvmCompilerAllocTemp(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07003473 opRegRegImm(cUnit, kOpAdd, tReg, rlIdxEnd.lowReg, delta);
3474 rlIdxEnd.lowReg = tReg;
Bill Buzbeec6f10662010-02-09 11:16:15 -08003475 dvmCompilerFreeTemp(cUnit, tReg);
Ben Cheng4238ec22009-08-24 16:32:22 -07003476 }
3477 /* Punt if "regIdxEnd < len(Array)" is false */
Bill Buzbee1465db52009-09-23 17:17:35 -07003478 genRegRegCheck(cUnit, kArmCondGe, rlIdxEnd.lowReg, regLength, 0,
Ben Cheng0fd31e42009-09-03 14:40:16 -07003479 (ArmLIR *) cUnit->loopAnalysis->branchToPCR);
Ben Cheng4238ec22009-08-24 16:32:22 -07003480}
3481
3482/*
3483 * vA = arrayReg;
3484 * vB = idxReg;
3485 * vC = endConditionReg;
3486 * arg[0] = maxC
3487 * arg[1] = minC
3488 * arg[2] = loopBranchConditionCode
3489 */
3490static void genHoistedChecksForCountDownLoop(CompilationUnit *cUnit, MIR *mir)
3491{
3492 DecodedInstruction *dInsn = &mir->dalvikInsn;
3493 const int lenOffset = offsetof(ArrayObject, length);
Bill Buzbeec6f10662010-02-09 11:16:15 -08003494 const int regLength = dvmCompilerAllocTemp(cUnit);
Ben Cheng4238ec22009-08-24 16:32:22 -07003495 const int maxC = dInsn->arg[0];
Bill Buzbee1465db52009-09-23 17:17:35 -07003496 RegLocation rlArray = cUnit->regLocation[mir->dalvikInsn.vA];
3497 RegLocation rlIdxInit = cUnit->regLocation[mir->dalvikInsn.vB];
Ben Cheng4238ec22009-08-24 16:32:22 -07003498
3499 /* regArray <- arrayRef */
Bill Buzbee1465db52009-09-23 17:17:35 -07003500 rlArray = loadValue(cUnit, rlArray, kCoreReg);
3501 rlIdxInit = loadValue(cUnit, rlIdxInit, kCoreReg);
3502 genRegImmCheck(cUnit, kArmCondEq, rlArray.lowReg, 0, 0,
Ben Cheng4238ec22009-08-24 16:32:22 -07003503 (ArmLIR *) cUnit->loopAnalysis->branchToPCR);
3504
3505 /* regLength <- len(arrayRef) */
Bill Buzbee1465db52009-09-23 17:17:35 -07003506 loadWordDisp(cUnit, rlArray.lowReg, lenOffset, regLength);
Ben Cheng4238ec22009-08-24 16:32:22 -07003507
3508 if (maxC) {
Bill Buzbeec6f10662010-02-09 11:16:15 -08003509 int tReg = dvmCompilerAllocTemp(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07003510 opRegRegImm(cUnit, kOpAdd, tReg, rlIdxInit.lowReg, maxC);
3511 rlIdxInit.lowReg = tReg;
Bill Buzbeec6f10662010-02-09 11:16:15 -08003512 dvmCompilerFreeTemp(cUnit, tReg);
Ben Cheng4238ec22009-08-24 16:32:22 -07003513 }
3514
3515 /* Punt if "regIdxInit < len(Array)" is false */
Bill Buzbee1465db52009-09-23 17:17:35 -07003516 genRegRegCheck(cUnit, kArmCondGe, rlIdxInit.lowReg, regLength, 0,
Ben Cheng0fd31e42009-09-03 14:40:16 -07003517 (ArmLIR *) cUnit->loopAnalysis->branchToPCR);
Ben Cheng4238ec22009-08-24 16:32:22 -07003518}
3519
3520/*
3521 * vA = idxReg;
3522 * vB = minC;
3523 */
3524static void genHoistedLowerBoundCheck(CompilationUnit *cUnit, MIR *mir)
3525{
3526 DecodedInstruction *dInsn = &mir->dalvikInsn;
Ben Cheng4238ec22009-08-24 16:32:22 -07003527 const int minC = dInsn->vB;
Bill Buzbee1465db52009-09-23 17:17:35 -07003528 RegLocation rlIdx = cUnit->regLocation[mir->dalvikInsn.vA];
Ben Cheng4238ec22009-08-24 16:32:22 -07003529
3530 /* regIdx <- initial index value */
Bill Buzbee1465db52009-09-23 17:17:35 -07003531 rlIdx = loadValue(cUnit, rlIdx, kCoreReg);
Ben Cheng4238ec22009-08-24 16:32:22 -07003532
3533 /* Punt if "regIdxInit + minC >= 0" is false */
Bill Buzbee1465db52009-09-23 17:17:35 -07003534 genRegImmCheck(cUnit, kArmCondLt, rlIdx.lowReg, -minC, 0,
Ben Cheng4238ec22009-08-24 16:32:22 -07003535 (ArmLIR *) cUnit->loopAnalysis->branchToPCR);
3536}
3537
3538/* Extended MIR instructions like PHI */
3539static void handleExtendedMIR(CompilationUnit *cUnit, MIR *mir)
3540{
Bill Buzbee1465db52009-09-23 17:17:35 -07003541 int opOffset = mir->dalvikInsn.opCode - kMirOpFirst;
Ben Cheng4238ec22009-08-24 16:32:22 -07003542 char *msg = dvmCompilerNew(strlen(extendedMIROpNames[opOffset]) + 1,
3543 false);
3544 strcpy(msg, extendedMIROpNames[opOffset]);
Bill Buzbee1465db52009-09-23 17:17:35 -07003545 newLIR1(cUnit, kArmPseudoExtended, (int) msg);
Ben Cheng4238ec22009-08-24 16:32:22 -07003546
3547 switch (mir->dalvikInsn.opCode) {
Bill Buzbee1465db52009-09-23 17:17:35 -07003548 case kMirOpPhi: {
Ben Cheng4238ec22009-08-24 16:32:22 -07003549 char *ssaString = dvmCompilerGetSSAString(cUnit, mir->ssaRep);
Bill Buzbee1465db52009-09-23 17:17:35 -07003550 newLIR1(cUnit, kArmPseudoSSARep, (int) ssaString);
Ben Cheng4238ec22009-08-24 16:32:22 -07003551 break;
3552 }
Bill Buzbee1465db52009-09-23 17:17:35 -07003553 case kMirOpNullNRangeUpCheck: {
Ben Cheng4238ec22009-08-24 16:32:22 -07003554 genHoistedChecksForCountUpLoop(cUnit, mir);
3555 break;
3556 }
Bill Buzbee1465db52009-09-23 17:17:35 -07003557 case kMirOpNullNRangeDownCheck: {
Ben Cheng4238ec22009-08-24 16:32:22 -07003558 genHoistedChecksForCountDownLoop(cUnit, mir);
3559 break;
3560 }
Bill Buzbee1465db52009-09-23 17:17:35 -07003561 case kMirOpLowerBound: {
Ben Cheng4238ec22009-08-24 16:32:22 -07003562 genHoistedLowerBoundCheck(cUnit, mir);
3563 break;
3564 }
Bill Buzbee1465db52009-09-23 17:17:35 -07003565 case kMirOpPunt: {
Ben Cheng4238ec22009-08-24 16:32:22 -07003566 genUnconditionalBranch(cUnit,
3567 (ArmLIR *) cUnit->loopAnalysis->branchToPCR);
3568 break;
3569 }
3570 default:
3571 break;
3572 }
3573}
3574
3575/*
3576 * Create a PC-reconstruction cell for the starting offset of this trace.
3577 * Since the PCR cell is placed near the end of the compiled code which is
3578 * usually out of range for a conditional branch, we put two branches (one
3579 * branch over to the loop body and one layover branch to the actual PCR) at the
3580 * end of the entry block.
3581 */
3582static void setupLoopEntryBlock(CompilationUnit *cUnit, BasicBlock *entry,
3583 ArmLIR *bodyLabel)
3584{
3585 /* Set up the place holder to reconstruct this Dalvik PC */
3586 ArmLIR *pcrLabel = dvmCompilerNew(sizeof(ArmLIR), true);
Ben Chenga4973592010-03-31 11:59:18 -07003587 pcrLabel->opCode = kArmPseudoPCReconstructionCell;
Ben Cheng4238ec22009-08-24 16:32:22 -07003588 pcrLabel->operands[0] =
3589 (int) (cUnit->method->insns + entry->startOffset);
3590 pcrLabel->operands[1] = entry->startOffset;
3591 /* Insert the place holder to the growable list */
3592 dvmInsertGrowableList(&cUnit->pcReconstructionList, pcrLabel);
3593
3594 /*
3595 * Next, create two branches - one branch over to the loop body and the
3596 * other branch to the PCR cell to punt.
3597 */
3598 ArmLIR *branchToBody = dvmCompilerNew(sizeof(ArmLIR), true);
Bill Buzbee1465db52009-09-23 17:17:35 -07003599 branchToBody->opCode = kThumbBUncond;
Ben Cheng4238ec22009-08-24 16:32:22 -07003600 branchToBody->generic.target = (LIR *) bodyLabel;
Ben Chengdcf3e5d2009-09-11 13:42:05 -07003601 setupResourceMasks(branchToBody);
Ben Cheng4238ec22009-08-24 16:32:22 -07003602 cUnit->loopAnalysis->branchToBody = (LIR *) branchToBody;
3603
3604 ArmLIR *branchToPCR = dvmCompilerNew(sizeof(ArmLIR), true);
Bill Buzbee1465db52009-09-23 17:17:35 -07003605 branchToPCR->opCode = kThumbBUncond;
Ben Cheng4238ec22009-08-24 16:32:22 -07003606 branchToPCR->generic.target = (LIR *) pcrLabel;
Ben Chengdcf3e5d2009-09-11 13:42:05 -07003607 setupResourceMasks(branchToPCR);
Ben Cheng4238ec22009-08-24 16:32:22 -07003608 cUnit->loopAnalysis->branchToPCR = (LIR *) branchToPCR;
3609}
3610
Ben Chengd5adae12010-03-26 17:45:28 -07003611#if defined(WITH_SELF_VERIFICATION)
3612static bool selfVerificationPuntOps(MIR *mir)
3613{
3614 DecodedInstruction *decInsn = &mir->dalvikInsn;
3615 OpCode op = decInsn->opCode;
3616 int flags = dexGetInstrFlags(gDvm.instrFlags, op);
3617 /*
3618 * All opcodes that can throw exceptions and use the
3619 * TEMPLATE_THROW_EXCEPTION_COMMON template should be excluded in the trace
3620 * under self-verification mode.
3621 */
3622 return (op == OP_MONITOR_ENTER || op == OP_MONITOR_EXIT ||
3623 op == OP_NEW_INSTANCE || op == OP_NEW_ARRAY ||
3624 op == OP_CHECK_CAST || op == OP_MOVE_EXCEPTION ||
3625 op == OP_FILL_ARRAY_DATA || op == OP_EXECUTE_INLINE ||
3626 op == OP_EXECUTE_INLINE_RANGE ||
3627 (flags & kInstrInvoke));
3628}
3629#endif
3630
Ben Chengba4fc8b2009-06-01 13:00:29 -07003631void dvmCompilerMIR2LIR(CompilationUnit *cUnit)
3632{
3633 /* Used to hold the labels of each block */
Bill Buzbee89efc3d2009-07-28 11:22:22 -07003634 ArmLIR *labelList =
3635 dvmCompilerNew(sizeof(ArmLIR) * cUnit->numBlocks, true);
Ben Chengcec26f62010-01-15 15:29:33 -08003636 GrowableList chainingListByType[kChainingCellGap];
Ben Chengba4fc8b2009-06-01 13:00:29 -07003637 int i;
3638
3639 /*
Ben Cheng38329f52009-07-07 14:19:20 -07003640 * Initialize various types chaining lists.
Ben Chengba4fc8b2009-06-01 13:00:29 -07003641 */
Ben Chengcec26f62010-01-15 15:29:33 -08003642 for (i = 0; i < kChainingCellGap; i++) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07003643 dvmInitGrowableList(&chainingListByType[i], 2);
3644 }
3645
3646 BasicBlock **blockList = cUnit->blockList;
3647
Bill Buzbee6e963e12009-06-17 16:56:19 -07003648 if (cUnit->executionCount) {
3649 /*
3650 * Reserve 6 bytes at the beginning of the trace
3651 * +----------------------------+
3652 * | execution count (4 bytes) |
3653 * +----------------------------+
3654 * | chain cell offset (2 bytes)|
3655 * +----------------------------+
3656 * ...and then code to increment the execution
3657 * count:
3658 * mov r0, pc @ move adr of "mov r0,pc" + 4 to r0
3659 * sub r0, #10 @ back up to addr of executionCount
3660 * ldr r1, [r0]
3661 * add r1, #1
3662 * str r1, [r0]
3663 */
Bill Buzbee1465db52009-09-23 17:17:35 -07003664 newLIR1(cUnit, kArm16BitData, 0);
3665 newLIR1(cUnit, kArm16BitData, 0);
Ben Chengcc6600c2009-06-22 14:45:16 -07003666 cUnit->chainCellOffsetLIR =
Bill Buzbee1465db52009-09-23 17:17:35 -07003667 (LIR *) newLIR1(cUnit, kArm16BitData, CHAIN_CELL_OFFSET_TAG);
Bill Buzbee6e963e12009-06-17 16:56:19 -07003668 cUnit->headerSize = 6;
Bill Buzbee270c1d62009-08-13 16:58:07 -07003669 /* Thumb instruction used directly here to ensure correct size */
Bill Buzbee1465db52009-09-23 17:17:35 -07003670 newLIR2(cUnit, kThumbMovRR_H2L, r0, rpc);
3671 newLIR2(cUnit, kThumbSubRI8, r0, 10);
3672 newLIR3(cUnit, kThumbLdrRRI5, r1, r0, 0);
3673 newLIR2(cUnit, kThumbAddRI8, r1, 1);
3674 newLIR3(cUnit, kThumbStrRRI5, r1, r0, 0);
Bill Buzbee6e963e12009-06-17 16:56:19 -07003675 } else {
3676 /* Just reserve 2 bytes for the chain cell offset */
Ben Chengcc6600c2009-06-22 14:45:16 -07003677 cUnit->chainCellOffsetLIR =
Bill Buzbee1465db52009-09-23 17:17:35 -07003678 (LIR *) newLIR1(cUnit, kArm16BitData, CHAIN_CELL_OFFSET_TAG);
Bill Buzbee6e963e12009-06-17 16:56:19 -07003679 cUnit->headerSize = 2;
3680 }
Ben Cheng1efc9c52009-06-08 18:25:27 -07003681
Ben Chengba4fc8b2009-06-01 13:00:29 -07003682 /* Handle the content in each basic block */
3683 for (i = 0; i < cUnit->numBlocks; i++) {
3684 blockList[i]->visited = true;
3685 MIR *mir;
3686
3687 labelList[i].operands[0] = blockList[i]->startOffset;
3688
Ben Chengcec26f62010-01-15 15:29:33 -08003689 if (blockList[i]->blockType >= kChainingCellGap) {
Ben Chengd44faf52010-06-02 15:33:51 -07003690 if (blockList[i]->firstMIRInsn != NULL &&
3691 ((blockList[i]->firstMIRInsn->dalvikInsn.opCode ==
3692 OP_MOVE_RESULT) ||
3693 (blockList[i]->firstMIRInsn->dalvikInsn.opCode ==
3694 OP_MOVE_RESULT_WIDE) ||
3695 (blockList[i]->firstMIRInsn->dalvikInsn.opCode ==
3696 OP_MOVE_RESULT_OBJECT))) {
3697 /* Align this block first since it is a return chaining cell */
3698 newLIR0(cUnit, kArmPseudoPseudoAlign4);
3699 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07003700 /*
3701 * Append the label pseudo LIR first. Chaining cells will be handled
3702 * separately afterwards.
3703 */
3704 dvmCompilerAppendLIR(cUnit, (LIR *) &labelList[i]);
3705 }
3706
Bill Buzbee1465db52009-09-23 17:17:35 -07003707 if (blockList[i]->blockType == kEntryBlock) {
Ben Chenga4973592010-03-31 11:59:18 -07003708 labelList[i].opCode = kArmPseudoEntryBlock;
Ben Cheng4238ec22009-08-24 16:32:22 -07003709 if (blockList[i]->firstMIRInsn == NULL) {
3710 continue;
3711 } else {
3712 setupLoopEntryBlock(cUnit, blockList[i],
3713 &labelList[blockList[i]->fallThrough->id]);
3714 }
Bill Buzbee1465db52009-09-23 17:17:35 -07003715 } else if (blockList[i]->blockType == kExitBlock) {
Ben Chenga4973592010-03-31 11:59:18 -07003716 labelList[i].opCode = kArmPseudoExitBlock;
Ben Cheng4238ec22009-08-24 16:32:22 -07003717 goto gen_fallthrough;
Bill Buzbee1465db52009-09-23 17:17:35 -07003718 } else if (blockList[i]->blockType == kDalvikByteCode) {
3719 labelList[i].opCode = kArmPseudoNormalBlockLabel;
Ben Chenge9695e52009-06-16 16:11:47 -07003720 /* Reset the register state */
Bill Buzbeec6f10662010-02-09 11:16:15 -08003721 dvmCompilerResetRegPool(cUnit);
3722 dvmCompilerClobberAllRegs(cUnit);
3723 dvmCompilerResetNullCheck(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003724 } else {
3725 switch (blockList[i]->blockType) {
Bill Buzbee1465db52009-09-23 17:17:35 -07003726 case kChainingCellNormal:
Ben Chenga4973592010-03-31 11:59:18 -07003727 labelList[i].opCode = kArmPseudoChainingCellNormal;
Ben Chengba4fc8b2009-06-01 13:00:29 -07003728 /* handle the codegen later */
3729 dvmInsertGrowableList(
Bill Buzbee1465db52009-09-23 17:17:35 -07003730 &chainingListByType[kChainingCellNormal], (void *) i);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003731 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07003732 case kChainingCellInvokeSingleton:
Ben Cheng38329f52009-07-07 14:19:20 -07003733 labelList[i].opCode =
Ben Chenga4973592010-03-31 11:59:18 -07003734 kArmPseudoChainingCellInvokeSingleton;
Ben Chengba4fc8b2009-06-01 13:00:29 -07003735 labelList[i].operands[0] =
3736 (int) blockList[i]->containingMethod;
3737 /* handle the codegen later */
3738 dvmInsertGrowableList(
Bill Buzbee1465db52009-09-23 17:17:35 -07003739 &chainingListByType[kChainingCellInvokeSingleton],
Ben Cheng38329f52009-07-07 14:19:20 -07003740 (void *) i);
3741 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07003742 case kChainingCellInvokePredicted:
Ben Cheng38329f52009-07-07 14:19:20 -07003743 labelList[i].opCode =
Ben Chenga4973592010-03-31 11:59:18 -07003744 kArmPseudoChainingCellInvokePredicted;
Ben Cheng38329f52009-07-07 14:19:20 -07003745 /* handle the codegen later */
3746 dvmInsertGrowableList(
Bill Buzbee1465db52009-09-23 17:17:35 -07003747 &chainingListByType[kChainingCellInvokePredicted],
Ben Cheng38329f52009-07-07 14:19:20 -07003748 (void *) i);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003749 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07003750 case kChainingCellHot:
Ben Chengba4fc8b2009-06-01 13:00:29 -07003751 labelList[i].opCode =
Ben Chenga4973592010-03-31 11:59:18 -07003752 kArmPseudoChainingCellHot;
Ben Chengba4fc8b2009-06-01 13:00:29 -07003753 /* handle the codegen later */
3754 dvmInsertGrowableList(
Bill Buzbee1465db52009-09-23 17:17:35 -07003755 &chainingListByType[kChainingCellHot],
Ben Chengba4fc8b2009-06-01 13:00:29 -07003756 (void *) i);
3757 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07003758 case kPCReconstruction:
Ben Chengba4fc8b2009-06-01 13:00:29 -07003759 /* Make sure exception handling block is next */
3760 labelList[i].opCode =
Ben Chenga4973592010-03-31 11:59:18 -07003761 kArmPseudoPCReconstructionBlockLabel;
Ben Chengba4fc8b2009-06-01 13:00:29 -07003762 assert (i == cUnit->numBlocks - 2);
3763 handlePCReconstruction(cUnit, &labelList[i+1]);
3764 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07003765 case kExceptionHandling:
3766 labelList[i].opCode = kArmPseudoEHBlockLabel;
Ben Chengba4fc8b2009-06-01 13:00:29 -07003767 if (cUnit->pcReconstructionList.numUsed) {
Bill Buzbee270c1d62009-08-13 16:58:07 -07003768 loadWordDisp(cUnit, rGLUE, offsetof(InterpState,
3769 jitToInterpEntries.dvmJitToInterpPunt),
3770 r1);
Bill Buzbee1465db52009-09-23 17:17:35 -07003771 opReg(cUnit, kOpBlx, r1);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003772 }
3773 break;
Bill Buzbee9c4b7c82009-09-10 10:10:38 -07003774#if defined(WITH_SELF_VERIFICATION) || defined(WITH_JIT_TUNING)
Bill Buzbee1465db52009-09-23 17:17:35 -07003775 case kChainingCellBackwardBranch:
Jeff Hao97319a82009-08-12 16:57:15 -07003776 labelList[i].opCode =
Ben Chenga4973592010-03-31 11:59:18 -07003777 kArmPseudoChainingCellBackwardBranch;
Jeff Hao97319a82009-08-12 16:57:15 -07003778 /* handle the codegen later */
3779 dvmInsertGrowableList(
Bill Buzbee1465db52009-09-23 17:17:35 -07003780 &chainingListByType[kChainingCellBackwardBranch],
Jeff Hao97319a82009-08-12 16:57:15 -07003781 (void *) i);
3782 break;
3783#endif
Ben Chengba4fc8b2009-06-01 13:00:29 -07003784 default:
3785 break;
3786 }
3787 continue;
3788 }
Ben Chenge9695e52009-06-16 16:11:47 -07003789
Bill Buzbee89efc3d2009-07-28 11:22:22 -07003790 ArmLIR *headLIR = NULL;
Ben Chenge9695e52009-06-16 16:11:47 -07003791
Ben Chengba4fc8b2009-06-01 13:00:29 -07003792 for (mir = blockList[i]->firstMIRInsn; mir; mir = mir->next) {
Bill Buzbee1465db52009-09-23 17:17:35 -07003793
Bill Buzbeec6f10662010-02-09 11:16:15 -08003794 dvmCompilerResetRegPool(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07003795 if (gDvmJit.disableOpt & (1 << kTrackLiveTemps)) {
Bill Buzbeec6f10662010-02-09 11:16:15 -08003796 dvmCompilerClobberAllRegs(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07003797 }
3798
3799 if (gDvmJit.disableOpt & (1 << kSuppressLoads)) {
Bill Buzbeec6f10662010-02-09 11:16:15 -08003800 dvmCompilerResetDefTracking(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07003801 }
3802
3803 if (mir->dalvikInsn.opCode >= kMirOpFirst) {
Ben Cheng4238ec22009-08-24 16:32:22 -07003804 handleExtendedMIR(cUnit, mir);
3805 continue;
3806 }
3807
Bill Buzbee1465db52009-09-23 17:17:35 -07003808
Ben Chengba4fc8b2009-06-01 13:00:29 -07003809 OpCode dalvikOpCode = mir->dalvikInsn.opCode;
3810 InstructionFormat dalvikFormat =
3811 dexGetInstrFormat(gDvm.instrFormat, dalvikOpCode);
Bill Buzbee89efc3d2009-07-28 11:22:22 -07003812 ArmLIR *boundaryLIR =
Ben Chenga4973592010-03-31 11:59:18 -07003813 newLIR2(cUnit, kArmPseudoDalvikByteCodeBoundary,
Ben Chengccd6c012009-10-15 14:52:45 -07003814 mir->offset,
3815 (int) dvmCompilerGetDalvikDisassembly(&mir->dalvikInsn)
3816 );
Ben Cheng4238ec22009-08-24 16:32:22 -07003817 if (mir->ssaRep) {
3818 char *ssaString = dvmCompilerGetSSAString(cUnit, mir->ssaRep);
Bill Buzbee1465db52009-09-23 17:17:35 -07003819 newLIR1(cUnit, kArmPseudoSSARep, (int) ssaString);
Ben Cheng4238ec22009-08-24 16:32:22 -07003820 }
3821
Ben Chenge9695e52009-06-16 16:11:47 -07003822 /* Remember the first LIR for this block */
3823 if (headLIR == NULL) {
3824 headLIR = boundaryLIR;
Ben Chengd7d426a2009-09-22 11:23:36 -07003825 /* Set the first boundaryLIR as a scheduling barrier */
3826 headLIR->defMask = ENCODE_ALL;
Ben Chenge9695e52009-06-16 16:11:47 -07003827 }
Ben Cheng4238ec22009-08-24 16:32:22 -07003828
Ben Chengba4fc8b2009-06-01 13:00:29 -07003829 bool notHandled;
3830 /*
3831 * Debugging: screen the opcode first to see if it is in the
3832 * do[-not]-compile list
3833 */
3834 bool singleStepMe =
3835 gDvmJit.includeSelectedOp !=
3836 ((gDvmJit.opList[dalvikOpCode >> 3] &
3837 (1 << (dalvikOpCode & 0x7))) !=
3838 0);
Ben Chengd5adae12010-03-26 17:45:28 -07003839#if defined(WITH_SELF_VERIFICATION)
3840 if (singleStepMe == false) {
3841 singleStepMe = selfVerificationPuntOps(mir);
3842 }
3843#endif
Ben Chengba4fc8b2009-06-01 13:00:29 -07003844 if (singleStepMe || cUnit->allSingleStep) {
3845 notHandled = false;
3846 genInterpSingleStep(cUnit, mir);
3847 } else {
3848 opcodeCoverage[dalvikOpCode]++;
3849 switch (dalvikFormat) {
3850 case kFmt10t:
3851 case kFmt20t:
3852 case kFmt30t:
3853 notHandled = handleFmt10t_Fmt20t_Fmt30t(cUnit,
3854 mir, blockList[i], labelList);
3855 break;
3856 case kFmt10x:
3857 notHandled = handleFmt10x(cUnit, mir);
3858 break;
3859 case kFmt11n:
3860 case kFmt31i:
3861 notHandled = handleFmt11n_Fmt31i(cUnit, mir);
3862 break;
3863 case kFmt11x:
3864 notHandled = handleFmt11x(cUnit, mir);
3865 break;
3866 case kFmt12x:
3867 notHandled = handleFmt12x(cUnit, mir);
3868 break;
3869 case kFmt20bc:
3870 notHandled = handleFmt20bc(cUnit, mir);
3871 break;
3872 case kFmt21c:
3873 case kFmt31c:
3874 notHandled = handleFmt21c_Fmt31c(cUnit, mir);
3875 break;
3876 case kFmt21h:
3877 notHandled = handleFmt21h(cUnit, mir);
3878 break;
3879 case kFmt21s:
3880 notHandled = handleFmt21s(cUnit, mir);
3881 break;
3882 case kFmt21t:
3883 notHandled = handleFmt21t(cUnit, mir, blockList[i],
3884 labelList);
3885 break;
3886 case kFmt22b:
3887 case kFmt22s:
3888 notHandled = handleFmt22b_Fmt22s(cUnit, mir);
3889 break;
3890 case kFmt22c:
3891 notHandled = handleFmt22c(cUnit, mir);
3892 break;
3893 case kFmt22cs:
3894 notHandled = handleFmt22cs(cUnit, mir);
3895 break;
3896 case kFmt22t:
3897 notHandled = handleFmt22t(cUnit, mir, blockList[i],
3898 labelList);
3899 break;
3900 case kFmt22x:
3901 case kFmt32x:
3902 notHandled = handleFmt22x_Fmt32x(cUnit, mir);
3903 break;
3904 case kFmt23x:
3905 notHandled = handleFmt23x(cUnit, mir);
3906 break;
3907 case kFmt31t:
3908 notHandled = handleFmt31t(cUnit, mir);
3909 break;
3910 case kFmt3rc:
3911 case kFmt35c:
3912 notHandled = handleFmt35c_3rc(cUnit, mir, blockList[i],
3913 labelList);
3914 break;
3915 case kFmt3rms:
3916 case kFmt35ms:
3917 notHandled = handleFmt35ms_3rms(cUnit, mir,blockList[i],
3918 labelList);
3919 break;
3920 case kFmt3inline:
Andy McFaddenb0a05412009-11-19 10:23:41 -08003921 case kFmt3rinline:
Bill Buzbeece46c942009-11-20 15:41:34 -08003922 notHandled = handleExecuteInline(cUnit, mir);
Andy McFaddenb0a05412009-11-19 10:23:41 -08003923 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07003924 case kFmt51l:
3925 notHandled = handleFmt51l(cUnit, mir);
3926 break;
3927 default:
3928 notHandled = true;
3929 break;
3930 }
3931 }
3932 if (notHandled) {
3933 LOGE("%#06x: Opcode 0x%x (%s) / Fmt %d not handled\n",
3934 mir->offset,
Andy McFaddenc6b25c72010-06-22 11:01:20 -07003935 dalvikOpCode, dexGetOpcodeName(dalvikOpCode),
Ben Chengba4fc8b2009-06-01 13:00:29 -07003936 dalvikFormat);
Bill Buzbeefc519dc2010-03-06 23:30:57 -08003937 dvmCompilerAbort(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003938 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07003939 }
3940 }
Ben Cheng4238ec22009-08-24 16:32:22 -07003941
Bill Buzbee1465db52009-09-23 17:17:35 -07003942 if (blockList[i]->blockType == kEntryBlock) {
Ben Cheng4238ec22009-08-24 16:32:22 -07003943 dvmCompilerAppendLIR(cUnit,
3944 (LIR *) cUnit->loopAnalysis->branchToBody);
3945 dvmCompilerAppendLIR(cUnit,
3946 (LIR *) cUnit->loopAnalysis->branchToPCR);
3947 }
3948
3949 if (headLIR) {
3950 /*
3951 * Eliminate redundant loads/stores and delay stores into later
3952 * slots
3953 */
3954 dvmCompilerApplyLocalOptimizations(cUnit, (LIR *) headLIR,
3955 cUnit->lastLIRInsn);
3956 }
3957
3958gen_fallthrough:
Ben Cheng1efc9c52009-06-08 18:25:27 -07003959 /*
3960 * Check if the block is terminated due to trace length constraint -
3961 * insert an unconditional branch to the chaining cell.
3962 */
3963 if (blockList[i]->needFallThroughBranch) {
3964 genUnconditionalBranch(cUnit,
3965 &labelList[blockList[i]->fallThrough->id]);
3966 }
3967
Ben Chengba4fc8b2009-06-01 13:00:29 -07003968 }
3969
Ben Chenge9695e52009-06-16 16:11:47 -07003970 /* Handle the chaining cells in predefined order */
Ben Chengcec26f62010-01-15 15:29:33 -08003971 for (i = 0; i < kChainingCellGap; i++) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07003972 size_t j;
3973 int *blockIdList = (int *) chainingListByType[i].elemList;
3974
3975 cUnit->numChainingCells[i] = chainingListByType[i].numUsed;
3976
3977 /* No chaining cells of this type */
3978 if (cUnit->numChainingCells[i] == 0)
3979 continue;
3980
3981 /* Record the first LIR for a new type of chaining cell */
3982 cUnit->firstChainingLIR[i] = (LIR *) &labelList[blockIdList[0]];
3983
3984 for (j = 0; j < chainingListByType[i].numUsed; j++) {
3985 int blockId = blockIdList[j];
3986
3987 /* Align this chaining cell first */
Bill Buzbee1465db52009-09-23 17:17:35 -07003988 newLIR0(cUnit, kArmPseudoPseudoAlign4);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003989
3990 /* Insert the pseudo chaining instruction */
3991 dvmCompilerAppendLIR(cUnit, (LIR *) &labelList[blockId]);
3992
3993
3994 switch (blockList[blockId]->blockType) {
Bill Buzbee1465db52009-09-23 17:17:35 -07003995 case kChainingCellNormal:
Ben Cheng1efc9c52009-06-08 18:25:27 -07003996 handleNormalChainingCell(cUnit,
Ben Chengba4fc8b2009-06-01 13:00:29 -07003997 blockList[blockId]->startOffset);
3998 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07003999 case kChainingCellInvokeSingleton:
Ben Cheng38329f52009-07-07 14:19:20 -07004000 handleInvokeSingletonChainingCell(cUnit,
Ben Chengba4fc8b2009-06-01 13:00:29 -07004001 blockList[blockId]->containingMethod);
4002 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07004003 case kChainingCellInvokePredicted:
Ben Cheng38329f52009-07-07 14:19:20 -07004004 handleInvokePredictedChainingCell(cUnit);
4005 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07004006 case kChainingCellHot:
Ben Cheng1efc9c52009-06-08 18:25:27 -07004007 handleHotChainingCell(cUnit,
Ben Chengba4fc8b2009-06-01 13:00:29 -07004008 blockList[blockId]->startOffset);
4009 break;
Bill Buzbee9c4b7c82009-09-10 10:10:38 -07004010#if defined(WITH_SELF_VERIFICATION) || defined(WITH_JIT_TUNING)
Bill Buzbee1465db52009-09-23 17:17:35 -07004011 case kChainingCellBackwardBranch:
Jeff Hao97319a82009-08-12 16:57:15 -07004012 handleBackwardBranchChainingCell(cUnit,
4013 blockList[blockId]->startOffset);
4014 break;
4015#endif
Ben Chengba4fc8b2009-06-01 13:00:29 -07004016 default:
Bill Buzbee1465db52009-09-23 17:17:35 -07004017 LOGE("Bad blocktype %d", blockList[blockId]->blockType);
Bill Buzbeefc519dc2010-03-06 23:30:57 -08004018 dvmCompilerAbort(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -07004019 }
4020 }
4021 }
Ben Chenge9695e52009-06-16 16:11:47 -07004022
Ben Chengcec26f62010-01-15 15:29:33 -08004023 /* Mark the bottom of chaining cells */
4024 cUnit->chainingCellBottom = (LIR *) newLIR0(cUnit, kArmChainingCellBottom);
4025
Ben Cheng6c10a972009-10-29 14:39:18 -07004026 /*
4027 * Generate the branch to the dvmJitToInterpNoChain entry point at the end
4028 * of all chaining cells for the overflow cases.
4029 */
4030 if (cUnit->switchOverflowPad) {
4031 loadConstant(cUnit, r0, (int) cUnit->switchOverflowPad);
4032 loadWordDisp(cUnit, rGLUE, offsetof(InterpState,
4033 jitToInterpEntries.dvmJitToInterpNoChain), r2);
4034 opRegReg(cUnit, kOpAdd, r1, r1);
4035 opRegRegReg(cUnit, kOpAdd, r4PC, r0, r1);
Ben Cheng978738d2010-05-13 13:45:57 -07004036#if defined(WITH_JIT_TUNING)
Ben Cheng6c10a972009-10-29 14:39:18 -07004037 loadConstant(cUnit, r0, kSwitchOverflow);
4038#endif
4039 opReg(cUnit, kOpBlx, r2);
4040 }
4041
Ben Chenge9695e52009-06-16 16:11:47 -07004042 dvmCompilerApplyGlobalOptimizations(cUnit);
jeffhao9e45c0b2010-02-03 10:24:05 -08004043
4044#if defined(WITH_SELF_VERIFICATION)
4045 selfVerificationBranchInsertPass(cUnit);
4046#endif
Ben Chengba4fc8b2009-06-01 13:00:29 -07004047}
4048
4049/* Accept the work and start compiling */
Bill Buzbee716f1202009-07-23 13:22:09 -07004050bool dvmCompilerDoWork(CompilerWorkOrder *work)
Ben Chengba4fc8b2009-06-01 13:00:29 -07004051{
Ben Chengccd6c012009-10-15 14:52:45 -07004052 bool res;
Ben Chengba4fc8b2009-06-01 13:00:29 -07004053
Ben Cheng6999d842010-01-26 16:46:15 -08004054 if (gDvmJit.codeCacheFull) {
Ben Chengccd6c012009-10-15 14:52:45 -07004055 return false;
4056 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07004057
Ben Chengccd6c012009-10-15 14:52:45 -07004058 switch (work->kind) {
4059 case kWorkOrderMethod:
4060 res = dvmCompileMethod(work->info, &work->result);
4061 break;
4062 case kWorkOrderTrace:
4063 /* Start compilation with maximally allowed trace length */
Bill Buzbeefc519dc2010-03-06 23:30:57 -08004064 res = dvmCompileTrace(work->info, JIT_MAX_TRACE_LEN, &work->result,
4065 work->bailPtr);
Ben Chengccd6c012009-10-15 14:52:45 -07004066 break;
4067 case kWorkOrderTraceDebug: {
4068 bool oldPrintMe = gDvmJit.printMe;
4069 gDvmJit.printMe = true;
4070 /* Start compilation with maximally allowed trace length */
Bill Buzbeefc519dc2010-03-06 23:30:57 -08004071 res = dvmCompileTrace(work->info, JIT_MAX_TRACE_LEN, &work->result,
4072 work->bailPtr);
Elliott Hughes672511b2010-04-26 17:40:13 -07004073 gDvmJit.printMe = oldPrintMe;
Ben Chengccd6c012009-10-15 14:52:45 -07004074 break;
4075 }
4076 default:
4077 res = false;
Bill Buzbeefc519dc2010-03-06 23:30:57 -08004078 LOGE("Jit: unknown work order type");
Elliott Hughes672511b2010-04-26 17:40:13 -07004079 assert(0); // Bail if debug build, discard otherwise
Ben Chengccd6c012009-10-15 14:52:45 -07004080 }
4081 return res;
Ben Chengba4fc8b2009-06-01 13:00:29 -07004082}
4083
Ben Chengba4fc8b2009-06-01 13:00:29 -07004084/* Architectural-specific debugging helpers go here */
4085void dvmCompilerArchDump(void)
4086{
4087 /* Print compiled opcode in this VM instance */
4088 int i, start, streak;
4089 char buf[1024];
4090
4091 streak = i = 0;
4092 buf[0] = 0;
4093 while (opcodeCoverage[i] == 0 && i < 256) {
4094 i++;
4095 }
4096 if (i == 256) {
4097 return;
4098 }
4099 for (start = i++, streak = 1; i < 256; i++) {
4100 if (opcodeCoverage[i]) {
4101 streak++;
4102 } else {
4103 if (streak == 1) {
4104 sprintf(buf+strlen(buf), "%x,", start);
4105 } else {
4106 sprintf(buf+strlen(buf), "%x-%x,", start, start + streak - 1);
4107 }
4108 streak = 0;
4109 while (opcodeCoverage[i] == 0 && i < 256) {
4110 i++;
4111 }
4112 if (i < 256) {
4113 streak = 1;
4114 start = i;
4115 }
4116 }
4117 }
4118 if (streak) {
4119 if (streak == 1) {
4120 sprintf(buf+strlen(buf), "%x", start);
4121 } else {
4122 sprintf(buf+strlen(buf), "%x-%x", start, start + streak - 1);
4123 }
4124 }
4125 if (strlen(buf)) {
Ben Cheng8b258bf2009-06-24 17:27:07 -07004126 LOGD("dalvik.vm.jit.op = %s", buf);
Ben Chengba4fc8b2009-06-01 13:00:29 -07004127 }
4128}
Ben Chengd7d426a2009-09-22 11:23:36 -07004129
4130/* Common initialization routine for an architecture family */
4131bool dvmCompilerArchInit()
4132{
4133 int i;
4134
Bill Buzbee1465db52009-09-23 17:17:35 -07004135 for (i = 0; i < kArmLast; i++) {
Ben Chengd7d426a2009-09-22 11:23:36 -07004136 if (EncodingMap[i].opCode != i) {
4137 LOGE("Encoding order for %s is wrong: expecting %d, seeing %d",
4138 EncodingMap[i].name, i, EncodingMap[i].opCode);
Bill Buzbeefc519dc2010-03-06 23:30:57 -08004139 dvmAbort(); // OK to dvmAbort - build error
Ben Chengd7d426a2009-09-22 11:23:36 -07004140 }
4141 }
4142
Ben Cheng5d90c202009-11-22 23:31:11 -08004143 return dvmCompilerArchVariantInit();
4144}
4145
4146void *dvmCompilerGetInterpretTemplate()
4147{
4148 return (void*) ((int)gDvmJit.codeCache +
4149 templateEntryOffsets[TEMPLATE_INTERPRET]);
4150}
4151
4152/* Needed by the ld/st optmizatons */
4153ArmLIR* dvmCompilerRegCopyNoInsert(CompilationUnit *cUnit, int rDest, int rSrc)
4154{
4155 return genRegCopyNoInsert(cUnit, rDest, rSrc);
4156}
4157
4158/* Needed by the register allocator */
4159ArmLIR* dvmCompilerRegCopy(CompilationUnit *cUnit, int rDest, int rSrc)
4160{
4161 return genRegCopy(cUnit, rDest, rSrc);
4162}
4163
4164/* Needed by the register allocator */
4165void dvmCompilerRegCopyWide(CompilationUnit *cUnit, int destLo, int destHi,
4166 int srcLo, int srcHi)
4167{
4168 genRegCopyWide(cUnit, destLo, destHi, srcLo, srcHi);
4169}
4170
4171void dvmCompilerFlushRegImpl(CompilationUnit *cUnit, int rBase,
4172 int displacement, int rSrc, OpSize size)
4173{
4174 storeBaseDisp(cUnit, rBase, displacement, rSrc, size);
4175}
4176
4177void dvmCompilerFlushRegWideImpl(CompilationUnit *cUnit, int rBase,
4178 int displacement, int rSrcLo, int rSrcHi)
4179{
4180 storeBaseDispWide(cUnit, rBase, displacement, rSrcLo, rSrcHi);
Ben Chengd7d426a2009-09-22 11:23:36 -07004181}