blob: 8e1b09c6035b9942db1539f0ae1aa6e970648238 [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);
buzbee8f8109a2010-08-31 10:16:35 -070034 ArmLIR *branchOver = genCmpImmBranch(cUnit, kArmCondEq, valReg, 0);
buzbee919eb062010-07-12 12:59:22 -070035 loadWordDisp(cUnit, rGLUE, offsetof(InterpState, cardTable),
36 regCardBase);
37 opRegRegImm(cUnit, kOpLsr, regCardNo, tgtAddrReg, GC_CARD_SHIFT);
38 storeBaseIndexed(cUnit, regCardBase, regCardNo, regCardBase, 0,
39 kUnsignedByte);
40 ArmLIR *target = newLIR0(cUnit, kArmPseudoTargetLabel);
41 target->defMask = ENCODE_ALL;
42 branchOver->generic.target = (LIR *)target;
buzbeebaf196a2010-08-04 10:13:15 -070043 dvmCompilerFreeTemp(cUnit, regCardBase);
44 dvmCompilerFreeTemp(cUnit, regCardNo);
buzbee919eb062010-07-12 12:59:22 -070045}
46
Ben Cheng5d90c202009-11-22 23:31:11 -080047static bool genConversionCall(CompilationUnit *cUnit, MIR *mir, void *funct,
48 int srcSize, int tgtSize)
49{
50 /*
51 * Don't optimize the register usage since it calls out to template
52 * functions
53 */
54 RegLocation rlSrc;
55 RegLocation rlDest;
Bill Buzbeec6f10662010-02-09 11:16:15 -080056 dvmCompilerFlushAllRegs(cUnit); /* Send everything to home location */
Ben Cheng5d90c202009-11-22 23:31:11 -080057 if (srcSize == 1) {
Bill Buzbeec6f10662010-02-09 11:16:15 -080058 rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
Ben Cheng5d90c202009-11-22 23:31:11 -080059 loadValueDirectFixed(cUnit, rlSrc, r0);
60 } else {
Bill Buzbeec6f10662010-02-09 11:16:15 -080061 rlSrc = dvmCompilerGetSrcWide(cUnit, mir, 0, 1);
Ben Cheng5d90c202009-11-22 23:31:11 -080062 loadValueDirectWideFixed(cUnit, rlSrc, r0, r1);
63 }
Ben Chengbd1326d2010-04-02 15:04:53 -070064 LOAD_FUNC_ADDR(cUnit, r2, (int)funct);
Ben Cheng5d90c202009-11-22 23:31:11 -080065 opReg(cUnit, kOpBlx, r2);
Elliott Hughes6a555132010-02-25 15:41:42 -080066 dvmCompilerClobberCallRegs(cUnit);
Ben Cheng5d90c202009-11-22 23:31:11 -080067 if (tgtSize == 1) {
68 RegLocation rlResult;
Bill Buzbeec6f10662010-02-09 11:16:15 -080069 rlDest = dvmCompilerGetDest(cUnit, mir, 0);
70 rlResult = dvmCompilerGetReturn(cUnit);
Ben Cheng5d90c202009-11-22 23:31:11 -080071 storeValue(cUnit, rlDest, rlResult);
72 } else {
73 RegLocation rlResult;
Bill Buzbeec6f10662010-02-09 11:16:15 -080074 rlDest = dvmCompilerGetDestWide(cUnit, mir, 0, 1);
75 rlResult = dvmCompilerGetReturnWide(cUnit);
Ben Cheng5d90c202009-11-22 23:31:11 -080076 storeValueWide(cUnit, rlDest, rlResult);
77 }
78 return false;
79}
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
Dan Bornstein9a1f8162010-12-01 17:02:26 -080088 switch (mir->dalvikInsn.opcode) {
Ben Cheng5d90c202009-11-22 23:31:11 -080089 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
Dan Bornstein9a1f8162010-12-01 17:02:26 -0800134 switch (mir->dalvikInsn.opcode) {
Ben Cheng5d90c202009-11-22 23:31:11 -0800135 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{
Dan Bornstein9a1f8162010-12-01 17:02:26 -0800175 Opcode opcode = mir->dalvikInsn.opcode;
Ben Cheng5d90c202009-11-22 23:31:11 -0800176
Dan Bornstein9a1f8162010-12-01 17:02:26 -0800177 switch (opcode) {
Ben Cheng5d90c202009-11-22 23:31:11 -0800178 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)
Dan Bornstein9a1f8162010-12-01 17:02:26 -0800205static void selfVerificationBranchInsert(LIR *currentLIR, ArmOpcode opcode,
jeffhao9e45c0b2010-02-03 10:24:05 -0800206 int dest, int src1)
Jeff Hao97319a82009-08-12 16:57:15 -0700207{
Carl Shapirofc75f3e2010-12-07 11:43:38 -0800208 ArmLIR *insn = (ArmLIR *) dvmCompilerNew(sizeof(ArmLIR), true);
Dan Bornstein9a1f8162010-12-01 17:02:26 -0800209 insn->opcode = opcode;
jeffhao9e45c0b2010-02-03 10:24:05 -0800210 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;
Dan Bornstein9a1f8162010-12-01 17:02:26 -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,
Dan Bornstein9a1f8162010-12-01 17:02:26 -0800227 (int) gDvmJit.codeCache + templateEntryOffsets[opcode],
228 (int) gDvmJit.codeCache + templateEntryOffsets[opcode]);
jeffhao9e45c0b2010-02-03 10:24:05 -0800229 selfVerificationBranchInsert((LIR *) thisLIR, kThumbBlx2,
Dan Bornstein9a1f8162010-12-01 17:02:26 -0800230 (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,
buzbeeecf8f6e2010-07-20 14:53:42 -0700304 int fieldOffset, bool isVolatile)
Ben Chengba4fc8b2009-06-01 13:00:29 -0700305{
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);
buzbeeecf8f6e2010-07-20 14:53:42 -0700319 if (isVolatile) {
buzbee2ce33c92010-11-01 15:53:27 -0700320 dvmCompilerGenMemBarrier(cUnit, kSY);
buzbeeecf8f6e2010-07-20 14:53:42 -0700321 }
Ben Cheng11d8f142010-03-24 15:24:19 -0700322
Bill Buzbee1465db52009-09-23 17:17:35 -0700323 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700324}
325
326/*
327 * Store a field to an object instance
328 *
Ben Chengba4fc8b2009-06-01 13:00:29 -0700329 */
Bill Buzbee270c1d62009-08-13 16:58:07 -0700330static void genIPut(CompilationUnit *cUnit, MIR *mir, OpSize size,
buzbeeecf8f6e2010-07-20 14:53:42 -0700331 int fieldOffset, bool isObject, bool isVolatile)
Ben Chengba4fc8b2009-06-01 13:00:29 -0700332{
Bill Buzbee749e8162010-07-07 06:55:56 -0700333 RegisterClass regClass = dvmCompilerRegClassBySize(size);
Bill Buzbeec6f10662010-02-09 11:16:15 -0800334 RegLocation rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
335 RegLocation rlObj = dvmCompilerGetSrc(cUnit, mir, 1);
Bill Buzbee1465db52009-09-23 17:17:35 -0700336 rlObj = loadValue(cUnit, rlObj, kCoreReg);
Bill Buzbee749e8162010-07-07 06:55:56 -0700337 rlSrc = loadValue(cUnit, rlSrc, regClass);
Bill Buzbee1465db52009-09-23 17:17:35 -0700338 genNullCheck(cUnit, rlObj.sRegLow, rlObj.lowReg, mir->offset,
339 NULL);/* null object? */
Ben Cheng11d8f142010-03-24 15:24:19 -0700340
buzbeeecf8f6e2010-07-20 14:53:42 -0700341 if (isVolatile) {
buzbee2ce33c92010-11-01 15:53:27 -0700342 dvmCompilerGenMemBarrier(cUnit, kSY);
buzbeeecf8f6e2010-07-20 14:53:42 -0700343 }
Ben Cheng11d8f142010-03-24 15:24:19 -0700344 HEAP_ACCESS_SHADOW(true);
Bill Buzbee1465db52009-09-23 17:17:35 -0700345 storeBaseDisp(cUnit, rlObj.lowReg, fieldOffset, rlSrc.lowReg, size);
Ben Cheng11d8f142010-03-24 15:24:19 -0700346 HEAP_ACCESS_SHADOW(false);
buzbee919eb062010-07-12 12:59:22 -0700347 if (isObject) {
348 /* NOTE: marking card based on object head */
349 markCard(cUnit, rlSrc.lowReg, rlObj.lowReg);
350 }
Ben Chengba4fc8b2009-06-01 13:00:29 -0700351}
352
353
Ben Chengba4fc8b2009-06-01 13:00:29 -0700354/*
355 * Generate array load
Ben Chengba4fc8b2009-06-01 13:00:29 -0700356 */
Bill Buzbee270c1d62009-08-13 16:58:07 -0700357static void genArrayGet(CompilationUnit *cUnit, MIR *mir, OpSize size,
Bill Buzbee1465db52009-09-23 17:17:35 -0700358 RegLocation rlArray, RegLocation rlIndex,
359 RegLocation rlDest, int scale)
Ben Chengba4fc8b2009-06-01 13:00:29 -0700360{
Bill Buzbee749e8162010-07-07 06:55:56 -0700361 RegisterClass regClass = dvmCompilerRegClassBySize(size);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700362 int lenOffset = offsetof(ArrayObject, length);
363 int dataOffset = offsetof(ArrayObject, contents);
Bill Buzbee1465db52009-09-23 17:17:35 -0700364 RegLocation rlResult;
365 rlArray = loadValue(cUnit, rlArray, kCoreReg);
366 rlIndex = loadValue(cUnit, rlIndex, kCoreReg);
367 int regPtr;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700368
369 /* null object? */
Ben Cheng4238ec22009-08-24 16:32:22 -0700370 ArmLIR * pcrLabel = NULL;
371
372 if (!(mir->OptimizationFlags & MIR_IGNORE_NULL_CHECK)) {
Bill Buzbee1465db52009-09-23 17:17:35 -0700373 pcrLabel = genNullCheck(cUnit, rlArray.sRegLow,
374 rlArray.lowReg, mir->offset, NULL);
Ben Cheng4238ec22009-08-24 16:32:22 -0700375 }
376
Bill Buzbeec6f10662010-02-09 11:16:15 -0800377 regPtr = dvmCompilerAllocTemp(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -0700378
Ben Cheng4238ec22009-08-24 16:32:22 -0700379 if (!(mir->OptimizationFlags & MIR_IGNORE_RANGE_CHECK)) {
Bill Buzbeec6f10662010-02-09 11:16:15 -0800380 int regLen = dvmCompilerAllocTemp(cUnit);
Ben Cheng4238ec22009-08-24 16:32:22 -0700381 /* Get len */
Bill Buzbee1465db52009-09-23 17:17:35 -0700382 loadWordDisp(cUnit, rlArray.lowReg, lenOffset, regLen);
383 /* regPtr -> array data */
384 opRegRegImm(cUnit, kOpAdd, regPtr, rlArray.lowReg, dataOffset);
385 genBoundsCheck(cUnit, rlIndex.lowReg, regLen, mir->offset,
386 pcrLabel);
Bill Buzbeec6f10662010-02-09 11:16:15 -0800387 dvmCompilerFreeTemp(cUnit, regLen);
Ben Cheng4238ec22009-08-24 16:32:22 -0700388 } else {
Bill Buzbee1465db52009-09-23 17:17:35 -0700389 /* regPtr -> array data */
390 opRegRegImm(cUnit, kOpAdd, regPtr, rlArray.lowReg, dataOffset);
Ben Cheng4238ec22009-08-24 16:32:22 -0700391 }
Bill Buzbee1465db52009-09-23 17:17:35 -0700392 if ((size == kLong) || (size == kDouble)) {
393 if (scale) {
Bill Buzbeec6f10662010-02-09 11:16:15 -0800394 int rNewIndex = dvmCompilerAllocTemp(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -0700395 opRegRegImm(cUnit, kOpLsl, rNewIndex, rlIndex.lowReg, scale);
396 opRegReg(cUnit, kOpAdd, regPtr, rNewIndex);
Bill Buzbeec6f10662010-02-09 11:16:15 -0800397 dvmCompilerFreeTemp(cUnit, rNewIndex);
Bill Buzbee1465db52009-09-23 17:17:35 -0700398 } else {
399 opRegReg(cUnit, kOpAdd, regPtr, rlIndex.lowReg);
400 }
Bill Buzbee749e8162010-07-07 06:55:56 -0700401 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, regClass, true);
Ben Cheng11d8f142010-03-24 15:24:19 -0700402
403 HEAP_ACCESS_SHADOW(true);
Bill Buzbee1465db52009-09-23 17:17:35 -0700404 loadPair(cUnit, regPtr, rlResult.lowReg, rlResult.highReg);
Ben Cheng11d8f142010-03-24 15:24:19 -0700405 HEAP_ACCESS_SHADOW(false);
406
Bill Buzbeec6f10662010-02-09 11:16:15 -0800407 dvmCompilerFreeTemp(cUnit, regPtr);
Bill Buzbee1465db52009-09-23 17:17:35 -0700408 storeValueWide(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700409 } else {
Bill Buzbee749e8162010-07-07 06:55:56 -0700410 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, regClass, true);
Ben Cheng11d8f142010-03-24 15:24:19 -0700411
412 HEAP_ACCESS_SHADOW(true);
Bill Buzbee1465db52009-09-23 17:17:35 -0700413 loadBaseIndexed(cUnit, regPtr, rlIndex.lowReg, rlResult.lowReg,
414 scale, size);
Ben Cheng11d8f142010-03-24 15:24:19 -0700415 HEAP_ACCESS_SHADOW(false);
416
Bill Buzbeec6f10662010-02-09 11:16:15 -0800417 dvmCompilerFreeTemp(cUnit, regPtr);
Bill Buzbee1465db52009-09-23 17:17:35 -0700418 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700419 }
420}
421
Ben Chengba4fc8b2009-06-01 13:00:29 -0700422/*
423 * Generate array store
424 *
Ben Chengba4fc8b2009-06-01 13:00:29 -0700425 */
Bill Buzbee270c1d62009-08-13 16:58:07 -0700426static void genArrayPut(CompilationUnit *cUnit, MIR *mir, OpSize size,
Bill Buzbee1465db52009-09-23 17:17:35 -0700427 RegLocation rlArray, RegLocation rlIndex,
428 RegLocation rlSrc, int scale)
Ben Chengba4fc8b2009-06-01 13:00:29 -0700429{
Bill Buzbee749e8162010-07-07 06:55:56 -0700430 RegisterClass regClass = dvmCompilerRegClassBySize(size);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700431 int lenOffset = offsetof(ArrayObject, length);
432 int dataOffset = offsetof(ArrayObject, contents);
433
Bill Buzbee1465db52009-09-23 17:17:35 -0700434 int regPtr;
435 rlArray = loadValue(cUnit, rlArray, kCoreReg);
436 rlIndex = loadValue(cUnit, rlIndex, kCoreReg);
Ben Chenge9695e52009-06-16 16:11:47 -0700437
Bill Buzbeec6f10662010-02-09 11:16:15 -0800438 if (dvmCompilerIsTemp(cUnit, rlArray.lowReg)) {
439 dvmCompilerClobber(cUnit, rlArray.lowReg);
Bill Buzbee1465db52009-09-23 17:17:35 -0700440 regPtr = rlArray.lowReg;
441 } else {
Bill Buzbeec6f10662010-02-09 11:16:15 -0800442 regPtr = dvmCompilerAllocTemp(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -0700443 genRegCopy(cUnit, regPtr, rlArray.lowReg);
444 }
Ben Chenge9695e52009-06-16 16:11:47 -0700445
Ben Cheng1efc9c52009-06-08 18:25:27 -0700446 /* null object? */
Ben Cheng4238ec22009-08-24 16:32:22 -0700447 ArmLIR * pcrLabel = NULL;
448
449 if (!(mir->OptimizationFlags & MIR_IGNORE_NULL_CHECK)) {
Bill Buzbee1465db52009-09-23 17:17:35 -0700450 pcrLabel = genNullCheck(cUnit, rlArray.sRegLow, rlArray.lowReg,
451 mir->offset, NULL);
Ben Cheng4238ec22009-08-24 16:32:22 -0700452 }
453
454 if (!(mir->OptimizationFlags & MIR_IGNORE_RANGE_CHECK)) {
Bill Buzbeec6f10662010-02-09 11:16:15 -0800455 int regLen = dvmCompilerAllocTemp(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -0700456 //NOTE: max live temps(4) here.
Ben Cheng4238ec22009-08-24 16:32:22 -0700457 /* Get len */
Bill Buzbee1465db52009-09-23 17:17:35 -0700458 loadWordDisp(cUnit, rlArray.lowReg, lenOffset, regLen);
459 /* regPtr -> array data */
460 opRegImm(cUnit, kOpAdd, regPtr, dataOffset);
461 genBoundsCheck(cUnit, rlIndex.lowReg, regLen, mir->offset,
462 pcrLabel);
Bill Buzbeec6f10662010-02-09 11:16:15 -0800463 dvmCompilerFreeTemp(cUnit, regLen);
Ben Cheng4238ec22009-08-24 16:32:22 -0700464 } else {
Bill Buzbee1465db52009-09-23 17:17:35 -0700465 /* regPtr -> array data */
466 opRegImm(cUnit, kOpAdd, regPtr, dataOffset);
Ben Cheng4238ec22009-08-24 16:32:22 -0700467 }
Bill Buzbee1465db52009-09-23 17:17:35 -0700468 /* at this point, regPtr points to array, 2 live temps */
Bill Buzbee1465db52009-09-23 17:17:35 -0700469 if ((size == kLong) || (size == kDouble)) {
470 //TODO: need specific wide routine that can handle fp regs
471 if (scale) {
Bill Buzbeec6f10662010-02-09 11:16:15 -0800472 int rNewIndex = dvmCompilerAllocTemp(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -0700473 opRegRegImm(cUnit, kOpLsl, rNewIndex, rlIndex.lowReg, scale);
474 opRegReg(cUnit, kOpAdd, regPtr, rNewIndex);
Bill Buzbeec6f10662010-02-09 11:16:15 -0800475 dvmCompilerFreeTemp(cUnit, rNewIndex);
Bill Buzbee1465db52009-09-23 17:17:35 -0700476 } else {
477 opRegReg(cUnit, kOpAdd, regPtr, rlIndex.lowReg);
478 }
Bill Buzbee749e8162010-07-07 06:55:56 -0700479 rlSrc = loadValueWide(cUnit, rlSrc, regClass);
Ben Cheng11d8f142010-03-24 15:24:19 -0700480
481 HEAP_ACCESS_SHADOW(true);
Bill Buzbee1465db52009-09-23 17:17:35 -0700482 storePair(cUnit, regPtr, rlSrc.lowReg, rlSrc.highReg);
Ben Cheng11d8f142010-03-24 15:24:19 -0700483 HEAP_ACCESS_SHADOW(false);
484
Bill Buzbeec6f10662010-02-09 11:16:15 -0800485 dvmCompilerFreeTemp(cUnit, regPtr);
Bill Buzbee270c1d62009-08-13 16:58:07 -0700486 } else {
Bill Buzbee749e8162010-07-07 06:55:56 -0700487 rlSrc = loadValue(cUnit, rlSrc, regClass);
Ben Cheng11d8f142010-03-24 15:24:19 -0700488
489 HEAP_ACCESS_SHADOW(true);
Bill Buzbee1465db52009-09-23 17:17:35 -0700490 storeBaseIndexed(cUnit, regPtr, rlIndex.lowReg, rlSrc.lowReg,
491 scale, size);
Ben Cheng11d8f142010-03-24 15:24:19 -0700492 HEAP_ACCESS_SHADOW(false);
jeffhao9e45c0b2010-02-03 10:24:05 -0800493 }
Ben Chengba4fc8b2009-06-01 13:00:29 -0700494}
495
Bill Buzbeebe6534f2010-03-12 16:01:35 -0800496/*
497 * Generate array object store
498 * Must use explicit register allocation here because of
499 * call-out to dvmCanPutArrayElement
500 */
501static void genArrayObjectPut(CompilationUnit *cUnit, MIR *mir,
502 RegLocation rlArray, RegLocation rlIndex,
503 RegLocation rlSrc, int scale)
504{
505 int lenOffset = offsetof(ArrayObject, length);
506 int dataOffset = offsetof(ArrayObject, contents);
507
508 dvmCompilerFlushAllRegs(cUnit);
509
510 int regLen = r0;
511 int regPtr = r4PC; /* Preserved across call */
512 int regArray = r1;
513 int regIndex = r7; /* Preserved across call */
514
515 loadValueDirectFixed(cUnit, rlArray, regArray);
516 loadValueDirectFixed(cUnit, rlIndex, regIndex);
517
518 /* null object? */
519 ArmLIR * pcrLabel = NULL;
520
521 if (!(mir->OptimizationFlags & MIR_IGNORE_NULL_CHECK)) {
522 pcrLabel = genNullCheck(cUnit, rlArray.sRegLow, regArray,
523 mir->offset, NULL);
524 }
525
526 if (!(mir->OptimizationFlags & MIR_IGNORE_RANGE_CHECK)) {
527 /* Get len */
528 loadWordDisp(cUnit, regArray, lenOffset, regLen);
529 /* regPtr -> array data */
530 opRegRegImm(cUnit, kOpAdd, regPtr, regArray, dataOffset);
531 genBoundsCheck(cUnit, regIndex, regLen, mir->offset,
532 pcrLabel);
533 } else {
534 /* regPtr -> array data */
535 opRegRegImm(cUnit, kOpAdd, regPtr, regArray, dataOffset);
536 }
537
538 /* Get object to store */
539 loadValueDirectFixed(cUnit, rlSrc, r0);
Ben Chengbd1326d2010-04-02 15:04:53 -0700540 LOAD_FUNC_ADDR(cUnit, r2, (int)dvmCanPutArrayElement);
Bill Buzbeebe6534f2010-03-12 16:01:35 -0800541
542 /* Are we storing null? If so, avoid check */
buzbee8f8109a2010-08-31 10:16:35 -0700543 ArmLIR *branchOver = genCmpImmBranch(cUnit, kArmCondEq, r0, 0);
Bill Buzbeebe6534f2010-03-12 16:01:35 -0800544
545 /* Make sure the types are compatible */
546 loadWordDisp(cUnit, regArray, offsetof(Object, clazz), r1);
547 loadWordDisp(cUnit, r0, offsetof(Object, clazz), r0);
548 opReg(cUnit, kOpBlx, r2);
549 dvmCompilerClobberCallRegs(cUnit);
Bill Buzbee900a3af2010-03-16 12:41:43 -0700550
551 /*
552 * Using fixed registers here, and counting on r4 and r7 being
553 * preserved across the above call. Tell the register allocation
554 * utilities about the regs we are using directly
555 */
556 dvmCompilerLockTemp(cUnit, regPtr); // r4PC
557 dvmCompilerLockTemp(cUnit, regIndex); // r7
558 dvmCompilerLockTemp(cUnit, r0);
buzbee919eb062010-07-12 12:59:22 -0700559 dvmCompilerLockTemp(cUnit, r1);
Bill Buzbee900a3af2010-03-16 12:41:43 -0700560
Bill Buzbeebe6534f2010-03-12 16:01:35 -0800561 /* Bad? - roll back and re-execute if so */
562 genRegImmCheck(cUnit, kArmCondEq, r0, 0, mir->offset, pcrLabel);
563
buzbee919eb062010-07-12 12:59:22 -0700564 /* Resume here - must reload element & array, regPtr & index preserved */
Bill Buzbeebe6534f2010-03-12 16:01:35 -0800565 loadValueDirectFixed(cUnit, rlSrc, r0);
buzbee919eb062010-07-12 12:59:22 -0700566 loadValueDirectFixed(cUnit, rlArray, r1);
Bill Buzbeebe6534f2010-03-12 16:01:35 -0800567
568 ArmLIR *target = newLIR0(cUnit, kArmPseudoTargetLabel);
569 target->defMask = ENCODE_ALL;
570 branchOver->generic.target = (LIR *) target;
571
Ben Cheng11d8f142010-03-24 15:24:19 -0700572 HEAP_ACCESS_SHADOW(true);
Bill Buzbeebe6534f2010-03-12 16:01:35 -0800573 storeBaseIndexed(cUnit, regPtr, regIndex, r0,
574 scale, kWord);
Ben Cheng11d8f142010-03-24 15:24:19 -0700575 HEAP_ACCESS_SHADOW(false);
buzbee919eb062010-07-12 12:59:22 -0700576
buzbeebaf196a2010-08-04 10:13:15 -0700577 dvmCompilerFreeTemp(cUnit, regPtr);
578 dvmCompilerFreeTemp(cUnit, regIndex);
579
buzbee919eb062010-07-12 12:59:22 -0700580 /* NOTE: marking card here based on object head */
581 markCard(cUnit, r0, r1);
Bill Buzbeebe6534f2010-03-12 16:01:35 -0800582}
583
Ben Cheng5d90c202009-11-22 23:31:11 -0800584static bool genShiftOpLong(CompilationUnit *cUnit, MIR *mir,
585 RegLocation rlDest, RegLocation rlSrc1,
586 RegLocation rlShift)
Ben Chengba4fc8b2009-06-01 13:00:29 -0700587{
Ben Chenge9695e52009-06-16 16:11:47 -0700588 /*
589 * Don't mess with the regsiters here as there is a particular calling
590 * convention to the out-of-line handler.
591 */
Bill Buzbee1465db52009-09-23 17:17:35 -0700592 RegLocation rlResult;
593
594 loadValueDirectWideFixed(cUnit, rlSrc1, r0, r1);
595 loadValueDirect(cUnit, rlShift, r2);
Dan Bornstein9a1f8162010-12-01 17:02:26 -0800596 switch( mir->dalvikInsn.opcode) {
Ben Chenge9695e52009-06-16 16:11:47 -0700597 case OP_SHL_LONG:
598 case OP_SHL_LONG_2ADDR:
599 genDispatchToHandler(cUnit, TEMPLATE_SHL_LONG);
600 break;
601 case OP_SHR_LONG:
602 case OP_SHR_LONG_2ADDR:
603 genDispatchToHandler(cUnit, TEMPLATE_SHR_LONG);
604 break;
605 case OP_USHR_LONG:
606 case OP_USHR_LONG_2ADDR:
607 genDispatchToHandler(cUnit, TEMPLATE_USHR_LONG);
608 break;
609 default:
610 return true;
611 }
Bill Buzbeec6f10662010-02-09 11:16:15 -0800612 rlResult = dvmCompilerGetReturnWide(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -0700613 storeValueWide(cUnit, rlDest, rlResult);
Ben Chenge9695e52009-06-16 16:11:47 -0700614 return false;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700615}
Ben Chenge9695e52009-06-16 16:11:47 -0700616
Ben Cheng5d90c202009-11-22 23:31:11 -0800617static bool genArithOpLong(CompilationUnit *cUnit, MIR *mir,
618 RegLocation rlDest, RegLocation rlSrc1,
619 RegLocation rlSrc2)
Ben Chengba4fc8b2009-06-01 13:00:29 -0700620{
Bill Buzbee1465db52009-09-23 17:17:35 -0700621 RegLocation rlResult;
622 OpKind firstOp = kOpBkpt;
623 OpKind secondOp = kOpBkpt;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700624 bool callOut = false;
625 void *callTgt;
626 int retReg = r0;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700627
Dan Bornstein9a1f8162010-12-01 17:02:26 -0800628 switch (mir->dalvikInsn.opcode) {
Ben Chengba4fc8b2009-06-01 13:00:29 -0700629 case OP_NOT_LONG:
Bill Buzbee1465db52009-09-23 17:17:35 -0700630 rlSrc2 = loadValueWide(cUnit, rlSrc2, kCoreReg);
Bill Buzbeec6f10662010-02-09 11:16:15 -0800631 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Bill Buzbee1465db52009-09-23 17:17:35 -0700632 opRegReg(cUnit, kOpMvn, rlResult.lowReg, rlSrc2.lowReg);
633 opRegReg(cUnit, kOpMvn, rlResult.highReg, rlSrc2.highReg);
634 storeValueWide(cUnit, rlDest, rlResult);
635 return false;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700636 break;
637 case OP_ADD_LONG:
638 case OP_ADD_LONG_2ADDR:
Bill Buzbee1465db52009-09-23 17:17:35 -0700639 firstOp = kOpAdd;
640 secondOp = kOpAdc;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700641 break;
642 case OP_SUB_LONG:
643 case OP_SUB_LONG_2ADDR:
Bill Buzbee1465db52009-09-23 17:17:35 -0700644 firstOp = kOpSub;
645 secondOp = kOpSbc;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700646 break;
647 case OP_MUL_LONG:
648 case OP_MUL_LONG_2ADDR:
Bill Buzbee1465db52009-09-23 17:17:35 -0700649 genMulLong(cUnit, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700650 return false;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700651 case OP_DIV_LONG:
652 case OP_DIV_LONG_2ADDR:
653 callOut = true;
654 retReg = r0;
655 callTgt = (void*)__aeabi_ldivmod;
656 break;
657 /* NOTE - result is in r2/r3 instead of r0/r1 */
658 case OP_REM_LONG:
659 case OP_REM_LONG_2ADDR:
660 callOut = true;
661 callTgt = (void*)__aeabi_ldivmod;
662 retReg = r2;
663 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700664 case OP_AND_LONG_2ADDR:
Bill Buzbee1465db52009-09-23 17:17:35 -0700665 case OP_AND_LONG:
666 firstOp = kOpAnd;
667 secondOp = kOpAnd;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700668 break;
669 case OP_OR_LONG:
670 case OP_OR_LONG_2ADDR:
Bill Buzbee1465db52009-09-23 17:17:35 -0700671 firstOp = kOpOr;
672 secondOp = kOpOr;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700673 break;
674 case OP_XOR_LONG:
675 case OP_XOR_LONG_2ADDR:
Bill Buzbee1465db52009-09-23 17:17:35 -0700676 firstOp = kOpXor;
677 secondOp = kOpXor;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700678 break;
Ben Chenge9695e52009-06-16 16:11:47 -0700679 case OP_NEG_LONG: {
Bill Buzbee51ecf602010-01-14 14:27:52 -0800680 //TUNING: can improve this using Thumb2 code
Bill Buzbeec6f10662010-02-09 11:16:15 -0800681 int tReg = dvmCompilerAllocTemp(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -0700682 rlSrc2 = loadValueWide(cUnit, rlSrc2, kCoreReg);
Bill Buzbeec6f10662010-02-09 11:16:15 -0800683 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Ben Chengbd1326d2010-04-02 15:04:53 -0700684 loadConstantNoClobber(cUnit, tReg, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -0700685 opRegRegReg(cUnit, kOpSub, rlResult.lowReg,
Bill Buzbee51ecf602010-01-14 14:27:52 -0800686 tReg, rlSrc2.lowReg);
687 opRegReg(cUnit, kOpSbc, tReg, rlSrc2.highReg);
688 genRegCopy(cUnit, rlResult.highReg, tReg);
Bill Buzbee1465db52009-09-23 17:17:35 -0700689 storeValueWide(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700690 return false;
Ben Chenge9695e52009-06-16 16:11:47 -0700691 }
Ben Chengba4fc8b2009-06-01 13:00:29 -0700692 default:
693 LOGE("Invalid long arith op");
Bill Buzbeefc519dc2010-03-06 23:30:57 -0800694 dvmCompilerAbort(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700695 }
696 if (!callOut) {
Bill Buzbee80cef862010-03-25 10:38:34 -0700697 genLong3Addr(cUnit, mir, firstOp, secondOp, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700698 } else {
Bill Buzbee1465db52009-09-23 17:17:35 -0700699 // Adjust return regs in to handle case of rem returning r2/r3
Bill Buzbeec6f10662010-02-09 11:16:15 -0800700 dvmCompilerFlushAllRegs(cUnit); /* Send everything to home location */
Bill Buzbee1465db52009-09-23 17:17:35 -0700701 loadValueDirectWideFixed(cUnit, rlSrc1, r0, r1);
Ben Chengbd1326d2010-04-02 15:04:53 -0700702 LOAD_FUNC_ADDR(cUnit, rlr, (int) callTgt);
Bill Buzbee1465db52009-09-23 17:17:35 -0700703 loadValueDirectWideFixed(cUnit, rlSrc2, r2, r3);
704 opReg(cUnit, kOpBlx, rlr);
Elliott Hughes6a555132010-02-25 15:41:42 -0800705 dvmCompilerClobberCallRegs(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -0700706 if (retReg == r0)
Bill Buzbeec6f10662010-02-09 11:16:15 -0800707 rlResult = dvmCompilerGetReturnWide(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -0700708 else
Bill Buzbeec6f10662010-02-09 11:16:15 -0800709 rlResult = dvmCompilerGetReturnWideAlt(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -0700710 storeValueWide(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700711 }
712 return false;
713}
714
Ben Cheng5d90c202009-11-22 23:31:11 -0800715static bool genArithOpInt(CompilationUnit *cUnit, MIR *mir,
716 RegLocation rlDest, RegLocation rlSrc1,
717 RegLocation rlSrc2)
Ben Chengba4fc8b2009-06-01 13:00:29 -0700718{
Bill Buzbee1465db52009-09-23 17:17:35 -0700719 OpKind op = kOpBkpt;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700720 bool callOut = false;
721 bool checkZero = false;
Bill Buzbee1465db52009-09-23 17:17:35 -0700722 bool unary = false;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700723 int retReg = r0;
724 void *callTgt;
Bill Buzbee1465db52009-09-23 17:17:35 -0700725 RegLocation rlResult;
Bill Buzbee0e605272009-12-01 14:28:05 -0800726 bool shiftOp = false;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700727
Dan Bornstein9a1f8162010-12-01 17:02:26 -0800728 switch (mir->dalvikInsn.opcode) {
Ben Chengba4fc8b2009-06-01 13:00:29 -0700729 case OP_NEG_INT:
Bill Buzbee1465db52009-09-23 17:17:35 -0700730 op = kOpNeg;
731 unary = true;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700732 break;
733 case OP_NOT_INT:
Bill Buzbee1465db52009-09-23 17:17:35 -0700734 op = kOpMvn;
735 unary = true;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700736 break;
737 case OP_ADD_INT:
738 case OP_ADD_INT_2ADDR:
Bill Buzbee1465db52009-09-23 17:17:35 -0700739 op = kOpAdd;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700740 break;
741 case OP_SUB_INT:
742 case OP_SUB_INT_2ADDR:
Bill Buzbee1465db52009-09-23 17:17:35 -0700743 op = kOpSub;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700744 break;
745 case OP_MUL_INT:
746 case OP_MUL_INT_2ADDR:
Bill Buzbee1465db52009-09-23 17:17:35 -0700747 op = kOpMul;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700748 break;
749 case OP_DIV_INT:
750 case OP_DIV_INT_2ADDR:
751 callOut = true;
752 checkZero = true;
753 callTgt = __aeabi_idiv;
754 retReg = r0;
755 break;
756 /* NOTE: returns in r1 */
757 case OP_REM_INT:
758 case OP_REM_INT_2ADDR:
759 callOut = true;
760 checkZero = true;
761 callTgt = __aeabi_idivmod;
762 retReg = r1;
763 break;
764 case OP_AND_INT:
765 case OP_AND_INT_2ADDR:
Bill Buzbee1465db52009-09-23 17:17:35 -0700766 op = kOpAnd;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700767 break;
768 case OP_OR_INT:
769 case OP_OR_INT_2ADDR:
Bill Buzbee1465db52009-09-23 17:17:35 -0700770 op = kOpOr;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700771 break;
772 case OP_XOR_INT:
773 case OP_XOR_INT_2ADDR:
Bill Buzbee1465db52009-09-23 17:17:35 -0700774 op = kOpXor;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700775 break;
776 case OP_SHL_INT:
777 case OP_SHL_INT_2ADDR:
Bill Buzbee0e605272009-12-01 14:28:05 -0800778 shiftOp = true;
Bill Buzbee1465db52009-09-23 17:17:35 -0700779 op = kOpLsl;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700780 break;
781 case OP_SHR_INT:
782 case OP_SHR_INT_2ADDR:
Bill Buzbee0e605272009-12-01 14:28:05 -0800783 shiftOp = true;
Bill Buzbee1465db52009-09-23 17:17:35 -0700784 op = kOpAsr;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700785 break;
786 case OP_USHR_INT:
787 case OP_USHR_INT_2ADDR:
Bill Buzbee0e605272009-12-01 14:28:05 -0800788 shiftOp = true;
Bill Buzbee1465db52009-09-23 17:17:35 -0700789 op = kOpLsr;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700790 break;
791 default:
792 LOGE("Invalid word arith op: 0x%x(%d)",
Dan Bornstein9a1f8162010-12-01 17:02:26 -0800793 mir->dalvikInsn.opcode, mir->dalvikInsn.opcode);
Bill Buzbeefc519dc2010-03-06 23:30:57 -0800794 dvmCompilerAbort(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700795 }
796 if (!callOut) {
Bill Buzbee1465db52009-09-23 17:17:35 -0700797 rlSrc1 = loadValue(cUnit, rlSrc1, kCoreReg);
798 if (unary) {
Bill Buzbeec6f10662010-02-09 11:16:15 -0800799 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Bill Buzbee1465db52009-09-23 17:17:35 -0700800 opRegReg(cUnit, op, rlResult.lowReg,
801 rlSrc1.lowReg);
Ben Chenge9695e52009-06-16 16:11:47 -0700802 } else {
Bill Buzbee1465db52009-09-23 17:17:35 -0700803 rlSrc2 = loadValue(cUnit, rlSrc2, kCoreReg);
Bill Buzbee0e605272009-12-01 14:28:05 -0800804 if (shiftOp) {
Bill Buzbeec6f10662010-02-09 11:16:15 -0800805 int tReg = dvmCompilerAllocTemp(cUnit);
Bill Buzbee0e605272009-12-01 14:28:05 -0800806 opRegRegImm(cUnit, kOpAnd, tReg, rlSrc2.lowReg, 31);
Bill Buzbeec6f10662010-02-09 11:16:15 -0800807 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Bill Buzbee0e605272009-12-01 14:28:05 -0800808 opRegRegReg(cUnit, op, rlResult.lowReg,
809 rlSrc1.lowReg, tReg);
Bill Buzbeec6f10662010-02-09 11:16:15 -0800810 dvmCompilerFreeTemp(cUnit, tReg);
Bill Buzbee0e605272009-12-01 14:28:05 -0800811 } else {
Bill Buzbeec6f10662010-02-09 11:16:15 -0800812 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Bill Buzbee0e605272009-12-01 14:28:05 -0800813 opRegRegReg(cUnit, op, rlResult.lowReg,
814 rlSrc1.lowReg, rlSrc2.lowReg);
815 }
Ben Chenge9695e52009-06-16 16:11:47 -0700816 }
Bill Buzbee1465db52009-09-23 17:17:35 -0700817 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700818 } else {
Bill Buzbee1465db52009-09-23 17:17:35 -0700819 RegLocation rlResult;
Bill Buzbeec6f10662010-02-09 11:16:15 -0800820 dvmCompilerFlushAllRegs(cUnit); /* Send everything to home location */
Bill Buzbee1465db52009-09-23 17:17:35 -0700821 loadValueDirectFixed(cUnit, rlSrc2, r1);
Ben Chengbd1326d2010-04-02 15:04:53 -0700822 LOAD_FUNC_ADDR(cUnit, r2, (int) callTgt);
Bill Buzbee1465db52009-09-23 17:17:35 -0700823 loadValueDirectFixed(cUnit, rlSrc1, r0);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700824 if (checkZero) {
Bill Buzbee1465db52009-09-23 17:17:35 -0700825 genNullCheck(cUnit, rlSrc2.sRegLow, r1, mir->offset, NULL);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700826 }
Bill Buzbee1465db52009-09-23 17:17:35 -0700827 opReg(cUnit, kOpBlx, r2);
Elliott Hughes6a555132010-02-25 15:41:42 -0800828 dvmCompilerClobberCallRegs(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -0700829 if (retReg == r0)
Bill Buzbeec6f10662010-02-09 11:16:15 -0800830 rlResult = dvmCompilerGetReturn(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -0700831 else
Bill Buzbeec6f10662010-02-09 11:16:15 -0800832 rlResult = dvmCompilerGetReturnAlt(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -0700833 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700834 }
835 return false;
836}
837
Ben Cheng5d90c202009-11-22 23:31:11 -0800838static bool genArithOp(CompilationUnit *cUnit, MIR *mir)
Ben Chengba4fc8b2009-06-01 13:00:29 -0700839{
Dan Bornstein9a1f8162010-12-01 17:02:26 -0800840 Opcode opcode = mir->dalvikInsn.opcode;
Bill Buzbee1465db52009-09-23 17:17:35 -0700841 RegLocation rlDest;
842 RegLocation rlSrc1;
843 RegLocation rlSrc2;
844 /* Deduce sizes of operands */
845 if (mir->ssaRep->numUses == 2) {
Bill Buzbeec6f10662010-02-09 11:16:15 -0800846 rlSrc1 = dvmCompilerGetSrc(cUnit, mir, 0);
847 rlSrc2 = dvmCompilerGetSrc(cUnit, mir, 1);
Bill Buzbee1465db52009-09-23 17:17:35 -0700848 } else if (mir->ssaRep->numUses == 3) {
Bill Buzbeec6f10662010-02-09 11:16:15 -0800849 rlSrc1 = dvmCompilerGetSrcWide(cUnit, mir, 0, 1);
850 rlSrc2 = dvmCompilerGetSrc(cUnit, mir, 2);
Bill Buzbee1465db52009-09-23 17:17:35 -0700851 } else {
Bill Buzbeec6f10662010-02-09 11:16:15 -0800852 rlSrc1 = dvmCompilerGetSrcWide(cUnit, mir, 0, 1);
853 rlSrc2 = dvmCompilerGetSrcWide(cUnit, mir, 2, 3);
Bill Buzbee1465db52009-09-23 17:17:35 -0700854 assert(mir->ssaRep->numUses == 4);
855 }
856 if (mir->ssaRep->numDefs == 1) {
Bill Buzbeec6f10662010-02-09 11:16:15 -0800857 rlDest = dvmCompilerGetDest(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -0700858 } else {
859 assert(mir->ssaRep->numDefs == 2);
Bill Buzbeec6f10662010-02-09 11:16:15 -0800860 rlDest = dvmCompilerGetDestWide(cUnit, mir, 0, 1);
Bill Buzbee1465db52009-09-23 17:17:35 -0700861 }
Ben Chengba4fc8b2009-06-01 13:00:29 -0700862
Dan Bornstein9a1f8162010-12-01 17:02:26 -0800863 if ((opcode >= OP_ADD_LONG_2ADDR) && (opcode <= OP_XOR_LONG_2ADDR)) {
Ben Cheng5d90c202009-11-22 23:31:11 -0800864 return genArithOpLong(cUnit,mir, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700865 }
Dan Bornstein9a1f8162010-12-01 17:02:26 -0800866 if ((opcode >= OP_ADD_LONG) && (opcode <= OP_XOR_LONG)) {
Ben Cheng5d90c202009-11-22 23:31:11 -0800867 return genArithOpLong(cUnit,mir, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700868 }
Dan Bornstein9a1f8162010-12-01 17:02:26 -0800869 if ((opcode >= OP_SHL_LONG_2ADDR) && (opcode <= OP_USHR_LONG_2ADDR)) {
Ben Cheng5d90c202009-11-22 23:31:11 -0800870 return genShiftOpLong(cUnit,mir, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700871 }
Dan Bornstein9a1f8162010-12-01 17:02:26 -0800872 if ((opcode >= OP_SHL_LONG) && (opcode <= OP_USHR_LONG)) {
Ben Cheng5d90c202009-11-22 23:31:11 -0800873 return genShiftOpLong(cUnit,mir, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700874 }
Dan Bornstein9a1f8162010-12-01 17:02:26 -0800875 if ((opcode >= OP_ADD_INT_2ADDR) && (opcode <= OP_USHR_INT_2ADDR)) {
Ben Cheng5d90c202009-11-22 23:31:11 -0800876 return genArithOpInt(cUnit,mir, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700877 }
Dan Bornstein9a1f8162010-12-01 17:02:26 -0800878 if ((opcode >= OP_ADD_INT) && (opcode <= OP_USHR_INT)) {
Ben Cheng5d90c202009-11-22 23:31:11 -0800879 return genArithOpInt(cUnit,mir, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700880 }
Dan Bornstein9a1f8162010-12-01 17:02:26 -0800881 if ((opcode >= OP_ADD_FLOAT_2ADDR) && (opcode <= OP_REM_FLOAT_2ADDR)) {
Ben Cheng5d90c202009-11-22 23:31:11 -0800882 return genArithOpFloat(cUnit,mir, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700883 }
Dan Bornstein9a1f8162010-12-01 17:02:26 -0800884 if ((opcode >= OP_ADD_FLOAT) && (opcode <= OP_REM_FLOAT)) {
Ben Cheng5d90c202009-11-22 23:31:11 -0800885 return genArithOpFloat(cUnit, mir, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700886 }
Dan Bornstein9a1f8162010-12-01 17:02:26 -0800887 if ((opcode >= OP_ADD_DOUBLE_2ADDR) && (opcode <= OP_REM_DOUBLE_2ADDR)) {
Ben Cheng5d90c202009-11-22 23:31:11 -0800888 return genArithOpDouble(cUnit,mir, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700889 }
Dan Bornstein9a1f8162010-12-01 17:02:26 -0800890 if ((opcode >= OP_ADD_DOUBLE) && (opcode <= OP_REM_DOUBLE)) {
Ben Cheng5d90c202009-11-22 23:31:11 -0800891 return genArithOpDouble(cUnit,mir, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700892 }
893 return true;
894}
895
Bill Buzbee1465db52009-09-23 17:17:35 -0700896/* Generate unconditional branch instructions */
897static ArmLIR *genUnconditionalBranch(CompilationUnit *cUnit, ArmLIR *target)
898{
899 ArmLIR *branch = opNone(cUnit, kOpUncondBr);
900 branch->generic.target = (LIR *) target;
901 return branch;
902}
903
Bill Buzbee1465db52009-09-23 17:17:35 -0700904/* Perform the actual operation for OP_RETURN_* */
905static void genReturnCommon(CompilationUnit *cUnit, MIR *mir)
906{
Ben Chengcfdeca32011-01-14 11:36:46 -0800907 if (!cUnit->methodJitMode) {
908 genDispatchToHandler(cUnit, gDvmJit.methodTraceSupport ?
909 TEMPLATE_RETURN_PROF :
910 TEMPLATE_RETURN);
Ben Cheng978738d2010-05-13 13:45:57 -0700911#if defined(WITH_JIT_TUNING)
Ben Chengcfdeca32011-01-14 11:36:46 -0800912 gDvmJit.returnOp++;
Bill Buzbee1465db52009-09-23 17:17:35 -0700913#endif
Ben Chengcfdeca32011-01-14 11:36:46 -0800914 int dPC = (int) (cUnit->method->insns + mir->offset);
915 /* Insert branch, but defer setting of target */
916 ArmLIR *branch = genUnconditionalBranch(cUnit, NULL);
917 /* Set up the place holder to reconstruct this Dalvik PC */
918 ArmLIR *pcrLabel = (ArmLIR *) dvmCompilerNew(sizeof(ArmLIR), true);
919 pcrLabel->opcode = kArmPseudoPCReconstructionCell;
920 pcrLabel->operands[0] = dPC;
921 pcrLabel->operands[1] = mir->offset;
922 /* Insert the place holder to the growable list */
923 dvmInsertGrowableList(&cUnit->pcReconstructionList,
924 (intptr_t) pcrLabel);
925 /* Branch to the PC reconstruction code */
926 branch->generic.target = (LIR *) pcrLabel;
927 }
928 /* TODO: Move result to InterpState for non-void returns */
Bill Buzbee1465db52009-09-23 17:17:35 -0700929}
930
Ben Chengba4fc8b2009-06-01 13:00:29 -0700931static void genProcessArgsNoRange(CompilationUnit *cUnit, MIR *mir,
932 DecodedInstruction *dInsn,
Bill Buzbee89efc3d2009-07-28 11:22:22 -0700933 ArmLIR **pcrLabel)
Ben Chengba4fc8b2009-06-01 13:00:29 -0700934{
935 unsigned int i;
936 unsigned int regMask = 0;
Bill Buzbee1465db52009-09-23 17:17:35 -0700937 RegLocation rlArg;
938 int numDone = 0;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700939
Bill Buzbee1465db52009-09-23 17:17:35 -0700940 /*
941 * Load arguments to r0..r4. Note that these registers may contain
942 * live values, so we clobber them immediately after loading to prevent
943 * them from being used as sources for subsequent loads.
944 */
Bill Buzbeec6f10662010-02-09 11:16:15 -0800945 dvmCompilerLockAllTemps(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700946 for (i = 0; i < dInsn->vA; i++) {
947 regMask |= 1 << i;
Bill Buzbeec6f10662010-02-09 11:16:15 -0800948 rlArg = dvmCompilerGetSrc(cUnit, mir, numDone++);
Bill Buzbee1465db52009-09-23 17:17:35 -0700949 loadValueDirectFixed(cUnit, rlArg, i);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700950 }
951 if (regMask) {
952 /* Up to 5 args are pushed on top of FP - sizeofStackSaveArea */
Bill Buzbee1465db52009-09-23 17:17:35 -0700953 opRegRegImm(cUnit, kOpSub, r7, rFP,
954 sizeof(StackSaveArea) + (dInsn->vA << 2));
Ben Chengba4fc8b2009-06-01 13:00:29 -0700955 /* generate null check */
956 if (pcrLabel) {
Bill Buzbeec6f10662010-02-09 11:16:15 -0800957 *pcrLabel = genNullCheck(cUnit, dvmCompilerSSASrc(mir, 0), r0,
Bill Buzbee1465db52009-09-23 17:17:35 -0700958 mir->offset, NULL);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700959 }
Bill Buzbee270c1d62009-08-13 16:58:07 -0700960 storeMultiple(cUnit, r7, regMask);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700961 }
962}
963
964static void genProcessArgsRange(CompilationUnit *cUnit, MIR *mir,
965 DecodedInstruction *dInsn,
Bill Buzbee89efc3d2009-07-28 11:22:22 -0700966 ArmLIR **pcrLabel)
Ben Chengba4fc8b2009-06-01 13:00:29 -0700967{
968 int srcOffset = dInsn->vC << 2;
969 int numArgs = dInsn->vA;
970 int regMask;
Bill Buzbee1465db52009-09-23 17:17:35 -0700971
972 /*
973 * Note: here, all promoted registers will have been flushed
974 * back to the Dalvik base locations, so register usage restrictins
975 * are lifted. All parms loaded from original Dalvik register
976 * region - even though some might conceivably have valid copies
977 * cached in a preserved register.
978 */
Bill Buzbeec6f10662010-02-09 11:16:15 -0800979 dvmCompilerLockAllTemps(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -0700980
Ben Chengba4fc8b2009-06-01 13:00:29 -0700981 /*
982 * r4PC : &rFP[vC]
983 * r7: &newFP[0]
984 */
Bill Buzbee1465db52009-09-23 17:17:35 -0700985 opRegRegImm(cUnit, kOpAdd, r4PC, rFP, srcOffset);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700986 /* load [r0 .. min(numArgs,4)] */
987 regMask = (1 << ((numArgs < 4) ? numArgs : 4)) - 1;
Ben Chengd7d426a2009-09-22 11:23:36 -0700988 /*
989 * Protect the loadMultiple instruction from being reordered with other
990 * Dalvik stack accesses.
jeffhao71eee1f2011-01-04 14:18:54 -0800991 *
992 * This code is also shared by the invoke jumbo instructions, and this
993 * does not need to be done if the invoke jumbo has no arguments.
Ben Chengd7d426a2009-09-22 11:23:36 -0700994 */
jeffhao71eee1f2011-01-04 14:18:54 -0800995 if (numArgs != 0) loadMultiple(cUnit, r4PC, regMask);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700996
Bill Buzbee1465db52009-09-23 17:17:35 -0700997 opRegRegImm(cUnit, kOpSub, r7, rFP,
998 sizeof(StackSaveArea) + (numArgs << 2));
Ben Chengba4fc8b2009-06-01 13:00:29 -0700999 /* generate null check */
1000 if (pcrLabel) {
Bill Buzbeec6f10662010-02-09 11:16:15 -08001001 *pcrLabel = genNullCheck(cUnit, dvmCompilerSSASrc(mir, 0), r0,
Bill Buzbee1465db52009-09-23 17:17:35 -07001002 mir->offset, NULL);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001003 }
1004
1005 /*
1006 * Handle remaining 4n arguments:
1007 * store previously loaded 4 values and load the next 4 values
1008 */
1009 if (numArgs >= 8) {
Bill Buzbee89efc3d2009-07-28 11:22:22 -07001010 ArmLIR *loopLabel = NULL;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001011 /*
1012 * r0 contains "this" and it will be used later, so push it to the stack
Bill Buzbee270c1d62009-08-13 16:58:07 -07001013 * first. Pushing r5 (rFP) is just for stack alignment purposes.
Ben Chengba4fc8b2009-06-01 13:00:29 -07001014 */
Bill Buzbee1465db52009-09-23 17:17:35 -07001015 opImm(cUnit, kOpPush, (1 << r0 | 1 << rFP));
Ben Chengba4fc8b2009-06-01 13:00:29 -07001016 /* No need to generate the loop structure if numArgs <= 11 */
1017 if (numArgs > 11) {
1018 loadConstant(cUnit, 5, ((numArgs - 4) >> 2) << 2);
Bill Buzbee1465db52009-09-23 17:17:35 -07001019 loopLabel = newLIR0(cUnit, kArmPseudoTargetLabel);
Ben Chengd7d426a2009-09-22 11:23:36 -07001020 loopLabel->defMask = ENCODE_ALL;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001021 }
Bill Buzbee270c1d62009-08-13 16:58:07 -07001022 storeMultiple(cUnit, r7, regMask);
Ben Chengd7d426a2009-09-22 11:23:36 -07001023 /*
1024 * Protect the loadMultiple instruction from being reordered with other
1025 * Dalvik stack accesses.
1026 */
Bill Buzbee270c1d62009-08-13 16:58:07 -07001027 loadMultiple(cUnit, r4PC, regMask);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001028 /* No need to generate the loop structure if numArgs <= 11 */
1029 if (numArgs > 11) {
Bill Buzbee1465db52009-09-23 17:17:35 -07001030 opRegImm(cUnit, kOpSub, rFP, 4);
1031 genConditionalBranch(cUnit, kArmCondNe, loopLabel);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001032 }
1033 }
1034
1035 /* Save the last batch of loaded values */
jeffhao71eee1f2011-01-04 14:18:54 -08001036 if (numArgs != 0) storeMultiple(cUnit, r7, regMask);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001037
1038 /* Generate the loop epilogue - don't use r0 */
1039 if ((numArgs > 4) && (numArgs % 4)) {
1040 regMask = ((1 << (numArgs & 0x3)) - 1) << 1;
Ben Chengd7d426a2009-09-22 11:23:36 -07001041 /*
1042 * Protect the loadMultiple instruction from being reordered with other
1043 * Dalvik stack accesses.
1044 */
Bill Buzbee270c1d62009-08-13 16:58:07 -07001045 loadMultiple(cUnit, r4PC, regMask);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001046 }
1047 if (numArgs >= 8)
Bill Buzbee1465db52009-09-23 17:17:35 -07001048 opImm(cUnit, kOpPop, (1 << r0 | 1 << rFP));
Ben Chengba4fc8b2009-06-01 13:00:29 -07001049
1050 /* Save the modulo 4 arguments */
1051 if ((numArgs > 4) && (numArgs % 4)) {
Bill Buzbee270c1d62009-08-13 16:58:07 -07001052 storeMultiple(cUnit, r7, regMask);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001053 }
1054}
1055
Ben Cheng38329f52009-07-07 14:19:20 -07001056/*
1057 * Generate code to setup the call stack then jump to the chaining cell if it
1058 * is not a native method.
1059 */
1060static void genInvokeSingletonCommon(CompilationUnit *cUnit, MIR *mir,
Bill Buzbee89efc3d2009-07-28 11:22:22 -07001061 BasicBlock *bb, ArmLIR *labelList,
1062 ArmLIR *pcrLabel,
Ben Cheng38329f52009-07-07 14:19:20 -07001063 const Method *calleeMethod)
Ben Chengba4fc8b2009-06-01 13:00:29 -07001064{
Bill Buzbee1465db52009-09-23 17:17:35 -07001065 /*
1066 * Note: all Dalvik register state should be flushed to
1067 * memory by the point, so register usage restrictions no
1068 * longer apply. All temp & preserved registers may be used.
1069 */
Bill Buzbeec6f10662010-02-09 11:16:15 -08001070 dvmCompilerLockAllTemps(cUnit);
Bill Buzbee89efc3d2009-07-28 11:22:22 -07001071 ArmLIR *retChainingCell = &labelList[bb->fallThrough->id];
Ben Chengba4fc8b2009-06-01 13:00:29 -07001072
1073 /* r1 = &retChainingCell */
Bill Buzbee1465db52009-09-23 17:17:35 -07001074 ArmLIR *addrRetChain = opRegRegImm(cUnit, kOpAdd, r1, rpc, 0);
Ben Chengc8293e72010-10-12 11:50:10 -07001075
Ben Chengba4fc8b2009-06-01 13:00:29 -07001076 /* r4PC = dalvikCallsite */
1077 loadConstant(cUnit, r4PC,
1078 (int) (cUnit->method->insns + mir->offset));
1079 addrRetChain->generic.target = (LIR *) retChainingCell;
Ben Chengc8293e72010-10-12 11:50:10 -07001080
1081 /* r7 = calleeMethod->registersSize */
1082 loadConstant(cUnit, r7, calleeMethod->registersSize);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001083 /*
Ben Cheng38329f52009-07-07 14:19:20 -07001084 * r0 = calleeMethod (loaded upon calling genInvokeSingletonCommon)
Ben Chengba4fc8b2009-06-01 13:00:29 -07001085 * r1 = &ChainingCell
Ben Chengc8293e72010-10-12 11:50:10 -07001086 * r2 = calleeMethod->outsSize (to be loaded later for Java callees)
Ben Chengba4fc8b2009-06-01 13:00:29 -07001087 * r4PC = callsiteDPC
Ben Chengc8293e72010-10-12 11:50:10 -07001088 * r7 = calleeMethod->registersSize
Ben Chengba4fc8b2009-06-01 13:00:29 -07001089 */
1090 if (dvmIsNativeMethod(calleeMethod)) {
buzbee18fba342011-01-19 15:31:15 -08001091 genDispatchToHandler(cUnit, gDvmJit.methodTraceSupport ?
1092 TEMPLATE_INVOKE_METHOD_NATIVE_PROF :
1093 TEMPLATE_INVOKE_METHOD_NATIVE);
Ben Cheng978738d2010-05-13 13:45:57 -07001094#if defined(WITH_JIT_TUNING)
Ben Cheng38329f52009-07-07 14:19:20 -07001095 gDvmJit.invokeNative++;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001096#endif
1097 } else {
Ben Chengc8293e72010-10-12 11:50:10 -07001098 /* For Java callees, set up r2 to be calleeMethod->outsSize */
1099 loadConstant(cUnit, r2, calleeMethod->outsSize);
buzbee18fba342011-01-19 15:31:15 -08001100 genDispatchToHandler(cUnit, gDvmJit.methodTraceSupport ?
1101 TEMPLATE_INVOKE_METHOD_CHAIN_PROF :
1102 TEMPLATE_INVOKE_METHOD_CHAIN);
Ben Cheng978738d2010-05-13 13:45:57 -07001103#if defined(WITH_JIT_TUNING)
Ben Cheng86717f72010-03-05 15:27:21 -08001104 gDvmJit.invokeMonomorphic++;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001105#endif
Ben Cheng38329f52009-07-07 14:19:20 -07001106 /* Branch to the chaining cell */
Ben Chengba4fc8b2009-06-01 13:00:29 -07001107 genUnconditionalBranch(cUnit, &labelList[bb->taken->id]);
1108 }
1109 /* Handle exceptions using the interpreter */
1110 genTrap(cUnit, mir->offset, pcrLabel);
1111}
1112
Ben Cheng38329f52009-07-07 14:19:20 -07001113/*
1114 * Generate code to check the validity of a predicted chain and take actions
1115 * based on the result.
1116 *
1117 * 0x426a99aa : ldr r4, [pc, #72] --> r4 <- dalvikPC of this invoke
1118 * 0x426a99ac : add r1, pc, #32 --> r1 <- &retChainingCell
1119 * 0x426a99ae : add r2, pc, #40 --> r2 <- &predictedChainingCell
1120 * 0x426a99b0 : blx_1 0x426a918c --+ TEMPLATE_INVOKE_METHOD_PREDICTED_CHAIN
1121 * 0x426a99b2 : blx_2 see above --+
1122 * 0x426a99b4 : b 0x426a99d8 --> off to the predicted chain
1123 * 0x426a99b6 : b 0x426a99c8 --> punt to the interpreter
1124 * 0x426a99b8 : ldr r0, [r7, #44] --> r0 <- this->class->vtable[methodIdx]
1125 * 0x426a99ba : cmp r1, #0 --> compare r1 (rechain count) against 0
1126 * 0x426a99bc : bgt 0x426a99c2 --> >=0? don't rechain
Ben Chengaf5aa1f2011-01-04 15:37:04 -08001127 * 0x426a99be : ldr r7, [pc, #off]--+ dvmJitToPatchPredictedChain
Ben Cheng38329f52009-07-07 14:19:20 -07001128 * 0x426a99c0 : blx r7 --+
1129 * 0x426a99c2 : add r1, pc, #12 --> r1 <- &retChainingCell
1130 * 0x426a99c4 : blx_1 0x426a9098 --+ TEMPLATE_INVOKE_METHOD_NO_OPT
1131 * 0x426a99c6 : blx_2 see above --+
1132 */
1133static void genInvokeVirtualCommon(CompilationUnit *cUnit, MIR *mir,
1134 int methodIndex,
Bill Buzbee89efc3d2009-07-28 11:22:22 -07001135 ArmLIR *retChainingCell,
1136 ArmLIR *predChainingCell,
1137 ArmLIR *pcrLabel)
Ben Cheng38329f52009-07-07 14:19:20 -07001138{
Bill Buzbee1465db52009-09-23 17:17:35 -07001139 /*
1140 * Note: all Dalvik register state should be flushed to
1141 * memory by the point, so register usage restrictions no
1142 * longer apply. Lock temps to prevent them from being
1143 * allocated by utility routines.
1144 */
Bill Buzbeec6f10662010-02-09 11:16:15 -08001145 dvmCompilerLockAllTemps(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07001146
Ben Cheng38329f52009-07-07 14:19:20 -07001147 /* "this" is already left in r0 by genProcessArgs* */
1148
1149 /* r4PC = dalvikCallsite */
1150 loadConstant(cUnit, r4PC,
1151 (int) (cUnit->method->insns + mir->offset));
1152
1153 /* r1 = &retChainingCell */
Bill Buzbee1465db52009-09-23 17:17:35 -07001154 ArmLIR *addrRetChain = opRegRegImm(cUnit, kOpAdd, r1, rpc, 0);
Ben Cheng38329f52009-07-07 14:19:20 -07001155 addrRetChain->generic.target = (LIR *) retChainingCell;
1156
1157 /* r2 = &predictedChainingCell */
Bill Buzbee1465db52009-09-23 17:17:35 -07001158 ArmLIR *predictedChainingCell = opRegRegImm(cUnit, kOpAdd, r2, rpc, 0);
Ben Cheng38329f52009-07-07 14:19:20 -07001159 predictedChainingCell->generic.target = (LIR *) predChainingCell;
1160
buzbee18fba342011-01-19 15:31:15 -08001161 genDispatchToHandler(cUnit, gDvmJit.methodTraceSupport ?
1162 TEMPLATE_INVOKE_METHOD_PREDICTED_CHAIN_PROF :
1163 TEMPLATE_INVOKE_METHOD_PREDICTED_CHAIN);
Ben Cheng38329f52009-07-07 14:19:20 -07001164
1165 /* return through lr - jump to the chaining cell */
1166 genUnconditionalBranch(cUnit, predChainingCell);
1167
1168 /*
1169 * null-check on "this" may have been eliminated, but we still need a PC-
1170 * reconstruction label for stack overflow bailout.
1171 */
1172 if (pcrLabel == NULL) {
1173 int dPC = (int) (cUnit->method->insns + mir->offset);
Carl Shapirofc75f3e2010-12-07 11:43:38 -08001174 pcrLabel = (ArmLIR *) dvmCompilerNew(sizeof(ArmLIR), true);
Dan Bornstein9a1f8162010-12-01 17:02:26 -08001175 pcrLabel->opcode = kArmPseudoPCReconstructionCell;
Ben Cheng38329f52009-07-07 14:19:20 -07001176 pcrLabel->operands[0] = dPC;
1177 pcrLabel->operands[1] = mir->offset;
1178 /* Insert the place holder to the growable list */
Ben Cheng00603072010-10-28 11:13:58 -07001179 dvmInsertGrowableList(&cUnit->pcReconstructionList,
1180 (intptr_t) pcrLabel);
Ben Cheng38329f52009-07-07 14:19:20 -07001181 }
1182
1183 /* return through lr+2 - punt to the interpreter */
1184 genUnconditionalBranch(cUnit, pcrLabel);
1185
1186 /*
1187 * return through lr+4 - fully resolve the callee method.
1188 * r1 <- count
1189 * r2 <- &predictedChainCell
1190 * r3 <- this->class
1191 * r4 <- dPC
1192 * r7 <- this->class->vtable
1193 */
1194
1195 /* r0 <- calleeMethod */
Bill Buzbee270c1d62009-08-13 16:58:07 -07001196 loadWordDisp(cUnit, r7, methodIndex * 4, r0);
Ben Cheng38329f52009-07-07 14:19:20 -07001197
1198 /* Check if rechain limit is reached */
buzbee8f8109a2010-08-31 10:16:35 -07001199 ArmLIR *bypassRechaining = genCmpImmBranch(cUnit, kArmCondGt, r1, 0);
Ben Cheng38329f52009-07-07 14:19:20 -07001200
Ben Chengaf5aa1f2011-01-04 15:37:04 -08001201 LOAD_FUNC_ADDR(cUnit, r7, (int) dvmJitToPatchPredictedChain);
Ben Cheng38329f52009-07-07 14:19:20 -07001202
Ben Chengb88ec3c2010-05-17 12:50:33 -07001203 genRegCopy(cUnit, r1, rGLUE);
1204
Ben Cheng38329f52009-07-07 14:19:20 -07001205 /*
1206 * r0 = calleeMethod
1207 * r2 = &predictedChainingCell
1208 * r3 = class
1209 *
1210 * &returnChainingCell has been loaded into r1 but is not needed
1211 * when patching the chaining cell and will be clobbered upon
1212 * returning so it will be reconstructed again.
1213 */
Bill Buzbee1465db52009-09-23 17:17:35 -07001214 opReg(cUnit, kOpBlx, r7);
Ben Cheng38329f52009-07-07 14:19:20 -07001215
1216 /* r1 = &retChainingCell */
Bill Buzbee1465db52009-09-23 17:17:35 -07001217 addrRetChain = opRegRegImm(cUnit, kOpAdd, r1, rpc, 0);
Ben Cheng38329f52009-07-07 14:19:20 -07001218 addrRetChain->generic.target = (LIR *) retChainingCell;
1219
1220 bypassRechaining->generic.target = (LIR *) addrRetChain;
1221 /*
1222 * r0 = calleeMethod,
1223 * r1 = &ChainingCell,
1224 * r4PC = callsiteDPC,
1225 */
buzbee18fba342011-01-19 15:31:15 -08001226 genDispatchToHandler(cUnit, gDvmJit.methodTraceSupport ?
1227 TEMPLATE_INVOKE_METHOD_NO_OPT_PROF :
1228 TEMPLATE_INVOKE_METHOD_NO_OPT);
Ben Cheng978738d2010-05-13 13:45:57 -07001229#if defined(WITH_JIT_TUNING)
Ben Cheng86717f72010-03-05 15:27:21 -08001230 gDvmJit.invokePolymorphic++;
Ben Cheng38329f52009-07-07 14:19:20 -07001231#endif
1232 /* Handle exceptions using the interpreter */
1233 genTrap(cUnit, mir->offset, pcrLabel);
1234}
1235
Ben Chengba4fc8b2009-06-01 13:00:29 -07001236/* Geneate a branch to go back to the interpreter */
1237static void genPuntToInterp(CompilationUnit *cUnit, unsigned int offset)
1238{
1239 /* r0 = dalvik pc */
Bill Buzbeec6f10662010-02-09 11:16:15 -08001240 dvmCompilerFlushAllRegs(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001241 loadConstant(cUnit, r0, (int) (cUnit->method->insns + offset));
Bill Buzbee270c1d62009-08-13 16:58:07 -07001242 loadWordDisp(cUnit, rGLUE, offsetof(InterpState,
1243 jitToInterpEntries.dvmJitToInterpPunt), r1);
Bill Buzbee1465db52009-09-23 17:17:35 -07001244 opReg(cUnit, kOpBlx, r1);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001245}
1246
1247/*
1248 * Attempt to single step one instruction using the interpreter and return
1249 * to the compiled code for the next Dalvik instruction
1250 */
1251static void genInterpSingleStep(CompilationUnit *cUnit, MIR *mir)
1252{
Dan Bornsteine4852762010-12-02 12:45:00 -08001253 int flags = dexGetFlagsFromOpcode(mir->dalvikInsn.opcode);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001254 int flagsToCheck = kInstrCanBranch | kInstrCanSwitch | kInstrCanReturn |
1255 kInstrCanThrow;
Bill Buzbee1465db52009-09-23 17:17:35 -07001256
Bill Buzbee45273872010-03-11 11:12:15 -08001257 //If already optimized out, just ignore
Dan Bornstein9a1f8162010-12-01 17:02:26 -08001258 if (mir->dalvikInsn.opcode == OP_NOP)
Bill Buzbee45273872010-03-11 11:12:15 -08001259 return;
1260
Bill Buzbee1465db52009-09-23 17:17:35 -07001261 //Ugly, but necessary. Flush all Dalvik regs so Interp can find them
Bill Buzbeec6f10662010-02-09 11:16:15 -08001262 dvmCompilerFlushAllRegs(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07001263
Ben Chengba4fc8b2009-06-01 13:00:29 -07001264 if ((mir->next == NULL) || (flags & flagsToCheck)) {
1265 genPuntToInterp(cUnit, mir->offset);
1266 return;
1267 }
1268 int entryAddr = offsetof(InterpState,
1269 jitToInterpEntries.dvmJitToInterpSingleStep);
Bill Buzbee270c1d62009-08-13 16:58:07 -07001270 loadWordDisp(cUnit, rGLUE, entryAddr, r2);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001271 /* r0 = dalvik pc */
1272 loadConstant(cUnit, r0, (int) (cUnit->method->insns + mir->offset));
1273 /* r1 = dalvik pc of following instruction */
1274 loadConstant(cUnit, r1, (int) (cUnit->method->insns + mir->next->offset));
Bill Buzbee1465db52009-09-23 17:17:35 -07001275 opReg(cUnit, kOpBlx, r2);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001276}
1277
Ben Chengfc075c22010-05-28 15:20:08 -07001278#if defined(WITH_DEADLOCK_PREDICTION) || defined(WITH_MONITOR_TRACKING) || \
1279 defined(_ARMV5TE) || defined(_ARMV5TE_VFP)
Bill Buzbeec1d9ed42010-02-02 11:04:33 -08001280/*
1281 * To prevent a thread in a monitor wait from blocking the Jit from
1282 * resetting the code cache, heavyweight monitor lock will not
1283 * be allowed to return to an existing translation. Instead, we will
1284 * handle them by branching to a handler, which will in turn call the
1285 * runtime lock routine and then branch directly back to the
1286 * interpreter main loop. Given the high cost of the heavyweight
1287 * lock operation, this additional cost should be slight (especially when
1288 * considering that we expect the vast majority of lock operations to
1289 * use the fast-path thin lock bypass).
1290 */
Ben Cheng5d90c202009-11-22 23:31:11 -08001291static void genMonitorPortable(CompilationUnit *cUnit, MIR *mir)
Bill Buzbee270c1d62009-08-13 16:58:07 -07001292{
Dan Bornstein9a1f8162010-12-01 17:02:26 -08001293 bool isEnter = (mir->dalvikInsn.opcode == OP_MONITOR_ENTER);
Bill Buzbee1465db52009-09-23 17:17:35 -07001294 genExportPC(cUnit, mir);
Bill Buzbeec6f10662010-02-09 11:16:15 -08001295 dvmCompilerFlushAllRegs(cUnit); /* Send everything to home location */
1296 RegLocation rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07001297 loadValueDirectFixed(cUnit, rlSrc, r1);
1298 loadWordDisp(cUnit, rGLUE, offsetof(InterpState, self), r0);
Bill Buzbeec1d9ed42010-02-02 11:04:33 -08001299 genNullCheck(cUnit, rlSrc.sRegLow, r1, mir->offset, NULL);
Bill Buzbeeefbd3c52009-11-04 22:18:40 -08001300 if (isEnter) {
Bill Buzbeec1d9ed42010-02-02 11:04:33 -08001301 /* Get dPC of next insn */
1302 loadConstant(cUnit, r4PC, (int)(cUnit->method->insns + mir->offset +
Dan Bornsteine4852762010-12-02 12:45:00 -08001303 dexGetWidthFromOpcode(OP_MONITOR_ENTER)));
Bill Buzbeec1d9ed42010-02-02 11:04:33 -08001304#if defined(WITH_DEADLOCK_PREDICTION)
1305 genDispatchToHandler(cUnit, TEMPLATE_MONITOR_ENTER_DEBUG);
1306#else
1307 genDispatchToHandler(cUnit, TEMPLATE_MONITOR_ENTER);
1308#endif
Bill Buzbee1465db52009-09-23 17:17:35 -07001309 } else {
Ben Chengbd1326d2010-04-02 15:04:53 -07001310 LOAD_FUNC_ADDR(cUnit, r2, (int)dvmUnlockObject);
Bill Buzbeec1d9ed42010-02-02 11:04:33 -08001311 /* Do the call */
1312 opReg(cUnit, kOpBlx, r2);
buzbee8f8109a2010-08-31 10:16:35 -07001313 /* Did we throw? */
1314 ArmLIR *branchOver = genCmpImmBranch(cUnit, kArmCondNe, r0, 0);
Bill Buzbee6bbdd6b2010-02-16 14:40:01 -08001315 loadConstant(cUnit, r0,
1316 (int) (cUnit->method->insns + mir->offset +
Dan Bornsteine4852762010-12-02 12:45:00 -08001317 dexGetWidthFromOpcode(OP_MONITOR_EXIT)));
Bill Buzbee6bbdd6b2010-02-16 14:40:01 -08001318 genDispatchToHandler(cUnit, TEMPLATE_THROW_EXCEPTION_COMMON);
1319 ArmLIR *target = newLIR0(cUnit, kArmPseudoTargetLabel);
1320 target->defMask = ENCODE_ALL;
1321 branchOver->generic.target = (LIR *) target;
Elliott Hughes6a555132010-02-25 15:41:42 -08001322 dvmCompilerClobberCallRegs(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07001323 }
Bill Buzbee270c1d62009-08-13 16:58:07 -07001324}
Ben Chengfc075c22010-05-28 15:20:08 -07001325#endif
Bill Buzbee270c1d62009-08-13 16:58:07 -07001326
Ben Chengba4fc8b2009-06-01 13:00:29 -07001327/*
1328 * The following are the first-level codegen routines that analyze the format
1329 * of each bytecode then either dispatch special purpose codegen routines
1330 * or produce corresponding Thumb instructions directly.
1331 */
1332
1333static bool handleFmt10t_Fmt20t_Fmt30t(CompilationUnit *cUnit, MIR *mir,
Bill Buzbee89efc3d2009-07-28 11:22:22 -07001334 BasicBlock *bb, ArmLIR *labelList)
Ben Chengba4fc8b2009-06-01 13:00:29 -07001335{
1336 /* For OP_GOTO, OP_GOTO_16, and OP_GOTO_32 */
1337 genUnconditionalBranch(cUnit, &labelList[bb->taken->id]);
1338 return false;
1339}
1340
1341static bool handleFmt10x(CompilationUnit *cUnit, MIR *mir)
1342{
Dan Bornstein9a1f8162010-12-01 17:02:26 -08001343 Opcode dalvikOpcode = mir->dalvikInsn.opcode;
1344 if ((dalvikOpcode >= OP_UNUSED_3E) && (dalvikOpcode <= OP_UNUSED_43)) {
1345 LOGE("Codegen: got unused opcode 0x%x\n",dalvikOpcode);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001346 return true;
1347 }
Dan Bornstein9a1f8162010-12-01 17:02:26 -08001348 switch (dalvikOpcode) {
Andy McFadden291758c2010-09-10 08:04:52 -07001349 case OP_RETURN_VOID_BARRIER:
buzbee2ce33c92010-11-01 15:53:27 -07001350 dvmCompilerGenMemBarrier(cUnit, kST);
1351 // Intentional fallthrough
1352 case OP_RETURN_VOID:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001353 genReturnCommon(cUnit,mir);
1354 break;
1355 case OP_UNUSED_73:
1356 case OP_UNUSED_79:
1357 case OP_UNUSED_7A:
Dan Bornstein90f15432010-12-02 16:46:25 -08001358 case OP_DISPATCH_FF:
Dan Bornstein9a1f8162010-12-01 17:02:26 -08001359 LOGE("Codegen: got unused opcode 0x%x\n",dalvikOpcode);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001360 return true;
1361 case OP_NOP:
1362 break;
1363 default:
1364 return true;
1365 }
1366 return false;
1367}
1368
1369static bool handleFmt11n_Fmt31i(CompilationUnit *cUnit, MIR *mir)
1370{
Bill Buzbee1465db52009-09-23 17:17:35 -07001371 RegLocation rlDest;
1372 RegLocation rlResult;
1373 if (mir->ssaRep->numDefs == 2) {
Bill Buzbeec6f10662010-02-09 11:16:15 -08001374 rlDest = dvmCompilerGetDestWide(cUnit, mir, 0, 1);
Bill Buzbee1465db52009-09-23 17:17:35 -07001375 } else {
Bill Buzbeec6f10662010-02-09 11:16:15 -08001376 rlDest = dvmCompilerGetDest(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07001377 }
Ben Chenge9695e52009-06-16 16:11:47 -07001378
Dan Bornstein9a1f8162010-12-01 17:02:26 -08001379 switch (mir->dalvikInsn.opcode) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07001380 case OP_CONST:
Ben Chenge9695e52009-06-16 16:11:47 -07001381 case OP_CONST_4: {
Bill Buzbeec6f10662010-02-09 11:16:15 -08001382 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kAnyReg, true);
Ben Chengbd1326d2010-04-02 15:04:53 -07001383 loadConstantNoClobber(cUnit, rlResult.lowReg, mir->dalvikInsn.vB);
Bill Buzbee1465db52009-09-23 17:17:35 -07001384 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001385 break;
Ben Chenge9695e52009-06-16 16:11:47 -07001386 }
1387 case OP_CONST_WIDE_32: {
Bill Buzbee1465db52009-09-23 17:17:35 -07001388 //TUNING: single routine to load constant pair for support doubles
Bill Buzbee964a7b02010-01-28 12:54:19 -08001389 //TUNING: load 0/-1 separately to avoid load dependency
Bill Buzbeec6f10662010-02-09 11:16:15 -08001390 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Ben Chengbd1326d2010-04-02 15:04:53 -07001391 loadConstantNoClobber(cUnit, rlResult.lowReg, mir->dalvikInsn.vB);
Bill Buzbee1465db52009-09-23 17:17:35 -07001392 opRegRegImm(cUnit, kOpAsr, rlResult.highReg,
1393 rlResult.lowReg, 31);
1394 storeValueWide(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001395 break;
Ben Chenge9695e52009-06-16 16:11:47 -07001396 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07001397 default:
1398 return true;
1399 }
1400 return false;
1401}
1402
1403static bool handleFmt21h(CompilationUnit *cUnit, MIR *mir)
1404{
Bill Buzbee1465db52009-09-23 17:17:35 -07001405 RegLocation rlDest;
1406 RegLocation rlResult;
1407 if (mir->ssaRep->numDefs == 2) {
Bill Buzbeec6f10662010-02-09 11:16:15 -08001408 rlDest = dvmCompilerGetDestWide(cUnit, mir, 0, 1);
Bill Buzbee1465db52009-09-23 17:17:35 -07001409 } else {
Bill Buzbeec6f10662010-02-09 11:16:15 -08001410 rlDest = dvmCompilerGetDest(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07001411 }
Bill Buzbeec6f10662010-02-09 11:16:15 -08001412 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kAnyReg, true);
Ben Chenge9695e52009-06-16 16:11:47 -07001413
Dan Bornstein9a1f8162010-12-01 17:02:26 -08001414 switch (mir->dalvikInsn.opcode) {
Ben Chenge9695e52009-06-16 16:11:47 -07001415 case OP_CONST_HIGH16: {
Ben Chengbd1326d2010-04-02 15:04:53 -07001416 loadConstantNoClobber(cUnit, rlResult.lowReg,
1417 mir->dalvikInsn.vB << 16);
Bill Buzbee1465db52009-09-23 17:17:35 -07001418 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001419 break;
Ben Chenge9695e52009-06-16 16:11:47 -07001420 }
1421 case OP_CONST_WIDE_HIGH16: {
Bill Buzbee1465db52009-09-23 17:17:35 -07001422 loadConstantValueWide(cUnit, rlResult.lowReg, rlResult.highReg,
1423 0, mir->dalvikInsn.vB << 16);
1424 storeValueWide(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001425 break;
Ben Chenge9695e52009-06-16 16:11:47 -07001426 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07001427 default:
1428 return true;
1429 }
1430 return false;
1431}
1432
jeffhao71eee1f2011-01-04 14:18:54 -08001433static bool handleFmt20bc_Fmt40sc(CompilationUnit *cUnit, MIR *mir)
Ben Chengba4fc8b2009-06-01 13:00:29 -07001434{
jeffhao71eee1f2011-01-04 14:18:54 -08001435 /* For OP_THROW_VERIFICATION_ERROR & OP_THROW_VERIFICATION_ERROR_JUMBO */
Ben Chengba4fc8b2009-06-01 13:00:29 -07001436 genInterpSingleStep(cUnit, mir);
1437 return false;
1438}
1439
jeffhao71eee1f2011-01-04 14:18:54 -08001440static bool handleFmt21c_Fmt31c_Fmt41c(CompilationUnit *cUnit, MIR *mir)
Ben Chengba4fc8b2009-06-01 13:00:29 -07001441{
Bill Buzbee1465db52009-09-23 17:17:35 -07001442 RegLocation rlResult;
1443 RegLocation rlDest;
1444 RegLocation rlSrc;
Ben Chenge9695e52009-06-16 16:11:47 -07001445
Dan Bornstein9a1f8162010-12-01 17:02:26 -08001446 switch (mir->dalvikInsn.opcode) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07001447 case OP_CONST_STRING_JUMBO:
1448 case OP_CONST_STRING: {
1449 void *strPtr = (void*)
1450 (cUnit->method->clazz->pDvmDex->pResStrings[mir->dalvikInsn.vB]);
Ben Chengdd6e8702010-05-07 13:05:47 -07001451
1452 if (strPtr == NULL) {
1453 LOGE("Unexpected null string");
1454 dvmAbort();
1455 }
1456
Bill Buzbeec6f10662010-02-09 11:16:15 -08001457 rlDest = dvmCompilerGetDest(cUnit, mir, 0);
1458 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Ben Chengbd1326d2010-04-02 15:04:53 -07001459 loadConstantNoClobber(cUnit, rlResult.lowReg, (int) strPtr );
Bill Buzbee1465db52009-09-23 17:17:35 -07001460 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001461 break;
1462 }
jeffhao71eee1f2011-01-04 14:18:54 -08001463 case OP_CONST_CLASS:
1464 case OP_CONST_CLASS_JUMBO: {
Ben Chengba4fc8b2009-06-01 13:00:29 -07001465 void *classPtr = (void*)
1466 (cUnit->method->clazz->pDvmDex->pResClasses[mir->dalvikInsn.vB]);
Ben Chengdd6e8702010-05-07 13:05:47 -07001467
1468 if (classPtr == NULL) {
1469 LOGE("Unexpected null class");
1470 dvmAbort();
1471 }
1472
Bill Buzbeec6f10662010-02-09 11:16:15 -08001473 rlDest = dvmCompilerGetDest(cUnit, mir, 0);
1474 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Ben Chengbd1326d2010-04-02 15:04:53 -07001475 loadConstantNoClobber(cUnit, rlResult.lowReg, (int) classPtr );
Bill Buzbee1465db52009-09-23 17:17:35 -07001476 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001477 break;
1478 }
jeffhao71eee1f2011-01-04 14:18:54 -08001479 case OP_SGET:
buzbeeecf8f6e2010-07-20 14:53:42 -07001480 case OP_SGET_VOLATILE:
jeffhao71eee1f2011-01-04 14:18:54 -08001481 case OP_SGET_JUMBO:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001482 case OP_SGET_OBJECT:
jeffhao71eee1f2011-01-04 14:18:54 -08001483 case OP_SGET_OBJECT_VOLATILE:
1484 case OP_SGET_OBJECT_JUMBO:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001485 case OP_SGET_BOOLEAN:
jeffhao71eee1f2011-01-04 14:18:54 -08001486 case OP_SGET_BOOLEAN_JUMBO:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001487 case OP_SGET_CHAR:
jeffhao71eee1f2011-01-04 14:18:54 -08001488 case OP_SGET_CHAR_JUMBO:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001489 case OP_SGET_BYTE:
jeffhao71eee1f2011-01-04 14:18:54 -08001490 case OP_SGET_BYTE_JUMBO:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001491 case OP_SGET_SHORT:
jeffhao71eee1f2011-01-04 14:18:54 -08001492 case OP_SGET_SHORT_JUMBO: {
Bill Buzbee50a6bf22009-07-08 13:08:04 -07001493 int valOffset = offsetof(StaticField, value);
Bill Buzbeec6f10662010-02-09 11:16:15 -08001494 int tReg = dvmCompilerAllocTemp(cUnit);
buzbeeecf8f6e2010-07-20 14:53:42 -07001495 bool isVolatile;
Ben Cheng7a2697d2010-06-07 13:44:23 -07001496 const Method *method = (mir->OptimizationFlags & MIR_CALLEE) ?
1497 mir->meta.calleeMethod : cUnit->method;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001498 void *fieldPtr = (void*)
Ben Cheng7a2697d2010-06-07 13:44:23 -07001499 (method->clazz->pDvmDex->pResFields[mir->dalvikInsn.vB]);
Ben Chengdd6e8702010-05-07 13:05:47 -07001500
1501 if (fieldPtr == NULL) {
1502 LOGE("Unexpected null static field");
1503 dvmAbort();
1504 }
1505
Dan Bornstein9a1f8162010-12-01 17:02:26 -08001506 isVolatile = (mir->dalvikInsn.opcode == OP_SGET_VOLATILE) ||
1507 (mir->dalvikInsn.opcode == OP_SGET_OBJECT_VOLATILE) ||
Carl Shapirofc75f3e2010-12-07 11:43:38 -08001508 dvmIsVolatileField((Field *) fieldPtr);
buzbeeecf8f6e2010-07-20 14:53:42 -07001509
Bill Buzbeec6f10662010-02-09 11:16:15 -08001510 rlDest = dvmCompilerGetDest(cUnit, mir, 0);
1511 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kAnyReg, true);
Bill Buzbee1465db52009-09-23 17:17:35 -07001512 loadConstant(cUnit, tReg, (int) fieldPtr + valOffset);
Ben Cheng11d8f142010-03-24 15:24:19 -07001513
buzbeeecf8f6e2010-07-20 14:53:42 -07001514 if (isVolatile) {
buzbee2ce33c92010-11-01 15:53:27 -07001515 dvmCompilerGenMemBarrier(cUnit, kSY);
buzbeeecf8f6e2010-07-20 14:53:42 -07001516 }
Ben Cheng11d8f142010-03-24 15:24:19 -07001517 HEAP_ACCESS_SHADOW(true);
Bill Buzbee1465db52009-09-23 17:17:35 -07001518 loadWordDisp(cUnit, tReg, 0, rlResult.lowReg);
Ben Cheng11d8f142010-03-24 15:24:19 -07001519 HEAP_ACCESS_SHADOW(false);
1520
Bill Buzbee1465db52009-09-23 17:17:35 -07001521 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001522 break;
1523 }
jeffhao71eee1f2011-01-04 14:18:54 -08001524 case OP_SGET_WIDE:
1525 case OP_SGET_WIDE_JUMBO: {
Bill Buzbee50a6bf22009-07-08 13:08:04 -07001526 int valOffset = offsetof(StaticField, value);
Ben Cheng7a2697d2010-06-07 13:44:23 -07001527 const Method *method = (mir->OptimizationFlags & MIR_CALLEE) ?
1528 mir->meta.calleeMethod : cUnit->method;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001529 void *fieldPtr = (void*)
Ben Cheng7a2697d2010-06-07 13:44:23 -07001530 (method->clazz->pDvmDex->pResFields[mir->dalvikInsn.vB]);
Ben Chengdd6e8702010-05-07 13:05:47 -07001531
1532 if (fieldPtr == NULL) {
1533 LOGE("Unexpected null static field");
1534 dvmAbort();
1535 }
1536
Bill Buzbeec6f10662010-02-09 11:16:15 -08001537 int tReg = dvmCompilerAllocTemp(cUnit);
Bill Buzbeec6f10662010-02-09 11:16:15 -08001538 rlDest = dvmCompilerGetDestWide(cUnit, mir, 0, 1);
1539 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kAnyReg, true);
Bill Buzbee1465db52009-09-23 17:17:35 -07001540 loadConstant(cUnit, tReg, (int) fieldPtr + valOffset);
Ben Cheng11d8f142010-03-24 15:24:19 -07001541
1542 HEAP_ACCESS_SHADOW(true);
Bill Buzbee1465db52009-09-23 17:17:35 -07001543 loadPair(cUnit, tReg, rlResult.lowReg, rlResult.highReg);
Ben Cheng11d8f142010-03-24 15:24:19 -07001544 HEAP_ACCESS_SHADOW(false);
1545
Bill Buzbee1465db52009-09-23 17:17:35 -07001546 storeValueWide(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001547 break;
1548 }
jeffhao71eee1f2011-01-04 14:18:54 -08001549 case OP_SPUT:
1550 case OP_SPUT_VOLATILE:
1551 case OP_SPUT_JUMBO:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001552 case OP_SPUT_OBJECT:
buzbeeddc7d292010-09-02 17:16:24 -07001553 case OP_SPUT_OBJECT_VOLATILE:
jeffhao71eee1f2011-01-04 14:18:54 -08001554 case OP_SPUT_OBJECT_JUMBO:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001555 case OP_SPUT_BOOLEAN:
jeffhao71eee1f2011-01-04 14:18:54 -08001556 case OP_SPUT_BOOLEAN_JUMBO:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001557 case OP_SPUT_CHAR:
jeffhao71eee1f2011-01-04 14:18:54 -08001558 case OP_SPUT_CHAR_JUMBO:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001559 case OP_SPUT_BYTE:
jeffhao71eee1f2011-01-04 14:18:54 -08001560 case OP_SPUT_BYTE_JUMBO:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001561 case OP_SPUT_SHORT:
jeffhao71eee1f2011-01-04 14:18:54 -08001562 case OP_SPUT_SHORT_JUMBO: {
Bill Buzbee50a6bf22009-07-08 13:08:04 -07001563 int valOffset = offsetof(StaticField, value);
Bill Buzbeec6f10662010-02-09 11:16:15 -08001564 int tReg = dvmCompilerAllocTemp(cUnit);
buzbeed3b0a4b2010-09-27 11:30:22 -07001565 int objHead;
buzbeeecf8f6e2010-07-20 14:53:42 -07001566 bool isVolatile;
buzbeed3b0a4b2010-09-27 11:30:22 -07001567 bool isSputObject;
Ben Cheng7a2697d2010-06-07 13:44:23 -07001568 const Method *method = (mir->OptimizationFlags & MIR_CALLEE) ?
1569 mir->meta.calleeMethod : cUnit->method;
1570 void *fieldPtr = (void*)
1571 (method->clazz->pDvmDex->pResFields[mir->dalvikInsn.vB]);
Ben Chenge9695e52009-06-16 16:11:47 -07001572
Dan Bornstein9a1f8162010-12-01 17:02:26 -08001573 isVolatile = (mir->dalvikInsn.opcode == OP_SPUT_VOLATILE) ||
1574 (mir->dalvikInsn.opcode == OP_SPUT_OBJECT_VOLATILE) ||
Carl Shapirofc75f3e2010-12-07 11:43:38 -08001575 dvmIsVolatileField((Field *) fieldPtr);
buzbeeecf8f6e2010-07-20 14:53:42 -07001576
Dan Bornstein9a1f8162010-12-01 17:02:26 -08001577 isSputObject = (mir->dalvikInsn.opcode == OP_SPUT_OBJECT) ||
jeffhao71eee1f2011-01-04 14:18:54 -08001578 (mir->dalvikInsn.opcode == OP_SPUT_OBJECT_JUMBO) ||
Dan Bornstein9a1f8162010-12-01 17:02:26 -08001579 (mir->dalvikInsn.opcode == OP_SPUT_OBJECT_VOLATILE);
buzbeed3b0a4b2010-09-27 11:30:22 -07001580
Ben Chengdd6e8702010-05-07 13:05:47 -07001581 if (fieldPtr == NULL) {
1582 LOGE("Unexpected null static field");
1583 dvmAbort();
1584 }
1585
Bill Buzbeec6f10662010-02-09 11:16:15 -08001586 rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07001587 rlSrc = loadValue(cUnit, rlSrc, kAnyReg);
buzbeeb78c76f2010-09-30 19:08:20 -07001588 loadConstant(cUnit, tReg, (int) fieldPtr);
buzbeed3b0a4b2010-09-27 11:30:22 -07001589 if (isSputObject) {
1590 objHead = dvmCompilerAllocTemp(cUnit);
buzbeeb78c76f2010-09-30 19:08:20 -07001591 loadWordDisp(cUnit, tReg, offsetof(Field, clazz), objHead);
buzbeed3b0a4b2010-09-27 11:30:22 -07001592 }
Ben Cheng11d8f142010-03-24 15:24:19 -07001593 HEAP_ACCESS_SHADOW(true);
buzbeeb78c76f2010-09-30 19:08:20 -07001594 storeWordDisp(cUnit, tReg, valOffset ,rlSrc.lowReg);
buzbeed3b0a4b2010-09-27 11:30:22 -07001595 dvmCompilerFreeTemp(cUnit, tReg);
Ben Cheng11d8f142010-03-24 15:24:19 -07001596 HEAP_ACCESS_SHADOW(false);
buzbeeecf8f6e2010-07-20 14:53:42 -07001597 if (isVolatile) {
buzbee2ce33c92010-11-01 15:53:27 -07001598 dvmCompilerGenMemBarrier(cUnit, kSY);
buzbeeecf8f6e2010-07-20 14:53:42 -07001599 }
buzbeed3b0a4b2010-09-27 11:30:22 -07001600 if (isSputObject) {
buzbeeb78c76f2010-09-30 19:08:20 -07001601 /* NOTE: marking card based sfield->clazz */
buzbeed3b0a4b2010-09-27 11:30:22 -07001602 markCard(cUnit, rlSrc.lowReg, objHead);
1603 dvmCompilerFreeTemp(cUnit, objHead);
buzbee919eb062010-07-12 12:59:22 -07001604 }
Ben Cheng11d8f142010-03-24 15:24:19 -07001605
Ben Chengba4fc8b2009-06-01 13:00:29 -07001606 break;
1607 }
jeffhao71eee1f2011-01-04 14:18:54 -08001608 case OP_SPUT_WIDE:
1609 case OP_SPUT_WIDE_JUMBO: {
Bill Buzbeec6f10662010-02-09 11:16:15 -08001610 int tReg = dvmCompilerAllocTemp(cUnit);
Bill Buzbee50a6bf22009-07-08 13:08:04 -07001611 int valOffset = offsetof(StaticField, value);
Ben Cheng7a2697d2010-06-07 13:44:23 -07001612 const Method *method = (mir->OptimizationFlags & MIR_CALLEE) ?
1613 mir->meta.calleeMethod : cUnit->method;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001614 void *fieldPtr = (void*)
Ben Cheng7a2697d2010-06-07 13:44:23 -07001615 (method->clazz->pDvmDex->pResFields[mir->dalvikInsn.vB]);
Ben Chenge9695e52009-06-16 16:11:47 -07001616
Ben Chengdd6e8702010-05-07 13:05:47 -07001617 if (fieldPtr == NULL) {
1618 LOGE("Unexpected null static field");
1619 dvmAbort();
1620 }
1621
Bill Buzbeec6f10662010-02-09 11:16:15 -08001622 rlSrc = dvmCompilerGetSrcWide(cUnit, mir, 0, 1);
Bill Buzbee1465db52009-09-23 17:17:35 -07001623 rlSrc = loadValueWide(cUnit, rlSrc, kAnyReg);
1624 loadConstant(cUnit, tReg, (int) fieldPtr + valOffset);
Ben Cheng11d8f142010-03-24 15:24:19 -07001625
1626 HEAP_ACCESS_SHADOW(true);
Bill Buzbee1465db52009-09-23 17:17:35 -07001627 storePair(cUnit, tReg, rlSrc.lowReg, rlSrc.highReg);
Ben Cheng11d8f142010-03-24 15:24:19 -07001628 HEAP_ACCESS_SHADOW(false);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001629 break;
1630 }
jeffhao71eee1f2011-01-04 14:18:54 -08001631 case OP_NEW_INSTANCE:
1632 case OP_NEW_INSTANCE_JUMBO: {
Ben Chenge9695e52009-06-16 16:11:47 -07001633 /*
1634 * Obey the calling convention and don't mess with the register
1635 * usage.
1636 */
Carl Shapirofc75f3e2010-12-07 11:43:38 -08001637 ClassObject *classPtr = (ClassObject *)
Ben Chengba4fc8b2009-06-01 13:00:29 -07001638 (cUnit->method->clazz->pDvmDex->pResClasses[mir->dalvikInsn.vB]);
Ben Chengdd6e8702010-05-07 13:05:47 -07001639
1640 if (classPtr == NULL) {
1641 LOGE("Unexpected null class");
1642 dvmAbort();
1643 }
1644
Ben Cheng79d173c2009-09-29 16:12:51 -07001645 /*
1646 * If it is going to throw, it should not make to the trace to begin
Bill Buzbee1465db52009-09-23 17:17:35 -07001647 * with. However, Alloc might throw, so we need to genExportPC()
Ben Cheng79d173c2009-09-29 16:12:51 -07001648 */
1649 assert((classPtr->accessFlags & (ACC_INTERFACE|ACC_ABSTRACT)) == 0);
Bill Buzbeec6f10662010-02-09 11:16:15 -08001650 dvmCompilerFlushAllRegs(cUnit); /* Everything to home location */
Bill Buzbee1465db52009-09-23 17:17:35 -07001651 genExportPC(cUnit, mir);
Ben Chengbd1326d2010-04-02 15:04:53 -07001652 LOAD_FUNC_ADDR(cUnit, r2, (int)dvmAllocObject);
Ben Chenge9695e52009-06-16 16:11:47 -07001653 loadConstant(cUnit, r0, (int) classPtr);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001654 loadConstant(cUnit, r1, ALLOC_DONT_TRACK);
Bill Buzbee1465db52009-09-23 17:17:35 -07001655 opReg(cUnit, kOpBlx, r2);
Elliott Hughes6a555132010-02-25 15:41:42 -08001656 dvmCompilerClobberCallRegs(cUnit);
Ben Cheng4f489172009-09-27 17:08:35 -07001657 /* generate a branch over if allocation is successful */
buzbee8f8109a2010-08-31 10:16:35 -07001658 ArmLIR *branchOver = genCmpImmBranch(cUnit, kArmCondNe, r0, 0);
Ben Cheng4f489172009-09-27 17:08:35 -07001659 /*
1660 * OOM exception needs to be thrown here and cannot re-execute
1661 */
1662 loadConstant(cUnit, r0,
1663 (int) (cUnit->method->insns + mir->offset));
1664 genDispatchToHandler(cUnit, TEMPLATE_THROW_EXCEPTION_COMMON);
1665 /* noreturn */
1666
Bill Buzbee1465db52009-09-23 17:17:35 -07001667 ArmLIR *target = newLIR0(cUnit, kArmPseudoTargetLabel);
Ben Cheng4f489172009-09-27 17:08:35 -07001668 target->defMask = ENCODE_ALL;
1669 branchOver->generic.target = (LIR *) target;
Bill Buzbeec6f10662010-02-09 11:16:15 -08001670 rlDest = dvmCompilerGetDest(cUnit, mir, 0);
1671 rlResult = dvmCompilerGetReturn(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07001672 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001673 break;
1674 }
jeffhao71eee1f2011-01-04 14:18:54 -08001675 case OP_CHECK_CAST:
1676 case OP_CHECK_CAST_JUMBO: {
Ben Chenge9695e52009-06-16 16:11:47 -07001677 /*
1678 * Obey the calling convention and don't mess with the register
1679 * usage.
1680 */
Ben Chengba4fc8b2009-06-01 13:00:29 -07001681 ClassObject *classPtr =
1682 (cUnit->method->clazz->pDvmDex->pResClasses[mir->dalvikInsn.vB]);
Bill Buzbee4df41a52009-11-12 17:07:16 -08001683 /*
1684 * Note: It is possible that classPtr is NULL at this point,
1685 * even though this instruction has been successfully interpreted.
1686 * If the previous interpretation had a null source, the
1687 * interpreter would not have bothered to resolve the clazz.
1688 * Bail out to the interpreter in this case, and log it
1689 * so that we can tell if it happens frequently.
1690 */
1691 if (classPtr == NULL) {
Ben Cheng11d8f142010-03-24 15:24:19 -07001692 LOGVV("null clazz in OP_CHECK_CAST, single-stepping");
Bill Buzbee4df41a52009-11-12 17:07:16 -08001693 genInterpSingleStep(cUnit, mir);
1694 return false;
1695 }
Bill Buzbeec6f10662010-02-09 11:16:15 -08001696 dvmCompilerFlushAllRegs(cUnit); /* Everything to home location */
Ben Chengba4fc8b2009-06-01 13:00:29 -07001697 loadConstant(cUnit, r1, (int) classPtr );
Bill Buzbeec6f10662010-02-09 11:16:15 -08001698 rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07001699 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
buzbee8f8109a2010-08-31 10:16:35 -07001700 /* Null? */
1701 ArmLIR *branch1 = genCmpImmBranch(cUnit, kArmCondEq,
1702 rlSrc.lowReg, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07001703 /*
1704 * rlSrc.lowReg now contains object->clazz. Note that
1705 * it could have been allocated r0, but we're okay so long
1706 * as we don't do anything desctructive until r0 is loaded
1707 * with clazz.
1708 */
Ben Chengba4fc8b2009-06-01 13:00:29 -07001709 /* r0 now contains object->clazz */
Bill Buzbee1465db52009-09-23 17:17:35 -07001710 loadWordDisp(cUnit, rlSrc.lowReg, offsetof(Object, clazz), r0);
Ben Chengbd1326d2010-04-02 15:04:53 -07001711 LOAD_FUNC_ADDR(cUnit, r2, (int)dvmInstanceofNonTrivial);
Bill Buzbee1465db52009-09-23 17:17:35 -07001712 opRegReg(cUnit, kOpCmp, r0, r1);
1713 ArmLIR *branch2 = opCondBranch(cUnit, kArmCondEq);
1714 opReg(cUnit, kOpBlx, r2);
Elliott Hughes6a555132010-02-25 15:41:42 -08001715 dvmCompilerClobberCallRegs(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07001716 /*
1717 * If null, check cast failed - punt to the interpreter. Because
1718 * interpreter will be the one throwing, we don't need to
1719 * genExportPC() here.
1720 */
Bill Buzbee270c1d62009-08-13 16:58:07 -07001721 genZeroCheck(cUnit, r0, mir->offset, NULL);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001722 /* check cast passed - branch target here */
Bill Buzbee1465db52009-09-23 17:17:35 -07001723 ArmLIR *target = newLIR0(cUnit, kArmPseudoTargetLabel);
Ben Chengd7d426a2009-09-22 11:23:36 -07001724 target->defMask = ENCODE_ALL;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001725 branch1->generic.target = (LIR *)target;
1726 branch2->generic.target = (LIR *)target;
1727 break;
1728 }
buzbee4d92e682010-07-29 15:24:14 -07001729 case OP_SGET_WIDE_VOLATILE:
1730 case OP_SPUT_WIDE_VOLATILE:
1731 genInterpSingleStep(cUnit, mir);
1732 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001733 default:
1734 return true;
1735 }
1736 return false;
1737}
1738
Ben Cheng7a2697d2010-06-07 13:44:23 -07001739/*
1740 * A typical example of inlined getter/setter from a monomorphic callsite:
1741 *
1742 * D/dalvikvm( 289): -------- dalvik offset: 0x0000 @ invoke-static (I)
1743 * D/dalvikvm( 289): -------- dalvik offset: 0x0000 @ sget-object (C) v0, ...
1744 * D/dalvikvm( 289): 0x4427fc22 (0002): ldr r0, [pc, #56]
1745 * D/dalvikvm( 289): 0x4427fc24 (0004): ldr r1, [r0, #0]
1746 * D/dalvikvm( 289): 0x4427fc26 (0006): str r1, [r5, #0]
1747 * D/dalvikvm( 289): 0x4427fc28 (0008): .align4
1748 * D/dalvikvm( 289): L0x0003:
1749 * D/dalvikvm( 289): -------- dalvik offset: 0x0003 @ move-result-object (I) v0
1750 *
1751 * Note the invoke-static and move-result-object with the (I) notation are
1752 * turned into no-op.
1753 */
Ben Chengba4fc8b2009-06-01 13:00:29 -07001754static bool handleFmt11x(CompilationUnit *cUnit, MIR *mir)
1755{
Dan Bornstein9a1f8162010-12-01 17:02:26 -08001756 Opcode dalvikOpcode = mir->dalvikInsn.opcode;
Bill Buzbee1465db52009-09-23 17:17:35 -07001757 RegLocation rlResult;
Dan Bornstein9a1f8162010-12-01 17:02:26 -08001758 switch (dalvikOpcode) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07001759 case OP_MOVE_EXCEPTION: {
1760 int offset = offsetof(InterpState, self);
1761 int exOffset = offsetof(Thread, exception);
Bill Buzbeec6f10662010-02-09 11:16:15 -08001762 int selfReg = dvmCompilerAllocTemp(cUnit);
1763 int resetReg = dvmCompilerAllocTemp(cUnit);
1764 RegLocation rlDest = dvmCompilerGetDest(cUnit, mir, 0);
1765 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Bill Buzbee1465db52009-09-23 17:17:35 -07001766 loadWordDisp(cUnit, rGLUE, offset, selfReg);
Bill Buzbeef9f33282009-11-22 12:45:30 -08001767 loadConstant(cUnit, resetReg, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07001768 loadWordDisp(cUnit, selfReg, exOffset, rlResult.lowReg);
Bill Buzbeef9f33282009-11-22 12:45:30 -08001769 storeWordDisp(cUnit, selfReg, exOffset, resetReg);
Bill Buzbee1465db52009-09-23 17:17:35 -07001770 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001771 break;
1772 }
1773 case OP_MOVE_RESULT:
1774 case OP_MOVE_RESULT_OBJECT: {
Ben Cheng7a2697d2010-06-07 13:44:23 -07001775 /* An inlined move result is effectively no-op */
1776 if (mir->OptimizationFlags & MIR_INLINED)
1777 break;
Bill Buzbeec6f10662010-02-09 11:16:15 -08001778 RegLocation rlDest = dvmCompilerGetDest(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07001779 RegLocation rlSrc = LOC_DALVIK_RETURN_VAL;
1780 rlSrc.fp = rlDest.fp;
1781 storeValue(cUnit, rlDest, rlSrc);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001782 break;
1783 }
1784 case OP_MOVE_RESULT_WIDE: {
Ben Cheng7a2697d2010-06-07 13:44:23 -07001785 /* An inlined move result is effectively no-op */
1786 if (mir->OptimizationFlags & MIR_INLINED)
1787 break;
Bill Buzbeec6f10662010-02-09 11:16:15 -08001788 RegLocation rlDest = dvmCompilerGetDestWide(cUnit, mir, 0, 1);
Bill Buzbee1465db52009-09-23 17:17:35 -07001789 RegLocation rlSrc = LOC_DALVIK_RETURN_VAL_WIDE;
1790 rlSrc.fp = rlDest.fp;
1791 storeValueWide(cUnit, rlDest, rlSrc);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001792 break;
1793 }
1794 case OP_RETURN_WIDE: {
Bill Buzbeec6f10662010-02-09 11:16:15 -08001795 RegLocation rlSrc = dvmCompilerGetSrcWide(cUnit, mir, 0, 1);
Bill Buzbee1465db52009-09-23 17:17:35 -07001796 RegLocation rlDest = LOC_DALVIK_RETURN_VAL_WIDE;
1797 rlDest.fp = rlSrc.fp;
1798 storeValueWide(cUnit, rlDest, rlSrc);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001799 genReturnCommon(cUnit,mir);
1800 break;
1801 }
1802 case OP_RETURN:
1803 case OP_RETURN_OBJECT: {
Bill Buzbeec6f10662010-02-09 11:16:15 -08001804 RegLocation rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07001805 RegLocation rlDest = LOC_DALVIK_RETURN_VAL;
1806 rlDest.fp = rlSrc.fp;
1807 storeValue(cUnit, rlDest, rlSrc);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001808 genReturnCommon(cUnit,mir);
1809 break;
1810 }
Bill Buzbee1465db52009-09-23 17:17:35 -07001811 case OP_MONITOR_EXIT:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001812 case OP_MONITOR_ENTER:
Bill Buzbeed0937ef2009-12-22 16:15:39 -08001813#if defined(WITH_DEADLOCK_PREDICTION) || defined(WITH_MONITOR_TRACKING)
Ben Cheng5d90c202009-11-22 23:31:11 -08001814 genMonitorPortable(cUnit, mir);
Bill Buzbee1465db52009-09-23 17:17:35 -07001815#else
Ben Cheng5d90c202009-11-22 23:31:11 -08001816 genMonitor(cUnit, mir);
Bill Buzbee1465db52009-09-23 17:17:35 -07001817#endif
Ben Chengba4fc8b2009-06-01 13:00:29 -07001818 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001819 case OP_THROW: {
1820 genInterpSingleStep(cUnit, mir);
1821 break;
1822 }
1823 default:
1824 return true;
1825 }
1826 return false;
1827}
1828
Bill Buzbeed45ba372009-06-15 17:00:57 -07001829static bool handleFmt12x(CompilationUnit *cUnit, MIR *mir)
1830{
Dan Bornstein9a1f8162010-12-01 17:02:26 -08001831 Opcode opcode = mir->dalvikInsn.opcode;
Bill Buzbee1465db52009-09-23 17:17:35 -07001832 RegLocation rlDest;
1833 RegLocation rlSrc;
1834 RegLocation rlResult;
Bill Buzbeed45ba372009-06-15 17:00:57 -07001835
Dan Bornstein9a1f8162010-12-01 17:02:26 -08001836 if ( (opcode >= OP_ADD_INT_2ADDR) && (opcode <= OP_REM_DOUBLE_2ADDR)) {
Ben Cheng5d90c202009-11-22 23:31:11 -08001837 return genArithOp( cUnit, mir );
Ben Chengba4fc8b2009-06-01 13:00:29 -07001838 }
1839
Bill Buzbee1465db52009-09-23 17:17:35 -07001840 if (mir->ssaRep->numUses == 2)
Bill Buzbeec6f10662010-02-09 11:16:15 -08001841 rlSrc = dvmCompilerGetSrcWide(cUnit, mir, 0, 1);
Bill Buzbee1465db52009-09-23 17:17:35 -07001842 else
Bill Buzbeec6f10662010-02-09 11:16:15 -08001843 rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07001844 if (mir->ssaRep->numDefs == 2)
Bill Buzbeec6f10662010-02-09 11:16:15 -08001845 rlDest = dvmCompilerGetDestWide(cUnit, mir, 0, 1);
Bill Buzbee1465db52009-09-23 17:17:35 -07001846 else
Bill Buzbeec6f10662010-02-09 11:16:15 -08001847 rlDest = dvmCompilerGetDest(cUnit, mir, 0);
Ben Chenge9695e52009-06-16 16:11:47 -07001848
Dan Bornstein9a1f8162010-12-01 17:02:26 -08001849 switch (opcode) {
Bill Buzbee1465db52009-09-23 17:17:35 -07001850 case OP_DOUBLE_TO_INT:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001851 case OP_INT_TO_FLOAT:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001852 case OP_FLOAT_TO_INT:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001853 case OP_DOUBLE_TO_FLOAT:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001854 case OP_FLOAT_TO_DOUBLE:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001855 case OP_INT_TO_DOUBLE:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001856 case OP_FLOAT_TO_LONG:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001857 case OP_LONG_TO_FLOAT:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001858 case OP_DOUBLE_TO_LONG:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001859 case OP_LONG_TO_DOUBLE:
Ben Cheng5d90c202009-11-22 23:31:11 -08001860 return genConversion(cUnit, mir);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001861 case OP_NEG_INT:
1862 case OP_NOT_INT:
Ben Cheng5d90c202009-11-22 23:31:11 -08001863 return genArithOpInt(cUnit, mir, rlDest, rlSrc, rlSrc);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001864 case OP_NEG_LONG:
1865 case OP_NOT_LONG:
Ben Cheng5d90c202009-11-22 23:31:11 -08001866 return genArithOpLong(cUnit, mir, rlDest, rlSrc, rlSrc);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001867 case OP_NEG_FLOAT:
Ben Cheng5d90c202009-11-22 23:31:11 -08001868 return genArithOpFloat(cUnit, mir, rlDest, rlSrc, rlSrc);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001869 case OP_NEG_DOUBLE:
Ben Cheng5d90c202009-11-22 23:31:11 -08001870 return genArithOpDouble(cUnit, mir, rlDest, rlSrc, rlSrc);
Bill Buzbee1465db52009-09-23 17:17:35 -07001871 case OP_MOVE_WIDE:
1872 storeValueWide(cUnit, rlDest, rlSrc);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001873 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07001874 case OP_INT_TO_LONG:
Bill Buzbeec6f10662010-02-09 11:16:15 -08001875 rlSrc = dvmCompilerUpdateLoc(cUnit, rlSrc);
1876 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Bill Buzbee964a7b02010-01-28 12:54:19 -08001877 //TUNING: shouldn't loadValueDirect already check for phys reg?
Bill Buzbee1465db52009-09-23 17:17:35 -07001878 if (rlSrc.location == kLocPhysReg) {
1879 genRegCopy(cUnit, rlResult.lowReg, rlSrc.lowReg);
1880 } else {
1881 loadValueDirect(cUnit, rlSrc, rlResult.lowReg);
1882 }
1883 opRegRegImm(cUnit, kOpAsr, rlResult.highReg,
1884 rlResult.lowReg, 31);
1885 storeValueWide(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001886 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07001887 case OP_LONG_TO_INT:
Bill Buzbeec6f10662010-02-09 11:16:15 -08001888 rlSrc = dvmCompilerUpdateLocWide(cUnit, rlSrc);
1889 rlSrc = dvmCompilerWideToNarrow(cUnit, rlSrc);
Bill Buzbee1465db52009-09-23 17:17:35 -07001890 // Intentional fallthrough
Ben Chengba4fc8b2009-06-01 13:00:29 -07001891 case OP_MOVE:
1892 case OP_MOVE_OBJECT:
Bill Buzbee1465db52009-09-23 17:17:35 -07001893 storeValue(cUnit, rlDest, rlSrc);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001894 break;
1895 case OP_INT_TO_BYTE:
Bill Buzbee1465db52009-09-23 17:17:35 -07001896 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
Bill Buzbeec6f10662010-02-09 11:16:15 -08001897 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Bill Buzbee1465db52009-09-23 17:17:35 -07001898 opRegReg(cUnit, kOp2Byte, rlResult.lowReg, rlSrc.lowReg);
1899 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001900 break;
1901 case OP_INT_TO_SHORT:
Bill Buzbee1465db52009-09-23 17:17:35 -07001902 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
Bill Buzbeec6f10662010-02-09 11:16:15 -08001903 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Bill Buzbee1465db52009-09-23 17:17:35 -07001904 opRegReg(cUnit, kOp2Short, rlResult.lowReg, rlSrc.lowReg);
1905 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001906 break;
1907 case OP_INT_TO_CHAR:
Bill Buzbee1465db52009-09-23 17:17:35 -07001908 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
Bill Buzbeec6f10662010-02-09 11:16:15 -08001909 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Bill Buzbee1465db52009-09-23 17:17:35 -07001910 opRegReg(cUnit, kOp2Char, rlResult.lowReg, rlSrc.lowReg);
1911 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001912 break;
1913 case OP_ARRAY_LENGTH: {
1914 int lenOffset = offsetof(ArrayObject, length);
Bill Buzbee1465db52009-09-23 17:17:35 -07001915 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
1916 genNullCheck(cUnit, rlSrc.sRegLow, rlSrc.lowReg,
1917 mir->offset, NULL);
Bill Buzbeec6f10662010-02-09 11:16:15 -08001918 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Bill Buzbee1465db52009-09-23 17:17:35 -07001919 loadWordDisp(cUnit, rlSrc.lowReg, lenOffset,
1920 rlResult.lowReg);
1921 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001922 break;
1923 }
1924 default:
1925 return true;
1926 }
1927 return false;
1928}
1929
1930static bool handleFmt21s(CompilationUnit *cUnit, MIR *mir)
1931{
Dan Bornstein9a1f8162010-12-01 17:02:26 -08001932 Opcode dalvikOpcode = mir->dalvikInsn.opcode;
Bill Buzbee1465db52009-09-23 17:17:35 -07001933 RegLocation rlDest;
1934 RegLocation rlResult;
1935 int BBBB = mir->dalvikInsn.vB;
Dan Bornstein9a1f8162010-12-01 17:02:26 -08001936 if (dalvikOpcode == OP_CONST_WIDE_16) {
Bill Buzbeec6f10662010-02-09 11:16:15 -08001937 rlDest = dvmCompilerGetDestWide(cUnit, mir, 0, 1);
1938 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Ben Chengbd1326d2010-04-02 15:04:53 -07001939 loadConstantNoClobber(cUnit, rlResult.lowReg, BBBB);
Bill Buzbee964a7b02010-01-28 12:54:19 -08001940 //TUNING: do high separately to avoid load dependency
Bill Buzbee1465db52009-09-23 17:17:35 -07001941 opRegRegImm(cUnit, kOpAsr, rlResult.highReg, rlResult.lowReg, 31);
1942 storeValueWide(cUnit, rlDest, rlResult);
Dan Bornstein9a1f8162010-12-01 17:02:26 -08001943 } else if (dalvikOpcode == OP_CONST_16) {
Bill Buzbeec6f10662010-02-09 11:16:15 -08001944 rlDest = dvmCompilerGetDest(cUnit, mir, 0);
1945 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kAnyReg, true);
Ben Chengbd1326d2010-04-02 15:04:53 -07001946 loadConstantNoClobber(cUnit, rlResult.lowReg, BBBB);
Bill Buzbee1465db52009-09-23 17:17:35 -07001947 storeValue(cUnit, rlDest, rlResult);
1948 } else
Ben Chengba4fc8b2009-06-01 13:00:29 -07001949 return true;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001950 return false;
1951}
1952
1953/* Compare agaist zero */
1954static bool handleFmt21t(CompilationUnit *cUnit, MIR *mir, BasicBlock *bb,
Bill Buzbee89efc3d2009-07-28 11:22:22 -07001955 ArmLIR *labelList)
Ben Chengba4fc8b2009-06-01 13:00:29 -07001956{
Dan Bornstein9a1f8162010-12-01 17:02:26 -08001957 Opcode dalvikOpcode = mir->dalvikInsn.opcode;
Bill Buzbee89efc3d2009-07-28 11:22:22 -07001958 ArmConditionCode cond;
Bill Buzbeec6f10662010-02-09 11:16:15 -08001959 RegLocation rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07001960 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
1961 opRegImm(cUnit, kOpCmp, rlSrc.lowReg, 0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001962
Bill Buzbee270c1d62009-08-13 16:58:07 -07001963//TUNING: break this out to allow use of Thumb2 CB[N]Z
Dan Bornstein9a1f8162010-12-01 17:02:26 -08001964 switch (dalvikOpcode) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07001965 case OP_IF_EQZ:
Bill Buzbee1465db52009-09-23 17:17:35 -07001966 cond = kArmCondEq;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001967 break;
1968 case OP_IF_NEZ:
Bill Buzbee1465db52009-09-23 17:17:35 -07001969 cond = kArmCondNe;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001970 break;
1971 case OP_IF_LTZ:
Bill Buzbee1465db52009-09-23 17:17:35 -07001972 cond = kArmCondLt;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001973 break;
1974 case OP_IF_GEZ:
Bill Buzbee1465db52009-09-23 17:17:35 -07001975 cond = kArmCondGe;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001976 break;
1977 case OP_IF_GTZ:
Bill Buzbee1465db52009-09-23 17:17:35 -07001978 cond = kArmCondGt;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001979 break;
1980 case OP_IF_LEZ:
Bill Buzbee1465db52009-09-23 17:17:35 -07001981 cond = kArmCondLe;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001982 break;
1983 default:
1984 cond = 0;
Dan Bornstein9a1f8162010-12-01 17:02:26 -08001985 LOGE("Unexpected opcode (%d) for Fmt21t\n", dalvikOpcode);
Bill Buzbeefc519dc2010-03-06 23:30:57 -08001986 dvmCompilerAbort(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001987 }
1988 genConditionalBranch(cUnit, cond, &labelList[bb->taken->id]);
1989 /* This mostly likely will be optimized away in a later phase */
1990 genUnconditionalBranch(cUnit, &labelList[bb->fallThrough->id]);
1991 return false;
1992}
1993
Elliott Hughesb4c05972010-02-24 16:36:18 -08001994static bool isPowerOfTwo(int x)
1995{
1996 return (x & (x - 1)) == 0;
1997}
1998
1999// Returns true if no more than two bits are set in 'x'.
2000static bool isPopCountLE2(unsigned int x)
2001{
2002 x &= x - 1;
2003 return (x & (x - 1)) == 0;
2004}
2005
2006// Returns the index of the lowest set bit in 'x'.
2007static int lowestSetBit(unsigned int x) {
2008 int bit_posn = 0;
2009 while ((x & 0xf) == 0) {
2010 bit_posn += 4;
2011 x >>= 4;
Bill Buzbee78cb0e22010-02-11 14:04:53 -08002012 }
Elliott Hughesb4c05972010-02-24 16:36:18 -08002013 while ((x & 1) == 0) {
2014 bit_posn++;
2015 x >>= 1;
2016 }
2017 return bit_posn;
2018}
2019
Elliott Hughes672511b2010-04-26 17:40:13 -07002020// Returns true if it added instructions to 'cUnit' to divide 'rlSrc' by 'lit'
2021// and store the result in 'rlDest'.
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002022static bool handleEasyDivide(CompilationUnit *cUnit, Opcode dalvikOpcode,
Elliott Hughes672511b2010-04-26 17:40:13 -07002023 RegLocation rlSrc, RegLocation rlDest, int lit)
2024{
2025 if (lit < 2 || !isPowerOfTwo(lit)) {
2026 return false;
2027 }
2028 int k = lowestSetBit(lit);
2029 if (k >= 30) {
2030 // Avoid special cases.
2031 return false;
2032 }
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002033 bool div = (dalvikOpcode == OP_DIV_INT_LIT8 || dalvikOpcode == OP_DIV_INT_LIT16);
Elliott Hughes672511b2010-04-26 17:40:13 -07002034 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
2035 RegLocation rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Elliott Hughes9c457022010-04-28 16:15:38 -07002036 if (div) {
2037 int tReg = dvmCompilerAllocTemp(cUnit);
2038 if (lit == 2) {
2039 // Division by 2 is by far the most common division by constant.
2040 opRegRegImm(cUnit, kOpLsr, tReg, rlSrc.lowReg, 32 - k);
2041 opRegRegReg(cUnit, kOpAdd, tReg, tReg, rlSrc.lowReg);
2042 opRegRegImm(cUnit, kOpAsr, rlResult.lowReg, tReg, k);
2043 } else {
2044 opRegRegImm(cUnit, kOpAsr, tReg, rlSrc.lowReg, 31);
2045 opRegRegImm(cUnit, kOpLsr, tReg, tReg, 32 - k);
2046 opRegRegReg(cUnit, kOpAdd, tReg, tReg, rlSrc.lowReg);
2047 opRegRegImm(cUnit, kOpAsr, rlResult.lowReg, tReg, k);
2048 }
Elliott Hughes672511b2010-04-26 17:40:13 -07002049 } else {
Elliott Hughes9c457022010-04-28 16:15:38 -07002050 int cReg = dvmCompilerAllocTemp(cUnit);
2051 loadConstant(cUnit, cReg, lit - 1);
2052 int tReg1 = dvmCompilerAllocTemp(cUnit);
2053 int tReg2 = dvmCompilerAllocTemp(cUnit);
2054 if (lit == 2) {
2055 opRegRegImm(cUnit, kOpLsr, tReg1, rlSrc.lowReg, 32 - k);
2056 opRegRegReg(cUnit, kOpAdd, tReg2, tReg1, rlSrc.lowReg);
2057 opRegRegReg(cUnit, kOpAnd, tReg2, tReg2, cReg);
2058 opRegRegReg(cUnit, kOpSub, rlResult.lowReg, tReg2, tReg1);
2059 } else {
2060 opRegRegImm(cUnit, kOpAsr, tReg1, rlSrc.lowReg, 31);
2061 opRegRegImm(cUnit, kOpLsr, tReg1, tReg1, 32 - k);
2062 opRegRegReg(cUnit, kOpAdd, tReg2, tReg1, rlSrc.lowReg);
2063 opRegRegReg(cUnit, kOpAnd, tReg2, tReg2, cReg);
2064 opRegRegReg(cUnit, kOpSub, rlResult.lowReg, tReg2, tReg1);
2065 }
Elliott Hughes672511b2010-04-26 17:40:13 -07002066 }
2067 storeValue(cUnit, rlDest, rlResult);
2068 return true;
2069}
2070
Elliott Hughesb4c05972010-02-24 16:36:18 -08002071// Returns true if it added instructions to 'cUnit' to multiply 'rlSrc' by 'lit'
2072// and store the result in 'rlDest'.
2073static bool handleEasyMultiply(CompilationUnit *cUnit,
2074 RegLocation rlSrc, RegLocation rlDest, int lit)
2075{
2076 // Can we simplify this multiplication?
2077 bool powerOfTwo = false;
2078 bool popCountLE2 = false;
2079 bool powerOfTwoMinusOne = false;
2080 if (lit < 2) {
2081 // Avoid special cases.
2082 return false;
2083 } else if (isPowerOfTwo(lit)) {
2084 powerOfTwo = true;
2085 } else if (isPopCountLE2(lit)) {
2086 popCountLE2 = true;
2087 } else if (isPowerOfTwo(lit + 1)) {
2088 powerOfTwoMinusOne = true;
2089 } else {
2090 return false;
2091 }
2092 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
2093 RegLocation rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
2094 if (powerOfTwo) {
2095 // Shift.
2096 opRegRegImm(cUnit, kOpLsl, rlResult.lowReg, rlSrc.lowReg,
2097 lowestSetBit(lit));
2098 } else if (popCountLE2) {
2099 // Shift and add and shift.
2100 int firstBit = lowestSetBit(lit);
2101 int secondBit = lowestSetBit(lit ^ (1 << firstBit));
2102 genMultiplyByTwoBitMultiplier(cUnit, rlSrc, rlResult, lit,
2103 firstBit, secondBit);
2104 } else {
2105 // Reverse subtract: (src << (shift + 1)) - src.
2106 assert(powerOfTwoMinusOne);
2107 // TODO: rsb dst, src, src lsl#lowestSetBit(lit + 1)
2108 int tReg = dvmCompilerAllocTemp(cUnit);
2109 opRegRegImm(cUnit, kOpLsl, tReg, rlSrc.lowReg, lowestSetBit(lit + 1));
2110 opRegRegReg(cUnit, kOpSub, rlResult.lowReg, tReg, rlSrc.lowReg);
2111 }
2112 storeValue(cUnit, rlDest, rlResult);
2113 return true;
Bill Buzbee78cb0e22010-02-11 14:04:53 -08002114}
2115
Ben Chengba4fc8b2009-06-01 13:00:29 -07002116static bool handleFmt22b_Fmt22s(CompilationUnit *cUnit, MIR *mir)
2117{
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002118 Opcode dalvikOpcode = mir->dalvikInsn.opcode;
Bill Buzbeec6f10662010-02-09 11:16:15 -08002119 RegLocation rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
2120 RegLocation rlDest = dvmCompilerGetDest(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07002121 RegLocation rlResult;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002122 int lit = mir->dalvikInsn.vC;
Ben Cheng4f489172009-09-27 17:08:35 -07002123 OpKind op = 0; /* Make gcc happy */
Bill Buzbee1465db52009-09-23 17:17:35 -07002124 int shiftOp = false;
2125 bool isDiv = false;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002126
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002127 switch (dalvikOpcode) {
Bill Buzbee1465db52009-09-23 17:17:35 -07002128 case OP_RSUB_INT_LIT8:
2129 case OP_RSUB_INT: {
2130 int tReg;
2131 //TUNING: add support for use of Arm rsub op
2132 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
Bill Buzbeec6f10662010-02-09 11:16:15 -08002133 tReg = dvmCompilerAllocTemp(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07002134 loadConstant(cUnit, tReg, lit);
Bill Buzbeec6f10662010-02-09 11:16:15 -08002135 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Bill Buzbee1465db52009-09-23 17:17:35 -07002136 opRegRegReg(cUnit, kOpSub, rlResult.lowReg,
2137 tReg, rlSrc.lowReg);
2138 storeValue(cUnit, rlDest, rlResult);
2139 return false;
2140 break;
2141 }
2142
Ben Chengba4fc8b2009-06-01 13:00:29 -07002143 case OP_ADD_INT_LIT8:
2144 case OP_ADD_INT_LIT16:
Bill Buzbee1465db52009-09-23 17:17:35 -07002145 op = kOpAdd;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002146 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002147 case OP_MUL_INT_LIT8:
Bill Buzbee78cb0e22010-02-11 14:04:53 -08002148 case OP_MUL_INT_LIT16: {
Elliott Hughesb4c05972010-02-24 16:36:18 -08002149 if (handleEasyMultiply(cUnit, rlSrc, rlDest, lit)) {
2150 return false;
Bill Buzbee78cb0e22010-02-11 14:04:53 -08002151 }
Elliott Hughesb4c05972010-02-24 16:36:18 -08002152 op = kOpMul;
Bill Buzbee1465db52009-09-23 17:17:35 -07002153 break;
Bill Buzbee78cb0e22010-02-11 14:04:53 -08002154 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07002155 case OP_AND_INT_LIT8:
2156 case OP_AND_INT_LIT16:
Bill Buzbee1465db52009-09-23 17:17:35 -07002157 op = kOpAnd;
2158 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002159 case OP_OR_INT_LIT8:
2160 case OP_OR_INT_LIT16:
Bill Buzbee1465db52009-09-23 17:17:35 -07002161 op = kOpOr;
2162 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002163 case OP_XOR_INT_LIT8:
2164 case OP_XOR_INT_LIT16:
Bill Buzbee1465db52009-09-23 17:17:35 -07002165 op = kOpXor;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002166 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002167 case OP_SHL_INT_LIT8:
Bill Buzbee0e605272009-12-01 14:28:05 -08002168 lit &= 31;
Bill Buzbee1465db52009-09-23 17:17:35 -07002169 shiftOp = true;
2170 op = kOpLsl;
2171 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002172 case OP_SHR_INT_LIT8:
Bill Buzbee0e605272009-12-01 14:28:05 -08002173 lit &= 31;
Bill Buzbee1465db52009-09-23 17:17:35 -07002174 shiftOp = true;
2175 op = kOpAsr;
2176 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002177 case OP_USHR_INT_LIT8:
Bill Buzbee0e605272009-12-01 14:28:05 -08002178 lit &= 31;
Bill Buzbee1465db52009-09-23 17:17:35 -07002179 shiftOp = true;
2180 op = kOpLsr;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002181 break;
2182
2183 case OP_DIV_INT_LIT8:
2184 case OP_DIV_INT_LIT16:
Ben Chengba4fc8b2009-06-01 13:00:29 -07002185 case OP_REM_INT_LIT8:
2186 case OP_REM_INT_LIT16:
2187 if (lit == 0) {
2188 /* Let the interpreter deal with div by 0 */
2189 genInterpSingleStep(cUnit, mir);
2190 return false;
2191 }
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002192 if (handleEasyDivide(cUnit, dalvikOpcode, rlSrc, rlDest, lit)) {
Elliott Hughes672511b2010-04-26 17:40:13 -07002193 return false;
2194 }
Bill Buzbeec6f10662010-02-09 11:16:15 -08002195 dvmCompilerFlushAllRegs(cUnit); /* Everything to home location */
Bill Buzbee1465db52009-09-23 17:17:35 -07002196 loadValueDirectFixed(cUnit, rlSrc, r0);
Bill Buzbeec6f10662010-02-09 11:16:15 -08002197 dvmCompilerClobber(cUnit, r0);
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002198 if ((dalvikOpcode == OP_DIV_INT_LIT8) ||
2199 (dalvikOpcode == OP_DIV_INT_LIT16)) {
Ben Chengbd1326d2010-04-02 15:04:53 -07002200 LOAD_FUNC_ADDR(cUnit, r2, (int)__aeabi_idiv);
Bill Buzbee1465db52009-09-23 17:17:35 -07002201 isDiv = true;
2202 } else {
Ben Chengbd1326d2010-04-02 15:04:53 -07002203 LOAD_FUNC_ADDR(cUnit, r2, (int)__aeabi_idivmod);
Bill Buzbee1465db52009-09-23 17:17:35 -07002204 isDiv = false;
2205 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07002206 loadConstant(cUnit, r1, lit);
Bill Buzbee1465db52009-09-23 17:17:35 -07002207 opReg(cUnit, kOpBlx, r2);
Elliott Hughes6a555132010-02-25 15:41:42 -08002208 dvmCompilerClobberCallRegs(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07002209 if (isDiv)
Bill Buzbeec6f10662010-02-09 11:16:15 -08002210 rlResult = dvmCompilerGetReturn(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07002211 else
Bill Buzbeec6f10662010-02-09 11:16:15 -08002212 rlResult = dvmCompilerGetReturnAlt(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07002213 storeValue(cUnit, rlDest, rlResult);
2214 return false;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002215 break;
2216 default:
2217 return true;
2218 }
Bill Buzbee1465db52009-09-23 17:17:35 -07002219 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
Bill Buzbeec6f10662010-02-09 11:16:15 -08002220 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Bill Buzbee1465db52009-09-23 17:17:35 -07002221 // Avoid shifts by literal 0 - no support in Thumb. Change to copy
2222 if (shiftOp && (lit == 0)) {
2223 genRegCopy(cUnit, rlResult.lowReg, rlSrc.lowReg);
2224 } else {
2225 opRegRegImm(cUnit, op, rlResult.lowReg, rlSrc.lowReg, lit);
2226 }
2227 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002228 return false;
2229}
2230
jeffhao71eee1f2011-01-04 14:18:54 -08002231static bool handleFmt22c_Fmt52c(CompilationUnit *cUnit, MIR *mir)
Ben Chengba4fc8b2009-06-01 13:00:29 -07002232{
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002233 Opcode dalvikOpcode = mir->dalvikInsn.opcode;
buzbee4d92e682010-07-29 15:24:14 -07002234 int fieldOffset = -1;
buzbeeecf8f6e2010-07-20 14:53:42 -07002235 bool isVolatile = false;
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002236 switch (dalvikOpcode) {
buzbee4d92e682010-07-29 15:24:14 -07002237 /*
2238 * Wide volatiles currently handled via single step.
2239 * Add them here if generating in-line code.
2240 * case OP_IGET_WIDE_VOLATILE:
2241 * case OP_IPUT_WIDE_VOLATILE:
2242 */
2243 case OP_IGET:
2244 case OP_IGET_VOLATILE:
jeffhao71eee1f2011-01-04 14:18:54 -08002245 case OP_IGET_JUMBO:
buzbee4d92e682010-07-29 15:24:14 -07002246 case OP_IGET_WIDE:
jeffhao71eee1f2011-01-04 14:18:54 -08002247 case OP_IGET_WIDE_JUMBO:
buzbee4d92e682010-07-29 15:24:14 -07002248 case OP_IGET_OBJECT:
2249 case OP_IGET_OBJECT_VOLATILE:
jeffhao71eee1f2011-01-04 14:18:54 -08002250 case OP_IGET_OBJECT_JUMBO:
buzbee4d92e682010-07-29 15:24:14 -07002251 case OP_IGET_BOOLEAN:
jeffhao71eee1f2011-01-04 14:18:54 -08002252 case OP_IGET_BOOLEAN_JUMBO:
buzbee4d92e682010-07-29 15:24:14 -07002253 case OP_IGET_BYTE:
jeffhao71eee1f2011-01-04 14:18:54 -08002254 case OP_IGET_BYTE_JUMBO:
buzbee4d92e682010-07-29 15:24:14 -07002255 case OP_IGET_CHAR:
jeffhao71eee1f2011-01-04 14:18:54 -08002256 case OP_IGET_CHAR_JUMBO:
buzbee4d92e682010-07-29 15:24:14 -07002257 case OP_IGET_SHORT:
jeffhao71eee1f2011-01-04 14:18:54 -08002258 case OP_IGET_SHORT_JUMBO:
buzbee4d92e682010-07-29 15:24:14 -07002259 case OP_IPUT:
2260 case OP_IPUT_VOLATILE:
jeffhao71eee1f2011-01-04 14:18:54 -08002261 case OP_IPUT_JUMBO:
buzbee4d92e682010-07-29 15:24:14 -07002262 case OP_IPUT_WIDE:
jeffhao71eee1f2011-01-04 14:18:54 -08002263 case OP_IPUT_WIDE_JUMBO:
buzbee4d92e682010-07-29 15:24:14 -07002264 case OP_IPUT_OBJECT:
2265 case OP_IPUT_OBJECT_VOLATILE:
jeffhao71eee1f2011-01-04 14:18:54 -08002266 case OP_IPUT_OBJECT_JUMBO:
buzbee4d92e682010-07-29 15:24:14 -07002267 case OP_IPUT_BOOLEAN:
jeffhao71eee1f2011-01-04 14:18:54 -08002268 case OP_IPUT_BOOLEAN_JUMBO:
buzbee4d92e682010-07-29 15:24:14 -07002269 case OP_IPUT_BYTE:
jeffhao71eee1f2011-01-04 14:18:54 -08002270 case OP_IPUT_BYTE_JUMBO:
buzbee4d92e682010-07-29 15:24:14 -07002271 case OP_IPUT_CHAR:
jeffhao71eee1f2011-01-04 14:18:54 -08002272 case OP_IPUT_CHAR_JUMBO:
2273 case OP_IPUT_SHORT:
2274 case OP_IPUT_SHORT_JUMBO: {
Ben Cheng7a2697d2010-06-07 13:44:23 -07002275 const Method *method = (mir->OptimizationFlags & MIR_CALLEE) ?
2276 mir->meta.calleeMethod : cUnit->method;
buzbee4d92e682010-07-29 15:24:14 -07002277 Field *fieldPtr =
Ben Cheng7a2697d2010-06-07 13:44:23 -07002278 method->clazz->pDvmDex->pResFields[mir->dalvikInsn.vC];
Ben Chengba4fc8b2009-06-01 13:00:29 -07002279
buzbee4d92e682010-07-29 15:24:14 -07002280 if (fieldPtr == NULL) {
2281 LOGE("Unexpected null instance field");
2282 dvmAbort();
2283 }
2284 isVolatile = dvmIsVolatileField(fieldPtr);
2285 fieldOffset = ((InstField *)fieldPtr)->byteOffset;
2286 break;
Ben Chengdd6e8702010-05-07 13:05:47 -07002287 }
buzbee4d92e682010-07-29 15:24:14 -07002288 default:
2289 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002290 }
buzbee4d92e682010-07-29 15:24:14 -07002291
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002292 switch (dalvikOpcode) {
jeffhao71eee1f2011-01-04 14:18:54 -08002293 case OP_NEW_ARRAY:
2294 case OP_NEW_ARRAY_JUMBO: {
Bill Buzbee1465db52009-09-23 17:17:35 -07002295 // Generates a call - use explicit registers
Bill Buzbeec6f10662010-02-09 11:16:15 -08002296 RegLocation rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
2297 RegLocation rlDest = dvmCompilerGetDest(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07002298 RegLocation rlResult;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002299 void *classPtr = (void*)
2300 (cUnit->method->clazz->pDvmDex->pResClasses[mir->dalvikInsn.vC]);
Ben Chengdd6e8702010-05-07 13:05:47 -07002301
2302 if (classPtr == NULL) {
2303 LOGE("Unexpected null class");
2304 dvmAbort();
2305 }
2306
Bill Buzbeec6f10662010-02-09 11:16:15 -08002307 dvmCompilerFlushAllRegs(cUnit); /* Everything to home location */
Bill Buzbee1465db52009-09-23 17:17:35 -07002308 genExportPC(cUnit, mir);
2309 loadValueDirectFixed(cUnit, rlSrc, r1); /* Len */
Ben Chengba4fc8b2009-06-01 13:00:29 -07002310 loadConstant(cUnit, r0, (int) classPtr );
Ben Chengbd1326d2010-04-02 15:04:53 -07002311 LOAD_FUNC_ADDR(cUnit, r3, (int)dvmAllocArrayByClass);
Ben Cheng4f489172009-09-27 17:08:35 -07002312 /*
2313 * "len < 0": bail to the interpreter to re-execute the
2314 * instruction
2315 */
Carl Shapiroe3c01da2010-05-20 22:54:18 -07002316 genRegImmCheck(cUnit, kArmCondMi, r1, 0, mir->offset, NULL);
Bill Buzbee270c1d62009-08-13 16:58:07 -07002317 loadConstant(cUnit, r2, ALLOC_DONT_TRACK);
Bill Buzbee1465db52009-09-23 17:17:35 -07002318 opReg(cUnit, kOpBlx, r3);
Elliott Hughes6a555132010-02-25 15:41:42 -08002319 dvmCompilerClobberCallRegs(cUnit);
Ben Cheng4f489172009-09-27 17:08:35 -07002320 /* generate a branch over if allocation is successful */
buzbee8f8109a2010-08-31 10:16:35 -07002321 ArmLIR *branchOver = genCmpImmBranch(cUnit, kArmCondNe, r0, 0);
Ben Cheng4f489172009-09-27 17:08:35 -07002322 /*
2323 * OOM exception needs to be thrown here and cannot re-execute
2324 */
2325 loadConstant(cUnit, r0,
2326 (int) (cUnit->method->insns + mir->offset));
2327 genDispatchToHandler(cUnit, TEMPLATE_THROW_EXCEPTION_COMMON);
2328 /* noreturn */
2329
Bill Buzbee1465db52009-09-23 17:17:35 -07002330 ArmLIR *target = newLIR0(cUnit, kArmPseudoTargetLabel);
Ben Cheng4f489172009-09-27 17:08:35 -07002331 target->defMask = ENCODE_ALL;
2332 branchOver->generic.target = (LIR *) target;
Bill Buzbeec6f10662010-02-09 11:16:15 -08002333 rlResult = dvmCompilerGetReturn(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07002334 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002335 break;
2336 }
jeffhao71eee1f2011-01-04 14:18:54 -08002337 case OP_INSTANCE_OF:
2338 case OP_INSTANCE_OF_JUMBO: {
Bill Buzbee1465db52009-09-23 17:17:35 -07002339 // May generate a call - use explicit registers
Bill Buzbeec6f10662010-02-09 11:16:15 -08002340 RegLocation rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
2341 RegLocation rlDest = dvmCompilerGetDest(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07002342 RegLocation rlResult;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002343 ClassObject *classPtr =
2344 (cUnit->method->clazz->pDvmDex->pResClasses[mir->dalvikInsn.vC]);
Bill Buzbee480e6782010-01-27 15:43:08 -08002345 /*
2346 * Note: It is possible that classPtr is NULL at this point,
2347 * even though this instruction has been successfully interpreted.
2348 * If the previous interpretation had a null source, the
2349 * interpreter would not have bothered to resolve the clazz.
2350 * Bail out to the interpreter in this case, and log it
2351 * so that we can tell if it happens frequently.
2352 */
2353 if (classPtr == NULL) {
2354 LOGD("null clazz in OP_INSTANCE_OF, single-stepping");
2355 genInterpSingleStep(cUnit, mir);
2356 break;
2357 }
Bill Buzbeec6f10662010-02-09 11:16:15 -08002358 dvmCompilerFlushAllRegs(cUnit); /* Everything to home location */
Bill Buzbee1465db52009-09-23 17:17:35 -07002359 loadValueDirectFixed(cUnit, rlSrc, r0); /* Ref */
Ben Chengba4fc8b2009-06-01 13:00:29 -07002360 loadConstant(cUnit, r2, (int) classPtr );
Ben Cheng752c7942009-06-22 10:50:07 -07002361 /* When taken r0 has NULL which can be used for store directly */
buzbee8f8109a2010-08-31 10:16:35 -07002362 ArmLIR *branch1 = genCmpImmBranch(cUnit, kArmCondEq, r0, 0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002363 /* r1 now contains object->clazz */
Bill Buzbee270c1d62009-08-13 16:58:07 -07002364 loadWordDisp(cUnit, r0, offsetof(Object, clazz), r1);
Bill Buzbee1465db52009-09-23 17:17:35 -07002365 /* r1 now contains object->clazz */
Ben Chengbd1326d2010-04-02 15:04:53 -07002366 LOAD_FUNC_ADDR(cUnit, r3, (int)dvmInstanceofNonTrivial);
Ben Cheng752c7942009-06-22 10:50:07 -07002367 loadConstant(cUnit, r0, 1); /* Assume true */
Bill Buzbee1465db52009-09-23 17:17:35 -07002368 opRegReg(cUnit, kOpCmp, r1, r2);
2369 ArmLIR *branch2 = opCondBranch(cUnit, kArmCondEq);
2370 genRegCopy(cUnit, r0, r1);
2371 genRegCopy(cUnit, r1, r2);
2372 opReg(cUnit, kOpBlx, r3);
Elliott Hughes6a555132010-02-25 15:41:42 -08002373 dvmCompilerClobberCallRegs(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002374 /* branch target here */
Bill Buzbee1465db52009-09-23 17:17:35 -07002375 ArmLIR *target = newLIR0(cUnit, kArmPseudoTargetLabel);
Ben Chengd7d426a2009-09-22 11:23:36 -07002376 target->defMask = ENCODE_ALL;
Bill Buzbeec6f10662010-02-09 11:16:15 -08002377 rlResult = dvmCompilerGetReturn(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07002378 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002379 branch1->generic.target = (LIR *)target;
2380 branch2->generic.target = (LIR *)target;
2381 break;
2382 }
2383 case OP_IGET_WIDE:
jeffhao71eee1f2011-01-04 14:18:54 -08002384 case OP_IGET_WIDE_JUMBO:
Ben Chengba4fc8b2009-06-01 13:00:29 -07002385 genIGetWide(cUnit, mir, fieldOffset);
2386 break;
buzbeeecf8f6e2010-07-20 14:53:42 -07002387 case OP_IGET_VOLATILE:
2388 case OP_IGET_OBJECT_VOLATILE:
2389 isVolatile = true;
2390 // NOTE: intentional fallthrough
Ben Chengba4fc8b2009-06-01 13:00:29 -07002391 case OP_IGET:
jeffhao71eee1f2011-01-04 14:18:54 -08002392 case OP_IGET_JUMBO:
Ben Chengba4fc8b2009-06-01 13:00:29 -07002393 case OP_IGET_OBJECT:
jeffhao71eee1f2011-01-04 14:18:54 -08002394 case OP_IGET_OBJECT_JUMBO:
Ben Chengba4fc8b2009-06-01 13:00:29 -07002395 case OP_IGET_BOOLEAN:
jeffhao71eee1f2011-01-04 14:18:54 -08002396 case OP_IGET_BOOLEAN_JUMBO:
Ben Chengba4fc8b2009-06-01 13:00:29 -07002397 case OP_IGET_BYTE:
jeffhao71eee1f2011-01-04 14:18:54 -08002398 case OP_IGET_BYTE_JUMBO:
Ben Chengba4fc8b2009-06-01 13:00:29 -07002399 case OP_IGET_CHAR:
jeffhao71eee1f2011-01-04 14:18:54 -08002400 case OP_IGET_CHAR_JUMBO:
Ben Chengba4fc8b2009-06-01 13:00:29 -07002401 case OP_IGET_SHORT:
jeffhao71eee1f2011-01-04 14:18:54 -08002402 case OP_IGET_SHORT_JUMBO:
buzbee3272e2f2010-09-09 14:07:01 -07002403 genIGet(cUnit, mir, kWord, fieldOffset, isVolatile);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002404 break;
2405 case OP_IPUT_WIDE:
jeffhao71eee1f2011-01-04 14:18:54 -08002406 case OP_IPUT_WIDE_JUMBO:
Ben Chengba4fc8b2009-06-01 13:00:29 -07002407 genIPutWide(cUnit, mir, fieldOffset);
2408 break;
2409 case OP_IPUT:
jeffhao71eee1f2011-01-04 14:18:54 -08002410 case OP_IPUT_JUMBO:
buzbee3272e2f2010-09-09 14:07:01 -07002411 case OP_IPUT_BOOLEAN:
jeffhao71eee1f2011-01-04 14:18:54 -08002412 case OP_IPUT_BOOLEAN_JUMBO:
2413 case OP_IPUT_BYTE:
2414 case OP_IPUT_BYTE_JUMBO:
2415 case OP_IPUT_CHAR:
2416 case OP_IPUT_CHAR_JUMBO:
2417 case OP_IPUT_SHORT:
2418 case OP_IPUT_SHORT_JUMBO:
buzbeeecf8f6e2010-07-20 14:53:42 -07002419 genIPut(cUnit, mir, kWord, fieldOffset, false, isVolatile);
buzbee919eb062010-07-12 12:59:22 -07002420 break;
buzbee4d92e682010-07-29 15:24:14 -07002421 case OP_IPUT_VOLATILE:
buzbeeecf8f6e2010-07-20 14:53:42 -07002422 case OP_IPUT_OBJECT_VOLATILE:
2423 isVolatile = true;
2424 // NOTE: intentional fallthrough
Ben Chengba4fc8b2009-06-01 13:00:29 -07002425 case OP_IPUT_OBJECT:
jeffhao71eee1f2011-01-04 14:18:54 -08002426 case OP_IPUT_OBJECT_JUMBO:
buzbeeecf8f6e2010-07-20 14:53:42 -07002427 genIPut(cUnit, mir, kWord, fieldOffset, true, isVolatile);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002428 break;
Bill Buzbeeb16344a2010-03-15 17:19:12 -07002429 case OP_IGET_WIDE_VOLATILE:
2430 case OP_IPUT_WIDE_VOLATILE:
Bill Buzbeeb16344a2010-03-15 17:19:12 -07002431 genInterpSingleStep(cUnit, mir);
2432 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002433 default:
2434 return true;
2435 }
2436 return false;
2437}
2438
2439static bool handleFmt22cs(CompilationUnit *cUnit, MIR *mir)
2440{
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002441 Opcode dalvikOpcode = mir->dalvikInsn.opcode;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002442 int fieldOffset = mir->dalvikInsn.vC;
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002443 switch (dalvikOpcode) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07002444 case OP_IGET_QUICK:
2445 case OP_IGET_OBJECT_QUICK:
buzbeeecf8f6e2010-07-20 14:53:42 -07002446 genIGet(cUnit, mir, kWord, fieldOffset, false);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002447 break;
2448 case OP_IPUT_QUICK:
buzbeeecf8f6e2010-07-20 14:53:42 -07002449 genIPut(cUnit, mir, kWord, fieldOffset, false, false);
buzbee919eb062010-07-12 12:59:22 -07002450 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002451 case OP_IPUT_OBJECT_QUICK:
buzbeeecf8f6e2010-07-20 14:53:42 -07002452 genIPut(cUnit, mir, kWord, fieldOffset, true, false);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002453 break;
2454 case OP_IGET_WIDE_QUICK:
2455 genIGetWide(cUnit, mir, fieldOffset);
2456 break;
2457 case OP_IPUT_WIDE_QUICK:
2458 genIPutWide(cUnit, mir, fieldOffset);
2459 break;
2460 default:
2461 return true;
2462 }
2463 return false;
2464
2465}
2466
2467/* Compare agaist zero */
2468static bool handleFmt22t(CompilationUnit *cUnit, MIR *mir, BasicBlock *bb,
Bill Buzbee89efc3d2009-07-28 11:22:22 -07002469 ArmLIR *labelList)
Ben Chengba4fc8b2009-06-01 13:00:29 -07002470{
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002471 Opcode dalvikOpcode = mir->dalvikInsn.opcode;
Bill Buzbee89efc3d2009-07-28 11:22:22 -07002472 ArmConditionCode cond;
Bill Buzbeec6f10662010-02-09 11:16:15 -08002473 RegLocation rlSrc1 = dvmCompilerGetSrc(cUnit, mir, 0);
2474 RegLocation rlSrc2 = dvmCompilerGetSrc(cUnit, mir, 1);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002475
Bill Buzbee1465db52009-09-23 17:17:35 -07002476 rlSrc1 = loadValue(cUnit, rlSrc1, kCoreReg);
2477 rlSrc2 = loadValue(cUnit, rlSrc2, kCoreReg);
2478 opRegReg(cUnit, kOpCmp, rlSrc1.lowReg, rlSrc2.lowReg);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002479
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002480 switch (dalvikOpcode) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07002481 case OP_IF_EQ:
Bill Buzbee1465db52009-09-23 17:17:35 -07002482 cond = kArmCondEq;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002483 break;
2484 case OP_IF_NE:
Bill Buzbee1465db52009-09-23 17:17:35 -07002485 cond = kArmCondNe;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002486 break;
2487 case OP_IF_LT:
Bill Buzbee1465db52009-09-23 17:17:35 -07002488 cond = kArmCondLt;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002489 break;
2490 case OP_IF_GE:
Bill Buzbee1465db52009-09-23 17:17:35 -07002491 cond = kArmCondGe;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002492 break;
2493 case OP_IF_GT:
Bill Buzbee1465db52009-09-23 17:17:35 -07002494 cond = kArmCondGt;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002495 break;
2496 case OP_IF_LE:
Bill Buzbee1465db52009-09-23 17:17:35 -07002497 cond = kArmCondLe;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002498 break;
2499 default:
2500 cond = 0;
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002501 LOGE("Unexpected opcode (%d) for Fmt22t\n", dalvikOpcode);
Bill Buzbeefc519dc2010-03-06 23:30:57 -08002502 dvmCompilerAbort(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002503 }
2504 genConditionalBranch(cUnit, cond, &labelList[bb->taken->id]);
2505 /* This mostly likely will be optimized away in a later phase */
2506 genUnconditionalBranch(cUnit, &labelList[bb->fallThrough->id]);
2507 return false;
2508}
2509
2510static bool handleFmt22x_Fmt32x(CompilationUnit *cUnit, MIR *mir)
2511{
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002512 Opcode opcode = mir->dalvikInsn.opcode;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002513
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002514 switch (opcode) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07002515 case OP_MOVE_16:
2516 case OP_MOVE_OBJECT_16:
2517 case OP_MOVE_FROM16:
Ben Chenge9695e52009-06-16 16:11:47 -07002518 case OP_MOVE_OBJECT_FROM16: {
Bill Buzbeec6f10662010-02-09 11:16:15 -08002519 storeValue(cUnit, dvmCompilerGetDest(cUnit, mir, 0),
2520 dvmCompilerGetSrc(cUnit, mir, 0));
Ben Chengba4fc8b2009-06-01 13:00:29 -07002521 break;
Ben Chenge9695e52009-06-16 16:11:47 -07002522 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07002523 case OP_MOVE_WIDE_16:
Ben Chenge9695e52009-06-16 16:11:47 -07002524 case OP_MOVE_WIDE_FROM16: {
Bill Buzbeec6f10662010-02-09 11:16:15 -08002525 storeValueWide(cUnit, dvmCompilerGetDestWide(cUnit, mir, 0, 1),
2526 dvmCompilerGetSrcWide(cUnit, mir, 0, 1));
Ben Chengba4fc8b2009-06-01 13:00:29 -07002527 break;
Ben Chenge9695e52009-06-16 16:11:47 -07002528 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07002529 default:
2530 return true;
2531 }
2532 return false;
2533}
2534
2535static bool handleFmt23x(CompilationUnit *cUnit, MIR *mir)
2536{
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002537 Opcode opcode = mir->dalvikInsn.opcode;
Bill Buzbee1465db52009-09-23 17:17:35 -07002538 RegLocation rlSrc1;
2539 RegLocation rlSrc2;
2540 RegLocation rlDest;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002541
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002542 if ( (opcode >= OP_ADD_INT) && (opcode <= OP_REM_DOUBLE)) {
Ben Cheng5d90c202009-11-22 23:31:11 -08002543 return genArithOp( cUnit, mir );
Ben Chengba4fc8b2009-06-01 13:00:29 -07002544 }
2545
Bill Buzbee1465db52009-09-23 17:17:35 -07002546 /* APUTs have 3 sources and no targets */
2547 if (mir->ssaRep->numDefs == 0) {
2548 if (mir->ssaRep->numUses == 3) {
Bill Buzbeec6f10662010-02-09 11:16:15 -08002549 rlDest = dvmCompilerGetSrc(cUnit, mir, 0);
2550 rlSrc1 = dvmCompilerGetSrc(cUnit, mir, 1);
2551 rlSrc2 = dvmCompilerGetSrc(cUnit, mir, 2);
Bill Buzbee1465db52009-09-23 17:17:35 -07002552 } else {
2553 assert(mir->ssaRep->numUses == 4);
Bill Buzbeec6f10662010-02-09 11:16:15 -08002554 rlDest = dvmCompilerGetSrcWide(cUnit, mir, 0, 1);
2555 rlSrc1 = dvmCompilerGetSrc(cUnit, mir, 2);
2556 rlSrc2 = dvmCompilerGetSrc(cUnit, mir, 3);
Bill Buzbee1465db52009-09-23 17:17:35 -07002557 }
2558 } else {
2559 /* Two sources and 1 dest. Deduce the operand sizes */
2560 if (mir->ssaRep->numUses == 4) {
Bill Buzbeec6f10662010-02-09 11:16:15 -08002561 rlSrc1 = dvmCompilerGetSrcWide(cUnit, mir, 0, 1);
2562 rlSrc2 = dvmCompilerGetSrcWide(cUnit, mir, 2, 3);
Bill Buzbee1465db52009-09-23 17:17:35 -07002563 } else {
2564 assert(mir->ssaRep->numUses == 2);
Bill Buzbeec6f10662010-02-09 11:16:15 -08002565 rlSrc1 = dvmCompilerGetSrc(cUnit, mir, 0);
2566 rlSrc2 = dvmCompilerGetSrc(cUnit, mir, 1);
Bill Buzbee1465db52009-09-23 17:17:35 -07002567 }
2568 if (mir->ssaRep->numDefs == 2) {
Bill Buzbeec6f10662010-02-09 11:16:15 -08002569 rlDest = dvmCompilerGetDestWide(cUnit, mir, 0, 1);
Bill Buzbee1465db52009-09-23 17:17:35 -07002570 } else {
2571 assert(mir->ssaRep->numDefs == 1);
Bill Buzbeec6f10662010-02-09 11:16:15 -08002572 rlDest = dvmCompilerGetDest(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07002573 }
2574 }
2575
2576
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002577 switch (opcode) {
Bill Buzbeed45ba372009-06-15 17:00:57 -07002578 case OP_CMPL_FLOAT:
2579 case OP_CMPG_FLOAT:
2580 case OP_CMPL_DOUBLE:
2581 case OP_CMPG_DOUBLE:
Ben Cheng5d90c202009-11-22 23:31:11 -08002582 return genCmpFP(cUnit, mir, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002583 case OP_CMP_LONG:
Bill Buzbee1465db52009-09-23 17:17:35 -07002584 genCmpLong(cUnit, mir, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002585 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002586 case OP_AGET_WIDE:
Bill Buzbee1465db52009-09-23 17:17:35 -07002587 genArrayGet(cUnit, mir, kLong, rlSrc1, rlSrc2, rlDest, 3);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002588 break;
2589 case OP_AGET:
2590 case OP_AGET_OBJECT:
Bill Buzbee1465db52009-09-23 17:17:35 -07002591 genArrayGet(cUnit, mir, kWord, rlSrc1, rlSrc2, rlDest, 2);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002592 break;
2593 case OP_AGET_BOOLEAN:
Bill Buzbee1465db52009-09-23 17:17:35 -07002594 genArrayGet(cUnit, mir, kUnsignedByte, rlSrc1, rlSrc2, rlDest, 0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002595 break;
2596 case OP_AGET_BYTE:
Bill Buzbee1465db52009-09-23 17:17:35 -07002597 genArrayGet(cUnit, mir, kSignedByte, rlSrc1, rlSrc2, rlDest, 0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002598 break;
2599 case OP_AGET_CHAR:
Bill Buzbee1465db52009-09-23 17:17:35 -07002600 genArrayGet(cUnit, mir, kUnsignedHalf, rlSrc1, rlSrc2, rlDest, 1);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002601 break;
2602 case OP_AGET_SHORT:
Bill Buzbee1465db52009-09-23 17:17:35 -07002603 genArrayGet(cUnit, mir, kSignedHalf, rlSrc1, rlSrc2, rlDest, 1);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002604 break;
2605 case OP_APUT_WIDE:
Bill Buzbee1465db52009-09-23 17:17:35 -07002606 genArrayPut(cUnit, mir, kLong, rlSrc1, rlSrc2, rlDest, 3);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002607 break;
2608 case OP_APUT:
Bill Buzbee1465db52009-09-23 17:17:35 -07002609 genArrayPut(cUnit, mir, kWord, rlSrc1, rlSrc2, rlDest, 2);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002610 break;
Bill Buzbeebe6534f2010-03-12 16:01:35 -08002611 case OP_APUT_OBJECT:
2612 genArrayObjectPut(cUnit, mir, rlSrc1, rlSrc2, rlDest, 2);
2613 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002614 case OP_APUT_SHORT:
2615 case OP_APUT_CHAR:
Bill Buzbee1465db52009-09-23 17:17:35 -07002616 genArrayPut(cUnit, mir, kUnsignedHalf, rlSrc1, rlSrc2, rlDest, 1);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002617 break;
2618 case OP_APUT_BYTE:
2619 case OP_APUT_BOOLEAN:
Bill Buzbee1465db52009-09-23 17:17:35 -07002620 genArrayPut(cUnit, mir, kUnsignedByte, rlSrc1, rlSrc2, rlDest, 0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002621 break;
2622 default:
2623 return true;
2624 }
2625 return false;
2626}
2627
Ben Cheng6c10a972009-10-29 14:39:18 -07002628/*
2629 * Find the matching case.
2630 *
2631 * return values:
2632 * r0 (low 32-bit): pc of the chaining cell corresponding to the resolved case,
2633 * including default which is placed at MIN(size, MAX_CHAINED_SWITCH_CASES).
2634 * r1 (high 32-bit): the branch offset of the matching case (only for indexes
2635 * above MAX_CHAINED_SWITCH_CASES).
2636 *
2637 * Instructions around the call are:
2638 *
2639 * mov r2, pc
2640 * blx &findPackedSwitchIndex
2641 * mov pc, r0
2642 * .align4
Bill Buzbeebd047242010-05-13 13:02:53 -07002643 * chaining cell for case 0 [12 bytes]
2644 * chaining cell for case 1 [12 bytes]
Ben Cheng6c10a972009-10-29 14:39:18 -07002645 * :
Bill Buzbeebd047242010-05-13 13:02:53 -07002646 * chaining cell for case MIN(size, MAX_CHAINED_SWITCH_CASES)-1 [12 bytes]
Ben Cheng6c10a972009-10-29 14:39:18 -07002647 * chaining cell for case default [8 bytes]
2648 * noChain exit
2649 */
Ben Chengbd1326d2010-04-02 15:04:53 -07002650static s8 findPackedSwitchIndex(const u2* switchData, int testVal, int pc)
Ben Cheng6c10a972009-10-29 14:39:18 -07002651{
2652 int size;
2653 int firstKey;
2654 const int *entries;
2655 int index;
2656 int jumpIndex;
2657 int caseDPCOffset = 0;
2658 /* In Thumb mode pc is 4 ahead of the "mov r2, pc" instruction */
2659 int chainingPC = (pc + 4) & ~3;
2660
2661 /*
2662 * Packed switch data format:
2663 * ushort ident = 0x0100 magic value
2664 * ushort size number of entries in the table
2665 * int first_key first (and lowest) switch case value
2666 * int targets[size] branch targets, relative to switch opcode
2667 *
2668 * Total size is (4+size*2) 16-bit code units.
2669 */
2670 size = switchData[1];
2671 assert(size > 0);
2672
2673 firstKey = switchData[2];
2674 firstKey |= switchData[3] << 16;
2675
2676
2677 /* The entries are guaranteed to be aligned on a 32-bit boundary;
2678 * we can treat them as a native int array.
2679 */
2680 entries = (const int*) &switchData[4];
2681 assert(((u4)entries & 0x3) == 0);
2682
2683 index = testVal - firstKey;
2684
2685 /* Jump to the default cell */
2686 if (index < 0 || index >= size) {
2687 jumpIndex = MIN(size, MAX_CHAINED_SWITCH_CASES);
2688 /* Jump to the non-chaining exit point */
2689 } else if (index >= MAX_CHAINED_SWITCH_CASES) {
2690 jumpIndex = MAX_CHAINED_SWITCH_CASES + 1;
2691 caseDPCOffset = entries[index];
2692 /* Jump to the inline chaining cell */
2693 } else {
2694 jumpIndex = index;
2695 }
2696
Bill Buzbeebd047242010-05-13 13:02:53 -07002697 chainingPC += jumpIndex * CHAIN_CELL_NORMAL_SIZE;
Ben Cheng6c10a972009-10-29 14:39:18 -07002698 return (((s8) caseDPCOffset) << 32) | (u8) chainingPC;
2699}
2700
2701/* See comments for findPackedSwitchIndex */
Ben Chengbd1326d2010-04-02 15:04:53 -07002702static s8 findSparseSwitchIndex(const u2* switchData, int testVal, int pc)
Ben Cheng6c10a972009-10-29 14:39:18 -07002703{
2704 int size;
2705 const int *keys;
2706 const int *entries;
2707 int chainingPC = (pc + 4) & ~3;
2708 int i;
2709
2710 /*
2711 * Sparse switch data format:
2712 * ushort ident = 0x0200 magic value
2713 * ushort size number of entries in the table; > 0
2714 * int keys[size] keys, sorted low-to-high; 32-bit aligned
2715 * int targets[size] branch targets, relative to switch opcode
2716 *
2717 * Total size is (2+size*4) 16-bit code units.
2718 */
2719
2720 size = switchData[1];
2721 assert(size > 0);
2722
2723 /* The keys are guaranteed to be aligned on a 32-bit boundary;
2724 * we can treat them as a native int array.
2725 */
2726 keys = (const int*) &switchData[2];
2727 assert(((u4)keys & 0x3) == 0);
2728
2729 /* The entries are guaranteed to be aligned on a 32-bit boundary;
2730 * we can treat them as a native int array.
2731 */
2732 entries = keys + size;
2733 assert(((u4)entries & 0x3) == 0);
2734
2735 /*
2736 * Run through the list of keys, which are guaranteed to
2737 * be sorted low-to-high.
2738 *
2739 * Most tables have 3-4 entries. Few have more than 10. A binary
2740 * search here is probably not useful.
2741 */
2742 for (i = 0; i < size; i++) {
2743 int k = keys[i];
2744 if (k == testVal) {
2745 /* MAX_CHAINED_SWITCH_CASES + 1 is the start of the overflow case */
2746 int jumpIndex = (i < MAX_CHAINED_SWITCH_CASES) ?
2747 i : MAX_CHAINED_SWITCH_CASES + 1;
Bill Buzbeebd047242010-05-13 13:02:53 -07002748 chainingPC += jumpIndex * CHAIN_CELL_NORMAL_SIZE;
Ben Cheng6c10a972009-10-29 14:39:18 -07002749 return (((s8) entries[i]) << 32) | (u8) chainingPC;
2750 } else if (k > testVal) {
2751 break;
2752 }
2753 }
Bill Buzbeebd047242010-05-13 13:02:53 -07002754 return chainingPC + MIN(size, MAX_CHAINED_SWITCH_CASES) *
2755 CHAIN_CELL_NORMAL_SIZE;
Ben Cheng6c10a972009-10-29 14:39:18 -07002756}
2757
Ben Chengba4fc8b2009-06-01 13:00:29 -07002758static bool handleFmt31t(CompilationUnit *cUnit, MIR *mir)
2759{
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002760 Opcode dalvikOpcode = mir->dalvikInsn.opcode;
2761 switch (dalvikOpcode) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07002762 case OP_FILL_ARRAY_DATA: {
Bill Buzbeec6f10662010-02-09 11:16:15 -08002763 RegLocation rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07002764 // Making a call - use explicit registers
Bill Buzbeec6f10662010-02-09 11:16:15 -08002765 dvmCompilerFlushAllRegs(cUnit); /* Everything to home location */
Bill Buzbee1465db52009-09-23 17:17:35 -07002766 genExportPC(cUnit, mir);
2767 loadValueDirectFixed(cUnit, rlSrc, r0);
Ben Chengbd1326d2010-04-02 15:04:53 -07002768 LOAD_FUNC_ADDR(cUnit, r2, (int)dvmInterpHandleFillArrayData);
Ben Cheng6c10a972009-10-29 14:39:18 -07002769 loadConstant(cUnit, r1,
2770 (int) (cUnit->method->insns + mir->offset + mir->dalvikInsn.vB));
Bill Buzbee1465db52009-09-23 17:17:35 -07002771 opReg(cUnit, kOpBlx, r2);
Elliott Hughes6a555132010-02-25 15:41:42 -08002772 dvmCompilerClobberCallRegs(cUnit);
Bill Buzbeefd023aa2009-11-02 09:23:49 -08002773 /* generate a branch over if successful */
buzbee8f8109a2010-08-31 10:16:35 -07002774 ArmLIR *branchOver = genCmpImmBranch(cUnit, kArmCondNe, r0, 0);
Bill Buzbeefd023aa2009-11-02 09:23:49 -08002775 loadConstant(cUnit, r0,
2776 (int) (cUnit->method->insns + mir->offset));
2777 genDispatchToHandler(cUnit, TEMPLATE_THROW_EXCEPTION_COMMON);
2778 ArmLIR *target = newLIR0(cUnit, kArmPseudoTargetLabel);
2779 target->defMask = ENCODE_ALL;
2780 branchOver->generic.target = (LIR *) target;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002781 break;
2782 }
2783 /*
Ben Cheng6c10a972009-10-29 14:39:18 -07002784 * Compute the goto target of up to
2785 * MIN(switchSize, MAX_CHAINED_SWITCH_CASES) + 1 chaining cells.
2786 * See the comment before findPackedSwitchIndex for the code layout.
Ben Chengba4fc8b2009-06-01 13:00:29 -07002787 */
2788 case OP_PACKED_SWITCH:
2789 case OP_SPARSE_SWITCH: {
Bill Buzbeec6f10662010-02-09 11:16:15 -08002790 RegLocation rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
2791 dvmCompilerFlushAllRegs(cUnit); /* Everything to home location */
Bill Buzbee1465db52009-09-23 17:17:35 -07002792 loadValueDirectFixed(cUnit, rlSrc, r1);
Bill Buzbeec6f10662010-02-09 11:16:15 -08002793 dvmCompilerLockAllTemps(cUnit);
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002794 if (dalvikOpcode == OP_PACKED_SWITCH) {
Ben Chengbd1326d2010-04-02 15:04:53 -07002795 LOAD_FUNC_ADDR(cUnit, r4PC, (int)findPackedSwitchIndex);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002796 } else {
Ben Chengbd1326d2010-04-02 15:04:53 -07002797 LOAD_FUNC_ADDR(cUnit, r4PC, (int)findSparseSwitchIndex);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002798 }
Ben Cheng6c10a972009-10-29 14:39:18 -07002799 /* r0 <- Addr of the switch data */
2800 loadConstant(cUnit, r0,
2801 (int) (cUnit->method->insns + mir->offset + mir->dalvikInsn.vB));
2802 /* r2 <- pc of the instruction following the blx */
2803 opRegReg(cUnit, kOpMov, r2, rpc);
Bill Buzbee1465db52009-09-23 17:17:35 -07002804 opReg(cUnit, kOpBlx, r4PC);
Elliott Hughes6a555132010-02-25 15:41:42 -08002805 dvmCompilerClobberCallRegs(cUnit);
Ben Cheng6c10a972009-10-29 14:39:18 -07002806 /* pc <- computed goto target */
2807 opRegReg(cUnit, kOpMov, rpc, r0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002808 break;
2809 }
2810 default:
2811 return true;
2812 }
2813 return false;
2814}
2815
Ben Cheng7a2697d2010-06-07 13:44:23 -07002816/*
2817 * See the example of predicted inlining listed before the
2818 * genValidationForPredictedInline function. The function here takes care the
2819 * branch over at 0x4858de78 and the misprediction target at 0x4858de7a.
2820 */
2821static void genLandingPadForMispredictedCallee(CompilationUnit *cUnit, MIR *mir,
2822 BasicBlock *bb,
2823 ArmLIR *labelList)
2824{
2825 BasicBlock *fallThrough = bb->fallThrough;
2826
2827 /* Bypass the move-result block if there is one */
2828 if (fallThrough->firstMIRInsn) {
2829 assert(fallThrough->firstMIRInsn->OptimizationFlags & MIR_INLINED_PRED);
2830 fallThrough = fallThrough->fallThrough;
2831 }
2832 /* Generate a branch over if the predicted inlining is correct */
2833 genUnconditionalBranch(cUnit, &labelList[fallThrough->id]);
2834
2835 /* Reset the register state */
2836 dvmCompilerResetRegPool(cUnit);
2837 dvmCompilerClobberAllRegs(cUnit);
2838 dvmCompilerResetNullCheck(cUnit);
2839
2840 /* Target for the slow invoke path */
2841 ArmLIR *target = newLIR0(cUnit, kArmPseudoTargetLabel);
2842 target->defMask = ENCODE_ALL;
2843 /* Hook up the target to the verification branch */
2844 mir->meta.callsiteInfo->misPredBranchOver->target = (LIR *) target;
2845}
2846
jeffhao71eee1f2011-01-04 14:18:54 -08002847static bool handleFmt35c_3rc_5rc(CompilationUnit *cUnit, MIR *mir,
2848 BasicBlock *bb, ArmLIR *labelList)
Ben Chengba4fc8b2009-06-01 13:00:29 -07002849{
Bill Buzbee9bc3df32009-07-30 10:52:29 -07002850 ArmLIR *retChainingCell = NULL;
Bill Buzbee89efc3d2009-07-28 11:22:22 -07002851 ArmLIR *pcrLabel = NULL;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002852
Ben Cheng7a2697d2010-06-07 13:44:23 -07002853 /* An invoke with the MIR_INLINED is effectively a no-op */
2854 if (mir->OptimizationFlags & MIR_INLINED)
2855 return false;
2856
Bill Buzbeef4ce16f2009-07-28 13:28:25 -07002857 if (bb->fallThrough != NULL)
2858 retChainingCell = &labelList[bb->fallThrough->id];
2859
Ben Chengba4fc8b2009-06-01 13:00:29 -07002860 DecodedInstruction *dInsn = &mir->dalvikInsn;
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002861 switch (mir->dalvikInsn.opcode) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07002862 /*
2863 * calleeMethod = this->clazz->vtable[
2864 * method->clazz->pDvmDex->pResMethods[BBBB]->methodIndex
2865 * ]
2866 */
2867 case OP_INVOKE_VIRTUAL:
jeffhao71eee1f2011-01-04 14:18:54 -08002868 case OP_INVOKE_VIRTUAL_RANGE:
2869 case OP_INVOKE_VIRTUAL_JUMBO: {
Bill Buzbee89efc3d2009-07-28 11:22:22 -07002870 ArmLIR *predChainingCell = &labelList[bb->taken->id];
Ben Chengba4fc8b2009-06-01 13:00:29 -07002871 int methodIndex =
2872 cUnit->method->clazz->pDvmDex->pResMethods[dInsn->vB]->
2873 methodIndex;
2874
Ben Cheng7a2697d2010-06-07 13:44:23 -07002875 /*
2876 * If the invoke has non-null misPredBranchOver, we need to generate
2877 * the non-inlined version of the invoke here to handle the
2878 * mispredicted case.
2879 */
2880 if (mir->meta.callsiteInfo->misPredBranchOver) {
2881 genLandingPadForMispredictedCallee(cUnit, mir, bb, labelList);
2882 }
2883
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002884 if (mir->dalvikInsn.opcode == OP_INVOKE_VIRTUAL)
Ben Chengba4fc8b2009-06-01 13:00:29 -07002885 genProcessArgsNoRange(cUnit, mir, dInsn, &pcrLabel);
2886 else
2887 genProcessArgsRange(cUnit, mir, dInsn, &pcrLabel);
2888
Ben Cheng38329f52009-07-07 14:19:20 -07002889 genInvokeVirtualCommon(cUnit, mir, methodIndex,
2890 retChainingCell,
2891 predChainingCell,
2892 pcrLabel);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002893 break;
2894 }
2895 /*
2896 * calleeMethod = method->clazz->super->vtable[method->clazz->pDvmDex
2897 * ->pResMethods[BBBB]->methodIndex]
2898 */
Ben Chengba4fc8b2009-06-01 13:00:29 -07002899 case OP_INVOKE_SUPER:
jeffhao71eee1f2011-01-04 14:18:54 -08002900 case OP_INVOKE_SUPER_RANGE:
2901 case OP_INVOKE_SUPER_JUMBO: {
Ben Cheng7a2697d2010-06-07 13:44:23 -07002902 /* Grab the method ptr directly from what the interpreter sees */
2903 const Method *calleeMethod = mir->meta.callsiteInfo->method;
2904 assert(calleeMethod == cUnit->method->clazz->super->vtable[
2905 cUnit->method->clazz->pDvmDex->
2906 pResMethods[dInsn->vB]->methodIndex]);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002907
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002908 if (mir->dalvikInsn.opcode == OP_INVOKE_SUPER)
Ben Chengba4fc8b2009-06-01 13:00:29 -07002909 genProcessArgsNoRange(cUnit, mir, dInsn, &pcrLabel);
2910 else
2911 genProcessArgsRange(cUnit, mir, dInsn, &pcrLabel);
2912
2913 /* r0 = calleeMethod */
2914 loadConstant(cUnit, r0, (int) calleeMethod);
2915
Ben Cheng38329f52009-07-07 14:19:20 -07002916 genInvokeSingletonCommon(cUnit, mir, bb, labelList, pcrLabel,
2917 calleeMethod);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002918 break;
2919 }
2920 /* calleeMethod = method->clazz->pDvmDex->pResMethods[BBBB] */
2921 case OP_INVOKE_DIRECT:
jeffhao71eee1f2011-01-04 14:18:54 -08002922 case OP_INVOKE_DIRECT_RANGE:
2923 case OP_INVOKE_DIRECT_JUMBO: {
Ben Cheng7a2697d2010-06-07 13:44:23 -07002924 /* Grab the method ptr directly from what the interpreter sees */
2925 const Method *calleeMethod = mir->meta.callsiteInfo->method;
2926 assert(calleeMethod ==
2927 cUnit->method->clazz->pDvmDex->pResMethods[dInsn->vB]);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002928
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002929 if (mir->dalvikInsn.opcode == OP_INVOKE_DIRECT)
Ben Chengba4fc8b2009-06-01 13:00:29 -07002930 genProcessArgsNoRange(cUnit, mir, dInsn, &pcrLabel);
2931 else
2932 genProcessArgsRange(cUnit, mir, dInsn, &pcrLabel);
2933
2934 /* r0 = calleeMethod */
2935 loadConstant(cUnit, r0, (int) calleeMethod);
2936
Ben Cheng38329f52009-07-07 14:19:20 -07002937 genInvokeSingletonCommon(cUnit, mir, bb, labelList, pcrLabel,
2938 calleeMethod);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002939 break;
2940 }
2941 /* calleeMethod = method->clazz->pDvmDex->pResMethods[BBBB] */
2942 case OP_INVOKE_STATIC:
jeffhao71eee1f2011-01-04 14:18:54 -08002943 case OP_INVOKE_STATIC_RANGE:
2944 case OP_INVOKE_STATIC_JUMBO: {
Ben Cheng7a2697d2010-06-07 13:44:23 -07002945 /* Grab the method ptr directly from what the interpreter sees */
2946 const Method *calleeMethod = mir->meta.callsiteInfo->method;
2947 assert(calleeMethod ==
2948 cUnit->method->clazz->pDvmDex->pResMethods[dInsn->vB]);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002949
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002950 if (mir->dalvikInsn.opcode == OP_INVOKE_STATIC)
Ben Chengba4fc8b2009-06-01 13:00:29 -07002951 genProcessArgsNoRange(cUnit, mir, dInsn,
2952 NULL /* no null check */);
2953 else
2954 genProcessArgsRange(cUnit, mir, dInsn,
2955 NULL /* no null check */);
2956
2957 /* r0 = calleeMethod */
2958 loadConstant(cUnit, r0, (int) calleeMethod);
2959
Ben Cheng38329f52009-07-07 14:19:20 -07002960 genInvokeSingletonCommon(cUnit, mir, bb, labelList, pcrLabel,
2961 calleeMethod);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002962 break;
2963 }
Ben Cheng09e50c92010-05-02 10:45:32 -07002964 /*
Ben Chengba4fc8b2009-06-01 13:00:29 -07002965 * calleeMethod = dvmFindInterfaceMethodInCache(this->clazz,
2966 * BBBB, method, method->clazz->pDvmDex)
Ben Cheng38329f52009-07-07 14:19:20 -07002967 *
Ben Cheng09e50c92010-05-02 10:45:32 -07002968 * The following is an example of generated code for
2969 * "invoke-interface v0"
Ben Cheng38329f52009-07-07 14:19:20 -07002970 *
Ben Cheng09e50c92010-05-02 10:45:32 -07002971 * -------- dalvik offset: 0x0008 @ invoke-interface v0
2972 * 0x47357e36 : ldr r0, [r5, #0] --+
2973 * 0x47357e38 : sub r7,r5,#24 |
2974 * 0x47357e3c : cmp r0, #0 | genProcessArgsNoRange
2975 * 0x47357e3e : beq 0x47357e82 |
2976 * 0x47357e40 : stmia r7, <r0> --+
2977 * 0x47357e42 : ldr r4, [pc, #120] --> r4 <- dalvikPC of this invoke
2978 * 0x47357e44 : add r1, pc, #64 --> r1 <- &retChainingCell
2979 * 0x47357e46 : add r2, pc, #72 --> r2 <- &predictedChainingCell
2980 * 0x47357e48 : blx_1 0x47348190 --+ TEMPLATE_INVOKE_METHOD_
2981 * 0x47357e4a : blx_2 see above --+ PREDICTED_CHAIN
2982 * 0x47357e4c : b 0x47357e90 --> off to the predicted chain
2983 * 0x47357e4e : b 0x47357e82 --> punt to the interpreter
2984 * 0x47357e50 : mov r8, r1 --+
2985 * 0x47357e52 : mov r9, r2 |
2986 * 0x47357e54 : ldr r2, [pc, #96] |
2987 * 0x47357e56 : mov r10, r3 |
2988 * 0x47357e58 : movs r0, r3 | dvmFindInterfaceMethodInCache
2989 * 0x47357e5a : ldr r3, [pc, #88] |
2990 * 0x47357e5c : ldr r7, [pc, #80] |
2991 * 0x47357e5e : mov r1, #1452 |
2992 * 0x47357e62 : blx r7 --+
2993 * 0x47357e64 : cmp r0, #0 --> calleeMethod == NULL?
2994 * 0x47357e66 : bne 0x47357e6e --> branch over the throw if !r0
2995 * 0x47357e68 : ldr r0, [pc, #80] --> load Dalvik PC of the invoke
2996 * 0x47357e6a : blx_1 0x47348494 --+ TEMPLATE_THROW_EXCEPTION_
2997 * 0x47357e6c : blx_2 see above --+ COMMON
2998 * 0x47357e6e : mov r1, r8 --> r1 <- &retChainingCell
2999 * 0x47357e70 : cmp r1, #0 --> compare against 0
3000 * 0x47357e72 : bgt 0x47357e7c --> >=0? don't rechain
Ben Chengaf5aa1f2011-01-04 15:37:04 -08003001 * 0x47357e74 : ldr r7, [pc, #off] --+
Ben Cheng09e50c92010-05-02 10:45:32 -07003002 * 0x47357e76 : mov r2, r9 | dvmJitToPatchPredictedChain
3003 * 0x47357e78 : mov r3, r10 |
3004 * 0x47357e7a : blx r7 --+
3005 * 0x47357e7c : add r1, pc, #8 --> r1 <- &retChainingCell
3006 * 0x47357e7e : blx_1 0x4734809c --+ TEMPLATE_INVOKE_METHOD_NO_OPT
3007 * 0x47357e80 : blx_2 see above --+
3008 * -------- reconstruct dalvik PC : 0x425719dc @ +0x0008
3009 * 0x47357e82 : ldr r0, [pc, #56]
Ben Cheng38329f52009-07-07 14:19:20 -07003010 * Exception_Handling:
Ben Cheng09e50c92010-05-02 10:45:32 -07003011 * 0x47357e84 : ldr r1, [r6, #92]
3012 * 0x47357e86 : blx r1
3013 * 0x47357e88 : .align4
3014 * -------- chaining cell (hot): 0x000b
3015 * 0x47357e88 : ldr r0, [r6, #104]
3016 * 0x47357e8a : blx r0
3017 * 0x47357e8c : data 0x19e2(6626)
3018 * 0x47357e8e : data 0x4257(16983)
3019 * 0x47357e90 : .align4
Ben Cheng38329f52009-07-07 14:19:20 -07003020 * -------- chaining cell (predicted)
Ben Cheng09e50c92010-05-02 10:45:32 -07003021 * 0x47357e90 : data 0xe7fe(59390) --> will be patched into bx
3022 * 0x47357e92 : data 0x0000(0)
3023 * 0x47357e94 : data 0x0000(0) --> class
3024 * 0x47357e96 : data 0x0000(0)
3025 * 0x47357e98 : data 0x0000(0) --> method
3026 * 0x47357e9a : data 0x0000(0)
3027 * 0x47357e9c : data 0x0000(0) --> rechain count
3028 * 0x47357e9e : data 0x0000(0)
3029 * -------- end of chaining cells (0x006c)
3030 * 0x47357eb0 : .word (0xad03e369)
3031 * 0x47357eb4 : .word (0x28a90)
3032 * 0x47357eb8 : .word (0x41a63394)
3033 * 0x47357ebc : .word (0x425719dc)
Ben Chengba4fc8b2009-06-01 13:00:29 -07003034 */
3035 case OP_INVOKE_INTERFACE:
jeffhao71eee1f2011-01-04 14:18:54 -08003036 case OP_INVOKE_INTERFACE_RANGE:
3037 case OP_INVOKE_INTERFACE_JUMBO: {
Bill Buzbee89efc3d2009-07-28 11:22:22 -07003038 ArmLIR *predChainingCell = &labelList[bb->taken->id];
Ben Chengba4fc8b2009-06-01 13:00:29 -07003039
Ben Cheng7a2697d2010-06-07 13:44:23 -07003040 /*
3041 * If the invoke has non-null misPredBranchOver, we need to generate
3042 * the non-inlined version of the invoke here to handle the
3043 * mispredicted case.
3044 */
3045 if (mir->meta.callsiteInfo->misPredBranchOver) {
3046 genLandingPadForMispredictedCallee(cUnit, mir, bb, labelList);
3047 }
Bill Buzbee1465db52009-09-23 17:17:35 -07003048
Dan Bornstein9a1f8162010-12-01 17:02:26 -08003049 if (mir->dalvikInsn.opcode == OP_INVOKE_INTERFACE)
Ben Chengba4fc8b2009-06-01 13:00:29 -07003050 genProcessArgsNoRange(cUnit, mir, dInsn, &pcrLabel);
3051 else
3052 genProcessArgsRange(cUnit, mir, dInsn, &pcrLabel);
3053
Ben Cheng38329f52009-07-07 14:19:20 -07003054 /* "this" is already left in r0 by genProcessArgs* */
3055
3056 /* r4PC = dalvikCallsite */
3057 loadConstant(cUnit, r4PC,
3058 (int) (cUnit->method->insns + mir->offset));
3059
3060 /* r1 = &retChainingCell */
Bill Buzbee270c1d62009-08-13 16:58:07 -07003061 ArmLIR *addrRetChain =
Bill Buzbee1465db52009-09-23 17:17:35 -07003062 opRegRegImm(cUnit, kOpAdd, r1, rpc, 0);
Ben Cheng38329f52009-07-07 14:19:20 -07003063 addrRetChain->generic.target = (LIR *) retChainingCell;
3064
3065 /* r2 = &predictedChainingCell */
Bill Buzbee89efc3d2009-07-28 11:22:22 -07003066 ArmLIR *predictedChainingCell =
Bill Buzbee1465db52009-09-23 17:17:35 -07003067 opRegRegImm(cUnit, kOpAdd, r2, rpc, 0);
Ben Cheng38329f52009-07-07 14:19:20 -07003068 predictedChainingCell->generic.target = (LIR *) predChainingCell;
3069
buzbee18fba342011-01-19 15:31:15 -08003070 genDispatchToHandler(cUnit, gDvmJit.methodTraceSupport ?
3071 TEMPLATE_INVOKE_METHOD_PREDICTED_CHAIN_PROF :
3072 TEMPLATE_INVOKE_METHOD_PREDICTED_CHAIN);
Ben Cheng38329f52009-07-07 14:19:20 -07003073
3074 /* return through lr - jump to the chaining cell */
3075 genUnconditionalBranch(cUnit, predChainingCell);
3076
3077 /*
3078 * null-check on "this" may have been eliminated, but we still need
3079 * a PC-reconstruction label for stack overflow bailout.
3080 */
3081 if (pcrLabel == NULL) {
3082 int dPC = (int) (cUnit->method->insns + mir->offset);
Carl Shapirofc75f3e2010-12-07 11:43:38 -08003083 pcrLabel = (ArmLIR *) dvmCompilerNew(sizeof(ArmLIR), true);
Dan Bornstein9a1f8162010-12-01 17:02:26 -08003084 pcrLabel->opcode = kArmPseudoPCReconstructionCell;
Ben Cheng38329f52009-07-07 14:19:20 -07003085 pcrLabel->operands[0] = dPC;
3086 pcrLabel->operands[1] = mir->offset;
3087 /* Insert the place holder to the growable list */
Ben Cheng00603072010-10-28 11:13:58 -07003088 dvmInsertGrowableList(&cUnit->pcReconstructionList,
3089 (intptr_t) pcrLabel);
Ben Cheng38329f52009-07-07 14:19:20 -07003090 }
3091
3092 /* return through lr+2 - punt to the interpreter */
3093 genUnconditionalBranch(cUnit, pcrLabel);
3094
3095 /*
3096 * return through lr+4 - fully resolve the callee method.
3097 * r1 <- count
3098 * r2 <- &predictedChainCell
3099 * r3 <- this->class
3100 * r4 <- dPC
3101 * r7 <- this->class->vtable
3102 */
3103
3104 /* Save count, &predictedChainCell, and class to high regs first */
Bill Buzbee1465db52009-09-23 17:17:35 -07003105 genRegCopy(cUnit, r8, r1);
3106 genRegCopy(cUnit, r9, r2);
3107 genRegCopy(cUnit, r10, r3);
Ben Cheng38329f52009-07-07 14:19:20 -07003108
Ben Chengba4fc8b2009-06-01 13:00:29 -07003109 /* r0 now contains this->clazz */
Bill Buzbee1465db52009-09-23 17:17:35 -07003110 genRegCopy(cUnit, r0, r3);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003111
3112 /* r1 = BBBB */
3113 loadConstant(cUnit, r1, dInsn->vB);
3114
3115 /* r2 = method (caller) */
3116 loadConstant(cUnit, r2, (int) cUnit->method);
3117
3118 /* r3 = pDvmDex */
3119 loadConstant(cUnit, r3, (int) cUnit->method->clazz->pDvmDex);
3120
Ben Chengbd1326d2010-04-02 15:04:53 -07003121 LOAD_FUNC_ADDR(cUnit, r7,
3122 (intptr_t) dvmFindInterfaceMethodInCache);
Bill Buzbee1465db52009-09-23 17:17:35 -07003123 opReg(cUnit, kOpBlx, r7);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003124 /* r0 = calleeMethod (returned from dvmFindInterfaceMethodInCache */
3125
Ben Cheng09e50c92010-05-02 10:45:32 -07003126 dvmCompilerClobberCallRegs(cUnit);
3127 /* generate a branch over if the interface method is resolved */
buzbee8f8109a2010-08-31 10:16:35 -07003128 ArmLIR *branchOver = genCmpImmBranch(cUnit, kArmCondNe, r0, 0);
Ben Cheng09e50c92010-05-02 10:45:32 -07003129 /*
3130 * calleeMethod == NULL -> throw
3131 */
3132 loadConstant(cUnit, r0,
3133 (int) (cUnit->method->insns + mir->offset));
3134 genDispatchToHandler(cUnit, TEMPLATE_THROW_EXCEPTION_COMMON);
3135 /* noreturn */
3136
3137 ArmLIR *target = newLIR0(cUnit, kArmPseudoTargetLabel);
3138 target->defMask = ENCODE_ALL;
3139 branchOver->generic.target = (LIR *) target;
3140
Bill Buzbee1465db52009-09-23 17:17:35 -07003141 genRegCopy(cUnit, r1, r8);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003142
Ben Cheng38329f52009-07-07 14:19:20 -07003143 /* Check if rechain limit is reached */
buzbee8f8109a2010-08-31 10:16:35 -07003144 ArmLIR *bypassRechaining = genCmpImmBranch(cUnit, kArmCondGt,
3145 r1, 0);
Ben Cheng38329f52009-07-07 14:19:20 -07003146
Ben Chengaf5aa1f2011-01-04 15:37:04 -08003147 LOAD_FUNC_ADDR(cUnit, r7, (int) dvmJitToPatchPredictedChain);
Ben Cheng38329f52009-07-07 14:19:20 -07003148
Ben Chengb88ec3c2010-05-17 12:50:33 -07003149 genRegCopy(cUnit, r1, rGLUE);
Bill Buzbee1465db52009-09-23 17:17:35 -07003150 genRegCopy(cUnit, r2, r9);
3151 genRegCopy(cUnit, r3, r10);
Ben Cheng38329f52009-07-07 14:19:20 -07003152
3153 /*
3154 * r0 = calleeMethod
3155 * r2 = &predictedChainingCell
3156 * r3 = class
3157 *
3158 * &returnChainingCell has been loaded into r1 but is not needed
3159 * when patching the chaining cell and will be clobbered upon
3160 * returning so it will be reconstructed again.
3161 */
Bill Buzbee1465db52009-09-23 17:17:35 -07003162 opReg(cUnit, kOpBlx, r7);
Ben Cheng38329f52009-07-07 14:19:20 -07003163
3164 /* r1 = &retChainingCell */
Bill Buzbee1465db52009-09-23 17:17:35 -07003165 addrRetChain = opRegRegImm(cUnit, kOpAdd, r1, rpc, 0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003166 addrRetChain->generic.target = (LIR *) retChainingCell;
Ben Cheng38329f52009-07-07 14:19:20 -07003167
3168 bypassRechaining->generic.target = (LIR *) addrRetChain;
3169
Ben Chengba4fc8b2009-06-01 13:00:29 -07003170 /*
3171 * r0 = this, r1 = calleeMethod,
3172 * r1 = &ChainingCell,
3173 * r4PC = callsiteDPC,
3174 */
buzbee18fba342011-01-19 15:31:15 -08003175 genDispatchToHandler(cUnit, gDvmJit.methodTraceSupport ?
3176 TEMPLATE_INVOKE_METHOD_NO_OPT_PROF :
3177 TEMPLATE_INVOKE_METHOD_NO_OPT);
Ben Cheng978738d2010-05-13 13:45:57 -07003178#if defined(WITH_JIT_TUNING)
Ben Cheng86717f72010-03-05 15:27:21 -08003179 gDvmJit.invokePolymorphic++;
Ben Chengba4fc8b2009-06-01 13:00:29 -07003180#endif
3181 /* Handle exceptions using the interpreter */
3182 genTrap(cUnit, mir->offset, pcrLabel);
3183 break;
3184 }
3185 /* NOP */
3186 case OP_INVOKE_DIRECT_EMPTY: {
buzbee18fba342011-01-19 15:31:15 -08003187 if (gDvmJit.methodTraceSupport)
3188 genInterpSingleStep(cUnit, mir);
3189 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07003190 }
3191 case OP_FILLED_NEW_ARRAY:
jeffhao71eee1f2011-01-04 14:18:54 -08003192 case OP_FILLED_NEW_ARRAY_RANGE:
3193 case OP_FILLED_NEW_ARRAY_JUMBO: {
Ben Chengba4fc8b2009-06-01 13:00:29 -07003194 /* Just let the interpreter deal with these */
3195 genInterpSingleStep(cUnit, mir);
3196 break;
3197 }
3198 default:
3199 return true;
3200 }
3201 return false;
3202}
3203
Ben Chengcfdeca32011-01-14 11:36:46 -08003204/* "this" pointer is already in r0 */
3205static void genValidationForMethodCallee(CompilationUnit *cUnit, MIR *mir,
3206 ArmLIR **classCheck)
3207{
3208 CallsiteInfo *callsiteInfo = mir->meta.callsiteInfo;
3209 dvmCompilerLockAllTemps(cUnit);
3210
3211 loadConstant(cUnit, r1, (int) callsiteInfo->clazz);
3212
3213 loadWordDisp(cUnit, r0, offsetof(Object, clazz), r2);
3214 /* Branch to the slow path if classes are not equal */
3215 opRegReg(cUnit, kOpCmp, r1, r2);
3216 /*
3217 * Set the misPredBranchOver target so that it will be generated when the
3218 * code for the non-optimized invoke is generated.
3219 */
3220 *classCheck = opCondBranch(cUnit, kArmCondNe);
3221}
3222
Ben Chengba4fc8b2009-06-01 13:00:29 -07003223static bool handleFmt35ms_3rms(CompilationUnit *cUnit, MIR *mir,
Bill Buzbee89efc3d2009-07-28 11:22:22 -07003224 BasicBlock *bb, ArmLIR *labelList)
Ben Chengba4fc8b2009-06-01 13:00:29 -07003225{
Bill Buzbee89efc3d2009-07-28 11:22:22 -07003226 ArmLIR *pcrLabel = NULL;
Ben Chengba4fc8b2009-06-01 13:00:29 -07003227
Ben Cheng7a2697d2010-06-07 13:44:23 -07003228 /* An invoke with the MIR_INLINED is effectively a no-op */
3229 if (mir->OptimizationFlags & MIR_INLINED)
3230 return false;
3231
Ben Chengba4fc8b2009-06-01 13:00:29 -07003232 DecodedInstruction *dInsn = &mir->dalvikInsn;
Dan Bornstein9a1f8162010-12-01 17:02:26 -08003233 switch (mir->dalvikInsn.opcode) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07003234 /* calleeMethod = this->clazz->vtable[BBBB] */
3235 case OP_INVOKE_VIRTUAL_QUICK_RANGE:
3236 case OP_INVOKE_VIRTUAL_QUICK: {
3237 int methodIndex = dInsn->vB;
Bill Buzbeea8589332010-12-27 09:31:21 -08003238 ArmLIR *retChainingCell = &labelList[bb->fallThrough->id];
3239 ArmLIR *predChainingCell = &labelList[bb->taken->id];
Ben Cheng7a2697d2010-06-07 13:44:23 -07003240
3241 /*
3242 * If the invoke has non-null misPredBranchOver, we need to generate
3243 * the non-inlined version of the invoke here to handle the
3244 * mispredicted case.
3245 */
3246 if (mir->meta.callsiteInfo->misPredBranchOver) {
3247 genLandingPadForMispredictedCallee(cUnit, mir, bb, labelList);
3248 }
3249
Dan Bornstein9a1f8162010-12-01 17:02:26 -08003250 if (mir->dalvikInsn.opcode == OP_INVOKE_VIRTUAL_QUICK)
Ben Chengba4fc8b2009-06-01 13:00:29 -07003251 genProcessArgsNoRange(cUnit, mir, dInsn, &pcrLabel);
3252 else
3253 genProcessArgsRange(cUnit, mir, dInsn, &pcrLabel);
3254
Ben Chengcfdeca32011-01-14 11:36:46 -08003255
3256 if (mir->OptimizationFlags & MIR_INVOKE_METHOD_JIT) {
3257 const Method *calleeMethod = mir->meta.callsiteInfo->method;
3258 void *calleeAddr = dvmJitGetMethodAddr(calleeMethod->insns);
3259 if (calleeAddr) {
3260 ArmLIR *classCheck;
3261 cUnit->printMe = true;
3262 genValidationForMethodCallee(cUnit, mir, &classCheck);
3263 newLIR2(cUnit, kThumbBl1, (int) calleeAddr,
3264 (int) calleeAddr);
3265 newLIR2(cUnit, kThumbBl2, (int) calleeAddr,
3266 (int) calleeAddr);
3267 genUnconditionalBranch(cUnit, retChainingCell);
3268
3269 /* Target of slow path */
3270 ArmLIR *slowPathLabel = newLIR0(cUnit,
3271 kArmPseudoTargetLabel);
3272
3273 slowPathLabel->defMask = ENCODE_ALL;
3274 classCheck->generic.target = (LIR *) slowPathLabel;
3275 }
3276 }
3277
Ben Cheng38329f52009-07-07 14:19:20 -07003278 genInvokeVirtualCommon(cUnit, mir, methodIndex,
3279 retChainingCell,
3280 predChainingCell,
3281 pcrLabel);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003282 break;
3283 }
3284 /* calleeMethod = method->clazz->super->vtable[BBBB] */
3285 case OP_INVOKE_SUPER_QUICK:
3286 case OP_INVOKE_SUPER_QUICK_RANGE: {
Ben Cheng7a2697d2010-06-07 13:44:23 -07003287 /* Grab the method ptr directly from what the interpreter sees */
3288 const Method *calleeMethod = mir->meta.callsiteInfo->method;
3289 assert(calleeMethod ==
3290 cUnit->method->clazz->super->vtable[dInsn->vB]);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003291
Dan Bornstein9a1f8162010-12-01 17:02:26 -08003292 if (mir->dalvikInsn.opcode == OP_INVOKE_SUPER_QUICK)
Ben Chengba4fc8b2009-06-01 13:00:29 -07003293 genProcessArgsNoRange(cUnit, mir, dInsn, &pcrLabel);
3294 else
3295 genProcessArgsRange(cUnit, mir, dInsn, &pcrLabel);
3296
3297 /* r0 = calleeMethod */
3298 loadConstant(cUnit, r0, (int) calleeMethod);
3299
Ben Cheng38329f52009-07-07 14:19:20 -07003300 genInvokeSingletonCommon(cUnit, mir, bb, labelList, pcrLabel,
3301 calleeMethod);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003302 break;
3303 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07003304 default:
3305 return true;
3306 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07003307 return false;
3308}
3309
3310/*
Bill Buzbeefd023aa2009-11-02 09:23:49 -08003311 * This operation is complex enough that we'll do it partly inline
3312 * and partly with a handler. NOTE: the handler uses hardcoded
3313 * values for string object offsets and must be revisitied if the
3314 * layout changes.
3315 */
3316static bool genInlinedCompareTo(CompilationUnit *cUnit, MIR *mir)
3317{
3318#if defined(USE_GLOBAL_STRING_DEFS)
Elliott Hughes7e914f12011-01-19 18:18:42 -08003319 return handleExecuteInlineC(cUnit, mir);
Bill Buzbeefd023aa2009-11-02 09:23:49 -08003320#else
3321 ArmLIR *rollback;
Bill Buzbeec6f10662010-02-09 11:16:15 -08003322 RegLocation rlThis = dvmCompilerGetSrc(cUnit, mir, 0);
3323 RegLocation rlComp = dvmCompilerGetSrc(cUnit, mir, 1);
Bill Buzbeefd023aa2009-11-02 09:23:49 -08003324
3325 loadValueDirectFixed(cUnit, rlThis, r0);
3326 loadValueDirectFixed(cUnit, rlComp, r1);
3327 /* Test objects for NULL */
3328 rollback = genNullCheck(cUnit, rlThis.sRegLow, r0, mir->offset, NULL);
3329 genNullCheck(cUnit, rlComp.sRegLow, r1, mir->offset, rollback);
3330 /*
3331 * TUNING: we could check for object pointer equality before invoking
3332 * handler. Unclear whether the gain would be worth the added code size
3333 * expansion.
3334 */
3335 genDispatchToHandler(cUnit, TEMPLATE_STRING_COMPARETO);
Bill Buzbeec6f10662010-02-09 11:16:15 -08003336 storeValue(cUnit, inlinedTarget(cUnit, mir, false),
3337 dvmCompilerGetReturn(cUnit));
Elliott Hughes7e914f12011-01-19 18:18:42 -08003338 return false;
Bill Buzbeefd023aa2009-11-02 09:23:49 -08003339#endif
3340}
3341
Elliott Hughes2bdbcb62010-04-12 14:29:37 -07003342static bool genInlinedFastIndexOf(CompilationUnit *cUnit, MIR *mir)
Bill Buzbeefd023aa2009-11-02 09:23:49 -08003343{
3344#if defined(USE_GLOBAL_STRING_DEFS)
Elliott Hughes7e914f12011-01-19 18:18:42 -08003345 return handleExecuteInlineC(cUnit, mir);
Bill Buzbeefd023aa2009-11-02 09:23:49 -08003346#else
Bill Buzbeec6f10662010-02-09 11:16:15 -08003347 RegLocation rlThis = dvmCompilerGetSrc(cUnit, mir, 0);
3348 RegLocation rlChar = dvmCompilerGetSrc(cUnit, mir, 1);
Bill Buzbeefd023aa2009-11-02 09:23:49 -08003349
3350 loadValueDirectFixed(cUnit, rlThis, r0);
3351 loadValueDirectFixed(cUnit, rlChar, r1);
Elliott Hughes2bdbcb62010-04-12 14:29:37 -07003352 RegLocation rlStart = dvmCompilerGetSrc(cUnit, mir, 2);
3353 loadValueDirectFixed(cUnit, rlStart, r2);
Bill Buzbeefd023aa2009-11-02 09:23:49 -08003354 /* Test objects for NULL */
3355 genNullCheck(cUnit, rlThis.sRegLow, r0, mir->offset, NULL);
3356 genDispatchToHandler(cUnit, TEMPLATE_STRING_INDEXOF);
Bill Buzbeec6f10662010-02-09 11:16:15 -08003357 storeValue(cUnit, inlinedTarget(cUnit, mir, false),
3358 dvmCompilerGetReturn(cUnit));
Elliott Hughes7e914f12011-01-19 18:18:42 -08003359 return false;
Bill Buzbeefd023aa2009-11-02 09:23:49 -08003360#endif
3361}
3362
Elliott Hughesee34f592010-04-05 18:13:52 -07003363// Generates an inlined String.isEmpty or String.length.
3364static bool genInlinedStringIsEmptyOrLength(CompilationUnit *cUnit, MIR *mir,
3365 bool isEmpty)
Bill Buzbee1f748632010-03-02 16:14:41 -08003366{
Elliott Hughesee34f592010-04-05 18:13:52 -07003367 // dst = src.length();
Bill Buzbee1f748632010-03-02 16:14:41 -08003368 RegLocation rlObj = dvmCompilerGetSrc(cUnit, mir, 0);
3369 RegLocation rlDest = inlinedTarget(cUnit, mir, false);
3370 rlObj = loadValue(cUnit, rlObj, kCoreReg);
3371 RegLocation rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
3372 genNullCheck(cUnit, rlObj.sRegLow, rlObj.lowReg, mir->offset, NULL);
3373 loadWordDisp(cUnit, rlObj.lowReg, gDvm.offJavaLangString_count,
3374 rlResult.lowReg);
Elliott Hughesee34f592010-04-05 18:13:52 -07003375 if (isEmpty) {
3376 // dst = (dst == 0);
3377 int tReg = dvmCompilerAllocTemp(cUnit);
3378 opRegReg(cUnit, kOpNeg, tReg, rlResult.lowReg);
3379 opRegRegReg(cUnit, kOpAdc, rlResult.lowReg, rlResult.lowReg, tReg);
3380 }
Bill Buzbee1f748632010-03-02 16:14:41 -08003381 storeValue(cUnit, rlDest, rlResult);
3382 return false;
3383}
3384
Elliott Hughesee34f592010-04-05 18:13:52 -07003385static bool genInlinedStringLength(CompilationUnit *cUnit, MIR *mir)
3386{
3387 return genInlinedStringIsEmptyOrLength(cUnit, mir, false);
3388}
3389
3390static bool genInlinedStringIsEmpty(CompilationUnit *cUnit, MIR *mir)
3391{
3392 return genInlinedStringIsEmptyOrLength(cUnit, mir, true);
3393}
3394
Bill Buzbee1f748632010-03-02 16:14:41 -08003395static bool genInlinedStringCharAt(CompilationUnit *cUnit, MIR *mir)
3396{
3397 int contents = offsetof(ArrayObject, contents);
3398 RegLocation rlObj = dvmCompilerGetSrc(cUnit, mir, 0);
3399 RegLocation rlIdx = dvmCompilerGetSrc(cUnit, mir, 1);
3400 RegLocation rlDest = inlinedTarget(cUnit, mir, false);
3401 RegLocation rlResult;
3402 rlObj = loadValue(cUnit, rlObj, kCoreReg);
3403 rlIdx = loadValue(cUnit, rlIdx, kCoreReg);
3404 int regMax = dvmCompilerAllocTemp(cUnit);
3405 int regOff = dvmCompilerAllocTemp(cUnit);
3406 int regPtr = dvmCompilerAllocTemp(cUnit);
3407 ArmLIR *pcrLabel = genNullCheck(cUnit, rlObj.sRegLow, rlObj.lowReg,
3408 mir->offset, NULL);
3409 loadWordDisp(cUnit, rlObj.lowReg, gDvm.offJavaLangString_count, regMax);
3410 loadWordDisp(cUnit, rlObj.lowReg, gDvm.offJavaLangString_offset, regOff);
3411 loadWordDisp(cUnit, rlObj.lowReg, gDvm.offJavaLangString_value, regPtr);
3412 genBoundsCheck(cUnit, rlIdx.lowReg, regMax, mir->offset, pcrLabel);
3413 dvmCompilerFreeTemp(cUnit, regMax);
3414 opRegImm(cUnit, kOpAdd, regPtr, contents);
3415 opRegReg(cUnit, kOpAdd, regOff, rlIdx.lowReg);
3416 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
3417 loadBaseIndexed(cUnit, regPtr, regOff, rlResult.lowReg, 1, kUnsignedHalf);
3418 storeValue(cUnit, rlDest, rlResult);
3419 return false;
3420}
3421
3422static bool genInlinedAbsInt(CompilationUnit *cUnit, MIR *mir)
3423{
3424 RegLocation rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
3425 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
Elliott Hughese22bd842010-08-20 18:47:36 -07003426 RegLocation rlDest = inlinedTarget(cUnit, mir, false);
Bill Buzbee1f748632010-03-02 16:14:41 -08003427 RegLocation rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
3428 int signReg = dvmCompilerAllocTemp(cUnit);
3429 /*
3430 * abs(x) = y<=x>>31, (x+y)^y.
3431 * Thumb2's IT block also yields 3 instructions, but imposes
3432 * scheduling constraints.
3433 */
3434 opRegRegImm(cUnit, kOpAsr, signReg, rlSrc.lowReg, 31);
3435 opRegRegReg(cUnit, kOpAdd, rlResult.lowReg, rlSrc.lowReg, signReg);
3436 opRegReg(cUnit, kOpXor, rlResult.lowReg, signReg);
3437 storeValue(cUnit, rlDest, rlResult);
3438 return false;
3439}
3440
3441static bool genInlinedAbsLong(CompilationUnit *cUnit, MIR *mir)
3442{
3443 RegLocation rlSrc = dvmCompilerGetSrcWide(cUnit, mir, 0, 1);
3444 RegLocation rlDest = inlinedTargetWide(cUnit, mir, false);
3445 rlSrc = loadValueWide(cUnit, rlSrc, kCoreReg);
3446 RegLocation rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
3447 int signReg = dvmCompilerAllocTemp(cUnit);
3448 /*
3449 * abs(x) = y<=x>>31, (x+y)^y.
3450 * Thumb2 IT block allows slightly shorter sequence,
3451 * but introduces a scheduling barrier. Stick with this
3452 * mechanism for now.
3453 */
3454 opRegRegImm(cUnit, kOpAsr, signReg, rlSrc.highReg, 31);
3455 opRegRegReg(cUnit, kOpAdd, rlResult.lowReg, rlSrc.lowReg, signReg);
3456 opRegRegReg(cUnit, kOpAdc, rlResult.highReg, rlSrc.highReg, signReg);
3457 opRegReg(cUnit, kOpXor, rlResult.lowReg, signReg);
3458 opRegReg(cUnit, kOpXor, rlResult.highReg, signReg);
3459 storeValueWide(cUnit, rlDest, rlResult);
3460 return false;
3461}
Bill Buzbeefd023aa2009-11-02 09:23:49 -08003462
Elliott Hughese22bd842010-08-20 18:47:36 -07003463static bool genInlinedIntFloatConversion(CompilationUnit *cUnit, MIR *mir)
3464{
3465 // Just move from source to destination...
3466 RegLocation rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
3467 RegLocation rlDest = inlinedTarget(cUnit, mir, false);
3468 storeValue(cUnit, rlDest, rlSrc);
3469 return false;
3470}
3471
3472static bool genInlinedLongDoubleConversion(CompilationUnit *cUnit, MIR *mir)
3473{
3474 // Just move from source to destination...
3475 RegLocation rlSrc = dvmCompilerGetSrcWide(cUnit, mir, 0, 1);
3476 RegLocation rlDest = inlinedTargetWide(cUnit, mir, false);
3477 storeValueWide(cUnit, rlDest, rlSrc);
3478 return false;
3479}
3480
Bill Buzbeefd023aa2009-11-02 09:23:49 -08003481/*
Elliott Hughes7e914f12011-01-19 18:18:42 -08003482 * JITs a call to a C function.
3483 * TODO: use this for faster native method invocation for simple native
3484 * methods (http://b/3069458).
3485 */
3486static bool handleExecuteInlineC(CompilationUnit *cUnit, MIR *mir)
3487{
3488 DecodedInstruction *dInsn = &mir->dalvikInsn;
3489 int operation = dInsn->vB;
3490 unsigned int i;
3491 const InlineOperation* inLineTable = dvmGetInlineOpsTable();
3492 uintptr_t fn = (int) inLineTable[operation].func;
3493 if (fn == 0) {
3494 dvmCompilerAbort(cUnit);
3495 }
3496 dvmCompilerFlushAllRegs(cUnit); /* Everything to home location */
3497 dvmCompilerClobberCallRegs(cUnit);
3498 dvmCompilerClobber(cUnit, r4PC);
3499 dvmCompilerClobber(cUnit, r7);
3500 int offset = offsetof(InterpState, retval);
3501 opRegRegImm(cUnit, kOpAdd, r4PC, rGLUE, offset);
3502 opImm(cUnit, kOpPush, (1<<r4PC) | (1<<r7));
3503 LOAD_FUNC_ADDR(cUnit, r4PC, fn);
3504 genExportPC(cUnit, mir);
3505 for (i=0; i < dInsn->vA; i++) {
3506 loadValueDirect(cUnit, dvmCompilerGetSrc(cUnit, mir, i), i);
3507 }
3508 opReg(cUnit, kOpBlx, r4PC);
3509 opRegImm(cUnit, kOpAdd, r13, 8);
3510 /* NULL? */
3511 ArmLIR *branchOver = genCmpImmBranch(cUnit, kArmCondNe, r0, 0);
3512 loadConstant(cUnit, r0, (int) (cUnit->method->insns + mir->offset));
3513 genDispatchToHandler(cUnit, TEMPLATE_THROW_EXCEPTION_COMMON);
3514 ArmLIR *target = newLIR0(cUnit, kArmPseudoTargetLabel);
3515 target->defMask = ENCODE_ALL;
3516 branchOver->generic.target = (LIR *) target;
3517 return false;
3518}
3519
3520/*
Bill Buzbeece46c942009-11-20 15:41:34 -08003521 * NOTE: Handles both range and non-range versions (arguments
3522 * have already been normalized by this point).
Ben Chengba4fc8b2009-06-01 13:00:29 -07003523 */
Bill Buzbeece46c942009-11-20 15:41:34 -08003524static bool handleExecuteInline(CompilationUnit *cUnit, MIR *mir)
Ben Chengba4fc8b2009-06-01 13:00:29 -07003525{
3526 DecodedInstruction *dInsn = &mir->dalvikInsn;
Elliott Hughes7e914f12011-01-19 18:18:42 -08003527 assert(dInsn->opcode == OP_EXECUTE_INLINE_RANGE ||
3528 dInsn->opcode == OP_EXECUTE_INLINE);
3529 switch (dInsn->vB) {
3530 case INLINE_EMPTYINLINEMETHOD:
3531 return false; /* Nop */
3532
3533 /* These ones we potentially JIT inline. */
3534 case INLINE_STRING_LENGTH:
3535 return genInlinedStringLength(cUnit, mir);
3536 case INLINE_STRING_IS_EMPTY:
3537 return genInlinedStringIsEmpty(cUnit, mir);
3538 case INLINE_MATH_ABS_INT:
3539 return genInlinedAbsInt(cUnit, mir);
3540 case INLINE_MATH_ABS_LONG:
3541 return genInlinedAbsLong(cUnit, mir);
3542 case INLINE_MATH_MIN_INT:
3543 return genInlinedMinMaxInt(cUnit, mir, true);
3544 case INLINE_MATH_MAX_INT:
3545 return genInlinedMinMaxInt(cUnit, mir, false);
3546 case INLINE_STRING_CHARAT:
3547 return genInlinedStringCharAt(cUnit, mir);
3548 case INLINE_MATH_SQRT:
3549 return genInlineSqrt(cUnit, mir);
3550 case INLINE_MATH_ABS_FLOAT:
3551 return genInlinedAbsFloat(cUnit, mir);
3552 case INLINE_MATH_ABS_DOUBLE:
3553 return genInlinedAbsDouble(cUnit, mir);
3554 case INLINE_STRING_COMPARETO:
3555 return genInlinedCompareTo(cUnit, mir);
3556 case INLINE_STRING_FASTINDEXOF_II:
3557 return genInlinedFastIndexOf(cUnit, mir);
3558 case INLINE_FLOAT_TO_RAW_INT_BITS:
3559 case INLINE_INT_BITS_TO_FLOAT:
3560 return genInlinedIntFloatConversion(cUnit, mir);
3561 case INLINE_DOUBLE_TO_RAW_LONG_BITS:
3562 case INLINE_LONG_BITS_TO_DOUBLE:
3563 return genInlinedLongDoubleConversion(cUnit, mir);
3564
3565 /*
3566 * These ones we just JIT a call to a C function for.
3567 * TODO: special-case these in the other "invoke" call paths.
3568 */
3569 case INLINE_STRING_EQUALS:
3570 case INLINE_MATH_COS:
3571 case INLINE_MATH_SIN:
3572 case INLINE_FLOAT_TO_INT_BITS:
3573 case INLINE_DOUBLE_TO_LONG_BITS:
3574 return handleExecuteInlineC(cUnit, mir);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003575 }
Elliott Hughes7e914f12011-01-19 18:18:42 -08003576 dvmCompilerAbort(cUnit);
3577 return false; // Not reachable; keeps compiler happy.
Ben Chengba4fc8b2009-06-01 13:00:29 -07003578}
3579
3580static bool handleFmt51l(CompilationUnit *cUnit, MIR *mir)
3581{
Bill Buzbee1465db52009-09-23 17:17:35 -07003582 //TUNING: We're using core regs here - not optimal when target is a double
Bill Buzbeec6f10662010-02-09 11:16:15 -08003583 RegLocation rlDest = dvmCompilerGetDestWide(cUnit, mir, 0, 1);
3584 RegLocation rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Ben Chengbd1326d2010-04-02 15:04:53 -07003585 loadConstantNoClobber(cUnit, rlResult.lowReg,
3586 mir->dalvikInsn.vB_wide & 0xFFFFFFFFUL);
3587 loadConstantNoClobber(cUnit, rlResult.highReg,
3588 (mir->dalvikInsn.vB_wide>>32) & 0xFFFFFFFFUL);
Bill Buzbee1465db52009-09-23 17:17:35 -07003589 storeValueWide(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003590 return false;
3591}
3592
Ben Chengba4fc8b2009-06-01 13:00:29 -07003593/*
3594 * The following are special processing routines that handle transfer of
3595 * controls between compiled code and the interpreter. Certain VM states like
3596 * Dalvik PC and special-purpose registers are reconstructed here.
3597 */
3598
Bill Buzbeebd047242010-05-13 13:02:53 -07003599/*
3600 * Insert a
3601 * b .+4
3602 * nop
3603 * pair at the beginning of a chaining cell. This serves as the
3604 * switch branch that selects between reverting to the interpreter or
3605 * not. Once the cell is chained to a translation, the cell will
3606 * contain a 32-bit branch. Subsequent chain/unchain operations will
3607 * then only alter that first 16-bits - the "b .+4" for unchaining,
3608 * and the restoration of the first half of the 32-bit branch for
3609 * rechaining.
3610 */
3611static void insertChainingSwitch(CompilationUnit *cUnit)
3612{
3613 ArmLIR *branch = newLIR0(cUnit, kThumbBUncond);
3614 newLIR2(cUnit, kThumbOrr, r0, r0);
3615 ArmLIR *target = newLIR0(cUnit, kArmPseudoTargetLabel);
3616 target->defMask = ENCODE_ALL;
3617 branch->generic.target = (LIR *) target;
3618}
3619
Ben Cheng1efc9c52009-06-08 18:25:27 -07003620/* Chaining cell for code that may need warmup. */
3621static void handleNormalChainingCell(CompilationUnit *cUnit,
3622 unsigned int offset)
Ben Chengba4fc8b2009-06-01 13:00:29 -07003623{
Ben Cheng11d8f142010-03-24 15:24:19 -07003624 /*
3625 * Use raw instruction constructors to guarantee that the generated
3626 * instructions fit the predefined cell size.
3627 */
Bill Buzbeebd047242010-05-13 13:02:53 -07003628 insertChainingSwitch(cUnit);
Ben Cheng11d8f142010-03-24 15:24:19 -07003629 newLIR3(cUnit, kThumbLdrRRI5, r0, rGLUE,
3630 offsetof(InterpState,
3631 jitToInterpEntries.dvmJitToInterpNormal) >> 2);
3632 newLIR1(cUnit, kThumbBlxR, r0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003633 addWordData(cUnit, (int) (cUnit->method->insns + offset), true);
3634}
3635
3636/*
Ben Cheng1efc9c52009-06-08 18:25:27 -07003637 * Chaining cell for instructions that immediately following already translated
3638 * code.
Ben Chengba4fc8b2009-06-01 13:00:29 -07003639 */
Ben Cheng1efc9c52009-06-08 18:25:27 -07003640static void handleHotChainingCell(CompilationUnit *cUnit,
3641 unsigned int offset)
Ben Chengba4fc8b2009-06-01 13:00:29 -07003642{
Ben Cheng11d8f142010-03-24 15:24:19 -07003643 /*
3644 * Use raw instruction constructors to guarantee that the generated
3645 * instructions fit the predefined cell size.
3646 */
Bill Buzbeebd047242010-05-13 13:02:53 -07003647 insertChainingSwitch(cUnit);
Ben Cheng11d8f142010-03-24 15:24:19 -07003648 newLIR3(cUnit, kThumbLdrRRI5, r0, rGLUE,
3649 offsetof(InterpState,
3650 jitToInterpEntries.dvmJitToInterpTraceSelect) >> 2);
3651 newLIR1(cUnit, kThumbBlxR, r0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003652 addWordData(cUnit, (int) (cUnit->method->insns + offset), true);
3653}
3654
Jeff Hao97319a82009-08-12 16:57:15 -07003655/* Chaining cell for branches that branch back into the same basic block */
3656static void handleBackwardBranchChainingCell(CompilationUnit *cUnit,
3657 unsigned int offset)
3658{
Ben Cheng11d8f142010-03-24 15:24:19 -07003659 /*
3660 * Use raw instruction constructors to guarantee that the generated
3661 * instructions fit the predefined cell size.
3662 */
Bill Buzbeebd047242010-05-13 13:02:53 -07003663 insertChainingSwitch(cUnit);
Bill Buzbee9c4b7c82009-09-10 10:10:38 -07003664#if defined(WITH_SELF_VERIFICATION)
Bill Buzbee1465db52009-09-23 17:17:35 -07003665 newLIR3(cUnit, kThumbLdrRRI5, r0, rGLUE,
Ben Cheng40094c12010-02-24 20:58:44 -08003666 offsetof(InterpState,
3667 jitToInterpEntries.dvmJitToInterpBackwardBranch) >> 2);
Bill Buzbee9c4b7c82009-09-10 10:10:38 -07003668#else
Bill Buzbee1465db52009-09-23 17:17:35 -07003669 newLIR3(cUnit, kThumbLdrRRI5, r0, rGLUE,
Bill Buzbee9c4b7c82009-09-10 10:10:38 -07003670 offsetof(InterpState, jitToInterpEntries.dvmJitToInterpNormal) >> 2);
3671#endif
Bill Buzbee1465db52009-09-23 17:17:35 -07003672 newLIR1(cUnit, kThumbBlxR, r0);
Jeff Hao97319a82009-08-12 16:57:15 -07003673 addWordData(cUnit, (int) (cUnit->method->insns + offset), true);
3674}
3675
Ben Chengba4fc8b2009-06-01 13:00:29 -07003676/* Chaining cell for monomorphic method invocations. */
Ben Cheng38329f52009-07-07 14:19:20 -07003677static void handleInvokeSingletonChainingCell(CompilationUnit *cUnit,
3678 const Method *callee)
Ben Chengba4fc8b2009-06-01 13:00:29 -07003679{
Ben Cheng11d8f142010-03-24 15:24:19 -07003680 /*
3681 * Use raw instruction constructors to guarantee that the generated
3682 * instructions fit the predefined cell size.
3683 */
Bill Buzbeebd047242010-05-13 13:02:53 -07003684 insertChainingSwitch(cUnit);
Ben Cheng11d8f142010-03-24 15:24:19 -07003685 newLIR3(cUnit, kThumbLdrRRI5, r0, rGLUE,
3686 offsetof(InterpState,
3687 jitToInterpEntries.dvmJitToInterpTraceSelect) >> 2);
3688 newLIR1(cUnit, kThumbBlxR, r0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003689 addWordData(cUnit, (int) (callee->insns), true);
3690}
3691
Ben Cheng38329f52009-07-07 14:19:20 -07003692/* Chaining cell for monomorphic method invocations. */
3693static void handleInvokePredictedChainingCell(CompilationUnit *cUnit)
3694{
3695
3696 /* Should not be executed in the initial state */
3697 addWordData(cUnit, PREDICTED_CHAIN_BX_PAIR_INIT, true);
3698 /* To be filled: class */
3699 addWordData(cUnit, PREDICTED_CHAIN_CLAZZ_INIT, true);
3700 /* To be filled: method */
3701 addWordData(cUnit, PREDICTED_CHAIN_METHOD_INIT, true);
3702 /*
3703 * Rechain count. The initial value of 0 here will trigger chaining upon
3704 * the first invocation of this callsite.
3705 */
3706 addWordData(cUnit, PREDICTED_CHAIN_COUNTER_INIT, true);
3707}
3708
Ben Chengba4fc8b2009-06-01 13:00:29 -07003709/* Load the Dalvik PC into r0 and jump to the specified target */
3710static void handlePCReconstruction(CompilationUnit *cUnit,
Bill Buzbee89efc3d2009-07-28 11:22:22 -07003711 ArmLIR *targetLabel)
Ben Chengba4fc8b2009-06-01 13:00:29 -07003712{
Bill Buzbee89efc3d2009-07-28 11:22:22 -07003713 ArmLIR **pcrLabel =
3714 (ArmLIR **) cUnit->pcReconstructionList.elemList;
Ben Chengba4fc8b2009-06-01 13:00:29 -07003715 int numElems = cUnit->pcReconstructionList.numUsed;
3716 int i;
3717 for (i = 0; i < numElems; i++) {
3718 dvmCompilerAppendLIR(cUnit, (LIR *) pcrLabel[i]);
3719 /* r0 = dalvik PC */
3720 loadConstant(cUnit, r0, pcrLabel[i]->operands[0]);
3721 genUnconditionalBranch(cUnit, targetLabel);
3722 }
3723}
3724
Bill Buzbee1465db52009-09-23 17:17:35 -07003725static char *extendedMIROpNames[kMirOpLast - kMirOpFirst] = {
3726 "kMirOpPhi",
3727 "kMirOpNullNRangeUpCheck",
3728 "kMirOpNullNRangeDownCheck",
3729 "kMirOpLowerBound",
3730 "kMirOpPunt",
Ben Cheng7a2697d2010-06-07 13:44:23 -07003731 "kMirOpCheckInlinePrediction",
Ben Cheng4238ec22009-08-24 16:32:22 -07003732};
3733
3734/*
3735 * vA = arrayReg;
3736 * vB = idxReg;
3737 * vC = endConditionReg;
3738 * arg[0] = maxC
3739 * arg[1] = minC
3740 * arg[2] = loopBranchConditionCode
3741 */
3742static void genHoistedChecksForCountUpLoop(CompilationUnit *cUnit, MIR *mir)
3743{
Bill Buzbee1465db52009-09-23 17:17:35 -07003744 /*
3745 * NOTE: these synthesized blocks don't have ssa names assigned
3746 * for Dalvik registers. However, because they dominate the following
3747 * blocks we can simply use the Dalvik name w/ subscript 0 as the
3748 * ssa name.
3749 */
Ben Cheng4238ec22009-08-24 16:32:22 -07003750 DecodedInstruction *dInsn = &mir->dalvikInsn;
3751 const int lenOffset = offsetof(ArrayObject, length);
Ben Cheng4238ec22009-08-24 16:32:22 -07003752 const int maxC = dInsn->arg[0];
Bill Buzbee1465db52009-09-23 17:17:35 -07003753 int regLength;
3754 RegLocation rlArray = cUnit->regLocation[mir->dalvikInsn.vA];
3755 RegLocation rlIdxEnd = cUnit->regLocation[mir->dalvikInsn.vC];
Ben Cheng4238ec22009-08-24 16:32:22 -07003756
3757 /* regArray <- arrayRef */
Bill Buzbee1465db52009-09-23 17:17:35 -07003758 rlArray = loadValue(cUnit, rlArray, kCoreReg);
3759 rlIdxEnd = loadValue(cUnit, rlIdxEnd, kCoreReg);
3760 genRegImmCheck(cUnit, kArmCondEq, rlArray.lowReg, 0, 0,
Ben Cheng4238ec22009-08-24 16:32:22 -07003761 (ArmLIR *) cUnit->loopAnalysis->branchToPCR);
3762
3763 /* regLength <- len(arrayRef) */
Bill Buzbeec6f10662010-02-09 11:16:15 -08003764 regLength = dvmCompilerAllocTemp(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07003765 loadWordDisp(cUnit, rlArray.lowReg, lenOffset, regLength);
Ben Cheng4238ec22009-08-24 16:32:22 -07003766
3767 int delta = maxC;
3768 /*
3769 * If the loop end condition is ">=" instead of ">", then the largest value
3770 * of the index is "endCondition - 1".
3771 */
3772 if (dInsn->arg[2] == OP_IF_GE) {
3773 delta--;
3774 }
3775
3776 if (delta) {
Bill Buzbeec6f10662010-02-09 11:16:15 -08003777 int tReg = dvmCompilerAllocTemp(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07003778 opRegRegImm(cUnit, kOpAdd, tReg, rlIdxEnd.lowReg, delta);
3779 rlIdxEnd.lowReg = tReg;
Bill Buzbeec6f10662010-02-09 11:16:15 -08003780 dvmCompilerFreeTemp(cUnit, tReg);
Ben Cheng4238ec22009-08-24 16:32:22 -07003781 }
3782 /* Punt if "regIdxEnd < len(Array)" is false */
Bill Buzbee1465db52009-09-23 17:17:35 -07003783 genRegRegCheck(cUnit, kArmCondGe, rlIdxEnd.lowReg, regLength, 0,
Ben Cheng0fd31e42009-09-03 14:40:16 -07003784 (ArmLIR *) cUnit->loopAnalysis->branchToPCR);
Ben Cheng4238ec22009-08-24 16:32:22 -07003785}
3786
3787/*
3788 * vA = arrayReg;
3789 * vB = idxReg;
3790 * vC = endConditionReg;
3791 * arg[0] = maxC
3792 * arg[1] = minC
3793 * arg[2] = loopBranchConditionCode
3794 */
3795static void genHoistedChecksForCountDownLoop(CompilationUnit *cUnit, MIR *mir)
3796{
3797 DecodedInstruction *dInsn = &mir->dalvikInsn;
3798 const int lenOffset = offsetof(ArrayObject, length);
Bill Buzbeec6f10662010-02-09 11:16:15 -08003799 const int regLength = dvmCompilerAllocTemp(cUnit);
Ben Cheng4238ec22009-08-24 16:32:22 -07003800 const int maxC = dInsn->arg[0];
Bill Buzbee1465db52009-09-23 17:17:35 -07003801 RegLocation rlArray = cUnit->regLocation[mir->dalvikInsn.vA];
3802 RegLocation rlIdxInit = cUnit->regLocation[mir->dalvikInsn.vB];
Ben Cheng4238ec22009-08-24 16:32:22 -07003803
3804 /* regArray <- arrayRef */
Bill Buzbee1465db52009-09-23 17:17:35 -07003805 rlArray = loadValue(cUnit, rlArray, kCoreReg);
3806 rlIdxInit = loadValue(cUnit, rlIdxInit, kCoreReg);
3807 genRegImmCheck(cUnit, kArmCondEq, rlArray.lowReg, 0, 0,
Ben Cheng4238ec22009-08-24 16:32:22 -07003808 (ArmLIR *) cUnit->loopAnalysis->branchToPCR);
3809
3810 /* regLength <- len(arrayRef) */
Bill Buzbee1465db52009-09-23 17:17:35 -07003811 loadWordDisp(cUnit, rlArray.lowReg, lenOffset, regLength);
Ben Cheng4238ec22009-08-24 16:32:22 -07003812
3813 if (maxC) {
Bill Buzbeec6f10662010-02-09 11:16:15 -08003814 int tReg = dvmCompilerAllocTemp(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07003815 opRegRegImm(cUnit, kOpAdd, tReg, rlIdxInit.lowReg, maxC);
3816 rlIdxInit.lowReg = tReg;
Bill Buzbeec6f10662010-02-09 11:16:15 -08003817 dvmCompilerFreeTemp(cUnit, tReg);
Ben Cheng4238ec22009-08-24 16:32:22 -07003818 }
3819
3820 /* Punt if "regIdxInit < len(Array)" is false */
Bill Buzbee1465db52009-09-23 17:17:35 -07003821 genRegRegCheck(cUnit, kArmCondGe, rlIdxInit.lowReg, regLength, 0,
Ben Cheng0fd31e42009-09-03 14:40:16 -07003822 (ArmLIR *) cUnit->loopAnalysis->branchToPCR);
Ben Cheng4238ec22009-08-24 16:32:22 -07003823}
3824
3825/*
3826 * vA = idxReg;
3827 * vB = minC;
3828 */
3829static void genHoistedLowerBoundCheck(CompilationUnit *cUnit, MIR *mir)
3830{
3831 DecodedInstruction *dInsn = &mir->dalvikInsn;
Ben Cheng4238ec22009-08-24 16:32:22 -07003832 const int minC = dInsn->vB;
Bill Buzbee1465db52009-09-23 17:17:35 -07003833 RegLocation rlIdx = cUnit->regLocation[mir->dalvikInsn.vA];
Ben Cheng4238ec22009-08-24 16:32:22 -07003834
3835 /* regIdx <- initial index value */
Bill Buzbee1465db52009-09-23 17:17:35 -07003836 rlIdx = loadValue(cUnit, rlIdx, kCoreReg);
Ben Cheng4238ec22009-08-24 16:32:22 -07003837
3838 /* Punt if "regIdxInit + minC >= 0" is false */
Bill Buzbee1465db52009-09-23 17:17:35 -07003839 genRegImmCheck(cUnit, kArmCondLt, rlIdx.lowReg, -minC, 0,
Ben Cheng4238ec22009-08-24 16:32:22 -07003840 (ArmLIR *) cUnit->loopAnalysis->branchToPCR);
3841}
3842
Ben Cheng7a2697d2010-06-07 13:44:23 -07003843/*
3844 * vC = this
3845 *
3846 * A predicted inlining target looks like the following, where instructions
3847 * between 0x4858de66 and 0x4858de72 are checking if the predicted class
3848 * matches "this", and the verificaion code is generated by this routine.
3849 *
3850 * (C) means the instruction is inlined from the callee, and (PI) means the
3851 * instruction is the predicted inlined invoke, whose corresponding
3852 * instructions are still generated to handle the mispredicted case.
3853 *
3854 * D/dalvikvm( 86): -------- kMirOpCheckInlinePrediction
3855 * D/dalvikvm( 86): 0x4858de66 (0002): ldr r0, [r5, #68]
3856 * D/dalvikvm( 86): 0x4858de68 (0004): ldr r1, [pc, #140]
3857 * D/dalvikvm( 86): 0x4858de6a (0006): cmp r0, #0
3858 * D/dalvikvm( 86): 0x4858de6c (0008): beq 0x4858deb2
3859 * D/dalvikvm( 86): 0x4858de6e (000a): ldr r2, [r0, #0]
3860 * D/dalvikvm( 86): 0x4858de70 (000c): cmp r1, r2
3861 * D/dalvikvm( 86): 0x4858de72 (000e): bne 0x4858de7a
3862 * D/dalvikvm( 86): -------- dalvik offset: 0x004c @ +iget-object-quick (C)
3863 * v4, v17, (#8)
3864 * D/dalvikvm( 86): 0x4858de74 (0010): ldr r3, [r0, #8]
3865 * D/dalvikvm( 86): 0x4858de76 (0012): str r3, [r5, #16]
3866 * D/dalvikvm( 86): -------- dalvik offset: 0x004c @
3867 * +invoke-virtual-quick/range (PI) v17..v17
3868 * D/dalvikvm( 86): 0x4858de78 (0014): b 0x4858debc
3869 * D/dalvikvm( 86): 0x4858de7a (0016): add r4,r5,#68
3870 * D/dalvikvm( 86): -------- BARRIER
3871 * D/dalvikvm( 86): 0x4858de7e (001a): ldmia r4, <r0>
3872 * D/dalvikvm( 86): -------- BARRIER
3873 * D/dalvikvm( 86): 0x4858de80 (001c): sub r7,r5,#24
3874 * D/dalvikvm( 86): 0x4858de84 (0020): cmp r0, #0
3875 * D/dalvikvm( 86): 0x4858de86 (0022): beq 0x4858deb6
3876 * D/dalvikvm( 86): -------- BARRIER
3877 * D/dalvikvm( 86): 0x4858de88 (0024): stmia r7, <r0>
3878 * D/dalvikvm( 86): -------- BARRIER
3879 * D/dalvikvm( 86): 0x4858de8a (0026): ldr r4, [pc, #104]
3880 * D/dalvikvm( 86): 0x4858de8c (0028): add r1, pc, #28
3881 * D/dalvikvm( 86): 0x4858de8e (002a): add r2, pc, #56
3882 * D/dalvikvm( 86): 0x4858de90 (002c): blx_1 0x48589198
3883 * D/dalvikvm( 86): 0x4858de92 (002e): blx_2 see above
3884 * D/dalvikvm( 86): 0x4858de94 (0030): b 0x4858dec8
3885 * D/dalvikvm( 86): 0x4858de96 (0032): b 0x4858deb6
3886 * D/dalvikvm( 86): 0x4858de98 (0034): ldr r0, [r7, #72]
3887 * D/dalvikvm( 86): 0x4858de9a (0036): cmp r1, #0
3888 * D/dalvikvm( 86): 0x4858de9c (0038): bgt 0x4858dea4
3889 * D/dalvikvm( 86): 0x4858de9e (003a): ldr r7, [r6, #116]
3890 * D/dalvikvm( 86): 0x4858dea0 (003c): movs r1, r6
3891 * D/dalvikvm( 86): 0x4858dea2 (003e): blx r7
3892 * D/dalvikvm( 86): 0x4858dea4 (0040): add r1, pc, #4
3893 * D/dalvikvm( 86): 0x4858dea6 (0042): blx_1 0x485890a0
3894 * D/dalvikvm( 86): 0x4858dea8 (0044): blx_2 see above
3895 * D/dalvikvm( 86): 0x4858deaa (0046): b 0x4858deb6
3896 * D/dalvikvm( 86): 0x4858deac (0048): .align4
3897 * D/dalvikvm( 86): L0x004f:
3898 * D/dalvikvm( 86): -------- dalvik offset: 0x004f @ move-result-object (PI)
3899 * v4, (#0), (#0)
3900 * D/dalvikvm( 86): 0x4858deac (0048): ldr r4, [r6, #8]
3901 * D/dalvikvm( 86): 0x4858deae (004a): str r4, [r5, #16]
3902 * D/dalvikvm( 86): 0x4858deb0 (004c): b 0x4858debc
3903 * D/dalvikvm( 86): -------- reconstruct dalvik PC : 0x42beefcc @ +0x004c
3904 * D/dalvikvm( 86): 0x4858deb2 (004e): ldr r0, [pc, #64]
3905 * D/dalvikvm( 86): 0x4858deb4 (0050): b 0x4858deb8
3906 * D/dalvikvm( 86): -------- reconstruct dalvik PC : 0x42beefcc @ +0x004c
3907 * D/dalvikvm( 86): 0x4858deb6 (0052): ldr r0, [pc, #60]
3908 * D/dalvikvm( 86): Exception_Handling:
3909 * D/dalvikvm( 86): 0x4858deb8 (0054): ldr r1, [r6, #100]
3910 * D/dalvikvm( 86): 0x4858deba (0056): blx r1
3911 * D/dalvikvm( 86): 0x4858debc (0058): .align4
3912 * D/dalvikvm( 86): -------- chaining cell (hot): 0x0050
3913 * D/dalvikvm( 86): 0x4858debc (0058): b 0x4858dec0
3914 * D/dalvikvm( 86): 0x4858debe (005a): orrs r0, r0
3915 * D/dalvikvm( 86): 0x4858dec0 (005c): ldr r0, [r6, #112]
3916 * D/dalvikvm( 86): 0x4858dec2 (005e): blx r0
3917 * D/dalvikvm( 86): 0x4858dec4 (0060): data 0xefd4(61396)
3918 * D/dalvikvm( 86): 0x4858dec6 (0062): data 0x42be(17086)
3919 * D/dalvikvm( 86): 0x4858dec8 (0064): .align4
3920 * D/dalvikvm( 86): -------- chaining cell (predicted)
3921 * D/dalvikvm( 86): 0x4858dec8 (0064): data 0xe7fe(59390)
3922 * D/dalvikvm( 86): 0x4858deca (0066): data 0x0000(0)
3923 * D/dalvikvm( 86): 0x4858decc (0068): data 0x0000(0)
3924 * D/dalvikvm( 86): 0x4858dece (006a): data 0x0000(0)
3925 * :
3926 */
3927static void genValidationForPredictedInline(CompilationUnit *cUnit, MIR *mir)
3928{
3929 CallsiteInfo *callsiteInfo = mir->meta.callsiteInfo;
3930 RegLocation rlThis = cUnit->regLocation[mir->dalvikInsn.vC];
3931
3932 rlThis = loadValue(cUnit, rlThis, kCoreReg);
3933 int regPredictedClass = dvmCompilerAllocTemp(cUnit);
3934 loadConstant(cUnit, regPredictedClass, (int) callsiteInfo->clazz);
3935 genNullCheck(cUnit, rlThis.sRegLow, rlThis.lowReg, mir->offset,
3936 NULL);/* null object? */
3937 int regActualClass = dvmCompilerAllocTemp(cUnit);
3938 loadWordDisp(cUnit, rlThis.lowReg, offsetof(Object, clazz), regActualClass);
3939 opRegReg(cUnit, kOpCmp, regPredictedClass, regActualClass);
3940 /*
3941 * Set the misPredBranchOver target so that it will be generated when the
3942 * code for the non-optimized invoke is generated.
3943 */
3944 callsiteInfo->misPredBranchOver = (LIR *) opCondBranch(cUnit, kArmCondNe);
3945}
3946
Ben Cheng4238ec22009-08-24 16:32:22 -07003947/* Extended MIR instructions like PHI */
3948static void handleExtendedMIR(CompilationUnit *cUnit, MIR *mir)
3949{
Dan Bornstein9a1f8162010-12-01 17:02:26 -08003950 int opOffset = mir->dalvikInsn.opcode - kMirOpFirst;
Carl Shapirofc75f3e2010-12-07 11:43:38 -08003951 char *msg = (char *)dvmCompilerNew(strlen(extendedMIROpNames[opOffset]) + 1,
3952 false);
Ben Cheng4238ec22009-08-24 16:32:22 -07003953 strcpy(msg, extendedMIROpNames[opOffset]);
Bill Buzbee1465db52009-09-23 17:17:35 -07003954 newLIR1(cUnit, kArmPseudoExtended, (int) msg);
Ben Cheng4238ec22009-08-24 16:32:22 -07003955
Dan Bornstein9a1f8162010-12-01 17:02:26 -08003956 switch (mir->dalvikInsn.opcode) {
Bill Buzbee1465db52009-09-23 17:17:35 -07003957 case kMirOpPhi: {
Ben Cheng4238ec22009-08-24 16:32:22 -07003958 char *ssaString = dvmCompilerGetSSAString(cUnit, mir->ssaRep);
Bill Buzbee1465db52009-09-23 17:17:35 -07003959 newLIR1(cUnit, kArmPseudoSSARep, (int) ssaString);
Ben Cheng4238ec22009-08-24 16:32:22 -07003960 break;
3961 }
Bill Buzbee1465db52009-09-23 17:17:35 -07003962 case kMirOpNullNRangeUpCheck: {
Ben Cheng4238ec22009-08-24 16:32:22 -07003963 genHoistedChecksForCountUpLoop(cUnit, mir);
3964 break;
3965 }
Bill Buzbee1465db52009-09-23 17:17:35 -07003966 case kMirOpNullNRangeDownCheck: {
Ben Cheng4238ec22009-08-24 16:32:22 -07003967 genHoistedChecksForCountDownLoop(cUnit, mir);
3968 break;
3969 }
Bill Buzbee1465db52009-09-23 17:17:35 -07003970 case kMirOpLowerBound: {
Ben Cheng4238ec22009-08-24 16:32:22 -07003971 genHoistedLowerBoundCheck(cUnit, mir);
3972 break;
3973 }
Bill Buzbee1465db52009-09-23 17:17:35 -07003974 case kMirOpPunt: {
Ben Cheng4238ec22009-08-24 16:32:22 -07003975 genUnconditionalBranch(cUnit,
3976 (ArmLIR *) cUnit->loopAnalysis->branchToPCR);
3977 break;
3978 }
Ben Cheng7a2697d2010-06-07 13:44:23 -07003979 case kMirOpCheckInlinePrediction: {
3980 genValidationForPredictedInline(cUnit, mir);
3981 break;
3982 }
Ben Cheng4238ec22009-08-24 16:32:22 -07003983 default:
3984 break;
3985 }
3986}
3987
3988/*
3989 * Create a PC-reconstruction cell for the starting offset of this trace.
3990 * Since the PCR cell is placed near the end of the compiled code which is
3991 * usually out of range for a conditional branch, we put two branches (one
3992 * branch over to the loop body and one layover branch to the actual PCR) at the
3993 * end of the entry block.
3994 */
3995static void setupLoopEntryBlock(CompilationUnit *cUnit, BasicBlock *entry,
3996 ArmLIR *bodyLabel)
3997{
3998 /* Set up the place holder to reconstruct this Dalvik PC */
Carl Shapirofc75f3e2010-12-07 11:43:38 -08003999 ArmLIR *pcrLabel = (ArmLIR *) dvmCompilerNew(sizeof(ArmLIR), true);
Dan Bornstein9a1f8162010-12-01 17:02:26 -08004000 pcrLabel->opcode = kArmPseudoPCReconstructionCell;
Ben Cheng4238ec22009-08-24 16:32:22 -07004001 pcrLabel->operands[0] =
4002 (int) (cUnit->method->insns + entry->startOffset);
4003 pcrLabel->operands[1] = entry->startOffset;
4004 /* Insert the place holder to the growable list */
Ben Cheng00603072010-10-28 11:13:58 -07004005 dvmInsertGrowableList(&cUnit->pcReconstructionList, (intptr_t) pcrLabel);
Ben Cheng4238ec22009-08-24 16:32:22 -07004006
4007 /*
4008 * Next, create two branches - one branch over to the loop body and the
4009 * other branch to the PCR cell to punt.
4010 */
Carl Shapirofc75f3e2010-12-07 11:43:38 -08004011 ArmLIR *branchToBody = (ArmLIR *) dvmCompilerNew(sizeof(ArmLIR), true);
Dan Bornstein9a1f8162010-12-01 17:02:26 -08004012 branchToBody->opcode = kThumbBUncond;
Ben Cheng4238ec22009-08-24 16:32:22 -07004013 branchToBody->generic.target = (LIR *) bodyLabel;
Ben Chengdcf3e5d2009-09-11 13:42:05 -07004014 setupResourceMasks(branchToBody);
Ben Cheng4238ec22009-08-24 16:32:22 -07004015 cUnit->loopAnalysis->branchToBody = (LIR *) branchToBody;
4016
Carl Shapirofc75f3e2010-12-07 11:43:38 -08004017 ArmLIR *branchToPCR = (ArmLIR *) dvmCompilerNew(sizeof(ArmLIR), true);
Dan Bornstein9a1f8162010-12-01 17:02:26 -08004018 branchToPCR->opcode = kThumbBUncond;
Ben Cheng4238ec22009-08-24 16:32:22 -07004019 branchToPCR->generic.target = (LIR *) pcrLabel;
Ben Chengdcf3e5d2009-09-11 13:42:05 -07004020 setupResourceMasks(branchToPCR);
Ben Cheng4238ec22009-08-24 16:32:22 -07004021 cUnit->loopAnalysis->branchToPCR = (LIR *) branchToPCR;
4022}
4023
Ben Chengd5adae12010-03-26 17:45:28 -07004024#if defined(WITH_SELF_VERIFICATION)
4025static bool selfVerificationPuntOps(MIR *mir)
4026{
4027 DecodedInstruction *decInsn = &mir->dalvikInsn;
Dan Bornstein9a1f8162010-12-01 17:02:26 -08004028 Opcode op = decInsn->opcode;
Ben Cheng7a2697d2010-06-07 13:44:23 -07004029
Ben Chengd5adae12010-03-26 17:45:28 -07004030 /*
4031 * All opcodes that can throw exceptions and use the
4032 * TEMPLATE_THROW_EXCEPTION_COMMON template should be excluded in the trace
4033 * under self-verification mode.
4034 */
4035 return (op == OP_MONITOR_ENTER || op == OP_MONITOR_EXIT ||
4036 op == OP_NEW_INSTANCE || op == OP_NEW_ARRAY ||
4037 op == OP_CHECK_CAST || op == OP_MOVE_EXCEPTION ||
4038 op == OP_FILL_ARRAY_DATA || op == OP_EXECUTE_INLINE ||
Ben Cheng7a2697d2010-06-07 13:44:23 -07004039 op == OP_EXECUTE_INLINE_RANGE);
Ben Chengd5adae12010-03-26 17:45:28 -07004040}
4041#endif
4042
Ben Chengba4fc8b2009-06-01 13:00:29 -07004043void dvmCompilerMIR2LIR(CompilationUnit *cUnit)
4044{
4045 /* Used to hold the labels of each block */
Bill Buzbee89efc3d2009-07-28 11:22:22 -07004046 ArmLIR *labelList =
Carl Shapirofc75f3e2010-12-07 11:43:38 -08004047 (ArmLIR *) dvmCompilerNew(sizeof(ArmLIR) * cUnit->numBlocks, true);
Ben Chengcec26f62010-01-15 15:29:33 -08004048 GrowableList chainingListByType[kChainingCellGap];
Ben Chengba4fc8b2009-06-01 13:00:29 -07004049 int i;
4050
4051 /*
Ben Cheng38329f52009-07-07 14:19:20 -07004052 * Initialize various types chaining lists.
Ben Chengba4fc8b2009-06-01 13:00:29 -07004053 */
Ben Chengcec26f62010-01-15 15:29:33 -08004054 for (i = 0; i < kChainingCellGap; i++) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07004055 dvmInitGrowableList(&chainingListByType[i], 2);
4056 }
4057
Ben Cheng00603072010-10-28 11:13:58 -07004058 GrowableListIterator iterator;
4059 dvmGrowableListIteratorInit(&cUnit->blockList, &iterator);
Ben Chengba4fc8b2009-06-01 13:00:29 -07004060
buzbee2e152ba2010-12-15 16:32:35 -08004061 /* Traces start with a profiling entry point. Generate it here */
4062 cUnit->profileCodeSize = genTraceProfileEntry(cUnit);
Ben Cheng1efc9c52009-06-08 18:25:27 -07004063
Ben Chengba4fc8b2009-06-01 13:00:29 -07004064 /* Handle the content in each basic block */
Ben Cheng00603072010-10-28 11:13:58 -07004065 for (i = 0; ; i++) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07004066 MIR *mir;
Ben Cheng00603072010-10-28 11:13:58 -07004067 BasicBlock *bb = (BasicBlock *) dvmGrowableListIteratorNext(&iterator);
4068 if (bb == NULL) break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07004069
Ben Cheng00603072010-10-28 11:13:58 -07004070 labelList[i].operands[0] = bb->startOffset;
Ben Chengba4fc8b2009-06-01 13:00:29 -07004071
Ben Cheng00603072010-10-28 11:13:58 -07004072 if (bb->blockType >= kChainingCellGap) {
4073 if (bb->isFallThroughFromInvoke == true) {
Ben Chengd44faf52010-06-02 15:33:51 -07004074 /* Align this block first since it is a return chaining cell */
4075 newLIR0(cUnit, kArmPseudoPseudoAlign4);
4076 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07004077 /*
4078 * Append the label pseudo LIR first. Chaining cells will be handled
4079 * separately afterwards.
4080 */
4081 dvmCompilerAppendLIR(cUnit, (LIR *) &labelList[i]);
4082 }
4083
Ben Cheng00603072010-10-28 11:13:58 -07004084 if (bb->blockType == kTraceEntryBlock) {
Dan Bornstein9a1f8162010-12-01 17:02:26 -08004085 labelList[i].opcode = kArmPseudoEntryBlock;
Ben Cheng00603072010-10-28 11:13:58 -07004086 if (bb->firstMIRInsn == NULL) {
Ben Cheng4238ec22009-08-24 16:32:22 -07004087 continue;
4088 } else {
Ben Cheng00603072010-10-28 11:13:58 -07004089 setupLoopEntryBlock(cUnit, bb,
4090 &labelList[bb->fallThrough->id]);
Ben Cheng4238ec22009-08-24 16:32:22 -07004091 }
Ben Cheng00603072010-10-28 11:13:58 -07004092 } else if (bb->blockType == kTraceExitBlock) {
Dan Bornstein9a1f8162010-12-01 17:02:26 -08004093 labelList[i].opcode = kArmPseudoExitBlock;
Ben Cheng4238ec22009-08-24 16:32:22 -07004094 goto gen_fallthrough;
Ben Cheng00603072010-10-28 11:13:58 -07004095 } else if (bb->blockType == kDalvikByteCode) {
Dan Bornstein9a1f8162010-12-01 17:02:26 -08004096 labelList[i].opcode = kArmPseudoNormalBlockLabel;
Ben Chenge9695e52009-06-16 16:11:47 -07004097 /* Reset the register state */
Bill Buzbeec6f10662010-02-09 11:16:15 -08004098 dvmCompilerResetRegPool(cUnit);
4099 dvmCompilerClobberAllRegs(cUnit);
4100 dvmCompilerResetNullCheck(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -07004101 } else {
Ben Cheng00603072010-10-28 11:13:58 -07004102 switch (bb->blockType) {
Bill Buzbee1465db52009-09-23 17:17:35 -07004103 case kChainingCellNormal:
Dan Bornstein9a1f8162010-12-01 17:02:26 -08004104 labelList[i].opcode = kArmPseudoChainingCellNormal;
Ben Chengba4fc8b2009-06-01 13:00:29 -07004105 /* handle the codegen later */
4106 dvmInsertGrowableList(
Ben Cheng00603072010-10-28 11:13:58 -07004107 &chainingListByType[kChainingCellNormal], i);
Ben Chengba4fc8b2009-06-01 13:00:29 -07004108 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07004109 case kChainingCellInvokeSingleton:
Dan Bornstein9a1f8162010-12-01 17:02:26 -08004110 labelList[i].opcode =
Ben Chenga4973592010-03-31 11:59:18 -07004111 kArmPseudoChainingCellInvokeSingleton;
Ben Chengba4fc8b2009-06-01 13:00:29 -07004112 labelList[i].operands[0] =
Ben Cheng00603072010-10-28 11:13:58 -07004113 (int) bb->containingMethod;
Ben Chengba4fc8b2009-06-01 13:00:29 -07004114 /* handle the codegen later */
4115 dvmInsertGrowableList(
Ben Cheng00603072010-10-28 11:13:58 -07004116 &chainingListByType[kChainingCellInvokeSingleton], i);
Ben Cheng38329f52009-07-07 14:19:20 -07004117 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07004118 case kChainingCellInvokePredicted:
Dan Bornstein9a1f8162010-12-01 17:02:26 -08004119 labelList[i].opcode =
Ben Chenga4973592010-03-31 11:59:18 -07004120 kArmPseudoChainingCellInvokePredicted;
Ben Cheng38329f52009-07-07 14:19:20 -07004121 /* handle the codegen later */
4122 dvmInsertGrowableList(
Ben Cheng00603072010-10-28 11:13:58 -07004123 &chainingListByType[kChainingCellInvokePredicted], i);
Ben Chengba4fc8b2009-06-01 13:00:29 -07004124 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07004125 case kChainingCellHot:
Dan Bornstein9a1f8162010-12-01 17:02:26 -08004126 labelList[i].opcode =
Ben Chenga4973592010-03-31 11:59:18 -07004127 kArmPseudoChainingCellHot;
Ben Chengba4fc8b2009-06-01 13:00:29 -07004128 /* handle the codegen later */
4129 dvmInsertGrowableList(
Ben Cheng00603072010-10-28 11:13:58 -07004130 &chainingListByType[kChainingCellHot], i);
Ben Chengba4fc8b2009-06-01 13:00:29 -07004131 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07004132 case kPCReconstruction:
Ben Chengba4fc8b2009-06-01 13:00:29 -07004133 /* Make sure exception handling block is next */
Dan Bornstein9a1f8162010-12-01 17:02:26 -08004134 labelList[i].opcode =
Ben Chenga4973592010-03-31 11:59:18 -07004135 kArmPseudoPCReconstructionBlockLabel;
Ben Chengba4fc8b2009-06-01 13:00:29 -07004136 assert (i == cUnit->numBlocks - 2);
4137 handlePCReconstruction(cUnit, &labelList[i+1]);
4138 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07004139 case kExceptionHandling:
Dan Bornstein9a1f8162010-12-01 17:02:26 -08004140 labelList[i].opcode = kArmPseudoEHBlockLabel;
Ben Chengba4fc8b2009-06-01 13:00:29 -07004141 if (cUnit->pcReconstructionList.numUsed) {
Bill Buzbee270c1d62009-08-13 16:58:07 -07004142 loadWordDisp(cUnit, rGLUE, offsetof(InterpState,
4143 jitToInterpEntries.dvmJitToInterpPunt),
4144 r1);
Bill Buzbee1465db52009-09-23 17:17:35 -07004145 opReg(cUnit, kOpBlx, r1);
Ben Chengba4fc8b2009-06-01 13:00:29 -07004146 }
4147 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07004148 case kChainingCellBackwardBranch:
Dan Bornstein9a1f8162010-12-01 17:02:26 -08004149 labelList[i].opcode =
Ben Chenga4973592010-03-31 11:59:18 -07004150 kArmPseudoChainingCellBackwardBranch;
Jeff Hao97319a82009-08-12 16:57:15 -07004151 /* handle the codegen later */
4152 dvmInsertGrowableList(
Bill Buzbee1465db52009-09-23 17:17:35 -07004153 &chainingListByType[kChainingCellBackwardBranch],
Ben Cheng00603072010-10-28 11:13:58 -07004154 i);
Jeff Hao97319a82009-08-12 16:57:15 -07004155 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07004156 default:
4157 break;
4158 }
4159 continue;
4160 }
Ben Chenge9695e52009-06-16 16:11:47 -07004161
Bill Buzbee89efc3d2009-07-28 11:22:22 -07004162 ArmLIR *headLIR = NULL;
Ben Chenge9695e52009-06-16 16:11:47 -07004163
Ben Cheng00603072010-10-28 11:13:58 -07004164 for (mir = bb->firstMIRInsn; mir; mir = mir->next) {
Bill Buzbee1465db52009-09-23 17:17:35 -07004165
Bill Buzbeec6f10662010-02-09 11:16:15 -08004166 dvmCompilerResetRegPool(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07004167 if (gDvmJit.disableOpt & (1 << kTrackLiveTemps)) {
Bill Buzbeec6f10662010-02-09 11:16:15 -08004168 dvmCompilerClobberAllRegs(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07004169 }
4170
4171 if (gDvmJit.disableOpt & (1 << kSuppressLoads)) {
Bill Buzbeec6f10662010-02-09 11:16:15 -08004172 dvmCompilerResetDefTracking(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07004173 }
4174
Dan Bornstein9a1f8162010-12-01 17:02:26 -08004175 if (mir->dalvikInsn.opcode >= kMirOpFirst) {
Ben Cheng4238ec22009-08-24 16:32:22 -07004176 handleExtendedMIR(cUnit, mir);
4177 continue;
4178 }
4179
Bill Buzbee1465db52009-09-23 17:17:35 -07004180
Dan Bornstein9a1f8162010-12-01 17:02:26 -08004181 Opcode dalvikOpcode = mir->dalvikInsn.opcode;
Dan Bornsteine4852762010-12-02 12:45:00 -08004182 InstructionFormat dalvikFormat = dexGetFormatFromOpcode(dalvikOpcode);
Ben Cheng7a2697d2010-06-07 13:44:23 -07004183 char *note;
4184 if (mir->OptimizationFlags & MIR_INLINED) {
4185 note = " (I)";
4186 } else if (mir->OptimizationFlags & MIR_INLINED_PRED) {
4187 note = " (PI)";
4188 } else if (mir->OptimizationFlags & MIR_CALLEE) {
4189 note = " (C)";
4190 } else {
4191 note = NULL;
4192 }
4193
Ben Cheng80211d22011-01-14 10:23:37 -08004194 ArmLIR *boundaryLIR;
4195
4196 /*
4197 * Don't generate the boundary LIR unless we are debugging this
4198 * trace or we need a scheduling barrier.
4199 */
4200 if (headLIR == NULL || cUnit->printMe == true) {
4201 boundaryLIR =
4202 newLIR2(cUnit, kArmPseudoDalvikByteCodeBoundary,
4203 mir->offset,
4204 (int) dvmCompilerGetDalvikDisassembly(
4205 &mir->dalvikInsn, note));
4206 /* Remember the first LIR for this block */
4207 if (headLIR == NULL) {
4208 headLIR = boundaryLIR;
4209 /* Set the first boundaryLIR as a scheduling barrier */
4210 headLIR->defMask = ENCODE_ALL;
4211 }
Ben Cheng4238ec22009-08-24 16:32:22 -07004212 }
4213
Ben Cheng80211d22011-01-14 10:23:37 -08004214 /* Don't generate the SSA annotation unless verbose mode is on */
4215 if (cUnit->printMe && mir->ssaRep) {
4216 char *ssaString = dvmCompilerGetSSAString(cUnit, mir->ssaRep);
4217 newLIR1(cUnit, kArmPseudoSSARep, (int) ssaString);
Ben Chenge9695e52009-06-16 16:11:47 -07004218 }
Ben Cheng4238ec22009-08-24 16:32:22 -07004219
Ben Chengba4fc8b2009-06-01 13:00:29 -07004220 bool notHandled;
4221 /*
4222 * Debugging: screen the opcode first to see if it is in the
4223 * do[-not]-compile list
4224 */
Dan Bornstein9a1f8162010-12-01 17:02:26 -08004225 bool singleStepMe = SINGLE_STEP_OP(dalvikOpcode);
Ben Chengd5adae12010-03-26 17:45:28 -07004226#if defined(WITH_SELF_VERIFICATION)
4227 if (singleStepMe == false) {
4228 singleStepMe = selfVerificationPuntOps(mir);
4229 }
4230#endif
Ben Chengba4fc8b2009-06-01 13:00:29 -07004231 if (singleStepMe || cUnit->allSingleStep) {
4232 notHandled = false;
4233 genInterpSingleStep(cUnit, mir);
4234 } else {
Dan Bornstein9a1f8162010-12-01 17:02:26 -08004235 opcodeCoverage[dalvikOpcode]++;
Ben Chengba4fc8b2009-06-01 13:00:29 -07004236 switch (dalvikFormat) {
4237 case kFmt10t:
4238 case kFmt20t:
4239 case kFmt30t:
4240 notHandled = handleFmt10t_Fmt20t_Fmt30t(cUnit,
Ben Cheng00603072010-10-28 11:13:58 -07004241 mir, bb, labelList);
Ben Chengba4fc8b2009-06-01 13:00:29 -07004242 break;
4243 case kFmt10x:
4244 notHandled = handleFmt10x(cUnit, mir);
4245 break;
4246 case kFmt11n:
4247 case kFmt31i:
4248 notHandled = handleFmt11n_Fmt31i(cUnit, mir);
4249 break;
4250 case kFmt11x:
4251 notHandled = handleFmt11x(cUnit, mir);
4252 break;
4253 case kFmt12x:
4254 notHandled = handleFmt12x(cUnit, mir);
4255 break;
4256 case kFmt20bc:
jeffhao71eee1f2011-01-04 14:18:54 -08004257 case kFmt40sc:
4258 notHandled = handleFmt20bc_Fmt40sc(cUnit, mir);
Ben Chengba4fc8b2009-06-01 13:00:29 -07004259 break;
4260 case kFmt21c:
4261 case kFmt31c:
jeffhao71eee1f2011-01-04 14:18:54 -08004262 case kFmt41c:
4263 notHandled = handleFmt21c_Fmt31c_Fmt41c(cUnit, mir);
Ben Chengba4fc8b2009-06-01 13:00:29 -07004264 break;
4265 case kFmt21h:
4266 notHandled = handleFmt21h(cUnit, mir);
4267 break;
4268 case kFmt21s:
4269 notHandled = handleFmt21s(cUnit, mir);
4270 break;
4271 case kFmt21t:
Ben Cheng00603072010-10-28 11:13:58 -07004272 notHandled = handleFmt21t(cUnit, mir, bb, labelList);
Ben Chengba4fc8b2009-06-01 13:00:29 -07004273 break;
4274 case kFmt22b:
4275 case kFmt22s:
4276 notHandled = handleFmt22b_Fmt22s(cUnit, mir);
4277 break;
4278 case kFmt22c:
jeffhao71eee1f2011-01-04 14:18:54 -08004279 case kFmt52c:
4280 notHandled = handleFmt22c_Fmt52c(cUnit, mir);
Ben Chengba4fc8b2009-06-01 13:00:29 -07004281 break;
4282 case kFmt22cs:
4283 notHandled = handleFmt22cs(cUnit, mir);
4284 break;
4285 case kFmt22t:
Ben Cheng00603072010-10-28 11:13:58 -07004286 notHandled = handleFmt22t(cUnit, mir, bb, labelList);
Ben Chengba4fc8b2009-06-01 13:00:29 -07004287 break;
4288 case kFmt22x:
4289 case kFmt32x:
4290 notHandled = handleFmt22x_Fmt32x(cUnit, mir);
4291 break;
4292 case kFmt23x:
4293 notHandled = handleFmt23x(cUnit, mir);
4294 break;
4295 case kFmt31t:
4296 notHandled = handleFmt31t(cUnit, mir);
4297 break;
4298 case kFmt3rc:
4299 case kFmt35c:
jeffhao71eee1f2011-01-04 14:18:54 -08004300 case kFmt5rc:
4301 notHandled = handleFmt35c_3rc_5rc(cUnit, mir, bb,
Ben Chengba4fc8b2009-06-01 13:00:29 -07004302 labelList);
4303 break;
4304 case kFmt3rms:
4305 case kFmt35ms:
Ben Cheng00603072010-10-28 11:13:58 -07004306 notHandled = handleFmt35ms_3rms(cUnit, mir, bb,
Ben Chengba4fc8b2009-06-01 13:00:29 -07004307 labelList);
4308 break;
Dan Bornstein7b3e9b02010-11-09 17:15:10 -08004309 case kFmt35mi:
4310 case kFmt3rmi:
Bill Buzbeece46c942009-11-20 15:41:34 -08004311 notHandled = handleExecuteInline(cUnit, mir);
Andy McFaddenb0a05412009-11-19 10:23:41 -08004312 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07004313 case kFmt51l:
4314 notHandled = handleFmt51l(cUnit, mir);
4315 break;
4316 default:
4317 notHandled = true;
4318 break;
4319 }
4320 }
4321 if (notHandled) {
4322 LOGE("%#06x: Opcode 0x%x (%s) / Fmt %d not handled\n",
4323 mir->offset,
Dan Bornstein9a1f8162010-12-01 17:02:26 -08004324 dalvikOpcode, dexGetOpcodeName(dalvikOpcode),
Ben Chengba4fc8b2009-06-01 13:00:29 -07004325 dalvikFormat);
Bill Buzbeefc519dc2010-03-06 23:30:57 -08004326 dvmCompilerAbort(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -07004327 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07004328 }
4329 }
Ben Cheng4238ec22009-08-24 16:32:22 -07004330
Ben Cheng00603072010-10-28 11:13:58 -07004331 if (bb->blockType == kTraceEntryBlock) {
Ben Cheng4238ec22009-08-24 16:32:22 -07004332 dvmCompilerAppendLIR(cUnit,
4333 (LIR *) cUnit->loopAnalysis->branchToBody);
4334 dvmCompilerAppendLIR(cUnit,
4335 (LIR *) cUnit->loopAnalysis->branchToPCR);
4336 }
4337
4338 if (headLIR) {
4339 /*
4340 * Eliminate redundant loads/stores and delay stores into later
4341 * slots
4342 */
4343 dvmCompilerApplyLocalOptimizations(cUnit, (LIR *) headLIR,
4344 cUnit->lastLIRInsn);
4345 }
4346
4347gen_fallthrough:
Ben Cheng1efc9c52009-06-08 18:25:27 -07004348 /*
4349 * Check if the block is terminated due to trace length constraint -
4350 * insert an unconditional branch to the chaining cell.
4351 */
Ben Cheng00603072010-10-28 11:13:58 -07004352 if (bb->needFallThroughBranch) {
Ben Cheng1efc9c52009-06-08 18:25:27 -07004353 genUnconditionalBranch(cUnit,
Ben Cheng00603072010-10-28 11:13:58 -07004354 &labelList[bb->fallThrough->id]);
Ben Cheng1efc9c52009-06-08 18:25:27 -07004355 }
4356
Ben Chengba4fc8b2009-06-01 13:00:29 -07004357 }
4358
Ben Chenge9695e52009-06-16 16:11:47 -07004359 /* Handle the chaining cells in predefined order */
Ben Chengcec26f62010-01-15 15:29:33 -08004360 for (i = 0; i < kChainingCellGap; i++) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07004361 size_t j;
4362 int *blockIdList = (int *) chainingListByType[i].elemList;
4363
4364 cUnit->numChainingCells[i] = chainingListByType[i].numUsed;
4365
4366 /* No chaining cells of this type */
4367 if (cUnit->numChainingCells[i] == 0)
4368 continue;
4369
4370 /* Record the first LIR for a new type of chaining cell */
4371 cUnit->firstChainingLIR[i] = (LIR *) &labelList[blockIdList[0]];
4372
4373 for (j = 0; j < chainingListByType[i].numUsed; j++) {
4374 int blockId = blockIdList[j];
Ben Cheng00603072010-10-28 11:13:58 -07004375 BasicBlock *chainingBlock =
4376 (BasicBlock *) dvmGrowableListGetElement(&cUnit->blockList,
4377 blockId);
Ben Chengba4fc8b2009-06-01 13:00:29 -07004378
4379 /* Align this chaining cell first */
Bill Buzbee1465db52009-09-23 17:17:35 -07004380 newLIR0(cUnit, kArmPseudoPseudoAlign4);
Ben Chengba4fc8b2009-06-01 13:00:29 -07004381
4382 /* Insert the pseudo chaining instruction */
4383 dvmCompilerAppendLIR(cUnit, (LIR *) &labelList[blockId]);
4384
4385
Ben Cheng00603072010-10-28 11:13:58 -07004386 switch (chainingBlock->blockType) {
Bill Buzbee1465db52009-09-23 17:17:35 -07004387 case kChainingCellNormal:
Ben Cheng00603072010-10-28 11:13:58 -07004388 handleNormalChainingCell(cUnit, chainingBlock->startOffset);
Ben Chengba4fc8b2009-06-01 13:00:29 -07004389 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07004390 case kChainingCellInvokeSingleton:
Ben Cheng38329f52009-07-07 14:19:20 -07004391 handleInvokeSingletonChainingCell(cUnit,
Ben Cheng00603072010-10-28 11:13:58 -07004392 chainingBlock->containingMethod);
Ben Chengba4fc8b2009-06-01 13:00:29 -07004393 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07004394 case kChainingCellInvokePredicted:
Ben Cheng38329f52009-07-07 14:19:20 -07004395 handleInvokePredictedChainingCell(cUnit);
4396 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07004397 case kChainingCellHot:
Ben Cheng00603072010-10-28 11:13:58 -07004398 handleHotChainingCell(cUnit, chainingBlock->startOffset);
Ben Chengba4fc8b2009-06-01 13:00:29 -07004399 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07004400 case kChainingCellBackwardBranch:
Jeff Hao97319a82009-08-12 16:57:15 -07004401 handleBackwardBranchChainingCell(cUnit,
Ben Cheng00603072010-10-28 11:13:58 -07004402 chainingBlock->startOffset);
Jeff Hao97319a82009-08-12 16:57:15 -07004403 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07004404 default:
Ben Cheng00603072010-10-28 11:13:58 -07004405 LOGE("Bad blocktype %d", chainingBlock->blockType);
Bill Buzbeefc519dc2010-03-06 23:30:57 -08004406 dvmCompilerAbort(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -07004407 }
4408 }
4409 }
Ben Chenge9695e52009-06-16 16:11:47 -07004410
Ben Chengcec26f62010-01-15 15:29:33 -08004411 /* Mark the bottom of chaining cells */
4412 cUnit->chainingCellBottom = (LIR *) newLIR0(cUnit, kArmChainingCellBottom);
4413
Ben Cheng6c10a972009-10-29 14:39:18 -07004414 /*
4415 * Generate the branch to the dvmJitToInterpNoChain entry point at the end
4416 * of all chaining cells for the overflow cases.
4417 */
4418 if (cUnit->switchOverflowPad) {
4419 loadConstant(cUnit, r0, (int) cUnit->switchOverflowPad);
4420 loadWordDisp(cUnit, rGLUE, offsetof(InterpState,
4421 jitToInterpEntries.dvmJitToInterpNoChain), r2);
4422 opRegReg(cUnit, kOpAdd, r1, r1);
4423 opRegRegReg(cUnit, kOpAdd, r4PC, r0, r1);
Ben Cheng978738d2010-05-13 13:45:57 -07004424#if defined(WITH_JIT_TUNING)
Ben Cheng6c10a972009-10-29 14:39:18 -07004425 loadConstant(cUnit, r0, kSwitchOverflow);
4426#endif
4427 opReg(cUnit, kOpBlx, r2);
4428 }
4429
Ben Chenge9695e52009-06-16 16:11:47 -07004430 dvmCompilerApplyGlobalOptimizations(cUnit);
jeffhao9e45c0b2010-02-03 10:24:05 -08004431
4432#if defined(WITH_SELF_VERIFICATION)
4433 selfVerificationBranchInsertPass(cUnit);
4434#endif
Ben Chengba4fc8b2009-06-01 13:00:29 -07004435}
4436
buzbee2e152ba2010-12-15 16:32:35 -08004437/*
4438 * Accept the work and start compiling. Returns true if compilation
4439 * is attempted.
4440 */
Bill Buzbee716f1202009-07-23 13:22:09 -07004441bool dvmCompilerDoWork(CompilerWorkOrder *work)
Ben Chengba4fc8b2009-06-01 13:00:29 -07004442{
Carl Shapirofc75f3e2010-12-07 11:43:38 -08004443 JitTraceDescription *desc;
buzbee2e152ba2010-12-15 16:32:35 -08004444 bool isCompile;
4445 bool success = true;
Ben Chengba4fc8b2009-06-01 13:00:29 -07004446
Ben Cheng6999d842010-01-26 16:46:15 -08004447 if (gDvmJit.codeCacheFull) {
Ben Chengccd6c012009-10-15 14:52:45 -07004448 return false;
4449 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07004450
Ben Chengccd6c012009-10-15 14:52:45 -07004451 switch (work->kind) {
Ben Chengccd6c012009-10-15 14:52:45 -07004452 case kWorkOrderTrace:
buzbee2e152ba2010-12-15 16:32:35 -08004453 isCompile = true;
Ben Chengccd6c012009-10-15 14:52:45 -07004454 /* Start compilation with maximally allowed trace length */
Carl Shapirofc75f3e2010-12-07 11:43:38 -08004455 desc = (JitTraceDescription *)work->info;
buzbee2e152ba2010-12-15 16:32:35 -08004456 success = dvmCompileTrace(desc, JIT_MAX_TRACE_LEN, &work->result,
4457 work->bailPtr, 0 /* no hints */);
Ben Chengccd6c012009-10-15 14:52:45 -07004458 break;
4459 case kWorkOrderTraceDebug: {
4460 bool oldPrintMe = gDvmJit.printMe;
4461 gDvmJit.printMe = true;
buzbee2e152ba2010-12-15 16:32:35 -08004462 isCompile = true;
Ben Chengccd6c012009-10-15 14:52:45 -07004463 /* Start compilation with maximally allowed trace length */
Carl Shapirofc75f3e2010-12-07 11:43:38 -08004464 desc = (JitTraceDescription *)work->info;
buzbee2e152ba2010-12-15 16:32:35 -08004465 success = dvmCompileTrace(desc, JIT_MAX_TRACE_LEN, &work->result,
4466 work->bailPtr, 0 /* no hints */);
Elliott Hughes672511b2010-04-26 17:40:13 -07004467 gDvmJit.printMe = oldPrintMe;
Ben Chengccd6c012009-10-15 14:52:45 -07004468 break;
4469 }
buzbee2e152ba2010-12-15 16:32:35 -08004470 case kWorkOrderProfileMode:
4471 dvmJitChangeProfileMode((TraceProfilingModes)work->info);
4472 isCompile = false;
4473 break;
Ben Chengccd6c012009-10-15 14:52:45 -07004474 default:
buzbee2e152ba2010-12-15 16:32:35 -08004475 isCompile = false;
Bill Buzbeefc519dc2010-03-06 23:30:57 -08004476 LOGE("Jit: unknown work order type");
Elliott Hughes672511b2010-04-26 17:40:13 -07004477 assert(0); // Bail if debug build, discard otherwise
Ben Chengccd6c012009-10-15 14:52:45 -07004478 }
buzbee2e152ba2010-12-15 16:32:35 -08004479 if (!success)
4480 work->result.codeAddress = NULL;
4481 return isCompile;
Ben Chengba4fc8b2009-06-01 13:00:29 -07004482}
4483
Ben Chengba4fc8b2009-06-01 13:00:29 -07004484/* Architectural-specific debugging helpers go here */
4485void dvmCompilerArchDump(void)
4486{
4487 /* Print compiled opcode in this VM instance */
4488 int i, start, streak;
4489 char buf[1024];
4490
4491 streak = i = 0;
4492 buf[0] = 0;
Dan Bornsteinccaab182010-12-03 15:32:40 -08004493 while (opcodeCoverage[i] == 0 && i < kNumPackedOpcodes) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07004494 i++;
4495 }
Dan Bornsteinccaab182010-12-03 15:32:40 -08004496 if (i == kNumPackedOpcodes) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07004497 return;
4498 }
Dan Bornsteinccaab182010-12-03 15:32:40 -08004499 for (start = i++, streak = 1; i < kNumPackedOpcodes; i++) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07004500 if (opcodeCoverage[i]) {
4501 streak++;
4502 } else {
4503 if (streak == 1) {
4504 sprintf(buf+strlen(buf), "%x,", start);
4505 } else {
4506 sprintf(buf+strlen(buf), "%x-%x,", start, start + streak - 1);
4507 }
4508 streak = 0;
Dan Bornsteinccaab182010-12-03 15:32:40 -08004509 while (opcodeCoverage[i] == 0 && i < kNumPackedOpcodes) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07004510 i++;
4511 }
Dan Bornsteinccaab182010-12-03 15:32:40 -08004512 if (i < kNumPackedOpcodes) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07004513 streak = 1;
4514 start = i;
4515 }
4516 }
4517 }
4518 if (streak) {
4519 if (streak == 1) {
4520 sprintf(buf+strlen(buf), "%x", start);
4521 } else {
4522 sprintf(buf+strlen(buf), "%x-%x", start, start + streak - 1);
4523 }
4524 }
4525 if (strlen(buf)) {
Ben Cheng8b258bf2009-06-24 17:27:07 -07004526 LOGD("dalvik.vm.jit.op = %s", buf);
Ben Chengba4fc8b2009-06-01 13:00:29 -07004527 }
4528}
Ben Chengd7d426a2009-09-22 11:23:36 -07004529
4530/* Common initialization routine for an architecture family */
4531bool dvmCompilerArchInit()
4532{
4533 int i;
4534
Bill Buzbee1465db52009-09-23 17:17:35 -07004535 for (i = 0; i < kArmLast; i++) {
Dan Bornstein9a1f8162010-12-01 17:02:26 -08004536 if (EncodingMap[i].opcode != i) {
Ben Chengd7d426a2009-09-22 11:23:36 -07004537 LOGE("Encoding order for %s is wrong: expecting %d, seeing %d",
Dan Bornstein9a1f8162010-12-01 17:02:26 -08004538 EncodingMap[i].name, i, EncodingMap[i].opcode);
Bill Buzbeefc519dc2010-03-06 23:30:57 -08004539 dvmAbort(); // OK to dvmAbort - build error
Ben Chengd7d426a2009-09-22 11:23:36 -07004540 }
4541 }
4542
Ben Cheng5d90c202009-11-22 23:31:11 -08004543 return dvmCompilerArchVariantInit();
4544}
4545
4546void *dvmCompilerGetInterpretTemplate()
4547{
4548 return (void*) ((int)gDvmJit.codeCache +
4549 templateEntryOffsets[TEMPLATE_INTERPRET]);
4550}
4551
buzbeebff121a2010-08-04 15:25:06 -07004552/* Needed by the Assembler */
4553void dvmCompilerSetupResourceMasks(ArmLIR *lir)
4554{
4555 setupResourceMasks(lir);
4556}
4557
Ben Cheng5d90c202009-11-22 23:31:11 -08004558/* Needed by the ld/st optmizatons */
4559ArmLIR* dvmCompilerRegCopyNoInsert(CompilationUnit *cUnit, int rDest, int rSrc)
4560{
4561 return genRegCopyNoInsert(cUnit, rDest, rSrc);
4562}
4563
4564/* Needed by the register allocator */
4565ArmLIR* dvmCompilerRegCopy(CompilationUnit *cUnit, int rDest, int rSrc)
4566{
4567 return genRegCopy(cUnit, rDest, rSrc);
4568}
4569
4570/* Needed by the register allocator */
4571void dvmCompilerRegCopyWide(CompilationUnit *cUnit, int destLo, int destHi,
4572 int srcLo, int srcHi)
4573{
4574 genRegCopyWide(cUnit, destLo, destHi, srcLo, srcHi);
4575}
4576
4577void dvmCompilerFlushRegImpl(CompilationUnit *cUnit, int rBase,
4578 int displacement, int rSrc, OpSize size)
4579{
4580 storeBaseDisp(cUnit, rBase, displacement, rSrc, size);
4581}
4582
4583void dvmCompilerFlushRegWideImpl(CompilationUnit *cUnit, int rBase,
4584 int displacement, int rSrcLo, int rSrcHi)
4585{
4586 storeBaseDispWide(cUnit, rBase, displacement, rSrcLo, rSrcHi);
Ben Chengd7d426a2009-09-22 11:23:36 -07004587}